Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tracy profiler integration #365

Merged
merged 15 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ find_package(pystring CONFIG REQUIRED)
find_package(cppwinrt CONFIG REQUIRED)
find_package(unordered_dense CONFIG REQUIRED)
find_package(efsw CONFIG REQUIRED)
find_package(Tracy CONFIG REQUIRED)

set(NVAPI_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/extern/nvapi/" CACHE STRING "Path to NVAPI include headers/shaders" )
set(NVAPI_LIBRARY "${CMAKE_SOURCE_DIR}/extern/nvapi/amd64/nvapi64.lib" CACHE STRING "Path to NVAPI .lib file")
Expand Down Expand Up @@ -68,6 +69,7 @@ target_link_libraries(
pystring::pystring
unordered_dense::unordered_dense
efsw::efsw
Tracy::TracyClient
${NVAPI_LIBRARY}
)

Expand Down
2 changes: 2 additions & 0 deletions include/PCH.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void* operator new[](size_t size, size_t alignment, size_t alignmentOffset, cons
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define TRACY_ENABLE
Pentalimbed marked this conversation as resolved.
Show resolved Hide resolved

using namespace std::literals;

namespace stl
Expand Down
13 changes: 13 additions & 0 deletions src/Deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ void Deferred::SetupResources()

void Deferred::CopyShadowData()
{
ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "CopyShadowData");

auto& context = State::GetSingleton()->context;

ID3D11UnorderedAccessView* uavs[1]{ perShadow->uav.get() };
Expand Down Expand Up @@ -239,6 +242,9 @@ void Deferred::UpdateConstantBuffer()

void Deferred::PrepassPasses()
{
ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Prepass");

auto& shaderCache = SIE::ShaderCache::Instance();

if (!shaderCache.IsEnabled())
Expand Down Expand Up @@ -391,6 +397,9 @@ void Deferred::StartDeferred()

void Deferred::DeferredPasses()
{
ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Deferred");

auto renderer = RE::BSGraphics::Renderer::GetSingleton();
auto& context = State::GetSingleton()->context;

Expand Down Expand Up @@ -431,6 +440,8 @@ void Deferred::DeferredPasses()

// Ambient Composite
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Ambient Composite");

ID3D11Buffer* buffer = skylighting->loaded ? skylighting->skylightingCB->CB() : nullptr;
context->CSSetConstantBuffers(1, 1, &buffer);

Expand Down Expand Up @@ -469,6 +480,8 @@ void Deferred::DeferredPasses()

// Deferred Composite
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Deferred Composite");

ID3D11Buffer* buffer = skylighting->loaded ? skylighting->skylightingCB->CB() : nullptr;
context->CSSetConstantBuffers(1, 1, &buffer);

Expand Down
2 changes: 2 additions & 0 deletions src/Features/DynamicCubemaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ void DynamicCubemaps::Irradiance(bool a_reflections)

void DynamicCubemaps::UpdateCubemap()
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Cubemap Update");

switch (nextTask) {
case NextTask::kInferrence:
nextTask = NextTask::kIrradiance;
Expand Down
11 changes: 11 additions & 0 deletions src/Features/ScreenSpaceGI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)
return;
}

ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSGI");

static uint lastFrameGITexIdx = 0;
static uint lastFrameAccumTexIdx = 0;
uint inputGITexIdx = lastFrameGITexIdx;
Expand Down Expand Up @@ -675,6 +678,8 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)

// prefilter depths
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSGI - Prefilter Depths");

srvs.at(0) = renderer->GetDepthStencilData().depthStencils[RE::RENDER_TARGETS_DEPTHSTENCIL::kPOST_ZPREPASS_COPY].depthSRV;
for (int i = 0; i < 5; ++i)
uavs.at(i) = uavWorkingDepth[i].get();
Expand All @@ -687,6 +692,8 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)

// fetch radiance and disocclusion
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSGI - Radiance Disocc");

resetViews();
srvs.at(0) = rts[deferred->forwardRenderTargets[0]].SRV;
srvs.at(1) = texGI[inputGITexIdx]->srv.get();
Expand Down Expand Up @@ -714,6 +721,8 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)

// GI
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSGI - GI");

