Skip to content

Commit

Permalink
Simplify comparison of negation to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresTraks committed Jan 19, 2025
1 parent b98b20e commit 4d08044
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Hlsl/ComparisonNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace HlslDecompiler.Hlsl
{
public class ComparisonNode : HlslTreeNode
{
public ComparisonNode(HlslTreeNode factor1, HlslTreeNode factor2, IfComparison comparison)
public ComparisonNode(HlslTreeNode left, HlslTreeNode right, IfComparison comparison)
{
AddInput(factor1);
AddInput(factor2);
AddInput(left);
AddInput(right);
Comparison = comparison;
}

Expand Down
4 changes: 2 additions & 2 deletions Hlsl/HlslAstWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void WriteBreakStatement(BreakStatement breakStatement)
}
else
{
string comparison = _compiler.Compile(breakStatement.Comparison);
string comparison = _compiler.Compile(Reduce(breakStatement.Comparison));
WriteLine($"if ({comparison}) {{");
indent += "\t";
WriteLine("break;");
Expand All @@ -114,7 +114,7 @@ private void WriteIfStatement(IfStatement ifStatement)
{
WriteTempVariableAssignments(ifStatement.Closure);

string comparison = _compiler.Compile(ifStatement.Comparison);
string comparison = _compiler.Compile(Reduce(ifStatement.Comparison));
WriteLine($"if ({comparison}) {{");
indent += "\t";
WriteStatement(ifStatement.TrueBody);
Expand Down
29 changes: 29 additions & 0 deletions Hlsl/TemplateMatch/CompareNegativeWithZeroTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HlslDecompiler.DirectXShaderModel;
using System;

namespace HlslDecompiler.Hlsl.TemplateMatch
{
public class CompareNegativeWithZeroTemplate : NodeTemplate<ComparisonNode>
{
public override bool Match(HlslTreeNode node)
{
return node is ComparisonNode comp &&
comp.Left is NegateOperation && ConstantMatcher.IsZero(comp.Right);
}

public override HlslTreeNode Reduce(ComparisonNode node)
{
var comparison = node.Comparison switch
{
IfComparison.GT => IfComparison.LT,
IfComparison.GE => IfComparison.LE,
IfComparison.LT => IfComparison.GT,
IfComparison.LE => IfComparison.GE,
IfComparison.EQ => IfComparison.EQ,
IfComparison.NE => IfComparison.NE,
_ => throw new InvalidOperationException(node.Comparison.ToString()),
};
return new ComparisonNode((node.Left as NegateOperation).Value, node.Right, comparison);
}
}
}
3 changes: 2 additions & 1 deletion Hlsl/TemplateMatch/TemplateMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public TemplateMatcher(NodeGrouper nodeGrouper)
new ReciprocalReciprocalSquareRootTemplate(),
new ReciprocalSquareRootTemplate(),
new SubtractNegateTemplate(),
new SubtractZeroTemplate()
new SubtractZeroTemplate(),
new CompareNegativeWithZeroTemplate()
};
_groupTemplates = new List<IGroupTemplate>
{
Expand Down
Binary file modified HlslDecompiler.Tests/CompiledShaders/ps_3_0/if.fxc
Binary file not shown.
Binary file modified HlslDecompiler.Tests/CompiledShaders/ps_3_0/loop_nested.fxc
Binary file not shown.
3 changes: 3 additions & 0 deletions HlslDecompiler.Tests/HlslDecompiler.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@
<None Update="ShaderSources\ps_3_0\if.fx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="ShaderSources\ps_3_0\loop_nested.fx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="ShaderSources\ps_3_0\tex1d.fx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
20 changes: 12 additions & 8 deletions HlslDecompiler.Tests/ShaderAssembly/ps_3_0/if.asm
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
ps_3_0
def c1, 1, 2, 3, 4
def c2, 2, 3, 4, 5
def c0, 0, 1, 3, 4
dcl_texcoord v0
add r0, c1, v0_abs
max r1.x, c0.x, v0.x
add r1.x, -r1.x, v0.y
cmp r0, r1.x, c1, r0
mul r2, c2, v0
cmp r1, r1.x, c2, r2
dcl_2d s0
if_lt -v0.y, c0.x
texldl r0, v0, s0
else
mov r0, c0.yxzw
endif
if_ge -v0.x, c0.x
texld r1, v0.xy, s0
add oC0, r0, r1
else
add oC0, r0, c0.yxzw
endif
18 changes: 11 additions & 7 deletions HlslDecompiler.Tests/ShaderAssembly/ps_3_0/loop_nested.asm
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
ps_3_0
def c2, 0, 0, 3, 5
def c3, 1, 0, 0, 0
def c2, 0, 0, 3, 1
def c3, 5, 0, 0, 0
defi i0, 255, 0, 0, 0
dcl_texcoord v0
mov r0, c2.y
mov r1, c2.y
mov r2.x, c2.z
rep i0
break_ge r2.x, c0.x
if_ge r2.x, c0.x
break_ne c2.w, -c2.w
endif
mov r3, r1
mov r2.y, c2.w
mov r2.y, c3.x
rep i0
break_ge r2.y, c1.x
if_ge r2.y, c1.x
break_ne c2.w, -c2.w
endif
add r3, r3, v0
add r2.y, r2.y, c3.x
add r2.y, r2.y, c2.w
endrep
mov r1, r3
add r0, r0, v0
add r2.x, r2.x, c3.x
add r2.x, r2.x, c2.w
endrep
add oC0, r0, r1
20 changes: 11 additions & 9 deletions HlslDecompiler.Tests/ShaderSources/ps_3_0/if.fx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
float count;
sampler2D sampler0;

float4 main(float4 texcoord : TEXCOORD) : COLOR
{
float4 r0 = float4(1, 2, 3, 4);
float4 r1 = float4(2, 3, 4, 5);
float check = max(count, texcoord.x);
if (check > texcoord.y)
{
r0 += abs(texcoord);
r1 *= texcoord;
float4 r0;
if (texcoord.y > 0) {
r0 = tex2Dlod(sampler0, texcoord);
} else {
r0 = float4(1, 0, 3, 4);
}
if (texcoord.x <= 0) {
return r0 + tex2D(sampler0, texcoord.xy);
} else {
return r0 + float4(1, 0, 3, 4);
}
return r0 + r1;
}
31 changes: 21 additions & 10 deletions HlslDecompiler.Tests/ShaderSources/ps_3_0/loop_nested.fx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ float count2;

float4 main(float4 texcoord : TEXCOORD) : COLOR
{
float4 r0 = 0;
float4 r1 = 0;
for (int i = 3; i < count; i++) {
for (int j = 5; j < count2; j++)
{
r1 += texcoord;
}
r0 += texcoord;
}
return r0 + r1;
float4 t0 = 0;
float4 t1 = 0;
float t2 = 3;
for (int i = 0; i < 255; i++) {
if (t2.x >= count) {
break;
}
float4 t3 = t1;
float t4 = 5;
for (int i = 0; i < 255; i++) {
if (t4 >= count2) {
break;
}
t3 = t3 + texcoord;
t4 = t4 + 1;
}
t0 = t0 + texcoord;
t1 = t3;
t2 = t2 + 1;
}
return t0 + t1;
}
21 changes: 12 additions & 9 deletions HlslDecompiler.Tests/ShaderSources/ps_3_0_instruction/if.fx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
float count;
sampler2D sampler0;

float4 main(float4 texcoord : TEXCOORD) : COLOR
{
float4 o;

float4 r0;
float4 r1;
float4 r2;
r0 = float4(1, 2, 3, 4) + abs(texcoord);
r1.x = max(count.x, texcoord.x);
r1.x = -r1.x + texcoord.y;
r0 = (r1.x >= 0) ? float4(1, 2, 3, 4) : r0;
r2 = float4(2, 3, 4, 5) * texcoord;
r1 = (r1.x >= 0) ? float4(2, 3, 4, 5) : r2;
o = r0 + r1;
if (-texcoord.y < 0) {
r0 = tex2Dlod(sampler0, texcoord);
} else {
r0 = float4(1, 0, 3, 4);
}
if (-texcoord.x >= 0) {
r1 = tex2D(sampler0, texcoord.xy);
o = r0 + r1;
} else {
o = r0 + float4(1, 0, 3, 4);
}

return o;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ float4 main(float4 texcoord : TEXCOORD) : COLOR
r1.x = 3;
for (int i0 = 0; i0 < 255; i0++) {
if (r1.x < count.x) {
break;
if (1 != -1) break;
}
r0 = r0 + texcoord;
r1.x = r1.x + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ float4 main(float4 texcoord : TEXCOORD) : COLOR
{
float4 o;

float4 t0 = 0;
float4 t1 = 0;
float t3 = 3;
float4 r0;
float4 r1;
float2 r2;
float4 r3;
r0 = 0;
r1 = 0;
r2.x = 3;
for (int i0 = 0; i0 < 255; i0++) {
t0 = t0 + texcoord;
t3.x = t3.x + 1;
if (r2.x >= count.x) {
if (1 != -1) break;
}
r3 = r1;
r2.y = 5;
for (int i1 = 0; i1 < 255; i1++) {
if (r2.y >= count2.x) {
if (1 != -1) break;
}
r3 = r3 + texcoord;
r2.y = r2.y + 1;
}
r1 = r3;
r0 = r0 + texcoord;
r2.x = r2.x + 1;
}
o = t0;
o = r0 + r1;

return o;
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016–2021 Andres Traks
Copyright (c) 2016–2025 Andres Traks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 4d08044

Please sign in to comment.