Fix constants tracking when rewriting shader fields

このコミットが含まれているのは:
Sergio Pedri committed 2022-05-04 23:28:08 +02:00
1 親 d707bc51fc
コミット ee16303aeb
3ファイル変更
+54 -7

検索条件に一致するファイルはありません

@@ -56,9 +56,11 @@ partial class ID2D1ShaderGenerator
var semanticModelProvider = new SemanticModelProvider(compilation);
var valueFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Process the discovered types and constants
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Gather the shader metadata
GatherD2D1AttributeInfo(
+3 -1
ファイルの表示
@@ -67,9 +67,11 @@ partial class IShaderGenerator
var sharedBuffers = GetSharedBuffers(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader);
var implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Process the discovered types and constants
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
diagnostics = builder.ToImmutable();
+48 -5
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp.Tests.Attributes;
using System;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -90,7 +91,7 @@ public partial class ShaderRewriterTests
{
float[] data = { 1, 6, 7, 3, 5, 2, 8, 4 };
using ReadWriteBuffer<float> buffer = device.Get().AllocateReadWriteBuffer<float>(data);
using ReadWriteBuffer<float> buffer = device.Get().AllocateReadWriteBuffer(data);
device.Get().For(1, new ModAndComparisonOperatorsShader(buffer));
@@ -123,9 +124,51 @@ public partial class ShaderRewriterTests
buffer[2] = mod.Z;
buffer[3] = mod.W;
buffer[4] = greaterThan.X ? 1 : 0;
buffer[5] = greaterThan.Y ? 1 : 0;;
buffer[6] = greaterThan.Z ? 1 : 0;;
buffer[7] = greaterThan.W ? 1 : 0; ;
buffer[5] = greaterThan.Y ? 1 : 0;
buffer[6] = greaterThan.Z ? 1 : 0;
buffer[7] = greaterThan.W ? 1 : 0;
}
}
// See: https://github.com/Sergio0694/ComputeSharp/issues/259
[CombinatorialTestMethod]
[AllDevices]
public void ConstantsInShaderConstantFields(Device device)
{
using ReadWriteBuffer<float> buffer = device.Get().AllocateReadWriteBuffer<float>(16);
device.Get().For(1, new ConstantsInShaderConstantFieldsShader(buffer));
float[] results = buffer.ToArray();
Assert.AreEqual(results[0], ConstantsInShaderConstantFieldsShader.rot_angle, 0.0001f);
Assert.AreEqual(results[1], ConstantsInShaderConstantFieldsShader.rot_angle2, 0.0001f);
Assert.AreEqual(results[2], (float)Math.Cos(ConstantsInShaderConstantFieldsShader.rot_angle), 0.0001f);
Assert.AreEqual(results[3], -(float)Math.Sin((float)(137.2 / 180.0 * Constants.PI2)), 0.0001f);
}
private static class Constants
{
public const float PI2 = (float)(Math.PI * 2);
}
[AutoConstructor]
internal readonly partial struct ConstantsInShaderConstantFieldsShader : IComputeShader
{
public const float rot_angle = (float)(137.2 / 180.0 * Math.PI);
public const float rot_angle2 = rot_angle + (float)Math.E * 100;
private static readonly float rot_11 = Hlsl.Cos(rot_angle);
private static readonly float rot_12 = -Hlsl.Sin((float)(137.2 / 180.0 * Constants.PI2));
public readonly ReadWriteBuffer<float> buffer;
public void Execute()
{
buffer[0] = rot_angle;
buffer[1] = rot_angle2;
buffer[2] = rot_11;
buffer[3] = rot_12;
}
}
}