Skip to content

Commit

Permalink
Initialize shader placeholders up front
Browse files Browse the repository at this point in the history
Then use the placeholder to create the shader instead of swapping RIDs
This fixes a false positive that reported leaked shaders
  • Loading branch information
clayjohn committed Aug 3, 2023
1 parent 237bd0a commit 558f4b7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
62 changes: 33 additions & 29 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5043,17 +5043,24 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_

_THREAD_SAFE_METHOD_

Shader shader;
RID id;
if (p_placeholder.is_null()) {
id = shader_owner.make_rid();
} else {
id = p_placeholder;
}

Shader *shader = shader_owner.get_or_null(id);

shader.vertex_input_mask = vertex_input_mask;
shader.fragment_output_mask = fragment_output_mask;
shader.push_constant = push_constant;
shader.is_compute = is_compute;
shader.compute_local_size[0] = compute_local_size[0];
shader.compute_local_size[1] = compute_local_size[1];
shader.compute_local_size[2] = compute_local_size[2];
shader.specialization_constants = specialization_constants;
shader.name = name;
shader->vertex_input_mask = vertex_input_mask;
shader->fragment_output_mask = fragment_output_mask;
shader->push_constant = push_constant;
shader->is_compute = is_compute;
shader->compute_local_size[0] = compute_local_size[0];
shader->compute_local_size[1] = compute_local_size[1];
shader->compute_local_size[2] = compute_local_size[2];
shader->specialization_constants = specialization_constants;
shader->name = name;

String error_text;

Expand Down Expand Up @@ -5085,7 +5092,7 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_
shader_stage.pName = "main";
shader_stage.pSpecializationInfo = nullptr;

shader.pipeline_stages.push_back(shader_stage);
shader->pipeline_stages.push_back(shader_stage);
}
// Proceed to create descriptor sets.

Expand Down Expand Up @@ -5128,8 +5135,8 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_
}
}

shader.sets.push_back(set);
shader.set_formats.push_back(format);
shader->sets.push_back(set);
shader->set_formats.push_back(format);
}
}

Expand All @@ -5139,13 +5146,13 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_
pipeline_layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_create_info.pNext = nullptr;
pipeline_layout_create_info.flags = 0;
pipeline_layout_create_info.setLayoutCount = shader.sets.size();
pipeline_layout_create_info.setLayoutCount = shader->sets.size();

Vector<VkDescriptorSetLayout> layouts;
layouts.resize(shader.sets.size());
layouts.resize(shader->sets.size());

for (int i = 0; i < layouts.size(); i++) {
layouts.write[i] = shader.sets[i].descriptor_set_layout;
layouts.write[i] = shader->sets[i].descriptor_set_layout;
}

pipeline_layout_create_info.pSetLayouts = layouts.ptr();
Expand All @@ -5164,7 +5171,7 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_
pipeline_layout_create_info.pPushConstantRanges = nullptr;
}

VkResult err = vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &shader.pipeline_layout);
VkResult err = vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &shader->pipeline_layout);

if (err) {
error_text = "Error (" + itos(err) + ") creating pipeline layout.";
Expand All @@ -5174,31 +5181,28 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector<uint8_t> &p_

if (!success) {
// Clean up if failed.
for (int i = 0; i < shader.pipeline_stages.size(); i++) {
vkDestroyShaderModule(device, shader.pipeline_stages[i].module, nullptr);
for (int i = 0; i < shader->pipeline_stages.size(); i++) {
vkDestroyShaderModule(device, shader->pipeline_stages[i].module, nullptr);
}

for (int i = 0; i < shader.sets.size(); i++) {
vkDestroyDescriptorSetLayout(device, shader.sets[i].descriptor_set_layout, nullptr);
for (int i = 0; i < shader->sets.size(); i++) {
vkDestroyDescriptorSetLayout(device, shader->sets[i].descriptor_set_layout, nullptr);
}

shader_owner.free(id);

ERR_FAIL_V_MSG(RID(), error_text);
}
RID id;
if (p_placeholder.is_null()) {
id = shader_owner.make_rid(shader);
} else {
shader_owner.initialize_rid(p_placeholder, shader);
id = p_placeholder;
}

#ifdef DEV_ENABLED
set_resource_name(id, "RID:" + itos(id.get_id()));
#endif
return id;
}

RID RenderingDeviceVulkan::shader_create_placeholder() {
return shader_owner.allocate_rid();
Shader shader;
return shader_owner.make_rid(shader);
}

uint32_t RenderingDeviceVulkan::shader_get_vertex_input_attribute_mask(RID p_shader) {
Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/renderer_rd/shader_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void ShaderRD::_clear_version(Version *p_version) {
// Clear versions if they exist.
if (p_version->variants) {
for (int i = 0; i < variant_defines.size(); i++) {
if (variants_enabled[i] && group_enabled[variant_defines[i].group]) {
if (p_version->variants[i].is_valid()) {
RD::get_singleton()->free(p_version->variants[i]);
}
}
Expand Down

0 comments on commit 558f4b7

Please sign in to comment.