| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // Copyright (c) 2008-2018 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Core/Context.h"
- #include "../Core/Profiler.h"
- #include "../Graphics/Graphics.h"
- #include "../Graphics/GraphicsEvents.h"
- #include "../Graphics/GraphicsImpl.h"
- #include "../Graphics/Renderer.h"
- #include "../Graphics/Texture2DArray.h"
- #include "../IO/FileSystem.h"
- #include "../IO/Log.h"
- #include "../Resource/ResourceCache.h"
- #include "../Resource/XMLFile.h"
- #include "../DebugNew.h"
- #ifdef _MSC_VER
- #pragma warning(disable:4355)
- #endif
- namespace Urho3D
- {
- Texture2DArray::Texture2DArray(Context* context) :
- Texture(context)
- {
- #ifdef URHO3D_OPENGL
- #ifndef GL_ES_VERSION_2_0
- target_ = GL_TEXTURE_2D_ARRAY;
- #endif
- #endif
- }
- Texture2DArray::~Texture2DArray()
- {
- Release();
- }
- void Texture2DArray::RegisterObject(Context* context)
- {
- context->RegisterFactory<Texture2DArray>();
- }
- bool Texture2DArray::BeginLoad(Deserializer& source)
- {
- auto* cache = GetSubsystem<ResourceCache>();
- // In headless mode, do not actually load the texture, just return success
- if (!graphics_)
- return true;
- // If device is lost, retry later
- if (graphics_->IsDeviceLost())
- {
- URHO3D_LOGWARNING("Texture load while device is lost");
- dataPending_ = true;
- return true;
- }
- cache->ResetDependencies(this);
- String texPath, texName, texExt;
- SplitPath(GetName(), texPath, texName, texExt);
- loadParameters_ = (new XMLFile(context_));
- if (!loadParameters_->Load(source))
- {
- loadParameters_.Reset();
- return false;
- }
- loadImages_.Clear();
- XMLElement textureElem = loadParameters_->GetRoot();
- XMLElement layerElem = textureElem.GetChild("layer");
- while (layerElem)
- {
- String name = layerElem.GetAttribute("name");
- // If path is empty, add the XML file path
- if (GetPath(name).Empty())
- name = texPath + name;
- loadImages_.Push(cache->GetTempResource<Image>(name));
- cache->StoreResourceDependency(this, name);
- layerElem = layerElem.GetNext("layer");
- }
- // Precalculate mip levels if async loading
- if (GetAsyncLoadState() == ASYNC_LOADING)
- {
- for (unsigned i = 0; i < loadImages_.Size(); ++i)
- {
- if (loadImages_[i])
- loadImages_[i]->PrecalculateLevels();
- }
- }
- return true;
- }
- bool Texture2DArray::EndLoad()
- {
- // In headless mode, do not actually load the texture, just return success
- if (!graphics_ || graphics_->IsDeviceLost())
- return true;
- // If over the texture budget, see if materials can be freed to allow textures to be freed
- CheckTextureBudget(GetTypeStatic());
- SetParameters(loadParameters_);
- SetLayers(loadImages_.Size());
- for (unsigned i = 0; i < loadImages_.Size(); ++i)
- SetData(i, loadImages_[i]);
- loadImages_.Clear();
- loadParameters_.Reset();
- return true;
- }
- void Texture2DArray::SetLayers(unsigned layers)
- {
- Release();
- layers_ = layers;
- }
- bool Texture2DArray::SetSize(unsigned layers, int width, int height, unsigned format, TextureUsage usage)
- {
- if (width <= 0 || height <= 0)
- {
- URHO3D_LOGERROR("Zero or negative texture array size");
- return false;
- }
- if (usage == TEXTURE_DEPTHSTENCIL)
- {
- URHO3D_LOGERROR("Depth-stencil usage not supported for texture arrays");
- return false;
- }
- // Delete the old rendersurface if any
- renderSurface_.Reset();
- usage_ = usage;
- if (usage == TEXTURE_RENDERTARGET)
- {
- renderSurface_ = new RenderSurface(this);
- // Nearest filtering by default
- filterMode_ = FILTER_NEAREST;
- }
- if (usage == TEXTURE_RENDERTARGET)
- SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(Texture2DArray, HandleRenderSurfaceUpdate));
- else
- UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
- width_ = width;
- height_ = height;
- format_ = format;
- depth_ = 1;
- if (layers)
- layers_ = layers;
- layerMemoryUse_.Resize(layers_);
- for (unsigned i = 0; i < layers_; ++i)
- layerMemoryUse_[i] = 0;
- return Create();
- }
- void Texture2DArray::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
- {
- if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
- {
- auto* renderer = GetSubsystem<Renderer>();
- if (renderer)
- renderer->QueueRenderSurface(renderSurface_);
- renderSurface_->ResetUpdateQueued();
- }
- }
- }
|