Skip to content

Commit 11286de

Browse files
committed
fix: Bugs in vector2.h, vector4.h, color2.h, color4.h, docs (#1892)
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]>
1 parent ceeaa87 commit 11286de

File tree

30 files changed

+896
-51
lines changed

30 files changed

+896
-51
lines changed

src/cmake/testing.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ macro (osl_add_all_tests)
252252
bug-param-duplicate bug-peep bug-return
253253
calculatenormal-reg
254254
cellnoise closure closure-array closure-layered closure-parameters closure-zero closure-conditional
255-
color color-reg colorspace comparison
255+
color color2 color4 color-reg colorspace comparison
256256
complement-reg compile-buffer compassign-bool compassign-reg
257257
component-range
258258
control-flow-reg connect-components
@@ -378,7 +378,7 @@ macro (osl_add_all_tests)
378378
userdata userdata-partial userdata-custom userdata-passthrough
379379
vararray-connect vararray-default
380380
vararray-deserialize vararray-param
381-
vecctr vector vector-reg
381+
vecctr vector vector2 vector4 vector-reg
382382
wavelength_color wavelength_color-reg Werror xml xml-reg )
383383

384384
# Only run the ocio test if the OIIO we are using has OCIO support

src/doc/languagespec.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ \subsection{Operator overloading}
27602760
{\cf \bfseries >} & {\cf __operator__gt__} & \\
27612761
{\cf \bfseries >=} & {\cf __operator__ge__} & \\
27622762
{\cf \bfseries ==} & {\cf __operator__eq__} & \\
2763-
{\cf \bfseries !=} & {\cf __operator__ne__} & \\[1.5ex]
2763+
{\cf \bfseries !=} & {\cf __operator__neq__} & \\[1.5ex]
27642764
{\cf \bfseries \&} & {\cf __operator__bitand__} & \\
27652765
{\cf \bfseries \textasciicircum} & {\cf __operator__xor__} & \\
27662766
{\cf \bfseries |} & {\cf __operator__bitor__} & \\

src/doc/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Operator | Overload function name | notes
518518
`>` | `__operator__gt__` |
519519
`>=` | `__operator__ge__` |
520520
`==` | `__operator__eq__` |
521-
`!=` | `__operator__ne__` |
521+
`!=` | `__operator__neq__` |
522522
`\&` | `__operator__bitand__` |
523523
`^` | `__operator__xor__` |
524524
`\|` | `__operator__bitor__` |

src/shaders/color2.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ color2 __operator__neg__(color2 a)
2626

2727
color2 __operator__add__(color2 a, color2 b)
2828
{
29-
return color2(a.r + a.r, b.a + b.a);
29+
return color2(a.r + b.r, a.a + b.a);
3030
}
3131

3232
color2 __operator__add__(color2 a, int b)
@@ -66,17 +66,17 @@ color2 __operator__sub__(color2 a, float b)
6666

6767
color2 __operator__sub__(int a, color2 b)
6868
{
69-
return b - color2(a, a);
69+
return color2(a,a) - b;
7070
}
7171

7272
color2 __operator__sub__(float a, color2 b)
7373
{
74-
return b - color2(a, a);
74+
return color2(a,a) - b;
7575
}
7676

7777
color2 __operator__mul__(color2 a, color2 b)
7878
{
79-
return color2(a.r * a.r, b.a * b.a);
79+
return color2(a.r * b.r, a.a * b.a);
8080
}
8181

8282
color2 __operator__mul__(color2 a, int b)
@@ -106,13 +106,13 @@ color2 __operator__div__(color2 a, color2 b)
106106

107107
color2 __operator__div__(color2 a, int b)
108108
{
109-
float b_inv = 1/b;
109+
float b_inv = 1.0 / float(b);
110110
return a * color2(b_inv, b_inv);
111111
}
112112

113113
color2 __operator__div__(color2 a, float b)
114114
{
115-
float b_inv = 1/b;
115+
float b_inv = 1.0 / b;
116116
return a * color2(b_inv, b_inv);
117117
}
118118

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

129129
int __operator__eq__(color2 a, color2 b)
130130
{
131-
return (a.r == a.r) && (b.a == b.a);
131+
return (a.r == b.r) && (a.a == b.a);
132132
}
133133

134-
int __operator__ne__(color2 a, color2 b)
134+
int __operator__neq__(color2 a, color2 b)
135135
{
136136
return (a.r != b.r) || (a.a != b.a);
137137
}
@@ -231,8 +231,8 @@ color2 min(color2 a, float b)
231231

232232
color2 fmod(color2 a, color2 b)
233233
{
234-
return color2(fmod(a.r, a.r),
235-
fmod(b.a, b.a));
234+
return color2(fmod(a.r, b.r),
235+
fmod(a.a, b.a));
236236
}
237237

238238
color2 fmod(color2 a, int b)
@@ -303,5 +303,3 @@ color2 atan2(color2 a, color2 b)
303303
return color2(atan2(a.r, b.r),
304304
atan2(a.a, b.a));
305305
}
306-
307-

src/shaders/color4.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ color4 __operator__div__(color4 a, color4 b)
106106

107107
color4 __operator__div__(color4 a, int b)
108108
{
109-
float b_inv = 1/b;
109+
float b_inv = 1.0 / float(b);
110110
return a * color4(color(b_inv), b_inv);
111111
}
112112

113113
color4 __operator__div__(color4 a, float b)
114114
{
115-
float b_inv = 1/b;
115+
float b_inv = 1.0 / b;
116116
return a * color4(color(b_inv), b_inv);
117117
}
118118

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

