Beispiel #1
0
 public Effect(
     GraphicsDevice graphicsDevice,
     string shaderName,
     VertexLayoutDescription vertexDescriptor)
     : this(graphicsDevice, shaderName, new[] { vertexDescriptor })
 {
 }
Beispiel #2
0
        public virtual bool Load(GraphicsDevice graphicsDevice, string vertexShaderFileName, string fragmentShaderFileName)
        {
            _vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription(nameof(Vertex.Position), VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                new VertexElementDescription(nameof(Vertex.Normal), VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                new VertexElementDescription(nameof(Vertex.TextureCoordinate), VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2));

            try
            {
                byte[]            vertexShaderBytes = File.ReadAllBytes(vertexShaderFileName);
                ShaderDescription vertexShaderDesc  = new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "main");

                byte[]            fragmentShaderBytes = File.ReadAllBytes(fragmentShaderFileName);
                ShaderDescription fragmentShaderDesc  = new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "main");

                _shaders = graphicsDevice.ResourceFactory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc);

                ShaderSet = new ShaderSetDescription(new[] { _vertexLayout }, _shaders);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                Dispose();

                return(false);
            }

            return(true);
        }
        private void CreateResources()
        {
            var rf = GraphicsDevice.ResourceFactory;

            _commandList = rf.CreateCommandList();

            var vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm),
                new VertexElementDescription("TextureCoordinate", VertexElementSemantic.TextureCoordinate,
                                             VertexElementFormat.Float2));

            _vertexShader   = VeldridHelper.LoadShader(rf, "SpriteShader", ShaderStages.Vertex, "VS");
            _fragmentShader = VeldridHelper.LoadShader(rf, "SpriteShader", ShaderStages.Fragment, "FS");

            _shaderSet = new ShaderSetDescription(
                new[] { vertexLayout },
                new[] { _vertexShader, _fragmentShader });

            _wvpLayout = rf.CreateResourceLayout(new ResourceLayoutDescription(
                                                     new ResourceLayoutElementDescription("Wvp", ResourceKind.UniformBuffer, ShaderStages.Vertex)));
            _textureLayout = rf.CreateResourceLayout(new ResourceLayoutDescription(
                                                         new ResourceLayoutElementDescription("Input", ResourceKind.TextureReadOnly, ShaderStages.Fragment)));
            _samplerLayout = rf.CreateResourceLayout(new ResourceLayoutDescription(
                                                         new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            CreateSamplerResourceSets();

            _resourceLayouts = new[] { _wvpLayout, _textureLayout, _samplerLayout };

            _wvpBuffer = rf.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            UpdateWvp();

            _wvpSet = rf.CreateResourceSet(new ResourceSetDescription(_wvpLayout, _wvpBuffer));
        }
Beispiel #4
0
        private static CachedShaderSet CreateAndCacheShaders(this ResourceFactory resourceFactory, SimpleShaderDescription simpleShaderDescription)
        {
            var vertexLayouts = new VertexLayoutDescription(
                new VertexElementDescription(nameof(Vertex.Position), VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                new VertexElementDescription(nameof(Vertex.UV), VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription(nameof(Vertex.VertexGroup), VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1));

            var vertexCode   = VertexCode;
            var fragmentCode = FragmentCode;

            var shaders = resourceFactory.CreateFromSpirv(
                new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(vertexCode), "main"),
                new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(fragmentCode), "main"));

            var shaderSetDescription = new ShaderSetDescription(new[] { vertexLayouts }, shaders);

            var resourceLayout = resourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                          new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                          new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                          new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                          new ResourceLayoutElementDescription("Transform", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                          new ResourceLayoutElementDescription("Nodes", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                          new ResourceLayoutElementDescription("SurfaceTex", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                          new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            var cachedShaderSet = new CachedShaderSet
            {
                ShaderSetDescription = shaderSetDescription,
                ResourceLayout       = resourceLayout,
            };

            _cachedShaderSets[resourceFactory].Add(simpleShaderDescription, cachedShaderSet);
            return(cachedShaderSet);
        }
Beispiel #5
0
        private Pipeline createPipeline(VertexLayoutDescription vertexLayout)
        {
            GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription();

            pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend;

            pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
                depthTestEnabled: true,
                depthWriteEnabled: true,
                comparisonKind: ComparisonKind.LessEqual);

            pipelineDescription.RasterizerState = new RasterizerStateDescription(
                cullMode: FaceCullMode.Back,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);

            pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
            pipelineDescription.ResourceLayouts   = System.Array.Empty <ResourceLayout>();
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: shaders);
            pipelineDescription.Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription;

            return(GraphicsDevice.ResourceFactory.CreateGraphicsPipeline(pipelineDescription));
        }
Beispiel #6
0
 protected RenderGroupState(IPipelineState pso, PrimitiveTopology pt, VertexLayoutDescription vertexLayout)
 {
     PipelineState     = pso;
     PrimitiveTopology = pt;
     VertexLayout      = vertexLayout;
     RenderInfoCache   = new Dictionary <Tuple <GraphicsDevice, ResourceFactory>, RenderInfo>();
 }
        private void CreateShaders()
        {
            var vertexLayout = new VertexLayoutDescription
                               (
                16,
                0,
                new VertexElementDescription[]
            {
                new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position),
                new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
            }
                               );

            var uniformDescriptions = new ResourceLayoutElementDescription[8][];

            uniformDescriptions[0] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("Sampler_Source", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                new ResourceLayoutElementDescription("Texture_Source", ResourceKind.Sampler, ShaderStages.Fragment)
            };

            uniformDescriptions[1] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("Sampler_Noise", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                new ResourceLayoutElementDescription("Texture_Noise", ResourceKind.Sampler, ShaderStages.Fragment)
            };

            uniformDescriptions[2] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("Sampler_ScanlineMask", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                new ResourceLayoutElementDescription("Texture_ScanlineMask", ResourceKind.Sampler, ShaderStages.Fragment)
            };

            uniformDescriptions[3] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("PixellateFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            uniformDescriptions[4] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("EdgeDetectionFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            uniformDescriptions[5] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("StaticFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            uniformDescriptions[6] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("OldMovieFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            uniformDescriptions[7] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("CrtEffectFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "StyleFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions);
        }
        private void CreateShaders()
        {
            var vertexLayout = new VertexLayoutDescription
                               (
                16,
                0,
                new VertexElementDescription[]
            {
                new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position),
                new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
            }
                               );

            var uniformDescriptions = new ResourceLayoutElementDescription[2][];

            uniformDescriptions[0] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("Factors", ResourceKind.UniformBuffer, ShaderStages.Fragment)
            };

            uniformDescriptions[1] = new ResourceLayoutElementDescription[]
            {
                new ResourceLayoutElementDescription("Sampler", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                new ResourceLayoutElementDescription("Texture", ResourceKind.Sampler, ShaderStages.Fragment)
            };

            _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "ColourFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions);
        }
        private void InitPipeline()
        {
            var pipelineDescription = new GraphicsPipelineDescription();

            var vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("TexturePosition", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2));

            var textureLayout = _graphicsDevice.ResourceFactory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("SkeletonTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)));

            _resourceSet = _graphicsDevice.ResourceFactory.CreateResourceSet(new ResourceSetDescription(textureLayout, _skeletonTextureView));

            pipelineDescription.BlendState        = BlendStateDescription.SingleAlphaBlend;
            pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
                depthTestEnabled: true,
                depthWriteEnabled: true,
                comparisonKind: ComparisonKind.LessEqual);
            pipelineDescription.RasterizerState = new RasterizerStateDescription(
                cullMode: FaceCullMode.None,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);
            pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleList;
            pipelineDescription.ResourceLayouts   = new [] { textureLayout };
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: new [] { vertexLayout },
                shaders: _shaders);
            pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription;
            _pipeline = _graphicsDevice.ResourceFactory.CreateGraphicsPipeline(pipelineDescription);
        }
