formatting edits in py api, no functional changes
[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 /* 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 static PyObject* GPU_export_shader(PyObject* UNUSED(self), PyObject *args, PyObject *kwds)
149 {
150         PyObject* pyscene;
151         PyObject* pymat;
152         PyObject* as_pointer;
153         PyObject* pointer;
154         PyObject* result;
155         PyObject* dict;
156         PyObject* val;
157         PyObject* seq;
158
159         int i;
160         Scene *scene;
161         PointerRNA tptr;
162         Material *material;
163         GPUShaderExport *shader;
164         GPUInputUniform *uniform;
165         GPUInputAttribute *attribute;
166
167         static const char *kwlist[] = {"scene", "material", NULL};
168
169         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char**)(kwlist), &pyscene, &pymat))
170                 return NULL;
171
172         if (!strcmp(Py_TYPE(pyscene)->tp_name, "Scene") && 
173                 (as_pointer = PyObject_GetAttrString(pyscene, "as_pointer")) != NULL &&
174                 PyCallable_Check(as_pointer)) {
175                 // must be a scene object
176                 pointer = PyObject_CallObject(as_pointer, NULL);
177                 if (!pointer) {
178                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
179                         return NULL;
180                 }
181                 scene = (Scene*)PyLong_AsVoidPtr(pointer);
182                 Py_DECREF(pointer);
183                 if (!scene) {
184                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
185                         return NULL;
186                 }
187         }
188         else {
189                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() first argument should be of Scene type");
190                 return NULL;
191         }
192
193         if (!strcmp(Py_TYPE(pymat)->tp_name, "Material") && 
194                 (as_pointer = PyObject_GetAttrString(pymat, "as_pointer")) != NULL &&
195                 PyCallable_Check(as_pointer)) {
196                 // must be a material object
197                 pointer = PyObject_CallObject(as_pointer, NULL);
198                 if (!pointer) {
199                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
200                         return NULL;
201                 }
202                 material = (Material*)PyLong_AsVoidPtr(pointer);
203                 Py_DECREF(pointer);
204                 if (!material) {
205                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
206                         return NULL;
207                 }
208         }
209         else {
210                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() second argument should be of Material type");
211                 return NULL;
212         }
213         // we can call our internal function at last:
214         shader = GPU_shader_export(scene, material);
215         if (!shader) {
216                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
217                 return NULL;
218         }
219         // build a dictionary
220         result = PyDict_New();
221         if (shader->fragment) {
222                 PY_DICT_ADD_STRING(result,shader,fragment);
223         }
224         if (shader->vertex) {
225                 PY_DICT_ADD_STRING(result,shader,vertex);
226         }
227         seq = PyList_New(BLI_countlist(&shader->uniforms));
228         for (i=0, uniform=shader->uniforms.first; uniform; uniform=uniform->next, i++) {
229                 dict = PyDict_New();
230                 PY_DICT_ADD_STRING(dict,uniform,varname);
231                 PY_DICT_ADD_LONG(dict,uniform,datatype);
232                 PY_DICT_ADD_LONG(dict,uniform,type);
233                 if (uniform->lamp) {
234                         PY_DICT_ADD_ID(dict,uniform,lamp);
235                 }
236                 if (uniform->image) {
237                         PY_DICT_ADD_ID(dict,uniform,image);
238                 }
239                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
240                         uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
241                         uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW) {
242                         PY_DICT_ADD_LONG(dict,uniform,texnumber);
243                 }
244                 if (uniform->texpixels) {
245                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
246                         PyDict_SetItemString(dict, "texpixels", val);
247                         Py_DECREF(val);
248                         PY_DICT_ADD_LONG(dict,uniform,texsize);
249                 }
250                 PyList_SET_ITEM(seq, i, dict);
251         }
252         PyDict_SetItemString(result, "uniforms", seq);
253         Py_DECREF(seq);
254
255         seq = PyList_New(BLI_countlist(&shader->attributes));
256         for (i=0, attribute=shader->attributes.first; attribute; attribute=attribute->next, i++) {
257                 dict = PyDict_New();
258                 PY_DICT_ADD_STRING(dict,attribute,varname);
259                 PY_DICT_ADD_LONG(dict,attribute,datatype);
260                 PY_DICT_ADD_LONG(dict,attribute,type);
261                 PY_DICT_ADD_LONG(dict,attribute,number);
262                 if (attribute->name) {
263                         if (attribute->name[0] != 0) {
264                                 PY_DICT_ADD_STRING(dict,attribute,name);
265                         }
266                         else {
267                                 val = PyLong_FromLong(0);
268                                 PyDict_SetItemString(dict, "name", val);
269                                 Py_DECREF(val);
270                         }
271                 }
272                 PyList_SET_ITEM(seq, i, dict);
273         }
274         PyDict_SetItemString(result, "attributes", seq);
275         Py_DECREF(seq);
276
277         GPU_free_shader_export(shader);
278
279         return result;
280 }
281
282 static PyMethodDef meth_export_shader[] = {{ "export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS,
283                                                                                   "export_shader(scene,material)\n\n"
284                                                                                   "Returns the GLSL shader that produces the visual effect of material in scene.\n\n"
285                                                                                   ":return: Dictionary defining the shader, uniforms and attributes.\n"
286                                                                                   ":rtype: Dict"}};
287
288 PyObject* GPU_initPython(void)
289 {
290         PyObject* module = PyInit_gpu();
291         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
292         PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);
293
294         return module;
295 }
296