From 4c907c35300cf40cb0c663666816593524c589b1 Mon Sep 17 00:00:00 2001 From: Bari <58800830+TheBarii@users.noreply.github.com> Date: Mon, 5 Aug 2024 13:36:45 +0600 Subject: [PATCH 1/2] Massive FPS boost, but gaussian blur looks like box blur --- CMakeSettings.json | 2 +- src/Client/GUI/Engine/Effects/Blur/blur.cpp | 51 +++---------------- src/Client/GUI/Engine/Engine.cpp | 18 +++---- .../Hook/Hooks/Render/SwapchainHook.cpp | 36 +++++++------ .../Hook/Hooks/Render/SwapchainHook.hpp | 1 + .../Modules/ArmorHUD/ArmorHUDListener.hpp | 1 + .../InventoryHUD/InventoryHUDListener.hpp | 2 + .../Modules/MotionBlur/MotionBlurListener.hpp | 3 +- .../MovableChat/MovableChatListener.hpp | 2 + .../Module/Modules/PaperDoll/DollListener.hpp | 4 +- 10 files changed, 42 insertions(+), 78 deletions(-) diff --git a/CMakeSettings.json b/CMakeSettings.json index d2bcfced..b81ead14 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -14,7 +14,7 @@ { "name": "x64-Release", "generator": "Ninja", - "configurationType": "Release", + "configurationType": "RelWithDebInfo", "buildRoot": "${projectDir}\\out\\build\\${name}", "installRoot": "${projectDir}\\out\\install\\${name}", "cmakeCommandArgs": "", diff --git a/src/Client/GUI/Engine/Effects/Blur/blur.cpp b/src/Client/GUI/Engine/Effects/Blur/blur.cpp index 9ad7dffe..d2979ea5 100644 --- a/src/Client/GUI/Engine/Effects/Blur/blur.cpp +++ b/src/Client/GUI/Engine/Effects/Blur/blur.cpp @@ -294,6 +294,8 @@ void Blur::RenderToRTV(ID3D11RenderTargetView *pRenderTargetView, ID3D11ShaderRe void Blur::RenderBlur(ID3D11RenderTargetView *pDstRenderTargetView, int iterations, float intensity) { + if(intensity < 1) return; + if (!SwapchainHook::GetBackbuffer()) return; ID3D11ShaderResourceView *pOrigShaderResourceView = MotionBlurListener::BackbufferToSRV(); @@ -319,56 +321,15 @@ void Blur::RenderBlur(ID3D11RenderTargetView *pDstRenderTargetView, int iteratio desc.BindFlags |= D3D11_BIND_RENDER_TARGET; - for (int i = 0; i <= iterations; i++) - { - ID3D11Texture2D *pFrameBuffer; - ID3D11RenderTargetView *pRenderTargetView; - ID3D11ShaderResourceView *pShaderResourceView; - - SwapchainHook::d3d11Device->CreateTexture2D(&desc, nullptr, &pFrameBuffer); - if (i == 0) - pRenderTargetView = pDstRenderTargetView; - else - SwapchainHook::d3d11Device->CreateRenderTargetView(pFrameBuffer, nullptr, &pRenderTargetView); - SwapchainHook::d3d11Device->CreateShaderResourceView(pFrameBuffer, nullptr, &pShaderResourceView); - - framebuffers.push_back(pFrameBuffer); - renderTargetViews.push_back(pRenderTargetView); - shaderResourceViews.push_back(pShaderResourceView); - fbSizes.push_back(XMFLOAT2(desc.Width, desc.Height)); - - desc.Width /= 2; - desc.Height /= 2; - } - - constantBuffer.offset = XMFLOAT2(intensity, intensity); - pContext->PSSetShader(pDownsampleShader, nullptr, 0); - RenderToRTV(renderTargetViews[1], pOrigShaderResourceView, fbSizes[1]); + XMFLOAT2 fbSize = XMFLOAT2(desc.Width, desc.Height); - for (int i = 1; i < iterations; i++) - { - RenderToRTV(renderTargetViews[i + 1], shaderResourceViews[i], fbSizes[i + 1]); - } + constantBuffer.offset = XMFLOAT2(intensity * 4, intensity * 4); pContext->PSSetShader(pUpsampleShader, nullptr, 0); + RenderToRTV(pDstRenderTargetView, pOrigShaderResourceView, fbSize); + - for (int i = iterations; i > 0; i--) - { - RenderToRTV(renderTargetViews[i - 1], shaderResourceViews[i], fbSizes[i - 1]); - } - for (int i = 0; i < iterations; i++) - { - if (i != 0) - renderTargetViews[i]->Release(); - framebuffers[i]->Release(); - shaderResourceViews[i]->Release(); - - renderTargetViews.clear(); - framebuffers.clear(); - shaderResourceViews.clear(); - fbSizes.clear(); - } pContext->Release(); pOrigShaderResourceView->Release(); diff --git a/src/Client/GUI/Engine/Engine.cpp b/src/Client/GUI/Engine/Engine.cpp index a52f663e..0e375c3c 100644 --- a/src/Client/GUI/Engine/Engine.cpp +++ b/src/Client/GUI/Engine/Engine.cpp @@ -765,7 +765,6 @@ std::string FlarialGUI::FlarialTextWithFont(float x, float y, const wchar_t *tex //std::cout << weightedName << std::endl; - float sizeMultiplier = 1.0f; if(hasEnding(weightedName, "2.0")) sizeMultiplier = 0.6f; @@ -773,7 +772,9 @@ std::string FlarialGUI::FlarialTextWithFont(float x, float y, const wchar_t *tex float fSize = (fontSize / 135) * sizeMultiplier; ImGui::SetWindowFontScale(fSize); - + std::string stringText = WideToNarrow(text).c_str(); + ImVec2 size = ImGui::CalcTextSize(stringText.c_str()); + std::string fontedName = weightedName + std::to_string(fSize); switch (alignment) { @@ -781,23 +782,22 @@ std::string FlarialGUI::FlarialTextWithFont(float x, float y, const wchar_t *tex break; case DWRITE_TEXT_ALIGNMENT_CENTER: { - x += (width / 2) - (ImGui::CalcTextSize(WideToNarrow(text).c_str()).x / 2); + x += (width / 2) - (size.x / 2); break; } case DWRITE_TEXT_ALIGNMENT_TRAILING: { - x += (width - ImGui::CalcTextSize(WideToNarrow(text).c_str()).x); + x += (width - size.x); break; } } - TextSizes[weightedName + std::to_string(fSize)] = ImGui::CalcTextSize(WideToNarrow(text).c_str()).x; - - y += (height / 2) - (ImGui::CalcTextSize(WideToNarrow(text).c_str()).y / 2); - ImGui::GetBackgroundDrawList()->AddText(ImVec2(x, y), ImColor(color.r, color.g, color.b, color.a), WideToNarrow(text).c_str()); + TextSizes[fontedName] = size.x; + y += (height / 2) - (size.y / 2); + ImGui::GetBackgroundDrawList()->AddText(ImVec2(x, y), ImColor(color.r, color.g, color.b, color.a), stringText.c_str()); ImGui::PopFont(); - return weightedName + std::to_string(fSize); + return fontedName; } void FlarialGUI::LoadFont(int resourceId) { diff --git a/src/Client/Hook/Hooks/Render/SwapchainHook.cpp b/src/Client/Hook/Hooks/Render/SwapchainHook.cpp index 8a36c131..1af1ea96 100644 --- a/src/Client/Hook/Hooks/Render/SwapchainHook.cpp +++ b/src/Client/Hook/Hooks/Render/SwapchainHook.cpp @@ -620,7 +620,7 @@ ID3D11Texture2D* SwapchainHook::GetBackbuffer() void SwapchainHook::SaveBackbuffer() { - Memory::SafeRelease(SavedD3D11BackBuffer); + if(SwapchainHook::queue) return; ID3D11DeviceContext* deviceContext; SwapchainHook::d3d11Device->GetImmediateContext(&deviceContext); @@ -633,18 +633,18 @@ ID3D11Texture2D* SwapchainHook::GetBackbuffer() D3D11_TEXTURE2D_DESC desc; buffer2D->GetDesc(&desc); - - ID3D11Texture2D* stageTex = nullptr; - D3D11_TEXTURE2D_DESC stageDesc = desc; - stageDesc.Usage = D3D11_USAGE_STAGING; - stageDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; - stageDesc.BindFlags = 0; - - //desc.Usage = DXGI_USAGE_SHADER_INPUT; - HRESULT r = SwapchainHook::d3d11Device->CreateTexture2D(&stageDesc, nullptr, &stageTex); + HRESULT r; + + if(!stageTex) { + D3D11_TEXTURE2D_DESC stageDesc = desc; + stageDesc.Usage = D3D11_USAGE_STAGING; + stageDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + stageDesc.BindFlags = 0; + r = SwapchainHook::d3d11Device->CreateTexture2D(&stageDesc, nullptr, &stageTex); + if (FAILED(r)) std::cout << "Failed to create stage texture: " << std::hex << r << std::endl; + } deviceContext->CopyResource(stageTex, buffer2D); - if (FAILED(r)) std::cout << "Failed to create stage texture: " << std::hex << r << std::endl; D3D11_TEXTURE2D_DESC defaultDesc = desc; @@ -652,18 +652,16 @@ ID3D11Texture2D* SwapchainHook::GetBackbuffer() defaultDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; defaultDesc.CPUAccessFlags = 0; - ID3D11Texture2D* defaultTexture = nullptr; - hr = SwapchainHook::d3d11Device->CreateTexture2D(&defaultDesc, nullptr, &defaultTexture); - if (FAILED(hr)) { - std::cout << "Failed to create def texture: " << std::hex << r << std::endl; + if(!SavedD3D11BackBuffer) { + hr = SwapchainHook::d3d11Device->CreateTexture2D(&defaultDesc, nullptr, &SavedD3D11BackBuffer); + if (FAILED(hr)) { + std::cout << "Failed to create def texture: " << std::hex << r << std::endl; + } } - deviceContext->CopyResource(defaultTexture, stageTex); + deviceContext->CopyResource(SavedD3D11BackBuffer, stageTex); - stageTex->Release(); Memory::SafeRelease(backBuffer); Memory::SafeRelease(buffer2D); Memory::SafeRelease(deviceContext); - - SavedD3D11BackBuffer = defaultTexture; } diff --git a/src/Client/Hook/Hooks/Render/SwapchainHook.hpp b/src/Client/Hook/Hooks/Render/SwapchainHook.hpp index 4f6cbc66..f60f2c15 100644 --- a/src/Client/Hook/Hooks/Render/SwapchainHook.hpp +++ b/src/Client/Hook/Hooks/Render/SwapchainHook.hpp @@ -59,6 +59,7 @@ class SwapchainHook : public Hook { static inline ID3D12GraphicsCommandList* DX12CommandLists; static bool hasResized; static int currentBitmap; + static inline ID3D11Texture2D* stageTex; static inline ID3D12Device5* d3d12Device5 = nullptr; diff --git a/src/Client/Module/Modules/ArmorHUD/ArmorHUDListener.hpp b/src/Client/Module/Modules/ArmorHUD/ArmorHUDListener.hpp index 9db2e476..aa565665 100644 --- a/src/Client/Module/Modules/ArmorHUD/ArmorHUDListener.hpp +++ b/src/Client/Module/Modules/ArmorHUD/ArmorHUDListener.hpp @@ -226,6 +226,7 @@ class ArmorHUDListener : public Listener { } void onSetupAndRender(SetupAndRenderEvent &event) override { + if(this->module->isEnabled()) if (ClientInstance::getTopScreenName() == "hud_screen") { auto muirc = event.getMuirc(); BaseActorRenderContext barc(muirc->screenContext, muirc->clientInstance, diff --git a/src/Client/Module/Modules/InventoryHUD/InventoryHUDListener.hpp b/src/Client/Module/Modules/InventoryHUD/InventoryHUDListener.hpp index da0b2515..a74d27af 100644 --- a/src/Client/Module/Modules/InventoryHUD/InventoryHUDListener.hpp +++ b/src/Client/Module/Modules/InventoryHUD/InventoryHUDListener.hpp @@ -86,6 +86,8 @@ class InventoryHUDListener : public Listener { } void onSetupAndRender(SetupAndRenderEvent &event) override { + + if(this->module->isEnabled()) if (ClientInstance::getTopScreenName() == "hud_screen") { auto muirc = event.getMuirc(); BaseActorRenderContext barc(muirc->screenContext, muirc->clientInstance, diff --git a/src/Client/Module/Modules/MotionBlur/MotionBlurListener.hpp b/src/Client/Module/Modules/MotionBlur/MotionBlurListener.hpp index 5058f49c..821349a9 100644 --- a/src/Client/Module/Modules/MotionBlur/MotionBlurListener.hpp +++ b/src/Client/Module/Modules/MotionBlur/MotionBlurListener.hpp @@ -263,8 +263,7 @@ void InitializeRenderResources(ID3D11Device* device) void RenderSRV(ID3D11ShaderResourceView* srv, float width, float height, float opacity) { - ID3D11DeviceContext* context; - SwapchainHook::d3d11Device->GetImmediateContext(&context); + ID3D11DeviceContext* context = SwapchainHook::context; context->OMSetRenderTargets(1, &gRTV, gDSV); diff --git a/src/Client/Module/Modules/MovableChat/MovableChatListener.hpp b/src/Client/Module/Modules/MovableChat/MovableChatListener.hpp index 2ffcc8a5..c3b82ee7 100644 --- a/src/Client/Module/Modules/MovableChat/MovableChatListener.hpp +++ b/src/Client/Module/Modules/MovableChat/MovableChatListener.hpp @@ -91,6 +91,8 @@ class MovableChatListener : public Listener { void onSetupAndRender(SetupAndRenderEvent &event) override { + if(this->module->isEnabled()) + if (SDK::screenView->VisualTree->root->LayerName == "hud_screen") { diff --git a/src/Client/Module/Modules/PaperDoll/DollListener.hpp b/src/Client/Module/Modules/PaperDoll/DollListener.hpp index b546bb3c..476d9c64 100644 --- a/src/Client/Module/Modules/PaperDoll/DollListener.hpp +++ b/src/Client/Module/Modules/PaperDoll/DollListener.hpp @@ -75,8 +75,9 @@ class DollListener : public Listener { } void onSetupAndRender(SetupAndRenderEvent &event) override { - if(SDK::currentScreen == "hud_screen") { + if(this->module->isEnabled()) + if(SDK::currentScreen == "hud_screen") { SDK::screenView->VisualTree->root->forEachControl([this](std::shared_ptr& control) { if (control->LayerName == "hud_player") { @@ -101,7 +102,6 @@ class DollListener : public Listener { return; // dont go through other controls } - }); } } From c7183dca16757c868594fce89baa5bfd9fed180b Mon Sep 17 00:00:00 2001 From: Bari <58800830+TheBarii@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:27:25 +0600 Subject: [PATCH 2/2] tes --- src/Client/GUI/Engine/Effects/Blur/blur.cpp | 72 ++++++++++----------- src/Client/GUI/Engine/Engine.hpp | 5 +- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Client/GUI/Engine/Effects/Blur/blur.cpp b/src/Client/GUI/Engine/Effects/Blur/blur.cpp index d2979ea5..c8feec01 100644 --- a/src/Client/GUI/Engine/Effects/Blur/blur.cpp +++ b/src/Client/GUI/Engine/Effects/Blur/blur.cpp @@ -78,54 +78,53 @@ float4 main(float4 screenSpace : SV_Position) : SV_TARGET const char *upsampleShaderSrc = R"( cbuffer BlurInputBuffer : register(b0) { + float intensity; float2 resolution; - float2 offset; - float2 halfPixel; }; sampler sampler0 : register(s0); -Texture2D texture0 : register(t0); +Texture2D inputTexture : register(t0); float4 main(float4 screenSpace : SV_Position) : SV_TARGET { - float2 uv = screenSpace.xy / resolution; - float4 colorSum = float4(0.0, 0.0, 0.0, 0.0); - - static const float2 offsets[9] = { - float2(-1.0, -1.0) * halfPixel * offset, - float2(0.0, -1.0) * halfPixel * offset, - float2(1.0, -1.0) * halfPixel * offset, - float2(-1.0, 0.0) * halfPixel * offset, - float2(0.0, 0.0) * halfPixel * offset, - float2(1.0, 0.0) * halfPixel * offset, - float2(-1.0, 1.0) * halfPixel * offset, - float2(0.0, 1.0) * halfPixel * offset, - float2(1.0, 1.0) * halfPixel * offset - }; - - static const float weights[9] = { - 0.06136, 0.12245, 0.06136, - 0.12245, 0.24477, 0.12245, - 0.06136, 0.12245, 0.06136 + float2 texSize = resolution; + float2 texelSize = 1.0f / texSize; + float2 texCoord = screenSpace.xy / texSize; + + // Scale offsets with a larger factor for more noticeable blur + float2 offsets[9] = { + float2(-1.0f, -1.0f), float2(0.0f, -1.0f), float2(1.0f, -1.0f), + float2(-1.0f, 0.0f), float2(0.0f, 0.0f), float2(1.0f, 0.0f), + float2(-1.0f, 1.0f), float2(0.0f, 1.0f), float2(1.0f, 1.0f) }; - float weightSum = 0.0; + float weights[9]; + float sum = 0.0f; + float sigma = intensity * 2.0f; // Scale intensity for more noticeable blur for (int i = 0; i < 9; i++) { - weightSum += weights[i]; + float2 offset = offsets[i] * texelSize * sigma; + weights[i] = exp(-dot(offset, offset) / (2.0f * sigma * sigma)); + sum += weights[i]; } + float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f); for (int i = 0; i < 9; i++) { - colorSum += texture0.Sample(sampler0, uv + offsets[i]) * (weights[i] / weightSum); + float2 offset = offsets[i] * texelSize * sigma; + color += inputTexture.Sample(sampler0, texCoord + offset) * weights[i]; } - return colorSum; + color /= sum; + + return color; } + )"; + const char *dbgDrawTextureShaderSrc = "cbuffer BlurInputBuffer : register(b0)\ {\ float2 resolution;\ @@ -146,20 +145,25 @@ float4 main(PS_INPUT input, float4 screenSpace : SV_Position) : SV_TARGET {\ ID3DBlob *TryCompileShader(const char *pSrcData, const char *pTarget) { HRESULT hr; + ID3DBlob *shaderBlob = nullptr; + ID3DBlob *errorBlob = nullptr; - ID3DBlob *shaderBlob; - ID3DBlob *errorBlob; hr = D3DCompile(pSrcData, strlen(pSrcData), nullptr, nullptr, nullptr, "main", pTarget, 0, 0, &shaderBlob, &errorBlob); if (FAILED(hr)) { - Logger::error("[Blur] Failed to compile shader"); - errorBlob->Release(); + if (errorBlob) + { + Logger::error(std::format("[Blur] Failed to compile shader: {}", static_cast(errorBlob->GetBufferPointer()))); + errorBlob->Release(); + } throw std::logic_error("Failed to compile shader!"); } + return shaderBlob; } + ID3D11PixelShader *dbgShader; void Blur::InitializePipeline() @@ -232,8 +236,6 @@ void Blur::RenderToRTV(ID3D11RenderTargetView *pRenderTargetView, ID3D11ShaderRe pContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView **)&null); pContext->OMSetRenderTargets(1, &pRenderTargetView, nullptr); - constantBuffer.resolution = rtvSize; - constantBuffer.halfpixel = XMFLOAT2(0.5 / rtvSize.x, 0.5 / rtvSize.y); pContext->UpdateSubresource(pConstantBuffer, 0, nullptr, &constantBuffer, 0, 0); pContext->IASetInputLayout(pInputLayout); @@ -323,14 +325,12 @@ void Blur::RenderBlur(ID3D11RenderTargetView *pDstRenderTargetView, int iteratio XMFLOAT2 fbSize = XMFLOAT2(desc.Width, desc.Height); - constantBuffer.offset = XMFLOAT2(intensity * 4, intensity * 4); + constantBuffer.intensity = intensity; + constantBuffer.resolution = fbSize; pContext->PSSetShader(pUpsampleShader, nullptr, 0); RenderToRTV(pDstRenderTargetView, pOrigShaderResourceView, fbSize); - - - pContext->Release(); pOrigShaderResourceView->Release(); } diff --git a/src/Client/GUI/Engine/Engine.hpp b/src/Client/GUI/Engine/Engine.hpp index 6ee3216a..9cb33a3c 100644 --- a/src/Client/GUI/Engine/Engine.hpp +++ b/src/Client/GUI/Engine/Engine.hpp @@ -27,10 +27,9 @@ using namespace DirectX; struct BlurInputBuffer { + FLOAT intensity; XMFLOAT2 resolution; - XMFLOAT2 offset; - XMFLOAT2 halfpixel; - XMFLOAT2 _dummy; + FLOAT padding[1]; };