2016-06-18 19:24:37 +02:00
|
|
|
/*
|
|
|
|
|
* Vulkan Example - Dynamic terrain tessellation
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
|
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vulkanexamplebase.h"
|
2020-07-28 20:20:38 +02:00
|
|
|
#include "VulkanglTFModel.h"
|
2016-06-23 22:01:48 +02:00
|
|
|
#include "frustum.hpp"
|
2019-08-03 10:39:39 +02:00
|
|
|
#include <ktx.h>
|
|
|
|
|
#include <ktxvulkan.h>
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
2017-02-11 14:18:24 +01:00
|
|
|
public:
|
|
|
|
|
bool wireframe = false;
|
|
|
|
|
bool tessellation = true;
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
// Holds the buffers for rendering the tessellated terrain
|
|
|
|
|
struct {
|
|
|
|
|
struct Vertices {
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
} vertices;
|
|
|
|
|
struct Indices {
|
|
|
|
|
int count;
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
} indices;
|
|
|
|
|
} terrain;
|
|
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
struct {
|
2017-02-09 21:55:35 +01:00
|
|
|
vks::Texture2D heightMap;
|
|
|
|
|
vks::Texture2D skySphere;
|
|
|
|
|
vks::Texture2DArray terrainArray;
|
2016-06-18 19:24:37 +02:00
|
|
|
} textures;
|
2017-02-11 14:18:24 +01:00
|
|
|
|
|
|
|
|
struct {
|
2020-07-28 20:20:38 +02:00
|
|
|
vkglTF::Model skysphere;
|
2017-02-11 14:18:24 +01:00
|
|
|
} models;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
struct {
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer terrainTessellation;
|
|
|
|
|
vks::Buffer skysphereVertex;
|
2016-12-24 12:48:01 +01:00
|
|
|
} uniformBuffers;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Shared values for tessellation control and evaluation stages
|
|
|
|
|
struct {
|
|
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 modelview;
|
2016-06-25 13:30:55 +02:00
|
|
|
glm::vec4 lightPos = glm::vec4(-48.0f, -40.0f, 46.0f, 0.0f);
|
2016-06-23 22:01:48 +02:00
|
|
|
glm::vec4 frustumPlanes[6];
|
|
|
|
|
float displacementFactor = 32.0f;
|
2016-06-24 20:59:56 +02:00
|
|
|
float tessellationFactor = 0.75f;
|
2016-06-18 19:24:37 +02:00
|
|
|
glm::vec2 viewportDim;
|
|
|
|
|
// Desired size of tessellated quad patch edge
|
|
|
|
|
float tessellatedEdgeSize = 20.0f;
|
|
|
|
|
} uboTess;
|
|
|
|
|
|
|
|
|
|
// Skysphere vertex shader stage
|
|
|
|
|
struct {
|
|
|
|
|
glm::mat4 mvp;
|
|
|
|
|
} uboVS;
|
|
|
|
|
|
2017-03-17 20:58:43 +01:00
|
|
|
struct Pipelines {
|
2016-06-18 19:24:37 +02:00
|
|
|
VkPipeline terrain;
|
2017-03-17 20:58:43 +01:00
|
|
|
VkPipeline wireframe = VK_NULL_HANDLE;
|
2016-06-18 19:24:37 +02:00
|
|
|
VkPipeline skysphere;
|
|
|
|
|
} pipelines;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkDescriptorSetLayout terrain;
|
|
|
|
|
VkDescriptorSetLayout skysphere;
|
|
|
|
|
} descriptorSetLayouts;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkPipelineLayout terrain;
|
|
|
|
|
VkPipelineLayout skysphere;
|
|
|
|
|
} pipelineLayouts;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkDescriptorSet terrain;
|
|
|
|
|
VkDescriptorSet skysphere;
|
|
|
|
|
} descriptorSets;
|
|
|
|
|
|
2016-06-23 22:01:48 +02:00
|
|
|
// Pipeline statistics
|
|
|
|
|
struct {
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
} queryResult;
|
2017-03-17 20:58:43 +01:00
|
|
|
VkQueryPool queryPool = VK_NULL_HANDLE;
|
2016-06-23 22:01:48 +02:00
|
|
|
uint64_t pipelineStats[2] = { 0 };
|
|
|
|
|
|
|
|
|
|
// View frustum passed to tessellation control shader for culling
|
2017-02-12 13:37:12 +01:00
|
|
|
vks::Frustum frustum;
|
2016-06-23 22:01:48 +02:00
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
title = "Dynamic terrain tessellation";
|
2016-06-20 20:17:46 +02:00
|
|
|
camera.type = Camera::CameraType::firstperson;
|
2016-06-18 19:24:37 +02:00
|
|
|
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f);
|
2016-06-25 21:23:12 +02:00
|
|
|
camera.setRotation(glm::vec3(-12.0f, 159.0f, 0.0f));
|
|
|
|
|
camera.setTranslation(glm::vec3(18.0f, 22.5f, 57.5f));
|
2016-06-20 20:17:46 +02:00
|
|
|
camera.movementSpeed = 7.5f;
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2020-05-29 16:08:53 +01:00
|
|
|
// Clean up used Vulkan resources
|
2016-06-18 19:24:37 +02:00
|
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
|
vkDestroyPipeline(device, pipelines.terrain, nullptr);
|
2017-03-17 20:58:43 +01:00
|
|
|
if (pipelines.wireframe != VK_NULL_HANDLE) {
|
|
|
|
|
vkDestroyPipeline(device, pipelines.wireframe, nullptr);
|
|
|
|
|
}
|
2016-06-25 17:58:27 +02:00
|
|
|
vkDestroyPipeline(device, pipelines.skysphere, nullptr);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.skysphere, nullptr);
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.terrain, nullptr);
|
|
|
|
|
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.terrain, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.skysphere, nullptr);
|
|
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
uniformBuffers.skysphereVertex.destroy();
|
|
|
|
|
uniformBuffers.terrainTessellation.destroy();
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-02-09 21:55:35 +01:00
|
|
|
textures.heightMap.destroy();
|
|
|
|
|
textures.skySphere.destroy();
|
|
|
|
|
textures.terrainArray.destroy();
|
2016-06-23 22:01:48 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
vkDestroyBuffer(device, terrain.vertices.buffer, nullptr);
|
|
|
|
|
vkFreeMemory(device, terrain.vertices.memory, nullptr);
|
|
|
|
|
vkDestroyBuffer(device, terrain.indices.buffer, nullptr);
|
|
|
|
|
vkFreeMemory(device, terrain.indices.memory, nullptr);
|
|
|
|
|
|
2017-03-17 20:58:43 +01:00
|
|
|
if (queryPool != VK_NULL_HANDLE) {
|
|
|
|
|
vkDestroyQueryPool(device, queryPool, nullptr);
|
|
|
|
|
vkDestroyBuffer(device, queryResult.buffer, nullptr);
|
|
|
|
|
vkFreeMemory(device, queryResult.memory, nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
|
2020-05-29 16:08:53 +01:00
|
|
|
// Enable physical device features required for this example
|
2017-03-17 20:58:43 +01:00
|
|
|
virtual void getEnabledFeatures()
|
|
|
|
|
{
|
|
|
|
|
// Tessellation shader support is required for this example
|
|
|
|
|
if (deviceFeatures.tessellationShader) {
|
|
|
|
|
enabledFeatures.tessellationShader = VK_TRUE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-01-21 18:28:17 +01:00
|
|
|
vks::tools::exitFatal("Selected GPU does not support tessellation shaders!", VK_ERROR_FEATURE_NOT_PRESENT);
|
2017-03-17 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
// Fill mode non solid is required for wireframe display
|
|
|
|
|
if (deviceFeatures.fillModeNonSolid) {
|
|
|
|
|
enabledFeatures.fillModeNonSolid = VK_TRUE;
|
|
|
|
|
};
|
|
|
|
|
// Pipeline statistics
|
|
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
enabledFeatures.pipelineStatisticsQuery = VK_TRUE;
|
|
|
|
|
};
|
2017-10-21 15:58:10 +02:00
|
|
|
// Enable anisotropic filtering if supported
|
|
|
|
|
if (deviceFeatures.samplerAnisotropy) {
|
|
|
|
|
enabledFeatures.samplerAnisotropy = VK_TRUE;
|
|
|
|
|
}
|
2020-05-29 16:08:53 +01:00
|
|
|
// Enable texture compression
|
2017-10-21 15:58:10 +02:00
|
|
|
if (deviceFeatures.textureCompressionBC) {
|
|
|
|
|
enabledFeatures.textureCompressionBC = VK_TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (deviceFeatures.textureCompressionASTC_LDR) {
|
|
|
|
|
enabledFeatures.textureCompressionASTC_LDR = VK_TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (deviceFeatures.textureCompressionETC2) {
|
|
|
|
|
enabledFeatures.textureCompressionETC2 = VK_TRUE;
|
|
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup pool and buffer for storing pipeline statistics results
|
|
|
|
|
void setupQueryResultBuffer()
|
|
|
|
|
{
|
|
|
|
|
uint32_t bufSize = 2 * sizeof(uint64_t);
|
|
|
|
|
|
|
|
|
|
VkMemoryRequirements memReqs;
|
2017-02-12 11:12:42 +01:00
|
|
|
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
2016-06-23 22:01:48 +02:00
|
|
|
VkBufferCreateInfo bufferCreateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::bufferCreateInfo(
|
2016-06-23 22:01:48 +02:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
|
|
|
bufSize);
|
|
|
|
|
|
|
|
|
|
// Results are saved in a host visible buffer for easy access by the application
|
|
|
|
|
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &queryResult.buffer));
|
|
|
|
|
vkGetBufferMemoryRequirements(device, queryResult.buffer, &memReqs);
|
|
|
|
|
memAlloc.allocationSize = memReqs.size;
|
2017-03-04 21:55:20 +01:00
|
|
|
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2016-06-23 22:01:48 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &queryResult.memory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindBufferMemory(device, queryResult.buffer, queryResult.memory, 0));
|
|
|
|
|
|
|
|
|
|
// Create query pool
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
VkQueryPoolCreateInfo queryPoolInfo = {};
|
|
|
|
|
queryPoolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
|
|
|
|
|
queryPoolInfo.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS;
|
|
|
|
|
queryPoolInfo.pipelineStatistics =
|
|
|
|
|
VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT |
|
|
|
|
|
VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT;
|
|
|
|
|
queryPoolInfo.queryCount = 2;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateQueryPool(device, &queryPoolInfo, NULL, &queryPool));
|
|
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieves the results of the pipeline statistics query submitted to the command buffer
|
|
|
|
|
void getQueryResults()
|
|
|
|
|
{
|
|
|
|
|
// We use vkGetQueryResults to copy the results into a host visible buffer
|
|
|
|
|
vkGetQueryPoolResults(
|
|
|
|
|
device,
|
|
|
|
|
queryPool,
|
|
|
|
|
0,
|
|
|
|
|
1,
|
|
|
|
|
sizeof(pipelineStats),
|
|
|
|
|
pipelineStats,
|
|
|
|
|
sizeof(uint64_t),
|
|
|
|
|
VK_QUERY_RESULT_64_BIT);
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-09 21:55:35 +01:00
|
|
|
void loadAssets()
|
2016-06-18 19:24:37 +02:00
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
|
|
|
|
models.skysphere.loadFromFile(getAssetPath() + "models/sphere.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
2017-03-17 20:58:43 +01:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
textures.skySphere.loadFromFile(getAssetPath() + "textures/skysphere_rgba.ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue);
|
2017-03-17 20:58:43 +01:00
|
|
|
// Terrain textures are stored in a texture array with layers corresponding to terrain height
|
2020-07-28 20:20:38 +02:00
|
|
|
textures.terrainArray.loadFromFile(getAssetPath() + "textures/terrain_texturearray_rgba.ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue);
|
2017-03-17 20:58:43 +01:00
|
|
|
|
2016-06-19 13:35:09 +02:00
|
|
|
// Height data is stored in a one-channel texture
|
2017-02-09 21:55:35 +01:00
|
|
|
textures.heightMap.loadFromFile(getAssetPath() + "textures/terrain_heightmap_r16.ktx", VK_FORMAT_R16_UNORM, vulkanDevice, queue);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSamplerCreateInfo samplerInfo = vks::initializers::samplerCreateInfo();
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Setup a mirroring sampler for the height map
|
|
|
|
|
vkDestroySampler(device, textures.heightMap.sampler, nullptr);
|
|
|
|
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
|
|
|
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
|
|
|
|
|
samplerInfo.addressModeV = samplerInfo.addressModeU;
|
|
|
|
|
samplerInfo.addressModeW = samplerInfo.addressModeU;
|
|
|
|
|
samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
samplerInfo.minLod = 0.0f;
|
|
|
|
|
samplerInfo.maxLod = (float)textures.heightMap.mipLevels;
|
|
|
|
|
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &samplerInfo, nullptr, &textures.heightMap.sampler));
|
|
|
|
|
textures.heightMap.descriptor.sampler = textures.heightMap.sampler;
|
|
|
|
|
|
|
|
|
|
// Setup a repeating sampler for the terrain texture layers
|
|
|
|
|
vkDestroySampler(device, textures.terrainArray.sampler, nullptr);
|
2017-02-12 11:12:42 +01:00
|
|
|
samplerInfo = vks::initializers::samplerCreateInfo();
|
2016-06-18 19:24:37 +02:00
|
|
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
|
|
|
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
|
samplerInfo.addressModeV = samplerInfo.addressModeU;
|
|
|
|
|
samplerInfo.addressModeW = samplerInfo.addressModeU;
|
|
|
|
|
samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
samplerInfo.minLod = 0.0f;
|
|
|
|
|
samplerInfo.maxLod = (float)textures.terrainArray.mipLevels;
|
|
|
|
|
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
|
|
|
|
if (deviceFeatures.samplerAnisotropy)
|
|
|
|
|
{
|
|
|
|
|
samplerInfo.maxAnisotropy = 4.0f;
|
|
|
|
|
samplerInfo.anisotropyEnable = VK_TRUE;
|
|
|
|
|
}
|
|
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &samplerInfo, nullptr, &textures.terrainArray.sampler));
|
|
|
|
|
textures.terrainArray.descriptor.sampler = textures.terrainArray.sampler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void buildCommandBuffers()
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[0].color = defaultClearColor;
|
|
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
2016-06-18 19:24:37 +02:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
|
|
|
|
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
vkCmdResetQueryPool(drawCmdBuffers[i], queryPool, 0, 2);
|
|
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
2016-06-18 19:24:37 +02:00
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
2016-06-18 19:24:37 +02:00
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f);
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
|
|
|
|
|
|
|
|
// Skysphere
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.skysphere);
|
2020-07-28 20:20:38 +02:00
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.skysphere, 0, 1, &descriptorSets.skysphere, 0, nullptr);
|
|
|
|
|
models.skysphere.draw(drawCmdBuffers[i]);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
// Tessellated terrain
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
2020-05-29 16:08:53 +01:00
|
|
|
// Begin pipeline statistics query
|
2019-09-11 20:49:31 +02:00
|
|
|
vkCmdBeginQuery(drawCmdBuffers[i], queryPool, 0, 0);
|
2017-03-17 20:58:43 +01:00
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
// Render
|
2016-06-18 19:24:37 +02:00
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, wireframe ? pipelines.wireframe : pipelines.terrain);
|
2020-07-28 20:20:38 +02:00
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.terrain, 0, 1, &descriptorSets.terrain, 0, nullptr);
|
|
|
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &terrain.vertices.buffer, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], terrain.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], terrain.indices.count, 1, 0, 0, 0);
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
// End pipeline statistics query
|
|
|
|
|
vkCmdEndQuery(drawCmdBuffers[i], queryPool, 0);
|
|
|
|
|
}
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2018-08-30 21:08:02 +02:00
|
|
|
drawUI(drawCmdBuffers[i]);
|
|
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:30:55 +02:00
|
|
|
// Encapsulate height map data for easy sampling
|
|
|
|
|
struct HeightMap
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
uint16_t *heightdata;
|
|
|
|
|
uint32_t dim;
|
|
|
|
|
uint32_t scale;
|
|
|
|
|
public:
|
2016-06-25 22:49:22 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
HeightMap(std::string filename, uint32_t patchsize, AAssetManager* assetManager)
|
|
|
|
|
#else
|
2016-06-25 13:30:55 +02:00
|
|
|
HeightMap(std::string filename, uint32_t patchsize)
|
2020-05-29 16:08:53 +01:00
|
|
|
#endif
|
2016-06-25 13:30:55 +02:00
|
|
|
{
|
2019-08-03 10:39:39 +02:00
|
|
|
ktxResult result;
|
|
|
|
|
ktxTexture* ktxTexture;
|
2016-06-25 22:49:22 +02:00
|
|
|
#if defined(__ANDROID__)
|
2019-08-03 10:39:39 +02:00
|
|
|
AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
|
2016-06-25 22:49:22 +02:00
|
|
|
assert(asset);
|
|
|
|
|
size_t size = AAsset_getLength(asset);
|
|
|
|
|
assert(size > 0);
|
2019-12-08 08:58:35 +01:00
|
|
|
ktx_uint8_t* textureData = new ktx_uint8_t[size];
|
2016-06-25 22:49:22 +02:00
|
|
|
AAsset_read(asset, textureData, size);
|
|
|
|
|
AAsset_close(asset);
|
2019-12-08 08:58:35 +01:00
|
|
|
result = ktxTexture_CreateFromMemory(textureData, size, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &ktxTexture);
|
|
|
|
|
delete[] textureData;
|
2019-08-03 10:39:39 +02:00
|
|
|
|
2016-06-25 22:49:22 +02:00
|
|
|
#else
|
2019-08-03 10:39:39 +02:00
|
|
|
result = ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &ktxTexture);
|
2016-06-25 22:49:22 +02:00
|
|
|
#endif
|
2019-08-03 10:39:39 +02:00
|
|
|
assert(result == KTX_SUCCESS);
|
|
|
|
|
ktx_size_t ktxSize = ktxTexture_GetImageSize(ktxTexture, 0);
|
|
|
|
|
ktx_uint8_t* ktxImage = ktxTexture_GetData(ktxTexture);
|
|
|
|
|
dim = ktxTexture->baseWidth;
|
2016-06-25 13:30:55 +02:00
|
|
|
heightdata = new uint16_t[dim * dim];
|
2019-08-03 10:39:39 +02:00
|
|
|
memcpy(heightdata, ktxImage, ktxSize);
|
2016-06-25 13:30:55 +02:00
|
|
|
this->scale = dim / patchsize;
|
2019-08-03 10:39:39 +02:00
|
|
|
ktxTexture_Destroy(ktxTexture);
|
2016-06-25 13:30:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
~HeightMap()
|
2020-05-29 16:08:53 +01:00
|
|
|
{
|
2016-06-25 13:30:55 +02:00
|
|
|
delete[] heightdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float getHeight(uint32_t x, uint32_t y)
|
|
|
|
|
{
|
|
|
|
|
glm::ivec2 rpos = glm::ivec2(x, y) * glm::ivec2(scale);
|
|
|
|
|
rpos.x = std::max(0, std::min(rpos.x, (int)dim-1));
|
|
|
|
|
rpos.y = std::max(0, std::min(rpos.y, (int)dim-1));
|
|
|
|
|
rpos /= glm::ivec2(scale);
|
|
|
|
|
return *(heightdata + (rpos.x + rpos.y * dim) * scale) / 65535.0f;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
// Generate a terrain quad patch for feeding to the tessellation control shader
|
2020-05-29 16:08:53 +01:00
|
|
|
void generateTerrain()
|
2016-06-18 19:24:37 +02:00
|
|
|
{
|
|
|
|
|
#define PATCH_SIZE 64
|
2016-06-19 13:35:09 +02:00
|
|
|
#define UV_SCALE 1.0f
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-10-21 16:05:27 +02:00
|
|
|
const uint32_t vertexCount = PATCH_SIZE * PATCH_SIZE;
|
2020-07-28 20:20:38 +02:00
|
|
|
// We use the Vertex definition from the glTF model loader, so we can re-use the vertex input state
|
|
|
|
|
vkglTF::Vertex *vertices = new vkglTF::Vertex[vertexCount];
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
const float wx = 2.0f;
|
|
|
|
|
const float wy = 2.0f;
|
|
|
|
|
|
|
|
|
|
for (auto x = 0; x < PATCH_SIZE; x++)
|
|
|
|
|
{
|
|
|
|
|
for (auto y = 0; y < PATCH_SIZE; y++)
|
|
|
|
|
{
|
|
|
|
|
uint32_t index = (x + y * PATCH_SIZE);
|
|
|
|
|
vertices[index].pos[0] = x * wx + wx / 2.0f - (float)PATCH_SIZE * wx / 2.0f;
|
|
|
|
|
vertices[index].pos[1] = 0.0f;
|
|
|
|
|
vertices[index].pos[2] = y * wy + wy / 2.0f - (float)PATCH_SIZE * wy / 2.0f;
|
|
|
|
|
vertices[index].uv = glm::vec2((float)x / PATCH_SIZE, (float)y / PATCH_SIZE) * UV_SCALE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:30:55 +02:00
|
|
|
// Calculate normals from height map using a sobel filter
|
2016-06-25 22:49:22 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
HeightMap heightMap(getAssetPath() + "textures/terrain_heightmap_r16.ktx", PATCH_SIZE, androidApp->activity->assetManager);
|
|
|
|
|
#else
|
2016-06-25 13:30:55 +02:00
|
|
|
HeightMap heightMap(getAssetPath() + "textures/terrain_heightmap_r16.ktx", PATCH_SIZE);
|
2016-06-25 22:49:22 +02:00
|
|
|
#endif
|
2016-06-25 13:30:55 +02:00
|
|
|
for (auto x = 0; x < PATCH_SIZE; x++)
|
|
|
|
|
{
|
|
|
|
|
for (auto y = 0; y < PATCH_SIZE; y++)
|
2020-05-29 16:08:53 +01:00
|
|
|
{
|
2016-06-25 13:30:55 +02:00
|
|
|
// Get height samples centered around current position
|
|
|
|
|
float heights[3][3];
|
|
|
|
|
for (auto hx = -1; hx <= 1; hx++)
|
|
|
|
|
{
|
|
|
|
|
for (auto hy = -1; hy <= 1; hy++)
|
|
|
|
|
{
|
|
|
|
|
heights[hx+1][hy+1] = heightMap.getHeight(x + hx, y + hy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 16:05:27 +02:00
|
|
|
// Calculate the normal
|
2016-06-25 13:30:55 +02:00
|
|
|
glm::vec3 normal;
|
|
|
|
|
// Gx sobel filter
|
|
|
|
|
normal.x = heights[0][0] - heights[2][0] + 2.0f * heights[0][1] - 2.0f * heights[2][1] + heights[0][2] - heights[2][2];
|
|
|
|
|
// Gy sobel filter
|
|
|
|
|
normal.z = heights[0][0] + 2.0f * heights[1][0] + heights[2][0] - heights[0][2] - 2.0f * heights[1][2] - heights[2][2];
|
|
|
|
|
// Calculate missing up component of the normal using the filtered x and y axis
|
|
|
|
|
// The first value controls the bump strength
|
|
|
|
|
normal.y = 0.25f * sqrt( 1.0f - normal.x * normal.x - normal.z * normal.z);
|
|
|
|
|
|
|
|
|
|
vertices[x + y * PATCH_SIZE].normal = glm::normalize(normal * glm::vec3(2.0f, 1.0f, 2.0f));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Indices
|
2016-06-18 19:24:37 +02:00
|
|
|
const uint32_t w = (PATCH_SIZE - 1);
|
2017-10-21 16:05:27 +02:00
|
|
|
const uint32_t indexCount = w * w * 4;
|
|
|
|
|
uint32_t *indices = new uint32_t[indexCount];
|
2016-06-18 19:24:37 +02:00
|
|
|
for (auto x = 0; x < w; x++)
|
|
|
|
|
{
|
|
|
|
|
for (auto y = 0; y < w; y++)
|
|
|
|
|
{
|
|
|
|
|
uint32_t index = (x + y * w) * 4;
|
|
|
|
|
indices[index] = (x + y * PATCH_SIZE);
|
|
|
|
|
indices[index + 1] = indices[index] + PATCH_SIZE;
|
|
|
|
|
indices[index + 2] = indices[index + 1] + 1;
|
|
|
|
|
indices[index + 3] = indices[index] + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-28 20:20:38 +02:00
|
|
|
terrain.indices.count = indexCount;
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
uint32_t vertexBufferSize = vertexCount * sizeof(vkglTF::Vertex);
|
2017-10-21 16:05:27 +02:00
|
|
|
uint32_t indexBufferSize = indexCount * sizeof(uint32_t);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
} vertexStaging, indexStaging;
|
|
|
|
|
|
|
|
|
|
// Create staging buffers
|
2017-01-07 19:42:00 +01:00
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
2017-01-07 19:42:00 +01:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-06-18 19:24:37 +02:00
|
|
|
vertexBufferSize,
|
|
|
|
|
&vertexStaging.buffer,
|
2017-01-07 19:42:00 +01:00
|
|
|
&vertexStaging.memory,
|
|
|
|
|
vertices));
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
2017-01-07 19:42:00 +01:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-06-18 19:24:37 +02:00
|
|
|
indexBufferSize,
|
|
|
|
|
&indexStaging.buffer,
|
2017-01-07 19:42:00 +01:00
|
|
|
&indexStaging.memory,
|
|
|
|
|
indices));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-01-07 19:42:00 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
|
|
|
|
vertexBufferSize,
|
2020-07-28 20:20:38 +02:00
|
|
|
&terrain.vertices.buffer,
|
|
|
|
|
&terrain.vertices.memory));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-01-07 19:42:00 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
|
|
|
|
indexBufferSize,
|
2020-07-28 20:20:38 +02:00
|
|
|
&terrain.indices.buffer,
|
|
|
|
|
&terrain.indices.memory));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Copy from staging buffers
|
2020-04-20 22:13:51 +02:00
|
|
|
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
VkBufferCopy copyRegion = {};
|
|
|
|
|
|
|
|
|
|
copyRegion.size = vertexBufferSize;
|
|
|
|
|
vkCmdCopyBuffer(
|
|
|
|
|
copyCmd,
|
|
|
|
|
vertexStaging.buffer,
|
2020-07-28 20:20:38 +02:00
|
|
|
terrain.vertices.buffer,
|
2016-06-18 19:24:37 +02:00
|
|
|
1,
|
|
|
|
|
©Region);
|
|
|
|
|
|
|
|
|
|
copyRegion.size = indexBufferSize;
|
|
|
|
|
vkCmdCopyBuffer(
|
|
|
|
|
copyCmd,
|
|
|
|
|
indexStaging.buffer,
|
2020-07-28 20:20:38 +02:00
|
|
|
terrain.indices.buffer,
|
2016-06-18 19:24:37 +02:00
|
|
|
1,
|
|
|
|
|
©Region);
|
|
|
|
|
|
2020-04-20 22:13:51 +02:00
|
|
|
vulkanDevice->flushCommandBuffer(copyCmd, queue, true);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
vkDestroyBuffer(device, vertexStaging.buffer, nullptr);
|
|
|
|
|
vkFreeMemory(device, vertexStaging.memory, nullptr);
|
|
|
|
|
vkDestroyBuffer(device, indexStaging.buffer, nullptr);
|
|
|
|
|
vkFreeMemory(device, indexStaging.memory, nullptr);
|
|
|
|
|
|
|
|
|
|
delete[] vertices;
|
|
|
|
|
delete[] indices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
|
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes =
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
|
|
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3)
|
2016-06-18 19:24:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolCreateInfo(
|
2016-06-25 17:58:27 +02:00
|
|
|
static_cast<uint32_t>(poolSizes.size()),
|
2016-06-18 19:24:37 +02:00
|
|
|
poolSizes.data(),
|
|
|
|
|
2);
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSetLayouts()
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout;
|
|
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
|
|
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings;
|
|
|
|
|
|
|
|
|
|
// Terrain
|
|
|
|
|
setLayoutBindings =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Shared Tessellation shader ubo
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(
|
2020-05-29 16:08:53 +01:00
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT,
|
|
|
|
|
0),
|
|
|
|
|
// Binding 1 : Height map
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
2016-06-23 22:01:48 +02:00
|
|
|
VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
2016-06-18 19:24:37 +02:00
|
|
|
1),
|
|
|
|
|
// Binding 3 : Terrain texture array layers
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
|
2),
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.terrain));
|
2017-02-12 11:12:42 +01:00
|
|
|
pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.terrain, 1);
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayouts.terrain));
|
|
|
|
|
|
|
|
|
|
// Skysphere
|
|
|
|
|
setLayoutBindings =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Vertex shader ubo
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
0),
|
|
|
|
|
// Binding 1 : Color map
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
|
1),
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.skysphere));
|
2017-02-12 11:12:42 +01:00
|
|
|
pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.skysphere, 1);
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayouts.skysphere));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSets()
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo;
|
|
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
|
|
|
|
|
|
|
|
|
// Terrain
|
2017-02-12 11:12:42 +01:00
|
|
|
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.terrain, 1);
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.terrain));
|
|
|
|
|
|
|
|
|
|
writeDescriptorSets =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Shared tessellation shader ubo
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::writeDescriptorSet(
|
2020-05-29 16:08:53 +01:00
|
|
|
descriptorSets.terrain,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
0,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.terrainTessellation.descriptor),
|
2016-06-18 19:24:37 +02:00
|
|
|
// Binding 1 : Displacement map
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::writeDescriptorSet(
|
2016-06-18 19:24:37 +02:00
|
|
|
descriptorSets.terrain,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
1,
|
|
|
|
|
&textures.heightMap.descriptor),
|
|
|
|
|
// Binding 2 : Color map (alpha channel)
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::writeDescriptorSet(
|
2016-06-18 19:24:37 +02:00
|
|
|
descriptorSets.terrain,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
2,
|
|
|
|
|
&textures.terrainArray.descriptor),
|
|
|
|
|
};
|
2016-06-25 17:58:27 +02:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Skysphere
|
2017-02-12 11:12:42 +01:00
|
|
|
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.skysphere, 1);
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.skysphere));
|
|
|
|
|
|
|
|
|
|
writeDescriptorSets =
|
|
|
|
|
{
|
|
|
|
|
// Binding 0 : Vertex shader ubo
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::writeDescriptorSet(
|
2016-06-18 19:24:37 +02:00
|
|
|
descriptorSets.skysphere,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
0,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.skysphereVertex.descriptor),
|
2016-06-18 19:24:37 +02:00
|
|
|
// Binding 1 : Fragment shader color map
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::writeDescriptorSet(
|
2016-06-18 19:24:37 +02:00
|
|
|
descriptorSets.skysphere,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
1,
|
|
|
|
|
&textures.skySphere.descriptor),
|
|
|
|
|
};
|
2016-06-25 17:58:27 +02:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_LINE_WIDTH };
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
|
2016-06-18 19:24:37 +02:00
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 4> shaderStages;
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
// We render the terrain as a grid of quad patches
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_PATCH_LIST, 0, VK_FALSE);
|
|
|
|
|
VkPipelineTessellationStateCreateInfo tessellationState = vks::initializers::pipelineTessellationStateCreateInfo(4);
|
2016-06-18 19:24:37 +02:00
|
|
|
// Terrain tessellation pipeline
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "terraintessellation/terrain.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "terraintessellation/terrain.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
|
shaderStages[2] = loadShader(getShadersPath() + "terraintessellation/terrain.tesc.spv", VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
|
|
|
|
|
shaderStages[3] = loadShader(getShadersPath() + "terraintessellation/terrain.tese.spv", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT);
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayouts.terrain, renderPass);
|
|
|
|
|
pipelineCI.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCI.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCI.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCI.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCI.pViewportState = &viewportState;
|
|
|
|
|
pipelineCI.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCI.pDynamicState = &dynamicState;
|
|
|
|
|
pipelineCI.pTessellationState = &tessellationState;
|
|
|
|
|
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
|
|
|
|
pipelineCI.pStages = shaderStages.data();
|
|
|
|
|
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({ vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Normal, vkglTF::VertexComponent::UV });
|
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.terrain));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Terrain wireframe pipeline
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.fillModeNonSolid) {
|
|
|
|
|
rasterizationState.polygonMode = VK_POLYGON_MODE_LINE;
|
2020-07-28 20:20:38 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.wireframe));
|
2017-03-17 20:58:43 +01:00
|
|
|
};
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Skysphere pipeline
|
2020-07-28 20:20:38 +02:00
|
|
|
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
|
2016-06-18 19:24:37 +02:00
|
|
|
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
|
|
|
|
|
// Revert to triangle list topology
|
|
|
|
|
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
|
|
|
// Reset tessellation state
|
2020-07-28 20:20:38 +02:00
|
|
|
pipelineCI.pTessellationState = nullptr;
|
2016-06-18 19:24:37 +02:00
|
|
|
// Don't write to depth buffer
|
|
|
|
|
depthStencilState.depthWriteEnable = VK_FALSE;
|
2020-07-28 20:20:38 +02:00
|
|
|
pipelineCI.stageCount = 2;
|
|
|
|
|
pipelineCI.layout = pipelineLayouts.skysphere;
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "terraintessellation/skysphere.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "terraintessellation/skysphere.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2020-07-28 20:20:38 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.skysphere));
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Shared tessellation shader stages uniform buffer
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.terrainTessellation,
|
|
|
|
|
sizeof(uboTess)));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
// Skysphere vertex shader uniform buffer
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-06-18 19:24:37 +02:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.skysphereVertex,
|
|
|
|
|
sizeof(uboVS)));
|
|
|
|
|
|
|
|
|
|
// Map persistent
|
|
|
|
|
VK_CHECK_RESULT(uniformBuffers.terrainTessellation.map());
|
|
|
|
|
VK_CHECK_RESULT(uniformBuffers.skysphereVertex.map());
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Tessellation
|
|
|
|
|
|
|
|
|
|
uboTess.projection = camera.matrices.perspective;
|
2017-09-24 18:17:07 +02:00
|
|
|
uboTess.modelview = camera.matrices.view * glm::mat4(1.0f);
|
2016-06-18 19:24:37 +02:00
|
|
|
uboTess.lightPos.y = -0.5f - uboTess.displacementFactor; // todo: Not uesed yet
|
|
|
|
|
uboTess.viewportDim = glm::vec2((float)width, (float)height);
|
|
|
|
|
|
2016-06-23 22:01:48 +02:00
|
|
|
frustum.update(uboTess.projection * uboTess.modelview);
|
|
|
|
|
memcpy(uboTess.frustumPlanes, frustum.planes.data(), sizeof(glm::vec4) * 6);
|
|
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
float savedFactor = uboTess.tessellationFactor;
|
|
|
|
|
if (!tessellation)
|
|
|
|
|
{
|
|
|
|
|
// Setting this to zero sets all tessellation factors to 1.0 in the shader
|
|
|
|
|
uboTess.tessellationFactor = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBuffers.terrainTessellation.mapped, &uboTess, sizeof(uboTess));
|
2016-06-18 19:24:37 +02:00
|
|
|
|
|
|
|
|
if (!tessellation)
|
|
|
|
|
{
|
|
|
|
|
uboTess.tessellationFactor = savedFactor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skysphere vertex shader
|
|
|
|
|
uboVS.mvp = camera.matrices.perspective * glm::mat4(glm::mat3(camera.matrices.view));
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBuffers.skysphereVertex.mapped, &uboVS, sizeof(uboVS));
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
2020-08-09 13:16:35 +02:00
|
|
|
// Command buffer to be submitted to the queue
|
2016-06-18 19:24:37 +02:00
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
|
|
|
|
|
|
|
|
// Submit to queue
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
// Read query results for displaying in next frame
|
|
|
|
|
getQueryResults();
|
|
|
|
|
}
|
2016-06-23 22:01:48 +02:00
|
|
|
|
2016-06-18 19:24:37 +02:00
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
2017-02-09 21:55:35 +01:00
|
|
|
loadAssets();
|
2016-06-23 22:01:48 +02:00
|
|
|
generateTerrain();
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
setupQueryResultBuffer();
|
|
|
|
|
}
|
2016-06-18 19:24:37 +02:00
|
|
|
prepareUniformBuffers();
|
|
|
|
|
setupDescriptorSetLayouts();
|
|
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSets();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2020-07-28 20:20:38 +02:00
|
|
|
if (camera.updated) {
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-13 23:04:53 -04:00
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
2016-06-18 19:24:37 +02:00
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->header("Settings")) {
|
2016-06-18 19:24:37 +02:00
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->checkBox("Tessellation", &tessellation)) {
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
if (overlay->inputFloat("Factor", &uboTess.tessellationFactor, 0.05f, 2)) {
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
2017-03-17 20:58:43 +01:00
|
|
|
if (deviceFeatures.fillModeNonSolid) {
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->checkBox("Wireframe", &wireframe)) {
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
}
|
2017-03-17 20:58:43 +01:00
|
|
|
}
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
2017-12-01 22:10:01 +01:00
|
|
|
if (deviceFeatures.pipelineStatisticsQuery) {
|
|
|
|
|
if (overlay->header("Pipeline statistics")) {
|
|
|
|
|
overlay->text("VS invocations: %d", pipelineStats[0]);
|
|
|
|
|
overlay->text("TE invocations: %d", pipelineStats[1]);
|
|
|
|
|
}
|
2017-11-01 14:22:10 +01:00
|
|
|
}
|
2016-06-18 19:24:37 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-13 23:04:53 -04:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|