Skip to content

Commit

Permalink
Align default precision across shading languages (AcademySoftwareFoun…
Browse files Browse the repository at this point in the history
…dation#1538)

This changelist removes explicit overrides of floating-point precision in shader generation, making the default precision 6 decimal places across shading languages, and allowing the client to adjust the precision of generated shaders via Value::setFloatPrecision.

This addresses a slight visual regression in generated OSL (with a maximum visual error on the order of 1e-3), caused by a hard-coding of floating-point precision to 3 decimal places in OslSyntax.
  • Loading branch information
jstone-lucasfilm authored Sep 22, 2023
1 parent a5a74fe commit 6c48683
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 9 deletions.
5 changes: 4 additions & 1 deletion source/MaterialXCore/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ ScopedFloatFormatting::ScopedFloatFormatting(Value::FloatFormat format, int prec
_precision(Value::getFloatPrecision())
{
Value::setFloatFormat(format);
Value::setFloatPrecision(precision);
if (precision >= 0)
{
Value::setFloatPrecision(precision);
}
}

ScopedFloatFormatting::~ScopedFloatFormatting()
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXCore/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ template <class T> class MX_CORE_API TypedValue : public Value
class MX_CORE_API ScopedFloatFormatting
{
public:
explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = 6);
explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = -1);
~ScopedFloatFormatting();

private:
Expand Down
4 changes: 1 addition & 3 deletions source/MaterialXGenGlsl/GlslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,7 @@ ShaderPtr GlslShaderGenerator::generate(const string& name, ElementPtr element,
{
ShaderPtr shader = createShader(name, element, context);

// Turn on fixed float formatting to make sure float values are
// emitted with a decimal point and not as integers, and to avoid
// any scientific notation which isn't supported by all OpenGL targets.
// Request fixed floating-point notation for consistency across targets.
ScopedFloatFormatting fmt(Value::FloatFormatFixed);

// Make sure we initialize/reset the binding context before generation.
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXGenMdl/MdlShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G

ShaderPtr shader = createShader(name, element, context);

// Request fixed floating-point notation for consistency across targets.
ScopedFloatFormatting fmt(Value::FloatFormatFixed);

ShaderGraph& graph = shader->getGraph();
ShaderStage& stage = shader->getStage(Stage::PIXEL);

Expand Down
4 changes: 1 addition & 3 deletions source/MaterialXGenMsl/MslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ ShaderPtr MslShaderGenerator::generate(const string& name, ElementPtr element, G
{
ShaderPtr shader = createShader(name, element, context);

// Turn on fixed float formatting to make sure float values are
// emitted with a decimal point and not as integers, and to avoid
// any scientific notation which isn't supported by all OpenGL targets.
// Request fixed floating-point notation for consistency across targets.
ScopedFloatFormatting fmt(Value::FloatFormatFixed);

// Make sure we initialize/reset the binding context before generation.
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXGenOsl/OslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ ShaderPtr OslShaderGenerator::generate(const string& name, ElementPtr element, G
{
ShaderPtr shader = createShader(name, element, context);

// Request fixed floating-point notation for consistency across targets.
ScopedFloatFormatting fmt(Value::FloatFormatFixed);

ShaderGraph& graph = shader->getGraph();
ShaderStage& stage = shader->getStage(Stage::PIXEL);

Expand Down
1 change: 0 additions & 1 deletion source/MaterialXGenOsl/OslSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ class OSLMatrix3TypeSyntax : public AggregateTypeSyntax

string getValue(const Value& value, bool uniform) const override
{
ScopedFloatFormatting fmt(Value::FloatFormatFixed, 3);
StringVec values = splitString(value.getValueString(), ",");
return getValue(values, uniform);
}
Expand Down

0 comments on commit 6c48683

Please sign in to comment.