[Bf-blender-cvs] [36d8a70] master: Code refactor: Change Cycles ImageManager arrays.
Thomas Dinges
noreply at git.blender.org
Fri May 6 13:20:10 CEST 2016
Commit: 36d8a70b00c2c0a1bc28ffad4cc2e283a77d7bab
Author: Thomas Dinges
Date: Fri May 6 11:57:30 2016 +0200
Branches: master
https://developer.blender.org/rB36d8a70b00c2c0a1bc28ffad4cc2e283a77d7bab
Code refactor: Change Cycles ImageManager arrays.
This commit simplifies the code for the image arrays. Instead of having 2 arrays for float and byte textures,
we now use an array here. This simplifies the code (avoids code duplication), and makes it possible to easily extend it
with float1 and half-float types in the future.
Only tested with CPU yet, plus some cleanup / code de-duplication is still possible here.
Reviewers: #cycles, sergey
Reviewed By: #cycles, sergey
Subscribers: jesterking, sergey
Differential Revision: https://developer.blender.org/D1969
===================================================================
M intern/cycles/render/image.cpp
M intern/cycles/render/image.h
===================================================================
diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index c0cbf0a..c763651 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -41,32 +41,32 @@ ImageManager::ImageManager(const DeviceInfo& info)
/* CPU */
if(info.type == DEVICE_CPU) {
- tex_num_byte_images = TEX_NUM_BYTE_IMAGES_CPU;
- tex_num_float_images = TEX_NUM_FLOAT_IMAGES_CPU;
+ tex_num_images[IMAGE_DATA_TYPE_BYTE] = TEX_NUM_BYTE_IMAGES_CPU;
+ tex_num_images[IMAGE_DATA_TYPE_FLOAT] = TEX_NUM_FLOAT_IMAGES_CPU;
tex_image_byte_start = TEX_IMAGE_BYTE_START_CPU;
}
/* CUDA (Fermi) */
else if((info.type == DEVICE_CUDA || info.type == DEVICE_MULTI) && !info.extended_images) {
- tex_num_byte_images = TEX_NUM_BYTE_IMAGES_CUDA;
- tex_num_float_images = TEX_NUM_FLOAT_IMAGES_CUDA;
+ tex_num_images[IMAGE_DATA_TYPE_BYTE] = TEX_NUM_BYTE_IMAGES_CUDA;
+ tex_num_images[IMAGE_DATA_TYPE_FLOAT] = TEX_NUM_FLOAT_IMAGES_CUDA;
tex_image_byte_start = TEX_IMAGE_BYTE_START_CUDA;
}
/* CUDA (Kepler and above) */
else if((info.type == DEVICE_CUDA || info.type == DEVICE_MULTI) && info.extended_images) {
- tex_num_byte_images = TEX_NUM_BYTE_IMAGES_CUDA_KEPLER;
- tex_num_float_images = TEX_NUM_FLOAT_IMAGES_CUDA_KEPLER;
+ tex_num_images[IMAGE_DATA_TYPE_BYTE] = TEX_NUM_BYTE_IMAGES_CUDA_KEPLER;
+ tex_num_images[IMAGE_DATA_TYPE_FLOAT] = TEX_NUM_FLOAT_IMAGES_CUDA_KEPLER;
tex_image_byte_start = TEX_IMAGE_BYTE_START_CUDA_KELPER;
}
/* OpenCL */
else if(info.pack_images) {
- tex_num_byte_images = TEX_NUM_BYTE_IMAGES_OPENCL;
- tex_num_float_images = TEX_NUM_FLOAT_IMAGES_OPENCL;
+ tex_num_images[IMAGE_DATA_TYPE_BYTE] = TEX_NUM_BYTE_IMAGES_OPENCL;
+ tex_num_images[IMAGE_DATA_TYPE_FLOAT] = TEX_NUM_FLOAT_IMAGES_OPENCL;
tex_image_byte_start = TEX_IMAGE_BYTE_START_OPENCL;
}
/* Should never happen */
else {
- tex_num_byte_images = 0;
- tex_num_float_images = 0;
+ tex_num_images[IMAGE_DATA_TYPE_BYTE] = 0;
+ tex_num_images[IMAGE_DATA_TYPE_FLOAT] = 0;
tex_image_byte_start = 0;
assert(0);
}
@@ -74,10 +74,10 @@ ImageManager::ImageManager(const DeviceInfo& info)
ImageManager::~ImageManager()
{
- for(size_t slot = 0; slot < images.size(); slot++)
- assert(!images[slot]);
- for(size_t slot = 0; slot < float_images.size(); slot++)
- assert(!float_images[slot]);
+ 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]);
+ }
}
void ImageManager::set_pack_images(bool pack_images_)
@@ -95,15 +95,14 @@ bool ImageManager::set_animation_frame_update(int frame)
if(frame != animation_frame) {
animation_frame = frame;
- for(size_t slot = 0; slot < images.size(); slot++)
- if(images[slot] && images[slot]->animated)
- return true;
-
- for(size_t slot = 0; slot < float_images.size(); slot++)
- if(float_images[slot] && float_images[slot]->animated)
- return true;
+ 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]->animated)
+ return true;
+ }
+ }
}
-
+
return false;
}
@@ -170,6 +169,34 @@ bool ImageManager::is_float_image(const string& filename, void *builtin_data, bo
return is_float;
}
+int ImageManager::type_index_to_flattened_slot(int slot, ImageDataType type)
+{
+ if(type == IMAGE_DATA_TYPE_BYTE)
+ return slot + tex_image_byte_start;
+ else
+ return slot;
+}
+
+int ImageManager::flattened_slot_to_type_index(int slot, ImageDataType *type)
+{
+ if(slot >= tex_image_byte_start) {
+ *type = IMAGE_DATA_TYPE_BYTE;
+ return slot -= tex_image_byte_start;
+ }
+ else {
+ *type = IMAGE_DATA_TYPE_FLOAT;
+ return slot;
+ }
+}
+
+string ImageManager::name_from_type(int type)
+{
+ if(type == IMAGE_DATA_TYPE_FLOAT)
+ return "float";
+ else
+ return "byte";
+}
+
static bool image_equals(ImageManager::Image *image,
const string& filename,
void *builtin_data,
@@ -195,152 +222,85 @@ int ImageManager::add_image(const string& filename,
Image *img;
size_t slot;
- /* load image info and find out if we need a float texture */
+ /* Load image info and find out if we need a float texture. */
is_float = (pack_images)? false: is_float_image(filename, builtin_data, is_linear);
- if(is_float) {
- /* find existing image */
- for(slot = 0; slot < float_images.size(); slot++) {
- img = float_images[slot];
- if(img && image_equals(img,
- filename,
- builtin_data,
- interpolation,
- extension))
- {
- if(img->frame != frame) {
- img->frame = frame;
- img->need_load = true;
- }
- if(img->use_alpha != use_alpha) {
- img->use_alpha = use_alpha;
- img->need_load = true;
- }
- img->users++;
- return slot;
- }
- }
-
- /* find free slot */
- for(slot = 0; slot < float_images.size(); slot++) {
- if(!float_images[slot])
- break;
- }
+ ImageDataType type = is_float? IMAGE_DATA_TYPE_FLOAT : IMAGE_DATA_TYPE_BYTE;
- if(slot == float_images.size()) {
- /* max images limit reached */
- if(float_images.size() == tex_num_float_images) {
- printf("ImageManager::add_image: float image limit reached %d, skipping '%s'\n",
- tex_num_float_images, filename.c_str());
- return -1;
+ /* Fnd existing image. */
+ for(slot = 0; slot < images[type].size(); slot++) {
+ img = images[type][slot];
+ if(img && image_equals(img,
+ filename,
+ builtin_data,
+ interpolation,
+ extension))
+ {
+ if(img->frame != frame) {
+ img->frame = frame;
+ img->need_load = true;
}
-
- float_images.resize(float_images.size() + 1);
- }
-
- /* add new image */
- img = new Image();
- img->filename = filename;
- img->builtin_data = builtin_data;
- img->need_load = true;
- img->animated = animated;
- img->frame = frame;
- img->interpolation = interpolation;
- img->extension = extension;
- img->users = 1;
- img->use_alpha = use_alpha;
-
- float_images[slot] = img;
- }
- else {
- for(slot = 0; slot < images.size(); slot++) {
- img = images[slot];
- if(img && image_equals(img,
- filename,
- builtin_data,
- interpolation,
- extension))
- {
- if(img->frame != frame) {
- img->frame = frame;
- img->need_load = true;
- }
- if(img->use_alpha != use_alpha) {
- img->use_alpha = use_alpha;
- img->need_load = true;
- }
- img->users++;
- return slot+tex_image_byte_start;
+ if(img->use_alpha != use_alpha) {
+ img->use_alpha = use_alpha;
+ img->need_load = true;
}
+ img->users++;
+ return type_index_to_flattened_slot(slot, type);
}
+ }
- /* find free slot */
- for(slot = 0; slot < images.size(); slot++) {
- if(!images[slot])
- break;
- }
-
- if(slot == images.size()) {
- /* max images limit reached */
- if(images.size() == tex_num_byte_images) {
- printf("ImageManager::add_image: byte image limit reached %d, skipping '%s'\n",
- tex_num_byte_images, filename.c_str());
- return -1;
- }
+ /* Find free slot. */
+ for(slot = 0; slot < images[type].size(); slot++) {
+ if(!images[type][slot])
+ break;
+ }
- images.resize(images.size() + 1);
+ if(slot == images[type].size()) {
+ /* Max images limit reached. */
+ if(images[type].size() == tex_num_images[type]) {
+ printf("ImageManager::add_image: Reached %s image limit (%d), skipping '%s'\n",
+ name_from_type(type).c_str(), tex_num_images[type], filename.c_str());
+ return -1;
}
- /* add new image */
- img = new Image();
- img->filename = filename;
- img->builtin_data = builtin_data;
- img->need_load = true;
- img->animated = animated;
- img->frame = frame;
- img->interpolation = interpolation;
- img->extension = extension;
- img->users = 1;
- img->use_alpha = use_alpha;
+ images[type].resize(images[type].size() + 1);
+ }
- images[slot] = img;
+ /* Add new image. */
+ img = new Image();
+ img->filename = filename;
+ img->builtin_data = builtin_data;
+ img->need_load = true;
+ img->animated = animated;
+ img->frame = frame;
+ img->interpolation = interpolation;
+ img->extension = extension;
+ img->users = 1;
+ img->use_alpha = use_alpha;
- slot += tex_image_byte_start;
- }
+ images[type][slot] = img;
need_update = true;
- return slot;
+ return type_index_to_flattened_slot(slot, type);
}
void ImageManager::remove_image(int slot)
{
- if(slot >= tex_image_byte_start) {
- slot -= tex_image_byte_start;
+ ImageDataType type;
+ slot = flattened_slot_to_type_index(slot, &type);
- assert(images[slot] != NULL);
+ assert(images[type][slot] != NULL);
- /* decrement user count */
- images[slot]->users--;
- assert(images[slot]->users >= 0);
-
- /* don't remove immediately, rather do it all together later on. one of
- * the reasons for this is that on shader changes we add and remove nodes
- * that use them, but we do not want to reload the image all the time. */
- if(images[slot]->users == 0)
- need_update = true;
- }
- else {
- /* decrement user count */
- float_images[slot]->users--;
- assert(float_images[slot]->users >= 0);
+ /* decrement user count */
+ images[type][slot]->users--;
+ assert(images[type][slot]->users >= 0);
- /* don't remove immediately, rather do it all together later on. one of
- * the reasons for this is that on shader changes we add and remove nodes
- * that use them, but we do not want to reload the image all the time. */
- if(float_images[slot]->users == 0)
- need_update = true;
- }
+ /* don't remove immediately, rather do it all together later on. one of
+ * the reasons for this is that on shader changes we add and remove nodes
+ * that use them, but we do not want to reload the image all the time. */
+ if(images[type][slot]->users == 0)
+ need_update = true;
}
void ImageManager::remove_image(const string& filename,
@@ -350,29 +310,16 @@ void ImageManager::remove_image(const string& filename
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list