Skip to content
Draft
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
7 changes: 0 additions & 7 deletions cmake/flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ if (MSVC)
# link_libraries(legacy_stdio_definitions)
endif ()

# TODO: proper std::execution availability check
if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
add_compile_definitions(${PROJECT_PREFIX}_HAS_EXEC_POLICIES=0)
else ()
add_compile_definitions(${PROJECT_PREFIX}_HAS_EXEC_POLICIES=1)
endif ()

if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
add_compile_definitions(${PROJECT_PREFIX}_DEBUG=1)
add_compile_definitions(${PROJECT_PREFIX}_RELEASE=0)
Expand Down
8 changes: 7 additions & 1 deletion example-app/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

#include <Vulk/Contexts/ContextVulkan.hpp>
#include <Vulk/Graphics/Rectangle.hpp>
#include <Vulk/Graphics/Triangle.hpp>
#include <Vulk/Window.hpp>

#include <iostream>
Expand All @@ -37,10 +38,15 @@ int main()
win.setTitle(buffer);
});

vulk::Rectangle obj{};

obj.setColor(vulk::Color::White);

while (win.isOpen())
{
win.pollEvents();

obj.draw();
win.display();
}

Expand Down
6 changes: 6 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(
include/Vulk/Vec3.hpp
include/Vulk/Rect.hpp
include/Vulk/Mat3.hpp
include/Vulk/Macros.hpp
include/Vulk/ClassUtils.hpp
src/Window.cpp include/Vulk/Window.hpp
src/Exceptions.cpp include/Vulk/Exceptions.hpp
Expand All @@ -17,6 +18,11 @@ add_library(
src/Keyboard.cpp include/Vulk/Keyboard.hpp
src/Mouse.cpp include/Vulk/Mouse.hpp
src/Objects.cpp include/Vulk/Objects.hpp
src/Graphics/ADrawable.cpp include/Vulk/Graphics/ADrawable.hpp
src/Graphics/AShape.cpp include/Vulk/Graphics/AShape.hpp
src/Graphics/Triangle.cpp include/Vulk/Graphics/Triangle.hpp
src/Graphics/Rectangle.cpp include/Vulk/Graphics/Rectangle.hpp
src/Graphics/Circle.cpp include/Vulk/Graphics/Circle.hpp
src/Color.cpp include/Vulk/Color.hpp
)

Expand Down
24 changes: 0 additions & 24 deletions lib/include/Vulk/ClassUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,6 @@

#pragma once

#define VULK_NO_COPY(ClassName) \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete;

#define VULK_NO_MOVE(ClassName) \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete;

#define VULK_NO_MOVE_OR_COPY(ClassName) \
VULK_NO_COPY(ClassName) \
VULK_NO_MOVE(ClassName)

#define VULK_DEFAULT_COPY(ClassName) \
ClassName(const ClassName&) = default; \
ClassName& operator=(const ClassName&) = default;

#define VULK_DEFAULT_MOVE(ClassName) \
ClassName(ClassName&&) = default; \
ClassName& operator=(ClassName&&) = default;

#define VULK_DEFAULT_MOVE_AND_COPY(ClassName) \
VULK_DEFAULT_COPY(ClassName) \
VULK_DEFAULT_MOVE(ClassName)

/**
* Forces a compile error and shows the type of a variable.
* Useful tool when searching for an auto type or deep library calls.
Expand Down
7 changes: 7 additions & 0 deletions lib/include/Vulk/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#pragma once

#include <glm/glm.hpp>

#include <compare>
#include <cstdint>

Expand Down Expand Up @@ -66,6 +68,11 @@ struct Color
return value;
}

[[nodiscard]] constexpr glm::vec3 to_glm_color() const noexcept
{
return glm::vec3{static_cast<float>(r) / 255.f, static_cast<float>(g) / 255.f, static_cast<float>(b) / 255.f};
}

static Color Red;
static Color Green;
static Color Blue;
Expand Down
6 changes: 3 additions & 3 deletions lib/include/Vulk/Contexts/ContextGLFW.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

#include <memory>

#include "Vulk/ClassUtils.hpp"
#include "Vulk/Macros.hpp"

namespace vulk {
namespace vulk::detail {
class ContextGLFW
{
public:
Expand All @@ -43,4 +43,4 @@ class ContextGLFW

static std::unique_ptr<ContextGLFW> s_instance;
};
} // namespace vulk
} // namespace vulk::detail
82 changes: 46 additions & 36 deletions lib/include/Vulk/Contexts/ContextVulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,56 @@
#include <memory>
#include <optional>

#include "Vulk/ClassUtils.hpp"
#include "Vulk/Exceptions.hpp"
#include "Vulk/Graphics/ADrawable.hpp"
#include "Vulk/Graphics/AShape.hpp"
#include "Vulk/Macros.hpp"
#include "Vulk/Objects.hpp"
#include "Vulk/Window.hpp"

namespace vulk {
namespace vulk::detail {
class ContextVulkan
{
public:
~ContextVulkan();

/**
* TODO: This should not be public. The Vulkan context should only be exposed to the window.
* Calling this function from the static `getInstance().draw()` can break stuff.
*/
void draw();

template<typename T>
void createBuffers(const std::vector<T>& vertices, vk::Buffer& outBuffer, vk::DeviceMemory& outDeviceMemory,
vk::BufferUsageFlagBits flags)
{
const size_t bufferSize = sizeof(std::remove_cvref_t<decltype(vertices)>::value_type) * vertices.size();

vk::Buffer stagingBuffer;
vk::DeviceMemory stagingBufferMemory;

createBuffer(bufferSize, vk::BufferUsageFlagBits::eTransferSrc,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
stagingBuffer, stagingBufferMemory);

void* data;
handleVulkanError(m_device.mapMemory(stagingBufferMemory, 0, bufferSize, {}, &data));
std::memcpy(data, vertices.data(), bufferSize);
m_device.unmapMemory(stagingBufferMemory);

createBuffer(bufferSize, vk::BufferUsageFlagBits::eTransferDst | flags,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, outBuffer,
outDeviceMemory);

copyBuffer(stagingBuffer, outBuffer, bufferSize);

// TODO: vk::raii
m_device.destroy(stagingBuffer);
m_device.freeMemory(stagingBufferMemory);
}

void destroyBuffer(vk::Buffer buffer);
void freeBufferMem(vk::DeviceMemory deviceMemory);

void registerDrawable(AShape& drawable);
void unregisterDrawable(AShape& drawable);

// TODO: should not be public, remove once events are implemented
void setFrameBufferResized(bool value) noexcept { m_frameBufferResized = value; }

Expand Down Expand Up @@ -130,8 +164,6 @@ class ContextVulkan
void createGraphicsPipeline();
void createFrameBuffers();
void createCommandPool();
void createVertexBuffer();
void createIndexBuffer();
void createUniformBuffers();
void createDescriptorPool();
void createDescriptorSets();
Expand All @@ -141,9 +173,10 @@ class ContextVulkan
void recordCommandBuffer(vk::CommandBuffer& commandBuffer, uint32_t imageIndex);

void recreateSwapChain();
void copyBuffer(const vk::Buffer& sourceBuffer, vk::Buffer& destinationBuffer, vk::DeviceSize size);

void createBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties,
vk::Buffer& outBuffer, vk::DeviceMemory& outDeviceMemory);
void copyBuffer(const vk::Buffer& sourceBuffer, vk::Buffer& destinationBuffer, vk::DeviceSize size);

