internal unsafe void RecordCommands(MyRenderableProxy proxy, VertexBufferId stream, int voxelMatId)
        {
            if (stream == VertexBufferId.NULL)
            {
                return;
            }

            var foliageType = MyVoxelMaterials1.Table[voxelMatId].FoliageType;

            MyMapping mapping = MyMapping.MapDiscard(RC.DeviceContext, proxy.ObjectBuffer);

            mapping.WriteAndPosition(ref proxy.NonVoxelObjectData);
            mapping.WriteAndPosition(ref proxy.CommonObjectData);
            mapping.Unmap();

            RC.SetCB(MyCommon.OBJECT_SLOT, proxy.ObjectBuffer);

            RC.SetGS(m_GS[foliageType]);
            RC.SetPS(m_PS[foliageType]);

            if (MyVoxelMaterials1.Table[voxelMatId].FoliageColorTextureArray != null)
            {
                RC.BindRawSRV(0, MyVoxelMaterials1.Table[voxelMatId].FoliageColorTextureArray.ShaderView);
                RC.BindRawSRV(1, MyVoxelMaterials1.Table[voxelMatId].FoliageNormalTextureArray.ShaderView);
            }
            else
            {
                RC.BindRawSRV(0, MyTextures.GetView(MyTextures.GetTexture(MyVoxelMaterials1.Table[voxelMatId].FoliageArray_Texture, MyTextureEnum.COLOR_METAL, true)));
                RC.BindRawSRV(1, MyTextures.GetView(MyTextures.GetTexture(MyVoxelMaterials1.Table[voxelMatId].FoliageArray_NormalTexture, MyTextureEnum.NORMALMAP_GLOSS, true)));
            }

            RC.SetVB(0, stream.Buffer, stream.Stride);
            Context.DrawAuto();
            RC.Stats.DrawAuto++;
        }
        internal unsafe void RecordCommands(MyRenderableProxy proxy, IVertexBuffer stream, int voxelMatId)
        {
            if (stream == null)
            {
                return;
            }

            var foliageType = MyVoxelMaterials1.Table[voxelMatId].FoliageType;

            MyMapping mapping = MyMapping.MapDiscard(RC, proxy.ObjectBuffer);

            mapping.WriteAndPosition(ref proxy.NonVoxelObjectData);
            mapping.WriteAndPosition(ref proxy.CommonObjectData);
            mapping.Unmap();

            RC.AllShaderStages.SetConstantBuffer(MyCommon.OBJECT_SLOT, proxy.ObjectBuffer);

            RC.GeometryShader.Set(m_GS[foliageType]);
            RC.PixelShader.Set(m_PS[foliageType]);

            if (MyVoxelMaterials1.Table[voxelMatId].FoliageColorTextureArray != null)
            {
                RC.AllShaderStages.SetSrv(0, MyVoxelMaterials1.Table[voxelMatId].FoliageColorTextureArray);
                RC.AllShaderStages.SetSrv(1, MyVoxelMaterials1.Table[voxelMatId].FoliageNormalTextureArray);
            }
            else
            {
                MyFileTextureManager texManager = MyManagers.FileTextures;
                RC.AllShaderStages.SetSrv(0, texManager.GetTexture(MyVoxelMaterials1.Table[voxelMatId].FoliageArray_Texture, MyFileTextureEnum.COLOR_METAL, true));
                RC.AllShaderStages.SetSrv(1, texManager.GetTexture(MyVoxelMaterials1.Table[voxelMatId].FoliageArray_NormalTexture, MyFileTextureEnum.NORMALMAP_GLOSS, true));
            }

            RC.SetVertexBuffer(0, stream);
            RC.DrawAuto();
        }
        internal unsafe void RecordCommands(MyRenderableProxy proxy, MyFoliageStream stream, int voxelMatId,
                                            VertexShader vertexShader, InputLayout inputLayout,
                                            int materialIndex, int indexCount, int startIndex, int baseVertex)
        {
            if (stream.m_stream == VertexBufferId.NULL)
            {
                return;
            }

            //var worldMatrix = proxy.WorldMatrix;
            //worldMatrix.Translation = Vector3D.Zero;
            //MyObjectData objectData = proxy.ObjectData;
            //objectData.LocalMatrix = Matrix.Identity;

            var worldMat = proxy.WorldMatrix;
            //worldMat.Translation -= MyEnvironment.CameraPosition;

            MyObjectDataCommon objectData = proxy.CommonObjectData;

            objectData.LocalMatrix = worldMat;

            MyMapping mapping = MyMapping.MapDiscard(RC.DeviceContext, proxy.ObjectBuffer);

            mapping.WriteAndPosition(ref proxy.VoxelCommonObjectData);
            mapping.WriteAndPosition(ref objectData);
            mapping.Unmap();

            RC.SetCB(MyCommon.OBJECT_SLOT, proxy.ObjectBuffer);

            BindProxyGeometry(proxy, RC);

            RC.SetVS(vertexShader);
            RC.SetIL(inputLayout);

            int offset = -1;

            if (!stream.Append)
            {
                offset        = 0;
                stream.Append = true;
            }

            Context.StreamOutput.SetTarget(stream.m_stream.Buffer, offset);
            RC.SetCB(MyCommon.FOLIAGE_SLOT, MyCommon.FoliageConstants);

            float densityFactor = MyVoxelMaterials1.Table[voxelMatId].FoliageDensity * MyRender11.Settings.GrassDensityFactor;

            mapping = MyMapping.MapDiscard(Context, MyCommon.FoliageConstants);
            mapping.WriteAndPosition(ref densityFactor);
            mapping.WriteAndPosition(ref materialIndex);
            mapping.WriteAndPosition(ref voxelMatId);
            mapping.Unmap();

            Context.DrawIndexed(indexCount, startIndex, baseVertex);
        }
