Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize float literals in output #2230

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.o
*.d
*.txt
/build
/test
/spirv-cross
/obj
Expand Down
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is one needed for fp16, or will that hit the format_float part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently float16_t will be converted to float before be converted to string:

SPIRV-Cross/spirv_glsl.cpp

Lines 5910 to 5913 in 42aac91

string CompilerGLSL::convert_half_to_string(const SPIRConstant &c, uint32_t col, uint32_t row)
{
string res;
float float_value = c.scalar_f16(col, row);

I think format_float16 is not needed.

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.
HansKristian-Work marked this conversation as resolved.
Show resolved Hide resolved
void set_float_formatter(FloatFormatter* formatter)
HansKristian-Work marked this conversation as resolved.
Show resolved Hide resolved
{
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;
HansKristian-Work marked this conversation as resolved.
Show resolved Hide resolved
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