Blender  V2.93
gpu_py_batch.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2015, Blender Foundation.
17  */
18 
29 #include <Python.h>
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "BLI_utildefines.h"
34 
35 #include "GPU_batch.h"
36 
37 #include "../mathutils/mathutils.h"
38 
39 #include "../generic/py_capi_utils.h"
40 
41 #include "gpu_py.h"
42 #include "gpu_py_element.h"
43 #include "gpu_py_shader.h"
44 #include "gpu_py_vertex_buffer.h"
45 
46 #include "gpu_py_batch.h" /* own include */
47 
48 /* -------------------------------------------------------------------- */
53 {
54  if (!self->batch->shader) {
55  PyErr_SetString(PyExc_RuntimeError, "batch does not have any program assigned to it");
56  return false;
57  }
58  return true;
59 }
60 
63 /* -------------------------------------------------------------------- */
67 static PyObject *pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
68 {
70 
71  const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
72 
74  BPyGPUVertBuf *py_vertbuf = NULL;
75  BPyGPUIndexBuf *py_indexbuf = NULL;
76 
77  static const char *_keywords[] = {"type", "buf", "elem", NULL};
78  static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0};
79  if (!_PyArg_ParseTupleAndKeywordsFast(args,
80  kwds,
81  &_parser,
83  &prim_type,
85  &py_vertbuf,
87  &py_indexbuf)) {
88  return NULL;
89  }
90 
91  BLI_assert(prim_type.value_found != GPU_PRIM_NONE);
92 
93  if (py_vertbuf == NULL) {
94  PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
95  return NULL;
96  }
97 
99  prim_type.value_found, py_vertbuf->buf, py_indexbuf ? py_indexbuf->elem : NULL);
100 
102 
103 #ifdef USE_GPU_PY_REFERENCES
104  ret->references = PyList_New(py_indexbuf ? 2 : 1);
105  PyList_SET_ITEM(ret->references, 0, (PyObject *)py_vertbuf);
106  Py_INCREF(py_vertbuf);
107 
108  if (py_indexbuf != NULL) {
109  PyList_SET_ITEM(ret->references, 1, (PyObject *)py_indexbuf);
110  Py_INCREF(py_indexbuf);
111  }
112 
113  PyObject_GC_Track(ret);
114 #endif
115 
116  return (PyObject *)ret;
117 }
118 
119 PyDoc_STRVAR(pygpu_batch_vertbuf_add_doc,
120 ".. method:: vertbuf_add(buf)\n"
121 "\n"
122 " Add another vertex buffer to the Batch.\n"
123 " It is not possible to add more vertices to the batch using this method.\n"
124 " Instead it can be used to add more attributes to the existing vertices.\n"
125 " A good use case would be when you have a separate\n"
126 " vertex buffer for vertex positions and vertex normals.\n"
127 " Current a batch can have at most " STRINGIFY(GPU_BATCH_VBO_MAX_LEN) " vertex buffers.\n"
128 "\n"
129 " :param buf: The vertex buffer that will be added to the batch.\n"
130 " :type buf: :class:`gpu.types.GPUVertBuf`\n"
131 );
132 static PyObject *pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
133 {
134  if (!BPyGPUVertBuf_Check(py_buf)) {
135  PyErr_Format(PyExc_TypeError, "Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
136  return NULL;
137  }
138 
139  if (GPU_vertbuf_get_vertex_len(self->batch->verts[0]) !=
140  GPU_vertbuf_get_vertex_len(py_buf->buf)) {
141  PyErr_Format(PyExc_TypeError,
142  "Expected %d length, got %d",
143  GPU_vertbuf_get_vertex_len(self->batch->verts[0]),
145  return NULL;
146  }
147 
148  if (self->batch->verts[GPU_BATCH_VBO_MAX_LEN - 1] != NULL) {
149  PyErr_SetString(
150  PyExc_RuntimeError,
151  "Maximum number of vertex buffers exceeded: " STRINGIFY(GPU_BATCH_VBO_MAX_LEN));
152  return NULL;
153  }
154 
155 #ifdef USE_GPU_PY_REFERENCES
156  /* Hold user */
157  PyList_Append(self->references, (PyObject *)py_buf);
158 #endif
159 
160  GPU_batch_vertbuf_add(self->batch, py_buf->buf);
161  Py_RETURN_NONE;
162 }
163 
165  pygpu_batch_program_set_doc,
166  ".. method:: program_set(program)\n"
167  "\n"
168  " Assign a shader to this batch that will be used for drawing when not overwritten later.\n"
169  " Note: This method has to be called in the draw context that the batch will be drawn in.\n"
170  " This function does not need to be called when you always\n"
171  " set the shader when calling :meth:`gpu.types.GPUBatch.draw`.\n"
172  "\n"
173  " :param program: The program/shader the batch will use in future draw calls.\n"
174  " :type program: :class:`gpu.types.GPUShader`\n");
175 static PyObject *pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
176 {
177  if (!BPyGPUShader_Check(py_shader)) {
178  PyErr_Format(PyExc_TypeError, "Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
179  return NULL;
180  }
181 
182  GPUShader *shader = py_shader->shader;
184 
185 #ifdef USE_GPU_PY_REFERENCES
186  /* Remove existing user (if any), hold new user. */
187  int i = PyList_GET_SIZE(self->references);
188  while (--i != -1) {
189  PyObject *py_shader_test = PyList_GET_ITEM(self->references, i);
190  if (BPyGPUShader_Check(py_shader_test)) {
191  PyList_SET_ITEM(self->references, i, (PyObject *)py_shader);
192  Py_INCREF(py_shader);
193  Py_DECREF(py_shader_test);
194  /* Only ever reference one shader. */
195  break;
196  }
197  }
198  if (i != -1) {
199  PyList_Append(self->references, (PyObject *)py_shader);
200  }
201 #endif
202 
203  Py_RETURN_NONE;
204 }
205 
206 PyDoc_STRVAR(pygpu_batch_draw_doc,
207  ".. method:: draw(program=None)\n"
208  "\n"
209  " Run the drawing program with the parameters assigned to the batch.\n"
210  "\n"
211  " :param program: Program that performs the drawing operations.\n"
212  " If ``None`` is passed, the last program set to this batch will run.\n"
213  " :type program: :class:`gpu.types.GPUShader`\n");
214 static PyObject *pygpu_batch_draw(BPyGPUBatch *self, PyObject *args)
215 {
216  BPyGPUShader *py_program = NULL;
217 
218  if (!PyArg_ParseTuple(args, "|O!:GPUBatch.draw", &BPyGPUShader_Type, &py_program)) {
219  return NULL;
220  }
221  if (py_program == NULL) {
222  if (!pygpu_batch_is_program_or_error(self)) {
223  return NULL;
224  }
225  }
226  else if (self->batch->shader != py_program->shader) {
227  GPU_batch_set_shader(self->batch, py_program->shader);
228  }
229 
230  GPU_batch_draw(self->batch);
231  Py_RETURN_NONE;
232 }
233 
235 {
236  if (!pygpu_batch_is_program_or_error(self)) {
237  return NULL;
238  }
239  GPU_shader_bind(self->batch->shader);
240  Py_RETURN_NONE;
241 }
242 
244 {
245  if (!pygpu_batch_is_program_or_error(self)) {
246  return NULL;
247  }
249  Py_RETURN_NONE;
250 }
251 
252 static struct PyMethodDef pygpu_batch__tp_methods[] = {
253  {"vertbuf_add", (PyCFunction)pygpu_batch_vertbuf_add, METH_O, pygpu_batch_vertbuf_add_doc},
254  {"program_set", (PyCFunction)pygpu_batch_program_set, METH_O, pygpu_batch_program_set_doc},
255  {"draw", (PyCFunction)pygpu_batch_draw, METH_VARARGS, pygpu_batch_draw_doc},
256  {"_program_use_begin", (PyCFunction)pygpu_batch_program_use_begin, METH_NOARGS, ""},
257  {"_program_use_end", (PyCFunction)pygpu_batch_program_use_end, METH_NOARGS, ""},
258  {NULL, NULL, 0, NULL},
259 };
260 
261 #ifdef USE_GPU_PY_REFERENCES
262 
263 static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
264 {
265  Py_VISIT(self->references);
266  return 0;
267 }
268 
270 {
271  Py_CLEAR(self->references);
272  return 0;
273 }
274 
275 #endif
276 
278 {
279  GPU_batch_discard(self->batch);
280 
281 #ifdef USE_GPU_PY_REFERENCES
282  PyObject_GC_UnTrack(self);
283  if (self->references) {
284  pygpu_batch__tp_clear(self);
285  Py_XDECREF(self->references);
286  }
287 #endif
288 
289  Py_TYPE(self)->tp_free(self);
290 }
291 
293  pygpu_batch__tp_doc,
294  ".. class:: GPUBatch(type, buf, elem=None)\n"
295  "\n"
296  " Reusable container for drawable geometry.\n"
297  "\n"
298  " :arg type: The primitive type of geometry to be drawn.\n"
299  " Possible values are `POINTS`, `LINES`, `TRIS`, `LINE_STRIP`, `LINE_LOOP`, `TRI_STRIP`, "
300  "`TRI_FAN`, `LINES_ADJ`, `TRIS_ADJ` and `LINE_STRIP_ADJ`.\n"
301  " :type type: str\n"
302  " :arg buf: Vertex buffer containing all or some of the attributes required for drawing.\n"
303  " :type buf: :class:`gpu.types.GPUVertBuf`\n"
304  " :arg elem: An optional index buffer.\n"
305  " :type elem: :class:`gpu.types.GPUIndexBuf`\n");
306 PyTypeObject BPyGPUBatch_Type = {
307  PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUBatch",
308  .tp_basicsize = sizeof(BPyGPUBatch),
309  .tp_dealloc = (destructor)pygpu_batch__tp_dealloc,
310 #ifdef USE_GPU_PY_REFERENCES
311  .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
312  .tp_doc = pygpu_batch__tp_doc,
313  .tp_traverse = (traverseproc)pygpu_batch__tp_traverse,
314  .tp_clear = (inquiry)pygpu_batch__tp_clear,
315 #else
316  .tp_flags = Py_TPFLAGS_DEFAULT,
317 #endif
318  .tp_methods = pygpu_batch__tp_methods,
319  .tp_new = pygpu_batch__tp_new,
320 };
321 
324 /* -------------------------------------------------------------------- */
329 {
330  BPyGPUBatch *self;
331 
332 #ifdef USE_GPU_PY_REFERENCES
333  self = (BPyGPUBatch *)_PyObject_GC_New(&BPyGPUBatch_Type);
334  self->references = NULL;
335 #else
336  self = PyObject_New(BPyGPUBatch, &BPyGPUBatch_Type);
337 #endif
338 
339  self->batch = batch;
340 
341  return (PyObject *)self;
342 }
343 
346 #undef BPY_GPU_BATCH_CHECK_OBJ
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define STRINGIFY(x)
#define UNUSED(x)
GPUBatch
Definition: GPU_batch.h:93
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
Definition: gpu_batch.cc:222
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:127
#define GPU_batch_create(prim, verts, elem)
Definition: GPU_batch.h:107
#define GPU_BATCH_VBO_MAX_LEN
Definition: GPU_batch.h:35
#define GPU_batch_vertbuf_add(batch, verts)
Definition: GPU_batch.h:121
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:234
_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_NONE
Definition: GPU_primitive.h:47
void GPU_shader_unbind(void)
Definition: gpu_shader.cc:516
struct GPUShader GPUShader
Definition: GPU_shader.h:33
void GPU_shader_bind(GPUShader *shader)
Definition: gpu_shader.cc:494
uint GPU_vertbuf_get_vertex_len(const GPUVertBuf *verts)
Read Guarded memory(de)allocation.
PyObject * self
Definition: bpy_driver.c:185
GPUBatch * batch
Definition: drawnode.c:3779
struct PyC_StringEnumItems bpygpu_primtype_items[]
Definition: gpu_py.c:38
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition: gpu_py.h:28
static PyObject * pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
Definition: gpu_py_batch.c:67
static int pygpu_batch__tp_clear(BPyGPUBatch *self)
Definition: gpu_py_batch.c:269
static PyObject * pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
Definition: gpu_py_batch.c:175
static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
Definition: gpu_py_batch.c:263
static void pygpu_batch__tp_dealloc(BPyGPUBatch *self)
Definition: gpu_py_batch.c:277
static bool pygpu_batch_is_program_or_error(BPyGPUBatch *self)
Definition: gpu_py_batch.c:52
PyObject * BPyGPUBatch_CreatePyObject(GPUBatch *batch)
Definition: gpu_py_batch.c:328
static PyObject * pygpu_batch_draw(BPyGPUBatch *self, PyObject *args)
Definition: gpu_py_batch.c:214
static PyObject * pygpu_batch_program_use_begin(BPyGPUBatch *self)
Definition: gpu_py_batch.c:234
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:252
static PyObject * pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
Definition: gpu_py_batch.c:132
static PyObject * pygpu_batch_program_use_end(BPyGPUBatch *self)
Definition: gpu_py_batch.c:243
PyTypeObject BPyGPUBatch_Type
Definition: gpu_py_batch.c:306
struct BPyGPUBatch BPyGPUBatch
PyTypeObject BPyGPUIndexBuf_Type
PyTypeObject BPyGPUShader_Type
#define BPyGPUShader_Check(v)
Definition: gpu_py_shader.h:25
PyTypeObject BPyGPUVertBuf_Type
#define BPyGPUVertBuf_Check(v)
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
int PyC_ParseStringEnum(PyObject *o, void *p)
return ret
PyObject * references
Definition: gpu_py_batch.h:37
PyObject_VAR_HEAD struct GPUIndexBuf * elem
PyObject_VAR_HEAD struct GPUShader * shader
Definition: gpu_py_shader.h:29
PyObject_VAR_HEAD struct GPUVertBuf * buf