Beispiel #4
0
        static unsafe void FillRandomVectors(MyMapping myMapping)
        {
            float maxTapMag = -1;

            for (uint i = 0; i < NUM_SAMPLES; i++)
            {
                float curr = m_filterKernel[i].Length();
                maxTapMag = (float)System.Math.Max(maxTapMag, curr);
            }


            float    maxTapMagInv          = 1.0f / maxTapMag;
            float    rsum                  = 0.0f;
            Vector4 *occluderPoints        = stackalloc Vector4[NUM_SAMPLES];
            Vector4 *occluderPointsFlipped = stackalloc Vector4[NUM_SAMPLES];

            for (uint i = 0; i < NUM_SAMPLES; i++)
            {
                Vector2 tapOffs = new Vector2(m_filterKernel[i].X * maxTapMagInv, m_filterKernel[i].Y * maxTapMagInv);

                occluderPoints[i].X = tapOffs.X;
                occluderPoints[i].Y = tapOffs.Y;
                occluderPoints[i].Z = 0;
                occluderPoints[i].W = (float)System.Math.Sqrt(1 - tapOffs.X * tapOffs.X - tapOffs.Y * tapOffs.Y);

                rsum += occluderPoints[i].W;

                //
                occluderPointsFlipped[i].X = tapOffs.X;
                occluderPointsFlipped[i].Y = -tapOffs.Y;
            }

            var colorScale = 1.0f / (2 * rsum);

            colorScale *= Params.ColorScale;

            for (int occluderIndex = 0; occluderIndex < NUM_SAMPLES; ++occluderIndex)
            {
                myMapping.WriteAndPosition(ref occluderPoints[occluderIndex]);
            }

            for (int occluderIndex = 0; occluderIndex < NUM_SAMPLES; ++occluderIndex)
            {
                myMapping.WriteAndPosition(ref occluderPointsFlipped[occluderIndex]);
            }
        }
