[Bf-blender-cvs] [f01bc597a8e] master: Cleanup: stop encoding image data type in slot index

Brecht Van Lommel noreply at git.blender.org
Wed Mar 11 17:08:44 CET 2020


Commit: f01bc597a8e6bf5df19f1af0c422918c96b25e41
Author: Brecht Van Lommel
Date:   Wed Feb 26 17:31:33 2020 +0100
Branches: master
https://developer.blender.org/rBf01bc597a8e6bf5df19f1af0c422918c96b25e41

Cleanup: stop encoding image data type in slot index

This is legacy code from when we had a fixed number of textures.

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

M	intern/cycles/device/cuda/device_cuda_impl.cpp
M	intern/cycles/device/device_cpu.cpp
M	intern/cycles/device/device_memory.cpp
M	intern/cycles/device/device_memory.h
M	intern/cycles/device/opencl/device_opencl_impl.cpp
M	intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h
M	intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h
M	intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h
M	intern/cycles/render/image.cpp
M	intern/cycles/render/image.h
M	intern/cycles/util/util_texture.h

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

diff --git a/intern/cycles/device/cuda/device_cuda_impl.cpp b/intern/cycles/device/cuda/device_cuda_impl.cpp
index 4e8d8b7ca7c..4df1ca2097a 100644
--- a/intern/cycles/device/cuda/device_cuda_impl.cpp
+++ b/intern/cycles/device/cuda/device_cuda_impl.cpp
@@ -1169,10 +1169,10 @@ void CUDADevice::tex_alloc(device_memory &mem)
   }
 
   /* Kepler+, bindless textures. */
