Skip to content

Commit

Permalink
fix: Bugs in vector2.h, vector4.h, color2.h, color4.h, docs (#1892)
Browse files Browse the repository at this point in the history
Fix a variety of typos in the 2- and 4-component color and vector
helper structs. Very embarrassing!

Also embarrassing, a typo in the docs had said that the way to
overload the `!=` operator was with a function called
`__operator__ne__` while actually, all along it had been
`__operator__neq__`.

Comprehensive test of color2, color4, vector2, vector4.

New testing header: testsuite/common/shaders/osl-unittest.h
This has utilities to make it easier to make unit tests for shader
functions in our testsuite.

Various other minor fixes to these headers.

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz authored Oct 30, 2024
1 parent 2a4c401 commit 0d3e9d2
Show file tree
Hide file tree
Showing 30 changed files with 896 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ macro (osl_add_all_tests)
bug-param-duplicate bug-peep bug-return
calculatenormal-reg
cellnoise closure closure-array closure-layered closure-parameters closure-zero closure-conditional
color color-reg colorspace comparison
color color2 color4 color-reg colorspace comparison
complement-reg compile-buffer compassign-bool compassign-reg
component-range
control-flow-reg connect-components
Expand Down Expand Up @@ -402,7 +402,7 @@ macro (osl_add_all_tests)
userdata userdata-defaults userdata-partial userdata-custom userdata-passthrough
vararray-connect vararray-default
vararray-deserialize vararray-param
vecctr vector vector-reg
vecctr vector vector2 vector4 vector-reg
wavelength_color wavelength_color-reg Werror xml xml-reg )

# Only run the ocio test if the OIIO we are using has OCIO support
Expand Down
2 changes: 1 addition & 1 deletion src/doc/languagespec.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,7 @@ \subsection{Operator overloading}
{\cf \bfseries >} & {\cf __operator__gt__} & \\
{\cf \bfseries >=} & {\cf __operator__ge__} & \\
{\cf \bfseries ==} & {\cf __operator__eq__} & \\
{\cf \bfseries !=} & {\cf __operator__ne__} & \\[1.5ex]
{\cf \bfseries !=} & {\cf __operator__neq__} & \\[1.5ex]
{\cf \bfseries \&} & {\cf __operator__bitand__} & \\
{\cf \bfseries \textasciicircum} & {\cf __operator__xor__} & \\
{\cf \bfseries |} & {\cf __operator__bitor__} & \\
Expand Down
2 changes: 1 addition & 1 deletion src/doc/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ Operator | Overload function name | notes
`>` | `__operator__gt__` |
`>=` | `__operator__ge__` |
`==` | `__operator__eq__` |
`!=` | `__operator__ne__` |
`!=` | `__operator__neq__` |
`\&` | `__operator__bitand__` |
`^` | `__operator__xor__` |
`\|` | `__operator__bitor__` |
Expand Down
22 changes: 10 additions & 12 deletions src/shaders/color2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ color2 __operator__neg__(color2 a)

color2 __operator__add__(color2 a, color2 b)
{
return color2(a.r + a.r, b.a + b.a);
return color2(a.r + b.r, a.a + b.a);
}

color2 __operator__add__(color2 a, int b)
Expand Down Expand Up @@ -66,17 +66,17 @@ color2 __operator__sub__(color2 a, float b)

color2 __operator__sub__(int a, color2 b)
{
return b - color2(a, a);
return color2(a,a) - b;
}

color2 __operator__sub__(float a, color2 b)
{
return b - color2(a, a);
return color2(a,a) - b;
}

color2 __operator__mul__(color2 a, color2 b)
{
return color2(a.r * a.r, b.a * b.a);
return color2(a.r * b.r, a.a * b.a);
}

color2 __operator__mul__(color2 a, int b)
Expand Down Expand Up @@ -106,13 +106,13 @@ color2 __operator__div__(color2 a, color2 b)

color2 __operator__div__(color2 a, int b)
{
float b_inv = 1/b;
float b_inv = 1.0 / float(b);
return a * color2(b_inv, b_inv);
}

color2 __operator__div__(color2 a, float b)
{
float b_inv = 1/b;
float b_inv = 1.0 / b;
return a * color2(b_inv, b_inv);
}

Expand All @@ -128,10 +128,10 @@ color2 __operator__div__(float a, color2 b)

int __operator__eq__(color2 a, color2 b)
{
return (a.r == a.r) && (b.a == b.a);
return (a.r == b.r) && (a.a == b.a);
}

