Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GenerateEnvironmentMapLight #9414

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
cd3e90f
WIP
JMS55 Aug 8, 2023
f682078
Rename EnvironmentMapPlugin to EnvironmentMapLightPlugin
JMS55 Aug 8, 2023
186283b
Small WIP for shader setup
JMS55 Aug 8, 2023
a6ad58d
WIP: Add most of the bind group setup
JMS55 Aug 8, 2023
5608501
WIP: Add node
JMS55 Aug 8, 2023
fd955e9
Export GenerateEnvironmentMapLight
JMS55 Aug 8, 2023
212ec68
More WIP
JMS55 Aug 10, 2023
22573cf
Rename
JMS55 Aug 10, 2023
fe75bba
Small fixes
JMS55 Aug 10, 2023
cf9b330
Small fix
JMS55 Aug 10, 2023
3ebbbdb
WIP
JMS55 Aug 10, 2023
368272d
Add further attribution
JMS55 Aug 10, 2023
53feb96
Bunch of fixes
JMS55 Aug 10, 2023
a92ab3c
Port downsample shader
JMS55 Aug 10, 2023
b0b23b3
Misc
JMS55 Aug 10, 2023
676c207
Misc
JMS55 Aug 12, 2023
2572f1c
WIP: Translate filter.wgsl
JMS55 Aug 12, 2023
71c15b1
Finish filter.wgsl
JMS55 Aug 13, 2023
d07c6ee
Filter shader fixes
JMS55 Aug 13, 2023
71fcf74
Finish filter dispatches
JMS55 Aug 13, 2023
848e92e
Fix specular map size (must be 128x128)
JMS55 Aug 13, 2023
bb74bce
Check texture format storage binding support
JMS55 Aug 13, 2023
00237ae
Add diffuse convolution
JMS55 Aug 13, 2023
2015a60
Update docs
JMS55 Aug 13, 2023
8ce8ee6
Fix no_renderer example
JMS55 Aug 13, 2023
3592760
Actually fix no_renderer example
JMS55 Aug 13, 2023
331a020
Only log warning once
JMS55 Aug 13, 2023
34664dd
Silence, clippy!
JMS55 Aug 13, 2023
d3606d7
Fix typo
JMS55 Aug 13, 2023
30c7805
Add platform support comment
JMS55 Aug 13, 2023
24750c6
Misc
JMS55 Aug 13, 2023
7dcd68f
Support both Rg11b10Float and Rgba16Float
JMS55 Aug 15, 2023
dd8dac6
Fix doc lints
JMS55 Aug 15, 2023
162bc35
Fix shader defs
JMS55 Aug 15, 2023
eee005d
Add new_with_texture_format()
JMS55 Aug 15, 2023
7574c51
Misc
JMS55 Aug 17, 2023
d9c03cf
Store coefficents in a binary file
JMS55 Aug 20, 2023
0521fee
Update crates/bevy_pbr/src/environment_map/generate_from_skybox.rs
JMS55 Aug 20, 2023
39138fa
Add shader notes
JMS55 Aug 20, 2023
c516896
Merge branch 'envfilter' of https://github.com/JMS55/bevy into envfilter
JMS55 Aug 20, 2023
e2bc48d
Misc doc change
JMS55 Aug 21, 2023
9ed1dcb
Merge commit '02025eff0b25bb8c18103dbafc33a764e1989ad4' into envfilter
JMS55 Sep 1, 2023
0695579
Rebase
JMS55 Sep 1, 2023
74ab5ce
Merge commit 'edba496697d3918ca5a2110363c502692ef9d2dd' into envfilter
JMS55 Sep 29, 2023
14ef095
Simplify shader
JMS55 Sep 29, 2023
08de4f9
Merge commit '4852fc7578b7a55db4c05a6fb9f8ebdd290571e1' into envfilter
JMS55 Nov 8, 2023
98d92c1
Finish rebase
JMS55 Nov 10, 2023
8bb1b30
Merge commit 'cbadc31d19e1d40841969ac503d49b3fff030f57' into envfilter
JMS55 Nov 10, 2023
dcd03c8
Merge commit '90958104cbe6a07c602a7ba4442e7cccb4148b1e' into envfilter
JMS55 Nov 28, 2023
e8ae28a
Fix shaders after rebase
JMS55 Nov 28, 2023
0b607e9
Merge commit 'a902ea6f85079777e61ea8627d86277c0d9abaf1' into envfilter
JMS55 Nov 28, 2023
2dc5475
Use new BGL API
JMS55 Nov 28, 2023
2ffeec6
Remove rust filter coefficients, fix typo
JMS55 Nov 28, 2023
9fe7a9d
Merge commit '11065974d487ca4b9680ae683a9d356f2f1b6c36' into envfilter
JMS55 Dec 14, 2023
4676948
Fix rebase
JMS55 Dec 14, 2023
82fe775
Address PR feedback
JMS55 Dec 15, 2023
eaf17d3
Address more CR feedback
JMS55 Dec 15, 2023
719402d
Merge commit '8067e46049f222d37ac394745805bad98979980f' into envfilter
JMS55 Dec 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/bevy_core_pipeline/src/skybox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ impl Plugin for SkyboxPlugin {

/// Adds a skybox to a 3D camera, based on a cubemap texture.
///
/// Note that this component does not (currently) affect the scene's lighting.
/// To do so, use `EnvironmentMapLight` alongside this component.
/// Note that this component does not affect the scene's lighting.
/// To do so, use `EnvironmentMapLight` or `GenerateEnvironmentMapLight`
/// alongside this component.
///
/// See also <https://en.wikipedia.org/wiki/Skybox_(video_games)>.
#[derive(Component, ExtractComponent, Clone)]
Expand Down
68 changes: 68 additions & 0 deletions crates/bevy_pbr/src/environment_map/diffuse_convolution.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Importance samples (Lambertian distribution) a skybox to produce a diffuse lighting cubemap
// Based on https://github.com/KhronosGroup/glTF-IBL-Sampler/blob/master/lib/source/shaders/filter.frag

#import bevy_pbr::utils::PI

@group(0) @binding(0) var skybox: texture_cube<f32>;
#ifdef RG11B10FLOAT
@group(0) @binding(1) var diffuse_map: texture_storage_2d_array<rg11b10float, write>;
#else
@group(0) @binding(1) var diffuse_map: texture_storage_2d_array<rgba16float, write>;
#endif
@group(0) @binding(2) var bilinear: sampler;

fn get_dir(u: f32, v: f32, face: u32) -> vec3<f32> {
switch face {
case 0u: { return vec3(1.0, v, -u); }
case 1u: { return vec3(-1.0, v, u); }
case 2u: { return vec3(u, 1.0, -v); }
case 3u: { return vec3(u, -1.0, v); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@JMS55 JMS55 Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same get_dir function from https://www.activision.com/cdn/research/filter_using_table_128.txt that I used in the other shaders

case 4u: { return vec3(u, v, 1.0); }
default { return vec3(-u, v, -1.0); }
}
}

fn generate_tbn(normal: vec3<f32>) -> mat3x3<f32> {
var bitangent = vec3(0.0, 1.0, 0.0);

let n_dot_up = dot(normal, bitangent);
if abs(n_dot_up) >= 0.9999999 {
bitangent = vec3(0.0, 0.0, sign(n_dot_up));
}

let tangent = normalize(cross(bitangent, normal));
bitangent = cross(normal, tangent);

return mat3x3(tangent, bitangent, normal);
}

@compute
@workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let u = (f32(id.x) * 2.0 + 1.0) / 64.0 - 1.0;
let v = -(f32(id.y) * 2.0 + 1.0) / 64.0 + 1.0;

let normal = normalize(get_dir(u, v, id.z));

var color = vec3(0.0);
for (var sample_i = 0u; sample_i < 32u; sample_i++) {
// R2 sequence - http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences
let r = fract(0.5 + f32(sample_i) * vec2<f32>(0.75487766624669276005, 0.5698402909980532659114));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor this out into a rand function :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 1 line, I'm inclined not to 😅


let cos_theta = sqrt(1.0 - f32(r.y));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let cos_theta = sqrt(1.0 - f32(r.y));
// map uniformly distributed [0..1)^2 into hemisphere with cosine importance (Lambertian distribution)
let cos_theta = sqrt(1.0 - f32(r.y));

let sin_theta = sqrt(r.y);
let phi = 2.0 * PI * r.x;

let local_space_direction = normalize(vec3(
sin_theta * cos(phi),
sin_theta * sin(phi),
cos_theta,
));
let direction = generate_tbn(normal) * local_space_direction;

color += textureSampleLevel(skybox, bilinear, direction, 0.0).rgb;
}
color /= 32.0;

textureStore(diffuse_map, id.xy, id.z, vec4(color, 1.0));
}
73 changes: 73 additions & 0 deletions crates/bevy_pbr/src/environment_map/downsample.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Step 1/2 in generating a specular lighting cubemap from a skybox: Downsamples a skybox into multiple mips
// Original source: https://www.activision.com/cdn/research/downsample_cubemap.txt

// Copyright 2016 Activision Publishing, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@group(0) @binding(0) var tex_hi_res: texture_cube<f32>;
#ifdef RG11B10FLOAT
@group(0) @binding(1) var tex_los_res: texture_storage_2d_array<rg11b10float, write>;
#else
@group(0) @binding(1) var tex_los_res: texture_storage_2d_array<rgba16float, write>;
#endif
@group(0) @binding(2) var bilinear: sampler;

fn get_dir(u: f32, v: f32, face: u32) -> vec3<f32> {
switch face {
case 0u: { return vec3(1.0, v, -u); }
case 1u: { return vec3(-1.0, v, u); }
case 2u: { return vec3(u, 1.0, -v); }
case 3u: { return vec3(u, -1.0, v); }
case 4u: { return vec3(u, v, 1.0); }
default { return vec3(-u, v, -1.0); }
}
}

fn calc_weight(u: f32, v: f32) -> f32 {
let val = u * u + v * v + 1.0;
return val * sqrt(val);
}

@compute
@workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let res_lo = textureDimensions(tex_los_res).x;

if all(vec2u(id.xy) < vec2u(res_lo)) {
let inv_res_lo = 1.0 / f32(res_lo);

let u0 = (f32(id.x) * 2.0 + 1.0 - 0.75) * inv_res_lo - 1.0;
let u1 = (f32(id.x) * 2.0 + 1.0 + 0.75) * inv_res_lo - 1.0;

let v0 = (f32(id.y) * 2.0 + 1.0 - 0.75) * -inv_res_lo + 1.0;
let v1 = (f32(id.y) * 2.0 + 1.0 + 0.75) * -inv_res_lo + 1.0;

var weights = vec4(calc_weight(u0, v0), calc_weight(u1, v0), calc_weight(u0, v1), calc_weight(u1, v1));
let wsum = 0.5 / dot(vec4(1.0), weights);
weights = weights * wsum + 0.125;

var color = textureSampleLevel(tex_hi_res, bilinear, get_dir(u0, v0, id.z), 0.0) * weights.x;
color += textureSampleLevel(tex_hi_res, bilinear, get_dir(u1, v0, id.z), 0.0) * weights.y;
color += textureSampleLevel(tex_hi_res, bilinear, get_dir(u0, v1, id.z), 0.0) * weights.z;
color += textureSampleLevel(tex_hi_res, bilinear, get_dir(u1, v1, id.z), 0.0) * weights.w;

textureStore(tex_los_res, id.xy, id.z, color);
}
}
183 changes: 183 additions & 0 deletions crates/bevy_pbr/src/environment_map/filter.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Step 2/2 in generating a specular lighting cubemap from a skybox: Importance sample the GGX distribution based on the downsampled cubemap
// Original source: https://www.activision.com/cdn/research/filter_using_table_128.txt

