Esempio n. 1
0
        public void DeviceCreated(GraphicsDevice device)
        {
            _device    = device;
            _resources = _device.ResourceFactory;

            var vertexShaderDescription   = new ShaderDescription(ShaderStages.Vertex, Encoding.ASCII.GetBytes(_vertexShaderSource), "main");
            var fragmentShaderDescription = new ShaderDescription(ShaderStages.Fragment, Encoding.ASCII.GetBytes(_fragmentShaderSource), "main");
            var shaders = _resources.CreateFromSpirv(vertexShaderDescription, fragmentShaderDescription);

            var shaderSetDescription = new ShaderSetDescription(new VertexLayoutDescription[0], shaders);

            _resourceLayout = _resources.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("Uniforms", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

            var sizeOf = (uint)Marshal.SizeOf <ShaderToyUniforms.Uniforms>();

            sizeOf    = ((sizeOf + 15) / 16) * 16;
            _uniforms = _resources.CreateBuffer(new BufferDescription(
                                                    sizeOf, BufferUsage.Dynamic | BufferUsage.UniformBuffer));

            _resourceSet = _resources.CreateResourceSet(new ResourceSetDescription(_resourceLayout, _uniforms));

            _pipeline = _resources.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                              BlendStateDescription.SingleOverrideBlend,
                                                              DepthStencilStateDescription.Disabled,
                                                              RasterizerStateDescription.CullNone,
                                                              PrimitiveTopology.TriangleStrip,
                                                              shaderSetDescription,
                                                              new ResourceLayout[] { _resourceLayout, },
                                                              _device.MainSwapchain.Framebuffer.OutputDescription
                                                              ));

            _cl = _resources.CreateCommandList();
        }
Esempio n. 2
0
 public void Dispose()
 {
     ResourceSet.Dispose();
     ResourceLayout.Dispose();
     Sampler.Dispose();
     Texture.Dispose();
 }
Esempio n. 3
0
        private void CreateDeviceResources()
        {
            ResourceFactory factory = _graphicsDevice.ResourceFactory;

            _commandList = factory.CreateCommandList();
            _transferTex = factory.CreateTexture(
                TextureDescription.Texture2D(Width, Height, 1, 1, PixelFormat.R32_G32_B32_A32_Float, TextureUsage.Sampled | TextureUsage.Storage));
            _texView = factory.CreateTextureView(_transferTex);

            ResourceLayout graphicsLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                             new ResourceLayoutElementDescription("SourceTex", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                             new ResourceLayoutElementDescription("SourceSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            _graphicsSet = factory.CreateResourceSet(new ResourceSetDescription(graphicsLayout, _texView, _graphicsDevice.LinearSampler));

            _graphicsPipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                                   BlendStateDescription.SingleOverrideBlend,
                                                                   DepthStencilStateDescription.Disabled,
                                                                   RasterizerStateDescription.CullNone,
                                                                   PrimitiveTopology.TriangleList,
                                                                   new ShaderSetDescription(
                                                                       Array.Empty <VertexLayoutDescription>(),
                                                                       new[]
            {
                factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, LoadShaderBytes("FramebufferBlitter-vertex"), "VS")),
                factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, LoadShaderBytes("FramebufferBlitter-fragment"), "FS"))
            }),
                                                                   graphicsLayout,
                                                                   _graphicsDevice.MainSwapchain.Framebuffer.OutputDescription));
        }
Esempio n. 4
0
        public static GraphicsPipelineDescription GeneratePipelinePNTTB <T>(
            ModelRuntimeDescriptor <T> modelRuntimeState,
            SceneRuntimeDescriptor sceneRuntimeState,
            RasterizerStateDescription rasterizerState,
            Framebuffer framebuffer,
            ResourceLayout[] effectLayouts) where T : struct, VertexLocateable
        {
            var resourceLayout = new ResourceLayout[] {
                sceneRuntimeState.CameraResourceLayout,
                sceneRuntimeState.LightResourceLayout,
                sceneRuntimeState.SpotLightResourceLayout,
                sceneRuntimeState.MaterialResourceLayout,
                modelRuntimeState.TextureResourceLayout
            };

            var completeResourceLayout = new ResourceLayout[resourceLayout.Length + effectLayouts.Length];

            resourceLayout.CopyTo(completeResourceLayout, 0);
            effectLayouts.CopyTo(completeResourceLayout, resourceLayout.Length);

            return(new GraphicsPipelineDescription()
            {
                BlendState = BlendStateDescription.SingleOverrideBlend,
                DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual,
                RasterizerState = rasterizerState,
                PrimitiveTopology = modelRuntimeState.PrimitiveTopology,
                ResourceLayouts = completeResourceLayout,
                ShaderSet = new ShaderSetDescription(
                    vertexLayouts: modelRuntimeState.VertexLayouts,
                    shaders: new Shader[] { modelRuntimeState.VertexShader, modelRuntimeState.FragmentShader }
                    ),
                Outputs = framebuffer.OutputDescription
            });
        }
Esempio n. 5
0
File: Game.cs Progetto: feliwir/game
        protected void CreateResources()
        {
            var blockTextures = new List <string>();

            foreach (var material in BlockMaterials)
            {
                blockTextures.Add(material.DiffuseTexture);
            }
            BlockDiffuseTextureArray      = CreateBlockTextureArray(blockTextures);
            BlockDiffuseTextureArray.Name = "DiffuseTextures";

            var blockNormalmaps = new List <string>();

            foreach (var material in BlockMaterials)
            {
                blockNormalmaps.Add(material.NormalMap);
            }
            BlockNormalMapArray      = CreateBlockTextureArray(blockNormalmaps);
            BlockNormalMapArray.Name = "NormalTextures";

            _projectionBuffer = Factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            _viewBuffer       = Factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            ResourceLayout projViewLayout = Factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("ProjectionBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("ViewBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            _projViewSet = Factory.CreateResourceSet(new ResourceSetDescription(
                                                         projViewLayout,
                                                         _projectionBuffer,
                                                         _viewBuffer));

            m_cl = Factory.CreateCommandList();
        }
Esempio n. 6
0
        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));
        }
        private void Initialize(BlendStateDescription?blendStateDescription = null)
        {
            commandList = factory.CreateCommandList();

            var indexBufferDescription = new BufferDescription(sizeof(ushort) * MAX_BATCH * 6, BufferUsage.IndexBuffer | BufferUsage.Dynamic);

            indexBuffer = factory.CreateBuffer(indexBufferDescription);

            var vertexBufferDescription = new BufferDescription(VERTEX_SIZE * MAX_BATCH * 4, BufferUsage.VertexBuffer | BufferUsage.Dynamic);

            vertexBuffer = factory.CreateBuffer(vertexBufferDescription);

            var worldMatrixBufferDescription = new BufferDescription(64, BufferUsage.UniformBuffer);

            worldMatrixBuffer = factory.CreateBuffer(worldMatrixBufferDescription);

            viewResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("WorldView", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            graphicsResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                      new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                      new ResourceLayoutElementDescription("TextureSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            var rasterizerStateDescription = new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true);

            var pipelineDescription = new GraphicsPipelineDescription(
                blendStateDescription ?? BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.DepthOnlyGreaterEqual,
                rasterizerStateDescription,
                PrimitiveTopology.TriangleList,
                shaderSet,
                new[] { viewResourceLayout, graphicsResourceLayout },
                framebuffer.OutputDescription);

            pipeline = factory.CreateGraphicsPipeline(pipelineDescription);
        }
Esempio n. 8
0
        public void Dispose_ResourceSet()
        {
            ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription(
                                                                new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                new ResourceLayoutElementDescription("Ortho", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            DeviceBuffer ub0 = RF.CreateBuffer(new BufferDescription(256, BufferUsage.UniformBuffer));
            DeviceBuffer ub1 = RF.CreateBuffer(new BufferDescription(256, BufferUsage.UniformBuffer));

            ResourceSet rs = RF.CreateResourceSet(new ResourceSetDescription(layout, ub0, ub1));

            rs.Dispose();
            Assert.True(rs.IsDisposed);
            Assert.False(ub0.IsDisposed);
            Assert.False(ub1.IsDisposed);
            Assert.False(layout.IsDisposed);
            layout.Dispose();
            Assert.True(layout.IsDisposed);
            Assert.False(ub0.IsDisposed);
            Assert.False(ub1.IsDisposed);
            ub0.Dispose();
            Assert.True(ub0.IsDisposed);
            ub1.Dispose();
            Assert.True(ub1.IsDisposed);
        }
Esempio n. 9
0
        public RoadShaderResources(
            GraphicsDevice graphicsDevice,
            GlobalShaderResources globalShaderResources)
            : base(
                graphicsDevice,
                "Road",
                new GlobalResourceSetIndices(0u, LightingType.Terrain, 1u, 2u, 3u, null),
                RoadVertex.VertexDescriptor)
        {
            _materialResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                        new ResourceLayoutDescription(
                                                            new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                            new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))));

            var resourceLayouts = new[]
            {
                globalShaderResources.GlobalConstantsResourceLayout,
                globalShaderResources.GlobalLightingConstantsResourceLayout,
                globalShaderResources.GlobalCloudResourceLayout,
                globalShaderResources.GlobalShadowResourceLayout,
                _materialResourceLayout
            };

            Pipeline = AddDisposable(graphicsDevice.ResourceFactory.CreateGraphicsPipeline(
                                         new GraphicsPipelineDescription(
                                             BlendStateDescription.SingleAlphaBlend,
                                             DepthStencilStateDescription.DepthOnlyLessEqualRead,
                                             RasterizerStateDescriptionUtility.CullNoneSolid, // TODO
                                             PrimitiveTopology.TriangleList,
                                             ShaderSet.Description,
                                             resourceLayouts,
                                             RenderPipeline.GameOutputDescription)));
        }
Esempio n. 10
0
        public Building(RgbaFloat color) : base("Building")
        {
            Resources.OnInitialize = (factory, device) => {
                _model.Buffer.Initialize(factory, device);
                _color = new UniformColor(color);
                _color.Buffer.Initialize(factory, device);

                ResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                  new ResourceLayoutElementDescription[]
                {
                    UniformModelTransformation.ResourceLayout,
                    UniformViewProjection.ResourceLayout,
                    _color.LayoutDescription
                }
                                                                  ));

                ResourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                            ResourceLayout,
                                                            _model.Buffer.DeviceBuffer,
                                                            CameraViewProjection.DeviceBuffer,
                                                            _color.Buffer.DeviceBuffer
                                                            ));
            };
            Resources.OnDispose = () => {
                _color.Buffer.Dispose();
                ResourceLayout.Dispose();
                ResourceSet.Dispose();
            };
        }
