diff --git a/include/tex_len.h b/include/tex_len.h new file mode 100644 index 0000000000..f6e54a82d1 --- /dev/null +++ b/include/tex_len.h @@ -0,0 +1,14 @@ +#ifndef TEX_LEN_H +#define TEX_LEN_H + +/** + * Compute a length for an array holding a texture. + * `type` is the array's element type. + * `width`, `height` are the texture dimensions. + * `bpp` is the texture's pixel size, in bits per pixels. + * The calculation computes the size of the texture in bits `width * height * bpp`, + * then divides by 8 to get the size in bytes, then divides by the element type size. + */ +#define TEX_LEN(type, width, height, bpp) ((width) * (height) * (bpp) / 8 / sizeof(type)) + +#endif diff --git a/tools/assets/extract/extase_oot64/dlist_resources.py b/tools/assets/extract/extase_oot64/dlist_resources.py index 63e8635a69..ad470c33aa 100644 --- a/tools/assets/extract/extase_oot64/dlist_resources.py +++ b/tools/assets/extract/extase_oot64/dlist_resources.py @@ -275,12 +275,19 @@ class TextureResource(Resource): self.width_name = f"{self.symbol_name}_WIDTH" self.height_name = f"{self.symbol_name}_HEIGHT" + def check_declare_length(self): + return ( + hasattr(self, "HACK_IS_STATIC_ON") or EXPLICIT_DL_AND_TEX_SIZES + ) and not self.is_tlut() + def get_c_declaration_base(self): if hasattr(self, "HACK_IS_STATIC_ON") and self.is_tlut(): raise NotImplementedError - if hasattr(self, "HACK_IS_STATIC_ON") or EXPLICIT_DL_AND_TEX_SIZES: - if not self.is_tlut(): - return f"{self.elem_type} {self.symbol_name}[{self.height_name} * {self.width_name} * {self.siz.bpp} / 8 / sizeof({self.elem_type})]" + if self.check_declare_length(): + return ( + f"{self.elem_type} {self.symbol_name}" + f"[TEX_LEN({self.elem_type}, {self.width_name}, {self.height_name}, {self.siz.bpp})]" + ) return f"{self.elem_type} {self.symbol_name}[]" def get_c_reference(self, resource_offset: int): @@ -518,7 +525,10 @@ class TextureResource(Resource): super().write_c_declaration(h) def get_h_includes(self): - return ("ultra64.h",) + return ( + "ultra64.h", + *(("tex_len.h",) if self.check_declare_length() else ()), + ) @reprlib.recursive_repr() def __repr__(self):