Merge pull request #805 from Sergio0694/dev/workaround-fxc-float-bug
Always emit 'asfloat' for float literals in D2D shaders
このコミットが含まれているのは:
10ファイル変更
+68
-16
検索条件に一致するファイルはありません
@@ -196,7 +196,7 @@ internal abstract partial class HlslSourceRewriter(
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public sealed override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node)
|
||||
public sealed unsafe override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node)
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -219,6 +219,7 @@ internal abstract partial class HlslSourceRewriter(
|
||||
// in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix.
|
||||
if (type.SpecialType == SpecialType.System_Single)
|
||||
{
|
||||
#if D3D12_SOURCE_GENERATOR
|
||||
string literal = updatedNode.Token.ValueText;
|
||||
|
||||
// If the numeric literal is neither a decimal nor an exponential, add the ".0" suffix
|
||||
@@ -228,6 +229,25 @@ internal abstract partial class HlslSourceRewriter(
|
||||
}
|
||||
|
||||
return updatedNode.WithToken(Literal(literal, 0f));
|
||||
#else
|
||||
// When compiling for D2D, we need to emit 'asfloat' expressions to work around an FXC
|
||||
// bug that can cause it to sometimes emit incorrect values for float literals. For
|
||||
// additional context, see: https://github.com/Sergio0694/ComputeSharp/issues/780.
|
||||
//
|
||||
// This code rewrites expressions as follows:
|
||||
// C#: 3.14f
|
||||
// HLSL: asfloat(1078523331)
|
||||
float literalValue = (float)operation.ConstantValue.Value!;
|
||||
uint literalValueAsUInt = *(uint*)&literalValue;
|
||||
|
||||
return
|
||||
InvocationExpression(IdentifierName("asfloat"))
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
LiteralExpression(
|
||||
SyntaxKind.NumericLiteralExpression,
|
||||
Literal(literalValueAsUInt))));
|
||||
#endif
|
||||
}
|
||||
else if (type.SpecialType == SpecialType.System_Double)
|
||||
{
|
||||
|
||||
+8
-8
@@ -186,8 +186,8 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
{
|
||||
int2 xy = (int2)D2DGetScenePosition().xy;
|
||||
float2 uv = xy / (float2)dispatchSize;
|
||||
float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, 1.0);
|
||||
float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, asfloat(1065353216U));
|
||||
}
|
||||
""";
|
||||
|
||||
@@ -482,8 +482,8 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
{
|
||||
int2 xy = (int2)D2DGetScenePosition().xy;
|
||||
float2 uv = xy / (float2)dispatchSize;
|
||||
float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, 1.0);
|
||||
float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, asfloat(1065353216U));
|
||||
}
|
||||
""";
|
||||
|
||||
@@ -830,8 +830,8 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
{
|
||||
int2 xy = (int2)D2DGetScenePosition().xy;
|
||||
float2 uv = xy / (float2)dispatchSize;
|
||||
float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, 1.0);
|
||||
float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, asfloat(1065353216U));
|
||||
}
|
||||
""";
|
||||
|
||||
@@ -1124,8 +1124,8 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
{
|
||||
int2 xy = (int2)D2DGetScenePosition().xy;
|
||||
float2 uv = xy / (float2)dispatchSize;
|
||||
float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, 1.0);
|
||||
float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4)));
|
||||
return float4(col, asfloat(1065353216U));
|
||||
}
|
||||
""";
|
||||
|
||||
|
||||
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 123 B |
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 919 KiB |
@@ -90,12 +90,15 @@
|
||||
<None Update="Assets\ContouredLayers.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="..\ComputeSharp.Tests\Assets\TerracedHills.png" Link="Assets\TerracedHills.png">
|
||||
<None Include="Assets\TerracedHills.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\RadialFadeOut.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\Green32x32.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="..\..\samples\ComputeSharp.SwapChain\Textures\RustyMetal.png" Link="Assets\Textures\RustyMetal.png" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public partial class D2D1ReflectionServicesTests
|
||||
float4 input1 = D2DGetInput(1);
|
||||
float4 input2 = D2DSampleInputAtPosition(2, xy + offset);
|
||||
float value3 = __reserved__buffer[0];
|
||||
float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, 0.5));
|
||||
float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, asfloat(1056964608U)));
|
||||
return input0 + input1 + input2 + value3 + value4;
|
||||
}
|
||||
""", shaderInfo.HlslSource);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Tests.Helpers;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
#pragma warning disable CA1822
|
||||
@@ -82,7 +83,7 @@ public partial class ShaderRewriterTests
|
||||
D2D_PS_ENTRY(Execute)
|
||||
{
|
||||
bool4 mask4 = bool4(true, false, true, true);
|
||||
float4 float4_1 = float4(1, 2, 3.14, 4);
|
||||
float4 float4_1 = float4(1, 2, asfloat(1078523331U), 4);
|
||||
float4 float4_2 = float4(5, 6, 7, 8);
|
||||
float4 float4_r = (mask4 ? float4_1 : float4_2);
|
||||
bool1x3 mask1x3 = bool1x3((bool)true, (bool)true, (bool)false);
|
||||
@@ -93,7 +94,7 @@ public partial class ShaderRewriterTests
|
||||
uint2x4 uint2x4_1 = uint2x4((uint)1, (uint)2, (uint)3, (uint)4, (uint)5, (uint)6, (uint)7, (uint)8);
|
||||
uint2x4 uint2x4_2 = uint2x4((uint)111, (uint)222, (uint)333, (uint)444, (uint)555, (uint)666, (uint)777, (uint)888);
|
||||
uint2x4 uint2x4_r = (mask2x4 ? uint2x4_1 : uint2x4_2);
|
||||
float2x2 f2x2_r = mul((float2x4)uint2x4_r, float4x2((float2)float4_r.xy, (float2)float4_r.zw, (float2)float2(int1x3_r._m00, int1x3_r._m01), (float2)float2(int1x3_r._m02, 1.0)));
|
||||
float2x2 f2x2_r = mul((float2x4)uint2x4_r, float4x2((float2)float4_r.xy, (float2)float4_r.zw, (float2)float2(int1x3_r._m00, int1x3_r._m01), (float2)float2(int1x3_r._m02, asfloat(1065353216U))));
|
||||
return float4(f2x2_r._m00, f2x2_r._m01, f2x2_r._m10, f2x2_r._m11);
|
||||
}
|
||||
""", shaderInfo.HlslSource);
|
||||
@@ -179,4 +180,32 @@ public partial class ShaderRewriterTests
|
||||
mask2x3_r_or.M11 ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
// See https://github.com/Sergio0694/ComputeSharp/issues/780
|
||||
[TestMethod]
|
||||
public void ProblematicFloatLiteralValue_IsRewrittenCorrectly()
|
||||
{
|
||||
D2D1TestRunner.RunAndCompareShader(
|
||||
new ProblematicFloatLiteralValueShader(131072.65f),
|
||||
32,
|
||||
32,
|
||||
"Green32x32.png");
|
||||
}
|
||||
|
||||
[D2DInputCount(0)]
|
||||
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
|
||||
[D2DOutputBuffer(D2D1BufferPrecision.Float32)]
|
||||
[D2DGeneratedPixelShaderDescriptor]
|
||||
internal readonly partial struct ProblematicFloatLiteralValueShader(float x) : ID2D1PixelShader
|
||||
{
|
||||
public float4 Execute()
|
||||
{
|
||||
if (x != 131072.65f)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return new float4(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class ShadersTests
|
||||
[TestMethod]
|
||||
public void TerracedHills()
|
||||
{
|
||||
RunTest<TerracedHills>(0.000026f);
|
||||
RunTest<TerracedHills>(0.000027f);
|
||||
}
|
||||
|
||||
private static void RunTest<T>(float threshold = 0.00001f)
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
<Link>Assets\Shaders\ProteanClouds.png</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="..\ComputeSharp.Tests\Assets\TerracedHills.png">
|
||||
<Content Include="..\ComputeSharp.D2D1.Tests\Assets\TerracedHills.png">
|
||||
<Link>Assets\Shaders\TerracedHills.png</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
||||
@@ -68,7 +68,7 @@ public class ShadersTests
|
||||
[DataRow(WrapperType.CanvasEffect)]
|
||||
public async Task TerracedHills(WrapperType wrapperType)
|
||||
{
|
||||
await RunTestAsync<TerracedHills>(wrapperType, 0.000026f);
|
||||
await RunTestAsync<TerracedHills>(wrapperType, 0.000027f);
|
||||
}
|
||||
|
||||
private static async Task RunTestAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(WrapperType wrapperType, float threshold = 0.00001f)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする