Skip to content

Commit

Permalink
Started adding color post processing, changes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
voldien committed Feb 7, 2025
1 parent af6c987 commit 9c91572
Show file tree
Hide file tree
Showing 45 changed files with 742 additions and 275 deletions.
7 changes: 5 additions & 2 deletions Shaders/common/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ struct tessellation_settings {
struct light_settings {
DirectionalLight directional[16];
PointLight point[64];
uint directionalCount;
uint pointCount;
};

/* */
layout(set = 1, binding = 1, std140) uniform UniformCommonBufferBlock { common_data constant; }
constantCommon;

/* */
layout(set = 1, binding = 2, std140) uniform UniformNodeBufferBlock { Node node[512]; }
layout(set = 1, binding = 2, std140) uniform UniformNodeBufferBlock { Node node[1024]; }
NodeUBO;

/* */
layout(set = 1, binding = 3, std140) uniform UniformSkeletonBufferBlock { mat4 gBones[512]; }
layout(set = 1, binding = 3, std140) uniform UniformSkeletonBufferBlock { mat4 gBones[1024]; }
skeletonUBO;

/* */
Expand Down Expand Up @@ -72,5 +74,6 @@ layout(binding = 11) uniform samplerCube prefilterMap;
layout(binding = 12) uniform sampler2D brdfLUT;

material getMaterial() { return MaterialUBO.materials[0]; }
mat4 getModel() { return NodeUBO.node[0].model; }

#endif
101 changes: 63 additions & 38 deletions Shaders/compute/downsample2x2.comp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#extension GL_ARB_shading_language_include : enable
#extension GL_GOOGLE_include_directive : enable

layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

layout(set = 0, binding = 0, rgba16f) uniform readonly image2D SourceTexture;
layout(set = 0, binding = 0) uniform sampler2D SourceTexture;
layout(set = 0, binding = 1, rgba16f) uniform writeonly image2D TargetTexture;

layout(push_constant) uniform Settings { layout(offset = 0) int filterRadius; }
Expand All @@ -14,53 +14,78 @@ settings;

void main() {

const ivec2 sourceSize = imageSize(SourceTexture);
/* */
const vec2 sourceSize = textureSize(SourceTexture, 0);
const ivec2 targetSize = imageSize(TargetTexture);

const vec2 ratioSize = ceil(vec2(gl_NumWorkGroups.xy * gl_WorkGroupSize.xy) / vec2(sourceSize));
/* */
const uvec2 workItems = gl_NumWorkGroups.xy * gl_WorkGroupSize.xy;
const vec2 ratioSize = ceil(vec2(workItems) / vec2(sourceSize));

/* */
const ivec2 targetDownSampleSize = targetSize * int(ratioSize.x);
const ivec2 sourceDownSampleSize = targetDownSampleSize * 2;

/* */
if (any(greaterThan(gl_GlobalInvocationID.xy, targetDownSampleSize))) {
if (any(greaterThanEqual(gl_GlobalInvocationID.xy, targetDownSampleSize))) {
return;
}

/* */
const uvec2 downSampleCoordinate = gl_GlobalInvocationID.xy;
const uvec2 SourceImageCoordinate = downSampleCoordinate * 2;

const uint x = settings.filterRadius;
const uint y = settings.filterRadius;

const vec3 a = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y + 2 * y) %
sourceDownSampleSize)
.rgb;
const vec3 b = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x, SourceImageCoordinate.y + 2 * y)).rgb;
const vec3 c =
imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y + 2 * y)).rgb;

const vec3 d = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y)).rgb;
const vec3 e = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x, SourceImageCoordinate.y)).rgb;
const vec3 f = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y)).rgb;

const vec3 g =
imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y - 2 * y)).rgb;
const vec3 h = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x, SourceImageCoordinate.y - 2 * y)).rgb;
const vec3 i =
imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y - 2 * y)).rgb;

const vec3 j = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x - x, SourceImageCoordinate.y + y)).rgb;
const vec3 k = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x + x, SourceImageCoordinate.y + y)).rgb;
const vec3 l = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x - x, SourceImageCoordinate.y - y)).rgb;
const vec3 m = imageLoad(SourceTexture, ivec2(SourceImageCoordinate.x + x, SourceImageCoordinate.y - y)).rgb;

vec3 downsample = e * 0.125;
downsample += (a + c + g + i) * 0.03125;
downsample += (b + d + f + h) * 0.0625;
downsample += (j + k + l + m) * 0.125;
const vec2 source_texel_size = (1.0 / sourceSize);
const vec2 maxUVSize = source_texel_size * sourceDownSampleSize;

