Skip to content

Commit

Permalink
Some work on deferred.
Browse files Browse the repository at this point in the history
Deferred shader/render targets.
  • Loading branch information
Williscool13 committed Sep 29, 2024
1 parent 46d02a1 commit 443b914
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 22 deletions.
92 changes: 92 additions & 0 deletions shaders/deferred.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_nonuniform_qualifier : enable

// world space
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec4 inColor;
layout (location = 3) in vec2 inUV;
layout (location = 4) in flat uint inMaterialIndex;

layout (location = 0) out vec4 normalTarget; // 8,8,8,8 normal 2 unused
layout (location = 1) out vec4 albedoTarget; // 8,8,8,8 albedo 2 unused
layout (location = 2) out vec4 pbrTarget; // 8 metallic, 8 roughness, 8 emissive, 8 unused


struct Model
{
mat4 modelMatrix;
};

struct Material
{
vec4 colorFactor;
vec4 metalRoughFactors;
vec4 textureImageIndices;
vec4 textureSamplerIndices;
vec4 alphaCutoff;
};

layout (buffer_reference, std430) readonly buffer ModelData
{
Model models[];
};

layout (buffer_reference, std430) readonly buffer MaterialData
{
Material materials[];
};

layout (set = 0, binding = 0) uniform addresses
{
MaterialData materialBufferDeviceAddress;
ModelData modelBufferDeviceAddress;
} bufferAddresses;

layout (set = 1, binding = 0) uniform sampler samplers[];
layout (set = 1, binding = 1) uniform texture2D textures[];

/**layout(set = 2, binding = 0) uniform sceneUniforms
{
} sceneData;*/

layout(set = 3, binding = 0) uniform samplerCube environmentDiffuseAndSpecular;
layout(set = 3, binding = 1) uniform sampler2D lut;

layout (push_constant) uniform PushConstants {
mat4 viewProj; // (64)
vec4 cameraPos; // (16) - w is for alignment
// (16)
// (16)
// (16)
} pushConstants;

void main() {
Material m = bufferAddresses.materialBufferDeviceAddress.materials[inMaterialIndex];
vec3 albedo = vec3(1.0f);

int colorSamplerIndex = int(m.textureSamplerIndices.x);
int colorImageIndex = int(m.textureImageIndices.x);
if (colorSamplerIndex >= 0){
albedo = texture(sampler2D(textures[nonuniformEXT(colorImageIndex)], samplers[nonuniformEXT(colorSamplerIndex)]), inUV).xyz;
}
albedo = albedo.xyz * inColor.rgb * m.colorFactor.rgb;

int metalSamplerIndex = int(m.textureSamplerIndices.y);
int metalImageIndex = int(m.textureImageIndices.y);

float metallic = m.metalRoughFactors.x;
float roughness = m.metalRoughFactors.y;
if (metalSamplerIndex >= 0){
vec4 metalRoughSample = texture(sampler2D(textures[nonuniformEXT(metalImageIndex)], samplers[nonuniformEXT(metalSamplerIndex)]), inUV);
metallic *= metalRoughSample.b;
roughness *= metalRoughSample.g;
}

// check cutoff if applicable

normalTarget = vec4(normalize(inNormal), 0.0f);
albedoTarget = vec4(albedo, 0.0f);
pbrTarget = vec4(metallic, roughness, 0.0f, 0.0f);
}
67 changes: 67 additions & 0 deletions shaders/deferred.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#version 450
#extension GL_EXT_buffer_reference: require

struct Model
{
mat4 modelMatrix;
};

struct Material
{
vec4 colorFactor;
vec4 metalRoughFactors;
ivec4 textureImageIndices;
ivec4 textureSamplerIndices;
vec4 alphaCutoff;
};

layout (buffer_reference, std430) readonly buffer ModelData
{
Model models[];
};

layout (buffer_reference, std430) readonly buffer MaterialData
{
Material materials[];
};

layout (set = 0, binding = 0) uniform addresses
{
MaterialData materialBufferDeviceAddress;
ModelData modelBufferDeviceAddress;
} bufferAddresses;

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec4 color;
layout (location = 3) in vec2 uv;
layout (location = 4) in uint materialIndex;

layout (location = 0) out vec3 outPosition;
layout (location = 1) out vec3 outNormal;
layout (location = 2) out vec4 outColor;
layout (location = 3) out vec2 outUV;
layout (location = 4) out flat uint outMaterialIndex;

