1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ResUtil.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: 13983 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "../precompiled.h"
17 
18 #include <nw/gfx/res/gfx_ResUtil.h>
19 #include <nw/gfx/res/gfx_ResGraphicsFile.h>
20 
21 namespace nw {
22 namespace gfx {
23 namespace res {
24 
25 //----------------------------------------
26 Result
SetupReferenceLut(ResReferenceLookupTable resReferenceLut,ResGraphicsFile graphicsFile)27 SetupReferenceLut(ResReferenceLookupTable resReferenceLut, ResGraphicsFile graphicsFile)
28 {
29     Result result = RESOURCE_RESULT_OK;
30 
31     if (resReferenceLut.IsValid())
32     {
33         ResImageLookupTable resImageLut = ResDynamicCast<ResImageLookupTable>(resReferenceLut.GetTargetLut());
34         if (!resImageLut.IsValid())
35         {
36             ::std::pair<ResLookupTable, bool> referenceResult;
37             referenceResult = GetReferenceLutTarget(resReferenceLut, graphicsFile);
38 
39             if (!referenceResult.second)
40             {
41                 result |= Result::MASK_FAIL_BIT;
42                 result |= RESOURCE_RESULT_NOT_FOUND_LUT;
43             }
44         }
45     }
46 
47     return result;
48 }
49 
50 //----------------------------------------
51 ::std::pair<ResLookupTable, bool>
GetReferenceLutTarget(ResReferenceLookupTable referenceLut,ResGraphicsFile graphicsFile)52 GetReferenceLutTarget(ResReferenceLookupTable referenceLut, ResGraphicsFile graphicsFile)
53 {
54     bool isExisted = false;
55     const char* path = referenceLut.GetPath();
56     const char* tableName = referenceLut.GetTableName();
57     int lutSetNum = graphicsFile.GetLutSetsCount();
58 
59     ResLookupTableSet resLutSet = graphicsFile.GetLutSets(path);
60 
61     if (resLutSet.IsValid())
62     {
63         ResImageLookupTable resLut =
64             ResDynamicCast<ResImageLookupTable>(resLutSet.GetSamplers(tableName));
65 
66         if (resLut.IsValid())
67         {
68             referenceLut.ref().toTargetLut.set_ptr(resLut.ptr());
69             isExisted = true;
70         }
71     }
72 
73     return ::std::make_pair(referenceLut.GetTargetLut(), isExisted);
74 }
75 
76 //----------------------------------------------------------------------------
77 ::std::pair<ResTexture, bool>
GetReferenceTextureTarget(ResReferenceTexture referenceTexture,ResGraphicsFile graphicsFile)78 GetReferenceTextureTarget(ResReferenceTexture referenceTexture, ResGraphicsFile graphicsFile)
79 {
80     bool isExisted = false;
81     const char* path = referenceTexture.GetPath();
82     ResTexture resTexture = graphicsFile.GetTextures(path);
83 
84     if (resTexture.IsValid())
85     {
86         ResReferenceTexture refer = ResDynamicCast<ResReferenceTexture>(resTexture);
87 
88         // 再帰呼び出しによって参照解決する
89         if (refer.IsValid())
90         {
91             ::std::pair<ResTexture, bool> referenceResult;
92             referenceResult = GetReferenceTextureTarget(refer, graphicsFile);
93             if (referenceResult.second)
94             {
95                 referenceTexture.ref().toTargetTexture.set_ptr(referenceResult.first.ptr());
96                 isExisted = true;
97             }
98         }
99         else
100         {
101             referenceTexture.ref().toTargetTexture.set_ptr(resTexture.ptr());
102             isExisted = true;
103         }
104     }
105 
106     return ::std::make_pair(referenceTexture.GetTargetTexture(), isExisted);
107 }
108 
109 //----------------------------------------
110 ::std::pair<ResShader, bool>
GetReferenceShaderTarget(ResReferenceShader referenceShader,ResGraphicsFile graphicsFile)111 GetReferenceShaderTarget(ResReferenceShader referenceShader, ResGraphicsFile graphicsFile)
112 {
113     bool isExisted = false;
114     const char* path = referenceShader.GetPath();
115     ResShader resShader = graphicsFile.GetShaders(path);
116 
117     if (resShader.IsValid())
118     {
119         ResReferenceShader refer = ResDynamicCast<ResReferenceShader>(resShader);
120 
121         // 再帰呼び出しによって参照解決する
122         if (refer.IsValid())
123         {
124             ::std::pair<ResShader, bool> referenceResult;
125             referenceResult = GetReferenceShaderTarget(refer, graphicsFile);
126             if (referenceResult.second)
127             {
128                 referenceShader.ref().toTargetShader.set_ptr(referenceResult.first.ptr());
129                 isExisted = true;
130             }
131         }
132         else
133         {
134             referenceShader.ref().toTargetShader.set_ptr(resShader.ptr());
135             isExisted = true;
136         }
137     }
138 
139     return ::std::make_pair(referenceShader.GetTargetShader(),isExisted);
140 }
141 
142 } /* namespace res */
143 } /* namespace gfx */
144 } /* namespace nw */
145