Ejemplo n.º 1
0
 private void EmitVertexInputsGetter(ShaderSetProcessorInput input, CsCodeWriter ccw)
 {
     using (ccw.PushBlock($"public static VertexInputDescription[] GetVertexInputs()"))
     {
         ccw.WriteLine($"return new VertexInputDescription[]");
         using (ccw.PushBlock(null, ";"))
         {
             ccw.WriteLine("new VertexInputDescription(");
             ccw.IncreaseIndentation();
             ParameterDefinition[] vsParams = input.VertexFunction.Parameters;
             foreach (ParameterDefinition param in vsParams)
             {
                 StructureDefinition sd = input.Model.GetStructureDefinition(param.Type);
                 foreach (FieldDefinition fd in sd.Fields)
                 {
                     string              name     = fd.Name;
                     VertexSemanticType  semantic = GetSemantic(fd.SemanticType);
                     VertexElementFormat format   = GetFormat(fd.Type);
                     ccw.Write($"new VertexInputElement(\"{name}\", VertexSemanticType.{semantic}, VertexElementFormat.{format})");
                     if (fd == sd.Fields.Last())
                     {
                         ccw.WriteLine(")");
                     }
                     else
                     {
                         ccw.WriteLine(",");
                     }
                 }
             }
             ccw.DecreaseIndentation();
         }
     }
 }
Ejemplo n.º 2
0
        public void ProcessShaderSet(ShaderSetProcessorInput input)
        {
            if (input.VertexFunction == null || input.FragmentFunction == null)
            {
                throw new InvalidOperationException("Veldrid.ShaderGen failed -- incomplete shader set.");
            }

            string outputPath = Path.Combine(UserArgs, input.SetName + ".SetInfo.cs");

            using (StreamWriter fs = File.CreateText(outputPath))
            {
                CsCodeWriter ccw = new CsCodeWriter(fs);

                ccw.WriteLine("// This file is generated.");
                ccw.Using("Veldrid");
                ccw.Using("Veldrid.Graphics");

                using (ccw.PushBlock($"public static class {input.SetName}SetInfo"))
                {
                    EmitVertexInputsGetter(input, ccw);
                    EmitResourceDescsGetter(input, ccw);
                    EmitCreateAll(input, ccw);
                }
            }
        }
Ejemplo n.º 3
0
     private void EmitCreateAll(ShaderSetProcessorInput input, CsCodeWriter ccw)
     {
         ccw.WriteLine(
             @"public static void CreateAll(
     ResourceFactory factory,
     CompiledShaderCode vsCode,
     CompiledShaderCode fsCode,
     out ShaderSet shaderSet,
     out ShaderResourceBindingSlots resourceSlots)
 {
     Shader vs = factory.CreateShader(ShaderStages.Vertex, vsCode);
     Shader fs = factory.CreateShader(ShaderStages.Fragment, fsCode);
     VertexInputLayout layout = factory.CreateInputLayout(GetVertexInputs());
     shaderSet = factory.CreateShaderSet(layout, vs, fs);
     resourceSlots = factory.CreateShaderResourceBindingSlots(shaderSet, GetResources());
 }");
     }
Ejemplo n.º 4
0
        private void EmitResourceDescsGetter(ShaderSetProcessorInput input, CsCodeWriter ccw)
        {
            using (ccw.PushBlock("public static ShaderResourceDescription[] GetResources()"))
            {
                ccw.WriteLine("return new ShaderResourceDescription[]");
                using (ccw.PushBlock(null, ";"))
                {
                    foreach (ResourceDefinition rd in input.Model.Resources)
                    {
                        string name        = rd.Name;
                        string secondParam =
                            rd.ResourceKind == ShaderResourceKind.Texture2D || rd.ResourceKind == ShaderResourceKind.TextureCube
                            ? "ShaderResourceType.Texture"
                            : rd.ResourceKind == ShaderResourceKind.Sampler
                                ? "ShaderResourceType.Sampler"
                                : GetConstantSecondParam(rd.ValueType, input.Model);

                        ccw.WriteLine($"new ShaderResourceDescription(\"{name}\", {secondParam}),");
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void ProcessShaderSet(ShaderSetProcessorInput input)
        {
            var shaderDefinition = new ShaderDefinition();

            foreach (var resource in input.Model.AllResources)
            {
                try
                {
                    var resourceBinding = CreateResourceBinding(input.Model, resource);
                    shaderDefinition.ResourceBindings.Add(resourceBinding);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Could not create resource binding for set {input.SetName}, resource {resource.Name}, value type {resource.ValueType.Name}", ex);
                }
            }

            var path = Path.Combine(UserArgs, input.SetName + ".json");
            var json = shaderDefinition.ToJson();

            File.WriteAllText(path, json);
        }
Ejemplo n.º 6
0
 public void ProcessShaderSet(ShaderSetProcessorInput input)
 {
     Result = string.Join(" ", input.Model.AllResources.Select(rd => rd.Name));
 }