Esempio n. 1
0
        // this is tricky call. The method assumes that Draw() has been called in this frame
        public void DrawGlass(MyRenderContext RC)
        {
            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

            RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height);
            //RC.SetRtvs(m_gbuffer, MyDepthStencilAccess.ReadWrite); <- the rtv is set out of the pass...

            FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix));

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

            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
            RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);

            IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 255); // <- the lod value does not matter in this case

            RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);
            //RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);

            foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups())
            {
                MyRenderProxy.Assert(itGroup.InstancesCount != 0);
                MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement);
                if (itGroup.Lod.GlassParts == null)
                {
                    continue;
                }
                foreach (var part in itGroup.Lod.GlassParts)
                {
                    MyGlassMaterial material = part.GlassMaterial;
                    RC.SetVertexBuffer(0, part.Parent.VB0);
                    RC.SetVertexBuffer(1, part.Parent.VB1);
                    RC.SetVertexBuffer(2, m_vbInstances);
                    RC.SetIndexBuffer(part.Parent.IB);

                    MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State);
                    RC.SetInputLayout(shaderBundle.InputLayout);
                    RC.VertexShader.Set(shaderBundle.VertexShader);
                    RC.PixelShader.Set(shaderBundle.PixelShader);
                    RC.PixelShader.SetSrvs(0, material.Srvs);
                    RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, GetGlassCB(RC, material));

                    int numInstances = itGroup.InstancesCount;
                    int ibOffset     = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount;
                    RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex,
                                            part.StartVertex, ibOffset);
                }
            }
        }
Esempio n. 2
0
        internal virtual void Begin()
        {
            MyUtils.Init(ref Locals);
            Locals.Clear();

            //if (!m_isImmediate)
            //{
            //    //Debug.Assert(m_RC == null);
            //    //m_RC = MyRenderContextPool.AcquireRC();
            //}

            var viewProjTranspose = Matrix.Transpose(ViewProjection);
            var mapping           = MyMapping.MapDiscard(RC, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref viewProjTranspose);
            mapping.Unmap();

            // common settings
            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            RC.SetViewport(Viewport.OffsetX, Viewport.OffsetY, Viewport.Width, Viewport.Height);

            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);

            RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.VOXELS_MATERIALS_LUT_SLOT, MyCommon.VoxelMaterialsConstants.Cb);
            RC.AllShaderStages.SetConstantBuffer(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants);

            RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);

            if (MyBigMeshTable.Table.m_IB != null)
            {
                var slotcounter = MyCommon.BIG_TABLE_INDICES;
                RC.VertexShader.SetSrv(slotcounter++, MyBigMeshTable.Table.m_IB);
                RC.VertexShader.SetSrv(slotcounter++, MyBigMeshTable.Table.m_VB_positions);
                RC.VertexShader.SetSrv(slotcounter++, MyBigMeshTable.Table.m_VB_rest);
            }
        }
Esempio n. 3
0
        internal virtual void Begin()
        {
            MyUtils.Init(ref Locals);
            Locals.Clear();

            //if (!m_isImmediate)
            //{
            //    //Debug.Assert(m_RC == null);
            //    //m_RC = MyRenderContextPool.AcquireRC();
            //}

            var viewProjTranspose = Matrix.Transpose(ViewProjection);
            var mapping           = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref viewProjTranspose);
            mapping.Unmap();

            // common settings
            Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            Context.Rasterizer.SetViewport(Viewport.OffsetX, Viewport.OffsetY, Viewport.Width, Viewport.Height);

            Context.PixelShader.SetSamplers(0, MyRender11.StandardSamplers);

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            RC.SetCB(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants);

            Context.PixelShader.SetShaderResource(MyCommon.DITHER_8X8_SLOT, MyTextures.Views[MyTextures.Dithering8x8TexId.Index]);

            if (MyBigMeshTable.Table.m_IB != null)
            {
                var slotcounter = MyCommon.BIG_TABLE_INDICES;
                RC.VSBindSRV(slotcounter++, MyBigMeshTable.Table.m_IB.Srv);
                RC.VSBindSRV(slotcounter++, MyBigMeshTable.Table.m_VB_positions.Srv);
                RC.VSBindSRV(slotcounter++, MyBigMeshTable.Table.m_VB_rest.Srv);
            }
        }
