This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Finished implementation of techniques. Quite a lot of refactoring in …

…order to keep the code under control. Had to change most of the code generation. Added some examples of the new .stitchedeffect format(s) in the demo project. Closes #1.
  • Loading branch information...
tgjones committed Nov 27, 2010
1 parent 0b294bb commit 7351c20496c6db2007ac42b178212ac9d83d0b7e
Showing with 1,006 additions and 444 deletions.
  1. +0 −366 src/StitchUp.Content.Pipeline/FragmentLinking/CodeGeneration/EffectCodeGenerator.cs
  2. +0 −13 src/StitchUp.Content.Pipeline/FragmentLinking/CodeGeneration/StringBuilderExtensionMethods.cs
  3. +1 −1 src/StitchUp.Content.Pipeline/FragmentLinking/CodeModel/TechniquePassNode.cs
  4. +6 −0 src/StitchUp.Content.Pipeline/FragmentLinking/CodeModel/TokenType.cs
  5. +35 −3 src/StitchUp.Content.Pipeline/FragmentLinking/Compiler/StitchedEffectBuilder.cs
  6. +9 −1 src/StitchUp.Content.Pipeline/FragmentLinking/EffectModel/TechniquePassSymbol.cs
  7. +12 −1 src/StitchUp.Content.Pipeline/FragmentLinking/EffectModel/TechniqueSymbol.cs
  8. +83 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/EffectCodeGenerator.cs
  9. +16 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/ExportedValueGenerator.cs
  10. +9 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/GeneratorContext.cs
  11. +32 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/HeaderCodeGenerator.cs
  12. +23 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/ParameterGenerator.cs
  13. +28 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/SamplerGenerator.cs
  14. +45 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/ScriptTextWriter.cs
  15. +1 −1 src/StitchUp.Content.Pipeline/FragmentLinking/{CodeGeneration → Generator}/SemanticGenerator.cs
  16. +169 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/ShaderGenerator.cs
  17. +162 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/StructGenerator.cs
  18. +41 −0 src/StitchUp.Content.Pipeline/FragmentLinking/Generator/TechniqueGenerator.cs
  19. +1 −1 src/StitchUp.Content.Pipeline/FragmentLinking/Parser/Parser.cs
  20. +84 −1 src/StitchUp.Content.Pipeline/FragmentLinking/Parser/StitchedEffectParser.cs
  21. +9 −0 src/StitchUp.Content.Pipeline/FragmentLinking/PreProcessor/ExportDictionary.cs
  22. +8 −0 src/StitchUp.Content.Pipeline/FragmentLinking/PreProcessor/ExportedValue.cs
  23. +22 −18 src/StitchUp.Content.Pipeline/FragmentLinking/PreProcessor/StitchedEffectPreProcessor.cs
  24. +18 −8 src/StitchUp.Content.Pipeline/ParsingImporter.cs
  25. +5 −9 src/StitchUp.Content.Pipeline/Processors/StitchedEffectProcessor.cs
  26. +9 −0 src/StitchUp.Content.Pipeline/Properties/Resources.Designer.cs
  27. +3 −0 src/StitchUp.Content.Pipeline/Properties/Resources.resx
  28. +13 −3 src/StitchUp.Content.Pipeline/StitchUp.Content.Pipeline.csproj
  29. +52 −0 src/StitchUp.Content.Pipeline/StitchedEffectImporter.cs
  30. +4 −0 src/StitchUp.Demo/StitchUp.Demo/StitchUp.Demo.csproj.Debug.cachefile
  31. +9 −1 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/BasicEffect.stitchedeffect
  32. +18 −0 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/BasicEffect2.stitchedeffect
  33. +17 −0 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/BasicEffect3.stitchedeffect
  34. +6 −0 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/BasicEffect4.stitchedeffect
  35. +19 −17 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/Fragments/LightingModels/Phong.fragment
  36. +18 −0 src/StitchUp.Demo/StitchUp.DemoContent/Shaders/Fragments/PixelColorOutputTest.fragment
  37. +19 −0 src/StitchUp.Demo/StitchUp.DemoContent/StitchUp.DemoContent.contentproj
