Merge pull request #100 from Sergio0694/feature/matrix-types
Added HLSL matrix types
このコミットが含まれているのは:
64ファイル変更
+32985
-2132
検索条件に一致するファイルはありません
@@ -11,6 +11,7 @@
|
||||
- [Quick start](#quick-start)
|
||||
- [Capturing variables](#capturing-variables)
|
||||
- [GPU resource types](#gpu-resource-types)
|
||||
- [HLSL vector and matrix types](#hlsl-vector-and-matrix-types)
|
||||
- [HLSL intrinsics](#hlsl-intrinsics)
|
||||
- [Working with images](#working-with-images)
|
||||
- [Shader metaprogramming](#shader-metaprogramming)
|
||||
@@ -83,6 +84,8 @@ Shaders can store either GPU resources or custom values in their fields, so that
|
||||
|
||||
✅ HLSL types: `Bool`, `Bool2`, `Bool3`, `Bool4`, `Float2`, `Float3`, `Float4`, `Int2`, `Int3`, `Int4`, `UInt2`, `Uint3`, etc.
|
||||
|
||||
✅ HLSL matrix types: `Float2x2`, `Float3x3`, `Float3x4`, `Float4x4`, `Float1x4`, `Float4x`, etc.
|
||||
|
||||
✅ Custom `struct` types containing any of the types above, as well as other valid custom `struct` types
|
||||
|
||||
## GPU resource types
|
||||
@@ -102,6 +105,27 @@ There are a number of extension APIs for the `GraphicsDevice` class that can be
|
||||
|
||||
> **NOTE:** although the various APIs to allocate buffers are simply generic methods with a `T : unmanaged` constrain, they should only be used with C# types that are supported (see notes above). Additionally, the `bool` type should not be used in buffers due to C#/HLSL differences: use the `Bool` type instead (or just an `int` buffer).
|
||||
|
||||
## HLSL vector and matrix types
|
||||
|
||||
As mentioned in the [Capturing variables](#capturing-variables) paragraph, **ComputeSharp** also exposes matrix types that can be used in compute shaders. These values store individual component values in row-major order (for consistency with .NET arrays) and can be indexed in several ways just like with HLSL vector types. One noticeable difference compared to HLSL vector types though is the lack of explicit properties to extract swizzled vectors (eg. `Float4.XZY` returns a `Float3` value with the `X`, `Z` and `Y` components). Due to the number of possible combinations being simply too high in the case of matrix types (eg. `Float4x4` alone would have had over 160k properties), the ability to extract swizzled vectors ([see here](https://docs.microsoft.com/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math)) can be achieved through the use of a special indexer property and values from the `MatrixIndex` type. Here is how it can be used:
|
||||
|
||||
```csharp
|
||||
Float4x4 matrix = default;
|
||||
|
||||
// Standard indexer for rows and individual items
|
||||
Float4 row = matrix[0];
|
||||
float item = matrix[0][1];
|
||||
|
||||
// Swizzled indexers, which can be made less verbose to
|
||||
// write by adding this using static directive to the file
|
||||
using static ComputeSharp.MatrixIndex;
|
||||
|
||||
Float4 diagonal = matrix[M11, M22, M33, M44];
|
||||
Float4 vertices = matrix[M11, M14, M44, M41];
|
||||
```
|
||||
|
||||
Matrix types also include a number of built-in operators to work with vector types, and the `Hlsl` class detailed below (see [HLSL intrinsics](#hlsl-intrinsics)) also includes several overloads for the available methods to work on both matrix and vector types at the same time (eg. for row/matrix multiplication).
|
||||
|
||||
## HLSL intrinsics
|
||||
|
||||
**ComputeSharp** offers support for all HLSL intrinsics that can be used from compute shaders. These are special functions (usually representing mathematical operations) that are optimized on the GPU and can execute very efficiently. Some of them can be mapped automatically from methods in the `System.Math` type (such as `Math.Sin` and `Math.Cos`), but if you want to have direct access to all of them you can just use the methods in the `Hlsl` class from a compute shader, and **ComputeSharp** will rewrite all those calls to use the native intrinsics in the compute shaders where those are used.
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "5.0.100"
|
||||
"version": "5.0.x"
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,9 @@ namespace ComputeSharp.SwapChain
|
||||
new SwapChainApplication<ColorfulInfinity>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
|
||||
new SwapChainApplication<FractalTiling>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
|
||||
new SwapChainApplication<TwoTiledTruchet>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
|
||||
new SwapChainApplication<MengerJourney>(static (texture, time) => new(texture, (float)time.TotalSeconds))
|
||||
new SwapChainApplication<MengerJourney>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
|
||||
new SwapChainApplication<Octagrams>(static (texture, time) => new(texture, (float)time.TotalSeconds)),
|
||||
new SwapChainApplication<ProteanClouds>(static (texture, time) => new(texture, (float)time.TotalSeconds))
|
||||
};
|
||||
|
||||
static void Main()
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating an octagram animation, inspired by arabesque.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/tlVGDt"/>.
|
||||
/// <para>Created by whisky_shusuky.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct Octagrams : IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The target texture.
|
||||
/// </summary>
|
||||
public readonly IReadWriteTexture2D<Float4> texture;
|
||||
|
||||
/// <summary>
|
||||
/// The current time since the start of the application.
|
||||
/// </summary>
|
||||
public readonly float time;
|
||||
|
||||
private static Float2x2 Rotate(float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, s, -s, c);
|
||||
}
|
||||
|
||||
private static float Box(Float3 pos, float scale)
|
||||
{
|
||||
static float SDBox(Float3 p, Float3 b)
|
||||
{
|
||||
Float3 q = Hlsl.Abs(p) - b;
|
||||
|
||||
return Hlsl.Length(Hlsl.Max(q, 0.0f)) +
|
||||
Hlsl.Min(Hlsl.Max(q.X, Hlsl.Max(q.Y, q.Z)), 0.0f);
|
||||
}
|
||||
|
||||
pos *= scale;
|
||||
|
||||
float b = SDBox(pos, new(0.4f, 0.4f, 0.1f)) / 1.5f;
|
||||
|
||||
pos.XY *= 5.0f;
|
||||
pos.Y -= 3.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.75f));
|
||||
|
||||
return -b;
|
||||
}
|
||||
|
||||
private static float BoxSet(Float3 pos, float gTime)
|
||||
{
|
||||
Float3 pos_origin = pos;
|
||||
|
||||
pos = pos_origin;
|
||||
pos.Y += Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box1 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.Y -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box2 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.X += Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box3 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.X -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box4 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box5 = Box(pos, 0.5f) * 6.0f;
|
||||
|
||||
pos = pos_origin;
|
||||
|
||||
float box6 = Box(pos, 0.5f) * 6.0f;
|
||||
float result = Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(box1, box2), box3), box4), box5), box6);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Int2 resolution = new(texture.Width, texture.Height);
|
||||
Float2 p = ((Float2)ThreadIds.XY * 2.0f - resolution) / Hlsl.Min(resolution.X, resolution.Y);
|
||||
Float3 ro = new(0.0f, -0.2f, time * 4.0f);
|
||||
Float3 ray = Hlsl.Normalize(new Float3(p, 1.5f));
|
||||
|
||||
ray.XY = Hlsl.Mul(ray.XY, Rotate(Hlsl.Sin(time * 0.03f) * 5.0f));
|
||||
ray.YZ = Hlsl.Mul(ray.YZ, Rotate(Hlsl.Sin(time * 0.05f) * 0.2f));
|
||||
|
||||
float t = 0.1f;
|
||||
float ac = 0.0f;
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
static Float3 Mod(Float3 x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
Float3 pos = ro + ray * t;
|
||||
|
||||
pos = Mod(pos - 2.0f, 4.0f) - 2.0f;
|
||||
|
||||
float gTime = time - i * 0.01f;
|
||||
float d = BoxSet(pos, gTime);
|
||||
|
||||
d = Hlsl.Max(Hlsl.Abs(d), 0.01f);
|
||||
ac += Hlsl.Exp(-d * 23.0f);
|
||||
t += d * 0.55f;
|
||||
}
|
||||
|
||||
Float3 col = ac * 0.02f + new Float3(0.0f, 0.2f * Hlsl.Abs(Hlsl.Sin(time)), 0.5f + Hlsl.Sin(time) * 0.2f);
|
||||
Float4 color = new(col, 1.0f - t * (0.02f + 0.02f * Hlsl.Sin(time)));
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders
|
||||
{
|
||||
/// <summary>
|
||||
/// Fully procedural 3D animated volume with three evaluations per step (for shading).
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/3l23Rh"/>.
|
||||
/// <para>Created by nimitz (twitter: @stormoid).</para>
|
||||
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct ProteanClouds : IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The target texture.
|
||||
/// </summary>
|
||||
public readonly IReadWriteTexture2D<Float4> texture;
|
||||
|
||||
/// <summary>
|
||||
/// The current time Hlsl.Since the start of the application.
|
||||
/// </summary>
|
||||
public readonly float time;
|
||||
|
||||
private static Float2x2 Rotate(in float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, s, -s, c);
|
||||
}
|
||||
|
||||
private static float LinStep(in float mn, in float mx, in float x)
|
||||
{
|
||||
return Hlsl.Clamp((x - mn) / (mx - mn), 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
private static Float2 Disp(float t)
|
||||
{
|
||||
return new Float2(Hlsl.Sin(t * 0.22f) * 1.0f, Hlsl.Cos(t * 0.175f) * 1.0f) * 2.0f;
|
||||
}
|
||||
|
||||
private Float2 Map(Float3 p, float prm1, Float2 bsMo)
|
||||
{
|
||||
Float3 p2 = p;
|
||||
|
||||
p2.XY -= Disp(p.Z).XY;
|
||||
p.XY = Hlsl.Mul(p.XY, Rotate(Hlsl.Sin(p.Z + time) * (0.1f + prm1 * 0.05f) + time * 0.09f));
|
||||
|
||||
float cl = Hlsl.Dot(p2.XY, p2.XY);
|
||||
float d = 0.0f;
|
||||
|
||||
p *= 0.61f;
|
||||
|
||||
float z = 1.0f;
|
||||
float trk = 1.0f;
|
||||
float dspAmp = 0.1f + prm1 * 0.2f;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p += Hlsl.Sin(p.ZXY * 0.75f * trk + time * trk * 0.8f) * dspAmp;
|
||||
d -= Hlsl.Abs(Hlsl.Dot(Hlsl.Cos(p), Hlsl.Sin(p.YZX)) * z);
|
||||
z *= 0.57f;
|
||||
trk *= 1.4f;
|
||||
|
||||
Float3x3 m3 = new Float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f;
|
||||
|
||||
p = Hlsl.Mul(p, m3);
|
||||
}
|
||||
|
||||
d = Hlsl.Abs(d + prm1 * 3.0f) + prm1 * 0.3f - 2.5f + bsMo.Y;
|
||||
|
||||
return new Float2(d + cl * 0.2f + 0.25f, cl);
|
||||
}
|
||||
|
||||
private Float4 Render(Float3 ro, Float3 rd, float time, float prm1, Float2 bsMo)
|
||||
{
|
||||
Float4 rez = 0;
|
||||
float t = 1.5f;
|
||||
float fogT = 0;
|
||||
|
||||
for (int i = 0; i < 130; i++)
|
||||
{
|
||||
if (rez.A > 0.99f) break;
|
||||
|
||||
Float3 pos = ro + t * rd;
|
||||
Float2 mpv = Map(pos, prm1, bsMo);
|
||||
float den = Hlsl.Clamp(mpv.X - 0.3f, 0.0f, 1.0f) * 1.12f;
|
||||
float dn = Hlsl.Clamp((mpv.X + 2.0f), 0.0f, 3.0f);
|
||||
Float4 col = 0f;
|
||||
|
||||
if (mpv.X > 0.6f)
|
||||
{
|
||||
|
||||
col = new Float4(Hlsl.Sin(new Float3(5.0f, 0.4f, 0.2f) + mpv.Y * 0.1f + Hlsl.Sin(pos.Z * 0.4f) * 0.5f + 1.8f) * 0.5f + 0.5f, 0.08f);
|
||||
col *= den * den * den;
|
||||
col.RGB *= LinStep(4.0f, -2.5f, mpv.X) * 2.3f;
|
||||
|
||||
float dif = Hlsl.Clamp((den - Map(pos + 0.8f, prm1, bsMo).X) / 9.0f, 0.001f, 1.0f);
|
||||
|
||||
dif += Hlsl.Clamp((den - Map(pos + 0.35f, prm1, bsMo).X) / 2.5f, 0.001f, 1.0f);
|
||||
col.XYZ *= den * (new Float3(0.005f, 0.045f, 0.075f) + 1.5f * new Float3(0.033f, 0.07f, 0.03f) * dif);
|
||||
}
|
||||
|
||||
float fogC = Hlsl.Exp(t * 0.2f - 2.2f);
|
||||
|
||||
col += new Float4(0.06f, 0.11f, 0.11f, 0.1f) * Hlsl.Clamp(fogC - fogT, 0.0f, 1.0f);
|
||||
fogT = fogC;
|
||||
rez += col * (1.0f - rez.A);
|
||||
t += Hlsl.Clamp(0.5f - dn * dn * 0.05f, 0.09f, 0.3f);
|
||||
}
|
||||
|
||||
return Hlsl.Clamp(rez, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
private static float GetSaturation(Float3 c)
|
||||
{
|
||||
float mi = Hlsl.Min(Hlsl.Min(c.X, c.Y), c.Z);
|
||||
float ma = Hlsl.Max(Hlsl.Max(c.X, c.Y), c.Z);
|
||||
|
||||
return (ma - mi) / (ma + (float)1e-7);
|
||||
}
|
||||
|
||||
private static Float3 ILerp(in Float3 a, in Float3 b, in float x)
|
||||
{
|
||||
Float3 ic = Hlsl.Lerp(a, b, x) + new Float3((float)1e-6, 0.0f, 0.0f);
|
||||
float sd = Hlsl.Abs(GetSaturation(ic) - Hlsl.Lerp(GetSaturation(a), GetSaturation(b), x));
|
||||
Float3 dir = Hlsl.Normalize(new Float3(2.0f * ic.X - ic.Y - ic.Z, 2.0f * ic.Y - ic.X - ic.Z, 2.0f * ic.Z - ic.Y - ic.X));
|
||||
float lgt = Hlsl.Dot(1.0f, ic);
|
||||
float ff = Hlsl.Dot(dir, Hlsl.Normalize(ic));
|
||||
|
||||
ic += 1.5f * dir * sd * ff * lgt;
|
||||
|
||||
return Hlsl.Clamp(ic, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Int2 resolution = new(texture.Width, texture.Height);
|
||||
Float2 q = (Float2)ThreadIds.XY / resolution;
|
||||
Float2 p = (ThreadIds.XY - 0.5f * (Float2)resolution) / resolution.Y;
|
||||
Float2 bsMo = -0.5f * (Float2)resolution / resolution.Y;
|
||||
float scaledTime = time * 3.0f;
|
||||
Float3 ro = new(0, 0, scaledTime);
|
||||
|
||||
ro += new Float3(Hlsl.Sin(time) * 0.5f, Hlsl.Sin(time * 1.0f) * 0.0f, 0);
|
||||
|
||||
float dspAmp = 0.85f;
|
||||
|
||||
ro.XY += Disp(ro.Z) * dspAmp;
|
||||
|
||||
float tgtDst = 3.5f;
|
||||
Float3 target = Hlsl.Normalize(ro - new Float3(Disp(scaledTime + tgtDst) * dspAmp, scaledTime + tgtDst));
|
||||
|
||||
ro.X -= bsMo.X * 2.0f;
|
||||
|
||||
Float3 rightdir = Hlsl.Normalize(Hlsl.Cross(target, new Float3(0, 1, 0)));
|
||||
Float3 updir = Hlsl.Normalize(Hlsl.Cross(rightdir, target));
|
||||
|
||||
rightdir = Hlsl.Normalize(Hlsl.Cross(updir, target));
|
||||
|
||||
Float3 rd = Hlsl.Normalize((p.X * rightdir + p.Y * updir) * 1.0f - target);
|
||||
|
||||
rd.XY = Hlsl.Mul(rd.XY, Rotate(-Disp(scaledTime + 3.5f).X * 0.2f + bsMo.X));
|
||||
|
||||
float prm1 = Hlsl.SmoothStep(-0.4f, 0.4f, Hlsl.Sin(time * 0.3f));
|
||||
Float4 scn = Render(ro, rd, scaledTime, prm1, bsMo);
|
||||
Float3 col = scn.RGB;
|
||||
|
||||
col = ILerp(col.BGR, col.RGB, Hlsl.Clamp(1.0f - prm1, 0.05f, 1.0f));
|
||||
col = Hlsl.Pow(col, new Float3(0.55f, 0.65f, 0.6f)) * new Float3(1.0f, 0.97f, 0.9f);
|
||||
col *= Hlsl.Pow(16.0f * q.X * q.Y * (1.0f - q.X) * (1.0f - q.Y), 0.12f) * 0.7f + 0.3f;
|
||||
|
||||
texture[ThreadIds.XY] = new Float4(col, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,4 +45,5 @@ CMPS0037 | ComputeSharp.SourceGenerators.AutoConstructorGenerator | Warning | Au
|
||||
CMPS0038 | ComputeSharp.SourceGenerators.IComputeShaderDataLoaderGenerator | Warning | IComputeShaderDataLoaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0039 | ComputeSharp.SourceGenerators.IComputeShaderHashCodeGenerator | Warning | IComputeShaderHashCodeGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0040 | ComputeSharp.SourceGenerators.IComputeShaderSourceGenerator | Warning | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
@@ -26,8 +26,9 @@
|
||||
<Compile Include="..\ComputeSharp\Core\Extensions\MethodInfoExtensions.cs" Link="Core\Extensions\MethodInfoExtensions.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Helpers\AlignmentHelper.cs" Link="Core\Helpers\AlignmentHelper.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Interfaces\IComputeShader.cs" Link="Core\Interfaces\IComputeShader.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Intrinsics\Attributes\PreserveMemberNameAttribute.cs" Link="Core\Intrinsics\Attributes\PreserveMemberNameAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" Link="Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Intrinsics\Hlsl.cs" Link="Core\Intrinsics\Hlsl.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Intrinsics\Hlsl.g.cs" Link="Core\Intrinsics\Hlsl.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool.cs" Link="Core\Primitives\Bool\Bool.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool2.cs" Link="Core\Primitives\Bool\Bool2.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool3.cs" Link="Core\Primitives\Bool\Bool3.cs" />
|
||||
@@ -35,30 +36,36 @@
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool2.g.cs" Link="Core\Primitives\Bool\Bool2.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool3.g.cs" Link="Core\Primitives\Bool\Bool3.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\Bool4.g.cs" Link="Core\Primitives\Bool\Bool4.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Bool\BoolMxN.g.cs" Link="Core\Primitives\Bool\BoolMxN.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double2.cs" Link="Core\Primitives\Double\Double2.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double3.cs" Link="Core\Primitives\Double\Double3.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double4.cs" Link="Core\Primitives\Double\Double4.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double2.g.cs" Link="Core\Primitives\Double\Double2.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double3.g.cs" Link="Core\Primitives\Double\Double3.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\Double4.g.cs" Link="Core\Primitives\Double\Double4.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Double\DoubleMxN.g.cs" Link="Core\Primitives\Double\DoubleMxN.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float2.cs" Link="Core\Primitives\Float\Float2.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float3.cs" Link="Core\Primitives\Float\Float3.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float4.cs" Link="Core\Primitives\Float\Float4.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float2.g.cs" Link="Core\Primitives\Float\Float2.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float3.g.cs" Link="Core\Primitives\Float\Float3.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\Float4.g.cs" Link="Core\Primitives\Float\Float4.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Float\FloatMxN.g.cs" Link="Core\Primitives\Float\FloatMxN.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int2.cs" Link="Core\Primitives\Int\Int2.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int3.cs" Link="Core\Primitives\Int\Int3.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int4.cs" Link="Core\Primitives\Int\Int4.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int2.g.cs" Link="Core\Primitives\Int\Int2.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int3.g.cs" Link="Core\Primitives\Int\Int3.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\Int4.g.cs" Link="Core\Primitives\Int\Int4.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\Int\IntMxN.g.cs" Link="Core\Primitives\Int\IntMxN.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt2.cs" Link="Core\Primitives\UInt\UInt2.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt3.cs" Link="Core\Primitives\UInt\UInt3.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt4.cs" Link="Core\Primitives\UInt\UInt4.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt2.g.cs" Link="Core\Primitives\UInt\UInt2.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt3.g.cs" Link="Core\Primitives\UInt\UInt3.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UInt4.g.cs" Link="Core\Primitives\UInt\UInt4.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\UInt\UIntMxN.g.cs" Link="Core\Primitives\UInt\UIntMxN.g.cs" />
|
||||
<Compile Include="..\ComputeSharp\Core\Primitives\MatrixIndex.cs" Link="Core\Primitives\MatrixIndex.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -587,5 +587,18 @@ namespace ComputeSharp.SourceGenerators.Diagnostics
|
||||
isEnabledByDefault: true,
|
||||
description: $"The {nameof(ShaderMethodSourceGenerator)} generator encountered an error while processing a method, and using it in a shader might result in a runtime crash. Please report this issue at https://github.com/Sergio0694/ComputeSharp/issues.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for an incorrect matrix swizzling property argument.
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor NonConstantMatrixSwizzledIndex = new(
|
||||
id: "CMPS0042",
|
||||
title: "Non constant matrix swizzled property argument",
|
||||
messageFormat: "The arguments in a swizzled indexer for a matrix type must be compile-time constants",
|
||||
category: typeof(ShaderMethodSourceGenerator).FullName,
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "The arguments in a swizzled indexer for a matrix type must be compile-time constants.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,17 @@ namespace ComputeSharp.SourceGenerators.Extensions
|
||||
return $"{declaringTypeName}.{symbol.Name}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="IFieldSymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="IFieldSymbol"/> instance.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
|
||||
[Pure]
|
||||
public static string GetFullMetadataName(this IFieldSymbol symbol)
|
||||
{
|
||||
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a valid filename for a target symbol and generator type.
|
||||
/// </summary>
|
||||
|
||||
@@ -160,14 +160,14 @@ namespace ComputeSharp.SourceGenerators
|
||||
resourceOffset = 0,
|
||||
rawDataOffset = sizeof(int) * 3;
|
||||
|
||||
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
|
||||
foreach (
|
||||
IFieldSymbol fieldSymbol in
|
||||
from fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>()
|
||||
where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } &&
|
||||
!fieldSymbol.IsConst
|
||||
select fieldSymbol)
|
||||
{
|
||||
if (fieldSymbol.Type is not INamedTypeSymbol { IsStatic: false } typeSymbol)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string typeName = typeSymbol.GetFullMetadataName();
|
||||
string typeName = fieldSymbol.Type.GetFullMetadataName();
|
||||
|
||||
if (HlslKnownTypes.IsTypedResourceType(typeName))
|
||||
{
|
||||
@@ -177,25 +177,53 @@ namespace ComputeSharp.SourceGenerators
|
||||
ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"),
|
||||
ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)"))));
|
||||
}
|
||||
else if (HlslKnownTypes.IsScalarOrVectorType(typeName))
|
||||
else if (HlslKnownTypes.IsKnownHlslType(typeName))
|
||||
{
|
||||
if (fieldSymbol.IsConst) continue;
|
||||
|
||||
// For scalar, vector and linear matrix types, serialize the value normally.
|
||||
// Only the initial alignment needs to be considered, while data is packed.
|
||||
var (fieldSize, fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
|
||||
|
||||
// Calculate the right offset with 16-bytes padding (HLSL constant buffer).
|
||||
// Since we're in a constant buffer, we need to both pad the starting offset
|
||||
// to be aligned to the packing size of the type, and also to align the initial
|
||||
// offset to ensure that values do not cross 16 bytes boundaries either.
|
||||
rawDataOffset = AlignmentHelper.AlignToBoundary(
|
||||
AlignmentHelper.Pad(rawDataOffset, fieldPack),
|
||||
fieldSize,
|
||||
16);
|
||||
// Check if the current type is a matrix type with more than one row. In this
|
||||
// case, each row is aligned as if it was a separate array, so the start of
|
||||
// each row needs to be at a multiple of 16 bytes (a float4 register).
|
||||
if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out string? elementName, out int rows, out int columns))
|
||||
{
|
||||
string
|
||||
rowTypeName = $"ComputeSharp.{elementName}{columns}",
|
||||
rowLocalName = $"__{fieldSymbol.Name}__row0";
|
||||
|
||||
statements.Add(ExpressionStatement(
|
||||
ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})")));
|
||||
statements.Add(ParseStatement($"ref {rowTypeName} {rowLocalName} = ref Unsafe.As<{typeName}, {rowTypeName}>(ref Unsafe.AsRef(in shader.{fieldSymbol.Name}));"));
|
||||
|
||||
rawDataOffset += fieldSize;
|
||||
// Generate the loading code for each individual row, with proper alignment.
|
||||
// This will result in the following (assuming Float2x3 m):
|
||||
//
|
||||
// ref Float3 __m__row0 = ref Unsafe.As<Float2x3, Float3>(ref Unsafe.AsRef(in shader.m));
|
||||
// Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, rawDataOffset), Unsafe.Add(ref __m__row0, 0));
|
||||
// Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, rawDataOffset + 16), Unsafe.Add(ref __m__row0, 1));
|
||||
for (int j = 0; j < rows; j++)
|
||||
{
|
||||
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
|
||||
|
||||
statements.Add(ParseStatement($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), Unsafe.Add(ref {rowLocalName}, {j}));"));
|
||||
|
||||
rawDataOffset += fieldPack * columns;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Calculate the right offset with 16-bytes padding (HLSL constant buffer).
|
||||
// Since we're in a constant buffer, we need to both pad the starting offset
|
||||
// to be aligned to the packing size of the type, and also to align the initial
|
||||
// offset to ensure that values do not cross 16 bytes boundaries either.
|
||||
rawDataOffset = AlignmentHelper.AlignToBoundary(
|
||||
AlignmentHelper.Pad(rawDataOffset, fieldPack),
|
||||
fieldSize,
|
||||
16);
|
||||
|
||||
statements.Add(ParseStatement($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name});"));
|
||||
|
||||
rawDataOffset += fieldSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,17 +42,11 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
knownKeywords.Add(nameof(WarpIds));
|
||||
|
||||
// HLSL intrinsics method names
|
||||
foreach (var method in
|
||||
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
|
||||
group method by method.Name
|
||||
into groups
|
||||
select (Name: groups.Key, MethodInfo: groups.First()))
|
||||
foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
string hlslName = method.MethodInfo.GetCustomAttribute<PreserveMemberNameAttribute>() != null
|
||||
? method.Name
|
||||
: method.Name.ToLowerInvariant();
|
||||
string name = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
|
||||
knownKeywords.Add(hlslName);
|
||||
knownKeywords.Add(name);
|
||||
}
|
||||
|
||||
return knownKeywords;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
{
|
||||
@@ -13,9 +15,9 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
internal static class HlslKnownMembers
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known indexers to HLSL vector type names.
|
||||
/// The mapping of supported known indexers to HLSL resource type names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownIndexers = new Dictionary<string, string>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownResourceIndexers = new Dictionary<string, string>
|
||||
{
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
@@ -183,13 +185,27 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
|
||||
// Programmatically load mappings for the instance members of the HLSL vector types
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.HlslMappedVectorTypes
|
||||
from type in HlslKnownTypes.KnownVectorTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}");
|
||||
}
|
||||
|
||||
// Load mappings for the matrix properties as well
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.KnownMatrixTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where Regex.IsMatch(property.Name, "^M[1-4]{2}$")
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
char
|
||||
row = (char)(item.Property.Name[1] - 1),
|
||||
column = (char)(item.Property.Name[2] - 1);
|
||||
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"_m{row}{column}");
|
||||
}
|
||||
|
||||
// Store GroupIds.Index for a quicker comparison afterwards
|
||||
PropertyInfo groupindexProperty = typeof(GroupIds).GetProperty(nameof(GroupIds.Index), BindingFlags.Static | BindingFlags.Public);
|
||||
|
||||
@@ -236,6 +252,66 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
return knownMembers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndexers = BuildKnownMatrixIndexers();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indexer properties.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndexers()
|
||||
{
|
||||
return (
|
||||
from type in Assembly.GetExecutingAssembly().ExportedTypes
|
||||
where Regex.IsMatch(type.FullName, @"ComputeSharp\.(Bool|Double|Float|Int|UInt)[1-4]x[1-4]")
|
||||
from property in type.GetProperties()
|
||||
let indices = property.GetIndexParameters()
|
||||
where indices.Length > 0 &&
|
||||
indices.All(static index => index.ParameterType == typeof(MatrixIndex))
|
||||
let metadataName = $"{type.FullName}.this[{string.Join(", ", indices.Select(index => index.ParameterType))}]"
|
||||
select metadataName).ToImmutableHashSet();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndices = BuildKnownMatrixIndices();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indices.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndices()
|
||||
{
|
||||
return (
|
||||
from name in Enum.GetNames(typeof(MatrixIndex))
|
||||
select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled indexer.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled indexer.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndexer(string name)
|
||||
{
|
||||
return KnownMatrixIndexers.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled index.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled index.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndex(string name)
|
||||
{
|
||||
return KnownMatrixIndices.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible member name for the input member name.
|
||||
/// </summary>
|
||||
@@ -249,15 +325,15 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name.
|
||||
/// Tries to get the mapped HLSL-compatible indexer resource type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedIndexerTypeName(string name, out string? mapped)
|
||||
public static bool TryGetMappedResourceIndexerTypeName(string name, out string? mapped)
|
||||
{
|
||||
return KnownIndexers.TryGetValue(name, out mapped);
|
||||
return KnownResourceIndexers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -107,14 +107,12 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
|
||||
group method by method.Name
|
||||
into groups
|
||||
select (Name: groups.Key, MethodInfo: groups.First()))
|
||||
select groups.First())
|
||||
{
|
||||
// Check whether the current method should be translated with the original name
|
||||
// or with the lowercase version. This is needed because all C# methods are exposed
|
||||
// with the upper camel case format, while HLSL intrinsics use multiple different formats.
|
||||
string hlslName = method.MethodInfo.GetCustomAttribute<PreserveMemberNameAttribute>() != null
|
||||
? method.Name
|
||||
: method.Name.ToLowerInvariant();
|
||||
string hlslName = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
{
|
||||
@@ -12,38 +15,38 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
/// <summary>
|
||||
/// The mapping of supported known sizes to HLSL type names.
|
||||
/// </summary>
|
||||
private static IReadOnlyDictionary<string, (int, int)> KnownSizes = new Dictionary<string, (int, int)>
|
||||
private static IReadOnlyDictionary<string, (int, int)> KnownSizes = BuildKnownSizesMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known type sizes and alignments.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, (int, int)> BuildKnownSizesMap()
|
||||
{
|
||||
[typeof(bool).FullName] = (4, 4),
|
||||
[typeof(int).FullName] = (4, 4),
|
||||
[typeof(int).FullName] = (4, 4),
|
||||
[typeof(float).FullName] = (4, 4),
|
||||
[typeof(double).FullName] = (8, 8),
|
||||
Dictionary<string, (int, int)> knownSizes = new()
|
||||
{
|
||||
[typeof(bool).FullName] = (4, 4),
|
||||
[typeof(int).FullName] = (4, 4),
|
||||
[typeof(int).FullName] = (4, 4),
|
||||
[typeof(float).FullName] = (4, 4),
|
||||
[typeof(double).FullName] = (8, 8),
|
||||
|
||||
[typeof(Vector2).FullName] = (8, 4),
|
||||
[typeof(Vector3).FullName] = (12, 4),
|
||||
[typeof(Vector4).FullName] = (16, 4),
|
||||
[typeof(Vector2).FullName] = (8, 4),
|
||||
[typeof(Vector3).FullName] = (12, 4),
|
||||
[typeof(Vector4).FullName] = (16, 4)
|
||||
};
|
||||
|
||||
[typeof(Bool2).FullName] = (8, 4),
|
||||
[typeof(Bool3).FullName] = (12, 4),
|
||||
[typeof(Bool4).FullName] = (16, 4),
|
||||
foreach (
|
||||
var type in
|
||||
from type in Assembly.GetExecutingAssembly().ExportedTypes
|
||||
where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)")
|
||||
select type)
|
||||
{
|
||||
knownSizes.Add(type.FullName, (type.StructLayoutAttribute.Size, type.StructLayoutAttribute.Pack));
|
||||
}
|
||||
|
||||
[typeof(Int2).FullName] = (8, 4),
|
||||
[typeof(Int3).FullName] = (12, 4),
|
||||
[typeof(Int4).FullName] = (16, 4),
|
||||
|
||||
[typeof(UInt2).FullName] = (8, 4),
|
||||
[typeof(UInt3).FullName] = (12, 4),
|
||||
[typeof(UInt4).FullName] = (16, 4),
|
||||
|
||||
[typeof(Float2).FullName] = (8, 4),
|
||||
[typeof(Float3).FullName] = (12, 4),
|
||||
[typeof(Float4).FullName] = (16, 4),
|
||||
|
||||
[typeof(Double2).FullName] = (16, 8),
|
||||
[typeof(Double3).FullName] = (24, 8),
|
||||
[typeof(Double3).FullName] = (32, 8)
|
||||
};
|
||||
return knownSizes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size and alignment info for a specified primitive type.
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
@@ -14,40 +15,19 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
internal static class HlslKnownTypes
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known types to HLSL types.
|
||||
/// Gets the known HLSL dispatch types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownTypes = new Dictionary<string, string>
|
||||
public static IReadOnlyCollection<Type> HlslDispatchTypes { get; } = new[]
|
||||
{
|
||||
[typeof(bool).FullName] = "bool",
|
||||
[typeof(Bool).FullName] = "bool",
|
||||
[typeof(Bool2).FullName] = "bool2",
|
||||
[typeof(Bool3).FullName] = "bool3",
|
||||
[typeof(Bool4).FullName] = "bool4",
|
||||
[typeof(int).FullName] = "int",
|
||||
[typeof(Int2).FullName] = "int2",
|
||||
[typeof(Int3).FullName] = "int3",
|
||||
[typeof(Int4).FullName] = "int4",
|
||||
[typeof(uint).FullName] = "uint",
|
||||
[typeof(UInt2).FullName] = "uint2",
|
||||
[typeof(UInt3).FullName] = "uint3",
|
||||
[typeof(UInt4).FullName] = "uint4",
|
||||
[typeof(float).FullName] = "float",
|
||||
[typeof(Float2).FullName] = "float2",
|
||||
[typeof(Float3).FullName] = "float3",
|
||||
[typeof(Float4).FullName] = "float4",
|
||||
[typeof(Vector2).FullName] = "float2",
|
||||
[typeof(Vector3).FullName] = "float3",
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(double).FullName] = "double",
|
||||
[typeof(Double2).FullName] = "double2",
|
||||
[typeof(Double3).FullName] = "double3",
|
||||
[typeof(Double4).FullName] = "double4"
|
||||
typeof(ThreadIds),
|
||||
typeof(GroupIds),
|
||||
typeof(WarpIds)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the known HLSL vector types available as mapped types.
|
||||
/// Gets the set of HLSL vector types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> HlslMappedVectorTypes { get; } = new[]
|
||||
public static IReadOnlyCollection<Type> KnownVectorTypes { get; } = new[]
|
||||
{
|
||||
typeof(Bool2), typeof(Bool3), typeof(Bool4),
|
||||
typeof(Int2), typeof(Int3), typeof(Int4),
|
||||
@@ -57,15 +37,70 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the known HLSL dispatch types.
|
||||
/// Gets the set of HLSL matrix types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> HlslDispatchTypes { get; } = new[]
|
||||
public static IReadOnlyCollection<Type> KnownMatrixTypes { get; } = new[]
|
||||
{
|
||||
typeof(ThreadIds),
|
||||
typeof(GroupIds),
|
||||
typeof(WarpIds)
|
||||
typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4),
|
||||
typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4),
|
||||
typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4),
|
||||
typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4),
|
||||
typeof(Int1x1), typeof(Int1x2), typeof(Int1x3), typeof(Int1x4),
|
||||
typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4),
|
||||
typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4),
|
||||
typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4),
|
||||
typeof(UInt1x1), typeof(UInt1x2), typeof(UInt1x3), typeof(UInt1x4),
|
||||
typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4),
|
||||
typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4),
|
||||
typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4),
|
||||
typeof(Float1x1), typeof(Float1x2), typeof(Float1x3), typeof(Float1x4),
|
||||
typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4),
|
||||
typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4),
|
||||
typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4),
|
||||
typeof(Double1x1), typeof(Double1x2), typeof(Double1x3), typeof(Double1x4),
|
||||
typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4),
|
||||
typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4),
|
||||
typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known types to HLSL types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownHlslTypes = BuildKnownHlslTypes();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known primitive types.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownHlslTypes()
|
||||
{
|
||||
Dictionary<string, string> knownTypes = new()
|
||||
{
|
||||
[typeof(bool).FullName] = "bool",
|
||||
[typeof(int).FullName] = "int",
|
||||
[typeof(uint).FullName] = "uint",
|
||||
[typeof(float).FullName] = "float",
|
||||
[typeof(Vector2).FullName] = "float2",
|
||||
[typeof(Vector3).FullName] = "float3",
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(double).FullName] = "double"
|
||||
};
|
||||
|
||||
// Add all the vector types
|
||||
foreach (var type in KnownVectorTypes)
|
||||
{
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
}
|
||||
|
||||
// Add all the matrix types
|
||||
foreach (var type in KnownMatrixTypes)
|
||||
{
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
}
|
||||
|
||||
return knownTypes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a structured buffer type.
|
||||
/// </summary>
|
||||
@@ -113,13 +148,64 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a typed resource type.
|
||||
/// Checks whether or not a given type name matches a known HLSL primitive type (scalar, vector or matrix).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
|
||||
public static bool IsScalarOrVectorType(string typeName)
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a scalar, vector or matrix type.</returns>
|
||||
public static bool IsKnownHlslType(string typeName)
|
||||
{
|
||||
return KnownTypes.ContainsKey(typeName);
|
||||
return KnownHlslTypes.ContainsKey(typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a vector type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a vector type.</returns>
|
||||
public static bool IsVectorType(string typeName)
|
||||
{
|
||||
return KnownVectorTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a matrix type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a matrix type.</returns>
|
||||
public static bool IsMatrixType(string typeName)
|
||||
{
|
||||
return KnownMatrixTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name is a non linear matrix type.
|
||||
/// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <param name="elementName">The element name of the matrix type.</param>
|
||||
/// <param name="rows">The number of rows in the matrix type.</param>
|
||||
/// <param name="columns">The number of columns in the matrix type.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a non linear matrix type.</returns>
|
||||
public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns)
|
||||
{
|
||||
if (KnownMatrixTypes.Any(type => type.FullName == typeName))
|
||||
{
|
||||
var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
elementName = match.Groups[1].Value;
|
||||
rows = int.Parse(match.Groups[2].Value);
|
||||
columns = int.Parse(match.Groups[3].Value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
elementName = null;
|
||||
rows = columns = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -142,7 +228,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName();
|
||||
|
||||
// If the current type is a custom type, format it as needed
|
||||
if (!KnownTypes.TryGetValue(genericArgumentName, out string? mapped))
|
||||
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mapped))
|
||||
{
|
||||
mapped = genericArgumentName.Replace(".", "__");
|
||||
}
|
||||
@@ -169,7 +255,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
};
|
||||
}
|
||||
|
||||
return KnownTypes[typeName];
|
||||
return KnownHlslTypes[typeName];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -182,7 +268,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
{
|
||||
string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
|
||||
|
||||
if (KnownTypes.TryGetValue(elementTypeName, out string? mapped))
|
||||
if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
|
||||
{
|
||||
return mapped;
|
||||
}
|
||||
@@ -197,7 +283,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
public static bool TryGetMappedName(string originalName, out string? mappedName)
|
||||
{
|
||||
return KnownTypes.TryGetValue(originalName, out mappedName);
|
||||
return KnownHlslTypes.TryGetValue(originalName, out mappedName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -210,7 +296,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
// Local function to recursively gather nested types
|
||||
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes)
|
||||
{
|
||||
if (KnownTypes.ContainsKey(type.GetFullMetadataName())) return;
|
||||
if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
|
||||
|
||||
if (!customTypes.Add(type)) return;
|
||||
|
||||
@@ -261,7 +347,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
|
||||
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
|
||||
|
||||
if (!KnownTypes.ContainsKey(fieldType.GetFullMetadataName()))
|
||||
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
|
||||
{
|
||||
_ = dependencies.Add(fieldType);
|
||||
}
|
||||
|
||||
@@ -240,7 +240,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
|
||||
}
|
||||
|
||||
return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList);
|
||||
// Add explicit casts for matrix constructors to help the overload resolution
|
||||
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType &&
|
||||
HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName()))
|
||||
{
|
||||
for (int i = 0; i < node.ArgumentList!.Arguments.Count; i++)
|
||||
{
|
||||
IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!;
|
||||
INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type;
|
||||
|
||||
updatedNode = updatedNode.ReplaceNode(
|
||||
updatedNode.ArgumentList!.Arguments[i].Expression,
|
||||
CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression));
|
||||
}
|
||||
}
|
||||
|
||||
return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList!);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -271,6 +286,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
return CastExpression(explicitType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
|
||||
}
|
||||
|
||||
// Add explicit casts like with the explicit object creation expressions above
|
||||
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType &&
|
||||
HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName()))
|
||||
{
|
||||
for (int i = 0; i < node.ArgumentList.Arguments.Count; i++)
|
||||
{
|
||||
IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!;
|
||||
INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type;
|
||||
|
||||
updatedNode = updatedNode.ReplaceNode(
|
||||
updatedNode.ArgumentList.Arguments[i].Expression,
|
||||
CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression));
|
||||
}
|
||||
}
|
||||
|
||||
return InvocationExpression(explicitType, updatedNode.ArgumentList);
|
||||
}
|
||||
|
||||
@@ -392,7 +422,9 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
this.semanticModel.GetOperation(node) is IMemberReferenceOperation operation)
|
||||
{
|
||||
// If the member access is a constant, track it and replace the tree with the processed constant name
|
||||
if (operation is IFieldReferenceOperation fieldOperation && fieldOperation.Field.IsConst)
|
||||
if (operation is IFieldReferenceOperation fieldOperation &&
|
||||
fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
this.constantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
@@ -557,12 +589,54 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
{
|
||||
var updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!;
|
||||
|
||||
if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation &&
|
||||
HlslKnownMembers.TryGetMappedIndexerTypeName(operation.Property.GetFullMetadataName(), out string? mapping))
|
||||
if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation)
|
||||
{
|
||||
var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments));
|
||||
string propertyName = operation.Property.GetFullMetadataName();
|
||||
|
||||
return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index))));
|
||||
// Rewrite texture resource indices taking vectors into individual indices as per HLSL spec.
|
||||
// For instance: texture[ThreadIds.XY] will be rewritten as texture[ThreadIds.X, ThreadIds.Y].
|
||||
if (HlslKnownMembers.TryGetMappedResourceIndexerTypeName(propertyName, out string? mapping))
|
||||
{
|
||||
var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments));
|
||||
|
||||
return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index))));
|
||||
}
|
||||
|
||||
// If the current property is a swizzled matrix indexer, ensure all the arguments are constants, and rewrite
|
||||
// the property access to the corresponding HLSL syntax. For instance, m[M11, M12] will become m._m00_m01.
|
||||
if (HlslKnownMembers.IsKnownMatrixIndexer(propertyName))
|
||||
{
|
||||
bool isValid = true;
|
||||
|
||||
// Validate the arguments
|
||||
foreach (ArgumentSyntax argument in node.ArgumentList.Arguments)
|
||||
{
|
||||
if (this.semanticModel.GetOperation(argument.Expression) is not IFieldReferenceOperation fieldReference ||
|
||||
!HlslKnownMembers.IsKnownMatrixIndex(fieldReference.Field.GetFullMetadataName()))
|
||||
{
|
||||
this.context.ReportDiagnostic(NonConstantMatrixSwizzledIndex, argument);
|
||||
|
||||
isValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isValid)
|
||||
{
|
||||
// Rewrite the indexer as a property access
|
||||
string hlslPropertyName = string.Join("",
|
||||
from argument in node.ArgumentList.Arguments
|
||||
let fieldReference = (IFieldReferenceOperation)this.semanticModel.GetOperation(argument.Expression)!
|
||||
let fieldName = fieldReference.Field.Name
|
||||
let row = (char)(fieldName[1] - 1)
|
||||
let column = (char)(fieldName[2] - 1)
|
||||
select $"_m{row}{column}");
|
||||
|
||||
return MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
updatedNode.Expression,
|
||||
IdentifierName(hlslPropertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return updatedNode;
|
||||
@@ -596,7 +670,9 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
{
|
||||
var updatedNode = (IdentifierNameSyntax)base.VisitIdentifierName(node)!;
|
||||
|
||||
if (this.semanticModel.GetOperation(node) is IFieldReferenceOperation operation && operation.Field.IsConst)
|
||||
if (this.semanticModel.GetOperation(node) is IFieldReferenceOperation operation &&
|
||||
operation.Field.IsConst &&
|
||||
operation.Type.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
this.constantDefinitions[operation.Field] = ((IFormattable)operation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Core\Intrinsics\Hlsl.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Hlsl.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Bool\Bool2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -45,6 +50,11 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Bool4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Bool\BoolMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>BoolMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Double\Double2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -60,6 +70,11 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Double4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Double\DoubleMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>DoubleMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Float\Float2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -75,6 +90,11 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Float4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Float\FloatMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>FloatMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Int\Int2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -90,6 +110,11 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Int4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\Int\IntMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>IntMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\UInt\UInt2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -105,9 +130,18 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UInt4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Core\Primitives\UInt\UIntMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UIntMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Core\Intrinsics\Hlsl.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Hlsl.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Bool\Bool2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Bool2.g.cs</LastGenOutput>
|
||||
@@ -120,6 +154,10 @@
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Bool4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Bool\BoolMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>BoolMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Double\Double2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Double2.g.cs</LastGenOutput>
|
||||
@@ -132,6 +170,10 @@
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Double4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Double\DoubleMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>DoubleMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Float\Float2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Float2.g.cs</LastGenOutput>
|
||||
@@ -144,6 +186,10 @@
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Float4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Float\FloatMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>FloatMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Int\Int2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Int2.g.cs</LastGenOutput>
|
||||
@@ -156,6 +202,10 @@
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Int4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\Int\IntMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>IntMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\UInt\UInt2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UInt2.g.cs</LastGenOutput>
|
||||
@@ -168,6 +218,10 @@
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UInt4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Core\Primitives\UInt\UIntMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UIntMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.Core.Intrinsics.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// An attribute indicating the native member name of a given HLSL intrinsic.
|
||||
/// </summary>
|
||||
internal sealed class HlslIntrinsicNameAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslIntrinsicNameAttribute"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the HLSL intrinsic.</param>
|
||||
public HlslIntrinsicNameAttribute(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the HLSL intrinsic.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.Core.Intrinsics.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// An attribute to indicate that a given member has the same name when mapped to HLSL.
|
||||
/// </summary>
|
||||
internal sealed class PreserveMemberNameAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,57 @@
|
||||
<#@include file="Hlsl.ttinclude" #>
|
||||
using System.Diagnostics.Contracts;
|
||||
using ComputeSharp.Core.Intrinsics.Attributes;
|
||||
using ComputeSharp.Exceptions;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Hlsl"/>
|
||||
public static partial class Hlsl
|
||||
{
|
||||
<#
|
||||
PushIndent(" ");
|
||||
PushIndent(" ");
|
||||
|
||||
bool isFirst = true;
|
||||
|
||||
foreach (var intrinsic in Intrinsics)
|
||||
{
|
||||
foreach (var overload in intrinsic.Overloads)
|
||||
{
|
||||
if (isFirst) isFirst = false;
|
||||
else WriteLine("");
|
||||
|
||||
WriteLine("/// <summary>");
|
||||
WriteLine($"/// {intrinsic.Summary}");
|
||||
WriteLine("/// </summary>");
|
||||
|
||||
foreach (var param in intrinsic.Parameters)
|
||||
{
|
||||
WriteLine($"/// <param name=\"{param.Name}\">{param.Info}</param>");
|
||||
}
|
||||
|
||||
if (intrinsic.Returns.Length > 0)
|
||||
{
|
||||
var returns = Regex.Replace(intrinsic.Returns, @"{(\w+)}", m => $"<paramref name=\"{m.Groups[1].Value}\"/>");
|
||||
|
||||
WriteLine($"/// <returns>{returns}</returns>");
|
||||
}
|
||||
|
||||
if (intrinsic.IsPure) WriteLine("[Pure]");
|
||||
WriteLine($"[HlslIntrinsicName(\"{intrinsic.HlslName}\")]");
|
||||
|
||||
Write($"public static {overload.Return} {intrinsic.Name}(");
|
||||
Write(string.Join(", ", intrinsic.Parameters.Zip(overload.Params, (a, b) => $"{(a.IsOut ? "out " : "")}{b} {a.Name}")));
|
||||
Write(") => throw new InvalidExecutionContextException(");
|
||||
|
||||
var parameters = string.Join(", ", overload.Params.Select(t => $"{{typeof({t})}}"));
|
||||
|
||||
WriteLine($"$\"{{typeof(Hlsl)}}.{{nameof({intrinsic.Name})}}({parameters})\");");
|
||||
}
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
PopIndent();
|
||||
#>
|
||||
}
|
||||
}
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -7,7 +7,7 @@ namespace ComputeSharp
|
||||
/// A <see langword="struct"/> that can be used in place of the <see cref="bool"/> type in HLSL shaders.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{Value}")]
|
||||
[StructLayout(LayoutKind.Explicit, Size = sizeof(int))]
|
||||
[StructLayout(LayoutKind.Explicit, Size = sizeof(int), Pack = 4)]
|
||||
public readonly struct Bool
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Bool2"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
|
||||
public partial struct Bool2
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Bool3"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
|
||||
public partial struct Bool3
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Bool4"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
|
||||
public partial struct Bool4
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,4 @@
|
||||
<#@include file="..\MatrixType.ttinclude" #>
|
||||
<#
|
||||
GenerateAllMatrixProperties("Bool", sizeof(int));
|
||||
#>
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Double2"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)]
|
||||
public partial struct Double2
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Double3"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 24)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 24, Pack = 8)]
|
||||
public partial struct Double3
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Double4"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 32)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 32, Pack = 8)]
|
||||
public partial struct Double4
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,4 @@
|
||||
<#@include file="..\MatrixType.ttinclude" #>
|
||||
<#
|
||||
GenerateAllMatrixProperties("Double", sizeof(double));
|
||||
#>
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Float2"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
|
||||
public partial struct Float2
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Float3"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
|
||||
public partial struct Float3
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Float4"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
|
||||
public partial struct Float4
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,4 @@
|
||||
<#@include file="..\MatrixType.ttinclude" #>
|
||||
<#
|
||||
GenerateAllMatrixProperties("Float", sizeof(float));
|
||||
#>
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Int2"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
|
||||
public partial struct Int2
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Int3"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
|
||||
public partial struct Int3
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="Int4"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
|
||||
public partial struct Int4
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,4 @@
|
||||
<#@include file="..\MatrixType.ttinclude" #>
|
||||
<#
|
||||
GenerateAllMatrixProperties("Int", sizeof(int));
|
||||
#>
|
||||
@@ -0,0 +1,97 @@
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A type indicating a base-1 index into a matrix type. This can be used to extract references to swizzled vectors within a given matrix value.
|
||||
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math"/>.
|
||||
/// <para>This type is meant to be used within a <see langword="using static"/> directive to simplify the code:</para>
|
||||
/// <para>
|
||||
/// <c>
|
||||
/// using static ComputeSharp.MatrixIndex;
|
||||
/// Float2x2 matrix = 0;
|
||||
/// Float2 row = matrix[M11, M12];
|
||||
/// </c>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public enum MatrixIndex
|
||||
{
|
||||
/// <summary>
|
||||
/// The first item in the first row.
|
||||
/// </summary>
|
||||
M11,
|
||||
|
||||
/// <summary>
|
||||
/// The second item in the first row.
|
||||
/// </summary>
|
||||
M12,
|
||||
|
||||
/// <summary>
|
||||
/// The third item in the first row.
|
||||
/// </summary>
|
||||
M13,
|
||||
|
||||
/// <summary>
|
||||
/// The fourth item in the first row.
|
||||
/// </summary>
|
||||
M14,
|
||||
|
||||
/// <summary>
|
||||
/// The first item in the second row.
|
||||
/// </summary>
|
||||
M21,
|
||||
|
||||
/// <summary>
|
||||
/// The second item in the second row.
|
||||
/// </summary>
|
||||
M22,
|
||||
|
||||
/// <summary>
|
||||
/// The third item in the second row.
|
||||
/// </summary>
|
||||
M23,
|
||||
|
||||
/// <summary>
|
||||
/// The fourth item in the second row.
|
||||
/// </summary>
|
||||
M24,
|
||||
|
||||
/// <summary>
|
||||
/// The first item in the third row.
|
||||
/// </summary>
|
||||
M31,
|
||||
|
||||
/// <summary>
|
||||
/// The second item in the third row.
|
||||
/// </summary>
|
||||
M32,
|
||||
|
||||
/// <summary>
|
||||
/// The third item in the third row.
|
||||
/// </summary>
|
||||
M33,
|
||||
|
||||
/// <summary>
|
||||
/// The fourth item in the third row.
|
||||
/// </summary>
|
||||
M34,
|
||||
|
||||
/// <summary>
|
||||
/// The first item in the fourth row.
|
||||
/// </summary>
|
||||
M41,
|
||||
|
||||
/// <summary>
|
||||
/// The second item in the fourth row.
|
||||
/// </summary>
|
||||
M42,
|
||||
|
||||
/// <summary>
|
||||
/// The third item in the fourth row.
|
||||
/// </summary>
|
||||
M43,
|
||||
|
||||
/// <summary>
|
||||
/// The fourth item in the fourth row.
|
||||
/// </summary>
|
||||
M44
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Text.RegularExpressions" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".g.cs"#>
|
||||
<#
|
||||
// Generates all matrix members and types for a given HLSL type
|
||||
void GenerateAllMatrixProperties(string typeName, int elementSize)
|
||||
{
|
||||
#>
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Exceptions;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
<#
|
||||
for (int rows = 1; rows <= 4; rows++)
|
||||
for (int columns = 1; columns <= 4; columns++)
|
||||
{
|
||||
if (rows > 1 || columns > 1) WriteLine("");
|
||||
|
||||
GenerateMatrixProperties(typeName, rows, columns, elementSize);
|
||||
}
|
||||
#>
|
||||
}
|
||||
<#
|
||||
}
|
||||
|
||||
// Generates all vector access properties for a specified type
|
||||
void GenerateMatrixProperties(string typeName, int rows, int columns, int elementSize)
|
||||
{
|
||||
string elementTypeName = typeName.ToLower();
|
||||
string fullTypeName = $"{typeName}{rows}x{columns}";
|
||||
string rowTypeName = columns > 1 ? $"{typeName}{columns}" : elementTypeName;
|
||||
List<string> fieldNames = new List<string>();
|
||||
#>
|
||||
/// <inheritdoc cref="<#=fullTypeName#>"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = <#=(elementSize * rows * columns)#>, Pack = <#=elementSize#>)]
|
||||
public partial struct <#=fullTypeName#>
|
||||
{
|
||||
<#
|
||||
PushIndent(" ");
|
||||
PushIndent(" ");
|
||||
|
||||
// Generate the private fields
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"[FieldOffset({((i - 1) * columns + j - 1) * elementSize})]");
|
||||
WriteLine($"private {elementTypeName} m{i}{j};");
|
||||
WriteLine("");
|
||||
|
||||
fieldNames.Add($"m{i}{j}");
|
||||
}
|
||||
|
||||
// Generate the elementwise constructor
|
||||
WriteLine("/// <summary>");
|
||||
WriteLine($"/// Creates a new <see cref=\"{fullTypeName}\"/> instance with the specified parameters.");
|
||||
WriteLine("/// </summary>");
|
||||
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"/// <param name=\"m{i}{j}\">The value to assign to the component at position [{i}, {j}].</param>");
|
||||
}
|
||||
|
||||
WriteLine($"public {fullTypeName}({string.Join(", ", fieldNames.Select(n => $"{elementTypeName} {n}"))})");
|
||||
WriteLine("{");
|
||||
PushIndent(" ");
|
||||
|
||||
foreach (string fieldName in fieldNames)
|
||||
{
|
||||
WriteLine($"this.{fieldName} = {fieldName};");
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
WriteLine("");
|
||||
|
||||
// Generate the row-wise constructor, if needed
|
||||
if (rows > 1 && columns > 1)
|
||||
{
|
||||
WriteLine("/// <summary>");
|
||||
WriteLine($"/// Creates a new <see cref=\"{fullTypeName}\"/> instance with the specified parameters.");
|
||||
WriteLine("/// </summary>");
|
||||
|
||||
for (int i = 1; i <= rows; i++)
|
||||
{
|
||||
WriteLine($"/// <param name=\"row{i}\">The value to assign to the row at position [{i}].</param>");
|
||||
}
|
||||
|
||||
Write($"public {fullTypeName}(");
|
||||
|
||||
for (int i = 1; i <= rows; i++)
|
||||
{
|
||||
if (i > 1) Write(", ");
|
||||
Write($"{rowTypeName} row{i}");
|
||||
}
|
||||
|
||||
WriteLine(")");
|
||||
WriteLine("{");
|
||||
PushIndent(" ");
|
||||
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"this.m{i}{j} = row{i}.{"XYZW"[j - 1]};");
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
PopIndent();
|
||||
PopIndent();
|
||||
#>
|
||||
/// <summary>
|
||||
/// Gets a reference to a specific row in the current <see cref="<#=fullTypeName#>"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="row">The row to access.</param>
|
||||
public ref <#=rowTypeName#> this[int row] => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}[int]");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a swizzled reference to a specific sequence of items in the current <see cref="<#=fullTypeName#>"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="xy0">The identifier of the first item to index.</param>
|
||||
/// <param name="xy1">The identifier of the second item to index.</param>
|
||||
public ref <#=typeName#>2 this[MatrixIndex xy0, MatrixIndex xy1] => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}[{nameof(MatrixIndex)}, {nameof(MatrixIndex)}]");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a swizzled reference to a specific sequence of items in the current <see cref="<#=fullTypeName#>"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="xy0">The identifier of the first item to index.</param>
|
||||
/// <param name="xy1">The identifier of the second item to index.</param>
|
||||
/// <param name="xy2">The identifier of the third item to index.</param>
|
||||
public ref <#=typeName#>3 this[MatrixIndex xy0, MatrixIndex xy1, MatrixIndex xy2] => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}[{nameof(MatrixIndex)}, {nameof(MatrixIndex)}, {nameof(MatrixIndex)}]");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a swizzled reference to a specific sequence of items in the current <see cref="<#=fullTypeName#>"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="xy0">The identifier of the first item to index.</param>
|
||||
/// <param name="xy1">The identifier of the second item to index.</param>
|
||||
/// <param name="xy2">The identifier of the third item to index.</param>
|
||||
/// <param name="xy3">The identifier of the fourth item to index.</param>
|
||||
public ref <#=typeName#>4 this[MatrixIndex xy0, MatrixIndex xy1, MatrixIndex xy2, MatrixIndex xy3] => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}[{nameof(MatrixIndex)}, {nameof(MatrixIndex)}, {nameof(MatrixIndex)}, {nameof(MatrixIndex)}]");
|
||||
<#
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="<#=elementTypeName#>"/> value representing the component at position [<#=i#>, <#=j#>].
|
||||
/// </summary>
|
||||
public ref <#=elementTypeName#> M<#=i#><#=j#> => throw new InvalidExecutionContextException($"{typeof(<#=fullTypeName#>)}.M<#=i#><#=j#>");
|
||||
<#
|
||||
}
|
||||
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="<#=fullTypeName#>"/> value with the same value for all its components.
|
||||
/// </summary>
|
||||
/// <param name="x">The value to use for the components of the new <see cref="<#=fullTypeName#>"/> instance.</param>
|
||||
public static implicit operator <#=fullTypeName#>(<#=elementTypeName#> x)
|
||||
<#
|
||||
// Complete the conversion operator above
|
||||
PushIndent(" ");
|
||||
PushIndent(" ");
|
||||
WriteLine("{");
|
||||
PushIndent(" ");
|
||||
WriteLine($"{fullTypeName} matrix;");
|
||||
WriteLine("");
|
||||
|
||||
for (int i = 1; i <= rows; i++)
|
||||
for (int j = 1; j <= columns; j++)
|
||||
{
|
||||
WriteLine($"matrix.m{i}{j} = x;");
|
||||
}
|
||||
|
||||
WriteLine("");
|
||||
WriteLine("return matrix;");
|
||||
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
PopIndent();
|
||||
PopIndent();
|
||||
|
||||
// Generate the negation operator
|
||||
if (elementTypeName != "Bool" && elementTypeName != "UInt")
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Negates a <see cref="<#=fullTypeName#>"/> value.
|
||||
/// </summary>
|
||||
/// <param name="matrix">The <see cref="<#=fullTypeName#>"/> value to negate.</param>
|
||||
public static <#=fullTypeName#> operator -(<#=fullTypeName#> matrix) => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}.-");
|
||||
<#
|
||||
}
|
||||
|
||||
// Generate the numerical operators
|
||||
if (elementTypeName != "Bool")
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Sums two <see cref="<#=fullTypeName#>"/> values.
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="<#=fullTypeName#>"/> value to sum.</param>
|
||||
/// <param name="right">The second <see cref="<#=fullTypeName#>"/> value to sum.</param>
|
||||
public static <#=fullTypeName#> operator +(<#=fullTypeName#> left, <#=fullTypeName#> right) => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}.+");
|
||||
|
||||
/// <summary>
|
||||
/// Divides two <see cref="<#=fullTypeName#>"/> values (elementwise division).
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="<#=fullTypeName#>"/> value to divide.</param>
|
||||
/// <param name="right">The second <see cref="<#=fullTypeName#>"/> value to divide.</param>
|
||||
public static <#=fullTypeName#> operator /(<#=fullTypeName#> left, <#=fullTypeName#> right) => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}./");
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two <see cref="<#=fullTypeName#>"/> values (elementwise product).
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="<#=fullTypeName#>"/> value to multiply.</param>
|
||||
/// <param name="right">The second <see cref="<#=fullTypeName#>"/> value to multiply.</param>
|
||||
public static <#=fullTypeName#> operator *(<#=fullTypeName#> left, <#=fullTypeName#> right) => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}.*");
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts two <see cref="<#=fullTypeName#>"/> values.
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="<#=fullTypeName#>"/> value to subtract.</param>
|
||||
/// <param name="right">The second <see cref="<#=fullTypeName#>"/> value to subtract.</param>
|
||||
public static <#=fullTypeName#> operator -(<#=fullTypeName#> left, <#=fullTypeName#> right) => throw new InvalidExecutionContextException($"{nameof(<#=fullTypeName#>)}.-");
|
||||
<#
|
||||
}
|
||||
|
||||
// Generate the casting operators for row/column vectors
|
||||
if (rows == 1 && columns > 1)
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Casts a <see cref="<#=typeName#><#=columns#>"/> value to a <see cref="<#=fullTypeName#>"/> one.
|
||||
/// </summary>
|
||||
/// <param name="vector">The input <see cref="<#=typeName#><#=columns#>"/> value to cast.</param>
|
||||
public static implicit operator <#=fullTypeName#>(<#=typeName#><#=columns#> vector) => throw new InvalidExecutionContextException($"{typeof(<#=fullTypeName#>)}.{nameof(<#=fullTypeName#>)}({nameof(<#=typeName#><#=columns#>)})");
|
||||
<#
|
||||
}
|
||||
|
||||
if (columns == 1 && rows > 1)
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Casts a <see cref="<#=fullTypeName#>"/> value to a <see cref="<#=typeName#><#=rows#>"/> one.
|
||||
/// </summary>
|
||||
/// <param name="matrix">The input <see cref="<#=fullTypeName#>"/> value to cast.</param>
|
||||
public static implicit operator <#=typeName#><#=rows#>(<#=fullTypeName#> matrix) => throw new InvalidExecutionContextException($"{typeof(<#=fullTypeName#>)}.{nameof(<#=typeName#><#=rows#>)}({nameof(<#=fullTypeName#>)})");
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
<#
|
||||
}
|
||||
#>
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="UInt2"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
|
||||
public partial struct UInt2
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="UInt3"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
|
||||
public partial struct UInt3
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
@@ -4,7 +4,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="UInt4"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
|
||||
public partial struct UInt4
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,4 @@
|
||||
<#@include file="..\MatrixType.ttinclude" #>
|
||||
<#
|
||||
GenerateAllMatrixProperties("UInt", sizeof(uint));
|
||||
#>
|
||||
@@ -72,7 +72,7 @@ using ComputeSharp.Exceptions;
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <inheritdoc cref="<#=typeName#>"/>
|
||||
[StructLayout(LayoutKind.Explicit, Size = <#=(elementSize * i)#>)]
|
||||
[StructLayout(LayoutKind.Explicit, Size = <#=(elementSize * i)#>, Pack = <#=elementSize#>)]
|
||||
public partial struct <#=typeName#>
|
||||
{
|
||||
<#
|
||||
|
||||
@@ -558,7 +558,7 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
public static unsafe TFeature CheckFeatureSupport<TFeature>(this ref ID3D12Device d3D12Device, D3D12_FEATURE d3D12Feature)
|
||||
where TFeature : unmanaged
|
||||
{
|
||||
TFeature feature;
|
||||
TFeature feature = default;
|
||||
|
||||
d3D12Device.CheckFeatureSupport(d3D12Feature, &feature, (uint)sizeof(TFeature)).Assert();
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.SourceGenerators")]
|
||||
[module: SkipLocalsInit]
|
||||
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")]
|
||||
@@ -10,7 +10,10 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
/// </summary>
|
||||
internal static class HlslKnownTypes
|
||||
{
|
||||
private static HashSet<Type> _HlslMappedVectorTypes { get; } = new HashSet<Type>(new[]
|
||||
/// <summary>
|
||||
/// The set of HLSL vector types.
|
||||
/// </summary>
|
||||
private static HashSet<Type> HlslMappedVectorTypes { get; } = new(new[]
|
||||
{
|
||||
typeof(Bool2), typeof(Bool3), typeof(Bool4),
|
||||
typeof(Int2), typeof(Int3), typeof(Int4),
|
||||
@@ -19,6 +22,33 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
typeof(Double2), typeof(Double3), typeof(Double4)
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// The set of HLSL matrix types.
|
||||
/// </summary>
|
||||
private static HashSet<Type> HlslMappedMatrixTypes { get; } = new(new[]
|
||||
{
|
||||
typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4),
|
||||
typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4),
|
||||
typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4),
|
||||
typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4),
|
||||
typeof(Int1x1), typeof(Int1x2), typeof(Int1x3), typeof(Int1x4),
|
||||
typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4),
|
||||
typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4),
|
||||
typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4),
|
||||
typeof(UInt1x1), typeof(UInt1x2), typeof(UInt1x3), typeof(UInt1x4),
|
||||
typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4),
|
||||
typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4),
|
||||
typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4),
|
||||
typeof(Float1x1), typeof(Float1x2), typeof(Float1x3), typeof(Float1x4),
|
||||
typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4),
|
||||
typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4),
|
||||
typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4),
|
||||
typeof(Double1x1), typeof(Double1x2), typeof(Double1x3), typeof(Double1x4),
|
||||
typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4),
|
||||
typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4),
|
||||
typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4)
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a known scalar type.
|
||||
/// </summary>
|
||||
@@ -38,11 +68,19 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
|
||||
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known HLSL vector type.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownVectorType(Type type) => _HlslMappedVectorTypes.Contains(type) ||
|
||||
public static bool IsKnownVectorType(Type type) => HlslMappedVectorTypes.Contains(type) ||
|
||||
type == typeof(Vector2) ||
|
||||
type == typeof(Vector3) ||
|
||||
type == typeof(Vector4);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a known matrix type.
|
||||
/// </summary>
|
||||
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
|
||||
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known HLSL matrix type.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixType(Type type) => HlslMappedMatrixTypes.Contains(type);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a known buffer type.
|
||||
/// </summary>
|
||||
|
||||
@@ -104,14 +104,17 @@ namespace ComputeSharp.Shaders.Translation
|
||||
fixed (char* entryPoint = nameof(IComputeShader.Execute))
|
||||
fixed (char* shaderProfile = "cs_6_0")
|
||||
fixed (char* optimization = "-O3")
|
||||
fixed (char* rowMajor = "-Zpr")
|
||||
{
|
||||
char** arguments = stackalloc char*[2] { optimization, rowMajor };
|
||||
|
||||
DxcCompiler.Get()->Compile(
|
||||
(IDxcBlob*)dxcBlobEncoding.Get(),
|
||||
(ushort*)shaderName,
|
||||
(ushort*)entryPoint,
|
||||
(ushort*)shaderProfile,
|
||||
(ushort**)&optimization,
|
||||
1,
|
||||
(ushort**)arguments,
|
||||
2,
|
||||
null,
|
||||
0,
|
||||
DxcIncludeHandler.Get(),
|
||||
|
||||
@@ -217,7 +217,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
this.hlslResourceInfo.Add(new HlslResourceInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++));
|
||||
this.totalResourceCount++;
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType))
|
||||
else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType) || HlslKnownTypes.IsKnownMatrixType(fieldType))
|
||||
{
|
||||
this.capturedFields.Add(fieldInfo);
|
||||
this.fieldsInfo.Add(new CapturedFieldInfo(hlslType, hlslName));
|
||||
|
||||
@@ -1097,6 +1097,47 @@ namespace ComputeSharp.Tests.Internals
|
||||
VerifyGeneratedDiagnostics<IComputeShaderSourceGenerator>(source, "CMPS0036");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NonConstantMatrixSwizzledIndex()
|
||||
{
|
||||
string source = @"
|
||||
using ComputeSharp;
|
||||
using static ComputeSharp.MatrixIndex;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
public class ReadWriteBuffer<T> { }
|
||||
public enum MatrixIndex
|
||||
{
|
||||
M11,
|
||||
M12,
|
||||
M13
|
||||
},
|
||||
public struct Float4 { }
|
||||
public struct Float4x4
|
||||
{
|
||||
public Float4 this[MatrixIndex m0, MatrixIndex m1, MatrixIndex m2, MatrixIndex m3] => default;
|
||||
}
|
||||
}
|
||||
|
||||
namespace MyFancyApp.Sample
|
||||
{
|
||||
public struct MyShader : IComputeShader
|
||||
{
|
||||
public ReadWriteBuffer<float> buffer;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
Float4x4 m = default;
|
||||
MatrixIndex index = M12;
|
||||
Float4 = m[M11, index, M13, M11];
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
VerifyGeneratedDiagnostics<IComputeShaderSourceGenerator>(source, "CMPS0042");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the output of a source generator.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
using ComputeSharp.__Internals;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.Tests.Internals
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("ShaderDataLoader")]
|
||||
public partial class ShaderDataLoaderTests
|
||||
{
|
||||
[AutoConstructor]
|
||||
public partial struct Shader1 : IComputeShader
|
||||
{
|
||||
public ReadWriteBuffer<float> _;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadShader1()
|
||||
{
|
||||
ulong* p0 = stackalloc ulong[128];
|
||||
byte* p1 = stackalloc byte[256];
|
||||
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(16);
|
||||
|
||||
int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new Shader1(buffer), ref *p0, ref *p1);
|
||||
|
||||
Assert.AreEqual(12, size);
|
||||
|
||||
Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public partial struct Shader2 : IComputeShader
|
||||
{
|
||||
public ReadWriteBuffer<float> _0;
|
||||
public ReadWriteBuffer<float> _1;
|
||||
public int a;
|
||||
public int b;
|
||||
public int c;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadShader2()
|
||||
{
|
||||
ulong* p0 = stackalloc ulong[128];
|
||||
byte* p1 = stackalloc byte[256];
|
||||
|
||||
using ReadWriteBuffer<float> buffer0 = Gpu.Default.AllocateReadWriteBuffer<float>(16);
|
||||
using ReadWriteBuffer<float> buffer1 = Gpu.Default.AllocateReadWriteBuffer<float>(16);
|
||||
|
||||
int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new Shader2(buffer0, buffer1, 1, 22, 77), ref *p0, ref *p1);
|
||||
|
||||
Assert.AreEqual(24, size);
|
||||
|
||||
Assert.AreEqual(p0[0], buffer0.D3D12GpuDescriptorHandle.ptr);
|
||||
Assert.AreEqual(p0[1], buffer1.D3D12GpuDescriptorHandle.ptr);
|
||||
|
||||
Assert.AreEqual(1, *(int*)&p1[12]);
|
||||
Assert.AreEqual(22, *(int*)&p1[16]);
|
||||
Assert.AreEqual(77, *(int*)&p1[20]);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public partial struct Shader3 : IComputeShader
|
||||
{
|
||||
public ReadWriteBuffer<float> _;
|
||||
public Float3 f3;
|
||||
public int a;
|
||||
public int b;
|
||||
public Double2 d2;
|
||||
public int c;
|
||||
public int d;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadShader3()
|
||||
{
|
||||
ulong* p0 = stackalloc ulong[128];
|
||||
byte* p1 = stackalloc byte[256];
|
||||
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(16);
|
||||
|
||||
Shader3 shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999);
|
||||
|
||||
int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1);
|
||||
|
||||
Assert.AreEqual(72, size);
|
||||
|
||||
Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr);
|
||||
|
||||
Assert.AreEqual(55, *(float*)&p1[16]);
|
||||
Assert.AreEqual(44, *(float*)&p1[20]);
|
||||
Assert.AreEqual(888, *(float*)&p1[24]);
|
||||
Assert.AreEqual(22, *(int*)&p1[28]);
|
||||
Assert.AreEqual(77, *(int*)&p1[32]);
|
||||
Assert.AreEqual(3.14, *(double*)&p1[48]);
|
||||
Assert.AreEqual(6.28, *(double*)&p1[56]);
|
||||
Assert.AreEqual(42, *(int*)&p1[64]);
|
||||
Assert.AreEqual(9999, *(int*)&p1[68]);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public partial struct Shader4 : IComputeShader
|
||||
{
|
||||
public ReadWriteBuffer<float> _;
|
||||
public Float2x3 f2x3;
|
||||
public int a;
|
||||
public Int1x3 i1x3;
|
||||
public Double2 d2;
|
||||
public int c;
|
||||
public Int1x2 i1x2;
|
||||
public Int2x2 i2x2;
|
||||
public int d;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadShader4()
|
||||
{
|
||||
ulong* p0 = stackalloc ulong[128];
|
||||
byte* p1 = stackalloc byte[256];
|
||||
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(16);
|
||||
|
||||
Shader4 shader = new(
|
||||
buffer,
|
||||
f2x3: new(55, 44, 888, 111, 222, 333),
|
||||
a: 22,
|
||||
i1x3: new(1, 2, 3),
|
||||
d2: new(3.14, 6.28),
|
||||
c: 42,
|
||||
i1x2: new(111, 222),
|
||||
i2x2: new(11, 22, 33, 44),
|
||||
d: 9999);
|
||||
|
||||
int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1);
|
||||
|
||||
Assert.AreEqual(124, size);
|
||||
|
||||
Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr);
|
||||
|
||||
Assert.AreEqual(55, *(float*)&p1[16]);
|
||||
Assert.AreEqual(44, *(float*)&p1[20]);
|
||||
Assert.AreEqual(888, *(float*)&p1[24]);
|
||||
Assert.AreEqual(111, *(float*)&p1[32]);
|
||||
Assert.AreEqual(222, *(float*)&p1[36]);
|
||||
Assert.AreEqual(333, *(float*)&p1[40]);
|
||||
Assert.AreEqual(22, *(int*)&p1[44]);
|
||||
Assert.AreEqual(1, *(int*)&p1[48]);
|
||||
Assert.AreEqual(2, *(int*)&p1[52]);
|
||||
Assert.AreEqual(3, *(int*)&p1[56]);
|
||||
Assert.AreEqual(3.14, *(double*)&p1[64]);
|
||||
Assert.AreEqual(6.28, *(double*)&p1[72]);
|
||||
Assert.AreEqual(42, *(int*)&p1[80]);
|
||||
Assert.AreEqual(111, *(int*)&p1[84]);
|
||||
Assert.AreEqual(222, *(int*)&p1[88]);
|
||||
Assert.AreEqual(11, *(int*)&p1[96]);
|
||||
Assert.AreEqual(22, *(int*)&p1[100]);
|
||||
Assert.AreEqual(33, *(int*)&p1[112]);
|
||||
Assert.AreEqual(44, *(int*)&p1[116]);
|
||||
Assert.AreEqual(9999, *(int*)&p1[120]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
namespace ComputeSharp.Tests.Internals
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("ShaderHashCodes")]
|
||||
[TestCategory("ShaderHashCode")]
|
||||
public class ShaderHashCodeTests
|
||||
{
|
||||
public struct Shader1 : IComputeShader
|
||||
|
||||
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 197 KiB |
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 29 KiB |
@@ -31,6 +31,8 @@
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\FractalTiling.cs" Link="Shaders\FractalTiling.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\MengerJourney.cs" Link="Shaders\MengerJourney.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TwoTiledTruchet.cs" Link="Shaders\TwoTiledTruchet.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\Octagrams.cs" Link="Shaders\Octagrams.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ProteanClouds.cs" Link="Shaders\ProteanClouds.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -47,9 +49,18 @@
|
||||
<None Update="Assets\MengerJourney.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\Octagrams.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\TwoTiledTruchet.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\Octagrams.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\ProteanClouds.jpg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Texture2DTests.Pixels.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Texture2DTests.Pixels.g.cs</LastGenOutput>
|
||||
|
||||
@@ -43,6 +43,20 @@ namespace ComputeSharp.Tests
|
||||
RunAndCompareShader(device, static texture => new TwoTiledTruchet(texture, 0), 0.00033f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Octagrams(Device device)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new Octagrams(texture, 0), 0.00000586f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void ProteanClouds(Device device)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new ProteanClouds(texture, 0), 0.00000107f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a given test for a specified shader.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする