Blender V5.3
eevee_occupancy_convert.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "eevee_defines.hh"
11
13
20
21bool is_front_face_hit(float stored_hit_depth)
22{
23 return stored_hit_depth < 0.0f;
24}
25
26[[vertex]] void convert_vert([[vertex_id]] const int vert_id, [[position]] float4 &out_position)
27{
28 fullscreen_vertex(vert_id, out_position);
29}
30
36 [[resource_table]] const Uniform &uni,
37 [[frag_coord]] const float4 frag_co)
38{
39 float hit_depths[VOLUME_HIT_DEPTH_MAX];
40 float hit_ordered[VOLUME_HIT_DEPTH_MAX + 1];
41 int hit_index[VOLUME_HIT_DEPTH_MAX];
42
43 int2 texel = int2(frag_co.xy);
44
45 int hit_count = int(imageLoad(srt.hit_count_img, texel).x);
46 hit_count = min(hit_count, VOLUME_HIT_DEPTH_MAX);
47
48 if (hit_count == 0) {
49 return;
50 }
51
52 /* Clear the texture for next layer / frame. */
53 imageStore(srt.hit_count_img, texel, uint4(0));
54
55 for (int i = 0; i < hit_count; i++) {
56 hit_depths[i] = imageLoad(srt.hit_depth_img, int3(texel, i)).r;
57 }
58
59 for (int i = 0; i < hit_count; i++) {
60 hit_index[i] = 0;
61 for (int j = 0; j < hit_count; j++) {
62 hit_index[i] += int(abs(hit_depths[i]) > abs(hit_depths[j]));
63 }
64 }
65
66 for (int i = 0; i < hit_count; i++) {
67 hit_ordered[hit_index[i]] = hit_depths[i];
68 }
69
70#if 0 /* Debug. Need to adjust the qualifier of the texture adjusted. */
71 for (int i = 0; i < hit_count; i++) {
72 imageStore(hit_depth_img, int3(texel, i), float4(hit_ordered[i]));
73 }
74#endif
75
76 /* Convert to occupancy bits. */
78 /* True if last interface was a volume entry. */
79 /* Initialized to front facing if first hit is a backface to support camera inside the volume. */
80 bool last_frontfacing = !is_front_face_hit(hit_ordered[0]);
81 /* Add artificial back-facing hit to close volumes we entered but never exited.
82 * Fixes issues with non-manifold meshes or things like water planes. */
83 hit_ordered[hit_count] = -1.0f;
84 /* Bit index of the last interface. */
85 int last_bit = 0;
86 for (int i = 0; i <= hit_count; i++) {
87 bool frontfacing = is_front_face_hit(hit_ordered[i]);
88 if (last_frontfacing == frontfacing) {
89 /* Same facing, do not treat as a volume interface. */
90 continue;
91 }
92 last_frontfacing = frontfacing;
93
94 int occupancy_bit_n = ::occupancy::bit_index_from_depth(abs(hit_ordered[i]),
95 uni.uniform_buf.volumes.tex_size.z);
96 if (last_bit == occupancy_bit_n) {
97 /* We did not cross a new voxel center. Do nothing. */
98 continue;
99 }
100 int bit_start = last_bit;
101 int bit_count = occupancy_bit_n - last_bit;
102 last_bit = occupancy_bit_n;
103
104 if (last_frontfacing == false) {
105 /* occupancy::Bits is cleared by default. No need to do anything for empty regions. */
106 continue;
107 }
108 occupancy = ::occupancy::set_bits_high(occupancy, bit_start, bit_count);
109 }
110
111 /* Write the occupancy bits */
112 for (int i = 0; i < imageSize(srt.occupancy_img).z; i++) {
113 if (occupancy.bits[i] != 0u) {
114 /* NOTE: Doesn't have to be atomic but we need to blend with other method. */
115 imageAtomicOr(srt.occupancy_img, int3(texel, i), occupancy.bits[i]);
116 }
117 }
118}
119
120} // namespace eevee::volume::occupancy
121
#define VOLUME_HIT_DEPTH_SLOT
#define VOLUME_HIT_COUNT_SLOT
#define VOLUME_OCCUPANCY_SLOT
PipelineGraphic eevee_volume_occupancy_convert(eevee::volume::occupancy::convert_vert, eevee::volume::occupancy::convert_frag)
#define texture_atomic
#define position
#define image(slot, qualifiers, format)
#define frag_coord
#define resource_table
#define fragment
#define vertex_id
#define vertex
#define abs
ImageBase< uint, 2 > uimage2D
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
IMG_TEMPLATE uint imageAtomicOr(T &, IntCoord, uint) RET
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
void fullscreen_vertex(int vertex_id, float4 &out_position)
void convert_vert(const int vert_id, float4 &out_position)
void convert_frag(Convert &srt, const Uniform &uni, const float4 frag_co)
bool is_front_face_hit(float stored_hit_depth)
Bits set_bits_high(Bits occupancy, int bit_start, int bit_count)
int bit_index_from_depth(float depth, int bit_count)
#define min(a, b)
Definition sort.cc:38