Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
300 changes: 300 additions & 0 deletions INSTRUCTIONS.md

Large diffs are not rendered by default.

313 changes: 30 additions & 283 deletions README.md

Large diffs are not rendered by default.

Binary file added img/analysis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/cull_distance.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/cull_frustum.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/cull_orientation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/static.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
205 changes: 178 additions & 27 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,66 @@ void Renderer::CreateTimeDescriptorSetLayout() {
}

void Renderer::CreateComputeDescriptorSetLayout() {
// TODO: Create the descriptor set layout for the compute pipeline
// Remember this is like a class definition stating why types of information
// will be stored at each binding
// TODONE: Create the descriptor set layout for the compute pipeline
// Remember this is like a class definition stating what types of information
// will be stored at each binding

VkDescriptorSetLayoutBinding inputBinding = {};
inputBinding.binding = 0;
inputBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
inputBinding.descriptorCount = 1;
inputBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
inputBinding.pImmutableSamplers = nullptr;

VkDescriptorSetLayoutBinding culledBinding = {};
culledBinding.binding = 1;
culledBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
culledBinding.descriptorCount = 1;
culledBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
culledBinding.pImmutableSamplers = nullptr;

VkDescriptorSetLayoutBinding numBladesBinding = {};
numBladesBinding.binding = 2;
numBladesBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
numBladesBinding.descriptorCount = 1;
numBladesBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
numBladesBinding.pImmutableSamplers = nullptr;

std::vector<VkDescriptorSetLayoutBinding> bindings = { inputBinding, culledBinding, numBladesBinding };

// actually creating the descriptor set layout for the compute pipeline.
// Like a class definition stating what types of information
// will be stored at each binding

VkDescriptorSetLayoutCreateInfo layout_info = {};
layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layout_info.bindingCount = static_cast<uint32_t>(bindings.size());
layout_info.pBindings = bindings.data();

if (vkCreateDescriptorSetLayout(logicalDevice, &layout_info, nullptr, &computeDescriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("Failed to create descriptor set layout in Renderer.");
}
}

void Renderer::CreateDescriptorPool() {
// Describe which descriptor types that the descriptor sets will contain
std::vector<VkDescriptorPoolSize> poolSizes = {
// Camera
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1},
// Camera
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1},

// Models + Blades
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER , static_cast<uint32_t>(scene->GetModels().size() + scene->GetBlades().size()) },
// Models + Blades
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER , static_cast<uint32_t>(scene->GetModels().size() + scene->GetBlades().size()) },

// Models + Blades
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , static_cast<uint32_t>(scene->GetModels().size() + scene->GetBlades().size()) },
// Models + Blades
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , static_cast<uint32_t>(scene->GetModels().size() + scene->GetBlades().size()) },

// Time (compute)
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 },
// Time
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 },

// TODO: Add any additional types and counts of descriptors you will need to allocate
// TODONE: Add any additional types and counts of descriptors you will need to allocate

// Compute Blades
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 3 * static_cast<uint32_t>(scene->GetBlades().size()) },
};

VkDescriptorPoolCreateInfo poolInfo = {};
Expand Down Expand Up @@ -318,8 +357,45 @@ void Renderer::CreateModelDescriptorSets() {
}

void Renderer::CreateGrassDescriptorSets() {
// TODO: Create Descriptor sets for the grass.
// This should involve creating descriptor sets which point to the model matrix of each group of grass blades
// TODONE: Create Descriptor sets for the grass.
// This should involve creating descriptor sets which point to the model matrix of each group of grass blades

grassDescriptorSets.resize(scene->GetBlades().size());

// describing the set
VkDescriptorSetLayout layouts[] = { modelDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(grassDescriptorSets.size());
allocInfo.pSetLayouts = layouts;

// allocating the set
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, grassDescriptorSets.data()) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate descriptor set in render");
}

