Esempio n. 1
0
        public void Render()
        {
            var device  = this.deviceResources.D3DDevice;
            var context = this.deviceResources.D3DContext;

            float[] ClearColor         = new float[] { 0.0f, 0.55f, 0.55f, 1.0f };
            D3D11RenderTargetView pRTV = this.deviceResources.D3DRenderTargetView;
            D3D11DepthStencilView pDSV = this.deviceResources.D3DDepthStencilView;

            context.ClearRenderTargetView(pRTV, ClearColor);
            context.ClearDepthStencilView(pDSV, D3D11ClearOptions.Depth, 1.0f, 0);

            this.cascadedShadow.InitFrame(device, this.selectedMesh);
            this.cascadedShadow.RenderShadowsForAllCascades(device, context, this.selectedMesh);

            D3D11Viewport vp = new D3D11Viewport(
                0.0f,
                0.0f,
                this.deviceResources.BackBufferWidth,
                this.deviceResources.BackBufferHeight,
                0.0f,
                1.0f);

            this.cascadedShadow.RenderScene(context, pRTV, pDSV, this.selectedMesh, vp, this.VisualizeCascades);

            context.RasterizerStageSetViewports(new D3D11Viewport[] { vp });
            context.OutputMergerSetRenderTargets(new[] { pRTV }, pDSV);
        }
Esempio n. 2
0
 public void SetRenderTarget(D3D11RenderTargetView rtv, D3D11DepthStencilView dsv)
 {
     this.renderTargetView = rtv;
     this.depthStencilView = dsv;
 }
Esempio n. 3
0
        public void CreateWindowSizeDependentResources()
        {
            uint width  = this.deviceResources.BackBufferWidth;
            uint height = this.deviceResources.BackBufferHeight;

            // Create fragment count buffer
            this.fragmentCountBuffer = this.deviceResources.D3DDevice.CreateTexture2D(
                new D3D11Texture2DDesc(
                    DxgiFormat.R32UInt,
                    width,
                    height,
                    1,
                    1,
                    D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource));

            // Create prefix sum buffer
            this.prefixSum = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                             width * height * 4,
                                                                             D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                             D3D11Usage.Default,
                                                                             D3D11CpuAccessOptions.None,
                                                                             D3D11ResourceMiscOptions.None,
                                                                             4));

            // Create the deep frame buffer.
            // This simple allocation scheme for the deep frame buffer allocates space for 8 times the size of the
            // frame buffer, which means that it can hold an average of 8 fragments per pixel.  This will usually waste some
            // space, and in some cases of high overdraw the buffer could run into problems with overflow.  It
            // may be useful to make the buffer size more intelligent to avoid these problems.
            this.deepBuffer = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                              width * height * 8 * 4,
                                                                              D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                              D3D11Usage.Default,
                                                                              D3D11CpuAccessOptions.None,
                                                                              D3D11ResourceMiscOptions.None,
                                                                              4));

            // Create deep frame buffer for color
            this.deepBufferColor = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc(
                                                                                   width * height * 8 * 4 * 1,
                                                                                   D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                                                                                   D3D11Usage.Default,
                                                                                   D3D11CpuAccessOptions.None,
                                                                                   D3D11ResourceMiscOptions.None,
                                                                                   4 * 1));

            this.fragmentCountRV = this.deviceResources.D3DDevice.CreateShaderResourceView(
                this.fragmentCountBuffer,
                new D3D11ShaderResourceViewDesc(
                    D3D11SrvDimension.Texture2D,
                    DxgiFormat.R32UInt,
                    0,
                    1));

            this.fragmentCountUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.fragmentCountBuffer,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Texture2D,
                    DxgiFormat.R32UInt,
                    0));

            this.prefixSumUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.prefixSum,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.R32UInt,
                    0,
                    width * height));

            this.deepBufferUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.deepBuffer,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.R32Float,
                    0,
                    width * height * 8));

            this.deepBufferColorUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(
                this.deepBufferColor,
                new D3D11UnorderedAccessViewDesc(
                    D3D11UavDimension.Buffer,
                    DxgiFormat.B8G8R8A8UNorm,
                    0,
                    width * height * 8));

            this.screenTexture = this.deviceResources.D3DDevice.CreateTexture2D(
                new D3D11Texture2DDesc(
                    DxgiFormat.B8G8R8A8UNorm,
                    width,
                    height,
                    1,
                    1,
                    D3D11BindOptions.RenderTarget | D3D11BindOptions.UnorderedAccess));

            this.screenTextureRTV = this.deviceResources.D3DDevice.CreateRenderTargetView(this.screenTexture, null);
            this.screenTextureUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(this.screenTexture, new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Texture2D, DxgiFormat.B8G8R8A8UNorm, 0));
        }
