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;
132 overlay_depth_ps_.init();
140 taa_accumulation_tx_.free();
141 sample0_depth_tx_.free();
147 smaa_mix_factor_ = 1.0f -
clamp_f(scene_state.
sample / 4.0f, 0.0f, 1.0f);
156 taa_accumulation_ps_.init();
157 taa_accumulation_ps_.state_set(scene_state.
sample == 0 ?
161 taa_accumulation_ps_.bind_texture(
"color_buffer", &resources.
color_tx);
162 taa_accumulation_ps_.push_constant(
"samplesWeights", weights_, 9);
165 smaa_edge_detect_ps_.init();
168 smaa_edge_detect_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
169 smaa_edge_detect_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
170 smaa_edge_detect_ps_.clear_color(
float4(0.0f));
173 smaa_aa_weight_ps_.init();
176 smaa_aa_weight_ps_.bind_texture(
"edges_tx", &smaa_edge_tx_);
177 smaa_aa_weight_ps_.bind_texture(
"area_tx", smaa_area_tx_);
178 smaa_aa_weight_ps_.bind_texture(
"search_tx", smaa_search_tx_);
179 smaa_aa_weight_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
180 smaa_aa_weight_ps_.clear_color(
float4(0.0f));
183 smaa_resolve_ps_.init();
186 smaa_resolve_ps_.bind_texture(
"blend_tx", &smaa_weight_tx_);
187 smaa_resolve_ps_.bind_texture(
"color_tx", &taa_accumulation_tx_);
188 smaa_resolve_ps_.push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
189 smaa_resolve_ps_.push_constant(
"mix_factor", &smaa_mix_factor_, 1);
190 smaa_resolve_ps_.push_constant(
"taa_accumulated_weight", &weight_accum_, 1);
191 smaa_resolve_ps_.clear_color(
float4(0.0f));
203 view.sync(viewmat, winmat);
213 sample_offset = taa_samples.
x5[scene_state.
sample];
216 sample_offset = taa_samples.
x8[scene_state.
sample];
219 sample_offset = taa_samples.
x11[scene_state.
sample];
222 sample_offset = taa_samples.
x16[scene_state.
sample];
225 sample_offset = taa_samples.
x32[scene_state.
sample];
236 view.sync(viewmat, winmat);
244 GPUTexture *depth_in_front_tx)
248 overlay_depth_fb_.bind();
249 manager.
submit(overlay_depth_ps_);
259 if (scene_state.
sample == 0) {
262 sample0_depth_in_front_tx_.ensure_2d(
267 sample0_depth_in_front_tx_.free();
273 if (sample0_depth_in_front_tx_.is_valid()) {
286 if (scene_state.
sample == 0) {
291 taa_accumulation_fb_.bind();
293 weight_accum_ += weights_sum_;
297 smaa_weight_tx_.acquire(scene_state.
resolution,
306 if (smaa_mix_factor_ > 0.0f) {
308 smaa_edge_fb_.bind();
312 smaa_weight_fb_.bind();
316 smaa_resolve_fb_.bind();
320 smaa_edge_tx_.release();
321 smaa_weight_tx_.release();