Skip to content

Commit

Permalink
nodes: add HexagonalBlur node
Browse files Browse the repository at this point in the history
  • Loading branch information
mbouron committed Mar 27, 2024
1 parent 4a82bbb commit 970ebf7
Show file tree
Hide file tree
Showing 14 changed files with 1,010 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Versioning](https://semver.org/spec/v2.0.0.html) for `libnopegl`.
- `DrawTexture.texture` now accepts transformation nodes before the texture node
to serve as a reframing mechanism (the transforms are applied to the texture
coordinates in a centered `[-1,1]` space with `(-1,-1)` in the bottom left)
- `HexagonalBlur` node to apply a post processing hexagonal bokeh blur effect to a
scene

### Changed
- `Text.font_files` text-based parameter is replaced with `Text.font_faces` node
Expand Down
4 changes: 4 additions & 0 deletions libnopegl/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ lib_src = files(
'src/node_graphicconfig.c',
'src/node_gridlayout.c',
'src/node_group.c',
'src/node_hblur.c',
'src/node_identity.c',
'src/node_io.c',
'src/node_eval.c',
Expand Down Expand Up @@ -675,6 +676,9 @@ shaders = {
'blur_downsample.frag': 'blur_downsample_frag.h',
'blur_upsample.frag': 'blur_upsample_frag.h',
'blur_interpolate.frag': 'blur_interpolate_frag.h',
'blur_hexagonal.vert': 'blur_hexagonal_vert.h',
'blur_hexagonal_pass1.frag': 'blur_hexagonal_pass1_frag.h',
'blur_hexagonal_pass2.frag': 'blur_hexagonal_pass2_frag.h',
'colorstats_init.comp': 'colorstats_init_comp.h',
'colorstats_sumscale.comp': 'colorstats_sumscale_comp.h',
'colorstats_waveform.comp': 'colorstats_waveform_comp.h',
Expand Down
33 changes: 33 additions & 0 deletions libnopegl/nodes.specs
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,39 @@
}
]
},
"HexagonalBlur": {
"file": "src/node_hblur.c",
"params": [
{
"name": "source",
"type": "node",
"node_types": ["Texture2D"],
"flags": ["nonull"],
"desc": "source to use for the blur"
},
{
"name": "destination",
"type": "node",
"node_types": ["Texture2D"],
"flags": ["nonull"],
"desc": "destination to use for the blur"
},
{
"name": "amount",
"type": "f32",
"default": 0.000000,
"flags": ["node"],
"desc": "amount of bluriness in the range [0,1]"
},
{
"name": "map",
"type": "node",
"node_types": ["Texture2D"],
"flags": [],
"desc": "blur map providing the CoC (circle of confusion) for each pixels"
}
]
},
"Identity": {
"file": "src/node_identity.c",
"params": [
Expand Down
31 changes: 31 additions & 0 deletions libnopegl/src/glsl/blur_hexagonal.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 Matthieu Bouron <[email protected]>
* Copyright 2023 Nope Forge
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const vec2 positions[] = vec2[](vec2(0.0, 0.0), vec2(2.0, 0.0), vec2(0.0, 2.0));

void main()
{
vec2 uv = positions[ngl_vertex_index];
ngl_out_pos = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
tex_coord = uv;
map_coord = uv;
}
48 changes: 48 additions & 0 deletions libnopegl/src/glsl/blur_hexagonal_pass1.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023-2024 Matthieu Bouron <[email protected]>
* Copyright 2023-2024 Nope Forge
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include helper_srgb.glsl
#include helper_blur.glsl

const vec2 up = vec2(0.0, 1.0); // cos(PI/2), sin(PI/2)
const vec2 down_left = vec2(0.8660254037844387, -0.5); // cos(-PI/6), sin(-PI/6)

void main()
{
float coc = texture(map, map_coord).r;
int radius = int(float(blur.radius) * coc);

float lod = 0.0;
float scale = 1.0;
int nb_samples = max(radius, 1);
if (radius > blur.nb_samples) {
scale = float(radius) / float(blur.nb_samples);
nb_samples = blur.nb_samples;
lod = log(scale);
}

vec4 color = ngli_blur_hexagonal(tex, tex_coord, map, map_coord, up, scale, lod, nb_samples);
vec4 color2 = ngli_blur_hexagonal(tex, tex_coord, map, map_coord, down_left, scale, lod, nb_samples);

ngl_out_color[0] = vec4(ngli_linear2srgb(color.rgb), color.a);
ngl_out_color[1] = vec4(ngli_linear2srgb(color.rgb + color2.rgb), color.a + color2.a);
}
55 changes: 55 additions & 0 deletions libnopegl/src/glsl/blur_hexagonal_pass2.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023-2024 Matthieu Bouron <[email protected]>
* Copyright 2023-2024 Nope Forge
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include helper_srgb.glsl
#include helper_blur.glsl

const vec2 down_left = vec2(0.8660254037844387, -0.5); // cos(-PI/6), sin(-PI/6)
const vec2 down_right = vec2(-0.8660254037844387, -0.5); // cos(-5*PI/6), sin(-5*PI/6)

/*
* Hexagonal blur pass 2 (down-left, down-right + combine)
* Adapted from WHITE, John, and BARRÉ-BRISEBOIS, Colin. More Performance! Five
* Rendering Ideas From Battlefield 3 and Need For Speed: The Run, Advances in
* Real-Time Rendering in Games, SIGGRAPH 2011:
* https://www.slideshare.net/DICEStudio/five-rendering-ideas-from-battlefield-3-need-for-speed-the-run
*/
void main()
{
float coc = texture(map, map_coord).r;
int radius = int(float(blur.radius) * coc);

float lod = 0.0;
float scale = 1.0;
int nb_samples = max(radius, 1);
if (radius > blur.nb_samples) {
scale = float(radius) / float(blur.nb_samples);
nb_samples = blur.nb_samples;
lod = log(scale);
}

vec4 color = ngli_blur_hexagonal(tex0, tex_coord, map, map_coord, down_left, scale, lod, nb_samples);
vec4 color2 = ngli_blur_hexagonal(tex1, tex_coord, map, map_coord, down_right, scale, lod, nb_samples);

vec4 out_color = mix(color, color2, 0.5);
ngl_out_color = vec4(ngli_linear2srgb(out_color.rgb), out_color.a);
}
47 changes: 47 additions & 0 deletions libnopegl/src/glsl/helper_blur.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Matthieu Bouron <[email protected]>
* Copyright 2024 Nope Forge
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

vec4 ngli_blur_hexagonal(sampler2D tex, vec2 tex_coord, sampler2D map, vec2 map_coord, vec2 direction, float scale, float lod, int nb_samples)
{
nb_samples = max(nb_samples, 1);

float use_coc = nb_samples > 1 ? 1.0 : 0.0;
float offset = 0.5 * use_coc;

vec2 tex_step = 1.0 / vec2(textureSize(tex, 0).xy);
vec2 tex_direction = tex_step * direction;

tex_coord += tex_direction * offset;
tex_direction *= scale;

vec4 color = vec4(0.0);
float amount = 0.0;
for (int i = 0; i < nb_samples; i++) {
vec2 coord_offset = float(i) * tex_direction;
vec4 value = textureLod(tex, tex_coord + coord_offset, lod);
float coc = texture(map, map_coord + coord_offset).r;
coc = mix(1.0, coc, use_coc);
color += vec4(ngli_srgb2linear(value.rgb), value.a) * coc;
amount += coc;
}
return color / amount;
}
Loading

0 comments on commit 970ebf7

Please sign in to comment.