Skip to content

Commit

Permalink
Prepare for SM4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresTraks committed Jun 23, 2022
1 parent a47be23 commit 7bcd91c
Show file tree
Hide file tree
Showing 231 changed files with 3,230 additions and 744 deletions.
131 changes: 79 additions & 52 deletions DirectXShaderModel/AsmWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HlslDecompiler.Util;
using System;
using System.Globalization;
using System.IO;

namespace HlslDecompiler.DirectXShaderModel
Expand All @@ -16,11 +15,6 @@ public AsmWriter(ShaderModel shader)
this.shader = shader;
}

void WriteLine()
{
asmWriter.WriteLine();
}

void WriteLine(string value)
{
asmWriter.WriteLine(value);
Expand All @@ -31,48 +25,11 @@ void WriteLine(string format, params object[] args)
asmWriter.WriteLine(format, args);
}

static string ApplyModifier(SourceModifier modifier, string value)
{
switch (modifier)
{
case SourceModifier.None:
return value;
case SourceModifier.Negate:
return $"-{value}";
case SourceModifier.Bias:
return $"{value}_bias";
case SourceModifier.BiasAndNegate:
return $"-{value}_bias";
case SourceModifier.Sign:
return $"{value}_bx2";
case SourceModifier.SignAndNegate:
return $"-{value}_bx2";
case SourceModifier.Complement:
throw new NotImplementedException();
case SourceModifier.X2:
return $"{value}_x2";
case SourceModifier.X2AndNegate:
return $"-{value}_x2";
case SourceModifier.DivideByZ:
return $"{value}_dz";
case SourceModifier.DivideByW:
return $"{value}_dw";
case SourceModifier.Abs:
return $"{value}_abs";
case SourceModifier.AbsAndNegate:
return $"-{value}_abs";
case SourceModifier.Not:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}

string GetDestinationName(Instruction instruction)
{
int destIndex = instruction.GetDestinationParamIndex();
string registerName = instruction.GetParamRegisterName(destIndex);
if (instruction.Opcode == Opcode.Loop)
if (instruction is D3D9Instruction d3D9Instruction && d3D9Instruction.Opcode == Opcode.Loop)
{
return registerName;
}
Expand All @@ -85,13 +42,16 @@ string GetDestinationName(Instruction instruction)

string GetSourceName(Instruction instruction, int srcIndex)
{
string sourceRegisterName = instruction.GetParamRegisterName(srcIndex);
if (instruction.Opcode == Opcode.Loop)
string sourceName = instruction.GetParamRegisterName(srcIndex);
if (instruction is D3D9Instruction d3D9Instruction)
{
return sourceRegisterName;
if (d3D9Instruction.Opcode != Opcode.Loop)
{
sourceName += instruction.GetSourceSwizzleName(srcIndex);
sourceName = ApplyModifier(d3D9Instruction.GetSourceModifier(srcIndex), sourceName);
}
}
sourceRegisterName += instruction.GetSourceSwizzleName(srcIndex);
return ApplyModifier(instruction.GetSourceModifier(srcIndex), sourceRegisterName);
return sourceName;
}

public void Write(string asmFilename)
Expand All @@ -104,14 +64,21 @@ public void Write(string asmFilename)

foreach (Instruction instruction in shader.Instructions)
{
WriteInstruction(instruction);
if (instruction is D3D10Instruction d3D10Instruction)
{
WriteD3D10Instruction(d3D10Instruction);
}
else
{
WriteInstruction(instruction as D3D9Instruction);
}
}

asmWriter.Dispose();
asmFile.Dispose();
}

private void WriteInstruction(Instruction instruction)
private void WriteInstruction(D3D9Instruction instruction)
{
switch (instruction.Opcode)
{
Expand Down Expand Up @@ -302,7 +269,30 @@ private void WriteInstruction(Instruction instruction)
}
}

private static string GetModifier(Instruction instruction)
private void WriteD3D10Instruction(D3D10Instruction instruction)
{
switch (instruction.Opcode)
{
case D3D10Opcode.Add:
break;
case D3D10Opcode.DclInputPS:
WriteLine("dcl_input_ps linear");
break;
case D3D10Opcode.DclTemps:
WriteLine("dcl_temps {0}", instruction.GetParamInt(0));
break;
case D3D10Opcode.Mov:
WriteLine("mov {0}, {1}", GetDestinationName(instruction), "v0.w");
break;
default:
WriteLine(instruction.Opcode.ToString());
Console.WriteLine(instruction.Opcode);
//throw new NotImplementedException();
break;
}
}

private static string GetModifier(D3D9Instruction instruction)
{
ResultModifier resultModifier = instruction.GetDestinationResultModifier();
switch (resultModifier)
Expand All @@ -319,5 +309,42 @@ private static string GetModifier(Instruction instruction)
throw new NotSupportedException("Not supported result modifier " + resultModifier);
}
}

