Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
tim.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
31#include "psyqo/gpu.hh"
33
34namespace psyqo {
35
59struct TIM {
68 enum class Type : uint8_t {
69 Tex4Bits = 0,
70 Tex8Bits = 1,
71 Tex16Bits = 2,
72 Tex24Bits = 3,
73 Mixed = 4,
74 };
75
88 enum class Error : uint8_t {
89 None = 0,
90 TooSmall,
91 BadMagic,
93 BadType,
94 Truncated,
96 };
97
105 struct Section {
106 const uint16_t* data = nullptr;
108 bool aligned = false;
109 };
110
123 bool parse(const uint8_t* data, uint32_t size);
124
138 void upload(GPU& gpu) const;
139
152 void upload(GPU& gpu, Vertex texturePosition, Vertex clutPosition) const;
153
161 PrimPieces::TPageAttr tpage(Vertex texturePosition) const;
162
167 PrimPieces::ClutIndex clutIndex(Vertex clutPosition) const;
168
169 bool hasCLUT() const { return m_hasCLUT; }
170 Type type() const { return m_type; }
171 Error error() const { return m_error; }
172
173 const Section& pixels() const { return m_pixels; }
174 const Section& clut() const { return m_clut; }
175
182 unsigned width() const;
183 unsigned height() const { return m_pixels.rect.size.h; }
184
185 private:
186 Section m_pixels;
187 Section m_clut;
188 Type m_type = Type::Tex4Bits;
189 Error m_error = Error::None;
190 bool m_hasCLUT = false;
191};
192
193} // namespace psyqo
The singleton GPU class.
Definition gpu.hh:83
Error
The error codes returned by every memory card operation.
Definition memory-card.hh:75
static int size
Definition string.h:149
Definition lua.hh:40
A primitive's CLUT command.
Definition common.hh:121
A primitive's tpage attribute.
Definition common.hh:188
The Rect struct.
Definition common.hh:74
Vertex size
Definition common.hh:79
One CLUT or pixel section.
Definition tim.hh:105
const uint16_t * data
Definition tim.hh:106
Rect rect
Definition tim.hh:107
bool aligned
Whether data sits on a four byte boundary, as DMA requires.
Definition tim.hh:108
A reader for Sony's TIM texture container.
Definition tim.hh:59
Error error() const
Definition tim.hh:171
unsigned width() const
The width of the image in texels rather than in VRAM units.
Definition tim.cpp:146
bool parse(const uint8_t *data, uint32_t size)
Parses a TIM out of a buffer, without copying or taking ownership.
Definition tim.cpp:47
Type type() const
Definition tim.hh:170
PrimPieces::TPageAttr tpage(Vertex texturePosition) const
Builds the texture page attribute describing where the pixels landed, with the colour mode taken from...
Definition tim.cpp:218
Error
Why a TIM was rejected.
Definition tim.hh:88
@ Truncated
A section claims to extend past the end of the buffer.
@ Compressed
The reserved field is set, which marks a compressed TIM.
@ BadSectionSize
A section's declared size disagrees with its dimensions.
@ BadType
The type field is outside the range the format defines.
@ BadMagic
The file does not start with 0x10, 0x00.
@ TooSmall
The buffer cannot hold even a file header.
bool hasCLUT() const
Definition tim.hh:169
void upload(GPU &gpu) const
Uploads the texture, and its palette if it has one, to the VRAM coordinates the file itself asks for.
Definition tim.cpp:208
const Section & pixels() const
Definition tim.hh:173
Type
The pixel format of the data, as declared by the file.
Definition tim.hh:68
unsigned height() const
Definition tim.hh:183
PrimPieces::ClutIndex clutIndex(Vertex clutPosition) const
Builds the CLUT index for a palette uploaded at the given VRAM position. Meaningless when the file ha...
Definition tim.cpp:241
const Section & clut() const
Definition tim.hh:174
The Vertex struct.
Definition common.hh:47
int16_t h
Definition common.hh:53