@@ -1,366 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Text.RegularExpressions;
-using StitchUp.Content.Pipeline.FragmentLinking.CodeModel;
-using StitchUp.Content.Pipeline.FragmentLinking.EffectModel;
-
-namespace StitchUp.Content.Pipeline.FragmentLinking.CodeGeneration
-{
- public class EffectCodeGenerator
- {
- private readonly StitchedEffectSymbol _stitchedEffect;
- private readonly ShaderProfile _targetShaderProfile;
- private readonly StringBuilder _output;
-
- public EffectCodeGenerator(StitchedEffectSymbol stitchedEffect, ShaderProfile targetShaderProfile)
- {
- _stitchedEffect = stitchedEffect;
- _targetShaderProfile = targetShaderProfile;
- _output = new StringBuilder();
- }
-
- public string GenerateCode()
- {
- WriteAllHeaderCode();
- WriteAllParams();
- WriteAllSamplers();
- WriteAllVertexInputStructures();
- WriteAllPixelInputStructures();
- WriteAllVertexOutputStructures();
- WriteAllPixelOutputStructures();
- WriteAllVertexShaders();
- WriteAllPixelShaders();
- WriteTechnique();
-
- return _output.ToString();
- }
-
- private void WriteAllPixelOutputStructures()
- {
- _output.AppendLine("// -------- pixel output structures --------");
-
- SemanticGenerator semanticGenerator = new SemanticGenerator("COLOR", 1);
- ForEachFragment(f => WritePixelOutputStructure(f, semanticGenerator));
-
- _output.AppendLine("struct PIXELOUTPUT");
- _output.AppendLine("{");
- _output.AppendLineFormat("\tfloat4 color : COLOR0;");
- ForEachFragment(f =>
- {
- if (f.FragmentNode.PixelOutputs != null && f.FragmentNode.PixelOutputs.VariableDeclarations.Any())
- _output.AppendLineFormat("\t{0}_PIXELOUTPUT {0};", f.UniqueName);
- });
- _output.AppendLine("};");
- _output.AppendLine();
- }
-
- private void WritePixelOutputStructure(StitchedFragmentSymbol stitchedFragment, SemanticGenerator semanticGenerator)
- {
- WriteShaderInputStructure(stitchedFragment, semanticGenerator, "PIXELOUTPUT", stitchedFragment.FragmentNode.PixelOutputs, true);
- }
-
- private void WriteAllVertexShaders()
- {
- ForEachFragment(WriteVertexShader);
-
- _output.AppendLine("// -------- vertex shader entrypoint --------");
- _output.AppendLine("VERTEXOUTPUT vs(const VERTEXINPUT i)");
- _output.AppendLine("{");
- _output.AppendLine("\tgVertexInput = i;");
- _output.AppendLine();
- _output.AppendLine("\tVERTEXOUTPUT output = (VERTEXOUTPUT) 0;");
- _output.AppendLine();
-
- ForEachFragment(f => _output.AppendLineFormat("\t{0}_vs(gVertexInput.{0}, output);", f.UniqueName));
-
- _output.AppendLine();
- _output.AppendLine("\treturn output;");
- _output.AppendLine("}");
- _output.AppendLine();
- }
-
- private void WriteVertexShader(StitchedFragmentSymbol stitchedFragment)
- {
- _output.AppendLineFormat("// -------- vertex shader {0} --------", stitchedFragment.UniqueName);
-
- ShaderCodeBlockNode shader = stitchedFragment.FragmentNode.VertexShaders.GetCodeBlock(_targetShaderProfile);
-
- if (shader != null)
- {
- string shaderCode = ReplaceOutputCalls(shader.Code, stitchedFragment.UniqueName);
- WriteShaderCode(stitchedFragment, shaderCode, "VERTEXINPUT", "VERTEXOUTPUT", "vs");
- }
- else
- {
- // Need to auto-generate vertex shader. Simply pass through all vertex inputs.
- _output.AppendLineFormat("void {0}_vs({0}_VERTEXINPUT input, inout VERTEXOUTPUT output)", stitchedFragment.UniqueName);
- _output.AppendLine("{");
- _output.Append(GetVertexPassThroughCode(stitchedFragment));
- _output.AppendLine("}");
- }
-
- _output.AppendLine();
- _output.AppendLine();
- }
-
- private static string ReplaceOutputCalls(string shaderCode, string uniqueName)
- {
- // Replace "output(*variable*);" with code to pass input value (i.e. vertex attribute)
- // with matching name through to output value (i.e. interpolator).
- return Regex.Replace(shaderCode, @"output\((?<NAME>[\w]+), (?<VALUE>[\s\S]+?)\);",
- string.Format("output.{0}.${{NAME}} = ${{VALUE}};", uniqueName));
- }
-
- private static string GetVertexPassThroughCode(StitchedFragmentSymbol stitchedFragment)
- {
- StringBuilder sb = new StringBuilder();
- if (stitchedFragment.FragmentNode.VertexAttributes != null)
- foreach (var variable in stitchedFragment.FragmentNode.VertexAttributes.VariableDeclarations)
- sb.AppendLine(GetVertexPassThroughCode(stitchedFragment, variable));
- return sb.ToString();
- }
-
- private static string GetVertexPassThroughCode(StitchedFragmentSymbol stitchedFragment, VariableDeclarationNode variable)
- {
- return string.Format("\toutput.{0}.{1} = input.{1};", stitchedFragment.UniqueName, variable.Name);
- }
-
- private void WriteShaderCode(StitchedFragmentSymbol stitchedFragment, string shaderCode,
- string inputStructName, string outputStructName, string functionSuffix)
- {
- string mangledCode = shaderCode;
-
- // Replace interpolators and sampler names which are used in the code with the mangled names.
- mangledCode = ReplaceVariableNames(stitchedFragment, stitchedFragment.FragmentNode.Parameters, mangledCode);
- //mangledCode = ReplaceVariableNames(stitchedFragment, stitchedFragment.FragmentNode.Interpolators, mangledCode);
- if (stitchedFragment.FragmentNode.Textures != null)
- stitchedFragment.FragmentNode.Textures.VariableDeclarations.ForEach(t =>
- mangledCode = Regex.Replace(mangledCode, @"(\W)(" + t.Name + @")(\W)", "$1" + stitchedFragment.UniqueName + "_$2_sampler$3"));
-
- mangledCode = mangledCode.Replace("void main(", string.Format("void {0}_{1}(", stitchedFragment.UniqueName, functionSuffix));
- mangledCode = mangledCode.Replace("INPUT", string.Format("{0}_{1}", stitchedFragment.UniqueName, inputStructName));
- mangledCode = mangledCode.Replace("OUTPUT", outputStructName);
-
- _output.Append(mangledCode.Replace("\r", Environment.NewLine));
- }
-
- private static string ReplaceVariableNames(StitchedFragmentSymbol stitchedFragment, ParameterBlockNode parameters, string mangledCode)
- {
- if (parameters != null)
- parameters.VariableDeclarations.ToList().ForEach(i =>
- mangledCode = Regex.Replace(mangledCode, @"([^\w\.])(" + i.Name + @")(\W)", "$1" + stitchedFragment.UniqueName + "_$2$3"));
- return mangledCode;
- }
-
- private void WriteAllPixelShaders()
- {
- ForEachFragment(WritePixelShader);
-
- _output.AppendLine("// -------- pixel shader entrypoint --------");
- _output.AppendLine("PIXELOUTPUT ps(const PIXELINPUT i)");
- _output.AppendLine("{");
- _output.AppendLine("\tgPixelInput = i;");
- _output.AppendLine();
- _output.AppendLine("\tPIXELOUTPUT output = (PIXELOUTPUT) 0;");
- _output.AppendLine();
- ForEachFragment(f =>
- {
- ShaderCodeBlockNode shader = f.FragmentNode.PixelShaders.GetCodeBlock(_targetShaderProfile);
- if (shader != null)
- _output.AppendLineFormat("\t{0}_ps(gPixelInput.{0}, output);", f.UniqueName);
- });
- _output.AppendLine();
- _output.AppendLine("\treturn output;");
- _output.AppendLine("}");
- _output.AppendLine();
- }
-
- private void WritePixelShader(StitchedFragmentSymbol stitchedFragment)
- {
- ShaderCodeBlockNode codeBlock = stitchedFragment.FragmentNode.PixelShaders.GetCodeBlock(_targetShaderProfile);
- if (codeBlock != null)
- {
- _output.AppendLineFormat("// -------- pixel shader {0} --------", stitchedFragment.UniqueName);
-
- string shaderCode = ReplaceOutputCalls(codeBlock.Code, stitchedFragment.UniqueName);
- WriteShaderCode(stitchedFragment, shaderCode, "PIXELINPUT", "PIXELOUTPUT", "ps");
-
- _output.AppendLine();
- _output.AppendLine();
- }
- }
-
- private void WriteTechnique()
- {
- _output.AppendLine("// -------- technique --------");
- _output.AppendLine("technique");
- _output.AppendLine("{");
- _output.AppendLine("\tpass");
- _output.AppendLine("\t{");
- _output.AppendLineFormat("\t\tVertexShader = compile vs_{0} vs();", _targetShaderProfile.GetDescription());
- _output.AppendLineFormat("\t\tPixelShader = compile ps_{0} ps();", _targetShaderProfile.GetDescription());
- _output.AppendLine("\t}");
- _output.AppendLine("};");
- }
-
- private void WriteAllHeaderCode()
- {
- List<string> seenFragmentTypes = new List<string>();
- foreach (StitchedFragmentSymbol stitchedFragmentNode in _stitchedEffect.StitchedFragments)
- {
- if (seenFragmentTypes.Contains(stitchedFragmentNode.FragmentNode.Name))
- continue;
- WriteHeaderCode(stitchedFragmentNode);
- seenFragmentTypes.Add(stitchedFragmentNode.FragmentNode.Name);
- }
- }
-
- private void WriteHeaderCode(StitchedFragmentSymbol stitchedFragment)
- {
- if (stitchedFragment.FragmentNode.HeaderCode == null || string.IsNullOrEmpty(stitchedFragment.FragmentNode.HeaderCode.Code))
- return;
-
- _output.AppendLineFormat("// {0} header code", stitchedFragment.UniqueName);
- _output.Append(stitchedFragment.FragmentNode.HeaderCode.Code);
- _output.AppendLine();
- _output.AppendLine();
- }
-
- private void WriteAllParams()
- {
- ForEachFragment(WriteParams);
- }
-
- private void WriteParams(StitchedFragmentSymbol stitchedFragment)
- {
- if (stitchedFragment.FragmentNode.Parameters == null || !stitchedFragment.FragmentNode.Parameters.VariableDeclarations.Any())
- return;
-
- _output.AppendLineFormat("// {0} params", stitchedFragment.UniqueName);
- stitchedFragment.FragmentNode.Parameters.VariableDeclarations.ForEach(p => _output.AppendLine(GetVariableDeclaration(stitchedFragment, p)));
- _output.AppendLine();
- }
-
- private void WriteAllSamplers()
- {
- ForEachFragment(WriteSamplers);
- }
-
- private void WriteSamplers(StitchedFragmentSymbol stitchedFragment)
- {
- if (stitchedFragment.FragmentNode.Textures == null || !stitchedFragment.FragmentNode.Textures.VariableDeclarations.Any())
- return;
-
- _output.AppendLineFormat("// {0} textures", stitchedFragment.UniqueName);
- stitchedFragment.FragmentNode.Textures.VariableDeclarations.ForEach(t =>
- {
- _output.AppendLineFormat(GetVariableDeclaration(stitchedFragment, t));
- _output.AppendLineFormat("sampler {0}_{1}_sampler = sampler_state {{ Texture = ({0}_{1}); }};",
- stitchedFragment.UniqueName, t.Name);
- });
- _output.AppendLine();
- }
-
- private void WriteAllVertexInputStructures()
- {
- _output.AppendLine("// -------- vertex input structures --------");
-
- SemanticGenerator semanticGenerator = new SemanticGenerator("TEXCOORD");
- ForEachFragment(f => WriteVertexInputStructure(f, semanticGenerator));
-
- _output.AppendLine("struct VERTEXINPUT");
- _output.AppendLine("{");
-
- ForEachFragment(f => _output.AppendLineFormat("\t{0}_VERTEXINPUT {0};", f.UniqueName));
-
- _output.AppendLine("};");
- _output.AppendLine();
- _output.AppendLine("static VERTEXINPUT gVertexInput;");
- _output.AppendLine();
- }
-
- private void WriteAllVertexOutputStructures()
- {
- _output.AppendLine("// -------- vertex output structures --------");
- _output.AppendLine("struct VERTEXOUTPUT");
- _output.AppendLine("{");
- _output.AppendLineFormat("\tfloat4 position : POSITION;");
- ForEachFragment(f =>
- {
- if (f.FragmentNode.Interpolators != null && f.FragmentNode.Interpolators.VariableDeclarations.Any())
- _output.AppendLineFormat("\t{0}_PIXELINPUT {0};", f.UniqueName);
- });
- _output.AppendLine("};");
- _output.AppendLine();
- }
-
- private void WriteAllPixelInputStructures()
- {
- _output.AppendLine("// -------- pixel input structures --------");
-
- SemanticGenerator semanticGenerator = new SemanticGenerator("TEXCOORD");
- ForEachFragment(f => WritePixelInputStructure(f, semanticGenerator));
-
- _output.AppendLine("struct PIXELINPUT");
- _output.AppendLine("{");
- ForEachFragment(f => _output.AppendLineFormat("\t{0}_PIXELINPUT {0};", f.UniqueName));
- _output.AppendLine("};");
- _output.AppendLine();
- _output.AppendLine("static PIXELINPUT gPixelInput;");
- _output.AppendLine();
- }
-
- private void WriteVertexInputStructure(StitchedFragmentSymbol stitchedFragment, SemanticGenerator semanticGenerator)
- {
- WriteShaderInputStructure(stitchedFragment, semanticGenerator, "VERTEXINPUT", stitchedFragment.FragmentNode.VertexAttributes, false);
- }
-
- private void WritePixelInputStructure(StitchedFragmentSymbol stitchedFragment, SemanticGenerator semanticGenerator)
- {
- WriteShaderInputStructure(stitchedFragment, semanticGenerator, "PIXELINPUT", stitchedFragment.FragmentNode.Interpolators, true);
- }
-
- private void WriteShaderInputStructure(StitchedFragmentSymbol stitchedFragment, SemanticGenerator semanticGenerator, string structSuffix,
- ParameterBlockNode parameterBlock, bool alwaysUseTexCoords)
- {
- WriteShaderStructure(stitchedFragment, semanticGenerator, structSuffix, parameterBlock);
- }
-
- private void WriteShaderStructure(StitchedFragmentSymbol stitchedFragment, SemanticGenerator semanticGenerator,
- string structSuffix, ParameterBlockNode parameterBlock)
- {
- _output.AppendLineFormat("struct {0}_{1}", stitchedFragment.UniqueName, structSuffix);
- _output.AppendLine("{");
-
- if (parameterBlock != null)
- parameterBlock.VariableDeclarations.ForEach(v =>
- {
- string semantic = semanticGenerator.GetNextSemantic(v);
- _output.AppendLineFormat("\t{0} {1} : {2};",
- Token.GetString(v.DataType), v.Name, semantic);
- });
-
- _output.AppendLine("};");
- _output.AppendLine();
- }
-
- private static string GetVariableDeclaration(StitchedFragmentSymbol stitchedFragment, VariableDeclarationNode variable)
- {
- string arrayStuff = (variable.IsArray && variable.ArraySize != null) ? "[" + variable.ArraySize + "]" : string.Empty;
- string semantic = (!string.IsNullOrEmpty(variable.Semantic)) ? " : " + variable.Semantic : string.Empty;
- string initialValue = (!string.IsNullOrEmpty(variable.InitialValue)) ? " = " + variable.InitialValue : string.Empty;
-
- return string.Format("{0} {1}_{2}{3}{4}{5};",
- Token.GetString(variable.DataType), stitchedFragment.UniqueName,
- variable.Name, arrayStuff, semantic, initialValue);
- }
-
- private void ForEachFragment(Action<StitchedFragmentSymbol> action)
- {
- foreach (StitchedFragmentSymbol stitchedFragmentNode in _stitchedEffect.StitchedFragments)
- action(stitchedFragmentNode);
- }
- }
-}
@@ -1,13 +0,0 @@
-using System.Text;
-
-namespace StitchUp.Content.Pipeline.FragmentLinking.CodeGeneration
-{
- public static class StringBuilderExtensionMethods
- {
- public static void AppendLineFormat(this StringBuilder sb, string format, params object[] args)
- {
- sb.AppendFormat(format, args);
- sb.AppendLine();
- }
- }
-}
@@ -5,6 +5,6 @@ namespace StitchUp.Content.Pipeline.FragmentLinking.CodeModel
public class TechniquePassNode : ParseNode
{
public string Name { get; set; }
- public List<string> FragmentIdentifiers { get; set; }
+ public List<Token> Fragments { get; set; }
}
}
Oops, something went wrong.

0 comments on commit 7351c20

Please sign in to comment.