diff options
author | Germano Cavalcantemano-wii <germano.costa@ig.com.br> | 2020-12-11 07:18:24 +0300 |
---|---|---|
committer | Germano Cavalcantemano-wii <germano.costa@ig.com.br> | 2021-01-11 01:01:53 +0300 |
commit | 9db3d1951da15254efbbcf028176facb78118ec1 (patch) | |
tree | 2cdc94f9143d3df00f2541263cb25369dff2679f | |
parent | 1d3b92bdeabc4a556372603c548155fad1e87be0 (diff) |
Fix typo; Documentation; Expose layer for framebuffer attachament; Add framebuffer viewport setter; Remove framebuffer restore; Expose framebuffer push/pop stack API; Remove blend modes; Remove depth_range_set; Implement GPU_face_culling, GPU_front_facing, GPU_point_size, GPU_line_width, GPU_viewport, GPU_color_mask and GPU_depth_mask
-rw-r--r-- | source/blender/gpu/GPU_framebuffer.h | 4 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_framebuffer.cc | 26 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 10 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 1 | ||||
-rw-r--r-- | source/blender/python/gpu/CMakeLists.txt | 6 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_api.c | 7 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_framebuffer.c | 480 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_framebuffer.h | 34 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_state.c | 325 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_state.h | 23 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_texture.c | 401 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_texture.h | 33 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_types.c | 8 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_types.h | 2 |
14 files changed, 1353 insertions, 7 deletions
diff --git a/source/blender/gpu/GPU_framebuffer.h b/source/blender/gpu/GPU_framebuffer.h index c0f91756bf6..726dcbf0174 100644 --- a/source/blender/gpu/GPU_framebuffer.h +++ b/source/blender/gpu/GPU_framebuffer.h @@ -209,6 +209,10 @@ void GPU_framebuffer_recursive_downsample(GPUFrameBuffer *fb, void (*callback)(void *userData, int level), void *userData); +void GPU_framebuffer_push(GPUFrameBuffer *fb); +GPUFrameBuffer *GPU_framebuffer_pop(void); +uint GPU_framebuffer_stack_level_get(void); + /* GPU OffScreen * - wrapper around frame-buffer and texture for simple off-screen drawing */ diff --git a/source/blender/gpu/intern/gpu_framebuffer.cc b/source/blender/gpu/intern/gpu_framebuffer.cc index d5d7994a154..910bdc531fe 100644 --- a/source/blender/gpu/intern/gpu_framebuffer.cc +++ b/source/blender/gpu/intern/gpu_framebuffer.cc @@ -476,10 +476,8 @@ void GPU_framebuffer_recursive_downsample(GPUFrameBuffer *gpu_fb, /** \} */ /* -------------------------------------------------------------------- */ -/** \name GPUOffScreen +/** \name Framebuffer Push/Pop * - * Container that holds a frame-buffer and its textures. - * Might be bound to multiple contexts. * \{ */ #define FRAMEBUFFER_STACK_DEPTH 16 @@ -489,22 +487,36 @@ static struct { uint top; } FrameBufferStack = {{nullptr}}; -static void gpuPushFrameBuffer(GPUFrameBuffer *fb) +void GPU_framebuffer_push(GPUFrameBuffer *fb) { BLI_assert(FrameBufferStack.top < FRAMEBUFFER_STACK_DEPTH); FrameBufferStack.framebuffers[FrameBufferStack.top] = fb; FrameBufferStack.top++; } -static GPUFrameBuffer *gpuPopFrameBuffer() +GPUFrameBuffer *GPU_framebuffer_pop(void) { BLI_assert(FrameBufferStack.top > 0); FrameBufferStack.top--; return FrameBufferStack.framebuffers[FrameBufferStack.top]; } +uint GPU_framebuffer_stack_level_get(void) +{ + return FrameBufferStack.top; +} + #undef FRAMEBUFFER_STACK_DEPTH +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name GPUOffScreen + * + * Container that holds a frame-buffer and its textures. + * Might be bound to multiple contexts. + * \{ */ + #define MAX_CTX_FB_LEN 3 struct GPUOffScreen { @@ -614,7 +626,7 @@ void GPU_offscreen_bind(GPUOffScreen *ofs, bool save) { if (save) { GPUFrameBuffer *fb = GPU_framebuffer_active_get(); - gpuPushFrameBuffer(fb); + GPU_framebuffer_push(fb); } unwrap(gpu_offscreen_fb_get(ofs))->bind(false); } @@ -623,7 +635,7 @@ void GPU_offscreen_unbind(GPUOffScreen *UNUSED(ofs), bool restore) { GPUFrameBuffer *fb = nullptr; if (restore) { - fb = gpuPopFrameBuffer(); + fb = GPU_framebuffer_pop(); } if (fb) { diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index d944cb435d0..24793a08ba9 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -282,6 +282,16 @@ int PyC_ParseStringEnum(PyObject *o, void *p) return 0; } +const char *PyC_StringEnum_find_id(struct PyC_StringEnum *e, const int value) +{ + for (int i = 0; e->items[i].id; i++) { + if (e->items[i].value == value) { + return e->items[i].id; + } + } + return NULL; +} + /* silly function, we dont use arg. just check its compatible with __deepcopy__ */ int PyC_CheckArgs_DeepCopy(PyObject *args) { diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index f0875b82c3c..6f41f9229c8 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -142,6 +142,7 @@ struct PyC_StringEnum { }; int PyC_ParseStringEnum(PyObject *o, void *p); +const char *PyC_StringEnum_find_id(struct PyC_StringEnum *e, const int value); int PyC_CheckArgs_DeepCopy(PyObject *args); diff --git a/source/blender/python/gpu/CMakeLists.txt b/source/blender/python/gpu/CMakeLists.txt index 7f6fd9eefab..25670a9be69 100644 --- a/source/blender/python/gpu/CMakeLists.txt +++ b/source/blender/python/gpu/CMakeLists.txt @@ -36,10 +36,13 @@ set(SRC gpu_py_api.c gpu_py_batch.c gpu_py_element.c + gpu_py_framebuffer.c gpu_py_matrix.c gpu_py_offscreen.c gpu_py_select.c gpu_py_shader.c + gpu_py_state.c + gpu_py_texture.c gpu_py_types.c gpu_py_vertex_buffer.c gpu_py_vertex_format.c @@ -47,10 +50,13 @@ set(SRC gpu_py_api.h gpu_py_batch.h gpu_py_element.h + gpu_py_framebuffer.h gpu_py_matrix.h gpu_py_offscreen.h gpu_py_select.h gpu_py_shader.h + gpu_py_state.h + gpu_py_texture.h gpu_py_types.h gpu_py_vertex_buffer.h gpu_py_vertex_format.h diff --git a/source/blender/python/gpu/gpu_py_api.c b/source/blender/python/gpu/gpu_py_api.c index 33130291162..70bf04c0992 100644 --- a/source/blender/python/gpu/gpu_py_api.c +++ b/source/blender/python/gpu/gpu_py_api.c @@ -35,6 +35,7 @@ #include "gpu_py_matrix.h" #include "gpu_py_select.h" +#include "gpu_py_state.h" #include "gpu_py_types.h" #include "gpu_py_api.h" /* own include */ @@ -134,6 +135,12 @@ PyObject *BPyInit_gpu(void) PyModule_AddObject(mod, "shader", (submodule = BPyInit_gpu_shader())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); + PyModule_AddObject(mod, "framebuffer", (submodule = BPyInit_gpu_framebuffer())); + PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); + + PyModule_AddObject(mod, "state", (submodule = BPyInit_gpu_state())); + PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); + return mod; } diff --git a/source/blender/python/gpu/gpu_py_framebuffer.c b/source/blender/python/gpu/gpu_py_framebuffer.c new file mode 100644 index 00000000000..248e592530f --- /dev/null +++ b/source/blender/python/gpu/gpu_py_framebuffer.c @@ -0,0 +1,480 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + * + * This file defines the framebuffer 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 "GPU_context.h" +#include "GPU_framebuffer.h" +#include "GPU_init_exit.h" + +#include "../generic/py_capi_utils.h" +#include "../generic/python_utildefines.h" +#include "../mathutils/mathutils.h" + +#include "gpu_py_api.h" +#include "gpu_py_texture.h" + +#include "gpu_py_framebuffer.h" /* own include */ + +/* -------------------------------------------------------------------- */ +/** \name GPUFrameBuffer Common Utilities + * \{ */ + +static int py_framebuffer_valid_check(BPyGPUFrameBuffer *bpygpu_fb) +{ + if (UNLIKELY(bpygpu_fb->fb == NULL)) { + PyErr_SetString(PyExc_ReferenceError, "GPU framebuffer was freed, no further access is valid"); + return -1; + } + return 0; +} + +#define PY_FRAMEBUFFER_CHECK_OBJ(bpygpu) \ + { \ + if (UNLIKELY(py_framebuffer_valid_check(bpygpu) == -1)) { \ + return NULL; \ + } \ + } \ + ((void)0) + +static void py_framebuffer_free_if_possible(GPUFrameBuffer *fb) +{ + if (!fb) { + return; + } + + if (GPU_is_init()) { + GPU_framebuffer_free(fb); + } + else { + printf("PyFramebuffer freed after the context has been destroyed.\n"); + } +} + +/* Keep less than or equal to #FRAMEBUFFER_STACK_DEPTH */ +#define GPU_PY_FRAMEBUFFER_STACK_LEN 16 + +static bool py_framebuffer_stack_push_or_error(GPUFrameBuffer *fb) +{ + if (GPU_framebuffer_stack_level_get() >= GPU_PY_FRAMEBUFFER_STACK_LEN) { + PyErr_SetString( + PyExc_RuntimeError, + "Maximum framebuffer stack depth " STRINGIFY(GPU_PY_FRAMEBUFFER_STACK_LEN) " reached"); + return false; + } + GPU_framebuffer_push(fb); + GPU_framebuffer_bind(fb); + return true; +} + +static bool py_framebuffer_stack_pop_or_error(void) +{ + if (GPU_framebuffer_stack_level_get() == 0) { + PyErr_SetString(PyExc_RuntimeError, "Minimum framebuffer stack depth reached"); + return false; + } + + GPUFrameBuffer *fb = GPU_framebuffer_pop(); + GPU_framebuffer_bind(fb); + return true; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name GPUFramebuffer Type + * \{ */ + +static PyObject *py_framebuffer_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds) +{ + BPYGPU_IS_INIT_OR_ERROR_OBJ; + if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) { + PyErr_SetString(PyExc_ValueError, "This function takes no arguments"); + return NULL; + } + + if (!GPU_context_active_get()) { + PyErr_SetString(PyExc_RuntimeError, "No active GPU context found"); + return NULL; + } + + GPUFrameBuffer *fb = GPU_framebuffer_create("python_fb"); + return BPyGPUFrameBuffer_CreatePyObject(fb); +} + +static PyObject *py_framebuffer_viewport_get(BPyGPUFrameBuffer *self, void *UNUSED(type)) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + int viewport[4]; + GPU_framebuffer_viewport_get(self->fb, viewport); + + PyObject *ret = PyTuple_New(4); + PyTuple_SET_ITEMS(ret, + PyLong_FromLong(viewport[0]), + PyLong_FromLong(viewport[1]), + PyLong_FromLong(viewport[2]), + PyLong_FromLong(viewport[3])); + return ret; +} + +static int py_framebuffer_viewport_set(BPyGPUFrameBuffer *self, + PyObject *py_values, + void *UNUSED(type)) +{ + int viewport[4]; + + if (!PySequence_Check(py_values)) { + return -1; + } + + if (PySequence_Size(py_values) != 4) { + PyErr_SetString(PyExc_AttributeError, "An array of length 4 is required"); + return -1; + } + + for (int i = 0; i < 4; i++) { + PyObject *ob = PySequence_GetItem(py_values, i); + viewport[i] = PyLong_AsLong(ob); + Py_DECREF(ob); + if (PyErr_Occurred()) { + return -1; + } + } + + GPU_framebuffer_viewport_set(self->fb, UNPACK4(viewport)); + return 0; +} + +PyDoc_STRVAR(py_framebuffer_is_bound_doc, + ".. method:: is_bound()\n" + "\n" + " Checks if this is the active framebuffer in the context.\n" + "\n"); +static PyObject *py_framebuffer_is_bound(BPyGPUFrameBuffer *self, void *UNUSED(type)) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + return PyBool_FromLong(GPU_framebuffer_bound(self->fb)); +} + +PyDoc_STRVAR(py_framebuffer_bind_doc, + ".. method:: bind()\n" + "\n" + " Bind the framebuffer object.\n" + " To make sure that the framebuffer gets restored whether an exception occurs or " + "not, pack it into a `with` statement.\n" + "\n"); +static PyObject *py_framebuffer_bind(BPyGPUFrameBuffer *self) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + + GPU_framebuffer_bind(self->fb); + Py_INCREF(self); + + return (PyObject *)self; +} + +PyDoc_STRVAR(py_framebuffer_free_doc, + ".. method:: free()\n" + "\n" + " Free the framebuffer object.\n" + " The framebuffer will no longer be accessible.\n"); +static PyObject *py_framebuffer_free(BPyGPUFrameBuffer *self) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + py_framebuffer_free_if_possible(self->fb); + self->fb = NULL; + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_framebuffer_texture_attach_doc, + ".. method:: texture_attach(texture, slot=0, layer=-1, mip=0)\n" + "\n" + " Attach a texture to slot.\n" + "\n" + " :arg texture: Texture to attach.\n" + " :type texture: :class:`gpu.types.GPUTexture`\n" + " :arg slot: Framebuffer color slot to attach the texture.\n" + " For depth or stencil textures this value is not used.\n" + " :type slot: `int`\n" + " :arg layer: When specified, attach a single layer of a 3D or array texture.\n" + " For cube map textures, layer is translated into a cube map face.\n" + " :type layer: `int`\n" + " :arg mip: Mipmap level of the texture image to be attached.\n" + " :type mip: `int`\n"); +static PyObject *py_framebuffer_texture_attach(BPyGPUFrameBuffer *self, + PyObject *args, + PyObject *kwds) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + BPyGPUTexture *py_texture; + int slot = 0; + int layer = -1; + int mip = 0; + + static const char *_keywords[] = {"texture", "slot", "layer", "mip", NULL}; + static _PyArg_Parser _parser = {"O!|ii:texture_attach", _keywords, 0}; + if (!_PyArg_ParseTupleAndKeywordsFast( + args, kwds, &_parser, &BPyGPUTexture_Type, &py_texture, &slot, &layer, &mip)) { + return NULL; + } + + if (py_texture == NULL) { + PyErr_SetString(PyExc_TypeError, + "GPUFrameBuffer.texture_attach() missing required argument texture (pos 1)"); + return NULL; + } + + if (slot && GPU_texture_depth(py_texture->tex) || GPU_texture_stencil(py_texture->tex)) { + PyErr_SetString( + PyExc_TypeError, + "GPUFrameBuffer.texture_attach() this slot is not intended for depth or stencil textures"); + return NULL; + } + + GPU_framebuffer_texture_layer_attach(self->fb, py_texture->tex, slot, layer, mip); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_framebuffer_texture_detach_doc, + ".. method:: texture_detach(texture)\n" + "\n" + " Dettach texture.\n" + "\n" + " :arg texture: Texture to detach.\n"); +static PyObject *py_framebuffer_texture_detach(BPyGPUFrameBuffer *self, PyObject *py_texture) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + if (!BPyGPUTexture_Check(py_texture)) { + return NULL; + } + + GPU_framebuffer_texture_detach(self->fb, ((BPyGPUTexture *)py_texture)->tex); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_framebuffer_clear_doc, + ".. method:: clear(color=(0.0, 0.0, 0.0, 1.0), depth=None, stencil=None,)\n" + "\n" + " Fill color, depth and stencil textures with specific value.\n" + "\n" + " :arg color: float sequence each representing ``(r, g, b, a)``.\n" + " :type color: sequence of 3 or 4 floats\n" + " :arg depth: depth value.\n" + " :type depth: `float`\n" + " :arg stencil: stencil value.\n" + " :type stencil: `int`\n"); +static PyObject *py_framebuffer_clear(BPyGPUFrameBuffer *self, PyObject *args, PyObject *kwds) +{ + PY_FRAMEBUFFER_CHECK_OBJ(self); + + if (!GPU_framebuffer_bound(self->fb)) { + return NULL; + } + + PyObject *py_col = NULL; + PyObject *py_depth = NULL; + PyObject *py_stencil = NULL; + + static const char *_keywords[] = {"color", "depth", "stencil", NULL}; + static _PyArg_Parser _parser = {"|OOO:texture_attach", _keywords, 0}; + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &py_col, &py_depth, &py_stencil)) { + return NULL; + } + + eGPUFrameBufferBits buffers = 0; + float col[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + float depth = 1.0f; + uint stencil = 0; + + if (py_col && py_col != Py_None) { + if (mathutils_array_parse(col, 3, 4, py_col, "GPUFrameBuffer.clear(), invalid 'color' arg") == + -1) { + return NULL; + } + buffers |= GPU_COLOR_BIT; + } + + if (py_depth && py_depth != Py_None) { + depth = PyFloat_AsDouble(py_depth); + if (PyErr_Occurred()) { + return NULL; + } + buffers |= GPU_DEPTH_BIT; + } + + if (py_stencil && py_stencil != Py_None) { + if ((stencil = PyC_Long_AsU32(py_stencil)) == (uint)-1) { + return NULL; + } + buffers |= GPU_STENCIL_BIT; + } + + GPU_framebuffer_clear(self->fb, buffers, col, depth, stencil); + Py_RETURN_NONE; +} + +static PyObject *py_framebuffer_bind_context_enter(BPyGPUFrameBuffer *self) +{ + if (!py_framebuffer_stack_push_or_error(self->fb)) { + return NULL; + } + Py_RETURN_NONE; +} + +static PyObject *py_framebuffer_bind_context_exit(BPyGPUFrameBuffer *UNUSED(self), + PyObject *UNUSED(args)) +{ + if (!py_framebuffer_stack_pop_or_error()) { + return NULL; + } + Py_RETURN_NONE; +} + +static void BPyGPUFrameBuffer__tp_dealloc(BPyGPUFrameBuffer *self) +{ + py_framebuffer_free_if_possible(self->fb); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyGetSetDef py_framebuffer_getseters[] = { + {"viewport", + (getter)py_framebuffer_viewport_get, + (setter)py_framebuffer_viewport_set, + NULL, + NULL}, + {"is_bound", (getter)py_framebuffer_is_bound, (setter)NULL, py_framebuffer_is_bound_doc, NULL}, + {NULL, NULL, NULL, NULL, NULL} /* Sentinel */ +}; + +static struct PyMethodDef py_framebuffer_methods[] = { + {"bind", (PyCFunction)py_framebuffer_bind, METH_NOARGS, py_framebuffer_bind_doc}, + {"free", (PyCFunction)py_framebuffer_free, METH_NOARGS, py_framebuffer_free_doc}, + {"texture_attach", + (PyCFunction)py_framebuffer_texture_attach, + METH_VARARGS | METH_KEYWORDS, + py_framebuffer_texture_attach_doc}, + {"texture_detach", + (PyCFunction)py_framebuffer_texture_detach, + METH_O, + py_framebuffer_texture_detach_doc}, + {"clear", + (PyCFunction)py_framebuffer_clear, + METH_VARARGS | METH_KEYWORDS, + py_framebuffer_clear_doc}, + {"__enter__", (PyCFunction)py_framebuffer_bind_context_enter, METH_NOARGS}, + {"__exit__", (PyCFunction)py_framebuffer_bind_context_exit, METH_VARARGS}, + {NULL, NULL, 0, NULL}, +}; + +PyDoc_STRVAR(py_framebuffer_doc, + ".. class:: GPUFrameBuffer()\n" + "\n" + " This object gives access to framebuffer functionallities.\n"); +PyTypeObject BPyGPUFrameBuffer_Type = { + PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUFrameBuffer", + .tp_basicsize = sizeof(BPyGPUFrameBuffer), + .tp_dealloc = (destructor)BPyGPUFrameBuffer__tp_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_doc = py_framebuffer_doc, + .tp_methods = py_framebuffer_methods, + .tp_getset = py_framebuffer_getseters, + .tp_new = py_framebuffer_new, +}; + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name gpu.framebuffer Module API + * \{ */ + +PyDoc_STRVAR(py_framebuffer_push_doc, + ".. function:: push()\n" + "\n" + " Bind and add the framebuffer to the stack so that the previous one can be " + "restored with the pop method.\n"); +static PyObject *py_framebuffer_push(BPyGPUFrameBuffer *self) +{ + if (!py_framebuffer_stack_push_or_error(self->fb)) { + return NULL; + } + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_framebuffer_pop_doc, + ".. function:: pop()\n" + "\n" + " Remove the last framebuffer from the stack.\n"); +static PyObject *py_framebuffer_pop(PyObject *UNUSED(self)) +{ + if (!py_framebuffer_stack_pop_or_error()) { + return NULL; + } + Py_RETURN_NONE; +} + +static struct PyMethodDef py_framebuffer_module_methods[] = { + {"push", (PyCFunction)py_framebuffer_push, METH_NOARGS, py_framebuffer_push_doc}, + {"pop", (PyCFunction)py_framebuffer_pop, METH_NOARGS, py_framebuffer_pop_doc}, + {NULL, NULL, 0, NULL}, +}; + +PyDoc_STRVAR(bpygpu_framebuffeer_module_doc, + "This module provides access to GPUFrameBuffer internal functions."); +static PyModuleDef BPyGPU_framebuffer_module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "gpu.framebuffer", + .m_doc = bpygpu_framebuffeer_module_doc, + .m_methods = py_framebuffer_module_methods, +}; + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Public API + * \{ */ + +PyObject *BPyGPUFrameBuffer_CreatePyObject(GPUFrameBuffer *fb) +{ + BPyGPUFrameBuffer *self; + + self = PyObject_New(BPyGPUFrameBuffer, &BPyGPUFrameBuffer_Type); + self->fb = fb; + + return (PyObject *)self; +} + +PyObject *BPyInit_gpu_framebuffer(void) +{ + PyObject *submodule; + + submodule = PyModule_Create(&BPyGPU_framebuffer_module_def); + + return submodule; +} + +/** \} */ + +#undef PY_FRAMEBUFFER_CHECK_OBJ diff --git a/source/blender/python/gpu/gpu_py_framebuffer.h b/source/blender/python/gpu/gpu_py_framebuffer.h new file mode 100644 index 00000000000..5ddc1a91a67 --- /dev/null +++ b/source/blender/python/gpu/gpu_py_framebuffer.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + */ + +#pragma once + +#include "BLI_compiler_attrs.h" + +extern PyTypeObject BPyGPUFrameBuffer_Type; + +#define BPyGPUFrameBuffer_Check(v) (Py_TYPE(v) == &BPyGPUFrameBuffer_Type) + +typedef struct BPyGPUFrameBuffer { + PyObject_HEAD struct GPUFrameBuffer *fb; +} BPyGPUFrameBuffer; + +PyObject *BPyGPUFrameBuffer_CreatePyObject(struct GPUFrameBuffer *fb) ATTR_NONNULL(1); +PyObject *BPyInit_gpu_framebuffer(void); diff --git a/source/blender/python/gpu/gpu_py_state.c b/source/blender/python/gpu/gpu_py_state.c new file mode 100644 index 00000000000..b4d9971144b --- /dev/null +++ b/source/blender/python/gpu/gpu_py_state.c @@ -0,0 +1,325 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + * + * This file defines the gpu.state API. + * + * - Use ``bpygpu_`` for local API. + * - Use ``BPyGPU`` for public API. + */ + +#include <Python.h> + +#include "GPU_state.h" + +#include "../generic/py_capi_utils.h" + +#include "gpu_py_state.h" /* own include */ + +/* -------------------------------------------------------------------- */ +/** \name Helper Functions + * \{ */ + +static const struct PyC_StringEnumItems pygpu_blend_items[] = { + {GPU_BLEND_NONE, "NONE"}, + {GPU_BLEND_ALPHA, "ALPHA"}, + {GPU_BLEND_ALPHA_PREMULT, "ALPHA_PREMULT"}, + {GPU_BLEND_ADDITIVE, "ADDITIVE"}, + {GPU_BLEND_ADDITIVE_PREMULT, "ADDITIVE_PREMULT"}, + {GPU_BLEND_MULTIPLY, "MULTIPLY"}, + {GPU_BLEND_SUBTRACT, "SUBTRACT"}, + {GPU_BLEND_INVERT, "INVERT"}, + /** + * These are quite special cases used inside the draw manager. + * {GPU_BLEND_OIT, "OIT"}, + * {GPU_BLEND_BACKGROUND, "BACKGROUND"}, + * {GPU_BLEND_CUSTOM, "CUSTOM"}, + */ + {0, NULL}, +}; + +static const struct PyC_StringEnumItems pygpu_depthtest_items[] = { + {GPU_DEPTH_NONE, "NONE"}, + {GPU_DEPTH_ALWAYS, "ALWAYS"}, + {GPU_DEPTH_LESS, "LESS"}, + {GPU_DEPTH_LESS_EQUAL, "LESS_EQUAL"}, + {GPU_DEPTH_EQUAL, "EQUAL"}, + {GPU_DEPTH_GREATER, "GREATER"}, + {GPU_DEPTH_GREATER_EQUAL, "GREATER_EQUAL"}, + {0, NULL}, +}; + +static const struct PyC_StringEnumItems pygpu_faceculling_items[] = { + {GPU_CULL_NONE, "NONE"}, + {GPU_CULL_FRONT, "FRONT"}, + {GPU_CULL_BACK, "BACK"}, + {0, NULL}, +}; + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Manage Stack + * \{ */ + +PyDoc_STRVAR(py_state_blend_set_doc, + ".. function:: blend_set(mode)\n" + "\n" + " Defines the fixed pipeline blending equation.\n" + "\n" + " :param mode: One of these modes: {\n" + " `NONE`,\n" + " `ALPHA`,\n" + " `ALPHA_PREMULT`,\n" + " `ADDITIVE`,\n" + " `ADDITIVE_PREMULT`,\n" + " `MULTIPLY`,\n" + " `SUBTRACT`,\n" + " `INVERT`,\n" + //" `OIT`,\n" + //" `BACKGROUND`,\n" + //" `CUSTOM`,\n" + " :type mode: `str`\n"); +static PyObject *py_state_blend_set(PyObject *UNUSED(self), PyObject *value) +{ + const struct PyC_StringEnum pygpu_blend = {&pygpu_blend_items, GPU_BLEND_NONE}; + if (!PyC_ParseStringEnum(value, &pygpu_blend)) { + return NULL; + } + GPU_blend(pygpu_blend.value_found); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_blend_get_doc, + ".. function:: blend_get()\n" + "\n" + " Current blending equation.\n" + "\n"); +static PyObject *py_state_blend_get(PyObject *UNUSED(self)) +{ + eGPUBlend blend = GPU_blend_get(); + return PyUnicode_FromString(PyC_StringEnum_find_id(&pygpu_blend_items, blend)); +} + +PyDoc_STRVAR(py_state_depth_test_set_doc, + ".. function:: depth_test_set(mode)\n" + "\n" + " Defines the depth_test equation.\n" + "\n" + " :param mode: One of these modes: {\n" + " `NONE`,\n" + " `ALWAYS`,\n" + " `LESS`,\n" + " `LESS_EQUAL`,\n" + " `EQUAL`,\n" + " `GREATER`,\n" + " `GREATER_EQUAL`,\n" + " :type mode: `str`\n"); +static PyObject *py_state_depth_test_set(PyObject *UNUSED(self), PyObject *value) +{ + const struct PyC_StringEnum pygpu_depth_test = {&pygpu_depthtest_items, GPU_DEPTH_NONE}; + if (!PyC_ParseStringEnum(value, &pygpu_depth_test)) { + return NULL; + } + GPU_depth_test(pygpu_depth_test.value_found); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_depth_test_get_doc, + ".. function:: blend_depth_test_get()\n" + "\n" + " Current depth_test equation.\n" + "\n"); +static PyObject *py_state_depth_test_get(PyObject *UNUSED(self)) +{ + eGPUDepthTest test = GPU_depth_test_get(); + return PyUnicode_FromString(PyC_StringEnum_find_id(&pygpu_depthtest_items, test)); +} + +PyDoc_STRVAR(py_state_face_culling_doc, + ".. function:: face_culling(culling)\n" + "\n" + " Specify whether none, front-facing or back-facing facets can be culled.\n" + "\n" + " :param mode: One of these modes: {\n" + " `NONE`,\n" + " `FRONT`,\n" + " `BACK`,\n" + " :type mode: `str`\n"); +static PyObject *py_state_face_culling(PyObject *UNUSED(self), PyObject *value) +{ + const struct PyC_StringEnum pygpu_faceculling = {&pygpu_faceculling_items, GPU_CULL_NONE}; + if (!PyC_ParseStringEnum(value, &pygpu_faceculling)) { + return NULL; + } + + GPU_face_culling(pygpu_faceculling.value_found); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_front_facing_doc, + ".. function:: front_facing(invert)\n" + "\n" + " Specifies the orientation of front-facing polygons.\n" + "\n" + " :param invert: True for clockwise polygons as front-facing.\n" + " :type mode: `bool`\n"); +static PyObject *py_state_front_facing(PyObject *UNUSED(self), PyObject *value) +{ + bool invert; + if (!PyC_ParseBool(value, &invert)) { + return NULL; + } + + GPU_front_facing(invert); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_point_size_doc, + ".. function:: point_size(size)\n" + "\n" + " Specify the diameter of rasterized points.\n" + "\n" + " :param size: New diameter.\n" + " :type mode: `float`\n"); +static PyObject *py_state_point_size(PyObject *UNUSED(self), PyObject *value) +{ + float size = (float)PyFloat_AsDouble(value); + if (PyErr_Occurred()) { + return NULL; + } + + GPU_point_size(size); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_line_width_doc, + ".. function:: line_width(width)\n" + "\n" + " Specify the width of rasterized lines.\n" + "\n" + " :param size: New width.\n" + " :type mode: `float`\n"); +static PyObject *py_state_line_width(PyObject *UNUSED(self), PyObject *value) +{ + float width = (float)PyFloat_AsDouble(value); + if (PyErr_Occurred()) { + return NULL; + } + + GPU_line_width(width); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_viewport_doc, + ".. function:: viewport(x, y, width, height)\n" + "\n" + " Specifies the viewport of the active framebuffer.\n" + "\n" + " :param x, y: lower left corner of the viewport rectangle, in pixels.\n" + " :param width, height: width and height of the viewport.\n" + " :type x, y, width, height: `int`\n"); +static int py_state_viewport(PyObject *UNUSED(self), PyObject *args) +{ + int x, y, width, height; + if (!PyArg_ParseTuple(args, "iiii:viewport", &x, &y, &width, &height)) { + return NULL; + } + + GPU_viewport(x, y, width, height); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_color_mask_doc, + ".. function:: color_mask(r, g, b, a)\n" + "\n" + " Enable or disable writing of frame buffer color components.\n" + "\n" + " :param r, g, b, a: components red, green, blue, and alpha.\n" + " :type r, g, b, a: `bool`\n"); +static int py_state_color_mask(PyObject *UNUSED(self), PyObject *args) +{ + int r, g, b, a; + if (!PyArg_ParseTuple(args, "pppp:color_mask", &r, &g, &b, &a)) { + return NULL; + } + + GPU_color_mask((bool)r, (bool)g, (bool)b, (bool)a); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_state_depth_mask_doc, + ".. function:: depth_mask_set(value)\n" + "\n" + " Write to depth component.\n" + "\n" + " :param value: True for writing to the depth component.\n" + " :type near: `bool`\n"); +static PyObject *py_state_depth_mask(PyObject *UNUSED(self), PyObject *value) +{ + bool write_to_depth; + if (!PyC_ParseBool(value, &write_to_depth)) { + return NULL; + } + GPU_depth_mask(write_to_depth); + Py_RETURN_NONE; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Module + * \{ */ + +static struct PyMethodDef bpygpu_py_state_methods[] = { + /* Manage Stack */ + {"blend_set", (PyCFunction)py_state_blend_set, METH_O, py_state_blend_set_doc}, + {"blend_get", (PyCFunction)py_state_blend_get, METH_NOARGS, py_state_blend_get_doc}, + {"depth_test_set", (PyCFunction)py_state_depth_test_set, METH_O, py_state_depth_test_set_doc}, + {"depth_test_get", + (PyCFunction)py_state_depth_test_get, + METH_NOARGS, + py_state_depth_test_get_doc}, + {"face_culling", (PyCFunction)py_state_face_culling, METH_O, py_state_face_culling_doc}, + {"front_facing", (PyCFunction)py_state_front_facing, METH_O, py_state_front_facing_doc}, + {"point_size", (PyCFunction)py_state_point_size, METH_O, py_state_point_size_doc}, + {"line_width", (PyCFunction)py_state_line_width, METH_O, py_state_line_width_doc}, + {"viewport", (PyCFunction)py_state_viewport, METH_VARARGS, py_state_viewport_doc}, + {"color_mask", (PyCFunction)py_state_color_mask, METH_VARARGS, py_state_color_mask_doc}, + {"depth_mask", (PyCFunction)py_state_depth_mask, METH_O, py_state_depth_mask_doc}, + {NULL, NULL, 0, NULL}, +}; + +PyDoc_STRVAR(bpygpu_py_state_doc, "This module provides access to the gpu state."); +static PyModuleDef BPyGPU_py_state_module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "gpu.state", + .m_doc = bpygpu_py_state_doc, + .m_methods = bpygpu_py_state_methods, +}; + +PyObject *BPyInit_gpu_state(void) +{ + PyObject *submodule; + + submodule = PyModule_Create(&BPyGPU_py_state_module_def); + + return submodule; +} + +/** \} */ diff --git a/source/blender/python/gpu/gpu_py_state.h b/source/blender/python/gpu/gpu_py_state.h new file mode 100644 index 00000000000..9fa92c67f09 --- /dev/null +++ b/source/blender/python/gpu/gpu_py_state.h @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + */ + +#pragma once + +PyObject *BPyInit_gpu_state(void); diff --git a/source/blender/python/gpu/gpu_py_texture.c b/source/blender/python/gpu/gpu_py_texture.c new file mode 100644 index 00000000000..11050bb31ba --- /dev/null +++ b/source/blender/python/gpu/gpu_py_texture.c @@ -0,0 +1,401 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + * + * This file defines the texture functionalities of the 'gpu' module + * + * - Use ``bpygpu_`` for local API. + * - Use ``BPyGPU`` for public API. + */ + +#include <Python.h> + +#include "GPU_context.h" +#include "GPU_texture.h" + +#include "../generic/py_capi_utils.h" + +#include "gpu_py_api.h" + +#include "gpu_py_texture.h" /* own include */ + +/* -------------------------------------------------------------------- */ +/** \name GPUTexture Common Utilities + * \{ */ + +static const struct PyC_StringEnumItems pygpu_textureformat_items[] = { + {GPU_RGBA8UI, "RGBA8UI"}, + {GPU_RGBA8I, "RGBA8I"}, + {GPU_RGBA8, "RGBA8"}, + {GPU_RGBA32UI, "RGBA32UI"}, + {GPU_RGBA32I, "RGBA32I"}, + {GPU_RGBA32F, "RGBA32F"}, + {GPU_RGBA16UI, "RGBA16UI"}, + {GPU_RGBA16I, "RGBA16I"}, + {GPU_RGBA16F, "RGBA16F"}, + {GPU_RGBA16, "RGBA16"}, + {GPU_RG8UI, "RG8UI"}, + {GPU_RG8I, "RG8I"}, + {GPU_RG8, "RG8"}, + {GPU_RG32UI, "RG32UI"}, + {GPU_RG32I, "RG32I"}, + {GPU_RG32F, "RG32F"}, + {GPU_RG16UI, "RG16UI"}, + {GPU_RG16I, "RG16I"}, + {GPU_RG16F, "RG16F"}, + {GPU_RG16, "RG16"}, + {GPU_R8UI, "R8UI"}, + {GPU_R8I, "R8I"}, + {GPU_R8, "R8"}, + {GPU_R32UI, "R32UI"}, + {GPU_R32I, "R32I"}, + {GPU_R32F, "R32F"}, + {GPU_R16UI, "R16UI"}, + {GPU_R16I, "R16I"}, + {GPU_R16F, "R16F"}, + {GPU_R16, "R16"}, + {GPU_R11F_G11F_B10F, "R11F_G11F_B10F"}, + {GPU_DEPTH32F_STENCIL8, "DEPTH32F_STENCIL8"}, + {GPU_DEPTH24_STENCIL8, "DEPTH24_STENCIL8"}, + {GPU_SRGB8_A8, "SRGB8_A8"}, + {GPU_RGB16F, "RGB16F"}, + {GPU_SRGB8_A8_DXT1, "SRGB8_A8_DXT1"}, + {GPU_SRGB8_A8_DXT3, "SRGB8_A8_DXT3"}, + {GPU_SRGB8_A8_DXT5, "SRGB8_A8_DXT5"}, + {GPU_RGBA8_DXT1, "RGBA8_DXT1"}, + {GPU_RGBA8_DXT3, "RGBA8_DXT3"}, + {GPU_RGBA8_DXT5, "RGBA8_DXT5"}, + {GPU_DEPTH_COMPONENT32F, "DEPTH_COMPONENT32F"}, + {GPU_DEPTH_COMPONENT24, "DEPTH_COMPONENT24"}, + {GPU_DEPTH_COMPONENT16, "DEPTH_COMPONENT16"}, + {0, NULL}, +}; + +static const struct PyC_StringEnumItems pygpu_dataformat_items[] = { + {GPU_DATA_FLOAT, "FLOAT"}, + {GPU_DATA_INT, "INT"}, + {GPU_DATA_UNSIGNED_INT, "UNSIGNED_INT"}, + {GPU_DATA_UNSIGNED_BYTE, "UNSIGNED_BYTE"}, + {GPU_DATA_UNSIGNED_INT_24_8, "UNSIGNED_INT_24_8"}, + {GPU_DATA_10_11_11_REV, "10_11_11_REV"}, + {0, NULL}, +}; + +static int py_texture_valid_check(BPyGPUTexture *bpygpu_tex) +{ + if (UNLIKELY(bpygpu_tex->tex == NULL)) { + PyErr_SetString(PyExc_ReferenceError, "GPU texture was freed, no further access is valid"); + return -1; + } + return 0; +} + +#define PY_TEXTURE_CHECK_OBJ(bpygpu) \ + { \ + if (UNLIKELY(py_texture_valid_check(bpygpu) == -1)) { \ + return NULL; \ + } \ + } \ + ((void)0) + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name GPUTexture Type + * \{ */ + +static PyObject *py_texture_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds) +{ + BPYGPU_IS_INIT_OR_ERROR_OBJ; + + GPUTexture *tex = NULL; + int width, height, mips; + const struct PyC_StringEnum pygpu_textureformat = {&pygpu_textureformat_items, GPU_RGBA8}; + char err_out[256] = "unknown error. See console"; + + static const char *_keywords[] = {"width", "height", "mips", "format", NULL}; + static _PyArg_Parser _parser = {"iiiO&:GPUTexture.__new__", _keywords, 0}; + if (!_PyArg_ParseTupleAndKeywordsFast(args, + kwds, + &_parser, + &width, + &height, + &mips, + PyC_ParseStringEnum, + &pygpu_textureformat)) { + return NULL; + } + + if (GPU_context_active_get()) { + tex = GPU_texture_create_2d( + "python_texture", width, height, mips, pygpu_textureformat.value_found, NULL); + } + else { + strncpy(err_out, "No active GPU context found", 256); + } + + if (tex == NULL) { + PyErr_Format(PyExc_RuntimeError, "gpu.texture.new(...) failed with '%s'", err_out); + return NULL; + } + + return BPyGPUTexture_CreatePyObject(tex); +} + +PyDoc_STRVAR(py_texture_width_doc, "Width of the texture.\n\n:type: `int`"); +static PyObject *py_texture_width_get(BPyGPUTexture *self, void *UNUSED(type)) +{ + PY_TEXTURE_CHECK_OBJ(self); + return PyLong_FromLong(GPU_texture_width(self->tex)); +} + +PyDoc_STRVAR(py_texture_height_doc, "Height of the texture.\n\n:type: `int`"); +static PyObject *py_texture_height_get(BPyGPUTexture *self, void *UNUSED(type)) +{ + PY_TEXTURE_CHECK_OBJ(self); + return PyLong_FromLong(GPU_texture_height(self->tex)); +} + +PyDoc_STRVAR(py_texture_bind_doc, + ".. method:: bind(number)\n" + "\n" + " Bind the texture object.\n" + "\n" + " :arg slot: texture slot.\n" + " :type slot: `int`\n"); +static PyObject *py_texture_bind(BPyGPUTexture *self, PyObject *args, PyObject *kwds) +{ + PY_TEXTURE_CHECK_OBJ(self); + int slot; + + static const char *_keywords[] = {"slot", NULL}; + static _PyArg_Parser _parser = {"i:bind", _keywords, 0}; + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &slot)) { + return NULL; + } + + GPU_texture_bind(self->tex, slot); + + Py_INCREF(self); + + return (PyObject *)self; +} + +PyDoc_STRVAR(py_texture_unbind_doc, + ".. method:: unbind()\n" + "\n" + " Unbind the texture object.\n"); +static PyObject *py_texture_unbind(BPyGPUTexture *self) +{ + GPU_texture_unbind(self->tex); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_texture_free_doc, + ".. method:: free()\n" + "\n" + " Free the texture object.\n" + " The texture object will no longer be accessible.\n"); +static PyObject *py_texture_free(BPyGPUTexture *self) +{ + PY_TEXTURE_CHECK_OBJ(self); + + GPU_texture_free(self->tex); + self->tex = NULL; + Py_RETURN_NONE; +} + +PyDoc_STRVAR(py_texture_clear_doc, + ".. method:: clear(format='FLOAT', value=(0.0, 0.0, 0.0, 1.0))\n" + "\n" + " Fill texture with specific value.\n" + "\n" + " :param format: One of these primitive types: {\n" + " `FLOAT`,\n" + " `INT`,\n" + " `UNSIGNED_INT`,\n" + " `UNSIGNED_BYTE`,\n" + " `UNSIGNED_INT_24_8`,\n" + " `10_11_11_REV`,\n" + " :type type: `str`\n" + " :arg value: sequence each representing the value to fill.\n" + " :type value: sequence of 1, 2, 3 or 4 values\n"); +static PyObject *py_texture_clear(BPyGPUTexture *self, PyObject *args) +{ + PY_TEXTURE_CHECK_OBJ(self); + const struct PyC_StringEnum pygpu_dataformat = {&pygpu_dataformat_items, GPU_DATA_FLOAT}; + union { + int i[4]; + float f[4]; + } values; + + PyObject *py_values; + if (!PyArg_ParseTuple(args, "O&O:clear", PyC_ParseStringEnum, &pygpu_dataformat, &py_values)) { + return NULL; + } + + if (!PySequence_Check(py_values)) { + return NULL; + } + + int dimensions = PySequence_Size(py_values); + if (dimensions > 4) { + PyErr_SetString(PyExc_AttributeError, "too many dimensions, max is 4"); + return NULL; + } + + memset(&values, 0, sizeof(values)); + for (int i = 0; i < dimensions; i++) { + PyObject *ob = PySequence_GetItem(py_values, i); + + if (pygpu_dataformat.value_found == GPU_DATA_FLOAT) { + values.f[i] = (float)PyFloat_AsDouble(ob); + } + else { + values.i[i] = PyLong_AsLong(ob); + } + Py_DECREF(ob); + } + + GPU_texture_clear(self->tex, pygpu_dataformat.value_found, &values); + Py_RETURN_NONE; +} + +static PyObject *py_texture_bind_context_enter(BPyGPUTexture *UNUSED(self)) +{ + Py_RETURN_NONE; +} + +static PyObject *py_texture_bind_context_exit(BPyGPUTexture *self, PyObject *UNUSED(args)) +{ + GPU_texture_unbind(self->tex); + Py_RETURN_NONE; +} + +static void BPyGPUTexture__tp_dealloc(BPyGPUTexture *self) +{ + if (self->tex) { + GPU_texture_free(self->tex); + } + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyGetSetDef py_texture_getseters[] = { + {"width", (getter)py_texture_width_get, (setter)NULL, py_texture_width_doc, NULL}, + {"height", (getter)py_texture_height_get, (setter)NULL, py_texture_height_doc, NULL}, + {NULL, NULL, NULL, NULL, NULL} /* Sentinel */ +}; + +static struct PyMethodDef py_texture_methods[] = { + {"bind", (PyCFunction)py_texture_bind, METH_VARARGS | METH_KEYWORDS, py_texture_bind_doc}, + {"unbind", + (PyCFunction)py_texture_unbind, + METH_VARARGS | METH_KEYWORDS, + py_texture_unbind_doc}, + {"free", (PyCFunction)py_texture_free, METH_NOARGS, py_texture_free_doc}, + {"clear", (PyCFunction)py_texture_clear, METH_VARARGS, py_texture_clear_doc}, + {"__enter__", (PyCFunction)py_texture_bind_context_enter, METH_NOARGS}, + {"__exit__", (PyCFunction)py_texture_bind_context_exit, METH_VARARGS}, + {NULL, NULL, 0, NULL}, +}; + +PyDoc_STRVAR(py_texture_doc, + ".. class:: GPUTexture(width, height, data_type)\n" + "\n" + " This object gives access to off screen buffers.\n" + "\n" + " :arg width: Horizontal dimension of the buffer.\n" + " :type width: `int`\n" + " :arg height: Vertical dimension of the buffer.\n" + " :type height: `int`\n" + " :param type: One of these primitive types: {\n" + " `RGBA8UI`,\n" + " `RGBA8I`,\n" + " `RGBA8`,\n" + " `RGBA32UI`,\n" + " `RGBA32I`,\n" + " `RGBA32F`,\n" + " `RGBA16UI`,\n" + " `RGBA16I`,\n" + " `RGBA16F`,\n" + " `RGBA16`,\n" + " `RG8UI`,\n" + " `RG8I`,\n" + " `RG8`,\n" + " `RG32UI`,\n" + " `RG32I`,\n" + " `RG32F`,\n" + " `RG16UI`,\n" + " `RG16I`,\n" + " `RG16F`,\n" + " `RG16`,\n" + " `R8UI`,\n" + " `R8I`,\n" + " `R8`,\n" + " `R32UI`,\n" + " `R32I`,\n" + " `R32F`,\n" + " `R16UI`,\n" + " `R16I`,\n" + " `R16F`,\n" + " `R16`,\n" + " `R11F_G11F_B10F`,\n" + " `DEPTH32F_STENCIL8`,\n" + " `DEPTH24_STENCIL8`,\n" + " `SRGB8_A8`,\n" + " `RGB16F`,\n" + " `SRGB8_A8_DXT1`,\n" + " `SRGB8_A8_DXT3`,\n" + " `SRGB8_A8_DXT5`,\n" + " `RGBA8_DXT1`,\n" + " `RGBA8_DXT3`,\n" + " `RGBA8_DXT5`,\n" + " `DEPTH_COMPONENT32F`,\n" + " `DEPTH_COMPONENT24`,\n" + " `DEPTH_COMPONENT16`,\n" + " :type type: `str`\n"); +PyTypeObject BPyGPUTexture_Type = { + PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUTexture", + .tp_basicsize = sizeof(BPyGPUTexture), + .tp_dealloc = (destructor)BPyGPUTexture__tp_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_doc = py_texture_doc, + .tp_methods = py_texture_methods, + .tp_getset = py_texture_getseters, + .tp_new = py_texture_new, +}; + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Public API + * \{ */ + +PyObject *BPyGPUTexture_CreatePyObject(GPUTexture *tex) +{ + BPyGPUTexture *self; + + self = PyObject_New(BPyGPUTexture, &BPyGPUTexture_Type); + self->tex = tex; + + return (PyObject *)self; +} + +/** \} */ + +#undef PY_TEXTURE_CHECK_OBJ diff --git a/source/blender/python/gpu/gpu_py_texture.h b/source/blender/python/gpu/gpu_py_texture.h new file mode 100644 index 00000000000..e2c36d35cf3 --- /dev/null +++ b/source/blender/python/gpu/gpu_py_texture.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +/** \file + * \ingroup bpygpu + */ + +#pragma once + +#include "BLI_compiler_attrs.h" + +extern PyTypeObject BPyGPUTexture_Type; + +#define BPyGPUTexture_Check(v) (Py_TYPE(v) == &BPyGPUTexture_Type) + +typedef struct BPyGPUTexture { + PyObject_HEAD struct GPUTexture *tex; +} BPyGPUTexture; + +PyObject *BPyGPUTexture_CreatePyObject(struct GPUTexture *tex) ATTR_NONNULL(1); diff --git a/source/blender/python/gpu/gpu_py_types.c b/source/blender/python/gpu/gpu_py_types.c index 165af47b4b8..e247f019870 100644 --- a/source/blender/python/gpu/gpu_py_types.c +++ b/source/blender/python/gpu/gpu_py_types.c @@ -61,6 +61,12 @@ PyObject *BPyInit_gpu_types(void) if (PyType_Ready(&BPyGPUShader_Type) < 0) { return NULL; } + if (PyType_Ready(&BPyGPUTexture_Type) < 0) { + return NULL; + } + if (PyType_Ready(&BPyGPUFrameBuffer_Type) < 0) { + return NULL; + } #define MODULE_TYPE_ADD(s, t) \ PyModule_AddObject(s, t.tp_name, (PyObject *)&t); \ @@ -72,6 +78,8 @@ PyObject *BPyInit_gpu_types(void) MODULE_TYPE_ADD(submodule, BPyGPUBatch_Type); MODULE_TYPE_ADD(submodule, BPyGPUOffScreen_Type); MODULE_TYPE_ADD(submodule, BPyGPUShader_Type); + MODULE_TYPE_ADD(submodule, BPyGPUTexture_Type); + MODULE_TYPE_ADD(submodule, BPyGPUFrameBuffer_Type); #undef MODULE_TYPE_ADD diff --git a/source/blender/python/gpu/gpu_py_types.h b/source/blender/python/gpu/gpu_py_types.h index 56f73b8a504..f6f4c0a9378 100644 --- a/source/blender/python/gpu/gpu_py_types.h +++ b/source/blender/python/gpu/gpu_py_types.h @@ -22,8 +22,10 @@ #include "gpu_py_batch.h" #include "gpu_py_element.h" +#include "gpu_py_framebuffer.h" #include "gpu_py_offscreen.h" #include "gpu_py_shader.h" +#include "gpu_py_texture.h" #include "gpu_py_vertex_buffer.h" #include "gpu_py_vertex_format.h" |