Skip to content

Commit

Permalink
Support configurable polygon modes for shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
nekoffski committed Mar 12, 2024
1 parent 381c58d commit 58e351e
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 72 deletions.
1 change: 1 addition & 0 deletions assets/shaders/Builtin.Shader.Material.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": 1.0,
"name": "Builtin.Shader.Material",
"cullMode": "back",
"polygonMode": "fill",
"stages": [
{ "stage": "vertex", "file": "Builtin.Shader.Material.vert.spv" },
{ "stage": "fragment", "file": "Builtin.Shader.Material.frag.spv" }
Expand Down
26 changes: 15 additions & 11 deletions editor/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ Application::Application(int argc, char** argv) :
.viewportWidth = w,
.viewportHeight = h,
});
m_activeCamera = m_eulerCamera.get();
m_ui.getState()->camera = m_activeCamera;
m_activeCamera = m_eulerCamera.get();

calculateViewport();

m_ui.getState()->camera = m_activeCamera;
m_ui.getState()->viewport = &m_viewport;
}

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

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

LOG_INFO("Starlight Editor starting");

Expand All @@ -97,7 +101,7 @@ int Application::run() {
LOG_VAR(m_activeCamera->getPosition());

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

m_renderer.renderFrame(deltaTime, renderPacket);

Expand Down Expand Up @@ -148,6 +152,8 @@ void Application::setupEventHandlers() {
m_renderer.onViewportResize(w, h);
m_eulerCamera->onViewportResize(w, h);
m_firstPersonCamera->onViewportResize(w, h);

calculateViewport();
};

const auto onSceneLoaded = [&](SceneLoaded* event) {
Expand All @@ -169,13 +175,11 @@ void Application::setupEventHandlers() {
.on<SceneSaved>(onSceneSaved);
}

sl::Viewport Application::calculateViewport() const {
void Application::calculateViewport() {
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
};
m_viewport.x = panelWidthFactor * w;
m_viewport.y = 0.25f * h;
m_viewport.width = (1.0f - 2.0f * panelWidthFactor) * w;
m_viewport.height = (0.75f) * h;
}
3 changes: 2 additions & 1 deletion editor/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class Application {

private:
void setupEventHandlers();
void calculateViewport();

sl::Viewport calculateViewport() const;
sl::Viewport m_viewport;

bool m_isRunning;
bool m_update;
Expand Down
5 changes: 5 additions & 0 deletions editor/ui/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "starlight/core/Core.h"
#include "starlight/core/math/Glm.h"
#include "starlight/renderer/camera/Camera.h"
#include "starlight/renderer/Viewport.h"
#include "starlight/ui/UI.h"

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

sl::Camera* camera;
sl::Viewport* viewport;

bool showGrid = true;
float gridSize = 25.0f;

sl::ui::ImageHandle* getOrCreateImageHandle(sl::Texture* texture);
};
Expand Down
11 changes: 0 additions & 11 deletions editor/ui/EntityInspectorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ 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
78 changes: 38 additions & 40 deletions editor/ui/ScenePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,44 @@

ScenePanel::ScenePanel(sl::Scene* scene, UIState* state, Logger* logger) :
m_scene(scene), m_state(state), m_logger(logger), m_tabs("scene-tabs") {
m_tabs
.addTab(
ICON_FA_SITEMAP " Scene graph",
[&]() {
if (sl::ui::button("Add Entity")) {
m_logger->debug("Adding new entity");
m_scene->addEntity();
}
sl::ui::separator();
sl::ui::treeNode(
ICON_FA_CUBE " Root",
[&]([[maybe_unused]] bool clicked) {
m_scene->forEachEntity(
[&](const std::string& name, sl::Entity* entity) -> void {
auto& selectedEntityId = m_state->selectedEntityId;

const auto entityId = entity->getId();
const auto color =
selectedEntityId && *selectedEntityId == entityId
? selectedColor
: defaultColor;

sl::ui::withColor(color, [&]() {
const auto title =
fmt::format("\t{} {}", ICON_FA_CUBE, name);

if (sl::ui::text(title)) {
m_logger->debug(
"Selected entity with id={}", entityId
);
selectedEntityId = entityId;
}
});
}
);
}
);
}
)
.addTab(ICON_FA_CLOUD " Skybox", [&]() {});
m_tabs.addTab(
ICON_FA_SITEMAP " Scene graph", [&]() { renderSceneGraph(); }
).addTab(ICON_FA_WRENCH " Scene Settings", [&]() { renderSceneSettings(); });
}

void ScenePanel::render() { m_tabs.render(); }

void ScenePanel::renderSceneGraph() {
if (sl::ui::button("Add Entity")) {
m_logger->debug("Adding new entity");
m_scene->addEntity();
}
sl::ui::separator();
sl::ui::treeNode(ICON_FA_CUBE " Root", [&]([[maybe_unused]] bool clicked) {
m_scene->forEachEntity(
[&](const std::string& name, sl::Entity* entity) -> void {
auto& selectedEntityId = m_state->selectedEntityId;

const auto entityId = entity->getId();
const auto color =
selectedEntityId && *selectedEntityId == entityId
? selectedColor
: defaultColor;

sl::ui::withColor(color, [&]() {
const auto title = fmt::format("\t{} {}", ICON_FA_CUBE, name);

if (sl::ui::text(title)) {
m_logger->debug("Selected entity with id={}", entityId);
selectedEntityId = entityId;
}
});
}
);
});
}

void ScenePanel::renderSceneSettings() {
sl::ui::checkbox("Show grid", m_state->showGrid);
sl::ui::separator();
}
3 changes: 3 additions & 0 deletions editor/ui/ScenePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class ScenePanel {
void render();

private:
void renderSceneGraph();
void renderSceneSettings();

sl::Scene* m_scene;
UIState* m_state;
Logger* m_logger;
Expand Down
29 changes: 29 additions & 0 deletions engine/src/starlight/renderer/PolygonMode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "PolygonMode.h"

#include "starlight/core/utils/Log.h"

namespace sl {

std::string polygonModeToString(PolygonMode polygonMode) {
switch (polygonMode) {
case PolygonMode::fill:
return "fill";
case PolygonMode::line:
return "line";
case PolygonMode::point:
return "point";
}
__builtin_unreachable();
}

PolygonMode polygonModeFromString(const std::string& polygonName) {
if (polygonName == "line")
return PolygonMode::line;
else if (polygonName == "fill")
return PolygonMode::fill;
else if (polygonName == "point")
return PolygonMode::point;
FATAL_ERROR("Could not parse polygon mode: {}", polygonName);
}

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

#include "starlight/core/Core.h"

namespace sl {

enum class PolygonMode : u8 { fill, line, point };

std::string polygonModeToString(PolygonMode polygonMode);
PolygonMode polygonModeFromString(const std::string& polygonName);

} // namespace sl
4 changes: 2 additions & 2 deletions engine/src/starlight/renderer/gpu/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ const std::string& Shader::getName() const { return m_name; }

Shader::Shader(const Properties& props, u32 id) :
m_name(props.name), m_id(id), m_useInstances(props.useInstances),
m_useLocals(props.useLocals), m_uniformProxy(*this), m_cullMode(props.cullMode) {
}
m_useLocals(props.useLocals), m_uniformProxy(*this), m_cullMode(props.cullMode),
m_polygonMode(props.polygonMode) {}

void Shader::UniformProxy::set(const std::string& uniform, const Texture* value) {
m_shader.setSampler(uniform, value);
Expand Down
3 changes: 3 additions & 0 deletions engine/src/starlight/renderer/gpu/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "starlight/core/Core.h"
#include "starlight/core/utils/Log.h"
#include "starlight/renderer/CullMode.h"
#include "starlight/renderer/PolygonMode.h"
#include "fwd.h"

#include "Texture.h"
Expand Down Expand Up @@ -106,6 +107,7 @@ class Shader {
Texture* defaultTexture;

CullMode cullMode;
PolygonMode polygonMode;
};

class UniformProxy {
Expand Down Expand Up @@ -163,6 +165,7 @@ class Shader {
UniformProxy m_uniformProxy;

CullMode m_cullMode;
PolygonMode m_polygonMode;

private:
virtual void bindGlobals() = 0;
Expand Down
5 changes: 3 additions & 2 deletions engine/src/starlight/renderer/gpu/vulkan/VKPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ class VKPipeline {
// Dynamic state
const uint32_t dynamic_state_count = 3;
VkDynamicState dynamic_states[dynamic_state_count] = {
VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_LINE_WIDTH
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_LINE_WIDTH,
};

VkPipelineDynamicStateCreateInfo dynamic_state_create_info = {
Expand Down
8 changes: 7 additions & 1 deletion engine/src/starlight/renderer/gpu/vulkan/VKShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ void VKShader::createDescriptorSetLayouts() {
}
}

static std::unordered_map<PolygonMode, VkPolygonMode> vkPolygonModes = {
{PolygonMode::fill, VK_POLYGON_MODE_FILL },
{ PolygonMode::line, VK_POLYGON_MODE_LINE },
{ PolygonMode::point, VK_POLYGON_MODE_POINT},
};

void VKShader::createPipeline(RenderPass* renderPass) {
// viewport & scissor
const auto [w, h] = WindowManager::get().getSize();
Expand Down Expand Up @@ -685,7 +691,7 @@ void VKShader::createPipeline(RenderPass* renderPass) {
pipelineProps.stages = stageCreateInfos;
pipelineProps.viewport = viewport;
pipelineProps.scissor = scissor;
pipelineProps.polygonMode = VK_POLYGON_MODE_FILL;
pipelineProps.polygonMode = vkPolygonModes.at(m_polygonMode);
pipelineProps.depthTestEnabled = true;
pipelineProps.cullMode = m_cullMode;

Expand Down
3 changes: 2 additions & 1 deletion engine/src/starlight/resource/configs/ShaderConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ std::optional<ShaderConfig> ShaderConfig::load(
.stages = processStages(getArray(root, "stages"), shadersPath, fs),
.uniformProperties = processUniforms(getArray(root, "uniforms")),
.defaultTexture = defaultTexture,
.cullMode = cullModeFromString(getFieldOr<std::string>(root, "cullMode", "back"))
.cullMode = cullModeFromString(getFieldOr<std::string>(root, "cullMode", "back")),
.polygonMode = polygonModeFromString(getFieldOr<std::string>(root, "polygonMode", "fill"))
}
};
// clang-format on
Expand Down
6 changes: 3 additions & 3 deletions engine/src/starlight/ui/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace sl::ui {

// auto foo() {
// ImGui::PushTex
// }
void checkbox(const std::string& label, bool& value) {
ImGui::Checkbox(label.c_str(), &value);
}

void pushFont(Font* font) { ImGui::PushFont(font->handle); }
void popFont() { ImGui::PopFont(); }
Expand Down
5 changes: 5 additions & 0 deletions engine/src/starlight/ui/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <imgui.h>
#include <fmt/core.h>

#include "starlight/renderer/Viewport.h"
#include "starlight/renderer/camera/Camera.h"

#include "starlight/core/math/Glm.h"
#include "starlight/renderer/gpu/UIRenderer.h"

Expand All @@ -13,6 +16,8 @@ namespace sl::ui {
using Callback = std::function<void()>; // TODO: measure if it causes bottlenecks ->
// if yes implement lightweight wrapper

void checkbox(const std::string& label, bool& value);

void pushFont(Font*);
void popFont();

Expand Down

0 comments on commit 58e351e

Please sign in to comment.