int __operator__ne__(color2 a, color2 b)
int __operator__neq__(color2 a, color2 b)
{
return (a.r != b.r) || (a.a != b.a);
}
Expand Down Expand Up @@ -231,8 +231,8 @@ color2 min(color2 a, float b)

color2 fmod(color2 a, color2 b)
{
return color2(fmod(a.r, a.r),
fmod(b.a, b.a));
return color2(fmod(a.r, b.r),
fmod(a.a, b.a));
}

color2 fmod(color2 a, int b)
Expand Down Expand Up @@ -303,5 +303,3 @@ color2 atan2(color2 a, color2 b)
return color2(atan2(a.r, b.r),
atan2(a.a, b.a));
}


11 changes: 3 additions & 8 deletions src/shaders/color4.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ color4 __operator__div__(color4 a, color4 b)

color4 __operator__div__(color4 a, int b)
{
float b_inv = 1/b;
float b_inv = 1.0 / float(b);
return a * color4(color(b_inv), b_inv);
}

color4 __operator__div__(color4 a, float b)
{
float b_inv = 1/b;
float b_inv = 1.0 / b;
return a * color4(color(b_inv), b_inv);
}

Expand All @@ -131,7 +131,7 @@ int __operator__eq__(color4 a, color4 b)
return (a.rgb == b.rgb) && (a.a == b.a);
}

int __operator__ne__(color4 a, color4 b)
int __operator__neq__(color4 a, color4 b)
{
return (a.rgb != b.rgb) || (a.a != b.a);
}
Expand Down Expand Up @@ -183,11 +183,6 @@ color4 mix(color4 a, color4 b, float x )
mix(a.a, b.a, x));
}

float dot(color4 a, color b)
{
return dot(a.rgb, b);
}

color4 smoothstep(color4 edge0, color4 edge1, color4 c)
{
return color4(smoothstep(edge0.rgb, edge1.rgb, c.rgb),
Expand Down
6 changes: 3 additions & 3 deletions src/shaders/matrix33.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#define MATRIX33_H


// matrix33 is just a trick to make what appears to be a 3x3 matrix, but
// underneath using a native OSL (4x4) matrix with appropriate zeroed extra
// components to make the math all work out equivalently.
struct matrix33
{
matrix m;
Expand Down Expand Up @@ -159,6 +162,3 @@ normal transform(matrix33 a, normal b)
{
return transform(a.m, b);
}



30 changes: 14 additions & 16 deletions src/shaders/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ vector2 __operator__div__(vector2 a, vector2 b)

vector2 __operator__div__(vector2 a, int b)
{
float b_inv = 1/b;
float b_inv = 1.0 / float(b);
return a * vector2(b_inv, b_inv);
}

vector2 __operator__div__(vector2 a, float b)
{
float b_inv = 1/b;
float b_inv = 1.0 / b;
return a * vector2(b_inv, b_inv);
}

Expand All @@ -130,7 +130,7 @@ int __operator__eq__(vector2 a, vector2 b)
return (a.x == b.x) && (a.y == b.y);
}

int __operator__ne__(vector2 a, vector2 b)
int __operator__neq__(vector2 a, vector2 b)
{
return (a.x != b.x) || (a.y != b.y);
}
Expand Down Expand Up @@ -221,25 +221,25 @@ vector2 max(vector2 a, vector2 b)
max(a.y, b.y));
}

vector2 max(vector2 a, float b)
vector2 min(vector2 a, vector2 b)
{
return max(a, vector2(b, b));
return vector2 (min(a.x, b.x),
min(a.y, b.y));
}

vector2 normalize(vector2 a)
vector2 min(vector2 a, float b)
{
return a / length(a);
return min(a, vector2(b, b));
}

vector2 min(vector2 a, vector2 b)
vector2 max(vector2 a, float b)
{
return vector2 (min(a.x, a.x),
min(b.y, b.y));
return max(a, vector2(b, b));
}

vector2 min(vector2 a, float b)
vector2 normalize(vector2 a)
{
return min(a, vector2(b, b));
return a / length(a);
}

vector2 mod(vector2 a, vector2 b)
Expand Down Expand Up @@ -307,13 +307,11 @@ vector2 acos(vector2 a)
vector2 atan2(vector2 a, float f)
{
return vector2(atan2(a.x, f),
atan2(a.y, f));
atan2(a.y, f));
}

vector2 atan2(vector2 a, vector2 b)
{
return vector2(atan2(a.x, b.x),
atan2(a.y, b.y));
atan2(a.y, b.y));
}


16 changes: 8 additions & 8 deletions src/shaders/vector4.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ vector4 __operator__div__(vector4 a, vector4 b)

