Back to index

nux  3.0.0
GLResourceManager.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "IOpenGLResource.h"
00025 #include "GLResourceManager.h"
00026 #include "GraphicsEngine.h"
00027 #include "GLTextureResourceManager.h"
00028 #include "GLVertexResourceManager.h"
00029 
00030 namespace nux
00031 {
00032 
00033   extern PixelFormatInfo GPixelFormats[];
00034 
00035   NUX_IMPLEMENT_OBJECT_TYPE(ResourceData);
00036   NUX_IMPLEMENT_OBJECT_TYPE(CachedResourceData);
00037 
00038   ResourceData::ResourceData(NUX_FILE_LINE_DECL)
00039     : Object(true, NUX_FILE_LINE_PARAM)
00040     , m_ResourceIndex(NUX_INVALID_INDEX)
00041   {
00042 #define GET_UNIQUE_RESOURCE_INDEX NUX_GLOBAL_OBJECT_INSTANCE(UniqueIndex)
00043     m_ResourceIndex = GET_UNIQUE_RESOURCE_INDEX.GetUniqueIndex();
00044 #undef GET_UNIQUE_RESOURCE_INDEX
00045   }
00046 
00047   ResourceData::~ResourceData()
00048   {
00049     // If the windows thread is deleted before any texture type resource, the
00050     // graphics display will not exist.
00051     GraphicsDisplay* display = GetGraphicsDisplay();
00052     if (display)
00053       display->GetGraphicsEngine()->FlushCachedResourceData(this);
00054   }
00055 
00056   int ResourceData::GetResourceIndex() const
00057   {
00058     return m_ResourceIndex;
00059   }
00060 
00061   NResourceSet::NResourceSet()
00062     : FirstResource( NULL )
00063   {
00064     if ( 1 )
00065     {
00066 
00067     }
00068   }
00069 
00070   CachedResourceData::CachedResourceData(NResourceSet *ResourceManager)
00071     :   Set(ResourceManager)
00072     ,   _cached(0)
00073     ,   NumRefs(0)
00074     ,   ResourceType(NULL)
00075     ,   Size(0)
00076     ,   UpdateHint(RUH_Static)
00077   {
00078     PrevResource = NULL;
00079     NextResource = Set->FirstResource;
00080     Set->FirstResource = this;
00081 
00082     if (NextResource)
00083     {
00084       NextResource->PrevResource = this;
00085     }
00086   }
00087 
00088   CachedResourceData::~CachedResourceData()
00089   {
00090     if (PrevResource)
00091     {
00092       PrevResource->NextResource = NextResource;
00093     }
00094     else
00095     {
00096       Set->FirstResource = NextResource;
00097     }
00098 
00099     if (NextResource)
00100     {
00101       NextResource->PrevResource = PrevResource;
00102     }
00103 
00104     if (_cached)
00105     {
00106       Set->FlushResource(this);
00107       _cached = false;
00108     }
00109   }
00110 
00111   void NResourceCache::InitializeResourceFactories()
00112   {
00113     // Define the factory pair ResourceData - CachedResourceData
00114 #define NUX_DEFINE_RESOURCE_FACTORY_PAIR(SourceTypeName, ResourceTypeName) \
00115     static TGLResourceFactory<SourceTypeName, ResourceTypeName> Factory##SourceTypeName(&SourceTypeName::StaticObjectType); \
00116     GetResourceFactories().push_back(&Factory##SourceTypeName);
00117 
00118     NUX_DEFINE_RESOURCE_FACTORY_PAIR(Texture2D, CachedTexture2D);
00119     NUX_DEFINE_RESOURCE_FACTORY_PAIR(TextureRectangle, CachedTextureRectangle);
00120     NUX_DEFINE_RESOURCE_FACTORY_PAIR(TextureCube, CachedTextureCube);
00121     NUX_DEFINE_RESOURCE_FACTORY_PAIR(TextureVolume, CachedTextureVolume);
00122     NUX_DEFINE_RESOURCE_FACTORY_PAIR(TextureFrameAnimation, CachedTextureFrameAnimation);
00123     NUX_DEFINE_RESOURCE_FACTORY_PAIR(VertexBuffer, CachedVertexBuffer);
00124     NUX_DEFINE_RESOURCE_FACTORY_PAIR(IndexBuffer, CachedIndexBuffer);
00125     NUX_DEFINE_RESOURCE_FACTORY_PAIR(VertexDeclaration, CachedVertexDeclaration);
00126     NUX_DEFINE_RESOURCE_FACTORY_PAIR(MeshBuffer, CachedMeshBuffer);
00127 
00128 #undef NUX_DEFINE_RESOURCE_FACTORY_PAIR
00129 
00130     // Define the factory updater for and ResourceData
00131 #define NUX_DEFINE_RESOURCE_UPDATER(SourceTypeName) \
00132     static NResourceUpdater Updater##SourceTypeName(&SourceTypeName::StaticObjectType); \
00133     GetResourceUpdaters().push_back(&Updater##SourceTypeName);
00134 
00135     NUX_DEFINE_RESOURCE_UPDATER(Texture2D);
00136     NUX_DEFINE_RESOURCE_UPDATER(TextureRectangle);
00137     NUX_DEFINE_RESOURCE_UPDATER(TextureCube);
00138     NUX_DEFINE_RESOURCE_UPDATER(TextureVolume);
00139     NUX_DEFINE_RESOURCE_UPDATER(TextureFrameAnimation);
00140     NUX_DEFINE_RESOURCE_UPDATER(VertexBuffer);
00141     NUX_DEFINE_RESOURCE_UPDATER(IndexBuffer);
00142     NUX_DEFINE_RESOURCE_UPDATER(VertexDeclaration);
00143     NUX_DEFINE_RESOURCE_UPDATER(MeshBuffer);
00144 
00145 #undef NUX_DEFINE_RESOURCE_UPDATER
00146   }
00147 
00148   ObjectPtr<CachedResourceData> NResourceCache::GetCachedResource(ResourceData *Source)
00149   {
00150     NUX_RETURN_VALUE_IF_NULL(Source, ObjectPtr<CachedResourceData>(0));
00151     
00152     // check to see if it already exists
00153     ObjectPtr<CachedResourceData> CachedResource = TResourceCache<int, CachedResourceData>::FindCachedResourceById(Source->GetResourceIndex());
00154 
00155     if (CachedResource.IsNull())
00156     {
00157       // iterate over the list of factory types
00158       for (unsigned int i = 0; i < GetResourceFactories().size(); ++i)
00159       {
00160         NResourceFactory *ResourceFactory = GetResourceFactories() [i];
00161 
00162         // check if the factory is valid for the source resource type
00163         if (ResourceFactory->BuildsThisResource(Source))
00164         {
00165           // cache the device resource
00166           CachedResourceData* ptr = ResourceFactory->BuildResource(this, Source);
00167           CachedResource = ObjectPtr<CachedResourceData> (ptr);
00168           ptr->UnReference();
00169           break;
00170         }
00171       }
00172 
00173       // make sure the result is valid
00174       if (CachedResource.IsNull())
00175       {
00176         nuxError("Cannot cache resource type %s", Source->Type().name );
00177       }
00178       else
00179       {
00180         // Get resource type
00181         CachedResource->ResourceType      = & (Source->Type());
00182         // add it to the pool of cached resources
00183         AddCachedResource(Source->GetResourceIndex(), CachedResource);
00184       }
00185     }
00186 
00187     return CachedResource;
00188   }
00189 
00190   bool NResourceCache::IsCachedResource(ResourceData *Source)
00191   {
00192     ObjectPtr<CachedResourceData> CachedResource = TResourceCache< int, CachedResourceData >::FindCachedResourceById(Source->GetResourceIndex());
00193     return(CachedResource.IsValid());
00194   }
00195 
00196 }