Cleaneer descriptor set layout and pipeline naming, enabled culling, fixed validation warnings for image extents

This commit is contained in:
saschawillems 2016-06-01 22:50:00 +02:00
parent ee8478cdec
commit 598b904a60
6 changed files with 88 additions and 82 deletions

View file

@ -1,5 +1,5 @@
glslangvalidator -V offscreen.vert -o offscreen.vert.spv glslangvalidator -V phong.vert -o phong.vert.spv
glslangvalidator -V offscreen.frag -o offscreen.frag.spv glslangvalidator -V phong.frag -o phong.frag.spv
glslangvalidator -V quad.vert -o quad.vert.spv glslangvalidator -V quad.vert -o quad.vert.spv
glslangvalidator -V quad.frag -o quad.frag.spv glslangvalidator -V quad.frag -o quad.frag.spv
glslangvalidator -V mirror.vert -o mirror.vert.spv glslangvalidator -V mirror.vert -o mirror.vert.spv

View file

@ -82,12 +82,13 @@ public:
struct { struct {
VkPipeline debug; VkPipeline debug;
VkPipeline shaded; VkPipeline shaded;
VkPipeline shadedOffscreen;
VkPipeline mirror; VkPipeline mirror;
} pipelines; } pipelines;
struct { struct {
VkPipelineLayout quad; VkPipelineLayout textured;
VkPipelineLayout offscreen; VkPipelineLayout shaded;
} pipelineLayouts; } pipelineLayouts;
struct { struct {
@ -97,7 +98,10 @@ public:
VkDescriptorSet debugQuad; VkDescriptorSet debugQuad;
} descriptorSets; } descriptorSets;
VkDescriptorSetLayout descriptorSetLayout; struct {
VkDescriptorSetLayout textured;
VkDescriptorSetLayout shaded;
} descriptorSetLayouts;
// Framebuffer for offscreen rendering // Framebuffer for offscreen rendering
struct FrameBufferAttachment { struct FrameBufferAttachment {
@ -151,12 +155,14 @@ public:
vkDestroyPipeline(device, pipelines.debug, nullptr); vkDestroyPipeline(device, pipelines.debug, nullptr);
vkDestroyPipeline(device, pipelines.shaded, nullptr); vkDestroyPipeline(device, pipelines.shaded, nullptr);
vkDestroyPipeline(device, pipelines.shadedOffscreen, nullptr);
vkDestroyPipeline(device, pipelines.mirror, nullptr); vkDestroyPipeline(device, pipelines.mirror, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.quad, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.textured, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.shaded, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.shaded, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textured, nullptr);
// Meshes // Meshes
vkMeshLoader::freeMeshBufferResources(device, &meshes.example); vkMeshLoader::freeMeshBufferResources(device, &meshes.example);
@ -275,6 +281,7 @@ public:
image.format = fbColorFormat; image.format = fbColorFormat;
image.extent.width = offScreenFrameBuf.width; image.extent.width = offScreenFrameBuf.width;
image.extent.height = offScreenFrameBuf.height; image.extent.height = offScreenFrameBuf.height;
image.extent.depth = 1;
image.mipLevels = 1; image.mipLevels = 1;
image.arrayLayers = 1; image.arrayLayers = 1;
image.samples = VK_SAMPLE_COUNT_1_BIT; image.samples = VK_SAMPLE_COUNT_1_BIT;
@ -408,9 +415,9 @@ public:
VkDeviceSize offsets[1] = { 0 }; VkDeviceSize offsets[1] = { 0 };
// Model // Mirrored scene
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.offscreen, 0, NULL); vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.shaded, 0, 1, &descriptorSets.offscreen, 0, NULL);
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shaded); vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shadedOffscreen);
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets); vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets);
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.example.indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.example.indices.buf, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.example.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(offScreenCmdBuffer, meshes.example.indexCount, 1, 0, 0, 0);
@ -536,7 +543,7 @@ public:
if (debugDisplay) if (debugDisplay)
{ {
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.quad, 0, 1, &descriptorSets.debugQuad, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.textured, 0, 1, &descriptorSets.debugQuad, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32);
@ -547,7 +554,7 @@ public:
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug);
// Reflection plane // Reflection plane
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.quad, 0, 1, &descriptorSets.mirror, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.textured, 0, 1, &descriptorSets.mirror, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.mirror); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.mirror);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.plane.vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.plane.vertices.buf, offsets);
@ -555,7 +562,7 @@ public:
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.plane.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.plane.indexCount, 1, 0, 0, 0);
// Model // Model
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.quad, 0, 1, &descriptorSets.model, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.shaded, 0, 1, &descriptorSets.model, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shaded); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shaded);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets);
@ -688,42 +695,39 @@ public:
void setupDescriptorSetLayout() void setupDescriptorSetLayout()
{ {
// Textured quad pipeline layout std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings;
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo;
{ VkPipelineLayoutCreateInfo pipelineLayoutInfo;
// Binding 0 : Vertex shader uniform buffer // Binding 0 : Vertex shader uniform buffer
vkTools::initializers::descriptorSetLayoutBinding( setLayoutBindings.push_back(vkTools::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_VERTEX_BIT, VK_SHADER_STAGE_VERTEX_BIT,
0), 0));
// Binding 1 : Fragment shader image sampler // Binding 1 : Fragment shader image sampler
vkTools::initializers::descriptorSetLayoutBinding( setLayoutBindings.push_back(vkTools::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT, VK_SHADER_STAGE_FRAGMENT_BIT,
1), 1));
// Binding 2 : Fragment shader image sampler // Binding 2 : Fragment shader image sampler
vkTools::initializers::descriptorSetLayoutBinding( setLayoutBindings.push_back(vkTools::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT, VK_SHADER_STAGE_FRAGMENT_BIT,
2) 2));
};
VkDescriptorSetLayoutCreateInfo descriptorLayout = // Shaded layouts (only use first layout binding)
vkTools::initializers::descriptorSetLayoutCreateInfo( descriptorLayoutInfo = vkTools::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), 1);
setLayoutBindings.data(), VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.shaded));
setLayoutBindings.size());
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); pipelineLayoutInfo = vkTools::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.shaded, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.shaded));
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = // Textured layouts (use all layout bindings)
vkTools::initializers::pipelineLayoutCreateInfo( descriptorLayoutInfo = vkTools::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
&descriptorSetLayout, VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.textured));
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.quad)); pipelineLayoutInfo = vkTools::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.textured, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayouts.textured));
// Offscreen pipeline layout
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
} }
void setupDescriptorSet() void setupDescriptorSet()
@ -732,7 +736,7 @@ public:
VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo allocInfo =
vkTools::initializers::descriptorSetAllocateInfo( vkTools::initializers::descriptorSetAllocateInfo(
descriptorPool, descriptorPool,
&descriptorSetLayout, &descriptorSetLayouts.textured,
1); 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.mirror)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.mirror));
@ -775,6 +779,29 @@ public:
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
// Debug quad
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.debugQuad));
std::vector<VkWriteDescriptorSet> debugQuadWriteDescriptorSets =
{
// Binding 0 : Vertex shader uniform buffer
vkTools::initializers::writeDescriptorSet(
descriptorSets.debugQuad,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformData.vsDebugQuad.descriptor),
// Binding 1 : Fragment shader texture sampler
vkTools::initializers::writeDescriptorSet(
descriptorSets.debugQuad,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
&texDescriptorMirror)
};
vkUpdateDescriptorSets(device, debugQuadWriteDescriptorSets.size(), debugQuadWriteDescriptorSets.data(), 0, NULL);
// Shaded descriptor sets
allocInfo.pSetLayouts = &descriptorSetLayouts.shaded;
// Model // Model
// No texture // No texture
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.model)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.model));
@ -803,26 +830,6 @@ public:
&uniformData.vsOffScreen.descriptor) &uniformData.vsOffScreen.descriptor)
}; };
vkUpdateDescriptorSets(device, offScreenWriteDescriptorSets.size(), offScreenWriteDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, offScreenWriteDescriptorSets.size(), offScreenWriteDescriptorSets.data(), 0, NULL);
// Debug quad
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.debugQuad));
std::vector<VkWriteDescriptorSet> debugQuadWriteDescriptorSets =
{
// Binding 0 : Vertex shader uniform buffer
vkTools::initializers::writeDescriptorSet(
descriptorSets.debugQuad,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformData.vsDebugQuad.descriptor),
// Binding 1 : Fragment shader texture sampler
vkTools::initializers::writeDescriptorSet(
descriptorSets.debugQuad,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
&texDescriptorMirror)
};
vkUpdateDescriptorSets(device, debugQuadWriteDescriptorSets.size(), debugQuadWriteDescriptorSets.data(), 0, NULL);
} }
void preparePipelines() void preparePipelines()
@ -836,7 +843,7 @@ public:
VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo rasterizationState =
vkTools::initializers::pipelineRasterizationStateCreateInfo( vkTools::initializers::pipelineRasterizationStateCreateInfo(
VK_POLYGON_MODE_FILL, VK_POLYGON_MODE_FILL,
VK_CULL_MODE_NONE, VK_CULL_MODE_FRONT_BIT,
VK_FRONT_FACE_CLOCKWISE, VK_FRONT_FACE_CLOCKWISE,
0); 0);
@ -883,7 +890,7 @@ public:
VkGraphicsPipelineCreateInfo pipelineCreateInfo = VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vkTools::initializers::pipelineCreateInfo( vkTools::initializers::pipelineCreateInfo(
pipelineLayouts.quad, pipelineLayouts.textured,
renderPass, renderPass,
0); 0);
@ -900,19 +907,25 @@ public:
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug));
// Flip culling
rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
// Mirror // Mirror
shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.mirror)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.mirror));
// Solid shading pipeline // Phong shading pipelines
shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); pipelineCreateInfo.layout = pipelineLayouts.shaded;
shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); // Scene
shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/phong.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
pipelineCreateInfo.layout = pipelineLayouts.offscreen; shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/phong.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.shaded)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.shaded));
// Offscreen
// Flip culling
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.shadedOffscreen));
} }
// Prepare and initialize uniform buffer containing shader uniforms // Prepare and initialize uniform buffer containing shader uniforms
@ -1061,14 +1074,7 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
vkDeviceWaitIdle(device);
draw(); draw();
vkDeviceWaitIdle(device);
if (!paused)
{
updateUniformBuffers();
updateUniformBufferOffscreen();
}
} }
virtual void viewChanged() virtual void viewChanged()