/* */
imageStore(TargetTexture, ivec2(downSampleCoordinate), vec4(downsample, 1));
const uvec2 downSampleCoordinate = gl_GlobalInvocationID.xy;
const vec2 SourceImageCoordinate = source_texel_size * ratioSize * (downSampleCoordinate + 0.5) * 2;

/* Filter offset. */
const float x = settings.filterRadius * source_texel_size.x;
const float y = settings.filterRadius * source_texel_size.y;

{
/* */
const vec3 a = texture(SourceTexture,
min(vec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y + 2 * y), maxUVSize))
.rgb;
const vec3 b =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x, SourceImageCoordinate.y + 2 * y), maxUVSize)).rgb;
const vec3 c = texture(SourceTexture,
min(vec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y + 2 * y), maxUVSize))
.rgb;

const vec3 d =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y), maxUVSize)).rgb;
const vec3 e =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x, SourceImageCoordinate.y), maxUVSize)).rgb;
const vec3 f =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y), maxUVSize)).rgb;

const vec3 g = texture(SourceTexture,
min(vec2(SourceImageCoordinate.x - 2 * x, SourceImageCoordinate.y - 2 * y), maxUVSize))
.rgb;
const vec3 h =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x, SourceImageCoordinate.y - 2 * y), maxUVSize)).rgb;
const vec3 i = texture(SourceTexture,
min(vec2(SourceImageCoordinate.x + 2 * x, SourceImageCoordinate.y - 2 * y), maxUVSize))
.rgb;

const vec3 j =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x - x, SourceImageCoordinate.y + y), maxUVSize)).rgb;
const vec3 k =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x + x, SourceImageCoordinate.y + y), maxUVSize)).rgb;
const vec3 l =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x - x, SourceImageCoordinate.y - y), maxUVSize)).rgb;
const vec3 m =
texture(SourceTexture, min(vec2(SourceImageCoordinate.x + x, SourceImageCoordinate.y - y), maxUVSize)).rgb;

/* */
vec3 downsample = e * 0.125;
downsample += (a + c + g + i) * 0.03125;
downsample += (b + d + f + h) * 0.0625;
downsample += (j + k + l + m) * 0.125;

/* */
imageStore(TargetTexture, ivec2(downSampleCoordinate), vec4(downsample, 1));
}
}
61 changes: 34 additions & 27 deletions Shaders/compute/upsample2x2.comp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#extension GL_ARB_shading_language_include : enable
#extension GL_GOOGLE_include_directive : enable

layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform sampler2D SourceTexture;
layout(set = 0, binding = 1, rgba16f) uniform restrict writeonly image2D TargetTexture;
Expand All @@ -18,14 +18,19 @@ settings;

