コード例 #1
0
        public void Render()
        {
            if (!loadingFinished || !physicalCamera.Ready)
            {
                return;
            }

            var device        = deviceResources.D3DDevice;
            var context       = deviceResources.D3DDeviceContext;
            int stride        = SharpDX.Utilities.SizeOf <VertexPositionUV>();
            int offset        = 0;
            var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(vertexBuffer, stride, offset);

            context.InputAssembler.SetVertexBuffers(0, bufferBinding);
            context.InputAssembler.SetIndexBuffer(indexBuffer, SharpDX.DXGI.Format.R16_UInt, 0);
            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.InputLayout       = inputLayout;

            context.VertexShader.SetShader(vertexShader, null, 0);
            context.VertexShader.SetConstantBuffers(0, modelConstantBuffer);
            context.PixelShader.SetShader(pixelShader, null, 0);
            var cameraTexture = physicalCamera.AcquireTexture();

            if (cameraTexture == null)
            {
                return;
            }
            var luminanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription()
            {
                Format    = SharpDX.DXGI.Format.R8_UInt,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });
            var chrominanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription()
            {
                Format    = SharpDX.DXGI.Format.R8G8_UInt,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });

            context.PixelShader.SetShaderResource(0, luminanceView);
            context.PixelShader.SetShaderResource(1, chrominanceView);

            context.DrawIndexedInstanced(6, 2, 0, 0, 0);

            luminanceView.Dispose();
            chrominanceView.Dispose();
            physicalCamera.ReleaseTexture();
        }
コード例 #2
0
        private void PerformProjection(MeshCollection meshes)
        {
            var device  = Resources.D3DDevice;
            var context = Resources.D3DDeviceContext;

            var newTriangleCount = meshes.TotalNumberOfTriangles;
            var newNumberOfSide  = (int)Math.Ceiling(Math.Sqrt(newTriangleCount / 2.0));

            context.VertexShader.Set(ProjectionVertexShader);
            context.GeometryShader.Set(ProjectionGeometryShader);
            context.GeometryShader.SetConstantBuffer(3, LayoutConstantBuffer);
            context.PixelShader.Set(ProjectionPixelShader);
            context.PixelShader.SetConstantBuffer(2, CameraConstantBuffer);
            var cameraTexture = Camera.AcquireTexture();

            if (cameraTexture == null)
            {
                return;
            }
            var luminanceView = new ShaderResourceView(device, cameraTexture, new ShaderResourceViewDescription
            {
                Format    = Format.R8_UInt,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });
            var chrominanceView = new ShaderResourceView(device, cameraTexture, new ShaderResourceViewDescription
            {
                Format    = Format.R8G8_UInt,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });

            context.PixelShader.SetShaderResource(1, luminanceView);
            context.PixelShader.SetShaderResource(2, chrominanceView);
            context.PixelShader.SetShaderResource(3, DepthResource);

            context.OutputMerger.SetRenderTargets(null, MeshTextures[CurrentTexture].RenderColorView);

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

            int newOffset = 0;

            meshes.Draw(numberOfIndices =>
            {
                context.DrawIndexed(numberOfIndices, 0, 0);
            },
                        (guid, numberOfIndices) =>
            {
                LayoutData.Offset = (uint)newOffset;
                LayoutData.Size   = (uint)newNumberOfSide;
                newOffset        += numberOfIndices / 3;
                context.UpdateSubresource(ref LayoutData, LayoutConstantBuffer);
                return(true);
            });

            context.PixelShader.SetShaderResource(1, null);
            context.PixelShader.SetShaderResource(2, null);
            context.PixelShader.SetShaderResource(3, null);

            context.OutputMerger.SetRenderTargets(null, (RenderTargetView)null);

            luminanceView.Dispose();
            chrominanceView.Dispose();
            Camera.ReleaseTexture();
        }
コード例 #3
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;
            }
        }