~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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
 * 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 GLTEXTURERESOURCEMANAGER_H
#define GLTEXTURERESOURCEMANAGER_H

#include "GLResourceManager.h"
#include "IOpenGLBaseTexture.h"

#include <gdk-pixbuf/gdk-pixbuf.h>

namespace nux
{

  class NTextureData;
  class BaseTexture;
  class CachedBaseTexture;

  /*!
   * Create and load a 2D texture filled with RGBA pixels loaded from the image
   * file pointed by filename. The loading uses the GdkPixbuf library which
   * implies that the supported formats depend on the GdkPixbuf loaders
   * installed on the client system.
   *
   * @max_size Specifies the maximal required size for the image to be loaded,
   * if the width or height exceeds that value, the image is scaled down
   * respecting the aspect ratio. A value of -1 means that no maximal value is
   * required.
   * @premultiply Specifies if the R, G and B color channels must be
   * premultiplied by the alpha channel before being uploaded to the texture.
   * Note that if there's no alpha channel, the argument is ignored.
   * @return The resulting texture.
   */
  BaseTexture* CreateTexture2DFromFile(const char* filename, int max_size,
                                        bool premultiply);

  /*!
   * Create and load a 2D texture filled with RGBA pixels loaded from the
   * GdkPixbuf pointed by pixbuf.
   *
   * @premultiply Specifies if the R, G and B color channels must be
   * premultiplied by the alpha channel before being uploaded to the texture.
   * Note that if there's no alpha channel, the argument is ignored.
   * @return The resulting texture.
   */
  BaseTexture* CreateTexture2DFromPixbuf(GdkPixbuf* pixbuf, bool premultiply);

  // FIXME(loicm) Should be deprecated.
  BaseTexture* CreateTextureFromPixbuf(GdkPixbuf* pixbuf);

  BaseTexture* CreateTextureFromFile(const char* TextureFilename);
  BaseTexture* CreateTextureFromBitmapData(const NBitmapData* BitmapData);

  BaseTexture* LoadTextureFromFile(const std::string& filename);

  //! Abstract base class for textures.
  class BaseTexture: public ResourceData
  {
    NUX_DECLARE_OBJECT_TYPE(BaseTexture, ResourceData);

    BaseTexture(NUX_FILE_LINE_PROTO);
    virtual ~BaseTexture();

    /*!
      Update the texture with the provided Bitmap data. In doing so, if the texture as been cached in the resource manager, then
      the the DeviceTexture inside the CachedTexture will no longer be returned by GetDeviceTexture(). Instead a new device texture will
      be returned.

      @BitmapData Pointer to the bitmap data.
      @UpdateAndCacheResource If true, then the texture is cached immediately. If false, the texture will be cached the first time
      GetDeviceTexture() or GetCachedTexture() is called.
      @return True if there was no error during the update.
    */
    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true) = 0;

    /*!
      Update the texture with the provided filename. In doing so, if the texture as been cached in the resource manager, then
      the the DeviceTexture inside the CachedTexture will no longer be returned by GetDeviceTexture(). Instead a new device texture will
      be returned.

      @BitmapData Pointer to the bitmap data.
      @UpdateAndCacheResource If true, then the texture is cached immediately. If false, the texture will be cached the first time
      GetDeviceTexture() or GetCachedTexture() is called.
      @return True if there was no error during the update.
    */
    virtual bool Update(const char* filename, bool UpdateAndCacheResource = true) = 0;

