Path: blob/master/libsnes/bsnes/snes/alt/ppu-compatibility/render/mode7.cpp
2 views
#ifdef PPU_CPP12//bsnes mode7 renderer3//4//base algorithm written by anomie5//bsnes implementation written by byuu6//7//supports mode 7 + extbg + rotate + zoom + direct color + scrolling + m7sel + windowing + mosaic8//interlace and pseudo-hires support are automatic via main rendering routine910//13-bit sign extend11//--s---vvvvvvvvvv -> ssssssvvvvvvvvvv12#define CLIP(x) ( ((x) & 0x2000) ? ( (x) | ~0x03ff) : ((x) & 0x03ff) )1314template<unsigned bg>15void PPU::render_line_mode7(uint8 pri0_pos, uint8 pri1_pos) {16if(layer_enabled[bg][0] == false) pri0_pos = 0;17if(layer_enabled[bg][1] == false) pri1_pos = 0;18if(pri0_pos + pri1_pos == 0) return;1920if(regs.bg_enabled[bg] == false && regs.bgsub_enabled[bg] == false) return;2122int32 px, py;23int32 tx, ty, tile, palette;2425int32 a = sclip<16>(cache.m7a);26int32 b = sclip<16>(cache.m7b);27int32 c = sclip<16>(cache.m7c);28int32 d = sclip<16>(cache.m7d);2930int32 cx = sclip<13>(cache.m7x);31int32 cy = sclip<13>(cache.m7y);32int32 hofs = sclip<13>(cache.m7_hofs);33int32 vofs = sclip<13>(cache.m7_vofs);3435int _pri, _x;36bool _bg_enabled = regs.bg_enabled[bg];37bool _bgsub_enabled = regs.bgsub_enabled[bg];3839build_window_tables(bg);40uint8 *wt_main = window[bg].main;41uint8 *wt_sub = window[bg].sub;4243int32 y = (regs.mode7_vflip == false ? line : 255 - line);4445uint16 *mtable_x, *mtable_y;46if(bg == BG1) {47mtable_x = (uint16*)mosaic_table[(regs.mosaic_enabled[BG1] == true) ? regs.mosaic_size : 0];48mtable_y = (uint16*)mosaic_table[(regs.mosaic_enabled[BG1] == true) ? regs.mosaic_size : 0];49} else { //bg == BG250//Mode7 EXTBG BG2 uses BG1 mosaic enable to control vertical mosaic,51//and BG2 mosaic enable to control horizontal mosaic...52mtable_x = (uint16*)mosaic_table[(regs.mosaic_enabled[BG2] == true) ? regs.mosaic_size : 0];53mtable_y = (uint16*)mosaic_table[(regs.mosaic_enabled[BG1] == true) ? regs.mosaic_size : 0];54}5556int32 psx = ((a * CLIP(hofs - cx)) & ~63) + ((b * CLIP(vofs - cy)) & ~63) + ((b * mtable_y[y]) & ~63) + (cx << 8);57int32 psy = ((c * CLIP(hofs - cx)) & ~63) + ((d * CLIP(vofs - cy)) & ~63) + ((d * mtable_y[y]) & ~63) + (cy << 8);58for(int32 x = 0; x < 256; x++) {59px = psx + (a * mtable_x[x]);60py = psy + (c * mtable_x[x]);6162//mask floating-point bits (low 8 bits)63px >>= 8;64py >>= 8;6566switch(regs.mode7_repeat) {67case 0: //screen repetition outside of screen area68case 1: { //same as case 069px &= 1023;70py &= 1023;71tx = ((px >> 3) & 127);72ty = ((py >> 3) & 127);73tile = vram[(ty * 128 + tx) << 1];74palette = vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];75} break;76case 2: { //palette color 0 outside of screen area77if((px | py) & ~1023) {78palette = 0;79} else {80px &= 1023;81py &= 1023;82tx = ((px >> 3) & 127);83ty = ((py >> 3) & 127);84tile = vram[(ty * 128 + tx) << 1];85palette = vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];86}87} break;88case 3: { //character 0 repetition outside of screen area89if((px | py) & ~1023) {90tile = 0;91} else {92px &= 1023;93py &= 1023;94tx = ((px >> 3) & 127);95ty = ((py >> 3) & 127);96tile = vram[(ty * 128 + tx) << 1];97}98palette = vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];99} break;100}101102if(bg == BG1) {103_pri = pri0_pos;104} else {105_pri = (palette >> 7) ? pri1_pos : pri0_pos;106palette &= 0x7f;107}108109if(!palette) continue;110111_x = (regs.mode7_hflip == false) ? (x) : (255 - x);112113uint32 col;114if(regs.direct_color == true && bg == BG1) {115//direct color mode does not apply to bg2, as it is only 128 colors...116col = get_direct_color(0, palette);117} else {118col = get_palette(palette);119}120121if(regs.bg_enabled[bg] == true && !wt_main[_x]) {122if(pixel_cache[_x].pri_main < _pri) {123pixel_cache[_x].pri_main = _pri;124pixel_cache[_x].bg_main = bg;125pixel_cache[_x].src_main = col;126pixel_cache[_x].ce_main = false;127}128}129if(regs.bgsub_enabled[bg] == true && !wt_sub[_x]) {130if(pixel_cache[_x].pri_sub < _pri) {131pixel_cache[_x].pri_sub = _pri;132pixel_cache[_x].bg_sub = bg;133pixel_cache[_x].src_sub = col;134pixel_cache[_x].ce_sub = false;135}136}137}138}139140#undef CLIP141142#endif143144145