upscale.glsl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. uniform sampler2D tex;
  2. uniform int factor;
  3. uniform float width;
  4. uniform float height;
  5. #define CORRECTION 2.25
  6. #define SIZE_ADDITION 3
  7. vec4 get_texture_at_position(vec2 position) {
  8. vec2 raw_position = position + vec2(CORRECTION, CORRECTION);
  9. vec2 raw_uv = raw_position / vec2(width + SIZE_ADDITION, height + SIZE_ADDITION);
  10. return texture2D(tex, raw_uv);
  11. }
  12. ivec2 get_corrected_position() {
  13. vec2 raw_uv = cogl_tex_coord0_in.st;
  14. vec2 raw_position = raw_uv * vec2(width + SIZE_ADDITION, height + SIZE_ADDITION);
  15. return ivec2(raw_position - vec2(CORRECTION, CORRECTION));
  16. }
  17. void main() {
  18. ivec2 corrected_position = get_corrected_position();
  19. vec2 adjusted_position = corrected_position / factor;
  20. cogl_color_out = get_texture_at_position(adjusted_position);
  21. // round
  22. if (distance(corrected_position, (floor(adjusted_position) + 0.5) * factor) < factor / 2.5) {
  23. //cogl_color_out = get_texture_at_position(adjusted_position);
  24. } else {
  25. //cogl_color_out = vec4(0, 0, 0, 1);
  26. }
  27. // square
  28. if (mod(corrected_position.x, factor) >= 2 && mod(corrected_position.y, factor) >= 2) {
  29. //cogl_color_out = get_texture_at_position(adjusted_position);
  30. } else {
  31. //cogl_color_out = vec4(0, 0, 0, 1);
  32. }
  33. // local mix
  34. vec4 color = vec4(0);
  35. int count = 0;
  36. for (int i = -1; i <= 1; i++) {
  37. for (int j = -1; j <= 1; j++) {
  38. vec2 lookup_position = adjusted_position + vec2(i, j);
  39. if (all(greaterThanEqual(lookup_position, vec2(0, 0))) &&
  40. all(lessThan(lookup_position, vec2(width, height) / factor))) {
  41. color += get_texture_at_position(lookup_position);
  42. count += 1;
  43. }
  44. }
  45. }
  46. //cogl_color_out = color / count;
  47. }