2 * $Logfile: /Freespace2/code/Graphics/GrD3DTexture.cpp $
7 * Code to manage loading textures into VRAM for Direct3D
10 * Revision 1.1 2002/05/03 03:28:09 root
14 * 22 9/09/99 8:53p Dave
15 * Fixed multiplayer degenerate orientation case problem. Make sure warp
16 * effect never goes lower than LOD 1.
18 * 21 9/05/99 11:19p Dave
19 * Made d3d texture cache much more safe. Fixed training scoring bug where
20 * it would backout scores without ever having applied them in the first
23 * 20 8/16/99 4:04p Dave
24 * Big honking checkin.
26 * 19 7/29/99 10:47p Dave
27 * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
29 * 18 7/16/99 1:49p Dave
30 * 8 bit aabitmaps. yay.
32 * 17 7/13/99 1:15p Dave
33 * 32 bit support. Whee!
35 * 16 7/09/99 9:51a Dave
36 * Added thick polyline code.
38 * 15 6/29/99 10:35a Dave
39 * Interface polygon bitmaps! Whee!
41 * 14 6/16/99 4:06p Dave
42 * New pilot info popup. Added new draw-bitmap-as-poly function.
44 * 13 5/05/99 9:02p Dave
45 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
46 * rotations, tweaked values, made bitmap selection more random). Fixed
47 * D3D beam weapon clipping problem. Added D3d frame dumping.
49 * 12 2/11/99 3:08p Dave
50 * PXO refresh button. Very preliminary squad war support.
52 * 11 2/05/99 12:52p Dave
53 * Fixed Glide nondarkening textures.
55 * 10 2/03/99 11:44a Dave
56 * Fixed d3d transparent textures.
58 * 9 1/30/99 5:08p Dave
59 * More new hi-res stuff.Support for nice D3D textures.
61 * 8 1/15/99 11:29a Neilk
62 * Fixed D3D screen/texture pixel formatting problem.
64 * 7 1/12/99 12:53a Dave
65 * More work on beam weapons - made collision detection very efficient -
66 * collide against all object types properly - made 3 movement types
67 * smooth. Put in test code to check for possible non-darkening pixels on
70 * 6 12/01/98 6:12p Johnson
71 * Make sure to page in weapon impact animations as xparent textures.
73 * 5 12/01/98 10:32a Johnson
74 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
77 * 4 11/30/98 1:07p Dave
78 * 16 bit conversion, first run.
80 * 3 10/09/98 2:57p Dave
81 * Starting splitting up OS stuff.
83 * 2 10/07/98 10:52a Dave
86 * 1 10/07/98 10:49a Dave
88 * 37 6/13/98 3:18p Hoffoss
89 * NOX()ed out a bunch of strings that shouldn't be translated.
91 * 36 5/23/98 6:12p John
92 * added code to use registry to force preloading of textures or not.
94 * 35 5/23/98 5:17p John
95 * added reg key to set texture divider
97 * 34 5/23/98 5:01p John
98 * made agp preloading happen if >= 6 MB of VRAM.
100 * 33 5/23/98 4:14p John
101 * Added code to preload textures to video card for AGP. Added in code
102 * to page in some bitmaps that weren't getting paged in at level start.
104 * 32 5/22/98 10:29p John
105 * made direct3d textures scale as little as glide.
107 * 31 5/22/98 12:54p John
108 * forced all cards to use a max of 256 pixel wide textures, but added a
109 * registry setting to disable it.
111 * 30 5/21/98 9:56p John
112 * Made Direct3D work with classic alpha-blending only devices, like the
113 * Virge. Added a texture type XPARENT that fills the alpha in in the
114 * bitmap for Virge. Added support for Permedia by making making
115 * additive alphablending be one/one instead of alpha/one, which didn't
116 * work, and there is no way to tell this from caps.
118 * 29 5/20/98 10:23a John
119 * put in code to fix an optimized build problem.
121 * 28 5/18/98 8:26p John
122 * Made cards with only 1bpp alpha fonts work.
124 * 27 5/12/98 7:53p John
125 * Fixed some 3dfx d3d bugs on allenders, jasen and johnson's computers
126 * caused by 8:3:3:2 format being used, but not liked by the card.
128 * 26 5/12/98 8:18a John
129 * Put in code to use a different texture format for alpha textures and
130 * normal textures. Turned off filtering for aabitmaps. Took out
131 * destblend=invsrccolor alpha mode that doesn't work on riva128.
133 * 25 5/11/98 10:58a John
134 * Fixed pilot name cursor bug. Started adding in code for alphachannel
137 * 24 5/09/98 12:37p John
138 * More texture caching
140 * 23 5/09/98 12:16p John
141 * Even better texture caching.
143 * 22 5/09/98 11:07a John
144 * Better Direct3D texture caching
146 * 21 5/08/98 5:41p John
148 * 20 5/08/98 5:36p John
149 * MAde texturing blink white but not crash
151 * 19 5/07/98 3:02p John
152 * Mpre texture cleanup. You can now reinit d3d without a crash.
154 * 18 5/07/98 11:31a John
155 * Removed DEMO defines
157 * 17 5/07/98 10:28a John
158 * Made texture format use 4444. Made fonts use alpha to render.
160 * 16 5/07/98 9:40a John
161 * Fixed some bitmap transparency issues with Direct3D.
163 * 15 5/06/98 11:21p John
164 * Fixed a bitmap bug with Direct3D. Started adding new caching code into
167 * 14 5/06/98 8:41p John
168 * Fixed some font clipping bugs. Moved texture handle set code for d3d
169 * into the texture module.
171 * 13 5/03/98 10:52a John
172 * Made D3D sort of work on 3dfx.
174 * 12 5/03/98 10:43a John
175 * Working on Direct3D.
177 * 11 4/09/98 11:05a John
178 * Removed all traces of Direct3D out of the demo version of Freespace and
181 * 10 3/12/98 5:36p John
182 * Took out any unused shaders. Made shader code take rgbc instead of
183 * matrix and vector since noone used it like a matrix and it would have
184 * been impossible to do in hardware. Made Glide implement a basic
185 * shader for online help.
187 * 9 3/08/98 10:25a John
188 * Made textures in VRAM reload if they changed
190 * 8 3/07/98 8:29p John
191 * Put in some Direct3D features. Transparency on bitmaps. Made fonts &
192 * aabitmaps render nice.
194 * 7 3/06/98 5:39p John
195 * Started adding in aabitmaps
197 * 6 3/02/98 6:00p John
198 * Moved MAX_BITMAPS into BmpMan.h so the stuff in the graphics code that
199 * is dependent on it won't break if it changes. Made ModelCache slots
200 * be equal to MAX_OBJECTS which is what it is.
202 * 5 2/17/98 7:46p John
203 * Took out debug code
205 * 4 2/17/98 7:28p John
206 * Got fonts and texturing working in Direct3D
208 * 3 2/06/98 4:56p John
209 * Turned off texturing
211 * 2 2/05/98 9:21p John
212 * Some new Direct3D code. Added code to monitor a ton of stuff in the
215 * 1 2/03/98 9:24p John
221 #include "grd3dinternal.h"
226 #include "systemvars.h"
227 #include "osregistry.h"
229 #include "multi_log.h"
231 typedef struct tcache_slot_d3d {
232 LPDIRECTDRAWSURFACE vram_texture_surface;
233 LPDIRECT3DTEXTURE2 vram_texture;
234 D3DTEXTUREHANDLE texture_handle;
235 float u_scale, v_scale;
238 char used_this_frame;
243 tcache_slot_d3d *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
244 tcache_slot_d3d *parent;
247 static void *Texture_sections = NULL;
248 tcache_slot_d3d *Textures = NULL;
251 int D3D_texture_sections = 0;
252 int D3D_texture_ram = 0;
253 int D3D_frame_count = 0;
254 int D3D_min_texture_width = 0;
255 int D3D_max_texture_width = 0;
256 int D3D_min_texture_height = 0;
257 int D3D_max_texture_height = 0;
258 int D3D_square_textures = 0;
259 int D3D_pow2_textures = 0;
260 int D3D_textures_in = 0;
261 int D3D_textures_in_frame = 0;
262 int D3D_last_bitmap_id = -1;
263 int D3D_last_detail = -1;
264 int D3D_last_bitmap_type = -1;
265 int D3D_last_section_x = -1;
266 int D3D_last_section_y = -1;
271 int d3d_free_texture( tcache_slot_d3d *t )
276 if ( t->bitmap_id > -1 ) {
277 // if I, or any of my children have been used this frame, bail
278 if(t->used_this_frame){
281 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
282 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
283 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
289 // ok, now we know its legal to free everything safely
290 if ( t->vram_texture ) {
291 t->vram_texture->Release();
292 t->vram_texture = NULL;
295 if ( t->vram_texture_surface ) {
296 t->vram_texture_surface->Release();
297 t->vram_texture_surface = NULL;
300 t->texture_handle = NULL;
302 if ( D3D_last_bitmap_id == t->bitmap_id ) {
303 D3D_last_bitmap_id = -1;
306 // if this guy has children, free them too, since the children
307 // actually make up his size
308 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
309 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
310 if(t->data_sections[idx][s_idx] != NULL){
311 d3d_free_texture(t->data_sections[idx][s_idx]);
317 t->used_this_frame = 0;
318 D3D_textures_in -= t->size;
324 // we must make sure we never free my parent or any of my siblings!!!!!
325 int d3d_older_test(tcache_slot_d3d *new_slot, tcache_slot_d3d *test, tcache_slot_d3d *oldest)
327 if ( (test != new_slot) && (test != new_slot->parent) && (test->bitmap_id > -1) && (!test->used_this_frame)) {
328 if ( (oldest == NULL) || (test->time_created < oldest->time_created)) {
337 int d3d_free_some_texture_ram(tcache_slot_d3d *t, int size)
339 tcache_slot_d3d *oldest = NULL;
341 // Go through all the textures... find the oldest one
342 // that was not used this frame yet.
345 int goal_size = D3D_textures_in - size*2;
346 if ( goal_size < 0 ) {
348 } else if ( goal_size > D3D_texture_ram*3/4 ) {
349 goal_size = D3D_texture_ram*3/4;
352 while( D3D_textures_in > goal_size ) {
354 for( i=0; i<MAX_BITMAPS; i++ ) {
355 // maybe pick this one
356 if(d3d_older_test(t, &Textures[i], oldest)){
357 oldest = &Textures[i];
361 if ( oldest == NULL ) {
362 mprintf(( "Couldn't free enough VRAM this frame... you might see some ugliness!\n" ));
366 d3d_free_texture(oldest);
369 mprintf(( "Freed 1/4 of the VRAM\n" ));
374 int Show_uploads = 0;
375 DCF_BOOL( show_uploads, Show_uploads )
378 // is the given existing texture handle the same dimensions as the passed in bitmap?
379 int d3d_tcache_same_dimension(int bitmap_id, int bitmap_type, tcache_slot_d3d *t)
382 int w, h, nframes, fps;
384 bitmap_section_info *s = NULL;
387 bm_get_info(bitmap_id, &w, &h, &flags, &nframes, &fps, &s);
395 if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
396 for(idx=0; idx<s->num_x; idx++){
397 for(s_idx=0; s_idx<s->num_y; s_idx++){
399 if(t->data_sections[idx][s_idx] == NULL){
403 // given a bitmap and a section, return the size (w, h)
404 bm_get_section_size(bitmap_id, idx, s_idx, &w, &h);
407 if((t->data_sections[idx][s_idx]->w != w) || (t->data_sections[idx][s_idx]->h != h)){
413 // non-sectioned bitmap
415 if((t->w != w) || (t->h != h)){
424 // get the final texture size (the one which will get allocated as a surface)
425 void d3d_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
430 if((w_out == NULL) || (h_out == NULL)){
438 if ( D3D_pow2_textures ) {
440 for (i=0; i<16; i++ ) {
441 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
447 for (i=0; i<16; i++ ) {
448 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
455 if ( tex_w < D3D_min_texture_width ) {
456 tex_w = D3D_min_texture_width;
457 } else if ( tex_w > D3D_max_texture_width ) {
458 tex_w = D3D_max_texture_width;
461 if ( tex_h < D3D_min_texture_height ) {
462 tex_h = D3D_min_texture_height;
463 } else if ( tex_h > D3D_max_texture_height ) {
464 tex_h = D3D_max_texture_height;
467 if ( D3D_square_textures ) {
469 // Make the both be equal to larger of the two
470 new_size = max(tex_w, tex_h);
475 // store the outgoing size
480 // data == start of bitmap data
481 // sx == x offset into bitmap
482 // sy == y offset into bitmap
483 // src_w == absolute width of section on source bitmap
484 // src_h == absolute height of section on source bitmap
485 // bmap_w == width of source bitmap
486 // bmap_h == height of source bitmap
487 // tex_w == width of final texture
488 // tex_h == height of final texture
489 int d3d_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_d3d *t, int reload, int fail_on_full)
491 LPDIRECTDRAWSURFACE sys_texture_surface = NULL;
492 LPDIRECT3DTEXTURE2 sys_texture = NULL;
496 if ( Show_uploads ) {
498 mprintf(( "Reloading '%s'\n", bm_get_filename(texture_handle) ));
500 mprintf(( "Uploading '%s'\n", bm_get_filename(texture_handle) ));
510 if ( t->used_this_frame ) {
511 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
516 if(!d3d_free_texture(t)){
523 DWORD dwHeight, dwWidth;
527 DDPIXELFORMAT *surface_desc;
529 switch( bitmap_type ) {
530 case TCACHE_TYPE_AABITMAP:
531 surface_desc = &AlphaTextureFormat;
534 case TCACHE_TYPE_XPARENT:
538 surface_desc = &NonAlphaTextureFormat;
541 // get final texture size
542 d3d_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
544 if ( (tex_w < 1) || (tex_h < 1) ) {
545 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
549 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
550 t->u_scale = (float)bmap_w / (float)tex_w;
551 t->v_scale = (float)bmap_h / (float)tex_h;
552 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
553 t->u_scale = (float)src_w / (float)tex_w;
554 t->v_scale = (float)src_h / (float)tex_h;
562 bmp_data = (ushort *)data;
563 ubyte *bmp_data_byte = (ubyte*)data;
565 // Create a surface in system memory and load texture into it.
567 // Create a surface of the given format using the dimensions of the bitmap
568 memset(&ddsd, 0, sizeof(DDSURFACEDESC));
570 ddsd.dwSize = sizeof(DDSURFACEDESC);
571 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
572 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
573 ddsd.dwHeight = dwHeight;
574 ddsd.dwWidth = dwWidth;
575 ddsd.ddpfPixelFormat = *surface_desc;
577 sys_texture_surface = NULL;
578 ddrval = lpDD->CreateSurface(&ddsd, &sys_texture_surface, NULL);
579 if ( (ddrval != DD_OK) || (sys_texture_surface == NULL) ) {
580 mprintf(("CreateSurface for texture failed (loadtex), w=%d, h=%d, %s\n", tex_w, tex_h, d3d_error_string(ddrval) ));
581 mprintf(( "Texture RAM = %d KB\n", D3D_textures_in / 1024 ));
582 // bm_unlock(bitmap_handle);
586 // Lock the surface so it can be filled with the bitmap data
587 memset(&ddsd, 0, sizeof(DDSURFACEDESC));
588 ddsd.dwSize = sizeof(DDSURFACEDESC);
589 ddrval = sys_texture_surface->Lock(NULL, &ddsd, 0, NULL);
590 if (ddrval != DD_OK) {
591 sys_texture_surface->Release();
592 mprintf(("Lock failed while loading surface (loadtex).\n" ));
596 Assert( surface_desc->dwRGBBitCount == 16 );
598 // Each RGB bit count requires different pointers
603 switch( bitmap_type ) {
604 case TCACHE_TYPE_AABITMAP:
605 // setup convenient translation table
606 for (i=0; i<16; i++ ) {
610 a = Gr_gamma_lookup[(i*255)/15];
611 r /= Gr_ta_red.scale;
612 g /= Gr_ta_green.scale;
613 b /= Gr_ta_blue.scale;
614 a /= Gr_ta_alpha.scale;
615 xlat[i] = unsigned short(((a<<Gr_ta_alpha.shift) | (r << Gr_ta_red.shift) | (g << Gr_ta_green.shift) | (b << Gr_ta_blue.shift)));
619 for ( ; i<256; i++ ) {
623 for (j = 0; j < tex_h; j++) {
624 lpSP = (unsigned short*)(((char*)ddsd.lpSurface) + ddsd.lPitch * j);
626 for (i = 0; i < tex_w; i++) {
627 if ( (i < bmap_w) && (j<bmap_h) ) {
628 *lpSP++ = xlat[(ubyte)bmp_data_byte[j*bmap_w+i]];
636 case TCACHE_TYPE_BITMAP_SECTION:
637 for (j = 0; j < src_h; j++) {
638 // the proper line in the temp ram
639 lpSP = (unsigned short*)(((char*)ddsd.lpSurface) + ddsd.lPitch * j);
642 for (i = 0; i < src_w; i++) {
643 // stuff the texture into vram
644 *lpSP++ = bmp_data[((j+sy) * bmap_w) + sx + i];
649 default: { // normal:
650 fix u, utmp, v, du, dv;
654 du = ( (bmap_w-1)*F1_0 ) / tex_w;
655 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
657 for (j = 0; j < tex_h; j++) {
658 lpSP = (unsigned short*)(((char*)ddsd.lpSurface) + ddsd.lPitch * j);
662 for (i = 0; i < tex_w; i++) {
663 *lpSP++ = bmp_data[f2i(v)*bmap_w+f2i(utmp)];
672 // bm_unlock(bitmap_handle);
674 // Unlock the texture
675 sys_texture_surface->Unlock(NULL);
678 ddrval = sys_texture_surface->QueryInterface(IID_IDirect3DTexture2, (LPVOID *)&sys_texture);
679 if ( (ddrval != DD_OK) || (sys_texture == NULL) ) {
680 mprintf(( "Getting sys surface's texture failed!\n" ));
685 goto FreeSurfacesAndExit;
691 // Create a surface of the given format using the dimensions of the bitmap
692 memset(&ddsd, 0, sizeof(DDSURFACEDESC));
694 ddsd.dwSize = sizeof(DDSURFACEDESC);
695 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
696 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD | DDSCAPS_VIDEOMEMORY;
697 //| DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_ALLOCONLOAD;
698 ddsd.dwHeight = dwHeight;
699 ddsd.dwWidth = dwWidth;
700 ddsd.ddpfPixelFormat = *surface_desc;
702 t->vram_texture_surface = NULL;
703 ddrval = lpDD->CreateSurface(&ddsd, &t->vram_texture_surface, NULL);
704 if ( (ddrval != DD_OK) || (t->vram_texture_surface == NULL) ) {
705 t->vram_texture = NULL;
706 t->vram_texture_surface = NULL;
707 t->texture_handle = NULL;
709 if ( ddrval==DDERR_OUTOFVIDEOMEMORY ) {
710 mprintf(("Out of VRAM (w=%d, h=%d, used=%d KB)\n", tex_w, tex_h, D3D_textures_in / 1024 ));
711 if ( fail_on_full ) {
715 goto FreeSurfacesAndExit;
717 if ( d3d_free_some_texture_ram(t,dwHeight*dwWidth*2)) {
721 mprintf(("CreateSurface for VRAM texture failed, w=%d, h=%d\n%s\n", tex_w, tex_h, d3d_error_string(ddrval) ));
722 mprintf(( "Texture RAM = %d KB\n", D3D_textures_in / 1024 ));
729 goto FreeSurfacesAndExit;
733 t->vram_texture = NULL;
734 ddrval = t->vram_texture_surface->QueryInterface(IID_IDirect3DTexture2, (LPVOID *)&t->vram_texture);
735 if ( (ddrval != DD_OK) || (t->vram_texture == NULL) ) {
736 mprintf(( "GR_D3D_INIT: TextureSurface->QueryInterface failed.\n" ));
742 goto FreeSurfacesAndExit;
745 // char *name = bm_get_filename(bitmap_handle);
746 // mprintf(( "Uploading '%s'\n", name ));
747 t->texture_handle = NULL;
748 ddrval = t->vram_texture->GetHandle(lpD3DDevice, &t->texture_handle );
749 if ( (ddrval != DD_OK) || (t->texture_handle == NULL) ) {
750 mprintf(( "GR_D3D_INIT: Texture->GetHandle failed.\n" ));
751 t->texture_handle = NULL;
757 goto FreeSurfacesAndExit;
761 // argh. this texture appears to be bogus. lets free it
762 if(t->vram_texture == NULL){
768 goto FreeSurfacesAndExit;
771 ddrval = t->vram_texture->Load( sys_texture );
772 if ( ddrval != DD_OK ) {
773 mprintf(("VRAM Load failed, w=%d, h=%d, %s\n", tex_w, tex_h, d3d_error_string(ddrval) ));
774 mprintf(( "Texture RAM = %d KB\n", D3D_textures_in / 1024 ));
780 goto FreeSurfacesAndExit;
783 t->bitmap_id = texture_handle;
784 t->time_created = D3D_frame_count;
785 t->used_this_frame = 0;
786 t->size = tex_w * tex_h * 2;
787 t->w = (ushort)tex_w;
788 t->h = (ushort)tex_h;
789 D3D_textures_in_frame += t->size;
791 D3D_textures_in += t->size;
796 sys_texture->Release();
800 if ( sys_texture_surface ) {
801 sys_texture_surface->Release();
802 sys_texture_surface = NULL;
805 // hopefully this is 1 :)
809 int d3d_create_texture(int bitmap_handle, int bitmap_type, tcache_slot_d3d *tslot, int fail_on_full)
813 int final_w, final_h;
817 // setup texture/bitmap flags
820 case TCACHE_TYPE_AABITMAP:
821 flags |= BMP_AABITMAP;
824 case TCACHE_TYPE_NORMAL:
825 flags |= BMP_TEX_OTHER;
826 case TCACHE_TYPE_XPARENT:
827 flags |= BMP_TEX_XPARENT;
829 case TCACHE_TYPE_NONDARKENING:
831 flags |= BMP_TEX_NONDARK;
835 // lock the bitmap into the proper format
836 bmp = bm_lock(bitmap_handle, bpp, flags);
838 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
845 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
846 max_w /= D3D_texture_divider;
847 max_h /= D3D_texture_divider;
849 // Detail.debris_culling goes from 0 to 4.
850 max_w /= 16 >> Detail.hardware_textures;
851 max_h /= 16 >> Detail.hardware_textures;
854 // get final texture size as it will be allocated as a DD surface
855 d3d_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
857 // if this tcache slot has no bitmap
858 if ( tslot->bitmap_id < 0) {
861 // different bitmap altogether - determine if the new one can use the old one's slot
862 else if (tslot->bitmap_id != bitmap_handle) {
863 if((final_w == tslot->w) && (final_h == tslot->h)){
866 ml_printf("Reloading texture %d\n", bitmap_handle);
873 int ret_val = d3d_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, max_w, max_h, tslot, reload, fail_on_full);
876 bm_unlock(bitmap_handle);
881 int d3d_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_d3d *tslot, int sx, int sy, int fail_on_full)
885 int final_w, final_h;
886 int section_x, section_y;
889 // setup texture/bitmap flags
890 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
891 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
892 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
894 flags = BMP_TEX_XPARENT;
896 // lock the bitmap in the proper format
897 bmp = bm_lock(bitmap_handle, 16, flags);
899 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
903 // determine the width and height of this section
904 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
906 // get final texture size as it will be allocated as a DD surface
907 d3d_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
909 // if this tcache slot has no bitmap
910 if ( tslot->bitmap_id < 0) {
913 // different bitmap altogether - determine if the new one can use the old one's slot
914 else if (tslot->bitmap_id != bitmap_handle) {
915 if((final_w == tslot->w) && (final_h == tslot->h)){
923 int ret_val = d3d_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, section_x, section_y, tslot, reload, fail_on_full);
926 bm_unlock(bitmap_handle);
931 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
932 int d3d_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full, int sx, int sy, int force )
938 if ( bitmap_id < 0 ) {
939 D3D_last_bitmap_id = -1;
943 if ( D3D_last_detail != Detail.hardware_textures ) {
944 D3D_last_detail = Detail.hardware_textures;
948 //mprintf(( "Setting texture %d\n", bitmap_handle ));
953 int n = bm_get_cache_slot( bitmap_id, 1 );
954 tcache_slot_d3d * t = &Textures[n];
956 if ( (D3D_last_bitmap_id == bitmap_id) && (D3D_last_bitmap_type==bitmap_type) && (t->bitmap_id == bitmap_id) && (D3D_last_section_x == sx) && (D3D_last_section_y == sy)) {
957 t->used_this_frame++;
959 // mark all children as used
960 if(D3D_texture_sections){
961 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
962 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
963 if(t->data_sections[idx][s_idx] != NULL){
964 t->data_sections[idx][s_idx]->used_this_frame++;
970 *u_scale = t->u_scale;
971 *v_scale = t->v_scale;
975 // if this is a sectioned bitmap
976 if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
977 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
978 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
984 // if the texture sections haven't been created yet
985 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
986 // lock the bitmap in the proper format
987 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
988 bm_unlock(bitmap_id);
990 // now lets do something for each texture
991 for(idx=0; idx<bmp->sections.num_x; idx++){
992 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
993 // hmm. i'd rather we didn't have to do it this way...
994 if(!d3d_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
998 // not used this frame
999 t->data_sections[idx][s_idx]->used_this_frame = 0;
1003 // zero out pretty much everything in the parent struct since he's just the root
1004 t->bitmap_id = bitmap_id;
1005 t->texture_handle = NULL;
1006 t->time_created = t->data_sections[sx][sy]->time_created;
1007 t->used_this_frame = 0;
1008 t->vram_texture = NULL;
1009 t->vram_texture_surface = NULL;
1012 // argh. we failed to upload. free anything we can
1014 d3d_free_texture(t);
1016 // swap in the texture we want
1018 t = t->data_sections[sx][sy];
1021 // all other "normal" textures
1022 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
1023 ret_val = d3d_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
1026 // everything went ok
1027 if(ret_val && (t->texture_handle != NULL) && !vram_full){
1028 *u_scale = t->u_scale;
1029 *v_scale = t->v_scale;
1031 d3d_SetRenderState(D3DRENDERSTATE_TEXTUREHANDLE, t->texture_handle );
1033 D3D_last_bitmap_id = t->bitmap_id;
1034 D3D_last_bitmap_type = bitmap_type;
1035 D3D_last_section_x = sx;
1036 D3D_last_section_y = sy;
1038 t->used_this_frame++;
1048 int D3D_should_preload = 0;
1050 void d3d_tcache_init(int use_sections)
1054 D3D_should_preload = 0;
1058 DWORD dwFree, dwTotal;
1060 memset(&ddsCaps,0,sizeof(ddsCaps) );
1061 ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1062 HRESULT ddrval = lpDD->GetAvailableVidMem(&ddsCaps, &dwTotal, &dwFree);
1063 if ( ddrval != DD_OK ) {
1064 mprintf(( "GR_D3D_INIT: GetAvailableVidMem failed.\n" ));
1069 D3D_texture_ram = dwFree;
1071 uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1073 if ( tmp_pl == 0 ) {
1074 D3D_should_preload = 0;
1075 } else if ( tmp_pl == 1 ) {
1076 D3D_should_preload = 1;
1078 if ( D3D_texture_ram >= 6*1024*1024 ) {
1079 D3D_should_preload = 1;
1081 D3D_should_preload = 0;
1086 int megs = dwFree / (1024*1024);
1087 uint tmp_val = os_config_read_uint( NULL, NOX("D3DTextureDivider"), 255 );
1089 if ( tmp_val == 0 ) {
1092 D3D_texture_divider = 4;
1093 } else if ( megs <=8 ) {
1094 D3D_texture_divider = 3;
1095 } else if ( megs <=16 ) {
1096 D3D_texture_divider = 2;
1098 D3D_texture_divider = 1;
1100 } else if ( tmp_val < 5 ) {
1101 D3D_texture_divider = tmp_val;
1104 D3D_texture_divider = 4;
1108 // setup texture divider
1109 uint tmp_val = os_config_read_uint( NULL, NOX("D3DFast"), 1 );
1110 D3D_texture_divider = 1;
1112 D3D_texture_divider = 4;
1116 int megs = dwFree / (1024*1024);
1117 mprintf(( "TEXTURE RAM: %d bytes (%d MB) available, size divisor = %d\n", dwFree, megs, D3D_texture_divider ));
1122 D3D_min_texture_width = (int)lpDevDesc->dwMinTextureWidth;
1123 D3D_max_texture_width = (int)lpDevDesc->dwMaxTextureWidth;
1124 D3D_min_texture_height = (int)lpDevDesc->dwMinTextureHeight;
1125 D3D_max_texture_height = (int)lpDevDesc->dwMaxTextureHeight;
1127 // The following checks are needed because the PowerVR reports
1128 // it's texture caps as 0,0 up to 0,0.
1129 if ( D3D_min_texture_width < 16 ) {
1130 D3D_min_texture_width = 16;
1132 if ( D3D_min_texture_height < 16 ) {
1133 D3D_min_texture_height = 16;
1135 if ( D3D_max_texture_width < 16 ) {
1136 mprintf(( "Driver claims to have a max texture width of %d. Bashing to 256.\n(Is this a PowerVR?)\n", D3D_max_texture_width ));
1137 D3D_max_texture_width = 256; // Can we assume 256?
1140 if ( D3D_max_texture_height < 16 ) {
1141 mprintf(( "Driver claims to have a max texture height of %d. Bashing to 256.\n(Is this a PowerVR?)\n", D3D_max_texture_height ));
1142 D3D_max_texture_height = 256; // Can we assume 256?
1145 if (lpDevDesc->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY) {
1146 mprintf(( "Darn. Driver needs square textures.\n" ));
1147 D3D_square_textures = 1;
1149 mprintf(( "Woohoo! Driver doesn't need square textures!\n" ));
1150 D3D_square_textures = 0;
1153 if ( lpDevDesc->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2 ) {
1154 mprintf(( "Textures must be power of 2.\n" ));
1155 D3D_pow2_textures = 1;
1157 mprintf(( "Textures can be any size.\n" ));
1158 D3D_pow2_textures = 0;
1162 if ( !(DD_driver_caps.dwCaps2 & DDCAPS2_WIDESURFACES) ) {
1163 if ( D3D_max_texture_width > gr_screen.max_w ) {
1164 D3D_max_texture_width = gr_screen.max_w;
1166 if ( D3D_pow2_textures ) {
1167 for (i=0; i<16; i++ ) {
1168 if ( (D3D_max_texture_width>= (1<<i)) && (D3D_max_texture_width < (1<<(i+1))) ) {
1169 D3D_max_texture_width = 1 << i;
1175 if ( D3D_max_texture_height > D3D_max_texture_width ) {
1176 D3D_max_texture_height = D3D_max_texture_width;
1179 mprintf(( "Doesn't support wide surfaces. Bashing max down to %d\n", D3D_max_texture_width ));
1183 if ( !os_config_read_uint( NULL, NOX("D3DUseLargeTextures"), 0 )) {
1184 mprintf(( "No large texture flag specified, so using max of 256\n" ));
1185 if ( D3D_max_texture_width > 256 ) {
1186 D3D_max_texture_width = 256;
1189 if ( D3D_max_texture_height > 256 ) {
1190 D3D_max_texture_height = 256;
1193 mprintf(( "Large textures enabled!\n" ));
1196 Textures = (tcache_slot_d3d *)malloc(MAX_BITMAPS*sizeof(tcache_slot_d3d));
1202 Texture_sections = (tcache_slot_d3d*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_d3d));
1203 if(!Texture_sections){
1206 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_d3d));
1209 // Init the texture structures
1210 int section_count = 0;
1211 for( i=0; i<MAX_BITMAPS; i++ ) {
1212 Textures[i].vram_texture = NULL;
1213 Textures[i].vram_texture_surface = NULL;
1214 Textures[i].texture_handle = NULL;
1216 Textures[i].bitmap_id = -1;
1217 Textures[i].size = 0;
1218 Textures[i].used_this_frame = 0;
1220 Textures[i].parent = NULL;
1222 // allocate sections
1224 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1225 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1226 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_d3d*)Texture_sections)[section_count++];
1227 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1229 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1230 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1231 Textures[i].data_sections[idx][s_idx]->texture_handle = NULL;
1233 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1234 Textures[i].data_sections[idx][s_idx]->size = 0;
1235 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1239 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1240 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1241 Textures[i].data_sections[idx][s_idx] = NULL;
1247 D3D_texture_sections = use_sections;
1249 D3D_last_detail = Detail.hardware_textures;
1250 D3D_last_bitmap_id = -1;
1251 D3D_last_bitmap_type = -1;
1253 D3D_last_section_x = -1;
1254 D3D_last_section_y = -1;
1256 D3D_textures_in = 0;
1257 D3D_textures_in_frame = 0;
1261 void d3d_tcache_flush()
1266 for( i=0; i<MAX_BITMAPS; i++ ) {
1267 d3d_free_texture( &Textures[i] );
1269 if ( D3D_textures_in != 0 ) {
1270 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", D3D_textures_in ));
1271 D3D_textures_in = 0;
1274 D3D_last_bitmap_id = -1;
1275 D3D_last_section_x = -1;
1276 D3D_last_section_y = -1;
1279 void d3d_tcache_cleanup()
1283 D3D_textures_in = 0;
1284 D3D_textures_in_frame = 0;
1291 if(Texture_sections != NULL){
1292 free(Texture_sections);
1293 Texture_sections = NULL;
1297 void d3d_tcache_frame()
1301 D3D_last_bitmap_id = -1;
1302 D3D_textures_in_frame = 0;
1307 for( i=0; i<MAX_BITMAPS; i++ ) {
1308 Textures[i].used_this_frame = 0;
1311 if(Textures[i].data_sections[0][0] != NULL){
1312 Assert(D3D_texture_sections);
1313 if(D3D_texture_sections){
1314 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1315 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1316 if(Textures[i].data_sections[idx][s_idx] != NULL){
1317 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1332 void gr_d3d_preload_init()
1334 if ( gr_screen.mode != GR_DIRECT3D ) {
1340 int gr_d3d_preload(int bitmap_num, int is_aabitmap)
1342 if ( gr_screen.mode != GR_DIRECT3D ) {
1346 if ( !D3D_should_preload ) {
1350 float u_scale, v_scale;
1354 if ( is_aabitmap ) {
1355 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
1357 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
1361 mprintf(("Texture upload failed!\n" ));
1367 // call this to safely fill in the texture shift and scale values for the specified texture type (Gr_t_*)
1368 void gr_d3d_get_tex_format(int alpha)
1371 DDPIXELFORMAT *surface_desc;
1376 // get the proper texture format
1378 surface_desc = &AlphaTextureFormat;
1380 surface_desc = &NonAlphaTextureFormat;
1383 // Determine the red, green and blue masks' shift and scale.
1384 for (s = 0, m = surface_desc->dwRBitMask; !(m & 1); s++, m >>= 1);
1386 Gr_t_red_scale = 255 / (surface_desc->dwRBitMask >> s);
1387 for (s = 0, m = surface_desc->dwGBitMask; !(m & 1); s++, m >>= 1);
1388 Gr_t_green_shift = s;
1389 Gr_t_green_scale = 255 / (surface_desc->dwGBitMask >> s);
1390 for (s = 0, m = surface_desc->dwBBitMask; !(m & 1); s++, m >>= 1);
1391 Gr_t_blue_shift = s;
1392 Gr_t_blue_scale = 255 / (surface_desc->dwBBitMask >> s);
1394 if ( surface_desc->dwFlags & DDPF_ALPHAPIXELS ) {
1395 for (s = 0, m = surface_desc->dwRGBAlphaBitMask; !(m & 1); s++, m >>= 1);
1396 Gr_t_alpha_shift = s;
1397 Gr_t_alpha_scale = 255 / (surface_desc->dwRGBAlphaBitMask >> s);
1399 Gr_t_alpha_shift = 0;
1400 Gr_t_alpha_scale = 256;