Skip to content

Commit 338cf46

Browse files
committed
chore: work on bloom fireflies
1 parent 2ec3d6b commit 338cf46

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

editor/editor_entities.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ pl__show_entity_components(plAppData* ptAppData, plScene* ptScene, plEntity tEnt
479479
if(ImGui::InputFloat("IOR", &ptMaterialComp->fIor)) bMaterialModified = true;
480480
if(ImGui::InputFloat("Dispersion", &ptMaterialComp->fDispersion)) bMaterialModified = true;
481481
if(ImGui::InputFloat("Thickness", &ptMaterialComp->fThickness)) bMaterialModified = true;
482+
if(ImGui::SliderFloat("Transmission", &ptMaterialComp->fTransmissionFactor, 0.0f, 1.0f)) bMaterialModified = true;
482483
if(ImGui::InputFloat("Attenuation Distance", &ptMaterialComp->fAttenuationDistance)) bMaterialModified = true;
483484
if(ImGui::ColorEdit3("Attenuation Color", ptMaterialComp->tAttenuationColor.d)) bMaterialModified = true;
484485

extensions/pl_renderer_ext.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3911,9 +3911,13 @@ pl_renderer_render_view(plView* ptView, plCamera* ptCamera, plCamera* ptCullCame
39113911
gptGfx->push_compute_debug_group(ptPostEncoder, "bloom_downsample", (plVec4){0.0f, 0.32f, 0.10f, 1.0f});
39123912
gptGfx->pipeline_barrier_compute(ptPostEncoder, PL_PIPELINE_STAGE_VERTEX_SHADER | PL_PIPELINE_STAGE_COMPUTE_SHADER, PL_ACCESS_SHADER_READ, PL_PIPELINE_STAGE_COMPUTE_SHADER, PL_ACCESS_SHADER_WRITE);
39133913

3914+
plDynamicBinding tTonemapDynamicBinding = pl__allocate_dynamic_data(ptDevice);
3915+
plGpuDynBloomData* ptTonemapData = (plGpuDynBloomData*)tTonemapDynamicBinding.pcData;
3916+
ptTonemapData->iMipLevel = i;
3917+
39143918
plComputeShaderHandle tTonemapShader = gptShaderVariant->get_compute_shader("bloom_downsample", NULL);
39153919
gptGfx->bind_compute_shader(ptPostEncoder, tTonemapShader);
3916-
gptGfx->bind_compute_bind_groups(ptPostEncoder, tTonemapShader, 0, 1, &tTonemapBG, 0, NULL);
3920+
gptGfx->bind_compute_bind_groups(ptPostEncoder, tTonemapShader, 0, 1, &tTonemapBG, 1, &tTonemapDynamicBinding);
39173921

39183922
plTexture* ptTargetTexture = gptGfx->get_texture(ptDevice, tTonemapTextureData[0].tTexture);
39193923

shaders/bloom_downsample.comp

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
#version 460
22
#extension GL_ARB_separate_shader_objects : enable
33

4+
#include "pl_shader_interop_renderer.h"
45

56
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
67

78
layout(set = 0, binding = 0, rgba16f) uniform image2D target;
89
layout(set = 0, binding = 1) uniform texture2D source;
910
layout(set = 0, binding = 2) uniform sampler tSamplerLinearClamp;
1011

12+
layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
13+
{
14+
plGpuDynBloomData tData;
15+
} tDynamicData;
16+
17+
vec3 PowVec3(vec3 v, float p)
18+
{
19+
return vec3(pow(v.x, p), pow(v.y, p), pow(v.z, p));
20+
}
21+
22+
const float invGamma = 1.0 / 2.2;
23+
vec3 ToSRGB(vec3 v) { return PowVec3(v, invGamma); }
24+
25+
float RGBToLuminance(vec3 col)
26+
{
27+
return dot(col, vec3(0.2126f, 0.7152f, 0.0722f));
28+
}
29+
30+
float KarisAverage(vec3 col)
31+
{
32+
// Formula is 1 / (1 + luma)
33+
float luma = RGBToLuminance(ToSRGB(col)) * 0.25f;
34+
return 1.0f / (1.0f + luma);
35+
}
36+
1137
void main()
1238
{
1339
ivec2 iUV = ivec2(gl_GlobalInvocationID.xy);
1440
ivec2 targetResolution = imageSize(target);
1541

16-
1742
if(iUV.x >= targetResolution.x || iUV.y >= targetResolution.y)
1843
return;
1944

@@ -24,27 +49,56 @@ void main()
2449

2550
vec3 color = vec3(0);
2651

52+
53+
2754
//sampling pattern from "Next Generation Post Processing in Call of Duty Advanced Warfare"
2855
//center
29-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv, 0).rgb * 0.125;
56+
vec3 e = textureLod(sampler2D(source, tSamplerLinearClamp), uv, 0).rgb;
3057

3158
//center pixel corners
32-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0.5, 0.5), 0).rgb * 0.125;
33-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0.5, -0.5), 0).rgb * 0.125;
34-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-0.5, 0.5), 0).rgb * 0.125;
35-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-0.5, -0.5), 0).rgb * 0.125;
59+
vec3 j = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0.5, 0.5), 0).rgb;
60+
vec3 k = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0.5, -0.5), 0).rgb;
61+
vec3 l = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-0.5, 0.5), 0).rgb;
62+
vec3 m = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-0.5, -0.5), 0).rgb;
3663

