1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #include "../Precompiled.h"
24 
25 #include "../Core/Context.h"
26 #include "../Core/Profiler.h"
27 #include "../Graphics/Graphics.h"
28 #include "../Graphics/GraphicsEvents.h"
29 #include "../Graphics/GraphicsImpl.h"
30 #include "../Graphics/Renderer.h"
31 #include "../Graphics/Texture2DArray.h"
32 #include "../IO/FileSystem.h"
33 #include "../IO/Log.h"
34 #include "../Resource/ResourceCache.h"
35 #include "../Resource/XMLFile.h"
36 
37 #include "../DebugNew.h"
38 
39 #ifdef _MSC_VER
40 #pragma warning(disable:4355)
41 #endif
42 
43 namespace Urho3D
44 {
45 
Texture2DArray(Context * context)46 Texture2DArray::Texture2DArray(Context* context) :
47     Texture(context),
48     layers_(0)
49 {
50 #ifdef URHO3D_OPENGL
51 #ifndef GL_ES_VERSION_2_0
52     target_ = GL_TEXTURE_2D_ARRAY;
53 #else
54     target_ = 0;
55 #endif
56 #endif
57 }
58 
~Texture2DArray()59 Texture2DArray::~Texture2DArray()
60 {
61     Release();
62 }
63 
RegisterObject(Context * context)64 void Texture2DArray::RegisterObject(Context* context)
65 {
66     context->RegisterFactory<Texture2DArray>();
67 }
68 
BeginLoad(Deserializer & source)69 bool Texture2DArray::BeginLoad(Deserializer& source)
70 {
71     ResourceCache* cache = GetSubsystem<ResourceCache>();
72 
73     // In headless mode, do not actually load the texture, just return success
74     if (!graphics_)
75         return true;
76 
77     // If device is lost, retry later
78     if (graphics_->IsDeviceLost())
79     {
80         URHO3D_LOGWARNING("Texture load while device is lost");
81         dataPending_ = true;
82         return true;
83     }
84 
85     cache->ResetDependencies(this);
86 
87     String texPath, texName, texExt;
88     SplitPath(GetName(), texPath, texName, texExt);
89 
90     loadParameters_ = (new XMLFile(context_));
91     if (!loadParameters_->Load(source))
92     {
93         loadParameters_.Reset();
94         return false;
95     }
96 
97     loadImages_.Clear();
98 
99     XMLElement textureElem = loadParameters_->GetRoot();
100     XMLElement layerElem = textureElem.GetChild("layer");
101     while (layerElem)
102     {
103         String name = layerElem.GetAttribute("name");
104 
105         // If path is empty, add the XML file path
106         if (GetPath(name).Empty())
107             name = texPath + name;
108 
109         loadImages_.Push(cache->GetTempResource<Image>(name));
110         cache->StoreResourceDependency(this, name);
111 
112         layerElem = layerElem.GetNext("layer");
113     }
114 
115     // Precalculate mip levels if async loading
116     if (GetAsyncLoadState() == ASYNC_LOADING)
117     {
118         for (unsigned i = 0; i < loadImages_.Size(); ++i)
119         {
120             if (loadImages_[i])
121                 loadImages_[i]->PrecalculateLevels();
122         }
123     }
124 
125     return true;
126 }
127 
EndLoad()128 bool Texture2DArray::EndLoad()
129 {
130     // In headless mode, do not actually load the texture, just return success
131     if (!graphics_ || graphics_->IsDeviceLost())
132         return true;
133 
134     // If over the texture budget, see if materials can be freed to allow textures to be freed
135     CheckTextureBudget(GetTypeStatic());
136 
137     SetParameters(loadParameters_);
138     SetLayers(loadImages_.Size());
139 
140     for (unsigned i = 0; i < loadImages_.Size(); ++i)
141         SetData(i, loadImages_[i]);
142 
143     loadImages_.Clear();
144     loadParameters_.Reset();
145 
146     return true;
147 }
148 
SetLayers(unsigned layers)149 void Texture2DArray::SetLayers(unsigned layers)
150 {
151     Release();
152 
153     layers_ = layers;
154 }
155 
SetSize(unsigned layers,int width,int height,unsigned format,TextureUsage usage)156 bool Texture2DArray::SetSize(unsigned layers, int width, int height, unsigned format, TextureUsage usage)
157 {
158     if (width <= 0 || height <= 0)
159     {
160         URHO3D_LOGERROR("Zero or negative texture array size");
161         return false;
162     }
163     if (usage == TEXTURE_DEPTHSTENCIL)
164     {
165         URHO3D_LOGERROR("Depth-stencil usage not supported for texture arrays");
166         return false;
167     }
168 
169     // Delete the old rendersurface if any
170     renderSurface_.Reset();
171 
172     usage_ = usage;
173 
174     if (usage == TEXTURE_RENDERTARGET)
175     {
176         renderSurface_ = new RenderSurface(this);
177 
178         // Nearest filtering by default
179         filterMode_ = FILTER_NEAREST;
180     }
181 
182     if (usage == TEXTURE_RENDERTARGET)
183         SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(Texture2DArray, HandleRenderSurfaceUpdate));
184     else
185         UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
186 
187     width_ = width;
188     height_ = height;
189     format_ = format;
190     depth_ = 1;
191     if (layers)
192         layers_ = layers;
193 
194     layerMemoryUse_.Resize(layers_);
195     for (unsigned i = 0; i < layers_; ++i)
196         layerMemoryUse_[i] = 0;
197 
198     return Create();
199 }
200 
HandleRenderSurfaceUpdate(StringHash eventType,VariantMap & eventData)201 void Texture2DArray::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
202 {
203     if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
204     {
205         Renderer* renderer = GetSubsystem<Renderer>();
206         if (renderer)
207             renderer->QueueRenderSurface(renderSurface_);
208         renderSurface_->ResetUpdateQueued();
209     }
210 }
211 
212 }
213