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 <sophia/util/assert.hpp>
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);
@@ -1,6 +1,6 @@
#include "descriptor_set_layout.hpp"
#include <cassert>
#include <sophia/util/assert.hpp>
#include <stdexcept>
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};
+9 -9
View File
@@ -1,6 +1,6 @@
#include "descriptor_writer.hpp"
#include <cassert>
#include <sophia/util/assert.hpp>
#include <stdexcept>
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;
+2 -2
View File
@@ -2,10 +2,10 @@
#include <imgui_impl_vulkan.h>
#include <cassert>
#include <memory>
#include <optional>
#include <sophia/types.hpp>
#include <sophia/util/assert.hpp>
#include <vector>
#include <vulkan/vulkan.hpp>
@@ -41,7 +41,7 @@ class Device {
~Device();
const vk::Device* get() const {
assert(this->device);
SOPHIA_ASSERT(this->device);
return &this->device.get();
}
+4 -4
View File
@@ -1,6 +1,6 @@
#include "mesh.hpp"
#include <cassert>
#include <sophia/util/assert.hpp>
#include <sophia/util/logger.hpp>
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<Vertex>& vertices) {
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;
+5 -2
View File
@@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <sophia/types.hpp>
#include <vector>
#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<Mesh> 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<Vertex>& vertices);
+5 -5
View File
@@ -1,7 +1,7 @@
#include "render_context.hpp"
#include <cassert>
#include <limits>
#include <sophia/util/assert.hpp>
#include <sophia/util/logger.hpp>
#include <stdexcept>
@@ -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();