Cleanup: remove redundant, invalid info from headers
[blender.git] / source / blender / python / gpu / gpu_py_offscreen.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2015, Blender Foundation.
17  */
18
19 /** \file blender/python/gpu/gpu_py_offscreen.c
20  *  \ingroup bpygpu
21  *
22  * This file defines the offscreen functionalities of the 'gpu' module
23  * used for off-screen OpenGL rendering.
24  *
25  * - Use ``bpygpu_`` for local API.
26  * - Use ``BPyGPU`` for public API.
27  */
28
29 #include <Python.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_utildefines.h"
34
35 #include "BKE_library.h"
36 #include "BKE_scene.h"
37
38 #include "DNA_screen_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_view3d_types.h"
41
42 #include "GPU_framebuffer.h"
43 #include "GPU_texture.h"
44
45 #include "../editors/include/ED_view3d.h"
46
47 #include "../mathutils/mathutils.h"
48
49 #include "../generic/py_capi_utils.h"
50
51 #include "gpu_py_api.h"
52 #include "gpu_py_offscreen.h" /* own include */
53
54
55  /* -------------------------------------------------------------------- */
56
57  /** \name GPUOffScreen Common Utilities
58   * \{ */
59
60 static int bpygpu_offscreen_valid_check(BPyGPUOffScreen *bpygpu_ofs)
61 {
62         if (UNLIKELY(bpygpu_ofs->ofs == NULL)) {
63                 PyErr_SetString(PyExc_ReferenceError, "GPU offscreen was freed, no further access is valid");
64                 return -1;
65         }
66         return 0;
67 }
68
69 #define BPY_GPU_OFFSCREEN_CHECK_OBJ(bpygpu) { \
70         if (UNLIKELY(bpygpu_offscreen_valid_check(bpygpu) == -1)) { \
71                 return NULL; \
72         } \
73 } ((void)0)
74
75 /** \} */
76
77
78 /* -------------------------------------------------------------------- */
79 /** \name GPUOffscreen Type
80  * \{ */
81
82
83 static PyObject *bpygpu_offscreen_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
84 {
85         BPYGPU_IS_INIT_OR_ERROR_OBJ;
86
87         GPUOffScreen *ofs;
88         int width, height, samples = 0;
89         char err_out[256];
90
91         static const char *_keywords[] = {"width", "height", "samples", NULL};
92         static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0};
93         if (!_PyArg_ParseTupleAndKeywordsFast(
94                 args, kwds, &_parser,
95                 &width, &height, &samples))
96         {
97                 return NULL;
98         }
99
100         ofs = GPU_offscreen_create(width, height, samples, true, false, err_out);
101
102         if (ofs == NULL) {
103                 PyErr_Format(PyExc_RuntimeError,
104                              "gpu.offscreen.new(...) failed with '%s'",
105                              err_out[0] ? err_out : "unknown error");
106                 return NULL;
107         }
108
109         return BPyGPUOffScreen_CreatePyObject(ofs);
110 }
111
112 PyDoc_STRVAR(bpygpu_offscreen_width_doc, "Width of the texture.\n\n:type: `int`");
113 static PyObject *bpygpu_offscreen_width_get(BPyGPUOffScreen *self, void *UNUSED(type))
114 {
115         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
116         return PyLong_FromLong(GPU_offscreen_width(self->ofs));
117 }
118
119 PyDoc_STRVAR(bpygpu_offscreen_height_doc, "Height of the texture.\n\n:type: `int`");
120 static PyObject *bpygpu_offscreen_height_get(BPyGPUOffScreen *self, void *UNUSED(type))
121 {
122         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
123         return PyLong_FromLong(GPU_offscreen_height(self->ofs));
124 }
125
126 PyDoc_STRVAR(bpygpu_offscreen_color_texture_doc, "OpenGL bindcode for the color texture.\n\n:type: `int`");
127 static PyObject *bpygpu_offscreen_color_texture_get(BPyGPUOffScreen *self, void *UNUSED(type))
128 {
129         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
130         GPUTexture *texture = GPU_offscreen_color_texture(self->ofs);
131         return PyLong_FromLong(GPU_texture_opengl_bindcode(texture));
132 }
133
134 PyDoc_STRVAR(bpygpu_offscreen_bind_doc,
135 ".. method:: bind(save=True)\n"
136 "\n"
137 "   Bind the offscreen object.\n"
138 "   To make sure that the offscreen gets unbind whether an exception occurs or not, pack it into a `with` statement.\n"
139 "\n"
140 "   :arg save: Save the current OpenGL state, so that it can be restored when unbinding.\n"
141 "   :type save: `bool`\n"
142 );
143 static PyObject *bpygpu_offscreen_bind(BPyGPUOffScreen *self, PyObject *args, PyObject *kwds)
144 {
145         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
146         bool save = true;
147
148         static const char *_keywords[] = {"save", NULL};
149         static _PyArg_Parser _parser = {"|O&:bind", _keywords, 0};
150         if (!_PyArg_ParseTupleAndKeywordsFast(
151                 args, kwds, &_parser,
152                 PyC_ParseBool, &save))
153         {
154                 return NULL;
155         }
156
157         GPU_offscreen_bind(self->ofs, save);
158
159         self->is_saved = save;
160         Py_INCREF(self);
161
162         return (PyObject *)self;
163 }
164
165 PyDoc_STRVAR(bpygpu_offscreen_unbind_doc,
166 ".. method:: unbind(restore=True)\n"
167 "\n"
168 "   Unbind the offscreen object.\n"
169 "\n"
170 "   :arg restore: Restore the OpenGL state, can only be used when the state has been saved before.\n"
171 "   :type restore: `bool`\n"
172 );
173 static PyObject *bpygpu_offscreen_unbind(BPyGPUOffScreen *self, PyObject *args, PyObject *kwds)
174 {
175         bool restore = true;
176
177         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
178
179         static const char *_keywords[] = {"restore", NULL};
180         static _PyArg_Parser _parser = {"|O&:unbind", _keywords, 0};
181         if (!_PyArg_ParseTupleAndKeywordsFast(
182                 args, kwds, &_parser,
183                 PyC_ParseBool, &restore))
184         {
185                 return NULL;
186         }
187
188         GPU_offscreen_unbind(self->ofs, restore);
189         Py_RETURN_NONE;
190 }
191
192 PyDoc_STRVAR(bpygpu_offscreen_draw_view3d_doc,
193 ".. method:: draw_view3d(scene, view3d, region, view_matrix, projection_matrix)\n"
194 "\n"
195 "   Draw the 3d viewport in the offscreen object.\n"
196 "\n"
197 "   :arg scene: Scene to draw.\n"
198 "   :type scene: :class:`bpy.types.Scene`\n"
199 "   :arg view_layer: View layer to draw.\n"
200 "   :type view_layer: :class:`bpy.types.ViewLayer`\n"
201 "   :arg view3d: 3D View to get the drawing settings from.\n"
202 "   :type view3d: :class:`bpy.types.SpaceView3D`\n"
203 "   :arg region: Region of the 3D View (required as temporary draw target).\n"
204 "   :type region: :class:`bpy.types.Region`\n"
205 "   :arg view_matrix: View Matrix (e.g. ``camera.matrix_world.inverted()``).\n"
206 "   :type view_matrix: :class:`mathutils.Matrix`\n"
207 "   :arg projection_matrix: Projection Matrix (e.g. ``camera.calc_matrix_camera(...)``).\n"
208 "   :type projection_matrix: :class:`mathutils.Matrix`\n"
209 );
210 static PyObject *bpygpu_offscreen_draw_view3d(BPyGPUOffScreen *self, PyObject *args, PyObject *kwds)
211 {
212         MatrixObject *py_mat_view, *py_mat_projection;
213         PyObject *py_scene, *py_view_layer, *py_region, *py_view3d;
214
215         struct Depsgraph *depsgraph;
216         struct Scene *scene;
217         struct ViewLayer *view_layer;
218         View3D *v3d;
219         ARegion *ar;
220         struct RV3DMatrixStore *rv3d_mats;
221
222         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
223
224         static const char *_keywords[] = {
225                 "scene", "view_layer", "view3d", "region",
226                 "view_matrix", "projection_matrix", NULL};
227
228         static _PyArg_Parser _parser = {"OOOOO&O&:draw_view3d", _keywords, 0};
229         if (!_PyArg_ParseTupleAndKeywordsFast(
230                 args, kwds, &_parser,
231                 &py_scene, &py_view_layer, &py_view3d, &py_region,
232                 Matrix_Parse4x4, &py_mat_view,
233                 Matrix_Parse4x4, &py_mat_projection) ||
234             (!(scene      = PyC_RNA_AsPointer(py_scene, "Scene")) ||
235              !(view_layer = PyC_RNA_AsPointer(py_view_layer, "ViewLayer")) ||
236              !(v3d        = PyC_RNA_AsPointer(py_view3d, "SpaceView3D")) ||
237              !(ar         = PyC_RNA_AsPointer(py_region, "Region"))))
238         {
239                 return NULL;
240         }
241
242         BLI_assert(BKE_id_is_in_global_main(&scene->id));
243
244         depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
245
246         rv3d_mats = ED_view3d_mats_rv3d_backup(ar->regiondata);
247
248         GPU_offscreen_bind(self->ofs, true);
249
250         ED_view3d_draw_offscreen(depsgraph,
251                                  scene,
252                                  v3d->shading.type,
253                                  v3d,
254                                  ar,
255                                  GPU_offscreen_width(self->ofs),
256                                  GPU_offscreen_height(self->ofs),
257                                  (float(*)[4])py_mat_view->matrix,
258                                  (float(*)[4])py_mat_projection->matrix,
259                                  false,
260                                  true,
261                                  "",
262                                  NULL,
263                                  self->ofs,
264                                  NULL);
265
266         GPU_offscreen_unbind(self->ofs, true);
267
268         ED_view3d_mats_rv3d_restore(ar->regiondata, rv3d_mats);
269         MEM_freeN(rv3d_mats);
270
271         Py_RETURN_NONE;
272 }
273
274 PyDoc_STRVAR(bpygpu_offscreen_free_doc,
275 ".. method:: free()\n"
276 "\n"
277 "   Free the offscreen object.\n"
278 "   The framebuffer, texture and render objects will no longer be accessible.\n"
279 );
280 static PyObject *bpygpu_offscreen_free(BPyGPUOffScreen *self)
281 {
282         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
283
284         GPU_offscreen_free(self->ofs);
285         self->ofs = NULL;
286         Py_RETURN_NONE;
287 }
288
289 static PyObject *bpygpu_offscreen_bind_context_enter(BPyGPUOffScreen *UNUSED(self))
290 {
291         Py_RETURN_NONE;
292 }
293
294 static PyObject *bpygpu_offscreen_bind_context_exit(BPyGPUOffScreen *self, PyObject *UNUSED(args))
295 {
296         GPU_offscreen_unbind(self->ofs, self->is_saved);
297         Py_RETURN_NONE;
298 }
299
300 static void BPyGPUOffScreen__tp_dealloc(BPyGPUOffScreen *self)
301 {
302         if (self->ofs)
303                 GPU_offscreen_free(self->ofs);
304         Py_TYPE(self)->tp_free((PyObject *)self);
305 }
306
307 static PyGetSetDef bpygpu_offscreen_getseters[] = {
308         {(char *)"color_texture", (getter)bpygpu_offscreen_color_texture_get, (setter)NULL, bpygpu_offscreen_color_texture_doc, NULL},
309         {(char *)"width", (getter)bpygpu_offscreen_width_get, (setter)NULL, bpygpu_offscreen_width_doc, NULL},
310         {(char *)"height", (getter)bpygpu_offscreen_height_get, (setter)NULL, bpygpu_offscreen_height_doc, NULL},
311         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
312 };
313
314 static struct PyMethodDef bpygpu_offscreen_methods[] = {
315         {"bind", (PyCFunction)bpygpu_offscreen_bind, METH_VARARGS | METH_KEYWORDS, bpygpu_offscreen_bind_doc},
316         {"unbind", (PyCFunction)bpygpu_offscreen_unbind, METH_VARARGS | METH_KEYWORDS, bpygpu_offscreen_unbind_doc},
317         {"draw_view3d", (PyCFunction)bpygpu_offscreen_draw_view3d, METH_VARARGS | METH_KEYWORDS, bpygpu_offscreen_draw_view3d_doc},
318         {"free", (PyCFunction)bpygpu_offscreen_free, METH_NOARGS, bpygpu_offscreen_free_doc},
319         {"__enter__", (PyCFunction)bpygpu_offscreen_bind_context_enter, METH_NOARGS},
320         {"__exit__",  (PyCFunction)bpygpu_offscreen_bind_context_exit,  METH_VARARGS},
321         {NULL, NULL, 0, NULL}
322 };
323
324 PyDoc_STRVAR(bpygpu_offscreen_doc,
325 ".. class:: GPUOffScreen(width, height, samples=0)\n"
326 "\n"
327 "   This object gives access to off screen buffers.\n"
328 "\n"
329 "   :arg width: Horizontal dimension of the buffer.\n"
330 "   :type width: `int`\n"
331 "   :arg height: Vertical dimension of the buffer.\n"
332 "   :type height: `int`\n"
333 "   :arg samples: OpenGL samples to use for MSAA or zero to disable.\n"
334 "   :type samples: `int`\n"
335 );
336 PyTypeObject BPyGPUOffScreen_Type = {
337         PyVarObject_HEAD_INIT(NULL, 0)
338         .tp_name = "GPUOffScreen",
339         .tp_basicsize = sizeof(BPyGPUOffScreen),
340         .tp_dealloc = (destructor)BPyGPUOffScreen__tp_dealloc,
341         .tp_flags = Py_TPFLAGS_DEFAULT,
342         .tp_doc = bpygpu_offscreen_doc,
343         .tp_methods = bpygpu_offscreen_methods,
344         .tp_getset = bpygpu_offscreen_getseters,
345         .tp_new = bpygpu_offscreen_new,
346 };
347
348 /** \} */
349
350
351 /* -------------------------------------------------------------------- */
352 /** \name Public API
353  * \{ */
354
355 PyObject *BPyGPUOffScreen_CreatePyObject(GPUOffScreen *ofs)
356 {
357         BPyGPUOffScreen *self;
358
359         self = PyObject_New(BPyGPUOffScreen, &BPyGPUOffScreen_Type);
360         self->ofs = ofs;
361
362         return (PyObject *)self;
363 }
364
365 /** \} */
366
367 #undef BPY_GPU_OFFSCREEN_CHECK_OBJ