Esempio n. 4
0
        private void CreateWindowSizeDependentResources()
        {
            this.d3dContext.OutputMergerSetRenderTargets(new D3D11RenderTargetView[] { null }, null);

            D3D11Utils.DisposeAndNull(ref this.backBuffer);
            D3D11Utils.DisposeAndNull(ref this.offscreenBuffer);
            D3D11Utils.DisposeAndNull(ref this.d3dRenderTargetView);
            D3D11Utils.DisposeAndNull(ref this.d3dDepthStencilView);
            D2D1Utils.DisposeAndNull(ref this.d2dRenderTarget);

            this.d3dContext.Flush();

            var createdBackBuffer = this.OnCreateBackBuffer();

            if (createdBackBuffer == null)
            {
                return;
            }

            this.backBuffer = createdBackBuffer;

            var backBufferDesc = this.backBuffer.Description;

            this.backBufferWidth  = backBufferDesc.Width;
            this.backBufferHeight = backBufferDesc.Height;

            if (this.d3dSampleDesc.Count > 1)
            {
                D3D11Texture2DDesc desc = new D3D11Texture2DDesc(
                    DxgiFormat.B8G8R8A8UNorm,
                    this.backBufferWidth,
                    this.backBufferHeight,
                    1,
                    1,
                    D3D11BindOptions.RenderTarget,
                    D3D11Usage.Default,
                    D3D11CpuAccessOptions.None,
                    this.d3dSampleDesc.Count,
                    this.d3dSampleDesc.Quality,
                    D3D11ResourceMiscOptions.None);

                this.offscreenBuffer = this.D3DDevice.CreateTexture2D(desc);
            }

            if (this.d3dSampleDesc.Count > 1)
            {
                D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2DMs);

                this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.offscreenBuffer, renderTargetViewDesc);
            }
            else
            {
                D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2D);

                this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.backBuffer, renderTargetViewDesc);
            }

            D3D11Texture2DDesc depthStencilDesc = new D3D11Texture2DDesc
            {
                Width            = this.backBufferWidth,
                Height           = this.backBufferHeight,
                MipLevels        = 1,
                ArraySize        = 1,
                Format           = DxgiFormat.D24UNormS8UInt,
                SampleDesc       = this.d3dSampleDesc,
                Usage            = D3D11Usage.Default,
                BindOptions      = D3D11BindOptions.DepthStencil,
                CpuAccessOptions = D3D11CpuAccessOptions.None,
                MiscOptions      = D3D11ResourceMiscOptions.None
            };

            using (var depthStencil = this.d3dDevice.CreateTexture2D(depthStencilDesc))
            {
                D3D11DepthStencilViewDesc depthStencilViewDesc = new D3D11DepthStencilViewDesc(this.d3dSampleDesc.Count > 1 ? D3D11DsvDimension.Texture2DMs : D3D11DsvDimension.Texture2D);

                this.d3dDepthStencilView = this.d3dDevice.CreateDepthStencilView(depthStencil, depthStencilViewDesc);
            }

            this.screenViewport = new D3D11Viewport
            {
                TopLeftX = 0,
                TopLeftY = 0,
                Width    = this.backBufferWidth,
                Height   = this.backBufferHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f
            };

            this.d3dContext.RasterizerStageSetViewports(new[] { this.screenViewport });

            using (var surface = new DxgiSurface2(this.d3dSampleDesc.Count > 1 ? this.offscreenBuffer.Handle : this.backBuffer.Handle))
            {
                float dpiX;
                float dpiY;
                this.d2dFactory.GetDesktopDpi(out dpiX, out dpiY);

                var properties = new D2D1RenderTargetProperties(
                    D2D1RenderTargetType.Default,
                    new D2D1PixelFormat(DxgiFormat.B8G8R8A8UNorm, D2D1AlphaMode.Premultiplied),
                    dpiX,
                    dpiY,
                    D2D1RenderTargetUsages.None,
                    D2D1FeatureLevel.Default);

                this.d2dRenderTarget = this.d2dFactory.CreateDxgiSurfaceRenderTarget(surface, properties);
            }

            this.d2dRenderTarget.AntialiasMode     = D2D1AntialiasMode.PerPrimitive;
            this.d2dRenderTarget.TextAntialiasMode = D2D1TextAntialiasMode.Grayscale;

            D3D11RasterizerDesc rasterizerStateDesc = new D3D11RasterizerDesc(D3D11FillMode.Solid, D3D11CullMode.Back, false, 0, 0.0f, 0.0f, true, false, true, false);

            using (var rasterizerState = this.d3dDevice.CreateRasterizerState(rasterizerStateDesc))
            {
                this.d3dContext.RasterizerStageSetState(rasterizerState);
            }
        }
