diff options
author | Germano Cavalcante <mano-wii> | 2022-09-01 14:27:29 +0300 |
---|---|---|
committer | Germano Cavalcante <germano.costa@ig.com.br> | 2022-09-01 14:31:56 +0300 |
commit | cb771dbe76d4d5c6d1127dfedce4edd06e1c5b7b (patch) | |
tree | 49c41d5f81d964af8b0ea2f190bd78c04b3a6624 /doc | |
parent | 6269d66da29ae000f214e775ee54dfc71623e642 (diff) |
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
Diffstat (limited to 'doc')
-rw-r--r-- | doc/python_api/examples/gpu.10.py | 50 | ||||
-rw-r--r-- | doc/python_api/examples/gpu.2.py | 52 | ||||
-rw-r--r-- | doc/python_api/examples/gpu.7.py | 54 |
3 files changed, 82 insertions, 74 deletions
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', { |