Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Jan 19, 2024
1 parent 9ac41c7 commit 54f25af
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 210 deletions.
92 changes: 46 additions & 46 deletions examples/deferred/deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ class VulkanExample : public VulkanExampleBase
vkglTF::Model floor;
} models;

struct {
struct UniformDataOffscreen {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 view;
glm::vec4 instancePos[3];
} uboOffscreenVS;
} uniformDataOffscreen;

struct Light {
glm::vec4 position;
glm::vec3 color;
float radius;
};

struct {
struct UniformDataComposition {
Light lights[6];
glm::vec4 viewPos;
int debugDisplayTarget = 0;
} uboComposition;
} uniformDataComposition;

struct {
vks::Buffer offscreen{ VK_NULL_HANDLE };
Expand Down Expand Up @@ -583,8 +583,8 @@ class VulkanExample : public VulkanExampleBase
void preparePipelines()
{
// Pipeline layout
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));

// Pipelines
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
Expand Down Expand Up @@ -648,19 +648,19 @@ class VulkanExample : public VulkanExampleBase
void prepareUniformBuffers()
{
// Offscreen vertex shader
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.offscreen, sizeof(uboOffscreenVS)));
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.offscreen, sizeof(UniformDataOffscreen)));

// Deferred fragment shader
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.composition, sizeof(uboComposition)));
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.composition, sizeof(UniformDataComposition)));

// Map persistent
VK_CHECK_RESULT(uniformBuffers.offscreen.map());
VK_CHECK_RESULT(uniformBuffers.composition.map());

// Setup instanced model positions
uboOffscreenVS.instancePos[0] = glm::vec4(0.0f);
uboOffscreenVS.instancePos[1] = glm::vec4(-4.0f, 0.0, -4.0f, 0.0f);
uboOffscreenVS.instancePos[2] = glm::vec4(4.0f, 0.0, -4.0f, 0.0f);
uniformDataOffscreen.instancePos[0] = glm::vec4(0.0f);
uniformDataOffscreen.instancePos[1] = glm::vec4(-4.0f, 0.0, -4.0f, 0.0f);
uniformDataOffscreen.instancePos[2] = glm::vec4(4.0f, 0.0, -4.0f, 0.0f);

// Update
updateUniformBufferOffscreen();
Expand All @@ -670,64 +670,64 @@ class VulkanExample : public VulkanExampleBase
// Update matrices used for the offscreen rendering of the scene
void updateUniformBufferOffscreen()
{
uboOffscreenVS.projection = camera.matrices.perspective;
uboOffscreenVS.view = camera.matrices.view;
uboOffscreenVS.model = glm::mat4(1.0f);
memcpy(uniformBuffers.offscreen.mapped, &uboOffscreenVS, sizeof(uboOffscreenVS));
uniformDataOffscreen.projection = camera.matrices.perspective;
uniformDataOffscreen.view = camera.matrices.view;
uniformDataOffscreen.model = glm::mat4(1.0f);
memcpy(uniformBuffers.offscreen.mapped, &uniformDataOffscreen, sizeof(UniformDataOffscreen));
}