Beispiel #10
0
        public TextureShader(ResourceFactory factory) : base(factory, "Tex")
        {
            Layout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4));

            ProjectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            WorldBuffer      = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            ProjViewLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex))
                );

            TextureLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("SourceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            ResourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                        ProjViewLayout,
                                                        ProjectionBuffer,
                                                        WorldBuffer));
        }
Beispiel #11
0
        private Pipeline CreatePipeline(
            Material material, FrontFace frontFace, PrimitiveTopology primitiveTopology,
            ResourceLayout resourceLayout, VertexLayoutDescription vertexLayout)
        {
            var pipelineDesc = new GraphicsPipelineDescription
            {
                BlendState        = material.BlendState,
                DepthStencilState = material.DepthStencilState,
                RasterizerState   = new RasterizerStateDescription(
                    cullMode: material.CullMode,
                    fillMode: material.FillMode,
                    frontFace: frontFace,
                    depthClipEnabled: true,
                    scissorTestEnabled: false
                    ),
                PrimitiveTopology = primitiveTopology,
                ResourceLayouts   = new ResourceLayout[]
                {
                    resourceLayout
                },
                ShaderSet = new ShaderSetDescription(
                    new VertexLayoutDescription[] { vertexLayout },
                    material.Shaders
                    ),
                Outputs = _framebuffer.OutputDescription
            };

            return(_factory.CreateGraphicsPipeline(pipelineDesc));
        }
        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";

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

            var res = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "imgui").ToTuple();

            _vertexShader   = res.Item1;
            _fragmentShader = res.Item2;

            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, Renderer.GlobalTexturePool.GetLayout() },
                outputDescription,
                ResourceBindingModel.Default);

            _pipeline      = factory.CreateGraphicsPipeline(ref pd);
            _pipeline.Name = "ImGuiPipeline";

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

            RecreateFontDeviceTexture(gd);
        }