Beispiel #5
0
        internal static void Init()
        {
            m_VSCopy = MyShaders.CreateVs("Postprocess/PostprocessCopy.hlsl");

            {
                m_VBFullscreen = MyManagers.Buffers.CreateVertexBuffer(
                    "MyScreenPass.VBFullscreen", 4, VRageRender.Vertex.MyVertexFormatPositionTextureH.STRIDE,
                    usage: ResourceUsage.Dynamic);
                m_vbData[0] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(-1, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0, 1f));
                m_vbData[1] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(-1, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0, 0));
                m_vbData[2] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(1, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(1, 1f));
                m_vbData[3] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(1, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(1, 0f));
                MyMapping mapping = MyMapping.MapDiscard(RC, m_VBFullscreen);
                mapping.WriteAndPosition(m_vbData, 4);
                mapping.Unmap();
            }

            {
                m_VBLeftPart = MyManagers.Buffers.CreateVertexBuffer(
                    "MyVRScreenPass.VBLeftPart", 4, VRageRender.Vertex.MyVertexFormatPositionTextureH.STRIDE,
                    usage: ResourceUsage.Dynamic);
                m_vbData[0] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(-1, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0, 1));
                m_vbData[1] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(-1, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0, 0));
                m_vbData[2] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(0, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0.5f, 1));
                m_vbData[3] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(0, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0.5f, 0f));
                MyMapping mapping = MyMapping.MapDiscard(RC, m_VBLeftPart);
                mapping.WriteAndPosition(m_vbData, 4);
                mapping.Unmap();
            }

            {
                m_VBRightPart = MyManagers.Buffers.CreateVertexBuffer(
                    "MyVRScreenPass.VBRightPart", 4, VRageRender.Vertex.MyVertexFormatPositionTextureH.STRIDE,
                    usage: ResourceUsage.Dynamic);
                m_vbData[0] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(0, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0.5f, 1));
                m_vbData[1] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(0, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(0.5f, 0));
                m_vbData[2] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(1, -1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(1, 1));
                m_vbData[3] = new VRageRender.Vertex.MyVertexFormatPositionTextureH(new Vector3(1, 1, 0),
                                                                                    new VRageMath.PackedVector.HalfVector2(1, 0));
                MyMapping mapping = MyMapping.MapDiscard(RC, m_VBRightPart);
                mapping.WriteAndPosition(m_vbData, 4);
                mapping.Unmap();
            }

            // just some shader bytecode is selected
            m_IL = MyShaders.CreateIL(m_VSCopy.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION3, MyVertexInputComponentType.TEXCOORD0_H));
        }
        private static void InitInternal(Vector2[] vertsForMask)
        {
            m_VB = MyHwBuffers.CreateVertexBuffer(vertsForMask.Length, MyVertexFormat2DPosition.STRIDE, BindFlags.VertexBuffer, ResourceUsage.Dynamic, null, "MyStereoStencilMask.VB");
            MyMapping mapping = MyMapping.MapDiscard(RC.DeviceContext, m_VB.Buffer);

            mapping.WriteAndPosition(vertsForMask, 0, vertsForMask.Length);
            mapping.Unmap();

            m_vs = MyShaders.CreateVs("stereo_stencil_mask.hlsl");
            m_ps = MyShaders.CreatePs("stereo_stencil_mask.hlsl");

            m_il = MyShaders.CreateIL(m_vs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION2));
        }
