internal static void PreparePointLights() { var activePointlights = 0; MyLights.Update(); BoundingFrustumD viewFrustumClippedD = MyRender11.Environment.ViewFrustumClippedD; if (MyStereoRender.Enable) { if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT) { viewFrustumClippedD = MyStereoRender.EnvMatricesLeftEye.ViewFrustumClippedD; } else if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT) { viewFrustumClippedD = MyStereoRender.EnvMatricesRightEye.ViewFrustumClippedD; } } MyLights.PointlightsBvh.OverlapAllFrustum(ref viewFrustumClippedD, VisiblePointlights); bool visiblePointlights = VisiblePointlights.Count != 0; if (!visiblePointlights && !m_lastFrameVisiblePointlights) { return; } m_lastFrameVisiblePointlights = visiblePointlights; if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared)); while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1); } } foreach (var light in VisiblePointlights) { MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]); activePointlights++; Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS); } for (int lightIndex = activePointlights; lightIndex < MyRender11Constants.MAX_POINT_LIGHTS; ++lightIndex) { MyLights.WritePointlightConstants(LightId.NULL, ref m_pointlightsCullBuffer[lightIndex]); } var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.WriteAndPosition(ref activePointlights); mapping.Unmap(); mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer); mapping.WriteAndPosition(m_pointlightsCullBuffer, 0, MyRender11Constants.MAX_POINT_LIGHTS); mapping.Unmap(); if (!MyStereoRender.Enable) { RC.CSSetCB(0, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } RC.CSSetCB(1, MyCommon.GetObjectCB(16)); //RC.BindUAV(0, MyScreenDependants.m_test); RC.BindUAV(0, MyScreenDependants.m_tileIndices); RC.BindGBufferForRead(0, MyGBuffer.Main); RC.CSBindRawSRV(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer); RC.SetCS(m_preparePointLights); Vector2I tiles = new Vector2I(MyScreenDependants.TilesX, MyScreenDependants.TilesY); if (MyStereoRender.Enable && MyStereoRender.RenderRegion != MyStereoRegion.FULLSCREEN) { tiles.X /= 2; } RC.DeviceContext.Dispatch(tiles.X, tiles.Y, 1); RC.SetCS(null); }
internal unsafe static void RenderSpotlights() { RC.BindDepthRT(MyGBuffer.Main.Get(MyGbufferSlot.DepthStencil), DepthStencilAccess.ReadOnly, MyGBuffer.Main.Get(MyGbufferSlot.LBuffer)); RC.DeviceContext.Rasterizer.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); RC.DeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; if (MyStereoRender.Enable) { MyStereoRender.PSBindRawCB_FrameConstants(RC); MyStereoRender.SetViewport(RC); } var coneMesh = MyMeshes.GetMeshId(X.TEXT_("Models/Debug/Cone.mwm"), 1.0f); var buffers = MyMeshes.GetLodMesh(coneMesh, 0).Buffers; RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride); RC.SetIB(buffers.IB.Buffer, buffers.IB.Format); RC.SetVS(SpotlightProxyVs); RC.SetIL(SpotlightProxyIL); RC.SetPS(SpotlightPs_Pixel); RC.SetRS(MyRender11.m_invTriRasterizerState); var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants)); RC.SetCB(1, cb); RC.DeviceContext.PixelShader.SetSampler(13, SamplerStates.m_alphamask); RC.DeviceContext.PixelShader.SetSampler(14, SamplerStates.m_shadowmap); RC.DeviceContext.PixelShader.SetSampler(15, SamplerStates.m_shadowmap); int index = 0; int casterIndex = 0; foreach (var id in VisibleSpotlights) { SpotlightConstants spotlight = new SpotlightConstants(); MyLights.WriteSpotlightConstants(id, ref spotlight); var mapping = MyMapping.MapDiscard(cb); mapping.WriteAndPosition(ref spotlight); mapping.Unmap(); RC.DeviceContext.PixelShader.SetShaderResource(13, MyTextures.GetView(MyLights.Spotlights[id.Index].ReflectorTexture)); if (id.CastsShadowsThisFrame) { RC.DeviceContext.PixelShader.SetShaderResource(14, MyRender11.DynamicShadows.ShadowmapsPool[casterIndex].SRV); casterIndex++; } if (MyRender11.MultisamplingEnabled) { RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0); RC.SetPS(SpotlightPs_Pixel); } RC.DeviceContext.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); if (MyRender11.MultisamplingEnabled) { RC.SetPS(SpotlightPs_Sample); RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0x80); RC.DeviceContext.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); } index++; if (index >= SPOTLIGHTS_MAX) { break; } } if (MyRender11.MultisamplingEnabled) { RC.SetDS(MyDepthStencilState.DefaultDepthState); } RC.SetRS(null); }
internal static IBorrowedUavTexture Run(ISrvBindable src) { IBorrowedUavTexture uav0 = MyManagers.RwTexturesPool.BorrowUav("MyLuminanceAverage.Uav0", Format.R32G32_Float); IBorrowedUavTexture uav1 = MyManagers.RwTexturesPool.BorrowUav("MyLuminanceAverage.Uav1", Format.R32G32_Float); Vector2I size = src.Size; int texelsNum = size.X * size.Y; uint sizeX = (uint)size.X; uint sizeY = (uint)size.Y; var buffer = MyCommon.GetObjectCB(16); var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref sizeX); mapping.WriteAndPosition(ref sizeY); mapping.WriteAndPosition(ref texelsNum); mapping.Unmap(); RC.ComputeShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.ComputeShader.SetConstantBuffer(1, MyCommon.GetObjectCB(16)); RC.ComputeShader.Set(m_initialShader); IBorrowedUavTexture output = uav0; ISrvBindable inputSrc = src; RC.ComputeShader.SetUav(0, output); RC.ComputeShader.SetSrv(0, inputSrc); int threadGroupCountX = ComputeGroupCount(size.X); int threadGroupCountY = ComputeGroupCount(size.Y); RC.Dispatch(threadGroupCountX, threadGroupCountY, 1); RC.ComputeShader.Set(m_sumShader); IBorrowedUavTexture input; int i = 0; while (true) { size.X = threadGroupCountX; size.Y = threadGroupCountY; if (size.X <= NUM_THREADS && size.Y <= NUM_THREADS) { break; } output = (i % 2 == 0) ? uav1 : uav0; input = (i % 2 == 0) ? uav0 : uav1; RC.ComputeShader.SetSrv(0, null); RC.ComputeShader.SetUav(0, output); RC.ComputeShader.SetSrv(0, input); threadGroupCountX = ComputeGroupCount(size.X); threadGroupCountY = ComputeGroupCount(size.Y); RC.Dispatch(threadGroupCountX, threadGroupCountY, 1); i++; } RC.ComputeShader.Set(m_finalShader); output = (i % 2 == 0) ? uav1 : uav0; input = (i % 2 == 0) ? uav0 : uav1; RC.ComputeShader.SetSrv(0, null); RC.ComputeShader.SetUav(0, output); RC.ComputeShader.SetSrvs(0, input, m_prevLum); threadGroupCountX = ComputeGroupCount(size.X); threadGroupCountY = ComputeGroupCount(size.Y); RC.Dispatch(threadGroupCountX, threadGroupCountY, 1); RC.ComputeShader.Set(null); RC.ComputeShader.SetUav(0, null); // Backup the result for later process RC.CopySubresourceRegion(output, 0, new ResourceRegion(0, 0, 0, 1, 1, 1), m_prevLum, 0); input.Release(); return(output); }