Skip to content

Commit e123860

Browse files
committed
bloom
1 parent 32bc650 commit e123860

File tree

9 files changed

+239
-28
lines changed

9 files changed

+239
-28
lines changed

data/cage/shaders/effects/bloomApply.glsl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ $define shader fragment
55

66
layout(std140, set = 2, binding = 0) uniform Params
77
{
8-
ivec4 uniLodLevel; // x
8+
vec4 uniBloomParams; // threshold, blur passes
99
};
1010

1111
layout(set = 2, binding = 1) uniform sampler2D texColor;
12-
layout(set = 2, binding = 2) uniform sampler2D texBloom;
12+
layout(set = 2, binding = 3) uniform sampler2D texBloom;
1313

1414
layout(location = 0) out vec4 outColor;
1515

1616
void main()
1717
{
18+
int levels = int(uniBloomParams.y);
1819
vec3 color = texelFetch(texColor, ivec2(gl_FragCoord), 0).rgb;
1920
vec3 bloom = vec3(0);
20-
vec2 uv = vec2(gl_FragCoord) / textureSize(texColor, 0).xy;
21-
for (int i = 0; i < uniLodLevel.x; i++)
21+
vec2 uv = vec2(gl_FragCoord) / vec2(textureSize(texColor, 0));
22+
for (int i = 0; i < levels; i++)
2223
bloom += textureLod(texBloom, uv, i).rgb;
23-
outColor = vec4(color + bloom / uniLodLevel.x, 1); // additive mixing
24+
outColor = vec4(color + bloom / levels, 1); // additive mixing
2425
}

data/cage/shaders/effects/bloomGenerate.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $define shader fragment
55

66
layout(std140, set = 2, binding = 0) uniform Params
77
{
8-
vec4 uniBloomParams; // threshold
8+
vec4 uniBloomParams; // threshold, blur passes
99
};
1010

1111
layout(set = 2, binding = 1) uniform sampler2D texColor;

data/cage/shaders/effects/effects.assets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dofApply.glsl
1010
dofCollect.glsl
1111
fxaa.glsl
1212
gaussianBlur.glsl
13+
makeMip.glsl
1314
sharpening.glsl
1415
ssaoDownscaleDepth.glsl
1516
ssaoGenerate.glsl

data/cage/shaders/effects/effects.pack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dofApply.glsl
55
dofCollect.glsl
66
fxaa.glsl
77
gaussianBlur.glsl
8+
makeMip.glsl
89
sharpening.glsl
910
ssaoDownscaleDepth.glsl
1011
ssaoGenerate.glsl

data/cage/shaders/effects/gaussianBlur.glsl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ $define shader fragment
88
const float offsets[3] = { 0.0, 1.3846153846, 3.2307692308 };
99
const float weights[3] = { 0.2270270270, 0.3162162162, 0.0702702703 };
1010

11-
layout(std140, set = 2, binding = 0) uniform Params
12-
{
13-
vec4 uniDirection; // xy
14-
ivec4 uniLodLevel; // x
15-
};
11+
#ifdef Vertical
12+
const vec2 direction = vec2(0, 1);
13+
#else
14+
const vec2 direction = vec2(1, 0);
15+
#endif
1616

17-
layout(set = 2, binding = 1) uniform sampler2D texInput;
17+
layout(set = 2, binding = 0) uniform sampler2D texInput;
1818

1919
layout(location = 0) out vec4 outOutput;
2020

