From 2696b3de088877c3a406e817ed232c252700a16c Mon Sep 17 00:00:00 2001
From: Ansariel <ansariel.hiller@phoenixviewer.com>
Date: Fri, 24 May 2024 03:25:28 +0200
Subject: Introduce LLWStringView to prevent unnecessary memory allocations

---
 indra/llcommon/llstring.cpp             | 2 +-
 indra/llcommon/llstring.h               | 3 ++-
 indra/newview/llfloaterimsessiontab.cpp | 4 ++--
 indra/newview/llfloaterimsessiontab.h   | 2 +-
 4 files changed, 6 insertions(+), 5 deletions(-)

(limited to 'indra')

diff --git a/indra/llcommon/llstring.cpp b/indra/llcommon/llstring.cpp
index bbb6aa2c20..5df0f8702d 100644
--- a/indra/llcommon/llstring.cpp
+++ b/indra/llcommon/llstring.cpp
@@ -758,7 +758,7 @@ std::string utf8str_showBytesUTF8(const std::string& utf8str)
 }
 
 // Search for any emoji symbol, return true if found
-bool wstring_has_emoji(const LLWString& wstr)
+bool wstring_has_emoji(LLWStringView wstr)
 {
     for (const llwchar& wch : wstr)
     {
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h
index 61d698687a..123f4184b5 100644
--- a/indra/llcommon/llstring.h
+++ b/indra/llcommon/llstring.h
@@ -448,6 +448,7 @@ template<class T> std::string LLStringUtilBase<T>::sLocale;
 typedef LLStringUtilBase<char> LLStringUtil;
 typedef LLStringUtilBase<llwchar> LLWStringUtil;
 typedef std::basic_string<llwchar> LLWString;
+typedef std::basic_string_view<llwchar> LLWStringView;
 
 //@ Use this where we want to disallow input in the form of "foo"
 //  This is used to catch places where english text is embedded in the code
@@ -749,7 +750,7 @@ LL_COMMON_API llwchar utf8str_to_wchar(const std::string& utf8str, size_t offset
 
 LL_COMMON_API std::string utf8str_showBytesUTF8(const std::string& utf8str);
 
-LL_COMMON_API bool wstring_has_emoji(const LLWString& wstr);
+LL_COMMON_API bool wstring_has_emoji(LLWStringView wstr);
 
 LL_COMMON_API bool wstring_remove_emojis(LLWString& wstr);
 
diff --git a/indra/newview/llfloaterimsessiontab.cpp b/indra/newview/llfloaterimsessiontab.cpp
index 81a4f9936c..a0b56b14f0 100644
--- a/indra/newview/llfloaterimsessiontab.cpp
+++ b/indra/newview/llfloaterimsessiontab.cpp
@@ -587,13 +587,13 @@ void LLFloaterIMSessionTab::appendMessage(const LLChat& chat, const LLSD& args)
     mChatHistory->appendMessage(chat, chat_args);
 }
 
-void LLFloaterIMSessionTab::updateUsedEmojis(LLWString text)
+void LLFloaterIMSessionTab::updateUsedEmojis(LLWStringView text)
 {
     LLEmojiDictionary* dictionary = LLEmojiDictionary::getInstance();
     llassert_always(dictionary);
 
     bool emojiSent = false;
-    for (llwchar& c : text)
+    for (const llwchar& c : text)
     {
         if (dictionary->isEmoji(c))
         {
diff --git a/indra/newview/llfloaterimsessiontab.h b/indra/newview/llfloaterimsessiontab.h
index f1773520af..0a8502cfc5 100644
--- a/indra/newview/llfloaterimsessiontab.h
+++ b/indra/newview/llfloaterimsessiontab.h
@@ -147,7 +147,7 @@ protected:
     std::string appendTime();
     void assignResizeLimits();
 
-    void updateUsedEmojis(LLWString text);
+    void updateUsedEmojis(LLWStringView text);
 
     S32  mFloaterExtraWidth;
 
-- 
cgit v1.2.3


From 7e645bd42d6e9cf403c4b4b7d7eb070c5bb1ebf3 Mon Sep 17 00:00:00 2001
From: Ansariel <ansariel.hiller@phoenixviewer.com>
Date: Mon, 27 May 2024 10:47:54 +0200
Subject: Try copying the VC++ runtime files from the redistributable package
 first

---
 indra/cmake/Copy3rdPartyLibs.cmake | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

(limited to 'indra')

diff --git a/indra/cmake/Copy3rdPartyLibs.cmake b/indra/cmake/Copy3rdPartyLibs.cmake
index a2ce9eab90..30dee3c6c1 100644
--- a/indra/cmake/Copy3rdPartyLibs.cmake
+++ b/indra/cmake/Copy3rdPartyLibs.cmake
@@ -98,14 +98,27 @@ if(WINDOWS)
         set(MSVC_VER 120)
     elseif (MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1920) # Visual Studio 2017
         set(MSVC_VER 140)
+        set(MSVC_TOOLSET_VER 141)
     elseif (MSVC_VERSION GREATER_EQUAL 1920 AND MSVC_VERSION LESS 1930) # Visual Studio 2019
         set(MSVC_VER 140)
-    elseif (MSVC_VERSION GREATER_EQUAL 1930 AND MSVC_VERSION LESS 1940) # Visual Studio 2022
+        set(MSVC_TOOLSET_VER 142)
+    elseif (MSVC_VERSION GREATER_EQUAL 1930 AND MSVC_VERSION LESS 1950) # Visual Studio 2022
         set(MSVC_VER 140)
+        set(MSVC_TOOLSET_VER 143)
     else (MSVC80)
         MESSAGE(WARNING "New MSVC_VERSION ${MSVC_VERSION} of MSVC: adapt Copy3rdPartyLibs.cmake")
     endif (MSVC80)
 
+    if (MSVC_TOOLSET_VER AND DEFINED ENV{VCTOOLSREDISTDIR})
+        if(ADDRESS_SIZE EQUAL 32)
+            set(redist_find_path "$ENV{VCTOOLSREDISTDIR}x86\\Microsoft.VC${MSVC_TOOLSET_VER}.CRT")
+        else(ADDRESS_SIZE EQUAL 32)
+            set(redist_find_path "$ENV{VCTOOLSREDISTDIR}x64\\Microsoft.VC${MSVC_TOOLSET_VER}.CRT")
+        endif(ADDRESS_SIZE EQUAL 32)
+        get_filename_component(redist_path "${redist_find_path}" ABSOLUTE)
+        MESSAGE(STATUS "VC Runtime redist path: ${redist_path}")
+    endif (MSVC_TOOLSET_VER AND DEFINED ENV{VCTOOLSREDISTDIR})
+
     if(ADDRESS_SIZE EQUAL 32)
         # this folder contains the 32bit DLLs.. (yes really!)
         set(registry_find_path "[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Windows;Directory]/SysWOW64")
@@ -128,7 +141,14 @@ if(WINDOWS)
             vcruntime${MSVC_VER}.dll
             vcruntime${MSVC_VER}_1.dll
             )
-        if(EXISTS "${registry_path}/${release_msvc_file}")
+        if(redist_path AND EXISTS "${redist_path}/${release_msvc_file}")
+            MESSAGE(STATUS "Copying redist file from ${redist_path}/${release_msvc_file}")
+            to_staging_dirs(
+                ${redist_path}
+                third_party_targets
+                ${release_msvc_file})
+        elseif(EXISTS "${registry_path}/${release_msvc_file}")
+            MESSAGE(STATUS "Copying redist file from ${registry_path}/${release_msvc_file}")
             to_staging_dirs(
                 ${registry_path}
                 third_party_targets
-- 
cgit v1.2.3


From 08c483c2693153f49cfacb8cf517cf6067f53205 Mon Sep 17 00:00:00 2001
From: Andrey Kleshchev <andreykproductengine@lindenlab.com>
Date: Thu, 30 May 2024 13:46:22 +0300
Subject: viewer#1583 [Win] Viewer fails to detect more than 4Gb of video
 memory

---
 indra/llwindow/llwindowwin32.cpp | 163 ++++++++++-----------------------------
 1 file changed, 40 insertions(+), 123 deletions(-)

(limited to 'indra')

diff --git a/indra/llwindow/llwindowwin32.cpp b/indra/llwindow/llwindowwin32.cpp
index 7ea53816cf..92702b3ffa 100644
--- a/indra/llwindow/llwindowwin32.cpp
+++ b/indra/llwindow/llwindowwin32.cpp
@@ -361,14 +361,8 @@ struct LLWindowWin32::LLWindowWin32Thread : public LL::ThreadPool
         mGLReady = true;
     }
 
-    // initialzie DXGI adapter (for querying available VRAM)
-    void initDX();
-
-    // initialize D3D (if DXGI cannot be used)
-    void initD3D();
-
-    //clean up DXGI/D3D resources
-    void cleanupDX();
+    // Use DXGI to check memory (because WMI doesn't report more than 4Gb)
+    void checkDXMem();
 
     /// called by main thread to post work to this window thread
     template <typename CALLABLE>
@@ -417,12 +411,9 @@ struct LLWindowWin32::LLWindowWin32Thread : public LL::ThreadPool
     // *HACK: Attempt to prevent startup crashes by deferring memory accounting
     // until after some graphics setup. See SL-20177. -Cosmic,2023-09-18
     bool mGLReady = false;
+    bool mGotGLBuffer = false;
 
     U32 mMaxVRAM = 0; // maximum amount of vram to allow in the "budget", or 0 for no maximum (see updateVRAMUsage)
-
-    IDXGIAdapter3* mDXGIAdapter = nullptr;
-    LPDIRECT3D9 mD3D = nullptr;
-    LPDIRECT3DDEVICE9 mD3DDevice = nullptr;
 };
 
 
@@ -4631,39 +4622,55 @@ private:
     std::string mPrev;
 };
 
-// Print hardware debug info about available graphics adapters in ordinal order
-void debugEnumerateGraphicsAdapters()
+void LLWindowWin32::LLWindowWin32Thread::checkDXMem()
 {
-    LL_INFOS("Window") << "Enumerating graphics adapters..." << LL_ENDL;
+    if (!mGLReady || mGotGLBuffer) { return; }
+
+    IDXGIFactory4* p_factory = nullptr;
+
+    HRESULT res = CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**)&p_factory);
 
-    IDXGIFactory1* factory;
-    HRESULT res = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&factory);
-    if (FAILED(res) || !factory)
+    if (FAILED(res))
     {
         LL_WARNS() << "CreateDXGIFactory1 failed: 0x" << std::hex << res << LL_ENDL;
     }
     else
     {
+        IDXGIAdapter3* p_dxgi_adapter = nullptr;
         UINT graphics_adapter_index = 0;
-        IDXGIAdapter3* dxgi_adapter;
         while (true)
         {
-            res = factory->EnumAdapters(graphics_adapter_index, reinterpret_cast<IDXGIAdapter**>(&dxgi_adapter));
+            res = p_factory->EnumAdapters(graphics_adapter_index, reinterpret_cast<IDXGIAdapter**>(&p_dxgi_adapter));
             if (FAILED(res))
             {
                 if (graphics_adapter_index == 0)
                 {
                     LL_WARNS() << "EnumAdapters failed: 0x" << std::hex << res << LL_ENDL;
                 }
-                else
-                {
-                    LL_INFOS("Window") << "Done enumerating graphics adapters" << LL_ENDL;
-                }
             }
             else
             {
+                if (graphics_adapter_index == 0) // Should it check largest one isntead of first?
+                {
+                    DXGI_QUERY_VIDEO_MEMORY_INFO info;
+                    p_dxgi_adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &info);
+
+                    // Alternatively use GetDesc from below to get adapter's memory
+                    UINT64 budget_mb = info.Budget / (1024 * 1024);
+                    if (gGLManager.mVRAM < (S32)budget_mb)
+                    {
+                        gGLManager.mVRAM = (S32)budget_mb;
+                        LL_INFOS("RenderInit") << "New VRAM Budget (DX9): " << gGLManager.mVRAM << " MB" << LL_ENDL;
+                    }
+                    else
+                    {
+                        LL_INFOS("RenderInit") << "VRAM Budget (DX9): " << budget_mb
+                            << " MB, current (WMI): " << gGLManager.mVRAM << " MB" << LL_ENDL;
+                    }
+                }
+
                 DXGI_ADAPTER_DESC desc;
-                dxgi_adapter->GetDesc(&desc);
+                p_dxgi_adapter->GetDesc(&desc);
                 std::wstring description_w((wchar_t*)desc.Description);
                 std::string description(description_w.begin(), description_w.end());
                 LL_INFOS("Window") << "Graphics adapter index: " << graphics_adapter_index << ", "
@@ -4676,10 +4683,10 @@ void debugEnumerateGraphicsAdapters()
                     << "SharedSystemMemory: " << desc.SharedSystemMemory / 1024 / 1024 << LL_ENDL;
             }
 
-            if (dxgi_adapter)
+            if (p_dxgi_adapter)
             {
-                dxgi_adapter->Release();
-                dxgi_adapter = NULL;
+                p_dxgi_adapter->Release();
+                p_dxgi_adapter = NULL;
             }
             else
             {
@@ -4690,95 +4697,12 @@ void debugEnumerateGraphicsAdapters()
         }
     }
 
