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
13 #include "gropenglinternal.h"
15 #include "grinternal.h"
16 #include "systemvars.h"
17 #include "osregistry.h"
22 typedef struct tcache_slot_opengl {
23 GLuint texture_handle;
24 float u_scale, v_scale;
32 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
33 tcache_slot_opengl *parent;
35 gr_texture_source texture_mode;
38 static void *Texture_sections = NULL;
39 static tcache_slot_opengl *Textures = NULL;
41 static tcache_slot_opengl *GL_bound_texture;
43 static int GL_frame_count = 0;
44 static int GL_last_bitmap_id = -1;
45 static int GL_last_detail = -1;
46 static int GL_last_bitmap_type = -1;
47 static int GL_last_section_x = -1;
48 static int GL_last_section_y = -1;
49 static int GL_should_preload = 0;
51 extern int Gr_textures_in;
53 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
55 static ubyte GL_xlat[256] = { 0 };
57 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
60 void opengl1_set_texture_state(gr_texture_source ts)
62 if (ts == TEXTURE_SOURCE_NONE) {
63 GL_bound_texture = NULL;
65 glBindTexture(GL_TEXTURE_2D, 0);
66 opengl1_tcache_set(-1, -1, NULL, NULL, 0, -1, -1, 0 );
67 } else if (GL_bound_texture &&
68 GL_bound_texture->texture_mode != ts) {
70 case TEXTURE_SOURCE_DECAL:
71 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
72 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
74 case TEXTURE_SOURCE_NO_FILTERING:
75 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
76 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
82 GL_bound_texture->texture_mode = ts;
85 GL_current_texture_source = ts;
89 void opengl1_tcache_init()
93 if ( os_config_read_uint("Video", "PreloadTextures", 1) ) {
94 GL_should_preload = 1;
96 GL_should_preload = 0;
99 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
104 if (gr_screen.use_sections) {
105 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
106 if(!Texture_sections){
109 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
112 // Init the texture structures
113 int section_count = 0;
114 for( i=0; i<MAX_BITMAPS; i++ ) {
115 Textures[i].texture_handle = 0;
117 Textures[i].bitmap_id = -1;
118 Textures[i].size = 0;
119 Textures[i].used_this_frame = 0;
121 Textures[i].parent = NULL;
124 if (gr_screen.use_sections) {
125 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
126 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
127 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
128 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
129 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
130 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
131 Textures[i].data_sections[idx][s_idx]->size = 0;
132 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
136 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
137 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
138 Textures[i].data_sections[idx][s_idx] = NULL;
144 GL_last_detail = Detail.hardware_textures;
145 GL_last_bitmap_id = -1;
146 GL_last_bitmap_type = -1;
148 GL_last_section_x = -1;
149 GL_last_section_y = -1;
151 memset(GL_xlat, 0, sizeof(GL_xlat));
154 static int opengl1_free_texture ( tcache_slot_opengl *t )
160 if ( t->bitmap_id > -1 ) {
161 // if I, or any of my children have been used this frame, bail
162 if(t->used_this_frame == GL_frame_count){
166 if (gr_screen.use_sections) {
167 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
168 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
169 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
176 // ok, now we know its legal to free everything safely
177 t->texture_mode = (gr_texture_source) -1;
178 glDeleteTextures (1, &t->texture_handle);
179 t->texture_handle = 0;
181 if ( GL_last_bitmap_id == t->bitmap_id ) {
182 GL_last_bitmap_id = -1;
185 // if this guy has children, free them too, since the children
186 // actually make up his size
187 if (gr_screen.use_sections) {
188 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
189 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
190 if(t->data_sections[idx][s_idx] != NULL){
191 opengl1_free_texture(t->data_sections[idx][s_idx]);
198 t->used_this_frame = 0;
199 Gr_textures_in -= t->size;
206 void opengl1_tcache_flush()
210 for( i=0; i<MAX_BITMAPS; i++ ) {
211 opengl1_free_texture ( &Textures[i] );
213 if (Gr_textures_in != 0) {
214 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", Gr_textures_in ));
218 GL_last_bitmap_id = -1;
219 GL_last_section_x = -1;
220 GL_last_section_y = -1;
223 void opengl1_tcache_cleanup()
225 opengl1_tcache_flush ();
232 if( Texture_sections != NULL ){
233 free(Texture_sections);
234 Texture_sections = NULL;
238 void opengl1_tcache_frame()
240 GL_last_bitmap_id = -1;
247 for( i=0; i<MAX_BITMAPS; i++ ) {
248 Textures[i].used_this_frame = 0;
251 if(Textures[i].data_sections[0][0] != NULL){
252 SDL_assert(GL_texture_sections);
253 if(GL_texture_sections){
254 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
255 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
256 if(Textures[i].data_sections[idx][s_idx] != NULL){
257 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
267 opengl1_tcache_flush();
272 static void opengl1_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
278 if((w_out == NULL) || (h_out == NULL)){
286 // set height and width to a power of 2
287 for (i=0; i<16; i++ ) {
288 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
294 for (i=0; i<16; i++ ) {
295 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
301 // try to keep an 8:1 size ratio
307 if ( tex_w < GL_min_texture_width ) {
308 tex_w = GL_min_texture_width;
309 } else if ( tex_w > GL_max_texture_width ) {
310 tex_w = GL_max_texture_width;
313 if ( tex_h < GL_min_texture_height ) {
314 tex_h = GL_min_texture_height;
315 } else if ( tex_h > GL_max_texture_height ) {
316 tex_h = GL_max_texture_height;
319 // store the outgoing size
324 // data == start of bitmap data
325 // sx == x offset into bitmap
326 // sy == y offset into bitmap
327 // src_w == absolute width of section on source bitmap
328 // src_h == absolute height of section on source bitmap
329 // bmap_w == width of source bitmap
330 // bmap_h == height of source bitmap
331 // tex_w == width of final texture
332 // tex_h == height of final texture
333 static int opengl1_create_texture_sub(int bitmap_type, int texture_handle, ushort *data, int sx, int sy, int src_w, int src_h, int bmap_w, int bmap_h, int tex_w, int tex_h, tcache_slot_opengl *t, int reload, int resize, int fail_on_full)
338 ubyte *bmp_data = ((ubyte*)data);
339 ubyte *texmem = NULL, *texmemp;
342 if((t == NULL) || (bmp_data == NULL)){
346 if ( t->used_this_frame == GL_frame_count ) {
347 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
352 if(!opengl1_free_texture(t)){
357 switch (bitmap_type) {
358 case TCACHE_TYPE_AABITMAP:
359 t->u_scale = (float)bmap_w / (float)tex_w;
360 t->v_scale = (float)bmap_h / (float)tex_h;
363 case TCACHE_TYPE_BITMAP_INTERFACE:
364 case TCACHE_TYPE_BITMAP_SECTION:
365 t->u_scale = (float)src_w / (float)tex_w;
366 t->v_scale = (float)src_h / (float)tex_h;
376 glGenTextures (1, &t->texture_handle);
379 if (t->texture_handle == 0) {
380 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
384 GL_bound_texture = t;
386 GL_bound_texture->texture_mode = (gr_texture_source) -1;
388 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
390 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
391 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
392 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
394 /* this should be set next anyway */
395 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
396 // gr_opengl_set_texture_state(GL_current_texture_source);
399 switch (bitmap_type) {
400 case TCACHE_TYPE_AABITMAP:
402 texmem = (ubyte *) malloc (tex_w*tex_h);
405 for (i=0;i<tex_h;i++)
407 for (j=0;j<tex_w;j++)
409 if (i < bmap_h && j < bmap_w) {
410 *texmemp++ = GL_xlat[bmp_data[i*bmap_w+j]];
420 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
422 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
430 case TCACHE_TYPE_BITMAP_INTERFACE:
431 case TCACHE_TYPE_BITMAP_SECTION:
433 // if we aren't resizing in any way then we can just use bmp_data directly
435 texmem = (ubyte *) malloc (tex_w*tex_h*2);
438 for (i=0;i<tex_h;i++) {
439 for (j=0;j<tex_w;j++) {
440 if (i < src_h && j < src_w) {
441 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
442 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
451 size = tex_w*tex_h*2;
454 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);
456 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
467 // if we aren't resizing then we can just use bmp_data directly
469 texmem = (ubyte *) malloc (tex_w*tex_h*2);
472 SDL_assert( texmem != NULL );
474 fix u, utmp, v, du, dv;
478 du = ( (bmap_w-1)*F1_0 ) / tex_w;
479 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
481 for (j=0;j<tex_h;j++) {
483 for (i=0;i<tex_w;i++) {
484 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
485 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
492 size = tex_w*tex_h*2;
495 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);
497 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data);
507 t->bitmap_id = texture_handle;
508 t->time_created = GL_frame_count;
509 t->used_this_frame = 0;
511 t->w = (ushort)tex_w;
512 t->h = (ushort)tex_h;
515 Gr_textures_in += t->size;
521 static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
525 int final_w, final_h;
531 // setup texture/bitmap flags
534 case TCACHE_TYPE_AABITMAP:
535 flags |= BMP_AABITMAP;
538 case TCACHE_TYPE_NORMAL:
539 flags |= BMP_TEX_OTHER;
542 case TCACHE_TYPE_BITMAP_INTERFACE:
543 case TCACHE_TYPE_XPARENT:
544 flags |= BMP_TEX_XPARENT;
551 // lock the bitmap into the proper format
552 bmp = bm_lock(bitmap_handle, bpp, flags);
554 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
562 // if we are going to cull the size then we need to force a resize
563 if (Detail.hardware_textures < 4) {
566 // Detail.debris_culling goes from 0 to 4.
567 max_w /= (16 >> Detail.hardware_textures);
568 max_h /= (16 >> Detail.hardware_textures);
573 // get final texture size as it will be allocated as a DD surface
574 opengl1_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
576 if ( (final_w < 1) || (final_h < 1) ) {
577 mprintf(("Bitmap is too small at %dx%d.\n", final_w, final_h));
581 // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work
582 if ( (max_w != final_w) || (max_h != final_h) ) {
586 // if this tcache slot has no bitmap
587 if ( tslot->bitmap_id < 0) {
590 // different bitmap altogether - determine if the new one can use the old one's slot
591 else if (tslot->bitmap_id != bitmap_handle) {
592 if((final_w == tslot->w) && (final_h == tslot->h)){
594 //ml_printf("Reloading texture %d\n", bitmap_handle);
601 int ret_val = opengl1_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, final_w, final_h, tslot, reload, resize, fail_on_full);
604 bm_unlock(bitmap_handle);
609 static int opengl1_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
613 int final_w, final_h;
614 int section_x, section_y;
618 SDL_assert( gr_screen.use_sections );
620 // setup texture/bitmap flags
621 SDL_assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
622 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
623 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
625 flags = BMP_TEX_XPARENT;
627 // lock the bitmap in the proper format
628 bmp = bm_lock(bitmap_handle, 16, flags);
630 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
633 // determine the width and height of this section
634 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
636 // get final texture size as it will be allocated as an opengl texture
637 opengl1_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
639 if ( (final_w < 1) || (final_h < 1) ) {
640 mprintf(("Bitmap is too small at %dx%d.\n", final_w, final_h));
644 // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work
645 if ( (bmp->sections.num_x == 1) && (bmp->sections.num_y == 1) && (section_x == final_w) && (section_y == final_h) ) {
649 // if this tcache slot has no bitmap
650 if ( tslot->bitmap_id < 0) {
653 // different bitmap altogether - determine if the new one can use the old one's slot
654 else if (tslot->bitmap_id != bitmap_handle) {
655 if((final_w == tslot->w) && (final_h == tslot->h)){
663 int ret_val = opengl1_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, bmp->w, bmp->h, final_w, final_h, tslot, reload, resize, fail_on_full);
666 bm_unlock(bitmap_handle);
671 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)
680 GL_last_bitmap_id = -1;
684 if ( GL_last_detail != Detail.hardware_textures ) {
685 GL_last_detail = Detail.hardware_textures;
686 opengl1_tcache_flush();
693 int n = bm_get_cache_slot (bitmap_id, 1);
694 tcache_slot_opengl *t = &Textures[n];
696 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)) {
697 t->used_this_frame = GL_frame_count;
699 // mark all children as used
700 if (gr_screen.use_sections) {
701 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
702 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
703 if(t->data_sections[idx][s_idx] != NULL){
704 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
710 *u_scale = t->u_scale;
711 *v_scale = t->v_scale;
715 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
716 SDL_assert( gr_screen.use_sections );
717 SDL_assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
718 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
724 // if the texture sections haven't been created yet
725 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
727 // lock the bitmap in the proper format
728 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
729 bm_unlock(bitmap_id);
731 // now lets do something for each texture
733 for(idx=0; idx<bmp->sections.num_x; idx++){
734 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
735 // hmm. i'd rather we didn't have to do it this way...
736 if(!opengl1_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
740 // not used this frame
741 t->data_sections[idx][s_idx]->used_this_frame = 0;
745 // zero out pretty much everything in the parent struct since he's just the root
746 t->bitmap_id = bitmap_id;
747 t->texture_handle = 0;
748 t->time_created = t->data_sections[sx][sy]->time_created;
749 t->used_this_frame = 0;
752 // argh. we failed to upload. free anything we can
754 opengl1_free_texture(t);
756 // swap in the texture we want
758 t = t->data_sections[sx][sy];
761 // all other "normal" textures
762 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
763 ret_val = opengl1_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
766 // everything went ok
767 if(ret_val && (t->texture_handle) && !vram_full){
768 *u_scale = t->u_scale;
769 *v_scale = t->v_scale;
771 GL_bound_texture = t;
773 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
775 /* this should be set next anyway */
776 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
777 // gr_opengl_set_texture_state(GL_current_texture_source);
780 GL_last_bitmap_id = t->bitmap_id;
781 GL_last_bitmap_type = bitmap_type;
782 GL_last_section_x = sx;
783 GL_last_section_y = sy;
785 t->used_this_frame = GL_frame_count;
789 GL_last_bitmap_id = -1;
790 GL_last_bitmap_type = -1;
792 GL_last_section_x = -1;
793 GL_last_section_y = -1;
795 GL_bound_texture = NULL;
797 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
804 void gr_opengl1_preload_init()
806 // if (gr_screen.mode != GR_OPENGL) {
810 opengl1_tcache_flush();
813 int gr_opengl1_preload(int bitmap_num, int is_aabitmap)
815 // if ( gr_screen.mode != GR_OPENGL) {
819 if ( !GL_should_preload ) {
823 float u_scale, v_scale;
825 int bitmap_type = TCACHE_TYPE_NORMAL;
828 bitmap_type = TCACHE_TYPE_AABITMAP;
831 retval = opengl1_tcache_set(bitmap_num, bitmap_type, &u_scale, &v_scale, 1, -1, -1, 0 );
834 mprintf(("Texture upload failed!\n" ));
840 void gr_opengl1_set_gamma(float)
844 // set the alpha gamma settings (for fonts)
845 for (i = 0; i < 16; i++) {
846 GL_xlat[i] = (ubyte)Gr_gamma_lookup[(i*255)/15];
849 GL_xlat[15] = GL_xlat[1];
851 // Flush any existing textures
852 opengl1_tcache_flush();
855 void gr_opengl1_release_texture(int handle)
857 for(int i=0; i<MAX_BITMAPS; i++ ) {
858 if (Textures[i].bitmap_id == handle) {
859 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
860 opengl1_free_texture( &Textures[i] );