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: Josh Lawrence #22

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
Binary file added 2017-11-07 00-48-58.mp4
Binary file not shown.
361 changes: 64 additions & 297 deletions README.md

Large diffs are not rendered by default.

Binary file added data.xlsx
Binary file not shown.
Binary file added img/data.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/graphcullcompare.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/graphcullvsnocull.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/graphfalloffvsnofalloff.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.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/grassvid.mp4
Binary file not shown.
6 changes: 6 additions & 0 deletions settingUp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
when you clone make sure its a recursive clone
have git bash installed.
rclick in window and bring up a git bash
do (without quotes):
"mkdir build; cd build; cmake-gui .."
click configure (might have to copy ), then generate, then open project
183 changes: 177 additions & 6 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Renderer::Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* c
CreateRenderPass();
CreateCameraDescriptorSetLayout();
CreateModelDescriptorSetLayout();
CreateGrassModelDescriptorSetLayout();
CreateTimeDescriptorSetLayout();
CreateComputeDescriptorSetLayout();
CreateDescriptorPool();
Expand Down Expand Up @@ -172,6 +173,27 @@ void Renderer::CreateModelDescriptorSetLayout() {
}
}

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

void Renderer::CreateTimeDescriptorSetLayout() {
// Describe the binding of the descriptor set layout
VkDescriptorSetLayoutBinding uboLayoutBinding = {};
Expand All @@ -198,24 +220,59 @@ 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 allGrassLayoutBinding = {};
allGrassLayoutBinding.binding = 0;
allGrassLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;//no need for us to access once on the gpu
allGrassLayoutBinding.descriptorCount = 1;
allGrassLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
allGrassLayoutBinding.pImmutableSamplers = nullptr;

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

VkDescriptorSetLayoutBinding numGrassLayoutBinding = {};
numGrassLayoutBinding.binding = 2;
numGrassLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
numGrassLayoutBinding.descriptorCount = 1;
numGrassLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
numGrassLayoutBinding.pImmutableSamplers = nullptr;
std::vector<VkDescriptorSetLayoutBinding> bindings = { allGrassLayoutBinding, culledGrassLayoutBinding, numGrassLayoutBinding };

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

void Renderer::CreateDescriptorPool() {
// Describe which descriptor types that the descriptor sets will contain
const uint32_t numModels = scene->GetModels().size();
const uint32_t numBlades = scene->GetBlades().size();
const uint32_t numModelsAndBlades = numModels + numBlades;
std::vector<VkDescriptorPoolSize> poolSizes = {
// 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()) },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER , numModelsAndBlades },

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

// Time (compute)
{ 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, static_cast<uint32_t>(3 * numBlades)}
};

VkDescriptorPoolCreateInfo poolInfo = {};
Expand Down Expand Up @@ -320,6 +377,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
grassDescriptorSets.resize(scene->GetBlades().size());

// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { grassModelDescriptorSetLayout};//same as model without the sampler
//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;

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

const uint32_t numWrites = 1;
std::vector<VkWriteDescriptorSet> descriptorWrites(numWrites * 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[numWrites * i + 0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[numWrites * i + 0].dstSet = grassDescriptorSets[i];
descriptorWrites[numWrites * i + 0].dstBinding = 0;
descriptorWrites[numWrites * i + 0].dstArrayElement = 0;
descriptorWrites[numWrites * i + 0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[numWrites * i + 0].descriptorCount = 1;
descriptorWrites[numWrites * i + 0].pBufferInfo = &modelBufferInfo;
descriptorWrites[numWrites * i + 0].pImageInfo = nullptr;
descriptorWrites[numWrites * i + 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 @@ -360,6 +456,74 @@ 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());

// Describe the desciptor set
VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout };//same as model without the sampler
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;

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

const uint32_t numWrites = 3;
std::vector<VkWriteDescriptorSet> descriptorWrites(numWrites * computeDescriptorSets.size());

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

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

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

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

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

descriptorWrites[numWrites * i + 2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[numWrites * i + 2].dstSet = computeDescriptorSets[i];
descriptorWrites[numWrites * i + 2].dstBinding = 2;
descriptorWrites[numWrites * i + 2].dstArrayElement = 0;
descriptorWrites[numWrites * i + 2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrites[numWrites * i + 2].descriptorCount = 1;
descriptorWrites[numWrites * i + 2].pBufferInfo = &numBladesBufferInfo;
descriptorWrites[numWrites * i + 2].pImageInfo = nullptr;
descriptorWrites[numWrites * 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 @@ -654,7 +818,8 @@ void Renderer::CreateGrassPipeline() {
colorBlending.blendConstants[2] = 0.0f;
colorBlending.blendConstants[3] = 0.0f;

std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { cameraDescriptorSetLayout, modelDescriptorSetLayout };
std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { cameraDescriptorSetLayout, grassModelDescriptorSetLayout };
//std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { cameraDescriptorSetLayout, modelDescriptorSetLayout };

// Pipeline layout: used to specify uniform values
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
Expand Down Expand Up @@ -717,7 +882,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 +1049,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, NUM_BLADES / WORKGROUP_SIZE, 1, 1);

// ~ End recording ~
if (vkEndCommandBuffer(computeCommandBuffer) != VK_SUCCESS) {
Expand Down Expand Up @@ -976,13 +1145,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 +1227,7 @@ Renderer::~Renderer() {
vkDestroyDescriptorSetLayout(logicalDevice, cameraDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, modelDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, timeDescriptorSetLayout, nullptr);
vkDestroyDescriptorSetLayout(logicalDevice, computeDescriptorSetLayout, 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 @@ -17,6 +17,7 @@ class Renderer {

void CreateCameraDescriptorSetLayout();
void CreateModelDescriptorSetLayout();
void CreateGrassModelDescriptorSetLayout();
void CreateTimeDescriptorSetLayout();
void CreateComputeDescriptorSetLayout();

Expand Down Expand Up @@ -55,12 +56,16 @@ class Renderer {

VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout grassModelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;
VkDescriptorSetLayout computeDescriptorSetLayout;

VkDescriptorPool descriptorPool;

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

VkPipelineLayout graphicsPipelineLayout;
Expand Down
40 changes: 36 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@
#include "Camera.h"
#include "Scene.h"
#include "Image.h"

#include <sstream>
Device* device;
SwapChain* swapChain;
Renderer* renderer;
Camera* camera;
const float planeDim = 15.f;//15 was original val
const float WIDTH = 1280;
const float HEIGHT = 960;
double old = 0.f;
double current = 0.f;
int fps = 0;
int fpstracker = 0;

std::string convertIntToString(int number)
{
std::stringstream ss;
ss << number;
return ss.str();
}

std::string convertFloatToString(double number)
{
std::stringstream ss;
ss << number;
return ss.str();
}

namespace {
void resizeCallback(GLFWwindow* window, int width, int height) {
Expand Down Expand Up @@ -67,7 +88,7 @@ namespace {

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

unsigned int glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
Expand All @@ -90,7 +111,7 @@ int main() {

swapChain = device->CreateSwapChain(surface, 5);

camera = new Camera(device, 640.f / 480.f);
camera = new Camera(device, WIDTH / HEIGHT);

VkCommandPoolCreateInfo transferPoolInfo = {};
transferPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Expand All @@ -116,7 +137,6 @@ int main() {
grassImageMemory
);

float planeDim = 15.f;
float halfWidth = planeDim * 0.5f;
Model* plane = new Model(device, transferCommandPool,
{
Expand Down Expand Up @@ -145,6 +165,18 @@ int main() {

while (!ShouldQuit()) {
glfwPollEvents();

//see rasterizer hw
current = (double)time(NULL);
fpstracker++;
const double elapsed = current - old;
if (elapsed >= 1) {
fps = (int)(fpstracker / elapsed);
fpstracker = 0;
old = current;
std::string title = "Vulkan Grass Rendering | " + convertIntToString(fps) + " FPS " + convertFloatToString(1000.f/(double)fps) + " ms";
glfwSetWindowTitle(GetGLFWWindow(), title.c_str());
}
scene->UpdateTime();
renderer->Frame();
}
Expand Down
Loading