Py-Driver: add 'self' option
[blender.git] / source / blender / python / intern / gpu.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  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Benoit Bolsee.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/python/intern/gpu.c
29  *  \ingroup pythonintern
30  *
31  * This file defines the 'gpu' module, used to get GLSL shader code and data
32  * from blender materials.
33  */
34
35 #include <Python.h>
36
37 #include "DNA_scene_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_ID.h"
40 #include "DNA_customdata_types.h"
41
42 #include "BLI_listbase.h"
43 #include "BLI_utildefines.h"
44
45 #include "RNA_access.h"
46
47 #include "bpy_rna.h"
48
49 #include "../generic/py_capi_utils.h"
50
51 #include "GPU_material.h"
52
53 #include "gpu.h"
54
55 #define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, # name, name)
56
57 PyDoc_STRVAR(M_gpu_doc,
58 "This module provides access to the GLSL shader and Offscreen rendering functionalities."
59 );
60 static struct PyModuleDef gpumodule = {
61         PyModuleDef_HEAD_INIT,
62         "gpu",     /* name of module */
63         M_gpu_doc, /* module documentation */
64         -1,        /* size of per-interpreter state of the module,
65                     *  or -1 if the module keeps state in global variables. */
66         NULL, NULL, NULL, NULL, NULL
67 };
68
69 static PyObject *PyInit_gpu(void)
70 {
71         PyObject *m;
72
73         m = PyModule_Create(&gpumodule);
74         if (m == NULL)
75                 return NULL;
76
77
78         /* Take care to update docs when editing: 'doc/python_api/rst/gpu.rst' */
79
80
81         /* -------------------------------------------------------------------- */
82         /* GPUDynamicType */
83
84         /* device constant groups */
85         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_MISC);
86         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_LAMP);
87         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_OBJECT);
88         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_SAMPLER);
89         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_MIST);
90         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_WORLD);
91         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_GROUP_MAT);
92
93         /* device constants */
94         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_NONE);
95         /* GPU_DYNAMIC_GROUP_OBJECT */
96         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWMAT);
97         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_MAT);
98         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWIMAT);
99         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_IMAT);
100         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_LOCTOVIEWMAT);
101         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_LOCTOVIEWIMAT);
102         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_COLOR);
103         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE);
104         /* GPU_DYNAMIC_GROUP_LAMP */
105         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNVEC);
106         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
107         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
108         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
109         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
110         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
111         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_ATT1);
112         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_ATT2);
113         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DISTANCE);
114         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_SPOTSIZE);
115         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_SPOTBLEND);
116         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_SPOTSCALE);
117         /* GPU_DYNAMIC_GROUP_SAMPLER */
118         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
119         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
120         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);
121         /* GPU_DYNAMIC_GROUP_MIST */
122         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_ENABLE);
123         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_START);
124         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_DISTANCE);
125         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_INTENSITY);
126         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_TYPE);
127         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MIST_COLOR);
128         /* GPU_DYNAMIC_GROUP_WORLD */
129         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_HORIZON_COLOR);
130         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_AMBIENT_COLOR);
131         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_ZENITH_COLOR);
132         /* GPU_DYNAMIC_GROUP_MAT */
133         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_DIFFRGB);
134         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_REF);
135         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_SPECRGB);
136         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_SPEC);
137         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_HARD);
138         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_EMIT);
139         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_AMB);
140         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_MAT_ALPHA);
141
142
143         /* -------------------------------------------------------------------- */
144         /* GPUDataType */
145
146         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
147         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
148         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
149         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
150         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
151         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
152         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
153         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);
154
155
156         /* -------------------------------------------------------------------- */
157         /* CustomDataType
158          *
159          * Intentionally only include the subset used by the GPU API.
160          */
161         PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
162         PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
163         PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
164         PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
165         return m;
166 }
167
168 #define PY_DICT_ADD_STRING(d, s, f)      \
169         val = PyUnicode_FromString(s->f);    \
170         PyDict_SetItemString(d, # f, val);   \
171         Py_DECREF(val)
172
173 #define PY_DICT_ADD_LONG(d, s, f)        \
174         val = PyLong_FromLong(s->f);         \
175         PyDict_SetItemString(d, # f, val);   \
176         Py_DECREF(val)
177
178 #define PY_DICT_ADD_ID(d, s, f)                      \
179         RNA_id_pointer_create((struct ID *)s->f, &tptr); \
180         val = pyrna_struct_CreatePyObject(&tptr);        \
181         PyDict_SetItemString(d, # f, val);               \
182         Py_DECREF(val)
183
184 #if 0  /* UNUSED */
185 #define PY_OBJ_ADD_ID(d, s, f)                      \
186         val = PyUnicode_FromString(&s->f->id.name[2]);  \
187         PyObject_SetAttrString(d, # f, val);            \
188         Py_DECREF(val)
189
190 #define PY_OBJ_ADD_LONG(d, s, f)         \
191         val = PyLong_FromLong(s->f);         \
192         PyObject_SetAttrString(d, # f, val); \
193         Py_DECREF(val)
194
195 #define PY_OBJ_ADD_STRING(d, s, f)       \
196         val = PyUnicode_FromString(s->f);    \
197         PyObject_SetAttrString(d, # f, val); \
198         Py_DECREF(val)
199 #endif
200
201 PyDoc_STRVAR(GPU_export_shader_doc,
202 "export_shader(scene, material)\n"
203 "\n"
204 "   Returns the GLSL shader that produces the visual effect of material in scene.\n"
205 "\n"
206 "   :return: Dictionary defining the shader, uniforms and attributes.\n"
207 "   :rtype: Dict"
208 );
209 static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
210 {
211         PyObject *pyscene;
212         PyObject *pymat;
213         PyObject *result;
214         PyObject *dict;
215         PyObject *val;
216         PyObject *seq;
217
218         int i;
219         Scene *scene;
220         PointerRNA tptr;
221         Material *material;
222         GPUShaderExport *shader;
223         GPUInputUniform *uniform;
224         GPUInputAttribute *attribute;
225
226         static const char *kwlist[] = {"scene", "material", NULL};
227
228         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char **)(kwlist), &pyscene, &pymat))
229                 return NULL;
230
231         scene = (Scene *)PyC_RNA_AsPointer(pyscene, "Scene");
232         if (scene == NULL) {
233                 return NULL;
234         }
235
236         material = (Material *)PyC_RNA_AsPointer(pymat, "Material");
237         if (material == NULL) {
238                 return NULL;
239         }
240
241         /* we can call our internal function at last: */
242         shader = GPU_shader_export(scene, material);
243         if (!shader) {
244                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
245                 return NULL;
246         }
247         /* build a dictionary */
248         result = PyDict_New();
249         if (shader->fragment) {
250                 PY_DICT_ADD_STRING(result, shader, fragment);
251         }
252         if (shader->vertex) {
253                 PY_DICT_ADD_STRING(result, shader, vertex);
254         }
255         seq = PyList_New(BLI_listbase_count(&shader->uniforms));
256         for (i = 0, uniform = shader->uniforms.first; uniform; uniform = uniform->next, i++) {
257                 dict = PyDict_New();
258                 PY_DICT_ADD_STRING(dict, uniform, varname);
259                 PY_DICT_ADD_LONG(dict, uniform, datatype);
260                 PY_DICT_ADD_LONG(dict, uniform, type);
261                 if (uniform->lamp) {
262                         PY_DICT_ADD_ID(dict, uniform, lamp);
263                 }
264                 if (uniform->material) {
265                         PY_DICT_ADD_ID(dict, uniform, material);
266                 }
267                 if (uniform->image) {
268                         PY_DICT_ADD_ID(dict, uniform, image);
269                 }
270                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
271                     uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
272                     uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW)
273                 {
274                         PY_DICT_ADD_LONG(dict, uniform, texnumber);
275                 }
276                 if (uniform->texpixels) {
277                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
278                         PyDict_SetItemString(dict, "texpixels", val);
279                         Py_DECREF(val);
280                         PY_DICT_ADD_LONG(dict, uniform, texsize);
281                 }
282                 PyList_SET_ITEM(seq, i, dict);
283         }
284         PyDict_SetItemString(result, "uniforms", seq);
285         Py_DECREF(seq);
286
287         seq = PyList_New(BLI_listbase_count(&shader->attributes));
288         for (i = 0, attribute = shader->attributes.first; attribute; attribute = attribute->next, i++) {
289                 dict = PyDict_New();
290                 PY_DICT_ADD_STRING(dict, attribute, varname);
291                 PY_DICT_ADD_LONG(dict, attribute, datatype);
292                 PY_DICT_ADD_LONG(dict, attribute, type);
293                 PY_DICT_ADD_LONG(dict, attribute, number);
294                 if (attribute->name) {
295                         if (attribute->name[0] != 0) {
296                                 PY_DICT_ADD_STRING(dict, attribute, name);
297                         }
298                         else {
299                                 val = PyLong_FromLong(0);
300                                 PyDict_SetItemString(dict, "name", val);
301                                 Py_DECREF(val);
302                         }
303                 }
304                 PyList_SET_ITEM(seq, i, dict);
305         }
306         PyDict_SetItemString(result, "attributes", seq);
307         Py_DECREF(seq);
308
309         GPU_free_shader_export(shader);
310
311         return result;
312 }
313
314 static PyMethodDef meth_export_shader[] = {
315         {"export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS, GPU_export_shader_doc}
316 };
317
318 /* -------------------------------------------------------------------- */
319 /* Initialize Module */
320
321 PyObject *GPU_initPython(void)
322 {
323         PyObject *module;
324         PyObject *submodule;
325         PyObject *sys_modules = PyThreadState_GET()->interp->modules;
326
327         module = PyInit_gpu();
328
329         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
330
331         /* gpu.offscreen */
332         PyModule_AddObject(module, "offscreen", (submodule = BPyInit_gpu_offscreen()));
333         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
334         Py_INCREF(submodule);
335
336         PyDict_SetItem(PyImport_GetModuleDict(), PyModule_GetNameObject(module), module);
337         return module;
338 }
339