[Bf-blender-cvs] [4a9e944704b] pygpu_extensions: GPU Python: Implement new 'gpu.state' module

Germano Cavalcante noreply at git.blender.org
Thu Feb 11 19:03:05 CET 2021


Commit: 4a9e944704bd5280c9332e0dc4d4e2f0c3cb93a2
Author: Germano Cavalcante
Date:   Thu Feb 11 13:58:21 2021 -0300
Branches: pygpu_extensions
https://developer.blender.org/rB4a9e944704bd5280c9332e0dc4d4e2f0c3cb93a2

GPU Python: Implement new 'gpu.state' module

This new module includes:
```
>>> gpu.state.
              blend_get(
              blend_set(
              color_mask_set(
              depth_mask_get(
              depth_mask_set(
              depth_test_get(
              depth_test_set(
              face_culling_set(
              front_facing_set(
              line_width_get(
              line_width_set(
              point_size_set(
              program_point_size_set(
              viewport_get(
              viewport_set(
```

More details will be in the Blender Python API documentation.

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

M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/gpu/CMakeLists.txt
M	source/blender/python/gpu/gpu_py_api.c
A	source/blender/python/gpu/gpu_py_state.c
A	source/blender/python/gpu/gpu_py_state.h

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

diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index ec6b8c54ac0..02c8ed30baf 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -282,6 +282,16 @@ int PyC_ParseStringEnum(PyObject *o, void *p)
   return 0;
 }
 