resetViews();
srvs.at(0) = texWorkingDepth->srv.get();
srvs.at(1) = rts[NORMALROUGHNESS].SRV;
Expand All @@ -738,6 +747,8 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)
// blur
if (settings.EnableBlur) {
for (uint i = 0; i < settings.BlurPasses; i++) {
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSGI - Blur");

resetViews();
srvs.at(0) = texGI[inputGITexIdx]->srv.get();
srvs.at(1) = texAccumFrames[lastFrameAccumTexIdx]->srv.get();
Expand Down
7 changes: 7 additions & 0 deletions src/Features/ScreenSpaceShadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ ID3D11ComputeShader* ScreenSpaceShadows::GetComputeRaymarchRight()

void ScreenSpaceShadows::DrawShadows()
{
ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Screen Space Shadows");

auto renderer = RE::BSGraphics::Renderer::GetSingleton();
auto context = State::GetSingleton()->context;

Expand Down Expand Up @@ -134,6 +137,8 @@ void ScreenSpaceShadows::DrawShadows()
float2 dynamicRes = { viewport->GetRuntimeData().dynamicResolutionWidthRatio, viewport->GetRuntimeData().dynamicResolutionHeightRatio };

for (int i = 0; i < dispatchList.DispatchCount; i++) {
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSS - Ray March");

auto dispatchData = dispatchList.Dispatch[i];

RaymarchCB data{};
Expand Down Expand Up @@ -174,6 +179,8 @@ void ScreenSpaceShadows::DrawShadows()
dispatchList = Bend::BuildDispatchList(lightProjectionRightF, viewportSize, minRenderBounds, maxRenderBounds);

for (int i = 0; i < dispatchList.DispatchCount; i++) {
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "SSS - Ray March (VR Right Eye)");

auto dispatchData = dispatchList.Dispatch[i];

RaymarchCB data{};
Expand Down
7 changes: 6 additions & 1 deletion src/Features/Skylighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ void Skylighting::CompileComputeShaders()

void Skylighting::Prepass()
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Skylighting - Update Probes");

auto& context = State::GetSingleton()->context;

{
Expand Down Expand Up @@ -649,7 +651,10 @@ void Skylighting::Main_Precipitation_RenderOcclusion::thunk()
Precipitation_SetupMask(precip); // Calling setup twice fixes an issue when it is raining

BSParticleShaderRainEmitter* rain = new BSParticleShaderRainEmitter;
Precipitation_RenderMask(precip, rain);
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Skylighting - Render Height Map");
Precipitation_RenderMask(precip, rain);
}
singleton->inOcclusion = false;
RE::BSParticleShaderCubeEmitter* cube = (RE::BSParticleShaderCubeEmitter*)rain;

Expand Down
7 changes: 7 additions & 0 deletions src/Features/SubsurfaceScattering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ void SubsurfaceScattering::DrawSSS()
if (!validMaterials)
return;

ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Subsurface Scattering");

validMaterials = false;

auto dispatchCount = Util::GetScreenDispatchCount();
Expand Down Expand Up @@ -204,6 +207,8 @@ void SubsurfaceScattering::DrawSSS()

// Horizontal pass to temporary texture
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Subsurface Scattering - Horizontal");

auto shader = GetComputeShaderHorizontalBlur();
context->CSSetShader(shader, nullptr, 0);

Expand All @@ -215,6 +220,8 @@ void SubsurfaceScattering::DrawSSS()

// Vertical pass to main texture
{
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Subsurface Scattering - Vertical");

views[0] = blurHorizontalTemp->srv.get();
context->CSSetShaderResources(0, 1, views);

Expand Down
3 changes: 3 additions & 0 deletions src/Features/TerrainOcclusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ void TerrainOcclusion::UpdateShadow()
if (!sunLight)
return;

ZoneScoped;
TracyD3D11Zone(State::GetSingleton()->tracyCtx, "Terrain Occlusion - Update Shadows");

/* ---- UPDATE CB ---- */
uint width = texNormalisedHeight->desc.Width;
uint height = texNormalisedHeight->desc.Height;
Expand Down
20 changes: 19 additions & 1 deletion src/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ HRESULT WINAPI hk_IDXGISwapChain_Present(IDXGISwapChain* This, UINT SyncInterval
{
State::GetSingleton()->Reset();
Menu::GetSingleton()->DrawOverlay();
return (This->*ptr_IDXGISwapChain_Present)(SyncInterval, Flags);
auto retval = (This->*ptr_IDXGISwapChain_Present)(SyncInterval, Flags);
TracyD3D11Collect(State::GetSingleton()->tracyCtx);
return retval;
}

void hk_BSGraphics_SetDirtyStates(bool isCompute);
Expand Down Expand Up @@ -443,6 +445,18 @@ namespace Hooks
static inline REL::Relocation<decltype(thunk)> func;
};

#ifdef TRACY_ENABLE
struct Main_Update
{
static void thunk(RE::Main* a_this, float a2)
{
func(a_this, a2);
FrameMark;
}
static inline REL::Relocation<decltype(thunk)> func;
};
#endif

void Install()
{
SKSE::AllocTrampoline(14);
Expand Down Expand Up @@ -482,5 +496,9 @@ namespace Hooks
stl::write_thunk_call<CreateRenderTarget_Snow>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x406, 0x409));
stl::write_thunk_call<CreateDepthStencil_PrecipitationMask>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x1245, 0x123B, 0x1917));
stl::write_thunk_call<CreateCubemapRenderTarget_Reflections>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0xA25, 0xA25, 0xCD2));

#ifdef TRACY_ENABLE
stl::write_thunk_call<Main_Update>(REL::RelocationID(35551, 36544).address() + REL::Relocate(0x11F, 0x160));
#endif
}
}
2 changes: 2 additions & 0 deletions src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ void State::SetupResources()
context = reinterpret_cast<ID3D11DeviceContext*>(renderer->GetRuntimeData().context);
device = reinterpret_cast<ID3D11Device*>(renderer->GetRuntimeData().forwarder);
context->QueryInterface(__uuidof(pPerf), reinterpret_cast<void**>(&pPerf));

tracyCtx = TracyD3D11Context(device, context);
}

void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescriptor, uint& a_pixelDescriptor, bool a_forceDeferred)
Expand Down
11 changes: 11 additions & 0 deletions src/State.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <Tracy/Tracy.hpp>
Pentalimbed marked this conversation as resolved.
Show resolved Hide resolved
#include <Tracy/TracyD3D11.hpp>

#include <Buffer.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
Expand Down Expand Up @@ -138,6 +141,14 @@ class State
ID3D11DeviceContext* context = nullptr;
ID3D11Device* device = nullptr;

TracyD3D11Ctx tracyCtx = nullptr; // Tracy context

inline ~State()
{
if (tracyCtx)
TracyD3D11Destroy(tracyCtx);
}

private:
std::shared_ptr<REX::W32::ID3DUserDefinedAnnotation> pPerf;
bool initialized = false;
Expand Down
6 changes: 5 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
"eastl",
"clib-util",
"unordered-dense",
"efsw"
"efsw",
{
"name": "tracy",
"version>=": "0.11.0"
FlayaN marked this conversation as resolved.
Show resolved Hide resolved
}
],
"overrides": [
{
Expand Down
Loading