citro3d
renderqueue.h
Go to the documentation of this file.
1 /**
2  * @file renderqueue.h
3  * @brief Set up rendertarget and render frame
4  */
5 #pragma once
6 #include "framebuffer.h"
7 
8 /// Render target structure
10 
12 {
13  C3D_RenderTarget *next, *prev;
14  C3D_FrameBuf frameBuf;
15 
16  bool used;
17  bool ownsColor, ownsDepth;
18 
19  bool linked;
20  gfxScreen_t screen;
21  gfx3dSide_t side;
22  u32 transferFlags;
23 };
24 
25 /// Flags for C3D_FrameBegin
26 enum
27 {
28  C3D_FRAME_SYNCDRAW = BIT(0), ///< Perform \ref C3D_FrameSync() before checking the GPU status
29  C3D_FRAME_NONBLOCK = BIT(1), ///< Return false instead of waiting if the GPU is busy
30 };
31 
32 /**
33  * @brief Specifies framerate cap.
34  * Specifies target framerate for \ref C3D_FrameSync().
35  * @param[in] fps Specifies the target framerate. Must be between 0.0 and 60.0 fps.
36  * @return Previous framerate.
37  */
38 float C3D_FrameRate(float fps);
39 
40 /**
41  * @brief Performs framerate limiting.
42  * Waits for the required amount of vblanks specified by \ref C3D_FrameRate().
43  * @note Used by \ref C3D_FrameBegin() when using \ref C3D_FRAME_SYNCDRAW.
44  */
45 void C3D_FrameSync(void);
46 
47 /**
48  * @brief Returns total number of frames drawn.
49  * @param[in] id Vblank frame counter id.
50  * @return Total number of frames.
51  */
53 
54 /**
55  * @brief Begins drawing frame.
56  * @param[in] flags Specifies options for rendering; 0 or more flags may be provided.
57  * \ref C3D_FRAME_SYNCDRAW specifies that \ref C3D_FrameSync() should be performed before checking the GPU status,
58  * \ref C3D_FRAME_NONBLOCK specifies that the function should return false instead of waiting for GPU to be ready.
59  * @return True if frame began successfully, otherwise false.
60  */
61 bool C3D_FrameBegin(u8 flags);
62 
63 /**
64  * @brief Specifies render target to draw frame to.
65  * @param[in] target Pointer to render target.
66  * @return True if rendertarget was set successfully, otherwise false.
67  */
69 
70 /**
71  * @brief Splits and submits the GPU cmdlist in the middle of a renderqueue frame.
72  * @param[in] flags Specifies 0 or more GX_CMDLIST flags.
73  */
74 void C3D_FrameSplit(u8 flags);
75 
76 /**
77  * @brief Ends drawing frame.
78  * @param[in] flags Specifies 0 or more GX_CMDLIST flags.
79  */
80 void C3D_FrameEnd(u8 flags);
81 
82 /**
83  * @brief Executes callback upon \ref C3D_FrameEnd().
84  * @param[in] hook Function callback.
85  * @param[in] param User data.
86  */
87 void C3D_FrameEndHook(void (* hook)(void*), void* param);
88 
89 /**
90  * @brief Gets time spent by the GPU during last render.
91  * @return Drawing time in milliseconds.
92  */
93 float C3D_GetDrawingTime(void);
94 
95 /**
96  * @brief Gets time elapsed between last \ref C3D_FrameBegin() and \ref C3D_FrameEnd().
97  * @return Time in milliseconds.
98  */
100 
101 #if defined(__GNUC__) && !defined(__cplusplus)
102 typedef union __attribute__((__transparent_union__))
103 {
104  int __i;
105  GPU_DEPTHBUF __e;
106 } C3D_DEPTHTYPE;
107 #else
109 {
110 private:
111  int __i;
112  GPU_DEPTHBUF __e;
113 public:
114  C3D_DEPTHTYPE(GPU_DEPTHBUF e) : __e(e) {}
115  C3D_DEPTHTYPE(int i) : __i(-1) { (void)i; }
116 };
117 #endif
118 
119 #define C3D_DEPTHTYPE_OK(_x) ((_x).__i >= 0)
120 #define C3D_DEPTHTYPE_VAL(_x) ((_x).__e)
121 
122 /**
123  * @brief Creates a new render target.
124  * @note When creating a rendertarget intended for display, keep in mind the orientation of the screens.
125  * When you hold a 3DS normally, the screens are rotated 90 degrees counter-clockwise.
126  * @param[in] width Specifies width of the render target in pixels.
127  * @param[in] height Specifies height of the render target in pixels.
128  * @param[in] colorFmt Specifies the color format of the render target.
129  * @param[in] depthFmt Specifies the depth format of the render target using \ref GPU_DEPTHBUF. (-1 for no depth type)
130  * @return Pointer to newly created render target.
131  */
132 C3D_RenderTarget* C3D_RenderTargetCreate(int width, int height, GPU_COLORBUF colorFmt, C3D_DEPTHTYPE depthFmt);
133 
134 /**
135  * @brief Constructs render target for texture.
136  * @param[in] tex Pointer to \ref C3D_Tex.
137  * @param[in] face Specifies face of cubemap to be used. (GPU_TEXFACE_2D if not cubemap)
138  * @param[in] level Specifies mipmap level to use.
139  * @param[in] depthFmt Specifies the depth format of the render target using \ref GPU_DEPTHBUF. (-1 for no depth type)
140  * @return Pointer to newly created render target.
141  */
143 
144 /**
145  * @brief Deletes render target.
146  * @param[in] target Pointer to render target.
147  */
149 
150 /**
151  * @brief Sets render target output to screen.
152  * @param[in] target Pointer to render target.
153  * @param[in] screen Screen to transfer the framebuffer to.
154  * @param[in] side Side of the screen to transfer the framebuffer to. (unused for the bottom screen)
155  * @param[in] transferFlags Specifies GX_TRANSFER bitflags.
156  */
157 void C3D_RenderTargetSetOutput(C3D_RenderTarget* target, gfxScreen_t screen, gfx3dSide_t side, u32 transferFlags);
158 
159 /**
160  * @brief Detaches render target from screen output.
161  * @param[in] target Pointer to render target.
162  */
164 {
165  C3D_RenderTargetSetOutput(NULL, target->screen, target->side, 0);
166 }
167 
168 /**
169  * @brief Clears framebuffer of target.
170  * @param[in] target Pointer to render target.
171  * @param[in] clearBits Specifies which buffers to clear. (see \ref C3D_ClearBits)
172  * @param[in] clearColor 32 bit RGBA value to clear color buffer with.
173  * @param[in] clearDepth Value to clear depth buffer with.
174  */
175 static inline void C3D_RenderTargetClear(C3D_RenderTarget* target, C3D_ClearBits clearBits, u32 clearColor, u32 clearDepth)
176 {
177  C3D_FrameBufClear(&target->frameBuf, clearBits, clearColor, clearDepth);
178 }
179 
180 /**
181  * @brief Synchronizes and initiates a display transfer.
182  * Synchronizes and initiates a \ref GX_DisplayTransfer().
183  * @param[in] inadr Address of the input.
184  * @param[in] indim \ref GX_BUFFER_DIM() of the input.
185  * @param[out] outadr Address of the output.
186  * @param[in] outdim \ref GX_BUFFER_DIM() of the output.
187  * @param[in] flags Flags to transfer with.
188  */
189 void C3D_SyncDisplayTransfer(u32* inadr, u32 indim, u32* outadr, u32 outdim, u32 flags);
190 
191 /**
192  * @brief Synchronizes and initiates a texture copy.
193  * Synchronizes and initiates a \ref GX_TextureCopy().
194  * @param[in] inadr Address of the input.
195  * @param[in] indim \ref GX_BUFFER_DIM() of the input.
196  * @param[out] outadr Address of the output.
197  * @param[in] outdim \ref GX_BUFFER_DIM() of the output.
198  * @param[in] size Size of the data to transfer.
199  * @param[in] flags Flags to transfer with.
200  */
201 void C3D_SyncTextureCopy(u32* inadr, u32 indim, u32* outadr, u32 outdim, u32 size, u32 flags);
202 
203 /**
204  * @brief Synchronizes and fills the memory of two buffers with the given values.
205  * Synchronizes and initiates a \ref GX_MemoryFill().
206  * @param[in] buf0a Start address of the first buffer.
207  * @param[in] buf0v Value to fill first buffer.
208  * @param[in] buf0e End address of the first buffer.
209  * @param[in] control0 Value to fill the first buffer with.
210  * @param[in] buf1a Start address of the second buffer.
211  * @param[in] buf1v Value to fill second buffer.
212  * @param[in] buf1e End address of the second buffer.
213  * @param[in] control1 Value to fill the second buffer with.
214  */
215 void C3D_SyncMemoryFill(u32* buf0a, u32 buf0v, u32* buf0e, u16 control0, u32* buf1a, u32 buf1v, u32* buf1e, u16 control1);
GPU_COLORBUF
GPU_TEXFACE
GPU_DEPTHBUF
Process render target framebuffer.
void C3D_FrameBufClear(C3D_FrameBuf *fb, C3D_ClearBits clearBits, u32 clearColor, u32 clearDepth)
Sets the clear bits and color for a framebuffer.
C3D_ClearBits
Flags for C3D_FrameBufClear.
Definition: framebuffer.h:23
gfxScreen_t
gfx3dSide_t
__attribute__((warn_unused_result)) rbtree_node_t *rbtree_insert(rbtree_t *tree
void C3D_FrameEndHook(void(*hook)(void *), void *param)
Executes callback upon C3D_FrameEnd().
static void C3D_RenderTargetDetachOutput(C3D_RenderTarget *target)
Detaches render target from screen output.
Definition: renderqueue.h:163
bool C3D_FrameDrawOn(C3D_RenderTarget *target)
Specifies render target to draw frame to.
C3D_RenderTarget * C3D_RenderTargetCreateFromTex(C3D_Tex *tex, GPU_TEXFACE face, int level, C3D_DEPTHTYPE depthFmt)
Constructs render target for texture.
void C3D_FrameSplit(u8 flags)
Splits and submits the GPU cmdlist in the middle of a renderqueue frame.
void C3D_SyncTextureCopy(u32 *inadr, u32 indim, u32 *outadr, u32 outdim, u32 size, u32 flags)
Synchronizes and initiates a texture copy. Synchronizes and initiates a GX_TextureCopy().
void C3D_FrameSync(void)
Performs framerate limiting. Waits for the required amount of vblanks specified by C3D_FrameRate().
float C3D_GetProcessingTime(void)
Gets time elapsed between last C3D_FrameBegin() and C3D_FrameEnd().
float C3D_GetDrawingTime(void)
Gets time spent by the GPU during last render.
u32 C3D_FrameCounter(int id)
Returns total number of frames drawn.
static void C3D_RenderTargetClear(C3D_RenderTarget *target, C3D_ClearBits clearBits, u32 clearColor, u32 clearDepth)
Clears framebuffer of target.
Definition: renderqueue.h:175
bool C3D_FrameBegin(u8 flags)
Begins drawing frame.
void C3D_SyncMemoryFill(u32 *buf0a, u32 buf0v, u32 *buf0e, u16 control0, u32 *buf1a, u32 buf1v, u32 *buf1e, u16 control1)
Synchronizes and fills the memory of two buffers with the given values. Synchronizes and initiates a ...
C3D_RenderTarget * C3D_RenderTargetCreate(int width, int height, GPU_COLORBUF colorFmt, C3D_DEPTHTYPE depthFmt)
Creates a new render target.
float C3D_FrameRate(float fps)
Specifies framerate cap. Specifies target framerate for C3D_FrameSync().
void C3D_RenderTargetSetOutput(C3D_RenderTarget *target, gfxScreen_t screen, gfx3dSide_t side, u32 transferFlags)
Sets render target output to screen.
@ C3D_FRAME_SYNCDRAW
Perform C3D_FrameSync() before checking the GPU status.
Definition: renderqueue.h:28
@ C3D_FRAME_NONBLOCK
Return false instead of waiting if the GPU is busy.
Definition: renderqueue.h:29
void C3D_SyncDisplayTransfer(u32 *inadr, u32 indim, u32 *outadr, u32 outdim, u32 flags)
Synchronizes and initiates a display transfer. Synchronizes and initiates a GX_DisplayTransfer().
void C3D_RenderTargetDelete(C3D_RenderTarget *target)
Deletes render target.
void C3D_FrameEnd(u8 flags)
Ends drawing frame.
Definition: framebuffer.h:9
Definition: renderqueue.h:12
Texture data.
Definition: texture.h:16
#define BIT(n)
uint8_t u8
uint16_t u16
uint32_t u32
Definition: renderqueue.h:109