335 lines
No EOL
15 KiB
C++
335 lines
No EOL
15 KiB
C++
/*
|
|
* Vulkan Example - Line rendering
|
|
*
|
|
* Copyright (C) 2024 by Sascha Willems - www.saschawillems.de
|
|
*
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
*/
|
|
|
|
#include "vulkanexamplebase.h"
|
|
#include "VulkanglTFModel.h"
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
{
|
|
public:
|
|
int32_t gridSize{ 3 };
|
|
|
|
vkglTF::Model model;
|
|
|
|
struct UniformData {
|
|
glm::mat4 projection;
|
|
glm::mat4 modelview;
|
|
glm::vec4 lightPos{ -10.0f, -10.0f, 10.0f, 1.0f };
|
|
} uniformData;
|
|
vks::Buffer uniformBuffer;
|
|
|
|
struct Box {
|
|
vks::Buffer vertices;
|
|
vks::Buffer indices;
|
|
uint32_t indexCount{ 0 };
|
|
} box;
|
|
|
|
PFN_vkCmdSetLineRasterizationModeEXT vkCmdSetLineRasterizationModeEXT{ VK_NULL_HANDLE };
|
|
PFN_vkCmdSetLineStippleEnableEXT vkCmdSetLineStippleEnableEXT{ VK_NULL_HANDLE };
|
|
PFN_vkCmdSetLineStippleEXT vkCmdSetLineStippleEXT{ VK_NULL_HANDLE };
|
|
|
|
VkPipeline pipeline{ VK_NULL_HANDLE };
|
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
|
|
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
|
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
|
|
|
|
VkPipeline pipelineLines{ VK_NULL_HANDLE };
|
|
|
|
VulkanExample() : VulkanExampleBase()
|
|
{
|
|
title = "Line rendering";
|
|
camera.type = Camera::CameraType::firstperson;
|
|
camera.setPosition(glm::vec3(-3.0f, 1.0f, -2.75f));
|
|
camera.setRotation(glm::vec3(-15.25f, -46.5f, 0.0f));
|
|
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
|
|
camera.movementSpeed = 4.0f;
|
|
camera.rotationSpeed = 0.25f;
|
|
// @todo
|
|
enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
|
|
enabledDeviceExtensions.push_back(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME);
|
|
}
|
|
|
|
~VulkanExample()
|
|
{
|
|
if (device) {
|
|
vkDestroyPipeline(device, pipeline, nullptr);
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
|
uniformBuffer.destroy();
|
|
}
|
|
}
|
|
|
|
// Creates vertex and index buffers for rendering a box using line segments
|
|
void generateBox(glm::vec3 scale)
|
|
{
|
|
std::vector<glm::vec3> vertices = {
|
|
// Front
|
|
{ -1.0f, -1.0f, 1.0f },
|
|
{ 1.0f, -1.0f, 1.0f },
|
|
{ 1.0f, 1.0f, 1.0f },
|
|
{ -1.0f, 1.0f, 1.0f },
|
|
// Back
|
|
{ -1.0f, -1.0f, -1.0f },
|
|
{ 1.0f, -1.0f, -1.0f },
|
|
{ 1.0f, 1.0f, -1.0f },
|
|
{ -1.0f, 1.0f, -1.0f },
|
|
};
|
|
for (glm::vec3& pos : vertices) {
|
|
pos *= scale;
|
|
}
|
|
|
|
// Each pair defines a line segment
|
|
std::vector<uint32_t> indices = {
|
|
0,1, 1,2, 2,3, 3,0, 4,5, 5,6, 6,7, 7,4, 0,4, 1,5, 2,6, 3,7
|
|
};
|
|
|
|
box.indexCount = static_cast<uint32_t>(indices.size());
|
|
|
|
// Create buffers and upload data to the GPU
|
|
struct StagingBuffers {
|
|
vks::Buffer vertices;
|
|
vks::Buffer indices;
|
|
} stagingBuffers;
|
|
|
|
// Host visible source buffers (staging)
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffers.vertices, vertices.size() * sizeof(glm::vec3), vertices.data()));
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffers.indices, indices.size() * sizeof(uint32_t), indices.data()));
|
|
|
|
// Device local destination buffers
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &box.vertices, vertices.size() * sizeof(glm::vec3)));
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &box.indices, indices.size() * sizeof(uint32_t)));
|
|
|
|
// Copy from host do device
|
|
vulkanDevice->copyBuffer(&stagingBuffers.vertices, &box.vertices, queue);
|
|
vulkanDevice->copyBuffer(&stagingBuffers.indices, &box.indices, queue);
|
|
|
|
// Clean up
|
|
stagingBuffers.vertices.destroy();
|
|
stagingBuffers.indices.destroy();
|
|
}
|
|
|
|
|
|
void buildCommandBuffers()
|
|
{
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
|
|
|
VkClearValue clearValues[2];
|
|
clearValues[0].color = defaultClearColor;
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::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) {
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
|
|
//vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
|
//vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
|
|
//vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &model.vertices.buffer, offsets);
|
|
//vkCmdBindIndexBuffer(drawCmdBuffers[i], model.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
for (int32_t y = 0; y < gridSize; y++) {
|
|
for (int32_t x = 0; x < gridSize; x++) {
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
|
|
model.bindBuffers(drawCmdBuffers[i]);
|
|
glm::vec3 pos = glm::vec3(float(x - (gridSize / 2.0f)) * 2.5f, 0.0f, float(y - (gridSize / 2.0f)) * 2.5f);
|
|
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
|
|
model.draw(drawCmdBuffers[i]);
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLines);
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &box.vertices.buffer, offsets);
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], box.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], box.indexCount, 1, 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
drawUI(drawCmdBuffers[i]);
|
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
|
}
|
|
}
|
|
|
|
void loadAssets()
|
|
{
|
|
model.loadFromFile(getAssetPath() + "models/retroufo_red_lowpoly.gltf", vulkanDevice, queue, vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::FlipY | vkglTF::FileLoadingFlags::PreMultiplyVertexColors);
|
|
// @todo
|
|
generateBox(glm::vec3(1.0));
|
|
// generateBox(model.dimensions.size);
|
|
}
|
|
|
|
void setupDescriptors()
|
|
{
|
|
// Pool
|
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3)
|
|
};
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3);
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
|
|
|
// Layout
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0)
|
|
};
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
|
|
|
// Set
|
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
|
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor)
|
|
};
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
|
}
|
|
|
|
void preparePipelines()
|
|
{
|
|
// Layout
|
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
|
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0);
|
|
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
|
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
|
|
|
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.data(), static_cast<uint32_t>(dynamicStateEnables.size()), 0);
|
|
VkPipelineTessellationStateCreateInfo tessellationState = vks::initializers::pipelineTessellationStateCreateInfo(3);
|
|
std::vector<VkPipelineShaderStageCreateInfo> shaderStages(2);
|
|
|
|
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;
|
|
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({ vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Normal, vkglTF::VertexComponent::Color });
|
|
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
|
pipelineCI.pStages = shaderStages.data();
|
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "linerendering/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getShadersPath() + "linerendering/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline));
|
|
|
|
// Line rendering
|
|
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
|
|
rasterizationState.cullMode = VK_CULL_MODE_NONE;
|
|
|
|
// Vertex bindings and attributes
|
|
VkVertexInputBindingDescription vertexInputBinding = vks::initializers::vertexInputBindingDescription(0, sizeof(glm::vec3), VK_VERTEX_INPUT_RATE_VERTEX);
|
|
VkVertexInputAttributeDescription vertexInputAttribute = vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, 0);
|
|
VkPipelineVertexInputStateCreateInfo vertexInputStateCI = vks::initializers::pipelineVertexInputStateCreateInfo();
|
|
vertexInputStateCI.vertexBindingDescriptionCount = 1;
|
|
vertexInputStateCI.pVertexBindingDescriptions = &vertexInputBinding;
|
|
vertexInputStateCI.vertexAttributeDescriptionCount = 1;
|
|
vertexInputStateCI.pVertexAttributeDescriptions = &vertexInputAttribute;
|
|
|
|
pipelineCI.pVertexInputState = &vertexInputStateCI;
|
|
|
|
VkPipelineRasterizationLineStateCreateInfoEXT lineRasterizationStateCI{};
|
|
lineRasterizationStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_KHR;
|
|
lineRasterizationStateCI.lineRasterizationMode = VK_LINE_RASTERIZATION_MODE_RECTANGULAR_SMOOTH_KHR;
|
|
lineRasterizationStateCI.stippledLineEnable = VK_TRUE;
|
|
lineRasterizationStateCI.lineStipplePattern = 0b01010101;
|
|
lineRasterizationStateCI.lineStippleFactor = 32;
|
|
|
|
rasterizationState.pNext = &lineRasterizationStateCI;
|
|
|
|
//pipelineCI.pNext = &lineRasterizationStateCI;
|
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "linerendering/line.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getShadersPath() + "linerendering/line.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelineLines));
|
|
|
|
}
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
void prepareUniformBuffers()
|
|
{
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffer, sizeof(UniformData)));
|
|
VK_CHECK_RESULT(uniformBuffer.map());
|
|
}
|
|
|
|
void updateUniformBuffers()
|
|
{
|
|
uniformData.projection = camera.matrices.perspective;
|
|
uniformData.modelview = camera.matrices.view;
|
|
memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
|
|
}
|
|
|
|
void prepare()
|
|
{
|
|
VulkanExampleBase::prepare();
|
|
|
|
//vkCmdSetLineRasterizationModeEXT = reinterpret_cast<PFN_vkCmdSetLineRasterizationModeEXT>(vkGetDeviceProcAddr(device, "vkCmdSetLineRasterizationModeEXT"));
|
|
//vkCmdSetLineStippleEnableEXT = reinterpret_cast<PFN_vkCmdSetLineStippleEnableEXT>(vkGetDeviceProcAddr(device, "vkCmdSetLineStippleEnableEXT"));
|
|
//vkCmdSetLineStippleEXT = reinterpret_cast<PFN_vkCmdSetLineStippleEXT>(vkGetDeviceProcAddr(device, "vkCmdSetLineStippleEXT"));
|
|
|
|
loadAssets();
|
|
prepareUniformBuffers();
|
|
setupDescriptors();
|
|
preparePipelines();
|
|
buildCommandBuffers();
|
|
prepared = true;
|
|
}
|
|
|
|
void draw()
|
|
{
|
|
VulkanExampleBase::prepareFrame();
|
|
submitInfo.commandBufferCount = 1;
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
VulkanExampleBase::submitFrame();
|
|
}
|
|
|
|
virtual void render()
|
|
{
|
|
if (!prepared)
|
|
return;
|
|
updateUniformBuffers();
|
|
draw();
|
|
}
|
|
|
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
|
{
|
|
// @todo
|
|
}
|
|
|
|
};
|
|
|
|
VULKAN_EXAMPLE_MAIN() |