Esempio n. 4
0
        internal unsafe override sealed void RecordCommands(MyRenderableProxy proxy)
        {
            if (proxy.Mesh.Buffers.IB == IndexBufferId.NULL || proxy.DrawSubmesh.IndexCount == 0 || proxy.SkipIfTooSmall())
            {
                return;
            }


            Stats.Meshes++;

            SetProxyConstants(proxy);
            BindProxyGeometry(proxy);

            Debug.Assert(proxy.DepthShaders.VS != null);

            RC.BindShaders(proxy.DepthShaders);

            if ((proxy.Flags & MyRenderableProxyFlags.DisableFaceCulling) > 0)
            {
                RC.SetRS(MyRender11.m_nocullRasterizerState);
            }
            else
            {
                RC.SetRS(DefaultRasterizer);
            }


            //for (int i = 0; i < proxy.depthOnlySubmeshes.Length; i++)
            //{
            //    Stats.Submeshes++;

            //    var submesh = proxy.depthOnlySubmeshes[i];

            var submesh = proxy.DrawSubmesh;

            if (submesh.MaterialId != Locals.matTexturesID && (!((proxy.Flags & MyRenderableProxyFlags.DepthSkipTextures) > 0)))
            {
                Stats.MaterialConstantsChanges++;

                Locals.matTexturesID = submesh.MaterialId;
                var material = MyMaterials1.ProxyPool.Data[submesh.MaterialId.Index];
                RC.MoveConstants(ref material.MaterialConstants);
                RC.SetConstants(ref material.MaterialConstants, MyCommon.MATERIAL_SLOT);
                RC.SetSRVs(ref material.MaterialSRVs);
            }

            if (proxy.SkinningMatrices != null)
            {
                Stats.ObjectConstantsChanges++;

                MyObjectData objectData = proxy.ObjectData;
                //objectData.Translate(-MyEnvironment.CameraPosition);

                MyMapping mapping;
                mapping = MyMapping.MapDiscard(RC.Context, proxy.ObjectBuffer);
                void *ptr = &objectData;
                mapping.stream.Write(new IntPtr(ptr), 0, sizeof(MyObjectData));

                if (proxy.SkinningMatrices != null)
                {
                    if (submesh.BonesMapping == null)
                    {
                        for (int j = 0; j < Math.Min(MyRender11Constants.SHADER_MAX_BONES, proxy.SkinningMatrices.Length); j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.SkinningMatrices[j]));
                        }
                    }
                    else
                    {
                        for (int j = 0; j < submesh.BonesMapping.Length; j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.SkinningMatrices[submesh.BonesMapping[j]]));
                        }
                    }
                }

                mapping.Unmap();
            }

            if (proxy.InstanceCount == 0)
            {
                RC.Context.DrawIndexed(submesh.IndexCount, submesh.StartIndex, submesh.BaseVertex);
                RC.Stats.DrawIndexed++;
                Stats.Instances++;
                Stats.Triangles += submesh.IndexCount / 3;
            }
            else
            {
                RC.Context.DrawIndexedInstanced(submesh.IndexCount, proxy.InstanceCount, submesh.StartIndex, submesh.BaseVertex, proxy.StartInstance);
                RC.Stats.DrawIndexedInstanced++;
                Stats.Instances += proxy.InstanceCount;
                Stats.Triangles += proxy.InstanceCount * submesh.IndexCount / 3;
            }
            //}
        }
Esempio n. 5
0
        protected override void Draw(MyRenderContext RC, List <MyInstanceComponent> visibleInstances)
        {
            int maxLodId = MyManagers.IDGenerator.DepthLods.GetHighestID();

            if (IsProfilingDoable())
            {
                ProfilerShort.Begin("Preparation");
            }
            m_drawableGroupDepthStrategy.Prepare(m_vbInstances);
            m_drawableGroupFactory.Compute(m_drawableGroupDepthStrategy, visibleInstances, PassId, maxLodId);
            bool isEmpty = !m_drawableGroupDepthStrategy.Finalize(RC, out m_vbInstances);

            if (!isEmpty)
            {
                FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix));

                RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

                RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height);
                RC.SetRtv(m_dsv, null);

                FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix));

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

                RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
                RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
                RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);

                // Just some placeholder:
                IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 0);
                RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);
                RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);

                if (IsProfilingDoable())
                {
                    ProfilerShort.BeginNextBlock("Recording commands");
                }

                foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups())
                {
                    MyRenderProxy.Assert(itGroup.InstancesCount != 0);
                    MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement);
                    foreach (var part in itGroup.Lod.Parts)
                    {
                        RC.SetVertexBuffer(0, part.Parent.VB0);
                        RC.SetVertexBuffer(2, m_vbInstances);
                        RC.SetIndexBuffer(part.Parent.IB);

                        RC.SetDepthStencilState(null);
                        RC.SetRasterizerState(GetRasterizerState(m_isCascade));
                        RC.SetBlendState(null);

                        MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State);
                        RC.SetInputLayout(shaderBundle.InputLayout);
                        RC.VertexShader.Set(shaderBundle.VertexShader);
                        RC.PixelShader.Set(shaderBundle.PixelShader);

                        int numInstances = itGroup.InstancesCount;
                        int ibOffset     = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount;
                        RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex,
                                                part.StartVertex, ibOffset);

                        m_stats.Triangles += (part.IndicesCount / 3) * numInstances;
                        m_stats.Instances += numInstances;
                        m_stats.Draws++;
                    }
                }
            }

            if (IsProfilingDoable())
            {
                ProfilerShort.End();
            }
        }
