psx_gpu: do enhanced lines
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu / psx_gpu_parse.c
index 61e7de5..86a816e 100644 (file)
@@ -408,7 +408,7 @@ u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
         vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
         vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
 
-        render_line(psx_gpu, vertexes, current_command, list[0]);
+        render_line(psx_gpu, vertexes, current_command, list[0], 0);
                        break;
       }
   
@@ -429,7 +429,7 @@ u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
           vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
           vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
 
-          render_line(psx_gpu, vertexes, current_command, list[0]);
+          render_line(psx_gpu, vertexes, current_command, list[0], 0);
 
           list_position++;
           num_vertexes++;
@@ -460,7 +460,7 @@ u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
         vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
         vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
 
-        render_line(psx_gpu, vertexes, current_command, 0);
+        render_line(psx_gpu, vertexes, current_command, 0, 0);
                        break;
       }
  
@@ -490,7 +490,7 @@ u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
           vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
           vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
 
-          render_line(psx_gpu, vertexes, current_command, 0);
+          render_line(psx_gpu, vertexes, current_command, 0, 0);
 
           list_position += 2;
           num_vertexes++;
@@ -750,22 +750,31 @@ breakloop:
   return list - list_start;
 }
 
+#define select_enhancement_buf(psx_gpu) { \
+  u32 _x, _b; \
+  _x = psx_gpu->saved_viewport_start_x + 8; \
+  for (_b = 0; _x >= psx_gpu->enhancement_x_threshold; _b++) \
+    _x -= psx_gpu->enhancement_x_threshold; \
+  psx_gpu->enhancement_current_buf_ptr = \
+    psx_gpu->enhancement_buf_ptr + _b * 1024 * 1024; \
+}
+
 #define enhancement_disable() { \
   psx_gpu->vram_out_ptr = psx_gpu->vram_ptr; \
   psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x; \
   psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y; \
   psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x; \
   psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y; \
-  psx_gpu->render_mode &= ~RENDER_DOUBLE_MODE; \
+  psx_gpu->uvrgb_phase = 0x8000; \
 }
 
 #define enhancement_enable() { \
-  psx_gpu->vram_out_ptr = psx_gpu->enhancement_buf_ptr; \
+  psx_gpu->vram_out_ptr = psx_gpu->enhancement_current_buf_ptr; \
   psx_gpu->viewport_start_x = psx_gpu->saved_viewport_start_x * 2; \
   psx_gpu->viewport_start_y = psx_gpu->saved_viewport_start_y * 2; \
   psx_gpu->viewport_end_x = psx_gpu->saved_viewport_end_x * 2; \
   psx_gpu->viewport_end_y = psx_gpu->saved_viewport_end_y * 2; \
-  psx_gpu->render_mode |= RENDER_DOUBLE_MODE; \
+  psx_gpu->uvrgb_phase = 0x1000; \
 }
 
 #define shift_vertices3(v) { \
@@ -835,6 +844,71 @@ static void do_quad_enhanced(psx_gpu_struct *psx_gpu, vertex_struct *vertexes,
   }
 }
 
+#define fill_vertex(i, x_, y_, u_, v_, rgb_) \
+  vertexes[i].x = x_; \
+  vertexes[i].y = y_; \
+  vertexes[i].u = u_; \
+  vertexes[i].v = v_; \
+  vertexes[i].r = rgb_; \
+  vertexes[i].g = (rgb_) >> 8; \
+  vertexes[i].b = (rgb_) >> 16
+
+static void do_esprite_in_triangles(psx_gpu_struct *psx_gpu, int x, int y,
+ u32 u, u32 v, u32 w, u32 h, u32 cmd_rgb)
+{
+  vertex_struct *vertex_ptrs[3];
+  u32 flags = (cmd_rgb >> 24);
+  u32 color = cmd_rgb & 0xffffff;
+  u32 render_state_base_saved = psx_gpu->render_state_base;
+  int x1, y1;
+  u8 u1, v1;
+
+  flags &=
+   (RENDER_FLAGS_MODULATE_TEXELS | RENDER_FLAGS_BLEND |
+   RENDER_FLAGS_TEXTURE_MAP);
+
+  set_triangle_color(psx_gpu, color);
+  if(color == 0x808080)
+    flags |= RENDER_FLAGS_MODULATE_TEXELS;
+
+  psx_gpu->render_state_base &= ~RENDER_STATE_DITHER;
+  enhancement_enable();
+
+  x1 = x + w;
+  y1 = y + h;
+  u1 = u + w;
+  v1 = v + h;
+  // FIXME..
+  if (u1 < u) u1 = 0xff;
+  if (v1 < v) v1 = 0xff;
+
+  // 0-2
+  // |/
+  // 1
+  fill_vertex(0, x,  y,  u,  v,  color);
+  fill_vertex(1, x,  y1, u,  v1, color);
+  fill_vertex(2, x1, y,  u1, v,  color);
+  if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
+    shift_vertices3(vertex_ptrs);
+    shift_triangle_area();
+    render_triangle_p(psx_gpu, vertex_ptrs, flags);
+  }
+
+  //   0
+  //  /|
+  // 1-2
+  fill_vertex(0, x1, y,  u1, v,  color);
+  fill_vertex(1, x,  y1, u,  v1, color);
+  fill_vertex(2, x1, y1, u1, v1, color);
+  if (prepare_triangle(psx_gpu, vertexes, vertex_ptrs)) {
+    shift_vertices3(vertex_ptrs);
+    shift_triangle_area();
+    render_triangle_p(psx_gpu, vertex_ptrs, flags);
+  }
+
+  psx_gpu->render_state_base = render_state_base_saved;
+}
+
 u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command)
 {
   u32 current_command = 0, command_length;
@@ -846,6 +920,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
   psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
   psx_gpu->saved_viewport_end_x = psx_gpu->viewport_end_x;
   psx_gpu->saved_viewport_end_y = psx_gpu->viewport_end_y;
+  select_enhancement_buf(psx_gpu);
 
   for(; list < list_end; list += 1 + command_length)
   {
@@ -877,7 +952,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
 
         do_fill(psx_gpu, x, y, width, height, color);
 
-        psx_gpu->vram_out_ptr = psx_gpu->enhancement_buf_ptr;
+        psx_gpu->vram_out_ptr = psx_gpu->enhancement_current_buf_ptr;
         x *= 2;
         y *= 2;
         width *= 2;
@@ -1004,7 +1079,9 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         vertexes[1].x = list_s16[4] + psx_gpu->offset_x;
         vertexes[1].y = list_s16[5] + psx_gpu->offset_y;
 
-        render_line(psx_gpu, vertexes, current_command, list[0]);
+        render_line(psx_gpu, vertexes, current_command, list[0], 0);
+        enhancement_enable();
+        render_line(psx_gpu, vertexes, current_command, list[0], 1);
         break;
       }
   
@@ -1025,7 +1102,10 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
           vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
           vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
 
-          render_line(psx_gpu, vertexes, current_command, list[0]);
+          enhancement_disable();
+          render_line(psx_gpu, vertexes, current_command, list[0], 0);
+          enhancement_enable();
+          render_line(psx_gpu, vertexes, current_command, list[0], 1);
 
           list_position++;
           num_vertexes++;
@@ -1056,7 +1136,9 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         vertexes[1].x = list_s16[6] + psx_gpu->offset_x;
         vertexes[1].y = list_s16[7] + psx_gpu->offset_y;
 
-        render_line(psx_gpu, vertexes, current_command, 0);
+        render_line(psx_gpu, vertexes, current_command, 0, 0);
+        enhancement_enable();
+        render_line(psx_gpu, vertexes, current_command, 0, 1);
         break;
       }
  
