Skip to content

Commit eaf4f79

Browse files
committed
Audio and video experiments. Closes #45
1 parent 8b0ebaf commit eaf4f79

File tree

8 files changed

+737
-54
lines changed

8 files changed

+737
-54
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ endif
233233

234234
ifeq ($(HAVE_GLES_DISPLAY), 1)
235235
OBJS += src/od-gles/gl.o
236+
OBJS += src/od-gles/shader_stuff.o
236237
OBJS += src/od-gles/gl_platform.o
237238
OBJS += src/od-gles/gles_gfx.o
238239
MORE_CFLAGS += -I/opt/vc/include/

fshader.glsl

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// CRT emulation filter fragment shader
2+
precision mediump float;
3+
varying vec2 v_texCoord;
4+
uniform sampler2D s_texture;
5+
uniform float u_framecount;
6+
uniform vec2 u_emulator_frame_size;
7+
uniform vec2 u_output_frame_size;
8+
9+
//#define TEXSTEP_H (0.50/800.0)
10+
//#define TEXSTEP_V (0.25/480.0)
11+
//#define TEXSTEP_H (0.50/1200.0)
12+
//#define TEXSTEP_V (0.25/720.0)
13+
14+
void main()
15+
{
16+
float tsh = 0.5 / 1200.0;
17+
float tsv = 0.5 / 720.0;
18+
vec2 tc = v_texCoord + vec2(tsh, tsv + sin(u_framecount) * 0.0);
19+
// gl_FragColor = texture2D( s_texture, v_texCoord );
20+
vec4 pix5 = texture2D( s_texture, tc ) * 0.8; // main, center pixel
21+
22+
vec4 pix2 = texture2D( s_texture, tc + vec2(0.0, -tsv) );
23+
vec4 pix4 = texture2D( s_texture, tc + vec2(-tsh, 0.0) );
24+
vec4 pix6 = texture2D( s_texture, tc + vec2(+tsh, 0.0) );
25+
vec4 pix8 = texture2D( s_texture, tc + vec2(0.0, +tsv) );
26+
27+
gl_FragColor =
28+
pix5
29+
//* clamp(cos(gl_FragCoord.y * 3.1415926) * 0.5 + 0.5, 0.0, 1.0)
30+
* clamp(cos(v_texCoord.y * 3.1415926 * 720.0) * 0.25 + 0.92, 0.0, 1.0)
31+
32+
// * vec4((sin(u_framecount) * 0.005 + 0.98))
33+
+ (
34+
pow(pix2 * .7, vec4(1.5))
35+
+ pow(pix4 * .7, vec4(1.5))
36+
+ pow(pix6 * .7, vec4(1.5))
37+
+ pow(pix8 * .7, vec4(1.5))
38+
39+
) * 0.3
40+
;
41+
// gl_FragColor = texture2D( s_texture, v_texCoord );
42+
43+
}
44+

src/od-gles/gl.cpp

+118-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
#include <math.h>
5+
46
#include <EGL/egl.h>
57
#include <GLES/gl.h>
8+
69
#include "gl_platform.h"
710
#include "gl.h"
811

12+
#include "shader_stuff.h"
13+
14+
915
static EGLDisplay edpy;
1016
static EGLSurface esfc;
1117
static EGLContext ectxt;
@@ -14,6 +20,29 @@ static EGLContext ectxt;
1420
void *gl_es_display;
1521
void *gl_es_surface;
1622