-    if (factory)
+    if (p_factory)
     {
-        factory->Release();
+        p_factory->Release();
     }
-}
-
-void LLWindowWin32::LLWindowWin32Thread::initDX()
-{
-    if (!mGLReady) { return; }
-
-    if (mDXGIAdapter == NULL)
-    {
-        debugEnumerateGraphicsAdapters();
 
-        IDXGIFactory4* pFactory = nullptr;
-
-        HRESULT res = CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**)&pFactory);
-
-        if (FAILED(res))
-        {
-            LL_WARNS() << "CreateDXGIFactory1 failed: 0x" << std::hex << res << LL_ENDL;
-        }
-        else
-        {
-            res = pFactory->EnumAdapters(0, reinterpret_cast<IDXGIAdapter**>(&mDXGIAdapter));
-            if (FAILED(res))
-            {
-                LL_WARNS() << "EnumAdapters failed: 0x" << std::hex << res << LL_ENDL;
-            }
-            else
-            {
-                LL_INFOS() << "EnumAdapters success" << LL_ENDL;
-            }
-        }
-
-        if (pFactory)
-        {
-            pFactory->Release();
-        }
-    }
-}
-
-void LLWindowWin32::LLWindowWin32Thread::initD3D()
-{
-    if (!mGLReady) { return; }
-
-    if (mDXGIAdapter == NULL && mD3DDevice == NULL && mWindowHandleThrd != 0)
-    {
-        mD3D = Direct3DCreate9(D3D_SDK_VERSION);
-
-        D3DPRESENT_PARAMETERS d3dpp;
-
-        ZeroMemory(&d3dpp, sizeof(d3dpp));
-        d3dpp.Windowed = TRUE;
-        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
-
-        HRESULT res = mD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mWindowHandleThrd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &mD3DDevice);
-
-        if (FAILED(res))
-        {
-            LL_WARNS() << "(fallback) CreateDevice failed: 0x" << std::hex << res << LL_ENDL;
-        }
-        else
-        {
-            LL_INFOS() << "(fallback) CreateDevice success" << LL_ENDL;
-        }
-    }
-}
-
-void LLWindowWin32::LLWindowWin32Thread::cleanupDX()
-{
-    //clean up DXGI/D3D resources
-    if (mDXGIAdapter)
-    {
-        mDXGIAdapter->Release();
-        mDXGIAdapter = nullptr;
-    }
-
-    if (mD3DDevice)
-    {
-        mD3DDevice->Release();
-        mD3DDevice = nullptr;
-    }
-
-    if (mD3D)
-    {
-        mD3D->Release();
-        mD3D = nullptr;
-    }
+    mGotGLBuffer = true;
 }
 
 void LLWindowWin32::LLWindowWin32Thread::run()
