Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Renderer.cpp
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Header section
- #include "Game/Loader/Assimp.h"
- #include "Renderer.h"
- #include <Graphics/System/MaterialSystem.h>
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Declaration section
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Definition section
- /// <summary>
- /// Parametric constructor.
- ///
- /// Builds renderer and sets everything up.
- /// </summary>
- /// <param name="engine">Pointer to Engine instance</param>
- /// <param name="eventSystem">Pointer to event system instance</param>
- /// <param name="options">Pointer to configuration instance</param>
- /// <param name="log">Pointer to logging system</param>
- /// <param name="renderer">Pointer to Direct3D 12 renderer</param>
- /// <param name="swapChain">Pointer to Swap chain for window</param>
- /// <param name="appState">Pointer to Scmdl class instance (holding state/values for application)</param>
- Renderer::Renderer(Engine::Core* engine, Engine::EventSystem* eventSystem, Engine::Constants* options, Engine::Log* log, Engine::D3DRenderer* renderer, Engine::D3DSwapChain* swapChain, Application* appState) :
- Engine::System("Renderer", eventSystem),
- mOptions(options),
- mLog(log),
- mRenderer(renderer),
- mSwapChain(swapChain),
- mAppState(appState)
- {
- // Run in background
- EnableUpdater(Engine::Task::BACKGROUND_SYNC_REPEATING);
- // Create compute/render pipeline
- mPipeline = new Engine::ComputeGraph(engine);
- // Create render nodes
- // Compute ray tracer - ray generator
- Engine::ComputeGraphNode* mRaygenNode = new Engine::ComputeGraphNode("Raytracer::Raygen", eventSystem, [&]() -> void
- {
- Engine::ComputeContext* context = mRenderer->GetComputeContext();
- context->Begin();
- mAppState->mGfxProfiler->BeginProfile(context, "Raytracer::Raygen");
- mAppState->mRayGenerator->InitPrimaryRaysDevice(mAppState->mRayBuffer, context, mRenderer->Heap(), mAppState->mScene->GetEntity("Camera"), mAppState->mViewportWidth, mAppState->mViewportHeight);
- mAppState->mGfxProfiler->EndProfile(context, "Raytracer::Raygen");
- context->Finish();
- });
- // Compute ray tracer - trace rays
- Engine::ComputeGraphNode* mRaytraceNode = new Engine::ComputeGraphNode("Raytracer::Trace", eventSystem, [&]() -> void
- {
- Engine::ComputeContext* context = mRenderer->GetComputeContext();
- context->Begin();
- mAppState->mGfxProfiler->BeginProfile(context, "Raytracer::Trace");
- mRaytracerPass->SetSources(mAppState->mRayBuffer, mAppState->mSceneBuffer, mAppState->mAggregate);
- mRaytracerPass->Process(mRenderer->Heap(), context, mAppState->mRenderNodes->GetBounds(), mAppState->mRaytracerInitialized);
- mAppState->mGfxProfiler->EndProfile(context, "Raytracer::Trace");
- context->Finish();
- });
- mRaytraceNode->AddInput(mRaygenNode);
- // Hardware ray tracer
- Engine::ComputeGraphNode* mRaytraceHWNode = new Engine::ComputeGraphNode("DXRPass", eventSystem, [&]() -> void
- {
- Engine::GraphicsContext* context = mRenderer->GetGraphicsContext();
- context->Begin();
- mAppState->mGfxProfiler->BeginProfile(context, "DXRPass");
- mRaytracerPassHW->SetSources(mAppState->mScene);
- mRaytracerPassHW->Process(mRenderer->Heap(), context, mAppState->mScene->GetEntity("Camera"), mAppState->mViewportWidth, mAppState->mViewportHeight);
- mAppState->mGfxProfiler->EndProfile(context, "DXRPass");
- context->Finish();
- });
- // Prepare scenegraph render nodes into buffer (for rendering)
- Engine::ComputeGraphNode* mRenderNodesNode = new Engine::ComputeGraphNode("RenderNodesPass", eventSystem, [&]() -> void
- {
- Engine::ComputeContext* context = mRenderer->GetComputeContext();
- context->Begin();
- mAppState->mRenderNodes->Process(context);
- context->Finish();
- });
- // Prepare material systems data into buffer (for rendering)
- Engine::ComputeGraphNode* mMaterialNode = new Engine::ComputeGraphNode("MaterialPass", eventSystem, [&]() -> void
- {
- Engine::ComputeContext* context = mRenderer->GetComputeContext();
- context->Begin();
- // If anything in editor was changed - check whether the object has material component, if it has - material system needs to be updated for this change
- if (mAppState->mEditor->GetChangedFlag())
- {
- std::set<int> ids = mAppState->mScene->GetState()->GetSelection();
- for (std::set<int>::iterator it = ids.begin(); it != ids.end(); it++)
- {
- Engine::Entity* entity = mAppState->mScene->GetEntity(*it);
- if (entity->GameObject().Has<Engine::MaterialComponent>())
- {
- Engine::MaterialComponent* materialComponent = entity->GameObject().Get<Engine::MaterialComponent>();
- int materialID = materialComponent->GetMaterialID();
- SkyeCuillin::MaterialSystem::Material* material = mAppState->mMaterialSystem->GetMaterial(materialID);
- material->mDiffuseMap = materialComponent->GetDiffuseMap()->GetSRVIndex();
- if (material->mDiffuseMap == (unsigned int)-1)
- {
- material->mDiffuseMap = mAppState->mDefault[(unsigned int)Application::DefaultTextures::DIFFUSE]->GetSRVIndex();
- }
- material->mNormalsMap = materialComponent->GetNormalsMap()->GetSRVIndex();
- if (material->mNormalsMap == (unsigned int)-1)
- {
- material->mNormalsMap = mAppState->mDefault[(unsigned int)Application::DefaultTextures::NORMAL]->GetSRVIndex();
- }
- material->mMetallicMap = materialComponent->GetMetallicMap()->GetSRVIndex();
- if (material->mMetallicMap == (unsigned int)-1)
- {
- material->mMetallicMap = mAppState->mDefault[(unsigned int)Application::DefaultTextures::METALLIC]->GetSRVIndex();
- }
- material->mRoughnessMap = materialComponent->GetRoughnessMap()->GetSRVIndex();
- if (material->mRoughnessMap == (unsigned int)-1)
- {
- material->mRoughnessMap = mAppState->mDefault[(unsigned int)Application::DefaultTextures::ROUGHNESS]->GetSRVIndex();
- }
- mAppState->mMaterialSystem->UpdateMaterial(materialID);
- }
- }
- }
- mAppState->mMaterialSystem->Process(context);
- context->Finish();
- });
- // Render all shadow maps in lighting system that need to be updated
- Engine::ComputeGraphNode* mShadowRenderNode = new Engine::ComputeGraphNode("ShadowPass", eventSystem, [&]() -> void
- {
- std::lock_guard<std::mutex> lock(mAppState->mRenderingMutex);
- Engine::GraphicsContext* context = mRenderer->GetGraphicsContext();
- context->Begin();
- mAppState->mGfxProfiler->BeginProfile(context, "ShadowPass");
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- break;
- case Application::ViewportState::TEXTURED:
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mLightingSystem->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mAppState->mMaterialSystem->GetMaterialsBuffer(), mAppState->mRenderNodes->GetBounds());
- break;
- default:
- break;
- }
- mAppState->mGfxProfiler->EndProfile(context, "ShadowPass");
- context->Finish();
- });
- mShadowRenderNode->AddInput(mRenderNodesNode);
- mShadowRenderNode->AddInput(mMaterialNode);
- mRenderNode = new Engine::ComputeGraphNode("Rendering", eventSystem, [&]() -> void
- {
- std::lock_guard<std::mutex> lock(mAppState->mRenderingMutex);
- Engine::GraphicsContext* context = mRenderer->GetGraphicsContext();
- context->Begin();
- mAppState->SetGraphicsContext(context);
- mAppState->mGfxProfiler->Reset(context);
- mAppState->mGfxProfiler->BeginProfile(context, "Frame");
- /*context->SetDescriptorHeap(Engine::DescriptorHeap::CBV_SRV_UAV, mRenderer->Heap());
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, true);
- context->SetRenderTargets(mSwapChain->GetBackBuffer(), mSwapChain->GetDepthBuffer());
- context->SetViewport(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->SetScissorRect(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->ClearColor(mSwapChain->GetBackBuffer(), 0.0f, 0.0f, 0.0f, 1.0f);
- context->ClearDepth(mSwapChain->GetDepthBuffer(), 1.0f, 0);*/
- Engine::Camera* cam = mAppState->mScene->GetEntity("Camera")->GameObject().Get<Engine::CameraComponent>()->GetCamera();
- mAppState->mGfxProfiler->BeginProfile(context, "CullPassHiZ");
- mCulling->SetBuffers(mAppState->mRenderNodes, cam->GetFrustum(), Engine::Culling::DEFAULT_CULLING_BUFFER, mAppState->mScene->GetEntity("Camera"));
- mCulling->Process(mRenderer->Heap(), context);
- mAppState->mGfxProfiler->EndProfile(context, "CullPassHiZ");
- mAppState->mGfxProfiler->BeginProfile(context, "Hi-ZPass1");
- mHiZPass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mAppState->mMaterialSystem->GetMaterialsBuffer(), mCulling);
- mAppState->mGfxProfiler->EndProfile(context, "Hi-ZPass1");
- mAppState->mGfxProfiler->BeginProfile(context, "Hi-ZPass2");
- mHiZPass->Process2(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mAppState->mMaterialSystem->GetMaterialsBuffer(), mCulling);
- mAppState->mGfxProfiler->EndProfile(context, "Hi-ZPass2");
- mAppState->mGfxProfiler->BeginProfile(context, "CullPass");
- //mCulling->SetBuffers(mAppState->mRenderNodes, f, SkyeCuillin::Culling::DEFAULT_CULLING_BUFFER, mAppState->mScene->GetEntity("Camera"), mHiZPass->GetWidth(), mHiZPass->GetHeight(), mHiZPass->GetNumMiplevels());
- mCulling->SetBuffers(mAppState->mRenderNodes, cam->GetFrustum(), Engine::Culling::DEFAULT_CULLING_BUFFER, mAppState->mScene->GetEntity("Camera"), mHiZPass->GetWidth(), mHiZPass->GetHeight(), mHiZPass->GetNumMiplevels());
- mCulling->Process(mRenderer->Heap(), context, Engine::Culling::DEFAULT_CULLING_BUFFER, mHiZPass->GetBuffer());
- //mCulling->SetBuffers(mAppState->mRenderNodes, cam->GetFrustum(), mAppState->mScene->GetEntity("Camera"));
- //mCulling->Process(mRenderer->Heap(), context);
- mAppState->mGfxProfiler->EndProfile(context, "CullPass");
- mAppState->mGfxProfiler->BeginProfile(context, "DrawPass");
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- //mWireframePass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mCulling);
- mWireframePass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mCulling);
- break;
- case Application::ViewportState::TEXTURED:
- mTexturedPass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mAppState->mMaterialSystem->GetMaterialsBuffer(), mCulling);
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mGBufferPass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context, mAppState->mRenderNodes, mAppState->mMaterialSystem->GetMaterialsBuffer(), mCulling);
- break;
- default:
- break;
- }
- mAppState->mGfxProfiler->EndProfile(context, "DrawPass");
- // TODO: This should be moved to place after all render nodes are used
- mAppState->mRenderNodes->Clean(context);
- mAppState->mGfxProfiler->BeginProfile(context, "NativeResolve");
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- mNativeResolvePass->SetSources(mWireframePass->GetBuffer(), mWireframePass->GetDepthBuffer());
- break;
- case Application::ViewportState::TEXTURED:
- mNativeResolvePass->SetSources(mTexturedPass->GetBuffer(), mTexturedPass->GetDepthBuffer());
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mNativeResolvePass->SetSources(mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::NORMAL_BUFFER), mGBufferPass->GetDepthBuffer());
- break;
- default:
- break;
- }
- mNativeResolvePass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context);
- mAppState->mGfxProfiler->EndProfile(context, "NativeResolve");
- mAppState->mGfxProfiler->BeginProfile(context, "ReductionPass");
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- mMultisampling->SetSources(mWireframePass->GetBuffer(), mWireframePass->GetDepthBuffer());
- break;
- case Application::ViewportState::TEXTURED:
- mMultisampling->SetSources(mTexturedPass->GetBuffer(), mTexturedPass->GetDepthBuffer());
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mMultisampling->SetSources(mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::NORMAL_BUFFER), mGBufferPass->GetDepthBuffer());
- break;
- default:
- break;
- }
- mMultisampling->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context);
- mAppState->mGfxProfiler->EndProfile(context, "ReductionPass");
- mAppState->mGfxProfiler->BeginProfile(context, "ResolvePass");
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- mResolve->SetSources(mWireframePass->GetBuffer(), mMultisampling->GetTiles(), mMultisampling->GetTilesCount(), mMultisampling->GetTileRecords(), mMultisampling->GetTileSamples());
- break;
- case Application::ViewportState::TEXTURED:
- mResolve->SetSources(mTexturedPass->GetBuffer(), mMultisampling->GetTiles(), mMultisampling->GetTilesCount(), mMultisampling->GetTileRecords(), mMultisampling->GetTileSamples());
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mLightingPass->SetSources(mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::COLOR_BUFFER),
- mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::NORMAL_BUFFER),
- mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::DEPTH_BUFFER),
- mMultisampling->GetTilesCount(),
- mMultisampling->GetTiles(),
- mMultisampling->GetTileRecords(),
- mMultisampling->GetTileSamples());
- mLightingPass->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context);
- mAvgLuminance->SetSources(mLightingPass->GetOutput(), mMultisampling->GetTiles(), mMultisampling->GetTilesCount(), mMultisampling->GetTileRecords(), mMultisampling->GetTileSamples());
- mAvgLuminance->Process(mRenderer->Heap(), context, mAppState->mDeltaTime);
- mResolve->SetSources(mLightingPass->GetOutput(),
- mGBufferPass->GetBuffer(Engine::GBuffer::Buffer::COLOR_BUFFER),
- mMultisampling->GetTiles(),
- mMultisampling->GetTilesCount(),
- mMultisampling->GetTileRecords(),
- mMultisampling->GetTileSamples(),
- mAvgLuminance->GetAvgLuminanceBuffer());
- break;
- default:
- break;
- }
- mResolve->Process(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context);
- mAppState->mGfxProfiler->EndProfile(context, "ResolvePass");
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, true);
- context->SetRenderTargets(mSwapChain->GetBackBuffer(), mSwapChain->GetDepthBuffer());
- context->SetViewport(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->SetScissorRect(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->ClearColor(mSwapChain->GetBackBuffer(), 0.0f, 0.0f, 0.0f, 1.0f);
- context->ClearDepth(mSwapChain->GetDepthBuffer(), 1.0f, 0);
- std::set<int> ids;
- Engine::float4 basis[3] = { Engine::float4(1.0f, 0.0f, 0.0f, 1.0f), Engine::float4(0.0f, 1.0f, 0.0f, 1.0f), Engine::float4(0.0f, 0.0f, 1.0f, 1.0f) };
- mGizmoPass->Clear();
- //mGizmoPass->Process(nodes, mAppState->mScene, mMatricesBuffer, ids);
- //mGizmoPass->ProcessIcons(nodes);
- mGizmoPass->AddTranslationAxis(Engine::float4(0.0f, 0.0f, 0.0f, 1.0f), basis, 10.0f, -1, -1);
- mGizmoPass->Render(mAppState->mScene->GetEntity("Camera"), mRenderer->Heap(), context);
- context->SetDescriptorHeap(Engine::DescriptorHeap::CBV_SRV_UAV, mRenderer->Heap());
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, true);
- context->SetRenderTargets(mSwapChain->GetBackBuffer(), mSwapChain->GetDepthBuffer());
- context->SetViewport(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->SetScissorRect(0.0f, 0.0f, (float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y);
- context->ClearColor(mSwapChain->GetBackBuffer(), 0.0f, 0.0f, 0.0f, 1.0f);
- context->ClearDepth(mSwapChain->GetDepthBuffer(), 1.0f, 0);
- // Draw Imgui user interface
- mImgui->Update(context, 1.0f / 60.0f);
- mImgui->NewFrame();
- mAppState->mMenuUI->Process(mAppState->mWindowSize);
- // Create root imgui window for dockspace
- ImGuiViewport* viewport = ImGui::GetMainViewport();
- ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
- ImGui::SetNextWindowSize(ImVec2((float)mAppState->mWindowSize.x, (float)mAppState->mWindowSize.y));
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
- ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoBackground);
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGuiID dockspace = ImGui::GetID("Dockspace");
- ImGui::DockSpace(dockspace, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_None | ImGuiDockNodeFlags_PassthruCentralNode);
- if (mAppState->mEditorPreferences)
- {
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
- if (ImGui::Begin("Preferences", &mAppState->mEditorPreferences, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking))
- {
- bool changed = false;
- // Visual style section
- ImGui::SeparatorText("Editor Style");
- const char* styles[] = { "Light", "Dark" };
- int styleCurrent = mOptions->Get<int>("Graphics.UI.Style");
- changed |= ImGui::Combo("Editor Style", &styleCurrent, styles, IM_ARRAYSIZE(styles));
- float alpha = mOptions->Get<float>("Graphics.UI.Alpha");
- changed |= ImGui::DragFloat("Editor Alpha", &alpha, 0.05f, 0.0f, 1.0f);
- // Fonts section
- ImGui::SeparatorText("Editor Fonts");
- std::string defaultFontFile = mOptions->Get<std::string>("Graphics.UI.Fonts.Default.Font");
- int defaultFontSize = mOptions->Get<int>("Graphics.UI.Fonts.Default.Size");
- if (ImGui::TreeNodeEx("Default Font", ImGuiTreeNodeFlags_DefaultOpen))
- {
- if (ImGui::Button(defaultFontFile.c_str()))
- {
- std::string filename;
- bool open = Engine::FileDialog::Show(mLog, "Select TrueType Font", "TrueType Font Files\0*.ttf\0All Files\0*.*\0", Engine::FileDialog::Type::OPEN_FILE_DIALOG, filename);
- if (open)
- {
- defaultFontFile = filename;
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- }
- if (ImGui::InputInt("Font size", &defaultFontSize, 1, 2))
- {
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- ImGui::TreePop();
- }
- std::string iconsFontFile = mOptions->Get<std::string>("Graphics.UI.Fonts.Icons.Font");
- int iconsFontSize = mOptions->Get<int>("Graphics.UI.Fonts.Icons.Size");
- if (ImGui::TreeNodeEx("Icons Font", ImGuiTreeNodeFlags_DefaultOpen))
- {
- if (ImGui::Button(iconsFontFile.c_str()))
- {
- std::string filename;
- bool open = Engine::FileDialog::Show(mLog, "Select TrueType Font", "TrueType Font Files\0*.ttf\0All Files\0*.*\0", Engine::FileDialog::Type::OPEN_FILE_DIALOG, filename);
- if (open)
- {
- iconsFontFile = filename;
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- }
- if (ImGui::InputInt("Font size", &iconsFontSize, 1, 2))
- {
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- ImGui::TreePop();
- }
- std::string monospaceFontFile = mOptions->Get<std::string>("Graphics.UI.Fonts.Monospace.Font");
- int monospaceFontSize = mOptions->Get<int>("Graphics.UI.Fonts.Monospace.Size");
- if (ImGui::TreeNodeEx("Monospace Font", ImGuiTreeNodeFlags_DefaultOpen))
- {
- if (ImGui::Button(monospaceFontFile.c_str()))
- {
- std::string filename;
- bool open = Engine::FileDialog::Show(mLog, "Select TrueType Font", "TrueType Font Files\0*.ttf\0All Files\0*.*\0", Engine::FileDialog::Type::OPEN_FILE_DIALOG, filename);
- if (open)
- {
- monospaceFontFile = filename;
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- }
- if (ImGui::InputInt("Font size", &monospaceFontSize, 1, 2))
- {
- changed = true;
- mAppState->mEditorPreferencesFontFlag = true;
- }
- ImGui::TreePop();
- }
- if (changed)
- {
- bool style = styleCurrent == 1 ? true : false;
- mImgui->SetupImGuiStyle(style, alpha);
- mOptions->Set<int>("Graphics.UI.Style", styleCurrent);
- mOptions->Set<float>("Graphics.UI.Alpha", alpha);
- mOptions->Set<std::string>("Graphics.UI.Fonts.Default.Font", defaultFontFile);
- mOptions->Set<int>("Graphics.UI.Fonts.Default.Size", defaultFontSize);
- mOptions->Set<std::string>("Graphics.UI.Fonts.Icons.Font", iconsFontFile);
- mOptions->Set<int>("Graphics.UI.Fonts.Icons.Size", iconsFontSize);
- mOptions->Set<std::string>("Graphics.UI.Fonts.Monospace.Font", monospaceFontFile);
- mOptions->Set<int>("Graphics.UI.Fonts.Monospace.Size", monospaceFontSize);
- }
- if (ImGui::Button("Save"))
- {
- mOptions->Save("../Data/Config/ConfigModel.conf");
- }
- }
- ImGui::End();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- }
- ImGui::End();
- unsigned int regionWidth = mAppState->mWindowSize.x;
- unsigned int regionHeight = mAppState->mWindowSize.y;
- if (ImGui::Begin("Debug"))
- {
- //ImGui::Image((ImTextureID)(mGBufferPass->GetBuffer(0)->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mWireframePass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mTexturedPass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mNativeResolvePass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- if (mAppState->GetViewportState() == Application::ViewportState::LIT_TEXTURED || mAppState->GetViewportState() == Application::ViewportState::LIT)
- {
- //ImGui::Image((ImTextureID)(mLightingSystem->GetShadowMap()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- ImGui::Image((ImTextureID)(mResolve->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- }
- else
- {
- //ImGui::Image((ImTextureID)(mHiZPass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mRaytracerPass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- ImGui::Image((ImTextureID)(mResolve->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- }
- ImVec2 regionMin = ImGui::GetWindowContentRegionMin();
- ImVec2 regionMax = ImGui::GetWindowContentRegionMax();
- regionWidth = (unsigned int)(regionMax.x - regionMin.x);
- regionHeight = (unsigned int)(regionMax.y - regionMin.y);
- if (regionWidth < 8)
- {
- regionWidth = 8;
- }
- if (regionWidth > 4096)
- {
- regionWidth = 4096;
- }
- if (regionHeight < 8)
- {
- regionHeight = 8;
- }
- if (regionHeight > 4096)
- {
- regionHeight = 4096;
- }
- }
- ImGui::End();
- if (ImGui::Begin("Debug2"))
- {
- ImGui::Image((ImTextureID)(mRaytracerPassHW->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mRaytracerPass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- //ImGui::Image((ImTextureID)(mLightingSystem->GetShadowMap()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- }
- ImGui::End();
- if (ImGui::Begin("Debug3"))
- {
- ImGui::Image((ImTextureID)(mRaytracerPass->GetBuffer()->GetSRV().mGpuHandle.ptr), ImGui::GetContentRegionAvail());
- }
- ImGui::End();
- if (ImGui::Begin("Histogram"))
- {
- if (ImPlot::BeginPlot("Histogram"))
- {
- float data[256];
- for (int i = 0; i < 256; i++)
- {
- data[i] = (float)(mAvgLuminance->GetHistogramHostBuffer()[i]);
- }
- ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Linear);
- ImPlot::SetupAxisLimits(ImAxis_X1, 0.0, 256.0);
- ImPlot::SetupAxisScale(ImAxis_Y1, ImPlotScale_Linear);
- ImPlot::SetupAxisLimits(ImAxis_Y1, 0.0, 1024.0);
- ImPlot::PlotLine("Buckets", data, 256);
- ImPlot::EndPlot();
- }
- }
- ImGui::End();
- if (ImGui::Begin("AverageLuminance"))
- {
- ImGui::LabelText("Average Luminance", "%f", mAvgLuminance->GetAvgLuminanceHostBuffer()[0]);
- }
- ImGui::End();
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
- if (ImGui::Begin("Scenegraph", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus))
- {
- mAppState->mEditorScenegraph->ImguiScenegraphEditor();
- }
- ImGui::End();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
- if (ImGui::Begin("Edit", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus))
- {
- mAppState->mEditorComponent->ImguiSelectionEditor();
- }
- ImGui::End();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
- ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
- if (ImGui::Begin("Directory View", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus))
- {
- mAppState->mDirTree->Imgui();
- }
- ImGui::End();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- ImGui::PopStyleVar();
- if (Engine::ComponentStatic::mEditedComponent != nullptr)
- {
- mAppState->mEditorManager->ImguiManagerList<Engine::Texture>("Select Texture", mAppState->mAssetManager->GetManager<Engine::Texture>());
- }
- static bool showProfiler = true;
- if (ImGui::Begin("Profile", &showProfiler))
- {
- char tmp[255] = { 0 };
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("Frame")); ImGui::LabelText("Frame", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("ShadowPass")); ImGui::LabelText("ShadowPass", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("Hi-ZPass1")); ImGui::LabelText("Hi-ZPass1", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("Hi-ZPass2")); ImGui::LabelText("Hi-ZPass2", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("CullPass")); ImGui::LabelText("CullPass", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("CullPassHiZ")); ImGui::LabelText("CullPassHiZ", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("DrawPass")); ImGui::LabelText("DrawPass", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("ReductionPass")); ImGui::LabelText("ReductionPass", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("ResolvePass")); ImGui::LabelText("ResolvePass", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("NativeResolve")); ImGui::LabelText("NativeResolve", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("Raytracer::Raygen")); ImGui::LabelText("Raytracer::Raygen", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("Raytracer::Trace")); ImGui::LabelText("Raytracer::Trace", tmp);
- snprintf(tmp, sizeof(tmp), "%.2f ms", mAppState->mGfxProfiler->GetTime("DXRPass")); ImGui::LabelText("DXRPass", tmp);
- }
- ImGui::End();
- mImgui->Render();
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_PRESENT, true);
- mAppState->mGfxProfiler->EndProfile(context, "Frame");
- mAppState->mGfxProfiler->Resolve(context);
- mAppState->ReleaseGraphicsContext();
- // Finish recording command list, execute command list and wait for execution
- uint64_t fence = context->Finish();
- if (mAppState->mEditorPreferencesFontFlag)
- {
- mAppState->mEditorPreferencesFontFlag = false;
- mImgui->SetupFonts(mRenderer);
- }
- mAvgLuminance->Readback();
- if (mAppState->mViewportWidth != regionWidth && mAppState->mViewportHeight != regionHeight)
- {
- mAppState->mViewportWidth = regionWidth;
- mAppState->mViewportHeight = regionHeight;
- mWireframePass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mTexturedPass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mGBufferPass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mNativeResolvePass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mMultisampling->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mResolve->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mLightingPass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mRaytracerPass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mRaytracerPassHW->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- mHiZPass->Resize(mAppState->mViewportWidth, mAppState->mViewportHeight);
- delete mAppState->mRayBuffer;
- mAppState->mRayBuffer = new Raytracer::RayBuffer(mRenderer, mAppState->mViewportWidth * mAppState->mViewportHeight);
- }
- if (mAppState->mBaseSignatureChanged == true)
- {
- mAppState->mBaseSignatureChanged = false;
- delete mAppState->mRenderNodes;
- switch (mAppState->GetViewportState())
- {
- case Application::ViewportState::WIREFRAME:
- mAppState->mRenderNodes = new Engine::RenderNodeList(mRenderer, mOptions->Get<int>("Scene.MaxObjects"), std::vector<Engine::RootSignature*> { mWireframePass->GetRootSignature(), nullptr, mHiZPass->GetRootSignature() });
- break;
- case Application::ViewportState::TEXTURED:
- mAppState->mRenderNodes = new Engine::RenderNodeList(mRenderer, mOptions->Get<int>("Scene.MaxObjects"), std::vector<Engine::RootSignature*> { mTexturedPass->GetRootSignature(), nullptr, mHiZPass->GetRootSignature() });
- break;
- case Application::ViewportState::LIT_TEXTURED:
- case Application::ViewportState::LIT:
- mAppState->mRenderNodes = new Engine::RenderNodeList(mRenderer, mOptions->Get<int>("Scene.MaxObjects"), std::vector<Engine::RootSignature*> { mGBufferPass->GetRootSignature(), mLightingSystem->GetRootSignature(), mHiZPass->GetRootSignature() });
- break;
- default:
- break;
- }
- }
- mAppState->mGfxProfiler->Process();
- // Swap buffers in swap chain and present to screen
- mSwapChain->SwapBuffers();
- });
- mRenderNode->AddInput(mRaytraceNode);
- mRenderNode->AddInput(mRaytraceHWNode);
- mRenderNode->AddInput(mShadowRenderNode);
- // Add render nodes into pipeline
- mPipeline->AddNode(mRaygenNode);
- mPipeline->AddNode(mRaytraceNode);
- mPipeline->AddNode(mRaytraceHWNode);
- mPipeline->AddNode(mRenderNodesNode);
- mPipeline->AddNode(mMaterialNode);
- mPipeline->AddNode(mShadowRenderNode);
- mPipeline->AddNode(mRenderNode);
- // Register events implemented on this class
- Engine::EventChannel chan(mEventSystem);
- chan.Add<Engine::Window::Resize>(*this);
- }
- /// <summary>
- /// Default virtual destructor
- /// </summary>
- Renderer::~Renderer()
- {
- delete mRenderNode;
- delete mPipeline;
- }
- /// <summary>
- /// Initialize system
- /// </summary>
- /// <returns>True on success, false otherwise</returns>
- bool Renderer::Init()
- {
- mImgui = new Engine::RenderPassImgui(mOptions, mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y);
- ImGui::GetStyle().WindowBorderSize = 0;
- ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
- mGBufferPass = new Engine::GBuffer(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mWireframePass = new Engine::RenderPassWireframe(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mTexturedPass = new Engine::RenderPassTextured(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mNativeResolvePass = new Engine::RenderPassResolveNative(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mMultisampling = new Engine::Multisample(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mResolve = new Engine::RenderPassResolveMultisample(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mCulling = new Engine::Culling(mRenderer, mOptions->Get<int>("Renderer.Culling.MaxBuffers"), mOptions->Get<int>("Scene.MaxObjects"));
- mLightingSystem = new Engine::LightingSystem(mRenderer, mOptions->Get<int>("Renderer.Lighting.MaxLights"), mOptions->Get<int>("Renderer.Lighting.VirtualShadowMapResolution"), mCulling);
- mLightingPass = new Engine::LightingPass(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"), mLightingSystem);
- mRaytracerPass = new Engine::RenderPassRaytracer(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y);
- mHiZPass = new Engine::RenderPassHiZ(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y);
- mRaytracerPassHW = new Engine::RenderPassRaytracerHW(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y);
- mAvgLuminance = new Engine::RenderPassAvgLuminance(mRenderer, mAppState->mWindowSize.x, mAppState->mWindowSize.y, mOptions->Get<int>("Renderer.Antialiasing.SamplesMSAA"));
- mGizmoPass = new Engine::RenderGizmo(mRenderer, 1024);
- mAppState->mRenderNodes = new Engine::RenderNodeList(mRenderer, mOptions->Get<int>("Scene.MaxObjects"), std::vector<Engine::RootSignature*> { mTexturedPass->GetRootSignature(), mLightingSystem->GetRootSignature(), mHiZPass->GetRootSignature() });
- return true;
- }
- /// <summary>
- /// Shutdown system
- /// </summary>
- void Renderer::Shutdown()
- {
- mRenderer->Flush();
- delete mImgui;
- delete mGBufferPass;
- delete mWireframePass;
- delete mTexturedPass;
- delete mNativeResolvePass;
- delete mMultisampling;
- delete mResolve;
- delete mLightingSystem;
- delete mCulling;
- delete mLightingPass;
- delete mRaytracerPass;
- delete mHiZPass;
- delete mRaytracerPassHW;
- delete mAvgLuminance;
- delete mGizmoPass;
- }
- /// <summary>
- /// Update call on system
- /// </summary>
- void Renderer::Update()
- {
- mPipeline->Update();
- }
- /// <summary>
- /// Window resize
- /// </summary>
- /// <param name="r">Window resize event parameter structure</param>
- void Renderer::Handle(const Engine::Window::Resize& r)
- {
- // Resize render pass objects bound to window size
- mImgui->Resize(r.mWidth, r.mHeight);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement