summaryrefslogtreecommitdiff
path: root/include/user/gim.h
blob: a3ed5474a5628b91b5e795b1e8139c57137ca071 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
	Vita Development Suite Libraries
*/

#ifndef _VDSUITE_USER_GIM_H
#define _VDSUITE_USER_GIM_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <gxm.h>
#include <kernel/libkernel.h>
#include <kernel/sysmem/memblock.h>

#define SCE_GIM_TAG        0x2E47494DUL    /* 'GIM.' */
#define SCE_GIM_VERSION    0x312E3030UL    /* '1.00' */

/** GIM data block types */
#define SCE_GIM_BLOCK_TYPE_ROOT     0x02
#define SCE_GIM_BLOCK_TYPE_PICTURE  0x03
#define SCE_GIM_BLOCK_TYPE_IMAGE    0x04
#define SCE_GIM_BLOCK_TYPE_PALETTE  0x05
#define SCE_GIM_BLOCK_TYPE_FILEINFO 0xFF

/** GIM texture data formats */
#define SCE_GIM_TYPE_RGBA5650 0x0000
#define SCE_GIM_TYPE_RGBA5551 0x0001
#define SCE_GIM_TYPE_RGBA4444 0x0002
#define SCE_GIM_TYPE_RGBA8888 0x0003
#define SCE_GIM_TYPE_INDEX4   0x0004
#define SCE_GIM_TYPE_INDEX8   0x0005

/** GIM error codes */
typedef int SceGimErrorCode;
#define SCE_GIM_ERROR_INVALID_VALUE        -1
#define SCE_GIM_ERROR_INVALID_POINTER      -2
#define SCE_GIM_ERROR_INVALID_ALIGNMENT    -3
#define SCE_GIM_ERROR_NOT_FOUND            -4

#define SCE_GIM_PALETTE_SIZE_P4    64U
#define SCE_GIM_PALETTE_SIZE_P8    1024U

#define _SCE_GIM_ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))

/**	A header for a GIM file.
	This structure should be located right at the start of the GIM file.
*/
typedef struct SceGimHeader {
	uint32_t tag;           //!< GIM Identifier
	uint32_t version;       //!< Version number
	uint32_t formatStyle;   //!< GIM file format
	uint32_t pad;           //!< Padding
} SceGimHeader;

/*	Contains information about data block in the GIM file. */
typedef struct SceGimBlockInfo {
	uint16_t blockId;           //!< Block id (level)
	uint16_t unk;               //!< Always 0 on PSP2
	uint32_t blockSize;         //!< Size of this block + children blocks
	uint32_t blockHeaderNext;   //!< Offset to the next block header
	uint32_t blockDataOffset;   //!< Offset to the data of this block
} SceGimBlockInfo;

/*	Contains information about texture block in the GIM file. */
typedef struct SceGimTextureInfo {
	uint16_t blockHeaderSize;   //!< Size of this header (0x30)
	uint16_t reference;         //!< Always 0 on PSP2
	uint16_t format;            //!< Texture format
	uint16_t pixelOrder;        //!< Always 0 (normal order) on PSP2
	uint16_t width;             //!< Texture width
	uint16_t height;            //!< Texture height
	uint16_t bppAlign;          //!< Image/palette alignment BPP
	uint16_t pitchAlign;        //!< Image/palette alignment X
	uint16_t heightAlign;       //!< Image/palette alignment Y
	uint16_t dimCount;          //!< Always 2
	uint16_t reserved;          //!< Always 0
	uint16_t reserved2;         //!< Always 0
	uint32_t indexStart;        //!< Index relative start offset
	uint32_t pixelsStart;       //!< First plane/level/frame relative start offset
	uint32_t pixelsEnd;         //!< Last plane/level/frame relative end offset
	uint32_t planeMask;         //!< Always 0 on PSP2
	uint16_t levelType;
	uint16_t levelCount;        //!< Number of mipmaps
	uint16_t frameType;         //!< Always 3
	uint16_t frameCount;        //!< Always 1 on PSP2 (static images)
	uint32_t levelOffset;       //!< Always 0x40 on PSP2
	uint8_t pad[12];            //!< Padding
} SceGimTextureInfo;

#ifndef SCE_GIM_ERROR_RETURN
#define SCE_GIM_ERROR_RETURN(COND, ERROR_CODE, FMT, ...) \
	do {                                                 \
		if (!(COND)) { return (ERROR_CODE); }            \
	} while (false)
#endif

#ifndef SCE_GIM_ASSERT
#define SCE_GIM_ASSERT(x) ((void)(x))
#endif

/// Check if a value is aligned to the given power of 2 alignment.
#define SCE_GIM_IS_ALIGNED(VALUE, ALIGNMENT) (((uint32_t)(VALUE) & ((ALIGNMENT) - 1)) == 0)

/** Gets the header size of a GIM file.
	@return					The size of the header in bytes.
*/
inline uint32_t sceGimGetHeaderSize(void) {
	return sizeof(SceGimHeader);
}

/** Gets the start address of the texture data within a GIM file.
	@param gim				A pointer to the GIM data.
	@return					A pointer to the start of the texture data.
*/
inline void* sceGimGetDataAddress(void *gim) {
	return (void*)((uint32_t)gim + sceGimGetHeaderSize());
}

/** Gets the size of the texture data within a GIM file.
	@param gim				A pointer to the GIM data.
	@return					The size of the texture data in bytes.
*/
inline uint32_t sceGimGetDataSize(void *gim) {
	void* data_hdr = sceGimGetDataAddress(gim);
	SceGimBlockInfo *hdr = (SceGimBlockInfo*)data_hdr;
	return hdr->blockSize;
}

/** Gets the size of the whole GIM file.
	@param gim				A pointer to the GIM data.
	@return					The size of the file in bytes.
*/
inline uint32_t sceGimGetSize(void *gim) {
	return sceGimGetDataSize(gim) + sceGimGetHeaderSize();
}

/** Gets address of the image block.
	@param gim				A pointer to the GIM data.
	@return					A pointer to the start of the image block.
*/
inline void* sceGimFindImageAddress(void *gim) {

	uint32_t nextHdrOffs = sceGimGetHeaderSize();
	uint32_t gimSize = sceGimGetSize(gim);
	SceGimBlockInfo *hdr;

	while (nextHdrOffs < gimSize) {
		hdr = (SceGimBlockInfo*)((uint32_t)gim + nextHdrOffs);

		if (hdr->blockId == SCE_GIM_BLOCK_TYPE_IMAGE)
			return (void *)hdr;

		nextHdrOffs = hdr->blockHeaderNext + nextHdrOffs;
	}

	return NULL;
}

/** Gets address of the palette block.
	@param gim				A pointer to the GIM data.
	@return					A pointer to the start of the palette block.
*/
inline void* sceGimFindPaletteAddress(void *gim) {

	uint32_t nextHdrOffs = sceGimGetHeaderSize();
	uint32_t gimSize = sceGimGetSize(gim);
	SceGimBlockInfo *hdr;

	while (nextHdrOffs < gimSize) {
		hdr = (SceGimBlockInfo*)((uint32_t)gim + nextHdrOffs);

		if (hdr->blockId == SCE_GIM_BLOCK_TYPE_PALETTE)
			return (void *)hdr;

		nextHdrOffs = hdr->blockHeaderNext + nextHdrOffs;
	}

	return NULL;
}

/** Gets the number of textures in a GIM file.
	@return					The number of textures.
*/
inline uint32_t sceGimGetTextureCount(void *gim) {
	return 1;
}

