20 #include "../generic/py_capi_utils.h"
35 if (!
self->batch->shader) {
36 PyErr_SetString(PyExc_RuntimeError,
"batch does not have any program assigned to it");
52 const char *exc_str_missing_arg =
"GPUBatch.__new__() missing required argument '%s' (pos %d)";
58 static const char *_keywords[] = {
"type",
"buf",
"elem",
NULL};
59 static _PyArg_Parser _parser = {
68 if (!_PyArg_ParseTupleAndKeywordsFast(args,
82 PyErr_WarnEx(PyExc_DeprecationWarning,
83 "'LINE_LOOP' is deprecated. Please use 'LINE_STRIP' and close the segment.",
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.",
94 if (py_vertbuf ==
NULL) {
95 PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
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);
109 if (py_indexbuf !=
NULL) {
110 PyList_SET_ITEM(
ret->references, 1, (PyObject *)py_indexbuf);
111 Py_INCREF(py_indexbuf);
114 PyObject_GC_Track(
ret);
117 return (PyObject *)
ret;
121 ".. method:: vertbuf_add(buf)\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"
130 " :param buf: The vertex buffer that will be added to the batch.\n"
131 " :type buf: :class:`gpu.types.GPUVertBuf`\n"
136 PyErr_Format(PyExc_TypeError,
"Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
142 PyErr_Format(PyExc_TypeError,
143 "Expected %d length, got %d",
156 #ifdef USE_GPU_PY_REFERENCES
158 PyList_Append(
self->references, (PyObject *)py_buf);
166 pygpu_batch_program_set_doc,
167 ".. method:: program_set(program)\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"
174 " :param program: The program/shader the batch will use in future draw calls.\n"
175 " :type program: :class:`gpu.types.GPUShader`\n");
179 PyErr_Format(PyExc_TypeError,
"Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
186 #ifdef USE_GPU_PY_REFERENCES
188 int i = PyList_GET_SIZE(
self->references);
190 PyObject *py_shader_test = PyList_GET_ITEM(
self->references, i);
192 PyList_SET_ITEM(
self->references, i, (PyObject *)py_shader);
193 Py_INCREF(py_shader);
194 Py_DECREF(py_shader_test);
200 PyList_Append(
self->references, (PyObject *)py_shader);
208 ".. method:: draw(program=None)\n"
210 " Run the drawing program with the parameters assigned to the batch.\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");
219 if (!PyArg_ParseTuple(args,
"|O!:GPUBatch.draw", &
BPyGPUShader_Type, &py_program)) {
222 if (py_program ==
NULL) {
227 else if (
self->batch->shader != py_program->
shader) {
256 {
"draw", (PyCFunction)
pygpu_batch_draw, METH_VARARGS, pygpu_batch_draw_doc},
262 #ifdef USE_GPU_PY_REFERENCES
266 Py_VISIT(
self->references);
272 Py_CLEAR(
self->references);
282 #ifdef USE_GPU_PY_REFERENCES
283 PyObject_GC_UnTrack(
self);
284 if (
self->references) {
286 Py_XDECREF(
self->references);
290 Py_TYPE(
self)->tp_free(
self);
295 ".. class:: GPUBatch(type, buf, elem=None)\n"
297 " Reusable container for drawable geometry.\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"
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");
308 PyVarObject_HEAD_INIT(
NULL, 0).tp_name =
"GPUBatch",
311 #ifdef USE_GPU_PY_REFERENCES
312 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
313 .tp_doc = pygpu_batch__tp_doc,
317 .tp_flags = Py_TPFLAGS_DEFAULT,
333 #ifdef USE_GPU_PY_REFERENCES
342 return (PyObject *)
self;
347 #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)
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)
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