vector4 __operator__div__(vector4 a, int b)
{
float b_inv = 1/b;
float b_inv = 1.0 / float(b);
return a * vector4(b_inv, b_inv, b_inv, b_inv);
}

vector4 __operator__div__(vector4 a, float b)
{
float b_inv = 1/b;
float b_inv = 1.0 / b;
return a * vector4(b_inv, b_inv, b_inv, b_inv);
}

Expand All @@ -133,7 +133,7 @@ int __operator__eq__(vector4 a, vector4 b)
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w);
}

int __operator__ne__(vector4 a, vector4 b)
int __operator__neq__(vector4 a, vector4 b)
{
return (a.x != b.x) || (a.y != b.y) || (a.z != b.z) || (a.w != b.w);
}
Expand Down Expand Up @@ -269,11 +269,6 @@ vector4 max(vector4 a, float b)
return max(a, vector4(b, b, b, b));
}

vector4 normalize(vector4 a)
{
return a / length(a);
}

vector4 min(vector4 a, vector4 b)
{
return vector4 (min(a.x, b.x),
Expand All @@ -287,6 +282,11 @@ vector4 min(vector4 a, float b)
return min(a, vector4(b, b, b, b));
}

vector4 normalize(vector4 a)
{
return a / length(a);
}

vector4 fmod(vector4 a, vector4 b)
{
return vector4 (fmod(a.x, b.x),
Expand Down
Empty file added testsuite/color2/BATCHED
Empty file.
Empty file added testsuite/color2/OPTIX
Empty file.
63 changes: 63 additions & 0 deletions testsuite/color2/ref/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Compiled test.osl -> test.oso
parameter initialization: param1 = 0.5 1.5
parameter initialization: param2 = 0.25 0.25

PASS: param1 == color2(0.5, 1.5)
PASS: -param1 == color2(-0.5, -1.5)
PASS: param1 != param2
PASS: param1 + param2 == color2(0.75, 1.75)
PASS: param1 + 1 == color2(1.5, 2.5)
PASS: param1 + 1.0 == color2(1.5, 2.5)
PASS: 1 + param1 == color2(1.5, 2.5)
PASS: 1.0 + param1 == color2(1.5, 2.5)
PASS: param1 - param2 == color2(0.25, 1.25)
PASS: param1 - 1 == color2(-0.5, 0.5)
PASS: param1 - 1.0 == color2(-0.5, 0.5)
PASS: 1 - param1 == color2(0.5, -0.5)
PASS: 1.0 - param1 == color2(0.5, -0.5)
PASS: param1 * param2 == color2(0.125, 0.375)
PASS: param1 * 2 == color2(1.0, 3.0)
PASS: param1 * 2.0 == color2(1.0, 3.0)
PASS: 2 * param1 == color2(1.0, 3.0)
PASS: 2.0 * param1 == color2(1.0, 3.0)
PASS: param1 / param2 == color2(2.0, 6.0)
PASS: param1 / 2 == color2(0.25, 0.75)
PASS: param1 / 2.0 == color2(0.25, 0.75)
PASS: 2 / param1 == color2(4.0, 2.0/1.5)
PASS: 2.0 / param1 == color2(4.0, 2.0/1.5)
PASS: abs(color2(0.5, 1.5)) == color2(abs(xcomp(color2(0.5, 1.5))), abs(ycomp(color2(0.5, 1.5))))
PASS: abs(color2(-0.5, -1.5)) == color2(abs(xcomp(color2(-0.5, -1.5))), abs(ycomp(color2(-0.5, -1.5))))
PASS: ceil(color2(0.5, 1.5)) == color2(ceil(xcomp(color2(0.5, 1.5))), ceil(ycomp(color2(0.5, 1.5))))
PASS: ceil(color2(-0.5, -1.5)) == color2(ceil(xcomp(color2(-0.5, -1.5))), ceil(ycomp(color2(-0.5, -1.5))))
PASS: floor(color2(0.5, 1.5)) == color2(floor(xcomp(color2(0.5, 1.5))), floor(ycomp(color2(0.5, 1.5))))
PASS: floor(color2(-0.5, -1.5)) == color2(floor(xcomp(color2(-0.5, -1.5))), floor(ycomp(color2(-0.5, -1.5))))
PASS: sqrt(color2(0.5, 1.5)) == color2(sqrt(xcomp(color2(0.5, 1.5))), sqrt(ycomp(color2(0.5, 1.5))))
PASS: exp(color2(0.5, 1.5)) == color2(exp(xcomp(color2(0.5, 1.5))), exp(ycomp(color2(0.5, 1.5))))
PASS: log(color2(0.5, 1.5)) == color2(log(xcomp(color2(0.5, 1.5))), log(ycomp(color2(0.5, 1.5))))
PASS: log2(color2(0.5, 1.5)) == color2(log2(xcomp(color2(0.5, 1.5))), log2(ycomp(color2(0.5, 1.5))))
PASS: mix(color2(1.0, 2.0), color2(21.0, 22.0), 0.0) == color2(1.0, 2.0)
PASS: mix(color2(1.0, 2.0), color2(21.0, 22.0), 1.0) == color2(21.0, 22.0)
PASS: mix(color2(1.0, 2.0), color2(21.0, 22.0), 0.5) == color2(11.0, 12.0)
PASS: smoothstep(color2(1.0, 2.0), color2(3.0, 4.0), color2(0.0, 0.0)) == color2(0.0, 0.0)
PASS: smoothstep(color2(1.0, 2.0), color2(3.0, 4.0), color2(10.0, 10.0)) == color2(1.0, 1.0)
PASS: smoothstep(color2(1.0, 2.0), color2(3.0, 4.0), color2(2.0, 3.0)) == color2(0.5, 0.5)
PASS: clamp(color2(0.0, 0.0), color2(1.0, 2.0), color2(2.0, 3.0)) == color2(1.0, 2.0)
PASS: clamp(color2(10.0, 10.0), color2(1.0, 2.0), color2(2.0, 3.0)) == color2(2.0, 3.0)
PASS: clamp(color2(1.5, 2.5), color2(1.0, 2.0), color2(2.0, 3.0)) == color2(1.5, 2.5)
PASS: clamp(color2(0.25, 0.5), 1.0, 2.0) == color2(1.0, 1.0)
PASS: clamp(color2(2.25, 2.5), 1.0, 2.0) == color2(2.0, 2.0)
PASS: clamp(color2(1.25, 1.5), 1.0, 2.0) == color2(1.25, 1.5)
PASS: max(color2(1.0, 4.0), color2(2.0, 3.0)) == color2(2.0, 4.0)
PASS: min(color2(1.0, 4.0), color2(2.0, 3.0)) == color2(1.0, 3.0)
PASS: fmod(color2(5.0, 8.0), color2(2.0, 3.0)) == color2(fmod(xcomp(color2(5.0, 8.0)), xcomp(color2(2.0, 3.0))), fmod(ycomp(color2(5.0, 8.0)), ycomp(color2(2.0, 3.0))))
PASS: pow(color2(2.0, 3.0), color2(2.5, 3.5)) == color2(pow(xcomp(color2(2.0, 3.0)), xcomp(color2(2.5, 3.5))), pow(ycomp(color2(2.0, 3.0)), ycomp(color2(2.5, 3.5))))
PASS: sign(color2(0.5, -0.5)) == color2(sign(xcomp(color2(0.5, -0.5))), sign(ycomp(color2(0.5, -0.5))))
PASS: sign(color2(-0.5, 0.5)) == color2(sign(xcomp(color2(-0.5, 0.5))), sign(ycomp(color2(-0.5, 0.5))))
PASS: sin(color2(0.5, 1.5)) == color2(sin(xcomp(color2(0.5, 1.5))), sin(ycomp(color2(0.5, 1.5))))
PASS: cos(color2(0.5, 1.5)) == color2(cos(xcomp(color2(0.5, 1.5))), cos(ycomp(color2(0.5, 1.5))))
PASS: tan(color2(0.5, 1.5)) == color2(tan(xcomp(color2(0.5, 1.5))), tan(ycomp(color2(0.5, 1.5))))
PASS: asin(color2(0.5, 0.25)) == color2(asin(xcomp(color2(0.5, 0.25))), asin(ycomp(color2(0.5, 0.25))))
PASS: acos(color2(0.5, 0.25)) == color2(acos(xcomp(color2(0.5, 0.25))), acos(ycomp(color2(0.5, 0.25))))
PASS: atan2(color2(0.5, 1.5), color2(1.0, 4.0)) == color2(atan2(xcomp(color2(0.5, 1.5)), xcomp(color2(1.0, 4.0))), atan2(ycomp(color2(0.5, 1.5)), ycomp(color2(1.0, 4.0))))
PASS: atan2(color2(2.0, 0.5), 1.0) == color2(atan2(2.0, 1.0), atan2(0.5, 1.0))

7 changes: 7 additions & 0 deletions testsuite/color2/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python

# Copyright Contributors to the Open Shading Language project.
# SPDX-License-Identifier: BSD-3-Clause
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

command = testshade("test")
Loading

0 comments on commit 0d3e9d2

Please sign in to comment.