2019-03-05 07:59:40 +00:00
|
|
|
// Copyright(c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-05-07 12:28:11 +00:00
|
|
|
//
|
|
|
|
// 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 : 09_InitDescriptorSet
|
|
|
|
// Initialize a descriptor set
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
#if defined( _MSC_VER )
|
|
|
|
# pragma warning( disable : 4201 ) // disable warning C4201: nonstandard extension used: nameless struct/union; needed
|
|
|
|
// to get glm/detail/type_vec?.hpp without warnings
|
|
|
|
#elif defined( __GNUC__ )
|
|
|
|
// don't know how to switch off that warning here
|
|
|
|
#else
|
|
|
|
// unknow compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 11:24:36 +00:00
|
|
|
#include "../utils/math.hpp"
|
2019-03-19 14:35:08 +00:00
|
|
|
#include "../utils/utils.hpp"
|
2018-05-07 12:28:11 +00:00
|
|
|
#include "vulkan/vulkan.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2019-03-05 07:59:40 +00:00
|
|
|
#include <iostream>
|
2018-05-07 12:28:11 +00:00
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "09_InitDescriptorSet";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2018-05-07 12:28:11 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2018-05-07 12:28:11 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Instance instance = vk::su::createInstance( AppName, EngineName );
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DebugUtilsMessengerEXT debugUtilsMessenger =
|
|
|
|
instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
2018-05-07 12:28:11 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
2018-05-07 12:28:11 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex =
|
|
|
|
vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
|
|
|
vk::Device device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
|
2018-05-07 12:28:11 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::BufferData uniformBufferData(
|
|
|
|
physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
|
2021-02-17 09:49:59 +00:00
|
|
|
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( vk::Extent2D( 0, 0 ) );
|
|
|
|
vk::su::copyToDevice( device, uniformBufferData.deviceMemory, mvpcMatrix );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(
|
2020-04-12 19:49:12 +00:00
|
|
|
device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
|
2018-05-07 12:28:11 +00:00
|
|
|
|
|
|
|
/* VULKAN_HPP_KEY_START */
|
|
|
|
|
|
|
|
// create a descriptor pool
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorPoolSize poolSize( vk::DescriptorType::eUniformBuffer, 1 );
|
|
|
|
vk::DescriptorPool descriptorPool = device.createDescriptorPool(
|
2020-07-08 08:58:37 +00:00
|
|
|
vk::DescriptorPoolCreateInfo( vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize ) );
|
2018-05-07 12:28:11 +00:00
|
|
|
|
|
|
|
// allocate a descriptor set
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( descriptorPool, descriptorSetLayout );
|
|
|
|
vk::DescriptorSet descriptorSet = device.allocateDescriptorSets( descriptorSetAllocateInfo ).front();
|
2018-05-07 12:28:11 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorBufferInfo descriptorBufferInfo( uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
|
|
|
|
vk::WriteDescriptorSet writeDescriptorSet(
|
|
|
|
descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, descriptorBufferInfo );
|
|
|
|
device.updateDescriptorSets( writeDescriptorSet, nullptr );
|
|
|
|
|
|
|
|
device.freeDescriptorSets( descriptorPool, descriptorSet );
|
|
|
|
device.destroyDescriptorPool( descriptorPool );
|
2018-05-07 12:28:11 +00:00
|
|
|
|
|
|
|
/* VULKAN_HPP_KEY_END */
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
device.destroyDescriptorSetLayout( descriptorSetLayout );
|
|
|
|
uniformBufferData.clear( device );
|
|
|
|
device.destroy();
|
|
|
|
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
|
|
|
instance.destroy();
|
2018-05-07 12:28:11 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2018-05-07 12:28:11 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-05-07 12:28:11 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2018-05-07 12:28:11 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-05-07 12:28:11 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2018-05-07 12:28:11 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-05-07 12:28:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|