4520495a4b8ec863e324b2b61582a59236c27625
[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 "BKE_global.h"
39
40 #include "ED_screen.h"
41
42 #include "GPU_compositing.h"
43 #include "GPU_framebuffer.h"
44
45 #include "../mathutils/mathutils.h"
46
47 #include "../generic/py_capi_utils.h"
48
49 #include "gpu.h"
50
51 #include "ED_view3d.h"
52
53 /* -------------------------------------------------------------------- */
54 /* GPU Offscreen PyObject */
55
56 typedef struct {
57         PyObject_HEAD
58         GPUOffScreen *ofs;
59 } BPy_GPUOffScreen;
60
61 static int bpy_gpu_offscreen_valid_check(BPy_GPUOffScreen *py_gpu_ofs)
62 {
63         if (UNLIKELY(py_gpu_ofs->ofs == NULL)) {
64                 PyErr_SetString(PyExc_ReferenceError, "GPU offscreen was freed, no further access is valid");
65                 return -1;
66         }
67         return 0;
68 }
69
70 #define BPY_GPU_OFFSCREEN_CHECK_OBJ(pygpu) { \
71         if (UNLIKELY(bpy_gpu_offscreen_valid_check(pygpu) == -1)) { \
72                 return NULL; \
73         } \
74 } ((void)0)
75
76 PyDoc_STRVAR(pygpu_offscreen_width_doc, "Texture width.\n\n:type: int");
77 static PyObject *pygpu_offscreen_width_get(BPy_GPUOffScreen *self, void *UNUSED(type))
78 {
79         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
80         return PyLong_FromLong(GPU_offscreen_width(self->ofs));
81 }
82
83 PyDoc_STRVAR(pygpu_offscreen_height_doc, "Texture height.\n\n:type: int");
84 static PyObject *pygpu_offscreen_height_get(BPy_GPUOffScreen *self, void *UNUSED(type))
85 {
86         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
87         return PyLong_FromLong(GPU_offscreen_height(self->ofs));
88 }
89
90 PyDoc_STRVAR(pygpu_offscreen_color_texture_doc, "Color texture.\n\n:type: int");
91 static PyObject *pygpu_offscreen_color_texture_get(BPy_GPUOffScreen *self, void *UNUSED(type))
92 {
93         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
94         return PyLong_FromLong(GPU_offscreen_color_texture(self->ofs));
95 }
96
97 PyDoc_STRVAR(pygpu_offscreen_bind_doc,
98 "bind(save=True)\n"
99 "\n"
100 "   Bind the offscreen object.\n"
101 "\n"
102 "   :param save: save OpenGL current states.\n"
103 "   :type save: bool\n"
104 );
105 static PyObject *pygpu_offscreen_bind(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
106 {
107         static const char *kwlist[] = {"save", NULL};
108         bool save = true;
109
110         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
111
112         if (!PyArg_ParseTupleAndKeywords(
113                 args, kwds, "|O&:bind", (char **)(kwlist),
114                 PyC_ParseBool, &save))
115         {
116                 return NULL;
117         }
118
119         GPU_offscreen_bind(self->ofs, save);
120         Py_RETURN_NONE;
121 }
122
123 PyDoc_STRVAR(pygpu_offscreen_unbind_doc,
124 "unbind(restore=True)\n"
125 "\n"
126 "   Unbind the offscreen object.\n"
127 "\n"
128 "   :param restore: restore OpenGL previous states.\n"
129 "   :type restore: bool\n"
130 );
131 static PyObject *pygpu_offscreen_unbind(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
132 {
133         static const char *kwlist[] = {"restore", NULL};
134         bool restore = true;
135
136         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
137
138         if (!PyArg_ParseTupleAndKeywords(
139                 args, kwds, "|O&:unbind", (char **)(kwlist),
140                 PyC_ParseBool, &restore))
141         {
142                 return NULL;
143         }
144
145         GPU_offscreen_unbind(self->ofs, restore);
146         Py_RETURN_NONE;
147 }
148
149 PyDoc_STRVAR(pygpu_offscreen_draw_view3d_doc,
150 "draw_view3d(scene, view3d, region, modelview_matrix, projection_matrix)\n"
151 "\n"
152 "   Draw the 3d viewport in the offscreen object.\n"
153 "\n"
154 "   :param scene: Scene to draw.\n"
155 "   :type scene: :class:`bpy.types.Scene`\n"
156 "   :param view3d: 3D View to get the drawing settings from.\n"
157 "   :type view3d: :class:`bpy.types.SpaceView3D`\n"
158 "   :param region: Region of the 3D View.\n"
159 "   :type region: :class:`bpy.types.Region`\n"
160 "   :param modelview_matrix: ModelView Matrix.\n"
161 "   :type modelview_matrix: :class:`mathutils.Matrix`\n"
162 "   :param projection_matrix: Projection Matrix.\n"
163 "   :type projection_matrix: :class:`mathutils.Matrix`\n"
164 );
165 static PyObject *pygpu_offscreen_draw_view3d(BPy_GPUOffScreen *self, PyObject *args, PyObject *kwds)
166 {
167         static const char *kwlist[] = {"scene", "view3d", "region", "projection_matrix", "modelview_matrix", NULL};
168
169         MatrixObject *py_mat_modelview, *py_mat_projection;
170         PyObject *py_scene, *py_region, *py_view3d;
171
172         struct Main *bmain = G.main;  /* XXX UGLY! */
173         Scene *scene;
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, "OOOO&O&:draw_view3d", (char **)(kwlist),
184                 &py_scene, &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              !(v3d      = PyC_RNA_AsPointer(py_view3d, "SpaceView3D")) ||
189              !(ar       = PyC_RNA_AsPointer(py_region, "Region"))))
190         {
191                 return NULL;
192         }
193
194         fx = GPU_fx_compositor_create();
195
196         fx_settings = v3d->fx_settings;  /* full copy */
197
198         ED_view3d_draw_offscreen_init(bmain, scene, v3d);
199
200         rv3d_mats = ED_view3d_mats_rv3d_backup(ar->regiondata);
201
202         GPU_offscreen_bind(self->ofs, true); /* bind */
203
204         ED_view3d_draw_offscreen(
205                 bmain, scene, v3d, ar, GPU_offscreen_width(self->ofs), GPU_offscreen_height(self->ofs),
206                 (float(*)[4])py_mat_modelview->matrix, (float(*)[4])py_mat_projection->matrix,
207                 false, true, true, "",
208                 fx, &fx_settings,
209                 self->ofs);
210
211         GPU_fx_compositor_destroy(fx);
212         GPU_offscreen_unbind(self->ofs, true); /* unbind */
213
214         ED_view3d_mats_rv3d_restore(ar->regiondata, rv3d_mats);
215         MEM_freeN(rv3d_mats);
216
217         Py_RETURN_NONE;
218 }
219
220 PyDoc_STRVAR(pygpu_offscreen_free_doc,
221 "free()\n"
222 "\n"
223 "   Free the offscreen object\n"
224 "   The framebuffer, texture and render objects will no longer be accessible.\n"
225 );
226 static PyObject *pygpu_offscreen_free(BPy_GPUOffScreen *self)
227 {
228         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
229
230         GPU_offscreen_free(self->ofs);
231         self->ofs = NULL;
232         Py_RETURN_NONE;
233 }
234
235 static void BPy_GPUOffScreen__tp_dealloc(BPy_GPUOffScreen *self)
236 {
237         if (self->ofs)
238                 GPU_offscreen_free(self->ofs);
239         Py_TYPE(self)->tp_free((PyObject *)self);
240 }
241
242 static PyGetSetDef bpy_gpu_offscreen_getseters[] = {
243         {(char *)"color_texture", (getter)pygpu_offscreen_color_texture_get, (setter)NULL, pygpu_offscreen_color_texture_doc, NULL},
244         {(char *)"width", (getter)pygpu_offscreen_width_get, (setter)NULL, pygpu_offscreen_width_doc, NULL},
245         {(char *)"height", (getter)pygpu_offscreen_height_get, (setter)NULL, pygpu_offscreen_height_doc, NULL},
246         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
247 };
248
249 static struct PyMethodDef bpy_gpu_offscreen_methods[] = {
250         {"bind", (PyCFunction)pygpu_offscreen_bind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_bind_doc},
251         {"unbind", (PyCFunction)pygpu_offscreen_unbind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_unbind_doc},
252         {"draw_view3d", (PyCFunction)pygpu_offscreen_draw_view3d, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_draw_view3d_doc},
253         {"free", (PyCFunction)pygpu_offscreen_free, METH_NOARGS, pygpu_offscreen_free_doc},
254         {NULL, NULL, 0, NULL}
255 };
256
257 PyDoc_STRVAR(py_gpu_offscreen_doc,
258 ".. class:: GPUOffscreen"
259 "\n"
260 "   This object gives access to off screen buffers.\n"
261 );
262 static PyTypeObject BPy_GPUOffScreen_Type = {
263         PyVarObject_HEAD_INIT(NULL, 0)
264         "GPUOffScreen",                              /* tp_name */
265         sizeof(BPy_GPUOffScreen),                      /* tp_basicsize */
266         0,                                           /* tp_itemsize */
267         /* methods */
268         (destructor)BPy_GPUOffScreen__tp_dealloc,      /* tp_dealloc */
269         NULL,                                        /* tp_print */
270         NULL,                                        /* tp_getattr */
271         NULL,                                        /* tp_setattr */
272         NULL,                                        /* tp_compare */
273         NULL,                                        /* tp_repr */
274         NULL,                                        /* tp_as_number */
275         NULL,                                        /* tp_as_sequence */
276         NULL,                                        /* tp_as_mapping */
277         NULL,                                        /* tp_hash */
278         NULL,                                        /* tp_call */
279         NULL,                                        /* tp_str */
280         NULL,                                        /* tp_getattro */
281         NULL,                                        /* tp_setattro */
282         NULL,                                        /* tp_as_buffer */
283         Py_TPFLAGS_DEFAULT,                          /* tp_flags */
284         py_gpu_offscreen_doc,                        /* Documentation string */
285         NULL,                                        /* tp_traverse */
286         NULL,                                        /* tp_clear */
287         NULL,                                        /* tp_richcompare */
288         0,                                           /* tp_weaklistoffset */
289         NULL,                                        /* tp_iter */
290         NULL,                                        /* tp_iternext */
291         bpy_gpu_offscreen_methods,                   /* tp_methods */
292         NULL,                                        /* tp_members */
293         bpy_gpu_offscreen_getseters,                 /* tp_getset */
294         NULL,                                        /* tp_base */
295         NULL,                                        /* tp_dict */
296         NULL,                                        /* tp_descr_get */
297         NULL,                                        /* tp_descr_set */
298         0,                                           /* tp_dictoffset */
299         0,                                           /* tp_init */
300         NULL,                                        /* tp_alloc */
301         NULL,                                        /* tp_new */
302         (freefunc)0,                                 /* tp_free */
303         NULL,                                        /* tp_is_gc */
304         NULL,                                        /* tp_bases */
305         NULL,                                        /* tp_mro */
306         NULL,                                        /* tp_cache */
307         NULL,                                        /* tp_subclasses */
308         NULL,                                        /* tp_weaklist */
309         (destructor) NULL                            /* tp_del */
310 };
311
312 /* -------------------------------------------------------------------- */
313 /* GPU offscreen methods */
314
315 static PyObject *BPy_GPU_OffScreen_CreatePyObject(GPUOffScreen *ofs)
316 {
317         BPy_GPUOffScreen *self;
318         self = PyObject_New(BPy_GPUOffScreen, &BPy_GPUOffScreen_Type);
319         self->ofs = ofs;
320         return (PyObject *)self;
321 }
322
323 PyDoc_STRVAR(pygpu_offscreen_new_doc,
324 "new(width, height, samples=0)\n"
325 "\n"
326 "   Return a GPUOffScreen.\n"
327 "\n"
328 "   :param width: Horizontal dimension of the buffer.\n"
329 "   :type width: int`\n"
330 "   :param height: Vertical dimension of the buffer.\n"
331 "   :type height: int`\n"
332 "   :param samples: OpenGL samples to use for MSAA or zero to disable.\n"
333 "   :type samples: int\n"
334 "   :return: Newly created off-screen buffer.\n"
335 "   :rtype: :class:`gpu.GPUOffscreen`\n"
336 );
337 static PyObject *pygpu_offscreen_new(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
338 {
339         static const char *kwlist[] = {"width", "height", "samples", NULL};
340
341         GPUOffScreen *ofs;
342         int width, height, samples = 0;
343         char err_out[256];
344
345         if (!PyArg_ParseTupleAndKeywords(
346                 args, kwds, "ii|i:new", (char **)(kwlist),
347                 &width, &height, &samples))
348         {
349                 return NULL;
350         }
351
352         ofs = GPU_offscreen_create(width, height, samples, err_out);
353
354         if (ofs == NULL) {
355                 PyErr_Format(PyExc_RuntimeError,
356                              "gpu.offscreen.new(...) failed with '%s'",
357                              err_out[0] ? err_out : "unknown error");
358                 return NULL;
359         }
360
361         return BPy_GPU_OffScreen_CreatePyObject(ofs);
362 }
363
364 static struct PyMethodDef BPy_GPU_offscreen_methods[] = {
365         {"new", (PyCFunction)pygpu_offscreen_new, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_new_doc},
366         {NULL, NULL, 0, NULL}
367 };
368
369 PyDoc_STRVAR(BPy_GPU_offscreen_doc,
370 "This module provides access to offscreen rendering functions."
371 );
372 static PyModuleDef BPy_GPU_offscreen_module_def = {
373         PyModuleDef_HEAD_INIT,
374         "gpu.offscreen",                             /* m_name */
375         BPy_GPU_offscreen_doc,                       /* m_doc */
376         0,                                           /* m_size */
377         BPy_GPU_offscreen_methods,                   /* m_methods */
378         NULL,                                        /* m_reload */
379         NULL,                                        /* m_traverse */
380         NULL,                                        /* m_clear */
381         NULL,                                        /* m_free */
382 };
383
384 PyObject *BPyInit_gpu_offscreen(void)
385 {
386         PyObject *submodule;
387
388         /* Register the 'GPUOffscreen' class */
389         if (PyType_Ready(&BPy_GPUOffScreen_Type)) {
390                 return NULL;
391         }
392
393         submodule = PyModule_Create(&BPy_GPU_offscreen_module_def);
394
395 #define MODULE_TYPE_ADD(s, t) \
396         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
397
398         MODULE_TYPE_ADD(submodule, BPy_GPUOffScreen_Type);
399
400 #undef MODULE_TYPE_ADD
401
402         return submodule;
403 }
404
405 #undef BPY_GPU_OFFSCREEN_CHECK_OBJ