Building samples for Linux (#491)
* CMake: Fix SAMPLES_BUILD and TESTS_BUILD * CMake: Enable sample build on linux * samples: Use glfw for window creation * samples: Fix include order * samples: Support new window handling * samples: Add build target for util * samples: Update CMake scripts to use util target * samples: Add WindowData to manage unique window * samples: Surface handling using WIndowData * sampels: Remove call for vk::su::destroyWindow() * CMake: FIx more options * samples: Build SurfaceCapabilities only on WIN32 * samples: Fixed RayTracing sample to build on Linux * samples: Fix wrong check on SurfaceProtectedCapabilitiesKHR This also fixes compilation with MinGW gcc/clang * CMake: check CMAKE_SYSTEM_NAME for Linux samples * CMake: Add source group for utils * samples: Fix potential bugs * samples: Reduce warnings on gcc/clang * samples: Fix missmatched new/free() which is UB * samples: Add missing initialization for dynamic dispatcher * samples: Remove unnecessary dispacther construct Co-authored-by: Andreas Süßenbach <asuessenbach@nvidia.com>
This commit is contained in:
committed by
Andreas Süßenbach
parent
f1668a6e28
commit
b6a190f4a2
43
samples/utils/CMakeLists.txt
Normal file
43
samples/utils/CMakeLists.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
# Copyright(c) 2020, 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(utils)
|
||||
|
||||
set(HEADERS
|
||||
math.hpp
|
||||
shaders.hpp
|
||||
utils.hpp
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
math.cpp
|
||||
shaders.cpp
|
||||
utils.cpp
|
||||
)
|
||||
|
||||
source_group(headers FILES ${HEADERS})
|
||||
source_group(sources FILES ${SOURCES})
|
||||
|
||||
add_library(utils
|
||||
${SOURCES}
|
||||
${HEADERS}
|
||||
)
|
||||
|
||||
target_link_libraries(utils PUBLIC glfw)
|
||||
target_link_libraries(utils PUBLIC glslang)
|
||||
target_link_libraries(utils PUBLIC glslang-default-resource-limits)
|
||||
target_link_libraries(utils PUBLIC SPIRV)
|
||||
target_compile_definitions(utils PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
|
||||
@@ -154,7 +154,7 @@ namespace vk
|
||||
vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState(false, vk::BlendFactor::eZero, vk::BlendFactor::eZero, vk::BlendOp::eAdd, vk::BlendFactor::eZero,
|
||||
vk::BlendFactor::eZero, vk::BlendOp::eAdd, colorComponentFlags);
|
||||
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(vk::PipelineColorBlendStateCreateFlags(), false, vk::LogicOp::eNoOp, 1, &pipelineColorBlendAttachmentState,
|
||||
{ { (1.0f, 1.0f, 1.0f, 1.0f) } });
|
||||
{ { 1.0f, 1.0f, 1.0f, 1.0f } });
|
||||
|
||||
vk::DynamicState dynamicStates[2] = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
|
||||
vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo(vk::PipelineDynamicStateCreateFlags(), 2, dynamicStates);
|
||||
@@ -608,7 +608,7 @@ namespace vk
|
||||
bufferInfos.reserve(bufferData.size());
|
||||
|
||||
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
||||
writeDescriptorSets.reserve(bufferData.size() + textureData.empty() ? 0 : 1);
|
||||
writeDescriptorSets.reserve(bufferData.size() + (textureData.empty() ? 0 : 1));
|
||||
uint32_t dstBinding = bindingOffset;
|
||||
for (auto const& bd : bufferData)
|
||||
{
|
||||
@@ -666,15 +666,16 @@ namespace vk
|
||||
imageView = device->createImageViewUnique(imageViewCreateInfo);
|
||||
}
|
||||
|
||||
SurfaceData::SurfaceData(vk::UniqueInstance &instance, std::string const& className, std::string const& windowName, vk::Extent2D const& extent_)
|
||||
SurfaceData::SurfaceData(vk::UniqueInstance &instance, std::string const &windowName, vk::Extent2D const &extent_)
|
||||
: extent(extent_)
|
||||
, window(vk::su::createWindow(windowName, extent))
|
||||
{
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
window = vk::su::initializeWindow(className.c_str(), windowName.c_str(), extent.width, extent.height);
|
||||
surface = instance->createWin32SurfaceKHRUnique(vk::Win32SurfaceCreateInfoKHR(vk::Win32SurfaceCreateFlagsKHR(), GetModuleHandle(nullptr), window));
|
||||
#else
|
||||
#pragma error "unhandled platform"
|
||||
#endif
|
||||
VkSurfaceKHR _surface;
|
||||
VkResult err = glfwCreateWindowSurface(instance.get(), window.handle, nullptr, &_surface);
|
||||
if (err != VK_SUCCESS)
|
||||
throw std::runtime_error("Failed to create window!");
|
||||
vk::ObjectDestroy<vk::Instance, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE> _deleter(instance.get());
|
||||
surface = vk::UniqueSurfaceKHR(_surface, _deleter);
|
||||
}
|
||||
|
||||
SwapChainData::SwapChainData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::SurfaceKHR const& surface, vk::Extent2D const& extent, vk::ImageUsageFlags usage,
|
||||
@@ -825,56 +826,54 @@ namespace vk
|
||||
memcpy(m_data, data, VK_UUID_SIZE * sizeof(uint8_t));
|
||||
}
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
WindowData::WindowData(GLFWwindow *wnd, std::string const &name, vk::Extent2D const &extent)
|
||||
: handle{wnd}
|
||||
, name{name}
|
||||
, extent{extent}
|
||||
{
|
||||
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)
|
||||
WindowData::WindowData(WindowData &&other)
|
||||
: handle{}
|
||||
, name{}
|
||||
, extent{}
|
||||
{
|
||||
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;
|
||||
std::swap(handle, other.handle);
|
||||
std::swap(name, other.name);
|
||||
std::swap(extent, other.extent);
|
||||
}
|
||||
|
||||
WindowData::~WindowData() noexcept
|
||||
{
|
||||
glfwDestroyWindow(handle);
|
||||
}
|
||||
|
||||
WindowData createWindow(std::string const &windowName, vk::Extent2D const &extent)
|
||||
{
|
||||
struct glfwContext
|
||||
{
|
||||
glfwContext()
|
||||
{
|
||||
glfwInit();
|
||||
glfwSetErrorCallback([](int error, const char *msg) {
|
||||
std::cerr << "glfw: "
|
||||
<< "(" << error << ") " << msg << std::endl;
|
||||
});
|
||||
}
|
||||
|
||||
~glfwContext()
|
||||
{
|
||||
glfwTerminate();
|
||||
}
|
||||
};
|
||||
|
||||
static auto glfwCtx = glfwContext();
|
||||
(void)glfwCtx;
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
GLFWwindow *window = glfwCreateWindow(extent.width, extent.height, windowName.c_str(), nullptr, nullptr);
|
||||
return WindowData(window, windowName, extent);
|
||||
}
|
||||
#else
|
||||
#pragma error "unhandled platform"
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
//
|
||||
|
||||
#include "vulkan/vulkan.hpp"
|
||||
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@@ -25,6 +29,71 @@ namespace vk
|
||||
{
|
||||
const uint64_t FenceTimeout = 100000000;
|
||||
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
commandBuffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
|
||||
func(commandBuffer);
|
||||
commandBuffer->end();
|
||||
queue.submit(vk::SubmitInfo(0, nullptr, nullptr, 1, &(*commandBuffer)), nullptr);
|
||||
queue.waitIdle();
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
|
||||
oneTimeSubmit(commandBuffer, queue, func);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const* pData, size_t count, size_t stride = sizeof(T))
|
||||
{
|
||||
assert(sizeof(T) <= stride);
|
||||
uint8_t* deviceData = static_cast<uint8_t*>(device->mapMemory(memory.get(), 0, count * stride));
|
||||
if (stride == sizeof(T))
|
||||
{
|
||||
memcpy(deviceData, pData, count * sizeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
memcpy(deviceData, &pData[i], sizeof(T));
|
||||
deviceData += stride;
|
||||
}
|
||||
}
|
||||
device->unmapMemory(memory.get());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const& data)
|
||||
{
|
||||
copyToDevice<T>(device, memory, &data, 1);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
VULKAN_HPP_INLINE constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||
{
|
||||
return v < lo ? lo : hi < v ? hi : v;
|
||||
}
|
||||
|
||||
void setImageLayout(vk::UniqueCommandBuffer const &commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout);
|
||||
|
||||
struct WindowData
|
||||
{
|
||||
WindowData(GLFWwindow *wnd, std::string const& name, vk::Extent2D const& extent);
|
||||
WindowData(const WindowData &) = delete;
|
||||
WindowData(WindowData &&other);
|
||||
~WindowData() noexcept;
|
||||
|
||||
GLFWwindow *handle;
|
||||
std::string name;
|
||||
vk::Extent2D extent;
|
||||
};
|
||||
|
||||
WindowData createWindow(std::string const &windowName, vk::Extent2D const &extent);
|
||||
|
||||
struct BufferData
|
||||
{
|
||||
BufferData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::DeviceSize size, vk::BufferUsageFlags usage,
|
||||
@@ -79,7 +148,7 @@ namespace vk
|
||||
vk::DeviceSize m_size;
|
||||
vk::BufferUsageFlags m_usage;
|
||||
vk::MemoryPropertyFlags m_propertyFlags;
|
||||
#endif)
|
||||
#endif
|
||||
};
|
||||
|
||||
struct ImageData
|
||||
@@ -100,12 +169,10 @@ namespace vk
|
||||
|
||||
struct SurfaceData
|
||||
{
|
||||
SurfaceData(vk::UniqueInstance &instance, std::string const& className, std::string const& windowName, vk::Extent2D const& extent);
|
||||
SurfaceData(vk::UniqueInstance &instance, std::string const& windowName, vk::Extent2D const& extent);
|
||||
|
||||
vk::Extent2D extent;
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
HWND window;
|
||||
#endif
|
||||
WindowData window;
|
||||
vk::UniqueSurfaceKHR surface;
|
||||
};
|
||||
|
||||
@@ -214,55 +281,6 @@ namespace vk
|
||||
return static_cast<TargetType>(value);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const* pData, size_t count, size_t stride = sizeof(T))
|
||||
{
|
||||
assert(sizeof(T) <= stride);
|
||||
uint8_t* deviceData = static_cast<uint8_t*>(device->mapMemory(memory.get(), 0, count * stride));
|
||||
if (stride == sizeof(T))
|
||||
{
|
||||
memcpy(deviceData, pData, count * sizeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
memcpy(deviceData, &pData[i], sizeof(T));
|
||||
deviceData += stride;
|
||||
}
|
||||
}
|
||||
device->unmapMemory(memory.get());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const& data)
|
||||
{
|
||||
copyToDevice<T>(device, memory, &data, 1);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
VULKAN_HPP_INLINE constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||
{
|
||||
return v < lo ? lo : hi < v ? hi : v;
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
commandBuffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
|
||||
func(commandBuffer);
|
||||
commandBuffer->end();
|
||||
queue.submit(vk::SubmitInfo(0, nullptr, nullptr, 1, &(*commandBuffer)), nullptr);
|
||||
queue.waitIdle();
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void oneTimeSubmit(vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue const& queue, Func const& func)
|
||||
{
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
|
||||
oneTimeSubmit(commandBuffer, queue, func);
|
||||
}
|
||||
|
||||
vk::UniqueDeviceMemory allocateMemory(vk::UniqueDevice const& device, vk::PhysicalDeviceMemoryProperties const& memoryProperties, vk::MemoryRequirements const& memoryRequirements,
|
||||
vk::MemoryPropertyFlags memoryPropertyFlags);
|
||||
bool contains(std::vector<vk::ExtensionProperties> const& extensionProperties, std::string const& extensionName);
|
||||
@@ -290,7 +308,6 @@ namespace vk
|
||||
vk::Format pickDepthFormat(vk::PhysicalDevice const& physicalDevice);
|
||||
vk::PresentModeKHR pickPresentMode(std::vector<vk::PresentModeKHR> const& presentModes);
|
||||
vk::SurfaceFormatKHR pickSurfaceFormat(std::vector<vk::SurfaceFormatKHR> const& formats);
|
||||
void setImageLayout(vk::UniqueCommandBuffer const& commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout);
|
||||
void submitAndWait(vk::UniqueDevice &device, vk::Queue queue, vk::UniqueCommandBuffer &commandBuffer);
|
||||
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData,
|
||||
@@ -299,11 +316,6 @@ namespace vk
|
||||
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData,
|
||||
std::vector<vk::su::TextureData> const& textureData, uint32_t bindingOffset = 0);
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
HWND initializeWindow(std::string const& className, std::string const& windowName, LONG width, LONG height);
|
||||
#else
|
||||
# error "unhandled platform"
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user