+const char *PyC_StringEnum_find_id(const struct PyC_StringEnumItems *items, const int value)
+{
+  for (int i = 0; items[i].id; i++) {
+    if (items[i].value == value) {
+      return items[i].id;
+    }
+  }
+  return NULL;
+}
+
 /* silly function, we dont use arg. just check its compatible with __deepcopy__ */
 int PyC_CheckArgs_DeepCopy(PyObject *args)
 {
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 358123657c7..4b895dea6a1 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -142,6 +142,7 @@ struct PyC_StringEnum {
 };
 
 int PyC_ParseStringEnum(PyObject *o, void *p);
+const char *PyC_StringEnum_find_id(const struct PyC_StringEnumItems *items, const int value);
 
 int PyC_CheckArgs_DeepCopy(PyObject *args);
 
diff --git a/source/blender/python/gpu/CMakeLists.txt b/source/blender/python/gpu/CMakeLists.txt
index 7f6fd9eefab..19a3a32a186 100644
--- a/source/blender/python/gpu/CMakeLists.txt
+++ b/source/blender/python/gpu/CMakeLists.txt
@@ -40,6 +40,7 @@ set(SRC
   gpu_py_offscreen.c
   gpu_py_select.c
   gpu_py_shader.c
+  gpu_py_state.c
   gpu_py_types.c
   gpu_py_vertex_buffer.c
   gpu_py_vertex_format.c
@@ -51,6 +52,7 @@ set(SRC
   gpu_py_offscreen.h
   gpu_py_select.h
   gpu_py_shader.h
+  gpu_py_state.h
   gpu_py_types.h
   gpu_py_vertex_buffer.h
   gpu_py_vertex_format.h
diff --git a/source/blender/python/gpu/gpu_py_api.c b/source/blender/python/gpu/gpu_py_api.c
index 954118ffa5f..3fd535303d6 100644
--- a/source/blender/python/gpu/gpu_py_api.c
+++ b/source/blender/python/gpu/gpu_py_api.c
@@ -35,6 +35,7 @@
 
 #include "gpu_py_matrix.h"
 #include "gpu_py_select.h"
+#include "gpu_py_state.h"
 #include "gpu_py_types.h"
 
 #include "gpu_py_api.h" /* own include */
@@ -134,6 +135,9 @@ PyObject *BPyInit_gpu(void)
   PyModule_AddObject(mod, "shader", (submodule = bpygpu_shader_init()));
   PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
 
+  PyModule_AddObject(mod, "state", (submodule = bpygpu_state_init()));
+  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
+
   return mod;
 }
 
diff --git a/source/blender/python/gpu/gpu_py_state.c b/source/blender/python/gpu/gpu_py_state.c
new file mode 100644
index 00000000000..03099d810b9
--- /dev/null
+++ b/source/blender/python/gpu/gpu_py_state.c
@@ -0,0 +1,405 @@
+/*
+ * 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.
+ */
+
+/** \file
+ * \ingroup bpygpu
+ *
+ * This file defines the gpu.state API.
+ *
+ * - Use ``bpygpu_`` for local API.
+ * - Use ``BPyGPU`` for public API.
+ */
+
+#include <Python.h>
+
+#include "GPU_state.h"
+
+#include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
+
+#include "gpu_py_state.h" /* own include */
+
+/* -------------------------------------------------------------------- */
+/** \name Helper Functions
+ * \{ */
+
+static const struct PyC_StringEnumItems pygpu_blend_items[] = {
+    {GPU_BLEND_NONE, "NONE"},
+    {GPU_BLEND_ALPHA, "ALPHA"},
+    {GPU_BLEND_ALPHA_PREMULT, "ALPHA_PREMULT"},
+    {GPU_BLEND_ADDITIVE, "ADDITIVE"},
+    {GPU_BLEND_ADDITIVE_PREMULT, "ADDITIVE_PREMULT"},
+    {GPU_BLEND_MULTIPLY, "MULTIPLY"},
+    {GPU_BLEND_SUBTRACT, "SUBTRACT"},
+    {GPU_BLEND_INVERT, "INVERT"},
+    /**
+     * These are quite special cases used inside the draw manager.
+     * {GPU_BLEND_OIT, "OIT"},
+     * {GPU_BLEND_BACKGROUND, "BACKGROUND"},
+     * {GPU_BLEND_CUSTOM, "CUSTOM"},
+     */
+    {0, NULL},
+};
+
+static const struct PyC_StringEnumItems pygpu_depthtest_items[] = {
+    {GPU_DEPTH_NONE, "NONE"},
+    {GPU_DEPTH_ALWAYS, "ALWAYS"},
+    {GPU_DEPTH_LESS, "LESS"},
+    {GPU_DEPTH_LESS_EQUAL, "LESS_EQUAL"},
+    {GPU_DEPTH_EQUAL, "EQUAL"},
+    {GPU_DEPTH_GREATER, "GREATER"},
+    {GPU_DEPTH_GREATER_EQUAL, "GREATER_EQUAL"},
+    {0, NULL},
+};
+
+static const struct PyC_StringEnumItems pygpu_faceculling_items[] = {
+    {GPU_CULL_NONE, "NONE"},
+    {GPU_CULL_FRONT, "FRONT"},
+    {GPU_CULL_BACK, "BACK"},
+    {0, NULL},
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Manage Stack
+ * \{ */
+
+PyDoc_STRVAR(py_state_blend_set_doc,
+             ".. function:: blend_set(mode)\n"
+             "\n"
+             "   Defines the fixed pipeline blending equation.\n"
+             "\n"
+             "   :param mode: One of these modes: {\n"
+             "      `NONE`,\n"
+             "      `ALPHA`,\n"
+             "      `ALPHA_PREMULT`,\n"
+             "      `ADDITIVE`,\n"
+             "      `ADDITIVE_PREMULT`,\n"
+             "      `MULTIPLY`,\n"
+             "      `SUBTRACT`,\n"
+             "      `INVERT`,\n"
+             //"      `OIT`,\n"
+             //"      `BACKGROUND`,\n"
+             //"      `CUSTOM`,\n"
+             "   :type mode: `str`\n");
+static PyObject *py_state_blend_set(PyObject *UNUSED(self), PyObject *value)
+{
+  struct PyC_StringEnum pygpu_blend = {pygpu_blend_items};
+  if (!PyC_ParseStringEnum(value, &pygpu_blend)) {
+    return NULL;
+  }
+  GPU_blend(pygpu_blend.value_found);
+  Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(py_state_blend_get_doc,
+             ".. function:: blend_get()\n"
+             "\n"
+             "    Current blending equation.\n"
+             "\n");
+static PyObject *py_state_blend_get(PyObject *UNUSED(self))
+{
+  eGPUBlend blend = GPU_blend_get();
+  return PyUnicode_FromString(PyC_StringEnum_find_id(pygpu_blend_items, blend));
+}
+
+PyDoc_STRVAR(py_state_depth_test_set_doc,
+             ".. function:: depth_test_set(mode)\n"
+             "\n"
+             "   Defines the depth_test equation.\n"
+             "\n"
+             "   :param mode: One of these modes: {\n"
+             "      `NONE`,\n"
+             "      `ALWAYS`,\n"
+             "      `LESS`,\n"
+             "      `LESS_EQUAL`,\n"
+             "      `EQUAL`,\n"
+             "      `GREATER`,\n"
+             "      `GREATER_EQUAL`,\n"
+             "   :type mode: `str`\n");
+static PyObject *py_state_depth_test_set(PyObject *UNUSED(self), PyObject *value)
+{
+  struct PyC_StringEnum pygpu_depth_test = {pygpu_depthtest_items};
+  if (!PyC_ParseStringEnum(value, &pygpu_depth_test)) {
+    return NULL;
+  }
+  GPU_depth_test(pygpu_depth_test.value_found);
+  Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(py_state_depth_test_get_doc,
+             ".. function:: blend_depth_test_get()\n"
+             "\n"
+             "    Current depth_test equation.\n"
+             "\n");
+static PyObject *py_state_depth_test_get(PyObject *UNUSED(self))
+{
+  eGPUDepthTest test = GPU_depth_test_get();
+  return PyUnicode_FromString(PyC_StringEnum_find_id(pygpu_depthtest_items, test));
+}
+
+PyDoc_STRVAR(py_state_depth_mask_set_doc,
+             ".. function:: depth_mask_set(value)\n"
+             "\n"
+             "   Write to depth component.\n"
+             "\n"
+             "   :param value: True for writing to the depth component.\n"
+             "   :type near: `bool`\n");
+static PyObject *py_state_depth_mask_set(PyObject *UNUSED(self), PyObject *value)
+{
+  bool write_to_depth;
+  if (!PyC_ParseBool(value, &write_to_depth)) {
+    return NULL;
+  }
+  GPU_depth_mask(write_to_depth);
+  Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(py_state_depth_mask_get_doc,
+             ".. function:: depth_mask_set_get()\n"
+             "\n"
+             "   Writing status in the depth component.\n");
+static PyObject *py_state_depth_mask_get(PyObject *UNUSED(self))
+{
+  return PyBool_FromLong(GPU_depth_mask_get());
+}
+
+PyDoc_STRVAR(py_state_viewport_set_doc,
+             ".. function:: viewport_set(x, y, width, height)\n"
+             "\n"
+             "   Specifies the viewport of the active framebuffer.\n"
+             "   Note: The viewport of state is not saved upon framebuffer rebind.\n"
+             "\n"
+             "   :param x, y: lower left corner of the viewport_set rectangle, in pixels.\n"
+             "   :param width, height: width and height of the viewport_set.\n"
+             "   :type x, y, width, height: `int`\n");
+static PyObject *py_state_viewport_set(PyObject *UNUSED(self), PyObject *args)
+{
+  int x, y, width, height;
+  if (!PyArg_ParseTuple(args, "iiii:viewport_set", &x, &y, &width, &height)) {
+    return NULL;
+  }
+
+  GPU_viewport(x, y, width, height);
+  Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(py_state_viewport_get_doc,
+             ".. function:: viewport_get()\n"
+             "\n"
+             "   Viewport of the active framebuffer.\n");
+static PyObject *py_state_viewport_get(PyObject *UNUSED(self), PyObject *args)
+{
+  int viewport[4];
+  GPU_viewport_size_get_i(viewport);
+
+  PyObject *ret = PyTuple_New(4);
+  PyTuple_SET_ITEMS(ret,
+                    PyLong_FromLong(viewport[0]),
+                    PyLong_FromLong(viewport[1]),
+                    PyLong_FromLong(viewport[2]),
+                    PyLong_FromLong(viewport[3]));
+  return ret;
+}
+
+PyDoc_STRVAR(py_state_line_width_set_doc,
+             ".. function:: line_width_set(width)\n"
+             "\n"
+             "   Specify the width of rasterized lines.\n"
+             "\n"
+    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list