Esempio n. 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));
        }
Esempio n. 12
0
        public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vertexBuffer      = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _indexBuffer       = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _vertexBuffer.Name = "SpriteVertexBuffer";
            _indexBuffer.Name  = "SpriteIndexBuffer";
            cl.UpdateBuffer(_vertexBuffer, 0, Vertices);
            cl.UpdateBuffer(_indexBuffer, 0, Indices);

            if (_pipelines != null)
            {
                foreach (var pipeline in _pipelines)
                {
                    pipeline.Value.Dispose();
                }
            }

            var keys = new[]
            {
                new SpriteShaderKey(true, true),
                new SpriteShaderKey(true, false),
                new SpriteShaderKey(false, true),
                new SpriteShaderKey(false, false)
            };

            _perSpriteResourceLayout = factory.CreateResourceLayout(PerSpriteLayoutDescription);
            _pipelines = keys.ToDictionary(x => x, x => BuildPipeline(gd, sc, x));
            _disposeCollector.Add(_vertexBuffer, _indexBuffer, _perSpriteResourceLayout);
        }
Esempio n. 13
0
        public MeshShaderResources(GraphicsDevice graphicsDevice)
        {
            _graphicsDevice            = graphicsDevice;
            _meshConstantsResourceSets = new Dictionary <MeshConstantsKey, ResourceSet>();

            MeshConstantsResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                            new ResourceLayoutDescription(
                                                                new ResourceLayoutElementDescription("MeshConstants", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment))));

            SkinningResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                       new ResourceLayoutDescription(
                                                           new ResourceLayoutElementDescription("SkinningBuffer", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex))));

            SamplerResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                      new ResourceLayoutDescription(
                                                          new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))));

            SamplerResourceSet = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceSet(
                                                   new ResourceSetDescription(
                                                       SamplerResourceLayout,
                                                       graphicsDevice.Aniso4xSampler)));

            RenderItemConstantsResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                                  new ResourceLayoutDescription(
                                                                      new ResourceLayoutElementDescription("RenderItemConstantsVS", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                      new ResourceLayoutElementDescription("RenderItemConstantsPS", ResourceKind.UniformBuffer, ShaderStages.Fragment))));
        }
