Commit 2d4bf0c
authored
Make hierarchical Z buffer generation properly conservative. (#22603)
The single-pass downsampling (SPD) shader is properly conservative only
for depth buffers with size lengths that are powers of two. This is
because it assumes that, for any texel in mip level N+1, all texels in
mip level N that contribute to that texel are contained within at most a
2×2 square, which is only true for textures that have side lengths that
have powers of two. (For textures that have side lengths that aren't
powers of two, proper conservative downsampling may require sampling up
to a 3×3 square.)
This PR solves the problem in a conservative way, by conceptually
rounding up the side lengths of the depth buffer to the *next* power of
two and scaling the depth buffer appropriately before performing
downsampling. This ensures that the SPD shader only sees textures with
side lengths that are powers of two at every step of the operation. Note
"conceptually"; in reality this patch doesn't actually generate such an
intermediate scaled texture. Instead, it changes the `load_mip_0`
function in the shader to return the value that *would* have been
produced by sampling such a scaled depth buffer. This is obviously more
efficient than actually performing such a scaling operation.
The sampling operations in the mesh preprocessing occlusion culling code
required no changes, as they simply use `textureDimensions` on the
hierarchical Z buffer to determine its size. I did, however, have to
change the meshlet code to use `textureDimensions` like the mesh
preprocessing code does. The meshlet culling indeed seems less broken
now (albeit still broken); the rabbits on the right side don't flicker
anymore in my testing.
Note that this approach, while popular (e.g. in zeux's [Niagara]), is
more conservative than a single-pass downsampler that properly handles
3×3 texel blocks would be. However, such a downsampler would be complex,
and I figured it was better to make our occlusion culling correct,
simple, and fast rather than possibly-complex and slow.
This fix allows us to move occlusion culling out of experimental status.
I opted not to do that in this PR in order to make it easier to review,
but a follow-up PR should do that.
[Niagara]:
zeux/niagara#15 (comment)1 parent 791a0e4 commit 2d4bf0c
File tree
3 files changed
+106
-17
lines changed- crates
- bevy_core_pipeline/src/mip_generation/experimental
- bevy_pbr/src/meshlet
3 files changed
+106
-17
lines changedLines changed: 33 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
| 3 | + | |
| 4 | + | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
515 | 514 | | |
516 | 515 | | |
517 | 516 | | |
518 | | - | |
| 517 | + | |
| 518 | + | |
519 | 519 | | |
520 | | - | |
521 | | - | |
| 520 | + | |
| 521 | + | |
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
| |||
616 | 616 | | |
617 | 617 | | |
618 | 618 | | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
619 | 635 | | |
620 | 636 | | |
621 | 637 | | |
| |||
625 | 641 | | |
626 | 642 | | |
627 | 643 | | |
628 | | - | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
629 | 649 | | |
630 | 650 | | |
631 | 651 | | |
| |||
712 | 732 | | |
713 | 733 | | |
714 | 734 | | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
Lines changed: 72 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
307 | 310 | | |
308 | 311 | | |
309 | 312 | | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
310 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
311 | 328 | | |
312 | | - | |
313 | | - | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
314 | 332 | | |
315 | 333 | | |
316 | | - | |
317 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
318 | 337 | | |
319 | 338 | | |
320 | 339 | | |
321 | 340 | | |
322 | 341 | | |
323 | | - | |
| 342 | + | |
| 343 | + | |
324 | 344 | | |
325 | 345 | | |
326 | | - | |
| 346 | + | |
327 | 347 | | |
328 | 348 | | |
329 | 349 | | |
330 | | - | |
| 350 | + | |
331 | 351 | | |
332 | 352 | | |
333 | 353 | | |
334 | 354 | | |
335 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
336 | 393 | | |
337 | 394 | | |
338 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
147 | 146 | | |
148 | | - | |
| 147 | + | |
149 | 148 | | |
150 | 149 | | |
151 | 150 | | |
| |||
157 | 156 | | |
158 | 157 | | |
159 | 158 | | |
160 | | - | |
161 | 159 | | |
162 | 160 | | |
163 | 161 | | |
| |||
0 commit comments