1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ResFragmentShader.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: $
16  *---------------------------------------------------------------------------*/
17 
18 #include "../precompiled.h"
19 
20 #include <nw/gfx/res/gfx_ResUtil.h>
21 #include <nw/gfx/res/gfx_ResFragmentShader.h>
22 #include <nw/gfx/res/gfx_ResGraphicsFile.h>
23 
24 namespace nw {
25 namespace gfx {
26 namespace res {
27 
28 //----------------------------------------------------------------------------
29 Result
Setup(os::IAllocator * allocator,ResGraphicsFile graphicsFile)30 ResFragmentShader::Setup(os::IAllocator* allocator, ResGraphicsFile graphicsFile)
31 {
32     NW_ASSERT( this->IsValid() );
33 
34     Result result = RESOURCE_RESULT_OK;
35 
36     ResFragmentLightingTable resFragmentLightingTable = this->GetFragmentLightingTable();
37     result |= resFragmentLightingTable.Setup(allocator, graphicsFile);
38 
39     // FragmentLightingのEnableフラグとレイヤ・コンフィグに沿って参照テーブルが配置されているかチェックします。
40     if (!this->CheckFragmentShader())
41     {
42         result |= RESOURCE_RESULT_IRRELEVANT_LOCATION_LUT;
43     }
44 
45     return result;
46 }
47 
48 //----------------------------------------------------------------------------
49 Result
Setup(os::IAllocator * allocator,ResGraphicsFile graphicsFile)50 ResFragmentLightingTable::Setup(os::IAllocator* allocator, ResGraphicsFile graphicsFile)
51 {
52     NW_UNUSED_VARIABLE(allocator);
53     NW_ASSERT( this->IsValid() );
54 
55     Result result = RESOURCE_RESULT_OK;
56 
57     if (this->GetDistribution0Sampler().IsValid())
58     {
59         ResReferenceLookupTable resReferenceLut =
60             ResDynamicCast<ResReferenceLookupTable>(this->GetDistribution0Sampler().GetSampler());
61 
62         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
63 
64         if (referenceResult.IsSuccess())
65         {
66             result |= this->GetDistribution0Sampler().GetSampler().Setup();
67         }
68 
69         result |= referenceResult;
70     }
71 
72     if (this->GetDistribution1Sampler().IsValid())
73     {
74         ResReferenceLookupTable resReferenceLut =
75             ResDynamicCast<ResReferenceLookupTable>(this->GetDistribution1Sampler().GetSampler());
76 
77         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
78 
79         if (referenceResult.IsSuccess())
80         {
81             result |= this->GetDistribution1Sampler().GetSampler().Setup();
82         }
83 
84         result |= referenceResult;
85     }
86 
87     if (this->GetReflectanceRSampler().IsValid())
88     {
89         ResReferenceLookupTable resReferenceLut =
90             ResDynamicCast<ResReferenceLookupTable>(this->GetReflectanceRSampler().GetSampler());
91 
92         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
93 
94         if (referenceResult.IsSuccess())
95         {
96             result |= this->GetReflectanceRSampler().GetSampler().Setup();
97         }
98 
99         result |= referenceResult;
100     }
101 
102     if (this->GetReflectanceGSampler().IsValid())
103     {
104         ResReferenceLookupTable resReferenceLut =
105             ResDynamicCast<ResReferenceLookupTable>(this->GetReflectanceGSampler().GetSampler());
106 
107         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
108 
109         if (referenceResult.IsSuccess())
110         {
111             result |= this->GetReflectanceGSampler().GetSampler().Setup();
112         }
113 
114         result |= referenceResult;
115     }
116 
117     if (this->GetReflectanceBSampler().IsValid())
118     {
119         ResReferenceLookupTable resReferenceLut =
120             ResDynamicCast<ResReferenceLookupTable>(this->GetReflectanceBSampler().GetSampler());
121 
122         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
123 
124         if (referenceResult.IsSuccess())
125         {
126             result |= this->GetReflectanceBSampler().GetSampler().Setup();
127         }
128 
129         result |= referenceResult;
130     }
131 
132     if (this->GetFresnelSampler().IsValid())
133     {
134         ResReferenceLookupTable resReferenceLut =
135             ResDynamicCast<ResReferenceLookupTable>(this->GetFresnelSampler().GetSampler());
136 
137         Result referenceResult = SetupReferenceLut(resReferenceLut, graphicsFile);
138 
139         if (referenceResult.IsSuccess())
140         {
141             result |= this->GetFresnelSampler().GetSampler().Setup();
142         }
143 
144         result |= referenceResult;
145     }
146 
147     return result;
148 }
149 
150 //----------------------------------------
151 bool
CheckFragmentShader()152 ResFragmentShader::CheckFragmentShader()
153 {
154     bool isExisted = true;
155 
156     ResFragmentLighting fragmentLighting = this->GetFragmentLighting();
157     ResFragmentLightingTable fragmentLightingTable = this->GetFragmentLightingTable();
158 
159     s32 flags = fragmentLighting.GetFlags();
160     if ( ut::CheckFlag(flags, ResFragmentLightingData::FLAG_DISTRIBUTION0_ENABLED) &&
161         (!fragmentLightingTable.GetDistribution0Sampler().IsValid() || !fragmentLightingTable.GetDistribution0Sampler().GetSampler().IsValid()))
162     {
163         flags = ut::DisableFlag(flags, ResFragmentLightingData::FLAG_DISTRIBUTION0_ENABLED);
164         NW_WARNING(false, "Distribution0 is enable, but LUT is not found.");
165         isExisted = false;
166     }
167 
168     if ( ut::CheckFlag(flags, ResFragmentLightingData::FLAG_DISTRIBUTION1_ENABLED) &&
169         (!fragmentLightingTable.GetDistribution1Sampler().IsValid() || !fragmentLightingTable.GetDistribution1Sampler().GetSampler().IsValid()))
170     {
171         flags = ut::DisableFlag(flags, ResFragmentLightingData::FLAG_DISTRIBUTION1_ENABLED);
172         NW_WARNING(false, "Distribution1 is enable, but LUT is not found.");
173         isExisted = false;
174     }
175 
176     if ( fragmentLighting.GetFresnelConfig() != ResFragmentLighting::CONFIG_NO_FRESNEL &&
177         (!fragmentLightingTable.GetFresnelSampler().IsValid() || !fragmentLightingTable.GetFresnelSampler().GetSampler().IsValid()))
178     {
179         fragmentLighting.SetFresnelConfig(ResFragmentLighting::CONFIG_NO_FRESNEL);
180         NW_WARNING(false, "Fresnel is enable, but LUT is not found.");
181         isExisted = false;
182     }
183 
184     if ( ut::CheckFlag(flags, ResFragmentLightingData::FLAG_REFLECTION_ENABLED) )
185     {
186         ResFragmentLighting::ConfigDetail config = ResFragmentLighting::ToConfigDetail(fragmentLighting.GetLayerConfig());
187 
188         bool invalid = false;
189 
190         if ( ut::CheckFlag(config, ResFragmentLighting::LUT_RR) &&
191             (!fragmentLightingTable.GetReflectanceRSampler().IsValid() || !fragmentLightingTable.GetReflectanceRSampler().GetSampler().IsValid()))
192         {
193             invalid = true;
194         }
195 
196         if ( ut::CheckFlag(config, ResFragmentLighting::LUT_RG) &&
197             (!fragmentLightingTable.GetReflectanceGSampler().IsValid() || !fragmentLightingTable.GetReflectanceGSampler().GetSampler().IsValid()))
198         {
199             invalid = true;
200         }
201 
202         if ( ut::CheckFlag(config, ResFragmentLighting::LUT_RB) &&
203             (!fragmentLightingTable.GetReflectanceBSampler().IsValid() || !fragmentLightingTable.GetReflectanceBSampler().GetSampler().IsValid()))
204         {
205             invalid = true;
206         }
207 
208         if ( invalid )
209         {
210             flags = ut::DisableFlag(flags, ResFragmentLightingData::FLAG_REFLECTION_ENABLED);
211             NW_WARNING(false, "Relection is enable, but LUT is not found.\n");
212             isExisted = false;
213         }
214     }
215     fragmentLighting.SetFlags(flags);
216     return isExisted;
217 }
218 
219 //----------------------------------------------------------------------------
220 void
Cleanup()221 ResFragmentShader::Cleanup()
222 {
223     ut::SafeCleanup(this->GetFragmentLightingTable());
224 }
225 
226 //----------------------------------------------------------------------------
227 void
Cleanup()228 ResFragmentLightingTable::Cleanup()
229 {
230     ut::SafeCleanup(this->GetReflectanceRSampler());
231     ut::SafeCleanup(this->GetReflectanceGSampler());
232     ut::SafeCleanup(this->GetReflectanceBSampler());
233     ut::SafeCleanup(this->GetDistribution0Sampler());
234     ut::SafeCleanup(this->GetDistribution1Sampler());
235     ut::SafeCleanup(this->GetFresnelSampler());
236 }
237 
238 } /* namespace res */
239 } /* namespace gfx */
240 } /* namespace nw */
241 
242