1 /*---------------------------------------------------------------------------*
2 
3   Copyright 2014 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 #ifndef _GX2UTEXPAND_H_
14 #define _GX2UTEXPAND_H_
15 
16 // Note: Currently this header depends on gx2.h being included first to define
17 // types like s32 and structures like GX2Surface.
18 #include <cafe/gx2.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /// @addtogroup GX2UTCopyAndConvertGroup
25 /// @{
26 
27 /// \brief Setup all of the constant render state needed for \ref GX2UTExpandAAColorBuffer.
28 ///
29 /// \param enable Enable or disable the state
30 ///
31 /// This enables and disables the state used by \ref GX2UTExpandAAColorBufferOp
32 /// After finishing all expand operations, it is necessary to call
33 /// this function to disable the clear state and set any changed state
34 /// to the GX2 default state. A smaller yet equivalent routine can be
35 /// written using knowledge of the application's desired state. To customize
36 /// the state that is restored, refer to the library source
37 /// (src/lib/gx2ut/gx2utExpandColor.cpp).
38 ///
39 /// \donotcall \gx2_typical \enddonotcall
40 ///
41 /// \clobberstate
42 ///
43 /// \writesgpu
44 /// \alwayswritesgpu
45 ///
46 void GX2UTSetExpandAAColorState(GX2Boolean enable);
47 
48 /// \brief Expand MSAA color buffer for use as a texture.
49 ///
50 /// \param cb MSAA color buffer to expand
51 ///
52 /// This function assumes \ref GX2UTSetExpandAAColorState
53 /// (or comparable state setup function) has been called to setup required
54 /// render state.
55 ///
56 /// This expands a compressed color buffer in the same way as
57 /// \ref GX2ExpandAAColorBuffer. It modifies the current GX2 state for
58 /// all shaders, GX2_RENDER_TARGET0, and Scissor/Viewport settings.
59 ///
60 /// \note It is not necessary to call \ref GX2Invalidate on the
61 ///        color buffer. This is done internally.
62 ///
63 /// \note This expand, as it is, will be impacted by the toss stage
64 ///       (\ref GX2TossStageSect) setup at GX2Init time. To avoid this, call
65 ///       this function from a separate state context with profiling disabled.
66 ///       See \ref GX2SetupContextStateEx for more information.
67 ///
68 /// \donotcall \gx2_typical \enddonotcall
69 ///
70 /// \clobberstate
71 ///
72 /// \writesgpu
73 /// \alwayswritesgpu
74 ///
75 void GX2UTExpandAAColorBufferOp(GX2ColorBuffer *cb);
76 
77 
78 /// \brief Expands a MSAA color buffer so it can be used as a texture.
79 ///
80 /// \param db Compressed (HiZ) depth buffer to expand
81 ///
82 /// This function mimics the behavior of GX2 and is intended to be an optimized
83 /// replacement for \ref GX2ExpandAAColorBuffer.
84 ///
85 /// \note It is not necessary to call \ref GX2Invalidate on the
86 ///        color buffer. This is done internally.
87 ///
88 /// \note This API changes rendering states and disables state shadowing.
89 ///       If you are using state shadowing, you must call
90 ///       \ref GX2SetContextState() afterward.
91 ///
92 /// \warning Better performing alternatives exist. Please see
93 ///          \ref GX2UTAdvancedGX2ReplacementAPISect.
94 ///
95 /// \clobberstate
96 /// \disablesstateshadow
97 ///
98 /// \donotcall \gx2_typical \enddonotcall
99 ///
100 /// \writesgpu
101 /// \alwayswritesgpu
102 ///
GX2UTExpandAAColorBuffer(GX2ColorBuffer * db)103 GX2_INLINE void GX2UTExpandAAColorBuffer(GX2ColorBuffer *db)
104 {
105     GX2UTDebugTagIndent(__func__);
106 
107     // Disable state shadowing.  If your app is using state shadowing,
108     // you will need to restore the context after calling this function.
109     GX2SetContextState(NULL);
110 
111     // Setup all of the constant render state needed for the color expand.
112     GX2UTSetExpandAAColorState(GX2_ENABLE);
113 
114     // Setup parameter specific render state and color expand.
115     GX2UTExpandAAColorBufferOp(db);
116 
117     // Disable special hardware state and set state back to GX2 default
118     GX2UTSetExpandAAColorState(GX2_DISABLE);
119 
120     GX2UTDebugTagUndent();
121 }
122 
123 /// \brief Setup all of the constant render state needed for \ref GX2UTExpandDepthBuffer.
124 ///
125 /// \param enable Enable or disable the state
126 ///
127 /// This enables and disables the state used by \ref GX2UTExpandDepthBuffer.
128 /// After finishing all expand operations, it is necessary to call
129 /// this function to disable the expand state and set any changed state
130 /// back to the GX2 default state. A smaller yet equivalent routine can be
131 /// written using knowledge of the application's desired state.
132 /// To customize the state that is restored, refer to the library source
133 /// (src/lib/gx2ut/gx2utExpandDepth.cpp).
134 ///
135 /// \donotcall \gx2_typical \enddonotcall
136 ///
137 /// \clobberstate
138 ///
139 /// \writesgpu
140 /// \alwayswritesgpu
141 ///
142 void GX2UTSetExpandDepthState(GX2Boolean enable);
143 
144 /// \brief Expand compressed depth buffer for use as a texture.
145 ///
146 /// \param db Compressed (HiZ) depth buffer to expand
147 ///
148 /// This function assumes \ref GX2UTSetExpandDepthState
149 /// (or comparable state setup function) has been called to setup required
150 /// render state.
151 ///
152 /// This expands a compressed (HiZ) depth buffer in the same way as
153 /// \ref GX2ExpandDepthBuffer. It modifies the curent GX2 state for all
154 /// shaders, depth buffer, and Scissor/Viewport settings
155 ///
156 /// \warning Subsequent draw calls to this buffer will undo the expand, so this
157 /// must be called between drawing to the buffer and texturing from it.
158 ///
159 /// \warning The depth buffer must not be an AA buffer or
160 ///          \ref GX2_SURFACE_FORMAT_D_D24_S8_UNORM. Please use
161 ///          \ref GX2UTConvertDepthBufferToTextureSurface instead.
162 ///
163 /// \clobberstate
164 ///
165 /// \note It is not necessary to call \ref GX2Invalidate on the
166 ///       depth buffer. This is done internally.
167 ///
168 /// \note This expand, as it is, will be impacted by the toss stage
169 ///       (\ref GX2TossStageSect) setup at GX2Init time. To avoid this, call
170 ///       this function from a separate state context with profiling disabled.
171 ///       See \ref GX2SetupContextStateEx for more information.
172 ///
173 /// \donotcall \gx2_typical \enddonotcall
174 ///
175 /// \writesgpu
176 /// \alwayswritesgpu
177 ///
178 void GX2UTExpandDepthBufferOp(GX2DepthBuffer *db);
179 
180 
181 /// \brief Expands a compressed depth buffer (HiZ enabled) so it can be used
182 /// as a texture.
183 ///
184 /// \param db Compressed (HiZ) depth buffer to expand
185 ///
186 /// This function mimics the behavior of GX2 and is intended to be an optimized
187 /// replacement for \ref GX2ExpandDepthBuffer.
188 ///
189 /// \note It is not necessary to call \ref GX2Invalidate on the
190 ///        color buffer. This is done internally.
191 ///
192 /// \note This API changes rendering states and disables state shadowing.
193 ///       If you are using state shadowing, you must call
194 ///       \ref GX2SetContextState() afterward.
195 ///
196 /// \warning Better performing alternatives exist. Please see
197 ///          \ref GX2UTAdvancedGX2ReplacementAPISect.
198 ///
199 /// \clobberstate
200 /// \disablesstateshadow
201 ///
202 /// \donotcall \gx2_typical \enddonotcall
203 ///
204 /// \writesgpu
205 /// \alwayswritesgpu
206 ///
GX2UTExpandDepthBuffer(GX2DepthBuffer * db)207 GX2_INLINE void GX2UTExpandDepthBuffer(GX2DepthBuffer *db)
208 {
209     GX2UTDebugTagIndent(__func__);
210 
211     // Disable state shadowing.  If your app is using state shadowing,
212     // you will need to restore the context after calling this function.
213     GX2SetContextState(NULL);
214 
215     // Setup all of the constant render state needed for the depth expand.
216     GX2UTSetExpandDepthState(GX2_ENABLE);
217 
218     // Setup parameter specific render state and depth expand.
219     GX2UTExpandDepthBufferOp(db);
220 
221     // Disable special hardware state and set state back to GX2 default
222     GX2UTSetExpandDepthState(GX2_DISABLE);
223 
224     GX2UTDebugTagUndent();
225 }
226 
227 /// \brief Setup all of the constant render state needed for \ref GX2UTConvertDepthBufferToTextureSurface.
228 ///
229 /// \param enable Enable or disable the state
230 ///
231 /// This enables and disables the state used by
232 /// \ref GX2UTConvertDepthBufferToTextureSurface. After fnishing all convert
233 /// operations, it is necessary to call this function to disable the convert
234 /// state and set any changed state to the GX2 default state. A smaller yet
235 /// equivalent routine can be written using knowledge of the application's
236 /// desired state. To customize the state that is restored, refer to the library
237 /// source (src/lib/gx2ut/gx2utConvertDepth.cpp).
238 ///
239 /// \donotcall \gx2_typical \enddonotcall
240 ///
241 /// \clobberstate
242 ///
243 /// \writesgpu
244 /// \alwayswritesgpu
245 ///
246 void GX2UTSetConvertDepthState(GX2Boolean enable);
247 
248 /// \brief Copy and Convert a depth buffer so it a format readable as a texture.
249 ///
250 /// \param srcBuffer  Pointer to depth buffer surface structure
251 /// \param dstSurface Pointer to destination surface structure
252 /// \param dstMip Mip level of the destination surface to write
253 /// \param dstSlice Slice to begin writing source slices to
254 ///
255 /// This function assumes \ref GX2UTSetConvertDepthState
256 /// (or comparable state setup function) has been called to setup required
257 /// render state.
258 ///
259 /// This copies a depth buffer to another surface while changing from
260 /// format-tiling to texture/color-format tiling. This is only necessary
261 /// for depth formats that cannot be read by the texture unit (MSAA or
262 /// \ref GX2_SURFACE_FORMAT_D_D24_S8_UNORM format). This routine behaves the
263 /// same as \ref GX2ConvertDepthBufferToTextureSurface. See this API for more
264 /// details.
265 ///
266 /// This routine modifies the current GX2 state for all shaders,
267 /// GX2_RENDER_TARGET0, depth buffer, and Scissor/Viewport Settings.
268 ///
269 /// \note It is not necessary to call \ref GX2Invalidate on the
270 ///       source/destination buffer. This is done internally.
271 ///
272 /// \note This convert, as it is, will be impacted by the toss stage
273 ///       (\ref GX2TossStageSect) setup at GX2Init time. To avoid this, call
274 ///       this function from a separate state context with profiling disabled.
275 ///       See \ref GX2SetupContextStateEx for more information.
276 ///
277 /// \donotcall \gx2_typical \enddonotcall
278 ///
279 /// \clobberstate
280 ///
281 /// \writesgpu
282 /// \alwayswritesgpu
283 ///
284 void GX2UTConvertDepthBufferToTextureSurfaceOp(const GX2DepthBuffer* srcBuffer,
285                                                GX2Surface* dstSurface,
286                                                u32 dstMip, u32 dstSlice);
287 
288 
289 /// \brief Copy and Convert a depth buffer so it a format readable as a texture.
290 ///
291 /// \param srcBuffer  Pointer to depth buffer surface structure
292 /// \param dstSurface Pointer to destination surface structure
293 /// \param dstMip Mip level of the destination surface to write
294 /// \param dstSlice Slice to begin writing source slices to
295 ///
296 /// This function mimics the behavior of GX2 and is intended to be an optimized
297 /// replacement for \ref GX2ConvertDepthBufferToTextureSurface.
298 ///
299 /// \note It is not necessary to call \ref GX2Invalidate on the
300 ///       source/destination buffer. This is done internally.
301 ///
302 /// \note This API changes rendering states and disables state shadowing.
303 ///       If you are using state shadowing, you must call
304 ///       \ref GX2SetContextState() afterward.
305 ///
306 /// \warning Better performing alternatives exist. Please see
307 ///          \ref GX2UTAdvancedGX2ReplacementAPISect.
308 ///
309 /// \clobberstate
310 /// \disablesstateshadow
311 ///
312 /// \donotcall \gx2_typical \enddonotcall
313 ///
314 /// \writesgpu
315 /// \alwayswritesgpu
316 ///
GX2UTConvertDepthBufferToTextureSurface(const GX2DepthBuffer * srcBuffer,GX2Surface * dstSurface,u32 dstMip,u32 dstSlice)317 GX2_INLINE void GX2UTConvertDepthBufferToTextureSurface(
318                                                const GX2DepthBuffer* srcBuffer,
319                                                GX2Surface* dstSurface,
320                                                u32 dstMip, u32 dstSlice)
321 {
322     GX2UTDebugTagIndent(__func__);
323 
324     // Disable state shadowing.  If your app is using state shadowing,
325     // you will need to restore the context after calling this function.
326     GX2SetContextState(NULL);
327 
328     // Setup all of the constant render state needed for the depth expand.
329     GX2UTSetConvertDepthState(GX2_ENABLE);
330 
331     // Setup parameter specific render state and depth convert.
332     GX2UTConvertDepthBufferToTextureSurfaceOp(srcBuffer, dstSurface, dstMip, dstSlice);
333 
334     // Disable special hardware state and set state back to GX2 default
335     GX2UTSetConvertDepthState(GX2_DISABLE);
336 
337     GX2UTDebugTagUndent();
338 }
339 
340 
341 /// @}
342 
343 #ifdef __cplusplus
344 }
345 #endif // __cplusplus
346 
347 #endif // _GX2UTEXPAND_H_
348