From 7ad802cf3ae500bc72863b6dba0f28a488fce3d1 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 23 Apr 2019 13:56:30 +0200 Subject: Cycles/Eevee: unified and improved texture image color space handling Cycles now uses the color space on the image datablock, and uses OpenColorIO to convert to scene linear as needed. Byte images do not take extra memory, they are compressed in scene linear + sRGB transfer function which in common cases is a no-op. Eevee and workbench were changed to work similar. Float images are stored as scene linear. Byte images are compressed as scene linear + sRGB and stored in a GL_SRGB8_ALPHA8 texture. From the GLSL shader side this means they are read as scene linear, simplifying the code and taking advantage of hardware support. Further, OpenGL image textures are now all stored with premultiplied alpha. Eevee texture sampling looks a little different now because interpolation happens premultiplied and in scene linear space. Overlays and grease pencil work in sRGB space so those now have an extra conversion to sRGB after reading from image textures. This is not particularly elegant but as long as engines use different conventions, one or the other needs to do conversion. This change breaks compatibility for cases where multiple image texture nodes were using the same image with different color space node settings. However it gives more predictable behavior for baking and texture painting if save, load and image editing operations have a single color space to handle. Differential Revision: https://developer.blender.org/D4807 --- intern/cycles/blender/blender_shader.cpp | 28 ++++++++++------------------ intern/cycles/render/colorspace.cpp | 32 ++++++++++++++++++++------------ intern/opencolorio/ocio_capi.h | 1 + intern/opencolorio/ocio_impl.cc | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) (limited to 'intern') diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index e6ec8b22d7a..a08f767f964 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -651,6 +651,9 @@ static ShaderNode *add_node(Scene *scene, image->builtin_data = NULL; } + PointerRNA colorspace_ptr = b_image.colorspace_settings().ptr; + image->colorspace = get_enum_identifier(colorspace_ptr, "name"); + image->animated = b_image_node.image_user().use_auto_refresh(); image->use_alpha = b_image.use_alpha(); @@ -662,18 +665,11 @@ static ShaderNode *add_node(Scene *scene, image->builtin_data, get_image_interpolation(b_image_node), get_image_extension(b_image_node), - image->use_alpha); + image->use_alpha, + image->colorspace); } #endif } - switch (b_image_node.color_space()) { - case BL::ShaderNodeTexImage::color_space_NONE: - image->colorspace = u_colorspace_raw; - break; - case BL::ShaderNodeTexImage::color_space_COLOR: - image->colorspace = u_colorspace_auto; - break; - } image->projection = (NodeImageProjection)b_image_node.projection(); image->interpolation = get_image_interpolation(b_image_node); image->extension = get_image_extension(b_image_node); @@ -703,6 +699,9 @@ static ShaderNode *add_node(Scene *scene, env->builtin_data = NULL; } + PointerRNA colorspace_ptr = b_image.colorspace_settings().ptr; + env->colorspace = get_enum_identifier(colorspace_ptr, "name"); + env->animated = b_env_node.image_user().use_auto_refresh(); env->use_alpha = b_image.use_alpha(); @@ -714,18 +713,11 @@ static ShaderNode *add_node(Scene *scene, env->builtin_data, get_image_interpolation(b_env_node), EXTENSION_REPEAT, - env->use_alpha); + env->use_alpha, + env->colorspace); } #endif } - switch (b_env_node.color_space()) { - case BL::ShaderNodeTexEnvironment::color_space_NONE: - env->colorspace = u_colorspace_raw; - break; - case BL::ShaderNodeTexEnvironment::color_space_COLOR: - env->colorspace = u_colorspace_auto; - break; - } env->interpolation = get_image_interpolation(b_env_node); env->projection = (NodeEnvironmentProjection)b_env_node.projection(); BL::TexMapping b_texture_mapping(b_env_node.texture_mapping()); diff --git a/intern/cycles/render/colorspace.cpp b/intern/cycles/render/colorspace.cpp index cdce1f70188..7b57478ff51 100644 --- a/intern/cycles/render/colorspace.cpp +++ b/intern/cycles/render/colorspace.cpp @@ -242,6 +242,10 @@ inline void processor_apply_pixels(const OCIO::Processor *processor, size_t width, size_t height) { + /* TODO: implement faster version for when we know the conversion + * is a simple matrix transform between linear spaces. In that case + * unpremultiply is not needed. */ + /* Process large images in chunks to keep temporary memory requirement down. */ size_t y_chunk_size = max(1, 16 * 1024 * 1024 / (sizeof(float4) * width)); vector float_pixels(y_chunk_size * width); @@ -252,7 +256,16 @@ inline void processor_apply_pixels(const OCIO::Processor *processor, for (size_t y = y0; y < y1; y++) { for (size_t x = 0; x < width; x++, i++) { - float_pixels[i] = cast_to_float4(pixels + 4 * (y * width + x)); + float4 value = cast_to_float4(pixels + 4 * (y * width + x)); + + if (!(value.w == 0.0f || value.w == 1.0f)) { + float inv_alpha = 1.0f / value.w; + value.x *= inv_alpha; + value.y *= inv_alpha; + value.z *= inv_alpha; + } + + float_pixels[i] = value; } } @@ -263,25 +276,20 @@ inline void processor_apply_pixels(const OCIO::Processor *processor, for (size_t y = y0; y < y1; y++) { for (size_t x = 0; x < width; x++, i++) { float4 value = float_pixels[i]; + + value.x *= value.w; + value.y *= value.w; + value.z *= value.w; + if (compress_as_srgb) { value = color_linear_to_srgb_v4(value); } + cast_from_float4(pixels + 4 * (y * width + x), value); } } } } - -/* Fast version for float images, which OpenColorIO can handle natively. */ -template<> -inline void processor_apply_pixels(const OCIO::Processor *processor, - float *pixels, - size_t width, - size_t height) -{ - OCIO::PackedImageDesc desc(pixels, width, height, 4); - processor->apply(desc); -} #endif template diff --git a/intern/opencolorio/ocio_capi.h b/intern/opencolorio/ocio_capi.h index f4d7717ba46..9ba2d8fb8f9 100644 --- a/intern/opencolorio/ocio_capi.h +++ b/intern/opencolorio/ocio_capi.h @@ -31,6 +31,7 @@ struct OCIO_GLSLDrawState; int unused; \ } * name +#define OCIO_ROLE_DATA "data" #define OCIO_ROLE_SCENE_LINEAR "scene_linear" #define OCIO_ROLE_COLOR_PICKING "color_picking" #define OCIO_ROLE_TEXTURE_PAINT "texture_paint" diff --git a/intern/opencolorio/ocio_impl.cc b/intern/opencolorio/ocio_impl.cc index 644117e0000..25c1df70045 100644 --- a/intern/opencolorio/ocio_impl.cc +++ b/intern/opencolorio/ocio_impl.cc @@ -552,7 +552,7 @@ void OCIOImpl::colorSpaceIsBuiltin(OCIO_ConstConfigRcPtr *config_, processor = (*config)->getProcessor((*cs)->getName(), "scene_linear"); } catch (Exception &exception) { - OCIO_reportException(exception); + /* Silently ignore if no conversion possible, then it's not scene linear or sRGB. */ is_scene_linear = false; is_srgb = false; return; -- cgit v1.2.3