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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
bin
external
312 changes: 39 additions & 273 deletions README.md

Large diffs are not rendered by default.

Binary file modified bin/Release/vulkan_grass_rendering.exe
Binary file not shown.
Binary file added img/blades_chart.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_chart.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 modified img/grass.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 modified img/grass2.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/no_ori.PNG
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 << 16;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
Expand Down
151 changes: 148 additions & 3 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,40 @@ 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

// Describe the binding of the descriptor set layout
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 numBladesLayoutBinding = {};
numBladesLayoutBinding.binding = 2;
numBladesLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
numBladesLayoutBinding.descriptorCount = 1;
numBladesLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
numBladesLayoutBinding.pImmutableSamplers = nullptr;

std::vector<VkDescriptorSetLayoutBinding> bindings = { bladeLayoutBinding, culledbladeLayoutBinding, numBladesLayoutBinding };

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

void Renderer::CreateDescriptorPool() {
Expand All @@ -216,6 +250,7 @@ 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, 3 }
};

VkDescriptorPoolCreateInfo poolInfo = {};
Expand Down Expand Up @@ -320,6 +355,42 @@ 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
auto& bladeGroups = scene->GetBlades();
grassDescriptorSets.resize(bladeGroups.size());
// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { modelDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = grassDescriptorSets.size();
allocInfo.pSetLayouts = layouts;

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

std::vector<VkWriteDescriptorSet> descriptorWrites( grassDescriptorSets.size());

