mirror of
https://github.com/KhronosGroup/Vulkan-Hpp
synced 2024-11-28 22:51:17 +00:00
First samples on using vulkan.hpp: 01_InitInstance, 02_EnumerateDevices, 03_InitDevice, 04_InitCommandBuffer, 05_InitSwapchain, 06_InitDepthBuffer. (#197)
This commit is contained in:
parent
f4767bace6
commit
bbaa5956c6
@ -26,6 +26,8 @@
|
|||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
cmake_minimum_required(VERSION 3.2)
|
||||||
|
|
||||||
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
|
||||||
project(VulkanHppGenerator)
|
project(VulkanHppGenerator)
|
||||||
|
|
||||||
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Vulkan-Docs/src/spec/vk.xml vk_spec)
|
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Vulkan-Docs/src/spec/vk.xml vk_spec)
|
||||||
@ -34,7 +36,7 @@ add_definitions(-DVK_SPEC="${vk_spec}")
|
|||||||
|
|
||||||
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/vulkan/vulkan.hpp vulkan_hpp)
|
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/vulkan/vulkan.hpp vulkan_hpp)
|
||||||
string(REPLACE "\\" "\\\\" vulkan_hpp ${vulkan_hpp})
|
string(REPLACE "\\" "\\\\" vulkan_hpp ${vulkan_hpp})
|
||||||
add_definitions(-DVULKAN_HPP="${vulkan_hpp}")
|
add_definitions(-DVULKAN_HPP_FILE="${vulkan_hpp}")
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
VulkanHppGenerator.hpp
|
VulkanHppGenerator.hpp
|
||||||
@ -68,3 +70,9 @@ add_executable(VulkanHppGenerator
|
|||||||
set_property(TARGET VulkanHppGenerator PROPERTY CXX_STANDARD 11)
|
set_property(TARGET VulkanHppGenerator PROPERTY CXX_STANDARD 11)
|
||||||
|
|
||||||
target_include_directories(VulkanHppGenerator PRIVATE "${CMAKE_SOURCE_DIR}/tinyxml2")
|
target_include_directories(VulkanHppGenerator PRIVATE "${CMAKE_SOURCE_DIR}/tinyxml2")
|
||||||
|
|
||||||
|
option (SAMPLES_BUILD OFF)
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD)
|
||||||
|
add_subdirectory(samples)
|
||||||
|
endif (SAMPLES_BUILD)
|
@ -4910,7 +4910,7 @@ int main( int argc, char **argv )
|
|||||||
|
|
||||||
std::string filename = (argc == 1) ? VK_SPEC : argv[1];
|
std::string filename = (argc == 1) ? VK_SPEC : argv[1];
|
||||||
std::cout << "Loading vk.xml from " << filename << std::endl;
|
std::cout << "Loading vk.xml from " << filename << std::endl;
|
||||||
std::cout << "Writing vulkan.hpp to " << VULKAN_HPP << std::endl;
|
std::cout << "Writing vulkan.hpp to " << VULKAN_HPP_FILE << std::endl;
|
||||||
|
|
||||||
tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
|
tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
|
||||||
if (error != tinyxml2::XML_SUCCESS)
|
if (error != tinyxml2::XML_SUCCESS)
|
||||||
@ -4986,7 +4986,7 @@ int main( int argc, char **argv )
|
|||||||
|
|
||||||
std::map<std::string, std::string> defaultValues = generator.createDefaults();
|
std::map<std::string, std::string> defaultValues = generator.createDefaults();
|
||||||
|
|
||||||
std::ofstream ofs(VULKAN_HPP);
|
std::ofstream ofs(VULKAN_HPP_FILE);
|
||||||
ofs << generator.getVulkanLicenseHeader() << std::endl
|
ofs << generator.getVulkanLicenseHeader() << std::endl
|
||||||
<< R"(
|
<< R"(
|
||||||
#ifndef VULKAN_HPP
|
#ifndef VULKAN_HPP
|
||||||
|
53
samples/01_InitInstance/01_InitInstance.cpp
Normal file
53
samples/01_InitInstance/01_InitInstance.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// 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 : 01_InitInstance
|
||||||
|
// Create and destroy a vk::UniqueInstance
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "01_InitInstance";
|
||||||
|
static char const* EngineName = "Vulkan.hpp";
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// initialize the vk::ApplicationInfo structure
|
||||||
|
vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1);
|
||||||
|
|
||||||
|
// create a UniqueInstance
|
||||||
|
vk::UniqueInstance instance = vk::createInstanceUnique(vk::InstanceCreateInfo(vk::InstanceCreateFlags(), &appInfo));
|
||||||
|
|
||||||
|
// Note: No need to explicitly destroy the instance, as the corresponding destroy function is
|
||||||
|
// called by the destructor of the UniqueInstance on leaving this scope.
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/01_InitInstance/CMakeLists.txt
Normal file
41
samples/01_InitInstance/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(01_InitInstance)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
01_InitInstance.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(01_InitInstance
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(01_InitInstance PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(01_InitInstance PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(01_InitInstance PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(01_InitInstance "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
52
samples/02_EnumerateDevices/02_EnumerateDevices.cpp
Normal file
52
samples/02_EnumerateDevices/02_EnumerateDevices.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// 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 : 02_EnumerateDevices
|
||||||
|
// Enumerate physical devices
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "02_EnumerateDevices";
|
||||||
|
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));
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
// enumerate the physicalDevices
|
||||||
|
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||||
|
|
||||||
|
// Note: PhysicalDevices are not created, but just enumerated. Therefore, there is nothing like a UniquePhysicalDevice.
|
||||||
|
// A PhysicalDevice is unique by definition, and there's no need to destroy it.
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/02_EnumerateDevices/CMakeLists.txt
Normal file
41
samples/02_EnumerateDevices/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(02_EnumerateDevices)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
02_EnumerateDevices.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(02_EnumerateDevices
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(02_EnumerateDevices PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(02_EnumerateDevices PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(02_EnumerateDevices PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(02_EnumerateDevices "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
66
samples/03_InitDevice/03_InitDevice.cpp
Normal file
66
samples/03_InitDevice/03_InitDevice.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
// 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 : 03_InitDevice
|
||||||
|
// Create and destroy a device
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "03_InitDevice";
|
||||||
|
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));
|
||||||
|
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||||
|
assert(!physicalDevices.empty());
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
// get the QueueFamilyProperties of the first PhysicalDevice
|
||||||
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||||
|
|
||||||
|
// get the first index into queueFamiliyProperties which supports graphics
|
||||||
|
size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(),
|
||||||
|
std::find_if(queueFamilyProperties.begin(),
|
||||||
|
queueFamilyProperties.end(),
|
||||||
|
[](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; }));
|
||||||
|
assert(graphicsQueueFamilyIndex < queueFamilyProperties.size());
|
||||||
|
|
||||||
|
// create a UniqueDevice
|
||||||
|
float queuePriority = 0.0f;
|
||||||
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo(vk::DeviceQueueCreateFlags(), static_cast<uint32_t>(graphicsQueueFamilyIndex), 1, &queuePriority);
|
||||||
|
vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo(vk::DeviceCreateFlags(), 1, &deviceQueueCreateInfo));
|
||||||
|
|
||||||
|
// Note: No need to explicitly destroy the device, as the corresponding destroy function is
|
||||||
|
// called by the destructor of the UniqueDevice on leaving this scope.
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/03_InitDevice/CMakeLists.txt
Normal file
41
samples/03_InitDevice/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(03_InitDevice)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
03_InitDevice.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(03_InitDevice
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(03_InitDevice PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(03_InitDevice PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(03_InitDevice PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(03_InitDevice "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
66
samples/04_InitCommandBuffer/04_InitCommandBuffer.cpp
Normal file
66
samples/04_InitCommandBuffer/04_InitCommandBuffer.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
// 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 : 04_InitCommandBuffer
|
||||||
|
// Create command buffer
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "04_InitCommandBuffer";
|
||||||
|
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));
|
||||||
|
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||||
|
assert(!physicalDevices.empty());
|
||||||
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||||
|
size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(),
|
||||||
|
std::find_if(queueFamilyProperties.begin(),
|
||||||
|
queueFamilyProperties.end(),
|
||||||
|
[](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; }));
|
||||||
|
assert(graphicsQueueFamilyIndex < queueFamilyProperties.size());
|
||||||
|
float queuePriority = 0.0f;
|
||||||
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast<uint32_t>(graphicsQueueFamilyIndex), 1, &queuePriority);
|
||||||
|
vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo));
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
// create a UniqueCommandPool to allocate a CommandBuffer from
|
||||||
|
vk::UniqueCommandPool commandPool = device->createCommandPoolUnique(vk::CommandPoolCreateInfo(vk::CommandPoolCreateFlags(), deviceQueueCreateInfo.queueFamilyIndex));
|
||||||
|
|
||||||
|
// allocate a CommandBuffer from the CommandPool
|
||||||
|
std::vector<vk::UniqueCommandBuffer> commandBuffers = device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(commandPool.get(), vk::CommandBufferLevel::ePrimary, 1));
|
||||||
|
|
||||||
|
// Note: No need to explicitly free the CommandBuffer or destroy the CommandPool, as the corresponding free and destroy
|
||||||
|
// functions are called by the destructor of the UniqueCommandBuffer and the UniqueCommandPool on leaving this scope.
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/04_InitCommandBuffer/CMakeLists.txt
Normal file
41
samples/04_InitCommandBuffer/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(04_InitCommandBuffer)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
04_InitCommandBuffer.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(04_InitCommandBuffer
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(04_InitCommandBuffer PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(04_InitCommandBuffer PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(04_InitCommandBuffer PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(04_InitCommandBuffer "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
281
samples/05_InitSwapchain/05_InitSwapchain.cpp
Normal file
281
samples/05_InitSwapchain/05_InitSwapchain.cpp
Normal file
@ -0,0 +1,281 @@
|
|||||||
|
// 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 : 05_InitSwapchain
|
||||||
|
// Initialize a swapchain
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "05_InitSwapchain";
|
||||||
|
static char const* EngineName = "Vulkan.hpp";
|
||||||
|
|
||||||
|
template<class T, class Compare>
|
||||||
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp)
|
||||||
|
{
|
||||||
|
return assert(!comp(hi, lo)),
|
||||||
|
comp(v, lo) ? lo : comp(hi, v) ? hi : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||||
|
{
|
||||||
|
return clamp(v, lo, hi, std::less<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::vector<char const*> getDeviceExtensions()
|
||||||
|
{
|
||||||
|
std::vector<char const*> extensions;
|
||||||
|
|
||||||
|
extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
||||||
|
|
||||||
|
return extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::vector<char const*> getInstanceExtensions()
|
||||||
|
{
|
||||||
|
std::vector<char const*> extensions;
|
||||||
|
|
||||||
|
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
||||||
|
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
|
||||||
|
extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_IOS_MVK)
|
||||||
|
extensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
|
||||||
|
extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_MIR_KHR)
|
||||||
|
extensions.push_back(VK_KHR_MIR_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_VI_NN)
|
||||||
|
extensions.push_back(VK_NN_VI_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
||||||
|
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XCB_KHR)
|
||||||
|
extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
|
||||||
|
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
|
||||||
|
extensions.push_back(VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
|
{
|
||||||
|
switch (uMsg)
|
||||||
|
{
|
||||||
|
case WM_CLOSE:
|
||||||
|
PostQuitMessage(0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
||||||
|
}
|
||||||
|
|
||||||
|
HWND initializeWindow(std::string const& className, std::string const& windowName, LONG width, LONG height)
|
||||||
|
{
|
||||||
|
WNDCLASSEX windowClass;
|
||||||
|
memset(&windowClass, 0, sizeof(WNDCLASSEX));
|
||||||
|
|
||||||
|
HINSTANCE instance = GetModuleHandle(nullptr);
|
||||||
|
windowClass.cbSize = sizeof(WNDCLASSEX);
|
||||||
|
windowClass.style = CS_HREDRAW | CS_VREDRAW;
|
||||||
|
windowClass.lpfnWndProc = WindowProc;
|
||||||
|
windowClass.hInstance = instance;
|
||||||
|
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||||
|
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||||
|
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
||||||
|
windowClass.lpszClassName = className.c_str();
|
||||||
|
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
|
||||||
|
|
||||||
|
if (!RegisterClassEx(&windowClass))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to register WNDCLASSEX -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
RECT windowRect = { 0, 0, width, height };
|
||||||
|
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
|
||||||
|
|
||||||
|
HWND window = CreateWindowEx(0, className.c_str(), windowName.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, windowRect.right - windowRect.left,
|
||||||
|
windowRect.bottom - windowRect.top, nullptr, nullptr, instance, nullptr);
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to create window -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1);
|
||||||
|
std::vector<char const*> instanceExtensions = getInstanceExtensions();
|
||||||
|
vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo, 0, nullptr, static_cast<uint32_t>(instanceExtensions.size()), instanceExtensions.data());
|
||||||
|
vk::UniqueInstance instance = vk::createInstanceUnique(instanceCreateInfo);
|
||||||
|
|
||||||
|
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||||
|
assert(!physicalDevices.empty());
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
uint32_t width = 50;
|
||||||
|
uint32_t height = 50;
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
HWND window = initializeWindow(AppName, "Sample", width, height);
|
||||||
|
|
||||||
|
vk::UniqueSurfaceKHR surface = instance->createWin32SurfaceKHRUnique(vk::Win32SurfaceCreateInfoKHR(vk::Win32SurfaceCreateFlagsKHR(), GetModuleHandle(nullptr), window));
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// determine a queueFamilyIndex that supports graphics
|
||||||
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||||
|
size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(),
|
||||||
|
std::find_if(queueFamilyProperties.begin(),
|
||||||
|
queueFamilyProperties.end(),
|
||||||
|
[](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; }));
|
||||||
|
|
||||||
|
// determine a queueFamilyIndex that suports present
|
||||||
|
// first check if the graphicsQueueFamiliyIndex is good enough
|
||||||
|
size_t presentQueueFamilyIndex = physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(graphicsQueueFamilyIndex), surface.get()) ? graphicsQueueFamilyIndex : queueFamilyProperties.size();
|
||||||
|
if (presentQueueFamilyIndex == queueFamilyProperties.size())
|
||||||
|
{
|
||||||
|
// the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both graphics and present
|
||||||
|
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
|
||||||
|
{
|
||||||
|
if ((queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
|
||||||
|
{
|
||||||
|
graphicsQueueFamilyIndex = i;
|
||||||
|
presentQueueFamilyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (presentQueueFamilyIndex == queueFamilyProperties.size())
|
||||||
|
{
|
||||||
|
// there's nothing like a single family index that supports both graphics and present -> look for an other family index that supports present
|
||||||
|
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
|
||||||
|
{
|
||||||
|
if (physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
|
||||||
|
{
|
||||||
|
presentQueueFamilyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((graphicsQueueFamilyIndex == queueFamilyProperties.size()) || (presentQueueFamilyIndex == queueFamilyProperties.size()))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Could not find a queue for graphics or present -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a device
|
||||||
|
float queuePriority = 0.0f;
|
||||||
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast<uint32_t>(graphicsQueueFamilyIndex), 1, &queuePriority);
|
||||||
|
std::vector<char const*> deviceExtensionNames = getDeviceExtensions();
|
||||||
|
vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo, 0, nullptr, static_cast<uint32_t>(deviceExtensionNames.size()), deviceExtensionNames.data()));
|
||||||
|
|
||||||
|
// get the supported VkFormats
|
||||||
|
std::vector<vk::SurfaceFormatKHR> formats = physicalDevices[0].getSurfaceFormatsKHR(surface.get());
|
||||||
|
assert(!formats.empty());
|
||||||
|
vk::Format format = (formats[0].format == vk::Format::eUndefined) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
|
||||||
|
|
||||||
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevices[0].getSurfaceCapabilitiesKHR(surface.get());
|
||||||
|
|
||||||
|
VkExtent2D swapchainExtent;
|
||||||
|
if (surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max())
|
||||||
|
{
|
||||||
|
// If the surface size is undefined, the size is set to the size of the images requested.
|
||||||
|
swapchainExtent.width = clamp(width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width);
|
||||||
|
swapchainExtent.height = clamp(height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If the surface size is defined, the swap chain size must match
|
||||||
|
swapchainExtent = surfaceCapabilities.currentExtent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The FIFO present mode is guaranteed by the spec to be supported
|
||||||
|
vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo;
|
||||||
|
|
||||||
|
vk::SurfaceTransformFlagBitsKHR preTransform = (surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity) ? vk::SurfaceTransformFlagBitsKHR::eIdentity : surfaceCapabilities.currentTransform;
|
||||||
|
|
||||||
|
vk::CompositeAlphaFlagBitsKHR compositeAlpha =
|
||||||
|
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied) ? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied :
|
||||||
|
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied) ? vk::CompositeAlphaFlagBitsKHR::ePostMultiplied :
|
||||||
|
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::eInherit) ? vk::CompositeAlphaFlagBitsKHR::eInherit : vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
||||||
|
|
||||||
|
vk::SwapchainCreateInfoKHR swapChainCreateInfo(vk::SwapchainCreateFlagsKHR(), surface.get(), surfaceCapabilities.minImageCount, format, vk::ColorSpaceKHR::eSrgbNonlinear,
|
||||||
|
swapchainExtent, 1, vk::ImageUsageFlagBits::eColorAttachment, vk::SharingMode::eExclusive, 0, nullptr, preTransform, compositeAlpha, swapchainPresentMode, true, nullptr);
|
||||||
|
|
||||||
|
uint32_t queueFamilyIndices[2] = { static_cast<uint32_t>(graphicsQueueFamilyIndex), static_cast<uint32_t>(presentQueueFamilyIndex) };
|
||||||
|
if (graphicsQueueFamilyIndex != presentQueueFamilyIndex)
|
||||||
|
{
|
||||||
|
// If the graphics and present queues are from different queue families, we either have to explicitly transfer ownership of images between
|
||||||
|
// the queues, or we have to create the swapchain with imageSharingMode as VK_SHARING_MODE_CONCURRENT
|
||||||
|
swapChainCreateInfo.imageSharingMode = vk::SharingMode::eConcurrent;
|
||||||
|
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
||||||
|
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
vk::UniqueSwapchainKHR swapChain = device->createSwapchainKHRUnique(swapChainCreateInfo);
|
||||||
|
|
||||||
|
std::vector<vk::Image> swapChainImages = device->getSwapchainImagesKHR(swapChain.get());
|
||||||
|
|
||||||
|
std::vector<vk::UniqueImageView> imageViews;
|
||||||
|
imageViews.reserve(swapChainImages.size());
|
||||||
|
for (auto image : swapChainImages)
|
||||||
|
{
|
||||||
|
vk::ComponentMapping componentMapping(vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA);
|
||||||
|
vk::ImageSubresourceRange subResourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
|
||||||
|
vk::ImageViewCreateInfo imageViewCreateInfo(vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format, componentMapping, subResourceRange);
|
||||||
|
imageViews.push_back(device->createImageViewUnique(imageViewCreateInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
DestroyWindow(window);
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Note: No need to explicitly destroy the ImageViews or the swapChain, as the corresponding destroy
|
||||||
|
// functions are called by the destructor of the UniqueImageView and the UniqueSwapChainKHR on leaving this scope.
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (std::runtime_error err)
|
||||||
|
{
|
||||||
|
std::cout << "std::runtime_error: " << err.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/05_InitSwapchain/CMakeLists.txt
Normal file
41
samples/05_InitSwapchain/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(05_InitSwapchain)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
05_InitSwapchain.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(05_InitSwapchain
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(05_InitSwapchain PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(05_InitSwapchain PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(05_InitSwapchain PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(05_InitSwapchain "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
262
samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp
Normal file
262
samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp
Normal file
@ -0,0 +1,262 @@
|
|||||||
|
// 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 : 05_InitSwapchain
|
||||||
|
// Initialize a swapchain
|
||||||
|
|
||||||
|
#include "vulkan/vulkan.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static char const* AppName = "05_InitSwapchain";
|
||||||
|
static char const* EngineName = "Vulkan.hpp";
|
||||||
|
|
||||||
|
template<class T, class Compare>
|
||||||
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp)
|
||||||
|
{
|
||||||
|
return assert(!comp(hi, lo)),
|
||||||
|
comp(v, lo) ? lo : comp(hi, v) ? hi : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||||
|
{
|
||||||
|
return clamp(v, lo, hi, std::less<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::vector<char const*> getDeviceExtensions()
|
||||||
|
{
|
||||||
|
std::vector<char const*> extensions;
|
||||||
|
|
||||||
|
extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
||||||
|
|
||||||
|
return extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::vector<char const*> getInstanceExtensions()
|
||||||
|
{
|
||||||
|
std::vector<char const*> extensions;
|
||||||
|
|
||||||
|
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
||||||
|
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
|
||||||
|
extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_IOS_MVK)
|
||||||
|
extensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
|
||||||
|
extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_MIR_KHR)
|
||||||
|
extensions.push_back(VK_KHR_MIR_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_VI_NN)
|
||||||
|
extensions.push_back(VK_NN_VI_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
||||||
|
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XCB_KHR)
|
||||||
|
extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
|
||||||
|
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
|
||||||
|
#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
|
||||||
|
extensions.push_back(VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
|
{
|
||||||
|
switch (uMsg)
|
||||||
|
{
|
||||||
|
case WM_CLOSE:
|
||||||
|
PostQuitMessage(0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
||||||
|
}
|
||||||
|
|
||||||
|
HWND initializeWindow(std::string const& className, std::string const& windowName, LONG width, LONG height)
|
||||||
|
{
|
||||||
|
WNDCLASSEX windowClass;
|
||||||
|
memset(&windowClass, 0, sizeof(WNDCLASSEX));
|
||||||
|
|
||||||
|
HINSTANCE instance = GetModuleHandle(nullptr);
|
||||||
|
windowClass.cbSize = sizeof(WNDCLASSEX);
|
||||||
|
windowClass.style = CS_HREDRAW | CS_VREDRAW;
|
||||||
|
windowClass.lpfnWndProc = WindowProc;
|
||||||
|
windowClass.hInstance = instance;
|
||||||
|
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||||
|
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||||
|
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
||||||
|
windowClass.lpszClassName = className.c_str();
|
||||||
|
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
|
||||||
|
|
||||||
|
if (!RegisterClassEx(&windowClass))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to register WNDCLASSEX -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
RECT windowRect = { 0, 0, width, height };
|
||||||
|
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
|
||||||
|
|
||||||
|
HWND window = CreateWindowEx(0, className.c_str(), windowName.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, windowRect.right - windowRect.left,
|
||||||
|
windowRect.bottom - windowRect.top, nullptr, nullptr, instance, nullptr);
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to create window -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1);
|
||||||
|
std::vector<char const*> instanceExtensions = getInstanceExtensions();
|
||||||
|
vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo, 0, nullptr, static_cast<uint32_t>(instanceExtensions.size()), instanceExtensions.data());
|
||||||
|
vk::UniqueInstance instance = vk::createInstanceUnique(instanceCreateInfo);
|
||||||
|
|
||||||
|
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||||
|
assert(!physicalDevices.empty());
|
||||||
|
|
||||||
|
uint32_t width = 50;
|
||||||
|
uint32_t height = 50;
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
HWND window = initializeWindow(AppName, "Sample", width, height);
|
||||||
|
|
||||||
|
vk::UniqueSurfaceKHR surface = instance->createWin32SurfaceKHRUnique(vk::Win32SurfaceCreateInfoKHR(vk::Win32SurfaceCreateFlagsKHR(), GetModuleHandle(nullptr), window));
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// determine a queueFamilyIndex that supports graphics
|
||||||
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||||
|
size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(),
|
||||||
|
std::find_if(queueFamilyProperties.begin(),
|
||||||
|
queueFamilyProperties.end(),
|
||||||
|
[](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; }));
|
||||||
|
|
||||||
|
// determine a queueFamilyIndex that suports present
|
||||||
|
// first check if the graphicsQueueFamiliyIndex is good enough
|
||||||
|
size_t presentQueueFamilyIndex = physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(graphicsQueueFamilyIndex), surface.get()) ? graphicsQueueFamilyIndex : queueFamilyProperties.size();
|
||||||
|
if (presentQueueFamilyIndex == queueFamilyProperties.size())
|
||||||
|
{
|
||||||
|
// the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both graphics and present
|
||||||
|
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
|
||||||
|
{
|
||||||
|
if ((queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
|
||||||
|
{
|
||||||
|
graphicsQueueFamilyIndex = i;
|
||||||
|
presentQueueFamilyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (presentQueueFamilyIndex == queueFamilyProperties.size())
|
||||||
|
{
|
||||||
|
// there's nothing like a single family index that supports both graphics and present -> look for an other family index that supports present
|
||||||
|
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
|
||||||
|
{
|
||||||
|
if (physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
|
||||||
|
{
|
||||||
|
presentQueueFamilyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((graphicsQueueFamilyIndex == queueFamilyProperties.size()) || (presentQueueFamilyIndex == queueFamilyProperties.size()))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Could not find a queue for graphics or present -> terminating");
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a device
|
||||||
|
float queuePriority = 0.0f;
|
||||||
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast<uint32_t>(graphicsQueueFamilyIndex), 1, &queuePriority);
|
||||||
|
std::vector<char const*> deviceExtensionNames = getDeviceExtensions();
|
||||||
|
vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo, 0, nullptr, static_cast<uint32_t>(deviceExtensionNames.size()), deviceExtensionNames.data()));
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_START */
|
||||||
|
|
||||||
|
const vk::Format depthFormat = vk::Format::eD16Unorm;
|
||||||
|
vk::FormatProperties formatProperties = physicalDevices[0].getFormatProperties(depthFormat);
|
||||||
|
|
||||||
|
vk::ImageTiling tiling;
|
||||||
|
if (formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment)
|
||||||
|
{
|
||||||
|
tiling = vk::ImageTiling::eLinear;
|
||||||
|
}
|
||||||
|
else if (formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment)
|
||||||
|
{
|
||||||
|
tiling = vk::ImageTiling::eOptimal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::runtime_error("DepthStencilAttachment is not supported for D16Unorm depth format.");
|
||||||
|
}
|
||||||
|
vk::ImageCreateInfo imageCreateInfo(vk::ImageCreateFlags(), vk::ImageType::e2D, depthFormat, vk::Extent3D(width, height, 1), 1, 1, vk::SampleCountFlagBits::e1, tiling, vk::ImageUsageFlagBits::eDepthStencilAttachment);
|
||||||
|
vk::UniqueImage depthImage = device->createImageUnique(imageCreateInfo);
|
||||||
|
|
||||||
|
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevices[0].getMemoryProperties();
|
||||||
|
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements(depthImage.get());
|
||||||
|
uint32_t typeBits = memoryRequirements.memoryTypeBits;
|
||||||
|
uint32_t typeIndex = ~0;
|
||||||
|
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++)
|
||||||
|
{
|
||||||
|
if ((typeBits & 1) && ((memoryProperties.memoryTypes[i].propertyFlags & vk::MemoryPropertyFlagBits::eDeviceLocal) == vk::MemoryPropertyFlagBits::eDeviceLocal))
|
||||||
|
{
|
||||||
|
typeIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
typeBits >>= 1;
|
||||||
|
}
|
||||||
|
assert(typeIndex != ~0);
|
||||||
|
vk::UniqueDeviceMemory depthMemory = device->allocateMemoryUnique(vk::MemoryAllocateInfo(memoryRequirements.size, typeIndex));
|
||||||
|
|
||||||
|
device->bindImageMemory(depthImage.get(), depthMemory.get(), 0);
|
||||||
|
|
||||||
|
vk::ComponentMapping componentMapping(vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA);
|
||||||
|
vk::ImageSubresourceRange subResourceRange(vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1);
|
||||||
|
vk::UniqueImageView depthView = device->createImageViewUnique(vk::ImageViewCreateInfo(vk::ImageViewCreateFlags(), depthImage.get(), vk::ImageViewType::e2D, depthFormat, componentMapping, subResourceRange));
|
||||||
|
|
||||||
|
/* VULKAN_HPP_KEY_END */
|
||||||
|
|
||||||
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
DestroyWindow(window);
|
||||||
|
#else
|
||||||
|
#pragma error "unhandled platform"
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
catch (vk::SystemError err)
|
||||||
|
{
|
||||||
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
catch (std::runtime_error err)
|
||||||
|
{
|
||||||
|
std::cout << "std::runtime_error: " << err.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cout << "unknown error\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
41
samples/06_InitDepthBuffer/CMakeLists.txt
Normal file
41
samples/06_InitDepthBuffer/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
project(06_InitDepthBuffer)
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
06_InitDepthBuffer.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
source_group(headers FILES ${HEADERS})
|
||||||
|
source_group(sources FILES ${SOURCES})
|
||||||
|
|
||||||
|
add_executable(06_InitDepthBuffer
|
||||||
|
${HEADERS}
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(06_InitDepthBuffer PROPERTIES FOLDER "Samples")
|
||||||
|
|
||||||
|
if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP)
|
||||||
|
target_include_directories(06_InitDepthBuffer PUBLIC "${CMAKE_SOURCE_DIR}")
|
||||||
|
endif()
|
||||||
|
target_include_directories(06_InitDepthBuffer PUBLIC "$ENV{VK_SDK_PATH}/include")
|
||||||
|
|
||||||
|
target_link_libraries(06_InitDepthBuffer "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")
|
34
samples/CMakeLists.txt
Normal file
34
samples/CMakeLists.txt
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
option (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP OFF)
|
||||||
|
|
||||||
|
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||||
|
add_definitions(-DNOMINMAX -DVK_USE_PLATFORM_WIN32_KHR)
|
||||||
|
else()
|
||||||
|
error("unhandled platform !")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
FILE (GLOB linkunits ${CMAKE_CURRENT_SOURCE_DIR}/*)
|
||||||
|
|
||||||
|
FOREACH( linkunit ${linkunits} )
|
||||||
|
if( IS_DIRECTORY ${linkunit} )
|
||||||
|
if( EXISTS ${linkunit}/CMakeLists.txt )
|
||||||
|
string( REGEX REPLACE "^.*/([^/]*)$" "\\1" LINK_NAME ${linkunit} )
|
||||||
|
add_subdirectory( ${LINK_NAME} )
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
ENDFOREACH( linkunit ${linkunits} )
|
Loading…
Reference in New Issue
Block a user