Beispiel #13
0
        //TODO: Add color space handling
        override public void CreateResources(SceneRuntimeDescriptor sceneRuntimeDescriptor,
                                             ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>[] modelPNTTBDescriptorArray,
                                             ModelRuntimeDescriptor <VertexPositionNormal>[] modelPNDescriptorArray,
                                             ModelRuntimeDescriptor <VertexPositionTexture>[] modelPTDescriptorArray,
                                             ModelRuntimeDescriptor <VertexPositionColor>[] modelPCDescriptorArray)
        {
            //ResourceFactory factory = GraphicsDevice.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";

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

            byte[] vertexShaderBytes   = LoadEmbeddedShaderCode(GraphicsDevice.ResourceFactory, "imgui-vertex");
            byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(GraphicsDevice.ResourceFactory, "imgui-frag");
            _vertexShader   = _factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, GraphicsDevice.BackendType == GraphicsBackend.Vulkan ? "main" : "VS"));
            _fragmentShader = _factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, GraphicsDevice.BackendType == GraphicsBackend.Vulkan ? "main" : "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, GraphicsDevice.IsClipSpaceYInverted),
                new SpecializationConstant(1, true),
            }),
                new ResourceLayout[] { _layout, _textureLayout },
                _frameBuffer.OutputDescription,
                ResourceBindingModel.Improved);

            _pipeline = _factory.CreateGraphicsPipeline(ref pd);

            _mainResourceSet = _factory.CreateResourceSet(new ResourceSetDescription(_layout,
                                                                                     _projMatrixBuffer,
                                                                                     GraphicsDevice.PointSampler));

            RecreateFontDeviceTexture(GraphicsDevice);
        }
Beispiel #14
0
 public Effect(
     GraphicsDevice graphicsDevice,
     string shaderName,
     VertexLayoutDescription vertexDescriptor,
     bool useNewShaders = false)
     : this(graphicsDevice, shaderName, new[] { vertexDescriptor }, useNewShaders)
 {
 }
        private static void CreateResources()
        {
            ResourceFactory factory = _graphicsDevice.ResourceFactory;

            VertexPositionColor[] quadVertices =
            {
                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)
            };
            BufferDescription vbDescription = new BufferDescription(
                4 * VertexPositionColor.SizeInBytes,
                BufferUsage.VertexBuffer);

            _vertexBuffer = factory.CreateBuffer(vbDescription);
            _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, quadVertices);

            ushort[]          quadIndices   = { 0, 1, 2, 3 };
            BufferDescription ibDescription = new BufferDescription(
                4 * sizeof(ushort),
                BufferUsage.IndexBuffer);

            _indexBuffer = factory.CreateBuffer(ibDescription);
            _graphicsDevice.UpdateBuffer(_indexBuffer, 0, quadIndices);

            VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2),
                new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float4));

            _vertexShader   = LoadShader(ShaderStages.Vertex);
            _fragmentShader = LoadShader(ShaderStages.Fragment);

            // Create pipeline
            GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription();

            pipelineDescription.BlendState        = BlendStateDescription.SingleOverrideBlend;
            pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
                depthTestEnabled: true,
                depthWriteEnabled: true,
                comparisonKind: ComparisonKind.LessEqual);
            pipelineDescription.RasterizerState = new RasterizerStateDescription(
                cullMode: FaceCullMode.Back,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);
            pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
            pipelineDescription.ResourceLayouts   = System.Array.Empty <ResourceLayout>();
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: new Shader[] { _vertexShader, _fragmentShader });
            pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription;

            _pipeline = factory.CreateGraphicsPipeline(pipelineDescription);

            _commandList = factory.CreateCommandList();
        }
