Merge pull request #558 from asuessenbach/hash

Provide std::hash<vk::HandleType> to make handles hashable.
This commit is contained in:
Andreas Süßenbach 2020-04-07 09:12:04 +02:00 committed by GitHub
commit eaa8ba4f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 437 additions and 2 deletions

View File

@ -2680,6 +2680,36 @@ void VulkanHppGenerator::appendHandlesCommandDefintions(std::string & str) const
str += "\n"; str += "\n";
} }
void VulkanHppGenerator::appendHashStructures(std::string& str) const
{
str += "\n"
"namespace std\n"
"{\n";
const std::string hashTemplate = R"( template <> struct hash<vk::${type}>
{
std::size_t operator()(vk::${type} const& ${name}) const VULKAN_HPP_NOEXCEPT
{
return std::hash<Vk${type}>{}(static_cast<Vk${type}>(${name}));
}
};
)";
for (auto handle : m_handles)
{
if (!handle.first.empty())
{
str += "\n";
appendPlatformEnter(str, !handle.second.alias.empty(), handle.second.platform);
std::string type = stripPrefix(handle.first, "Vk");
std::string name = startLowerCase(type);
str += replaceWithMap(hashTemplate, { {"name", name}, {"type", type} });
appendPlatformLeave(str, !handle.second.alias.empty(), handle.second.platform);
}
}
str += "}\n";
}
// Intended only for `enum class Result`! // Intended only for `enum class Result`!
void VulkanHppGenerator::appendResultExceptions(std::string & str) const void VulkanHppGenerator::appendResultExceptions(std::string & str) const
{ {
@ -6444,6 +6474,7 @@ int main(int argc, char **argv)
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <functional>
#include <initializer_list> #include <initializer_list>
#include <string> #include <string>
#include <system_error> #include <system_error>
@ -6757,8 +6788,9 @@ namespace std
generator.appendHandlesCommandDefintions(str); generator.appendHandlesCommandDefintions(str);
generator.appendStructureChainValidation(str); generator.appendStructureChainValidation(str);
generator.appendDispatchLoaderDynamic(str); generator.appendDispatchLoaderDynamic(str);
str += "} // namespace VULKAN_HPP_NAMESPACE\n" str += "} // namespace VULKAN_HPP_NAMESPACE\n";
"#endif\n"; generator.appendHashStructures(str);
str += "#endif\n";
assert(str.length() < estimatedLength); assert(str.length() < estimatedLength);
cleanup(str); cleanup(str);

View File

@ -34,6 +34,7 @@ class VulkanHppGenerator
void appendForwardDeclarations(std::string & str) const; void appendForwardDeclarations(std::string & str) const;
void appendHandles(std::string & str) const; void appendHandles(std::string & str) const;
void appendHandlesCommandDefintions(std::string & str) const; void appendHandlesCommandDefintions(std::string & str) const;
void appendHashStructures(std::string& str) const;
void appendResultExceptions(std::string & str) const; void appendResultExceptions(std::string & str) const;
void appendStructs(std::string & str) const; void appendStructs(std::string & str) const;
void appendStructureChainValidation(std::string & str); void appendStructureChainValidation(std::string & str);

37
tests/Hash/CMakeLists.txt Normal file
View File

@ -0,0 +1,37 @@
# Copyright(c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.2)
if (NOT TESTS_BUILD_ONLY_DYNAMIC)
project(Hash)
set(HEADERS
)
set(SOURCES
Hash.cpp
)
source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})
add_executable(Hash
${HEADERS}
${SOURCES}
)
set_target_properties(Hash PROPERTIES FOLDER "Tests")
target_link_libraries(Hash "${Vulkan_LIBRARIES}")
endif()

55
tests/Hash/Hash.cpp Normal file
View File

@ -0,0 +1,55 @@
// Copyright(c) 2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// VulkanHpp Samples : Hash
// Compile test on using std::hash on handles
#include "vulkan/vulkan.hpp"
#include <iostream>
#include <unordered_map>
#include <unordered_set>
static char const* AppName = "Hash";
static char const* EngineName = "Vulkan.hpp";
int main(int /*argc*/, char ** /*argv*/)
{
try
{
vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1);
vk::UniqueInstance instance = vk::createInstanceUnique(vk::InstanceCreateInfo({}, &appInfo));
auto h1 = std::hash<vk::Instance>{}(*instance);
auto h2 = std::hash<VkInstance>{}(*instance);
assert(h1 == h2);
std::unordered_set<vk::Instance> uset;
uset.insert(*instance);
std::unordered_map<vk::Instance, size_t> umap;
umap[*instance] = 1;
}
catch (vk::SystemError const& err)
{
std::cout << "vk::SystemError: " << err.what() << std::endl;
exit(-1);
}
catch (...)
{
std::cout << "unknown error\n";
exit(-1);
}
return 0;
}

