[Base] Float clamping cleanup

This commit is contained in:
Triang3l 2021-06-05 14:55:55 +03:00
parent 89a7c8f41f
commit 3c20a83972
6 changed files with 26 additions and 15 deletions

View File

@ -851,7 +851,7 @@ bool XmaContext::ConvertFrame(const uint8_t** samples, int num_channels,
auto in = reinterpret_cast<const float*>(samples[j]); auto in = reinterpret_cast<const float*>(samples[j]);
// Raw samples sometimes aren't within [-1, 1] // Raw samples sometimes aren't within [-1, 1]
float scaled_sample = xe::saturate(in[i]) * scale; float scaled_sample = xe::saturate_signed(in[i]) * scale;
// Convert the sample and output it in big endian. // Convert the sample and output it in big endian.
auto sample = static_cast<int16_t>(scaled_sample); auto sample = static_cast<int16_t>(scaled_sample);

View File

@ -57,8 +57,16 @@ constexpr T round_up(T value, V multiple) {
return value ? (((value + multiple - 1) / multiple) * multiple) : multiple; return value ? (((value + multiple - 1) / multiple) * multiple) : multiple;
} }
constexpr float saturate(float value) { // Using the same conventions as in shading languages, returning 0 for NaN.
return std::max(std::min(1.0f, value), -1.0f); // std::max is `a < b ? b : a`, thus in case of NaN, the first argument is
// always returned. Also -0 is not < +0, so +0 is also chosen for it.
template <typename T>
constexpr T saturate_unsigned(T value) {
return std::min(static_cast<T>(1.0f), std::max(static_cast<T>(0.0f), value));
}
template <typename T>
constexpr T saturate_signed(T value) {
return std::min(static_cast<T>(1.0f), std::max(static_cast<T>(-1.0f), value));
} }
// Gets the next power of two value that is greater than or equal to the given // Gets the next power of two value that is greater than or equal to the given

View File

@ -9,6 +9,8 @@
#include "xenia/gpu/d3d12/d3d12_graphics_system.h" #include "xenia/gpu/d3d12/d3d12_graphics_system.h"
#include <algorithm>
#include "xenia/base/logging.h" #include "xenia/base/logging.h"
#include "xenia/base/math.h" #include "xenia/base/math.h"
#include "xenia/gpu/d3d12/d3d12_command_processor.h" #include "xenia/gpu/d3d12/d3d12_command_processor.h"

View File

@ -409,12 +409,13 @@ void GetHostViewportInfo(const RegisterFile& regs, uint32_t resolution_scale,
float axis_0 = offset_axis - scale_axis_abs; float axis_0 = offset_axis - scale_axis_abs;
float axis_1 = offset_axis + scale_axis_abs; float axis_1 = offset_axis + scale_axis_abs;
float axis_max_unscaled_float = float(xy_max_unscaled[i]); float axis_max_unscaled_float = float(xy_max_unscaled[i]);
// fmax to drop NaN and < 0, min as float (axis_max_unscaled_float is well // max(0.0f, xy) drops NaN and < 0 - max picks the first argument in the
// below 2^24) to safely drop very large values. // !(a < b) case (always for NaN), min as float (axis_max_unscaled_float
// is well below 2^24) to safely drop very large values.
uint32_t axis_0_int = uint32_t axis_0_int =
uint32_t(std::min(std::fmax(axis_0, 0.0f), axis_max_unscaled_float)); uint32_t(std::min(axis_max_unscaled_float, std::max(0.0f, axis_0)));
uint32_t axis_1_int = uint32_t axis_1_int =
uint32_t(std::min(std::fmax(axis_1, 0.0f), axis_max_unscaled_float)); uint32_t(std::min(axis_max_unscaled_float, std::max(0.0f, axis_1)));
uint32_t axis_extent_int = axis_1_int - axis_0_int; uint32_t axis_extent_int = axis_1_int - axis_0_int;
viewport_info_out.xy_offset[i] = axis_0_int * resolution_scale; viewport_info_out.xy_offset[i] = axis_0_int * resolution_scale;
viewport_info_out.xy_extent[i] = axis_extent_int * resolution_scale; viewport_info_out.xy_extent[i] = axis_extent_int * resolution_scale;
@ -517,9 +518,8 @@ void GetHostViewportInfo(const RegisterFile& regs, uint32_t resolution_scale,
// extension. But cases when this really matters are yet to be found - // extension. But cases when this really matters are yet to be found -
// trying to fix this will result in more correct depth values, but // trying to fix this will result in more correct depth values, but
// incorrect clipping. // incorrect clipping.
z_min = std::min(std::fmax(host_clip_offset_z, 0.0f), 1.0f); z_min = xe::saturate_unsigned(host_clip_offset_z);
z_max = std::min(std::fmax(host_clip_offset_z + host_clip_scale_z, 0.0f), z_max = xe::saturate_unsigned(host_clip_offset_z + host_clip_scale_z);
1.0f);
// Direct3D 12 doesn't allow reverse depth range - on some drivers it // Direct3D 12 doesn't allow reverse depth range - on some drivers it
// works, on some drivers it doesn't, actually, but it was never // works, on some drivers it doesn't, actually, but it was never
// explicitly allowed by the specification. // explicitly allowed by the specification.

View File

@ -585,10 +585,11 @@ bool RenderTargetCache::Update(bool is_rasterization_done,
// Using floor, or, rather, truncation (because maxing with zero anyway) // Using floor, or, rather, truncation (because maxing with zero anyway)
// similar to how viewport scissoring behaves on real AMD, Intel and Nvidia // similar to how viewport scissoring behaves on real AMD, Intel and Nvidia
// GPUs on Direct3D 12, also like in draw_util::GetHostViewportInfo. // GPUs on Direct3D 12, also like in draw_util::GetHostViewportInfo.
// fmax to drop NaN and < 0, min as float (height_used is well below 2^24) // max(0.0f, viewport_bottom) to drop NaN and < 0 - max picks the first
// to safely drop very large values. // argument in the !(a < b) case (always for NaN), min as float (height_used
height_used = uint32_t( // is well below 2^24) to safely drop very large values.
std::min(std::fmax(viewport_bottom, 0.0f), float(height_used))); height_used =
uint32_t(std::min(float(height_used), std::max(0.0f, viewport_bottom)));
} }
int32_t scissor_bottom = int32_t scissor_bottom =
int32_t(regs.Get<reg::PA_SC_WINDOW_SCISSOR_BR>().br_y); int32_t(regs.Get<reg::PA_SC_WINDOW_SCISSOR_BR>().br_y);

View File

@ -1031,7 +1031,7 @@ void ProgressBar(float frac, float width, float height = 0,
if (height == 0) { if (height == 0) {
height = ImGui::GetTextLineHeightWithSpacing(); height = ImGui::GetTextLineHeightWithSpacing();
} }
frac = xe::saturate(frac); frac = xe::saturate_unsigned(frac);
const auto fontAtlas = ImGui::GetIO().Fonts; const auto fontAtlas = ImGui::GetIO().Fonts;