From 55231bb8d3e1a4e2752ac3d444c4287eb0ca4e8b Mon Sep 17 00:00:00 2001 From: void_17 <61356189+void2012@users.noreply.github.com> Date: Fri, 6 Mar 2026 02:11:18 +0700 Subject: Remove AUTO_VAR macro and _toString function (#592) --- Minecraft.Client/Common/Consoles_App.cpp | 145 ++++++++++++------------------- 1 file changed, 57 insertions(+), 88 deletions(-) (limited to 'Minecraft.Client/Common/Consoles_App.cpp') diff --git a/Minecraft.Client/Common/Consoles_App.cpp b/Minecraft.Client/Common/Consoles_App.cpp index b476ca90..d17d0678 100644 --- a/Minecraft.Client/Common/Consoles_App.cpp +++ b/Minecraft.Client/Common/Consoles_App.cpp @@ -1475,9 +1475,8 @@ void CMinecraftApp::ActionGameSettings(int iPad,eGameSetting eVal) app.SetXuiServerAction(iPad,eXuiServerAction_ServerSettingChanged_Gamertags); PlayerList *players = MinecraftServer::getInstance()->getPlayerList(); - for(AUTO_VAR(it3, players->players.begin()); it3 != players->players.end(); ++it3) + for( auto& decorationPlayer : players->players ) { - shared_ptr decorationPlayer = *it3; decorationPlayer->setShowOnMaps((app.GetGameHostOption(eGameHostOption_Gamertags)!=0)?true:false); } } @@ -5641,7 +5640,7 @@ bool CMinecraftApp::isXuidNotch(PlayerUID xuid) bool CMinecraftApp::isXuidDeadmau5(PlayerUID xuid) { - AUTO_VAR(it, MojangData.find( xuid )); // 4J Stu - The .at and [] accessors insert elements if they don't exist + auto it = MojangData.find(xuid); // 4J Stu - The .at and [] accessors insert elements if they don't exist if (it != MojangData.end() ) { MOJANG_DATA *pMojangData=MojangData[xuid]; @@ -5659,7 +5658,7 @@ void CMinecraftApp::AddMemoryTextureFile(const wstring &wName,PBYTE pbData,DWORD EnterCriticalSection(&csMemFilesLock); // check it's not already in PMEMDATA pData=NULL; - AUTO_VAR(it, m_MEM_Files.find(wName)); + auto it = m_MEM_Files.find(wName); if(it != m_MEM_Files.end()) { #ifndef _CONTENT_PACKAGE @@ -5704,7 +5703,7 @@ void CMinecraftApp::RemoveMemoryTextureFile(const wstring &wName) { EnterCriticalSection(&csMemFilesLock); - AUTO_VAR(it, m_MEM_Files.find(wName)); + auto it = m_MEM_Files.find(wName); if(it != m_MEM_Files.end()) { #ifndef _CONTENT_PACKAGE @@ -5730,7 +5729,7 @@ bool CMinecraftApp::DefaultCapeExists() bool val = false; EnterCriticalSection(&csMemFilesLock); - AUTO_VAR(it, m_MEM_Files.find(wTex)); + auto it = m_MEM_Files.find(wTex); if(it != m_MEM_Files.end()) val = true; LeaveCriticalSection(&csMemFilesLock); @@ -5742,7 +5741,7 @@ bool CMinecraftApp::IsFileInMemoryTextures(const wstring &wName) bool val = false; EnterCriticalSection(&csMemFilesLock); - AUTO_VAR(it, m_MEM_Files.find(wName)); + auto it = m_MEM_Files.find(wName); if(it != m_MEM_Files.end()) val = true; LeaveCriticalSection(&csMemFilesLock); @@ -5752,7 +5751,7 @@ bool CMinecraftApp::IsFileInMemoryTextures(const wstring &wName) void CMinecraftApp::GetMemFileDetails(const wstring &wName,PBYTE *ppbData,DWORD *pdwBytes) { EnterCriticalSection(&csMemFilesLock); - AUTO_VAR(it, m_MEM_Files.find(wName)); + auto it = m_MEM_Files.find(wName); if(it != m_MEM_Files.end()) { PMEMDATA pData = (*it).second; @@ -5767,7 +5766,7 @@ void CMinecraftApp::AddMemoryTPDFile(int iConfig,PBYTE pbData,DWORD dwBytes) EnterCriticalSection(&csMemTPDLock); // check it's not already in PMEMDATA pData=NULL; - AUTO_VAR(it, m_MEM_TPD.find(iConfig)); + auto it = m_MEM_TPD.find(iConfig); if(it == m_MEM_TPD.end()) { pData = (PMEMDATA)new BYTE[sizeof(MEMDATA)]; @@ -5787,7 +5786,7 @@ void CMinecraftApp::RemoveMemoryTPDFile(int iConfig) EnterCriticalSection(&csMemTPDLock); // check it's not already in PMEMDATA pData=NULL; - AUTO_VAR(it, m_MEM_TPD.find(iConfig)); + auto it = m_MEM_TPD.find(iConfig); if(it != m_MEM_TPD.end()) { pData=m_MEM_TPD[iConfig]; @@ -5844,7 +5843,7 @@ bool CMinecraftApp::IsFileInTPD(int iConfig) bool val = false; EnterCriticalSection(&csMemTPDLock); - AUTO_VAR(it, m_MEM_TPD.find(iConfig)); + auto it = m_MEM_TPD.find(iConfig); if(it != m_MEM_TPD.end()) val = true; LeaveCriticalSection(&csMemTPDLock); @@ -5854,7 +5853,7 @@ bool CMinecraftApp::IsFileInTPD(int iConfig) void CMinecraftApp::GetTPD(int iConfig,PBYTE *ppbData,DWORD *pdwBytes) { EnterCriticalSection(&csMemTPDLock); - AUTO_VAR(it, m_MEM_TPD.find(iConfig)); + auto it = m_MEM_TPD.find(iConfig); if(it != m_MEM_TPD.end()) { PMEMDATA pData = (*it).second; @@ -6989,7 +6988,7 @@ HRESULT CMinecraftApp::RegisterDLCData(eDLCContentType eType, WCHAR *pwchBannerN // check if we already have this info from the local DLC file wstring wsTemp=wchUppercaseProductID; - AUTO_VAR(it, DLCInfo_Full.find(wsTemp)); + auto it = DLCInfo_Full.find(wsTemp); if( it == DLCInfo_Full.end() ) { // Not found @@ -7097,7 +7096,7 @@ HRESULT CMinecraftApp::RegisterDLCData(char *pchDLCName, unsigned int uiSortInde #if defined( __PS3__) || defined(__ORBIS__) || defined(__PSVITA__) bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,ULONGLONG *pullVal) { - AUTO_VAR(it, DLCInfo_SkinName.find(FirstSkin)); + auto it = DLCInfo_SkinName.find(FirstSkin); if( it == DLCInfo_SkinName.end() ) { return false; @@ -7110,7 +7109,7 @@ bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,ULONGLON } bool CMinecraftApp::GetDLCNameForPackID(const int iPackID,char **ppchKeyID) { - AUTO_VAR(it, DLCTextures_PackID.find(iPackID)); + auto it = DLCTextures_PackID.find(iPackID); if( it == DLCTextures_PackID.end() ) { *ppchKeyID=NULL; @@ -7128,7 +7127,7 @@ DLC_INFO *CMinecraftApp::GetDLCInfo(char *pchDLCName) if(DLCInfo.size()>0) { - AUTO_VAR(it, DLCInfo.find(tempString)); + auto it = DLCInfo.find(tempString); if( it == DLCInfo.end() ) { @@ -7185,7 +7184,7 @@ char *CMinecraftApp::GetDLCInfoTextures(int iIndex) #elif defined _XBOX_ONE bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,wstring &ProductId) { - AUTO_VAR(it, DLCInfo_SkinName.find(FirstSkin)); + auto it = DLCInfo_SkinName.find(FirstSkin); if( it == DLCInfo_SkinName.end() ) { return false; @@ -7198,7 +7197,7 @@ bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,wstring } bool CMinecraftApp::GetDLCFullOfferIDForPackID(const int iPackID,wstring &ProductId) { - AUTO_VAR(it, DLCTextures_PackID.find(iPackID)); + auto it = DLCTextures_PackID.find(iPackID); if( it == DLCTextures_PackID.end() ) { return false; @@ -7243,7 +7242,7 @@ wstring CMinecraftApp::GetDLCInfoTexturesFullOffer(int iIndex) #else bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,ULONGLONG *pullVal) { - AUTO_VAR(it, DLCInfo_SkinName.find(FirstSkin)); + auto it = DLCInfo_SkinName.find(FirstSkin); if( it == DLCInfo_SkinName.end() ) { return false; @@ -7256,15 +7255,15 @@ bool CMinecraftApp::GetDLCFullOfferIDForSkinID(const wstring &FirstSkin,ULONGLON } bool CMinecraftApp::GetDLCFullOfferIDForPackID(const int iPackID,ULONGLONG *pullVal) { - AUTO_VAR(it, DLCTextures_PackID.find(iPackID)); + auto it = DLCTextures_PackID.find(iPackID); if( it == DLCTextures_PackID.end() ) { - *pullVal=(ULONGLONG)0; + *pullVal=0ULL; return false; } else { - *pullVal=(ULONGLONG)it->second; + *pullVal=it->second; return true; } } @@ -7273,7 +7272,7 @@ DLC_INFO *CMinecraftApp::GetDLCInfoForTrialOfferID(ULONGLONG ullOfferID_Trial) //DLC_INFO *pDLCInfo=NULL; if(DLCInfo_Trial.size()>0) { - AUTO_VAR(it, DLCInfo_Trial.find(ullOfferID_Trial)); + auto it = DLCInfo_Trial.find(ullOfferID_Trial); if( it == DLCInfo_Trial.end() ) { @@ -7330,7 +7329,7 @@ DLC_INFO *CMinecraftApp::GetDLCInfoForFullOfferID(WCHAR *pwchProductID) wstring wsTemp = pwchProductID; if(DLCInfo_Full.size()>0) { - AUTO_VAR(it, DLCInfo_Full.find(wsTemp)); + auto it = DLCInfo_Full.find(wsTemp); if( it == DLCInfo_Full.end() ) { @@ -7370,7 +7369,7 @@ DLC_INFO *CMinecraftApp::GetDLCInfoForFullOfferID(ULONGLONG ullOfferID_Full) if(DLCInfo_Full.size()>0) { - AUTO_VAR(it, DLCInfo_Full.find(ullOfferID_Full)); + auto it = DLCInfo_Full.find(ullOfferID_Full); if( it == DLCInfo_Full.end() ) { @@ -7570,9 +7569,8 @@ void CMinecraftApp::AddLevelToBannedLevelList(int iPad, PlayerUID xuid, char *ps DWORD dwDataBytes=(DWORD)(sizeof(BANNEDLISTDATA)*m_vBannedListA[iPad]->size()); PBANNEDLISTDATA pBannedList = (BANNEDLISTDATA *)(new CHAR [dwDataBytes]); int iCount=0; - for(AUTO_VAR(it, m_vBannedListA[iPad]->begin()); it != m_vBannedListA[iPad]->end(); ++it) + for (PBANNEDLISTDATA pData : *m_vBannedListA[iPad] ) { - PBANNEDLISTDATA pData=*it; memcpy(&pBannedList[iCount++],pData,sizeof(BANNEDLISTDATA)); } @@ -7590,9 +7588,8 @@ void CMinecraftApp::AddLevelToBannedLevelList(int iPad, PlayerUID xuid, char *ps bool CMinecraftApp::IsInBannedLevelList(int iPad, PlayerUID xuid, char *pszLevelName) { - for(AUTO_VAR(it, m_vBannedListA[iPad]->begin()); it != m_vBannedListA[iPad]->end(); ++it) + for( PBANNEDLISTDATA pData : *m_vBannedListA[iPad] ) { - PBANNEDLISTDATA pData=*it; #ifdef _XBOX_ONE PlayerUID bannedPlayerUID = pData->wchPlayerUID; if(IsEqualXUID (bannedPlayerUID,xuid) && (strcmp(pData->pszLevelName,pszLevelName)==0)) @@ -7613,7 +7610,7 @@ void CMinecraftApp::RemoveLevelFromBannedLevelList(int iPad, PlayerUID xuid, cha //bool bRes; // we will have retrieved the banned level list from TMS, so remove this one from it and write it back to TMS - for(AUTO_VAR(it, m_vBannedListA[iPad]->begin()); it != m_vBannedListA[iPad]->end(); ) + for (auto it = m_vBannedListA[iPad]->begin(); it != m_vBannedListA[iPad]->end(); ) { PBANNEDLISTDATA pBannedListData = *it; @@ -8321,10 +8318,8 @@ unsigned int CMinecraftApp::CreateImageTextData(PBYTE bTextMetadata, __int64 see void CMinecraftApp::AddTerrainFeaturePosition(_eTerrainFeatureType eFeatureType,int x,int z) { // check we don't already have this in - for(AUTO_VAR(it, m_vTerrainFeatures.begin()); it < m_vTerrainFeatures.end(); ++it) + for( FEATURE_DATA *pFeatureData : m_vTerrainFeatures ) { - FEATURE_DATA *pFeatureData=*it; - if((pFeatureData->eTerrainFeature==eFeatureType) &&(pFeatureData->x==x) && (pFeatureData->z==z)) return; } @@ -8338,10 +8333,8 @@ void CMinecraftApp::AddTerrainFeaturePosition(_eTerrainFeatureType eFeatureType, _eTerrainFeatureType CMinecraftApp::IsTerrainFeature(int x,int z) { - for(AUTO_VAR(it, m_vTerrainFeatures.begin()); it < m_vTerrainFeatures.end(); ++it) + for(FEATURE_DATA *pFeatureData : m_vTerrainFeatures ) { - FEATURE_DATA *pFeatureData=*it; - if((pFeatureData->x==x) && (pFeatureData->z==z)) return pFeatureData->eTerrainFeature; } @@ -8350,10 +8343,8 @@ _eTerrainFeatureType CMinecraftApp::IsTerrainFeature(int x,int z) bool CMinecraftApp::GetTerrainFeaturePosition(_eTerrainFeatureType eType,int *pX, int *pZ) { - for(AUTO_VAR(it, m_vTerrainFeatures.begin()); it < m_vTerrainFeatures.end(); ++it) + for ( const FEATURE_DATA *pFeatureData : m_vTerrainFeatures ) { - FEATURE_DATA *pFeatureData=*it; - if(pFeatureData->eTerrainFeature==eType) { *pX=pFeatureData->x; @@ -8489,10 +8480,8 @@ unsigned int CMinecraftApp::AddDLCRequest(eDLCMarketplaceType eType, bool bPromo // If it's already in there, promote it to the top of the list int iPosition=0; - for(AUTO_VAR(it, m_DLCDownloadQueue.begin()); it != m_DLCDownloadQueue.end(); ++it) + for( DLCRequest *pCurrent : m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - if(pCurrent->dwType==m_dwContentTypeA[eType]) { // already got this in the list @@ -8543,7 +8532,7 @@ unsigned int CMinecraftApp::AddTMSPPFileTypeRequest(eDLCContentType eType, bool bool bPromoted=false; - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for ( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { TMSPPRequest *pCurrent = *it; @@ -8601,10 +8590,8 @@ unsigned int CMinecraftApp::AddTMSPPFileTypeRequest(eDLCContentType eType, bool // this may already be present in the vector because of a previous trial/full offer bool bAlreadyInQueue=false; - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; - if(wcscmp(pDLC->wchDataFile,pCurrent->wchFilename)==0) { bAlreadyInQueue=true; @@ -8664,10 +8651,8 @@ unsigned int CMinecraftApp::AddTMSPPFileTypeRequest(eDLCContentType eType, bool if(!bPresent) // retrieve it from TMSPP { bool bAlreadyInQueue=false; - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; - if(wcscmp(pDLC->wchBanner,pCurrent->wchFilename)==0) { bAlreadyInQueue=true; @@ -8721,10 +8706,8 @@ unsigned int CMinecraftApp::AddTMSPPFileTypeRequest(eDLCContentType eType, bool // this may already be present in the vector because of a previous trial/full offer bool bAlreadyInQueue=false; - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; - if(wcscmp(pDLC->wchBanner,pCurrent->wchFilename)==0) { bAlreadyInQueue=true; @@ -8767,10 +8750,8 @@ unsigned int CMinecraftApp::AddTMSPPFileTypeRequest(eDLCContentType eType, bool bool CMinecraftApp::CheckTMSDLCCanStop() { EnterCriticalSection(&csTMSPPDownloadQueue); - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; - if(pCurrent->eState==e_TMS_ContentState_Retrieving) { LeaveCriticalSection(&csTMSPPDownloadQueue); @@ -8796,10 +8777,8 @@ bool CMinecraftApp::RetrieveNextDLCContent() } EnterCriticalSection(&csDLCDownloadQueue); - for(AUTO_VAR(it, m_DLCDownloadQueue.begin()); it != m_DLCDownloadQueue.end(); ++it) + for( const DLCRequest* pCurrent : m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - if(pCurrent->eState==e_DLC_ContentState_Retrieving) { LeaveCriticalSection(&csDLCDownloadQueue); @@ -8808,10 +8787,8 @@ bool CMinecraftApp::RetrieveNextDLCContent() } // Now look for the next retrieval - for(AUTO_VAR(it, m_DLCDownloadQueue.begin()); it != m_DLCDownloadQueue.end(); ++it) + for( DLCRequest *pCurrent : m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - if(pCurrent->eState==e_DLC_ContentState_Idle) { #ifdef _DEBUG @@ -8853,9 +8830,8 @@ int CMinecraftApp::TMSPPFileReturned(LPVOID pParam,int iPad,int iUserData,C4JSto // find the right one in the vector EnterCriticalSection(&pClass->csTMSPPDownloadQueue); - for(AUTO_VAR(it, pClass->m_TMSPPDownloadQueue.begin()); it != pClass->m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : pClass->m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; #if defined(_XBOX) || defined(_WINDOWS64) char szFile[MAX_TMSFILENAME_SIZE]; wcstombs(szFile,pCurrent->wchFilename,MAX_TMSFILENAME_SIZE); @@ -8956,8 +8932,7 @@ bool CMinecraftApp::RetrieveNextTMSPPContent() if(ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())==false) return false; - EnterCriticalSection(&csTMSPPDownloadQueue); - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { TMSPPRequest *pCurrent = *it; @@ -8970,7 +8945,7 @@ bool CMinecraftApp::RetrieveNextTMSPPContent() } // Now look for the next retrieval - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { TMSPPRequest *pCurrent = *it; @@ -9074,11 +9049,10 @@ void CMinecraftApp::ClearAndResetDLCDownloadQueue() int iPosition=0; EnterCriticalSection(&csTMSPPDownloadQueue); - for(AUTO_VAR(it, m_DLCDownloadQueue.begin()); it != m_DLCDownloadQueue.end(); ++it) + for( DLCRequest *pCurrent : m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - - delete pCurrent; + if ( pCurrent ) + delete pCurrent; iPosition++; } m_DLCDownloadQueue.clear(); @@ -9100,11 +9074,10 @@ void CMinecraftApp::ClearTMSPPFilesRetrieved() { int iPosition=0; EnterCriticalSection(&csTMSPPDownloadQueue); - for(AUTO_VAR(it, m_TMSPPDownloadQueue.begin()); it != m_TMSPPDownloadQueue.end(); ++it) + for ( TMSPPRequest *pCurrent : m_TMSPPDownloadQueue ) { - TMSPPRequest *pCurrent = *it; - - delete pCurrent; + if ( pCurrent ) + delete pCurrent; iPosition++; } m_TMSPPDownloadQueue.clear(); @@ -9118,10 +9091,8 @@ int CMinecraftApp::DLCOffersReturned(void *pParam, int iOfferC, DWORD dwType, in // find the right one in the vector EnterCriticalSection(&pClass->csTMSPPDownloadQueue); - for(AUTO_VAR(it, pClass->m_DLCDownloadQueue.begin()); it != pClass->m_DLCDownloadQueue.end(); ++it) + for( DLCRequest *pCurrent : pClass->m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - // avatar items are coming back as type Content, so we can't trust the type setting if(pCurrent->dwType==dwType) { @@ -9151,10 +9122,8 @@ bool CMinecraftApp::DLCContentRetrieved(eDLCMarketplaceType eType) // If there's already a retrieve in progress, quit // we may have re-ordered the list, so need to check every item EnterCriticalSection(&csDLCDownloadQueue); - for(AUTO_VAR(it, m_DLCDownloadQueue.begin()); it != m_DLCDownloadQueue.end(); ++it) + for( DLCRequest *pCurrent : m_DLCDownloadQueue ) { - DLCRequest *pCurrent = *it; - if((pCurrent->dwType==m_dwContentTypeA[eType]) && (pCurrent->eState==e_DLC_ContentState_Retrieved)) { LeaveCriticalSection(&csDLCDownloadQueue); @@ -9208,17 +9177,17 @@ vector * CMinecraftApp::SetAdditionalSkinBoxes(DWORD dwSkinID, vect app.DebugPrintf("*** SetAdditionalSkinBoxes - Inserting model parts for skin %d from array of Skin Boxes\n",dwSkinID&0x0FFFFFFF); // convert the skin boxes into model parts, and add to the humanoid model - for(AUTO_VAR(it, pvSkinBoxA->begin());it != pvSkinBoxA->end(); ++it) + for( auto& it : *pvSkinBoxA ) { if(pModel) { - ModelPart *pModelPart=pModel->AddOrRetrievePart(*it); + ModelPart *pModelPart=pModel->AddOrRetrievePart(it); pvModelPart->push_back(pModelPart); } } - m_AdditionalModelParts.insert( std::pair *>(dwSkinID, pvModelPart) ); - m_AdditionalSkinBoxes.insert( std::pair *>(dwSkinID, pvSkinBoxA) ); + m_AdditionalModelParts.emplace(dwSkinID, pvModelPart); + m_AdditionalSkinBoxes.emplace(dwSkinID, pvSkinBoxA); LeaveCriticalSection( &csAdditionalSkinBoxes ); LeaveCriticalSection( &csAdditionalModelParts ); @@ -9232,7 +9201,7 @@ vector *CMinecraftApp::GetAdditionalModelParts(DWORD dwSkinID) vector *pvModelParts=NULL; if(m_AdditionalModelParts.size()>0) { - AUTO_VAR(it, m_AdditionalModelParts.find(dwSkinID)); + auto it = m_AdditionalModelParts.find(dwSkinID); if(it!=m_AdditionalModelParts.end()) { pvModelParts = (*it).second; @@ -9249,7 +9218,7 @@ vector *CMinecraftApp::GetAdditionalSkinBoxes(DWORD dwSkinID) vector *pvSkinBoxes=NULL; if(m_AdditionalSkinBoxes.size()>0) { - AUTO_VAR(it,m_AdditionalSkinBoxes.find(dwSkinID)); + auto it = m_AdditionalSkinBoxes.find(dwSkinID); if(it!=m_AdditionalSkinBoxes.end()) { pvSkinBoxes = (*it).second; @@ -9267,7 +9236,7 @@ unsigned int CMinecraftApp::GetAnimOverrideBitmask(DWORD dwSkinID) if(m_AnimOverrides.size()>0) { - AUTO_VAR(it, m_AnimOverrides.find(dwSkinID)); + auto it = m_AnimOverrides.find(dwSkinID); if(it!=m_AnimOverrides.end()) { uiAnimOverrideBitmask = (*it).second; @@ -9285,7 +9254,7 @@ void CMinecraftApp::SetAnimOverrideBitmask(DWORD dwSkinID,unsigned int uiAnimOve if(m_AnimOverrides.size()>0) { - AUTO_VAR(it, m_AnimOverrides.find(dwSkinID)); + auto it = m_AnimOverrides.find(dwSkinID); if(it!=m_AnimOverrides.end()) { LeaveCriticalSection( &csAnimOverrideBitmask ); -- cgit v1.2.3 From cbcf3de358f97ae1f687f3ffa47fcdb910e39bcb Mon Sep 17 00:00:00 2001 From: ModMaker101 <119018978+ModMaker101@users.noreply.github.com> Date: Thu, 5 Mar 2026 14:41:17 -0500 Subject: Fix gamma slider via pixel shader #178 (#481) * Fix gamma slider via pixel shader #178 * LCE-like gamma using postprocess shader --- Minecraft.Client/Common/Consoles_App.cpp | 3 +- Minecraft.Client/Common/PostProcesser.h | 57 +++ Minecraft.Client/GameRenderer.cpp | 349 +++++++++++----- Minecraft.Client/GameRenderer.h | 13 + Minecraft.Client/Minecraft.Client.vcxproj | 45 +- Minecraft.Client/Minecraft.Client.vcxproj.filters | 10 +- Minecraft.Client/Windows64/PostProcesser.cpp | 451 +++++++++++++++++++++ Minecraft.Client/Windows64/Windows64_Minecraft.cpp | 3 + 8 files changed, 811 insertions(+), 120 deletions(-) create mode 100644 Minecraft.Client/Common/PostProcesser.h create mode 100644 Minecraft.Client/Windows64/PostProcesser.cpp (limited to 'Minecraft.Client/Common/Consoles_App.cpp') diff --git a/Minecraft.Client/Common/Consoles_App.cpp b/Minecraft.Client/Common/Consoles_App.cpp index d17d0678..00dedbe1 100644 --- a/Minecraft.Client/Common/Consoles_App.cpp +++ b/Minecraft.Client/Common/Consoles_App.cpp @@ -1,5 +1,4 @@ - -#include "stdafx.h" +#include "stdafx.h" #include "..\..\Minecraft.World\net.minecraft.world.entity.item.h" #include "..\..\Minecraft.World\net.minecraft.world.entity.player.h" #include "..\..\Minecraft.World\net.minecraft.world.level.tile.entity.h" diff --git a/Minecraft.Client/Common/PostProcesser.h b/Minecraft.Client/Common/PostProcesser.h new file mode 100644 index 00000000..ab7aedf6 --- /dev/null +++ b/Minecraft.Client/Common/PostProcesser.h @@ -0,0 +1,57 @@ +#pragma once + +#include + +class PostProcesser +{ +public: + static PostProcesser& GetInstance() + { + static PostProcesser instance; + return instance; + } + + void Init(); + void Apply() const; + void SetViewport(const D3D11_VIEWPORT& viewport); + void ResetViewport(); + void CopyBackbuffer(); // Copy backbuffer once before multi-pass gamma + void ApplyFromCopied() const; // Apply gamma using already-copied offscreen texture + void Cleanup(); + void SetGamma(float gamma); + float GetGamma() const { return m_gamma; } + PostProcesser(const PostProcesser&) = delete; + PostProcesser& operator=(const PostProcesser&) = delete; + +private: + PostProcesser(); + ~PostProcesser(); + + static bool IsRunningUnderWine(); + + ID3D11Texture2D* m_pGammaOffscreenTex = nullptr; + ID3D11ShaderResourceView* m_pGammaOffscreenSRV = nullptr; + ID3D11RenderTargetView* m_pGammaOffscreenRTV = nullptr; + ID3D11VertexShader* m_pGammaVS = nullptr; + ID3D11PixelShader* m_pGammaPS = nullptr; + ID3D11Buffer* m_pGammaCB = nullptr; + ID3D11SamplerState* m_pGammaSampler = nullptr; + ID3D11RasterizerState* m_pGammaRastState = nullptr; + ID3D11DepthStencilState* m_pGammaDepthState = nullptr; + ID3D11BlendState* m_pGammaBlendState = nullptr; + + bool m_initialized = false; + float m_gamma = 1.0f; + bool m_wineMode = false; + D3D11_VIEWPORT m_customViewport; + bool m_useCustomViewport = false; + + struct GammaCBData + { + float gamma; + float pad[3]; + }; + + static const char* g_gammaVSCode; + static const char* g_gammaPSCode; +}; \ No newline at end of file diff --git a/Minecraft.Client/GameRenderer.cpp b/Minecraft.Client/GameRenderer.cpp index 5a936c82..390f114d 100644 --- a/Minecraft.Client/GameRenderer.cpp +++ b/Minecraft.Client/GameRenderer.cpp @@ -51,6 +51,7 @@ #include "TexturePackRepository.h" #include "TexturePack.h" #include "TextureAtlas.h" +#include "Common/PostProcesser.h" bool GameRenderer::anaglyph3d = false; int GameRenderer::anaglyphPass = 0; @@ -832,124 +833,253 @@ void GameRenderer::tickLightTexture() void GameRenderer::updateLightTexture(float a) { - // 4J-JEV: Now doing light textures on PER PLAYER basis. - // 4J - we *had* added separate light textures for all dimensions, and this loop to update them all here - for(int j = 0; j < XUSER_MAX_COUNT; j++ ) - { - // Loop over all the players - shared_ptr player = Minecraft::GetInstance()->localplayers[j]; - if (player == NULL) continue; - - Level *level = player->level; // 4J - was mc->level when it was just to update the one light texture - - float skyDarken1 = level->getSkyDarken((float) 1); - for (int i = 0; i < 256; i++) - { - float darken = skyDarken1 * 0.95f + 0.05f; - float sky = level->dimension->brightnessRamp[i / 16] * darken; - float block = level->dimension->brightnessRamp[i % 16] * (blr * 0.1f + 1.5f); - - if (level->skyFlashTime > 0) - { - sky = level->dimension->brightnessRamp[i / 16]; - } + CachePlayerGammas(); - float rs = sky * (skyDarken1 * 0.65f + 0.35f); - float gs = sky * (skyDarken1 * 0.65f + 0.35f); - float bs = sky; - - float rb = block; - float gb = block * ((block * 0.6f + 0.4f) * 0.6f + 0.4f); - float bb = block * ((block * block) * 0.6f + 0.4f); - - float _r = (rs + rb); - float _g = (gs + gb); - float _b = (bs + bb); + for (int j = 0; j < XUSER_MAX_COUNT; j++) + { + shared_ptr player = Minecraft::GetInstance()->localplayers[j]; + if (player == nullptr) + continue; + + Level *level = player->level; + + const float skyDarken1 = level->getSkyDarken(1.0f); + for (int i = 0; i < 256; i++) + { + const float darken = skyDarken1 * 0.95f + 0.05f; + float sky = level->dimension->brightnessRamp[i / 16] * darken; + const float block = level->dimension->brightnessRamp[i % 16] * (blr * 0.1f + 1.5f); + + if (level->skyFlashTime < 0) + { + sky = level->dimension->brightnessRamp[i / 16]; + } + + const float rs = sky * (skyDarken1 * 0.65f + 0.35f); + const float gs = sky * (skyDarken1 * 0.65f + 0.35f); + const float bs = sky; + + const float rb = block; + const float gb = block * ((block * 0.6f + 0.4f) * 0.6f + 0.4f); + const float bb = block * ((block * block) * 0.6f + 0.4f); + + float _r = (rs + rb); + float _g = (gs + gb); + float _b = (bs + bb); + + _r = _r * 0.96f + 0.03f; + _g = _g * 0.96f + 0.03f; + _b = _b * 0.96f + 0.03f; + + if (darkenWorldAmount > 0) + { + const float amount = darkenWorldAmountO + (darkenWorldAmount - darkenWorldAmountO) * a; + _r = _r * (1.0f - amount) + (_r * 0.7f) * amount; + _g = _g * (1.0f - amount) + (_g * 0.6f) * amount; + _b = _b * (1.0f - amount) + (_b * 0.6f) * amount; + } + + if (level->dimension->id == 1) + { + _r = (0.22f + rb * 0.75f); + _g = (0.28f + gb * 0.75f); + _b = (0.25f + bb * 0.75f); + } + + if (player->hasEffect(MobEffect::nightVision)) + { + const float scale = getNightVisionScale(player, a); + float dist = 1.0f / _r; + if (dist > (1.0f / _g)) + dist = (1.0f / _g); + if (dist > (1.0f / _b)) + dist = (1.0f / _b); + _r = _r * (1.0f - scale) + (_r * dist) * scale; + _g = _g * (1.0f - scale) + (_g * dist) * scale; + _b = _b * (1.0f - scale) + (_b * dist) * scale; + } + + if (_r > 1.0f) + _r = 1.0f; + if (_r < 0.0f) + _r = 0.0f; + if (_g > 1.0f) + _g = 1.0f; + if (_g < 0.0f) + _g = 0.0f; + if (_b > 1.0f) + _b = 1.0f; + if (_b < 0.0f) + _b = 0.0f; + + constexpr int alpha = 255; + const int r = static_cast(_r * 255); + const int g = static_cast(_g * 255); + const int b = static_cast(_b * 255); - _r = _r * 0.96f + 0.03f; - _g = _g * 0.96f + 0.03f; - _b = _b * 0.96f + 0.03f; +#if ( defined _DURANGO || defined _WIN64 || __PSVITA__ ) + lightPixels[j][i] = alpha << 24 | b << 16 | g << 8 | r; +#elif ( defined _XBOX || defined __ORBIS__ ) + lightPixels[j][i] = alpha << 24 | r << 16 | g << 8 | b; +#else + lightPixels[j][i] = r << 24 | g << 16 | b << 8 | alpha; +#endif + } - if (darkenWorldAmount > 0) - { - float amount = darkenWorldAmountO + (darkenWorldAmount - darkenWorldAmountO) * a; - _r = _r * (1.0f - amount) + (_r * .7f) * amount; - _g = _g * (1.0f - amount) + (_g * .6f) * amount; - _b = _b * (1.0f - amount) + (_b * .6f) * amount; - } + mc->textures->replaceTextureDirect(lightPixels[j], 16, 16, getLightTexture(j, level)); + } +} - if (level->dimension->id == 1) - { - _r = (0.22f + rb * 0.75f); - _g = (0.28f + gb * 0.75f); - _b = (0.25f + bb * 0.75f); - } +float GameRenderer::ComputeGammaFromSlider(float slider0to100) +{ + float slider = slider0to100; + slider = max(slider, 0.0f); + slider = min(slider, 100.0f); + + if (slider > 50.0f) + return 1.0f + (slider - 50.0f) / 50.0f * 3.0f; // 1.0 -> 4.0 + else + return 1.0f - (50.0f - slider) / 50.0f * 0.85f; // 1.0 -> 0.15 +} - if (player->hasEffect(MobEffect::nightVision)) - { - float scale = getNightVisionScale(player, a); - { - float dist = 1.0f / _r; - if (dist > (1.0f / _g)) - { - dist = (1.0f / _g); - } - if (dist > (1.0f / _b)) - { - dist = (1.0f / _b); - } - _r = _r * (1.0f - scale) + (_r * dist) * scale; - _g = _g * (1.0f - scale) + (_g * dist) * scale; - _b = _b * (1.0f - scale) + (_b * dist) * scale; - } - } +void GameRenderer::CachePlayerGammas() +{ + for (int j = 0; j < XUSER_MAX_COUNT && j < NUM_LIGHT_TEXTURES; ++j) + { + std::shared_ptr player = Minecraft::GetInstance()->localplayers[j]; + if (!player) + { + m_cachedGammaPerPlayer[j] = 1.0f; + continue; + } + + const float slider = app.GetGameSettings(j, eGameSetting_Gamma); // 0..100 + m_cachedGammaPerPlayer[j] = ComputeGammaFromSlider(slider); + } +} - if (_r > 1) _r = 1; - if (_g > 1) _g = 1; - if (_b > 1) _b = 1; - - float brightness = mc->options->gamma; - - float ir = 1 - _r; - float ig = 1 - _g; - float ib = 1 - _b; - ir = 1 - (ir * ir * ir * ir); - ig = 1 - (ig * ig * ig * ig); - ib = 1 - (ib * ib * ib * ib); - _r = _r * (1 - brightness) + ir * brightness; - _g = _g * (1 - brightness) + ig * brightness; - _b = _b * (1 - brightness) + ib * brightness; - - _r = _r * 0.96f + 0.03f; - _g = _g * 0.96f + 0.03f; - _b = _b * 0.96f + 0.03f; - - if (_r > 1) _r = 1; - if (_g > 1) _g = 1; - if (_b > 1) _b = 1; - if (_r < 0) _r = 0; - if (_g < 0) _g = 0; - if (_b < 0) _b = 0; - - int alpha = 255; - int r = (int) (_r * 255); - int g = (int) (_g * 255); - int b = (int) (_b * 255); +bool GameRenderer::ComputeViewportForPlayer(int j, D3D11_VIEWPORT &outViewport) const +{ + int active = 0; + int indexMap[NUM_LIGHT_TEXTURES] = {-1, -1, -1, -1}; + for (int i = 0; i < XUSER_MAX_COUNT && i < NUM_LIGHT_TEXTURES; ++i) + { + if (Minecraft::GetInstance()->localplayers[i]) + indexMap[active++] = i; + } -#if ( defined _DURANGO || defined _WIN64 || __PSVITA__ ) - lightPixels[j][i] = alpha << 24 | b << 16 | g << 8 | r; -#elif ( defined _XBOX || defined __ORBIS__ ) - lightPixels[j][i] = alpha << 24 | r << 16 | g << 8 | b; -#else - lightPixels[j][i] = r << 24 | g << 16 | b << 8 | alpha; -#endif - } + if (active <= 1) + { + outViewport.TopLeftX = 0.0f; + outViewport.TopLeftY = 0.0f; + outViewport.Width = static_cast(mc->width); + outViewport.Height = static_cast(mc->height); + outViewport.MinDepth = 0.0f; + outViewport.MaxDepth = 1.0f; + return true; + } + + int k = -1; + for (int ord = 0; ord < active; ++ord) + if (indexMap[ord] == j) + { + k = ord; + break; + } + if (k < 0) + return false; + + const float width = static_cast(mc->width); + const float height = static_cast(mc->height); + + if (active == 2) + { + const float halfH = height * 0.5f; + outViewport.TopLeftX = 0.0f; + outViewport.Width = width; + outViewport.MinDepth = 0.0f; + outViewport.MaxDepth = 1.0f; + if (k == 0) + { + outViewport.TopLeftY = 0.0f; + outViewport.Height = halfH; + } + else + { + outViewport.TopLeftY = halfH; + outViewport.Height = halfH; + } + return true; + } + else + { + const float halfW = width * 0.5f; + const float halfH = height * 0.5f; + const int row = (k >= 2) ? 1 : 0; + const int col = (k % 2); + outViewport.TopLeftX = col ? halfW : 0.0f; + outViewport.TopLeftY = row ? halfH : 0.0f; + outViewport.Width = halfW; + outViewport.Height = halfH; + outViewport.MinDepth = 0.0f; + outViewport.MaxDepth = 1.0f; + return true; + } +} - mc->textures->replaceTextureDirect( lightPixels[j], 16, 16, getLightTexture(j,level) ); - // lightTexture->upload(); // 4J: not relevant +uint32_t GameRenderer::BuildPlayerViewports(D3D11_VIEWPORT *outViewports, float *outGammas, UINT maxCount) const +{ + UINT count = 0; + for (int j = 0; j < XUSER_MAX_COUNT && j < NUM_LIGHT_TEXTURES && count < maxCount; ++j) + { + if (!Minecraft::GetInstance()->localplayers[j]) + continue; + D3D11_VIEWPORT vp; + if (!ComputeViewportForPlayer(j, vp)) + continue; + outViewports[count] = vp; + outGammas[count] = m_cachedGammaPerPlayer[j]; + ++count; + } + return count; +} - //_updateLightTexture = false; - } +void GameRenderer::ApplyGammaPostProcess() const +{ + D3D11_VIEWPORT vps[NUM_LIGHT_TEXTURES]; + float gammas[NUM_LIGHT_TEXTURES]; + const UINT n = BuildPlayerViewports(vps, gammas, NUM_LIGHT_TEXTURES); + if (n == 0) + return; + + bool anyEffect = false; + for (UINT i = 0; i < n; ++i) + { + if (gammas[i] < 0.99f || gammas[i] > 1.01f) + { + anyEffect = true; + break; + } + } + if (!anyEffect) + return; + + if (n == 1) + { + PostProcesser::GetInstance().SetGamma(gammas[0]); + PostProcesser::GetInstance().Apply(); + } + else + { + PostProcesser::GetInstance().CopyBackbuffer(); + for (UINT i = 0; i < n; ++i) + { + PostProcesser::GetInstance().SetGamma(gammas[i]); + PostProcesser::GetInstance().SetViewport(vps[i]); + PostProcesser::GetInstance().ApplyFromCopied(); + } + PostProcesser::GetInstance().ResetViewport(); + } } float GameRenderer::getNightVisionScale(shared_ptr player, float a) @@ -1039,6 +1169,7 @@ void GameRenderer::render(float a, bool bFirst) lastNsTime = System::nanoTime(); + ApplyGammaPostProcess(); if (!mc->options->hideGui || mc->screen != NULL) { diff --git a/Minecraft.Client/GameRenderer.h b/Minecraft.Client/GameRenderer.h index 1db7713a..70fbe8b3 100644 --- a/Minecraft.Client/GameRenderer.h +++ b/Minecraft.Client/GameRenderer.h @@ -1,4 +1,9 @@ #pragma once + +#ifdef _WIN64 +#include +#endif + class Minecraft; class Entity; class Random; @@ -72,6 +77,14 @@ private: float darkenWorldAmount; float darkenWorldAmountO; + // Gamma caching + float m_cachedGammaPerPlayer[NUM_LIGHT_TEXTURES]; + static float ComputeGammaFromSlider(float slider0to100); + void CachePlayerGammas(); + void ApplyGammaPostProcess() const; + bool ComputeViewportForPlayer(int j, D3D11_VIEWPORT& outViewport) const; + uint32_t BuildPlayerViewports(D3D11_VIEWPORT* outViewports, float* outGammas, UINT maxCount) const; + bool isInClouds; float m_fov; diff --git a/Minecraft.Client/Minecraft.Client.vcxproj b/Minecraft.Client/Minecraft.Client.vcxproj index 6a4a3e1b..31a595a0 100644 --- a/Minecraft.Client/Minecraft.Client.vcxproj +++ b/Minecraft.Client/Minecraft.Client.vcxproj @@ -1571,7 +1571,7 @@ if not exist "$(TargetDir)\savedata" mkdir "$(TargetDir)\savedata" true $(OutDir)$(ProjectName).pdb - d3d11.lib;..\Minecraft.World\x64_Debug\Minecraft.World.lib;%(AdditionalDependencies);XInput9_1_0.lib;..\Minecraft.Client\Windows64\Miles\Lib\mss64.lib;wsock32.lib + legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -1800,7 +1800,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - legacy_stdio_definitions.lib;d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false UseLinkTimeCodeGeneration @@ -1846,7 +1846,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - legacy_stdio_definitions.lib;d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -1891,7 +1891,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -1932,7 +1932,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -1973,7 +1973,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -2014,7 +2014,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU true $(OutDir)$(ProjectName).pdb - d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) + d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies) NotSet false @@ -6082,6 +6082,7 @@ xcopy /q /y /i /s /e $(ProjectDir)Durango\CU $(LayoutDir)Image\Loose\CUfalse + @@ -38458,6 +38459,36 @@ xcopy /q /y /i /s /e $(ProjectDir)Durango\CU $(LayoutDir)Image\Loose\CUtrue true + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true diff --git a/Minecraft.Client/Minecraft.Client.vcxproj.filters b/Minecraft.Client/Minecraft.Client.vcxproj.filters index 2be7710a..e7de632e 100644 --- a/Minecraft.Client/Minecraft.Client.vcxproj.filters +++ b/Minecraft.Client/Minecraft.Client.vcxproj.filters @@ -723,10 +723,10 @@ {0061db22-43de-4b54-a161-c43958cdcd7e} - + {889a84db-3009-4a7c-8234-4bf93d412690} - + {e5d7fb24-25b8-413c-84ec-974bf0d4a3d1} @@ -2098,6 +2098,9 @@ Common + + Common + PS3\4JLibs\inc @@ -4920,6 +4923,9 @@ Windows64\Source Files + + Windows64\Source Files + Windows64\Source Files diff --git a/Minecraft.Client/Windows64/PostProcesser.cpp b/Minecraft.Client/Windows64/PostProcesser.cpp new file mode 100644 index 00000000..5abd83b4 --- /dev/null +++ b/Minecraft.Client/Windows64/PostProcesser.cpp @@ -0,0 +1,451 @@ +#include "stdafx.h" +#include "..\Common\PostProcesser.h" +#include +#include + +#pragma comment(lib, "d3dcompiler.lib") + +extern ID3D11Device* g_pd3dDevice; +extern ID3D11DeviceContext* g_pImmediateContext; +extern IDXGISwapChain* g_pSwapChain; +extern ID3D11RenderTargetView* g_pRenderTargetView; + +const char* PostProcesser::g_gammaVSCode = + "void main(uint id : SV_VertexID, out float4 pos : SV_Position, out float2 uv : TEXCOORD0)\n" + "{\n" + " uv = float2((id << 1) & 2, id & 2);\n" + " pos = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);\n" + "}\n"; + +const char* PostProcesser::g_gammaPSCode = + "cbuffer GammaCB : register(b0)\n" + "{\n" + " float gamma;\n" + " float3 pad;\n" + "};\n" + "Texture2D sceneTex : register(t0);\n" + "SamplerState sceneSampler : register(s0);\n" + "float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target\n" + "{\n" + " float4 color = sceneTex.Sample(sceneSampler, uv);\n" + " color.rgb = pow(max(color.rgb, 0.0), 1.0 / gamma);\n" + " return color;\n" + "}\n"; + +PostProcesser::PostProcesser() = default; + +PostProcesser::~PostProcesser() +{ + Cleanup(); +} + +bool PostProcesser::IsRunningUnderWine() +{ + const HMODULE ntdll = GetModuleHandleA("ntdll.dll"); + if (ntdll) + { + if (GetProcAddress(ntdll, "wine_get_version")) + return true; + } + return false; +} + +void PostProcesser::SetGamma(float gamma) +{ + m_gamma = gamma; +} + +void PostProcesser::Init() +{ + if (!g_pd3dDevice || !g_pSwapChain) + return; + + if (m_initialized) + return; + + m_wineMode = IsRunningUnderWine(); + + HRESULT hr; + ID3D11Texture2D* pBackBuffer = nullptr; + hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast(&pBackBuffer)); + if (FAILED(hr)) + return; + + D3D11_TEXTURE2D_DESC bbDesc; + pBackBuffer->GetDesc(&bbDesc); + pBackBuffer->Release(); + + DXGI_FORMAT texFormat = bbDesc.Format; + if (m_wineMode) + { + texFormat = DXGI_FORMAT_R8G8B8A8_UNORM; + } + + D3D11_TEXTURE2D_DESC texDesc; + ZeroMemory(&texDesc, sizeof(texDesc)); + texDesc.Width = bbDesc.Width; + texDesc.Height = bbDesc.Height; + texDesc.MipLevels = 1; + texDesc.ArraySize = 1; + texDesc.Format = texFormat; + texDesc.SampleDesc.Count = 1; + texDesc.SampleDesc.Quality = 0; + texDesc.Usage = D3D11_USAGE_DEFAULT; + texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET; + + hr = g_pd3dDevice->CreateTexture2D(&texDesc, nullptr, &m_pGammaOffscreenTex); + if (FAILED(hr)) + return; + + hr = g_pd3dDevice->CreateShaderResourceView(m_pGammaOffscreenTex, nullptr, &m_pGammaOffscreenSRV); + if (FAILED(hr)) + return; + + hr = g_pd3dDevice->CreateRenderTargetView(m_pGammaOffscreenTex, nullptr, &m_pGammaOffscreenRTV); + if (FAILED(hr)) + return; + + ID3DBlob* vsBlob = nullptr; + ID3DBlob* errBlob = nullptr; + UINT compileFlags = D3DCOMPILE_ENABLE_STRICTNESS; + if (m_wineMode) + { + compileFlags |= D3DCOMPILE_AVOID_FLOW_CONTROL; + } + hr = D3DCompile(g_gammaVSCode, strlen(g_gammaVSCode), "GammaVS", nullptr, nullptr, "main", "vs_4_0", compileFlags, 0, &vsBlob, &errBlob); + if (FAILED(hr)) + { + if (errBlob) + errBlob->Release(); + return; + } + + hr = g_pd3dDevice->CreateVertexShader(vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), nullptr, &m_pGammaVS); + vsBlob->Release(); + if (errBlob) + errBlob->Release(); + if (FAILED(hr)) + return; + + errBlob = nullptr; + ID3DBlob* psBlob = nullptr; + hr = D3DCompile(g_gammaPSCode, strlen(g_gammaPSCode), "GammaPS", nullptr, nullptr, "main", "ps_4_0", compileFlags, 0, &psBlob, &errBlob); + if (FAILED(hr)) + { + if (errBlob) + errBlob->Release(); + return; + } + + hr = g_pd3dDevice->CreatePixelShader(psBlob->GetBufferPointer(), psBlob->GetBufferSize(), nullptr, &m_pGammaPS); + psBlob->Release(); + if (errBlob) + errBlob->Release(); + if (FAILED(hr)) + return; + + D3D11_BUFFER_DESC cbDesc; + ZeroMemory(&cbDesc, sizeof(cbDesc)); + cbDesc.ByteWidth = sizeof(GammaCBData); + cbDesc.Usage = D3D11_USAGE_DYNAMIC; + cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + + GammaCBData initData = {1.0f, {0, 0, 0}}; + D3D11_SUBRESOURCE_DATA srData; + srData.pSysMem = &initData; + srData.SysMemPitch = 0; + srData.SysMemSlicePitch = 0; + + hr = g_pd3dDevice->CreateBuffer(&cbDesc, &srData, &m_pGammaCB); + if (FAILED(hr)) + return; + + D3D11_SAMPLER_DESC sampDesc; + ZeroMemory(&sampDesc, sizeof(sampDesc)); + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + + hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &m_pGammaSampler); + if (FAILED(hr)) + return; + + D3D11_RASTERIZER_DESC rasDesc; + ZeroMemory(&rasDesc, sizeof(rasDesc)); + rasDesc.FillMode = D3D11_FILL_SOLID; + rasDesc.CullMode = D3D11_CULL_NONE; + rasDesc.DepthClipEnable = FALSE; + rasDesc.ScissorEnable = FALSE; + + hr = g_pd3dDevice->CreateRasterizerState(&rasDesc, &m_pGammaRastState); + if (FAILED(hr)) + return; + + D3D11_DEPTH_STENCIL_DESC dsDesc; + ZeroMemory(&dsDesc, sizeof(dsDesc)); + dsDesc.DepthEnable = FALSE; + dsDesc.StencilEnable = FALSE; + + hr = g_pd3dDevice->CreateDepthStencilState(&dsDesc, &m_pGammaDepthState); + if (FAILED(hr)) + return; + + D3D11_BLEND_DESC blendDesc; + ZeroMemory(&blendDesc, sizeof(blendDesc)); + blendDesc.RenderTarget[0].BlendEnable = FALSE; + blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + + hr = g_pd3dDevice->CreateBlendState(&blendDesc, &m_pGammaBlendState); + if (FAILED(hr)) + return; + + m_initialized = true; +} + +void PostProcesser::Apply() const +{ + if (!m_initialized) + return; + + if (m_gamma > 0.99f && m_gamma < 1.01f) + return; + + ID3D11DeviceContext *ctx = g_pImmediateContext; + + ID3D11Texture2D *pBackBuffer = nullptr; + HRESULT hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast(&pBackBuffer)); + if (FAILED(hr)) + return; + + ctx->CopyResource(m_pGammaOffscreenTex, pBackBuffer); + + D3D11_MAPPED_SUBRESOURCE mapped; + const D3D11_MAP mapType = m_wineMode ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD; + hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped); + if (SUCCEEDED(hr)) + { + GammaCBData *cb = static_cast(mapped.pData); + cb->gamma = m_gamma; + ctx->Unmap(m_pGammaCB, 0); + } + + ID3D11RenderTargetView* oldRTV = nullptr; + ID3D11DepthStencilView* oldDSV = nullptr; + ctx->OMGetRenderTargets(1, &oldRTV, &oldDSV); + + UINT numViewports = 1; + D3D11_VIEWPORT oldViewport; + ctx->RSGetViewports(&numViewports, &oldViewport); + + ID3D11RenderTargetView* bbRTV = g_pRenderTargetView; + ctx->OMSetRenderTargets(1, &bbRTV, nullptr); + + D3D11_TEXTURE2D_DESC bbDesc; + pBackBuffer->GetDesc(&bbDesc); + pBackBuffer->Release(); + + D3D11_VIEWPORT vp; + if (m_useCustomViewport) + { + vp = m_customViewport; + } + else + { + vp.Width = static_cast(bbDesc.Width); + vp.Height = static_cast(bbDesc.Height); + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = 0; + vp.TopLeftY = 0; + } + + ctx->RSSetViewports(1, &vp); + + ctx->IASetInputLayout(nullptr); + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ctx->VSSetShader(m_pGammaVS, nullptr, 0); + ctx->PSSetShader(m_pGammaPS, nullptr, 0); + ctx->PSSetShaderResources(0, 1, &m_pGammaOffscreenSRV); + ctx->PSSetSamplers(0, 1, &m_pGammaSampler); + ctx->PSSetConstantBuffers(0, 1, &m_pGammaCB); + ctx->RSSetState(m_pGammaRastState); + ctx->OMSetDepthStencilState(m_pGammaDepthState, 0); + + constexpr float blendFactor[4] = {0, 0, 0, 0}; + ctx->OMSetBlendState(m_pGammaBlendState, blendFactor, 0xFFFFFFFF); + + ctx->Draw(3, 0); + + ID3D11ShaderResourceView* nullSrv = nullptr; + ctx->PSSetShaderResources(0, 1, &nullSrv); + + ctx->OMSetRenderTargets(1, &oldRTV, oldDSV); + ctx->RSSetViewports(1, &oldViewport); + + if (oldRTV) + oldRTV->Release(); + if (oldDSV) + oldDSV->Release(); +} + +void PostProcesser::SetViewport(const D3D11_VIEWPORT& viewport) +{ + m_customViewport = viewport; + m_useCustomViewport = true; +} + +void PostProcesser::ResetViewport() +{ + m_useCustomViewport = false; +} + +void PostProcesser::CopyBackbuffer() +{ + if (!m_initialized) + return; + + ID3D11DeviceContext* ctx = g_pImmediateContext; + + ID3D11Texture2D* pBackBuffer = nullptr; + HRESULT hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast(&pBackBuffer)); + if (FAILED(hr)) + return; + + ctx->CopyResource(m_pGammaOffscreenTex, pBackBuffer); + pBackBuffer->Release(); +} + +void PostProcesser::ApplyFromCopied() const +{ + if (!m_initialized) + return; + + if (m_gamma > 0.99f && m_gamma < 1.01f) + return; + + ID3D11DeviceContext* ctx = g_pImmediateContext; + + D3D11_MAPPED_SUBRESOURCE mapped; + const D3D11_MAP mapType = m_wineMode ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD; + const HRESULT hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped); + if (SUCCEEDED(hr)) + { + const auto cb = static_cast(mapped.pData); + cb->gamma = m_gamma; + ctx->Unmap(m_pGammaCB, 0); + } + + ID3D11RenderTargetView* oldRTV = nullptr; + ID3D11DepthStencilView* oldDSV = nullptr; + ctx->OMGetRenderTargets(1, &oldRTV, &oldDSV); + + UINT numViewports = 1; + D3D11_VIEWPORT oldViewport; + ctx->RSGetViewports(&numViewports, &oldViewport); + + ID3D11RenderTargetView* bbRTV = g_pRenderTargetView; + ctx->OMSetRenderTargets(1, &bbRTV, nullptr); + + D3D11_VIEWPORT vp; + if (m_useCustomViewport) + { + vp = m_customViewport; + } + else + { + D3D11_TEXTURE2D_DESC texDesc; + m_pGammaOffscreenTex->GetDesc(&texDesc); + vp.Width = static_cast(texDesc.Width); + vp.Height = static_cast(texDesc.Height); + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = 0; + vp.TopLeftY = 0; + } + + ctx->RSSetViewports(1, &vp); + + ctx->IASetInputLayout(nullptr); + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ctx->VSSetShader(m_pGammaVS, nullptr, 0); + ctx->PSSetShader(m_pGammaPS, nullptr, 0); + ctx->PSSetShaderResources(0, 1, &m_pGammaOffscreenSRV); + ctx->PSSetSamplers(0, 1, &m_pGammaSampler); + ctx->PSSetConstantBuffers(0, 1, &m_pGammaCB); + ctx->RSSetState(m_pGammaRastState); + ctx->OMSetDepthStencilState(m_pGammaDepthState, 0); + + constexpr float blendFactor[4] = {0, 0, 0, 0}; + ctx->OMSetBlendState(m_pGammaBlendState, blendFactor, 0xFFFFFFFF); + + ctx->Draw(3, 0); + + ID3D11ShaderResourceView* nullSrv = nullptr; + ctx->PSSetShaderResources(0, 1, &nullSrv); + + ctx->OMSetRenderTargets(1, &oldRTV, oldDSV); + ctx->RSSetViewports(1, &oldViewport); + + if (oldRTV) + oldRTV->Release(); + if (oldDSV) + oldDSV->Release(); +} + +void PostProcesser::Cleanup() +{ + if (m_pGammaBlendState) + { + m_pGammaBlendState->Release(); + m_pGammaBlendState = nullptr; + } + if (m_pGammaDepthState) + { + m_pGammaDepthState->Release(); + m_pGammaDepthState = nullptr; + } + if (m_pGammaRastState) + { + m_pGammaRastState->Release(); + m_pGammaRastState = nullptr; + } + if (m_pGammaSampler) + { + m_pGammaSampler->Release(); + m_pGammaSampler = nullptr; + } + if (m_pGammaCB) + { + m_pGammaCB->Release(); + m_pGammaCB = nullptr; + } + if (m_pGammaPS) + { + m_pGammaPS->Release(); + m_pGammaPS = nullptr; + } + if (m_pGammaVS) + { + m_pGammaVS->Release(); + m_pGammaVS = nullptr; + } + if (m_pGammaOffscreenRTV) + { + m_pGammaOffscreenRTV->Release(); + m_pGammaOffscreenRTV = nullptr; + } + if (m_pGammaOffscreenSRV) + { + m_pGammaOffscreenSRV->Release(); + m_pGammaOffscreenSRV = nullptr; + } + if (m_pGammaOffscreenTex) + { + m_pGammaOffscreenTex->Release(); + m_pGammaOffscreenTex = nullptr; + } + + m_initialized = false; +} diff --git a/Minecraft.Client/Windows64/Windows64_Minecraft.cpp b/Minecraft.Client/Windows64/Windows64_Minecraft.cpp index f5989c42..f1bc9eb8 100644 --- a/Minecraft.Client/Windows64/Windows64_Minecraft.cpp +++ b/Minecraft.Client/Windows64/Windows64_Minecraft.cpp @@ -40,6 +40,7 @@ #include "Resource.h" #include "..\..\Minecraft.World\compression.h" #include "..\..\Minecraft.World\OldChunkStorage.h" +#include "Common/PostProcesser.h" #include "Network\WinsockNetLayer.h" #include "Xbox/resource.h" @@ -874,6 +875,8 @@ HRESULT InitDevice() RenderManager.Initialise(g_pd3dDevice, g_pSwapChain); + PostProcesser::GetInstance().Init(); + return S_OK; } -- cgit v1.2.3