Code cleanup: refactor Cycles image metadata retrieval to use a struct.

This commit is contained in:
2018-02-27 22:16:45 +01:00
parent 450be3b226
commit 2eb94be750
6 changed files with 165 additions and 167 deletions

View File

@@ -337,8 +337,9 @@ static void create_mesh_volume_attribute(BL::Object& b_ob,
Attribute *attr = mesh->attributes.add(std); Attribute *attr = mesh->attributes.add(std);
VoxelAttribute *volume_data = attr->data_voxel(); VoxelAttribute *volume_data = attr->data_voxel();
bool is_float, is_linear; ImageMetaData metadata;
bool animated = false; bool animated = false;
bool use_alpha = true;
volume_data->manager = image_manager; volume_data->manager = image_manager;
volume_data->slot = image_manager->add_image( volume_data->slot = image_manager->add_image(
@@ -346,11 +347,10 @@ static void create_mesh_volume_attribute(BL::Object& b_ob,
b_ob.ptr.data, b_ob.ptr.data,
animated, animated,
frame, frame,
is_float,
is_linear,
INTERPOLATION_LINEAR, INTERPOLATION_LINEAR,
EXTENSION_CLIP, EXTENSION_CLIP,
true); use_alpha,
metadata);
} }
static void create_mesh_volume_attributes(Scene *scene, static void create_mesh_volume_attributes(Scene *scene,

View File

@@ -135,7 +135,7 @@ void BlenderSession::create_session()
scene = new Scene(scene_params, session->device); scene = new Scene(scene_params, session->device);
/* setup callbacks for builtin image support */ /* setup callbacks for builtin image support */
scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6, _7, _8); scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3);
scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4, _5); scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4, _5);
scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4, _5); scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4, _5);
@@ -1018,20 +1018,11 @@ int BlenderSession::builtin_image_frame(const string &builtin_name)
void BlenderSession::builtin_image_info(const string &builtin_name, void BlenderSession::builtin_image_info(const string &builtin_name,
void *builtin_data, void *builtin_data,
bool &is_float, ImageMetaData& metadata)
int &width,
int &height,
int &depth,
int &channels,
bool& free_cache)
{ {
/* empty image */ /* empty image */
is_float = false; metadata.width = 1;
width = 1; metadata.height = 1;
height = 1;
depth = 0;
channels = 0;
free_cache = false;
if(!builtin_data) if(!builtin_data)
return; return;
@@ -1045,21 +1036,21 @@ void BlenderSession::builtin_image_info(const string &builtin_name,
/* image data */ /* image data */
BL::Image b_image(b_id); BL::Image b_image(b_id);
free_cache = !b_image.has_data(); metadata.builtin_free_cache = !b_image.has_data();
is_float = b_image.is_float(); metadata.is_float = b_image.is_float();
width = b_image.size()[0]; metadata.width = b_image.size()[0];
height = b_image.size()[1]; metadata.height = b_image.size()[1];
depth = 1; metadata.depth = 1;
channels = b_image.channels(); metadata.channels = b_image.channels();
} }
else if(b_id.is_a(&RNA_Object)) { else if(b_id.is_a(&RNA_Object)) {
/* smoke volume data */ /* smoke volume data */
BL::Object b_ob(b_id); BL::Object b_ob(b_id);
BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob); BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
is_float = true; metadata.is_float = true;
depth = 1; metadata.depth = 1;
channels = 1; metadata.channels = 1;
if(!b_domain) if(!b_domain)
return; return;
@@ -1068,11 +1059,11 @@ void BlenderSession::builtin_image_info(const string &builtin_name,
builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME) || builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME) ||
builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT) || builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT) ||
builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_TEMPERATURE)) builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_TEMPERATURE))
channels = 1; metadata.channels = 1;
else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR)) else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR))
channels = 4; metadata.channels = 4;
else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY)) else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY))
channels = 3; metadata.channels = 3;
else else
return; return;
@@ -1086,9 +1077,9 @@ void BlenderSession::builtin_image_info(const string &builtin_name,
amplify = 1; amplify = 1;
} }
width = resolution.x * amplify; metadata.width = resolution.x * amplify;
height = resolution.y * amplify; metadata.height = resolution.y * amplify;
depth = resolution.z * amplify; metadata.depth = resolution.z * amplify;
} }
else { else {
/* TODO(sergey): Check we're indeed in shader node tree. */ /* TODO(sergey): Check we're indeed in shader node tree. */
@@ -1097,9 +1088,11 @@ void BlenderSession::builtin_image_info(const string &builtin_name,
BL::Node b_node(ptr); BL::Node b_node(ptr);
if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) { if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
BL::ShaderNodeTexPointDensity b_point_density_node(b_node); BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
channels = 4; metadata.channels = 4;
width = height = depth = b_point_density_node.resolution(); metadata.width = b_point_density_node.resolution();
is_float = true; metadata.height = metadata.width;
metadata.depth = metadata.width;
metadata.is_float = true;
} }
} }
} }

