Metal `setFragmentBytes` not uploading data
I am trying to pass an array of 2D points to my Metal fragment shader
fragment float4 bg_frag(FragIn frag [[ stage_in ]], constant float4& color [[ buffer(0) ]], constant float2* points [[ buffer(1) ]])
but the data I see in the shader is complete garbage. I am passing it to the shader using
Before you ask: yes I know for sure there are 10 points in the array, I fixed its size just to get this feature working. Using print(pointsArray)
prints out 10 SIMD2<Float> points.
Using the Metal inspector to capture a frame, the data length is correct and the buffer is set (as shown in the first attached image) but the data in buffer(1)
is total garbage (second image). Any ideas what might be going on here?
My guess is that the MTLRenderCommandEncoder
is uploading this data lazily, so the array falls out of scope and is deallocated before the MTLRCE
gets around to synchronizing it for the raster operation. Maybe? That's my guess. I can't think of any OTHER reason why this is happening. I have some other code in that switch statement that works as expected:
This properly uploads the color in buffer(0)
and I have yet to see it mangle the data in the same way that the array upload is doing. Any thoughts? Thanks!1 Reply
After some more investigation: I'm now trying to upload 8 points using a
SIMD16<Float>
, and that appears to be working. I think it has something to do with using the array; maybe the scoping issue/address resolution for the upload is the issue for things like arrays?