Skip to content

Commit

Permalink
Introducing Render Graph (#306)
Browse files Browse the repository at this point in the history
* render graph 1st iteration

* fixed running on integrated GPU

* basic deferred shading

* ajusted config for integrated gpu

* fixed present mode

* cleanup shader

* deleted dead code

* fixed the skybox implementation
  • Loading branch information
JeanPhilippeKernel committed Feb 17, 2024
1 parent 6354a35 commit ba4cafb
Show file tree
Hide file tree
Showing 52 changed files with 2,121 additions and 1,093 deletions.
21 changes: 0 additions & 21 deletions Resources/Shaders/cubemap.vert

This file was deleted.

12 changes: 12 additions & 0 deletions Resources/Shaders/depth_prepass_scene.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "vertex_common.glsl"

void main()
{
DrawVertex v = FetchVertexData();

mat4 model = TransformBuffer.Data[gl_BaseInstance];
vec4 worldPosition = model * vec4(v.x, v.y, v.z, 1.0);
gl_Position = Camera.Projection * Camera.View * worldPosition;
}
81 changes: 2 additions & 79 deletions Resources/Shaders/final_color.frag
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 460
#extension GL_EXT_nonuniform_qualifier : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64: enable
#extension GL_GOOGLE_include_directive : require
#include "fragment_common.glsl"

layout(location = 0) in vec3 uvw;
layout(location = 1) in vec3 worldNormal;
Expand All @@ -10,83 +10,6 @@ layout(location = 4) in vec4 CameraPosition;

layout(location = 0) out vec4 outColor;

struct MaterialData
{
vec4 AmbientColor;
vec4 EmissiveColor;
vec4 AlbedoColor;
vec4 DiffuseColor;
vec4 RoughnessColor;

float TransparencyFactor;
float MetallicFactor;
float AlphaTest;

uint64_t EmissiveTextureMap;
uint64_t AlbedoTextureMap;
uint64_t NormalTextureMap;
uint64_t OpacityTextureMap;
};

layout(set = 0, binding = 5) readonly buffer MatSB { MaterialData Data[]; } MaterialDataBuffer;
layout(set = 0, binding = 9) uniform sampler2D TextureArray[];

// http://www.thetenthplanet.de/archives/1180
// modified to fix handedness of the resulting cotangent frame
mat3 cotangentFrame( vec3 N, vec3 p, vec2 uv )
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );

// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;

// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );

// calculate handedness of the resulting cotangent frame
float w = (dot(cross(N, T), B) < 0.0) ? -1.0 : 1.0;

// adjust tangent if needed
T = T * w;

return mat3( T * invmax, B * invmax, N );
}

vec3 perturbNormal(vec3 n, vec3 v, vec3 normalSample, vec2 uv)
{
vec3 map = normalize( 2.0 * normalSample - vec3(1.0) );
mat3 TBN = cotangentFrame(n, v, uv);
return normalize(TBN * map);
}

void runAlphaTest(float alpha, float alphaThreshold)
{
if (alphaThreshold > 0.0)
{
// http://alex-charlton.com/posts/Dithering_on_the_GPU/
// https://forums.khronos.org/showthread.php/5091-screen-door-transparency
mat4 thresholdMatrix = mat4(
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
);

alpha = clamp(alpha - 0.5 * thresholdMatrix[int(mod(gl_FragCoord.x, 4.0))][int(mod(gl_FragCoord.y, 4.0))], 0.0, 1.0);

if (alpha < alphaThreshold)
discard;
}
}


void main()
{
MaterialData material = MaterialDataBuffer.Data[materialIdx];
Expand Down
85 changes: 85 additions & 0 deletions Resources/Shaders/fragment_common.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#extension GL_EXT_nonuniform_qualifier : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64: enable

struct MaterialData
{
vec4 AmbientColor;
vec4 EmissiveColor;
vec4 AlbedoColor;
vec4 DiffuseColor;
vec4 RoughnessColor;

float TransparencyFactor;
float MetallicFactor;
float AlphaTest;

uint64_t EmissiveTextureMap;
uint64_t AlbedoTextureMap;
uint64_t NormalTextureMap;
uint64_t OpacityTextureMap;
};

// http://www.thetenthplanet.de/archives/1180
// modified to fix handedness of the resulting cotangent frame
mat3 cotangentFrame( vec3 N, vec3 p, vec2 uv )
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );

// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;

// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );

// calculate handedness of the resulting cotangent frame
float w = (dot(cross(N, T), B) < 0.0) ? -1.0 : 1.0;

// adjust tangent if needed
T = T * w;

return mat3( T * invmax, B * invmax, N );
}

vec3 perturbNormal(vec3 n, vec3 v, vec3 normalSample, vec2 uv)
{
vec3 map = normalize( 2.0 * normalSample - vec3(1.0) );
mat3 TBN = cotangentFrame(n, v, uv);
return normalize(TBN * map);
}

void runAlphaTest(float alpha, float alphaThreshold)
{
if (alphaThreshold > 0.0)
{
// http://alex-charlton.com/posts/Dithering_on_the_GPU/
// https://forums.khronos.org/showthread.php/5091-screen-door-transparency
mat4 thresholdMatrix = mat4(
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
);

alpha = clamp(alpha - 0.5 * thresholdMatrix[int(mod(gl_FragCoord.x, 4.0))][int(mod(gl_FragCoord.y, 4.0))], 0.0, 1.0);

if (alpha < alphaThreshold)
discard;
}
}

#define INVALID_TEXTURE_INDEX 0xFFFFFFFFu

layout(set = 0, binding = 5) readonly buffer MatSB { MaterialData Data[]; } MaterialDataBuffer;
layout(set = 0, binding = 9) uniform sampler2D TextureArray[];

MaterialData FetchMaterial(uint dataIndex)
{
return MaterialDataBuffer.Data[dataIndex];
}
28 changes: 28 additions & 0 deletions Resources/Shaders/g_buffer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "fragment_common.glsl"

layout (location = 0) in vec3 FragmentPosition;
layout (location = 1) in vec3 WorldNormal;
layout (location = 2) in vec2 TextureCoord;
layout (location = 3) in flat uint MaterialIdx;

layout (location = 0) out vec4 OutAlbedoColor;
layout (location = 1) out vec4 OutSpecular;
layout (location = 2) out vec3 OutNormal;
layout (location = 3) out vec3 OutPosition;

void main()
{
MaterialData material = FetchMaterial(MaterialIdx);

OutNormal = normalize(WorldNormal);
OutPosition = FragmentPosition;
OutSpecular = vec4(1.0);

if (material.AlbedoTextureMap < INVALID_TEXTURE_INDEX)
{
uint texId = uint(material.AlbedoTextureMap);
OutAlbedoColor = texture( TextureArray[nonuniformEXT(texId)], TextureCoord);
}
}
26 changes: 26 additions & 0 deletions Resources/Shaders/g_buffer.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "vertex_common.glsl"


layout (location = 0) out vec3 FragmentPosition;
layout (location = 1) out vec3 WorldNormal;
layout (location = 2) out vec2 TextureCoord;
layout (location = 3) out flat uint MaterialIdx;
layout (location = 4) out vec4 ViewPosition;

void main()
{
DrawVertex v = FetchVertexData();
mat4 model = FetchTransform();

vec4 worldPosition = model * vec4(v.x, v.y, v.z, 1.0);
FragmentPosition = worldPosition.xyz;

WorldNormal = transpose(inverse(mat3(model))) * vec3(v.nx, v.ny, v.nz);
TextureCoord = vec2(v.u, v.v);
MaterialIdx = DrawDataBuffer.Data[gl_BaseInstance].MaterialIndex;
ViewPosition = Camera.Position;

gl_Position = Camera.Projection * Camera.View * worldPosition;
}
5 changes: 3 additions & 2 deletions Resources/Shaders/infinite_grid.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#include "utility.glsl"

layout (location = 0) in vec2 uv;
layout (location = 1) in float scaleFactor;

