graphics: Move texel-region adjustment from shaders to program

This reduces GPU burden.
This commit is contained in:
Hajime Hoshi 2019-02-16 13:38:23 +09:00
parent 09ca873c41
commit b1b8d0b4b1
4 changed files with 25 additions and 20 deletions

View File

@ -81,10 +81,12 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty
wf := float32(width) wf := float32(width)
hf := float32(height) hf := float32(height)
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
return quadVerticesImpl(float32(sx1-sx0), float32(sy1-sy0), u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca) return quadVerticesImpl(wf, hf, float32(sx1-sx0), float32(sy1-sy0), u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca)
} }
func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) []float32 { const TexelAdjustmentFactor = 512.0
func quadVerticesImpl(sw, sh, x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) []float32 {
// Specifying a range explicitly here is redundant but this helps optimization // Specifying a range explicitly here is redundant but this helps optimization
// to eliminate boundary checks. // to eliminate boundary checks.
// //
@ -93,6 +95,9 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca f
ax, by, cx, dy := a*x, b*y, c*x, d*y ax, by, cx, dy := a*x, b*y, c*x, d*y
du := 1.0 / sw / TexelAdjustmentFactor
dv := 1.0 / sh / TexelAdjustmentFactor
// Vertex coordinates // Vertex coordinates
vs[0] = tx vs[0] = tx
vs[1] = ty vs[1] = ty
@ -104,8 +109,8 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca f
vs[3] = v0 vs[3] = v0
vs[4] = u0 vs[4] = u0
vs[5] = v0 vs[5] = v0
vs[6] = u1 vs[6] = u1 - du
vs[7] = v1 vs[7] = v1 - dv
vs[8] = cr vs[8] = cr
vs[9] = cg vs[9] = cg
vs[10] = cb vs[10] = cb
@ -118,8 +123,8 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca f
vs[15] = v0 vs[15] = v0
vs[16] = u0 vs[16] = u0
vs[17] = v0 vs[17] = v0
vs[18] = u1 vs[18] = u1 - du
vs[19] = v1 vs[19] = v1 - dv
vs[20] = cr vs[20] = cr
vs[21] = cg vs[21] = cg
vs[22] = cb vs[22] = cb
@ -131,8 +136,8 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca f
vs[27] = v1 vs[27] = v1
vs[28] = u0 vs[28] = u0
vs[29] = v0 vs[29] = v0
vs[30] = u1 vs[30] = u1 - du
vs[31] = v1 vs[31] = v1 - dv
vs[32] = cr vs[32] = cr
vs[33] = cg vs[33] = cg
vs[34] = cb vs[34] = cb
@ -144,8 +149,8 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca f
vs[39] = v1 vs[39] = v1
vs[40] = u0 vs[40] = u0
vs[41] = v0 vs[41] = v0
vs[42] = u1 vs[42] = u1 - du
vs[43] = v1 vs[43] = v1 - dv
vs[44] = cr vs[44] = cr
vs[45] = cg vs[45] = cg
vs[46] = cb vs[46] = cb

View File

@ -126,8 +126,8 @@ struct GetColorFromTexel<FILTER_NEAREST, address> {
float2 p = AdjustTexelByAddress<address>(v.tex, v.tex_region); float2 p = AdjustTexelByAddress<address>(v.tex, v.tex_region);
if (p.x < v.tex_region[0] || if (p.x < v.tex_region[0] ||
p.y < v.tex_region[1] || p.y < v.tex_region[1] ||
(v.tex_region[2] - texel_size.x / 512.0) <= p.x || v.tex_region[2] <= p.x ||
(v.tex_region[3] - texel_size.y / 512.0) <= p.y) { v.tex_region[3] <= p.y) {
return 0.0; return 0.0;
} }
return texture.sample(texture_sampler, p); return texture.sample(texture_sampler, p);
@ -159,11 +159,11 @@ struct GetColorFromTexel<FILTER_LINEAR, address> {
c0 = 0; c0 = 0;
c1 = 0; c1 = 0;
} }
if ((v.tex_region[2] - texel_size.x / 512.0) <= p1.x) { if (v.tex_region[2] <= p1.x) {
c1 = 0; c1 = 0;
c3 = 0; c3 = 0;
} }
if ((v.tex_region[3] - texel_size.y / 512.0) <= p1.y) { if (v.tex_region[3] <= p1.y) {
c2 = 0; c2 = 0;
c3 = 0; c3 = 0;
} }

View File

@ -191,8 +191,8 @@ void main(void) {
color = texture2D(texture, pos); color = texture2D(texture, pos);
if (pos.x < varying_tex_region[0] || if (pos.x < varying_tex_region[0] ||
pos.y < varying_tex_region[1] || pos.y < varying_tex_region[1] ||
(varying_tex_region[2] - texel_size.x / 512.0) <= pos.x || varying_tex_region[2] <= pos.x ||
(varying_tex_region[3] - texel_size.y / 512.0) <= pos.y) { varying_tex_region[3] <= pos.y) {
color = vec4(0, 0, 0, 0); color = vec4(0, 0, 0, 0);
} }
#endif #endif
@ -217,11 +217,11 @@ void main(void) {
c0 = vec4(0, 0, 0, 0); c0 = vec4(0, 0, 0, 0);
c1 = vec4(0, 0, 0, 0); c1 = vec4(0, 0, 0, 0);
} }
if ((varying_tex_region[2] - texel_size.x / 512.0) <= p1.x) { if (varying_tex_region[2] <= p1.x) {
c1 = vec4(0, 0, 0, 0); c1 = vec4(0, 0, 0, 0);
c3 = vec4(0, 0, 0, 0); c3 = vec4(0, 0, 0, 0);
} }
if ((varying_tex_region[3] - texel_size.y / 512.0) <= p1.y) { if (varying_tex_region[3] <= p1.y) {
c2 = vec4(0, 0, 0, 0); c2 = vec4(0, 0, 0, 0);
c3 = vec4(0, 0, 0, 0); c3 = vec4(0, 0, 0, 0);
} }

View File

@ -249,8 +249,8 @@ func (i *Image) PutVertex(vs []float32, dx, dy, sx, sy float32, bx0, by0, bx1, b
vs[3] = sy / float32(h) vs[3] = sy / float32(h)
vs[4] = bx0 / float32(w) vs[4] = bx0 / float32(w)
vs[5] = by0 / float32(h) vs[5] = by0 / float32(h)
vs[6] = bx1 / float32(w) vs[6] = bx1/float32(w) - 1.0/float32(w)/graphics.TexelAdjustmentFactor
vs[7] = by1 / float32(h) vs[7] = by1/float32(h) - 1.0/float32(h)/graphics.TexelAdjustmentFactor
vs[8] = cr vs[8] = cr
vs[9] = cg vs[9] = cg
vs[10] = cb vs[10] = cb