Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 6: Rudraksha Shah #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
294 changes: 41 additions & 253 deletions README.md

Large diffs are not rendered by default.

Binary file added bin/Debug/vulkan_grass_rendering.exe
Binary file not shown.
Binary file added bin/Debug/vulkan_grass_rendering.ilk
Binary file not shown.
Binary file added bin/Debug/vulkan_grass_rendering.pdb
Binary file not shown.
Binary file added bin/Release/vulkan_grass_rendering.exe
Binary file not shown.
Binary file added img/Blooper01.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/Grass_DepthCulling.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/Grass_Implementation.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/Grass_OrientationCulling.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/chart_VNGB.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/chart_WDPO.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 159 additions & 7 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Renderer::Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* c
CreateModelDescriptorSetLayout();
CreateTimeDescriptorSetLayout();
CreateComputeDescriptorSetLayout();

CreateDescriptorPool();
CreateCameraDescriptorSet();
CreateModelDescriptorSets();
Expand Down Expand Up @@ -196,15 +197,51 @@ 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
// Remember this is like a class definition stating what types of information
// will be stored at each binding

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

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

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

std::vector<VkDescriptorSetLayoutBinding> bindings = { inputBladesBufferLayoutBinding, outputCulledBladesBufferLayoutBinding, outBladeCountLayoutBinding };

// Create the descriptor set layout
VkDescriptorSetLayoutCreateInfo layoutInfo = {};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
layoutInfo.pBindings = bindings.data();

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

}


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()) },
Expand All @@ -216,6 +253,10 @@ void Renderer::CreateDescriptorPool() {
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 },

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

// Input Blades + Output Blades + No of Baldes
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER , 3 }

};

VkDescriptorPoolCreateInfo poolInfo = {};
Expand Down Expand Up @@ -320,6 +361,40 @@ 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

// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { modelDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = layouts;

// Allocate descriptor sets
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, &grassDescriptorSet) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate grass descriptor set");
}

std::array<VkWriteDescriptorSet, 1> descriptorWrites = {};

VkDescriptorBufferInfo cameraBufferInfo = {};
cameraBufferInfo.buffer = scene->GetBlades()[0]->GetModelBuffer();
cameraBufferInfo.offset = 0;
cameraBufferInfo.range = sizeof(ModelBufferObject);

descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = grassDescriptorSet;
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pBufferInfo = &cameraBufferInfo;
descriptorWrites[0].pImageInfo = nullptr;
descriptorWrites[0].pTexelBufferView = nullptr;

// Update descriptor sets
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);

}

void Renderer::CreateTimeDescriptorSet() {
Expand Down Expand Up @@ -359,7 +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
// The descriptors should point to Storage buffers which will hold the grass blades, the culled grass blades, and the output number of grass blades

// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = layouts;

// Allocate descriptor sets
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, &computeDescriptorSet) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate compute descriptor set");
}

std::array<VkWriteDescriptorSet, 3> descriptorWrites = {};

// Input Blades
VkDescriptorBufferInfo InputBladesBufferInfo = {};
InputBladesBufferInfo.buffer = scene->GetBlades()[0]->GetBladesBuffer();
InputBladesBufferInfo.offset = 0;
InputBladesBufferInfo.range = NUM_BLADES * sizeof(Blade);

// Output Culled Blades
VkDescriptorBufferInfo outputBladesBufferInfo = {};
outputBladesBufferInfo.buffer = scene->GetBlades()[0]->GetCulledBladesBuffer();
outputBladesBufferInfo.offset = 0;
outputBladesBufferInfo.range = NUM_BLADES * sizeof(Blade);

// Output Culled Blades Count
VkDescriptorBufferInfo OutBladeCountBufferInfo = {};
OutBladeCountBufferInfo.buffer = scene->GetBlades()[0]->GetNumBladesBuffer();
OutBladeCountBufferInfo.offset = 0;
OutBladeCountBufferInfo.range = sizeof(BladeDrawIndirect);

