SOPHIA_ASSERT

This commit is contained in:
2026-09-09 02:02:03 -07:00
parent 73d2121bc1
commit 9aef3f57fc
8 changed files with 57 additions and 26 deletions
+26
View File
@@ -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 <cassert>'s
* assert() semantics: don't rely on side effects inside the condition.
*/
#pragma once
#include <cstdlib>
#include <sophia/util/logger.hpp>
#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
+4 -2
View File
@@ -4,6 +4,8 @@
*/ */
#include "buffer.hpp" #include "buffer.hpp"
#include <sophia/util/assert.hpp>
namespace sophia { namespace sophia {
VkDeviceSize Buffer::getAlignment(VkDeviceSize instanceSize, VkDeviceSize Buffer::getAlignment(VkDeviceSize instanceSize,
@@ -36,7 +38,7 @@ Buffer::~Buffer() {
} }
vk::Result Buffer::map(vk::DeviceSize size, vk::DeviceSize offset) { 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{}; vk::MemoryMapFlags emptyFlag{};
return this->device.get()->mapMemory(this->memory, offset, size, 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, void Buffer::writeToBuffer(void* data, vk::DeviceSize size,
vk::DeviceSize offset) { vk::DeviceSize offset) {
assert(mapped && "Cannot copy to unmapped buffer"); SOPHIA_ASSERT(mapped, "Cannot copy to unmapped buffer");
if (size == VK_WHOLE_SIZE) { if (size == VK_WHOLE_SIZE) {
memcpy(mapped, data, bufferSize); memcpy(mapped, data, bufferSize);
@@ -1,6 +1,6 @@
#include "descriptor_set_layout.hpp" #include "descriptor_set_layout.hpp"
#include <cassert> #include <sophia/util/assert.hpp>
#include <stdexcept> #include <stdexcept>
namespace sophia { namespace sophia {
@@ -8,7 +8,7 @@ namespace sophia {
DescriptorSetLayout::Builder& DescriptorSetLayout::Builder::addBinding( DescriptorSetLayout::Builder& DescriptorSetLayout::Builder::addBinding(
u32 binding, vk::DescriptorType descriptorType, u32 binding, vk::DescriptorType descriptorType,
vk::ShaderStageFlags stageFlags, u32 count) { 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, vk::DescriptorSetLayoutBinding layoutBinding{binding, descriptorType, count,
stageFlags}; stageFlags};
+5 -5
View File
@@ -1,6 +1,6 @@
#include "descriptor_writer.hpp" #include "descriptor_writer.hpp"
#include <cassert> #include <sophia/util/assert.hpp>
#include <stdexcept> #include <stdexcept>
namespace sophia { namespace sophia {
@@ -11,12 +11,12 @@ DescriptorWriter::DescriptorWriter(DescriptorSetLayout& setLayout,
DescriptorWriter& DescriptorWriter::writeBuffer( DescriptorWriter& DescriptorWriter::writeBuffer(
u32 binding, vk::DescriptorBufferInfo* bufferInfo) { u32 binding, vk::DescriptorBufferInfo* bufferInfo) {
assert(this->setLayout.bindings.count(binding) == 1 && SOPHIA_ASSERT(this->setLayout.bindings.count(binding) == 1,
"setLayout does not contain specified binding"); "setLayout does not contain specified binding");
auto& bindingDescription = this->setLayout.bindings[binding]; auto& bindingDescription = this->setLayout.bindings[binding];
assert(bindingDescription.descriptorCount == 1 && SOPHIA_ASSERT(bindingDescription.descriptorCount == 1,
"binding single descriptor info, but binding expects multiple"); "binding single descriptor info, but binding expects multiple");
vk::WriteDescriptorSet write{}; vk::WriteDescriptorSet write{};
@@ -31,12 +31,12 @@ DescriptorWriter& DescriptorWriter::writeBuffer(
DescriptorWriter& DescriptorWriter::writeImage( DescriptorWriter& DescriptorWriter::writeImage(
u32 binding, vk::DescriptorImageInfo* imageInfo) { u32 binding, vk::DescriptorImageInfo* imageInfo) {
assert(this->setLayout.bindings.count(binding) == 1 && SOPHIA_ASSERT(this->setLayout.bindings.count(binding) == 1,
"setLayout does not contain specified binding"); "setLayout does not contain specified binding");
auto& bindingDescription = this->setLayout.bindings[binding]; auto& bindingDescription = this->setLayout.bindings[binding];
assert(bindingDescription.descriptorCount == 1 && SOPHIA_ASSERT(bindingDescription.descriptorCount == 1,
"binding single descriptor info, but binding expects multiple"); "binding single descriptor info, but binding expects multiple");
vk::WriteDescriptorSet write{}; vk::WriteDescriptorSet write{};
+2 -2
View File
@@ -2,10 +2,10 @@
#include <imgui_impl_vulkan.h> #include <imgui_impl_vulkan.h>
#include <cassert>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <sophia/types.hpp> #include <sophia/types.hpp>
#include <sophia/util/assert.hpp>
#include <vector> #include <vector>
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -41,7 +41,7 @@ class Device {
~Device(); ~Device();
const vk::Device* get() const { const vk::Device* get() const {
assert(this->device); SOPHIA_ASSERT(this->device);
return &this->device.get(); return &this->device.get();
} }
+4 -4
View File
@@ -1,6 +1,6 @@
#include "mesh.hpp" #include "mesh.hpp"
#include <cassert> #include <sophia/util/assert.hpp>
#include <sophia/util/logger.hpp> #include <sophia/util/logger.hpp>
namespace sophia { namespace sophia {
@@ -38,7 +38,7 @@ Mesh::Mesh(Device& device, const Builder& builder) : device{device} {
Mesh::~Mesh() {} Mesh::~Mesh() {}
void Mesh::bind(vk::CommandBuffer commandBuffer) { void Mesh::bind(vk::CommandBuffer commandBuffer) const {
vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()}; vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()};
vk::DeviceSize offset[] = {0}; vk::DeviceSize offset[] = {0};
commandBuffer.bindVertexBuffers(0, 1, buffers, offset); 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) { if (this->hasIndexBuffer) {
commandBuffer.drawIndexed(this->indexCount, 1, 0, 0, 0); commandBuffer.drawIndexed(this->indexCount, 1, 0, 0, 0);
} else { } else {
@@ -59,7 +59,7 @@ void Mesh::draw(vk::CommandBuffer commandBuffer) {
void Mesh::createVertexBuffers(const std::vector<Vertex>& vertices) { void Mesh::createVertexBuffers(const std::vector<Vertex>& vertices) {
this->vertexCount = static_cast<u32>(vertices.size()); this->vertexCount = static_cast<u32>(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; vk::DeviceSize bufferSize = sizeof(vertices[0]) * this->vertexCount;
+5 -2
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <sophia/types.hpp>
#include <vector> #include <vector>
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
@@ -14,6 +15,8 @@ namespace sophia {
class Mesh { class Mesh {
public: public:
using HandleType = MeshHandle;
struct Vertex { struct Vertex {
glm::vec2 position{}; glm::vec2 position{};
glm::vec3 color{}; glm::vec3 color{};
@@ -46,8 +49,8 @@ class Mesh {
// static std::unique_ptr<Mesh> createMeshFromFile( // static std::unique_ptr<Mesh> createMeshFromFile(
// Device& device, const std::string& filepath); // Device& device, const std::string& filepath);
void bind(vk::CommandBuffer commandBuffer); void bind(vk::CommandBuffer commandBuffer) const;
void draw(vk::CommandBuffer commandBuffer); void draw(vk::CommandBuffer commandBuffer) const;
private: private:
void createVertexBuffers(const std::vector<Vertex>& vertices); void createVertexBuffers(const std::vector<Vertex>& vertices);
+3 -3
View File
@@ -1,7 +1,7 @@
#include "render_context.hpp" #include "render_context.hpp"
#include <cassert>
#include <limits> #include <limits>
#include <sophia/util/assert.hpp>
#include <sophia/util/logger.hpp> #include <sophia/util/logger.hpp>
#include <stdexcept> #include <stdexcept>
@@ -16,7 +16,7 @@ RenderContext::RenderContext(Window& window, Device& device)
RenderContext::~RenderContext() { this->device.waitIdle(); } RenderContext::~RenderContext() { this->device.waitIdle(); }
vk::CommandBuffer RenderContext::beginFrame() { vk::CommandBuffer RenderContext::beginFrame() {
assert(!this->isFrameStarted && SOPHIA_ASSERT(!this->isFrameStarted,
"Can't call beginFrame while already in progress"); "Can't call beginFrame while already in progress");
FrameData& frame = this->frames[this->currentFrameIndex]; FrameData& frame = this->frames[this->currentFrameIndex];
@@ -71,7 +71,7 @@ vk::CommandBuffer RenderContext::beginFrame() {
} }
void RenderContext::endFrame() { void RenderContext::endFrame() {
assert(this->isFrameStarted && SOPHIA_ASSERT(this->isFrameStarted,
"Can't call endFrame while no frame is in progress"); "Can't call endFrame while no frame is in progress");
FrameData& frame = this->frames[this->currentFrameIndex]; FrameData& frame = this->frames[this->currentFrameIndex];