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 _GX2UTRESOLVE_H_
14 #define _GX2UTRESOLVE_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 GX2UTResolveAAColorBuffer.
28 ///
29 /// \param enable Enable or disable the state
30 ///
31 /// This enables and disables the state used by \ref GX2UTResolveAAColorBuffer.
32 /// After finishing all resolve operations, it is necessary to call
33 /// this function to disable the resolve state and set any changed state
34 /// back to the GX2 default state. A smaller yet equivalent routine can be
35 /// written using knowledge of the application's desired state.
36 /// To customize the state that is restored, refer to the library source
37 /// (src/lib/gx2ut/gx2utResolve.cpp).
38 ///
39 /// \donotcall \gx2_typical \enddonotcall
40 ///
41 /// \clobberstate
42 ///
43 /// \writesgpu
44 /// \alwayswritesgpu
45 ///
46 void GX2UTSetResolveAAColorState(GX2Boolean enable);
47
48 /// \brief Resolves a MSAA color buffer to a single-sampled surface.
49 ///
50 /// \param cb Pointer to color buffer surface structure
51 /// \param dstSurface Destination color/texture surface.
52 /// \param dstMip Destination mipmap level for dstSurface.
53 /// \param dstSlice Destination slice for dstSurface.
54 /// \param subRect Pointer to the region to resolve.
55 ///
56 /// This function assumes \ref GX2UTSetResolveAAColorState
57 /// (or comparable state setup function) has been called to setup required
58 /// render state.
59 ///
60 /// This resolves a MSAA color buffer to a single-sampled surface in the same
61 /// way as \ref GX2ResolveAAColorBuffer. It modifies the currently bound VS/PS
62 /// shaders, GX2_RENDER_TARGET0, GX2_RENDER_TARGET1, and Scissor/Viewport
63 /// settings. In addition it overwrites VS uniform locations 0-3, and it
64 /// changes the \ref GX2SetShaderMode to
65 /// \ref GX2_SHADER_MODE_UNIFORM_REGISTER.
66 ///
67 /// Resolves a region from one surface to a region of another surface. Setting
68 /// the rectangular regions to a top-left of (0,0) and a bottom-right of (width,
69 /// height) will resolve the entire surface without any flipping (the right and
70 /// bottom are exclusive).
71 ///
72 /// The subRect dimensions should be relative to the mipmap level dimensions,
73 /// not the base level dimensions. The subRect origin (left, top) must be
74 /// aligned on a 2x2 pixel boundary.
75 ///
76 /// \note It is not necessary to call \ref GX2Invalidate on the
77 /// source/destination buffer. This is done internally.
78 ///
79 /// \note This routine has the same limitations as \ref GX2ResolveAAColorBuffer.
80 /// \ref GX2IsResolveSupported can be used to determine if a source
81 /// GX2ColorBuffer can be resolved.
82 ///
83 /// \note This resolve, as it is, will be impacted by the toss stage
84 /// (\ref GX2TossStageSect) setup at GX2Init time. To avoid this, call
85 /// this function from a separate state context with profiling disabled.
86 /// See \ref GX2SetupContextStateEx for more information.
87 ///
88 /// \warning The color buffer must not be an AA buffer and the destination
89 /// surface must be a non-AA color/texture surface.
90 ///
91 /// \clobberstate
92 ///
93 /// \donotcall \gx2_typical \enddonotcall
94 ///
95 /// \writesgpu
96 /// \alwayswritesgpu
97 ///
98 void GX2UTResolveAAColorBufferRectOp(const GX2ColorBuffer *cb,
99 GX2Surface *dstSurface,
100 u32 dstMip, u32 dstSlice,
101 GX2UTRect * subRect);
102
103 /// \brief Resolves a MSAA color buffer to a single-sampled surface.
104 ///
105 /// \param cb Pointer to color buffer surface structure
106 /// \param dstSurface Destination color/texture surface.
107 /// \param dstMip Destination mipmap level for dstSurface.
108 /// \param dstSlice Destination slice for dstSurface.
109 ///
110 /// This function assumes \ref GX2UTSetResolveAAColorState
111 /// (or comparable state setup function) has been called to setup required
112 /// render state.
113 ///
114 /// This resolves a MSAA color buffer to a single-sampled surface in the same
115 /// way as \ref GX2ResolveAAColorBuffer. It modifies the currently bound VS/PS
116 /// shaders, GX2_RENDER_TARGET0, GX2_RENDER_TARGET1, and Scissor/Viewport
117 /// settings. In addition it overwrites VS uniform locations 0-3, and it
118 /// changes the \ref GX2SetShaderMode to
119 /// \ref GX2_SHADER_MODE_UNIFORM_REGISTER.
120 ///
121 /// \note It is not necessary to call \ref GX2Invalidate on the
122 /// source/destination buffer. This is done internally.
123 ///
124 /// \note This routine has the same limitations as \ref GX2ResolveAAColorBuffer.
125 /// \ref GX2IsResolveSupported can be used to determine if a source
126 /// GX2ColorBuffer can be resolved.
127 ///
128 /// \note This resolve, as it is, will be impacted by the toss stage
129 /// (\ref GX2TossStageSect) setup at GX2Init time. To avoid this, call
130 /// this function from a separate state context with profiling disabled.
131 /// See \ref GX2SetupContextStateEx for more information.
132 ///
133 /// \warning The color buffer must not be an AA buffer and the destination
134 /// surface must be a non-AA color/texture surface.
135 ///
136 /// \clobberstate
137 ///
138 /// \donotcall \gx2_typical \enddonotcall
139 ///
140 /// \writesgpu
141 /// \alwayswritesgpu
142 ///
GX2UTResolveAAColorBufferOp(const GX2ColorBuffer * cb,GX2Surface * dstSurface,u32 dstMip,u32 dstSlice)143 GX2_INLINE void GX2UTResolveAAColorBufferOp(const GX2ColorBuffer *cb,
144 GX2Surface *dstSurface, u32 dstMip,
145 u32 dstSlice)
146 {
147 GX2UTRect subRect = {0};
148
149 subRect.right = GX2Max(1, dstSurface->width >> dstMip);
150 subRect.bottom = GX2Max(1, dstSurface->height >> dstMip);
151
152 GX2UTDebugTagIndent(__func__);
153
154 GX2UTResolveAAColorBufferRectOp(cb, dstSurface, dstMip, dstSlice, &subRect);
155
156 GX2UTDebugTagUndent();
157 }
158
159
160 /// \brief Resolves a MSAA color buffer to a single-sampled surface.
161 ///
162 /// \param cb Pointer to color buffer surface structure
163 /// \param dstSurface Destination color/texture surface.
164 /// \param dstMip Destination mipmap level for dstSurface.
165 /// \param dstSlice Destination slice for dstSurface.
166 ///
167 /// This function mimics the behavior of GX2 and is intended to be an optimized
168 /// replacement for \ref GX2ResolveAAColorBuffer.
169 ///
170 /// \note This API changes rendering states and disables state shadowing.
171 /// If you are using state shadowing, you must call
172 /// \ref GX2SetContextState() afterward.
173 ///
174 /// \note This routine has the same limitations as \ref GX2ResolveAAColorBuffer.
175 /// \ref GX2IsResolveSupported can be used to determine if a source
176 /// GX2ColorBuffer can be resolved.
177 ///
178 /// \warning Better performing alternatives exist. Please see
179 /// \ref GX2UTAdvancedGX2ReplacementAPISect.
180 ///
181 /// \clobberstate
182 /// \disablesstateshadow
183 ///
184 /// \donotcall \gx2_typical \enddonotcall
185 ///
186 /// \writesgpu
187 /// \alwayswritesgpu
188 ///
GX2UTResolveAAColorBuffer(const GX2ColorBuffer * cb,GX2Surface * dstSurface,u32 dstMip,u32 dstSlice)189 GX2_INLINE void GX2UTResolveAAColorBuffer(const GX2ColorBuffer *cb,
190 GX2Surface *dstSurface, u32 dstMip,
191 u32 dstSlice)
192 {
193 GX2UTDebugTagIndent(__func__);
194
195 // Disable state shadowing. If your app is using state shadowing,
196 // you will need to restore the context after calling this function.
197 GX2SetContextState(NULL);
198
199 // Setup all of the constant render state needed for the color resolve.
200 GX2UTSetResolveAAColorState(GX2_ENABLE);
201
202 // Setup parameter specific render state and color resolve.
203 GX2UTResolveAAColorBufferOp(cb, dstSurface, dstMip, dstSlice);
204
205 // Disable special hardware state and set state back to GX2 default
206 GX2UTSetResolveAAColorState(GX2_DISABLE);
207
208 GX2UTDebugTagUndent();
209 }
210
211
212 /// @}
213
214 #ifdef __cplusplus
215 }
216 #endif // __cplusplus
217
218 #endif // _GX2UTRESOLVE_H_
219