Factor out common functions used in tile alpha shaders

This commit is contained in:
Patrick Walton 2020-02-24 19:42:32 -08:00
parent 5421525eaa
commit c96cb62f47
8 changed files with 176 additions and 80 deletions

View File

@ -12,22 +12,54 @@
#extension GL_GOOGLE_include_directive : enable
precision highp float; precision highp float;
out vec4 oFragColor;
uniform sampler2D uStencilTexture; uniform sampler2D uStencilTexture;
uniform sampler2D uPaintTexture; uniform sampler2D uPaintTexture;
uniform sampler2D uDest;
uniform vec2 uFramebufferSize; uniform vec2 uFramebufferSize;
in vec2 vColorTexCoord; in vec2 vColorTexCoord;
in vec2 vMaskTexCoord; in vec2 vMaskTexCoord;
out vec4 oFragColor;
void main(){ vec4 sampleSrcColor(){
float coverage = texture(uStencilTexture, vMaskTexCoord). r; float coverage = texture(uStencilTexture, vMaskTexCoord). r;
vec4 color = texture(uPaintTexture, vColorTexCoord); vec4 srcRGBA = texture(uPaintTexture, vColorTexCoord);
color . a *= coverage; return vec4(srcRGBA . rgb, srcRGBA . a * coverage);
color . rgb *= color . a; }
oFragColor = color;
vec4 sampleDestColor(){
vec2 destTexCoord = gl_FragCoord . xy / uFramebufferSize;
return texture(uDest, destTexCoord);
}
vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB){
return vec4(srcRGBA . a *(1.0 - destRGBA . a)* srcRGBA . rgb +
srcRGBA . a * destRGBA . a * blendedRGB +
(1.0 - srcRGBA . a)* destRGBA . a * destRGBA . rgb,
1.0);
}
void main(){
vec4 srcRGBA = sampleSrcColor();
oFragColor = vec4(srcRGBA . rgb * srcRGBA . a, srcRGBA . a);
} }

View File

@ -14,21 +14,54 @@
#extension GL_GOOGLE_include_directive : enable #extension GL_GOOGLE_include_directive : enable
precision highp float; precision highp float;
uniform ivec3 uBlendHSL;
out vec4 oFragColor;
uniform sampler2D uStencilTexture; uniform sampler2D uStencilTexture;
uniform sampler2D uPaintTexture; uniform sampler2D uPaintTexture;
uniform sampler2D uDest; uniform sampler2D uDest;
uniform ivec3 uBlendHSL;
uniform vec2 uFramebufferSize; uniform vec2 uFramebufferSize;
in vec2 vColorTexCoord; in vec2 vColorTexCoord;
in vec2 vMaskTexCoord; in vec2 vMaskTexCoord;
out vec4 oFragColor;
vec4 sampleSrcColor(){
float coverage = texture(uStencilTexture, vMaskTexCoord). r;
vec4 srcRGBA = texture(uPaintTexture, vColorTexCoord);
return vec4(srcRGBA . rgb, srcRGBA . a * coverage);
}
vec4 sampleDestColor(){
vec2 destTexCoord = gl_FragCoord . xy / uFramebufferSize;
return texture(uDest, destTexCoord);
}
vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB){
return vec4(srcRGBA . a *(1.0 - destRGBA . a)* srcRGBA . rgb +
srcRGBA . a * destRGBA . a * blendedRGB +
(1.0 - srcRGBA . a)* destRGBA . a * destRGBA . rgb,
1.0);
}
@ -65,12 +98,8 @@ vec3 convertRGBToHSL(vec3 rgb){
} }
void main(){ void main(){
float coverage = texture(uStencilTexture, vMaskTexCoord). r; vec4 srcRGBA = sampleSrcColor();
vec4 srcRGBA = texture(uPaintTexture, vColorTexCoord); vec4 destRGBA = sampleDestColor();
srcRGBA . a *= coverage;
vec2 destTexCoord = gl_FragCoord . xy / uFramebufferSize;
vec4 destRGBA = texture(uDest, destTexCoord);
vec3 destHSL = convertRGBToHSL(destRGBA . rgb); vec3 destHSL = convertRGBToHSL(destRGBA . rgb);
vec3 srcHSL = convertRGBToHSL(srcRGBA . rgb); vec3 srcHSL = convertRGBToHSL(srcRGBA . rgb);
@ -80,11 +109,6 @@ void main(){
blendDest . z ? destHSL . z : srcHSL . z); blendDest . z ? destHSL . z : srcHSL . z);
vec3 blendedRGB = convertHSLToRGB(blendedHSL); vec3 blendedRGB = convertHSLToRGB(blendedHSL);
oFragColor = blendColors(destRGBA, srcRGBA, blendedRGB);
vec4 color = vec4(srcRGBA . a *(1.0 - destRGBA . a)* srcRGBA . rgb +
srcRGBA . a * destRGBA . a * blendedRGB +
(1.0 - srcRGBA . a)* destRGBA . a * destRGBA . rgb,
1.0);
oFragColor = color;
} }

