Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions agdk/common/simple_renderer/renderer_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,65 @@
#ifndef SIMPLERENDERER_DEBUG_H
#define SIMPLERENDERER_DEBUG_H

#include "common.hpp"
#include <cstdlib>
#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);
}

Expand Down
5 changes: 3 additions & 2 deletions agdk/common/simple_renderer/renderer_debug_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 3 additions & 6 deletions agdk/common/simple_renderer/renderer_debug_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion agdk/common/simple_renderer/renderer_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions agdk/common/simple_renderer/renderer_index_buffer_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,29 @@ 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;
alloc_info.flags = 0;
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 = {};
Expand All @@ -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_);
}
}
}
4 changes: 2 additions & 2 deletions agdk/common/simple_renderer/renderer_index_buffer_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions agdk/common/simple_renderer/renderer_render_pass_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "renderer_render_pass_gles.h"
#include <GLES3/gl3.h>
#include "renderer_debug.h"

namespace simple_renderer {
Expand Down
12 changes: 6 additions & 6 deletions agdk/common/simple_renderer/renderer_render_state_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions agdk/common/simple_renderer/renderer_render_state_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<RenderPassVk*>(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);
Expand Down
14 changes: 7 additions & 7 deletions agdk/common/simple_renderer/renderer_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
20 changes: 10 additions & 10 deletions agdk/common/simple_renderer/renderer_shader_program_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions agdk/common/simple_renderer/renderer_shader_program_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions agdk/common/simple_renderer/renderer_texture_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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 };
Expand Down
Loading
Loading