From 501f2612e6dc25d03784b91e4a5eba3c44d227e7 Mon Sep 17 00:00:00 2001 From: AmyFoxie Date: Sat, 4 Nov 2023 15:12:26 +0100 Subject: [PATCH] fix compilation errors --- .github/workflows/cmake-multi-platform.yml | 4 +- .idea/QtSettings.xml | 8 ++ CMakeLists.txt | 3 +- editor/CMakeLists.txt | 2 +- editor/main.cpp | 5 +- editor/widgets/GlViewport.cpp | 38 --------- editor/widgets/GlViewport.h | 28 ------- editor/widgets/GlViewportRenderer.cpp | 93 --------------------- editor/widgets/GlViewportRenderer.h | 54 ------------- engine/Buffer.cpp | 2 +- engine/Buffer.h | 10 +-- engine/ShaderModule.h | 1 + engine/Window.cpp | 2 +- engine/Window.h | 5 +- engine/gl/GlBuffer.cpp | 94 +++++++++++++--------- engine/gl/GlBuffer.h | 24 +++--- engine/gl/GlRenderer.cpp | 6 +- engine/gl/GlRenderer.h | 3 +- engine/gl/GlShaderModule.cpp | 41 +--------- engine/gl/GlShaderModule.h | 5 +- engine/gl/GlShaderProgram.cpp | 16 ++-- engine/gl/GlShaderProgram.h | 8 +- engine/gl/GlVertexArrayObject.cpp | 8 +- engine/gl/GlVertexArrayObject.h | 3 +- engine/gl/GlVixen.cpp | 4 +- engine/gl/GlVixen.h | 2 +- engine/gl/GlWindow.cpp | 8 +- engine/gl/GlWindow.h | 14 +--- engine/gl/test/main.cpp | 36 ++++----- engine/vk/CMakeLists.txt | 2 +- 30 files changed, 145 insertions(+), 384 deletions(-) delete mode 100644 editor/widgets/GlViewport.cpp delete mode 100644 editor/widgets/GlViewport.h delete mode 100644 editor/widgets/GlViewportRenderer.cpp delete mode 100644 editor/widgets/GlViewportRenderer.h diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index d1cb994..a624df3 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -2,9 +2,9 @@ name: CMake on multiple platforms on: push: - branches: [ "main" ] + branches: [ "main", "staging" ] pull_request: - branches: [ "main" ] + branches: [ "main", "staging" ] jobs: build: diff --git a/.idea/QtSettings.xml b/.idea/QtSettings.xml index 21aca9c..a5b87f0 100644 --- a/.idea/QtSettings.xml +++ b/.idea/QtSettings.xml @@ -12,6 +12,14 @@ + + + + + + diff --git a/CMakeLists.txt b/CMakeLists.txt index b74c9f9..b5fd576 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.25) project("Vixen") set(CMAKE_INCLUDE_CURRENT_DIR ON) @@ -16,6 +16,7 @@ endif () set(SKIP_INSTALL_ALL ON) +include(FetchContent) find_package(PkgConfig REQUIRED) option(ENABLE_VULKAN "Enable or disable Vulkan support" ON) diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt index b4b0038..6006a19 100644 --- a/editor/CMakeLists.txt +++ b/editor/CMakeLists.txt @@ -4,7 +4,7 @@ find_package(Qt6 REQUIRED COMPONENTS Core Qml Widgets Quick) qt_standard_project_setup() qt_policy(SET QTP0001 NEW) -add_executable(editor main.cpp widgets/GlViewport.cpp widgets/GlViewport.h widgets/GlViewportRenderer.cpp widgets/GlViewportRenderer.h) +add_executable(editor main.cpp) target_include_directories(editor PUBLIC ../engine) target_link_libraries(editor PRIVATE Vixen Qt6::Core Qt6::Qml Qt6::Quick Qt6::Widgets) diff --git a/editor/main.cpp b/editor/main.cpp index f104247..937e762 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -2,9 +2,6 @@ #include #include #include -#include "widgets/GlViewport.h" - -using namespace Vixen::Editor; int main(int argc, char **argv) { spdlog::set_level(spdlog::level::trace); @@ -14,7 +11,7 @@ int main(int argc, char **argv) { QCoreApplication::setApplicationName("Vixen Editor"); QCoreApplication::setOrganizationName("Vixen"); - qmlRegisterType("Vixen", 1, 0, "GlViewport"); + //qmlRegisterType("Vixen", 1, 0, "GlViewport"); QQmlApplicationEngine engine; engine.load(u"qrc:/editor/ui/main.qml"_qs); diff --git a/editor/widgets/GlViewport.cpp b/editor/widgets/GlViewport.cpp deleted file mode 100644 index 8c9a2d6..0000000 --- a/editor/widgets/GlViewport.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "GlViewport.h" - -namespace Vixen::Editor { - GlViewport::GlViewport(QQuickItem *parent) : QQuickItem(parent), renderer(nullptr) { - connect(this, &QQuickItem::windowChanged, this, &GlViewport::handleWindowChanged); - } - - void GlViewport::handleWindowChanged(QQuickWindow *window) const { - if (window) { - connect(window, &QQuickWindow::beforeSynchronizing, this, &GlViewport::sync, Qt::DirectConnection); - connect(window, &QQuickWindow::sceneGraphInvalidated, this, &GlViewport::cleanup, Qt::DirectConnection); - - window->setColor(Qt::black); - } - } - - void GlViewport::sync() { - if (!renderer) { - renderer = std::make_unique(); - connect(window(), &QQuickWindow::beforeRendering, renderer.get(), &GlViewportRenderer::init, - Qt::DirectConnection); - connect(window(), &QQuickWindow::beforeRenderPassRecording, renderer.get(), &GlViewportRenderer::paint, - Qt::DirectConnection); - } - - renderer->setViewportSize(window()->size() * window()->devicePixelRatio()); - renderer->setWindow(window()); - } - - void GlViewport::cleanup() { - renderer = nullptr; - } - - void GlViewport::releaseResources() { - //window()->scheduleRenderJob(new CleanupJob(renderer), QQuickWindow::BeforeSynchronizingStage); - renderer = nullptr; - } -} diff --git a/editor/widgets/GlViewport.h b/editor/widgets/GlViewport.h deleted file mode 100644 index 9c6f6f6..0000000 --- a/editor/widgets/GlViewport.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include -#include "GlViewportRenderer.h" - -namespace Vixen::Editor { - class GlViewport : public QQuickItem { - Q_OBJECT - - void releaseResources() override; - - public: - explicit GlViewport(QQuickItem *parent = nullptr); - - public slots: - - void sync(); - - void cleanup(); - - private slots: - void handleWindowChanged(QQuickWindow *window) const; - - private: - std::unique_ptr renderer; - }; -} diff --git a/editor/widgets/GlViewportRenderer.cpp b/editor/widgets/GlViewportRenderer.cpp deleted file mode 100644 index b1d2c4c..0000000 --- a/editor/widgets/GlViewportRenderer.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "GlViewportRenderer.h" - -bool glewInitialized = false; - -namespace Vixen::Editor { - void GlViewportRenderer::setViewportSize(const QSize &size) { - viewportSize = size; - } - - void GlViewportRenderer::setWindow(QQuickWindow *win) { - window = win; - } - - void GlViewportRenderer::init() { - if (!glewInitialized) { - glewInitialized = true; - glewExperimental = GL_TRUE; - auto result = glewInit(); - if (GLEW_OK != result) { - spdlog::error("Failed to initialize glew: {}", - reinterpret_cast(glewGetErrorString(result))); - throw std::runtime_error("Failed to initialize glew"); - } - -#ifdef DEBUG - if (GLEW_ARB_debug_output) { - spdlog::debug("Enabling OpenGL debug extension"); - glEnable(GL_DEBUG_OUTPUT); - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glDebugMessageCallback(glDebugCallback, nullptr); - glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, GL_DONT_CARE, 0, nullptr, GL_TRUE); - } -#endif - } - - if (!program) { - std::ifstream vertexStream("../../editor/shaders/triangle.vert"); - std::string vertexSource((std::istreambuf_iterator(vertexStream)), std::istreambuf_iterator()); - auto vertexModule = std::make_shared(Vixen::Vk::ShaderModule::Stage::VERTEX, - vertexSource); - - std::ifstream fragmentStream("../../editor/shaders/triangle.frag"); - std::string fragmentSource((std::istreambuf_iterator(fragmentStream)), - std::istreambuf_iterator()); - auto fragmentModule = std::make_shared(Vixen::Vk::ShaderModule::Stage::FRAGMENT, - fragmentSource); - - program = std::make_shared( - std::vector>{vertexModule, fragmentModule}); - } - - if (!vbo) { - vbo = std::make_shared( - vertices.size() * sizeof(glm::vec3) + indices.size() * sizeof(std::uint32_t), - Vixen::Vk::BufferUsage::VERTEX | Vixen::Vk::BufferUsage::INDEX, - Vixen::Vk::AllocationUsage::GPU_ONLY - ); - vbo->write(vertices, 0); - vbo->write(indices, vertices.size() * sizeof(glm::vec3)); - } - - if (!vao) { - vao = std::make_shared( - std::vector{ - VertexBinding( - vbo, - { - VertexBinding::Location(0, 3, GL_FLOAT, GL_FALSE, 0, sizeof(glm::vec3)) - } - ) - }, - vertices.size() * sizeof(glm::vec3) - ); - } - } - - void GlViewportRenderer::paint() { - window->beginExternalCommands(); - - glViewport(0, 0, viewportSize.width(), viewportSize.height()); - glDisable(GL_BLEND); - - program->bind(); - vao->bind(); - - glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, (void *) vao->indexOffset); - - glBindVertexArray(0); - glUseProgram(0); - - window->endExternalCommands(); - } -} diff --git a/editor/widgets/GlViewportRenderer.h b/editor/widgets/GlViewportRenderer.h deleted file mode 100644 index fcb6cce..0000000 --- a/editor/widgets/GlViewportRenderer.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include "gl/GlShaderModule.h" -#include "gl/GlShaderProgram.h" -#include "buffer/gl/WritableGlBuffer.h" -#include "gl/GlVertexArrayObject.h" -#include "gl/GlWindow.h" - -#include -#include -#include -#include -#include - -using namespace Vixen::Vk; - -namespace Vixen::Editor { - class GlViewportRenderer : public QObject { - Q_OBJECT - - std::vector vertices = { - {0.5f, 0.5f, 0.0f}, // top right - {0.5f, -0.5f, 0.0f}, // bottom right - {-0.5f, -0.5f, 0.0f}, // bottom left - {-0.5f, 0.5f, 0.0f} // top left - }; - - std::vector indices = { // note that we start from 0! - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - }; - - QSize viewportSize; - - QQuickWindow *window = nullptr; - - std::shared_ptr program; - - std::shared_ptr vbo; - - std::shared_ptr vao; - - public: - void setViewportSize(const QSize &size); - - void setWindow(QQuickWindow *win); - - public slots: - - void init(); - - void paint(); - }; -} diff --git a/engine/Buffer.cpp b/engine/Buffer.cpp index 9efadf1..ddcefcb 100644 --- a/engine/Buffer.cpp +++ b/engine/Buffer.cpp @@ -1,6 +1,6 @@ #include "Buffer.h" -namespace Vixen::Vk { +namespace Vixen { Buffer::Buffer(Usage bufferUsage, const size_t &size) : bufferUsage(bufferUsage), size(size) {} diff --git a/engine/Buffer.h b/engine/Buffer.h index 3a47fdc..12841fe 100644 --- a/engine/Buffer.h +++ b/engine/Buffer.h @@ -4,7 +4,7 @@ #include #include -namespace Vixen::Vk { +namespace Vixen { /** * A persistently mapped, coherent, synchronized, host or server data buffer. */ @@ -18,7 +18,9 @@ namespace Vixen::Vk { TRANSFER_SRC = 1 << 4, }; - Buffer &operator=(const Buffer &) = delete; + virtual char *map() = 0; + + virtual void unmap() = 0; /** * Map the buffer, write to it and unmap the buffer from host memory. @@ -44,10 +46,6 @@ namespace Vixen::Vk { * @param allocationUsage Specifies how this buffer's allocated memory will be used. */ Buffer(Usage bufferUsage, const std::size_t &size); - - virtual char *map() = 0; - - virtual void unmap() = 0; }; inline Buffer::Usage operator|(Buffer::Usage a, Buffer::Usage b) { diff --git a/engine/ShaderModule.h b/engine/ShaderModule.h index cbf3215..c5dba63 100644 --- a/engine/ShaderModule.h +++ b/engine/ShaderModule.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/engine/Window.cpp b/engine/Window.cpp index a269366..dade37a 100644 --- a/engine/Window.cpp +++ b/engine/Window.cpp @@ -1,5 +1,5 @@ #define GLFW_INCLUDE_NONE - +#include #include "Window.h" namespace Vixen { diff --git a/engine/Window.h b/engine/Window.h index cc79130..49e68d9 100644 --- a/engine/Window.h +++ b/engine/Window.h @@ -1,8 +1,9 @@ #pragma once +#define GLFW_INCLUDE_NONE +#include #include #include -#include #include #include #include "Monitor.h" @@ -23,6 +24,7 @@ namespace Vixen { std::unordered_map monitors; + public: Window( const std::string &title, const uint32_t &width, @@ -32,7 +34,6 @@ namespace Vixen { ~Window(); - public: /** * Has the current window been requested to close? * @return True if the window should close, false if not. diff --git a/engine/gl/GlBuffer.cpp b/engine/gl/GlBuffer.cpp index 3123435..b614f42 100644 --- a/engine/gl/GlBuffer.cpp +++ b/engine/gl/GlBuffer.cpp @@ -1,31 +1,31 @@ #include "GlBuffer.h" -namespace Vixen::Vk { +namespace Vixen::Gl { // TODO: Synchronization - GlBuffer::GlBuffer(GLbitfield flags, const size_t &size, BufferUsage bufferUsage, AllocationUsage allocationUsage) - : Buffer(size, bufferUsage, allocationUsage), buffer() { - this->flags = flags; - this->flags |= GL_MAP_COHERENT_BIT | - GL_MAP_PERSISTENT_BIT; - + GlBuffer::GlBuffer(Usage bufferUsage, const size_t &size) + : Buffer(bufferUsage, size), + flags(GL_MAP_WRITE_BIT) { glCreateBuffers(1, &buffer); - switch (allocationUsage) { - case AllocationUsage::GPU_ONLY: - case AllocationUsage::CPU_TO_GPU: + switch (bufferUsage) { +// case AllocationUsage::GPU_ONLY: +// case AllocationUsage::CPU_TO_GPU: +// break; +// case AllocationUsage::CPU_ONLY: +// case AllocationUsage::GPU_TO_CPU: +// flags |= GL_CLIENT_STORAGE_BIT; +// break; + case Usage::VERTEX: + break; + case Usage::INDEX: + break; + case Usage::UNIFORM: + break; + case Usage::TRANSFER_DST: break; - case AllocationUsage::CPU_ONLY: - case AllocationUsage::GPU_TO_CPU: - flags |= GL_CLIENT_STORAGE_BIT; + case Usage::TRANSFER_SRC: break; } - // TODO - if (bufferUsage & BufferUsage::VERTEX); - if (bufferUsage & BufferUsage::INDEX); - if (bufferUsage & BufferUsage::TRANSFER_DST); - if (bufferUsage & BufferUsage::TRANSFER_SRC); - if (bufferUsage & BufferUsage::UNIFORM); - glNamedBufferStorage( buffer, static_cast(size), @@ -33,33 +33,47 @@ namespace Vixen::Vk { this->flags ); spdlog::trace("Created new GL commandBuffer {} ({}B) and flags {}", buffer, size, this->flags); - dataPointer = map(0, size); - } - - GlBuffer::GlBuffer(GLuint buffer, GLbitfield flags, const size_t &size, BufferUsage bufferUsage, - AllocationUsage allocationUsage) - : Buffer(size, bufferUsage, allocationUsage), buffer(buffer), flags(flags) { - dataPointer = map(0, size); } GlBuffer::~GlBuffer() { - glUnmapNamedBuffer(buffer); glDeleteBuffers(1, &buffer); } - void *GlBuffer::map(std::size_t offset, std::size_t length) const { - if (offset > size) - error("Offset is greater than the total commandBuffer size"); - if (offset + length > size) - error("The offset plus length is greater than the total commandBuffer size"); +// void *GlBuffer::map(std::size_t offset, std::size_t length) const { +// if (offset > size) +// error("Offset is greater than the total commandBuffer size"); +// if (offset + length > size) +// error("The offset plus length is greater than the total commandBuffer size"); +// +// void *d = glMapNamedBufferRange(buffer, static_cast(offset), static_cast(length), +// flags); +// if (d == nullptr) +// error("Failed to map GL commandBuffer {}", buffer); +// +// spdlog::trace("Mapped GL commandBuffer {} into {} at offset {}B and length {}B using flags {}", buffer, d, +// offset, +// length, flags); +// return d; +// } - void *d = glMapNamedBufferRange(buffer, static_cast(offset), static_cast(length), - flags); - if (d == nullptr) - error("Failed to map GL commandBuffer {}", buffer); + void GlBuffer::write(const char *data, size_t dataSize, size_t offset) { + if (offset + dataSize > size) + throw std::runtime_error("Buffer overflow"); + + char *d = map(); + memcpy(static_cast(d) + offset, data, dataSize); + unmap(); + } + + char *GlBuffer::map() { + return static_cast(glMapNamedBuffer(buffer, GL_WRITE_ONLY)); + } + + void GlBuffer::unmap() { + glUnmapNamedBuffer(buffer); + } - spdlog::trace("Mapped GL commandBuffer {} into {} at offset {}B and length {}B using flags {}", buffer, d, offset, - length, flags); - return d; + GLuint GlBuffer::getBuffer() const { + return buffer; } } diff --git a/engine/gl/GlBuffer.h b/engine/gl/GlBuffer.h index 82bd33f..a27aca0 100644 --- a/engine/gl/GlBuffer.h +++ b/engine/gl/GlBuffer.h @@ -5,26 +5,24 @@ #include "../Buffer.h" #include "../Util.h" -namespace Vixen::Vk { +namespace Vixen::Gl { class GlBuffer : public Buffer { - friend class WritableGlBuffer; - - friend class GlVertexArrayObject; - - protected: - GLuint buffer; + private: + GLuint buffer = 0; GLbitfield flags; - void *dataPointer; + public: + GlBuffer(Usage bufferUsage, const size_t &size); - GlBuffer(GLbitfield flags, const size_t &size, BufferUsage bufferUsage, AllocationUsage allocationUsage); + ~GlBuffer(); - GlBuffer(GLuint buffer, GLbitfield flags, const size_t &size, BufferUsage bufferUsage, - AllocationUsage allocationUsage); + void write(const char *data, size_t dataSize, size_t offset) override; - ~GlBuffer(); + char *map() override; + + void unmap() override; - [[nodiscard]] void *map(std::size_t offset, std::size_t length) const; + [[nodiscard]] GLuint getBuffer() const; }; } diff --git a/engine/gl/GlRenderer.cpp b/engine/gl/GlRenderer.cpp index 9ab25f8..fae182c 100644 --- a/engine/gl/GlRenderer.cpp +++ b/engine/gl/GlRenderer.cpp @@ -1,12 +1,12 @@ #include "GlRenderer.h" -namespace Vixen::Vk { +namespace Vixen::Gl { GlRenderer::GlRenderer() { } void GlRenderer::submit() { - for (const auto &pass: passes) { + /*for (const auto &pass: passes) { for (const auto &material: pass.materials) { material.shader.bind(); @@ -29,6 +29,6 @@ namespace Vixen::Vk { static_cast(commands.size()), 0); } } - } + }*/ } } diff --git a/engine/gl/GlRenderer.h b/engine/gl/GlRenderer.h index 09dbd59..c3234a6 100644 --- a/engine/gl/GlRenderer.h +++ b/engine/gl/GlRenderer.h @@ -1,8 +1,7 @@ -#include #include #include "../Renderer.h" -namespace Vixen::Vk { +namespace Vixen::Gl { class GlRenderer : public Renderer { struct DrawElementsIndirectCommand { std::uint32_t count; diff --git a/engine/gl/GlShaderModule.cpp b/engine/gl/GlShaderModule.cpp index c4ca347..fe130e4 100644 --- a/engine/gl/GlShaderModule.cpp +++ b/engine/gl/GlShaderModule.cpp @@ -1,45 +1,8 @@ #include "GlShaderModule.h" -namespace Vixen::Vk { +namespace Vixen::Gl { GlShaderModule::GlShaderModule(Stage stage, const std::string &source, const std::string &entry) - : Vk::ShaderModule(stage, source, entry), module(0) { - // TODO: Not sure if compiling from the SPIR-V is the best route to go, so (possibly temporarily) just grab - // TODO: the raw source and compile it. This might cause issues later for Vulkan specific features, but we might - // TODO: just not support those in the end. This is something we should decide on later. - /*spirv_cross::CompilerGLSL compiler(binary); - spirv_cross::CompilerGLSL::Options options { - .version = 450, - .es = false, - .force_temporary = false, - .force_recompile_max_debug_iterations = 3, - .vulkan_semantics = false, - .separate_shader_objects = true, - .flatten_multidimensional_arrays = false, - .enable_420pack_extension = true, - .emit_push_constant_as_uniform_buffer = true, - .emit_uniform_buffer_as_plain_uniforms = false, - .emit_line_directives = false, - .enable_storage_image_qualifier_deduction = true, - .force_zero_initialized_variables = false, - .force_flattened_io_blocks = false, - .relax_nan_checks = false, - // .enable_row_major_workaround = true, - .ovr_multiview_view_count = 0, - .vertex = { - .fixup_clipspace = true, - .flip_vert_y = false, - .support_nonzero_base_instance = true - }, - .fragment = { - .default_float_precision = spirv_cross::CompilerGLSL::Options::Mediump, - .default_int_precision = spirv_cross::CompilerGLSL::Options::Highp, - } - }; - - compiler.set_common_options(options); - auto crossed = compiler.compile(); - spdlog::trace("Cross-compiled GLSL shader\n{}", crossed);*/ - + : ShaderModule(stage, entry, {}, {}), module(0) { switch (stage) { case Stage::VERTEX: module = glCreateShader(GL_VERTEX_SHADER); diff --git a/engine/gl/GlShaderModule.h b/engine/gl/GlShaderModule.h index 225bc40..4e8f355 100644 --- a/engine/gl/GlShaderModule.h +++ b/engine/gl/GlShaderModule.h @@ -1,11 +1,10 @@ #pragma once -#include -#include #include +#include #include "../ShaderModule.h" -namespace Vixen::Vk { +namespace Vixen::Gl { class GlShaderModule : public ShaderModule { friend class GlShaderProgram; diff --git a/engine/gl/GlShaderProgram.cpp b/engine/gl/GlShaderProgram.cpp index 085f3e5..b6b49e0 100644 --- a/engine/gl/GlShaderProgram.cpp +++ b/engine/gl/GlShaderProgram.cpp @@ -1,11 +1,17 @@ #include "GlShaderProgram.h" -namespace Vixen::Vk { - GlShaderProgram::GlShaderProgram(const std::vector> &modules) - : Vk::ShaderProgram(modules) { +namespace Vixen::Gl { + GlShaderProgram::GlShaderProgram(const std::shared_ptr &vertex, + const std::shared_ptr &fragment) + : ShaderProgram(vertex, fragment) { program = glCreateProgram(); - for (const auto &m: modules) - glAttachShader(program, m->module); + + if (vertex != nullptr) + glAttachShader(program, vertex->module); + + if (fragment != nullptr) + glAttachShader(program, fragment->module); + glLinkProgram(program); int success; diff --git a/engine/gl/GlShaderProgram.h b/engine/gl/GlShaderProgram.h index 83f28d1..89e75a0 100644 --- a/engine/gl/GlShaderProgram.h +++ b/engine/gl/GlShaderProgram.h @@ -1,15 +1,15 @@ #pragma once -#include #include "../ShaderProgram.h" #include "GlShaderModule.h" -namespace Vixen::Vk { +namespace Vixen::Gl { class GlShaderProgram : ShaderProgram { unsigned int program; public: - explicit GlShaderProgram(const std::vector> &modules); + explicit GlShaderProgram(const std::shared_ptr &vertex, + const std::shared_ptr &fragment); GlShaderProgram(const GlShaderProgram &) = delete; @@ -19,6 +19,6 @@ namespace Vixen::Vk { void bind() const; - static void unbind() ; + static void unbind(); }; } diff --git a/engine/gl/GlVertexArrayObject.cpp b/engine/gl/GlVertexArrayObject.cpp index a82677d..484c0bb 100644 --- a/engine/gl/GlVertexArrayObject.cpp +++ b/engine/gl/GlVertexArrayObject.cpp @@ -1,13 +1,13 @@ #include "GlVertexArrayObject.h" -namespace Vixen::Vk { +namespace Vixen::Gl { GlVertexArrayObject::GlVertexArrayObject(const std::vector &bindings, size_t indexOffset) : bindings(), indexOffset(indexOffset) { glCreateVertexArrays(1, &vao); for (const auto &binding: bindings) { for (const auto &location: binding.locations) { - glVertexArrayVertexBuffer(vao, location.index, binding.buffer->buffer, location.offset, + glVertexArrayVertexBuffer(vao, location.index, binding.buffer->getBuffer(), location.offset, location.stride); glVertexArrayAttribFormat(vao, location.index, location.size, location.type, location.normalized, location.offset); @@ -16,8 +16,8 @@ namespace Vixen::Vk { glVertexArrayAttribBinding(vao, location.index, location.index); } - if (binding.buffer->bufferUsage & BufferUsage::INDEX) - glVertexArrayElementBuffer(vao, binding.buffer->buffer); + if (binding.buffer->getBufferUsage() & Buffer::Usage::INDEX) + glVertexArrayElementBuffer(vao, binding.buffer->getBuffer()); } } diff --git a/engine/gl/GlVertexArrayObject.h b/engine/gl/GlVertexArrayObject.h index cb3aa24..475bed2 100644 --- a/engine/gl/GlVertexArrayObject.h +++ b/engine/gl/GlVertexArrayObject.h @@ -1,11 +1,10 @@ #pragma once -#include #include #include #include "GlBuffer.h" -namespace Vixen::Vk { +namespace Vixen::Gl { struct VertexBinding { struct Location { Location(GLuint index, GLint size, GLenum type, GLboolean normalized, GLintptr offset, GLsizei stride) diff --git a/engine/gl/GlVixen.cpp b/engine/gl/GlVixen.cpp index f63839b..72a4f8a 100644 --- a/engine/gl/GlVixen.cpp +++ b/engine/gl/GlVixen.cpp @@ -1,8 +1,8 @@ #include "GlVixen.h" -namespace Vixen::Vk { +namespace Vixen::Gl { GlVixen::GlVixen(const std::string &appTitle, const glm::vec3 &appVersion) - : Vixen(appTitle, appVersion, std::make_shared()), + : Vixen(appTitle, appVersion), window(GlWindow(appTitle, 720, 480, false)) { window.center(); window.setVisible(true); diff --git a/engine/gl/GlVixen.h b/engine/gl/GlVixen.h index 27b1fe2..0fdf919 100644 --- a/engine/gl/GlVixen.h +++ b/engine/gl/GlVixen.h @@ -2,7 +2,7 @@ #include "GlWindow.h" #include "GlRenderer.h" -namespace Vixen::Vk { +namespace Vixen::Gl { class GlVixen : public Vixen { GlWindow window; diff --git a/engine/gl/GlWindow.cpp b/engine/gl/GlWindow.cpp index e4dfcfa..409061f 100644 --- a/engine/gl/GlWindow.cpp +++ b/engine/gl/GlWindow.cpp @@ -1,8 +1,8 @@ #include "GlWindow.h" -namespace Vixen::Vk { - GlWindow::GlWindow(const std::string &title, const int &width, const int &height, bool transparentFrameBuffer) : - Vixen::Vk::Window(transparentFrameBuffer) { +namespace Vixen::Gl { + GlWindow::GlWindow(const std::string &title, const int &width, const int &height, bool transparentFrameBuffer) + : Window(title, width, height, transparentFrameBuffer) { spdlog::trace("Creating new OpenGL window"); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); @@ -42,7 +42,7 @@ namespace Vixen::Vk { } void GlWindow::clear() { - glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + glClearColor(0, 0, 0, 1); int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); diff --git a/engine/gl/GlWindow.h b/engine/gl/GlWindow.h index 820bee2..ca3b708 100644 --- a/engine/gl/GlWindow.h +++ b/engine/gl/GlWindow.h @@ -1,22 +1,16 @@ #pragma once +#include #include #include -#include #include #include #include #include "../Window.h" +namespace Vixen::Gl { #ifdef DEBUG - - - -#endif - -namespace Vixen::Vk { -#ifdef DEBUG - static void APIENTRY glDebugCallback( + static void glDebugCallback( GLenum source, GLenum type, GLuint id, @@ -109,7 +103,7 @@ namespace Vixen::Vk { #endif - class GlWindow : public BaseWindow { + class GlWindow : public Window { public: GlWindow(const std::string &title, const int &width, const int &height, bool transparentFrameBuffer); diff --git a/engine/gl/test/main.cpp b/engine/gl/test/main.cpp index a76ce5e..9399353 100644 --- a/engine/gl/test/main.cpp +++ b/engine/gl/test/main.cpp @@ -3,7 +3,6 @@ #include "../GlShaderModule.h" #include "../GlShaderProgram.h" #include "GlBuffer.h" -#include "../../buffer/gl/WritableGlBuffer.h" #include "../GlVertexArrayObject.h" #include @@ -13,8 +12,6 @@ #endif -using namespace Vixen::Vk; - int main() { #ifdef _WIN32 system(("chcp " + std::to_string(CP_UTF8)).c_str()); @@ -32,36 +29,35 @@ int main() { 1, 2, 3 // second triangle }; - auto window = GlWindow("Vixen OpenGL Test", 720, 480, true); + auto window = Vixen::Gl::GlWindow("Vixen OpenGL Test", 720, 480, true); window.center(); - window.setClearColor(0.13f, 0.23f, 0.33f, 1.0f); window.setVisible(true); std::ifstream vertexStream("../../editor/shaders/triangle.vert"); std::string vertexSource((std::istreambuf_iterator(vertexStream)), std::istreambuf_iterator()); - auto vertexModule = std::make_shared(Vixen::Vk::ShaderModule::Stage::VERTEX, vertexSource); + auto vertexModule = std::make_shared(Vixen::ShaderModule::Stage::VERTEX, vertexSource); std::ifstream fragmentStream("../../editor/shaders/triangle.frag"); std::string fragmentSource((std::istreambuf_iterator(fragmentStream)), std::istreambuf_iterator()); - auto fragmentModule = std::make_shared(Vixen::Vk::ShaderModule::Stage::FRAGMENT, - fragmentSource); + auto fragmentModule = std::make_shared(Vixen::ShaderModule::Stage::FRAGMENT, + fragmentSource); - GlShaderProgram program({vertexModule, fragmentModule}); + Vixen::Gl::GlShaderProgram program(vertexModule, fragmentModule); - auto vbo = std::make_shared( - vertices.size() * sizeof(glm::vec3) + indices.size() * sizeof(std::uint32_t), - Vixen::Vk::BufferUsage::VERTEX | Vixen::Vk::BufferUsage::INDEX, - Vixen::Vk::AllocationUsage::GPU_ONLY + auto vbo = std::make_shared( + Vixen::Buffer::Usage::VERTEX | Vixen::Buffer::Usage::INDEX, + vertices.size() * sizeof(glm::vec3) + + indices.size() * sizeof(std::uint32_t) ); - vbo->write(vertices, 0); - vbo->write(indices, vertices.size() * sizeof(glm::vec3)); + vbo->write(reinterpret_cast(vertices.data()), vertices.size() * sizeof(glm::vec3), 0); + vbo->write(reinterpret_cast(indices.data()), vertices.size() * sizeof(glm::vec3), 0); - auto vao = GlVertexArrayObject( + auto vao = Vixen::Gl::GlVertexArrayObject( { - VertexBinding( + Vixen::Gl::VertexBinding( vbo, { - VertexBinding::Location(0, 3, GL_FLOAT, GL_FALSE, 0, sizeof(glm::vec3)) + Vixen::Gl::VertexBinding::Location(0, 3, GL_FLOAT, GL_FALSE, 0, sizeof(glm::vec3)) } ) }, @@ -73,9 +69,9 @@ int main() { program.bind(); vao.bind(); - glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, (void*) vao.indexOffset); + glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, (void *) vao.indexOffset); - GlWindow::update(); + window.update(); window.swap(); } return EXIT_SUCCESS; diff --git a/engine/vk/CMakeLists.txt b/engine/vk/CMakeLists.txt index 093718b..bea89bd 100644 --- a/engine/vk/CMakeLists.txt +++ b/engine/vk/CMakeLists.txt @@ -6,8 +6,8 @@ find_package(spirv_cross_glsl CONFIG REQUIRED) find_package(spirv_cross_reflect CONFIG REQUIRED) find_package(spirv_cross_cpp CONFIG REQUIRED) find_package(glslang CONFIG REQUIRED) +#pkg_check_modules(glslang REQUIRED IMPORTED_TARGET glslang) -include(FetchContent) FetchContent_Declare( vma GIT_REPOSITORY https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator