How to check if a shader is valid when loading

I am currently having a bit of a hangup when parsing all of the files in my dropbox of shaders, I noticed that with image and other assets it can return if it was successful or nil if not, but so far I can’t seem to find a way to avoid getting a full program stopping error when i do shader(xxx) where xxx is a problematic shader.

As in any attempt at checking seems to need to use the shader() object which gives the error … oh I might have figured out what the problem shader was… I had a raymarch1.3 so I guess it can’t deal with another .

Dropbox:SDF_march_1.3:::string

GLSL_Shader:77: bad argument #1 to ‘shader’ (Shader not found)
stack traceback:
[C]: in function ‘shader’
GLSL_Shader:77: in method ‘setShader’
Main:50: in function Main:50


I am still trying to figure out a way to do this though, as I want to make sure there are fail safes in place rather than crash the app if some faulty data comes in.

I am looking into assert

@AxiomCrux You’re not giving enough info on your shader or how you’re creating it. Here is an example of a simple shader. In setup there is a sh=myShader. If the shader exists, sh shows as a table. If the shader isn’t there, sh is nil. So I can check sh for nil and go from there.

function setup()
    sh=myShader
    print(sh)
    m = mesh()
    if sh~=nil then
        m.shader = shader(myShader.vert, myShader.frag)
    end
end

function draw()
    background(40, 40, 50)
    m:draw()
end

myShader={
vert=[[
    uniform mat4 modelViewProjection;
    attribute vec4 position;
    attribute vec4 color;    
    varying lowp vec4 vColor;
    void main()
    {   
        vColor = color;
        gl_Position = modelViewProjection * position;
    }
    ]],

frag=[[    
    varying lowp vec4 vColor;
    void main()
    {   gl_FragColor = vColor;
    }
    ]]
}

if i remember rightly, pcall can trap an error in the code

I appologize @dave1707 , I should have posted some code and explained further, I am referring to the shader assets from the dropbox I have made using th shader lab. They load differently than the ones I do as string blocks inside a codea project tab. Its easy enough to parse and check the strings all sorts of ways, but when I was testing the ability to use the listAssets(SHADERS) and populate the whole folder, some of them were giving errors, but instead of giving an error and moving on, it would fully halt the activity of the test app.

I believe I found the culprit, and it was a named file with a “.” in it, this project is really moving along and coming out pretty cool. I will post a video clip to show my early test asap. currently it populates the screen with little thumbnails of all the shaders in the folder and they all work, but I need to figure out how to parse some JSON to set better default values for the uniforms. It looks like there is JSON in the codea shader asset folders on my dropbox which is storing the shader lab uniform links.

I hope to then do a few possible things with this project, one is that I am looking at the shadertoy api and may be able to create a fun interactive shader playground, another is that I have been thinking of making an SDF modelling tool for ray marching, similar to F3. and Finally I want to impliment Interactiveshaderformat.com spec fully, and maybe make a cool little VJ audiovisualizer app. The only thing standing in my way for that one is I have no idea how I will get audio into the codea project from an external source as it stands. I am wondering if there might be something I can link it to in C++ with openframeworks or cinder or something.

I really love working in codea… it makes programming feel playful and fun again, not so daunting and work involved as I am used to on my hardware and firmware projects.