layout (location = 0) out vec4 outColor;


float gridSize = 1000.0;
float gridCellSize = 0.025;

vec4 gridColorThin = vec4(1.0, 1.0, 1.0, 1.0);
Expand All @@ -31,7 +32,7 @@ void main()
float lod2a = max2(vec2(1.0) - abs(satv(mod(uv, lod2) / dudv) * 2.0 - vec2(1.0)) );

vec4 c = lod2a > 0.0 ? gridColorThick : lod1a > 0.0 ? mix(gridColorThick, gridColorThin, lodFade) : gridColorThin;
float opacityFalloff = (1.0 - satf(length(uv) / gridSize));
float opacityFalloff = (1.0 - satf(length(uv) / scaleFactor));
c.a *= lod2a > 0.0 ? lod2a : lod1a > 0.0 ? lod1a : (lod0a * (1.0-lodFade));
c.a *= opacityFalloff;
outColor = c;
Expand Down
15 changes: 6 additions & 9 deletions Resources/Shaders/infinite_grid.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
#include "vertex_common.glsl"

layout (location = 0) out vec2 uv;
layout (location = 1) out float scaleFactor;

float gridSize = 1000.0;

void main()
{
DrawData dd = DrawDataBuffer.Data[gl_BaseInstance];
scaleFactor = 300.0;

uint refIdx = dd.IndexOffset + gl_VertexIndex;
uint verIdx = IndexBuffer.Data[refIdx] + dd.VertexOffset;
DrawVertex v = VertexBuffer.Data[verIdx];

vec3 vpos = vec3(v.x, v.y, v.z) * gridSize;
gl_Position = Camera.Projection * Camera.View * vec4(vpos, 1.0);
uv = vpos.xz;
DrawVertex v = FetchVertexData();
vec3 posScale = vec3(v.x, v.y, v.z) * scaleFactor;
uv = posScale.xz;
gl_Position = Camera.Projection * Camera.View * vec4(posScale, 1.0);
}
File renamed without changes.
14 changes: 14 additions & 0 deletions Resources/Shaders/skybox.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 460
#extension GL_GOOGLE_include_directive : require
#include "vertex_common.glsl"

layout (location = 0) out vec3 dir;

void main()
{
DrawVertex v = FetchVertexData();

dir = vec3(v.x, -v.y, v.z);
vec4 position = Camera.Projection * Camera.RotScaleView * vec4(v.x, v.y, v.z, 1.0f);
gl_Position = position.xyww;
}
28 changes: 26 additions & 2 deletions Resources/Shaders/vertex_common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,32 @@ struct DrawData
uint IndexCount;
};

layout(set = 0, binding = 0) uniform UBCamera { mat4 View; mat4 Projection; vec4 Position; } Camera;
layout(set = 0, binding = 0) uniform UBCamera
{
mat4 View;
mat4 RotScaleView;
mat4 Projection;
vec4 Position;
} Camera;

layout(set = 0, binding = 1) readonly buffer VertexSB { DrawVertex Data[]; } VertexBuffer;
layout(set = 0, binding = 2) readonly buffer IndexSB { uint Data[]; } IndexBuffer;
layout(set = 0, binding = 3) readonly buffer DrawDataSB { DrawData Data[]; } DrawDataBuffer;
layout(set = 0, binding = 4) readonly buffer TransformSB { mat4 Data[]; } TransformBuffer;
layout(set = 0, binding = 4) readonly buffer TransformSB { mat4 Data[]; } TransformBuffer;


DrawVertex FetchVertexData()
{
DrawData dd = DrawDataBuffer.Data[gl_BaseInstance];

uint refIdx = dd.IndexOffset + gl_VertexIndex;
uint verIdx = IndexBuffer.Data[refIdx] + dd.VertexOffset;
DrawVertex v = VertexBuffer.Data[verIdx];

return v;
}

mat4 FetchTransform()
{
return TransformBuffer.Data[gl_BaseInstance];
}

0 comments on commit ba4cafb

Please sign in to comment.