0

The following fragment shader will accept an array of [int2]

fragment half4 array_FragmentShader(Vertex_Out in [[ stage_in ]],
constant int2 *int2Array [[ buffer(1) ]]){
half4 retVal = half4(0.0,0.0,0.0,1.0);

if(int2Array[0].x==1){retVal = half4(0.6,0.0,0.0,1.0);}

return retVal;
}

nothing particularly difficult about that, however what Im wondering at the minute is how I pass an array of arrays into a fragment shader. Im only trying to find out the parameter syntax , for example do I use two asterisks to indicate a pointer to a pointer or something like that?

constant int2 **int2Array [[ buffer(1) ]]) 

addendum, should have included this in the very first place...

the code calling this is on the cpu side and is utilising a SCNProgram , see below:

let gridProgram = {
        let p = SCNProgram()
        p.vertexFunctionName = "boolVertexShader"
        p.fragmentFunctionName = "array_FragmentShader"
        p.isOpaque = false;
        return p
    }()

    currentMaterial.program = gridProgram

    let i2 = simd_make_int2(1,1)

    var int2Array = [i2]
    
    gridProgram.handleBinding(ofBufferNamed: "int2Array", frequency: .perFrame) { (bufferStream, node, shadable, renderer) in
        bufferStream.writeBytes(&int2Array,count:MemoryLayout<SIMD2<Int32>>.stride*int2Array.count)
    }

I also need to figure out how to send the 2d array from the cpu side because the .handleBinding requires 1: the swift side array and 2: the size of the data going to the cpu, Im guessing that if I go down this route with a 2d array that the individual arrays will have to be of uniform size?... anyone come across this before?

JohnOfIreland
  • 271
  • 2
  • 11
  • Does this answer your question? [How to pass \[\[Int\]\] into MTLBuffer in Swift and receive it in Metal?](https://stackoverflow.com/questions/72882694/how-to-pass-int-into-mtlbuffer-in-swift-and-receive-it-in-metal) – Hamid Yusifli Jul 08 '23 at 21:04
  • not...quite what Im trying to get, (though that question actually prompted me to do an update on my question), that question is very similar to mine in that the user is trying to send a 2d array of ints over to the metal side, now one of the guys answering has pointed out that the compute func was accepting a constant parameter when it needed to be a device param but as to whether or not there is specific syntax for a metal function to accept a 2d buffer in its params docent seem to be in that thread - its still using a 1d pointer - "constant int *graph [[ buffer(0) ]" – JohnOfIreland Jul 09 '23 at 11:30
  • ok, I've decided to go about this in a different way, im now defining a type in a .h file which includes a fixed size array of values. Then Im sending an array of that type over to the gpu side. I'll spend a while checking to see if that gets the job done.. – JohnOfIreland Jul 09 '23 at 14:16

0 Answers0