Skip to content

Commit

Permalink
Fix depth write mask
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresTraks committed Jan 26, 2025
1 parent 6a83bfc commit 3aaa264
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DirectXShaderModel/AsmWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ static string GetDestinationName(Instruction instruction)
{
int destIndex = instruction.GetDestinationParamIndex();
string registerName = instruction.GetParamRegisterName(destIndex);
const int registerLength = 4;
string writeMaskName = instruction.GetDestinationWriteMaskName(registerLength, false);
int destinationLength = instruction.GetDestinationSemanticSize();
string writeMaskName = instruction.GetDestinationWriteMaskName(destinationLength);

return $"{registerName}{writeMaskName}";
}
Expand Down
11 changes: 10 additions & 1 deletion DirectXShaderModel/D3D10Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public override string GetSourceSwizzleName(int srcIndex, int? destinationLength
public override string GetDeclSemantic()
{
string name;
switch (GetOperandType(0))
switch (GetOperandType(GetDestinationParamIndex()))
{
case OperandType.Input:
name = "SV_Position";
Expand All @@ -294,6 +294,15 @@ public override string GetDeclSemantic()
return name;
}

public override int GetDestinationSemanticSize()
{
if (GetOperandType(GetDestinationParamIndex()) == OperandType.OutputDepth)
{
return 1;
}
return 4;
}

private byte[] GetOperandValueBytes(int index, int componentIndex)
{
Span<uint> span = OperandTokens.GetSpan(index);
Expand Down
10 changes: 10 additions & 0 deletions DirectXShaderModel/D3D9Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public SamplerTextureType GetDeclSamplerTextureType()
return (SamplerTextureType)((Params[0] >> 27) & 0xF);
}

public override int GetDestinationSemanticSize()
{
RegisterType registerType = GetParamRegisterType(GetDestinationParamIndex());
if (registerType == RegisterType.DepthOut)
{
return 1;
}
return 4;
}

public override int GetDestinationParamIndex()
{
if (Opcode == Opcode.Dcl) return 1;
Expand Down
16 changes: 6 additions & 10 deletions DirectXShaderModel/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ public abstract class Instruction

public abstract int GetDestinationWriteMask();

public string GetDestinationWriteMaskName(int destinationLength, bool hlsl)
public string GetDestinationWriteMaskName(int destinationLength)
{
int writeMask = GetDestinationWriteMask();
int writeMaskLength = GetDestinationMaskLength();
int destinationMask = (1 << destinationLength) - 1;
int writeMask = GetDestinationWriteMask() & destinationMask;

if (!hlsl)
{
destinationLength = 4; // explicit mask in assembly
}

// Check if mask is the same length and of the form .xyzw
if (writeMaskLength == destinationLength && writeMask == ((1 << writeMaskLength) - 1))
if (writeMask == destinationMask)
{
return "";
}
Expand Down Expand Up @@ -77,5 +71,7 @@ public byte[] GetSourceSwizzleComponents(int srcIndex)
public abstract string GetSourceSwizzleName(int srcIndex, int? destinationLength);

public abstract string GetDeclSemantic();

public abstract int GetDestinationSemanticSize();
}
}
12 changes: 10 additions & 2 deletions Hlsl/HlslSimpleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private string GetDestinationName(Instruction instruction)
string registerName = _registers.GetRegisterName(registerKey);
registerName = registerName ?? instruction.GetParamRegisterName(destIndex);
int registerLength = _registers.GetRegisterFullLength(registerKey);
string writeMaskName = instruction.GetDestinationWriteMaskName(registerLength, true);
string writeMaskName = instruction.GetDestinationWriteMaskName(registerLength);

return string.Format("{0}{1}", registerName, writeMaskName);
}
Expand Down Expand Up @@ -452,7 +452,15 @@ private string GetSourceConstantValue(D3D9Instruction instruction, int srcIndex,
{
if (instruction.HasDestination)
{
destinationLength = instruction.GetDestinationMaskLength();
int writeMask = instruction.GetDestinationWriteMask();
destinationLength = 0;
for (int i = 0; i < 4; i++)
{
if ((writeMask & (1 << i)) != 0)
{
destinationLength++;
}
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ PS_OUT main(PS_IN i)

o.color.w = (i.vface >= 0) ? 1 : -1;
o.color.xyz = i.vpos.xxy * float3(0, 1, 1) + float3(0.3, 0, 0);
o.depth.xyzw = -123456;
o.depth = -123456;

return o;
}

0 comments on commit 3aaa264

Please sign in to comment.