1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 2010-2011 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 // gx2Draw.h
14 //
15 // Declares draw-related function prototypes for gx2 library.
16 
17 #ifndef _CAFE_GX2_DRAW_H_
18 #define _CAFE_GX2_DRAW_H_
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif // __cplusplus
24 
25 /// @addtogroup GX2DrawGroup
26 /// @{
27 
28 /// @addtogroup GX2DrawClearGroup
29 /// @{
30 
31 /// \brief Clears the given render target to the specified color.
32 ///
33 /// \note This API changes rendering states and disables state shadowing.
34 ///       If you are using state shadowing, you must call
35 ///       \ref GX2SetContextState() afterward.
36 ///
37 /// \note This function clears only the single miplevel specified by
38 ///       colorBuffer->viewMip. This function clears the number of slices
39 ///       specified by colorBuffer->viewNumSlices starting with slice number
40 ///       colorBuffer->viewFirstSlice.
41 ///
42 /// \param colorBuffer pointer to color buffer to clear
43 /// \param r           Red color component of clear value
44 /// \param g           Green color component of clear value
45 /// \param b           Blue color component of clear value
46 /// \param a           Alpha color component of clear value
47 ///
48 /// \donotcall \gx2_typical \enddonotcall
49 ///
50 /// \clobberstate
51 /// \disablesstateshadow
52 /// \notincompute
53 ///
54 /// \writesgpu
55 /// \alwayswritesgpu
56 ///
57 void GX2API GX2ClearColor(GX2ColorBuffer *colorBuffer,
58                           f32 r, f32 g, f32 b, f32 a);
59 
60 /// \brief Clear the depth/stencil surface with given depth and stencil values.
61 ///
62 /// \param depthBuffer   pointer to depth/stencil buffer to clear
63 /// \param depthValue    Depth clear value (from 0.0 to 1.0)
64 /// \param stencilValue  Stencil clear value (from 0 to 255)
65 /// \param clearFlags    Indicates if depth and/or stencil buffer should be cleared
66 ///
67 /// \note This function clears only the single miplevel specified by
68 ///       depthBuffer->viewMip. This function clears the number of slices
69 ///       specified by depthBuffer->viewNumSlices starting with slice number
70 ///       depthBuffer->viewFirstSlice.
71 ///
72 /// \note To generate valuable HiStencil pretest results while clearing the
73 ///       stencil buffer, please see \ref GX2UTClearRectOp.
74 ///
75 /// \donotcall \gx2_typical \enddonotcall
76 ///
77 /// \clobberstate
78 /// \disablesstateshadow
79 /// \notincompute
80 ///
81 /// \writesgpu
82 /// \alwayswritesgpu
83 ///
84 void GX2API GX2ClearDepthStencilEx(GX2DepthBuffer *depthBuffer,
85                                    f32 depthValue,
86                                    u8 stencilValue,
87                                    GX2ClearMode clearFlags);
88 
89 /// \brief Clears the given render target and depth/stencil surface.
90 ///
91 /// \note This API changes rendering states and disables state shadowing.
92 ///       If you are using state shadowing, you must call
93 ///       \ref GX2SetContextState() afterward.
94 /// \note Is is also necessary to call \ref GX2SetClearDepthStencil()
95 ///       to set the depth/stencil buffer's clear values.
96 /// \note The colorBuffer and depthBuffer must have the same width, height, viewNumSlices,
97 ///       and number of MSAA samples.
98 /// \note This function clears only the single miplevel specified by colorBuffer->viewMip for the color buffer
99 ///       and depthBuffer->viewMip for the depth buffer.
100 ///       colorBuffer->viewNumSlices must match depthBuffer->viewNumSlices.
101 ///       This function clears the number of slices specified by colorBuffer->viewNumSlices starting with slice number
102 ///       colorBuffer->viewFirstSlice, for the color buffer, and depthBuffer->viewFirstSlice, for the depth buffer.
103 /// \note To generate valuable HiStencil pretest results while clearing the stencil buffer,
104 ///       please see \ref GX2UTClearSurface.
105 ///
106 /// \param colorBuffer   pointer to color buffer to clear
107 /// \param depthBuffer   pointer to depth/stencil buffer to clear
108 /// \param r             Red color component of clear value
109 /// \param g             Green color component of clear value
110 /// \param b             Blue color component of clear value
111 /// \param a             Alpha color component of clear value
112 /// \param depthValue    Depth clear value (from 0.0 to 1.0)
113 /// \param stencilValue  Stencil clear value (from 0 to 255)
114 /// \param clearFlags    Indicates if depth and/or stencil buffer should be cleared
115 ///
116 /// \donotcall \gx2_typical \enddonotcall
117 ///
118 /// \clobberstate
119 /// \disablesstateshadow
120 /// \notincompute
121 ///
122 /// \writesgpu
123 /// \alwayswritesgpu
124 ///
125 void GX2API GX2ClearBuffersEx(GX2ColorBuffer *colorBuffer,
126                               GX2DepthBuffer *depthBuffer,
127                               f32 r, f32 g, f32 b, f32 a,
128                               f32 depthValue,
129                               u8 stencilValue,
130                               GX2ClearMode clearFlags);
131 
132 /// \brief Clear the depth/stencil surface with saved depth and stencil values.
133 ///
134 /// \param depthBuffer   pointer to depth/stencil buffer to clear
135 /// \param clearFlags    Indicates if depth and/or stencil buffer should be cleared
136 ///
137 /// \note This API changes rendering states and disables state shadowing.
138 ///       If you are using state shadowing, you must call
139 ///       \ref GX2SetContextState() afterward
140 ///       (when (clearFlags & (GX2_CLEAR_DEPTH|GX2_CLEAR_STENCIL)) != 0).
141 ///
142 /// \note This API will use the current clearDepth and clearStencil values
143 ///       from the \ref GX2DepthBuffer.
144 ///
145 /// \note This function clears only the single miplevel specified by
146 ///       depthBuffer->viewMip. This function clears the number of slices
147 ///       specified by depthBuffer->viewNumSlices starting with slice number
148 ///       depthBuffer->viewFirstSlice.
149 ///
150 /// \note To generate valuable HiStencil pretest results while clearing the
151 ///       stencil buffer, please see \ref GX2UTClearRectOp.
152 ///
153 /// \donotcall \gx2_typical \enddonotcall
154 ///
155 /// \clobberstate
156 /// \disablesstateshadow
157 /// \notincompute
158 ///
159 /// \writesgpu
160 /// \alwayswritesgpu
161 ///
GX2ClearDepthStencil(GX2DepthBuffer * depthBuffer,GX2ClearMode clearFlags)162 GX2_INLINE void GX2ClearDepthStencil(GX2DepthBuffer *depthBuffer,
163                                  GX2ClearMode clearFlags) {
164     GX2ClearDepthStencilEx(depthBuffer, depthBuffer->clearDepth, (u8)depthBuffer->clearStencil, clearFlags);
165 }
166 
167 /// \brief Clears the given render target and depth/stencil surface.
168 ///
169 /// \note This API changes rendering states and disables state shadowing.
170 ///       If you are using state shadowing, you must call
171 ///       \ref GX2SetContextState() afterward.
172 /// \note This API will use the current clearDepth and clearStencil values
173 ///       from the \ref GX2DepthBuffer.
174 /// \note The colorBuffer and depthBuffer must have the same width, height, viewNumSlices,
175 ///       and number of MSAA samples.
176 /// \note This function clears only the single miplevel specified by colorBuffer->viewMip for the color buffer
177 ///       and depthBuffer->viewMip for the depth buffer.
178 ///       colorBuffer->viewNumSlices must match depthBuffer->viewNumSlices.
179 ///       This function clears the number of slices specified by colorBuffer->viewNumSlices starting with slice number
180 ///       colorBuffer->viewFirstSlice, for the color buffer, and depthBuffer->viewFirstSlice, for the depth buffer.
181 /// \note To generate valuable HiStencil pretest results while clearing the stencil buffer,
182 ///       please see \ref GX2UTClearRectOp.
183 ///
184 /// \param colorBuffer pointer to color buffer to clear
185 /// \param depthBuffer pointer to depth/stencil buffer to clear
186 /// \param r Red color component of clear value
187 /// \param g Green color component of clear value
188 /// \param b Blue color component of clear value
189 /// \param a Alpha color component of clear value
190 /// \param clearFlags    Indicates if depth and/or stencil buffer should be cleared
191 ///
192 /// \donotcall \gx2_typical \enddonotcall
193 ///
194 /// \clobberstate
195 /// \disablesstateshadow
196 /// \notincompute
197 ///
198 /// \writesgpu
199 /// \alwayswritesgpu
200 ///
GX2ClearBuffers(GX2ColorBuffer * colorBuffer,GX2DepthBuffer * depthBuffer,f32 r,f32 g,f32 b,f32 a,GX2ClearMode clearFlags)201 GX2_INLINE void GX2ClearBuffers(GX2ColorBuffer *colorBuffer,
202                             GX2DepthBuffer *depthBuffer,
203                             f32 r, f32 g, f32 b, f32 a,
204                             GX2ClearMode clearFlags) {
205     GX2ClearBuffersEx(colorBuffer, depthBuffer, r, g, b, a, depthBuffer->clearDepth, (u8)depthBuffer->clearStencil, clearFlags);
206 }
207 
208 /// \brief Sets the clear values for the depth/stencil surface.
209 /// \note This API will write the depth/stencil clear registers. If using state
210 ///       shadowing, ensure that state shadowing is enabled prior to this
211 ///       API call.
212 ///
213 /// \param depthBuffer pointer to depth/stencil buffer to clear
214 /// \param depthValue    Depth clear value (from 0.0 to 1.0)
215 /// \param stencilValue  Stencil clear value (from 0 to 255)
216 ///
217 /// \donotcall \gx2_typical \enddonotcall
218 ///
219 /// \writesgpu
220 /// \alwayswritesgpu
221 ///
222 void GX2API GX2SetClearDepthStencil(GX2DepthBuffer *depthBuffer,
223                                     f32 depthValue, u8 stencilValue);
224 
225 /// \brief Sets the clear values for the depth surface.
226 /// \note This API will write the depth clear register. If using state
227 ///       shadowing, ensure that state shadowing is enabled prior to this
228 ///       API call.
229 ///
230 /// \param depthBuffer pointer to depth/stencil buffer to clear
231 /// \param depthValue    Depth clear value (from 0.0 to 1.0)
232 void GX2API GX2SetClearDepth(GX2DepthBuffer *depthBuffer, f32 depthValue);
233 
234 /// \brief Sets the clear values for the stencil surface.
235 /// \note This API will write the stencil clear register. If using state
236 ///       shadowing, ensure that state shadowing is enabled prior to this
237 ///       API call.
238 ///
239 /// \param depthBuffer pointer to depth/stencil buffer to clear
240 /// \param stencilValue  Stencil clear value (from 0 to 255)
241 void GX2API GX2SetClearStencil(GX2DepthBuffer *depthBuffer, u8 stencilValue);
242 
243 /// @}
244 /// @addtogroup GX2DrawPrimitiveGroup
245 /// @{
246 
247 /// \brief Set up a vertex attribute buffer
248 ///
249 /// \param index   Which vertex attribute buffer to set up
250 /// \param size    Size in bytes of this buffer
251 /// \param stride  Stride in bytes between the start of one vertex and the next
252 /// \param ptr     Address ptr to the first vertex (no alignment requirements)
253 ///
254 /// \donotcall \gx2_typical \enddonotcall
255 ///
256 /// \writesgpu
257 /// \alwayswritesgpu
258 ///
259 void GX2API GX2SetAttribBuffer(u32 index, u32 size, u32 stride, const void* ptr);
260 
261 /// \brief Set up the index value that indicates primitive restart.
262 /// This allows multiple triangle strips (for example) to be drawn with one call.
263 ///
264 /// The initial default value is 0xffffffff (for 32-bit indices).  If you
265 /// use 16-bit indices, you must set a different value (such as 0xffff).
266 ///
267 /// \param index   The index value to use to indicate primitive restart
268 ///
269 /// \donotcall \gx2_typical \enddonotcall
270 ///
271 /// \writesgpu
272 /// \alwayswritesgpu
273 ///
274 void GX2API GX2SetPrimitiveRestartIndex(u32 index);
275 
276 // -----------------
277 //  GX2DrawIndexed
278 
279 /// \brief Draw indexed primitives (passing index buffer by reference) (extended version)
280 ///
281 /// This is an extended version of \ref GX2DrawIndexed which adds baseVertex
282 /// to gl_VertexID when fetching non-instanced attribute data. It also provides
283 /// support for instancing.
284 ///
285 /// \param mode         Type of primitive to draw
286 /// \param count        How many vertices to draw
287 /// \param indexFormat  Specifies if indices are 16-bit or 32-bit
288 /// \param indices      Pointer to the index buffer
289 /// \param baseVertex   Which vertex in vertex buffers is considered first
290 /// \param numInstances How many instances to draw
291 ///
292 /// \donotcall \gx2_typical \enddonotcall
293 ///
294 /// \writesgpu
295 /// \alwayswritesgpu
296 ///
297 void GX2API GX2DrawIndexedEx(GX2PrimitiveType mode, u32 count,
298                              GX2IndexFormat indexFormat,
299                              const void* indices, u32 baseVertex,
300                              u32 numInstances);
301 
302 /// \brief Draw indexed primitives (passing index buffer by reference) (extended version)
303 ///
304 /// This is an extended version of \ref GX2DrawIndexedEx which adds baseInstance
305 /// to gl_InstanceID when fetching instanced attribute data. Similarly baseVertex
306 /// is added to gl_VertexID when fetching non-instanced attribute data.
307 ///
308 /// \param mode         Type of primitive to draw
309 /// \param count        How many vertices to draw
310 /// \param indexFormat  Specifies if indices are 16-bit or 32-bit
311 /// \param indices      Pointer to the index buffer
312 /// \param baseVertex   Which vertex in vertex buffers is considered first
313 /// \param numInstances How many instances to draw
314 /// \param baseInstance Base instance offset value
315 ///
316 /// \donotcall \gx2_typical \enddonotcall
317 ///
318 /// \writesgpu
319 /// \alwayswritesgpu
320 ///
321 void GX2API GX2DrawIndexedEx2(GX2PrimitiveType mode, u32 count,
322                              GX2IndexFormat indexFormat,
323                              const void* indices, u32 baseVertex,
324                              u32 numInstances, u32 baseInstance);
325 
326 /// \brief Draw using adaptive tessellation
327 ///
328 /// \param mode         Type of primitive to draw
329 /// \param count        How many vertices to draw
330 /// \param pTessFactors Tessellation factors buffer
331 /// \param baseVertex   Which vertex is considered first
332 ///
333 /// \donotcall \gx2_typical \enddonotcall
334 ///
335 /// \writesgpu
336 /// \alwayswritesgpu
337 ///
GX2DrawAdaptive(GX2PrimitiveType mode,u32 count,const void * pTessFactors,u32 baseVertex)338 GX2_INLINE void GX2DrawAdaptive(GX2PrimitiveType mode,
339                                 u32 count,
340                                 const void* pTessFactors,
341                                 u32 baseVertex)
342 {
343      ASSERT(((mode == GX2_PRIMITIVE_TESSELLATE_LINES)      ||
344              (mode == GX2_PRIMITIVE_TESSELLATE_TRIANGLES)  ||
345              (mode == GX2_PRIMITIVE_TESSELLATE_QUADS)) &&
346              "Only list primitive types are supported for adaptive tessellation.");
347 
348      GX2DrawIndexedEx(mode, count, GX2_INDEX_FORMAT_U32_LE, pTessFactors, baseVertex, 1);
349 }
350 
351 /// \brief Draw indexed primitives (passing index buffer by reference) (simple version)
352 ///
353 /// \param mode         Type of primitive to draw
354 /// \param count        How many vertices to draw
355 /// \param indexFormat  Specifies if indices are 16-bit or 32-bit
356 /// \param indices      Pointer to the index buffer
357 ///
358 /// \donotcall \gx2_typical \enddonotcall
359 ///
360 /// \writesgpu
361 /// \alwayswritesgpu
362 ///
GX2DrawIndexed(GX2PrimitiveType mode,u32 count,GX2IndexFormat indexFormat,const void * indices)363 GX2_INLINE void GX2DrawIndexed(GX2PrimitiveType mode, u32 count,
364                            GX2IndexFormat indexFormat, const void* indices)
365 {
366     GX2DrawIndexedEx(mode, count, indexFormat, indices, 0, 1);
367 }
368 
369 // -----------------
370 //  GX2DrawIndexedImmediate
371 
372 /// \brief Draw indexed primitives (passing indices into the command buffer) (extended version)
373 ///
374 /// \note baseVertex does not apply to attributes indexed by instance ID
375 ///
376 /// \param mode         Type of primitive to draw
377 /// \param count        How many vertices to draw
378 /// \param indexFormat  Specifies if indices are 16-bit or 32-bit
379 /// \param indices      2 byte aligned pointer to the index buffer
380 /// \param baseVertex   Which vertex in vertex buffers is considered first
381 /// \param numInstances How many instances to draw
382 ///
383 /// \donotcall \gx2_typical \enddonotcall
384 ///
385 /// \writesgpu
386 /// \alwayswritesgpu
387 ///
388 void GX2API GX2DrawIndexedImmediateEx(GX2PrimitiveType mode, u32 count,
389                                       GX2IndexFormat indexFormat,
390                                       const void* indices, u32 baseVertex,
391                                       u32 numInstances);
392 
393 /// \brief Draw indexed primitives (passing indices into the command buffer) (simple version)
394 ///
395 /// \param mode         Type of primitive to draw
396 /// \param count        How many vertices to draw
397 /// \param indexFormat  Specifies if indices are 16-bit or 32-bit
398 /// \param indices      2 byte aligned pointer to the index buffer
399 ///
400 /// \donotcall \gx2_typical \enddonotcall
401 ///
402 /// \writesgpu
403 /// \alwayswritesgpu
404 ///
GX2DrawIndexedImmediate(GX2PrimitiveType mode,u32 count,GX2IndexFormat indexFormat,const void * indices)405 GX2_INLINE void GX2DrawIndexedImmediate(GX2PrimitiveType mode, u32 count,
406                                     GX2IndexFormat indexFormat,
407                                     const void* indices)
408 {
409     GX2DrawIndexedImmediateEx(mode, count, indexFormat, indices, 0, 1);
410 }
411 
412 // -----------------
413 //  GX2Draw
414 
415 /// \brief Draw non-indexed primitives (ie, auto-generated, consecutive indices) (extended version)
416 ///
417 /// This is an extended version of \ref GX2Draw which adds firstVertex
418 /// to gl_VertexID when fetching non-instanced attribute data. It also provides
419 /// support for instancing.
420 ///
421 /// \param mode         Type of primitive to draw
422 /// \param count        How many vertices to draw
423 /// \param firstVertex  Starting vertex index to draw
424 /// \param numInstances How many instances to draw
425 ///
426 /// \donotcall \gx2_typical \enddonotcall
427 ///
428 /// \writesgpu
429 /// \alwayswritesgpu
430 ///
431 void GX2API GX2DrawEx(GX2PrimitiveType mode, u32 count, u32 firstVertex,
432                       u32 numInstances);
433 
434 /// \brief Draw non-indexed primitives (ie, auto-generated, consecutive indices) (extended version)
435 ///
436 /// This is an extended version of \ref GX2DrawEx which adds baseInstance
437 /// to gl_InstanceID when fetching instanced attribute data. Similarly baseVertex
438 /// is added to gl_VertexID when fetching non-instanced attribute data.
439 ///
440 /// \param mode         Type of primitive to draw
441 /// \param count        How many vertices to draw
442 /// \param firstVertex  Starting vertex index to draw
443 /// \param numInstances How many instances to draw
444 /// \param baseInstance Base instance offset value
445 ///
446 /// \donotcall \gx2_typical \enddonotcall
447 ///
448 /// \writesgpu
449 /// \alwayswritesgpu
450 ///
451 void GX2API GX2DrawEx2(GX2PrimitiveType mode, u32 count, u32 firstVertex,
452                        u32 numInstances, u32 baseInstance);
453 
454 /// \brief Draw non-indexed primitives (ie, auto-generated, consecutive indices) (simple version)
455 ///
456 /// \param mode         Type of primitive to draw
457 /// \param count        How many vertices to draw
458 ///
459 /// \donotcall \gx2_typical \enddonotcall
460 ///
461 /// \writesgpu
462 /// \alwayswritesgpu
463 ///
GX2Draw(GX2PrimitiveType mode,u32 count)464 GX2_INLINE void GX2Draw(GX2PrimitiveType mode, u32 count)
465 {
466     GX2DrawEx(mode, count, 0, 1);
467 }
468 
469 /// \brief Draw non-indexed primitives from a stream out buffer.
470 ///
471 /// \note The count is determined by the HW based on the number of bytes written to the stream out
472 ///       buffer.
473 ///
474 /// \note The stream out buffer must also be set as one of the attribute buffers
475 ///
476 /// \note \ref GX2SaveStreamOutContext() must be called before this function is called
477 ///
478 /// \note Only the following primitive types are supported:
479 ///          GX2_PRIMITIVE_POINTS
480 ///          GX2_PRIMITIVE_LINES
481 ///          GX2_PRIMITIVE_LINE_STRIP
482 ///          GX2_PRIMITIVE_TRIANGLES
483 ///          GX2_PRIMITIVE_TRIANGLE_STRIP
484 ///
485 /// \param mode          Type of primitive to draw
486 /// \param pStreamOutBuf Pointer to stream out buffer structure
487 ///
488 /// \donotcall \gx2_typical \enddonotcall
489 ///
490 /// \writesgpu
491 /// \alwayswritesgpu
492 ///
493 void GX2API GX2DrawStreamOut(GX2PrimitiveType mode,
494                              const GX2StreamOutBuffer* pStreamOutBuf);
495 
496 /// \brief Dispatch a compute vector.
497 ///
498 /// This starts the execution of a \ref GX2ComputeShader. The number of work-items
499 /// executed is equal to (num_groups_x * num_groups_y * num_groups_z) *
500 /// \ref GX2_MAX_WORK_ITEMS_PER_WORK_GROUP.  This set describes a cube within
501 /// the work-group domain.  That cube is then subdivided for execution according to the shader layout.
502 ///
503 /// \warning \p dispatchParams must be invalidated from the CPU caches using \ref GX2Invalidate.
504 /// \warning \p dispatchParams must not be modified until the last GX2DispatchCompute call referring to it has completed.
505 /// \warning num_groups_x, num_groups_y and num_groups_z must be at least 1.
506 /// \warning num_groups_x, num_groups_y and num_groups_z must be written in big-endian format.
507 ///
508 /// \ingroup GX2ShaderComputeGroup
509 ///
510 /// For more information about Compute Shaders see \ref GX2ComputePage "GX2 Compute Shaders"
511 ///
512 /// \param dispatchParams Pointer to a GX2DispatchParams structure containing the num_groups_x, num_groups_y and num_groups_z to be dispatched.
513 ///
514 /// \donotcall \gx2_typical \enddonotcall
515 ///
516 /// \writesgpu
517 /// \alwayswritesgpu
518 ///
519 void GX2API GX2DispatchCompute(GX2DispatchParams *dispatchParams);
520 
521 /// \brief Handy macro to adjust index buffer starting address
522 /// if you wish to start drawing from an index other than the first.
523 /// For 32-bit indices.
524 #define GX2_INDEX32_OFFSET(ptr, offset) (void*)((u32)ptr + offset*sizeof(u32))
525 
526 /// \brief Handy macro to adjust index buffer starting address
527 /// if you wish to start drawing from an index other than the first.
528 /// For 16-bit indices.
529 #define GX2_INDEX16_OFFSET(ptr, offset) (void*)((u32)ptr + offset*sizeof(u16))
530 
531 /// @}
532 
533 /// @}
534 
535 #ifdef __cplusplus
536 }
537 #endif // __cplusplus
538 
539 #endif // _CAFE_GX2_DRAW_H_
540