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