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