Beispiel #16
0
        public SkyboxRenderer(GraphicsDevice device, Framebuffer target, Image <Rgba32> positiveXImage, Image <Rgba32> negativeXImage,
                              Image <Rgba32> positiveYImage, Image <Rgba32> negativeYImage,
                              Image <Rgba32> positiveZImage, Image <Rgba32> negativeZImage)
        {
            _skyboxTexture = new ImageSharpCubemapTexture(positiveXImage, negativeXImage, positiveYImage, negativeYImage, positiveZImage, negativeZImage);

            var factory = device.ResourceFactory;

            var deviceTexture = _skyboxTexture.CreateDeviceTexture(device, factory);

            _skyboxTexture = null;
            TextureView textureView = factory.CreateTextureView(new TextureViewDescription(deviceTexture));

            _vb = factory.CreateBuffer(new BufferDescription(VertexPosition.SizeInBytes * (uint)s_vertices.Length, BufferUsage.VertexBuffer));
            device.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(sizeof(ushort) * (uint)s_indices.Length, BufferUsage.IndexBuffer));
            device.UpdateBuffer(_ib, 0, s_indices);

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                //device.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new DepthStencilStateDescription(false, false, ComparisonKind.LessEqual),
                new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(
                    vertexLayouts,
                    factory.CreateFromSpirv(
                        new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(SkyboxShader.VertexCode), "main"),
                        new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(SkyboxShader.FragmentCode), "main"))),
                new ResourceLayout[] { _layout },
                target.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            ProjectionMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            ViewMatrixBuffer       = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         ProjectionMatrixBuffer,
                                                         ViewMatrixBuffer,
                                                         textureView,
                                                         device.Aniso4xSampler));
        }
Beispiel #17
0
        public MIDIPatternIO(GraphicsDevice gd, ImGuiView view, Func <Vector2> computeSize, MIDIPatternConnect pattern) : base(gd, view, computeSize)
        {
            Buffers = new BufferList <VertexPositionColor> [257]; // first buffer for general purpose, others for keys
            for (int i = 0; i < 257; i++)
            {
                Buffers[i] = dispose.Add(new BufferList <VertexPositionColor>(gd, 6 * 2048 * 16, new[] { 0, 3, 2, 0, 2, 1 }));
            }
            PatternHandler     = pattern;
            CurrentInteraction = new MIDIPatternInteractionIdle(PatternHandler);

            ProjMatrix = Factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            dispose.Add(ProjMatrix);

            VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4));

            Layout = Factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                      new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex)));
            dispose.Add(Layout);

            ShaderDescription vertexShaderDesc = new ShaderDescription(
                ShaderStages.Vertex,
                Encoding.UTF8.GetBytes(VertexCode),
                "main");
            ShaderDescription fragmentShaderDesc = new ShaderDescription(
                ShaderStages.Fragment,
                Encoding.UTF8.GetBytes(FragmentCode),
                "main");

            Shaders = dispose.AddArray(Factory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc));

            var pipelineDescription = new GraphicsPipelineDescription();

            pipelineDescription.BlendState        = BlendStateDescription.SingleAlphaBlend;
            pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
                depthTestEnabled: true,
                depthWriteEnabled: true,
                comparisonKind: ComparisonKind.LessEqual);
            pipelineDescription.RasterizerState = new RasterizerStateDescription(
                cullMode: FaceCullMode.Front,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);
            pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleList;
            pipelineDescription.ResourceLayouts   = new ResourceLayout[] { Layout };
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: Shaders);
            pipelineDescription.Outputs = Canvas.FrameBuffer.OutputDescription;

            Pipeline = Factory.CreateGraphicsPipeline(pipelineDescription);

            MainResourceSet = Factory.CreateResourceSet(new ResourceSetDescription(Layout, ProjMatrix));
        }