View File

@ -1,4 +1,6 @@
// Automatically generated from files in pathfinder/shaders/. Do not edit! // Automatically generated from files in pathfinder/shaders/. Do not edit!
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#include <metal_stdlib> #include <metal_stdlib>
#include <simd/simd.h> #include <simd/simd.h>
@ -23,15 +25,18 @@ struct main0_in
float2 vMaskTexCoord [[user(locn1)]]; float2 vMaskTexCoord [[user(locn1)]];
}; };
float4 sampleSrcColor(thread texture2d<float> uStencilTexture, thread const sampler uStencilTextureSmplr, thread float2& vMaskTexCoord, thread texture2d<float> uPaintTexture, thread const sampler uPaintTextureSmplr, thread float2& vColorTexCoord)
{
float coverage = uStencilTexture.sample(uStencilTextureSmplr, vMaskTexCoord).x;
float4 srcRGBA = uPaintTexture.sample(uPaintTextureSmplr, vColorTexCoord);
return float4(srcRGBA.xyz, srcRGBA.w * coverage);
}
fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]]) fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]])
{ {
main0_out out = {}; main0_out out = {};
float coverage = spvDescriptorSet0.uStencilTexture.sample(spvDescriptorSet0.uStencilTextureSmplr, in.vMaskTexCoord).x; float4 srcRGBA = sampleSrcColor(spvDescriptorSet0.uStencilTexture, spvDescriptorSet0.uStencilTextureSmplr, in.vMaskTexCoord, spvDescriptorSet0.uPaintTexture, spvDescriptorSet0.uPaintTextureSmplr, in.vColorTexCoord);
float4 color = spvDescriptorSet0.uPaintTexture.sample(spvDescriptorSet0.uPaintTextureSmplr, in.vColorTexCoord); out.oFragColor = float4(srcRGBA.xyz * srcRGBA.w, srcRGBA.w);
color.w *= coverage;
float3 _41 = color.xyz * color.w;
color = float4(_41.x, _41.y, _41.z, color.w);
out.oFragColor = color;
return out; return out;
} }

View File

