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
268 changes: 22 additions & 246 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/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/final.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/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/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/performance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
300 changes: 300 additions & 0 deletions instruction.md

Large diffs are not rendered by default.

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 << 17;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
Expand Down
142 changes: 136 additions & 6 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ 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 b0 = { 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr };
VkDescriptorSetLayoutBinding b1 = { 1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr };
VkDescriptorSetLayoutBinding b2 = { 2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr };

std::vector<VkDescriptorSetLayoutBinding> bindings = { b0, b1, b2 };

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 allocate descriptor sets!");
}

}

void Renderer::CreateDescriptorPool() {
Expand All @@ -216,7 +231,9 @@ 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 * (uint32_t)(scene->GetBlades().size()) },

};

VkDescriptorPoolCreateInfo poolInfo = {};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
Expand Down Expand Up @@ -320,6 +337,44 @@ 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

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

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;
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, grassDescriptorSets.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate grass descriptor sets!");
}
std::vector<VkWriteDescriptorSet>descriptorWrites(grassDescriptorSets.size());

for (int i = 0; i < scene->GetBlades().size(); i++) {
VkDescriptorBufferInfo modelBufferInfo = {};
modelBufferInfo.buffer = scene->GetBlades()[i]->GetModelBuffer();
modelBufferInfo.offset = 0;
modelBufferInfo.range = sizeof(ModelBufferObject);

VkDescriptorImageInfo imageInfo = {};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = depthImageView;
imageInfo.sampler = scene->GetBlades()[i]->GetTextureSampler();

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

}

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

void Renderer::CreateTimeDescriptorSet() {
Expand Down Expand Up @@ -360,6 +415,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
computeDescriptorSets.resize(scene->GetBlades().size());

VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout };
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 compute descriptor set");
}

