From 479abde9f4c752aaf1ca113d18159a74bc8aebb1 Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Sun, 11 Oct 2015 13:37:41 +1300 Subject: [PATCH] BPMemory: Convert a number of unions to BitFields --- Source/Core/VideoBackends/Software/Tev.cpp | 2 +- Source/Core/VideoCommon/BPMemory.cpp | 3 +- Source/Core/VideoCommon/BPMemory.h | 189 +++++++----------- Source/Core/VideoCommon/BPStructs.cpp | 7 +- Source/Core/VideoCommon/PixelShaderGen.cpp | 5 +- Source/Core/VideoCommon/VertexManagerBase.cpp | 2 +- 6 files changed, 87 insertions(+), 121 deletions(-) diff --git a/Source/Core/VideoBackends/Software/Tev.cpp b/Source/Core/VideoBackends/Software/Tev.cpp index 5cd4491cc9..261b2535d1 100644 --- a/Source/Core/VideoBackends/Software/Tev.cpp +++ b/Source/Core/VideoBackends/Software/Tev.cpp @@ -769,7 +769,7 @@ void Tev::Draw() // - scaling of the "k" coefficient isn't clear either. // First, calculate the offset from the viewport center (normalized to 0..1) - float offset = (Position[0] - (static_cast(bpmem.fogRange.Base.Center) - 342)) / + float offset = (Position[0] - (static_cast(bpmem.fogRange.Base.Center.Value()) - 342)) / static_cast(xfmem.viewport.wd); // Based on that, choose the index such that points which are far away from the z-axis use the diff --git a/Source/Core/VideoCommon/BPMemory.cpp b/Source/Core/VideoCommon/BPMemory.cpp index 67eeb994b1..26891ba941 100644 --- a/Source/Core/VideoCommon/BPMemory.cpp +++ b/Source/Core/VideoCommon/BPMemory.cpp @@ -24,8 +24,7 @@ float FogParam0::GetA() const float FogParam3::GetC() const { // scale mantissa from 11 to 23 bits - const u32 integral = (static_cast(c_sign) << 31) | (static_cast(c_exp) << 23) | - (static_cast(c_mant) << 12); + const u32 integral = (c_sign.Value() << 31) | (c_exp.Value() << 23) | (c_mant.Value() << 12); float real; std::memcpy(&real, &integral, sizeof(u32)); diff --git a/Source/Core/VideoCommon/BPMemory.h b/Source/Core/VideoCommon/BPMemory.h index 366dc191a5..ceb1fd3899 100644 --- a/Source/Core/VideoCommon/BPMemory.h +++ b/Source/Core/VideoCommon/BPMemory.h @@ -301,40 +301,37 @@ struct TevStageCombiner { union ColorCombiner { - struct // abc=8bit,d=10bit - { - u32 d : 4; // TEVSELCC_X - u32 c : 4; // TEVSELCC_X - u32 b : 4; // TEVSELCC_X - u32 a : 4; // TEVSELCC_X + // abc=8bit,d=10bit + BitField<0, 4, u32> d; // TEVSELCC_X + BitField<4, 4, u32> c; // TEVSELCC_X + BitField<8, 4, u32> b; // TEVSELCC_X + BitField<12, 4, u32> a; // TEVSELCC_X - u32 bias : 2; - u32 op : 1; - u32 clamp : 1; + BitField<16, 2, u32> bias; + BitField<18, 1, u32> op; + BitField<19, 1, u32> clamp; + + BitField<20, 2, u32> shift; + BitField<22, 2, u32> dest; // 1,2,3 - u32 shift : 2; - u32 dest : 2; // 1,2,3 - }; u32 hex; }; union AlphaCombiner { - struct - { - u32 rswap : 2; - u32 tswap : 2; - u32 d : 3; // TEVSELCA_ - u32 c : 3; // TEVSELCA_ - u32 b : 3; // TEVSELCA_ - u32 a : 3; // TEVSELCA_ + BitField<0, 2, u32> rswap; + BitField<2, 2, u32> tswap; + BitField<4, 3, u32> d; // TEVSELCA_ + BitField<7, 3, u32> c; // TEVSELCA_ + BitField<10, 3, u32> b; // TEVSELCA_ + BitField<13, 3, u32> a; // TEVSELCA_ - u32 bias : 2; // GXTevBias - u32 op : 1; - u32 clamp : 1; + BitField<16, 2, u32> bias; // GXTevBias + BitField<18, 1, u32> op; + BitField<19, 1, u32> clamp; + + BitField<20, 2, u32> shift; + BitField<22, 2, u32> dest; // 1,2,3 - u32 shift : 2; - u32 dest : 2; // 1,2,3 - }; u32 hex; }; @@ -353,21 +350,18 @@ struct TevStageCombiner union TevStageIndirect { - struct - { - u32 bt : 2; // Indirect tex stage ID - u32 fmt : 2; // Format: ITF_X - u32 bias : 3; // ITB_X - u32 bs : 2; // ITBA_X, indicates which coordinate will become the 'bump alpha' - u32 mid : 4; // Matrix ID to multiply offsets with - u32 sw : 3; // ITW_X, wrapping factor for S of regular coord - u32 tw : 3; // ITW_X, wrapping factor for T of regular coord - u32 lb_utclod : 1; // Use modified or unmodified texture coordinates for LOD computation - u32 fb_addprev : 1; // 1 if the texture coordinate results from the previous TEV stage should - // be added - u32 pad0 : 3; - u32 rid : 8; - }; + BitField<0, 2, u32> bt; // Indirect tex stage ID + BitField<2, 2, u32> fmt; // Format: ITF_X + BitField<4, 3, u32> bias; // ITB_X + BitField<7, 2, u32> bs; // ITBA_X, indicates which coordinate will become the 'bump alpha' + BitField<9, 4, u32> mid; // Matrix ID to multiply offsets with + BitField<13, 3, u32> sw; // ITW_X, wrapping factor for S of regular coord + BitField<16, 3, u32> tw; // ITW_X, wrapping factor for T of regular coord + BitField<19, 1, u32> lb_utclod; // Use modified or unmodified texture + // coordinates for LOD computation + BitField<20, 1, u32> fb_addprev; // 1 if the texture coordinate results from the previous TEV + // stage should be added + struct { u32 hex : 21; @@ -381,28 +375,23 @@ union TevStageIndirect union TwoTevStageOrders { - struct - { - u32 texmap0 : 3; // Indirect tex stage texmap - u32 texcoord0 : 3; - u32 enable0 : 1; // 1 if should read from texture - u32 colorchan0 : 3; // RAS1_CC_X + BitField<0, 3, u32> texmap0; // Indirect tex stage texmap + BitField<3, 3, u32> texcoord0; + BitField<6, 1, u32> enable0; // 1 if should read from texture + BitField<7, 3, u32> colorchan0; // RAS1_CC_X - u32 pad0 : 2; + BitField<12, 3, u32> texmap1; + BitField<15, 3, u32> texcoord1; + BitField<18, 1, u32> enable1; // 1 if should read from texture + BitField<19, 3, u32> colorchan1; // RAS1_CC_X - u32 texmap1 : 3; - u32 texcoord1 : 3; - u32 enable1 : 1; // 1 if should read from texture - u32 colorchan1 : 3; // RAS1_CC_X + BitField<24, 8, u32> rid; - u32 pad1 : 2; - u32 rid : 8; - }; u32 hex; - int getTexMap(int i) const { return i ? texmap1 : texmap0; } - int getTexCoord(int i) const { return i ? texcoord1 : texcoord0; } - int getEnable(int i) const { return i ? enable1 : enable0; } - int getColorChan(int i) const { return i ? colorchan1 : colorchan0; } + u32 getTexMap(int i) const { return i ? texmap1.Value() : texmap0.Value(); } + u32 getTexCoord(int i) const { return i ? texcoord1.Value() : texcoord0.Value(); } + u32 getEnable(int i) const { return i ? enable1.Value() : enable0.Value(); } + u32 getColorChan(int i) const { return i ? colorchan1.Value() : colorchan0.Value(); } }; union TEXSCALE @@ -527,20 +516,14 @@ union TexTLUT union ZTex1 { - struct - { - u32 bias : 24; - }; + BitField<0, 24, u32> bias; u32 hex; }; union ZTex2 { - struct - { - u32 type : 2; // TEV_Z_TYPE_X - u32 op : 2; // GXZTexOp - }; + BitField<0, 2, u32> type; // TEV_Z_TYPE_X + BitField<2, 2, u32> op; // GXZTexOp u32 hex; }; @@ -681,14 +664,12 @@ union FogParam0 union FogParam3 { - struct - { - u32 c_mant : 11; - u32 c_exp : 8; - u32 c_sign : 1; - u32 proj : 1; // 0 - perspective, 1 - orthographic - u32 fsel : 3; // 0 - off, 2 - linear, 4 - exp, 5 - exp2, 6 - backward exp, 7 - backward exp2 - }; + BitField<0, 11, u32> c_mant; + BitField<11, 8, u32> c_exp; + BitField<19, 1, u32> c_sign; + BitField<20, 1, u32> proj; // 0 - perspective, 1 - orthographic + BitField<21, 3, u32> fsel; // 0 - off, 2 - linear, 4 - exp, 5 - exp2, 6 - + // backward exp, 7 - backward exp2 // amount to subtract from eyespacez after range adjustment float GetC() const; @@ -698,15 +679,12 @@ union FogParam3 union FogRangeKElement { - struct - { - u32 HI : 12; - u32 LO : 12; - u32 regid : 8; - }; + BitField<0, 12, u32> HI; + BitField<12, 12, u32> LO; + BitField<24, 8, u32> regid; // TODO: Which scaling coefficient should we use here? This is just a guess! - float GetValue(int i) const { return (i ? HI : LO) / 256.f; } + float GetValue(int i) const { return (i ? HI.Value() : LO.Value()) / 256.f; } u32 HEX; }; @@ -714,13 +692,9 @@ struct FogRangeParams { union RangeBase { - struct - { - u32 Center : 10; // viewport center + 342 - u32 Enabled : 1; - u32 unused : 13; - u32 regid : 8; - }; + BitField<0, 10, u32> Center; // viewport center + 342 + BitField<10, 1, u32> Enabled; + BitField<24, 8, u32> regid; u32 hex; }; RangeBase Base; @@ -736,12 +710,9 @@ struct FogParams union FogColor { - struct - { - u32 b : 8; - u32 g : 8; - u32 r : 8; - }; + BitField<0, 8, u32> b; + BitField<8, 8, u32> g; + BitField<16, 8, u32> r; u32 hex; }; @@ -771,11 +742,8 @@ union ZMode union ConstantAlpha { - struct - { - u32 alpha : 8; - u32 enable : 1; - }; + BitField<0, 8, u32> alpha; + BitField<8, 1, u32> enable; u32 hex; }; @@ -881,19 +849,16 @@ union TevReg union TevKSel { - struct - { - u32 swap1 : 2; - u32 swap2 : 2; - u32 kcsel0 : 5; - u32 kasel0 : 5; - u32 kcsel1 : 5; - u32 kasel1 : 5; - }; + BitField<0, 2, u32> swap1; + BitField<2, 2, u32> swap2; + BitField<4, 5, u32> kcsel0; + BitField<9, 5, u32> kasel0; + BitField<14, 5, u32> kcsel1; + BitField<19, 5, u32> kasel1; u32 hex; - int getKC(int i) const { return i ? kcsel1 : kcsel0; } - int getKA(int i) const { return i ? kasel1 : kasel0; } + u32 getKC(int i) const { return i ? kcsel1.Value() : kcsel0.Value(); } + u32 getKA(int i) const { return i ? kasel1.Value() : kasel0.Value(); } }; union AlphaTest diff --git a/Source/Core/VideoCommon/BPStructs.cpp b/Source/Core/VideoCommon/BPStructs.cpp index 204c637b94..509718003c 100644 --- a/Source/Core/VideoCommon/BPStructs.cpp +++ b/Source/Core/VideoCommon/BPStructs.cpp @@ -158,7 +158,8 @@ static void BPWritten(const BPCmd& bp) } return; case BPMEM_CONSTANTALPHA: // Set Destination Alpha - PRIM_LOG("constalpha: alp=%d, en=%d", bpmem.dstalpha.alpha, bpmem.dstalpha.enable); + PRIM_LOG("constalpha: alp=%d, en=%d", bpmem.dstalpha.alpha.Value(), + bpmem.dstalpha.enable.Value()); if (bp.changes & 0xFF) PixelShaderManager::SetDestAlpha(); if (bp.changes & 0x100) @@ -322,7 +323,7 @@ static void BPWritten(const BPCmd& bp) } return; case BPMEM_BIAS: // BIAS - PRIM_LOG("ztex bias=0x%x", bpmem.ztex1.bias); + PRIM_LOG("ztex bias=0x%x", bpmem.ztex1.bias.Value()); if (bp.changes) PixelShaderManager::SetZTextureBias(); return; @@ -1281,7 +1282,7 @@ void GetBPRegInfo(const u8* data, std::string* name, std::string* desc) "Tex sel: %d\n", (data[0] - BPMEM_TEV_ALPHA_ENV) / 2, tevin[ac.a], tevin[ac.b], tevin[ac.c], tevin[ac.d], tevbias[ac.bias], tevop[ac.op], no_yes[ac.clamp], - tevscale[ac.shift], tevout[ac.dest], ac.rswap, ac.tswap); + tevscale[ac.shift], tevout[ac.dest], ac.rswap.Value(), ac.tswap.Value()); break; } diff --git a/Source/Core/VideoCommon/PixelShaderGen.cpp b/Source/Core/VideoCommon/PixelShaderGen.cpp index fe4ef11bb0..d70806153e 100644 --- a/Source/Core/VideoCommon/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/PixelShaderGen.cpp @@ -824,7 +824,7 @@ static void WriteStage(ShaderCode& out, const pixel_shader_uid_data* uid_data, i const char* tevIndAlphaSel[] = {"", "x", "y", "z"}; const char* tevIndAlphaMask[] = {"248", "224", "240", "248"}; // 0b11111000, 0b11100000, 0b11110000, 0b11111000 - out.Write("alphabump = iindtex%d.%s & %s;\n", tevind.bt, tevIndAlphaSel[tevind.bs], + out.Write("alphabump = iindtex%d.%s & %s;\n", tevind.bt.Value(), tevIndAlphaSel[tevind.bs], tevIndAlphaMask[tevind.fmt]); } else @@ -836,7 +836,8 @@ static void WriteStage(ShaderCode& out, const pixel_shader_uid_data* uid_data, i { // format const char* tevIndFmtMask[] = {"255", "31", "15", "7"}; - out.Write("\tint3 iindtevcrd%d = iindtex%d & %s;\n", n, tevind.bt, tevIndFmtMask[tevind.fmt]); + out.Write("\tint3 iindtevcrd%d = iindtex%d & %s;\n", n, tevind.bt.Value(), + tevIndFmtMask[tevind.fmt]); // bias - TODO: Check if this needs to be this complicated.. const char* tevIndBiasField[] = {"", "x", "y", "xy", diff --git a/Source/Core/VideoCommon/VertexManagerBase.cpp b/Source/Core/VideoCommon/VertexManagerBase.cpp index b6088ab2e4..502f67f417 100644 --- a/Source/Core/VideoCommon/VertexManagerBase.cpp +++ b/Source/Core/VideoCommon/VertexManagerBase.cpp @@ -195,7 +195,7 @@ void VertexManagerBase::Flush() #if defined(_DEBUG) || defined(DEBUGFAST) PRIM_LOG("frame%d:\n texgen=%d, numchan=%d, dualtex=%d, ztex=%d, cole=%d, alpe=%d, ze=%d", g_ActiveConfig.iSaveTargetId, xfmem.numTexGen.numTexGens, xfmem.numChan.numColorChans, - xfmem.dualTexTrans.enabled, bpmem.ztex2.op, (int)bpmem.blendmode.colorupdate, + xfmem.dualTexTrans.enabled, (int)bpmem.ztex2.op, (int)bpmem.blendmode.colorupdate, (int)bpmem.blendmode.alphaupdate, (int)bpmem.zmode.updateenable); for (unsigned int i = 0; i < xfmem.numChan.numColorChans; ++i)