Since the gpu module is being worked on to support different rendering
interfaces on the backend, the current way of creating and compiling
GLSL shaders will be deprecated.
So an alternative is needed to keep existing addons still working in
non-OpenGL interfaces.
This patch exposes the ShaderCreateInfo strategy.
The new features can be listed as follows:
>>> gpu.types.GPUShaderCreateInfo.
define(
fragment_out(
fragment_source(
push_constant(
sampler(
typedef_source(
uniform_buf(
vertex_in(
vertex_out(
vertex_source(
>>> gpu.types.GPUStageInterfaceInfo.
flat(
name
no_perspective(
smooth(
>>> gpu.shader.create_from_info(Usage example:
import gpu
shader_info = gpu.types.GPUShaderCreateInfo()
shader_info.define("USE_CLIP_PLANES")
shader_info.typedef_source(
"struct Data {\n"
"#ifdef USE_CLIP_PLANES\n"
" mat4 ModelMatrix;"
" vec4 WorldClipPlanes[4];\n"
"#endif\n"
" int offset;\n"
"#ifdef USE_CLIP_PLANES\n"
" bool use_clip_planes;\n"
"#endif\n"
"};\n"
)
shader_info.push_constant("MAT4", "ModelViewProjectionMatrix")
shader_info.uniform_buf(0, "Data", "g_data")
shader_info.vertex_in(0, "VEC3", "pos")
shader_info.vertex_source(
# #define USE_CLIP_PLANES
# uniform mat4 ModelViewProjectionMatrix;
# uniform Data g_data;
# in vec3 pos;
"void main()"
"{\n"
"#ifdef USE_CLIP_PLANES\n"
" if (g_data.use_clip_planes) {"
" vec4 wpos = g_data.ModelMatrix * vec4(pos, 1.0);"
" gl_ClipDistance[0] = dot(g_data.WorldClipPlanes[0], wpos);"
" gl_ClipDistance[1] = dot(g_data.WorldClipPlanes[1], wpos);"
" gl_ClipDistance[2] = dot(g_data.WorldClipPlanes[2], wpos);"
" gl_ClipDistance[3] = dot(g_data.WorldClipPlanes[3], wpos);"
" }\n"
"#endif\n"
" gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);"
"}"
)
shader_info.fragment_out(0, "UINT", "fragColor")
shader_info.fragment_source(
# uniform Data g_data;
# out uint fragColor;
"void main() {fragColor = uint(gl_PrimitiveID + g_data.offset);}"
)
shader = gpu.shader.create_from_info(shader_info)