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
switch param {
...
case .points(let pointsArray):
var pointsArray = pointsArray
renderEncoder.setFragmentBytes(&pointsArray, length: MemoryLayout<SIMD2<Float>>.stride * 10, index: 1)
}
switch param {
...
case .points(let pointsArray):
var pointsArray = pointsArray
renderEncoder.setFragmentBytes(&pointsArray, length: MemoryLayout<SIMD2<Float>>.stride * 10, index: 1)
}
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:
case .color(let color):
var color = color
renderEncoder.setFragmentBytes(&color, length: MemoryLayout<SIMD4<Float>>.stride, index: 0)
case .color(let color):
var color = color
renderEncoder.setFragmentBytes(&color, length: MemoryLayout<SIMD4<Float>>.stride, index: 0)
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
grimlessbbs
grimlessbbs2y ago
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?
Want results from more Discord servers?
Add your server