1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     tex-tc-alloc.c
4 
5   Copyright 1998-2006 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------*
14     tex-tc-alloc
15         texture cache region allocator test
16  *---------------------------------------------------------------------------*/
17 
18 
19 /*---------------------------------------------------------------------------*
20    Header files
21  *---------------------------------------------------------------------------*/
22 #include <demo.h>
23 #include <stdlib.h>
24 
25 /*---------------------------------------------------------------------------*
26    Macro definitions
27  *---------------------------------------------------------------------------*/
28 #define NUM_TEXTURES    16
29 #define MAX_REGIONS     8
30 #define NUM_TMEM_MODES  7
31 
32 #define Clamp(val,min,max) \
33     ((val) = (((val)<(min)) ? (min) : ((val)>(max)) ? (max) : (val)))
34 
35 /*---------------------------------------------------------------------------*
36    Structure definitions
37  *---------------------------------------------------------------------------*/
38 // for camera
39 typedef struct
40 {
41     Vec    location;
42     Vec    up;
43     Vec    target;
44     f32    left;
45     f32    top;
46     f32    znear;
47     f32    zfar;
48 } CameraConfig;
49 
50 typedef struct
51 {
52     CameraConfig  cfg;
53     Mtx           view;
54     Mtx44         proj;
55 } MyCameraObj;
56 
57 // coord generator
58 typedef struct
59 {
60     u32    currentColumn;
61     s32    count[MAX_REGIONS];
62 } MyCoordGenObj;
63 
64 // for texture cache management
65 typedef struct
66 {
67     u32            numRegions;
68     u32            current;
69     GXTexRegion    region[MAX_REGIONS];
70     u32            tag[MAX_REGIONS];
71 } MyTexCacheManageObj;
72 
73 // for entire scene control
74 typedef struct
75 {
76     MyCameraObj    cam;
77     s32            ypos;
78     s32            ymax;
79     u32            numQuads;
80     u32            tmemMode;
81     u32            rndSeed;
82     u16            scrWidth;
83     u16            scrHeight;
84     GXTexObj       texture[NUM_TEXTURES];
85 } MySceneCtrlObj;
86 
87 /*---------------------------------------------------------------------------*
88    Forward references
89  *---------------------------------------------------------------------------*/
90 void        main               ( void );
91 static void DrawInit           ( MySceneCtrlObj* sc );
92 static void DrawTick           ( MySceneCtrlObj* sc );
93 static void AnimTick           ( MySceneCtrlObj* sc );
94 static void DrawQuad           ( void );
95 static void SetCamera          ( MyCameraObj* cam );
96 
97 static void ResetCoordGen      ( void );
98 inline void SetCoordGenCol     ( u32 col );
99 static void GetMtxFromCoordGen ( Mtx mt );
100 static s32  GetCoordGenMax     ( void );
101 
102 static void ConfigureTmemMap   ( u32 mode );
103 static void TmemMapper32K      ( MyTexCacheManageObj* tcm );
104 static void TmemMapper128K     ( MyTexCacheManageObj* tcm );
105 static void TmemMapper512K     ( MyTexCacheManageObj* tcm );
106 static GXTexRegion*
107             TexRegionCallback0 ( const GXTexObj* texObj, GXTexMapID mapID );
108 static GXTexRegion*
109             TexRegionCallback1 ( const GXTexObj* texObj, GXTexMapID mapID );
110 static GXTexRegion*
111             TexRegionCallback2 ( const GXTexObj* texObj, GXTexMapID mapID );
112 static GXTexRegion*
113             TexRegionCallback3 ( const GXTexObj* texObj, GXTexMapID mapID );
114 
115 static void PrintIntro         ( void );
116 
117 
118 /*---------------------------------------------------------------------------*
119    Model Data
120  *---------------------------------------------------------------------------*/
121 static s8 QuadVertices[3*8] ATTRIBUTE_ALIGN(32) =
122 {
123     -32,  32,  32, // 0
124      32,  32,  32, // 1
125      32, -32,  32, // 2
126     -32, -32,  32, // 3
127 };
128 
129 static s8 QuadTexCoords[2*4] ATTRIBUTE_ALIGN(32) =
130 {
131       0,   0,
132       1,   0,
133       1,   1,
134       0,   1
135 };
136 
137 /*---------------------------------------------------------------------------*
138    Tmem mapper & allocator (tex region callback) table
139  *---------------------------------------------------------------------------*/
140 static void (* MyTmemMappers[NUM_TMEM_MODES])
141             ( MyTexCacheManageObj* tcm ) =
142 {
143     TmemMapper32K,
144     TmemMapper32K,
145     TmemMapper32K,
146     TmemMapper128K,
147     TmemMapper128K,
148     TmemMapper128K,
149     TmemMapper512K
150 };
151 
152 static GXTexRegion* (* MyTexRegionCallbacks[NUM_TMEM_MODES])
153                     ( const GXTexObj* texObj, GXTexMapID mapID ) =
154 {
155     TexRegionCallback1,
156     TexRegionCallback2,
157     TexRegionCallback3,
158     TexRegionCallback1,
159     TexRegionCallback2,
160     TexRegionCallback3,
161     TexRegionCallback0
162 };
163 
164 static char* TmemModeMsg[NUM_TMEM_MODES] =
165 {
166     "8x32K caches  / allocation algorithm 0",
167     "8x32K caches  / allocation algorithm 1",
168     "8x32K caches  / allocation algorithm 2",
169     "4x128K caches / allocation algorithm 0",
170     "4x128K caches / allocation algorithm 1",
171     "4x128K caches / allocation algorithm 2",
172     "1x512K cache  / every texture uses one cache"
173 };
174 
175 /*---------------------------------------------------------------------------*
176    Camera configuration
177  *---------------------------------------------------------------------------*/
178 static CameraConfig DefaultCamera =
179 {
180     { 0.0F, 0.0F, 500.0F }, // location
181     { 0.0F, 1.0F,   0.0F }, // up
182     { 0.0F, 0.0F,   0.0F }, // target
183     -320.0F, // left
184     240.0F,  // top
185     50.0F,   // near
186     2000.0F  // far
187 };
188 
189 /*---------------------------------------------------------------------------*
190    Global variables
191  *---------------------------------------------------------------------------*/
192 static MySceneCtrlObj       SceneCtrl;                // scene control parameters
193 
194 static TPLPalettePtr        MyTplObj = NULL; // texture palette
195 static MyTexCacheManageObj  TexCacheManager; // texture cache manager
196 static MyCoordGenObj        CoordGenerator;  // display coord generator
197 
198 /*---------------------------------------------------------------------------*
199    Application main loop
200  *---------------------------------------------------------------------------*/
main(void)201 void main ( void )
202 {
203     DEMOInit(NULL);  // Init the OS, game pad, graphics and video.
204 
205     DrawInit(&SceneCtrl); // Initialize vertex formats, array pointers
206                           // and default scene settings.
207 
208     PrintIntro();    // Print demo directions
209 
210     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
211     {
212 		DEMOBeforeRender();
213         DrawTick(&SceneCtrl);    // Draw the model.
214         DEMODoneRender();
215         DEMOPadRead();           // Read controller
216         AnimTick(&SceneCtrl);    // Do animation
217     }
218 
219     OSHalt("End of demo");
220 }
221 
222 /*---------------------------------------------------------------------------*
223    Functions
224  *---------------------------------------------------------------------------*/
225 /*---------------------------------------------------------------------------*
226     Name:           DrawInit
227 
228     Description:    Initializes the vertex attribute format and sets up
229                     the array pointer for the indexed data.
230                     This function also initializes scene control parameters.
231 
232     Arguments:      sc : pointer to the structure of scene control parameters
233 
234     Returns:        none
235  *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)236 static void DrawInit( MySceneCtrlObj* sc )
237 {
238     GXRenderModeObj*  rmode;
239     TPLDescriptorPtr  tdp;
240     GXBool            mipMapFlag;
241     u32               i;
242 
243     // Vertex Attribute (VTXFMT0 is used by DEMOPuts library)
244     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_POS, GX_POS_XYZ, GX_S8, 0);
245     GXSetVtxAttrFmt(GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST, GX_S8, 0);
246 
247     // Array pointers
248     GXSetArray(GX_VA_POS,  QuadVertices, 3 * sizeof(s8));
249     GXSetArray(GX_VA_TEX0, QuadTexCoords, 2 * sizeof(s8));
250 
251     // Load TPL file and initialize texture objects
252     TPLGetPalette(&MyTplObj, "gxTests/tex-07.tpl");
253     for ( i = 0 ; i < NUM_TEXTURES ; ++i )
254     {
255         tdp = TPLGet(MyTplObj, i);
256         mipMapFlag =
257             (GXBool)(( tdp->textureHeader->minLOD == tdp->textureHeader->maxLOD ) ?
258                      GX_FALSE : GX_TRUE);
259 
260         GXInitTexObj(
261             &sc->texture[i],
262             tdp->textureHeader->data,
263             tdp->textureHeader->width,
264             tdp->textureHeader->height,
265             (GXTexFmt)tdp->textureHeader->format,
266             tdp->textureHeader->wrapS, // s
267             tdp->textureHeader->wrapT, // t
268             mipMapFlag ); // Mipmap
269 
270         GXInitTexObjLOD(
271             &sc->texture[i],
272             tdp->textureHeader->minFilter,
273             tdp->textureHeader->magFilter,
274             tdp->textureHeader->minLOD,
275             tdp->textureHeader->maxLOD,
276             tdp->textureHeader->LODBias,
277             GX_FALSE,
278             tdp->textureHeader->edgeLODEnable,
279             GX_ANISO_1 );
280 
281         // UserData field is used as ID number.
282         GXInitTexObjUserData(&sc->texture[i], (void*)i);
283     }
284 
285     // Get Screen Information
286     rmode = DEMOGetRenderModeObj();
287     sc->scrWidth  = (u16)rmode->fbWidth;   // Screen Width
288     sc->scrHeight = (u16)rmode->efbHeight; // Screen Height
289 
290 
291     // Default scene control parameter settings
292 
293     // camera and position
294     sc->cam.cfg = DefaultCamera;
295     sc->ypos = 0;
296     sc->ymax = 0;
297 
298     // number of quads to be displayed
299     sc->numQuads = 500;
300 
301     // tmem configuration mode
302     sc->tmemMode = 0;
303 
304     // seed of random function
305     sc->rndSeed = 1;
306 }
307 
308 /*---------------------------------------------------------------------------*
309     Name:           DrawTick
310 
311     Description:    Draw the model by using given scene parameters
312 
313     Arguments:      sc : pointer to the structure of scene control parameters
314 
315     Returns:        none
316  *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)317 static void DrawTick( MySceneCtrlObj* sc )
318 {
319     Mtx  mt, mv;
320     u32  ti;
321     s32  i;
322 
323     // configure tmem map
324     ConfigureTmemMap(sc->tmemMode);
325 
326     // set camera position
327     SetCamera(&sc->cam);
328 
329     // reset coord generator
330     ResetCoordGen();
331 
332     // set up Tev, TexGen and Zmode
333     GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
334     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL);
335     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
336     GXSetZMode(GX_ENABLE, GX_LEQUAL, GX_ENABLE);
337 
338     // set seed of random number
339     srand(sc->rndSeed);
340 
341     for ( i = 0 ; i < sc->numQuads ; ++i )
342     {
343         // Make random tex number for each face
344         ti = (u32)rand() % NUM_TEXTURES;
345 
346         // Load texture
347         GXLoadTexObj(&sc->texture[ti], GX_TEXMAP0);
348 
349         // Set modelview matrix
350         GetMtxFromCoordGen(mt);
351         MTXConcat(sc->cam.view, mt, mv);
352         GXLoadPosMtxImm(mv, GX_PNMTX0);
353 
354         // Draw a textured Quad
355         DrawQuad();
356     }
357     // Keep max Y value of current coord generator
358     sc->ymax = GetCoordGenMax() * 48 - 400;
359 
360 
361     // Draw Captions
362 
363     // DEMOPuts lib uses a texture which ID is not defined.
364     // So set the Callback which never uses ID information.
365     GXSetTexRegionCallback(TexRegionCallback0);
366 
367     DEMOInitCaption(DM_FT_OPQ, sc->scrWidth, sc->scrHeight);
368     DEMOPrintf(56, (s16)(sc->scrHeight - 40), 0, TmemModeMsg[sc->tmemMode]);
369     for ( i = 0 ; i < TexCacheManager.numRegions ; ++i )
370     {
371         DEMOPrintf((s16)(i*68+48), 32, 0, "[CACHE%d]", i);
372     }
373 }
374 
375 /*---------------------------------------------------------------------------*
376     Name:           AnimTick
377 
378     Description:    Changes scene parameters according to the pad status.
379 
380     Arguments:      sc : pointer to the structure of scene control parameters
381 
382     Returns:        none
383  *---------------------------------------------------------------------------*/
AnimTick(MySceneCtrlObj * sc)384 static void AnimTick( MySceneCtrlObj* sc )
385 {
386     static u32 frameCounter = 0;
387     u16 down;
388 
389     down = DEMOPadGetButtonDown(0);
390 
391     // change TMEM map & allocator mode
392     if ( down & PAD_TRIGGER_L )
393     {
394         sc->tmemMode += NUM_TMEM_MODES - 1;
395         sc->tmemMode = sc->tmemMode % NUM_TMEM_MODES;
396         sc->ypos = 0;
397     }
398     if ( down & PAD_TRIGGER_R )
399     {
400         sc->tmemMode += 1;
401         sc->tmemMode = sc->tmemMode % NUM_TMEM_MODES;
402         sc->ypos = 0;
403     }
404 
405     // change number of quads to be displayed
406     if ( down & PAD_BUTTON_Y )
407     {
408         if ( sc->numQuads > 100 )
409         {
410             sc->numQuads -= 100;
411             OSReport("Quads : %d\n", sc->numQuads);
412         }
413     }
414     if ( down & PAD_BUTTON_X )
415     {
416         if ( sc->numQuads < 1000 )
417         {
418             sc->numQuads += 100;
419             OSReport("Quads : %d\n", sc->numQuads);
420         }
421     }
422 
423     // change seed of random value generation
424     if ( down & PAD_BUTTON_B )
425     {
426         sc->rndSeed = sc->rndSeed * sc->rndSeed + frameCounter;
427         sc->ypos = 0;
428     }
429 
430     // move camera position
431     sc->ypos -= DEMOPadGetStickY(0) / 2;
432     Clamp(sc->ypos, 0, sc->ymax);
433     sc->cam.cfg.location.y = (f32)(- sc->ypos);
434     sc->cam.cfg.target.y   = (f32)(- sc->ypos);
435 
436     // count up frames
437     ++frameCounter;
438 }
439 
440 /*---------------------------------------------------------------------------*
441     Name:           DrawQuad
442 
443     Description:    Draw a textured Quad. Each face can have different texture
444                     which is specified by given texture number list.
445 
446     Arguments:      texNoList : a pointer to texture number list
447 
448     Returns:        none
449  *---------------------------------------------------------------------------*/
DrawQuad(void)450 static void DrawQuad( void )
451 {
452     u8   i;
453 
454     // set vertex descriptor
455     GXClearVtxDesc();
456     GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
457     GXSetVtxDesc(GX_VA_TEX0, GX_INDEX8);
458 
459     // draw the quad
460     GXBegin(GX_QUADS, GX_VTXFMT1, 4);
461         for ( i = 0 ; i < 4 ; ++i )
462         {
463             GXPosition1x8(i);
464             GXTexCoord1x8(i);
465         }
466     GXEnd();
467 }
468 
469 /*---------------------------------------------------------------------------*
470     Name:           SetCamera
471 
472     Description:    Set view matrix and load projection matrix into hardware
473 
474     Arguments:      cam : pointer to the MyCameraObj structure
475 
476     Returns:        none
477  *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)478 static void SetCamera( MyCameraObj* cam )
479 {
480     MTXLookAt(
481         cam->view,
482         &cam->cfg.location,
483         &cam->cfg.up,
484         &cam->cfg.target );
485 
486     MTXOrtho(
487         cam->proj,
488         cam->cfg.top,
489         - (cam->cfg.top),
490         cam->cfg.left,
491         - (cam->cfg.left),
492         cam->cfg.znear,
493         cam->cfg.zfar );
494     GXSetProjection(cam->proj, GX_ORTHOGRAPHIC);
495 }
496 
497 /*---------------------------------------------------------------------------*
498     Name:           ResetCoordGen
499 
500     Description:    Reset coordinate generator which is used to decide
501                     position for each quads dynamically.
502 
503     Arguments:      none
504 
505     Returns:        none
506  *---------------------------------------------------------------------------*/
ResetCoordGen(void)507 static void ResetCoordGen( void )
508 {
509     u32  i;
510 
511     CoordGenerator.currentColumn = 0;
512     for ( i = 0 ; i < MAX_REGIONS ; ++i )
513     {
514         CoordGenerator.count[i] = 0;
515     }
516 }
517 
518 /*---------------------------------------------------------------------------*
519     Name:           SetCoordGenCol
520 
521     Description:    Set current column for generating coordinates
522 
523     Arguments:      col : column number
524 
525     Returns:        none
526  *---------------------------------------------------------------------------*/
SetCoordGenCol(u32 col)527 inline void SetCoordGenCol( u32 col )
528 {
529     CoordGenerator.currentColumn = col;
530 }
531 
532 /*---------------------------------------------------------------------------*
533     Name:           GetMtxFromCoordGen
534 
535     Description:    Get model transfer matrix by using current state
536                     of the coordinate generator
537 
538     Arguments:      mt : a matrix the result should be put
539 
540     Returns:        none
541  *---------------------------------------------------------------------------*/
GetMtxFromCoordGen(Mtx mt)542 static void GetMtxFromCoordGen( Mtx mt )
543 {
544     u32  col;
545     s32  cnt;
546 
547     col = CoordGenerator.currentColumn;
548     cnt = CoordGenerator.count[col]++;
549 
550     // model transfer matrix
551     MTXTrans( mt,
552               (f32)col * 68.0F - 240.0F,
553               (f32)cnt * -48.0F + 160.0F,
554               0.0F );
555 }
556 
557 /*---------------------------------------------------------------------------*
558     Name:           GetCoordGenMax
559 
560     Descriptions:   Get the maximum count number of all columns
561                     at current state of the coordinate generator
562 
563     Arguments:      none
564 
565     Returns:        the maximum number
566  *---------------------------------------------------------------------------*/
GetCoordGenMax(void)567 static s32 GetCoordGenMax( void )
568 {
569     u32  i;
570     s32  max = 0;
571 
572     for ( i = 0 ; i < MAX_REGIONS ; ++i )
573     {
574         if ( CoordGenerator.count[i] > max )
575         {
576             max = CoordGenerator.count[i];
577         }
578     }
579 
580     return max;
581 }
582 
583 /*---------------------------------------------------------------------------*
584     Name:           ConfigureTmemMap
585 
586     Description:    Initialize TMEM configuration.
587                     Set up cache regions.
588 
589     Arguments:      mode : tmem map & cache allocation mode
590 
591     Returns:        none
592  *---------------------------------------------------------------------------*/
ConfigureTmemMap(u32 mode)593 static void ConfigureTmemMap( u32 mode )
594 {
595     u32  i;
596 
597     // Synchronization for new TMEM cache configuration
598     GXTexModeSync();
599 
600     // Call tmem mapper for specified mode.
601     (*MyTmemMappers[mode])(&TexCacheManager);
602 
603     // Tex region Callback for specified mode.
604     GXSetTexRegionCallback(MyTexRegionCallbacks[mode]);
605 
606     // Initialize cache manager
607     TexCacheManager.current = 0;
608     for ( i = 0 ; i < MAX_REGIONS ; ++i )
609     {
610         TexCacheManager.tag[i]   = 0;
611     }
612 
613     // Invalidate all new caches
614     GXInvalidateTexAll();
615 }
616 
617 /*---------------------------------------------------------------------------*
618     Name:           TmemMapper32K
619 
620     Description:    Set up 8x32K texture cache regions
621 
622     Arguments:      tcm : a pointer to the cache manager
623 
624     Returns:        none
625  *---------------------------------------------------------------------------*/
TmemMapper32K(MyTexCacheManageObj * tcm)626 static void TmemMapper32K( MyTexCacheManageObj* tcm )
627 {
628     u32  i;
629 
630     tcm->numRegions = MAX_REGIONS;
631 
632     for ( i = 0 ; i < tcm->numRegions ; ++i )
633     {
634         // The region is used as a 32K cache.
635         GXInitTexCacheRegion(
636             &tcm->region[i],
637             GX_FALSE,               // 32b mipmap
638             0x00000 + i * 0x08000,  // tmem_even
639             GX_TEXCACHE_32K,        // size_even
640             0x80000 + i * 0x08000,  // tmem_odd
641             GX_TEXCACHE_32K );      // size_odd
642     }
643 }
644 
645 /*---------------------------------------------------------------------------*
646     Name:           TmemMapper128K
647 
648     Description:    Set up 4x128K texture cache regions
649 
650     Arguments:      tcm : a pointer to the cache manager
651 
652     Returns:        none
653  *---------------------------------------------------------------------------*/
TmemMapper128K(MyTexCacheManageObj * tcm)654 static void TmemMapper128K( MyTexCacheManageObj* tcm )
655 {
656     u32  i;
657 
658     tcm->numRegions = 4;
659 
660     for ( i = 0 ; i < tcm->numRegions ; ++i )
661     {
662         // The region is used as a 128K cache.
663         GXInitTexCacheRegion(
664             &tcm->region[i],
665             GX_FALSE,               // 32b mipmap
666             0x00000 + i * 0x20000,  // tmem_even
667             GX_TEXCACHE_128K,       // size_even
668             0x80000 + i * 0x20000,  // tmem_odd
669             GX_TEXCACHE_128K );     // size_odd
670     }
671 }
672 
673 /*---------------------------------------------------------------------------*
674     Name:           TmemMapper512K
675 
676     Description:    Set up 1x512K texture cache regions
677 
678     Arguments:      tcm : a pointer to the cache manager
679 
680     Returns:        none
681  *---------------------------------------------------------------------------*/
TmemMapper512K(MyTexCacheManageObj * tcm)682 static void TmemMapper512K( MyTexCacheManageObj* tcm )
683 {
684     tcm->numRegions = 1;
685 
686     // The region is used as a 512K cache.
687     GXInitTexCacheRegion(
688         &tcm->region[0],
689         GX_FALSE,           // 32b mipmap
690         0x00000,            // tmem_even
691         GX_TEXCACHE_512K,   // size_even
692         0x80000,            // tmem_odd
693         GX_TEXCACHE_512K ); // size_odd
694 }
695 
696 /*---------------------------------------------------------------------------*
697     Name:           TexRegionCallback0
698 
699     Description:    Tex cache allocator which always returns region 0
700 
701     Arguments:      texObj : a pointer to texture object to be loaded
702                     mapID  : destination texmap ID (just same as GXLoadTexObj)
703 
704     Returns:        appropriate tex cache region for loading texture.
705  *---------------------------------------------------------------------------*/
TexRegionCallback0(const GXTexObj * texObj,GXTexMapID mapID)706 static GXTexRegion* TexRegionCallback0(const GXTexObj* texObj, GXTexMapID mapID)
707 {
708     #pragma unused(texObj)
709     #pragma unused(mapID)
710 
711     SetCoordGenCol(0);
712 
713     return &TexCacheManager.region[0];
714 }
715 
716 /*---------------------------------------------------------------------------*
717     Name:           TexRegionCallback1
718 
719     Description:    Tex cache allocator using simple round algorithm
720 
721     Arguments:      texObj : a pointer to texture object to be loaded
722                     mapID  : destination texmap ID (just same as GXLoadTexObj)
723 
724     Returns:        appropriate tex cache region for loading texture.
725  *---------------------------------------------------------------------------*/
TexRegionCallback1(const GXTexObj * texObj,GXTexMapID mapID)726 static GXTexRegion* TexRegionCallback1(const GXTexObj* texObj, GXTexMapID mapID)
727 {
728     #pragma unused(texObj)
729     #pragma unused(mapID)
730 
731     u32  regionNum;
732 
733     regionNum = TexCacheManager.current++;
734     TexCacheManager.current %= TexCacheManager.numRegions;
735 
736     SetCoordGenCol(regionNum);
737 
738     return &TexCacheManager.region[regionNum];
739 }
740 
741 /*---------------------------------------------------------------------------*
742     Name:           TexRegionCallback2
743 
744     Description:    Tex cache allocator using texture ID
745 
746     Arguments:      texObj : a pointer to texture object to be loaded
747                     mapID  : destination texmap ID (just same as GXLoadTexObj)
748 
749     Returns:        appropriate tex cache region for loading texture.
750  *---------------------------------------------------------------------------*/
TexRegionCallback2(const GXTexObj * texObj,GXTexMapID mapID)751 static GXTexRegion* TexRegionCallback2(const GXTexObj* texObj, GXTexMapID mapID)
752 {
753     #pragma unused(mapID)
754 
755     u32  texID, regionNum;
756 
757     texID     = (u32)GXGetTexObjUserData(texObj);
758     regionNum = texID % TexCacheManager.numRegions;
759 
760     SetCoordGenCol(regionNum);
761 
762     return &TexCacheManager.region[regionNum];
763 }
764 
765 /*---------------------------------------------------------------------------*
766     Name:           TexRegionCallback3
767 
768     Description:    Check whether ID of given texture is used in any
769                     cache region. If it hits, uses the region.
770 
771     Arguments:      texObj : a pointer to texture object to be loaded
772                     mapID  : destination texmap ID (just same as GXLoadTexObj)
773 
774     Returns:        appropriate tex cache region for loading texture.
775  *---------------------------------------------------------------------------*/
TexRegionCallback3(const GXTexObj * texObj,GXTexMapID mapID)776 static GXTexRegion* TexRegionCallback3(const GXTexObj* texObj, GXTexMapID mapID)
777 {
778     #pragma unused(mapID)
779 
780     u32  i, n;
781     u32  texID, regionNum;
782 
783     texID = (u32)GXGetTexObjUserData(texObj);
784     n = TexCacheManager.numRegions;
785     // Search given ID from tags
786     for ( i = 0 ; i < n ; ++i )
787     {
788         if ( TexCacheManager.tag[i] == texID )
789         {
790             regionNum = i;
791             break;
792         }
793     }
794 
795     if ( i == n )
796     {
797         regionNum = TexCacheManager.current++;
798         TexCacheManager.current %= TexCacheManager.numRegions;
799     }
800 
801     TexCacheManager.tag[regionNum] = texID;
802     SetCoordGenCol(regionNum);
803 
804     return &TexCacheManager.region[regionNum];
805 }
806 
807 /*---------------------------------------------------------------------------*
808     Name:           PrintIntro
809 
810     Description:    Prints the directions on how to use this demo.
811 
812     Arguments:      none
813 
814     Returns:        none
815  *---------------------------------------------------------------------------*/
PrintIntro(void)816 static void PrintIntro( void )
817 {
818     OSReport("\n\n");
819     OSReport("************************************************\n");
820     OSReport("tex-tc-alloc: tex cache region allocator test\n");
821     OSReport("************************************************\n");
822     OSReport("to quit hit the start button\n");
823     OSReport("\n");
824     OSReport("Main Stick   : Scroll the scene\n");
825     OSReport("L/R Triggers : Change TMEM map & allocation algorithm\n");
826     OSReport("X/Y Buttons  : Change number of quads to be drawn.\n");
827     OSReport("B Button     : Change seed of random function.\n");
828     OSReport("************************************************\n\n");
829 }
830 
831 /*============================================================================*/
832