Skip to content

Commit

Permalink
Code cleanup, fixed HLSL shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Jan 14, 2024
1 parent 6444281 commit 7ad9ee1
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 404 deletions.
471 changes: 182 additions & 289 deletions examples/terraintessellation/terraintessellation.cpp

Large diffs are not rendered by default.

169 changes: 66 additions & 103 deletions examples/tessellation/tessellation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,25 @@ class VulkanExample : public VulkanExampleBase

vkglTF::Model model;

struct {
vks::Buffer tessControl, tessEval;
} uniformBuffers;

struct UBOTessControl {
float tessLevel = 3.0f;
} uboTessControl;

struct UBOTessEval {
// One uniform data block is used by both tessellation shader stages
struct UniformData {
glm::mat4 projection;
glm::mat4 modelView;
float tessAlpha = 1.0f;
} uboTessEval;
float tessLevel = 3.0f;
} uniformData;
vks::Buffer uniformBuffer;

struct Pipelines {
VkPipeline solid;
VkPipeline wire = VK_NULL_HANDLE;
VkPipeline solidPassThrough;
VkPipeline wirePassThrough = VK_NULL_HANDLE;
VkPipeline solid{ VK_NULL_HANDLE };
VkPipeline wire{ VK_NULL_HANDLE };
VkPipeline solidPassThrough{ VK_NULL_HANDLE };
VkPipeline wirePassThrough{ VK_NULL_HANDLE };
} pipelines;

VkPipelineLayout pipelineLayout;
VkDescriptorSet descriptorSet;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };

VulkanExample() : VulkanExampleBase()
{
Expand All @@ -56,28 +51,29 @@ class VulkanExample : public VulkanExampleBase

~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.solid, nullptr);
if (pipelines.wire != VK_NULL_HANDLE) {
vkDestroyPipeline(device, pipelines.wire, nullptr);
};
vkDestroyPipeline(device, pipelines.solidPassThrough, nullptr);
if (pipelines.wirePassThrough != VK_NULL_HANDLE) {
vkDestroyPipeline(device, pipelines.wirePassThrough, nullptr);
};

vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);

uniformBuffers.tessControl.destroy();
uniformBuffers.tessEval.destroy();
if (device) {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.solid, nullptr);
if (pipelines.wire != VK_NULL_HANDLE) {
vkDestroyPipeline(device, pipelines.wire, nullptr);
};
vkDestroyPipeline(device, pipelines.solidPassThrough, nullptr);
if (pipelines.wirePassThrough != VK_NULL_HANDLE) {
vkDestroyPipeline(device, pipelines.wirePassThrough, nullptr);
};

vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);

uniformBuffer.destroy();
}
}

// Enable physical device features required for this example
virtual void getEnabledFeatures()
{
// Example uses tessellation shaders
// Example requires tessellation shaders
if (deviceFeatures.tessellationShader) {
enabledFeatures.tessellationShader = VK_TRUE;
}
Expand Down Expand Up @@ -156,50 +152,44 @@ class VulkanExample : public VulkanExampleBase
model.loadFromFile(getAssetPath() + "models/deer.gltf", vulkanDevice, queue, vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::FlipY);
}

void setupDescriptorPool()
void setupDescriptors()
{
// Pool
const std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
};
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 1);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}

void setupDescriptorSetLayout()
{
// Layout
const std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
// Binding 0 : Tessellation control shader ubo
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, 0),
// Binding 1 : Tessellation evaluation shader ubo
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, 1),
// Binding 0 : Tessellation shader ubo
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, 0),
};
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));

// Layout uses set 0 for passing tessellation shader ubos and set 1 for fragment shader images (taken from glTF model)
const std::vector<VkDescriptorSetLayout> setLayouts = {
descriptorSetLayout,
vkglTF::descriptorSetLayoutImage,
};
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), 2);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}

void setupDescriptorSet()
{
// Sets
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
// Binding 0 : Tessellation control shader ubo
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.tessControl.descriptor),
// Binding 1 : Tessellation evaluation shader ubo
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &uniformBuffers.tessEval.descriptor),
// Binding 0 : Tessellation shader ubo
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
}

void preparePipelines()
{
// Layout uses set 0 for passing tessellation shader ubos and set 1 for fragment shader images (taken from glTF model)
const std::vector<VkDescriptorSetLayout> setLayouts = {
descriptorSetLayout,
vkglTF::descriptorSetLayoutImage,
};
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), 2);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));

// Pipelines
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_PATCH_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);
Expand Down Expand Up @@ -260,80 +250,53 @@ class VulkanExample : public VulkanExampleBase
void prepareUniformBuffers()
{
// Tessellation evaluation shader uniform buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.tessEval,
sizeof(uboTessEval)));

// Tessellation control shader uniform buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.tessControl,
sizeof(uboTessControl)));

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)));
// Map persistent
VK_CHECK_RESULT(uniformBuffers.tessControl.map());
VK_CHECK_RESULT(uniformBuffers.tessEval.map());

updateUniformBuffers();
VK_CHECK_RESULT(uniformBuffer.map());
}

