1 2uniform sampler2D colorBuffer; 3uniform sampler2D depthBuffer; 4 5uniform int bgType; 6uniform vec4 colorOverride; 7 8in vec4 uvcoordsvar; 9 10out vec4 fragColor; 11 12#define BG_SOLID 0 13#define BG_GRADIENT 1 14#define BG_CHECKER 2 15#define BG_RADIAL 3 16#define BG_SOLID_CHECKER 4 17#define SQRT2 1.4142135623730950488 18 19/* 4x4 bayer matrix prepared for 8bit UNORM precision error. */ 20#define P(x) (((x + 0.5) * (1.0 / 16.0) - 0.5) * (1.0 / 255.0)) 21const vec4 dither_mat4x4[4] = vec4[4](vec4(P(0.0), P(8.0), P(2.0), P(10.0)), 22 vec4(P(12.0), P(4.0), P(14.0), P(6.0)), 23 vec4(P(3.0), P(11.0), P(1.0), P(9.0)), 24 vec4(P(15.0), P(7.0), P(13.0), P(5.0))); 25 26float dither(void) 27{ 28 ivec2 co = ivec2(gl_FragCoord.xy) % 4; 29 return dither_mat4x4[co.x][co.y]; 30} 31 32void main() 33{ 34 /* The blend equation is: 35 * resutl.rgb = SRC.rgb * (1 - DST.a) + DST.rgb * (SRC.a) 36 * result.a = SRC.a * 0 + DST.a * SRC.a 37 * This removes the alpha channel and put the background behind reference images 38 * while masking the reference images by the render alpha. 39 */ 40 float alpha = texture(colorBuffer, uvcoordsvar.st).a; 41 float depth = texture(depthBuffer, uvcoordsvar.st).r; 42 43 vec3 bg_col; 44 vec3 col_high; 45 vec3 col_low; 46 47 /* BG_SOLID_CHECKER selects BG_SOLID when no pixel has been drawn otherwise use the BG_CHERKER. 48 */ 49 int bg_type = bgType == BG_SOLID_CHECKER ? (depth == 1.0 ? BG_SOLID : BG_CHECKER) : bgType; 50 51 switch (bg_type) { 52 case BG_SOLID: 53 bg_col = colorBackground.rgb; 54 break; 55 case BG_GRADIENT: 56 /* XXX do interpolation in a non-linear space to have a better visual result. */ 57 col_high = pow(colorBackground.rgb, vec3(1.0 / 2.2)); 58 col_low = pow(colorBackgroundGradient.rgb, vec3(1.0 / 2.2)); 59 bg_col = mix(col_low, col_high, uvcoordsvar.t); 60 /* Convert back to linear. */ 61 bg_col = pow(bg_col, vec3(2.2)); 62 /* Dither to hide low precision buffer. (Could be improved) */ 63 bg_col += dither(); 64 break; 65 case BG_RADIAL: 66 /* Do interpolation in a non-linear space to have a better visual result. */ 67 col_high = pow(colorBackground.rgb, vec3(1.0 / 2.2)); 68 col_low = pow(colorBackgroundGradient.rgb, vec3(1.0 / 2.2)); 69 70 vec2 uv_n = uvcoordsvar.xy - 0.5; 71 bg_col = mix(col_high, col_low, length(uv_n) * SQRT2); 72 73 /* Convert back to linear. */ 74 bg_col = pow(bg_col, vec3(2.2)); 75 /* Dither to hide low precision buffer. (Could be improved) */ 76 bg_col += dither(); 77 break; 78 case BG_CHECKER: 79 float size = sizeChecker * sizePixel; 80 ivec2 p = ivec2(floor(gl_FragCoord.xy / size)); 81 bool check = mod(p.x, 2) == mod(p.y, 2); 82 bg_col = (check) ? colorCheckerPrimary.rgb : colorCheckerSecondary.rgb; 83 break; 84 } 85 86 bg_col = mix(bg_col, colorOverride.rgb, colorOverride.a); 87 88 /* Mimic alpha under behavior. Result is premultiplied. */ 89 fragColor = vec4(bg_col, 1.0) * (1.0 - alpha); 90 91 /* Special case: If the render is not transparent, do not clear alpha values. */ 92 if (depth == 1.0 && alpha == 1.0) { 93 fragColor.a = 1.0; 94 } 95} 96