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
This commit is contained in:
Thomas Dinges 2016-05-06 11:57:30 +02:00
parent 8aa3bac7af
commit 36d8a70b00
2 changed files with 202 additions and 285 deletions

View File

@ -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;
ImageDataType type = is_float? IMAGE_DATA_TYPE_FLOAT : IMAGE_DATA_TYPE_BYTE;
/* 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;
}
}
/* find free slot */
for(slot = 0; slot < float_images.size(); slot++) {
if(!float_images[slot])
break;
}
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;
if(img->use_alpha != use_alpha) {
img->use_alpha = use_alpha;
img->need_load = true;
}
float_images.resize(float_images.size() + 1);
img->users++;
return type_index_to_flattened_slot(slot, type);
}
/* 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;
}
}
/* 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;
}
images.resize(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;
images[slot] = img;
slot += tex_image_byte_start;
/* Find free slot. */
for(slot = 0; slot < images[type].size(); slot++) {
if(!images[type][slot])
break;
}
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;
}
images[type].resize(images[type].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;
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);
/* 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(images[slot]->users == 0)
need_update = true;
}
else {
/* decrement user count */
float_images[slot]->users--;
assert(float_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(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,
{
size_t slot;
for(slot = 0; slot < images.size(); slot++) {
if(images[slot] && image_equals(images[slot],
filename,
builtin_data,
interpolation,
extension))
{
remove_image(slot+tex_image_byte_start);
break;
}
}
if(slot == images.size()) {
/* see if it's in a float texture slot */
for(slot = 0; slot < float_images.size(); slot++) {
if(float_images[slot] && image_equals(float_images[slot],
filename,
builtin_data,
interpolation,
extension))
for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
for(slot = 0; slot < images[type].size(); slot++) {
if(images[type][slot] && image_equals(images[type][slot],
filename,
builtin_data,
interpolation,
extension))
{
remove_image(slot);
break;
remove_image(type_index_to_flattened_slot(slot, (ImageDataType)type));
return;
}
}
}
@ -387,30 +334,15 @@ void ImageManager::tag_reload_image(const string& filename,
InterpolationType interpolation,
ExtensionType extension)
{
size_t slot;
for(slot = 0; slot < images.size(); slot++) {
if(images[slot] && image_equals(images[slot],
filename,
builtin_data,
interpolation,
extension))
{
images[slot]->need_load = true;
break;
}
}
if(slot == images.size()) {
/* see if it's in a float texture slot */
for(slot = 0; slot < float_images.size(); slot++) {
if(float_images[slot] && image_equals(float_images[slot],
filename,
builtin_data,
interpolation,
extension))
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] && image_equals(images[type][slot],
filename,
builtin_data,
interpolation,
extension))
{
float_images[slot]->need_load = true;
images[type][slot]->need_load = true;
break;
}
}
@ -696,25 +628,21 @@ void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int sl
return;
Image *img;
bool is_float;
ImageDataType type;
int slot_offset;
if(slot >= tex_image_byte_start) {
img = images[slot - tex_image_byte_start];
is_float = false;
}
else {
img = float_images[slot];
is_float = true;
}
slot_offset = flattened_slot_to_type_index(slot, &type);
img = images[type][slot_offset];
if(osl_texture_system && !img->builtin_data)
return;
if(is_float) {
string filename = path_filename(float_images[slot]->filename);
progress->set_status("Updating Images", "Loading " + filename);
string filename = path_filename(images[type][slot_offset]->filename);
progress->set_status("Updating Images", "Loading " + filename);
device_vector<float4>& tex_img = dscene->tex_float_image[slot];
if(type == IMAGE_DATA_TYPE_FLOAT) {
device_vector<float4>& tex_img = dscene->tex_float_image[slot_offset];
if(tex_img.device_pointer) {
thread_scoped_lock device_lock(device_mutex);
@ -746,10 +674,7 @@ void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int sl
}
}
else {
string filename = path_filename(images[slot - tex_image_byte_start]->filename);
progress->set_status("Updating Images", "Loading " + filename);
device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
device_vector<uchar4>& tex_img = dscene->tex_image[slot_offset];
if(tex_img.device_pointer) {
thread_scoped_lock device_lock(device_mutex);
@ -787,26 +712,21 @@ void ImageManager::device_load_image(Device *device, DeviceScene *dscene, int sl
void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int slot)
{
Image *img;
bool is_float;
ImageDataType type;
if(slot >= tex_image_byte_start) {
img = images[slot - tex_image_byte_start];
is_float = false;
}
else {
img = float_images[slot];
is_float = true;
}
int slot_offset = flattened_slot_to_type_index(slot, &type);
img = images[type][slot_offset];
if(img) {
if(osl_texture_system && !img->builtin_data) {
#ifdef WITH_OSL
ustring filename(images[slot]->filename);
ustring filename(images[type][slot]->filename);
((OSL::TextureSystem*)osl_texture_system)->invalidate(filename);
#endif
}
else if(is_float) {
device_vector<float4>& tex_img = dscene->tex_float_image[slot];
else if(type == IMAGE_DATA_TYPE_FLOAT) {
device_vector<float4>& tex_img = dscene->tex_float_image[slot_offset];
if(tex_img.device_pointer) {
thread_scoped_lock device_lock(device_mutex);
@ -815,11 +735,11 @@ void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int sl
tex_img.clear();
delete float_images[slot];
float_images[slot] = NULL;
delete images[type][slot_offset];
images[type][slot_offset] = NULL;
}
else {
device_vector<uchar4>& tex_img = dscene->tex_image[slot - tex_image_byte_start];
device_vector<uchar4>& tex_img = dscene->tex_image[slot_offset];
if(tex_img.device_pointer) {
thread_scoped_lock device_lock(device_mutex);
@ -828,8 +748,8 @@ void ImageManager::device_free_image(Device *device, DeviceScene *dscene, int sl
tex_img.clear();
delete images[slot - tex_image_byte_start];
images[slot - tex_image_byte_start] = NULL;
delete images[type][slot_offset];
images[type][slot_offset] = NULL;
}
}
}
@ -841,29 +761,18 @@ void ImageManager::device_update(Device *device, DeviceScene *dscene, Progress&
TaskPool pool;
for(size_t slot = 0; slot < images.size(); slot++) {
if(!images[slot])
continue;
for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
for(size_t slot = 0; slot < images[type].size(); slot++) {
if(!images[type][slot])
continue;
if(images[slot]->users == 0) {
device_free_image(device, dscene, slot + tex_image_byte_start);
}
else if(images[slot]->need_load) {
if(!osl_texture_system || images[slot]->builtin_data)
pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot + tex_image_byte_start, &progress));
}
}
for(size_t slot = 0; slot < float_images.size(); slot++) {
if(!float_images[slot])
continue;
if(float_images[slot]->users == 0) {
device_free_image(device, dscene, slot);
}
else if(float_images[slot]->need_load) {
if(!osl_texture_system || float_images[slot]->builtin_data)
pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, slot, &progress));
if(images[type][slot]->users == 0) {
device_free_image(device, dscene, slot + tex_image_byte_start);
}
else if(images[type][slot]->need_load) {
if(!osl_texture_system || images[type][slot]->builtin_data)
pool.push(function_bind(&ImageManager::device_load_image, this, device, dscene, type_index_to_flattened_slot(slot, (ImageDataType)type), &progress));
}
}
}
@ -881,20 +790,18 @@ void ImageManager::device_update_slot(Device *device,
Progress *progress)
{
Image *image;
if(slot >= tex_image_byte_start) {
int byte_slot = slot - tex_image_byte_start;
assert(images[byte_slot] != NULL);
image = images[byte_slot];
}
else {
assert(float_images[slot] != NULL);
image = float_images[slot];
}
ImageDataType type;
slot = flattened_slot_to_type_index(slot, &type);
assert(images[type][slot] != NULL);
image = images[type][slot];
if(image->users == 0) {
device_free_image(device, dscene, slot);
}
else if(image->need_load) {
if(!osl_texture_system || float_images[slot]->builtin_data)
if(!osl_texture_system || image->builtin_data)
device_load_image(device,
dscene,
slot,
@ -910,21 +817,24 @@ void ImageManager::device_pack_images(Device *device,
* will do our own interpolation in the kernel */
size_t size = 0;
for(size_t slot = 0; slot < images.size(); slot++) {
if(!images[slot])
/* Only byte textures are supported atm */
ImageDataType type = IMAGE_DATA_TYPE_BYTE;
for(size_t slot = 0; slot < images[type].size(); slot++) {
if(!images[type][slot])
continue;
device_vector<uchar4>& tex_img = dscene->tex_image[slot];
size += tex_img.size();
}
uint4 *info = dscene->tex_image_packed_info.resize(images.size());
uint4 *info = dscene->tex_image_packed_info.resize(images[type].size());
uchar4 *pixels = dscene->tex_image_packed.resize(size);
size_t offset = 0;
for(size_t slot = 0; slot < images.size(); slot++) {
if(!images[slot])
for(size_t slot = 0; slot < images[type].size(); slot++) {
if(!images[type][slot])
continue;
device_vector<uchar4>& tex_img = dscene->tex_image[slot];
@ -934,7 +844,7 @@ void ImageManager::device_pack_images(Device *device,
/* The image options are packed
bit 0 -> periodic
bit 1 + 2 -> interpolation type */
uint8_t interpolation = (images[slot]->interpolation << 1) + 1;
uint8_t interpolation = (images[type][slot]->interpolation << 1) + 1;
info[slot] = make_uint4(tex_img.data_width, tex_img.data_height, offset, interpolation);
memcpy(pixels+offset, (void*)tex_img.data_pointer, tex_img.memory_size());
@ -959,30 +869,28 @@ void ImageManager::device_pack_images(Device *device,
void ImageManager::device_free_builtin(Device *device, DeviceScene *dscene)
{
for(size_t slot = 0; slot < images.size(); slot++)
if(images[slot] && images[slot]->builtin_data)
device_free_image(device, dscene, slot + tex_image_byte_start);
for(size_t slot = 0; slot < float_images.size(); slot++)
if(float_images[slot] && float_images[slot]->builtin_data)
device_free_image(device, dscene, slot);
for(int 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]->builtin_data)
device_free_image(device, dscene, type_index_to_flattened_slot(slot, (ImageDataType)type));
}
}
}
void ImageManager::device_free(Device *device, DeviceScene *dscene)
{
for(size_t slot = 0; slot < images.size(); slot++)
device_free_image(device, dscene, slot + tex_image_byte_start);
for(size_t slot = 0; slot < float_images.size(); slot++)
device_free_image(device, dscene, slot);
for(int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
for(size_t slot = 0; slot < images[type].size(); slot++) {
device_free_image(device, dscene, type_index_to_flattened_slot(slot, (ImageDataType)type));
}
images[type].clear();
}
device->tex_free(dscene->tex_image_packed);
device->tex_free(dscene->tex_image_packed_info);
dscene->tex_image_packed.clear();
dscene->tex_image_packed_info.clear();
images.clear();
float_images.clear();
}
CCL_NAMESPACE_END

View File

@ -35,6 +35,13 @@ public:
ImageManager(const DeviceInfo& info);
~ImageManager();
enum ImageDataType {
IMAGE_DATA_TYPE_FLOAT = 0,
IMAGE_DATA_TYPE_BYTE = 1,
IMAGE_DATA_NUM_TYPES
};
int add_image(const string& filename,
void *builtin_data,
bool animated,
@ -85,20 +92,22 @@ public:
};
private:
int tex_num_byte_images;
int tex_num_float_images;
int tex_num_images[IMAGE_DATA_NUM_TYPES];
int tex_image_byte_start;
thread_mutex device_mutex;
int animation_frame;
vector<Image*> images;
vector<Image*> float_images;
vector<Image*> images[IMAGE_DATA_NUM_TYPES];
void *osl_texture_system;
bool pack_images;
bool file_load_image(Image *img, device_vector<uchar4>& tex_img);
bool file_load_float_image(Image *img, device_vector<float4>& tex_img);
int type_index_to_flattened_slot(int slot, ImageDataType type);
int flattened_slot_to_type_index(int slot, ImageDataType *type);
string name_from_type(int type);
void device_load_image(Device *device, DeviceScene *dscene, int slot, Progress *progess);
void device_free_image(Device *device, DeviceScene *dscene, int slot);