Trying to understand the GPUshader module API changes

Hi, I was looking through the changes made in Blender 3.5 for the gpumodule
https://docs.blender.org/api/current/gpu.html

I noticed that some changes are being made but i am unsure on how to get a working version prior to the api change converted to the new one.

Here is the fragment/vertex shader that works in Blender 3.4

Fragment shaders for rounded points

    vshader = """
        uniform mat4 ModelViewProjectionMatrix;
        in vec3 pos;
        void main()
        {
            gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0) ;
        }
    """

    fshader = """



        void main()
        {
            float r = 0.0, delta = 0.0, alpha = 0.0;
            vec2 cxy = 2.0 * gl_PointCoord - 1.0;
            r = dot(cxy, cxy);
            if (r > 1.0) {
                discard;
            }


            gl_FragColor = vec4(1.0, 1.0, 1.0, 0.5);


        }
    """
    shader = GPUShader(vshader, fshader )if not bpy.app.background else None

    batch = batch_for_shader(shader, 'POINTS', {"pos": self.points})

    shader.bind()
   
    batch.draw(shader)

how would I go about getting the same result with the new api changes for Blender 3.5?

I’m not exactly sure what this does…just made this:

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

vshader = """
    uniform mat4 ModelViewProjectionMatrix;
    in vec3 pos;
    void main()
    {
        gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0) ;
    }
"""

fshader = """
    void main()
    {
        float r = 0.0, delta = 0.0, alpha = 0.0;
        vec2 cxy = 2.0 * gl_PointCoord - 1.0;
        r = dot(cxy, cxy);
        if (r > 1.0) {
            discard;
        }
        gl_FragColor = vec4(1.0, 1.0, 1.0, 0.5);
    }
"""
shader = gpu.types.GPUShader(vshader, fshader ) #if not bpy.app.background else None

points = [(1, 1, 1), (2, 0, 0), (-2, -1, 3)]
batch = batch_for_shader(shader, 'POINTS', {"pos": points})

shader.bind()
   
batch.draw(shader)

…runable without errore in 3.4.1 and 3.5.1… (where is the API change ?? )

Is there a reason why they are changing it like this?

shader = gpu.shader.create_from_info(shader_info)

See https://docs.blender.org/api/current/gpu.html#triangle-with-custom-shader

I thought they were deprecating opengl? or is this not being affected

Well… seems to be depricated since 3.4… when they put it into the combined shader info (but does work in 3.5?? … as i said i didn’t looked too deep into you code :sweat_smile: … bbut sometimes just another pair of eyes… :eyes: sees something different… )

Maybe it’s conected to the internal mesh format they spoke of ?? And so the associated shader do have different structure…

shader.create_from_info abstracts custom shaders by making the process use less moving parts.

What does this mean?

For custom shaders, that is, shaders where we define the source ourselves, we:

  • no longer write the vertex inputs/outputs directly in source.
  • no longer define constants in source
  • no longer define uniforms in source
  • make the GLSL abstract enough to allow opengl > vulkan cross compilation.

Edit:

Just found this page that may provide some more detailed info.
https://wiki.blender.org/wiki/Source/EEVEE_%26_Viewport/GPU_Module/GLSL_Cross_Compilation

The old way of writing custom shaders (with static glsl source) is still going to work. There’s no plan to deprecate opengl GLSL shaders yet. However, the gpu API as it were from 2.83 to 3.4 isn’t vulkan-ready, so when we do get to the point where Blender is able to select between using opengl and vulkan backends and you use static glsl code, it will simply fall back to opengl.

If you plan to support Blender versions below 3.4, you would still need to do it the old way, obviously.

1 Like