[Bf-blender-cvs] [06a60fe9f70] master: PyAPI: GPU: expose clip distances

Germano Cavalcante noreply at git.blender.org
Thu Aug 26 18:03:40 CEST 2021


Commit: 06a60fe9f70061cd28af3ffcbf075273225d54b2
Author: Germano Cavalcante
Date:   Thu Aug 26 13:02:52 2021 -0300
Branches: master
https://developer.blender.org/rB06a60fe9f70061cd28af3ffcbf075273225d54b2

PyAPI: GPU: expose clip distances

Now you can get a shader that uses Clip Planes and set the number of
Clip Distanes with `gpu.state.clip_distances_set(value)`.

===================================================================

M	source/blender/python/gpu/gpu_py_shader.c
M	source/blender/python/gpu/gpu_py_state.c

===================================================================

diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index 145586d8ab0..c7b59ee0d6e 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -70,6 +70,12 @@ static const struct PyC_StringEnumItems pygpu_shader_builtin_items[] = {
     {0, NULL},
 };
 
+static const struct PyC_StringEnumItems pygpu_shader_config_items[] = {
+    {GPU_SHADER_CFG_DEFAULT, "DEFAULT"},
+    {GPU_SHADER_CFG_CLIPPED, "CLIPPED"},
+    {0, NULL},
+};
+
 static int pygpu_shader_uniform_location_get(GPUShader *shader,
                                              const char *name,
                                              const char *error_prefix)
@@ -711,29 +717,48 @@ static PyObject *pygpu_shader_unbind(BPyGPUShader *UNUSED(self))
 }
 
 PyDoc_STRVAR(pygpu_shader_from_builtin_doc,
-             ".. function:: from_builtin(pygpu_shader_name)\n"
+             ".. function:: from_builtin(shader_name, config='DEFAULT')\n"
              "\n"
              "   Shaders that are embedded in the blender internal code.\n"
              "   They all read the uniform ``mat4 ModelViewProjectionMatrix``,\n"
              "   which can be edited by the :mod:`gpu.matrix` module.\n"
+             "   You can also choose a shader configuration that uses clip_planes by setting the "
+             "``CLIPPED`` value to the config parameter. Note that in this case you also need to "
+             "manually set the value of ``ModelMatrix``.\n"
+             "\n"
              "   For more details, you can check the shader code with the\n"
              "   :func:`gpu.shader.code_from_builtin` function.\n"
              "\n"
-             "   :param pygpu_shader_name: One of these builtin shader names:\n"
+             "   :param shader_name: One of these builtin shader names:\n"
              "\n" PYDOC_BUILTIN_SHADER_LIST
-             "   :type pygpu_shader_name: str\n"
+             "   :type shader_name: str\n"
+             "   :param config: One of these types of shader configuration:\n"
+             "      - ``DEFAULT``\n"
+             "      - ``CLIPPED``\n"
+             "   :type config: str\n"
              "   :return: Shader object corresponding to the given name.\n"
              "   :rtype: :class:`bpy.types.GPUShader`\n");
-static PyObject *pygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg)
+static PyObject *pygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
 {
   BPYGPU_IS_INIT_OR_ERROR_OBJ;
 
   struct PyC_StringEnum pygpu_bultinshader = {pygpu_shader_builtin_items};
-  if (!PyC_ParseStringEnum(arg, &pygpu_bultinshader)) {
+  struct PyC_StringEnum pygpu_config = {pygpu_shader_config_items, GPU_SHADER_CFG_DEFAULT};
+
+  static const char *_keywords[] = {"shader_name", "config", NULL};
+  static _PyArg_Parser _parser = {"O&|$O&:from_builtin", _keywords, 0};
+  if (!_PyArg_ParseTupleAndKeywordsFast(args,
+                                        kwds,
+                                        &_parser,
+                                        PyC_ParseStringEnum,
+                                        &pygpu_bultinshader,
+                                        PyC_ParseStringEnum,
+                                        &pygpu_config)) {
     return NULL;
   }
 
-  GPUShader *shader = GPU_shader_get_builtin_shader(pygpu_bultinshader.value_found);
+  GPUShader *shader = GPU_shader_get_builtin_shader_with_config(pygpu_bultinshader.value_found,
+                                                                pygpu_config.value_found);
 
   return BPyGPUShader_CreatePyObject(shader, true);
 }
@@ -788,7 +813,7 @@ static struct PyMethodDef pygpu_shader_module__tp_methods[] = {
     {"unbind", (PyCFunction)pygpu_shader_unbind, METH_NOARGS, pygpu_shader_unbind_doc},
     {"from_builtin",
      (PyCFunction)pygpu_shader_from_builtin,
-     METH_O,
+     METH_VARARGS | METH_KEYWORDS,
      pygpu_shader_from_builtin_doc},
     {"code_from_builtin",
      (PyCFunction)pygpu_shader_code_from_builtin,
diff --git a/source/blender/python/gpu/gpu_py_state.c b/source/blender/python/gpu/gpu_py_state.c
index 7b7a61cc338..757c787882b 100644
--- a/source/blender/python/gpu/gpu_py_state.c
+++ b/source/blender/python/gpu/gpu_py_state.c
@@ -123,6 +123,28 @@ static PyObject *pygpu_state_blend_get(PyObject *UNUSED(self))
   return PyUnicode_FromString(PyC_StringEnum_FindIDFromValue(pygpu_state_blend_items, blend));
 }
 
+PyDoc_STRVAR(pygpu_state_clip_distances_set_doc,
+             ".. function:: clip_distances_set(distances_enabled)\n"
+             "\n"
+             "   Sets number of `gl_ClipDistance`s that will be used for clip geometry.\n"
+             "\n"
+             "   :param distances_enabled: Number of clip distances enabled.\n"
+             "   :type distances_enabled: int\n");
+static PyObject *pygpu_state_clip_distances_set(PyObject *UNUSED(self), PyObject *value)
+{
+  int distances_enabled = (int)PyLong_AsUnsignedLong(value);
+  if (distances_enabled == -1) {
+    return NULL;
+  }
+
+  if (distances_enabled > 6) {
+    PyErr_SetString(PyExc_ValueError, "too many distances enabled, max is 6");
+  }
+
+  GPU_clip_distances(distances_enabled);
+  Py_RETURN_NONE;
+}
+
 PyDoc_STRVAR(pygpu_state_depth_test_set_doc,
              ".. function:: depth_test_set(mode)\n"
              "\n"
@@ -356,6 +378,10 @@ static struct PyMethodDef pygpu_state__tp_methods[] = {
     /* Manage Stack */
     {"blend_set", (PyCFunction)pygpu_state_blend_set, METH_O, pygpu_state_blend_set_doc},
     {"blend_get", (PyCFunction)pygpu_state_blend_get, METH_NOARGS, pygpu_state_blend_get_doc},
+    {"clip_distances_set",
+     (PyCFunction)pygpu_state_clip_distances_set,
+     METH_O,
+     pygpu_state_clip_distances_set_doc},
     {"depth_test_set",
      (PyCFunction)pygpu_state_depth_test_set,
      METH_O,



More information about the Bf-blender-cvs mailing list