Cycles: Cleanup in image manager, switch statement instead of if-else

Allows to catch enumerator values which are missing from being handled.

Also use `const char*`, no need to construct string just to throw it away.
This commit is contained in:
Sergey Sharybin
2018-07-27 10:16:30 +02:00
parent fe8d8aa27e
commit e977fe985f
2 changed files with 19 additions and 19 deletions

View File

@@ -253,24 +253,23 @@ int ImageManager::flattened_slot_to_type_index(int flat_slot, ImageDataType *typ
return flat_slot >> IMAGE_DATA_TYPE_SHIFT; return flat_slot >> IMAGE_DATA_TYPE_SHIFT;
} }
string ImageManager::name_from_type(int type) const char* ImageManager::name_from_type(ImageDataType type)
{ {
if(type == IMAGE_DATA_TYPE_FLOAT4) switch(type) {
return "float4"; case IMAGE_DATA_TYPE_FLOAT4: return "float4";
else if(type == IMAGE_DATA_TYPE_FLOAT) case IMAGE_DATA_TYPE_BYTE4: return "byte4";
return "float"; case IMAGE_DATA_TYPE_HALF4: return "half4";
else if(type == IMAGE_DATA_TYPE_BYTE) case IMAGE_DATA_TYPE_FLOAT: return "float";
return "byte"; case IMAGE_DATA_TYPE_BYTE: return "byte";
else if(type == IMAGE_DATA_TYPE_HALF4) case IMAGE_DATA_TYPE_HALF: return "half";
return "half4"; case IMAGE_DATA_TYPE_USHORT4: return "ushort4";
else if(type == IMAGE_DATA_TYPE_HALF) case IMAGE_DATA_TYPE_USHORT: return "ushort";
return "half"; case IMAGE_DATA_NUM_TYPES:
else if(type == IMAGE_DATA_TYPE_USHORT) assert(!"System enumerator type, should never be used");
return "ushort"; return "";
else if(type == IMAGE_DATA_TYPE_USHORT4) }
return "ushort4"; assert(!"Unhandled image data type");
else return "";
return "byte4";
} }
static bool image_equals(ImageManager::Image *image, static bool image_equals(ImageManager::Image *image,
@@ -732,7 +731,8 @@ void ImageManager::device_load_image(Device *device,
/* Slot assignment */ /* Slot assignment */
int flat_slot = type_index_to_flattened_slot(slot, type); int flat_slot = type_index_to_flattened_slot(slot, type);
img->mem_name = string_printf("__tex_image_%s_%03d", name_from_type(type).c_str(), flat_slot); img->mem_name = string_printf("__tex_image_%s_%03d",
name_from_type(type), flat_slot);
/* Free previous texture in slot. */ /* Free previous texture in slot. */
if(img->mem) { if(img->mem) {

View File

@@ -152,7 +152,7 @@ private:
int max_flattened_slot(ImageDataType type); int max_flattened_slot(ImageDataType type);
int type_index_to_flattened_slot(int slot, ImageDataType type); int type_index_to_flattened_slot(int slot, ImageDataType type);
int flattened_slot_to_type_index(int flat_slot, ImageDataType *type); int flattened_slot_to_type_index(int flat_slot, ImageDataType *type);
string name_from_type(int type); const char* name_from_type(ImageDataType type);
void device_load_image(Device *device, void device_load_image(Device *device,
Scene *scene, Scene *scene,