PyAPI: expose matrix parsing function
[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         static const char *kwlist[] = {"scene", "view3d", "region", "projection_matrix", "modelview_matrix", NULL};
166
167         MatrixObject *py_mat_modelview, *py_mat_projection;
168         PyObject *py_scene, *py_region, *py_view3d;
169
170         Scene *scene;
171         View3D *v3d;
172         ARegion *ar;
173         GPUFX *fx;
174         GPUFXSettings fx_settings;
175         struct RV3DMatrixStore *rv3d_mats;
176
177         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
178
179         if (!PyArg_ParseTupleAndKeywords(
180                 args, kwds, "OOOO&O&:draw_view3d", (char **)(kwlist),
181                 &py_scene, &py_view3d, &py_region,
182                 Matrix_Parse4x4, &py_mat_projection,
183                 Matrix_Parse4x4, &py_mat_modelview) ||
184             (!(scene    = PyC_RNA_AsPointer(py_scene, "Scene")) ||
185              !(v3d      = PyC_RNA_AsPointer(py_view3d, "SpaceView3D")) ||
186              !(ar       = PyC_RNA_AsPointer(py_region, "Region"))))
187         {
188                 return NULL;
189         }
190
191         fx = GPU_fx_compositor_create();
192
193         fx_settings = v3d->fx_settings;  /* full copy */
194
195         ED_view3d_draw_offscreen_init(scene, v3d);
196
197         rv3d_mats = ED_view3d_mats_rv3d_backup(ar->regiondata);
198
199         GPU_offscreen_bind(self->ofs, true); /* bind */
200
201         ED_view3d_draw_offscreen(
202                 scene, v3d, ar, GPU_offscreen_width(self->ofs), GPU_offscreen_height(self->ofs),
203                 (float(*)[4])py_mat_modelview->matrix, (float(*)[4])py_mat_projection->matrix,
204                 false, true, true, "",
205                 fx, &fx_settings,
206                 self->ofs);
207
208         GPU_fx_compositor_destroy(fx);
209         GPU_offscreen_unbind(self->ofs, true); /* unbind */
210
211         ED_view3d_mats_rv3d_restore(ar->regiondata, rv3d_mats);
212         MEM_freeN(rv3d_mats);
213
214         Py_RETURN_NONE;
215 }
216
217 PyDoc_STRVAR(pygpu_offscreen_free_doc,
218 "free()\n"
219 "\n"
220 "   Free the offscreen object\n"
221 "   The framebuffer, texture and render objects will no longer be accessible.\n"
222 );
223 static PyObject *pygpu_offscreen_free(BPy_GPUOffScreen *self)
224 {
225         BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
226
227         GPU_offscreen_free(self->ofs);
228         self->ofs = NULL;
229         Py_RETURN_NONE;
230 }
231
232 static void BPy_GPUOffScreen__tp_dealloc(BPy_GPUOffScreen *self)
233 {
234         if (self->ofs)
235                 GPU_offscreen_free(self->ofs);
236         Py_TYPE(self)->tp_free((PyObject *)self);
237 }
238
239 static PyGetSetDef bpy_gpu_offscreen_getseters[] = {
240         {(char *)"color_texture", (getter)pygpu_offscreen_color_texture_get, (setter)NULL, pygpu_offscreen_color_texture_doc, NULL},
241         {(char *)"width", (getter)pygpu_offscreen_width_get, (setter)NULL, pygpu_offscreen_width_doc, NULL},
242         {(char *)"height", (getter)pygpu_offscreen_height_get, (setter)NULL, pygpu_offscreen_height_doc, NULL},
243         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
244 };
245
246 static struct PyMethodDef bpy_gpu_offscreen_methods[] = {
247         {"bind", (PyCFunction)pygpu_offscreen_bind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_bind_doc},
248         {"unbind", (PyCFunction)pygpu_offscreen_unbind, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_unbind_doc},
249         {"draw_view3d", (PyCFunction)pygpu_offscreen_draw_view3d, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_draw_view3d_doc},
250         {"free", (PyCFunction)pygpu_offscreen_free, METH_NOARGS, pygpu_offscreen_free_doc},
251         {NULL, NULL, 0, NULL}
252 };
253
254 PyDoc_STRVAR(py_gpu_offscreen_doc,
255 ".. class:: GPUOffscreen"
256 "\n"
257 "   This object gives access to off screen buffers.\n"
258 );
259 static PyTypeObject BPy_GPUOffScreen_Type = {
260         PyVarObject_HEAD_INIT(NULL, 0)
261         "GPUOffScreen",                              /* tp_name */
262         sizeof(BPy_GPUOffScreen),                      /* tp_basicsize */
263         0,                                           /* tp_itemsize */
264         /* methods */
265         (destructor)BPy_GPUOffScreen__tp_dealloc,      /* tp_dealloc */
266         NULL,                                        /* tp_print */
267         NULL,                                        /* tp_getattr */
268         NULL,                                        /* tp_setattr */
269         NULL,                                        /* tp_compare */
270         NULL,                                        /* tp_repr */
271         NULL,                                        /* tp_as_number */
272         NULL,                                        /* tp_as_sequence */
273         NULL,                                        /* tp_as_mapping */
274         NULL,                                        /* tp_hash */
275         NULL,                                        /* tp_call */
276         NULL,                                        /* tp_str */
277         NULL,                                        /* tp_getattro */
278         NULL,                                        /* tp_setattro */
279         NULL,                                        /* tp_as_buffer */
280         Py_TPFLAGS_DEFAULT,                          /* tp_flags */
281         py_gpu_offscreen_doc,                        /* Documentation string */
282         NULL,                                        /* tp_traverse */
283         NULL,                                        /* tp_clear */
284         NULL,                                        /* tp_richcompare */
285         0,                                           /* tp_weaklistoffset */
286         NULL,                                        /* tp_iter */
287         NULL,                                        /* tp_iternext */
288         bpy_gpu_offscreen_methods,                   /* tp_methods */
289         NULL,                                        /* tp_members */
290         bpy_gpu_offscreen_getseters,                 /* tp_getset */
291         NULL,                                        /* tp_base */
292         NULL,                                        /* tp_dict */
293         NULL,                                        /* tp_descr_get */
294         NULL,                                        /* tp_descr_set */
295         0,                                           /* tp_dictoffset */
296         0,                                           /* tp_init */
297         NULL,                                        /* tp_alloc */
298         NULL,                                        /* tp_new */
299         (freefunc)0,                                 /* tp_free */
300         NULL,                                        /* tp_is_gc */
301         NULL,                                        /* tp_bases */
302         NULL,                                        /* tp_mro */
303         NULL,                                        /* tp_cache */
304         NULL,                                        /* tp_subclasses */
305         NULL,                                        /* tp_weaklist */
306         (destructor) NULL                            /* tp_del */
307 };
308
309 /* -------------------------------------------------------------------- */
310 /* GPU offscreen methods */
311
312 static PyObject *BPy_GPU_OffScreen_CreatePyObject(GPUOffScreen *ofs)
313 {
314         BPy_GPUOffScreen *self;
315         self = PyObject_New(BPy_GPUOffScreen, &BPy_GPUOffScreen_Type);
316         self->ofs = ofs;
317         return (PyObject *)self;
318 }
319
320 PyDoc_STRVAR(pygpu_offscreen_new_doc,
321 "new(width, height, samples=0)\n"
322 "\n"
323 "   Return a GPUOffScreen.\n"
324 "\n"
325 "   :param width: Horizontal dimension of the buffer.\n"
326 "   :type width: int`\n"
327 "   :param height: Vertical dimension of the buffer.\n"
328 "   :type height: int`\n"
329 "   :param samples: OpenGL samples to use for MSAA or zero to disable.\n"
330 "   :type samples: int\n"
331 "   :return: Newly created off-screen buffer.\n"
332 "   :rtype: :class:`gpu.GPUOffscreen`\n"
333 );
334 static PyObject *pygpu_offscreen_new(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
335 {
336         static const char *kwlist[] = {"width", "height", "samples", NULL};
337
338         GPUOffScreen *ofs;
339         int width, height, samples = 0;
340         char err_out[256];
341
342         if (!PyArg_ParseTupleAndKeywords(
343                 args, kwds, "ii|i:new", (char **)(kwlist),
344                 &width, &height, &samples))
345         {
346                 return NULL;
347         }
348
349         ofs = GPU_offscreen_create(width, height, samples, err_out);
350
351         if (ofs == NULL) {
352                 PyErr_Format(PyExc_RuntimeError,
353                              "gpu.offscreen.new(...) failed with '%s'",
354                              err_out[0] ? err_out : "unknown error");
355                 return NULL;
356         }
357
358         return BPy_GPU_OffScreen_CreatePyObject(ofs);
359 }
360
361 static struct PyMethodDef BPy_GPU_offscreen_methods[] = {
362         {"new", (PyCFunction)pygpu_offscreen_new, METH_VARARGS | METH_KEYWORDS, pygpu_offscreen_new_doc},
363         {NULL, NULL, 0, NULL}
364 };
365
366 PyDoc_STRVAR(BPy_GPU_offscreen_doc,
367 "This module provides access to offscreen rendering functions."
368 );
369 static PyModuleDef BPy_GPU_offscreen_module_def = {
370         PyModuleDef_HEAD_INIT,
371         "gpu.offscreen",                             /* m_name */
372         BPy_GPU_offscreen_doc,                       /* m_doc */
373         0,                                           /* m_size */
374         BPy_GPU_offscreen_methods,                   /* m_methods */
375         NULL,                                        /* m_reload */
376         NULL,                                        /* m_traverse */
377         NULL,                                        /* m_clear */
378         NULL,                                        /* m_free */
379 };
380
381 PyObject *BPyInit_gpu_offscreen(void)
382 {
383         PyObject *submodule;
384
385         /* Register the 'GPUOffscreen' class */
386         if (PyType_Ready(&BPy_GPUOffScreen_Type)) {
387                 return NULL;
388         }
389
390         submodule = PyModule_Create(&BPy_GPU_offscreen_module_def);
391
392 #define MODULE_TYPE_ADD(s, t) \
393         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
394
395         MODULE_TYPE_ADD(submodule, BPy_GPUOffScreen_Type);
396
397 #undef MODULE_TYPE_ADD
398
399         return submodule;
400 }
401
402 #undef BPY_GPU_OFFSCREEN_CHECK_OBJ