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