// Input Blades
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = computeDescriptorSet;
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pBufferInfo = &InputBladesBufferInfo;
descriptorWrites[0].pImageInfo = nullptr;
descriptorWrites[0].pTexelBufferView = nullptr;

// Output Culled Baldes
descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[1].dstSet = computeDescriptorSet;
descriptorWrites[1].dstBinding = 1;
descriptorWrites[1].dstArrayElement = 0;
descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[1].descriptorCount = 1;
descriptorWrites[1].pBufferInfo = &outputBladesBufferInfo;
descriptorWrites[1].pImageInfo = nullptr;
descriptorWrites[1].pTexelBufferView = nullptr;

// Total Number of Output Baldes
descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[2].dstSet = computeDescriptorSet;
descriptorWrites[2].dstBinding = 2;
descriptorWrites[2].dstArrayElement = 0;
descriptorWrites[2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[2].descriptorCount = 1;
descriptorWrites[2].pBufferInfo = &OutBladeCountBufferInfo;
descriptorWrites[2].pImageInfo = nullptr;
descriptorWrites[2].pTexelBufferView = nullptr;

// Update descriptor sets
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);

}

void Renderer::CreateGraphicsPipeline() {
Expand Down Expand Up @@ -717,7 +862,7 @@ void Renderer::CreateComputePipeline() {
computeShaderStageInfo.pName = "main";

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

// Create pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
Expand Down Expand Up @@ -884,6 +1029,8 @@ void Renderer::RecordComputeCommandBuffer() {
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
vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 2, 1, &computeDescriptorSet, 0, nullptr);
vkCmdDispatch(computeCommandBuffer, NUM_BLADES / WORKGROUP_SIZE, 1, 1);

// ~ End recording ~
if (vkEndCommandBuffer(computeCommandBuffer) != VK_SUCCESS) {
Expand Down Expand Up @@ -976,13 +1123,14 @@ void Renderer::RecordCommandBuffers() {
VkBuffer vertexBuffers[] = { scene->GetBlades()[j]->GetCulledBladesBuffer() };
VkDeviceSize offsets[] = { 0 };
// TODO: Uncomment this when the buffers are populated
// vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);

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

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

// End render pass
Expand Down Expand Up @@ -1064,4 +1212,8 @@ Renderer::~Renderer() {
DestroyFrameResources();
vkDestroyCommandPool(logicalDevice, computeCommandPool, nullptr);
vkDestroyCommandPool(logicalDevice, graphicsCommandPool, nullptr);

// Newly added being deleted
vkDestroyDescriptorSetLayout(logicalDevice, computeDescriptorSetLayout, nullptr);
// Done
}
9 changes: 7 additions & 2 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Renderer {
void CreateModelDescriptorSetLayout();
void CreateTimeDescriptorSetLayout();
void CreateComputeDescriptorSetLayout();

void CreateDescriptorPool();

void CreateCameraDescriptorSet();
Expand Down Expand Up @@ -56,7 +55,13 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;


// Newly added
VkDescriptorSetLayout computeDescriptorSetLayout;
VkDescriptorSet computeDescriptorSet;
VkDescriptorSet grassDescriptorSet;
// Done

VkDescriptorPool descriptorPool;

VkDescriptorSet cameraDescriptorSet;
Expand Down
2 changes: 1 addition & 1 deletion src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void Scene::AddBlades(Blades* blades) {
void Scene::UpdateTime() {
high_resolution_clock::time_point currentTime = high_resolution_clock::now();
duration<float> nextDeltaTime = duration_cast<duration<float>>(currentTime - startTime);
startTime = currentTime;
startTime = currentTime;

time.deltaTime = nextDeltaTime.count();
time.totalTime += time.deltaTime;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace {

int main() {
static constexpr char* applicationName = "Vulkan Grass Rendering";
InitializeWindow(640, 480, applicationName);
InitializeWindow(400, 400, applicationName); // 640, 480

unsigned int glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
Expand Down
Loading