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
297 changes: 297 additions & 0 deletions INSTRUCTION.md

Large diffs are not rendered by default.

288 changes: 35 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/Different_culling_method.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/bitant.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/culling_vs_no_culling.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_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/normal.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Blades.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <array>
#include "Model.h"

constexpr static unsigned int NUM_BLADES = 1 << 13;
constexpr static unsigned int NUM_BLADES = 1 << 15;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
Expand Down
180 changes: 169 additions & 11 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Renderer::Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* c
CreateCameraDescriptorSetLayout();
CreateModelDescriptorSetLayout();
CreateTimeDescriptorSetLayout();
CreateComputeDescriptorSetLayout();
CreateGrassDescriptorSetLayout();
CreateComputeDescriptorSetLayout();

CreateDescriptorPool();
CreateCameraDescriptorSet();
CreateModelDescriptorSets();
Expand Down Expand Up @@ -140,7 +142,7 @@ void Renderer::CreateCameraDescriptorSetLayout() {
layoutInfo.pBindings = bindings.data();

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

Expand Down Expand Up @@ -168,7 +170,7 @@ void Renderer::CreateModelDescriptorSetLayout() {
layoutInfo.pBindings = bindings.data();

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

Expand All @@ -190,14 +192,67 @@ void Renderer::CreateTimeDescriptorSetLayout() {
layoutInfo.pBindings = bindings.data();

if (vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &timeDescriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("Failed to create descriptor set layout");
throw std::runtime_error("Failed to create time descriptor set layout");
}
}
void Renderer::CreateGrassDescriptorSetLayout() {
VkDescriptorSetLayoutBinding uboLayoutBinding = {};
uboLayoutBinding.binding = 0;
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uboLayoutBinding.descriptorCount = 1;
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
uboLayoutBinding.pImmutableSamplers = nullptr;

std::vector<VkDescriptorSetLayoutBinding> bindings = { uboLayoutBinding};

// 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, &grassDescriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("Failed to create grass descriptor set layout");
}

}
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
VkDescriptorSetLayoutBinding bladeLayoutBinding = {};
bladeLayoutBinding.binding = 0;
bladeLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;;
bladeLayoutBinding.descriptorCount = 1;
bladeLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
bladeLayoutBinding.pImmutableSamplers = nullptr;

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

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

std::vector<VkDescriptorSetLayoutBinding>bindings = { bladeLayoutBinding, CulledBladeLayoutBinding ,BladesNumBinding };


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 descriptor set layout");
}

}

void Renderer::CreateDescriptorPool() {
Expand All @@ -216,6 +271,8 @@ void Renderer::CreateDescriptorPool() {
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 },

// TODO: Add any additional types and counts of descriptors you will need to allocate
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER , (uint32_t)scene->GetBlades().size() * 3}

};

VkDescriptorPoolCreateInfo poolInfo = {};
Expand Down Expand Up @@ -318,8 +375,36 @@ 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
//Todo
grassDescriptorSet.resize(scene->GetBlades().size());
VkDescriptorSetLayout layouts[] = { grassDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(grassDescriptorSet.size());
allocInfo.pSetLayouts = layouts;
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, grassDescriptorSet.data()) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate descriptor set");
}
std::vector<VkWriteDescriptorSet> descriptorWrites(grassDescriptorSet.size());
for (uint32_t i = 0; i < scene->GetBlades().size(); i++) {
VkDescriptorBufferInfo grassBufferInfo = {};
grassBufferInfo.buffer = scene->GetBlades()[i]->GetModelBuffer();
grassBufferInfo.offset = 0;
grassBufferInfo.range = sizeof(ModelBufferObject);

// Bind sampler resources to the descriptor
descriptorWrites[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[i].dstSet = grassDescriptorSet[i];
descriptorWrites[i].dstBinding = 0;
descriptorWrites[i].dstArrayElement = 0;
descriptorWrites[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[i].descriptorCount = 1;
descriptorWrites[i].pBufferInfo = &grassBufferInfo;
descriptorWrites[i].pImageInfo = nullptr;
descriptorWrites[i].pTexelBufferView = nullptr;
}
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(),0, nullptr);
}

