Skip to content

Commit

Permalink
Modified the naming of the shaders and loading of the SPIR-V binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
tstullich committed Nov 4, 2020
1 parent a25ad40 commit 72e4b56
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 87 deletions.
6 changes: 4 additions & 2 deletions shaders/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# New shaders need to be specified here in order for CMake to pickup changes
set(SHADERS shader.frag
shader.vert)
set(SHADERS deferred.frag
deferred.vert
offscreen.vert
offscreen.frag)

add_custom_target(
CompileShaders ALL DEPENDS ${SHADERS}
Expand Down
4 changes: 2 additions & 2 deletions shaders/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ then
else
echo "Compiling shaders"
# By GLSL convention we search for the following files that have the given extensions below
glslc shader.vert -o vert.spv
glslc shader.frag -o frag.spv
glslc offscreen.vert -o offscreen-vert.spv
glslc offscreen.frag -o offscreen-frag.spv
glslc deferred.vert -o deferred-vert.spv
glslc deferred.frag -o deferred-frag.spv
fi
50 changes: 50 additions & 0 deletions shaders/deferred.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(binding = 0) uniform Camera {
mat4 model;
mat4 view;
mat4 projection;
vec3 position;
} cam;

layout(binding = 1) uniform Light {
vec3 position;
vec3 emissiveColor;
} light;

layout(binding = 2) uniform sampler2D texSampler;

layout(location = 0) in vec3 fragPosition;
layout(location = 1) in vec3 fragNormal;
layout(location = 2) in vec2 fragTexCoord;

layout(location = 0) out vec4 outColor;

vec3 diffuse(in vec3 materialDiffuse, in vec3 normal) {
vec3 L = normalize(light.position - fragPosition);
float nDotL = max(0.0, dot(normal, L));

return materialDiffuse * light.emissiveColor * nDotL;
}

vec3 specular(in vec3 materialSpecular, in vec3 normal) {
vec3 matSpec = vec3(1.0);
vec3 eyePos = normalize(cam.position);

vec3 L = normalize(light.position - fragPosition);
vec3 V = normalize(eyePos - fragPosition);
vec3 B = normalize(L + V);

float nDotB = max(0.0, dot(normal, B));

return materialSpecular * pow(nDotB, 100);
}

void main() {
vec4 matAlbedo = texture(texSampler, fragTexCoord);
vec3 diff = diffuse(matAlbedo.rgb, fragNormal);
vec3 spec = specular(matAlbedo.rgb, fragNormal);

vec3 distanceToLight = light.position - fragPosition;
float d = length(distanceToLight);
float attenuation = clamp(10.0 / d, 0.0, 1.0);

vec3 finalColor = attenuation * clamp(diff + spec, 0.0, 1.0);
outColor = vec4(finalColor, 1.0); // Ignore the alpha component for now
}
22 changes: 20 additions & 2 deletions shaders/deferred.vert
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(binding = 0) uniform Camera {
mat4 model;
mat4 view;
mat4 projection;
vec3 position;
} cam;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inTexCoord;

layout(location = 0) out vec3 fragPosition;
layout(location = 1) out vec3 fragNormal;
layout(location = 2) out vec2 fragTexCoord;

void main() {
gl_Position = vec4(vec3(0.0), 1.0);
}
gl_Position = cam.projection * cam.view * cam.model * vec4(inPosition, 1.0f);
fragPosition = inPosition;
fragNormal = (transpose(inverse(cam.model)) * vec4(inNormal, 1.0f)).xyz;
fragTexCoord = inTexCoord;
}
5 changes: 5 additions & 0 deletions shaders/offscreen.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

void main() {
}
6 changes: 6 additions & 0 deletions shaders/offscreen.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

void main() {
gl_Position = vec4(vec3(0.0), 1.0);
}
55 changes: 0 additions & 55 deletions shaders/shader.frag

This file was deleted.

24 changes: 0 additions & 24 deletions shaders/shader.vert

This file was deleted.

4 changes: 2 additions & 2 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ void blitz::Renderer::createOffscreenFramebuffer() {

void blitz::Renderer::createPipelines() {
// Load our shader modules in from disk
auto vertShaderCode = ShaderLoader::load("shaders/vert.spv");
auto fragShaderCode = ShaderLoader::load("shaders/frag.spv");
auto vertShaderCode = ShaderLoader::load("shaders/deferred-vert.spv");
auto fragShaderCode = ShaderLoader::load("shaders/deferred-frag.spv");

VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
Expand Down

0 comments on commit 72e4b56

Please sign in to comment.