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: Ziyu Li #8

Open
wants to merge 2 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
303 changes: 21 additions & 282 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 removed img/blade_model.jpg
Binary file not shown.
Binary file removed img/cube_demo.png
Binary file not shown.
Binary file added img/dist.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 removed img/grass.gif
Binary file not shown.
Binary file added img/grass_s.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/grass_sdist.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/or.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/view.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 << 14;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
Expand Down
2,074 changes: 1,127 additions & 947 deletions src/Renderer.cpp

Large diffs are not rendered by default.

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

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> grassDescriptorSets;
std::vector<VkDescriptorSet> computeDescriptorSets;


VkPipelineLayout graphicsPipelineLayout;
VkPipelineLayout grassPipelineLayout;
Expand All @@ -71,6 +77,8 @@ class Renderer {
VkPipeline grassPipeline;
VkPipeline computePipeline;

VkFormat depthFormat;

std::vector<VkImageView> imageViews;
VkImage depthImage;
VkDeviceMemory depthImageMemory;
Expand Down
141 changes: 126 additions & 15 deletions src/shaders/compute.comp
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,147 @@ struct Blade {
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 = 2) buffer NumBlades {
uint vertexCount; // Write the number of blades remaining here
uint instanceCount; // = 1
uint firstVertex; // = 0
uint firstInstance; // = 0
} numBlades;

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

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

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

bool inBound(float value, float a, float b) {
return (value >= a) && (value <= b);
}

vec3 fp(vec3 p0, vec3 p1, vec3 p2, float v)
{
return p0 + v*(p1 - p0) + v*(p1 + v*(p2 - p1) - p0 + v*(p1 - p0));
}


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
barrier();

uint idx = gl_GlobalInvocationID.x;

Blade b = blades[idx];
vec3 up = b.up.xyz;
vec3 p0 = b.v0.xyz; vec3 p1 = b.v1.xyz; vec3 p2 = b.v2.xyz;
vec3 face = normalize(cross(up, vec3(sin(b.v0.w), 0, cos(b.v0.w))));


vec3 g = vec3(0, -9.81, 0);
vec3 gravityForce = face * 0.25 * g + g;

float h = b.v1.w;
vec3 iv2 = p0 + up * h;
float maxc = 1.8/ min(h, 1.8);
vec3 recoveryForce = (iv2 - p2) * b.up.w * maxc;


vec3 windDirection = normalize(vec3(0.5, 0, 0.5));
//vec3 windDirection = -normalize(p0.xyz);
float m = dot(windDirection, vec3(p0.x, 0.0, p0.z));
float t = totalTime*3.0;
float s = cos((m + t) / 6.0);
float dirAlign = 1.0 - abs(dot(windDirection ,normalize(p2 - p0)));
vec3 windForce = windDirection * dirAlign * dot(up, p2 - p0) * s * 4.0 / h;


vec3 TotalForce = gravityForce + recoveryForce + windForce;
vec3 dF = TotalForce * deltaTime;
vec3 v2 = p2 + dF - min(dot(up, p2 + dF - p0), 0.0) * up;
float lproj_h = length(v2 - p0 - up * dot(v2 - p0, up)) / h;
vec3 v1 = h * up * max(1.0 - lproj_h , max(lproj_h, 1.0) / 20.0) + p0;


float _n = 3;
float L0 = length(v2 - p0);
float L1 = length(v1 - p0) + length(v2 - v1);
float L = (L0 * 2.0 + (_n - 1.0) * L1) / (1.0 + _n);
float r = h / L;
b.v1.xyz = p0 + r*(v1 - p0);
b.v2.xyz = b.v1.xyz + r*(v2 - v1);
blades[idx] = b;


// Culling
mat4 invView = inverse(camera.view);
mat4 viewProj = camera.proj * camera.view;
vec3 viewVectorWorld = (invView * vec4(0,0,1,0)).xyz;
p0 = b.v0.xyz; p1 = b.v1.xyz; p2 = b.v2.xyz;


vec4 projPosV0= viewProj * vec4(p0, 1.0);
vec4 projPosV2= viewProj * vec4(p2, 1.0);
projPosV0 /= projPosV0.w;
projPosV2 /= projPosV2.w;

vec4 projCenter1 = viewProj * vec4(fp(p0, p1, p2, 0.25), 1.0);
vec4 projCenter3 = viewProj * vec4(fp(p0, p1, p2, 0.75), 1.0);
vec4 projCenter2 = viewProj * vec4(0.25*p0 * 0.5*p1 * 0.25*p2, 1.0);
projCenter1 /= projCenter1.w;
projCenter2 /= projCenter2.w;
projCenter3 /= projCenter3.w;

float clipVal = 1.3;
float near = 0.1;
float far = 100.0;

bool mV0x = inBounds(projPosV0.x, clipVal);
bool mV0y = inBounds(projPosV0.y, clipVal);
bool mV0z = inBound(projPosV0.z, 0.0, 1.0);
bool mV0 = mV0x && mV0y && mV0z;

bool mV2x = inBounds(projPosV2.x, clipVal);
bool mV2y = inBounds(projPosV2.y, clipVal);
bool mV2z = inBound(projPosV2.z, 0.0, 1.0);
bool mV2 = mV2x && mV2y && mV2z;

bool mP1x = inBounds(projCenter1.x, clipVal);
bool mP1y = inBounds(projCenter1.y, clipVal);
bool mP1z = inBound(projCenter1.z, 0.0, 1.0);
bool mP1 = mP1x && mP1y && mP1z;

bool mP2x = inBounds(projCenter2.x, clipVal);
bool mP2y = inBounds(projCenter2.y, clipVal);
bool mP2z = inBound(projCenter2.z, 0.0, 1.0);
bool mP2 = mP2x && mP2y && mP2z;

bool mP3x = inBounds(projCenter3.x, clipVal);
bool mP3y = inBounds(projCenter3.y, clipVal);
bool mP3z = inBound(projCenter3.z, 0.0, 1.0);
bool mP3 = mP3x && mP3y && mP3z;

// TODO: Apply forces on every blade and update the vertices in the buffer
bool culledByFrustum = !(mV0 && mV2 && mP1 && mP2 && mP3);
bool culledByOrientation = abs(dot(normalize(vec3(viewVectorWorld.x, 0.0, viewVectorWorld.z)), face)) < 0.11;
bool culledByDistance = ((2.0 * near) / (far + near - projPosV0.z * (far - near))) > 0.95;

// 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
if(!culledByOrientation && !culledByFrustum && !culledByDistance) culledBlades[atomicAdd(numBlades.vertexCount , 1)] = b;
}