void main() {

/* */
const ivec2 sourceSize = textureSize(SourceTexture, 0);
const ivec2 targetSize = imageSize(TargetTexture);

const vec2 ratioSize = ceil(vec2(gl_NumWorkGroups.xy * gl_WorkGroupSize.xy) / vec2(sourceSize));
/* */
const uvec2 workItems = gl_NumWorkGroups.xy * gl_WorkGroupSize.xy;
const vec2 ratioSize = (vec2(sourceSize) / vec2(workItems));

const ivec2 sourceDownSampleSize = targetSize / int(ratioSize.x);
const ivec2 targetDownSampleSize = sourceDownSampleSize * 2;
/* */
const vec2 sourceDownSampleSize = (sourceSize / ratioSize.x) * 0.5;
const ivec2 targetDownSampleSize = ivec2(sourceDownSampleSize) * 2;

/* */
if (any(greaterThan(gl_GlobalInvocationID.xy, targetDownSampleSize))) {
return;
}
Expand All @@ -34,36 +39,38 @@ void main() {
const uvec2 upsampleCoordinate = gl_GlobalInvocationID.xy;

/* */
const vec2 sourceSizeInverse = 1.0 / textureSize(SourceTexture, 0);
const vec2 maxUVSize = sourceSizeInverse * sourceDownSampleSize;
const vec2 source_texel_size = 1.0 / textureSize(SourceTexture, 0);
const vec2 maxUVSize = source_texel_size * sourceDownSampleSize;

/* */
const float x = sourceSizeInverse.x * settings.filterRadius * 0.5;
const float y = sourceSizeInverse.y * settings.filterRadius * 0.5;
const vec2 texCoord = upsampleCoordinate * sourceSizeInverse * 0.5;
const float x = source_texel_size.x * settings.filterRadius;
const float y = source_texel_size.y * settings.filterRadius;
const vec2 texCoord = (upsampleCoordinate + 0.5) * source_texel_size * 0.5;

vec3 a = texture(SourceTexture, vec2(texCoord.x - 2 * x, texCoord.y + 2 * y)).rgb;
vec3 b = texture(SourceTexture, vec2(texCoord.x, texCoord.y + 2 * y)).rgb;
vec3 c = texture(SourceTexture, vec2(texCoord.x + 2 * x, texCoord.y + 2 * y)).rgb;
{
const vec3 a = texture(SourceTexture, min(vec2(texCoord.x - 2 * x, texCoord.y + 2 * y), maxUVSize)).rgb;
const vec3 b = texture(SourceTexture, min(vec2(texCoord.x, texCoord.y + 2 * y), maxUVSize)).rgb;
const vec3 c = texture(SourceTexture, min(vec2(texCoord.x + 2 * x, texCoord.y + 2 * y), maxUVSize)).rgb;

vec3 d = texture(SourceTexture, vec2(texCoord.x - 2 * x, texCoord.y)).rgb;
vec3 e = texture(SourceTexture, vec2(texCoord.x, texCoord.y)).rgb;
vec3 f = texture(SourceTexture, vec2(texCoord.x + 2 * x, texCoord.y)).rgb;
const vec3 d = texture(SourceTexture, min(vec2(texCoord.x - 2 * x, texCoord.y), maxUVSize)).rgb;
const vec3 e = texture(SourceTexture, min(vec2(texCoord.x, texCoord.y), maxUVSize)).rgb;
const vec3 f = texture(SourceTexture, min(vec2(texCoord.x + 2 * x, texCoord.y), maxUVSize)).rgb;

vec3 g = texture(SourceTexture, vec2(texCoord.x - 2 * x, texCoord.y - 2 * y)).rgb;
vec3 h = texture(SourceTexture, vec2(texCoord.x, texCoord.y - 2 * y)).rgb;
vec3 i = texture(SourceTexture, vec2(texCoord.x + 2 * x, texCoord.y - 2 * y)).rgb;
const vec3 g = texture(SourceTexture, min(vec2(texCoord.x - 2 * x, texCoord.y - 2 * y), maxUVSize)).rgb;
const vec3 h = texture(SourceTexture, min(vec2(texCoord.x, texCoord.y - 2 * y), maxUVSize)).rgb;
const vec3 i = texture(SourceTexture, min(vec2(texCoord.x + 2 * x, texCoord.y - 2 * y), maxUVSize)).rgb;

vec3 j = texture(SourceTexture, vec2(texCoord.x - x, texCoord.y + y)).rgb;
vec3 k = texture(SourceTexture, vec2(texCoord.x + x, texCoord.y + y)).rgb;
vec3 l = texture(SourceTexture, vec2(texCoord.x - x, texCoord.y - y)).rgb;
vec3 m = texture(SourceTexture, vec2(texCoord.x + x, texCoord.y - y)).rgb;
const vec3 j = texture(SourceTexture, min(vec2(texCoord.x - x, texCoord.y + y), maxUVSize)).rgb;
const vec3 k = texture(SourceTexture, min(vec2(texCoord.x + x, texCoord.y + y), maxUVSize)).rgb;
const vec3 l = texture(SourceTexture, min(vec2(texCoord.x - x, texCoord.y - y), maxUVSize)).rgb;
const vec3 m = texture(SourceTexture, min(vec2(texCoord.x + x, texCoord.y - y), maxUVSize)).rgb;

vec3 upsample = e * 4.0;
upsample += (b + d + f + h) * 2.0;
upsample += (a + c + g + i);
vec3 upsample = e * 4.0;
upsample += (b + d + f + h) * 2.0;
upsample += (a + c + g + i);

upsample *= 1.0 / 16.0;
upsample *= 1.0 / 16.0;

imageStore(TargetTexture, ivec2(upsampleCoordinate), vec4(upsample, 1));
imageStore(TargetTexture, ivec2(upsampleCoordinate), vec4(upsample, 1));
}
}
57 changes: 20 additions & 37 deletions Shaders/postprocessingeffects/box_blur.comp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ precision mediump int;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform sampler2D ColorTexture;
layout(set = 0, binding = 1, rgba16f) uniform coherent image2D TargetTexture;
layout(set = 0, binding = 1, rgba16f) uniform restrict image2D TargetTexture;

layout(push_constant) uniform Settings {
layout(offset = 0) float variance;
layout(offset = 4) float mean;
layout(offset = 8) float radius;
layout(offset = 12) int samples;
layout(offset = 0) float blend;
layout(offset = 4) float variance;
layout(offset = 8) float mean;
layout(offset = 12) float radius;
layout(offset = 16) int samples;
}
settings;

#include "postprocessing_base.glsl"
layout(constant_id = 16) const int MAX_SAMPLES = 7 + 7 + 1;

const uint work_group_nr_samples = (16 + (MAX_SAMPLES - 1) / 2) * (16 + (MAX_SAMPLES - 1) / 2);
shared vec4 localResult[work_group_nr_samples];

void main() {

/* */
Expand All @@ -39,40 +37,25 @@ void main() {
/* */
const vec2 resolution = textureSize(ColorTexture, 0);
const vec2 TexelInvOffset = 1.0 / resolution;
const ivec2 TexCoord = ivec2(gl_GlobalInvocationID.xy);
const vec2 NormalizeCoordinate = TexCoord / resolution;

/* */
const int samples = settings.samples;
const int samples = clamp(settings.samples, 1, MAX_SAMPLES / 2);
const float radius = settings.radius;
const float totalInverse = 1.0f / samples;

/* Cache data to shared. . */
{

const int start = clamp(-((samples - 1) / 2), -MAX_SAMPLES, -1);
const int end = clamp(((samples - 1) / 2), 1, MAX_SAMPLES);
for (int x = start; x <= end; x++) {
for (int y = start; y <= end; y++) {
const vec2 coordinate = vec2(x, y);
}
const float totalInverse = 1.0f / (samples * samples);

vec4 color = vec4(0);
for (uint x = 0; x < samples; x++) {
for (uint y = 0; y < samples; y++) {
/* */
const vec2 uvP =
NormalizeCoordinate + TexelInvOffset * vec2(x + -samples * 0.5, y + -samples * 0.5) * radius;
color += texture(ColorTexture, uvP);
}
}
memoryBarrierShared();

const ivec2 TexCoord = ivec2(gl_GlobalInvocationID.xy);

const int start = clamp(-((samples - 1) / 2), -MAX_SAMPLES, -1);
const int end = clamp(((samples - 1) / 2), 1, MAX_SAMPLES);

vec4 color1 = vec4(0.0);
float total = EPSILON;

for (int x = start; x <= end; x++) {
const ivec2 uv = TexCoord + ivec2(vec2(x, 0) * radius);

// color1 += imageLoad(ColorTexture, uv).rgba, 1.0);
}

// Normalize color.
const vec4 finalColor = color1 * totalInverse;
/* Normalize color. */
const vec4 finalColor = color * totalInverse;
imageStore(TargetTexture, TexCoord, finalColor);
}
7 changes: 4 additions & 3 deletions Shaders/postprocessingeffects/chromaticabbrevation.frag
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ layout(location = 0) in vec2 screenUV;
layout(set = 0, binding = 0) uniform sampler2D ColorTexture;

layout(push_constant) uniform Settings {
layout(offset = 0) float redOffset;
layout(offset = 4) float greenOffset;
layout(offset = 8) float blueOffset;
layout(offset = 0) float blend;
layout(offset = 4) float redOffset;
layout(offset = 8) float greenOffset;
layout(offset = 12) float blueOffset;
layout(offset = 16) vec2 direction_center;
}
settings;
Expand Down
36 changes: 36 additions & 0 deletions Shaders/postprocessingeffects/color/color_balance.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 460 core
#extension GL_ARB_derivative_control : enable
#extension GL_ARB_enhanced_layouts : enable
#extension GL_ARB_shader_image_load_store : enable
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_shading_language_include : enable
#extension GL_GOOGLE_include_directive : enable

precision mediump float;
precision mediump int;

layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

layout(set = 0, binding = 1, rgba16f) uniform image2D ColorTexture;

layout(push_constant) uniform Settings {
layout(offset = 0) float blend;
layout(offset = 4) float exposure;
layout(offset = 8) float gamma;
}
settings;

#include "common.glsl"

void main() {

/* */
if (any(greaterThan(gl_GlobalInvocationID.xy, imageSize(ColorTexture)))) {
return;
}

const ivec2 TexCoord = ivec2(gl_GlobalInvocationID.xy);

vec4 fragColor = imageLoad(ColorTexture, TexCoord);
imageStore(ColorTexture, TexCoord, vec4(fragColor.rgb, 1.0));
}
Loading

0 comments on commit 9c91572

Please sign in to comment.