private string CompileNodesWithComponents(List <HlslTreeNode> components, HlslTreeNode first, int promoteToVectorSize) { var componentsWithIndices = components.Cast <IHasComponentIndex>(); if (first is RegisterInputNode shaderInput) { var registerKey = shaderInput.RegisterComponentKey.RegisterKey; string swizzle = ""; if (registerKey.Type != RegisterType.Sampler) { swizzle = GetAstSourceSwizzleName(componentsWithIndices, _registers.GetRegisterFullLength(registerKey), promoteToVectorSize); } string name = _registers.GetRegisterName(registerKey); return($"{name}{swizzle}"); } if (first is TextureLoadOutputNode textureLoad) { string swizzle = GetAstSourceSwizzleName(componentsWithIndices, 4); string sampler = Compile(new[] { textureLoad.SamplerInput }); string texcoords = Compile(textureLoad.TextureCoordinateInputs); return($"tex2D({sampler}, {texcoords}){swizzle}"); } if (first is NormalizeOutputNode) { string input = Compile(first.Inputs); string swizzle = GetAstSourceSwizzleName(componentsWithIndices, 4); return($"normalize({input}){swizzle}"); } throw new NotImplementedException(); }
protected override void Write() { if (_shader.Type == ShaderType.Expression) { Write($"// Writing expression"); WriteExpression(_shader); return; } _registers = new RegisterState(_shader); WriteConstantDeclarations(); if (_shader.Preshader != null) { WriteExpression(_shader.Preshader.Shader); } if (_registers.MethodInputRegisters.Count > 1) { WriteInputStructureDeclaration(); } if (_registers.MethodOutputRegisters.Count > 1) { WriteOutputStructureDeclaration(); } string methodReturnType = GetMethodReturnType(); string methodParameters = GetMethodParameters(); string methodSemantic = GetMethodSemantic(); WriteLine("{0} {1}({2}){3}", methodReturnType, _entryPoint, methodParameters, methodSemantic); WriteLine("{"); Indent++; if (_registers.MethodOutputRegisters.Count > 1) { var outputStructType = _shader.Type == ShaderType.Pixel ? "PS_OUT" : "VS_OUT"; WriteIndent(); WriteLine($"{outputStructType} o;"); } else { var output = _registers.MethodOutputRegisters.First().Value; WriteIndent(); WriteLine("{0} {1};", methodReturnType, _registers.GetRegisterName(output.RegisterKey)); } WriteTemps(); HlslAst ast = null; if (_doAstAnalysis) { var parser = new BytecodeParser(); ast = parser.Parse(_shader); ast.ReduceTree(); WriteAst(ast); } else { WriteInstructionList(); if (_registers.MethodOutputRegisters.Count > 1) { WriteIndent(); WriteLine($"return o;"); } else { var output = _registers.MethodOutputRegisters.First().Value; WriteIndent(); WriteLine($"return {_registers.GetRegisterName(output.RegisterKey)};"); } } Indent--; WriteLine("}"); }