View File

@@ -26,6 +26,7 @@
CCL_NAMESPACE_BEGIN CCL_NAMESPACE_BEGIN
class ImageMetaData;
class Scene; class Scene;
class Session; class Session;
class RenderBuffers; class RenderBuffers;
@@ -152,12 +153,7 @@ protected:
int builtin_image_frame(const string &builtin_name); int builtin_image_frame(const string &builtin_name);
void builtin_image_info(const string &builtin_name, void builtin_image_info(const string &builtin_name,
void *builtin_data, void *builtin_data,
bool &is_float, ImageMetaData& metadata);
int &width,
int &height,
int &depth,
int &channels,
bool &free_cache);
bool builtin_image_pixels(const string &builtin_name, bool builtin_image_pixels(const string &builtin_name,
void *builtin_data, void *builtin_data,
unsigned char *pixels, unsigned char *pixels,

View File

@@ -84,73 +84,81 @@ bool ImageManager::set_animation_frame_update(int frame)
return false; return false;
} }
ImageDataType ImageManager::get_image_metadata(const string& filename, bool ImageManager::get_image_metadata(const string& filename,
void *builtin_data, void *builtin_data,
bool& is_linear, ImageMetaData& metadata)
bool& builtin_free_cache)
{ {
bool is_float = false, is_half = false; memset(&metadata, 0, sizeof(metadata));
is_linear = false;
builtin_free_cache = false;
int channels = 4;
if(builtin_data) { if(builtin_data) {
if(builtin_image_info_cb) { if(builtin_image_info_cb) {
int width, height, depth; builtin_image_info_cb(filename, builtin_data, metadata);
builtin_image_info_cb(filename, builtin_data, is_float, width, height, depth, channels, builtin_free_cache);
}
if(is_float) {
is_linear = true;
return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
} }
else { else {
return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE; return false;
} }
if(metadata.is_float) {
metadata.is_linear = true;
metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
}
else {
metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
}
return true;
} }
/* Perform preliminary checks, with meaningful logging. */ /* Perform preliminary checks, with meaningful logging. */
if(!path_exists(filename)) { if(!path_exists(filename)) {
VLOG(1) << "File '" << filename << "' does not exist."; VLOG(1) << "File '" << filename << "' does not exist.";
return IMAGE_DATA_TYPE_BYTE4; return false;
} }
if(path_is_directory(filename)) { if(path_is_directory(filename)) {
VLOG(1) << "File '" << filename << "' is a directory, can't use as image."; VLOG(1) << "File '" << filename << "' is a directory, can't use as image.";
return IMAGE_DATA_TYPE_BYTE4; return false;
} }
ImageInput *in = ImageInput::create(filename); ImageInput *in = ImageInput::create(filename);
if(in) { if(!in) {
ImageSpec spec; return false;
}
ImageSpec spec;
if(!in->open(filename, spec)) {
delete in;
return false;
}
metadata.width = spec.width;
metadata.height = spec.height;
metadata.depth = spec.depth;
if(in->open(filename, spec)) {
/* check the main format, and channel formats; /* check the main format, and channel formats;
* if any take up more than one byte, we'll need a float texture slot */ * if any take up more than one byte, we'll need a float texture slot */
if(spec.format.basesize() > 1) { if(spec.format.basesize() > 1) {
is_float = true; metadata.is_float = true;
is_linear = true; metadata.is_linear = true;
} }
for(size_t channel = 0; channel < spec.channelformats.size(); channel++) { for(size_t channel = 0; channel < spec.channelformats.size(); channel++) {
if(spec.channelformats[channel].basesize() > 1) { if(spec.channelformats[channel].basesize() > 1) {
is_float = true; metadata.is_float = true;
is_linear = true; metadata.is_linear = true;
} }
} }
/* check if it's half float */ /* check if it's half float */
if(spec.format == TypeDesc::HALF) if(spec.format == TypeDesc::HALF)
is_half = true; metadata.is_half = true;
channels = spec.nchannels;
/* basic color space detection, not great but better than nothing /* basic color space detection, not great but better than nothing
* before we do OpenColorIO integration */ * before we do OpenColorIO integration */
if(is_float) { if(metadata.is_float) {
string colorspace = spec.get_string_attribute("oiio:ColorSpace"); string colorspace = spec.get_string_attribute("oiio:ColorSpace");
is_linear = !(colorspace == "sRGB" || metadata.is_linear = !(colorspace == "sRGB" ||
colorspace == "GammaCorrected" || colorspace == "GammaCorrected" ||
(colorspace == "" && (colorspace == "" &&
(strcmp(in->format_name(), "png") == 0 || (strcmp(in->format_name(), "png") == 0 ||
@@ -159,24 +167,26 @@ ImageDataType ImageManager::get_image_metadata(const string& filename,
strcmp(in->format_name(), "jpeg2000") == 0))); strcmp(in->format_name(), "jpeg2000") == 0)));
} }
else { else {
is_linear = false; metadata.is_linear = false;
}
/* set type and channels */
metadata.channels = spec.nchannels;
if(metadata.is_half) {
metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
}
else if(metadata.is_float) {
metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
}
else {
metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
} }
in->close(); in->close();
}
delete in; delete in;
}
if(is_half) { return true;
return (channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
}
else if(is_float) {
return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
}
else {
return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
}
} }
int ImageManager::max_flattened_slot(ImageDataType type) int ImageManager::max_flattened_slot(ImageDataType type)
@@ -237,23 +247,19 @@ int ImageManager::add_image(const string& filename,
void *builtin_data, void *builtin_data,
bool animated, bool animated,
float frame, float frame,
bool& is_float,
bool& is_linear,
InterpolationType interpolation, InterpolationType interpolation,
ExtensionType extension, ExtensionType extension,
bool use_alpha) bool use_alpha,
ImageMetaData& metadata)
{ {
Image *img; Image *img;
size_t slot; size_t slot;
bool builtin_free_cache;
ImageDataType type = get_image_metadata(filename, builtin_data, is_linear, builtin_free_cache); get_image_metadata(filename, builtin_data, metadata);
ImageDataType type = metadata.type;
thread_scoped_lock device_lock(device_mutex); thread_scoped_lock device_lock(device_mutex);
/* Check whether it's a float texture. */
is_float = (type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4);
/* No half textures on OpenCL, use full float instead. */ /* No half textures on OpenCL, use full float instead. */
if(!has_half_images) { if(!has_half_images) {
if(type == IMAGE_DATA_TYPE_HALF4) { if(type == IMAGE_DATA_TYPE_HALF4) {
@@ -313,7 +319,7 @@ int ImageManager::add_image(const string& filename,
img = new Image(); img = new Image();
img->filename = filename; img->filename = filename;
img->builtin_data = builtin_data; img->builtin_data = builtin_data;
img->builtin_free_cache = builtin_free_cache; img->builtin_free_cache = metadata.builtin_free_cache;
img->need_load = true; img->need_load = true;
img->animated = animated; img->animated = animated;
img->frame = frame; img->frame = frame;
@@ -444,8 +450,13 @@ bool ImageManager::file_load_image_generic(Image *img,
if(!builtin_image_info_cb || !builtin_image_pixels_cb) if(!builtin_image_info_cb || !builtin_image_pixels_cb)
return false; return false;
bool is_float, free_cache; ImageMetaData metadata;
builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, depth, components, free_cache); builtin_image_info_cb(img->filename, img->builtin_data, metadata);
width = metadata.width;
height = metadata.height;
depth = metadata.depth;
components = metadata.channels;
} }
/* we only handle certain number of components */ /* we only handle certain number of components */

View File

@@ -31,6 +31,19 @@ class Device;
class Progress; class Progress;
class Scene; class Scene;
class ImageMetaData {
public:
/* Must be set by image file or builtin callback. */
bool is_float, is_half;
int channels;
size_t width, height, depth;
bool builtin_free_cache;
/* Automatically set. */
ImageDataType type;
bool is_linear;
};
class ImageManager { class ImageManager {
public: public:
explicit ImageManager(const DeviceInfo& info); explicit ImageManager(const DeviceInfo& info);
@@ -40,11 +53,10 @@ public:
void *builtin_data, void *builtin_data,
bool animated, bool animated,
float frame, float frame,
bool& is_float,
bool& is_linear,
InterpolationType interpolation, InterpolationType interpolation,
ExtensionType extension, ExtensionType extension,
bool use_alpha); bool use_alpha,
ImageMetaData& metadata);
void remove_image(int flat_slot); void remove_image(int flat_slot);
void remove_image(const string& filename, void remove_image(const string& filename,
void *builtin_data, void *builtin_data,
@@ -56,10 +68,9 @@ public:
InterpolationType interpolation, InterpolationType interpolation,
ExtensionType extension, ExtensionType extension,
bool use_alpha); bool use_alpha);
ImageDataType get_image_metadata(const string& filename, bool get_image_metadata(const string& filename,
void *builtin_data, void *builtin_data,
bool& is_linear, ImageMetaData& metadata);
bool& builtin_free_cache);
void device_update(Device *device, void device_update(Device *device,
Scene *scene, Scene *scene,
@@ -82,12 +93,7 @@ public:
*/ */
function<void(const string &filename, function<void(const string &filename,
void *data, void *data,
bool &is_float, ImageMetaData& metadata)> builtin_image_info_cb;
int &width,
int &height,
int &depth,
int &channels,
bool &free_cache)> builtin_image_info_cb;
function<bool(const string &filename, function<bool(const string &filename,
void *data, void *data,
unsigned char *pixels, unsigned char *pixels,

View File

@@ -302,17 +302,17 @@ void ImageTextureNode::compile(SVMCompiler& compiler)
image_manager = compiler.image_manager; image_manager = compiler.image_manager;
if(is_float == -1) { if(is_float == -1) {
bool is_float_bool; ImageMetaData metadata;
slot = image_manager->add_image(filename.string(), slot = image_manager->add_image(filename.string(),
builtin_data, builtin_data,
animated, animated,
0, 0,
is_float_bool,
is_linear,
interpolation, interpolation,
extension, extension,
use_alpha); use_alpha,
is_float = (int)is_float_bool; metadata);
is_float = metadata.is_float;
is_linear = metadata.is_linear;
} }
if(slot != -1) { if(slot != -1) {
@@ -363,26 +363,22 @@ void ImageTextureNode::compile(OSLCompiler& compiler)
image_manager = compiler.image_manager; image_manager = compiler.image_manager;
if(is_float == -1) { if(is_float == -1) {
ImageMetaData metadata;
if(builtin_data == NULL) { if(builtin_data == NULL) {
ImageDataType type; image_manager->get_image_metadata(filename.string(), NULL, metadata);
bool builtin_free_cache;
type = image_manager->get_image_metadata(filename.string(), NULL, is_linear, builtin_free_cache);
if(type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4)
is_float = 1;
} }
else { else {
bool is_float_bool;
slot = image_manager->add_image(filename.string(), slot = image_manager->add_image(filename.string(),
builtin_data, builtin_data,
animated, animated,
0, 0,
is_float_bool,
is_linear,
interpolation, interpolation,
extension, extension,
use_alpha); use_alpha,
is_float = (int)is_float_bool; metadata);
} }
is_float = metadata.is_float;
is_linear = metadata.is_linear;
} }
if(slot == -1) { if(slot == -1) {
@@ -501,17 +497,17 @@ void EnvironmentTextureNode::compile(SVMCompiler& compiler)
image_manager = compiler.image_manager; image_manager = compiler.image_manager;
if(slot == -1) { if(slot == -1) {
bool is_float_bool; ImageMetaData metadata;
slot = image_manager->add_image(filename.string(), slot = image_manager->add_image(filename.string(),
builtin_data, builtin_data,
animated, animated,
0, 0,
is_float_bool,
is_linear,
interpolation, interpolation,
EXTENSION_REPEAT, EXTENSION_REPEAT,
use_alpha); use_alpha,
is_float = (int)is_float_bool; metadata);
is_float = metadata.is_float;
is_linear = metadata.is_linear;
} }
if(slot != -1) { if(slot != -1) {
@@ -553,26 +549,22 @@ void EnvironmentTextureNode::compile(OSLCompiler& compiler)
*/ */
image_manager = compiler.image_manager; image_manager = compiler.image_manager;
if(is_float == -1) { if(is_float == -1) {
ImageMetaData metadata;
if(builtin_data == NULL) { if(builtin_data == NULL) {
ImageDataType type; image_manager->get_image_metadata(filename.string(), NULL, metadata);
bool builtin_free_cache;
type = image_manager->get_image_metadata(filename.string(), NULL, is_linear, builtin_free_cache);
if(type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4)
is_float = 1;
} }
else { else {
bool is_float_bool;
slot = image_manager->add_image(filename.string(), slot = image_manager->add_image(filename.string(),
builtin_data, builtin_data,
animated, animated,
0, 0,
is_float_bool,
is_linear,
interpolation, interpolation,
EXTENSION_REPEAT, EXTENSION_REPEAT,
use_alpha); use_alpha,
is_float = (int)is_float_bool; metadata);
} }
is_float = metadata.is_float;
is_linear = metadata.is_linear;
} }
if(slot == -1) { if(slot == -1) {
@@ -1421,13 +1413,13 @@ void PointDensityTextureNode::compile(SVMCompiler& compiler)
if(use_density || use_color) { if(use_density || use_color) {
if(slot == -1) { if(slot == -1) {
bool is_float, is_linear; ImageMetaData metadata;
slot = image_manager->add_image(filename.string(), builtin_data, slot = image_manager->add_image(filename.string(), builtin_data,
false, 0, false, 0,
is_float, is_linear,
interpolation, interpolation,
EXTENSION_CLIP, EXTENSION_CLIP,
true); true,
metadata);
} }
if(slot != -1) { if(slot != -1) {
@@ -1473,13 +1465,13 @@ void PointDensityTextureNode::compile(OSLCompiler& compiler)
if(use_density || use_color) { if(use_density || use_color) {
if(slot == -1) { if(slot == -1) {
bool is_float, is_linear; ImageMetaData metadata;
slot = image_manager->add_image(filename.string(), builtin_data, slot = image_manager->add_image(filename.string(), builtin_data,
false, 0, false, 0,
is_float, is_linear,
interpolation, interpolation,
EXTENSION_CLIP, EXTENSION_CLIP,
true); true,
metadata);
} }
if(slot != -1) { if(slot != -1) {