3764
//left, rigth, up and down
38-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, 0), 0).rgb * 0.0625;
39-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, 0), 0).rgb * 0.0625;
40-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0, 1.5), 0).rgb * 0.0625;
41-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0, -1.5), 0).rgb * 0.0625;
65+
vec3 b = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, 0), 0).rgb;
66+
vec3 d = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, 0), 0).rgb;
67+
vec3 f = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0, 1.5), 0).rgb;
68+
vec3 h = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(0, -1.5), 0).rgb;
4269

4370
//diagonal corners
44-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, 1.5), 0).rgb * 0.03125;
45-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, -1.5), 0).rgb * 0.03125;
46-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, 1.5), 0).rgb * 0.03125;
47-
color += textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, -1.5), 0).rgb * 0.03125;
71+
vec3 a = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, 1.5), 0).rgb;
72+
vec3 c = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2( 1.5, -1.5), 0).rgb;
73+
vec3 g = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, 1.5), 0).rgb;
74+
vec3 i = textureLod(sampler2D(source, tSamplerLinearClamp), uv + texelSize * vec2(-1.5, -1.5), 0).rgb;
75+
76+
// vec3 groups[5];
77+
// switch (tDynamicData.tData.iMipLevel)
78+
// {
79+
// case 0:
80+
// // We are writing to mip 0, so we need to apply Karis average to each block
81+
// // of 4 samples to prevent fireflies (very bright subpixels, leads to pulsating
82+
// // artifacts).
83+
// groups[0] = (a+b+d+e) * (0.125f/4.0f);
84+
// groups[1] = (b+c+e+f) * (0.125f/4.0f);
85+
// groups[2] = (d+e+g+h) * (0.125f/4.0f);
86+
// groups[3] = (e+f+h+i) * (0.125f/4.0f);
87+
// groups[4] = (j+k+l+m) * (0.5f/4.0f);
88+
// groups[0] *= KarisAverage(groups[0]);
89+
// groups[1] *= KarisAverage(groups[1]);
90+
// groups[2] *= KarisAverage(groups[2]);
91+
// groups[3] *= KarisAverage(groups[3]);
92+
// groups[4] *= KarisAverage(groups[4]);
93+
// color = groups[0]+groups[1]+groups[2]+groups[3]+groups[4];
94+
// break;
95+
// default:
96+
color = e*0.125;
97+
color += (a+c+g+i)*0.03125;
98+
color += (b+d+f+h)*0.0625;
99+
color += (j+k+l+m)*0.125;
100+
// break;
101+
// }
48102

49103
color = clamp(color, 0.0, 100.0);
50104
imageStore(target, iUV, vec4(color, 0));

shaders/pl_shader_interop_renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ PL_BEGIN_STRUCT(plGpuDynBloomData)
298298
float blurRadius;
299299
float bloomStrength;
300300
int isLowestMip;
301+
int iMipLevel;
301302
PL_END_STRUCT(plGpuDynBloomData)
302303

303304
PL_BEGIN_STRUCT(plGpuDynTonemap)

0 commit comments

Comments
 (0)