Esempio n. 14
0
        private List <IDisposable> createTransformationPipelineUniform()
        {
            _transformationPipelineBuffer = _factory.CreateBuffer(new BufferDescription(192, BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            ResourceLayoutElementDescription resourceLayoutElementDescription = new ResourceLayoutElementDescription("transformPipeline", ResourceKind.UniformBuffer, ShaderStages.Vertex);

            ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription };
            ResourceLayoutDescription          resourceLayoutDescription         = new ResourceLayoutDescription(resourceLayoutElementDescriptions);

            BindableResource[] bindableResources = new BindableResource[] { _transformationPipelineBuffer };

            _transformationPipelineResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription);
            ResourceSetDescription resourceSetDescription = new ResourceSetDescription(_transformationPipelineResourceLayout, bindableResources);

            _transformationPipelineResourceSet = _factory.CreateResourceSet(resourceSetDescription);

            //_graphicsDevice.UpdateBuffer(_transformationPipelineBuffer,0,_camera.ViewMatrix);
            GraphicsDevice.UpdateBuffer(_transformationPipelineBuffer, 64, Camera.ProjectionMatrix);

            return(new List <IDisposable>()
            {
                _transformationPipelineBuffer,
                _transformationPipelineResourceLayout,
                _transformationPipelineResourceSet
            });
        }
Esempio n. 15
0
        public SpriteShaderResources(ShaderSetStore store)
            : base(store, "Sprite", SpriteVertex.VertexDescriptor)
        {
            _pipelines           = new Dictionary <PipelineKey, Pipeline>();
            _samplerResourceSets = new Dictionary <Sampler, ResourceSet>();

            _spriteConstantsResourceLayout = AddDisposable(store.GraphicsDevice.ResourceFactory.CreateResourceLayout(
                                                               new ResourceLayoutDescription(
                                                                   new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                   new ResourceLayoutElementDescription("SpriteConstants", ResourceKind.UniformBuffer, ShaderStages.Fragment))));

            _samplerResourceLayout = AddDisposable(store.GraphicsDevice.ResourceFactory.CreateResourceLayout(
                                                       new ResourceLayoutDescription(
                                                           new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))));

            _textureResourceLayout = AddDisposable(store.GraphicsDevice.ResourceFactory.CreateResourceLayout(
                                                       new ResourceLayoutDescription(
                                                           new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))));

            _alphaMaskResourceLayout = AddDisposable(store.GraphicsDevice.ResourceFactory.CreateResourceLayout(
                                                         new ResourceLayoutDescription(
                                                             new ResourceLayoutElementDescription("AlphaMask", ResourceKind.TextureReadOnly, ShaderStages.Fragment))));

            _resourceLayouts = new[]
            {
                _spriteConstantsResourceLayout,
                _samplerResourceLayout,
                _textureResourceLayout,
                _alphaMaskResourceLayout,
            };
        }