@ -36,6 +36,19 @@ Tx mod(Tx x, Ty y)
return x - y * floor(x / y); return x - y * floor(x / y);
} }
float4 sampleSrcColor(thread texture2d<float> uStencilTexture, thread const sampler uStencilTextureSmplr, thread float2& vMaskTexCoord, thread texture2d<float> uPaintTexture, thread const sampler uPaintTextureSmplr, thread float2& vColorTexCoord)
{
float coverage = uStencilTexture.sample(uStencilTextureSmplr, vMaskTexCoord).x;
float4 srcRGBA = uPaintTexture.sample(uPaintTextureSmplr, vColorTexCoord);
return float4(srcRGBA.xyz, srcRGBA.w * coverage);
}
float4 sampleDestColor(thread float4& gl_FragCoord, thread float2 uFramebufferSize, thread texture2d<float> uDest, thread const sampler uDestSmplr)
{
float2 destTexCoord = gl_FragCoord.xy / uFramebufferSize;
return uDest.sample(uDestSmplr, destTexCoord);
}
float3 convertRGBToHSL(thread const float3& rgb) float3 convertRGBToHSL(thread const float3& rgb)
{ {
float v = fast::max(rgb.y, rgb.z); float v = fast::max(rgb.y, rgb.z);
@ -77,51 +90,55 @@ float3 convertHSLToRGB(thread const float3& hsl)
return hsl.zzz - (fast::clamp(fast::min(ks - float3(3.0), float3(9.0) - ks), float3(-1.0), float3(1.0)) * a); return hsl.zzz - (fast::clamp(fast::min(ks - float3(3.0), float3(9.0) - ks), float3(-1.0), float3(1.0)) * a);
} }
float4 blendColors(thread const float4& destRGBA, thread const float4& srcRGBA, thread const float3& blendedRGB)
{
return float4(((srcRGBA.xyz * (srcRGBA.w * (1.0 - destRGBA.w))) + (blendedRGB * (srcRGBA.w * destRGBA.w))) + (destRGBA.xyz * ((1.0 - srcRGBA.w) * destRGBA.w)), 1.0);
}
fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], float4 gl_FragCoord [[position]]) fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], float4 gl_FragCoord [[position]])
{ {
main0_out out = {}; main0_out out = {};
float coverage = spvDescriptorSet0.uStencilTexture.sample(spvDescriptorSet0.uStencilTextureSmplr, in.vMaskTexCoord).x; float4 srcRGBA = sampleSrcColor(spvDescriptorSet0.uStencilTexture, spvDescriptorSet0.uStencilTextureSmplr, in.vMaskTexCoord, spvDescriptorSet0.uPaintTexture, spvDescriptorSet0.uPaintTextureSmplr, in.vColorTexCoord);
float4 srcRGBA = spvDescriptorSet0.uPaintTexture.sample(spvDescriptorSet0.uPaintTextureSmplr, in.vColorTexCoord); float4 destRGBA = sampleDestColor(gl_FragCoord, (*spvDescriptorSet0.uFramebufferSize), spvDescriptorSet0.uDest, spvDescriptorSet0.uDestSmplr);
srcRGBA.w *= coverage;
float2 destTexCoord = gl_FragCoord.xy / (*spvDescriptorSet0.uFramebufferSize);
float4 destRGBA = spvDescriptorSet0.uDest.sample(spvDescriptorSet0.uDestSmplr, destTexCoord);
float3 param = destRGBA.xyz; float3 param = destRGBA.xyz;
float3 destHSL = convertRGBToHSL(param); float3 destHSL = convertRGBToHSL(param);
float3 param_1 = srcRGBA.xyz; float3 param_1 = srcRGBA.xyz;
float3 srcHSL = convertRGBToHSL(param_1); float3 srcHSL = convertRGBToHSL(param_1);
bool3 blendDest = (*spvDescriptorSet0.uBlendHSL) == int3(0); bool3 blendDest = (*spvDescriptorSet0.uBlendHSL) == int3(0);
float _225; float _281;
if (blendDest.x) if (blendDest.x)
{ {
_225 = destHSL.x; _281 = destHSL.x;
} }
else else
{ {
_225 = srcHSL.x; _281 = srcHSL.x;
} }
float _236; float _292;
if (blendDest.y) if (blendDest.y)
{ {
_236 = destHSL.y; _292 = destHSL.y;
} }
else else
{ {
_236 = srcHSL.y; _292 = srcHSL.y;
} }
float _247; float _303;
if (blendDest.z) if (blendDest.z)
{ {
_247 = destHSL.z; _303 = destHSL.z;
} }
else else
{ {
_247 = srcHSL.z; _303 = srcHSL.z;
} }
float3 blendedHSL = float3(_225, _236, _247); float3 blendedHSL = float3(_281, _292, _303);
float3 param_2 = blendedHSL; float3 param_2 = blendedHSL;
float3 blendedRGB = convertHSLToRGB(param_2); float3 blendedRGB = convertHSLToRGB(param_2);
float4 color = float4(((srcRGBA.xyz * (srcRGBA.w * (1.0 - destRGBA.w))) + (blendedRGB * (srcRGBA.w * destRGBA.w))) + (destRGBA.xyz * ((1.0 - srcRGBA.w) * destRGBA.w)), 1.0); float4 param_3 = destRGBA;
out.oFragColor = color; float4 param_4 = srcRGBA;
float3 param_5 = blendedRGB;
out.oFragColor = blendColors(param_3, param_4, param_5);
return out; return out;
} }

View File

@ -33,6 +33,7 @@ SHADERS=\
INCLUDES=\ INCLUDES=\
filter_text_convolve.inc.glsl \ filter_text_convolve.inc.glsl \
filter_text_gamma_correct.inc.glsl \ filter_text_gamma_correct.inc.glsl \
tile_alpha_sample.inc.glsl \
$(EMPTY) $(EMPTY)
OUT=\ OUT=\

View File

@ -10,21 +10,15 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#extension GL_GOOGLE_include_directive : enable
precision highp float; precision highp float;
uniform sampler2D uStencilTexture;
uniform sampler2D uPaintTexture;
uniform vec2 uFramebufferSize;
in vec2 vColorTexCoord;
in vec2 vMaskTexCoord;
out vec4 oFragColor; out vec4 oFragColor;
#include "tile_alpha_sample.inc.glsl"
void main() { void main() {
float coverage = texture(uStencilTexture, vMaskTexCoord).r; vec4 srcRGBA = sampleSrcColor();
vec4 color = texture(uPaintTexture, vColorTexCoord); oFragColor = vec4(srcRGBA.rgb * srcRGBA.a, srcRGBA.a);
color.a *= coverage;
color.rgb *= color.a;
oFragColor = color;
} }

