diff --git a/MobileGL/Defines.h b/MobileGL/Defines.h index 48272aaf..46f58ff5 100644 --- a/MobileGL/Defines.h +++ b/MobileGL/Defines.h @@ -41,4 +41,14 @@ #define MOBILEGL_LOG_FILE_PATH "/sdcard/MG/latest.log" #else #define MOBILEGL_LOG_FILE_PATH "" -#endif \ No newline at end of file +#endif + +// =============================== Utils ================================ // +#define MOBILEGL_ASSERT(condition, ...) \ + do { \ + if (!(condition)) { \ + MGLOG_F("Assertion failed" __VA_OPT__(": ") __VA_ARGS__); \ + MGLOG_F(" at %s:%d (%s)", __FILE__, __LINE__, __func__); \ + assert(false); \ + } \ + } while (0) diff --git a/MobileGL/Includes.h b/MobileGL/Includes.h index f0cdc208..8e433b04 100644 --- a/MobileGL/Includes.h +++ b/MobileGL/Includes.h @@ -84,5 +84,5 @@ #endif // Post-includes for significant project headers -#include "MG_Util/Types.h" -#include "MG_Util/Debug/Log.h" \ No newline at end of file +#include "MG_Util/Debug/Log.h" +#include "MG_Util/Types.h" \ No newline at end of file diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 607282de..c53d627a 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -63,7 +63,7 @@ namespace MobileGL { if (bufSize == 0) return; - memcpy(dst, src, sz); + Memcpy(dst, src, sz); dst[sz] = '\0'; } @@ -426,14 +426,14 @@ namespace MobileGL { auto* ttype = programObject->GetUniformTType(location); if (!ttype->isMatrix() || ttype->getMatrixCols() != 3) - memcpy(params, pUBO + offset, size); + Memcpy(params, pUBO + offset, size); else { // TODO: we only deal with mat3 yet, deal with other types later // assuming float here, which may not be the case auto* pBase = pUBO + offset; for (int i = 0; i < ttype->getMatrixRows(); i++) { - memcpy((char*)params + ttype->getMatrixCols() * sizeof(float) * i, pBase + 4 * sizeof(float) * i, - ttype->getMatrixCols() * sizeof(float)); + Memcpy((char*)params + ttype->getMatrixCols() * sizeof(float) * i, + pBase + 4 * sizeof(float) * i, ttype->getMatrixCols() * sizeof(float)); } } } @@ -504,12 +504,15 @@ namespace MobileGL { } template - void Uniform_State(MG_State::GLState::ProgramObject& programObject, GLuint location, T* value, SizeT byteOffsetInsideUniform = 0) { + void Uniform_State(MG_State::GLState::ProgramObject& programObject, GLuint location, T* value, + SizeT byteOffsetInsideUniform = 0) { if (!programObject.IsUniformOpaqueAtLocation(location)) { auto size = programObject.GetUniformSizesInBytes(location); auto offset = programObject.GetUniformOffset(location); - assert(size >= ItemCount * sizeof(T)); - memcpy((char*)programObject.MapUBO() + offset + byteOffsetInsideUniform, value, ItemCount * sizeof(T)); + MOBILEGL_ASSERT(size >= ItemCount * sizeof(T), + "Uniform size mismatch, expected at least %zu bytes, got %zu bytes.", + ItemCount * sizeof(T), size); + Memcpy((char*)programObject.MapUBO() + offset + byteOffsetInsideUniform, value, ItemCount * sizeof(T)); } else { auto* ttype = programObject.GetUniformTType(location); if (ttype->isTexture() || ttype->isImage()) { @@ -716,12 +719,14 @@ namespace MobileGL { GLfloat transposedMatrix[9]; TransposeMatrix3x3(value + i * 9, transposedMatrix); for (int row = 0; row < 3; ++row) { - Uniform_State<3>(*programObject, location + i, transposedMatrix + row * 3, row * 4 * sizeof(float)); + Uniform_State<3>(*programObject, location + i, transposedMatrix + row * 3, + row * 4 * sizeof(float)); } } else { // No transpose needed, directly copy the matrix data for (int row = 0; row < 3; ++row) { - Uniform_State<3>(*programObject, location + i, value + i * 9 + row * 3, row * 4 * sizeof(float)); + Uniform_State<3>(*programObject, location + i, value + i * 9 + row * 3, + row * 4 * sizeof(float)); } } } diff --git a/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp b/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp index 31b66d12..15805fc4 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp @@ -17,9 +17,11 @@ namespace MobileGL { } void BufferObject::UploadData(DataPtr data, SizeT atOffset) { - assert(atOffset + data.size <= m_size); - assert(!m_isMapped); - memcpy(m_dataPtr->data() + atOffset, data.data, data.size); + MOBILEGL_ASSERT(atOffset + data.size <= m_size, + "UploadData out of bounds: atOffset (%zu) + data.size (%zu) > m_size (%zu)", atOffset, + data.size, m_size); + MOBILEGL_ASSERT(!m_isMapped, "Cannot upload data while buffer is mapped."); + Memcpy(m_dataPtr->data() + atOffset, data.data, data.size); m_dirtyRange.UnionUpdate(atOffset, atOffset + data.size); } @@ -32,7 +34,7 @@ namespace MobileGL { if (m_mappingAccess & BufferMappingAccessBit::Write) { // if we wrote to the buffer if (!(m_mappingAccess & BufferMappingAccessBit::FlushExplicit)) { // if we didn't flush explicitly - memcpy(m_dataPtr->data() + m_mappedRange.start, m_stagingData.data(), + Memcpy(m_dataPtr->data() + m_mappedRange.start, m_stagingData.data(), m_mappedRange.end - m_mappedRange.start); m_dirtyRange.UnionUpdate(m_mappedRange.start, m_mappedRange.end); } @@ -47,35 +49,44 @@ namespace MobileGL { } void BufferObject::FlushMemoryRange(SizeT offset, SizeT length) { - assert(m_isMapped); - assert((m_mappingAccess & BufferMappingAccessBit::FlushExplicit)); - assert((m_mappingAccess & BufferMappingAccessBit::Write)); + MOBILEGL_ASSERT(m_isMapped, "Buffer must be mapped to flush memory range."); + MOBILEGL_ASSERT((m_mappingAccess & BufferMappingAccessBit::FlushExplicit), + "Buffer must be mapped with FlushExplicit access to flush memory range."); + MOBILEGL_ASSERT((m_mappingAccess & BufferMappingAccessBit::Write), + "Buffer must be mapped with Write access to flush memory range."); SizeT start = m_mappedRange.start + offset; SizeT end = start + length; - assert(end <= m_mappedRange.end); + MOBILEGL_ASSERT(end <= m_mappedRange.end, + "Flush range out of bounds: mappedRange.end (%zu) < end (%zu)", m_mappedRange.end, end); - memcpy(m_dataPtr->data() + start, m_stagingData.data() + offset, length); + Memcpy(m_dataPtr->data() + start, m_stagingData.data() + offset, length); m_dirtyRange.UnionUpdate(start, end); } void BufferObject::UploadSubData(DataPtr data, SizeT atOffset) { - assert(!m_isMapped); - assert(atOffset + data.size <= m_size); + MOBILEGL_ASSERT(!m_isMapped, "Cannot upload sub data while buffer is mapped."); + MOBILEGL_ASSERT(atOffset + data.size <= m_size, + "UploadSubData out of bounds: atOffset (%zu) + data.size (%zu) > m_size (%zu)", + atOffset, data.size, m_size); - memcpy(m_dataPtr->data() + atOffset, data.data, data.size); + Memcpy(m_dataPtr->data() + atOffset, data.data, data.size); m_dirtyRange.UnionUpdate(atOffset, atOffset + data.size); } void BufferObject::CopyDataFrom(const SharedPtr& src, SizeT srcOffset, SizeT dstOffset, SizeT size) { - assert(!m_isMapped); - assert(!src->IsMapped()); - assert(srcOffset + size <= src->GetSize()); - assert(dstOffset + size <= m_size); + MOBILEGL_ASSERT(!m_isMapped, "Cannot copy data while buffer is mapped."); + MOBILEGL_ASSERT(!src->IsMapped(), "Cannot copy data from a buffer that is mapped."); + MOBILEGL_ASSERT(srcOffset + size <= src->GetSize(), + "Source buffer copy out of bounds: srcOffset (%zu) + size (%zu) > src->GetSize() (%zu)", + srcOffset, size, src->GetSize()); + MOBILEGL_ASSERT(dstOffset + size <= m_size, + "Destination buffer copy out of bounds: dstOffset (%zu) + size (%zu) > m_size (%zu)", + dstOffset, size, m_size); const Uint8* srcData = src->m_dataPtr->data() + srcOffset; - memcpy(m_dataPtr->data() + dstOffset, srcData, size); + Memcpy(m_dataPtr->data() + dstOffset, srcData, size); m_dirtyRange.UnionUpdate(dstOffset, dstOffset + size); } @@ -93,7 +104,7 @@ namespace MobileGL { if (!(m_mappingAccess & (BufferMappingAccessBit::InvalidateRange | BufferMappingAccessBit::InvalidateBuffer))) { - memcpy(m_stagingData.data(), m_dataPtr->data(), m_size); + Memcpy(m_stagingData.data(), m_dataPtr->data(), m_size); } return m_stagingData.data(); @@ -104,7 +115,9 @@ namespace MobileGL { } void* BufferObject::AcquireMemoryRange(Range1D range, Flags access) { - assert(range.end <= m_size && range.start <= range.end); + MOBILEGL_ASSERT(range.end <= m_size && range.start <= range.end, + "AcquireMemoryRange out of bounds: range (%zu, %zu) exceeds m_size (%zu)", range.start, + range.end, m_size); m_isMapped = true; m_mappingAccess = access; m_mappedRange = range; @@ -115,7 +128,7 @@ namespace MobileGL { if (!(access & (BufferMappingAccessBit::InvalidateRange | BufferMappingAccessBit::InvalidateBuffer))) { - memcpy(m_stagingData.data(), m_dataPtr->data() + range.start, m_stagingData.size()); + Memcpy(m_stagingData.data(), m_dataPtr->data() + range.start, m_stagingData.size()); } return m_stagingData.data(); diff --git a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp index 595f3932..50acdc0f 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp @@ -35,7 +35,7 @@ namespace MobileGL { return m_bindingSlots[i]; } } - assert(false); + MOBILEGL_ASSERT(false, "Invalid BufferTarget enum value: %d", static_cast(target)); return m_bindingSlots[0]; } @@ -62,14 +62,14 @@ namespace MobileGL { return m_bufferObjects.find(index) != m_bufferObjects.end(); } - BindingSlotRange1D & - BufferState::GetBindingPoint(BufferTarget target, Uint index) { + BindingSlotRange1D& BufferState::GetBindingPoint(BufferTarget target, Uint index) { for (SizeT i = 0; i < BufferBindPointTargets.size(); ++i) { if (BufferBindPointTargets[i] == target) { return m_bufferBindPointTargets[i][index]; } } - assert(false); + MOBILEGL_ASSERT(false, "Invalid BufferTarget enum value for binding point: %d", + static_cast(target)); return m_bufferBindPointTargets[0][index]; } } // namespace GLState diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index 8a1f8a3b..77a04028 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -53,13 +53,9 @@ namespace MobileGL { BindingSlot& GLContext::GetBufferBindingSlot(BufferTarget target) { if (target == BufferTarget::Index) { const auto& vao = m_vertexArrayState.GetBoundVertexArray(); - if (vao) { - return vao->GetIndexBufferBindingSlot(); - } else { - assert(false); - static BindingSlot defaultSlot(BufferTarget::Index); - return defaultSlot; - } + MOBILEGL_ASSERT(vao != nullptr, + "No VAO is currently bound when accessing index buffer binding slot."); + return vao->GetIndexBufferBindingSlot(); } return m_bufferState.GetBindingSlot(target); diff --git a/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp b/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp index e8614cc3..8aab573c 100644 --- a/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp +++ b/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp @@ -43,7 +43,7 @@ namespace MobileGL { return m_bindingSlots[i]; } } - assert(false); + MOBILEGL_ASSERT(false, "Invalid FramebufferTarget enum value: %d", static_cast(target)); return m_bindingSlots[0]; } diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp index a8e296ff..fde78a2f 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp @@ -370,7 +370,7 @@ namespace MobileGL { MGLOG_E("ProgramObject %u: GenerateBinary - last compiled shader src: \n%s", m_externalIndex, m_shaders[i]->GetShaderSource().c_str()); } - assert(res); // keep original assert but log first + MOBILEGL_ASSERT(res, "CompileShader failed during binary generation"); shaders[i] = res.value(); MGLOG_D("ProgramObject %u: GenerateBinary - compiled shader[%zu] -> TShader ptr %p", m_externalIndex, i, shaders[i].get()); @@ -385,7 +385,7 @@ namespace MobileGL { MGLOG_E("ProgramObject %u: GenerateBinary - LinkProgram failed during binary generation", m_externalIndex); } - assert(programResult); + MOBILEGL_ASSERT(programResult, "LinkProgram failed during binary generation"); auto program = programResult.value(); MGLOG_D("ProgramObject %u: GenerateBinary - got linked program object", m_externalIndex); @@ -398,7 +398,7 @@ namespace MobileGL { if (!binaryResult) { MGLOG_E("ProgramObject %u: GenerateBinary - GetSpirvBinaryFromProgram failed", m_externalIndex); } - assert(binaryResult); + MOBILEGL_ASSERT(binaryResult, "GetSpirvBinaryFromProgram failed"); m_generatedSpirv = Move(binaryResult.value()); MGLOG_D("ProgramObject %u: GenerateBinary - generated %zu SPIR-V modules", m_externalIndex, m_generatedSpirv.size()); diff --git a/MobileGL/MG_State/GLState/RenderState/RenderState.cpp b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp index b8b0d63b..cd937b9f 100644 --- a/MobileGL/MG_State/GLState/RenderState/RenderState.cpp +++ b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp @@ -161,9 +161,7 @@ namespace MobileGL { m_unpackParameters.LSBFirst = value != 0; break; default: - MGLOG_F("RenderState::SetPixelStoreParam: Invalid PixelStoreParam enum: %d", - static_cast(param)); - assert(false && "Invalid PixelStoreParam enum"); + MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast(param)); return; } } @@ -203,9 +201,7 @@ namespace MobileGL { case PixelStoreParam::UnpackLsbFirst: return m_unpackParameters.LSBFirst ? 1 : 0; default: - MGLOG_F("RenderState::GetPixelStoreParam: Invalid PixelStoreParam enum: %d", - static_cast(param)); - assert(false && "Invalid PixelStoreParam enum"); + MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast(param)); return 0; } } diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp index 5239dbc6..32b28a18 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp @@ -59,9 +59,7 @@ namespace MobileGL { MipmapLevelInternal& TextureObjectBase::GetMipmap(Int index) { if (index >= m_mipmaps.size()) { - MGLOG_F("TextureObjectBase::GetMipmap: Requested mipmap level %d exceeds available levels %zu", - index, m_mipmaps.size()); - assert(false && "Requested mipmap level exceeds available levels"); + MOBILEGL_ASSERT(false, "GetMipmap: index %d out of bounds, returning last mipmap level", index); index = static_cast(m_mipmaps.size() - 1); } return m_mipmaps[index]; @@ -100,9 +98,8 @@ namespace MobileGL { case TextureSwizzleParam::Alpha: return m_swizzleParams[3]; default: - MGLOG_F("TextureObjectBase::GetSwizzleParam: Invalid TextureSwizzleParam: %d", - static_cast(param)); - assert(false && "Invalid TextureSwizzleParam"); + MOBILEGL_ASSERT(false, "TextureObjectBase::GetSwizzleParam: Invalid TextureSwizzleParam: %d", + static_cast(param)); return TextureSwizzleParam::Red; } } @@ -122,9 +119,8 @@ namespace MobileGL { m_swizzleParams[3] = value; break; default: - MGLOG_F("TextureObjectBase::SetSwizzleParam: Invalid TextureSwizzleParam: %d", - static_cast(param)); - assert(false && "Invalid TextureSwizzleParam"); + MOBILEGL_ASSERT(false, "TextureObjectBase::SetSwizzleParam: Invalid TextureSwizzleParam: %d", + static_cast(param)); break; } } diff --git a/MobileGL/MG_Util/Types.h b/MobileGL/MG_Util/Types.h index d189ab25..3fc05b61 100644 --- a/MobileGL/MG_Util/Types.h +++ b/MobileGL/MG_Util/Types.h @@ -104,13 +104,14 @@ namespace MobileGL { SizeT end = ~0u; void Update(SizeT newStart, SizeT newEnd) { - assert(newStart <= newEnd); + MOBILEGL_ASSERT(newStart <= newEnd, "Range1D::Update: newStart (%zu) > newEnd (%zu)", newStart, newEnd); start = newStart; end = newEnd; } void UnionUpdate(SizeT newStart, SizeT newEnd) { - assert(newStart <= newEnd); + MOBILEGL_ASSERT(newStart <= newEnd, "Range1D::UnionUpdate: newStart (%zu) > newEnd (%zu)", newStart, + newEnd); if (start == end) { Update(newStart, newEnd); return; @@ -120,10 +121,13 @@ namespace MobileGL { } void IntersectionUpdate(SizeT newStart, SizeT newEnd) { - assert(newStart <= newEnd); + MOBILEGL_ASSERT(newStart <= newEnd, "Range1D::IntersectionUpdate: newStart (%zu) > newEnd (%zu)", newStart, + newEnd); start = std::max(start, newStart); end = std::min(end, newEnd); - assert(start <= end); + MOBILEGL_ASSERT(start <= end, + "Range1D::IntersectionUpdate resulted in invalid range: start (%zu) > end (%zu)", start, + end); } }; @@ -148,25 +152,20 @@ namespace MobileGL { }; template - class BindingSlotRange1D: public BindingSlot { + class BindingSlotRange1D : public BindingSlot { public: using TargetEnum = typename ObjectType::TargetEnum; BindingSlotRange1D() : BindingSlot() {} explicit BindingSlotRange1D(TargetEnum target, const Range1D& range = Range1D()) - : BindingSlot(target), m_range(range) {} + : BindingSlot(target), m_range(range) {} - Range1D GetRange() const { - return m_range; - } + Range1D GetRange() const { return m_range; } - void SetRange(const Range1D& range) { - m_range = range; - } + void SetRange(const Range1D& range) { m_range = range; } + + void ClearRange() { m_range = Range1D(); } - void ClearRange() { - m_range = Range1D(); - } private: Range1D m_range; };