/** Initializes the texture control words for a given GIM texture.

	@param texture			A pointer to texture to be initialized.
	@param gim				A pointer to the GIM data.

	@retval
	SCE_OK The operation was successful.
	@retval
	SCE_GIM_ERROR_INVALID_ALIGNMENT The operation failed due to an invalid buffer alignment.
	@retval
	SCE_GIM_ERROR_INVALID_VALUE The operation failed due to an invalid input parameter.
	@retval
	SCE_GIM_ERROR_INVALID_POINTER The operation failed due to an invalid input pointer.
*/
inline SceGimErrorCode sceGimInitTexture(SceGxmTexture *texture, void *gim) {

	SceGimTextureInfo *imgHdr = NULL;
	SceGimHeader *hdr = (SceGimHeader*)gim;

	// check parameters
	SCE_GIM_ERROR_RETURN(
		texture,
		SCE_GIM_ERROR_INVALID_POINTER,
		"");
	SCE_GIM_ERROR_RETURN(
		gim,
		SCE_GIM_ERROR_INVALID_POINTER,
		"");

	void* img = sceGimFindImageAddress(gim);

	SCE_GIM_ERROR_RETURN(
		img,
		SCE_GIM_ERROR_NOT_FOUND,
		"");

	void* pal = sceGimFindPaletteAddress(gim);

	if (pal)
		pal = (void*)((uint32_t)pal + 0x50);

	imgHdr = (SceGimTextureInfo*)((uint32_t)img + 0x10);
	img = (void*)((uint32_t)img + 0x50);

	SceGxmTextureFormat texFormat;

	if(hdr->version == SCE_GIM_VERSION) {
		switch(imgHdr->format)
		{
		case SCE_GIM_TYPE_INDEX8:
			texFormat = SCE_GXM_TEXTURE_FORMAT_P8_ABGR;
			break;
		case SCE_GIM_TYPE_INDEX4:
			texFormat = SCE_GXM_TEXTURE_FORMAT_P4_ABGR;
			break;
		case SCE_GIM_TYPE_RGBA8888:
			texFormat = SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR;
			break;
		case SCE_GIM_TYPE_RGBA4444:
			texFormat = SCE_GXM_TEXTURE_FORMAT_U4U4U4U4_ABGR;
			break;
		case SCE_GIM_TYPE_RGBA5551:
			texFormat = SCE_GXM_TEXTURE_FORMAT_U1U5U5U5_ABGR;
			break;
		case SCE_GIM_TYPE_RGBA5650:
			texFormat = SCE_GXM_TEXTURE_FORMAT_U5U6U5_BGR;
			break;
		default:
			// Unsupported type
			SCE_GIM_ASSERT(0);
		}
	}
	else {
		SCE_GIM_ERROR_RETURN(
			false,
			SCE_GIM_ERROR_INVALID_VALUE,
			"");
	}

	SceGxmErrorCode res = SCE_OK;

	int32_t align = imgHdr->pitchAlign * 8 / imgHdr->bppAlign - 1;
	int32_t strideWidth = (imgHdr->width + align) & ~align;

	res = sceGxmTextureInitLinear(
		texture,
		img,
		texFormat,
		strideWidth,
		imgHdr->height,
		imgHdr->levelCount);

	if (res < 0) {
		return res;
	}

	if (pal) {
		uint32_t pal_size = 0;

		if (imgHdr->format == SCE_GIM_TYPE_INDEX8) {
			pal_size = SCE_GIM_PALETTE_SIZE_P8;
		}
		else if (imgHdr->format == SCE_GIM_TYPE_INDEX4) {
			pal_size = SCE_GIM_PALETTE_SIZE_P4;
		}

		pal_size = _SCE_GIM_ALIGN(pal_size, 4 * 1024);

		SceUID id = sceKernelAllocMemBlock("gpu_mem", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, pal_size, NULL);

		if (id < 0) {
			return id;
		}

		void* pal_al;

		sceKernelGetMemBlockBase(id, &pal_al);

		res = sceGxmMapMemory(pal_al, pal_size, SCE_GXM_MEMORY_ATTRIB_READ);

		if (res < 0) {
			return res;
		}

		if (imgHdr->format == SCE_GIM_TYPE_INDEX8) {
			sceClibMemcpy(pal_al, pal, SCE_GIM_PALETTE_SIZE_P8);
		}
		else if (imgHdr->format == SCE_GIM_TYPE_INDEX4) {
			sceClibMemcpy(pal_al, pal, SCE_GIM_PALETTE_SIZE_P4);
		}

		res = sceGxmTextureSetPalette(texture, pal_al);
	}

	return res;
}

/** Checks if a pointer looks like a GIM file.

	@param gim			A pointer to the GIM data.

	@retval
	SCE_OK The data structure passes GIM header validation.
	@retval
	SCE_GIM_ERROR_INVALID_VALUE The operation failed because the header magic number was invalid
	or the version numbers are not compatible.
	@retval
	SCE_GIM_ERROR_INVALID_POINTER The operation failed due to an invalid input pointer.
*/
inline SceGimErrorCode sceGimCheckData(void *gim) {
	// check parameter
	SCE_GIM_ERROR_RETURN(
		gim,
		SCE_GIM_ERROR_INVALID_POINTER,
		"");

	// check the magic sequence
	SCE_GIM_ERROR_RETURN(
		((SceGimHeader*)gim)->tag == SCE_GIM_TAG,
		SCE_GIM_ERROR_INVALID_VALUE,
		"");

	// check the header version
	SCE_GIM_ERROR_RETURN(
		((SceGimHeader*)gim)->version == SCE_GIM_VERSION,
		SCE_GIM_ERROR_INVALID_VALUE,
		"");

	// all ok
	return SCE_OK;
}

#ifdef __cplusplus
}
#endif

#endif /* _VDSUITE_USER_GIM_H */