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
307 changes: 22 additions & 285 deletions README.md

Large diffs are not rendered by default.

Binary file added img/blade-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 added img/record.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 << 20;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
Expand Down
2,058 changes: 1,093 additions & 965 deletions src/Renderer.cpp

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ class Renderer {
VkDescriptorSetLayout cameraDescriptorSetLayout;
VkDescriptorSetLayout modelDescriptorSetLayout;
VkDescriptorSetLayout timeDescriptorSetLayout;

VkDescriptorSetLayout computeDescriptorSetLayout;
VkDescriptorSetLayout grassDescriptorSetLayout;

VkDescriptorPool descriptorPool;

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


VkPipelineLayout graphicsPipelineLayout;
VkPipelineLayout grassPipelineLayout;
Expand Down
25 changes: 22 additions & 3 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 <sstream>

Device* device;
SwapChain* swapChain;
Expand Down Expand Up @@ -67,7 +68,7 @@ namespace {

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

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

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

camera = new Camera(device, 640.f / 480.f);
camera = new Camera(device, 1280.f / 960.f);

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

float planeDim = 15.f;
float planeDim = 45.f;
float halfWidth = planeDim * 0.5f;
Model* plane = new Model(device, transferCommandPool,
{
Expand All @@ -143,10 +144,28 @@ int main() {
glfwSetMouseButtonCallback(GetGLFWWindow(), mouseDownCallback);
glfwSetCursorPosCallback(GetGLFWWindow(), mouseMoveCallback);

double fps = 0;
double timebase = 0;
int frame = 0;

while (!ShouldQuit()) {
glfwPollEvents();
frame++;
double time = glfwGetTime();
if (time - timebase > 1.0) {
fps = frame / (time - timebase);
timebase = time;
frame = 0;
}
scene->UpdateTime();
renderer->Frame();
std::ostringstream ss;
ss << "[";
ss.precision(1);
ss << std::fixed << fps;
ss << " fps] ";
glfwSetWindowTitle(GetGLFWWindow(), ss.str().c_str());

}

vkDeviceWaitIdle(device->GetVkDevice());
Expand Down
134 changes: 105 additions & 29 deletions src/shaders/compute.comp
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,128 @@
layout(local_size_x = WORKGROUP_SIZE, local_size_y = 1, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform CameraBufferObject {
mat4 view;
mat4 proj;
mat4 view;
mat4 proj;
} camera;

layout(set = 1, binding = 0) uniform Time {
float deltaTime;
float totalTime;
float deltaTime;
float totalTime;
};

struct Blade {
vec4 v0;
vec4 v1;
vec4 v2;
vec4 up;
vec4 v0;
vec4 v1;
vec4 v2;
vec4 up;
};

// TODO: Add bindings to:
// 1. Store the input blades
// 2. Write out the culled blades
// 3. Write the total number of blades remaining

// 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 InputBlades {
Blade blades[];
} inputBlades;

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

layout(set = 2, binding = 2) buffer NumBlades {
uint vertexCount;
uint instanceCount;
uint firstVertex;
uint firstInstance;
} numBlades;

bool inBounds(float value, float bounds) {
return (value >= -bounds) && (value <= 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
// TODO: Apply forces on every blade and update the vertices in the buffer
uint index = gl_GlobalInvocationID.x;
Blade blade = inputBlades.blades[index];
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;
// Gravity
vec3 gE = vec3(0.0, -9.8, 0.0);
vec3 f = normalize(cross(up, vec3(sin(blade.v0.w), 0.0, cos(blade.v0.w))));
vec3 gF = 0.25 * 9.8 * f;
vec3 gravity = gE + gF;
// Recovery
vec3 recovery = (v0 + h * up - v2) * stiffness;

// Wind + Repulsion
vec3 dir = vec3(1.0, 0.0, 0.0) * cos(totalTime);
float fd = 1 - abs(dot(normalize(dir), normalize(v2 - v0)));
float fr = dot((v2 - v0), up) / h;
vec3 wind = vec3(0);

// Get position on a circle based on time
float x = cos(totalTime * 2.0);
float y = sin(totalTime * 2.0);
vec3 repulsor = vec3(x, 0.0, y) * vec3(10.0);
float dist = (distance(repulsor, v0) / 1.0);
vec3 repulseDir = v0 - repulsor;
float influenceRadius = 3.0 * abs(sin(totalTime / 7.0)) + 1.0;
if (dist < influenceRadius) {
wind = repulseDir * dist * fd * fr;
} else {
wind = dir * fd * fr;
}

v2 += (recovery + gravity + wind) * deltaTime;

v2 = v2 - up * min(dot(up, v2 - v0), 0.0);
float lproj = length(v2 - v0 - up * dot(v2 - v0, up));
v1 = v0 + h * up * max(1.0 - (lproj / h), 0.05 * max(lproj / h, 1.0));
float n = 2.0;
float L0 = distance(v0, v2);
float L1 = distance(v0, v1) + distance(v1, v2);
float L = ((2.0 * L0) + (n - 1.0) * L1) / (n + 1.0);
float r = h / L;
vec3 v1corr = v0 + r * (v1 - v0);
vec3 v2corr = v1corr + r * (v2 - v1);
inputBlades.blades[index].v1.xyz = v1corr;
inputBlades.blades[index].v2.xyz = v2corr;

// CVULLINH:

// 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
#define ORIENTATION_CULL;
#define FRUSTUM_CULL;
#define DISTANCE_CULL;

vec4 eye = inverse(camera.view) * vec4(0.0, 0.0, 0.0, 1.0);

#ifdef ORIENTATION_CULL
vec3 dir_c = normalize(eye.xyz - v0); vec3 dir_b = f;
if (dot(dir_c, dir_b) > 0.9) {
return;
}
#endif

#ifdef FRUSTUM_CULL
vec4 v0_prime = camera.proj * camera.view * vec4(v0, 1.0);
float hg = v0_prime.w + 2.0;
if (!inBounds(v0_prime.x, hg) || !inBounds(v0_prime.y, hg) || !inBounds(v0_prime.z, hg)) {
return;
}
#endif

#ifdef DISTANCE_CULL
float d_proj = length(v0 - eye.xyz - up * dot(up, v0 - eye.xyz));
if (mod(index, 10.0) > floor(10.0 * (1 - (d_proj / 100.0)))) {
return;
}
#endif
culledBlades.blades[atomicAdd(numBlades.vertexCount, 1)] = inputBlades.blades[index];
}
24 changes: 20 additions & 4 deletions src/shaders/grass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@
#extension GL_ARB_separate_shader_objects : enable

layout(set = 0, binding = 0) uniform CameraBufferObject {
mat4 view;
mat4 proj;
mat4 view;
mat4 proj;
} camera;

// TODO: Declare fragment shader inputs
//
layout(location = 0) in vec4 pos;
layout(location = 1) in vec4 nor;
layout(location = 2) in vec2 uv;

layout(location = 0) out vec4 outColor;

vec3 cosPalette( float t, vec3 a, vec3 b, vec3 c, vec3 d ){
return a + b*cos( 6.28318*(c*t+d) );
}

void main() {
// TODO: Compute fragment color
// TODO: Compute fragment color
outColor = vec4(cosPalette(uv[1],vec3(0.2,0.5,0.3),vec3(0.0,0.5,0.7),vec3(1.0,1.0,1.0),vec3(0.0,0.3,0.7)), 1.0);
outColor *= pos/vec4(10.0);

vec4 topColor = vec4(37, 189, 5, 255) / vec4(255);
vec4 bottomColor = vec4(107, 169, 73, 255) / vec4(255);
vec4 lightDir = normalize(vec4(1.0, 0.0, -1.0, 0.0));
outColor = mix(bottomColor, topColor, uv.y) * max(dot(lightDir, nor), 0.0);


outColor = vec4(1.0);
//outColor = vec4(1.0);
}
31 changes: 22 additions & 9 deletions src/shaders/grass.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@
layout(vertices = 1) out;

layout(set = 0, binding = 0) uniform CameraBufferObject {
mat4 view;
mat4 proj;
mat4 view;
mat4 proj;
} camera;

// TODO: Declare tessellation control shader inputs and outputs
layout(location = 0) in vec4 v0[];
layout(location = 1) in vec4 v1[];
layout(location = 2) in vec4 v2[];
layout(location = 3) in vec4 up[];

layout(location = 0) out vec4 v0_out[];
layout(location = 1) out vec4 v1_out[];
layout(location = 2) out vec4 v2_out[];
layout(location = 3) out vec4 up_out[];

void main() {
// Don't move the origin location of the patch
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;

// TODO: Write any shader outputs
v0_out[gl_InvocationID] = v0[gl_InvocationID];
v1_out[gl_InvocationID] = v1[gl_InvocationID];
v2_out[gl_InvocationID] = v2[gl_InvocationID];
up_out[gl_InvocationID] = up[gl_InvocationID];

// TODO: Set level of tesselation
// gl_TessLevelInner[0] = ???
// gl_TessLevelInner[1] = ???
// gl_TessLevelOuter[0] = ???
// gl_TessLevelOuter[1] = ???
// gl_TessLevelOuter[2] = ???
// gl_TessLevelOuter[3] = ???
gl_TessLevelInner[0] = 2.0;
gl_TessLevelInner[1] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 2.0;
gl_TessLevelOuter[2] = 5.0;
gl_TessLevelOuter[3] = 2.0;
}
30 changes: 25 additions & 5 deletions src/shaders/grass.tese
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@
layout(quads, equal_spacing, ccw) in;

layout(set = 0, binding = 0) uniform CameraBufferObject {
mat4 view;
mat4 proj;
mat4 view;
mat4 proj;
} camera;

// TODO: Declare tessellation evaluation shader inputs and outputs
layout(location = 0) in vec4 v0[];
layout(location = 1) in vec4 v1[];
layout(location = 2) in vec4 v2[];
layout(location = 3) in vec4 up[];

layout(location = 0) out vec4 pos;
layout(location = 1) out vec4 nor;
layout(location = 2) out vec2 uv;

void main() {
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
uv = vec2(u, v);

vec3 a = v0[0].xyz + v * (v1[0].xyz - v0[0].xyz);
vec3 b = v1[0].xyz + v * (v2[0].xyz - v1[0].xyz);
vec3 c = a + v * (b - a);
vec3 t1 = vec3(sin(v0[0].w), 0.0, cos(v0[0].w));
vec3 c0 = c - v2[0].w * t1;
vec3 c1 = c + v2[0].w * t1;
vec3 t0 = normalize(b - a);
nor.xyz = normalize(cross(t0, t1));
float t = u + 0.5 * v - u * v;
pos.xyz = (1.0 - t) * c0 + t * c1;
gl_Position = camera.proj * camera.view * vec4(pos.xyz, 1.0);

// TODO: Use u and v to parameterize along the grass blade and output positions for each vertex of the grass blade
}
Loading