// Copyright 2016 Activision Publishing, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@group(0) @binding(0) var tex_in: texture_cube<f32>;
#ifdef RG11B10FLOAT
@group(0) @binding(1) var text_out0: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(2) var text_out1: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(3) var text_out2: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(4) var text_out3: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(5) var text_out4: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(6) var text_out5: texture_storage_2d_array<rg11b10float, write>;
@group(0) @binding(7) var text_out6: texture_storage_2d_array<rg11b10float, write>;
#else
@group(0) @binding(1) var text_out0: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(2) var text_out1: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(3) var text_out2: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(4) var text_out3: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(5) var text_out4: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(6) var text_out5: texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(7) var text_out6: texture_storage_2d_array<rgba16float, write>;
#endif
@group(0) @binding(8) var trilinear: sampler;
@group(0) @binding(9) var<uniform> coeffs: array<array<array<array<vec4<f32>, 24>, 3>, 5>, 7>;

fn get_dir(u: f32, v: f32, face: u32) -> vec3<f32> {
switch face {
case 0u: { return vec3(1.0, v, -u); }
case 1u: { return vec3(-1.0, v, u); }
case 2u: { return vec3(u, 1.0, -v); }
case 3u: { return vec3(u, -1.0, v); }
case 4u: { return vec3(u, v, 1.0); }
default { return vec3(-u, v, -1.0); }
}
}

