Vulkan-Hpp
11_InitShaders.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // VulkanHpp Samples : 11_InitShaders
16 // Initialize vertex and fragment shaders
17 
18 #include "../utils/shaders.hpp"
19 #include "../utils/utils.hpp"
20 #include "SPIRV/GlslangToSpv.h"
21 
22 #include <iostream>
23 
24 static char const * AppName = "11_InitShaders";
25 static char const * EngineName = "Vulkan.hpp";
26 
27 int main( int /*argc*/, char ** /*argv*/ )
28 {
29  try
30  {
31  vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
32 #if !defined( NDEBUG )
34 #endif
35 
36  vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
37 
38  uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
39  vk::Device device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
40 
41  /* VULKAN_HPP_KEY_START */
42 
43  glslang::InitializeProcess();
44 
45  std::vector<unsigned int> vertexShaderSPV;
46 #if !defined( NDEBUG )
47  bool ok =
48 #endif
50  assert( ok );
51 
52  vk::ShaderModuleCreateInfo vertexShaderModuleCreateInfo( vk::ShaderModuleCreateFlags(), vertexShaderSPV );
53  vk::ShaderModule vertexShaderModule = device.createShaderModule( vertexShaderModuleCreateInfo );
54 
55  std::vector<unsigned int> fragmentShaderSPV;
56 #if !defined( NDEBUG )
57  ok =
58 #endif
60  assert( ok );
61 
62  vk::ShaderModuleCreateInfo fragmentShaderModuleCreateInfo( vk::ShaderModuleCreateFlags(), fragmentShaderSPV );
63  vk::ShaderModule fragmentShaderModule = device.createShaderModule( fragmentShaderModuleCreateInfo );
64 
65  glslang::FinalizeProcess();
66 
67  device.destroyShaderModule( fragmentShaderModule );
68  device.destroyShaderModule( vertexShaderModule );
69 
70  /* VULKAN_HPP_KEY_END */
71 
72  device.destroy();
73 #if !defined( NDEBUG )
74  instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
75 #endif
76  instance.destroy();
77  }
78  catch ( vk::SystemError & err )
79  {
80  std::cout << "vk::SystemError: " << err.what() << std::endl;
81  exit( -1 );
82  }
83  catch ( std::exception & err )
84  {
85  std::cout << "std::exception: " << err.what() << std::endl;
86  exit( -1 );
87  }
88  catch ( ... )
89  {
90  std::cout << "unknown error\n";
91  exit( -1 );
92  }
93  return 0;
94 }
int main(int, char **)
void cout(vk::SurfaceCapabilitiesKHR const &surfaceCapabilities)
VULKAN_HPP_NODISCARD Result createShaderModule(const vk::ShaderModuleCreateInfo *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::ShaderModule *pShaderModule, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyShaderModule(vk::ShaderModule shaderModule, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result createDebugUtilsMessengerEXT(const vk::DebugUtilsMessengerCreateInfoEXT *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::DebugUtilsMessengerEXT *pMessenger, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result enumeratePhysicalDevices(uint32_t *pPhysicalDeviceCount, vk::PhysicalDevice *pPhysicalDevices, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyDebugUtilsMessengerEXT(vk::DebugUtilsMessengerEXT messenger, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void getQueueFamilyProperties(uint32_t *pQueueFamilyPropertyCount, vk::QueueFamilyProperties *pQueueFamilyProperties, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
virtual const char * what() const VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6206
uint32_t findGraphicsQueueFamilyIndex(std::vector< vk::QueueFamilyProperties > const &queueFamilyProperties)
Definition: utils.cpp:409
vk::DebugUtilsMessengerCreateInfoEXT makeDebugUtilsMessengerCreateInfoEXT()
Definition: utils.cpp:1025
bool GLSLtoSPV(const vk::ShaderStageFlagBits shaderType, std::string const &glslShader, std::vector< unsigned int > &spvShader)
Definition: shaders.cpp:49
vk::Instance createInstance(std::string const &appName, std::string const &engineName, std::vector< std::string > const &layers, std::vector< std::string > const &extensions, uint32_t apiVersion)
Definition: utils.cpp:279
vk::Device createDevice(vk::PhysicalDevice const &physicalDevice, uint32_t queueFamilyIndex, std::vector< std::string > const &extensions, vk::PhysicalDeviceFeatures const *physicalDeviceFeatures, void const *pNext)
Definition: utils.cpp:86
std::vector< std::string > getInstanceExtensions()
Definition: utils.cpp:477
const std::string vertexShaderText_PC_C
Definition: shaders.hpp:31
const std::string fragmentShaderText_C_C
Definition: shaders.hpp:79