Cleanup: GPU Python: Use 'PyC_ParseStringEnum' for string enum
This commit is contained in:
@@ -39,41 +39,17 @@
|
|||||||
/** \name Enum Conversion.
|
/** \name Enum Conversion.
|
||||||
* \{ */
|
* \{ */
|
||||||
|
|
||||||
static int bpygpu_ParseBultinShaderEnum(PyObject *o, void *p)
|
static const struct PyC_StringEnumItems pygpu_bultinshader_items[] = {
|
||||||
{
|
{GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"},
|
||||||
Py_ssize_t mode_id_len;
|
{GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"},
|
||||||
const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
|
{GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"},
|
||||||
if (mode_id == NULL) {
|
{GPU_SHADER_2D_IMAGE, "2D_IMAGE"},
|
||||||
PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
|
{GPU_SHADER_3D_UNIFORM_COLOR, "3D_UNIFORM_COLOR"},
|
||||||
return 0;
|
{GPU_SHADER_3D_FLAT_COLOR, "3D_FLAT_COLOR"},
|
||||||
}
|
{GPU_SHADER_3D_SMOOTH_COLOR, "3D_SMOOTH_COLOR"},
|
||||||
#define MATCH_ID(id) \
|
{GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "3D_POLYLINE_UNIFORM_COLOR"},
|
||||||
if (mode_id_len == (Py_ssize_t)strlen(STRINGIFY(id))) { \
|
{0, NULL},
|
||||||
if (STREQ(mode_id, STRINGIFY(id))) { \
|
};
|
||||||
mode = GPU_SHADER_##id; \
|
|
||||||
goto success; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
((void)0)
|
|
||||||
|
|
||||||
eGPUBuiltinShader mode;
|
|
||||||
MATCH_ID(2D_UNIFORM_COLOR);
|
|
||||||
MATCH_ID(2D_FLAT_COLOR);
|
|
||||||
MATCH_ID(2D_SMOOTH_COLOR);
|
|
||||||
MATCH_ID(2D_IMAGE);
|
|
||||||
MATCH_ID(3D_UNIFORM_COLOR);
|
|
||||||
MATCH_ID(3D_FLAT_COLOR);
|
|
||||||
MATCH_ID(3D_SMOOTH_COLOR);
|
|
||||||
MATCH_ID(3D_POLYLINE_UNIFORM_COLOR);
|
|
||||||
|
|
||||||
#undef MATCH_ID
|
|
||||||
PyErr_Format(PyExc_ValueError, "unknown type literal: '%s'", mode_id);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
success:
|
|
||||||
(*(eGPUBuiltinShader *)p) = mode;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int bpygpu_uniform_location_get(GPUShader *shader,
|
static int bpygpu_uniform_location_get(GPUShader *shader,
|
||||||
const char *name,
|
const char *name,
|
||||||
@@ -672,13 +648,12 @@ static PyObject *bpygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *ar
|
|||||||
{
|
{
|
||||||
BPYGPU_IS_INIT_OR_ERROR_OBJ;
|
BPYGPU_IS_INIT_OR_ERROR_OBJ;
|
||||||
|
|
||||||
eGPUBuiltinShader shader_id;
|
const struct PyC_StringEnum pygpu_bultinshader = {&pygpu_bultinshader_items, -1};
|
||||||
|
if (!PyC_ParseStringEnum(arg, &pygpu_bultinshader)) {
|
||||||
if (!bpygpu_ParseBultinShaderEnum(arg, &shader_id)) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
GPUShader *shader = GPU_shader_get_builtin_shader(shader_id);
|
GPUShader *shader = GPU_shader_get_builtin_shader(pygpu_bultinshader.value_found);
|
||||||
|
|
||||||
return BPyGPUShader_CreatePyObject(shader, true);
|
return BPyGPUShader_CreatePyObject(shader, true);
|
||||||
}
|
}
|
||||||
@@ -701,8 +676,6 @@ PyDoc_STRVAR(bpygpu_shader_code_from_builtin_doc,
|
|||||||
" :rtype: dict\n");
|
" :rtype: dict\n");
|
||||||
static PyObject *bpygpu_shader_code_from_builtin(BPyGPUShader *UNUSED(self), PyObject *arg)
|
static PyObject *bpygpu_shader_code_from_builtin(BPyGPUShader *UNUSED(self), PyObject *arg)
|
||||||
{
|
{
|
||||||
eGPUBuiltinShader shader_id;
|
|
||||||
|
|
||||||
const char *vert;
|
const char *vert;
|
||||||
const char *frag;
|
const char *frag;
|
||||||
const char *geom;
|
const char *geom;
|
||||||
@@ -710,11 +683,13 @@ static PyObject *bpygpu_shader_code_from_builtin(BPyGPUShader *UNUSED(self), PyO
|
|||||||
|
|
||||||
PyObject *item, *r_dict;
|
PyObject *item, *r_dict;
|
||||||
|
|
||||||
if (!bpygpu_ParseBultinShaderEnum(arg, &shader_id)) {
|
const struct PyC_StringEnum pygpu_bultinshader = {&pygpu_bultinshader_items, -1};
|
||||||
|
if (!PyC_ParseStringEnum(arg, &pygpu_bultinshader)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
GPU_shader_get_builtin_shader_code(shader_id, &vert, &frag, &geom, &defines);
|
GPU_shader_get_builtin_shader_code(
|
||||||
|
pygpu_bultinshader.value_found, &vert, &frag, &geom, &defines);
|
||||||
|
|
||||||
r_dict = PyDict_New();
|
r_dict = PyDict_New();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user