PyGPU: only use 3D shaders and rename string enums

Since rB6269d66da, creating formats no longer depends solely on the
shader, but now depends on the dimensions used to fill the VBOs.

This allows 3D shaders to work flawlessly when assigned dimensions are
2D.

So there's no real benefit to us having shaders that are limited to 2D
use anymore.

This limitation makes it difficult to implement other builtin shaders
as they indirectly require a 2D version.

So this commit removes the 2D versions of the builtin sahders used in
Python , renames the string enums but keeps the old enums working for
backward compatibility.

(This brings parts of the changes reviewed in D15836).
This commit is contained in:
Germano Cavalcante 2022-09-02 09:33:29 -03:00
parent c8ac1280bb
commit 8cfca8e1bd
Notes: blender-bot 2023-11-01 20:53:50 +01:00
Referenced by issue #114359, Addons with gpu rendering may not work in 4.0.0
4 changed files with 38 additions and 36 deletions

View File

@ -793,7 +793,7 @@ class Gizmo(StructRNA):
vbo = GPUVertBuf(len=len(verts), format=fmt)
vbo.attr_fill(id=pos_id, data=verts)
batch = GPUBatch(type=type, buf=vbo)
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR' if dims == 3 else '2D_UNIFORM_COLOR')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch.program_set(shader)
return (batch, shader)

View File

@ -42,7 +42,7 @@ def draw_circle_2d(position, color, radius, *, segments=None):
vbo = GPUVertBuf(len=len(verts), format=fmt)
vbo.attr_fill(id=pos_id, data=verts)
batch = GPUBatch(type='LINE_STRIP', buf=vbo)
shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch.program_set(shader)
shader.uniform_float("color", color)
batch.draw()
@ -67,7 +67,7 @@ def draw_texture_2d(texture, position, width, height):
coords = ((0, 0), (1, 0), (1, 1), (0, 1))
shader = gpu.shader.from_builtin('2D_IMAGE')
shader = gpu.shader.from_builtin('IMAGE')
batch = batch_for_shader(
shader, 'TRI_FAN',
{"pos": coords, "texCoord": coords},
@ -77,7 +77,7 @@ def draw_texture_2d(texture, position, width, height):
gpu.matrix.translate(position)
gpu.matrix.scale((width, height))
shader = gpu.shader.from_builtin('2D_IMAGE')
shader = gpu.shader.from_builtin('IMAGE')
shader.bind()
if isinstance(texture, int):

View File

@ -15,7 +15,7 @@ def draw_callback_px(self, context):
blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))
# 50% alpha, 2 pixel width line
shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
gpu.state.blend_set('ALPHA')
gpu.state.line_width_set(2.0)
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": self.mouse_path})

View File

@ -31,52 +31,36 @@
* \{ */
#define PYDOC_BUILTIN_SHADER_DESCRIPTION \
"``2D_FLAT_COLOR``\n" \
" :Attributes: vec2 pos, vec4 color\n" \
" :Uniforms: none\n" \
"``2D_IMAGE``\n" \
" :Attributes: vec2 pos, vec2 texCoord\n" \
" :Uniforms: sampler2D image\n" \
"``2D_SMOOTH_COLOR``\n" \
" :Attributes: vec2 pos, vec4 color\n" \
" :Uniforms: none\n" \
"``2D_UNIFORM_COLOR``\n" \
" :Attributes: vec2 pos\n" \
" :Uniforms: vec4 color\n" \
"``3D_FLAT_COLOR``\n" \
"``FLAT_COLOR``\n" \
" :Attributes: vec3 pos, vec4 color\n" \
" :Uniforms: none\n" \
"``3D_IMAGE``\n" \
"``IMAGE``\n" \
" :Attributes: vec3 pos, vec2 texCoord\n" \
" :Uniforms: sampler2D image\n" \
"``3D_SMOOTH_COLOR``\n" \
"``SMOOTH_COLOR``\n" \
" :Attributes: vec3 pos, vec4 color\n" \
" :Uniforms: none\n" \
"``3D_UNIFORM_COLOR``\n" \
"``UNIFORM_COLOR``\n" \
" :Attributes: vec3 pos\n" \
" :Uniforms: vec4 color\n" \
"``3D_POLYLINE_FLAT_COLOR``\n" \
"``POLYLINE_FLAT_COLOR``\n" \
" :Attributes: vec3 pos, vec4 color\n" \
" :Uniforms: vec2 viewportSize, float lineWidth\n" \
"``3D_POLYLINE_SMOOTH_COLOR``\n" \
"``POLYLINE_SMOOTH_COLOR``\n" \
" :Attributes: vec3 pos, vec4 color\n" \
" :Uniforms: vec2 viewportSize, float lineWidth\n" \
"``3D_POLYLINE_UNIFORM_COLOR``\n" \
"``POLYLINE_UNIFORM_COLOR``\n" \
" :Attributes: vec3 pos\n" \
" :Uniforms: vec2 viewportSize, float lineWidth\n"
static const struct PyC_StringEnumItems pygpu_shader_builtin_items[] = {
{GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"},
{GPU_SHADER_2D_IMAGE, "2D_IMAGE"},
{GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"},
{GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"},
{GPU_SHADER_3D_FLAT_COLOR, "3D_FLAT_COLOR"},
{GPU_SHADER_3D_IMAGE, "3D_IMAGE"},
{GPU_SHADER_3D_SMOOTH_COLOR, "3D_SMOOTH_COLOR"},
{GPU_SHADER_3D_UNIFORM_COLOR, "3D_UNIFORM_COLOR"},
{GPU_SHADER_3D_POLYLINE_FLAT_COLOR, "3D_POLYLINE_FLAT_COLOR"},
{GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR, "3D_POLYLINE_SMOOTH_COLOR"},
{GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "3D_POLYLINE_UNIFORM_COLOR"},
{GPU_SHADER_3D_FLAT_COLOR, "FLAT_COLOR"},
{GPU_SHADER_3D_IMAGE, "IMAGE"},
{GPU_SHADER_3D_SMOOTH_COLOR, "SMOOTH_COLOR"},
{GPU_SHADER_3D_UNIFORM_COLOR, "UNIFORM_COLOR"},
{GPU_SHADER_3D_POLYLINE_FLAT_COLOR, "POLYLINE_FLAT_COLOR"},
{GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR, "POLYLINE_SMOOTH_COLOR"},
{GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "POLYLINE_UNIFORM_COLOR"},
{0, NULL},
};
@ -784,6 +768,24 @@ PyTypeObject BPyGPUShader_Type = {
/** \name gpu.shader Module API
* \{ */
static int pyc_parse_buitinshader_w_backward_compatibility(PyObject *o, void *p)
{
struct PyC_StringEnum *e = p;
const char *value = PyUnicode_AsUTF8(o);
if (value && ELEM(value[0], u'2', u'3')) {
/* Deprecated enums that start with "3D_" or "2D_". */
value += 3;
for (int i = 0; e->items[i].id; i++) {
if (STREQ(e->items[i].id, value)) {
e->value_found = e->items[i].value;
return 1;
}
}
}
return PyC_ParseStringEnum(o, p);
}
PyDoc_STRVAR(pygpu_shader_unbind_doc,
".. function:: unbind()\n"
"\n"
@ -834,7 +836,7 @@ static PyObject *pygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg
if (!_PyArg_ParseTupleAndKeywordsFast(args,
kwds,
&_parser,
PyC_ParseStringEnum,
pyc_parse_buitinshader_w_backward_compatibility,
&pygpu_bultinshader,
PyC_ParseStringEnum,
&pygpu_config)) {