Remove unnecessary buffer map and unmap
Code cleanup
This commit is contained in:
parent
5693fc0b6e
commit
1201b8d10c
1 changed files with 22 additions and 83 deletions
|
|
@ -157,19 +157,16 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
VkImageAspectFlags aspectMask = 0;
|
VkImageAspectFlags aspectMask = 0;
|
||||||
VkImageLayout imageLayout;
|
|
||||||
|
|
||||||
attachment->format = format;
|
attachment->format = format;
|
||||||
|
|
||||||
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
||||||
{
|
{
|
||||||
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
||||||
}
|
}
|
||||||
if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
|
if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
|
||||||
{
|
{
|
||||||
aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
||||||
imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(aspectMask > 0);
|
assert(aspectMask > 0);
|
||||||
|
|
@ -535,63 +532,34 @@ public:
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4),
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 4),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 4),
|
||||||
};
|
};
|
||||||
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo( static_cast<uint32_t>(poolSizes.size()), poolSizes.data(), 4);
|
||||||
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
||||||
vks::initializers::descriptorPoolCreateInfo(
|
|
||||||
static_cast<uint32_t>(poolSizes.size()),
|
|
||||||
poolSizes.data(),
|
|
||||||
4);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSetLayout()
|
void setupDescriptorSetLayout()
|
||||||
{
|
{
|
||||||
// Deferred shading layout
|
// Deferred shading layout
|
||||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
||||||
{
|
|
||||||
// Binding 0 : Vertex shader uniform buffer
|
// Binding 0 : Vertex shader uniform buffer
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0)
|
||||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
VK_SHADER_STAGE_VERTEX_BIT,
|
|
||||||
0)
|
|
||||||
};
|
};
|
||||||
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
|
||||||
vks::initializers::descriptorSetLayoutCreateInfo(
|
|
||||||
setLayoutBindings.data(),
|
|
||||||
static_cast<uint32_t>(setLayoutBindings.size()));
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.scene));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.scene));
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
|
||||||
vks::initializers::pipelineLayoutCreateInfo(
|
|
||||||
&descriptorSetLayouts.scene,
|
|
||||||
1);
|
|
||||||
|
|
||||||
// Offscreen (scene) rendering pipeline layout
|
// Offscreen (scene) rendering pipeline layout
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.scene, 1);
|
||||||
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSet()
|
void setupDescriptorSet()
|
||||||
{
|
{
|
||||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
||||||
|
|
||||||
VkDescriptorSetAllocateInfo allocInfo =
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.scene, 1);
|
||||||
vks::initializers::descriptorSetAllocateInfo(
|
|
||||||
descriptorPool,
|
|
||||||
&descriptorSetLayouts.scene,
|
|
||||||
1);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene));
|
||||||
writeDescriptorSets =
|
writeDescriptorSets = {
|
||||||
{
|
|
||||||
// Binding 0: Vertex shader uniform buffer
|
// Binding 0: Vertex shader uniform buffer
|
||||||
vks::initializers::writeDescriptorSet(
|
vks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.GBuffer.descriptor)
|
||||||
descriptorSets.scene,
|
|
||||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
0,
|
|
||||||
&uniformBuffers.GBuffer.descriptor)
|
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
@ -646,25 +614,13 @@ public:
|
||||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
||||||
{
|
{
|
||||||
// Binding 0: Position input attachment
|
// Binding 0: Position input attachment
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, VK_SHADER_STAGE_FRAGMENT_BIT, 0),
|
||||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
|
||||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
0),
|
|
||||||
// Binding 1: Normal input attachment
|
// Binding 1: Normal input attachment
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, VK_SHADER_STAGE_FRAGMENT_BIT, 1),
|
||||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
|
||||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
1),
|
|
||||||
// Binding 2: Albedo input attachment
|
// Binding 2: Albedo input attachment
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, VK_SHADER_STAGE_FRAGMENT_BIT, 2),
|
||||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
|
||||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
2),
|
|
||||||
// Binding 3: Light positions
|
// Binding 3: Light positions
|
||||||
vks::initializers::descriptorSetLayoutBinding(
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 3),
|
||||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
3),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
||||||
|
|
@ -675,15 +631,11 @@ public:
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.composition));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.composition));
|
||||||
|
|
||||||
// Pipeline layout
|
// Pipeline layout
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.composition, 1);
|
||||||
vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.composition, 1);
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayouts.composition));
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.composition));
|
|
||||||
|
|
||||||
// Descriptor sets
|
// Descriptor sets
|
||||||
VkDescriptorSetAllocateInfo allocInfo =
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.composition, 1);
|
||||||
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.composition, 1);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.composition));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.composition));
|
||||||
|
|
||||||
// Image descriptors for the offscreen color attachments
|
// Image descriptors for the offscreen color attachments
|
||||||
|
|
@ -770,8 +722,8 @@ public:
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.transparent));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.transparent));
|
||||||
|
|
||||||
// Pipeline layout
|
// Pipeline layout
|
||||||
pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.transparent, 1);
|
pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.transparent, 1);
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.transparent));
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayouts.transparent));
|
||||||
|
|
||||||
// Descriptor sets
|
// Descriptor sets
|
||||||
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.transparent, 1);
|
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.transparent, 1);
|
||||||
|
|
@ -807,18 +759,12 @@ public:
|
||||||
void prepareUniformBuffers()
|
void prepareUniformBuffers()
|
||||||
{
|
{
|
||||||
// Deferred vertex shader
|
// Deferred vertex shader
|
||||||
vulkanDevice->createBuffer(
|
vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.GBuffer, sizeof(uboGBuffer));
|
||||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
VK_CHECK_RESULT(uniformBuffers.GBuffer.map());
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
||||||
&uniformBuffers.GBuffer,
|
|
||||||
sizeof(uboGBuffer));
|
|
||||||
|
|
||||||
// Deferred fragment shader
|
// Deferred fragment shader
|
||||||
vulkanDevice->createBuffer(
|
vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.lights, sizeof(uboLights));
|
||||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
VK_CHECK_RESULT(uniformBuffers.lights.map());
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
||||||
&uniformBuffers.lights,
|
|
||||||
sizeof(uboLights));
|
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
updateUniformBufferDeferredMatrices();
|
updateUniformBufferDeferredMatrices();
|
||||||
|
|
@ -830,10 +776,7 @@ public:
|
||||||
uboGBuffer.projection = camera.matrices.perspective;
|
uboGBuffer.projection = camera.matrices.perspective;
|
||||||
uboGBuffer.view = camera.matrices.view;
|
uboGBuffer.view = camera.matrices.view;
|
||||||
uboGBuffer.model = glm::mat4(1.0f);
|
uboGBuffer.model = glm::mat4(1.0f);
|
||||||
|
|
||||||
VK_CHECK_RESULT(uniformBuffers.GBuffer.map());
|
|
||||||
memcpy(uniformBuffers.GBuffer.mapped, &uboGBuffer, sizeof(uboGBuffer));
|
memcpy(uniformBuffers.GBuffer.mapped, &uboGBuffer, sizeof(uboGBuffer));
|
||||||
uniformBuffers.GBuffer.unmap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initLights()
|
void initLights()
|
||||||
|
|
@ -862,12 +805,8 @@ public:
|
||||||
// Update fragment shader light position uniform block
|
// Update fragment shader light position uniform block
|
||||||
void updateUniformBufferDeferredLights()
|
void updateUniformBufferDeferredLights()
|
||||||
{
|
{
|
||||||
// Current view position
|
|
||||||
uboLights.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
|
uboLights.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
|
||||||
|
|
||||||
VK_CHECK_RESULT(uniformBuffers.lights.map());
|
|
||||||
memcpy(uniformBuffers.lights.mapped, &uboLights, sizeof(uboLights));
|
memcpy(uniformBuffers.lights.mapped, &uboLights, sizeof(uboLights));
|
||||||
uniformBuffers.lights.unmap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
void draw()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue