Commit 92ac42fa by Alexis Hetu Committed by Alexis Hétu

Texture load functionality for integer types

- Added cases required to load integer type textures into swiftshader's internal formats. - Cleaned up copy functions that simply perform a memcpy for a given number of bytes per row. - Removed unused functionality for mini floats. Right now, all we need is the ability to import these formats, so there's currently no need to keep the export code. Change-Id: I61500c41e668f885d3ec1e687b6f888c117221f2 Reviewed-on: https://swiftshader-review.googlesource.com/4140Tested-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent d3a2d3d7
...@@ -346,211 +346,28 @@ namespace sw ...@@ -346,211 +346,28 @@ namespace sw
class RGB9E5Data class RGB9E5Data
{ {
union unsigned int R : 9;
{ unsigned int G : 9;
struct unsigned int B : 9;
{ unsigned int E : 5;
unsigned int R : 9;
unsigned int G : 9;
unsigned int B : 9;
unsigned int E : 5;
};
unsigned int uint;
};
// Exponent Bias
static const int Bias = 15;
// Number of mantissa bits per component
static const int MantissaBits = 9;
public: public:
RGB9E5Data(float red, float green, float blue) void toRGBFloats(float* rgb) const
{
// Maximum allowed biased exponent value
static const int MaxExponent = 31;
static const float MaxValue = ((pow(2.0f, MantissaBits) - 1) / pow(2.0f, MantissaBits)) * pow(2.0f, MaxExponent - Bias);
const float red_c = sw::max(0.0f, sw::min(MaxValue, red));
const float green_c = sw::max(0.0f, sw::min(MaxValue, green));
const float blue_c = sw::max(0.0f, sw::min(MaxValue, blue));
const float max_c = sw::max(sw::max(red_c, green_c), blue_c);
const float exp_p = sw::max(-Bias - 1.0f, floorf(log(max_c))) + 1.0f + Bias;
const int max_s = static_cast<int>(floor((max_c / (pow(2.0f, exp_p - Bias - MantissaBits))) + 0.5f));
const int exp_s = static_cast<int>((max_s < pow(2.0f, MantissaBits)) ? exp_p : exp_p + 1);
R = static_cast<unsigned int>(floor((red_c / (pow(2.0f, exp_s - Bias - MantissaBits))) + 0.5f));
G = static_cast<unsigned int>(floor((green_c / (pow(2.0f, exp_s - Bias - MantissaBits))) + 0.5f));
B = static_cast<unsigned int>(floor((blue_c / (pow(2.0f, exp_s - Bias - MantissaBits))) + 0.5f));
E = exp_s;
}
void toRGBFloats(float *red, float *green, float *blue) const
{ {
*red = R * pow(2.0f, (int)E - Bias - MantissaBits); static const float Offset = -24.0f; // Exponent Bias (15) + Number of mantissa bits per component (9) = 24
*green = G * pow(2.0f, (int)E - Bias - MantissaBits);
*blue = B * pow(2.0f, (int)E - Bias - MantissaBits);
}
unsigned int toUInt() const const float factor = powf(2.0f, static_cast<float>(E) + Offset);
{ rgb[0] = static_cast<float>(R) * factor;
return uint; rgb[1] = static_cast<float>(G) * factor;
rgb[2] = static_cast<float>(B) * factor;
} }
}; };
class R11G11B10FData class R11G11B10FData
{ {
union unsigned int R : 11;
{ unsigned int G : 11;
struct unsigned int B : 10;
{
unsigned int R : 11;
unsigned int G : 11;
unsigned int B : 10;
};
unsigned int uint;
};
static inline unsigned short float32ToFloat11(float fp32)
{
const unsigned int float32MantissaMask = 0x7FFFFF;
const unsigned int float32ExponentMask = 0x7F800000;
const unsigned int float32SignMask = 0x80000000;
const unsigned int float32ValueMask = ~float32SignMask;
const unsigned int float32ExponentFirstBit = 23;
const unsigned int float32ExponentBias = 127;
const unsigned short float11Max = 0x7BF;
const unsigned short float11MantissaMask = 0x3F;
const unsigned short float11ExponentMask = 0x7C0;
const unsigned short float11BitMask = 0x7FF;
const unsigned int float11ExponentBias = 14;
const unsigned int float32Maxfloat11 = 0x477E0000;
const unsigned int float32Minfloat11 = 0x38800000;
const unsigned int float32Bits = *(unsigned int*)(&fp32);
const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
unsigned int float32Val = float32Bits & float32ValueMask;
if((float32Val & float32ExponentMask) == float32ExponentMask)
{
// INF or NAN
if((float32Val & float32MantissaMask) != 0)
{
return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask);
}
else if(float32Sign)
{
// -INF is clamped to 0 since float11 is positive only
return 0;
}
else
{
return float11ExponentMask;
}
}
else if(float32Sign)
{
// float11 is positive only, so clamp to zero
return 0;
}
else if(float32Val > float32Maxfloat11)
{
// The number is too large to be represented as a float11, set to max
return float11Max;
}
else
{
if(float32Val < float32Minfloat11)
{
// The number is too small to be represented as a normalized float11
// Convert it to a denormalized value.
const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit);
float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
}
else
{
// Rebias the exponent to represent the value as a normalized float11
float32Val += 0xC8000000;
}
return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
}
}
static inline unsigned short float32ToFloat10(float fp32)
{
const unsigned int float32MantissaMask = 0x7FFFFF;
const unsigned int float32ExponentMask = 0x7F800000;
const unsigned int float32SignMask = 0x80000000;
const unsigned int float32ValueMask = ~float32SignMask;
const unsigned int float32ExponentFirstBit = 23;
const unsigned int float32ExponentBias = 127;
const unsigned short float10Max = 0x3DF;
const unsigned short float10MantissaMask = 0x1F;
const unsigned short float10ExponentMask = 0x3E0;
const unsigned short float10BitMask = 0x3FF;
const unsigned int float10ExponentBias = 14;
const unsigned int float32Maxfloat10 = 0x477C0000;
const unsigned int float32Minfloat10 = 0x38800000;
const unsigned int float32Bits = *(unsigned int*)(&fp32);
const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
unsigned int float32Val = float32Bits & float32ValueMask;
if((float32Val & float32ExponentMask) == float32ExponentMask)
{
// INF or NAN
if((float32Val & float32MantissaMask) != 0)
{
return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask);
}
else if(float32Sign)
{
// -INF is clamped to 0 since float11 is positive only
return 0;
}
else
{
return float10ExponentMask;
}
}
else if(float32Sign)
{
// float10 is positive only, so clamp to zero
return 0;
}
else if(float32Val > float32Maxfloat10)
{
// The number is too large to be represented as a float11, set to max
return float10Max;
}
else
{
if(float32Val < float32Minfloat10)
{
// The number is too small to be represented as a normalized float11
// Convert it to a denormalized value.
const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit);
float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
}
else
{
// Rebias the exponent to represent the value as a normalized float11
float32Val += 0xC8000000;
}
return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
}
}
static inline float float11ToFloat32(unsigned short fp11) static inline float float11ToFloat32(unsigned short fp11)
{ {
...@@ -635,23 +452,11 @@ namespace sw ...@@ -635,23 +452,11 @@ namespace sw
} }
public: public:
R11G11B10FData(float r, float g, float b) void toRGBFloats(float* rgb) const
{
R = float32ToFloat11(r);
G = float32ToFloat11(g);
B = float32ToFloat10(b);
}
void toRGBFloats(float *red, float *green, float *blue) const
{
*red = float11ToFloat32(R);
*green = float11ToFloat32(G);
*blue = float10ToFloat32(B);
}
unsigned int toUInt() const
{ {
return uint; rgb[0] = float11ToFloat32(R);
rgb[1] = float11ToFloat32(G);
rgb[2] = float10ToFloat32(B);
} }
}; };
} }
......
...@@ -171,6 +171,7 @@ protected: ...@@ -171,6 +171,7 @@ protected:
virtual ~Image(); virtual ~Image();
void loadD24S8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer); void loadD24S8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer);
void loadD32FS8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer);
}; };
#ifdef __ANDROID__ #ifdef __ANDROID__
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment