From 194eebf57c4c475752a962884f660ea18c42351d Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Sun, 12 Jun 2022 13:48:08 +0200 Subject: [PATCH] Finish Texture_ktx conversion to serializer --- libraries/gpu/src/gpu/Texture.h | 27 ++++++++ libraries/gpu/src/gpu/Texture_ktx.cpp | 92 +++++---------------------- libraries/shared/src/SerDes.h | 36 +++++++++++ tests/shared/src/SerializerTests.cpp | 8 +++ 4 files changed, 88 insertions(+), 75 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 5143cc1c23..2e7f8c62ca 100644 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -92,6 +92,33 @@ public: }; typedef std::shared_ptr< SphericalHarmonics > SHPointer; + +inline SerDes &operator<<(SerDes &ser, const SphericalHarmonics &h) { + ser << h.L00 << h.spare0; + ser << h.L1m1 << h.spare1; + ser << h.L10 << h.spare2; + ser << h.L11 << h.spare3; + ser << h.L2m2 << h.spare4; + ser << h.L2m1 << h.spare5; + ser << h.L20 << h.spare6; + ser << h.L21 << h.spare7; + ser << h.L22 << h.spare8; + return ser; +} + +inline SerDes &operator>>(SerDes &des, SphericalHarmonics &h) { + des >> h.L00 >> h.spare0; + des >> h.L1m1 >> h.spare1; + des >> h.L10 >> h.spare2; + des >> h.L11 >> h.spare3; + des >> h.L2m2 >> h.spare4; + des >> h.L2m1 >> h.spare5; + des >> h.L20 >> h.spare6; + des >> h.L21 >> h.spare7; + des >> h.L22 >> h.spare8; + return des; +} + class Sampler { public: diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index 748ec1a702..dc3d23948d 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -43,7 +43,7 @@ struct GPUKTXPayload { TextureUsageType _usageType; glm::ivec2 _originalSize { 0, 0 }; - void serialize2(SerDes &ser) { + void serialize(SerDes &ser) { ser << CURRENT_VERSION; ser << _samplerDesc; @@ -55,7 +55,7 @@ struct GPUKTXPayload { ser.addPadding(PADDING); } - bool unserialize2(SerDes &dsr) { + bool unserialize(SerDes &dsr) { Version version = 0; uint32 usageData; uint8_t usagetype = 0; @@ -80,60 +80,6 @@ struct GPUKTXPayload { return true; } - - Byte* serialize(Byte* data) const { - *(Version*)data = CURRENT_VERSION; - data += sizeof(Version); - - memcpy(data, &_samplerDesc, sizeof(Sampler::Desc)); - data += sizeof(Sampler::Desc); - - // We can't copy the bitset in Texture::Usage in a crossplateform manner - // So serialize it manually - uint32 usageData = _usage._flags.to_ulong(); - memcpy(data, &usageData, sizeof(uint32)); - data += sizeof(uint32); - - memcpy(data, &_usageType, sizeof(TextureUsageType)); - data += sizeof(TextureUsageType); - - memcpy(data, glm::value_ptr(_originalSize), sizeof(glm::ivec2)); - data += sizeof(glm::ivec2); - - return data + PADDING; - } - - bool unserialize(const Byte* data, size_t size) { - Version version = *(const Version*)data; - data += sizeof(Version); - - if (version > CURRENT_VERSION) { - // If we try to load a version that we don't know how to parse, - // it will render incorrectly - return false; - } - - memcpy(&_samplerDesc, data, sizeof(Sampler::Desc)); - data += sizeof(Sampler::Desc); - - // We can't copy the bitset in Texture::Usage in a crossplateform manner - // So unserialize it manually - uint32 usageData; - memcpy(&usageData, data, sizeof(uint32)); - _usage = Texture::Usage(usageData); - data += sizeof(uint32); - - memcpy(&_usageType, data, sizeof(TextureUsageType)); - data += sizeof(TextureUsageType); - - if (version >= 2) { - memcpy(&_originalSize, data, sizeof(glm::ivec2)); - data += sizeof(glm::ivec2); - } - - return true; - } - static bool isGPUKTX(const ktx::KeyValue& val) { return (val._key.compare(KEY) == 0); } @@ -143,8 +89,7 @@ struct GPUKTXPayload { if (found != keyValues.end()) { auto value = found->_value; SerDes dsr(value.data(), value.size()); - return payload.unserialize2(dsr); - //return payload.unserialize(value.data(), value.size()); + return payload.unserialize(dsr); } return false; } @@ -164,29 +109,24 @@ struct IrradianceKTXPayload { SphericalHarmonics _irradianceSH; - Byte* serialize(Byte* data) const { - *(Version*)data = CURRENT_VERSION; - data += sizeof(Version); - - memcpy(data, &_irradianceSH, sizeof(SphericalHarmonics)); - data += sizeof(SphericalHarmonics); - - return data + PADDING; + void serialize(SerDes &ser) const { + ser << CURRENT_VERSION; + ser << _irradianceSH; + ser.addPadding(PADDING); } - bool unserialize(const Byte* data, size_t size) { - if (size != SIZE) { + bool unserialize(SerDes &des) { + Version version; + if (des.length() != SIZE) { return false; } - Version version = *(const Version*)data; + des >> version; if (version != CURRENT_VERSION) { return false; } - data += sizeof(Version); - - memcpy(&_irradianceSH, data, sizeof(SphericalHarmonics)); + des >> _irradianceSH; return true; } @@ -198,7 +138,8 @@ struct IrradianceKTXPayload { auto found = std::find_if(keyValues.begin(), keyValues.end(), isIrradianceKTX); if (found != keyValues.end()) { auto value = found->_value; - return payload.unserialize(value.data(), value.size()); + SerDes des(value.data(), value.size()); + return payload.unserialize(des); } return false; } @@ -510,7 +451,7 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture, const glm::ivec Byte keyvalPayload[GPUKTXPayload::SIZE]; SerDes ser(keyvalPayload, sizeof(keyvalPayload)); - gpuKeyval.serialize2(ser); + gpuKeyval.serialize(ser); ktx::KeyValues keyValues; keyValues.emplace_back(GPUKTXPayload::KEY, (uint32)GPUKTXPayload::SIZE, (ktx::Byte*) &keyvalPayload); @@ -520,7 +461,8 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture, const glm::ivec irradianceKeyval._irradianceSH = *texture.getIrradiance(); Byte irradianceKeyvalPayload[IrradianceKTXPayload::SIZE]; - irradianceKeyval.serialize(irradianceKeyvalPayload); + SerDes ser(irradianceKeyvalPayload, sizeof(irradianceKeyvalPayload)); + irradianceKeyval.serialize(ser); keyValues.emplace_back(IrradianceKTXPayload::KEY, (uint32)IrradianceKTXPayload::SIZE, (ktx::Byte*) &irradianceKeyvalPayload); } diff --git a/libraries/shared/src/SerDes.h b/libraries/shared/src/SerDes.h index fe445fe854..356bb45522 100644 --- a/libraries/shared/src/SerDes.h +++ b/libraries/shared/src/SerDes.h @@ -344,6 +344,42 @@ class SerDes { /////////////////////////////////////////////////////////// + /** + * @brief Add an float to the output + * + * @param val Value to add + * @return SerDes& This object + */ + SerDes &operator<<(float val) { + if (!extendBy(sizeof(val))) { + return *this; + } + + memcpy(&_store[_pos], (char*)&val, sizeof(val)); + _pos += sizeof(val); + return *this; + } + + /** + * @brief Read an float from the buffer + * + * @param val Value to read + * @return SerDes& This object + */ + SerDes &operator>>(float &val) { + if ( _pos + sizeof(val) <= _length ) { + memcpy((char*)&val, &_store[_pos], sizeof(val)); + _pos += sizeof(val); + } else { + _overflow = true; + qCritical() << "Deserializer trying to read past end of input, reading float from position " << _pos << ", length " << _length; + } + return *this; + } + + /////////////////////////////////////////////////////////// + + /** * @brief Add an glm::vec3 to the output * diff --git a/tests/shared/src/SerializerTests.cpp b/tests/shared/src/SerializerTests.cpp index a1b3a37544..89598a3c2a 100644 --- a/tests/shared/src/SerializerTests.cpp +++ b/tests/shared/src/SerializerTests.cpp @@ -54,6 +54,9 @@ void SerializerTests::testAdd() { s << v; QCOMPARE(s.length(), 40); + s << 1.2345f; + QCOMPARE(s.length(), 44); + qDebug() << s; } @@ -66,6 +69,8 @@ void SerializerTests::testAddAndRead() { glm::vec4 v4_b; glm::ivec2 iv2_a{10, 24}; glm::ivec2 iv2_b; + float f_a = 1.2345f; + float f_b; s << (qint8)1; s << (qint16)0xaabb; @@ -73,6 +78,7 @@ void SerializerTests::testAddAndRead() { s << v3_a; s << v4_a; s << iv2_a; + s << f_a; qint8 i8; qint16 i16; @@ -86,6 +92,7 @@ void SerializerTests::testAddAndRead() { s >> v3_b; s >> v4_b; s >> iv2_b; + s >> f_b; qDebug() << s; @@ -95,6 +102,7 @@ void SerializerTests::testAddAndRead() { QCOMPARE(v3_a, v3_b); QCOMPARE(v4_a, v4_b); QCOMPARE(iv2_a, iv2_b); + QCOMPARE(f_a, f_b); } void SerializerTests::testReadPastEnd() {