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