40 if (!self->
batch->shader) {
41 PyErr_SetString(PyExc_RuntimeError,
"batch does not have any program assigned to it");
57 const char *exc_str_missing_arg =
"GPUBatch.__new__() missing required argument '%s' (pos %d)";
64 static const char *_keywords[] = {
"type",
"buf",
"elem",
nullptr};
65 static _PyArg_Parser _parser = {
74 if (!_PyArg_ParseTupleAndKeywordsFast(args,
82 &py_indexbuf_or_none))
89 PyErr_WarnEx(PyExc_DeprecationWarning,
90 "'LINE_LOOP' is deprecated. Please use 'LINE_STRIP' and close the segment.",
95 PyExc_DeprecationWarning,
96 "'TRI_FAN' is deprecated. Please use 'TRI_STRIP' or 'TRIS' and try modifying your "
97 "vertices or indices to match the topology.",
101 if (py_vertbuf ==
nullptr) {
102 PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
108 py_indexbuf ? py_indexbuf->
elem :
nullptr);
112#ifdef USE_GPU_PY_REFERENCES
113 ret->references = PyList_New(py_indexbuf ? 2 : 1);
114 PyList_SET_ITEM(
ret->references, 0, (PyObject *)py_vertbuf);
115 Py_INCREF(py_vertbuf);
117 if (py_indexbuf !=
nullptr) {
118 PyList_SET_ITEM(
ret->references, 1, (PyObject *)py_indexbuf);
119 Py_INCREF(py_indexbuf);
123 PyObject_GC_Track(
ret);
126 return reinterpret_cast<PyObject *
>(
ret);
131 pygpu_batch_vertbuf_add_doc,
".. method:: vertbuf_add(buf)\n"
133" Add another vertex buffer to the Batch.\n"
134" It is not possible to add more vertices to the batch using this method.\n"
135" Instead it can be used to add more attributes to the existing vertices.\n"
136" A good use case would be when you have a separate\n"
137" vertex buffer for vertex positions and vertex normals.\n"
140" :param buf: The vertex buffer that will be added to the batch.\n"
141" :type buf: :class:`gpu.types.GPUVertBuf`\n"
146 PyErr_Format(PyExc_TypeError,
"Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
152 PyErr_Format(PyExc_TypeError,
153 "Expected %d length, got %d",
166#ifdef USE_GPU_PY_REFERENCES
168 PyList_Append(self->
references,
reinterpret_cast<PyObject *
>(py_buf));
181#ifdef USE_GPU_PY_REFERENCES
185 PyObject *py_shader_test = PyList_GET_ITEM(self->
references, i);
187 PyList_SET_ITEM(self->
references, i,
reinterpret_cast<PyObject *
>(py_shader));
188 Py_INCREF(py_shader);
189 Py_DECREF(py_shader_test);
196 PyList_Append(self->
references,
reinterpret_cast<PyObject *
>(py_shader));
209 const bool shader_changed = self->
batch->shader != py_shader->
shader;
213 if (shader_changed) {
220 pygpu_batch_program_set_doc,
221 ".. method:: program_set(program)\n"
223 " Assign a shader to this batch that will be used for drawing when not overwritten later.\n"
224 " Note: This method has to be called in the draw context that the batch will be drawn in.\n"
225 " This function does not need to be called when you always\n"
226 " set the shader when calling :meth:`gpu.types.GPUBatch.draw`.\n"
228 " :param program: The program/shader the batch will use in future draw calls.\n"
229 " :type program: :class:`gpu.types.GPUShader`\n");
232 static bool deprecation_warning_issued =
false;
235 if (!deprecation_warning_issued) {
236 PyErr_WarnEx(PyExc_DeprecationWarning,
237 "Calls to GPUBatch.program_set are deprecated."
238 "Please set the shader via the 'program' parameter when calling "
239 "GPUBatch.draw/draw_instanced/draw_range.",
241 deprecation_warning_issued =
true;
245 PyErr_Format(PyExc_TypeError,
"Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
260 if (!batch->shader) {
275 if ((format.stride % 4) != 0) {
276 return "For POLYLINE shaders, only 4-byte aligned formats are supported";
279 int pos_attr_id = -1;
280 int col_attr_id = -1;
281 for (
uint a_idx = 0; a_idx < format.attr_len; a_idx++) {
283 if ((a->
offset % 4) != 0) {
284 return "For POLYLINE shaders, only 4-byte aligned attributes are supported";
287 if (pos_attr_id == -1 && name ==
"pos") {
289 return "For POLYLINE shaders, the 'pos' attribute needs to be 'F32'";
292 return "For POLYLINE shaders, the 'pos' attribute must use the 'FLOAT' fetch type";
296 else if (col_attr_id == -1 && name ==
"color") {
298 return "For POLYLINE shaders, the 'color' attribute needs to be 'F32' or 'U8'";
302 if (pos_attr_id != -1 && col_attr_id != -1) {
312 pygpu_batch_draw_doc,
313 ".. method:: draw(shader=None)\n"
315 " Run the drawing shader with the parameters assigned to the batch.\n"
317 " :param shader: Shader that performs the drawing operations.\n"
318 " If ``None`` is passed, the last shader set to this batch will run.\n"
319 " :type shader: :class:`gpu.types.GPUShader` | None\n");
322 static bool deprecation_warning_issued =
false;
327 static const char *_keywords[] = {
"shader",
nullptr};
328 static _PyArg_Parser _parser = {
335 if (!_PyArg_ParseTupleAndKeywordsFast(
340 if (py_shader ==
nullptr) {
341 if (!deprecation_warning_issued) {
344 PyErr_WarnEx(PyExc_DeprecationWarning,
345 "Calling GPUBatch.draw without specifying a shader is deprecated. "
346 "Please provide a valid GPUShader as the 'shader' parameter.",
348 deprecation_warning_issued =
true;
367 if (line_width > 1.0f || use_linesmooth) {
369 PyErr_WarnEx(PyExc_DeprecationWarning,
370 "Calling GPUBatch.draw to draw wide or smooth lines with "
371 "GPU_SHADER_3D_FLAT_COLOR is deprecated. "
372 "Use GPU_SHADER_3D_POLYLINE_FLAT_COLOR instead.",
376 PyErr_WarnEx(PyExc_DeprecationWarning,
377 "Calling GPUBatch.draw to draw wide or smooth lines with "
378 "GPU_SHADER_3D_SMOOTH_COLOR is deprecated. "
379 "Use GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR instead.",
383 PyErr_WarnEx(PyExc_DeprecationWarning,
384 "Calling GPUBatch.draw to draw wide or smooth lines with "
385 "GPU_SHADER_3D_UNIFORM_COLOR is deprecated. "
386 "Use GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR instead.",
397 PyErr_WarnEx(PyExc_DeprecationWarning,
398 "Calling GPUBatch.draw to draw points with "
399 "GPU_SHADER_3D_FLAT_COLOR is deprecated. "
400 "Use GPU_SHADER_3D_POINT_FLAT_COLOR instead.",
404 PyErr_WarnEx(PyExc_DeprecationWarning,
405 "Calling GPUBatch.draw to draw points with "
406 "GPU_SHADER_3D_SMOOTH_COLOR is deprecated. "
407 "Use GPU_SHADER_3D_POINT_FLAT_COLOR instead.",
411 PyErr_WarnEx(PyExc_DeprecationWarning,
412 "Calling GPUBatch.draw to draw points with "
413 "GPU_SHADER_3D_UNIFORM_COLOR is deprecated. "
414 "Use GPU_SHADER_3D_POINT_SMOOTH_COLOR instead.",
420 PyErr_SetString(PyExc_RuntimeError, error);
430 pygpu_batch_draw_instanced_doc,
431 ".. method:: draw_instanced(program, *, instance_start=0, instance_count=0)\n"
433 " Draw multiple instances of the drawing program with the parameters assigned\n"
434 " to the batch. In the vertex shader, ``gpu_InstanceIndex`` will contain the instance\n"
435 " number being drawn including the ``instance_start``.\n"
437 " :param program: Program that performs the drawing operations.\n"
438 " :type program: :class:`gpu.types.GPUShader`\n"
439 " :param instance_start: Number of the first instance to draw.\n"
440 " :type instance_start: int\n"
441 " :param instance_count: Number of instances to draw. When not provided or set to 0\n"
442 " the number of instances will be determined by the number of rows in the first\n"
444 " :type instance_count: int\n");
448 int instance_start = 0;
449 int instance_count = 0;
451 static const char *_keywords[] = {
"program",
"instance_start",
"instance_count",
nullptr};
452 static _PyArg_Parser _parser = {
457 ":GPUBatch.draw_instanced",
461 if (!_PyArg_ParseTupleAndKeywordsFast(
462 args, kw, &_parser, &
BPyGPUShader_Type, &py_program, &instance_start, &instance_count))
474 pygpu_batch_draw_range_doc,
475 ".. method:: draw_range(program, *, elem_start=0, elem_count=0)\n"
477 " Run the drawing program with the parameters assigned to the batch. "
478 "Only draw the ``elem_count`` elements of the index buffer starting at ``elem_start``.\n"
480 " :param program: Program that performs the drawing operations.\n"
481 " :type program: :class:`gpu.types.GPUShader`\n"
482 " :param elem_start: First index to draw. When not provided or set to 0 drawing\n"
483 " will start from the first element of the index buffer.\n"
484 " :type elem_start: int\n"
485 " :param elem_count: Number of elements of the index buffer to draw. When not\n"
486 " provided or set to 0 all elements from ``elem_start`` to the end of the\n"
487 " index buffer will be drawn.\n"
488 " :type elem_count: int\n");
495 static const char *_keywords[] = {
"program",
"elem_start",
"elem_count",
nullptr};
496 static _PyArg_Parser _parser = {
501 ":GPUBatch.draw_range",
505 if (!_PyArg_ParseTupleAndKeywordsFast(
536# pragma clang diagnostic push
537# pragma clang diagnostic ignored "-Wcast-function-type"
539# pragma GCC diagnostic push
540# pragma GCC diagnostic ignored "-Wcast-function-type"
548 pygpu_batch_vertbuf_add_doc},
552 pygpu_batch_program_set_doc},
555 METH_VARARGS | METH_KEYWORDS,
556 pygpu_batch_draw_doc},
559 METH_VARARGS | METH_KEYWORDS,
560 pygpu_batch_draw_instanced_doc},
563 METH_VARARGS | METH_KEYWORDS,
564 pygpu_batch_draw_range_doc},
565 {
"_program_use_begin",
573 {
nullptr,
nullptr, 0,
nullptr},
578# pragma clang diagnostic pop
580# pragma GCC diagnostic pop
584#ifdef USE_GPU_PY_REFERENCES
609#ifdef USE_GPU_PY_REFERENCES
610 PyObject_GC_UnTrack(self);
617 Py_TYPE(self)->tp_free(self);
623 ".. class:: GPUBatch\n"
625 " Reusable container for drawable geometry.\n"
627 " .. method:: __init__(type, buf, elem=None)\n"
629 " :param type: The primitive type of geometry to be drawn.\n"
632 " :param buf: Vertex buffer containing all or some of the attributes required for "
634 " :type buf: :class:`gpu.types.GPUVertBuf`\n"
635 " :param elem: An optional index buffer.\n"
636 " :type elem: :class:`gpu.types.GPUIndexBuf` | None\n");
638 PyVarObject_HEAD_INIT(
nullptr, 0)
657#ifdef USE_GPU_PY_REFERENCES
658 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
663#ifdef USE_GPU_PY_REFERENCES
689#ifdef USE_GPU_PY_REFERENCES
715#ifdef USE_GPU_PY_REFERENCES
724 return reinterpret_cast<PyObject *
>(self);
729#undef BPY_GPU_BATCH_CHECK_OBJ
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
#define PYDOC_PRIMTYPE_LITERAL
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
#define USE_GPU_PY_REFERENCES
#define BPyGPUShader_Check(v)
#define BPyGPUVertBuf_Check(v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PyTypeObject BPyGPUBatch_Type
bool bpygpu_shader_is_polyline(gpu::Shader *shader)
static void pygpu_batch__tp_dealloc(BPyGPUBatch *self)
static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
PyTypeObject BPyGPUVertBuf_Type
static PyObject * pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
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")
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
static constexpr int GPU_BATCH_VBO_MAX_LEN
static int pygpu_batch__tp_is_gc(BPyGPUBatch *self)
void GPU_batch_discard(gpu::Batch *batch)
PyTypeObject BPyGPUIndexBuf_Type
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_batch_draw_range(gpu::Batch *batch, int vertex_first, int vertex_count)
static PyMethodDef pygpu_batch__tp_methods[]
float GPU_line_width_get()
static const char * pygpu_shader_check_compatibility(gpu::Batch *batch)
@ GPU_SHADER_3D_SMOOTH_COLOR
@ GPU_SHADER_3D_UNIFORM_COLOR
@ GPU_SHADER_3D_FLAT_COLOR
static PyObject * pygpu_batch_draw(BPyGPUBatch *self, PyObject *args, PyObject *kw)
void GPU_batch_draw_instance_range(gpu::Batch *batch, int instance_first, int instance_count)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static int pygpu_batch__tp_clear(BPyGPUBatch *self)
void GPU_batch_draw(gpu::Batch *batch)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
static bool pygpu_batch_is_program_or_error(BPyGPUBatch *self)
static PyObject * pygpu_batch__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
static PyObject * pygpu_batch_draw_range(BPyGPUBatch *self, PyObject *args, PyObject *kw)
PyC_StringEnumItems bpygpu_primtype_items[]
bool GPU_line_smooth_get()
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
static PyObject * pygpu_batch_program_use_begin(BPyGPUBatch *self)
int PyC_ParseStringEnum(PyObject *o, void *p)
PyTypeObject BPyGPUShader_Type
static PyObject * pygpu_batch_program_use_end(BPyGPUBatch *self)
static void pygpu_batch_shader_set_reference_only(BPyGPUBatch *self, BPyGPUShader *py_shader)
static PyObject * pygpu_batch_draw_instanced(BPyGPUBatch *self, PyObject *args, PyObject *kw)
int PyC_ParseTypeOrNone(PyObject *o, void *p)
static void pygpu_batch_shader_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
PyObject * BPyGPUBatch_CreatePyObject(gpu::Batch *batch)
int GPU_batch_vertbuf_add(gpu::Batch *batch, gpu::VertBuf *vertex_buf, bool own_vbo)
static PyObject * pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
#define PyC_TYPE_OR_NONE_INIT(py_type, value_p)
header-only compatibility defines.
PyObject_HEAD gpu::Batch * batch
PyObject_HEAD gpu::IndexBuf * elem
PyObject_HEAD gpu::Shader * shader
PyObject_HEAD gpu::VertBuf * buf
GPUVertFetchMode fetch_mode() const
GPUVertCompType comp_type() const
struct blender::GPUVertAttr::Type type