Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static CachedShaderSet GetShaderSet(this ResourceFactory resourceFactory, SimpleShaderDescription simpleShaderDescription)
        {
            if (!_cachedShaderSets.TryGetValue(resourceFactory, out var shaderDictionary))
            {
                shaderDictionary = new Dictionary <SimpleShaderDescription, CachedShaderSet>();
                _cachedShaderSets.Add(resourceFactory, shaderDictionary);
            }

            return(shaderDictionary.TryGetValue(simpleShaderDescription, out var value) ? value : resourceFactory.CreateAndCacheShaders(simpleShaderDescription));
        }