-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify comparison of negation to zero
- Loading branch information
1 parent
b98b20e
commit 4d08044
Showing
15 changed files
with
131 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file modified
BIN
+32 Bytes
(110%)
HlslDecompiler.Tests/CompiledShaders/ps_3_0/loop_nested.fxc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
HlslDecompiler.Tests/ShaderAssembly/ps_3_0/loop_nested.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 12 additions & 9 deletions
21
HlslDecompiler.Tests/ShaderSources/ps_3_0_instruction/if.fx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters