[Bf-blender-cvs] [76a6b204282] pygpu_extensions: Update texture creation: Dimensions are now passed as a tuple or int

Germano Cavalcante noreply at git.blender.org
Fri Feb 12 19:23:43 CET 2021


Commit: 76a6b204282dd020b6bbd50eff5934ff887ecbc1
Author: Germano Cavalcante
Date:   Fri Feb 12 15:23:34 2021 -0300
Branches: pygpu_extensions
https://developer.blender.org/rB76a6b204282dd020b6bbd50eff5934ff887ecbc1

Update texture creation: Dimensions are now passed as a tuple or int

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

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

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

diff --git a/source/blender/python/gpu/gpu_py_texture.c b/source/blender/python/gpu/gpu_py_texture.c
index 0021da9f9d4..ab3d926c760 100644
--- a/source/blender/python/gpu/gpu_py_texture.c
+++ b/source/blender/python/gpu/gpu_py_texture.c
@@ -111,28 +111,50 @@ static int py_texture_valid_check(BPyGPUTexture *bpygpu_tex)
 /** \name GPUTexture Type
  * \{ */
 
+inline int py_texture_component_len(eGPUTextureFormat format)
+{
+  switch (format) {
+    case GPU_RGBA8:
+    case GPU_RGBA8UI:
+    case GPU_RGBA16F:
+    case GPU_RGBA16:
+    case GPU_RGBA32F:
+    case GPU_SRGB8_A8:
+      return 4;
+    case GPU_RGB16F:
+    case GPU_R11F_G11F_B10F:
+      return 3;
+    case GPU_RG8:
+    case GPU_RG16:
+    case GPU_RG16F:
+    case GPU_RG16I:
+    case GPU_RG16UI:
+    case GPU_RG32F:
+      return 2;
+    default:
+      return 1;
+  }
+}
+
 static PyObject *py_texture_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
 {
   BPYGPU_IS_INIT_OR_ERROR_OBJ;
 
-  GPUTexture *tex = NULL;
-  int width;
-  int height = 0, depth = 0;
+  PyObject *py_size;
+  int size[3] = {1, 1, 1};
+  int layers = 0;
+  int is_cubemap = false;
   struct PyC_StringEnum pygpu_textureformat = {pygpu_textureformat_items, GPU_RGBA8};
-  int is_layered = false, is_cubemap = false;
   PyBuffer *pybuffer_obj = NULL;
   char err_out[256] = "unknown error. See console";
 
-  static const char *_keywords[] = {
-      "width", "height", "depth", "is_layered", "is_cubemap", "format", "data", NULL};
-  static _PyArg_Parser _parser = {"i|ii$ppO&O!:GPUTexture.__new__", _keywords, 0};
+  static const char *_keywords[] = {"size", "layers", "is_cubemap", "format", "data", NULL};
+  static _PyArg_Parser _parser = {"O|$ipO&O!:GPUTexture.__new__", _keywords, 0};
   if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                         kwds,
                                         &_parser,
-                                        &width,
-                                        &height,
-                                        &depth,
-                                        &is_layered,
+                                        &py_size,
+                                        &layers,
                                         &is_cubemap,
                                         PyC_ParseStringEnum,
                                         &pygpu_textureformat,
@@ -141,6 +163,21 @@ static PyObject *py_texture_new(PyTypeObject *UNUSED(self), PyObject *args, PyOb
     return NULL;
   }
 
+  int len = 1;
+  if (PyTuple_Check(py_size)) {
+    len = PyTuple_Size(py_size);
+    if (PyC_AsArray(size, py_size, len, &PyLong_Type, false, "GPUTexture.__new__") == -1) {
+      return NULL;
+    }
+  }
+  else if (PyLong_Check(py_size)) {
+    size[0] = PyLong_AsLong(py_size);
+  }
+  else {
+    PyErr_SetString(PyExc_ValueError, "GPUTexture.__new__: Expected an int or tuple as first arg");
+    return NULL;
+  }
+
   void *data = NULL;
   if (pybuffer_obj) {
     if (pybuffer_obj->format != GPU_DATA_FLOAT) {
@@ -149,25 +186,29 @@ static PyObject *py_texture_new(PyTypeObject *UNUSED(self), PyObject *args, PyOb
       return NULL;
     }
 
-    size_t texture_space = (max_ii(width, 1) * max_ii(height, 1) * sizeof(float));
-    if (bpygpu_Buffer_size(pybuffer_obj) < texture_space) {
+    int component_len = py_texture_component_len(pygpu_textureformat.value_found);
+    int component_size_expected = sizeof(float);
+    size_t data_space_expected = (size_t)size[0] * size[1] * size[2] * component_len *
+                                 component_size_expected;
+
+    if (bpygpu_Buffer_size(pybuffer_obj) < data_space_expected) {
       PyErr_SetString(PyExc_ValueError, "GPUTexture.__new__: Buffer size smaller than requested");
       return NULL;
     }
     data = pybuffer_obj->buf.asvoid;
   }
 
-  if (is_cubemap && height != 0) {
-    strncpy(err_out, "cubemaps reuse the 'width' value as 'height', no need to set a value", 256);
-  }
-  else if (is_cubemap && is_layered && depth == 0) {
-    strncpy(err_out, "layered cubemaps require depth", 256);
+  GPUTexture *tex = NULL;
+  if (is_cubemap && len != 1) {
+    strncpy(err_out,
+            "In cubemaps the same dimension represents height, width and depth. No tuple needed",
+            256);
   }
-  else if (!is_cubemap && is_layered && height == 0) {
-    strncpy(err_out, "layered textures require height", 256);
+  else if (size[0] < 1 || size[1] < 1 || size[2] < 1) {
+    strncpy(err_out, "Values less than 1 are not allowed in dimensions", 256);
   }
-  else if (height == 0 && depth != 0) {
-    strncpy(err_out, "except for cubemaps, textures with depth require height", 256);
+  else if (layers && len == 3) {
+    strncpy(err_out, "3D textures have no layers", 256);
   }
   else if (!GPU_context_active_get()) {
     strncpy(err_out, "No active GPU context found", 256);
@@ -175,33 +216,40 @@ static PyObject *py_texture_new(PyTypeObject *UNUSED(self), PyObject *args, PyOb
   else {
     const char *name = "python_texture";
     if (is_cubemap) {
-      if (is_layered) {
+      if (layers) {
         tex = GPU_texture_create_cube_array(
-            name, width, depth, 1, pygpu_textureformat.value_found, data);
+            name, size[0], layers, 1, pygpu_textureformat.value_found, data);
       }
       else {
-        tex = GPU_texture_create_cube(name, width, 1, pygpu_textureformat.value_found, data);
+        tex = GPU_texture_create_cube(name, size[0], 1, pygpu_textureformat.value_found, data);
       }
     }
-    else if (is_layered) {
-      if (depth) {
+    else if (layers) {
+      if (len == 2) {
         tex = GPU_texture_create_2d_array(
-            name, width, height, depth, 1, pygpu_textureformat.value_found, data);
+            name, size[0], size[1], layers, 1, pygpu_textureformat.value_found, data);
       }
       else {
         tex = GPU_texture_create_1d_array(
-            name, width, height, 1, pygpu_textureformat.value_found, data);
+            name, size[0], layers, 1, pygpu_textureformat.value_found, data);
       }
     }
-    else if (depth) {
-      tex = GPU_texture_create_3d(
-          name, width, height, depth, 1, pygpu_textureformat.value_found, GPU_DATA_FLOAT, data);
+    else if (len == 3) {
+      tex = GPU_texture_create_3d(name,
+                                  size[0],
+                                  size[1],
+                                  size[2],
+                                  1,
+                                  pygpu_textureformat.value_found,
+                                  GPU_DATA_FLOAT,
+                                  NULL);
     }
-    else if (height) {
-      tex = GPU_texture_create_2d(name, width, height, 1, pygpu_textureformat.value_found, data);
+    else if (len == 2) {
+      tex = GPU_texture_create_2d(
+          name, size[0], size[1], 1, pygpu_textureformat.value_found, data);
     }
     else {
-      tex = GPU_texture_create_1d(name, width, 1, pygpu_textureformat.value_found, data);
+      tex = GPU_texture_create_1d(name, size[0], 1, pygpu_textureformat.value_found, data);
     }
   }
 
@@ -347,70 +395,66 @@ static struct PyMethodDef py_texture_methods[] = {
     {NULL, NULL, 0, NULL},
 };
 
-PyDoc_STRVAR(py_texture_doc,
-             ".. class:: GPUTexture(width, height, depth, is_layered=False, is_cubemap=False, "
-             "format='RGBA8', data=None)\n"
-             "\n"
-             "   This object gives access to off GPU textures.\n"
-             "\n"
-             "   :arg width: Horizontal dimension of the texture.\n"
-             "   :type width: `int`\n"
-             "   :arg height: Vertical dimension of the texture.\n"
-             "   :type height: `int`\n"
-             "   :arg depth: Depth dimension of the texture.\n"
-             "   :type depth: `int`\n"
-             "   :arg is_layered: Indicates the creation of an array texture.\n"
-             "   :type is_layered: `bool`\n"
-             "   :arg is_cubemap: Indicates the creation of a cubemap texture.\n"
-             "   :type is_cubemap: `int`\n"
-             "   :arg format: One of these primitive types: {\n"
-             "      `RGBA8UI`,\n"
-             "      `RGBA8I`,\n"
-             "      `RGBA8`,\n"
-             "      `RGBA32UI`,\n"
-             "      `RGBA32I`,\n"
-             "      `RGBA32F`,\n"
-             "      `RGBA16UI`,\n"
-             "      `RGBA16I`,\n"
-             "      `RGBA16F`,\n"
-             "      `RGBA16`,\n"
-             "      `RG8UI`,\n"
-             "      `RG8I`,\n"
-             "      `RG8`,\n"
-             "      `RG32UI`,\n"
-             "      `RG32I`,\n"
-             "      `RG32F`,\n"
-             "      `RG16UI`,\n"
-             "      `RG16I`,\n"
-             "      `RG16F`,\n"
-             "      `RG16`,\n"
-             "      `R8UI`,\n"
-             "      `R8I`,\n"
-             "      `R8`,\n"
-             "      `R32UI`,\n"
-             "      `R32I`,\n"
-             "      `R32F`,\n"
-             "      `R16UI`,\n"
-             "      `R16I`,\n"
-             "      `R16F`,\n"
-             "      `R16`,\n"
-             "      `R11F_G11F_B10F`,\n"
-             "      `DEPTH32F_STENCIL8`,\n"
-             "      `DEPTH24_STENCIL8`,\n"
-             "      `SRGB8_A8`,\n"
-             "      `RGB16F`,\n"
-             "      `SRGB8_A8_DXT1`,\n"
-             "      `SRGB8_A8_DXT3`,\n"
-             "      `SRGB8_A8_DXT5`,\n"
-             "      `RGBA8_DXT1`,\n"
-             "      `RGBA8_DXT3`,\n"
-             "      `RGBA8_DXT5`,\n"
-             "      `DEPTH_COMPONENT32F`,\n"
-             "      `DEPTH_COMPONENT24`,\n"
-             "      `DEPTH_COMPONENT16`,\n"
-             "   :type format: `str`\n"
-             "   :arg data: Buffer object to fill the texture.\n"
-             "   :type data: `Buffer`\n");
+PyDoc_STRVAR(
+    py_texture_doc,
+    ".. class:: GPUTexture(size, layers=0, is_cubemap=False, format='RGBA8', data=None)\n"
+    "\n"
+    "   This object gives access to off GPU textures.\n"
+    "\n"
+    "   :arg size: Dimensions of the texture 1D, 2D, 3D or cubemap.\n"
+    "   :type size: `tuple` or `int`\n"
+    "   :arg layers: Number of layers in te

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list