23 int closest_index = 0;
24 float closest_squared_distance = 1.0f;
28 if (squared_dist < closest_squared_distance) {
29 closest_squared_distance = squared_dist;
34 const float2 closest_sample = samples[closest_index];
46 if (closest_index != 0) {
47 std::swap(samples[0], samples[closest_index]);
51 for (
int i = 0; i < samples.
size() - 2; i++) {
52 float squared_dist = 0.0;
54 for (
int j = i + 1; j < samples.
size(); j++) {
56 if (_squared_dist > squared_dist) {
57 squared_dist = _squared_dist;
61 std::swap(samples[i + 1], samples[
index]);
66 std::array<float2, 5>
x5;
67 std::array<float2, 8>
x8;
68 std::array<float2, 11>
x11;
69 std::array<float2, 16>
x16;
70 std::array<float2, 32>
x32;
139 overlay_depth_ps_.init();
147 taa_accumulation_tx_.free();
148 sample0_depth_tx_.free();
154 smaa_mix_factor_ = 1.0f -
clamp_f(scene_state.
sample / 4.0f, 0.0f, 1.0f);
156 taa_accumulation_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_16_16_16_16,
159 sample0_depth_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
163 taa_accumulation_ps_.init();
164 taa_accumulation_ps_.state_set(scene_state.
sample == 0 ?
168 taa_accumulation_ps_.bind_texture(
"color_buffer", &resources.
color_tx);
169 taa_accumulation_ps_.push_constant(
"samplesWeights", weights_, 9);
172 smaa_edge_detect_ps_.init();
175 smaa_edge_detect_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
176 smaa_edge_detect_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
177 smaa_edge_detect_ps_.clear_color(
float4(0.0f));
180 smaa_aa_weight_ps_.init();
183 smaa_aa_weight_ps_.bind_texture(
"edges_tx", &smaa_edge_tx_);
184 smaa_aa_weight_ps_.bind_texture(
"area_tx", smaa_area_tx_);
185 smaa_aa_weight_ps_.bind_texture(
"search_tx", smaa_search_tx_);
186 smaa_aa_weight_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
187 smaa_aa_weight_ps_.clear_color(
float4(0.0f));
190 smaa_resolve_ps_.init();
193 smaa_resolve_ps_.bind_texture(
"blend_tx", &smaa_weight_tx_);
194 smaa_resolve_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
195 smaa_resolve_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
196 smaa_resolve_ps_.push_constant(
"mix_factor", &smaa_mix_factor_, 1);
197 smaa_resolve_ps_.push_constant(
"taa_accumulated_weight", &weight_accum_, 1);
198 smaa_resolve_ps_.clear_color(
float4(0.0f));
210 view.
sync(viewmat, winmat);
220 sample_offset = taa_samples.
x5[scene_state.
sample];
223 sample_offset = taa_samples.
x8[scene_state.
sample];
226 sample_offset = taa_samples.
x11[scene_state.
sample];
229 sample_offset = taa_samples.
x16[scene_state.
sample];
232 sample_offset = taa_samples.
x32[scene_state.
sample];
243 view.
sync(viewmat, winmat);
255 overlay_depth_fb_.bind();
256 manager.
submit(overlay_depth_ps_);
266 if (scene_state.
sample == 0) {
269 sample0_depth_in_front_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
275 sample0_depth_in_front_tx_.free();
281 if (sample0_depth_in_front_tx_.is_valid()) {
294 if (scene_state.
sample == 0) {
299 taa_accumulation_fb_.bind();
300 manager.
submit(taa_accumulation_ps_, view);
301 weight_accum_ += weights_sum_;
305 smaa_weight_tx_.acquire_2d(scene_state.
resolution,
306 gpu::TextureFormat::UNORM_8_8_8_8,
308 smaa_edge_tx_.acquire_2d(scene_state.
resolution,
309 gpu::TextureFormat::UNORM_8_8,
314 if (smaa_mix_factor_ > 0.0f) {
316 smaa_edge_fb_.bind();
317 manager.
submit(smaa_edge_detect_ps_, view);
320 smaa_weight_fb_.bind();
321 manager.
submit(smaa_aa_weight_ps_, view);
324 smaa_resolve_fb_.bind();
325 manager.
submit(smaa_resolve_ps_, view);
328 smaa_edge_tx_.release();
329 smaa_weight_tx_.release();