Permalink
Browse files

glw: Rewrite OpenGL shader code to be more clean OpenGL and less Open…

…GL ES2.0 compatible

This hopefully makes it more compatible with Mesa and
we will need separate code for OpenGL ES2.0 anyway I think
  • Loading branch information...
1 parent 011d011 commit 33a36760c9e4340b04c0d5d3a363af55885ad694 @andoma committed Aug 4, 2011
View
@@ -319,7 +319,7 @@ SRCS-$(CONFIG_GLW_BACKEND_GX) += src/ui/glw/glw_gxasm.S
SRCS-$(CONFIG_NVCTRL) += src/ui/linux/nvidia.c
-BUNDLES-$(CONFIG_GLW_BACKEND_OPENGL) += src/ui/glw/glsl
+BUNDLES-$(CONFIG_GLW_BACKEND_OPENGL) += src/ui/glw/opengl
#
# GTK based interface
@@ -22,7 +22,7 @@
#include "glw_renderer.h"
#include "fileaccess/fileaccess.h"
-
+#define SHADERPATH "bundle://src/ui/glw/opengl/"
// #define DEBUG_SHADERS
@@ -99,7 +99,7 @@ glw_wirebox(glw_root_t *gr, glw_rctx_t *rc)
#if CONFIG_GLW_BACKEND_OPENGL
glw_backend_root_t *gbr = &gr->gr_be;
glw_load_program(gbr, gbr->gbr_renderer_flat);
- glw_program_set_modelview(gbr, rc);
+ glLoadMatrixf(glw_mtx_get(rc->rc_mtx));
glw_program_set_uniform_color(gbr, 1,1,1,1);
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINE_LOOP);
@@ -124,7 +124,7 @@ glw_wirecube(glw_root_t *gr, glw_rctx_t *rc)
glw_backend_root_t *gbr = &gr->gr_be;
glw_load_program(gbr, gbr->gbr_renderer_flat);
- glw_program_set_modelview(gbr, rc);
+ glLoadMatrixf(glw_mtx_get(rc->rc_mtx));
glw_program_set_uniform_color(gbr, 1,1,1,1);
glBegin(GL_LINE_LOOP);
@@ -330,21 +330,11 @@ shader_render(struct glw_root *root,
1.5 * blur / tex->height);
}
- glUniformMatrix4fv(gp->gp_uniform_modelview, 1, 0,
- glw_mtx_get(m) ?: identitymtx);
-
- glVertexAttribPointer(gp->gp_attribute_position,
- 3, GL_FLOAT, 0, sizeof(float) * VERTEX_SIZE,
- vertices);
-
- glVertexAttribPointer(gp->gp_attribute_color,
- 4, GL_FLOAT, 0, sizeof(float) * VERTEX_SIZE,
- vertices + 5);
+ glLoadMatrixf(glw_mtx_get(m) ?: identitymtx);
- if(gp->gp_attribute_texcoord != -1)
- glVertexAttribPointer(gp->gp_attribute_texcoord,
- 2, GL_FLOAT, 0, sizeof(float) * VERTEX_SIZE,
- vertices + 3);
+ glVertexPointer(3, GL_FLOAT, sizeof(float) * VERTEX_SIZE, vertices);
+ glColorPointer(4, GL_FLOAT, sizeof(float) * VERTEX_SIZE, vertices + 5);
+ glTexCoordPointer(2, GL_FLOAT, sizeof(float) * VERTEX_SIZE, vertices + 3);
if(indices != NULL)
glDrawElements(GL_TRIANGLES, num_triangles * 3,
@@ -447,11 +437,6 @@ glw_make_program(glw_backend_root_t *gbr, const char *title,
glUseProgram(p);
gbr->gbr_current = gp;
- gp->gp_attribute_position = glGetAttribLocation(p, "a_position");
- gp->gp_attribute_texcoord = glGetAttribLocation(p, "a_texcoord");
- gp->gp_attribute_color = glGetAttribLocation(p, "a_color");
-
- gp->gp_uniform_modelview = glGetUniformLocation(p, "u_modelview");
gp->gp_uniform_color = glGetUniformLocation(p, "u_color");
gp->gp_uniform_colormtx = glGetUniformLocation(p, "u_colormtx");
gp->gp_uniform_blend = glGetUniformLocation(p, "u_blend");
@@ -460,10 +445,6 @@ glw_make_program(glw_backend_root_t *gbr, const char *title,
#ifdef DEBUG_SHADERS
printf("Loaded %s\n", title);
- printf(" a_position = %d\n", gp->gp_attribute_position);
- printf(" a_texcoord = %d\n", gp->gp_attribute_texcoord);
- printf(" a_color = %d\n", gp->gp_attribute_color);
-
printf(" u_modelview = %d\n", gp->gp_uniform_modelview);
printf(" u_color = %d\n", gp->gp_uniform_color);
printf(" u_colormtx = %d\n", gp->gp_uniform_colormtx);
@@ -495,16 +476,6 @@ glw_load_program(glw_backend_root_t *gbr, glw_program_t *gp)
if(gbr->gbr_current == gp)
return;
- if(gbr->gbr_current != NULL) {
- glw_program_t *old = gbr->gbr_current;
- if(old->gp_attribute_position != -1)
- glDisableVertexAttribArray(old->gp_attribute_position);
- if(old->gp_attribute_texcoord != -1)
- glDisableVertexAttribArray(old->gp_attribute_texcoord);
- if(old->gp_attribute_color != -1)
- glDisableVertexAttribArray(old->gp_attribute_color);
- }
-
gbr->gbr_current = gp;
if(gp == NULL) {
@@ -513,27 +484,9 @@ glw_load_program(glw_backend_root_t *gbr, glw_program_t *gp)
}
glUseProgram(gp->gp_program);
-
- if(gp->gp_attribute_position != -1)
- glEnableVertexAttribArray(gp->gp_attribute_position);
- if(gp->gp_attribute_texcoord != -1)
- glEnableVertexAttribArray(gp->gp_attribute_texcoord);
- if(gp->gp_attribute_color != -1)
- glEnableVertexAttribArray(gp->gp_attribute_color);
}
-
-/**
- *
- */
-void
-glw_program_set_modelview(glw_backend_root_t *gbr, glw_rctx_t *rc)
-{
- const float *m = rc ? glw_mtx_get(rc->rc_mtx) : identitymtx;
- glUniformMatrix4fv(gbr->gbr_current->gp_uniform_modelview, 1, 0, m);
-}
-
/**
*
*/
@@ -602,60 +555,61 @@ glw_opengl_init_context(glw_root_t *gr)
TRACE(TRACE_INFO, "GLW", "OpenGL Renderer: '%s' by '%s'", renderer, vendor);
int use_shaders = 1;
-
+#if 0
if(strstr(renderer, "Mesa"))
use_shaders = 0;
-
+#endif
if(use_shaders) {
GLuint vs, fs;
- vs = glw_compile_shader("bundle://src/ui/glw/glsl/v1.glsl",
+ vs = glw_compile_shader(SHADERPATH"v1.glsl",
GL_VERTEX_SHADER);
- fs = glw_compile_shader("bundle://src/ui/glw/glsl/f_tex.glsl",
+ fs = glw_compile_shader(SHADERPATH"f_tex.glsl",
GL_FRAGMENT_SHADER);
gbr->gbr_renderer_tex = glw_make_program(gbr, "Texture", vs, fs);
glDeleteShader(fs);
- fs = glw_compile_shader("bundle://src/ui/glw/glsl/f_tex_blur.glsl",
+ fs = glw_compile_shader(SHADERPATH"f_tex_blur.glsl",
GL_FRAGMENT_SHADER);
- gbr->gbr_renderer_tex_blur = glw_make_program(gbr, "Texture", vs, fs);
+ gbr->gbr_renderer_tex_blur = glw_make_program(gbr, "BlurTexture", vs, fs);
glDeleteShader(fs);
- fs = glw_compile_shader("bundle://src/ui/glw/glsl/f_flat.glsl",
+ fs = glw_compile_shader(SHADERPATH"f_flat.glsl",
GL_FRAGMENT_SHADER);
gbr->gbr_renderer_flat = glw_make_program(gbr, "Flat", vs, fs);
glDeleteShader(fs);
glDeleteShader(vs);
- // gbr->gbr_renderer_draw = glw_renderer_shader;
-
-
// Video renderer
- vs = glw_compile_shader("bundle://src/ui/glw/glsl/yuv2rgb_v.glsl",
+ vs = glw_compile_shader(SHADERPATH "yuv2rgb_v.glsl",
GL_VERTEX_SHADER);
- fs = glw_compile_shader("bundle://src/ui/glw/glsl/yuv2rgb_1f_norm.glsl",
+ fs = glw_compile_shader(SHADERPATH "yuv2rgb_1f_norm.glsl",
GL_FRAGMENT_SHADER);
gbr->gbr_yuv2rgb_1f = glw_make_program(gbr, "yuv2rgb_1f_norm", vs, fs);
glDeleteShader(fs);
- fs = glw_compile_shader("bundle://src/ui/glw/glsl/yuv2rgb_2f_norm.glsl",
+ fs = glw_compile_shader(SHADERPATH "yuv2rgb_2f_norm.glsl",
GL_FRAGMENT_SHADER);
gbr->gbr_yuv2rgb_2f = glw_make_program(gbr, "yuv2rgb_2f_norm", vs, fs);
glDeleteShader(fs);
glDeleteShader(vs);
- gr->gr_render = shader_render;
+ gr->gr_render = shader_render;
prop_set_string(prop_create(gr->gr_uii.uii_prop, "rendermode"),
"OpenGL VP/FP shaders");
+ glEnableClientState(GL_COLOR_ARRAY);
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+
} else {
gr->gr_set_hw_clipper = hw_set_clip_conf;
@@ -665,15 +619,15 @@ glw_opengl_init_context(glw_root_t *gr)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
gr->gr_render = ff_render;
-
- glMatrixMode(GL_PROJECTION);
- glLoadMatrixf(projection);
- glMatrixMode(GL_MODELVIEW);
prop_set_string(prop_create(gr->gr_uii.uii_prop, "rendermode"),
"OpenGL fixed function");
}
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadMatrixf(projection);
+ glMatrixMode(GL_MODELVIEW);
return 0;
}
@@ -56,13 +56,7 @@ typedef struct glw_program {
const char *gp_title;
GLuint gp_program;
- // Attributes
- GLint gp_attribute_position;
- GLint gp_attribute_texcoord;
- GLint gp_attribute_color;
-
// Uniforms
- GLint gp_uniform_modelview;
GLint gp_uniform_color;
GLint gp_uniform_colormtx;
GLint gp_uniform_blend;
@@ -172,8 +166,6 @@ glw_program_t *glw_make_program(glw_backend_root_t *gbr,
void glw_load_program(glw_backend_root_t *gbr, glw_program_t *gp);
-void glw_program_set_modelview(glw_backend_root_t *gbr, struct glw_rctx *rc);
-
void glw_program_set_uniform_color(glw_backend_root_t *gbr,
float r, float g, float b, float a);
@@ -485,14 +485,15 @@ render_video_quad(int interlace, int rectmode, int width, int height,
glw_load_program(gbr, gp);
glw_program_set_uniform_color(gbr, 1, 1, 1, rc->rc_alpha);
- glw_program_set_modelview(gbr, rc);
+ glLoadMatrixf(glw_mtx_get(rc->rc_mtx));
if(gp->gp_uniform_blend != -1)
glUniform1f(gp->gp_uniform_blend, gv->gv_blend);
glUniformMatrix4fv(gp->gp_uniform_colormtx, 1, GL_FALSE, gv->gv_cmatrix_cur);
-
- glVertexAttribPointer(gp->gp_attribute_texcoord, 3, GL_FLOAT, 0, 0, tc);
- glVertexAttribPointer(gp->gp_attribute_position, 2, GL_FLOAT, 0, 0, vertices);
+
+ glVertexPointer(2, GL_FLOAT, 0, vertices);
+ glTexCoordPointer(3, GL_FLOAT, 0, tc);
+
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, elements);
}
@@ -0,0 +1,6 @@
+varying vec4 f_col_off;
+
+void main()
+{
+ gl_FragColor = gl_Color + f_col_off;
+}
@@ -0,0 +1,7 @@
+uniform sampler2D u_t0;
+varying vec4 f_col_off;
+
+void main()
+{
+ gl_FragColor = gl_Color * texture2D(u_t0, gl_TexCoord[0].st) + f_col_off;
+}
@@ -0,0 +1,21 @@
+uniform sampler2D u_t0;
+
+varying vec4 f_col_off;
+varying vec2 f_blur_amount;
+
+void main()
+{
+ vec4 col =
+ texture2D(u_t0, gl_TexCoord[0].st + vec2(f_blur_amount.x, 0)) +
+ texture2D(u_t0, gl_TexCoord[0].st - vec2(f_blur_amount.x, 0)) +
+ texture2D(u_t0, gl_TexCoord[0].st + vec2(0, f_blur_amount.y)) +
+ texture2D(u_t0, gl_TexCoord[0].st - vec2(0, f_blur_amount.y)) +
+ texture2D(u_t0, gl_TexCoord[0].st + vec2(f_blur_amount.x, f_blur_amount.y)) +
+ texture2D(u_t0, gl_TexCoord[0].st - vec2(f_blur_amount.x, f_blur_amount.y)) +
+ texture2D(u_t0, gl_TexCoord[0].st + vec2(-f_blur_amount.x, f_blur_amount.y)) +
+ texture2D(u_t0, gl_TexCoord[0].st - vec2(-f_blur_amount.x, f_blur_amount.y));
+
+ col = col * 0.12;
+
+ gl_FragColor = gl_Color * col + f_col_off;
+}
@@ -0,0 +1,16 @@
+uniform vec4 u_color;
+uniform vec4 u_color_offset;
+uniform vec2 u_blur_amount;
+
+varying vec4 f_col_mul;
+varying vec4 f_col_off;
+varying vec2 f_blur_amount;
+
+void main()
+{
+ gl_Position = ftransform();
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+ gl_FrontColor = gl_Color * clamp(u_color, 0.0, 1.0);
+ f_col_off = u_color_offset;
+ f_blur_amount = u_blur_amount;
+}
@@ -0,0 +1,18 @@
+uniform sampler2D u_t0;
+uniform sampler2D u_t1;
+uniform sampler2D u_t2;
+uniform mat4 u_colormtx;
+uniform vec4 u_color;
+
+void main()
+{
+ vec3 rgb;
+
+ rgb = vec3(u_colormtx * vec4(texture2D(u_t0, gl_TexCoord[0].xy).r,
+ texture2D(u_t1, gl_TexCoord[0].xy).r,
+ texture2D(u_t2, gl_TexCoord[0].xy).r,
+ 1));
+
+ gl_FragColor = vec4(rgb, u_color.a);
+}
+
@@ -0,0 +1,25 @@
+uniform sampler2D u_t0;
+uniform sampler2D u_t1;
+uniform sampler2D u_t2;
+uniform sampler2D u_t3;
+uniform sampler2D u_t4;
+uniform sampler2D u_t5;
+uniform mat4 u_colormtx;
+uniform vec4 u_color;
+uniform float u_blend;
+
+
+void main()
+{
+ vec3 rgb1 = vec3(u_colormtx * vec4(texture2D(u_t0, gl_TexCoord[0].xy).r,
+ texture2D(u_t1, gl_TexCoord[0].xy).r,
+ texture2D(u_t2, gl_TexCoord[0].xy).r,
+ 1));
+
+ vec3 rgb2 = vec3(u_colormtx * vec4(texture2D(u_t3, gl_TexCoord[0].xz).r,
+ texture2D(u_t4, gl_TexCoord[0].xz).r,
+ texture2D(u_t5, gl_TexCoord[0].xz).r,
+ 1));
+
+ gl_FragColor = vec4(mix(rgb2, rgb1, u_blend), u_color.a);
+}
@@ -0,0 +1,5 @@
+void main()
+{
+ gl_Position = ftransform();
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+}

0 comments on commit 33a3676

Please sign in to comment.