Esempio n. 16
0
        public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope)
        {
            var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector);

            BonesBuffer = factory.CreateBuffer(new BufferDescription((uint)(Marshal.SizeOf <Matrix4x4>() * MDLConstants.MaxBones), BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            RenderArgumentsBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioRenderArguments>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            TextureDataBuffer = factory.CreateBuffer(new BufferDescription((uint)Marshal.SizeOf <StudioTextureData>(), BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            _sharedLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                             new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                             new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                             new ResourceLayoutElementDescription("WorldAndInverse", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                             new ResourceLayoutElementDescription("Bones", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                             new ResourceLayoutElementDescription("RenderArguments", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment),
                                                             new ResourceLayoutElementDescription("TextureData", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                             new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment),
                                                             new ResourceLayoutElementDescription("LightingInfo", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

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

            var vertexLayouts = new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                    new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3),
                    new VertexElementDescription("VertexBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1),
                    new VertexElementDescription("NormalBoneIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1))
            };

            for (var cullMode = CullBack; cullMode < CullModeCount; ++cullMode)
            {
                for (var maskMode = MaskDisabled; maskMode < MaskModeCount; ++maskMode)
                {
                    for (var additiveMode = AdditiveDisabled; additiveMode < AdditiveModeCount; ++additiveMode)
                    {
                        _pipelines[cullMode, maskMode, additiveMode] = CreatePipelines(
                            gd, sc, vertexLayouts, _sharedLayout, TextureLayout, factory,
                            cullMode == CullBack,
                            maskMode == MaskEnabled,
                            additiveMode == AdditiveEnabled);
                    }
                }
            }

            SharedResourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                              _sharedLayout,
                                                              sc.ProjectionMatrixBuffer,
                                                              sc.ViewMatrixBuffer,
                                                              sc.WorldAndInverseBuffer,
                                                              BonesBuffer,
                                                              RenderArgumentsBuffer,
                                                              TextureDataBuffer,
                                                              sc.MainSampler,
                                                              sc.LightingInfoBuffer
                                                              ));
        }
Esempio n. 17
0
        public void CreateDeviceResources(GraphicsDevice gd, ResourceFactory factory)
        {
            _projectionBuffer = factory.CreateBuffer(new BufferDescription(224, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            var DiffuseIntensity  = 0.65f;
            var SpecularIntensity = 0.25f;
            var AmbientIntensity  = 0.10f;
            var Shininess         = 12;
            var lightModel        = new Vector4(
                DiffuseIntensity,
                SpecularIntensity,
                AmbientIntensity,
                Shininess);

            _ubo.DiffuseSpecularAmbientShininess = lightModel;
            _ubo.GlobeOneOverRadiiSquared        = Shape.OneOverRadiiSquared;
            //_ubo.UseAverageDepth = false;
            //提前更新参数
            gd.UpdateBuffer(_projectionBuffer, 0, _ubo);


            ResourceLayout projViewLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment | ShaderStages.Geometry)
                    ));

            _projViewSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         projViewLayout,
                                                         _projectionBuffer
                                                         ));

            //保存一些公共资源
            ShareResource.ProjectionBuffer         = _projectionBuffer;
            ShareResource.ProjectionResourceLayout = projViewLayout;
            ShareResource.ProjectionResourceSet    = _projViewSet;
        }
Esempio n. 18
0
        public override ResourceLayout CreateResourceLayout(ref ResourceLayoutDescription description)
        {
            ResourceLayout layout = Factory.CreateResourceLayout(ref description);

            DisposeCollector.Add(layout);
            return(layout);
        }
Esempio n. 19
0
 public RenderUnit(ResourceFactory factory, Description d)
 {
     Layout        = factory.CreateResourceLayout(d.LayoutDescription);
     Resources     = d.ResourceElements.ToArray();
     ResourceNames = d.ResourceNames;
     Update(factory);
 }
Esempio n. 20
0
        public SpriteShaderResources(GraphicsDevice graphicsDevice)
            : base(
                graphicsDevice,
                "Sprite",
                new GlobalResourceSetIndices(null, LightingType.None, null, null, null, null),
                SpriteVertex.VertexDescriptor)
        {
            _pipelines           = new Dictionary <PipelineKey, Pipeline>();
            _samplerResourceSets = new Dictionary <Sampler, ResourceSet>();

            _spriteConstantsResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                               new ResourceLayoutDescription(
                                                                   new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                   new ResourceLayoutElementDescription("SpriteConstants", ResourceKind.UniformBuffer, ShaderStages.Fragment))));

            _samplerResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                       new ResourceLayoutDescription(
                                                           new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))));

            _textureResourceLayout = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceLayout(
                                                       new ResourceLayoutDescription(
                                                           new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))));

            _resourceLayouts = new[]
            {
                _spriteConstantsResourceLayout,
                _samplerResourceLayout,
                _textureResourceLayout
            };
        }
