// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

float power_of_2_string_to_float(string val) 
{
    if (val == "256") {
        return 256/256.0;
    }
    if (val == "128") {
        return 128/256.0;
    }
    if (val == "64") {
        return 64/256.0;
    }
    if (val == "32") {
        return 32/256.0;
    }
    if (val == "16") {
        return 16/256.0;
    }
    if (val == "8") {
        return 8/256.0;
    }
    if (val == "4") {
        return 4/256.0;
    }
    if (val == "2") {
        return 2/256.0;
    }
    if (val == "1") {
        return 1/256.0;
    }
    return 0;
 }

shader
test_uniform_index_varying_string (output color Cout = color(0,0,0))
{
    // Test array referencing with varying index
    string rarray[3] = { "2", "256", "8" };
    
    if (P[0] > 0.2) {
        rarray[1] = "4";
    }
    
    
    // Using getattribute to obtain a uniform, but non-const index    
    int res[2];
    getattribute("camera:resolution", res);
    int uniformIndexR = res[0]%3;
    
    string indirectR = rarray[uniformIndexR];
    
    Cout[0] = power_of_2_string_to_float(indirectR);
}

