From 0fe582358bed4803da9f2ad5bf4c2763bf02402f Mon Sep 17 00:00:00 2001 From: Nate Trost Date: Tue, 17 Nov 2026 09:50:55 -0600 Subject: [PATCH] simple_renderer: Fix release build, decouple debug macros from agdktunnel Fix simple_renderer so release builds compile and the library no longer depends on agdktunnel headers: - Release builds failed to compile. Under NDEBUG, RENDERER_CHECK_VK was defined with one parameter, but every call site passes two (result, message), so `./gradlew assembleRelease` failed at every Vulkan result check. Release (RelWithDebInfo) builds do get -O2 -DNDEBUG, but release had simply never been built. - renderer_debug.h included agdktunnel's common.hpp for ALOGE/ALOGI and MY_ASSERT, so simple_renderer could not be built without agdktunnel's headers. The macros now only use base_game_framework's DebugManager (tag "SimpleRenderer") and the C++ library: - RENDERER_ERROR / RENDERER_LOG log through DebugManager. - RENDERER_ASSERT logs the condition, file and line, then aborts. Under NDEBUG it is compiled out: the condition is type checked as a bool but not evaluated. - RENDERER_CHECK_VK and RENDERER_CHECK_GLES are bool expressions in all builds (true if there was no error), so they behave the same in debug and release when used in conditions. Vulkan results are checked and logged in all builds, which only costs a comparison. GLES errors are only checked in debug builds, since glGetError can stall the pipeline; in release RENDERER_CHECK_GLES is always true. - RENDERER_ERROR, RENDERER_LOG and RENDERER_ASSERT are single statements that need a trailing ';'. Call sites that relied on ALOGE/MY_ASSERT supplying their own ';' or braces now have one. - renderer_render_pass_gles.cpp got the GLES headers only through common.hpp. It now includes itself. - IndexBufferVk and VertexBufferVk checked staging_alloc_result a second time instead of buffer_alloc_result, so a failed index/vertex buffer allocation went unnoticed. Now that release builds run, where a failed check logs instead of aborting, both also stop on allocation failure instead of copying into a null mapping or recording a copy into a null buffer. Their buffer handles start as VK_NULL_HANDLE, and the destructors skip buffers that were never created. - RendererCheckVk / RendererCheckGLES handle a null message. Two positive VkResult cases that could never be reached in the negative-result switch are removed. The shader info log buffers are zero-initialized before glGet*InfoLog fills them. Tested on a Pixel 10 Pro with agdktunnel: - assembleDebug and assembleRelease both build (release failed before). - A test file using the macros compiles with -Wall -Wextra -Werror -Wunused-variable in debug and release: as conditions, on a bit-field, and in an unbraced if/else. - Debug Vulkan, debug forced GLES, and release (signed with the debug key, non-debuggable): launch, play, background and resume work, and renderer logs appear under the SimpleRenderer tag. There are no assertion failures, API errors or crashes. Not exercised: the buffer allocation failure paths (no allocation failure could be forced). --- agdk/common/simple_renderer/renderer_debug.h | 59 ++++++++++++++++--- .../simple_renderer/renderer_debug_gles.cpp | 5 +- .../simple_renderer/renderer_debug_vk.cpp | 9 +-- agdk/common/simple_renderer/renderer_gles.cpp | 2 +- .../renderer_index_buffer_vk.cpp | 27 +++++++-- .../renderer_index_buffer_vk.h | 4 +- .../renderer_render_pass_gles.cpp | 1 + .../renderer_render_state_gles.cpp | 12 ++-- .../renderer_render_state_vk.cpp | 8 +-- .../simple_renderer/renderer_resources.cpp | 14 ++--- .../renderer_shader_program_gles.cpp | 20 +++---- .../renderer_shader_program_vk.cpp | 4 +- .../simple_renderer/renderer_texture_vk.cpp | 6 +- .../renderer_uniform_buffer_gles.cpp | 10 ++-- .../renderer_uniform_buffer_vk.cpp | 10 ++-- .../renderer_vertex_buffer_vk.cpp | 29 ++++++--- .../renderer_vertex_buffer_vk.h | 4 +- agdk/common/simple_renderer/renderer_vk.cpp | 6 +- 18 files changed, 151 insertions(+), 79 deletions(-) diff --git a/agdk/common/simple_renderer/renderer_debug.h b/agdk/common/simple_renderer/renderer_debug.h index efbf187e..7663c70e 100644 --- a/agdk/common/simple_renderer/renderer_debug.h +++ b/agdk/common/simple_renderer/renderer_debug.h @@ -17,22 +17,65 @@ #ifndef SIMPLERENDERER_DEBUG_H #define SIMPLERENDERER_DEBUG_H -#include "common.hpp" +#include +#include "debug_manager.h" -#define RENDERER_ERROR ALOGE -#define RENDERER_LOG ALOGI -#define RENDERER_ASSERT MY_ASSERT +// Logging, assertion and API error check macros for simple_renderer. They only depend on +// base_game_framework (DebugManager), not on the application using the renderer. +// RENDERER_ERROR, RENDERER_LOG and RENDERER_ASSERT are statements and must be followed +// by a ';'. RENDERER_CHECK_GLES and RENDERER_CHECK_VK are bool expressions (true if there +// was no error) in all builds, so they can be used in conditions. + +#define RENDERER_LOG_TAG "SimpleRenderer" + +#define RENDERER_ERROR(...) \ + ::base_game_framework::DebugManager::Log( \ + ::base_game_framework::DebugManager::kLog_Channel_Default, \ + ::base_game_framework::DebugManager::kLog_Level_Error, RENDERER_LOG_TAG, __VA_ARGS__) + +#define RENDERER_LOG(...) \ + ::base_game_framework::DebugManager::Log( \ + ::base_game_framework::DebugManager::kLog_Channel_Default, \ + ::base_game_framework::DebugManager::kLog_Level_Info, RENDERER_LOG_TAG, __VA_ARGS__) #ifdef NDEBUG -#define RENDERER_CHECK_GLES(a) -#define RENDERER_CHECK_VK(b) +// Release builds: assertions are compiled out (the condition is type checked but not +// evaluated), and GLES errors are not checked, since glGetError can stall the pipeline. +// Vulkan results are still checked, which only costs a comparison, so failures are logged +// in release too. +#define RENDERER_ASSERT(cond) do { (void)sizeof((cond) ? true : false); } while (0) +#define RENDERER_CHECK_GLES(message) ::simple_renderer::RendererCheckGLESDisabled(message) #else -#define RENDERER_CHECK_GLES(a) RendererCheckGLES(a) -#define RENDERER_CHECK_VK(a, b) RendererCheckVk(a, b) +#define RENDERER_ASSERT(cond) \ + do { \ + if (!(cond)) { \ + ::simple_renderer::RendererAssertFailed(#cond, __FILE__, __LINE__); \ + } \ + } while (0) +#define RENDERER_CHECK_GLES(message) ::simple_renderer::RendererCheckGLES(message) #endif +#define RENDERER_CHECK_VK(result, message) ::simple_renderer::RendererCheckVk(result, message) + namespace simple_renderer { + +// Logs the failed assertion and aborts +[[noreturn]] inline void RendererAssertFailed(const char* condition, const char* file, + int line) { + RENDERER_ERROR("ASSERTION FAILED: %s (%s:%d)", condition, file, line); + std::abort(); +} + +// Logs any pending GL errors. Returns true if there were none, asserts in debug builds +// otherwise. bool RendererCheckGLES(const char* message); +// Release builds don't check GL errors (see RENDERER_CHECK_GLES) +inline bool RendererCheckGLESDisabled(const char* message) { + (void)message; + return true; +} +// Logs a failed VkResult. Returns true if result is not an error, asserts in debug builds +// otherwise. bool RendererCheckVk(int result, const char* message); } diff --git a/agdk/common/simple_renderer/renderer_debug_gles.cpp b/agdk/common/simple_renderer/renderer_debug_gles.cpp index c1209afd..bfe6aec5 100644 --- a/agdk/common/simple_renderer/renderer_debug_gles.cpp +++ b/agdk/common/simple_renderer/renderer_debug_gles.cpp @@ -42,10 +42,11 @@ bool RendererCheckGLES(const char* message) { default: break; } - RENDERER_ERROR("GL error: (%u) %s from %s", error, error_string, message) + RENDERER_ERROR("GL error: (%u) %s from %s", error, error_string, + (message != nullptr) ? message : ""); error = glGetError(); } - RENDERER_ASSERT(no_error) + RENDERER_ASSERT(no_error); return no_error; } diff --git a/agdk/common/simple_renderer/renderer_debug_vk.cpp b/agdk/common/simple_renderer/renderer_debug_vk.cpp index 8bbb4c71..1855e3dc 100644 --- a/agdk/common/simple_renderer/renderer_debug_vk.cpp +++ b/agdk/common/simple_renderer/renderer_debug_vk.cpp @@ -55,14 +55,10 @@ bool RendererCheckVk(int result, const char* message) { error_string = "VK_ERROR_FRAGMENTATION"; break; case VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS: error_string = "VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS"; break; - case VK_PIPELINE_COMPILE_REQUIRED: - error_string = "VK_PIPELINE_COMPILE_REQUIRED"; break; case VK_ERROR_SURFACE_LOST_KHR: error_string = "VK_ERROR_SURFACE_LOST_KHR"; break; case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: error_string = "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR"; break; - case VK_SUBOPTIMAL_KHR: - error_string = "VK_SUBOPTIMAL_KHR"; break; case VK_ERROR_OUT_OF_DATE_KHR: error_string = "VK_ERROR_OUT_OF_DATE_KHR"; break; case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: @@ -80,8 +76,9 @@ bool RendererCheckVk(int result, const char* message) { default: break; } - RENDERER_ERROR("Vk error: (%d) %s from %s", result, error_string, message) - RENDERER_ASSERT(false) + RENDERER_ERROR("Vk error: (%d) %s from %s", result, error_string, + (message != nullptr) ? message : ""); + RENDERER_ASSERT(false); } return (result >= 0); } diff --git a/agdk/common/simple_renderer/renderer_gles.cpp b/agdk/common/simple_renderer/renderer_gles.cpp index 16a7d25d..5044ea5d 100644 --- a/agdk/common/simple_renderer/renderer_gles.cpp +++ b/agdk/common/simple_renderer/renderer_gles.cpp @@ -86,7 +86,7 @@ void RendererGLES::BeginFrame( resources_.ProcessDeleteQueue(); EGLBoolean result = eglMakeCurrent(egl_display_, egl_surface_, egl_surface_, egl_context_); if (result == EGL_FALSE) { - RENDERER_ERROR("eglMakeCurrent failed: %d", eglGetError()) + RENDERER_ERROR("eglMakeCurrent failed: %d", eglGetError()); } // Make sure errors are cleared at top of frame diff --git a/agdk/common/simple_renderer/renderer_index_buffer_vk.cpp b/agdk/common/simple_renderer/renderer_index_buffer_vk.cpp index d300d910..afaf9010 100644 --- a/agdk/common/simple_renderer/renderer_index_buffer_vk.cpp +++ b/agdk/common/simple_renderer/renderer_index_buffer_vk.cpp @@ -40,8 +40,16 @@ IndexBufferVk::IndexBufferVk(const IndexBuffer::IndexBufferCreationParams& param VmaAllocationInfo staging_info = {}; const VkResult staging_alloc_result = vmaCreateBuffer(allocator, &create_info, &alloc_info, &staging_buffer, &staging_alloc, &staging_info); - RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (staging)"); - RENDERER_ASSERT(staging_info.pMappedData != nullptr) + // The checks below log (and assert in debug builds); in release builds an allocation + // failure leaves the buffer empty instead of using null handles or memory. + if (!RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (staging)")) { + return; + } + RENDERER_ASSERT(staging_info.pMappedData != nullptr); + if (staging_info.pMappedData == nullptr) { + vmaDestroyBuffer(allocator, staging_buffer, staging_alloc); + return; + } memcpy(staging_info.pMappedData, params.index_data, params.data_byte_size); create_info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT; @@ -49,7 +57,12 @@ IndexBufferVk::IndexBufferVk(const IndexBuffer::IndexBufferCreationParams& param const VkResult buffer_alloc_result = vmaCreateBuffer(allocator, &create_info, &alloc_info, &index_buffer_, &index_buffer_alloc_, nullptr); - RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (index buffer)"); + if (!RENDERER_CHECK_VK(buffer_alloc_result, "vmaCreateBuffer (index buffer)")) { + index_buffer_ = VK_NULL_HANDLE; + index_buffer_alloc_ = VK_NULL_HANDLE; + vmaDestroyBuffer(allocator, staging_buffer, staging_alloc); + return; + } VkCommandBuffer command_buffer = renderer.BeginStagingCommandBuffer(); VkBufferCopy copy_region = {}; @@ -62,8 +75,10 @@ IndexBufferVk::IndexBufferVk(const IndexBuffer::IndexBufferCreationParams& param } IndexBufferVk::~IndexBufferVk() { - RENDERER_ASSERT(index_buffer_ != VK_NULL_HANDLE) - RendererVk& renderer = RendererVk::GetInstanceVk(); - vmaDestroyBuffer(renderer.GetAllocator(), index_buffer_, index_buffer_alloc_); + // Null if creation failed + if (index_buffer_ != VK_NULL_HANDLE) { + RendererVk& renderer = RendererVk::GetInstanceVk(); + vmaDestroyBuffer(renderer.GetAllocator(), index_buffer_, index_buffer_alloc_); + } } } \ No newline at end of file diff --git a/agdk/common/simple_renderer/renderer_index_buffer_vk.h b/agdk/common/simple_renderer/renderer_index_buffer_vk.h index 0324a312..4c11264e 100644 --- a/agdk/common/simple_renderer/renderer_index_buffer_vk.h +++ b/agdk/common/simple_renderer/renderer_index_buffer_vk.h @@ -31,8 +31,8 @@ class IndexBufferVk : public IndexBuffer { VkBuffer GetIndexBuffer() const { return index_buffer_; } private: - VkBuffer index_buffer_; - VmaAllocation index_buffer_alloc_; + VkBuffer index_buffer_ = VK_NULL_HANDLE; + VmaAllocation index_buffer_alloc_ = VK_NULL_HANDLE; }; } // namespace simple_renderer diff --git a/agdk/common/simple_renderer/renderer_render_pass_gles.cpp b/agdk/common/simple_renderer/renderer_render_pass_gles.cpp index ffe5e1ff..4a894fe3 100644 --- a/agdk/common/simple_renderer/renderer_render_pass_gles.cpp +++ b/agdk/common/simple_renderer/renderer_render_pass_gles.cpp @@ -15,6 +15,7 @@ */ #include "renderer_render_pass_gles.h" +#include #include "renderer_debug.h" namespace simple_renderer { diff --git a/agdk/common/simple_renderer/renderer_render_state_gles.cpp b/agdk/common/simple_renderer/renderer_render_state_gles.cpp index 74315efe..f14e77c1 100644 --- a/agdk/common/simple_renderer/renderer_render_state_gles.cpp +++ b/agdk/common/simple_renderer/renderer_render_state_gles.cpp @@ -141,19 +141,19 @@ void RenderStateGLES::InitializeAttributes(const GLuint program_handle) { vertex_attribute_locations_[kAttribute_Position] = glGetAttribLocation( program_handle, vertex_attribute_names[kAttribute_Position]); RENDERER_CHECK_GLES("glGetUniformLocation (position)"); - RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_Position] >= 0) + RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_Position] >= 0); if (state_vertex_layout_ == VertexBuffer::kVertexFormat_P3T2 || state_vertex_layout_ == VertexBuffer::kVertexFormat_P3T2C4) { vertex_attribute_locations_[kAttribute_TexCoord] = glGetAttribLocation( program_handle, vertex_attribute_names[kAttribute_TexCoord]); RENDERER_CHECK_GLES("glGetUniformLocation (texture)"); - RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_TexCoord] >= 0) + RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_TexCoord] >= 0); sampler_location_ = glGetUniformLocation( program_handle, sampler_name); RENDERER_CHECK_GLES("glGetUniformLocation (sampler)"); - RENDERER_ASSERT(sampler_location_ >= 0) + RENDERER_ASSERT(sampler_location_ >= 0); } else { vertex_attribute_locations_[kAttribute_TexCoord] = -1; sampler_location_ = -1; @@ -164,7 +164,7 @@ void RenderStateGLES::InitializeAttributes(const GLuint program_handle) { vertex_attribute_locations_[kAttribute_Color] = glGetAttribLocation( program_handle, vertex_attribute_names[kAttribute_Color]); RENDERER_CHECK_GLES("glGetUniformLocation (color)"); - RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_Color] >= 0) + RENDERER_ASSERT(vertex_attribute_locations_[kAttribute_Color] >= 0); } else { vertex_attribute_locations_[kAttribute_Color] = -1; } @@ -173,12 +173,12 @@ void RenderStateGLES::InitializeAttributes(const GLuint program_handle) { void RenderStateGLES::InitializeUniforms(const GLuint program_handle) { const UniformBufferGLES& buffer = GetUniformBuffer(); uint32_t i = 0; - RENDERER_ASSERT(buffer.GetBufferElementCount() <= UniformBuffer::kMaxUniforms) + RENDERER_ASSERT(buffer.GetBufferElementCount() <= UniformBuffer::kMaxUniforms); while (i < buffer.GetBufferElementCount()) { const char* element_name = buffer.GetElement(i).element_name; uniform_locations_[i] = glGetUniformLocation(program_handle, element_name); RENDERER_CHECK_GLES("glGetUniformLocation (shader uniform)"); - RENDERER_ASSERT(uniform_locations_[i] >= 0) + RENDERER_ASSERT(uniform_locations_[i] >= 0); ++i; } diff --git a/agdk/common/simple_renderer/renderer_render_state_vk.cpp b/agdk/common/simple_renderer/renderer_render_state_vk.cpp index ec32b2cc..8c5ba96c 100644 --- a/agdk/common/simple_renderer/renderer_render_state_vk.cpp +++ b/agdk/common/simple_renderer/renderer_render_state_vk.cpp @@ -109,7 +109,7 @@ static uint32_t ConfigureAttributeDescriptions(const VertexBuffer::VertexFormat attribute_offset += kPositionAttributeSize; if (state_vertex_layout == VertexBuffer::kVertexFormat_P3T2 || state_vertex_layout == VertexBuffer::kVertexFormat_P3T2C4) { - RENDERER_ASSERT(attribute_count < max_attributes) + RENDERER_ASSERT(attribute_count < max_attributes); attributes->binding = 0; attributes->location = attribute_count; attributes->format = VK_FORMAT_R32G32_SFLOAT; @@ -120,7 +120,7 @@ static uint32_t ConfigureAttributeDescriptions(const VertexBuffer::VertexFormat } if (state_vertex_layout == VertexBuffer::kVertexFormat_P3C4 || state_vertex_layout == VertexBuffer::kVertexFormat_P3T2C4) { - RENDERER_ASSERT(attribute_count < max_attributes) + RENDERER_ASSERT(attribute_count < max_attributes); attributes->binding = 0; attributes->location = attribute_count; attributes->format = VK_FORMAT_R32G32B32A32_SFLOAT; @@ -185,10 +185,10 @@ RenderStateVk::RenderStateVk(const RenderStateCreationParams& params) : state_program_->GetVertexDebugName().c_str(), state_program_->GetFragmentDebugName().c_str(), state_uniform_->GetBufferDebugName().c_str(), - state_vertex_layout_) + state_vertex_layout_); const RenderPassVk& render_pass_vk = *(static_cast(render_pass_.get())); VkRenderPass render_pass = render_pass_vk.GetRenderPassVk(); - RENDERER_ASSERT(render_pass != VK_NULL_HANDLE) + RENDERER_ASSERT(render_pass != VK_NULL_HANDLE); descriptor_set_layout_ = RendererVk::GetInstanceVk().GetDescriptorSetLayout(params.state_vertex_layout); diff --git a/agdk/common/simple_renderer/renderer_resources.cpp b/agdk/common/simple_renderer/renderer_resources.cpp index 2adc4902..56809984 100644 --- a/agdk/common/simple_renderer/renderer_resources.cpp +++ b/agdk/common/simple_renderer/renderer_resources.cpp @@ -23,37 +23,37 @@ static constexpr long kExpectedUseCount = 1; void RendererResources::ProcessDeleteQueue() { while (!index_buffer_delete_queue_.empty()) { - RENDERER_ASSERT(index_buffer_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(index_buffer_delete_queue_.front().use_count() == kExpectedUseCount); index_buffer_delete_queue_.pop(); } while (!render_state_delete_queue_.empty()) { - RENDERER_ASSERT(render_state_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(render_state_delete_queue_.front().use_count() == kExpectedUseCount); render_state_delete_queue_.pop(); } while (!render_pass_delete_queue_.empty()) { - RENDERER_ASSERT(render_pass_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(render_pass_delete_queue_.front().use_count() == kExpectedUseCount); render_pass_delete_queue_.pop(); } while (!shader_program_delete_queue_.empty()) { - RENDERER_ASSERT(shader_program_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(shader_program_delete_queue_.front().use_count() == kExpectedUseCount); shader_program_delete_queue_.pop(); } while (!texture_delete_queue_.empty()) { - RENDERER_ASSERT(texture_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(texture_delete_queue_.front().use_count() == kExpectedUseCount); texture_delete_queue_.pop(); } while (!uniform_buffer_delete_queue_.empty()) { - RENDERER_ASSERT(uniform_buffer_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(uniform_buffer_delete_queue_.front().use_count() == kExpectedUseCount); uniform_buffer_delete_queue_.pop(); } while (!vertex_buffer_delete_queue_.empty()) { - RENDERER_ASSERT(vertex_buffer_delete_queue_.front().use_count() == kExpectedUseCount) + RENDERER_ASSERT(vertex_buffer_delete_queue_.front().use_count() == kExpectedUseCount); vertex_buffer_delete_queue_.pop(); } } diff --git a/agdk/common/simple_renderer/renderer_shader_program_gles.cpp b/agdk/common/simple_renderer/renderer_shader_program_gles.cpp index b14d8b05..6bd14449 100644 --- a/agdk/common/simple_renderer/renderer_shader_program_gles.cpp +++ b/agdk/common/simple_renderer/renderer_shader_program_gles.cpp @@ -24,12 +24,12 @@ static bool CheckProgramStatus(GLuint program_handle) { bool valid = true; glGetProgramiv(program_handle, GL_LINK_STATUS, &status); if (status == 0) { - char error_buffer[2048]; + char error_buffer[2048] = {}; valid = false; - RENDERER_ERROR("Shader program link failed %u", program_handle) + RENDERER_ERROR("Shader program link failed %u", program_handle); glGetProgramInfoLog(program_handle, sizeof(error_buffer) - 1, nullptr, error_buffer); - RENDERER_ERROR("*** Info log:\n%s", error_buffer) - RENDERER_ASSERT(false) + RENDERER_ERROR("*** Info log:\n%s", error_buffer); + RENDERER_ASSERT(false); } return valid; } @@ -39,12 +39,12 @@ static bool CheckShaderStatus(GLuint shader_handle) { bool valid = true; glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); if (status == 0) { - char error_buffer[2048]; + char error_buffer[2048] = {}; valid = false; - RENDERER_ERROR("Shader compile failed %u", shader_handle) + RENDERER_ERROR("Shader compile failed %u", shader_handle); glGetShaderInfoLog(shader_handle, sizeof(error_buffer) - 1, nullptr, error_buffer); - RENDERER_ERROR("*** Info log:\n%s", error_buffer) - RENDERER_ASSERT(false) + RENDERER_ERROR("*** Info log:\n%s", error_buffer); + RENDERER_ASSERT(false); } return valid; } @@ -78,8 +78,8 @@ ShaderProgramGLES::ShaderProgramGLES(const ShaderProgram::ShaderProgramCreationP } } } else { - RENDERER_ERROR("Failed to create shader program handles") - RENDERER_ASSERT(false) + RENDERER_ERROR("Failed to create shader program handles"); + RENDERER_ASSERT(false); } } diff --git a/agdk/common/simple_renderer/renderer_shader_program_vk.cpp b/agdk/common/simple_renderer/renderer_shader_program_vk.cpp index 7b5019d6..5ca705ed 100644 --- a/agdk/common/simple_renderer/renderer_shader_program_vk.cpp +++ b/agdk/common/simple_renderer/renderer_shader_program_vk.cpp @@ -24,8 +24,8 @@ ShaderProgramVk::ShaderProgramVk(const ShaderProgram::ShaderProgramCreationParam RendererVk& renderer = RendererVk::GetInstanceVk(); valid_program_ = false; // Input SPIR-V code must be 32 bit aligned - RENDERER_ASSERT((((uint64_t)params.vertex_shader_data) & 0x3) == 0) - RENDERER_ASSERT((((uint64_t)params.fragment_shader_data) & 0x3) == 0) + RENDERER_ASSERT((((uint64_t)params.vertex_shader_data) & 0x3) == 0); + RENDERER_ASSERT((((uint64_t)params.fragment_shader_data) & 0x3) == 0); VkShaderModuleCreateInfo vertex_create_info = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO }; vertex_create_info.codeSize = params.vertex_data_byte_count; diff --git a/agdk/common/simple_renderer/renderer_texture_vk.cpp b/agdk/common/simple_renderer/renderer_texture_vk.cpp index 2c81c1c3..5bb6c57b 100644 --- a/agdk/common/simple_renderer/renderer_texture_vk.cpp +++ b/agdk/common/simple_renderer/renderer_texture_vk.cpp @@ -90,7 +90,7 @@ static VkFormat GetTextureVkFormat(const Texture::TextureCreationParams& params) case Texture::kTextureCompression_ASTC_LDR_12x12: texture_format = VK_FORMAT_ASTC_4x4_UNORM_BLOCK; break; default: - RENDERER_ASSERT(false) + RENDERER_ASSERT(false); break; } } else if (params.format == Texture::kTextureFormat_ETC2) { @@ -108,7 +108,7 @@ static VkFormat GetTextureVkFormat(const Texture::TextureCreationParams& params) case Texture::kTextureCompression_ETC2_SRGB8_ALPHA8_EAC: texture_format = VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK; break; default: - RENDERER_ASSERT(false) + RENDERER_ASSERT(false); break; } } @@ -143,7 +143,7 @@ TextureVk::TextureVk(const Texture::TextureCreationParams& params) staging_alloc_result = vmaCreateBuffer(allocator, &create_info, &staging_create_info, &staging_buffer, &staging_alloc, &staging_info); RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (staging)"); - RENDERER_ASSERT(staging_info.pMappedData != nullptr) + RENDERER_ASSERT(staging_info.pMappedData != nullptr); memcpy(staging_info.pMappedData, params.texture_data, params.texture_sizes[0]); VkImageCreateInfo image_create_info = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; diff --git a/agdk/common/simple_renderer/renderer_uniform_buffer_gles.cpp b/agdk/common/simple_renderer/renderer_uniform_buffer_gles.cpp index dcca60b9..3c7698d9 100644 --- a/agdk/common/simple_renderer/renderer_uniform_buffer_gles.cpp +++ b/agdk/common/simple_renderer/renderer_uniform_buffer_gles.cpp @@ -41,7 +41,7 @@ UniformBufferGLES::UniformBufferGLES(const UniformBuffer::UniformBufferCreationP } RENDERER_LOG("Element %u : name: %s type: %u set: %u index: %u", i, element.element_name, element.element_type, - element.element_bind_set, element.element_bind_index) + element.element_bind_set, element.element_bind_index); ++i; } while (i < kMaxUniforms) { @@ -56,7 +56,7 @@ UniformBufferGLES::~UniformBufferGLES() { } uint32_t UniformBufferGLES::GetElementOffset(uint32_t index) const { - RENDERER_ASSERT(index < GetBufferElementCount()) + RENDERER_ASSERT(index < GetBufferElementCount()); if (index > GetBufferElementCount()) { index = 0; @@ -66,13 +66,13 @@ uint32_t UniformBufferGLES::GetElementOffset(uint32_t index) const { void UniformBufferGLES::SetBufferElementData(const uint32_t index, const float* data, const size_t size) { - RENDERER_ASSERT(index < GetBufferElementCount()) + RENDERER_ASSERT(index < GetBufferElementCount()); if (index < GetBufferElementCount()) { const uint32_t offset = element_offsets_[index]; const uint32_t end_offset = offset + (size / sizeof(float)); - RENDERER_ASSERT(offset < kMaxUniformBufferFloatSize) - RENDERER_ASSERT(end_offset <= kMaxUniformBufferFloatSize) + RENDERER_ASSERT(offset < kMaxUniformBufferFloatSize); + RENDERER_ASSERT(end_offset <= kMaxUniformBufferFloatSize); if (offset < kMaxUniformBufferFloatSize && end_offset <= kMaxUniformBufferFloatSize) { memcpy(&buffer_data_[offset], data, size); } diff --git a/agdk/common/simple_renderer/renderer_uniform_buffer_vk.cpp b/agdk/common/simple_renderer/renderer_uniform_buffer_vk.cpp index 62ad96bb..8456d9fa 100644 --- a/agdk/common/simple_renderer/renderer_uniform_buffer_vk.cpp +++ b/agdk/common/simple_renderer/renderer_uniform_buffer_vk.cpp @@ -43,7 +43,7 @@ UniformBufferVk::UniformBufferVk(const UniformBuffer::UniformBufferCreationParam } RENDERER_LOG("Element %u : name: %s type: %u set: %u index: %u", i, element.element_name, element.element_type, - element.element_bind_set, element.element_bind_index) + element.element_bind_set, element.element_bind_index); ++i; } while (i < kMaxUniforms) { @@ -66,7 +66,7 @@ UniformBufferVk::~UniformBufferVk() { } uint32_t UniformBufferVk::GetElementOffset(uint32_t index) const { - RENDERER_ASSERT(index < GetBufferElementCount()) + RENDERER_ASSERT(index < GetBufferElementCount()); if (index > GetBufferElementCount()) { index = 0; @@ -76,13 +76,13 @@ uint32_t UniformBufferVk::GetElementOffset(uint32_t index) const { void UniformBufferVk::SetBufferElementData(const uint32_t index, const float* data, const size_t size) { - RENDERER_ASSERT(index < GetBufferElementCount()) + RENDERER_ASSERT(index < GetBufferElementCount()); if (index < GetBufferElementCount()) { const uint32_t offset = element_offsets_[index]; const uint32_t end_offset = offset + (size / sizeof(float)); - RENDERER_ASSERT(offset < kMaxUniformBufferFloatSize) - RENDERER_ASSERT(end_offset < kMaxUniformBufferFloatSize) + RENDERER_ASSERT(offset < kMaxUniformBufferFloatSize); + RENDERER_ASSERT(end_offset < kMaxUniformBufferFloatSize); if (offset < kMaxUniformBufferFloatSize && end_offset < kMaxUniformBufferFloatSize) { memcpy(&buffer_data_[offset], data, size); } diff --git a/agdk/common/simple_renderer/renderer_vertex_buffer_vk.cpp b/agdk/common/simple_renderer/renderer_vertex_buffer_vk.cpp index b947e9f0..042daf8d 100644 --- a/agdk/common/simple_renderer/renderer_vertex_buffer_vk.cpp +++ b/agdk/common/simple_renderer/renderer_vertex_buffer_vk.cpp @@ -39,9 +39,17 @@ VertexBufferVk::VertexBufferVk(const VertexBuffer::VertexBufferCreationParams& p VmaAllocation staging_alloc = VK_NULL_HANDLE; VmaAllocationInfo staging_info = {}; const VkResult staging_alloc_result = vmaCreateBuffer(allocator, &create_info, &alloc_info, - &staging_buffer, &staging_alloc, &staging_info); - RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (staging)"); - RENDERER_ASSERT(staging_info.pMappedData != nullptr) + &staging_buffer, &staging_alloc, &staging_info); + // The checks below log (and assert in debug builds); in release builds an allocation + // failure leaves the buffer empty instead of using null handles or memory. + if (!RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (staging)")) { + return; + } + RENDERER_ASSERT(staging_info.pMappedData != nullptr); + if (staging_info.pMappedData == nullptr) { + vmaDestroyBuffer(allocator, staging_buffer, staging_alloc); + return; + } memcpy(staging_info.pMappedData, params.vertex_data, params.data_byte_size); create_info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; @@ -49,7 +57,12 @@ VertexBufferVk::VertexBufferVk(const VertexBuffer::VertexBufferCreationParams& p const VkResult buffer_alloc_result = vmaCreateBuffer(allocator, &create_info, &alloc_info, &vertex_buffer_, &vertex_buffer_alloc_, nullptr); - RENDERER_CHECK_VK(staging_alloc_result, "vmaCreateBuffer (vertex buffer)"); + if (!RENDERER_CHECK_VK(buffer_alloc_result, "vmaCreateBuffer (vertex buffer)")) { + vertex_buffer_ = VK_NULL_HANDLE; + vertex_buffer_alloc_ = VK_NULL_HANDLE; + vmaDestroyBuffer(allocator, staging_buffer, staging_alloc); + return; + } VkCommandBuffer command_buffer = renderer.BeginStagingCommandBuffer(); VkBufferCopy copy_region = {}; @@ -62,8 +75,10 @@ VertexBufferVk::VertexBufferVk(const VertexBuffer::VertexBufferCreationParams& p } VertexBufferVk::~VertexBufferVk() { - RENDERER_ASSERT(vertex_buffer_ != VK_NULL_HANDLE) - RendererVk& renderer = RendererVk::GetInstanceVk(); - vmaDestroyBuffer(renderer.GetAllocator(), vertex_buffer_, vertex_buffer_alloc_); + // Null if creation failed + if (vertex_buffer_ != VK_NULL_HANDLE) { + RendererVk& renderer = RendererVk::GetInstanceVk(); + vmaDestroyBuffer(renderer.GetAllocator(), vertex_buffer_, vertex_buffer_alloc_); + } } } \ No newline at end of file diff --git a/agdk/common/simple_renderer/renderer_vertex_buffer_vk.h b/agdk/common/simple_renderer/renderer_vertex_buffer_vk.h index df9de6fe..bf9b212b 100644 --- a/agdk/common/simple_renderer/renderer_vertex_buffer_vk.h +++ b/agdk/common/simple_renderer/renderer_vertex_buffer_vk.h @@ -31,8 +31,8 @@ class VertexBufferVk : public VertexBuffer { VkBuffer GetVertexBuffer() const { return vertex_buffer_; } private: - VkBuffer vertex_buffer_; - VmaAllocation vertex_buffer_alloc_; + VkBuffer vertex_buffer_ = VK_NULL_HANDLE; + VmaAllocation vertex_buffer_alloc_ = VK_NULL_HANDLE; }; } // namespace simple_renderer diff --git a/agdk/common/simple_renderer/renderer_vk.cpp b/agdk/common/simple_renderer/renderer_vk.cpp index 56cfbe56..69eaeb7f 100644 --- a/agdk/common/simple_renderer/renderer_vk.cpp +++ b/agdk/common/simple_renderer/renderer_vk.cpp @@ -124,7 +124,7 @@ void RendererVk::BeginFrame( display_manager.GetSwapchainFrameResourcesVk(frame_handle, swap_, true); active_extent_ = swap_.swapchain_extent; - RENDERER_ASSERT(swap_.swapchain_frame_index < descriptor_pools_.size()) + RENDERER_ASSERT(swap_.swapchain_frame_index < descriptor_pools_.size()); active_frame_pool_ = descriptor_pools_[swap_.swapchain_frame_index]; VkResult reset_result = vkResetDescriptorPool(vk_.device, active_frame_pool_, 0); RENDERER_CHECK_VK(reset_result, "vkResetDescriptorPool"); @@ -440,7 +440,7 @@ VkDescriptorSetLayout RendererVk::GetDescriptorSetLayout( } VkCommandBuffer RendererVk::BeginStagingCommandBuffer() { - RENDERER_ASSERT(staging_command_buffer_ != nullptr) + RENDERER_ASSERT(staging_command_buffer_ != nullptr); VkCommandBufferBeginInfo begin_info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, @@ -454,7 +454,7 @@ VkCommandBuffer RendererVk::BeginStagingCommandBuffer() { } void RendererVk::EndStagingCommandBuffer() { - RENDERER_ASSERT(staging_command_buffer_ != nullptr) + RENDERER_ASSERT(staging_command_buffer_ != nullptr); const VkResult end_result = vkEndCommandBuffer(staging_command_buffer_); RENDERER_CHECK_VK(end_result, "vkEndCommandBuffer (EndStagingCommandBuffer");