diff --git a/desmume/src/MMU.cpp b/desmume/src/MMU.cpp index c832e3604..3b41ff4fc 100644 --- a/desmume/src/MMU.cpp +++ b/desmume/src/MMU.cpp @@ -4704,7 +4704,7 @@ u32 FASTCALL _MMU_ARM9_read32(u32 adr) case eng_3D_CLIPMTX_RESULT+60: { //LOG("4000640h..67Fh - CLIPMTX_RESULT - Read Current Clip Coordinates Matrix (R)"); - return gfx3d_GetClipMatrix ((adr-0x04000640)/4); + return gfx3d_GetClipMatrix((adr-0x04000640)/4); } case eng_3D_VECMTX_RESULT: case eng_3D_VECMTX_RESULT+4: @@ -4717,7 +4717,7 @@ u32 FASTCALL _MMU_ARM9_read32(u32 adr) case eng_3D_VECMTX_RESULT+32: { //LOG("4000680h..6A3h - VECMTX_RESULT - Read Current Directional Vector Matrix (R)"); - return gfx3d_GetDirectionalMatrix ((adr-0x04000680)/4); + return gfx3d_GetDirectionalMatrix((adr-0x04000680)/4); } case eng_3D_RAM_COUNT: diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index 8ab4f4524..d17ac9859 100644 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -1054,7 +1054,7 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D &engine) // safe to use. glBindBuffer(GL_UNIFORM_BUFFER, OGLRef.uboRenderStatesID); - OGLRenderStates *state = (OGLRenderStates *)glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(OGLRenderStates), GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); + OGLRenderStates *state = (OGLRenderStates *)glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(OGLRenderStates), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); state->framebufferSize.x = this->_framebufferWidth; state->framebufferSize.y = this->_framebufferHeight; diff --git a/desmume/src/gfx3d.cpp b/desmume/src/gfx3d.cpp index 24f9f5bad..09f40b663 100644 --- a/desmume/src/gfx3d.cpp +++ b/desmume/src/gfx3d.cpp @@ -226,7 +226,7 @@ public: for(u32 i=0;i<4;i++) read8le(&junk8,f); read8le(&junk8,f); } - else if(version == 2) + else if (version == 2) { read32le(&shiftCommand,f); read32le(¶mCounter,f); @@ -260,7 +260,7 @@ CACHE_ALIGN u8 mixTable555[32][32][32]; CACHE_ALIGN u32 dsDepthExtend_15bit_to_24bit[32768]; //is this a crazy idea? this table spreads 5 bits evenly over 31 from exactly 0 to INT_MAX -CACHE_ALIGN const int material_5bit_to_31bit[] = { +CACHE_ALIGN const u32 material_5bit_to_31bit[] = { 0x00000000, 0x04210842, 0x08421084, 0x0C6318C6, 0x10842108, 0x14A5294A, 0x18C6318C, 0x1CE739CE, 0x21084210, 0x25294A52, 0x294A5294, 0x2D6B5AD6, @@ -325,7 +325,7 @@ int _hack_getMatrixStackLevel(int which) { return mtxStack[which].position; } static CACHE_ALIGN s32 mtxCurrent [4][16]; static CACHE_ALIGN s32 mtxTemporal[16]; -static u32 mode = 0; +static MatrixMode mode = MATRIXMODE_PROJECTION; // Indexes for matrix loading/multiplication static u8 ML4x4ind = 0; @@ -337,7 +337,7 @@ static u8 MM3x3ind = 0; // Data for vertex submission static CACHE_ALIGN s16 s16coord[4] = {0, 0, 0, 0}; static char coordind = 0; -static u32 vtxFormat = GFX3D_TRIANGLES; +static PolygonPrimitiveType vtxFormat = GFX3D_TRIANGLES; static BOOL inBegin = FALSE; // Data for basic transforms @@ -405,7 +405,8 @@ int listTwiddle = 1; int triStripToggle; //list-building state -struct tmpVertInfo { +struct tmpVertInfo +{ //the number of verts registered in this list int count; //indices to the main vert list @@ -415,7 +416,8 @@ struct tmpVertInfo { } tempVertInfo; -static void twiddleLists() { +static void twiddleLists() +{ listTwiddle++; listTwiddle &= 1; polylist = &polylists[listTwiddle]; @@ -436,7 +438,7 @@ static void makeTables() { //produce the color bits of a 24bpp color from a DS RGB15 using bit logic (internal use only). RGB are reverse of usual #define RGB15TO24_BITLOGIC_REVERSE(col) ( (material_5bit_to_8bit[(col)&0x1F]<<16) | (material_5bit_to_8bit[((col)>>5)&0x1F]<<8) | material_5bit_to_8bit[((col)>>10)&0x1F] ) - for(u16 i=0;i<32768;i++) + for (size_t i = 0; i < 32768; i++) { color_15bit_to_24bit[i] = LE_TO_LOCAL_32( RGB15TO24_BITLOGIC(i) ); color_15bit_to_24bit_reverse[i] = LE_TO_LOCAL_32( RGB15TO24_BITLOGIC_REVERSE(i) ); @@ -446,21 +448,22 @@ static void makeTables() { dsDepthExtend_15bit_to_24bit[i] = LE_TO_LOCAL_32( (i*0x200)+((i+1)>>15)*0x01FF ); } - for (int i = 0; i < 65536; i++) + for (size_t i = 0; i < 65536; i++) float16table[i] = fix2float((signed short)i); - for (int i = 0; i < 1024; i++) + for (size_t i = 0; i < 1024; i++) float10Table[i] = ((signed short)(i<<6)) / (float)(1<<12); - for (int i = 0; i < 1024; i++) + for (size_t i = 0; i < 1024; i++) float10RelTable[i] = ((signed short)(i<<6)) / (float)(1<<18); - for (int i = 0; i < 1024; i++) + for (size_t i = 0; i < 1024; i++) normalTable[i] = ((signed short)(i<<6)) / (float)(1<<15); - for(int r=0;r<=31;r++) - for(int oldr=0;oldr<=31;oldr++) - for(int a=0;a<=31;a++) { + for (size_t r = 0; r <= 31; r++) + for (size_t oldr = 0; oldr <= 31; oldr++) + for (size_t a = 0; a <= 31; a++) + { int temp = (r*a + oldr*(31-a)) / 31; mixTable555[a][r][oldr] = temp; } @@ -601,7 +604,7 @@ void gfx3d_reset() textureFormat = 0; texturePalette = 0; polyAttrPending = 0; - mode = 0; + mode = MATRIXMODE_PROJECTION; s16coord[0] = s16coord[1] = s16coord[2] = s16coord[3] = 0; coordind = 0; vtxFormat = GFX3D_TRIANGLES; @@ -746,17 +749,19 @@ static void SetVertex() //TODO - viewport transform? int continuation = 0; - if(vtxFormat==GFX3D_TRIANGLE_STRIP && !tempVertInfo.first) + if (vtxFormat==GFX3D_TRIANGLE_STRIP && !tempVertInfo.first) continuation = 2; - else if(vtxFormat==GFX3D_QUAD_STRIP && !tempVertInfo.first) + else if (vtxFormat==GFX3D_QUAD_STRIP && !tempVertInfo.first) continuation = 2; //record the vertex //VERT &vert = tempVertList.list[tempVertList.count]; - int vertIndex = vertlist->count + tempVertInfo.count - continuation; - if(vertIndex<0) { + const size_t vertIndex = vertlist->count + tempVertInfo.count - continuation; + if (vertIndex >= VERTLIST_SIZE) + { printf("wtf\n"); } + VERT &vert = vertlist->list[vertIndex]; //printf("%f %f %f\n",coordTransformed[0],coordTransformed[1],coordTransformed[2]); @@ -787,7 +792,8 @@ static void SetVertex() //possibly complete a polygon { polygonListCompleted = 2; - switch(vtxFormat) { + switch(vtxFormat) + { case GFX3D_TRIANGLES: if(tempVertInfo.count!=3) break; @@ -797,9 +803,10 @@ static void SetVertex() SUBMITVERTEX(1,1); SUBMITVERTEX(2,2); vertlist->count+=3; - polylist->list[polylist->count].type = 3; + polylist->list[polylist->count].type = POLYGON_TYPE_TRIANGLE; tempVertInfo.count = 0; break; + case GFX3D_QUADS: if(tempVertInfo.count!=4) break; @@ -809,9 +816,10 @@ static void SetVertex() SUBMITVERTEX(2,2); SUBMITVERTEX(3,3); vertlist->count+=4; - polylist->list[polylist->count].type = 4; + polylist->list[polylist->count].type = POLYGON_TYPE_QUAD; tempVertInfo.count = 0; break; + case GFX3D_TRIANGLE_STRIP: if(tempVertInfo.count!=3) break; @@ -819,7 +827,7 @@ static void SetVertex() SUBMITVERTEX(0,0); SUBMITVERTEX(1,1); SUBMITVERTEX(2,2); - polylist->list[polylist->count].type = 3; + polylist->list[polylist->count].type = POLYGON_TYPE_TRIANGLE; if(triStripToggle) tempVertInfo.map[1] = vertlist->count+2-continuation; @@ -835,6 +843,7 @@ static void SetVertex() tempVertInfo.first = false; tempVertInfo.count = 2; break; + case GFX3D_QUAD_STRIP: if(tempVertInfo.count!=4) break; @@ -843,7 +852,7 @@ static void SetVertex() SUBMITVERTEX(1,1); SUBMITVERTEX(2,3); SUBMITVERTEX(3,2); - polylist->list[polylist->count].type = 4; + polylist->list[polylist->count].type = POLYGON_TYPE_QUAD; tempVertInfo.map[0] = vertlist->count+2-continuation; tempVertInfo.map[1] = vertlist->count+3-continuation; if(tempVertInfo.first) @@ -852,11 +861,12 @@ static void SetVertex() tempVertInfo.first = false; tempVertInfo.count = 2; break; + default: return; } - if(polygonListCompleted == 1) + if (polygonListCompleted == 1) { POLY &poly = polylist->list[polylist->count]; @@ -867,9 +877,9 @@ static void SetVertex() if (!(textureFormat & (7 << 26))) // no texture { bool duplicated = false; - VERT &vert0 = vertlist->list[poly.vertIndexes[0]]; - VERT &vert1 = vertlist->list[poly.vertIndexes[1]]; - VERT &vert2 = vertlist->list[poly.vertIndexes[2]]; + const VERT &vert0 = vertlist->list[poly.vertIndexes[0]]; + const VERT &vert1 = vertlist->list[poly.vertIndexes[1]]; + const VERT &vert2 = vertlist->list[poly.vertIndexes[2]]; if ( (vert0.x == vert1.x) && (vert0.y == vert1.y) ) duplicated = true; else if ( (vert1.x == vert2.x) && (vert1.y == vert2.y) ) duplicated = true; @@ -880,7 +890,7 @@ static void SetVertex() if (duplicated) { //printf("Line Segmet detected (poly type %i, mode %i, texparam %08X)\n", poly.type, poly.vtxFormat, textureFormat); - poly.vtxFormat = vtxFormat + 4; + poly.vtxFormat = (PolygonPrimitiveType)(vtxFormat + 4); } } @@ -910,7 +920,7 @@ static void gfx3d_glTexImage_cache() texCoordinateTransform = (textureFormat>>30); } -static void gfx3d_glLightDirection_cache(int index) +static void gfx3d_glLightDirection_cache(const size_t index) { s32 v = lightDirection[index]; @@ -928,7 +938,7 @@ static void gfx3d_glLightDirection_cache(int index) //Calculate the half angle vector s32 lineOfSight[4] = {0, 0, (-1)<<12, 0}; - for(int i = 0; i < 4; i++) + for (size_t i = 0; i < 4; i++) { cacheHalfVector[index][i] = ((cacheLightDirection[index][i] + lineOfSight[i])); } @@ -937,11 +947,11 @@ static void gfx3d_glLightDirection_cache(int index) //can't believe the hardware really does this... but yet it seems... s32 halfLength = ((s32)(sqrt((double)vec3dot_fixed32(cacheHalfVector[index],cacheHalfVector[index]))))<<6; - if(halfLength!=0) + if (halfLength != 0) { halfLength = abs(halfLength); halfLength >>= 6; - for(int i = 0; i < 4; i++) + for (size_t i = 0; i < 4; i++) { s32 temp = cacheHalfVector[index][i]; temp <<= 6; @@ -955,7 +965,7 @@ static void gfx3d_glLightDirection_cache(int index) //=============================================================================== static void gfx3d_glMatrixMode(u32 v) { - mode = (v&3); + mode = (MatrixMode)(v & 0x03); GFX_DELAY(1); } @@ -963,24 +973,24 @@ static void gfx3d_glMatrixMode(u32 v) static void gfx3d_glPushMatrix() { //this command always works on both pos and vector when either pos or pos-vector are the current mtx mode - short mymode = (mode==1?2:mode); + const MatrixMode mymode = ((mode == MATRIXMODE_POSITION) ? MATRIXMODE_POSITION_VECTOR : mode); MatrixStackPushMatrix(&mtxStack[mymode], mtxCurrent[mymode]); GFX_DELAY(17); - if(mymode==2) - MatrixStackPushMatrix (&mtxStack[1], mtxCurrent[1]); + if (mymode == MATRIXMODE_POSITION_VECTOR) + MatrixStackPushMatrix(&mtxStack[1], mtxCurrent[1]); } static void gfx3d_glPopMatrix(s32 i) { // The stack has only one level (at address 0) in projection mode, // in that mode, the parameter value is ignored, the offset is always +1 in that mode. - if (mode == 0) i = 1; + if (mode == MATRIXMODE_PROJECTION) i = 1; //this command always works on both pos and vector when either pos or pos-vector are the current mtx mode - short mymode = (mode==1?2:mode); + const MatrixMode mymode = ((mode == MATRIXMODE_POSITION) ? MATRIXMODE_POSITION_VECTOR : mode); //i = (i<<26)>>26; //previously, we sign extended here. that isnt really necessary since the stacks are apparently modularly addressed. so i am somewhat doubtful that this is a real concept. @@ -994,18 +1004,18 @@ static void gfx3d_glPopMatrix(s32 i) GFX_DELAY(36); - if (mymode == 2) + if (mymode == MATRIXMODE_POSITION_VECTOR) MatrixStackPopMatrix(mtxCurrent[1], &mtxStack[1], i); } static void gfx3d_glStoreMatrix(u32 v) { //this command always works on both pos and vector when either pos or pos-vector are the current mtx mode - short mymode = (mode==1?2:mode); + const MatrixMode mymode = ((mode == MATRIXMODE_POSITION) ? MATRIXMODE_POSITION_VECTOR : mode); //limit height of these stacks. //without the mymode==3 namco classics galaxian will try to use pos=1 and overrun the stack, corrupting emu - if(mymode==0 || mymode==3) + if (mymode == MATRIXMODE_PROJECTION || mymode == MATRIXMODE_TEXTURE) v = 0; v &= 31; @@ -1014,25 +1024,25 @@ static void gfx3d_glStoreMatrix(u32 v) //spider-man 2 tests this on the spiderman model (and elsewhere) //i am somewhat skeptical of this, but we'll leave it this way for now. //a test shouldnt be too hard - if(v==31) + if (v == 31) MMU_new.gxstat.se = 1; - MatrixStackLoadMatrix (&mtxStack[mymode], v, mtxCurrent[mymode]); + MatrixStackLoadMatrix(&mtxStack[mymode], v, mtxCurrent[mymode]); GFX_DELAY(17); - if(mymode==2) - MatrixStackLoadMatrix (&mtxStack[1], v, mtxCurrent[1]); + if (mymode == MATRIXMODE_POSITION_VECTOR) + MatrixStackLoadMatrix(&mtxStack[1], v, mtxCurrent[1]); } static void gfx3d_glRestoreMatrix(u32 v) { //this command always works on both pos and vector when either pos or pos-vector are the current mtx mode - short mymode = (mode==1?2:mode); + const MatrixMode mymode = ((mode == MATRIXMODE_POSITION) ? MATRIXMODE_POSITION_VECTOR : mode); //limit height of these stacks //without the mymode==3 namco classics galaxian will try to use pos=1 and overrun the stack, corrupting emu - if(mymode==0 || mymode==3) + if (mymode == MATRIXMODE_PROJECTION || mymode == MATRIXMODE_TEXTURE) v = 0; v &= 31; @@ -1041,26 +1051,26 @@ static void gfx3d_glRestoreMatrix(u32 v) //spider-man 2 tests this on the spiderman model (and elsewhere) //i am somewhat skeptical of this, but we'll leave it this way for now. //a test shouldnt be too hard - if(v==31) + if (v == 31) MMU_new.gxstat.se = 1; - MatrixCopy (mtxCurrent[mymode], MatrixStackGetPos(&mtxStack[mymode], v)); + MatrixCopy(mtxCurrent[mymode], MatrixStackGetPos(&mtxStack[mymode], v)); GFX_DELAY(36); - if (mymode == 2) - MatrixCopy (mtxCurrent[1], MatrixStackGetPos(&mtxStack[1], v)); + if (mymode == MATRIXMODE_POSITION_VECTOR) + MatrixCopy(mtxCurrent[1], MatrixStackGetPos(&mtxStack[1], v)); } static void gfx3d_glLoadIdentity() { - MatrixIdentity (mtxCurrent[mode]); + MatrixIdentity(mtxCurrent[mode]); GFX_DELAY(19); - if (mode == 2) - MatrixIdentity (mtxCurrent[1]); + if (mode == MATRIXMODE_POSITION_VECTOR) + MatrixIdentity(mtxCurrent[1]); //printf("identity: %d to: \n",mode); MatrixPrint(mtxCurrent[1]); } @@ -1077,8 +1087,8 @@ static BOOL gfx3d_glLoadMatrix4x4(s32 v) //vector_fix2float<4>(mtxCurrent[mode], 4096.f); - if (mode == 2) - MatrixCopy (mtxCurrent[1], mtxCurrent[2]); + if (mode == MATRIXMODE_POSITION_VECTOR) + MatrixCopy(mtxCurrent[1], mtxCurrent[2]); //printf("load4x4: matrix %d to: \n",mode); MatrixPrint(mtxCurrent[1]); return TRUE; @@ -1101,8 +1111,8 @@ static BOOL gfx3d_glLoadMatrix4x3(s32 v) GFX_DELAY(30); - if (mode == 2) - MatrixCopy (mtxCurrent[1], mtxCurrent[2]); + if (mode == MATRIXMODE_POSITION_VECTOR) + MatrixCopy(mtxCurrent[1], mtxCurrent[2]); //printf("load4x3: matrix %d to: \n",mode); MatrixPrint(mtxCurrent[1]); return TRUE; } @@ -1119,17 +1129,17 @@ static BOOL gfx3d_glMultMatrix4x4(s32 v) //vector_fix2float<4>(mtxTemporal, 4096.f); - MatrixMultiply (mtxCurrent[mode], mtxTemporal); + MatrixMultiply(mtxCurrent[mode], mtxTemporal); - if (mode == 2) + if (mode == MATRIXMODE_POSITION_VECTOR) { - MatrixMultiply (mtxCurrent[1], mtxTemporal); + MatrixMultiply(mtxCurrent[1], mtxTemporal); GFX_DELAY_M2(30); } //printf("mult4x4: matrix %d to: \n",mode); MatrixPrint(mtxCurrent[1]); - MatrixIdentity (mtxTemporal); + MatrixIdentity(mtxTemporal); return TRUE; } @@ -1138,8 +1148,8 @@ static BOOL gfx3d_glMultMatrix4x3(s32 v) mtxTemporal[MM4x3ind] = v; MM4x3ind++; - if((MM4x3ind & 0x03) == 3) MM4x3ind++; - if(MM4x3ind<16) return FALSE; + if ((MM4x3ind & 0x03) == 3) MM4x3ind++; + if (MM4x3ind < 16) return FALSE; MM4x3ind = 0; GFX_DELAY(31); @@ -1148,11 +1158,11 @@ static BOOL gfx3d_glMultMatrix4x3(s32 v) //fill in the unusued matrix values mtxTemporal[3] = mtxTemporal[7] = mtxTemporal[11] = 0; - mtxTemporal[15] = 1<<12; + mtxTemporal[15] = 1 << 12; MatrixMultiply (mtxCurrent[mode], mtxTemporal); - if (mode == 2) + if (mode == MATRIXMODE_POSITION_VECTOR) { MatrixMultiply (mtxCurrent[1], mtxTemporal); GFX_DELAY_M2(30); @@ -1161,18 +1171,17 @@ static BOOL gfx3d_glMultMatrix4x3(s32 v) //printf("mult4x3: matrix %d to: \n",mode); MatrixPrint(mtxCurrent[1]); //does this really need to be done? - MatrixIdentity (mtxTemporal); + MatrixIdentity(mtxTemporal); return TRUE; } static BOOL gfx3d_glMultMatrix3x3(s32 v) { mtxTemporal[MM3x3ind] = v; - - + MM3x3ind++; - if((MM3x3ind & 0x03) == 3) MM3x3ind++; - if(MM3x3ind<12) return FALSE; + if ((MM3x3ind & 0x03) == 3) MM3x3ind++; + if (MM3x3ind<12) return FALSE; MM3x3ind = 0; GFX_DELAY(28); @@ -1184,11 +1193,11 @@ static BOOL gfx3d_glMultMatrix3x3(s32 v) mtxTemporal[15] = 1<<12; mtxTemporal[12] = mtxTemporal[13] = mtxTemporal[14] = 0; - MatrixMultiply (mtxCurrent[mode], mtxTemporal); + MatrixMultiply(mtxCurrent[mode], mtxTemporal); - if (mode == 2) + if (mode == MATRIXMODE_POSITION_VECTOR) { - MatrixMultiply (mtxCurrent[1], mtxTemporal); + MatrixMultiply(mtxCurrent[1], mtxTemporal); GFX_DELAY_M2(30); } @@ -1196,7 +1205,7 @@ static BOOL gfx3d_glMultMatrix3x3(s32 v) //does this really need to be done? - MatrixIdentity (mtxTemporal); + MatrixIdentity(mtxTemporal); return TRUE; } @@ -1206,10 +1215,10 @@ static BOOL gfx3d_glScale(s32 v) ++scaleind; - if(scaleind<3) return FALSE; + if (scaleind < 3) return FALSE; scaleind = 0; - MatrixScale (mtxCurrent[(mode==2?1:mode)], scale); + MatrixScale(mtxCurrent[(mode == MATRIXMODE_POSITION_VECTOR ? MATRIXMODE_POSITION : mode)], scale); //printf("scale: matrix %d to: \n",mode); MatrixPrint(mtxCurrent[1]); GFX_DELAY(22); @@ -1228,16 +1237,16 @@ static BOOL gfx3d_glTranslate(s32 v) ++transind; - if(transind<3) return FALSE; + if (transind < 3) return FALSE; transind = 0; - MatrixTranslate (mtxCurrent[mode], trans); + MatrixTranslate(mtxCurrent[mode], trans); GFX_DELAY(22); - if (mode == 2) + if (mode == MATRIXMODE_POSITION_VECTOR) { - MatrixTranslate (mtxCurrent[1], trans); + MatrixTranslate(mtxCurrent[1], trans); GFX_DELAY_M2(30); } @@ -1295,9 +1304,9 @@ static void gfx3d_glNormal(s32 v) int vertexColor[3] = { emission[0], emission[1], emission[2] }; - for(int i=0; i<4; i++) + for (size_t i = 0; i < 4; i++) { - if(!((lightMask>>i)&1)) continue; + if (!((lightMask>>i)&1)) continue; u8 _lightColor[3] = { (lightColor[i])&0x1F, @@ -1313,7 +1322,7 @@ static void gfx3d_glNormal(s32 v) s32 dot = vec3dot_fixed32(fixedTempNegativeHalf, normal); s32 fixedshininess = 0; - if(dot>0) //prevent shininess on opposite side + if (dot > 0) //prevent shininess on opposite side { //we have cos(a). it seems that we need cos(2a). trig identity is a fast way to get it. //cos^2(a)=(1/2)(1+cos(2a)) @@ -1327,7 +1336,7 @@ static void gfx3d_glNormal(s32 v) fixedshininess = std::min(fixedshininess,4095); fixedshininess = std::max(fixedshininess,0); - if(dsSpecular & 0x8000) + if (dsSpecular & 0x8000) { //shininess is 20.12 fixed point, so >>5 gives us .7 which is 128 entries //the entries are 8bits each so <<4 gives us .12 again, compatible with the lighting formulas below @@ -1335,7 +1344,7 @@ static void gfx3d_glNormal(s32 v) fixedshininess = gfx3d.state.shininessTable[fixedshininess>>5]<<4; } - for(int c = 0; c < 3; c++) + for (size_t c = 0; c < 3; c++) { s32 specComp = ((specular[c] * _lightColor[c] * fixedshininess)>>17); //5 bits for color*color and 12 bits for the shininess s32 diffComp = ((diffuse[c] * _lightColor[c] * fixed_diffuse)>>17); //5bits for the color*color and 12 its for the diffuse @@ -1344,7 +1353,7 @@ static void gfx3d_glNormal(s32 v) } } - for(int c=0;c<3;c++) + for (size_t c = 0; c < 3; c++) { colorRGB[c] = std::min(31,vertexColor[c]); } @@ -1377,7 +1386,7 @@ static void gfx3d_glTexCoord(s32 val) static BOOL gfx3d_glVertex16b(s32 v) { - if(coordind==0) + if (coordind == 0) { s16coord[0] = (v<<16)>>16; s16coord[1] = (v>>16)&0xFFFF; @@ -1492,23 +1501,23 @@ static void gfx3d_glMaterial1(u32 val) 20-29 Directional Vector's Z component (1bit sign + 9bit fractional part) 30-31 Light Number (0..3) */ -static void gfx3d_glLightDirection (u32 v) +static void gfx3d_glLightDirection(u32 v) { - int index = v>>30; + const size_t index = v >> 30; - lightDirection[index] = (s32)(v&0x3FFFFFFF); + lightDirection[index] = (s32)(v & 0x3FFFFFFF); gfx3d_glLightDirection_cache(index); GFX_DELAY(6); } -static void gfx3d_glLightColor (u32 v) +static void gfx3d_glLightColor(u32 v) { - int index = v>>30; + const size_t index = v >> 30; lightColor[index] = v; GFX_DELAY(1); } -static BOOL gfx3d_glShininess (u32 val) +static BOOL gfx3d_glShininess(u32 val) { gfx3d.state.shininessTable[shininessInd++] = ((val & 0xFF)); gfx3d.state.shininessTable[shininessInd++] = (((val >> 8) & 0xFF)); @@ -1524,7 +1533,7 @@ static BOOL gfx3d_glShininess (u32 val) static void gfx3d_glBegin(u32 v) { inBegin = TRUE; - vtxFormat = v&0x03; + vtxFormat = (PolygonPrimitiveType)(v & 0x03); triStripToggle = 0; tempVertInfo.count = 0; tempVertInfo.first = true; @@ -1644,7 +1653,7 @@ static BOOL gfx3d_glBoxTest(u32 v) ////--------------------- //transform all coords - for(int i=0;i<8;i++) + for (size_t i = 0; i < 8; i++) { //this cant work. its left as a reminder that we could (and probably should) do the boxtest in all fixed point values //MatrixMultVec4x4_M2(mtxCurrent[0], verts[i].coord); @@ -1660,7 +1669,7 @@ static BOOL gfx3d_glBoxTest(u32 v) } //clip each poly - for(int i=0;i<6;i++) + for (size_t i = 0; i < 6; i++) { const POLY &thePoly = polys[i]; const VERT *vertTable[4] = { @@ -1673,14 +1682,15 @@ static BOOL gfx3d_glBoxTest(u32 v) boxtestClipper.clipPoly(thePoly, vertTable); //if any portion of this poly was retained, then the test passes. - if(boxtestClipper.clippedPolyCounter>0) { + if (boxtestClipper.clippedPolyCounter > 0) + { //printf("%06d PASS %d\n",boxcounter,gxFIFO.size); MMU_new.gxstat.tr = 1; break; } } - if(MMU_new.gxstat.tr == 0) + if (MMU_new.gxstat.tr == 0) { //printf("%06d FAIL %d\n",boxcounter,gxFIFO.size); } @@ -1748,12 +1758,12 @@ static void gfx3d_glVecTest(u32 v) //================================================================================= (end) //================================================================================= -void VIEWPORT::decode(u32 v) +void VIEWPORT::decode(const u32 v) { - x = (v&0xFF); - y = std::min(191,(int)(((v>>8)&0xFF))); - width = (((v>>16)&0xFF)+1)-(v&0xFF); - height = ((v>>24)+1)-((v>>8)&0xFF); + this->x = (v & 0xFF); + this->y = std::min(191, (v >> 8) & 0xFF); + this->width = ((v >> 16) & 0xFF) + 1 - this->x; + this->height = std::min(191, (v >> 24) & 0xFF) + 1 - this->y; } void gfx3d_glFogColor(u32 v) @@ -1761,9 +1771,9 @@ void gfx3d_glFogColor(u32 v) gfx3d.state.fogColor = v; } -void gfx3d_glFogOffset (u32 v) +void gfx3d_glFogOffset(u32 v) { - gfx3d.state.fogOffset = (v&0x7fff); + gfx3d.state.fogOffset = (v & 0x7FFF); } void gfx3d_glClearDepth(u32 v) @@ -1805,18 +1815,15 @@ void gfx3d_UpdateToonTable(u8 offset, u32 val) //printf("toon %d set to %04X\n",offset+1,gfx3d.state.u16ToonTable[offset+1]); } -s32 gfx3d_GetClipMatrix (unsigned int index) +s32 gfx3d_GetClipMatrix(const u32 index) { - s32 val = MatrixGetMultipliedIndex (index, mtxCurrent[0], mtxCurrent[1]); - //printf("reading clip matrix: %d\n",index); - - return (s32)val; + return (s32)MatrixGetMultipliedIndex(index, mtxCurrent[0], mtxCurrent[1]); } -s32 gfx3d_GetDirectionalMatrix (unsigned int index) +s32 gfx3d_GetDirectionalMatrix(const u32 index) { - int _index = (((index / 3) * 4) + (index % 3)); + const size_t _index = (((index / 3) * 4) + (index % 3)); //return (s32)(mtxCurrent[2][_index]*(1<<12)); return mtxCurrent[2][_index]; @@ -1824,12 +1831,12 @@ s32 gfx3d_GetDirectionalMatrix (unsigned int index) void gfx3d_glAlphaFunc(u32 v) { - gfx3d.state.alphaTestRef = v&31; + gfx3d.state.alphaTestRef = v & 0x1F; } -unsigned int gfx3d_glGetPosRes(unsigned int index) +u32 gfx3d_glGetPosRes(const size_t index) { - return (unsigned int)(PTcoords[index] * 4096.0f); + return (u32)(PTcoords[index] * 4096.0f); } //#define _3D_LOG_EXEC @@ -2095,10 +2102,11 @@ void gfx3d_execute3D() //this is a SPEED HACK //fifo is currently emulated more accurately than it probably needs to be. //without this batch size the emuloop will escape way too often to run fast. - const int HACK_FIFO_BATCH_SIZE = 64; + static const size_t HACK_FIFO_BATCH_SIZE = 64; - for(int i=0;ilist[num1]; const POLY &poly2 = polylist->list[num2]; - if(poly1.maxy != poly2.maxy) + if (poly1.maxy != poly2.maxy) return poly1.maxy < poly2.maxy; - if(poly1.miny != poly2.miny) + if (poly1.miny != poly2.miny) return poly1.miny < poly2.miny; return num1 < num2; @@ -2201,7 +2209,7 @@ static void gfx3d_doFlush() gfx3d.vertlist = vertlist; //and also our current render state - if(BIT1(control)) gfx3d.state.shading = GFX3D_State::HIGHLIGHT; + if (BIT1(control)) gfx3d.state.shading = GFX3D_State::HIGHLIGHT; else gfx3d.state.shading = GFX3D_State::TOON; gfx3d.state.enableTexturing = BIT0(control); gfx3d.state.enableAlphaTest = BIT2(control); @@ -2218,18 +2226,18 @@ static void gfx3d_doFlush() gfx3d.renderState = gfx3d.state; // Override render states per user settings - if(!CommonSettings.GFX3D_Texture) + if (!CommonSettings.GFX3D_Texture) gfx3d.renderState.enableTexturing = false; - if(!CommonSettings.GFX3D_EdgeMark) + if (!CommonSettings.GFX3D_EdgeMark) gfx3d.renderState.enableEdgeMarking = false; - if(!CommonSettings.GFX3D_Fog) + if (!CommonSettings.GFX3D_Fog) gfx3d.renderState.enableFog = false; gfx3d.state.activeFlushCommand = gfx3d.state.pendingFlushCommand; - int polycount = polylist->count; + const size_t polycount = polylist->count; #ifdef _SHOW_VTX_COUNTERS max_polys = max((u32)polycount, max_polys); max_verts = max((u32)vertlist->count, max_verts); @@ -2242,7 +2250,7 @@ static void gfx3d_doFlush() //TODO - this _MUST_ be moved later in the pipeline, after clipping. //the w-division here is just an approximation to fix the shop in harvest moon island of happiness //also the buttons in the knights in the nightmare frontend depend on this - for(int i=0; ilist[poly.vertIndexes[j]].y; vertw = (vertlist->list[poly.vertIndexes[j]].w != 0.0f) ? vertlist->list[poly.vertIndexes[j]].w : 0.00000001f; @@ -2267,17 +2275,20 @@ static void gfx3d_doFlush() //we need to sort the poly list with alpha polys last //first, look for opaque polys - int ctr=0; - for(int i=0;ilist[i]; - if(!poly.isTranslucent()) + size_t ctr = 0; + for (size_t i = 0; i < polycount; i++) + { + const POLY &poly = polylist->list[i]; + if (!poly.isTranslucent()) gfx3d.indexlist.list[ctr++] = i; } - int opaqueCount = ctr; + const size_t opaqueCount = ctr; + //then look for translucent polys - for(int i=0;ilist[i]; - if(poly.isTranslucent()) + for (size_t i = 0; i < polycount; i++) + { + const POLY &poly = polylist->list[i]; + if (poly.isTranslucent()) gfx3d.indexlist.list[ctr++] = i; } @@ -2291,7 +2302,7 @@ static void gfx3d_doFlush() //should this be done after clipping?? std::stable_sort(gfx3d.indexlist.list, gfx3d.indexlist.list + opaqueCount, gfx3d_ysort_compare); - if(!gfx3d.state.sortmode) + if (!gfx3d.state.sortmode) { //if we are autosorting translucent polys, we need to do this also //TODO - this is unverified behavior. need a test case @@ -2301,7 +2312,7 @@ static void gfx3d_doFlush() //switch to the new lists twiddleLists(); - if(driver->view3d->IsRunning()) + if (driver->view3d->IsRunning()) { viewer3d_state->frameNumber = currFrameCounter; viewer3d_state->state = gfx3d.state; @@ -2329,11 +2340,11 @@ void gfx3d_VBlankSignal() void gfx3d_VBlankEndSignal(bool skipFrame) { if (!drawPending) return; - if(skipFrame) return; + if (skipFrame) return; drawPending = FALSE; - if(!CommonSettings.showGpu.main) + if (!CommonSettings.showGpu.main) { memset(gfx3d_convertedScreen, 0, sizeof(gfx3d_framebufferWidth * gfx3d_framebufferHeight * sizeof(FragmentColor))); return; @@ -2430,22 +2441,23 @@ void gfx3d_glGetMatrix(unsigned int m_mode, int index, float* dest) //} //MatrixCopy(dest, MatrixStackGetPos(&mtxStack[m_mode], index)); - s32* src; - if(index==-1) + s32 *src; + if (index == -1) src = mtxCurrent[m_mode]; - else src=MatrixStackGetPos(&mtxStack[m_mode],index); - for(int i=0;i<16;i++) + else src = MatrixStackGetPos(&mtxStack[m_mode], index); + + for (size_t i = 0; i < 16; i++) dest[i] = src[i]/4096.0f; } -void gfx3d_glGetLightDirection(unsigned int index, unsigned int* dest) +void gfx3d_glGetLightDirection(const size_t index, u32 &dst) { - *dest = lightDirection[index]; + dst = lightDirection[index]; } -void gfx3d_glGetLightColor(unsigned int index, unsigned int* dest) +void gfx3d_glGetLightColor(const size_t index, u32 &dst) { - *dest = lightColor[index]; + dst = lightColor[index]; } void gfx3d_GetLineData(int line, u8** dst) @@ -2462,13 +2474,13 @@ void gfx3d_GetLineData15bpp(int line, u16** dst) u8* lineData; gfx3d_GetLineData(line, &lineData); - for(int i=0; icount); - for(int i=0;icount;i++) + for (size_t i = 0; i < vertlist->count; i++) vertlist->list[i].save(os); OSWRITE(polylist->count); - for(int i=0;icount;i++) + for (size_t i = 0; i < polylist->count; i++) polylist->list[i].save(os); - for(int i=0;i<4;i++) + for (size_t i = 0; i < 4; i++) { OSWRITE(mtxStack[i].position); - for(int j=0;j=1) + if (version >= 1) { OSREAD(vertlist->count); - for(int i=0;icount;i++) + for (size_t i = 0; i < vertlist->count; i++) vertlist->list[i].load(is); OSREAD(polylist->count); - for(int i=0;icount;i++) + for (size_t i = 0; i < polylist->count; i++) polylist->list[i].load(is); } - if(version>=2) + if (version >= 2) { - for(int i=0;i<4;i++) + for (size_t i=0; i < 4; i++) { OSREAD(mtxStack[i].position); - for(int j=0;j=3) { + if (version >= 3) + { gxf_hardware.loadstate(is); } @@ -2641,7 +2654,7 @@ bool gfx3d_loadstate(EMUFILE* is, int size) gfx3d.polylist->count=0; gfx3d.vertlist->count=0; - if(version >= 4) + if (version >= 4) { OSREAD(cacheLightDirection); OSREAD(cacheHalfVector); @@ -2664,11 +2677,11 @@ bool gfx3d_loadstate(EMUFILE* is, int size) #define CLIPLOG2(X,Y,Z) template -static T interpolate(const float ratio, const T& x0, const T& x1) { +static T interpolate(const float ratio, const T& x0, const T& x1) +{ return (T)(x0 + (float)(x1-x0) * (ratio)); } - //http://www.cs.berkeley.edu/~ug/slide/pipeline/assignments/as6/discussion.shtml #ifdef OPTIMIZED_CLIPPING_METHOD template static FORCEINLINE VERT clipPoint(bool hirez, const VERT *inside, const VERT *outside) @@ -2677,29 +2690,18 @@ static FORCEINLINE VERT clipPoint(const VERT *inside, const VERT *outside, int c #endif { VERT ret; - - float coord_inside = inside->coord[coord]; - float coord_outside = outside->coord[coord]; - float w_inside = inside->coord[3]; - float w_outside = outside->coord[3]; - - float t; - - if(which==-1) { - w_outside = -w_outside; - w_inside = -w_inside; - } + const float coord_inside = inside->coord[coord]; + const float coord_outside = outside->coord[coord]; + const float w_inside = (which == -1) ? -inside->coord[3] : inside->coord[3]; + const float w_outside = (which == -1) ? -outside->coord[3] : outside->coord[3]; + const float t = (coord_inside - w_inside) / ((w_outside-w_inside) - (coord_outside-coord_inside)); - t = (coord_inside - w_inside)/ ((w_outside-w_inside) - (coord_outside-coord_inside)); - - #define INTERP(X) ret . X = interpolate(t, inside-> X ,outside-> X ) INTERP(coord[0]); INTERP(coord[1]); INTERP(coord[2]); INTERP(coord[3]); INTERP(texcoord[0]); INTERP(texcoord[1]); - - //if(CommonSettings.GFX3D_HighResolutionInterpolateColor) - if(hirez) + + if (hirez) { INTERP(fcolor[0]); INTERP(fcolor[1]); INTERP(fcolor[2]); } @@ -2711,7 +2713,7 @@ static FORCEINLINE VERT clipPoint(const VERT *inside, const VERT *outside, int c //this seems like a prudent measure to make sure that math doesnt make a point pop back out //of the clip volume through interpolation - if(which==-1) + if (which == -1) ret.coord[coord] = -ret.coord[3]; else ret.coord[coord] = ret.coord[3]; @@ -2740,7 +2742,7 @@ public: void clipVert(bool hirez, const VERT *vert) { - if(m_prevVert) + if (m_prevVert) this->clipSegmentVsPlane(hirez, m_prevVert, vert); else m_firstVert = (VERT *)vert; @@ -2755,25 +2757,17 @@ public: } private: - VERT* m_prevVert; VERT* m_firstVert; Next& m_next; - + FORCEINLINE void clipSegmentVsPlane(bool hirez, const VERT *vert0, const VERT *vert1) { const float *vert0coord = vert0->coord; const float *vert1coord = vert1->coord; - bool out0, out1; - if(which==-1) - out0 = vert0coord[coord] < -vert0coord[3]; - else - out0 = vert0coord[coord] > vert0coord[3]; - if(which==-1) - out1 = vert1coord[coord] < -vert1coord[3]; - else - out1 = vert1coord[coord] > vert1coord[3]; - + const bool out0 = (which == -1) ? (vert0coord[coord] < -vert0coord[3]) : (vert0coord[coord] > vert0coord[3]); + const bool out1 = (which == -1) ? (vert1coord[coord] < -vert1coord[3]) : (vert1coord[coord] > vert1coord[3]); + //CONSIDER: should we try and clip things behind the eye? does this code even successfully do it? not sure. //if(coord==2 && which==1) { // out0 = vert0coord[2] < 0; @@ -2781,33 +2775,35 @@ private: //} //both outside: insert no points - if(out0 && out1) { + if (out0 && out1) + { CLIPLOG(" both outside\n"); } //both inside: insert the next point - if(!out0 && !out1) + if (!out0 && !out1) { CLIPLOG(" both inside\n"); m_next.clipVert(hirez, vert1); } //exiting volume: insert the clipped point - if(!out0 && out1) + if (!out0 && out1) { CLIPLOG(" exiting\n"); assert((u32)numScratchClipVerts < MAX_SCRATCH_CLIP_VERTS); scratchClipVerts[numScratchClipVerts] = clipPoint(hirez, vert0, vert1); - m_next.clipVert(hirez,&scratchClipVerts[numScratchClipVerts++]); + m_next.clipVert(hirez, &scratchClipVerts[numScratchClipVerts++]); } //entering volume: insert clipped point and the next (interior) point - if(out0 && !out1) { + if (out0 && !out1) + { CLIPLOG(" entering\n"); assert((u32)numScratchClipVerts < MAX_SCRATCH_CLIP_VERTS); scratchClipVerts[numScratchClipVerts] = clipPoint(hirez, vert1, vert0); - m_next.clipVert(hirez,&scratchClipVerts[numScratchClipVerts++]); - m_next.clipVert(hirez,vert1); + m_next.clipVert(hirez, &scratchClipVerts[numScratchClipVerts++]); + m_next.clipVert(hirez, vert1); } } }; @@ -2820,16 +2816,19 @@ public: m_nextDestVert = verts; m_numVerts = 0; } + void clipVert(bool hirez, const VERT *vert) { assert((u32)m_numVerts < MAX_CLIPPED_VERTS); *m_nextDestVert++ = *vert; m_numVerts++; } + int finish(bool hirez) { return m_numVerts; } + private: VERT* m_nextDestVert; int m_numVerts; @@ -2845,20 +2844,22 @@ typedef ClipperPlane<1,-1,Stage4> Stage3; static Stage3 clipper3 (clipper typedef ClipperPlane<0, 1,Stage3> Stage2; static Stage2 clipper2 (clipper3); // right plane typedef ClipperPlane<0,-1,Stage2> Stage1; static Stage1 clipper (clipper2); // left plane -template void GFX3D_Clipper::clipPoly(const POLY &poly, const VERT **verts) +template +void GFX3D_Clipper::clipPoly(const POLY &poly, const VERT **verts) { CLIPLOG("==Begin poly==\n"); - int type = poly.type; + const PolygonType type = poly.type; numScratchClipVerts = 0; clipper.init(clippedPolys[clippedPolyCounter].clipVerts); - for(int i=0;icoord[coord] < -verts[0]->coord[3]; - else - out0 = verts[0]->coord[coord] > verts[0]->coord[3]; - if(which==-1) - out1 = verts[1]->coord[coord] < -verts[1]->coord[3]; - else - out1 = verts[1]->coord[coord] > verts[1]->coord[3]; + const bool out0 = (which == -1) ? (verts[0]->coord[coord] < -verts[0]->coord[3]) : (verts[0]->coord[coord] > verts[0]->coord[3]); + const bool out1 = (which == -1) ? (verts[1]->coord[coord] < -verts[1]->coord[3]) : (verts[1]->coord[coord] > verts[1]->coord[3]); //CONSIDER: should we try and clip things behind the eye? does this code even successfully do it? not sure. //if(coord==2 && which==1) { @@ -2907,30 +2901,31 @@ FORCEINLINE void GFX3D_Clipper::clipSegmentVsPlane(VERT** verts, const int coord //} //both outside: insert no points - if(out0 && out1) { + if (out0 && out1) + { CLIPLOG(" both outside\n"); } //both inside: insert the first point - if(!out0 && !out1) + if (!out0 && !out1) { CLIPLOG(" both inside\n"); outClippedPoly.clipVerts[outClippedPoly.type++] = *verts[1]; } //exiting volume: insert the clipped point and the first (interior) point - if(!out0 && out1) + if (!out0 && out1) { CLIPLOG(" exiting\n"); outClippedPoly.clipVerts[outClippedPoly.type++] = clipPoint(verts[0],verts[1], coord, which); } //entering volume: insert clipped point - if(out0 && !out1) { + if (out0 && !out1) + { CLIPLOG(" entering\n"); outClippedPoly.clipVerts[outClippedPoly.type++] = clipPoint(verts[1],verts[0], coord, which); outClippedPoly.clipVerts[outClippedPoly.type++] = *verts[1]; - } } @@ -2938,9 +2933,9 @@ FORCEINLINE void GFX3D_Clipper::clipPolyVsPlane(const int coord, int which) { outClippedPoly.type = 0; CLIPLOG2("Clipping coord %d against %f\n",coord,x); - for(int i=0;i>2)<<2; + const size_t iMod = index%4, iDiv = (index>>2)<<2; s64 temp = ((s64)matrix[iMod ]*rightMatrix[iDiv ])+((s64)matrix[iMod+ 4]*rightMatrix[iDiv+1])+ ((s64)matrix[iMod+8]*rightMatrix[iDiv+2])+((s64)matrix[iMod+12]*rightMatrix[iDiv+3]); @@ -271,21 +271,21 @@ void MatrixStackPopMatrix (s32 *mtxCurr, MatrixStack *stack, int size) MatrixCopy (mtxCurr, &stack->matrix[stack->position*16]); } -s32 * MatrixStackGetPos (MatrixStack *stack, int pos) +s32* MatrixStackGetPos(MatrixStack *stack, const size_t pos) { assert(pos<31); return &stack->matrix[pos*16]; } -s32 * MatrixStackGet (MatrixStack *stack) +s32* MatrixStackGet (MatrixStack *stack) { return &stack->matrix[stack->position*16]; } -void MatrixStackLoadMatrix (MatrixStack *stack, int pos, const s32 *ptr) +void MatrixStackLoadMatrix (MatrixStack *stack, const size_t pos, const s32 *ptr) { assert(pos<31); - MatrixCopy (&stack->matrix[pos*16], ptr); + MatrixCopy(&stack->matrix[pos*16], ptr); } void Vector2Copy(float *dst, const float *src) diff --git a/desmume/src/matrix.h b/desmume/src/matrix.h index bd85452ac..63027109d 100644 --- a/desmume/src/matrix.h +++ b/desmume/src/matrix.h @@ -49,8 +49,8 @@ void MatrixInit (s32 *matrix); //without having to make new build types to exclude the assembly files. //a bit sloppy, but there aint much to it -float MatrixGetMultipliedIndex (int index, float *matrix, float *rightMatrix); -s32 MatrixGetMultipliedIndex (int index, s32 *matrix, s32 *rightMatrix); +float MatrixGetMultipliedIndex (const u32 index, float *matrix, float *rightMatrix); +s32 MatrixGetMultipliedIndex (const u32 index, s32 *matrix, s32 *rightMatrix); void MatrixSet (s32 *matrix, int x, int y, s32 value); void MatrixCopy (s32 * matrixDST, const s32 * matrixSRC); int MatrixCompare (const s32 * matrixDST, const float * matrixSRC); @@ -60,9 +60,9 @@ void MatrixStackInit (MatrixStack *stack); void MatrixStackSetMaxSize (MatrixStack *stack, int size); void MatrixStackPushMatrix (MatrixStack *stack, const s32 *ptr); void MatrixStackPopMatrix (s32 *mtxCurr, MatrixStack *stack, int size); -s32* MatrixStackGetPos (MatrixStack *stack, int pos); +s32* MatrixStackGetPos (MatrixStack *stack, const size_t pos); s32* MatrixStackGet (MatrixStack *stack); -void MatrixStackLoadMatrix (MatrixStack *stack, int pos, const s32 *ptr); +void MatrixStackLoadMatrix (MatrixStack *stack, const size_t pos, const s32 *ptr); void Vector2Copy(float *dst, const float *src); void Vector2Add(float *dst, const float *src); diff --git a/desmume/src/rasterize.cpp b/desmume/src/rasterize.cpp index e66115db1..f9dd6cd7f 100644 --- a/desmume/src/rasterize.cpp +++ b/desmume/src/rasterize.cpp @@ -515,7 +515,7 @@ public: case POLYGON_MODE_DECAL: { - if(sampler.enabled) + if (sampler.enabled) { dst.r = decal_table[mainTexColor.a][mainTexColor.r][src.r]; dst.g = decal_table[mainTexColor.a][mainTexColor.g][src.g]; @@ -533,7 +533,7 @@ public: { const FragmentColor toonColor = this->_softRender->toonColor32LUT[src.r >> 1]; - if(gfx3d.renderState.shading == GFX3D_State::HIGHLIGHT) + if (gfx3d.renderState.shading == GFX3D_State::HIGHLIGHT) { dst.r = modulate_table[mainTexColor.r][src.r]; dst.g = modulate_table[mainTexColor.g][src.r]; @@ -1004,8 +1004,8 @@ public: lastTexKey = NULL; - GFX3D_Clipper::TClippedPoly &firstClippedPoly = this->_softRender->clippedPolys[0]; - POLY &firstPoly = *firstClippedPoly.poly; + const GFX3D_Clipper::TClippedPoly &firstClippedPoly = this->_softRender->clippedPolys[0]; + const POLY &firstPoly = *firstClippedPoly.poly; PolygonAttributes polyAttr = firstPoly.getAttributes(); u32 lastPolyAttr = firstPoly.polyAttr; u32 lastTexParams = firstPoly.texParam; @@ -1020,8 +1020,8 @@ public: polynum = i; GFX3D_Clipper::TClippedPoly &clippedPoly = this->_softRender->clippedPolys[i]; - POLY &thePoly = *clippedPoly.poly; - int type = clippedPoly.type; + const POLY &thePoly = *clippedPoly.poly; + const PolygonType type = clippedPoly.type; if (lastPolyAttr != thePoly.polyAttr) { @@ -1232,7 +1232,7 @@ Render3DError SoftRasterizerRenderer::InitTables() { static bool needTableInit = true; - if(needTableInit) + if (needTableInit) { for (size_t i = 0; i < 64; i++) { @@ -1265,7 +1265,8 @@ Render3DError SoftRasterizerRenderer::InitTables() return RENDER3DERROR_NOERR; } -size_t SoftRasterizerRenderer::performClipping(bool hirez, const VERTLIST *vertList, const POLYLIST *polyList, const INDEXLIST *indexList) +template +size_t SoftRasterizerRenderer::performClipping(const VERTLIST *vertList, const POLYLIST *polyList, const INDEXLIST *indexList) { //submit all polys to clipper clipper.reset(); @@ -1276,15 +1277,12 @@ size_t SoftRasterizerRenderer::performClipping(bool hirez, const VERTLIST *vertL &vertList->list[poly.vertIndexes[0]], &vertList->list[poly.vertIndexes[1]], &vertList->list[poly.vertIndexes[2]], - poly.type==4 + poly.type==POLYGON_TYPE_QUAD ?&vertList->list[poly.vertIndexes[3]] :NULL }; - if(hirez) - clipper.clipPoly(poly, clipVerts); - else - clipper.clipPoly(poly, clipVerts); + clipper.clipPoly(poly, clipVerts); } return clipper.clippedPolyCounter; @@ -1347,7 +1345,7 @@ void SoftRasterizerRenderer::performCoordAdjustment() for (size_t i = 0; i < this->_clippedPolyCount; i++) { GFX3D_Clipper::TClippedPoly &clippedPoly = clippedPolys[i]; - int type = clippedPoly.type; + const PolygonType type = clippedPoly.type; VERT *verts = &clippedPoly.clipVerts[0]; //here is a hack which needs to be removed. @@ -1359,28 +1357,33 @@ void SoftRasterizerRenderer::performCoordAdjustment() } } - void SoftRasterizerRenderer::setupTextures() { - TexCacheItem *lastTexKey = NULL; - u32 lastTextureFormat = 0, lastTexturePalette = 0; - bool needInitTexture = true; + if (this->_clippedPolyCount == 0) + { + return; + } + + const GFX3D_Clipper::TClippedPoly &firstClippedPoly = this->clippedPolys[0]; + const POLY &firstPoly = *firstClippedPoly.poly; + u32 lastTexParams = firstPoly.texParam; + u32 lastTexPalette = firstPoly.texPalette; + TexCacheItem *lastTexKey = TexCache_SetTexture(TexFormat_15bpp, firstPoly.texParam, firstPoly.texPalette); for (size_t i = 0; i < this->_clippedPolyCount; i++) { - GFX3D_Clipper::TClippedPoly &clippedPoly = clippedPolys[i]; - POLY *thePoly = clippedPoly.poly; + const GFX3D_Clipper::TClippedPoly &clippedPoly = clippedPolys[i]; + const POLY &thePoly = *clippedPoly.poly; //make sure all the textures we'll need are cached //(otherwise on a multithreaded system there will be multiple writers-- //this SHOULD be read-only, although some day the texcache may collect statistics or something //and then it won't be safe. - if (needInitTexture || lastTextureFormat != thePoly->texParam || lastTexturePalette != thePoly->texPalette) + if (lastTexParams != thePoly.texParam || lastTexPalette != thePoly.texPalette) { - lastTexKey = TexCache_SetTexture(TexFormat_15bpp, thePoly->texParam, thePoly->texPalette); - lastTextureFormat = thePoly->texParam; - lastTexturePalette = thePoly->texPalette; - needInitTexture = false; + lastTexKey = TexCache_SetTexture(TexFormat_15bpp, thePoly.texParam, thePoly.texPalette); + lastTexParams = thePoly.texParam; + lastTexPalette = thePoly.texPalette; } //printf("%08X %d\n",poly->texParam,rasterizerUnit[0].textures.currentNum); @@ -1415,11 +1418,11 @@ void SoftRasterizerRenderer::performBackfaceTests() { for (size_t i = 0; i < this->_clippedPolyCount; i++) { - GFX3D_Clipper::TClippedPoly &clippedPoly = clippedPolys[i]; - POLY *thePoly = clippedPoly.poly; - int type = clippedPoly.type; - VERT* verts = &clippedPoly.clipVerts[0]; - const PolygonAttributes polyAttr = thePoly->getAttributes(); + const GFX3D_Clipper::TClippedPoly &clippedPoly = clippedPolys[i]; + const POLY &thePoly = *clippedPoly.poly; + const PolygonType type = clippedPoly.type; + const VERT *verts = &clippedPoly.clipVerts[0]; + const PolygonAttributes polyAttr = thePoly.getAttributes(); //HACK: backface culling //this should be moved to gfx3d, but first we need to redo the way the lists are built @@ -1434,11 +1437,11 @@ void SoftRasterizerRenderer::performBackfaceTests() //a better approach // we have to support somewhat non-convex polygons (see NSMB world map 1st screen). // this version should handle those cases better. - size_t n = type - 1; + const size_t n = type - 1; float facing = (verts[0].y + verts[n].y) * (verts[0].x - verts[n].x) + (verts[1].y + verts[0].y) * (verts[1].x - verts[0].x) + (verts[2].y + verts[1].y) * (verts[2].x - verts[1].x); - for(int j = 2; j < n; j++) + for (size_t j = 2; j < n; j++) facing += (verts[j+1].y + verts[j].y) * (verts[j+1].x - verts[j].x); polyBackfacing[i] = (facing < 0); @@ -1460,7 +1463,14 @@ Render3DError SoftRasterizerRenderer::BeginRender(const GFX3D &engine) // Keep the current render states for later use this->currentRenderState = (GFX3D_State *)&engine.renderState; - this->_clippedPolyCount = this->performClipping(CommonSettings.GFX3D_HighResolutionInterpolateColor, engine.vertlist, engine.polylist, &engine.indexlist); + if (CommonSettings.GFX3D_HighResolutionInterpolateColor) + { + this->_clippedPolyCount = this->performClipping(engine.vertlist, engine.polylist, &engine.indexlist); + } + else + { + this->_clippedPolyCount = this->performClipping(engine.vertlist, engine.polylist, &engine.indexlist); + } if (rasterizerCores >= 4) { @@ -1553,15 +1563,15 @@ Render3DError SoftRasterizerRenderer::RenderEdgeMarking(const u16 *colorTable, c const FragmentAttributes dstAttributes = this->_framebufferAttributes[i]; const u8 polyID = dstAttributes.opaquePolyID; - if(this->edgeMarkDisabled[polyID>>3]) continue; - if(dstAttributes.isTranslucentPoly) continue; + if (this->edgeMarkDisabled[polyID>>3]) continue; + if (dstAttributes.isTranslucentPoly) continue; // > is used instead of != to prevent double edges // between overlapping polys of different IDs. // also note that the edge generally goes on the outside, not the inside, (maybe needs to change later) // and that polys with the same edge color can make edges against each other. - FragmentColor edgeColor = this->edgeMarkTable[polyID>>3]; + const FragmentColor edgeColor = this->edgeMarkTable[polyID>>3]; #define PIXOFFSET(dx,dy) ((dx)+(this->_framebufferWidth*(dy))) #define ISEDGE(dx,dy) ((x+(dx) < this->_framebufferWidth) && (y+(dy) < this->_framebufferHeight) && polyID > this->_framebufferAttributes[i+PIXOFFSET(dx,dy)].opaquePolyID) @@ -1706,12 +1716,12 @@ Render3DError SoftRasterizerRenderer::RenderFog(const u8 *densityTable, const u3 { for (size_t i = 0; i < framebufferFragmentCount; i++) { - const FragmentAttributes &destFragment = _framebufferAttributes[i]; + const FragmentAttributes &destFragment = this->_framebufferAttributes[i]; const size_t fogIndex = destFragment.depth >> 9; assert(fogIndex < 32768); const u8 fog = (destFragment.isFogged) ? this->fogTable[fogIndex] : 0; - FragmentColor &destFragmentColor = _framebufferColor[i]; + FragmentColor &destFragmentColor = this->_framebufferColor[i]; destFragmentColor.r = ((128-fog)*destFragmentColor.r + r*fog)>>7; destFragmentColor.g = ((128-fog)*destFragmentColor.g + g*fog)>>7; destFragmentColor.b = ((128-fog)*destFragmentColor.b + b*fog)>>7; @@ -1722,12 +1732,12 @@ Render3DError SoftRasterizerRenderer::RenderFog(const u8 *densityTable, const u3 { for (size_t i = 0; i < framebufferFragmentCount; i++) { - const FragmentAttributes &destFragment = _framebufferAttributes[i]; + const FragmentAttributes &destFragment = this->_framebufferAttributes[i]; const size_t fogIndex = destFragment.depth >> 9; assert(fogIndex < 32768); const u8 fog = (destFragment.isFogged) ? this->fogTable[fogIndex] : 0; - FragmentColor &destFragmentColor = _framebufferColor[i]; + FragmentColor &destFragmentColor = this->_framebufferColor[i]; destFragmentColor.a = ((128-fog)*destFragmentColor.a + a*fog)>>7; } } @@ -1741,7 +1751,7 @@ Render3DError SoftRasterizerRenderer::RenderEdgeMarkingAndFog(const SoftRasteriz { for (size_t x = 0; x < this->_framebufferWidth; x++, i++) { - FragmentColor &dstColor = _framebufferColor[i]; + FragmentColor &dstColor = this->_framebufferColor[i]; const FragmentAttributes dstAttributes = this->_framebufferAttributes[i]; const u32 depth = dstAttributes.depth; const u8 polyID = dstAttributes.opaquePolyID; diff --git a/desmume/src/rasterize.h b/desmume/src/rasterize.h index fc28ab75d..15d2fa9c3 100644 --- a/desmume/src/rasterize.h +++ b/desmume/src/rasterize.h @@ -53,7 +53,7 @@ protected: // SoftRasterizer-specific methods virtual Render3DError InitTables(); - size_t performClipping(bool hirez, const VERTLIST *vertList, const POLYLIST *polyList, const INDEXLIST *indexList); + template size_t performClipping(const VERTLIST *vertList, const POLYLIST *polyList, const INDEXLIST *indexList); // Base rendering methods virtual Render3DError BeginRender(const GFX3D &engine);