Beispiel #18
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneRenderPipeline sp)
        {
            var factory = gd.ResourceFactory;

            WorldBuffer = Renderer.UniformBufferAllocator.Allocate(128, 128);
            WorldBuffer.FillBuffer(cl, ref _World);

            VertexLayoutDescription[] mainVertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("position", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.Float3),
                    new VertexElementDescription("normal", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.SByte4),
                    new VertexElementDescription("color", VertexElementSemantic.TextureCoordinate, Veldrid.VertexElementFormat.Byte4))
            };

            var res = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Collision").ToTuple();

            Shaders = new Shader[] { res.Item1, res.Item2 };

            ResourceLayout projViewLayout = StaticResourceCache.GetResourceLayout(
                gd.ResourceFactory,
                StaticResourceCache.SceneParamLayoutDescription);

            ResourceLayout mainPerObjectLayout = StaticResourceCache.GetResourceLayout(gd.ResourceFactory, new ResourceLayoutDescription(
                                                                                           new ResourceLayoutElementDescription("WorldBuffer", ResourceKind.StructuredBufferReadWrite, ShaderStages.Vertex | ShaderStages.Fragment, ResourceLayoutElementOptions.None)));

            PerObjRS = StaticResourceCache.GetResourceSet(factory, new ResourceSetDescription(mainPerObjectLayout,
                                                                                              Renderer.UniformBufferAllocator._backingBuffer));

            bool isTriStrip = false;
            var  fres       = ColResource.Get();

            if (fres != null)
            {
                var mesh = fres.GPUMeshes[ColMeshIndex];
            }

            GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription();

            pipelineDescription.BlendState        = BlendStateDescription.SingleOverrideBlend;
            pipelineDescription.DepthStencilState = DepthStencilStateDescription.DepthOnlyGreaterEqual;
            pipelineDescription.RasterizerState   = new RasterizerStateDescription(
                cullMode: FaceCullMode.Back,
                fillMode: PolygonFillMode.Solid,
                frontFace: WindClockwise ? FrontFace.Clockwise : FrontFace.CounterClockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);
            pipelineDescription.PrimitiveTopology = isTriStrip ? PrimitiveTopology.TriangleStrip : PrimitiveTopology.TriangleList;
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: mainVertexLayouts,
                shaders: Shaders);
            pipelineDescription.ResourceLayouts = new ResourceLayout[] { projViewLayout, mainPerObjectLayout, Renderer.GlobalTexturePool.GetLayout(), Renderer.GlobalCubeTexturePool.GetLayout(), Renderer.MaterialBufferAllocator.GetLayout(), SamplerSet.SamplersLayout };
            pipelineDescription.Outputs         = gd.SwapchainFramebuffer.OutputDescription;
            RenderPipeline = StaticResourceCache.GetPipeline(factory, ref pipelineDescription);
        }
Beispiel #19
0
    public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription)
    {
        _gd = gd;
        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";
        var fontMr = new FontManager();

        fontMr.SetupFonts();
        RecreateFontDeviceTexture(gd);
        Log.Debug("Fonts OK!");

        _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);
        byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment);
        _vertexShader   = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "VS" : "main"));
        _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "FS" : "main"));

        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, false, true),
            PrimitiveTopology.TriangleList,
            new ShaderSetDescription(vertexLayouts, new[] { _vertexShader, _fragmentShader }),
            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));
    }
 public Model(
     GraphicsDevice gd,
     ResourceFactory factory,
     Stream stream,
     string extension,
     VertexLayoutDescription layout,
     ModelCreateInfo?createInfo,
     PostProcessSteps flags = DefaultPostProcessSteps)
 {
     Init(gd, factory, stream, extension, layout, createInfo, flags);
 }
Beispiel #21
0
        private IList <IDisposable> createTexturedQuadResources()
        {
            Mesh <VertexPositionTexture> quad = GeometryFactory.GenerateQuadPT_XY();

            if (_indexBufferQuad == null)
            {
                throw new ApplicationException("_indexBufferQuad should have been created");
            }

            _vertexBufferTexturedQuad = _factory.CreateBuffer(new BufferDescription(quad.Vertices.LengthUnsigned() * VertexPositionTexture.SizeInBytes, BufferUsage.VertexBuffer));
            GraphicsDevice.UpdateBuffer(_vertexBufferTexturedQuad, 0, quad.Vertices);

            VertexLayoutDescription vertexLayout
                = new VertexLayoutDescription(
                      new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                      new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)
                      );

            _worldTransTexturedQuad = Matrix4x4.CreateWorld(new Vector3(5, 0, 0), -Vector3.UnitZ, Vector3.UnitY);

            _vertexShaderTexturedQuad   = IO.LoadShader("QuadTexture", ShaderStages.Vertex, GraphicsDevice);
            _fragmentShaderTexturedQuad = IO.LoadShader("QuadTexture", ShaderStages.Fragment, GraphicsDevice);

            GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription()
            {
                BlendState        = BlendStateDescription.SingleOverrideBlend,
                DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual,
                RasterizerState   = new RasterizerStateDescription(
                    cullMode: FaceCullMode.Back,
                    fillMode: PolygonFillMode.Solid,
                    frontFace: FrontFace.Clockwise,
                    depthClipEnabled: true,
                    scissorTestEnabled: false
                    ),
                PrimitiveTopology = PrimitiveTopology.TriangleStrip,
                ResourceLayouts   = new ResourceLayout[] { _transformationPipelineResourceLayout, _offscreenLayout },
                ShaderSet         = new ShaderSetDescription(
                    vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                    shaders: new Shader[] { _vertexShaderTexturedQuad, _fragmentShaderTexturedQuad }
                    ),
                Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription
            };

            _pipelineTexturedQuad = _factory.CreateGraphicsPipeline(pipelineDescription);

            return(new List <IDisposable>()
            {
                _textureOffscreenResourceSet,
                _vertexBufferTexturedQuad,
                _vertexShaderTexturedQuad,
                _fragmentShaderTexturedQuad,
                _pipelineTexturedQuad
            });
        }