@@ -1086,7 +1168,10 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
           vertexes[1].x = (xy & 0xFFFF) + psx_gpu->offset_x;
           vertexes[1].y = (xy >> 16) + psx_gpu->offset_y;
 
-          render_line(psx_gpu, vertexes, current_command, 0);
+          enhancement_disable();
+          render_line(psx_gpu, vertexes, current_command, 0, 0);
+          enhancement_enable();
+          render_line(psx_gpu, vertexes, current_command, 0, 1);
 
           list_position += 2;
           num_vertexes++;
@@ -1111,6 +1196,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         u32 height = list_s16[5] & 0x1FF;
 
         render_sprite(psx_gpu, x, y, 0, 0, width, height, current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, 0, 0, width, height, list[0]);
         break;
       }
   
@@ -1118,14 +1204,16 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
       {        
         u32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
         u32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
-        u32 uv = list_s16[4];
+        u8 u = list_s16[4];
+        u8 v = list_s16[4] >> 8;
         u32 width = list_s16[6] & 0x3FF;
         u32 height = list_s16[7] & 0x1FF;
 
         set_clut(psx_gpu, list_s16[5]);
 
-        render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, width, height,
+        render_sprite(psx_gpu, x, y, u, v, width, height,
          current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, u, v, width, height, list[0]);
         break;
       }
   
@@ -1138,6 +1226,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
 
         render_sprite(psx_gpu, x, y, 0, 0, 1, 1, current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, 0, 0, 1, 1, list[0]);
         break;
       }
   
@@ -1150,6 +1239,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
 
         render_sprite(psx_gpu, x, y, 0, 0, 8, 8, current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, 0, 0, 8, 8, list[0]);
         break;
       }
   
@@ -1160,12 +1250,14 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
       {        
         s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
         s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
-        u32 uv = list_s16[4];
+        u8 u = list_s16[4];
+        u8 v = list_s16[4] >> 8;
 
         set_clut(psx_gpu, list_s16[5]);
 
-        render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 8, 8,
+        render_sprite(psx_gpu, x, y, u, v, 8, 8,
          current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, u, v, 8, 8, list[0]);
         break;
       }
   
@@ -1178,6 +1270,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
 
         render_sprite(psx_gpu, x, y, 0, 0, 16, 16, current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, 0, 0, 16, 16, list[0]);
         break;
       }
   
@@ -1188,12 +1281,13 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
       {        
         s32 x = sign_extend_11bit(list_s16[2] + psx_gpu->offset_x);
         s32 y = sign_extend_11bit(list_s16[3] + psx_gpu->offset_y);
-        u32 uv = list_s16[4];
+        u8 u = list_s16[4];
+        u8 v = list_s16[4] >> 8;
 
         set_clut(psx_gpu, list_s16[5]);
 
-        render_sprite(psx_gpu, x, y, uv & 0xFF, (uv >> 8) & 0xFF, 16, 16,
-         current_command, list[0]);
+        render_sprite(psx_gpu, x, y, u, v, 16, 16, current_command, list[0]);
+        do_esprite_in_triangles(psx_gpu, x, y, u, v, 16, 16, list[0]);
         break;
       }
   
@@ -1281,6 +1375,7 @@ u32 gpu_parse_enhanced(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_c
         psx_gpu->viewport_start_y = (list[0] >> 10) & 0x1FF;
         psx_gpu->saved_viewport_start_x = psx_gpu->viewport_start_x;
         psx_gpu->saved_viewport_start_y = psx_gpu->viewport_start_y;
+        select_enhancement_buf(psx_gpu);
 
 #ifdef TEXTURE_CACHE_4BPP
         psx_gpu->viewport_mask =