2018-10-15 19:52:16 -04:00
|
|
|
#include "worldpass.h"
|
|
|
|
|
|
|
|
#include <array>
|
2018-10-16 08:58:25 -04:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2018-10-15 19:52:16 -04:00
|
|
|
|
|
|
|
#include "renderer.h"
|
2018-10-16 08:49:25 -04:00
|
|
|
#include "world.h"
|
|
|
|
#include "mesh.h"
|
2018-10-18 21:46:48 -04:00
|
|
|
#include "light.h"
|
2018-10-25 08:57:36 -04:00
|
|
|
#include "camera.h"
|
2018-11-06 09:08:55 -05:00
|
|
|
#include "material.h"
|
2018-10-15 19:52:16 -04:00
|
|
|
|
|
|
|
WorldPass::WorldPass(Renderer& renderer) : renderer_(renderer) {
|
2018-10-17 09:28:47 -04:00
|
|
|
createRenderPass();
|
2018-10-18 21:46:48 -04:00
|
|
|
createDescriptorSetLayout();
|
2018-10-15 19:52:16 -04:00
|
|
|
createPipeline();
|
2018-10-18 21:46:48 -04:00
|
|
|
createUniformBuffer();
|
|
|
|
createDescriptorSet();
|
2018-10-15 19:52:16 -04:00
|
|
|
}
|
|
|
|
|
2018-10-15 20:03:01 -04:00
|
|
|
WorldPass::~WorldPass() {
|
2018-10-17 10:12:34 -04:00
|
|
|
vkDestroyRenderPass(renderer_.getDevice(), renderPass_, nullptr);
|
|
|
|
|
2018-10-25 08:40:24 -04:00
|
|
|
vkDestroyDescriptorSetLayout(renderer_.getDevice(), setLayout_, nullptr);
|
2018-10-24 19:29:44 -04:00
|
|
|
|
2018-10-15 20:03:01 -04:00
|
|
|
vkDestroyPipeline(renderer_.getDevice(), pipeline_, nullptr);
|
|
|
|
vkDestroyPipelineLayout(renderer_.getDevice(), pipelineLayout_, nullptr);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-24 19:29:44 -04:00
|
|
|
vkFreeMemory(renderer_.getDevice(), lightMemory_, nullptr);
|
|
|
|
vkDestroyBuffer(renderer_.getDevice(), lightBuffer_, nullptr);
|
2018-10-15 20:03:01 -04:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:57:36 -04:00
|
|
|
void WorldPass::render(VkCommandBuffer commandBuffer, World& world, Camera& camera, RenderTarget* target) {
|
2018-10-18 21:46:48 -04:00
|
|
|
struct ShaderLight {
|
2018-11-08 13:51:08 -05:00
|
|
|
glm::vec4 position;
|
2018-10-18 21:46:48 -04:00
|
|
|
glm::vec3 color;
|
|
|
|
};
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
ShaderLight* data;
|
2018-11-08 13:51:08 -05:00
|
|
|
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-26 20:56:06 -04:00
|
|
|
for(const auto& light : world.lights) {
|
2018-11-08 13:51:08 -05:00
|
|
|
glm::vec3 position;
|
|
|
|
if(light->type == LightType::Point)
|
|
|
|
position = light->position;
|
|
|
|
else
|
|
|
|
position = glm::normalize(glm::vec3(0) - light->position);
|
|
|
|
|
|
|
|
data->position = glm::vec4(position, ((int)light->type) + 1);
|
2018-10-26 20:56:06 -04:00
|
|
|
data->color = light->color;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
data++;
|
|
|
|
}
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
vkUnmapMemory(renderer_.getDevice(), lightMemory_);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
|
2018-10-18 21:46:48 -04:00
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &descriptorSet_, 0, nullptr);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-16 08:49:25 -04:00
|
|
|
for(const auto& mesh : world.meshes) {
|
2018-11-06 09:08:55 -05:00
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 1, 1, &mesh->material->set, 0, nullptr);
|
|
|
|
|
2018-11-08 08:57:41 -05:00
|
|
|
glm::mat4 vp;
|
|
|
|
vp = glm::perspective(glm::radians(75.0f), (float)target->extent.width / target->extent.height, camera.near, camera.far);
|
|
|
|
vp *= glm::lookAt(camera.position, camera.target, glm::vec3(0, -1, 0));
|
2018-10-16 08:58:25 -04:00
|
|
|
|
2018-11-08 08:57:41 -05:00
|
|
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &vp);
|
|
|
|
|
|
|
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), sizeof(glm::mat4), &world.lights[0]->matrix);
|
2018-10-16 08:58:25 -04:00
|
|
|
|
2018-10-16 08:49:25 -04:00
|
|
|
VkDeviceSize offsets[] = {0};
|
|
|
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &mesh->vertexBuffer, offsets);
|
|
|
|
vkCmdBindIndexBuffer(commandBuffer, mesh->indexBuffer, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(commandBuffer, mesh->indices.size(), 1, 0, 0, 0);
|
|
|
|
}
|
2018-10-17 09:28:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorldPass::createRenderPass() {
|
|
|
|
VkAttachmentDescription colorAttachment = {};
|
|
|
|
colorAttachment.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
|
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2018-11-06 13:47:33 -05:00
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-24 19:20:23 -04:00
|
|
|
VkAttachmentDescription depthAttachment = {};
|
|
|
|
depthAttachment.format = VK_FORMAT_D32_SFLOAT;
|
|
|
|
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2018-11-06 13:47:33 -05:00
|
|
|
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
2018-10-17 09:28:47 -04:00
|
|
|
|
|
|
|
VkAttachmentReference colorAttachmentRef = {};
|
|
|
|
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
|
2018-10-24 19:20:23 -04:00
|
|
|
VkAttachmentReference depthAttachmentRef = {};
|
|
|
|
depthAttachmentRef.attachment = 1;
|
|
|
|
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-17 09:28:47 -04:00
|
|
|
VkSubpassDescription subpass = {};
|
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
|
|
subpass.colorAttachmentCount = 1;
|
|
|
|
subpass.pColorAttachments = &colorAttachmentRef;
|
2018-10-24 19:20:23 -04:00
|
|
|
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-24 19:20:23 -04:00
|
|
|
const std::array<VkAttachmentDescription, 2> attachments = {
|
|
|
|
colorAttachment,
|
|
|
|
depthAttachment
|
|
|
|
};
|
2018-10-17 09:28:47 -04:00
|
|
|
|
2018-11-06 13:47:33 -05:00
|
|
|
std::array<VkSubpassDependency, 2> dependencies;
|
|
|
|
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
|
|
|
|
dependencies[0].dstSubpass = 0;
|
|
|
|
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
|
|
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
|
|
|
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
|
|
|
|
|
|
|
dependencies[1].srcSubpass = 0;
|
|
|
|
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
|
|
|
|
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
|
|
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
|
|
|
dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
|
|
|
|
2018-10-17 09:28:47 -04:00
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
2018-10-24 19:20:23 -04:00
|
|
|
renderPassInfo.attachmentCount = attachments.size();
|
|
|
|
renderPassInfo.pAttachments = attachments.data();
|
2018-10-17 09:28:47 -04:00
|
|
|
renderPassInfo.subpassCount = 1;
|
|
|
|
renderPassInfo.pSubpasses = &subpass;
|
2018-11-06 13:47:33 -05:00
|
|
|
renderPassInfo.dependencyCount = dependencies.size();
|
|
|
|
renderPassInfo.pDependencies = dependencies.data();
|
2018-10-17 09:28:47 -04:00
|
|
|
|
|
|
|
vkCreateRenderPass(renderer_.getDevice(), &renderPassInfo, nullptr, &renderPass_);
|
2018-10-15 19:52:16 -04:00
|
|
|
}
|
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
void WorldPass::createDescriptorSetLayout() {
|
|
|
|
VkDescriptorSetLayoutBinding lightBufferBinding = {};
|
|
|
|
lightBufferBinding.descriptorCount = 1;
|
|
|
|
lightBufferBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
|
|
lightBufferBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-11-08 08:57:41 -05:00
|
|
|
VkDescriptorSetLayoutBinding shadowBinding = {};
|
|
|
|
shadowBinding.descriptorCount = 1;
|
|
|
|
shadowBinding.binding = 1;
|
|
|
|
shadowBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
shadowBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
|
|
|
|
const std::array<VkDescriptorSetLayoutBinding, 2> bindings = {
|
|
|
|
lightBufferBinding,
|
|
|
|
shadowBinding
|
|
|
|
};
|
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
VkDescriptorSetLayoutCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
2018-11-08 08:57:41 -05:00
|
|
|
createInfo.bindingCount = bindings.size();
|
|
|
|
createInfo.pBindings = bindings.data();
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
vkCreateDescriptorSetLayout(renderer_.getDevice(), &createInfo, nullptr, &setLayout_);
|
|
|
|
}
|
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
void WorldPass::createPipeline() {
|
2018-11-05 21:12:08 -05:00
|
|
|
VkShaderModule vertShaderModule = renderer_.createShader("shaders/mesh.vert.spv");
|
|
|
|
VkShaderModule fragShaderModule = renderer_.createShader("shaders/mesh.frag.spv");
|
2018-10-15 19:52:16 -04:00
|
|
|
|
|
|
|
VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
|
|
|
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
|
|
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
vertShaderStageInfo.module = vertShaderModule;
|
|
|
|
vertShaderStageInfo.pName = "main";
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-11-08 13:51:08 -05:00
|
|
|
VkSpecializationMapEntry mapEntry = {};
|
|
|
|
mapEntry.size = sizeof(int);
|
|
|
|
|
|
|
|
VkSpecializationInfo specializationInfo = {};
|
|
|
|
specializationInfo.mapEntryCount = 1;
|
|
|
|
specializationInfo.pMapEntries = &mapEntry;
|
|
|
|
specializationInfo.dataSize = sizeof(int);
|
|
|
|
|
|
|
|
int shadowFilterPCF = renderer_.getConfig().filterPCF;
|
|
|
|
specializationInfo.pData = &shadowFilterPCF;
|
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
|
|
|
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
|
|
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
fragShaderStageInfo.module = fragShaderModule;
|
|
|
|
fragShaderStageInfo.pName = "main";
|
2018-11-08 13:51:08 -05:00
|
|
|
fragShaderStageInfo.pSpecializationInfo = &specializationInfo;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertShaderStageInfo, fragShaderStageInfo};
|
2018-10-16 08:49:25 -04:00
|
|
|
|
|
|
|
VkVertexInputBindingDescription vertexBindingDescription = {};
|
|
|
|
vertexBindingDescription.stride = sizeof(Vertex);
|
|
|
|
|
|
|
|
VkVertexInputAttributeDescription positionAttributeDescription = {};
|
|
|
|
positionAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT;
|
|
|
|
|
2018-10-16 13:03:26 -04:00
|
|
|
VkVertexInputAttributeDescription normalAttributeDescription = {};
|
|
|
|
normalAttributeDescription.location = 1;
|
|
|
|
normalAttributeDescription.offset = offsetof(Vertex, normal);
|
|
|
|
normalAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT;
|
2018-10-17 09:28:47 -04:00
|
|
|
|
2018-11-06 09:08:55 -05:00
|
|
|
VkVertexInputAttributeDescription uvAttributeDescription = {};
|
|
|
|
uvAttributeDescription.location = 2;
|
|
|
|
uvAttributeDescription.offset = offsetof(Vertex, uv);
|
|
|
|
uvAttributeDescription .format = VK_FORMAT_R32G32_SFLOAT;
|
|
|
|
|
|
|
|
const std::array<VkVertexInputAttributeDescription, 3> attributes = {
|
2018-10-17 09:28:47 -04:00
|
|
|
positionAttributeDescription,
|
2018-11-06 09:08:55 -05:00
|
|
|
normalAttributeDescription,
|
|
|
|
uvAttributeDescription
|
2018-10-16 13:03:26 -04:00
|
|
|
};
|
2018-10-17 09:28:47 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
|
|
|
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
2018-10-16 08:49:25 -04:00
|
|
|
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
|
|
|
vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDescription;
|
2018-10-16 13:03:26 -04:00
|
|
|
vertexInputInfo.vertexAttributeDescriptionCount = attributes.size();
|
|
|
|
vertexInputInfo.pVertexAttributeDescriptions = attributes.data();
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
|
|
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
|
|
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineViewportStateCreateInfo viewportState = {};
|
|
|
|
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
|
|
|
viewportState.viewportCount = 1;
|
|
|
|
viewportState.scissorCount = 1;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
|
|
|
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
2018-10-16 08:49:25 -04:00
|
|
|
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
2018-10-15 19:52:16 -04:00
|
|
|
rasterizer.lineWidth = 1.0f;
|
|
|
|
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
|
|
|
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineMultisampleStateCreateInfo multisampling = {};
|
|
|
|
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
|
|
|
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
|
|
|
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
|
|
|
colorBlendAttachment.blendEnable = VK_FALSE;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-11-07 07:20:52 -05:00
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
|
|
|
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
|
|
|
colorBlending.attachmentCount = 1;
|
|
|
|
colorBlending.pAttachments = &colorBlendAttachment;
|
|
|
|
|
2018-10-24 19:20:23 -04:00
|
|
|
VkPipelineDepthStencilStateCreateInfo depthState = {};
|
|
|
|
depthState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
2018-10-26 20:56:06 -04:00
|
|
|
depthState.depthTestEnable = VK_TRUE;
|
|
|
|
depthState.depthWriteEnable = VK_TRUE;
|
2018-10-24 19:20:23 -04:00
|
|
|
depthState.depthCompareOp = VK_COMPARE_OP_LESS;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
const std::array<VkDynamicState, 2> dynamicStates = {
|
|
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
|
|
};
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = {};
|
|
|
|
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
|
|
|
dynamicState.dynamicStateCount = dynamicStates.size();
|
|
|
|
dynamicState.pDynamicStates = dynamicStates.data();
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-16 08:58:25 -04:00
|
|
|
VkPushConstantRange mvpPushConstant = {};
|
2018-11-08 08:57:41 -05:00
|
|
|
mvpPushConstant.size = sizeof(glm::mat4) * 2;
|
2018-10-16 08:58:25 -04:00
|
|
|
mvpPushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
|
2018-11-06 09:08:55 -05:00
|
|
|
const std::array<VkDescriptorSetLayout, 2> setLayouts = {
|
|
|
|
setLayout_,
|
|
|
|
renderer_.getMaterialSetLayout()
|
|
|
|
};
|
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
|
|
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
2018-11-06 09:08:55 -05:00
|
|
|
pipelineLayoutInfo.setLayoutCount = setLayouts.size();
|
|
|
|
pipelineLayoutInfo.pSetLayouts = setLayouts.data();
|
2018-10-16 08:58:25 -04:00
|
|
|
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
|
|
|
pipelineLayoutInfo.pPushConstantRanges = &mvpPushConstant;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
vkCreatePipelineLayout(renderer_.getDevice(), &pipelineLayoutInfo, nullptr, &pipelineLayout_);
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
|
|
|
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
|
|
|
pipelineInfo.stageCount = shaderStages.size();
|
|
|
|
pipelineInfo.pStages = shaderStages.data();
|
|
|
|
pipelineInfo.pVertexInputState = &vertexInputInfo;
|
|
|
|
pipelineInfo.pInputAssemblyState = &inputAssembly;
|
|
|
|
pipelineInfo.pViewportState = &viewportState;
|
|
|
|
pipelineInfo.pRasterizationState = &rasterizer;
|
|
|
|
pipelineInfo.pMultisampleState = &multisampling;
|
2018-10-24 19:20:23 -04:00
|
|
|
pipelineInfo.pDepthStencilState = &depthState;
|
2018-10-15 19:52:16 -04:00
|
|
|
pipelineInfo.pColorBlendState = &colorBlending;
|
|
|
|
pipelineInfo.pDynamicState = &dynamicState;
|
|
|
|
pipelineInfo.layout = pipelineLayout_;
|
2018-10-17 09:28:47 -04:00
|
|
|
pipelineInfo.renderPass = renderPass_;
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
vkCreateGraphicsPipelines(renderer_.getDevice(), nullptr, 1, &pipelineInfo, nullptr, &pipeline_);
|
2018-10-16 08:49:25 -04:00
|
|
|
|
2018-10-15 19:52:16 -04:00
|
|
|
vkDestroyShaderModule(renderer_.getDevice(), fragShaderModule, nullptr);
|
|
|
|
vkDestroyShaderModule(renderer_.getDevice(), vertShaderModule, nullptr);
|
|
|
|
}
|
2018-10-18 21:46:48 -04:00
|
|
|
|
|
|
|
void WorldPass::createUniformBuffer() {
|
|
|
|
VkBufferCreateInfo bufferInfo = {};
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
2018-11-08 13:51:08 -05:00
|
|
|
bufferInfo.size = sizeof(float) * (4 + 3) * 32;
|
2018-10-18 21:46:48 -04:00
|
|
|
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
vkCreateBuffer(renderer_.getDevice(), &bufferInfo, nullptr, &lightBuffer_);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetBufferMemoryRequirements(renderer_.getDevice(), lightBuffer_, &memRequirements);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
VkMemoryAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
|
|
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &lightMemory_);
|
|
|
|
vkBindBufferMemory(renderer_.getDevice(), lightBuffer_, lightMemory_, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorldPass::createDescriptorSet() {
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
|
|
|
allocInfo.descriptorPool = renderer_.getDescriptorPool();
|
|
|
|
allocInfo.descriptorSetCount = 1;
|
|
|
|
allocInfo.pSetLayouts = &setLayout_;
|
|
|
|
|
|
|
|
vkAllocateDescriptorSets(renderer_.getDevice(), &allocInfo, &descriptorSet_);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
VkDescriptorBufferInfo bufferInfo = {};
|
|
|
|
bufferInfo.buffer = lightBuffer_;
|
2018-11-08 13:51:08 -05:00
|
|
|
bufferInfo.range = sizeof(float) * (4 + 3) * 32;
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-11-08 08:57:41 -05:00
|
|
|
VkDescriptorImageInfo imageInfo = {};
|
|
|
|
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
imageInfo.imageView = renderer_.getShadowPass().getImageView();
|
|
|
|
imageInfo.sampler = renderer_.getShadowPass().getSampler();
|
|
|
|
|
|
|
|
VkWriteDescriptorSet bufferDescriptorWrite = {};
|
|
|
|
bufferDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
bufferDescriptorWrite.descriptorCount = 1;
|
|
|
|
bufferDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
|
|
bufferDescriptorWrite.dstSet = descriptorSet_;
|
|
|
|
bufferDescriptorWrite.pBufferInfo = &bufferInfo;
|
|
|
|
|
|
|
|
VkWriteDescriptorSet shadowDescriptorWrite = {};
|
|
|
|
shadowDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
shadowDescriptorWrite.descriptorCount = 1;
|
|
|
|
shadowDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
shadowDescriptorWrite.dstBinding = 1;
|
|
|
|
shadowDescriptorWrite.dstSet = descriptorSet_;
|
|
|
|
shadowDescriptorWrite.pImageInfo = &imageInfo;
|
|
|
|
|
|
|
|
const std::array<VkWriteDescriptorSet, 2> descriptorWrites = {
|
|
|
|
bufferDescriptorWrite,
|
|
|
|
shadowDescriptorWrite
|
|
|
|
};
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-11-08 08:57:41 -05:00
|
|
|
vkUpdateDescriptorSets(renderer_.getDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-10-18 21:46:48 -04:00
|
|
|
float* data;
|
2018-11-08 13:51:08 -05:00
|
|
|
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
2018-10-25 08:40:24 -04:00
|
|
|
|
2018-11-08 13:51:08 -05:00
|
|
|
for(uint32_t i = 0; i < (4 + 3) * 32; i++)
|
2018-10-18 21:46:48 -04:00
|
|
|
data[i] = 0.0f;
|
|
|
|
|
|
|
|
vkUnmapMemory(renderer_.getDevice(), lightMemory_);
|
|
|
|
}
|