Beispiel #1
0
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_countlist(&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_countlist(&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;
}
Beispiel #2
0
static PyObject* GPU_export_shader(PyObject* UNUSED(self), PyObject *args, PyObject *kwds)
{
	PyObject* pyscene;
	PyObject* pymat;
	PyObject* as_pointer;
	PyObject* pointer;
	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;

	if (!strcmp(Py_TYPE(pyscene)->tp_name, "Scene") && 
		(as_pointer = PyObject_GetAttrString(pyscene, "as_pointer")) != NULL &&
		PyCallable_Check(as_pointer)) {
		// must be a scene object
		pointer = PyObject_CallObject(as_pointer, NULL);
		if (!pointer) {
			PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
			return NULL;
		}
		scene = (Scene*)PyLong_AsVoidPtr(pointer);
		Py_DECREF(pointer);
		if (!scene) {
			PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
			return NULL;
		}
	} else {
		PyErr_SetString(PyExc_TypeError, "gpu.export_shader() first argument should be of Scene type");
		return NULL;
	}

	if (!strcmp(Py_TYPE(pymat)->tp_name, "Material") && 
		(as_pointer = PyObject_GetAttrString(pymat, "as_pointer")) != NULL &&
		PyCallable_Check(as_pointer)) {
		// must be a material object
		pointer = PyObject_CallObject(as_pointer, NULL);
		if (!pointer) {
			PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
			return NULL;
		}
		material = (Material*)PyLong_AsVoidPtr(pointer);
		Py_DECREF(pointer);
		if (!material) {
			PyErr_SetString(PyExc_SystemError, "scene.as_pointer() failed");
			return NULL;
		}
	} else {
		PyErr_SetString(PyExc_TypeError, "gpu.export_shader() second argument should be of Material type");
		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_countlist(&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_countlist(&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;
}