Browse Source

Fix texture reload when device is initially lost.

Lasse Öörni 11 years ago
parent
commit
d93fdb5aa4

+ 2 - 2
Source/Engine/Graphics/Direct3D9/D3D9Texture2D.cpp

@@ -90,7 +90,7 @@ bool Texture2D::BeginLoad(Deserializer& source)
 bool Texture2D::EndLoad()
 {
     // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
+    if (!graphics_ || graphics_->IsDeviceLost())
         return true;
     
     // If over the texture budget, see if materials can be freed to allow textures to be freed
@@ -119,7 +119,7 @@ void Texture2D::OnDeviceReset()
         ResourceCache* cache = GetSubsystem<ResourceCache>();
         if (cache->Exists(GetName()))
             dataLost_ = !cache->ReloadResource(this);
-
+        
         if (!object_)
         {
             Create();

+ 1 - 1
Source/Engine/Graphics/Direct3D9/D3D9Texture3D.cpp

@@ -131,7 +131,7 @@ bool Texture3D::BeginLoad(Deserializer& source)
 bool Texture3D::EndLoad()
 {
     // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
+    if (!graphics_ || graphics_->IsDeviceLost())
         return true;
     
     // If over the texture budget, see if materials can be freed to allow textures to be freed

+ 81 - 81
Source/Engine/Graphics/Direct3D9/D3D9TextureCube.cpp

@@ -65,6 +65,87 @@ void TextureCube::RegisterObject(Context* context)
     context->RegisterFactory<TextureCube>();
 }
 
+bool TextureCube::BeginLoad(Deserializer& source)
+{
+    ResourceCache* 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())
+    {
+        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 faceElem = textureElem.GetChild("face");
+    while (faceElem)
+    {
+        String name = faceElem.GetAttribute("name");
+        
+        String faceTexPath, faceTexName, faceTexExt;
+        SplitPath(name, faceTexPath, faceTexName, faceTexExt);
+        // If path is empty, add the XML file path
+        if (faceTexPath.Empty())
+            name = texPath + name;
+        
+        loadImages_.Push(cache->GetTempResource<Image>(name));
+        cache->StoreResourceDependency(this, name);
+        
+        faceElem = faceElem.GetNext("face");
+    }
+
+    // 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 TextureCube::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_);
+    
+    for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
+        SetData((CubeMapFace)i, loadImages_[i]);
+    
+    loadImages_.Clear();
+    loadParameters_.Reset();
+    
+    return true;
+}
+
 void TextureCube::OnDeviceLost()
 {
     if (pool_ == D3DPOOL_DEFAULT)
@@ -282,87 +363,6 @@ bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int wi
     return true;
 }
 
-bool TextureCube::BeginLoad(Deserializer& source)
-{
-    ResourceCache* 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())
-    {
-        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 faceElem = textureElem.GetChild("face");
-    while (faceElem)
-    {
-        String name = faceElem.GetAttribute("name");
-        
-        String faceTexPath, faceTexName, faceTexExt;
-        SplitPath(name, faceTexPath, faceTexName, faceTexExt);
-        // If path is empty, add the XML file path
-        if (faceTexPath.Empty())
-            name = texPath + name;
-        
-        loadImages_.Push(cache->GetTempResource<Image>(name));
-        cache->StoreResourceDependency(this, name);
-        
-        faceElem = faceElem.GetNext("face");
-    }
-
-    // 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 TextureCube::EndLoad()
-{
-    // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
-        return true;
-    
-    // If over the texture budget, see if materials can be freed to allow textures to be freed
-    CheckTextureBudget(GetTypeStatic());
-
-    SetParameters(loadParameters_);
-    
-    for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
-        SetData((CubeMapFace)i, loadImages_[i]);
-    
-    loadImages_.Clear();
-    loadParameters_.Reset();
-    
-    return true;
-}
-
 bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
 {
     SharedPtr<Image> image(new Image(context_));

+ 1 - 1
Source/Engine/Graphics/OpenGL/OGLTexture2D.cpp

@@ -92,7 +92,7 @@ bool Texture2D::BeginLoad(Deserializer& source)
 bool Texture2D::EndLoad()
 {
     // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
+     if (!graphics_ || graphics_->IsDeviceLost())
         return true;
     
     // If over the texture budget, see if materials can be freed to allow textures to be freed

+ 1 - 1
Source/Engine/Graphics/OpenGL/OGLTexture3D.cpp

@@ -137,7 +137,7 @@ bool Texture3D::BeginLoad(Deserializer& source)
 bool Texture3D::EndLoad()
 {
     // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
+    if (!graphics_ || graphics_->IsDeviceLost())
         return true;
     
     // If over the texture budget, see if materials can be freed to allow textures to be freed

+ 82 - 82
Source/Engine/Graphics/OpenGL/OGLTextureCube.cpp

@@ -66,10 +66,91 @@ void TextureCube::RegisterObject(Context* context)
     context->RegisterFactory<TextureCube>();
 }
 
+bool TextureCube::BeginLoad(Deserializer& source)
+{
+    ResourceCache* 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())
+    {
+        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 faceElem = textureElem.GetChild("face");
+    while (faceElem)
+    {
+        String name = faceElem.GetAttribute("name");
+        
+        String faceTexPath, faceTexName, faceTexExt;
+        SplitPath(name, faceTexPath, faceTexName, faceTexExt);
+        // If path is empty, add the XML file path
+        if (faceTexPath.Empty())
+            name = texPath + name;
+        
+        loadImages_.Push(cache->GetTempResource<Image>(name));
+        cache->StoreResourceDependency(this, name);
+        
+        faceElem = faceElem.GetNext("face");
+    }
+
+    // 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 TextureCube::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_);
+    
+    for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
+        SetData((CubeMapFace)i, loadImages_[i]);
+    
+    loadImages_.Clear();
+    loadParameters_.Reset();
+    
+    return true;
+}
+
 void TextureCube::OnDeviceLost()
 {
     GPUObject::OnDeviceLost();
-    
+
     for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
     {
         if (renderSurfaces_[i])
@@ -242,87 +323,6 @@ bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int wi
     return true;
 }
 
-bool TextureCube::BeginLoad(Deserializer& source)
-{
-    ResourceCache* 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())
-    {
-        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 faceElem = textureElem.GetChild("face");
-    while (faceElem)
-    {
-        String name = faceElem.GetAttribute("name");
-        
-        String faceTexPath, faceTexName, faceTexExt;
-        SplitPath(name, faceTexPath, faceTexName, faceTexExt);
-        // If path is empty, add the XML file path
-        if (faceTexPath.Empty())
-            name = texPath + name;
-        
-        loadImages_.Push(cache->GetTempResource<Image>(name));
-        cache->StoreResourceDependency(this, name);
-        
-        faceElem = faceElem.GetNext("face");
-    }
-
-    // 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 TextureCube::EndLoad()
-{
-    // In headless mode, do not actually load the texture, just return success
-    if (!graphics_)
-        return true;
-    
-    // If over the texture budget, see if materials can be freed to allow textures to be freed
-    CheckTextureBudget(GetTypeStatic());
-
-    SetParameters(loadParameters_);
-    
-    for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
-        SetData((CubeMapFace)i, loadImages_[i]);
-    
-    loadImages_.Clear();
-    loadParameters_.Reset();
-    
-    return true;
-}
-
 bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
 {
     SharedPtr<Image> image(new Image(context_));