Commit a683328c by Ben Clayton

Reactor: Change the signature of PrintValue::Ty

So that the print value's 'fmt' is a dynamic function instead of a constexpr. Let's us do smarter stuff based on the type and value, and will allow us to move functionality from overloads on rr::PrintValue to PrintValue::Ty template specializations. Also fix a bunch of missing integer expansion casts which were missing from ca8e3d7c. Bug: b/140287657 Change-Id: I7e18c35ae23d8a2dd6b726562ae1348a1b7075dd Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/32049Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com> Tested-by: 's avatarBen Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
parent 51f08312
...@@ -94,4 +94,39 @@ namespace sw ...@@ -94,4 +94,39 @@ namespace sw
UInt4 halfToFloatBits(UInt4 halfBits); UInt4 halfToFloatBits(UInt4 halfBits);
} }
#ifdef ENABLE_RR_PRINT
namespace rr {
template <> struct PrintValue::Ty<sw::Vector4f>
{
static std::string fmt(const sw::Vector4f& v)
{
return "[x: " + PrintValue::fmt(v.x) + ","
" y: " + PrintValue::fmt(v.y) + ","
" z: " + PrintValue::fmt(v.z) + ","
" w: " + PrintValue::fmt(v.w) + "]";
}
static std::vector<rr::Value*> val(const sw::Vector4f& v)
{
return PrintValue::vals(v.x, v.y, v.z, v.w);
}
};
template <> struct PrintValue::Ty<sw::Vector4s>
{
static std::string fmt(const sw::Vector4s& v)
{
return "[x: " + PrintValue::fmt(v.x) + ","
" y: " + PrintValue::fmt(v.y) + ","
" z: " + PrintValue::fmt(v.z) + ","
" w: " + PrintValue::fmt(v.w) + "]";
}
static std::vector<rr::Value*> val(const sw::Vector4s& v)
{
return PrintValue::vals(v.x, v.y, v.z, v.w);
}
};
}
#endif // ENABLE_RR_PRINT
#endif // sw_ShaderCore_hpp #endif // sw_ShaderCore_hpp
...@@ -4525,6 +4525,7 @@ namespace rr ...@@ -4525,6 +4525,7 @@ namespace rr
return elements; return elements;
} }
std::vector<Value*> PrintValue::Ty<Byte>::val(const RValue<Byte>& v) { return toInt({v.value}, false); }
std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); } std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); }
std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); } std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); }
std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); } std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); }
...@@ -4532,7 +4533,9 @@ namespace rr ...@@ -4532,7 +4533,9 @@ namespace rr
std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); } std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); }
std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); } std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); }
std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); } std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); }
std::vector<Value*> PrintValue::Ty<Short>::val(const RValue<Short>& v) { return toInt({v.value}, true); }
std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); } std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); }
std::vector<Value*> PrintValue::Ty<UShort>::val(const RValue<UShort>& v) { return toInt({v.value}, false); }
std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); } std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); }
std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); } std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); }
std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); } std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); }
......
...@@ -3295,29 +3295,16 @@ namespace rr ...@@ -3295,29 +3295,16 @@ namespace rr
{ {
// Ty is a template that can be specialized for printing type T. // Ty is a template that can be specialized for printing type T.
// Each specialization must expose: // Each specialization must expose:
// * A 'static constexpr const char* fmt' field that provides the // * A 'static std::string fmt(const T& v)' method that provides the
// printf format specifier. // printf format specifier.
// * A 'static std::vector<rr::Value*> val(const T& v)' method that // * A 'static std::vector<rr::Value*> val(const T& v)' method that
// returns all the printf format values. // returns all the printf format values.
template <typename T> struct Ty template <typename T> struct Ty
{ {
// static constexpr const char* fmt; // static std::string fmt(const T& v);
// static std::vector<rr::Value*> val(const T& v) // static std::vector<rr::Value*> val(const T& v);
}; };
// returns the printf value(s) for the given LValue.
template <typename T>
static std::vector<Value*> val(const LValue<T>& v) { return val(RValue<T>(v.loadValue())); }
// returns the printf value(s) for the given RValue.
template <typename T>
static std::vector<Value*> val(const RValue<T>& v) { return Ty<T>::val(v); }
// returns the printf value from for the given type with a
// PrintValue::Ty<T> specialization.
template <typename T>
static std::vector<Value*> val(const T& v) { return Ty<T>::val(v); }
// returns the printf values for all the values in the given array. // returns the printf values for all the values in the given array.
template <typename T> template <typename T>
static std::vector<Value*> val(const T* list, int count) { static std::vector<Value*> val(const T* list, int count) {
...@@ -3331,15 +3318,16 @@ namespace rr ...@@ -3331,15 +3318,16 @@ namespace rr
return values; return values;
} }
// fmt returns a comma-delimited list of the string el repeated count // fmt returns the comma-delimited list of printf format strings for
// times enclosed in square brackets. // every element in the provided list, all enclosed in square brackets.
static std::string fmt(const char* el, int count) template <typename T>
static std::string fmt(const T* list, int count)
{ {
std::string out = "["; std::string out = "[";
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
if (i > 0) { out += ", "; } if (i > 0) { out += ", "; }
out += el; out += fmt(list[i]);
} }
return out + "]"; return out + "]";
} }
...@@ -3357,16 +3345,16 @@ namespace rr ...@@ -3357,16 +3345,16 @@ namespace rr
// Constructs a PrintValue for the given value. // Constructs a PrintValue for the given value.
template <typename T> template <typename T>
PrintValue(const T& v) : format(Ty<T>::fmt), values(val(v)) {} PrintValue(const T& v) : format(fmt(v)), values(val(v)) {}
// Constructs a PrintValue for the given static array. // Constructs a PrintValue for the given static array.
template <typename T, int N> template <typename T, int N>
PrintValue(const T (&v)[N]) : format(fmt(Ty<T>::fmt, N)), values(val(&v[0], N)) {} PrintValue(const T (&v)[N]) : format(fmt(&v[0], N)), values(val(&v[0], N)) {}
// Constructs a PrintValue for the given array starting at arr of length // Constructs a PrintValue for the given array starting at arr of length
// len. // len.
template <typename T> template <typename T>
PrintValue(const T* arr, int len) : format(fmt(Ty<T>::fmt, len)), values(val(arr, len)) {} PrintValue(const T* arr, int len) : format(fmt(arr, len)), values(val(arr, len)) {}
// PrintValue constructors for plain-old-data values. // PrintValue constructors for plain-old-data values.
PrintValue(bool v) : format(v ? "true" : "false") {} PrintValue(bool v) : format(v ? "true" : "false") {}
...@@ -3397,11 +3385,13 @@ namespace rr ...@@ -3397,11 +3385,13 @@ namespace rr
// { // {
// template <> struct PrintValue::Ty<Mat4x4> // template <> struct PrintValue::Ty<Mat4x4>
// { // {
// static constexpr const char* fmt = // static std::string fmt(const Mat4x4& v)
// "[a: <%f, %f, %f, %f>," // {
// " b: <%f, %f, %f, %f>," // return "[a: <%f, %f, %f, %f>,"
// " c: <%f, %f, %f, %f>," // " b: <%f, %f, %f, %f>,"
// " d: <%f, %f, %f, %f>]"; // " c: <%f, %f, %f, %f>,"
// " d: <%f, %f, %f, %f>]";
// }
// static std::vector<rr::Value*> val(const Mat4x4& v) // static std::vector<rr::Value*> val(const Mat4x4& v)
// { // {
// return PrintValue::vals(v.a, v.b, v.c, v.d); // return PrintValue::vals(v.a, v.b, v.c, v.d);
...@@ -3419,114 +3409,124 @@ namespace rr ...@@ -3419,114 +3409,124 @@ namespace rr
} }
return joined; return joined;
} }
// returns the printf format specifier for the given type via the
// PrintValue::Ty<T> specialization.
template <typename T>
static std::string fmt(const T& v) { return Ty<T>::fmt(v); }
// returns the printf value for the given type with a
// PrintValue::Ty<T> specialization.
template <typename T>
static std::vector<Value*> val(const T& v) { return Ty<T>::val(v); }
}; };
// PrintValue::Ty<T> specializations for basic types. // PrintValue::Ty<T> specializations for basic types.
template <> struct PrintValue::Ty<const char*> template <> struct PrintValue::Ty<const char*>
{ {
static constexpr const char* fmt = "%s"; static std::string fmt(const char* v) { return "%s"; }
static std::vector<Value*> val(const char* v); static std::vector<Value*> val(const char* v);
}; };
template <> struct PrintValue::Ty<std::string> template <> struct PrintValue::Ty<std::string>
{ {
static constexpr const char* fmt = PrintValue::Ty<const char*>::fmt; static std::string fmt(const std::string& v) { return PrintValue::Ty<const char*>::fmt(v.c_str()); }
static std::vector<Value*> val(const std::string& v) { return PrintValue::Ty<const char*>::val(v.c_str()); } static std::vector<Value*> val(const std::string& v) { return PrintValue::Ty<const char*>::val(v.c_str()); }
}; };
// PrintValue::Ty<T> specializations for standard Reactor types. // PrintValue::Ty<T> specializations for standard Reactor types.
template <> struct PrintValue::Ty<Bool> template <> struct PrintValue::Ty<Bool>
{ {
static constexpr const char* fmt = "%d"; static std::string fmt(const RValue<Bool>& v) { return "%d"; }
static std::vector<Value*> val(const RValue<Bool>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<Bool>& v) { return {v.value}; }
}; };
template <> struct PrintValue::Ty<Byte> template <> struct PrintValue::Ty<Byte>
{ {
static constexpr const char* fmt = "%d"; static std::string fmt(const RValue<Byte>& v) { return "%d"; }
static std::vector<Value*> val(const RValue<Byte>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<Byte>& v);
}; };
template <> struct PrintValue::Ty<Byte4> template <> struct PrintValue::Ty<Byte4>
{ {
static constexpr const char* fmt = "[%d, %d, %d, %d]"; static std::string fmt(const RValue<Byte4>& v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value*> val(const RValue<Byte4>& v); static std::vector<Value*> val(const RValue<Byte4>& v);
}; };
template <> struct PrintValue::Ty<Int> template <> struct PrintValue::Ty<Int>
{ {
static constexpr const char* fmt = "%d"; static std::string fmt(const RValue<Int>& v) { return "%d"; }
static std::vector<Value*> val(const RValue<Int>& v); static std::vector<Value*> val(const RValue<Int>& v);
}; };
template <> struct PrintValue::Ty<Int2> template <> struct PrintValue::Ty<Int2>
{ {
static constexpr const char* fmt = "[%d, %d]"; static std::string fmt(const RValue<Int>& v) { return "[%d, %d]"; }
static std::vector<Value*> val(const RValue<Int2>& v); static std::vector<Value*> val(const RValue<Int2>& v);
}; };
template <> struct PrintValue::Ty<Int4> template <> struct PrintValue::Ty<Int4>
{ {
static constexpr const char* fmt = "[%d, %d, %d, %d]"; static std::string fmt(const RValue<Int4>& v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value*> val(const RValue<Int4>& v); static std::vector<Value*> val(const RValue<Int4>& v);
}; };
template <> struct PrintValue::Ty<UInt> template <> struct PrintValue::Ty<UInt>
{ {
static constexpr const char* fmt = "%u"; static std::string fmt(const RValue<UInt>& v) { return "%u"; }
static std::vector<Value*> val(const RValue<UInt>& v); static std::vector<Value*> val(const RValue<UInt>& v);
}; };
template <> struct PrintValue::Ty<UInt2> template <> struct PrintValue::Ty<UInt2>
{ {
static constexpr const char* fmt = "[%u, %u]"; static std::string fmt(const RValue<UInt>& v) { return "[%u, %u]"; }
static std::vector<Value*> val(const RValue<UInt2>& v); static std::vector<Value*> val(const RValue<UInt2>& v);
}; };
template <> struct PrintValue::Ty<UInt4> template <> struct PrintValue::Ty<UInt4>
{ {
static constexpr const char* fmt = "[%u, %u, %u, %u]"; static std::string fmt(const RValue<UInt4>& v) { return "[%u, %u, %u, %u]"; }
static std::vector<Value*> val(const RValue<UInt4>& v); static std::vector<Value*> val(const RValue<UInt4>& v);
}; };
template <> struct PrintValue::Ty<Short> template <> struct PrintValue::Ty<Short>
{ {
static constexpr const char* fmt = "%d"; static std::string fmt(const RValue<Short>& v) { return "%d"; }
static std::vector<Value*> val(const RValue<Short>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<Short>& v);
}; };
template <> struct PrintValue::Ty<Short4> template <> struct PrintValue::Ty<Short4>
{ {
static constexpr const char* fmt = "[%d, %d, %d, %d]"; static std::string fmt(const RValue<Short4>& v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value*> val(const RValue<Short4>& v); static std::vector<Value*> val(const RValue<Short4>& v);
}; };
template <> struct PrintValue::Ty<UShort> template <> struct PrintValue::Ty<UShort>
{ {
static constexpr const char* fmt = "%u"; static std::string fmt(const RValue<UShort>& v) { return "%u"; }
static std::vector<Value*> val(const RValue<UShort>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<UShort>& v);
}; };
template <> struct PrintValue::Ty<UShort4> template <> struct PrintValue::Ty<UShort4>
{ {
static constexpr const char* fmt = "[%u, %u, %u, %u]"; static std::string fmt(const RValue<UShort4>& v) { return "[%u, %u, %u, %u]"; }
static std::vector<Value*> val(const RValue<UShort4>& v); static std::vector<Value*> val(const RValue<UShort4>& v);
}; };
template <> struct PrintValue::Ty<Float> template <> struct PrintValue::Ty<Float>
{ {
static constexpr const char* fmt = "[%f]"; static std::string fmt(const RValue<Float>& v) { return "[%f]"; }
static std::vector<Value*> val(const RValue<Float>& v); static std::vector<Value*> val(const RValue<Float>& v);
}; };
template <> struct PrintValue::Ty<Float4> template <> struct PrintValue::Ty<Float4>
{ {
static constexpr const char* fmt = "[%f, %f, %f, %f]"; static std::string fmt(const RValue<Float4>& v) { return "[%f, %f, %f, %f]"; }
static std::vector<Value*> val(const RValue<Float4>& v); static std::vector<Value*> val(const RValue<Float4>& v);
}; };
template <> struct PrintValue::Ty<Long> template <> struct PrintValue::Ty<Long>
{ {
static constexpr const char* fmt = "%lld"; static std::string fmt(const RValue<Long>& v) { return "%lld"; }
static std::vector<Value*> val(const RValue<Long>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<Long>& v) { return {v.value}; }
}; };
template <typename T> struct PrintValue::Ty< Pointer<T> > template <typename T> struct PrintValue::Ty< Pointer<T> >
{ {
static constexpr const char* fmt = "%p"; static std::string fmt(const RValue<Pointer<T>>& v) { return "%p"; }
static std::vector<Value*> val(const RValue<Pointer<T>>& v) { return {v.value}; } static std::vector<Value*> val(const RValue<Pointer<T>>& v) { return {v.value}; }
}; };
template <typename T> struct PrintValue::Ty< Reference<T> > template <typename T> struct PrintValue::Ty< Reference<T> >
{ {
static constexpr const char* fmt = PrintValue::Ty<T>::fmt; static std::string fmt(const Reference<T>& v) { return PrintValue::Ty<T>::fmt(v); }
static std::vector<Value*> val(const Reference<T>& v) { return PrintValue::Ty<T>::val(v); } static std::vector<Value*> val(const Reference<T>& v) { return PrintValue::Ty<T>::val(v); }
}; };
template <typename T> struct PrintValue::Ty< RValue<T> > template <typename T> struct PrintValue::Ty< RValue<T> >
{ {
static constexpr const char* fmt = PrintValue::Ty<T>::fmt; static std::string fmt(const RValue<T>& v) { return PrintValue::Ty<T>::fmt(v); }
static std::vector<Value*> val(const RValue<T>& v) { return PrintValue::Ty<T>::val(v); } static std::vector<Value*> val(const RValue<T>& v) { return PrintValue::Ty<T>::val(v); }
}; };
......
...@@ -386,11 +386,13 @@ namespace sw ...@@ -386,11 +386,13 @@ namespace sw
namespace rr { namespace rr {
template <> struct PrintValue::Ty<sw::Vector4f> template <> struct PrintValue::Ty<sw::Vector4f>
{ {
static constexpr const char* fmt = static std::string fmt(const sw::Vector4f& v)
"[x: <%f, %f, %f, %f>," {
" y: <%f, %f, %f, %f>," return "[x: " + PrintValue::fmt(v.x) + ","
" z: <%f, %f, %f, %f>," " y: " + PrintValue::fmt(v.y) + ","
" w: <%f, %f, %f, %f>]"; " z: " + PrintValue::fmt(v.z) + ","
" w: " + PrintValue::fmt(v.w) + "]";
}
static std::vector<rr::Value*> val(const sw::Vector4f& v) static std::vector<rr::Value*> val(const sw::Vector4f& v)
{ {
...@@ -399,11 +401,13 @@ namespace rr { ...@@ -399,11 +401,13 @@ namespace rr {
}; };
template <> struct PrintValue::Ty<sw::Vector4s> template <> struct PrintValue::Ty<sw::Vector4s>
{ {
static constexpr const char* fmt = static std::string fmt(const sw::Vector4s& v)
"[x: <%d, %d, %d, %d>," {
" y: <%d, %d, %d, %d>," return "[x: " + PrintValue::fmt(v.x) + ","
" z: <%d, %d, %d, %d>," " y: " + PrintValue::fmt(v.y) + ","
" w: <%d, %d, %d, %d>]"; " z: " + PrintValue::fmt(v.z) + ","
" w: " + PrintValue::fmt(v.w) + "]";
}
static std::vector<rr::Value*> val(const sw::Vector4s& v) static std::vector<rr::Value*> val(const sw::Vector4s& v)
{ {
......
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