Skip to content

Commit

Permalink
Fix get_texture_format & add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepe20129 committed Apr 23, 2024
1 parent d540d7f commit 21599b2
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 72 deletions.
148 changes: 80 additions & 68 deletions fast64_internal/f3d/f3d_gbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,9 @@ def to_c(self):
data.source += "};\n\n"
return data

def empty_logging_func(a, b):
pass


class GfxList:
def __init__(self, name, tag, DLFormat):
Expand Down Expand Up @@ -2202,9 +2205,16 @@ def to_c_dynamic(self):
data += "\treturn glistp;\n}\n\n"
return data

def to_soh_xml(self, modelDirPath, objectPath):
def to_soh_xml(self, modelDirPath, objectPath, *args, **kwargs):
logging_func = kwargs.get('logging_func', empty_logging_func)

logging_func({"INFO"}, "GfxList to_soh_xml 1")

data = '<DisplayList Version="0">\n'
for command in self.commands:

logging_func({"INFO"}, "GfxList to_soh_xml 2 command=" + (str(command) if command is not None else "None"))

if isinstance(command, (SPDisplayList, SPBranchList, SPVertex, DPSetTextureImage)):
data += "\t" + command.to_soh_xml(objectPath) + "\n"
else:
Expand Down Expand Up @@ -2326,6 +2336,70 @@ def __eq__(self, __o: object) -> bool:
return self.palFormat == __o.palFormat and self.imagesSharingPalette == __o.imagesSharingPalette


def get_texture_format(texFormat, bitSize):
match texFormat:
case "RGBA16":
match bitSize:
case "G_IM_SIZ_16b":
return 2
case "G_IM_SIZ_32b":
return 1
case "RGBA32":
match bitSize:
case "G_IM_SIZ_16b":
return 2
case "G_IM_SIZ_32b":
return 1
case "CI4":
match bitSize:
case "G_IM_SIZ_4b":
return 3
case "G_IM_SIZ_8b":
return 4
case "CI8":
match bitSize:
case "G_IM_SIZ_4b":
return 3
case "G_IM_SIZ_8b":
return 4
case "I4":
match bitSize:
case "G_IM_SIZ_4b":
return 5
case "G_IM_SIZ_8b":
return 6
case "I8":
match bitSize:
case "G_IM_SIZ_4b":
return 5
case "G_IM_SIZ_8b":
return 6
case "IA4":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9
case "IA8":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9
case "IA16":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9


