23 #include "../generic/py_capi_utils.h"
85 PyErr_SetString(PyExc_ReferenceError,
87 "GPU texture was freed, no further access is valid"
89 "GPU texture: internal error"
98 #define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu) \
100 if (UNLIKELY(pygpu_texture_valid_check(bpygpu) == -1)) { \
117 int size[3] = {1, 1, 1};
119 int is_cubemap =
false;
122 char err_out[256] =
"unknown error. See console";
124 static const char *_keywords[] = {
"size",
"layers",
"is_cubemap",
"format",
"data",
NULL};
125 static _PyArg_Parser _parser = {
132 ":GPUTexture.__new__",
136 if (!_PyArg_ParseTupleAndKeywordsFast(args,
143 &pygpu_textureformat,
150 if (PySequence_Check(py_size)) {
151 len = PySequence_Size(py_size);
152 if ((
len < 1) || (
len > 3)) {
153 PyErr_Format(PyExc_ValueError,
154 "GPUTexture.__new__: \"size\" must be between 1 and 3 in length (got %d)",
162 else if (PyLong_Check(py_size)) {
163 size[0] = PyLong_AsLong(py_size);
166 PyErr_SetString(PyExc_ValueError,
"GPUTexture.__new__: Expected an int or tuple as first arg");
173 PyErr_SetString(PyExc_ValueError,
174 "GPUTexture.__new__: Only Buffer of format `FLOAT` is currently supported");
179 int component_size_expected =
sizeof(
float);
180 size_t data_space_expected = (size_t)
size[0] *
size[1] *
size[2] *
max_ii(1, layers) *
181 component_len * component_size_expected;
183 data_space_expected *= 6 *
size[0];
187 PyErr_SetString(PyExc_ValueError,
"GPUTexture.__new__: Buffer size smaller than requested");
194 if (is_cubemap &&
len != 1) {
196 "In cubemaps the same dimension represents height, width and depth. No tuple needed");
199 STRNCPY(err_out,
"Values less than 1 are not allowed in dimensions");
201 else if (layers &&
len == 3) {
202 STRNCPY(err_out,
"3D textures have no layers");
205 STRNCPY(err_out,
"No active GPU context found");
208 const char *name =
"python_texture";
248 PyErr_Format(PyExc_RuntimeError,
"gpu.texture.new(...) failed with '%s'", err_out);
255 PyDoc_STRVAR(pygpu_texture_width_doc,
"Width of the texture.\n\n:type: `int`");
262 PyDoc_STRVAR(pygpu_texture_height_doc,
"Height of the texture.\n\n:type: `int`");
269 PyDoc_STRVAR(pygpu_texture_format_doc,
"Format of the texture.\n\n:type: `str`");
278 pygpu_texture_clear_doc,
279 ".. method:: clear(format='FLOAT', value=(0.0, 0.0, 0.0, 1.0))\n"
281 " Fill texture with specific value.\n"
283 " :param format: The format that describes the content of a single item.\n"
284 " Possible values are `FLOAT`, `INT`, `UINT`, `UBYTE`, `UINT_24_8` and `10_11_11_REV`.\n"
286 " :arg value: sequence each representing the value to fill.\n"
287 " :type value: sequence of 1, 2, 3 or 4 values\n");
300 static const char *_keywords[] = {
"format",
"value",
NULL};
301 static _PyArg_Parser _parser = {
309 if (!_PyArg_ParseTupleAndKeywordsFast(
314 int shape = PySequence_Size(py_values);
320 PyErr_SetString(PyExc_AttributeError,
"too many dimensions, max is 4");
326 PyErr_SetString(PyExc_AttributeError,
327 "`UINT_24_8` and `10_11_11_REV` only support single values");
331 memset(&values, 0,
sizeof(values));
344 values.c[0] = values.i[0];
345 values.c[1] = values.i[1];
346 values.c[2] = values.i[2];
347 values.c[3] = values.i[3];
355 ".. method:: read()\n"
357 " Creates a buffer with the value of all pixels.\n"
367 switch (tex_format) {
407 int shape_len = (shape[2] == 1) ? 2 : 3;
411 #ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
413 ".. method:: free()\n"
415 " Free the texture object.\n"
416 " The texture object will no longer be accessible.\n");
430 #ifndef GPU_NO_USE_PY_REFERENCES
435 Py_TYPE(
self)->tp_free((PyObject *)
self);
448 METH_VARARGS | METH_KEYWORDS,
449 pygpu_texture_clear_doc},
451 #ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
452 {
"free", (PyCFunction)pygpu_texture_free, METH_NOARGS, pygpu_texture_free_doc},
458 pygpu_texture__tp_doc,
459 ".. class:: GPUTexture(size, layers=0, is_cubemap=False, format='RGBA8', data=None)\n"
461 " This object gives access to off GPU textures.\n"
463 " :arg size: Dimensions of the texture 1D, 2D, 3D or cubemap.\n"
464 " :type size: tuple or int\n"
465 " :arg layers: Number of layers in texture array or number of cubemaps in cubemap array\n"
466 " :type layers: int\n"
467 " :arg is_cubemap: Indicates the creation of a cubemap texture.\n"
468 " :type is_cubemap: int\n"
469 " :arg format: Internal data format inside GPU memory. Possible values are:\n"
500 " `R11F_G11F_B10F`,\n"
501 " `DEPTH32F_STENCIL8`,\n"
502 " `DEPTH24_STENCIL8`,\n"
505 " `SRGB8_A8_DXT1`,\n"
506 " `SRGB8_A8_DXT3`,\n"
507 " `SRGB8_A8_DXT5`,\n"
511 " `DEPTH_COMPONENT32F`,\n"
512 " `DEPTH_COMPONENT24`,\n"
513 " `DEPTH_COMPONENT16`,\n"
514 " :type format: str\n"
515 " :arg data: Buffer object to fill the texture.\n"
516 " :type data: :class:`gpu.types.Buffer`\n");
518 PyVarObject_HEAD_INIT(
NULL, 0).tp_name =
"GPUTexture",
521 .tp_flags = Py_TPFLAGS_DEFAULT,
522 .tp_doc = pygpu_texture__tp_doc,
535 ".. function:: from_image(image)\n"
537 " Get GPUTexture corresponding to an Image datablock. The GPUTexture memory is "
538 "shared with Blender.\n"
539 " Note: Colors read from the texture will be in scene linear color space and have "
540 "premultiplied or straight alpha matching the image alpha mode.\n"
542 " :arg image: The Image datablock.\n"
543 " :type image: :class:`bpy.types.Image`\n"
544 " :return: The GPUTexture used by the image.\n"
545 " :rtype: :class:`gpu.types.GPUTexture`\n");
565 PyDoc_STRVAR(pygpu_texture__m_doc,
"This module provides utils for textures.");
567 PyModuleDef_HEAD_INIT,
568 .m_name =
"gpu.texture",
569 .m_doc = pygpu_texture__m_doc,
588 PyExc_ValueError,
"expected a texture or None object, got %s", Py_TYPE(o)->tp_name);
618 if (shared_reference) {
619 #ifndef GPU_NO_USE_PY_REFERENCES
626 return (PyObject *)
self;
636 #ifndef GPU_NO_USE_PY_REFERENCES
641 return (PyObject *)
self;
646 #undef BPYGPU_TEXTURE_CHECK_OBJ
typedef float(TangentPoint)[2]
struct GPUTexture * BKE_image_get_gpu_texture(struct Image *image, struct ImageUser *iuser, struct ImBuf *ibuf)
void BKE_imageuser_default(struct ImageUser *iuser)
MINLINE int max_ii(int a, int b)
#define STRNCPY(dst, src)
#define POINTER_OFFSET(v, ofs)
GPUContext * GPU_context_active_get(void)
_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
GPUTexture * GPU_texture_create_2d_array(const char *name, int w, int h, int d, int mip_len, eGPUTextureFormat format, const float *data)
GPUTexture * GPU_texture_create_1d_array(const char *name, int w, int h, int mip_len, eGPUTextureFormat format, const float *data)
void GPU_texture_py_reference_set(GPUTexture *tex, void **py_ref)
void ** GPU_texture_py_reference_get(GPUTexture *tex)
int GPU_texture_height(const GPUTexture *tex)
GPUTexture * GPU_texture_create_1d(const char *name, int w, int mip_len, eGPUTextureFormat format, const float *data)
void GPU_texture_clear(GPUTexture *tex, eGPUDataFormat data_format, const void *data)
struct GPUTexture GPUTexture
int GPU_texture_width(const GPUTexture *tex)
void * GPU_texture_read(GPUTexture *tex, eGPUDataFormat data_format, int miplvl)
void GPU_texture_ref(GPUTexture *tex)
void GPU_texture_free(GPUTexture *tex)
GPUTexture * GPU_texture_create_2d(const char *name, int w, int h, int mip_len, eGPUTextureFormat format, const float *data)
GPUTexture * GPU_texture_create_3d(const char *name, int w, int h, int d, int mip_len, eGPUTextureFormat texture_format, eGPUDataFormat data_format, const void *data)
eGPUTextureFormat GPU_texture_format(const GPUTexture *tex)
GPUTexture * GPU_texture_create_cube(const char *name, int w, int mip_len, eGPUTextureFormat format, const float *data)
size_t GPU_texture_component_len(eGPUTextureFormat format)
GPUTexture * GPU_texture_create_cube_array(const char *name, int w, int d, int mip_len, eGPUTextureFormat format, const float *data)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img GPU_RGBA16F
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx GPU_R32F
struct PyC_StringEnumItems bpygpu_dataformat_items[]
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
BPyGPUBuffer * BPyGPU_Buffer_CreatePyObject(const int format, const Py_ssize_t *shape, const int shape_len, void *buffer)
PyTypeObject BPyGPU_BufferType
size_t bpygpu_Buffer_size(BPyGPUBuffer *buffer)
#define BPYGPU_USE_GPUOBJ_FREE_METHOD
static PyObject * pygpu_texture_read(BPyGPUTexture *self)
static PyObject * pygpu_texture_format_get(BPyGPUTexture *self, void *UNUSED(type))
PyTypeObject BPyGPUTexture_Type
static PyObject * pygpu_texture__tp_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
static struct PyMethodDef pygpu_texture__m_methods[]
static struct PyMethodDef pygpu_texture__tp_methods[]
PyObject * bpygpu_texture_init(void)
static const struct PyC_StringEnumItems pygpu_textureformat_items[]
static void BPyGPUTexture__tp_dealloc(BPyGPUTexture *self)
static PyObject * pygpu_texture_clear(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
PyObject * BPyGPUTexture_CreatePyObject(GPUTexture *tex, bool shared_reference)
static int pygpu_texture_valid_check(BPyGPUTexture *bpygpu_tex)
static PyGetSetDef pygpu_texture__tp_getseters[]
static PyObject * pygpu_texture_width_get(BPyGPUTexture *self, void *UNUSED(type))
PyDoc_STRVAR(pygpu_texture_width_doc, "Width of the texture.\n\n:type: `int`")
#define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu)
int bpygpu_ParseTexture(PyObject *o, void *p)
static PyObject * pygpu_texture_from_image(PyObject *UNUSED(self), PyObject *arg)
static PyObject * pygpu_texture_height_get(BPyGPUTexture *self, void *UNUSED(type))
static PyModuleDef pygpu_texture_module_def
struct BPyGPUTexture BPyGPUTexture
#define BPyGPUTexture_Check(v)
int PyC_ParseStringEnum(PyObject *o, void *p)
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
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 struct PyC_StringEnumItems *items, const int value)
union BPyGPUBuffer::@1155 buf
PyObject_HEAD struct GPUTexture * tex