Esempio n. 21
0
        private List <IDisposable> createOffscreenFBO()
        {
            Texture offscreenTexture = _factory.CreateTexture(TextureDescription.Texture2D(
                                                                  RenderResoultion.Horizontal.ToUnsigned(),
                                                                  RenderResoultion.Vertical.ToUnsigned(), 1, 1,
                                                                  PixelFormat.B8_G8_R8_A8_UNorm,
                                                                  TextureUsage.RenderTarget | TextureUsage.Sampled));
            Texture offscreenDepth = _factory.CreateTexture(TextureDescription.Texture2D(
                                                                RenderResoultion.Horizontal.ToUnsigned(),
                                                                RenderResoultion.Vertical.ToUnsigned(),
                                                                1, 1,
                                                                PixelFormat.R16_UNorm, TextureUsage.DepthStencil));

            _offscreenTextureView = _factory.CreateTextureView(offscreenTexture);
            _offScreenFBO         = _factory.CreateFramebuffer(new FramebufferDescription(offscreenDepth, offscreenTexture));

            _offscreenLayout = _factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                 new ResourceLayoutElementDescription("colorTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                 new ResourceLayoutElementDescription("colorSampler", ResourceKind.Sampler, ShaderStages.Fragment)
                                                                 ));

            _textureOffscreenResourceSet = _factory.CreateResourceSet(new ResourceSetDescription(_offscreenLayout, _offscreenTextureView, GraphicsDevice.LinearSampler));

            return(new List <IDisposable>()
            {
                _offscreenTextureView,
                _offScreenFBO,
                _offscreenLayout,
                _textureOffscreenResourceSet
            });
        }
        public void ResourceSet_TooFewOrTooManyElements_Fails()
        {
            ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription(
                                                                new ResourceLayoutElementDescription("UB0", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                new ResourceLayoutElementDescription("UB1", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                new ResourceLayoutElementDescription("UB2", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            DeviceBuffer ub = RF.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            Assert.Throws <VeldridException>(() =>
            {
                RF.CreateResourceSet(new ResourceSetDescription(layout, ub));
            });

            Assert.Throws <VeldridException>(() =>
            {
                RF.CreateResourceSet(new ResourceSetDescription(layout, ub, ub));
            });

            Assert.Throws <VeldridException>(() =>
            {
                RF.CreateResourceSet(new ResourceSetDescription(layout, ub, ub, ub, ub));
            });

            Assert.Throws <VeldridException>(() =>
            {
                RF.CreateResourceSet(new ResourceSetDescription(layout, ub, ub, ub, ub, ub));
            });
        }
Esempio n. 23
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));
        }
Esempio n. 24
0
        public void CreateDeviceObjects(IRendererContext context)
        {
            var c  = (VeldridRendererContext)context;
            var cl = c.CommandList;
            var gd = c.GraphicsDevice;
            var sc = c.SceneContext;

            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _ib = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _miscUniformBuffer      = factory.CreateBuffer(new BufferDescription(MiscUniformData.SizeInBytes, BufferUsage.UniformBuffer));
            _vb.Name                = "TileMapVertexBuffer";
            _ib.Name                = "TileMapIndexBuffer";
            _miscUniformBuffer.Name = "TileMapMiscBuffer";
            cl.UpdateBuffer(_vb, 0, Vertices);
            cl.UpdateBuffer(_ib, 0, Indices);

            var shaderCache = Resolve <IShaderCache>();

            _shaders = shaderCache.GetShaderPair(gd.ResourceFactory,
                                                 VertexShaderName,
                                                 FragmentShaderName,
                                                 shaderCache.GetGlsl(VertexShaderName),
                                                 shaderCache.GetGlsl(FragmentShaderName));

            var shaderSet = new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, _shaders);

            _layout         = factory.CreateResourceLayout(PerSpriteLayoutDescription);
            _textureSampler = gd.ResourceFactory.CreateSampler(new SamplerDescription(
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerFilter.MinPoint_MagPoint_MipPoint,
                                                                   null, 1, 0, 0, 0, SamplerBorderColor.TransparentBlack
                                                                   ));

            var depthStencilMode = gd.IsDepthRangeZeroToOne
                    ? DepthStencilStateDescription.DepthOnlyLessEqual
                    : DepthStencilStateDescription.DepthOnlyGreaterEqual;

            var rasterizerMode = new RasterizerStateDescription(
                FaceCullMode.Front, PolygonFillMode.Solid, FrontFace.Clockwise,
                true, true);

            var pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                depthStencilMode,
                rasterizerMode,
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(new[] { VertexLayout, InstanceLayout },
                                         shaderSet.Shaders,
                                         ShaderHelper.GetSpecializations(gd)),
                new[] { _layout, sc.CommonResourceLayout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline      = factory.CreateGraphicsPipeline(ref pd);
            _pipeline.Name = "P_TileMapRenderer";
            _disposeCollector.Add(_vb, _ib, _layout, _textureSampler, _pipeline);
        }
Esempio n. 25
0
        public void CreateDeviceResources(GraphicsDevice gd, ResourceFactory factory)
        {
            _gd = gd;
            ///至多100个点输入,至多102个indices输入
            _lineVertexBuffer = factory.CreateBuffer(new BufferDescription((uint)(6 * 100), BufferUsage.VertexBuffer));
            //gd.UpdateBuffer(_vertexBuffer, 0, this.Positions);
            _lineIndicesBuffer = factory.CreateBuffer(new BufferDescription((uint)(sizeof(ushort) * 102), BufferUsage.IndexBuffer));
            //gd.UpdateBuffer(_indexBuffer, 0, this.Indices);
            _polylinetyleBuffer = factory.CreateBuffer(new BufferDescription(32, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            gd.UpdateBuffer(_polylinetyleBuffer, 0, new LineVectorStyleUBO(RgbaFloat.Red));


            ResourceLayout styleLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("LineStyle", ResourceKind.UniformBuffer, ShaderStages.Fragment | ShaderStages.Geometry)
                    ));


            var curAss = this.GetType().Assembly;


            //这里position的定义极有可能是vec3,因此传入vec2可能出现问题,具体可以参考vk里的源码
            ShaderSetDescription shaderSetBoundingBox = new ShaderSetDescription(
                new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3))
            },
                new[]
            {
                ResourceHelper.LoadEmbbedShader(ShaderStages.Vertex, "DrawLineVS.spv", gd, curAss),
                ResourceHelper.LoadEmbbedShader(ShaderStages.Fragment, "DrawLineFS.spv", gd, curAss),
                ResourceHelper.LoadEmbbedShader(ShaderStages.Geometry, "DrawLineGS.spv", gd, curAss)
            });


            var rasterizer = RasterizerStateDescription.Default;

            rasterizer.FillMode  = PolygonFillMode.Wireframe;
            rasterizer.FrontFace = FrontFace.CounterClockwise;
            //gpu的lineWidth实际绘制的效果并不好仍然需要GeometryShader来实现更好的效果
            //rasterizer.LineWidth = 8.0f;
            _pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                           BlendStateDescription.SingleOverrideBlend,
                                                           DepthStencilStateDescription.DepthOnlyLessEqual,
                                                           rasterizer,
                                                           PrimitiveTopology.LinesAdjacency,
                                                           shaderSetBoundingBox,
                                                           //共享View和prj的buffer
                                                           new ResourceLayout[] { ShareResource.ProjectionResourceLayout, styleLayout },
                                                           gd.MainSwapchain.Framebuffer.OutputDescription));


            //创建一个StyleresourceSet,0是线样式1是面样式
            _styleResourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                              styleLayout,
                                                              _polylinetyleBuffer
                                                              ));
        }