// Update lights and parameters passed to the composition shaders
void updateUniformBufferComposition()
{
// White
uboComposition.lights[0].position = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
uboComposition.lights[0].color = glm::vec3(1.5f);
uboComposition.lights[0].radius = 15.0f * 0.25f;
uniformDataComposition.lights[0].position = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
uniformDataComposition.lights[0].color = glm::vec3(1.5f);
uniformDataComposition.lights[0].radius = 15.0f * 0.25f;
// Red
uboComposition.lights[1].position = glm::vec4(-2.0f, 0.0f, 0.0f, 0.0f);
uboComposition.lights[1].color = glm::vec3(1.0f, 0.0f, 0.0f);
uboComposition.lights[1].radius = 15.0f;
uniformDataComposition.lights[1].position = glm::vec4(-2.0f, 0.0f, 0.0f, 0.0f);
uniformDataComposition.lights[1].color = glm::vec3(1.0f, 0.0f, 0.0f);
uniformDataComposition.lights[1].radius = 15.0f;
// Blue
uboComposition.lights[2].position = glm::vec4(2.0f, -1.0f, 0.0f, 0.0f);
uboComposition.lights[2].color = glm::vec3(0.0f, 0.0f, 2.5f);
uboComposition.lights[2].radius = 5.0f;
uniformDataComposition.lights[2].position = glm::vec4(2.0f, -1.0f, 0.0f, 0.0f);
uniformDataComposition.lights[2].color = glm::vec3(0.0f, 0.0f, 2.5f);
uniformDataComposition.lights[2].radius = 5.0f;
// Yellow
uboComposition.lights[3].position = glm::vec4(0.0f, -0.9f, 0.5f, 0.0f);
uboComposition.lights[3].color = glm::vec3(1.0f, 1.0f, 0.0f);
uboComposition.lights[3].radius = 2.0f;
uniformDataComposition.lights[3].position = glm::vec4(0.0f, -0.9f, 0.5f, 0.0f);
uniformDataComposition.lights[3].color = glm::vec3(1.0f, 1.0f, 0.0f);
uniformDataComposition.lights[3].radius = 2.0f;
// Green
uboComposition.lights[4].position = glm::vec4(0.0f, -0.5f, 0.0f, 0.0f);
uboComposition.lights[4].color = glm::vec3(0.0f, 1.0f, 0.2f);
uboComposition.lights[4].radius = 5.0f;
uniformDataComposition.lights[4].position = glm::vec4(0.0f, -0.5f, 0.0f, 0.0f);
uniformDataComposition.lights[4].color = glm::vec3(0.0f, 1.0f, 0.2f);
uniformDataComposition.lights[4].radius = 5.0f;
// Yellow
uboComposition.lights[5].position = glm::vec4(0.0f, -1.0f, 0.0f, 0.0f);
uboComposition.lights[5].color = glm::vec3(1.0f, 0.7f, 0.3f);
uboComposition.lights[5].radius = 25.0f;
uniformDataComposition.lights[5].position = glm::vec4(0.0f, -1.0f, 0.0f, 0.0f);
uniformDataComposition.lights[5].color = glm::vec3(1.0f, 0.7f, 0.3f);
uniformDataComposition.lights[5].radius = 25.0f;

// Animate the lights
if (!paused) {
uboComposition.lights[0].position.x = sin(glm::radians(360.0f * timer)) * 5.0f;
uboComposition.lights[0].position.z = cos(glm::radians(360.0f * timer)) * 5.0f;
uniformDataComposition.lights[0].position.x = sin(glm::radians(360.0f * timer)) * 5.0f;
uniformDataComposition.lights[0].position.z = cos(glm::radians(360.0f * timer)) * 5.0f;

uboComposition.lights[1].position.x = -4.0f + sin(glm::radians(360.0f * timer) + 45.0f) * 2.0f;
uboComposition.lights[1].position.z = 0.0f + cos(glm::radians(360.0f * timer) + 45.0f) * 2.0f;
uniformDataComposition.lights[1].position.x = -4.0f + sin(glm::radians(360.0f * timer) + 45.0f) * 2.0f;
uniformDataComposition.lights[1].position.z = 0.0f + cos(glm::radians(360.0f * timer) + 45.0f) * 2.0f;

uboComposition.lights[2].position.x = 4.0f + sin(glm::radians(360.0f * timer)) * 2.0f;
uboComposition.lights[2].position.z = 0.0f + cos(glm::radians(360.0f * timer)) * 2.0f;
uniformDataComposition.lights[2].position.x = 4.0f + sin(glm::radians(360.0f * timer)) * 2.0f;
uniformDataComposition.lights[2].position.z = 0.0f + cos(glm::radians(360.0f * timer)) * 2.0f;

uboComposition.lights[4].position.x = 0.0f + sin(glm::radians(360.0f * timer + 90.0f)) * 5.0f;
uboComposition.lights[4].position.z = 0.0f - cos(glm::radians(360.0f * timer + 45.0f)) * 5.0f;
uniformDataComposition.lights[4].position.x = 0.0f + sin(glm::radians(360.0f * timer + 90.0f)) * 5.0f;
uniformDataComposition.lights[4].position.z = 0.0f - cos(glm::radians(360.0f * timer + 45.0f)) * 5.0f;

uboComposition.lights[5].position.x = 0.0f + sin(glm::radians(-360.0f * timer + 135.0f)) * 10.0f;
uboComposition.lights[5].position.z = 0.0f - cos(glm::radians(-360.0f * timer - 45.0f)) * 10.0f;
uniformDataComposition.lights[5].position.x = 0.0f + sin(glm::radians(-360.0f * timer + 135.0f)) * 10.0f;
uniformDataComposition.lights[5].position.z = 0.0f - cos(glm::radians(-360.0f * timer - 45.0f)) * 10.0f;
}

// Current view position
uboComposition.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
uniformDataComposition.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);

uboComposition.debugDisplayTarget = debugDisplayTarget;
uniformDataComposition.debugDisplayTarget = debugDisplayTarget;

memcpy(uniformBuffers.composition.mapped, &uboComposition, sizeof(uboComposition));
memcpy(uniformBuffers.composition.mapped, &uniformDataComposition, sizeof(UniformDataComposition));
}

void prepare()
Expand Down
Loading

0 comments on commit 54f25af

Please sign in to comment.