Beispiel #7
0
        static void FillRandomVectors(MyMapping myMapping)
        {
            float maxTapMag = -1;
            for (uint i = 0; i < NUM_SAMPLES; i++)
            {
                float curr = m_filterKernel[i].Length();
                maxTapMag = (float)System.Math.Max(maxTapMag, curr);
            }


            float maxTapMagInv = 1.0f / maxTapMag;
            float rsum = 0.0f;
            for (uint i = 0; i < NUM_SAMPLES; i++)
            {
                Vector2 tapOffs = new Vector2(m_filterKernel[i].X * maxTapMagInv, m_filterKernel[i].Y * maxTapMagInv);

                m_tmpOccluderPoints[i].X = tapOffs.X;
                m_tmpOccluderPoints[i].Y = tapOffs.Y;
                m_tmpOccluderPoints[i].Z = 0;
                m_tmpOccluderPoints[i].W = (float)System.Math.Sqrt(1 - tapOffs.X * tapOffs.X - tapOffs.Y * tapOffs.Y);

                rsum += m_tmpOccluderPointsFlipped[i].W;

                m_tmpOccluderPointsFlipped[i].X = tapOffs.X;
                m_tmpOccluderPointsFlipped[i].Y = -tapOffs.Y;
            }

            var colorScale = 1.0f / (2 * rsum);
            colorScale *= Params.Data.ColorScale;

            for (int occluderIndex = 0; occluderIndex < NUM_SAMPLES; ++occluderIndex)
                myMapping.WriteAndPosition(ref m_tmpOccluderPoints[occluderIndex]);

            for (int occluderIndex = 0; occluderIndex < NUM_SAMPLES; ++occluderIndex)
                myMapping.WriteAndPosition(ref m_tmpOccluderPointsFlipped[occluderIndex]);
        }
Beispiel #8
0
        private static void InitInternal(Vector2[] vertsForMask)
        {
            m_VB = MyManagers.Buffers.CreateVertexBuffer(
                "MyStereoStencilMask.VB", vertsForMask.Length, MyVertexFormat2DPosition.STRIDE,
                usage: ResourceUsage.Dynamic);
            MyMapping mapping = MyMapping.MapDiscard(RC, m_VB);

            mapping.WriteAndPosition(vertsForMask, vertsForMask.Length);
            mapping.Unmap();

            m_vs = MyShaders.CreateVs("Stereo/StereoStencilMask.hlsl");
            m_ps = MyShaders.CreatePs("Stereo/StereoStencilMask.hlsl");

            m_il = MyShaders.CreateIL(m_vs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION2));
        }
Beispiel #9
0
        internal static unsafe void Render(MyRenderContext RC, IDepthStencil ds, IRtvBindable rtv)
        {
            bool debugDraw = MyRender11.Settings.DrawOcclusionQueriesDebug;

            if (m_vb == null || m_tempBuffer2.Length < m_queries.Count)
            {
                int allocCount = System.Math.Max(m_queries.Count * 3 / 2, 32);
                System.Diagnostics.Debug.Assert(allocCount < 256);

                if (m_vb == null)
                {
                    m_vb = VRage.Render11.Common.MyManagers.Buffers.CreateVertexBuffer("MyOcclusionQueryRenderer.VB", allocCount,
                                                                                       sizeof(MyVbConstantElement), usage: SharpDX.Direct3D11.ResourceUsage.Dynamic);
                }
                else
                {
                    VRage.Render11.Common.MyManagers.Buffers.Resize(m_vb, allocCount);
                }
                m_tempBuffer  = new MyVbConstantElement[allocCount];
                m_tempBuffer2 = new MyOcclusionQuery[allocCount];
            }

            VRage.Profiler.ProfilerShort.Begin("Gather");
            int   ctr         = 0;
            float currentTime = MyCommon.TimerMs;

            foreach (var item in m_queries)
            {
                Vector3 cameraPos = item.Position - MyRender11.Environment.Matrices.CameraPosition;
                if (debugDraw)
                {
                    item.LastResult = 0;
                }
                else
                {
                    if (item.Running)
                    {
                        var result = item.Query.GetResult(false);
                        if (result != -1)
                        {
                            var dist    = cameraPos.Length();
                            var viewPos = new Vector3(item.Size, item.Size, dist);
                            var projPos = Vector3.Transform(viewPos, MyRender11.Environment.Matrices.Projection);
                            var pixels  = new Vector2(projPos.X, projPos.Y) * MyRender11.ResolutionF / 2;
                            var squared = System.Math.Abs(pixels.X * pixels.Y);
                            item.LastResult    = System.Math.Min(result / squared, 1.0f);
                            item.NextQueryTime = currentTime + item.FreqMinMs + m_random.NextFloat() * item.FreqRndMs;
                            item.Running       = false;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }

                if (!item.Visible || currentTime < item.NextQueryTime)
                {
                    continue;
                }

                item.Running = true;

                var data = new MyVbConstantElement
                {
                    Position = cameraPos,
                    Size     = item.Size
                };
                m_tempBuffer2[ctr] = item.Query;
                m_tempBuffer[ctr]  = data;
                ctr++;

                item.Visible = false;
            }

            if (ctr > 0)
            {
                VRage.Profiler.ProfilerShort.BeginNextBlock("Setup");
                RC.SetInputLayout(m_inputLayout);
                RC.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);

                RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);

                RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState);
                RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState);

                RC.VertexShader.Set(m_vs);
                if (debugDraw)
                {
                    RC.PixelShader.Set(m_ps);
                    RC.SetRtv(ds, MyDepthStencilAccess.ReadOnly, rtv);
                }
                else
                {
                    RC.SetRtv(ds, MyDepthStencilAccess.ReadOnly, null);
                    RC.PixelShader.Set(null);
                }

                VRage.Profiler.ProfilerShort.BeginNextBlock("Map");
                MyMapping mappingVb = MyMapping.MapDiscard(RC, m_vb);
                mappingVb.WriteAndPosition(m_tempBuffer, ctr);
                mappingVb.Unmap();

                RC.SetVertexBuffer(0, m_vb);

                VRage.Profiler.ProfilerShort.BeginNextBlock("Render");
                if (debugDraw)
                {
                    for (int i = 0; i < ctr; i++)
                    {
                        RC.DrawInstanced(4, 1, 0, i);
                    }
                }
                else
                {
                    for (int i = 0; i < ctr; i++)
                    {
                        m_tempBuffer2[i].Begin();
                        RC.DrawInstanced(4, 1, 0, i);

                        m_tempBuffer2[i].End();
                    }
                }
            }
            VRage.Profiler.ProfilerShort.End();

            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
        }
