1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     gr_Shader.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 47511 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_GR_CTR_GR_SHADER_H_
17 #define NN_GR_CTR_GR_SHADER_H_
18 
19 #include <nn/gr/CTR/gr_Utility.h>
20 #include <nn/gr/CTR/gr_BindSymbol.h>
21 
22 namespace nn
23 {
24     namespace gr
25     {
26         namespace CTR
27         {
28 
29             /* Please see man pages for details
30 
31              */
32             class Shader
33             {
34             public :
35                 /* Please see man pages for details
36 
37                 */
38                 Shader( void );
39 
40                 /* Please see man pages for details
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51                 */
52                 void SetupBinary( const void* shader_binary, const s32 vtx_shader_index, const s32 geo_shader_index );
53 
54 
55                 /* Please see man pages for details
56 
57 
58 
59 
60 
61 
62 
63 
64 
65                 */
66                 void SetShaderIndex( const s32 vtx_shader_index, const s32 geo_shader_index );
67 
68             public :
69                 /* Please see man pages for details
70 
71 
72 
73 
74                  */
SetUniformBool(const BindSymbolVSBool & symbol,bool isEnable)75                 void SetUniformBool( const BindSymbolVSBool& symbol, bool isEnable )
76                 {
77                     m_VtxShaderBoolMapUniform = ( m_VtxShaderBoolMapUniform & ~( 1 << symbol.start ) ) | ( ( isEnable ? 1 : 0 ) << symbol.start );
78                 }
79 
80                 /* Please see man pages for details
81 
82 
83 
84 
85                  */
SetUniformBool(const BindSymbolGSBool & symbol,bool isEnable)86                 void SetUniformBool( const BindSymbolGSBool& symbol, bool isEnable )
87                 {
88                     m_GeoShaderBoolMapUniform = ( m_GeoShaderBoolMapUniform & ~( 1 << symbol.start ) ) | ( ( isEnable ? 1 : 0 ) << symbol.start );
89                 }
90 
91                 /* Please see man pages for details
92 
93 
94 
95 
96                  */
SetDrawMode(const PicaDataDrawMode draw_mode)97                 void SetDrawMode( const PicaDataDrawMode draw_mode )
98                 {
99                     m_DrawMode = draw_mode;
100                 }
101 
102             public :
103                 /* Please see man pages for details
104 
105 
106 
107 
108 
109 
110                  */
111                 bit32* MakeFullCommand( bit32* command ) const;
112 
113 
114                 /* Please see man pages for details
115 
116 
117 
118 
119 
120 
121 
122 
123 
124                  */
125                 bit32* MakeShaderCommand( bit32* command, const bool isMakePrepareCommand = true ) const;
126 
127                 /* Please see man pages for details
128 
129 
130 
131 
132 
133                  */
134                 static bit32* MakeDisableCommand( bit32* command );
135 
136             public :
137                 /* Please see man pages for details
138 
139 
140 
141 
142 
143 
144                  */
MakeVtxBoolMapCommand(bit32 * command)145                 bit32* MakeVtxBoolMapCommand( bit32* command ) const
146                 {
147                     *command++ = 0x7fff0000 | m_VtxShaderBoolMapUniform;
148                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_BOOL ); // 0x2b0
149                     return command;
150                 }
151 
152                 /* Please see man pages for details
153 
154 
155 
156 
157 
158                  */
MakeGeoBoolMapCommand(bit32 * command)159                 bit32* MakeGeoBoolMapCommand( bit32* command ) const
160                 {
161                     *command++ = 0x7fff0000 | m_GeoShaderBoolMapUniform;
162                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_BOOL ); // 0x280
163                     return command;
164                 }
165 
166                 /* Please see man pages for details
167 
168 
169 
170 
171 
172 
173                  */
174                 bit32* MakePrepareCommand( bit32* command ) const;
175 
176                 /* Please see man pages for details
177 
178 
179 
180 
181 
182 
183                  */
184                 bit32* MakeVtxProgramCommand( bit32* command ) const;
185 
186                 /* Please see man pages for details
187 
188 
189 
190 
191 
192 
193 
194                  */
195                 bit32* MakeGeoProgramCommand( bit32* command ) const;
196 
197                 /* Please see man pages for details
198 
199 
200 
201 
202 
203 
204                  */
MakeVtxSwizzleCommand(bit32 * command)205                 bit32* MakeVtxSwizzleCommand( bit32* command ) const
206                 {
207                     *command++ = 0;
208                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_PROG_SWIZZLE_ADDR );
209                     return MakeLoadCommand_( command, PICA_REG_VS_PROG_SWIZZLE_DATA0, m_Swizzle, m_SwizzleCount );
210                 }
211 
212                 /* Please see man pages for details
213 
214 
215 
216 
217 
218 
219                  */
MakeGeoSwizzleCommand(bit32 * command)220                 bit32* MakeGeoSwizzleCommand( bit32* command ) const
221                 {
222                     *command++ = 0;
223                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_PROG_SWIZZLE_ADDR );
224                     return MakeLoadCommand_( command, PICA_REG_GS_PROG_SWIZZLE_DATA0, m_Swizzle, m_SwizzleCount );
225                 }
226 
227                 /* Please see man pages for details
228 
229 
230 
231 
232 
233 
234                  */
MakeVtxConstRgCommand(bit32 * command)235                 bit32* MakeVtxConstRgCommand( bit32* command ) const
236                 {
237                     std::memcpy( command, m_CmdCacheConstArray[ m_VtxShaderIndex ],
238                                  m_CmdCacheConstNumArray[ m_VtxShaderIndex ] * sizeof( bit32 ) );
239                     return command + m_CmdCacheConstNumArray[ m_VtxShaderIndex ];
240                 }
241 
242                 /* Please see man pages for details
243 
244 
245 
246 
247 
248 
249                  */
MakeGeoConstRgCommand(bit32 * command)250                 bit32* MakeGeoConstRgCommand( bit32* command ) const
251                 {
252                     std::memcpy( command, m_CmdCacheConstArray[ m_GeoShaderIndex ],
253                                  m_CmdCacheConstNumArray[ m_GeoShaderIndex ] * sizeof( bit32 ) );
254                     return command + m_CmdCacheConstNumArray[ m_GeoShaderIndex ];
255                 }
256 
257                 /* Please see man pages for details
258 
259 
260 
261 
262 
263 
264                  */
MakeOutAttrCommand(bit32 * command)265                 bit32* MakeOutAttrCommand( bit32* command ) const
266                 {
267                     // If the geometry shader mode and render mode do not match, ASSERT
268                     NN_GR_ASSERT( ( ! IsEnableGeoShader() ) || ( m_DrawMode == PICA_DATA_DRAW_GEOMETRY_PRIMITIVE ) );
269 
270                     // Generate commands based on the triangle's render mode
271                     // 0x229 [8:8]
272                     bit32 value_229 = 0x0100;
273                     if ( m_DrawMode != PICA_DATA_DRAW_TRIANGLES )
274                     {
275                         value_229 = 0x0;
276                     }
277                     *command++ = value_229;
278                     *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_DRAW_MODE0, 0x2 );
279 
280                     // 0x253 [8:8]
281                     bit32 value_253 = 0x0100;
282                     if ( m_DrawMode != PICA_DATA_DRAW_TRIANGLES )
283                     {
284                         value_253 = 0x0;
285                     }
286                     *command++ = value_253;
287                     *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_DRAW_MODE1, 0x2 );
288 
289                     std::memcpy( command, m_CmdCacheOutAttrArray,
290                                  m_CmdCacheOutAttrNum * sizeof( bit32 ) );
291 
292                     return command + m_CmdCacheOutAttrNum;
293                 }
294 
295             public : // Get information from the binary
296                 /* Please see man pages for details
297 
298 
299 
300 
301 
302 
303 
304 
305 
306                  */
307                 bool SearchBindSymbol(
308                     BindSymbol*      symbol,
309                     const char*      name ) const;
310 
311                  /* Please see man pages for details
312 
313 
314 
315 
316 
317 
318 
319 
320 
321                  */
322                 u32  SearchBindSymbolNum(
323                     const BindSymbol::ShaderType shader_type,
324                     const BindSymbol::SymbolType symbol_type ) const;
325 
326                 /* Please see man pages for details
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337                  */
338                 bool SearchBindSymbol(
339                     BindSymbol*      symbol,
340                     const u8         symbol_index ) const;
341 
342             public : // Get settings
343                 /* Please see man pages for details
344 
345 
346 
347                  */
GetShaderNum()348                 u8 GetShaderNum()           const
349                 {
350                     return m_ExeImageInfoNum;
351                 }
352 
353                 /* Please see man pages for details
354 
355 
356 
357 
358                  */
IsEnableGeoShader()359                 bool IsEnableGeoShader()    const
360                 {
361                     return ( 0 <= GetGeoShaderIndex() );
362                 }
363 
364                 /* Please see man pages for details
365 
366 
367 
368 
369                  */
370 
GetVtxShaderIndex()371                 s32 GetVtxShaderIndex()     const
372                 {
373                     return m_VtxShaderIndex;
374                 }
375 
376                 /* Please see man pages for details
377 
378 
379 
380 
381                  */
GetGeoShaderIndex()382                 s32 GetGeoShaderIndex()     const
383                 {
384                     return m_GeoShaderIndex;
385                 }
386 
387                 /* Please see man pages for details
388 
389 
390 
391                  */
GetVtxShaderBoolMapUniform()392                 bit32 GetVtxShaderBoolMapUniform() const
393                 {
394                     return m_VtxShaderBoolMapUniform;
395                 }
396 
397                 /* Please see man pages for details
398 
399 
400 
401                  */
GetGeoShaderBoolMap()402                 bit32 GetGeoShaderBoolMap()    const
403                 {
404                     return m_GeoShaderBoolMapUniform;
405                 }
406 
407             protected :
408                 /* Please see man pages for details
409 
410                  */
411                  //
412                  static const s32 EXE_IMAGE_MAX = 32;
413 
414                  //
415                  static const s32 SWIZZLE_PATTERN_MAX = 128;
416 
417                  //
418                  static const s32 DUMMY_DATA_NUM_251 = 10;
419 
420                  //
421                  static const s32 DUMMY_DATA_NUM_200 = 30;
422 
423                  //
424                  static const u32 PADDING_DATA = 0xead0fead;
425 
426                  //
427                  static const s32 OUT_ATTR_COMMAND_MAX = 48;
428 
429                  //
430                  static const u32 CONST_REG_COMMAND_MAX = 96 * 6 + 5 * 2;
431 
432             protected :
433                 /* Please see man pages for details
434 
435 
436 
437 
438 
439                 */
440                 void CheckVtxShaderIndex_( const s32 vtx_shader_index );
441 
442                /* Please see man pages for details
443 
444 
445 
446 
447 
448                 */
449                 void CheckGeoShaderIndex_( const s32 geo_shader_index );
450 
451                 /* Please see man pages for details
452 
453                  */
454                 void MakeShaderConstCommandCache_( void );
455 
456                 /* Please see man pages for details
457 
458                  */
459                 void MakeShaderOutAttrCommandCache_( void );
460 
461                 /* Please see man pages for details
462 
463 
464 
465 
466 
467 
468 
469                  */
470                 static bit32* MakeShaderModeCommand_( bit32* command,
471                                                       const bool isEnableGeoShader,
472                                                       const PicaDataDrawMode drawMode );
473 
474                 /* Please see man pages for details
475 
476 
477 
478 
479 
480 
481                  */
482 
483                 bit32* MakeConstRgCommand_( bit32* command,
484                                             const s32 shader_index );
485 
486                 /* Please see man pages for details
487 
488 
489 
490 
491 
492 
493 
494                  */
495                 bit32* MakeOutAttrCommand_( bit32* command, const s32 vtx_shader_index, const s32 geo_shader_index );
496 
497                 /* Please see man pages for details
498 
499 
500 
501 
502 
503 
504 
505 
506                  */
507                 bit32* MakeLoadCommand_( bit32* command,
508                                          const bit32  load_reg,
509                                          const u32* src_buffer_ptr,
510                                          const u32  src_size ) const;
511 
512                 /* Please see man pages for details
513 
514 
515 
516 
517 
518 
519 
520                  */
521                  static bit32* MakeDummyCommand_( bit32* command,
522                                                   const bit32 load_reg,
523                                                   const u32 data_num );
524 
525                 /* Please see man pages for details
526 
527                  */
528                 struct ExeImageInfo
529                 {
530                     //
531                     bit32 signature;
532                     //
533                     u16 version;
534                     //
535                     u8  isGeoShader;
536                     //
537                     u8  outputMaps;
538                     //
539                     uptr mainAddress;
540                     //
541                     uptr endAddress;
542                     //
543                     bit32 maskInputOutput;
544                     //
545                     u8  gsDataMode;
546                     //
547                     u8  gsVertexStartIndex;
548                     //
549                     u8  gsPatchSize;
550                     //
551                     u8  gsVertexNum;
552                     //
553                     u32 setupOffset;
554                     //
555                     u32 setupCount;
556                     //
557                     u32 labelOffset;
558                     //
559                     u32 labelCount;
560                     //
561                     u32 outMapOffset;
562                     //
563                     u32 outMapCount;
564                     //
565                     u32 bindSymbolOffset;
566                     //
567                     u32 bindSymbolCount;
568                     //
569                     u32 stringOffset;
570                     //
571                     u32 stringCount;
572                 };
573 
574                 /* Please see man pages for details
575 
576                 */
577                 s32 m_VtxShaderIndex;
578 
579                 /* Please see man pages for details
580 
581                 */
582                 s32 m_GeoShaderIndex;
583 
584                 /* Please see man pages for details
585 
586                 */
587                 u8 m_ExeImageInfoNum;
588                 NN_PADDING3;
589 
590                 /* Please see man pages for details
591 
592                 */
593                 const ExeImageInfo* m_ExeImageInfo[ EXE_IMAGE_MAX ];
594 
595                 /* Please see man pages for details
596 
597                 */
598                 const bit32* m_Instruction;
599 
600                 /* Please see man pages for details
601 
602                 */
603                 u32 m_InstructionCount;
604 
605                 /* Please see man pages for details
606 
607                 */
608                 bit32 m_Swizzle[ SWIZZLE_PATTERN_MAX ];
609 
610                 /* Please see man pages for details
611 
612                 */
613                 u32 m_SwizzleCount;
614 
615                 /* Please see man pages for details
616 
617                 */
618                 PicaDataDrawMode m_DrawMode;
619                 NN_PADDING3;
620 
621                 /* Please see man pages for details
622 
623                 */
624                 bit32 m_VtxShaderBoolMapUniform;
625 
626                 /* Please see man pages for details
627 
628                 */
629                 bit32 m_GeoShaderBoolMapUniform;
630 
631                 /* Please see man pages for details
632 
633                 */
634                 bit32 m_CmdCacheOutAttrArray[ OUT_ATTR_COMMAND_MAX ];
635 
636                 /* Please see man pages for details
637 
638                 */
639                 u32 m_CmdCacheOutAttrNum;
640 
641                 /* Please see man pages for details
642 
643                 */
644                 bit32 m_CmdCacheConstArray[ EXE_IMAGE_MAX ][ CONST_REG_COMMAND_MAX ];
645 
646                 /* Please see man pages for details
647 
648                 */
649                 u32 m_CmdCacheConstNumArray[ EXE_IMAGE_MAX ];
650             };
651 
652         } // namespace CTR
653     } // namespace gr
654 } // namespace nn
655 
656 #endif // NN_GR_CTR_GR_SHADER_H_
657