-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVidHdWindowBeam.cpp
More file actions
332 lines (299 loc) · 10.5 KB
/
VidHdWindowBeam.cpp
File metadata and controls
332 lines (299 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
// VidHdModesManager.cpp
// SuperDuperDisplay
//
// Created by Henri Asseily on 27/01/2025.
//
#include "VidHdWindowBeam.h"
#include "common.h"
#include <SDL_timer.h>
#include "A2VideoManager.h"
#include "SDHRManager.h"
#include "MemoryManager.h"
VidHdWindowBeam::VidHdWindowBeam(VidHdMode_e _mode)
{
vram_text = new uint32_t[_VIDHDMODES_TEXT_WIDTH*_VIDHDMODES_TEXT_HEIGHT];
shader = Shader();
shader.Build(_SHADER_A2_VERTEX_DEFAULT, _SHADER_VIDHD_TEXT_FRAGMENT);
this->SetVideoMode(_mode);
this->UpdateVertexArray();
}
VidHdWindowBeam::~VidHdWindowBeam()
{
if (vram_text!= nullptr)
{
delete[] vram_text;
vram_text = nullptr;
}
if (VRAMTEX != UINT_MAX)
glDeleteTextures(1, &VRAMTEX);
if (VAO != UINT_MAX)
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
}
void VidHdWindowBeam::WriteCharacter(uint8_t hpos, uint8_t vpos, uint8_t value)
{
if (hpos >= _VIDHDMODES_TEXT_WIDTH)
return;
if (vpos >= _VIDHDMODES_TEXT_HEIGHT)
return;
VidHdVramTextEntry textEntry;
textEntry.character = value;
textEntry.unused = 0;
textEntry.color = MemoryManager::GetInstance()->switch_c022;
textEntry.alpha = textAlpha;
vram_text[_VIDHDMODES_TEXT_WIDTH*vpos + hpos] = *reinterpret_cast<uint32_t*>(&textEntry);
}
uint8_t VidHdWindowBeam::ReadCharacter(uint8_t hpos, uint8_t vpos)
{
if (hpos >= _VIDHDMODES_TEXT_WIDTH)
return 0;
if (vpos >= _VIDHDMODES_TEXT_HEIGHT)
return 0;
VidHdVramTextEntry* textEntry = reinterpret_cast<VidHdVramTextEntry*>(vram_text + _VIDHDMODES_TEXT_WIDTH*vpos + hpos);
return textEntry->character;
}
void VidHdWindowBeam::SetAlpha(uint8_t alpha)
{
textAlpha = alpha;
}
void VidHdWindowBeam::SetVideoMode(VidHdMode_e mode)
{
if (mode == video_mode)
return;
if (mode == VIDHDMODE_TOTAL_COUNT)
video_mode = VIDHDMODE_NONE;
else
video_mode = mode;
bModeDidChange = true;
switch (video_mode) {
case VIDHDMODE_TEXT_40X24:
modeSize.x = 40;
modeSize.y = 24;
fontTex = _TEXUNIT_IMAGE_FONT_ROM_DEFAULT - GL_TEXTURE0;
glyphSize = glm::uvec2(14,16);
fontScale = glm::uvec2(2,2);
// The base A2 font texture is twice the size it really is,
// so we fix it by doubling the screen size we send to the shader
screen_count = {_A2VIDEO_LEGACY_WIDTH*2,_A2VIDEO_LEGACY_HEIGHT*2};
break;
case VIDHDMODE_TEXT_80X24:
modeSize.x = 80;
modeSize.y = 24;
fontTex = _TEXUNIT_IMAGE_FONT_ROM_DEFAULT - GL_TEXTURE0;
glyphSize = glm::uvec2(14,16);
fontScale = glm::uvec2(1,2);
// The base A2 font texture is twice the size it really is,
// so we fix it by doubling the screen size we send to the shader
screen_count = {_A2VIDEO_LEGACY_WIDTH*2,_A2VIDEO_LEGACY_HEIGHT*2};
break;
case VIDHDMODE_TEXT_80X45:
modeSize.x = 80;
modeSize.y = 45;
fontTex = _TEXUNIT_IMAGE_FONT_VIDHD_8X8 - GL_TEXTURE0;
glyphSize = glm::uvec2(8,8);
fontScale = glm::uvec2(3,3);
screen_count = {_VIDHDMODES_PIXEL_WIDTH,_VIDHDMODES_PIXEL_HEIGHT};
break;
case VIDHDMODE_TEXT_120X67:
modeSize.x = 120;
modeSize.y = 67;
fontTex = _TEXUNIT_IMAGE_FONT_VIDHD_8X8 - GL_TEXTURE0;
glyphSize = glm::uvec2(8,8);
fontScale = glm::uvec2(2,2);
screen_count = {_VIDHDMODES_PIXEL_WIDTH,_VIDHDMODES_PIXEL_HEIGHT};
break;
case VIDHDMODE_TEXT_240X135:
modeSize.x = 240;
modeSize.y = 135;
fontTex = _TEXUNIT_IMAGE_FONT_VIDHD_8X8 - GL_TEXTURE0;
glyphSize = glm::uvec2(8,8);
fontScale = glm::uvec2(1,1);
screen_count = {_VIDHDMODES_PIXEL_WIDTH,_VIDHDMODES_PIXEL_HEIGHT};
break;
default:
modeSize.x = 0;
modeSize.y = 0;
fontTex = _TEXUNIT_IMAGE_FONT_ROM_DEFAULT - GL_TEXTURE0;
glyphSize = glm::uvec2(14,16);
fontScale = glm::uvec2(2,2);
screen_count = {0,0};
break;
}
// Clear all the columns that are beyond the width of the mode
if (modeSize.x < _VIDHDMODES_TEXT_WIDTH)
{
for (int j = 0; j < modeSize.y; ++j) {
memset(vram_text+(_VIDHDMODES_TEXT_WIDTH*j + modeSize.x), 0, (_VIDHDMODES_TEXT_WIDTH - modeSize.x) * sizeof(uint32_t));
}
}
// Clear all the rows that are beyond the height of the mode
memset(vram_text+(modeSize.x*modeSize.y), 0, _VIDHDMODES_TEXT_WIDTH * (_VIDHDMODES_TEXT_HEIGHT - modeSize.y) * sizeof(uint32_t));
}
uint32_t VidHdWindowBeam::GetWidth() const
{
return screen_count.x;
}
uint32_t VidHdWindowBeam::GetHeight() const
{
return screen_count.y;
}
void VidHdWindowBeam::SetQuadRelativeBounds(SDL_FRect bounds)
{
quad = bounds;
this->UpdateVertexArray();
}
void VidHdWindowBeam::UpdateVertexArray()
{
// Assign the vertex array.
// The first 2 values are the relative XY, bound from -1 to 1.
// The second pair of values is the actual pixel value on screen
vertices.clear();
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x, quad.y), glm::ivec2(0, screen_count.y) })); // top left
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x + quad.w, quad.y + quad.h), glm::ivec2(screen_count.x, 0) })); // bottom right
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x + quad.w, quad.y), glm::ivec2(screen_count.x, screen_count.y) })); // top right
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x, quad.y), glm::ivec2(0, screen_count.y) })); // top left
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x, quad.y + quad.h), glm::ivec2(0, 0) })); // bottom left
vertices.push_back(VidHdBeamVertex({ glm::vec2(quad.x + quad.w, quad.y + quad.h), glm::ivec2(screen_count.x, 0) })); // bottom right
}
void VidHdWindowBeam::Render()
{
// std::cerr << "Rendering vidhd mode " << (int)video_mode << std::endl;
if (video_mode == VIDHDMODE_NONE)
return;
if (!shader.isReady)
return;
if (vertices.size() == 0)
return;
GLenum glerr;
if (VRAMTEX == UINT_MAX)
{
glGenTextures(1, &VRAMTEX);
glActiveTexture(_TEXUNIT_DATABUFFER_RGBA8UI);
glBindTexture(GL_TEXTURE_2D, VRAMTEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glActiveTexture(GL_TEXTURE0);
}
if (VAO == UINT_MAX)
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
}
shader.Use();
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL VidHdWindowBeam glUseProgram error: " << glerr << std::endl;
return;
}
glBindVertexArray(VAO);
// Always reload the vertices
// because compatibility with GL-ES on the rPi
{
// load data into vertex buffers
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(VidHdBeamVertex), &vertices[0], GL_STATIC_DRAW);
// set the vertex attribute pointers
// vertex relative Positions: position 0, size 2
// (vec4 values x and y)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(VidHdBeamVertex), (void*)0);
// vertex pixel Positions: position 1, size 2
// (vec4 values z and w)
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VidHdBeamVertex), (void*)offsetof(VidHdBeamVertex, PixelPos));
}
glActiveTexture(_TEXUNIT_DATABUFFER_RGBA8UI);
glBindTexture(GL_TEXTURE_2D, VRAMTEX);
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "VidHdWindowBeam::Render 5 error: " << glerr << std::endl;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
if (bVramTextureExists) // it exists, do a glTexSubImage2D() update
{
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _VIDHDMODES_TEXT_WIDTH, _VIDHDMODES_TEXT_HEIGHT, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, vram_text);
}
else { // texture doesn't exist, create it with glTexImage2D()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, _VIDHDMODES_TEXT_WIDTH, _VIDHDMODES_TEXT_HEIGHT, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, vram_text);
bVramTextureExists = true;
}
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "VidHdWindowBeam::Render error: " << glerr << std::endl;
}
shader.SetUniform("ticks", SDL_GetTicks());
if (bModeDidChange)
{
bModeDidChange = false;
shader.SetUniform("VRAMTEX", _TEXUNIT_DATABUFFER_RGBA8UI - GL_TEXTURE0);
shader.SetUniform("fontTex", fontTex);
shader.SetUniform("glyphSize", glyphSize);
shader.SetUniform("fontScale", fontScale);
}
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)this->vertices.size());
glActiveTexture(GL_TEXTURE0);
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "VidHdWindowBeam render error: " << glerr << std::endl;
}
return;
}
void VidHdWindowBeam::DisplayImGuiWindow(bool* p_open)
{
// Begin the ImGui window. If the window is collapsed, end immediately.
if (!ImGui::Begin("VidHdWindowBeam Control", p_open))
{
ImGui::End();
return;
}
if (ImGui::Button("Reload Shader"))
{
std::string _ps = "/Users/henri/Documents/Repos/SuperDuperDisplay/";
_ps.append(_SHADER_VIDHD_TEXT_FRAGMENT);
shader.Build(_SHADER_A2_VERTEX_DEFAULT, _ps.c_str());
auto _vm = this->GetVideoMode();
this->SetVideoMode(VidHdMode_e::VIDHDMODE_NONE);
this->SetVideoMode(_vm);
this->UpdateVertexArray();
}
// --- Input Controls to Test Writing into vram_text ---
// Static variables retain values across frames.
static int x_pos = 0;
static int y_pos = 0;
static int ascii_value = 32; // Default to a space character
ImGui::InputInt("X Position", &x_pos);
ImGui::InputInt("Y Position", &y_pos);
ImGui::InputInt("ASCII Value", &ascii_value);
if (ImGui::Button("Write Character"))
{
// Write the ASCII value (converted to uint8_t) at the specified position.
WriteCharacter(static_cast<uint8_t>(x_pos),
static_cast<uint8_t>(y_pos),
static_cast<uint8_t>(ascii_value+128));
A2VideoManager::GetInstance()->ForceBeamFullScreenRender();
}
if (ImGui::Button("Test String"))
{
for (int xx = 0; xx < modeSize.x; ++xx) {
for (int yy = 0; yy < modeSize.y; ++yy) {
WriteCharacter(static_cast<uint8_t>(xx),
static_cast<uint8_t>(yy),
static_cast<uint8_t>((xx % 0x80) + 0x80));
}
}
A2VideoManager::GetInstance()->ForceBeamFullScreenRender();
}
// Display the character at the given position
uint8_t current_char = ReadCharacter(static_cast<uint8_t>(x_pos), static_cast<uint8_t>(y_pos));
// Only display printable characters; otherwise show a dot.
char display_char = (current_char >= (128+32) && current_char < 255) ? static_cast<char>(current_char-128) : '.';
ImGui::Text("Character at (%d, %d): '%c' (ASCII: %d)", x_pos, y_pos, display_char, current_char);
// --- Button to Clear the Entire VRAM Text Buffer ---
if (ImGui::Button("Clear VRAM Text"))
{
memset(vram_text, 0, _VIDHDMODES_TEXT_WIDTH * _VIDHDMODES_TEXT_HEIGHT * sizeof(uint32_t));
A2VideoManager::GetInstance()->ForceBeamFullScreenRender();
}
ImGui::End();
}