--- nono/vm/planevram.cpp 2026/04/29 17:05:25 1.1.1.3 +++ nono/vm/planevram.cpp 2026/04/29 17:05:42 1.1.1.6 @@ -11,6 +11,15 @@ #include "planevram.h" #include "mybswap.h" +#include +#include + +// v[] がいずれかでも true なら true を返す。 +static bool +AnyOf(const std::vector& v) +{ + return std::any_of(v.begin(), v.end(), [](bool x) { return x; }); +} // コンストラクタ PlaneVRAMDevice::PlaneVRAMDevice(int width_, int height_) @@ -21,35 +30,16 @@ PlaneVRAMDevice::PlaneVRAMDevice(int wid composite.Create(width_, height_); - dirty.resize(height_); - - // レンダリング時に使うテーブルを事前に計算。 - InitDeptable(); - -#if defined(HAVE_AVX2) - enable_avx2 = gMainApp.enable_avx2; -#endif -} - -// デストラクタ -PlaneVRAMDevice::~PlaneVRAMDevice() -{ -} - -// リセット -void -PlaneVRAMDevice::ResetHard(bool poweron) -{ - // XXX ここ? - Invalidate(); + dirty.line.resize(height_); + pending.line.resize(height_); } -// レンダリング時に使うテーブルを事前に計算。 -void -PlaneVRAMDevice::InitDeptable() +// 初期化 +bool +PlaneVRAMDevice::Init() { - deptable.reset(new uint64[256]); - + // レンダリング時に使うテーブルを事前に計算。 + // // %abcdefgh の8ビットを // リトルエンディアンホストでは // %0000000h'0000000g'0000000f'0000000e'0000000d'0000000c'0000000b'0000000a @@ -57,7 +47,14 @@ PlaneVRAMDevice::InitDeptable() // %0000000a'0000000b'0000000c'0000000d'0000000e'0000000f'0000000g'0000000h // の64ビットに伸張する。 // (8ビットを各バイトの最下位ビットに対応させる) - for (int i = 0; i < 256; i++) { + constexpr uint num = 256; + deptable.reset(new(std::nothrow) uint64[num]); + if ((bool)deptable == false) { + warnx("Cannot allocate %zu bytes at %s", + sizeof(uint64) * num, __method__); + return false; + } + for (uint i = 0; i < num; i++) { uint64 res = 0; for (int bit = 0; bit < 8; bit++) { int n = bit * 8; @@ -68,57 +65,97 @@ PlaneVRAMDevice::InitDeptable() } deptable[i] = res; } + + return true; +} + +// デストラクタ +PlaneVRAMDevice::~PlaneVRAMDevice() +{ +} + +// リセット +void +PlaneVRAMDevice::ResetHard(bool poweron) +{ + // XXX ここ? + Invalidate(); } // 全画面の更新フラグを立てる void PlaneVRAMDevice::Invalidate() { - std::fill(dirty.begin(), dirty.end(), 1); + std::fill(dirty.line.begin(), dirty.line.end(), 1); } -// 全画面の更新フラグを下ろす -// VM スレッドから呼ばれる。 +// VRAM には更新がないが再レンダリングが必要。 +// パレット変更、スクロール等。VM スレッドから呼ばれる。 void -PlaneVRAMDevice::ClearDirty() +PlaneVRAMDevice::Invalidate2() { - std::fill(dirty.begin(), dirty.end(), 0); + dirty.invalidate2 = true; } -// 更新情報を取得。 -// VM スレッドから呼ばれる。 -ModifyInfo -PlaneVRAMDevice::GetModify() const -{ - ModifyInfo mod; - memcpy(&mod.bits[0], &dirty[0], mod.bits.size()); - // TODO: 互換性のために n_dirty 計算。廃止するかも。 - for (int i = 0; i < dirty.size(); i++) { - if (dirty[i]) { - mod.n_dirty++; +// 画面更新する必要があるか。 +// dirty から pending を更新してみて、更新が一つでもあれば true を返す。 +// VM スレッドで呼ばれる。 +bool +PlaneVRAMDevice::Snap() +{ + const uint height = composite.GetHeight(); + bool update_needed; + + // 更新 (dirty) があれば pending に重ねる。 + { + std::unique_lock lock(mtx); + for (int y = 0; y < height; y++) { + // std::vector には operator|=() がない。 + if (__predict_false(dirty.line[y])) { + pending.line[y] = true; + dirty.line[y] = false; + } + } + + if (__predict_false(dirty.invalidate2)) { + pending.invalidate2 = true; + dirty.invalidate2 = false; } + + update_needed = AnyOf(pending.line) || pending.invalidate2; } - return mod; + return update_needed; } // 画面合成。 // レンダラスレッドから呼ばれる。 +// dst を更新すれば true を返す。 bool -PlaneVRAMDevice::Render(BitmapRGBX& dst, const ModifyInfo& modify) +PlaneVRAMDevice::Render(BitmapRGBX& dst) { bool updated = false; - // VRAM に更新があれば composite を更新 - if (modify.IsDirty()) { - RenderVRAMToComposite(modify); + // ローカルにコピー。 + ModifyInfo modified; + { + std::unique_lock lock(mtx); + modified.line = pending.line; + std::fill(pending.line.begin(), pending.line.end(), false); + modified.invalidate2 = pending.invalidate2; + pending.invalidate2 = false; + } + + // VRAM に更新があれば composite を更新。 + if (AnyOf(modified.line)) { + RenderVRAMToComposite(modified.line); updated = true; } - // composite に更新があるか(update)、 - // 無条件に更新するか(invalidate2) なら dst を更新。 - if (updated || modify.invalidate2) { - RenderCompositeToRGBX(dst, modify); + // composite に更新があるか(updated)、 + // 無条件に更新するか(modified.invalidate2) なら dst を更新。 + if (updated || modified.invalidate2) { + RenderCompositeToRGBX(dst, modified); updated = true; } @@ -128,19 +165,19 @@ PlaneVRAMDevice::Render(BitmapRGBX& dst, // VRAM から composite 画面を合成する。 // レンダラスレッドから呼ばれる。 void -PlaneVRAMDevice::RenderVRAMToComposite(const ModifyInfo& modify) +PlaneVRAMDevice::RenderVRAMToComposite(const std::vector& modified) { - const int width = composite.GetWidth(); - const int height = composite.GetHeight(); - const int planesz = (width / 8) * height; + const uint width = composite.GetWidth(); + const uint height = composite.GetHeight(); + const uint planesz = (width / 8) * height; // 1bpp はパレット的にはほぼ 4bpp と同じ扱いになる。 const int nplane4 = (nplane == 1) ? 4 : nplane; // テキスト合成画面を描く - for (int y = 0; y < height; y++) { - uint8 mod = modify.bits[y]; - if (mod == 0) { + for (uint y = 0; y < height; y++) { + bool mod = modified[y]; + if (mod == false) { continue; } @@ -148,7 +185,7 @@ PlaneVRAMDevice::RenderVRAMToComposite(c src += y * width / 32; uint64 *dst = (uint64 *)composite.GetRowPtr(y); - for (int x = 0; x < width / 32; x++) { + for (uint x = 0; x < width / 32; x++) { std::array data; // VRAM はホストエンディアンに関係なく 32bit で読み込んだ @@ -174,7 +211,7 @@ PlaneVRAMDevice::RenderVRAMToComposite(c data[2] = 0xffffffff; data[3] = 0xffffffff; } else { - int offset = planesz / sizeof(uint32); + uint offset = planesz / sizeof(uint32); for (int plane = 0; plane < nplane; plane++) { data[plane] = bswap32(src[plane * offset]); } @@ -211,21 +248,21 @@ PlaneVRAMDevice::RenderVRAMToComposite(c } // composite 画面とパレット情報から RGBX 画面を合成する。 -// modify は composite 上での変更箇所を示している。 +// modified は composite 上での変更箇所を示している。 // レンダラスレッドから呼ばれる。 void PlaneVRAMDevice::RenderCompositeToRGBX(BitmapRGBX& dst, - const ModifyInfo& modify) + const ModifyInfo& modified) { - const int width = dst.GetWidth(); - const int height = dst.GetHeight(); + const uint width = dst.GetWidth(); + const uint height = dst.GetHeight(); // view_mod は表示領域 view における更新ラスタ BitmapI8 view(width, height); std::vector view_mod(height); // この view に対する modify を計算する - if (__predict_false(modify.invalidate2)) { + if (__predict_false(modified.invalidate2)) { std::fill(view_mod.begin(), view_mod.end(), 1); } else { // Y scroll 方向の modify 計算 @@ -234,7 +271,7 @@ PlaneVRAMDevice::RenderCompositeToRGBX(B if (sy >= composite.GetHeight()) { sy -= composite.GetHeight(); } - view_mod[y] = modify.bits[sy]; + view_mod[y] = modified.line[sy]; } // 右がはみ出す場合は、はみ出した先のラスタも加味する。 @@ -245,7 +282,7 @@ PlaneVRAMDevice::RenderCompositeToRGBX(B sy -= composite.GetHeight(); } int sy2 = GetWrapY(sy); - view_mod[y] |= modify.bits[sy2]; + view_mod[y] |= modified.line[sy2]; } } } @@ -277,53 +314,5 @@ PlaneVRAMDevice::RenderCompositeToRGBX(B } // view (I8) を Bitmap (RGBX) に展開する -#if defined(HAVE_AVX2) - if (__predict_true(enable_avx2)) { - RenderI8toRGBX_avx2(dst, view, view_mod); - } else -#endif - { - RenderI8toRGBX_gen(dst, view, view_mod); - } -} - -// I8 から RGBX への変換。(C++ 版) -void -PlaneVRAMDevice::RenderI8toRGBX_gen(BitmapRGBX& dst, const BitmapI8& view, - const std::vector& view_mod) -{ - for (int y = 0; y < view_mod.size(); y++) { - if (view_mod[y] == 0) { - continue; - } - - const uint8 *s8 = view.GetRowPtr(y); - uint32 *d = (uint32 *)dst.GetRowPtr(y); - - const uint32 *s = (const uint32 *)s8; - const uint32 *send = (const uint32 *)(s8 + view.GetWidth()); - for (; s < send;) { - // 4ピクセルずつ処理する - - // s は BitmapI8 なのでビッグエンディアン並びに相当。 - uint32 cc4 = *s++; - // 左側ピクセルが下位バイトになるよう並び替える - cc4 = htole32(cc4); - - // 4ピクセルをそれぞれ xBGR32 に変換 - uint32 c0 = palette[ cc4 & 0xff].xbgr; - uint32 c1 = palette[(cc4 >> 8) & 0xff].xbgr; - uint32 c2 = palette[(cc4 >> 16) & 0xff].xbgr; - uint32 c3 = palette[ cc4 >> 24 ].xbgr; - - // c0 = $x0B0G0R0 - // c1 = $x1B1G1R1 - // c2 = $x2B2G2R2 - // c3 = $x3B3G3R3 - *d++ = le32toh(c0); - *d++ = le32toh(c1); - *d++ = le32toh(c2); - *d++ = le32toh(c3); - } - } + dst.DrawBitmapI8Raster(view, palette, &view_mod[0]); }