Skip to content

Commit

Permalink
Add named resources and debug labels in RenderDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
clayjohn committed Jan 26, 2021
1 parent ad0f1c6 commit 7323cba
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 12 deletions.
66 changes: 66 additions & 0 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7040,6 +7040,72 @@ void RenderingDeviceVulkan::free(RID p_id) {
_free_internal(p_id);
}

// The full list of resources that can be named is in the VkObjectType enum
// We just expose the resources that are owned and can be accessed easily.
void RenderingDeviceVulkan::set_resource_name(RID p_id, const String p_name) {
if (texture_owner.owns(p_id)) {
Texture *texture = texture_owner.getornull(p_id);
if (texture->owner.is_null()) {
// Don't set the source texture's name when calling on a texture view
context->set_object_name(VK_OBJECT_TYPE_IMAGE, uint64_t(texture->image), p_name);
}
context->set_object_name(VK_OBJECT_TYPE_IMAGE_VIEW, uint64_t(texture->view), p_name + " View");
} else if (framebuffer_owner.owns(p_id)) {
//Framebuffer *framebuffer = framebuffer_owner.getornull(p_id);
// Not implemented for now as the relationship between Framebuffer and RenderPass is very complex
} else if (sampler_owner.owns(p_id)) {
VkSampler *sampler = sampler_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_SAMPLER, uint64_t(*sampler), p_name);
} else if (vertex_buffer_owner.owns(p_id)) {
Buffer *vertex_buffer = vertex_buffer_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_BUFFER, uint64_t(vertex_buffer->buffer), p_name);
} else if (index_buffer_owner.owns(p_id)) {
IndexBuffer *index_buffer = index_buffer_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_BUFFER, uint64_t(index_buffer->buffer), p_name);
} else if (shader_owner.owns(p_id)) {
Shader *shader = shader_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_PIPELINE_LAYOUT, uint64_t(shader->pipeline_layout), p_name + " Pipeline Layout");
for (int i = 0; i < shader->sets.size(); i++) {
context->set_object_name(VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, uint64_t(shader->sets[i].descriptor_set_layout), p_name);
}
} else if (uniform_buffer_owner.owns(p_id)) {
Buffer *uniform_buffer = uniform_buffer_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_BUFFER, uint64_t(uniform_buffer->buffer), p_name);
} else if (texture_buffer_owner.owns(p_id)) {
TextureBuffer *texture_buffer = texture_buffer_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_BUFFER, uint64_t(texture_buffer->buffer.buffer), p_name);
context->set_object_name(VK_OBJECT_TYPE_BUFFER_VIEW, uint64_t(texture_buffer->view), p_name + " View");
} else if (storage_buffer_owner.owns(p_id)) {
Buffer *storage_buffer = storage_buffer_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_BUFFER, uint64_t(storage_buffer->buffer), p_name);
} else if (uniform_set_owner.owns(p_id)) {
UniformSet *uniform_set = uniform_set_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_DESCRIPTOR_SET, uint64_t(uniform_set->descriptor_set), p_name);
} else if (render_pipeline_owner.owns(p_id)) {
RenderPipeline *pipeline = render_pipeline_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_PIPELINE, uint64_t(pipeline->pipeline), p_name);
context->set_object_name(VK_OBJECT_TYPE_PIPELINE_LAYOUT, uint64_t(pipeline->pipeline_layout), p_name + " Layout");
} else if (compute_pipeline_owner.owns(p_id)) {
ComputePipeline *pipeline = compute_pipeline_owner.getornull(p_id);
context->set_object_name(VK_OBJECT_TYPE_PIPELINE, uint64_t(pipeline->pipeline), p_name);
context->set_object_name(VK_OBJECT_TYPE_PIPELINE_LAYOUT, uint64_t(pipeline->pipeline_layout), p_name + " Layout");
} else {
ERR_PRINT("Attempted to name invalid ID: " + itos(p_id.get_id()));
}
}

void RenderingDeviceVulkan::draw_command_begin_label(String p_label_name, const Color p_color) {
context->command_begin_label(frames[frame].draw_command_buffer, p_label_name, p_color);
}

