Debian Codegraph @

src/gui/render/renderGL1.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "renderGL1.h"
21 #include "../../ta-log.h"
22 #include "SDL_opengl.h"
23 #include "backends/imgui_impl_opengl2.h"
24 #include "../engine/bsr.h"
25
26 #define C(x) x; if (glGetError()!=GL_NO_ERROR) logW("OpenGL error in %s:%d: " #x,__FILE__,__LINE__);
27
28 class FurnaceGL1Texture: public FurnaceGUITexture {
29 public:
30 GLuint id;
31 int width, height, widthReal, heightReal;
32 FurnaceGUITextureFormat format;
33 unsigned char* lockedData;
34 FurnaceGL1Texture():
35 id(0),
36 width(0),
37 height(0),
38 widthReal(0),
39 heightReal(0),
40 format(GUI_TEXFORMAT_UNKNOWN),
41 lockedData(NULL) {}
42 };
43
44 ImTextureID FurnaceGUIRenderGL1::getTextureID(FurnaceGUITexture* which) {
45 intptr_t ret=((FurnaceGL1Texture*)which)->id;
46 return (ImTextureID)ret;
47 }
48
49 float FurnaceGUIRenderGL1::getTextureU(FurnaceGUITexture* which) {
50 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
51 if (t->widthReal<1) return 0.0f;
52 return (float)t->width/(float)t->widthReal;
53 }
54
55 float FurnaceGUIRenderGL1::getTextureV(FurnaceGUITexture* which) {
56 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
57 if (t->heightReal<1) return 0.0f;
58 return (float)t->height/(float)t->heightReal;
59 }
60
61 FurnaceGUITextureFormat FurnaceGUIRenderGL1::getTextureFormat(FurnaceGUITexture* which) {
62 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
63 return t->format;
64 }
65
66 bool FurnaceGUIRenderGL1::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) {
67 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
68 if (t->lockedData!=NULL) return false;
69 t->lockedData=new unsigned char[t->widthReal*t->heightReal*4];
70
71 *data=t->lockedData;
72 *pitch=t->widthReal*4;
73 return true;
74 }
75
76 bool FurnaceGUIRenderGL1::unlockTexture(FurnaceGUITexture* which) {
77 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
78 if (t->lockedData==NULL) return false;
79
80 C(glBindTexture(GL_TEXTURE_2D,t->id));
81 C(glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,t->widthReal,t->heightReal,0,GL_RGBA,GL_UNSIGNED_BYTE,t->lockedData));
82
83 C(glFlush());
84 delete[] t->lockedData;
85 t->lockedData=NULL;
86
87 return true;
88 }
89
90 bool FurnaceGUIRenderGL1::updateTexture(FurnaceGUITexture* which, void* data, int pitch) {
91 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
92
93 if (t->width*4!=pitch) return false;
94
95 logV("GL1 updateTexture...");
96
97 C(glBindTexture(GL_TEXTURE_2D,t->id));
98 C(glTexSubImage2D(GL_TEXTURE_2D,0,0,0,t->width,t->height,GL_RGBA,GL_UNSIGNED_BYTE,data));
99 return true;
100 }
101
102 FurnaceGUITexture* FurnaceGUIRenderGL1::createTexture(bool dynamic, int width, int height, bool interpolate, FurnaceGUITextureFormat format) {
103 if (format!=GUI_TEXFORMAT_ABGR32) {
104 logE("unsupported texture format!");
105 return NULL;
106 }
107 FurnaceGL1Texture* t=new FurnaceGL1Texture;
108 C(glGenTextures(1,&t->id));
109 C(glBindTexture(GL_TEXTURE_2D,t->id));
110 if (interpolate) {
111 C(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR));
112 C(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR));
113 } else {
114 C(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST));
115 C(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST));
116 }
117
118 int widthReal=width;
119 int heightReal=height;
120
121 if ((widthReal&(widthReal-1))!=0) {
122 widthReal=1<<bsr(width);
123 }
124 if ((heightReal&(heightReal-1))!=0) {
125 heightReal=1<<bsr(height);
126 }
127 logV("width: %d (requested)... %d (actual)",width,widthReal);
128 logV("height: %d (requested)... %d (actual)",height,heightReal);
129
130 C(glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,widthReal,heightReal,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL));
131 t->width=width;
132 t->height=height;
133 t->widthReal=widthReal;
134 t->heightReal=heightReal;
135 t->format=format;
136 return t;
137 }
138
139 bool FurnaceGUIRenderGL1::destroyTexture(FurnaceGUITexture* which) {
140 FurnaceGL1Texture* t=(FurnaceGL1Texture*)which;
141 C(glDeleteTextures(1,&t->id));
142 delete t;
143 return true;
144 }
145
146 void FurnaceGUIRenderGL1::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) {
147 }
148
149 void FurnaceGUIRenderGL1::setBlendMode(FurnaceGUIBlendMode mode) {
150 switch (mode) {
151 case GUI_BLEND_MODE_NONE:
152 C(glBlendFunc(GL_ONE,GL_ZERO));
153 C(glDisable(GL_BLEND));
154 break;
155 case GUI_BLEND_MODE_BLEND:
156 C(glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA));
157 C(glEnable(GL_BLEND));
158 break;
159 case GUI_BLEND_MODE_ADD:
160 C(glBlendFunc(GL_SRC_ALPHA,GL_ONE));
161 C(glEnable(GL_BLEND));
162 break;
163 case GUI_BLEND_MODE_MULTIPLY:
164 C(glBlendFunc(GL_ZERO,GL_SRC_COLOR));
165 C(glEnable(GL_BLEND));
166 break;
167 }
168 }
169
170 void FurnaceGUIRenderGL1::clear(ImVec4 color) {
171 SDL_GL_MakeCurrent(sdlWin,context);
172 C(glClearColor(color.x,color.y,color.z,color.w));
173 C(glClear(GL_COLOR_BUFFER_BIT));
174 }
175
176 void FurnaceGUIRenderGL1::newFrame() {
177 ImGui_ImplOpenGL2_NewFrame();
178 }
179
180 bool FurnaceGUIRenderGL1::canVSync() {
181 return swapIntervalSet;
182 }
183
184 void FurnaceGUIRenderGL1::renderGUI() {
185 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
186 }
187
188 void FurnaceGUIRenderGL1::wipe(float alpha) {
189 C(glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA));
190 C(glEnable(GL_BLEND));
191
192 C(glBindTexture(GL_TEXTURE_2D,0));
193 C(glBegin(GL_TRIANGLE_STRIP));
194 C(glColor4f(0.0,0.0,0.0,alpha));
195 C(glNormal3f(0.0f,0.0f,0.0f));
196 C(glVertex3f(-1.0f,-1.0f,0.0f));
197 C(glNormal3f(1.0f,0.0f,0.0f));
198 C(glVertex3f(1.0f,-1.0f,0.0f));
199 C(glNormal3f(0.0f,1.0f,0.0f));
200 C(glVertex3f(-1.0f,1.0f,0.0f));
201 C(glNormal3f(1.0f,1.0f,0.0f));
202 C(glVertex3f(1.0f,1.0f,0.0f));
203 C(glEnd());
204 }
205
206 void FurnaceGUIRenderGL1::present() {
207 SDL_GL_SwapWindow(sdlWin);
208 C(glFlush());
209 }
210
211 bool FurnaceGUIRenderGL1::getOutputSize(int& w, int& h) {
212 SDL_GL_GetDrawableSize(sdlWin,&w,&h);
213 return true;
214 }
215
216 int FurnaceGUIRenderGL1::getWindowFlags() {
217 return SDL_WINDOW_OPENGL;
218 }
219
220 bool FurnaceGUIRenderGL1::areTexturesSquare() {
221 return true;
222 }
223
224 int FurnaceGUIRenderGL1::getMaxTextureWidth() {
225 return maxWidth;
226 }
227
228 int FurnaceGUIRenderGL1::getMaxTextureHeight() {
229 return maxHeight;
230 }
231
232 unsigned int FurnaceGUIRenderGL1::getTextureFormats() {
233 return GUI_TEXFORMAT_ABGR32;
234 }
235
236 const char* FurnaceGUIRenderGL1::getBackendName() {
237 return "OpenGL 1.1";
238 }
239
240 const char* FurnaceGUIRenderGL1::getVendorName() {
241 return vendorName.c_str();
242 }
243
244 const char* FurnaceGUIRenderGL1::getDeviceName() {
245 return deviceName.c_str();
246 }
247
248 const char* FurnaceGUIRenderGL1::getAPIVersion() {
249 return apiVersion.c_str();
250 }
251
252 void FurnaceGUIRenderGL1::setSwapInterval(int swapInterval) {
253 SDL_GL_SetSwapInterval(swapInterval);
254 if (swapInterval>0 && SDL_GL_GetSwapInterval()==0) {
255 swapIntervalSet=false;
256 logW("tried to enable VSync but couldn't!");
257 } else {
258 swapIntervalSet=true;
259 }
260 }
261
262 void FurnaceGUIRenderGL1::preInit(const DivConfig& conf) {
263 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,0);
264 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,0);
265 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,1);
266 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,1);
267
268 SDL_GL_SetAttribute(SDL_GL_RED_SIZE,conf.getInt("glRedSize",8));
269 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,conf.getInt("glGreenSize",8));
270 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,conf.getInt("glBlueSize",8));
271 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,conf.getInt("glAlphaSize",0));
272 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,conf.getInt("glDoubleBuffer",1));
273 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,conf.getInt("glDepthSize",24));
274 if (conf.getInt("glSetBS",0)) {
275 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,conf.getInt("glStencilSize",0));
276 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,conf.getInt("glBufferSize",32));
277 }
278 }
279
280 #define LOAD_PROC_MANDATORY(_v,_t,_s) \
281 _v=(_t)SDL_GL_GetProcAddress(_s); \
282 if (!_v) { \
283 logE(_s " not found"); \
284 return false; \
285 }
286
287 #define LOAD_PROC_OPTIONAL(_v,_t,_s) \
288 _v=(_t)SDL_GL_GetProcAddress(_s); \
289 if (!_v) { \
290 logW(_s " not found"); \
291 }
292
293 bool FurnaceGUIRenderGL1::init(SDL_Window* win, int swapInterval) {
294 sdlWin=win;
295 context=SDL_GL_CreateContext(win);
296 if (context==NULL) {
297 return false;
298 }
299 SDL_GL_MakeCurrent(win,context);
300 SDL_GL_SetSwapInterval(swapInterval);
301 if (swapInterval>0 && SDL_GL_GetSwapInterval()==0) {
302 swapIntervalSet=false;
303 logW("tried to enable VSync but couldn't!");
304 } else {
305 swapIntervalSet=true;
306 }
307
308 const char* next=(const char*)glGetString(GL_VENDOR);
309 if (next==NULL) {
310 vendorName="???";
311 } else {
312 vendorName=next;
313 }
314 next=(const char*)glGetString(GL_RENDERER);
315 if (next==NULL) {
316 deviceName="???";
317 } else {
318 deviceName=next;
319 }
320 next=(const char*)glGetString(GL_VERSION);
321 if (next==NULL) {
322 apiVersion="???";
323 } else {
324 apiVersion=next;
325 }
326
327 int maxSize=1024;
328 glGetIntegerv(GL_MAX_TEXTURE_SIZE,&maxSize);
329
330 maxWidth=maxSize;
331 maxHeight=maxSize;
332
333 return true;
334 }
335
336 void FurnaceGUIRenderGL1::initGUI(SDL_Window* win) {
337 ImGui_ImplSDL2_InitForOpenGL(win,context);
338 ImGui_ImplOpenGL2_Init();
339 }
340
341 bool FurnaceGUIRenderGL1::quit() {
342 if (context==NULL) return false;
343 SDL_GL_DeleteContext(context);
344 context=NULL;
345 return true;
346 }
347
348 void FurnaceGUIRenderGL1::quitGUI() {
349 ImGui_ImplOpenGL2_Shutdown();
350 }
351
352 // sadly, OpenGL 1.1 doesn't have the ability to recover from death...
353 bool FurnaceGUIRenderGL1::isDead() {
354 return false;
355 }