Esempio n. 5
0
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            D3D11RenderTargetView[]   pRTV = new D3D11RenderTargetView[2];
            D3D11ShaderResourceView[] pSRV = new D3D11ShaderResourceView[8];

            // Array of our samplers
            D3D11SamplerState[] ppSamplerStates = new D3D11SamplerState[3] {
                this.g_pSamplePoint, this.g_pSampleLinear, this.g_pSamplePointCmp
            };
            context.PixelShaderSetSamplers(0, ppSamplerStates);

            // Store off original render target, this is the back buffer of the swap chain
            D3D11RenderTargetView pOrigRTV = this.deviceResources.D3DRenderTargetView;
            D3D11DepthStencilView pOrigDSV = this.deviceResources.D3DDepthStencilView;

            // Clear the render target
            float[] ClearColor = { 0.0f, 0.25f, 0.25f, 1.0f };
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, ClearColor);
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0);

            // disable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateColorWritesOff, null, 0xffffffff);

            this.RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir);

            // enable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateNoBlend, null, 0xffffffff);

            // Get the projection & view matrix from the camera class
            XMMatrix mView = this.ViewMatrix;
            XMMatrix mProj = this.ProjectionMatrix;
            XMMatrix mWorldViewProjection = mView * mProj;

            // Setup the constant buffer for the scene vertex shader
            ConstantBufferConstants pConstants = new ConstantBufferConstants
            {
                WorldViewProjection = mWorldViewProjection.Transpose(),
                WorldViewProjLight  = mViewProjLight.ToMatrix().Transpose(),
                ShadowMapDimensions = new XMFloat4(
                    g_fShadowMapWidth,
                    g_fShadowMapHeight,
                    1.0f / g_fShadowMapWidth,
                    1.0f / g_fShadowMapHeight),
                LightDir = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, 0.0f),
                SunWidth = this.SunWidth
            };

            context.UpdateSubresource(this.g_pcbConstants, 0, null, pConstants, 0, 0);
            context.VertexShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });
            context.PixelShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });

            // Set the shaders
            context.VertexShaderSetShader(this.g_pSceneVS, null);
            context.PixelShaderSetShader(this.g_pScenePS, null);

            // Set the vertex buffer format
            context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout);

            // Rebind to original back buffer and depth buffer
            pRTV[0] = pOrigRTV;
            context.OutputMergerSetRenderTargets(pRTV, pOrigDSV);

            // set the shadow map
            context.PixelShaderSetShaderResources(1, new[] { this.g_pDepthTextureSRV });

            // Render the scene
            this.g_SceneMesh.Render(0, -1, -1);
            this.g_Poles.Render(0, -1, -1);

            // restore resources
            context.PixelShaderSetShaderResources(0, pSRV);
        }
