- int xt = egpu.enhancement_x_threshold;
- u16 *src, *dst;
- int wb, i;
-
- w += x & 7;
- x &= ~7;
- w = (w + 7) & ~7;
- if (y + h > 512)
- h = 512 - y;
-
- for (i = 0; i < 4 && w > 0; i++) {
- if (x < 512) {
- wb = w;
- if (x + w > 512)
- wb = 512 - x;
- src = gpu.vram + xt * i + y * 1024 + x;
- dst = egpu.enhancement_buf_ptr +
- (1024*1024 + xt * 2) * i + (y * 1024 + x) * 2;
- scale2x_tiles8(dst, src, wb / 8, h);
- }
-
- x -= xt;
- if (x < 0) {
- w += x;
- x = 0;
- }
+ int i, right = x + w, bottom = y + h;
+ const u16 *src = gpu.vram;
+ // use these because the scanout struct may hold reduced w, h
+ // due to intersection stuff, see the update_enhancement_buf_scanouts() mess
+ int s_w = max(gpu.screen.hres, gpu.screen.w);
+ int s_h = gpu.screen.vres;
+ if (gpu.screen.y < 0)
+ s_h -= gpu.screen.y;
+ s_w = min(s_w, 512);
+ for (i = 0; i < ARRAY_SIZE(egpu.enhancement_scanouts); i++) {
+ const struct psx_gpu_scanout *s = &egpu.enhancement_scanouts[i];
+ u16 *dst = select_enhancement_buf_by_index(&egpu, i);
+ int x1, x2, y1, y2;
+ if (s->w == 0) continue;
+ if (s->x >= right) continue;
+ if (s->x + s_w <= x) continue;
+ if (s->y >= bottom) continue;
+ if (s->y + s_h <= y) continue;
+ x1 = max(x, s->x);
+ x2 = min(right, s->x + s_w);
+ y1 = max(y, s->y);
+ y2 = min(bottom, s->y + s_h);
+ // 16-byte align for the asm version
+ x2 += x1 & 7;
+ x1 &= ~7;
+ scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
+ src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);