Skip to content

Commit

Permalink
Allow to customize float literals in output
Browse files Browse the repository at this point in the history
  • Loading branch information
chirsz-ever committed Nov 22, 2023
1 parent ec7a23c commit 20dd53b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
20 changes: 14 additions & 6 deletions spirv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ inline std::string convert_to_string(double t, char locale_radix_point)
return buf;
}

#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif

class FloatFormatter
{
public:
virtual ~FloatFormatter() = default;
virtual std::string format_float(float value) = 0;
virtual std::string format_double(double value) = 0;
};

template <typename T>
struct ValueSaver
{
Expand All @@ -318,12 +332,6 @@ struct ValueSaver
T saved;
};

#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif

struct Instruction
{
uint16_t op = 0;
Expand Down
25 changes: 22 additions & 3 deletions spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5936,7 +5936,7 @@ string CompilerGLSL::convert_half_to_string(const SPIRConstant &c, uint32_t col,
type.basetype = SPIRType::Half;
type.vecsize = 1;
type.columns = 1;
res = join(type_to_glsl(type), "(", convert_to_string(float_value, current_locale_radix_character), ")");
res = join(type_to_glsl(type), "(", format_float(float_value), ")");
}

return res;
Expand Down Expand Up @@ -6004,7 +6004,7 @@ string CompilerGLSL::convert_float_to_string(const SPIRConstant &c, uint32_t col
}
else
{
res = convert_to_string(float_value, current_locale_radix_character);
res = format_float(float_value);
if (backend.float_literal_suffix)
res += "f";
}
Expand Down Expand Up @@ -6087,7 +6087,7 @@ std::string CompilerGLSL::convert_double_to_string(const SPIRConstant &c, uint32
}
else
{
res = convert_to_string(double_value, current_locale_radix_character);
res = format_double(double_value);
if (backend.double_literal_suffix)
res += "lf";
}
Expand Down Expand Up @@ -18643,3 +18643,22 @@ uint32_t CompilerGLSL::type_to_location_count(const SPIRType &type) const

return count;
}

std::string CompilerGLSL::format_float(float value) const
{
if (float_formatter)
return float_formatter->format_float(value);

// default behavior
return convert_to_string(value, current_locale_radix_character);
}

std::string CompilerGLSL::format_double(double value) const
{
if (float_formatter)
return float_formatter->format_double(value);

// default behavior
return convert_to_string(value, current_locale_radix_character);
}

12 changes: 12 additions & 0 deletions spirv_glsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ class CompilerGLSL : public Compiler
void mask_stage_output_by_location(uint32_t location, uint32_t component);
void mask_stage_output_by_builtin(spv::BuiltIn builtin);

// Allow to control how to format float literals in the output.
// Set to "nullptr" to use the default "convert_to_string" function.
// It needs to ensure that the pointer is valid before the compiler object destructs.
void set_float_formatter(FloatFormatter* formatter)
{
float_formatter = formatter;
}

protected:
struct ShaderSubgroupSupportHelper
{
Expand Down Expand Up @@ -1031,6 +1039,10 @@ class CompilerGLSL : public Compiler
std::unordered_set<LocationComponentPair, InternalHasher> masked_output_locations;
std::unordered_set<uint32_t> masked_output_builtins;

FloatFormatter* float_formatter = nullptr;
std::string format_float(float value) const;
std::string format_double(double value) const;

private:
void init();

Expand Down
3 changes: 1 addition & 2 deletions spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,8 +1274,7 @@ void CompilerMSL::emit_entry_point_declarations()
args.push_back(join("max_anisotropy(", s.max_anisotropy, ")"));
if (s.lod_clamp_enable)
{
args.push_back(join("lod_clamp(", convert_to_string(s.lod_clamp_min, current_locale_radix_character), ", ",
convert_to_string(s.lod_clamp_max, current_locale_radix_character), ")"));
args.push_back(join("lod_clamp(", format_float(s.lod_clamp_min), ", ", format_float(s.lod_clamp_max), ")"));
}

// If we would emit no arguments, then omit the parentheses entirely. Otherwise,
Expand Down

0 comments on commit 20dd53b

Please sign in to comment.