summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Marchesin <stephane.marchesin@gmail.com>2010-04-22 01:00:53 -0700
committerStephane Marchesin <stephane.marchesin@gmail.com>2010-04-22 01:00:53 -0700
commita7d10eda78d1b657973d34232b7f630f3438fac4 (patch)
tree52f70d8ae4c7edf0e79d5ccb4627bf132bf577f3
parent0b1329f231e114e981710adf6929360c4711d6fb (diff)
GL: should work everywhere now.
-rw-r--r--render-gl.c278
1 files changed, 13 insertions, 265 deletions
diff --git a/render-gl.c b/render-gl.c
index c5d6340..7b9d17b 100644
--- a/render-gl.c
+++ b/render-gl.c
@@ -20,25 +20,15 @@
#include "render.h"
-#define GLSL 1
-
-static PFNGLBINDFRAMEBUFFEREXTPROC pglBindFramebuffer;
-static PFNGLGENFRAMEBUFFERSEXTPROC pglGenFramebuffers;
-static PFNGLFRAMEBUFFERTEXTURE2DEXTPROC pglFramebufferTexture2D;
static PFNGLPROGRAMLOCALPARAMETER4FARBPROC pglProgramLocalParameter4f;
static PFNGLGENPROGRAMSARBPROC pglGenPrograms;
static PFNGLBINDPROGRAMARBPROC pglBindProgram;
static PFNGLPROGRAMSTRINGARBPROC pglProgramString;
static PFNGLGETPROGRAMIVARBPROC pglGetProgramiv;
-static PFNGLUSEPROGRAMOBJECTARBPROC pglUseProgramObjectARB;
-static PFNGLUNIFORM1IARBPROC pglUniform1iARB;
-static PFNGLGETUNIFORMLOCATIONARBPROC pglGetUniformLocationARB;
-static PFNGLUNIFORM2FARBPROC pglUniform2fARB;
static GLuint atlas_texture, palette_texture, page_coor_texture,
page_colour_texture, page_hoff_texture;
static GLuint tile_fragment_program, page_fragment_program;
-static char tile_glsl_program, page_glsl_program;
static GLuint render_tex;
static GLuint render_fb;
static int pix_size;
@@ -86,17 +76,8 @@ static void set_render_state(enum render_state render_state)
break;
case state_page:
-#if GLSL
- pglUseProgramObjectARB(page_glsl_program);
- pglUniform1iARB(pglGetUniformLocationARB(page_glsl_program, "atlas_texture"),0);
- pglUniform1iARB(pglGetUniformLocationARB(page_glsl_program, "palette_texture"),1);
- pglUniform1iARB(pglGetUniformLocationARB(page_glsl_program, "page_coor_texture"),2);
- pglUniform1iARB(pglGetUniformLocationARB(page_glsl_program, "page_colour_texture"),3);
- pglUniform1iARB(pglGetUniformLocationARB(page_glsl_program, "page_hoff_texture"),4);
-#else
glEnable(GL_FRAGMENT_PROGRAM_ARB);
pglBindProgram(GL_FRAGMENT_PROGRAM_ARB, page_fragment_program);
-#endif
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_2D, atlas_texture);
@@ -113,14 +94,8 @@ static void set_render_state(enum render_state render_state)
break;
case state_tile:
-#if GLSL
- pglUseProgramObjectARB(tile_glsl_program);
- pglUniform1iARB(pglGetUniformLocationARB(tile_glsl_program, "atlas_texture"),0);
- pglUniform1iARB(pglGetUniformLocationARB(tile_glsl_program, "palette_texture"),1);
-#else
glEnable(GL_FRAGMENT_PROGRAM_ARB);
pglBindProgram(GL_FRAGMENT_PROGRAM_ARB, tile_fragment_program);
-#endif
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_2D, atlas_texture);
@@ -137,11 +112,7 @@ static void set_render_state(enum render_state render_state)
break;
case state_rect:
-#if GLSL
- pglUseProgramObjectARB(0);
-#else
glDisable(GL_FRAGMENT_PROGRAM_ARB);
-#endif
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
@@ -156,11 +127,7 @@ static void set_render_state(enum render_state render_state)
break;
case state_final:
-#if GLSL
- pglUseProgramObjectARB(0);
-#else
glDisable(GL_FRAGMENT_PROGRAM_ARB);
-#endif
glColor3ub(255,255,255);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, render_tex);
@@ -229,18 +196,12 @@ void render_page(u32 xscroll, u32 yscroll, u32 h, u32 w)
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_LUMINANCE,
GL_UNSIGNED_SHORT, hoff);
-#if GLSL
- pglUniform2fARB( pglGetUniformLocationARB(page_glsl_program, "local0"), (float)xscroll/w, (float)yscroll/h );
- pglUniform2fARB( pglGetUniformLocationARB(page_glsl_program, "local1"), 512.0f/w, 256.0f/h );
- pglUniform2fARB( pglGetUniformLocationARB(page_glsl_program, "local2"), w/2048.0f, h/2048.0f );
-#else
pglProgramLocalParameter4f(GL_FRAGMENT_PROGRAM_ARB, 0,
(float)xscroll/w, (float)yscroll/h, 0, 0);
pglProgramLocalParameter4f(GL_FRAGMENT_PROGRAM_ARB, 1,
512.0f/w, 256.0f/h, 0, 0);
pglProgramLocalParameter4f(GL_FRAGMENT_PROGRAM_ARB, 2,
w/2048.0f, h/2048.0f, 0, 0);
-#endif
float tx0 = 0.0f;
float ty0 = 0.0f;
@@ -267,11 +228,7 @@ void render_texture(int x, int y, int w, int h, u16 atlas_x, u16 atlas_y, u8 pal
{
set_render_state(state_tile);
-#if GLSL
- pglUniform2fARB( pglGetUniformLocationARB(tile_glsl_program, "local0"), pal_offset/255.0, alpha/255.0 );
-#else
glColor4ub(pal_offset, 0, 0, alpha);
-#endif
error();
float tx0 = (float)atlas_x/ATLAS_W;
@@ -319,8 +276,6 @@ error();
void render_begin(void)
{
- pglBindFramebuffer(GL_FRAMEBUFFER_EXT, render_fb);
- glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
@@ -333,7 +288,9 @@ void render_begin(void)
void render_end(void)
{
- pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
+ glBindTexture(GL_TEXTURE_RECTANGLE_ARB, render_tex);
+ glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, 320, 240, 0);
+
glViewport(0, 0, 320 * pix_size, 240 * pix_size);
set_render_state(state_final);
@@ -367,7 +324,7 @@ static void init_textures(void)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, 128, 32, 0,
- GL_LUMINANCE_ALPHA, GL_UNSIGNED_SHORT, 0);
+ GL_LUMINANCE, GL_UNSIGNED_SHORT, 0);
glGenTextures(1, &page_colour_texture);
glBindTexture(GL_TEXTURE_2D, page_colour_texture);
@@ -384,13 +341,10 @@ static void init_textures(void)
GL_LUMINANCE, GL_UNSIGNED_SHORT, 0);
glGenTextures(1, &render_tex);
- pglGenFramebuffers(1, &render_fb);
- pglBindFramebuffer(GL_FRAMEBUFFER_EXT, render_fb);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, render_tex);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB8, 320, 240, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
- pglFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, render_tex, 0);
error();
}
@@ -484,7 +438,7 @@ static void init_fragment_programs(void)
"!!ARBfp1.0\n"
"OPTION ARB_precision_hint_nicest; \n"
- "TEMP coor, tile_coor, tile, tile_colour; \n"
+ "TEMP coor, tile_coor, tile, tile_colour, temptile; \n"
"TEMP atlas_coor, palette_coor, colour, hoff; \n"
"TEX hoff, fragment.texcoord[0], texture[4], 1D; \n" //**
@@ -501,8 +455,13 @@ static void init_fragment_programs(void)
"PARAM tile_scale = { 0.015625, 0.03125 }; \n"
"MUL tile_coor, tile_coor, tile_scale; \n"
- "TEX tile, tile_coor, texture[2], 2D; \n" //**
+ "TEX tile.x, tile_coor, texture[2], 2D; \n" //**
"TEX tile_colour, tile_coor, texture[3], 2D; \n"
+
+ "ADD tile_coor.x, tile_coor.x, 0.0078125; \n"
+ "TEX temptile.x, tile_coor, texture[2], 2D; \n" //**
+ "MOV tile.w, temptile.x;\n"
+
"MAD atlas_coor, atlas_coor, program.local[2], tile.xwww; \n"
"TEX palette_coor, atlas_coor, texture[0], 2D; \n" //**
@@ -516,204 +475,6 @@ static void init_fragment_programs(void)
init_fragment_program(&page_fragment_program, page_frag);
}
-#if 1
-PFNGLGETOBJECTPARAMETERIVARBPROC eglGetObjectParameterivARB =NULL;
-PFNGLGETINFOLOGARBPROC eglGetInfoLogARB =NULL;
-PFNGLCREATESHADEROBJECTARBPROC eglCreateShaderObjectARB =NULL;
-PFNGLSHADERSOURCEARBPROC eglShaderSourceARB =NULL;
-PFNGLCOMPILESHADERARBPROC eglCompileShaderARB =NULL;
-PFNGLCREATEPROGRAMOBJECTARBPROC eglCreateProgramObjectARB =NULL;
-PFNGLATTACHOBJECTARBPROC eglAttachObjectARB =NULL;
-PFNGLLINKPROGRAMARBPROC eglLinkProgramARB =NULL;
-PFNGLPROGRAMPARAMETERIEXTPROC eglProgramParameteriEXT =NULL;
-
-
-static void glsl_dump_log(GLhandleARB p,const GLcharARB* shader)
-{
- GLint log_size;
- eglGetObjectParameterivARB(p, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
- if (log_size>1)
- {
- GLcharARB* s=(GLcharARB*)malloc((log_size+1)*sizeof(GLcharARB));
- eglGetInfoLogARB(p,log_size,NULL,s);
- printf("============================================================\n");
- printf("Faulty ARB program. Log (%d bytes) : \n%s\n",log_size,s);
- free(s);
- printf("Faulty ARB program : \n");
- int lc=0;
- printf("\n%4d: ",++lc);
- for(unsigned i=0;i<strlen(shader);i++)
- {
- if (shader[i]=='\n')
- printf("\n%4d: ",++lc);
- else
- printf("%c",shader[i]);
- }
- printf("\n");
- printf("============================================================\n");
- }
-}
-
-
-static void init_glsl_program(char* prog, const char* vshader, const char* fshader)
-{
- eglGetObjectParameterivARB=(PFNGLGETOBJECTPARAMETERIVARBPROC)SDL_GL_GetProcAddress("glGetObjectParameterivARB");
- eglGetInfoLogARB=(PFNGLGETINFOLOGARBPROC)SDL_GL_GetProcAddress("glGetInfoLogARB");
- eglCreateShaderObjectARB=(PFNGLCREATESHADEROBJECTARBPROC)SDL_GL_GetProcAddress("glCreateShaderObjectARB");
- eglShaderSourceARB=(PFNGLSHADERSOURCEARBPROC)SDL_GL_GetProcAddress("glShaderSourceARB");
- eglCompileShaderARB=(PFNGLCOMPILESHADERARBPROC)SDL_GL_GetProcAddress("glCompileShaderARB");
- eglCreateProgramObjectARB=(PFNGLCREATEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glCreateProgramObjectARB");
- eglAttachObjectARB=(PFNGLATTACHOBJECTARBPROC)SDL_GL_GetProcAddress("glAttachObjectARB");
- eglLinkProgramARB=(PFNGLLINKPROGRAMARBPROC)SDL_GL_GetProcAddress("glLinkProgramARB");
-
- GLint ok;
-
- GLhandleARB vs=eglCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
- GLhandleARB fs=eglCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
- eglShaderSourceARB(vs, 1, &vshader,NULL);
- eglShaderSourceARB(fs, 1, &fshader,NULL);
- eglCompileShaderARB(vs);
- glsl_dump_log(vs,vshader);
- eglGetObjectParameterivARB(vs,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
- if (!ok)
- fatal("compiling vertex shader\n");
-
- eglCompileShaderARB(fs);
- glsl_dump_log(fs,fshader);
- eglGetObjectParameterivARB(fs,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
- if (!ok)
- fatal("compiling fragment shader\n");
-
- GLhandleARB p = eglCreateProgramObjectARB();
- eglAttachObjectARB(p,vs);
- eglAttachObjectARB(p,fs);
- eglLinkProgramARB(p);
- glsl_dump_log(p,"(Linking Stage)");
- eglGetObjectParameterivARB(p,GL_OBJECT_LINK_STATUS_ARB,&ok);
- if (!ok)
- fatal("linking program\n");
-
- *prog=p;
-}
-
-static void init_glsl_programs(void)
-{
- const char common_vert[] =
- "void main() \n"
- "{ \n"
- " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
- " gl_Position = ftransform(); \n"
- "} \n";
-
- /*
- * texture 0 2D atlas_texture
- * texture 1 1D palette_texture
- */
- const char tile_frag[] =
- "uniform sampler2D atlas_texture; \n"
- "uniform sampler1D palette_texture; \n"
- "uniform vec2 local0; \n"
- "void main() \n"
- "{ \n"
- " float index = texture2D(atlas_texture, gl_TexCoord[0]); \n"
- " index += local0.r; \n"
- " vec4 color = texture1D(palette_texture, index); \n"
- " color.a *= local0.g; \n"
- " gl_FragColor = color; \n"
- "} \n";
-/*
- "!!ARBfp1.0\n"
- "OPTION ARB_precision_hint_nicest; \n"
-
- "TEMP r0; \n"
-
- "TEX r0.x, fragment.texcoord[0], texture[0], 2D; \n"
- "ADD r0.x, r0.x, fragment.color.r; \n"
-
- "TEX r0, r0, texture[1], 1D; \n"
- "MUL r0.a, r0.a, fragment.color.a; \n"
- "MOV result.color, r0; \n"
-
- "END";*/
- init_glsl_program(&tile_glsl_program, common_vert, tile_frag);
-
- /*
- * texture 0 2D atlas_texture
- * texture 1 1D palette_texture
- * texture 2 2D page_coor_texture
- * texture 3 2D page_colour_texture
- * texture 4 1D page_hoff_texture
- */
-
- const char page_frag[] =
- "uniform sampler2D atlas_texture; \n"
- "uniform sampler1D palette_texture; \n"
- "uniform sampler2D page_coor_texture; \n"
- "uniform sampler2D page_colour_texture; \n"
- "uniform sampler1D page_hoff_texture; \n"
- "uniform vec2 local0; \n"
- "uniform vec2 local1; \n"
- "uniform vec2 local2; \n"
- "void main() \n"
- "{ \n"
- " float hoff = texture1D(page_hoff_texture, gl_TexCoord[0].x); \n"
- " vec2 coor = gl_TexCoord[0].xy; \n"
-
- " coor.y += hoff; \n"
- " coor = coor.yxxx * local1.xyyy + local0.xyyy; \n"
- " vec2 tile_coor = floor(coor) / vec2(64.0, 32.0); \n"
- " vec4 tile;\n"
- " tile.x = texture2D(page_coor_texture, tile_coor); \n"
- " tile.w = texture2D(page_coor_texture, tile_coor + vec2(1.0/128.0, 0.0) ); \n"
- " vec4 tile_colour = texture2D(page_colour_texture, tile_coor); \n"
-
- " vec2 atlas_coor = fract(coor) *local2 + tile.xwww; \n"
- " vec4 palette_coor = texture2D(atlas_texture, atlas_coor); \n"
- " palette_coor += tile_colour.r; \n"
-
- " vec4 colour = texture1D(palette_texture, palette_coor.x); \n"
- " colour.a *= tile_colour.a; \n"
- " gl_FragColor = colour; \n"
- "} \n";
-
-/*
- "!!ARBfp1.0\n"
- "OPTION ARB_precision_hint_nicest; \n"
-
- "TEMP coor, tile_coor, tile, tile_colour; \n"
- "TEMP atlas_coor, palette_coor, colour, hoff; \n"
-
- "TEX hoff, fragment.texcoord[0], texture[4], 1D; \n"
-
- "MOV coor, fragment.texcoord[0]; \n"
- "ADD coor.y, coor, hoff; \n"
- "MAD coor, coor.yxxx, program.local[1], program.local[0]; \n"
- // now coor is integer tile #, fraction coor within tile
-
- "FLR tile_coor, coor; \n"
- "FRC atlas_coor, coor; \n"
-
- // 1/64,1/32
- "PARAM tile_scale = { 0.015625, 0.03125 }; \n"
- "MUL tile_coor, tile_coor, tile_scale; \n"
-
- "TEX tile, tile_coor, texture[2], 2D; \n"
- "TEX tile_colour, tile_coor, texture[3], 2D; \n"
- "MAD atlas_coor, atlas_coor, program.local[2], tile.xwww; \n"
-
- "TEX palette_coor, atlas_coor, texture[0], 2D; \n"
- "ADD palette_coor, palette_coor, tile_colour.r; \n"
-
- "TEX colour, palette_coor, texture[1], 1D; \n"
- "MUL colour.a, colour.a, tile_colour.a; \n"
- "MOV result.color, colour; \n"
-
- "END";
-*/
- init_glsl_program(&page_glsl_program, common_vert, page_frag);
-}
-#endif
-
void render_init(u32 pixel_size)
{
printf("GL RENDERER: %s\n", glGetString(GL_RENDERER));
@@ -723,22 +484,13 @@ void render_init(u32 pixel_size)
pix_size = pixel_size;
- pglBindFramebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC) SDL_GL_GetProcAddress("glBindFramebufferEXT");
- pglGenFramebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC) SDL_GL_GetProcAddress("glGenFramebuffersEXT");
- pglFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
pglProgramLocalParameter4f = (PFNGLPROGRAMLOCALPARAMETER4FARBPROC) SDL_GL_GetProcAddress("glProgramLocalParameter4fARB");
pglGenPrograms = (PFNGLGENPROGRAMSARBPROC) SDL_GL_GetProcAddress("glGenProgramsARB");
pglBindProgram = (PFNGLBINDPROGRAMARBPROC) SDL_GL_GetProcAddress("glBindProgramARB");
pglProgramString = (PFNGLPROGRAMSTRINGARBPROC) SDL_GL_GetProcAddress("glProgramStringARB");
pglGetProgramiv = (PFNGLGETPROGRAMIVARBPROC) SDL_GL_GetProcAddress("glGetProgramivARB");
- pglUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glUseProgramObjectARB");
- pglUniform1iARB = (PFNGLUNIFORM1IARBPROC)SDL_GL_GetProcAddress("glUniform1i");
- pglGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC)SDL_GL_GetProcAddress("glGetUniformLocation");
- pglUniform2fARB = (PFNGLUNIFORM2FARBPROC)SDL_GL_GetProcAddress("glUniform2f");
-
- if (!( pglGenFramebuffers && pglBindFramebuffer && pglFramebufferTexture2D && pglProgramLocalParameter4f
- && pglGenPrograms && pglBindProgram && pglProgramString && pglGetProgramiv && pglUseProgramObjectARB
- && pglUniform1iARB && pglGetUniformLocationARB && pglUniform2fARB))
+
+ if (!( pglProgramLocalParameter4f && pglGenPrograms && pglBindProgram && pglProgramString && pglGetProgramiv ))
fatal("Sorry, your OpenGL misses some cool features\n");
glViewport(0, 0, pixel_size*320, pixel_size*240);
@@ -755,10 +507,6 @@ void render_init(u32 pixel_size)
error();
init_textures();
-#if GLSL
- init_glsl_programs();
-#else
init_fragment_programs();
-#endif
}