19 int closest_index = 0;
20 float closest_squared_distance = 1.0f;
24 if (squared_dist < closest_squared_distance) {
25 closest_squared_distance = squared_dist;
30 const float2 closest_sample = samples[closest_index];
42 if (closest_index != 0) {
43 std::swap(samples[0], samples[closest_index]);
47 for (
int i = 0;
i < samples.
size() - 2;
i++) {
48 float squared_dist = 0.0;
50 for (
int j =
i + 1; j < samples.
size(); j++) {
52 if (_squared_dist > squared_dist) {
53 squared_dist = _squared_dist;
57 std::swap(samples[
i + 1], samples[index]);
62 std::array<float2, 5>
x5;
63 std::array<float2, 8>
x8;
64 std::array<float2, 11>
x11;
65 std::array<float2, 16>
x16;
66 std::array<float2, 32>
x32;
135 overlay_depth_ps_.init();
143 taa_accumulation_tx_.free();
144 sample0_depth_tx_.free();
150 smaa_mix_factor_ = 1.0f -
clamp_f(scene_state.
sample / 4.0f, 0.0f, 1.0f);
152 taa_accumulation_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_16_16_16_16,
155 sample0_depth_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
159 taa_accumulation_ps_.init();
160 taa_accumulation_ps_.state_set(scene_state.
sample == 0 ?
164 taa_accumulation_ps_.bind_texture(
"color_buffer", &resources.
color_tx);
165 taa_accumulation_ps_.push_constant(
"samplesWeights", weights_, 9);
168 smaa_edge_detect_ps_.init();
171 smaa_edge_detect_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
172 smaa_edge_detect_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
173 smaa_edge_detect_ps_.clear_color(
float4(0.0f));
176 smaa_aa_weight_ps_.init();
179 smaa_aa_weight_ps_.bind_texture(
"edges_tx", &smaa_edge_tx_);
180 smaa_aa_weight_ps_.bind_texture(
"area_tx", smaa_area_tx_);
181 smaa_aa_weight_ps_.bind_texture(
"search_tx", smaa_search_tx_);
182 smaa_aa_weight_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
183 smaa_aa_weight_ps_.clear_color(
float4(0.0f));
186 smaa_resolve_ps_.init();
189 smaa_resolve_ps_.bind_texture(
"blend_tx", &smaa_weight_tx_);
190 smaa_resolve_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
191 smaa_resolve_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
192 smaa_resolve_ps_.push_constant(
"mix_factor", &smaa_mix_factor_, 1);
193 smaa_resolve_ps_.push_constant(
"taa_accumulated_weight", &weight_accum_, 1);
194 smaa_resolve_ps_.clear_color(
float4(0.0f));
206 view.sync(viewmat, winmat);
216 sample_offset = taa_samples.
x5[scene_state.
sample];
219 sample_offset = taa_samples.
x8[scene_state.
sample];
222 sample_offset = taa_samples.
x11[scene_state.
sample];
225 sample_offset = taa_samples.
x16[scene_state.
sample];
228 sample_offset = taa_samples.
x32[scene_state.
sample];
239 view.sync(viewmat, winmat);
251 overlay_depth_fb_.bind();
252 manager.
submit(overlay_depth_ps_);
262 if (scene_state.
sample == 0) {
265 sample0_depth_in_front_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
271 sample0_depth_in_front_tx_.free();
277 if (sample0_depth_in_front_tx_.is_valid()) {
290 if (scene_state.
sample == 0) {
295 taa_accumulation_fb_.bind();
297 weight_accum_ += weights_sum_;
301 smaa_weight_tx_.acquire(scene_state.
resolution,
302 gpu::TextureFormat::UNORM_8_8_8_8,
305 gpu::TextureFormat::UNORM_8_8,
310 if (smaa_mix_factor_ > 0.0f) {
312 smaa_edge_fb_.bind();
316 smaa_weight_fb_.bind();
320 smaa_resolve_fb_.bind();
324 smaa_edge_tx_.release();
325 smaa_weight_tx_.release();