1 /* $Id: texmerge.c,v 1.3 2002-09-04 22:47:25 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Routines to cache merged textures.
20 * Revision 1.1 1995/05/16 15:31:36 allender
23 * Revision 2.0 1995/02/27 11:31:08 john
24 * New version 2.0, which has no anonymous unions, builds with
25 * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
27 * Revision 1.28 1995/01/14 19:16:56 john
28 * First version of new bitmap paging code.
30 * Revision 1.27 1994/12/14 18:21:58 yuan
31 * *** empty log message ***
33 * Revision 1.26 1994/12/13 09:50:08 john
34 * Added Asserts to stop if wall looks like door.
36 * Revision 1.25 1994/12/07 00:35:24 mike
37 * change how flat shading average color is computed for paste-ons.
39 * Revision 1.24 1994/11/19 15:20:29 mike
40 * rip out unused code and data
42 * Revision 1.23 1994/11/12 16:38:51 mike
43 * deal with avg_color in texture merging.
45 * Revision 1.22 1994/11/09 19:55:39 john
46 * Added full rle support with texture rle caching.
48 * Revision 1.21 1994/10/20 15:21:16 john
49 * Took out the texmerge caching.
51 * Revision 1.20 1994/10/10 19:00:57 john
52 * Made caching info print every 1000 frames.
54 * Revision 1.19 1994/10/10 18:41:21 john
55 * Printed out texture caching info.
57 * Revision 1.18 1994/08/11 18:59:02 mike
58 * Use new assembler version of merge functions.
60 * Revision 1.17 1994/06/09 12:13:14 john
61 * Changed selectors so that all bitmaps have a selector of
62 * 0, but inside the texture mapper they get a selector set.
64 * Revision 1.16 1994/05/14 17:15:15 matt
65 * Got rid of externs in source (non-header) files
67 * Revision 1.15 1994/05/09 17:21:09 john
68 * Took out mprintf with cache hits/misses.
70 * Revision 1.14 1994/05/05 12:55:07 john
71 * Made SuperTransparency work.
73 * Revision 1.13 1994/05/04 11:15:37 john
74 * Added Super Transparency
76 * Revision 1.12 1994/04/28 23:36:04 john
77 * Took out a debugging mprintf.
79 * Revision 1.11 1994/04/22 17:44:48 john
80 * Made top 2 bits of paste-ons pick the
81 * orientation of the bitmap.
83 * Revision 1.10 1994/03/31 12:05:51 matt
86 * Revision 1.9 1994/03/15 16:31:52 yuan
87 * Cleaned up bm-loading code.
90 * Revision 1.8 1994/01/24 13:15:19 john
91 * Made caching work with pointers, not texture numbers,
92 * that way, the animated textures cache.
94 * Revision 1.7 1994/01/21 16:38:10 john
95 * Took out debug info.
97 * Revision 1.6 1994/01/21 16:28:43 john
98 * added warning to print cache hit/miss.
100 * Revision 1.5 1994/01/21 16:22:30 john
103 * Revision 1.4 1994/01/21 15:34:49 john
104 * *** empty log message ***
106 * Revision 1.3 1994/01/21 15:33:08 john
107 * *** empty log message ***
109 * Revision 1.2 1994/01/21 15:15:35 john
110 * Created new module texmerge, that merges textures together and
111 * caches the results.
113 * Revision 1.1 1994/01/21 14:55:29 john
125 #include "pa_enabl.h" //$$POLY_ACC
129 #include "textures.h"
134 #if defined(POLY_ACC)
135 #include "poly_acc.h"
139 #include "ogl_init.h"
140 #define MAX_NUM_CACHE_BITMAPS 200
142 #define MAX_NUM_CACHE_BITMAPS 50
145 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
149 grs_bitmap * bottom_bmp;
150 grs_bitmap * top_bmp;
155 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
157 static int num_cache_entries = 0;
159 static int cache_hits = 0;
160 static int cache_misses = 0;
162 void texmerge_close();
163 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
165 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
168 #if defined(POLY_ACC) // useful to all of D2 I think.
169 extern grs_bitmap * rle_get_id_sub( grs_bitmap * bmp );
171 //----------------------------------------------------------------------
172 // Given pointer to a bitmap returns a unique value that describes the bitmap.
173 // Returns 0xFFFFFFFF if this bitmap isn't a texmerge'd bitmap.
174 uint texmerge_get_unique_id( grs_bitmap * bmp )
179 // Check in texmerge cache
180 for (i=0; i<num_cache_entries; i++ ) {
181 if ( (Cache[i].last_frame_used > -1) && (Cache[i].bitmap == bmp) ) {
182 tmp = (uint)Cache[i].orient<<30;
183 tmp |= ((uint)(Cache[i].top_bmp - GameBitmaps))<<16;
184 tmp |= (uint)(Cache[i].bottom_bmp - GameBitmaps);
188 // Check in rle cache
189 tmpbmp = rle_get_id_sub( bmp );
191 return (uint)(tmpbmp-GameBitmaps);
193 // Must be a normal bitmap
194 return (uint)(bmp-GameBitmaps);
198 //----------------------------------------------------------------------
200 int texmerge_init(int num_cached_textures)
204 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
205 num_cache_entries = num_cached_textures;
207 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
209 for (i=0; i<num_cache_entries; i++ ) {
210 // Make temp tmap for use when combining
211 Cache[i].bitmap = gr_create_bitmap( 64, 64 );
213 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
214 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
216 Cache[i].last_frame_used = -1;
217 Cache[i].top_bmp = NULL;
218 Cache[i].bottom_bmp = NULL;
219 Cache[i].orient = -1;
221 atexit( texmerge_close );
226 void texmerge_flush()
230 for (i=0; i<num_cache_entries; i++ ) {
231 Cache[i].last_frame_used = -1;
232 Cache[i].top_bmp = NULL;
233 Cache[i].bottom_bmp = NULL;
234 Cache[i].orient = -1;
239 //-------------------------------------------------------------------------
240 void texmerge_close()
244 for (i=0; i<num_cache_entries; i++ ) {
245 gr_free_bitmap( Cache[i].bitmap );
246 Cache[i].bitmap = NULL;
250 //--unused-- int info_printed = 0;
252 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
254 grs_bitmap *bitmap_top, *bitmap_bottom;
256 int lowest_frame_count;
257 int least_recently_used;
259 // if ( ((FrameCount % 1000)==0) && ((cache_hits+cache_misses)>0) && (!info_printed) ) {
260 // mprintf( 0, "Texmap caching: %d hits, %d misses. (Missed=%d%%)\n", cache_hits, cache_misses, (cache_misses*100)/(cache_hits+cache_misses) );
266 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
267 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
269 orient = ((tmap_top&0xC000)>>14) & 3;
271 least_recently_used = 0;
272 lowest_frame_count = Cache[0].last_frame_used;
274 for (i=0; i<num_cache_entries; i++ ) {
275 if ( (Cache[i].last_frame_used > -1) && (Cache[i].top_bmp==bitmap_top) && (Cache[i].bottom_bmp==bitmap_bottom) && (Cache[i].orient==orient )) {
277 Cache[i].last_frame_used = FrameCount;
278 return Cache[i].bitmap;
280 if ( Cache[i].last_frame_used < lowest_frame_count ) {
281 lowest_frame_count = Cache[i].last_frame_used;
282 least_recently_used = i;
286 //---- Page out the LRU bitmap;
289 // Make sure the bitmaps are paged in...
290 #ifdef PIGGY_USE_PAGING
291 piggy_page_flushed = 0;
293 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
294 PIGGY_PAGE_IN(Textures[tmap_bottom]);
295 if (piggy_page_flushed) {
296 // If cache got flushed, re-read 'em.
297 piggy_page_flushed = 0;
298 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
299 PIGGY_PAGE_IN(Textures[tmap_bottom]);
301 Assert( piggy_page_flushed == 0 );
305 ogl_freebmtexture(Cache[least_recently_used].bitmap);
309 if (bitmap_top->bm_flags & BM_FLAG_SUPER_TRANSPARENT) {
310 merge_textures_super_xparent( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
311 Cache[least_recently_used].bitmap->bm_flags = BM_FLAG_TRANSPARENT;
312 Cache[least_recently_used].bitmap->avg_color = bitmap_top->avg_color;
314 merge_textures_new( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
315 Cache[least_recently_used].bitmap->bm_flags = bitmap_bottom->bm_flags & (~BM_FLAG_RLE);
316 Cache[least_recently_used].bitmap->avg_color = bitmap_bottom->avg_color;
319 Cache[least_recently_used].top_bmp = bitmap_top;
320 Cache[least_recently_used].bottom_bmp = bitmap_bottom;
321 Cache[least_recently_used].last_frame_used = FrameCount;
322 Cache[least_recently_used].orient = orient;
324 return Cache[least_recently_used].bitmap;
327 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
329 ubyte * top_data, *bottom_data;
331 if ( top_bmp->bm_flags & BM_FLAG_RLE )
332 top_bmp = rle_expand_texture(top_bmp);
334 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
335 bottom_bmp = rle_expand_texture(bottom_bmp);
337 // Assert( bottom_bmp != top_bmp );
339 top_data = top_bmp->bm_data;
340 bottom_data = bottom_bmp->bm_data;
342 // Assert( bottom_data != top_data );
344 // mprintf( 0, "Type=%d\n", type );
350 gr_merge_textures( bottom_data, top_data, dest_data );
353 gr_merge_textures_1( bottom_data, top_data, dest_data );
356 gr_merge_textures_2( bottom_data, top_data, dest_data );
359 gr_merge_textures_3( bottom_data, top_data, dest_data );
364 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
369 ubyte * top_data, *bottom_data;
371 if ( top_bmp->bm_flags & BM_FLAG_RLE )
372 top_bmp = rle_expand_texture(top_bmp);
374 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
375 bottom_bmp = rle_expand_texture(bottom_bmp);
377 // Assert( bottom_bmp != top_bmp );
379 top_data = top_bmp->bm_data;
380 bottom_data = bottom_bmp->bm_data;
382 // Assert( bottom_data != top_data );
384 //mprintf( 0, "SuperX remapping type=%d\n", type );
390 for (y=0; y<64; y++ )
391 for (x=0; x<64; x++ ) {
392 c = top_data[ 64*y+x ];
393 if (c==TRANSPARENCY_COLOR)
394 c = bottom_data[ 64*y+x ];
396 c = TRANSPARENCY_COLOR;
402 for (y=0; y<64; y++ )
403 for (x=0; x<64; x++ ) {
404 c = top_data[ 64*x+(63-y) ];
405 if (c==TRANSPARENCY_COLOR)
406 c = bottom_data[ 64*y+x ];
408 c = TRANSPARENCY_COLOR;
414 for (y=0; y<64; y++ )
415 for (x=0; x<64; x++ ) {
416 c = top_data[ 64*(63-y)+(63-x) ];
417 if (c==TRANSPARENCY_COLOR)
418 c = bottom_data[ 64*y+x ];
420 c = TRANSPARENCY_COLOR;
426 for (y=0; y<64; y++ )
427 for (x=0; x<64; x++ ) {
428 c = top_data[ 64*(63-x)+y ];
429 if (c==TRANSPARENCY_COLOR)
430 c = bottom_data[ 64*y+x ];
432 c = TRANSPARENCY_COLOR;