Refactored geometry shader example, added normal map toggle, updated shaders, etc.

This commit is contained in:
saschawillems 2016-06-05 19:57:35 +02:00
parent 284465227a
commit 54801493aa
10 changed files with 119 additions and 109 deletions

View file

@ -8,6 +8,11 @@ layout (location = 1) in vec3 inNormal;
layout (location = 0) out vec3 outNormal; layout (location = 0) out vec3 outNormal;
out gl_PerVertex
{
vec4 gl_Position;
};
void main(void) void main(void)
{ {
outNormal = inNormal; outNormal = inNormal;

View file

@ -3,23 +3,21 @@
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable #extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec2 vTexcoord; layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 vNormal; layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 vColor; layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 vEyePos; layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec3 vLightVec;
layout (location = 0) out vec4 outFragColor; layout (location = 0) out vec4 outFragColor;
void main() void main()
{ {
vec3 Eye = normalize(-vEyePos); vec3 N = normalize(inNormal);
vec3 Reflected = normalize(reflect(-vLightVec, vNormal)); vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec4 IAmbient = vec4(0.2, 0.2, 0.2, 1.0); vec3 R = reflect(-L, N);
vec4 IDiffuse = vec4(0.5, 0.5, 0.5, 0.5) * max(dot(vNormal, vLightVec), 0.0); vec3 ambient = vec3(0.1);
float specular = 0.75; vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
vec4 ISpecular = vec4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 0.8) * specular; vec3 specular = pow(max(dot(R, V), 0.0), 16.0) * vec3(0.75);
outFragColor = vec4((ambient + diffuse) * inColor.rgb + specular, 1.0);
outFragColor = vec4((IAmbient + IDiffuse) * vec4(vColor, 1.0) + ISpecular);
} }

View file

@ -5,28 +5,34 @@
layout (location = 0) in vec4 inPos; layout (location = 0) in vec4 inPos;
layout (location = 1) in vec3 inNormal; layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inTexCoord; layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inColor;
layout (binding = 0) uniform UBO layout (set = 0, binding = 0) uniform UBO
{ {
mat4 projection; mat4 projection;
mat4 model; mat4 model;
} ubo; } ubo;
layout (location = 0) out vec2 vTexcoord; layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 vNormal; layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 vColor; layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 vEyePos; layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec3 vLightVec;
out gl_PerVertex
{
vec4 gl_Position;
};
void main() void main()
{ {
vTexcoord = inTexCoord.st; outNormal = inNormal;
vNormal = inNormal; outColor = inColor;
vColor = inColor;
gl_Position = ubo.projection * ubo.model * inPos; gl_Position = ubo.projection * ubo.model * inPos;
vEyePos = vec3(ubo.model * inPos);
vec4 lightPos = vec4(0.0, 0.0, 0.0, 1.0) * ubo.model; vec4 pos = ubo.model * vec4(inPos.xyz, 1.0);
vLightVec = normalize(lightPos.xyz - vEyePos); outNormal = mat3(ubo.model) * inNormal;
vec3 lightPos = vec3(1.0f, -1.0f, 1.0f);
outLightVec = lightPos.xyz - pos.xyz;
outViewVec = -pos.xyz;
} }

View file

@ -18,7 +18,7 @@ layout (location = 0) out vec3 outColor;
void main(void) void main(void)
{ {
float normalLength = 0.015; float normalLength = 0.02;
for(int i=0; i<gl_in.length(); i++) for(int i=0; i<gl_in.length(); i++)
{ {
vec3 pos = gl_in[i].gl_Position.xyz; vec3 pos = gl_in[i].gl_Position.xyz;

View file

@ -28,12 +28,14 @@ std::vector<vkMeshLoader::VertexLayout> vertexLayout =
{ {
vkMeshLoader::VERTEX_LAYOUT_POSITION, vkMeshLoader::VERTEX_LAYOUT_POSITION,
vkMeshLoader::VERTEX_LAYOUT_NORMAL, vkMeshLoader::VERTEX_LAYOUT_NORMAL,
vkMeshLoader::VERTEX_LAYOUT_UV vkMeshLoader::VERTEX_LAYOUT_COLOR
}; };
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
{ {
public: public:
bool displayNormals = true;
struct { struct {
VkPipelineVertexInputStateCreateInfo inputState; VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputBindingDescription> bindingDescriptions; std::vector<VkVertexInputBindingDescription> bindingDescriptions;
@ -72,6 +74,7 @@ public:
{ {
zoom = -8.0f; zoom = -8.0f;
rotation = glm::vec3(0.0f, -25.0f, 0.0f); rotation = glm::vec3(0.0f, -25.0f, 0.0f);
enableTextOverlay = true;
title = "Vulkan Example - Geometry shader"; title = "Vulkan Example - Geometry shader";
} }
@ -91,6 +94,16 @@ public:
vkTools::destroyUniformData(device, &uniformData.GS); vkTools::destroyUniformData(device, &uniformData.GS);
} }
void reBuildCommandBuffers()
{
if (!checkCommandBuffers())
{
destroyCommandBuffers();
createCommandBuffers();
}
buildCommandBuffers();
}
void buildCommandBuffers() void buildCommandBuffers()
{ {
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
@ -108,32 +121,20 @@ public:
renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.pClearValues = clearValues;
VkResult err;
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
{ {
// Set target frame buffer // Set target frame buffer
renderPassBeginInfo.framebuffer = frameBuffers[i]; renderPassBeginInfo.framebuffer = frameBuffers[i];
err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo); VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
assert(!err);
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport( VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f
(float)width,
(float)height,
0.0f,
1.0f
); );
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D( VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
width,
height,
0,
0
);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f); vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f);
@ -149,43 +150,18 @@ public:
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.object.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.object.indexCount, 1, 0, 0, 0);
// Normal debugging // Normal debugging
if (displayNormals)
{
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.normals); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.normals);
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.object.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], meshes.object.indexCount, 1, 0, 0, 0);
}
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
err = vkEndCommandBuffer(drawCmdBuffers[i]); VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
assert(!err);
} }
} }
void draw()
{
VkResult err;
// Get next image in the swap chain (back/front buffer)
err = swapChain.acquireNextImage(semaphores.presentComplete, &currentBuffer);
assert(!err);
submitPostPresentBarrier(swapChain.buffers[currentBuffer].image);
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
assert(!err);
submitPrePresentBarrier(swapChain.buffers[currentBuffer].image);
err = swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete);
assert(!err);
err = vkQueueWaitIdle(queue);
assert(!err);
}
void loadMeshes() void loadMeshes()
{ {
loadMesh(getAssetPath() + "models/suzanne.obj", &meshes.object, vertexLayout, 0.25f); loadMesh(getAssetPath() + "models/suzanne.obj", &meshes.object, vertexLayout, 0.25f);
@ -221,12 +197,12 @@ public:
VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 3); sizeof(float) * 3);
// Location 2 : Texture coordinates // Location 2 : Color
vertices.attributeDescriptions[2] = vertices.attributeDescriptions[2] =
vkTools::initializers::vertexInputAttributeDescription( vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID, VERTEX_BUFFER_BIND_ID,
2, 2,
VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 6); sizeof(float) * 6);
// Assign to vertex buffer // Assign to vertex buffer
@ -251,8 +227,7 @@ public:
poolSizes.data(), poolSizes.data(),
2); 2);
VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
assert(!vkRes);
} }
void setupDescriptorSetLayout() void setupDescriptorSetLayout()
@ -276,16 +251,14 @@ public:
setLayoutBindings.data(), setLayoutBindings.data(),
setLayoutBindings.size()); setLayoutBindings.size());
VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
assert(!err);
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkTools::initializers::pipelineLayoutCreateInfo( vkTools::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout, &descriptorSetLayout,
1); 1);
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout); VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
assert(!err);
} }
void setupDescriptorSet() void setupDescriptorSet()
@ -296,8 +269,7 @@ public:
&descriptorSetLayout, &descriptorSetLayout,
1); 1);
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
assert(!vkRes);
std::vector<VkWriteDescriptorSet> writeDescriptorSets = std::vector<VkWriteDescriptorSet> writeDescriptorSets =
{ {
@ -320,8 +292,6 @@ public:
void preparePipelines() void preparePipelines()
{ {
VkResult err;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkTools::initializers::pipelineInputAssemblyStateCreateInfo( vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
@ -398,17 +368,13 @@ public:
pipelineCreateInfo.renderPass = renderPass; pipelineCreateInfo.renderPass = renderPass;
// Normal debugging pipeline // Normal debugging pipeline
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.normals); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.normals));
assert(!err);
// Solid rendering pipeline // Solid rendering pipeline
// Shader
shaderStages[0] = loadShader(getAssetPath() + "shaders/geometryshader/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/geometryshader/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/geometryshader/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/geometryshader/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
pipelineCreateInfo.stageCount = 2; pipelineCreateInfo.stageCount = 2;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
assert(!err);
} }
// Prepare and initialize uniform buffer containing shader uniforms // Prepare and initialize uniform buffer containing shader uniforms
@ -417,8 +383,9 @@ public:
// Vertex shader uniform buffer block // Vertex shader uniform buffer block
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboVS), sizeof(uboVS),
&uboVS, nullptr,
&uniformData.VS.buffer, &uniformData.VS.buffer,
&uniformData.VS.memory, &uniformData.VS.memory,
&uniformData.VS.descriptor); &uniformData.VS.descriptor);
@ -426,8 +393,9 @@ public:
// Geometry shader uniform buffer block // Geometry shader uniform buffer block
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboGS), sizeof(uboGS),
&uboGS, nullptr,
&uniformData.GS.buffer, &uniformData.GS.buffer,
&uniformData.GS.memory, &uniformData.GS.memory,
&uniformData.GS.descriptor); &uniformData.GS.descriptor);
@ -438,33 +406,41 @@ public:
void updateUniformBuffers() void updateUniformBuffers()
{ {
// Vertex shader // Vertex shader
glm::mat4 viewMatrix = glm::mat4();
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom)); glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
float offset = 0.5f; uboVS.model = viewMatrix * glm::translate(glm::mat4(), cameraPos);
int uboIndex = 1;
uboVS.model = glm::mat4();
uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
uint8_t *pData; uint8_t *pData;
VkResult err = vkMapMemory(device, uniformData.VS.memory, 0, sizeof(uboVS), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformData.VS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboVS, sizeof(uboVS)); memcpy(pData, &uboVS, sizeof(uboVS));
vkUnmapMemory(device, uniformData.VS.memory); vkUnmapMemory(device, uniformData.VS.memory);
// Geometry shader // Geometry shader
uboGS.model = uboVS.model; uboGS.model = uboVS.model;
uboGS.projection = uboVS.projection; uboGS.projection = uboVS.projection;
err = vkMapMemory(device, uniformData.GS.memory, 0, sizeof(uboGS), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformData.GS.memory, 0, sizeof(uboGS), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboGS, sizeof(uboGS)); memcpy(pData, &uboGS, sizeof(uboGS));
vkUnmapMemory(device, uniformData.GS.memory); vkUnmapMemory(device, uniformData.GS.memory);
} }
void draw()
{
VulkanExampleBase::prepareFrame();
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}
void prepare() void prepare()
{ {
VulkanExampleBase::prepare(); VulkanExampleBase::prepare();
@ -483,15 +459,40 @@ public:
{ {
if (!prepared) if (!prepared)
return; return;
vkDeviceWaitIdle(device);
draw(); draw();
vkDeviceWaitIdle(device);
} }
virtual void viewChanged() virtual void viewChanged()
{ {
updateUniformBuffers(); updateUniformBuffers();
} }
void toggleNormals()
{
displayNormals = !displayNormals;
reBuildCommandBuffers();
}
virtual void keyPressed(uint32_t keyCode)
{
switch (keyCode)
{
case 0x4E:
case GAMEPAD_BUTTON_A:
toggleNormals();
break;
}
}
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
{
#if defined(__ANDROID__)
textOverlay->addText("Press \"Button A\" to toggle normals", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
#else
textOverlay->addText("Press \"n\" to toggle normals", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
#endif
}
}; };
VulkanExample *vulkanExample; VulkanExample *vulkanExample;