GPU: Fix color difference when rendering to gpu_py_offscreen
[blender.git] / source / blender / python / gpu / gpu_py_batch.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
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
36 #include "GPU_batch.h"
37
38 #include "../mathutils/mathutils.h"
39
40 #include "../generic/py_capi_utils.h"
41
42 #include "gpu_py_api.h"
43 #include "gpu_py_shader.h"
44 #include "gpu_py_vertex_buffer.h"
45 #include "gpu_py_element.h"
46 #include "gpu_py_batch.h" /* own include */
47
48
49 /* -------------------------------------------------------------------- */
50 /** \name Utility Functions
51  * \{ */
52
53 static bool bpygpu_batch_is_program_or_error(BPyGPUBatch *self)
54 {
55         if (!glIsProgram(self->batch->program)) {
56                 PyErr_SetString(
57                         PyExc_RuntimeError,
58                         "batch does not have any program assigned to it");
59                 return false;
60         }
61         return true;
62 }
63
64 /** \} */
65
66
67 /* -------------------------------------------------------------------- */
68 /** \name GPUBatch Type
69  * \{ */
70
71 static PyObject *bpygpu_Batch_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
72 {
73         BPYGPU_IS_INIT_OR_ERROR_OBJ;
74
75         const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
76
77         struct {
78                 GPUPrimType type_id;
79                 BPyGPUVertBuf *py_vertbuf;
80                 BPyGPUIndexBuf *py_indexbuf;
81         } params = {GPU_PRIM_NONE, NULL, NULL};
82
83         static const char *_keywords[] = {"type", "buf", "elem", NULL};
84         static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0};
85         if (!_PyArg_ParseTupleAndKeywordsFast(
86                 args, kwds, &_parser,
87                 bpygpu_ParsePrimType, &params.type_id,
88                 &BPyGPUVertBuf_Type, &params.py_vertbuf,
89                 &BPyGPUIndexBuf_Type, &params.py_indexbuf))
90         {
91                 return NULL;
92         }
93
94         if (params.type_id == GPU_PRIM_NONE) {
95                 PyErr_Format(PyExc_TypeError,
96                              exc_str_missing_arg, _keywords[0], 1);
97                 return NULL;
98         }
99
100         if (params.py_vertbuf == NULL) {
101                 PyErr_Format(PyExc_TypeError,
102                              exc_str_missing_arg, _keywords[1], 2);
103                 return NULL;
104         }
105
106         GPUBatch *batch = GPU_batch_create(
107                 params.type_id,
108                 params.py_vertbuf->buf,
109                 params.py_indexbuf ? params.py_indexbuf->elem : NULL);
110
111         BPyGPUBatch *ret = (BPyGPUBatch *)BPyGPUBatch_CreatePyObject(batch);
112
113 #ifdef USE_GPU_PY_REFERENCES
114         ret->references = PyList_New(params.py_indexbuf ? 2 : 1);
115         PyList_SET_ITEM(ret->references, 0, (PyObject *)params.py_vertbuf);
116         Py_INCREF(params.py_vertbuf);
117
118         if (params.py_indexbuf != NULL) {
119                 PyList_SET_ITEM(ret->references, 1, (PyObject *)params.py_indexbuf);
120                 Py_INCREF(params.py_indexbuf);
121         }
122
123         PyObject_GC_Track(ret);
124 #endif
125
126         return (PyObject *)ret;
127 }
128
129 PyDoc_STRVAR(bpygpu_Batch_vertbuf_add_doc,
130 ".. method:: vertbuf_add(buf)\n"
131 "\n"
132 "   Add another vertex buffer to the Batch.\n"
133 "   It is not possible to add more vertices to the batch using this method.\n"
134 "   Instead it can be used to add more attributes to the existing vertices.\n"
135 "   A good use case would be when you have a separate vertex buffer for vertex positions and vertex normals.\n"
136 "   Current a batch can have at most " STRINGIFY(GPU_BATCH_VBO_MAX_LEN) " vertex buffers.\n"
137 "\n"
138 "   :param buf: The vertex buffer that will be added to the batch.\n"
139 "   :type buf: :class:`gpu.types.GPUVertBuf`\n"
140 );
141 static PyObject *bpygpu_Batch_vertbuf_add(BPyGPUBatch *self, BPyGPUVertBuf *py_buf)
142 {
143         if (!BPyGPUVertBuf_Check(py_buf)) {
144                 PyErr_Format(PyExc_TypeError,
145                              "Expected a GPUVertBuf, got %s",
146                              Py_TYPE(py_buf)->tp_name);
147                 return NULL;
148         }
149
150         if (self->batch->verts[0]->vertex_len != py_buf->buf->vertex_len) {
151                 PyErr_Format(PyExc_TypeError,
152                              "Expected %d length, got %d",
153                              self->batch->verts[0]->vertex_len, py_buf->buf->vertex_len);
154                 return NULL;
155         }
156
157         if (self->batch->verts[GPU_BATCH_VBO_MAX_LEN - 1] != NULL) {
158                 PyErr_SetString(
159                         PyExc_RuntimeError,
160                         "Maximum number of vertex buffers exceeded: " STRINGIFY(GPU_BATCH_VBO_MAX_LEN));
161                 return NULL;
162         }
163
164 #ifdef USE_GPU_PY_REFERENCES
165         /* Hold user */
166         PyList_Append(self->references, (PyObject *)py_buf);
167 #endif
168
169         GPU_batch_vertbuf_add(self->batch, py_buf->buf);
170         Py_RETURN_NONE;
171 }
172
173 PyDoc_STRVAR(bpygpu_Batch_program_set_doc,
174 ".. method:: program_set(program)\n"
175 "\n"
176 "   Assign a shader to this batch that will be used for drawing when not overwritten later.\n"
177 "   Note: This method has to be called in the draw context that the batch will be drawn in.\n"
178 "   This function does not need to be called when you always set the shader when calling `batch.draw`.\n"
179 "\n"
180 "   :param program: The program/shader the batch will use in future draw calls.\n"
181 "   :type program: :class:`gpu.types.GPUShader`\n"
182 );
183 static PyObject *bpygpu_Batch_program_set(BPyGPUBatch *self, BPyGPUShader *py_shader)
184 {
185         if (!BPyGPUShader_Check(py_shader)) {
186                 PyErr_Format(PyExc_TypeError,
187                              "Expected a GPUShader, got %s",
188                              Py_TYPE(py_shader)->tp_name);
189                 return NULL;
190         }
191
192         GPUShader *shader = py_shader->shader;
193         GPU_batch_program_set(
194                 self->batch,
195                 GPU_shader_get_program(shader),
196                 GPU_shader_get_interface(shader));
197
198 #ifdef USE_GPU_PY_REFERENCES
199         /* Remove existing user (if any), hold new user. */
200         int i = PyList_GET_SIZE(self->references);
201         while (--i != -1) {
202                 PyObject *py_shader_test = PyList_GET_ITEM(self->references, i);
203                 if (BPyGPUShader_Check(py_shader_test)) {
204                         PyList_SET_ITEM(self->references, i, (PyObject *)py_shader);
205                         Py_INCREF(py_shader);
206                         Py_DECREF(py_shader_test);
207                         /* Only ever reference one shader. */
208                         break;
209                 }
210         }
211         if (i != -1) {
212                 PyList_Append(self->references, (PyObject *)py_shader);
213         }
214 #endif
215
216         Py_RETURN_NONE;
217 }
218
219 PyDoc_STRVAR(bpygpu_Batch_draw_doc,
220 ".. method:: draw(program=None)\n"
221 "\n"
222 "   Run the drawing program with the parameters assigned to the batch.\n"
223 "\n"
224 "   :param program: Program that performs the drawing operations.\n"
225 "      If ``None`` is passed, the last program setted to this batch will run.\n"
226 "   :type program: :class:`gpu.types.GPUShader`\n"
227 );
228 static PyObject *bpygpu_Batch_draw(BPyGPUBatch *self, PyObject *args)
229 {
230         BPyGPUShader *py_program = NULL;
231
232         if (!PyArg_ParseTuple(
233                 args, "|O!:GPUBatch.draw",
234                 &BPyGPUShader_Type, &py_program))
235         {
236                 return NULL;
237         }
238         else if (py_program == NULL) {
239                 if (!bpygpu_batch_is_program_or_error(self)) {
240                         return NULL;
241                 }
242         }
243         else if (self->batch->program != GPU_shader_get_program(py_program->shader)) {
244                 GPU_batch_program_set(
245                         self->batch,
246                         GPU_shader_get_program(py_program->shader),
247                         GPU_shader_get_interface(py_program->shader));
248         }
249
250         GPU_batch_draw(self->batch);
251         Py_RETURN_NONE;
252 }
253
254 static PyObject *bpygpu_Batch_program_use_begin(BPyGPUBatch *self)
255 {
256         if (!bpygpu_batch_is_program_or_error(self)) {
257                 return NULL;
258         }
259         GPU_batch_program_use_begin(self->batch);
260         Py_RETURN_NONE;
261 }
262
263 static PyObject *bpygpu_Batch_program_use_end(BPyGPUBatch *self)
264 {
265         if (!bpygpu_batch_is_program_or_error(self)) {
266                 return NULL;
267         }
268         GPU_batch_program_use_end(self->batch);
269         Py_RETURN_NONE;
270 }
271
272 static struct PyMethodDef bpygpu_Batch_methods[] = {
273         {"vertbuf_add", (PyCFunction)bpygpu_Batch_vertbuf_add,
274          METH_O, bpygpu_Batch_vertbuf_add_doc},
275         {"program_set", (PyCFunction)bpygpu_Batch_program_set,
276          METH_O, bpygpu_Batch_program_set_doc},
277         {"draw", (PyCFunction) bpygpu_Batch_draw,
278          METH_VARARGS, bpygpu_Batch_draw_doc},
279         {"_program_use_begin", (PyCFunction)bpygpu_Batch_program_use_begin,
280          METH_NOARGS, ""},
281         {"_program_use_end", (PyCFunction)bpygpu_Batch_program_use_end,
282          METH_NOARGS, ""},
283         {NULL, NULL, 0, NULL},
284 };
285
286 #ifdef USE_GPU_PY_REFERENCES
287
288 static int bpygpu_Batch_traverse(BPyGPUBatch *self, visitproc visit, void *arg)
289 {
290         Py_VISIT(self->references);
291         return 0;
292 }
293
294 static int bpygpu_Batch_clear(BPyGPUBatch *self)
295 {
296         Py_CLEAR(self->references);
297         return 0;
298 }
299
300 #endif
301
302 static void bpygpu_Batch_dealloc(BPyGPUBatch *self)
303 {
304         GPU_batch_discard(self->batch);
305
306 #ifdef USE_GPU_PY_REFERENCES
307         if (self->references) {
308                 PyObject_GC_UnTrack(self);
309                 bpygpu_Batch_clear(self);
310                 Py_XDECREF(self->references);
311         }
312 #endif
313
314         Py_TYPE(self)->tp_free(self);
315 }
316
317 PyDoc_STRVAR(py_gpu_batch_doc,
318 ".. class:: GPUBatch(type, buf, elem=None)\n"
319 "\n"
320 "   Reusable container for drawable geometry.\n"
321 "\n"
322 "   :arg type: One of these primitive types: {\n"
323 "       `POINTS`,\n"
324 "       `LINES`,\n"
325 "       `TRIS`,\n"
326 "       `LINE_STRIP`,\n"
327 "       `LINE_LOOP`,\n"
328 "       `TRI_STRIP`,\n"
329 "       `TRI_FAN`,\n"
330 "       `LINES_ADJ`,\n"
331 "       `TRIS_ADJ`,\n"
332 "       `LINE_STRIP_ADJ` }\n"
333 "   :type type: `str`\n"
334 "   :arg buf: Vertex buffer containing all or some of the attributes required for drawing.\n"
335 "   :type buf: :class:`gpu.types.GPUVertBuf`\n"
336 "   :arg elem: An optional index buffer.\n"
337 "   :type elem: :class:`gpu.types.GPUIndexBuf`\n"
338 );
339 PyTypeObject BPyGPUBatch_Type = {
340         PyVarObject_HEAD_INIT(NULL, 0)
341         .tp_name = "GPUBatch",
342         .tp_basicsize = sizeof(BPyGPUBatch),
343         .tp_dealloc = (destructor)bpygpu_Batch_dealloc,
344 #ifdef USE_GPU_PY_REFERENCES
345         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
346         .tp_doc = py_gpu_batch_doc,
347         .tp_traverse = (traverseproc)bpygpu_Batch_traverse,
348         .tp_clear = (inquiry)bpygpu_Batch_clear,
349 #else
350         .tp_flags = Py_TPFLAGS_DEFAULT,
351 #endif
352         .tp_methods = bpygpu_Batch_methods,
353         .tp_new = bpygpu_Batch_new,
354 };
355
356 /** \} */
357
358
359 /* -------------------------------------------------------------------- */
360 /** \name Public API
361  * \{ */
362
363 PyObject *BPyGPUBatch_CreatePyObject(GPUBatch *batch)
364 {
365         BPyGPUBatch *self;
366
367 #ifdef USE_GPU_PY_REFERENCES
368         self = (BPyGPUBatch *)_PyObject_GC_New(&BPyGPUBatch_Type);
369         self->references = NULL;
370 #else
371         self = PyObject_New(BPyGPUBatch, &BPyGPUBatch_Type);
372 #endif
373
374         self->batch = batch;
375
376         return (PyObject *)self;
377 }
378
379 /** \} */
380
381 #undef BPY_GPU_BATCH_CHECK_OBJ