Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Jetbrain IDEs
.idea/

# Visual Studio Code
.vscode/

# Visual Studio
*.sln*
*.vcxproj*
Expand Down
2 changes: 2 additions & 0 deletions Source/Runtime/Engine/Private/Renderwerk/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Renderwerk/Core/Misc/Guid.hpp"
#include "Renderwerk/Engine/EngineModule.hpp"
#include "Renderwerk/Job/JobModule.hpp"
#include "Renderwerk/Platform/WindowModule.hpp"
#include "Renderwerk/Profiler/Profiler.hpp"
#include "Renderwerk/Renderer/RendererModule.hpp"
Expand Down Expand Up @@ -47,6 +48,7 @@ void FEngine::Run()

RegisterModule<FWindowModule>();
RegisterModule<FRendererModule>();
RegisterModule<FJobModule>();

MainThread = NewOwned<FMainThread>(&bShouldRun);
RenderThread = NewOwned<FRenderThread>(&bShouldRun);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IEngineThread::IEngineThread(FString InName, TAtomic<bool8>* InShouldRun, const
{
if (!bUseExistingThread)
{
Thread = NewOwned<FThread>([&]()
Thread = NewOwned<FThread>([&]([[maybe_unused]] void* UserData)
{
const FAnsiString ConvertedName = FStringUtilities::ConvertToAnsi(Name);
const FAnsiChar* ThreadName = ConvertedName.c_str();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ void FMainThread::Initialize()

void FMainThread::OnTick()
{
PROFILE_FUNCTION();

const TRef<FEngine> Engine = GetEngine();
Engine->GetMainThreadTickDelegate().Broadcast();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void FRenderThread::Initialize()

void FRenderThread::OnTick()
{
PROFILE_FUNCTION();
GetEngine()->GetRenderThreadTickDelegate().Broadcast();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void FUpdateThread::Initialize()

void FUpdateThread::OnTick()
{
PROFILE_FUNCTION();
GetEngine()->GetUpdateThreadTickDelegate().Broadcast();
}

Expand Down
18 changes: 17 additions & 1 deletion Source/Runtime/Engine/Private/Renderwerk/Graphics/GfxDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ FGfxDevice::FGfxDevice(FGfxAdapter* InGfxAdapter, const FGfxDeviceDesc& InDevice
CopyQueue = CreateInternalCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);

GraphicsWorkFence = CreateFence();
CopyWorkFence = CreateFence();

FGfxDescriptorHeapDesc RTVHeapDesc;
RTVHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
Expand All @@ -52,12 +53,12 @@ FGfxDevice::FGfxDevice(FGfxAdapter* InGfxAdapter, const FGfxDeviceDesc& InDevice
FGfxDevice::~FGfxDevice()
{
ShaderCompiler.reset();
ResourceManager->ReleaseUploadRequests();
ResourceManager.reset();

SRVDescriptorHeap.reset();
RTVDescriptorHeap.reset();

CopyWorkFence.reset();
GraphicsWorkFence.reset();

CopyQueue.Reset();
Expand All @@ -73,6 +74,12 @@ void FGfxDevice::FlushGraphicsQueue() const
RW_VERIFY(GraphicsWorkFence->Wait());
}

void FGfxDevice::FlushCopyQueue() const
{
CopyWorkFence->SignalCommandQueue(CopyQueue);
RW_VERIFY(CopyWorkFence->Wait());
}

void FGfxDevice::SubmitGraphicsWork(const TRef<FGfxCommandList>& CommandList) const
{
const TArray CommandLists = {
Expand All @@ -82,6 +89,15 @@ void FGfxDevice::SubmitGraphicsWork(const TRef<FGfxCommandList>& CommandList) co
GraphicsQueue->ExecuteCommandLists(static_cast<uint32>(CommandLists.size()), CommandLists.data());
}

void FGfxDevice::SubmitCopyWork(const TRef<FGfxCommandList>& CommandList) const
{
const TArray CommandLists = {
CommandList->GetNativeObject<ID3D12CommandList>(NativeObjectIds::D3D12_CommandList),
};
// ReSharper disable once CppRedundantCastExpression
CopyQueue->ExecuteCommandLists(static_cast<uint32>(CommandLists.size()), CommandLists.data());
}

TRef<FGfxDescriptorHeap> FGfxDevice::CreateDescriptorHeap(const FGfxDescriptorHeapDesc& HeapDesc, const FStringView& DebugName)
{
return NewRef<FGfxDescriptorHeap>(this, HeapDesc, DebugName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,63 @@

#include "Renderwerk/Graphics/GfxResourceManager.hpp"

#include "Renderwerk/Engine/Engine.hpp"
#include "Renderwerk/Graphics/GfxBuffer.hpp"
#include "Renderwerk/Graphics/GfxCommandList.hpp"
#include "Renderwerk/Graphics/GfxDevice.hpp"
#include "Renderwerk/Graphics/GfxTexture.hpp"
#include "Renderwerk/Job/JobModule.hpp"
#include "Renderwerk/Platform/Threading/ScopedLock.hpp"
#include "Renderwerk/Profiler/Profiler.hpp"

FGfxUploadJob::FGfxUploadJob(FGfxDevice* InGfxDevice, const TVector<FGfxUploadRequest>& InUploadRequests)
: GfxDevice(InGfxDevice), UploadRequests(InUploadRequests)
{
}

void FGfxUploadJob::Execute()
{
const TRef<FGfxCommandList> CommandList = GfxDevice->CreateCommandList(D3D12_COMMAND_LIST_TYPE_COPY);
CommandList->Open();
{
PROFILE_SCOPE("UploadResources");
if (UploadRequests.empty())
{
return;
}
ID3D12GraphicsCommandList10* NativeCommandList = CommandList->GetNativeObject<ID3D12GraphicsCommandList10>(NativeObjectIds::D3D12_CommandList);
for (const FGfxUploadRequest& UploadRequest : UploadRequests)
{
RW_LOG(Debug, "Processing upload request for '{}'", UploadRequest.Resource->GetDebugName());
switch (UploadRequest.Type)
{
case EGfxUploadRequestType::Buffer:
{
ID3D12Resource2* NativeUploadResource = UploadRequest.Resource->GetNativeObject<ID3D12Resource2>(NativeObjectIds::D3D12_Resource);
ID3D12Resource2* NativeStagingResource = UploadRequest.StagingBuffer->GetNativeObject<ID3D12Resource2>(NativeObjectIds::D3D12_Resource);
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_COPY_DEST);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE);
NativeCommandList->CopyResource(NativeUploadResource, NativeStagingResource);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COMMON);
}
break;
case EGfxUploadRequestType::Texture:
{
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_COPY_DEST);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE);
CommandList->CopyBufferToTexture(UploadRequest.Resource, UploadRequest.StagingBuffer);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COMMON);
}
break;
}
}
}
CommandList->Close();
GfxDevice->SubmitCopyWork(CommandList);
GfxDevice->FlushCopyQueue();
UploadRequests.clear();
}

FGfxResourceManager::FGfxResourceManager(FGfxDevice* InGfxDevice, const FGfxResourceManagerDesc& InResourceManagerDesc)
: FGfxResourceManager(InGfxDevice, InResourceManagerDesc, TEXT("UnnamedResourceManager"))
{
Expand All @@ -22,7 +72,9 @@ FGfxResourceManager::FGfxResourceManager(FGfxDevice* InGfxDevice, const FGfxReso

FGfxResourceManager::~FGfxResourceManager()
{
UploadRequests.clear();
Buffers.clear();
BufferPool.Destroy();
Textures.clear();
TexturePool.Destroy();
}
Expand Down Expand Up @@ -88,6 +140,13 @@ void FGfxResourceManager::CollectResourceUploads()
QueueBufferUpload(Buffer);
}
}

if (!UploadRequests.empty())
{
const TRef<FJobModule> JobModule = GetEngine()->GetModule<FJobModule>();
JobModule->QueueJob(NewRef<FGfxUploadJob>(GfxDevice, UploadRequests));
UploadRequests.clear();
}
}

void FGfxResourceManager::QueueTextureUpload(const TRef<FGfxTexture>& Texture)
Expand Down Expand Up @@ -141,46 +200,3 @@ void FGfxResourceManager::QueueBufferUpload(const TRef<FGfxBuffer>& Buffer)
UploadRequests.push_back(Request);
Buffer->ResetDirtyState();
}

void FGfxResourceManager::FlushUploadRequests(const TRef<FGfxCommandList>& CommandList)
{
PROFILE_FUNCTION();
FScopedLock Lock(&RequestSection);
if (UploadRequests.empty())
{
return;
}
ID3D12GraphicsCommandList10* NativeCommandList = CommandList->GetNativeObject<ID3D12GraphicsCommandList10>(NativeObjectIds::D3D12_CommandList);
for (const FGfxUploadRequest& UploadRequest : UploadRequests)
{
RW_LOG(Debug, "Processing upload request for '{}'", UploadRequest.Resource->GetDebugName());
switch (UploadRequest.Type)
{
case EGfxUploadRequestType::Buffer:
{
ID3D12Resource2* NativeUploadResource = UploadRequest.Resource->GetNativeObject<ID3D12Resource2>(NativeObjectIds::D3D12_Resource);
ID3D12Resource2* NativeStagingResource = UploadRequest.StagingBuffer->GetNativeObject<ID3D12Resource2>(NativeObjectIds::D3D12_Resource);
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_COPY_DEST);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE);
NativeCommandList->CopyResource(NativeUploadResource, NativeStagingResource);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COMMON);
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
}
break;
case EGfxUploadRequestType::Texture:
{
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_COPY_DEST);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE);
CommandList->CopyBufferToTexture(UploadRequest.Resource, UploadRequest.StagingBuffer);
CommandList->ResourceBarrier(UploadRequest.StagingBuffer, D3D12_RESOURCE_STATE_COMMON);
CommandList->ResourceBarrier(UploadRequest.Resource, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE);
}
break;
}
}
}