Esempio n. 6
0
        private void RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir)
        {
            var context = this.deviceResources.D3DContext;

            D3D11Rect[]     oldRects = context.RasterizerStageGetScissorRects();
            D3D11Viewport[] oldVp    = context.RasterizerStageGetViewports();

            D3D11Rect[] rects = new D3D11Rect[1] {
                new D3D11Rect(0, (int)g_fShadowMapWidth, 0, (int)g_fShadowMapHeight)
            };
            context.RasterizerStageSetScissorRects(rects);

            D3D11Viewport[] vp = new D3D11Viewport[1] {
                new D3D11Viewport(0, 0, g_fShadowMapWidth, g_fShadowMapHeight, 0.0f, 1.0f)
            };
            context.RasterizerStageSetViewports(vp);

            // Set our scene render target & keep original depth buffer
            D3D11RenderTargetView[] pRTVs = new D3D11RenderTargetView[2];
            context.OutputMergerSetRenderTargets(pRTVs, this.g_pDepthStencilTextureDSV);

            // Clear the render target
            context.ClearDepthStencilView(this.g_pDepthStencilTextureDSV, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0);

            // Get the projection & view matrix from the camera class
            XMFloat3 up           = new XMFloat3(0, 1, 0);
            XMFloat4 vLight       = new XMFloat4(0.0f, 0.0f, 0.0f, 1.0f);
            XMFloat4 vLightLookAt = new XMFloat4(0.0f, -0.5f, 1.0f, 0.0f);

            vLightLookAt = vLight.ToVector() + vLightLookAt.ToVector();
            vLight       = XMVector4.Transform(vLight, this.LightWorldMatrix);
            vLightLookAt = XMVector4.Transform(vLightLookAt, this.LightWorldMatrix);

            vLightDir = XMVector.Subtract(vLightLookAt, vLight);

            XMMatrix mProj = XMMatrix.OrthographicOffCenterLH(-8.5f, 9, -15, 11, -20, 20);
            XMMatrix mView = XMMatrix.LookAtLH(vLight, vLightLookAt, up);

            mViewProjLight = mView * mProj;

            // Setup the constant buffer for the scene vertex shader
            ConstantBufferConstants pConstants = new ConstantBufferConstants
            {
                WorldViewProjection = mViewProjLight.ToMatrix().Transpose(),
                WorldViewProjLight  = mViewProjLight.ToMatrix().Transpose(),
                ShadowMapDimensions = new XMFloat4(
                    g_fShadowMapWidth,
                    g_fShadowMapHeight,
                    1.0f / g_fShadowMapWidth,
                    1.0f / g_fShadowMapHeight),
                LightDir = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, 0.0f),
                SunWidth = this.SunWidth
            };

            context.UpdateSubresource(this.g_pcbConstants, 0, null, pConstants, 0, 0);
            context.VertexShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });
            context.PixelShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });

            // Set the shaders
            context.VertexShaderSetShader(this.g_pShadowMapVS, null);
            context.PixelShaderSetShader(null, null);

            // Set the vertex buffer format
            context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout);

            // Render the scene
            this.g_Poles.Render(0, -1, -1);

            // reset the old viewport etc.
            context.RasterizerStageSetScissorRects(oldRects);
            context.RasterizerStageSetViewports(oldVp);
        }