Skip to content
Open
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
5 changes: 3 additions & 2 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <stdexcept>

#include "IController.h"
#include "core/Controller.h"

int main([[maybe_unused]] int argc, [[maybe_unused]] char* args[]) {
try {
const auto controller = createInstance();
controller->init();
const auto controller = createController();
controller->run();
} catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception: " << e.what() << '\n';
}
Expand Down
2 changes: 1 addition & 1 deletion include/IController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IController {
*
* This method initializes the necessary components for the controller.
*/
virtual void init() const = 0;
virtual void run() const = 0;

/*!
* \brief Updates the controller status.
Expand Down
1 change: 0 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ target_sources(${PROJECT_NAME}
ModelImpl.cpp
View.cpp
ViewImpl.cpp
createInstance.cpp
)
4 changes: 2 additions & 2 deletions src/core/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

#include "core/ControllerImpl.h"

IController::Ptr createController(IModel::Ptr ptr) {
return std::make_shared<ControllerImpl>(std::move(ptr));
IController::Ptr createController() {
return std::make_shared<ControllerImpl>();
}
53 changes: 48 additions & 5 deletions src/core/ControllerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
#include <chrono>
#include <cmath>
#include <string>
#include <thread>
#include <utility>

#include <SDL_events.h>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
#include <imgui_impl_sdl2.h>

#include "core/Model.h"
#include "core/View.h"
#include "scene/Camera.h"

ControllerImpl::ControllerImpl(IModel::Ptr model) : _model(std::move(model)) {}
ControllerImpl::ControllerImpl() : _view(createView()), _model(createModel()) {}

double getCurrentGlobalTime() {
// Get the current time point
Expand Down Expand Up @@ -56,8 +60,47 @@ void ControllerImpl::processEvent(SDL_Event &e) const {
_model->getCamera()->processSDLEvent(e);
}

void ControllerImpl::init() const {
const auto controller = shared_from_this();
const auto view = createView(controller, _model);
view->run();
void ControllerImpl::createCubes() const {
for (int i = 0; i < 5; i++) {
_model->createMesh("cube" + std::to_string(i));
}
}

void ControllerImpl::run() const {
createCubes();

SDL_Event e;
bool bQuit = false;
bool stop_rendering = false;

// main loop
while (!bQuit) {
// Handle events on queue
while (SDL_PollEvent(&e) != 0) {
// close the window when user alt-f4s or clicks the X button
if (e.type == SDL_QUIT) bQuit = true;

if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
stop_rendering = true;
}
if (e.window.event == SDL_WINDOWEVENT_RESTORED) {
stop_rendering = false;
}
}

processEvent(e);
ImGui_ImplSDL2_ProcessEvent(&e);
}

// do not draw if we are minimized
if (stop_rendering) {
// throttle the speed to avoid the endless spinning
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}

_view->render();
update();
}
}
14 changes: 2 additions & 12 deletions src/core/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Mesh.cpp
#include "core/Mesh.h"

#include "graphics/vulkan/vk_engine.h"

Mesh::Mesh(const std::string &filePath) : _currentModelPath(filePath) {
VulkanEngine &engine = VulkanEngine::Get();
_rid = engine.registerMesh(filePath);
}

Mesh::~Mesh() {
remove_model();
}

void Mesh::set_model(const std::string &filePath) {
VulkanEngine &engine = VulkanEngine::Get();

Expand All @@ -26,14 +24,6 @@ void Mesh::set_model(const std::string &filePath) {
engine.setMeshTransform(_rid, _transform);
}

void Mesh::remove_model() {
if (!_currentModelPath.empty()) {
VulkanEngine &engine = VulkanEngine::Get();
engine.unregisterMesh(_rid);
_currentModelPath.clear();
}
}


void Mesh::set_transform(glm::mat4 t) {
VulkanEngine &engine = VulkanEngine::Get();
Expand Down
18 changes: 4 additions & 14 deletions src/core/ModelImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#include "core/ModelImpl.h"

#include <SDL_video.h>

#include "core/Mesh.h"
#include "graphics/vulkan/vk_engine.h"

ModelImpl::~ModelImpl() {
_engine.cleanup();
}

ModelImpl::ModelImpl() = default;

void ModelImpl::registerWindow(SDL_Window *window) {
_engine.mainCamera = &_camera;
_engine.init(window);
}
ModelImpl::ModelImpl()
: _camera(), _engine(std::make_shared<VulkanEngine>(_camera)) {};

void ModelImpl::updateVulkan() {
_engine.update();
_engine->update();
}

Camera *ModelImpl::getCamera() {
Expand Down
4 changes: 2 additions & 2 deletions src/core/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

#include "core/ViewImpl.h"

IView::Ptr createView(IController::Ptr controller, IModel::Ptr model) {
return std::make_unique<ViewImpl>(std::move(controller), std::move(model));
IView::Ptr createView() {
return std::make_unique<ViewImpl>();
}
100 changes: 16 additions & 84 deletions src/core/ViewImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,33 @@

#include <SDL2/SDL.h>
#include <SDL_events.h>
#include <SDL_video.h>

#ifdef __GNUC__
#include <bits/chrono.h>
#else
#include <chrono>
#endif

#include <memory>
#include <string>
#include <thread>
#include <utility>

#include "graphics/vulkan/vk_engine.h"
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_vulkan.h"

namespace {
constexpr auto kWindowFlags =
static_cast<uint32_t>(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
}

ViewImpl::ViewImpl(IController::Ptr controller, IModel::Ptr model)
: _controller(std::move(controller)), _model(std::move(model)) {
ViewImpl::ViewImpl() {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("engine", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 1700, 900, kWindowFlags);
}

void createCubes(const std::shared_ptr<IModel> &_model) {
for (int i = 0; i < 5; i++) {
_model->createMesh("cube" + std::to_string(i));
}
}

void ViewImpl::run() const {
_model->registerWindow(window);

createCubes(_model);

SDL_Event e;
bool bQuit = false;
struct EventAdapter final : Event {
explicit EventAdapter(const SDL_Event& event) : event(event) {}
SDL_Event event;
};

bool stop_rendering = false;
void ViewImpl::render() {
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();

// main loop
while (!bQuit) {
// Handle events on queue
while (SDL_PollEvent(&e) != 0) {
// close the window when user alt-f4s or clicks the X button
if (e.type == SDL_QUIT) bQuit = true;

if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
stop_rendering = true;
}
if (e.window.event == SDL_WINDOWEVENT_RESTORED) {
stop_rendering = false;
}
}

_controller->processEvent(e);
ImGui_ImplSDL2_ProcessEvent(&e);
}

// do not draw if we are minimized
if (stop_rendering) {
// throttle the speed to avoid the endless spinning
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}

// imgui new frame
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();

if (ImGui::Begin("background")) {
VulkanEngine &engine = VulkanEngine::Get();
ImGui::SliderFloat("Render Scale", &engine.renderScale, 0.3f, 1.f);
// other code
}
ImGui::End();

// make imgui calculate internal draw structures
ImGui::Render();

_controller->update();
//_model->updateVulkan();

// engine.update();
if (ImGui::Begin("background")) {
VulkanEngine &engine = VulkanEngine::Get();
ImGui::SliderFloat("Render Scale", &engine.renderScale, 0.3f, 1.f);
// other code
}
}
ImGui::End();

ViewImpl::~ViewImpl() {
SDL_DestroyWindow(window);
// make imgui calculate internal draw structures
ImGui::Render();
}
9 changes: 0 additions & 9 deletions src/core/createInstance.cpp

This file was deleted.

3 changes: 3 additions & 0 deletions src/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ target_sources(${PROJECT_NAME}
vulkan/vk_pipelines.cpp
vulkan/pipelines.cpp
vulkan/ComputePipeline.cpp
vulkan/DescriptorSetLayout.cpp
vulkan/Device.cpp
vulkan/GraphicsPipeline.cpp
vulkan/VulkanInit.cpp
Graphics.cpp
)
18 changes: 18 additions & 0 deletions src/graphics/vulkan/DescriptorSetLayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "graphics/vulkan/DescriptorSetLayout.h"

#include "graphics/vulkan/vk_descriptors.h"

void DescriptorSetLayout::create(
const VkDevice pDevice, const VkShaderStageFlags pShaderStages,
std::initializer_list<std::pair<uint32_t, VkDescriptorType>> bindings) {
device = pDevice;
DescriptorLayoutBuilder builder;
for (const auto& [binding, type] : bindings) {
builder.add_binding(binding, type);
}
set = builder.build(pDevice, pShaderStages);
}

DescriptorSetLayout::~DescriptorSetLayout() {
vkDestroyDescriptorSetLayout(device, set, nullptr);
}
46 changes: 46 additions & 0 deletions src/graphics/vulkan/Device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "graphics/vulkan/Device.h"

#include "core/Logging.h"

Device::Device(const vkb::Instance& instance, const VkSurfaceKHR surface)
: physical(VK_NULL_HANDLE), logical([&] {
VkPhysicalDeviceVulkan13Features features{
.synchronization2 = true,
.dynamicRendering = true,
};

VkPhysicalDeviceVulkan12Features features12{
.descriptorIndexing = true,
.bufferDeviceAddress = true,
};

// use vkbootstrap to select a gpu.
// We want a gpu that can write to the SDL surface and supports
// vulkan 1.3 with the correct features
vkb::PhysicalDeviceSelector selector{instance};

auto res = selector.set_minimum_version(1, 3)
.set_required_features_13(features)
.set_required_features_12(features12)
.set_surface(surface)
.select();
if (!res) {
LOGE("Failed to select physical device. Error: {}",
res.error().message());
}

const vkb::PhysicalDevice& physical_device = res.value();

const vkb::DeviceBuilder device_builder{physical_device};
auto dv_res = device_builder.build();
if (!dv_res) {
LOGE("Failed to create logical device. Error: {}",
dv_res.error().message());
}
physical = physical_device.physical_device;
return dv_res.value();
}()) {}

Device::~Device() {
vkb::destroy_device(logical);
}
Loading
Loading