Blender V5.3
eevee_surf_volume.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/* Based on Frostbite Unified Volumetric.
6 * https://www.ea.com/frostbite/news/physically-based-unified-volumetric-rendering-in-frostbite */
7
8/* Store volumetric properties into the froxel textures. */
9
10#pragma once
11
13
14/* Needed includes for shader nodes. */
15#include "draw_volume.bsl.hh"
16#include "eevee_attributes_volume_lib.bsl.hh" /* IWYU pragma: export */
21
22namespace eevee {
23
30
31struct SurfVolume {
34
36
38
39 [[image(
41 [[image(
46
48 {
49 float2 phase = float2(prop.anisotropy, 1.0f);
50
51 /* Do not add phase weight if there's no scattering. */
52 if (all(equal(prop.scattering, float3(0.0f)))) {
53 phase = float2(0.0f);
54 }
55
56 float3 extinction = prop.scattering + prop.absorption;
57
58 if (!is_world) [[static_branch]] {
59 /* Additive Blending. No race condition since we have a barrier between each conflicting
60 * invocations. */
61 prop.scattering += imageLoadFast(out_scattering_img, froxel).rgb;
62 prop.emission += imageLoadFast(out_emissive_img, froxel).rgb;
63 extinction += imageLoadFast(out_extinction_img, froxel).rgb;
64 phase.x += imageLoadFast(out_phase_img, froxel).r;
65 phase.y += imageLoadFast(out_phase_weight_img, froxel).r;
66 }
67
69 imageStoreFast(out_extinction_img, froxel, extinction.xyzz);
70 imageStoreFast(out_emissive_img, froxel, prop.emission.xyzz);
71 imageStoreFast(out_phase_img, froxel, phase.xxxx);
72 imageStoreFast(out_phase_weight_img, froxel, phase.yyyy);
73 }
74
76 ShadingData sd,
77 const Uniform &uni,
78 const ViewMatrices view,
79 const ObjectMatrices obj,
80 const ObjectInfos ob_infos,
81 int3 froxel,
82 float jitter)
83 {
84 float3 uvw = (float3(froxel) + float3(0.5f, 0.5f, 0.5f - jitter)) *
85 uni.uniform_buf.volumes.inv_tex_size;
86
87 float3 vP = volume_jitter_to_view(uni, view, uvw);
88 float3 wP = view.point_view_to_world(vP);
89 float3 lP = obj.point_world_to_object(wP);
90 /* Compute Original Coordinate (ORCO). */
91 float3 lP_orco = lP * ob_infos.orco_mul + ob_infos.orco_add;
92
93 sd.P = wP;
94 sd.N = float3(0.0f);
95 sd.Ng = float3(0.0f);
96 sd.ray_length = distance(sd.P, view.position());
97
98 VolumePoint volume_pt;
99 volume_pt.lP = lP;
100 volume_pt.orco_default = lP_orco;
101 for (int i = 0; i < 16 /* DRW_GRID_PER_VOLUME_MAX */; i++) [[unroll]] {
102 volume_pt.grid_co[i] = transform_point(volume.drw_volume.grids_xform[i], lP);
103 }
104
105 attrib_load(volume_pt);
106
107 nodetree_volume(kg, sd);
108
112
113 VolumeProperties prop;
116 prop.emission = sd.emission;
118 return prop;
119 }
120};
121
122/* Note: Only the front fragments have to be invoked. */
126 [[resource_table]] SurfVolume &srt,
127 [[resource_table]] const Uniform &uni,
128 [[resource_table]] const draw::Model &models,
129 [[resource_table]] const draw::View &views,
130 [[resource_table]] const draw::Infos &infos,
131 [[resource_table]] const Sampling &sampling,
132 [[resource_table]] const UtilityTexture & /*util_tx*/,
133 [[in]] const VertOutCommon &interp,
134 [[in]] [[condition(is_curves)]] const VertOutCurves &curves_interp,
135 [[in]] [[condition(is_pointcloud)]] const VertOutPointcloud &ptcloud_interp,
136 [[frag_coord]] const float4 frag_co,
137 [[front_facing]] const bool front_face)
138{
139 int3 froxel = int3(int2(frag_co.xy), 0);
140 float offset = sampling.rng_1D_get(SAMPLING_VOLUME_W);
141 float jitter = volume_froxel_jitter(froxel.xy, offset);
142
143 draw::ID id{interp.resource_id_raw};
144 const uint resource_id = id.resource_id<1>();
145 const ObjectMatrices obj = models.get(resource_id);
146 const ObjectInfos ob_infos = infos.get(resource_id);
147 const ViewMatrices view = views.get(0);
148
149 VolumeProperties prop;
150
151 ShadingData sd = init_globals(pipe, uni, interp, view, front_face, frag_co);
152 if (pipe.is_mesh) [[static_branch]] {
154 }
155 else if (pipe.is_curves) [[static_branch]] {
156 init_globals_curves(interp, curves_interp, sd, view);
157 }
158 else if (pipe.is_pointcloud) [[static_branch]] {
159 init_globals_pointcloud(ptcloud_interp, sd);
160 }
161
162 if (srt.is_homogenous) [[static_branch]] {
163 /* Homogenous volumes only evaluate properties at volume entrance and write the same values for
164 * each froxel. */
165 prop = srt.eval_froxel(kg, sd, uni, view, obj, ob_infos, froxel, jitter);
166 }
167
169
170 if (!srt.is_world) [[static_branch]] {
171 for (int j = 0; j < 8; j++) {
172 occupancy.bits[j] = imageLoad(srt.occupancy_img, int3(froxel.xy, j)).r;
173 }
174 }
175
176 /* Check all occupancy bits. */
177 for (int j = 0; j < 8; j++) {
178 for (int i = 0; i < 32; i++) {
179 froxel.z = j * 32 + i;
180
181 if (froxel.z >= imageSize(srt.out_scattering_img).z) {
182 break;
183 }
184
185 if (!srt.is_world) [[static_branch]] {
186 if (((occupancy.bits[j] >> i) & 1u) == 0) {
187 continue;
188 }
189 }
190
191 if (!srt.is_homogenous) [[static_branch]] {
192 /* Heterogeneous volumes evaluate properties at every froxel position. */
193 prop = srt.eval_froxel(kg, sd, uni, view, obj, ob_infos, froxel, jitter);
194 }
195 srt.write_froxel(froxel, prop);
196 }
197 }
198}
199
200} // namespace eevee
#define VOLUME_PROP_SCATTERING_IMG_SLOT
#define VOLUME_PROP_PHASE_WEIGHT_IMG_SLOT
#define VOLUME_OCCUPANCY_SLOT
#define VOLUME_PROP_EXTINCTION_IMG_SLOT
#define VOLUME_PROP_PHASE_IMG_SLOT
#define VOLUME_PROP_EMISSION_IMG_SLOT
void attrib_load(WorldPoint)
Closure nodetree_volume(KernelGlobals &, ShadingData &)
float3 volume_jitter_to_view(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
float volume_froxel_jitter(int2 froxel, float offset)
#define texture_atomic
#define image(slot, qualifiers, format)
#define front_facing
#define early_fragment_tests
#define frag_coord
#define compilation_constant
#define resource_table
#define static_branch
#define fragment
#define unroll
#define condition(cond)
float distance(float, float) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< uint, 3, false, true > uimage3DAtomic
ImageBase< float, 3 > image3D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageLoadFast
#define imageStoreFast
float3 transform_point(float3x3 mat, float3 point)
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
ShadingData init_globals(const eevee::PipelineConstants &pipe, const eevee::Uniform &uni, const VertOutCommon &interp, const ViewMatrices view, bool front_face, float4 fragment_co)
void surf_volume(KernelGlobals &kg, PipelineConstants &pipe, SurfVolume &srt, const Uniform &uni, const draw::Model &models, const draw::View &views, const draw::Infos &infos, const Sampling &sampling, const UtilityTexture &, const VertOutCommon &interp, const VertOutCurves &curves_interp, const VertOutPointcloud &ptcloud_interp, const float4 frag_co, const bool front_face)
void init_globals_mesh(const VertOutCommon &interp, ShadingData &sd, float3 barycentric_co)
void init_globals_pointcloud(const VertOutPointcloud &ptcloud_interp, ShadingData &sd)
void init_globals_curves(const VertOutCommon &interp, const VertOutCurves &curve_interp, ShadingData &sd, const ViewMatrices view)
packed_float3 orco_add
packed_float3 orco_mul
float3 point_world_to_object(float3 P) const
packed_float3 volume_absorption
packed_float3 emission
packed_float3 volume_scattering
float3 position() const
float3 point_view_to_world(float3 vP) const
float4x4 grids_xform[16]
float3 grid_co[DRW_GRID_PER_VOLUME_MAX]
VolumeInfos & drw_volume
VolumeProperties eval_froxel(KernelGlobals &kg, ShadingData sd, const Uniform &uni, const ViewMatrices view, const ObjectMatrices obj, const ObjectInfos ob_infos, int3 froxel, float jitter)
void write_froxel(int3 froxel, VolumeProperties prop)
const UniformData & uniform_buf
float x
float y