Compare commits

...

2 Commits

1 changed files with 81 additions and 44 deletions

View File

@ -164,7 +164,7 @@ pub const Ppu = struct {
const x = (sprite.x() +% i) % width;
const ix = @bitCast(i9, x);
if (self.scanline.top()[x] != null) continue;
if (!shouldDrawSprite(self.bldcnt, &self.scanline, x)) continue;
const sprite_start = sprite.x();
const isprite_start = @bitCast(i9, sprite_start);
@ -191,7 +191,10 @@ pub const Ppu = struct {
const pal_id: u16 = if (!is_8bpp) get4bppTilePalette(sprite.palBank(), col, tile) else tile;
// Sprite Palette starts at 0x0500_0200
if (pal_id != 0) self.scanline.top()[x] = self.palette.read(u16, 0x200 + pal_id * 2);
if (pal_id != 0) {
const bgr555 = self.palette.read(u16, 0x200 + pal_id * 2);
copyToSpriteBuffer(self.bldcnt, &self.scanline, x, bgr555);
}
}
}
@ -211,15 +214,7 @@ pub const Ppu = struct {
const x = (sprite.x() +% i) % width;
const ix = @bitCast(i9, x);
if (self.scanline.top()[x] != null) continue;
if (self.scanline.btm()[x] != null) {
if (self.bldcnt.mode.read() != 0b01) continue;
const b_layers = self.bldcnt.layer_b.read();
const is_blend_enabled = (b_layers >> 4) & 1 == 1;
if (!is_blend_enabled) continue;
}
if (!shouldDrawSprite(self.bldcnt, &self.scanline, x)) continue;
const sprite_start = sprite.x();
const isprite_start = @bitCast(i9, sprite_start);
@ -254,19 +249,7 @@ pub const Ppu = struct {
// Sprite Palette starts at 0x0500_0200
if (pal_id != 0) {
const bgr555 = self.palette.read(u16, 0x200 + pal_id * 2);
if (self.bldcnt.mode.read() == 0b01) {
// Alpha Blending
const a_layers = self.bldcnt.layer_a.read();
const is_blend_enabled = (a_layers >> 4) & 1 == 1;
if (is_blend_enabled) {
self.scanline.btm()[x] = bgr555;
return;
}
}
self.scanline.top()[x] = bgr555;
copyToSpriteBuffer(self.bldcnt, &self.scanline, x, bgr555);
}
}
}
@ -312,7 +295,7 @@ pub const Ppu = struct {
if (pal_id != 0) {
const bgr555 = self.palette.read(u16, pal_id * 2);
drawToScanlineBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
copyToBackgroundBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
}
}
@ -369,7 +352,7 @@ pub const Ppu = struct {
if (pal_id != 0) {
const bgr555 = self.palette.read(u16, pal_id * 2);
drawToScanlineBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
copyToBackgroundBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
}
}
}
@ -407,8 +390,7 @@ pub const Ppu = struct {
const maybe_top = maybe_px;
const maybe_btm = self.scanline.btm()[i];
// TODO: Why must I reverse this?
const bgr555 = getBgr555(&self.palette, self.bldalpha, maybe_btm, maybe_top);
const bgr555 = self.getBgr555(maybe_top, maybe_btm);
std.mem.writeIntNative(u32, self.framebuf.get(.Emulator)[fb_base + i * @sizeOf(u32) ..][0..@sizeOf(u32)], COLOUR_LUT[bgr555 & 0x7FFF]);
}
@ -435,8 +417,7 @@ pub const Ppu = struct {
const maybe_top = maybe_px;
const maybe_btm = self.scanline.btm()[i];
// TODO: Why must I reverse this?
const bgr555 = getBgr555(&self.palette, self.bldalpha, maybe_btm, maybe_top);
const bgr555 = self.getBgr555(maybe_top, maybe_btm);
std.mem.writeIntNative(u32, self.framebuf.get(.Emulator)[fb_base + i * @sizeOf(u32) ..][0..@sizeOf(u32)], COLOUR_LUT[bgr555 & 0x7FFF]);
}
@ -462,8 +443,7 @@ pub const Ppu = struct {
const maybe_top = maybe_px;
const maybe_btm = self.scanline.btm()[i];
// TODO: Why must I reverse this?
const bgr555 = getBgr555(&self.palette, self.bldalpha, maybe_btm, maybe_top);
const bgr555 = self.getBgr555(maybe_top, maybe_btm);
std.mem.writeIntNative(u32, self.framebuf.get(.Emulator)[fb_base + i * @sizeOf(u32) ..][0..@sizeOf(u32)], COLOUR_LUT[bgr555 & 0x7FFF]);
}
@ -514,6 +494,44 @@ pub const Ppu = struct {
}
}
fn getBgr555(self: *Self, maybe_top: ?u16, maybe_btm: ?u16) u16 {
if (maybe_btm) |btm| {
return switch (self.bldcnt.mode.read()) {
0b00 => if (maybe_top) |top| top else btm,
0b01 => if (maybe_top) |top| alphaBlend(btm, top, self.bldalpha) else btm,
0b10 => blk: {
const evy: u16 = self.bldy.evy.read();
const r = btm & 0x1F;
const g = (btm >> 5) & 0x1F;
const b = (btm >> 10) & 0x1F;
const bld_r = r + (((31 - r) * evy) >> 4);
const bld_g = g + (((31 - g) * evy) >> 4);
const bld_b = b + (((31 - b) * evy) >> 4);
break :blk (bld_b << 10) | (bld_g << 5) | bld_r;
},
0b11 => blk: {
const evy: u16 = self.bldy.evy.read();
const btm_r = btm & 0x1F;
const btm_g = (btm >> 5) & 0x1F;
const btm_b = (btm >> 10) & 0x1F;
const bld_r = btm_r - ((btm_r * evy) >> 4);
const bld_g = btm_g - ((btm_g * evy) >> 4);
const bld_b = btm_b - ((btm_b * evy) >> 4);
break :blk (bld_b << 10) | (bld_g << 5) | bld_r;
},
};
}
if (maybe_top) |top| return top;
return self.palette.getBackdrop();
}
// TODO: Comment this + get a better understanding
fn tilemapOffset(size: u2, x: u32, y: u32) u32 {
// Current Row: (y % PIXEL_COUNT) / 8
@ -1050,16 +1068,6 @@ fn toRgba8888Talarubi(bgr555: u16) u32 {
return @floatToInt(u32, out_r) << 24 | @floatToInt(u32, out_g) << 16 | @floatToInt(u32, out_b) << 8 | 0xFF;
}
fn getBgr555(palette: *const Palette, bldalpha: io.BldAlpha, maybe_top: ?u16, maybe_btm: ?u16) u16 {
if (maybe_top) |top| {
if (maybe_btm) |btm| return alphaBlend(top, btm, bldalpha);
return top;
}
if (maybe_btm) |btm| return btm;
return palette.getBackdrop();
}
fn alphaBlend(top: u16, btm: u16, bldalpha: io.BldAlpha) u16 {
const eva: u16 = bldalpha.eva.read();
const evb: u16 = bldalpha.evb.read();
@ -1110,8 +1118,22 @@ fn shouldDrawBackground(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline,
return true;
}
fn drawToScanlineBuffer(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline, i: usize, bgr555: u16) void {
if (bldcnt.mode.read() == 0b01) {
fn shouldDrawSprite(bldcnt: io.BldCnt, scanline: *Scanline, x: u9) bool {
if (scanline.top()[x] != null) return false;
if (scanline.btm()[x] != null) {
if (bldcnt.mode.read() != 0b01) return false;
const b_layers = bldcnt.layer_b.read();
const is_blend_enabled = (b_layers >> 4) & 1 == 1;
if (!is_blend_enabled) return false;
}
return true;
}
fn copyToBackgroundBuffer(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline, i: usize, bgr555: u16) void {
if (bldcnt.mode.read() != 0b00) {
// Standard Alpha Blending
const a_layers = bldcnt.layer_a.read();
const is_blend_enabled = (a_layers >> n) & 1 == 1;
@ -1127,6 +1149,21 @@ fn drawToScanlineBuffer(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline,
scanline.top()[i] = bgr555;
}
fn copyToSpriteBuffer(bldcnt: io.BldCnt, scanline: *Scanline, x: u9, bgr555: u16) void {
if (bldcnt.mode.read() != 0b00) {
// Alpha Blending
const a_layers = bldcnt.layer_a.read();
const is_blend_enabled = (a_layers >> 4) & 1 == 1;
if (is_blend_enabled) {
scanline.btm()[x] = bgr555;
return;
}
}
scanline.top()[x] = bgr555;
}
const Scanline = struct {
const Self = @This();