layout (push_constant) uniform PushConstants {
mat4 viewProj; // (64)
vec4 cameraPos; // (16) - w is for alignment
// (16)
// (16)
// (16)
} pushConstants;

void main() {
mat4 model = bufferAddresses.modelBufferDeviceAddress.models[gl_InstanceIndex].modelMatrix;

vec4 mPos = model * vec4(position, 1.0); // why is this red underlined in CLion?!?!?!?!

// use world matrix when it becomes available
outPosition = mPos.xyz;
outNormal = inverse(transpose(mat3(model))) * normal;
outColor = color;
outUV = uv;
outMaterialIndex = materialIndex;

gl_Position = pushConstants.viewProj * mPos;
}
25 changes: 25 additions & 0 deletions shaders/include/environment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const float MAX_REFLECTION_LOD = 4.0;
const uint DIFF_IRRA_MIP_LEVEL = 5;
const bool FLIP_ENVIRONMENT_MAP_Y = true;

// Hard-coded to 3. Be careful.
layout(set = 3, binding = 0) uniform samplerCube environmentDiffuseAndSpecular;
layout(set = 3, binding = 1) uniform sampler2D lut;

vec3 DiffuseIrradiance(vec3 N)
{
vec3 ENV_N = N;
if (FLIP_ENVIRONMENT_MAP_Y) { ENV_N.y = -ENV_N.y; }
return textureLod(environmentDiffuseAndSpecular, ENV_N, DIFF_IRRA_MIP_LEVEL).rgb;
}

vec3 SpecularReflection(vec3 V, vec3 N, float roughness, vec3 F) {
vec3 R = reflect(-V, N);
if (FLIP_ENVIRONMENT_MAP_Y) { R.y = -R.y; }
// dont need to skip mip 5 because never goes above 4
vec3 prefilteredColor = textureLod(environmentDiffuseAndSpecular, R, roughness * MAX_REFLECTION_LOD).rgb;

vec2 envBRDF = texture(lut, vec2(max(dot(N, V), 0.0f), roughness)).rg;

return prefilteredColor * (F * envBRDF.x + envBRDF.y);
}
2 changes: 1 addition & 1 deletion shaders/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ layout (buffer_reference, std430) readonly buffer MaterialData
Material materials[];
};