View File

@ -12,26 +12,21 @@
#extension GL_GOOGLE_include_directive : enable #extension GL_GOOGLE_include_directive : enable
#define BLEND_TERM_DEST 0
#define BLEND_TERM_SRC 1
precision highp float; precision highp float;
uniform sampler2D uStencilTexture;
uniform sampler2D uPaintTexture;
uniform sampler2D uDest;
uniform ivec3 uBlendHSL; uniform ivec3 uBlendHSL;
uniform vec2 uFramebufferSize;
in vec2 vColorTexCoord;
in vec2 vMaskTexCoord;
out vec4 oFragColor; out vec4 oFragColor;
#include "tile_alpha_sample.inc.glsl"
#define PI_2 6.283185307179586 #define PI_2 6.283185307179586
#define DEG_30_INV 1.9098593171027443 #define DEG_30_INV 1.9098593171027443
#define DEG_60 1.0471975511965976 #define DEG_60 1.0471975511965976
#define BLEND_TERM_DEST 0
#define BLEND_TERM_SRC 1
// https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative // https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
vec3 convertHSLToRGB(vec3 hsl) { vec3 convertHSLToRGB(vec3 hsl) {
float a = hsl.y * min(hsl.z, 1.0 - hsl.z); float a = hsl.y * min(hsl.z, 1.0 - hsl.z);
@ -63,12 +58,8 @@ vec3 convertRGBToHSL(vec3 rgb) {
} }
void main() { void main() {
float coverage = texture(uStencilTexture, vMaskTexCoord).r; vec4 srcRGBA = sampleSrcColor();
vec4 srcRGBA = texture(uPaintTexture, vColorTexCoord); vec4 destRGBA = sampleDestColor();
srcRGBA.a *= coverage;
vec2 destTexCoord = gl_FragCoord.xy / uFramebufferSize;
vec4 destRGBA = texture(uDest, destTexCoord);
vec3 destHSL = convertRGBToHSL(destRGBA.rgb); vec3 destHSL = convertRGBToHSL(destRGBA.rgb);
vec3 srcHSL = convertRGBToHSL(srcRGBA.rgb); vec3 srcHSL = convertRGBToHSL(srcRGBA.rgb);
@ -78,10 +69,5 @@ void main() {
blendDest.z ? destHSL.z : srcHSL.z); blendDest.z ? destHSL.z : srcHSL.z);
vec3 blendedRGB = convertHSLToRGB(blendedHSL); vec3 blendedRGB = convertHSLToRGB(blendedHSL);
// FIXME(pcwalton): What should the output alpha be here? oFragColor = blendColors(destRGBA, srcRGBA, blendedRGB);
vec4 color = vec4(srcRGBA.a * (1.0 - destRGBA.a) * srcRGBA.rgb +
srcRGBA.a * destRGBA.a * blendedRGB +
(1.0 - srcRGBA.a) * destRGBA.a * destRGBA.rgb,
1.0);
oFragColor = color;
} }

View File

@ -0,0 +1,37 @@
// pathfinder/shaders/tile_alpha_sample.inc.glsl
//
// Copyright © 2020 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
uniform sampler2D uStencilTexture;
uniform sampler2D uPaintTexture;
uniform sampler2D uDest;
uniform vec2 uFramebufferSize;
in vec2 vColorTexCoord;
in vec2 vMaskTexCoord;
// NB: This does not premultiply.
vec4 sampleSrcColor() {
float coverage = texture(uStencilTexture, vMaskTexCoord).r;
vec4 srcRGBA = texture(uPaintTexture, vColorTexCoord);
return vec4(srcRGBA.rgb, srcRGBA.a * coverage);
}
vec4 sampleDestColor() {
vec2 destTexCoord = gl_FragCoord.xy / uFramebufferSize;
return texture(uDest, destTexCoord);
}
// FIXME(pcwalton): What should the output alpha be here?
vec4 blendColors(vec4 destRGBA, vec4 srcRGBA, vec3 blendedRGB) {
return vec4(srcRGBA.a * (1.0 - destRGBA.a) * srcRGBA.rgb +
srcRGBA.a * destRGBA.a * blendedRGB +
(1.0 - srcRGBA.a) * destRGBA.a * destRGBA.rgb,
1.0);
}