documentation - brief descriptions for bpy api files.
[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_LAMP_DYNVEC);
91         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
92         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
93         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
94         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
95         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
96         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
97         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
98         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);
99
100         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
101         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
102         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
103         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
104         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
105         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
106         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
107         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);
108
109         PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
110         PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
111         PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
112         PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
113         return m;
114 }
115
116 #define PY_DICT_ADD_STRING(d,s,f) \
117         val = PyUnicode_FromString(s->f);       \
118         PyDict_SetItemString(d, #f, val);       \
119         Py_DECREF(val)
120
121 #define PY_DICT_ADD_LONG(d,s,f) \
122         val = PyLong_FromLong(s->f);    \
123         PyDict_SetItemString(d, #f, val);       \
124         Py_DECREF(val)
125
126 #define PY_DICT_ADD_ID(d,s,f) \
127         RNA_id_pointer_create((struct ID*)s->f, &tptr); \
128         val = pyrna_struct_CreatePyObject(&tptr);       \
129         PyDict_SetItemString(d, #f, val);       \
130         Py_DECREF(val)
131
132 #define PY_OBJ_ADD_ID(d,s,f) \
133         val = PyUnicode_FromString(&s->f->id.name[2]);  \
134         PyObject_SetAttrString(d, #f, val);     \
135         Py_DECREF(val)
136
137 #define PY_OBJ_ADD_LONG(d,s,f) \
138         val = PyLong_FromLong(s->f);    \
139         PyObject_SetAttrString(d, #f, val);     \
140         Py_DECREF(val)
141
142 #define PY_OBJ_ADD_STRING(d,s,f) \
143         val = PyUnicode_FromString(s->f);       \
144         PyObject_SetAttrString(d, #f, val);     \
145         Py_DECREF(val)
146
147 static PyObject* GPU_export_shader(PyObject* UNUSED(self), PyObject *args, PyObject *kwds)
148 {
149         PyObject* pyscene;
150         PyObject* pymat;
151         PyObject* as_pointer;
152         PyObject* pointer;
153         PyObject* result;
154         PyObject* dict;
155         PyObject* val;
156         PyObject* seq;
157
158         int i;
159         Scene *scene;
160         PointerRNA tptr;
161         Material *material;
162         GPUShaderExport *shader;
163         GPUInputUniform *uniform;
164         GPUInputAttribute *attribute;
165
166         static const char *kwlist[] = {"scene", "material", NULL};
167
168         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char**)(kwlist), &pyscene, &pymat))
169                 return NULL;
170
171         if (!strcmp(Py_TYPE(pyscene)->tp_name, "Scene") && 
172                 (as_pointer = PyObject_GetAttrString(pyscene, "as_pointer")) != NULL &&
173                 PyCallable_Check(as_pointer)) {
174                 // must be a scene object
175                 pointer = PyObject_CallObject(as_pointer, NULL);
176                 if (!pointer) {
177                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
178                         return NULL;
179                 }
180                 scene = (Scene*)PyLong_AsVoidPtr(pointer);
181                 Py_DECREF(pointer);
182                 if (!scene) {
183                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
184                         return NULL;
185                 }
186         } else {
187                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() first argument should be of Scene type");
188                 return NULL;
189         }
190
191         if (!strcmp(Py_TYPE(pymat)->tp_name, "Material") && 
192                 (as_pointer = PyObject_GetAttrString(pymat, "as_pointer")) != NULL &&
193                 PyCallable_Check(as_pointer)) {
194                 // must be a material object
195                 pointer = PyObject_CallObject(as_pointer, NULL);
196                 if (!pointer) {
197                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
198                         return NULL;
199                 }
200                 material = (Material*)PyLong_AsVoidPtr(pointer);
201                 Py_DECREF(pointer);
202                 if (!material) {
203                         PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
204                         return NULL;
205                 }
206         } else {
207                 PyErr_SetString(PyExc_TypeError, "gpu.export_shader() second argument should be of Material type");
208                 return NULL;
209         }
210         // we can call our internal function at last:
211         shader = GPU_shader_export(scene, material);
212         if (!shader) {
213                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
214                 return NULL;
215         }
216         // build a dictionary
217         result = PyDict_New();
218         if (shader->fragment) {
219                 PY_DICT_ADD_STRING(result,shader,fragment);
220         }
221         if (shader->vertex) {
222                 PY_DICT_ADD_STRING(result,shader,vertex);
223         }
224         seq = PyList_New(BLI_countlist(&shader->uniforms));
225         for (i=0, uniform=shader->uniforms.first; uniform; uniform=uniform->next, i++) {
226                 dict = PyDict_New();
227                 PY_DICT_ADD_STRING(dict,uniform,varname);
228                 PY_DICT_ADD_LONG(dict,uniform,datatype);
229                 PY_DICT_ADD_LONG(dict,uniform,type);
230                 if (uniform->lamp) {
231                         PY_DICT_ADD_ID(dict,uniform,lamp);
232                 }
233                 if (uniform->image) {
234                         PY_DICT_ADD_ID(dict,uniform,image);
235                 }
236                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
237                         uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
238                         uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW) {
239                         PY_DICT_ADD_LONG(dict,uniform,texnumber);
240                 }
241                 if (uniform->texpixels) {
242                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
243                         PyDict_SetItemString(dict, "texpixels", val);
244                         Py_DECREF(val);
245                         PY_DICT_ADD_LONG(dict,uniform,texsize);
246                 }
247                 PyList_SET_ITEM(seq, i, dict);
248         }
249         PyDict_SetItemString(result, "uniforms", seq);
250         Py_DECREF(seq);
251
252         seq = PyList_New(BLI_countlist(&shader->attributes));
253         for (i=0, attribute=shader->attributes.first; attribute; attribute=attribute->next, i++) {
254                 dict = PyDict_New();
255                 PY_DICT_ADD_STRING(dict,attribute,varname);
256                 PY_DICT_ADD_LONG(dict,attribute,datatype);
257                 PY_DICT_ADD_LONG(dict,attribute,type);
258                 PY_DICT_ADD_LONG(dict,attribute,number);
259                 if (attribute->name) {
260                         if (attribute->name[0] != 0) {
261                                 PY_DICT_ADD_STRING(dict,attribute,name);
262                         } else {
263                                 val = PyLong_FromLong(0);
264                                 PyDict_SetItemString(dict, "name", val);
265                                 Py_DECREF(val);
266                         }
267                 }
268                 PyList_SET_ITEM(seq, i, dict);
269         }
270         PyDict_SetItemString(result, "attributes", seq);
271         Py_DECREF(seq);
272
273         GPU_free_shader_export(shader);
274
275         return result;
276 }
277
278 static PyMethodDef meth_export_shader[] = {{ "export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS,
279                                                                                   "export_shader(scene,material)\n\n"
280                                                                                   "Returns the GLSL shader that produces the visual effect of material in scene.\n\n"
281                                                                                   ":return: Dictionary defining the shader, uniforms and attributes.\n"
282                                                                                   ":rtype: Dict"}};
283
284 PyObject* GPU_initPython(void)
285 {
286         PyObject* module = PyInit_gpu();
287         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
288         PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);
289
290         return module;
291 }
292