public MainPage() { this.InitializeComponent(); m_ctx = new ResourceCreateContext(); m_swapChain = new CompositionSwapChainResources(m_ctx, m_swapChainPanel); var display = DisplayInformation.GetForCurrentView(); display.DpiChanged += new TypedEventHandler <DisplayInformation, object>(OnDpiChanged); m_triangle = makeTriangle(m_ctx); m_compute = makeCompute(m_ctx); m_buffer = m_ctx.CreateByteAddressBuffer(4096, ResourceState.CopyDestination); m_texture = m_ctx.CreateTexture2D(256, 256, 1, GraphicsFormat.R8_UNORM, ResourceState.CopyDestination); var ctx = m_swapChain.CreateGraphicsComputeCommandContext(); { float[] positions = { 0.0f, 0.5f, 0.5f, 1.0f, -0.5f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f, 0.5f, 1.0f }; byte[] b0 = new byte[positions.Length * 4]; Buffer.BlockCopy(positions, 0, b0, 0, b0.Length); float[] colors = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; byte[] b1 = new byte[colors.Length * 4]; Buffer.BlockCopy(colors, 0, b1, 0, b1.Length); ctx.UpdateBuffer(m_buffer, 0, b0); ctx.UpdateBuffer(m_buffer, (uint)b0.Length, b1); } { var subResourceData = new SubresourceData(); subResourceData.Data = GenerateTextureData(); subResourceData.RowPitch = 256; subResourceData.SlicePitch = 256 * 256; SubresourceData[] datas = { subResourceData }; ctx.UploadResource(m_texture, 0, 1, datas); } ctx.TransitionResource(m_texture, ResourceState.CopyDestination, ResourceState.PixelShaderResource | ResourceState.NonPixelShaderResource); ctx.TransitionResource(m_buffer, ResourceState.CopyDestination, ResourceState.NonPixelShaderResource); ctx.SubmitAndWaitToExecute(); }
public MainPage() { this.InitializeComponent(); m_ctx = new ResourceCreateContext(); m_swapChain = new CompositionSwapChainResources(m_ctx, m_swapChainPanel); var display = DisplayInformation.GetForCurrentView(); display.DpiChanged += new TypedEventHandler <DisplayInformation, object>(OnDpiChanged); var ctx = m_swapChain.CreateGraphicsComputeCommandContext(); m_mechanicMaterial = new DerivativesSkinnedMaterial(m_ctx, ctx, ""); m_mechanicModel = new DerivativesSkinnedModel(m_mechanicMaterial, m_ctx, ctx, @"Assets\\models\\military_mechanic.derivatives_skinned_model.model"); m_mechanicInstance = new DerivativesSkinnedModelInstance(m_mechanicModel, identity()); ctx.SubmitAndWaitToExecute(); m_lighting = makeLighting(m_ctx); }
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(); }