static string ApplyModifier(SourceModifier modifier, string value)
{
switch (modifier)
{
case SourceModifier.None:
return value;
case SourceModifier.Negate:
return $"-{value}";
case SourceModifier.Bias:
return $"{value}_bias";
case SourceModifier.BiasAndNegate:
return $"-{value}_bias";
case SourceModifier.Sign:
return $"{value}_bx2";
case SourceModifier.SignAndNegate:
return $"-{value}_bx2";
case SourceModifier.Complement:
throw new NotImplementedException();
case SourceModifier.X2:
return $"{value}_x2";
case SourceModifier.X2AndNegate:
return $"-{value}_x2";
case SourceModifier.DivideByZ:
return $"{value}_dz";
case SourceModifier.DivideByW:
return $"{value}_dw";
case SourceModifier.Abs:
return $"{value}_abs";
case SourceModifier.AbsAndNegate:
return $"-{value}_abs";
case SourceModifier.Not:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
}
}
137 changes: 137 additions & 0 deletions DirectXShaderModel/D3D10Instruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System;

namespace HlslDecompiler.DirectXShaderModel
{
public class D3D10Instruction : Instruction
{
public D3D10Opcode Opcode { get; }
public D3D10ParamCollection ParamTokens { get; }

public D3D10Instruction(D3D10Opcode opcode, uint[] paramTokens)
{
Opcode = opcode;
ParamTokens = new D3D10ParamCollection(paramTokens);
}

public override bool HasDestination
{
get
{
switch (Opcode)
{
case D3D10Opcode.Add:
case D3D10Opcode.DclInputPS:
case D3D10Opcode.DclOutput:
case D3D10Opcode.DclTemps:
case D3D10Opcode.Dp2:
case D3D10Opcode.GE:
case D3D10Opcode.Mad:
case D3D10Opcode.Mov:
case D3D10Opcode.MovC:
case D3D10Opcode.Mul:
return true;
default:
return false;
}
}
}

public override bool IsTextureOperation
{
get
{
switch (Opcode)
{
default:
return false;
}
}
}

public override int GetDestinationParamIndex()
{
throw new NotImplementedException();
}

public override int GetDestinationWriteMask()
{
throw new NotImplementedException();
}

public override int GetDestinationMaskedLength()
{
return 4;
}

public override int GetSourceSwizzle(int srcIndex)
{
throw new NotImplementedException();
}

public override string GetSourceSwizzleName(int srcIndex)
{
throw new NotImplementedException();
}

public override string GetDeclSemantic()
{
switch (GetParamOperandType(0))
{
case OperandType.Output:
return "sem";
//return GetDeclUsage().ToString().ToUpper();
default:
throw new NotImplementedException();
}
}

public override float GetParamSingle(int index)
{
throw new NotImplementedException();
}

public override float GetParamInt(int index)
{
throw new NotImplementedException();
}

private Span<uint> GetParamSpan(int index)
{
int paramCount = 0;
for (int i = 0; i < ParamTokens.Count; i++)
{
uint param = ParamTokens.Tokens[i];
int numComponents = (int)(param & 3);
numComponents.ToString();
}
return new Span<uint>(ParamTokens.Tokens, 0, 1);
}

public override RegisterKey GetParamRegisterKey(int index)
{
return new RegisterKey(
GetParamOperandType(index));
}

public OperandType GetParamOperandType(int index)
{
uint p = ParamTokens.Tokens[index];
return (OperandType)((p >> 12) & 0xFF);
}

public override string GetParamRegisterName(int index)
{
throw new NotImplementedException();
}

public override int GetParamRegisterNumber(int index)
{
throw new NotImplementedException();
}

public override string ToString()
{
return Opcode.ToString();
}
}
}
Loading

0 comments on commit 7bcd91c

Please sign in to comment.