--- nono/vm/planevram.cpp 2026/04/29 17:05:17 1.1.1.2 +++ nono/vm/planevram.cpp 2026/04/29 17:05:50 1.1.1.7 @@ -11,9 +11,9 @@ #include "planevram.h" #include "mybswap.h" -#if defined(__x86_64__) -#include -#endif +#include +#include +#include // コンストラクタ PlaneVRAMDevice::PlaneVRAMDevice(int width_, int height_) @@ -23,32 +23,14 @@ PlaneVRAMDevice::PlaneVRAMDevice(int wid nplane = -1; composite.Create(width_, height_); - - dirty.resize(height_); - - // レンダリング時に使うテーブルを事前に計算。 - InitDeptable(); -} - -// デストラクタ -PlaneVRAMDevice::~PlaneVRAMDevice() -{ -} - -// リセット -void -PlaneVRAMDevice::ResetHard(bool poweron) -{ - // XXX ここ? - Invalidate(); } -// レンダリング時に使うテーブルを事前に計算。 -void -PlaneVRAMDevice::InitDeptable() +// 初期化 +bool +PlaneVRAMDevice::Init() { - deptable.reset(new uint64[256]); - + // レンダリング時に使うテーブルを事前に計算。 + // // %abcdefgh の8ビットを // リトルエンディアンホストでは // %0000000h'0000000g'0000000f'0000000e'0000000d'0000000c'0000000b'0000000a @@ -56,7 +38,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; @@ -67,57 +56,91 @@ 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); + dirty.line.set(); } -// 全画面の更新フラグを下ろす -// VM スレッドから呼ばれる。 +// VRAM には更新がないが再レンダリングが必要。 +// パレット変更、スクロール等。VM スレッドから呼ばれる。 void -PlaneVRAMDevice::ClearDirty() +PlaneVRAMDevice::Invalidate2() { - std::fill(dirty.begin(), dirty.end(), 0); + dirty.invalidate2 = true; } -// 更新情報を取得。 -// VM スレッドから呼ばれる。 -ModifyInfo -PlaneVRAMDevice::GetModify() const +// 画面更新する必要があるか。 +// dirty から pending を更新してみて、更新が一つでもあれば true を返す。 +// VM スレッドで呼ばれる。 +bool +PlaneVRAMDevice::Snap() { - 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++; + bool update_needed; + + // 更新 (dirty) があれば pending に重ねる。 + { + std::lock_guard lock(mtx); + pending.line |= dirty.line; + dirty.line.reset(); + + if (__predict_false(dirty.invalidate2)) { + pending.invalidate2 = true; + dirty.invalidate2 = false; } + + update_needed = pending.line.any() || pending.invalidate2; } - return mod; + return update_needed; } // 画面合成。 // レンダラスレッドから呼ばれる。 +// dst を更新すれば true を返す。 bool -PlaneVRAMDevice::Render(BitmapRGB& bitmap, const ModifyInfo& modify) +PlaneVRAMDevice::Render(BitmapRGBX& dst) { bool updated = false; - // VRAM に更新があれば composite を更新 - if (modify.IsDirty()) { - RenderVRAMToComposite(modify); + // ローカルにコピー。 + ModifyInfo modified; + { + std::lock_guard lock(mtx); + modified.line = pending.line; + modified.invalidate2 = pending.invalidate2; + pending.line.reset(); + pending.invalidate2 = false; + } + + // VRAM に更新があれば composite を更新。 + if (modified.line.any()) { + RenderVRAMToComposite(modified.line); updated = true; } - // composite に更新があるか(update)、 - // 無条件に更新するか(invalidate2) なら bitmap を更新。 - if (updated || modify.invalidate2) { - RenderCompositeToRGB(bitmap, modify); + // composite に更新があるか(updated)、 + // 無条件に更新するか(modified.invalidate2) なら dst を更新。 + if (updated || modified.invalidate2) { + RenderCompositeToRGBX(dst, modified); updated = true; } @@ -127,27 +150,26 @@ PlaneVRAMDevice::Render(BitmapRGB& bitma // VRAM から composite 画面を合成する。 // レンダラスレッドから呼ばれる。 void -PlaneVRAMDevice::RenderVRAMToComposite(const ModifyInfo& modify) +PlaneVRAMDevice::RenderVRAMToComposite(const bitset1024& 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++) { + if (modified.test(y) == false) { continue; } const uint32 *src = (const uint32 *)&mem[0]; src += y * width / 32; - uint64 *dst = (uint64 *)composite.GetPtr(0, y); + 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 で読み込んだ @@ -173,7 +195,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]); } @@ -209,22 +231,22 @@ PlaneVRAMDevice::RenderVRAMToComposite(c } } -// composite 画面とパレット情報から RGB 画面を合成する。 -// modify は composite 上での変更箇所を示している。 +// composite 画面とパレット情報から RGBX 画面を合成する。 +// modified は composite 上での変更箇所を示している。 // レンダラスレッドから呼ばれる。 void -PlaneVRAMDevice::RenderCompositeToRGB(BitmapRGB& bitmap, - const ModifyInfo& modify) +PlaneVRAMDevice::RenderCompositeToRGBX(BitmapRGBX& dst, + const ModifyInfo& modified) { - const int width = bitmap.GetWidth(); - const int height = bitmap.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 計算 @@ -233,7 +255,7 @@ PlaneVRAMDevice::RenderCompositeToRGB(Bi if (sy >= composite.GetHeight()) { sy -= composite.GetHeight(); } - view_mod[y] = modify.bits[sy]; + view_mod[y] = modified.line[sy]; } // 右がはみ出す場合は、はみ出した先のラスタも加味する。 @@ -244,7 +266,7 @@ PlaneVRAMDevice::RenderCompositeToRGB(Bi sy -= composite.GetHeight(); } int sy2 = GetWrapY(sy); - view_mod[y] |= modify.bits[sy2]; + view_mod[y] |= modified.line[sy2]; } } } @@ -259,7 +281,7 @@ PlaneVRAMDevice::RenderCompositeToRGB(Bi continue; } - uint8 *v = view.GetPtr(0, y); + uint8 *v = view.GetRowPtr(y); uint8 *c = composite.GetPtr(xscroll, sy); if (xscroll + view.GetWidth() <= composite.GetWidth()) { memcpy(v, c, view.GetWidth()); @@ -269,96 +291,12 @@ PlaneVRAMDevice::RenderCompositeToRGB(Bi memcpy(v, c, len1); v += len1; int sy2 = GetWrapY(sy); - c = composite.GetPtr(0, sy2); + c = composite.GetRowPtr(sy2); int len2 = view.GetWidth() - len1; memcpy(v, c, len2); } } - // view (I8) を Bitmap (RGB) に展開する - for (int y = 0; y < view_mod.size(); y++) { - if (view_mod[y] == 0) { - continue; - } - - const uint8 *s8 = view.GetPtr(0, y); - uint32 *d = (uint32 *)bitmap.GetPtr(0, y); - - const uint32 *s = (const uint32 *)s8; - const uint32 *send = (const uint32 *)(s8 + view.GetWidth()); - for (; s < send;) { - // s は BitmapI8 なのでビッグエンディアン並びに相当 - -#if defined(__AVX2__) && !defined(NO_AVX) // オプション -mavx2 が必要 - // 35.1msec - - // 8ピクセルずつ処理する - - // idx0 = $00000000'00000000'I7I6I5I4'I3I2I1I0 - __m128i idx0 = _mm_loadl_epi64((const __m128i *)s); - s += 2; - - // idx = $000000I7'000000I6'000000I5'000000I4' - // 000000I3'000000I2'000000I1'000000I0 - __m256i idx = _mm256_cvtepu8_epi32(idx0); - - // 8個の 32bit インデックスから XBGR を取得 - // c = $00B7G7R7'00B6G6R6'00B5G5R5'00B4G4R4' - // 00B3G3R3'00B2G2R2'00B1G1R1'00B0G0R0 - __m256i c = _mm256_i32gather_epi32( - (const int *)palette, idx, 4); - - // 256bit リニアに使えれば下詰めで済んだんだが、 - // 上位 128bit と下位 128bit をまたいでシャッフルは - // 出来なくて、かつこの後のストアは連続領域への - // 書き込みになるので、中央でつなげておく。うーん。 - // c = $00000000'B7G7R7B6'G6R6B5G5'R5B4G4R4' - // B3G3R3B2'G2R2B1G1'R1B0G0R0'00000000 - __m256i shuff = _mm256_set_epi8( - -1,-1,-1,-1, 14,13,12,10, 9, 8, 6, 5, 4, 2, 1, 0, - 14,13,12,10, 9, 8, 6, 5, 4, 2, 1, 0, -1,-1,-1,-1 - ); - __m256i res = _mm256_shuffle_epi8(c, shuff); - - // 先頭(下位)と末尾(上位)の 32bit ずつを除いたところを - // 書き出す。 - __m256i mask = _mm256_set_epi32( - 0, -1, -1, -1, -1, -1, -1, 0 - ); - _mm256_maskstore_epi32((int *)(d - 1), mask, res); - d += 6; -#else - // 46.5msec - - // 4ピクセルずつ処理する - - uint32 cc4 = *s++; - // 左側ピクセルが下位バイトになるよう並び替える - // (le32toh() はビッグエンディアンなら bswap32 の意) - cc4 = le32toh(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; - - // xBGR32 4つ(16バイト)を RGB24 3つ(12バイト)に変換して - // ロングワードで書き出す。 - // c0 = $x0B0G0R0 - // c1 = $x1B1G1R1 - // c2 = $x2B2G2R2 - // c3 = $x3B3G3R3 - // - // d[0] = $00B0G0R0 | $R1000000 = $R1B0G0R0 - // d[1] = $0000B1G1 | $G2R20000 = $G2R2B1G1 - // d[2] = $000000B2 | $B3G3R300 = $B3G3R3B2 - - // (le32toh() はビッグエンディアンなら bswap32 の意) - *d++ = le32toh((c0 ) | (c1 << 24)); - *d++ = le32toh((c1 >> 8) | (c2 << 16)); - *d++ = le32toh((c2 >> 16) | (c3 << 8)); -#endif - } - } + // view (I8) を Bitmap (RGBX) に展開する + dst.DrawBitmapI8Raster(view, palette, &view_mod[0]); }