void RenderingDeviceVulkan::draw_command_insert_label(String p_label_name, const Color p_color) {
context->command_insert_label(frames[frame].draw_command_buffer, p_label_name, p_color);
}

void RenderingDeviceVulkan::draw_command_end_label() {
context->command_end_label(frames[frame].draw_command_buffer);
}

void RenderingDeviceVulkan::_finalize_command_bufers() {
if (draw_list) {
ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work).");
Expand Down
6 changes: 6 additions & 0 deletions drivers/vulkan/rendering_device_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,12 @@ class RenderingDeviceVulkan : public RenderingDevice {

virtual uint64_t get_memory_usage() const;

virtual void set_resource_name(RID p_id, const String p_name);

virtual void draw_command_begin_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
virtual void draw_command_insert_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
virtual void draw_command_end_label();

RenderingDeviceVulkan();
~RenderingDeviceVulkan();
};
Expand Down
58 changes: 54 additions & 4 deletions drivers/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Error VulkanContext::_initialize_extensions() {

enabled_extension_count = 0;
enabled_layer_count = 0;
enabled_debug_utils = false;
/* Look for instance extensions */
VkBool32 surfaceExtFound = 0;
VkBool32 platformSurfaceExtFound = 0;
Expand Down Expand Up @@ -251,9 +252,8 @@ Error VulkanContext::_initialize_extensions() {
}
}
if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
if (use_validation_layers) {
extension_names[enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
}
extension_names[enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
enabled_debug_utils = true;
}
if (enabled_extension_count >= MAX_EXTENSIONS) {
free(instance_extensions);
Expand Down Expand Up @@ -436,7 +436,7 @@ Error VulkanContext::_create_physical_device() {
" extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
"vkCreateInstance Failure");

if (use_validation_layers) {
if (enabled_debug_utils) {
// Setup VK_EXT_debug_utils function pointers always (we use them for
// debug labels and names).
CreateDebugUtilsMessengerEXT =
Expand Down Expand Up @@ -1567,6 +1567,56 @@ void VulkanContext::local_device_free(RID p_local_device) {
local_device_owner.free(p_local_device);
}

void VulkanContext::command_begin_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
if (!enabled_debug_utils) {
return;
}
VkDebugUtilsLabelEXT label;
label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
label.pNext = nullptr;
label.pLabelName = p_label_name.utf8().get_data();
label.color[0] = p_color[0];
label.color[1] = p_color[1];
label.color[2] = p_color[2];
label.color[3] = p_color[3];
CmdBeginDebugUtilsLabelEXT(p_command_buffer, &label);
}

void VulkanContext::command_insert_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color) {
if (!enabled_debug_utils) {
return;
}
VkDebugUtilsLabelEXT label;
label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
label.pNext = nullptr;
label.pLabelName = p_label_name.utf8().get_data();
label.color[0] = p_color[0];
label.color[1] = p_color[1];
label.color[2] = p_color[2];
label.color[3] = p_color[3];
CmdInsertDebugUtilsLabelEXT(p_command_buffer, &label);
}

void VulkanContext::command_end_label(VkCommandBuffer p_command_buffer) {
if (!enabled_debug_utils) {
return;
}
CmdEndDebugUtilsLabelEXT(p_command_buffer);
}

void VulkanContext::set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name) {
if (!enabled_debug_utils) {
return;
}
VkDebugUtilsObjectNameInfoEXT name_info;
name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
name_info.pNext = nullptr;
name_info.objectType = p_object_type;
name_info.objectHandle = p_object_handle;
name_info.pObjectName = p_object_name.utf8().get_data();
SetDebugUtilsObjectNameEXT(device, &name_info);
}

VulkanContext::VulkanContext() {
use_validation_layers = Engine::get_singleton()->is_validation_layers_enabled();

Expand Down
6 changes: 6 additions & 0 deletions drivers/vulkan/vulkan_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class VulkanContext {
bool VK_GOOGLE_display_timing_enabled = true;
uint32_t enabled_extension_count = 0;
const char *extension_names[MAX_EXTENSIONS];
bool enabled_debug_utils = false;

uint32_t enabled_layer_count = 0;
const char *enabled_layers[MAX_LAYERS];
Expand Down Expand Up @@ -209,6 +210,11 @@ class VulkanContext {
Error swap_buffers();
Error initialize();

void command_begin_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color);
void command_insert_label(VkCommandBuffer p_command_buffer, String p_label_name, const Color p_color);
void command_end_label(VkCommandBuffer p_command_buffer);
void set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name);

VulkanContext();
virtual ~VulkanContext();
};
Expand Down
20 changes: 17 additions & 3 deletions servers/rendering/renderer_rd/effects_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,11 @@ void EffectsRD::gather_ssao(RD::ComputeListID p_compute_list, const Vector<RID>

void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_depth_mipmaps_texture, const Vector<RID> &p_depth_mipmaps, RID p_ao, const Vector<RID> p_ao_slices, RID p_ao_pong, const Vector<RID> p_ao_pong_slices, RID p_upscale_buffer, RID p_importance_map, RID p_importance_map_pong, const CameraMatrix &p_projection, const SSAOSettings &p_settings, bool p_invalidate_uniform_sets) {
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();

RD::get_singleton()->draw_command_begin_label("SSAO");
/* FIRST PASS */
// Downsample and deinterleave the depth buffer.
{
RD::get_singleton()->draw_command_begin_label("Downsample Depth");
if (p_invalidate_uniform_sets) {
Vector<RD::Uniform> uniforms;
{
Expand Down Expand Up @@ -1076,11 +1077,13 @@ void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_dep

RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
RD::get_singleton()->compute_list_add_barrier(compute_list);
RD::get_singleton()->draw_command_end_label(); // Downsample SSAO
}

/* SECOND PASS */
// Sample SSAO
{
RD::get_singleton()->draw_command_begin_label("Gather Samples");
ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;

Expand Down Expand Up @@ -1181,6 +1184,7 @@ void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_dep
}

if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
ssao.importance_map_push_constant.intensity = p_settings.intensity;
Expand Down Expand Up @@ -1215,17 +1219,20 @@ void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_dep
RD::get_singleton()->compute_list_add_barrier(compute_list);

RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
RD::get_singleton()->draw_command_end_label(); // Importance Map
} else {
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
}

gather_ssao(compute_list, p_ao_slices, p_settings, false);
RD::get_singleton()->draw_command_end_label(); // Gather SSAO
}

// /* THIRD PASS */
// // Blur
//
{
RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
Expand Down Expand Up @@ -1275,12 +1282,14 @@ void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_dep
RD::get_singleton()->compute_list_add_barrier(compute_list);
}
}
RD::get_singleton()->draw_command_end_label(); // Blur
}

/* FOURTH PASS */
// Interleave buffers
// back to full size
{
RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
Expand Down Expand Up @@ -1309,8 +1318,9 @@ void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_dep

RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
RD::get_singleton()->compute_list_add_barrier(compute_list);
RD::get_singleton()->draw_command_end_label(); // Interleave
}

RD::get_singleton()->draw_command_end_label(); //SSAO
RD::get_singleton()->compute_list_end();

int zero[1] = { 0 };
Expand Down Expand Up @@ -1797,6 +1807,7 @@ EffectsRD::EffectsRD() {
ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
int zero[1] = { 0 };
RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, false);
RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");

Vector<RD::Uniform> uniforms;
{
Expand All @@ -1807,6 +1818,7 @@ EffectsRD::EffectsRD() {
uniforms.push_back(u);
}
ssao.counter_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 2), 2);
RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
}
{
Vector<String> ssao_modes;
Expand Down Expand Up @@ -1835,7 +1847,7 @@ EffectsRD::EffectsRD() {
ssao.interleave_shader_version = ssao.interleave_shader.version_create();
for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));

RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
pipeline++;
}
}
Expand Down Expand Up @@ -2040,12 +2052,14 @@ EffectsRD::EffectsRD() {
sampler.max_lod = 0;

default_sampler = RD::get_singleton()->sampler_create(sampler);
RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");

sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
sampler.max_lod = 1e20;

default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");

{ //create index array for copy shaders
Vector<uint8_t> pv;
Expand Down
Loading

0 comments on commit 7323cba

Please sign in to comment.