Blender V5.3
gpu_py_shader_create_info.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include "BLI_utildefines.hh"
15
16#include "GPU_shader.hh"
18
20#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
21
22#include "gpu_py_shader.hh" /* own include */
23#include "gpu_py_texture.hh"
24
25namespace blender {
26
27#define USE_PYGPU_SHADER_INFO_IMAGE_METHOD
28
33using gpu::shader::ShaderCreateInfo;
34using gpu::shader::StageInterfaceInfo;
36
37#ifdef USE_PYGPU_SHADER_INFO_IMAGE_METHOD
39
40# define PYDOC_QUALIFIERS_LITERAL "Literal['NO_RESTRICT', 'READ', 'WRITE']"
41static const PyC_FlagSet pygpu_qualifiers[] = {
42 {int(Qualifier::no_restrict), "NO_RESTRICT"},
43 {int(Qualifier::read), "READ"},
44 {int(Qualifier::write), "WRITE"},
45 {0, nullptr},
46};
47#endif
48
49#define PYDOC_TYPE_LITERAL \
50 "Literal[" \
51 "'FLOAT', " \
52 "'VEC2', " \
53 "'VEC3', " \
54 "'VEC4', " \
55 "'MAT3', " \
56 "'MAT4', " \
57 "'UINT', " \
58 "'UVEC2', " \
59 "'UVEC3', " \
60 "'UVEC4', " \
61 "'INT', " \
62 "'IVEC2', " \
63 "'IVEC3', " \
64 "'IVEC4', " \
65 "'BOOL']"
67 {int(Type::float_t), "FLOAT"},
68 {int(Type::float2_t), "VEC2"},
69 {int(Type::float3_t), "VEC3"},
70 {int(Type::float4_t), "VEC4"},
71 {int(Type::float3x3_t), "MAT3"},
72 {int(Type::float4x4_t), "MAT4"},
73 {int(Type::uint_t), "UINT"},
74 {int(Type::uint2_t), "UVEC2"},
75 {int(Type::uint3_t), "UVEC3"},
76 {int(Type::uint4_t), "UVEC4"},
77 {int(Type::int_t), "INT"},
78 {int(Type::int2_t), "IVEC2"},
79 {int(Type::int3_t), "IVEC3"},
80 {int(Type::int4_t), "IVEC4"},
81 {int(Type::bool_t), "BOOL"},
82 {0, nullptr},
83};
84
85#define PYDOC_IMAGE_TYPES_LITERAL \
86 "Literal[" \
87 "'FLOAT_BUFFER', " \
88 "'FLOAT_1D', " \
89 "'FLOAT_1D_ARRAY', " \
90 "'FLOAT_2D', " \
91 "'FLOAT_2D_ARRAY', " \
92 "'FLOAT_3D', " \
93 "'FLOAT_CUBE', " \
94 "'FLOAT_CUBE_ARRAY', " \
95 "'INT_BUFFER', " \
96 "'INT_1D', " \
97 "'INT_1D_ARRAY', " \
98 "'INT_2D', " \
99 "'INT_2D_ARRAY', " \
100 "'INT_3D', " \
101 "'INT_CUBE', " \
102 "'INT_CUBE_ARRAY', " \
103 "'UINT_BUFFER', " \
104 "'UINT_1D', " \
105 "'UINT_1D_ARRAY', " \
106 "'UINT_2D', " \
107 "'UINT_2D_ARRAY', " \
108 "'UINT_3D', " \
109 "'UINT_CUBE', " \
110 "'UINT_CUBE_ARRAY', " \
111 "'SHADOW_2D', " \
112 "'SHADOW_2D_ARRAY', " \
113 "'SHADOW_CUBE', " \
114 "'SHADOW_CUBE_ARRAY', " \
115 "'DEPTH_2D', " \
116 "'DEPTH_2D_ARRAY', " \
117 "'DEPTH_CUBE', " \
118 "'DEPTH_CUBE_ARRAY']"
120 {int(ImageType::FloatBuffer), "FLOAT_BUFFER"},
121 {int(ImageType::Float1D), "FLOAT_1D"},
122 {int(ImageType::Float1DArray), "FLOAT_1D_ARRAY"},
123 {int(ImageType::Float2D), "FLOAT_2D"},
124 {int(ImageType::Float2DArray), "FLOAT_2D_ARRAY"},
125 {int(ImageType::Float3D), "FLOAT_3D"},
126 {int(ImageType::FloatCube), "FLOAT_CUBE"},
127 {int(ImageType::FloatCubeArray), "FLOAT_CUBE_ARRAY"},
128 {int(ImageType::IntBuffer), "INT_BUFFER"},
129 {int(ImageType::Int1D), "INT_1D"},
130 {int(ImageType::Int1DArray), "INT_1D_ARRAY"},
131 {int(ImageType::Int2D), "INT_2D"},
132 {int(ImageType::Int2DArray), "INT_2D_ARRAY"},
133 {int(ImageType::Int3D), "INT_3D"},
134 {int(ImageType::IntCube), "INT_CUBE"},
135 {int(ImageType::IntCubeArray), "INT_CUBE_ARRAY"},
136 {int(ImageType::AtomicInt2D), "INT_2D_ATOMIC"},
137 {int(ImageType::AtomicInt2DArray), "INT_2D_ARRAY_ATOMIC"},
138 {int(ImageType::AtomicInt3D), "INT_3D_ATOMIC"},
139 {int(ImageType::UintBuffer), "UINT_BUFFER"},
140 {int(ImageType::Uint1D), "UINT_1D"},
141 {int(ImageType::Uint1DArray), "UINT_1D_ARRAY"},
142 {int(ImageType::Uint2D), "UINT_2D"},
143 {int(ImageType::Uint2DArray), "UINT_2D_ARRAY"},
144 {int(ImageType::Uint3D), "UINT_3D"},
145 {int(ImageType::UintCube), "UINT_CUBE"},
146 {int(ImageType::UintCubeArray), "UINT_CUBE_ARRAY"},
147 {int(ImageType::AtomicUint2D), "UINT_2D_ATOMIC"},
148 {int(ImageType::AtomicUint2DArray), "UINT_2D_ARRAY_ATOMIC"},
149 {int(ImageType::AtomicUint3D), "UINT_3D_ATOMIC"},
150 {int(ImageType::Shadow2D), "SHADOW_2D"},
151 {int(ImageType::Shadow2DArray), "SHADOW_2D_ARRAY"},
152 {int(ImageType::ShadowCube), "SHADOW_CUBE"},
153 {int(ImageType::ShadowCubeArray), "SHADOW_CUBE_ARRAY"},
154 {int(ImageType::Depth2D), "DEPTH_2D"},
155 {int(ImageType::Depth2DArray), "DEPTH_2D_ARRAY"},
156 {int(ImageType::DepthCube), "DEPTH_CUBE"},
157 {int(ImageType::DepthCubeArray), "DEPTH_CUBE_ARRAY"},
158 {0, nullptr},
159};
160
162 {int(DualBlend::NONE), "NONE"},
163 {int(DualBlend::SRC_0), "SRC_0"},
164 {int(DualBlend::SRC_1), "SRC_1"},
165 {0, nullptr},
166};
167
169 {int(DepthWrite::UNCHANGED), "UNCHANGED"},
170 {int(DepthWrite::ANY), "ANY"},
171 {int(DepthWrite::GREATER), "GREATER"},
172 {int(DepthWrite::LESS), "LESS"},
173 {0, nullptr},
174};
175
176#define PYDOC_TEX_FORMAT_LITERAL \
177 "Literal[" \
178 "'RGBA8UI', " \
179 "'RGBA8I', " \
180 "'RGBA8', " \
181 "'RGBA32UI', " \
182 "'RGBA32I', " \
183 "'RGBA32F', " \
184 "'RGBA16UI', " \
185 "'RGBA16I', " \
186 "'RGBA16F', " \
187 "'RGBA16', " \
188 "'RG8UI', " \
189 "'RG8I', " \
190 "'RG8', " \
191 "'RG32UI', " \
192 "'RG32I', " \
193 "'RG32F', " \
194 "'RG16UI', " \
195 "'RG16I', " \
196 "'RG16F', " \
197 "'RG16', " \
198 "'R8UI', " \
199 "'R8I', " \
200 "'R8', " \
201 "'R32UI', " \
202 "'R32I', " \
203 "'R32F', " \
204 "'R16UI', " \
205 "'R16I', " \
206 "'R16F', " \
207 "'R16', " \
208 "'R11F_G11F_B10F', " \
209 "'DEPTH32F_STENCIL8', " \
210 "'DEPTH24_STENCIL8', " \
211 "'SRGB8_A8', " \
212 "'RGB16F', " \
213 "'SRGB8_A8_DXT1', " \
214 "'SRGB8_A8_DXT3', " \
215 "'SRGB8_A8_DXT5', " \
216 "'RGBA8_DXT1', " \
217 "'RGBA8_DXT3', " \
218 "'RGBA8_DXT5', " \
219 "'DEPTH_COMPONENT32F', " \
220 "'DEPTH_COMPONENT24', " \
221 "'DEPTH_COMPONENT16']"
222extern const PyC_StringEnumItems pygpu_tex_format_items[];
223
224/* -------------------------------------------------------------------- */
227
229 PyObject *args,
230 const char *format,
231 Type *r_type,
232 const char **r_name)
233{
235 PyObject *py_name;
236
237 if (!PyArg_ParseTuple(args, format, PyC_ParseStringEnum, &pygpu_type, &py_name)) {
238 return false;
239 }
240
241 const char *name = PyUnicode_AsUTF8(py_name);
242 if (name == nullptr) {
243 return false;
244 }
245
246#ifdef USE_GPU_PY_REFERENCES
247 PyList_Append(self->references, py_name);
248#endif
249
250 *r_type = Type(pygpu_type.value_found);
251 *r_name = name;
252 return true;
253}
254
256 /* Wrap. */
257 pygpu_interface_info_smooth_doc,
258 ".. method:: smooth(type, name)\n"
259 "\n"
260 " Add an attribute with qualifier of type *smooth* to the interface block.\n"
261 "\n"
262 " :param type: The data type of the attribute.\n"
263 " :type type: " PYDOC_TYPE_LITERAL
264 "\n"
265 " :param name: name of the attribute.\n"
266 " :type name: str\n");
267static PyObject *pygpu_interface_info_smooth(BPyGPUStageInterfaceInfo *self, PyObject *args)
268{
269 Type type;
270 const char *name;
272 args,
273 "O&" /* `type` */
274 "O" /* `name` */
275 ":smooth",
276 &type,
277 &name))
278 {
279 return nullptr;
280 }
281
282 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(self->interface);
283 interface->smooth(type, name);
284 Py_RETURN_NONE;
285}
286
288 /* Wrap. */
289 pygpu_interface_info_flat_doc,
290 ".. method:: flat(type, name)\n"
291 "\n"
292 " Add an attribute with qualifier of type ``flat`` to the interface block.\n"
293 "\n"
294 " :param type: The data type of the attribute.\n"
295 " :type type: " PYDOC_TYPE_LITERAL
296 "\n"
297 " :param name: name of the attribute.\n"
298 " :type name: str\n");
299static PyObject *pygpu_interface_info_flat(BPyGPUStageInterfaceInfo *self, PyObject *args)
300{
301 Type type;
302 const char *name;
304 args,
305 "O&" /* `type` */
306 "O" /* `name` */
307 ":flat",
308 &type,
309 &name))
310 {
311 return nullptr;
312 }
313
314 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(self->interface);
315 interface->flat(type, name);
316 Py_RETURN_NONE;
317}
318
320 /* Wrap. */
321 pygpu_interface_info_no_perspective_doc,
322 ".. method:: no_perspective(type, name)\n"
323 "\n"
324 " Add an attribute with qualifier of type ``no_perspective`` to the interface block.\n"
325 "\n"
326 " :param type: The data type of the attribute.\n"
327 " :type type: " PYDOC_TYPE_LITERAL
328 "\n"
329 " :param name: name of the attribute.\n"
330 " :type name: str\n");
332 PyObject *args)
333{
334 Type type;
335 const char *name;
337 args,
338 "O&" /* `type` */
339 "O" /* `name` */
340 ":no_perspective",
341 &type,
342 &name))
343 {
344 return nullptr;
345 }
346
347 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(self->interface);
348 interface->no_perspective(type, name);
349 Py_RETURN_NONE;
350}
351
352static PyMethodDef pygpu_interface_info__tp_methods[] = {
353 {"smooth",
354 reinterpret_cast<PyCFunction>(pygpu_interface_info_smooth),
355 METH_VARARGS,
356 pygpu_interface_info_smooth_doc},
357 {"flat",
358 reinterpret_cast<PyCFunction>(pygpu_interface_info_flat),
359 METH_VARARGS,
360 pygpu_interface_info_flat_doc},
361 {"no_perspective",
362 reinterpret_cast<PyCFunction>(pygpu_interface_info_no_perspective),
363 METH_VARARGS,
364 pygpu_interface_info_no_perspective_doc},
365 {nullptr, nullptr, 0, nullptr},
366};
367
369
370/* -------------------------------------------------------------------- */
373
375 /* Wrap. */
376 pygpu_interface_info_name_doc,
377 "Name of the interface block.\n"
378 "\n"
379 ":type: str\n");
380static PyObject *pygpu_interface_info_name_get(BPyGPUStageInterfaceInfo *self, void * /*closure*/)
381{
382 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(self->interface);
383 return PyC_UnicodeFromStdStr(interface->name);
384}
385
387 {"name",
388 reinterpret_cast<getter>(pygpu_interface_info_name_get),
389 static_cast<setter>(nullptr),
390 pygpu_interface_info_name_doc,
391 nullptr},
392 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
393};
394
396
397/* -------------------------------------------------------------------- */
400
401static PyObject *pygpu_interface_info__tp_new(PyTypeObject * /*type*/,
402 PyObject *args,
403 PyObject *kwds)
404{
405 if (kwds) {
406 PyErr_SetString(PyExc_TypeError, "no keywords are expected");
407 return nullptr;
408 }
409
410 const char *name;
411 if (!PyArg_ParseTuple(args,
412 "s" /* `name` */
413 ":GPUStageInterfaceInfo.__new__*",
414 &name))
415 {
416 return nullptr;
417 }
418
419 StageInterfaceInfo *interface = new StageInterfaceInfo(name, "");
420 GPUStageInterfaceInfo *interface_info = reinterpret_cast<GPUStageInterfaceInfo *>(interface);
421
422 PyObject *self = BPyGPUStageInterfaceInfo_CreatePyObject(interface_info);
423
424#ifdef USE_GPU_PY_REFERENCES
425 PyObject *py_name = PyTuple_GET_ITEM(args, 0);
426 PyList_Append((reinterpret_cast<BPyGPUStageInterfaceInfo *>(self))->references, py_name);
427#endif
428
429 return self;
430}
431
432#ifdef USE_GPU_PY_REFERENCES
433
434static int pygpu_interface_info__tp_traverse(PyObject *self, visitproc visit, void *arg)
435{
436 BPyGPUStageInterfaceInfo *py_interface = reinterpret_cast<BPyGPUStageInterfaceInfo *>(self);
437 Py_VISIT(py_interface->references);
438 return 0;
439}
440
441static int pygpu_interface_info__tp_clear(PyObject *self)
442{
443 BPyGPUStageInterfaceInfo *py_interface = reinterpret_cast<BPyGPUStageInterfaceInfo *>(self);
444 Py_CLEAR(py_interface->references);
445 return 0;
446}
447
448#endif
449
450static void pygpu_interface_info__tp_dealloc(PyObject *self)
451{
452 BPyGPUStageInterfaceInfo *py_interface = reinterpret_cast<BPyGPUStageInterfaceInfo *>(self);
453 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(py_interface->interface);
454 delete interface;
455
456#ifdef USE_GPU_PY_REFERENCES
457 PyObject_GC_UnTrack(self);
458 if (py_interface->references) {
460 Py_CLEAR(py_interface->references);
461 }
462#endif
463
464 Py_TYPE(self)->tp_free(self);
465}
466
468 /* Wrap. */
469 pygpu_interface_info__tp_doc,
470 ".. class:: GPUStageInterfaceInfo\n"
471 "\n"
472 " List of varyings between shader stages.\n"
473 "\n"
474 " .. method:: __init__(name)\n"
475 "\n"
476 " :param name: Name of the interface block.\n"
477 " :type name: str\n");
479 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
480 /*tp_name*/ "GPUStageInterfaceInfo",
481 /*tp_basicsize*/ sizeof(BPyGPUStageInterfaceInfo),
482 /*tp_itemsize*/ 0,
484 /*tp_vectorcall_offset*/ 0,
485 /*tp_getattr*/ nullptr,
486 /*tp_setattr*/ nullptr,
487 /*tp_compare*/ nullptr,
488 /*tp_repr*/ nullptr,
489 /*tp_as_number*/ nullptr,
490 /*tp_as_sequence*/ nullptr,
491 /*tp_as_mapping*/ nullptr,
492 /*tp_hash*/ nullptr,
493 /*tp_call*/ nullptr,
494 /*tp_str*/ nullptr,
495 /*tp_getattro*/ nullptr,
496 /*tp_setattro*/ nullptr,
497 /*tp_as_buffer*/ nullptr,
499 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
500#else
501 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
502#endif
503 /*tp_doc*/ pygpu_interface_info__tp_doc,
505 /*tp_traverse*/ pygpu_interface_info__tp_traverse,
506#else
507 /*tp_traverse*/ nullptr,
508#endif
511#else
512 /*tp_clear*/ nullptr,
513#endif
514 /*tp_richcompare*/ nullptr,
515 /*tp_weaklistoffset*/ 0,
516 /*tp_iter*/ nullptr,
517 /*tp_iternext*/ nullptr,
519 /*tp_members*/ nullptr,
521 /*tp_base*/ nullptr,
522 /*tp_dict*/ nullptr,
523 /*tp_descr_get*/ nullptr,
524 /*tp_descr_set*/ nullptr,
525 /*tp_dictoffset*/ 0,
526 /*tp_init*/ nullptr,
527 /*tp_alloc*/ nullptr,
529 /*tp_free*/ nullptr,
530 /*tp_is_gc*/ nullptr,
531 /*tp_bases*/ nullptr,
532 /*tp_mro*/ nullptr,
533 /*tp_cache*/ nullptr,
534 /*tp_subclasses*/ nullptr,
535 /*tp_weaklist*/ nullptr,
536 /*tp_del*/ nullptr,
537 /*tp_version_tag*/ 0,
538 /*tp_finalize*/ nullptr,
539 /*tp_vectorcall*/ nullptr,
540};
541
543
544/* -------------------------------------------------------------------- */
547
549 /* Wrap. */
550 pygpu_shader_info_vertex_in_doc,
551 ".. method:: vertex_in(slot, type, name)\n"
552 "\n"
553 " Add a vertex shader input attribute.\n"
554 "\n"
555 " :param slot: The attribute index.\n"
556 " :type slot: int\n"
557 " :param type: The data type of the attribute.\n"
558 " :type type: " PYDOC_TYPE_LITERAL
559 "\n"
560 " :param name: name of the attribute.\n"
561 " :type name: str\n");
562static PyObject *pygpu_shader_info_vertex_in(BPyGPUShaderCreateInfo *self, PyObject *args)
563{
564 int slot;
566 const char *param;
567
568 if (!PyArg_ParseTuple(args,
569 "i" /* `slot` */
570 "O&" /* `type` */
571 "s" /* `name` */
572 ":vertex_in",
573 &slot,
575 &pygpu_type,
576 &param))
577 {
578 return nullptr;
579 }
580
581#ifdef USE_GPU_PY_REFERENCES
582 PyObject *py_name = PyTuple_GET_ITEM(args, 2);
583 PyList_Append(self->references, py_name);
584#endif
585
586 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
587 info->vertex_in(slot, Type(pygpu_type.value_found), param);
588 Py_RETURN_NONE;
589}
590
592 /* Wrap. */
593 pygpu_shader_info_vertex_out_doc,
594 ".. method:: vertex_out(interface)\n"
595 "\n"
596 " Add a vertex shader output interface block.\n"
597 "\n"
598 " :param interface: Object describing the block.\n"
599 " :type interface: :class:`gpu.types.GPUStageInterfaceInfo`\n");
602{
604 PyErr_Format(PyExc_TypeError, "Expected a GPUStageInterfaceInfo, got %s", Py_TYPE(o)->tp_name);
605 return nullptr;
606 }
607
608#ifdef USE_GPU_PY_REFERENCES
609 PyList_Append(self->references, reinterpret_cast<PyObject *>(o));
610#endif
611
612 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
613 StageInterfaceInfo *interface = reinterpret_cast<StageInterfaceInfo *>(o->interface);
614 info->vertex_out(*interface);
615
616 Py_RETURN_NONE;
617}
618
620 /* Wrap. */
621 pygpu_shader_info_fragment_out_doc,
622 ".. method:: fragment_out(slot, type, name, *, blend='NONE')\n"
623 "\n"
624 " Specify a fragment output corresponding to a framebuffer target slot.\n"
625 "\n"
626 " :param slot: The attribute index.\n"
627 " :type slot: int\n"
628 " :param type: The data type of the output.\n"
629 " :type type: " PYDOC_TYPE_LITERAL
630 "\n"
631 " :param name: Name of the attribute.\n"
632 " :type name: str\n"
633 " :param blend: Dual Source Blending Index.\n"
634 " :type blend: Literal['NONE', 'SRC_0', 'SRC_1']\n");
636 PyObject *args,
637 PyObject *kwds)
638{
639 int slot;
641 PyObject *py_name;
642 PyC_StringEnum blend_type = {pygpu_dualblend_items, int(DualBlend::NONE)};
643
644 static const char *_keywords[] = {"slot", "type", "name", "blend", nullptr};
645 static _PyArg_Parser _parser = {
646 "i" /* `slot` */
647 "O&" /* `type` */
648 "U" /* `name` */
649 "|$" /* Optional, keyword only arguments. */
650 "O&" /* `blend` */
651 ":fragment_out",
652 _keywords,
653 nullptr,
654 };
655 if (!_PyArg_ParseTupleAndKeywordsFast(args,
656 kwds,
657 &_parser,
658 &slot,
660 &pygpu_type,
661 &py_name,
663 &blend_type))
664 {
665 return nullptr;
666 }
667 const char *name = PyUnicode_AsUTF8(py_name);
668 if (name == nullptr) [[unlikely]] {
669 return nullptr;
670 }
671
672#ifdef USE_GPU_PY_REFERENCES
673 PyList_Append(self->references, py_name);
674#endif
675
676 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
677 info->fragment_out(slot, Type(pygpu_type.value_found), name, DualBlend(blend_type.value_found));
678
679 Py_RETURN_NONE;
680}
681
683 /* Wrap. */
684 pygpu_shader_info_depth_write_doc,
685 ".. method:: depth_write(value)\n"
686 "\n"
687 " Specify a depth write behavior when modifying gl_FragDepth.\n"
688 "\n"
689 " There is a common optimization for GPUs that relies on an early depth\n"
690 " test to be run before the fragment shader so that the shader evaluation\n"
691 " can be skipped if the fragment ends up being discarded because it is occluded.\n"
692 "\n"
693 " This optimization does not affect the final rendering, and is typically\n"
694 " possible when the fragment does not change the depth programmatically.\n"
695 " There is, however, a class of operations on the depth in the shader which\n"
696 " could still be performed while allowing the early depth test to operate.\n"
697 "\n"
698 " This function alters the behavior of the optimization to allow those operations\n"
699 " to be performed.\n"
700 "\n"
701 " :param value: Depth write value.\n"
702 " :UNCHANGED: disables depth write in a fragment shader and execution of the "
703 "fragments can be optimized away.\n"
704 " :ANY: enables depth write in a fragment shader for any fragments\n"
705 " :GREATER: enables depth write in a fragment shader for depth values that "
706 "are greater than the depth value in the output buffer.\n"
707 " :LESS: enables depth write in a fragment shader for depth values that "
708 "are less than the depth value in the output buffer.\n"
709 " :type value: Literal['UNCHANGED', 'ANY', 'GREATER', 'LESS']\n");
710static PyObject *pygpu_shader_info_depth_write(BPyGPUShaderCreateInfo *self, PyObject *args)
711{
712 PyC_StringEnum depth_write = {pygpu_depth_write_items, int(DepthWrite::UNCHANGED)};
713 if (!PyC_ParseStringEnum(args, &depth_write)) {
714 return nullptr;
715 }
716
717 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
718 info->depth_write(DepthWrite(depth_write.value_found));
719
720 Py_RETURN_NONE;
721}
722
724 /* Wrap. */
725 pygpu_shader_info_uniform_buf_doc,
726 ".. method:: uniform_buf(slot, type_name, name)\n"
727 "\n"
728 " Specify a uniform variable whose type can be one of those declared in "
729 ":meth:`gpu.types.GPUShaderCreateInfo.typedef_source`.\n"
730 "\n"
731 " :param slot: The uniform variable index.\n"
732 " :type slot: int\n"
733 " :param type_name: Name of the data type. "
734 "It can be a struct type defined in the source passed through the "
735 ":meth:`gpu.types.GPUShaderCreateInfo.typedef_source`.\n"
736 " :type type_name: str\n"
737 " :param name: The uniform variable name.\n"
738 " :type name: str\n");
739static PyObject *pygpu_shader_info_uniform_buf(BPyGPUShaderCreateInfo *self, PyObject *args)
740{
741 int slot;
742 const char *type_name;
743 const char *name;
744
745 if (!PyArg_ParseTuple(args,
746 "i" /* `slot` */
747 "s" /* `type_name` */
748 "s" /* `name` */
749 ":uniform_buf",
750 &slot,
751 &type_name,
752 &name))
753 {
754 return nullptr;
755 }
756
757#ifdef USE_GPU_PY_REFERENCES
758 PyList_Append(self->references, PyTuple_GET_ITEM(args, 1)); /* type_name */
759 PyList_Append(self->references, PyTuple_GET_ITEM(args, 2)); /* name */
760#endif
761
762 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
763 info->uniform_buf(slot, type_name, name);
764
765 Py_RETURN_NONE;
766}
767
769 /* Wrap. */
770 pygpu_shader_info_storage_buf_doc,
771 ".. method:: storage_buf(slot, type_name, name, *, qualifiers={'READ', 'WRITE'})\n"
772 "\n"
773 " Specify a storage buffer variable whose type can be one of those declared in "
774 ":meth:`gpu.types.GPUShaderCreateInfo.typedef_source`.\n"
775 "\n"
776 " :param slot: The storage buffer variable index.\n"
777 " :type slot: int\n"
778 " :param type_name: Name of the data type. "
779 "It can be a struct type defined in the source passed through the "
780 ":meth:`gpu.types.GPUShaderCreateInfo.typedef_source`.\n"
781 " :type type_name: str\n"
782 " :param name: The storage buffer variable name.\n"
783 " :type name: str\n"
784 " :param qualifiers: Set containing values that describe how the storage buffer is to be "
785 "read or written.\n"
786 " :type qualifiers: set[" PYDOC_QUALIFIERS_LITERAL "]\n");
788 PyObject *args,
789 PyObject *kwds)
790{
791 int slot;
792 PyObject *py_type_name;
793 PyObject *py_name;
794 PyObject *py_qualifiers = nullptr;
795 Qualifier qualifier = Qualifier::read_write;
796
797 static const char *_keywords[] = {"slot", "type_name", "name", "qualifiers", nullptr};
798 static _PyArg_Parser _parser = {
799 "i" /* `slot` */
800 "U" /* `type_name` */
801 "U" /* `name` */
802 "|$" /* Optional, keyword only arguments. */
803 "O" /* `qualifiers` */
804 ":storage_buf",
805 _keywords,
806 nullptr,
807 };
808 if (!_PyArg_ParseTupleAndKeywordsFast(
809 args, kwds, &_parser, &slot, &py_type_name, &py_name, &py_qualifiers))
810 {
811 return nullptr;
812 }
813 const char *type_name = PyUnicode_AsUTF8(py_type_name);
814 if (type_name == nullptr) [[unlikely]] {
815 return nullptr;
816 }
817 const char *name = PyUnicode_AsUTF8(py_name);
818 if (name == nullptr) [[unlikely]] {
819 return nullptr;
820 }
821
822 if (py_qualifiers && PyC_FlagSet_ToBitfield(pygpu_qualifiers,
823 py_qualifiers,
824 reinterpret_cast<int *>(&qualifier),
825 "shader_info.storage_buf") == -1)
826 {
827 return nullptr;
828 }
829
830#ifdef USE_GPU_PY_REFERENCES
831 PyList_Append(self->references, py_type_name);
832 PyList_Append(self->references, py_name);
833#endif
834
835 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
836 info->storage_buf(slot, qualifier, type_name, name);
837
838 Py_RETURN_NONE;
839}
840
841#ifdef USE_PYGPU_SHADER_INFO_IMAGE_METHOD
843 /* Wrap. */
844 pygpu_shader_info_image_doc,
845 ".. method:: image(slot, format, type, name, *, qualifiers={'NO_RESTRICT'})\n"
846 "\n"
847 " Specify an image resource used for arbitrary load and store operations.\n"
848 "\n"
849 " :param slot: The image resource index.\n"
850 " :type slot: int\n"
851 " :param format: The GPUTexture format that is passed to the shader.\n"
852 " :type format: " PYDOC_TEX_FORMAT_LITERAL
853 "\n"
854 " :param type: The data type describing how the image is to be read in the shader.\n"
855 " :type type: " PYDOC_IMAGE_TYPES_LITERAL
856 "\n"
857 " :param name: The image resource name.\n"
858 " :type name: str\n"
859 " :param qualifiers: Set containing values that describe how the image resource is to be "
860 "read or written.\n"
861 " :type qualifiers: set[" PYDOC_QUALIFIERS_LITERAL "]\n");
863 PyObject *args,
864 PyObject *kwds)
865{
866 int slot;
867 PyC_StringEnum pygpu_texformat = {pygpu_textureformat_items};
868 PyC_StringEnum pygpu_imagetype = {pygpu_imagetype_items};
869 PyObject *py_name;
870 PyObject *py_qualifiers = nullptr;
871 Qualifier qualifier = Qualifier::no_restrict;
872
873 static const char *_keywords[] = {"slot", "format", "type", "name", "qualifiers", nullptr};
874 static _PyArg_Parser _parser = {
875 "i" /* `slot` */
876 "O&" /* `format` */
877 "O&" /* `type` */
878 "U" /* `name` */
879 "|$" /* Optional, keyword only arguments. */
880 "O" /* `qualifiers` */
881 ":image",
882 _keywords,
883 nullptr,
884 };
885 if (!_PyArg_ParseTupleAndKeywordsFast(args,
886 kwds,
887 &_parser,
888 &slot,
890 &pygpu_texformat,
892 &pygpu_imagetype,
893 &py_name,
894 &py_qualifiers))
895 {
896 return nullptr;
897 }
898 const char *name = PyUnicode_AsUTF8(py_name);
899 if (name == nullptr) [[unlikely]] {
900 return nullptr;
901 }
902
903 if (py_qualifiers && PyC_FlagSet_ToBitfield(pygpu_qualifiers,
904 py_qualifiers,
905 reinterpret_cast<int *>(&qualifier),
906 "shader_info.image") == -1)
907 {
908 return nullptr;
909 }
910
911 if (pygpu_texformat.value_found == GPU_DEPTH24_STENCIL8_DEPRECATED) {
912 pygpu_texformat.value_found = int(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
913 PyErr_WarnEx(
914 PyExc_DeprecationWarning, "'DEPTH24_STENCIL8' is deprecated. Use 'DEPTH32F_STENCIL8'.", 1);
915 }
916 if (pygpu_texformat.value_found == GPU_DEPTH_COMPONENT24_DEPRECATED) {
917 pygpu_texformat.value_found = int(gpu::TextureFormat::SFLOAT_32_DEPTH);
918 PyErr_WarnEx(PyExc_DeprecationWarning,
919 "'DEPTH_COMPONENT24' is deprecated. Use 'DEPTH_COMPONENT32F'.",
920 1);
921 }
922
923# ifdef USE_GPU_PY_REFERENCES
924 PyList_Append(self->references, py_name);
925# endif
926
927 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
928 info->image(slot,
929 gpu::TextureFormat(pygpu_texformat.value_found),
930 qualifier,
932 name);
933
934 Py_RETURN_NONE;
935}
936#endif
937
939 /* Wrap. */
940 pygpu_shader_info_sampler_doc,
941 ".. method:: sampler(slot, type, name)\n"
942 "\n"
943 " Specify an image texture sampler.\n"
944 "\n"
945 " :param slot: The image texture sampler index.\n"
946 " :type slot: int\n"
947 " :param type: The data type describing the format of each sampler unit.\n"
948 " :type type: " PYDOC_IMAGE_TYPES_LITERAL
949 "\n"
950 " :param name: The image texture sampler name.\n"
951 " :type name: str\n");
952static PyObject *pygpu_shader_info_sampler(BPyGPUShaderCreateInfo *self, PyObject *args)
953{
954 int slot;
955 PyC_StringEnum pygpu_samplertype = {pygpu_imagetype_items};
956 const char *name;
957
958 if (!PyArg_ParseTuple(args,
959 "i" /* `slot` */
960 "O&" /* `type` */
961 "s" /* `name` */
962 ":sampler",
963 &slot,
965 &pygpu_samplertype,
966 &name))
967 {
968 return nullptr;
969 }
970
971#ifdef USE_GPU_PY_REFERENCES
972 PyList_Append(self->references, PyTuple_GET_ITEM(args, 2)); /* name */
973#endif
974
975 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
976 info->sampler(slot, ImageType(pygpu_samplertype.value_found), name);
977
978 Py_RETURN_NONE;
979}
980
981static int constant_type_size(Type type)
982{
983 switch (type) {
984 case Type::bool_t:
985 case Type::float_t:
986 case Type::int_t:
987 case Type::uint_t:
988 case Type::uchar4_t:
989 case Type::char4_t:
990 case Type::float3_10_10_10_2_t:
991 case Type::ushort2_t:
992 case Type::short2_t:
993 return 4;
994 break;
995 case Type::ushort3_t:
996 case Type::short3_t:
997 return 6;
998 break;
999 case Type::float2_t:
1000 case Type::uint2_t:
1001 case Type::int2_t:
1002 case Type::ushort4_t:
1003 case Type::short4_t:
1004 return 8;
1005 break;
1006 case Type::float3_t:
1007 case Type::uint3_t:
1008 case Type::int3_t:
1009 return 12;
1010 break;
1011 case Type::float4_t:
1012 case Type::uint4_t:
1013 case Type::int4_t:
1014 return 16;
1015 break;
1016 case Type::float3x3_t:
1017 return 36 + 3 * 4;
1018 case Type::float4x4_t:
1019 return 64;
1020 break;
1021 case Type::uchar_t:
1022 case Type::char_t:
1023 return 1;
1024 break;
1025 case Type::uchar2_t:
1026 case Type::char2_t:
1027 case Type::ushort_t:
1028 case Type::short_t:
1029 return 2;
1030 break;
1031 case Type::uchar3_t:
1032 case Type::char3_t:
1033 return 3;
1034 break;
1035 }
1036 BLI_assert(false);
1037 return -1;
1038}
1039
1041{
1042 int size_prev = 0;
1043 int size_last = 0;
1045 int pad = 0;
1046 int size = constant_type_size(uniform.type);
1047 if (size_last && size_last != size) {
1048 /* Calc pad. */
1049 int pack = (size == 8) ? 8 : 16;
1050 if (size_last < size) {
1051 pad = pack - (size_last % pack);
1052 }
1053 else {
1054 pad = size_prev % pack;
1055 }
1056 }
1057 else if (size == 12) {
1058 /* It is still unclear how Vulkan handles padding for `vec3` constants. For now let's follow
1059 * the rules of the `std140` layout. */
1060 pad = 4;
1061 }
1062 size_prev += pad + size * std::max(1, uniform.array_size);
1063 size_last = size;
1064 }
1065 return size_prev + (size_prev % 16);
1066}
1067
1069 /* Wrap. */
1070 pygpu_shader_info_push_constant_doc,
1071 ".. method:: push_constant(type, name, size=0)\n"
1072 "\n"
1073 " Specify a global access constant.\n"
1074 "\n"
1075 " :param type: The data type of the constant.\n"
1076 " :type type: " PYDOC_TYPE_LITERAL
1077 "\n"
1078 " :param name: Name of the constant.\n"
1079 " :type name: str\n"
1080 " :param size: If not zero, indicates that the constant is an array "
1081 "with the specified size.\n"
1082 " :type size: int\n");
1084 PyObject *args,
1085 PyObject *kwds)
1086{
1087 PyC_StringEnum pygpu_type = {pygpu_attrtype_items};
1088 PyObject *py_name = nullptr;
1089 int array_size = 0;
1090
1091 static const char *_keywords[] = {"type", "name", "size", nullptr};
1092 static _PyArg_Parser _parser = {
1093 "O&" /* `type` */
1094 "U" /* `name` */
1095 "|" /* Optional arguments. */
1096 "I" /* `size` */
1097 ":push_constant",
1098 _keywords,
1099 nullptr,
1100 };
1101 if (!_PyArg_ParseTupleAndKeywordsFast(
1102 args, kwds, &_parser, PyC_ParseStringEnum, &pygpu_type, &py_name, &array_size))
1103 {
1104 return nullptr;
1105 }
1106 const char *name = PyUnicode_AsUTF8(py_name);
1107 if (name == nullptr) [[unlikely]] {
1108 return nullptr;
1109 }
1110
1111#ifdef USE_GPU_PY_REFERENCES
1112 PyList_Append(self->references, py_name);
1113#endif
1114
1115 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1116 info->push_constant(Type(pygpu_type.value_found), name, array_size);
1117
1118#define VULKAN_LIMIT 128
1119 int size = constants_calc_size(info);
1120 if (size > VULKAN_LIMIT) {
1121 printf("Push constants have a minimum supported size of " STRINGIFY(VULKAN_LIMIT) " bytes, however the constants added so far already reach %d bytes. Consider using UBO.\n", size);
1122 }
1123#undef VULKAN_LIMIT
1124
1125 Py_RETURN_NONE;
1126}
1127
1129 /* Wrap. */
1130 pygpu_shader_info_vertex_source_doc,
1131 ".. method:: vertex_source(source)\n"
1132 "\n"
1133 " Vertex shader source code written in GLSL.\n"
1134 "\n"
1135 " Example:\n"
1136 "\n"
1137 " .. code-block:: python\n"
1138 "\n"
1139 " \"void main() {gl_Position = vec4(pos, 1.0);}\"\n"
1140 "\n"
1141 " :param source: The vertex shader source code.\n"
1142 " :type source: str\n"
1143 "\n"
1144 " .. seealso:: `GLSL Cross Compilation "
1145 "<https://developer.blender.org/docs/features/gpu/glsl_cross_compilation/>`__\n");
1147{
1148 const char *vertex_source = PyUnicode_AsUTF8(o);
1149 if (vertex_source == nullptr) {
1150 PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
1151 return nullptr;
1152 }
1153
1154#ifdef USE_GPU_PY_REFERENCES
1155 if (self->vertex_source) {
1156 Py_DECREF(self->vertex_source);
1157 }
1158
1159 self->vertex_source = o;
1160 Py_INCREF(o);
1161#endif
1162
1163 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1164 info->vertex_source("gpu_shader_python_base.glsl");
1165 info->vertex_source_generated = vertex_source;
1166
1167 Py_RETURN_NONE;
1168}
1169
1171 /* Wrap. */
1172 pygpu_shader_info_compute_source_doc,
1173 ".. method:: compute_source(source)\n"
1174 "\n"
1175 " compute shader source code written in GLSL.\n"
1176 "\n"
1177 " Example:\n"
1178 "\n"
1179 " .. code-block:: python\n"
1180 "\n"
1181 " \"\"\"void main() {\n"
1182 " int2 index = int2(gl_GlobalInvocationID.xy);\n"
1183 " vec4 color = vec4(0.0, 0.0, 0.0, 1.0);\n"
1184 " imageStore(img_output, index, color);\n"
1185 " }\"\"\"\n"
1186 "\n"
1187 " :param source: The compute shader source code.\n"
1188 " :type source: str\n"
1189 "\n"
1190 " .. seealso:: `GLSL Cross Compilation "
1191 "<https://developer.blender.org/docs/features/gpu/glsl_cross_compilation/>`__\n");
1193{
1194 const char *compute_source = PyUnicode_AsUTF8(o);
1195 if (compute_source == nullptr) {
1196 PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
1197 return nullptr;
1198 }
1199
1200#ifdef USE_GPU_PY_REFERENCES
1201 if (self->compute_source) {
1202 Py_DECREF(self->compute_source);
1203 }
1204
1205 self->compute_source = o;
1206 Py_INCREF(o);
1207#endif
1208
1209 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1210 info->compute_source("gpu_shader_python_base.glsl");
1211 info->compute_source_generated = compute_source;
1212
1213 Py_RETURN_NONE;
1214}
1215
1217 /* Wrap. */
1218 pygpu_shader_info_fragment_source_doc,
1219 ".. method:: fragment_source(source)\n"
1220 "\n"
1221 " Fragment shader source code written in GLSL.\n"
1222 "\n"
1223 " Example:\n"
1224 "\n"
1225 " .. code-block:: python\n"
1226 "\n"
1227 " \"void main() {fragColor = vec4(0.0, 0.0, 0.0, 1.0);}\"\n"
1228 "\n"
1229 " :param source: The fragment shader source code.\n"
1230 " :type source: str\n"
1231 "\n"
1232 " .. seealso:: `GLSL Cross Compilation "
1233 "<https://developer.blender.org/docs/features/gpu/glsl_cross_compilation/>`__\n");
1235{
1236 const char *fragment_source = PyUnicode_AsUTF8(o);
1237 if (fragment_source == nullptr) {
1238 PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
1239 return nullptr;
1240 }
1241
1242#ifdef USE_GPU_PY_REFERENCES
1243 if (self->fragment_source) {
1244 Py_DECREF(self->fragment_source);
1245 }
1246
1247 self->fragment_source = o;
1248 Py_INCREF(o);
1249#endif
1250
1251 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1252 info->fragment_source("gpu_shader_python_base.glsl");
1253 info->fragment_source_generated = fragment_source;
1254
1255 Py_RETURN_NONE;
1256}
1257
1259 /* Wrap. */
1260 pygpu_shader_info_typedef_source_doc,
1261 ".. method:: typedef_source(source)\n"
1262 "\n"
1263 " Source code included before resource declaration. "
1264 "Useful for defining structs used by Uniform Buffers.\n"
1265 "\n"
1266 " Example:\n"
1267 "\n"
1268 " .. code-block:: python\n"
1269 "\n"
1270 " \"struct MyType {int foo; float bar;};\"\n"
1271 "\n"
1272 " :param source: The source code defining types.\n"
1273 " :type source: str\n");
1275{
1276 const char *typedef_source = PyUnicode_AsUTF8(o);
1277 if (typedef_source == nullptr) {
1278 PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
1279 return nullptr;
1280 }
1281
1282#ifdef USE_GPU_PY_REFERENCES
1283 if (self->typedef_source) {
1284 Py_DECREF(self->typedef_source);
1285 }
1286
1287 self->typedef_source = o;
1288 Py_INCREF(o);
1289#endif
1290
1291 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1292#if 0
1293 if (info->typedef_sources_.is_empty()) {
1294 info->typedef_source("GPU_shader_shared_utils.hh");
1295 }
1296#endif
1297 info->typedef_source_generated = typedef_source;
1298
1299 Py_RETURN_NONE;
1300}
1301
1303 /* Wrap. */
1304 pygpu_shader_info_define_doc,
1305 ".. method:: define(name, value)\n"
1306 "\n"
1307 " Add a preprocessing define directive. In GLSL it would be something like:\n"
1308 "\n"
1309 " .. code-block:: glsl\n"
1310 "\n"
1311 " #define name value\n"
1312 "\n"
1313 " :param name: Token name.\n"
1314 " :type name: str\n"
1315 " :param value: Text that replaces token occurrences.\n"
1316 " :type value: str\n");
1317static PyObject *pygpu_shader_info_define(BPyGPUShaderCreateInfo *self, PyObject *args)
1318{
1319 const char *name;
1320 const char *value = nullptr;
1321
1322 if (!PyArg_ParseTuple(args,
1323 "s" /* `name` */
1324 "|" /* Optional arguments. */
1325 "s" /* `value` */
1326 ":define",
1327 &name,
1328 &value))
1329 {
1330 return nullptr;
1331 }
1332
1333#ifdef USE_GPU_PY_REFERENCES
1334 PyList_Append(self->references, PyTuple_GET_ITEM(args, 0)); /* name */
1335 if (value) {
1336 PyList_Append(self->references, PyTuple_GET_ITEM(args, 1)); /* value */
1337 }
1338#endif
1339
1340 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1341 if (value) {
1342 info->define(name, value);
1343 }
1344 else {
1345 info->define(name);
1346 }
1347
1348 Py_RETURN_NONE;
1349}
1350
1352 /* Wrap. */
1353 pygpu_shader_info_local_group_size_doc,
1354 ".. method:: local_group_size(x, y=1, z=1)\n"
1355 "\n"
1356 " Specify the local group size for compute shaders.\n"
1357 "\n"
1358 " :param x: The local group size in the x dimension.\n"
1359 " :type x: int\n"
1360 " :param y: The local group size in the y dimension. Optional. Defaults to 1.\n"
1361 " :type y: int\n"
1362 " :param z: The local group size in the z dimension. Optional. Defaults to 1.\n"
1363 " :type z: int\n");
1364static PyObject *pygpu_shader_info_local_group_size(BPyGPUShaderCreateInfo *self, PyObject *args)
1365{
1366 int x = -1, y = 1, z = 1;
1367
1368 if (!PyArg_ParseTuple(args,
1369 "i" /* `x` */
1370 "|" /* Optional arguments. */
1371 "i" /* `y` */
1372 "i" /* `z` */
1373 ":local_group_size",
1374 &x,
1375 &y,
1376 &z))
1377 {
1378 return nullptr;
1379 }
1380
1381 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(self->info);
1382 info->local_group_size(x, y, z);
1383
1384 Py_RETURN_NONE;
1385}
1386
1387static PyMethodDef pygpu_shader_info__tp_methods[] = {
1388 {"vertex_in",
1389 reinterpret_cast<PyCFunction>(pygpu_shader_info_vertex_in),
1390 METH_VARARGS,
1391 pygpu_shader_info_vertex_in_doc},
1392 {"vertex_out",
1393 reinterpret_cast<PyCFunction>(pygpu_shader_info_vertex_out),
1394 METH_O,
1395 pygpu_shader_info_vertex_out_doc},
1396 {"depth_write",
1397 (PyCFunction)(void *)(pygpu_shader_info_depth_write),
1398 METH_O,
1399 pygpu_shader_info_depth_write_doc},
1400 {"fragment_out",
1401 (PyCFunction)(void *)(pygpu_shader_info_fragment_out),
1402 METH_VARARGS | METH_KEYWORDS,
1403 pygpu_shader_info_fragment_out_doc},
1404 {"uniform_buf",
1405 (PyCFunction)(void *)(pygpu_shader_info_uniform_buf),
1406 METH_VARARGS,
1407 pygpu_shader_info_uniform_buf_doc},
1408 {"storage_buf",
1409 (PyCFunction)(void *)(pygpu_shader_info_storage_buf),
1410 METH_VARARGS | METH_KEYWORDS,
1411 pygpu_shader_info_storage_buf_doc},
1412#ifdef USE_PYGPU_SHADER_INFO_IMAGE_METHOD
1413 {"image",
1414 (PyCFunction)(void *)(pygpu_shader_info_image),
1415 METH_VARARGS | METH_KEYWORDS,
1416 pygpu_shader_info_image_doc},
1417#endif
1418 {"sampler",
1419 reinterpret_cast<PyCFunction>(pygpu_shader_info_sampler),
1420 METH_VARARGS,
1421 pygpu_shader_info_sampler_doc},
1422 {"push_constant",
1423 (PyCFunction)(void *)(pygpu_shader_info_push_constant),
1424 METH_VARARGS | METH_KEYWORDS,
1425 pygpu_shader_info_push_constant_doc},
1426 {"vertex_source",
1427 reinterpret_cast<PyCFunction>(pygpu_shader_info_vertex_source),
1428 METH_O,
1429 pygpu_shader_info_vertex_source_doc},
1430 {"fragment_source",
1431 reinterpret_cast<PyCFunction>(pygpu_shader_info_fragment_source),
1432 METH_O,
1433 pygpu_shader_info_fragment_source_doc},
1434 {"compute_source",
1435 reinterpret_cast<PyCFunction>(pygpu_shader_info_compute_source),
1436 METH_O,
1437 pygpu_shader_info_compute_source_doc},
1438 {"typedef_source",
1439 reinterpret_cast<PyCFunction>(pygpu_shader_info_typedef_source),
1440 METH_O,
1441 pygpu_shader_info_typedef_source_doc},
1442 {"define",
1443 reinterpret_cast<PyCFunction>(pygpu_shader_info_define),
1444 METH_VARARGS,
1445 pygpu_shader_info_define_doc},
1446 {"local_group_size",
1447 reinterpret_cast<PyCFunction>(pygpu_shader_info_local_group_size),
1448 METH_VARARGS,
1449 pygpu_shader_info_local_group_size_doc},
1450 {nullptr, nullptr, 0, nullptr},
1451};
1452
1454
1455/* -------------------------------------------------------------------- */
1458
1459static PyObject *pygpu_shader_info__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
1460{
1461 if (PyTuple_Size(args) || kwds) {
1462 PyErr_SetString(PyExc_TypeError, "no args or keywords are expected");
1463 return nullptr;
1464 }
1465
1466 ShaderCreateInfo *info = new ShaderCreateInfo("pyGPU_Shader");
1467 GPUShaderCreateInfo *shader_info = reinterpret_cast<GPUShaderCreateInfo *>(info);
1468
1469 return BPyGPUShaderCreateInfo_CreatePyObject(shader_info);
1470}
1471
1472#ifdef USE_GPU_PY_REFERENCES
1473
1474static int pygpu_shader_info__tp_traverse(PyObject *self, visitproc visit, void *arg)
1475{
1476 BPyGPUShaderCreateInfo *py_info = reinterpret_cast<BPyGPUShaderCreateInfo *>(self);
1477 Py_VISIT(py_info->vertex_source);
1478 Py_VISIT(py_info->fragment_source);
1479 Py_VISIT(py_info->compute_source);
1480 Py_VISIT(py_info->typedef_source);
1481 Py_VISIT(py_info->references);
1482 return 0;
1483}
1484
1485static int pygpu_shader_info__tp_clear(PyObject *self)
1486{
1487 BPyGPUShaderCreateInfo *py_info = reinterpret_cast<BPyGPUShaderCreateInfo *>(self);
1488 Py_CLEAR(py_info->vertex_source);
1489 Py_CLEAR(py_info->fragment_source);
1490 Py_CLEAR(py_info->compute_source);
1491 Py_CLEAR(py_info->typedef_source);
1492 Py_CLEAR(py_info->references);
1493 return 0;
1494}
1495
1496#endif
1497
1498static void pygpu_shader_info__tp_dealloc(PyObject *self)
1499{
1500 BPyGPUShaderCreateInfo *py_info = reinterpret_cast<BPyGPUShaderCreateInfo *>(self);
1501 ShaderCreateInfo *info = reinterpret_cast<ShaderCreateInfo *>(py_info->info);
1502 delete info;
1503
1504#ifdef USE_GPU_PY_REFERENCES
1505 PyObject_GC_UnTrack(self);
1507#endif
1508
1509 Py_TYPE(self)->tp_free(self);
1510}
1511
1513 /* Wrap. */
1514 pygpu_shader_info__tp_doc,
1515 ".. class:: GPUShaderCreateInfo()\n"
1516 "\n"
1517 " Stores and describes types and variables that are used in shader sources.\n");
1519 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
1520 /*tp_name*/ "GPUShaderCreateInfo",
1521 /*tp_basicsize*/ sizeof(BPyGPUShaderCreateInfo),
1522 /*tp_itemsize*/ 0,
1523 /*tp_dealloc*/ pygpu_shader_info__tp_dealloc,
1524 /*tp_vectorcall_offset*/ 0,
1525 /*tp_getattr*/ nullptr,
1526 /*tp_setattr*/ nullptr,
1527 /*tp_compare*/ nullptr,
1528 /*tp_repr*/ nullptr,
1529 /*tp_as_number*/ nullptr,
1530 /*tp_as_sequence*/ nullptr,
1531 /*tp_as_mapping*/ nullptr,
1532 /*tp_hash*/ nullptr,
1533 /*tp_call*/ nullptr,
1534 /*tp_str*/ nullptr,
1535 /*tp_getattro*/ nullptr,
1536 /*tp_setattro*/ nullptr,
1537 /*tp_as_buffer*/ nullptr,
1539 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1540#else
1541 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
1542#endif
1543 /*tp_doc*/ pygpu_shader_info__tp_doc,
1545 /*tp_traverse*/ pygpu_shader_info__tp_traverse,
1546#else
1547 /*tp_traverse*/ nullptr,
1548#endif
1550 /*tp_clear*/ pygpu_shader_info__tp_clear,
1551#else
1552 /*tp_clear*/ nullptr,
1553#endif
1554 /*tp_richcompare*/ nullptr,
1555 /*tp_weaklistoffset*/ 0,
1556 /*tp_iter*/ nullptr,
1557 /*tp_iternext*/ nullptr,
1558 /*tp_methods*/ pygpu_shader_info__tp_methods,
1559 /*tp_members*/ nullptr,
1560 /*tp_getset*/ nullptr,
1561 /*tp_base*/ nullptr,
1562 /*tp_dict*/ nullptr,
1563 /*tp_descr_get*/ nullptr,
1564 /*tp_descr_set*/ nullptr,
1565 /*tp_dictoffset*/ 0,
1566 /*tp_init*/ nullptr,
1567 /*tp_alloc*/ nullptr,
1568 /*tp_new*/ pygpu_shader_info__tp_new,
1569 /*tp_free*/ nullptr,
1570 /*tp_is_gc*/ nullptr,
1571 /*tp_bases*/ nullptr,
1572 /*tp_mro*/ nullptr,
1573 /*tp_cache*/ nullptr,
1574 /*tp_subclasses*/ nullptr,
1575 /*tp_weaklist*/ nullptr,
1576 /*tp_del*/ nullptr,
1577 /*tp_version_tag*/ 0,
1578 /*tp_finalize*/ nullptr,
1579 /*tp_vectorcall*/ nullptr,
1580};
1581
1583
1584/* -------------------------------------------------------------------- */
1587
1588PyObject *BPyGPUStageInterfaceInfo_CreatePyObject(GPUStageInterfaceInfo *interface)
1589{
1591
1592#ifdef USE_GPU_PY_REFERENCES
1593 self = reinterpret_cast<BPyGPUStageInterfaceInfo *>(
1594 _PyObject_GC_New(&BPyGPUStageInterfaceInfo_Type));
1595 self->references = PyList_New(0);
1596#else
1598#endif
1599
1600 self->interface = interface;
1601
1602 return reinterpret_cast<PyObject *>(self);
1603}
1604
1605PyObject *BPyGPUShaderCreateInfo_CreatePyObject(GPUShaderCreateInfo *info)
1606{
1608
1609#ifdef USE_GPU_PY_REFERENCES
1610 self = reinterpret_cast<BPyGPUShaderCreateInfo *>(
1611 _PyObject_GC_New(&BPyGPUShaderCreateInfo_Type));
1612 self->vertex_source = nullptr;
1613 self->fragment_source = nullptr;
1614 self->compute_source = nullptr;
1615 self->typedef_source = nullptr;
1616 self->references = PyList_New(0);
1617#else
1619#endif
1620
1621 self->info = info;
1622 self->constants_total_size = 0;
1623
1624 return reinterpret_cast<PyObject *>(self);
1625}
1626
1628
1629} // namespace blender
int pad[32 - sizeof(int)]
#define STRINGIFY(x)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define USE_GPU_PY_REFERENCES
#define BPyGPUStageInterfaceInfo_Check(v)
#define PYDOC_TYPE_LITERAL
#define PYDOC_QUALIFIERS_LITERAL
#define VULKAN_LIMIT
#define PYDOC_IMAGE_TYPES_LITERAL
#define PYDOC_TEX_FORMAT_LITERAL
#define uniform(slot)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * pygpu_shader_info_depth_write(BPyGPUShaderCreateInfo *self, PyObject *args)
static PyObject * pygpu_shader_info_define(BPyGPUShaderCreateInfo *self, PyObject *args)
int PyC_FlagSet_ToBitfield(const PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix)
static PyObject * pygpu_shader_info__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
static int pygpu_shader_info__tp_traverse(PyObject *self, visitproc visit, void *arg)
static PyObject * pygpu_shader_info_storage_buf(BPyGPUShaderCreateInfo *self, PyObject *args, PyObject *kwds)
static PyObject * pygpu_shader_info_typedef_source(BPyGPUShaderCreateInfo *self, PyObject *o)
static const PyC_StringEnumItems pygpu_depth_write_items[]
static PyObject * pygpu_shader_info_vertex_in(BPyGPUShaderCreateInfo *self, PyObject *args)
static PyObject * pygpu_shader_info_vertex_out(BPyGPUShaderCreateInfo *self, BPyGPUStageInterfaceInfo *o)
static PyMethodDef pygpu_interface_info__tp_methods[]
static bool pygpu_interface_info_get_args(BPyGPUStageInterfaceInfo *self, PyObject *args, const char *format, Type *r_type, const char **r_name)
static PyObject * pygpu_shader_info_sampler(BPyGPUShaderCreateInfo *self, PyObject *args)
static const PyC_StringEnumItems pygpu_dualblend_items[]
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PyTypeObject BPyGPUStageInterfaceInfo_Type
static PyObject * pygpu_interface_info_no_perspective(BPyGPUStageInterfaceInfo *self, PyObject *args)
constexpr int GPU_DEPTH24_STENCIL8_DEPRECATED
static int pygpu_shader_info__tp_clear(PyObject *self)
static const PyC_StringEnumItems pygpu_imagetype_items[]
static PyObject * pygpu_shader_info_local_group_size(BPyGPUShaderCreateInfo *self, PyObject *args)
static PyObject * pygpu_interface_info_flat(BPyGPUStageInterfaceInfo *self, PyObject *args)
static PyGetSetDef pygpu_interface_info__tp_getseters[]
static int constant_type_size(Type type)
static int constants_calc_size(ShaderCreateInfo *info)
static PyObject * pygpu_interface_info__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
static PyMethodDef pygpu_shader_info__tp_methods[]
static PyObject * pygpu_shader_info_image(BPyGPUShaderCreateInfo *self, PyObject *args, PyObject *kwds)
const struct PyC_StringEnumItems pygpu_attrtype_items[]
static int pygpu_interface_info__tp_traverse(PyObject *self, visitproc visit, void *arg)
static PyObject * pygpu_shader_info_push_constant(BPyGPUShaderCreateInfo *self, PyObject *args, PyObject *kwds)
const PyC_StringEnumItems pygpu_tex_format_items[]
const PyC_StringEnumItems pygpu_textureformat_items[]
constexpr int GPU_DEPTH_COMPONENT24_DEPRECATED
static int pygpu_interface_info__tp_clear(PyObject *self)
static void pygpu_interface_info__tp_dealloc(PyObject *self)
static PyObject * pygpu_shader_info_compute_source(BPyGPUShaderCreateInfo *self, PyObject *o)
static PyObject * pygpu_shader_info_uniform_buf(BPyGPUShaderCreateInfo *self, PyObject *args)
static const PyC_FlagSet pygpu_qualifiers[]
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
int PyC_ParseStringEnum(PyObject *o, void *p)
static PyObject * pygpu_interface_info_smooth(BPyGPUStageInterfaceInfo *self, PyObject *args)
static PyObject * pygpu_shader_info_vertex_source(BPyGPUShaderCreateInfo *self, PyObject *o)
static PyObject * pygpu_shader_info_fragment_source(BPyGPUShaderCreateInfo *self, PyObject *o)
PyTypeObject BPyGPUShaderCreateInfo_Type
PyObject * BPyGPUStageInterfaceInfo_CreatePyObject(GPUStageInterfaceInfo *interface)
static void pygpu_shader_info__tp_dealloc(PyObject *self)
static PyObject * pygpu_shader_info_fragment_out(BPyGPUShaderCreateInfo *self, PyObject *args, PyObject *kwds)
static PyObject * pygpu_interface_info_name_get(BPyGPUStageInterfaceInfo *self, void *)
PyObject * BPyGPUShaderCreateInfo_CreatePyObject(GPUShaderCreateInfo *info)
header-only compatibility defines.
PyObject_HEAD GPUShaderCreateInfo * info
PyObject_HEAD GPUStageInterfaceInfo * interface
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & uniform_buf(int slot, StringRefNull type_name, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & image(int slot, TextureFormat format, Qualifier qualifiers, ImageReadWriteType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & compute_source(StringRefNull filename)
Self & fragment_source(StringRefNull filename)
Self & vertex_in(int slot, Type type, StringRefNull name, ConditionFn cond=nullptr)
Self & fragment_out(int slot, Type type, StringRefNull name, DualBlend blend=DualBlend::NONE, int raster_order_group=-1, ConditionFn cond=nullptr)
Self & typedef_source(StringRefNull filename)
Self & storage_buf(int slot, Qualifier qualifiers, StringRefNull type_name, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & local_group_size(int local_size_x, int local_size_y=1, int local_size_z=1)
Self & vertex_out(StageInterfaceInfo &interface, ConditionFn cond=nullptr)
Self & sampler(int slot, ImageType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & define(StringRefNull name, StringRefNull value="")
Self & push_constant(Type type, StringRefNull name, int array_size=0, ConditionFn cond=nullptr)
Self & smooth(Type type, StringRefNull _name)
Self & no_perspective(Type type, StringRefNull _name)
Self & flat(Type type, StringRefNull _name)