Beispiel #22
0
        //TODO: Refactor renderFlag preEffectsInstancingFlag and instancingFlag into an class/struct
        public ModelRuntimeDescriptor(Model <T, RealtimeMaterial> modelIn, string vShaderName, string fShaderName, VertexRuntimeTypes vertexRuntimeType, PrimitiveTopology primitiveTopology, RenderDescription renderDescription, InstancingRenderDescription instancingRenderDescription)
        {
            if (!Verifier.VerifyVertexStruct <T>(vertexRuntimeType))
            {
                throw new ArgumentException($"Type Mismatch ModelRuntimeDescriptor");
            }

            Model  = modelIn;
            Length = modelIn.MeshCount;

            TotalInstanceCount = 1;

            _vertexShaderName   = vShaderName;
            _fragmentShaderName = fShaderName;

            VertexRuntimeType = vertexRuntimeType;
            PrimitiveTopology = primitiveTopology;

            RenderDescription        = renderDescription;
            PreEffectsFlag           = RenderFlags.PRE_EFFECTS_MASK & renderDescription.RenderModeFlag;
            PreEffectsInstancingFlag = instancingRenderDescription.PreEffectsFlag;
            InstancingDataFlag       = instancingRenderDescription.RenderModeFlag;

            var preEffectsInstancing = RenderFlags.GetAllPreEffectFor(PreEffectsInstancingFlag);

            VertexBufferList    = new List <DeviceBuffer>();
            IndexBufferList     = new List <DeviceBuffer>();
            Pipelines           = new Pipeline[RenderFlags.EFFCTS_TOTAL_COUNT];
            InstanceBufferLists = new List <DeviceBuffer> [RenderFlags.EFFCTS_TOTAL_COUNT];
            InstanceBufferLists[RenderFlags.NORMAL_ARRAY_INDEX] = new List <DeviceBuffer>();
            foreach (var preEffect in preEffectsInstancing)
            {
                InstanceBufferLists[RenderFlags.GetArrayIndexForFlag(preEffect)] = new List <DeviceBuffer>();
            }
            InstanceBuffers                              = new DeviceBuffer[RenderFlags.EFFCTS_TOTAL_COUNT][];
            TextureResourceSetsList                      = new List <ResourceSet>();
            VertexInstanceLayoutGenerationList           = new EventHandlerList();
            VertexPreEffectsInstanceLayoutGenerationList = new EventHandlerList();

            VertexPreEffectShaders   = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT];
            GeometryPreEffectShaders = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT];
            FragmentPreEffectShaders = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT];

            // Reserve first spot for base vertex geometry
            //TODO: Make on list
            VertexLayouts           = new VertexLayoutDescription[InstancingDataFlags.GetSizeOfPreEffectFlag(InstancingDataFlag) + 1];
            VertexPreEffectsLayouts = new VertexLayoutDescription[RenderFlags.GetSizeOfPreEffectFlag(PreEffectsInstancingFlag) + 1];

            EffectResourceSets = new ResourceSet[RenderFlags.EFFCTS_TOTAL_COUNT][];
            for (int i = 0; i < RenderFlags.EFFCTS_TOTAL_COUNT; i++)
            {
                EffectResourceSets[i] = new ResourceSet[0];
            }
        }
