| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright 2015, Blender Foundation. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup bpygpu | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file defines the offscreen functionalities of the 'gpu' module | 
					
						
							|  |  |  |  * used for off-screen OpenGL rendering. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - Use ``bpygpu_`` for local API. | 
					
						
							|  |  |  |  * - Use ``BPyGPU`` for public API. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "GPU_batch.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../mathutils/mathutils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  | #include "gpu_py.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "gpu_py_element.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-14 09:32:19 -03:00
										 |  |  | #include "gpu_py_shader.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #include "gpu_py_vertex_buffer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  | #include "gpu_py_batch.h" /* own include */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							| 
									
										
										
										
											2018-10-31 12:02:22 +11:00
										 |  |  | /** \name Utility Functions
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static bool pygpu_batch_is_program_or_error(BPyGPUBatch *self) | 
					
						
							| 
									
										
										
										
											2018-10-31 12:02:22 +11:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-09 00:52:45 +02:00
										 |  |  |   if (!self->batch->shader) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     PyErr_SetString(PyExc_RuntimeError, "batch does not have any program assigned to it"); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2018-10-31 12:02:22 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							| 
									
										
										
										
											2018-10-31 12:34:10 +11:00
										 |  |  | /** \name GPUBatch Type
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BPYGPU_IS_INIT_OR_ERROR_OBJ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   struct PyC_StringEnum prim_type = {bpygpu_primtype_items, GPU_PRIM_NONE}; | 
					
						
							|  |  |  |   BPyGPUVertBuf *py_vertbuf = NULL; | 
					
						
							|  |  |  |   BPyGPUIndexBuf *py_indexbuf = NULL; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   static const char *_keywords[] = {"type", "buf", "elem", NULL}; | 
					
						
							|  |  |  |   static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0}; | 
					
						
							|  |  |  |   if (!_PyArg_ParseTupleAndKeywordsFast(args, | 
					
						
							|  |  |  |                                         kwds, | 
					
						
							|  |  |  |                                         &_parser, | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |                                         PyC_ParseStringEnum, | 
					
						
							|  |  |  |                                         &prim_type, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |                                         &BPyGPUVertBuf_Type, | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |                                         &py_vertbuf, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |                                         &BPyGPUIndexBuf_Type, | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |                                         &py_indexbuf)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   BLI_assert(prim_type.value_found != GPU_PRIM_NONE); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   if (py_vertbuf == NULL) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   GPUBatch *batch = GPU_batch_create( | 
					
						
							|  |  |  |       prim_type.value_found, py_vertbuf->buf, py_indexbuf ? py_indexbuf->elem : NULL); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   BPyGPUBatch *ret = (BPyGPUBatch *)BPyGPUBatch_CreatePyObject(batch); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   ret->references = PyList_New(py_indexbuf ? 2 : 1); | 
					
						
							|  |  |  |   PyList_SET_ITEM(ret->references, 0, (PyObject *)py_vertbuf); | 
					
						
							|  |  |  |   Py_INCREF(py_vertbuf); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   if (py_indexbuf != NULL) { | 
					
						
							|  |  |  |     PyList_SET_ITEM(ret->references, 1, (PyObject *)py_indexbuf); | 
					
						
							|  |  |  |     Py_INCREF(py_indexbuf); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject_GC_Track(ret); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return (PyObject *)ret; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | PyDoc_STRVAR(pygpu_batch_vertbuf_add_doc, | 
					
						
							| 
									
										
										
										
											2018-11-13 12:59:50 +01:00
										 |  |  | ".. method:: vertbuf_add(buf)\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2018-11-14 09:04:24 +11:00
										 |  |  | "   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" | 
					
						
							| 
									
										
										
										
											2019-04-29 19:59:13 +10:00
										 |  |  | "   A good use case would be when you have a separate\n" | 
					
						
							|  |  |  | "   vertex buffer for vertex positions and vertex normals.\n" | 
					
						
							| 
									
										
										
										
											2018-11-14 09:04:24 +11:00
										 |  |  | "   Current a batch can have at most " STRINGIFY(GPU_BATCH_VBO_MAX_LEN) " vertex buffers.\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 12:59:50 +01:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2018-11-14 09:04:24 +11:00
										 |  |  | "   :param buf: The vertex buffer that will be added to the batch.\n" | 
					
						
							|  |  |  | "   :type buf: :class:`gpu.types.GPUVertBuf`\n" | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (!BPyGPUVertBuf_Check(py_buf)) { | 
					
						
							|  |  |  |     PyErr_Format(PyExc_TypeError, "Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 16:40:07 +02:00
										 |  |  |   if (GPU_vertbuf_get_vertex_len(self->batch->verts[0]) != | 
					
						
							|  |  |  |       GPU_vertbuf_get_vertex_len(py_buf->buf)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                  "Expected %d length, got %d", | 
					
						
							| 
									
										
										
										
											2020-09-06 16:40:07 +02:00
										 |  |  |                  GPU_vertbuf_get_vertex_len(self->batch->verts[0]), | 
					
						
							|  |  |  |                  GPU_vertbuf_get_vertex_len(py_buf->buf)); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (self->batch->verts[GPU_BATCH_VBO_MAX_LEN - 1] != NULL) { | 
					
						
							|  |  |  |     PyErr_SetString( | 
					
						
							|  |  |  |         PyExc_RuntimeError, | 
					
						
							|  |  |  |         "Maximum number of vertex buffers exceeded: " STRINGIFY(GPU_BATCH_VBO_MAX_LEN)); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-31 12:11:38 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   /* Hold user */ | 
					
						
							|  |  |  |   PyList_Append(self->references, (PyObject *)py_buf); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   GPU_batch_vertbuf_add(self->batch, py_buf->buf); | 
					
						
							|  |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | PyDoc_STRVAR( | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     pygpu_batch_program_set_doc, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     ".. method:: program_set(program)\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "   Assign a shader to this batch that will be used for drawing when not overwritten later.\n" | 
					
						
							|  |  |  |     "   Note: This method has to be called in the draw context that the batch will be drawn in.\n" | 
					
						
							| 
									
										
										
										
											2020-10-11 18:19:42 -04:00
										 |  |  |     "   This function does not need to be called when you always\n" | 
					
						
							|  |  |  |     "   set the shader when calling :meth:`gpu.types.GPUBatch.draw`.\n" | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     "\n" | 
					
						
							|  |  |  |     "   :param program: The program/shader the batch will use in future draw calls.\n" | 
					
						
							|  |  |  |     "   :type program: :class:`gpu.types.GPUShader`\n"); | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader) | 
					
						
							| 
									
										
										
										
											2018-09-14 09:32:19 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (!BPyGPUShader_Check(py_shader)) { | 
					
						
							|  |  |  |     PyErr_Format(PyExc_TypeError, "Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GPUShader *shader = py_shader->shader; | 
					
						
							| 
									
										
										
										
											2020-07-30 01:07:29 +02:00
										 |  |  |   GPU_batch_set_shader(self->batch, shader); | 
					
						
							| 
									
										
										
										
											2018-09-14 09:32:19 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:38:43 -03:00
										 |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   /* Remove existing user (if any), hold new user. */ | 
					
						
							|  |  |  |   int i = PyList_GET_SIZE(self->references); | 
					
						
							|  |  |  |   while (--i != -1) { | 
					
						
							|  |  |  |     PyObject *py_shader_test = PyList_GET_ITEM(self->references, i); | 
					
						
							|  |  |  |     if (BPyGPUShader_Check(py_shader_test)) { | 
					
						
							|  |  |  |       PyList_SET_ITEM(self->references, i, (PyObject *)py_shader); | 
					
						
							|  |  |  |       Py_INCREF(py_shader); | 
					
						
							|  |  |  |       Py_DECREF(py_shader_test); | 
					
						
							|  |  |  |       /* Only ever reference one shader. */ | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (i != -1) { | 
					
						
							|  |  |  |     PyList_Append(self->references, (PyObject *)py_shader); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-20 16:38:43 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2018-09-14 09:32:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | PyDoc_STRVAR(pygpu_batch_draw_doc, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |              ".. method:: draw(program=None)\n" | 
					
						
							|  |  |  |              "\n" | 
					
						
							|  |  |  |              "   Run the drawing program with the parameters assigned to the batch.\n" | 
					
						
							|  |  |  |              "\n" | 
					
						
							|  |  |  |              "   :param program: Program that performs the drawing operations.\n" | 
					
						
							| 
									
										
										
										
											2019-08-01 13:53:25 +10:00
										 |  |  |              "      If ``None`` is passed, the last program set to this batch will run.\n" | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |              "   :type program: :class:`gpu.types.GPUShader`\n"); | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch_draw(BPyGPUBatch *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BPyGPUShader *py_program = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!PyArg_ParseTuple(args, "|O!:GPUBatch.draw", &BPyGPUShader_Type, &py_program)) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  |   if (py_program == NULL) { | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     if (!pygpu_batch_is_program_or_error(self)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |       return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-09 00:52:45 +02:00
										 |  |  |   else if (self->batch->shader != py_program->shader) { | 
					
						
							| 
									
										
										
										
											2020-07-30 01:07:29 +02:00
										 |  |  |     GPU_batch_set_shader(self->batch, py_program->shader); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GPU_batch_draw(self->batch); | 
					
						
							|  |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch_program_use_begin(BPyGPUBatch *self) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |   if (!pygpu_batch_is_program_or_error(self)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-09 00:52:45 +02:00
										 |  |  |   GPU_shader_bind(self->batch->shader); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_batch_program_use_end(BPyGPUBatch *self) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |   if (!pygpu_batch_is_program_or_error(self)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-09 00:52:45 +02:00
										 |  |  |   GPU_shader_unbind(); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static struct PyMethodDef pygpu_batch__tp_methods[] = { | 
					
						
							|  |  |  |     {"vertbuf_add", (PyCFunction)pygpu_batch_vertbuf_add, METH_O, pygpu_batch_vertbuf_add_doc}, | 
					
						
							|  |  |  |     {"program_set", (PyCFunction)pygpu_batch_program_set, METH_O, pygpu_batch_program_set_doc}, | 
					
						
							|  |  |  |     {"draw", (PyCFunction)pygpu_batch_draw, METH_VARARGS, pygpu_batch_draw_doc}, | 
					
						
							|  |  |  |     {"_program_use_begin", (PyCFunction)pygpu_batch_program_use_begin, METH_NOARGS, ""}, | 
					
						
							|  |  |  |     {"_program_use_end", (PyCFunction)pygpu_batch_program_use_end, METH_NOARGS, ""}, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     {NULL, NULL, 0, NULL}, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_VISIT(self->references); | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static int pygpu_batch__tp_clear(BPyGPUBatch *self) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_CLEAR(self->references); | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static void pygpu_batch__tp_dealloc(BPyGPUBatch *self) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   GPU_batch_discard(self->batch); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2021-03-04 15:06:15 +11:00
										 |  |  |   PyObject_GC_UnTrack(self); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (self->references) { | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     pygpu_batch__tp_clear(self); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     Py_XDECREF(self->references); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_TYPE(self)->tp_free(self); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | PyDoc_STRVAR( | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     pygpu_batch__tp_doc, | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     ".. class:: GPUBatch(type, buf, elem=None)\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "   Reusable container for drawable geometry.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2021-03-16 12:48:00 -03:00
										 |  |  |     "   :arg type: The primitive type of geometry to be drawn.\n" | 
					
						
							|  |  |  |     "      Possible values are `POINTS`, `LINES`, `TRIS`, `LINE_STRIP`, `LINE_LOOP`, `TRI_STRIP`, " | 
					
						
							|  |  |  |     "`TRI_FAN`, `LINES_ADJ`, `TRIS_ADJ` and `LINE_STRIP_ADJ`.\n" | 
					
						
							|  |  |  |     "   :type type: str\n" | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     "   :arg buf: Vertex buffer containing all or some of the attributes required for drawing.\n" | 
					
						
							|  |  |  |     "   :type buf: :class:`gpu.types.GPUVertBuf`\n" | 
					
						
							|  |  |  |     "   :arg elem: An optional index buffer.\n" | 
					
						
							|  |  |  |     "   :type elem: :class:`gpu.types.GPUIndexBuf`\n"); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | PyTypeObject BPyGPUBatch_Type = { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUBatch", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(BPyGPUBatch), | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     .tp_dealloc = (destructor)pygpu_batch__tp_dealloc, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     .tp_doc = pygpu_batch__tp_doc, | 
					
						
							|  |  |  |     .tp_traverse = (traverseproc)pygpu_batch__tp_traverse, | 
					
						
							|  |  |  |     .tp_clear = (inquiry)pygpu_batch__tp_clear, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     .tp_methods = pygpu_batch__tp_methods, | 
					
						
							|  |  |  |     .tp_new = pygpu_batch__tp_new, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Public API
 | 
					
						
							| 
									
										
										
										
											2019-02-11 10:51:25 +11:00
										 |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyObject *BPyGPUBatch_CreatePyObject(GPUBatch *batch) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BPyGPUBatch *self; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_GPU_PY_REFERENCES
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   self = (BPyGPUBatch *)_PyObject_GC_New(&BPyGPUBatch_Type); | 
					
						
							|  |  |  |   self->references = NULL; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   self = PyObject_New(BPyGPUBatch, &BPyGPUBatch_Type); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   self->batch = batch; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return (PyObject *)self; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef BPY_GPU_BATCH_CHECK_OBJ
 |