internal static void Combine(RwTexId targetArray, MyShadowCascades firstCascades, MyShadowCascades secondCascades) { if (!MyRender11.Settings.EnableShadows) { return; } ProfilerShort.Begin("MyShadowCascadesPostProcess.Combine"); MyGpuProfiler.IC_BeginBlock("MyShadowCascadesPostProcess.Combine"); firstCascades.FillConstantBuffer(firstCascades.CascadeConstantBuffer); secondCascades.FillConstantBuffer(secondCascades.CascadeConstantBuffer); secondCascades.PostProcessor.MarkCascadesInStencil(secondCascades.CascadeInfo); MyRenderContext renderContext = MyRenderContext.Immediate; DeviceContext deviceContext = renderContext.DeviceContext; deviceContext.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); deviceContext.PixelShader.SetConstantBuffer(10, firstCascades.CascadeConstantBuffer); deviceContext.PixelShader.SetConstantBuffer(11, secondCascades.CascadeConstantBuffer); deviceContext.PixelShader.SetConstantBuffer(12, m_inverseConstants); for (int subresourceIndex = 0; subresourceIndex < targetArray.Description2d.ArraySize; ++subresourceIndex) { renderContext.BindGBufferForRead(0, MyGBuffer.Main); deviceContext.OutputMerger.SetTargets((DepthStencilView)null, (RenderTargetView)targetArray.SubresourceRtv(subresourceIndex)); deviceContext.PixelShader.SetShaderResource(0, firstCascades.CascadeShadowmapArray.SubresourceSrv(subresourceIndex)); deviceContext.PixelShader.SetShaderResource(1, secondCascades.CascadeShadowmapArray.ShaderView); //deviceContext.PixelShader.SetShaderResource(4, MyGBuffer.Main.DepthStencil.m_SRV_stencil); renderContext.SetPS(m_combinePS); Matrix inverseCascadeMatrix = MatrixD.Transpose(MatrixD.Invert(firstCascades.CascadeInfo[subresourceIndex].CurrentLocalToProjection * MyMatrixHelpers.ClipspaceToTexture)); var mapping = MyMapping.MapDiscard(m_inverseConstants); mapping.WriteAndPosition(ref inverseCascadeMatrix); mapping.Unmap(); MyScreenPass.DrawFullscreenQuad(new MyViewport(0, 0, targetArray.Description2d.Width, targetArray.Description2d.Height)); } deviceContext.OutputMerger.SetTargets(null as DepthStencilView, null as RenderTargetView); deviceContext.PixelShader.SetShaderResource(0, null); deviceContext.PixelShader.SetShaderResource(1, null); deviceContext.PixelShader.SetShaderResource(2, null); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); }
unsafe static void DrawBatches(MyRenderContext RC, MyStringId material, int matIndex, bool transparent) { if (m_jobs.Count == 0) { return; } var matDesc = m_materials[material][matIndex]; MyScreenDecalType type = matDesc.DecalType; if (transparent) { // Always fallback to colormap for transparent surface decals type = MyScreenDecalType.ColorMap; } switch (type) { case MyScreenDecalType.NormalMap: BindResources(RC); RC.SetPS(m_psNormalMap); RC.SetBS(MyRender11.BlendDecalNormal); break; case MyScreenDecalType.ColorMap: if (transparent) { BindResourcesTransparentBillboards(RC); RC.SetPS(m_psColorMapTransparent); } else { BindResources(RC); RC.SetPS(m_psColorMap); RC.SetBS(MyRender11.BlendDecalColor); } break; case MyScreenDecalType.NormalColorMap: BindResources(RC); RC.SetPS(m_psNormalColorMap); RC.SetBS(MyRender11.BlendDecalNormalColor); break; case MyScreenDecalType.NormalColorExtMap: BindResources(RC); RC.SetPS(m_psNormalColorExtMap); RC.SetBS(MyRender11.BlendDecalNormalColorExt); break; default: throw new Exception("Unknown decal type"); } // factor 1 makes overwriting of gbuffer color & subtracting from ao RC.DeviceContext.PixelShader.SetShaderResource(3, MyTextures.GetView(matDesc.AlphamaskTexture)); RC.DeviceContext.PixelShader.SetShaderResource(4, MyTextures.GetView(matDesc.ColorMetalTexture)); RC.DeviceContext.PixelShader.SetShaderResource(5, MyTextures.GetView(matDesc.NormalmapTexture)); RC.DeviceContext.PixelShader.SetShaderResource(6, MyTextures.GetView(matDesc.ExtensionsTexture)); var decalCb = MyCommon.GetObjectCB(sizeof(MyDecalConstants) * DECAL_BATCH_SIZE); int batchCount = m_jobs.Count / DECAL_BATCH_SIZE + 1; int offset = 0; for (int i1 = 0; i1 < batchCount; i1++) { var mapping = MyMapping.MapDiscard(decalCb); int leftDecals = m_jobs.Count - offset; int decalCount = leftDecals > DECAL_BATCH_SIZE ? DECAL_BATCH_SIZE : leftDecals; for (int i2 = 0; i2 < decalCount; ++i2) { MyDecalConstants constants = new MyDecalConstants(); EncodeJobConstants(i2, ref constants); mapping.WriteAndPosition(ref constants); } mapping.Unmap(); // Draw a box without buffer: 36 vertices -> 12 triangles. 2 triangles per face -> 6 faces MyImmediateRC.RC.DeviceContext.DrawIndexed(36 * decalCount, 0, 0); offset += DECAL_BATCH_SIZE; } }
unsafe static void DrawBatches(MyRenderContext RC, MyStringId material) { if (m_matrices.Count == 0) { return; } var matDesc = m_materials[material]; switch (matDesc.DecalType) { case MyScreenDecalType.NormalMap: BindResources(RC, false); RC.SetPS(m_psNormalMap); RC.SetBS(MyRender11.BlendDecalNormal); break; case MyScreenDecalType.ColorMap: BindResources(RC, true); RC.SetPS(m_psColorMap); RC.SetBS(MyRender11.BlendDecalColor); break; case MyScreenDecalType.NormalColorMap: BindResources(RC, false); RC.SetPS(m_psNormalColorMap); RC.SetBS(MyRender11.BlendDecalNormalColor); break; default: throw new Exception("Unknown decal type"); } // factor 1 makes overwriting of gbuffer color & subtracting from ao RC.DeviceContext.PixelShader.SetShaderResource(3, MyTextures.GetView(matDesc.AlphamaskTexture)); RC.DeviceContext.PixelShader.SetShaderResource(4, MyTextures.GetView(matDesc.ColorMetalTexture)); RC.DeviceContext.PixelShader.SetShaderResource(5, MyTextures.GetView(matDesc.NormalmapTexture)); var decalCb = MyCommon.GetObjectCB(sizeof(MyDecalConstants) * DECAL_BATCH_SIZE); int batchCount = m_matrices.Count / DECAL_BATCH_SIZE + 1; int offset = 0; for (int i1 = 0; i1 < batchCount; i1++) { var mapping = MyMapping.MapDiscard(decalCb); int leftDecals = m_matrices.Count - offset; int decalCount = leftDecals > DECAL_BATCH_SIZE ? DECAL_BATCH_SIZE : leftDecals; for (int i2 = 0; i2 < decalCount; ++i2) { Matrix worldMatrix = Matrix.Transpose(m_matrices[i2]); Matrix transposeInverseMatrix = Matrix.Transpose(Matrix.Invert(m_matrices[i2])); mapping.WriteAndPosition(ref worldMatrix); mapping.WriteAndPosition(ref transposeInverseMatrix); } mapping.Unmap(); // Draw a box without buffer: 36 vertices -> 12 triangles. 2 triangles per face -> 6 faces MyImmediateRC.RC.DeviceContext.DrawIndexed(36 * decalCount, 0, 0); offset += DECAL_BATCH_SIZE; } m_matrices.Clear(); }
private unsafe void MarkCascadesInStencil(MyProjectionInfo[] cascadeInfo) { MyGpuProfiler.IC_BeginBlock("MarkCascadesInStencil"); //RC.SetRS(MyRasterizerState.CullCW); MyRenderContext renderContext = MyRenderContext.Immediate; renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; renderContext.SetVB(0, m_cascadesBoundingsVertices.Buffer, m_cascadesBoundingsVertices.Stride); renderContext.SetIB(m_cubeIB.Buffer, m_cubeIB.Format); renderContext.SetIL(m_inputLayout); renderContext.DeviceContext.Rasterizer.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); renderContext.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); renderContext.BindDepthRT(MyGBuffer.Main.DepthStencil, DepthStencilAccess.DepthReadOnly, null); renderContext.SetVS(m_markVS); renderContext.SetPS(m_markPS); const int vertexCount = 8; Vector3D *frustumVerticesSS = stackalloc Vector3D[vertexCount]; frustumVerticesSS[0] = new Vector3D(-1, -1, 0); frustumVerticesSS[1] = new Vector3D(-1, 1, 0); frustumVerticesSS[2] = new Vector3D(1, 1, 0); frustumVerticesSS[3] = new Vector3D(1, -1, 0); frustumVerticesSS[4] = new Vector3D(-1, -1, 1); frustumVerticesSS[5] = new Vector3D(-1, 1, 1); frustumVerticesSS[6] = new Vector3D(1, 1, 1); frustumVerticesSS[7] = new Vector3D(1, -1, 1); Vector3D *lightVertices = stackalloc Vector3D[vertexCount]; Vector3 * tmpFloatVertices = stackalloc Vector3[vertexCount]; var mapping = MyMapping.MapDiscard(m_cascadesBoundingsVertices.Buffer); for (int cascadeIndex = 0; cascadeIndex < MyRender11.Settings.ShadowCascadeCount; ++cascadeIndex) { var inverseViewProj = MatrixD.Invert(cascadeInfo[cascadeIndex].CurrentLocalToProjection); for (int arrayIndex = 0; arrayIndex < vertexCount; ++arrayIndex) { Vector3D.Transform(ref frustumVerticesSS[arrayIndex], ref inverseViewProj, out lightVertices[arrayIndex]); tmpFloatVertices[arrayIndex] = lightVertices[arrayIndex]; } for (int arrayIndex = 0; arrayIndex < vertexCount; ++arrayIndex) { mapping.WriteAndPosition(ref tmpFloatVertices[arrayIndex]); } } mapping.Unmap(); for (int cascadeIndex = 0; cascadeIndex < MyRender11.Settings.ShadowCascadeCount; ++cascadeIndex) { renderContext.SetDS(MyDepthStencilState.MarkIfInsideCascade[cascadeIndex], 1 << cascadeIndex); // mark ith bit on depth near renderContext.DeviceContext.DrawIndexed(36, 0, 8 * cascadeIndex); } renderContext.BindDepthRT(null, DepthStencilAccess.DepthReadOnly, null); renderContext.SetDS(null); renderContext.SetRS(null); MyGpuProfiler.IC_EndBlock(); }
unsafe static void DrawBatches(MyRenderContext RC, MyStringId material) { if (m_matrices.Count == 0) return; var matDesc = m_materials[material]; switch (matDesc.DecalType) { case MyScreenDecalType.NormalMap: BindResources(RC, false); RC.SetPS(m_psNormalMap); RC.SetBS(MyRender11.BlendDecalNormal); break; case MyScreenDecalType.ColorMap: BindResources(RC, true); RC.SetPS(m_psColorMap); RC.SetBS(MyRender11.BlendDecalColor); break; case MyScreenDecalType.NormalColorMap: BindResources(RC, false); RC.SetPS(m_psNormalColorMap); RC.SetBS(MyRender11.BlendDecalNormalColor); break; default: throw new Exception("Unknown decal type"); } // factor 1 makes overwriting of gbuffer color & subtracting from ao RC.DeviceContext.PixelShader.SetShaderResource(3, MyTextures.GetView(matDesc.AlphamaskTexture)); RC.DeviceContext.PixelShader.SetShaderResource(4, MyTextures.GetView(matDesc.ColorMetalTexture)); RC.DeviceContext.PixelShader.SetShaderResource(5, MyTextures.GetView(matDesc.NormalmapTexture)); var decalCb = MyCommon.GetObjectCB(sizeof(MyDecalConstants) * DECAL_BATCH_SIZE); int batchCount = m_matrices.Count / DECAL_BATCH_SIZE + 1; int offset = 0; for (int i1 = 0; i1 < batchCount; i1++) { var mapping = MyMapping.MapDiscard(decalCb); int leftDecals = m_matrices.Count - offset; int decalCount = leftDecals > DECAL_BATCH_SIZE ? DECAL_BATCH_SIZE : leftDecals; for (int i2 = 0; i2 < decalCount; ++i2) { Matrix worldMatrix = Matrix.Transpose(m_matrices[i2]); Matrix transposeInverseMatrix = Matrix.Transpose(Matrix.Invert(m_matrices[i2])); mapping.WriteAndPosition(ref worldMatrix); mapping.WriteAndPosition(ref transposeInverseMatrix); } mapping.Unmap(); // Draw a box without buffer: 36 vertices -> 12 triangles. 2 triangles per face -> 6 faces MyImmediateRC.RC.DeviceContext.DrawIndexed(36 * decalCount, 0, 0); offset += DECAL_BATCH_SIZE; } m_matrices.Clear(); }