Esempio n. 26
0
        protected unsafe override void CreateResources(ResourceFactory factory)
        {
            _projectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            _viewBuffer       = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            _worldBuffer      = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            _vertexBuffer = factory.CreateBuffer(new BufferDescription((uint)(VertexPositionTexture.SizeInBytes * _vertices.Length), BufferUsage.VertexBuffer));
            GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, _vertices);

            _indexBuffer = factory.CreateBuffer(new BufferDescription(sizeof(ushort) * (uint)_indices.Length, BufferUsage.IndexBuffer));
            GraphicsDevice.UpdateBuffer(_indexBuffer, 0, _indices);

            _surfaceTexture     = _stoneTexData.CreateDeviceTexture(GraphicsDevice, ResourceFactory, TextureUsage.Sampled);
            _surfaceTextureView = factory.CreateTextureView(_surfaceTexture);

            ShaderSetDescription shaderSet = new ShaderSetDescription(
                new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2))
            },
                factory.CreateFromSpirv(
                    new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(VertexCode), "main"),
                    new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(FragmentCode), "main")));

            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.DepthOnlyLessEqual,
                                                           RasterizerStateDescription.Default,
                                                           PrimitiveTopology.TriangleList,
                                                           shaderSet,
                                                           new[] { projViewLayout, worldTextureLayout },
                                                           MainSwapchain.Framebuffer.OutputDescription));

            _projViewSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         projViewLayout,
                                                         _projectionBuffer,
                                                         _viewBuffer));

            _worldTextureSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                             worldTextureLayout,
                                                             _worldBuffer,
                                                             _surfaceTextureView,
                                                             GraphicsDevice.Aniso4xSampler));

            _cl = factory.CreateCommandList();
        }
