Permalink
Browse files

make float printing match exactly between mac and windows (code taken…

… from glsl-optimizer)
  • Loading branch information...
1 parent 8cb66a2 commit dee0f8dab53dc25b2b9f7841ec9013c1ce9b4fea @aras-p committed Jan 31, 2013
@@ -12,6 +12,48 @@
#define snprintf _snprintf
#endif
+
+void print_float (std::stringstream& out, float f)
+{
+ // Kind of roundabout way, but this is to satisfy two things:
+ // * MSVC and gcc-based compilers differ a bit in how they treat float
+ // width/precision specifiers. Want to match for tests.
+ // * GLSL (early version at least) require floats to have ".0" or
+ // exponential notation.
+ char tmp[64];
+ sprintf(tmp, "%.6g", f);
+
+ char* posE = NULL;
+ posE = strchr(tmp, 'e');
+ if (!posE)
+ posE = strchr(tmp, 'E');
+
+ #if _MSC_VER
+ // While gcc would print something like 1.0e+07, MSVC will print 1.0e+007 -
+ // only for exponential notation, it seems, will add one extra useless zero. Let's try to remove
+ // that so compiler output matches.
+ if (posE != NULL)
+ {
+ if((posE[1] == '+' || posE[1] == '-') && posE[2] == '0')
+ {
+ char* p = posE+2;
+ while (p[0])
+ {
+ p[0] = p[1];
+ ++p;
+ }
+ }
+ }
+ #endif
+
+ out << tmp;
+
+ // need to append ".0"?
+ if (!strchr(tmp,'.') && (posE == NULL))
+ out << ".0";
+}
+
+
bool isShadowSampler(TBasicType t) {
switch (t) {
case EbtSampler1DShadow:
@@ -98,7 +140,7 @@ void writeConstantConstructor( std::stringstream& out, EGlslSymbolType t, TPreci
out << c->toInt(v);
break;
case EbtFloat:
- GlslSymbol::writeFloat(out, c->toFloat(v));
+ print_float(out, c->toFloat(v));
break;
default:
assert(0);
@@ -84,28 +84,6 @@ void GlslSymbol::writeDecl (std::stringstream& out, unsigned flags)
}
-// two requirements here:
-// 1) We always need a decimal-point so that the GLSL output is clearly a float
-// 2) We want to have reasonably consistent behaviour between MacOS & Windows (so tests are consistent)
-// historically there was this pattern instead
-// active.setf ( std::stringstream::showpoint );
-// active.unsetf(std::ios::fixed);
-// active.unsetf(std::ios::scientific);
-// active.precision (6);
-// but the interpretation of precision was different between platforms
-
-void GlslSymbol::writeFloat(std::stringstream &out, float f)
-{
- static char buffer[64];
-
- if (fractionalPart(f) == 0.f)
- sprintf(buffer, "%.1f", f);
- else
- sprintf(buffer, "%.*g", FLT_DIG, f);
-
- out << buffer;
-}
-
void GlslSymbol::mangleName()
{
std::stringstream s;
@@ -9,7 +9,6 @@
#include "glslCommon.h"
#include "glslStruct.h"
-
class GlslSymbol
{
public:
@@ -55,8 +54,6 @@ class GlslSymbol
void releaseRef() { assert (refCount >= 0 ); if ( refCount > 0 ) refCount--; }
int getRef() const { return refCount; }
- static void writeFloat(std::stringstream &out, float f);
-
private:
std::string name;
std::string mangledName;
@@ -80,7 +80,7 @@ float Linear01Depth( in float z ) {
#line 8
float DecodeFloatRGBA( in vec4 enc ) {
#line 10
- vec4 kDecodeDot = vec4( 1.0, (1.0 / 255.0), (1.0 / 65025.0), (1.0 / 160581376.0));
+ vec4 kDecodeDot = vec4( 1.0, (1.0 / 255.0), (1.0 / 65025.0), (1.0 / 1.60581e+08));
return dot( enc, kDecodeDot);
}
#line 39
@@ -80,7 +80,7 @@ highp float Linear01Depth( in highp float z ) {
#line 8
highp float DecodeFloatRGBA( in highp vec4 enc ) {
#line 10
- highp vec4 kDecodeDot = vec4( 1.0, (1.0 / 255.0), (1.0 / 65025.0), (1.0 / 160581376.0));
+ highp vec4 kDecodeDot = vec4( 1.0, (1.0 / 255.0), (1.0 / 65025.0), (1.0 / 1.60581e+08));
return dot( enc, kDecodeDot);
}
#line 39

0 comments on commit dee0f8d

Please sign in to comment.