[Bf-blender-cvs] [cb771dbe76d] master: PyDoc: update the shader creation examples with gpu module

Germano Cavalcante noreply at git.blender.org
Thu Sep 1 13:32:09 CEST 2022


Commit: cb771dbe76d4d5c6d1127dfedce4edd06e1c5b7b
Author: Germano Cavalcante
Date:   Thu Sep 1 08:27:29 2022 -0300
Branches: master
https://developer.blender.org/rBcb771dbe76d4d5c6d1127dfedce4edd06e1c5b7b

PyDoc: update the shader creation examples with gpu module

The old way of creating shaders is being replaced by using
`GPUShaderCreateInfo` for more portability.

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D14634

===================================================================

M	doc/python_api/examples/gpu.10.py
M	doc/python_api/examples/gpu.2.py
M	doc/python_api/examples/gpu.7.py

===================================================================

diff --git a/doc/python_api/examples/gpu.10.py b/doc/python_api/examples/gpu.10.py
index 8e354dd09a8..b47ff732e2b 100644
--- a/doc/python_api/examples/gpu.10.py
+++ b/doc/python_api/examples/gpu.10.py
@@ -14,33 +14,36 @@ from random import random
 from mathutils import Vector
 from gpu_extras.batch import batch_for_shader
 
-vertex_shader = '''
-    uniform mat4 u_ViewProjectionMatrix;
+vert_out = gpu.types.GPUStageInterfaceInfo("my_interface")
+vert_out.smooth('FLOAT', "v_ArcLength")
 
-    in vec3 position;
-    in float arcLength;
+shader_info = gpu.types.GPUShaderCreateInfo()
+shader_info.push_constant('MAT4', "u_ViewProjectionMatrix")
+shader_info.push_constant('FLOAT', "u_Scale")
+shader_info.vertex_in(0, 'VEC3', "position")
+shader_info.vertex_in(1, 'FLOAT', "arcLength")
+shader_info.vertex_out(vert_out)
+shader_info.fragment_out(0, 'VEC4', "FragColor")
 
-    out float v_ArcLength;
-
-    void main()
-    {
-        v_ArcLength = arcLength;
-        gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);
-    }
-'''
-
-fragment_shader = '''
-    uniform float u_Scale;
+shader_info.vertex_source(
+    "void main()"
+    "{"
+    "  v_ArcLength = arcLength;"
+    "  gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);"
+    "}"
+)
 
-    in float v_ArcLength;
-    out vec4 FragColor;
+shader_info.fragment_source(
+    "void main()"
+    "{"
+    "  if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;"
+    "  FragColor = vec4(1.0);"
+    "}"
+)
 
-    void main()
-    {
-        if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;
-        FragColor = vec4(1.0);
-    }
-'''
+shader = gpu.shader.create_from_info(shader_info)
+del vert_out
+del shader_info
 
 coords = [Vector((random(), random(), random())) * 5 for _ in range(5)]
 
@@ -48,7 +51,6 @@ arc_lengths = [0]
 for a, b in zip(coords[:-1], coords[1:]):
     arc_lengths.append(arc_lengths[-1] + (a - b).length)
 
-shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
 batch = batch_for_shader(
     shader, 'LINE_STRIP',
     {"position": coords, "arcLength": arc_lengths},
diff --git a/doc/python_api/examples/gpu.2.py b/doc/python_api/examples/gpu.2.py
index 565ec5d5d0c..2a46e833752 100644
--- a/doc/python_api/examples/gpu.2.py
+++ b/doc/python_api/examples/gpu.2.py
@@ -6,33 +6,37 @@ import bpy
 import gpu
 from gpu_extras.batch import batch_for_shader
 
-vertex_shader = '''
-    uniform mat4 viewProjectionMatrix;
 
-    in vec3 position;
-    out vec3 pos;
-
-    void main()
-    {
-        pos = position;
-        gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
-    }
-'''
-
-fragment_shader = '''
-    uniform float brightness;
-
-    in vec3 pos;
-    out vec4 FragColor;
-
-    void main()
-    {
-        FragColor = vec4(pos * brightness, 1.0);
-    }
-'''
+vert_out = gpu.types.GPUStageInterfaceInfo("my_interface")
+vert_out.smooth('VEC3', "pos")
+
+shader_info = gpu.types.GPUShaderCreateInfo()
+shader_info.push_constant('MAT4', "viewProjectionMatrix")
+shader_info.push_constant('FLOAT', "brightness")
+shader_info.vertex_in(0, 'VEC3', "position")
+shader_info.vertex_out(vert_out)
+shader_info.fragment_out(0, 'VEC4', "FragColor")
+
+shader_info.vertex_source(
+    "void main()"
+    "{"
+    "  pos = position;"
+    "  gl_Position = viewProjectionMatrix * vec4(position, 1.0f);"
+    "}"
+)
+
+shader_info.fragment_source(
+    "void main()"
+    "{"
+    "  FragColor = vec4(pos * brightness, 1.0);"
+    "}"
+)
+
+shader = gpu.shader.create_from_info(shader_info)
+del vert_out
+del shader_info
 
 coords = [(1, 1, 1), (2, 0, 0), (-2, -1, 3)]
-shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
 batch = batch_for_shader(shader, 'TRIS', {"position": coords})
 
 
diff --git a/doc/python_api/examples/gpu.7.py b/doc/python_api/examples/gpu.7.py
index 9d881831c21..e3bfbd14e34 100644
--- a/doc/python_api/examples/gpu.7.py
+++ b/doc/python_api/examples/gpu.7.py
@@ -35,35 +35,37 @@ with offscreen.bind():
 # Drawing the generated texture in 3D space
 #############################################
 
-vertex_shader = '''
-    uniform mat4 modelMatrix;
-    uniform mat4 viewProjectionMatrix;
-
-    in vec2 position;
-    in vec2 uv;
-
-    out vec2 uvInterp;
-
-    void main()
-    {
-        uvInterp = uv;
-        gl_Position = viewProjectionMatrix * modelMatrix * vec4(position, 0.0, 1.0);
-    }
-'''
-
-fragment_shader = '''
-    uniform sampler2D image;
+vert_out = gpu.types.GPUStageInterfaceInfo("my_interface")
+vert_out.smooth('VEC2', "uvInterp")
+
+shader_info = gpu.types.GPUShaderCreateInfo()
+shader_info.push_constant('MAT4', "viewProjectionMatrix")
+shader_info.push_constant('MAT4', "modelMatrix")
+shader_info.sampler(0, 'FLOAT_2D', "image")
+shader_info.vertex_in(0, 'VEC2', "position")
+shader_info.vertex_in(1, 'VEC2', "uv")
+shader_info.vertex_out(vert_out)
+shader_info.fragment_out(0, 'VEC4', "FragColor")
+
+shader_info.vertex_source(
+    "void main()"
+    "{"
+    "  uvInterp = uv;"
+    "  gl_Position = viewProjectionMatrix * modelMatrix * vec4(position, 0.0, 1.0);"
+    "}"
+)
 
-    in vec2 uvInterp;
-    out vec4 FragColor;
+shader_info.fragment_source(
+    "void main()"
+    "{"
+    "  FragColor = texture(image, uvInterp);"
+    "}"
+)
 
-    void main()
-    {
-        FragColor = texture(image, uvInterp);
-    }
-'''
+shader = gpu.shader.create_from_info(shader_info)
+del vert_out
+del shader_info
 
-shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
 batch = batch_for_shader(
     shader, 'TRI_FAN',
     {



More information about the Bf-blender-cvs mailing list