void FGfxResourceManager::ReleaseUploadRequests()
{
UploadRequests.clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

#include "imgui.h"

#include "Renderwerk/Engine/Engine.hpp"
#include "Renderwerk/Graphics/GfxCommandList.hpp"
#include "Renderwerk/Graphics/GfxDevice.hpp"
#include "Renderwerk/Graphics/GfxFence.hpp"
#include "Renderwerk/Graphics/GfxResourceManager.hpp"
#include "Renderwerk/Graphics/GfxSwapchain.hpp"
#include "Renderwerk/Graphics/Pipeline/GfxGraphicsPipeline.hpp"
#include "Renderwerk/Job/JobModule.hpp"
#include "Renderwerk/Platform/Window.hpp"
#include "Renderwerk/Platform/Threading/ScopedLock.hpp"
#include "Renderwerk/Profiler/Profiler.hpp"
Expand Down Expand Up @@ -86,8 +87,6 @@ void FGfxSurface::Render()
const FGfxFrame& Frame = GetCurrentFrame();
RW_VERIFY(Frame.Fence->Wait());

GfxDevice->GetResourceManager()->ReleaseUploadRequests();

const TRef<FGfxTexture>& BackBuffer = Swapchain->GetBackBuffer(Swapchain->GetBackBufferIndex());

const TRef<FGfxCommandList> CommandList = Frame.CommandList;
Expand All @@ -98,7 +97,6 @@ void FGfxSurface::Render()
{
PROFILE_RENDER_SCOPE(ProfilerContext, CommandList, "ResourceUploading")
GfxDevice->GetResourceManager()->CollectResourceUploads();
GfxDevice->GetResourceManager()->FlushUploadRequests(CommandList);
}

CommandList->ResourceBarrier(BackBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET);
Expand Down
19 changes: 19 additions & 0 deletions Source/Runtime/Engine/Private/Renderwerk/Job/Job.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "pch.hpp"

#include "Renderwerk/Job/Job.hpp"

IJob::IJob(const EJobPriority InPriority)
: Priority(InPriority)
{
}

void IJob::SetPriority(const EJobPriority NewPriority)
{
Priority = NewPriority;
}

void IJob::MarkAsFinished()
{
bIsFinished = true;
CompletionDelegate.ExecuteIfBound();
}
Loading
Loading