134-
int __operator__ne__(color4 a, color4 b)
134+
int __operator__neq__(color4 a, color4 b)
135135
{
136136
return (a.rgb != b.rgb) || (a.a != b.a);
137137
}
@@ -183,11 +183,6 @@ color4 mix(color4 a, color4 b, float x )
183183
mix(a.a, b.a, x));
184184
}
185185

186-
float dot(color4 a, color b)
187-
{
188-
return dot(a.rgb, b);
189-
}
190-
191186
color4 smoothstep(color4 edge0, color4 edge1, color4 c)
192187
{
193188
return color4(smoothstep(edge0.rgb, edge1.rgb, c.rgb),

src/shaders/matrix33.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#define MATRIX33_H
88

99

10+
// matrix33 is just a trick to make what appears to be a 3x3 matrix, but
11+
// underneath using a native OSL (4x4) matrix with appropriate zeroed extra
12+
// components to make the math all work out equivalently.
1013
struct matrix33
1114
{
1215
matrix m;
@@ -159,6 +162,3 @@ normal transform(matrix33 a, normal b)
159162
{
160163
return transform(a.m, b);
161164
}
162-
163-
164-

src/shaders/vector2.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ vector2 __operator__div__(vector2 a, vector2 b)
105105

106106
vector2 __operator__div__(vector2 a, int b)
107107
{
108-
float b_inv = 1/b;
108+
float b_inv = 1.0 / float(b);
109109
return a * vector2(b_inv, b_inv);
110110
}
111111

112112
vector2 __operator__div__(vector2 a, float b)
113113
{
114-
float b_inv = 1/b;
114+
float b_inv = 1.0 / b;
115115
return a * vector2(b_inv, b_inv);
116116
}
117117

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

133-
int __operator__ne__(vector2 a, vector2 b)
133+
int __operator__neq__(vector2 a, vector2 b)
134134
{
135135
return (a.x != b.x) || (a.y != b.y);
136136
}
@@ -221,25 +221,25 @@ vector2 max(vector2 a, vector2 b)
221221
max(a.y, b.y));
222222
}
223223

224-
vector2 max(vector2 a, float b)
224+
vector2 min(vector2 a, vector2 b)
225225
{
226-
return max(a, vector2(b, b));
226+
return vector2 (min(a.x, b.x),
227+
min(a.y, b.y));
227228
}
228229

229-
vector2 normalize(vector2 a)
230+
vector2 min(vector2 a, float b)
230231
{
231-
return a / length(a);
232+
return min(a, vector2(b, b));
232233
}
233234

234-
vector2 min(vector2 a, vector2 b)
235+
vector2 max(vector2 a, float b)
235236
{
236-
return vector2 (min(a.x, a.x),
237-
min(b.y, b.y));
237+
return max(a, vector2(b, b));
238238
}
239239

240-
vector2 min(vector2 a, float b)
240+
vector2 normalize(vector2 a)
241241
{
242-
return min(a, vector2(b, b));
242+
return a / length(a);
243243
}
244244

245245
vector2 mod(vector2 a, vector2 b)
@@ -307,13 +307,11 @@ vector2 acos(vector2 a)
307307
vector2 atan2(vector2 a, float f)
308308
{
309309
return vector2(atan2(a.x, f),
310-
atan2(a.y, f));
310+
atan2(a.y, f));
311311
}
312312

313313
vector2 atan2(vector2 a, vector2 b)
314314
{
315315
return vector2(atan2(a.x, b.x),
316-
atan2(a.y, b.y));
316+
atan2(a.y, b.y));
317317
}
318-
319-

src/shaders/vector4.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ vector4 __operator__div__(vector4 a, vector4 b)
108108

109109
vector4 __operator__div__(vector4 a, int b)
110110
{
111-
float b_inv = 1/b;
111+
float b_inv = 1.0 / float(b);
112112
return a * vector4(b_inv, b_inv, b_inv, b_inv);
113113
}
114114

115115
vector4 __operator__div__(vector4 a, float b)
116116
{
117-
float b_inv = 1/b;
117+
float b_inv = 1.0 / b;
118118
return a * vector4(b_inv, b_inv, b_inv, b_inv);
119119
}
120120

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

136-
int __operator__ne__(vector4 a, vector4 b)
136+
int __operator__neq__(vector4 a, vector4 b)
137137
{
138138
return (a.x != b.x) || (a.y != b.y) || (a.z != b.z) || (a.w != b.w);
139139
}
@@ -269,11 +269,6 @@ vector4 max(vector4 a, float b)
269269
return max(a, vector4(b, b, b, b));
270270
}
271271

272-
vector4 normalize(vector4 a)
273-
{
274-
return a / length(a);
275-
}
276-
277272
vector4 min(vector4 a, vector4 b)
278273
{
279274
return vector4 (min(a.x, b.x),
@@ -287,6 +282,11 @@ vector4 min(vector4 a, float b)
287282
return min(a, vector4(b, b, b, b));
288283
}
289284

285+
vector4 normalize(vector4 a)
286+
{
287+
return a / length(a);
288+
}
289+
290290
vector4 fmod(vector4 a, vector4 b)
291291
{
292292
return vector4 (fmod(a.x, b.x),

testsuite/color2/BATCHED

Whitespace-only changes.

testsuite/color2/OPTIX

Whitespace-only changes.

0 commit comments

Comments
 (0)