layout (set = 0, binding = 0) uniform addresses
layout (set = 0, binding = 0) uniform Addresses
{
MaterialData materialBufferDeviceAddress;
ModelData modelBufferDeviceAddress;
Expand Down
22 changes: 11 additions & 11 deletions shaders/pbr.vert
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_buffer_reference: require

struct Model
{
Expand All @@ -15,20 +15,20 @@ struct Material
vec4 alphaCutoff;
};

layout(buffer_reference, std430) readonly buffer ModelData
layout (buffer_reference, std430) readonly buffer ModelData
{
Model models[];
};

layout(buffer_reference, std430) readonly buffer MaterialData
layout (buffer_reference, std430) readonly buffer MaterialData
{
Material materials[];
};

layout(set = 0, binding = 0) uniform addresses
layout (set = 0, binding = 0) uniform addresses
{
MaterialData materialBufferDeviceAddress;
ModelData modelBufferDeviceAddress;
MaterialData materialBufferDeviceAddress;
ModelData modelBufferDeviceAddress;
} bufferAddresses;

layout (location = 0) in vec3 position;
Expand All @@ -47,20 +47,20 @@ layout (location = 4) out flat uint outMaterialIndex;
layout (push_constant) uniform PushConstants {
mat4 viewProj; // (64)
vec4 cameraPos; // (16) - w is for alignment
// (16)
// (16)
// (16)
// (16)
// (16)
// (16)
} pushConstants;

void main() {

mat4 model = bufferAddresses.modelBufferDeviceAddress.models[gl_InstanceIndex].modelMatrix;

vec4 mPos = model * vec4(position, 1.0); // why is this red underlined in CLion?!?!?!?!
vec4 mPos = model * vec4(position, 1.0); // why is this red underlined in CLion?!?!?!?!

// use world matrix when it becomes available
outPosition = mPos.xyz;
outNormal = inverse(transpose(mat3(model))) * normal;
outNormal = inverse(transpose(mat3(model))) * normal;
outColor = color;
outUV = uv;
outMaterialIndex = materialIndex;
Expand Down
114 changes: 114 additions & 0 deletions shaders/shading.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#version 460

#include "pbr.glsl"
#include "environment.glsl"

layout (local_size_x = 16, local_size_y = 16) in;

layout (rgba8_snorm, set = 0, binding = 0) uniform image2D normalRenderTarget;
layout (rgba8, set = 0, binding = 1) uniform image2D albedoRenderTarget;
layout (rgba8, set = 0, binding = 2) uniform image2D pbrRenderTarget;
layout (r32f, set = 0, binding = 3) uniform image2D depthBuffer;
layout (rgba32f, set = 0, binding = 4) uniform image2D outputImage;

layout (std140, set = 1, binding = 0) uniform CameraData {
mat4 invProjection;
mat4 invView;
vec3 cameraPos;
float pad;
} cameraData;


struct DirectionalLight {
vec3 direction;
float intensity;
vec3 color;
float pad;
};

struct PointLight {
vec4 position;
vec3 color;
float radius;
};

layout (std140, set = 2, binding = 1) uniform LightData {
DirectionalLight mainLight;
PointLight pointLights[6];
} lightData;

vec3 reconstructPosition(ivec2 texCoord, float depth) {
// Get normalized device coordinates
vec2 texSize = vec2(imageSize(depthBuffer));
vec2 ndc = (vec2(texCoord) + 0.5) / texSize * 2.0 - 1.0;

// Reconstruct view-space position
vec4 positionVS = cameraData.invProjection * vec4(ndc, depth, 1.0);
positionVS /= positionVS.w;

// Transform to world-space
vec4 positionWS = cameraData.invView * positionVS;

return positionWS.xyz;
}

void main() {
ivec2 texCoord = ivec2(gl_GlobalInvocationID.xy);

float depth = imageLoad(depthBuffer, texCoord).r;
vec3 position = reconstructPosition(texCoord, depth);
vec3 normal = imageLoad(normalRenderTarget, texCoord).rgb;
vec4 albedo = imageLoad(albedoRenderTarget, texCoord);

vec4 pbrData = imageLoad(pbrRenderTarget, texCoord);
float roughness = pbrData.g;
float metallic = pbrData.r;
float emissive = pbrData.b;


vec3 N = imageLoad(normalRenderTarget, texCoord).xyz;
vec3 V = normalize(cameraData.cameraPos.xyz - position);

vec3 L = normalize(lightData.mainLight.direction); // for point lights, light.pos - inPosition
vec3 H = normalize(V + L);

// SPECULAR
float NDF = D_GGX(N, H, roughness);
float G = G_SCHLICKGGX_SMITH(N, V, L, roughness);
vec3 F0 = mix(vec3(0.04), albedo.xyz, metallic);
vec3 F = F_SCHLICK(V, H, F0);

vec3 numerator = NDF * G * F;
float denominator = 4.0f * max(dot(N, V), 0.0f) * max(dot(N, L), 0.0f);
vec3 specular = numerator / max(denominator, 0.001f);

vec3 kS = F;
vec3 kD = vec3(1.0f) - kS;
kD *= 1.0f - metallic;

// DIFFUSE
float nDotL = max(dot(N, L), 0.0f);
vec3 diffuse = Lambert(kD, albedo.xyz);

// REFLECTIONS
vec3 irradiance = DiffuseIrradiance(N);
vec3 reflection_diffuse = irradiance * albedo.xyz;

vec3 reflection_specular = SpecularReflection(V, N, roughness, F);
vec3 ambient = (kD * reflection_diffuse + reflection_specular);

vec3 finalColor = (diffuse + specular) * lightData.mainLight.color * nDotL;
finalColor += ambient;

/**vec3 corrected_ambient = ambient / (ambient + vec3(1.0f)); // Reinhard
corrected_ambient = pow(corrected_ambient, vec3(1.0f / 2.2f)); // gamma correction
finalColor += corrected_ambient;

FragColor = vec4(finalColor, albedo.w);*/

vec3 correctedFinalColor = finalColor / (finalColor + vec3(1.0f)); // Reinhard
correctedFinalColor = pow(correctedFinalColor, vec3(1.0f / 2.2f)); // gamma correction

imageStore(outputImage, texCoord, vec4(correctedFinalColor, albedo.w));
//FragColor = vec4(correctedFinalColor, albedo.w);
}
Loading

0 comments on commit 443b914

Please sign in to comment.