Esempio n. 27
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";

            _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);
        }
Esempio n. 28
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);
        }
Esempio n. 29
0
 public void Update(ResourceLayout TexLayout, Image Img)
 {
     using (Bitmap Bmp = new Bitmap(Img)) {
         BitmapData BmpData = Bmp.LockBits(new DRect(0, 0, Img.Width, Img.Height), ImageLockMode.ReadOnly, PixFormat.Format32bppArgb);
         Update(TexLayout, Img.Width, Img.Height, BmpData.Scan0, Bmp.Width * Bmp.Height * 4);
         Bmp.UnlockBits(BmpData);
     }
 }
Esempio n. 30
0
        public BasicMaterial(DrawingContext context, Texture2D texture, bool twoSided = false)
            : base(context)
        {
            // NOTE: Quick solution to draw without culling
            this.TwoSided = twoSided;

            _projectionBuffer = context.ResourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            _viewBuffer       = context.ResourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            _worldBuffer      = context.ResourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            _surfaceTexture     = texture.CreateDeviceTexture(context.GraphicsDevice, context.ResourceFactory, TextureUsage.Sampled);
            _surfaceTextureView = context.ResourceFactory.CreateTextureView(_surfaceTexture);

            ShaderSetDescription shaderSet = new ShaderSetDescription(
                new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("Normal", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                    new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4)
                    )
            },
                context.ResourceFactory.CreateFromSpirv(
                    new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(VertexCode), "main"),
                    new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(FragmentCode), "main")));

            ResourceLayout projViewLayout = context.ResourceFactory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            ResourceLayout worldTextureLayout = context.ResourceFactory.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 = context.ResourceFactory.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                                           BlendStateDescription.SingleOverrideBlend,
                                                                           DepthStencilStateDescription.DepthOnlyLessEqual,
                                                                           twoSided ? RasterizerStateDescription.CullNone : RasterizerStateDescription.Default,
                                                                           PrimitiveTopology.TriangleList,
                                                                           shaderSet,
                                                                           new[] { projViewLayout, worldTextureLayout },
                                                                           context.MainSwapchain.Framebuffer.OutputDescription));

            _projViewSet = context.ResourceFactory.CreateResourceSet(new ResourceSetDescription(
                                                                         projViewLayout,
                                                                         _projectionBuffer,
                                                                         _viewBuffer));

            _worldTextureSet = context.ResourceFactory.CreateResourceSet(new ResourceSetDescription(
                                                                             worldTextureLayout,
                                                                             _worldBuffer,
                                                                             _surfaceTextureView,
                                                                             context.GraphicsDevice.Aniso4xSampler));
        }