Sergio0694/ComputeSharp

Primitive math doesn't work on the CPU

ServerTimeOut opened this issue · 4 comments

Description (optional)

When doing math on the cpu using float3, the result is always 0,0,0.

Reproduction Steps

float3 ValueX = new float3(1, 2, 3);
float3 ValueY = new float3(1, 2, 3);

float3 ValueZ = ValueX + ValueY;
//Result: ValueZ = 0,0,0
ValueZ = ValueX - ValueY;
//Result: ValueZ = 0,0,0
ValueZ = ValueX * ValueY;
//Result: ValueZ = 0,0,0
ValueZ = ValueX / ValueY;
//Result: ValueZ = 0,0,0

Expected Behavior

//Result: ValueZ = 2,4,6
//Result: ValueZ = 0,0,0
//Result: ValueZ = 1,4,9
//Result: ValueZ = 1,1,1

System info

This section should contain useful info such as:

  • ComputeSharp NuGet version
  • Win10

This is by design … @Sergio0694 aren’t these supposed to throw?

This is by design, yeah. The XML docs also say that those methods only work on the GPU 🙂

"aren’t these supposed to throw?"

No they're just no-ops on the CPU so you can use them in static field initializers in shaders. If they threw an exception (which they used to in the past) you wouldn't be able to set static fields into shaders using any of those APIs in the declaration.

If you want to do operations on the CPU, you can use System.Numerics.Vector2,3,4. They will implicitly cast to-and-from float2,3,4.

Thankyou, I got it working with System.Numerics.
I had a read through the Wiki and it isn't called out (or it is unclear) that math operations on HLSL types are GPU only.
I think it needs clarification under the Capturing variables section but I am unable to make Pull Requests to the Wiki.