[vert.glsl] #version 130 uniform mat4 u_zoom_settings; uniform vec4 u_texel_size; uniform vec4 u_screen_size; attribute vec2 in_TexCoord; attribute vec2 in_Position; out vec4 v_center_x, v_center_y, v_index_x, v_index_y; void main(void) { gl_Position = vec4(in_Position, 0.0, 1.0); /* Center point in [-.5,.5], apply zoom and translation * transformation, and go back to texture coordinates * in [0,1]. That's the ideal point we would like to * compute the value for. Then add or remove half the * size of a texel: the distance from this new point to * the final point will be our error. */ vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625); vec4 zoomscale = vec4(u_zoom_settings[0][2], u_zoom_settings[1][2], u_zoom_settings[2][2], u_zoom_settings[3][2]); vec4 zoomtx = vec4(u_zoom_settings[0][0], u_zoom_settings[1][0], u_zoom_settings[2][0], u_zoom_settings[3][0]); vec4 zoomty = vec4(u_zoom_settings[0][1], u_zoom_settings[1][1], u_zoom_settings[2][1], u_zoom_settings[3][1]); v_center_x = zoomscale * in_TexCoord.x + zoomtx + offsets.xyxy * u_texel_size.x; v_center_y = zoomscale * in_TexCoord.y - zoomty + offsets.xyyx * u_texel_size.y; /* Precompute the multiple of one texel where our ideal * point lies. The fragment shader will call floor() on * this value. We add or remove a slight offset to avoid * rounding issues at the image's edges. */ v_index_x = v_center_x * u_screen_size.z - offsets.zwzw; v_index_y = v_center_y * u_screen_size.w - offsets.zwwz; } [frag.glsl] #version 130 #if defined GL_ES precision highp float; #endif uniform vec4 u_texel_size; uniform sampler2D u_texture; in vec4 v_center_x, v_center_y, v_index_x, v_index_y; void main(void) { vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5); vec4 rx, ry, t0, dx, dy, dd; /* Get a pixel coordinate from each slice into rx & ry */ rx = u_texel_size.x + u_texel_size.z * floor(v_index_x); ry = u_texel_size.y + u_texel_size.w * floor(v_index_y); /* Compute inverse distance to expected pixel in dd, * and put zero if we fall outside the texture. */ t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05); dx = rx - v_center_x; dy = ry - v_center_y; #if 0 vec4 dd = t0 * (abs(dx) + abs(dy)); vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy))); #endif dd = t0 / (0.000001 + (dx * dx) + (dy * dy)); /* Modify Y coordinate to select proper quarter. */ ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75); #if 1 # if 0 /* XXX: disabled until we can autodetect i915 */ /* t1.x <-- dd.x > dd.y */ /* t1.y <-- dd.z > dd.w */ vec2 t1 = step(dd.xz, dd.yw); /* ret.x <-- max(rx.x, rx.y) wrt. t1.x */ /* ret.y <-- max(rx.z, rx.w) wrt. t1.y */ /* ret.z <-- max(ry.x, ry.y) wrt. t1.x */ /* ret.w <-- max(ry.z, ry.w) wrt. t1.y */ vec4 ret = mix(vec4(rx.xz, ry.xz), vec4(rx.yw, ry.yw), t1.xyxy); /* dd.x <-- max(dd.x, dd.y) */ /* dd.z <-- max(dd.z, dd.w) */ dd.xy = mix(dd.xz, dd.yw, t1); /* t2 <-- dd.x > dd.z */ float t2 = step(dd.x, dd.y); /* ret.x <-- max(ret.x, ret.y); */ /* ret.y <-- max(ret.z, ret.w); */ ret.xy = mix(ret.xz, ret.yw, t2); # else /* Fallback for i915 cards -- the trick to reduce the * number of operations is to compute both step(a,b) * and step(b,a) and hope that their sum is 1. This is * almost always the case, and when it isn't we can * afford to have a few wrong pixels. However, a real * problem is when panning the image, because half the * screen is likely to flicker. To avoid this problem, * we cheat a little (see m_translate comment above). */ vec4 t1 = step(dd.xzyw, dd.ywxz); vec4 ret = vec4(rx.xz, ry.xz) * t1.zwzw + vec4(rx.yw, ry.yw) * t1.xyxy; dd.xy = dd.xz * t1.zw + dd.yw * t1.xy; vec2 t2 = step(dd.xy, dd.yx); ret.xy = ret.xz * t2.yy + ret.yw * t2.xx; # endif /* Nearest neighbour */ gl_FragColor = texture2D(u_texture, ret.xy); #else /* Alternate version: some kind of linear interpolation */ vec4 p0 = texture2D(u_texture, vec2(rx.x, ry.x)); vec4 p1 = texture2D(u_texture, vec2(rx.y, ry.y)); vec4 p2 = texture2D(u_texture, vec2(rx.z, ry.z)); vec4 p3 = texture2D(u_texture, vec2(rx.w, ry.w)); gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w) * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3); #endif }