Example #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();
         }
     }
 }
Example #2
0
            public CodeBlock(CsCodeWriter cw, string header, string trailing)
            {
                _cw       = cw;
                _trailing = trailing;
                if (header != null)
                {
                    _cw.WriteLine(header);
                }

                _cw.WriteLine("{");
                _cw.IncreaseIndentation();
            }
Example #3
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);
                }
            }
        }
Example #4
0
 public void Dispose()
 {
     _cw.DecreaseIndentation();
     _cw.Write("}");
     if (_trailing != null)
     {
         _cw.Write(_trailing);
     }
     _cw.WriteLine();
 }
Example #5
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}),");
                    }
                }
            }
        }
Example #6
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());
 }");
     }
Example #7
0
 public void Dispose()
 {
     _cw.WriteLine($"#endif // {_condition}");
 }
Example #8
0
 public IfDefSection(CsCodeWriter cw, string condition)
 {
     _cw        = cw;
     _condition = condition;
     _cw.WriteLine($"#if {condition}");
 }