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