Blender V5.3
eevee_velocity.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "draw_view.bsl.hh"
10#include "eevee_defines.hh"
14
15namespace eevee::velocity {
16
18{
19 return data * 0.01f;
20}
21
23{
24 return data * 100.0f;
25}
26
27struct VertexCopy {
28 [[storage(0, read)]] const float (&in_buf)[];
29 [[storage(1, write)]] float4 (&out_buf)[];
33};
34
35/* Buffer copy using compute shader.
36 * Allows to pad data for 16byte alignment regardless of input layout. */
39 [[global_invocation_id]] const uint3 global_id,
40 [[num_work_groups]] const uint3 groups_count)
41{
42 uint vert_start = uint(srt.start_offset);
43 uint vert_count = uint(srt.vertex_count);
44 uint vert_stride = uint(srt.vertex_stride);
45
46 uint vertices_per_thread = divide_ceil(vert_count, uint(VERTEX_COPY_GROUP_SIZE)) /
47 groups_count.x;
48 uint vertex_start = min(global_id.x * vertices_per_thread, vert_count);
49 uint vertex_end = min(vertex_start + vertices_per_thread, vert_count);
50
51 for (uint vertex_id = vertex_start; vertex_id < vertex_end; vertex_id++) {
52 srt.out_buf[vert_start + vertex_id] = float4(
53 srt.in_buf[vertex_id * vert_stride + 0],
54 srt.in_buf[vertex_id * vert_stride + 1],
55 srt.in_buf[vertex_id * vert_stride + 2],
56 1.0f /* TODO(fclem): Remove padding or use it for some other data (radius?). */);
57 }
58}
59
60} // namespace eevee::velocity
61
62namespace eevee {
63
68 /* U is periodic. */
70 {
71 if (camera_curr.type == CAMERA_PANO_EQUIRECT) {
72 const float period = abs(M_TAU * camera_curr.equirect_scale_inv.x);
73 motion.xz -= period * round(motion.xz / period);
74 }
75 return motion;
76 }
77
83 {
84 float2 prev_uv, curr_uv, next_uv;
85 const bool panoramic = is_panoramic(camera_curr.type);
86 if (panoramic) {
87 /* Project through the panoramic mapping so the motion vector lands in the same warped film
88 * space the accumulation buffer is stored in. */
90 transform_point(camera_prev.viewmat, P_prv));
93 transform_point(camera_next.viewmat, P_nxt));
94 }
95 else {
96 /* NOTE: We use CameraData matrices instead of drw_view().persmat to avoid adding the TAA
97 * jitter to the velocity. */
98 prev_uv = project_point(camera_prev.persmat, P_prv).xy;
99 curr_uv = project_point(camera_curr.persmat, P).xy;
100 next_uv = project_point(camera_next.persmat, P_nxt).xy;
101 }
102 /* Fix issue with perspective division. */
103 if (any(isnan(prev_uv))) {
104 prev_uv = curr_uv;
105 }
106 if (any(isnan(next_uv))) {
107 next_uv = curr_uv;
108 }
109 /* NOTE: We output both vectors in the same direction so we can reuse the same vector
110 * with RGRG swizzle in viewport. */
111 float4 motion = float4(prev_uv - curr_uv, curr_uv - next_uv);
112 if (!panoramic) {
113 /* Convert NDC velocity to UV velocity. `uv_from_view` above already returns UV space
114 * directly for the panoramic case. */
115 motion *= 0.5f;
116 }
117 else {
118 motion = equirect_motion_wrap(motion);
119 }
120
121 return motion;
122 }
123
130 float3 P_prv,
131 float3 P,
132 float3 P_nxt) const
133 {
134 if (!is_panoramic(camera_curr.type)) {
135 return surface_velocity(P_prv, P, P_nxt);
136 }
137 float4x4 face_mat = view.viewmat * camera_curr.viewinv;
138 float4x4 viewmat_prev = face_mat * camera_prev.viewmat;
139 float4x4 viewmat_next = face_mat * camera_next.viewmat;
140
141 float2 curr_ndc = project_point(view.winmat, transform_point(view.viewmat, P)).xy;
142 float2 prev_ndc = project_point(view.winmat, transform_point(viewmat_prev, P_prv)).xy;
143 float2 next_ndc = project_point(view.winmat, transform_point(viewmat_next, P_nxt)).xy;
144
145 const float max_valid_ndc = 5.0f;
146 if (any(isnan(prev_ndc)) || any(greaterThan(abs(prev_ndc), float2(max_valid_ndc)))) {
147 prev_ndc = curr_ndc;
148 }
149 if (any(isnan(next_ndc)) || any(greaterThan(abs(next_ndc), float2(max_valid_ndc)))) {
150 next_ndc = curr_ndc;
151 }
152 return float4(prev_ndc - curr_ndc, curr_ndc - next_ndc) * 0.5f;
153 }
154
155 /* World-space view direction. Must use `view`, which is this cubeface for panoramic camera. */
157 {
158 /* Use view vector to avoid imprecision if camera is far from origin. */
159 float3 vV = -view.view_incident_vector(view.point_screen_to_view(float3(uv, 1.0f)));
160 return transform_direction(view.viewinv, vV);
161 }
162
168 float4 resolve_local(const ViewMatrices &view, float4 vector_local, float2 uv, float depth) const
169 {
170 if (vector_local.x != VELOCITY_INVALID) {
171 return velocity::unpack(vector_local);
172 }
173 if (depth == 1.0f) {
175 if (!is_panoramic(camera_curr.type)) {
176 /* NOTE: We use CameraData matrices instead of drw_view().winmat to avoid adding the TAA
177 * jitter to the velocity. */
178 float2 prev_ndc =
180 float2 curr_ndc =
182 float2 next_ndc =
184 /* NOTE: We output both vectors in the same direction so we can reuse the same vector
185 * with RGRG swizzle in viewport. */
186 float4 motion = float4(prev_ndc - curr_ndc, curr_ndc - next_ndc);
187 /* Convert NDC velocity to UV velocity. */
188 return motion * 0.5f;
189 }
190 float2 curr_ndc = uv * 2.0f - 1.0f;
191 float4x4 face_mat = view.viewmat * camera_curr.viewinv;
192 float4x4 viewmat_prev = face_mat * camera_prev.viewmat;
193 float4x4 viewmat_next = face_mat * camera_next.viewmat;
194 float2 prev_ndc = project_point(view.winmat, transform_direction(viewmat_prev, V)).xy;
195 float2 next_ndc = project_point(view.winmat, transform_direction(viewmat_next, V)).xy;
196
197 const float max_valid_ndc = 5.0f;
198 if (any(isnan(prev_ndc)) || any(greaterThan(abs(prev_ndc), float2(max_valid_ndc)))) {
199 prev_ndc = curr_ndc;
200 }
201 if (any(isnan(next_ndc)) || any(greaterThan(abs(next_ndc), float2(max_valid_ndc)))) {
202 next_ndc = curr_ndc;
203 }
204 return float4(prev_ndc - curr_ndc, curr_ndc - next_ndc) * 0.5f;
205 }
206
207 float3 P = view.point_screen_to_world(float3(uv, depth));
208 return surface_velocity_local(view, P, P, P);
209 }
210
216 float4 resolve_local(const draw::View &views, sampler2D vector_tx, int2 texel, float depth) const
217 {
218 const ViewMatrices view = views.get(0);
219 float2 uv = (float2(texel) + 0.5f) / float2(textureSize(vector_tx, 0).xy);
220 float4 vector = texelFetch(vector_tx, texel, 0);
221 return resolve_local(view, vector, uv, depth);
222 }
223
226 float4 local_motion,
227 float2 uv,
228 float depth) const
229 {
230 if (!is_panoramic(camera_curr.type)) {
231 return local_motion;
232 }
233 /* Reconstruct world position from depth and reproject through each camera directly.
234 * This cubeface's frustum only reaches 90 degrees. */
235 float3 P = view.point_screen_to_world(float3(uv, depth));
236
237 float4 camera_only_motion = surface_velocity_local(view, P, P, P);
238 float4 object_motion_delta = (local_motion - camera_only_motion) * 2.0f;
239 float2 curr_ndc = uv * 2.0f - 1.0f;
240 float3 P_prv = view.point_screen_to_world(
241 float3((curr_ndc + object_motion_delta.xy) * 0.5f + 0.5f, depth));
242 float3 P_nxt = view.point_screen_to_world(
243 float3((curr_ndc - object_motion_delta.zw) * 0.5f + 0.5f, depth));
244
245 float3 curr_dir = normalize(transform_point(camera_curr.viewmat, P));
246 float3 prev_dir = normalize(transform_point(camera_prev.viewmat, P_prv));
247 float3 next_dir = normalize(transform_point(camera_next.viewmat, P_nxt));
251
252 if (!panoramic_validate_uvs(curr_dir, curr_uv, prev_dir, prev_uv, next_dir, next_uv)) {
253 return float4(0.0f);
254 }
255 return equirect_motion_wrap(float4(prev_uv - curr_uv, curr_uv - next_uv));
256 }
257
261 bool panoramic_uv_is_valid(CameraData cam, float3 dir, float2 uv) const
262 {
263 if (cam.type != CAMERA_PANO_FISHEYE_LENS_POLYNOMIAL) {
264 return true;
265 }
266 /* Fisheye polynomial fits a wiggly curve that can't be solved directly, so we can only
267 * approximate it with Newton's method. In certain cases, the numbers are way off, so need
268 * to check if uv actually maps back to dir. */
269 return dot(eevee::camera::view_from_uv(cam, uv), dir) > 0.99999f;
270 }
271
275 float2 curr_uv,
276 float3 prev_dir,
277 float2 prev_uv,
278 float3 next_dir,
279 float2 next_uv) const
280 {
281 const float max_valid_uv = 5.0f;
282 if (any(isnan(curr_uv)) || any(greaterThan(abs(curr_uv), float2(max_valid_uv))) ||
283 any(isnan(prev_uv)) || any(greaterThan(abs(prev_uv), float2(max_valid_uv))) ||
284 any(isnan(next_uv)) || any(greaterThan(abs(next_uv), float2(max_valid_uv))) ||
285 !panoramic_uv_is_valid(camera_curr, curr_dir, curr_uv) ||
286 !panoramic_uv_is_valid(camera_prev, prev_dir, prev_uv) ||
287 !panoramic_uv_is_valid(camera_next, next_dir, next_uv))
288 {
289 return false;
290 }
291 return true;
292 }
293
296 sampler2D vector_tx,
297 int2 texel,
298 float depth) const
299 {
300 const ViewMatrices view = views.get(0);
301 float2 uv = (float2(texel) + 0.5f) / float2(textureSize(vector_tx, 0).xy);
302 float4 vector = texelFetch(vector_tx, texel, 0);
303 if (vector.x == VELOCITY_INVALID && depth == 1.0f) {
304 /* Resolve background directly in panoramic space. */
306 float3 prev_dir = transform_direction(camera_prev.viewmat, V);
307 float3 curr_dir = transform_direction(camera_curr.viewmat, V);
308 float3 next_dir = transform_direction(camera_next.viewmat, V);
312 if (!panoramic_validate_uvs(curr_dir, curr_uv, prev_dir, prev_uv, next_dir, next_uv)) {
313 return float4(0.0f);
314 }
315 return equirect_motion_wrap(float4(prev_uv - curr_uv, curr_uv - next_uv));
316 }
317 float4 local_motion = resolve_local(view, vector, uv, depth);
318 return local_to_panoramic(view, local_motion, uv, depth);
319 }
320};
321
322/* -------------------------------------------------------------------- */
323
324/* Pass world space deltas to the fragment shader.
325 * This is to make sure that the resulting motion vectors are valid even with displacement.
326 * WARNING: The next value is invalid when rendering the viewport. */
331
332/* Surface Velocity
333 *
334 * Combined with the depth pre-pass shader.
335 * Outputs the view motion vectors for animated objects. */
338
344 read)]] const VelocityIndex (&velocity_indirection_buf)[];
345
351 float3 lP, int vert_id, float3 &lP_prev, float3 &lP_next, uint resource_id) const
352 {
353 VelocityIndex vel = velocity_indirection_buf[resource_id];
354 lP_next = lP_prev = lP;
355 if (vel.geo.do_deform) {
356 if (vel.geo.ofs[STEP_PREVIOUS] != -1) {
357 lP_prev = velocity_geo_prev_buf[vel.geo.ofs[STEP_PREVIOUS] + vert_id].xyz;
358 }
359 if (vel.geo.ofs[STEP_NEXT] != -1) {
360 lP_next = velocity_geo_next_buf[vel.geo.ofs[STEP_NEXT] + vert_id].xyz;
361 }
362 }
363 }
364
371 float3 lP,
372 float3 lP_next,
373 float3 &motion_prev,
374 float3 &motion_next,
375 uint resource_id,
376 float4x4 model_mat) const
377 {
378 VelocityIndex vel = velocity_indirection_buf[resource_id];
379 float4x4 obmat_prev = velocity_obj_prev_buf[vel.obj.ofs[STEP_PREVIOUS]];
380 float4x4 obmat_next = velocity_obj_next_buf[vel.obj.ofs[STEP_NEXT]];
381 float3 P_prev = transform_point(obmat_prev, lP_prev);
382 float3 P_next = transform_point(obmat_next, lP_next);
383 float3 P = transform_point(model_mat, lP);
384 motion_prev = P_prev - P;
385 motion_next = P_next - P;
386 }
387};
388
389} // namespace eevee
390
#define M_TAU
#define VERTEX_COPY_GROUP_SIZE
#define VELOCITY_INDIRECTION_BUF_SLOT
#define VELOCITY_CAMERA_NEXT_BUF
#define VELOCITY_CAMERA_PREV_BUF
#define VELOCITY_OBJ_PREV_BUF_SLOT
#define VELOCITY_GEO_PREV_BUF_SLOT
#define VELOCITY_GEO_NEXT_BUF_SLOT
#define VELOCITY_CAMERA_CURR_BUF
#define VELOCITY_OBJ_NEXT_BUF_SLOT
PipelineCompute eevee_vertex_copy(eevee::velocity::vertex_copy)
#define VELOCITY_INVALID
#define uniform(slot)
#define storage(slot, qualifiers)
#define smooth
#define push_constant
#define resource_table
#define global_invocation_id
#define vertex_id
#define compute
#define num_work_groups
#define local_size(...)
#define isnan
#define round
bool any(VecOp< bool, D >) RET
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 abs
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
int divide_ceil(int a, int b)
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
float2 project_point(float3x3 mat, float2 point)
MatBase< float, 4, 4 > float4x4
float3 view_from_uv(float4x4 projmat, float2 uv)
float2 uv_from_view(float4x4 projmat, bool is_persp, float3 vV)
float4 unpack(float4 data)
float4 pack(float4 data)
void vertex_copy(VertexCopy &srt, const uint3 global_id, const uint3 groups_count)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
float3 point_screen_to_world(float3 ssP) const
float3 view_incident_vector(float3 vP) const
float3 point_screen_to_view(float3 ssP) const
ViewMatrices get(uint view_id) const
bool panoramic_validate_uvs(float3 curr_dir, float2 curr_uv, float3 prev_dir, float2 prev_uv, float3 next_dir, float2 next_uv) const
float4 resolve_local(const draw::View &views, sampler2D vector_tx, int2 texel, float depth) const
float4 surface_velocity(float3 P_prv, float3 P, float3 P_nxt) const
float4 surface_velocity_local(const ViewMatrices &view, float3 P_prv, float3 P, float3 P_nxt) const
float4 equirect_motion_wrap(float4 motion) const
bool panoramic_uv_is_valid(CameraData cam, float3 dir, float2 uv) const
float4 local_to_panoramic(const ViewMatrices &view, float4 local_motion, float2 uv, float depth) const
const CameraData & camera_curr
float4 resolve_local(const ViewMatrices &view, float4 vector_local, float2 uv, float depth) const
float4 resolve_panoramic(const draw::View &views, sampler2D vector_tx, int2 texel, float depth) const
const CameraData & camera_prev
float3 background_world_direction(const ViewMatrices &view, float2 uv) const
const CameraData & camera_next
const VelocityIndex(& velocity_indirection_buf)[]
void local_position_deltas(float3 lP, int vert_id, float3 &lP_prev, float3 &lP_next, uint resource_id) const
const float4(& velocity_geo_next_buf)[]
const float4(& velocity_geo_prev_buf)[]
const float4x4(& velocity_obj_next_buf)[]
void vertex_velocity(float3 lP_prev, float3 lP, float3 lP_next, float3 &motion_prev, float3 &motion_next, uint resource_id, float4x4 model_mat) const
const float4x4(& velocity_obj_prev_buf)[]
float x
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V