user1378224
user1378224

Reputation:

Pass float3 from Swift struct to metal shader

Boiling the issue down to minimal code, I have a metal fragment shader that returns a colour:

constant float3 uSeaColor = float3(0.39216,0.4549,0.51373);

fragment float4 fragmentMain(constant Uniforms& uniforms [[buffer(2)]]){
    return float4(uSeaColor.rgb, 1);
}

This returns the colour I expected and works as intended. However, I'm trying to pass this colour (and other uniforms) from a swift struct with:

struct Uniforms {
  var overlayOpacity: Float = 1.0
  var gridAlpha:CGPoint = CGPoint(x: 0.5, y: 0.5)
  var uSeaColor = SCNVector3(0.39216,0.4549,0.51373)
}

var uniforms = Uniforms()
let data = NSData(bytes: &uniforms, length: MemoryLayout.size(ofValue: uniforms))
sphereNode.geometry?.firstMaterial?.setValue(data, forKey: "uniforms")

and in my shader, a matching struct:

struct Uniforms {
  float overlayOpacity;
  float2 girdAlpha;
  float3 uSeaColor;
}

The issue is when I swap from the constant to the struct uniform colour, the colour doesn't match at all. Using the comparisons from here, I've tried passing in a float3 (import simd) and SCNVector3.

How can I make sure the constant float3 I use in my shader will be represented the same way when I pass it in from Swift?

Upvotes: 2

Views: 1435

Answers (1)

user1378224
user1378224

Reputation:

My struct had a variety of types in it, using what I had read from the table here I was passing in my float2 as a CGPoint. Changing the types to match exactly what they are in the metal shader fixed all my problems. Obvious in retrospect.

Upvotes: 2

Related Questions