89 PyErr_SetString(PyExc_ReferenceError,
91 "GPU texture was freed, no further access is valid"
93 "GPU texture: internal error"
102#define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu) \
104 if (UNLIKELY(pygpu_texture_valid_check(bpygpu) == -1)) { \
121 int size[3] = {1, 1, 1};
123 int is_cubemap =
false;
126 char err_out[256] =
"unknown error. See console";
128 static const char *_keywords[] = {
"size",
"layers",
"is_cubemap",
"format",
"data",
nullptr};
129 static _PyArg_Parser _parser = {
137 ":GPUTexture.__new__",
141 if (!_PyArg_ParseTupleAndKeywordsFast(args,
148 &pygpu_textureformat,
156 if (PySequence_Check(py_size)) {
157 len = PySequence_Size(py_size);
158 if ((
len < 1) || (
len > 3)) {
159 PyErr_Format(PyExc_ValueError,
160 "GPUTexture.__new__: \"size\" must be between 1 and 3 in length (got %d)",
168 else if (PyLong_Check(py_size)) {
169 size[0] = PyLong_AsLong(py_size);
172 PyErr_SetString(PyExc_ValueError,
"GPUTexture.__new__: Expected an int or tuple as first arg");
176 void *
data =
nullptr;
179 PyErr_SetString(PyExc_ValueError,
180 "GPUTexture.__new__: Only Buffer of format `FLOAT` is currently supported");
186 int component_size_expected =
sizeof(float);
187 size_t data_space_expected = size_t(
size[0]) *
size[1] *
size[2] *
max_ii(1, layers) *
188 component_len * component_size_expected;
190 data_space_expected *= 6 *
size[0];
194 PyErr_SetString(PyExc_ValueError,
"GPUTexture.__new__: Buffer size smaller than requested");
200 GPUTexture *tex =
nullptr;
201 if (is_cubemap &&
len != 1) {
203 "In cubemaps the same dimension represents height, width and depth. No tuple needed");
206 STRNCPY(err_out,
"Values less than 1 are not allowed in dimensions");
208 else if (layers &&
len == 3) {
209 STRNCPY(err_out,
"3D textures have no layers");
212 STRNCPY(err_out,
"No active GPU context found");
215 const char *name =
"python_texture";
225 static_cast<const float *
>(
data));
233 static_cast<const float *
>(
data));
245 static_cast<const float *
>(
data));
254 static_cast<const float *
>(
data));
274 static_cast<const float *
>(
data));
282 static_cast<const float *
>(
data));
286 if (tex ==
nullptr) {
287 PyErr_Format(PyExc_RuntimeError,
"gpu.texture.new(...) failed with '%s'", err_out);
296 pygpu_texture_width_doc,
297 "Width of the texture.\n"
308 pygpu_texture_height_doc,
309 "Height of the texture.\n"
320 pygpu_texture_format_doc,
321 "Format of the texture.\n"
333 pygpu_texture_clear_doc,
334 ".. method:: clear(format='FLOAT', value=(0.0, 0.0, 0.0, 1.0))\n"
336 " Fill texture with specific value.\n"
338 " :arg format: The format that describes the content of a single item.\n"
339 " Possible values are `FLOAT`, `INT`, `UINT`, `UBYTE`, `UINT_24_8` and `10_11_11_REV`.\n"
340 " :type format: str\n"
341 " :arg value: Sequence each representing the value to fill. Sizes 1..4 are supported.\n"
342 " :type value: Sequence[float]\n");
355 static const char *_keywords[] = {
"format",
"value",
nullptr};
356 static _PyArg_Parser _parser = {
365 if (!_PyArg_ParseTupleAndKeywordsFast(
371 int shape = PySequence_Size(py_values);
377 PyErr_SetString(PyExc_AttributeError,
"too many dimensions, max is 4");
383 PyErr_SetString(PyExc_AttributeError,
384 "`UINT_24_8` and `10_11_11_REV` only support single values");
388 memset(&values, 0,
sizeof(values));
402 values.c[0] = values.i[0];
403 values.c[1] = values.i[1];
404 values.c[2] = values.i[2];
405 values.c[3] = values.i[3];
414 pygpu_texture_read_doc,
415 ".. method:: read()\n"
417 " Creates a buffer with the value of all pixels.\n"
427 switch (tex_format) {
467 int shape_len = (shape[2] == 1) ? 2 : 3;
471#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
474 pygpu_texture_free_doc,
475 ".. method:: free()\n"
477 " Free the texture object.\n"
478 " The texture object will no longer be accessible.\n");
492#ifndef GPU_NO_USE_PY_REFERENCES
497 Py_TYPE(
self)->tp_free((PyObject *)
self);
505 pygpu_texture_height_doc,
510 pygpu_texture_format_doc,
512 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
517# pragma clang diagnostic push
518# pragma clang diagnostic ignored "-Wcast-function-type"
520# pragma GCC diagnostic push
521# pragma GCC diagnostic ignored "-Wcast-function-type"
528 METH_VARARGS | METH_KEYWORDS,
529 pygpu_texture_clear_doc},
531#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
532 {
"free", (PyCFunction)pygpu_texture_free, METH_NOARGS, pygpu_texture_free_doc},
534 {
nullptr,
nullptr, 0,
nullptr},
539# pragma clang diagnostic pop
541# pragma GCC diagnostic pop
547 pygpu_texture__tp_doc,
548 ".. class:: GPUTexture(size, layers=0, is_cubemap=False, format='RGBA8', data=None)\n"
550 " This object gives access to off GPU textures.\n"
552 " :arg size: Dimensions of the texture 1D, 2D, 3D or cubemap.\n"
553 " :type size: int | Sequence[int]\n"
554 " :arg layers: Number of layers in texture array or number of cubemaps in cubemap array\n"
555 " :type layers: int\n"
556 " :arg is_cubemap: Indicates the creation of a cubemap texture.\n"
557 " :type is_cubemap: int\n"
558 " :arg format: Internal data format inside GPU memory. Possible values are:\n"
589 " `R11F_G11F_B10F`,\n"
590 " `DEPTH32F_STENCIL8`,\n"
591 " `DEPTH24_STENCIL8`,\n"
594 " `SRGB8_A8_DXT1`,\n"
595 " `SRGB8_A8_DXT3`,\n"
596 " `SRGB8_A8_DXT5`,\n"
600 " `DEPTH_COMPONENT32F`,\n"
601 " `DEPTH_COMPONENT24`,\n"
602 " `DEPTH_COMPONENT16`,\n"
603 " :type format: str\n"
604 " :arg data: Buffer object to fill the texture.\n"
605 " :type data: :class:`gpu.types.Buffer`\n");
607 PyVarObject_HEAD_INIT(
nullptr, 0)
627 pygpu_texture__tp_doc,
666 pygpu_texture_from_image_doc,
667 ".. function:: from_image(image)\n"
669 " Get GPUTexture corresponding to an Image datablock. The GPUTexture memory is "
670 "shared with Blender.\n"
671 " Note: Colors read from the texture will be in scene linear color space and have "
672 "premultiplied or straight alpha matching the image alpha mode.\n"
674 " :arg image: The Image datablock.\n"
675 " :type image: :class:`bpy.types.Image`\n"
676 " :return: The GPUTexture used by the image.\n"
677 " :rtype: :class:`gpu.types.GPUTexture`\n");
681 if (ima ==
nullptr) {
694 {
nullptr,
nullptr, 0,
nullptr},
699 pygpu_texture__m_doc,
700 "This module provides utils for textures.");
704 pygpu_texture__m_doc,
722 *(GPUTexture **)p =
nullptr;
728 PyExc_ValueError,
"expected a texture or None object, got %s", Py_TYPE(o)->tp_name);
758 if (shared_reference) {
759#ifndef GPU_NO_USE_PY_REFERENCES
766 return (PyObject *)
self;
776#ifndef GPU_NO_USE_PY_REFERENCES
781 return (PyObject *)
self;
786#undef BPYGPU_TEXTURE_CHECK_OBJ
void BKE_imageuser_default(ImageUser *iuser)
GPUTexture * BKE_image_get_gpu_texture(Image *image, ImageUser *iuser)
MINLINE int max_ii(int a, int b)
char * STRNCPY(char(&dst)[N], const char *src)
#define POINTER_OFFSET(v, ofs)
GPUContext * GPU_context_active_get()
int GPU_texture_height(const GPUTexture *texture)
GPUTexture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
GPUTexture * GPU_texture_create_1d(const char *name, int width, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(GPUTexture *texture)
int GPU_texture_width(const GPUTexture *texture)
void GPU_texture_clear(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_ref(GPUTexture *texture)
void ** GPU_texture_py_reference_get(GPUTexture *texture)
void * GPU_texture_read(GPUTexture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_texture_py_reference_set(GPUTexture *texture, void **py_ref)
GPUTexture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
@ GPU_TEXTURE_USAGE_GENERAL
GPUTexture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
GPUTexture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const void *data)
GPUTexture * GPU_texture_create_cube(const char *name, int width, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
GPUTexture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
size_t GPU_texture_component_len(eGPUTextureFormat format)
eGPUTextureFormat GPU_texture_format(const GPUTexture *texture)
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
PyC_StringEnumItems bpygpu_dataformat_items[]
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
PyTypeObject BPyGPU_BufferType
BPyGPUBuffer * BPyGPU_Buffer_CreatePyObject(const int format, const Py_ssize_t *shape, const int shape_len, void *buffer)
size_t bpygpu_Buffer_size(BPyGPUBuffer *buffer)
#define BPYGPU_USE_GPUOBJ_FREE_METHOD
PyTypeObject BPyGPUTexture_Type
static PyObject * pygpu_texture_width_get(BPyGPUTexture *self, void *)
PyObject * BPyGPUTexture_CreatePyObject(GPUTexture *tex, bool shared_reference)
static PyObject * pygpu_texture_clear(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
PyObject * bpygpu_texture_init()
PyDoc_STRVAR(pygpu_texture_width_doc, "Width of the texture.\n" "\n" ":type: int")
static void BPyGPUTexture__tp_dealloc(BPyGPUTexture *self)
static int pygpu_texture_valid_check(BPyGPUTexture *bpygpu_tex)
static PyGetSetDef pygpu_texture__tp_getseters[]
#define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu)
static PyObject * pygpu_texture_from_image(PyObject *, PyObject *arg)
static PyObject * pygpu_texture__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
static PyObject * pygpu_texture_height_get(BPyGPUTexture *self, void *)
int bpygpu_ParseTexture(PyObject *o, void *p)
static PyObject * pygpu_texture_read(BPyGPUTexture *self)
const PyC_StringEnumItems pygpu_textureformat_items[]
static PyMethodDef pygpu_texture__m_methods[]
static PyMethodDef pygpu_texture__tp_methods[]
static PyModuleDef pygpu_texture_module_def
static PyObject * pygpu_texture_format_get(BPyGPUTexture *self, void *)
#define BPyGPUTexture_Check(v)
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
int PyC_ParseStringEnum(PyObject *o, void *p)
int PyC_AsArray(void *array, const size_t array_item_size, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
const char * PyC_StringEnum_FindIDFromValue(const PyC_StringEnumItems *items, const int value)
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
union BPyGPUBuffer::@156235326231167136112107117153043033043051263025 buf
PyObject_HEAD GPUTexture * tex