Skip to content

Commit 1778aaa

Browse files
committed
feat: Optional texture argument for colorspace (part 1 -- don't reject) (#1966)
Optional texture argument for colorspace (part 1 -- don't reject) A while back, we proposed a new optional token/value parameter for the texture functions: "colorspace", followed by a color space name, which indicates that the texture name referenced is understood to be in that colorspace (this is passed to the TextureSystem, which has known for some time how to ensure the right transformations are applied as tile data is read in). We do not fully implement this yet! It's more involved than I have time to get right by the scheduled OSL 1.14 non-beta release (which was supposed to have been on Apr 1, oh no). Not wanting to delay things further, this change at least causes the use of the new parameter (which apparently, MaterialX has been using) to be silently accepted and ignored, rather than generating runtime errors. We'll come back right away for a more complete working implementation. Signed-off-by: Larry Gritz <[email protected]>
1 parent 9357886 commit 1778aaa

File tree

10 files changed

+87
-4
lines changed

10 files changed

+87
-4
lines changed

src/cmake/testing.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ macro (osl_add_all_tests)
392392
test-fmt-cxpf test-fmt-noise test-fmt-matrixcolor
393393
test-fmt-stpf test-fmt-errorwarning test-fmt-errorwarning-repeats
394394
texture-alpha texture-alpha-derivs
395-
texture-blur texture-connected-options
395+
texture-blur texture-colorspace texture-connected-options
396396
texture-derivs texture-environment texture-errormsg
397397
texture-environment-opts-reg
398398
texture-firstchannel texture-interp

src/include/OSL/batched_texture.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct UniformTextureOptions {
4545
#endif
4646
float fill = 0.0f; ///< Fill value for missing channels
4747
const float* missingcolor = nullptr; ///< Color for missing texture
48+
#ifdef OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE
49+
int colortransformid = 0; ///< Color space id of the texture
50+
#endif
51+
// Options set INTERNALLY by libtexture after the options are passed
52+
// by the user. Users should not attempt to alter these!
53+
int private_envlayout = 0; // Layout for environment wrap
4854
};
4955

5056
template<int WidthT> struct VaryingTextureOptions {
@@ -72,7 +78,7 @@ template<int WidthT> struct BatchedTextureOptions {
7278

7379
// Options set INTERNALLY by libtexture after the options are passed
7480
// by the user. Users should not attempt to alter these!
75-
int private_envlayout = 0; // Layout for environment wrap
81+
// int private_envlayout = 0; // Layout for environment wrap
7682

7783
// Implementation detail
7884
// keep order synchronized to the data members in this structure
@@ -96,6 +102,9 @@ template<int WidthT> struct BatchedTextureOptions {
96102
conservative_filter,
97103
fill,
98104
missingcolor,
105+
#ifdef OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE
106+
colortransformid,
107+
#endif
99108
private_envlayout,
100109
count
101110
};
@@ -220,6 +229,12 @@ static_assert(
220229
offsetof(OIIO::TextureOptBatch, missingcolor)
221230
== uniform_offset + offsetof(UniformTextureOptions, missingcolor),
222231
"BatchedTextureOptions members offset different than OIIO::TextureOptBatch");
232+
# ifdef OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE
233+
static_assert(
234+
offsetof(OIIO::TextureOptBatch, colortransformid)
235+
== uniform_offset + offsetof(UniformTextureOptions, colortransformid),
236+
"BatchedTextureOptions members offset different than OIIO::TextureOptBatch");
237+
# endif
223238

224239
OSL_PRAGMA_WARNING_POP
225240
} // namespace validate_offsets

src/include/OSL/strdecls.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ STRDECL("CIE", CIE)
142142
STRDECL("AdobeRGB", AdobeRGB)
143143
STRDECL("ACES2065-1", ACES2065_1)
144144
STRDECL("ACEScg", ACEScg)
145+
STRDECL("colorspace", colorspace)
145146
STRDECL("colorsystem", colorsystem)
146147
STRDECL("arraylength", arraylength)
147148
STRDECL("unknown", unknown)
148149
STRDECL("ERROR: Unknown color space transformation \"%s\" -> \"%s\"\n",
149150
ErrorColorTransform)
150-
STRDECL("world", world)
151+
STRDECL("world", world)

src/liboslexec/batched_llvm_gen.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,6 +4316,9 @@ llvm_batched_texture_options(BatchedBackendLLVM& rop, int opnum,
43164316
llvm::Value* anisotropic = rop.ll.constant(32);
43174317
llvm::Value* conservative_filter = rop.ll.constant(1);
43184318
llvm::Value* fill = rop.ll.constant(0.0f);
4319+
#ifdef OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE
4320+
llvm::Value* colortransformid = rop.ll.constant(0);
4321+
#endif
43194322

43204323
bool is_swrap_uniform = true;
43214324
bool is_twrap_uniform = true;
@@ -4606,6 +4609,18 @@ llvm_batched_texture_options(BatchedBackendLLVM& rop, int opnum,
46064609
continue;
46074610
}
46084611

4612+
if (name == Strings::colorspace && valtype == TypeDesc::STRING) {
4613+
if (Val.is_constant()) {
4614+
// Just ignore this option for now.
4615+
// FIXME: need full implementation
4616+
continue;
4617+
} else {
4618+
rop.shadingcontext()->errorfmt(
4619+
"texture{} optional argument \"{}\" must be constant after optimization ({}:{})",
4620+
tex3d ? "3d" : "", name, op.sourcefile(), op.sourceline());
4621+
continue;
4622+
}
4623+
}
46094624
if (name == Strings::time
46104625
&& (valtype == TypeDesc::FLOAT || valtype == TypeDesc::INT)) {
46114626
// NOTE: currently no supported 3d texture format makes use of time.
@@ -4693,6 +4708,14 @@ llvm_batched_texture_options(BatchedBackendLLVM& rop, int opnum,
46934708
static_cast<int>(
46944709
LLVMMemberIndex::missingcolor)));
46954710

4711+
#ifdef OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE
4712+
// FIXME: We currently ignore the color space, so just make it 0
4713+
rop.ll.op_unmasked_store(
4714+
colortransformid,
4715+
rop.ll.GEP(bto_type, bto, 0,
4716+
static_cast<int>(LLVMMemberIndex::colortransformid)));
4717+
#endif
4718+
46964719
// blur's and width's are always communicated as wide, we we will handle them here
46974720
rop.ll.op_unmasked_store(sblur, rop.ll.GEP(bto_type, bto, 0,
46984721
static_cast<int>(
@@ -4936,6 +4959,7 @@ llvm_batched_texture_varying_options(BatchedBackendLLVM& rop, int opnum,
49364959
continue;
49374960
}
49384961

4962+
SKIP_PARAM_WIDE_STRING(colorspace)
49394963
SKIP_PARAM_WIDE_FLOAT(time)
49404964

49414965
rop.shadingcontext()->errorfmt(

src/liboslexec/batched_llvm_instance.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ BatchedBackendLLVM::llvm_type_batched_texture_options()
769769
sg_types.push_back(ll.type_int()); // conservative_filter
770770
sg_types.push_back(ll.type_float()); // fill
771771
sg_types.push_back(ll.type_ptr(ll.type_float())); // missingcolor
772+
sg_types.push_back(ll.type_int()); // colortransformid
772773

773774
// Private internal data
774775
sg_types.push_back(ll.type_int()); // envlayout
@@ -2748,7 +2749,9 @@ BatchedBackendLLVM::build_offsets_of_BatchedTextureOptions(
27482749
offset_by_index.push_back(uniform_offset
27492750
+ offsetof(UniformTextureOptions, missingcolor));
27502751
offset_by_index.push_back(
2751-
offsetof(BatchedTextureOptions<WidthT>, private_envlayout));
2752+
uniform_offset + offsetof(UniformTextureOptions, colortransformid));
2753+
offset_by_index.push_back(
2754+
uniform_offset + offsetof(UniformTextureOptions, private_envlayout));
27522755
}
27532756

27542757

src/liboslexec/llvm_gen.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,6 +2756,19 @@ llvm_gen_texture_options(BackendLLVM& rop, int opnum, int first_optional_arg,
27562756
rop.ll.constant(nchans), val);
27572757
continue;
27582758
}
2759+
if (name == Strings::colorspace && valtype == TypeDesc::STRING) {
2760+
if (Val.is_constant()) {
2761+
// Just ignore this option for now.
2762+
// FIXME: need full implementation
2763+
continue;
2764+
} else {
2765+
rop.shadingcontext()->errorfmt(
2766+
"texture{} optional argument \"{}\" must be constant after optimization ({}:{})",
2767+
tex3d ? "3d" : "", name, op.sourcefile(), op.sourceline());
2768+
continue;
2769+
}
2770+
}
2771+
27592772

27602773
// PARAM_FLOAT(time)
27612774
if (name == Strings::time

testsuite/texture-colorspace/BATCHED

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Compiled test.osl -> test.oso
2+
3+
Output Cout to out.exr
4+
color space - none specified: 0.5 0.5 0.5
5+
color space - sRGB: 0.5 0.5 0.5
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright Contributors to the Open Shading Language project.
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
6+
7+
command += oiiotool ("-pattern constant:color=0.5,0.5,0.5 64x64 3 -d half -otex grey.exr")
8+
command += testshade("-g 1 1 --center -od half -o Cout out.exr test")
9+
outputs = [ "out.txt" ]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright Contributors to the Open Shading Language project.
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
4+
5+
shader
6+
test (string filename = "grey.exr",
7+
output color Cout = 0)
8+
{
9+
Cout = (color)texture (filename, u, v);
10+
printf("color space - none specified: %g\n", Cout);
11+
Cout = (color)texture (filename, u, v, "colorspace", "sRGB");
12+
printf("color space - sRGB: %g\n", Cout);
13+
}

0 commit comments

Comments
 (0)