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: 22128 $
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 bool GraphicsDevice::s_ScissorEnabled = false;
61 
62 Viewport GraphicsDevice::s_Viewport;
63 ut::Rect GraphicsDevice::s_Scissor;
64 
65 u32 GraphicsDevice::s_RenderBufferWidth = 400;
66 u32 GraphicsDevice::s_RenderBufferHeight = 240;
67 
68 u32 GraphicsDevice::s_FrameBufferCommand[10] =
69 {
70     1,
71     internal::MakeCommandHeader(REG_BUFFER_CACHE_TAG_CLEAR, 1, false, 0xF),
72     1,
73     internal::MakeCommandHeader(REG_BUFFER_CACHE_CLEAR, 1, false, 0xF),
74     0,
75     internal::MakeCommandHeader(REG_BUFFER_MASK_BASE, 4, true, 0xF),
76     0,
77     0,
78     0,
79     0
80 };
81 
82 //----------------------------------------
83 void
ActivateLookupTable(ResImageLookupTable lookupTable,LutTarget target)84 GraphicsDevice::ActivateLookupTable(ResImageLookupTable lookupTable, LutTarget target)
85 {
86     NW_ASSERT(lookupTable.IsValid());
87     NW_MINMAXLT_ASSERT(target, LUT_TARGET_D0, LUT_TARGET_COUNT);
88 
89     if (m_LutTargets[target] != lookupTable)
90     {
91         m_LutTargets[target] = lookupTable;
92         ActivateLutLoadSetting(target);
93         internal::NWUseCmdlist( lookupTable.GetCommandCache(), lookupTable.GetCommandCacheCount() );
94     }
95 }
96 
97 //----------------------------------------
98 void
InvalidateLookupTable(ResImageLookupTable lookupTable)99 GraphicsDevice::InvalidateLookupTable(ResImageLookupTable lookupTable)
100 {
101     NW_ASSERT(lookupTable.IsValid());
102 
103     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
104     {
105         if (m_LutTargets[target] == lookupTable)
106         {
107             m_LutTargets[target] = ResImageLookupTable(NULL);
108         }
109     }
110 }
111 
112 //----------------------------------------
113 void
InvalidateAllLookupTables()114 GraphicsDevice::InvalidateAllLookupTables()
115 {
116     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
117     {
118         m_LutTargets[target] = ResImageLookupTable(NULL);
119     }
120 }
121 
122 //----------------------------------------
123 void
Report()124 GraphicsDevice::Report()
125 {
126     NW_LOG("[nw::gfx::LookupTableCache]\n");
127     NW_LOG("attr  : address : name\n");
128 
129     for (int target = 0; target < LUT_TARGET_COUNT; ++target)
130     {
131         static const char* table[] =
132         {
133             "D0",
134             "D1",
135             "SP",
136             "FR",
137             "RB",
138             "RG",
139             "RR",
140             "SP0",
141             "SP1",
142             "SP2",
143             "SP3",
144             "SP4",
145             "SP5",
146             "SP6",
147             "SP7",
148             "DA0",
149             "DA1",
150             "DA2",
151             "DA3",
152             "DA4",
153             "DA5",
154             "DA6",
155             "DA7",
156             "FOG"
157         };
158 
159         NW_LOG("%5s : 0x%8x : %s\n", table[target], m_LutTargets[target].ptr(), m_LutTargets[target].IsValid() ? m_LutTargets[target].GetName() : "");
160     }
161 }
162 
163 } // namespace gfx
164 } // namespace nw
165