From 6c6b1c015c570e728b007cd94723eb2c711bec4b Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Mon, 1 Mar 2021 17:35:10 -0300 Subject: GPU Python: Implement gpu.texture.from_image It can be useful to replace `image.bindcode` and `image.gl_load`. Used for example in https://docs.blender.org/api/current/gpu.html#d-image Reviewed By: brecht Differential Revision: https://developer.blender.org/D10458 --- source/blender/python/gpu/gpu_py_api.c | 3 ++ source/blender/python/gpu/gpu_py_texture.c | 59 ++++++++++++++++++++++++++++++ source/blender/python/gpu/gpu_py_texture.h | 2 + 3 files changed, 64 insertions(+) (limited to 'source/blender/python') diff --git a/source/blender/python/gpu/gpu_py_api.c b/source/blender/python/gpu/gpu_py_api.c index 68f7eb9816c..0bc18e73d0c 100644 --- a/source/blender/python/gpu/gpu_py_api.c +++ b/source/blender/python/gpu/gpu_py_api.c @@ -73,6 +73,9 @@ PyObject *BPyInit_gpu(void) PyModule_AddObject(mod, "state", (submodule = bpygpu_state_init())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); + PyModule_AddObject(mod, "texture", (submodule = bpygpu_texture_init())); + PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); + return mod; } diff --git a/source/blender/python/gpu/gpu_py_texture.c b/source/blender/python/gpu/gpu_py_texture.c index 12855b8dbcc..14f901624fe 100644 --- a/source/blender/python/gpu/gpu_py_texture.c +++ b/source/blender/python/gpu/gpu_py_texture.c @@ -27,9 +27,13 @@ #include "BLI_string.h" +#include "DNA_image_types.h" + #include "GPU_context.h" #include "GPU_texture.h" +#include "BKE_image.h" + #include "../generic/py_capi_utils.h" #include "gpu_py.h" @@ -509,6 +513,53 @@ PyTypeObject BPyGPUTexture_Type = { /** \} */ +/* -------------------------------------------------------------------- */ +/** \name GPU Texture module + * \{ */ +PyDoc_STRVAR(pygpu_texture_from_image_doc, + ".. function:: from_image(image)\n" + "\n" + " Get GPUTexture corresponding to an Image datablock. The GPUTexture memory is " + "shared with Blender.\n" + " Note: Colors read from the texture will be in scene linear color space and have " + "premultiplied or straight alpha matching the image alpha mode.\n" + "\n" + " :arg image: The Image datablock.\n" + " :type image: `bpy.types.Image`\n" + " :return: The GPUTexture used by the image.\n" + " :rtype: :class:`gpu.types.GPUTexture`\n"); +static PyObject *pygpu_texture_from_image(PyObject *UNUSED(self), PyObject *arg) +{ + Image *ima = PyC_RNA_AsPointer(arg, "Image"); + if (ima == NULL) { + return NULL; + } + + ImageUser iuser; + BKE_imageuser_default(&iuser); + GPUTexture *tex = BKE_image_get_gpu_texture(ima, &iuser, NULL); + + /* Increase the texture reference count. */ + GPU_texture_ref(tex); + + return BPyGPUTexture_CreatePyObject(tex); +} + +static struct PyMethodDef pygpu_texture__m_methods[] = { + {"from_image", (PyCFunction)pygpu_texture_from_image, METH_O, pygpu_texture_from_image_doc}, + {NULL, NULL, 0, NULL}, +}; + +PyDoc_STRVAR(pygpu_texure__m_doc, "This module provides utils for textures."); +static PyModuleDef pygpu_texture_module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "gpu.texture", + .m_doc = pygpu_texure__m_doc, + .m_methods = pygpu_texture__m_methods, +}; + +/** \} */ + /* -------------------------------------------------------------------- */ /** \name Local API * \{ */ @@ -534,6 +585,14 @@ int bpygpu_ParseTexture(PyObject *o, void *p) return 1; } +PyObject *bpygpu_texture_init(void) +{ + PyObject *submodule; + submodule = PyModule_Create(&pygpu_texture_module_def); + + return submodule; +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/python/gpu/gpu_py_texture.h b/source/blender/python/gpu/gpu_py_texture.h index be7348b2bd4..5130273f971 100644 --- a/source/blender/python/gpu/gpu_py_texture.h +++ b/source/blender/python/gpu/gpu_py_texture.h @@ -31,4 +31,6 @@ typedef struct BPyGPUTexture { } BPyGPUTexture; int bpygpu_ParseTexture(PyObject *o, void *p); +PyObject *bpygpu_texture_init(void); + PyObject *BPyGPUTexture_CreatePyObject(struct GPUTexture *tex) ATTR_NONNULL(1); -- cgit v1.2.3