Skip to content

Commit 7310c4e

Browse files
committed
arm9/videoGL: minor refactor to make texture VRAM management optional
1 parent c7c1a61 commit 7310c4e

3 files changed

Lines changed: 156 additions & 133 deletions

File tree

include/nds/arm9/videoGL.h

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ much about it. This is only an issue because of hte mix of inlined/real function
338338
---------------------------------------------------------------------------------*/
339339

340340
typedef struct gl_hidden_globals {
341-
GL_MATRIX_MODE_ENUM matrixMode; // holds the current Matrix Mode
341+
u8 isActive; // Has this been called before?
342+
342343
s_vramBlock *vramBlocks[ 2 ]; // Two classe instances, one for textures, and one for palettes
343344
int vramLock[ 2 ]; // Holds the current lock state of the VRAM banks
344345

@@ -355,19 +356,15 @@ typedef struct gl_hidden_globals {
355356
int activePalette; // The current active palette name
356357
int texCount;
357358
int palCount;
358-
359-
// holds the current state of the clear color register
360-
u32 clearColor; // state of clear color register
361-
362-
u8 isActive; // Has this been called before?
363359
} gl_hidden_globals;
364360

361+
extern u32 glCurClearColor;
365362

366363
// Pointer to global data for videoGL
367364
extern gl_hidden_globals glGlobalData;
368365

369366
// Pointer to global data for videoGL
370-
static gl_hidden_globals* glGlob = &glGlobalData;
367+
#define glGlob (&glGlobalData)
371368

372369
//---------------------------------------------------------------------------------
373370
//Fifo commands
@@ -413,6 +410,9 @@ static gl_hidden_globals* glGlob = &glGlobalData;
413410
extern "C" {
414411
#endif
415412

413+
/*! \brief Initializes the gl state machine (must be called once before using gl calls) */
414+
void glInit(void);
415+
416416
/*! \brief Rotates the model view matrix by angle about the specified unit vector
417417
\param angle The angle to rotate by
418418
\param x X component of the unit vector axis.
@@ -522,13 +522,6 @@ void glTexCoord2f32(s32 u, s32 v);
522522
\param color the color to set for that material property */
523523
void glMaterialf(GL_MATERIALS_ENUM mode, rgb color);
524524

525-
// This handles initialization of the GL state; this is called from glInit to keep globals synced between compilation units
526-
void glInit_C(void);
527-
528-
// This returns a pointer to the globals for videoGL
529-
gl_hidden_globals* glGetGlobals();
530-
531-
532525
#ifdef __cplusplus
533526
}
534527
#endif
@@ -1299,13 +1292,6 @@ void glCutoffDepth(fixed12d3 wVal) {
12991292
GFX_CUTOFF_DEPTH = wVal;
13001293
}
13011294

1302-
GL_STATIC_INL
1303-
/*! \fn void glInit()
1304-
\brief Initializes the gl state machine (must be called once before using gl calls) */
1305-
void glInit() {
1306-
glInit_C(); // actually does the initialization
1307-
}
1308-
13091295
GL_STATIC_INL
13101296
/*! \fn void glClearColor(u8 red, u8 green, u8 blue, u8 alpha)
13111297
\brief sets the color of the rear-plane(a.k.a Clear Color/Plane)
@@ -1314,15 +1300,15 @@ GL_STATIC_INL
13141300
\param blue component (0-31)
13151301
\param alpha from 0(clear) to 31(opaque)*/
13161302
void glClearColor(u8 red, u8 green, u8 blue, u8 alpha) {
1317-
GFX_CLEAR_COLOR = glGlob->clearColor = ( glGlob->clearColor & 0xFFE08000) | (0x7FFF & RGB15(red, green, blue)) | ((alpha & 0x1F) << 16);
1303+
GFX_CLEAR_COLOR = glCurClearColor = ( glCurClearColor & 0xFFE08000) | (0x7FFF & RGB15(red, green, blue)) | ((alpha & 0x1F) << 16);
13181304
}
13191305

13201306
GL_STATIC_INL
13211307
/*! \fn void glClearPolyID(u8 ID)
13221308
\brief sets the polygon ID of the rear-plane(a.k.a. Clear/Color Plane), useful for antialiasing and edge coloring
13231309
\param ID the polygon ID to give the rear-plane */
13241310
void glClearPolyID(u8 ID) {
1325-
GFX_CLEAR_COLOR = glGlob->clearColor = ( glGlob->clearColor & 0xC0FFFFFF) | (( ID & 0x3F ) << 24 );
1311+
GFX_CLEAR_COLOR = glCurClearColor = ( glCurClearColor & 0xC0FFFFFF) | (( ID & 0x3F ) << 24 );
13261312
}
13271313

13281314
GL_STATIC_INL

source/arm9/videoGL.c

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -38,76 +38,8 @@
3838
#include <nds/arm9/sassert.h>
3939

4040
// this is the actual data of the globals for videoGL
41-
// Please use the glGlob pointer to access this data since that makes it easier to move stuff in/out of the header.
4241
gl_hidden_globals glGlobalData;
4342

44-
45-
46-
// This returns the pointer to the globals for videoGL
47-
gl_hidden_globals* glGetGlobals() {
48-
return &glGlobalData;
49-
}
50-
51-
//---------------------------------------------------------------------------------
52-
void glRotatef32i(int angle, s32 x, s32 y, s32 z) {
53-
//---------------------------------------------------------------------------------
54-
s32 axis[3];
55-
s32 sine = sinLerp(angle);//SIN[angle & LUT_MASK];
56-
s32 cosine = cosLerp(angle);//COS[angle & LUT_MASK];
57-
s32 one_minus_cosine = inttof32(1) - cosine;
58-
59-
axis[0]=x;
60-
axis[1]=y;
61-
axis[2]=z;
62-
63-
normalizef32(axis); // should require passed in normalized?
64-
65-
MATRIX_MULT3x3 = cosine + mulf32(one_minus_cosine, mulf32(axis[0], axis[0]));
66-
MATRIX_MULT3x3 = mulf32(one_minus_cosine, mulf32(axis[0], axis[1])) + mulf32(axis[2], sine);
67-
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) - mulf32(axis[1], sine);
68-
69-
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[1]) - mulf32(axis[2], sine);
70-
MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[1]), axis[1]);
71-
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) + mulf32(axis[0], sine);
72-
73-
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) + mulf32(axis[1], sine);
74-
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) - mulf32(axis[0], sine);
75-
MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[2]), axis[2]);
76-
}
77-
78-
79-
80-
81-
//---------------------------------------------------------------------------------
82-
void glMaterialf(GL_MATERIALS_ENUM mode, rgb color) {
83-
//---------------------------------------------------------------------------------
84-
static u32 diffuse_ambient = 0;
85-
static u32 specular_emission = 0;
86-
87-
switch(mode) {
88-
case GL_AMBIENT:
89-
diffuse_ambient = (color << 16) | (diffuse_ambient & 0xFFFF);
90-
break;
91-
case GL_DIFFUSE:
92-
diffuse_ambient = color | (diffuse_ambient & 0xFFFF0000);
93-
break;
94-
case GL_AMBIENT_AND_DIFFUSE:
95-
diffuse_ambient= color + (color << 16);
96-
break;
97-
case GL_SPECULAR:
98-
specular_emission = color | (specular_emission & 0xFFFF0000);
99-
break;
100-
case GL_SHININESS:
101-
break;
102-
case GL_EMISSION:
103-
specular_emission = (color << 16) | (specular_emission & 0xFFFF);
104-
break;
105-
}
106-
107-
GFX_DIFFUSE_AMBIENT = diffuse_ambient;
108-
GFX_SPECULAR_EMISSION = specular_emission;
109-
}
110-
11143
//---------------------------------------------------------------------------------
11244
void glTexCoord2f32(s32 u, s32 v) {
11345
//---------------------------------------------------------------------------------
@@ -520,17 +452,11 @@ vramBlock_getSize( s_vramBlock *mb, u32 index ) {
520452
//---------------------------------------------------------------------------------
521453
//---------------------------------------------------------------------------------
522454

523-
524-
525455
//---------------------------------------------------------------------------------
526-
void glInit_C(void) {
456+
void _glInitGlobals(void) {
527457
//---------------------------------------------------------------------------------
528458
int i;
529459

530-
powerOn(POWER_3D_CORE | POWER_MATRIX); // enable 3D core & geometry engine
531-
532-
glGlob = glGetGlobals();
533-
534460
if( glGlob->isActive )
535461
return;
536462

@@ -543,8 +469,6 @@ void glInit_C(void) {
543469

544470
// init texture globals
545471

546-
glGlob->clearColor = 0;
547-
548472
glGlob->activeTexture = 0;
549473
glGlob->activePalette = 0;
550474
glGlob->texCount = 1;
@@ -565,39 +489,6 @@ void glInit_C(void) {
565489
DynamicArraySet( &glGlob->deallocPal, i, (void*)0 );
566490
}
567491

568-
while (GFX_STATUS & (1<<27)); // wait till gfx engine is not busy
569-
570-
// Clear the FIFO
571-
GFX_STATUS |= (1<<29);
572-
573-
// Clear overflows from list memory
574-
glResetMatrixStack();
575-
576-
// prime the vertex/polygon buffers
577-
glFlush(0);
578-
579-
// reset the control bits
580-
GFX_CONTROL = 0;
581-
582-
// reset the rear-plane(a.k.a. clear color) to black, ID=0, and opaque
583-
glClearColor(0,0,0,31);
584-
glClearPolyID(0);
585-
586-
// reset the depth to it's max
587-
glClearDepth(GL_MAX_DEPTH);
588-
589-
GFX_TEX_FORMAT = 0;
590-
GFX_POLY_FORMAT = 0;
591-
592-
glMatrixMode(GL_PROJECTION);
593-
glLoadIdentity();
594-
595-
glMatrixMode(GL_MODELVIEW);
596-
glLoadIdentity();
597-
598-
glMatrixMode(GL_TEXTURE);
599-
glLoadIdentity();
600-
601492
glGlob->isActive = 1;
602493
}
603494

source/arm9/videoGL_base.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*---------------------------------------------------------------------------------
2+
3+
Video API vaguely similar to OpenGL
4+
5+
Copyright (C) 2005
6+
Michael Noland (joat)
7+
Jason Rogers (dovoto)
8+
Dave Murphy (WinterMute)
9+
10+
This software is provided 'as-is', without any express or implied
11+
warranty. In no event will the authors be held liable for any
12+
damages arising from the use of this software.
13+
14+
Permission is granted to anyone to use this software for any
15+
purpose, including commercial applications, and to alter it and
16+
redistribute it freely, subject to the following restrictions:
17+
18+
1. The origin of this software must not be misrepresented; you
19+
must not claim that you wrote the original software. If you use
20+
this software in a product, an acknowledgment in the product
21+
documentation would be appreciated but is not required.
22+
2. Altered source versions must be plainly marked as such, and
23+
must not be misrepresented as being the original software.
24+
3. This notice may not be removed or altered from any source
25+
distribution.
26+
27+
28+
---------------------------------------------------------------------------------*/
29+
30+
#include <nds/ndstypes.h>
31+
#include <nds/memory.h>
32+
#include <nds/bios.h>
33+
#include <nds/system.h>
34+
#include <nds/arm9/math.h>
35+
#include <nds/arm9/video.h>
36+
#include <nds/arm9/videoGL.h>
37+
#include <nds/arm9/trig_lut.h>
38+
#include <nds/arm9/sassert.h>
39+
40+
u32 glCurClearColor;
41+
42+
//---------------------------------------------------------------------------------
43+
void glRotatef32i(int angle, s32 x, s32 y, s32 z) {
44+
//---------------------------------------------------------------------------------
45+
s32 axis[3];
46+
s32 sine = sinLerp(angle);//SIN[angle & LUT_MASK];
47+
s32 cosine = cosLerp(angle);//COS[angle & LUT_MASK];
48+
s32 one_minus_cosine = inttof32(1) - cosine;
49+
50+
axis[0]=x;
51+
axis[1]=y;
52+
axis[2]=z;
53+
54+
normalizef32(axis); // should require passed in normalized?
55+
56+
MATRIX_MULT3x3 = cosine + mulf32(one_minus_cosine, mulf32(axis[0], axis[0]));
57+
MATRIX_MULT3x3 = mulf32(one_minus_cosine, mulf32(axis[0], axis[1])) + mulf32(axis[2], sine);
58+
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) - mulf32(axis[1], sine);
59+
60+
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[1]) - mulf32(axis[2], sine);
61+
MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[1]), axis[1]);
62+
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) + mulf32(axis[0], sine);
63+
64+
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) + mulf32(axis[1], sine);
65+
MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) - mulf32(axis[0], sine);
66+
MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[2]), axis[2]);
67+
}
68+
69+
//---------------------------------------------------------------------------------
70+
void glMaterialf(GL_MATERIALS_ENUM mode, rgb color) {
71+
//---------------------------------------------------------------------------------
72+
static u32 diffuse_ambient = 0;
73+
static u32 specular_emission = 0;
74+
75+
switch(mode) {
76+
case GL_AMBIENT:
77+
diffuse_ambient = (color << 16) | (diffuse_ambient & 0xFFFF);
78+
break;
79+
case GL_DIFFUSE:
80+
diffuse_ambient = color | (diffuse_ambient & 0xFFFF0000);
81+
break;
82+
case GL_AMBIENT_AND_DIFFUSE:
83+
diffuse_ambient= color + (color << 16);
84+
break;
85+
case GL_SPECULAR:
86+
specular_emission = color | (specular_emission & 0xFFFF0000);
87+
break;
88+
case GL_SHININESS:
89+
break;
90+
case GL_EMISSION:
91+
specular_emission = (color << 16) | (specular_emission & 0xFFFF);
92+
break;
93+
}
94+
95+
GFX_DIFFUSE_AMBIENT = diffuse_ambient;
96+
GFX_SPECULAR_EMISSION = specular_emission;
97+
}
98+
99+
//---------------------------------------------------------------------------------
100+
MK_WEAK void _glInitGlobals(void) {
101+
//---------------------------------------------------------------------------------
102+
// Dummy implementation when not using the texture VRAM manager
103+
}
104+
105+
//---------------------------------------------------------------------------------
106+
void glInit(void) {
107+
//---------------------------------------------------------------------------------
108+
powerOn(POWER_3D_CORE | POWER_MATRIX); // enable 3D core & geometry engine
109+
110+
// Initialize texture VRAM manager (if needed)
111+
_glInitGlobals();
112+
113+
while (GFX_STATUS & (1<<27)); // wait till gfx engine is not busy
114+
115+
// Clear the FIFO
116+
GFX_STATUS |= (1<<29);
117+
118+
// Clear overflows from list memory
119+
glResetMatrixStack();
120+
121+
// prime the vertex/polygon buffers
122+
glFlush(0);
123+
124+
// reset the control bits
125+
GFX_CONTROL = 0;
126+
127+
// reset the rear-plane(a.k.a. clear color) to black, ID=0, and opaque
128+
glCurClearColor = 0;
129+
glClearColor(0,0,0,31);
130+
glClearPolyID(0);
131+
132+
// reset the depth to it's max
133+
glClearDepth(GL_MAX_DEPTH);
134+
135+
GFX_TEX_FORMAT = 0;
136+
GFX_POLY_FORMAT = 0;
137+
138+
glMatrixMode(GL_PROJECTION);
139+
glLoadIdentity();
140+
141+
glMatrixMode(GL_MODELVIEW);
142+
glLoadIdentity();
143+
144+
glMatrixMode(GL_TEXTURE);
145+
glLoadIdentity();
146+
}

0 commit comments

Comments
 (0)