1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_CommandCache.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: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef DEMO_COMMAND_CACHE_H_
17 #define DEMO_COMMAND_CACHE_H_
18 
19 #include "demo/Utility/demo_Utility.h"
20 
21 namespace demo
22 {
23 
24     namespace detail
25     {
26         /* Please see man pages for details
27 
28 
29 
30         */
31         struct CommandBuffer3d
32         {
33             unsigned data : 32; // Data to write to the register
34             unsigned addr : 16; // Address of the register where writing is performed
35             unsigned be   : 4;  // Byte enable
36             unsigned size : 7;  // Data count - 1. Single access if 0, burst access if 1 or greater.
37             unsigned rsv  : 4;  // Reserved region
38             unsigned seq  : 1;  // Access mode during burst access.
39                                 // Single register writing when 0, continuous register writing when 1 */
40         };
41 
42         // Address of register setting the index of the vertex shader floating point register
43         const u32 VS_UNIFORM_FLOAT_REG_INDEX = 0x2c0;
44         // Start address of the register to write the floating point uniform value of the vertex shader
45         const u32 VS_UNIFORM_FLOAT_REG_VALUE_BEGIN = 0x2c1;
46         // End address of the register to write the floating point uniform value of the vertex shader
47         const u32 VS_UNIFORM_FLOAT_REG_VALUE_END = 0x2c8;
48 
49         // Number of floating-point registers for the vertex shader
50         const u32 VS_UNIFORM_FLOAT_REG_NUM = 96;
51 
52         // Maximum number when the vertex shader floating point register is used as a 4x4 matrix
53         const u32 VS_UNIFORM_FLOAT_REG_MATRIX_NUM = VS_UNIFORM_FLOAT_REG_NUM / 16;
54 
55         /* Please see man pages for details
56 
57 
58 
59         */
60         class VertexShaderUniformMatrix :
61             private nn::util::NonCopyable<VertexShaderUniformMatrix>
62         {
63         public:
64             VertexShaderUniformMatrix(void);
65             virtual ~VertexShaderUniformMatrix();
66         public:
67             void Initialize(void);
68             void Finalize(void);
69 
70         public:
71             bool m_Valid;
72             bool m_Padding[3];
73 
74             // Index of floating-point registers for vertex shader
75             u32 m_RegisterIndex;
76             // Offset in the 3D command buffer
77             u32 m_Offset[4][4];
78         };
79 
80         void GetVSUniformOffset(u8* bufferAddress, const u32 startOffset,
81             const GLsizei bufferSize, const u32 uniformFloatRegIndex, u32* uniformOffsetArray);
82     }
83 
84     enum
85     {
86         COMMAND_CACHE_UNINITIALIZED, // Command list is uninitialized
87         COMMAND_CACHE_INITIALIZED,   // Command list is initialized
88         COMMAND_CACHE_FINALIZED,     // Command list is finalized
89         COMMAND_CACHE_BEGIN_SAVE,    // Save of command list has started
90         COMMAND_CACHE_END_SAVE       // Save of command list has completed
91     };
92 
93     const u32 DEMO_MAX_COMMAND_CACHE_MODELS_NUM = 128;
94     const u32 DEMO_MAX_COMMAND_CACHE_MATRIX_NUM = demo::detail::VS_UNIFORM_FLOAT_REG_MATRIX_NUM;
95 
96     /* Please see man pages for details
97 
98 
99 
100     */
101     class CommandCache : private nn::util::NonCopyable<CommandCache>
102     {
103     public:
104         CommandCache(void);
105         virtual ~CommandCache(void);
106 
107     public:
108         void Initialize(const u32 CommandListSize, const u32 RequestNum,
109             const GLboolean copyCommandBuffer, const GLbitfield stateMask,
110             const u32 totalModelNum = 1);
111         void Finalize(void);
112 
113     public:
114         // The index of the vertex shader floating point register is saved in the specified array index.
115         void SetVSUniformMatrixRegisterIndex(const u32 modelIndex, const u32 uniformMatrixIndex, const u32 regsiterIndex);
116 
117     public:
118         // Starts saving the command list.
119         void BeginSave(void);
120         // Saves the offset to the start of the command list when rendering one model
121         void SaveCommandBufferStartOffset(const u32 modelIndex);
122         // Ends saving the command list.
123         void EndSave(void);
124 
125     public:
126         // Updates the vertex shader floating point matrix values specified by the array index of the specified matrix.
127         void UpdateVSUniformMatrix(const u32 modelIndex, const u32 uniformMatrixIndex, const nn::math::MTX44& matrix);
128         // Adds this saved command list to the current command list.
129         void Append(void);
130 
131     public:
132         void Print(void);
133 
134     protected:
135         u32 m_State;
136 
137         GLsizei m_CommandListSize;
138         GLsizei m_CommandListRequestNum;
139         GLboolean m_CopyCommandBuffer;
140         GLboolean m_Padding[3];
141         GLbitfield m_StateMask;
142 
143         u32 m_TotalModelNum;
144         u32 m_ModelIndex;
145 
146         GLuint m_CurrentCommandListId;
147         GLuint m_CommandListId;
148         GLint m_CommandBufferStartOffsetArray[DEMO_MAX_COMMAND_CACHE_MODELS_NUM];
149         GLuint m_CommandBufferOffset;
150         GLsizei m_CommandBufferSize;
151         GLuint m_CommandRequestBeginId;
152         GLsizei m_CommandRequestSize;
153         u8* m_CommandBufferAddr;
154 
155         // Array to store the 3D command buffer offset of the vertex shader uniform matrix
156         detail::VertexShaderUniformMatrix m_VertexShaderUniformMatrixArray2d[DEMO_MAX_COMMAND_CACHE_MODELS_NUM][DEMO_MAX_COMMAND_CACHE_MATRIX_NUM];
157     };
158 
159     void PrintCurrentCmdlist(void);
160 
161 }
162 
163 #endif
164