Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu.c « intern « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4580e8035bd9a870baa732f51364c3f702bb9ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Benoit Bolsee.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/python/intern/gpu.c
 *  \ingroup pythonintern
 *
 * This file defines the 'gpu' module, used to get GLSL shader code and data
 * from blender materials.
 */

/* python redefines */
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif

#include <Python.h>

#include "DNA_scene_types.h"
#include "DNA_material_types.h"
#include "DNA_ID.h"
#include "DNA_customdata_types.h"

#include "BLI_listbase.h"
#include "BLI_utildefines.h"

#include "RNA_access.h"

#include "bpy_rna.h"

#include "../generic/py_capi_utils.h"

#include "GPU_material.h"

#include "gpu.h"

#define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, # name, name)

PyDoc_STRVAR(M_gpu_doc,
"This module provides access to the GLSL shader."
);
static struct PyModuleDef gpumodule = {
	PyModuleDef_HEAD_INIT,
	"gpu",     /* name of module */
	M_gpu_doc, /* module documentation */
	-1,        /* size of per-interpreter state of the module,
	            *  or -1 if the module keeps state in global variables. */
	NULL, NULL, NULL, NULL, NULL
};

static PyObject *PyInit_gpu(void)
{
	PyObject *m;

	m = PyModule_Create(&gpumodule);
	if (m == NULL)
		return NULL;

	/* device constants */
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWMAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_MAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_VIEWIMAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_IMAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_COLOR);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNVEC);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCO);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNIMAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNPERSMAT);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNENERGY);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_LAMP_DYNCOL);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DBUFFER);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DIMAGE);
	PY_MODULE_ADD_CONSTANT(m, GPU_DYNAMIC_SAMPLER_2DSHADOW);

	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1I);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_1F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_2F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_3F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_9F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_16F);
	PY_MODULE_ADD_CONSTANT(m, GPU_DATA_4UB);

	PY_MODULE_ADD_CONSTANT(m, CD_MTFACE);
	PY_MODULE_ADD_CONSTANT(m, CD_ORCO);
	PY_MODULE_ADD_CONSTANT(m, CD_TANGENT);
	PY_MODULE_ADD_CONSTANT(m, CD_MCOL);
	return m;
}