16 changes: 14 additions & 2 deletions src/shaders/grass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ layout(set = 0, binding = 0) uniform CameraBufferObject {
} camera;

// TODO: Declare fragment shader inputs
layout(location = 0) in vec4 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec3 Wind;
layout(location = 3) in vec2 UV;

layout(location = 0) out vec4 outColor;

void main() {
// TODO: Compute fragment color
vec3 upper = vec3(151.0 / 255.0, 250.0 / 255.0, 86.0 / 255.0);
vec3 lower = vec3(124.0 / 255.0, 214.0 / 255.0, 68.0 / 255.0);
vec3 sun = normalize(vec3(0.0, 5.0, -3.0));

outColor = vec4(1.0);
vec3 upDark = vec3(105.0 / 255.0, 191.0 / 255.0, 51.0 / 255.0);
vec3 lowDark= vec3(89.0 / 255.0, 168.0 / 255.0, 40.0 / 255.0);

float NoL = clamp(dot(Normal, sun), 0.3, 1.0);
vec3 mix = mix(lower, upper, UV.y);

outColor = vec4(mix * NoL, 1.0);
}
41 changes: 35 additions & 6 deletions src/shaders/grass.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,47 @@ layout(set = 0, binding = 0) uniform CameraBufferObject {
} camera;

