1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_GraphicsDevice.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision: 27810 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/gfx/gfx_GraphicsDevice.h>
19 #include <nw/gfx/gfx_CommandUtil.h>
20 #include <nw/dev.h>
21 
22 namespace nw
23 {
24 namespace gfx
25 {
26 
27 enum
28 {
29     REG_BUFFER_CACHE_CLEAR     = 0x110,
30     REG_BUFFER_CACHE_TAG_CLEAR = 0x111,
31     REG_BUFFER_MASK_BASE       = 0x112
32 };
33 
34 uint GraphicsDevice::s_LutIsAbs = 0;
35 uint GraphicsDevice::s_LutInput = 0;
36 uint GraphicsDevice::s_LutScale = 0;
37 
38 u32  GraphicsDevice::s_DepthFormat = RENDER_DEPTH_FORMAT_24_STENCIL8;
39 u32  GraphicsDevice::s_WScale24 = 0;
40 f32  GraphicsDevice::s_DepthRangeNear = 0.0f;
41 f32  GraphicsDevice::s_DepthRangeFar  = 1.0f;
42 u32  GraphicsDevice::s_DepthRange24   = ut::Float24::Float32ToBits24( -1.0f );
43 bool GraphicsDevice::s_PolygonOffsetEnabled = false;
44 f32  GraphicsDevice::s_PolygonOffsetUnit = 0.0f;
45 ResImageLookupTable GraphicsDevice::m_LutTargets[LUT_TARGET_COUNT];
46 
47 u32  GraphicsDevice::s_LightPositionW = 0;
48 u32  GraphicsDevice::s_LightShadowed = 0xFF;
49 u32  GraphicsDevice::s_LightSpotEnabled = 0xFF;
50 u32  GraphicsDevice::s_LightDistanceAttnEnabled = 0xFF;
51 
52 ResFragmentOperation::FragmentOperationMode GraphicsDevice::s_FragOperationMode = ResFragmentOperation::FRAGMENT_OPERATION_MODE_GL;
53 bool GraphicsDevice::s_IsFrameBufferUpdated = true; // 初期値を true にしないと初回時に更新されない。
54 bool GraphicsDevice::s_BlendEnabled = true;
55 bool GraphicsDevice::s_DepthTestEnabled = true;
56 bool GraphicsDevice::s_StencilTestEnabled = false;
57 bool GraphicsDevice::s_DepthMask = true;
58 u8  GraphicsDevice::s_StencilMask = 0xFF;
59 u32 GraphicsDevice::s_ColorMask = 0xF;
60 
61 u32 GraphicsDevice::s_RenderBufferWidth = 400;
62 u32 GraphicsDevice::s_RenderBufferHeight = 240;
63 
64 u32 GraphicsDevice::s_FrameBufferCommand[10] =
65 {
66     1,
67     internal::MakeCommandHeader(REG_BUFFER_CACHE_TAG_CLEAR, 1, false, 0xF),
68     1,
69     internal::MakeCommandHeader(REG_BUFFER_CACHE_CLEAR, 1, false, 0xF),
70     0,
71     internal::MakeCommandHeader(REG_BUFFER_MASK_BASE, 4, true, 0xF),
72     0,
73     0,
74     0,
75     0
76 };
77 
78 //----------------------------------------
79 void
ActivateLookupTable(ResImageLookupTable lookupTable,LutTarget target)80 GraphicsDevice::ActivateLookupTable(ResImageLookupTable lookupTable, LutTarget target)
81 {
82     NW_ASSERT(lookupTable.IsValid());
83     NW_MINMAXLT_ASSERT(target, LUT_TARGET_D0, LUT_TARGET_COUNT);
84 
85     if (m_LutTargets[target] != lookupTable)
86     {
87         m_LutTargets[target] = lookupTable;
88         ActivateLutLoadSetting(target);
89         internal::NWUseCmdlist( lookupTable.GetCommandCache(), lookupTable.GetCommandCacheCount() );
90     }
91 }
92 
93 //----------------------------------------
94 void
InvalidateLookupTable(ResImageLookupTable lookupTable)95 GraphicsDevice::InvalidateLookupTable(ResImageLookupTable lookupTable)
96 {
97     NW_ASSERT(lookupTable.IsValid());
98 
99     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
100     {
101         if (m_LutTargets[target] == lookupTable)
102         {
103             m_LutTargets[target] = ResImageLookupTable(NULL);
104         }
105     }
106 }
107 
108 //----------------------------------------
109 void
InvalidateAllLookupTables()110 GraphicsDevice::InvalidateAllLookupTables()
111 {
112     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
113     {
114         m_LutTargets[target] = ResImageLookupTable(NULL);
115     }
116 }
117 
118 //----------------------------------------
119 void
Report()120 GraphicsDevice::Report()
121 {
122 #if !defined(NW_RELEASE)
123     NW_LOG("\n<<RenderBuffer>>\n");
124     NW_LOG("[RenderBufferWidth]\n");
125     NW_LOG("    0x%x\n", s_RenderBufferWidth);
126 
127     NW_LOG("[RenderBufferHeight]\n");
128     NW_LOG("    0x%x\n", s_RenderBufferHeight);
129 
130     NW_LOG("[DepthFormat]\n");
131     NW_LOG("    0x%x\n", s_DepthFormat);
132 
133     NW_LOG("[WScale24]\n");
134     NW_LOG("    0x%x\n", s_WScale24);
135 
136     NW_LOG("[DepthRangeNear]\n");
137     NW_LOG("    %f\n", s_DepthRangeNear);
138 
139     NW_LOG("[DepthRangeFar]\n");
140     NW_LOG("    %f\n", s_DepthRangeFar);
141 
142     NW_LOG("[DepthRange24]\n");
143     NW_LOG("    0x%x\n", s_DepthRange24);
144 
145     NW_LOG("\n<<FragmentOperation>>\n");
146     NW_LOG("[FragOperationMode]\n");
147     {
148         static const char* table[] =
149         {
150             "    FRAGMENT_OPERATION_MODE_GL",
151             "    FRAGMENT_OPERATION_MODE_GAS",
152             "    FRAGMENT_OPERATION_MODE_SHADOW"
153         };
154 
155         NW_LOG("    %s\n", table[s_FragOperationMode]);
156     }
157 
158     NW_LOG("[BlendEnabled]\n");
159     NW_LOG(s_BlendEnabled ? "    True\n" : "    False\n");
160 
161     NW_LOG("[DepthTestEnabled]\n");
162     NW_LOG(s_DepthTestEnabled ? "    True\n" : "    False\n");
163 
164     NW_LOG("[StencilTestEnabled]\n");
165     NW_LOG(s_StencilTestEnabled ? "    True\n" : "    False\n");
166 
167     NW_LOG("[DepthMask]\n");
168     NW_LOG(s_DepthMask ? "    True\n" : "    False\n");
169 
170     NW_LOG("[StencilMask]\n");
171     NW_LOG("    0x%x\n", s_StencilMask);
172 
173     NW_LOG("[ColorMask]\n");
174     NW_LOG("    0x%x\n", s_ColorMask);
175 
176     NW_LOG("\n<<FrameBuffer>>\n");
177     NW_LOG("[IsFrameBufferUpdated]\n");
178     NW_LOG(s_IsFrameBufferUpdated ? "    True\n" : "    False\n");
179     NW_LOG("[FrameBufferCommand]\n");
180     for (int i = 0; i < FRAME_BUFFER_COMMAND_COUNT; ++i)
181     {
182         NW_LOG("    0x%x\n", s_FrameBufferCommand[i]);
183     }
184 
185     NW_LOG("[PolygonOffsetEnabled]\n");
186     NW_LOG("    0x%x\n", s_PolygonOffsetEnabled);
187 
188     NW_LOG("[PolygonOffsetUnit]\n");
189     NW_LOG("    %f\n", s_PolygonOffsetUnit);
190 
191     NW_LOG("\n<<Light>>\n");
192     NW_LOG("[LightPositionW]\n");
193     NW_LOG("    0x%x\n", s_LightPositionW);
194 
195     NW_LOG("[LightShadowed]\n");
196     NW_LOG("    0x%x\n", s_LightShadowed);
197 
198     NW_LOG("[LightSpotEnabled]\n");
199     NW_LOG("    0x%x\n", s_LightSpotEnabled);
200 
201     NW_LOG("[LightDistanceAttnEnabled]\n");
202     NW_LOG("    0x%x\n", s_LightDistanceAttnEnabled);
203 
204     NW_LOG("\n<<LookupTable>>\n");
205     NW_LOG("[LutIsAbs]\n");
206     NW_LOG("    0x%x\n", s_LutIsAbs);
207 
208     NW_LOG("[LutInput]\n");
209     NW_LOG("    0x%x\n", s_LutInput);
210 
211     NW_LOG("[LutScale]\n");
212     NW_LOG("    0x%x\n", s_LutScale);
213 
214     NW_LOG("[LookupTableCache]\n");
215     NW_LOG(" attr :    address : name\n");
216 
217     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
218     {
219         static const char* table[] =
220         {
221             "D0", "D1", "SP", "FR", "RB", "RG", "RR",
222             "SP0", "SP1", "SP2", "SP3", "SP4", "SP5", "SP6", "SP7",
223             "DA0", "DA1", "DA2", "DA3", "DA4", "DA5", "DA6", "DA7",
224             "FOG"
225         };
226 
227         NW_LOG(
228             "%5s : 0x%8x : %s\n",
229             table[target],
230             m_LutTargets[target].ptr(),
231             m_LutTargets[target].IsValid() && m_LutTargets[target].GetName() != NULL ? m_LutTargets[target].GetName() : "NA");
232     }
233 #endif
234 }
235 
236 } // namespace gfx
237 } // namespace nw
238