How to submit a Float form to iOS Metal shader using Swift?

I want to transfer the float to my metal shader. I can’t understand how.

Here is my shader:

vertex float4 model_vertex(unsigned int iid[[instance_id]] constant float angle) { float number = float(iid) / 64.0; return float4(number * sin(angle), number * cos(angle), 0.0, 1.0); } 

Now I want to pass it to the shader:

 let renderPassDescriptor = MTLRenderPassDescriptor() let renderEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor) // ... let angle: Float = 0.5 renderEncoder.setUniform1(angle) // What do I do here? 

How to pass a single float value?

+5
source share
2 answers

I have not seen setUniform * before. To pass a form to your vertex shader, use:

 setVertexBuffer(buffer: MTLBuffer?, offset: Int, atIndex index: Int) 

Where the buffer will be an array with one float, in your example. To pass a uniform to a shader fragment, use setFragmentBuffer.

+7
source

Also in 10.11+ and iOS 9+ you can use:

 public func setVertexBytes(bytes: UnsafePointer<Void>, length: Int, atIndex index: Int) 

Which is better documented than creating MTLBuffer if you use the buffer once (and your data is less than 4K in length).

+7
source

Source: https://habr.com/ru/post/1204413/


All Articles