Пример #1
0
		private static void WriteVertexShader(EffectCodeGenerator generator, StitchedFragmentSymbol fragment,
			string uniquePassName, Dictionary<string, List<string>> exports)
		{
			ScriptTextWriter writer = generator.Writer;

			writer.WriteLine("// -------- vertex shader {0} --------", fragment.UniqueName);

			ShaderCodeBlockNode shader = fragment.FragmentNode.VertexShaders.GetCodeBlock(generator.Context.TargetShaderProfile);

			if (shader != null)
			{
				string shaderCode = StitchedEffectPreProcessor.PreProcessCodeBlock(fragment.UniqueName, shader, exports);
				shaderCode = ReplaceOutputCalls(shaderCode, fragment.UniqueName);
				WriteShaderCode(generator, fragment, shaderCode, "VERTEXINPUT", "VERTEXOUTPUT", "vs", uniquePassName);
			}
			else
			{
				// Need to auto-generate vertex shader. Simply pass through all vertex inputs.
				writer.WriteLine("void {0}_{1}_vs({0}_{1}_VERTEXINPUT input, inout {0}_VERTEXOUTPUT output)", uniquePassName, fragment.UniqueName);
				writer.WriteLine("{");
				writer.Write(GetVertexPassThroughCode(fragment));
				writer.WriteLine("}");
			}

			writer.WriteLine();
			writer.WriteLine();
		}
Пример #2
0
		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();
		}
Пример #3
0
		private static void WriteParams(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment)
		{
			if (stitchedFragment.FragmentNode.Parameters == null || !stitchedFragment.FragmentNode.Parameters.VariableDeclarations.Any())
				return;

			generator.Writer.WriteLine("// {0} params", stitchedFragment.UniqueName);
			stitchedFragment.FragmentNode.Parameters.VariableDeclarations.ForEach(p => generator.Writer.WriteLine(generator.GetVariableDeclaration(stitchedFragment, p)));
			generator.Writer.WriteLine();
		}
Пример #4
0
		private static void GenerateHeaderCode(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment)
		{
			if (stitchedFragment.FragmentNode.HeaderCode == null || string.IsNullOrEmpty(stitchedFragment.FragmentNode.HeaderCode.Code))
				return;

			generator.Writer.WriteLine("// {0} header code", stitchedFragment.UniqueName);
			generator.Writer.Write(stitchedFragment.FragmentNode.HeaderCode.Code.Replace("\r", Environment.NewLine));
			generator.Writer.WriteLine();
			generator.Writer.WriteLine();
		}
Пример #5
0
		private static void WriteSamplers(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment)
		{
			if (stitchedFragment.FragmentNode.Textures == null || !stitchedFragment.FragmentNode.Textures.VariableDeclarations.Any())
				return;

			generator.Writer.WriteLine("// {0} textures", stitchedFragment.UniqueName);
			stitchedFragment.FragmentNode.Textures.VariableDeclarations.ForEach(t =>
			{
				generator.Writer.WriteLine(generator.GetVariableDeclaration(stitchedFragment, t));
				generator.Writer.WriteLine("sampler {0}_{1}_sampler = sampler_state {{ Texture = ({0}_{1}); }};",
					stitchedFragment.UniqueName, t.Name);
			});
			generator.Writer.WriteLine();
		}
Пример #6
0
		private static void WritePixelInputStructure(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment, string uniqueName, SemanticGenerator semanticGenerator)
		{
			WriteShaderInputStructure(generator, stitchedFragment, uniqueName, semanticGenerator, "PIXELINPUT", stitchedFragment.FragmentNode.Interpolators, true);
		}
Пример #7
0
		private static void WriteShaderStructure(EffectCodeGenerator generator,
			StitchedFragmentSymbol stitchedFragment, string uniquePassName, SemanticGenerator semanticGenerator,
			string structSuffix, ParameterBlockNode parameterBlock)
		{
			generator.Writer.WriteLine("struct {0}_{1}_{2}", uniquePassName, stitchedFragment.UniqueName, structSuffix);
			generator.Writer.WriteLine("{");

			if (parameterBlock != null)
				parameterBlock.VariableDeclarations.ForEach(v =>
				{
					string semantic = semanticGenerator.GetNextSemantic(v);
					generator.Writer.WriteLine("\t{0} {1} : {2};",
						Token.GetString(v.DataType), v.Name, semantic);
				});

			generator.Writer.WriteLine("};");
			generator.Writer.WriteLine();
		}
Пример #8
0
		private static void WriteShaderInputStructure(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment, string uniqueName, SemanticGenerator semanticGenerator, string structSuffix,
			ParameterBlockNode parameterBlock, bool alwaysUseTexCoords)
		{
			WriteShaderStructure(generator, stitchedFragment, uniqueName, semanticGenerator, structSuffix, parameterBlock);
		}
Пример #9
0
		private static void WriteVertexInputStructure(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment, string uniqueName, SemanticGenerator semanticGenerator)
		{
			WriteShaderInputStructure(generator, stitchedFragment, uniqueName, semanticGenerator, "VERTEXINPUT", stitchedFragment.FragmentNode.VertexAttributes, false);
		}
Пример #10
0
		internal string GetVariableDeclaration(StitchedFragmentSymbol stitchedFragment, VariableDeclarationNode variable)
		{
			return GetVariableDeclaration(variable, stitchedFragment.UniqueName + "_");
		}
Пример #11
0
		private static void WriteShaderCode(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment, string shaderCode,
			string inputStructName, string outputStructName, string functionSuffix, string uniquePassName)
		{
			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}_{2}(", uniquePassName, stitchedFragment.UniqueName, functionSuffix));

			// This is just here to support surface shaders. Ideally it would be done somewhere else.
			mangledCode = Regex.Replace(mangledCode, @"(\b)surface\(", string.Format("$1{0}_{1}_{2}_surface(", uniquePassName, stitchedFragment.UniqueName, functionSuffix));

			mangledCode = mangledCode.Replace("INPUT", string.Format("{0}_{1}_{2}", uniquePassName, stitchedFragment.UniqueName, inputStructName));
			mangledCode = mangledCode.Replace("OUTPUT", uniquePassName + "_" + outputStructName);

			generator.Writer.Write(mangledCode.Replace("\r", Environment.NewLine));
		}
Пример #12
0
		private static string GetVertexPassThroughCode(StitchedFragmentSymbol stitchedFragment, VariableDeclarationNode variable)
		{
			return string.Format("\toutput.{0}.{1} = input.{1};", stitchedFragment.UniqueName, variable.Name);
		}
Пример #13
0
		private static void WritePixelShader(EffectCodeGenerator generator, StitchedFragmentSymbol stitchedFragment,
			string uniquePassName, Dictionary<string, List<string>> exports)
		{
			ShaderCodeBlockNode codeBlock = stitchedFragment.FragmentNode.PixelShaders.GetCodeBlock(generator.Context.TargetShaderProfile);
			if (codeBlock != null)
			{
				generator.Writer.WriteLine("// -------- pixel shader {0} --------", stitchedFragment.UniqueName);

				string shaderCode = StitchedEffectPreProcessor.PreProcessCodeBlock(stitchedFragment.UniqueName, codeBlock, exports);
				shaderCode = ReplaceOutputCalls(shaderCode, stitchedFragment.UniqueName);
				WriteShaderCode(generator, stitchedFragment, shaderCode, "PIXELINPUT", "PIXELOUTPUT", "ps", uniquePassName);

				generator.Writer.WriteLine();
				generator.Writer.WriteLine();
			}
		}
Пример #14
0
		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;
		}