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_opengl.h"
14 #include "gropenglinternal.h"
17 #include "grinternal.h"
18 #include "systemvars.h"
19 #include "osregistry.h"
22 static int vram_full = 0;
24 typedef struct tcache_slot_opengl {
25 GLuint texture_handle;
26 float u_scale, v_scale;
34 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
35 tcache_slot_opengl *parent;
37 gr_texture_source texture_mode;
40 static void *Texture_sections = NULL;
41 static tcache_slot_opengl *Textures = NULL;
43 static tcache_slot_opengl *GL_bound_texture;
45 static int GL_frame_count = 0;
46 static int GL_last_bitmap_id = -1;
47 static int GL_last_detail = -1;
48 static int GL_last_bitmap_type = -1;
49 static int GL_last_section_x = -1;
50 static int GL_last_section_y = -1;
51 static int GL_should_preload = 0;
53 extern int Gr_textures_in;
55 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
57 static ubyte GL_xlat[256] = { 0 };
59 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
62 void opengl1_set_texture_state(gr_texture_source ts)
64 if (ts == TEXTURE_SOURCE_NONE) {
65 GL_bound_texture = NULL;
67 glBindTexture(GL_TEXTURE_2D, 0);
68 opengl1_tcache_set(-1, -1, NULL, NULL, 0, -1, -1, 0 );
69 } else if (GL_bound_texture &&
70 GL_bound_texture->texture_mode != ts) {
72 case TEXTURE_SOURCE_DECAL:
73 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76 case TEXTURE_SOURCE_NO_FILTERING:
77 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
78 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
84 GL_bound_texture->texture_mode = ts;
87 GL_current_texture_source = ts;
91 void opengl1_tcache_init()
95 if ( os_config_read_uint("Video", "PreloadTextures", 1) ) {
96 GL_should_preload = 1;
98 GL_should_preload = 0;
101 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
106 if (gr_screen.use_sections) {
107 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
108 if(!Texture_sections){
111 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
114 // Init the texture structures
115 int section_count = 0;
116 for( i=0; i<MAX_BITMAPS; i++ ) {
117 Textures[i].texture_handle = 0;
119 Textures[i].bitmap_id = -1;
120 Textures[i].size = 0;
121 Textures[i].used_this_frame = 0;
123 Textures[i].parent = NULL;
126 if (gr_screen.use_sections) {
127 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
128 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
129 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
130 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
131 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
132 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
133 Textures[i].data_sections[idx][s_idx]->size = 0;
134 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
138 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
139 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
140 Textures[i].data_sections[idx][s_idx] = NULL;
146 GL_last_detail = Detail.hardware_textures;
147 GL_last_bitmap_id = -1;
148 GL_last_bitmap_type = -1;
150 GL_last_section_x = -1;
151 GL_last_section_y = -1;
153 memset(GL_xlat, 0, sizeof(GL_xlat));
156 static int opengl1_free_texture ( tcache_slot_opengl *t )
162 if ( t->bitmap_id > -1 ) {
163 // if I, or any of my children have been used this frame, bail
164 if(t->used_this_frame == GL_frame_count){
168 if (gr_screen.use_sections) {
169 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
170 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
171 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
178 // ok, now we know its legal to free everything safely
179 t->texture_mode = (gr_texture_source) -1;
180 glDeleteTextures (1, &t->texture_handle);
181 t->texture_handle = 0;
183 if ( GL_last_bitmap_id == t->bitmap_id ) {
184 GL_last_bitmap_id = -1;
187 // if this guy has children, free them too, since the children
188 // actually make up his size
189 if (gr_screen.use_sections) {
190 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
191 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
192 if(t->data_sections[idx][s_idx] != NULL){
193 opengl1_free_texture(t->data_sections[idx][s_idx]);
200 t->used_this_frame = 0;
201 Gr_textures_in -= t->size;
208 void opengl1_tcache_flush()
212 for( i=0; i<MAX_BITMAPS; i++ ) {
213 opengl1_free_texture ( &Textures[i] );
215 if (Gr_textures_in != 0) {
216 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", Gr_textures_in ));
220 GL_last_bitmap_id = -1;
221 GL_last_section_x = -1;
222 GL_last_section_y = -1;
225 void opengl1_tcache_cleanup()
227 opengl1_tcache_flush ();
234 if( Texture_sections != NULL ){
235 free(Texture_sections);
236 Texture_sections = NULL;
240 void opengl1_tcache_frame()
242 GL_last_bitmap_id = -1;
249 for( i=0; i<MAX_BITMAPS; i++ ) {
250 Textures[i].used_this_frame = 0;
253 if(Textures[i].data_sections[0][0] != NULL){
254 SDL_assert(GL_texture_sections);
255 if(GL_texture_sections){
256 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
257 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
258 if(Textures[i].data_sections[idx][s_idx] != NULL){
259 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
269 opengl1_tcache_flush();
274 static void opengl1_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
280 if((w_out == NULL) || (h_out == NULL)){
288 // set height and width to a power of 2
289 for (i=0; i<16; i++ ) {
290 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
296 for (i=0; i<16; i++ ) {
297 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
303 // try to keep an 8:1 size ratio
309 if ( tex_w < GL_min_texture_width ) {
310 tex_w = GL_min_texture_width;
311 } else if ( tex_w > GL_max_texture_width ) {
312 tex_w = GL_max_texture_width;
315 if ( tex_h < GL_min_texture_height ) {
316 tex_h = GL_min_texture_height;
317 } else if ( tex_h > GL_max_texture_height ) {
318 tex_h = GL_max_texture_height;
321 // store the outgoing size
326 // bmp == bitmap structure with w, h, and data
327 // sx == x offset into bitmap
328 // sy == y offset into bitmap
329 // src_w == absolute width of section on source bitmap
330 // src_h == absolute height of section on source bitmap
331 // bmap_w == width of source bitmap
332 // bmap_h == height of source bitmap
333 // tex_w == width of final texture
334 // tex_h == height of final texture
335 static int opengl1_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap *bmp, tcache_slot_opengl *t, int sx, int sy, int src_w, int src_h, int tex_w, int tex_h, bool reload, bool resize, int fail_on_full)
338 if ( (bmp == NULL) || (t == NULL) ) {
342 if (t->used_this_frame == GL_frame_count) {
343 mprintf(("ARGHH!!! Texture already used this frame! Cannot free it!\n"));
348 if ( !opengl1_free_texture(t) ) {
352 glGenTextures(1, &t->texture_handle);
354 if ( !t->texture_handle ) {
355 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
360 switch (bitmap_type) {
361 case TCACHE_TYPE_AABITMAP:
362 t->u_scale = (float)bmp->w / (float)tex_w;
363 t->v_scale = (float)bmp->h / (float)tex_h;
366 case TCACHE_TYPE_BITMAP_INTERFACE:
367 case TCACHE_TYPE_BITMAP_SECTION:
368 t->u_scale = (float)src_w / (float)tex_w;
369 t->v_scale = (float)src_h / (float)tex_h;
378 t->texture_mode = TEXTURE_SOURCE_NO_FILTERING;
380 glBindTexture(GL_TEXTURE_2D, t->texture_handle);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
388 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
390 ubyte *bmp_data = (ubyte*)bmp->data;
391 ubyte *texmem = NULL, *texmemp;
395 switch (bitmap_type) {
396 case TCACHE_TYPE_AABITMAP: {
397 texmem = (ubyte *) malloc(tex_w * tex_h);
400 for (i = 0; i < tex_h; i++) {
401 for (j = 0;j < tex_w; j++) {
402 if ( (i < bmp->h) && (j < bmp->w) ) {
403 *texmemp++ = GL_xlat[bmp_data[i*bmp->w+j]];
410 size = tex_w * tex_h;
413 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
415 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
423 case TCACHE_TYPE_BITMAP_INTERFACE:
424 case TCACHE_TYPE_BITMAP_SECTION: {
425 // if we aren't resizing in any way then we can just use bmp_data directly
427 texmem = (ubyte *) malloc(tex_w * tex_h * 2);
430 for (i = 0;i < tex_h; i++) {
431 for (j = 0; j < tex_w; j++) {
432 if ( (i < src_h) && (j < src_w) ) {
433 *texmemp++ = bmp_data[((i+sy)*bmp->w+(j+sx))*2+0];
434 *texmemp++ = bmp_data[((i+sy)*bmp->w+(j+sx))*2+1];
443 size = tex_w * tex_h * 2;
446 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
448 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
459 // if we aren't resizing then we can just use bmp_data directly
461 texmem = (ubyte *) malloc (tex_w * tex_h * 2);
466 fix u = 0, utmp, v = 0, du, dv;
468 du = ((bmp->w - 1) * F1_0) / tex_w;
469 dv = ((bmp->h - 1) * F1_0) / tex_h;
471 for (j = 0; j < tex_h; j++) {
474 for (i = 0; i < tex_w; i++) {
475 *texmemp++ = bmp_data[(f2i(v)*bmp->w+f2i(utmp))*2+0];
476 *texmemp++ = bmp_data[(f2i(v)*bmp->w+f2i(utmp))*2+1];
485 size = tex_w * tex_h * 2;
488 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
490 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
501 t->bitmap_id = bitmap_handle;
502 t->time_created = GL_frame_count;
503 t->used_this_frame = 0;
505 t->w = (ushort)tex_w;
506 t->h = (ushort)tex_h;
509 Gr_textures_in += t->size;
515 static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
518 int final_w, final_h;
521 bool cull_size = false;
523 // setup texture/bitmap flags
525 case TCACHE_TYPE_AABITMAP:
526 flags |= BMP_AABITMAP;
529 case TCACHE_TYPE_NORMAL:
530 flags |= BMP_TEX_OTHER;
533 case TCACHE_TYPE_BITMAP_INTERFACE:
534 case TCACHE_TYPE_XPARENT:
535 flags |= BMP_TEX_XPARENT;
542 // lock the bitmap into the proper format
543 bitmap *bmp = bm_lock(bitmap_handle, bpp, flags);
545 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
552 if ( cull_size && (Detail.hardware_textures < 4) ) {
553 // if we are going to cull the size then we need to force a resize
556 // Detail.hardware_textures goes form 0 to 4
557 int val = 16 >> Detail.hardware_textures;
563 // get final texture size as it will be allocated as a DD surface
564 opengl1_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
566 if ( (final_w < 1) || (final_h < 1) ) {
567 mprintf(("Bitmap is too small at %dx%d.\n", final_w, final_h));
571 // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work
572 if ( (max_w != final_w) || (max_h != final_h) ) {
578 // see if we can reuse this slot for a new bitmap
579 if ( tslot->texture_handle && (tslot->bitmap_id != bitmap_handle) ) {
580 if ( (final_w == tslot->w) && (final_h == tslot->h) ) {
586 int ret_val = opengl1_create_texture_sub(bitmap_handle, bitmap_type, bmp, tslot, 0, 0, bmp->w, bmp->h, final_w, final_h, reload, resize, fail_on_full);
589 bm_unlock(bitmap_handle);
594 static int opengl1_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
596 int final_w, final_h;
597 int section_x, section_y;
600 SDL_assert( gr_screen.use_sections );
602 // setup texture/bitmap flags
603 SDL_assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
604 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
605 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
608 // lock the bitmap in the proper format
609 bitmap *bmp = bm_lock(bitmap_handle, 16, BMP_TEX_XPARENT);
611 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
614 // determine the width and height of this section
615 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
617 // get final texture size as it will be allocated as an opengl texture
618 opengl1_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
620 if ( (final_w < 1) || (final_h < 1) ) {
621 mprintf(("Bitmap is too small at %dx%d.\n", final_w, final_h));
625 // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work
626 if ( (bmp->sections.num_x == 1) && (bmp->sections.num_y == 1) && (section_x == final_w) && (section_y == final_h) ) {
632 // see if we can reuse this slot for a new bitmap
633 if ( tslot->texture_handle && (tslot->bitmap_id != bitmap_handle) ) {
634 if ( (final_w == tslot->w) && (final_h == tslot->h) ) {
640 int ret_val = opengl1_create_texture_sub(bitmap_handle, bitmap_type, bmp, tslot, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, final_w, final_h, reload, resize, fail_on_full);
643 bm_unlock(bitmap_handle);
648 int opengl1_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full, int sx, int sy, int force)
657 GL_last_bitmap_id = -1;
661 if ( GL_last_detail != Detail.hardware_textures ) {
662 GL_last_detail = Detail.hardware_textures;
663 opengl1_tcache_flush();
670 int n = bm_get_cache_slot (bitmap_id, 1);
671 tcache_slot_opengl *t = &Textures[n];
673 if ( (GL_last_bitmap_id == bitmap_id) && (GL_last_bitmap_type==bitmap_type) && (t->bitmap_id == bitmap_id) && (GL_last_section_x == sx) && (GL_last_section_y == sy)) {
674 t->used_this_frame = GL_frame_count;
676 // mark all children as used
677 if (gr_screen.use_sections) {
678 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
679 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
680 if(t->data_sections[idx][s_idx] != NULL){
681 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
687 *u_scale = t->u_scale;
688 *v_scale = t->v_scale;
692 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
693 SDL_assert( gr_screen.use_sections );
694 SDL_assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
695 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
701 // if the texture sections haven't been created yet
702 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
704 // lock the bitmap in the proper format
705 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
706 bm_unlock(bitmap_id);
708 // now lets do something for each texture
710 for(idx=0; idx<bmp->sections.num_x; idx++){
711 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
712 // hmm. i'd rather we didn't have to do it this way...
713 if(!opengl1_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
717 // not used this frame
718 t->data_sections[idx][s_idx]->used_this_frame = 0;
722 // zero out pretty much everything in the parent struct since he's just the root
723 t->bitmap_id = bitmap_id;
724 t->texture_handle = 0;
725 t->time_created = t->data_sections[sx][sy]->time_created;
726 t->used_this_frame = 0;
729 // argh. we failed to upload. free anything we can
731 opengl1_free_texture(t);
733 // swap in the texture we want
735 t = t->data_sections[sx][sy];
738 // all other "normal" textures
739 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
740 ret_val = opengl1_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
743 // everything went ok
744 if(ret_val && (t->texture_handle) && !vram_full){
745 *u_scale = t->u_scale;
746 *v_scale = t->v_scale;
748 GL_bound_texture = t;
750 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
752 GL_last_bitmap_id = t->bitmap_id;
753 GL_last_bitmap_type = bitmap_type;
754 GL_last_section_x = sx;
755 GL_last_section_y = sy;
757 t->used_this_frame = GL_frame_count;
761 GL_last_bitmap_id = -1;
762 GL_last_bitmap_type = -1;
764 GL_last_section_x = -1;
765 GL_last_section_y = -1;
767 GL_bound_texture = NULL;
769 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
776 void gr_opengl1_preload_init()
778 opengl1_tcache_flush();
781 int gr_opengl1_preload(int bitmap_num, int is_aabitmap)
783 if ( !GL_should_preload ) {
787 float u_scale, v_scale;
789 int bitmap_type = TCACHE_TYPE_NORMAL;
792 bitmap_type = TCACHE_TYPE_AABITMAP;
795 retval = opengl1_tcache_set(bitmap_num, bitmap_type, &u_scale, &v_scale, 1, -1, -1, 0 );
798 mprintf(("Texture upload failed!\n" ));
804 void gr_opengl1_set_gamma(float)
808 // set the alpha gamma settings (for fonts)
809 for (i = 0; i < 16; i++) {
810 GL_xlat[i] = (ubyte)Gr_gamma_lookup[(i*255)/15];
813 GL_xlat[15] = GL_xlat[1];
815 // Flush any existing textures
816 opengl1_tcache_flush();
819 void gr_opengl1_release_texture(int handle)
821 for(int i=0; i<MAX_BITMAPS; i++ ) {
822 if (Textures[i].bitmap_id == handle) {
823 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
824 opengl1_free_texture( &Textures[i] );