std::vector<VkWriteDescriptorSet> descriptorWrites(3 * computeDescriptorSets.size());
for (int i = 0; i < scene->GetBlades().size(); i++) {
VkDescriptorBufferInfo bladeBufferInfo = {};
bladeBufferInfo.buffer = scene->GetBlades()[i]->GetBladesBuffer();
bladeBufferInfo.offset = 0;
bladeBufferInfo.range = sizeof(Blade) * NUM_BLADES;

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 = &bladeBufferInfo;
descriptorWrites[3 * i + 0].pImageInfo = nullptr;
descriptorWrites[3 * i + 0].pTexelBufferView = nullptr;

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

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 = &culledBladeBufferInfo;
descriptorWrites[3 * i + 1].pImageInfo = nullptr;
descriptorWrites[3 * i + 1].pTexelBufferView = nullptr;

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

// Bind image and sampler resources to the descriptor
VkDescriptorImageInfo imageInfo = {};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = scene->GetModels()[i]->GetTextureView();
imageInfo.sampler = scene->GetModels()[i]->GetTextureSampler();

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 = &numberBladeBufferInfo;
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 +843,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,7 +1010,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 (int i = 0; i < scene->GetBlades().size(); i++) {
vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 2, 1, &computeDescriptorSets[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 +1105,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 @@ -1064,4 +1194,4 @@ Renderer::~Renderer() {
DestroyFrameResources();
vkDestroyCommandPool(logicalDevice, computeCommandPool, nullptr);
vkDestroyCommandPool(logicalDevice, graphicsCommandPool, nullptr);
}
}
10 changes: 10 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,17 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;
// add compute descriptor set layout
VkDescriptorSetLayout computeDescriptorSetLayout;


VkDescriptorPool descriptorPool;

// add grass and compute descriptor sets
std::vector <VkDescriptorSet> grassDescriptorSets;
std::vector<VkDescriptorSet> computeDescriptorSets;


VkDescriptorSet cameraDescriptorSet;
std::vector<VkDescriptorSet> modelDescriptorSets;
VkDescriptorSet timeDescriptorSet;
Expand All @@ -79,4 +87,6 @@ class Renderer {

std::vector<VkCommandBuffer> commandBuffers;
VkCommandBuffer computeCommandBuffer;


};
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Camera.h"
#include "Scene.h"
#include "Image.h"
#include <iostream>

Device* device;
SwapChain* swapChain;
Expand Down Expand Up @@ -143,12 +144,15 @@ int main() {
glfwSetMouseButtonCallback(GetGLFWWindow(), mouseDownCallback);
glfwSetCursorPosCallback(GetGLFWWindow(), mouseMoveCallback);


while (!ShouldQuit()) {
glfwPollEvents();
scene->UpdateTime();
renderer->Frame();
}



vkDeviceWaitIdle(device->GetVkDevice());

vkDestroyImage(device->GetVkDevice(), grassImage, nullptr);
Expand Down
119 changes: 117 additions & 2 deletions src/shaders/compute.comp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,136 @@ struct Blade {
// uint firstInstance; // = 0
// } numBlades;

layout(set = 2, binding = 0) buffer Blades {
Blade blades[];
};

layout(set = 2, binding = 1) buffer CulledBlades {
Blade 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);
}

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

// TODO: Apply forces on every blade and update the vertices in the buffer
uint idx = gl_GlobalInvocationID.x;
Blade blade = blades[idx];
vec3 v0 = blade.v0.xyz;
vec3 v1 = blade.v1.xyz;
vec3 v2 = blade.v2.xyz;
vec3 up = blade.up.xyz;

float o = blade.v0.w;
float h = blade.v1.w;
float w = blade.v2.w;
float s = blade.up.w;

//gravity
vec3 gE = vec3(0.f, -9.8, 0.f);

vec3 surface = normalize(cross(vec3(sin(o), 0.f, cos(o)), up));
vec3 gF = 0.25 * gE * surface;
vec3 g = gE + gF;


// recovery
vec3 Iv2 = v0 + up * h;
vec3 re = s * (Iv2 - v2);


// wind
vec3 windDirection = vec3(1.f, 0.f, 0.f);
float windForce = 8 * sin(totalTime);

float fd = 1 - abs(dot (normalize(windDirection), normalize(v2 - v0)));
float fr = dot(v2 - v0, up) / h;
vec3 wind = windDirection * windForce * fd * fr;

//Total force
v2 += deltaTime * (wind + g + re);

v2 -= up * min(dot(up, v2 - v0), 0);

float lproj = length(v2 - v0 - up * dot(v2 - v0, up));

v1 = v0 + h * up * max(1 - lproj / h, 0.05 * max(lproj / h, 1));

float L0 = distance(v2, v0);
float L1 = distance(v2, v1) + distance(v1, v0);
float n = 3;
float L = (2.0 * L0 + (n - 1) * L1) / (n + 1);

float ratio = h / L;
vec3 v1Cor = v0 + ratio * (v1 - v0);
vec3 v2Cor = v1Cor + ratio * (v2 - v1);

blades[idx].v1.xyz = v1Cor;
blades[idx].v2.xyz = v2Cor;


// 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
}
v0 = blade.v0.xyz;
v1 = blade.v1.xyz;
v2 = blade.v2.xyz;

// Orientation culling
bool orient_cull = false;
mat4 invView = inverse(camera.view);
vec3 viewWorld = (invView * vec4(0,0,1,0)).xyz;
if (abs(dot(surface, normalize(viewWorld))) > 0.1) {
orient_cull = true;
}


// View frustum culling
bool frustum_cull = false;
vec3 mid = 0.25 * v0 + 0.5 * v1 + 0.25 * v2;

mat4 vp = camera.proj * camera.view;

vec4 ndc0 = vp * vec4(v0, 1.f);
vec4 ndc2 = vp * vec4(v2, 1.f);
vec4 ndcm = vp * vec4(mid, 1.f);

float tolerance = 0.1f;
float h0 = ndc0.w + tolerance;
float h2 = ndc2.w + tolerance;
float hm = ndcm.w + tolerance;

frustum_cull = !(inBounds(ndc0.x, h0) && inBounds(ndc0.y, h0) && inBounds(ndc0.z, h0)
&& inBounds(ndc2.x, h2) && inBounds(ndc2.y, h2) && inBounds(ndc2.z, h2)
&& inBounds(ndcm.x, hm) && inBounds(ndcm.y, hm) && inBounds(ndcm.z, hm));

//distance culling
vec4 camera = invView * vec4(0.f, 0.f, 0.f, 1.f);
vec3 cxyz = camera.xyz;
float dproj = length(v0 - cxyz - up * dot(up, (v0 - cxyz)));
float dmax = 10.0;
int num = 10;

bool distance_cull = (idx % num) < floor(num * (1.0 - dproj / dmax));


if(!orient_cull && !frustum_cull && !distance_cull ) {
culledBlades[atomicAdd(numBlades.vertexCount , 1)] = blade;
}
}
Loading