Skip to content

Commit

Permalink
printf: Improve the debugprintf settings
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-lunarg committed Sep 19, 2024
1 parent 9a46ae0 commit 6c97ee6
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 76 deletions.
55 changes: 52 additions & 3 deletions layers/VkLayer_khronos_validation.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@
"value": [
"error"
]
},
{
"key": "debug_action",
"value": "VK_DBG_LAYER_ACTION_LOG_MSG"
},
{
"key": "enable_message_limit",
"value": true
}
]
},
Expand Down Expand Up @@ -211,6 +219,14 @@
"value": [
"error"
]
},
{
"key": "debug_action",
"value": "VK_DBG_LAYER_ACTION_LOG_MSG"
},
{
"key": "enable_message_limit",
"value": true
}
]
},
Expand Down Expand Up @@ -288,6 +304,14 @@
"warn",
"perf"
]
},
{
"key": "debug_action",
"value": "VK_DBG_LAYER_ACTION_LOG_MSG"
},
{
"key": "enable_message_limit",
"value": true
}
]
},
Expand Down Expand Up @@ -363,6 +387,14 @@
"value": [
"error"
]
},
{
"key": "debug_action",
"value": "VK_DBG_LAYER_ACTION_LOG_MSG"
},
{
"key": "enable_message_limit",
"value": true
}
]
},
Expand Down Expand Up @@ -440,6 +472,14 @@
"value": [
"error"
]
},
{
"key": "debug_action",
"value": "VK_DBG_LAYER_ACTION_LOG_MSG"
},
{
"key": "enable_message_limit",
"value": true
}
]
},
Expand Down Expand Up @@ -511,8 +551,17 @@
{
"key": "report_flags",
"value": [
"error"
"error",
"info"
]
},
{
"key": "debug_action",
"value": ""
},
{
"key": "enable_message_limit",
"value": false
}
]
}
Expand Down Expand Up @@ -819,7 +868,7 @@
{
"key": "printf_verbose",
"label": "Printf verbose",
"description": "Set the verbosity of debug printf messages",
"description": "Will print out handles, instruction location, position in command buffer, and more",
"type": "BOOL",
"default": false,
"platforms": [
Expand All @@ -839,7 +888,7 @@
{
"key": "printf_buffer_size",
"label": "Printf buffer size",
"description": "Set the size in bytes of the buffer used by debug printf",
"description": "Set the size in bytes of the buffer per draw/dispatch/traceRays to hold the message",
"type": "INT",
"default": 1024,
"range": {
Expand Down
8 changes: 6 additions & 2 deletions layers/error_message/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,9 @@ VKAPI_ATTR VkBool32 VKAPI_CALL MessengerLogCallback(VkDebugUtilsMessageSeverityF

msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
<< "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << '\n';
msg_buffer << " Objects: " << callback_data->objectCount << '\n';
if (callback_data->objectCount > 0) {
msg_buffer << " Objects: " << callback_data->objectCount << '\n';
}
for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
msg_buffer << " [" << obj << "] " << std::hex << std::showbase
<< HandleToUint64(callback_data->pObjects[obj].objectHandle) << ", type: " << std::dec << std::noshowbase
Expand Down Expand Up @@ -777,7 +779,9 @@ VKAPI_ATTR VkBool32 VKAPI_CALL MessengerWin32DebugOutputMsg(VkDebugUtilsMessageS

msg_buffer << callback_data->pMessageIdName << "(" << msg_severity << " / " << msg_type
<< "): msgNum: " << callback_data->messageIdNumber << " - " << callback_data->pMessage << '\n';
msg_buffer << " Objects: " << callback_data->objectCount << '\n';
if (callback_data->objectCount > 0) {
msg_buffer << " Objects: " << callback_data->objectCount << '\n';
}

for (uint32_t obj = 0; obj < callback_data->objectCount; ++obj) {
msg_buffer << " [" << obj << "] " << std::hex << std::showbase
Expand Down
2 changes: 1 addition & 1 deletion layers/error_message/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class DebugReport {
// This mutex is defined as mutable since the normal usage for a debug report object is as 'const'. The mutable keyword allows
// the layers to continue this pattern, but also allows them to use/change this specific member for synchronization purposes.
mutable std::mutex debug_output_mutex;
uint32_t duplicate_message_limit = 0;
uint32_t duplicate_message_limit = 0; // zero will keep printing forever
const void *instance_pnext_chain{};
bool force_default_log_callback{false};
uint32_t device_created = 0;
Expand Down
10 changes: 4 additions & 6 deletions layers/gpu/core/gpu_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ struct GpuAVSettings {
validate_indirect_trace_rays_buffers = enabled;
validate_buffer_copies = enabled;
}
};

struct DebugPrintfSettings {
bool to_stdout = false;
bool verbose = false;
uint32_t buffer_size = 1024;
};
bool debug_printf_to_stdout = false;
bool debug_printf_verbose = false;
uint32_t debug_printf_buffer_size = 1024;
};
32 changes: 17 additions & 15 deletions layers/gpu/debug_printf/debug_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/

#include "gpu/debug_printf/debug_printf.h"
#include "error_message/log_message_type.h"
#include "error_message/logging.h"
#include "generated/layer_chassis_dispatch.h"
#include "chassis/chassis_modification_state.h"
#include "gpu/shaders/gpu_error_header.h"
Expand All @@ -38,14 +40,11 @@ void Validator::PostCreateDevice(const VkDeviceCreateInfo *pCreateInfo, const Lo
return;
}

verbose = printf_settings.verbose;
use_stdout = printf_settings.to_stdout;

// This option was published when DebugPrintf came out, leave to not break people's flow
// Deprecated right after the 1.3.280 SDK release
if (!GetEnvironment("DEBUG_PRINTF_TO_STDOUT").empty()) {
InternalWarning(device, loc, "DEBUG_PRINTF_TO_STDOUT was set, this is deprecated, please use VK_LAYER_PRINTF_TO_STDOUT");
use_stdout = true;
gpuav_settings.debug_printf_to_stdout = true;
}

debug_printf_binding_slot_ = (uint32_t)instrumentation_bindings_.size(); // get next free binding
Expand Down Expand Up @@ -340,39 +339,42 @@ void Validator::AnalyzeAndGenerateMessage(VkCommandBuffer command_buffer, VkQueu
shader_message << temp_string.c_str();
}

if (verbose) {
const bool use_stdout = gpuav_settings.debug_printf_to_stdout;
if (gpuav_settings.debug_printf_verbose) {
std::string debug_info_message = GenerateDebugInfoMessage(
command_buffer, instructions, debug_record->stage_id, debug_record->stage_info_0, debug_record->stage_info_1,
debug_record->stage_info_2, debug_record->instruction_position, tracker_info, debug_record->shader_id,
buffer_info.pipeline_bind_point, buffer_info.action_command_index);
if (use_stdout) {
std::cout << "WARNING-DEBUG-PRINTF " << shader_message.str() << '\n' << debug_info_message;
std::cout << "VVL-DEBUG-PRINTF " << shader_message.str() << '\n' << debug_info_message;
} else {
LogInfo("WARNING-DEBUG-PRINTF", queue, loc, "%s\n%s", shader_message.str().c_str(), debug_info_message.c_str());
LogObjectList objlist(queue, command_buffer);
LogInfo("VVL-DEBUG-PRINTF", objlist, loc, "%s\n%s", shader_message.str().c_str(), debug_info_message.c_str());
}

} else {
if (use_stdout) {
std::cout << shader_message.str();
} else {
// Don't let LogInfo process any '%'s in the string
LogInfo("WARNING-DEBUG-PRINTF", queue, loc, "%s", shader_message.str().c_str());
// LogInfo will print out a lot of extra information (Object handles, VUID, hash, etc)
// If the user doesn't set "verbose", but wants to use the debug callback, we should limit it to the bare minimum
debug_report->DebugLogMsg(kInformationBit, {}, shader_message.str().c_str(), "VVL-DEBUG-PRINTF");
}
}
index += debug_record->size;
}
if ((index - gpuav::kDebugPrintfOutputBufferData) != output_record_counts) {
std::stringstream message;
message << "Debug Printf message was truncated due to a buffer size (" << printf_settings.buffer_size
message << "Debug Printf message was truncated due to a buffer size (" << gpuav_settings.debug_printf_buffer_size
<< ") being too small for the messages. (This can be adjusted with VK_LAYER_PRINTF_BUFFER_SIZE or vkconfig)";
InternalWarning(queue, loc, message.str().c_str());
InternalWarning(command_buffer, loc, message.str().c_str());
}

// Only memset what is needed, in case we are only using a small portion of a large buffer_size.
// At the same time we want to make sure we don't memset past the actual VkBuffer allocation
uint32_t clear_size =
sizeof(uint32_t) * (debug_output_buffer[gpuav::kDebugPrintfOutputBufferSize] + gpuav::kDebugPrintfOutputBufferData);
clear_size = std::min(printf_settings.buffer_size, clear_size);
clear_size = std::min(gpuav_settings.debug_printf_buffer_size, clear_size);
memset(debug_output_buffer, 0, clear_size);
}

Expand Down Expand Up @@ -607,7 +609,7 @@ void Validator::AllocateDebugPrintfResources(const VkCommandBuffer cmd_buffer, c
// Allocate memory for the output block that the gpu will use to return values for printf
gpu::DeviceMemoryBlock output_block = {};
VkBufferCreateInfo buffer_info = vku::InitStructHelper();
buffer_info.size = printf_settings.buffer_size;
buffer_info.size = gpuav_settings.debug_printf_buffer_size;
buffer_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
VmaAllocationCreateInfo alloc_info = {};
alloc_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
Expand All @@ -624,12 +626,12 @@ void Validator::AllocateDebugPrintfResources(const VkCommandBuffer cmd_buffer, c
InternalError(cmd_buffer, loc, "Unable to allocate map memory.", true);
return;
}
memset(data, 0, printf_settings.buffer_size);
memset(data, 0, gpuav_settings.debug_printf_buffer_size);
vmaUnmapMemory(vma_allocator_, output_block.allocation);

// Write the descriptor
VkDescriptorBufferInfo output_desc_buffer_info = {};
output_desc_buffer_info.range = printf_settings.buffer_size;
output_desc_buffer_info.range = gpuav_settings.debug_printf_buffer_size;
output_desc_buffer_info.buffer = output_block.buffer;
output_desc_buffer_info.offset = 0;

Expand Down
4 changes: 0 additions & 4 deletions layers/gpu/debug_printf/debug_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,5 @@ class Validator : public gpu::GpuShaderInstrumentor {

std::shared_ptr<vvl::CommandBuffer> CreateCmdBufferState(VkCommandBuffer cb, const VkCommandBufferAllocateInfo* create_info,
const vvl::CommandPool* pool) final;

private:
bool verbose = false;
bool use_stdout = false;
};
} // namespace debug_printf
Loading

0 comments on commit 6c97ee6

Please sign in to comment.