1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 2010-2011 Nintendo.  All rights reserved.
4 
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law.  They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10 
11  *---------------------------------------------------------------------------*/
12 
13 #pragma once
14 #ifndef __GFDFILEIO_H__
15 #define __GFDFILEIO_H__
16 
17 #ifdef __cplusplus
18 extern "C"
19 {
20 #endif // __cplusplus
21 
22 /// @addtogroup GFDFileIOGroup
23 /// @{
24 
25 /// \brief Round up value to power-of-2 size
26 #define GFD_ROUND_UP(value, size) (((value) + ((size) - 1)) & ~((size) - 1))
27 
28 //
29 // Write Functions
30 //
31 
32 /// \brief Writes a GFD header to the file.
33 ///
34 /// \param fp                   Dest Output file pointer.
35 /// \param gpuVer               Gpu version.
36 /// \param alignMode            Align mode.
37 /// \return true or false
38 ///
39 /// \donotcall \notthreadsafe \hostonly \enddonotcall
40 ///
41 GFD_DECLSPEC bool GFD_API GFDWriteFileHeader(FILE *fp, GFDGPUVersion gpuVer, GFDAlignMode alignMode);
42 
43 /// \brief Writes a block header to the file.
44 ///
45 /// \param fp                   Dest Output file pointer.
46 /// \param type                 Type of Block
47 /// \param dataSize             Data size in bytes following this header.
48 /// \return true or false
49 ///
50 /// \donotcall \notthreadsafe \hostonly \enddonotcall
51 ///
52 GFD_DECLSPEC bool GFD_API GFDWriteFileBlockHeader(FILE *fp, GFDBlockType type, u32 dataSize);
53 
54 /// \brief Writes a GPU data to the file.
55 ///
56 /// \param fp                   Dest Output file pointer.
57 /// \param numElements          Number of input elements
58 /// \param elemSize             Element size
59 /// \param swapMode             Endian Swap mode for data writing
60 /// \param pData                Input Data pointer.
61 /// \return true or false
62 ///
63 /// \donotcall \notthreadsafe \hostonly \enddonotcall
64 ///
65 GFD_DECLSPEC bool GFD_API GFDWriteFileGPUData(FILE *fp, u32 numElements, GFDElementSize elemSize, GFDEndianSwapMode swapMode, void *pData);
66 
67 /// \brief Writes a PPC data to the file.
68 ///
69 /// \param fp                   Dest Output file pointer
70 /// \param numElements          Number of input elements
71 /// \param elemSize             Element size
72 /// \param pData                Input Data pointer.
73 /// \return true or false
74 ///
75 /// \donotcall \notthreadsafe \hostonly \enddonotcall
76 ///
GFDWriteFilePPCData(FILE * fp,u32 numElements,GFDElementSize elemSize,void * pData)77 GFD_DECLSPEC inline bool GFD_API GFDWriteFilePPCData(FILE *fp, u32 numElements, GFDElementSize elemSize, void *pData)
78 {
79     return GFDWriteFileGPUData(fp, numElements, elemSize, GFD_ENDIAN_SWAP_MODE_BIG, pData);
80 }
81 
82 /// \brief Writes a padding block header and padding data block with 0 to the file.
83 ///
84 /// \param fp                   Dest Output file pointer.
85 /// \param padSize              Padding size in bytes for data block
86 /// \return true or false
87 ///
88 /// \donotcall \notthreadsafe \hostonly \enddonotcall
89 ///
90 GFD_DECLSPEC bool GFD_API GFDWriteFilePadBlock(FILE *fp, u32 padSize);
91 
92 /// \brief Open gfd file. Need to call GFDCloseFile to close.
93 ///
94 /// \param fp                  A pointer to the file pointer that will receive the pointer to the opened file.
95 /// \param fileName            Name of the input gtx file.
96 /// \param mode                string containing a file access mode for fopen_s.
97 /// \return 0 on successful to open. Return fopen_s error code on failure.
98 ///
99 /// \donotcall \notthreadsafe \hostonly \enddonotcall
100 ///
101 GFD_DECLSPEC errno_t GFD_API GFDOpenFile(FILE **fp, const char* fileName,  const char *mode);
102 
103 /// \brief Close gfd file.
104 ///
105 /// \param fp                   Pointer to FILE structure.
106 /// \return 0 on the stream is successfully closed.
107 ///
108 /// \donotcall \notthreadsafe \hostonly \enddonotcall
109 ///
110 GFD_DECLSPEC int GFD_API GFDCloseFile(FILE *fp);
111 
112 //
113 // Read Functions
114 //
115 
116 /// \brief Reads a GPU data from the file. Need to use GFDOpenFile for the file pointer.
117 ///
118 /// \param pData                Output buffer pointer.
119 /// \param numElements          Number of output elements
120 /// \param elemSize             Element size
121 /// \param swapMode             Endian Swap mode for data reading
122 /// \param fp                   Input file pointer.
123 /// \return true or false
124 ///
125 /// \donotcall \notthreadsafe \hostonly \enddonotcall
126 ///
127 GFD_DECLSPEC bool GFD_API GFDReadFileGPUData(void *pData, u32 numElements, GFDElementSize elemSize, GFDEndianSwapMode swapMode, FILE *fp);
128 
129 /// \brief Reads a CPU data from the file.
130 ///
131 /// \param pData                Output buffer pointer.
132 /// \param numElements          Number of output elements
133 /// \param elemSize             Element size
134 /// \param fp                   Input file pointer.
135 /// \return true or false
136 ///
137 /// \donotcall \notthreadsafe \hostonly \enddonotcall
138 ///
GFDReadFilePPCData(void * pData,u32 numElements,GFDElementSize elemSize,FILE * fp)139 GFD_DECLSPEC inline bool GFD_API GFDReadFilePPCData(void *pData, u32 numElements, GFDElementSize elemSize, FILE *fp)
140 {
141     return GFDReadFileGPUData(pData, numElements, elemSize, GFD_ENDIAN_SWAP_MODE_BIG, fp);
142 }
143 
144 /// \brief Checks File Header magic and version values.
145 ///
146 /// \param pHeader              Header pointer to check
147 /// \return true or false
148 ///
149 /// \donotcall \notthreadsafe \hostonly \enddonotcall
150 ///
GFDCheckHeaderMagicVersions(const GFDHeader * pHeader)151 GFD_DECLSPEC inline bool GFD_API GFDCheckHeaderMagicVersions(const GFDHeader *pHeader)
152 {
153     u32 magic = pHeader->magic;
154 
155     if (!(GFD_HEADER_MAGIC == GFD_SWAP_8_IN_32(magic)))
156     {
157         ASSERT(!"Unknown GFD format.");
158         return false;
159     }
160 
161     if (!(GFD_HEADER_MAJOR == pHeader->majorVersion))
162     {
163         ASSERT(!"Unsupported GFD version.");
164         return false;
165     }
166 
167     return true;
168 }
169 
170 /// \brief Checks Block Header magic and version values.
171 ///
172 /// \param pBlockHeader              Header pointer to check
173 /// \return true or false
174 ///
175 /// \donotcall \notthreadsafe \hostonly \enddonotcall
176 ///
GFDCheckBlockHeaderMagicVersions(const GFDBlockHeader * pBlockHeader)177 GFD_DECLSPEC inline bool GFD_API GFDCheckBlockHeaderMagicVersions(const GFDBlockHeader *pBlockHeader)
178 {
179     u32 magic = pBlockHeader->magic;
180 
181     if (!(GFD_BLOCK_HEADER_MAGIC == GFD_SWAP_8_IN_32(magic)))
182     {
183         ASSERT(!"Unknown GFD block format.");
184         return false;
185     }
186 
187     if (!(GFD_BLOCK_HEADER_MAJOR == pBlockHeader->majorVersion))
188     {
189         ASSERT(!"Unsupported GFD block version.");
190         return false;
191     }
192 
193     return true;
194 }
195 
196 /// @}
197 
198 #ifdef __cplusplus
199 }
200 #endif // __cplusplus
201 
202 #endif  // __GFDFILEIO_H__
203