    virtual void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0) = 0;

    /*!
      @return The texture width.
    */
    virtual int GetWidth() const = 0;

    /*!
      @return The texture height.
    */
    virtual int GetHeight() const = 0;

    /*!
      @return The texture depth. Return 1 for Texture2D, TextureCube, TextureRenctangle. TextureVolume and TextureFrameAnimation have a depth equal or greater than 1.
      For TextureFrameAnimation the depth represents the number of frames.
    */
    virtual int GetDepth() const
    {
      return 1;
    }

    /*!
      @return True if the width and height of the texture are powers of two.
    */
    virtual bool IsPowerOfTwo() const = 0;

    /*!
      @return The texture data format.
    */
    virtual BitmapFormat GetFormat() const = 0;

    /*!
      @return The number of mip maps in the texture.
    */
    virtual int GetNumMipLevel() const = 0;

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const = 0;

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const = 0;

    /*!
        Cache the texture if it hasn't been been already and return the device texture.
        \sa IOpenGLBaseTexture;

        @return The device texture.
    */
    ObjectPtr<IOpenGLBaseTexture> GetDeviceTexture();

    /*!
        Cache the texture if it hasn't been been already and return the cached texture.
        \sa CachedBaseTexture;

        @return The cached texture.
    */
    ObjectPtr<CachedBaseTexture> GetCachedTexture();
  };

  //! General Texture
  /*!
      The class of texture that cover power-of-two and non-power-of-two dimensions.
  */
  class Texture2D: public BaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(Texture2D, BaseTexture);

  public:
    Texture2D(NUX_FILE_LINE_PROTO);
    Texture2D(const Texture2D& texture, NUX_FILE_LINE_PROTO);
    Texture2D(const NTextureData& BaseTexture, NUX_FILE_LINE_PROTO);
    Texture2D& operator = (const Texture2D& texture);
    ~Texture2D();

    /*!
        Update the hardware resources associated to this with the provided texture data.
        @param BitmapData The texture data to update into the hardware resource.
        @param UpdateAndCacheResource if True, then the texture data is loaded into this object, and the caching into
        hardware data is done right away. If false, the caching is done latter by calling
        GetGraphicsDisplay()->GetGraphicsEngine()->CacheResource(this);
        @return True is there was not error.
    */
    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);

    /*!
        Update the hardware resources associated to this object with the data associated to the file name.
        @param Filename File name of texture data to update into the hardware resource.
        @param UpdateAndCacheResource if True, then the texture data is loaded into this object, and the caching into
        hardware data is done right away. If false, the caching is done latter by calling
        GetGraphicsDisplay()->GetGraphicsEngine()->CacheResource(this);
        @return True is there was not error.
    */
    virtual bool Update(const char* Filename, bool UpdateAndCacheResource = true);

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const
    {
      return _image.IsNull();
    }

    void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);

    /*!
      @return The number of mip maps in the texture.
    */
    int GetNumMipLevel() const
    {
      return _image.GetNumMipmap();
    }
    /*!
      @return The texture width.
    */
    int GetWidth() const
    {
      return _image.GetWidth();
    }

    /*!
      @return The texture height.
    */
    int GetHeight() const
    {
      return _image.GetHeight();
    }

    /*!
      @return The texture data format.
    */
    BitmapFormat GetFormat() const
    {
      return _image.GetFormat();
    }

    /*!
      @return True if the width and height of the texture are powers of two.
    */
    bool IsPowerOfTwo() const
    {
      return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
    }

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const;

  private:
    NTextureData _image; //!< Storage for the texture data
  };

  class TextureRectangle: public BaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(TextureRectangle, BaseTexture);

  public:
    TextureRectangle(NUX_FILE_LINE_PROTO);
    TextureRectangle(const TextureRectangle& texture);
    TextureRectangle(const NTextureData& Image);
    TextureRectangle& operator = (const TextureRectangle& texture);
    ~TextureRectangle();

    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
    virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const
    {
      return _image.IsNull();
    }

    void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);

    /*!
      @return The number of mip maps in the texture.
    */
    int GetNumMipLevel() const
    {
      return _image.GetNumMipmap();
    }

    /*!
      @return The texture width.
    */
    int GetWidth() const
    {
      return _image.GetWidth();
    }

    /*!
      @return The texture height.
    */
    int GetHeight() const
    {
      return _image.GetHeight();
    }

    /*!
      @return The texture data format.
    */
    BitmapFormat GetFormat() const
    {
      return _image.GetFormat();
    }

    /*!
      @return True if the width and height of the texture are powers of two.
    */
    bool IsPowerOfTwo() const
    {
      return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
    }

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const;

  private:
    NTextureData _image;
  };

  class TextureCube: public BaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(TextureCube, BaseTexture);

  public:
    TextureCube(NUX_FILE_LINE_PROTO);
    //Texture2D(const NTextureData& Image);
    TextureCube(const TextureCube& texture);
    TextureCube& operator = (const TextureCube& texture);
    ~TextureCube();

    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
    virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const
    {
      return _image.IsNull();
    }
    void GetData(void* Buffer, int MipIndex, int StrideY, int face = 0);

    /*!
      @return The number of mip maps in the texture.
    */
    int GetNumMipLevel() const
    {
      return _image.GetNumMipmap();
    }

    /*!
      @return The texture width.
    */
    int GetWidth() const
    {
      return _image.GetWidth();
    }

    /*!
      @return The texture height.
    */
    int GetHeight() const
    {
      return _image.GetHeight();
    }

    /*!
      @return The texture data format.
    */
    BitmapFormat GetFormat() const
    {
      return _image.GetFormat();
    }

    /*!
      @return True if the width and height of the texture are powers of two.
    */
    bool IsPowerOfTwo() const
    {
      return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
    }

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const;

  private:
    NCubemapData _image;
  };

  class TextureVolume: public BaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(TextureVolume, BaseTexture);

  public:
    TextureVolume(NUX_FILE_LINE_PROTO);
    //Texture2D(const NTextureData& Image);
    TextureVolume(const TextureVolume& texture);
    TextureVolume& operator = (const TextureVolume& texture);
    ~TextureVolume();

    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
    virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const
    {
      return _image.IsNull();
    }
    void GetData(void* Buffer, int MipIndex, int StrideY, int slice = 0);
    
    /*!
      @return The number of mip maps in the texture.
    */
    int GetNumMipLevel() const
    {
      return _image.GetNumMipmap();
    }

    /*!
      @return The texture width.
    */
    int GetWidth() const
    {
      return _image.GetWidth();
    }

    /*!
      @return The texture height.
    */
    int GetHeight() const
    {
      return _image.GetHeight();
    }

    /*!
      @return The texture depth. 
      For TextureFrameAnimation the depth represents the number of frames.
    */
    int GetDepth() const
    {
      return _image.GetDepth();
    }

    /*!
      @return The texture data format.
    */
    BitmapFormat GetFormat() const
    {
      return _image.GetFormat();
    }

    /*!
      @return True if the width and height of the texture are powers of two.
    */
    bool IsPowerOfTwo() const
    {
      return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
    }

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const;

  private:
    NVolumeData _image;
  };

  class TextureFrameAnimation: public BaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(TextureFrameAnimation, BaseTexture);

  public:
    TextureFrameAnimation(NUX_FILE_LINE_PROTO);
    TextureFrameAnimation(const TextureFrameAnimation& texture);
    TextureFrameAnimation& operator = (const TextureFrameAnimation& texture);
    ~TextureFrameAnimation();

    virtual bool Update(const NBitmapData* BitmapData, bool UpdateAndCacheResource = true);
    virtual bool Update(const char* filename, bool UpdateAndCacheResource = true);

    /*!
      @return True if the texture storage contains valid bitmap data.
    */
    virtual bool IsNull() const
    {
      return _image.IsNull();
    }
    void GetData(void* Buffer, int MipIndex, int StrideY, int slice = 0);
    int GetFrameTime(int Frame);

    /*!
      @return The number of mip maps in the texture.
    */
    int GetNumMipLevel() const
    {
      return _image.GetNumMipmap();
    }

    /*!
      @return The texture width.
    */
    int GetWidth() const
    {
      return _image.GetWidth();
    }

    /*!
      @return The texture height.
    */
    int GetHeight() const
    {
      return _image.GetHeight();
    }

    /*!
      @return The number of animation frames in the texture.
    */
    int GetDepth() const
    {
      return _image.GetDepth();
    }

    /*!
      @return The texture data format.
    */
    BitmapFormat GetFormat() const
    {
      return _image.GetFormat();
    }

    /*!
      @return true if the width and heigth of the texture are powers of two.
    */
    bool IsPowerOfTwo() const
    {
      return IsPowerOf2(_image.GetWidth()) && IsPowerOf2(_image.GetHeight());
    }

    /*!
      Clone the texture.
      @return A cloned version of this texture.
    */
    virtual BaseTexture* Clone() const;

  private:
    NAnimatedTextureData _image;
  };

  class CachedBaseTexture: public CachedResourceData
  {
    NUX_DECLARE_OBJECT_TYPE(CachedBaseTexture, CachedResourceData);
  public:
    ObjectPtr < IOpenGLBaseTexture > m_Texture;

    CachedBaseTexture(NResourceSet* ResourceManager);
    ~CachedBaseTexture();

    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel) = 0;

    virtual bool UpdateResource(ResourceData* Resource);

    bool RecreateTexture(BaseTexture* Source);

    virtual void UpdateTexture(BaseTexture* Source) = 0;

    unsigned int SourceWidth;
    unsigned int SourceHeight;
    unsigned int SourceDepth;
    BitmapFormat SourceFormat;
  };

  class CachedTexture2D: public CachedBaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(CachedTexture2D, CachedBaseTexture);
  public:
    CachedTexture2D(NResourceSet* ResourceManager, Texture2D* SourceTexture);
    ~CachedTexture2D();

    virtual void UpdateTexture(BaseTexture* Source);
    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
  };

  class CachedTextureRectangle: public CachedBaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(CachedTextureRectangle, CachedBaseTexture);
  public:
    CachedTextureRectangle(NResourceSet* ResourceManager, TextureRectangle* SourceTexture);
    ~CachedTextureRectangle();

    virtual void UpdateTexture(BaseTexture* Source);
    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
  };

  class CachedTextureCube: public CachedBaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(CachedTextureCube, CachedBaseTexture);
  public:
    CachedTextureCube(NResourceSet* ResourceManager, TextureCube* SourceTexture);
    ~CachedTextureCube();

    virtual void UpdateTexture(BaseTexture* Source);
    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
  };

  class CachedTextureVolume: public CachedBaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(CachedTextureVolume, CachedBaseTexture);
  public:
    CachedTextureVolume(NResourceSet* ResourceManager, TextureVolume* SourceTexture);
    ~CachedTextureVolume();

    virtual void UpdateTexture(BaseTexture* Source);
    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
  };

  class CachedTextureFrameAnimation: public CachedBaseTexture
  {
    NUX_DECLARE_OBJECT_TYPE(CachedTextureFrameAnimation, CachedBaseTexture);
  public:
    CachedTextureFrameAnimation(NResourceSet* ResourceManager, TextureFrameAnimation* SourceTexture);
    ~CachedTextureFrameAnimation();

    virtual void UpdateTexture(BaseTexture* Source);
    virtual void LoadMipLevel(BaseTexture* SourceTexture, int MipLevel);
  };

}

#endif // GLTEXTURERESOURCEMANAGER_H