~hikiko/nux/arb-srgba-shader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#ifndef GLRESOURCEMANAGER_H
#define GLRESOURCEMANAGER_H

namespace nux
{

// class BaseTexture;
// class Texture2D;
// class TextureRectangle;
// class TextureCube;
// class TextureVolume;
// class TextureFrameAnimation;
//
// class NVertexBuffer;
// class NIndexBuffer;
//
// class CachedTexture2D;
// class CachedTextureRectangle;
// class CachedTextureCube;
// class CachedTextureVolume;
// class CachedTextureFrameAnimation;

//     ResourceData           CachedResourceData
//         |                   |
//     BaseTexture            CachedBaseTexture
//         |                   |
//     Texture2D              CachedTexture2D
//
//
//     NResourceFactory
//         |
//     NGLResourceFactory
//         |
//     TGLResourceFactory<class Texture2D, class CachedTexture2D>
//
//
//     NResourceSet
//         |
//     TResourceCache<class IdType(int), class ResourceType(CachedResourceData)>
//         |
//     NResourceCache: TResourceCache<int, CachedResourceData>
//
//

  //! Base class for all types of resources.
  class ResourceData: public Object
  {
    NUX_DECLARE_OBJECT_TYPE(ResourceData, Object);
  public:
    ResourceData(NUX_FILE_LINE_PROTO);
    virtual ~ResourceData();
    int GetResourceIndex() const;

  private:
    int m_ResourceIndex;
  };

  class CachedResourceData;

  class NResourceSet
  {
  public:
    NResourceSet();
    virtual ~NResourceSet() {};

  protected:
    CachedResourceData *FirstResource;

    // Flush
    virtual void Flush() {}

    // FreeResource - Called when a potentially cached resource has been freed.
    virtual void FreeResource(ResourceData *Resource) {}

    // FlushResource - Removes a resource from the set.
    virtual void FlushResource(CachedResourceData *Resource) {}

    friend class CachedResourceData;
  };

  enum EResourceUpdateHint
  {
    RUH_Static,				// The resource is updated once, at creation time.
    RUH_CacheableDynamic,	// The resource is updated occasionally, but not every frame.
    RUH_Dynamic				// The resource changes every frame.
  };

  class CachedResourceData: public Object
  {
    NUX_DECLARE_OBJECT_TYPE(CachedResourceData, Object);

  public:
    CachedResourceData(NResourceSet *InSet);
    virtual ~CachedResourceData();

    //! Returns the size in bytes of the resource.
    /*! Returns the size in bytes of the resource.
        @return Size of the resource in bytes.
    */
    virtual unsigned int GetSize() const
    {
      return Size;
    }

    //! Returns the size of the max LOD of the resource.
    /*!
        Returns the size of the max LOD of the resource. For a texture, this is the size of mipmap 0.
        Texture resource overwrite this function.
        @return Size of the resource in bytes.
    */
    virtual unsigned int GetMaxLodSize() const
    {
      return Size;
    }

    /*!
        Updates the resource.
    */
    virtual bool UpdateResource(ResourceData *Resource) = 0;

  protected:
    NResourceSet       *Set;
    bool                _cached;
    unsigned int        NumRefs;
    NObjectType        *ResourceType;

    unsigned int        Size;
    EResourceUpdateHint UpdateHint;

    CachedResourceData *PrevResource;
    CachedResourceData *NextResource;

    template<class IdType, class ResourceType>
    friend class TResourceCache;
    friend class NResourceCache;
  };


//! Device independent resource factory.
  class NResourceFactory
  {
  public:
    NResourceFactory(NObjectType *Type)
      :   m_ResourceType(Type)
    {}

    // Returns the resource type for this factory
    const NObjectType &Type() const
    {
      return *m_ResourceType;
    }

    /*!
      Returns true if the given ResourceData is created by this factory.
      @param  Resource - the resource in question.
    */
    bool BuildsThisResource(ResourceData *Resource)
    {
      return Resource->Type().IsObjectType(Type());
    }

    virtual CachedResourceData *BuildResource(NResourceSet *ResourceManager, ResourceData *Resource)
    {
      return NULL;
    }

  private:
    //! Type associated with this factory class.
    NObjectType *m_ResourceType;
  };

  template <typename T, typename U>
  class TGLResourceFactory : public NResourceFactory
  {
  public:
    /*!
        Constructor.
        @param  type - resource class type to associate w/ this factory.
    */
    TGLResourceFactory(NObjectType *Type)
      :   NResourceFactory(Type)
    {}

    virtual	~TGLResourceFactory(void)
    {}

    //! Create a new resource.
    /*! Create a new resource for the given ResourceData.
        @param  ResourceManager The resource manager.
        @param  Resource        Resource to build and cache.
        @return The built resource.
    */
    virtual CachedResourceData *BuildResource(NResourceSet *ResourceManager, ResourceData *Resource)
    {
      return new U(ResourceManager, (T *)Resource);
    }
  };


//! Device independent resource updater.
  class NResourceUpdater
  {
  public:
    NResourceUpdater(NObjectType *Type)
      :   m_ResourceType(Type)
    {}

