internal static void WriteSpotlightConstants(LightId lid, ref SpotlightConstants data) { data.Spotlight = Spotlights[lid.Index].Spotlight; data.Spotlight.ShadowsRange = Lights.Data[lid.Index].CastsShadowsThisFrame ? Lights.Data[lid.Index].ShadowsDistance : 0; data.Spotlight.Light.Position = Lights.Data[lid.Index].SpotPosition - MyRender11.Environment.Matrices.CameraPosition; float ratio = (float)Math.Sqrt(1 - data.Spotlight.ApertureCos * data.Spotlight.ApertureCos) / data.Spotlight.ApertureCos; float h = ratio * data.Spotlight.Light.Range; Matrix viewProjAt0 = MyRender11.Environment.Matrices.ViewProjectionAt0; if (MyStereoRender.Enable) { if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT) { viewProjAt0 = MyStereoRender.EnvMatricesLeftEye.ViewProjectionAt0; } if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT) { viewProjAt0 = MyStereoRender.EnvMatricesRightEye.ViewProjectionAt0; } } data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Spotlight.Light.Range) * Matrix.CreateWorld(data.Spotlight.Light.Position, data.Spotlight.Direction, data.Spotlight.Up) * viewProjAt0); data.ShadowMatrix = CreateShadowMatrix(lid); }
internal static void WriteSpotlightConstants(LightId lid, ref SpotlightConstants data) { data.ApertureCos = Spotlights[lid.Index].ApertureCos; data.Range = Spotlights[lid.Index].Range; data.Color = Spotlights[lid.Index].Color; data.Direction = Spotlights[lid.Index].Direction; data.Up = Spotlights[lid.Index].Up; data.ShadowsRange = Lights.Data[lid.Index].CastsShadows ? Lights.Data[lid.Index].ShadowsDistance : 0; data.Position = Lights.Data[lid.Index].Position - MyEnvironment.CameraPosition; float ratio = (float)Math.Sqrt(1 - data.ApertureCos * data.ApertureCos) / data.ApertureCos; float h = ratio * data.Range; data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Range) * Matrix.CreateWorld(data.Position, data.Direction, data.Up) * MyEnvironment.ViewProjectionAt0); }
internal static void WriteSpotlightConstants(LightId lid, ref SpotlightConstants data) { data.ApertureCos = Spotlights[lid.Index].ApertureCos; data.Range = Spotlights[lid.Index].Range; data.Color = Spotlights[lid.Index].Color; data.Direction = Spotlights[lid.Index].Direction; data.Up = Spotlights[lid.Index].Up; data.ShadowsRange = Lights.Data[lid.Index].CastsShadowsThisFrame ? Lights.Data[lid.Index].ShadowsDistance : 0; data.Position = Lights.Data[lid.Index].PositionWithOffset - MyEnvironment.CameraPosition; float ratio = (float)Math.Sqrt(1 - data.ApertureCos * data.ApertureCos) / data.ApertureCos; float h = ratio * data.Range; data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Range) * Matrix.CreateWorld(data.Position, data.Direction, data.Up) * MyEnvironment.ViewProjectionAt0); }
internal unsafe static void RenderSpotlights() { RC.SetRtv(MyGBuffer.Main.DepthStencil, MyDepthStencilAccess.ReadOnly, MyGBuffer.Main.LBuffer); RC.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); RC.SetPrimitiveTopology(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.SetVertexBuffer(0, buffers.VB0.Buffer, buffers.VB0.Stride); RC.SetIndexBuffer(buffers.IB.Buffer, buffers.IB.Format); RC.VertexShader.Set(SpotlightProxyVs); RC.SetInputLayout(SpotlightProxyIL); RC.PixelShader.Set(SpotlightPs_Pixel); RC.SetRasterizerState(MyRasterizerStateManager.InvTriRasterizerState); var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants)); RC.AllShaderStages.SetConstantBuffer(1, cb); RC.PixelShader.SetSampler(13, MySamplerStateManager.Alphamask); RC.PixelShader.SetSampler(14, MySamplerStateManager.Shadowmap); RC.PixelShader.SetSampler(15, MySamplerStateManager.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.PixelShader.SetSrv(13, MyLights.Spotlights[id.Index].ReflectorTexture); if (id.CastsShadowsThisFrame) { RC.PixelShader.SetSrv(14, MyRender11.DynamicShadows.ShadowmapsPool[casterIndex]); casterIndex++; } if (MyRender11.MultisamplingEnabled) { RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0); RC.PixelShader.Set(SpotlightPs_Pixel); } RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); if (MyRender11.MultisamplingEnabled) { RC.PixelShader.Set(SpotlightPs_Sample); RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0x80); RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); } index++; if (index >= SPOTLIGHTS_MAX) { break; } } if (MyRender11.MultisamplingEnabled) { RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState); } RC.SetRasterizerState(null); RC.SetRtv(null); }
internal unsafe static void RenderSpotlights() { RC.SetRtv(MyGBuffer.Main.DepthStencil, MyDepthStencilAccess.ReadOnly, MyGBuffer.Main.LBuffer); RC.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); RC.SetPrimitiveTopology(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.SetVertexBuffer(0, buffers.VB0); RC.SetIndexBuffer(buffers.IB); RC.VertexShader.Set(SpotlightProxyVs); RC.SetInputLayout(SpotlightProxyIL); RC.PixelShader.Set(SpotlightPs_Pixel); RC.SetRasterizerState(MyRasterizerStateManager.InvTriRasterizerState); var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants)); RC.AllShaderStages.SetConstantBuffer(1, cb); RC.PixelShader.SetSampler(13, MySamplerStateManager.Alphamask); RC.PixelShader.SetSampler(14, MySamplerStateManager.Shadowmap); RC.PixelShader.SetSampler(15, MySamplerStateManager.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.PixelShader.SetSrv(13, MyLights.Spotlights[id.Index].ReflectorTexture); if (id.CastsShadowsThisFrame) { RC.PixelShader.SetSrv(14, MyRender11.DynamicShadows.ShadowmapsPool[casterIndex]); casterIndex++; } if (MyRender11.MultisamplingEnabled) { RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0); RC.PixelShader.Set(SpotlightPs_Pixel); } RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); if (MyRender11.MultisamplingEnabled) { RC.PixelShader.Set(SpotlightPs_Sample); RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0x80); RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); } index++; if (index >= SPOTLIGHTS_MAX) break; } if (MyRender11.MultisamplingEnabled) { RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState); } RC.SetRasterizerState(null); RC.SetRtv(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); }