2016-02-25 22:08:47 +01:00
|
|
|
/*
|
2016-03-06 12:55:52 +01:00
|
|
|
* Vulkan Example - Multi threaded command buffer generation and rendering
|
2016-02-25 22:08:47 +01:00
|
|
|
*
|
2023-12-20 20:14:36 +01:00
|
|
|
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
2016-02-25 22:08:47 +01:00
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vulkanexamplebase.h"
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
#include "threadpool.hpp"
|
2016-04-03 13:51:47 +02:00
|
|
|
#include "frustum.hpp"
|
2016-04-01 13:27:03 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
#include "VulkanglTFModel.h"
|
2017-02-11 14:18:24 +01:00
|
|
|
|
2016-02-25 22:08:47 +01:00
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-07-28 20:20:38 +02:00
|
|
|
bool displayStarSphere = true;
|
2017-02-11 14:18:24 +01:00
|
|
|
|
|
|
|
|
struct {
|
2020-07-28 20:20:38 +02:00
|
|
|
vkglTF::Model ufo;
|
|
|
|
|
vkglTF::Model starSphere;
|
2017-02-11 14:18:24 +01:00
|
|
|
} models;
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Shared matrices used for thread push constant blocks
|
|
|
|
|
struct {
|
2016-02-25 22:08:47 +01:00
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 view;
|
2016-04-01 13:27:03 +02:00
|
|
|
} matrices;
|
2016-02-25 22:08:47 +01:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkPipeline phong;
|
2016-04-01 22:48:16 +02:00
|
|
|
VkPipeline starsphere;
|
2016-02-25 22:08:47 +01:00
|
|
|
} pipelines;
|
|
|
|
|
|
|
|
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
VkCommandBuffer primaryCommandBuffer;
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2020-08-08 13:59:58 +02:00
|
|
|
// Secondary scene command buffers used to store backdrop and user interface
|
2018-09-09 09:34:03 +02:00
|
|
|
struct SecondaryCommandBuffers {
|
|
|
|
|
VkCommandBuffer background;
|
|
|
|
|
VkCommandBuffer ui;
|
|
|
|
|
} secondaryCommandBuffers;
|
2016-04-01 13:27:03 +02:00
|
|
|
|
|
|
|
|
// Number of animated objects to be renderer
|
|
|
|
|
// by using threads and secondary command buffers
|
|
|
|
|
uint32_t numObjectsPerThread;
|
|
|
|
|
|
2016-02-25 22:08:47 +01:00
|
|
|
// Multi threaded stuff
|
|
|
|
|
// Max. number of concurrent threads
|
|
|
|
|
uint32_t numThreads;
|
|
|
|
|
|
|
|
|
|
// Use push constants to update shader
|
|
|
|
|
// parameters on a per-thread base
|
|
|
|
|
struct ThreadPushConstantBlock {
|
2016-04-01 13:27:03 +02:00
|
|
|
glm::mat4 mvp;
|
2016-02-25 22:08:47 +01:00
|
|
|
glm::vec3 color;
|
|
|
|
|
};
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-04-01 14:24:46 +02:00
|
|
|
struct ObjectData {
|
2016-04-01 22:48:16 +02:00
|
|
|
glm::mat4 model;
|
2016-04-01 14:24:46 +02:00
|
|
|
glm::vec3 pos;
|
|
|
|
|
glm::vec3 rotation;
|
2016-04-01 15:58:59 +02:00
|
|
|
float rotationDir;
|
|
|
|
|
float rotationSpeed;
|
2016-04-01 22:48:16 +02:00
|
|
|
float scale;
|
2016-04-01 14:24:46 +02:00
|
|
|
float deltaT;
|
2016-04-03 13:51:47 +02:00
|
|
|
float stateT = 0;
|
|
|
|
|
bool visible = true;
|
2016-04-01 14:24:46 +02:00
|
|
|
};
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
struct ThreadData {
|
|
|
|
|
VkCommandPool commandPool;
|
2016-04-01 14:24:46 +02:00
|
|
|
// One command buffer per render object
|
2016-04-01 13:27:03 +02:00
|
|
|
std::vector<VkCommandBuffer> commandBuffer;
|
2016-04-01 14:24:46 +02:00
|
|
|
// One push constant block per render object
|
|
|
|
|
std::vector<ThreadPushConstantBlock> pushConstBlock;
|
|
|
|
|
// Per object information (position, rotation, etc.)
|
|
|
|
|
std::vector<ObjectData> objectData;
|
2016-02-25 22:08:47 +01:00
|
|
|
};
|
2016-04-01 13:27:03 +02:00
|
|
|
std::vector<ThreadData> threadData;
|
|
|
|
|
|
2017-02-12 13:39:08 +01:00
|
|
|
vks::ThreadPool threadPool;
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
// Fence to wait for all command buffers to finish before
|
|
|
|
|
// presenting to the swap chain
|
|
|
|
|
VkFence renderFence = {};
|
|
|
|
|
|
2016-04-03 13:51:47 +02:00
|
|
|
// View frustum for culling invisible objects
|
2017-02-12 13:37:12 +01:00
|
|
|
vks::Frustum frustum;
|
2016-04-03 13:51:47 +02:00
|
|
|
|
2018-01-18 21:31:19 +01:00
|
|
|
std::default_random_engine rndEngine;
|
|
|
|
|
|
2016-02-25 22:08:47 +01:00
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
title = "Multi threaded command buffer";
|
2020-04-22 20:58:24 +02:00
|
|
|
camera.type = Camera::CameraType::lookat;
|
|
|
|
|
camera.setPosition(glm::vec3(0.0f, -0.0f, -32.5f));
|
|
|
|
|
camera.setRotation(glm::vec3(0.0f));
|
|
|
|
|
camera.setRotationSpeed(0.5f);
|
|
|
|
|
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
|
2020-08-08 18:22:10 +02:00
|
|
|
// Get number of max. concurrent threads
|
2016-02-25 22:08:47 +01:00
|
|
|
numThreads = std::thread::hardware_concurrency();
|
|
|
|
|
assert(numThreads > 0);
|
2016-04-01 22:48:16 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
LOGD("numThreads = %d", numThreads);
|
|
|
|
|
#else
|
2016-02-25 22:08:47 +01:00
|
|
|
std::cout << "numThreads = " << numThreads << std::endl;
|
2016-04-01 22:48:16 +02:00
|
|
|
#endif
|
2016-04-01 13:27:03 +02:00
|
|
|
threadPool.setThreadCount(numThreads);
|
2016-09-21 18:52:27 +02:00
|
|
|
numObjectsPerThread = 512 / numThreads;
|
2018-01-18 21:31:19 +01:00
|
|
|
rndEngine.seed(benchmark.active ? 0 : (unsigned)time(nullptr));
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2020-05-29 16:08:53 +01:00
|
|
|
// Clean up used Vulkan resources
|
2016-02-25 22:08:47 +01:00
|
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
|
vkDestroyPipeline(device, pipelines.phong, nullptr);
|
2016-04-01 22:48:16 +02:00
|
|
|
vkDestroyPipeline(device, pipelines.starsphere, nullptr);
|
2016-02-25 22:08:47 +01:00
|
|
|
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
for (auto& thread : threadData) {
|
2016-04-01 13:27:03 +02:00
|
|
|
vkFreeCommandBuffers(device, thread.commandPool, thread.commandBuffer.size(), thread.commandBuffer.data());
|
|
|
|
|
vkDestroyCommandPool(device, thread.commandPool, nullptr);
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
2016-05-29 13:15:16 +02:00
|
|
|
|
|
|
|
|
vkDestroyFence(device, renderFence, nullptr);
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 15:58:59 +02:00
|
|
|
float rnd(float range)
|
|
|
|
|
{
|
2018-01-18 21:31:19 +01:00
|
|
|
std::uniform_real_distribution<float> rndDist(0.0f, range);
|
|
|
|
|
return rndDist(rndEngine);
|
2016-04-01 15:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Create all threads and initialize shader push constants
|
|
|
|
|
void prepareMultiThreadedRenderer()
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
2016-04-01 13:27:03 +02:00
|
|
|
// Since this demo updates the command buffers on each frame
|
|
|
|
|
// we don't use the per-framebuffer command buffers from the
|
|
|
|
|
// base class, and create a single primary command buffer instead
|
2016-04-01 22:48:16 +02:00
|
|
|
VkCommandBufferAllocateInfo cmdBufAllocateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::commandBufferAllocateInfo(
|
2016-04-01 13:27:03 +02:00
|
|
|
cmdPool,
|
|
|
|
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
|
|
|
1);
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &primaryCommandBuffer));
|
2016-03-06 12:55:52 +01:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
// Create additional secondary CBs for background and ui
|
2016-04-01 22:48:16 +02:00
|
|
|
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY;
|
2018-09-09 09:34:03 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &secondaryCommandBuffers.background));
|
|
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &secondaryCommandBuffers.ui));
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
threadData.resize(numThreads);
|
2016-03-06 12:55:52 +01:00
|
|
|
|
2016-04-01 15:58:59 +02:00
|
|
|
float maxX = std::floor(std::sqrt(numThreads * numObjectsPerThread));
|
|
|
|
|
uint32_t posX = 0;
|
|
|
|
|
uint32_t posZ = 0;
|
|
|
|
|
|
2018-01-18 21:31:19 +01:00
|
|
|
for (uint32_t i = 0; i < numThreads; i++) {
|
2016-04-01 13:27:03 +02:00
|
|
|
ThreadData *thread = &threadData[i];
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Create one command pool for each thread
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandPoolCreateInfo cmdPoolInfo = vks::initializers::commandPoolCreateInfo();
|
2016-04-01 13:27:03 +02:00
|
|
|
cmdPoolInfo.queueFamilyIndex = swapChain.queueNodeIndex;
|
|
|
|
|
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &thread->commandPool));
|
2016-03-06 12:55:52 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// One secondary command buffer per object that is updated by this thread
|
|
|
|
|
thread->commandBuffer.resize(numObjectsPerThread);
|
|
|
|
|
// Generate secondary command buffers for each thread
|
2016-04-01 22:48:16 +02:00
|
|
|
VkCommandBufferAllocateInfo secondaryCmdBufAllocateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::commandBufferAllocateInfo(
|
2016-04-01 13:27:03 +02:00
|
|
|
thread->commandPool,
|
|
|
|
|
VK_COMMAND_BUFFER_LEVEL_SECONDARY,
|
|
|
|
|
thread->commandBuffer.size());
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &secondaryCmdBufAllocateInfo, thread->commandBuffer.data()));
|
2016-03-06 12:55:52 +01:00
|
|
|
|
2016-04-01 14:24:46 +02:00
|
|
|
thread->pushConstBlock.resize(numObjectsPerThread);
|
|
|
|
|
thread->objectData.resize(numObjectsPerThread);
|
|
|
|
|
|
2018-01-18 21:31:19 +01:00
|
|
|
for (uint32_t j = 0; j < numObjectsPerThread; j++) {
|
|
|
|
|
float theta = 2.0f * float(M_PI) * rnd(1.0f);
|
|
|
|
|
float phi = acos(1.0f - 2.0f * rnd(1.0f));
|
2016-09-21 18:52:27 +02:00
|
|
|
thread->objectData[j].pos = glm::vec3(sin(phi) * cos(theta), 0.0f, cos(phi)) * 35.0f;
|
2016-04-01 15:58:59 +02:00
|
|
|
|
2016-04-01 22:48:16 +02:00
|
|
|
thread->objectData[j].rotation = glm::vec3(0.0f, rnd(360.0f), 0.0f);
|
2016-04-01 15:58:59 +02:00
|
|
|
thread->objectData[j].deltaT = rnd(1.0f);
|
|
|
|
|
thread->objectData[j].rotationDir = (rnd(100.0f) < 50.0f) ? 1.0f : -1.0f;
|
2016-04-03 13:51:47 +02:00
|
|
|
thread->objectData[j].rotationSpeed = (2.0f + rnd(4.0f)) * thread->objectData[j].rotationDir;
|
2016-04-01 22:48:16 +02:00
|
|
|
thread->objectData[j].scale = 0.75f + rnd(0.5f);
|
2016-04-01 15:58:59 +02:00
|
|
|
|
|
|
|
|
thread->pushConstBlock[j].color = glm::vec3(rnd(1.0f), rnd(1.0f), rnd(1.0f));
|
2016-04-01 14:24:46 +02:00
|
|
|
}
|
2016-03-06 12:55:52 +01:00
|
|
|
}
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Builds the secondary command buffer for each thread
|
|
|
|
|
void threadRenderCode(uint32_t threadIndex, uint32_t cmdBufferIndex, VkCommandBufferInheritanceInfo inheritanceInfo)
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
2016-04-03 13:51:47 +02:00
|
|
|
ThreadData *thread = &threadData[threadIndex];
|
|
|
|
|
ObjectData *objectData = &thread->objectData[cmdBufferIndex];
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
// Check visibility against view frustum using a simple sphere check based on the radius of the mesh
|
|
|
|
|
objectData->visible = frustum.checkSphere(objectData->pos, models.ufo.dimensions.radius * 0.5f);
|
2016-04-03 13:51:47 +02:00
|
|
|
|
|
|
|
|
if (!objectData->visible)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo commandBufferBeginInfo = vks::initializers::commandBufferBeginInfo();
|
2016-04-01 13:27:03 +02:00
|
|
|
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
|
|
|
|
|
commandBufferBeginInfo.pInheritanceInfo = &inheritanceInfo;
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
VkCommandBuffer cmdBuffer = thread->commandBuffer[cmdBufferIndex];
|
2016-03-06 12:55:52 +01:00
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffer, &commandBufferBeginInfo));
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
2016-04-01 13:27:03 +02:00
|
|
|
vkCmdSetViewport(cmdBuffer, 0, 1, &viewport);
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
2016-04-01 13:27:03 +02:00
|
|
|
vkCmdSetScissor(cmdBuffer, 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.phong);
|
|
|
|
|
|
|
|
|
|
// Update
|
2018-01-18 21:31:19 +01:00
|
|
|
if (!paused) {
|
|
|
|
|
objectData->rotation.y += 2.5f * objectData->rotationSpeed * frameTimer;
|
|
|
|
|
if (objectData->rotation.y > 360.0f) {
|
|
|
|
|
objectData->rotation.y -= 360.0f;
|
|
|
|
|
}
|
|
|
|
|
objectData->deltaT += 0.15f * frameTimer;
|
|
|
|
|
if (objectData->deltaT > 1.0f)
|
|
|
|
|
objectData->deltaT -= 1.0f;
|
|
|
|
|
objectData->pos.y = sin(glm::radians(objectData->deltaT * 360.0f)) * 2.5f;
|
2016-04-01 15:58:59 +02:00
|
|
|
}
|
2016-04-01 14:24:46 +02:00
|
|
|
|
2017-09-24 18:17:07 +02:00
|
|
|
objectData->model = glm::translate(glm::mat4(1.0f), objectData->pos);
|
2016-04-01 22:48:16 +02:00
|
|
|
objectData->model = glm::rotate(objectData->model, -sinf(glm::radians(objectData->deltaT * 360.0f)) * 0.25f, glm::vec3(objectData->rotationDir, 0.0f, 0.0f));
|
|
|
|
|
objectData->model = glm::rotate(objectData->model, glm::radians(objectData->rotation.y), glm::vec3(0.0f, objectData->rotationDir, 0.0f));
|
|
|
|
|
objectData->model = glm::rotate(objectData->model, glm::radians(objectData->deltaT * 360.0f), glm::vec3(0.0f, objectData->rotationDir, 0.0f));
|
|
|
|
|
objectData->model = glm::scale(objectData->model, glm::vec3(objectData->scale));
|
2016-04-01 14:24:46 +02:00
|
|
|
|
2016-04-01 22:48:16 +02:00
|
|
|
thread->pushConstBlock[cmdBufferIndex].mvp = matrices.projection * matrices.view * objectData->model;
|
2016-04-01 13:27:03 +02:00
|
|
|
|
|
|
|
|
// Update shader push constant block
|
|
|
|
|
// Contains model view matrix
|
|
|
|
|
vkCmdPushConstants(
|
|
|
|
|
cmdBuffer,
|
|
|
|
|
pipelineLayout,
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
0,
|
|
|
|
|
sizeof(ThreadPushConstantBlock),
|
2016-04-01 14:24:46 +02:00
|
|
|
&thread->pushConstBlock[cmdBufferIndex]);
|
2016-04-01 13:27:03 +02:00
|
|
|
|
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
2017-02-11 14:18:24 +01:00
|
|
|
vkCmdBindVertexBuffers(cmdBuffer, 0, 1, &models.ufo.vertices.buffer, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(cmdBuffer, models.ufo.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
2020-07-28 20:20:38 +02:00
|
|
|
vkCmdDrawIndexed(cmdBuffer, models.ufo.indices.count, 1, 0, 0, 0);
|
2016-09-21 18:52:27 +02:00
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(cmdBuffer));
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
void updateSecondaryCommandBuffers(VkCommandBufferInheritanceInfo inheritanceInfo)
|
2016-04-01 22:48:16 +02:00
|
|
|
{
|
|
|
|
|
// Secondary command buffer for the sky sphere
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo commandBufferBeginInfo = vks::initializers::commandBufferBeginInfo();
|
2016-04-01 22:48:16 +02:00
|
|
|
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
|
|
|
|
|
commandBufferBeginInfo.pInheritanceInfo = &inheritanceInfo;
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
|
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
/*
|
|
|
|
|
Background
|
|
|
|
|
*/
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(secondaryCommandBuffers.background, &commandBufferBeginInfo));
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
vkCmdSetViewport(secondaryCommandBuffers.background, 0, 1, &viewport);
|
|
|
|
|
vkCmdSetScissor(secondaryCommandBuffers.background, 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(secondaryCommandBuffers.background, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.starsphere);
|
|
|
|
|
|
2020-04-22 20:58:24 +02:00
|
|
|
glm::mat4 mvp = matrices.projection * matrices.view;
|
|
|
|
|
mvp[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
2020-07-28 20:20:38 +02:00
|
|
|
mvp = glm::scale(mvp, glm::vec3(2.0f));
|
2018-09-09 09:34:03 +02:00
|
|
|
|
|
|
|
|
vkCmdPushConstants(
|
|
|
|
|
secondaryCommandBuffers.background,
|
|
|
|
|
pipelineLayout,
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
0,
|
|
|
|
|
sizeof(mvp),
|
|
|
|
|
&mvp);
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
models.starSphere.draw(secondaryCommandBuffers.background);
|
|
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(secondaryCommandBuffers.background));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
User interface
|
|
|
|
|
|
|
|
|
|
With VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS, the primary command buffer's content has to be defined
|
|
|
|
|
by secondary command buffers, which also applies to the UI overlay command buffer
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(secondaryCommandBuffers.ui, &commandBufferBeginInfo));
|
|
|
|
|
|
|
|
|
|
vkCmdSetViewport(secondaryCommandBuffers.ui, 0, 1, &viewport);
|
|
|
|
|
vkCmdSetScissor(secondaryCommandBuffers.ui, 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(secondaryCommandBuffers.ui, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.starsphere);
|
2018-08-31 22:33:26 +02:00
|
|
|
|
2022-08-01 16:11:57 -04:00
|
|
|
drawUI(secondaryCommandBuffers.ui);
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(secondaryCommandBuffers.ui));
|
2016-04-01 22:48:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 16:08:53 +01:00
|
|
|
// Updates the secondary command buffers using a thread pool
|
|
|
|
|
// and puts them into the primary command buffer that's
|
2016-04-01 14:24:46 +02:00
|
|
|
// lat submitted to the queue for rendering
|
2016-04-01 13:27:03 +02:00
|
|
|
void updateCommandBuffers(VkFramebuffer frameBuffer)
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
2018-09-09 09:34:03 +02:00
|
|
|
// Contains the list of secondary command buffers to be submitted
|
|
|
|
|
std::vector<VkCommandBuffer> commandBuffers;
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-02-25 22:08:47 +01: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-02-25 22:08:47 +01: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;
|
2016-04-01 13:27:03 +02:00
|
|
|
renderPassBeginInfo.framebuffer = frameBuffer;
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Set target frame buffer
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(primaryCommandBuffer, &cmdBufInfo));
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// The primary command buffer does not contain any rendering commands
|
|
|
|
|
// These are stored (and retrieved) from the secondary command buffers
|
|
|
|
|
vkCmdBeginRenderPass(primaryCommandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Inheritance info for the secondary command buffers
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferInheritanceInfo inheritanceInfo = vks::initializers::commandBufferInheritanceInfo();
|
2016-04-01 13:27:03 +02:00
|
|
|
inheritanceInfo.renderPass = renderPass;
|
|
|
|
|
// Secondary command buffer also use the currently active framebuffer
|
|
|
|
|
inheritanceInfo.framebuffer = frameBuffer;
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
// Update secondary sene command buffers
|
|
|
|
|
updateSecondaryCommandBuffers(inheritanceInfo);
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
if (displayStarSphere) {
|
2018-09-09 09:34:03 +02:00
|
|
|
commandBuffers.push_back(secondaryCommandBuffers.background);
|
|
|
|
|
}
|
2016-04-01 22:48:16 +02:00
|
|
|
|
2016-04-24 13:14:48 +02:00
|
|
|
// Add a job to the thread's queue for each object to be rendered
|
2016-04-01 13:27:03 +02:00
|
|
|
for (uint32_t t = 0; t < numThreads; t++)
|
|
|
|
|
{
|
2016-04-01 14:24:46 +02:00
|
|
|
for (uint32_t i = 0; i < numObjectsPerThread; i++)
|
|
|
|
|
{
|
|
|
|
|
threadPool.threads[t]->addJob([=] { threadRenderCode(t, i, inheritanceInfo); });
|
2016-04-24 13:14:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-04-24 13:14:48 +02:00
|
|
|
threadPool.wait();
|
|
|
|
|
|
|
|
|
|
// Only submit if object is within the current view frustum
|
|
|
|
|
for (uint32_t t = 0; t < numThreads; t++)
|
|
|
|
|
{
|
|
|
|
|
for (uint32_t i = 0; i < numObjectsPerThread; i++)
|
|
|
|
|
{
|
2016-04-03 13:51:47 +02:00
|
|
|
if (threadData[t].objectData[i].visible)
|
|
|
|
|
{
|
|
|
|
|
commandBuffers.push_back(threadData[t].commandBuffer[i]);
|
|
|
|
|
}
|
2016-04-01 14:24:46 +02:00
|
|
|
}
|
2016-04-01 13:27:03 +02:00
|
|
|
}
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2018-09-09 09:34:03 +02:00
|
|
|
// Render ui last
|
|
|
|
|
if (UIOverlay.visible) {
|
|
|
|
|
commandBuffers.push_back(secondaryCommandBuffers.ui);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
// Execute render commands from the secondary command buffer
|
|
|
|
|
vkCmdExecuteCommands(primaryCommandBuffer, commandBuffers.size(), commandBuffers.data());
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
vkCmdEndRenderPass(primaryCommandBuffer);
|
|
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(primaryCommandBuffer));
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-09 10:15:51 +02:00
|
|
|
void loadAssets()
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
|
|
|
|
models.ufo.loadFromFile(getAssetPath() + "models/retroufo_red_lowpoly.gltf",vulkanDevice, queue,glTFLoadingFlags);
|
|
|
|
|
models.starSphere.loadFromFile(getAssetPath() + "models/sphere.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-02 12:47:08 +02:00
|
|
|
void setupPipelineLayout()
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineLayoutCreateInfo(nullptr, 0);
|
2016-02-25 22:08:47 +01:00
|
|
|
|
|
|
|
|
// Push constants for model matrices
|
|
|
|
|
VkPushConstantRange pushConstantRange =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pushConstantRange(
|
2016-04-02 12:47:08 +02:00
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
2016-04-01 13:27:03 +02:00
|
|
|
sizeof(ThreadPushConstantBlock),
|
2016-02-25 22:08:47 +01:00
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
// Push constant ranges are part of the pipeline layout
|
|
|
|
|
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
|
|
|
|
pPipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
|
|
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
|
|
|
|
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};
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
|
2016-02-25 22:08:47 +01:00
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
|
|
|
|
|
2018-09-09 10:15:51 +02:00
|
|
|
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
|
|
|
|
|
pipelineCI.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCI.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCI.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCI.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCI.pViewportState = &viewportState;
|
|
|
|
|
pipelineCI.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCI.pDynamicState = &dynamicState;
|
2023-12-20 20:14:36 +01:00
|
|
|
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
2018-09-09 10:15:51 +02:00
|
|
|
pipelineCI.pStages = shaderStages.data();
|
2020-07-28 20:20:38 +02:00
|
|
|
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Normal, vkglTF::VertexComponent::Color});
|
2016-02-25 22:08:47 +01:00
|
|
|
|
2018-09-09 10:15:51 +02:00
|
|
|
// Object rendering pipeline
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "multithreading/phong.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "multithreading/phong.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2018-09-09 10:15:51 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.phong));
|
2016-04-01 22:48:16 +02:00
|
|
|
|
|
|
|
|
// Star sphere rendering pipeline
|
|
|
|
|
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
|
|
|
|
|
depthStencilState.depthWriteEnable = VK_FALSE;
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "multithreading/starsphere.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "multithreading/starsphere.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2018-09-09 10:15:51 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.starsphere));
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 13:27:03 +02:00
|
|
|
void updateMatrices()
|
2016-02-25 22:08:47 +01:00
|
|
|
{
|
2020-04-22 20:58:24 +02:00
|
|
|
matrices.projection = camera.matrices.perspective;
|
|
|
|
|
matrices.view = camera.matrices.view;
|
2016-04-03 13:51:47 +02:00
|
|
|
frustum.update(matrices.projection * matrices.view);
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
// Wait for fence to signal that all command buffers are ready
|
|
|
|
|
VkResult fenceRes;
|
2018-01-18 21:31:19 +01:00
|
|
|
do {
|
2016-05-29 13:15:16 +02:00
|
|
|
fenceRes = vkWaitForFences(device, 1, &renderFence, VK_TRUE, 100000000);
|
|
|
|
|
} while (fenceRes == VK_TIMEOUT);
|
|
|
|
|
VK_CHECK_RESULT(fenceRes);
|
|
|
|
|
vkResetFences(device, 1, &renderFence);
|
|
|
|
|
|
2018-09-09 10:05:21 +02:00
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
2018-09-09 10:15:51 +02:00
|
|
|
updateCommandBuffers(frameBuffers[currentBuffer]);
|
|
|
|
|
|
2018-09-09 10:05:21 +02:00
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &primaryCommandBuffer;
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, renderFence));
|
|
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 22:08:47 +01:00
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
2016-05-29 13:15:16 +02:00
|
|
|
// Create a fence for synchronization
|
2018-09-09 10:05:21 +02:00
|
|
|
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
|
2018-09-09 10:15:51 +02:00
|
|
|
vkCreateFence(device, &fenceCreateInfo, nullptr, &renderFence);
|
|
|
|
|
loadAssets();
|
2016-04-02 12:47:08 +02:00
|
|
|
setupPipelineLayout();
|
2016-02-25 22:08:47 +01:00
|
|
|
preparePipelines();
|
|
|
|
|
prepareMultiThreadedRenderer();
|
2016-04-01 13:27:03 +02:00
|
|
|
updateMatrices();
|
2016-02-25 22:08:47 +01:00
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2020-07-28 20:20:38 +02:00
|
|
|
if (camera.updated)
|
|
|
|
|
{
|
|
|
|
|
updateMatrices();
|
|
|
|
|
}
|
2016-02-25 22:08:47 +01:00
|
|
|
}
|
2016-05-29 13:15:16 +02:00
|
|
|
|
2022-06-13 23:04:53 -04:00
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
updateMatrices();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
2016-05-29 13:15:16 +02:00
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->header("Statistics")) {
|
|
|
|
|
overlay->text("Active threads: %d", numThreads);
|
|
|
|
|
}
|
2018-08-31 22:33:26 +02:00
|
|
|
if (overlay->header("Settings")) {
|
2020-07-28 20:20:38 +02:00
|
|
|
overlay->checkBox("Stars", &displayStarSphere);
|
2018-08-31 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-29 13:15:16 +02:00
|
|
|
}
|
2016-02-25 22:08:47 +01:00
|
|
|
};
|
|
|
|
|
|
2022-06-13 23:04:53 -04:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|