void cleanupSwapchain(vk::SwapchainKHR& swapchain);
void cleanupSwapchainSubObjects();
Expand Down Expand Up @@ -202,46 +235,23 @@ class ContextVulkan
std::vector<FrameSyncObjects> m_frameSyncObjects{};
std::vector<vk::Fence> m_imagesInFlight{};

vk::Buffer m_vertexBuffer{};
vk::DeviceMemory m_vertexBufferMemory{};
vk::Buffer m_indexBuffer{};
vk::DeviceMemory m_indexBufferMemory{};

std::vector<vk::Buffer> m_uniformBuffers{};
std::vector<vk::DeviceMemory> m_uniformBuffersMemory{};

vk::DescriptorPool m_descriptorPool{};
std::vector<vk::DescriptorSet> m_descriptorSets{};

std::vector<vulk::AShape*> m_shapes{};

// TODO: May be better to store in the FrameManager
size_t m_currentFrame{};
static const size_t s_maxFramesInFlight;
//

bool m_frameBufferResized{false};

template<typename T>
static constexpr vk::IndexType getIndexType()
{
// could be a concept with C++20
static_assert(std::is_same_v<T, uint16_t> || std::is_same_v<T, uint32_t>);

if (std::is_same_v<T, uint16_t>)
return vk::IndexType::eUint16;
if (std::is_same_v<T, uint32_t>)
return vk::IndexType::eUint32;

assert(0);
return vk::IndexType::eNoneKHR;
}

