📄 scanline.cpp
字号:
if (size > 16) { x >>= (size - 16); } else if (size < 16) { x = ggl_expand(x, size, 16); } x += x >> 15; return x;}void blend_factor(context_t* c, pixel_t* r, uint32_t factor, const pixel_t* src, const pixel_t* dst){ switch (factor) { case GGL_ZERO: r->c[1] = r->c[2] = r->c[3] = r->c[0] = 0; break; case GGL_ONE: r->c[1] = r->c[2] = r->c[3] = r->c[0] = FIXED_ONE; break; case GGL_DST_COLOR: r->c[1] = blendfactor(dst->c[1], dst->s[1]); r->c[2] = blendfactor(dst->c[2], dst->s[2]); r->c[3] = blendfactor(dst->c[3], dst->s[3]); r->c[0] = blendfactor(dst->c[0], dst->s[0]); break; case GGL_SRC_COLOR: r->c[1] = blendfactor(src->c[1], src->s[1]); r->c[2] = blendfactor(src->c[2], src->s[2]); r->c[3] = blendfactor(src->c[3], src->s[3]); r->c[0] = blendfactor(src->c[0], src->s[0]); break; case GGL_ONE_MINUS_DST_COLOR: r->c[1] = FIXED_ONE - blendfactor(dst->c[1], dst->s[1]); r->c[2] = FIXED_ONE - blendfactor(dst->c[2], dst->s[2]); r->c[3] = FIXED_ONE - blendfactor(dst->c[3], dst->s[3]); r->c[0] = FIXED_ONE - blendfactor(dst->c[0], dst->s[0]); break; case GGL_ONE_MINUS_SRC_COLOR: r->c[1] = FIXED_ONE - blendfactor(src->c[1], src->s[1]); r->c[2] = FIXED_ONE - blendfactor(src->c[2], src->s[2]); r->c[3] = FIXED_ONE - blendfactor(src->c[3], src->s[3]); r->c[0] = FIXED_ONE - blendfactor(src->c[0], src->s[0]); break; case GGL_SRC_ALPHA: r->c[1] = r->c[2] = r->c[3] = r->c[0] = blendfactor(src->c[0], src->s[0], FIXED_ONE); break; case GGL_ONE_MINUS_SRC_ALPHA: r->c[1] = r->c[2] = r->c[3] = r->c[0] = FIXED_ONE - blendfactor(src->c[0], src->s[0], FIXED_ONE); break; case GGL_DST_ALPHA: r->c[1] = r->c[2] = r->c[3] = r->c[0] = blendfactor(dst->c[0], dst->s[0], FIXED_ONE); break; case GGL_ONE_MINUS_DST_ALPHA: r->c[1] = r->c[2] = r->c[3] = r->c[0] = FIXED_ONE - blendfactor(dst->c[0], dst->s[0], FIXED_ONE); break; case GGL_SRC_ALPHA_SATURATE: // XXX: GGL_SRC_ALPHA_SATURATE break; }}static GGLfixed wrapping(int32_t coord, uint32_t size, int tx_wrap){ GGLfixed d; if (tx_wrap == GGL_REPEAT) { d = (uint32_t(coord)>>16) * size; } else if (tx_wrap == GGL_CLAMP) { // CLAMP_TO_EDGE semantics const GGLfixed clamp_min = FIXED_HALF; const GGLfixed clamp_max = (size << 16) - FIXED_HALF; if (coord < clamp_min) coord = clamp_min; if (coord > clamp_max) coord = clamp_max; d = coord; } else { // 1:1 const GGLfixed clamp_min = 0; const GGLfixed clamp_max = (size << 16); if (coord < clamp_min) coord = clamp_min; if (coord > clamp_max) coord = clamp_max; d = coord; } return d;}static inlineGGLcolor ADJUST_COLOR_ITERATOR(GGLcolor v, GGLcolor dvdx, int len){ const int32_t end = dvdx * (len-1) + v; if (end < 0) v -= end; v &= ~(v>>31); return v;}void scanline(context_t* c){ const uint32_t enables = c->state.enables; const int xs = c->iterators.xl; const int x1 = c->iterators.xr; int xc = x1 - xs; const int16_t* covPtr = c->state.buffers.coverage + xs; // All iterated values are sampled at the pixel center // reset iterators for that scanline... GGLcolor r, g, b, a; iterators_t& ci = c->iterators; if (enables & GGL_ENABLE_SMOOTH) { r = (xs * c->shade.drdx) + ci.ydrdy; g = (xs * c->shade.dgdx) + ci.ydgdy; b = (xs * c->shade.dbdx) + ci.ydbdy; a = (xs * c->shade.dadx) + ci.ydady; r = ADJUST_COLOR_ITERATOR(r, c->shade.drdx, xc); g = ADJUST_COLOR_ITERATOR(g, c->shade.dgdx, xc); b = ADJUST_COLOR_ITERATOR(b, c->shade.dbdx, xc); a = ADJUST_COLOR_ITERATOR(a, c->shade.dadx, xc); } else { r = ci.ydrdy; g = ci.ydgdy; b = ci.ydbdy; a = ci.ydady; } // z iterators are 1.31 GGLfixed z = (xs * c->shade.dzdx) + ci.ydzdy; GGLfixed f = (xs * c->shade.dfdx) + ci.ydfdy; struct { GGLfixed s, t; } tc[GGL_TEXTURE_UNIT_COUNT]; if (enables & GGL_ENABLE_TMUS) { for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; ++i) { if (c->state.texture[i].enable) { texture_iterators_t& ti = c->state.texture[i].iterators; if (enables & GGL_ENABLE_W) { tc[i].s = ti.ydsdy; tc[i].t = ti.ydtdy; } else { tc[i].s = (xs * ti.dsdx) + ti.ydsdy; tc[i].t = (xs * ti.dtdx) + ti.ydtdy; } } } } pixel_t fragment; pixel_t texel; pixel_t fb; uint32_t x = xs; uint32_t y = c->iterators.y; while (xc--) { { // just a scope // read color (convert to 8 bits by keeping only the integer part) fragment.s[1] = fragment.s[2] = fragment.s[3] = fragment.s[0] = 8; fragment.c[1] = r >> (GGL_COLOR_BITS-8); fragment.c[2] = g >> (GGL_COLOR_BITS-8); fragment.c[3] = b >> (GGL_COLOR_BITS-8); fragment.c[0] = a >> (GGL_COLOR_BITS-8); // texturing if (enables & GGL_ENABLE_TMUS) { for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; ++i) { texture_t& tx = c->state.texture[i]; if (!tx.enable) continue; texture_iterators_t& ti = tx.iterators; int32_t u, v; // s-coordinate if (tx.s_coord != GGL_ONE_TO_ONE) { const int w = tx.surface.width; u = wrapping(tc[i].s, w, tx.s_wrap); tc[i].s += ti.dsdx; } else { u = (((tx.shade.is0>>16) + x)<<16) + FIXED_HALF; } // t-coordinate if (tx.t_coord != GGL_ONE_TO_ONE) { const int h = tx.surface.height; v = wrapping(tc[i].t, h, tx.t_wrap); tc[i].t += ti.dtdx; } else { v = (((tx.shade.it0>>16) + y)<<16) + FIXED_HALF; } // read texture if (tx.mag_filter == GGL_NEAREST && tx.min_filter == GGL_NEAREST) { u >>= 16; v >>= 16; tx.surface.read(&tx.surface, c, u, v, &texel); } else { const int w = tx.surface.width; const int h = tx.surface.height; u -= FIXED_HALF; v -= FIXED_HALF; int u0 = u >> 16; int v0 = v >> 16; int u1 = u0 + 1; int v1 = v0 + 1; if (tx.s_wrap == GGL_REPEAT) { if (u0<0) u0 += w; if (u1<0) u1 += w; if (u0>=w) u0 -= w; if (u1>=w) u1 -= w; } else { if (u0<0) u0 = 0; if (u1<0) u1 = 0; if (u0>=w) u0 = w-1; if (u1>=w) u1 = w-1; } if (tx.t_wrap == GGL_REPEAT) { if (v0<0) v0 += h; if (v1<0) v1 += h; if (v0>=h) v0 -= h; if (v1>=h) v1 -= h; } else { if (v0<0) v0 = 0; if (v1<0) v1 = 0; if (v0>=h) v0 = h-1; if (v1>=h) v1 = h-1; } pixel_t texels[4]; uint32_t mm[4]; tx.surface.read(&tx.surface, c, u0, v0, &texels[0]); tx.surface.read(&tx.surface, c, u0, v1, &texels[1]); tx.surface.read(&tx.surface, c, u1, v0, &texels[2]); tx.surface.read(&tx.surface, c, u1, v1, &texels[3]); u = (u >> 12) & 0xF; v = (v >> 12) & 0xF; u += u>>3; v += v>>3; mm[0] = (0x10 - u) * (0x10 - v); mm[1] = (0x10 - u) * v; mm[2] = u * (0x10 - v); mm[3] = 0x100 - (mm[0] + mm[1] + mm[2]); for (int j=0 ; j<4 ; j++) { texel.s[j] = texels[0].s[j]; if (!texel.s[j]) continue; texel.s[j] += 8; texel.c[j] = texels[0].c[j]*mm[0] + texels[1].c[j]*mm[1] + texels[2].c[j]*mm[2] + texels[3].c[j]*mm[3] ; } } // Texture environnement... for (int j=0 ; j<4 ; j++) { uint32_t& Cf = fragment.c[j]; uint32_t& Ct = texel.c[j]; uint8_t& sf = fragment.s[j]; uint8_t& st = texel.s[j]; uint32_t At = texel.c[0]; uint8_t sat = texel.s[0]; switch (tx.env) { case GGL_REPLACE: if (st) { Cf = Ct; sf = st; } break; case GGL_MODULATE: if (st) { uint32_t factor = Ct + (Ct>>(st-1)); Cf = (Cf * factor) >> st; } break; case GGL_DECAL: if (sat) { rescale(Cf, sf, Ct, st); Cf += ((Ct - Cf) * (At + (At>>(sat-1)))) >> sat; } break; case GGL_BLEND: if (st) { uint32_t Cc = tx.env_color[i]; if (sf>8) Cc = (Cc * ((1<<sf)-1))>>8; else if (sf<8) Cc = (Cc - (Cc>>(8-sf)))>>(8-sf); uint32_t factor = Ct + (Ct>>(st-1)); Cf = ((((1<<st) - factor) * Cf) + Ct*Cc)>>st; } break; } } } } // coverage application if (enables & GGL_ENABLE_AA) { int16_t cf = *covPtr++; fragment.c[0] = (int64_t(fragment.c[0]) * cf) >> 15; } // alpha-test if (enables & GGL_ENABLE_ALPHA_TEST) { GGLcolor ref = c->state.alpha_test.ref; GGLcolor alpha = (uint64_t(fragment.c[0]) * ((1<<GGL_COLOR_BITS)-1)) / ((1<<fragment.s[0])-1); switch (c->state.alpha_test.func) { case GGL_NEVER: goto discard; case GGL_LESS: if (alpha<ref) break; goto discard; case GGL_EQUAL: if (alpha==ref) break; goto discard; case GGL_LEQUAL: if (alpha<=ref) break; goto discard; case GGL_GREATER: if (alpha>ref) break; goto discard; case GGL_NOTEQUAL: if (alpha!=ref) break; goto discard; case GGL_GEQUAL: if (alpha>=ref) break; goto discard; } } // depth test if (c->state.buffers.depth.format) { if (enables & GGL_ENABLE_DEPTH_TEST) { surface_t* cb = &(c->state.buffers.depth); uint16_t* p = (uint16_t*)(cb->data)+(x+(cb->stride*y)); uint16_t zz = uint32_t(z)>>(16); uint16_t depth = *p; switch (c->state.depth_test.func) { case GGL_NEVER: goto discard; case GGL_LESS: if (zz<depth) break; goto discard; case GGL_EQUAL: if (zz==depth) break; goto discard; case GGL_LEQUAL: if (zz<=depth) break; goto discard; case GGL_GREATER: if (zz>depth) break; goto discard; case GGL_NOTEQUAL: if (zz!=depth) break; goto discard; case GGL_GEQUAL: if (zz>=depth) break; goto discard; } // depth buffer is not enabled, if depth-test is not enabled/* fragment.s[1] = fragment.s[2] = fragment.s[3] = fragment.s[0] = 8; fragment.c[1] = fragment.c[2] = fragment.c[3] = fragment.c[0] = 255 - (zz>>8);*/ if (c->state.mask.depth) { *p = zz; } } } // fog if (enables & GGL_ENABLE_FOG) { for (int i=1 ; i<=3 ; i++) { GGLfixed fc = (c->state.fog.color[i] * 0x10000) / 0xFF; uint32_t& c = fragment.c[i]; uint8_t& s = fragment.s[i]; c = (c * 0x10000) / ((1<<s)-1); c = gglMulAddx(c, f, gglMulx(fc, 0x10000 - f));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -