From 33c7954b6e7b9c54cbc2cc8c3083e1dadc68fb9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Mon, 9 Apr 2018 10:53:55 +0200 Subject: [PATCH] Sample on vulkan.hpp: 07_InitUniformBuffer. (#203) --- .gitmodules | 3 + glm | 1 + .../06_InitDepthBuffer/06_InitDepthBuffer.cpp | 6 +- .../07_InitUniformBuffer.cpp | 104 ++++++++++++++++++ samples/07_InitUniformBuffer/CMakeLists.txt | 41 +++++++ 5 files changed, 152 insertions(+), 3 deletions(-) create mode 160000 glm create mode 100644 samples/07_InitUniformBuffer/07_InitUniformBuffer.cpp create mode 100644 samples/07_InitUniformBuffer/CMakeLists.txt diff --git a/.gitmodules b/.gitmodules index e42f4f9..9066fe1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ +[submodule "glm"] + path = glm + url = https://github.com/g-truc/glm.git [submodule "tinyxml2"] path = tinyxml2 url = https://github.com/leethomason/tinyxml2.git diff --git a/glm b/glm new file mode 160000 index 0000000..6fa203e --- /dev/null +++ b/glm @@ -0,0 +1 @@ +Subproject commit 6fa203eeb7fbcbb6f620501fad40359c8a456049 diff --git a/samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp b/samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp index 98efb2c..adfc183 100644 --- a/samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp +++ b/samples/06_InitDepthBuffer/06_InitDepthBuffer.cpp @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -// VulkanHpp Samples : 05_InitSwapchain -// Initialize a swapchain +// VulkanHpp Samples : 06_InitDepthBuffer +// Initialize a depth buffer #include "vulkan/vulkan.hpp" #include -static char const* AppName = "05_InitSwapchain"; +static char const* AppName = "06_InitDepthBuffer"; static char const* EngineName = "Vulkan.hpp"; template diff --git a/samples/07_InitUniformBuffer/07_InitUniformBuffer.cpp b/samples/07_InitUniformBuffer/07_InitUniformBuffer.cpp new file mode 100644 index 0000000..e035c52 --- /dev/null +++ b/samples/07_InitUniformBuffer/07_InitUniformBuffer.cpp @@ -0,0 +1,104 @@ +// 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 : 07_InitUniformBuffer +// Initialize a uniform buffer + +#include +#include "vulkan/vulkan.hpp" + +#define GLM_FORCE_RADIANS +#include + +static char const* AppName = "07_InitUniformBuffer"; +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::InstanceCreateInfo instanceCreateInfo({}, &appInfo); + vk::UniqueInstance instance = vk::createInstanceUnique(instanceCreateInfo); + + std::vector physicalDevices = instance->enumeratePhysicalDevices(); + assert(!physicalDevices.empty()); + + // determine a queueFamilyIndex that supports graphics + std::vector 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; })); + + // create a device + float queuePriority = 0.0f; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast(graphicsQueueFamilyIndex), 1, &queuePriority); + vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo, 0, nullptr)); + + /* VULKAN_HPP_KEY_START */ + + glm::mat4x4 projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f); + glm::mat4x4 view = glm::lookAt(glm::vec3(-5.0f, 3.0f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)); + glm::mat4x4 model = glm::mat4x4(1.0f); + glm::mat4x4 clip = glm::mat4x4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f); // vulkan clip space has inverted y and half z ! + glm::mat4x4 mvpc = clip * projection * view * model; + + vk::UniqueBuffer uniformDataBuffer = device->createBufferUnique(vk::BufferCreateInfo(vk::BufferCreateFlags(), sizeof(mvpc), vk::BufferUsageFlagBits::eUniformBuffer)); + + vk::MemoryRequirements memoryRequirements = device->getBufferMemoryRequirements(uniformDataBuffer.get()); + uint32_t typeBits = memoryRequirements.memoryTypeBits; + + vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevices[0].getMemoryProperties(); + vk::MemoryPropertyFlags requirementsMask = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; + uint32_t typeIndex = ~0; + for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) + { + if ((typeBits & 1) && ((memoryProperties.memoryTypes[i].propertyFlags & requirementsMask) == requirementsMask)) + { + typeIndex = i; + break; + } + typeBits >>= 1; + } + assert(typeIndex != ~0); + vk::UniqueDeviceMemory uniformDataMemory = device->allocateMemoryUnique(vk::MemoryAllocateInfo(memoryRequirements.size, typeIndex)); + + uint8_t* pData = static_cast(device->mapMemory(uniformDataMemory.get(), 0, memoryRequirements.size)); + memcpy(pData, &mvpc, sizeof(mvpc)); + device->unmapMemory(uniformDataMemory.get()); + + device->bindBufferMemory(uniformDataBuffer.get(), uniformDataMemory.get(), 0); + + // Note: No need to explicitly destroy the memory or the buffer, as the corresponding destroy function is + // called by the destructor of the UniqueMemory or UniqueBuffer, respectively, 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; +} diff --git a/samples/07_InitUniformBuffer/CMakeLists.txt b/samples/07_InitUniformBuffer/CMakeLists.txt new file mode 100644 index 0000000..5d89e96 --- /dev/null +++ b/samples/07_InitUniformBuffer/CMakeLists.txt @@ -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(07_InitUniformBuffer) + +set(HEADERS +) + +set(SOURCES + 07_InitUniformBuffer.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(07_InitUniformBuffer + ${HEADERS} + ${SOURCES} +) + +set_target_properties(07_InitUniformBuffer PROPERTIES FOLDER "Samples") + +if (SAMPLES_BUILD_WITH_LOCAL_VULKAN_HPP) + target_include_directories(07_InitUniformBuffer PUBLIC "${CMAKE_SOURCE_DIR}") +endif() +target_include_directories(07_InitUniformBuffer PUBLIC "$ENV{VK_SDK_PATH}/include" "${CMAKE_SOURCE_DIR}/glm") + +target_link_libraries(07_InitUniformBuffer "$ENV{VK_SDK_PATH}/Lib/vulkan-1.lib")