Esempio n. 6
0
        protected override void Draw(MyRenderContext RC, List <MyInstanceComponent> visibleInstances)
        {
            int maxLodId = MyManagers.IDGenerator.GBufferLods.GetHighestID();

            ProfilerShort.Begin("Preparation");
            m_drawableGroupGBufferStrategy.Prepare(m_vbInstances);
            m_drawableGroupFactory.Compute(m_drawableGroupGBufferStrategy, visibleInstances, 0, maxLodId);
            bool isEmpty = !m_drawableGroupGBufferStrategy.Finalize(RC, out m_vbInstances);

            if (!isEmpty)
            {
                RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

                RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height);
                RC.SetRtvs(m_gbuffer, MyDepthStencilAccess.ReadWrite);

                FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix));

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

                RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
                RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
                RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);

                IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 255); // <- the lod value does not matter in this case
                RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);
                RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData);

                ProfilerShort.BeginNextBlock("Recording commands");

                foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups())
                {
                    MyRenderProxy.Assert(itGroup.InstancesCount != 0);
                    MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement);
                    foreach (var part in itGroup.Lod.Parts)
                    {
                        MyStandardMaterial material = part.StandardMaterial;
                        RC.SetVertexBuffer(0, part.Parent.VB0);
                        RC.SetVertexBuffer(1, part.Parent.VB1);
                        RC.SetVertexBuffer(2, m_vbInstances);
                        RC.SetIndexBuffer(part.Parent.IB);
                        if (MyRender11.Settings.Wireframe)
                        {
                            RC.SetDepthStencilState(MyDepthStencilStateManager.DepthTestWrite);
                            RC.SetBlendState(null);
                            RC.SetRasterizerState(MyRasterizerStateManager.NocullWireframeRasterizerState);
                        }
                        else
                        {
                            RC.SetDepthStencilState(part.StandardMaterial.DepthStencilState);
                            RC.SetRasterizerState(material.RasterizerState);
                            RC.SetBlendState(part.StandardMaterial.BlendState);
                        }

                        MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State);
                        RC.SetInputLayout(shaderBundle.InputLayout);
                        RC.VertexShader.Set(shaderBundle.VertexShader);
                        RC.PixelShader.Set(shaderBundle.PixelShader);
                        RC.PixelShader.SetSrvs(0, SrvStrategy.GetSrvs(part));

                        int numInstances = itGroup.InstancesCount;
                        int ibOffset     = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount;
                        RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex,
                                                part.StartVertex, ibOffset);

                        m_stats.Triangles += (part.IndicesCount / 3) * numInstances;
                        m_stats.Instances += numInstances;
                        m_stats.Draws++;
                    }
                }
            }

            ProfilerShort.End();
        }
Esempio n. 7
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();
        }
Esempio n. 8
0
        internal static unsafe void Draw(MyBindableResource depth)
        {
            RC.SetupScreenViewport();
            RC.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
            RC.SetIL(m_inputLayout);

            RC.SetRS(MyRender11.m_linesRasterizerState);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);

            RC.SetDS(MyDepthStencilState.DefaultDepthState);

            CheckBufferSize(m_vertices.Count);
            RC.SetVB(0, m_VB.Buffer, m_VB.Stride);

            RC.BindDepthRT(depth, DepthStencilAccess.ReadOnly, MyRender11.Backbuffer);

            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            if (m_batches.Count > 0)
            {
                var mapping = MyMapping.MapDiscard(m_VB.Buffer);
                for (int i = 0; i < m_vertices.Count; i++)
                {
                    mapping.stream.Write(m_vertices[i]);
                }
                mapping.Unmap();

                Matrix prevMatrix = Matrix.Zero;
                foreach (var batch in m_batches)
                {
                    Matrix matrix;
                    if (batch.CustomViewProjection.HasValue)
                    {
                        matrix = batch.CustomViewProjection.Value;
                    }
                    else
                    {
                        matrix = MyEnvironment.ViewProjection;
                    }

                    if (prevMatrix != matrix)
                    {
                        prevMatrix = matrix;

                        mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
                        mapping.stream.Write(Matrix.Transpose(matrix));
                        mapping.Unmap();
                    }

                    if (batch.IgnoreDepth)
                    {
                        RC.SetDS(MyDepthStencilState.IgnoreDepthStencil);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.DefaultDepthState);
                    }

                    RC.Context.Draw(batch.VertexCount, batch.StartVertex);
                }
            }

            RC.SetDS(null);
            RC.SetRS(null);

            m_vertices.Clear();

            foreach (var batch in m_batches)
            {
                m_batchesPool.Deallocate(batch);
            }
            m_batches.Clear();
        }