Beispiel #23
0
        public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription)
        {
#if DEBUG
            using Profiler fullProfiler = new Profiler(GetType());
#endif
            graphicsDevice = gd;

            vertexBuffer      = gd.ResourceFactory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic));
            vertexBuffer.Name = "ImGui.NET Vertex Buffer";
            indexBuffer       = gd.ResourceFactory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic));
            indexBuffer.Name  = "ImGui.NET Index Buffer";

            RecreateFontDeviceTexture(gd);

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

            Span <byte> vertexShaderBytes   = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex");
            Span <byte> fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag");
            vertexShader   = gd.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes.ToArray(), "main"));
            fragmentShader = gd.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes.ToArray(), "main"));

            Span <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 = gd.ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                 new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                 new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment)));
            textureLayout = gd.ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                        new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)));

            // TODO: Check pipeline description
            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.ToArray(), new Shader[] { vertexShader, fragmentShader }),
                new ResourceLayout[] { layout, textureLayout },
                outputDescription);
            pipeline = gd.ResourceFactory.CreateGraphicsPipeline(ref pd);

            mainResourceSet = gd.ResourceFactory.CreateResourceSet(new ResourceSetDescription(layout,
                                                                                              projMatrixBuffer,
                                                                                              gd.PointSampler));

            fontTextureResourceSet = gd.ResourceFactory.CreateResourceSet(new ResourceSetDescription(textureLayout, fontTextureView));
        }
        public RendererBase(GraphicsDevice gd, TArgs args)
        {
            _gd      = gd;
            _factory = gd.ResourceFactory;
            Initialize(args);
            _framebuffer = GetFramebuffer();
            _shaders     = GetShaders();

            var vertices = new[]
            {
                new Vector2(-1f, 1f),
                new Vector2(1f, 1f),
                new Vector2(-1f, -1f),
                new Vector2(1f, -1f),
            };

            var indices = new ushort[] { 0, 1, 2, 3 };

            _vertexBuffer = _factory.CreateBuffer(new BufferDescription(4 * sizeof(float) * 2, BufferUsage.VertexBuffer));
            _indexBuffer  = _factory.CreateBuffer(new BufferDescription(4 * sizeof(ushort), BufferUsage.IndexBuffer));

            _gd.UpdateBuffer(_vertexBuffer, 0, vertices);
            _gd.UpdateBuffer(_indexBuffer, 0, indices);

            _resourceLayout = GetResourceLayout();
            _resourceSet    = GetResourceSet(_resourceLayout);

            var vertexElements = new[] { new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2) };

            _vertexLayoutDesc = new VertexLayoutDescription(vertexElements);

            _pipelineDesc = new GraphicsPipelineDescription
            {
                BlendState        = BlendStateDescription.SingleOverrideBlend,
                DepthStencilState = new DepthStencilStateDescription(
                    depthTestEnabled: false,
                    depthWriteEnabled: true,
                    comparisonKind: ComparisonKind.LessEqual),
                RasterizerState = new RasterizerStateDescription(
                    cullMode: FaceCullMode.None,
                    fillMode: PolygonFillMode.Solid,
                    frontFace: FrontFace.Clockwise,
                    depthClipEnabled: true,
                    scissorTestEnabled: false),
                PrimitiveTopology = PrimitiveTopology.TriangleStrip,
                ResourceLayouts   = new[] { _resourceLayout },
                ShaderSet         = new ShaderSetDescription(new VertexLayoutDescription[] { _vertexLayoutDesc }, _shaders),
                Outputs           = _framebuffer.OutputDescription,
            };

            _pipeline = _factory.CreateGraphicsPipeline(_pipelineDesc);
        }
Beispiel #25
0
            public static InputLayoutCacheKey CreatePermanentKey(VertexLayoutDescription[] original)
            {
                VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[original.Length];
                for (int i = 0; i < original.Length; i++)
                {
                    vertexLayouts[i].Stride   = original[i].Stride;
                    vertexLayouts[i].Elements = (VertexElementDescription[])original[i].Elements.Clone();
                }

                return(new InputLayoutCacheKey {
                    VertexLayouts = vertexLayouts
                });
            }
