38#define PYDOC_BUILTIN_SHADER_DESCRIPTION \
40 " :Attributes: vec3 pos, vec4 color\n" \
41 " :Uniforms: none\n" \
43 " :Attributes: vec3 pos, vec2 texCoord\n" \
44 " :Uniforms: sampler2D image\n" \
45 "``IMAGE_SCENE_LINEAR_TO_REC709_SRGB``\n" \
46 " :Attributes: vec3 pos, vec2 texCoord\n" \
47 " :Uniforms: sampler2D image\n" \
48 " :Note: Expect texture to be in scene linear color space\n" \
50 " :Attributes: vec3 pos, vec2 texCoord\n" \
51 " :Uniforms: sampler2D image, vec4 color\n" \
52 "``IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB``\n" \
53 " :Attributes: vec3 pos, vec2 texCoord\n" \
54 " :Uniforms: sampler2D image, vec4 color\n" \
55 " :Note: Expect texture to be in scene linear color space\n" \
56 "``SMOOTH_COLOR``\n" \
57 " :Attributes: vec3 pos, vec4 color\n" \
58 " :Uniforms: none\n" \
59 "``UNIFORM_COLOR``\n" \
60 " :Attributes: vec3 pos\n" \
61 " :Uniforms: vec4 color\n" \
62 "``POLYLINE_FLAT_COLOR``\n" \
63 " :Attributes: vec3 pos, vec4 color\n" \
64 " :Uniforms: vec2 viewportSize, float lineWidth\n" \
65 "``POLYLINE_SMOOTH_COLOR``\n" \
66 " :Attributes: vec3 pos, vec4 color\n" \
67 " :Uniforms: vec2 viewportSize, float lineWidth\n" \
68 "``POLYLINE_UNIFORM_COLOR``\n" \
69 " :Attributes: vec3 pos\n" \
70 " :Uniforms: vec2 viewportSize, float lineWidth, vec4 color\n" \
71 "``POINT_FLAT_COLOR``\n" \
72 " :Attributes: vec3 pos, vec4 color\n" \
73 " :Uniforms: float size\n" \
74 "``POINT_UNIFORM_COLOR``\n" \
75 " :Attributes: vec3 pos\n" \
76 " :Uniforms: vec4 color, float size\n"
84 "IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB"},
103 const char *error_prefix)
108 PyErr_Format(PyExc_ValueError,
"%s: uniform %.32s not found", error_prefix,
name);
122 pygpu_shader_bind_doc,
123 ".. method:: bind()\n"
125 " Bind the shader object. Required to be able to change uniforms of this shader.\n");
134 pygpu_shader_uniform_from_name_doc,
135 ".. method:: uniform_from_name(name)\n"
137 " Get uniform location by name.\n"
139 " :arg name: Name of the uniform variable whose location is to be queried.\n"
141 " :return: Location of the uniform variable.\n"
145 const char *
name = PyUnicode_AsUTF8(arg);
146 if (
name ==
nullptr) {
151 self->shader,
name,
"GPUShader.get_uniform");
157 return PyLong_FromLong(uniform);
162 pygpu_shader_uniform_block_from_name_doc,
163 ".. method:: uniform_block_from_name(name)\n"
165 " Get uniform block location by name.\n"
167 " :arg name: Name of the uniform block variable whose location is to be queried.\n"
169 " :return: The location of the uniform block variable.\n"
173 const char *
name = PyUnicode_AsUTF8(arg);
174 if (
name ==
nullptr) {
181 PyErr_Format(PyExc_ValueError,
"GPUShader.get_uniform_block: uniform %.32s not found",
name);
185 return PyLong_FromLong(uniform);
193 Py_buffer *r_pybuffer)
198 if (!PyArg_ParseTuple(
199 args,
"iOi|i:GPUShader.uniform_vector_*", r_location, &buffer, r_length, r_count))
204 if (PyObject_GetBuffer(buffer, r_pybuffer, PyBUF_SIMPLE) == -1) {
209 if (r_pybuffer->len < (*r_length * *r_count * elem_size)) {
210 PyErr_SetString(PyExc_OverflowError,
211 "GPUShader.uniform_vector_*: buffer size smaller than required.");
220 pygpu_shader_uniform_vector_float_doc,
221 ".. method:: uniform_vector_float(location, buffer, length, count)\n"
223 " Set the buffer to fill the uniform.\n"
225 " :arg location: Location of the uniform variable to be modified.\n"
226 " :type location: int\n"
227 " :arg buffer: The data that should be set. Can support the buffer protocol.\n"
228 " :type buffer: Sequence[float]\n"
229 " :arg length: Size of the uniform data type:\n"
232 " - 2: vec2 or float[2]\n"
233 " - 3: vec3 or float[3]\n"
234 " - 4: vec4 or float[4]\n"
237 " :type length: int\n"
238 " :arg count: Specifies the number of elements, vector or matrices that are to "
240 " :type count: int\n");
248 args,
sizeof(
float), &location, &
length, &
count, &pybuffer))
255 self->shader, location,
length,
count,
static_cast<const float *
>(pybuffer.buf));
257 PyBuffer_Release(&pybuffer);
264 pygpu_shader_uniform_vector_int_doc,
265 ".. method:: uniform_vector_int(location, buffer, length, count)\n"
267 " See GPUShader.uniform_vector_float(...) description.\n");
281 self->shader, location,
length,
count,
static_cast<const int *
>(pybuffer.buf));
283 PyBuffer_Release(&pybuffer);
290 pygpu_shader_uniform_bool_doc,
291 ".. method:: uniform_bool(name, value)\n"
293 " Specify the value of a uniform variable for the current program object.\n"
295 " :arg name: Name of the uniform variable whose value is to be changed.\n"
297 " :arg value: Value that will be used to update the specified uniform variable.\n"
298 " :type value: bool | Sequence[bool]\n");
301 const char *error_prefix =
"GPUShader.uniform_bool";
308 if (!PyArg_ParseTuple(args,
"sO:GPUShader.uniform_bool", &
params.id, &
params.seq)) {
315 if (PySequence_Check(
params.seq)) {
316 PyObject *seq_fast = PySequence_Fast(
params.seq, error_prefix);
317 if (seq_fast ==
nullptr) {
318 PyErr_Format(PyExc_TypeError,
319 "%s: expected a sequence, got %s",
321 Py_TYPE(
params.seq)->tp_name);
324 length = PySequence_Fast_GET_SIZE(seq_fast);
326 PyErr_Format(PyExc_TypeError,
327 "%s: invalid sequence length. expected 1..4, got %d",
333 values,
sizeof(*values), seq_fast,
length, &PyLong_Type, error_prefix);
338 else if (((values[0] =
int(PyLong_AsLong(
params.seq))) != -1) &&
ELEM(values[0], 0, 1)) {
344 PyExc_ValueError,
"expected a bool or sequence, got %s", Py_TYPE(
params.seq)->tp_name);
353 if (location == -1) {
365 pygpu_shader_uniform_float_doc,
366 ".. method:: uniform_float(name, value)\n"
368 " Specify the value of a uniform variable for the current program object.\n"
370 " :arg name: Name of the uniform variable whose value is to be changed.\n"
372 " :arg value: Value that will be used to update the specified uniform variable.\n"
373 " :type value: float | Sequence[float]\n");
376 const char *error_prefix =
"GPUShader.uniform_float";
383 if (!PyArg_ParseTuple(args,
"sO:GPUShader.uniform_float", &
params.id, &
params.seq)) {
390 if (PyFloat_Check(
params.seq)) {
394 else if (PyLong_Check(
params.seq)) {
404 PyErr_SetString(PyExc_ValueError,
"Expected 3x3 or 4x4 matrix");
408 memcpy(values, mat->matrix,
sizeof(
float) *
length);
418 PyErr_SetString(PyExc_TypeError,
419 "Expected a single float or a sequence of floats of length 1..4, 9 or 16.");
425 if (location == -1) {
437 pygpu_shader_uniform_int_doc,
438 ".. method:: uniform_int(name, seq)\n"
440 " Specify the value of a uniform variable for the current program object.\n"
442 " :arg name: name of the uniform variable whose value is to be changed.\n"
444 " :arg seq: Value that will be used to update the specified uniform variable.\n"
445 " :type seq: Sequence[int]\n");
448 const char *error_prefix =
"GPUShader.uniform_int";
455 if (!PyArg_ParseTuple(args,
"sO:GPUShader.uniform_int", &
params.id, &
params.seq)) {
463 if (PyLong_Check(
params.seq)) {
464 values[0] = PyC_Long_AsI32(
params.seq);
469 PyObject *seq_fast = PySequence_Fast(
params.seq, error_prefix);
470 if (seq_fast ==
nullptr) {
471 PyErr_Format(PyExc_TypeError,
472 "%s: expected a sequence, got %s",
474 Py_TYPE(
params.seq)->tp_name);
478 length = PySequence_Fast_GET_SIZE(seq_fast);
480 PyErr_Format(PyExc_TypeError,
481 "%s: invalid sequence length. expected 1..4, got %d",
488 values,
sizeof(*values), seq_fast,
length, &PyLong_Type, error_prefix);
499 if (location == -1) {
511 pygpu_shader_uniform_sampler_doc,
512 ".. method:: uniform_sampler(name, texture)\n"
514 " Specify the value of a texture uniform variable for the current GPUShader.\n"
516 " :arg name: name of the uniform variable whose texture is to be specified.\n"
518 " :arg texture: Texture to attach.\n"
519 " :type texture: :class:`gpu.types.GPUTexture`\n");
524 if (!PyArg_ParseTuple(
540 pygpu_shader_image_doc,
541 ".. method:: image(name, texture)\n"
543 " Specify the value of an image variable for the current GPUShader.\n"
545 " :arg name: Name of the image variable to which the texture is to be bound.\n"
547 " :arg texture: Texture to attach.\n"
548 " :type texture: :class:`gpu.types.GPUTexture`\n");
559 if (image_unit == -1) {
560 PyErr_Format(PyExc_ValueError,
"Image '%s' not found in shader",
name);
571 pygpu_shader_uniform_block_doc,
572 ".. method:: uniform_block(name, ubo)\n"
574 " Specify the value of an uniform buffer object variable for the current GPUShader.\n"
576 " :arg name: name of the uniform variable whose UBO is to be specified.\n"
578 " :arg ubo: Uniform Buffer to attach.\n"
579 " :type texture: :class:`gpu.types.GPUUniformBuf`\n");
584 if (!PyArg_ParseTuple(
594 "GPUShader.uniform_block: uniform block not found, make sure the name is correct");
606 pygpu_shader_attr_from_name_doc,
607 ".. method:: attr_from_name(name)\n"
609 " Get attribute location by name.\n"
611 " :arg name: The name of the attribute variable whose location is to be queried.\n"
613 " :return: The location of an attribute variable.\n"
617 const char *
name = PyUnicode_AsUTF8(arg);
618 if (
name ==
nullptr) {
625 PyErr_Format(PyExc_ValueError,
"GPUShader.attr_from_name: attribute %.32s not found",
name);
629 return PyLong_FromLong(attr);
634 pygpu_shader_format_calc_doc,
635 ".. method:: format_calc()\n"
637 " Build a new format based on the attributes of the shader.\n"
639 " :return: vertex attribute format for the shader\n"
640 " :rtype: :class:`gpu.types.GPUVertFormat`\n");
658 return (PyObject *)
ret;
663 pygpu_shader_attrs_info_get_doc,
664 ".. method:: attrs_info_get()\n"
666 " Information about the attributes used in the Shader.\n"
668 " :return: tuples containing information about the attributes in order (name, type)\n"
669 " :rtype: tuple[tuple[str, str | None], ...]\n");
675 int location_test = 0, attrs_added = 0;
684 ret = PyTuple_New(input_len);
685 while (attrs_added < input_len) {
698 py_type = PyUnicode_InternFromString(
706 PyObject *attr_info = PyTuple_New(2);
708 PyTuple_SetItem(
ret, attrs_added, attr_info);
715 ret = PyTuple_New(attr_len);
716 while (attrs_added < attr_len) {
722 py_type = PyUnicode_InternFromString(
730 PyObject *attr_info = PyTuple_New(2);
732 PyTuple_SetItem(
ret, attrs_added, attr_info);
741# pragma clang diagnostic push
742# pragma clang diagnostic ignored "-Wcast-function-type"
744# pragma GCC diagnostic push
745# pragma GCC diagnostic ignored "-Wcast-function-type"
751 {
"uniform_from_name",
754 pygpu_shader_uniform_from_name_doc},
755 {
"uniform_block_from_name",
758 pygpu_shader_uniform_block_from_name_doc},
759 {
"uniform_vector_float",
762 pygpu_shader_uniform_vector_float_doc},
763 {
"uniform_vector_int",
766 pygpu_shader_uniform_vector_int_doc},
770 pygpu_shader_uniform_bool_doc},
774 pygpu_shader_uniform_float_doc},
778 pygpu_shader_uniform_int_doc},
782 pygpu_shader_uniform_sampler_doc},
787 pygpu_shader_uniform_block_doc},
791 pygpu_shader_attr_from_name_doc},
795 pygpu_shader_format_calc_doc},
799 pygpu_shader_attrs_info_get_doc},
800 {
nullptr,
nullptr, 0,
nullptr},
805# pragma clang diagnostic pop
807# pragma GCC diagnostic pop
813 pygpu_shader_name_doc,
814 "The name of the shader object for debugging purposes (read-only).\n"
824 pygpu_shader_program_doc,
825 "The name of the program object for use by the OpenGL API (read-only).\n"
826 "This is deprecated and will always return -1.\n"
832 PyExc_DeprecationWarning,
"'program' is deprecated. No valid handle will be returned.", 1);
833 return PyLong_FromLong(-1);
840 pygpu_shader_program_doc,
842 {
"name", (getter)
pygpu_shader_name, (setter)
nullptr, pygpu_shader_name_doc,
nullptr},
843 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
848 if (
self->is_builtin ==
false) {
851 Py_TYPE(
self)->tp_free((PyObject *)
self);
855 PyVarObject_HEAD_INIT(
nullptr, 0)
914 pygpu_shader_unbind_doc,
915 ".. function:: unbind()\n"
917 " Unbind the bound shader object.\n");
926 pygpu_shader_from_builtin_doc,
927 ".. function:: from_builtin(shader_name, *, config='DEFAULT')\n"
929 " Shaders that are embedded in the blender internal code (see :ref:`built-in-shaders`).\n"
930 " They all read the uniform ``mat4 ModelViewProjectionMatrix``,\n"
931 " which can be edited by the :mod:`gpu.matrix` module.\n"
933 " You can also choose a shader configuration that uses clip_planes by setting the "
934 "``CLIPPED`` value to the config parameter. Note that in this case you also need to "
935 "manually set the value of ``mat4 ModelMatrix``.\n"
937 " :arg shader_name: One of the builtin shader names.\n"
938 " :type shader_name: str\n"
939 " :arg config: One of these types of shader configuration:\n"
943 " :type config: str\n"
944 " :return: Shader object corresponding to the given name.\n"
945 " :rtype: :class:`gpu.types.GPUShader`\n");
953 static const char *_keywords[] = {
"shader_name",
"config",
nullptr};
954 static _PyArg_Parser _parser = {
963 if (!_PyArg_ParseTupleAndKeywordsFast(args,
978 PyErr_SetString(PyExc_ValueError,
"Builtin shader doesn't exist in the requested config");
987 pygpu_shader_create_from_info_doc,
988 ".. function:: create_from_info(shader_info)\n"
990 " Create shader from a GPUShaderCreateInfo.\n"
992 " :arg shader_info: GPUShaderCreateInfo\n"
993 " :type shader_info: :class:`gpu.types.GPUShaderCreateInfo`\n"
994 " :return: Shader object corresponding to the given name.\n"
995 " :rtype: :class:`gpu.types.GPUShader`\n");
1001 PyErr_Format(PyExc_TypeError,
"Expected a GPUShaderCreateInfo, got %s", Py_TYPE(o)->tp_name);
1007 PyErr_SetString(PyExc_Exception,
error);
1013 PyErr_SetString(PyExc_Exception,
"Shader Compile Error, see console for more details");
1022# pragma clang diagnostic push
1023# pragma clang diagnostic ignored "-Wcast-function-type"
1025# pragma GCC diagnostic push
1026# pragma GCC diagnostic ignored "-Wcast-function-type"
1034 METH_VARARGS | METH_KEYWORDS,
1035 pygpu_shader_from_builtin_doc},
1036 {
"create_from_info",
1039 pygpu_shader_create_from_info_doc},
1040 {
nullptr,
nullptr, 0,
nullptr},
1045# pragma clang diagnostic pop
1047# pragma GCC diagnostic pop
1053 pygpu_shader_module__tp_doc,
1054 "This module provides access to GPUShader internal functions.\n"
1056 ".. _built-in-shaders:\n"
1058 ".. rubric:: Built-in shaders\n"
1060 "All built-in shaders have the ``mat4 ModelViewProjectionMatrix`` uniform.\n"
1062 "Its value must be modified using the :class:`gpu.matrix` module.\n"
1066 " Shader uniforms must be explicitly initialized to avoid retaining values from previous "
1072 pygpu_shader_module__tp_doc,
1093 self->is_builtin = is_builtin;
1095 return (PyObject *)
self;
1100 PyObject *submodule;
blender::gpu::Shader * GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)
int GPU_shader_get_ubo_binding(blender::gpu::Shader *shader, const char *name)
int GPU_shader_get_uniform_block(blender::gpu::Shader *shader, const char *name)
void GPU_shader_free(blender::gpu::Shader *shader)
bool GPU_shader_get_attribute_info(const blender::gpu::Shader *shader, int attr_location, char r_name[256], int *r_type)
bool GPU_shader_get_ssbo_input_info(const blender::gpu::Shader *shader, int ssbo_location, char r_name[256])
int GPU_shader_get_sampler_binding(blender::gpu::Shader *shader, const char *name)
uint GPU_shader_get_attribute_len(const blender::gpu::Shader *shader)
uint GPU_shader_get_ssbo_input_len(const blender::gpu::Shader *shader)
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
int GPU_shader_get_ssbo_binding(blender::gpu::Shader *shader, const char *name)
void GPU_shader_uniform_int_ex(blender::gpu::Shader *shader, int location, int length, int array_size, const int *value)
void GPU_shader_uniform_1i(blender::gpu::Shader *sh, const char *name, int value)
const char * GPU_shader_get_name(blender::gpu::Shader *shader)
int GPU_shader_get_attribute(const blender::gpu::Shader *shader, const char *name)
int GPU_shader_get_uniform(blender::gpu::Shader *shader, const char *name)
bool GPU_shader_create_info_check_error(const GPUShaderCreateInfo *_info, char r_error[128])
void GPU_shader_uniform_float_ex(blender::gpu::Shader *shader, int location, int length, int array_size, const float *value)
blender::gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
@ GPU_SHADER_3D_SMOOTH_COLOR
@ GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_3D_POINT_FLAT_COLOR
@ GPU_SHADER_3D_UNIFORM_COLOR
@ GPU_SHADER_3D_FLAT_COLOR
@ GPU_SHADER_3D_POLYLINE_FLAT_COLOR
@ GPU_SHADER_3D_IMAGE_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_3D_POINT_UNIFORM_COLOR
@ GPU_SHADER_3D_IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_3D_IMAGE_COLOR
blender::gpu::Shader * GPU_shader_get_builtin_shader_with_config(GPUBuiltinShader shader, GPUShaderConfig sh_cfg)
void GPU_texture_image_bind(blender::gpu::Texture *texture, int unit)
void GPU_texture_bind(blender::gpu::Texture *texture, int unit)
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
#define PYDOC_BUILTIN_SHADER_DESCRIPTION
static PyObject * pygpu_shader_program_get(BPyGPUShader *, void *)
static const PyC_StringEnumItems pygpu_shader_builtin_items[]
static PyMethodDef pygpu_shader__tp_methods[]
static PyObject * pygpu_shader_unbind(BPyGPUShader *)
static PyObject * pygpu_shader_uniform_block(BPyGPUShader *self, PyObject *args)
static PyObject * pygpu_shader_uniform_vector_int(BPyGPUShader *self, PyObject *args)
bool bpygpu_shader_is_polyline(blender::gpu::Shader *shader)
static PyObject * pygpu_shader_uniform_from_name(BPyGPUShader *self, PyObject *arg)
static PyObject * pygpu_shader_uniform_int(BPyGPUShader *self, PyObject *args)
static int pygpu_shader_uniform_location_get(blender::gpu::Shader *shader, const char *name, const char *error_prefix)
static PyObject * pygpu_shader_create_from_info(BPyGPUShader *, BPyGPUShaderCreateInfo *o)
static PyObject * pygpu_shader_name(BPyGPUShader *self, void *)
PyTypeObject BPyGPUShader_Type
static PyObject * pygpu_shader_bind(BPyGPUShader *self)
static bool pygpu_shader_uniform_vector_impl(PyObject *args, int elem_size, int *r_location, int *r_length, int *r_count, Py_buffer *r_pybuffer)
PyObject * bpygpu_shader_init()
static PyObject * pygpu_shader_uniform_bool(BPyGPUShader *self, PyObject *args)
static PyObject * pygpu_shader_image(BPyGPUShader *self, PyObject *args)
static const PyC_StringEnumItems pygpu_shader_config_items[]
static void pygpu_shader__tp_dealloc(BPyGPUShader *self)
PyObject * BPyGPUShader_CreatePyObject(blender::gpu::Shader *shader, bool is_builtin)
static PyObject * pygpu_shader_from_builtin(PyObject *, PyObject *args, PyObject *kwds)
static PyObject * pygpu_shader_attr_from_name(BPyGPUShader *self, PyObject *arg)
static PyObject * pygpu_shader_format_calc(BPyGPUShader *self, PyObject *)
static PyObject * pygpu_shader_uniform_sampler(BPyGPUShader *self, PyObject *args)
static PyMethodDef pygpu_shader_module__tp_methods[]
static PyObject * pygpu_shader_uniform_float(BPyGPUShader *self, PyObject *args)
static PyObject * pygpu_shader_uniform_vector_float(BPyGPUShader *self, PyObject *args)
static PyObject * pygpu_shader_uniform_block_from_name(BPyGPUShader *self, PyObject *arg)
static PyObject * pygpu_shader_attrs_info_get(BPyGPUShader *self, PyObject *)
static PyModuleDef pygpu_shader_module_def
PyDoc_STRVAR(pygpu_shader_bind_doc, ".. method:: bind()\n" "\n" " Bind the shader object. Required to be able to change uniforms of this shader.\n")
static PyGetSetDef pygpu_shader__tp_getseters[]
const struct PyC_StringEnumItems pygpu_attrtype_items[]
#define BPyGPUShaderCreateInfo_Check(v)
PyTypeObject BPyGPUTexture_Type
float length(VecOp< float, D >) RET
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
#define BaseMath_ReadCallback(_self)
#define MatrixObject_Check(v)
static void error(const char *str)
int PyC_ParseStringEnum(PyObject *o, void *p)
const char * PyC_StringEnum_FindIDFromValue(const PyC_StringEnumItems *items, const int value)
int PyC_AsArray_FAST(void *array, const size_t array_item_size, PyObject *value_fast, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
#define PyTuple_SET_ITEMS(op_arg,...)
PyObject_VAR_HEAD GPUShaderCreateInfo * info
PyObject_HEAD blender::gpu::Texture * tex