Exemplo n.º 1
0
        public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription, ColorSpaceHandling colorSpaceHandling)
        {
            _gd = gd;
            _colorSpaceHandling = colorSpaceHandling;
            ResourceFactory factory = gd.ResourceFactory;

            _vertexBuffer      = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic));
            _vertexBuffer.Name = "ImGui.NET Vertex Buffer";
            _indexBuffer       = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic));
            _indexBuffer.Name  = "ImGui.NET Index Buffer";
            RecreateFontDeviceTexture(gd);

            _projMatrixBuffer      = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            _projMatrixBuffer.Name = "ImGui.NET Projection Buffer";

            byte[] vertexShaderBytes   = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex, _colorSpaceHandling);
            byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment, _colorSpaceHandling);
            _vertexShader   = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "VS"));
            _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "FS"));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2),
                    new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                    new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm))
            };

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment)));
            _textureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                              new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                new DepthStencilStateDescription(false, false, ComparisonKind.Always),
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(
                    vertexLayouts,
                    new[] { _vertexShader, _fragmentShader },
                    new[]
            {
                new SpecializationConstant(0, gd.IsClipSpaceYInverted),
                new SpecializationConstant(1, _colorSpaceHandling == ColorSpaceHandling.Legacy),
            }),
                new ResourceLayout[] { _layout, _textureLayout },
                outputDescription,
                ResourceBindingModel.Default);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout,
                                                                                    _projMatrixBuffer,
                                                                                    gd.PointSampler));

            _fontTextureResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_textureLayout, _fontTextureView));
        }
Exemplo n.º 2
0
        public void CreateDeviceResources(GraphicsDevice gd, CommandList cl, OutputDescription outputDescription)
        {
            _gd = gd;
            ResourceFactory factory = gd.ResourceFactory;

            _vertexBuffer = factory.CreateVertexBuffer(new BufferDescription(10000, true));
            _indexBuffer  = factory.CreateIndexBuffer(new IndexBufferDescription(2000, IndexFormat.UInt16, true));
            RecreateFontDeviceTexture(gd, cl);

            _projMatrixBuffer = factory.CreateUniformBuffer(new BufferDescription(64));

            byte[] vertexShaderBytes   = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex);
            byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment);
            _vertexShader   = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes));
            _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2),
                    new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                    new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4))
            };

            ShaderStageDescription[] shaderStages = new ShaderStageDescription[]
            {
                new ShaderStageDescription(ShaderStages.Vertex, _vertexShader, "VS"),
                new ShaderStageDescription(ShaderStages.Fragment, _fragmentShader, "FS")
            };

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.Uniform, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("FontTexture", ResourceKind.Texture, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("FontSampler", ResourceKind.Sampler, ShaderStages.Fragment)));


            PipelineDescription pd = new PipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                new DepthStencilStateDescription(false, false, DepthComparisonKind.Always),
                new RasterizerStateDescription(FaceCullMode.None, TriangleFillMode.Solid, FrontFace.Clockwise, false, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, shaderStages),
                new ResourceLayout[] { _layout },
                outputDescription);

            _pipeline = factory.CreatePipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout,
                                                                                _projMatrixBuffer,
                                                                                _fontTextureView,
                                                                                gd.PointSampler));
        }
Exemplo n.º 3
0
        public void CreateDeviceResources(RenderContext rc)
        {
            _rc = rc;
            ResourceFactory factory = rc.ResourceFactory;

            _vertexBuffer = factory.CreateVertexBuffer(1000, true);
            _indexBuffer  = factory.CreateIndexBuffer(500, true);
            _blendState   = factory.CreateCustomBlendState(
                true,
                Blend.InverseSourceAlpha, Blend.Zero, BlendFunction.Add,
                Blend.SourceAlpha, Blend.InverseSourceAlpha, BlendFunction.Add,
                RgbaFloat.Black);
            _depthDisabledState = factory.CreateDepthStencilState(false, DepthComparison.Always);
            _rasterizerState    = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true);
            RecreateFontDeviceTexture(rc);

            var    vertexShaderCode   = LoadEmbeddedShaderCode(rc.ResourceFactory, "imgui-vertex", ShaderStages.Vertex);
            var    fragmentShaderCode = LoadEmbeddedShaderCode(rc.ResourceFactory, "imgui-frag", ShaderStages.Fragment);
            Shader vertexShader       = factory.CreateShader(ShaderStages.Vertex, vertexShaderCode);
            Shader fragmentShader     = factory.CreateShader(ShaderStages.Fragment, fragmentShaderCode);

            VertexInputLayout inputLayout = factory.CreateInputLayout(
                new VertexInputDescription(20, new VertexInputElement[]
            {
                new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float2),
                new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2),
                new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4)
            }));

            _shaderSet = factory.CreateShaderSet(inputLayout, vertexShader, fragmentShader);

            _resourceBindings = factory.CreateShaderResourceBindingSlots(
                _shaderSet,
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("FontTexture", ShaderResourceType.Texture),
                new ShaderResourceDescription("FontSampler", ShaderResourceType.Sampler));
            _projMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
        }