forked from project64/angrylion-rdp
/
gl_screen.c
264 lines (220 loc) · 7.38 KB
/
gl_screen.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
#include "gl_screen.h"
#include "core/msg.h"
#include <stdlib.h>
#include <string.h>
#ifdef GLES
#include <GLES3/gl3.h>
#define SHADER_HEADER "#version 300 es\nprecision lowp float;\n"
#define TEX_FORMAT GL_RGBA
#define TEX_TYPE GL_UNSIGNED_BYTE
#else
#include "gl_core_3_3/gl_core_3_3.c"
#define SHADER_HEADER "#version 330 core\n"
#define TEX_FORMAT GL_BGRA
#define TEX_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
#endif
static GLuint program;
static GLuint vao;
static GLuint texture;
static int32_t tex_width;
static int32_t tex_height;
static int32_t tex_display_height;
#ifdef _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) {
msg_error("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";
}
msg_debug("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);
msg_error("%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);
msg_error("Shader link error: %s\n", log);
}
glDeleteShader(frag);
glDeleteShader(vert);
return program;
}
void gl_screen_init(struct n64video_config* config)
{
#ifndef GLES
// load OpenGL function pointers
ogl_LoadFunctions();
#endif
msg_debug("%s: GL_VERSION='%s'", __FUNCTION__, glGetString(GL_VERSION));
msg_debug("%s: GL_VENDOR='%s'", __FUNCTION__, glGetString(GL_VENDOR));
msg_debug("%s: GL_RENDERER='%s'", __FUNCTION__, glGetString(GL_RENDERER));
msg_debug("%s: GL_SHADING_LANGUAGE_VERSION='%s'", __FUNCTION__, glGetString(GL_SHADING_LANGUAGE_VERSION));
// 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"
#ifdef GLES
" color = texture(tex0, uv);\n"
#else
" color.bgra = texture(tex0, uv);\n"
#endif
"}\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(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// select interpolation method
GLint filter;
switch (config->vi.interp) {
case VI_INTERP_LINEAR:
filter = GL_LINEAR;
break;
case VI_INTERP_NEAREST:
default:
filter = GL_NEAREST;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
// check if there was an error when using any of the commands above
gl_check_errors();
}
bool gl_screen_write(struct frame_buffer* fb, int32_t output_height)
{
bool buffer_size_changed = tex_width != fb->width || tex_height != fb->height;
// check if the framebuffer size has changed
if (buffer_size_changed) {
tex_width = fb->width;
tex_height = 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_RGBA, tex_width,
tex_height, 0, TEX_FORMAT, TEX_TYPE, fb->pixels);
msg_debug("%s: resized framebuffer texture: %dx%d", __FUNCTION__, tex_width, tex_height);
} else {
// copy local buffer to GPU texture buffer
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_width, tex_height,
TEX_FORMAT, TEX_TYPE, fb->pixels);
}
// update output size
tex_display_height = output_height;
return buffer_size_changed;
}
void gl_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(int32_t win_width, int32_t win_height, int32_t win_x, int32_t win_y)
{
int32_t hw = tex_display_height * win_width;
int32_t wh = tex_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 / tex_display_height;
win_x += (win_width - w_max) / 2;
win_width = w_max;
} else if (hw < wh) {
int32_t h_max = hw / tex_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);
// check if there was an error when using any of the commands above
gl_check_errors();
}
void gl_screen_clear(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void gl_screen_close(void)
{
tex_width = 0;
tex_height = 0;
tex_display_height = 0;
glDeleteTextures(1, &texture);
glDeleteVertexArrays(1, &vao);
glDeleteProgram(program);
}