From 2478a4e37b19ca05643280f8e0613b1cd891638f Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 14 Aug 2024 08:29:51 -0700 Subject: [PATCH] feat: fix ply loading Signed-off-by: Michael Pollind --- Forge/BUCK | 4 +- .../FluidStudios/MemoryManager/mmgr.c | 4 +- Forge/Formats/ply/TF_ply.c | 152 ++++++++++-------- Forge/Formats/ply/TF_ply.h | 7 +- platforms/defs.bzl | 2 +- 5 files changed, 98 insertions(+), 71 deletions(-) diff --git a/Forge/BUCK b/Forge/BUCK index 41271fe087..dfdd2b2aad 100644 --- a/Forge/BUCK +++ b/Forge/BUCK @@ -43,10 +43,10 @@ configure_define( file = "TF_Config.in", out = "TF_Config.h", variables = { - "LOG_LEVEL": read_config("tf", "log-level", "eAll") + "LOG_LEVEL": read_root_config("tf", "log-level", "eAll") }, feature = { - "TF_ENABLE_MEMORY_TRACKING": read_config("tf", "memory-tracking", "0") == "1", + "TF_ENABLE_MEMORY_TRACKING": read_root_config("tf", "memory-tracking", "0") == "1", "TF_FEATURE_D3D12": select_has(["tf_platform//D3D12:supported"]), "TF_FEATURE_D3D11": select_has(["tf_platform//D3D11:supported"]), "TF_FEATURE_VULKAN": select_has(["tf_platform//VULKAN:supported"]), diff --git a/Forge/Common_3/Utilities/ThirdParty/OpenSource/FluidStudios/MemoryManager/mmgr.c b/Forge/Common_3/Utilities/ThirdParty/OpenSource/FluidStudios/MemoryManager/mmgr.c index d1f4d7f96e..c6aae6bc59 100644 --- a/Forge/Common_3/Utilities/ThirdParty/OpenSource/FluidStudios/MemoryManager/mmgr.c +++ b/Forge/Common_3/Utilities/ThirdParty/OpenSource/FluidStudios/MemoryManager/mmgr.c @@ -657,8 +657,8 @@ static void dumpLine(FileStream* fileToWrite, const char* format, ...) vsnprintf(buffer, BUFFER_SIZE, format, args); va_end(args); - _OutputDebugString(buffer); - _OutputDebugString("\n"); + LOGF(eDEBUG,"%s",buffer); + LOGF(eDEBUG, "\n"); if (fileToWrite != NULL && fileToWrite->pIO) { fsWriteToStream(fileToWrite, buffer, strlen(buffer)); diff --git a/Forge/Formats/ply/TF_ply.c b/Forge/Formats/ply/TF_ply.c index ce7aa0a297..3e4b552c38 100644 --- a/Forge/Formats/ply/TF_ply.c +++ b/Forge/Formats/ply/TF_ply.c @@ -104,10 +104,10 @@ void tfFreePlyFileReader(struct TPlyReader* reader) { } size_t tfPlyReadAttribCount(FileStream* stream, struct TPlyReader* reader, size_t cursor, struct TPlyAttribute* attrib, size_t* numElements) { - if (attrib->mAttributeListType != PLY_ATTRIBUTE_UNKNOWN) { + if (attrib->mListType != PLY_ATTRIBUTE_UNKNOWN) { struct TPlyNumber number; - if(tfPlyDecodeNumber(stream, cursor, reader->mFormat, attrib->mAttributeListType, &number)) { - switch(attrib->mAttributeListType) { + if(tfPlyDecodeNumber(stream, cursor, reader->mFormat, attrib->mListType, &number)) { + switch(attrib->mListType) { case PLY_ATTRIBUTE_CHAR8: (*numElements) = number.i8; break; @@ -133,7 +133,7 @@ size_t tfPlyReadAttribCount(FileStream* stream, struct TPlyReader* reader, size_ (*numElements) = number.dbl; break; } - return toPlyAttributeSize(attrib->mAttributeListType); + return toPlyAttributeSize(attrib->mListType); } } (*numElements) = 1; @@ -156,58 +156,81 @@ bool tfPlySeekElementStream(FileStream* stream, struct TPlyReader* reader, struc return false; } -bool tfPlyDecodeNumber(FileStream* stream, size_t cursor, enum PlyFormatData format, enum PlyAttributeType type, struct TPlyNumber* result) { - memset(result, 0, sizeof(struct TPlyNumber)); - const size_t expectedAttribSize = toPlyAttributeSize(type); - ASSERT(expectedAttribSize > 0); - fsSeekStream(stream, SBO_START_OF_FILE, cursor); - if (fsReadFromStream(stream, &result->buf, expectedAttribSize) == expectedAttribSize) { - switch (type) { - case PLY_ATTRIBUTE_SHORT16: - case PLY_ATTRIBUTE_USHORT16: - switch (format) { - case PLY_FORMAT_BIG_ENDIAN: - result->u16 = tfBE16ToHost(result->u16); - case PLY_FORMAT_LITTLE_ENDIAN: - result->u16 = tfLE16ToHost(result->u16); - break; - default: +void tfPlyDecodeNumbersByRefs(FileStream* stream, struct TPlyReader* reader, struct TPlyElement* element, size_t cursor, size_t numAttribs, + const hash32_t* refAttribs, struct TPlyNumber* attribNumbers) { + size_t offset = 0; + for (size_t i = 0; i < arrlen(element->mAttributes); i++) { + struct TPlyAttribute* attrib = &element->mAttributes[i]; + size_t numElements = 0; + offset += tfPlyReadAttribCount(stream, reader, cursor, attrib, &numElements); + for (size_t k = 0; k < numAttribs; k++) { + if (refAttribs[k] == attrib->mRefHash) { + tfPlyDecodeNumber(stream, offset + cursor, reader->mFormat, attrib->mType, &attribNumbers[k]); break; } + } + offset += toPlyAttributeSize(attrib->mType) * numElements; + } +} + +bool tfPlyDecodeNumber(FileStream* stream, size_t cursor, enum PlyFormatData format, enum PlyAttributeType type, + struct TPlyNumber* result) { + memset(result, 0, sizeof(struct TPlyNumber)); + fsSeekStream(stream, SBO_START_OF_FILE, cursor); + switch (type) { + case PLY_ATTRIBUTE_SHORT16: + case PLY_ATTRIBUTE_USHORT16: { + size_t bytesRead = fsReadFromStream(stream, &result->buf, sizeof(uint16_t)); + ASSERT(bytesRead == sizeof(uint16_t)); + switch (format) { + case PLY_FORMAT_BIG_ENDIAN: + result->u16 = tfBE16ToHost(result->u16); + case PLY_FORMAT_LITTLE_ENDIAN: + result->u16 = tfLE16ToHost(result->u16); break; - case PLY_ATTRIBUTE_INT32: - case PLY_ATTRIBUTE_UINT32: - case PLY_ATTRIBUTE_FLOAT32: - switch (format) { - case PLY_FORMAT_BIG_ENDIAN: - result->u32 = tfBE32ToHost(result->u32); - case PLY_FORMAT_LITTLE_ENDIAN: - result->u32 = tfLE32ToHost(result->u32); - break; - default: - break; - } + default: + break; + } + break; + } + case PLY_ATTRIBUTE_INT32: + case PLY_ATTRIBUTE_UINT32: + case PLY_ATTRIBUTE_FLOAT32: { + size_t bytesRead = fsReadFromStream(stream, &result->buf, sizeof(uint32_t)); + ASSERT(bytesRead == sizeof(uint32_t)); + switch (format) { + case PLY_FORMAT_BIG_ENDIAN: + result->u32 = tfBE32ToHost(result->u32); + case PLY_FORMAT_LITTLE_ENDIAN: + result->u32 = tfLE32ToHost(result->u32); break; - case PLY_ATTRIBUTE_FLOAT64: - switch (format) { - case PLY_FORMAT_BIG_ENDIAN: - result->u64 = tfBE64ToHost(result->u64); - case PLY_FORMAT_LITTLE_ENDIAN: - result->u64 = tfLE64ToHost(result->u64); - break; - default: - break; - } default: - case PLY_ATTRIBUTE_CHAR8: - case PLY_ATTRIBUTE_UCHAR8: break; } - - return true; + break; } - - return false; + case PLY_ATTRIBUTE_FLOAT64: { + size_t bytesRead = fsReadFromStream(stream, &result->buf, sizeof(uint64_t)); + ASSERT(bytesRead == sizeof(uint64_t)); + switch (format) { + case PLY_FORMAT_BIG_ENDIAN: + result->u64 = tfBE64ToHost(result->u64); + case PLY_FORMAT_LITTLE_ENDIAN: + result->u64 = tfLE64ToHost(result->u64); + break; + default: + break; + } + } + default: + case PLY_ATTRIBUTE_CHAR8: + case PLY_ATTRIBUTE_UCHAR8: { + size_t bytesRead = fsReadFromStream(stream, &result->buf, sizeof(uint8_t)); + ASSERT(bytesRead == sizeof(uint64_t)); + break; + } + } + return true; } bool tfPlyFindAttrib(FileStream* stream, struct TPlyReader* reader, size_t cursor, struct TPlyElement* element, struct TStrSpan attribName, @@ -218,16 +241,17 @@ bool tfPlyFindAttrib(FileStream* stream, struct TPlyReader* reader, size_t curso size_t numElements = 0; offset += tfPlyReadAttribCount(stream, reader, cursor, attrib, &numElements); if(tfStrEqual(attrib->mName, attribName)) { - result->mType = attrib->mAttributeType; + result->mType = attrib->mType; result->mNumElement = numElements; result->mCursor = cursor + offset; - result->mStride = toPlyAttributeSize(attrib->mAttributeType); + result->mStride = toPlyAttributeSize(attrib->mType); return true; } - offset += toPlyAttributeSize(attrib->mAttributeType) * numElements; + offset += toPlyAttributeSize(attrib->mType) * numElements; } return false; } + bool tfPlyFindAttribRef(FileStream* stream, struct TPlyReader* reader, size_t cursor, struct TPlyElement* element, hash32_t attribHash, struct TPlyAttribResult* result) { size_t offset = 0; @@ -236,13 +260,13 @@ bool tfPlyFindAttribRef(FileStream* stream, struct TPlyReader* reader, size_t cu size_t numElements = 0; offset += tfPlyReadAttribCount(stream, reader, cursor, attrib, &numElements); if(attrib->mRefHash == attribHash) { - result->mType = attrib->mAttributeType; + result->mType = attrib->mType; result->mNumElement = numElements; result->mCursor = cursor + offset; - result->mStride = toPlyAttributeSize(attrib->mAttributeType); + result->mStride = toPlyAttributeSize(attrib->mType); return true; } - offset += toPlyAttributeSize(attrib->mAttributeType) * numElements; + offset += toPlyAttributeSize(attrib->mType) * numElements; } return false; @@ -254,7 +278,7 @@ size_t tfPlyNextElement(FileStream* stream, struct TPlyReader* reader, size_t cu struct TPlyAttribute* attrib = &element->mAttributes[i]; size_t numElements = 0; offset += tfPlyReadAttribCount(stream, reader, cursor, attrib, &numElements); - offset += toPlyAttributeSize(attrib->mAttributeType) * numElements; + offset += toPlyAttributeSize(attrib->mType) * numElements; } return offset; } @@ -350,13 +374,13 @@ bool tfAddPlyFileReader(FileStream* stream, struct TPlyReader* plyReader) { if (tfStrIndexOf(arg1, tfCToStrRef("list")) >= 0) { enum PlyAttributeType attribListType = toPlyAttribute(tfStrTrim(tfStrSplitIter(&lineIterable))); enum PlyAttributeType attribType = toPlyAttribute(tfStrTrim(tfStrSplitIter(&lineIterable))); - attribute.mAttributeListType = attribListType; - attribute.mAttributeType = attribType; + attribute.mListType = attribListType; + attribute.mType = attribType; if (attribListType == PLY_ATTRIBUTE_UNKNOWN || attribType == PLY_ATTRIBUTE_UNKNOWN) goto error; } else { enum PlyAttributeType attribType = toPlyAttribute(arg1); - attribute.mAttributeType = attribType; + attribute.mType = attribType; if (attribType == PLY_ATTRIBUTE_UNKNOWN) goto error; } @@ -397,10 +421,10 @@ bool tfAddPlyFileReader(FileStream* stream, struct TPlyReader* plyReader) { struct TFStrSplitIterable lineIterable = { tfToStrRef(line), tfCToStrRef(" "), 0 }; for (size_t attrIdx = 0; attrIdx < arrlen(plyElement->mAttributes); attrIdx++) { struct TPlyAttribute* attr = &plyElement->mAttributes[attrIdx]; - const size_t attribSize = toPlyAttributeSize((enum PlyAttributeType)attr->mAttributeType); - const size_t listAttribSize = toPlyAttributeSize(attr->mAttributeListType); + const size_t attribSize = toPlyAttributeSize((enum PlyAttributeType)attr->mType); + const size_t listAttribSize = toPlyAttributeSize(attr->mListType); - if (attr->mAttributeListType != PLY_ATTRIBUTE_UNKNOWN) { + if (attr->mListType != PLY_ATTRIBUTE_UNKNOWN) { struct TStrSpan lenSpan = tfStrTrim(tfStrSplitIter(&lineIterable)); unsigned long long len = 0; if (!tfStrReadull(lenSpan, &len)) { @@ -412,13 +436,13 @@ bool tfAddPlyFileReader(FileStream* stream, struct TPlyReader* plyReader) { bufferSize = bufferSize + (bufferSize / 2); buf = tf_realloc(buf, bufferSize); } - if (!writeAttributeNativeASCII(attr->mAttributeListType, lenSpan, buf, &bufferPos)) { + if (!writeAttributeNativeASCII(attr->mListType, lenSpan, buf, &bufferPos)) { tf_free(buf); goto error; } for (size_t i = 0; i < len; i++) { struct TStrSpan valSpan = tfStrTrim(tfStrSplitIter(&lineIterable)); - if (!writeAttributeNativeASCII(attr->mAttributeType, valSpan, buf, &bufferPos)) { + if (!writeAttributeNativeASCII(attr->mType, valSpan, buf, &bufferPos)) { tf_free(buf); goto error; } @@ -429,7 +453,7 @@ bool tfAddPlyFileReader(FileStream* stream, struct TPlyReader* plyReader) { buf = tf_realloc(buf, bufferSize); } struct TStrSpan valSpan = tfStrTrim(tfStrSplitIter(&lineIterable)); - if (!writeAttributeNativeASCII(attr->mAttributeType, valSpan, buf, &bufferPos)) { + if (!writeAttributeNativeASCII(attr->mType, valSpan, buf, &bufferPos)) { tf_free(buf); goto error; } diff --git a/Forge/Formats/ply/TF_ply.h b/Forge/Formats/ply/TF_ply.h index 43980690bb..6b0c050399 100644 --- a/Forge/Formats/ply/TF_ply.h +++ b/Forge/Formats/ply/TF_ply.h @@ -26,9 +26,9 @@ enum PlyAttributeType { struct TPlyAttribute { - uint32_t mAttributeType; - uint32_t mAttributeListType; uint32_t mRefHash; + uint32_t mType; + uint32_t mListType; struct TStrSpan mName; }; @@ -90,10 +90,13 @@ bool tfPlyFindAttribRef(FileStream* stream, struct TPlyReader* reader, size_t hash32_t attribName, struct TPlyAttribResult* result); bool tfPlyDecodeNumber(FileStream* stream, size_t cursor, enum PlyFormatData format, enum PlyAttributeType type, struct TPlyNumber* result); +void tfPlyDecodeNumbersByRefs(FileStream* stream, struct TPlyReader* reader, struct TPlyElement* element, size_t cursor, size_t numAttribs, + const hash32_t* refAttribs, struct TPlyNumber* attribNumbers); void tfFreePlyFileReader(struct TPlyReader* reader); enum PlyAttributeType toPlyAttribute(struct TStrSpan input); + static inline size_t toPlyAttributeSize(enum PlyAttributeType attribute) { switch (attribute) { case PLY_ATTRIBUTE_CHAR8: diff --git a/platforms/defs.bzl b/platforms/defs.bzl index e1aca01449..da882750dc 100644 --- a/platforms/defs.bzl +++ b/platforms/defs.bzl @@ -54,7 +54,7 @@ def _host_os_configuration() -> str: def _common_constraints() -> list[str]: cfg = [] - build_type = read_config("tf", "build", "debug") + build_type = read_root_config("tf", "build", "debug") if build_type == "debug": cfg.append("tf_platform//build:debug") elif build_type == "release":