Example #1
0
        private void Render()
        {
            {
                var ctx        = m_swapChain.CreateGraphicsComputeCommandContext();
                var backBuffer = m_swapChain.BackBuffer;
                var size       = backBuffer.Size2D;
                var w          = (uint)backBuffer.Size2D.Width;
                var h          = (uint)backBuffer.Size2D.Height;

                var frameColorBuffer = m_ctx.CreateFrameColorBuffer(w, h, GraphicsFormat.R8G8B8A8_UNORM, ResourceState.RenderTarget);
                var frameDepthBuffer = m_ctx.CreateFrameDepthBuffer(w, h, DepthBufferFormat.Depth32Single, ResourceState.DepthWrite);
                var postProcess      = m_ctx.CreateFrameColorBuffer(w, h, GraphicsFormat.R8G8B8A8_UNORM, ResourceState.UnorderedAccess);

                ctx.SetRenderTarget(frameColorBuffer, frameDepthBuffer);
                ctx.SetGraphicsPipelineStateObject(m_triangle);
                ctx.SetDescriptorHeaps();
                ctx.Clear(frameColorBuffer);
                ctx.Clear(frameDepthBuffer);

                ctx.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

                {
                    Size2D s = size;

                    {
                        ViewPort v;

                        v.MinDepth = 0.0f;
                        v.MaxDepth = 1.0f;
                        v.TopLeftX = 0.0f;
                        v.TopLeftY = 0.0f;
                        v.Width    = s.Width;
                        v.Height   = s.Height;
                        ctx.SetViewPort(v);
                    }

                    {
                        Rectangle2D v;

                        v.Left   = 0;
                        v.Top    = 0;
                        v.Right  = s.Width;
                        v.Bottom = s.Height;

                        ctx.SetScissorRectangle(v);
                    }
                }

                ctx.SetGraphicsSRV(5, 0, m_buffer);
                ctx.SetGraphicsSRV(5, 1, m_texture);
                ctx.Draw(3, 0);

                ctx.TransitionResource(frameColorBuffer, ResourceState.RenderTarget, ResourceState.NonPixelShaderResource);
                ctx.SetComputePipelineStateObject(m_compute);

                ctx.SetComputeUAV(4, 0, postProcess);
                ctx.SetComputeSRV(5, 0, frameColorBuffer);
                ctx.Dispatch((w + 7) / 8, (h + 7) / 8, 1);

                ctx.TransitionResource(backBuffer, ResourceState.Present, ResourceState.CopyDestination);
                ctx.TransitionResource(postProcess, ResourceState.UnorderedAccess, ResourceState.CopySource);
                ctx.CopyResource(backBuffer, postProcess);
                ctx.TransitionResource(backBuffer, ResourceState.CopyDestination, ResourceState.Present);
                ctx.Submit();
            }

            m_swapChain.Present();

            m_swapChain.MoveToNextFrame();

            //flush all buffers
            m_ctx.Sync();
            m_swapChain.Sync();
        }
        private void Render()
        {
            {
                var ctx        = m_swapChain.CreateGraphicsComputeCommandContext();
                var backBuffer = m_swapChain.BackBuffer;
                var size       = backBuffer.Size2D;
                var w          = (uint)backBuffer.Size2D.Width;
                var h          = (uint)backBuffer.Size2D.Height;

                var albedo = m_ctx.CreateFrameColorBuffer(w, h, GraphicsFormat.R8G8B8A8_UNORM, ResourceState.RenderTarget);
                var depth  = m_ctx.CreateFrameDepthBuffer(w, h, DepthBufferFormat.Depth32Single, ResourceState.DepthWrite);

                ctx.SetRenderTarget(albedo, depth);
                ctx.SetDescriptorHeaps();
                ctx.Clear(albedo);
                ctx.Clear(depth);

                AlbedoPassData albedoPass;
                DepthPassData  depthPass;

                {
                    Size2D s = size;

                    {
                        ViewPort v;

                        v.MinDepth = 0.0f;
                        v.MaxDepth = 1.0f;
                        v.TopLeftX = 0.0f;
                        v.TopLeftY = 0.0f;
                        v.Width    = s.Width;
                        v.Height   = s.Height;

                        albedoPass.ViewPort.Width    = v.Width;
                        albedoPass.ViewPort.Height   = v.Height;
                        albedoPass.ViewPort.MinimumZ = v.MinDepth;
                        albedoPass.ViewPort.MaximumZ = v.MaxDepth;

                        depthPass.ViewPort.Width    = v.Width;
                        depthPass.ViewPort.Height   = v.Height;
                        depthPass.ViewPort.MinimumZ = v.MinDepth;
                        depthPass.ViewPort.MaximumZ = v.MaxDepth;

                        ctx.SetViewPort(v);
                    }

                    {
                        Rectangle2D v;

                        v.Left   = 0;
                        v.Top    = 0;
                        v.Right  = s.Width;
                        v.Bottom = s.Height;

                        ctx.SetScissorRectangle(v);
                    }
                }


                albedoPass.Camera.ViewTransform        = CameraMatrix();
                albedoPass.Camera.PerspectiveTransform = PerspectiveMatrix();
                depthPass.Camera.ViewTransform         = CameraMatrix();
                depthPass.Camera.PerspectiveTransform  = PerspectiveMatrix();

                //Depth prime the buffer
                {
                    m_mechanicMaterial.SubmitDepth(depthPass, ctx);
                    m_mechanicModel.SubmitDepth(ctx);
                    m_mechanicInstance.SubmitDepth(ctx);
                }

                //Read from the buffer, submit with depth test
                ctx.TransitionResource(depth, ResourceState.DepthWrite, ResourceState.DepthRead);

                {
                    //Submit per material data
                    m_mechanicMaterial.SubmitAlbedo(albedoPass, ctx);
                    //Submit Per Model Data
                    m_mechanicModel.SubmitAlbedo(ctx);
                    //Submit as many instances with different world matrices
                    m_mechanicInstance.SubmitAlbedo(ctx);
                }

                ctx.TransitionResource(albedo, ResourceState.RenderTarget, ResourceState.CopySource);
                ctx.TransitionResource(backBuffer, ResourceState.Present, ResourceState.CopyDestination);
                ctx.CopyResource(backBuffer, albedo);
                ctx.TransitionResource(backBuffer, ResourceState.CopyDestination, ResourceState.Present);
                ctx.Submit();
            }

            m_swapChain.Present();

            m_swapChain.MoveToNextFrame();

            //flush all buffers
            m_ctx.Sync();
            m_swapChain.Sync();
        }