forked from AcademySoftwareFoundation/OpenShadingLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwide_opcolor.cpp
More file actions
544 lines (466 loc) · 18.3 KB
/
wide_opcolor.cpp
File metadata and controls
544 lines (466 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
/////////////////////////////////////////////////////////////////////////
/// \file
///
/// Shader interpreter implementation of color operations.
///
/////////////////////////////////////////////////////////////////////////
#include <OSL/oslconfig.h>
#include <OSL/batched_rendererservices.h>
#include <OSL/batched_shaderglobals.h>
#include <OSL/wide.h>
#include <OpenImageIO/fmath.h>
#include "oslexec_pvt.h"
#include "opcolor_impl.h"
#include "opcolor.h"
OSL_NAMESPACE_BEGIN
namespace __OSL_WIDE_PVT {
OSL_USING_DATA_WIDTH(__OSL_WIDTH)
#include "define_opname_macros.h"
namespace {
OSL_FORCEINLINE ShadingContext*
context_from_bsg(void* bsg_)
{
auto* bsg = reinterpret_cast<BatchedShaderGlobals*>(bsg_);
return bsg->uniform.context;
}
OSL_FORCEINLINE const ColorSystem&
cs_from_bsg(void* bsg)
{
return context_from_bsg(bsg)->shadingsys().colorsystem();
}
}; // End anonymous namespace
OSL_BATCHOP void
__OSL_OP(blackbody_vf)(void* bsg_, void* out, float temp)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
*(Color3*)out = cs.blackbody_rgb(temp);
}
OSL_BATCHOP void
__OSL_MASKED_OP2(blackbody, Wv, Wf)(void* bsg_, void* wout_, void* wtemp_,
unsigned int mask_value)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
Masked<Color3> wR(wout_, Mask(mask_value)); //output
Wide<const float> wL(wtemp_); //input lambda
Block<int> computeRequiredBlock;
Wide<int> wcomputeRequired(computeRequiredBlock);
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
float temperature = wL[lane];
bool canNotLookup = !cs.can_lookup_blackbody(temperature);
wcomputeRequired[lane] = canNotLookup & wR.mask()[lane];
if (canNotLookup) {
// We choose to run computation unmasked so that
// clang will treat the ColorSystem as uniform
// which will avoid many gathers
// Ensure temperature values from disabled lanes
// are inbounds.
temperature = 0.0f;
}
Color3 rgb = cs.lookup_blackbody_rgb(temperature);
wR[lane] = rgb;
}
if (testIfAnyLaneIsNonZero(wcomputeRequired)) {
// Complex nested loop in the real computation may not vectorize in
// in all compilers, which is why we have split off the fast path of
// using the lookup table so it can be vectorized independently
OSL_OMP_COMPLEX_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
float temperature = wL[lane];
int computeRequired = wcomputeRequired[lane];
if (computeRequired != 0) {
OSL_DASSERT(
wR.mask()[lane]
&& "computeRequired should have already considered the result mask");
Color3 rgb = cs.compute_blackbody_rgb(temperature);
wR[ActiveLane(lane)] = rgb;
}
}
}
}
OSL_BATCHOP void
__OSL_OP(wavelength_color_vf)(void* bsg_, void* out, float lambda)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
Color3 rgb = cs.XYZ_to_RGB(wavelength_color_XYZ(lambda));
// constrain_rgb (rgb);
rgb *= 1.0 / 2.52; // Empirical scale from lg to make all comps <= 1
// norm_rgb (rgb);
clamp_zero(rgb);
*(Color3*)out = rgb;
}
OSL_BATCHOP void
__OSL_MASKED_OP2(wavelength_color, Wv, Wf)(void* bsg_, void* wout_,
void* wlambda_,
unsigned int mask_value)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
Masked<Color3> wR(wout_, Mask(mask_value)); //output
Wide<const float> wL(wlambda_); //input lambda
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
float lambda = wL[lane];
// Normally we want to access all data we can before applying the mask
// so those loads can be unmasked. When accessing an object like
// ColorSystem, it is tricky. By calling a method of a non stack based
// object (like ColorSystem) inside the masked region, the compiler may
// consider it illegal to dereference the pointer unmasked.
// When vectorizing with Clang, this effectively changed a uniform
// load/broadcast of Matrix33 ColorSystem::m_XYZ2RGB to a series of
// masked gathers. One solution is to create a ColorSystem copy on the
// stack before the loop, but incurs large overhead to copy object.
// Another solution is to just copy out the Matrix33 from the
// ColorSystem and not call any methods by reproducing the method's
// code here. In this case though, because the conversion code can
// handle any input lambda value, we will just perform all the work
// unmasked which avoids the issue.
//if (wR.mask()[lane]) {
Color3 rgb = cs.XYZ_to_RGB(wavelength_color_XYZ(lambda));
rgb *= 1.0 / 2.52; // Empirical scale from lg to make all comps <= 1
clamp_zero(rgb);
//}
wR[lane] = rgb;
}
}
OSL_BATCHOP void
__OSL_OP(prepend_color_from_vs)(void* bsg_, void* c_, const char* from)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
Color3& c(*(Color3*)c_);
c = cs.to_rgb(USTR(from), c, context_from_bsg(bsg_));
}
namespace {
// NOTE: keep implementation as mirror of ColorSystem::to_rgb
void
wide_prepend_color_from(ShadingContext* ctx, const ColorSystem& cs,
Masked<Color3> wR, ustring fromspace)
{
// Rather than attempt outer loop vectorization of ColorSystem::to_rgb
// we will pull it's implementation up and insert SIMD loops inside
// the uniform branches
if (fromspace == Strings::RGB || fromspace == Strings::rgb
|| fromspace == cs.colorspace()) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
wR[lane] = C;
}
return;
}
if (fromspace == Strings::hsv) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
if (wR.mask()[lane]) {
Color3 R = hsv_to_rgb(C);
wR[ActiveLane(lane)] = R;
}
}
return;
}
if (fromspace == Strings::hsl) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
if (wR.mask()[lane]) {
Color3 R = hsl_to_rgb(C);
wR[ActiveLane(lane)] = R;
}
}
return;
}
if (fromspace == Strings::YIQ) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
if (wR.mask()[lane]) {
Color3 R = YIQ_to_rgb(C);
wR[ActiveLane(lane)] = R;
}
}
return;
}
if (fromspace == Strings::XYZ) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
if (wR.mask()[lane]) {
Color3 R = cs.XYZ_to_RGB(C);
wR[ActiveLane(lane)] = R;
}
}
return;
}
if (fromspace == Strings::xyY) {
OSL_OMP_PRAGMA(omp simd simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
Color3 C = wR[lane];
if (wR.mask()[lane]) {
Color3 R = cs.XYZ_to_RGB(xyY_to_XYZ(C));
wR[ActiveLane(lane)] = R;
}
}
return;
}
// Serialize calls to ocio
wR.mask().foreach ([=, &cs](ActiveLane lane) -> void {
Color3 C = wR[lane];
Color3 R = cs.ocio_transform(fromspace, Strings::RGB, C, ctx);
wR[lane] = R;
});
}
} // namespace
OSL_BATCHOP void
__OSL_MASKED_OP2(prepend_color_from, Wv, s)(void* bsg_, void* c_,
const char* from,
unsigned int mask_value)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
ShadingContext* ctx = context_from_bsg(bsg_);
Masked<Color3> wR(c_, Mask(mask_value));
ustring fromspace = USTR(from);
wide_prepend_color_from(ctx, cs, wR, fromspace);
}
OSL_BATCHOP void
__OSL_MASKED_OP2(prepend_color_from, Wv, Ws)(void* bsg_, void* c_, void* from_,
unsigned int mask_value)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
ShadingContext* ctx = context_from_bsg(bsg_);
Wide<const ustring> wFrom(from_);
foreach_unique(wFrom, Mask(mask_value),
[=, &cs](const ustring& from, Mask from_mask) {
// Reuse the uniform from implementation by restricting results to
// just the lanes with the same value of "from".
Masked<Color3> wsub_result(c_, from_mask);
wide_prepend_color_from(ctx, cs, wsub_result, from);
});
}
namespace {
// Note: Clang 14 seems to no longer allow vectorizing these loops
#if ((OSL_CLANG_VERSION && OSL_CLANG_VERSION < 140000) \
|| OSL_INTEL_CLASSIC_COMPILER_VERSION || OSL_INTEL_LLVM_COMPILER_VERSION)
# define WIDE_TRANSFORMC_OMP_SIMD_LOOP(...) OSL_OMP_SIMD_LOOP(__VA_ARGS__)
#else
# define WIDE_TRANSFORMC_OMP_SIMD_LOOP(...)
#endif
template<typename COLOR>
OSL_NOINLINE void
wide_transformc(const ColorSystem cs, ustring fromspace, ustring tospace,
Masked<COLOR> wOutput, Wide<const COLOR> wInput,
ShadingContext* context);
// NOTE: keep implementation as mirror of ColorSystem::transformc
template<typename COLOR>
void
wide_transformc(const ColorSystem cs, ustring fromspace, ustring tospace,
Masked<COLOR> wOutput, Wide<const COLOR> wInput,
ShadingContext* context)
{
// Rather than attempt outer loop vectorization of ColorSystem::transformc
// we will pull it's implementation up and insert SIMD loops inside
// the uniform branches
bool use_colorconfig = false;
Block<COLOR> bCrgb;
Wide<COLOR> wCrgb(bCrgb);
if (fromspace == Strings::RGB || fromspace == Strings::rgb
|| fromspace == Strings::linear || fromspace == cs.colorspace()) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
wCrgb[lane] = C;
}
} else if (fromspace == Strings::hsv) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = hsv_to_rgb(C);
wCrgb[ActiveLane(lane)] = R;
}
}
} else if (fromspace == Strings::hsl) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = hsl_to_rgb(C);
wCrgb[ActiveLane(lane)] = R;
}
}
} else if (fromspace == Strings::YIQ) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = YIQ_to_rgb(C);
wCrgb[ActiveLane(lane)] = R;
}
}
} else if (fromspace == Strings::XYZ) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = cs.XYZ_to_RGB(C);
wCrgb[ActiveLane(lane)] = R;
}
}
} else if (fromspace == Strings::xyY) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = cs.XYZ_to_RGB(xyY_to_XYZ(C));
wCrgb[ActiveLane(lane)] = R;
}
}
} else if (fromspace == Strings::sRGB) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wInput[lane];
if (wOutput.mask()[lane]) {
COLOR R = sRGB_to_linear(C);
wCrgb[ActiveLane(lane)] = R;
}
}
} else {
use_colorconfig = true;
}
if (use_colorconfig) {
// do things the ColorConfig way, so skip all these other clauses...
} else if (tospace == Strings::RGB || tospace == Strings::rgb
|| tospace == Strings::linear || tospace == cs.colorspace()) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR C = wCrgb[lane];
wOutput[lane] = C;
}
} else if (tospace == Strings::hsv) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = rgb_to_hsv(Crgb);
wOutput[ActiveLane(lane)] = Cto;
}
}
} else if (tospace == Strings::hsl) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = rgb_to_hsl(Crgb);
wOutput[ActiveLane(lane)] = Cto;
}
}
} else if (tospace == Strings::YIQ) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = rgb_to_YIQ(Crgb);
wOutput[ActiveLane(lane)] = Cto;
}
}
} else if (tospace == Strings::XYZ) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = cs.RGB_to_XYZ(Crgb);
wOutput[ActiveLane(lane)] = Cto;
}
}
} else if (tospace == Strings::xyY) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = XYZ_to_xyY(cs.RGB_to_XYZ(Crgb));
wOutput[ActiveLane(lane)] = Cto;
}
}
} else if (tospace == Strings::sRGB) {
WIDE_TRANSFORMC_OMP_SIMD_LOOP(simdlen(__OSL_WIDTH))
for (int lane = 0; lane < __OSL_WIDTH; ++lane) {
COLOR Crgb = wCrgb[lane];
if (wOutput.mask()[lane]) {
COLOR Cto = linear_to_sRGB(Crgb);
wOutput[ActiveLane(lane)] = Cto;
}
}
} else {
use_colorconfig = true;
}
if (use_colorconfig) {
// Serialize calls to ocio
wOutput.mask().foreach ([=, &cs](ActiveLane lane) -> void {
COLOR C = wInput[lane];
COLOR Cto = cs.ocio_transform(fromspace, tospace, C, context);
wOutput[lane] = Cto;
});
}
}
#undef WIDE_TRANSFORMC_OMP_SIMD_LOOP
} // namespace
OSL_BATCHOP void
__OSL_MASKED_OP3(transform_color, Wv, s,
s)(void* bsg_, void* Cin, int Cin_derivs, void* Cout,
int Cout_derivs, ustring_pod from_, ustring_pod to_,
unsigned int mask_value)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
ShadingContext* ctx = context_from_bsg(bsg_);
const ustring& from = USTR(from_);
const ustring& to = USTR(to_);
if (Cout_derivs) {
if (Cin_derivs) {
Masked<Dual2<Color3>> wOutput(Cout, Mask(mask_value));
Wide<const Dual2<Color3>> wInput(Cin);
wide_transformc(cs, from, to, wOutput, wInput, ctx);
return;
} else {
// We had output derivs, but not input. Zero the output
// derivs and fall through to the non-deriv case.
MaskedDx<Color3> wOutputDx(Cout, Mask(mask_value));
MaskedDy<Color3> wOutputDy(Cout, Mask(mask_value));
assign_all(wOutputDx, Color3(0.0f));
assign_all(wOutputDy, Color3(0.0f));
}
}
// No-derivs case
Masked<Color3> wOutput(Cout, Mask(mask_value));
Wide<const Color3> wInput(Cin);
wide_transformc(cs, from, to, wOutput, wInput, ctx);
return;
}
OSL_BATCHOP void
__OSL_OP3(transform_color, v, s, s)(void* bsg_, void* Cin, int Cin_derivs,
void* Cout, int Cout_derivs,
ustring_pod from_, ustring_pod to_)
{
const ColorSystem& cs = cs_from_bsg(bsg_);
ShadingContext* ctx = context_from_bsg(bsg_);
const ustring& from = USTR(from_);
const ustring& to = USTR(to_);
if (Cout_derivs) {
if (Cin_derivs) {
DCOL(Cout) = cs.transformc(from, to, DCOL(Cin), ctx);
return;
} else {
// We had output derivs, but not input. Zero the output
// derivs and fall through to the non-deriv case.
((Color3*)Cout)[1].setValue(0.0f, 0.0f, 0.0f);
((Color3*)Cout)[2].setValue(0.0f, 0.0f, 0.0f);
}
}
// No-derivs case
COL(Cout) = cs.transformc(from, to, COL(Cin), ctx);
return;
}
} // namespace __OSL_WIDE_PVT
OSL_NAMESPACE_END
#include "undef_opname_macros.h"