openMSX
ImGuiBitmapViewer.cc
Go to the documentation of this file.
2
3#include "ImGuiCpp.hh"
4#include "ImGuiManager.hh"
5#include "ImGuiPalette.hh"
6#include "ImGuiUtils.hh"
7
8#include "DisplayMode.hh"
9#include "VDP.hh"
10#include "VDPVRAM.hh"
11
12#include "MemBuffer.hh"
13#include "ranges.hh"
14
15#include <imgui.h>
16
17namespace openmsx {
18
19using namespace std::literals;
20
21void ImGuiBitmapViewer::save(ImGuiTextBuffer& buf)
22{
23 savePersistent(buf, *this, persistentElements);
24}
25
26void ImGuiBitmapViewer::loadLine(std::string_view name, zstring_view value)
27{
28 loadOnePersistent(name, value, *this, persistentElements);
29}
30
32{
33 if (!showBitmapViewer) return;
34 if (!motherBoard) return;
35
36 ImGui::SetNextWindowSize({528, 618}, ImGuiCond_FirstUseEver);
37 im::Window("Bitmap viewer", &showBitmapViewer, [&]{
38 auto* vdp = dynamic_cast<VDP*>(motherBoard->findDevice("VDP")); // TODO name based OK?
39 if (!vdp) return;
40
41 auto parseMode = [](DisplayMode mode) {
42 auto base = mode.getBase();
43 if (base == DisplayMode::GRAPHIC4) return SCR5;
44 if (base == DisplayMode::GRAPHIC5) return SCR6;
45 if (base == DisplayMode::GRAPHIC6) return SCR7;
46 if (base != DisplayMode::GRAPHIC7) return OTHER;
47 if (mode.getByte() & DisplayMode::YJK) {
48 if (mode.getByte() & DisplayMode::YAE) {
49 return SCR11;
50 } else {
51 return SCR12;
52 }
53 } else {
54 return SCR8;
55 }
56 };
57 int vdpMode = parseMode(vdp->getDisplayMode());
58
59 int vdpPages = vdpMode <= SCR6 ? 4 : 2;
60 int vdpPage = vdp->getDisplayPage();
61 if (vdpPage >= vdpPages) vdpPage &= 1;
62
63 int vdpLines = (vdp->getNumberOfLines() == 192) ? 0 : 1;
64
65 int vdpColor0 = [&]{
66 if (vdpMode == one_of(SCR8, SCR11, SCR12) || !vdp->getTransparency()) {
67 return 16; // no replacement
68 }
69 return vdp->getBackgroundColor() & 15;
70 }();
71
72 auto modeToStr = [](int mode) {
73 if (mode == SCR5 ) return "screen 5";
74 if (mode == SCR6 ) return "screen 6";
75 if (mode == SCR7 ) return "screen 7";
76 if (mode == SCR8 ) return "screen 8";
77 if (mode == SCR11) return "screen 11";
78 if (mode == SCR12) return "screen 12";
79 if (mode == OTHER) return "non-bitmap";
80 assert(false); return "ERROR";
81 };
82
83 static const char* const color0Str = "0\0001\0002\0003\0004\0005\0006\0007\0008\0009\00010\00011\00012\00013\00014\00015\000none\000";
84 bool manualMode = overrideAll || overrideMode;
85 bool manualPage = overrideAll || overridePage;
86 bool manualLines = overrideAll || overrideLines;
87 bool manualColor0 = overrideAll || overrideColor0;
88 im::Group([&]{
89 ImGui::TextUnformatted("VDP settings");
90 im::Disabled(manualMode, [&]{
91 ImGui::AlignTextToFramePadding();
92 ImGui::StrCat("Screen mode: ", modeToStr(vdpMode));
93 });
94 im::Disabled(manualPage, [&]{
95 ImGui::AlignTextToFramePadding();
96 ImGui::StrCat("Display page: ", vdpPage);
97 });
98 im::Disabled(manualLines, [&]{
99 ImGui::AlignTextToFramePadding();
100 ImGui::StrCat("Visible lines: ", vdpLines ? 212 : 192);
101 });
102 im::Disabled(manualColor0, [&]{
103 ImGui::AlignTextToFramePadding();
104 ImGui::StrCat("Replace color 0: ", getComboString(vdpColor0, color0Str));
105 });
106 // TODO interlace
107 });
108 ImGui::SameLine();
109 im::Group([&]{
110 ImGui::Checkbox("Manual override", &overrideAll);
111 im::Group([&]{
112 im::Disabled(overrideAll, [&]{
113 ImGui::Checkbox("##mode", overrideAll ? &overrideAll : &overrideMode);
114 ImGui::Checkbox("##page", overrideAll ? &overrideAll : &overridePage);
115 ImGui::Checkbox("##lines", overrideAll ? &overrideAll : &overrideLines);
116 ImGui::Checkbox("##color0", overrideAll ? &overrideAll : &overrideColor0);
117 });
118 });
119 ImGui::SameLine();
120 im::Group([&]{
121 im::ItemWidth(ImGui::GetFontSize() * 9.0f, [&]{
122 im::Disabled(!manualMode, [&]{
123 ImGui::Combo("##Screen mode", &bitmapScrnMode, "screen 5\000screen 6\000screen 7\000screen 8\000screen 11\000screen 12\000");
124 });
125 im::Disabled(!manualPage, [&]{
126 int numPages = bitmapScrnMode <= SCR6 ? 4 : 2; // TODO extended VRAM
127 if (bitmapPage >= numPages) bitmapPage = numPages - 1;
128 if (bitmapPage < 0) bitmapPage = numPages;
129 ImGui::Combo("##Display page", &bitmapPage, numPages == 2 ? "0\0001\000All\000" : "0\0001\0002\0003\000All\000");
130 if (bitmapPage == numPages) bitmapPage = -1;
131 });
132 im::Disabled(!manualLines || bitmapPage < 0, [&]{
133 ImGui::Combo("##Visible lines", &bitmapLines, "192\000212\000256\000");
134 });
135 im::Disabled(!manualColor0, [&]{
136 ImGui::Combo("##Color 0 replacement", &bitmapColor0, color0Str);
137 });
138 });
139 });
140 });
141
142 ImGui::SameLine();
143 ImGui::Dummy(ImVec2(25, 1));
144 ImGui::SameLine();
145 im::Group([&]{
146 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 10.0f);
147 ImGui::Combo("Palette", &manager.palette->whichPalette, "VDP\000Custom\000Fixed\000");
148 if (ImGui::Button("Open palette editor")) {
149 manager.palette->window.raise();
150 }
151 ImGui::Separator();
152 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 3.0f);
153 ImGui::Combo("Zoom", &bitmapZoom, "1x\0002x\0003x\0004x\0005x\0006x\0007x\0008x\000");
154 ImGui::Checkbox("grid", &bitmapGrid);
155 ImGui::SameLine();
156 im::Disabled(!bitmapGrid, [&]{
157 ImGui::ColorEdit4("Grid color", bitmapGridColor.data(),
158 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaBar);
159 });
160 });
161
162 ImGui::Separator();
163
164 const auto& vram = vdp->getVRAM();
165 int mode = manualMode ? bitmapScrnMode : vdpMode;
166 int page = manualPage ? bitmapPage : vdpPage;
167 int lines = manualLines ? bitmapLines : vdpLines;
168 int color0 = manualColor0 ? bitmapColor0 : vdpColor0;
169 int width = mode == one_of(SCR6, SCR7) ? 512 : 256;
170 int height = (lines == 0) ? 192
171 : (lines == 1) ? 212
172 : 256;
173 if (page < 0) {
174 int numPages = mode <= SCR6 ? 4 : 2;
175 height = 256 * numPages;
176 page = 0;
177 }
178
179 std::array<uint32_t, 16> palette;
180 auto msxPalette = manager.palette->getPalette(vdp);
181 ranges::transform(msxPalette, palette.data(),
182 [](uint16_t msx) { return ImGuiPalette::toRGBA(msx); });
183 if (color0 < 16) palette[0] = palette[color0];
184
185 MemBuffer<uint32_t> pixels(512 * 256 * 2); // max size: 512*256*2 or 256*256*4
186 renderBitmap(vram.getData(), palette, mode, height, page,
187 pixels.data());
188 if (!bitmapTex) {
189 bitmapTex.emplace(false, false); // no interpolation, no wrapping
190 }
191 bitmapTex->bind();
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
193 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
194 int zx = (1 + bitmapZoom) * (width == 256 ? 2 : 1);
195 int zy = (1 + bitmapZoom) * 2;
196
197 gl::vec2 scrnPos;
198 gl::vec2 size(float(width * zx), float(height * zy));
199 gl::vec2 availSize = gl::vec2(ImGui::GetContentRegionAvail()) - gl::vec2(0.0f, ImGui::GetTextLineHeightWithSpacing());
200 gl::vec2 reqSize = size + gl::vec2(ImGui::GetStyle().ScrollbarSize);
201 im::Child("##bitmap", min(availSize, reqSize), 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
202 scrnPos = ImGui::GetCursorScreenPos();
203 auto pos = ImGui::GetCursorPos();
204 ImGui::Image(bitmapTex->getImGui(), size);
205
206 if (bitmapGrid && (zx > 1) && (zy > 1)) {
207 auto color = ImGui::ColorConvertFloat4ToU32(bitmapGridColor);
208 for (auto y : xrange(zy)) {
209 auto* line = &pixels[y * zx];
210 for (auto x : xrange(zx)) {
211 line[x] = (x == 0 || y == 0) ? color : 0;
212 }
213 }
214 if (!bitmapGridTex) {
215 bitmapGridTex.emplace(false, true); // no interpolation, with wrapping
216 }
217 bitmapGridTex->bind();
218 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, zx, zy, 0,
219 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
220 ImGui::SetCursorPos(pos);
221 ImGui::Image(bitmapGridTex->getImGui(), size,
222 ImVec2(0.0f, 0.0f), ImVec2(float(width), float(height)));
223 }
224 });
225 if (ImGui::IsItemHovered() && (mode != OTHER)) {
226 gl::vec2 zoom{float(zx), float(zy)};
227 auto [x_, y_] = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / zoom);
228 auto x = x_; auto y = y_; // clang workaround
229 if ((0 <= x) && (x < width) && (0 <= y) && (y < height)) {
230 auto dec3 = [&](int d) {
232 ImGui::SameLine(0.0f, 0.0f);
233 ImGui::Text("%3d", d);
234 };
235 auto hex2 = [&](unsigned h) {
237 ImGui::SameLine(0.0f, 0.0f);
238 ImGui::StrCat(hex_string<2>(h));
239 };
240 auto hex5 = [&](unsigned h) {
242 ImGui::SameLine(0.0f, 0.0f);
243 ImGui::StrCat(hex_string<5>(h));
244 };
245
246 ImGui::TextUnformatted("x="sv); dec3(x);
247 ImGui::SameLine();
248 ImGui::TextUnformatted("y="sv); dec3(y);
249
250 unsigned physAddr = 0x8000 * page + 128 * y;
251 switch (mode) {
252 case SCR5: physAddr += x / 2; break;
253 case SCR6: physAddr += x / 4; break;
254 case SCR7: physAddr += x / 4 + 0x08000 * (x & 2); break;
255 case SCR8: case SCR11: case SCR12:
256 physAddr += x / 2 + 0x10000 * (x & 1); break;
257 default: assert(false);
258 }
259
260 auto value = vram.getData()[physAddr];
261 auto color = [&]() -> uint8_t {
262 switch (mode) {
263 case SCR5: case SCR7:
264 return (value >> (4 * (1 - (x & 1)))) & 0x0f;
265 case SCR6:
266 return (value >> (2 * (3 - (x & 3)))) & 0x03;
267 default:
268 return value;
269 }
270 }();
271 if (mode != one_of(SCR11, SCR12)) {
272 ImGui::SameLine();
273 ImGui::TextUnformatted(" color="sv); dec3(color);
274 }
275
276 ImGui::SameLine();
277 ImGui::TextUnformatted(" vram: addr=0x");
278 if (mode == one_of(SCR5, SCR6)) {
279 hex5(physAddr);
280 } else {
281 unsigned logAddr = (physAddr & 0x0ffff) << 1 | (physAddr >> 16);
282 hex5(logAddr),
283 ImGui::SameLine(0.0f, 0.0f);
284 ImGui::TextUnformatted("(log)/0x"sv); hex5(physAddr);
285 ImGui::SameLine(0.0f, 0.0f);
286 ImGui::TextUnformatted("(phys)"sv);
287 }
288 ImGui::SameLine();
289 ImGui::TextUnformatted(" value=0x"sv); hex2(value);
290 }
291 }
292 });
293}
294
295// TODO avoid code duplication with src/video/BitmapConverter
296void ImGuiBitmapViewer::renderBitmap(std::span<const uint8_t> vram, std::span<const uint32_t, 16> palette16,
297 int mode, int lines, int page, uint32_t* output) const
298{
299 auto yjk2rgb = [](int y, int j, int k) -> std::tuple<int, int, int> {
300 // Note the formula for 'blue' differs from the 'traditional' formula
301 // (e.g. as specified in the V9958 datasheet) in the rounding behavior.
302 // Confirmed on real turbor machine. For details see:
303 // https://github.com/openMSX/openMSX/issues/1394
304 // https://twitter.com/mdpc___/status/1480432007180341251?s=20
305 int r = std::clamp(y + j, 0, 31);
306 int g = std::clamp(y + k, 0, 31);
307 int b = std::clamp((5 * y - 2 * j - k + 2) / 4, 0, 31);
308 return {r, g, b};
309 };
310
311 // TODO handle less than 128kB VRAM (will crash now)
312 size_t addr = 0x8000 * page;
313 switch (mode) {
314 case SCR5:
315 for (auto y : xrange(lines)) {
316 auto* line = &output[256 * y];
317 for (auto x : xrange(128)) {
318 auto value = vram[addr];
319 line[2 * x + 0] = palette16[(value >> 4) & 0x0f];
320 line[2 * x + 1] = palette16[(value >> 0) & 0x0f];
321 ++addr;
322 }
323 }
324 break;
325
326 case SCR6:
327 for (auto y : xrange(lines)) {
328 auto* line = &output[512 * y];
329 for (auto x : xrange(128)) {
330 auto value = vram[addr];
331 line[4 * x + 0] = palette16[(value >> 6) & 3];
332 line[4 * x + 1] = palette16[(value >> 4) & 3];
333 line[4 * x + 2] = palette16[(value >> 2) & 3];
334 line[4 * x + 3] = palette16[(value >> 0) & 3];
335 ++addr;
336 }
337 }
338 break;
339
340 case SCR7:
341 for (auto y : xrange(lines)) {
342 auto* line = &output[512 * y];
343 for (auto x : xrange(128)) {
344 auto value0 = vram[addr + 0x00000];
345 auto value1 = vram[addr + 0x10000];
346 line[4 * x + 0] = palette16[(value0 >> 4) & 0x0f];
347 line[4 * x + 1] = palette16[(value0 >> 0) & 0x0f];
348 line[4 * x + 2] = palette16[(value1 >> 4) & 0x0f];
349 line[4 * x + 3] = palette16[(value1 >> 0) & 0x0f];
350 ++addr;
351 }
352 }
353 break;
354
355 case SCR8: {
356 auto toColor = [](uint8_t value) {
357 int r = (value & 0x1c) >> 2;
358 int g = (value & 0xe0) >> 5;
359 int b = (value & 0x03) >> 0;
360 int rr = (r << 5) | (r << 2) | (r >> 1);
361 int gg = (g << 5) | (g << 2) | (g >> 1);
362 int bb = (b << 6) | (b << 4) | (b << 2) | (b << 0);
363 int aa = 255;
364 return (rr << 0) | (gg << 8) | (bb << 16) | (aa << 24);
365 };
366 for (auto y : xrange(lines)) {
367 auto* line = &output[256 * y];
368 for (auto x : xrange(128)) {
369 line[2 * x + 0] = toColor(vram[addr + 0x00000]);
370 line[2 * x + 1] = toColor(vram[addr + 0x10000]);
371 ++addr;
372 }
373 }
374 break;
375 }
376
377 case SCR11:
378 for (auto y : xrange(lines)) {
379 auto* line = &output[256 * y];
380 for (auto x : xrange(64)) {
381 std::array<unsigned, 4> p = {
382 vram[addr + 0 + 0x00000],
383 vram[addr + 0 + 0x10000],
384 vram[addr + 1 + 0x00000],
385 vram[addr + 1 + 0x10000],
386 };
387 addr += 2;
388 int j = narrow<int>((p[2] & 7) + ((p[3] & 3) << 3)) - narrow<int>((p[3] & 4) << 3);
389 int k = narrow<int>((p[0] & 7) + ((p[1] & 3) << 3)) - narrow<int>((p[1] & 4) << 3);
390 for (auto n : xrange(4)) {
391 uint32_t pix;
392 if (p[n] & 0x08) {
393 pix = palette16[p[n] >> 4];
394 } else {
395 int Y = narrow<int>(p[n] >> 3);
396 auto [r, g, b] = yjk2rgb(Y, j, k);
397 int rr = (r << 3) | (r >> 2);
398 int gg = (g << 3) | (g >> 2);
399 int bb = (b << 3) | (b >> 2);
400 int aa = 255;
401 pix = (rr << 0) | (gg << 8) | (bb << 16) | (aa << 24);
402 }
403 line[4 * x + n] = pix;
404 }
405 }
406 }
407 break;
408
409 case SCR12:
410 for (auto y : xrange(lines)) {
411 auto* line = &output[256 * y];
412 for (auto x : xrange(64)) {
413 std::array<unsigned, 4> p = {
414 vram[addr + 0 + 0x00000],
415 vram[addr + 0 + 0x10000],
416 vram[addr + 1 + 0x00000],
417 vram[addr + 1 + 0x10000],
418 };
419 addr += 2;
420 int j = narrow<int>((p[2] & 7) + ((p[3] & 3) << 3)) - narrow<int>((p[3] & 4) << 3);
421 int k = narrow<int>((p[0] & 7) + ((p[1] & 3) << 3)) - narrow<int>((p[1] & 4) << 3);
422 for (auto n : xrange(4)) {
423 int Y = narrow<int>(p[n] >> 3);
424 auto [r, g, b] = yjk2rgb(Y, j, k);
425 int rr = (r << 3) | (r >> 2);
426 int gg = (g << 3) | (g >> 2);
427 int bb = (b << 3) | (b >> 2);
428 int aa = 255;
429 line[4 * x + n] = (rr << 0) | (gg << 8) | (bb << 16) | (aa << 24);
430 }
431 }
432 }
433 break;
434
435 case OTHER:
436 for (auto y : xrange(lines)) {
437 auto* line = &output[256 * y];
438 for (auto x : xrange(256)) {
439 line[x] = getColor(imColor::GRAY);
440 }
441 }
442 break;
443 }
444}
445
446} // namespace openmsx
int g
Represents a VDP display mode.
static constexpr uint8_t GRAPHIC4
static constexpr uint8_t GRAPHIC5
static constexpr uint8_t GRAPHIC7
static constexpr byte YAE
Encoding of YAE flag.
static constexpr uint8_t GRAPHIC6
static constexpr byte YJK
Encoding of YJK flag.
void loadLine(std::string_view name, zstring_view value) override
void paint(MSXMotherBoard *motherBoard) override
void save(ImGuiTextBuffer &buf) override
std::unique_ptr< ImGuiPalette > palette
ImGuiManager & manager
Definition ImGuiPart.hh:30
MSXDevice * findDevice(std::string_view name)
Find a MSXDevice by name.
This class manages the lifetime of a block of memory.
Definition MemBuffer.hh:29
const T * data() const
Returns pointer to the start of the memory buffer.
Definition MemBuffer.hh:81
Unified implementation of MSX Video Display Processors (VDPs).
Definition VDP.hh:67
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
void StrCat(Ts &&...ts)
Definition ImGuiUtils.hh:43
void TextUnformatted(const std::string &str)
Definition ImGuiUtils.hh:24
vecN< 2, float > vec2
Definition gl_vec.hh:178
void Window(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:63
void ItemWidth(float item_width, std::invocable<> auto next)
Definition ImGuiCpp.hh:204
void Child(const char *str_id, const ImVec2 &size, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:110
void Disabled(bool b, std::invocable<> auto next)
Definition ImGuiCpp.hh:506
void Group(std::invocable<> auto next)
Definition ImGuiCpp.hh:236
This file implemented 3 utility functions:
Definition Autofire.cc:11
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
ImU32 getColor(imColor col)
const char * getComboString(int item, const char *itemsSeparatedByZeros)
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
Definition ranges.hh:279
constexpr auto xrange(T e)
Definition xrange.hh:132