Beispiel #10
0
        static void DrawInstanceComponent(MyInstanceComponent instanceComponent, List <MyHighlightDesc> highlightDescs)
        {
            MyRenderContext RC = MyRender11.RC;

            // common settings (combination of MyHighlightPass.cs and MyRenderingPass.cs):
            MyMapping mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
            Matrix    matrix  = MyRender11.Environment.Matrices.ViewProjectionAt0;

            matrix = Matrix.Transpose(matrix);
            mapping.WriteAndPosition(ref matrix);
            mapping.Unmap();

            RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
            RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);
            //RC.AllShaderStages.SetConstantBuffer(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants); // not used! Maybe impostors?
            RC.SetDepthStencilState(MyDepthStencilStateManager.WriteHighlightStencil, MyHighlight.HIGHLIGHT_STENCIL_MASK);
            RC.SetBlendState(null);
            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState);
            RC.SetScreenViewport();

            RC.PixelShader.SetConstantBuffer(4, MyCommon.HighlightConstants);


            MyLod lod = instanceComponent.GetHighlightLod();
            MyInstanceLodState stateId = MyInstanceLodState.Solid;
            float stateData            = 0;

            RC.SetIndexBuffer(lod.IB);
            RC.SetVertexBuffer(0, lod.VB0);

            IConstantBuffer objectCB = GetObjectCB(RC, instanceComponent, stateData);

            RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB);
            RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB);

            foreach (MyHighlightDesc desc in highlightDescs)
            {
                MyHighlightDesc descRef = desc;
                WriteHighlightConstants(ref descRef);

                if (string.IsNullOrEmpty(desc.SectionName))
                {
                    foreach (var part in lod.Parts)
                    {
                        DrawHighlightedPart(RC, part, stateId);
                    }
                }
                else
                {
                    if (lod.HighlightSections != null && lod.HighlightSections.ContainsKey(desc.SectionName))
                    {
                        foreach (var part in lod.HighlightSections[desc.SectionName].Parts)
                        {
                            DrawHighlightedPart(RC, part, stateId);
                        }
                    }
                }
            }
        }