Blender  V3.3
gpu_py_batch.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2015 Blender Foundation. */
3 
14 #include <Python.h>
15 
16 #include "BLI_utildefines.h"
17 
18 #include "GPU_batch.h"
19 
20 #include "../generic/py_capi_utils.h"
21 
22 #include "gpu_py.h"
23 #include "gpu_py_element.h"
24 #include "gpu_py_shader.h"
25 #include "gpu_py_vertex_buffer.h"
26 
27 #include "gpu_py_batch.h" /* own include */
28 
29 /* -------------------------------------------------------------------- */
34 {
35  if (!self->batch->shader) {
36  PyErr_SetString(PyExc_RuntimeError, "batch does not have any program assigned to it");
37  return false;
38  }
39  return true;
40 }
41 
44 /* -------------------------------------------------------------------- */
48 static PyObject *pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
49 {
51 
52  const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
53 
55  BPyGPUVertBuf *py_vertbuf = NULL;
56  BPyGPUIndexBuf *py_indexbuf = NULL;
57 
58  static const char *_keywords[] = {"type", "buf", "elem", NULL};
59  static _PyArg_Parser _parser = {
60  "|$" /* Optional keyword only arguments. */
61  "O&" /* `type` */
62  "O!" /* `buf` */
63  "O!" /* `elem` */
64  ":GPUBatch.__new__",
65  _keywords,
66  0,
67  };
68  if (!_PyArg_ParseTupleAndKeywordsFast(args,
69  kwds,
70  &_parser,
72  &prim_type,
74  &py_vertbuf,
76  &py_indexbuf)) {
77  return NULL;
78  }
79 
80  BLI_assert(prim_type.value_found != GPU_PRIM_NONE);
81  if (prim_type.value_found == GPU_PRIM_LINE_LOOP) {
82  PyErr_WarnEx(PyExc_DeprecationWarning,
83  "'LINE_LOOP' is deprecated. Please use 'LINE_STRIP' and close the segment.",
84  1);
85  }
86  else if (prim_type.value_found == GPU_PRIM_TRI_FAN) {
87  PyErr_WarnEx(
88  PyExc_DeprecationWarning,
89  "'TRI_FAN' is deprecated. Please use 'TRI_STRIP' or 'TRIS' and try modifying your "
90  "vertices or indices to match the topology.",
91  1);
92  }
93 
94  if (py_vertbuf == NULL) {
95  PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
96  return NULL;
97  }
98 
100  prim_type.value_found, py_vertbuf->buf, py_indexbuf ? py_indexbuf->elem : NULL);
101 
103 
104 #ifdef USE_GPU_PY_REFERENCES
105  ret->references = PyList_New(py_indexbuf ? 2 : 1);
106  PyList_SET_ITEM(ret->references, 0, (PyObject *)py_vertbuf);
107  Py_INCREF(py_vertbuf);
108 
109  if (py_indexbuf != NULL) {
110  PyList_SET_ITEM(ret->references, 1, (PyObject *)py_indexbuf);
111  Py_INCREF(py_indexbuf);
112  }
113 
114  PyObject_GC_Track(ret);
115 #endif
116 
117  return (PyObject *)ret;
118 }
119 
120 PyDoc_STRVAR(pygpu_batch_vertbuf_add_doc,
121 ".. method:: vertbuf_add(buf)\n"
122 "\n"
123 " Add another vertex buffer to the Batch.\n"
124 " It is not possible to add more vertices to the batch using this method.\n"
125 " Instead it can be used to add more attributes to the existing vertices.\n"
126 " A good use case would be when you have a separate\n"
127 " vertex buffer for vertex positions and vertex normals.\n"
128 " Current a batch can have at most " STRINGIFY(GPU_BATCH_VBO_MAX_LEN) " vertex buffers.\n"
129 "\n"
130 " :param buf: The vertex buffer that will be added to the batch.\n"
131 " :type buf: :class:`gpu.types.GPUVertBuf`\n"
132 );
133 static PyObject *pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
134 {
135  if (!BPyGPUVertBuf_Check(py_buf)) {
136  PyErr_Format(PyExc_TypeError, "Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
137  return NULL;
138  }
139 
140  if (GPU_vertbuf_get_vertex_len(self->batch->verts[0]) !=
141  GPU_vertbuf_get_vertex_len(py_buf->buf)) {
142  PyErr_Format(PyExc_TypeError,
143  "Expected %d length, got %d",
144  GPU_vertbuf_get_vertex_len(self->batch->verts[0]),
146  return NULL;
147  }
148 
149  if (self->batch->verts[GPU_BATCH_VBO_MAX_LEN - 1] != NULL) {
150  PyErr_SetString(
151  PyExc_RuntimeError,
152  "Maximum number of vertex buffers exceeded: " STRINGIFY(GPU_BATCH_VBO_MAX_LEN));
153  return NULL;
154  }
155 
156 #ifdef USE_GPU_PY_REFERENCES
157  /* Hold user */
158  PyList_Append(self->references, (PyObject *)py_buf);
159 #endif
160 
161  GPU_batch_vertbuf_add(self->batch, py_buf->buf);
162  Py_RETURN_NONE;
163 }
164 
166  pygpu_batch_program_set_doc,
167  ".. method:: program_set(program)\n"
168  "\n"
169  " Assign a shader to this batch that will be used for drawing when not overwritten later.\n"
170  " Note: This method has to be called in the draw context that the batch will be drawn in.\n"
171  " This function does not need to be called when you always\n"
172  " set the shader when calling :meth:`gpu.types.GPUBatch.draw`.\n"
173  "\n"
174  " :param program: The program/shader the batch will use in future draw calls.\n"
175  " :type program: :class:`gpu.types.GPUShader`\n");
176 static PyObject *pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
177 {
178  if (!BPyGPUShader_Check(py_shader)) {
179  PyErr_Format(PyExc_TypeError, "Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
180  return NULL;
181  }
182 
183  GPUShader *shader = py_shader->shader;
184  GPU_batch_set_shader(self->batch, shader);
185 
186 #ifdef USE_GPU_PY_REFERENCES
187  /* Remove existing user (if any), hold new user. */
188  int i = PyList_GET_SIZE(self->references);
189  while (--i != -1) {
190  PyObject *py_shader_test = PyList_GET_ITEM(self->references, i);
191  if (BPyGPUShader_Check(py_shader_test)) {
192  PyList_SET_ITEM(self->references, i, (PyObject *)py_shader);
193  Py_INCREF(py_shader);
194  Py_DECREF(py_shader_test);
195  /* Only ever reference one shader. */
196  break;
197  }
198  }
199  if (i != -1) {
200  PyList_Append(self->references, (PyObject *)py_shader);
201  }
202 #endif
203 
204  Py_RETURN_NONE;
205 }
206 
207 PyDoc_STRVAR(pygpu_batch_draw_doc,
208  ".. method:: draw(program=None)\n"
209  "\n"
210  " Run the drawing program with the parameters assigned to the batch.\n"
211  "\n"
212  " :param program: Program that performs the drawing operations.\n"
213  " If ``None`` is passed, the last program set to this batch will run.\n"
214  " :type program: :class:`gpu.types.GPUShader`\n");
215 static PyObject *pygpu_batch_draw(BPyGPUBatch *self, PyObject *args)
216 {
217  BPyGPUShader *py_program = NULL;
218 
219  if (!PyArg_ParseTuple(args, "|O!:GPUBatch.draw", &BPyGPUShader_Type, &py_program)) {
220  return NULL;
221  }
222  if (py_program == NULL) {
223  if (!pygpu_batch_is_program_or_error(self)) {
224  return NULL;
225  }
226  }
227  else if (self->batch->shader != py_program->shader) {
228  GPU_batch_set_shader(self->batch, py_program->shader);
229  }
230 
231  GPU_batch_draw(self->batch);
232  Py_RETURN_NONE;
233 }
234 
236 {
237  if (!pygpu_batch_is_program_or_error(self)) {
238  return NULL;
239  }
240  GPU_shader_bind(self->batch->shader);
241  Py_RETURN_NONE;
242 }
243 
245 {
246  if (!pygpu_batch_is_program_or_error(self)) {
247  return NULL;
248  }
250  Py_RETURN_NONE;
251 }
252 
253 static struct PyMethodDef pygpu_batch__tp_methods[] = {
254  {"vertbuf_add", (PyCFunction)pygpu_batch_vertbuf_add, METH_O, pygpu_batch_vertbuf_add_doc},
255  {"program_set", (PyCFunction)pygpu_batch_program_set, METH_O, pygpu_batch_program_set_doc},
256  {"draw", (PyCFunction)pygpu_batch_draw, METH_VARARGS, pygpu_batch_draw_doc},
257  {"_program_use_begin", (PyCFunction)pygpu_batch_program_use_begin, METH_NOARGS, ""},
258  {"_program_use_end", (PyCFunction)pygpu_batch_program_use_end, METH_NOARGS, ""},
259  {NULL, NULL, 0, NULL},
260 };
261 
262 #ifdef USE_GPU_PY_REFERENCES
263 
264 static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
265 {
266  Py_VISIT(self->references);
267  return 0;
268 }
269 
271 {
272  Py_CLEAR(self->references);
273  return 0;
274 }
275 
276 #endif
277 
279 {
280  GPU_batch_discard(self->batch);
281 
282 #ifdef USE_GPU_PY_REFERENCES
283  PyObject_GC_UnTrack(self);
284  if (self->references) {
285  pygpu_batch__tp_clear(self);
286  Py_XDECREF(self->references);
287  }
288 #endif
289 
290  Py_TYPE(self)->tp_free(self);
291 }
292 
294  pygpu_batch__tp_doc,
295  ".. class:: GPUBatch(type, buf, elem=None)\n"
296  "\n"
297  " Reusable container for drawable geometry.\n"
298  "\n"
299  " :arg type: The primitive type of geometry to be drawn.\n"
300  " Possible values are `POINTS`, `LINES`, `TRIS`, `LINE_STRIP`, `LINE_LOOP`, `TRI_STRIP`, "
301  "`TRI_FAN`, `LINES_ADJ`, `TRIS_ADJ` and `LINE_STRIP_ADJ`.\n"
302  " :type type: str\n"
303  " :arg buf: Vertex buffer containing all or some of the attributes required for drawing.\n"
304  " :type buf: :class:`gpu.types.GPUVertBuf`\n"
305  " :arg elem: An optional index buffer.\n"
306  " :type elem: :class:`gpu.types.GPUIndexBuf`\n");
307 PyTypeObject BPyGPUBatch_Type = {
308  PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUBatch",
309  .tp_basicsize = sizeof(BPyGPUBatch),
310  .tp_dealloc = (destructor)pygpu_batch__tp_dealloc,
311 #ifdef USE_GPU_PY_REFERENCES
312  .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
313  .tp_doc = pygpu_batch__tp_doc,
314  .tp_traverse = (traverseproc)pygpu_batch__tp_traverse,
315  .tp_clear = (inquiry)pygpu_batch__tp_clear,
316 #else
317  .tp_flags = Py_TPFLAGS_DEFAULT,
318 #endif
319  .tp_methods = pygpu_batch__tp_methods,
320  .tp_new = pygpu_batch__tp_new,
321 };
322 
325 /* -------------------------------------------------------------------- */
330 {
331  BPyGPUBatch *self;
332 
333 #ifdef USE_GPU_PY_REFERENCES
334  self = (BPyGPUBatch *)_PyObject_GC_New(&BPyGPUBatch_Type);
335  self->references = NULL;
336 #else
337  self = PyObject_New(BPyGPUBatch, &BPyGPUBatch_Type);
338 #endif
339 
340  self->batch = batch;
341 
342  return (PyObject *)self;
343 }
344 
347 #undef BPY_GPU_BATCH_CHECK_OBJ
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define STRINGIFY(x)
#define UNUSED(x)
GPUBatch
Definition: GPU_batch.h:78
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
Definition: gpu_batch.cc:211
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:109
#define GPU_batch_create(prim, verts, elem)
Definition: GPU_batch.h:95
#define GPU_BATCH_VBO_MAX_LEN
Definition: GPU_batch.h:20
#define GPU_batch_vertbuf_add(batch, verts)
Definition: GPU_batch.h:124
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
@ GPU_PRIM_TRI_FAN
Definition: GPU_primitive.h:25
@ GPU_PRIM_LINE_LOOP
Definition: GPU_primitive.h:23
@ GPU_PRIM_NONE
Definition: GPU_primitive.h:33
void GPU_shader_unbind(void)
Definition: gpu_shader.cc:513
struct GPUShader GPUShader
Definition: GPU_shader.h:20
void GPU_shader_bind(GPUShader *shader)
Definition: gpu_shader.cc:491
uint GPU_vertbuf_get_vertex_len(const GPUVertBuf *verts)
PyObject * self
Definition: bpy_driver.c:165
struct @653::@655 batch
struct PyC_StringEnumItems bpygpu_primtype_items[]
Definition: gpu_py.c:24
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition: gpu_py.h:14
static PyObject * pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
Definition: gpu_py_batch.c:48
static int pygpu_batch__tp_clear(BPyGPUBatch *self)
Definition: gpu_py_batch.c:270
static PyObject * pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
Definition: gpu_py_batch.c:176
static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
Definition: gpu_py_batch.c:264
static void pygpu_batch__tp_dealloc(BPyGPUBatch *self)
Definition: gpu_py_batch.c:278
static bool pygpu_batch_is_program_or_error(BPyGPUBatch *self)
Definition: gpu_py_batch.c:33
PyObject * BPyGPUBatch_CreatePyObject(GPUBatch *batch)
Definition: gpu_py_batch.c:329
static PyObject * pygpu_batch_draw(BPyGPUBatch *self, PyObject *args)
Definition: gpu_py_batch.c:215
static PyObject * pygpu_batch_program_use_begin(BPyGPUBatch *self)
Definition: gpu_py_batch.c:235
PyDoc_STRVAR(pygpu_batch_vertbuf_add_doc, ".. method:: vertbuf_add(buf)\n" "\n" " Add another vertex buffer to the Batch.\n" " It is not possible to add more vertices to the batch using this method.\n" " Instead it can be used to add more attributes to the existing vertices.\n" " A good use case would be when you have a separate\n" " vertex buffer for vertex positions and vertex normals.\n" " Current a batch can have at most " STRINGIFY(GPU_BATCH_VBO_MAX_LEN) " vertex buffers.\n" "\n" " :param buf: The vertex buffer that will be added to the batch.\n" " :type buf: :class:`gpu.types.GPUVertBuf`\n")
static struct PyMethodDef pygpu_batch__tp_methods[]
Definition: gpu_py_batch.c:253
static PyObject * pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
Definition: gpu_py_batch.c:133
static PyObject * pygpu_batch_program_use_end(BPyGPUBatch *self)
Definition: gpu_py_batch.c:244
PyTypeObject BPyGPUBatch_Type
Definition: gpu_py_batch.c:307
struct BPyGPUBatch BPyGPUBatch
PyTypeObject BPyGPUIndexBuf_Type
PyTypeObject BPyGPUShader_Type
#define BPyGPUShader_Check(v)
Definition: gpu_py_shader.h:17
PyTypeObject BPyGPUVertBuf_Type
#define BPyGPUVertBuf_Check(v)
int PyC_ParseStringEnum(PyObject *o, void *p)
return ret
PyObject * references
Definition: gpu_py_batch.h:23
PyObject_VAR_HEAD struct GPUIndexBuf * elem
PyObject_VAR_HEAD struct GPUShader * shader
Definition: gpu_py_shader.h:21
PyObject_VAR_HEAD struct GPUVertBuf * buf