@@ -4798,15 +4722,11 @@ void LLWindowWin32::LLWindowWin32Thread::run()
     {
         LL_PROFILE_ZONE_SCOPED_CATEGORY_WIN32;
 
-        // lazily call initD3D inside this loop to catch when mGLReady has been set to true
-        initDX();
+        // Check memory budget using DirectX
+        checkDXMem();
 
         if (mWindowHandleThrd != 0)
         {
-            // lazily call initD3D inside this loop to catch when mWindowHandle has been set, and mGLReady has been set to true
-            // *TODO: Shutdown if this fails when mWindowHandle exists
-            initD3D();
-
             MSG msg;
             BOOL status;
             if (mhDCThrd == 0)
@@ -4847,8 +4767,6 @@ void LLWindowWin32::LLWindowWin32Thread::run()
         }
 #endif
     }
-
-    cleanupDX();
 }
 
 void LLWindowWin32::LLWindowWin32Thread::wakeAndDestroy()
@@ -4948,7 +4866,6 @@ void LLWindowWin32::LLWindowWin32Thread::wakeAndDestroy()
             // very unsafe
             TerminateThread(pair.second.native_handle(), 0);
             pair.second.detach();
-            cleanupDX();
         }
     }
     LL_DEBUGS("Window") << "thread pool shutdown complete" << LL_ENDL;
-- 
cgit v1.2.3