From e610f4ef767c42c421cdbfce57ea9ca015347dc7 Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Sat, 31 Dec 2022 13:25:42 -0800 Subject: [PATCH] Simplify OSL support library - Add 'mx' prefixes to OSL helper functions. - Remove outdated OSL helper functions. - Minor improvements to code style. --- libraries/stdlib/genosl/include/mx_funcs.h | 626 +++--------------- .../stdlib/genosl/mx_fractal3d_color3.osl | 2 +- .../stdlib/genosl/mx_fractal3d_color4.osl | 4 +- .../stdlib/genosl/mx_fractal3d_fa_color3.osl | 2 +- .../stdlib/genosl/mx_fractal3d_fa_color4.osl | 2 +- .../stdlib/genosl/mx_fractal3d_fa_vector2.osl | 2 +- .../stdlib/genosl/mx_fractal3d_fa_vector3.osl | 2 +- .../stdlib/genosl/mx_fractal3d_fa_vector4.osl | 2 +- .../stdlib/genosl/mx_fractal3d_float.osl | 2 +- .../stdlib/genosl/mx_fractal3d_vector2.osl | 2 +- .../stdlib/genosl/mx_fractal3d_vector3.osl | 2 +- .../stdlib/genosl/mx_fractal3d_vector4.osl | 2 +- libraries/stdlib/genosl/mx_noise2d_color4.osl | 2 +- .../stdlib/genosl/mx_noise2d_fa_color4.osl | 2 +- .../stdlib/genosl/mx_noise2d_fa_vector2.osl | 2 +- .../stdlib/genosl/mx_noise2d_fa_vector4.osl | 2 +- .../stdlib/genosl/mx_noise2d_vector2.osl | 2 +- .../stdlib/genosl/mx_noise2d_vector4.osl | 2 +- libraries/stdlib/genosl/mx_noise3d_color4.osl | 4 +- .../stdlib/genosl/mx_noise3d_fa_color4.osl | 2 +- .../stdlib/genosl/mx_noise3d_fa_vector2.osl | 2 +- .../stdlib/genosl/mx_noise3d_fa_vector4.osl | 2 +- .../stdlib/genosl/mx_noise3d_vector2.osl | 2 +- .../stdlib/genosl/mx_noise3d_vector4.osl | 2 +- libraries/stdlib/genosl/mx_remap.inline | 2 +- 25 files changed, 120 insertions(+), 558 deletions(-) diff --git a/libraries/stdlib/genosl/include/mx_funcs.h b/libraries/stdlib/genosl/include/mx_funcs.h index 7c15fb653c..531b13cd4d 100644 --- a/libraries/stdlib/genosl/include/mx_funcs.h +++ b/libraries/stdlib/genosl/include/mx_funcs.h @@ -51,7 +51,7 @@ closure color mx_add (closure color a, closure color b) { return a+b; } matrix33 mx_add(matrix33 a, matrix33 b) { - return matrix33 (matrix( + return matrix33(matrix( a.m[0][0]+b.m[0][0], a.m[0][1]+b.m[0][1], a.m[0][2]+b.m[0][2], 0.0, a.m[1][0]+b.m[1][0], a.m[1][1]+b.m[1][1], a.m[1][2]+b.m[1][2], 0.0, a.m[2][0]+b.m[2][0], a.m[2][1]+b.m[2][1], a.m[2][2]+b.m[2][2], 0.0, @@ -60,26 +60,29 @@ matrix33 mx_add(matrix33 a, matrix33 b) matrix33 mx_add(matrix33 a, float b) { - return matrix33 (matrix(a.m[0][0]+b, a.m[0][1]+b, a.m[0][2]+b, 0.0, - a.m[1][0]+b, a.m[1][1]+b, a.m[1][2]+b, 0.0, - a.m[2][0]+b, a.m[2][1]+b, a.m[2][2]+b, 0.0, - 0.0, 0.0, 0.0, 1.0)); + return matrix33(matrix( + a.m[0][0]+b, a.m[0][1]+b, a.m[0][2]+b, 0.0, + a.m[1][0]+b, a.m[1][1]+b, a.m[1][2]+b, 0.0, + a.m[2][0]+b, a.m[2][1]+b, a.m[2][2]+b, 0.0, + 0.0, 0.0, 0.0, 1.0)); } matrix mx_add(matrix a, matrix b) { - return matrix (a[0][0]+b[0][0], a[0][1]+b[0][1], a[0][2]+b[0][2], a[0][3]+b[0][3], - a[1][0]+b[1][0], a[1][1]+b[1][1], a[1][2]+b[1][2], a[1][3]+b[1][3], - a[2][0]+b[2][0], a[2][1]+b[2][1], a[2][2]+b[2][2], a[2][3]+b[2][3], - a[3][0]+b[3][0], a[3][1]+b[3][1], a[3][2]+b[3][2], a[3][3]+b[3][3]); + return matrix( + a[0][0]+b[0][0], a[0][1]+b[0][1], a[0][2]+b[0][2], a[0][3]+b[0][3], + a[1][0]+b[1][0], a[1][1]+b[1][1], a[1][2]+b[1][2], a[1][3]+b[1][3], + a[2][0]+b[2][0], a[2][1]+b[2][1], a[2][2]+b[2][2], a[2][3]+b[2][3], + a[3][0]+b[3][0], a[3][1]+b[3][1], a[3][2]+b[3][2], a[3][3]+b[3][3]); } matrix mx_add(matrix a, float b) { - return matrix (a[0][0]+b, a[0][1]+b, a[0][2]+b, a[0][3]+b, - a[1][0]+b, a[1][1]+b, a[1][2]+b, a[1][3]+b, - a[2][0]+b, a[2][1]+b, a[2][2]+b, a[2][3]+b, - a[3][0]+b, a[3][1]+b, a[3][2]+b, a[3][3]+b); + return matrix( + a[0][0]+b, a[0][1]+b, a[0][2]+b, a[0][3]+b, + a[1][0]+b, a[1][1]+b, a[1][2]+b, a[1][3]+b, + a[2][0]+b, a[2][1]+b, a[2][2]+b, a[2][3]+b, + a[3][0]+b, a[3][1]+b, a[3][2]+b, a[3][3]+b); } @@ -98,46 +101,45 @@ color mx_sub (color a, float b) { return a-b; } color4 mx_sub (color4 a, color4 b) { return a-b; } color4 mx_sub (color4 a, float b) { return a-b; } -matrix33 mx_sub (matrix33 a, matrix33 b) +matrix33 mx_sub(matrix33 a, matrix33 b) { - return matrix33 (matrix( + return matrix33(matrix( a.m[0][0]-b.m[0][0], a.m[0][1]-b.m[0][1], a.m[0][2]-b.m[0][2], 0.0, a.m[1][0]-b.m[1][0], a.m[1][1]-b.m[1][1], a.m[1][2]-b.m[1][2], 0.0, a.m[2][0]-b.m[2][0], a.m[2][1]-b.m[2][1], a.m[2][2]-b.m[2][2], 0.0, 0.0, 0.0, 0.0, 1.0)); } -matrix33 mx_sub (matrix33 a, float b) +matrix33 mx_sub(matrix33 a, float b) { - return matrix33 (matrix( + return matrix33(matrix( a.m[0][0]-b, a.m[0][1]-b, a.m[0][2]-b, 0.0, a.m[1][0]-b, a.m[1][1]-b, a.m[1][2]-b, 0.0, a.m[2][0]-b, a.m[2][1]-b, a.m[2][2]-b, 0.0, 0.0, 0.0, 0.0, 1.0)); } -matrix mx_sub (matrix a, matrix b) +matrix mx_sub(matrix a, matrix b) { - return matrix(a[0][0]-b[0][0], a[0][1]-b[0][1], a[0][2]-b[0][2], a[0][3]-b[0][3], - a[1][0]-b[1][0], a[1][1]-b[1][1], a[1][2]-b[1][2], a[1][3]-b[1][3], - a[2][0]-b[2][0], a[2][1]-b[2][1], a[2][2]-b[2][2], a[2][3]-b[2][3], - a[3][0]-b[3][0], a[3][1]-b[3][1], a[3][2]-b[3][2], a[3][3]-b[3][3]); + return matrix( + a[0][0]-b[0][0], a[0][1]-b[0][1], a[0][2]-b[0][2], a[0][3]-b[0][3], + a[1][0]-b[1][0], a[1][1]-b[1][1], a[1][2]-b[1][2], a[1][3]-b[1][3], + a[2][0]-b[2][0], a[2][1]-b[2][1], a[2][2]-b[2][2], a[2][3]-b[2][3], + a[3][0]-b[3][0], a[3][1]-b[3][1], a[3][2]-b[3][2], a[3][3]-b[3][3]); } -matrix mx_sub (matrix a, float b) +matrix mx_sub(matrix a, float b) { - return matrix (a[0][0]-b, a[0][1]-b, a[0][2]-b, a[0][3]-b, - a[1][0]-b, a[1][1]-b, a[1][2]-b, a[1][3]-b, - a[2][0]-b, a[2][1]-b, a[2][2]-b, a[2][3]-b, - a[3][0]-b, a[3][1]-b, a[3][2]-b, a[3][3]-b); + return matrix( + a[0][0]-b, a[0][1]-b, a[0][2]-b, a[0][3]-b, + a[1][0]-b, a[1][1]-b, a[1][2]-b, a[1][3]-b, + a[2][0]-b, a[2][1]-b, a[2][2]-b, a[2][3]-b, + a[3][0]-b, a[3][1]-b, a[3][2]-b, a[3][3]-b); } - -// remap `in` from [inLow, inHigh] to [outLow, outHigh], optionally clamping -// to the new range. -// -float remap(float in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) +// remap `in` from [inLow, inHigh] to [outLow, outHigh], optionally clamping to the new range. +float mx_remap(float in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) { float x = (in - inLow)/(inHigh-inLow); if (doClamp == 1) { @@ -146,7 +148,7 @@ float remap(float in, float inLow, float inHigh, float outLow, float outHigh, in return outLow + (outHigh - outLow) * x; } -color remap(color in, color inLow, color inHigh, color outLow, color outHigh, int doClamp) +color mx_remap(color in, color inLow, color inHigh, color outLow, color outHigh, int doClamp) { color x = (in - inLow) / (inHigh - inLow); if (doClamp == 1) { @@ -155,7 +157,7 @@ color remap(color in, color inLow, color inHigh, color outLow, color outHigh, in return outLow + (outHigh - outLow) * x; } -color remap(color in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) +color mx_remap(color in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) { color x = (in - inLow) / (inHigh - inLow); if (doClamp == 1) { @@ -164,103 +166,49 @@ color remap(color in, float inLow, float inHigh, float outLow, float outHigh, in return outLow + (outHigh - outLow) * x; } -color4 remap(color4 c, color4 inLow, color4 inHigh, color4 outLow, color4 outHigh, int doClamp) +color4 mx_remap(color4 c, color4 inLow, color4 inHigh, color4 outLow, color4 outHigh, int doClamp) { - return color4(remap(c.rgb, inLow.rgb, inHigh.rgb, outLow.rgb, outHigh.rgb, doClamp), - remap(c.a, inLow.a, inHigh.a, outLow.a, outHigh.a, doClamp)); + return color4(mx_remap(c.rgb, inLow.rgb, inHigh.rgb, outLow.rgb, outHigh.rgb, doClamp), + mx_remap(c.a, inLow.a, inHigh.a, outLow.a, outHigh.a, doClamp)); } -color4 remap(color4 c, float inLow, float inHigh, float outLow, float outHigh, int doClamp) +color4 mx_remap(color4 c, float inLow, float inHigh, float outLow, float outHigh, int doClamp) { color4 c4_inLow = color4(color(inLow), inLow); color4 c4_inHigh = color4(color(inHigh), inHigh); color4 c4_outLow = color4(color(outLow), outLow); color4 c4_outHigh = color4(color(outHigh), outHigh); - return remap(c, c4_inLow, c4_inHigh, c4_outLow, c4_outHigh, doClamp); -} - -vector2 remap(vector2 in, vector2 inLow, vector2 inHigh, vector2 outLow, vector2 outHigh, int doClamp) -{ - return vector2 (remap(in.x, inLow.x, inHigh.x, outLow.x, outHigh.x, doClamp), - remap(in.y, inLow.y, inHigh.y, outLow.y, outHigh.y, doClamp)); -} - -vector2 remap(vector2 in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) -{ - return vector2 (remap(in.x, inLow, inHigh, outLow, outHigh, doClamp), - remap(in.y, inLow, inHigh, outLow, outHigh, doClamp)); -} - -vector4 remap(vector4 in, vector4 inLow, vector4 inHigh, vector4 outLow, vector4 outHigh, int doClamp) -{ - return vector4 (remap(in.x, inLow.x, inHigh.x, outLow.x, outHigh.x, doClamp), - remap(in.y, inLow.y, inHigh.y, outLow.y, outHigh.y, doClamp), - remap(in.z, inLow.z, inHigh.z, outLow.z, outHigh.z, doClamp), - remap(in.w, inLow.w, inHigh.w, outLow.w, outHigh.w, doClamp)); -} - -vector4 remap(vector4 in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) -{ - return vector4 (remap(in.x, inLow, inHigh, outLow, outHigh, doClamp), - remap(in.y, inLow, inHigh, outLow, outHigh, doClamp), - remap(in.z, inLow, inHigh, outLow, outHigh, doClamp), - remap(in.w, inLow, inHigh, outLow, outHigh, doClamp)); + return mx_remap(c, c4_inLow, c4_inHigh, c4_outLow, c4_outHigh, doClamp); } - - -float fgamma(float in, float g) +vector2 mx_remap(vector2 in, vector2 inLow, vector2 inHigh, vector2 outLow, vector2 outHigh, int doClamp) { - return sign(in) * pow(abs(in), g); + return vector2(mx_remap(in.x, inLow.x, inHigh.x, outLow.x, outHigh.x, doClamp), + mx_remap(in.y, inLow.y, inHigh.y, outLow.y, outHigh.y, doClamp)); } -color fgamma(color in, color g) +vector2 mx_remap(vector2 in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) { - return sign(in) * pow(abs(in), g); + return vector2(mx_remap(in.x, inLow, inHigh, outLow, outHigh, doClamp), + mx_remap(in.y, inLow, inHigh, outLow, outHigh, doClamp)); } -color fgamma(color in, float g) +vector4 mx_remap(vector4 in, vector4 inLow, vector4 inHigh, vector4 outLow, vector4 outHigh, int doClamp) { - return sign(in) * pow(abs(in), g); + return vector4(mx_remap(in.x, inLow.x, inHigh.x, outLow.x, outHigh.x, doClamp), + mx_remap(in.y, inLow.y, inHigh.y, outLow.y, outHigh.y, doClamp), + mx_remap(in.z, inLow.z, inHigh.z, outLow.z, outHigh.z, doClamp), + mx_remap(in.w, inLow.w, inHigh.w, outLow.w, outHigh.w, doClamp)); } -color4 fgamma(color4 a, color4 b) +vector4 mx_remap(vector4 in, float inLow, float inHigh, float outLow, float outHigh, int doClamp) { - return color4(fgamma(a.rgb, b.rgb), fgamma(a.a, b.a)); + return vector4(mx_remap(in.x, inLow, inHigh, outLow, outHigh, doClamp), + mx_remap(in.y, inLow, inHigh, outLow, outHigh, doClamp), + mx_remap(in.z, inLow, inHigh, outLow, outHigh, doClamp), + mx_remap(in.w, inLow, inHigh, outLow, outHigh, doClamp)); } -color4 fgamma(color4 a, float b) -{ - return fgamma(a, color4(color(b), b)); -} - -vector2 fgamma(vector2 in, vector2 g) -{ - return vector2 (fgamma(in.x, g.x), fgamma(in.y, g.y)); -} - -vector2 fgamma(vector2 in, float g) -{ - return vector2 (fgamma(in.x, g), fgamma(in.y, g)); -} - -vector4 fgamma(vector4 in, vector4 g) -{ - return vector4 (fgamma(in.x, g.x), - fgamma(in.y, g.y), - fgamma(in.z, g.z), - fgamma(in.w, g.w)); -} - -vector4 fgamma(vector4 in, float g) -{ - return vector4 (fgamma(in.x, g), - fgamma(in.y, g), - fgamma(in.z, g), - fgamma(in.w, g)); -} - - // // mx_contrast scales the input around a central `pivot` value. @@ -313,113 +261,66 @@ vector2 mx_contrast(vector2 in, float amount, float pivot) vector4 mx_contrast(vector4 in, vector4 amount, vector4 pivot) { - return vector4 (mx_contrast(in.x, amount.x, pivot.x), - mx_contrast(in.y, amount.y, pivot.y), - mx_contrast(in.z, amount.z, pivot.z), - mx_contrast(in.w, amount.w, pivot.w)); + return vector4(mx_contrast(in.x, amount.x, pivot.x), + mx_contrast(in.y, amount.y, pivot.y), + mx_contrast(in.z, amount.z, pivot.z), + mx_contrast(in.w, amount.w, pivot.w)); } vector4 mx_contrast(vector4 in, float amount, float pivot) { - return vector4 (mx_contrast(in.x, amount, pivot), - mx_contrast(in.y, amount, pivot), - mx_contrast(in.z, amount, pivot), - mx_contrast(in.w, amount, pivot)); + return vector4(mx_contrast(in.x, amount, pivot), + mx_contrast(in.y, amount, pivot), + mx_contrast(in.z, amount, pivot), + mx_contrast(in.w, amount, pivot)); } - -vector2 noise (string noisetype, float x, float y) +vector2 mx_noise(string noisetype, float x, float y) { - color cnoise = (color) noise (noisetype, x, y); + color cnoise = (color) noise(noisetype, x, y); return vector2 (cnoise[0], cnoise[1]); } -color4 noise (string noisetype, float x, float y) +color4 mx_noise(string noisetype, float x, float y) { - color cnoise = (color) noise (noisetype, x, y); - float fnoise = (float) noise (noisetype, x + 19, y + 73); + color cnoise = (color) noise(noisetype, x, y); + float fnoise = (float) noise(noisetype, x + 19, y + 73); return color4 (cnoise, fnoise); } -vector4 noise (string noisetype, float x, float y) +vector4 mx_noise(string noisetype, float x, float y) { - color cnoise = (color) noise (noisetype, x, y); - float fnoise = (float) noise (noisetype, x + 19, y + 73); + color cnoise = (color) noise(noisetype, x, y); + float fnoise = (float) noise(noisetype, x + 19, y + 73); return vector4 (cnoise[0], cnoise[1], cnoise[2], fnoise); } - -vector2 noise (string noisetype, point position) +vector2 mx_noise(string noisetype, point position) { - color cnoise = (color) noise (noisetype, position); + color cnoise = (color) noise(noisetype, position); return vector2 (cnoise[0], cnoise[1]); } -color4 noise (string noisetype, point position) +color4 mx_noise(string noisetype, point position) { - color cnoise = (color) noise (noisetype, position); - float fnoise = (float) noise (noisetype, position+vector(19,73,29)); + color cnoise = (color) noise(noisetype, position); + float fnoise = (float) noise(noisetype, position+vector(19,73,29)); return color4 (cnoise, fnoise); } -vector4 noise (string noisetype, point position) +vector4 mx_noise(string noisetype, point position) { - color cnoise = (color) noise (noisetype, position); - float fnoise = (float) noise (noisetype, position+vector(19,73,29)); + color cnoise = (color) noise(noisetype, position); + float fnoise = (float) noise(noisetype, position+vector(19,73,29)); return vector4 (cnoise[0], cnoise[1], cnoise[2], fnoise); } - -vector2 cellnoise (float x, float y) -{ - color cnoise = (color) cellnoise (x, y); - return vector2 (cnoise[0], cnoise[1]); -} - -color4 cellnoise (float x, float y) -{ - color cnoise = (color) cellnoise (x, y); - float fnoise = (float) cellnoise (x + 19, y + 73); - return color4 (cnoise, fnoise); -} - -vector4 cellnoise (float x, float y) -{ - color cnoise = (color) cellnoise (x, y); - float fnoise = (float) cellnoise (x + 19, y + 73); - return vector4 (cnoise[0], cnoise[1], cnoise[2], fnoise); -} - - - -vector2 cellnoise (point position) -{ - color cnoise = (color) cellnoise (position); - return vector2 (cnoise[0], cnoise[1]); -} - -color4 cellnoise (point position) -{ - color cnoise = (color) cellnoise (position); - float fnoise = (float) cellnoise (position+vector(19,73,29)); - return color4 (cnoise, fnoise); -} - -vector4 cellnoise (point position) -{ - color cnoise = (color) cellnoise (position); - float fnoise = (float) cellnoise (position+vector(19,73,29)); - return vector4 (cnoise[0], cnoise[1], cnoise[2], fnoise); -} - - - // // fractional Brownian motion // -float fBm( point position, int octaves, float lacunarity, float diminish, string noisetype) +float mx_fbm(point position, int octaves, float lacunarity, float diminish, string noisetype) { float out = 0; float amp = 1.0; @@ -433,7 +334,7 @@ float fBm( point position, int octaves, float lacunarity, float diminish, string return out; } -color fBm( point position, int octaves, float lacunarity, float diminish, string noisetype) +color mx_fbm(point position, int octaves, float lacunarity, float diminish, string noisetype) { color out = 0; float amp = 1.0; @@ -447,23 +348,23 @@ color fBm( point position, int octaves, float lacunarity, float diminish, string return out; } -vector2 fBm( point position, int octaves, float lacunarity, float diminish, string noisetype) +vector2 mx_fbm(point position, int octaves, float lacunarity, float diminish, string noisetype) { - return vector2 ((float) fBm (position, octaves, lacunarity, diminish, noisetype), - (float) fBm (position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype)); + return vector2((float) mx_fbm(position, octaves, lacunarity, diminish, noisetype), + (float) mx_fbm(position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype)); } -color4 fBm( point position, int octaves, float lacunarity, float diminish, string noisetype) +color4 mx_fbm(point position, int octaves, float lacunarity, float diminish, string noisetype) { - color c = (color) fBm (position, octaves, lacunarity, diminish, noisetype); - float f = (float) fBm (position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype); + color c = (color) mx_fbm(position, octaves, lacunarity, diminish, noisetype); + float f = (float) mx_fbm(position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype); return color4 (c, f); } -vector4 fBm( point position, int octaves, float lacunarity, float diminish, string noisetype) +vector4 mx_fbm(point position, int octaves, float lacunarity, float diminish, string noisetype) { - color c = (color) fBm (position, octaves, lacunarity, diminish, noisetype); - float f = (float) fBm (position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype); + color c = (color) mx_fbm(position, octaves, lacunarity, diminish, noisetype); + float f = (float) mx_fbm(position+point(19, 193, 17), octaves, lacunarity, diminish, noisetype); return vector4 (c[0], c[1], c[2], f); } @@ -675,346 +576,7 @@ vector mx_worley_noise_vector3(vector p, float jitter, int metric) } -float swizzle_float (float in[4], string channels) -{ - float out; - float outF[4]; - int c_len = strlen(channels); - - for (int i=0; i