View File

@ -39,6 +39,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <functional>
#include <initializer_list> #include <initializer_list>
#include <string> #include <string>
#include <system_error> #include <system_error>
@ -77967,4 +77968,313 @@ namespace VULKAN_HPP_NAMESPACE
}; };
} // namespace VULKAN_HPP_NAMESPACE } // namespace VULKAN_HPP_NAMESPACE
namespace std
{
template <> struct hash<vk::AccelerationStructureKHR>
{
std::size_t operator()(vk::AccelerationStructureKHR const& accelerationStructureKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkAccelerationStructureKHR>{}(static_cast<VkAccelerationStructureKHR>(accelerationStructureKHR));
}
};
template <> struct hash<vk::Buffer>
{
std::size_t operator()(vk::Buffer const& buffer) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkBuffer>{}(static_cast<VkBuffer>(buffer));
}
};
template <> struct hash<vk::BufferView>
{
std::size_t operator()(vk::BufferView const& bufferView) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkBufferView>{}(static_cast<VkBufferView>(bufferView));
}
};
template <> struct hash<vk::CommandBuffer>
{
std::size_t operator()(vk::CommandBuffer const& commandBuffer) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkCommandBuffer>{}(static_cast<VkCommandBuffer>(commandBuffer));
}
};
template <> struct hash<vk::CommandPool>
{
std::size_t operator()(vk::CommandPool const& commandPool) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkCommandPool>{}(static_cast<VkCommandPool>(commandPool));
}
};
template <> struct hash<vk::DebugReportCallbackEXT>
{
std::size_t operator()(vk::DebugReportCallbackEXT const& debugReportCallbackEXT) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDebugReportCallbackEXT>{}(static_cast<VkDebugReportCallbackEXT>(debugReportCallbackEXT));
}
};
template <> struct hash<vk::DebugUtilsMessengerEXT>
{
std::size_t operator()(vk::DebugUtilsMessengerEXT const& debugUtilsMessengerEXT) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDebugUtilsMessengerEXT>{}(static_cast<VkDebugUtilsMessengerEXT>(debugUtilsMessengerEXT));
}
};
#ifdef VK_ENABLE_BETA_EXTENSIONS
template <> struct hash<vk::DeferredOperationKHR>
{
std::size_t operator()(vk::DeferredOperationKHR const& deferredOperationKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDeferredOperationKHR>{}(static_cast<VkDeferredOperationKHR>(deferredOperationKHR));
}
};
#endif /*VK_ENABLE_BETA_EXTENSIONS*/
template <> struct hash<vk::DescriptorPool>
{
std::size_t operator()(vk::DescriptorPool const& descriptorPool) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDescriptorPool>{}(static_cast<VkDescriptorPool>(descriptorPool));
}
};
template <> struct hash<vk::DescriptorSet>
{
std::size_t operator()(vk::DescriptorSet const& descriptorSet) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDescriptorSet>{}(static_cast<VkDescriptorSet>(descriptorSet));
}
};
template <> struct hash<vk::DescriptorSetLayout>
{
std::size_t operator()(vk::DescriptorSetLayout const& descriptorSetLayout) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDescriptorSetLayout>{}(static_cast<VkDescriptorSetLayout>(descriptorSetLayout));
}
};
template <> struct hash<vk::DescriptorUpdateTemplate>
{
std::size_t operator()(vk::DescriptorUpdateTemplate const& descriptorUpdateTemplate) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDescriptorUpdateTemplate>{}(static_cast<VkDescriptorUpdateTemplate>(descriptorUpdateTemplate));
}
};
template <> struct hash<vk::Device>
{
std::size_t operator()(vk::Device const& device) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDevice>{}(static_cast<VkDevice>(device));
}
};
template <> struct hash<vk::DeviceMemory>
{
std::size_t operator()(vk::DeviceMemory const& deviceMemory) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDeviceMemory>{}(static_cast<VkDeviceMemory>(deviceMemory));
}
};
template <> struct hash<vk::DisplayKHR>
{
std::size_t operator()(vk::DisplayKHR const& displayKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDisplayKHR>{}(static_cast<VkDisplayKHR>(displayKHR));
}
};
template <> struct hash<vk::DisplayModeKHR>
{
std::size_t operator()(vk::DisplayModeKHR const& displayModeKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkDisplayModeKHR>{}(static_cast<VkDisplayModeKHR>(displayModeKHR));
}
};
template <> struct hash<vk::Event>
{
std::size_t operator()(vk::Event const& event) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkEvent>{}(static_cast<VkEvent>(event));
}
};
template <> struct hash<vk::Fence>
{
std::size_t operator()(vk::Fence const& fence) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkFence>{}(static_cast<VkFence>(fence));
}
};
template <> struct hash<vk::Framebuffer>
{
std::size_t operator()(vk::Framebuffer const& framebuffer) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkFramebuffer>{}(static_cast<VkFramebuffer>(framebuffer));
}
};
template <> struct hash<vk::Image>
{
std::size_t operator()(vk::Image const& image) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkImage>{}(static_cast<VkImage>(image));
}
};
template <> struct hash<vk::ImageView>
{
std::size_t operator()(vk::ImageView const& imageView) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkImageView>{}(static_cast<VkImageView>(imageView));
}
};
template <> struct hash<vk::IndirectCommandsLayoutNV>
{
std::size_t operator()(vk::IndirectCommandsLayoutNV const& indirectCommandsLayoutNV) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkIndirectCommandsLayoutNV>{}(static_cast<VkIndirectCommandsLayoutNV>(indirectCommandsLayoutNV));
}
};
template <> struct hash<vk::Instance>
{
std::size_t operator()(vk::Instance const& instance) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkInstance>{}(static_cast<VkInstance>(instance));
}
};
template <> struct hash<vk::PerformanceConfigurationINTEL>
{
std::size_t operator()(vk::PerformanceConfigurationINTEL const& performanceConfigurationINTEL) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkPerformanceConfigurationINTEL>{}(static_cast<VkPerformanceConfigurationINTEL>(performanceConfigurationINTEL));
}
};
template <> struct hash<vk::PhysicalDevice>
{
std::size_t operator()(vk::PhysicalDevice const& physicalDevice) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkPhysicalDevice>{}(static_cast<VkPhysicalDevice>(physicalDevice));
}
};
template <> struct hash<vk::Pipeline>
{
std::size_t operator()(vk::Pipeline const& pipeline) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkPipeline>{}(static_cast<VkPipeline>(pipeline));
}
};
template <> struct hash<vk::PipelineCache>
{
std::size_t operator()(vk::PipelineCache const& pipelineCache) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkPipelineCache>{}(static_cast<VkPipelineCache>(pipelineCache));
}
};
template <> struct hash<vk::PipelineLayout>
{
std::size_t operator()(vk::PipelineLayout const& pipelineLayout) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkPipelineLayout>{}(static_cast<VkPipelineLayout>(pipelineLayout));
}
};
template <> struct hash<vk::QueryPool>
{
std::size_t operator()(vk::QueryPool const& queryPool) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkQueryPool>{}(static_cast<VkQueryPool>(queryPool));
}
};
template <> struct hash<vk::Queue>
{
std::size_t operator()(vk::Queue const& queue) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkQueue>{}(static_cast<VkQueue>(queue));
}
};
template <> struct hash<vk::RenderPass>
{
std::size_t operator()(vk::RenderPass const& renderPass) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkRenderPass>{}(static_cast<VkRenderPass>(renderPass));
}
};
template <> struct hash<vk::Sampler>
{
std::size_t operator()(vk::Sampler const& sampler) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkSampler>{}(static_cast<VkSampler>(sampler));
}
};
template <> struct hash<vk::SamplerYcbcrConversion>
{
std::size_t operator()(vk::SamplerYcbcrConversion const& samplerYcbcrConversion) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkSamplerYcbcrConversion>{}(static_cast<VkSamplerYcbcrConversion>(samplerYcbcrConversion));
}
};
template <> struct hash<vk::Semaphore>
{
std::size_t operator()(vk::Semaphore const& semaphore) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkSemaphore>{}(static_cast<VkSemaphore>(semaphore));
}
};
template <> struct hash<vk::ShaderModule>
{
std::size_t operator()(vk::ShaderModule const& shaderModule) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkShaderModule>{}(static_cast<VkShaderModule>(shaderModule));
}
};
template <> struct hash<vk::SurfaceKHR>
{
std::size_t operator()(vk::SurfaceKHR const& surfaceKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkSurfaceKHR>{}(static_cast<VkSurfaceKHR>(surfaceKHR));
}
};
template <> struct hash<vk::SwapchainKHR>
{
std::size_t operator()(vk::SwapchainKHR const& swapchainKHR) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkSwapchainKHR>{}(static_cast<VkSwapchainKHR>(swapchainKHR));
}
};
template <> struct hash<vk::ValidationCacheEXT>
{
std::size_t operator()(vk::ValidationCacheEXT const& validationCacheEXT) const VULKAN_HPP_NOEXCEPT
{
return std::hash<VkValidationCacheEXT>{}(static_cast<VkValidationCacheEXT>(validationCacheEXT));
}
};
}
#endif #endif