private void PerformPrepass(MeshCollection meshes, SpatialCoordinateSystem coordinateSystem)
        {
            var device  = Resources.D3DDevice;
            var context = Resources.D3DDeviceContext;

            CameraData.ViewProjection = Matrix4x4.Transpose(Camera.GetWorldToCameraMatrix(coordinateSystem));
            context.UpdateSubresource(ref CameraData, CameraConstantBuffer);

            context.VertexShader.Set(PrepassVertexShader);
            context.VertexShader.SetConstantBuffer(2, CameraConstantBuffer);
            context.GeometryShader.Set(null);
            context.PixelShader.Set(null);

            context.ClearDepthStencilView(DepthTarget, DepthStencilClearFlags.Depth, 1.0f, 0);
            context.OutputMerger.SetRenderTargets(DepthTarget, (RenderTargetView)null);

            context.Rasterizer.SetViewport(0, 0, Camera.Width, Camera.Height);
            context.Rasterizer.State = new RasterizerState(device, new RasterizerStateDescription
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid
            });

            meshes.Draw(numberOfIndices =>
            {
                context.DrawIndexed(numberOfIndices, 0, 0);
            });

            context.OutputMerger.SetRenderTargets(null, (RenderTargetView)null);
        }
Exemple #2
0
        public void Render()
        {
            if (!Active)
            {
                return;
            }

            var device  = DeviceResources.D3DDevice;
            var context = DeviceResources.D3DDeviceContext;

            context.VertexShader.Set(VertexShader);
            context.PixelShader.Set(PixelShader);
            context.PixelShader.SetShaderResource(0, CubeMap.ShaderResourceView);

            Meshes.Draw(numberOfIndices =>
            {
                context.DrawIndexedInstanced(numberOfIndices, 2, 0, 0, 0);
            });

            context.PixelShader.SetShaderResource(0, null);

            if (CubeMapUpdateRequired && !Paused)
            {
                CameraData.ViewProjection = Matrix4x4.Transpose(PhysicalCamera.GetWorldToCameraMatrix(CoordinateSystem));
                context.UpdateSubresource(ref CameraData, CameraConstantBuffer);
                CameraDirectionData.Vector = PhysicalCamera.Forward;
                context.UpdateSubresource(ref CameraDirectionData, CameraDirectionConstantBuffer);

                context.VertexShader.Set(CubeVertexShader);
                context.VertexShader.SetConstantBuffer(1, CubeMap.CubeArrayBuffer);
                context.VertexShader.SetConstantBuffer(2, CameraConstantBuffer);
                context.PixelShader.Set(CubePixelShader);
                var cameraTexture = PhysicalCamera.AcquireTexture();
                if (cameraTexture == null)
                {
                    return;
                }
                var luminanceView = new ShaderResourceView(device, cameraTexture, new ShaderResourceViewDescription()
                {
                    Format    = SharpDX.DXGI.Format.R8_UInt,
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                    {
                        MipLevels = 1
                    }
                });
                var chrominanceView = new ShaderResourceView(device, cameraTexture, new ShaderResourceViewDescription()
                {
                    Format    = SharpDX.DXGI.Format.R8G8_UInt,
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                    {
                        MipLevels = 1
                    }
                });
                context.PixelShader.SetShaderResource(0, luminanceView);
                context.PixelShader.SetShaderResource(1, chrominanceView);
                context.PixelShader.SetConstantBuffer(3, CameraDirectionConstantBuffer);

                context.Rasterizer.SetViewport(0.0f, 0.0f, CubeMap.Resolution, CubeMap.Resolution);
                context.Rasterizer.State = new RasterizerState(device, new RasterizerStateDescription()
                {
                    CullMode = CullMode.None,
                    FillMode = FillMode.Solid
                });
                var blendDescription = new BlendStateDescription();
                for (int i = 0; i < 6; i++)
                {
                    blendDescription.RenderTarget[i].IsBlendEnabled        = true;
                    blendDescription.RenderTarget[i].SourceBlend           = BlendOption.SourceAlpha;
                    blendDescription.RenderTarget[i].DestinationBlend      = BlendOption.InverseSourceAlpha;
                    blendDescription.RenderTarget[i].SourceAlphaBlend      = BlendOption.One;
                    blendDescription.RenderTarget[i].DestinationAlphaBlend = BlendOption.Zero;
                    blendDescription.RenderTarget[i].BlendOperation        = BlendOperation.Add;
                    blendDescription.RenderTarget[i].AlphaBlendOperation   = BlendOperation.Add;
                    blendDescription.RenderTarget[i].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                }
                context.OutputMerger.BlendState = new BlendState(device, blendDescription);
                context.OutputMerger.SetRenderTargets(CubeMap.DepthStencilView, CubeMap.RenderTargetView);
                context.ClearDepthStencilView(CubeMap.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);

                Meshes.Draw(numberOfIndices =>
                {
                    context.DrawIndexedInstanced(numberOfIndices, 6, 0, 0, 0);
                });

                luminanceView.Dispose();
                chrominanceView.Dispose();
                PhysicalCamera.ReleaseTexture();

                CubeMapUpdateRequired = false;
            }
        }