2016-02-16 15:07:25 +01:00
|
|
|
/*
|
|
|
|
|
* Vulkan Example - Cube map texture loading and displaying
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
|
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
2016-03-08 21:52:40 +01:00
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
2016-02-16 15:07:25 +01:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
|
|
|
|
|
#include <gli/gli.hpp>
|
|
|
|
|
|
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
|
#include "vulkanexamplebase.h"
|
|
|
|
|
|
|
|
|
|
#define VERTEX_BUFFER_BIND_ID 0
|
|
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
// Vertex layout for this example
|
|
|
|
|
std::vector<vkMeshLoader::VertexLayout> vertexLayout =
|
|
|
|
|
{
|
|
|
|
|
vkMeshLoader::VERTEX_LAYOUT_POSITION,
|
|
|
|
|
vkMeshLoader::VERTEX_LAYOUT_NORMAL,
|
|
|
|
|
vkMeshLoader::VERTEX_LAYOUT_UV
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-06-04 11:03:17 +02:00
|
|
|
bool displaySkybox = true;
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkTools::VulkanTexture cubeMap;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkPipelineVertexInputStateCreateInfo inputState;
|
|
|
|
|
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
|
|
|
|
|
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
|
|
|
|
} vertices;
|
|
|
|
|
|
|
|
|
|
struct {
|
2016-06-04 11:03:17 +02:00
|
|
|
vkMeshLoader::MeshBuffer skybox;
|
|
|
|
|
std::vector<vkMeshLoader::MeshBuffer> objects;
|
|
|
|
|
uint32_t objectIndex = 0;
|
2016-02-16 15:07:25 +01:00
|
|
|
} meshes;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
vkTools::UniformData objectVS;
|
|
|
|
|
vkTools::UniformData skyboxVS;
|
|
|
|
|
} uniformData;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 model;
|
2016-06-04 13:25:40 +02:00
|
|
|
float lodBias = 0.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
} uboVS;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkPipeline skybox;
|
|
|
|
|
VkPipeline reflect;
|
|
|
|
|
} pipelines;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkDescriptorSet object;
|
|
|
|
|
VkDescriptorSet skybox;
|
|
|
|
|
} descriptorSets;
|
|
|
|
|
|
|
|
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
|
|
|
|
|
|
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
|
|
|
|
zoom = -4.0f;
|
|
|
|
|
rotationSpeed = 0.25f;
|
2016-09-17 10:31:04 +02:00
|
|
|
rotation = { -7.25f, -120.0f, 0.0f };
|
2016-06-04 11:03:17 +02:00
|
|
|
enableTextOverlay = true;
|
2016-02-16 15:07:25 +01:00
|
|
|
title = "Vulkan Example - Cube map";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
|
|
|
|
// Clean up used Vulkan resources
|
|
|
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
|
|
|
|
|
|
// Clean up texture resources
|
|
|
|
|
vkDestroyImageView(device, cubeMap.view, nullptr);
|
|
|
|
|
vkDestroyImage(device, cubeMap.image, nullptr);
|
|
|
|
|
vkDestroySampler(device, cubeMap.sampler, nullptr);
|
|
|
|
|
vkFreeMemory(device, cubeMap.deviceMemory, nullptr);
|
|
|
|
|
|
|
|
|
|
vkDestroyPipeline(device, pipelines.skybox, nullptr);
|
|
|
|
|
vkDestroyPipeline(device, pipelines.reflect, nullptr);
|
|
|
|
|
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
for (size_t i = 0; i < meshes.objects.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.objects[i]);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.skybox);
|
|
|
|
|
|
|
|
|
|
vkTools::destroyUniformData(device, &uniformData.objectVS);
|
|
|
|
|
vkTools::destroyUniformData(device, &uniformData.skyboxVS);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 15:29:38 +01:00
|
|
|
void loadCubemap(std::string filename, VkFormat format, bool forceLinearTiling)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2016-03-25 15:29:38 +01:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
// Textures are stored inside the apk on Android (compressed)
|
|
|
|
|
// So they need to be loaded via the asset manager
|
|
|
|
|
AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
|
|
|
|
|
assert(asset);
|
|
|
|
|
size_t size = AAsset_getLength(asset);
|
|
|
|
|
assert(size > 0);
|
|
|
|
|
|
|
|
|
|
void *textureData = malloc(size);
|
|
|
|
|
AAsset_read(asset, textureData, size);
|
|
|
|
|
AAsset_close(asset);
|
|
|
|
|
|
|
|
|
|
gli::textureCube texCube(gli::load((const char*)textureData, size));
|
|
|
|
|
#else
|
2016-02-16 15:07:25 +01:00
|
|
|
gli::textureCube texCube(gli::load(filename));
|
2016-03-25 15:29:38 +01:00
|
|
|
#endif
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
assert(!texCube.empty());
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
cubeMap.width = texCube.dimensions().x;
|
|
|
|
|
cubeMap.height = texCube.dimensions().y;
|
|
|
|
|
cubeMap.mipLevels = texCube.levels();
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo memAllocInfo = vkTools::initializers::memoryAllocateInfo();
|
|
|
|
|
VkMemoryRequirements memReqs;
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
// Create a host-visible staging buffer that contains the raw image data
|
|
|
|
|
VkBuffer stagingBuffer;
|
|
|
|
|
VkDeviceMemory stagingMemory;
|
|
|
|
|
|
|
|
|
|
VkBufferCreateInfo bufferCreateInfo = vkTools::initializers::bufferCreateInfo();
|
|
|
|
|
bufferCreateInfo.size = texCube.size();
|
|
|
|
|
// This buffer is used as a transfer source for the buffer copy
|
|
|
|
|
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
|
|
|
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-30 20:57:38 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &stagingBuffer));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
// Get memory requirements for the staging buffer (alignment, memory type bits)
|
|
|
|
|
vkGetBufferMemoryRequirements(device, stagingBuffer, &memReqs);
|
|
|
|
|
memAllocInfo.allocationSize = memReqs.size;
|
|
|
|
|
// Get memory type index for a host visible buffer
|
2016-07-23 20:42:03 +02:00
|
|
|
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
2016-05-30 20:57:38 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &stagingMemory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffer, stagingMemory, 0));
|
2016-05-14 11:46:56 +02:00
|
|
|
|
|
|
|
|
// Copy texture data into staging buffer
|
|
|
|
|
uint8_t *data;
|
2016-05-30 20:57:38 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data));
|
2016-05-14 11:46:56 +02:00
|
|
|
memcpy(data, texCube.data(), texCube.size());
|
|
|
|
|
vkUnmapMemory(device, stagingMemory);
|
|
|
|
|
|
|
|
|
|
// Create optimal tiled target image
|
|
|
|
|
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
|
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
|
imageCreateInfo.format = format;
|
2016-06-04 11:31:22 +02:00
|
|
|
imageCreateInfo.mipLevels = cubeMap.mipLevels;
|
2016-05-14 11:46:56 +02:00
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
2016-05-14 11:46:56 +02:00
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2016-05-14 11:46:56 +02:00
|
|
|
imageCreateInfo.extent = { cubeMap.width, cubeMap.height, 1 };
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
2016-05-14 11:46:56 +02:00
|
|
|
// Cube faces count as array layers in Vulkan
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.arrayLayers = 6;
|
2016-05-14 11:46:56 +02:00
|
|
|
// This flag is required for cube map images
|
|
|
|
|
imageCreateInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &cubeMap.image));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkGetImageMemoryRequirements(device, cubeMap.image, &memReqs);
|
|
|
|
|
|
|
|
|
|
memAllocInfo.allocationSize = memReqs.size;
|
2016-07-23 20:42:03 +02:00
|
|
|
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &cubeMap.deviceMemory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, cubeMap.image, cubeMap.deviceMemory, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
2016-03-13 14:03:45 +01:00
|
|
|
|
2016-06-04 11:31:22 +02:00
|
|
|
// Setup buffer copy regions for each face including all of it's miplevels
|
|
|
|
|
std::vector<VkBufferImageCopy> bufferCopyRegions;
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
for (uint32_t face = 0; face < 6; face++)
|
|
|
|
|
{
|
|
|
|
|
for (uint32_t level = 0; level < cubeMap.mipLevels; level++)
|
|
|
|
|
{
|
|
|
|
|
VkBufferImageCopy bufferCopyRegion = {};
|
|
|
|
|
bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
bufferCopyRegion.imageSubresource.mipLevel = level;
|
|
|
|
|
bufferCopyRegion.imageSubresource.baseArrayLayer = face;
|
|
|
|
|
bufferCopyRegion.imageSubresource.layerCount = 1;
|
|
|
|
|
bufferCopyRegion.imageExtent.width = texCube[face][level].dimensions().x;
|
|
|
|
|
bufferCopyRegion.imageExtent.height = texCube[face][level].dimensions().y;
|
|
|
|
|
bufferCopyRegion.imageExtent.depth = 1;
|
|
|
|
|
bufferCopyRegion.bufferOffset = offset;
|
|
|
|
|
|
|
|
|
|
bufferCopyRegions.push_back(bufferCopyRegion);
|
|
|
|
|
|
|
|
|
|
// Increase offset into staging buffer for next level / face
|
|
|
|
|
offset += texCube[face][level].size();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
// Image barrier for optimal image (target)
|
|
|
|
|
// Set initial layout for all array layers (faces) of the optimal (target) tiled texture
|
2016-03-13 14:03:45 +01:00
|
|
|
VkImageSubresourceRange subresourceRange = {};
|
|
|
|
|
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
subresourceRange.baseMipLevel = 0;
|
2016-06-04 11:31:22 +02:00
|
|
|
subresourceRange.levelCount = cubeMap.mipLevels;
|
2016-03-13 14:03:45 +01:00
|
|
|
subresourceRange.layerCount = 6;
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkTools::setImageLayout(
|
2016-05-14 11:46:56 +02:00
|
|
|
copyCmd,
|
2016-02-16 15:07:25 +01:00
|
|
|
cubeMap.image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
2016-06-22 20:30:14 +02:00
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2016-03-13 14:03:45 +01:00
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
|
subresourceRange);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
// Copy the cube map faces from the staging buffer to the optimal tiled image
|
|
|
|
|
vkCmdCopyBufferToImage(
|
|
|
|
|
copyCmd,
|
|
|
|
|
stagingBuffer,
|
|
|
|
|
cubeMap.image,
|
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
2016-06-04 11:31:22 +02:00
|
|
|
static_cast<uint32_t>(bufferCopyRegions.size()),
|
|
|
|
|
bufferCopyRegions.data()
|
2016-05-14 11:46:56 +02:00
|
|
|
);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-03-13 14:03:45 +01:00
|
|
|
// Change texture image layout to shader read after all faces have been copied
|
|
|
|
|
cubeMap.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
vkTools::setImageLayout(
|
2016-05-14 11:46:56 +02:00
|
|
|
copyCmd,
|
2016-03-13 14:03:45 +01:00
|
|
|
cubeMap.image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
|
cubeMap.imageLayout,
|
|
|
|
|
subresourceRange);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create sampler
|
|
|
|
|
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
|
|
|
|
|
sampler.magFilter = VK_FILTER_LINEAR;
|
|
|
|
|
sampler.minFilter = VK_FILTER_LINEAR;
|
|
|
|
|
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
|
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
|
|
|
sampler.addressModeV = sampler.addressModeU;
|
|
|
|
|
sampler.addressModeW = sampler.addressModeU;
|
|
|
|
|
sampler.mipLodBias = 0.0f;
|
|
|
|
|
sampler.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
sampler.minLod = 0.0f;
|
2016-06-04 11:31:22 +02:00
|
|
|
sampler.maxLod = cubeMap.mipLevels;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
2016-09-17 10:31:04 +02:00
|
|
|
sampler.maxAnisotropy = 1.0f;
|
|
|
|
|
if (vulkanDevice->features.samplerAnisotropy)
|
|
|
|
|
{
|
|
|
|
|
sampler.maxAnisotropy = vulkanDevice->properties.limits.maxSamplerAnisotropy;
|
|
|
|
|
sampler.anisotropyEnable = VK_TRUE;
|
|
|
|
|
}
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &cubeMap.sampler));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create image view
|
|
|
|
|
VkImageViewCreateInfo view = vkTools::initializers::imageViewCreateInfo();
|
2016-05-14 11:46:56 +02:00
|
|
|
// Cube map view type
|
2016-02-16 15:07:25 +01:00
|
|
|
view.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
|
|
|
|
|
view.format = format;
|
|
|
|
|
view.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
|
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
2016-05-14 11:46:56 +02:00
|
|
|
// 6 array layers (faces)
|
2016-02-16 15:07:25 +01:00
|
|
|
view.subresourceRange.layerCount = 6;
|
2016-06-04 11:31:22 +02:00
|
|
|
// Set number of mip levels
|
|
|
|
|
view.subresourceRange.levelCount = cubeMap.mipLevels;
|
2016-02-16 15:07:25 +01:00
|
|
|
view.image = cubeMap.image;
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &cubeMap.view));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
// Clean up staging resources
|
|
|
|
|
vkFreeMemory(device, stagingMemory, nullptr);
|
|
|
|
|
vkDestroyBuffer(device, stagingBuffer, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
void reBuildCommandBuffers()
|
|
|
|
|
{
|
|
|
|
|
if (!checkCommandBuffers())
|
|
|
|
|
{
|
|
|
|
|
destroyCommandBuffers();
|
|
|
|
|
createCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void buildCommandBuffers()
|
|
|
|
|
{
|
|
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
|
|
|
|
|
|
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[0].color = defaultClearColor;
|
|
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
|
|
|
|
renderPassBeginInfo.renderPass = renderPass;
|
|
|
|
|
renderPassBeginInfo.renderArea.offset.x = 0;
|
|
|
|
|
renderPassBeginInfo.renderArea.offset.y = 0;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.width = width;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = height;
|
|
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
|
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
// Set target frame buffer
|
|
|
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
|
|
|
|
|
|
|
|
// Skybox
|
2016-06-04 11:03:17 +02:00
|
|
|
if (displaySkybox)
|
|
|
|
|
{
|
|
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.skybox, 0, NULL);
|
|
|
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.skybox.vertices.buf, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.skybox.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.skybox);
|
|
|
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.skybox.indexCount, 1, 0, 0, 0);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 3D object
|
|
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.object, 0, NULL);
|
2016-06-04 11:03:17 +02:00
|
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.objects[meshes.objectIndex].vertices.buf, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.objects[meshes.objectIndex].indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.reflect);
|
2016-06-04 11:03:17 +02:00
|
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.objects[meshes.objectIndex].indexCount, 1, 0, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadMeshes()
|
|
|
|
|
{
|
2016-06-04 11:03:17 +02:00
|
|
|
// Skybox
|
2016-03-25 15:29:38 +01:00
|
|
|
loadMesh(getAssetPath() + "models/cube.obj", &meshes.skybox, vertexLayout, 0.05f);
|
2016-06-04 11:03:17 +02:00
|
|
|
// Objects
|
|
|
|
|
meshes.objects.resize(3);
|
|
|
|
|
loadMesh(getAssetPath() + "models/sphere.obj", &meshes.objects[0], vertexLayout, 0.05f);
|
|
|
|
|
loadMesh(getAssetPath() + "models/teapot.dae", &meshes.objects[1], vertexLayout, 0.05f);
|
|
|
|
|
loadMesh(getAssetPath() + "models/torusknot.obj", &meshes.objects[2], vertexLayout, 0.05f);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupVertexDescriptions()
|
|
|
|
|
{
|
|
|
|
|
// Binding description
|
|
|
|
|
vertices.bindingDescriptions.resize(1);
|
|
|
|
|
vertices.bindingDescriptions[0] =
|
|
|
|
|
vkTools::initializers::vertexInputBindingDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
vkMeshLoader::vertexSize(vertexLayout),
|
|
|
|
|
VK_VERTEX_INPUT_RATE_VERTEX);
|
|
|
|
|
|
|
|
|
|
// Attribute descriptions
|
|
|
|
|
// Describes memory layout and shader positions
|
|
|
|
|
vertices.attributeDescriptions.resize(3);
|
|
|
|
|
// Location 0 : Position
|
|
|
|
|
vertices.attributeDescriptions[0] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
0,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
0);
|
|
|
|
|
// Location 1 : Normal
|
|
|
|
|
vertices.attributeDescriptions[1] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
1,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
sizeof(float) * 3);
|
|
|
|
|
// Location 2 : Texture coordinates
|
|
|
|
|
vertices.attributeDescriptions[2] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
2,
|
|
|
|
|
VK_FORMAT_R32G32_SFLOAT,
|
|
|
|
|
sizeof(float) * 5);
|
|
|
|
|
|
|
|
|
|
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
|
|
|
|
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
|
|
|
|
|
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
|
|
|
|
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size();
|
|
|
|
|
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
|
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes =
|
|
|
|
|
{
|
|
|
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
|
|
|
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
|
|
|
vkTools::initializers::descriptorPoolCreateInfo(
|
|
|
|
|
poolSizes.size(),
|
|
|
|
|
poolSizes.data(),
|
|
|
|
|
2);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSetLayout()
|
|
|
|
|
{
|
|
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
|
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
0),
|
|
|
|
|
// Binding 1 : Fragment shader image sampler
|
|
|
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
|
1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
|
|
|
|
vkTools::initializers::descriptorSetLayoutCreateInfo(
|
|
|
|
|
setLayoutBindings.data(),
|
|
|
|
|
setLayoutBindings.size());
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
|
|
|
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
|
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSets()
|
|
|
|
|
{
|
|
|
|
|
// Image descriptor for the cube map texture
|
|
|
|
|
VkDescriptorImageInfo cubeMapDescriptor =
|
|
|
|
|
vkTools::initializers::descriptorImageInfo(
|
|
|
|
|
cubeMap.sampler,
|
|
|
|
|
cubeMap.view,
|
|
|
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
|
|
|
|
vkTools::initializers::descriptorSetAllocateInfo(
|
|
|
|
|
descriptorPool,
|
|
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
// 3D object descriptor set
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.object));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
|
|
|
vkTools::initializers::writeDescriptorSet(
|
|
|
|
|
descriptorSets.object,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
0,
|
|
|
|
|
&uniformData.objectVS.descriptor),
|
|
|
|
|
// Binding 1 : Fragment shader cubemap sampler
|
|
|
|
|
vkTools::initializers::writeDescriptorSet(
|
|
|
|
|
descriptorSets.object,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
1,
|
|
|
|
|
&cubeMapDescriptor)
|
|
|
|
|
};
|
|
|
|
|
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
|
|
|
|
|
|
// Sky box descriptor set
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.skybox));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
writeDescriptorSets =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
|
|
|
vkTools::initializers::writeDescriptorSet(
|
|
|
|
|
descriptorSets.skybox,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
0,
|
|
|
|
|
&uniformData.skyboxVS.descriptor),
|
|
|
|
|
// Binding 1 : Fragment shader cubemap sampler
|
|
|
|
|
vkTools::initializers::writeDescriptorSet(
|
|
|
|
|
descriptorSets.skybox,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
1,
|
|
|
|
|
&cubeMapDescriptor)
|
|
|
|
|
};
|
|
|
|
|
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
|
|
|
|
|
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
|
|
|
|
|
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
|
|
|
|
0,
|
|
|
|
|
VK_FALSE);
|
|
|
|
|
|
|
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState =
|
|
|
|
|
vkTools::initializers::pipelineRasterizationStateCreateInfo(
|
|
|
|
|
VK_POLYGON_MODE_FILL,
|
2016-06-04 11:03:17 +02:00
|
|
|
VK_CULL_MODE_BACK_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState =
|
|
|
|
|
vkTools::initializers::pipelineColorBlendAttachmentState(
|
|
|
|
|
0xf,
|
|
|
|
|
VK_FALSE);
|
|
|
|
|
|
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState =
|
|
|
|
|
vkTools::initializers::pipelineColorBlendStateCreateInfo(
|
|
|
|
|
1,
|
|
|
|
|
&blendAttachmentState);
|
|
|
|
|
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState =
|
|
|
|
|
vkTools::initializers::pipelineDepthStencilStateCreateInfo(
|
2016-06-04 11:03:17 +02:00
|
|
|
VK_FALSE,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_FALSE,
|
|
|
|
|
VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
|
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState =
|
|
|
|
|
vkTools::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
|
|
|
|
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState =
|
|
|
|
|
vkTools::initializers::pipelineMultisampleStateCreateInfo(
|
|
|
|
|
VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = {
|
|
|
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
|
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
|
|
|
};
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState =
|
|
|
|
|
vkTools::initializers::pipelineDynamicStateCreateInfo(
|
|
|
|
|
dynamicStateEnables.data(),
|
|
|
|
|
dynamicStateEnables.size(),
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
// Skybox pipeline (background cube)
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
|
|
|
|
|
|
2016-03-25 15:29:38 +01:00
|
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/cubemap/skybox.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/cubemap/skybox.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
|
|
|
|
vkTools::initializers::pipelineCreateInfo(
|
|
|
|
|
pipelineLayout,
|
|
|
|
|
renderPass,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
|
|
|
|
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCreateInfo.pViewportState = &viewportState;
|
|
|
|
|
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCreateInfo.pDynamicState = &dynamicState;
|
|
|
|
|
pipelineCreateInfo.stageCount = shaderStages.size();
|
|
|
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.skybox));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Cube map reflect pipeline
|
2016-03-25 15:29:38 +01:00
|
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/cubemap/reflect.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/cubemap/reflect.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2016-06-04 11:03:17 +02:00
|
|
|
// Enable depth test and write
|
2016-02-16 15:07:25 +01:00
|
|
|
depthStencilState.depthWriteEnable = VK_TRUE;
|
2016-06-04 11:03:17 +02:00
|
|
|
depthStencilState.depthTestEnable = VK_TRUE;
|
|
|
|
|
// Flip cull mode
|
|
|
|
|
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.reflect));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// 3D objact
|
|
|
|
|
createBuffer(
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-06-04 11:03:17 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
sizeof(uboVS),
|
2016-06-04 11:03:17 +02:00
|
|
|
nullptr,
|
2016-02-16 15:07:25 +01:00
|
|
|
&uniformData.objectVS.buffer,
|
|
|
|
|
&uniformData.objectVS.memory,
|
|
|
|
|
&uniformData.objectVS.descriptor);
|
|
|
|
|
|
|
|
|
|
// Skybox
|
|
|
|
|
createBuffer(
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-06-04 11:03:17 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
sizeof(uboVS),
|
2016-06-04 11:03:17 +02:00
|
|
|
nullptr,
|
2016-02-16 15:07:25 +01:00
|
|
|
&uniformData.skyboxVS.buffer,
|
|
|
|
|
&uniformData.skyboxVS.memory,
|
|
|
|
|
&uniformData.skyboxVS.descriptor);
|
2016-06-04 11:03:17 +02:00
|
|
|
|
|
|
|
|
updateUniformBuffers();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// 3D object
|
|
|
|
|
glm::mat4 viewMatrix = glm::mat4();
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));
|
|
|
|
|
|
|
|
|
|
uboVS.model = glm::mat4();
|
2016-06-04 11:03:17 +02:00
|
|
|
uboVS.model = viewMatrix * glm::translate(uboVS.model, cameraPos);
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
uint8_t *pData;
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.objectVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
2016-02-16 15:07:25 +01:00
|
|
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
|
|
|
|
vkUnmapMemory(device, uniformData.objectVS.memory);
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
// Skybox
|
2016-02-16 15:07:25 +01:00
|
|
|
viewMatrix = glm::mat4();
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
uboVS.model = glm::mat4();
|
|
|
|
|
uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 11:46:56 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.skyboxVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
2016-02-16 15:07:25 +01:00
|
|
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
|
|
|
|
vkUnmapMemory(device, uniformData.skyboxVS.memory);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
|
|
|
|
loadMeshes();
|
|
|
|
|
setupVertexDescriptions();
|
|
|
|
|
prepareUniformBuffers();
|
2016-03-25 15:29:38 +01:00
|
|
|
loadCubemap(
|
|
|
|
|
getAssetPath() + "textures/cubemap_yokohama.ktx",
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_FORMAT_BC3_UNORM_BLOCK,
|
|
|
|
|
false);
|
|
|
|
|
setupDescriptorSetLayout();
|
|
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSets();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
2016-06-04 11:03:17 +02:00
|
|
|
|
|
|
|
|
void toggleSkyBox()
|
|
|
|
|
{
|
|
|
|
|
displaySkybox = !displaySkybox;
|
|
|
|
|
reBuildCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void toggleObject()
|
|
|
|
|
{
|
|
|
|
|
meshes.objectIndex++;
|
|
|
|
|
if (meshes.objectIndex >= static_cast<uint32_t>(meshes.objects.size()))
|
|
|
|
|
{
|
|
|
|
|
meshes.objectIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
reBuildCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 13:25:40 +02:00
|
|
|
void changeLodBias(float delta)
|
|
|
|
|
{
|
|
|
|
|
uboVS.lodBias += delta;
|
|
|
|
|
if (uboVS.lodBias < 0.0f)
|
|
|
|
|
{
|
|
|
|
|
uboVS.lodBias = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
if (uboVS.lodBias > cubeMap.mipLevels)
|
|
|
|
|
{
|
|
|
|
|
uboVS.lodBias = cubeMap.mipLevels;
|
|
|
|
|
}
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
updateTextOverlay();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 11:03:17 +02:00
|
|
|
virtual void keyPressed(uint32_t keyCode)
|
|
|
|
|
{
|
|
|
|
|
switch (keyCode)
|
|
|
|
|
{
|
2016-08-11 13:15:49 +02:00
|
|
|
case KEY_S:
|
2016-06-04 11:03:17 +02:00
|
|
|
case GAMEPAD_BUTTON_A:
|
|
|
|
|
toggleSkyBox();
|
|
|
|
|
break;
|
2016-08-11 13:15:49 +02:00
|
|
|
case KEY_SPACE:
|
2016-06-04 11:03:17 +02:00
|
|
|
case GAMEPAD_BUTTON_X:
|
|
|
|
|
toggleObject();
|
|
|
|
|
break;
|
2016-08-11 13:15:49 +02:00
|
|
|
case KEY_KPADD:
|
2016-06-04 13:25:40 +02:00
|
|
|
case GAMEPAD_BUTTON_R1:
|
|
|
|
|
changeLodBias(0.1f);
|
|
|
|
|
break;
|
2016-08-11 13:15:49 +02:00
|
|
|
case KEY_KPSUB:
|
2016-06-04 13:25:40 +02:00
|
|
|
case GAMEPAD_BUTTON_L1:
|
|
|
|
|
changeLodBias(-0.1f);
|
|
|
|
|
break;
|
2016-06-04 11:03:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
|
|
|
|
|
{
|
2016-06-04 13:25:40 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << std::setprecision(2) << std::fixed << uboVS.lodBias;
|
2016-06-04 11:03:17 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
textOverlay->addText("Press \"Button A\" to toggle skybox", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
|
|
|
|
textOverlay->addText("Press \"Button X\" to toggle object", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
|
2016-06-04 13:25:40 +02:00
|
|
|
textOverlay->addText("LOD bias: " + ss.str() + " (Buttons L1/R1 to change)", 5.0f, 115.0f, VulkanTextOverlay::alignLeft);
|
2016-06-04 11:03:17 +02:00
|
|
|
#else
|
|
|
|
|
textOverlay->addText("Press \"s\" to toggle skybox", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
|
|
|
|
textOverlay->addText("Press \"space\" to toggle object", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
|
2016-06-04 13:25:40 +02:00
|
|
|
textOverlay->addText("LOD bias: " + ss.str() + " (numpad +/- to change)", 5.0f, 115.0f, VulkanTextOverlay::alignLeft);
|
2016-06-04 11:03:17 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VulkanExample *vulkanExample;
|
|
|
|
|
|
2016-03-25 15:29:38 +01:00
|
|
|
#if defined(_WIN32)
|
2016-02-16 15:07:25 +01:00
|
|
|
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
|
{
|
|
|
|
|
if (vulkanExample != NULL)
|
|
|
|
|
{
|
|
|
|
|
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
|
|
|
|
}
|
|
|
|
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
|
|
|
}
|
2016-11-04 13:32:58 -07:00
|
|
|
#elif defined(__linux__) && !defined(__ANDROID__) && !defined(_DIRECT2DISPLAY)
|
2016-02-16 15:07:25 +01:00
|
|
|
static void handleEvent(const xcb_generic_event_t *event)
|
|
|
|
|
{
|
|
|
|
|
if (vulkanExample != NULL)
|
|
|
|
|
{
|
|
|
|
|
vulkanExample->handleEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-03-25 15:29:38 +01:00
|
|
|
// Main entry point
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
// Windows entry point
|
2016-02-16 15:07:25 +01:00
|
|
|
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
|
2016-03-25 15:29:38 +01:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
|
// Android entry point
|
|
|
|
|
void android_main(android_app* state)
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
// Linux entry point
|
2016-02-16 15:07:25 +01:00
|
|
|
int main(const int argc, const char *argv[])
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2016-03-25 15:29:38 +01:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
// Removing this may cause the compiler to omit the main entry point
|
|
|
|
|
// which would make the application crash at start
|
|
|
|
|
app_dummy();
|
|
|
|
|
#endif
|
2016-02-16 15:07:25 +01:00
|
|
|
vulkanExample = new VulkanExample();
|
2016-03-25 15:29:38 +01:00
|
|
|
#if defined(_WIN32)
|
2016-02-16 15:07:25 +01:00
|
|
|
vulkanExample->setupWindow(hInstance, WndProc);
|
2016-03-25 15:29:38 +01:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
|
// Attach vulkan example to global android application state
|
|
|
|
|
state->userData = vulkanExample;
|
|
|
|
|
state->onAppCmd = VulkanExample::handleAppCommand;
|
|
|
|
|
state->onInputEvent = VulkanExample::handleAppInput;
|
|
|
|
|
vulkanExample->androidApp = state;
|
2016-11-04 13:32:58 -07:00
|
|
|
#elif defined(__linux__) && !defined(_DIRECT2DISPLAY)
|
2016-02-16 15:07:25 +01:00
|
|
|
vulkanExample->setupWindow();
|
|
|
|
|
#endif
|
2016-03-25 15:29:38 +01:00
|
|
|
#if !defined(__ANDROID__)
|
2016-02-16 15:07:25 +01:00
|
|
|
vulkanExample->initSwapchain();
|
|
|
|
|
vulkanExample->prepare();
|
2016-03-25 15:29:38 +01:00
|
|
|
#endif
|
2016-02-16 15:07:25 +01:00
|
|
|
vulkanExample->renderLoop();
|
|
|
|
|
delete(vulkanExample);
|
2016-03-26 13:21:19 +01:00
|
|
|
#if !defined(__ANDROID__)
|
2016-02-16 15:07:25 +01:00
|
|
|
return 0;
|
2016-03-25 15:29:38 +01:00
|
|
|
#endif
|
2016-08-11 13:15:49 +02:00
|
|
|
}
|