Skip to content

Commit

Permalink
Nested loop variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresTraks committed May 8, 2021
1 parent b3347c9 commit b011ff8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin
obj
packages
.vs
/Properties/launchSettings.json
2 changes: 1 addition & 1 deletion Hlsl/HlslAstWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override void WriteMethodBody()
WriteLine();
}

var parser = new BytecodeParser();
var parser = new InstructionToAstParser();
HlslAst ast = parser.Parse(_shader);
ast.ReduceTree(new NodeGrouper(_registers));

Expand Down
13 changes: 12 additions & 1 deletion Hlsl/HlslSimpleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace HlslDecompiler
{
public class HlslSimpleWriter : HlslWriter
{
private int _loopVariableIndex = -1;

public HlslSimpleWriter(ShaderModel shader)
: base(shader)
{
Expand Down Expand Up @@ -123,8 +125,10 @@ private void WriteInstruction(Instruction instruction)
WriteLine("}");
break;
case Opcode.EndLoop:
case Opcode.EndRep:
indent = indent.Substring(0, indent.Length - 1);
WriteLine("}");
_loopVariableIndex--;
break;
case Opcode.Exp:
WriteLine("{0} = exp2({1});", GetDestinationName(instruction), GetSourceName(instruction, 1));
Expand Down Expand Up @@ -191,7 +195,8 @@ private void WriteInstruction(Instruction instruction)
uint end = intRegister.Value[0];
uint start = intRegister.Value[1];
uint stride = intRegister.Value[2];
string loopVariable = "i0";
_loopVariableIndex++;
string loopVariable = "i" + _loopVariableIndex;
if (stride == 1)
{
WriteLine("for (int {2} = {0}; {2} < {1}; {2}++) {{", start, end, loopVariable);
Expand Down Expand Up @@ -238,6 +243,12 @@ private void WriteInstruction(Instruction instruction)
case Opcode.Rcp:
WriteLine("{0} = 1 / {1};", GetDestinationName(instruction), GetSourceName(instruction, 1));
break;
case Opcode.Rep:
ConstantIntRegister loopRegister = _registers.FindConstantIntRegister(instruction.GetParamRegisterNumber(0));
_loopVariableIndex++;
WriteLine("for (int {1} = 0; {1} < {0}; {1}++) {{", loopRegister[0], "i" + _loopVariableIndex);
indent += "\t";
break;
case Opcode.Rsq:
WriteLine("{0} = 1 / sqrt({1});", GetDestinationName(instruction), GetSourceName(instruction, 1));
break;
Expand Down

0 comments on commit b011ff8

Please sign in to comment.