// write to the set
std::vector<VkWriteDescriptorSet> descriptorWrites(grassDescriptorSets.size());
for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) {
VkDescriptorBufferInfo modelBufferInfo = {};
modelBufferInfo.buffer = scene->GetBlades()[i]->GetModelBuffer();
modelBufferInfo.offset = 0;
modelBufferInfo.range = sizeof(ModelBufferObject);

descriptorWrites[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[i].dstSet = grassDescriptorSets[i];
descriptorWrites[i].dstBinding = 0;
descriptorWrites[i].dstArrayElement = 0;
descriptorWrites[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[i].descriptorCount = 1;
descriptorWrites[i].pBufferInfo = &modelBufferInfo;
descriptorWrites[i].pImageInfo = nullptr;
descriptorWrites[i].pTexelBufferView = nullptr;
}

// update the set
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}

void Renderer::CreateTimeDescriptorSet() {
Expand Down Expand Up @@ -358,8 +434,77 @@ void Renderer::CreateTimeDescriptorSet() {
}

void Renderer::CreateComputeDescriptorSets() {
// TODO: Create Descriptor sets for the compute pipeline
// The descriptors should point to Storage buffers which will hold the grass blades, the culled grass blades, and the output number of grass blades
// TODONE: Create Descriptor sets for the compute pipeline
// The descriptors should point to Storage buffers which will hold the grass blades, the culled grass blades, and the output number of grass blades

computeDescriptorSets.resize(scene->GetBlades().size());

// describing the set
VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(computeDescriptorSets.size());
allocInfo.pSetLayouts = layouts;

// allocating the set
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, computeDescriptorSets.data()) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate descriptor set in render");
}

// write to the set
std::vector<VkWriteDescriptorSet> descriptorWrites(3 * computeDescriptorSets.size());
for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) {
uint32_t index = 3 * i;

VkDescriptorBufferInfo inputInfo = {};
inputInfo.buffer = scene->GetBlades()[i]->GetBladesBuffer();
inputInfo.offset = 0;
inputInfo.range = sizeof(Blade) * NUM_BLADES;

descriptorWrites[index].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[index].dstSet = computeDescriptorSets[i];
descriptorWrites[index].dstBinding = 0;
descriptorWrites[index].dstArrayElement = 0;
descriptorWrites[index].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[index].descriptorCount = 1;
descriptorWrites[index].pBufferInfo = &inputInfo;
descriptorWrites[index].pImageInfo = nullptr;
descriptorWrites[index].pTexelBufferView = nullptr;

VkDescriptorBufferInfo culledInfo = {};
culledInfo.buffer = scene->GetBlades()[i]->GetCulledBladesBuffer();
culledInfo.offset = 0;
culledInfo.range = sizeof(Blade) * NUM_BLADES;

descriptorWrites[++index].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[index].dstSet = computeDescriptorSets[i];
descriptorWrites[index].dstBinding = 1;
descriptorWrites[index].dstArrayElement = 0;
descriptorWrites[index].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[index].descriptorCount = 1;
descriptorWrites[index].pBufferInfo = &culledInfo;
descriptorWrites[index].pImageInfo = nullptr;
descriptorWrites[index].pTexelBufferView = nullptr;

VkDescriptorBufferInfo numBladesInfo = {};
numBladesInfo.buffer = scene->GetBlades()[i]->GetNumBladesBuffer();
numBladesInfo.offset = 0;
numBladesInfo.range = sizeof(BladeDrawIndirect);

descriptorWrites[++index].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[index].dstSet = computeDescriptorSets[i];
descriptorWrites[index].dstBinding = 2;
descriptorWrites[index].dstArrayElement = 0;
descriptorWrites[index].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[index].descriptorCount = 1;
descriptorWrites[index].pBufferInfo = &numBladesInfo;
descriptorWrites[index].pImageInfo = nullptr;
descriptorWrites[index].pTexelBufferView = nullptr;
}

// update the set
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}

