Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vk ext shader object optional layer #780

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
89d6fe8
Add VK_EXT_shader_object sample
Apr 17, 2023
c76194e
add an ability to have optional vulkan layers. If it's an optional l…
gpx1000 Aug 17, 2023
2b0356d
Merge remote-tracking branch 'upstream/main'
gpx1000 Aug 17, 2023
53482ab
Merge branch 'main' into VK_EXT_shader_object_optional_layer
gpx1000 Aug 17, 2023
a86f7d2
fix the formatting and copyright breaks.
gpx1000 Aug 17, 2023
fb01dcf
get full_screen_exclusive to build.
gpx1000 Aug 17, 2023
33bdc15
clang format locally wants it formatted differently than CI.
gpx1000 Aug 17, 2023
ad8e78c
if there's an error in clang-format then save off the diff output so …
gpx1000 Aug 17, 2023
9290b5f
attempt to fix clang-format
gpx1000 Aug 17, 2023
d1cb82d
Merge remote-tracking branch 'refs/remotes/upstream/main' into VK_EXT…
gpx1000 May 16, 2024
892f289
Refactor validation layers to use vector instead of map
gpx1000 May 16, 2024
48d8b14
revert the change in check.yml
gpx1000 May 16, 2024
e92bdb0
fix the copyright and formating and doxygen problems.
gpx1000 May 16, 2024
3b73db8
revert vulkan and volk submodule changes.
gpx1000 May 17, 2024
d2b132c
clang format
gpx1000 May 17, 2024
27e35ba
update the clang-format
gpx1000 May 17, 2024
709d7de
revert full_screen_exclusive.cpp and hello_triangle.cpp
gpx1000 May 17, 2024
898da21
Merge branch 'main' into VK_EXT_shader_object_optional_layer
gpx1000 Dec 18, 2024
e7e78b1
Refactor layer validation handling for consistency.
gpx1000 Dec 21, 2024
1579864
Fix inconsistent indentation and formatting across files
gpx1000 Dec 21, 2024
90802bf
Add get_validation_layers() method and formatting adjustment
gpx1000 Dec 21, 2024
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
49 changes: 49 additions & 0 deletions framework/core/hpp_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugReportFlagsEXT flags
}
#endif

bool validate_layers(std::unordered_map<const char *, bool> &required,
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
const std::vector<vk::LayerProperties> &available)
{
std::vector<const char *> remove_vec;
for (auto layer : required)
{
bool found = false;
for (auto &available_layer : available)
{
if (strcmp(available_layer.layerName, layer.first) == 0)
{
found = true;
break;
}
}

if (!found)
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!layer.second)
{
LOGW("Optional Layer {} not found, removing it", layer.first)
remove_vec.push_back(layer.first);
continue;
}
LOGE("Validation Layer {} not found", layer.first)
return false;
}
}
for (auto &rem : remove_vec)
{
required.erase(rem);
}
return true;
}

bool validate_layers(const std::vector<const char *> &required,
const std::vector<vk::LayerProperties> &available)
{
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -291,6 +326,20 @@ HPPInstance::HPPInstance(const std::string &applicati
throw std::runtime_error("Required validation layers are missing.");
}

std::unordered_map<const char *, bool> layers = (std::unordered_map<const char *, bool>) (requested_layers);
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
if (validate_layers(layers, supported_validation_layers))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : layers)
{
LOGI(" \t{}", layer.first);
}
}
else
{
throw std::runtime_error("Required validation layers are missing.");
}

vk::ApplicationInfo app_info(application_name.c_str(), 0, "Vulkan Samples", 0, api_version);

vk::InstanceCreateInfo instance_info({}, &app_info, requested_validation_layers, enabled_extensions);
Expand Down
49 changes: 49 additions & 0 deletions framework/core/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugReportFlagsEXT flags
}
#endif

bool validate_layers(std::unordered_map<const char *, bool> &required,
const std::vector<VkLayerProperties> &available)
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<const char *> remove_vec;
for (auto layer : required)
{
bool found = false;
for (auto &available_layer : available)
{
if (strcmp(available_layer.layerName, layer.first) == 0)
{
found = true;
break;
}
}

if (!found)
{
if (!layer.second)
{
LOGW("Optional Layer {} not found, removing it", layer.first)
remove_vec.push_back(layer.first);
continue;
}
LOGE("Validation Layer {} not found", layer.first)
return false;
}
}
for (auto &rem : remove_vec)
{
required.erase(rem);
}
return true;
}

bool validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available)
{
Expand Down Expand Up @@ -302,6 +337,20 @@ Instance::Instance(const std::string &application_nam
throw std::runtime_error("Required validation layers are missing.");
}

std::unordered_map<const char *, bool> layers = (std::unordered_map<const char *, bool>) (requested_layers);
if (validate_layers(layers, supported_validation_layers))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : layers)
{
LOGI(" \t{}", layer.first);
}
}
else
{
throw std::runtime_error("Required validation layers are missing.");
}

VkApplicationInfo app_info{VK_STRUCTURE_TYPE_APPLICATION_INFO};

app_info.pApplicationName = application_name.c_str();
Expand Down
1 change: 1 addition & 0 deletions framework/vulkan_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class VulkanSample : public vkb::Application

/** @brief Set of instance extensions to be enabled for this example and whether they are optional (must be set in the derived constructor) */
std::unordered_map<const char *, bool> instance_extensions;
std::unordered_map<const char *, bool> instance_layers;

/** @brief Vector of layer settings to be enabled for this example (must be set in the derived constructor) */
std::vector<vk::LayerSettingEXT> layer_settings;
Expand Down
10 changes: 6 additions & 4 deletions samples/extensions/shader_object/README.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
////
- Copyright 2023 Nintendo
- Copyright 2024 Nintendo
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -436,13 +436,15 @@ The layer can be shipped with your application, and it will disable itself if a
The emulation layer can be enabled by adding `VK_LAYER_KHRONOS_shader_object` to `ppEnabledLayerNames` in `VkDeviceCreateInfo`.

The sample framework already has an existing abstraction normally used for enabling the validation layer.
This sample repurposes this mechanism to instead load the emulation layer.
This
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
sample repurposes this mechanism to instead load the emulation layer. NB: the layer is set to be optional as that's
what the false does.

[,CPP]
----
const std::vector<const char *> ShaderObject::get_validation_layers()
const std::unordered_map<const char *, bool> ShaderObject::get_validation_layers()
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
{
return {"VK_LAYER_KHRONOS_shader_object"};
return {"VK_LAYER_KHRONOS_shader_object", false};
}
----

Expand Down
21 changes: 13 additions & 8 deletions samples/extensions/shader_object/shader_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ ShaderObject::ShaderObject()

// Enable extensions for sample
add_device_extension(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME);

// Enable the shader object layer if it's available. Its optional.
add_layer("VK_LAYER_KHRONOS_shader_object", false);
}

ShaderObject::~ShaderObject()
Expand Down Expand Up @@ -105,13 +108,6 @@ ShaderObject::~ShaderObject()
}
}

// Currently the sample calls through this function in order to get the list of any instance layers, not just validation layers.
// This is not suitable for a real application implementation using the layer, the layer will need to be shipped with the application.
const std::vector<const char *> ShaderObject::get_validation_layers()
{
return {"VK_LAYER_KHRONOS_shader_object"};
}

bool ShaderObject::resize(const uint32_t _width, const uint32_t _height)
{
if (!has_device())
Expand Down Expand Up @@ -229,6 +225,11 @@ void ShaderObject::setup_framebuffer()
// Create render pass for UI drawing
void ShaderObject::setup_render_pass()
{
// delete existing render pass
if (render_pass != VK_NULL_HANDLE)
gpx1000 marked this conversation as resolved.
Show resolved Hide resolved
{
vkDestroyRenderPass(get_device().get_handle(), render_pass, VK_NULL_HANDLE);
}
VkAttachmentDescription color_attachment{};

// Color attachment set to load color and ignore stencil
Expand Down Expand Up @@ -415,6 +416,10 @@ void ShaderObject::load_assets()

VkSamplerCreateInfo sampler_create_info = vkb::initializers::sampler_create_info();

// destroy created sampler before re-creating
vkDestroySampler(get_device().get_handle(), heightmap_texture.sampler, nullptr);
vkDestroySampler(get_device().get_handle(), terrain_array_textures.sampler, nullptr);

// Setup a mirroring sampler for the height map
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the calling load_assets multiple times bug has been fixed.

vkDestroySampler(get_device().get_handle(), heightmap_texture.sampler, nullptr);
sampler_create_info.magFilter = filter;
Expand Down Expand Up @@ -1955,7 +1960,7 @@ void ShaderObject::build_linked_shaders(VkDevice device, ShaderObject::Shader *v
shader_create.flags |= VK_SHADER_CREATE_LINK_STAGE_BIT_EXT;
}

VkShaderEXT shaderEXTs[2];
VkShaderEXT shaderEXTs[2] = {VK_NULL_HANDLE, VK_NULL_HANDLE};

// Create the shader objects
VkResult result = vkCreateShadersEXT(device,
Expand Down
2 changes: 0 additions & 2 deletions samples/extensions/shader_object/shader_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ class ShaderObject : public ApiVulkanSample
ShaderObject();
~ShaderObject() override;

const std::vector<const char *> get_validation_layers() override;

bool resize(const uint32_t width, const uint32_t height) override;
bool prepare(const vkb::ApplicationOptions &options) override;
void setup_framebuffer() override;
Expand Down
Loading