#define PY_DICT_ADD_STRING(d, s, f)      \
	val = PyUnicode_FromString(s->f);    \
	PyDict_SetItemString(d, # f, val);   \
	Py_DECREF(val)

#define PY_DICT_ADD_LONG(d, s, f)        \
	val = PyLong_FromLong(s->f);         \
	PyDict_SetItemString(d, # f, val);   \
	Py_DECREF(val)

#define PY_DICT_ADD_ID(d, s, f)                      \
	RNA_id_pointer_create((struct ID *)s->f, &tptr); \
	val = pyrna_struct_CreatePyObject(&tptr);        \
	PyDict_SetItemString(d, # f, val);               \
	Py_DECREF(val)

#if 0  /* UNUSED */
#define PY_OBJ_ADD_ID(d, s, f)                      \
	val = PyUnicode_FromString(&s->f->id.name[2]);  \
	PyObject_SetAttrString(d, # f, val);            \
	Py_DECREF(val)

#define PY_OBJ_ADD_LONG(d, s, f)         \
	val = PyLong_FromLong(s->f);         \
	PyObject_SetAttrString(d, # f, val); \
	Py_DECREF(val)

#define PY_OBJ_ADD_STRING(d, s, f)       \
	val = PyUnicode_FromString(s->f);    \
	PyObject_SetAttrString(d, # f, val); \
	Py_DECREF(val)
#endif

PyDoc_STRVAR(GPU_export_shader_doc,
"export_shader(scene, material)\n"
"\n"
"   Returns the GLSL shader that produces the visual effect of material in scene.\n"
"\n"
"   :return: Dictionary defining the shader, uniforms and attributes.\n"
"   :rtype: Dict"
);
static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
{
	PyObject *pyscene;
	PyObject *pymat;
	PyObject *result;
	PyObject *dict;
	PyObject *val;
	PyObject *seq;

	int i;
	Scene *scene;
	PointerRNA tptr;
	Material *material;
	GPUShaderExport *shader;
	GPUInputUniform *uniform;
	GPUInputAttribute *attribute;

	static const char *kwlist[] = {"scene", "material", NULL};

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char **)(kwlist), &pyscene, &pymat))
		return NULL;

	scene = (Scene *)PyC_RNA_AsPointer(pyscene, "Scene");
	if (scene == NULL) {
		return NULL;
	}

	material = (Material *)PyC_RNA_AsPointer(pymat, "Material");
	if (material == NULL) {
		return NULL;
	}

	/* we can call our internal function at last: */
	shader = GPU_shader_export(scene, material);
	if (!shader) {
		PyErr_SetString(PyExc_RuntimeError, "cannot export shader");
		return NULL;
	}
	/* build a dictionary */
	result = PyDict_New();
	if (shader->fragment) {
		PY_DICT_ADD_STRING(result, shader, fragment);
	}
	if (shader->vertex) {
		PY_DICT_ADD_STRING(result, shader, vertex);
	}
	seq = PyList_New(BLI_listbase_count(&shader->uniforms));
	for (i = 0, uniform = shader->uniforms.first; uniform; uniform = uniform->next, i++) {
		dict = PyDict_New();
		PY_DICT_ADD_STRING(dict, uniform, varname);
		PY_DICT_ADD_LONG(dict, uniform, datatype);
		PY_DICT_ADD_LONG(dict, uniform, type);
		if (uniform->lamp) {
			PY_DICT_ADD_ID(dict, uniform, lamp);
		}
		if (uniform->image) {
			PY_DICT_ADD_ID(dict, uniform, image);
		}
		if (uniform->type == GPU_DYNAMIC_SAMPLER_2DBUFFER ||
		    uniform->type == GPU_DYNAMIC_SAMPLER_2DIMAGE ||
		    uniform->type == GPU_DYNAMIC_SAMPLER_2DSHADOW)
		{
			PY_DICT_ADD_LONG(dict, uniform, texnumber);
		}
		if (uniform->texpixels) {
			val = PyByteArray_FromStringAndSize((const char *)uniform->texpixels, uniform->texsize * 4);
			PyDict_SetItemString(dict, "texpixels", val);
			Py_DECREF(val);
			PY_DICT_ADD_LONG(dict, uniform, texsize);
		}
		PyList_SET_ITEM(seq, i, dict);
	}
	PyDict_SetItemString(result, "uniforms", seq);
	Py_DECREF(seq);

	seq = PyList_New(BLI_listbase_count(&shader->attributes));
	for (i = 0, attribute = shader->attributes.first; attribute; attribute = attribute->next, i++) {
		dict = PyDict_New();
		PY_DICT_ADD_STRING(dict, attribute, varname);
		PY_DICT_ADD_LONG(dict, attribute, datatype);
		PY_DICT_ADD_LONG(dict, attribute, type);
		PY_DICT_ADD_LONG(dict, attribute, number);
		if (attribute->name) {
			if (attribute->name[0] != 0) {
				PY_DICT_ADD_STRING(dict, attribute, name);
			}
			else {
				val = PyLong_FromLong(0);
				PyDict_SetItemString(dict, "name", val);
				Py_DECREF(val);
			}
		}
		PyList_SET_ITEM(seq, i, dict);
	}
	PyDict_SetItemString(result, "attributes", seq);
	Py_DECREF(seq);

	GPU_free_shader_export(shader);

	return result;
}

static PyMethodDef meth_export_shader[] = {
	{"export_shader", (PyCFunction)GPU_export_shader, METH_VARARGS | METH_KEYWORDS, GPU_export_shader_doc}
};

PyObject *GPU_initPython(void)
{
	PyObject *module = PyInit_gpu();
	PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL));
	PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module);

	return module;
}