// TODO: Declare tessellation control shader inputs and outputs
layout(location = 0) in vec4 tv1[];
layout(location = 1) in vec4 tv2[];
layout(location = 2) in vec4 tup[];
layout(location = 3) in vec4 twd[];

layout(location = 0) patch out vec4 _tv1;
layout(location = 1) patch out vec4 _tv2;
layout(location = 2) patch out vec4 _tup;
layout(location = 3) patch out vec4 _twd;


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

// TODO: Write any shader outputs
_tv1 = tv1[0];
_tv2 = tv2[0];
_tup = tup[0];
_twd = twd[0];

float near = 0.1;
float far = 25.0;

float depth = -(camera.view * gl_in[gl_InvocationID].gl_Position).z / (far - near);
depth = clamp(depth, 0.0, 1.0);

float maxLevel = 5.0;
float minLevel = 1.0;

depth = depth * depth;

float level = mix(maxLevel, minLevel, depth);
_twd.w = depth;


// 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] = 1.0;
gl_TessLevelInner[1] = level;
gl_TessLevelOuter[0] = level;
gl_TessLevelOuter[1] = 1.0;
gl_TessLevelOuter[2] = level;
gl_TessLevelOuter[3] = 1.0;
}
32 changes: 32 additions & 0 deletions src/shaders/grass.tese
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,41 @@ layout(set = 0, binding = 0) uniform CameraBufferObject {

// TODO: Declare tessellation evaluation shader inputs and outputs

layout(location = 0) patch in vec4 tv1;
layout(location = 1) patch in vec4 tv2;
layout(location = 2) patch in vec4 tup;
layout(location = 3) patch in vec4 twd;

layout(location = 0) out vec4 Position;
layout(location = 1) out vec3 Normal;
layout(location = 2) out vec3 Wind;
layout(location = 3) out vec2 UV;

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

mat4 viewProj = camera.proj * camera.view;
vec3 pos = gl_in[0].gl_Position.xyz;

vec3 t0 = pos + v * (tv1.xyz - pos);
vec3 t1 = tv1.xyz + v * (tv2.xyz - tv1.xyz);
vec3 t2 = t0 + v * (t1 - t0);

vec3 wbt = 0.5 * twd.xyz * tv2.w;

float t = 0.5 * v + u - u * v;
vec4 pos_t = vec4(t * (t2 + wbt) + (1.0 - t) * (t2 - wbt), 1.0);


Position = viewProj * pos_t;
UV = vec2(u, v);
Normal = normalize(cross(twd.xyz, normalize(t1 - t0)));
Wind = twd.xyz;

gl_Position = Position;

Position.w = twd.w;

// TODO: Use u and v to parameterize along the grass blade and output positions for each vertex of the grass blade
}
23 changes: 23 additions & 0 deletions src/shaders/grass.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,34 @@ layout(set = 1, binding = 0) uniform ModelBufferObject {
};

// TODO: Declare vertex 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 tv1;
layout(location = 1) out vec4 tv2;
layout(location = 2) out vec4 tup;
layout(location = 3) out vec4 twd;


out gl_PerVertex {
vec4 gl_Position;
};

void main() {
// TODO: Write gl_Position and any other shader outputs

vec4 _v0 = model * vec4(v0.xyz, 1.0);
vec4 _v1 = model * vec4(v1.xyz, 1.0);
vec4 _v2 = model * vec4(v2.xyz, 1.0);

tv1 = vec4(_v1.xyz, v1.w);
tv2 = vec4(_v2.xyz, v2.w);
tup = vec4(normalize(up.xyz), tup.w);

vec3 dir = normalize(cross(tup.xyz, vec3(sin(v0.w), 0, cos(v0.w))));
twd = vec4(normalize(cross(dir, tup.xyz)), v1.w * 0.4);

gl_Position = _v0;
}