Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions out/Android-arm32/args.gn
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ cflags_c = [
"-fslp-vectorize",
"-pipe",
"-integrated-as",
"-mllvm",
"-inlinehint-threshold=100000",
"-flto",
"-marm",
"-D_FILE_OFFSET_BITS=64",
Expand All @@ -82,6 +84,8 @@ cflags_cc = [
"-march=armv7-a+neon",
"-mllvm",
"-hot-cold-split=true",
"-mllvm",
"-inlinehint-threshold=100000",
"-fdata-sections",
"-ffunction-sections",
"-fmerge-all-constants",
Expand Down
4 changes: 4 additions & 0 deletions out/Android-arm64/args.gn
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ cflags_c = [
"-polly-ast-use-context",
"-mllvm",
"-polly-parallel",
"-mllvm",
"-inlinehint-threshold=100000",
"-flto",
"-D_FILE_OFFSET_BITS=64",
"-D__USE_BSD",
Expand Down Expand Up @@ -125,6 +127,8 @@ cflags_cc = [
"-polly-ast-use-context",
"-mllvm",
"-polly-parallel",
"-mllvm",
"-inlinehint-threshold=100000",
"-flto",
"-D_FILE_OFFSET_BITS=64",
"-D__USE_BSD",
Expand Down
55 changes: 29 additions & 26 deletions src/common/BinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BinaryInputStream : angle::NonCopyable

// readInt will generate an error for bool types
template <class IntT>
IntT readInt()
ANGLE_INLINE IntT readInt()
{
static_assert(!std::is_same<bool, std::remove_cv<IntT>()>(), "Use readBool");
using PromotedIntT = typename PromotedIntegerType<IntT>::type;
Expand All @@ -50,7 +50,7 @@ class BinaryInputStream : angle::NonCopyable
}

template <class IntT>
void readInt(IntT *outValue)
ANGLE_INLINE void readInt(IntT *outValue)
{
*outValue = readInt<IntT>();
}
Expand Down Expand Up @@ -83,28 +83,28 @@ class BinaryInputStream : angle::NonCopyable
}

template <class EnumT>
EnumT readEnum()
ANGLE_INLINE EnumT readEnum()
{
using UnderlyingType = typename std::underlying_type<EnumT>::type;
return static_cast<EnumT>(readInt<UnderlyingType>());
}

template <class EnumT>
void readEnum(EnumT *outValue)
ANGLE_INLINE void readEnum(EnumT *outValue)
{
*outValue = readEnum<EnumT>();
}

bool readBool()
ANGLE_INLINE bool readBool()
{
int value = 0;
read(angle::byte_span_from_ref(value));
return value > 0;
}

void readBool(bool *outValue) { *outValue = readBool(); }
ANGLE_INLINE void readBool(bool *outValue) { *outValue = readBool(); }

void readBytes(angle::Span<uint8_t> outArray) { read(outArray); }
ANGLE_INLINE void readBytes(angle::Span<uint8_t> outArray) { read(outArray); }

std::string readString()
{
Expand All @@ -125,7 +125,6 @@ class BinaryInputStream : angle::NonCopyable

angle::CheckedNumeric<size_t> checkedOffset(mOffset);
checkedOffset += length;

if (!checkedOffset.IsValid() || checkedOffset.ValueOrDie() > mData.size())
{
mError = true;
Expand All @@ -136,7 +135,7 @@ class BinaryInputStream : angle::NonCopyable
mOffset = checkedOffset.ValueOrDie();
}

float readFloat()
ANGLE_INLINE float readFloat()
{
float f = 0.0f;
read(angle::byte_span_from_ref(f));
Expand All @@ -157,17 +156,17 @@ class BinaryInputStream : angle::NonCopyable
mOffset = checkedOffset.ValueOrDie();
}

bool error() const { return mError; }
bool endOfStream() const { return mOffset == mData.size(); }
ANGLE_INLINE bool error() const { return mError; }
ANGLE_INLINE bool endOfStream() const { return mOffset == mData.size(); }

// data() and size() methods allow implicit conversion to span.
const uint8_t *data() const { return mData.data(); }
size_t size() const { return mData.size(); }
ANGLE_INLINE const uint8_t *data() const { return mData.data(); }
ANGLE_INLINE size_t size() const { return mData.size(); }

angle::Span<const uint8_t> remainingSpan() const { return mData.subspan(mOffset); }
ANGLE_INLINE angle::Span<const uint8_t> remainingSpan() const { return mData.subspan(mOffset); }

private:
void read(angle::Span<uint8_t> dstSpan)
ANGLE_INLINE void read(angle::Span<uint8_t> dstSpan)
{
angle::CheckedNumeric<size_t> checkedOffset(mOffset);
checkedOffset += dstSpan.size();
Expand All @@ -194,9 +193,12 @@ class BinaryOutputStream : angle::NonCopyable
BinaryOutputStream() = default;
~BinaryOutputStream() = default;

// Pre-allocate internal buffer to avoid reallocations.
void reserve(size_t capacity) { mData.reserve(capacity); }

// writeInt also handles bool types
template <class IntT>
void writeInt(IntT param)
ANGLE_INLINE void writeInt(IntT param)
{
static_assert(std::is_integral<IntT>::value, "Not an integral type");
static_assert(!std::is_same<bool, std::remove_cv<IntT>()>(), "Use writeBool");
Expand All @@ -208,7 +210,7 @@ class BinaryOutputStream : angle::NonCopyable

// Specialized writeInt for values that can also be exactly -1.
template <class UintT>
void writeIntOrNegOne(UintT param)
ANGLE_INLINE void writeIntOrNegOne(UintT param)
{
if (param == static_cast<UintT>(-1))
{
Expand Down Expand Up @@ -248,38 +250,39 @@ class BinaryOutputStream : angle::NonCopyable
}

template <class EnumT>
void writeEnum(EnumT param)
ANGLE_INLINE void writeEnum(EnumT param)
{
using UnderlyingType = typename std::underlying_type<EnumT>::type;
writeInt<UnderlyingType>(static_cast<UnderlyingType>(param));
}

void writeString(std::string_view v)
ANGLE_INLINE void writeString(std::string_view v)
{
writeInt(v.size());
write(angle::as_byte_span(v));
}

void writeBytes(angle::Span<const uint8_t> bytes) { write(bytes); }
ANGLE_INLINE void writeBytes(angle::Span<const uint8_t> bytes) { write(bytes); }

void writeBool(bool value)
ANGLE_INLINE void writeBool(bool value)
{
const int intValue = value ? 1 : 0;
write(angle::byte_span_from_ref(intValue));
}

void writeFloat(float value) { write(angle::byte_span_from_ref(value)); }
ANGLE_INLINE void writeFloat(float value) { write(angle::byte_span_from_ref(value)); }

// data() and size() methods allow implicit conversion to span.
const uint8_t *data() const { return mData.data(); }
size_t size() const { return mData.size(); }
ANGLE_INLINE const uint8_t *data() const { return mData.data(); }
ANGLE_INLINE size_t size() const { return mData.size(); }

// No further use of this stream allowed after data is taken.
std::vector<uint8_t> takeData() { return std::move(mData); }

private:
void write(angle::Span<const uint8_t> srcSpan)
ANGLE_INLINE void write(angle::Span<const uint8_t> srcSpan)
{
// Insert copies the entire span in one call, efficiently using memmove.
mData.insert(mData.end(), srcSpan.begin(), srcSpan.end());
}

Expand All @@ -288,4 +291,4 @@ class BinaryOutputStream : angle::NonCopyable

} // namespace gl

#endif // COMMON_BINARYSTREAM_H_
#endif // COMMON_BINARYSTREAM_H_
20 changes: 16 additions & 4 deletions src/common/FastVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstring>
#include <initializer_list>
#include <iterator>
#include <type_traits>

namespace angle
{
Expand Down Expand Up @@ -557,10 +558,10 @@ ANGLE_INLINE void FastVector<T, N, Storage>::remove_and_permute(iterator pos)
pop_back();
}

// Optimized capacity growth: use 1.5x factor instead of doubling to reduce memory fragmentation.
template <class T, size_t N, class Storage>
void FastVector<T, N, Storage>::ensure_capacity(size_t capacity)
{
// We have a minimum capacity of N.
if (mReservedSize < capacity)
{
increase_capacity(capacity);
Expand All @@ -571,17 +572,28 @@ template <class T, size_t N, class Storage>
ANGLE_NOINLINE void FastVector<T, N, Storage>::increase_capacity(size_t capacity)
{
ASSERT(capacity > N);
// Use 1.5x growth factor (rounded up) for better memory reuse.
size_type newSize = std::max(mReservedSize, N);
while (newSize < capacity)
{
newSize *= 2;
newSize = newSize + newSize / 2; // 1.5x
if (newSize < capacity)
newSize = capacity; // ensure at least capacity
}

pointer newData = new value_type[newSize];

if (mSize > 0)
{
std::move(begin(), end(), newData);
// Optimized copy for trivially copyable types using memcpy.
if constexpr (std::is_trivially_copyable_v<value_type>)
{
std::memcpy(newData, mData, mSize * sizeof(value_type));
}
else
{
std::move(begin(), end(), newData);
}
}

if (!uses_fixed_storage())
Expand Down Expand Up @@ -812,4 +824,4 @@ class FlatUnorderedSet final
};
} // namespace angle

#endif // COMMON_FASTVECTOR_H_
#endif // COMMON_FASTVECTOR_H_
2 changes: 1 addition & 1 deletion src/common/FixedVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,4 @@ typename angle::FixedVector<T, N, Storage>::size_type erase_if(angle::FixedVecto
}
} // namespace std

#endif // COMMON_FIXEDVECTOR_H_
#endif // COMMON_FIXEDVECTOR_H_
22 changes: 11 additions & 11 deletions src/common/mathutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ unsigned int convertRGBFloatsTo999E5(float red, float green, float blue)
const float blue_c = std::max<float>(0, std::min(g_sharedexp_max, blue));

const float max_c = std::max<float>({red_c, green_c, blue_c});
const float exp_p =
std::max<float>(-g_sharedexp_bias - 1, floor(log(max_c))) + 1 + g_sharedexp_bias;
// Use log2 and exp2 for faster and more accurate exponent calculation
const float exp_p = std::max<float>(-g_sharedexp_bias - 1,
std::floor(std::log2(max_c))) + 1 + g_sharedexp_bias;
const int max_s = static_cast<int>(
floor((max_c / (pow(2.0f, exp_p - g_sharedexp_biased_mantissabits))) + 0.5f));
const int exp_s =
static_cast<int>((max_s < pow(2.0f, g_sharedexp_mantissabits)) ? exp_p : exp_p + 1);
const float pow2_exp = pow(2.0f, static_cast<float>(exp_s) - g_sharedexp_biased_mantissabits);
std::floor((max_c / std::exp2(exp_p - g_sharedexp_biased_mantissabits)) + 0.5f));
const int exp_s = static_cast<int>((max_s < (1 << g_sharedexp_mantissabits)) ? exp_p : exp_p + 1);
const float pow2_exp = std::exp2(static_cast<float>(exp_s) - g_sharedexp_biased_mantissabits);

RGB9E5Data output;
output.R = static_cast<unsigned int>(floor((red_c / pow2_exp) + 0.5f));
output.G = static_cast<unsigned int>(floor((green_c / pow2_exp) + 0.5f));
output.B = static_cast<unsigned int>(floor((blue_c / pow2_exp) + 0.5f));
output.R = static_cast<unsigned int>(std::floor((red_c / pow2_exp) + 0.5f));
output.G = static_cast<unsigned int>(std::floor((green_c / pow2_exp) + 0.5f));
output.B = static_cast<unsigned int>(std::floor((blue_c / pow2_exp) + 0.5f));
output.E = exp_s;

return bitCast<unsigned int>(output);
Expand All @@ -72,8 +72,8 @@ void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float
{
const RGB9E5Data *inputData = reinterpret_cast<const RGB9E5Data *>(&input);

const float pow2_exp =
pow(2.0f, static_cast<float>(inputData->E) - g_sharedexp_biased_mantissabits);
// Use ldexp for faster exponent to float conversion
const float pow2_exp = std::ldexp(1.0f, inputData->E - g_sharedexp_biased_mantissabits);

*red = inputData->R * pow2_exp;
*green = inputData->G * pow2_exp;
Expand Down
3 changes: 1 addition & 2 deletions src/common/mathutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,7 @@ inline float Ldexp(float x, int exp)
{
return 0.0f;
}
double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp));
return static_cast<float>(result);
return std::ldexp(x, exp);
}

// First, both normalized floating-point values are converted into 16-bit integer values.
Expand Down
Loading
Loading