@compute
@workgroup_size(64, 1, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
var id = global_id;
var level = 0u;
if id.x < 128u * 128u {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you went the other way (starting at the smallest mip level, which we will call level 0 in the below formula and going up) then you can calculate the minimum ID value for a level with ((1 << (level * 2)) - 1) & 0x55555555. (Bitwise, this encodes the pattern 0b1, 0b101, 0b10101, 0b1010101, etc.) You could put that in a function and then turn this into a loop, either linear search or binary search. That would result in cleaner code (and might be faster with binary search).

Going the other way would also let you avoid hardcoding 128 in a few places.

I did find a branchless way to calculate the level and texel within that level given the ID (with tricky use of leading zero count), but I think it's probably slower than linear/binary search given that branch divergence is likely to be low.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it has to do the biggest mip first for other reasons, maybe because all the mips after it depend on it. but wgsl workgroups are not guaranteed to run in any particular order so idk

level = 0u;
} else if id.x < 128u * 128u + 64u * 64u {
level = 1u;
id.x -= 128u * 128u;
} else if id.x < 128u * 128u + 64u * 64u + 32u * 32u {
level = 2u;
id.x -= 128u * 128u + 64u * 64u;
} else if id.x < 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u {
level = 3u;
id.x -= 128u * 128u + 64u * 64u + 32u * 32u;
} else if id.x < 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u + 8u * 8u {
level = 4u;
id.x -= 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u;
} else if id.x < 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u + 8u * 8u + 4u * 4u {
level = 5u;
id.x -= 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u + 8u * 8u;
} else if id.x < 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u + 8u * 8u + 4u * 4u + 2u * 2u {
level = 6u;
id.x -= 128u * 128u + 64u * 64u + 32u * 32u + 16u * 16u + 8u * 8u + 4u * 4u;
} else {
return;
}

id.z = id.y;
let res = 128u >> level;
id.y = id.x / res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could save an integer division here with

Suggested change
id.y = id.x / res;
id.y = id.x >> (7 - level);

id.x -= id.y * res;

let u = (f32(id.x) * 2.0 + 1.0) / f32(res) - 1.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let u = (f32(id.x) * 2.0 + 1.0) / f32(res) - 1.0;
// remap integers [0..res-1]^2 to the centers of every 2x2 texel patch we are mipping down to one texel, in (-1..1)^2
let u = (f32(id.x) * 2.0 + 1.0) / f32(res) - 1.0;

let v = -(f32(id.y) * 2.0 + 1.0) / f32(res) + 1.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain the intended mapping between id and uv? I would expect that this would map id = (0, 0) and id = (res, res) to uv = (-1, 1) and uv = (1, -1) respectively, but it doesn't seem to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, it's what the original shader does though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it maps to the centers of every other 2x2 texel patch, because the lower mip level must represent those texels as one larger one that covers the area of all 4 original ones.


let dir = get_dir(u, v, id.z);
let frame_z = normalize(dir);
let adir = abs(dir);

var color = vec4(0.0);
for (var axis = 0u; axis < 3u; axis++) {
let other_axis0 = 1u - (axis & 1u) - (axis >> 1u);
let other_axis1 = 2u - (axis >> 1u);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like if you're switching over axis below anyway you might as well just set other_axis0 and other_axis1 explicitly in a switch statement instead of using these bit tricks.


let frame_weight = (max(adir[other_axis0], adir[other_axis1]) - 0.75) / 0.25;
if frame_weight > 0.0 {
var up_vector = vec3(0.0);
switch axis {
case 0u: { up_vector = vec3(1.0, 0.0, 0.0); }
case 1u: { up_vector = vec3(0.0, 1.0, 0.0); }
default { up_vector = vec3(0.0, 0.0, 1.0); }
}
let frame_x = normalize(cross(up_vector, frame_z));
let frame_y = cross(frame_z, frame_x);

var nx = dir[other_axis0];
var ny = dir[other_axis1];
let nz = adir[axis];

let nmax_xy = max(abs(ny), abs(nx));
nx /= nmax_xy;
ny /= nmax_xy;

var theta = 0.0;
if ny < nx {
if ny <= -0.999 { theta = nx; } else { theta = ny; }
} else {
if ny >= 0.999 { theta = -nx; } else { theta = -ny; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of wonder if 0.999 should be factored out into an EPSILON constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I'm just keeping it to match the original shader code

}

var phi = 0.0;
if nz <= -0.999 {
phi = -nmax_xy;
} else if nz >= 0.999 {
phi = nmax_xy;
} else {
phi = nz;
}
let theta2 = theta * theta;
let phi2 = phi * phi;

for (var i_super_tap = 0u; i_super_tap < 8u; i_super_tap++) {
let index = 8u * axis + i_super_tap;
var coeffs_dir0 = array(vec4(0.0), vec4(0.0), vec4(0.0));
var coeffs_dir1 = array(vec4(0.0), vec4(0.0), vec4(0.0));
var coeffs_dir2 = array(vec4(0.0), vec4(0.0), vec4(0.0));
var coeffs_level = array(vec4(0.0), vec4(0.0), vec4(0.0));
var coeffs_weight = array(vec4(0.0), vec4(0.0), vec4(0.0));

for (var i_coeff = 0u; i_coeff < 3u; i_coeff++) {
coeffs_dir0[i_coeff] = coeffs[level][0u][i_coeff][index];
coeffs_dir1[i_coeff] = coeffs[level][1u][i_coeff][index];
coeffs_dir2[i_coeff] = coeffs[level][2u][i_coeff][index];
coeffs_level[i_coeff] = coeffs[level][3u][i_coeff][index];
coeffs_weight[i_coeff] = coeffs[level][4u][i_coeff][index];
}

for (var i_sub_tap = 0u; i_sub_tap < 4u; i_sub_tap++) {
var sample_dir = frame_x * (coeffs_dir0[0u][i_sub_tap] + coeffs_dir0[1u][i_sub_tap] * theta2 + coeffs_dir0[2u][i_sub_tap] * phi2) + frame_y * (coeffs_dir1[0u][i_sub_tap] + coeffs_dir1[1u][i_sub_tap] * theta2 + coeffs_dir1[2u][i_sub_tap] * phi2) + frame_z * (coeffs_dir2[0u][i_sub_tap] + coeffs_dir2[1u][i_sub_tap] * theta2 + coeffs_dir2[2u][i_sub_tap] * phi2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frame_x, frame_y, and frame_z are just basis vectors of a 3x3 matrix, no? Pretty sure you could turn this huge expression into just a matrix multiply if you converted coeffs_dir0, etc. into matrices.

I know that would be a lot of refactoring so feel free to leave this to a followup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might've been faster to not use the matrix ops, I'm inclined to leave it to match the original shader.


var sample_level = coeffs_level[0u][i_sub_tap] + coeffs_level[1u][i_sub_tap] * theta2 + coeffs_level[2u][i_sub_tap] * phi2;

var sample_weight = coeffs_weight[0u][i_sub_tap] + coeffs_weight[1u][i_sub_tap] * theta2 + coeffs_weight[2u][i_sub_tap] * phi2;
sample_weight *= frame_weight;

sample_dir /= max(abs(sample_dir[0u]), max(abs(sample_dir[1u]), abs(sample_dir[2u])));
JMS55 marked this conversation as resolved.
Show resolved Hide resolved
sample_level += 0.75 * log2(dot(sample_dir, sample_dir));
JMS55 marked this conversation as resolved.
Show resolved Hide resolved

color += vec4(textureSampleLevel(tex_in, trilinear, sample_dir, sample_level).rgb * sample_weight, sample_weight);
}
}
}
}
color /= color.a;
color = vec4(max(color.rgb, vec3(0.0)), 1.0);

switch level {
case 0u: { textureStore(text_out0, id.xy, id.z, color); }
case 1u: { textureStore(text_out1, id.xy, id.z, color); }
case 2u: { textureStore(text_out2, id.xy, id.z, color); }
case 3u: { textureStore(text_out3, id.xy, id.z, color); }
case 4u: { textureStore(text_out4, id.xy, id.z, color); }
case 5u: { textureStore(text_out5, id.xy, id.z, color); }
default { textureStore(text_out6, id.xy, id.z, color); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a local texture variable and then factor out the textureStore to avoid duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's any cleaner, really

}
}
Binary file not shown.
Loading
Loading