void Renderer::CreateTimeDescriptorSet() {
Expand Down Expand Up @@ -360,6 +445,66 @@ 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
computeDescriptorSet.resize(scene->GetBlades().size());
VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(computeDescriptorSet.size());
allocInfo.pSetLayouts = layouts;

if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, computeDescriptorSet.data()) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate descriptor set");
}
std::vector<VkWriteDescriptorSet> descriptorWrites(3 * computeDescriptorSet.size());
for (uint32_t i = 0; i < scene->GetBlades().size(); i++) {
VkDescriptorBufferInfo BladeBufferInfo = {};
BladeBufferInfo.buffer = scene->GetBlades()[i]->GetBladesBuffer();
BladeBufferInfo.offset = 0;
BladeBufferInfo.range = NUM_BLADES * sizeof(Blade);

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

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

descriptorWrites[3 * i].sType= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[3 * i].dstSet = computeDescriptorSet[i];
descriptorWrites[3 * i].dstBinding = 0;
descriptorWrites[3 * i].dstArrayElement = 0;
descriptorWrites[3 * i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[3 * i].descriptorCount = 1;
descriptorWrites[3 * i].pBufferInfo = &BladeBufferInfo;
descriptorWrites[3 * i].pImageInfo = nullptr;
descriptorWrites[3 * i].pTexelBufferView = nullptr;

descriptorWrites[3 * i + 1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[3 * i + 1].dstSet = computeDescriptorSet[i];
descriptorWrites[3 * i + 1].dstBinding = 1;
descriptorWrites[3 * i + 1].dstArrayElement = 0;
descriptorWrites[3 * i + 1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[3 * i + 1].descriptorCount = 1;
descriptorWrites[3 * i + 1].pBufferInfo = &CulledBladeBufferInfo;
descriptorWrites[3 * i + 1].pImageInfo = nullptr;
descriptorWrites[3 * i + 1].pTexelBufferView = nullptr;

descriptorWrites[3 * i + 2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[3 * i + 2].dstSet = computeDescriptorSet[i];
descriptorWrites[3 * i + 2].dstBinding = 2;
descriptorWrites[3 * i + 2].dstArrayElement = 0;
descriptorWrites[3 * i + 2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[3 * i + 2].descriptorCount = 1;
descriptorWrites[3 * i + 2].pBufferInfo = &BladeNumBufferInfo;
descriptorWrites[3 * i + 2].pImageInfo = nullptr;
descriptorWrites[3 * i + 2].pTexelBufferView = nullptr;
}
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);

}

void Renderer::CreateGraphicsPipeline() {
Expand Down Expand Up @@ -717,7 +862,8 @@ 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,7 +1030,11 @@ 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

for (size_t i = 0; i < scene->GetBlades().size(); i++) {
vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 2, 1, &computeDescriptorSet[i], 0, nullptr);
vkCmdDispatch(computeCommandBuffer, (int)(NUM_BLADES / WORKGROUP_SIZE+1), 1, 1);
}

// ~ End recording ~
if (vkEndCommandBuffer(computeCommandBuffer) != VK_SUCCESS) {
throw std::runtime_error("Failed to record compute command buffer");
Expand Down Expand Up @@ -976,13 +1126,13 @@ 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[j], 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 @@ -1043,6 +1193,7 @@ Renderer::~Renderer() {

// TODO: destroy any resources you created


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

Expand All @@ -1057,6 +1208,9 @@ Renderer::~Renderer() {
vkDestroyDescriptorSetLayout(logicalDevice, cameraDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, modelDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, timeDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, grassDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, computeDescriptorSetLayout, nullptr);


vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr);

Expand All @@ -1065,3 +1219,7 @@ Renderer::~Renderer() {
vkDestroyCommandPool(logicalDevice, computeCommandPool, nullptr);
vkDestroyCommandPool(logicalDevice, graphicsCommandPool, nullptr);
}




6 changes: 6 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Renderer {
void CreateCameraDescriptorSetLayout();
void CreateModelDescriptorSetLayout();
void CreateTimeDescriptorSetLayout();
void CreateGrassDescriptorSetLayout();
void CreateComputeDescriptorSetLayout();

void CreateDescriptorPool();
Expand Down Expand Up @@ -56,12 +57,17 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;
VkDescriptorSetLayout grassDescriptorSetLayout;
VkDescriptorSetLayout computeDescriptorSetLayout;

VkDescriptorPool descriptorPool;

VkDescriptorSet cameraDescriptorSet;
std::vector<VkDescriptorSet> modelDescriptorSets;
VkDescriptorSet timeDescriptorSet;
std::vector<VkDescriptorSet> grassDescriptorSet;
std::vector<VkDescriptorSet> computeDescriptorSet;


VkPipelineLayout graphicsPipelineLayout;
VkPipelineLayout grassPipelineLayout;
Expand Down
33 changes: 32 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@
#include "Camera.h"
#include "Scene.h"
#include "Image.h"
#include<sstream>

Device* device;
SwapChain* swapChain;
Renderer* renderer;
Camera* camera;

std::string IntToString(int num)
{
std::stringstream ss;
ss << num;
return ss.str();
}
std::string FloatToString(double num)
{
std::stringstream ss;
ss << num;
return ss.str();
}

namespace {
void resizeCallback(GLFWwindow* window, int width, int height) {
if (width == 0 || height == 0) return;
Expand Down Expand Up @@ -143,8 +157,25 @@ int main() {
glfwSetMouseButtonCallback(GetGLFWWindow(), mouseDownCallback);
glfwSetCursorPosCallback(GetGLFWWindow(), mouseMoveCallback);


double origin = 0.f;
double current = 0.f;
double fps = 0;
int frame = 0;

while (!ShouldQuit()) {
glfwPollEvents();
glfwPollEvents();
current = (double)time(NULL);
frame++;
const double elapsed = current - origin;
if (elapsed >= 1) {
fps = frame / elapsed;
frame = 0;
origin = current;
std::string title = "Vulkan Grass Rendering | " + FloatToString(fps) + " FPS " + FloatToString(1000.f / (double)fps) + " ms";
glfwSetWindowTitle(GetGLFWWindow(), title.c_str());
}

scene->UpdateTime();
renderer->Frame();
}
Expand Down
Loading