diff --git a/engine/include/sophia/util/assert.hpp b/engine/include/sophia/util/assert.hpp new file mode 100644 index 0000000..5bc0363 --- /dev/null +++ b/engine/include/sophia/util/assert.hpp @@ -0,0 +1,26 @@ +/** + * Usage: + * SOPHIA_ASSERT(pipelineLayout != nullptr); + * SOPHIA_ASSERT(index < count, "index", index, "out of bounds:", count); + * + * Compiled out entirely in release (NDEBUG) builds, matching 's + * assert() semantics: don't rely on side effects inside the condition. + */ + +#pragma once + +#include +#include + +#ifndef NDEBUG +#define SOPHIA_ASSERT(condition, ...) \ + do { \ + if (!(condition)) { \ + ::sophia::log::fatal("Assertion failed:", #condition, "at", __FILE__, \ + ":", __LINE__ __VA_OPT__(, ) __VA_ARGS__); \ + std::abort(); \ + } \ + } while (false) +#else +#define SOPHIA_ASSERT(condition, ...) ((void)0) +#endif // !NDEBUG diff --git a/engine/src/buffer.cpp b/engine/src/buffer.cpp index 2b42268..341c436 100644 --- a/engine/src/buffer.cpp +++ b/engine/src/buffer.cpp @@ -4,6 +4,8 @@ */ #include "buffer.hpp" +#include + namespace sophia { VkDeviceSize Buffer::getAlignment(VkDeviceSize instanceSize, @@ -36,7 +38,7 @@ Buffer::~Buffer() { } vk::Result Buffer::map(vk::DeviceSize size, vk::DeviceSize offset) { - assert(buffer && memory && "Called map on buffer before create"); + SOPHIA_ASSERT(buffer && memory, "Called map on buffer before create"); vk::MemoryMapFlags emptyFlag{}; return this->device.get()->mapMemory(this->memory, offset, size, emptyFlag, @@ -52,7 +54,7 @@ void Buffer::unmap() { void Buffer::writeToBuffer(void* data, vk::DeviceSize size, vk::DeviceSize offset) { - assert(mapped && "Cannot copy to unmapped buffer"); + SOPHIA_ASSERT(mapped, "Cannot copy to unmapped buffer"); if (size == VK_WHOLE_SIZE) { memcpy(mapped, data, bufferSize); diff --git a/engine/src/descriptors/descriptor_set_layout.cpp b/engine/src/descriptors/descriptor_set_layout.cpp index c1e9022..65ff21e 100644 --- a/engine/src/descriptors/descriptor_set_layout.cpp +++ b/engine/src/descriptors/descriptor_set_layout.cpp @@ -1,6 +1,6 @@ #include "descriptor_set_layout.hpp" -#include +#include #include namespace sophia { @@ -8,7 +8,7 @@ namespace sophia { DescriptorSetLayout::Builder& DescriptorSetLayout::Builder::addBinding( u32 binding, vk::DescriptorType descriptorType, vk::ShaderStageFlags stageFlags, u32 count) { - assert(this->bindings.count(binding) == 0 && "binding already in use"); + SOPHIA_ASSERT(this->bindings.count(binding) == 0, "binding already in use"); vk::DescriptorSetLayoutBinding layoutBinding{binding, descriptorType, count, stageFlags}; diff --git a/engine/src/descriptors/descriptor_writer.cpp b/engine/src/descriptors/descriptor_writer.cpp index 8b272e5..41d4411 100644 --- a/engine/src/descriptors/descriptor_writer.cpp +++ b/engine/src/descriptors/descriptor_writer.cpp @@ -1,6 +1,6 @@ #include "descriptor_writer.hpp" -#include +#include #include namespace sophia { @@ -11,13 +11,13 @@ DescriptorWriter::DescriptorWriter(DescriptorSetLayout& setLayout, DescriptorWriter& DescriptorWriter::writeBuffer( u32 binding, vk::DescriptorBufferInfo* bufferInfo) { - assert(this->setLayout.bindings.count(binding) == 1 && - "setLayout does not contain specified binding"); + SOPHIA_ASSERT(this->setLayout.bindings.count(binding) == 1, + "setLayout does not contain specified binding"); auto& bindingDescription = this->setLayout.bindings[binding]; - assert(bindingDescription.descriptorCount == 1 && - "binding single descriptor info, but binding expects multiple"); + SOPHIA_ASSERT(bindingDescription.descriptorCount == 1, + "binding single descriptor info, but binding expects multiple"); vk::WriteDescriptorSet write{}; write.descriptorType = bindingDescription.descriptorType; @@ -31,13 +31,13 @@ DescriptorWriter& DescriptorWriter::writeBuffer( DescriptorWriter& DescriptorWriter::writeImage( u32 binding, vk::DescriptorImageInfo* imageInfo) { - assert(this->setLayout.bindings.count(binding) == 1 && - "setLayout does not contain specified binding"); + SOPHIA_ASSERT(this->setLayout.bindings.count(binding) == 1, + "setLayout does not contain specified binding"); auto& bindingDescription = this->setLayout.bindings[binding]; - assert(bindingDescription.descriptorCount == 1 && - "binding single descriptor info, but binding expects multiple"); + SOPHIA_ASSERT(bindingDescription.descriptorCount == 1, + "binding single descriptor info, but binding expects multiple"); vk::WriteDescriptorSet write{}; write.descriptorType = bindingDescription.descriptorType; diff --git a/engine/src/device.hpp b/engine/src/device.hpp index 3d5a5fb..a690f3b 100644 --- a/engine/src/device.hpp +++ b/engine/src/device.hpp @@ -2,10 +2,10 @@ #include -#include #include #include #include +#include #include #include @@ -41,7 +41,7 @@ class Device { ~Device(); const vk::Device* get() const { - assert(this->device); + SOPHIA_ASSERT(this->device); return &this->device.get(); } diff --git a/engine/src/mesh.cpp b/engine/src/mesh.cpp index f1d8311..533fefd 100644 --- a/engine/src/mesh.cpp +++ b/engine/src/mesh.cpp @@ -1,6 +1,6 @@ #include "mesh.hpp" -#include +#include #include namespace sophia { @@ -38,7 +38,7 @@ Mesh::Mesh(Device& device, const Builder& builder) : device{device} { Mesh::~Mesh() {} -void Mesh::bind(vk::CommandBuffer commandBuffer) { +void Mesh::bind(vk::CommandBuffer commandBuffer) const { vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()}; vk::DeviceSize offset[] = {0}; commandBuffer.bindVertexBuffers(0, 1, buffers, offset); @@ -49,7 +49,7 @@ void Mesh::bind(vk::CommandBuffer commandBuffer) { } } -void Mesh::draw(vk::CommandBuffer commandBuffer) { +void Mesh::draw(vk::CommandBuffer commandBuffer) const { if (this->hasIndexBuffer) { commandBuffer.drawIndexed(this->indexCount, 1, 0, 0, 0); } else { @@ -59,7 +59,7 @@ void Mesh::draw(vk::CommandBuffer commandBuffer) { void Mesh::createVertexBuffers(const std::vector& vertices) { this->vertexCount = static_cast(vertices.size()); - assert(this->vertexCount >= 3 && "vertex count must be at least 3"); + SOPHIA_ASSERT(this->vertexCount >= 3, "vertex count must be at least 3"); vk::DeviceSize bufferSize = sizeof(vertices[0]) * this->vertexCount; diff --git a/engine/src/mesh.hpp b/engine/src/mesh.hpp index 0a16710..880fb09 100644 --- a/engine/src/mesh.hpp +++ b/engine/src/mesh.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #define GLM_FORCE_RADIANS @@ -14,6 +15,8 @@ namespace sophia { class Mesh { public: + using HandleType = MeshHandle; + struct Vertex { glm::vec2 position{}; glm::vec3 color{}; @@ -46,8 +49,8 @@ class Mesh { // static std::unique_ptr createMeshFromFile( // Device& device, const std::string& filepath); - void bind(vk::CommandBuffer commandBuffer); - void draw(vk::CommandBuffer commandBuffer); + void bind(vk::CommandBuffer commandBuffer) const; + void draw(vk::CommandBuffer commandBuffer) const; private: void createVertexBuffers(const std::vector& vertices); diff --git a/engine/src/render_context.cpp b/engine/src/render_context.cpp index 25dfd3e..9cfe4fd 100644 --- a/engine/src/render_context.cpp +++ b/engine/src/render_context.cpp @@ -1,7 +1,7 @@ #include "render_context.hpp" -#include #include +#include #include #include @@ -16,8 +16,8 @@ RenderContext::RenderContext(Window& window, Device& device) RenderContext::~RenderContext() { this->device.waitIdle(); } vk::CommandBuffer RenderContext::beginFrame() { - assert(!this->isFrameStarted && - "Can't call beginFrame while already in progress"); + SOPHIA_ASSERT(!this->isFrameStarted, + "Can't call beginFrame while already in progress"); FrameData& frame = this->frames[this->currentFrameIndex]; @@ -71,8 +71,8 @@ vk::CommandBuffer RenderContext::beginFrame() { } void RenderContext::endFrame() { - assert(this->isFrameStarted && - "Can't call endFrame while no frame is in progress"); + SOPHIA_ASSERT(this->isFrameStarted, + "Can't call endFrame while no frame is in progress"); FrameData& frame = this->frames[this->currentFrameIndex]; frame.commandBuffer.end();