static void CreateResources() { var factory = _graphicsDevice.ResourceFactory; VertexPositionColor[] vertices = { new VertexPositionColor(new Vector2(-.75f, .75f), RgbaFloat.Red), new VertexPositionColor(new Vector2(.75f, .75f), RgbaFloat.Green), new VertexPositionColor(new Vector2(-.75f, -.75f), RgbaFloat.Blue), new VertexPositionColor(new Vector2(.75f, -.75f), RgbaFloat.Yellow) }; ushort[] indicies = { 0, 1, 2, 3 }; _vertexBuffer = factory.CreateBuffer(new BufferDescription(4 * VertexPositionColor.SizeInBytes, BufferUsage.VertexBuffer)); _indexBuffer = factory.CreateBuffer(new BufferDescription(4 * sizeof(uint), BufferUsage.IndexBuffer)); _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, vertices); _graphicsDevice.UpdateBuffer(_indexBuffer, 0, indicies); var description = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float4)); _vertexShader = LoadShader(ShaderStages.Vertex); _fragmentShader = LoadShader(ShaderStages.Fragment); var pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = new DepthStencilStateDescription(true, true, ComparisonKind.LessEqual), RasterizerState = new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, false), PrimitiveTopology = PrimitiveTopology.TriangleStrip, ResourceLayouts = System.Array.Empty <ResourceLayout>(), ShaderSet = new ShaderSetDescription(new[] { description }, new[] { _vertexShader, _fragmentShader }), Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = factory.CreateGraphicsPipeline(pipelineDescription); _commandList = factory.CreateCommandList(); }
private void CreateResources(ResourceFactory factory) { _screenSizeBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); _shiftBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); _vertexBuffer = factory.CreateBuffer(new BufferDescription(16 * 4, BufferUsage.VertexBuffer)); _indexBuffer = factory.CreateBuffer(new BufferDescription(2 * 6, BufferUsage.IndexBuffer)); _computeShader = factory.CreateShader(new ShaderDescription( ShaderStages.Compute, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Compute.{GetExtension(factory.BackendType)}")))); _computeLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Tex", ResourceKind.TextureReadWrite, ShaderStages.Compute), new ResourceLayoutElementDescription("ScreenSizeBuffer", ResourceKind.UniformBuffer, ShaderStages.Compute), new ResourceLayoutElementDescription("ShiftBuffer", ResourceKind.UniformBuffer, ShaderStages.Compute))); ComputePipelineDescription computePipelineDesc = new ComputePipelineDescription( new ShaderStageDescription(ShaderStages.Compute, _computeShader, "CS"), new[] { _computeLayout }); _computePipeline = factory.CreateComputePipeline(ref computePipelineDesc); _vertexShader = factory.CreateShader(new ShaderDescription( ShaderStages.Vertex, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Vertex.{GetExtension(factory.BackendType)}")))); _fragmentShader = factory.CreateShader(new ShaderDescription( ShaderStages.Fragment, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Fragment.{GetExtension(factory.BackendType)}")))); ShaderSetDescription shaderSet = new ShaderSetDescription( new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("TexCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { new ShaderStageDescription(ShaderStages.Vertex, _vertexShader, "VS"), new ShaderStageDescription(ShaderStages.Fragment, _fragmentShader, "FS") }); _graphicsLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Tex", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Tex11", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Tex22", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SS", ResourceKind.Sampler, ShaderStages.Fragment))); GraphicsPipelineDescription fullScreenQuadDesc = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.Disabled, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, false, false), PrimitiveTopology.TriangleList, shaderSet, new[] { _graphicsLayout }, _gd.SwapchainFramebuffer.OutputDescription); _graphicsPipeline = factory.CreateGraphicsPipeline(ref fullScreenQuadDesc); _cl = factory.CreateCommandList(); CreateWindowSizedResources(); InitResources(factory); }
private void CreateResources(ResourceFactory factory) { _particleBuffer = factory.CreateBuffer( new BufferDescription( (uint)Unsafe.SizeOf <ParticleInfo>() * ParticleCount, BufferUsage.StructuredBufferReadWrite, (uint)Unsafe.SizeOf <ParticleInfo>())); _screenSizeBuffer = factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); _computeShader = factory.CreateShader(new ShaderDescription( ShaderStages.Compute, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Compute.{GetExtension(factory.BackendType)}")))); ResourceLayout particleStorageLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ParticlesBuffer", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute))); ResourceLayout screenSizeLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ScreenSizeBuffer", ResourceKind.UniformBuffer, ShaderStages.Compute))); ComputePipelineDescription computePipelineDesc = new ComputePipelineDescription( new ShaderStageDescription(ShaderStages.Compute, _computeShader, "CS"), new[] { particleStorageLayout, screenSizeLayout }); _computePipeline = factory.CreateComputePipeline(ref computePipelineDesc); _computeResourceSet = factory.CreateResourceSet(new ResourceSetDescription(particleStorageLayout, _particleBuffer)); _computeScreenSizeResourceSet = factory.CreateResourceSet(new ResourceSetDescription(screenSizeLayout, _screenSizeBuffer)); _vertexShader = factory.CreateShader(new ShaderDescription( ShaderStages.Vertex, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Vertex.{GetExtension(factory.BackendType)}")))); _fragmentShader = factory.CreateShader(new ShaderDescription( ShaderStages.Fragment, File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Shaders", $"Fragment.{GetExtension(factory.BackendType)}")))); ShaderSetDescription shaderSet = new ShaderSetDescription( Array.Empty <VertexLayoutDescription>(), new[] { new ShaderStageDescription(ShaderStages.Vertex, _vertexShader, "VS"), new ShaderStageDescription(ShaderStages.Fragment, _fragmentShader, "FS") }); particleStorageLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ParticlesBuffer", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex))); screenSizeLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ScreenSizeBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex))); GraphicsPipelineDescription particleDrawPipelineDesc = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.Disabled, RasterizerStateDescription.Default, PrimitiveTopology.PointList, shaderSet, new[] { particleStorageLayout, screenSizeLayout }, _gd.SwapchainFramebuffer.OutputDescription); _graphicsPipeline = factory.CreateGraphicsPipeline(ref particleDrawPipelineDesc); _graphicsParticleResourceSet = factory.CreateResourceSet(new ResourceSetDescription( particleStorageLayout, _particleBuffer)); _screenSizeResourceSet = factory.CreateResourceSet(new ResourceSetDescription( screenSizeLayout, _screenSizeBuffer)); _cl = factory.CreateCommandList(); InitResources(factory); }
private void CreateResources(ResourceFactory factory) { _cl = factory.CreateCommandList(); _cl.Begin(); _projectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _viewBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); _worldBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); VertexPositionTexture[] vertices = GetCubeVertices(); _vertexBuffer = factory.CreateBuffer(new BufferDescription((uint)(VertexPositionTexture.SizeInBytes * vertices.Length), BufferUsage.VertexBuffer)); _cl.UpdateBuffer(_vertexBuffer, 0, vertices); ushort[] indices = GetCubeIndices(); _indexBuffer = factory.CreateBuffer(new BufferDescription(sizeof(ushort) * (uint)indices.Length, BufferUsage.IndexBuffer)); _cl.UpdateBuffer(_indexBuffer, 0, indices); ImageSharpTexture stoneImage = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "spnza_bricks_a_diff.png")); _surfaceTexture = stoneImage.CreateDeviceTexture(_gd, factory); _surfaceTextureView = factory.CreateTextureView(_surfaceTexture); _cl.End(); _gd.ExecuteCommands(_cl); _gd.WaitForIdle(); ShaderSetDescription shaderSet = new ShaderSetDescription( new[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)) }, new[] { new ShaderStageDescription(ShaderStages.Vertex, LoadShader(factory, "Cube", ShaderStages.Vertex), "VS"), new ShaderStageDescription(ShaderStages.Fragment, LoadShader(factory, "Cube", ShaderStages.Fragment), "FS") }); ResourceLayout projViewLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex))); ResourceLayout worldTextureLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); _pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.LessEqual, RasterizerStateDescription.Default, PrimitiveTopology.TriangleList, shaderSet, new[] { projViewLayout, worldTextureLayout }, _gd.SwapchainFramebuffer.OutputDescription)); _projViewSet = factory.CreateResourceSet(new ResourceSetDescription( projViewLayout, _projectionBuffer, _viewBuffer)); _worldTextureSet = factory.CreateResourceSet(new ResourceSetDescription( worldTextureLayout, _worldBuffer, _surfaceTextureView, _gd.Aniso4xSampler)); }
private unsafe void RenderImDrawData(DrawData *draw_data, GraphicsDevice gd, CommandList cl) { uint vertexOffsetInVertices = 0; uint indexOffsetInElements = 0; if (draw_data->CmdListsCount == 0) { return; } uint totalVBSize = (uint)(draw_data->TotalVtxCount * sizeof(DrawVert)); if (totalVBSize > _vertexBuffer.SizeInBytes) { _vertexBuffer.Dispose(); _vertexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalVBSize * 1.5f), BufferUsage.VertexBuffer | BufferUsage.Dynamic)); } uint totalIBSize = (uint)(draw_data->TotalIdxCount * sizeof(ushort)); if (totalIBSize > _indexBuffer.SizeInBytes) { _indexBuffer.Dispose(); _indexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalIBSize * 1.5f), BufferUsage.IndexBuffer | BufferUsage.Dynamic)); } MappedResource vbMap = _gd.Map(_vertexBuffer, MapMode.Write); MappedResource ibMap = _gd.Map(_indexBuffer, MapMode.Write); for (int i = 0; i < draw_data->CmdListsCount; i++) { NativeDrawList *cmd_list = draw_data->CmdLists[i]; Unsafe.CopyBlock( (byte *)vbMap.Data.ToPointer() + vertexOffsetInVertices * sizeof(DrawVert), cmd_list->VtxBuffer.Data, (uint)(cmd_list->VtxBuffer.Size * sizeof(DrawVert))); Unsafe.CopyBlock( (byte *)ibMap.Data.ToPointer() + indexOffsetInElements * sizeof(ushort), cmd_list->IdxBuffer.Data, (uint)(cmd_list->IdxBuffer.Size * sizeof(ushort))); vertexOffsetInVertices += (uint)cmd_list->VtxBuffer.Size; indexOffsetInElements += (uint)cmd_list->IdxBuffer.Size; } _gd.Unmap(_vertexBuffer); _gd.Unmap(_indexBuffer); // Setup orthographic projection matrix into our constant buffer { var io = ImGui.GetIO(); Matrix4x4 mvp = Matrix4x4.CreateOrthographicOffCenter( 0f, io.DisplaySize.X, io.DisplaySize.Y, 0.0f, -1.0f, 1.0f); cl.UpdateBuffer(_projMatrixBuffer, 0, ref mvp); } cl.SetVertexBuffer(0, _vertexBuffer); cl.SetIndexBuffer(_indexBuffer, IndexFormat.UInt16); cl.SetPipeline(_pipeline); cl.SetGraphicsResourceSet(0, _mainResourceSet); ImGui.ScaleClipRects(draw_data, ImGui.GetIO().DisplayFramebufferScale); // Render command lists int vtx_offset = 0; int idx_offset = 0; for (int n = 0; n < draw_data->CmdListsCount; n++) { NativeDrawList *cmd_list = draw_data->CmdLists[n]; for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]); if (pcmd->UserCallback != IntPtr.Zero) { throw new NotImplementedException(); } else { if (pcmd->TextureId != IntPtr.Zero) { if (pcmd->TextureId == _fontAtlasID) { cl.SetGraphicsResourceSet(1, _fontTextureResourceSet); } else { cl.SetGraphicsResourceSet(1, GetImageResourceSet(pcmd->TextureId)); } } cl.SetScissorRect( 0, (uint)pcmd->ClipRect.X, (uint)pcmd->ClipRect.Y, (uint)pcmd->ClipRect.Z, (uint)pcmd->ClipRect.W); cl.DrawIndexed(pcmd->ElemCount, 1, (uint)idx_offset, vtx_offset, 0); } idx_offset += (int)pcmd->ElemCount; } vtx_offset += cmd_list->VtxBuffer.Size; } }