Blender V5.3
libocio_display.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "libocio_display.hh"
6#include "OCIO_display.hh"
7
8#include "BLI_index_range.hh"
9
10#include "OCIO_config.hh"
11
12#include "CLG_log.h"
13
14#include "../opencolorio.hh"
15
16#include "error_handling.hh"
17#include "libocio_config.hh"
20
21namespace blender {
22
23static CLG_LogRef LOG = {"color_management"};
24
25namespace ocio {
26
27static OCIO_NAMESPACE::ConstColorSpaceRcPtr get_display_view_colorspace(
28 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config, const char *display, const char *view)
29{
30 const char *display_colorspace = ocio_config->getDisplayViewColorSpaceName(display, view);
31 if (display_colorspace == nullptr) {
32 return nullptr;
33 }
34
35 /* Shared view transforms can use this special display name to indicate
36 * the display colorspace name is the same as the display name. */
37 if (STREQ(display_colorspace, "<USE_DISPLAY_NAME>")) {
38 return ocio_config->getColorSpace(display);
39 }
40
41 return ocio_config->getColorSpace(display_colorspace);
42}
43
44LibOCIODisplay::LibOCIODisplay(const int index, const LibOCIOConfig &config) : config_(&config)
45{
46 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config = config.get_ocio_config();
47
48 this->index = index;
49
50 name_ = ocio_config->getDisplay(index);
51
52 CLOG_TRACE(&LOG, "Add display: %s", name_.c_str());
53
54 /* Initialize views. */
55 const int num_views = ocio_config->getNumViews(name_.c_str());
56 if (num_views < 0) {
57 report_error("Invalid OpenColorIO configuration: negative number of views");
58 return;
59 }
60
61 /* Try to assign a display colorspace to every view even if missing. In particular for
62 * Raw we still want to set the colorspace. */
63 OCIO_NAMESPACE::ConstColorSpaceRcPtr ocio_fallback_display_colorspace;
64 for (const int view_index : IndexRange(num_views)) {
65 const char *view_name = ocio_config->getView(name_.c_str(), view_index);
66 ocio_fallback_display_colorspace = get_display_view_colorspace(
67 ocio_config, name_.c_str(), view_name);
68 if (ocio_fallback_display_colorspace &&
69 ocio_fallback_display_colorspace->getReferenceSpaceType() ==
70 OCIO_NAMESPACE::REFERENCE_SPACE_DISPLAY)
71 {
72 break;
73 }
74 ocio_fallback_display_colorspace.reset();
75 }
76
77 bool support_emulation = config.get_color_space(OCIO_NAMESPACE::ROLE_INTERCHANGE_DISPLAY) !=
78 nullptr;
79
80 views_.reserve(num_views);
81 for (const int view_index : IndexRange(num_views)) {
82 const char *view_name = ocio_config->getView(name_.c_str(), view_index);
83
84 OCIO_NAMESPACE::ConstColorSpaceRcPtr ocio_display_colorspace = get_display_view_colorspace(
85 ocio_config, name_.c_str(), view_name);
86 if (!ocio_display_colorspace) {
87 ocio_display_colorspace = ocio_fallback_display_colorspace;
88 }
89
90 /* There does not exist a description for displays, if there is an associated display
91 * colorspace it's likely to be a useful description. */
92 if (description_.is_empty() && ocio_display_colorspace &&
93 ocio_display_colorspace->getReferenceSpaceType() ==
94 OCIO_NAMESPACE::REFERENCE_SPACE_DISPLAY)
95 {
96 description_ = ocio_display_colorspace->getDescription();
97 }
98
99 const char *view_description = nullptr;
100 const char *view_transform_name = ocio_config->getDisplayViewTransformName(name_.c_str(),
101 view_name);
102 if (view_transform_name) {
103 const OCIO_NAMESPACE::ConstViewTransformRcPtr view_transform = ocio_config->getViewTransform(
104 view_transform_name);
105 if (view_transform) {
106 view_description = view_transform->getDescription();
107 }
108 }
109 if (view_description == nullptr) {
110 view_description = "";
111 }
112
113 /* Detect if view is HDR, through encoding of display colorspace. */
114 bool view_is_hdr = false;
115 bool view_is_data = false;
116 if (ocio_display_colorspace) {
117 StringRefNull encoding = ocio_display_colorspace->getEncoding();
118 view_is_hdr = encoding == "hdr-video" || encoding == "edr-video";
119 is_hdr_ |= view_is_hdr;
120 view_is_data = ocio_display_colorspace->isData();
121 }
122
123 /* Detect if display emulation is supported. */
124 bool view_support_emulation = support_emulation && ocio_display_colorspace &&
125 ocio_display_colorspace->getReferenceSpaceType() ==
126 OCIO_NAMESPACE::REFERENCE_SPACE_DISPLAY;
127
128 /* Detect gamut and transfer function through interop ID. When unknown, things
129 * should still work correctly but may miss optimizations. */
130 Gamut gamut = Gamut::Unknown;
132
133 const LibOCIOColorSpace *display_colorspace =
134 (ocio_display_colorspace) ? static_cast<const LibOCIOColorSpace *>(config.get_color_space(
135 ocio_display_colorspace->getName())) :
136 nullptr;
137 const StringRef display_interop_id = interop_id_drop_namespace(
138 (display_colorspace) ? display_colorspace->interop_id() : "");
139
140 if (!display_interop_id.is_empty()) {
141 if (display_interop_id.endswith("_rec709_display") ||
142 display_interop_id.endswith("_rec709_scene"))
143 {
144 gamut = Gamut::Rec709;
145 }
146 else if (display_interop_id.endswith("_p3d65_display") ||
147 display_interop_id.endswith("_p3d65_scene"))
148 {
149 gamut = Gamut::P3D65;
150 }
151 else if (display_interop_id.endswith("_rec2020_display") ||
152 display_interop_id.endswith("_rec2020_scene"))
153 {
154 gamut = Gamut::Rec2020;
155 }
156
157 if (display_interop_id.startswith("srgb_")) {
158 transfer_function = TransferFunction::sRGB;
159 }
160 else if (display_interop_id.startswith("srgbe_")) {
161 transfer_function = TransferFunction::ExtendedsRGB;
162 }
163 else if (display_interop_id.startswith("pq_")) {
164 transfer_function = TransferFunction::PQ;
165 }
166 else if (display_interop_id.startswith("hlg_")) {
167 transfer_function = TransferFunction::HLG;
168 }
169 else if (display_interop_id.startswith("g18_")) {
170 transfer_function = TransferFunction::Gamma18;
171 }
172 else if (display_interop_id.startswith("g22_")) {
173 transfer_function = TransferFunction::Gamma22;
174 }
175 else if (display_interop_id.startswith("g24_")) {
176 transfer_function = TransferFunction::Gamma24;
177 }
178 else if (display_interop_id.startswith("g26_")) {
179 transfer_function = TransferFunction::Gamma26;
180 }
181 }
182
184 " Add view: %s (colorspace: %s, %s)",
185 view_name,
186 display_colorspace ? display_colorspace->name().c_str() : "<none>",
187 view_is_hdr ? "HDR" : "SDR");
188
189 views_.append_as(view_index,
190 view_name,
191 view_description,
192 view_is_hdr,
193 view_is_data,
194 view_support_emulation,
195 gamut,
196 transfer_function,
197 display_colorspace);
198 }
199
200 /* Detect untonemapped view transform. */
201 if (untonemapped_view_ == nullptr) {
202 /* Use Blender config and ACES config naming conventions. */
203 for (const LibOCIOView &view : views_) {
204 if (view.name() == "Un-tone-mapped" || view.name() == "Standard") {
205 untonemapped_view_ = &view;
206 break;
207 }
208 }
209 if (untonemapped_view_ == nullptr) {
210 /* Use config wide default view transform between reference and display spaces.
211 * Note this is not always the same as the default view transform of the display. */
212 const char *default_view_transform = ocio_config->getDefaultViewTransformName();
213 for (const LibOCIOView &view : views_) {
214 if (view.name() == default_view_transform) {
215 untonemapped_view_ = &view;
216 break;
217 }
218 }
219 }
220 }
221
222 /* Hide redundant suffix from ACES config. */
223 if (name_.endswith(" - Display")) {
224 ui_name_ = StringRef(name_).drop_known_suffix(" - Display");
225 }
226}
227
229{
230 return untonemapped_view_;
231}
232
234{
235 /* TODO(sergey): Is there faster way to lookup Blender-side view? */
236 for (const LibOCIOView &view : views_) {
237 if (view.name() == name) {
238 return &view;
239 }
240 }
241 return nullptr;
242}
243
245{
246 return views_.size();
247}
248
250{
251 if (index < 0 || index >= views_.size()) {
252 return nullptr;
253 }
254 return &views_[index];
255}
256
257std::unique_ptr<LibOCIOCPUProcessor> LibOCIODisplay::create_scene_linear_cpu_processor(
258 const bool use_display_emulation, const bool inverse) const
259{
260 const View *view = get_untonemapped_view();
261 if (view == nullptr) {
262 view = get_default_view();
263 }
264
265 DisplayParameters display_parameters;
266 display_parameters.from_colorspace = OCIO_NAMESPACE::ROLE_SCENE_LINEAR;
267 display_parameters.view = view->name();
268 display_parameters.display = name_;
269 display_parameters.inverse = inverse;
270 display_parameters.use_display_emulation = use_display_emulation;
271 OCIO_NAMESPACE::ConstProcessorRcPtr ocio_processor = create_ocio_display_processor(
272 *config_, display_parameters);
273 if (!ocio_processor) {
274 return nullptr;
275 }
276
277 OCIO_NAMESPACE::ConstCPUProcessorRcPtr ocio_cpu_processor =
278 ocio_processor->getDefaultCPUProcessor();
279 if (!ocio_cpu_processor) {
280 return nullptr;
281 }
282
283 return std::make_unique<LibOCIOCPUProcessor>(ocio_cpu_processor);
284}
285
287 const bool use_display_emulation) const
288{
289 const CPUProcessorCache &cache = (use_display_emulation) ?
290 to_scene_linear_emulation_cpu_processor_ :
291 to_scene_linear_cpu_processor_;
292 return cache.get([&] { return create_scene_linear_cpu_processor(use_display_emulation, true); });
293}
294
296 const bool use_display_emulation) const
297{
298 const CPUProcessorCache &cache = (use_display_emulation) ?
299 from_scene_linear_emulation_cpu_processor_ :
300 from_scene_linear_cpu_processor_;
301 return cache.get(
302 [&] { return create_scene_linear_cpu_processor(use_display_emulation, false); });
303}
304
306{
307 to_scene_linear_cpu_processor_ = CPUProcessorCache();
308 to_scene_linear_emulation_cpu_processor_ = CPUProcessorCache();
309 from_scene_linear_cpu_processor_ = CPUProcessorCache();
310 from_scene_linear_emulation_cpu_processor_ = CPUProcessorCache();
311}
312
313} // namespace ocio
314} // namespace blender
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define STREQ(a, b)
constexpr IndexRange()=default
constexpr const char * c_str() const
constexpr StringRef()
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
constexpr bool endswith(StringRef suffix) const
const CPUProcessor * get(const std::function< std::unique_ptr< CPUProcessor >()> &create_processor) const
StringRefNull name() const override
StringRefNull interop_id() const override
const ColorSpace * get_color_space(StringRefNull name) const override
const OCIO_NAMESPACE::ConstConfigRcPtr & get_ocio_config() const
StringRefNull name() const override
LibOCIODisplay(int index, const LibOCIOConfig &config)
const View * get_untonemapped_view() const override
const View * get_view_by_index(int index) const override
const View * get_view_by_name(StringRefNull name) const override
std::unique_ptr< LibOCIOCPUProcessor > create_scene_linear_cpu_processor(const bool use_display_emulation, const bool inverse) const
const CPUProcessor * get_from_scene_linear_cpu_processor(bool use_display_emulation) const override
const CPUProcessor * get_to_scene_linear_cpu_processor(bool use_display_emulation) const override
int get_num_views() const override
const View * get_default_view() const override
virtual StringRefNull name() const =0
MatBase< C, R > inverse(MatBase< C, R >) RET
OCIO_NAMESPACE::ConstProcessorRcPtr create_ocio_display_processor(const LibOCIOConfig &config, const DisplayParameters &display_parameters)
StringRef interop_id_drop_namespace(const StringRef interop_id)
void report_error(const StringRefNull error)
static OCIO_NAMESPACE::ConstColorSpaceRcPtr get_display_view_colorspace(const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config, const char *display, const char *view)
BMesh Iterators.
static CLG_LogRef LOG
Definition logging.cpp:12