From 00b7a2ee5a66341676d0a4f8aab92616db81126c Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 24 Feb 2020 21:20:56 -0800 Subject: [PATCH] Factor out 3-element selection into a `select3()` function --- resources/shaders/gl3/tile_alpha.fs.glsl | 4 + resources/shaders/gl3/tile_alpha_hsl.fs.glsl | 9 ++- .../shaders/metal/tile_alpha_hsl.fs.metal | 77 ++++++++++--------- shaders/tile_alpha_hsl.fs.glsl | 5 +- shaders/tile_alpha_sample.inc.glsl | 4 + 5 files changed, 56 insertions(+), 43 deletions(-) diff --git a/resources/shaders/gl3/tile_alpha.fs.glsl b/resources/shaders/gl3/tile_alpha.fs.glsl index eafe14f2..1715cc27 100644 --- a/resources/shaders/gl3/tile_alpha.fs.glsl +++ b/resources/shaders/gl3/tile_alpha.fs.glsl @@ -57,6 +57,10 @@ vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB){ 1.0); } +vec3 select3(bvec3 cond, vec3 a, vec3 b){ + return vec3(cond . x ? a . x : b . x, cond . y ? a . y : b . y, cond . z ? a . z : b . z); +} + void main(){ vec4 srcRGBA = sampleSrcColor(); diff --git a/resources/shaders/gl3/tile_alpha_hsl.fs.glsl b/resources/shaders/gl3/tile_alpha_hsl.fs.glsl index 1502c033..fb794256 100644 --- a/resources/shaders/gl3/tile_alpha_hsl.fs.glsl +++ b/resources/shaders/gl3/tile_alpha_hsl.fs.glsl @@ -59,6 +59,10 @@ vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB){ 1.0); } +vec3 select3(bvec3 cond, vec3 a, vec3 b){ + return vec3(cond . x ? a . x : b . x, cond . y ? a . y : b . y, cond . z ? a . z : b . z); +} + @@ -103,10 +107,7 @@ void main(){ vec3 destHSL = convertRGBToHSL(destRGBA . rgb); vec3 srcHSL = convertRGBToHSL(srcRGBA . rgb); - bvec3 blendDest = equal(uBlendHSL, ivec3(0)); - vec3 blendedHSL = vec3(blendDest . x ? destHSL . x : srcHSL . x, - blendDest . y ? destHSL . y : srcHSL . y, - blendDest . z ? destHSL . z : srcHSL . z); + vec3 blendedHSL = select3(equal(uBlendHSL, ivec3(0)), destHSL, srcHSL); vec3 blendedRGB = convertHSLToRGB(blendedHSL); oFragColor = blendColors(destRGBA, srcRGBA, blendedRGB); diff --git a/resources/shaders/metal/tile_alpha_hsl.fs.metal b/resources/shaders/metal/tile_alpha_hsl.fs.metal index bdd73bf8..ff61ce4c 100644 --- a/resources/shaders/metal/tile_alpha_hsl.fs.metal +++ b/resources/shaders/metal/tile_alpha_hsl.fs.metal @@ -83,6 +83,38 @@ float3 convertRGBToHSL(thread const float3& rgb) return float3(h, s, l); } +float3 select3(thread const bool3& cond, thread const float3& a, thread const float3& b) +{ + float _125; + if (cond.x) + { + _125 = a.x; + } + else + { + _125 = b.x; + } + float _137; + if (cond.y) + { + _137 = a.y; + } + else + { + _137 = b.y; + } + float _149; + if (cond.z) + { + _149 = a.z; + } + else + { + _149 = b.z; + } + return float3(_125, _137, _149); +} + float3 convertHSLToRGB(thread const float3& hsl) { float a = hsl.y * fast::min(hsl.z, 1.0 - hsl.z); @@ -104,41 +136,16 @@ fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuff float3 destHSL = convertRGBToHSL(param); float3 param_1 = srcRGBA.xyz; float3 srcHSL = convertRGBToHSL(param_1); - bool3 blendDest = (*spvDescriptorSet0.uBlendHSL) == int3(0); - float _281; - if (blendDest.x) - { - _281 = destHSL.x; - } - else - { - _281 = srcHSL.x; - } - float _292; - if (blendDest.y) - { - _292 = destHSL.y; - } - else - { - _292 = srcHSL.y; - } - float _303; - if (blendDest.z) - { - _303 = destHSL.z; - } - else - { - _303 = srcHSL.z; - } - float3 blendedHSL = float3(_281, _292, _303); - float3 param_2 = blendedHSL; - float3 blendedRGB = convertHSLToRGB(param_2); - float4 param_3 = destRGBA; - float4 param_4 = srcRGBA; - float3 param_5 = blendedRGB; - out.oFragColor = blendColors(param_3, param_4, param_5); + bool3 param_2 = (*spvDescriptorSet0.uBlendHSL) == int3(0); + float3 param_3 = destHSL; + float3 param_4 = srcHSL; + float3 blendedHSL = select3(param_2, param_3, param_4); + float3 param_5 = blendedHSL; + float3 blendedRGB = convertHSLToRGB(param_5); + float4 param_6 = destRGBA; + float4 param_7 = srcRGBA; + float3 param_8 = blendedRGB; + out.oFragColor = blendColors(param_6, param_7, param_8); return out; } diff --git a/shaders/tile_alpha_hsl.fs.glsl b/shaders/tile_alpha_hsl.fs.glsl index 72f69206..7b74eebc 100644 --- a/shaders/tile_alpha_hsl.fs.glsl +++ b/shaders/tile_alpha_hsl.fs.glsl @@ -63,10 +63,7 @@ void main() { vec3 destHSL = convertRGBToHSL(destRGBA.rgb); vec3 srcHSL = convertRGBToHSL(srcRGBA.rgb); - bvec3 blendDest = equal(uBlendHSL, ivec3(BLEND_TERM_DEST)); - vec3 blendedHSL = vec3(blendDest.x ? destHSL.x : srcHSL.x, - blendDest.y ? destHSL.y : srcHSL.y, - blendDest.z ? destHSL.z : srcHSL.z); + vec3 blendedHSL = select3(equal(uBlendHSL, ivec3(BLEND_TERM_DEST)), destHSL, srcHSL); vec3 blendedRGB = convertHSLToRGB(blendedHSL); oFragColor = blendColors(destRGBA, srcRGBA, blendedRGB); diff --git a/shaders/tile_alpha_sample.inc.glsl b/shaders/tile_alpha_sample.inc.glsl index ae70ad71..820937ee 100644 --- a/shaders/tile_alpha_sample.inc.glsl +++ b/shaders/tile_alpha_sample.inc.glsl @@ -35,3 +35,7 @@ vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB) { (1.0 - srcRGBA.a) * destRGBA.a * destRGBA.rgb, 1.0); } + +vec3 select3(bvec3 cond, vec3 a, vec3 b) { + return vec3(cond.x ? a.x : b.x, cond.y ? a.y : b.y, cond.z ? a.z : b.z); +}