iosglslmetalfragment-shadershading

IOS Metal Fragment shader file compilation


I have fragment shader "fsh" file and I am trying to compile it, it is originally taken from Shadertoy, and it is in GLSL, I am trying to port it to METAL, and I am getting the following error:

program_source:129:12: error: program scope variable must reside in constant address space const vec3 ro, rd;

As far as I can understand, I can not define ro and rd in global scope like this, how can I fix this ?

Thank you very much.

The code is below:

const vec3 ro, rd;

....

void main(void)
{
    float t = u_time;
    vec3 col = vec3(0.);
    vec2 uv = gl_FragCoord.xy / iResolution.xy; // 0 <> 1

    uv -= .5;
    uv.x *= iResolution.x/iResolution.y;

    vec2 mouse = gl_FragCoord.xy/iResolution.xy;

    vec3 pos = vec3(.3, .15, 0.);

    float bt = t * 5.;
    float h1 = N(floor(bt));
    float h2 = N(floor(bt+1.));
    float bumps = mix(h1, h2, fract(bt))*.1;
    bumps = bumps*bumps*bumps*CAM_SHAKE;

    pos.y += bumps;
    float lookatY = pos.y+bumps;
    vec3 lookat = vec3(0.3, lookatY, 1.);
    vec3 lookat2 = vec3(0., lookatY, .7);
    lookat = mix(lookat, lookat2, sin(t*.1)*.5+.5);

    uv.y += bumps*4.;
    CameraSetup(uv, pos, lookat, 2., mouse.x);

    t *= .03;
    t += mouse.x;

    // fix for GLES devices by MacroMachines
    #ifdef GL_ES
    const float stp = 1./8.;
    #else
    float stp = 1./8.;
    #endif

    for(float i=0.; i<1.; i+=stp) {
       col += StreetLights(i, t);
    }

    for(float i=0.; i<1.; i+=stp) {
        float n = N(i+floor(t));
        col += HeadLights(i+n*stp*.7, t);
    }

    #ifndef GL_ES
    #ifdef HIGH_QUALITY
    stp = 1./32.;
    #else
    stp = 1./16.;
    #endif
    #endif

    for(float i=0.; i<1.; i+=stp) {
       col += EnvironmentLights(i, t);
    }

    col += TailLights(0., t);
    col += TailLights(.5, t);

    col += sat(rd.y)*vec3(.6, .5, .9);

    gl_FragColor = vec4(col, 0.);
}

Solution

  • The equivalent declaration in Metal Shading Language (MSL) would be

    constant float3 ro, rd;
    

    However, you should also initialize these variables with values, since your shader functions will not be allowed to mutate them. Something like

    constant float3 ro(0, 0, 0), rd(1, 1, 1);
    

    A few more translation hints: