Sample on vulkan.hpp: 07_InitUniformBuffer. (#203)

This commit is contained in:
Andreas Süßenbach 2018-04-09 10:53:55 +02:00 committed by Markus Tavenrath
parent bbaa5956c6
commit 33c7954b6e
5 changed files with 152 additions and 3 deletions

3
.gitmodules vendored
View File

@ -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

1
glm Submodule

@ -0,0 +1 @@
Subproject commit 6fa203eeb7fbcbb6f620501fad40359c8a456049

View File

@ -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 <iostream>
static char const* AppName = "05_InitSwapchain";
static char const* AppName = "06_InitDepthBuffer";
static char const* EngineName = "Vulkan.hpp";
template<class T, class Compare>

View File

@ -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 <iostream>
#include "vulkan/vulkan.hpp"
#define GLM_FORCE_RADIANS
#include <glm/gtc/matrix_transform.hpp>
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<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
assert(!physicalDevices.empty());
// 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; }));
// create a device
float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast<uint32_t>(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<uint8_t*>(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;
}

View 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(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")