Blender V5.3
gpu_py_framebuffer.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
14
15#include <Python.h>
16
17#include "GPU_context.hh"
18#include "GPU_framebuffer.hh"
19#include "GPU_init_exit.hh"
20
22#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
24
26
27#include "gpu_py.hh"
28#include "gpu_py_buffer.hh"
29#include "gpu_py_framebuffer.hh" /* own include */
30#include "gpu_py_texture.hh"
31
32namespace blender {
33
34/* -------------------------------------------------------------------- */
37
39{
40 if (bpygpu_fb->fb == nullptr) [[unlikely]] {
41 PyErr_SetString(PyExc_ReferenceError, "GPU framebuffer was freed, no further access is valid");
42 return -1;
43 }
44 return 0;
45}
46
47#define PYGPU_FRAMEBUFFER_CHECK_OBJ(bpygpu) \
48 { \
49 if (pygpu_framebuffer_valid_check(bpygpu) == -1) [[unlikely]] { \
50 return nullptr; \
51 } \
52 } \
53 ((void)0)
54
56{
57 if (GPU_is_init()) {
59 }
60 else {
61 printf("PyFramebuffer freed after the context has been destroyed.\n");
62 }
63}
64
66{
67 if (self->fb) {
68#ifndef GPU_NO_USE_PY_REFERENCES
70 if (!self->shared_reference)
71#endif
72 {
74 }
75
76 self->fb = nullptr;
77 }
78}
79
80/* Keep less than or equal to #FRAMEBUFFER_STACK_DEPTH */
81#define GPU_PY_FRAMEBUFFER_STACK_LEN 16
82
84{
86 PyErr_SetString(
87 PyExc_RuntimeError,
88 "Maximum framebuffer stack depth " STRINGIFY(GPU_PY_FRAMEBUFFER_STACK_LEN) " reached");
89 return false;
90 }
93 return true;
94}
95
97{
99 PyErr_SetString(PyExc_RuntimeError, "Minimum framebuffer stack depth reached");
100 return false;
101 }
102
103 if (fb && !GPU_framebuffer_bound(fb)) {
104 PyErr_SetString(PyExc_RuntimeError, "Framebuffer is not bound");
105 return false;
106 }
107
109 GPU_framebuffer_bind(fb_prev);
110 return true;
111}
112
114
115/* -------------------------------------------------------------------- */
121
123 PyObject_HEAD /* Required Python macro. */
125 int level;
126};
127
129{
130 Py_DECREF(self->py_fb);
131 PyObject_DEL(self);
132}
133
135{
137
138 /* sanity - should never happen */
139 if (self->level != -1) {
140 PyErr_SetString(PyExc_RuntimeError, "Already in use");
141 return nullptr;
142 }
143
145 return nullptr;
146 }
147
149 Py_RETURN_NONE;
150}
151
153 PyObject * /*args*/)
154{
156
157 /* sanity - should never happen */
158 if (self->level == -1) {
159 fprintf(stderr, "Not yet in use\n");
160 return nullptr;
161 }
162
163 const int level = GPU_framebuffer_stack_level_get();
164 if (level != self->level) {
165 fprintf(stderr, "Level of bind mismatch, expected %d, got %d\n", self->level, level);
166 }
167
169 return nullptr;
170 }
171 Py_RETURN_NONE;
172}
173
174#ifdef __GNUC__
175# ifdef __clang__
176# pragma clang diagnostic push
177# pragma clang diagnostic ignored "-Wcast-function-type"
178# else
179# pragma GCC diagnostic push
180# pragma GCC diagnostic ignored "-Wcast-function-type"
181# endif
182#endif
183
185 {"__enter__",
186 reinterpret_cast<PyCFunction>(pygpu_framebuffer_stack_context_enter),
187 METH_NOARGS},
188 {"__exit__",
189 reinterpret_cast<PyCFunction>(pygpu_framebuffer_stack_context_exit),
190 METH_VARARGS},
191 {nullptr},
192};
193
194#ifdef __GNUC__
195# ifdef __clang__
196# pragma clang diagnostic pop
197# else
198# pragma GCC diagnostic pop
199# endif
200#endif
201
202static PyTypeObject FramebufferStackContext_Type = {
203 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
204 /*tp_name*/ "GPUFrameBufferStackContext",
205 /*tp_basicsize*/ sizeof(PyFrameBufferStackContext),
206 /*tp_itemsize*/ 0,
207 /*tp_dealloc*/ reinterpret_cast<destructor>(pygpu_framebuffer_stack_context__tp_dealloc),
208 /*tp_vectorcall_offset*/ 0,
209 /*tp_getattr*/ nullptr,
210 /*tp_setattr*/ nullptr,
211 /*tp_as_async*/ nullptr,
212 /*tp_repr*/ nullptr,
213 /*tp_as_number*/ nullptr,
214 /*tp_as_sequence*/ nullptr,
215 /*tp_as_mapping*/ nullptr,
216 /*tp_hash*/ nullptr,
217 /*tp_call*/ nullptr,
218 /*tp_str*/ nullptr,
219 /*tp_getattro*/ nullptr,
220 /*tp_setattro*/ nullptr,
221 /*tp_as_buffer*/ nullptr,
222 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
223 /*tp_doc*/ nullptr,
224 /*tp_traverse*/ nullptr,
225 /*tp_clear*/ nullptr,
226 /*tp_richcompare*/ nullptr,
227 /*tp_weaklistoffset*/ 0,
228 /*tp_iter*/ nullptr,
229 /*tp_iternext*/ nullptr,
231 /*tp_members*/ nullptr,
232 /*tp_getset*/ nullptr,
233 /*tp_base*/ nullptr,
234 /*tp_dict*/ nullptr,
235 /*tp_descr_get*/ nullptr,
236 /*tp_descr_set*/ nullptr,
237 /*tp_dictoffset*/ 0,
238 /*tp_init*/ nullptr,
239 /*tp_alloc*/ nullptr,
240 /*tp_new*/ nullptr,
241 /*tp_free*/ nullptr,
242 /*tp_is_gc*/ nullptr,
243 /*tp_bases*/ nullptr,
244 /*tp_mro*/ nullptr,
245 /*tp_cache*/ nullptr,
246 /*tp_subclasses*/ nullptr,
247 /*tp_weaklist*/ nullptr,
248 /*tp_del*/ nullptr,
249 /*tp_version_tag*/ 0,
250 /*tp_finalize*/ nullptr,
251 /*tp_vectorcall*/ nullptr,
252};
253
255 /* Wrap. */
256 pygpu_framebuffer_bind_doc,
257 ".. method:: bind()\n"
258 "\n"
259 " Context manager to ensure balanced bind calls, even in the case of an error.\n");
261{
264 ret->py_fb = self;
265 ret->level = -1;
266 Py_INCREF(self);
267 return reinterpret_cast<PyObject *>(ret);
268}
269
271
272/* -------------------------------------------------------------------- */
275
276/* Fill in the GPUAttachment according to the PyObject parameter.
277 * PyObject *o can be nullptr, Py_None, BPyGPUTexture or a dictionary containing the keyword
278 * "texture" and the optional keywords "layer" and "mip". Returns false on error. In this case, a
279 * python message will be raised and GPUAttachment will not be touched. */
280static bool pygpu_framebuffer_new_parse_arg(PyObject *o, GPUAttachment *r_attach)
281{
283
284 if (!o || o == Py_None) {
285 /* Pass. */;
286 }
287 else if (BPyGPUTexture_Check(o)) {
288 if (!bpygpu_ParseTexture(o, &tmp_attach.tex)) {
289 return false;
290 }
291 }
292 else {
293 const char *c_texture = "texture";
294 const char *c_layer = "layer";
295 const char *c_mip = "mip";
296 PyObject *key, *value;
297 Py_ssize_t pos = 0;
298 while (PyDict_Next(o, &pos, &key, &value)) {
299 if (!PyUnicode_Check(key)) {
300 PyErr_SetString(PyExc_TypeError, "keywords must be strings");
301 return false;
302 }
303
304 if (c_texture && PyUnicode_CompareWithASCIIString(key, c_texture) == 0) {
305 /* Compare only once. */
306 c_texture = nullptr;
307 if (!bpygpu_ParseTexture(value, &tmp_attach.tex)) {
308 return false;
309 }
310 }
311 else if (c_layer && PyUnicode_CompareWithASCIIString(key, c_layer) == 0) {
312 /* Compare only once. */
313 c_layer = nullptr;
314 tmp_attach.layer = PyLong_AsLong(value);
315 if (tmp_attach.layer == -1 && PyErr_Occurred()) {
316 return false;
317 }
318 }
319 else if (c_mip && PyUnicode_CompareWithASCIIString(key, c_mip) == 0) {
320 /* Compare only once. */
321 c_mip = nullptr;
322 tmp_attach.mip = PyLong_AsLong(value);
323 if (tmp_attach.mip == -1 && PyErr_Occurred()) {
324 return false;
325 }
326 }
327 else {
328 PyErr_Format(
329 PyExc_TypeError, "'%U' is an invalid keyword argument for this attribute", key);
330 return false;
331 }
332 }
333 }
334
335 *r_attach = tmp_attach;
336 return true;
337}
338
339static PyObject *pygpu_framebuffer__tp_new(PyTypeObject * /*self*/, PyObject *args, PyObject *kwds)
340{
342
343 if (!GPU_context_active_get()) {
344 PyErr_SetString(PyExc_RuntimeError, "No active GPU context found");
345 return nullptr;
346 }
347
348 PyObject *depth_attachment = nullptr;
349 PyObject *color_attachements = nullptr;
350 static const char *_keywords[] = {"depth_slot", "color_slots", nullptr};
351 static _PyArg_Parser _parser = {
352 "|$" /* Optional, keyword only arguments. */
353 "O" /* `depth_slot` */
354 "O" /* `color_slots` */
355 ":GPUFrameBuffer.__new__",
356 _keywords,
357 nullptr,
358 };
359 if (!_PyArg_ParseTupleAndKeywordsFast(
360 args, kwds, &_parser, &depth_attachment, &color_attachements))
361 {
362 return nullptr;
363 }
364
365/* Keep in sync with #GPU_FB_MAX_COLOR_ATTACHMENT.
366 * TODO: share the define. */
367#define BPYGPU_FB_MAX_COLOR_ATTACHMENT 6
368
370
371 if (!pygpu_framebuffer_new_parse_arg(depth_attachment, &config[0])) {
372 return nullptr;
373 }
374 if (config[0].tex && !GPU_texture_has_depth_format(config[0].tex)) {
375 PyErr_SetString(PyExc_ValueError, "Depth texture with incompatible format");
376 return nullptr;
377 }
378
379 int color_attachements_len = 0;
380 if (color_attachements && color_attachements != Py_None) {
381 if (PySequence_Check(color_attachements)) {
382 color_attachements_len = PySequence_Size(color_attachements);
383 if (color_attachements_len > BPYGPU_FB_MAX_COLOR_ATTACHMENT) {
384 PyErr_SetString(PyExc_AttributeError,
385 "too many attachments, max is " STRINGIFY(BPYGPU_FB_MAX_COLOR_ATTACHMENT));
386 return nullptr;
387 }
388
389 for (int i = 0; i < color_attachements_len; i++) {
390 PyObject *o = PySequence_GetItem(color_attachements, i);
391 bool ok = pygpu_framebuffer_new_parse_arg(o, &config[i + 1]);
392 Py_DECREF(o);
393 if (!ok) {
394 return nullptr;
395 }
396 }
397 }
398 else {
399 if (!pygpu_framebuffer_new_parse_arg(color_attachements, &config[1])) {
400 return nullptr;
401 }
402 color_attachements_len = 1;
403 }
404 }
405
406 gpu::FrameBuffer *fb_python = GPU_framebuffer_create("fb_python");
407 GPU_framebuffer_config_array(fb_python, config, color_attachements_len + 1);
408
409 return BPyGPUFrameBuffer_CreatePyObject(fb_python, false);
410}
411
413 /* Wrap. */
414 pygpu_framebuffer_is_bound_doc,
415 "Checks if this is the active frame-buffer in the context.\n"
416 "\n"
417 ":type: bool\n");
418static PyObject *pygpu_framebuffer_is_bound(BPyGPUFrameBuffer *self, void * /*type*/)
419{
421 return PyBool_FromLong(GPU_framebuffer_bound(self->fb));
422}
423
425 /* Wrap. */
426 pygpu_framebuffer_clear_doc,
427 ".. method:: clear(*, color=None, depth=None, stencil=None)\n"
428 "\n"
429 " Fill color, depth and stencil textures with specific value.\n"
430 " Common values: color=(0.0, 0.0, 0.0, 1.0), depth=1.0, stencil=0.\n"
431 "\n"
432 " :param color: Sequence of 3 or 4 floats representing ``(r, g, b, a)``.\n"
433 " :type color: Sequence[float] | None\n"
434 " :param depth: depth value.\n"
435 " :type depth: float | None\n"
436 " :param stencil: stencil value.\n"
437 " :type stencil: int | None\n");
438static PyObject *pygpu_framebuffer_clear(BPyGPUFrameBuffer *self, PyObject *args, PyObject *kwds)
439{
441
442 if (!GPU_framebuffer_bound(self->fb)) {
443 return nullptr;
444 }
445
446 PyObject *py_col = nullptr;
447 PyObject *py_depth = nullptr;
448 PyObject *py_stencil = nullptr;
449
450 static const char *_keywords[] = {"color", "depth", "stencil", nullptr};
451 static _PyArg_Parser _parser = {
452 "|$" /* Optional, keyword only arguments. */
453 "O" /* `color` */
454 "O" /* `depth` */
455 "O" /* `stencil` */
456 ":clear",
457 _keywords,
458 nullptr,
459 };
460 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &py_col, &py_depth, &py_stencil)) {
461 return nullptr;
462 }
463
465 float col[4] = {0.0f, 0.0f, 0.0f, 1.0f};
466 float depth = 1.0f;
467 uint stencil = 0;
468
469 if (py_col && py_col != Py_None) {
471 col, 3, 4, py_col, "gpu::FrameBuffer.clear(), invalid 'color' arg") == -1)
472 {
473 return nullptr;
474 }
475 buffers |= GPU_COLOR_BIT;
476 }
477
478 if (py_depth && py_depth != Py_None) {
479 depth = PyFloat_AsDouble(py_depth);
480 if (PyErr_Occurred()) {
481 return nullptr;
482 }
483 buffers |= GPU_DEPTH_BIT;
484 }
485
486 if (py_stencil && py_stencil != Py_None) {
487 if ((stencil = PyC_Long_AsU32(py_stencil)) == uint(-1)) {
488 return nullptr;
489 }
490 buffers |= GPU_STENCIL_BIT;
491 }
492
493 GPU_framebuffer_clear(self->fb, buffers, double4(UNPACK4(col)), depth, stencil);
494 Py_RETURN_NONE;
495}
496
498 /* Wrap. */
499 pygpu_framebuffer_viewport_set_doc,
500 ".. method:: viewport_set(x, y, xsize, ysize)\n"
501 "\n"
502 " Set the viewport for this framebuffer object.\n"
503 " Note: The viewport state is not saved upon framebuffer rebind.\n"
504 "\n"
505 " :param x: Lower left corner x of the viewport rectangle, in pixels.\n"
506 " :type x: int\n"
507 " :param y: Lower left corner y of the viewport rectangle, in pixels.\n"
508 " :type y: int\n"
509 " :param xsize: Width of the viewport.\n"
510 " :type xsize: int\n"
511 " :param ysize: Height of the viewport.\n"
512 " :type ysize: int\n");
513static PyObject *pygpu_framebuffer_viewport_set(BPyGPUFrameBuffer *self, PyObject *args)
514{
515 int x, y, xsize, ysize;
516 if (!PyArg_ParseTuple(args,
517 "i" /* `x` */
518 "i" /* `y` */
519 "i" /* `xsize` */
520 "i" /* `ysize` */
521 ":viewport_set",
522 &x,
523 &y,
524 &xsize,
525 &ysize))
526 {
527 return nullptr;
528 }
529
530 GPU_framebuffer_viewport_set(self->fb, x, y, xsize, ysize);
531 Py_RETURN_NONE;
532}
533
535 /* Wrap. */
536 pygpu_framebuffer_viewport_get_doc,
537 ".. method:: viewport_get()\n"
538 "\n"
539 " Returns position and dimension to current viewport.\n"
540 "\n"
541 " :return: The viewport as ``(x, y, width, height)``.\n"
542 " :rtype: tuple[int, int, int, int]\n");
544{
546 int viewport[4];
547 GPU_framebuffer_viewport_get(self->fb, viewport);
548
549 PyObject *ret = PyTuple_New(4);
551 PyLong_FromLong(viewport[0]),
552 PyLong_FromLong(viewport[1]),
553 PyLong_FromLong(viewport[2]),
554 PyLong_FromLong(viewport[3]));
555 return ret;
556}
557
559 /* Wrap. */
560 pygpu_framebuffer_read_color_doc,
561 ".. method:: read_color(x, y, xsize, ysize, channels, slot, format, *, data=None)\n"
562 "\n"
563 " Read a block of pixels from the frame buffer.\n"
564 "\n"
565 " :param x: Lower left corner x of a rectangular block of pixels.\n"
566 " :type x: int\n"
567 " :param y: Lower left corner y of a rectangular block of pixels.\n"
568 " :type y: int\n"
569 " :param xsize: Width of the pixel rectangle.\n"
570 " :type xsize: int\n"
571 " :param ysize: Height of the pixel rectangle.\n"
572 " :type ysize: int\n"
573 " :param channels: Number of components to read.\n"
574 " :type channels: int\n"
575 " :param slot: The framebuffer slot to read data from.\n"
576 " :type slot: int\n"
577 " :param format: The format that describes the content of a single channel.\n"
578 " ``UINT_24_8`` is deprecated, use ``FLOAT`` instead.\n"
579 " :type format: " PYDOC_DATAFORMAT_LITERAL
580 "\n"
581 " :param data: Optional Buffer object to fill with the pixels values.\n"
582 " :type data: :class:`gpu.types.Buffer` | None\n"
583 " :return: The Buffer with the read pixels.\n"
584 " :rtype: :class:`gpu.types.Buffer`\n");
586 PyObject *args,
587 PyObject *kwds)
588{
590 int x, y, w, h, channels;
591 uint slot;
592 PyC_StringEnum pygpu_dataformat = {bpygpu_dataformat_items,
593 int(gpu::TextureFormat::UNORM_8_8_8_8)};
594 BPyGPUBuffer *py_buffer = nullptr;
595 PyC_TypeOrNone py_buffer_or_none = PyC_TYPE_OR_NONE_INIT(&BPyGPU_BufferType, &py_buffer);
596
597 static const char *_keywords[] = {
598 "x", "y", "xsize", "ysize", "channels", "slot", "format", "data", nullptr};
599 static _PyArg_Parser _parser = {
600 "i" /* `x` */
601 "i" /* `y` */
602 "i" /* `xsize` */
603 "i" /* `ysize` */
604 "i" /* `channels` */
605 "I" /* `slot` */
606 "O&" /* `format` */
607 "|$" /* Optional, keyword only arguments. */
608 "O&" /* `data` */
609 ":read_color",
610 _keywords,
611 nullptr,
612 };
613 if (!_PyArg_ParseTupleAndKeywordsFast(args,
614 kwds,
615 &_parser,
616 &x,
617 &y,
618 &w,
619 &h,
620 &channels,
621 &slot,
623 &pygpu_dataformat,
625 &py_buffer_or_none))
626 {
627 return nullptr;
628 }
629
630 if (pygpu_dataformat.value_found == GPU_DATA_UINT_24_8_DEPRECATED) {
631 PyErr_WarnEx(PyExc_DeprecationWarning, "`UINT_24_8` is deprecated, use `FLOAT` instead", 1);
632 }
633
634 if (!IN_RANGE_INCL(channels, 1, 4)) {
635 PyErr_SetString(PyExc_AttributeError, "Color channels must be 1, 2, 3 or 4");
636 return nullptr;
637 }
638
639 if (slot >= BPYGPU_FB_MAX_COLOR_ATTACHMENT) {
640 PyErr_SetString(PyExc_ValueError, "slot overflow");
641 return nullptr;
642 }
643
644 int2 extent = GPU_framebuffer_extent_get(self->fb);
645 if (x < 0 || w < 0 || x + w > extent.x || y < 0 || h < 0 || y + h > extent.y) {
646 PyErr_SetString(PyExc_ValueError,
647 "Trying to read color outside the extent of the framebuffer");
648 return nullptr;
649 }
650
651 if (py_buffer) {
652 if (pygpu_dataformat.value_found != py_buffer->format) {
653 PyErr_SetString(PyExc_AttributeError,
654 "the format of the buffer is different from that specified");
655 return nullptr;
656 }
657
658 size_t size_curr = bpygpu_Buffer_size(py_buffer);
659 size_t size_expected = w * h * channels *
661 eGPUDataFormat(pygpu_dataformat.value_found));
662 if (size_curr < size_expected) {
663 PyErr_SetString(PyExc_BufferError, "the buffer size is smaller than expected");
664 return nullptr;
665 }
666 Py_INCREF(py_buffer);
667 }
668 else {
669 const Py_ssize_t shape[3] = {h, w, channels};
670 py_buffer = BPyGPU_Buffer_CreatePyObject(pygpu_dataformat.value_found, shape, 3, nullptr);
671 BLI_assert(bpygpu_Buffer_size(py_buffer) ==
672 size_t(w) * size_t(h) * size_t(channels) *
674 }
675
677 x,
678 y,
679 w,
680 h,
681 channels,
682 int(slot),
683 eGPUDataFormat(pygpu_dataformat.value_found),
684 py_buffer->buf.as_void);
685
686 return reinterpret_cast<PyObject *>(py_buffer);
687}
688
690 /* Wrap. */
691 pygpu_framebuffer_read_depth_doc,
692 ".. method:: read_depth(x, y, xsize, ysize, *, data=None)\n"
693 "\n"
694 " Read a pixel depth block from the frame buffer.\n"
695 "\n"
696 " :param x: Lower left corner x of a rectangular block of pixels.\n"
697 " :type x: int\n"
698 " :param y: Lower left corner y of a rectangular block of pixels.\n"
699 " :type y: int\n"
700 " :param xsize: Width of the pixel rectangle.\n"
701 " :type xsize: int\n"
702 " :param ysize: Height of the pixel rectangle.\n"
703 " :type ysize: int\n"
704 " :param data: Optional Buffer object to fill with the pixels values.\n"
705 " :type data: :class:`gpu.types.Buffer` | None\n"
706 " :return: The Buffer with the read pixels.\n"
707 " :rtype: :class:`gpu.types.Buffer`\n");
709 PyObject *args,
710 PyObject *kwds)
711{
713 int x, y, w, h;
714 BPyGPUBuffer *py_buffer = nullptr;
715 PyC_TypeOrNone py_buffer_or_none = PyC_TYPE_OR_NONE_INIT(&BPyGPU_BufferType, &py_buffer);
716
717 static const char *_keywords[] = {"x", "y", "xsize", "ysize", "data", nullptr};
718 static _PyArg_Parser _parser = {
719 "i" /* `x` */
720 "i" /* `y` */
721 "i" /* `xsize` */
722 "i" /* `ysize` */
723 "|$" /* Optional, keyword only arguments. */
724 "O&" /* `data` */
725 ":read_depth",
726 _keywords,
727 nullptr,
728 };
729 if (!_PyArg_ParseTupleAndKeywordsFast(
730 args, kwds, &_parser, &x, &y, &w, &h, PyC_ParseTypeOrNone, &py_buffer_or_none))
731 {
732 return nullptr;
733 }
734
735 int2 extent = GPU_framebuffer_extent_get(self->fb);
736 if (x < 0 || w < 0 || x + w > extent.x || y < 0 || h < 0 || y + h > extent.y) {
737 PyErr_SetString(PyExc_ValueError,
738 "Trying to read depth outside the extent of the framebuffer");
739 return nullptr;
740 }
741
742 if (py_buffer) {
743 if (py_buffer->format != GPU_DATA_FLOAT) {
744 PyErr_SetString(PyExc_AttributeError, "the format of the buffer must be 'GPU_DATA_FLOAT'");
745 return nullptr;
746 }
747
748 size_t size_curr = bpygpu_Buffer_size(py_buffer);
749 size_t size_expected = w * h * GPU_texture_dataformat_size(GPU_DATA_FLOAT);
750 if (size_curr < size_expected) {
751 PyErr_SetString(PyExc_BufferError, "the buffer size is smaller than expected");
752 return nullptr;
753 }
754 Py_INCREF(py_buffer);
755 }
756 else {
757 const Py_ssize_t shape[2] = {h, w};
758 py_buffer = BPyGPU_Buffer_CreatePyObject(GPU_DATA_FLOAT, shape, 2, nullptr);
759 BLI_assert(bpygpu_Buffer_size(py_buffer) ==
761 }
762
763 GPU_framebuffer_read_depth(self->fb, x, y, w, h, GPU_DATA_FLOAT, py_buffer->buf.as_void);
764
765 return reinterpret_cast<PyObject *>(py_buffer);
766}
767
768#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
770 /* Wrap. */
771 pygpu_framebuffer_free_doc,
772 ".. method:: free()\n"
773 "\n"
774 " Free the framebuffer object.\n"
775 " The framebuffer will no longer be accessible.\n");
776static PyObject *pygpu_framebuffer_free(BPyGPUFrameBuffer *self)
777{
780 Py_RETURN_NONE;
781}
782#endif
783
785{
787 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
788}
789
790static PyGetSetDef pygpu_framebuffer__tp_getseters[] = {
791 {"is_bound",
792 reinterpret_cast<getter>(pygpu_framebuffer_is_bound),
793 static_cast<setter>(nullptr),
794 pygpu_framebuffer_is_bound_doc,
795 nullptr},
796 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
797};
798
799#ifdef __GNUC__
800# ifdef __clang__
801# pragma clang diagnostic push
802# pragma clang diagnostic ignored "-Wcast-function-type"
803# else
804# pragma GCC diagnostic push
805# pragma GCC diagnostic ignored "-Wcast-function-type"
806# endif
807#endif
808
809static PyMethodDef pygpu_framebuffer__tp_methods[] = {
810 {"bind",
811 reinterpret_cast<PyCFunction>(pygpu_framebuffer_bind),
812 METH_NOARGS,
813 pygpu_framebuffer_bind_doc},
814 {"clear",
815 reinterpret_cast<PyCFunction>(pygpu_framebuffer_clear),
816 METH_VARARGS | METH_KEYWORDS,
817 pygpu_framebuffer_clear_doc},
818 {"viewport_set",
819 reinterpret_cast<PyCFunction>(pygpu_framebuffer_viewport_set),
820 METH_VARARGS,
821 pygpu_framebuffer_viewport_set_doc},
822 {"viewport_get",
823 reinterpret_cast<PyCFunction>(pygpu_framebuffer_viewport_get),
824 METH_NOARGS,
825 pygpu_framebuffer_viewport_get_doc},
826 {"read_color",
827 reinterpret_cast<PyCFunction>(pygpu_framebuffer_read_color),
828 METH_VARARGS | METH_KEYWORDS,
829 pygpu_framebuffer_read_color_doc},
830 {"read_depth",
831 reinterpret_cast<PyCFunction>(pygpu_framebuffer_read_depth),
832 METH_VARARGS | METH_KEYWORDS,
833 pygpu_framebuffer_read_depth_doc},
834#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
835 {"free", (PyCFunction)pygpu_framebuffer_free, METH_NOARGS, pygpu_framebuffer_free_doc},
836#endif
837 {nullptr, nullptr, 0, nullptr},
838};
839
840#ifdef __GNUC__
841# ifdef __clang__
842# pragma clang diagnostic pop
843# else
844# pragma GCC diagnostic pop
845# endif
846#endif
847
848/* Ideally type aliases would de-duplicate:
849 * `GPUTexture | dict[str, int | GPUTexture]` in this docstring. */
851 /* Wrap. */
852 pygpu_framebuffer__tp_doc,
853 ".. class:: GPUFrameBuffer\n"
854 "\n"
855 " This object gives access to framebuffer functionalities.\n"
856 " When a 'layer' is specified in a argument, a single layer of a 3D or array "
857 "texture is attached to the frame-buffer.\n"
858 " For cube map textures, layer is translated into a cube map face.\n"
859 "\n"
860 " .. method:: __init__(*, depth_slot=None, color_slots=None)\n"
861 "\n"
862 " :param depth_slot: GPUTexture to attach or a ``dict`` containing keywords: "
863 "'texture', 'layer' and 'mip'.\n"
864 " :type depth_slot: :class:`gpu.types.GPUTexture` | dict[str, int | "
865 ":class:`gpu.types.GPUTexture`] | None\n"
866 " :param color_slots: Tuple where each item can be a GPUTexture or a ``dict`` "
867 "containing keywords: 'texture', 'layer' and 'mip'.\n"
868 " :type color_slots: :class:`gpu.types.GPUTexture` | "
869 "dict[str, int | :class:`gpu.types.GPUTexture`] | "
870 "Sequence[:class:`gpu.types.GPUTexture` | dict[str, int | "
871 ":class:`gpu.types.GPUTexture`]] | "
872 "None\n");
874 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
875 /*tp_name*/ "GPUFrameBuffer",
876 /*tp_basicsize*/ sizeof(BPyGPUFrameBuffer),
877 /*tp_itemsize*/ 0,
878 /*tp_dealloc*/ reinterpret_cast<destructor>(BPyGPUFrameBuffer__tp_dealloc),
879 /*tp_vectorcall_offset*/ 0,
880 /*tp_getattr*/ nullptr,
881 /*tp_setattr*/ nullptr,
882 /*tp_as_async*/ nullptr,
883 /*tp_repr*/ nullptr,
884 /*tp_as_number*/ nullptr,
885 /*tp_as_sequence*/ nullptr,
886 /*tp_as_mapping*/ nullptr,
887 /*tp_hash*/ nullptr,
888 /*tp_call*/ nullptr,
889 /*tp_str*/ nullptr,
890 /*tp_getattro*/ nullptr,
891 /*tp_setattro*/ nullptr,
892 /*tp_as_buffer*/ nullptr,
893 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
894 /*tp_doc*/ pygpu_framebuffer__tp_doc,
895 /*tp_traverse*/ nullptr,
896 /*tp_clear*/ nullptr,
897 /*tp_richcompare*/ nullptr,
898 /*tp_weaklistoffset*/ 0,
899 /*tp_iter*/ nullptr,
900 /*tp_iternext*/ nullptr,
901 /*tp_methods*/ pygpu_framebuffer__tp_methods,
902 /*tp_members*/ nullptr,
904 /*tp_base*/ nullptr,
905 /*tp_dict*/ nullptr,
906 /*tp_descr_get*/ nullptr,
907 /*tp_descr_set*/ nullptr,
908 /*tp_dictoffset*/ 0,
909 /*tp_init*/ nullptr,
910 /*tp_alloc*/ nullptr,
911 /*tp_new*/ pygpu_framebuffer__tp_new,
912 /*tp_free*/ nullptr,
913 /*tp_is_gc*/ nullptr,
914 /*tp_bases*/ nullptr,
915 /*tp_mro*/ nullptr,
916 /*tp_cache*/ nullptr,
917 /*tp_subclasses*/ nullptr,
918 /*tp_weaklist*/ nullptr,
919 /*tp_del*/ nullptr,
920 /*tp_version_tag*/ 0,
921 /*tp_finalize*/ nullptr,
922 /*tp_vectorcall*/ nullptr,
923};
924
926
927/* -------------------------------------------------------------------- */
930
931PyObject *BPyGPUFrameBuffer_CreatePyObject(gpu::FrameBuffer *fb, bool shared_reference)
932{
933 BPyGPUFrameBuffer *self;
934
935#ifndef GPU_NO_USE_PY_REFERENCES
936 if (shared_reference) {
938 if (ref) {
939 /* Retrieve BPyGPUFrameBuffer reference. */
940 self = reinterpret_cast<BPyGPUFrameBuffer *> POINTER_OFFSET(
942 BLI_assert(self->fb == fb);
943 Py_INCREF(self);
944 return reinterpret_cast<PyObject *>(self);
945 }
946 }
947#else
948 UNUSED_VARS(shared_reference);
949#endif
950
951 self = PyObject_New(BPyGPUFrameBuffer, &BPyGPUFrameBuffer_Type);
952 self->fb = fb;
953
954#ifndef GPU_NO_USE_PY_REFERENCES
955 self->shared_reference = shared_reference;
956
958 GPU_framebuffer_py_reference_set(fb, reinterpret_cast<void **>(&self->fb));
959#endif
960
961 return reinterpret_cast<PyObject *>(self);
962}
963
965
966#undef PYGPU_FRAMEBUFFER_CHECK_OBJ
967
968} // namespace blender
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 GPU_ATTACHMENT_NONE
#define UNPACK4(a)
#define UNUSED_VARS(...)
#define STRINGIFY(x)
#define POINTER_OFFSET(v, ofs)
#define IN_RANGE_INCL(a, b, c)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#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 PYGPU_FRAMEBUFFER_CHECK_OBJ(bpygpu)
#define BPYGPU_FB_MAX_COLOR_ATTACHMENT
#define GPU_PY_FRAMEBUFFER_STACK_LEN
#define BPyGPUTexture_Check(v)
BMesh Iterators.
PyTypeObject BPyGPU_BufferType
PyC_StringEnumItems bpygpu_dataformat_items[]
Definition gpu_py.cc:42
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
static PyMethodDef pygpu_framebuffer__tp_methods[]
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * pygpu_framebuffer_read_depth(BPyGPUFrameBuffer *self, PyObject *args, PyObject *kwds)
bool GPU_texture_has_depth_format(const gpu::Texture *texture)
gpu::FrameBuffer * GPU_framebuffer_active_get()
void GPU_framebuffer_read_color(gpu::FrameBuffer *fb, int x, int y, int width, int height, int channels, int slot, eGPUDataFormat data_format, void *r_data)
void GPU_framebuffer_clear(gpu::FrameBuffer *fb, GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, unsigned int clear_stencil)
static PyGetSetDef pygpu_framebuffer__tp_getseters[]
unsigned int uint
Definition BKE_text.h:55
void GPU_framebuffer_py_reference_set(gpu::FrameBuffer *fb, void **py_ref)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
void GPU_framebuffer_viewport_set(gpu::FrameBuffer *fb, int x, int y, int width, int height)
BLI_INLINE float fb(float length, float L)
int bpygpu_ParseTexture(PyObject *o, void *p)
static void BPyGPUFrameBuffer__tp_dealloc(BPyGPUFrameBuffer *self)
int2 GPU_framebuffer_extent_get(gpu::FrameBuffer *fb)
Get the extent of the framebuffer.
@ 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")
static PyMethodDef pygpu_framebuffer_stack_context__tp_methods[]
void GPU_framebuffer_push(gpu::FrameBuffer *fb)
static PyObject * pygpu_framebuffer_viewport_get(BPyGPUFrameBuffer *self)
static void pygpu_framebuffer_free_if_possible(gpu::FrameBuffer *fb)
static PyTypeObject FramebufferStackContext_Type
PyObject * BPyGPUFrameBuffer_CreatePyObject(gpu::FrameBuffer *fb, bool shared_reference)
static bool pygpu_framebuffer_stack_pop_and_restore_or_error(gpu::FrameBuffer *fb)
static int pygpu_framebuffer_valid_check(BPyGPUFrameBuffer *bpygpu_fb)
gpu::FrameBuffer * GPU_framebuffer_pop()
static PyObject * pygpu_framebuffer_bind(BPyGPUFrameBuffer *self)
static void pygpu_framebuffer_stack_context__tp_dealloc(PyFrameBufferStackContext *self)
static bool pygpu_framebuffer_stack_push_and_bind_or_error(gpu::FrameBuffer *fb)
size_t bpygpu_Buffer_size(BPyGPUBuffer *buffer)
static void pygpu_framebuffer_free_safe(BPyGPUFrameBuffer *self)
VecBase< int32_t, 2 > int2
size_t GPU_texture_dataformat_size(eGPUDataFormat data_format)
static PyObject * pygpu_framebuffer_is_bound(BPyGPUFrameBuffer *self, void *)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
void ** GPU_framebuffer_py_reference_get(gpu::FrameBuffer *fb)
void GPU_framebuffer_config_array(gpu::FrameBuffer *fb, const GPUAttachment *config, int config_len)
static bool pygpu_framebuffer_new_parse_arg(PyObject *o, GPUAttachment *r_attach)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
uint GPU_framebuffer_stack_level_get()
uint32_t PyC_Long_AsU32(PyObject *value)
static PyObject * pygpu_framebuffer_stack_context_enter(PyFrameBufferStackContext *self)
static PyObject * pygpu_framebuffer_read_color(BPyGPUFrameBuffer *self, PyObject *args, PyObject *kwds)
BPyGPUBuffer * BPyGPU_Buffer_CreatePyObject(const int format, const Py_ssize_t *shape, const int shape_len, void *buffer)
static PyObject * pygpu_framebuffer_clear(BPyGPUFrameBuffer *self, PyObject *args, PyObject *kwds)
PyTypeObject BPyGPUFrameBuffer_Type
static PyObject * pygpu_framebuffer_stack_context_exit(PyFrameBufferStackContext *self, PyObject *)
void GPU_framebuffer_viewport_get(gpu::FrameBuffer *fb, int r_viewport[4])
int PyC_ParseStringEnum(PyObject *o, void *p)
void GPU_framebuffer_read_depth(gpu::FrameBuffer *fb, int x, int y, int width, int height, eGPUDataFormat data_format, void *r_data)
static PyObject * pygpu_framebuffer_viewport_set(BPyGPUFrameBuffer *self, PyObject *args)
bool GPU_framebuffer_bound(gpu::FrameBuffer *fb)
int PyC_ParseTypeOrNone(PyObject *o, void *p)
VecBase< double, 4 > double4
bool GPU_is_init()
GPUContext * GPU_context_active_get()
static PyObject * pygpu_framebuffer__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
#define PyC_TYPE_OR_NONE_INIT(py_type, value_p)
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
union blender::BPyGPUBuffer::@121160014351146313222050217274041240110051113162 buf
PyObject_HEAD gpu::FrameBuffer * fb
PyObject_HEAD BPyGPUFrameBuffer * py_fb