class FModel:
def __init__(
self,
Expand Down Expand Up @@ -2785,69 +2859,6 @@ def save_textures(self, exportPath):
image.filepath = oldpath
return texturesSaved

def get_texture_format(texFormat, bitSize):
match texFormat:
case "RGBA16":
match bitSize:
case "G_IM_SIZ_16b":
return 2
case "G_IM_SIZ_32b":
return 1
case "RGBA32":
match bitSize:
case "G_IM_SIZ_16b":
return 2
case "G_IM_SIZ_32b":
return 1
case "CI4":
match bitSize:
case "G_IM_SIZ_4b":
return 3
case "G_IM_SIZ_8b":
return 4
case "CI8":
match bitSize:
case "G_IM_SIZ_4b":
return 3
case "G_IM_SIZ_8b":
return 4
case "I4":
match bitSize:
case "G_IM_SIZ_4b":
return 5
case "G_IM_SIZ_8b":
return 6
case "I8":
match bitSize:
case "G_IM_SIZ_4b":
return 5
case "G_IM_SIZ_8b":
return 6
case "IA4":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9
case "IA8":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9
case "IA16":
match bitSize:
case "G_IM_SIZ_4b":
return 7
case "G_IM_SIZ_8b":
return 8
case "G_IM_SIZ_16b":
return 9

def save_soh_textures(self, exportPath, logging_func):
# TODO: Saving texture should come from FImage
texturesSaved = 0
Expand Down Expand Up @@ -3185,7 +3196,7 @@ def to_soh_xml(self, modelDirPath, objectPath, logging_func):
logging_func({"INFO"}, "FMesh.to_soh_xml 3")

# drawData = "<!-- Self.Draw Start -->\n"
drawData = self.draw.to_soh_xml(modelDirPath, objectPath)
drawData = self.draw.to_soh_xml(modelDirPath, objectPath, logging_func=logging_func)
# drawData += "<!-- Self.Draw End -->\n"

logging_func({"INFO"}, "FMesh.to_soh_xml 4")
Expand Down Expand Up @@ -3243,17 +3254,18 @@ def to_soh_xml(self, modelDirPath, objectPath, logging_func):
vtxData += self.vertexList.to_soh_xml()
# vtxData += "<!-- VertexList End -->\n"

logging_func({"INFO"}, "FTriGroup.to_soh_xml 1")
logging_func({"INFO"}, "FTriGroup.to_soh_xml 1.1 modelDirPath=" + (str(modelDirPath) if modelDirPath is not None else "None"))
logging_func({"INFO"}, "FTriGroup.to_soh_xml 1.2 self.vertexList.name=" + (str(self.vertexList.name) if self.vertexList.name is not None else "None"))

writeXMLData(vtxData, os.path.join(modelDirPath, self.vertexList.name))
# writeXMLData(vtxData, os.path.join(modelDirPath, self.vertexList.name))

logging_func({"INFO"}, "FTriGroup.to_soh_xml 2")

triListData = ""
# triListData += "<!-- TriList Start ({triListName}) -->\n".format(triListName = self.triList.name)
triListData += self.triList.to_soh_xml(modelDirPath, objectPath)
# triListData += "<!-- TriList End -->\n"
writeXMLData(triListData, os.path.join(modelDirPath, self.triList.name))
# writeXMLData(triListData, os.path.join(modelDirPath, self.triList.name))

logging_func({"INFO"}, "FTriGroup.to_soh_xml 3")

Expand Down
43 changes: 42 additions & 1 deletion fast64_internal/f3d/f3d_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,52 +409,84 @@ def saveMeshWithLargeTexturesByFaces(
return currentGroupIndex


def empty_logging_func(a, b):
pass


# Make sure to set original_name before calling this
# used when duplicating an object
def saveStaticModel(
triConverterInfo, fModel, obj, transformMatrix, ownerName, convertTextureData, revertMatAtEnd, drawLayerField
triConverterInfo, fModel, obj, transformMatrix, ownerName, convertTextureData, revertMatAtEnd, drawLayerField, *args, **kwargs
):
logging_func = kwargs.get('logging_func', empty_logging_func)

logging_func({"INFO"}, "saveStaticModel 1")

if len(obj.data.polygons) == 0:
return None

logging_func({"INFO"}, "saveStaticModel 2")

# checkForF3DMaterial(obj)

logging_func({"INFO"}, "saveStaticModel 3")

faces_by_mat = {}
for face in obj.data.loop_triangles:
if face.material_index not in faces_by_mat:
faces_by_mat[face.material_index] = []
faces_by_mat[face.material_index].append(face)

logging_func({"INFO"}, "saveStaticModel 4")

# sort by material slot
faces_by_mat = {
mat_index: faces_by_mat[mat_index]
for mat_index, _ in enumerate(obj.material_slots)
if mat_index in faces_by_mat
}

logging_func({"INFO"}, "saveStaticModel 5")

fMeshes = {}
for material_index, faces in faces_by_mat.items():
material = obj.material_slots[material_index].material

logging_func({"INFO"}, "saveStaticModel 6")

if drawLayerField is not None and material.mat_ver > 3:
drawLayer = getattr(material.f3d_mat.draw_layer, drawLayerField)
drawLayerName = drawLayer
else:
drawLayer = fModel.getDrawLayerV3(obj)
drawLayerName = None

logging_func({"INFO"}, "saveStaticModel 7")

if drawLayer not in fMeshes:
logging_func({"INFO"}, "saveStaticModel 7.11 " + (obj.original_name if obj.original_name is not None else "None" ))
logging_func({"INFO"}, "saveStaticModel 7.12 " + (ownerName if ownerName is not None else "None" ))
logging_func({"INFO"}, "saveStaticModel 7.13 " + (drawLayerName if drawLayerName is not None else "None" ))
logging_func({"INFO"}, "saveStaticModel 7.14 " + (str(obj) if obj is not None else "None" ))
fMesh = fModel.addMesh(obj.original_name, ownerName, drawLayerName, False, obj)
logging_func({"INFO"}, "saveStaticModel 7.2")
fMeshes[drawLayer] = fMesh
logging_func({"INFO"}, "saveStaticModel 7.3")

if obj.use_f3d_culling and (fModel.f3d.F3DEX_GBI or fModel.f3d.F3DEX_GBI_2):
logging_func({"INFO"}, "saveStaticModel 7.4")
addCullCommand(obj, fMesh, transformMatrix, fModel.matWriteMethod)
logging_func({"INFO"}, "saveStaticModel 7.5")
else:
fMesh = fMeshes[drawLayer]

logging_func({"INFO"}, "saveStaticModel 8")

checkForF3dMaterialInFaces(obj, material)
fMaterial, texDimensions = saveOrGetF3DMaterial(material, fModel, obj, drawLayer, convertTextureData)

logging_func({"INFO"}, "saveStaticModel 9")

if fMaterial.isTexLarge[0] or fMaterial.isTexLarge[1]:
saveMeshWithLargeTexturesByFaces(
material,
Expand Down Expand Up @@ -486,11 +518,20 @@ def saveStaticModel(
None,
)

logging_func({"INFO"}, "saveStaticModel 10")

logging_func({"INFO"}, "saveStaticModel 11")

for drawLayer, fMesh in fMeshes.items():
if revertMatAtEnd:
revertMatAndEndDraw(fMesh.draw, [])
else:
fModel.endDraw(fMesh, obj)

logging_func({"INFO"}, "saveStaticModel 12")

logging_func({"INFO"}, "saveStaticModel 13")

return fMeshes


Expand Down
Loading

0 comments on commit 21599b2

Please sign in to comment.