|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2022 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // プレーン VRAM (垂直 VRAM)
9: // Lunafb と X68k TVRAM の共通部分
10: //
11:
12: #include "planevram.h"
13: #include "mybswap.h"
14:
15: // コンストラクタ
1.1.1.2 root 16: PlaneVRAMDevice::PlaneVRAMDevice(int width_, int height_)
17: : inherited(OBJ_PLANEVRAM)
1.1 root 18: {
1.1.1.2 root 19: // ポカ避け。継承クラス側が設定する。
1.1 root 20: nplane = -1;
21:
22: composite.Create(width_, height_);
23:
1.1.1.2 root 24: dirty.resize(height_);
1.1 root 25:
26: // レンダリング時に使うテーブルを事前に計算。
27: InitDeptable();
1.1.1.3 ! root 28:
! 29: #if defined(HAVE_AVX2)
! 30: enable_avx2 = gMainApp.enable_avx2;
! 31: #endif
1.1 root 32: }
33:
34: // デストラクタ
35: PlaneVRAMDevice::~PlaneVRAMDevice()
36: {
37: }
38:
39: // リセット
40: void
41: PlaneVRAMDevice::ResetHard(bool poweron)
42: {
43: // XXX ここ?
44: Invalidate();
45: }
46:
47: // レンダリング時に使うテーブルを事前に計算。
48: void
49: PlaneVRAMDevice::InitDeptable()
50: {
51: deptable.reset(new uint64[256]);
52:
53: // %abcdefgh の8ビットを
54: // リトルエンディアンホストでは
55: // %0000000h'0000000g'0000000f'0000000e'0000000d'0000000c'0000000b'0000000a
56: // ビッグエンディアンホストでは
57: // %0000000a'0000000b'0000000c'0000000d'0000000e'0000000f'0000000g'0000000h
58: // の64ビットに伸張する。
59: // (8ビットを各バイトの最下位ビットに対応させる)
60: for (int i = 0; i < 256; i++) {
61: uint64 res = 0;
62: for (int bit = 0; bit < 8; bit++) {
63: int n = bit * 8;
64: #if BYTE_ORDER == LITTLE_ENDIAN
65: n = 56 - n;
66: #endif
67: res |= (uint64)((i >> bit) & 1) << n;
68: }
69: deptable[i] = res;
70: }
71: }
72:
73: // 全画面の更新フラグを立てる
74: void
75: PlaneVRAMDevice::Invalidate()
76: {
77: std::fill(dirty.begin(), dirty.end(), 1);
78: }
79:
80: // 全画面の更新フラグを下ろす
81: // VM スレッドから呼ばれる。
82: void
83: PlaneVRAMDevice::ClearDirty()
84: {
85: std::fill(dirty.begin(), dirty.end(), 0);
86: }
87:
88: // 更新情報を取得。
89: // VM スレッドから呼ばれる。
90: ModifyInfo
91: PlaneVRAMDevice::GetModify() const
92: {
93: ModifyInfo mod;
1.1.1.2 root 94: memcpy(&mod.bits[0], &dirty[0], mod.bits.size());
95: // TODO: 互換性のために n_dirty 計算。廃止するかも。
96: for (int i = 0; i < dirty.size(); i++) {
97: if (dirty[i]) {
98: mod.n_dirty++;
1.1 root 99: }
100: }
101:
102: return mod;
103: }
104:
105: // 画面合成。
106: // レンダラスレッドから呼ばれる。
107: bool
1.1.1.3 ! root 108: PlaneVRAMDevice::Render(BitmapRGBX& dst, const ModifyInfo& modify)
1.1 root 109: {
110: bool updated = false;
111:
112: // VRAM に更新があれば composite を更新
113: if (modify.IsDirty()) {
114: RenderVRAMToComposite(modify);
115: updated = true;
116: }
117:
118: // composite に更新があるか(update)、
1.1.1.3 ! root 119: // 無条件に更新するか(invalidate2) なら dst を更新。
1.1 root 120: if (updated || modify.invalidate2) {
1.1.1.3 ! root 121: RenderCompositeToRGBX(dst, modify);
1.1 root 122: updated = true;
123: }
124:
125: return updated;
126: }
127:
128: // VRAM から composite 画面を合成する。
129: // レンダラスレッドから呼ばれる。
130: void
131: PlaneVRAMDevice::RenderVRAMToComposite(const ModifyInfo& modify)
132: {
133: const int width = composite.GetWidth();
134: const int height = composite.GetHeight();
135: const int planesz = (width / 8) * height;
136:
1.1.1.2 root 137: // 1bpp はパレット的にはほぼ 4bpp と同じ扱いになる。
138: const int nplane4 = (nplane == 1) ? 4 : nplane;
139:
1.1 root 140: // テキスト合成画面を描く
1.1.1.2 root 141: for (int y = 0; y < height; y++) {
142: uint8 mod = modify.bits[y];
143: if (mod == 0) {
144: continue;
145: }
146:
147: const uint32 *src = (const uint32 *)&mem[0];
148: src += y * width / 32;
1.1.1.3 ! root 149: uint64 *dst = (uint64 *)composite.GetRowPtr(y);
1.1.1.2 root 150:
151: for (int x = 0; x < width / 32; x++) {
152: std::array<uint32, 8> data;
153:
154: // VRAM はホストエンディアンに関係なく 32bit で読み込んだ
155: // 時の MSB が左端ピクセル。
156: // ここでは左側から 8ピクセル(8ビット)ずつ 4回処理する
157: // のを最下位バイトから順に右シフトで行いたいので、
158: // (バイト内のビット順は維持したまま) バイトスワップする。
159: //
160: // 31 24 23 16 15 8 7 0
161: // +----------+----------+----------+----------+
162: // VRAM |+00 .. +07|+08 .. +15|+16 .. +23|+24 .. +31|
163: // +----------+----------+----------+----------+
164: // ↓
165: // +----------+----------+----------+----------+
166: // data[] |+24 .. +31|+16 .. +23|+08 .. +15|+00 .. +07|
167: // +----------+----------+----------+----------+
168: if (__predict_false(nplane == 1)) {
169: // LUNA 1bpp の場合残りの3プレーンは ff で埋める。
170: // 実際にも存在しないプレーンは ff が読み出せて
171: // カラーインデックスは $e か $f の二択になるので。
172: data[0] = bswap32(src[0]);
173: data[1] = 0xffffffff;
174: data[2] = 0xffffffff;
175: data[3] = 0xffffffff;
176: } else {
177: int offset = planesz / sizeof(uint32);
178: for (int plane = 0; plane < nplane; plane++) {
179: data[plane] = bswap32(src[plane * offset]);
180: }
1.1 root 181: }
1.1.1.2 root 182: src++;
1.1 root 183:
1.1.1.2 root 184: // data[] の下位 8bit ずつを取り出して deptable[] で変換。
185: // p0..p3 は1バイト x 8個。
186: //
187: // data[] = %xxxxxxxx'xxxxxxxx'xxxxxxxx'ABCDEFGH
188: // ↓
189: // (リトルエンディアンホストの場合)
190: // p.H = %0000000H'0000000G'0000000F'0000000E
191: // p.L = %0000000D'0000000C'0000000B'0000000A
192: for (int b = 0; b < 4; b++) {
193: uint64 cc = 0;
194:
195: for (int plane = 0; plane < nplane4; plane++) {
196: uint64 p;
197: p = deptable[data[plane] & 0xff];
198: p <<= plane;
199: cc |= p;
200: data[plane] >>= 8;
1.1 root 201: }
1.1.1.2 root 202:
203: // cc (64bit = 8バイト) は各バイトが各ピクセルの
204: // カラーインデックスになっているので
205: // BitmapI8 に 8バイト一気に書き込める。
206: // (エンディアンの影響は deptable[] で処理してある)
207: *dst++ = cc;
1.1 root 208: }
209: }
210: }
211: }
212:
1.1.1.3 ! root 213: // composite 画面とパレット情報から RGBX 画面を合成する。
1.1 root 214: // modify は composite 上での変更箇所を示している。
215: // レンダラスレッドから呼ばれる。
216: void
1.1.1.3 ! root 217: PlaneVRAMDevice::RenderCompositeToRGBX(BitmapRGBX& dst,
1.1 root 218: const ModifyInfo& modify)
219: {
1.1.1.3 ! root 220: const int width = dst.GetWidth();
! 221: const int height = dst.GetHeight();
1.1 root 222:
1.1.1.2 root 223: // view_mod は表示領域 view における更新ラスタ
1.1 root 224: BitmapI8 view(width, height);
1.1.1.2 root 225: std::vector<uint8> view_mod(height);
1.1 root 226:
227: // この view に対する modify を計算する
228: if (__predict_false(modify.invalidate2)) {
1.1.1.2 root 229: std::fill(view_mod.begin(), view_mod.end(), 1);
1.1 root 230: } else {
231: // Y scroll 方向の modify 計算
1.1.1.2 root 232: int sy = yscroll;
233: for (int y = 0; y < height; y++, sy++) {
234: if (sy >= composite.GetHeight()) {
235: sy -= composite.GetHeight();
1.1 root 236: }
237: view_mod[y] = modify.bits[sy];
238: }
239:
1.1.1.2 root 240: // 右がはみ出す場合は、はみ出した先のラスタも加味する。
241: if (xscroll + view.GetWidth() > composite.GetWidth()) {
242: sy = yscroll;
243: for (int y = 0; y < height; y++, sy++) {
244: if (sy >= composite.GetHeight()) {
245: sy -= composite.GetHeight();
246: }
247: int sy2 = GetWrapY(sy);
248: view_mod[y] |= modify.bits[sy2];
1.1 root 249: }
250: }
251: }
252:
253: // composite からスクロールを加味した表示領域 view を作る
1.1.1.2 root 254: int sy = yscroll;
255: for (int y = 0; y < height; y++, sy++) {
256: if (sy >= composite.GetHeight()) {
257: sy -= composite.GetHeight();
258: }
259: if (view_mod[y] == 0) {
260: continue;
261: }
1.1 root 262:
1.1.1.3 ! root 263: uint8 *v = view.GetRowPtr(y);
1.1.1.2 root 264: uint8 *c = composite.GetPtr(xscroll, sy);
265: if (xscroll + view.GetWidth() <= composite.GetWidth()) {
266: memcpy(v, c, view.GetWidth());
267: } else {
268: // はみ出しの折り返し
269: int len1 = composite.GetWidth() - xscroll;
270: memcpy(v, c, len1);
271: v += len1;
272: int sy2 = GetWrapY(sy);
1.1.1.3 ! root 273: c = composite.GetRowPtr(sy2);
1.1.1.2 root 274: int len2 = view.GetWidth() - len1;
275: memcpy(v, c, len2);
1.1 root 276: }
277: }
278:
1.1.1.3 ! root 279: // view (I8) を Bitmap (RGBX) に展開する
! 280: #if defined(HAVE_AVX2)
! 281: if (__predict_true(enable_avx2)) {
! 282: RenderI8toRGBX_avx2(dst, view, view_mod);
! 283: } else
! 284: #endif
! 285: {
! 286: RenderI8toRGBX_gen(dst, view, view_mod);
! 287: }
! 288: }
! 289:
! 290: // I8 から RGBX への変換。(C++ 版)
! 291: void
! 292: PlaneVRAMDevice::RenderI8toRGBX_gen(BitmapRGBX& dst, const BitmapI8& view,
! 293: const std::vector<uint8>& view_mod)
! 294: {
1.1.1.2 root 295: for (int y = 0; y < view_mod.size(); y++) {
296: if (view_mod[y] == 0) {
297: continue;
298: }
299:
1.1.1.3 ! root 300: const uint8 *s8 = view.GetRowPtr(y);
! 301: uint32 *d = (uint32 *)dst.GetRowPtr(y);
1.1 root 302:
1.1.1.2 root 303: const uint32 *s = (const uint32 *)s8;
304: const uint32 *send = (const uint32 *)(s8 + view.GetWidth());
305: for (; s < send;) {
306: // 4ピクセルずつ処理する
1.1 root 307:
1.1.1.3 ! root 308: // s は BitmapI8 なのでビッグエンディアン並びに相当。
1.1.1.2 root 309: uint32 cc4 = *s++;
310: // 左側ピクセルが下位バイトになるよう並び替える
1.1.1.3 ! root 311: cc4 = htole32(cc4);
1.1.1.2 root 312:
313: // 4ピクセルをそれぞれ xBGR32 に変換
314: uint32 c0 = palette[ cc4 & 0xff].xbgr;
315: uint32 c1 = palette[(cc4 >> 8) & 0xff].xbgr;
316: uint32 c2 = palette[(cc4 >> 16) & 0xff].xbgr;
317: uint32 c3 = palette[ cc4 >> 24 ].xbgr;
318:
1.1.1.3 ! root 319: // c0 = $x0B0G0R0
! 320: // c1 = $x1B1G1R1
! 321: // c2 = $x2B2G2R2
! 322: // c3 = $x3B3G3R3
! 323: *d++ = le32toh(c0);
! 324: *d++ = le32toh(c1);
! 325: *d++ = le32toh(c2);
! 326: *d++ = le32toh(c3);
1.1 root 327: }
328: }
329: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.