Skip to content

Commit

Permalink
RendererFoundation refactor (ezEngine#358)
Browse files Browse the repository at this point in the history
Added concept of passes and command encoder to renderer foundation

Also added a factory for device creation. This allows us to switch between renderer implementations via command line argument e.g. "-renderer DX11" or "-renderer Vulkan".
  • Loading branch information
C-Core authored Jan 8, 2021
1 parent 67d7f4b commit a83650b
Show file tree
Hide file tree
Showing 119 changed files with 4,084 additions and 3,659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
#include <GameEngine/GameApplication/GameApplication.h>
#include <RendererCore/Pipeline/View.h>
#include <RendererCore/RenderWorld/RenderWorld.h>
#include <RendererFoundation/Context/Context.h>
#include <RendererFoundation/CommandEncoder/RenderCommandEncoder.h>
#include <RendererFoundation/Device/Device.h>
#include <RendererFoundation/Device/Pass.h>
#include <RendererFoundation/Resources/RenderTargetSetup.h>
#include <Texture/Image/Image.h>
#include <Texture/Image/ImageUtils.h>
Expand Down Expand Up @@ -439,7 +440,13 @@ void ezEngineProcessDocumentContext::UpdateDocumentContext()

// Download image
{
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->ReadbackTexture(m_hThumbnailColorRT);
auto pGALPass = ezGALDevice::GetDefaultDevice()->BeginPass("Thumbnail Readback");
EZ_SCOPE_EXIT(ezGALDevice::GetDefaultDevice()->EndPass(pGALPass));

auto pGALCommandEncoder = pGALPass->BeginRendering(ezGALRenderingSetup());
EZ_SCOPE_EXIT(pGALPass->EndRendering(pGALCommandEncoder));

pGALCommandEncoder->ReadbackTexture(m_hThumbnailColorRT);

ezGALSystemMemoryDescription MemDesc;
{
Expand All @@ -457,7 +464,7 @@ void ezEngineProcessDocumentContext::UpdateDocumentContext()

MemDesc.m_pData = image.GetPixelPointer<ezUInt8>();
ezArrayPtr<ezGALSystemMemoryDescription> SysMemDescs(&MemDesc, 1);
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->CopyTextureReadbackResult(m_hThumbnailColorRT, &SysMemDescs);
pGALCommandEncoder->CopyTextureReadbackResult(m_hThumbnailColorRT, &SysMemDescs);

ezImage imageSwap;
ezImage* pImage = &image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <RendererCore/Pipeline/RenderPipeline.h>
#include <RendererCore/Pipeline/View.h>
#include <RendererCore/RenderContext/RenderContext.h>
#include <RendererFoundation/Context/Context.h>
#include <RendererFoundation/Resources/Texture.h>

// clang-format off
Expand Down Expand Up @@ -69,12 +68,15 @@ void ezPickingRenderPass::Execute(const ezRenderViewContext& renderViewContext,

const ezGALTexture* pDepthTexture = ezGALDevice::GetDefaultDevice()->GetTexture(m_hPickingDepthRT);
EZ_ASSERT_DEV(m_uiWindowWidth == pDepthTexture->GetDescription().m_uiWidth, "");
EZ_ASSERT_DEV(m_uiWindowHeight == pDepthTexture->GetDescription().m_uiHeight, "");
EZ_ASSERT_DEV(m_uiWindowHeight == pDepthTexture->GetDescription().m_uiHeight, "");

ezGALRenderingSetup renderingSetup;
renderingSetup.m_RenderTargetSetup = m_RenderTargetSetup;
renderingSetup.m_uiRenderTargetClearMask = 0xFFFFFFFF;
renderingSetup.m_bClearDepth = true;
renderingSetup.m_bClearStencil = true;

renderViewContext.m_pRenderContext->SetViewportAndRenderTargetSetup(viewPortRect, m_RenderTargetSetup);

ezGALContext* pGALContext = renderViewContext.m_pRenderContext->GetGALContext();
pGALContext->Clear(ezColor(0.0f, 0.0f, 0.0f, 0.0f));
auto pCommandEncoder = ezRenderContext::BeginPassAndRenderingScope(renderViewContext, renderingSetup, GetName());

ezViewRenderMode::Enum viewRenderMode = renderViewContext.m_pViewData->m_ViewRenderMode;
if (viewRenderMode == ezViewRenderMode::WireframeColor || viewRenderMode == ezViewRenderMode::WireframeMonochrome)
Expand Down Expand Up @@ -141,7 +143,7 @@ void ezPickingRenderPass::Execute(const ezRenderViewContext& renderViewContext,
{
if (m_uiWindowWidth != 0 && m_uiWindowHeight != 0)
{
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->ReadbackTexture(GetPickingDepthRT());
pCommandEncoder->ReadbackTexture(GetPickingDepthRT());

ezMat4 mProj;
renderViewContext.m_pCamera->GetProjectionMatrix((float)m_uiWindowWidth / m_uiWindowHeight, mProj);
Expand Down Expand Up @@ -185,15 +187,15 @@ void ezPickingRenderPass::Execute(const ezRenderViewContext& renderViewContext,

MemDesc.m_pData = m_PickingResultsDepth.GetData();
ezArrayPtr<ezGALSystemMemoryDescription> SysMemDescsDepth(&MemDesc, 1);
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->CopyTextureReadbackResult(GetPickingDepthRT(), &SysMemDescsDepth);
pCommandEncoder->CopyTextureReadbackResult(GetPickingDepthRT(), &SysMemDescsDepth);
}
}

{
// download the picking information from the GPU
if (m_uiWindowWidth != 0 && m_uiWindowHeight != 0)
{
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->ReadbackTexture(GetPickingIdRT());
pCommandEncoder->ReadbackTexture(GetPickingIdRT());

ezMat4 mProj;
renderViewContext.m_pCamera->GetProjectionMatrix((float)m_uiWindowWidth / m_uiWindowHeight, mProj);
Expand Down Expand Up @@ -237,7 +239,7 @@ void ezPickingRenderPass::Execute(const ezRenderViewContext& renderViewContext,

MemDesc.m_pData = m_PickingResultsID.GetData();
ezArrayPtr<ezGALSystemMemoryDescription> SysMemDescs(&MemDesc, 1);
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->CopyTextureReadbackResult(GetPickingIdRT(), &SysMemDescs);
pCommandEncoder->CopyTextureReadbackResult(GetPickingIdRT(), &SysMemDescs);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Code/Editor/EditorFramework/EditorApp/Startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ void ezQtEditorApp::StartupEditor(ezBitflags<StartupFlags> startupFlags, const c
s_pEngineViewProcess = new ezEditorEngineProcessConnection;

s_pEngineViewProcess->SetWaitForDebugger(m_StartupFlags.IsSet(StartupFlags::Debug));
s_pEngineViewProcess->SetRenderer(pCmd->GetStringOption("-renderer", 0, ""));

m_LongOpControllerManager.Startup(&s_pEngineViewProcess->GetCommunicationChannel());

Expand Down
6 changes: 6 additions & 0 deletions Code/Editor/EditorFramework/IPC/EngineProcessConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ void ezEditorEngineProcessConnection::Initialize(const ezRTTI* pFirstAllowedMess
if (m_bProcessShouldWaitForDebugger)
{
args << "-debug";
}

if (!m_sRenderer.IsEmpty())
{
args << "-renderer";
args << m_sRenderer.GetData();
}

{
Expand Down
4 changes: 4 additions & 0 deletions Code/Editor/EditorFramework/IPC/EngineProcessConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class EZ_EDITORFRAMEWORK_DLL ezEditorEngineProcessConnection
void SetWaitForDebugger(bool bWait) { m_bProcessShouldWaitForDebugger = bWait; }
bool GetWaitForDebugger() const { return m_bProcessShouldWaitForDebugger; }

void SetRenderer(const char* szRenderer) { m_sRenderer = szRenderer; }
const char* GetRenderer() const { return m_sRenderer; }

bool IsEngineSetup() const { return m_bClientIsConfigured; }

/// /brief Sends a message that the document has been opened or closed. Resends all document data.
Expand Down Expand Up @@ -99,6 +102,7 @@ class EZ_EDITORFRAMEWORK_DLL ezEditorEngineProcessConnection
bool m_bProcessShouldBeRunning;
bool m_bProcessCrashed;
bool m_bClientIsConfigured;
ezString m_sRenderer;
ezEditorProcessCommunicationChannel m_IPC;
ezUniquePtr<ezEditorProcessRemoteCommunicationChannel> m_pRemoteProcess;
ezApplicationFileSystemConfig m_FileSystemConfig;
Expand Down
8 changes: 4 additions & 4 deletions Code/Editor/EditorProcessor/EditorProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ezEditorApplication : public ezApplication

const ezStringBuilder sProject = opt_Project.GetOptionValue(ezCommandLineOption::LogMode::Always);

if (!opt_Transform.GetOptionValue(ezCommandLineOption::LogMode::AlwaysIfSpecified).IsEmpty())
if (!ezStringUtils::IsNullOrEmpty(opt_Transform.GetOptionValue(ezCommandLineOption::LogMode::AlwaysIfSpecified)))
{
ezQtEditorApp::GetSingleton()->OpenProject(sProject).IgnoreResult();

Expand All @@ -157,12 +157,12 @@ class ezEditorApplication : public ezApplication

bTransform = false;

const ezString sPlatform = opt_Transform.GetOptionValue(ezCommandLineOption::LogMode::Never);
const ezUInt32 uiPlatform = ezAssetCurator::GetSingleton()->FindAssetProfileByName(sPlatform);
const char* szPlatform = opt_Transform.GetOptionValue(ezCommandLineOption::LogMode::Never);
const ezUInt32 uiPlatform = ezAssetCurator::GetSingleton()->FindAssetProfileByName(szPlatform);

if (uiPlatform == ezInvalidIndex)
{
ezLog::Error("Asset platform config '{0}' is unknown", sPlatform);
ezLog::Error("Asset platform config '{0}' is unknown", szPlatform);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ void ezGridRenderer::RenderBatch(const ezRenderViewContext& renderViewContext, c
return;

ezRenderContext* pRenderContext = renderViewContext.m_pRenderContext;
ezGALContext* pGALContext = pRenderContext->GetGALContext();

pRenderContext->SetShaderPermutationVariable("PRE_TRANSFORMED_VERTICES", "FALSE");
pRenderContext->BindShader(m_hShader);
Expand All @@ -197,7 +196,7 @@ void ezGridRenderer::RenderBatch(const ezRenderViewContext& renderViewContext, c
const ezUInt32 uiNumLineVerticesInBatch = ezMath::Min<ezUInt32>(uiNumLineVertices, LineVerticesPerBatch);
EZ_ASSERT_DEBUG(uiNumLineVerticesInBatch % 2 == 0, "Vertex count must be a multiple of 2.");

pGALContext->UpdateBuffer(m_hVertexBuffer, 0, ezMakeArrayPtr(pLineData, uiNumLineVerticesInBatch).ToByteArray());
pRenderContext->GetCommandEncoder()->UpdateBuffer(m_hVertexBuffer, 0, ezMakeArrayPtr(pLineData, uiNumLineVerticesInBatch).ToByteArray());

pRenderContext->BindMeshBuffer(m_hVertexBuffer, ezGALBufferHandle(), &m_VertexDeclarationInfo, ezGALPrimitiveTopology::Lines, uiNumLineVerticesInBatch / 2);
pRenderContext->DrawMeshBuffer().IgnoreResult();
Expand Down
2 changes: 1 addition & 1 deletion Code/Engine/Foundation/Utilities/CommandLineOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class EZ_FOUNDATION_DLL ezCommandLineOptionString : public ezCommandLineOptionDo
ezCommandLineOptionString(const char* szSortingGroup, const char* szArgument, const char* szLongDesc, const char* szDefaultValue, bool bCaseSensitive = false);

/// \brief Returns the value of this option. Either what was specified on the command line, or the default value.
ezString GetOptionValue(LogMode logMode, const ezCommandLineUtils* pUtils = ezCommandLineUtils::GetGlobalInstance()) const; // [tested]
const char* GetOptionValue(LogMode logMode, const ezCommandLineUtils* pUtils = ezCommandLineUtils::GetGlobalInstance()) const; // [tested]

/// \brief Modifies the default value
void SetDefault(const char* value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,9 @@ ezCommandLineOptionString::ezCommandLineOptionString(const char* szSortingGroup,
m_szDefaultValue = szDefaultValue;
}

ezString ezCommandLineOptionString::GetOptionValue(LogMode logMode, const ezCommandLineUtils* pUtils /*= ezCommandLineUtils::GetGlobalInstance()*/) const
const char* ezCommandLineOptionString::GetOptionValue(LogMode logMode, const ezCommandLineUtils* pUtils /*= ezCommandLineUtils::GetGlobalInstance()*/) const
{
ezString result = m_szDefaultValue;
const char* result = m_szDefaultValue;

ezStringBuilder sOption;
const bool bSpecified = IsOptionSpecified(&sOption, pUtils);
Expand Down
5 changes: 2 additions & 3 deletions Code/Engine/GameEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ ez_create_target(LIBRARY ${PROJECT_NAME})

target_link_libraries(${PROJECT_NAME}
PRIVATE
RendererDX11
)

target_link_libraries(${PROJECT_NAME}
PUBLIC
RendererCore
RendererCore
ozz
)

if (EZ_3RDPARTY_IMGUI_SUPPORT)
target_link_libraries(${PROJECT_NAME} PUBLIC Imgui)
target_link_libraries(${PROJECT_NAME} PUBLIC Imgui)
endif()

if (EZ_CMAKE_PLATFORM_WINDOWS_UWP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void ezImguiRenderer::RenderBatch(const ezRenderViewContext& renderContext, cons
return;

ezRenderContext* pRenderContext = renderContext.m_pRenderContext;
ezGALContext* pGALContext = pRenderContext->GetGALContext();
ezGALRenderCommandEncoder* pCommandEncoder = pRenderContext->GetRenderCommandEncoder();

pRenderContext->BindShader(m_hShader);
const auto& textures = ezImgui::GetSingleton()->m_hTextures;
Expand All @@ -168,8 +168,8 @@ void ezImguiRenderer::RenderBatch(const ezRenderViewContext& renderContext, cons
EZ_ASSERT_DEV(pRenderData->m_Vertices.GetCount() < VertexBufferSize, "GUI has too many elements to render in one drawcall");
EZ_ASSERT_DEV(pRenderData->m_Indices.GetCount() < IndexBufferSize, "GUI has too many elements to render in one drawcall");

pGALContext->UpdateBuffer(m_hVertexBuffer, 0, ezMakeArrayPtr(pRenderData->m_Vertices.GetPtr(), pRenderData->m_Vertices.GetCount()).ToByteArray());
pGALContext->UpdateBuffer(m_hIndexBuffer, 0, ezMakeArrayPtr(pRenderData->m_Indices.GetPtr(), pRenderData->m_Indices.GetCount()).ToByteArray());
pCommandEncoder->UpdateBuffer(m_hVertexBuffer, 0, ezMakeArrayPtr(pRenderData->m_Vertices.GetPtr(), pRenderData->m_Vertices.GetCount()).ToByteArray());
pCommandEncoder->UpdateBuffer(m_hIndexBuffer, 0, ezMakeArrayPtr(pRenderData->m_Indices.GetPtr(), pRenderData->m_Indices.GetCount()).ToByteArray());

pRenderContext->BindMeshBuffer(m_hVertexBuffer, m_hIndexBuffer, &m_VertexDeclarationInfo, ezGALPrimitiveTopology::Triangles, pRenderData->m_Indices.GetCount() / 3);

Expand All @@ -181,7 +181,7 @@ void ezImguiRenderer::RenderBatch(const ezRenderViewContext& renderContext, cons

if (imGuiBatch.m_uiVertexCount > 0 && imGuiBatch.m_uiTextureID < numTextures)
{
pGALContext->SetScissorRect(imGuiBatch.m_ScissorRect);
pCommandEncoder->SetScissorRect(imGuiBatch.m_ScissorRect);
pRenderContext->BindTexture2D("BaseTexture", textures[imGuiBatch.m_uiTextureID]);
pRenderContext->DrawMeshBuffer(imGuiBatch.m_uiVertexCount / 3, uiFirstIndex / 3).IgnoreResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include <Core/Curves/Curve1DResource.h>
#include <Core/Prefabs/PrefabResource.h>
#include <Foundation/IO/FileSystem/DataDirTypeFolder.h>
#include <Foundation/Utilities/CommandLineOptions.h>
#include <GameEngine/Animation/PropertyAnimResource.h>
#include <GameEngine/GameApplication/GameApplication.h>
#include <GameEngine/Physics/SurfaceResource.h>
#include <GameEngine/VisualScript/VisualScriptResource.h>
#include <RendererCore/AnimationSystem/AnimationClipResource.h>
#include <RendererCore/Decals/DecalAtlasResource.h>
#include <RendererCore/GPUResourcePool/GPUResourcePool.h>
#include <RendererCore/Material/MaterialResource.h>
#include <RendererCore/Meshes/MeshResource.h>
Expand All @@ -18,13 +20,16 @@
#include <RendererCore/ShaderCompiler/ShaderManager.h>
#include <RendererCore/Textures/Texture2DResource.h>
#include <RendererCore/Textures/TextureCubeResource.h>
#include <RendererFoundation/Device/Device.h>
#include <RendererFoundation/Device/DeviceFactory.h>

#if EZ_ENABLED(EZ_PLATFORM_WINDOWS)
# include <RendererDX11/Device/DeviceDX11.h>
typedef ezGALDeviceDX11 ezGALDeviceDefault;
#ifdef BUILDSYSTEM_ENABLE_VULKAN_SUPPORT
constexpr const char* szDefaultRenderer = "Vulkan";
#else
constexpr const char* szDefaultRenderer = "DX11";
#endif

#include <RendererCore/Decals/DecalAtlasResource.h>
ezCommandLineOptionString opt_Renderer("app", "-renderer", "The renderer implementation to use.", szDefaultRenderer);

void ezGameApplication::Init_ConfigureAssetManagement()
{
Expand Down Expand Up @@ -209,6 +214,10 @@ void ezGameApplication::Init_SetupDefaultResources()
}
}

ezString GetRendererNameFromCommandLine()
{
return opt_Renderer.GetOptionValue(ezCommandLineOption::LogMode::FirstTimeIfSpecified);
}

void ezGameApplication::Init_SetupGraphicsDevice()
{
Expand All @@ -224,9 +233,15 @@ void ezGameApplication::Init_SetupGraphicsDevice()
ezGALDevice* pDevice = nullptr;

if (s_DefaultDeviceCreator.IsValid())
{
pDevice = s_DefaultDeviceCreator(DeviceInit);
}
else
pDevice = EZ_DEFAULT_NEW(ezGALDeviceDefault, DeviceInit);
{
const char* szRendererName = GetRendererNameFromCommandLine();
pDevice = ezGALDeviceFactory::CreateDevice(szRendererName, ezFoundation::GetDefaultAllocator(), DeviceInit);
EZ_ASSERT_DEV(pDevice != nullptr, "Device implemention for '{}' not found", szRendererName);
}

EZ_VERIFY(pDevice->Init() == EZ_SUCCESS, "Graphics device creation failed!");
ezGALDevice::SetDefaultDevice(pDevice);
Expand All @@ -241,16 +256,16 @@ void ezGameApplication::Init_SetupGraphicsDevice()

void ezGameApplication::Init_LoadRequiredPlugins()
{
const char* szRendererName = GetRendererNameFromCommandLine();
const char* szShaderModel = "";
const char* szShaderCompiler = "";
ezGALDeviceFactory::GetShaderModelAndCompiler(szRendererName, szShaderModel, szShaderCompiler);
ezShaderManager::Configure(szShaderModel, true);

#if EZ_ENABLED(EZ_COMPILE_FOR_DEVELOPMENT)
ezPlugin::LoadPlugin("ezInspectorPlugin").IgnoreResult();

# ifdef BUILDSYSTEM_ENABLE_VULKAN_SUPPORT
ezShaderManager::Configure("VULKAN", true);
EZ_VERIFY(ezPlugin::LoadPlugin("ezShaderCompilerDXC").Succeeded(), "DXC compiler plugin not found");
# else
ezShaderManager::Configure("DX11_SM50", true);
EZ_VERIFY(ezPlugin::LoadPlugin("ezShaderCompilerHLSL").Succeeded(), "HLSL compiler plugin not found");
# endif
EZ_VERIFY(ezPlugin::LoadPlugin(szShaderCompiler).Succeeded(), "Shader compiler '{}' plugin not found", szShaderCompiler);

# ifdef BUILDSYSTEM_ENABLE_RENDERDOC_SUPPORT
ezPlugin::LoadPlugin("ezRenderDocPlugin").IgnoreResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <GameEnginePCH.h>

#include <GameEngine/GameApplication/WindowOutputTarget.h>
#include <RendererFoundation/Context/Context.h>
#include <RendererFoundation/CommandEncoder/RenderCommandEncoder.h>
#include <RendererFoundation/Device/Device.h>
#include <RendererFoundation/Device/Pass.h>
#include <RendererFoundation/Resources/Texture.h>
#include <Texture/Image/Image.h>

Expand Down Expand Up @@ -32,9 +33,16 @@ void ezWindowOutputTargetGAL::Present(bool bEnableVSync)
ezResult ezWindowOutputTargetGAL::CaptureImage(ezImage& out_Image)
{
ezGALDevice* pDevice = ezGALDevice::GetDefaultDevice();

auto pGALPass = pDevice->BeginPass("CaptureImage");
EZ_SCOPE_EXIT(pDevice->EndPass(pGALPass));

auto pGALCommandEncoder = pGALPass->BeginRendering(ezGALRenderingSetup());
EZ_SCOPE_EXIT(pGALPass->EndRendering(pGALCommandEncoder));

ezGALTextureHandle hBackbuffer = pDevice->GetBackBufferTextureFromSwapChain(m_hSwapChain);

ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->ReadbackTexture(hBackbuffer);
pGALCommandEncoder->ReadbackTexture(hBackbuffer);

const ezGALTexture* pBackbuffer = ezGALDevice::GetDefaultDevice()->GetTexture(hBackbuffer);
const ezUInt32 uiWidth = pBackbuffer->GetDescription().m_uiWidth;
Expand All @@ -50,7 +58,7 @@ ezResult ezWindowOutputTargetGAL::CaptureImage(ezImage& out_Image)
/// \todo Make this more efficient
MemDesc.m_pData = backbufferData.GetData();
ezArrayPtr<ezGALSystemMemoryDescription> SysMemDescsDepth(&MemDesc, 1);
ezGALDevice::GetDefaultDevice()->GetPrimaryContext()->CopyTextureReadbackResult(hBackbuffer, &SysMemDescsDepth);
pGALCommandEncoder->CopyTextureReadbackResult(hBackbuffer, &SysMemDescsDepth);

ezImageHeader header;
header.SetWidth(uiWidth);
Expand Down
Loading

0 comments on commit a83650b

Please sign in to comment.