Committing patch "[#27676] Change window size/resolution in realtime" by me.
[blender-staging.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 /* python redefines */
36 #ifdef _POSIX_C_SOURCE
37 #undef _POSIX_C_SOURCE
38 #endif
39
40 #include <Python.h>
41
42 #include "GPU_material.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_image_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_lamp_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_ID.h"
50 #include "DNA_customdata_types.h"
51
52 #include "BLI_listbase.h"
53 #include "BLI_utildefines.h"
54
55 #include "RNA_access.h"
56
57 #include "bpy_rna.h"
58
59 #include "gpu.h"
60
61 #define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, #name, name)
62
63 PyDoc_STRVAR(M_gpu_doc,
64                          "This module provides access to the GLSL shader.");
65
66 static struct PyModuleDef gpumodule = {
67         PyModuleDef_HEAD_INIT,
68         "gpu",     /* name of module */
69         M_gpu_doc, /* module documentation */
70         -1,        /* size of per-interpreter state of the module,
71                                   or -1 if the module keeps state in global variables. */
72    NULL, NULL, NULL, NULL, NULL
73 };
74
75 PyMODINIT_FUNC
76 PyInit_gpu(void)
77 {
78         PyObject* m;
79
80         m = PyModule_Create(&gpumodule);
81         if (m == NULL)
82                 return NULL;
83
84         // device constants
85         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWMAT);
86         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_MAT);
87         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWIMAT);
88         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_IMAT);
89         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_COLOR);
90         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE);
91         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNVEC);
92         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
93         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
94         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
95         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
96         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
97         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
98         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
99         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);
100
101         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
102         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
103         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
104         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
105         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
106         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
107         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
108         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);
109
110         PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
111         PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
112         PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
113         PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
114         return m;
115 }
116
117 #define PY_DICT_ADD_STRING(d,s,f) \
118         val = PyUnicode_FromString(s->f);       \
119         PyDict_SetItemString(d, #f, val);       \
120         Py_DECREF(val)
121
122 #define PY_DICT_ADD_LONG(d,s,f) \
123         val = PyLong_FromLong(s->f);    \
124         PyDict_SetItemString(d, #f, val);       \
125         Py_DECREF(val)
126
127 #define PY_DICT_ADD_ID(d,s,f) \
128         RNA_id_pointer_create((struct ID*)s->f, &tptr); \
129         val = pyrna_struct_CreatePyObject(&tptr);       \
130         PyDict_SetItemString(d, #f, val);       \
131         Py_DECREF(val)
132
133 #define PY_OBJ_ADD_ID(d,s,f) \
134         val = PyUnicode_FromString(&s->f->id.name[2]);  \
135         PyObject_SetAttrString(d, #f, val);     \
136         Py_DECREF(val)
137
138 #define PY_OBJ_ADD_LONG(d,s,f) \
139         val = PyLong_FromLong(s->f);    \
140         PyObject_SetAttrString(d, #f, val);     \
141         Py_DECREF(val)
142
143 #define PY_OBJ_ADD_STRING(d,s,f) \
144         val = PyUnicode_FromString(s->f);       \
145         PyObject_SetAttrString(d, #f, val);     \
146         Py_DECREF(val)
147
148 PyDoc_STRVAR(GPU_export_shader_doc,
149 "export_shader(scene, material)\n"
150 "\n"
151 "   Returns the GLSL shader that produces the visual effect of material in scene.\n"
152 "\n"
153 "   :return: Dictionary defining the shader, uniforms and attributes.\n"
154 "   :rtype: Dict"
155 );
156 static PyObject* GPU_export_shader(PyObject* UNUSED(self), PyObject *args, PyObject *kwds)
157 {
158         PyObject* pyscene;
159         PyObject* pymat;
160         PyObject* as_pointer;
161         PyObject* pointer;
162         PyObject* result;
163         PyObject* dict;
164         PyObject* val;
165         PyObject* seq;
166
167         int i;
168         Scene *scene;
169         PointerRNA tptr;
170         Material *material;
171         GPUShaderExport *shader;
172         GPUInputUniform *uniform;
173         GPUInputAttribute *attribute;
174
175         static const char *kwlist[] = {"scene", "material", NULL};
176
177         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char**)(kwlist), &pyscene, &pymat))
178                 return NULL;
179
180         if (!strcmp(Py_TYPE(pyscene)->tp_name, "Scene") && 
181                 (as_pointer = PyObject_GetAttrString(pyscene, "as_pointer")) != NULL &&
182                 PyCallable_Check(as_pointer)) {
183                 // must be a scene object
184                 pointer = PyObject_CallObject(as_pointer, NULL);
185                 if (!pointer) {
186                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
187                         return NULL;
188                 }
189                 scene = (Scene*)PyLong_AsVoidPtr(pointer);
190                 Py_DECREF(pointer);
191                 if (!scene) {
192                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
193                         return NULL;
194                 }
195         }
196         else {
197                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() first argument should be of Scene type");
198                 return NULL;
199         }
200
201         if (!strcmp(Py_TYPE(pymat)->tp_name, "Material") && 
202                 (as_pointer = PyObject_GetAttrString(pymat, "as_pointer")) != NULL &&
203                 PyCallable_Check(as_pointer)) {
204                 // must be a material object
205                 pointer = PyObject_CallObject(as_pointer, NULL);
206                 if (!pointer) {
207                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
208                         return NULL;
209                 }
210                 material = (Material*)PyLong_AsVoidPtr(pointer);
211                 Py_DECREF(pointer);
212                 if (!material) {
213                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
214                         return NULL;
215                 }
216         }
217         else {
218                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() second argument should be of Material type");
219                 return NULL;
220         }
221         // we can call our internal function at last:
222         shader = GPU_shader_export(scene, material);
223         if (!shader) {
224                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
225                 return NULL;
226         }
227         // build a dictionary
228         result = PyDict_New();
229         if (shader->fragment) {
230                 PY_DICT_ADD_STRING(result,shader,fragment);
231         }
232         if (shader->vertex) {
233                 PY_DICT_ADD_STRING(result,shader,vertex);
234         }
235         seq = PyList_New(BLI_countlist(&shader->uniforms));
236         for (i=0, uniform=shader->uniforms.first; uniform; uniform=uniform->next, i++) {
237                 dict = PyDict_New();
238                 PY_DICT_ADD_STRING(dict,uniform,varname);
239                 PY_DICT_ADD_LONG(dict,uniform,datatype);
240                 PY_DICT_ADD_LONG(dict,uniform,type);
241                 if (uniform->lamp) {
242                         PY_DICT_ADD_ID(dict,uniform,lamp);
243                 }
244                 if (uniform->image) {
245                         PY_DICT_ADD_ID(dict,uniform,image);
246                 }
247                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
248                         uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
249                         uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW) {
250                         PY_DICT_ADD_LONG(dict,uniform,texnumber);
251                 }
252                 if (uniform->texpixels) {
253                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
254                         PyDict_SetItemString(dict, "texpixels", val);
255                         Py_DECREF(val);
256                         PY_DICT_ADD_LONG(dict,uniform,texsize);
257                 }
258                 PyList_SET_ITEM(seq, i, dict);
259         }
260         PyDict_SetItemString(result, "uniforms", seq);
261         Py_DECREF(seq);
262
263         seq = PyList_New(BLI_countlist(&shader->attributes));
264         for (i=0, attribute=shader->attributes.first; attribute; attribute=attribute->next, i++) {
265                 dict = PyDict_New();
266                 PY_DICT_ADD_STRING(dict,attribute,varname);
267                 PY_DICT_ADD_LONG(dict,attribute,datatype);
268                 PY_DICT_ADD_LONG(dict,attribute,type);
269                 PY_DICT_ADD_LONG(dict,attribute,number);
270                 if (attribute->name) {
271                         if (attribute->name[0] != 0) {
272                                 PY_DICT_ADD_STRING(dict,attribute,name);
273                         }
274                         else {
275                                 val = PyLong_FromLong(0);
276                                 PyDict_SetItemString(dict, "name", val);
277                                 Py_DECREF(val);
278                         }
279                 }
280                 PyList_SET_ITEM(seq, i, dict);
281         }
282         PyDict_SetItemString(result, "attributes", seq);
283         Py_DECREF(seq);
284
285         GPU_free_shader_export(shader);
286
287         return result;
288 }
289
290 static PyMethodDef meth_export_shader[] = {
291         {"export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS, GPU_export_shader_doc}
292 };
293
294 PyObject* GPU_initPython(void)
295 {
296         PyObject* module = PyInit_gpu();
297         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
298         PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);
299
300         return module;
301 }
302