Skip to content

Commit 98c5ff6

Browse files
null77Commit Bot
authored andcommitted
BinaryStream: Preserve 64-bit integer data.
Previously the code would truncate 64-bit data to fit in 32-bits. This ran into a serialization bug when expanding a 64-bit mask. The new blend state masks for extended range were out of range for the 32-bit promotion that was happening before. Also refactors how we capture bools and enums to be more consistent. size_t is now correctly saved and loaded as 64-bits. Bug: angleproject:5247 Change-Id: I452a98c1b0add4c0cf45493032e9310e7d8321b2 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2497561 Reviewed-by: Courtney Goeltzenleuchter <courtneygo@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 parent 5cbf54d commit 98c5ff6

File tree

7 files changed

+215
-200
lines changed

7 files changed

+215
-200
lines changed

src/libANGLE/BinaryStream.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
namespace gl
2121
{
22+
template <typename IntT>
23+
struct PromotedIntegerType
24+
{
25+
using type = typename std::conditional<
26+
std::is_signed<IntT>::value,
27+
typename std::conditional<sizeof(IntT) <= 4, int32_t, int64_t>::type,
28+
typename std::conditional<sizeof(IntT) <= 4, uint32_t, uint64_t>::type>::type;
29+
};
2230

2331
class BinaryInputStream : angle::NonCopyable
2432
{
@@ -35,8 +43,11 @@ class BinaryInputStream : angle::NonCopyable
3543
template <class IntT>
3644
IntT readInt()
3745
{
38-
int value = 0;
46+
static_assert(!std::is_same<bool, std::remove_cv<IntT>()>(), "Use readBool");
47+
using PromotedIntT = typename PromotedIntegerType<IntT>::type;
48+
PromotedIntT value = 0;
3949
read(&value);
50+
ASSERT(angle::IsValueInRangeForNumericType<IntT>(value));
4051
return static_cast<IntT>(value);
4152
}
4253

@@ -49,8 +60,8 @@ class BinaryInputStream : angle::NonCopyable
4960
template <class IntT, class VectorElementT>
5061
void readIntVector(std::vector<VectorElementT> *param)
5162
{
52-
unsigned int size = readInt<unsigned int>();
53-
for (unsigned int index = 0; index < size; ++index)
63+
size_t size = readInt<size_t>();
64+
for (size_t index = 0; index < size; ++index)
5465
{
5566
param->push_back(readInt<IntT>());
5667
}
@@ -186,8 +197,8 @@ class BinaryOutputStream : angle::NonCopyable
186197
template <class IntT>
187198
void writeInt(IntT param)
188199
{
189-
using PromotedIntT =
190-
typename std::conditional<std::is_signed<IntT>::value, int, unsigned>::type;
200+
static_assert(!std::is_same<bool, std::remove_cv<IntT>()>(), "Use writeBool");
201+
using PromotedIntT = typename PromotedIntegerType<IntT>::type;
191202
ASSERT(angle::IsValueInRangeForNumericType<PromotedIntT>(param));
192203
PromotedIntT intValue = static_cast<PromotedIntT>(param);
193204
write(&intValue, 1);
@@ -232,22 +243,28 @@ class BinaryOutputStream : angle::NonCopyable
232243

233244
void writeBytes(const unsigned char *bytes, size_t count) { write(bytes, count); }
234245

246+
void writeBool(bool value)
247+
{
248+
int intValue = value ? 1 : 0;
249+
write(&intValue, 1);
250+
}
251+
235252
size_t length() const { return mData.size(); }
236253

237254
const void *data() const { return mData.size() ? &mData[0] : nullptr; }
238255

239256
const std::vector<uint8_t> &getData() const { return mData; }
240257

241258
private:
242-
std::vector<uint8_t> mData;
243-
244259
template <typename T>
245260
void write(const T *v, size_t num)
246261
{
247262
static_assert(std::is_fundamental<T>::value, "T must be a fundamental type.");
248263
const char *asBytes = reinterpret_cast<const char *>(v);
249264
mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
250265
}
266+
267+
std::vector<uint8_t> mData;
251268
};
252269

253270
inline BinaryOutputStream::BinaryOutputStream() {}

0 commit comments

Comments
 (0)