2018-09-14 09:32:19 -03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bpygpu
|
2018-09-14 09:32:19 -03:00
|
|
|
*
|
|
|
|
* - Use ``bpygpu_`` for local API.
|
|
|
|
* - Use ``BPyGPU`` for public API.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
#include "GPU_shader.h"
|
|
|
|
|
|
|
|
#include "../generic/py_capi_utils.h"
|
|
|
|
#include "../generic/python_utildefines.h"
|
2018-10-23 10:17:38 +02:00
|
|
|
#include "../mathutils/mathutils.h"
|
2018-09-14 09:32:19 -03:00
|
|
|
|
2019-01-03 00:55:07 +11:00
|
|
|
#include "gpu_py_api.h"
|
2018-09-14 09:32:19 -03:00
|
|
|
#include "gpu_py_shader.h" /* own include */
|
2018-10-09 11:17:29 +02:00
|
|
|
#include "gpu_py_vertex_format.h"
|
2018-09-14 09:32:19 -03:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Enum Conversion.
|
|
|
|
* \{ */
|
|
|
|
|
2020-12-11 15:35:55 -03:00
|
|
|
static const struct PyC_StringEnumItems pygpu_bultinshader_items[] = {
|
|
|
|
{GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"},
|
|
|
|
{GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"},
|
|
|
|
{GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"},
|
|
|
|
{GPU_SHADER_2D_IMAGE, "2D_IMAGE"},
|
|
|
|
{GPU_SHADER_3D_UNIFORM_COLOR, "3D_UNIFORM_COLOR"},
|
|
|
|
{GPU_SHADER_3D_FLAT_COLOR, "3D_FLAT_COLOR"},
|
|
|
|
{GPU_SHADER_3D_SMOOTH_COLOR, "3D_SMOOTH_COLOR"},
|
|
|
|
{GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "3D_POLYLINE_UNIFORM_COLOR"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
2018-09-14 09:32:19 -03:00
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static int py_uniform_location_get(GPUShader *shader, const char *name, const char *error_prefix)
|
2018-10-03 23:34:27 -03:00
|
|
|
{
|
2020-08-20 16:10:13 +10:00
|
|
|
const int uniform = GPU_shader_get_uniform(shader, name);
|
2018-10-03 23:34:27 -03:00
|
|
|
|
2018-10-12 15:48:22 -03:00
|
|
|
if (uniform == -1) {
|
2019-06-05 08:44:24 -03:00
|
|
|
PyErr_Format(PyExc_ValueError, "%s: uniform %.32s not found", error_prefix, name);
|
2018-10-03 23:34:27 -03:00
|
|
|
}
|
|
|
|
|
2018-10-12 15:48:22 -03:00
|
|
|
return uniform;
|
2018-10-03 23:34:27 -03:00
|
|
|
}
|
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Shader Type
|
|
|
|
* \{ */
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
2019-01-03 01:08:26 +11:00
|
|
|
BPYGPU_IS_INIT_OR_ERROR_OBJ;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
struct {
|
|
|
|
const char *vertexcode;
|
|
|
|
const char *fragcode;
|
|
|
|
const char *geocode;
|
|
|
|
const char *libcode;
|
|
|
|
const char *defines;
|
|
|
|
} params = {0};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:22:57 -03:00
|
|
|
static const char *_keywords[] = {
|
|
|
|
"vertexcode", "fragcode", "geocode", "libcode", "defines", NULL};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:22:57 -03:00
|
|
|
static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0};
|
2019-01-03 01:08:26 +11:00
|
|
|
if (!_PyArg_ParseTupleAndKeywordsFast(args,
|
2018-09-27 00:22:57 -03:00
|
|
|
kwds,
|
|
|
|
&_parser,
|
2018-09-14 09:32:19 -03:00
|
|
|
¶ms.vertexcode,
|
|
|
|
¶ms.fragcode,
|
|
|
|
¶ms.geocode,
|
|
|
|
¶ms.libcode,
|
|
|
|
¶ms.defines)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-04-14 20:44:45 +02:00
|
|
|
GPUShader *shader = GPU_shader_create_from_python(
|
|
|
|
params.vertexcode, params.fragcode, params.geocode, params.libcode, params.defines);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
if (shader == NULL) {
|
|
|
|
PyErr_SetString(PyExc_Exception, "Shader Compile Error, see console for more details");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-20 15:21:59 -03:00
|
|
|
return BPyGPUShader_CreatePyObject(shader, false);
|
2018-09-14 09:32:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_bind_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: bind()\n"
|
|
|
|
"\n"
|
2018-11-14 09:04:24 +11:00
|
|
|
" Bind the shader object. Required to be able to change uniforms of this shader.\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_bind(BPyGPUShader *self)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
GPU_shader_bind(self->shader);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_from_name_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: uniform_from_name(name)\n"
|
|
|
|
"\n"
|
|
|
|
" Get uniform location by name.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: Name of the uniform variable whose location is to be queried.\n"
|
|
|
|
" :type name: `str`\n"
|
|
|
|
" :return: Location of the uniform variable.\n"
|
|
|
|
" :rtype: `int`\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_from_name(BPyGPUShader *self, PyObject *arg)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
const char *name = PyUnicode_AsUTF8(arg);
|
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
const int uniform = py_uniform_location_get(self->shader, name, "GPUShader.get_uniform");
|
2018-09-14 09:32:19 -03:00
|
|
|
|
|
|
|
if (uniform == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyLong_FromLong(uniform);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_block_from_name_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: uniform_block_from_name(name)\n"
|
|
|
|
"\n"
|
|
|
|
" Get uniform block location by name.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: Name of the uniform block variable whose location is to be queried.\n"
|
|
|
|
" :type name: `str`\n"
|
|
|
|
" :return: The location of the uniform block variable.\n"
|
|
|
|
" :rtype: `int`\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_block_from_name(BPyGPUShader *self, PyObject *arg)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
const char *name = PyUnicode_AsUTF8(arg);
|
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:10:13 +10:00
|
|
|
const int uniform = GPU_shader_get_uniform_block(self->shader, name);
|
2018-09-14 09:32:19 -03:00
|
|
|
|
|
|
|
if (uniform == -1) {
|
2018-10-12 15:48:22 -03:00
|
|
|
PyErr_Format(PyExc_ValueError, "GPUShader.get_uniform_block: uniform %.32s not found", name);
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyLong_FromLong(uniform);
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:08:00 +11:00
|
|
|
static bool py_shader_uniform_vector_impl(PyObject *args,
|
|
|
|
int elem_size,
|
|
|
|
int *r_location,
|
|
|
|
int *r_length,
|
|
|
|
int *r_count,
|
|
|
|
Py_buffer *r_pybuffer)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
PyObject *buffer;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
*r_count = 1;
|
|
|
|
if (!PyArg_ParseTuple(
|
|
|
|
args, "iOi|i:GPUShader.uniform_vector_*", r_location, &buffer, r_length, r_count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
if (PyObject_GetBuffer(buffer, r_pybuffer, PyBUF_SIMPLE) == -1) {
|
|
|
|
/* PyObject_GetBuffer raise a PyExc_BufferError */
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
if (r_pybuffer->len != (*r_length * *r_count * elem_size)) {
|
|
|
|
PyErr_SetString(PyExc_BufferError, "GPUShader.uniform_vector_*: buffer size does not match.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_vector_float_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: uniform_vector_float(location, buffer, length, count)\n"
|
|
|
|
"\n"
|
|
|
|
" Set the buffer to fill the uniform.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param location: Location of the uniform variable to be modified.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type location: int\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param buffer: The data that should be set. Can support the buffer protocol.\n"
|
|
|
|
" :type buffer: sequence of floats\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" :param length: Size of the uniform data type:\n\n"
|
2018-09-15 08:00:47 +10:00
|
|
|
" - 1: float\n"
|
|
|
|
" - 2: vec2 or float[2]\n"
|
|
|
|
" - 3: vec3 or float[3]\n"
|
|
|
|
" - 4: vec4 or float[4]\n"
|
|
|
|
" - 9: mat3\n"
|
|
|
|
" - 16: mat4\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type length: int\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param count: Specifies the number of elements, vector or matrices that are to "
|
|
|
|
"be modified.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type count: int\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_vector_float(BPyGPUShader *self, PyObject *args)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
int location, length, count;
|
|
|
|
|
|
|
|
Py_buffer pybuffer;
|
|
|
|
|
2021-02-04 13:08:00 +11:00
|
|
|
if (!py_shader_uniform_vector_impl(args, sizeof(float), &location, &length, &count, &pybuffer)) {
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GPU_shader_uniform_vector(self->shader, location, length, count, pybuffer.buf);
|
|
|
|
|
|
|
|
PyBuffer_Release(&pybuffer);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_vector_int_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: uniform_vector_int(location, buffer, length, count)\n"
|
|
|
|
"\n"
|
2018-09-15 08:00:47 +10:00
|
|
|
" See GPUShader.uniform_vector_float(...) description.\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_vector_int(BPyGPUShader *self, PyObject *args)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
int location, length, count;
|
|
|
|
|
|
|
|
Py_buffer pybuffer;
|
|
|
|
|
2021-02-04 13:08:00 +11:00
|
|
|
if (!py_shader_uniform_vector_impl(args, sizeof(int), &location, &length, &count, &pybuffer)) {
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GPU_shader_uniform_vector_int(self->shader, location, length, count, pybuffer.buf);
|
|
|
|
|
|
|
|
PyBuffer_Release(&pybuffer);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_bool_doc,
|
2018-10-03 23:34:27 -03:00
|
|
|
".. method:: uniform_bool(name, seq)\n"
|
|
|
|
"\n"
|
|
|
|
" Specify the value of a uniform variable for the current program object.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: Name of the uniform variable whose value is to be changed.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type name: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param seq: Value that will be used to update the specified uniform variable.\n"
|
2018-10-03 23:34:27 -03:00
|
|
|
" :type seq: sequence of bools\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_bool(BPyGPUShader *self, PyObject *args)
|
2018-10-03 23:34:27 -03:00
|
|
|
{
|
|
|
|
const char *error_prefix = "GPUShader.uniform_bool";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
struct {
|
|
|
|
const char *id;
|
|
|
|
PyObject *seq;
|
|
|
|
} params;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "sO:GPUShader.uniform_bool", ¶ms.id, ¶ms.seq)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
int values[4];
|
|
|
|
int length;
|
|
|
|
int ret;
|
|
|
|
{
|
|
|
|
PyObject *seq_fast = PySequence_Fast(params.seq, error_prefix);
|
|
|
|
if (seq_fast == NULL) {
|
2018-10-04 13:48:08 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%s: expected a sequence, got %s",
|
|
|
|
error_prefix,
|
|
|
|
Py_TYPE(params.seq)->tp_name);
|
2018-10-03 23:34:27 -03:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
2018-10-04 13:48:08 -03:00
|
|
|
length = PySequence_Fast_GET_SIZE(seq_fast);
|
2018-10-03 23:34:27 -03:00
|
|
|
if (length == 0 || length > 4) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%s: invalid sequence length. expected 1..4, got %d",
|
|
|
|
error_prefix,
|
|
|
|
length);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = PyC_AsArray_FAST(values, seq_fast, length, &PyLong_Type, false, error_prefix);
|
|
|
|
}
|
|
|
|
Py_DECREF(seq_fast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
const int location = py_uniform_location_get(self->shader, params.id, error_prefix);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (location == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
GPU_shader_uniform_vector_int(self->shader, location, length, 1, values);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_float_doc,
|
2018-10-23 10:17:38 +02:00
|
|
|
".. method:: uniform_float(name, value)\n"
|
2018-10-03 23:34:27 -03:00
|
|
|
"\n"
|
|
|
|
" Specify the value of a uniform variable for the current program object.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: Name of the uniform variable whose value is to be changed.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type name: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param value: Value that will be used to update the specified uniform variable.\n"
|
2018-10-23 10:17:38 +02:00
|
|
|
" :type value: single number or sequence of numbers\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_float(BPyGPUShader *self, PyObject *args)
|
2018-09-20 19:51:02 +00:00
|
|
|
{
|
2018-10-03 23:34:27 -03:00
|
|
|
const char *error_prefix = "GPUShader.uniform_float";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
struct {
|
|
|
|
const char *id;
|
|
|
|
PyObject *seq;
|
|
|
|
} params;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "sO:GPUShader.uniform_float", ¶ms.id, ¶ms.seq)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
float values[16];
|
|
|
|
int length;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-23 10:17:38 +02:00
|
|
|
if (PyFloat_Check(params.seq)) {
|
|
|
|
values[0] = (float)PyFloat_AsDouble(params.seq);
|
|
|
|
length = 1;
|
2018-10-03 23:34:27 -03:00
|
|
|
}
|
2018-10-23 10:17:38 +02:00
|
|
|
else if (PyLong_Check(params.seq)) {
|
|
|
|
values[0] = (float)PyLong_AsDouble(params.seq);
|
|
|
|
length = 1;
|
|
|
|
}
|
2018-10-24 18:45:47 +11:00
|
|
|
else if (MatrixObject_Check(params.seq)) {
|
|
|
|
MatrixObject *mat = (MatrixObject *)params.seq;
|
|
|
|
if (BaseMath_ReadCallback(mat) == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((mat->num_row != mat->num_col) || !ELEM(mat->num_row, 3, 4)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Expected 3x3 or 4x4 matrix");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
length = mat->num_row * mat->num_col;
|
|
|
|
memcpy(values, mat->matrix, sizeof(float) * length);
|
|
|
|
}
|
2018-10-23 10:17:38 +02:00
|
|
|
else {
|
|
|
|
length = mathutils_array_parse(values, 2, 16, params.seq, "");
|
2018-10-24 18:45:47 +11:00
|
|
|
if (length == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-23 10:17:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-23 10:17:38 +02:00
|
|
|
if (!ELEM(length, 1, 2, 3, 4, 9, 16)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"Expected a single float or a sequence of floats of length 1..4, 9 or 16.");
|
2018-10-03 23:34:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
const int location = py_uniform_location_get(self->shader, params.id, error_prefix);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (location == -1) {
|
2018-09-20 19:51:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
GPU_shader_uniform_vector(self->shader, location, length, 1, values);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-20 19:51:02 +00:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_uniform_int_doc,
|
2018-10-03 23:34:27 -03:00
|
|
|
".. method:: uniform_int(name, seq)\n"
|
2018-09-14 09:32:19 -03:00
|
|
|
"\n"
|
2018-10-03 23:34:27 -03:00
|
|
|
" Specify the value of a uniform variable for the current program object.\n"
|
2018-09-14 09:32:19 -03:00
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: name of the uniform variable whose value is to be changed.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type name: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param seq: Value that will be used to update the specified uniform variable.\n"
|
2018-10-03 23:34:27 -03:00
|
|
|
" :type seq: sequence of numbers\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_uniform_int(BPyGPUShader *self, PyObject *args)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
2018-10-03 23:34:27 -03:00
|
|
|
const char *error_prefix = "GPUShader.uniform_int";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
struct {
|
|
|
|
const char *id;
|
|
|
|
PyObject *seq;
|
|
|
|
} params;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "sO:GPUShader.uniform_int", ¶ms.id, ¶ms.seq)) {
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
int values[4];
|
|
|
|
int length;
|
|
|
|
int ret;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-26 11:59:49 +11:00
|
|
|
if (PyLong_Check(params.seq)) {
|
|
|
|
values[0] = PyC_Long_AsI32(params.seq);
|
|
|
|
length = 1;
|
2018-10-28 17:22:26 +01:00
|
|
|
ret = 0;
|
2018-10-26 11:59:49 +11:00
|
|
|
}
|
|
|
|
else {
|
2018-10-03 23:34:27 -03:00
|
|
|
PyObject *seq_fast = PySequence_Fast(params.seq, error_prefix);
|
|
|
|
if (seq_fast == NULL) {
|
2018-10-04 13:48:08 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%s: expected a sequence, got %s",
|
|
|
|
error_prefix,
|
|
|
|
Py_TYPE(params.seq)->tp_name);
|
2018-10-03 23:34:27 -03:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
2018-10-04 13:48:08 -03:00
|
|
|
length = PySequence_Fast_GET_SIZE(seq_fast);
|
2018-10-03 23:34:27 -03:00
|
|
|
if (length == 0 || length > 4) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%s: invalid sequence length. expected 1..4, got %d",
|
|
|
|
error_prefix,
|
|
|
|
length);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = PyC_AsArray_FAST(values, seq_fast, length, &PyLong_Type, false, error_prefix);
|
|
|
|
}
|
|
|
|
Py_DECREF(seq_fast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
const int location = py_uniform_location_get(self->shader, params.id, error_prefix);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
if (location == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
GPU_shader_uniform_vector_int(self->shader, location, length, 1, values);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-14 09:32:19 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_attr_from_name_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. method:: attr_from_name(name)\n"
|
|
|
|
"\n"
|
|
|
|
" Get attribute location by name.\n"
|
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param name: The name of the attribute variable whose location is to be queried.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type name: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :return: The location of an attribute variable.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :rtype: int\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_attr_from_name(BPyGPUShader *self, PyObject *arg)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
const char *name = PyUnicode_AsUTF8(arg);
|
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:10:13 +10:00
|
|
|
const int attr = GPU_shader_get_attribute(self->shader, name);
|
2018-09-14 09:32:19 -03:00
|
|
|
|
2018-10-23 10:49:36 +11:00
|
|
|
if (attr == -1) {
|
2018-10-12 15:48:22 -03:00
|
|
|
PyErr_Format(PyExc_ValueError, "GPUShader.attr_from_name: attribute %.32s not found", name);
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-23 10:49:36 +11:00
|
|
|
return PyLong_FromLong(attr);
|
2018-09-14 09:32:19 -03:00
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_calc_format_doc,
|
2018-10-09 11:17:29 +02:00
|
|
|
".. method:: calc_format()\n"
|
|
|
|
"\n"
|
|
|
|
" Build a new format based on the attributes of the shader.\n"
|
|
|
|
"\n"
|
|
|
|
" :return: vertex attribute format for the shader\n"
|
|
|
|
" :rtype: GPUVertFormat\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_calc_format(BPyGPUShader *self, PyObject *UNUSED(arg))
|
2018-10-09 11:17:29 +02:00
|
|
|
{
|
|
|
|
BPyGPUVertFormat *ret = (BPyGPUVertFormat *)BPyGPUVertFormat_CreatePyObject(NULL);
|
2020-06-02 12:11:39 +02:00
|
|
|
GPU_vertformat_from_shader(&ret->fmt, self->shader);
|
2018-10-09 11:17:29 +02:00
|
|
|
return (PyObject *)ret;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static struct PyMethodDef py_shader_methods[] = {
|
|
|
|
{"bind", (PyCFunction)py_shader_bind, METH_NOARGS, py_shader_bind_doc},
|
2018-09-14 09:32:19 -03:00
|
|
|
{"uniform_from_name",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_from_name,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_O,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_from_name_doc},
|
2018-09-14 09:32:19 -03:00
|
|
|
{"uniform_block_from_name",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_block_from_name,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_O,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_block_from_name_doc},
|
2018-09-14 09:32:19 -03:00
|
|
|
{"uniform_vector_float",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_vector_float,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_VARARGS,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_vector_float_doc},
|
2018-09-14 09:32:19 -03:00
|
|
|
{"uniform_vector_int",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_vector_int,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_VARARGS,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_vector_int_doc},
|
2018-10-03 23:34:27 -03:00
|
|
|
{"uniform_bool",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_bool,
|
2018-10-03 23:34:27 -03:00
|
|
|
METH_VARARGS,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_bool_doc},
|
2018-09-20 19:51:02 +00:00
|
|
|
{"uniform_float",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_uniform_float,
|
2018-09-20 19:51:02 +00:00
|
|
|
METH_VARARGS,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_uniform_float_doc},
|
|
|
|
{"uniform_int", (PyCFunction)py_shader_uniform_int, METH_VARARGS, py_shader_uniform_int_doc},
|
2018-09-14 09:32:19 -03:00
|
|
|
{"attr_from_name",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_attr_from_name,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_O,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_attr_from_name_doc},
|
|
|
|
{"format_calc", (PyCFunction)py_shader_calc_format, METH_NOARGS, py_shader_calc_format_doc},
|
2019-02-03 14:01:45 +11:00
|
|
|
{NULL, NULL, 0, NULL},
|
2018-09-14 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
2018-10-03 23:34:27 -03:00
|
|
|
PyDoc_STRVAR(
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_program_doc,
|
2018-10-03 23:34:27 -03:00
|
|
|
"The name of the program object for use by the OpenGL API (read-only).\n\n:type: int");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_program_get(BPyGPUShader *self, void *UNUSED(closure))
|
2018-10-03 23:34:27 -03:00
|
|
|
{
|
|
|
|
return PyLong_FromLong(GPU_shader_get_program(self->shader));
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyGetSetDef py_shader_getseters[] = {
|
|
|
|
{"program", (getter)py_shader_program_get, (setter)NULL, py_shader_program_doc, NULL},
|
2018-09-14 09:32:19 -03:00
|
|
|
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static void py_shader_dealloc(BPyGPUShader *self)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
2018-09-20 15:21:59 -03:00
|
|
|
if (self->is_builtin == false) {
|
|
|
|
GPU_shader_free(self->shader);
|
|
|
|
}
|
2018-09-14 09:32:19 -03:00
|
|
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_doc,
|
2018-11-13 14:55:15 +01:00
|
|
|
".. class:: GPUShader(vertexcode, fragcode, geocode=None, libcode=None, defines=None)\n"
|
2018-09-15 08:00:47 +10:00
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" GPUShader combines multiple GLSL shaders into a program used for drawing.\n"
|
|
|
|
" It must contain a vertex and fragment shaders, with an optional geometry shader.\n"
|
2018-09-15 08:00:47 +10:00
|
|
|
"\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" The GLSL ``#version`` directive is automatically included at the top of shaders,\n"
|
|
|
|
" and set to 330. Some preprocessor directives are automatically added according to\n"
|
|
|
|
" the Operating System or availability: ``GPU_ATI``, ``GPU_NVIDIA`` and ``GPU_INTEL``.\n"
|
2018-09-15 08:00:47 +10:00
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" The following extensions are enabled by default if supported by the GPU:\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" ``GL_ARB_texture_gather``, ``GL_ARB_texture_cube_map_array``\n"
|
|
|
|
" and ``GL_ARB_shader_draw_parameters``.\n"
|
2018-09-14 09:32:19 -03:00
|
|
|
"\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" For drawing user interface elements and gizmos, use\n"
|
|
|
|
" ``fragOutput = blender_srgb_to_framebuffer_space(fragOutput)``\n"
|
|
|
|
" to transform the output sRGB colors to the frame-buffer color-space.\n"
|
2020-04-14 20:44:45 +02:00
|
|
|
"\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param vertexcode: Vertex shader code.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type vertexcode: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param fragcode: Fragment shader code.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type value: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param geocode: Geometry shader code.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type value: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param libcode: Code with functions and presets to be shared between shaders.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type value: str\n"
|
2018-11-13 14:55:15 +01:00
|
|
|
" :param defines: Preprocessor directives.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :type value: str\n");
|
2018-09-14 09:32:19 -03:00
|
|
|
PyTypeObject BPyGPUShader_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUShader",
|
|
|
|
.tp_basicsize = sizeof(BPyGPUShader),
|
2020-12-11 16:05:58 -03:00
|
|
|
.tp_dealloc = (destructor)py_shader_dealloc,
|
2018-09-14 09:32:19 -03:00
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
2020-12-11 16:05:58 -03:00
|
|
|
.tp_doc = py_shader_doc,
|
|
|
|
.tp_methods = py_shader_methods,
|
|
|
|
.tp_getset = py_shader_getseters,
|
|
|
|
.tp_new = py_shader_new,
|
2018-09-14 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name gpu.shader Module API
|
|
|
|
* \{ */
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_unbind_doc,
|
2018-09-14 09:32:19 -03:00
|
|
|
".. function:: unbind()\n"
|
|
|
|
"\n"
|
|
|
|
" Unbind the bound shader object.\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_unbind(BPyGPUShader *UNUSED(self))
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
GPU_shader_unbind();
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_from_builtin_doc,
|
2018-10-22 15:01:25 +02:00
|
|
|
".. function:: from_builtin(shader_name)\n"
|
2018-10-09 12:18:28 -03:00
|
|
|
"\n"
|
2018-11-14 13:04:12 +01:00
|
|
|
" Shaders that are embedded in the blender internal code.\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" They all read the uniform ``mat4 ModelViewProjectionMatrix``,\n"
|
|
|
|
" which can be edited by the :mod:`gpu.matrix` module.\n"
|
|
|
|
" For more details, you can check the shader code with the\n"
|
|
|
|
" :func:`gpu.shader.code_from_builtin` function.\n"
|
2018-10-30 11:38:42 -03:00
|
|
|
"\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" :param shader_name: One of these builtin shader names:\n\n"
|
|
|
|
" - ``2D_UNIFORM_COLOR``\n"
|
|
|
|
" - ``2D_FLAT_COLOR``\n"
|
|
|
|
" - ``2D_SMOOTH_COLOR``\n"
|
|
|
|
" - ``2D_IMAGE``\n"
|
|
|
|
" - ``3D_UNIFORM_COLOR``\n"
|
|
|
|
" - ``3D_FLAT_COLOR``\n"
|
|
|
|
" - ``3D_SMOOTH_COLOR``\n"
|
2018-10-09 12:18:28 -03:00
|
|
|
" :type shader_name: str\n"
|
2018-11-14 13:04:12 +01:00
|
|
|
" :return: Shader object corresponding to the given name.\n"
|
|
|
|
" :rtype: :class:`bpy.types.GPUShader`\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
2019-01-03 01:08:26 +11:00
|
|
|
BPYGPU_IS_INIT_OR_ERROR_OBJ;
|
|
|
|
|
2020-12-11 17:54:12 -03:00
|
|
|
struct PyC_StringEnum pygpu_bultinshader = {pygpu_bultinshader_items};
|
2020-12-11 15:35:55 -03:00
|
|
|
if (!PyC_ParseStringEnum(arg, &pygpu_bultinshader)) {
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:35:55 -03:00
|
|
|
GPUShader *shader = GPU_shader_get_builtin_shader(pygpu_bultinshader.value_found);
|
2018-09-14 09:32:19 -03:00
|
|
|
|
2018-09-20 15:21:59 -03:00
|
|
|
return BPyGPUShader_CreatePyObject(shader, true);
|
2018-09-14 09:32:19 -03:00
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_code_from_builtin_doc,
|
2018-10-22 15:01:25 +02:00
|
|
|
".. function:: code_from_builtin(shader_name)\n"
|
2018-10-09 12:18:28 -03:00
|
|
|
"\n"
|
2018-11-14 13:04:12 +01:00
|
|
|
" Exposes the internal shader code for query.\n"
|
2018-10-30 11:38:42 -03:00
|
|
|
"\n"
|
2020-10-11 18:19:42 -04:00
|
|
|
" :param shader_name: One of these builtin shader names:\n\n"
|
|
|
|
" - ``2D_UNIFORM_COLOR``\n"
|
|
|
|
" - ``2D_FLAT_COLOR``\n"
|
|
|
|
" - ``2D_SMOOTH_COLOR``\n"
|
|
|
|
" - ``2D_IMAGE``\n"
|
|
|
|
" - ``3D_UNIFORM_COLOR``\n"
|
|
|
|
" - ``3D_FLAT_COLOR``\n"
|
|
|
|
" - ``3D_SMOOTH_COLOR``\n"
|
2018-10-09 12:18:28 -03:00
|
|
|
" :type shader_name: str\n"
|
2018-11-14 13:05:45 +01:00
|
|
|
" :return: Vertex, fragment and geometry shader codes.\n"
|
2018-10-04 13:08:04 +10:00
|
|
|
" :rtype: dict\n");
|
2020-12-11 16:05:58 -03:00
|
|
|
static PyObject *py_shader_code_from_builtin(BPyGPUShader *UNUSED(self), PyObject *arg)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
const char *vert;
|
|
|
|
const char *frag;
|
|
|
|
const char *geom;
|
|
|
|
const char *defines;
|
|
|
|
|
|
|
|
PyObject *item, *r_dict;
|
|
|
|
|
2020-12-11 17:54:12 -03:00
|
|
|
struct PyC_StringEnum pygpu_bultinshader = {pygpu_bultinshader_items};
|
2020-12-11 15:35:55 -03:00
|
|
|
if (!PyC_ParseStringEnum(arg, &pygpu_bultinshader)) {
|
2018-09-14 09:32:19 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:35:55 -03:00
|
|
|
GPU_shader_get_builtin_shader_code(
|
|
|
|
pygpu_bultinshader.value_found, &vert, &frag, &geom, &defines);
|
2018-09-14 09:32:19 -03:00
|
|
|
|
|
|
|
r_dict = PyDict_New();
|
|
|
|
|
|
|
|
PyDict_SetItemString(r_dict, "vertex_shader", item = PyUnicode_FromString(vert));
|
|
|
|
Py_DECREF(item);
|
|
|
|
|
|
|
|
PyDict_SetItemString(r_dict, "fragment_shader", item = PyUnicode_FromString(frag));
|
|
|
|
Py_DECREF(item);
|
|
|
|
|
|
|
|
if (geom) {
|
|
|
|
PyDict_SetItemString(r_dict, "geometry_shader", item = PyUnicode_FromString(geom));
|
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
|
|
|
if (defines) {
|
|
|
|
PyDict_SetItemString(r_dict, "defines", item = PyUnicode_FromString(defines));
|
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
|
|
|
return r_dict;
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
static struct PyMethodDef py_shader_module_methods[] = {
|
|
|
|
{"unbind", (PyCFunction)py_shader_unbind, METH_NOARGS, py_shader_unbind_doc},
|
|
|
|
{"from_builtin", (PyCFunction)py_shader_from_builtin, METH_O, py_shader_from_builtin_doc},
|
2018-10-04 13:51:21 -03:00
|
|
|
{"code_from_builtin",
|
2020-12-11 16:05:58 -03:00
|
|
|
(PyCFunction)py_shader_code_from_builtin,
|
2018-09-14 09:32:19 -03:00
|
|
|
METH_O,
|
2020-12-11 16:05:58 -03:00
|
|
|
py_shader_code_from_builtin_doc},
|
2019-02-03 14:01:45 +11:00
|
|
|
{NULL, NULL, 0, NULL},
|
2018-09-14 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
2020-12-11 16:05:58 -03:00
|
|
|
PyDoc_STRVAR(py_shader_module_doc,
|
2020-10-11 18:21:28 -04:00
|
|
|
"This module provides access to GPUShader internal functions.\n"
|
|
|
|
"\n"
|
|
|
|
".. rubric:: Built-in shaders\n"
|
|
|
|
"\n"
|
|
|
|
"All built-in shaders have the ``mat4 ModelViewProjectionMatrix`` uniform.\n"
|
|
|
|
"The value of it can only be modified using the :class:`gpu.matrix` module.\n"
|
|
|
|
"\n"
|
|
|
|
"2D_UNIFORM_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos\n"
|
|
|
|
" :Uniforms: vec4 color\n"
|
|
|
|
"2D_FLAT_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos, vec4 color\n"
|
|
|
|
" :Uniforms: none\n"
|
|
|
|
"2D_SMOOTH_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos, vec4 color\n"
|
|
|
|
" :Uniforms: none\n"
|
|
|
|
"2D_IMAGE\n"
|
|
|
|
" :Attributes: vec3 pos, vec2 texCoord\n"
|
|
|
|
" :Uniforms: sampler2D image\n"
|
|
|
|
"3D_UNIFORM_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos\n"
|
|
|
|
" :Uniforms: vec4 color\n"
|
|
|
|
"3D_FLAT_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos, vec4 color\n"
|
|
|
|
" :Uniforms: none\n"
|
|
|
|
"3D_SMOOTH_COLOR\n"
|
|
|
|
" :Attributes: vec3 pos, vec4 color\n"
|
|
|
|
" :Uniforms: none\n");
|
2018-09-14 09:32:19 -03:00
|
|
|
static PyModuleDef BPyGPU_shader_module_def = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "gpu.shader",
|
2020-12-11 16:05:58 -03:00
|
|
|
.m_doc = py_shader_module_doc,
|
|
|
|
.m_methods = py_shader_module_methods,
|
2018-09-14 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Public API
|
|
|
|
* \{ */
|
|
|
|
|
2018-09-20 15:21:59 -03:00
|
|
|
PyObject *BPyGPUShader_CreatePyObject(GPUShader *shader, bool is_builtin)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
|
|
|
BPyGPUShader *self;
|
|
|
|
|
|
|
|
self = PyObject_New(BPyGPUShader, &BPyGPUShader_Type);
|
|
|
|
self->shader = shader;
|
2018-09-20 15:21:59 -03:00
|
|
|
self->is_builtin = is_builtin;
|
2018-09-14 09:32:19 -03:00
|
|
|
|
|
|
|
return (PyObject *)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *BPyInit_gpu_shader(void)
|
|
|
|
{
|
|
|
|
PyObject *submodule;
|
|
|
|
|
|
|
|
submodule = PyModule_Create(&BPyGPU_shader_module_def);
|
|
|
|
|
|
|
|
return submodule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|