Blender V5.3
eevee_hiz_update.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
19
20#pragma once
21
22#include "eevee_defines.hh"
26
27namespace eevee::hiz {
28
29struct Update {
31
33
37
38 [[storage(0, read_write)]] uint &finished_tile_counter;
39
45 [[image(5, read_write, SFLOAT_32)]] image2D out_mip_5;
47
48 [[shared]] float local_depths[HIZ_GROUP_SIZE][HIZ_GROUP_SIZE];
49
50 /* Load values from the previous LOD level. */
52 {
53 pixel *= 2;
54 return float4(local_depths[pixel.y + 1][pixel.x + 0],
55 local_depths[pixel.y + 1][pixel.x + 1],
56 local_depths[pixel.y + 0][pixel.x + 1],
57 local_depths[pixel.y + 0][pixel.x + 0]);
58 }
59
60 void store_local_depth(int2 pixel, float depth)
61 {
62 local_depths[pixel.y][pixel.x] = depth;
63 }
64};
65
66[[compute, local_size(HIZ_GROUP_SIZE, HIZ_GROUP_SIZE)]]
68 [[local_invocation_id]] const uint3 local_id,
69 [[work_group_id]] const uint3 group_id)
70{
71 int2 local_px = int2(local_id.xy);
72 /* Bottom left corner of the kernel. */
73 int2 kernel_origin = int2(HIZ_GROUP_SIZE * group_id.xy);
74
75 /* Copy level 0. */
76 int2 src_px = int2(kernel_origin + local_px) * 2;
77 float4 samp;
78
79 if (srt.use_layer) [[static_branch]] {
80 float2 samp_co = float2(src_px + 1) / float2(textureSize(srt.depth_layered_tx, 0).xy);
81 samp = textureGather(srt.depth_layered_tx, float3(samp_co, float(srt.layer_id)));
82 }
83 else {
84 float2 samp_co = float2(src_px + 1) / float2(textureSize(srt.depth_tx, 0));
85 samp = textureGather(srt.depth_tx, samp_co);
86 }
87
88 samp = reverse_z::read(samp);
89
90 if (srt.update_mip_0) {
91 imageStoreFast(srt.out_mip_0, src_px + int2(0, 1), samp.xxxx);
92 imageStoreFast(srt.out_mip_0, src_px + int2(1, 1), samp.yyyy);
93 imageStoreFast(srt.out_mip_0, src_px + int2(1, 0), samp.zzzz);
94 imageStoreFast(srt.out_mip_0, src_px + int2(0, 0), samp.wwww);
95 }
96
97 /* Level 1. (No load) */
98 float max_depth = reduce_max(samp);
99 int2 dst_px = int2(kernel_origin + local_px);
100 imageStoreFast(srt.out_mip_1, dst_px, float4(max_depth));
101 srt.store_local_depth(local_px, max_depth);
102
103 /* Level 2. */
104 {
105 barrier(); /* Wait for previous writes to finish. */
106 bool active_thread = all(lessThan(uint2(local_px), uint2(HIZ_GROUP_SIZE) >> uint(1)));
107 if (active_thread) {
108 max_depth = reduce_max(srt.load_local_depths(local_px));
109 dst_px = int2((kernel_origin >> 1) + local_px);
110 imageStoreFast(srt.out_mip_2, dst_px, float4(max_depth));
111 }
112 barrier(); /* Wait for previous reads to finish. */
113 if (active_thread) {
114 srt.store_local_depth(local_px, max_depth);
115 }
116 }
117 /* Level 3. */
118 {
119 barrier(); /* Wait for previous writes to finish. */
120 bool active_thread = all(lessThan(uint2(local_px), uint2(HIZ_GROUP_SIZE) >> uint(2)));
121 if (active_thread) {
122 max_depth = reduce_max(srt.load_local_depths(local_px));
123 dst_px = int2((kernel_origin >> 2) + local_px);
124 imageStoreFast(srt.out_mip_3, dst_px, float4(max_depth));
125 }
126 barrier(); /* Wait for previous reads to finish. */
127 if (active_thread) {
128 srt.store_local_depth(local_px, max_depth);
129 }
130 }
131 /* Level 4. */
132 {
133 barrier(); /* Wait for previous writes to finish. */
134 bool active_thread = all(lessThan(uint2(local_px), uint2(HIZ_GROUP_SIZE) >> uint(3)));
135 if (active_thread) {
136 max_depth = reduce_max(srt.load_local_depths(local_px));
137 dst_px = int2((kernel_origin >> 3) + local_px);
138 imageStoreFast(srt.out_mip_4, dst_px, float4(max_depth));
139 }
140 barrier(); /* Wait for previous reads to finish. */
141 if (active_thread) {
142 srt.store_local_depth(local_px, max_depth);
143 }
144 }
145 /* Level 5. */
146 {
147 barrier(); /* Wait for previous writes to finish. */
148 bool active_thread = all(lessThan(uint2(local_px), uint2(HIZ_GROUP_SIZE) >> uint(4)));
149 if (active_thread) {
150 max_depth = reduce_max(srt.load_local_depths(local_px));
151 dst_px = int2((kernel_origin >> 4) + local_px);
152 imageStoreFast(srt.out_mip_5, dst_px, float4(max_depth));
153 }
154 barrier(); /* Wait for previous reads to finish. */
155 if (active_thread) {
156 srt.store_local_depth(local_px, max_depth);
157 }
158 }
159
160 /* Since we pad the destination texture, the mip size is equal to the dispatch size. */
161 uint2 mip5_size = uint2(imageSize(srt.out_mip_5).xy);
162 uint tile_count = mip5_size.x * mip5_size.y;
163 /* Let the last tile handle the remaining LOD. */
164 bool last_tile = atomicAdd(srt.finished_tile_counter, 1u) + 1u < tile_count;
165 if (last_tile == false) {
166 return;
167 }
168 /* Reset for next dispatch. */
169 srt.finished_tile_counter = 0u;
170
171 int2 iter = divide_ceil(imageSize(srt.out_mip_5), int2(HIZ_GROUP_SIZE * 2u));
172 int2 image_border = imageSize(srt.out_mip_5) - 1;
173 for (int y = 0; y < iter.y; y++) {
174 for (int x = 0; x < iter.x; x++) {
175 /* Load result of the other work groups. */
176 kernel_origin = int2(HIZ_GROUP_SIZE) * int2(x, y);
177 src_px = int2(kernel_origin + local_px) * 2;
178 float4 samp;
179 samp.x = imageLoadFast(srt.out_mip_5, min(src_px + int2(0, 1), image_border)).x;
180 samp.y = imageLoadFast(srt.out_mip_5, min(src_px + int2(1, 1), image_border)).x;
181 samp.z = imageLoadFast(srt.out_mip_5, min(src_px + int2(1, 0), image_border)).x;
182 samp.w = imageLoadFast(srt.out_mip_5, min(src_px + int2(0, 0), image_border)).x;
183 /* Level 6. */
184 float max_depth = reduce_max(samp);
185 int2 dst_px = int2(kernel_origin + local_px);
186 imageStoreFast(srt.out_mip_6, dst_px, float4(max_depth));
187 srt.store_local_depth(local_px, max_depth);
188
189 /* Level 7 requires barriers inside a non-uniform control flow. */
190 // downsample_level(out_mip_7, 1);
191
192 /* Limited by OpenGL maximum of 8 image slot. */
193 // downsample_level(out_mip_8, 2);
194 // downsample_level(out_mip_9, 3);
195 // downsample_level(out_mip_10, 4);
196 }
197 }
198}
199
200} // namespace eevee::hiz
201
202#ifndef GLSL_CPP_STUBS
205 eevee::hiz::Update{.use_layer = true});
206#endif
PipelineCompute eevee_hiz_update_layer(eevee::hiz::update_main, eevee::hiz::Update{.use_layer=true})
PipelineCompute eevee_hiz_update(eevee::hiz::update_main, eevee::hiz::Update{.use_layer=false})
#define shared
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define push_constant
#define compilation_constant
#define resource_table
#define sampler(slot)
#define static_branch
#define specialization_constant(default_value)
#define local_invocation_id
#define compute
#define condition(cond)
#define local_size(...)
int atomicAdd(int &, int) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageLoadFast
#define imageStoreFast
SamplerBase< float, 2, false, true, false, true > sampler2DArrayDepth
TEX_TEMPLATE DataVec textureGather(T, FltCoord, int=0) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
TEX_TEMPLATE SizeVec textureSize(T, int) RET
int divide_ceil(int a, int b)
float reduce_max(float2 a)
void update_main(Update &srt, const uint3 local_id, const uint3 group_id)
T read(T depth_buffer_value)
#define min(a, b)
Definition sort.cc:38
void store_local_depth(int2 pixel, float depth)
float local_depths[HIZ_GROUP_SIZE][HIZ_GROUP_SIZE]
sampler2DArrayDepth depth_layered_tx
float4 load_local_depths(int2 pixel)
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13