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