Blender V5.3
gpu_py_texture.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include <Python.h>
15
16#include "BLI_math_base_c.hh"
17#include "BLI_string_utf8.hh"
18
19#include "DNA_image_types.h"
20
21#include "GPU_capabilities.hh"
22#include "GPU_context.hh"
23#include "GPU_texture.hh"
24
25#include "BKE_global.hh"
26#include "BKE_image.hh"
27#include "BKE_image_gpu.hh"
28
30#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
31
32#include "gpu_py.hh"
33#include "gpu_py_buffer.hh"
34
35#include "gpu_py_texture.hh" /* own include */
36
37/* Docstring Literal type for texture formats. */
38
39#define PYDOC_TEX_FORMAT_LITERAL \
40 "Literal[" \
41 "'RGBA8UI', " \
42 "'RGBA8I', " \
43 "'RGBA8', " \
44 "'RGBA32UI', " \
45 "'RGBA32I', " \
46 "'RGBA32F', " \
47 "'RGBA16UI', " \
48 "'RGBA16I', " \
49 "'RGBA16F', " \
50 "'RGBA16', " \
51 "'RG8UI', " \
52 "'RG8I', " \
53 "'RG8', " \
54 "'RG32UI', " \
55 "'RG32I', " \
56 "'RG32F', " \
57 "'RG16UI', " \
58 "'RG16I', " \
59 "'RG16F', " \
60 "'RG16', " \
61 "'R8UI', " \
62 "'R8I', " \
63 "'R8', " \
64 "'R32UI', " \
65 "'R32I', " \
66 "'R32F', " \
67 "'R16UI', " \
68 "'R16I', " \
69 "'R16F', " \
70 "'R16', " \
71 "'R11F_G11F_B10F', " \
72 "'DEPTH32F_STENCIL8', " \
73 "'DEPTH24_STENCIL8', " \
74 "'SRGB8_A8', " \
75 "'RGB16F', " \
76 "'SRGB8_A8_DXT1', " \
77 "'SRGB8_A8_DXT3', " \
78 "'SRGB8_A8_DXT5', " \
79 "'RGBA8_DXT1', " \
80 "'RGBA8_DXT3', " \
81 "'RGBA8_DXT5', " \
82 "'DEPTH_COMPONENT32F', " \
83 "'DEPTH_COMPONENT24', " \
84 "'DEPTH_COMPONENT16']"
85
86namespace blender {
87
88/* -------------------------------------------------------------------- */
91
93 {int(gpu::TextureFormat::UINT_8_8_8_8), "RGBA8UI"},
94 {int(gpu::TextureFormat::SINT_8_8_8_8), "RGBA8I"},
95 {int(gpu::TextureFormat::UNORM_8_8_8_8), "RGBA8"},
96 {int(gpu::TextureFormat::UINT_32_32_32_32), "RGBA32UI"},
97 {int(gpu::TextureFormat::SINT_32_32_32_32), "RGBA32I"},
98 {int(gpu::TextureFormat::SFLOAT_32_32_32_32), "RGBA32F"},
99 {int(gpu::TextureFormat::UINT_16_16_16_16), "RGBA16UI"},
100 {int(gpu::TextureFormat::SINT_16_16_16_16), "RGBA16I"},
101 {int(gpu::TextureFormat::SFLOAT_16_16_16_16), "RGBA16F"},
102 {int(gpu::TextureFormat::UNORM_16_16_16_16), "RGBA16"},
103 {int(gpu::TextureFormat::UINT_8_8), "RG8UI"},
104 {int(gpu::TextureFormat::SINT_8_8), "RG8I"},
105 {int(gpu::TextureFormat::UNORM_8_8), "RG8"},
106 {int(gpu::TextureFormat::UINT_32_32), "RG32UI"},
107 {int(gpu::TextureFormat::SINT_32_32), "RG32I"},
108 {int(gpu::TextureFormat::SFLOAT_32_32), "RG32F"},
109 {int(gpu::TextureFormat::UINT_16_16), "RG16UI"},
110 {int(gpu::TextureFormat::SINT_16_16), "RG16I"},
111 {int(gpu::TextureFormat::SFLOAT_16_16), "RG16F"},
112 {int(gpu::TextureFormat::UNORM_16_16), "RG16"},
113 {int(gpu::TextureFormat::UINT_8), "R8UI"},
114 {int(gpu::TextureFormat::SINT_8), "R8I"},
115 {int(gpu::TextureFormat::UNORM_8), "R8"},
116 {int(gpu::TextureFormat::UINT_32), "R32UI"},
117 {int(gpu::TextureFormat::SINT_32), "R32I"},
118 {int(gpu::TextureFormat::SFLOAT_32), "R32F"},
119 {int(gpu::TextureFormat::UINT_16), "R16UI"},
120 {int(gpu::TextureFormat::SINT_16), "R16I"},
121 {int(gpu::TextureFormat::SFLOAT_16), "R16F"},
122 {int(gpu::TextureFormat::UNORM_16), "R16"},
123 {int(gpu::TextureFormat::UFLOAT_11_11_10), "R11F_G11F_B10F"},
124 {int(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8), "DEPTH32F_STENCIL8"},
125 {GPU_DEPTH24_STENCIL8_DEPRECATED, "DEPTH24_STENCIL8"},
126 {int(gpu::TextureFormat::SRGBA_8_8_8_8), "SRGB8_A8"},
127 {int(gpu::TextureFormat::SFLOAT_16_16_16), "RGB16F"},
128 {int(gpu::TextureFormat::SRGB_DXT1), "SRGB8_A8_DXT1"},
129 {int(gpu::TextureFormat::SRGB_DXT3), "SRGB8_A8_DXT3"},
130 {int(gpu::TextureFormat::SRGB_DXT5), "SRGB8_A8_DXT5"},
131 {int(gpu::TextureFormat::SNORM_DXT1), "RGBA8_DXT1"},
132 {int(gpu::TextureFormat::SNORM_DXT3), "RGBA8_DXT3"},
133 {int(gpu::TextureFormat::SNORM_DXT5), "RGBA8_DXT5"},
134 {int(gpu::TextureFormat::SFLOAT_32_DEPTH), "DEPTH_COMPONENT32F"},
135 {GPU_DEPTH_COMPONENT24_DEPRECATED, "DEPTH_COMPONENT24"},
136 {int(gpu::TextureFormat::UNORM_16_DEPTH), "DEPTH_COMPONENT16"},
137 {0, nullptr},
138};
139
147
149{
150 if (bpygpu_tex->tex == nullptr) [[unlikely]] {
151 PyErr_SetString(PyExc_ReferenceError,
153 "GPU texture was freed, no further access is valid"
154#else
155 "GPU texture: internal error"
156#endif
157 );
158
159 return -1;
160 }
161 return 0;
162}
163
164#define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu) \
165 { \
166 if (pygpu_texture_valid_check(bpygpu) == -1) [[unlikely]] { \
167 return nullptr; \
168 } \
169 } \
170 ((void)0)
171
173
174/* -------------------------------------------------------------------- */
177
178#define BPYGPU_TEXTURE_EXTEND_MODE_ARG_DOC \
179 " :param extend_mode: the specified extent mode.\n" \
180 " :type extend_mode: Literal['EXTEND', 'REPEAT', 'MIRRORED_REPEAT', 'CLAMP_TO_BORDER']\n";
181
192static bool pygpu_texture_size_check(const int size[3],
193 const int len,
194 const bool is_cubemap,
195 char *err_out,
196 const size_t err_out_size)
197{
198 const bool is_3d = (len == 3);
199 const int limit = is_3d ? GPU_max_texture_3d_size() : GPU_max_texture_size();
200 /* Cubemaps are created from a single dimension (the face size). */
201 const int dims = is_cubemap ? 1 : len;
202
203 for (int i = 0; i < dims; i++) {
204 if (size[i] <= limit) {
205 continue;
206 }
207 if (is_3d) {
208 BLI_snprintf_utf8(err_out,
209 err_out_size,
210 "3D texture of size %dx%dx%d exceeds the GPU limit of %d per axis",
211 size[0],
212 size[1],
213 size[2],
214 limit);
215 }
216 else if (is_cubemap) {
217 BLI_snprintf_utf8(err_out,
218 err_out_size,
219 "Cubemap of face size %d exceeds the GPU limit of %d",
220 size[0],
221 limit);
222 }
223 else if (len == 2) {
224 BLI_snprintf_utf8(err_out,
225 err_out_size,
226 "2D texture of size %dx%d exceeds the GPU limit of %d",
227 size[0],
228 size[1],
229 limit);
230 }
231 else {
232 BLI_snprintf_utf8(err_out,
233 err_out_size,
234 "1D texture of size %d exceeds the GPU limit of %d",
235 size[0],
236 limit);
237 }
238 return false;
239 }
240 return true;
241}
242
243static PyObject *pygpu_texture__tp_new(PyTypeObject * /*self*/, PyObject *args, PyObject *kwds)
244{
246
247 PyObject *py_size;
248 int size[3] = {1, 1, 1};
249 int layers = 0;
250 int is_cubemap = false;
251 PyC_StringEnum pygpu_textureformat = {pygpu_textureformat_items,
252 int(gpu::TextureFormat::UNORM_8_8_8_8)};
253 BPyGPUBuffer *pybuffer_obj = nullptr;
254 PyC_TypeOrNone pybuffer_or_none = PyC_TYPE_OR_NONE_INIT(&BPyGPU_BufferType, &pybuffer_obj);
255 char err_out[256];
256 STRNCPY_UTF8(err_out,
257 (G.debug & G_DEBUG_GPU) ?
258 "the driver could not create this texture; see the console for details" :
259 "the driver could not create this texture; run with --debug-gpu for details");
260
261 static const char *_keywords[] = {"size", "layers", "is_cubemap", "format", "data", nullptr};
262 static _PyArg_Parser _parser = {
263 "O" /* `size` */
264 "|$" /* Optional, keyword only arguments. */
265 "i" /* `layers` */
266 "p" /* `is_cubemap` */
267 "O&" /* `format` */
268 "O&" /* `data` */
269 ":GPUTexture.__new__",
270 _keywords,
271 nullptr,
272 };
273 if (!_PyArg_ParseTupleAndKeywordsFast(args,
274 kwds,
275 &_parser,
276 &py_size,
277 &layers,
278 &is_cubemap,
280 &pygpu_textureformat,
282 &pybuffer_or_none))
283 {
284 return nullptr;
285 }
286
287 if (pygpu_textureformat.value_found == GPU_DEPTH24_STENCIL8_DEPRECATED) {
288 pygpu_textureformat.value_found = int(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
289 PyErr_WarnEx(
290 PyExc_DeprecationWarning, "'DEPTH24_STENCIL8' is deprecated. Use 'DEPTH32F_STENCIL8'.", 1);
291 }
292 if (pygpu_textureformat.value_found == GPU_DEPTH_COMPONENT24_DEPRECATED) {
293 pygpu_textureformat.value_found = int(gpu::TextureFormat::SFLOAT_32_DEPTH);
294 PyErr_WarnEx(PyExc_DeprecationWarning,
295 "'DEPTH_COMPONENT24' is deprecated. Use 'DEPTH_COMPONENT32F'.",
296 1);
297 }
298
299 int len = 1;
300 if (PySequence_Check(py_size)) {
301 len = PySequence_Size(py_size);
302 if ((len < 1) || (len > 3)) {
303 PyErr_Format(PyExc_ValueError,
304 "GPUTexture.__new__: \"size\" must be between 1 and 3 in length (got %d)",
305 len);
306 return nullptr;
307 }
308 if (PyC_AsArray(size, sizeof(*size), py_size, len, &PyLong_Type, "GPUTexture.__new__") == -1) {
309 return nullptr;
310 }
311 }
312 else if (PyLong_Check(py_size)) {
313 size[0] = PyLong_AsLong(py_size);
314 }
315 else {
316 PyErr_SetString(PyExc_ValueError, "GPUTexture.__new__: Expected an int or tuple as first arg");
317 return nullptr;
318 }
319
320 void *data = nullptr;
321 if (pybuffer_obj) {
322 if (pybuffer_obj->format != GPU_DATA_FLOAT) {
323 PyErr_SetString(PyExc_ValueError,
324 "GPUTexture.__new__: Only Buffer of format `FLOAT` is currently supported");
325 return nullptr;
326 }
327
328 int component_len = GPU_texture_component_len(
329 gpu::TextureFormat(pygpu_textureformat.value_found));
330 int component_size_expected = sizeof(float);
331 size_t data_space_expected = size_t(size[0]) * size[1] * size[2] * max_ii(1, layers) *
332 component_len * component_size_expected;
333 if (is_cubemap) {
334 data_space_expected *= 6 * size[0];
335 }
336
337 if (bpygpu_Buffer_size(pybuffer_obj) < data_space_expected) {
338 PyErr_SetString(PyExc_ValueError, "GPUTexture.__new__: Buffer size smaller than requested");
339 return nullptr;
340 }
341 data = pybuffer_obj->buf.as_void;
342 }
343
344 gpu::Texture *tex = nullptr;
345 if (is_cubemap && len != 1) {
346 STRNCPY_UTF8(err_out, "Cubemaps use a single dimension, not a tuple");
347 }
348 else if (size[0] < 1 || size[1] < 1 || size[2] < 1) {
349 STRNCPY_UTF8(err_out, "Texture dimensions must be at least 1");
350 }
351 else if (layers && len == 3) {
352 STRNCPY_UTF8(err_out, "3D textures do not support layers");
353 }
354 else if (!GPU_context_active_get()) {
355 STRNCPY_UTF8(err_out, "No active GPU context found");
356 }
357 else if (!pygpu_texture_size_check(size, len, is_cubemap, err_out, sizeof(err_out))) {
358 /* Specific message written by pygpu_texture_size_check(). */
359 }
360 else {
361 const char *name = "python_texture";
363 if (is_cubemap) {
364 if (layers) {
366 size[0],
367 layers,
368 1,
369 gpu::TextureFormat(pygpu_textureformat.value_found),
370 usage,
371 static_cast<const float *>(data));
372 }
373 else {
374 tex = GPU_texture_create_cube(name,
375 size[0],
376 1,
377 gpu::TextureFormat(pygpu_textureformat.value_found),
378 usage,
379 static_cast<const float *>(data));
380 }
381 }
382 else if (layers) {
383 if (len == 2) {
385 size[0],
386 size[1],
387 layers,
388 1,
389 gpu::TextureFormat(pygpu_textureformat.value_found),
390 usage,
391 static_cast<const float *>(data));
392 }
393 else {
395 size[0],
396 layers,
397 1,
398 gpu::TextureFormat(pygpu_textureformat.value_found),
399 usage,
400 static_cast<const float *>(data));
401 }
402 }
403 else if (len == 3) {
404 tex = GPU_texture_create_3d(name,
405 size[0],
406 size[1],
407 size[2],
408 1,
409 gpu::TextureFormat(pygpu_textureformat.value_found),
410 usage,
411 data);
412 }
413 else if (len == 2) {
414 tex = GPU_texture_create_2d(name,
415 size[0],
416 size[1],
417 1,
418 gpu::TextureFormat(pygpu_textureformat.value_found),
419 usage,
420 static_cast<const float *>(data));
421 }
422 else {
423 tex = GPU_texture_create_1d(name,
424 size[0],
425 1,
426 gpu::TextureFormat(pygpu_textureformat.value_found),
427 usage,
428 static_cast<const float *>(data));
429 }
430 }
431
432 if (tex == nullptr) {
433 PyErr_Format(PyExc_RuntimeError, "gpu.texture.new(...) failed with '%s'", err_out);
434 return nullptr;
435 }
436
437 return BPyGPUTexture_CreatePyObject(tex, false);
438}
439
441 /* Wrap. */
442 pygpu_texture_width_doc,
443 "Width of the texture.\n"
444 "\n"
445 ":type: int\n");
446static PyObject *pygpu_texture_width_get(BPyGPUTexture *self, void * /*type*/)
447{
449 return PyLong_FromLong(GPU_texture_width(self->tex));
450}
451
453 /* Wrap. */
454 pygpu_texture_height_doc,
455 "Height of the texture.\n"
456 "\n"
457 ":type: int\n");
458static PyObject *pygpu_texture_height_get(BPyGPUTexture *self, void * /*type*/)
459{
461 return PyLong_FromLong(GPU_texture_height(self->tex));
462}
463
465 /* Wrap. */
466 pygpu_texture_format_doc,
467 "Format of the texture.\n"
468 "\n"
469 ":type: str\n");
470static PyObject *pygpu_texture_format_get(BPyGPUTexture *self, void * /*type*/)
471{
474 return PyUnicode_FromString(
476}
477
479 /* Wrap. */
480 pygpu_texture_extend_mode_x_doc,
481 ".. method:: extend_mode_x(extend_mode='EXTEND', /)\n"
482 "\n"
483 " Set texture sampling method for coordinates outside of the [0..1] uv range along the x "
484 "axis.\n"
486static PyObject *pygpu_texture_extend_mode_x(BPyGPUTexture *self, PyObject *value)
487{
489
491 if (!PyC_ParseStringEnum(value, &extend_mode)) {
492 return nullptr;
493 }
494
496 Py_RETURN_NONE;
497}
498
500 /* Wrap. */
501 pygpu_texture_extend_mode_y_doc,
502 ".. method:: extend_mode_y(extend_mode='EXTEND', /)\n"
503 "\n"
504 " Set texture sampling method for coordinates outside of the [0..1] uv range along the y "
505 "axis.\n"
507static PyObject *pygpu_texture_extend_mode_y(BPyGPUTexture *self, PyObject *value)
508{
510
512 if (!PyC_ParseStringEnum(value, &extend_mode)) {
513 return nullptr;
514 }
515
517 Py_RETURN_NONE;
518}
519
521 /* Wrap. */
522 pygpu_texture_extend_mode_doc,
523 ".. method:: extend_mode(extend_mode='EXTEND', /)\n"
524 "\n"
525 " Set texture sampling method for coordinates outside of the [0..1] uv range along\n"
526 " both the x and y axis.\n"
528static PyObject *pygpu_texture_extend_mode(BPyGPUTexture *self, PyObject *value)
529{
531
533 if (!PyC_ParseStringEnum(value, &extend_mode)) {
534 return nullptr;
535 }
536
538 Py_RETURN_NONE;
539}
540
542 /* Wrap. */
543 pygpu_texture_filter_mode_doc,
544 ".. method:: filter_mode(use_filter)\n"
545 "\n"
546 " Set texture filter usage.\n"
547 "\n"
548 " :param use_filter: If set to true, the texture will use linear interpolation between "
549 "neighboring texels.\n"
550 " :type use_filter: bool\n");
551static PyObject *pygpu_texture_filter_mode(BPyGPUTexture *self, PyObject *value)
552{
554
555 bool use_filter;
556 if (!PyC_ParseBool(value, &use_filter)) {
557 return nullptr;
558 }
559
560 GPU_texture_filter_mode(self->tex, use_filter);
561 Py_RETURN_NONE;
562}
563
565 /* Wrap. */
566 pygpu_texture_mipmap_mode_doc,
567 ".. method:: mipmap_mode(use_mipmap=True, use_filter=True)\n"
568 "\n"
569 " Set texture filter and mip-map usage.\n"
570 "\n"
571 " :param use_mipmap: If set to true, the texture will use mip-mapping as anti-aliasing "
572 "method.\n"
573 " :type use_mipmap: bool\n"
574 " :param use_filter: If set to true, the texture will use linear interpolation between "
575 "neighboring texels.\n"
576 " :type use_filter: bool\n");
577static PyObject *pygpu_texture_mipmap_mode(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
578{
580
581 bool use_mipmap = true;
582 bool use_filter = true;
583 static const char *_keywords[] = {"use_mipmap", "use_filter"};
584 static _PyArg_Parser _parser = {
585 "|$" /* Optional, keyword only arguments. */
586 "b" /* `use_mipmap` */
587 "b" /* `use_filter` */
588 ":mipmap_mode",
589 _keywords,
590 nullptr,
591 };
592
593 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &use_mipmap, &use_filter)) {
594 return nullptr;
595 }
596
597 GPU_texture_mipmap_mode(self->tex, use_mipmap, use_filter);
598 Py_RETURN_NONE;
599}
600
602 /* Wrap. */
603 pygpu_texture_anisotropic_filter_doc,
604 ".. method:: anisotropic_filter(use_anisotropic)\n"
605 "\n"
606 " Set anisotropic filter usage. This only has effect if mipmapping is enabled.\n"
607 "\n"
608 " :param use_anisotropic: If set to true, the texture will use anisotropic filtering.\n"
609 " :type use_anisotropic: bool\n");
610static PyObject *pygpu_texture_anisotropic_filter(BPyGPUTexture *self, PyObject *value)
611{
613
614 bool use_anisotropic;
615 if (!PyC_ParseBool(value, &use_anisotropic)) {
616 return nullptr;
617 }
618
619 GPU_texture_anisotropic_filter(self->tex, use_anisotropic);
620 Py_RETURN_NONE;
621}
622
624 /* Wrap. */
625 pygpu_texture_clear_doc,
626 ".. method:: clear(format='FLOAT', value=(0.0, 0.0, 0.0, 1.0))\n"
627 "\n"
628 " Fill texture with specific value.\n"
629 "\n"
630 " :param format: The format that describes the content of a single item.\n"
631 " ``UINT_24_8`` is deprecated, use ``FLOAT`` instead.\n"
632 " :type format: " PYDOC_DATAFORMAT_LITERAL
633 "\n"
634 " :param value: Sequence each representing the value to fill. Sizes 1..4 are supported.\n"
635 " :type value: Sequence[float] | Sequence[int]\n");
636static PyObject *pygpu_texture_clear(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
637{
639 PyC_StringEnum pygpu_dataformat = {bpygpu_dataformat_items};
640 union {
641 int i[4];
642 float f[4];
643 char c[4];
644 } values;
645
646 PyObject *py_values;
647
648 static const char *_keywords[] = {"format", "value", nullptr};
649 static _PyArg_Parser _parser = {
650 "$" /* Keyword only arguments. */
651 "O&" /* `format` */
652 "O" /* `value` */
653 ":clear",
654 _keywords,
655 nullptr,
656 };
657 if (!_PyArg_ParseTupleAndKeywordsFast(
658 args, kwds, &_parser, PyC_ParseStringEnum, &pygpu_dataformat, &py_values))
659 {
660 return nullptr;
661 }
662 if (pygpu_dataformat.value_found == GPU_DATA_UINT_24_8_DEPRECATED) {
663 PyErr_WarnEx(PyExc_DeprecationWarning, "`UINT_24_8` is deprecated, use `FLOAT` instead", 1);
664 }
665
666 int shape = PySequence_Size(py_values);
667 if (shape == -1) {
668 return nullptr;
669 }
670
671 if (shape > 4) {
672 PyErr_SetString(PyExc_AttributeError, "too many dimensions, max is 4");
673 return nullptr;
674 }
675
676 if (shape != 1 &&
678 {
679 PyErr_SetString(PyExc_AttributeError,
680 "`UINT_24_8` and `10_11_11_REV` only support single values");
681 return nullptr;
682 }
683
684 memset(&values, 0, sizeof(values));
685 if (PyC_AsArray(&values,
686 (pygpu_dataformat.value_found == GPU_DATA_FLOAT) ? sizeof(*values.f) :
687 sizeof(*values.i),
688 py_values,
689 shape,
690 (pygpu_dataformat.value_found == GPU_DATA_FLOAT) ? &PyFloat_Type : &PyLong_Type,
691 "clear") == -1)
692 {
693 return nullptr;
694 }
695
696 if (pygpu_dataformat.value_found == GPU_DATA_UBYTE) {
697 /* Convert to byte. */
698 values.c[0] = values.i[0];
699 values.c[1] = values.i[1];
700 values.c[2] = values.i[2];
701 values.c[3] = values.i[3];
702 }
703
704 GPU_texture_clear(self->tex, eGPUDataFormat(pygpu_dataformat.value_found), &values);
705 Py_RETURN_NONE;
706}
707
709 /* Wrap. */
710 pygpu_texture_read_doc,
711 ".. method:: read()\n"
712 "\n"
713 " Creates a buffer with the value of all pixels.\n"
714 "\n"
715 " :return: The Buffer with the read pixels.\n"
716 " :rtype: :class:`gpu.types.Buffer`\n");
717static PyObject *pygpu_texture_read(BPyGPUTexture *self)
718{
720 gpu::TextureFormat tex_format = GPU_texture_format(self->tex);
721
722 /* #GPU_texture_read is restricted in combining 'data_format' with 'tex_format'.
723 * So choose data_format here. */
724 eGPUDataFormat best_data_format;
725 switch (tex_format) {
726 case gpu::TextureFormat::UNORM_16_DEPTH:
727 case gpu::TextureFormat::SFLOAT_32_DEPTH:
728 case gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8:
729 best_data_format = GPU_DATA_FLOAT;
730 break;
731 case gpu::TextureFormat::UINT_8:
732 case gpu::TextureFormat::UINT_16:
733 case gpu::TextureFormat::UINT_16_16:
734 case gpu::TextureFormat::UINT_32:
735 best_data_format = GPU_DATA_UINT;
736 break;
737 case gpu::TextureFormat::SINT_16_16:
738 case gpu::TextureFormat::SINT_16:
739 best_data_format = GPU_DATA_INT;
740 break;
741 case gpu::TextureFormat::UNORM_8:
742 case gpu::TextureFormat::UNORM_8_8:
743 case gpu::TextureFormat::UNORM_8_8_8_8:
744 case gpu::TextureFormat::UINT_8_8_8_8:
745 case gpu::TextureFormat::SRGBA_8_8_8_8:
746 best_data_format = GPU_DATA_UBYTE;
747 break;
748 case gpu::TextureFormat::UFLOAT_11_11_10:
749 best_data_format = GPU_DATA_10_11_11_REV;
750 break;
751 default:
752 best_data_format = GPU_DATA_FLOAT;
753 break;
754 }
755
756 void *buf = GPU_texture_read(self->tex, best_data_format, 0);
757 const Py_ssize_t component_len = GPU_texture_component_len(tex_format);
758 Py_ssize_t shape[4];
759 int shape_len;
760 if (GPU_texture_dimensions(self->tex) == 3) {
761 shape[0] = GPU_texture_depth(self->tex);
762 shape[1] = GPU_texture_height(self->tex);
763 shape[2] = GPU_texture_width(self->tex);
764 shape[3] = component_len;
765 shape_len = (component_len == 1) ? 3 : 4;
766 }
767 else {
768 shape[0] = GPU_texture_height(self->tex);
769 shape[1] = GPU_texture_width(self->tex);
770 shape[2] = component_len;
771 shape_len = (component_len == 1) ? 2 : 3;
772 }
773 return reinterpret_cast<PyObject *>(
774 BPyGPU_Buffer_CreatePyObject(best_data_format, shape, shape_len, buf));
775}
776
777#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
779 /* Wrap. */
780 pygpu_texture_free_doc,
781 ".. method:: free()\n"
782 "\n"
783 " Free the texture object.\n"
784 " The texture object will no longer be accessible.\n");
785static PyObject *pygpu_texture_free(BPyGPUTexture *self)
786{
788
789 GPU_texture_free(self->tex);
790 self->tex = nullptr;
791 Py_RETURN_NONE;
792}
793#endif
794
796{
797 if (self->tex) {
798#ifndef GPU_NO_USE_PY_REFERENCES
799 GPU_texture_py_reference_set(self->tex, nullptr);
800#endif
801 GPU_texture_free(self->tex);
802 }
803 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
804}
805
806static PyGetSetDef pygpu_texture__tp_getseters[] = {
807 {"width",
808 reinterpret_cast<getter>(pygpu_texture_width_get),
809 static_cast<setter>(nullptr),
810 pygpu_texture_width_doc,
811 nullptr},
812 {"height",
813 reinterpret_cast<getter>(pygpu_texture_height_get),
814 static_cast<setter>(nullptr),
815 pygpu_texture_height_doc,
816 nullptr},
817 {"format",
818 reinterpret_cast<getter>(pygpu_texture_format_get),
819 static_cast<setter>(nullptr),
820 pygpu_texture_format_doc,
821 nullptr},
822 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
823};
824
825#ifdef __GNUC__
826# ifdef __clang__
827# pragma clang diagnostic push
828# pragma clang diagnostic ignored "-Wcast-function-type"
829# else
830# pragma GCC diagnostic push
831# pragma GCC diagnostic ignored "-Wcast-function-type"
832# endif
833#endif
834
835static PyMethodDef pygpu_texture__tp_methods[] = {
836 {"clear",
837 reinterpret_cast<PyCFunction>(pygpu_texture_clear),
838 METH_VARARGS | METH_KEYWORDS,
839 pygpu_texture_clear_doc},
840 {"read",
841 reinterpret_cast<PyCFunction>(pygpu_texture_read),
842 METH_NOARGS,
843 pygpu_texture_read_doc},
844#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
845 {"free", (PyCFunction)pygpu_texture_free, METH_NOARGS, pygpu_texture_free_doc},
846#endif
847 {"extend_mode_x",
848 reinterpret_cast<PyCFunction>(pygpu_texture_extend_mode_x),
849 METH_O,
850 pygpu_texture_extend_mode_x_doc},
851 {"extend_mode_y",
852 reinterpret_cast<PyCFunction>(pygpu_texture_extend_mode_y),
853 METH_O,
854 pygpu_texture_extend_mode_y_doc},
855 {"extend_mode",
856 reinterpret_cast<PyCFunction>(pygpu_texture_extend_mode),
857 METH_O,
858 pygpu_texture_extend_mode_doc},
859 {"filter_mode",
860 reinterpret_cast<PyCFunction>(pygpu_texture_filter_mode),
861 METH_O,
862 pygpu_texture_filter_mode_doc},
863 {"mipmap_mode",
864 reinterpret_cast<PyCFunction>(pygpu_texture_mipmap_mode),
865 METH_VARARGS | METH_KEYWORDS,
866 pygpu_texture_mipmap_mode_doc},
867 {"anisotropic_filter",
868 reinterpret_cast<PyCFunction>(pygpu_texture_anisotropic_filter),
869 METH_O,
870 pygpu_texture_anisotropic_filter_doc},
871 {nullptr, nullptr, 0, nullptr},
872};
873
874#ifdef __GNUC__
875# ifdef __clang__
876# pragma clang diagnostic pop
877# else
878# pragma GCC diagnostic pop
879# endif
880#endif
881
883 /* Wrap. */
884 pygpu_texture__tp_doc,
885 ".. class:: GPUTexture\n"
886 "\n"
887 " This object gives access to GPU textures.\n"
888 "\n"
889 " .. method:: __init__(size, *, layers=0, is_cubemap=False, format='RGBA8', data=None)\n"
890 "\n"
891 " :param size: Dimensions of the texture 1D, 2D, 3D or cubemap.\n"
892 " :type size: int | Sequence[int]\n"
893 " :param layers: Number of layers in texture array or number of cubemaps in cubemap "
894 "array\n"
895 " :type layers: int\n"
896 " :param is_cubemap: Indicates the creation of a cubemap texture.\n"
897 " :type is_cubemap: bool\n"
898 " :param format: Internal data format inside GPU memory.\n"
899 " ``DEPTH24_STENCIL8`` is deprecated, use ``DEPTH32F_STENCIL8``.\n"
900 " ``DEPTH_COMPONENT24`` is deprecated, use ``DEPTH_COMPONENT32F``.\n"
901 " :type format: " PYDOC_TEX_FORMAT_LITERAL
902 "\n"
903 " :param data: Buffer object to fill the texture.\n"
904 " :type data: :class:`gpu.types.Buffer` | None\n");
905PyTypeObject BPyGPUTexture_Type = {
906 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
907 /*tp_name*/ "GPUTexture",
908 /*tp_basicsize*/ sizeof(BPyGPUTexture),
909 /*tp_itemsize*/ 0,
910 /*tp_dealloc*/ reinterpret_cast<destructor>(BPyGPUTexture__tp_dealloc),
911 /*tp_vectorcall_offset*/ 0,
912 /*tp_getattr*/ nullptr,
913 /*tp_setattr*/ nullptr,
914 /*tp_as_async*/ nullptr,
915 /*tp_repr*/ nullptr,
916 /*tp_as_number*/ nullptr,
917 /*tp_as_sequence*/ nullptr,
918 /*tp_as_mapping*/ nullptr,
919 /*tp_hash*/ nullptr,
920 /*tp_call*/ nullptr,
921 /*tp_str*/ nullptr,
922 /*tp_getattro*/ nullptr,
923 /*tp_setattro*/ nullptr,
924 /*tp_as_buffer*/ nullptr,
925 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
926 /*tp_doc*/ pygpu_texture__tp_doc,
927 /*tp_traverse*/ nullptr,
928 /*tp_clear*/ nullptr,
929 /*tp_richcompare*/ nullptr,
930 /*tp_weaklistoffset*/ 0,
931 /*tp_iter*/ nullptr,
932 /*tp_iternext*/ nullptr,
933 /*tp_methods*/ pygpu_texture__tp_methods,
934 /*tp_members*/ nullptr,
935 /*tp_getset*/ pygpu_texture__tp_getseters,
936 /*tp_base*/ nullptr,
937 /*tp_dict*/ nullptr,
938 /*tp_descr_get*/ nullptr,
939 /*tp_descr_set*/ nullptr,
940 /*tp_dictoffset*/ 0,
941 /*tp_init*/ nullptr,
942 /*tp_alloc*/ nullptr,
943 /*tp_new*/ pygpu_texture__tp_new,
944 /*tp_free*/ nullptr,
945 /*tp_is_gc*/ nullptr,
946 /*tp_bases*/ nullptr,
947 /*tp_mro*/ nullptr,
948 /*tp_cache*/ nullptr,
949 /*tp_subclasses*/ nullptr,
950 /*tp_weaklist*/ nullptr,
951 /*tp_del*/ nullptr,
952 /*tp_version_tag*/ 0,
953 /*tp_finalize*/ nullptr,
954 /*tp_vectorcall*/ nullptr,
955};
956
958
959/* -------------------------------------------------------------------- */
962
964 /* Wrap. */
965 pygpu_texture_from_image_doc,
966 ".. function:: from_image(image)\n"
967 "\n"
968 " Get GPUTexture corresponding to an Image data-block. The GPUTexture "
969 "memory is "
970 "shared with Blender.\n"
971 " Note: Colors read from the texture will be in scene linear color space and have "
972 "premultiplied or straight alpha matching the image alpha mode.\n"
973 "\n"
974 " :param image: The Image data-block.\n"
975 " :type image: :class:`bpy.types.Image`\n"
976 " :return: The GPUTexture used by the image.\n"
977 " :rtype: :class:`gpu.types.GPUTexture`\n");
978static PyObject *pygpu_texture_from_image(PyObject * /*self*/, PyObject *arg)
979{
980 Image *ima = static_cast<Image *>(PyC_RNA_AsPointer(arg, "Image"));
981 if (ima == nullptr) {
982 return nullptr;
983 }
984
985 ImageUser iuser;
986 BKE_imageuser_default(&iuser);
988
989 PyObject *result = BPyGPUTexture_CreatePyObject(tex, true);
990 if (tex) {
991 GPU_texture_free(tex);
992 }
993 return result;
994}
995
996static PyMethodDef pygpu_texture__m_methods[] = {
997 {"from_image",
998 static_cast<PyCFunction>(pygpu_texture_from_image),
999 METH_O,
1000 pygpu_texture_from_image_doc},
1001 {nullptr, nullptr, 0, nullptr},
1002};
1003
1005 /* Wrap. */
1006 pygpu_texture__m_doc,
1007 "This module provides utilities for textures.");
1008static PyModuleDef pygpu_texture_module_def = {
1009 /*m_base*/ PyModuleDef_HEAD_INIT,
1010 /*m_name*/ "gpu.texture",
1011 /*m_doc*/ pygpu_texture__m_doc,
1012 /*m_size*/ 0,
1013 /*m_methods*/ pygpu_texture__m_methods,
1014 /*m_slots*/ nullptr,
1015 /*m_traverse*/ nullptr,
1016 /*m_clear*/ nullptr,
1017 /*m_free*/ nullptr,
1018};
1019
1021
1022/* -------------------------------------------------------------------- */
1025
1026int bpygpu_ParseTexture(PyObject *o, void *p)
1027{
1028 if (o == Py_None) {
1029 *static_cast<gpu::Texture **>(p) = nullptr;
1030 return 1;
1031 }
1032
1033 if (!BPyGPUTexture_Check(o)) {
1034 PyErr_Format(
1035 PyExc_ValueError, "expected a texture or None object, got %s", Py_TYPE(o)->tp_name);
1036 return 0;
1037 }
1038
1039 if (pygpu_texture_valid_check((BPyGPUTexture *)o) == -1) [[unlikely]] {
1040 return 0;
1041 }
1042
1043 *static_cast<gpu::Texture **>(p) = (reinterpret_cast<BPyGPUTexture *>(o))->tex;
1044 return 1;
1045}
1046
1048{
1049 PyObject *submodule;
1050 submodule = PyModule_Create(&pygpu_texture_module_def);
1051
1052 return submodule;
1053}
1054
1056
1057/* -------------------------------------------------------------------- */
1060
1061PyObject *BPyGPUTexture_CreatePyObject(gpu::Texture *tex, bool shared_reference)
1062{
1063 BPyGPUTexture *self;
1064
1065 if (shared_reference) {
1066#ifndef GPU_NO_USE_PY_REFERENCES
1067 void **ref = GPU_texture_py_reference_get(tex);
1068 if (ref) {
1069 /* Retrieve BPyGPUTexture reference. */
1070 self = reinterpret_cast<BPyGPUTexture *> POINTER_OFFSET(ref, -offsetof(BPyGPUTexture, tex));
1071 BLI_assert(self->tex == tex);
1072 Py_INCREF(self);
1073 return reinterpret_cast<PyObject *>(self);
1074 }
1075#endif
1076
1077 GPU_texture_ref(tex);
1078 }
1079
1080 self = PyObject_New(BPyGPUTexture, &BPyGPUTexture_Type);
1081 self->tex = tex;
1082
1083#ifndef GPU_NO_USE_PY_REFERENCES
1085 GPU_texture_py_reference_set(tex, reinterpret_cast<void **>(&self->tex));
1086#endif
1087
1088 return reinterpret_cast<PyObject *>(self);
1089}
1090
1092
1093#undef BPYGPU_TEXTURE_CHECK_OBJ
1094
1095} // namespace blender
#define STRNCPY_UTF8(dst, src)
PyDoc_STRVAR(FalseBP1D___doc__, "Class hierarchy: :class:`freestyle.types.BinaryPredicate1D` > :class:`FalseBP1D`\n" "\n" ".. method:: __call__(inter1, inter2)\n" "\n" " Always returns false.\n" "\n" " :param inter1: The first Interface1D object.\n" " :type inter1: :class:`freestyle.types.Interface1D`\n" " :param inter2: The second Interface1D object.\n" " :type inter2: :class:`freestyle.types.Interface1D`\n" " :return: False.\n" " :rtype: bool\n")
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define offsetof(t, d)
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
#define PYDOC_DATAFORMAT_LITERAL
Definition gpu_py.hh:34
#define BPYGPU_USE_GPUOBJ_FREE_METHOD
#define PYDOC_TEX_FORMAT_LITERAL
#define BPYGPU_TEXTURE_EXTEND_MODE_ARG_DOC
#define BPYGPU_TEXTURE_CHECK_OBJ(bpygpu)
#define BPyGPUTexture_Check(v)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
PyTypeObject BPyGPU_BufferType
PyC_StringEnumItems bpygpu_dataformat_items[]
Definition gpu_py.cc:42
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
gpu::Texture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
int GPU_texture_depth(const gpu::Texture *texture)
static PyObject * pygpu_texture_extend_mode(BPyGPUTexture *self, PyObject *value)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
int PyC_ParseBool(PyObject *o, void *p)
static PyObject * pygpu_texture_format_get(BPyGPUTexture *self, void *)
static PyObject * pygpu_texture_height_get(BPyGPUTexture *self, void *)
int bpygpu_ParseTexture(PyObject *o, void *p)
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
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)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
static int pygpu_texture_valid_check(BPyGPUTexture *bpygpu_tex)
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
constexpr int GPU_DEPTH24_STENCIL8_DEPRECATED
MINLINE int max_ii(int a, int b)
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
int GPU_max_texture_size()
static PyObject * pygpu_texture_extend_mode_x(BPyGPUTexture *self, PyObject *value)
static PyObject * pygpu_texture_from_image(PyObject *, PyObject *arg)
static PyMethodDef pygpu_texture__tp_methods[]
void ** GPU_texture_py_reference_get(gpu::Texture *texture)
int GPU_texture_dimensions(const gpu::Texture *texture)
Global G
Definition blender.cc:57
static bool pygpu_texture_size_check(const int size[3], const int len, const bool is_cubemap, char *err_out, const size_t err_out_size)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
size_t bpygpu_Buffer_size(BPyGPUBuffer *buffer)
@ GPU_TEXTURE_USAGE_GENERAL
BMesh const char void * data
static PyObject * pygpu_texture_read(BPyGPUTexture *self)
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
static PyMethodDef pygpu_texture__m_methods[]
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
int GPU_texture_width(const gpu::Texture *texture)
static PyObject * pygpu_texture_clear(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
static PyObject * pygpu_texture__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
size_t GPU_texture_component_len(gpu::TextureFormat format)
PyObject * bpygpu_texture_init()
const char * PyC_StringEnum_FindIDFromValue(const PyC_StringEnumItems *items, const int value)
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
static PyGetSetDef pygpu_texture__tp_getseters[]
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
const PyC_StringEnumItems pygpu_textureformat_items[]
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
PyTypeObject BPyGPUTexture_Type
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
constexpr int GPU_DEPTH_COMPONENT24_DEPRECATED
BPyGPUBuffer * BPyGPU_Buffer_CreatePyObject(const int format, const Py_ssize_t *shape, const int shape_len, void *buffer)
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
static PyObject * pygpu_texture_filter_mode(BPyGPUTexture *self, PyObject *value)
static PyModuleDef pygpu_texture_module_def
static PyObject * pygpu_texture_width_get(BPyGPUTexture *self, void *)
gpu::Texture * BKE_image_acquire_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:732
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
static PyObject * pygpu_texture_extend_mode_y(BPyGPUTexture *self, PyObject *value)
int PyC_ParseStringEnum(PyObject *o, void *p)
int GPU_max_texture_3d_size()
void BKE_imageuser_default(ImageUser *iuser)
int PyC_ParseTypeOrNone(PyObject *o, void *p)
void GPU_texture_py_reference_set(gpu::Texture *texture, void **py_ref)
static PyObject * pygpu_texture_anisotropic_filter(BPyGPUTexture *self, PyObject *value)
static PyObject * pygpu_texture_mipmap_mode(BPyGPUTexture *self, PyObject *args, PyObject *kwds)
const PyC_StringEnumItems pygpu_textureextendmode_items[]
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
GPUContext * GPU_context_active_get()
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
PyObject * BPyGPUTexture_CreatePyObject(gpu::Texture *tex, bool shared_reference)
static void BPyGPUTexture__tp_dealloc(BPyGPUTexture *self)
void GPU_texture_ref(gpu::Texture *texture)
#define PyC_TYPE_OR_NONE_INIT(py_type, value_p)
header-only compatibility defines.
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
union blender::BPyGPUBuffer::@121160014351146313222050217274041240110051113162 buf
PyObject_HEAD gpu::Texture * tex