libctru  v2.4.1
gpu.h
Go to the documentation of this file.
1 /**
2  * @file gpu.h
3  * @brief Barebones GPU communications driver.
4  */
5 #pragma once
6 
7 #include "registers.h"
8 #include "enums.h"
9 
10 /// Creates a GPU command header from its write increments, mask, and register.
11 #define GPUCMD_HEADER(incremental, mask, reg) (((incremental)<<31)|(((mask)&0xF)<<16)|((reg)&0x3FF))
12 
13 extern u32* gpuCmdBuf; ///< GPU command buffer.
14 extern u32 gpuCmdBufSize; ///< GPU command buffer size.
15 extern u32 gpuCmdBufOffset; ///< GPU command buffer offset.
16 
17 /**
18  * @brief Sets the GPU command buffer to use.
19  * @param adr Pointer to the command buffer.
20  * @param size Size of the command buffer.
21  * @param offset Offset of the command buffer.
22  */
23 static inline void GPUCMD_SetBuffer(u32* adr, u32 size, u32 offset)
24 {
25  gpuCmdBuf=adr;
26  gpuCmdBufSize=size;
27  gpuCmdBufOffset=offset;
28 }
29 
30 /**
31  * @brief Sets the offset of the GPU command buffer.
32  * @param offset Offset of the command buffer.
33  */
34 static inline void GPUCMD_SetBufferOffset(u32 offset)
35 {
36  gpuCmdBufOffset=offset;
37 }
38 
39 /**
40  * @brief Gets the current GPU command buffer.
41  * @param addr Pointer to output the command buffer to.
42  * @param size Pointer to output the size (in words) of the command buffer to.
43  * @param offset Pointer to output the offset of the command buffer to.
44  */
45 static inline void GPUCMD_GetBuffer(u32** addr, u32* size, u32* offset)
46 {
47  if(addr)*addr=gpuCmdBuf;
48  if(size)*size=gpuCmdBufSize;
49  if(offset)*offset=gpuCmdBufOffset;
50 }
51 
52 /**
53  * @brief Adds raw GPU commands to the current command buffer.
54  * @param cmd Buffer containing commands to add.
55  * @param size Size of the buffer.
56  */
57 void GPUCMD_AddRawCommands(const u32* cmd, u32 size);
58 
59 /**
60  * @brief Adds a GPU command to the current command buffer.
61  * @param header Header of the command.
62  * @param param Parameters of the command.
63  * @param paramlength Size of the parameter buffer.
64  */
65 void GPUCMD_Add(u32 header, const u32* param, u32 paramlength);
66 
67 /**
68  * @brief Splits the current GPU command buffer.
69  * @param addr Pointer to output the command buffer to.
70  * @param size Pointer to output the size (in words) of the command buffer to.
71  */
72 void GPUCMD_Split(u32** addr, u32* size);
73 
74 /**
75  * @brief Converts a 32-bit float to a 16-bit float.
76  * @param f Float to convert.
77  * @return The converted float.
78  */
79 u32 f32tof16(float f);
80 
81 /**
82  * @brief Converts a 32-bit float to a 20-bit float.
83  * @param f Float to convert.
84  * @return The converted float.
85  */
86 u32 f32tof20(float f);
87 
88 /**
89  * @brief Converts a 32-bit float to a 24-bit float.
90  * @param f Float to convert.
91  * @return The converted float.
92  */
93 u32 f32tof24(float f);
94 
95 /**
96  * @brief Converts a 32-bit float to a 31-bit float.
97  * @param f Float to convert.
98  * @return The converted float.
99  */
100 u32 f32tof31(float f);
101 
102 /// Adds a command with a single parameter to the current command buffer.
103 static inline void GPUCMD_AddSingleParam(u32 header, u32 param)
104 {
105  GPUCMD_Add(header, &param, 1);
106 }
107 
108 /// Adds a masked register write to the current command buffer.
109 #define GPUCMD_AddMaskedWrite(reg, mask, val) GPUCMD_AddSingleParam(GPUCMD_HEADER(0, (mask), (reg)), (val))
110 /// Adds a register write to the current command buffer.
111 #define GPUCMD_AddWrite(reg, val) GPUCMD_AddMaskedWrite((reg), 0xF, (val))
112 /// Adds multiple masked register writes to the current command buffer.
113 #define GPUCMD_AddMaskedWrites(reg, mask, vals, num) GPUCMD_Add(GPUCMD_HEADER(0, (mask), (reg)), (vals), (num))
114 /// Adds multiple register writes to the current command buffer.
115 #define GPUCMD_AddWrites(reg, vals, num) GPUCMD_AddMaskedWrites((reg), 0xF, (vals), (num))
116 /// Adds multiple masked incremental register writes to the current command buffer.
117 #define GPUCMD_AddMaskedIncrementalWrites(reg, mask, vals, num) GPUCMD_Add(GPUCMD_HEADER(1, (mask), (reg)), (vals), (num))
118 /// Adds multiple incremental register writes to the current command buffer.
119 #define GPUCMD_AddIncrementalWrites(reg, vals, num) GPUCMD_AddMaskedIncrementalWrites((reg), 0xF, (vals), (num))
GPU enumeration values.
static void GPUCMD_SetBuffer(u32 *adr, u32 size, u32 offset)
Sets the GPU command buffer to use.
Definition: gpu.h:23
void GPUCMD_Add(u32 header, const u32 *param, u32 paramlength)
Adds a GPU command to the current command buffer.
u32 gpuCmdBufOffset
GPU command buffer offset.
void GPUCMD_Split(u32 **addr, u32 *size)
Splits the current GPU command buffer.
u32 f32tof31(float f)
Converts a 32-bit float to a 31-bit float.
u32 f32tof16(float f)
Converts a 32-bit float to a 16-bit float.
u32 f32tof24(float f)
Converts a 32-bit float to a 24-bit float.
static void GPUCMD_GetBuffer(u32 **addr, u32 *size, u32 *offset)
Gets the current GPU command buffer.
Definition: gpu.h:45
u32 gpuCmdBufSize
GPU command buffer size.
void GPUCMD_AddRawCommands(const u32 *cmd, u32 size)
Adds raw GPU commands to the current command buffer.
static void GPUCMD_AddSingleParam(u32 header, u32 param)
Adds a command with a single parameter to the current command buffer.
Definition: gpu.h:103
u32 f32tof20(float f)
Converts a 32-bit float to a 20-bit float.
static void GPUCMD_SetBufferOffset(u32 offset)
Sets the offset of the GPU command buffer.
Definition: gpu.h:34
u32 * gpuCmdBuf
GPU command buffer.
@description GPU registers.
uint32_t u32
32-bit unsigned integer
Definition: types.h:23