void Renderer::CreateGraphicsPipeline() {
Expand Down Expand Up @@ -716,8 +861,8 @@ void Renderer::CreateComputePipeline() {
computeShaderStageInfo.module = computeShaderModule;
computeShaderStageInfo.pName = "main";

// TODO: Add the compute dsecriptor set layout you create to this list
std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { cameraDescriptorSetLayout, timeDescriptorSetLayout };
// TODONE: Add the compute descriptor set layout you create to this list
std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { cameraDescriptorSetLayout, timeDescriptorSetLayout, computeDescriptorSetLayout };

// Create pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
Expand Down Expand Up @@ -883,7 +1028,11 @@ void Renderer::RecordComputeCommandBuffer() {
// Bind descriptor set for time uniforms
vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 1, 1, &timeDescriptorSet, 0, nullptr);

// TODO: For each group of blades bind its descriptor set and dispatch
// TODONE: For each group of blades bind its descriptor set and dispatch
for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) {
vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 2, 1, &computeDescriptorSets[i], 0, nullptr);
vkCmdDispatch(computeCommandBuffer, (NUM_BLADES + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE, 1, 1);
}

// ~ End recording ~
if (vkEndCommandBuffer(computeCommandBuffer) != VK_SUCCESS) {
Expand Down Expand Up @@ -926,7 +1075,7 @@ void Renderer::RecordCommandBuffers() {
renderPassInfo.renderArea.extent = swapChain->GetVkExtent();

std::array<VkClearValue, 2> clearValues = {};
clearValues[0].color = { 0.0f, 0.0f, 0.0f, 1.0f };
clearValues[0].color = { 0.678f, 0.845f, 0.91f, 1.0f };
clearValues[1].depthStencil = { 1.0f, 0 };
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
Expand Down Expand Up @@ -975,14 +1124,15 @@ void Renderer::RecordCommandBuffers() {
for (uint32_t j = 0; j < scene->GetBlades().size(); ++j) {
VkBuffer vertexBuffers[] = { scene->GetBlades()[j]->GetCulledBladesBuffer() };
VkDeviceSize offsets[] = { 0 };
// TODO: Uncomment this when the buffers are populated
// vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
// TODONE: Uncomment this when the buffers are populated
vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);

// TODO: Bind the descriptor set for each grass blades model
// Bind the descriptor set for each grass blades model
vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, grassPipelineLayout, 1, 1, &grassDescriptorSets[j], 0, nullptr);

// Draw
// TODO: Uncomment this when the buffers are populated
// vkCmdDrawIndirect(commandBuffers[i], scene->GetBlades()[j]->GetNumBladesBuffer(), 0, 1, sizeof(BladeDrawIndirect));
// TODONE: Uncomment this when the buffers are populated
vkCmdDrawIndirect(commandBuffers[i], scene->GetBlades()[j]->GetNumBladesBuffer(), 0, 1, sizeof(BladeDrawIndirect));
}

// End render pass
Expand Down Expand Up @@ -1041,8 +1191,6 @@ void Renderer::Frame() {
Renderer::~Renderer() {
vkDeviceWaitIdle(logicalDevice);

// TODO: destroy any resources you created

vkFreeCommandBuffers(logicalDevice, graphicsCommandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
vkFreeCommandBuffers(logicalDevice, computeCommandPool, 1, &computeCommandBuffer);

Expand All @@ -1058,6 +1206,9 @@ Renderer::~Renderer() {
vkDestroyDescriptorSetLayout(logicalDevice, modelDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, timeDescriptorSetLayout, nullptr);

// destroy any resources you created
vkDestroyDescriptorSetLayout(logicalDevice, computeDescriptorSetLayout, nullptr);

vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr);

vkDestroyRenderPass(logicalDevice, renderPass, nullptr);
Expand Down
5 changes: 4 additions & 1 deletion src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;
VkDescriptorSetLayout computeDescriptorSetLayout;

VkDescriptorPool descriptorPool;

VkDescriptorSet cameraDescriptorSet;
std::vector<VkDescriptorSet> modelDescriptorSets;
VkDescriptorSet timeDescriptorSet;
std::vector<VkDescriptorSet> modelDescriptorSets;
std::vector<VkDescriptorSet> grassDescriptorSets;
std::vector<VkDescriptorSet> computeDescriptorSets;

VkPipelineLayout graphicsPipelineLayout;
VkPipelineLayout grassPipelineLayout;
Expand Down
6 changes: 6 additions & 0 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "Scene.h"
#include "BufferUtils.h"
#include <iostream>

Scene::Scene(Device* device) : device(device) {
BufferUtils::CreateBuffer(device, sizeof(Time), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, timeBuffer, timeBufferMemory);
vkMapMemory(device->GetVkDevice(), timeBufferMemory, 0, sizeof(Time), 0, &mappedData);
memcpy(mappedData, &time, sizeof(Time));
totalFrames = 0;
}

const std::vector<Model*>& Scene::GetModels() const {
Expand All @@ -30,6 +32,7 @@ void Scene::UpdateTime() {

time.deltaTime = nextDeltaTime.count();
time.totalTime += time.deltaTime;
++totalFrames;

memcpy(mappedData, &time, sizeof(Time));
}
Expand All @@ -42,4 +45,7 @@ Scene::~Scene() {
vkUnmapMemory(device->GetVkDevice(), timeBufferMemory);
vkDestroyBuffer(device->GetVkDevice(), timeBuffer, nullptr);
vkFreeMemory(device->GetVkDevice(), timeBufferMemory, nullptr);
float fps = (float)totalFrames / time.totalTime;
std::cout << fps << std::endl;
//abort(); // To see fps before application exits
}
1 change: 1 addition & 0 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Scene {
VkBuffer timeBuffer;
VkDeviceMemory timeBufferMemory;
Time time;
int totalFrames;

void* mappedData;

Expand Down
Loading