    //! Returns the resource type for this factory.
    const NObjectType &Type() const
    {
      return *m_ResourceType;
    }

    /*!
        Returns true if the given ResourceData can be updated by this factory.
        @param  Resource    The resource in question.
    */
    bool UpdatesThisResource(ResourceData *Resource)
    {
      return Resource->Type().IsObjectType(Type());
    }

    virtual bool UpdateResource(ObjectPtr< CachedResourceData > DeviceResource, ResourceData *Resource) const
    {
      return DeviceResource->UpdateResource(Resource);
    }

  private:
    //! Type associated with this factory class.
    NObjectType	*m_ResourceType;
  };

  template<typename IdType, typename ResourceType>
  class TResourceCache: public NResourceSet
  {
  public:
    std::map< IdType, ObjectPtr< ResourceType > > ResourceMap;

    // Resource factory instances for each ResourceData/CachedResourceData pair.
    std::vector<NResourceFactory *> ResourceFactories;

    // Resource updater instances for each ResourceData type.
    std::vector<NResourceUpdater *> ResourceUpdaters;


    TResourceCache()
      :   NResourceSet()
    {}

    void Flush()
    {
      // See the language FAQ 35.18 at http://www.parashift.com/c++-faq-lite/templates.html  for why the "typename".
      typename std::map< IdType, ObjectPtr< ResourceType > >::iterator It;

      for (It = ResourceMap.begin(); It != ResourceMap.end(); It++)
      {
//         ObjectPtr< ResourceType >	CachedResource = (*It).second;
//         CachedResource->_cached = 0;
//         CachedResource.Release();
        (*It).second->_cached = 0;
        (*It).second.Release();
      }

      // Erases all elements from the map.
      ResourceMap.clear();
    }

    void AddCachedResource(const IdType &Id, ObjectPtr< ResourceType > Resource)
    {
      typedef std::map< IdType, ObjectPtr< ResourceType > >  MapType;
      ResourceMap.insert(typename MapType::value_type(Id, Resource));
      Resource->_cached = 1;
    }

    ObjectPtr<ResourceType> FindCachedResourceById(const IdType &Id)
    {
      typedef std::map< IdType, ObjectPtr< ResourceType > >  MapType;
      typename MapType::iterator it = ResourceMap.find(Id);

      if (it != ResourceMap.end())
        return(*it).second;

      return ObjectPtr<ResourceType> (0);
    }

    /*!
        Remove a cached resource from the cache. The cached resource may still have references to it.
        The resource internal flag "_cached" is set to false.
    */
    void FlushResourceId(const IdType &Id)
    {
      ObjectPtr<ResourceType>	CachedResource(0);

      typedef std::map<IdType, ObjectPtr<ResourceType> >  MapType;
      typename MapType::iterator it = ResourceMap.find(Id);

      if (it != ResourceMap.end())
        CachedResource = (*it).second;

      if (CachedResource.IsValid())
      {
        ResourceMap.erase(it);
        CachedResource->_cached = false;
      }
    }

    virtual void FlushResource(CachedResourceData *Resource)
    {
      typedef std::map< IdType, ObjectPtr< ResourceType > >  MapType;
      typename MapType::iterator it;

      for (it = ResourceMap.begin(); it != ResourceMap.end(); it++)
      {
        ObjectPtr< ResourceType >	CachedResource = (*it).second;

        if (CachedResource == Resource)
        {
          ResourceMap.erase(it);
          CachedResource->_cached = 0; // Make sure that if the following line deletes the resource, it doesn't try to remove itself from the TDynamicMap we're iterating over.
          return;
        }
      }
    }

    // Register CachedResourceData with the corresponding ResourceData
    virtual void InitializeResourceFactories() = 0;

    std::vector<NResourceFactory *>&	GetResourceFactories(void)
    {
      return(ResourceFactories);
    }
    std::vector<NResourceUpdater *>&	GetResourceUpdaters(void)
    {
      return(ResourceUpdaters);
    }
  };

  class NResourceCache: public TResourceCache<int, CachedResourceData>
  {
  public:
    NResourceCache()
      :   TResourceCache<int, CachedResourceData>()
    {}

    ObjectPtr< CachedResourceData > GetCachedResource(ResourceData *Source);
    bool         IsCachedResource(ResourceData *Source);

    virtual void InitializeResourceFactories();
//     virtual void FreeResource(ResourceData *Resource)
//     {
//       FlushResourceId(Resource->GetResourceIndex());
//     }
  };


////////////////////////////////////////////////////////////////////////////////////////////////////

}

#endif // GLRESOURCEMANAGER_H