X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2Fdraw.c;h=0480242f7879cbd7ffaf4521397c9c919db38956;hb=d3bbc8d2cbb5fcc61e9f4e282effd0f2777a5cc3;hp=9b9ce91c81f9b403c2549e2e4aefaf68afe3842a;hpb=bfa124288c633b7631a37bb0e6be0aca1e2363ea;p=picodrive.git diff --git a/pico/draw.c b/pico/draw.c index 9b9ce91..0480242 100644 --- a/pico/draw.c +++ b/pico/draw.c @@ -1,11 +1,11 @@ -// This is part of Pico Library - -// (c) Copyright 2004 Dave, All rights reserved. -// (c) Copyright 2006-2008 notaz, All rights reserved. -// Free for non-commercial use. - -// For commercial use, separate licencing terms must be obtained. - +/* + * line renderer + * (c) Copyright Dave, 2004 + * (C) notaz, 2006-2010 + * + * This work is licensed under the terms of MAME license. + * See COPYING file in the top-level directory. + */ /* * The renderer has 4 modes now: * - normal @@ -19,7 +19,7 @@ * * since renderer always draws line in 8bit mode, there are 2 spare bits: * b \ mode: s/h as sonic - * 00 normal - - + * 00 normal - pal index * 01 shadow - pal index * 10 hilight+op spr spr pal index * 11 shadow +op spr - pal index @@ -34,14 +34,15 @@ int (*PicoScanBegin)(unsigned int num) = NULL; int (*PicoScanEnd) (unsigned int num) = NULL; -#if OVERRIDE_HIGHCOL static unsigned char DefHighCol[8+320+8]; -unsigned char *HighCol=DefHighCol; -#else -unsigned char HighCol[8+320+8]; -#endif -unsigned short DefOutBuff[320*2]; -void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to +unsigned char *HighCol = DefHighCol; +static unsigned char *HighColBase = DefHighCol; +static int HighColIncrement; + +static unsigned int DefOutBuff[320*2/2]; +void *DrawLineDest = DefOutBuff; // pointer to dest buffer where to draw this line to +void *DrawLineDestBase = DefOutBuff; +int DrawLineDestIncrement; static int HighCacheA[41+1]; // caches for high layers static int HighCacheB[41+1]; @@ -53,8 +54,9 @@ int HighPreSpr[80*2+1]; // slightly preprocessed sprites #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]... -int rendstatus = 0; -int DrawScanline = 0; +int rendstatus, rendstatus_old; +int rendlines; +int DrawScanline; int PicoDrawMask = -1; static int skip_next_line=0; @@ -74,13 +76,11 @@ struct TileStrip // stuff available in asm: #ifdef _ASM_DRAW_C void DrawWindow(int tstart, int tend, int prio, int sh); -void BackFill(int reg7, int sh); void DrawAllSprites(unsigned char *sprited, int prio, int sh); void DrawTilesFromCache(int *hc, int sh, int rlim); void DrawSpritesSHi(unsigned char *sprited); void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells); -void FinalizeLineBGR444(int sh); -void FinalizeLineRGB555(int sh); +void FinalizeLineBGR444(int sh, int line); void *blockcpy(void *dst, const void *src, size_t n); void blockcpy_or(void *dst, void *src, size_t n, int pat); #else @@ -1140,7 +1140,7 @@ static void DrawAllSprites(unsigned char *sprited, int prio, int sh) // -------------------------------------------- -static void BackFill(int reg7, int sh) +void BackFill(int reg7, int sh) { unsigned int back; @@ -1161,74 +1161,43 @@ unsigned short HighPal[0x100]; #ifndef _ASM_DRAW_C void PicoDoHighPal555(int sh) { - unsigned short *pal=HighPal; - int i, t; + unsigned int *spal, *dpal; + unsigned int t, i; Pico.m.dirtyPal = 0; - { - unsigned int *spal=(void *)Pico.cram; - unsigned int *dpal=(void *)HighPal; - for (i = 0x3f/2; i >= 0; i--) + spal = (void *)Pico.cram; + dpal = (void *)HighPal; + + for (i = 0; i < 0x40 / 2; i++) { + t = spal[i]; #ifdef USE_BGR555 - dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4); + t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4); #else - dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7); + t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7); #endif + // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that. + // otherwise intensity difference between this and s/h will be wrong + t |= (t >> 4) & 0x08610861; // 0x18e318e3 + dpal[i] = t; } + // norm: xxx0, sh: 0xxx, hi: 0xxx + 7 if (sh) { // shadowed pixels - for (i = 0x3f; i >= 0; i--) - pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e); + for (i = 0; i < 0x40 / 2; i++) + dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e; // hilighted pixels - for (i = 0x3f; i >= 0; i--) { - t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c; - pal[0x80|i]=(unsigned short)t; + for (i = 0; i < 0x40 / 2; i++) { + t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef; + t |= (t >> 4) & 0x08610861; + dpal[0x80/2 | i] = t; } } } -static void FinalizeLineBGR444(int sh) -{ - unsigned short *pd=DrawLineDest; - unsigned char *ps=HighCol+8; - unsigned short *pal=Pico.cram; - int len, i, t, mask=0xff; - - if (Pico.video.reg[12]&1) { - len = 320; - } else { - if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32; - len = 256; - } - - if(sh) { - pal=HighPal; - if(Pico.m.dirtyPal) { - blockcpy(pal, Pico.cram, 0x40*2); - // shadowed pixels - for(i = 0x3f; i >= 0; i--) - pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777); - // hilighted pixels - for(i = 0x3f; i >= 0; i--) { - t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee; - pal[0x80|i]=(unsigned short)t; - } - Pico.m.dirtyPal = 0; - } - } - - if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI)) - mask=0x3f; // accurate sprites - - for(i = 0; i < len; i++) - pd[i] = pal[ps[i] & mask]; -} - - -static void FinalizeLineRGB555(int sh) +void FinalizeLine555(int sh, int line) { unsigned short *pd=DrawLineDest; unsigned char *ps=HighCol+8; @@ -1264,13 +1233,13 @@ static void FinalizeLineRGB555(int sh) } #endif -static void FinalizeLine8bit(int sh) +static void FinalizeLine8bit(int sh, int line) { - unsigned char *pd=DrawLineDest; + unsigned char *pd = DrawLineDest; int len, rs = rendstatus; static int dirty_count; - if (!sh && Pico.m.dirtyPal == 1 && DrawScanline < 222) + if (!sh && Pico.m.dirtyPal == 1) { // a hack for mid-frame palette changes if (!(rs & PDRAW_SONIC_MODE)) @@ -1288,7 +1257,8 @@ static void FinalizeLine8bit(int sh) if (Pico.video.reg[12]&1) { len = 320; } else { - if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32; + if (!(PicoOpt & POPT_DIS_32C_BORDER)) + pd += 32; len = 256; } @@ -1303,26 +1273,10 @@ static void FinalizeLine8bit(int sh) } } -static void (*FinalizeLine)(int sh) = FinalizeLineBGR444; +static void (*FinalizeLine)(int sh, int line); // -------------------------------------------- -static void DrawBlankedLine(void) -{ - int sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight? - - if (PicoScanBegin != NULL) - PicoScanBegin(DrawScanline); - - BackFill(Pico.video.reg[7], sh); - - if (FinalizeLine != NULL) - FinalizeLine(sh); - - if (PicoScanEnd != NULL) - PicoScanEnd(DrawScanline); -} - static int DrawDisplay(int sh) { unsigned char *sprited = &HighLnSpr[DrawScanline][0]; @@ -1417,75 +1371,192 @@ static int DrawDisplay(int sh) // MUST be called every frame PICO_INTERNAL void PicoFrameStart(void) { + int offs = 8, lines = 224; + // prepare to do this frame rendstatus = 0; - if ((Pico.video.reg[12]&6) == 6) + if ((Pico.video.reg[12] & 6) == 6) rendstatus |= PDRAW_INTERLACE; // interlace mode + if (!(Pico.video.reg[12] & 1)) + rendstatus |= PDRAW_32_COLS; + if (Pico.video.reg[1] & 8) { + offs = 0; + lines = 240; + } - if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed + if (rendstatus != rendstatus_old || lines != rendlines) { + rendlines = lines; + // mode_change() might reset rendstatus_old by calling SetColorFormat + emu_video_mode_change((lines == 240) ? 0 : 8, + lines, (Pico.video.reg[12] & 1) ? 0 : 1); + rendstatus_old = rendstatus; + } + + HighCol = HighColBase + offs * HighColIncrement; + DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement; + DrawScanline = 0; + skip_next_line = 0; + + if (PicoOpt & POPT_ALT_RENDERER) + return; - DrawScanline=0; + if (Pico.m.dirtyPal) + Pico.m.dirtyPal = 2; // reset dirty if needed PrepareSprites(1); - skip_next_line=0; } -static void PicoLine(void) +static void DrawBlankedLine(int line, int offs, int sh, int bgc) { - int sh; - if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines + if (PicoScanBegin != NULL) + PicoScanBegin(line + offs); + + BackFill(bgc, sh); - sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight? + if (FinalizeLine != NULL) + FinalizeLine(sh, line); + + if (PicoScanEnd != NULL) + PicoScanEnd(line + offs); + + HighCol += HighColIncrement; + DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement; +} +static void PicoLine(int line, int offs, int sh, int bgc) +{ + int skip = 0; + + if (skip_next_line > 0) { + skip_next_line--; + return; + } + + DrawScanline = line; if (PicoScanBegin != NULL) - skip_next_line = PicoScanBegin(DrawScanline); + skip = PicoScanBegin(line + offs); + + if (skip) { + skip_next_line = skip - 1; + return; + } // Draw screen: - BackFill(Pico.video.reg[7], sh); + BackFill(bgc, sh); if (Pico.video.reg[1]&0x40) DrawDisplay(sh); if (FinalizeLine != NULL) - FinalizeLine(sh); + FinalizeLine(sh, line); if (PicoScanEnd != NULL) - skip_next_line = PicoScanEnd(DrawScanline); + skip_next_line = PicoScanEnd(line + offs); + + HighCol += HighColIncrement; + DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement; } void PicoDrawSync(int to, int blank_last_line) { - for (; DrawScanline < to; DrawScanline++) + int line, offs = 0; + int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight? + int bgc = Pico.video.reg[7]; + + pprof_start(draw); + + if (rendlines != 240) + offs = 8; + + for (line = DrawScanline; line < to; line++) { -#if !CAN_HANDLE_240_LINES - if (DrawScanline >= 224) break; -#endif - PicoLine(); + PicoLine(line, offs, sh, bgc); } -#if !CAN_HANDLE_240_LINES - if (DrawScanline >= 224) { DrawScanline = 240; return; } -#endif - // last line - if (DrawScanline <= to) + if (line <= to) { if (blank_last_line) - DrawBlankedLine(); - else PicoLine(); - DrawScanline++; + DrawBlankedLine(line, offs, sh, bgc); + else PicoLine(line, offs, sh, bgc); + line++; } + DrawScanline = line; + + pprof_end(draw); } -void PicoDrawSetColorFormat(int which) +// also works for fast renderer +void PicoDrawUpdateHighPal(void) +{ + int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight? + if (PicoOpt & POPT_ALT_RENDERER) + sh = 0; // no s/h support + + PicoDoHighPal555(sh); + if (rendstatus & PDRAW_SONIC_MODE) { + // FIXME? + memcpy(HighPal + 0x40, HighPal, 0x40*2); + memcpy(HighPal + 0x80, HighPal, 0x40*2); + } +} + +void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode) { switch (which) { - case 2: FinalizeLine = FinalizeLine8bit; break; - case 1: FinalizeLine = FinalizeLineRGB555; break; - case 0: FinalizeLine = FinalizeLineBGR444; break; - default:FinalizeLine = NULL; break; + case PDF_8BIT: + FinalizeLine = FinalizeLine8bit; + break; + + case PDF_RGB555: + if ((PicoAHW & PAHW_32X) && use_32x_line_mode) + FinalizeLine = FinalizeLine32xRGB555; + else + FinalizeLine = FinalizeLine555; + break; + + default: + FinalizeLine = NULL; + break; } -#if OVERRIDE_HIGHCOL - if (which) HighCol=DefHighCol; -#endif + PicoDrawSetOutFormat32x(which, use_32x_line_mode); + PicoDrawSetOutputMode4(which); + rendstatus_old = -1; } +// note: may be called on the middle of frame +void PicoDrawSetOutBuf(void *dest, int increment) +{ + DrawLineDestBase = dest; + DrawLineDestIncrement = increment; + DrawLineDest = (unsigned char*)DrawLineDestBase + DrawScanline * increment; +} + +void PicoDrawSetInternalBuf(void *dest, int increment) +{ + if (dest != NULL) { + HighColBase = dest; + HighColIncrement = increment; + HighCol = HighColBase + DrawScanline * increment; + } + else { + HighColBase = DefHighCol; + HighColIncrement = 0; + } +} + +void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num)) +{ + PicoScanBegin = NULL; + PicoScanEnd = NULL; + PicoScan32xBegin = NULL; + PicoScan32xEnd = NULL; + + if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) { + PicoScan32xBegin = begin; + PicoScan32xEnd = end; + } + else { + PicoScanBegin = begin; + PicoScanEnd = end; + } +}