-  int flat_slot = 0;
+  int slot = 0;
   if (string_startswith(mem.name, "__tex_image")) {
     int pos = string(mem.name).rfind("_");
-    flat_slot = atoi(mem.name + pos + 1);
+    slot = atoi(mem.name + pos + 1);
   }
   else {
     assert(0);
@@ -1214,15 +1214,16 @@ void CUDADevice::tex_alloc(device_memory &mem)
   cuda_assert(cuTexObjectCreate(&cmem->texobject, &resDesc, &texDesc, NULL));
 
   /* Resize once */
-  if (flat_slot >= texture_info.size()) {
+  if (slot >= texture_info.size()) {
     /* Allocate some slots in advance, to reduce amount
      * of re-allocations. */
-    texture_info.resize(flat_slot + 128);
+    texture_info.resize(slot + 128);
   }
 
   /* Set Mapping and tag that we need to (re-)upload to device */
-  TextureInfo &info = texture_info[flat_slot];
+  TextureInfo &info = texture_info[slot];
   info.data = (uint64_t)cmem->texobject;
+  info.data_type = mem.image_data_type;
   info.cl_buffer = 0;
   info.interpolation = mem.interpolation;
   info.extension = mem.extension;
diff --git a/intern/cycles/device/device_cpu.cpp b/intern/cycles/device/device_cpu.cpp
index d11918ccbbf..56569a5ee3d 100644
--- a/intern/cycles/device/device_cpu.cpp
+++ b/intern/cycles/device/device_cpu.cpp
@@ -465,23 +465,24 @@ class CPUDevice : public Device {
     }
     else {
       /* Image Texture. */
-      int flat_slot = 0;
+      int slot = 0;
       if (string_startswith(mem.name, "__tex_image")) {
         int pos = string(mem.name).rfind("_");
-        flat_slot = atoi(mem.name + pos + 1);
+        slot = atoi(mem.name + pos + 1);
       }
       else {
         assert(0);
       }
 
-      if (flat_slot >= texture_info.size()) {
+      if (slot >= texture_info.size()) {
         /* Allocate some slots in advance, to reduce amount
          * of re-allocations. */
-        texture_info.resize(flat_slot + 128);
+        texture_info.resize(slot + 128);
       }
 
-      TextureInfo &info = texture_info[flat_slot];
+      TextureInfo &info = texture_info[slot];
       info.data = (uint64_t)mem.host_pointer;
+      info.data_type = mem.image_data_type;
       info.cl_buffer = 0;
       info.interpolation = mem.interpolation;
       info.extension = mem.extension;
diff --git a/intern/cycles/device/device_memory.cpp b/intern/cycles/device/device_memory.cpp
index 3a99a49dffc..f22b91f3fa1 100644
--- a/intern/cycles/device/device_memory.cpp
+++ b/intern/cycles/device/device_memory.cpp
@@ -31,6 +31,7 @@ device_memory::device_memory(Device *device, const char *name, MemoryType type)
       data_depth(0),
       type(type),
       name(name),
+      image_data_type(IMAGE_DATA_NUM_TYPES),
       interpolation(INTERPOLATION_NONE),
       extension(EXTENSION_REPEAT),
       device(device),
diff --git a/intern/cycles/device/device_memory.h b/intern/cycles/device/device_memory.h
index 2949773ef0c..617cc0c4342 100644
--- a/intern/cycles/device/device_memory.h
+++ b/intern/cycles/device/device_memory.h
@@ -208,6 +208,7 @@ class device_memory {
   size_t data_depth;
   MemoryType type;
   const char *name;
+  ImageDataType image_data_type;
   InterpolationType interpolation;
   ExtensionType extension;
 
diff --git a/intern/cycles/device/opencl/device_opencl_impl.cpp b/intern/cycles/device/opencl/device_opencl_impl.cpp
index 3dbe54b38aa..09d3b78dd28 100644
--- a/intern/cycles/device/opencl/device_opencl_impl.cpp
+++ b/intern/cycles/device/opencl/device_opencl_impl.cpp
@@ -1298,6 +1298,8 @@ void OpenCLDevice::flush_texture_buffers()
     if (string_startswith(slot.name, "__tex_image")) {
       device_memory *mem = textures[slot.name];
 
+      info.data_type = mem->image_data_type;
+
       info.width = mem->data_width;
       info.height = mem->data_height;
       info.depth = mem->data_depth;
diff --git a/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h b/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h
index 8f311baf010..7eb66b0b4ca 100644
--- a/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h
+++ b/intern/cycles/kernel/kernels/cpu/kernel_cpu_image.h
@@ -474,7 +474,7 @@ ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, fl
 {
   const TextureInfo &info = kernel_tex_fetch(__texture_info, id);
 
-  switch (kernel_tex_type(id)) {
+  switch (info.data_type) {
     case IMAGE_DATA_TYPE_HALF:
       return TextureInterpolator<half>::interp(info, x, y);
     case IMAGE_DATA_TYPE_BYTE:
@@ -503,7 +503,7 @@ ccl_device float4 kernel_tex_image_interp_3d(
 {
   const TextureInfo &info = kernel_tex_fetch(__texture_info, id);
 
-  switch (kernel_tex_type(id)) {
+  switch (info.data_type) {
     case IMAGE_DATA_TYPE_HALF:
       return TextureInterpolator<half>::interp_3d(info, x, y, z, interp);
     case IMAGE_DATA_TYPE_BYTE:
diff --git a/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h b/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h
index 7c68f08ea10..24bc3c7b59e 100644
--- a/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h
+++ b/intern/cycles/kernel/kernels/cuda/kernel_cuda_image.h
@@ -124,7 +124,7 @@ ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, fl
   CUtexObject tex = (CUtexObject)info.data;
 
   /* float4, byte4, ushort4 and half4 */
-  const int texture_type = kernel_tex_type(id);
+  const int texture_type = info.data_type;
   if (texture_type == IMAGE_DATA_TYPE_FLOAT4 || texture_type == IMAGE_DATA_TYPE_BYTE4 ||
       texture_type == IMAGE_DATA_TYPE_HALF4 || texture_type == IMAGE_DATA_TYPE_USHORT4) {
     if (info.interpolation == INTERPOLATION_CUBIC) {
@@ -156,7 +156,7 @@ ccl_device float4 kernel_tex_image_interp_3d(
   CUtexObject tex = (CUtexObject)info.data;
   uint interpolation = (interp == INTERPOLATION_NONE) ? info.interpolation : interp;
 
-  const int texture_type = kernel_tex_type(id);
+  const int texture_type = info.data_type;
   if (texture_type == IMAGE_DATA_TYPE_FLOAT4 || texture_type == IMAGE_DATA_TYPE_BYTE4 ||
       texture_type == IMAGE_DATA_TYPE_HALF4 || texture_type == IMAGE_DATA_TYPE_USHORT4) {
     if (interpolation == INTERPOLATION_CUBIC) {
diff --git a/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h b/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h
index b6390679331..f7dea383b82 100644
--- a/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h
+++ b/intern/cycles/kernel/kernels/opencl/kernel_opencl_image.h
@@ -47,7 +47,7 @@ ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg,
                                                 int id,
                                                 int offset)
 {
-  const int texture_type = kernel_tex_type(id);
+  const int texture_type = info->data_type;
 
   /* Float4 */
   if (texture_type == IMAGE_DATA_TYPE_FLOAT4) {
diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index e5fb2fcaf3d..7053d992621 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -50,21 +50,6 @@ bool isfinite(uint16_t /*value*/)
   return true;
 }
 
-/* The lower three bits of a device texture slot number indicate its type.
- * These functions convert the slot ids from ImageManager "images" ones
- * to device ones and vice verse.
- */
-int type_index_to_flattened_slot(int slot, ImageDataType type)
-{
-  return (slot << IMAGE_DATA_TYPE_SHIFT) | (type);
-}
-
-int flattened_slot_to_type_index(int flat_slot, ImageDataType *type)
-{
-  *type = (ImageDataType)(flat_slot & IMAGE_DATA_TYPE_MASK);
-  return flat_slot >> IMAGE_DATA_TYPE_SHIFT;
-}
-
 const char *name_from_type(ImageDataType type)
 {
   switch (type) {
@@ -104,17 +89,13 @@ ImageManager::ImageManager(const DeviceInfo &info)
   max_num_images = TEX_NUM_MAX;
   has_half_images = info.has_half_images;
 
-  for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
-    tex_num_images[type] = 0;
-  }
+  tex_num_images = 0;
 }
 
 ImageManager::~ImageManager()
 {
-  for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
-    for (size_t slot = 0; slot < images[type].size(); slot++)
-      assert(!images[type][slot]);
-  }
+  for (size_t slot = 0; slot < images.size(); slot++)
+    assert(!images[slot]);
 }
 
 void ImageManager::set_osl_texture_system(void *texture_system)
@@ -127,37 +108,32 @@ bool ImageManager::set_animation_frame_update(int frame)
   if (frame != animation_frame) {
     animation_frame = frame;
 
-    for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
-      for (size_t slot = 0; slot < images[type].size(); slot++) {
-        if (images[type][slot] && images[type][slot]->key.animated)
-          return true;
-      }
+    for (size_t slot = 0; slot < images.size(); slot++) {
+      if (images[slot] && images[slot]->key.animated)
+        return true;
     }
   }
 
   return false;
 }
 
-device_memory *ImageManager::image_memory(int flat_slot)
+device_memory *ImageManager::image_memory(int slot)
 {
-  ImageDataType type;
-  int slot = flattened_slot_to_type_index(flat_slot, &type);
-
-  Image *img = images[type][slot];
+  if (slot == -1) {
+    return NULL;
+  }
 
-  return img->mem;
+  Image *img = images[slot];
+  return img ? img->mem : NULL;
 }
 
-bool ImageManager::get_image_metadata(int flat_slot, ImageMetaData &metadata)
+bool ImageManager::get_image_metadata(int slot, ImageMetaData &metadata)
 {
-  if (flat_slot == -1) {
+  if (slot == -1) {
     return false;
   }
 
-  ImageDataType type;
-  int slot = flattened_slot_to_type_index(flat_slot, &type);
-
-  Image *img = images[type][slot];
+  Image *img = images[slot];
   if (img) {
     metadata = img->metadata;
     return true;
@@ -297,23 +273,22 @@ int ImageManager::add_image(const ImageKey &key, float frame, ImageMetaData &met
   size_t slot;
 
   get_image_metadata(key, metadata);
-  ImageDataType type = metadata.type;
 
   thread_scoped_lock device_lock(device_mutex);
 
   /* No half textures on OpenCL, use full float instead. */
   if (

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list