37 #include "../mathutils/mathutils.h"
39 #include "../generic/py_capi_utils.h"
54 if (!
self->batch->shader) {
55 PyErr_SetString(PyExc_RuntimeError,
"batch does not have any program assigned to it");
71 const char *exc_str_missing_arg =
"GPUBatch.__new__() missing required argument '%s' (pos %d)";
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,
93 if (py_vertbuf ==
NULL) {
94 PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
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);
108 if (py_indexbuf !=
NULL) {
109 PyList_SET_ITEM(
ret->references, 1, (PyObject *)py_indexbuf);
110 Py_INCREF(py_indexbuf);
113 PyObject_GC_Track(
ret);
116 return (PyObject *)
ret;
120 ".. method:: vertbuf_add(buf)\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"
129 " :param buf: The vertex buffer that will be added to the batch.\n"
130 " :type buf: :class:`gpu.types.GPUVertBuf`\n"
135 PyErr_Format(PyExc_TypeError,
"Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
141 PyErr_Format(PyExc_TypeError,
142 "Expected %d length, got %d",
155 #ifdef USE_GPU_PY_REFERENCES
157 PyList_Append(
self->references, (PyObject *)py_buf);
165 pygpu_batch_program_set_doc,
166 ".. method:: program_set(program)\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"
173 " :param program: The program/shader the batch will use in future draw calls.\n"
174 " :type program: :class:`gpu.types.GPUShader`\n");
178 PyErr_Format(PyExc_TypeError,
"Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
185 #ifdef USE_GPU_PY_REFERENCES
187 int i = PyList_GET_SIZE(
self->references);
189 PyObject *py_shader_test = PyList_GET_ITEM(
self->references, i);
191 PyList_SET_ITEM(
self->references, i, (PyObject *)py_shader);
192 Py_INCREF(py_shader);
193 Py_DECREF(py_shader_test);
199 PyList_Append(
self->references, (PyObject *)py_shader);
207 ".. method:: draw(program=None)\n"
209 " Run the drawing program with the parameters assigned to the batch.\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");
218 if (!PyArg_ParseTuple(args,
"|O!:GPUBatch.draw", &
BPyGPUShader_Type, &py_program)) {
221 if (py_program ==
NULL) {
226 else if (
self->batch->shader != py_program->
shader) {
255 {
"draw", (PyCFunction)
pygpu_batch_draw, METH_VARARGS, pygpu_batch_draw_doc},
261 #ifdef USE_GPU_PY_REFERENCES
265 Py_VISIT(
self->references);
271 Py_CLEAR(
self->references);
281 #ifdef USE_GPU_PY_REFERENCES
282 PyObject_GC_UnTrack(
self);
283 if (
self->references) {
285 Py_XDECREF(
self->references);
289 Py_TYPE(
self)->tp_free(
self);
294 ".. class:: GPUBatch(type, buf, elem=None)\n"
296 " Reusable container for drawable geometry.\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"
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");
307 PyVarObject_HEAD_INIT(
NULL, 0).tp_name =
"GPUBatch",
310 #ifdef USE_GPU_PY_REFERENCES
311 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
312 .tp_doc = pygpu_batch__tp_doc,
316 .tp_flags = Py_TPFLAGS_DEFAULT,
332 #ifdef USE_GPU_PY_REFERENCES
341 return (PyObject *)
self;
346 #undef BPY_GPU_BATCH_CHECK_OBJ
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
void GPU_batch_discard(GPUBatch *)
#define GPU_batch_create(prim, verts, elem)
#define GPU_BATCH_VBO_MAX_LEN
#define GPU_batch_vertbuf_add(batch, verts)
void GPU_batch_draw(GPUBatch *batch)
_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
void GPU_shader_unbind(void)
struct GPUShader GPUShader
void GPU_shader_bind(GPUShader *shader)
uint GPU_vertbuf_get_vertex_len(const GPUVertBuf *verts)
Read Guarded memory(de)allocation.
struct PyC_StringEnumItems bpygpu_primtype_items[]
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
static PyObject * pygpu_batch__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
static int pygpu_batch__tp_clear(BPyGPUBatch *self)
static PyObject * pygpu_batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
static int pygpu_batch__tp_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
static void pygpu_batch__tp_dealloc(BPyGPUBatch *self)
static bool pygpu_batch_is_program_or_error(BPyGPUBatch *self)
PyObject * BPyGPUBatch_CreatePyObject(GPUBatch *batch)
static PyObject * pygpu_batch_draw(BPyGPUBatch *self, PyObject *args)
static PyObject * pygpu_batch_program_use_begin(BPyGPUBatch *self)
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[]
static PyObject * pygpu_batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
static PyObject * pygpu_batch_program_use_end(BPyGPUBatch *self)
PyTypeObject BPyGPUBatch_Type
struct BPyGPUBatch BPyGPUBatch
PyTypeObject BPyGPUIndexBuf_Type
PyTypeObject BPyGPUShader_Type
#define BPyGPUShader_Check(v)
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)
PyObject_VAR_HEAD struct GPUIndexBuf * elem
PyObject_VAR_HEAD struct GPUShader * shader
PyObject_VAR_HEAD struct GPUVertBuf * buf