Merging r44227 through r45619 from trunk into soc-2011-tomato
[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 "../generic/py_capi_utils.h"
60
61 #include "gpu.h"
62
63 #define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, # name, name)
64
65 PyDoc_STRVAR(M_gpu_doc,
66 "This module provides access to the GLSL shader."
67 );
68 static struct PyModuleDef gpumodule = {
69         PyModuleDef_HEAD_INIT,
70         "gpu",     /* name of module */
71         M_gpu_doc, /* module documentation */
72         -1,        /* size of per-interpreter state of the module,
73                     *  or -1 if the module keeps state in global variables. */
74         NULL, NULL, NULL, NULL, NULL
75 };
76
77 PyMODINIT_FUNC
78 PyInit_gpu(void)
79 {
80         PyObject *m;
81
82         m = PyModule_Create(&gpumodule);
83         if (m == NULL)
84                 return NULL;
85
86         // device constants
87         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWMAT);
88         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_MAT);
89         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWIMAT);
90         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_IMAT);
91         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_COLOR);
92         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE);
93         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNVEC);
94         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
95         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
96         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
97         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
98         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
99         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
100         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
101         PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);
102
103         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
104         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
105         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
106         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
107         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
108         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
109         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
110         PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);
111
112         PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
113         PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
114         PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
115         PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
116         return m;
117 }
118
119 #define PY_DICT_ADD_STRING(d, s, f)      \
120         val = PyUnicode_FromString(s->f);    \
121         PyDict_SetItemString(d, # f, val);   \
122         Py_DECREF(val)
123
124 #define PY_DICT_ADD_LONG(d, s, f)        \
125         val = PyLong_FromLong(s->f);         \
126         PyDict_SetItemString(d, # f, val);   \
127         Py_DECREF(val)
128
129 #define PY_DICT_ADD_ID(d, s, f)                      \
130         RNA_id_pointer_create((struct ID *)s->f, &tptr); \
131         val = pyrna_struct_CreatePyObject(&tptr);        \
132         PyDict_SetItemString(d, # f, val);               \
133         Py_DECREF(val)
134
135 #define PY_OBJ_ADD_ID(d, s, f)                      \
136         val = PyUnicode_FromString(&s->f->id.name[2]);  \
137         PyObject_SetAttrString(d, # f, val);            \
138         Py_DECREF(val)
139
140 #define PY_OBJ_ADD_LONG(d, s, f)         \
141         val = PyLong_FromLong(s->f);         \
142         PyObject_SetAttrString(d, # f, val); \
143         Py_DECREF(val)
144
145 #define PY_OBJ_ADD_STRING(d, s, f)       \
146         val = PyUnicode_FromString(s->f);    \
147         PyObject_SetAttrString(d, # f, val); \
148         Py_DECREF(val)
149
150 PyDoc_STRVAR(GPU_export_shader_doc,
151 "export_shader(scene, material)\n"
152 "\n"
153 "   Returns the GLSL shader that produces the visual effect of material in scene.\n"
154 "\n"
155 "   :return: Dictionary defining the shader, uniforms and attributes.\n"
156 "   :rtype: Dict"
157 );
158 static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
159 {
160         PyObject *pyscene;
161         PyObject *pymat;
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         scene = (Scene *)PyC_RNA_AsPointer(pyscene, "Scene");
181         if (scene == NULL) {
182                 return NULL;
183         }
184
185         material = (Material *)PyC_RNA_AsPointer(pymat, "Material");
186         if (material == NULL) {
187                 return NULL;
188         }
189
190         // we can call our internal function at last:
191         shader = GPU_shader_export(scene, material);
192         if (!shader) {
193                 PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
194                 return NULL;
195         }
196         // build a dictionary
197         result = PyDict_New();
198         if (shader->fragment) {
199                 PY_DICT_ADD_STRING(result, shader, fragment);
200         }
201         if (shader->vertex) {
202                 PY_DICT_ADD_STRING(result, shader, vertex);
203         }
204         seq = PyList_New(BLI_countlist(&shader->uniforms));
205         for (i = 0, uniform = shader->uniforms.first; uniform; uniform = uniform->next, i++) {
206                 dict = PyDict_New();
207                 PY_DICT_ADD_STRING(dict, uniform, varname);
208                 PY_DICT_ADD_LONG(dict, uniform, datatype);
209                 PY_DICT_ADD_LONG(dict, uniform, type);
210                 if (uniform->lamp) {
211                         PY_DICT_ADD_ID(dict, uniform, lamp);
212                 }
213                 if (uniform->image) {
214                         PY_DICT_ADD_ID(dict, uniform, image);
215                 }
216                 if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
217                     uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
218                     uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW)
219                 {
220                         PY_DICT_ADD_LONG(dict, uniform, texnumber);
221                 }
222                 if (uniform->texpixels) {
223                         val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
224                         PyDict_SetItemString(dict, "texpixels", val);
225                         Py_DECREF(val);
226                         PY_DICT_ADD_LONG(dict, uniform, texsize);
227                 }
228                 PyList_SET_ITEM(seq, i, dict);
229         }
230         PyDict_SetItemString(result, "uniforms", seq);
231         Py_DECREF(seq);
232
233         seq = PyList_New(BLI_countlist(&shader->attributes));
234         for (i = 0, attribute = shader->attributes.first; attribute; attribute = attribute->next, i++) {
235                 dict = PyDict_New();
236                 PY_DICT_ADD_STRING(dict, attribute, varname);
237                 PY_DICT_ADD_LONG(dict, attribute, datatype);
238                 PY_DICT_ADD_LONG(dict, attribute, type);
239                 PY_DICT_ADD_LONG(dict, attribute, number);
240                 if (attribute->name) {
241                         if (attribute->name[0] != 0) {
242                                 PY_DICT_ADD_STRING(dict, attribute, name);
243                         }
244                         else {
245                                 val = PyLong_FromLong(0);
246                                 PyDict_SetItemString(dict, "name", val);
247                                 Py_DECREF(val);
248                         }
249                 }
250                 PyList_SET_ITEM(seq, i, dict);
251         }
252         PyDict_SetItemString(result, "attributes", seq);
253         Py_DECREF(seq);
254
255         GPU_free_shader_export(shader);
256
257         return result;
258 }
259
260 static PyMethodDef meth_export_shader[] = {
261         {"export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS, GPU_export_shader_doc}
262 };
263
264 PyObject *GPU_initPython(void)
265 {
266         PyObject *module = PyInit_gpu();
267         PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
268         PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);
269
270         return module;
271 }
272