[Bf-blender-cvs] [2aad8fc7bc2] master: PyAPI: GPU Shader: add 'state' parameter to uniform sampler

Germano Cavalcante noreply at git.blender.org
Wed Sep 1 15:40:50 CEST 2021


Commit: 2aad8fc7bc2a45d5f749430c7cb9b82b9f6c9e49
Author: Germano Cavalcante
Date:   Mon Aug 30 17:08:32 2021 -0300
Branches: master
https://developer.blender.org/rB2aad8fc7bc2a45d5f749430c7cb9b82b9f6c9e49

PyAPI: GPU Shader: add 'state' parameter to uniform sampler

Now you can choose the state of texture (as a filter and repetition) to
render it.

This is important as the original state is very limited.

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

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

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

diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index 95e505b1343..d2167f2f102 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -76,6 +76,21 @@ static const struct PyC_StringEnumItems pygpu_shader_config_items[] = {
     {0, NULL},
 };
 
+static const struct PyC_FlagSet pygpu_texture_samplerstate_items[] = {
+    {GPU_SAMPLER_DEFAULT, "DEFAULT"},
+    {GPU_SAMPLER_FILTER, "FILTER"},
+    {GPU_SAMPLER_MIPMAP, "MIPMAP"},
+    {GPU_SAMPLER_REPEAT_S, "REPEAT_S"},
+    {GPU_SAMPLER_REPEAT_T, "REPEAT_T"},
+    {GPU_SAMPLER_REPEAT_R, "REPEAT_R"},
+    {GPU_SAMPLER_CLAMP_BORDER, "CLAMP_BORDER"},
+    {GPU_SAMPLER_COMPARE, "COMPARE"},
+    {GPU_SAMPLER_ANISO, "ANISO"},
+    {GPU_SAMPLER_ICON, "ICON"},
+    {GPU_SAMPLER_REPEAT, "REPEAT"},
+    {0, NULL},
+};
+
 static int pygpu_shader_uniform_location_get(GPUShader *shader,
                                              const char *name,
                                              const char *error_prefix)
@@ -492,25 +507,53 @@ static PyObject *pygpu_shader_uniform_int(BPyGPUShader *self, PyObject *args)
 }
 
 PyDoc_STRVAR(pygpu_shader_uniform_sampler_doc,
-             ".. method:: uniform_sampler(name, texture)\n"
+             ".. method:: uniform_sampler(name, texture, state={'DEFAULT'})\n"
              "\n"
-             "   Specify the value of a texture uniform variable for the current GPUShader.\n"
+             "   Specify the texture and state for an uniform sampler in the current GPUShader.\n"
              "\n"
              "   :param name: name of the uniform variable whose texture is to be specified.\n"
              "   :type name: str\n"
              "   :param texture: Texture to attach.\n"
-             "   :type texture: :class:`gpu.types.GPUTexture`\n");
-static PyObject *pygpu_shader_uniform_sampler(BPyGPUShader *self, PyObject *args)
+             "   :type texture: :class:`gpu.types.GPUTexture`\n"
+             "   :param state: set of values in:\n"
+             "\n"
+             "      - ``DEFAULT``\n"
+             "      - ``FILTER``\n"
+             "      - ``MIPMAP``\n"
+             "      - ``REPEAT_S``\n"
+             "      - ``REPEAT_T``\n"
+             "      - ``REPEAT_R``\n"
+             "      - ``CLAMP_BORDER``\n"
+             "      - ``COMPARE``\n"
+             "      - ``ANISO``\n"
+             "      - ``ICON``\n"
+             "      - ``REPEAT``\n"
+             "   :type state: set\n");
+static PyObject *pygpu_shader_uniform_sampler(BPyGPUShader *self, PyObject *args, PyObject *kwds)
 {
   const char *name;
   BPyGPUTexture *py_texture;
-  if (!PyArg_ParseTuple(
-          args, "sO!:GPUShader.uniform_sampler", &name, &BPyGPUTexture_Type, &py_texture)) {
+  PyObject *py_samplerstate = NULL;
+
+  static const char *_keywords[] = {"name", "texture", "state", NULL};
+  static _PyArg_Parser _parser = {"sO!|$O:uniform_sampler", _keywords, 0};
+  if (!_PyArg_ParseTupleAndKeywordsFast(
+          args, kwds, &_parser, &name, &BPyGPUTexture_Type, &py_texture, &py_samplerstate)) {
     return NULL;
   }
 
+  int sampler_state = GPU_SAMPLER_DEFAULT;
+  if (py_samplerstate) {
+    if (PyC_FlagSet_ToBitfield(pygpu_texture_samplerstate_items,
+                               py_samplerstate,
+                               &sampler_state,
+                               "shader.uniform_sampler") == -1) {
+      return NULL;
+    }
+  }
+
   int slot = GPU_shader_get_texture_binding(self->shader, name);
-  GPU_texture_bind(py_texture->tex, slot);
+  GPU_texture_bind_ex(py_texture->tex, (eGPUSamplerState)sampler_state, slot, false);
   GPU_shader_uniform_1i(self->shader, name, slot);
 
   Py_RETURN_NONE;
@@ -622,7 +665,7 @@ static struct PyMethodDef pygpu_shader__tp_methods[] = {
      pygpu_shader_uniform_int_doc},
     {"uniform_sampler",
      (PyCFunction)pygpu_shader_uniform_sampler,
-     METH_VARARGS,
+     METH_VARARGS | METH_KEYWORDS,
      pygpu_shader_uniform_sampler_doc},
     {"uniform_block",
      (PyCFunction)pygpu_shader_uniform_block,



More information about the Bf-blender-cvs mailing list