workaround for all-tiles-hi-priority performance issue
[picodrive.git] / Pico / Draw.c
index 051b16d..c43f7f0 100644 (file)
@@ -22,8 +22,8 @@ static int  HighCacheS[80+1];   // and sprites
 static int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
 char HighSprZ[320+8+8]; // Z-buffer for accurate sprites and shadow/hilight mode\r
                         // (if bit 7 == 0, sh caused by tile; if bit 6 == 0 pixel must be shadowed, else hilighted, if bit5 == 1)\r
-// lsb->msb: moved sprites, all window tiles don't use same priority, accurate sprites (copied from PicoOpt), interlace\r
-//           dirty sprites, sonic mode\r
+// lsb->msb: moved sprites, not all window tiles use same priority, accurate sprites (copied from PicoOpt), interlace\r
+//           dirty sprites, sonic mode, have layer with all hi prio tiles (mk3), layer sh/hi already processed\r
 int rendstatus;\r
 void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to\r
 int Scanline=0; // Scanline\r
@@ -316,6 +316,8 @@ static void DrawStrip(struct TileStrip *ts, int sh)
 \r
   // terminate the cache list\r
   *ts->hc = 0;\r
+  // if oldcode wasn't changed, it means all layer is hi priority\r
+  if (oldcode == -1) rendstatus|=0x40;\r
 }\r
 \r
 // this is messy\r
@@ -381,6 +383,7 @@ void DrawStripVSRam(struct TileStrip *ts, int plane)
 \r
   // terminate the cache list\r
   *ts->hc = 0;\r
+  if (oldcode == -1) rendstatus|=0x40;\r
 }\r
 #endif\r
 \r
@@ -578,33 +581,72 @@ static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
 \r
 static void DrawTilesFromCache(int *hc, int sh)\r
 {\r
-  int code, addr, zero, dx;\r
+  int code, addr, dx;\r
   int pal;\r
-  short blank=-1; // The tile we know is blank\r
 \r
   // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r
 \r
-  while((code=*hc++)) {\r
-    if(!sh && (short)code == blank) continue;\r
+  if (sh && (rendstatus&0xc0))\r
+  {\r
+    if (!(rendstatus&0x80))\r
+    {\r
+      // as some layer has covered whole line with hi priority tiles,\r
+      // we can process whole line and then act as if sh/hi mode was off.\r
+      rendstatus|=0x80;\r
+      int c = 320/4, *zb = (int *)(HighCol+8);\r
+      while (c--)\r
+      {\r
+        int tmp = *zb;\r
+        if (!(tmp & 0x80808080)) *zb=tmp&0x3f3f3f3f;\r
+        else {\r
+          if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;\r
+          if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;\r
+          *zb=tmp;\r
+        }\r
+        zb++;\r
+      }\r
+    }\r
+    sh = 0;\r
+  }\r
 \r
-    // Get tile address/2:\r
-    addr=(code&0x7ff)<<4;\r
-    addr+=(unsigned int)code>>25; // y offset into tile\r
-    dx=(code>>16)&0x1ff;\r
-    if(sh) {\r
-      unsigned char *zb = HighCol+dx;\r
+  if (sh)\r
+  {\r
+    while((code=*hc++)) {\r
+      unsigned char *zb;\r
+      // Get tile address/2:\r
+      addr=(code&0x7ff)<<4;\r
+      addr+=(unsigned int)code>>25; // y offset into tile\r
+      dx=(code>>16)&0x1ff;\r
+      zb = HighCol+dx;\r
       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
+\r
+      pal=((code>>9)&0x30);\r
+\r
+      if (code&0x0800) TileFlip(dx,addr,pal);\r
+      else             TileNorm(dx,addr,pal);\r
     }\r
+  }\r
+  else\r
+  {\r
+    short blank=-1; // The tile we know is blank\r
+    while((code=*hc++)) {\r
+      int zero;\r
+      if((short)code == blank) continue;\r
+      // Get tile address/2:\r
+      addr=(code&0x7ff)<<4;\r
+      addr+=(unsigned int)code>>25; // y offset into tile\r
+      dx=(code>>16)&0x1ff;\r
 \r
-    pal=((code>>9)&0x30);\r
+      pal=((code>>9)&0x30);\r
 \r
-    if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
-    else             zero=TileNorm(dx,addr,pal);\r
+      if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
+      else             zero=TileNorm(dx,addr,pal);\r
 \r
-    if(zero) blank=(short)code;\r
+      if(zero) blank=(short)code;\r
+    }\r
   }\r
 }\r
 \r
@@ -1189,6 +1231,8 @@ static int DrawDisplay(int sh)
   int win=0,edge=0,hvwind=0;\r
   int maxw, maxcells;\r
 \r
+  rendstatus&=~0xc0;\r
+\r
   if(pvid->reg[12]&1) {\r
     maxw = 328; maxcells = 40;\r
   } else {\r
@@ -1236,6 +1280,15 @@ static int DrawDisplay(int sh)
     if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh);\r
   DrawAllSprites(HighCacheS, maxw, 1, sh);\r
 \r
+#if 0\r
+  {\r
+    int *c, a, b;\r
+    for (a = 0, c = HighCacheA; *c; c++, a++);\r
+    for (b = 0, c = HighCacheB; *c; c++, b++);\r
+    printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, Scanline, a, b);\r
+  }\r
+#endif\r
+\r
   return 0;\r
 }\r
 \r