Using GLSL Shaders on objects in UPBGE

First, make sure this code I made is used first:

    import bge
    import bge.logic as gl
    import bge.texture as tex

    own = cont.owner

    mat = tex.materialID(own, 'MA' + own['material'])

    channel = own['channel']

    texture = tex.Texture(own, matID, channel)

    name = own['shader']

    shader = gl.expandPath('//shader_path/' + name)

    new_source = tex.ImageFFmpeg(shader)
    
    gl.texture = texture
    gl.texture.source = new_source
    gl.texture.refresh(True)

Then, save this GLSL code I made below for testing the bug (make sure there is a shader_path directory in the same path as the blend file), and use it on the cube:

out vec4 fragColor;

void main(void) {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

If you set it up correctly, the cube should turn bright red. On an M1 iMac or MacBook, it will instead remain black due to the M1 shader compiling bug. This requires the usage of a Python script in a controller, NOT a 2D Filter actuator and a material property (get the material name you typed in), a shader property that points to the filename that must end in .glsl, and a channel property, this tells the code what node texture to use.

Don’t you get an error for cont not beeing defined?

I forgot to mention you need to either define the name cont as in the controller, or use def main(cont) and indent the text.

Try using a glsl shader on it. Should you be on an M1 Mac, it shall just be black with an unloadable texture due to M1’s shader compiling bug. On other machines it should work flawlessly.

Nope, doesn’t work. Windows machine, no error messages.

Looks like I’ll have to wait until the Metal version of UPBGE comes out to test and write my own shaders.

By the way, I do check upbge.org’s download page sometimes for new nightly builds, but it can be a month, and still no nightly release build despite it saying “Released a new build every week.” A week is 7 days, not a random amount of days.

With UPBGE 0.36 having Metal support, this would eliminate the slow “compiling shaders (x amount done)” almost completely on the Mac version, as the GPU Metal backend will be able to do it much faster. It would also increase performance, and allow for us, Mac users to once again map our written shaders via videoTexture to an object like I was trying to do without the hassle of trying to write arrays or something complicated just to get it to work.

Shaders as mentioned before, especially 2D filters will be much faster and more responsive.

The M1’s “UNSUPPORTED (log once) POSSIBLE ISSUE” shader compilation bug will no longer be an issue due to shaders being supported on the Metal backend, not the depreciated openGL.

I wonder if SetSource() will come back for 0.36? I’m a bit curious as to when this version would get released.

I’m sorry to drag everyone through this post, but openGL seems to not work properly on M1 when a shader is mapped to an object as a texture through an int value (bge.logic.videotexture.materialID). It should work in the next version through Metal though, although I’m not excited. Nevertheless, I look forward to UPBGE becoming a great game engine for everyone out there.

As stated on GitHub, UPBGE no longer supports BL_Shader.setSource() or other former BGE compatible KX_BlenderMaterial attributes/methods. Instead, use the GPU module as BluePrintRandom mentioned.