2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2015 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-09-05 21:10:42 -03:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bpygpu
|
2018-09-05 21:10:42 -03:00
|
|
|
*
|
2021-07-20 22:52:31 +10:00
|
|
|
* This file defines the off-screen functionalities of the 'gpu' module
|
2018-09-05 21:10:42 -03:00
|
|
|
* used for off-screen OpenGL rendering.
|
|
|
|
|
*
|
2021-07-20 22:52:31 +10:00
|
|
|
* - Use `bpygpu_` for local API.
|
|
|
|
|
* - Use `BPyGPU` for public API.
|
2018-09-05 21:10:42 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "GPU_batch.hh"
|
2018-09-05 21:10:42 -03:00
|
|
|
|
|
|
|
|
#include "../generic/py_capi_utils.h"
|
2023-08-30 14:05:32 +10:00
|
|
|
#include "../generic/python_compat.h"
|
2018-09-05 21:10:42 -03:00
|
|
|
|
2024-03-23 10:06:45 -04:00
|
|
|
#include "gpu_py.hh"
|
|
|
|
|
#include "gpu_py_element.hh"
|
|
|
|
|
#include "gpu_py_shader.hh"
|
|
|
|
|
#include "gpu_py_vertex_buffer.hh"
|
2018-09-05 21:10:42 -03:00
|
|
|
|
2024-03-23 10:06:45 -04:00
|
|
|
#include "gpu_py_batch.hh" /* own include */
|
2021-02-22 08:26:45 -03:00
|
|
|
|
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) {
|
2018-10-31 12:02:22 +11:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "batch does not have any program assigned to it");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2018-10-31 12:34:10 +11:00
|
|
|
/** \name GPUBatch Type
|
2018-09-05 21:10:42 -03:00
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static PyObject *pygpu_batch__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
|
2018-09-05 21:10:42 -03:00
|
|
|
{
|
2018-09-27 00:53:45 -03:00
|
|
|
const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-22 11:33:36 +10:00
|
|
|
PyC_StringEnum prim_type = {bpygpu_primtype_items, GPU_PRIM_NONE};
|
2023-07-21 19:41:03 +02:00
|
|
|
BPyGPUVertBuf *py_vertbuf = nullptr;
|
|
|
|
|
BPyGPUIndexBuf *py_indexbuf = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static const char *_keywords[] = {"type", "buf", "elem", nullptr};
|
2022-04-08 09:41:28 +10:00
|
|
|
static _PyArg_Parser _parser = {
|
2023-08-30 14:05:32 +10:00
|
|
|
PY_ARG_PARSER_HEAD_COMPAT()
|
2022-04-08 09:41:28 +10:00
|
|
|
"|$" /* Optional keyword only arguments. */
|
|
|
|
|
"O&" /* `type` */
|
|
|
|
|
"O!" /* `buf` */
|
|
|
|
|
"O!" /* `elem` */
|
|
|
|
|
":GPUBatch.__new__",
|
|
|
|
|
_keywords,
|
2023-08-03 19:14:53 +10:00
|
|
|
nullptr,
|
2022-04-08 09:41:28 +10:00
|
|
|
};
|
2019-01-03 01:08:26 +11:00
|
|
|
if (!_PyArg_ParseTupleAndKeywordsFast(args,
|
2018-09-27 00:22:57 -03:00
|
|
|
kwds,
|
|
|
|
|
&_parser,
|
2021-02-22 08:26:45 -03:00
|
|
|
PyC_ParseStringEnum,
|
|
|
|
|
&prim_type,
|
2018-09-27 00:53:45 -03:00
|
|
|
&BPyGPUVertBuf_Type,
|
2021-02-22 08:26:45 -03:00
|
|
|
&py_vertbuf,
|
2018-09-27 00:53:45 -03:00
|
|
|
&BPyGPUIndexBuf_Type,
|
2021-02-22 08:26:45 -03:00
|
|
|
&py_indexbuf))
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-22 08:26:45 -03:00
|
|
|
BLI_assert(prim_type.value_found != GPU_PRIM_NONE);
|
2022-04-21 12:40:30 -03:00
|
|
|
if (prim_type.value_found == GPU_PRIM_LINE_LOOP) {
|
|
|
|
|
PyErr_WarnEx(PyExc_DeprecationWarning,
|
|
|
|
|
"'LINE_LOOP' is deprecated. Please use 'LINE_STRIP' and close the segment.",
|
|
|
|
|
1);
|
|
|
|
|
}
|
|
|
|
|
else if (prim_type.value_found == GPU_PRIM_TRI_FAN) {
|
|
|
|
|
PyErr_WarnEx(
|
|
|
|
|
PyExc_DeprecationWarning,
|
|
|
|
|
"'TRI_FAN' is deprecated. Please use 'TRI_STRIP' or 'TRIS' and try modifying your "
|
|
|
|
|
"vertices or indices to match the topology.",
|
|
|
|
|
1);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
if (py_vertbuf == nullptr) {
|
2018-09-27 00:53:45 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, exc_str_missing_arg, _keywords[1], 2);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-03-26 03:06:25 +01:00
|
|
|
blender::gpu::Batch *batch = GPU_batch_create(GPUPrimType(prim_type.value_found),
|
|
|
|
|
py_vertbuf->buf,
|
|
|
|
|
py_indexbuf ? py_indexbuf->elem : nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-05 21:10:42 -03:00
|
|
|
BPyGPUBatch *ret = (BPyGPUBatch *)BPyGPUBatch_CreatePyObject(batch);
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
if (py_indexbuf != nullptr) {
|
2021-02-22 08:26:45 -03:00
|
|
|
PyList_SET_ITEM(ret->references, 1, (PyObject *)py_indexbuf);
|
|
|
|
|
Py_INCREF(py_indexbuf);
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 16:09:12 +10:00
|
|
|
BLI_assert(!PyObject_GC_IsTracked((PyObject *)ret));
|
2018-09-05 21:10:42 -03:00
|
|
|
PyObject_GC_Track(ret);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return (PyObject *)ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
pygpu_batch_vertbuf_add_doc, ".. method:: vertbuf_add(buf)\n"
|
2018-11-13 12:59:50 +01:00
|
|
|
"\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"
|
2022-09-19 14:22:31 +10:00
|
|
|
" :arg buf: The vertex buffer that will be added to the batch.\n"
|
2018-11-14 09:04:24 +11:00
|
|
|
" :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
|
|
|
{
|
|
|
|
|
if (!BPyGPUVertBuf_Check(py_buf)) {
|
|
|
|
|
PyErr_Format(PyExc_TypeError, "Expected a GPUVertBuf, got %s", Py_TYPE(py_buf)->tp_name);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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))
|
|
|
|
|
{
|
2018-09-05 21:10:42 -03: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));
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
if (self->batch->verts[GPU_BATCH_VBO_MAX_LEN - 1] != nullptr) {
|
2018-10-31 12:11:38 +11:00
|
|
|
PyErr_SetString(
|
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
|
"Maximum number of vertex buffers exceeded: " STRINGIFY(GPU_BATCH_VBO_MAX_LEN));
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-10-31 12:11:38 +11:00
|
|
|
}
|
|
|
|
|
|
2018-09-05 21:10:42 -03:00
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
|
|
|
|
/* Hold user */
|
|
|
|
|
PyList_Append(self->references, (PyObject *)py_buf);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-09 21:37:23 +01:00
|
|
|
GPU_batch_vertbuf_add(self->batch, py_buf->buf, false);
|
2018-09-05 21:10:42 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 12:34:10 +11:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2021-02-17 10:16:41 -03:00
|
|
|
pygpu_batch_program_set_doc,
|
2018-11-13 12:59:50 +01:00
|
|
|
".. method:: program_set(program)\n"
|
|
|
|
|
"\n"
|
2018-11-14 09:04:24 +11:00
|
|
|
" 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"
|
2018-11-13 12:59:50 +01:00
|
|
|
"\n"
|
2022-09-19 14:22:31 +10:00
|
|
|
" :arg program: The program/shader the batch will use in future draw calls.\n"
|
2018-11-14 09:04:24 +11:00
|
|
|
" :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
|
|
|
{
|
2024-03-04 08:29:41 +01:00
|
|
|
|
|
|
|
|
static bool deprecation_warning_issued = false;
|
|
|
|
|
|
|
|
|
|
/* Deprecation warning raised when calling `gpu.types.GPUBatch.program_set`. */
|
|
|
|
|
if (!deprecation_warning_issued) {
|
|
|
|
|
PyErr_WarnEx(PyExc_DeprecationWarning,
|
|
|
|
|
"Calls to GPUBatch.program_set are deprecated."
|
|
|
|
|
"Please set the shader via the 'program' parameter when calling "
|
|
|
|
|
"GPUBatch.draw/draw_instanced/draw_range.",
|
|
|
|
|
1);
|
|
|
|
|
deprecation_warning_issued = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
if (!BPyGPUShader_Check(py_shader)) {
|
|
|
|
|
PyErr_Format(PyExc_TypeError, "Expected a GPUShader, got %s", Py_TYPE(py_shader)->tp_name);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-14 09:32:19 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
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
|
2018-10-31 12:30:56 +11: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
|
|
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
pygpu_batch_draw_doc,
|
|
|
|
|
".. method:: draw(program=None)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Run the drawing program with the parameters assigned to the batch.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg program: Program that performs the drawing operations.\n"
|
|
|
|
|
" If ``None`` is passed, the last program set to this batch will run.\n"
|
|
|
|
|
" :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
|
|
|
{
|
2024-03-04 08:29:41 +01:00
|
|
|
static bool deprecation_warning_issued = false;
|
|
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
BPyGPUShader *py_program = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O!:GPUBatch.draw", &BPyGPUShader_Type, &py_program)) {
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2023-07-21 19:41:03 +02:00
|
|
|
if (py_program == nullptr) {
|
2024-03-04 08:29:41 +01:00
|
|
|
|
|
|
|
|
if (!deprecation_warning_issued) {
|
|
|
|
|
/* Deprecation warning raised when calling gpu.types.GPUBatch.draw without a valid GPUShader.
|
|
|
|
|
*/
|
|
|
|
|
PyErr_WarnEx(PyExc_DeprecationWarning,
|
|
|
|
|
"Calling GPUBatch.draw without specifying a program is deprecated. "
|
|
|
|
|
"Please provide a valid GPUShader as the 'program' parameter.",
|
|
|
|
|
1);
|
|
|
|
|
deprecation_warning_issued = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 10:16:41 -03:00
|
|
|
if (!pygpu_batch_is_program_or_error(self)) {
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-10-05 11:55:17 -03:00
|
|
|
}
|
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);
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-05 21:10:42 -03:00
|
|
|
GPU_batch_draw(self->batch);
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 10:23:38 +01:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2023-03-01 10:23:38 +01:00
|
|
|
pygpu_batch_draw_instanced_doc,
|
|
|
|
|
".. method:: draw_instanced(program, *, instance_start=0, instance_count=0)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Draw multiple instances of the drawing program with the parameters assigned\n"
|
|
|
|
|
" to the batch. In the vertex shader, `gl_InstanceID` will contain the instance\n"
|
|
|
|
|
" number being drawn.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg program: Program that performs the drawing operations.\n"
|
|
|
|
|
" :type program: :class:`gpu.types.GPUShader`\n"
|
|
|
|
|
" :arg instance_start: Number of the first instance to draw.\n"
|
|
|
|
|
" :type instance_start: int\n"
|
|
|
|
|
" :arg instance_count: Number of instances to draw. When not provided or set to 0\n"
|
|
|
|
|
" the number of instances will be determined by the number of rows in the first\n"
|
|
|
|
|
" vertex buffer.\n"
|
|
|
|
|
" :type instance_count: int\n");
|
|
|
|
|
static PyObject *pygpu_batch_draw_instanced(BPyGPUBatch *self, PyObject *args, PyObject *kw)
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
BPyGPUShader *py_program = nullptr;
|
2023-03-01 10:23:38 +01:00
|
|
|
int instance_start = 0;
|
|
|
|
|
int instance_count = 0;
|
|
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static const char *_keywords[] = {"program", "instance_start", "instance_count", nullptr};
|
2023-03-01 10:23:38 +01:00
|
|
|
static _PyArg_Parser _parser = {
|
2023-08-30 14:05:32 +10:00
|
|
|
PY_ARG_PARSER_HEAD_COMPAT()
|
2023-03-01 10:23:38 +01:00
|
|
|
"O!" /* `program` */
|
|
|
|
|
"|$" /* Optional keyword only arguments. */
|
|
|
|
|
"i" /* `instance_start` */
|
|
|
|
|
"i" /* `instance_count' */
|
|
|
|
|
":GPUBatch.draw_instanced",
|
|
|
|
|
_keywords,
|
2023-08-03 19:14:53 +10:00
|
|
|
nullptr,
|
2023-03-01 10:23:38 +01:00
|
|
|
};
|
|
|
|
|
if (!_PyArg_ParseTupleAndKeywordsFast(
|
|
|
|
|
args, kw, &_parser, &BPyGPUShader_Type, &py_program, &instance_start, &instance_count))
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2023-03-01 10:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPU_batch_set_shader(self->batch, py_program->shader);
|
|
|
|
|
GPU_batch_draw_instance_range(self->batch, instance_start, instance_count);
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
pygpu_batch_draw_range_doc,
|
|
|
|
|
".. method:: draw_range(program, *, elem_start=0, elem_count=0)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Run the drawing program with the parameters assigned to the batch. Only draw\n"
|
|
|
|
|
" the `elem_count` elements of the index buffer starting at `elem_start` \n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg program: Program that performs the drawing operations.\n"
|
|
|
|
|
" :type program: :class:`gpu.types.GPUShader`\n"
|
|
|
|
|
" :arg elem_start: First index to draw. When not provided or set to 0 drawing\n"
|
|
|
|
|
" will start from the first element of the index buffer.\n"
|
|
|
|
|
" :type elem_start: int\n"
|
|
|
|
|
" :arg elem_count: Number of elements of the index buffer to draw. When not\n"
|
|
|
|
|
" provided or set to 0 all elements from `elem_start` to the end of the\n"
|
|
|
|
|
" index buffer will be drawn.\n"
|
|
|
|
|
" :type elem_count: int\n");
|
2023-03-01 10:23:38 +01:00
|
|
|
static PyObject *pygpu_batch_draw_range(BPyGPUBatch *self, PyObject *args, PyObject *kw)
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
BPyGPUShader *py_program = nullptr;
|
2023-03-01 10:23:38 +01:00
|
|
|
int elem_start = 0;
|
|
|
|
|
int elem_count = 0;
|
|
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static const char *_keywords[] = {"program", "elem_start", "elem_count", nullptr};
|
2023-03-01 10:23:38 +01:00
|
|
|
static _PyArg_Parser _parser = {
|
2023-08-30 14:05:32 +10:00
|
|
|
PY_ARG_PARSER_HEAD_COMPAT()
|
2023-03-01 10:23:38 +01:00
|
|
|
"O!" /* `program` */
|
|
|
|
|
"|$" /* Optional keyword only arguments. */
|
|
|
|
|
"i" /* `elem_start' */
|
|
|
|
|
"i" /* `elem_count' */
|
|
|
|
|
":GPUBatch.draw_range",
|
|
|
|
|
_keywords,
|
2023-08-03 19:14:53 +10:00
|
|
|
nullptr,
|
2023-03-01 10:23:38 +01:00
|
|
|
};
|
|
|
|
|
if (!_PyArg_ParseTupleAndKeywordsFast(
|
|
|
|
|
args, kw, &_parser, &BPyGPUShader_Type, &py_program, &elem_start, &elem_count))
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2023-03-01 10:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPU_batch_set_shader(self->batch, py_program->shader);
|
|
|
|
|
GPU_batch_draw_range(self->batch, elem_start, elem_count);
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
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)) {
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2020-08-09 00:52:45 +02:00
|
|
|
GPU_shader_bind(self->batch->shader);
|
2018-09-05 21:10:42 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
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)) {
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
}
|
2020-08-09 00:52:45 +02:00
|
|
|
GPU_shader_unbind();
|
2018-09-05 21:10:42 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 11:11:42 +10:00
|
|
|
#if (defined(__GNUC__) && !defined(__clang__))
|
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
static PyMethodDef pygpu_batch__tp_methods[] = {
|
2021-02-17 10:16:41 -03:00
|
|
|
{"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},
|
2023-03-01 10:23:38 +01:00
|
|
|
{"draw_instanced",
|
|
|
|
|
(PyCFunction)pygpu_batch_draw_instanced,
|
|
|
|
|
METH_VARARGS | METH_KEYWORDS,
|
|
|
|
|
pygpu_batch_draw_instanced_doc},
|
|
|
|
|
{"draw_range",
|
|
|
|
|
(PyCFunction)pygpu_batch_draw_range,
|
|
|
|
|
METH_VARARGS | METH_KEYWORDS,
|
|
|
|
|
pygpu_batch_draw_range_doc},
|
2021-02-17 10:16:41 -03:00
|
|
|
{"_program_use_begin", (PyCFunction)pygpu_batch_program_use_begin, METH_NOARGS, ""},
|
|
|
|
|
{"_program_use_end", (PyCFunction)pygpu_batch_program_use_end, METH_NOARGS, ""},
|
2023-07-21 19:41:03 +02:00
|
|
|
{nullptr, nullptr, 0, nullptr},
|
2018-09-05 21:10:42 -03:00
|
|
|
};
|
|
|
|
|
|
2023-07-22 11:11:42 +10:00
|
|
|
#if (defined(__GNUC__) && !defined(__clang__))
|
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
|
#endif
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
Py_VISIT(self->references);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 10:16:41 -03:00
|
|
|
static int pygpu_batch__tp_clear(BPyGPUBatch *self)
|
2018-09-05 21:10:42 -03:00
|
|
|
{
|
|
|
|
|
Py_CLEAR(self->references);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 16:09:12 +10:00
|
|
|
static int pygpu_batch__tp_is_gc(BPyGPUBatch *self)
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
return self->references != nullptr;
|
2022-09-28 16:09:12 +10:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
GPU_batch_discard(self->batch);
|
|
|
|
|
|
|
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
2021-03-04 15:06:15 +11:00
|
|
|
PyObject_GC_UnTrack(self);
|
2018-09-05 21:10:42 -03:00
|
|
|
if (self->references) {
|
2021-02-17 10:16:41 -03:00
|
|
|
pygpu_batch__tp_clear(self);
|
2018-09-05 21:10:42 -03:00
|
|
|
Py_XDECREF(self->references);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2021-02-17 10:16:41 -03:00
|
|
|
pygpu_batch__tp_doc,
|
2018-11-13 14:55:15 +01:00
|
|
|
".. class:: GPUBatch(type, buf, elem=None)\n"
|
2018-09-05 21:10:42 -03:00
|
|
|
"\n"
|
2018-11-14 09:04:24 +11:00
|
|
|
" Reusable container for drawable geometry.\n"
|
2018-09-05 21:10:42 -03:00
|
|
|
"\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"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :arg buf: Vertex buffer containing all or some of the attributes required for drawing.\n"
|
2018-11-13 12:59:50 +01:00
|
|
|
" :type buf: :class:`gpu.types.GPUVertBuf`\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :arg elem: An optional index buffer.\n"
|
2018-11-13 12:59:50 +01:00
|
|
|
" :type elem: :class:`gpu.types.GPUIndexBuf`\n");
|
2018-09-05 21:10:42 -03:00
|
|
|
PyTypeObject BPyGPUBatch_Type = {
|
2023-07-21 19:41:03 +02:00
|
|
|
/*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_name*/ "GPUBatch",
|
|
|
|
|
/*tp_basicsize*/ sizeof(BPyGPUBatch),
|
|
|
|
|
/*tp_itemsize*/ 0,
|
|
|
|
|
/*tp_dealloc*/ (destructor)pygpu_batch__tp_dealloc,
|
|
|
|
|
/*tp_vectorcall_offset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_getattr*/ nullptr,
|
|
|
|
|
/*tp_setattr*/ nullptr,
|
|
|
|
|
/*tp_as_async*/ nullptr,
|
|
|
|
|
/*tp_repr*/ nullptr,
|
|
|
|
|
/*tp_as_number*/ nullptr,
|
|
|
|
|
/*tp_as_sequence*/ nullptr,
|
|
|
|
|
/*tp_as_mapping*/ nullptr,
|
|
|
|
|
/*tp_hash*/ nullptr,
|
|
|
|
|
/*tp_call*/ nullptr,
|
|
|
|
|
/*tp_str*/ nullptr,
|
|
|
|
|
/*tp_getattro*/ nullptr,
|
|
|
|
|
/*tp_setattro*/ nullptr,
|
|
|
|
|
/*tp_as_buffer*/ nullptr,
|
2018-09-05 21:10:42 -03:00
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
2018-09-05 21:10:42 -03:00
|
|
|
#else
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_flags*/ Py_TPFLAGS_DEFAULT,
|
2018-09-05 21:10:42 -03:00
|
|
|
#endif
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_doc*/ pygpu_batch__tp_doc,
|
|
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
|
|
|
|
/*tp_traverse*/ (traverseproc)pygpu_batch__tp_traverse,
|
|
|
|
|
#else
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_traverse*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
#endif
|
|
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
|
|
|
|
/*tp_clear*/ (inquiry)pygpu_batch__tp_clear,
|
|
|
|
|
#else
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_clear*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
#endif
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_richcompare*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_weaklistoffset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_iter*/ nullptr,
|
|
|
|
|
/*tp_iternext*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_methods*/ pygpu_batch__tp_methods,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_members*/ nullptr,
|
|
|
|
|
/*tp_getset*/ nullptr,
|
|
|
|
|
/*tp_base*/ nullptr,
|
|
|
|
|
/*tp_dict*/ nullptr,
|
|
|
|
|
/*tp_descr_get*/ nullptr,
|
|
|
|
|
/*tp_descr_set*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_dictoffset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_init*/ nullptr,
|
|
|
|
|
/*tp_alloc*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_new*/ pygpu_batch__tp_new,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_free*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
|
|
|
|
/*tp_is_gc*/ (inquiry)pygpu_batch__tp_is_gc,
|
|
|
|
|
#else
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_is_gc*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
#endif
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_bases*/ nullptr,
|
|
|
|
|
/*tp_mro*/ nullptr,
|
|
|
|
|
/*tp_cache*/ nullptr,
|
|
|
|
|
/*tp_subclasses*/ nullptr,
|
|
|
|
|
/*tp_weaklist*/ nullptr,
|
|
|
|
|
/*tp_del*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_version_tag*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_finalize*/ nullptr,
|
|
|
|
|
/*tp_vectorcall*/ nullptr,
|
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
|
|
|
|
2024-03-26 03:06:25 +01:00
|
|
|
PyObject *BPyGPUBatch_CreatePyObject(blender::gpu::Batch *batch)
|
2018-09-05 21:10:42 -03:00
|
|
|
{
|
|
|
|
|
BPyGPUBatch *self;
|
|
|
|
|
|
|
|
|
|
#ifdef USE_GPU_PY_REFERENCES
|
|
|
|
|
self = (BPyGPUBatch *)_PyObject_GC_New(&BPyGPUBatch_Type);
|
2023-07-21 19:41:03 +02:00
|
|
|
self->references = nullptr;
|
2018-09-05 21:10:42 -03:00
|
|
|
#else
|
|
|
|
|
self = PyObject_New(BPyGPUBatch, &BPyGPUBatch_Type);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
self->batch = batch;
|
|
|
|
|
|
|
|
|
|
return (PyObject *)self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
#undef BPY_GPU_BATCH_CHECK_OBJ
|