Skip to content

Commit

Permalink
feat: fix ply loading
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Aug 14, 2024
1 parent 29aa059 commit 2478a4e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 71 deletions.
4 changes: 2 additions & 2 deletions Forge/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
152 changes: 88 additions & 64 deletions Forge/Formats/ply/TF_ply.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions Forge/Formats/ply/TF_ply.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion platforms/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 2478a4e

Please sign in to comment.