Path: blob/master/libsnes/bsnes/snes/alt/ppu-compatibility/render/bg.cpp
2 views
#ifdef PPU_CPP12//called once at the start of every rendered scanline3void PPU::update_bg_info() {4const unsigned hires = (regs.bg_mode == 5 || regs.bg_mode == 6);5const unsigned width = (!hires ? 256 : 512);67for(unsigned bg = 0; bg < 4; bg++) {8bg_info[bg].th = (regs.bg_tilesize[bg] ? 4 : 3);9bg_info[bg].tw = (hires ? 4 : bg_info[bg].th);1011bg_info[bg].mx = (bg_info[bg].th == 4 ? (width << 1) : width);12bg_info[bg].my = bg_info[bg].mx;13if(regs.bg_scsize[bg] & 0x01) bg_info[bg].mx <<= 1;14if(regs.bg_scsize[bg] & 0x02) bg_info[bg].my <<= 1;15bg_info[bg].mx--;16bg_info[bg].my--;1718bg_info[bg].scy = (regs.bg_scsize[bg] & 0x02) ? (32 << 5) : 0;19bg_info[bg].scx = (regs.bg_scsize[bg] & 0x01) ? (32 << 5) : 0;20if(regs.bg_scsize[bg] == 3) bg_info[bg].scy <<= 1;21}22}2324template<unsigned bg>25uint16 PPU::bg_get_tile(uint16 x, uint16 y) {26x = (x & bg_info[bg].mx) >> bg_info[bg].tw;27y = (y & bg_info[bg].my) >> bg_info[bg].th;2829uint16 pos = ((y & 0x1f) << 5) + (x & 0x1f);30if(y & 0x20) pos += bg_info[bg].scy;31if(x & 0x20) pos += bg_info[bg].scx;3233const uint16 addr = regs.bg_scaddr[bg] + (pos << 1);34return vram[addr] + (vram[addr + 1] << 8);35}3637#define setpixel_main(x) \38if(pixel_cache[x].pri_main < tile_pri) { \39pixel_cache[x].pri_main = tile_pri; \40pixel_cache[x].bg_main = bg; \41pixel_cache[x].src_main = col; \42pixel_cache[x].ce_main = false; \43}4445#define setpixel_sub(x) \46if(pixel_cache[x].pri_sub < tile_pri) { \47pixel_cache[x].pri_sub = tile_pri; \48pixel_cache[x].bg_sub = bg; \49pixel_cache[x].src_sub = col; \50pixel_cache[x].ce_sub = false; \51}5253template<unsigned mode, unsigned bg, unsigned color_depth>54void PPU::render_line_bg(uint8 pri0_pos, uint8 pri1_pos) {55if(layer_enabled[bg][0] == false) pri0_pos = 0;56if(layer_enabled[bg][1] == false) pri1_pos = 0;57if(pri0_pos + pri1_pos == 0) return;5859if(regs.bg_enabled[bg] == false && regs.bgsub_enabled[bg] == false) return;6061const bool bg_enabled = regs.bg_enabled[bg];62const bool bgsub_enabled = regs.bgsub_enabled[bg];6364const uint16 opt_valid_bit = (bg == BG1) ? 0x2000 : (bg == BG2) ? 0x4000 : 0x0000;65const uint8 bgpal_index = (mode == 0 ? (bg << 5) : 0);6667const uint8 pal_size = 2 << color_depth; //<<2 (*4), <<4 (*16), <<8 (*256)68const uint16 tile_mask = 0x0fff >> color_depth; //0x0fff, 0x07ff, 0x03ff69//4 + color_depth = >>(4-6) -- / {16, 32, 64 } bytes/tile70//index is a tile number count to add to base tile number71const unsigned tiledata_index = regs.bg_tdaddr[bg] >> (4 + color_depth);7273const uint8 *bg_td = bg_tiledata[color_depth];74const uint8 *bg_td_state = bg_tiledata_state[color_depth];7576const uint8 tile_width = bg_info[bg].tw;77const uint8 tile_height = bg_info[bg].th;78const uint16 mask_x = bg_info[bg].mx; //screen width mask79const uint16 mask_y = bg_info[bg].my; //screen height mask8081uint16 y = regs.bg_y[bg];82uint16 hscroll = regs.bg_hofs[bg];83uint16 vscroll = regs.bg_vofs[bg];8485const unsigned hires = (mode == 5 || mode == 6);86const unsigned width = (!hires ? 256 : 512);8788if(hires) {89hscroll <<= 1;90if(regs.interlace) y = (y << 1) + field();91}9293uint16 hval, vval;94uint16 tile_pri, tile_num;95uint8 pal_index, pal_num;96uint16 hoffset, voffset, opt_x, col;97bool mirror_x, mirror_y;9899const uint8 *tile_ptr;100const uint16 *mtable = mosaic_table[regs.mosaic_enabled[bg] ? regs.mosaic_size : 0];101const bool is_opt_mode = (mode == 2 || mode == 4 || mode == 6);102const bool is_direct_color_mode = (regs.direct_color == true && bg == BG1 && (mode == 3 || mode == 4));103104build_window_tables(bg);105const uint8 *wt_main = window[bg].main;106const uint8 *wt_sub = window[bg].sub;107108uint16 prev_x = 0xffff, prev_y = 0xffff, prev_optx = 0xffff;109for(uint16 x = 0; x < width; x++) {110hoffset = mtable[x] + hscroll;111voffset = y + vscroll;112113if(is_opt_mode) {114opt_x = (x + (hscroll & 7));115116//tile 0 is unaffected by OPT mode...117if(opt_x >= 8) {118//cache tile data in hval, vval if possible119if((opt_x >> 3) != (prev_optx >> 3)) {120prev_optx = opt_x;121122hval = bg_get_tile<BG3>((opt_x - 8) + (regs.bg_hofs[BG3] & ~7), regs.bg_vofs[BG3]);123if(mode != 4) {124vval = bg_get_tile<BG3>((opt_x - 8) + (regs.bg_hofs[BG3] & ~7), regs.bg_vofs[BG3] + 8);125}126}127128if(mode == 4) {129if(hval & opt_valid_bit) {130if(!(hval & 0x8000)) {131hoffset = opt_x + (hval & ~7);132} else {133voffset = y + hval;134}135}136} else {137if(hval & opt_valid_bit) {138hoffset = opt_x + (hval & ~7);139}140if(vval & opt_valid_bit) {141voffset = y + vval;142}143}144}145}146147hoffset &= mask_x;148voffset &= mask_y;149150if((hoffset >> 3) != prev_x || (voffset >> 3) != prev_y) {151prev_x = (hoffset >> 3);152prev_y = (voffset >> 3);153154tile_num = bg_get_tile<bg>(hoffset, voffset); //format = vhopppcc cccccccc155mirror_y = (tile_num & 0x8000);156mirror_x = (tile_num & 0x4000);157tile_pri = (tile_num & 0x2000) ? pri1_pos : pri0_pos;158pal_num = ((tile_num >> 10) & 7);159pal_index = bgpal_index + (pal_num << pal_size);160161if(tile_width == 4) { //16x16 horizontal tile mirroring162if((bool)(hoffset & 8) != mirror_x) tile_num++;163}164165if(tile_height == 4) { //16x16 vertical tile mirroring166if((bool)(voffset & 8) != mirror_y) tile_num += 16;167}168169tile_num &= 0x03ff;170tile_num += tiledata_index;171tile_num &= tile_mask;172173if(bg_td_state[tile_num] == 1) {174render_bg_tile<color_depth>(tile_num);175}176177if(mirror_y) voffset ^= 7; //invert y tile pos178tile_ptr = bg_td + (tile_num * 64) + ((voffset & 7) * 8);179}180181if(mirror_x) hoffset ^= 7; //invert x tile pos182col = *(tile_ptr + (hoffset & 7));183if(col) {184if(is_direct_color_mode) {185col = get_direct_color(pal_num, col);186} else {187col = get_palette(col + pal_index);188}189190if(!hires) {191if(bg_enabled == true && !wt_main[x]) { setpixel_main(x); }192if(bgsub_enabled == true && !wt_sub[x]) { setpixel_sub(x); }193} else {194int hx = x >> 1;195if(x & 1) {196if(bg_enabled == true && !wt_main[hx]) { setpixel_main(hx); }197} else {198if(bgsub_enabled == true && !wt_sub[hx]) { setpixel_sub(hx); }199}200}201}202}203}204205#undef setpixel_main206#undef setpixel_sub207208#endif209210211