Beispiel #26
0
        public void CreateDeviceObjects(GraphicsDevice gd, OutputDescription outputs)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription((uint)(s_vertices.Length * 12), BufferUsage.VertexBuffer));
            gd.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription((uint)(s_indices.Length * 2), BufferUsage.IndexBuffer));
            gd.UpdateBuffer(_ib, 0, s_indices);

            ImageSharpCubemapTexture imageSharpCubemapTexture = new ImageSharpCubemapTexture(_front, _back, _top, _bottom, _right, _left, true);

            Texture     textureCube = imageSharpCubemapTexture.CreateDeviceTexture(gd, factory);
            TextureView textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            Shader[] shaders = factory.CreateFromSpirv(
                new ShaderDescription(ShaderStages.Vertex, Encoding.ASCII.GetBytes(VertexShader), "main"),
                new ShaderDescription(ShaderStages.Fragment, Encoding.ASCII.GetBytes(FragmentShader), "main"));
            _disposables.Add(shaders[0]);
            _disposables.Add(shaders[1]);

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("UBO", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, shaders),
                new ResourceLayout[] { _layout },
                outputs);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _ubo = factory.CreateBuffer(new BufferDescription(64 * 3, BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         _ubo,
                                                         textureView,
                                                         gd.PointSampler));
        }
Beispiel #27
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            ImageSharpCubemapTexture imageSharpCubemapTexture = new ImageSharpCubemapTexture(_right, _left, _top, _bottom, _back, _front, false);

            Texture     textureCube = imageSharpCubemapTexture.CreateDeviceTexture(gd, factory);
            TextureView textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }, ShaderHelper.GetSpecializations(gd)),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline           = factory.CreateGraphicsPipeline(ref pd);
            pd.Outputs          = sc.ReflectionFramebuffer.OutputDescription;
            _reflectionPipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, textureView, _layout, _pipeline, _reflectionPipeline, _resourceSet, vs, fs);
        }
Beispiel #28
0
        private static void CreateResources()
        {
            ResourceFactory factory = _graphicsDevice.ResourceFactory;

            _vertexBuffer = factory.CreateBuffer(new BufferDescription(4 * VertexPositionColor.SizeInBytes, BufferUsage.VertexBuffer));
            _indexBuffer  = factory.CreateBuffer(new BufferDescription(4 * sizeof(ushort), BufferUsage.IndexBuffer));

            ushort[] quadIndices = { 0, 1, 2, 3 };


            VertexPositionColor[] quadVertices =
            {
                new VertexPositionColor(new Vector3(-.75f,  .75f, 0f), RgbaFloat.Red),
                new VertexPositionColor(new Vector3(.75f,   .75f, 0f), RgbaFloat.Green),
                new VertexPositionColor(new Vector3(-.75f, -.75f, 0f), RgbaFloat.Blue),
                new VertexPositionColor(new Vector3(.75f,  -.75f, 0f), RgbaFloat.Yellow),
            };

            _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, quadVertices);
            _graphicsDevice.UpdateBuffer(_indexBuffer, 0, quadIndices);

            VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4));

            //Create a shaderResource
            _shaderResourceManager.AddShaderResource(
                "MyColorBlock",
                MyColorBlock.SizeInBytes,
                BufferUsage.UniformBuffer | BufferUsage.Dynamic,
                ResourceKind.UniformBuffer,
                ShaderStages.Fragment);

            _shaderResourceManager.InitShaderResourceBuffer("MyColorBlock", new MyColorBlock {
                Color = RgbaFloat.White
            });

            _shaders = new ShadersBuilder(factory)
                       .AddShader(ShaderStages.Vertex, entryPoint: "main", fileName: "vertex")
                       .AddShader(ShaderStages.Fragment, entryPoint: "main", fileName: "fragment")
                       .BuildShaders();

            _pipeline = new PipelineBuilder(_graphicsDevice)
                        .BuildDefault()
                        .AddResourceLayouts(_shaderResourceManager.AllResourceLayouts)
                        .AddShaderSet(new VertexLayoutDescription[] { vertexLayout }, _shaders)
                        .BuildPipeLine(factory);

            _commandList = factory.CreateCommandList();
        }
 public Model(
     GraphicsDevice gd,
     ResourceFactory factory,
     string filename,
     VertexLayoutDescription layout,
     ModelCreateInfo?createInfo,
     PostProcessSteps flags = DefaultPostProcessSteps)
 {
     using (FileStream fs = File.OpenRead(filename))
     {
         string extension = Path.GetExtension(filename);
         Init(gd, factory, fs, extension, layout, createInfo, flags);
     }
 }
Beispiel #30
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            ImageSharpCubemapTexture cubemap = PreloadTexture(sc);
            Texture textureCube = cubemap.CreateDeviceTexture(gd, factory);

            TextureLoaded?.Invoke(sc, cubemap);
            _pendingCubemap = null;

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs, SpecializationConstant[] specs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("CameraInfo", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }, specs),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.CameraInfoBuffer,
                                                         textureCube,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, _layout, _pipeline, _resourceSet, vs, fs);
        }