svn merge ^/trunk/blender -r40950:40997, bmesh is in sync with head again
[blender.git] / source / blender / python / intern / gpu.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Benoit Bolsee.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/python/intern/gpu.c
31  *  \ingroup pythonintern
32  */
33
34 /* python redefines */
35 #ifdef _POSIX_C_SOURCE
36 #undef _POSIX_C_SOURCE
37 #endif
38
39 #include <Python.h>
40
41 #include "GPU_material.h"
42
43 #include "DNA_scene_types.h"
44 #include "DNA_image_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_ID.h"
49 #include "DNA_customdata_types.h"
50
51 #include "BLI_listbase.h"
52 #include "BLI_utildefines.h"
53
54 #include "RNA_access.h"
55
56 #include "bpy_rna.h"
57
58 #include "gpu.h"
59
60 #define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, #name, name)
61
62 PyDoc_STRVAR(M_gpu_doc,
63                          "This module provides access to the GLSL shader.");
64
65 static struct PyModuleDef gpumodule = {
66         PyModuleDef_HEAD_INIT,
67         "gpu",     /* name of module */
68         M_gpu_doc, /* module documentation */
69         -1,        /* size of per-interpreter state of the module,
70                                   or -1 if the module keeps state in global variables. */
71    NULL, NULL, NULL, NULL, NULL
72 };
73
74 PyMODINIT_FUNC
75 PyInit_gpu(void)
76 {
77         PyObject* m;
78
79         m = PyModule_Create(&gpumodule);
80         if (m == NULL)
81                 return NULL;
82
83         // device constants
84         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWMAT);
85         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_MAT);
86         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWIMAT);
87         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_IMAT);
88         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_COLOR);
89         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNVEC);
90         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
91         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
92         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
93         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
94         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
95         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
96         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
97         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);
98
99         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
100         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
101         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
102         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
103         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
104         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
105         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
106         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);
107
108         PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
109         PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
110         PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
111         PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
112         return m;
113 }
114
115 #define PY_DICT_ADD_STRING(d,s,f) \
116         val = PyUnicode_FromString(s->f);       \
117         PyDict_SetItemString(d, #f, val);       \
118         Py_DECREF(val)
119
120 #define PY_DICT_ADD_LONG(d,s,f) \
121         val = PyLong_FromLong(s->f);    \
122         PyDict_SetItemString(d, #f, val);       \
123         Py_DECREF(val)
124
125 #define PY_DICT_ADD_ID(d,s,f) \
126         RNA_id_pointer_create((struct ID*)s->f, &tptr); \
127         val = pyrna_struct_CreatePyObject(&tptr);       \
128         PyDict_SetItemString(d, #f, val);       \
129         Py_DECREF(val)
130
131 #define PY_OBJ_ADD_ID(d,s,f) \
132         val = PyUnicode_FromString(&s->f->id.name[2]);  \
133         PyObject_SetAttrString(d, #f, val);     \
134         Py_DECREF(val)
135
136 #define PY_OBJ_ADD_LONG(d,s,f) \
137         val = PyLong_FromLong(s->f);    \
138         PyObject_SetAttrString(d, #f, val);     \
139         Py_DECREF(val)
140
141 #define PY_OBJ_ADD_STRING(d,s,f) \
142         val = PyUnicode_FromString(s->f);       \
143         PyObject_SetAttrString(d, #f, val);     \
144         Py_DECREF(val)
145
146 static PyObject* GPU_export_shader(PyObject* UNUSED(self), PyObject *args, PyObject *kwds)
147 {
148         PyObject* pyscene;
149         PyObject* pymat;
150         PyObject* as_pointer;
151         PyObject* pointer;
152         PyObject* result;
153         PyObject* dict;
154         PyObject* val;
155         PyObject* seq;
156
157         int i;
158         Scene *scene;
159         PointerRNA tptr;
160         Material *material;
161         GPUShaderExport *shader;
162         GPUInputUniform *uniform;
163         GPUInputAttribute *attribute;
164
165         static const char *kwlist[] = {"scene", "material", NULL};
166
167         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char**)(kwlist), &pyscene, &pymat))
168                 return NULL;
169
170         if (!strcmp(Py_TYPE(pyscene)->tp_name, "Scene") && 
171                 (as_pointer = PyObject_GetAttrString(pyscene, "as_pointer")) != NULL &&
172                 PyCallable_Check(as_pointer)) {
173                 // must be a scene object
174                 pointer = PyObject_CallObject(as_pointer, NULL);
175                 if (!pointer) {
176                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
177                         return NULL;
178                 }
179                 scene = (Scene*)PyLong_AsVoidPtr(pointer);
180                 Py_DECREF(pointer);
181                 if (!scene) {
182                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
183                         return NULL;
184                 }
185         } else {
186                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() first argument should be of Scene type");
187                 return NULL;
188         }
189
190         if (!strcmp(Py_TYPE(pymat)->tp_name, "Material") && 
191                 (as_pointer = PyObject_GetAttrString(pymat, "as_pointer")) != NULL &&
192                 PyCallable_Check(as_pointer)) {
193                 // must be a material object
194                 pointer = PyObject_CallObject(as_pointer, NULL);
195                 if (!pointer) {
196                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
197                         return NULL;
198                 }
199                 material = (Material*)PyLong_AsVoidPtr(pointer);
200                 Py_DECREF(pointer);
201                 if (!material) {
202                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
203                         return NULL;
204                 }
205         } else {
206                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() second argument should be of Material type");
207                 return NULL;
208         }
209         // we can call our internal function at last:
210         shader = GPU_shader_export(scene, material);
211         if (!shader) {
212                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
213                 return NULL;
214         }
215         // build a dictionary
216         result = PyDict_New();
217         if (shader->fragment) {
218                 PY_DICT_ADD_STRING(result,shader,fragment);
219         }
220         if (shader->vertex) {
221                 PY_DICT_ADD_STRING(result,shader,vertex);
222         }
223         seq = PyList_New(BLI_countlist(&shader->uniforms));
224         for (i=0, uniform=shader->uniforms.first; uniform; uniform=uniform->next, i++) {
225                 dict = PyDict_New();
226                 PY_DICT_ADD_STRING(dict,uniform,varname);
227                 PY_DICT_ADD_LONG(dict,uniform,datatype);
228                 PY_DICT_ADD_LONG(dict,uniform,type);
229                 if (uniform->lamp) {
230                         PY_DICT_ADD_ID(dict,uniform,lamp);
231                 }
232                 if (uniform->image) {
233                         PY_DICT_ADD_ID(dict,uniform,image);
234                 }
235                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
236                         uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
237                         uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW) {
238                         PY_DICT_ADD_LONG(dict,uniform,texnumber);
239                 }
240                 if (uniform->texpixels) {
241                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize);
242                         PyDict_SetItemString(dict, "texpixels", val);
243                         Py_DECREF(val);
244                         PY_DICT_ADD_LONG(dict,uniform,texsize);
245                 }
246                 PyList_SET_ITEM(seq, i, dict);
247         }
248         PyDict_SetItemString(result, "uniforms", seq);
249         Py_DECREF(seq);
250
251         seq = PyList_New(BLI_countlist(&shader->attributes));
252         for (i=0, attribute=shader->attributes.first; attribute; attribute=attribute->next, i++) {
253                 dict = PyDict_New();
254                 PY_DICT_ADD_STRING(dict,attribute,varname);
255                 PY_DICT_ADD_LONG(dict,attribute,datatype);
256                 PY_DICT_ADD_LONG(dict,attribute,type);
257                 PY_DICT_ADD_LONG(dict,attribute,number);
258                 if (attribute->name) {
259                         if (attribute->name[0] != 0) {
260                                 PY_DICT_ADD_STRING(dict,attribute,name);
261                         } else {
262                                 val = PyLong_FromLong(0);
263                                 PyDict_SetItemString(dict, "name", val);
264                                 Py_DECREF(val);
265                         }
266                 }
267                 PyList_SET_ITEM(seq, i, dict);
268         }
269         PyDict_SetItemString(result, "attributes", seq);
270         Py_DECREF(seq);
271
272         GPU_free_shader_export(shader);
273
274         return result;
275 }
276
277 static PyMethodDef meth_export_shader[] = {{ "export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS,
278                                                                                   "export_shader(scene,material)\n\n"
279                                                                                   "Returns the GLSL shader that produces the visual effect of material in scene.\n\n"
280                                                                                   ":return: Dictionary defining the shader, uniforms and attributes.\n"
281                                                                                   ":rtype: Dict"}};
282
283 PyObject* GPU_initPython(void)
284 {
285         PyObject* module = PyInit_gpu();
286         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
287         PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);
288
289         return module;
290 }
291