コード例 #1
0
        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 == null)
            {
                return;
            }

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

            var worldMat = proxy.WorldMatrix;
            //worldMat.Translation -= MyRender11.Environment.CameraPosition;

            MyObjectDataCommon objectData = proxy.CommonObjectData;

            objectData.LocalMatrix = worldMat;

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

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

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

            BindProxyGeometry(proxy, RC);

            RC.VertexShader.Set(vertexShader);
            RC.SetInputLayout(inputLayout);

            int offset = -1;

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

            RC.SetTarget(stream.m_stream, offset);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.FOLIAGE_SLOT, MyCommon.FoliageConstants);

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

            float zero = 0;

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

            RC.DrawIndexed(indexCount, startIndex, baseVertex);
        }
コード例 #2
0
        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();
        }
コード例 #3
0
        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++;
        }
コード例 #4
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));
        }
コード例 #5
0
        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));
        }
コード例 #6
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));
        }
コード例 #7
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);
        }
コード例 #8
0
ファイル: MyHighlight.cs プロジェクト: viktorius/Viktor
        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);
                        }
                    }
                }
            }
        }
コード例 #9
0
        internal static unsafe void Draw()
        {
            if (m_inputLayout == null)
            {
                var linesInput = MyVertexInput.Empty()
                                 .Append(MyVertexInputComponentType.POSITION_PACKED)
                                 .Append(MyVertexInputComponentType.COLOR4);

                m_inputLayout = MyVertexInput.CreateLayout(linesInput.Hash, MyShaderCache.CompileFromFile("line.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_linesVertexBuffer.Buffer, MyVertexFormatPositionColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);
            context.Rasterizer.State = MyRender.m_linesRasterizerState;

            context.VertexShader.Set(m_vertexShader.VertexShader);
            context.VertexShader.SetConstantBuffer(0, MyResources.ProjectionConstants.Buffer);

            context.PixelShader.Set(m_pixelShader.PixelShader);

            context.OutputMerger.ResetTargets();
            context.OutputMerger.SetTargets(MyRender.Backbuffer.RenderTarget);

            DataStream stream;

            context.MapSubresource(m_linesVertexBuffer.Buffer, MapMode.WriteNoOverwrite, MapFlags.None, out stream);
            for (int i = 0; i < m_lineVertexList.Count; i++)
            {
                stream.Write(m_lineVertexList[i]);
            }
            context.UnmapSubresource(m_linesVertexBuffer.Buffer, 0);
            stream.Dispose();

            Matrix viewProjection;

            for (int b = 0; b < m_lineBatches.Count; b++)
            {
                if (m_lineBatches[b].m_customViewProjection.HasValue)
                {
                    viewProjection = m_lineBatches[b].m_customViewProjection.Value;
                }
                else
                {
                    viewProjection = MyEnvironment.CameraView * MyEnvironment.Projection;
                }


                stream = MyMapping.MapDiscard(MyResources.ProjectionConstants.Buffer);
                stream.Write(Matrix.Transpose(viewProjection));
                MyMapping.Unmap(MyResources.ProjectionConstants.Buffer, stream);


                context.Draw(m_lineBatches[b].m_vertexCount, m_lineBatches[b].m_startVertex);
            }

            // cleanup
            m_lineVertexList.Clear();
            m_lineBatches.Clear();
        }