23+
24+
static float vertex_coords[] = {
25+
-1.0f, 1.0f, 0.0f, // 0 0 1
26+
1.0f, 1.0f, 0.0f, // 1 ^
27+
-1.0f, -1.0f, 0.0f, // 2 | 2 3
28+
1.0f, -1.0f, 0.0f, // 3 +-->
29+
};
30+
31+
static float orig_texture_coords[] = {
32+
-0.5f, -0.5f,
33+
0.5f, -0.5f,
34+
-0.5f, 0.5f,
35+
0.5f, 0.5f,
36+
};
37+
38+
static float texture_coords[] = {
39+
0.0f, 0.0f, // we flip this:
40+
1.0f, 0.0f, // v^
41+
0.0f, 1.0f, // | u
42+
1.0f, 1.0f, // +-->
43+
};
44+
45+
1746
static int gl_have_error(const char *name)
1847
{
1948
GLenum e = glGetError();
@@ -42,11 +71,25 @@ int gl_init(void *display, void *window, int *quirks)
4271
EGLint num_config;
4372
int retval = -1;
4473
int ret;
45-
EGLint attr[] =
46-
{
47-
EGL_NONE
48-
};
49-
74+
75+
static const EGLint config_attributes[] =
76+
{
77+
EGL_RED_SIZE, 8,
78+
EGL_GREEN_SIZE, 8,
79+
EGL_BLUE_SIZE, 8,
80+
EGL_ALPHA_SIZE, 8,
81+
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
82+
EGL_NONE
83+
};
84+
85+
static const EGLint context_attributes[] =
86+
{
87+
EGL_CONTEXT_CLIENT_VERSION, 2,
88+
EGL_NONE
89+
};
90+
91+
92+
// gl_platform_init() does Raspi-specific stuff like bcm_host_init()
5093
ret = gl_platform_init(&display, &window, quirks);
5194
if (ret != 0) {
5295
printf("gl_platform_init failed with %d\n", ret);
@@ -70,7 +113,7 @@ int gl_init(void *display, void *window, int *quirks)
70113
goto out;
71114
}
72115

73-
if (!eglChooseConfig(edpy, attr, &ecfg, 1, &num_config)) {
116+
if (!eglChooseConfig(edpy, config_attributes, &ecfg, 1, &num_config)) {
74117
printf("Failed to choose config (%x)\n", eglGetError());
75118
goto out;
76119
}
@@ -88,7 +131,8 @@ int gl_init(void *display, void *window, int *quirks)
88131
goto out;
89132
}
90133

91-
ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
134+
// ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
135+
ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, context_attributes);
92136
if (ectxt == EGL_NO_CONTEXT) {
93137
printf("Unable to create EGL context (%x)\n",
94138
eglGetError());
@@ -97,16 +141,20 @@ int gl_init(void *display, void *window, int *quirks)
97141

98142
eglMakeCurrent(edpy, esfc, esfc, ectxt);
99143

100-
glEnable(GL_TEXTURE_2D);
144+
//glEnable(GL_TEXTURE_2D); // for old fixed-function pipeline
145+
//if (gl_have_error("glEnable(GL_TEXTURE_2D)")) goto out;
101146

102147
glGenTextures(1, &texture_name);
148+
if (gl_have_error("glGenTextures")) goto out;
149+
150+
103151

104152
glBindTexture(GL_TEXTURE_2D, texture_name);
153+
if (gl_have_error("glBindTexture")) goto out;
105154

106155
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGB,
107156
GL_UNSIGNED_SHORT_5_6_5, tmp_texture_mem);
108-
if (gl_have_error("glTexImage2D"))
109-
goto out;
157+
if (gl_have_error("glTexImage2D")) goto out;
110158

111159
// no mipmaps
112160
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -126,57 +174,93 @@ int gl_init(void *display, void *window, int *quirks)
126174
gl_es_display = (void *)edpy;
127175
gl_es_surface = (void *)esfc;
128176
retval = 0;
177+
178+
int shader_stuff_result;
179+
shader_stuff_result = shader_stuff_init();
180+
shader_stuff_result = shader_stuff_reload_shaders();
181+
shader_stuff_result = shader_stuff_set_data(vertex_coords, texture_coords, texture_name);
182+
129183
out:
130184
free(tmp_texture_mem);
131185
return retval;
132186
}
133187

134-
static float vertices[] = {
135-
-1.0f, 1.0f, 0.0f, // 0 0 1
136-
1.0f, 1.0f, 0.0f, // 1 ^
137-
-1.0f, -1.0f, 0.0f, // 2 | 2 3
138-
1.0f, -1.0f, 0.0f, // 3 +-->
139-
};
140-
141-
static float texture[] = {
142-
0.0f, 0.0f, // we flip this:
143-
1.0f, 0.0f, // v^
144-
0.0f, 1.0f, // | u
145-
1.0f, 1.0f, // +-->
146-
};
188+
static int framecount = 0;
147189

148190
int gl_flip(const void *fb, int w, int h)
149191
{
150192
static int old_w, old_h;
151193

194+
if (framecount % 60 == 0)
195+
{
196+
// printf("gl_flip() w: %d, h: %d\n", w, h);
197+
}
198+
199+
if (framecount % 30 == 0)
200+
{
201+
if (shader_stuff_shader_needs_reload()) {
202+
shader_stuff_reload_shaders();
203+
// shader_stuff_set_data(vertex_coords, texture_coords, texture_name);
204+
205+
}
206+
}
207+
208+
framecount++;
209+
float floattime = (framecount * 0.04f);
210+
152211
if (fb != NULL) {
153212
if (w != old_w || h != old_h) {
154213
float f_w = (float)w / 1024.0f;
155214
float f_h = (float)h / 512.0f;
156-
texture[1*2 + 0] = f_w;
157-
texture[2*2 + 1] = f_h;
158-
texture[3*2 + 0] = f_w;
159-
texture[3*2 + 1] = f_h;
215+
texture_coords[1*2 + 0] = f_w;
216+
texture_coords[2*2 + 1] = f_h;
217+
texture_coords[3*2 + 0] = f_w;
218+
texture_coords[3*2 + 1] = f_h;
160219
old_w = w;
161220
old_h = h;
221+
}
222+
/*
223+
float rotmat[4]; // 2d rotation matrix
224+
rotmat[0] = cos(floattime);
225+
rotmat[1] = sin(floattime);
226+
rotmat[2] = -sin(floattime);
227+
rotmat[3] = cos(floattime);
228+
229+
for (int i=0; i<4; i++) {
230+
float f_w = (float)w / 1024.0f;
231+
float f_h = (float)h / 512.0f;
232+
float x = orig_texture_coords[i*2 + 0] * f_w;
233+
float y = orig_texture_coords[i*2 + 1] * f_h;
234+
texture_coords[i*2 + 0] =
235+
f_w * 0.5f + (x * rotmat[0] + y * rotmat[1]);
236+
texture_coords[i*2 + 1] =
237+
f_h * 0.5f + (x * rotmat[2] + y * rotmat[3]);
238+
162239
}
240+
*/
163241

164242
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
165243
GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
166244
if (gl_have_error("glTexSubImage2D"))
167245
return -1;
168-
}
246+
} // if (fb != NULL)
247+
248+
// glVertexPointer(3, GL_FLOAT, 0, vertex_coords);
249+
// if (gl_have_error("glVertexPointer")) return -1;
250+
251+
// glTexCoordPointer(2, GL_FLOAT, 0, texture_coords);
252+
// if (gl_have_error("glTexCoordPointer")) return -1;
253+
254+
shader_stuff_frame(framecount, w, h, 800, 480); // TODO! hard-coded output size
255+
if (gl_have_error("use program")) return -1;
169256

170-
glVertexPointer(3, GL_FLOAT, 0, vertices);
171-
glTexCoordPointer(2, GL_FLOAT, 0, texture);
172257
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
258+
// glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
173259

174-
if (gl_have_error("glDrawArrays"))
175-
return -1;
260+
if (gl_have_error("glDrawArrays")) return -1;
176261

177262
eglSwapBuffers(edpy, esfc);
178-
if (gles_have_error("eglSwapBuffers"))
179-
return -1;
263+
if (gles_have_error("eglSwapBuffers")) return -1;
180264

181265
return 0;
182266
}

src/od-gles/gles_gfx.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ static void open_screen(struct uae_prefs *p)
137137
}
138138

139139

140+
if(Dummy_prSDLScreen)
141+
{ // y.f. 2016-10-13 : free the previous screen surface every time,
142+
// so we can have fullscreen while running and windowed while in config window.
143+
// Apparently, something somewhere is resetting the screen.
144+
SDL_FreeSurface(Dummy_prSDLScreen);
145+
Dummy_prSDLScreen = NULL;
146+
}
140147

141148
if(Dummy_prSDLScreen == NULL )
142149
{

0 commit comments

Comments
 (0)