[Bf-blender-cvs] [e0394761b95] master: GPUShader: Expose name for debugging & identifying shaders

Jon Denning noreply at git.blender.org
Mon Sep 13 08:17:30 CEST 2021


Commit: e0394761b954759f8723bdc8f4a1686bf70954ed
Author: Jon Denning
Date:   Mon Sep 13 16:12:12 2021 +1000
Branches: master
https://developer.blender.org/rBe0394761b954759f8723bdc8f4a1686bf70954ed

GPUShader: Expose name for debugging & identifying shaders

Added optional `name` argument to `GPUShader` constructor
(defaults to `pyGPUShader`), and added `name` getter to `GPUShader`.

Ref D12393

Reviewed By: campbellbarton, jbakker

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

M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/python/gpu/gpu_py_shader.c

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

diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index f834ee5b234..62b748b7edf 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -54,7 +54,8 @@ GPUShader *GPU_shader_create_from_python(const char *vertcode,
                                          const char *fragcode,
                                          const char *geomcode,
                                          const char *libcode,
-                                         const char *defines);
+                                         const char *defines,
+                                         const char *name);
 GPUShader *GPU_shader_create_ex(const char *vertcode,
                                 const char *fragcode,
                                 const char *geomcode,
@@ -85,6 +86,8 @@ void GPU_shader_free(GPUShader *shader);
 void GPU_shader_bind(GPUShader *shader);
 void GPU_shader_unbind(void);
 
+const char *GPU_shader_get_name(GPUShader *shader);
+
 /* Returns true if transform feedback was successfully enabled. */
 bool GPU_shader_transform_feedback_enable(GPUShader *shader, struct GPUVertBuf *vertbuf);
 void GPU_shader_transform_feedback_disable(GPUShader *shader);
diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc
index c754a649924..9340d311472 100644
--- a/source/blender/gpu/intern/gpu_shader.cc
+++ b/source/blender/gpu/intern/gpu_shader.cc
@@ -229,7 +229,8 @@ GPUShader *GPU_shader_create_from_python(const char *vertcode,
                                          const char *fragcode,
                                          const char *geomcode,
                                          const char *libcode,
-                                         const char *defines)
+                                         const char *defines,
+                                         const char *name)
 {
   char *libcodecat = nullptr;
 
@@ -240,6 +241,9 @@ GPUShader *GPU_shader_create_from_python(const char *vertcode,
     libcode = libcodecat = BLI_strdupcat(libcode, datatoc_gpu_shader_colorspace_lib_glsl);
   }
 
+  /* Use pyGPUShader as default name for shader. */
+  const char *shname = name != nullptr ? name : "pyGPUShader";
+
   GPUShader *sh = GPU_shader_create_ex(vertcode,
                                        fragcode,
                                        geomcode,
@@ -249,7 +253,7 @@ GPUShader *GPU_shader_create_from_python(const char *vertcode,
                                        GPU_SHADER_TFB_NONE,
                                        nullptr,
                                        0,
-                                       "pyGPUShader");
+                                       shname);
 
   MEM_SAFE_FREE(libcodecat);
   return sh;
@@ -368,6 +372,17 @@ void GPU_shader_unbind(void)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Shader name
+ * \{ */
+
+const char *GPU_shader_get_name(GPUShader *shader)
+{
+  return unwrap(shader)->name_get();
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Transform feedback
  *
diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index 95e505b1343..1bdf9766c1b 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -105,12 +105,13 @@ static PyObject *pygpu_shader__tp_new(PyTypeObject *UNUSED(type), PyObject *args
     const char *geocode;
     const char *libcode;
     const char *defines;
+    const char *name;
   } params = {0};
 
   static const char *_keywords[] = {
-      "vertexcode", "fragcode", "geocode", "libcode", "defines", NULL};
+      "vertexcode", "fragcode", "geocode", "libcode", "defines", "name", NULL};
 
-  static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0};
+  static _PyArg_Parser _parser = {"ss|$ssss:GPUShader.__new__", _keywords, 0};
   if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                         kwds,
                                         &_parser,
@@ -118,12 +119,17 @@ static PyObject *pygpu_shader__tp_new(PyTypeObject *UNUSED(type), PyObject *args
                                         &params.fragcode,
                                         &params.geocode,
                                         &params.libcode,
-                                        &params.defines)) {
+                                        &params.defines,
+                                        &params.name)) {
     return NULL;
   }
 
-  GPUShader *shader = GPU_shader_create_from_python(
-      params.vertexcode, params.fragcode, params.geocode, params.libcode, params.defines);
+  GPUShader *shader = GPU_shader_create_from_python(params.vertexcode,
+                                                    params.fragcode,
+                                                    params.geocode,
+                                                    params.libcode,
+                                                    params.defines,
+                                                    params.name);
 
   if (shader == NULL) {
     PyErr_SetString(PyExc_Exception, "Shader Compile Error, see console for more details");
@@ -639,6 +645,13 @@ static struct PyMethodDef pygpu_shader__tp_methods[] = {
     {NULL, NULL, 0, NULL},
 };
 
+PyDoc_STRVAR(pygpu_shader_name_doc,
+             "The name of the shader object for debugging purposes (read-only).\n\n:type: str");
+static PyObject *pygpu_shader_name(BPyGPUShader *self)
+{
+  return PyUnicode_FromString(GPU_shader_get_name(self->shader));
+}
+
 PyDoc_STRVAR(
     pygpu_shader_program_doc,
     "The name of the program object for use by the OpenGL API (read-only).\n\n:type: int");
@@ -649,6 +662,7 @@ static PyObject *pygpu_shader_program_get(BPyGPUShader *self, void *UNUSED(closu
 
 static PyGetSetDef pygpu_shader__tp_getseters[] = {
     {"program", (getter)pygpu_shader_program_get, (setter)NULL, pygpu_shader_program_doc, NULL},
+    {"name", (getter)pygpu_shader_name, (setter)NULL, pygpu_shader_name_doc, NULL},
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
 
@@ -662,7 +676,8 @@ static void pygpu_shader__tp_dealloc(BPyGPUShader *self)
 
 PyDoc_STRVAR(
     pygpu_shader__tp_doc,
-    ".. class:: GPUShader(vertexcode, fragcode, geocode=None, libcode=None, defines=None)\n"
+    ".. class:: GPUShader(vertexcode, fragcode, geocode=None, libcode=None, defines=None, "
+    "name='pyGPUShader')\n"
     "\n"
     "   GPUShader combines multiple GLSL shaders into a program used for drawing.\n"
     "   It must contain at least a vertex and fragment shaders.\n"
@@ -688,6 +703,8 @@ PyDoc_STRVAR(
     "   :param libcode: Code with functions and presets to be shared between shaders.\n"
     "   :type value: str\n"
     "   :param defines: Preprocessor directives.\n"
+    "   :type value: str\n"
+    "   :param name: Name of shader code, for debugging purposes.\n"
     "   :type value: str\n");
 PyTypeObject BPyGPUShader_Type = {
     PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUShader",



More information about the Bf-blender-cvs mailing list