Blender V5.3
eevee_lightprobe_display.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
7#include "draw_view.bsl.hh"
13
15
16namespace planar::display {
17
18struct VertOut {
21};
22
23struct FragOut {
25};
26
27struct Resources {
29
30 [[storage(0, read)]] PlanarProbeDisplayData (&display_data_buf)[];
31};
32
33[[vertex]] [[clip_control]]
35 [[resource_table]] const draw::View &views,
36 [[vertex_id]] const int vert_id,
37 [[position]] float4 &out_position,
38 [[out]] VertOut &v_out)
39{
40 /* Constant array moved inside function scope.
41 * Minimizes local register allocation in MSL. */
42 const float2 pos[6] = {float2(-1.0f, -1.0f),
43 float2(1.0f, -1.0f),
44 float2(-1.0f, 1.0f),
45
46 float2(1.0f, -1.0f),
47 float2(1.0f, 1.0f),
48 float2(-1.0f, 1.0f)};
49
50 float2 lP = pos[vert_id % 6];
51
52 v_out.display_index = vert_id / 6;
53 v_out.probe_index = srt.display_data_buf[v_out.display_index].probe_index;
54
55 float4x4 plane_to_world = srt.display_data_buf[v_out.display_index].plane_to_world;
56
57 float3 P = transform_point(plane_to_world, float3(lP, 0.0f));
58 out_position = views.get(0).point_world_to_homogenous(P);
59 /* Small bias to let the probe draw without Z-fighting. */
60 out_position.z -= 0.0001f;
61 out_position = reverse_z::transform(out_position);
62}
63
64[[fragment]]
66 [[resource_table]] const draw::View &views,
69 [[frag_coord]] const float4 frag_co,
70 [[in]] const VertOut &v_out,
71 [[out]] FragOut &frag_out)
72{
73 float4x4 plane_to_world = srt.display_data_buf[v_out.display_index].plane_to_world;
74
75 float2 uv = frag_co.xy / float2(textureSize(planes.planar_radiance_tx, 0).xy);
76 /* Render is inverted in Y. */
77 float2 planar_uv = float2(uv.x, 1.0f - uv.y);
78
79 const ViewMatrices view = views.get(0);
80
81 float depth = reverse_z::read(
82 textureLod(planes.planar_depth_tx, float3(planar_uv, v_out.probe_index), 0.0f).r);
83 if (depth == 1.0f) {
84 float3 ndc = view.screen_to_ndc(float3(uv, 0.0f));
85 float3 wP = view.point_ndc_to_world(ndc);
87 float3 R = -reflect(V, safe_normalize(float3(plane_to_world[2].xyz)));
88
89 SphereProbeUvArea world_atlas_coord = reinterpret_as_atlas_coord(srt.world_coord_packed);
90 frag_out.color = spheres.sample_probe(R, 0.0f, world_atlas_coord);
91 }
92 else {
93 frag_out.color = textureLod(
94 planes.planar_radiance_tx, float3(planar_uv, v_out.probe_index), 0.0f);
95 }
96 frag_out.color.a = 0.0f;
97}
98
99} // namespace planar::display
100
102
103struct VertOut {
106};
107
108struct FragOut {
110};
111
125
126[[vertex]] [[clip_control]]
128 [[resource_table]] const draw::View &views,
129 [[vertex_id]] const int vert_id,
130 [[position]] float4 &out_position,
131 [[out]] VertOut &v_out)
132{
133 /* Constant array moved inside function scope.
134 * Minimizes local register allocation in MSL. */
135 const float2 pos[6] = {float2(-1.0f, -1.0f),
136 float2(1.0f, -1.0f),
137 float2(-1.0f, 1.0f),
138
139 float2(1.0f, -1.0f),
140 float2(1.0f, 1.0f),
141 float2(-1.0f, 1.0f)};
142
143 v_out.lP = pos[vert_id % 6];
144 int cell_index = vert_id / 6;
145
146 int3 grid_res = srt.grid_resolution;
147
148 v_out.cell = int3(cell_index / (grid_res.z * grid_res.y),
149 (cell_index / grid_res.z) % grid_res.y,
150 cell_index % grid_res.z);
151
153 srt.grid_to_world, grid_res, v_out.cell);
154
155 float sphere_radius_final = srt.sphere_radius;
156 if (srt.display_validity) {
157 float validity = texelFetch(srt.validity_tx, v_out.cell, 0).r;
158 sphere_radius_final *= mix(1.0f, 0.1f, validity);
159 }
160
161 const ViewMatrices view = views.get(0);
162
163 float3 vs_offset = float3(v_out.lP, 0.0f) * sphere_radius_final;
164 float3 vP = view.point_world_to_view(ws_cell_pos) + vs_offset;
165
166 out_position = view.point_view_to_homogenous(vP);
167 /* Small bias to let the icon draw without Z-fighting. */
168 out_position.z += 0.0001f;
169 out_position = reverse_z::transform(out_position);
170}
171
172[[fragment]]
174 [[resource_table]] const draw::View &views,
175 [[in]] const VertOut &v_out,
176 [[out]] FragOut &frag_out)
177{
178 float dist_squared = length_squared(v_out.lP);
179
180 /* Discard outside the circle. */
181 if (dist_squared > 1.0f) {
183 return;
184 }
185
187 sh.L0.M0 = texelFetch(srt.irradiance_a_tx, v_out.cell, 0);
188 sh.L1.Mn1 = texelFetch(srt.irradiance_b_tx, v_out.cell, 0);
189 sh.L1.M0 = texelFetch(srt.irradiance_c_tx, v_out.cell, 0);
190 sh.L1.Mp1 = texelFetch(srt.irradiance_d_tx, v_out.cell, 0);
191 float validity = texelFetch(srt.validity_tx, v_out.cell, 0).r;
192
193 float3 vN = float3(v_out.lP, sqrt(max(0.0f, 1.0f - dist_squared)));
194 float3 N = views.get(0).normal_view_to_world(vN);
195 float3 lN = transform_direction(srt.world_to_grid, N);
196
197 float3 irradiance = sh.evaluate_lambert(lN).rgb;
198
199 if (srt.display_validity) {
200 frag_out.color = float4(mix(float3(1.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), validity),
201 0.0f);
202 }
203 else {
204 frag_out.color = float4(irradiance, 0.0f);
205 }
206}
207
208} // namespace volume::display
209
211
217
218struct FragOut {
220};
221
222struct Resources {
224
225 [[storage(0, read)]] SphereProbeDisplayData (&display_data_buf)[];
226};
227
228[[vertex]] [[clip_control]]
230 [[resource_table]] const draw::View &views,
232 [[vertex_id]] const int vert_id,
233 [[position]] float4 &out_position,
234 [[out]] VertOut &v_out)
235{
236 /* Constant array moved inside function scope.
237 * Minimizes local register allocation in MSL. */
238 const float2 pos[6] = {float2(-1.0f, -1.0f),
239 float2(1.0f, -1.0f),
240 float2(-1.0f, 1.0f),
241
242 float2(1.0f, -1.0f),
243 float2(1.0f, 1.0f),
244 float2(-1.0f, 1.0f)};
245
246 v_out.lP = pos[vert_id % 6];
247 int display_index = vert_id / 6;
248
249 v_out.probe_index = srt.display_data_buf[display_index].probe_index;
250 float sphere_radius = srt.display_data_buf[display_index].display_size;
251
252 float3 ws_probe_pos = spheres.lightprobe_sphere_buf[v_out.probe_index].location;
253
254 const ViewMatrices view = views.get(0);
255
256 float3 vs_offset = float3(v_out.lP, 0.0f) * sphere_radius;
257 float3 vP = view.point_world_to_view(ws_probe_pos) + vs_offset;
258 v_out.P = view.point_view_to_world(vP);
259
260 out_position = view.point_view_to_homogenous(vP);
261 /* Small bias to let the icon draw without Z-fighting. */
262 out_position.z += 0.0001f;
263 out_position = reverse_z::transform(out_position);
264}
265
266[[fragment]]
267void frag_main([[resource_table]] const Resources & /*srt*/,
268 [[resource_table]] const draw::View &views,
270 [[in]] const VertOut &v_out,
271 [[out]] FragOut &frag_out)
272{
273 float dist_squared = length_squared(v_out.lP);
274
275 /* Discard outside the circle. */
276 if (dist_squared > 1.0f) {
278 return;
279 }
280
281 const ViewMatrices view = views.get(0);
282
283 float3 vN = float3(v_out.lP, sqrt(max(0.0f, 1.0f - dist_squared)));
284 float3 N = view.normal_view_to_world(vN);
285 float3 V = view.world_incident_vector(v_out.P);
286 float3 L = reflect(-V, N);
287
288 frag_out.color = spheres.sample_probe(
289 L, 0, spheres.lightprobe_sphere_buf[v_out.probe_index].atlas_coord);
290 frag_out.color.a = 0.0f;
291}
292
293} // namespace sphere::display
294
295} // namespace eevee::lightprobe
296
btScalar max
PipelineGraphic eevee_display_lightprobe_planar(eevee::lightprobe::planar::display::vert_main, eevee::lightprobe::planar::display::frag_main)
PipelineGraphic eevee_display_lightprobe_volume(eevee::lightprobe::volume::display::vert_main, eevee::lightprobe::volume::display::frag_main)
PipelineGraphic eevee_display_lightprobe_sphere(eevee::lightprobe::sphere::display::vert_main, eevee::lightprobe::sphere::display::frag_main)
#define storage(slot, qualifiers)
#define position
#define flat
#define frag_coord
#define smooth
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define clip_control
#define vertex
#define frag_color(slot)
void gpu_discard_fragment()
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
#define sqrt
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 3 > sampler3D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
float length_squared(VecT a)
VecT safe_normalize(VecT vector)
#define N
#define R
#define L
MatBase< float, 4, 4 > float4x4
void vert_main(const Resources &srt, const draw::View &views, const int vert_id, float4 &out_position, VertOut &v_out)
void frag_main(const Resources &srt, const draw::View &views, const LightprobeSphereRenderData &spheres, const LightprobePlaneRenderData &planes, const float4 frag_co, const VertOut &v_out, FragOut &frag_out)
void frag_main(const Resources &, const draw::View &views, const LightprobeSphereRenderData &spheres, const VertOut &v_out, FragOut &frag_out)
void vert_main(const Resources &srt, const draw::View &views, const LightprobeSphereRenderData &spheres, const int vert_id, float4 &out_position, VertOut &v_out)
void vert_main(const Resources &srt, const draw::View &views, const int vert_id, float4 &out_position, VertOut &v_out)
void frag_main(const Resources &srt, const draw::View &views, const VertOut &v_out, FragOut &frag_out)
float3 grid_sample_position(float4x4 grid_local_to_world_mat, int3 grid_res, int3 cell_coord)
SphereProbeUvArea reinterpret_as_atlas_coord(int4 packed_coord)
T read(T depth_buffer_value)
float4 transform(float4 hs_position)
#define mix
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
float3 point_world_to_view(float3 P) const
float4 point_view_to_homogenous(float3 vP) const
float3 point_ndc_to_world(float3 ssP) const
T screen_to_ndc(T ss_P) const
float3 point_view_to_world(float3 vP) const
float3 world_incident_vector(float3 P) const
float x
float y
float z
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V