Skip to content

Commit

Permalink
Set viewport on world/skybox render views to avoid rendering under th…
Browse files Browse the repository at this point in the history
…e UI
  • Loading branch information
nekoffski committed Mar 12, 2024
1 parent cf91e27 commit 381c58d
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
[submodule "3rdparty/imgui"]
path = 3rdparty/imgui
url = https://github.com/ocornut/imgui.git
[submodule "3rdparty/ImGuizmo"]
path = 3rdparty/ImGuizmo
url = https://github.com/CedricGuillemet/ImGuizmo.git
9 changes: 6 additions & 3 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ add_subdirectory(expected)
# imgui

set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/imgui)
set(IMGUIZMO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ImGuizmo)

file(GLOB_RECURSE IMGUI_SRC ${IMGUI_DIR}/imgui.cpp ${IMGUI_DIR}/imgui_widgets.cpp ${IMGUI_DIR}/imgui_tables.cpp
${IMGUI_DIR}/imgui_draw.cpp ${IMGUI_DIR}/misc/*.cpp ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp ${IMGUI_DIR}/imgui_demo.cpp )
${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp ${IMGUI_DIR}/imgui_demo.cpp ${IMGUIZMO_DIR}/ImGuizmo.cpp)
add_library(imgui STATIC ${IMGUI_SRC})
target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/imgui)
target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/imgui ${CMAKE_CURRENT_SOURCE_DIR}/ImGuizmo)
set_property(TARGET imgui PROPERTY CXX_STANDARD 20)
target_link_libraries(imgui PUBLIC freetype)
target_include_directories(imgui PUBLIC /usr/include/freetype2) # TODO: generic way
target_include_directories(imgui PUBLIC /usr/include/freetype2) # TODO: generic way


1 change: 1 addition & 0 deletions 3rdparty/ImGuizmo
Submodule ImGuizmo added at ba662b
26 changes: 21 additions & 5 deletions editor/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Application::Application(int argc, char** argv) :
.viewportWidth = w,
.viewportHeight = h,
});
m_activeCamera = m_eulerCamera.get();
m_activeCamera = m_eulerCamera.get();
m_ui.getState()->camera = m_activeCamera;
}

int Application::run() {
Expand Down Expand Up @@ -82,8 +83,8 @@ int Application::run() {
sl::SkyboxRenderView skyboxView{ m_activeCamera, skyboxShader, skybox };

m_views.push_back(&skyboxView);
m_views.push_back(&worldView);
m_views.push_back(&uiView);
m_views.push_back(&worldView);

LOG_INFO("Starlight Editor starting");

Expand All @@ -95,7 +96,9 @@ int Application::run() {
LOG_VAR(deltaTime);
LOG_VAR(m_activeCamera->getPosition());

auto renderPacket = m_scene.getRenderPacket();
auto renderPacket = m_scene.getRenderPacket();
renderPacket.viewport = calculateViewport();

m_renderer.renderFrame(deltaTime, renderPacket);

m_activeCamera->update(deltaTime);
Expand All @@ -121,12 +124,14 @@ void Application::setupEventHandlers() {
if (key == SL_KEY_8) m_renderer.setRenderMode(sl::RenderMode::standard);
if (key == SL_KEY_6) sl::enableVariableLogging();
if (key == SL_KEY_4) {
m_activeCamera = m_eulerCamera.get();
m_activeCamera = m_eulerCamera.get();
m_ui.getState()->camera = m_activeCamera;
m_window->showCursor();
for (auto& view : m_views) view->setCamera(m_activeCamera);
}
if (key == SL_KEY_3) {
m_activeCamera = m_firstPersonCamera.get();
m_activeCamera = m_firstPersonCamera.get();
m_ui.getState()->camera = m_activeCamera;
m_window->hideCursor();
for (auto& view : m_views) view->setCamera(m_activeCamera);
}
Expand Down Expand Up @@ -163,3 +168,14 @@ void Application::setupEventHandlers() {
.on<SceneLoaded>(onSceneLoaded)
.on<SceneSaved>(onSceneSaved);
}

sl::Viewport Application::calculateViewport() const {
const auto [w, h] = m_window->getSize();

return sl::Viewport{
.x = panelWidthFactor * w,
.y = 0.25f * h,
.width = (1.0f - 2.0f * panelWidthFactor) * w,
.height = (0.75f) * h
};
}
2 changes: 2 additions & 0 deletions editor/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Application {
private:
void setupEventHandlers();

sl::Viewport calculateViewport() const;

bool m_isRunning;
bool m_update;

Expand Down
6 changes: 4 additions & 2 deletions editor/ui/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "starlight/core/Core.h"
#include "starlight/core/math/Glm.h"

#include "starlight/renderer/camera/Camera.h"
#include "starlight/ui/UI.h"

enum class ResourceType { mesh = 0, texture, shader, material };
Expand All @@ -17,10 +17,12 @@ struct UIState {
std::optional<ResourceType> selectedResourceType;
std::unordered_map<sl::u64, std::unique_ptr<sl::ui::ImageHandle>> imageHandles;

sl::Camera* camera;

sl::ui::ImageHandle* getOrCreateImageHandle(sl::Texture* texture);
};

static constexpr float leftComboWidthFactor = 0.15f;
static constexpr float panelWidthFactor = 0.15f;

static const sl::Vec3f selectedColor = { 0.1f, 0.7f, 0.1f };
static const sl::Vec3f defaultColor = { 1.0f, 1.0f, 1.0f };
13 changes: 13 additions & 0 deletions editor/ui/EntityInspectorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "starlight/scene/components/All.h"
#include "starlight/resource/All.h"

#include <ImGuizmo.h>

EntityInspectorPanel::EntityInspectorPanel(
sl::Scene* scene, UIState* state, Logger* logger
) :
Expand All @@ -26,6 +28,17 @@ void EntityInspectorPanel::renderEntityUI(sl::u64 entityId) {
auto entity = m_scene->getEntity(entityId);
auto& data = m_entitiesData[entity->getId()];

auto view = m_state->camera->getViewMatrix();
auto projection = m_state->camera->getProjectionMatrix();

ImGuiIO& io = ImGui::GetIO();
ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);

ImGuizmo::DrawGrid(
glm::value_ptr(view), glm::value_ptr(projection),
glm::value_ptr(sl::identityMatrix), 100.f
);

sl::ui::namedScope(fmt::format("Entity_{}", entity->getId()), [&]() {
sl::ui::text("Entity: {}/{}", entity->getId(), entity->getName());
sl::ui::separator();
Expand Down
14 changes: 8 additions & 6 deletions editor/ui/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ UI::UI(sl::u64 w, sl::u64 h, sl::RendererFrontend& renderer, sl::Scene* scene) :
m_leftCombo(
"left-combo",
sl::ui::PanelCombo::Properties{
.position = {0, 0},
.size = { leftComboWidthFactor * w, h},
.position = {0, 0},
.size = { panelWidthFactor * w, h},
.alignWithMainMenuBar = true,
.orientation = sl::ui::PanelCombo::Orientation::vertical
}
),
m_rightCombo(
"right-combo",
sl::ui::PanelCombo::Properties{
.position = { w * (1.0f - leftComboWidthFactor), 0 },
.size = { leftComboWidthFactor * w, h },
.position = { w * (1.0f - panelWidthFactor), 0 },
.size = { panelWidthFactor * w, h },
.alignWithMainMenuBar = true,
.orientation = sl::ui::PanelCombo::Orientation::vertical }
),
m_bottomCombo(
"bottom-combo",
sl::ui::PanelCombo::Properties{
.position = { leftComboWidthFactor * w, (1.0f - 0.25f) * h },
.size = { (1.0f - leftComboWidthFactor * 2) * w, h * 0.25f },
.position = { panelWidthFactor * w, (1.0f - 0.25f) * h },
.size = { (1.0f - panelWidthFactor * 2) * w, h * 0.25f },
.alignWithMainMenuBar = true,
.orientation = sl::ui::PanelCombo::Orientation::horizontal }

Expand Down Expand Up @@ -98,4 +98,6 @@ void UI::render() {

bool UI::shouldExit() const { return m_shouldExit; }

UIState* UI::getState() { return &m_state; }

Logger* UI::getLogger() { return m_logger; }
1 change: 1 addition & 0 deletions editor/ui/UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class UI {
void render();
bool shouldExit() const;

UIState* getState();
Logger* getLogger();

private:
Expand Down
2 changes: 2 additions & 0 deletions engine/src/starlight/renderer/RenderPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Material.h"
#include "PointLight.h"
#include "Viewport.h"

namespace sl {

Expand All @@ -18,6 +19,7 @@ struct RenderEntity {
struct RenderPacket {
std::vector<RenderEntity> entities;
std::vector<PointLight> pointLights;
Viewport viewport;
};

} // namespace sl
12 changes: 12 additions & 0 deletions engine/src/starlight/renderer/Viewport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace sl {

struct Viewport {
float x;
float y;
float width;
float height;
};

} // namespace sl
26 changes: 18 additions & 8 deletions engine/src/starlight/renderer/gpu/RendererBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@
#include "starlight/core/math/Vertex.h"
#include "starlight/core/math/Vertex.h"

#include "starlight/renderer/gpu/Mesh.h"
#include "starlight/renderer/fwd.h"
#include "starlight/renderer/Material.h"
#include "starlight/renderer/Viewport.h"
#include "starlight/renderer/gpu/Mesh.h"
#include "starlight/renderer/gpu/Texture.h"
#include "starlight/renderer/gpu/Shader.h"
#include "starlight/renderer/fwd.h"
#include "starlight/renderer/gpu/CommandBuffer.h"
#include "starlight/renderer/Viewport.h"

#include "RendererBackendProxy.h"

namespace sl {

// TODO: change to objects or something
constexpr int builtinRenderPassWorld = 1;
constexpr int builtinRenderPassUI = 2;

struct RendererBackend {
virtual ~RendererBackend() = default;

virtual bool beginFrame(float deltaTime) = 0;
virtual bool endFrame(float deltaTime) = 0;
template <typename C>
requires Callable<C> u64 renderFrame(float deltaTime, C&& callback) {
if (beginFrame(deltaTime)) {
callback();
endFrame(deltaTime);
}
return getRenderedVertexCount();
}

virtual void setViewport(const Viewport& viewport) = 0;

virtual u64 getRenderedVertexCount() const = 0;
virtual bool beginFrame(float deltaTime) = 0;
virtual bool endFrame(float deltaTime) = 0;

virtual void drawMesh(const Mesh& mesh) = 0;

Expand Down
2 changes: 2 additions & 0 deletions engine/src/starlight/renderer/gpu/RendererBackendProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Mesh.h"

#include "starlight/core/memory/Memory.hpp"
#include "starlight/renderer/Viewport.h"

#include "UIRenderer.h"

Expand All @@ -18,6 +19,7 @@ struct RendererBackendProxy {
virtual Texture* getDepthBuffer() = 0;
virtual void gpuCall(std::function<void(CommandBuffer&)>&&) = 0;
virtual UniqPtr<UIRenderer> createUIRendererer(RenderPass* renderPass) = 0;
virtual void setViewport(const Viewport& viewport) = 0;
};

} // namespace sl
75 changes: 46 additions & 29 deletions engine/src/starlight/renderer/gpu/vulkan/VKRendererBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ void VKRendererBackend::onViewportResize(u32 width, u32 height) {
m_recreatingSwapchain = false;
}

u64 VKRendererBackend::getRenderedVertexCount() const { return m_renderedVertices; }

void VKRendererBackend::setViewport(const Viewport& viewport) {
setViewport(m_commandBuffers[m_imageIndex], viewport);
}

void VKRendererBackend::createCommandBuffers() {
const auto swapchainImagesCount = m_swapchain->getImageCount();
m_commandBuffers.clear();
Expand All @@ -187,33 +193,6 @@ void VKRendererBackend::recreateSwapchain() {
LOG_INFO("Resized, booting.");
}

void VKRendererBackend::recordCommands(VKCommandBuffer& commandBuffer) {
commandBuffer.reset();
commandBuffer.begin(VKCommandBuffer::BeginFlags{
.isSingleUse = false,
.isRenderpassContinue = false,
.isSimultaneousUse = false,
});

// Dynamic state
VkViewport viewport;
viewport.x = 0.0f;
viewport.y = (float)m_framebufferHeight;
viewport.width = (float)m_framebufferWidth;
viewport.height = -(float)m_framebufferHeight;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;

// Scissor
VkRect2D scissor;
scissor.offset.x = scissor.offset.y = 0;
scissor.extent.width = m_framebufferWidth;
scissor.extent.height = m_framebufferHeight;

vkCmdSetViewport(commandBuffer.getHandle(), 0, 1, &viewport);
vkCmdSetScissor(commandBuffer.getHandle(), 0, 1, &scissor);
}

VKFence* VKRendererBackend::acquireImageFence() {
auto& fence = m_imagesInFlight[m_imageIndex];
if (fence) fence->wait(UINT64_MAX);
Expand All @@ -233,6 +212,29 @@ Texture* VKRendererBackend::getDepthBuffer() {

VKRendererBackendProxy* VKRendererBackend::getProxy() { return &m_proxy; }

void VKRendererBackend::setViewport(
VKCommandBuffer& commandBuffer, const Viewport& viewport
) {
VkViewport vkViewport;
vkViewport.x = viewport.x;
vkViewport.y = viewport.height;
vkViewport.width = viewport.width;
vkViewport.height = -viewport.height;
vkViewport.minDepth = 0.0f;
vkViewport.maxDepth = 1.0f;

vkCmdSetViewport(commandBuffer.getHandle(), 0, 1, &vkViewport);
}

void VKRendererBackend::setScissors(VKCommandBuffer& commandBuffer) {
VkRect2D scissor;
scissor.offset.x = scissor.offset.y = 0;
scissor.extent.width = m_framebufferWidth;
scissor.extent.height = m_framebufferHeight;

vkCmdSetScissor(commandBuffer.getHandle(), 0, 1, &scissor);
}

bool VKRendererBackend::beginFrame(float deltaTime) {
m_renderedVertices = 0u;
const auto logicalDevice = m_device->getLogicalDevice();
Expand Down Expand Up @@ -269,9 +271,24 @@ bool VKRendererBackend::beginFrame(float deltaTime) {

// Begin recording commands.
auto& commandBuffer = m_commandBuffers[m_imageIndex];
recordCommands(commandBuffer);

// m_mainRenderPass->setAreaSize(m_framebufferWidth, m_framebufferHeight);
commandBuffer.reset();
commandBuffer.begin(VKCommandBuffer::BeginFlags{
.isSingleUse = false,
.isRenderpassContinue = false,
.isSimultaneousUse = false,
});

setViewport(
commandBuffer,
Viewport{
0.0f,
0.0f,
static_cast<float>(m_framebufferWidth),
static_cast<float>(m_framebufferHeight),
}
);
setScissors(commandBuffer);

return true;
}
Expand Down
Loading

0 comments on commit 381c58d

Please sign in to comment.