void updateUniformBuffers()
{
uboTessEval.projection = camera.matrices.perspective;
uboTessEval.modelView = camera.matrices.view;
// Adjust camera perspective if split screen is enabled
camera.setPerspective(45.0f, (float)(width * ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f);
uniformData.projection = camera.matrices.perspective;
uniformData.modelView = camera.matrices.view;
// Tessellation evaluation uniform block
memcpy(uniformBuffers.tessEval.mapped, &uboTessEval, sizeof(uboTessEval));
// Tessellation control uniform block
memcpy(uniformBuffers.tessControl.mapped, &uboTessControl, sizeof(uboTessControl));
}

void draw()
{
VulkanExampleBase::prepareFrame();

submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));

VulkanExampleBase::submitFrame();
memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
}

void prepare()
{
VulkanExampleBase::prepare();
loadAssets();
prepareUniformBuffers();
setupDescriptorSetLayout();
setupDescriptors();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}

virtual void render()
void draw()
{
if (!prepared)
return;
draw();
if (camera.updated) {
updateUniformBuffers();
}
VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}

virtual void viewChanged()
virtual void render()
{
camera.setPerspective(45.0f, (float)(width * ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f);
if (!prepared)
return;
updateUniformBuffers();
draw();
}

virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
{
if (overlay->header("Settings")) {
if (overlay->inputFloat("Tessellation level", &uboTessControl.tessLevel, 0.25f, 2)) {
if (overlay->inputFloat("Tessellation level", &uniformData.tessLevel, 0.25f, 2)) {
updateUniformBuffers();
}
if (deviceFeatures.fillModeNonSolid) {
Expand Down
3 changes: 2 additions & 1 deletion shaders/glsl/tessellation/passthrough.tese
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

layout (triangles, fractional_odd_spacing, cw) in;

layout (binding = 1) uniform UBO
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
float tessAlpha;
float tessLevel;
} ubo;

layout (location = 0) in vec3 inNormal[];
Expand Down
Binary file modified shaders/glsl/tessellation/passthrough.tese.spv
Binary file not shown.
5 changes: 4 additions & 1 deletion shaders/glsl/tessellation/pntriangles.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ struct PnPatch
// tessellation levels
layout (binding = 0) uniform UBO
{
float tessLevel;
mat4 projection;
mat4 model;
float tessAlpha;
float tessLevel;
} ubo;

layout(vertices=3) out;
Expand Down
Binary file modified shaders/glsl/tessellation/pntriangles.tesc.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion shaders/glsl/tessellation/pntriangles.tese
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ struct PnPatch
float n101;
};

layout (binding = 1) uniform UBO
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
float tessAlpha;
float tessLevel;
} ubo;

layout(triangles, fractional_odd_spacing, cw) in;
Expand Down
Binary file modified shaders/glsl/tessellation/pntriangles.tese.spv
Binary file not shown.
4 changes: 2 additions & 2 deletions shaders/hlsl/tessellation/base.frag
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020 Google LLC

Texture2D textureColorMap : register(t2);
SamplerState samplerColorMap : register(s2);
Texture2D textureColorMap : register(t0, space1);
SamplerState samplerColorMap : register(s0, space1);

struct DSOutput
{
Expand Down
Binary file modified shaders/hlsl/tessellation/base.frag.spv
Binary file not shown.
9 changes: 5 additions & 4 deletions shaders/hlsl/tessellation/passthrough.tese
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

struct UBO
{
float4x4 projection;
float4x4 model;
float tessAlpha;
float4x4 projection;
float4x4 model;
float tessAlpha;
float tessLevel;
};

cbuffer ubo : register(b1) { UBO ubo; }
cbuffer ubo : register(b0) { UBO ubo; }

struct HSOutput
{
Expand Down
Binary file modified shaders/hlsl/tessellation/passthrough.tese.spv
Binary file not shown.
7 changes: 5 additions & 2 deletions shaders/hlsl/tessellation/pntriangles.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ struct PnPatch
// tessellation levels
struct UBO
{
float tessLevel;
float4x4 projection;
float4x4 model;
float tessAlpha;
float tessLevel;
};

cbuffer ubo : register(b0) { UBO ubo; }
Expand Down Expand Up @@ -83,7 +86,7 @@ ConstantsHSOutput ConstantsHS(InputPatch<VSOutput, 3> patch, uint InvocationID :

[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_ccw")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("ConstantsHS")]
[maxtessfactor(20.0f)]
Expand Down
Binary file modified shaders/hlsl/tessellation/pntriangles.tesc.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion shaders/hlsl/tessellation/pntriangles.tese
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ struct UBO
float4x4 projection;
float4x4 model;
float tessAlpha;
float tessLevel;
};

cbuffer ubo : register(b1) { UBO ubo; }
cbuffer ubo : register(b0) { UBO ubo; }

struct HSOutput
{
Expand Down
Binary file modified shaders/hlsl/tessellation/pntriangles.tese.spv
Binary file not shown.

0 comments on commit 7ad9ee1

Please sign in to comment.