Merge branch 'master' into blender2.8
[blender.git] / source / blender / python / intern / gpu_offscreen.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Copyright 2015, Blender Foundation.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/gpu_offscreen.c
24  *  \ingroup pythonintern
25  *
26  * This file defines the offscreen functionalities of the 'gpu' module
27  * used for off-screen OpenGL rendering.
28  */
29
30 #include <Python.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35
36 #include "WM_types.h"
37
38 #include "ED_screen.h"
39
40 #include "GPU_compositing.h"
41 #include "GPU_framebuffer.h"
42
43 #include "../mathutils/mathutils.h"
44
45 #include "../generic/py_capi_utils.h"
46
47 #include "gpu.h"
48
49 #include "ED_view3d.h"
50
51 /* -------------------------------------------------------------------- */
52 /* GPU Offscreen PyObject */
53
54 typedef struct {
55         PyObject_HEAD
56         GPUOffScreen *ofs;
57 } BPy_GPUOffScreen;
58
59 static int bpy_gpu_offscreen_valid_check(BPy_GPUOffScreen *py_gpu_ofs)
60 {
61         if (UNLIKELY(py_gpu_ofs->ofs == NULL)) {
62                 PyErr_SetString(PyExc_ReferenceError, "GPU offscreen was freed, no further access is valid");
63                 return -1;
64         }
65         return 0;
66 }
67
68 #define BPY_GPU_OFFSCREEN_CHECK_OBJ(pygpu) { \
69         if (UNLIKELY(bpy_gpu_offscreen_valid_check(pygpu) == -1)) { \
70                 return NULL; \
71         } \
72 } ((void)0)
73
74 PyDoc_STRVAR(pygpu_offscreen_width_doc, "Texture width.\n\n:type: int");
75 static PyObject *pygpu_offscreen_width_get(BPy_GPUOffScreen *self, void *UNUSED(type))
76 {
77         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
78         return PyLong_FromLong(GPU_offscreen_width(self->ofs));
79 }
80
81 PyDoc_STRVAR(pygpu_offscreen_height_doc, "Texture height.\n\n:type: int");
82 static PyObject *pygpu_offscreen_height_get(BPy_GPUOffScreen *self, void *UNUSED(type))
83 {
84         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
85         return PyLong_FromLong(GPU_offscreen_height(self->ofs));
86 }
87
88 PyDoc_STRVAR(pygpu_offscreen_color_texture_doc, "Color texture.\n\n:type: int");
89 static PyObject *pygpu_offscreen_color_texture_get(BPy_GPUOffScreen *self, void *UNUSED(type))
90 {
91         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
92         return PyLong_FromLong(GPU_offscreen_color_texture(self->ofs));
93 }
94
95 PyDoc_STRVAR(pygpu_offscreen_bind_doc,
96 "bind(save=True)\n"
97 "\n"
98 "   Bind the offscreen object.\n"
99 "\n"
100 "   :param save: save OpenGL current states.\n"
101 "   :type save: bool\n"
102 );
103 static PyObject *pygpu_offscreen_bind(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
104 {
105         static const char *kwlist[] = {"save", NULL};
106         bool save = true;
107
108         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
109
110         if (!PyArg_ParseTupleAndKeywords(
111                 args, kwds, "|O&:bind", (char **)(kwlist),
112                 PyC_ParseBool, &save))
113         {
114                 return NULL;
115         }
116
117         GPU_offscreen_bind(self->ofs, save);
118         Py_RETURN_NONE;
119 }
120
121 PyDoc_STRVAR(pygpu_offscreen_unbind_doc,
122 "unbind(restore=True)\n"
123 "\n"
124 "   Unbind the offscreen object.\n"
125 "\n"
126 "   :param restore: restore OpenGL previous states.\n"
127 "   :type restore: bool\n"
128 );
129 static PyObject *pygpu_offscreen_unbind(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
130 {
131         static const char *kwlist[] = {"restore", NULL};
132         bool restore = true;
133
134         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
135
136         if (!PyArg_ParseTupleAndKeywords(
137                 args, kwds, "|O&:unbind", (char **)(kwlist),
138                 PyC_ParseBool, &restore))
139         {
140                 return NULL;
141         }
142
143         GPU_offscreen_unbind(self->ofs, restore);
144         Py_RETURN_NONE;
145 }
146
147 PyDoc_STRVAR(pygpu_offscreen_draw_view3d_doc,
148 "draw_view3d(scene, view3d, region, modelview_matrix, projection_matrix)\n"
149 "\n"
150 "   Draw the 3d viewport in the offscreen object.\n"
151 "\n"
152 "   :param scene: Scene to draw.\n"
153 "   :type scene: :class:`bpy.types.Scene`\n"
154 "   :param view3d: 3D View to get the drawing settings from.\n"
155 "   :type view3d: :class:`bpy.types.SpaceView3D`\n"
156 "   :param region: Region of the 3D View.\n"
157 "   :type region: :class:`bpy.types.Region`\n"
158 "   :param modelview_matrix: ModelView Matrix.\n"
159 "   :type modelview_matrix: :class:`mathutils.Matrix`\n"
160 "   :param projection_matrix: Projection Matrix.\n"
161 "   :type projection_matrix: :class:`mathutils.Matrix`\n"
162 );
163 static PyObject *pygpu_offscreen_draw_view3d(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
164 {
165         /* TODO: This doesn't work currently because of eval_ctx. */
166 #if 0
167         static const char *kwlist[] = {"scene", "render_layer", "view3d", "region", "projection_matrix", "modelview_matrix", NULL};
168
169         MatrixObject *py_mat_modelview, *py_mat_projection;
170         PyObject *py_scene, *py_scene_layer, *py_region, *py_view3d;
171
172         Scene *scene;
173         SceneLayer *sl;
174         View3D *v3d;
175         ARegion *ar;
176         GPUFX *fx;
177         GPUFXSettings fx_settings;
178         struct RV3DMatrixStore *rv3d_mats;
179
180         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
181
182         if (!PyArg_ParseTupleAndKeywords(
183                 args, kwds, "OOOOO&O&:draw_view3d", (char **)(kwlist),
184                 &py_scene, &py_scene_layer, &py_view3d, &py_region,
185                 Matrix_Parse4x4, &py_mat_projection,
186                 Matrix_Parse4x4, &py_mat_modelview) ||
187             (!(scene    = PyC_RNA_AsPointer(py_scene, "Scene")) ||
188              !(sl       = PyC_RNA_AsPointer(py_scene_layer, "SceneLayer")) ||
189              !(v3d      = PyC_RNA_AsPointer(py_view3d, "SpaceView3D")) ||
190              !(ar       = PyC_RNA_AsPointer(py_region, "Region"))))
191         {
192                 return NULL;
193         }
194
195         fx = GPU_fx_compositor_create();
196
197         fx_settings = v3d->fx_settings;  /* full copy */
198
199         ED_view3d_draw_offscreen_init(scene, sl, v3d);
200
201         rv3d_mats = ED_view3d_mats_rv3d_backup(ar->regiondata);
202
203         GPU_offscreen_bind(self->ofs, true); /* bind */
204
205         ED_view3d_draw_offscreen(
206                 scene, sl, v3d, ar, GPU_offscreen_width(self->ofs), GPU_offscreen_height(self->ofs),
207                 (float(*)[4])py_mat_modelview->matrix, (float(*)[4])py_mat_projection->matrix,
208                 false, true, true, "",
209                 fx, &fx_settings,
210                 self->ofs);
211
212         GPU_fx_compositor_destroy(fx);
213         GPU_offscreen_unbind(self->ofs, true); /* unbind */
214
215         ED_view3d_mats_rv3d_restore(ar->regiondata, rv3d_mats);
216         MEM_freeN(rv3d_mats);
217
218         Py_RETURN_NONE;
219 #else
220         UNUSED_VARS(self, args, kwds);
221 #endif
222         return NULL;
223 }
224
225 PyDoc_STRVAR(pygpu_offscreen_free_doc,
226 "free()\n"
227 "\n"
228 "   Free the offscreen object\n"
229 "   The framebuffer, texture and render objects will no longer be accessible.\n"
230 );
231 static PyObject *pygpu_offscreen_free(BPy_GPUOffScreen *self)
232 {
233         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
234
235         GPU_offscreen_free(self->ofs);
236         self->ofs = NULL;
237         Py_RETURN_NONE;
238 }
239
240 static void BPy_GPUOffScreen__tp_dealloc(BPy_GPUOffScreen *self)
241 {
242         if (self->ofs)
243                 GPU_offscreen_free(self->ofs);
244         Py_TYPE(self)->tp_free((PyObject *)self);
245 }
246
247 static PyGetSetDef bpy_gpu_offscreen_getseters[] = {
248         {(char *)"color_texture", (getter)pygpu_offscreen_color_texture_get, (setter)NULL, pygpu_offscreen_color_texture_doc, NULL},
249         {(char *)"width", (getter)pygpu_offscreen_width_get, (setter)NULL, pygpu_offscreen_width_doc, NULL},
250         {(char *)"height", (getter)pygpu_offscreen_height_get, (setter)NULL, pygpu_offscreen_height_doc, NULL},
251         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
252 };
253
254 static struct PyMethodDef bpy_gpu_offscreen_methods[] = {
255         {"bind", (PyCFunction)pygpu_offscreen_bind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_bind_doc},
256         {"unbind", (PyCFunction)pygpu_offscreen_unbind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_unbind_doc},
257         {"draw_view3d", (PyCFunction)pygpu_offscreen_draw_view3d, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_draw_view3d_doc},
258         {"free", (PyCFunction)pygpu_offscreen_free, METH_NOARGS, pygpu_offscreen_free_doc},
259         {NULL, NULL, 0, NULL}
260 };
261
262 PyDoc_STRVAR(py_gpu_offscreen_doc,
263 ".. class:: GPUOffscreen"
264 "\n"
265 "   This object gives access to off screen buffers.\n"
266 );
267 static PyTypeObject BPy_GPUOffScreen_Type = {
268         PyVarObject_HEAD_INIT(NULL, 0)
269         "GPUOffScreen",                              /* tp_name */
270         sizeof(BPy_GPUOffScreen),                      /* tp_basicsize */
271         0,                                           /* tp_itemsize */
272         /* methods */
273         (destructor)BPy_GPUOffScreen__tp_dealloc,      /* tp_dealloc */
274         NULL,                                        /* tp_print */
275         NULL,                                        /* tp_getattr */
276         NULL,                                        /* tp_setattr */
277         NULL,                                        /* tp_compare */
278         NULL,                                        /* tp_repr */
279         NULL,                                        /* tp_as_number */
280         NULL,                                        /* tp_as_sequence */
281         NULL,                                        /* tp_as_mapping */
282         NULL,                                        /* tp_hash */
283         NULL,                                        /* tp_call */
284         NULL,                                        /* tp_str */
285         NULL,                                        /* tp_getattro */
286         NULL,                                        /* tp_setattro */
287         NULL,                                        /* tp_as_buffer */
288         Py_TPFLAGS_DEFAULT,                          /* tp_flags */
289         py_gpu_offscreen_doc,                        /* Documentation string */
290         NULL,                                        /* tp_traverse */
291         NULL,                                        /* tp_clear */
292         NULL,                                        /* tp_richcompare */
293         0,                                           /* tp_weaklistoffset */
294         NULL,                                        /* tp_iter */
295         NULL,                                        /* tp_iternext */
296         bpy_gpu_offscreen_methods,                   /* tp_methods */
297         NULL,                                        /* tp_members */
298         bpy_gpu_offscreen_getseters,                 /* tp_getset */
299         NULL,                                        /* tp_base */
300         NULL,                                        /* tp_dict */
301         NULL,                                        /* tp_descr_get */
302         NULL,                                        /* tp_descr_set */
303         0,                                           /* tp_dictoffset */
304         0,                                           /* tp_init */
305         NULL,                                        /* tp_alloc */
306         NULL,                                        /* tp_new */
307         (freefunc)0,                                 /* tp_free */
308         NULL,                                        /* tp_is_gc */
309         NULL,                                        /* tp_bases */
310         NULL,                                        /* tp_mro */
311         NULL,                                        /* tp_cache */
312         NULL,                                        /* tp_subclasses */
313         NULL,                                        /* tp_weaklist */
314         (destructor) NULL                            /* tp_del */
315 };
316
317 /* -------------------------------------------------------------------- */
318 /* GPU offscreen methods */
319
320 static PyObject *BPy_GPU_OffScreen_CreatePyObject(GPUOffScreen *ofs)
321 {
322         BPy_GPUOffScreen *self;
323         self = PyObject_New(BPy_GPUOffScreen, &BPy_GPUOffScreen_Type);
324         self->ofs = ofs;
325         return (PyObject *)self;
326 }
327
328 PyDoc_STRVAR(pygpu_offscreen_new_doc,
329 "new(width, height, samples=0)\n"
330 "\n"
331 "   Return a GPUOffScreen.\n"
332 "\n"
333 "   :param width: Horizontal dimension of the buffer.\n"
334 "   :type width: int`\n"
335 "   :param height: Vertical dimension of the buffer.\n"
336 "   :type height: int`\n"
337 "   :param samples: OpenGL samples to use for MSAA or zero to disable.\n"
338 "   :type samples: int\n"
339 "   :return: Newly created off-screen buffer.\n"
340 "   :rtype: :class:`gpu.GPUOffscreen`\n"
341 );
342 static PyObject *pygpu_offscreen_new(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
343 {
344         static const char *kwlist[] = {"width", "height", "samples", NULL};
345
346         GPUOffScreen *ofs;
347         int width, height, samples = 0;
348         char err_out[256];
349
350         if (!PyArg_ParseTupleAndKeywords(
351                 args, kwds, "ii|i:new", (char **)(kwlist),
352                 &width, &height, &samples))
353         {
354                 return NULL;
355         }
356
357         ofs = GPU_offscreen_create(width, height, samples, err_out);
358
359         if (ofs == NULL) {
360                 PyErr_Format(PyExc_RuntimeError,
361                              "gpu.offscreen.new(...) failed with '%s'",
362                              err_out[0] ? err_out : "unknown error");
363                 return NULL;
364         }
365
366         return BPy_GPU_OffScreen_CreatePyObject(ofs);
367 }
368
369 static struct PyMethodDef BPy_GPU_offscreen_methods[] = {
370         {"new", (PyCFunction)pygpu_offscreen_new, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_new_doc},
371         {NULL, NULL, 0, NULL}
372 };
373
374 PyDoc_STRVAR(BPy_GPU_offscreen_doc,
375 "This module provides access to offscreen rendering functions."
376 );
377 static PyModuleDef BPy_GPU_offscreen_module_def = {
378         PyModuleDef_HEAD_INIT,
379         "gpu.offscreen",                             /* m_name */
380         BPy_GPU_offscreen_doc,                       /* m_doc */
381         0,                                           /* m_size */
382         BPy_GPU_offscreen_methods,                   /* m_methods */
383         NULL,                                        /* m_reload */
384         NULL,                                        /* m_traverse */
385         NULL,                                        /* m_clear */
386         NULL,                                        /* m_free */
387 };
388
389 PyObject *BPyInit_gpu_offscreen(void)
390 {
391         PyObject *submodule;
392
393         /* Register the 'GPUOffscreen' class */
394         if (PyType_Ready(&BPy_GPUOffScreen_Type)) {
395                 return NULL;
396         }
397
398         submodule = PyModule_Create(&BPy_GPU_offscreen_module_def);
399
400 #define MODULE_TYPE_ADD(s, t) \
401         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
402
403         MODULE_TYPE_ADD(submodule, BPy_GPUOffScreen_Type);
404
405 #undef MODULE_TYPE_ADD
406
407         return submodule;
408 }
409
410 #undef BPY_GPU_OFFSCREEN_CHECK_OBJ