static constexpr auto s_noTimeout = std::numeric_limits<uint64_t>::max();
static constexpr std::array s_vertices{Vertex{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}}, //
Vertex{{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}}, //
Vertex{{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}, //
Vertex{{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}}};
static constexpr std::array<uint16_t, 6> s_indices{0, 1, 2, 2, 3, 0};
static constexpr auto NO_TIMEOUT = std::numeric_limits<uint64_t>::max();

static std::unique_ptr<ContextVulkan> s_instance;
};
} // namespace vulk
} // namespace vulk::detail
2 changes: 1 addition & 1 deletion lib/include/Vulk/Exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <stdexcept>

#include "Vulk/ClassUtils.hpp"
#include "Vulk/Macros.hpp"

namespace vulk {
class Exception : public std::exception
Expand Down
48 changes: 48 additions & 0 deletions lib/include/Vulk/Graphics/ADrawable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2021-2021 [fill name later]
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#pragma once

#include <glm/vec2.hpp>

namespace vulk {
class ADrawable
{
public:
ADrawable();
explicit ADrawable(const glm::vec2& position) : m_position{position} {}

virtual ~ADrawable();

virtual void draw() const = 0;

virtual void setOrigin(const glm::vec2& origin);
virtual void setPosition(const glm::vec2& position);
virtual void setScale(const glm::vec2& scale);

[[nodiscard]] const auto& getOrigin() const noexcept { return m_origin; }
[[nodiscard]] const auto& getPosition() const noexcept { return m_position; }
[[nodiscard]] const auto& getScale() const noexcept { return m_scale; }

protected:
glm::vec2 m_origin{};
glm::vec2 m_position{};
glm::vec2 m_scale{};
};
} // namespace vulk
75 changes: 75 additions & 0 deletions lib/include/Vulk/Graphics/AShape.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2021-2021 [fill name later]
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#pragma once

#include <vulkan/vulkan.hpp>

#include "Vulk/Color.hpp"
#include "Vulk/Graphics/ADrawable.hpp"
#include "Vulk/Objects.hpp"

namespace vulk {
class AShape : public ADrawable
{
public:
AShape();
~AShape() override;
explicit AShape(const glm::vec2& position) : ADrawable{position} {}

void setColor(Color color);

[[nodiscard]] size_t getVertexCount() const noexcept { return m_vertices.size(); }
[[nodiscard]] size_t getIndexCount() const noexcept { return m_indices.size(); }

// TODO: these two should NOT be public, dirty workaround until a proper renderer is made
[[nodiscard]] const auto& getVertexBuffer() const noexcept { return m_vertexBuffer; }
[[nodiscard]] const auto& getIndexBuffer() const noexcept { return m_indexBuffer; }

protected:
static constexpr vk::IndexType INDEX_TYPE = vk::IndexType::eUint32;

template<size_t Count>
void setVertices(const std::array<vulk::Vertex, Count>& vertices)
{
m_vertices.reserve(Count);
m_vertices.insert(m_vertices.end(), vertices.cbegin(), vertices.cend());
}

template<size_t Count>
void setIndices(const std::array<uint32_t, Count>& indices)
{
m_indices.reserve(Count);
m_indices.insert(m_indices.end(), indices.cbegin(), indices.cend());
}

void makeBuffers();

private:
void resetBuffers();

vk::Buffer m_vertexBuffer{};
vk::DeviceMemory m_vertexMemory{};
vk::Buffer m_indexBuffer{};
vk::DeviceMemory m_indexMemory{};

std::vector<Vertex> m_vertices{};
std::vector<uint32_t> m_indices{};
};
} // namespace vulk
Loading