2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
9 #include "SDL_opengles2.h"
12 #include "gropenglinternal.h"
19 #include "grinternal.h"
21 #include "osregistry.h"
24 int GL_two_inited = 0;
26 bool Use_mipmaps = false;
28 static GLuint FB_texture = 0;
29 static GLuint FB_id = 0;
30 static GLuint FB_rb_id = 0;
32 static GLuint GL_saved_screen_tex = 0;
33 static GLuint GL_stream_tex = 0;
36 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
37 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
39 void opengl2_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
41 opengl2_set_texture_state(ts);
43 if (ab != GL_current_alpha_blend) {
45 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
46 glBlendFunc(GL_ONE, GL_ZERO);
48 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
49 glBlendFunc(GL_ONE, GL_ONE);
51 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
52 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
54 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
55 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
57 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
58 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
64 GL_current_alpha_blend = ab;
67 if (zt != GL_current_zbuffer_type) {
69 case ZBUFFER_TYPE_NONE:
70 glDepthFunc(GL_ALWAYS);
71 glDepthMask(GL_FALSE);
73 case ZBUFFER_TYPE_READ:
75 glDepthMask(GL_FALSE);
77 case ZBUFFER_TYPE_WRITE:
78 glDepthFunc(GL_ALWAYS);
81 case ZBUFFER_TYPE_FULL:
89 GL_current_zbuffer_type = zt;
93 static void opengl2_init_func_pointers()
95 gr_screen.gf_flip = gr_opengl2_flip;
96 gr_screen.gf_set_clip = gr_opengl2_set_clip;
97 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
99 gr_screen.gf_clear = gr_opengl_clear;
101 gr_screen.gf_aabitmap = gr_opengl2_aabitmap;
102 gr_screen.gf_aabitmap_ex = gr_opengl2_aabitmap_ex;
104 gr_screen.gf_rect = gr_opengl2_rect;
105 gr_screen.gf_shade = gr_opengl2_shade;
106 gr_screen.gf_string = gr_opengl2_string;
107 gr_screen.gf_circle = gr_opengl2_circle;
109 gr_screen.gf_line = gr_opengl2_line;
110 gr_screen.gf_aaline = gr_opengl2_aaline;
111 gr_screen.gf_pixel = gr_opengl2_pixel;
112 gr_screen.gf_scaler = gr_opengl2_scaler;
113 gr_screen.gf_tmapper = gr_opengl2_tmapper;
115 gr_screen.gf_gradient = gr_opengl2_gradient;
117 gr_screen.gf_print_screen = gr_opengl_print_screen;
119 gr_screen.gf_fade_in = gr_opengl2_fade_in;
120 gr_screen.gf_fade_out = gr_opengl2_fade_out;
121 gr_screen.gf_flash = gr_opengl2_flash;
123 gr_screen.gf_zbuffer_clear = gr_opengl2_zbuffer_clear;
125 gr_screen.gf_save_screen = gr_opengl2_save_screen;
126 gr_screen.gf_restore_screen = gr_opengl2_restore_screen;
127 gr_screen.gf_free_screen = gr_opengl2_free_screen;
129 gr_screen.gf_dump_frame_start = gr_opengl2_dump_frame_start;
130 gr_screen.gf_dump_frame_stop = gr_opengl2_dump_frame_stop;
131 gr_screen.gf_dump_frame = gr_opengl2_dump_frame;
133 gr_screen.gf_stream_start = gr_opengl2_stream_start;
134 gr_screen.gf_stream_frame = gr_opengl2_stream_frame;
135 gr_screen.gf_stream_stop = gr_opengl2_stream_stop;
137 gr_screen.gf_set_gamma = gr_opengl2_set_gamma;
139 gr_screen.gf_lock = gr_opengl_lock;
140 gr_screen.gf_unlock = gr_opengl_unlock;
142 gr_screen.gf_fog_set = gr_opengl2_fog_set;
144 gr_screen.gf_get_region = gr_opengl2_get_region;
146 gr_screen.gf_set_cull = gr_opengl_set_cull;
148 gr_screen.gf_cross_fade = gr_opengl2_cross_fade;
150 gr_screen.gf_preload_init = gr_opengl2_preload_init;
151 gr_screen.gf_preload = gr_opengl2_preload;
153 gr_screen.gf_zbias = gr_opengl_zbias;
155 gr_screen.gf_set_viewport = gr_opengl2_set_viewport;
157 gr_screen.gf_activate = gr_opengl_activate;
159 gr_screen.gf_release_texture = gr_opengl2_release_texture;
162 static int opengl2_create_framebuffer()
165 glGenTextures(1, &FB_texture);
166 glBindTexture(GL_TEXTURE_2D, FB_texture);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gr_screen.max_w, gr_screen.max_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
175 glBindTexture(GL_TEXTURE_2D, 0);
177 // create renderbuffer
178 glGenRenderbuffers(1, &FB_rb_id);
179 glBindRenderbuffer(GL_RENDERBUFFER, FB_rb_id);
181 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, gr_screen.max_w, gr_screen.max_h);
183 // create framebuffer
184 glGenFramebuffers(1, &FB_id);
185 glBindFramebuffer(GL_FRAMEBUFFER, FB_id);
187 // attach texture and renderbuffer
188 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FB_texture, 0);
189 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, FB_rb_id);
191 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
193 glBindFramebuffer(GL_FRAMEBUFFER, 0);
195 if (status != GL_FRAMEBUFFER_COMPLETE) {
196 glBindFramebuffer(GL_FRAMEBUFFER, 0);
199 glDeleteTextures(1, &FB_texture);
204 glDeleteRenderbuffers(1, &FB_rb_id);
209 glDeleteFramebuffers(1, &FB_id);
219 void opengl2_cleanup()
221 if ( !GL_two_inited ) {
225 glBindFramebuffer(GL_FRAMEBUFFER, 0);
228 glDeleteTextures(1, &FB_texture);
233 glDeleteRenderbuffers(1, &FB_rb_id);
238 glDeleteFramebuffers(1, &FB_id);
242 opengl2_tcache_cleanup();
243 opengl2_shader_cleanup();
246 SDL_GL_DeleteContext(GL_context);
261 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
262 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
263 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
265 GL_context = SDL_GL_CreateContext(GL_window);
272 mprintf((" Vendor : %s\n", glGetString(GL_VENDOR)));
273 mprintf((" Renderer : %s\n", glGetString(GL_RENDERER)));
274 mprintf((" Version : %s\n", glGetString(GL_VERSION)));
276 // initial viewport setup
277 gr_opengl2_set_viewport(gr_screen.max_w, gr_screen.max_h);
279 // set up generic variables
280 opengl_set_variables();
282 opengl2_init_func_pointers();
283 opengl2_tcache_init();
285 if ( !opengl2_shader_init() ) {
290 if ( !opengl2_create_framebuffer() ) {
296 glEnable(GL_DEPTH_TEST);
298 glEnable(GL_TEXTURE_2D);
300 glDepthRangef(0.0f, 1.0f);
302 glPixelStorei(GL_PACK_ALIGNMENT, 1);
303 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
305 if ( SDL_GL_ExtensionSupported("GL_OES_texture_npot") ) {
309 mprintf((" Mipmaps : %s\n", Use_mipmaps ? "Enabled" : "Disabled"));
314 gr_opengl_set_cull(1);
319 void gr_opengl2_flip()
321 if ( !GL_two_inited ) {
326 static bool show_cursor = false;
329 glBindFramebuffer(GL_FRAMEBUFFER, 0);
331 gr_opengl_reset_clip();
333 // set viewport to window size
334 glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
336 glClear(GL_COLOR_BUFFER_BIT);
341 float w = i2fl(GL_viewport_w);
342 float h = i2fl(GL_viewport_h);
344 const float tex_coord[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };
345 const float ver_coord[] = { x, y, x, h, w, y, w, h };
347 opengl2_shader_use(PROG_WINDOW);
349 glEnableVertexAttribArray(SDRI_POSITION);
350 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
352 glEnableVertexAttribArray(SDRI_TEXCOORD);
353 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
355 glBindTexture(GL_TEXTURE_2D, FB_texture);
357 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
359 glBindTexture(GL_TEXTURE_2D, 0);
361 glDisableVertexAttribArray(SDRI_TEXCOORD);
362 glDisableVertexAttribArray(SDRI_POSITION);
367 if ( mouse_is_visible() ) {
370 mouse_get_pos(&mx, &my);
372 float u_scale, v_scale;
374 if ( opengl2_tcache_set(Gr_cursor, TCACHE_TYPE_BITMAP_INTERFACE, &u_scale, &v_scale, 0) ) {
375 opengl2_set_state(TEXTURE_SOURCE_DECAL, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE);
378 bm_get_info(Gr_cursor, &bw, &bh);
380 float x = i2fl(mx) * GL_viewport_scale_w;
381 float y = i2fl(my) * GL_viewport_scale_h;
382 float w = x + (bw * GL_viewport_scale_w);
383 float h = y + (bh * GL_viewport_scale_h);
385 const float tex_coord[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
386 const float ver_coord[] = { x, y, x, h, w, y, w, h };
388 opengl2_shader_use(PROG_WINDOW);
390 glEnableVertexAttribArray(SDRI_POSITION);
391 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
393 glEnableVertexAttribArray(SDRI_TEXCOORD);
394 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
396 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
398 glDisableVertexAttribArray(SDRI_TEXCOORD);
399 glDisableVertexAttribArray(SDRI_POSITION);
402 else if ( !show_cursor ) {
410 GLenum error = GL_NO_ERROR;
413 error = glGetError();
415 if (error != GL_NO_ERROR) {
416 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
418 } while (error != GL_NO_ERROR);
421 SDL_GL_SwapWindow(GL_window);
423 opengl2_tcache_frame();
425 glBindFramebuffer(GL_FRAMEBUFFER, FB_id);
427 // set viewport to game screen size
428 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
431 void gr_opengl2_set_clip(int x, int y, int w, int h)
433 // check for sanity of parameters
434 CAP(x, 0, gr_screen.max_w - 1);
435 CAP(y, 0, gr_screen.max_h - 1);
436 CAP(w, 0, gr_screen.max_w - x);
437 CAP(h, 0, gr_screen.max_h - y);
439 gr_screen.offset_x = x;
440 gr_screen.offset_y = y;
441 gr_screen.clip_left = 0;
442 gr_screen.clip_right = w-1;
443 gr_screen.clip_top = 0;
444 gr_screen.clip_bottom = h-1;
445 gr_screen.clip_width = w;
446 gr_screen.clip_height = h;
448 glEnable(GL_SCISSOR_TEST);
449 glScissor(x, gr_screen.max_h-y-h, w, h);
452 void gr_opengl2_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
454 gr_screen.current_fog_mode = fog_mode;
456 if (fog_mode == GR_FOGMODE_NONE) {
460 gr_screen.fog_near = fog_near;
461 gr_screen.fog_far = fog_far;
463 gr_init_color(&gr_screen.current_fog_color, r, g, b);
466 void gr_opengl2_zbuffer_clear(int mode)
470 Gr_zbuffering_mode = GR_ZBUFF_FULL;
471 Gr_global_zbuffering = 1;
473 opengl2_set_state(TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL);
474 glClear(GL_DEPTH_BUFFER_BIT);
477 Gr_zbuffering_mode = GR_ZBUFF_NONE;
478 Gr_global_zbuffering = 0;
482 void gr_opengl2_fade_in(int instantaneous)
487 void gr_opengl2_fade_out(int instantaneous)
492 void gr_opengl2_get_region(int front, int w, int h, ubyte *data)
494 opengl2_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
496 GLenum pxtype = GL_UNSIGNED_SHORT_5_5_5_1;
498 if (gr_screen.bytes_per_pixel == 4) {
499 pxtype = GL_UNSIGNED_BYTE;
502 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, pxtype, data);
505 int gr_opengl2_save_screen()
507 gr_opengl_reset_clip();
509 if (GL_saved_screen_tex) {
510 mprintf(( "Screen already saved!\n" ));
514 glGenTextures(1, &GL_saved_screen_tex);
516 if ( !GL_saved_screen_tex ) {
517 mprintf(( "Couldn't create texture for saved screen!\n" ));
521 glBindTexture(GL_TEXTURE_2D, GL_saved_screen_tex);
523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
525 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
526 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
528 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0,
529 gr_screen.max_w, gr_screen.max_h, 0);
531 glBindTexture(GL_TEXTURE_2D, 0);
536 void gr_opengl2_restore_screen(int)
538 gr_opengl_reset_clip();
540 if ( !GL_saved_screen_tex ) {
547 float w = i2fl(gr_screen.max_w);
548 float h = i2fl(gr_screen.max_h);
550 const float tex_coord[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f }; // y-flipped
551 const float ver_coord[] = { x, y, x, h, w, y, w, h };
553 opengl2_shader_use(PROG_TEX);
555 glVertexAttrib4f(SDRI_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
557 glEnableVertexAttribArray(SDRI_POSITION);
558 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
560 glEnableVertexAttribArray(SDRI_TEXCOORD);
561 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
563 glBindTexture(GL_TEXTURE_2D, GL_saved_screen_tex);
565 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
567 glBindTexture(GL_TEXTURE_2D, 0);
569 glDisableVertexAttribArray(SDRI_TEXCOORD);
570 glDisableVertexAttribArray(SDRI_POSITION);
573 void gr_opengl2_free_screen(int)
575 if (GL_saved_screen_tex) {
576 glDeleteTextures(1, &GL_saved_screen_tex);
577 GL_saved_screen_tex = 0;
581 void gr_opengl2_dump_frame_start(int first_frame, int frames_between_dumps)
586 void gr_opengl2_dump_frame_stop()
591 void gr_opengl2_dump_frame()
596 static int GL_stream_w = 0;
597 static int GL_stream_h = 0;
599 static rb_t GL_stream[4];
601 void gr_opengl2_stream_start(int x, int y, int w, int h)
607 glGenTextures(1, &GL_stream_tex);
609 glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
611 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
612 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
613 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
614 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
616 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
618 glBindTexture(GL_TEXTURE_2D, 0);
620 uint scale = os_config_read_uint("Video", "ScaleMovies", 1);
626 sx = scale ? 0 : ((gr_screen.max_w - w) / 2);
631 float h_factor = scale ? (gr_screen.max_w / i2fl(w)) : 1.0f;
634 sy = (gr_screen.max_h - fl2i(h * h_factor)) / 2;
643 sw = gr_screen.max_w - (sx * 2);
644 sh = gr_screen.max_h - (sy * 2);
650 GL_stream[0].x = i2fl(sx);
651 GL_stream[0].y = i2fl(sy);
652 GL_stream[0].u = 0.0f;
653 GL_stream[0].v = 0.0f;
655 GL_stream[1].x = i2fl(sx);
656 GL_stream[1].y = i2fl(sy + sh);
657 GL_stream[1].u = 0.0f;
658 GL_stream[1].v = 1.0f;
660 GL_stream[2].x = i2fl(sx + sw);
661 GL_stream[2].y = i2fl(sy);
662 GL_stream[2].u = 1.0f;
663 GL_stream[2].v = 0.0f;
665 GL_stream[3].x = i2fl(sx + sw);
666 GL_stream[3].y = i2fl(sy + sh);
667 GL_stream[3].u = 1.0f;
668 GL_stream[3].v = 1.0f;
670 glDisable(GL_DEPTH_TEST);
673 void gr_opengl2_stream_frame(ubyte *frame)
675 if ( !GL_stream_tex ) {
679 opengl2_shader_use(PROG_TEX);
681 glVertexAttrib4f(SDRI_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
683 glEnableVertexAttribArray(SDRI_POSITION);
684 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &GL_stream[0].x);
686 glEnableVertexAttribArray(SDRI_TEXCOORD);
687 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &GL_stream[0].u);
689 glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
691 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GL_stream_w, GL_stream_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, frame);
693 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
695 glBindTexture(GL_TEXTURE_2D, 0);
697 glDisableVertexAttribArray(SDRI_TEXCOORD);
698 glDisableVertexAttribArray(SDRI_POSITION);
701 void gr_opengl2_stream_stop()
704 glBindTexture(GL_TEXTURE_2D, 0);
705 glDeleteTextures(1, &GL_stream_tex);
708 glEnable(GL_DEPTH_TEST);
712 void gr_opengl2_set_viewport(int width, int height)
716 float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
719 h = fl2i((width / ratio) + 0.5f);
723 w = fl2i((height * ratio) + 0.5f);
727 y = (height - h) / 2;
733 GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
734 GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
736 opengl2_shader_update();