2121
void main()
2222
{
23-
vec2 texelSize = 1 / vec2(textureSize(texInput, uniLodLevel.x));
23+
vec2 texelSize = 1 / vec2(textureSize(texInput, 0));
2424
vec2 center = gl_FragCoord.xy;
25-
vec4 val = textureLod(texInput, center * texelSize, uniLodLevel.x) * weights[0];
25+
vec4 val = textureLod(texInput, center * texelSize, 0) * weights[0];
2626
for(int i = 1; i < 3; i++)
2727
{
28-
val += textureLod(texInput, (center - uniDirection.xy * offsets[i]) * texelSize, uniLodLevel.x) * weights[i];
29-
val += textureLod(texInput, (center + uniDirection.xy * offsets[i]) * texelSize, uniLodLevel.x) * weights[i];
28+
val += textureLod(texInput, (center - direction * offsets[i]) * texelSize, 0) * weights[i];
29+
val += textureLod(texInput, (center + direction * offsets[i]) * texelSize, 0) * weights[i];
3030
}
3131
outOutput = val;
3232
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
$include vertex.glsl
3+
4+
$define shader fragment
5+
6+
layout(set = 2, binding = 0) uniform sampler2D texColor;
7+
8+
layout(location = 0) out vec4 outColor;
9+
10+
void main()
11+
{
12+
vec2 uv = 2 * vec2(gl_FragCoord) / vec2(textureSize(texColor, 0));
13+
outColor = textureLod(texColor, uv, 0);
14+
}

sources/libengine/graphics/bindings.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <svector.h>
2+
13
#include <cage-core/assetsManager.h>
24
#include <cage-core/concurrent.h>
35
#include <cage-core/hashString.h>
@@ -56,7 +58,7 @@ namespace cage
5658

5759
wgpu::BindGroupLayout createLayout(GraphicsDevice *device, const BindingsCreateConfig &config)
5860
{
59-
std::vector<wgpu::BindGroupLayoutEntry> entries;
61+
ankerl::svector<wgpu::BindGroupLayoutEntry, 10> entries;
6062
entries.reserve(config.buffers.size() + config.textures.size() * 2);
6163

6264
for (const auto &b : config.buffers)
@@ -114,7 +116,7 @@ namespace cage
114116

115117
wgpu::BindGroup createGroup(GraphicsDevice *device, const wgpu::BindGroupLayout &layout, const BindingsCreateConfig &config)
116118
{
117-
std::vector<wgpu::BindGroupEntry> entries;
119+
ankerl::svector<wgpu::BindGroupEntry, 10> entries;
118120
entries.reserve(config.buffers.size() + config.textures.size() * 2);
119121

120122
for (const auto &b : config.buffers)
@@ -164,18 +166,26 @@ namespace cage
164166

165167
struct Key
166168
{
167-
std::vector<uint32> keys;
169+
ankerl::svector<uint32, 5> keys;
168170

169171
std::size_t hash = 0;
170172

171173
Key(const BindingsCreateConfig &config)
172174
{
173175
keys.reserve(config.buffers.size() + 1 + config.textures.size());
174176
for (const auto &b : config.buffers)
175-
keys.push_back(b.binding + (b.dynamic * (1u << 10)) + (b.buffer->type() << 11));
177+
{
178+
const uint32 dyn = (uint32)b.dynamic << 10;
179+
const uint32 type = (uint32)b.buffer->type() << 11;
180+
keys.push_back(b.binding + dyn + type);
181+
}
176182
keys.push_back(75431564); // separator
177183
for (const auto &t : config.textures)
178-
keys.push_back(t.binding + ((uint32)t.texture->flags << 10));
184+
{
185+
const uint32 filterable = (uint32)isFormatFilterable(t.texture->nativeTexture().GetFormat()) << 10;
186+
const uint32 flags = (uint32)t.texture->flags << 11;
187+
keys.push_back(t.binding + filterable + flags);
188+
}
179189

180190
auto hashCombine = [&](std::unsigned_integral auto v) { hash ^= std::hash<std::decay_t<decltype(v)>>{}(v) + 0x9e3779b9 + (hash << 6) + (hash >> 2); };
181191
for (uint32 k : keys)

sources/libengine/graphics/encoder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <svector.h>
12
#include <webgpu/webgpu_cpp.h>
23

34
#include <cage-core/concurrent.h>
@@ -27,7 +28,7 @@ namespace cage
2728
struct Key
2829
{
2930
std::array<WGPUBindGroupLayout, 3> layouts = {};
30-
std::vector<wgpu::TextureFormat> colorFormats;
31+
ankerl::svector<wgpu::TextureFormat, 5> colorFormats;
3132
const Shader *shader = nullptr;
3233
MeshComponentsFlags meshComponents = MeshComponentsFlags::None;
3334
wgpu::TextureFormat depthFormat = wgpu::TextureFormat::Undefined;
@@ -122,7 +123,7 @@ namespace cage
122123
}
123124

124125
wgpu::BlendState blendState = convertBlending(config.blending);
125-
std::vector<wgpu::ColorTargetState> colors;
126+
ankerl::svector<wgpu::ColorTargetState, 1> colors;
126127
colors.reserve(passData.colorTargets.size());
127128
for (const auto &ct : passData.colorTargets)
128129
{
@@ -251,7 +252,7 @@ namespace cage
251252
if (!passData.bindings)
252253
passData.bindings = getEmptyBindings(device);
253254

254-
std::vector<wgpu::RenderPassColorAttachment> atts;
255+
ankerl::svector<wgpu::RenderPassColorAttachment, 1> atts;
255256
atts.reserve(passData.colorTargets.size());
256257
for (const auto &it : passData.colorTargets)
257258
{

0 commit comments

Comments
 (0)