1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     gr_Utility.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: 49793 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_GR_CTR_GR_UTILITY_H_
17 #define NN_GR_CTR_GR_UTILITY_H_
18 
19 #include <nn/types.h>
20 #include <nn/math.h>
21 #include <nn/nstd.h>
22 #include <nn/gx.h>
23 #include <nn/gx/CTR/gx_CommandAccess.h>
24 
25 #include <nn/gr/CTR/gr_Prefix.h>
26 
27 namespace nn
28 {
29     namespace gr
30     {
31         namespace CTR
32         {
33             //------------------------------------------------------------------------------
34 
35             /* Please see man pages for details
36 
37             */
38             class CommandBufferJumpHelper
39             {
40             public:
41                 /* Please see man pages for details
42 
43 
44 
45                 */
46                 CommandBufferJumpHelper(bit32* address);
47                 //
48                 ~CommandBufferJumpHelper();
49 
50                 /* Please see man pages for details
51 
52 
53 
54                 */
GetSize()55                 inline size_t GetSize() const { return GetOffsetFromPtr(m_StartPtr, m_CurrentPtr); }
56 
57                 /* Please see man pages for details
58 
59 
60 
61                 */
GetSizeToFirstJump()62                 inline size_t GetSizeToFirstJump() const { return m_SizeToFirstJump; }
63 
64                 /* Please see man pages for details
65 
66 
67 
68                 */
GetStartPtr()69                 inline bit32* GetStartPtr() { return m_StartPtr; }
70 
71                 /* Please see man pages for details
72 
73 
74 
75                 */
GetCurrentPtr()76                 inline bit32* GetCurrentPtr() { return m_CurrentPtr; }
77 
78                 /* Please see man pages for details
79 
80 
81 
82                 */
Put(bit32 * address)83                 void Put(bit32* address) {
84                     NN_MIN_ASSERT(reinterpret_cast<u32*>(address), reinterpret_cast<u32*>(m_CurrentPtr));
85                     m_CurrentPtr = address;
86                 }
87 
88                 /* Please see man pages for details
89 
90 
91 
92                 */
Put(uptr address)93                 void Put(uptr address)
94                 {
95                     NN_MIN_ASSERT(reinterpret_cast<u32*>(address), reinterpret_cast<u32*>(m_CurrentPtr));
96                     m_CurrentPtr = reinterpret_cast<bit32*>(address);
97                 }
98 
99                 /* Please see man pages for details
100 
101 
102 
103 
104                 */
CopyFrom(const bit32 * begin,const bit32 * end)105                 void CopyFrom(const bit32* begin, const bit32* end)
106                 {
107                     CopyFrom(begin, end - begin);
108                 }
109                 /* Please see man pages for details
110 
111 
112 
113 
114                 */
CopyFrom(const bit32 * begin,int count)115                 void CopyFrom(const bit32* begin, int count)
116                 {
117                     nstd::MemCpy(m_CurrentPtr, begin, count * sizeof(bit32));
118                     m_CurrentPtr += count;
119                 }
120 
121                 /* Please see man pages for details
122 
123 
124 
125 
126                 */
127                 void Jump(uptr subroutine, size_t subroutineSize );
128 
129                 /* Please see man pages for details
130 
131 
132 
133                 */
134                 void FinalizeJump(bit32* endAddress);
135 
136             private:
137                 //
138                 bit32* m_CurrentPtr;
139                 //
140                 bit32* m_StartPtr;
141                 //
142                 bit32* m_ReturnPtr;
143                 //
144                 uptr   m_CmdSizePtr;
145                 //
146                 u32    m_SizeToFirstJump;
147 
148                 //
GetOffsetFromPtr(bit32 * start,bit32 * end)149                 inline size_t GetOffsetFromPtr( bit32* start, bit32* end ) const
150                 {
151                     return (end - start) * sizeof(bit32);
152                 }
153             };
154 
155             /* Please see man pages for details
156 
157 
158 
159 
160 
161 
162             */
163             bit32* AddDummyDataForCommandBuffer( bit32* command, const size_t addSize );
164 
165             //------------------------------------------------------------------------------
166 
167             /* Please see man pages for details
168 
169 
170 
171 
172 
173             */
174             void CopyMtx34WithHeader(
175                 f32*  dst,
176                 const nn::math::MTX34* src,
177                 bit32   header );
178 
179             /* Please see man pages for details
180 
181 
182 
183 
184 
185             */
186             void CopyMtx44WithHeader(
187                 f32* dst,
188                 const nn::math::MTX44* src,
189                 bit32 header );
190 
191             //------------------------------------------------------------------------------------
192 
193             /* Please see man pages for details
194 
195 
196 
197 
198 
199 
200 
201              */
MakeUniformCommandVS(bit32 * command,u8 location,const nn::math::MTX34 & mtx34)202             inline bit32* MakeUniformCommandVS( bit32* command, u8 location, const nn::math::MTX34& mtx34 )
203             {
204                 *command++ = 0x80000000 | location;
205                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_FLOAT_ADDR );
206                 CopyMtx34WithHeader( reinterpret_cast<f32*>( command ), &mtx34, PICA_CMD_HEADER_VS_F32( 3 ) );
207                 return command + 14;
208             }
209 
210             //------------------------------------------------------------------------------------
211 
212             /* Please see man pages for details
213 
214 
215 
216 
217 
218 
219 
220              */
MakeUniformCommandVS(bit32 * command,u8 location,const nn::math::MTX44 & mtx44)221             inline bit32* MakeUniformCommandVS( bit32* command, u8 location, const nn::math::MTX44& mtx44 )
222             {
223                 *command++ = 0x80000000 | location;
224                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_FLOAT_ADDR );
225                 CopyMtx44WithHeader( reinterpret_cast<f32*>( command ), &mtx44, PICA_CMD_HEADER_VS_F32( 4 ) );
226                 return command + 18;
227             }
228 
229             //------------------------------------------------------------------------------------
230 
231             /* Please see man pages for details
232 
233 
234 
235 
236 
237 
238 
239              */
MakeUniformCommandVS(bit32 * command,u8 location,const nn::math::VEC4 & vec4)240             inline bit32* MakeUniformCommandVS( bit32* command, u8 location, const nn::math::VEC4& vec4 )
241             {
242                 // 0x2c0
243                 *command++ = 0x80000000 | location;
244                 *command++ = PICA_CMD_HEADER_BURSTSEQ( PICA_REG_VS_FLOAT_ADDR, 5 );
245                 // 0x2c1-0x2c4
246                 *command++ = nn::math::F32AsU32( vec4.w );
247                 *command++ = nn::math::F32AsU32( vec4.z );
248                 *command++ = nn::math::F32AsU32( vec4.y );
249                 *command++ = nn::math::F32AsU32( vec4.x );
250                 return command;
251             }
252 
253 
254             //------------------------------------------------------------------------------------
255 
256             /* Please see man pages for details
257 
258 
259 
260 
261 
262 
263 
264 
265              */
MakeUniformCommandVS(bit32 * command,u8 location,const nn::math::VEC4 vec4[],const int num)266             inline bit32* MakeUniformCommandVS( bit32* command, u8 location, const nn::math::VEC4 vec4[], const int num )
267             {
268                 const u32 MAX_VS_F32_VEC4_NUM = 64;
269                 NN_GR_ASSERT( num <= MAX_VS_F32_VEC4_NUM );
270 
271                 NN_UNUSED_VAR( MAX_VS_F32_VEC4_NUM );
272 
273                 // 0x2c0
274                 *command++ = 0x80000000 | location;
275                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_FLOAT_ADDR );
276 
277                 // 0x2c1
278                 *command++ = nn::math::F32AsU32( vec4[0].w );
279                 *command++ = PICA_CMD_HEADER_VS_F32( num );
280                 *command++ = nn::math::F32AsU32( vec4[0].z );
281                 *command++ = nn::math::F32AsU32( vec4[0].y );
282                 *command++ = nn::math::F32AsU32( vec4[0].x );
283 
284                 for ( int i = 1; i < num; ++i )
285                 {
286                     *command++ = nn::math::F32AsU32( vec4[i].w );
287                     *command++ = nn::math::F32AsU32( vec4[i].z );
288                     *command++ = nn::math::F32AsU32( vec4[i].y );
289                     *command++ = nn::math::F32AsU32( vec4[i].x );
290                 }
291 
292                 *command++ = 0; // Padding
293 
294                 return command;
295             }
296 
297             //------------------------------------------------------------------------------------
298 
299             /* Please see man pages for details
300 
301 
302 
303 
304 
305 
306 
307 
308 
309              */
MakeUniformCommandVS(bit32 * command,u8 location,u8 x,u8 y,u8 z)310             inline bit32* MakeUniformCommandVS( bit32* command, u8 location, u8 x, u8 y, u8 z )
311             {
312                 *command++ = PICA_CMD_DATA_VS_INT( x, y, z );
313                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_VS_INT0 + location );
314                 return command;
315             }
316 
317             //------------------------------------------------------------------------------------
318 
319             /* Please see man pages for details
320 
321 
322 
323 
324 
325 
326 
327              */
MakeUniformCommandGS(bit32 * command,u8 location,const nn::math::MTX34 & mtx34)328             inline bit32* MakeUniformCommandGS( bit32* command, u8 location, const nn::math::MTX34& mtx34 )
329             {
330                 *command++ = 0x80000000 | location;
331                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_FLOAT_ADDR );
332                 CopyMtx34WithHeader( reinterpret_cast<f32*>( command ), &mtx34, PICA_CMD_HEADER_GS_F32( 3 ) );
333                 return command + 14;
334             }
335 
336             //------------------------------------------------------------------------------------
337 
338             /* Please see man pages for details
339 
340 
341 
342 
343 
344 
345 
346              */
MakeUniformCommandGS(bit32 * command,u8 location,const nn::math::MTX44 & mtx44)347             inline bit32* MakeUniformCommandGS( bit32* command, u8 location, const nn::math::MTX44& mtx44 )
348             {
349                 *command++ = 0x80000000 | location;
350                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_FLOAT_ADDR );
351                 CopyMtx44WithHeader( reinterpret_cast<f32*>( command ), &mtx44, PICA_CMD_HEADER_GS_F32( 4 ) );
352                 return command + 18;
353             }
354 
355             //------------------------------------------------------------------------------------
356 
357             /* Please see man pages for details
358 
359 
360 
361 
362 
363 
364 
365              */
MakeUniformCommandGS(bit32 * command,u8 location,const nn::math::VEC4 & vec4)366             inline bit32* MakeUniformCommandGS( bit32* command, u8 location, const nn::math::VEC4& vec4 )
367             {
368                 *command++ = 0x80000000 | location;
369                 *command++ = PICA_CMD_HEADER_BURSTSEQ( PICA_REG_GS_FLOAT_ADDR, 5 );
370                 *command++ = nn::math::F32AsU32( vec4.w ); // a
371                 *command++ = nn::math::F32AsU32( vec4.z ); // b
372                 *command++ = nn::math::F32AsU32( vec4.y ); // g
373                 *command++ = nn::math::F32AsU32( vec4.x ); // r
374                 return command;
375             }
376 
377             //------------------------------------------------------------------------------------
378 
379             /* Please see man pages for details
380 
381 
382 
383 
384 
385 
386 
387 
388              */
MakeUniformCommandGS(bit32 * command,u8 location,const nn::math::VEC4 vec4[],const int num)389             inline bit32* MakeUniformCommandGS( bit32* command, u8 location, const nn::math::VEC4 vec4[], const int num )
390             {
391                 const u32 MAX_GS_F32_VEC4_NUM = 64;
392                 NN_GR_ASSERT( num <= MAX_GS_F32_VEC4_NUM );
393 
394                 NN_UNUSED_VAR( MAX_GS_F32_VEC4_NUM );
395 
396                 *command++ = 0x80000000 | location;
397                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_FLOAT_ADDR );
398                 *command++ = nn::math::F32AsU32( vec4[0].w );
399                 *command++ = PICA_CMD_HEADER_GS_F32( num );
400                 *command++ = nn::math::F32AsU32( vec4[0].z );
401                 *command++ = nn::math::F32AsU32( vec4[0].y );
402                 *command++ = nn::math::F32AsU32( vec4[0].x );
403 
404                 for ( int i = 1; i < num; ++i )
405                 {
406                     *command++ = nn::math::F32AsU32( vec4[i].w );
407                     *command++ = nn::math::F32AsU32( vec4[i].z );
408                     *command++ = nn::math::F32AsU32( vec4[i].y );
409                     *command++ = nn::math::F32AsU32( vec4[i].x );
410                 }
411 
412                 *command++ = 0; // Padding
413 
414                 return command;
415             }
416 
417             //------------------------------------------------------------------------------------
418 
419             /* Please see man pages for details
420 
421 
422 
423 
424 
425 
426 
427 
428 
429              */
MakeUniformCommandGS(bit32 * command,u8 location,u8 x,u8 y,u8 z)430             inline bit32* MakeUniformCommandGS( bit32* command, u8 location, u8 x, u8 y, u8 z )
431             {
432                 *command++ = PICA_CMD_DATA_GS_INT( x, y, z );
433                 *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_GS_INT0 + location );
434                 return command;
435             }
436 
437             //------------------------------------------------------------------------------------
438 
439             /* Please see man pages for details
440 
441 
442 
443 
444 
445             */
FloatToUnsignedByte(f32 val)446             inline u8 FloatToUnsignedByte(
447                 f32 val
448                 )
449             {
450                 return ( u8 )( 0.5f + ( val < 0.f ? 0.f : ( 1.f < val ? 1.f : val ) )  * ( 0xff ) );
451             }
452 
453             //------------------------------------------------------------------------------------
454 
455             /* Please see man pages for details
456 
457 
458 
459 
460 
461             */
FloatToUnsignedByteNoClamp(f32 val)462             inline u8 FloatToUnsignedByteNoClamp(
463                 f32 val
464                 )
465             {
466                 return ( u8 )( 0.5f + val * 0xff );
467             }
468 
469             //------------------------------------------------------------------------------------
470 
471             /* Please see man pages for details
472 
473 
474 
475 
476 
477             */
Float32ToFloat16(f32 val)478             inline u16 Float32ToFloat16( f32 val )
479             {
480                 static const int bias_ = 128 - (1 << (5 - 1));
481 
482                 u32 uval_ = *( reinterpret_cast<u32*>( &val ) );
483                 int e_ = (uval_ & 0x7fffffff) ? (((uval_ >> 23) & 0xff) - bias_) : 0;
484                 if (e_ >= 0)
485                 {
486                     return ( u16 )( ((uval_ & 0x7fffff) >> (23 - 10)) | (e_ << 10) | ((uval_ >> 31) << (10 + 5)) );
487                 }
488                 return ( u16 )((uval_ >> 31) << (10 + 5));
489             }
490 
491             //------------------------------------------------------------------------------------
492 
493             /* Please see man pages for details
494 
495 
496 
497 
498 
499             */
Float32ToFloat24(f32 val)500             inline u32 Float32ToFloat24( f32 val )
501             {
502                 static const int bias_ = 128 - (1 << (7 - 1));
503                 u32 uval_   = *( reinterpret_cast<unsigned*>( &val ) );
504                 s32 e_      = (uval_ & 0x7fffffff) ? (((uval_ >> 23) & 0xff) - bias_) : 0;
505 
506                 return e_ >= 0 ? ((uval_ & 0x7fffff) >> (23 - 16)) | (e_ << 16) | ((uval_ >> 31) << (16 + 7)) : ((uval_ >> 31) << (16 + 7));
507             }
508 
509             //------------------------------------------------------------------------------------
510 
511             /* Please see man pages for details
512 
513 
514 
515 
516 
517             */
Float32ToFloat20(f32 val)518             inline u32 Float32ToFloat20( f32 val )
519             {
520                 static const int bias_ = 128 - (1 << (7 - 1));
521                 u32 uval_   = *( reinterpret_cast<unsigned*>( &val ) );
522                 s32 e_      = (uval_ & 0x7fffffff) ? (((uval_ >> 23) & 0xff) - bias_) : 0;
523 
524                 return e_ >= 0 ? ((uval_ & 0x7fffff) >> (23 - 12)) | (e_ << 12) | ((uval_ >> 31) << (12 + 7)) : ((uval_ >> 31) << (12 + 7));
525             }
526 
527             //------------------------------------------------------------------------------------
528 
529             /* Please see man pages for details
530 
531 
532 
533 
534 
535             */
Float32ToFloat31(f32 val)536             inline u32 Float32ToFloat31( f32 val )
537             {
538                 unsigned uval_, m_;
539                 int e_;
540                 float f_ = val;
541                 static const int bias_ = 128 - (1 << (7 - 1));
542                 uval_ = *( reinterpret_cast<unsigned*>( &f_ ) );
543                 e_ = (uval_ & 0x7fffffff) ? (((uval_ >> 23) & 0xff) - bias_) : 0;
544                 m_ = (uval_ & 0x7fffff) >> (23 - 23);
545                 return e_ >= 0 ? m_ | (e_ << 23) | ((uval_ >> 31) << (23 + 7)) : ((uval_ >> 31) << (23 + 7));
546             }
547 
548             //------------------------------------------------------------------------------------
549             /* Please see man pages for details
550 
551 
552 
553 
554 
555             */
Float32ToUnsignedFix24(f32 val)556             inline u32 Float32ToUnsignedFix24( f32 val )
557             {
558                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
559 
560                 if (val <= 0 || (v_ & 0x7f800000) == 0x7f800000)
561                 {
562                     return 0;
563                 }
564                 else
565                 {
566                     val *= 1 << (24 - 0);
567 
568                     if ( val >= (1 << 24) )
569                     {
570                         return (1 << 24) - 1;
571                     }
572                     else
573                     {
574                         return (unsigned)(val);
575                     }
576                 }
577             }
578 
579             //------------------------------------------------------------------------------------
580             /* Please see man pages for details
581 
582 
583 
584 
585 
586             */
Float32ToUnsignedFix16(f32 val)587             inline u32 Float32ToUnsignedFix16( f32 val )
588             {
589                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val) );
590 
591                 if (val <= 0 || (v_ & 0x7f800000) == 0x7f800000)
592                 {
593                     return 0;
594                 }
595                 else
596                 {
597                     val *= 1 << (16 - 0);
598                     if ( val >= (1 << 16) )
599                     {
600                                 return (1 << 16) - 1;
601                     }
602                     else
603                     {
604                                 return (unsigned)( val );
605                     }
606                 }
607             }
608 
609             //------------------------------------------------------------------------------------
610             /* Please see man pages for details
611 
612 
613 
614 
615 
616             */
Float32ToFix16(f32 val)617             inline u16 Float32ToFix16( f32 val )
618             {
619                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
620 
621                 if ( (val == 0.f) || ( (v_ & 0x7f800000) == 0x7f800000 ) )
622                 {
623                     return 0;
624                 }
625                 else
626                 {
627                     val += 0.5f * (1 << 4);
628                     val *= 1 << (16 - 4);
629                     if (val < 0)
630                     {
631                         val = 0;
632                     }
633                     else if (val >= (1 << 16))
634                     {
635                         val = (1 << 16) - 1;
636                     }
637 
638                     if (val >= (1 << (16 - 1)))
639                     {
640                         return (unsigned)(val - (1 << (16 - 1)));
641                     }
642                     else
643                     {
644                         return (unsigned)(val + (1 << (16 - 1)));
645                     }
646                 }
647             }
648 
649             //------------------------------------------------------------------------------------
650 
651             /* Please see man pages for details
652 
653 
654 
655 
656 
657             */
Float32ToUnsignedFix12(f32 val)658             inline u32 Float32ToUnsignedFix12( f32 val )
659             {
660                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
661                 if( val <= 0 || (v_ & 0x7f800000) == 0x7f800000 )
662                 {
663                     return 0;
664                 }
665                 else
666                 {
667                     unsigned uval_;
668 
669                     val *= 1 << (12 - 0);
670                     if (val >= (1 << 12))
671                     {
672                         uval_ = (1 << 12) - 1;
673                     }
674                     else
675                     {
676                         uval_ = (unsigned)(val);
677                     }
678 
679                     return uval_;
680                 }
681             }
682 
683             //------------------------------------------------------------------------------------
684 
685             /* Please see man pages for details
686 
687 
688 
689 
690 
691             */
Float32ToFix12(f32 val)692             inline u32 Float32ToFix12( f32 val )
693             {
694                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
695                 if( val == 0.f || (v_ & 0x7f800000) == 0x7f800000 )
696                     return 0;
697 
698                 int ret;
699 
700                 val *= (1 << (12 - 1));
701 
702                 if( val < 0 )
703                 {
704                     ret = 1 << (12 - 1);
705                     val = -val;
706                 }
707                 else
708                     ret = 0;
709 
710                 if( val >= (1 << (12 - 1)) )
711                     val  = (1 << (12 - 1)) - 1;
712 
713                 ret |= (unsigned)(val);
714                 return ret;
715             }
716 
717             /* Please see man pages for details
718 
719 
720 
721 
722 
723             */
Float32ToFix12Fraction11(f32 val)724             inline u32 Float32ToFix12Fraction11( f32 val )
725             {
726                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
727 
728                 if (val == 0.f || (v_ & 0x7f800000) == 0x7f800000)
729                 {
730                     return 0;
731                 }
732                 else
733                 {
734                     val += 0.5f * (1 << 1);
735                     val *= 1 << (12 - 1);
736 
737                     if (val < 0)
738                     {
739                         val = 0;
740                     }
741                     else if (val >= (1 << 12))
742                     {
743                             val = (1 << 12) - 1;
744                     }
745 
746                     if (val >= (1 << (12 - 1)))
747                     {
748                         return (unsigned)(val - (1 << (12 - 1)));
749                     }
750                     else
751                     {
752                         return (unsigned)(val + (1 << (12 - 1)));
753                     }
754                 }
755             }
756 
757             //------------------------------------------------------------------------------------
758 
759             /* Please see man pages for details
760 
761 
762 
763 
764 
765             */
Float32ToFix13Fraction8(f32 val)766             inline u32 Float32ToFix13Fraction8( f32 val )
767             {
768                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
769                 if ( ( val == 0.0f ) ||
770                      ( (v_ & 0x7f800000) == 0x7f800000 ) )
771                 {
772                     return 0;
773                 }
774 
775                 val += 0.5f * (1 << 5);
776                 val *= 1 << (13 - 5);
777                 if (val < 0)
778                 {
779                     val = 0;
780                 }
781                 else if (val >= (1 << 13))
782                 {
783                     val = (1 << 13) - 1;
784                 }
785 
786                 return (val >= (1 << (13 - 1))) ? (unsigned)(val - (1 << (13 - 1))) : (unsigned)(val + (1 << (13 - 1)));
787             }
788 
789             //------------------------------------------------------------------------------------
790 
791             /* Please see man pages for details
792 
793 
794 
795 
796 
797             */
Float32ToFix13Fraction11(f32 val)798             inline u32 Float32ToFix13Fraction11( f32 val )
799             {
800                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
801                 if( val == 0.f || (v_ & 0x7f800000) == 0x7f800000 )
802                     return 0;
803 
804                 val += 0.5f * (1 << 2);
805                 val *= 1 << (13 - 2);
806                 if (val < 0)
807                     val = 0;
808                 else if (val >= (1 << 13))
809                     val = (1 << 13) - 1;
810 
811                 return (val >= (1 << (13 - 1))) ? (unsigned)(val - (1 << (13 - 1))) : (unsigned)(val + (1 << (13 - 1)));
812             }
813 
814             //------------------------------------------------------------------------------------
815 
816             /* Please see man pages for details
817 
818 
819 
820 
821 
822             */
Float32ToUnsignedFix11(f32 val)823             inline u32 Float32ToUnsignedFix11( f32 val )
824             {
825                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
826                 if( val <= 0 || (v_ & 0x7f800000) == 0x7f800000 )
827                     return 0;
828 
829                 unsigned uval_;
830 
831                 val *= 1 << (11 - 0);
832                 if (val >= (1 << 11))
833                     uval_ = (1 << 11) - 1;
834                 else
835                     uval_ = (unsigned)(val);
836 
837                 return uval_;
838             }
839 
840              //------------------------------------------------------------------------------------
841 
842             /* Please see man pages for details
843 
844 
845 
846 
847 
848             */
Float32ToFix8Fraction7(f32 val)849             inline u8 Float32ToFix8Fraction7( f32 val )
850             {
851                 unsigned v_ = *( reinterpret_cast<unsigned*>( &val ) );
852 
853                 if (val == 0.f || (v_ & 0x7f800000) == 0x7f800000)
854                 {
855                     return 0;
856                 }
857                 else
858                 {
859                     val += 0.5f * (1 << 1);
860                     val *= 1 << (8 - 1);
861 
862                     if (val < 0)
863                     {
864                         val = 0;
865                     }
866                     else if (val >= (1 << 8))
867                     {
868                         val = (1 << 8) - 1;
869                     }
870 
871                     if (val >= (1 << (8 - 1)))
872                     {
873                         return (unsigned)(val - (1 << (8 - 1)));
874                     }
875                     else
876                     {
877                         return (unsigned)(val + (1 << (8 - 1)));
878                     }
879                 }
880             }
881 
882             //------------------------------------------------------------------------------------
883             /* Please see man pages for details
884 
885 
886 
887 
888 
889             */
PicaDataVertexAttrTypeToByteSize(const PicaDataVertexAttrType type)890             inline u32 PicaDataVertexAttrTypeToByteSize( const PicaDataVertexAttrType type )
891             {
892                 switch ( type )
893                 {
894                     case PICA_DATA_SIZE_1_BYTE          : return 1 * sizeof(  s8 );
895                     case PICA_DATA_SIZE_1_UNSIGNED_BYTE : return 1 * sizeof(  u8 );
896                     case PICA_DATA_SIZE_1_SHORT         : return 1 * sizeof( s16 );
897                     case PICA_DATA_SIZE_1_FLOAT         : return 1 * sizeof( f32 );
898                     case PICA_DATA_SIZE_2_BYTE          : return 2 * sizeof(  s8 );
899                     case PICA_DATA_SIZE_2_UNSIGNED_BYTE : return 2 * sizeof(  u8 );
900                     case PICA_DATA_SIZE_2_SHORT         : return 2 * sizeof( s16 );
901                     case PICA_DATA_SIZE_2_FLOAT         : return 2 * sizeof( f32 );
902                     case PICA_DATA_SIZE_3_BYTE          : return 3 * sizeof(  s8 );
903                     case PICA_DATA_SIZE_3_UNSIGNED_BYTE : return 3 * sizeof(  u8 );
904                     case PICA_DATA_SIZE_3_SHORT         : return 3 * sizeof( s16 );
905                     case PICA_DATA_SIZE_3_FLOAT         : return 3 * sizeof( f32 );
906                     case PICA_DATA_SIZE_4_BYTE          : return 4 * sizeof(  s8 );
907                     case PICA_DATA_SIZE_4_UNSIGNED_BYTE : return 4 * sizeof(  u8 );
908                     case PICA_DATA_SIZE_4_SHORT         : return 4 * sizeof( s16 );
909                     case PICA_DATA_SIZE_4_FLOAT         : return 4 * sizeof( f32 );
910                 }
911                 return 0;
912             }
913 
914 
915             //------------------------------------------------------------------------------------
916 
917             /* Please see man pages for details
918 
919 
920 
921 
922 
923 
924 
925             */
926             bit32* MakeDisableAllCommand( bit32* command );
927 
928             //------------------------------------------------------------------------------------
929 
930             /* Please see man pages for details
931 
932             */
933             enum CommandBufferChannel
934             {
935                 //
936                 CMD_BUF_CH0 = 0x0,
937                 //
938                 CMD_BUF_CH1 = 0x1
939             };
940 
941             /* Please see man pages for details
942 
943 
944 
945 
946 
947 
948             */
949             bit32* MakeChannelKickCommand( bit32* command, const CommandBufferChannel channel );
950 
951             //------------------------------------------------------------------------------------
952 
953             /* Please see man pages for details
954 
955 
956 
957 
958 
959 
960 
961 
962             */
963             bit32* MakeChannel0SubroutineCommand( bit32* command, uptr* bufferSizePtr, const uptr commandBufferPtr, const size_t commandBufferSize );
964 
965             //------------------------------------------------------------------------------------
966 
967             /* Please see man pages for details
968 
969 
970 
971 
972 
973 
974 
975 
976             */
977             bit32* MakeChannel1SubroutineCommand( bit32* command, uptr* bufferSizePtr, const uptr commandBufferPtr, const size_t commandBufferSize );
978 
979             //------------------------------------------------------------------------------------
980 
981             /* Please see man pages for details
982 
983 
984 
985 
986 
987 
988 
989             */
990             bit32* MakeChannel0JumpCommand( bit32* command, const uptr commandBufferPtr, const size_t commandBufferSize );
991 
992             //------------------------------------------------------------------------------------
993 
994             /* Please see man pages for details
995 
996 
997 
998 
999 
1000 
1001 
1002             */
1003             bit32* MakeChannel1JumpCommand( bit32* command, const uptr commandBufferPtr, const size_t commandBufferSize );
1004 
1005             /* Please see man pages for details
1006 
1007             */
1008             enum PostVertexCacheEntryType
1009             {
1010                 //
1011                 POST_VERTEX_CACHE_ENTRY_TYPE_32 = 0x0,
1012                 //
1013                 POST_VERTEX_CACHE_ENTRY_TYPE_16 = 0x1
1014             };
1015 
1016             /* Please see man pages for details
1017 
1018 
1019 
1020 
1021 
1022 
1023             */
1024             bit32* MakePostVertexCacheEntryTypeCommand( bit32* command, const PostVertexCacheEntryType type );
1025 
1026             //------------------------------------------------------------------------------------
1027 
Jump(uptr subroutine,size_t subroutineSize)1028             inline void CommandBufferJumpHelper::Jump(uptr subroutine, size_t subroutineSize )
1029             {
1030                 if ( !subroutine )
1031                     return;
1032 
1033                 // Use of nn::gr::MakeChannel0SubroutineCommand is presumed, the size of commands being added is assumed to be 24 bytes.
1034                 m_CurrentPtr = AddDummyDataForCommandBuffer(m_CurrentPtr, GetOffsetFromPtr(m_ReturnPtr, m_CurrentPtr) + 24);
1035 
1036                 uptr cmdSizePtr;
1037                 m_CurrentPtr = MakeChannel0SubroutineCommand(m_CurrentPtr, &cmdSizePtr, subroutine, subroutineSize);
1038 
1039                 if ( m_CmdSizePtr )
1040                 {
1041                     *(reinterpret_cast<u32*>(m_CmdSizePtr)) = GetOffsetFromPtr(m_ReturnPtr, m_CurrentPtr) >> 3;
1042                 }
1043                 m_CmdSizePtr = cmdSizePtr;
1044                 m_ReturnPtr  = m_CurrentPtr;
1045 
1046                 if ( m_SizeToFirstJump == 0 )
1047                 {
1048                     m_SizeToFirstJump = GetOffsetFromPtr(m_StartPtr, m_CurrentPtr);
1049                 }
1050             }
1051         } // namespace CTR
1052     } // namespace gr
1053 } // namespace nn
1054 
1055 #endif // NN_GR_CTR_GR_UTILITY_H_
1056