-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathoptexture.cpp
More file actions
497 lines (418 loc) · 15.4 KB
/
optexture.cpp
File metadata and controls
497 lines (418 loc) · 15.4 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
/*
Copyright (c) 2009-2015 Sony Pictures Imageworks Inc., et al.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Sony Pictures Imageworks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/////////////////////////////////////////////////////////////////////////
/// \file
///
/// Shader interpreter implementation of texture operations.
///
/////////////////////////////////////////////////////////////////////////
#include <OpenImageIO/fmath.h>
#include <OpenImageIO/simd.h>
#include <iostream>
#include <cmath>
#include "oslexec_pvt.h"
#include <OSL/dual.h>
OSL_NAMESPACE_ENTER
namespace pvt {
// Utility: retrieve a pointer to the ShadingContext's texture options
// struct, also re-initialize its contents.
OSL_SHADEOP void *
osl_get_texture_options (void *sg_)
{
ShaderGlobals *sg = (ShaderGlobals *)sg_;
TextureOpt *opt = sg->context->texture_options_ptr ();
new (opt) TextureOpt;
return opt;
}
OSL_SHADEOP void
osl_texture_set_firstchannel (void *opt, int x)
{
((TextureOpt *)opt)->firstchannel = x;
}
OSL_SHADEOP void
osl_texture_set_swrap (void *opt, const char *x)
{
((TextureOpt *)opt)->swrap = TextureOpt::decode_wrapmode(USTR(x));
}
OSL_SHADEOP void
osl_texture_set_twrap (void *opt, const char *x)
{
((TextureOpt *)opt)->twrap = TextureOpt::decode_wrapmode(USTR(x));
}
OSL_SHADEOP void
osl_texture_set_rwrap (void *opt, const char *x)
{
((TextureOpt *)opt)->rwrap = TextureOpt::decode_wrapmode(USTR(x));
}
OSL_SHADEOP void
osl_texture_set_stwrap (void *opt, const char *x)
{
TextureOpt::Wrap code = TextureOpt::decode_wrapmode(USTR(x));
((TextureOpt *)opt)->swrap = code;
((TextureOpt *)opt)->twrap = code;
}
OSL_SHADEOP void
osl_texture_set_swrap_code (void *opt, int mode)
{
((TextureOpt *)opt)->swrap = (TextureOpt::Wrap)mode;
}
OSL_SHADEOP void
osl_texture_set_twrap_code (void *opt, int mode)
{
((TextureOpt *)opt)->twrap = (TextureOpt::Wrap)mode;
}
OSL_SHADEOP void
osl_texture_set_rwrap_code (void *opt, int mode)
{
((TextureOpt *)opt)->rwrap = (TextureOpt::Wrap)mode;
}
OSL_SHADEOP void
osl_texture_set_stwrap_code (void *opt, int mode)
{
((TextureOpt *)opt)->swrap = (TextureOpt::Wrap)mode;
((TextureOpt *)opt)->twrap = (TextureOpt::Wrap)mode;
}
OSL_SHADEOP void
osl_texture_set_sblur (void *opt, float x)
{
((TextureOpt *)opt)->sblur = x;
}
OSL_SHADEOP void
osl_texture_set_tblur (void *opt, float x)
{
((TextureOpt *)opt)->tblur = x;
}
OSL_SHADEOP void
osl_texture_set_rblur (void *opt, float x)
{
((TextureOpt *)opt)->rblur = x;
}
OSL_SHADEOP void
osl_texture_set_stblur (void *opt, float x)
{
((TextureOpt *)opt)->sblur = x;
((TextureOpt *)opt)->tblur = x;
}
OSL_SHADEOP void
osl_texture_set_swidth (void *opt, float x)
{
((TextureOpt *)opt)->swidth = x;
}
OSL_SHADEOP void
osl_texture_set_twidth (void *opt, float x)
{
((TextureOpt *)opt)->twidth = x;
}
OSL_SHADEOP void
osl_texture_set_rwidth (void *opt, float x)
{
((TextureOpt *)opt)->rwidth = x;
}
OSL_SHADEOP void
osl_texture_set_stwidth (void *opt, float x)
{
((TextureOpt *)opt)->swidth = x;
((TextureOpt *)opt)->twidth = x;
}
OSL_SHADEOP void
osl_texture_set_fill (void *opt, float x)
{
((TextureOpt *)opt)->fill = x;
}
OSL_SHADEOP void
osl_texture_set_time (void *opt, float x)
{
((TextureOpt *)opt)->time = x;
}
OSL_SHADEOP void
osl_texture_set_anisotropic (void *opt, int x)
{
((TextureOpt *)opt)->anisotropic = x;
}
inline int
tex_interp_to_code (ustring modename)
{
static ustring u_linear ("linear");
static ustring u_smartcubic ("smartcubic");
static ustring u_cubic ("cubic");
static ustring u_closest ("closest");
int mode = -1;
if (modename == u_smartcubic)
mode = TextureOpt::InterpSmartBicubic;
else if (modename == u_linear)
mode = TextureOpt::InterpBilinear;
else if (modename == u_cubic)
mode = TextureOpt::InterpBicubic;
else if (modename == u_closest)
mode = TextureOpt::InterpClosest;
return mode;
}
OSL_SHADEOP void
osl_texture_set_interp (void *opt, const char *modename)
{
int mode = tex_interp_to_code (USTR(modename));
if (mode >= 0)
((TextureOpt *)opt)->interpmode = (TextureOpt::InterpMode)mode;
}
OSL_SHADEOP void
osl_texture_set_interp_code (void *opt, int mode)
{
((TextureOpt *)opt)->interpmode = (TextureOpt::InterpMode)mode;
}
OSL_SHADEOP void
osl_texture_set_subimage (void *opt, int subimage)
{
((TextureOpt *)opt)->subimage = subimage;
}
OSL_SHADEOP void
osl_texture_set_subimagename (void *opt, const char *subimagename)
{
((TextureOpt *)opt)->subimagename = USTR(subimagename);
}
OSL_SHADEOP void
osl_texture_set_missingcolor_arena (void *opt, const void *missing)
{
((TextureOpt *)opt)->missingcolor = (const float *)missing;
}
OSL_SHADEOP void
osl_texture_set_missingcolor_alpha (void *opt, int alphaindex,
float missingalpha)
{
float *m = (float *)((TextureOpt *)opt)->missingcolor;
if (m)
m[alphaindex] = missingalpha;
}
OSL_SHADEOP int
osl_texture (void *sg_, const char *name, void *handle,
void *opt_, float s, float t,
float dsdx, float dtdx, float dsdy, float dtdy,
int chans, void *result, void *dresultdx, void *dresultdy,
void *alpha, void *dalphadx, void *dalphady,
ustring *errormessage)
{
ShaderGlobals *sg = (ShaderGlobals *)sg_;
TextureOpt *opt = (TextureOpt *)opt_;
bool derivs = (dresultdx != NULL);
// It's actually faster to ask for 4 channels (even if we need fewer)
// and ensure that they're being put in aligned memory.
OIIO::simd::float4 result_simd, dresultds_simd, dresultdt_simd;
bool ok = sg->renderer->texture (USTR(name),
(TextureSystem::TextureHandle *)handle, NULL,
*opt, sg, s, t, dsdx, dtdx, dsdy, dtdy, 4,
(float *)&result_simd,
derivs ? (float *)&dresultds_simd : NULL,
derivs ? (float *)&dresultdt_simd : NULL,
errormessage);
for (int i = 0; i < chans; ++i)
((float *)result)[i] = result_simd[i];
if (alpha)
((float *)alpha)[0] = result_simd[chans];
// Correct our st texture space gradients into xy-space gradients
if (derivs) {
OIIO::simd::float4 dresultdx_simd = dresultds_simd * dsdx + dresultdt_simd * dtdx;
OIIO::simd::float4 dresultdy_simd = dresultds_simd * dsdy + dresultdt_simd * dtdy;
for (int i = 0; i < chans; ++i)
((float *)dresultdx)[i] = dresultdx_simd[i];
for (int i = 0; i < chans; ++i)
((float *)dresultdy)[i] = dresultdy_simd[i];
if (dalphadx) {
((float *)dalphadx)[0] = dresultdx_simd[chans];
((float *)dalphady)[0] = dresultdy_simd[chans];
}
}
if (ok && errormessage)
*errormessage = Strings::_emptystring_;
return ok;
}
OSL_SHADEOP int
osl_texture3d (void *sg_, const char *name, void *handle,
void *opt_, void *P_,
void *dPdx_, void *dPdy_, void *dPdz_, int chans,
void *result, void *dresultdx,
void *dresultdy, void *dresultdz,
void *alpha, void *dalphadx,
void *dalphady, void *dalphadz,
ustring *errormessage)
{
const Vec3 &P (*(Vec3 *)P_);
const Vec3 &dPdx (*(Vec3 *)dPdx_);
const Vec3 &dPdy (*(Vec3 *)dPdy_);
const Vec3 &dPdz (*(Vec3 *)dPdz_);
ShaderGlobals *sg = (ShaderGlobals *)sg_;
TextureOpt *opt = (TextureOpt *)opt_;
bool derivs = (dresultdx != NULL || dalphadx != NULL);
// It's actually faster to ask for 4 channels (even if we need fewer)
// and ensure that they're being put in aligned memory.
OIIO::simd::float4 result_simd, dresultds_simd, dresultdt_simd, dresultdr_simd;
bool ok = sg->renderer->texture3d (USTR(name),
(TextureSystem::TextureHandle *)handle, NULL,
*opt, sg, P, dPdx, dPdy, dPdz,
4, (float *)&result_simd,
derivs ? (float *)&dresultds_simd : NULL,
derivs ? (float *)&dresultdt_simd : NULL,
derivs ? (float *)&dresultdr_simd : NULL,
errormessage);
for (int i = 0; i < chans; ++i)
((float *)result)[i] = result_simd[i];
if (alpha)
((float *)alpha)[0] = result_simd[chans];
// Correct our str texture space gradients into xyz-space gradients
if (derivs) {
OIIO::simd::float4 dresultdx_simd = dresultds_simd * dPdx[0] + dresultdt_simd * dPdx[1] + dresultdr_simd * dPdx[2];
OIIO::simd::float4 dresultdy_simd = dresultds_simd * dPdy[0] + dresultdt_simd * dPdy[1] + dresultdr_simd * dPdy[2];
OIIO::simd::float4 dresultdz_simd = dresultds_simd * dPdz[0] + dresultdt_simd * dPdz[1] + dresultdr_simd * dPdz[2];
if (dresultdx) {
for (int i = 0; i < chans; ++i)
((float *)dresultdx)[i] = dresultdx_simd[i];
for (int i = 0; i < chans; ++i)
((float *)dresultdy)[i] = dresultdy_simd[i];
for (int i = 0; i < chans; ++i)
((float *)dresultdz)[i] = dresultdz_simd[i];
}
if (dalphadx) {
((float *)dalphadx)[0] = dresultdx_simd[chans];
((float *)dalphady)[0] = dresultdy_simd[chans];
((float *)dalphadz)[0] = dresultdz_simd[chans];
}
}
if (ok && errormessage)
*errormessage = Strings::_emptystring_;
return ok;
}
OSL_SHADEOP int
osl_environment (void *sg_, const char *name, void *handle,
void *opt_, void *R_,
void *dRdx_, void *dRdy_, int chans,
void *result, void *dresultdx, void *dresultdy,
void *alpha, void *dalphadx, void *dalphady,
ustring *errormessage)
{
const Vec3 &R (*(Vec3 *)R_);
const Vec3 &dRdx (*(Vec3 *)dRdx_);
const Vec3 &dRdy (*(Vec3 *)dRdy_);
ShaderGlobals *sg = (ShaderGlobals *)sg_;
TextureOpt *opt = (TextureOpt *)opt_;
// It's actually faster to ask for 4 channels (even if we need fewer)
// and ensure that they're being put in aligned memory.
OIIO::simd::float4 local_result;
bool ok = sg->renderer->environment (USTR(name),
(TextureSystem::TextureHandle *)handle,
NULL, *opt, sg, R, dRdx, dRdy, 4,
(float *)&local_result, NULL, NULL,
errormessage);
for (int i = 0; i < chans; ++i)
((float *)result)[i] = local_result[i];
// For now, just zero out the result derivatives. If somebody needs
// derivatives of environment lookups, we'll fix it. The reason
// that this is a pain is that OIIO's environment call (unwisely?)
// returns the st gradients, but we want the xy gradients, which is
// tricky because we (this function you're reading) don't know which
// projection is used to generate st from R. Ugh. Sweep under the
// rug for a day when somebody is really asking for it.
if (dresultdx) {
for (int i = 0; i < chans; ++i)
((float *)dresultdx)[i] = 0.0f;
for (int i = 0; i < chans; ++i)
((float *)dresultdy)[i] = 0.0f;
}
if (alpha) {
((float *)alpha)[0] = local_result[chans];
// Zero out the alpha derivatives, for the same reason as above.
if (dalphadx)
((float *)dalphadx)[0] = 0.0f;
if (dalphady)
((float *)dalphady)[0] = 0.0f;
}
if (ok && errormessage)
*errormessage = Strings::_emptystring_;
return ok;
}
OSL_SHADEOP int
osl_get_textureinfo (void *sg_, const char *name, void *handle,
void *dataname, int type,
int arraylen, int aggregate, void *data)
{
// recreate TypeDesc
TypeDesc typedesc;
typedesc.basetype = type;
typedesc.arraylen = arraylen;
typedesc.aggregate = aggregate;
ShaderGlobals *sg = (ShaderGlobals *)sg_;
return sg->renderer->get_texture_info (sg, USTR(name),
(RendererServices::TextureHandle *)handle,
0 /*FIXME-ptex*/,
USTR(dataname), typedesc, data);
}
// Trace
// Utility: retrieve a pointer to the ShadingContext's trace options
// struct, also re-initialize its contents.
OSL_SHADEOP void *
osl_get_trace_options (void *sg_)
{
ShaderGlobals *sg = (ShaderGlobals *)sg_;
RendererServices::TraceOpt *opt = sg->context->trace_options_ptr ();
new (opt) RendererServices::TraceOpt;
return opt;
}
OSL_SHADEOP void
osl_trace_set_mindist (void *opt, float x)
{
((RendererServices::TraceOpt *)opt)->mindist = x;
}
OSL_SHADEOP void
osl_trace_set_maxdist (void *opt, float x)
{
((RendererServices::TraceOpt *)opt)->maxdist = x;
}
OSL_SHADEOP void
osl_trace_set_shade (void *opt, int x)
{
((RendererServices::TraceOpt *)opt)->shade = x;
}
OSL_SHADEOP void
osl_trace_set_traceset (void *opt, const char *x)
{
((RendererServices::TraceOpt *)opt)->traceset = USTR(x);
}
OSL_SHADEOP int
osl_trace (void *sg_, void *opt_, void *Pos_, void *dPosdx_, void *dPosdy_,
void *Dir_, void *dDirdx_, void *dDirdy_)
{
ShaderGlobals *sg = (ShaderGlobals *)sg_;
RendererServices::TraceOpt *opt = (RendererServices::TraceOpt *)opt_;
static const Vec3 Zero (0.0f, 0.0f, 0.0f);
const Vec3 *Pos = (Vec3 *)Pos_;
const Vec3 *dPosdx = dPosdx_ ? (Vec3 *)dPosdx_ : &Zero;
const Vec3 *dPosdy = dPosdy_ ? (Vec3 *)dPosdy_ : &Zero;
const Vec3 *Dir = (Vec3 *)Dir_;
const Vec3 *dDirdx = dDirdx_ ? (Vec3 *)dDirdx_ : &Zero;
const Vec3 *dDirdy = dDirdy_ ? (Vec3 *)dDirdy_ : &Zero;
return sg->renderer->trace (*opt, sg, *Pos, *dPosdx, *dPosdy,
*Dir, *dDirdx, *dDirdy);
}
} // namespace pvt
OSL_NAMESPACE_EXIT