forked from simple64/parallel-rdp-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglguts.c
423 lines (366 loc) · 14.4 KB
/
glguts.c
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "glguts.h"
#include "gfx_m64p.h"
#include "parallel_imp.h"
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#if !defined(GLES)
#include <GL/gl.h>
#include <GL/glext.h>
#else
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#include <EGL/egl.h>
#define GL_MAP_PERSISTENT_BIT 0x0040
#define GL_MAP_COHERENT_BIT 0x0080
typedef void (* PFNGLBUFFERSTORAGEPROC) (GLenum target, GLsizeiptr size, const void *data, GLbitfield flags);
extern PFNGLBUFFERSTORAGEPROC ptrBufferStorage;
#endif
/* definitions of pointers to Core video extension functions */
static ptr_VidExt_Init CoreVideo_Init = NULL;
static ptr_VidExt_Quit CoreVideo_Quit = NULL;
static ptr_VidExt_ListFullscreenModes CoreVideo_ListFullscreenModes = NULL;
static ptr_VidExt_SetVideoMode CoreVideo_SetVideoMode = NULL;
static ptr_VidExt_SetCaption CoreVideo_SetCaption = NULL;
static ptr_VidExt_ToggleFullScreen CoreVideo_ToggleFullScreen = NULL;
static ptr_VidExt_ResizeWindow CoreVideo_ResizeWindow = NULL;
static ptr_VidExt_GL_GetProcAddress CoreVideo_GL_GetProcAddress = NULL;
static ptr_VidExt_GL_SetAttribute CoreVideo_GL_SetAttribute = NULL;
static ptr_VidExt_GL_GetAttribute CoreVideo_GL_GetAttribute = NULL;
static ptr_VidExt_GL_SwapBuffers CoreVideo_GL_SwapBuffers = NULL;
#if !defined(GLES)
static PFNGLCREATESHADERPROC glCreateShader;
static PFNGLSHADERSOURCEPROC glShaderSource;
static PFNGLCOMPILESHADERPROC glCompileShader;
static PFNGLGETSHADERIVPROC glGetShaderiv;
static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
static PFNGLCREATEPROGRAMPROC glCreateProgram;
static PFNGLATTACHSHADERPROC glAttachShader;
static PFNGLLINKPROGRAMPROC glLinkProgram;
static PFNGLGETPROGRAMIVPROC glGetProgramiv;
static PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
static PFNGLDELETESHADERPROC glDeleteShader;
static PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays;
static PFNGLDELETEPROGRAMPROC glDeleteProgram;
static PFNGLUSEPROGRAMPROC glUseProgram;
static PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
static PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
static PFNGLBINDBUFFERPROC glBindBuffer;
static PFNGLGENBUFFERSPROC glGenBuffers;
static PFNGLDELETEBUFFERSPROC glDeleteBuffers;
static PFNGLBUFFERSTORAGEPROC glBufferStorage;
static PFNGLMAPBUFFERRANGEPROC glMapBufferRange;
static PFNGLUNMAPBUFFERPROC glUnmapBuffer;
static PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
static PFNGLUNIFORM1IPROC glUniform1i;
#define SHADER_HEADER "#version 330 core\n"
#else
static PFNGLBUFFERSTORAGEPROC glBufferStorage;
#define SHADER_HEADER "#version 300 es\nprecision lowp float;\n"
#endif
static bool toggle_fs;
static int toggle_buffer;
// framebuffer texture states
int32_t window_width;
int32_t window_height;
int32_t window_fullscreen;
#define TEX_FORMAT GL_RGBA
#define TEX_TYPE GL_UNSIGNED_BYTE
static GLuint program;
static GLuint vao;
static GLuint buffer;
static GLuint texture[2];
static uint8_t *buffer_data;
static uint32_t buffer_size = (640*8) * (480*8) * sizeof(uint32_t);
int32_t tex_width[2];
int32_t tex_height[2];
int display_width;
int display_height;
#ifdef GL_DEBUG
static void gl_check_errors(void)
{
GLenum err;
static int32_t invalid_op_count = 0;
while ((err = glGetError()) != GL_NO_ERROR)
{
// if gl_check_errors is called from a thread with no valid
// GL context, it would be stuck in an infinite loop here, since
// glGetError itself causes GL_INVALID_OPERATION, so check for a few
// cycles and abort if there are too many errors of that kind
if (err == GL_INVALID_OPERATION)
{
if (++invalid_op_count >= 100)
{
printf("gl_check_errors: invalid OpenGL context!");
}
}
else
{
invalid_op_count = 0;
}
char *err_str;
switch (err)
{
case GL_INVALID_OPERATION:
err_str = "INVALID_OPERATION";
break;
case GL_INVALID_ENUM:
err_str = "INVALID_ENUM";
break;
case GL_INVALID_VALUE:
err_str = "INVALID_VALUE";
break;
case GL_OUT_OF_MEMORY:
err_str = "OUT_OF_MEMORY";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
err_str = "INVALID_FRAMEBUFFER_OPERATION";
break;
default:
err_str = "unknown";
}
printf("gl_check_errors: %d (%s)", err, err_str);
}
}
#else
#define gl_check_errors(...)
#endif
static GLuint gl_shader_compile(GLenum type, const GLchar *source)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint param;
glGetShaderiv(shader, GL_COMPILE_STATUS, ¶m);
if (!param)
{
GLchar log[4096];
glGetShaderInfoLog(shader, sizeof(log), NULL, log);
printf("%s shader error: %s\n", type == GL_FRAGMENT_SHADER ? "Frag" : "Vert", log);
}
return shader;
}
static GLuint gl_shader_link(GLuint vert, GLuint frag)
{
GLuint program = glCreateProgram();
glAttachShader(program, vert);
glAttachShader(program, frag);
glLinkProgram(program);
GLint param;
glGetProgramiv(program, GL_LINK_STATUS, ¶m);
if (!param)
{
GLchar log[4096];
glGetProgramInfoLog(program, sizeof(log), NULL, log);
printf("Shader link error: %s\n", log);
}
glDeleteShader(frag);
glDeleteShader(vert);
return program;
}
void screen_write(struct frame_buffer *fb)
{
bool buffer_size_changed = tex_width[toggle_buffer] != fb->width || tex_height[toggle_buffer] != fb->height;
char* offset = NULL;
offset += toggle_buffer * buffer_size;
glBindTexture(GL_TEXTURE_2D, texture[toggle_buffer]);
// check if the framebuffer size has changed
if (buffer_size_changed)
{
tex_width[toggle_buffer] = fb->width;
tex_height[toggle_buffer] = fb->height;
// set pitch for all unpacking operations
glPixelStorei(GL_UNPACK_ROW_LENGTH, fb->pitch);
// reallocate texture buffer on GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_width[toggle_buffer],
tex_height[toggle_buffer], 0, TEX_FORMAT, TEX_TYPE, offset);
}
else
{
// copy local buffer to GPU texture buffer
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_width[toggle_buffer], tex_height[toggle_buffer],
TEX_FORMAT, TEX_TYPE, offset);
}
toggle_buffer = !toggle_buffer;
}
void screen_read(struct frame_buffer *fb, bool alpha)
{
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);
fb->width = vp[2];
fb->height = vp[3];
fb->pitch = fb->width;
if (fb->pixels)
{
glReadPixels(vp[0], vp[1], vp[2], vp[3], alpha ? GL_RGBA : GL_RGB, TEX_TYPE, fb->pixels);
}
}
void gl_screen_render()
{
display_width = 640 * vk_rescaling;
display_height = 480 * vk_rescaling;
int win_width = window_width;
int win_height = window_height;
int win_x = 0;
int win_y = 0;
int32_t hw = display_height * win_width;
int32_t wh = display_width * win_height;
// add letterboxes or pillarboxes if the window has a different aspect ratio
// than the current display mode
if (hw > wh) {
int32_t w_max = wh / display_height;
win_x += (win_width - w_max) / 2;
win_width = w_max;
} else if (hw < wh) {
int32_t h_max = hw / display_width;
win_y += (win_height - h_max) / 2;
win_height = h_max;
}
// configure viewport
glViewport(win_x, win_y, win_width, win_height);
// draw fullscreen triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void gl_screen_clear(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void gl_screen_close(void)
{
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glDeleteTextures(2, &texture[0]);
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &buffer);
glDeleteProgram(program);
}
uint8_t* screen_get_texture_data()
{
return buffer_data + (toggle_buffer * buffer_size);
}
void screen_init()
{
tex_width[0] = 0;
tex_height[0] = 0;
tex_width[1] = 0;
tex_height[1] = 0;
toggle_fs = false;
toggle_buffer = 0;
/* Get the core Video Extension function pointers from the library handle */
CoreVideo_Init = (ptr_VidExt_Init)DLSYM(CoreLibHandle, "VidExt_Init");
CoreVideo_Quit = (ptr_VidExt_Quit)DLSYM(CoreLibHandle, "VidExt_Quit");
CoreVideo_ListFullscreenModes = (ptr_VidExt_ListFullscreenModes)DLSYM(CoreLibHandle, "VidExt_ListFullscreenModes");
CoreVideo_SetVideoMode = (ptr_VidExt_SetVideoMode)DLSYM(CoreLibHandle, "VidExt_SetVideoMode");
CoreVideo_SetCaption = (ptr_VidExt_SetCaption)DLSYM(CoreLibHandle, "VidExt_SetCaption");
CoreVideo_ToggleFullScreen = (ptr_VidExt_ToggleFullScreen)DLSYM(CoreLibHandle, "VidExt_ToggleFullScreen");
CoreVideo_ResizeWindow = (ptr_VidExt_ResizeWindow)DLSYM(CoreLibHandle, "VidExt_ResizeWindow");
CoreVideo_GL_GetProcAddress = (ptr_VidExt_GL_GetProcAddress)DLSYM(CoreLibHandle, "VidExt_GL_GetProcAddress");
CoreVideo_GL_SetAttribute = (ptr_VidExt_GL_SetAttribute)DLSYM(CoreLibHandle, "VidExt_GL_SetAttribute");
CoreVideo_GL_GetAttribute = (ptr_VidExt_GL_GetAttribute)DLSYM(CoreLibHandle, "VidExt_GL_GetAttribute");
CoreVideo_GL_SwapBuffers = (ptr_VidExt_GL_SwapBuffers)DLSYM(CoreLibHandle, "VidExt_GL_SwapBuffers");
CoreVideo_Init();
CoreVideo_GL_SetAttribute(M64P_GL_CONTEXT_PROFILE_MASK, M64P_GL_CONTEXT_PROFILE_CORE);
CoreVideo_GL_SetAttribute(M64P_GL_CONTEXT_MAJOR_VERSION, 3);
CoreVideo_GL_SetAttribute(M64P_GL_CONTEXT_MINOR_VERSION, 3);
CoreVideo_SetVideoMode(window_width, window_height, 0, window_fullscreen ? M64VIDEO_FULLSCREEN : M64VIDEO_WINDOWED, M64VIDEOFLAG_SUPPORT_RESIZING);
CoreVideo_SetCaption("Mupen64Plus-Parallel");
#if !defined(GLES)
glCreateShader = (PFNGLCREATESHADERPROC) CoreVideo_GL_GetProcAddress("glCreateShader");
glShaderSource = (PFNGLSHADERSOURCEPROC) CoreVideo_GL_GetProcAddress("glShaderSource");
glCompileShader = (PFNGLCOMPILESHADERPROC) CoreVideo_GL_GetProcAddress("glCompileShader");
glGetShaderiv = (PFNGLGETSHADERIVPROC) CoreVideo_GL_GetProcAddress("glGetShaderiv");
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) CoreVideo_GL_GetProcAddress("glGetShaderInfoLog");
glCreateProgram = (PFNGLCREATEPROGRAMPROC) CoreVideo_GL_GetProcAddress("glCreateProgram");
glAttachShader = (PFNGLATTACHSHADERPROC) CoreVideo_GL_GetProcAddress("glAttachShader");
glLinkProgram = (PFNGLLINKPROGRAMPROC) CoreVideo_GL_GetProcAddress("glLinkProgram");
glGetProgramiv = (PFNGLGETPROGRAMIVPROC) CoreVideo_GL_GetProcAddress("glGetProgramiv");
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) CoreVideo_GL_GetProcAddress("glGetProgramInfoLog");
glDeleteShader = (PFNGLDELETESHADERPROC) CoreVideo_GL_GetProcAddress("glDeleteShader");
glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC) CoreVideo_GL_GetProcAddress("glDeleteVertexArrays");
glDeleteProgram = (PFNGLDELETEPROGRAMPROC) CoreVideo_GL_GetProcAddress("glDeleteProgram");
glUseProgram = (PFNGLUSEPROGRAMPROC) CoreVideo_GL_GetProcAddress("glUseProgram");
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC) CoreVideo_GL_GetProcAddress("glGenVertexArrays");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC) CoreVideo_GL_GetProcAddress("glBindVertexArray");
glBindBuffer = (PFNGLBINDBUFFERPROC) CoreVideo_GL_GetProcAddress("glBindBuffer");
glGenBuffers = (PFNGLGENBUFFERSPROC) CoreVideo_GL_GetProcAddress("glGenBuffers");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC) CoreVideo_GL_GetProcAddress("glDeleteBuffers");
glBufferStorage = (PFNGLBUFFERSTORAGEPROC) CoreVideo_GL_GetProcAddress("glBufferStorage");
glMapBufferRange = (PFNGLMAPBUFFERRANGEPROC) CoreVideo_GL_GetProcAddress("glMapBufferRange");
glUnmapBuffer = (PFNGLUNMAPBUFFERPROC) CoreVideo_GL_GetProcAddress("glUnmapBuffer");
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) CoreVideo_GL_GetProcAddress("glGetUniformLocation");
glUniform1i = (PFNGLUNIFORM1IPROC) CoreVideo_GL_GetProcAddress("glUniform1i");
#else
glBufferStorage = (PFNGLBUFFERSTORAGEPROC) eglGetProcAddress("glBufferStorageEXT");
#endif
// shader sources for drawing a clipped full-screen triangle. the geometry
// is defined by the vertex ID, so a VBO is not required.
const GLchar *vert_shader =
SHADER_HEADER
"out vec2 uv;\n"
"void main(void) {\n"
" uv = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);\n"
" gl_Position = vec4(uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0), 0.0, 1.0);\n"
"}\n";
const GLchar *frag_shader =
SHADER_HEADER
"in vec2 uv;\n"
"layout(location = 0) out vec4 color;\n"
"uniform sampler2D tex0;\n"
"void main(void) {\n"
"color = texture(tex0, uv);\n"
"}\n";
// compile and link OpenGL program
GLuint vert = gl_shader_compile(GL_VERTEX_SHADER, vert_shader);
GLuint frag = gl_shader_compile(GL_FRAGMENT_SHADER, frag_shader);
program = gl_shader_link(vert, frag);
glUseProgram(program);
// prepare dummy VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// prepare texture
glGenTextures(2, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenBuffers(1, &buffer);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
glBufferStorage(GL_PIXEL_UNPACK_BUFFER, buffer_size * 2, 0, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
buffer_data = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, buffer_size * 2, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
// check if there was an error when using any of the commands above
gl_check_errors();
}
void screen_swap(bool blank)
{
if (toggle_fs)
{
CoreVideo_ToggleFullScreen();
toggle_fs = false;
}
gl_screen_clear();
if (!blank)
{
gl_screen_render();
}
(*render_callback)(1);
CoreVideo_GL_SwapBuffers();
}
void screen_set_fullscreen(bool _fullscreen)
{
toggle_fs = true;
}
bool screen_get_fullscreen()
{
return false;
}
void screen_toggle_fullscreen()
{
screen_set_fullscreen(!screen_get_fullscreen());
}
void screen_close(void)
{
gl_screen_close();
CoreVideo_Quit();
}