Blender V5.3
eevee_volume_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8#include "draw_view.bsl.hh"
11#include "eevee_uniform.bsl.hh"
14
15/* Based on Frostbite Unified Volumetric.
16 * https://www.ea.com/frostbite/news/physically-based-unified-volumetric-rendering-in-frostbite */
17
25
26/* Per froxel jitter to break slices and flickering.
27 * Wrapped so that changing it is easier. */
28float volume_froxel_jitter(int2 froxel, float offset)
29{
30 return random::interleaved_gradient(float2(froxel), 0.0f, offset);
31}
32
33/* Volume froxel texture normalized linear Z to view space Z.
34 * Not dependent on projection matrix (as long as drw_view_is_perspective is consistent). */
35float volume_z_to_view_z(const eevee::Uniform &uni, const ViewMatrices view, float z)
36{
37 float near = uni.uniform_buf.volumes.depth_near;
38 float far = uni.uniform_buf.volumes.depth_far;
39 float distribution = uni.uniform_buf.volumes.depth_distribution;
40 if (view.is_perspective()) {
41 /* Exponential distribution. */
42 return (exp2(z / distribution) - near) / far;
43 }
44 else {
45 /* Linear distribution. */
46 return near + (far - near) * z;
47 }
48}
49
50/* View space Z to volume froxel texture normalized linear Z.
51 * Not dependent on projection matrix (as long as drw_view_is_perspective is consistent). */
53 const ViewMatrices view, float depth, float near, float far, float distribution)
54{
55 if (view.is_perspective()) {
56 /* Exponential distribution. */
57 return distribution * log2(max(1e-8, depth * far + near));
58 }
59 else {
60 /* Linear distribution. */
61 return (depth - near) / (far - near);
62 }
63}
64float view_z_to_volume_z(const eevee::Uniform &uni, const ViewMatrices view, float depth)
65{
66 return view_z_to_volume_z(view,
67 depth,
68 uni.uniform_buf.volumes.depth_near,
69 uni.uniform_buf.volumes.depth_far,
70 uni.uniform_buf.volumes.depth_distribution);
71}
72
73/* Perspective volumes slice the froxel grid by planar slices, which under-integrates fog density
74 * away from screen center. Each panoramic face covers ~90 degrees FOV, wide enough for that error
75 * to visibly thin the fog toward the face corners. Panoramic cameras slice by signed camera
76 * distance instead (radial slices, equal thickness at any angle), shared by all 6 faces. */
78{
79 return is_panoramic(uni.uniform_buf.camera.type);
80}
81
82/* Reconstruct a view-space point at a known view-space Z. */
84 const ViewMatrices view,
85 float2 screen_uv,
86 float view_z)
87{
88 /* Since we use an infinite projection matrix for rendering inside the jittered volumes,
89 * we need to use a different matrix to reconstruct positions as the infinite matrix is not
90 * always invertible. */
91 float4x4 winmat = uni.uniform_buf.volumes.winmat_finite;
92 float4x4 wininv = uni.uniform_buf.volumes.wininv_finite;
93 /* We need to recover the NDC position for correct perspective divide. */
94 float ndc_z = view.perspective_divide(winmat * float4(0.0f, 0.0f, view_z, 1.0f)).z;
95 float2 ndc_xy = view.screen_to_ndc(screen_uv);
96 /* NDC to view. */
97 return view.perspective_divide(wininv * float4(ndc_xy, ndc_z, 1.0f)).xyz;
98}
99
100/* Return the signed depth that is used to address a volume Z slice. */
102{
103 return volume_uses_radial_depth(uni) ? -length(vP) : vP.z;
104}
105
106/* Jittered volume texture normalized coordinates to view space position. */
108{
109 /* Input coordinates are in jittered volume texture space. */
110 float view_z = volume_z_to_view_z(uni, view, coord.z);
111 float3 vP = volume_screen_to_view(uni, view, coord.xy(), view_z);
112 if (volume_uses_radial_depth(uni)) {
113 return normalize(vP) * -view_z;
114 }
115 return vP;
116}
117
118/* View space position to jittered volume texture normalized coordinates. */
120{
121 /* Since we use an infinite projection matrix for rendering inside the jittered volumes,
122 * we need to use a different matrix to reconstruct positions as the infinite matrix is not
123 * always invertible. */
124 float4x4 winmat = uni.uniform_buf.volumes.winmat_finite;
125 /* View to ndc. */
126 float3 ndc_P = view.perspective_divide(winmat * float4(vP, 1.0f));
127 /* Here, screen is the same as volume texture UVW space. */
128 return float3(view.ndc_to_screen(ndc_P.xy()),
129 view_z_to_volume_z(uni, view, volume_view_depth_get(uni, vP)));
130}
131
132/* Volume texture normalized coordinates (UVW) to render screen (UV).
133 * Expect active view to be the main view. */
135{
136 if (volume_uses_radial_depth(uni)) {
137 float3 vP = volume_jitter_to_view(uni, view, coord);
138 coord.z = view.depth_view_to_screen(vP.z);
139 }
140 else {
141 coord.z = volume_z_to_view_z(uni, view, coord.z);
142 coord.z = view.depth_view_to_screen(coord.z);
143 }
144 coord.xy /= uni.uniform_buf.volumes.coord_scale;
145 return coord;
146}
147/* Render screen (UV) to volume texture normalized coordinates (UVW).
148 * Expect active view to be the main view. */
150{
151 coord.xy *= uni.uniform_buf.volumes.coord_scale;
152 float view_z = view.depth_screen_to_view(coord.z);
153 if (volume_uses_radial_depth(uni)) {
154 float3 vP = volume_screen_to_view(uni, view, coord.xy(), view_z);
155 coord.z = view_z_to_volume_z(uni, view, volume_view_depth_get(uni, vP));
156 }
157 else {
158 coord.z = view_z_to_volume_z(uni, view, view_z);
159 }
160 return coord;
161}
162
163/* Returns the uvw (normalized coordinate) of a froxel in the previous frame.
164 * Returns float3(-1) if history is unavailable. */
166{
167 /* We can't re-project by a simple matrix multiplication. We first need to remap to the view Z,
168 * then transform, then remap back to Volume range. */
169 float3 uvw = (float3(froxel) + 0.5f) * uni.uniform_buf.volumes.inv_tex_size;
170 float3 vs_P;
171 if (volume_uses_radial_depth(uni)) {
172 vs_P = volume_jitter_to_view(uni, view, uvw);
173 }
174 else {
175 float4x4 wininv = uni.uniform_buf.volumes.wininv_stable;
176 float4x4 winmat = uni.uniform_buf.volumes.winmat_stable;
177 float3 ndc_P = view.screen_to_ndc(uvw);
178 /* We need to recover the NDC position for correct perspective divide. */
179 float view_z = volume_z_to_view_z(uni, view, uvw.z);
180 ndc_P.z = view.perspective_divide(winmat * float4(0.0f, 0.0f, view_z, 1.0f)).z;
181 /* NDC to view. */
182 vs_P = project_point(wininv, ndc_P);
183 }
184
185 /* Transform to previous camera view space. */
186 float3 vs_P_history = transform_point(uni.uniform_buf.volumes.curr_view_to_past_view, vs_P);
187
188 /* View to NDC. */
189 float4 hs_P_history = uni.uniform_buf.volumes.history_winmat_stable * float4(vs_P_history, 1.0f);
190 float3 ndc_P_history = view.perspective_divide(hs_P_history);
191
192 if (hs_P_history.w < 0.0f || any(greaterThan(abs(ndc_P_history.xy), float2(1.0f)))) {
193 return float3(-1.0f);
194 }
195
196 float3 uvw_history;
197 uvw_history.xy = view.ndc_to_screen(ndc_P_history.xy());
198 uvw_history.z = view_z_to_volume_z(view,
199 volume_view_depth_get(uni, vs_P_history),
200 uni.uniform_buf.volumes.history_depth_near,
201 uni.uniform_buf.volumes.history_depth_far,
202 uni.uniform_buf.volumes.history_depth_distribution);
203
204 if (uvw_history.z < 0.0f || uvw_history.z > 1.0f) {
205 return float3(-1.0f);
206 }
207 return uvw_history;
208}
209
211{
212 return 1.0f / (4.0f * M_PI);
213}
214
216{
217 /* Henyey-Greenstein. */
218 float cos_theta = dot(V, L);
219 g = clamp(g, -1.0f + 1e-3f, 1.0f - 1e-3f);
220 float sqr_g = g * g;
221 return (1 - sqr_g) / max(1e-8f, 4.0f * M_PI * pow(1 + sqr_g - 2 * g * cos_theta, 3.0f / 2.0f));
222}
223
225{
226 /* Compute rotated zonal harmonic.
227 * From Bartlomiej Wronsky
228 * "Volumetric Fog: Unified compute shader based solution to atmospheric scattering" page 55
229 * SIGGRAPH 2014
230 * https://bartwronski.files.wordpress.com/2014/08/bwronski_volumetric_fog_siggraph2014.pdf
231 */
237 return sh;
238}
239
244
248
251
253 {
255
256 /* Volumes objects have the same aliasing problems has shadow maps.
257 * To fix this we need a quantization bias (the size of a step in Z) and a slope bias
258 * (multiplied by the size of a froxel in 2D). */
259 coord.z -= uniforms.uniform_buf.volumes.inv_tex_size.z;
260 /* TODO(fclem): Slope bias. */
261
262 VolumeResolveSample volume;
263 volume.scattering = texture(volume_scattering_tx, coord).rgb;
264 volume.transmittance = texture(volume_transmittance_tx, coord).rgb;
265 return volume;
266 }
267};
#define M_PI
ccl_device_inline float cos_theta(const float3 w)
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define VOLUME_PROP_SCATTERING_IMG_SLOT
#define VOLUME_PROP_PHASE_WEIGHT_IMG_SLOT
#define VOLUME_TRANSMITTANCE_TEX_SLOT
#define VOLUME_PROP_EXTINCTION_IMG_SLOT
#define VOLUME_PROP_PHASE_IMG_SLOT
#define VOLUME_SCATTERING_TEX_SLOT
#define VOLUME_PROP_EMISSION_IMG_SLOT
float volume_view_depth_get(const eevee::Uniform &uni, float3 vP)
float3 volume_resolve_to_screen(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
SphericalHarmonicL1< float4 > volume_phase_function_as_sh_L1(float3 V, float g)
float volume_z_to_view_z(const eevee::Uniform &uni, const ViewMatrices view, float z)
float3 volume_view_to_jitter(const eevee::Uniform &uni, const ViewMatrices view, float3 vP)
float3 volume_jitter_to_view(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
float3 volume_screen_to_resolve(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
float volume_phase_function(float3 V, float3 L, float g)
float volume_phase_function_isotropic()
float3 volume_screen_to_view(const eevee::Uniform &uni, const ViewMatrices view, float2 screen_uv, float view_z)
float volume_froxel_jitter(int2 froxel, float offset)
float view_z_to_volume_z(const ViewMatrices view, float depth, float near, float far, float distribution)
float3 volume_history_uvw_get(const eevee::Uniform &uni, const ViewMatrices view, int3 froxel)
bool volume_uses_radial_depth(const eevee::Uniform &uni)
#define image(slot, qualifiers, format)
#define resource_table
#define sampler(slot)
#define exp2
bool any(VecOp< bool, D >) RET
#define pow
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define log2
#define abs
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
ImageBase< float, 3 > image3D
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 3 > sampler3D
float3 transform_point(float3x3 mat, float3 point)
float2 project_point(float3x3 mat, float2 point)
#define L
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
float interleaved_gradient(float2 pixel, float seed, float offset)
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
VolumeResolveSample resolve(float3 ndc_P) const
bool is_perspective() const
float3 perspective_divide(float4 hs_P) const
float depth_screen_to_view(float ss_depth) const
T ndc_to_screen(T ndc_P) const
T screen_to_ndc(T ss_P) const
float depth_view_to_screen(float v_depth) const
ViewMatrices get(uint view_id) const
const UniformData & uniform_buf
float z
Definition sky_math.h:136
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V