for (uint32_t i = 0; i < bladeGroups.size(); ++i) {
VkDescriptorBufferInfo modelBufferInfo = {};
modelBufferInfo.buffer = scene->GetModels()[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 descriptor sets
vkUpdateDescriptorSets(logicalDevice, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}

void Renderer::CreateTimeDescriptorSet() {
Expand Down Expand Up @@ -360,6 +431,73 @@ 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
auto& bladeGroups = scene->GetBlades();
computeDescriptorSets.resize(bladeGroups.size());
// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { bladeDescriptorSetLayout };
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = computeDescriptorSets.size();
allocInfo.pSetLayouts = layouts;

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

std::vector<VkWriteDescriptorSet> descriptorWrites( 3 * computeDescriptorSets.size());

for (uint32_t i = 0; i < bladeGroups.size(); ++i) {
VkDescriptorBufferInfo bladesBufferInfo = {};
bladesBufferInfo.buffer = bladeGroups[i]->GetBladesBuffer();
bladesBufferInfo.offset = 0;
bladesBufferInfo.range = sizeof(Blade) * NUM_BLADES;

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

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

std::array<VkWriteDescriptorSet, 3> descriptorWrites = {};
descriptorWrites[3*i + 0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[3*i + 0].dstSet = computeDescriptorSets[i];
descriptorWrites[3*i + 0].dstBinding = 0;
descriptorWrites[3*i + 0].dstArrayElement = 0;
descriptorWrites[3*i + 0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[3*i + 0].descriptorCount = 1;
descriptorWrites[3*i + 0].pBufferInfo = &bladesBufferInfo;
descriptorWrites[3*i + 0].pImageInfo = nullptr;
descriptorWrites[3*i + 0].pTexelBufferView = nullptr;

descriptorWrites[3*i + 1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[3*i + 1].dstSet = computeDescriptorSets[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 = &culledBladesBufferInfo;
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 = computeDescriptorSets[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 = &numBladesBufferInfo;
descriptorWrites[3*i + 2].pImageInfo = nullptr;
descriptorWrites[3*i + 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 +855,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, bladeDescriptorSetLayout };

// Create pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
Expand Down Expand Up @@ -884,6 +1022,10 @@ 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, &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 @@ -976,13 +1118,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, &grassDescriptorSets[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 @@ -1057,6 +1200,8 @@ Renderer::~Renderer() {
vkDestroyDescriptorSetLayout(logicalDevice, cameraDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, modelDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, timeDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, bladeDescriptorSetLayout, nullptr);


vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr);

Expand Down
5 changes: 5 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;

VkDescriptorSetLayout bladeDescriptorSetLayout;

VkDescriptorPool descriptorPool;

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

std::vector<VkDescriptorSet> grassDescriptorSets;
std::vector<VkDescriptorSet> computeDescriptorSets;

VkPipelineLayout graphicsPipelineLayout;
VkPipelineLayout grassPipelineLayout;
VkPipelineLayout computePipelineLayout;
Expand Down
128 changes: 120 additions & 8 deletions src/shaders/compute.comp
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,141 @@ struct Blade {

// The project is using vkCmdDrawIndirect to use a buffer as the arguments for a draw call
// This is sort of an advanced feature so we've showed you what this buffer should look like
//
// layout(set = ???, binding = ???) buffer NumBlades {
// uint vertexCount; // Write the number of blades remaining here
// uint instanceCount; // = 1
// uint firstVertex; // = 0
// uint firstInstance; // = 0
// } numBlades;
layout(set = 2, binding = 0) buffer AllBlades {
Blade blades[];
} allBlades;

layout(set = 2, binding = 1) buffer CulledBlades {
Blade blades[];
} culledBlades;

layout(set = 2, binding = 2) buffer NumBlades {
uint vertexCount; // Write the number of blades remaining here
uint instanceCount; // = 1
uint firstVertex; // = 0
uint firstInstance; // = 0
} numBlades;

bool inBounds(float value, float bounds) {
return (value >= -bounds) && (value <= bounds);
}

bool isBoundsVec3(vec3 pos, float bounds) {
return inBounds(pos.x, bounds) &&
inBounds(pos.y, bounds) &&
inBounds(pos.z, bounds);
}

const vec3 GRAVITY = vec3(0.0, -9.8, 0.0);
const float DISTANCE_MAX = 1.0;
const uint DISTANCE_LEVELS_N= 12;

#define View_frustum_test 1
#define Orientation_test 1
#define Distance_test 0

void main() {

// Reset the number of blades to 0
if (gl_GlobalInvocationID.x == 0) {
// numBlades.vertexCount = 0;
numBlades.vertexCount = 0;
}
barrier(); // Wait till all threads reach this point

uint idx = gl_GlobalInvocationID.x;

Blade blade = allBlades.blades[idx];
// unpack data
float dirAngle = blade.v0.w;
float h = blade.v1.w;
float w = blade.v2.w;
float stiffness = blade.up.w;

vec3 v0 = blade.v0.xyz;
vec3 v1 = blade.v1.xyz;
vec3 v2 = blade.v2.xyz;
vec3 up = blade.up.xyz;

vec3 dir_b = vec3(sin(dirAngle), 0.0, cos(dirAngle));

// TODO: Apply forces on every blade and update the vertices in the buffer

// GRAVITY
vec3 f = normalize(cross(up, dir_b));
vec3 gF = 9.8 * f / 4.0;
vec3 gravity = GRAVITY + gF;

// RECOVERY
vec3 recovery = (v0 + h * up - v2) * stiffness;

// WIND
vec3 w_v0 = vec3(1, 1, 0.5) * sin(v0.y + v0.x + totalTime);
float fd = 1.0 - abs(dot(normalize(w_v0), normalize(v2 - v0)));
float fr = dot((v2 - v0), up) / h;
float theta = fd * fr;
vec3 wind = w_v0 * theta;

// Update v2 with all forces
v2 += (gravity + recovery + wind) * deltaTime;

allBlades.blades[idx].v1.xyz = v1;
allBlades.blades[idx].v2.xyz = v2;


// State validation
// 1. v2 must be above the local plane
v2 = v2 - up * min(dot(up, v2 - v0), 0.0);
// 2. Calculate v1's position given v2
float l_proj = length(v2 - v0 - dot(v2 - v0, up) * up);
v1 = v0 + h * up * max(1.0 - (l_proj / h), 0.05 * max(l_proj / h, 1.0));
// 3. ensure the length of curve is not larger than the height of blade
float L0 = distance(v0, v2);
float L1 = distance(v0, v1) + distance(v1, v2);
float L = (2.0 * L0 + L1) / 3.0; // BEZIER degree to be 2

float r = h / L;
vec3 v1_corr = v0 + r * (v1 - v0);
vec3 v2_corr = v1_corr + r * (v2 - v1);

allBlades.blades[idx].v1.xyz = v1_corr;
allBlades.blades[idx].v2.xyz = v2_corr;

// TODO: Cull blades that are too far away or not in the camera frustum and write them
// to the culled blades buffer
// Note: to do this, you will need to use an atomic operation to read and update numBlades.vertexCount
// You want to write the visible blades to the buffer without write conflicts between threads

// Culling
vec3 cameraPos = (inverse(camera.view) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;

#if Orientation_test
// Orientation test
// dir_c * dir_b < 0.9 -> blade culled
vec3 dir_c = normalize(cameraPos - v0);
if(abs(dot(dir_c, dir_b)) > 0.9) { return; }
#endif

#if View_frustum_test
// View-frustum test
vec4 p_v0 = camera.proj * camera.view * vec4(v0, 1.0);
vec4 p_v2 = camera.proj * camera.view * vec4(v2, 1.0);
vec4 p_m = camera.proj * camera.view * vec4((v0 + v2) / 4.0 + v1 / 2.0, 1.0);

if (!isBoundsVec3(p_v0.xyz, p_v0.w + 2.0) ||
!isBoundsVec3(p_v2.xyz, p_v2.w + 2.0) ||
!isBoundsVec3(p_m.xyz, p_m.w + 2.0)) {
return;
}
#endif

#if Distance_test
// Distance test
// n distance levels over [0, d_max]
float d_proj = length(v0 - cameraPos - up * dot(up, v0 - cameraPos));
if (mod(idx, DISTANCE_LEVELS_N) > floor(DISTANCE_LEVELS_N * (1.0 - (d_proj / DISTANCE_MAX)))) {
return;
}
#endif

culledBlades.blades[atomicAdd(numBlades.vertexCount, 1)] = allBlades.blades[idx];
}
Loading