1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ResTexture.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_GFX_RESTEXTURE_H_
19 #define NW_GFX_RESTEXTURE_H_
20 
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 
24 #include <nw/ut/ut_Color.h>
25 #include <nw/ut/ut_ResUtil.h>
26 #include <nw/ut/ut_ResDictionary.h>
27 #include <nw/gfx/res/gfx_ResSceneObject.h>
28 #include <nw/gfx/res/gfx_ResRevision.h>
29 #include <nw/gfx/res/gfx_ResTypeInfo.h>
30 
31 namespace nw
32 {
33 namespace os
34 {
35 class IAllocator;
36 } // namesapce os
37 
38 namespace gfx {
39 namespace res {
40 
41 class ResGraphicsFile;
42 
43 //! @details :private
44 struct ResTextureData : public ResSceneObjectData
45 {
46 };
47 
48 //! @details :private
49 struct ResPixelBasedTextureData : public ResTextureData
50 {
51     nw::ut::ResS32 m_Height;
52     nw::ut::ResS32 m_Width;
53     nw::ut::ResU32 m_Format;
54     nw::ut::ResU32 m_FormatType;
55     nw::ut::ResS32 m_MipmapSize;
56     nw::ut::ResU32 m_TextureObject;
57     nw::ut::ResU32 m_LocationFlag;
58     nw::ut::ResU32 m_FormatHW;
59 };
60 
61 //! @details :private
62 struct ResPixelBasedImageData
63 {
64     enum
65     {
66         AREA_NO_MALLOC = 0,
67         AREA_FCRAM = NN_GX_MEM_FCRAM,
68         AREA_VRAMA = NN_GX_MEM_VRAMA,
69         AREA_VRAMB = NN_GX_MEM_VRAMB
70     };
71 
72     nw::ut::ResS32      m_Height;
73     nw::ut::ResS32      m_Width;
74     nw::ut::ResS32      m_ImageDataTableCount;
75     nw::ut::Offset      toImageDataTable;
76     nw::os::IAllocator* m_DynamicAllocator;
77     u32                 m_BitsPerPixel;
78     u32                 m_LocationAddress;
79     u32                 m_MemoryArea;
80 };
81 
82 //! @details :private
83 struct ResImageTextureData : public ResPixelBasedTextureData
84 {
85     nw::ut::Offset toImage;
86 };
87 
88 //! @details :private
89 struct ResCubeTextureData : public ResPixelBasedTextureData
90 {
91     enum { MAX_CUBE_FACE = 6 };
92 
93     union
94     {
95         struct
96         {
97             nw::ut::Offset toPositiveXImage;
98             nw::ut::Offset toNegativeXImage;
99             nw::ut::Offset toPositiveYImage;
100             nw::ut::Offset toNegativeYImage;
101             nw::ut::Offset toPositiveZImage;
102             nw::ut::Offset toNegativeZImage;
103         };
104 
105         nw::ut::Offset toImagesTables[ MAX_CUBE_FACE ];
106     };
107 };
108 
109 //! @details :private
110 struct ResShadowTextureData : public ResPixelBasedTextureData
111 {
112     nw::ut::Offset toImage;
113     nw::ut::ResBool m_IsPerspectiveShadow;
114     nw::ut::ResF32 m_ShadowZBias;
115     nw::ut::ResF32 m_ShadowZScale;
116 };
117 
118 //! @details :private
119 struct ResReferenceTextureData : public ResTextureData
120 {
121     nw::ut::BinString toPath;
122     nw::ut::Offset toTargetTexture;
123 };
124 
125 //--------------------------------------------------------------------------
126 //! @brief テクスチャを表すバイナリリソースの基底クラスです。
127 //---------------------------------------------------------------------------
128 class ResTexture : public ResSceneObject
129 {
130 public:
131     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResTexture) };
132     enum { SIGNATURE = NW_RES_SIGNATURE32('TXOB') };
133     enum { BINARY_REVISION = REVISION_RES_TEXTURE };
134 
NW_RES_CTOR_INHERIT(ResTexture,ResSceneObject)135     NW_RES_CTOR_INHERIT( ResTexture, ResSceneObject )
136 
137     //---------------------------------------------------------------------------
138     //! @brief        リビジョンを取得します。
139     //!
140     //! @return       リソースのリビジョン情報です。
141     //---------------------------------------------------------------------------
142     u32 GetRevision() const { return this->GetHeader().revision; }
143 
144     //---------------------------------------------------------------------------
145     //! @brief        参照ではない実体のテクスチャを取得します。
146     //!
147     //! @return       参照テクスチャの場合には参照先を返します。
148     //!               それ以外の場合には自身のインスタンスを返します。
149     //---------------------------------------------------------------------------
150     NW_INLINE ResTexture        Dereference();
151     NW_INLINE const ResTexture  Dereference() const;
152 
153     //---------------------------------------------------------------------------
154     //! @brief        テクスチャリソースを初期化します。
155     //!
156     //! @param[in]   allocator    アロケータを指定します。
157     //! @param[in]   graphicsFile 外部参照があった場合に解決をする為の
158     //!
159     //! @return       Setup の結果を返します。
160     //---------------------------------------------------------------------------
161     Result Setup(os::IAllocator* allocator, ResGraphicsFile graphicsFile);
162     Result Setup(os::IAllocator* allocator);
163 
164     //---------------------------------------------------------------------------
165     //! @brief        リソースの後始末をおこないます。
166     //---------------------------------------------------------------------------
167     void    Cleanup();
168 };
169 
170 typedef nw::ut::ResArrayPatricia<ResTexture>::type  ResTextureArray;
171 
172 //--------------------------------------------------------------------------
173 //! @brief 画素ベースのテクスチャを表すバイナリリソースクラスです。
174 //---------------------------------------------------------------------------
175 class ResPixelBasedTexture : public ResTexture
176 {
177 public:
178     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResPixelBasedTexture) };
179     enum { SIGNATURE = NW_RES_SIGNATURE32('TXPB') };
180 
181     NW_RES_CTOR_INHERIT( ResPixelBasedTexture, ResTexture )
182 
183     //---------------------------------------------------------------------------
184     //! @fn           s32 GetHeight() const
185     //! @brief        テクスチャの高さを取得します。
186     //---------------------------------------------------------------------------
187     //---------------------------------------------------------------------------
188     //! @fn           void SetHeight(s32 value)
189     //! @brief        テクスチャの高さを設定します。
190     //---------------------------------------------------------------------------
191     NW_RES_FIELD_PRIMITIVE_DECL( s32, Height )        // GetHeight(), SetHeight()
192 
193     //---------------------------------------------------------------------------
194     //! @fn           s32 GetWidth() const
195     //! @brief        テクスチャの幅を取得します。
196     //---------------------------------------------------------------------------
197     //---------------------------------------------------------------------------
198     //! @fn           void SetWidth(s32 value)
199     //! @brief        テクスチャの幅を設定します。
200     //---------------------------------------------------------------------------
201     NW_RES_FIELD_PRIMITIVE_DECL( s32, Width )         // GetWidth(), SetWidth()
202 
203     //---------------------------------------------------------------------------
204     //! @fn           u32 GetFormatType() const
205     //! @brief        テクスチャフォーマットタイプを取得します。
206     //---------------------------------------------------------------------------
207     //---------------------------------------------------------------------------
208     //! @fn           void SetFormatType(u32 value)
209     //! @brief        テクスチャフォーマットタイプを設定します。
210     //---------------------------------------------------------------------------
211     NW_RES_FIELD_PRIMITIVE_DECL( u32, FormatType )    // GetFormatType(), SetFormatType()
212 
213     //---------------------------------------------------------------------------
214     //! @fn           s32 GetMipmapSize() const
215     //! @brief        ミップマップの数を取得します。
216     //---------------------------------------------------------------------------
217     //---------------------------------------------------------------------------
218     //! @fn           void SetMipmapSize(s32 value)
219     //! @brief        ミップマップの数を設定します。
220     //---------------------------------------------------------------------------
221     NW_RES_FIELD_PRIMITIVE_DECL( s32, MipmapSize )    // GetMipmapSize(), SetMipmapSize()
222 
223     //---------------------------------------------------------------------------
224     //! @fn           u32 GetTextureObject() const
225     //! @brief        テクスチャオブジェクトを取得します。
226     //---------------------------------------------------------------------------
227     //---------------------------------------------------------------------------
228     //! @fn           void SetTextureObject(u32 value)
229     //! @brief        テクスチャオブジェクトを設定します。
230     //---------------------------------------------------------------------------
231     NW_RES_FIELD_PRIMITIVE_DECL( u32, TextureObject ) // GetTextureObject(), SetTextureObject()
232 
233     //---------------------------------------------------------------------------
234     //! @fn           u32 GetLocationFlag() const
235     //! @brief        メモリ配置を表すフラグを取得します。
236     //---------------------------------------------------------------------------
237     //---------------------------------------------------------------------------
238     //! @fn           void SetLocationFlag(u32 value)
239     //! @brief        メモリ配置を表すフラグを設定します。
240     //---------------------------------------------------------------------------
241     NW_RES_FIELD_PRIMITIVE_DECL( u32, LocationFlag )  // GetLocationFlag(), SetLocationFlag()
242 
243     //---------------------------------------------------------------------------
244     //! @fn           u32 GetFormatHW() const
245     //! @brief        テクスチャフォーマットを取得します。
246     //---------------------------------------------------------------------------
247     //---------------------------------------------------------------------------
248     //! @fn           void SetFormatHW(u32 value)
249     //! @brief        テクスチャフォーマットを設定します。
250     //---------------------------------------------------------------------------
251     NW_RES_FIELD_PRIMITIVE_DECL( u32, FormatHW )      // GetFormatHW(), SetFormatHW()
252 
253     enum FormatHW
254     {
255         FORMAT_HW_RGBA8    = 0,     //!< 1ピクセルが4バイト、RGBAが各8ビットのフォーマットです。
256         FORMAT_HW_RGB8     = 1,     //!< 1ピクセルが3バイト、RGBが各8ビットのフォーマットです。
257         FORMAT_HW_RGBA5551 = 2,     //!< 1ピクセルが2バイト、RGBが各5ビット、Aが1ビットのフォーマットです。
258         FORMAT_HW_RGB565   = 3,     //!< 1ピクセルが2バイト、RGBが各5,6,5ビットのフォーマットです。
259         FORMAT_HW_RGBA4    = 4,     //!< 1ピクセルが2バイト、RGBAが各4ビットのフォーマットです。
260         FORMAT_HW_LA8      = 5,     //!< 1ピクセルが2バイト、AlphaとLuminanceが各8ビットのフォーマットです。
261         FORMAT_HW_HILO8    = 6,     //!< 1ピクセルが2バイト、X,Yが各8ビットのフォーマットです。
262         FORMAT_HW_L8       = 7,     //!< 1ピクセルが1バイト、Luminanceが8ビットのフォーマットです。
263         FORMAT_HW_A8       = 8,     //!< 1ピクセルが1バイト、Alphaが8ビットのフォーマットです。
264         FORMAT_HW_LA4      = 9,     //!< 1ピクセルが1バイト、AlphaとLuminanceが各4ビットのフォーマットです。
265         FORMAT_HW_L4       = 10,    //!< 1ピクセルが4ビット、Luminanceが4ビットのフォーマットです。
266         FORMAT_HW_A4       = 11,    //!< 1ピクセルが4ビット、Alphaが4ビットのフォーマットです。
267         FORMAT_HW_ETC1     = 12,    //!< 1ピクセルが4ビット相当の圧縮フォーマットです。
268         FORMAT_HW_ETC1A4   = 13     //!< 1ピクセルが8ビット相当の圧縮フォーマットです。
269     };
270 };
271 
272 //--------------------------------------------------------------------------
273 //! @brief テクスチャのピクセルデータと解像度情報を表すバイナリリソースクラスです。
274 //---------------------------------------------------------------------------
275 class ResPixelBasedImage : public nw::ut::ResCommon< ResPixelBasedImageData >
276 {
277 public:
NW_RES_CTOR(ResPixelBasedImage)278     NW_RES_CTOR( ResPixelBasedImage )
279 
280     //---------------------------------------------------------------------------
281     //! @fn           s32 GetHeight() const
282     //! @brief        ピクセルデータの高さを取得します。
283     //---------------------------------------------------------------------------
284     //---------------------------------------------------------------------------
285     //! @fn           void SetHeight(s32 value)
286     //! @brief        ピクセルデータの高さを設定します。
287     //---------------------------------------------------------------------------
288     NW_RES_FIELD_PRIMITIVE_DECL( s32, Height )        // GetHeight(), SetHeight()
289 
290     //---------------------------------------------------------------------------
291     //! @fn           s32 GetWidth() const
292     //! @brief        ピクセルデータの幅を取得します。
293     //---------------------------------------------------------------------------
294     //---------------------------------------------------------------------------
295     //! @fn           void SetWidth(s32 value)
296     //! @brief        ピクセルデータの幅を設定します。
297     //---------------------------------------------------------------------------
298     NW_RES_FIELD_PRIMITIVE_DECL( s32, Width )         // GetWidth(), SetWidth()
299 
300     //---------------------------------------------------------------------------
301     //! @fn           s32 GetImageDataCount() const
302     //! @brief        画像データのバイナリデータの要素数を取得します。
303     //---------------------------------------------------------------------------
304     //---------------------------------------------------------------------------
305     //! @fn           u8 GetImageData(int idx) const
306     //! @brief        画像データのバイナリデータを取得します。
307     //---------------------------------------------------------------------------
308     //---------------------------------------------------------------------------
309     //! @fn           void SetImageData(int idx, u8 value)
310     //! @brief        画像データのバイナリデータのリストに要素を設定します。
311     //---------------------------------------------------------------------------
312     NW_RES_FIELD_PRIMITIVE_LIST_DECL( u8, ImageData ) // GetImageData(), GetImageData(int idx), GetImageDataCount()
313 
314     //---------------------------------------------------------------------------
315     //! @brief        コピーされたテクスチャのアドレスを取得します。
316     //!
317     //! @return       コピーされたテクスチャの論理アドレスです。
318     //---------------------------------------------------------------------------
319     u32 GetLocationAddress() const { return ref().m_LocationAddress; }
320 
321     //---------------------------------------------------------------------------
322     //! @brief        コピーされたテクスチャのアドレスを設定します。
323     //!
324     //! @details      Setup 後には アドレスは TextureMapper 内のコマンドに格納されるので
325     //!               Setup の前に設定します。
326     //!
327     //! @param[in]    address コピーされたテクスチャの論理アドレスです。VRAM 上のアドレスでも構いません。
328     //---------------------------------------------------------------------------
SetLocationAddress(u32 address)329     void SetLocationAddress(u32 address) { ref().m_LocationAddress = address; }
SetLocationAddress(const void * address)330     void SetLocationAddress(const void* address) { ref().m_LocationAddress = reinterpret_cast<u32>(address); }
331 
332     //---------------------------------------------------------------------------
333     //! @brief        1 ピクセルあたりのビット長を取得します。
334     //---------------------------------------------------------------------------
GetBitsPerPixel()335     u32 GetBitsPerPixel() const { return ref().m_BitsPerPixel; }
336 
337     //---------------------------------------------------------------------------
338     //! @brief        GPU から参照されるイメージのアドレスを取得します。
339     //!
340     //! @details      LocationAddess が設定されている場合はそのアドレスを使用し、
341     //!               LocationAddess が NULL の場合には、ImageData のアドレスを直接 GPU から参照します。
342     //!
343     //! @return       GPU から参照されるイメージのアドレスです。
344     //---------------------------------------------------------------------------
GetImageAddress()345     u32 GetImageAddress() const
346     {
347         u32 locationAddress = this->GetLocationAddress();
348 
349         if ( locationAddress )
350         {
351             return locationAddress;
352         }
353         else
354         {
355             return reinterpret_cast<u32>( this->GetImageData() );
356         }
357     }
358 
359     //---------------------------------------------------------------------------
360     //! @brief        テクスチャイメージのメモリ配置が不正でないかチェックします。
361     //!
362     //! @return       問題なければ true 問題があれば false を返します。
363     //---------------------------------------------------------------------------
364     bool CheckMemoryLocation() const;
365 };
366 typedef nw::ut::ResArrayClass<ResPixelBasedImage>::type        ResPixelBasedImageArray;
367 typedef nw::ut::ResArrayClass<const ResPixelBasedImage>::type  ResPixelBasedImageArrayConst;
368 
369 //--------------------------------------------------------------------------
370 //! @brief 画像データによる平面テクスチャを表すバイナリリソースクラスです。
371 //---------------------------------------------------------------------------
372 class ResImageTexture : public ResPixelBasedTexture
373 {
374 public:
375     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResImageTexture) };
376     enum { SIGNATURE = NW_RES_SIGNATURE32('TXIM') };
377 
NW_RES_CTOR_INHERIT(ResImageTexture,ResPixelBasedTexture)378     NW_RES_CTOR_INHERIT( ResImageTexture, ResPixelBasedTexture )
379 
380     //---------------------------------------------------------------------------
381     //! @fn           ResPixelBasedImage GetImage()
382     //! @brief        ピクセルデータを取得します。
383     //---------------------------------------------------------------------------
384     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, Image ) // GetImage()
385 
386     //---------------------------------------------------------------------------
387     //! @brief        テクスチャイメージのアドレスを取得します。
388     //!
389     //! @return       テクスチャイメージの論理アドレスです。
390     //---------------------------------------------------------------------------
391     u32 GetLocationAddress() const { return this->GetImage().GetLocationAddress(); }
392 
393     //---------------------------------------------------------------------------
394     //! @brief        展開済みのテクスチャイメージのアドレスを設定します。
395     //!
396     //! @details      Setup 前に設定します。
397     //!               LocationFlag と LocationAddress を両方設定した場合には
398     //!               LocationAddress を優先し、LocationFlag は無効となります。
399     //!               LocationAddress ではテクスチャのコピーはおこないません。
400     //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
401     //!
402     //! @param[in]    address 展開済みのテクスチャイメージの論理アドレスです。VRAM 上のアドレスでも構いません。
403     //---------------------------------------------------------------------------
SetLocationAddress(u32 address)404     void SetLocationAddress(u32 address) { this->GetImage().SetLocationAddress( address ); }
SetLocationAddress(const void * address)405     void SetLocationAddress(const void* address) { this->GetImage().SetLocationAddress( address ); }
406 
407     //! @brief 設定内容です。
408     struct Description
409     {
410         s32 height;                 //!< テクスチャの高さです。
411         s32 width;                  //!< テクスチャの幅です。
412         s32 mipmapSize;             //!< ミップマップサイズです。
413         u32 locationFlag;           //!< テクスチャの配置情報フラグです。
414         u32 locationAddress;        //!< テクスチャの配置アドレスです。
415         u32 formatHW;               //!< レジスタに設定するフォーマットの値です。
416         bool executingMemoryFill;   //!< 確保したピクセル領域をゼロでクリアするかどうかを表します。
417         bool isDynamicAllocation;   //!< 動的にメモリ確保を行うフラグです。
418 
419         //! @brief コンストラクタです。
DescriptionDescription420         Description()
421          : height(0),
422            width(0),
423            mipmapSize(1),
424            locationFlag(NN_GX_MEM_VRAMA | GL_NO_COPY_FCRAM_DMP),
425            locationAddress(0),
426            formatHW(ResPixelBasedTexture::FORMAT_HW_RGBA8),
427            executingMemoryFill(false),
428            isDynamicAllocation(true)
429         {}
430     };
431 
432     //----------------------------------------
433     //! @name 作成/破棄
434     //@{
435 
436     //! @brief テクスチャを動的に構築するためのクラスです。
437     //!
438     //!        指定したテクスチャの高さ × 幅 × 1ピクセルあたりのバイト数分の
439     //!        メモリが確保されます。
440     //!        ミップマップサイズが指定されている場合は、ミップマップのための
441     //!        領域も併せて確保されます。
442     class DynamicBuilder
443     {
444     public:
445         //! コンストラクタです。
DynamicBuilder()446         DynamicBuilder() {}
447         //! デストラクタです。
~DynamicBuilder()448         ~DynamicBuilder() {}
449 
450         //! テクスチャの高さを設定します。
Height(s32 height)451         DynamicBuilder& Height(s32 height)
452         {
453             m_Description.height = height;
454             return *this;
455         }
456 
457         //! テクスチャの幅を設定します。
Width(s32 width)458         DynamicBuilder& Width(s32 width)
459         {
460             m_Description.width = width;
461             return *this;
462         }
463 
464         //! ミップマップサイズを設定します。
465         //! ミップマップを使用しないときは 1 を、
466         //! ミップマップを使用するときは ミップマップ数 + 1 を指定します。
MipmapSize(s32 mipmapSize)467         DynamicBuilder& MipmapSize(s32 mipmapSize)
468         {
469             m_Description.mipmapSize = mipmapSize;
470             return *this;
471         }
472 
473         //---------------------------------------------------------------------------
474         //! @brief        テクスチャの配置情報フラグを設定します。
475         //!
476         //! @details      LocationFlag と LocationAddress を両方設定した場合には
477         //!               LocationAddress を優先し、LocationFlag は無効となります。
478         //!
479         //! @param[in]    locationFlag イメージの配置メモリを指定します。
480         //!
481         //! @return       自インスタンスへの参照を返します。
482         //---------------------------------------------------------------------------
LocationFlag(u32 locationFlag)483         DynamicBuilder& LocationFlag(u32 locationFlag)
484         {
485             m_Description.locationFlag = locationFlag;
486             return *this;
487         }
488 
489         //---------------------------------------------------------------------------
490         //! @brief        テクスチャの配置アドレスを設定します。
491         //!
492         //! @details      LocationFlag と LocationAddress を両方設定した場合には
493         //!               LocationAddress を優先し、LocationFlag は無効となります。
494         //!               LocationAddress ではテクスチャのコピーはおこないません。
495         //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
496         //!
497         //! @param[in]    locationAddress イメージを展開済みのアドレスを指定します。VRAM 上のアドレスでも構いません。
498         //!
499         //! @return       自インスタンスへの参照を返します。
500         //---------------------------------------------------------------------------
LocationAddress(u32 locationAddress)501         DynamicBuilder& LocationAddress(u32 locationAddress)
502         {
503             m_Description.locationAddress = locationAddress;
504             return *this;
505         }
506 
507         //! レジスタに設定するフォーマットの値を設定します。
Format(ResPixelBasedTexture::FormatHW format)508         DynamicBuilder& Format(ResPixelBasedTexture::FormatHW format)
509         {
510             m_Description.formatHW = static_cast<u32>(format);
511             return *this;
512         }
513 
514         //! 確保したピクセル領域をゼロでクリアするかどうかを設定します。
ExecutingMemoryFill(bool executingMemoryFill)515         DynamicBuilder& ExecutingMemoryFill(bool executingMemoryFill)
516         {
517             m_Description.executingMemoryFill = executingMemoryFill;
518             return *this;
519         }
520 
521         //! @brief 動的に FCRAM 上に領域を確保するフラグを設定します。
522         //!
523         //!        ResImageTexture ではデフォルトで動的にメモリ確保を行います。
DynamicAllocation(bool isDynamicAllocation)524         DynamicBuilder& DynamicAllocation(bool isDynamicAllocation)
525         {
526             m_Description.isDynamicAllocation = isDynamicAllocation;
527             return *this;
528         }
529 
530         //! @brief        テクスチャを動的に生成します。
531         //!
532         //! @param[in]    allocator リソース用のメモリを解放するアロケータです。
533         //!
534         //! @return       生成したテクスチャを返します。
535         //!
536         ResImageTexture Create(os::IAllocator* allocator);
537 
538     private:
539         Description m_Description;
540     };
541 
542     //! @brief 動的に生成したテクスチャを破棄します。
543     //!
544     void DynamicDestroy();
545 
546     //@}
547 };
548 
549 //--------------------------------------------------------------------------
550 //! @brief キューブテクスチャを表すバイナリリソースクラスです。
551 //---------------------------------------------------------------------------
552 class ResCubeTexture : public ResPixelBasedTexture
553 {
554 public:
555 
556     //! @brief キューブテクスチャの面を表します。
557     enum CubeFace
558     {
559         CUBE_FACE_POSITIVE_X = 0,   //!< X 軸の+方向です。
560         CUBE_FACE_NEGATIVE_X,       //!< X 軸の-方向です。
561         CUBE_FACE_POSITIVE_Y,       //!< Y 軸の+方向です。
562         CUBE_FACE_NEGATIVE_Y,       //!< Y 軸の-方向です。
563         CUBE_FACE_POSITIVE_Z,       //!< Z 軸の+方向です。
564         CUBE_FACE_NEGATIVE_Z,       //!< Z 軸の-方向です。
565         MAX_CUBE_FACE               //!< 面の数です。
566     };
567 
568     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResCubeTexture) };
569     enum { SIGNATURE = NW_RES_SIGNATURE32('TXCB') };
570 
NW_RES_CTOR_INHERIT(ResCubeTexture,ResPixelBasedTexture)571     NW_RES_CTOR_INHERIT( ResCubeTexture, ResPixelBasedTexture )
572 
573     //---------------------------------------------------------------------------
574     //! @fn           ResPixelBasedImage GetPositiveXImage()
575     //! @brief        X正方向のピクセルデータを取得します。
576     //---------------------------------------------------------------------------
577     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, PositiveXImage ) // GetPositiveXImage()
578 
579     //---------------------------------------------------------------------------
580     //! @fn           ResPixelBasedImage GetNegativeXImage()
581     //! @brief        X負方向のピクセルデータを取得します。
582     //---------------------------------------------------------------------------
583     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, NegativeXImage ) // GetNegativeXImage()
584 
585     //---------------------------------------------------------------------------
586     //! @fn           ResPixelBasedImage GetPositiveYImage()
587     //! @brief        Y正方向のピクセルデータを取得します。
588     //---------------------------------------------------------------------------
589     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, PositiveYImage ) // GetPositiveYImage()
590 
591     //---------------------------------------------------------------------------
592     //! @fn           ResPixelBasedImage GetNegativeYImage()
593     //! @brief        Y負方向のピクセルデータを取得します。
594     //---------------------------------------------------------------------------
595     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, NegativeYImage ) // GetNegativeYImage()
596 
597     //---------------------------------------------------------------------------
598     //! @fn           ResPixelBasedImage GetPositiveZImage()
599     //! @brief        Z正方向のピクセルデータを取得します。
600     //---------------------------------------------------------------------------
601     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, PositiveZImage ) // GetPositiveZImage()
602 
603     //---------------------------------------------------------------------------
604     //! @fn           ResPixelBasedImage GetNegativeZImage()
605     //! @brief        Z負方向のピクセルデータを取得します。
606     //---------------------------------------------------------------------------
607     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, NegativeZImage ) // GetNegativeZImage()
608 
609     //---------------------------------------------------------------------------
610     //! @brief  キューブテクスチャの面の数を取得します。
611     //!
612     //! @return キューブテクスチャの面の数を返します。
613     //---------------------------------------------------------------------------
614     s32     GetCubeFaceCount() const { return MAX_CUBE_FACE; }
615 
616     //---------------------------------------------------------------------------
617     //! @brief      キューブテクスチャのイメージを取得します。
618     //!
619     //! @param[in]  face 取得する面です。
620     //!
621     //! @return     指定した面のイメージを返します。
622     //---------------------------------------------------------------------------
GetImage(CubeFace face)623     ResPixelBasedImage  GetImage( CubeFace face )
624     {
625         NW_ASSERT( 0 <= face && face < MAX_CUBE_FACE );
626 
627         return ResPixelBasedImage( ref().toImagesTables[ face ].to_ptr() );
628     }
629 
630     //---------------------------------------------------------------------------
631     //! @brief      キューブテクスチャのイメージを取得します。
632     //!
633     //! @param[in]  face 取得する面です。
634     //!
635     //! @return     指定した面のイメージを返します。
636     //---------------------------------------------------------------------------
GetImage(CubeFace face)637     const ResPixelBasedImage    GetImage( CubeFace face ) const
638     {
639         NW_ASSERT( 0 <= face && face < MAX_CUBE_FACE );
640 
641         return ResPixelBasedImage( ref().toImagesTables[ face ].to_ptr() );
642     }
643 
644     //---------------------------------------------------------------------------
645     //! @brief        テクスチャイメージのアドレスを取得します。
646     //!
647     //! @param[in]    face キューブテクスチャの面です。
648     //!
649     //! @return       テクスチャイメージの論理アドレスです。
650     //---------------------------------------------------------------------------
GetLocationAddress(CubeFace face)651     u32 GetLocationAddress( CubeFace face ) const { return this->GetImage( face ).GetLocationAddress(); }
652 
653     //---------------------------------------------------------------------------
654     //! @brief        展開済みのテクスチャイメージのアドレスを設定します。
655     //!
656     //! @details      Setup 前に設定します。
657     //!               LocationAddress ではテクスチャのコピーはおこないません。
658     //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
659     //!
660     //! @param[in]    face    キューブテクスチャの面です。
661     //! @param[in]    address 展開済みのテクスチャイメージの論理アドレスです。VRAM 上のアドレスでも構いません。
662     //---------------------------------------------------------------------------
SetLocationAddress(CubeFace face,u32 address)663     void SetLocationAddress( CubeFace face, u32 address) { this->GetImage( face ).SetLocationAddress( address ); }
SetLocationAddress(CubeFace face,const void * address)664     void SetLocationAddress( CubeFace face, const void* address) { this->GetImage( face ).SetLocationAddress( address ); }
665 
666     //! @brief 設定内容です。
667     struct Description
668     {
669         s32 width;                          //!< 1 つの面の幅です。
670         s32 mipmapSize;                     //!< ミップマップサイズです。
671         u32 locationFlag;                   //!< テクスチャの配置情報フラグです。
672         u32 locationAddress[MAX_CUBE_FACE]; //!< テクスチャの配置アドレスです。
673         u32 formatHW;                       //!< レジスタに設定するフォーマットの値です。
674         bool executingMemoryFill;           //!< 確保したピクセル領域をゼロでクリアするかどうかを表します。
675         bool isDynamicAllocation;           //!< 動的にメモリ確保を行うフラグです。
676 
677         //! @brief コンストラクタです。
DescriptionDescription678         Description()
679         : width(0),
680           mipmapSize(1),
681           locationFlag(NN_GX_MEM_VRAMA | GL_NO_COPY_FCRAM_DMP),
682           formatHW(ResPixelBasedTexture::FORMAT_HW_RGBA8),
683           executingMemoryFill(false),
684           isDynamicAllocation(true)
685         {
686             for (int i = 0 ; i < MAX_CUBE_FACE ; ++i)
687             {
688                 locationAddress[i] = 0;
689             }
690         }
691     };
692 
693     //----------------------------------------
694     //! @name 作成/破棄
695     //@{
696 
697     //! @brief キューブテクスチャを動的に構築するためのクラスです。
698     //!
699     //!        指定したテクスチャの幅 × 幅 × 1 ピクセルあたりのバイト数 × 6 の
700     //!        メモリが確保されます。
701     //!        ミップマップサイズが指定されている場合は、ミップマップのための
702     //!        領域も併せて確保されます。
703     class DynamicBuilder
704     {
705     public:
706         //! コンストラクタです。
DynamicBuilder()707         DynamicBuilder() {}
708         //! デストラクタです。
~DynamicBuilder()709         ~DynamicBuilder() {}
710 
711         //! @brief 1 つの面の幅を設定します。
712         //!
713         //!        キューブテクスチャの 1 つの面は正方形である必要があるため、
714         //!        幅と高さを個別に指定することはできません。
Width(s32 width)715         DynamicBuilder& Width(s32 width)
716         {
717             m_Description.width = width;
718             return *this;
719         }
720 
721         //! ミップマップサイズを設定します。
722         //! ミップマップを使用しないときは 1 を、
723         //! ミップマップを使用するときは ミップマップ数 + 1 を指定します。
MipmapSize(s32 mipmapSize)724         DynamicBuilder& MipmapSize(s32 mipmapSize)
725         {
726             m_Description.mipmapSize = mipmapSize;
727             return *this;
728         }
729 
730         //---------------------------------------------------------------------------
731         //! @brief        テクスチャの配置情報フラグを設定します。
732         //!
733         //! @details      LocationFlag と 6 面分の LocationAddress を両方設定した場合には
734         //!               LocationAddress を優先し、LocationFlag は無効となります。
735         //!
736         //! @param[in]    locationFlag イメージの配置メモリを指定します。
737         //!
738         //! @return       自インスタンスへの参照を返します。
739         //---------------------------------------------------------------------------
LocationFlag(u32 locationFlag)740         DynamicBuilder& LocationFlag(u32 locationFlag)
741         {
742             m_Description.locationFlag = locationFlag;
743             return *this;
744         }
745 
746         //---------------------------------------------------------------------------
747         //! @brief        テクスチャの配置アドレスを設定します。
748         //!
749         //! @details      LocationFlag と 6 面分の LocationAddress を両方設定した場合には
750         //!               LocationAddress を優先し、LocationFlag は無効となります。
751         //!               LocationAddress ではテクスチャのコピーはおこないません。
752         //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
753         //!
754         //! @param[in]    face            face    キューブテクスチャの面です。
755         //! @param[in]    locationAddress イメージを展開済みのアドレスを指定します。VRAM 上のアドレスでも構いません。
756         //!
757         //! @return       自インスタンスへの参照を返します。
758         //---------------------------------------------------------------------------
LocationAddress(CubeFace face,u32 locationAddress)759         DynamicBuilder& LocationAddress(CubeFace face, u32 locationAddress)
760         {
761             NW_MINMAXLT_ASSERT(face, 0, MAX_CUBE_FACE);
762             m_Description.locationAddress[face] = locationAddress;
763             return *this;
764         }
765 
766         //! レジスタに設定するフォーマットの値を設定します。
Format(ResPixelBasedTexture::FormatHW format)767         DynamicBuilder& Format(ResPixelBasedTexture::FormatHW format)
768         {
769             m_Description.formatHW = static_cast<u32>(format);
770             return *this;
771         }
772 
773         //! 確保したピクセル領域をゼロでクリアするかどうかを設定します。
ExecutingMemoryFill(bool executingMemoryFill)774         DynamicBuilder& ExecutingMemoryFill(bool executingMemoryFill)
775         {
776             m_Description.executingMemoryFill = executingMemoryFill;
777             return *this;
778         }
779 
780         //! @brief 動的に FCRAM 上に領域を確保するフラグを設定します。
781         //!
782         //!        ResCubeTexture ではデフォルトで動的にメモリ確保を行います。
DynamicAllocation(bool isDynamicAllocation)783         DynamicBuilder& DynamicAllocation(bool isDynamicAllocation)
784         {
785             m_Description.isDynamicAllocation = isDynamicAllocation;
786             return *this;
787         }
788 
789         //! @brief        テクスチャを動的に生成します。
790         //!
791         //!               すべての面を 32MB 境界内に収めるというキューブテクスチャの制限事項を考慮せずに
792         //!               アロケータからメモリを確保します。
793         //!
794         //!               この問題は確保されるアドレスがすべて 32MB 境界内におさまるような
795         //!               アロケータを実装して使用して回避することができます。
796         //!
797         //! @param[in]    allocator リソース用のメモリを解放するアロケータです。
798         //!
799         //! @return       生成したテクスチャを返します。
800         //!
801         ResCubeTexture Create(os::IAllocator* allocator);
802 
803     private:
804         Description m_Description;
805     };
806 
807     //! @brief 動的に生成したキューブテクスチャを破棄します。
808     //!
809     void DynamicDestroy();
810 
811     //@}
812 };
813 
814 
815 //--------------------------------------------------------------------------
816 //! @brief 画像データによるシャドウテクスチャを表すバイナリリソースクラスです。
817 //---------------------------------------------------------------------------
818 class ResShadowTexture : public ResPixelBasedTexture
819 {
820 public:
821     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResShadowTexture) };
822     enum { SIGNATURE = NW_RES_SIGNATURE32('TXSH') };
823 
NW_RES_CTOR_INHERIT(ResShadowTexture,ResPixelBasedTexture)824     NW_RES_CTOR_INHERIT( ResShadowTexture, ResPixelBasedTexture )
825 
826     //---------------------------------------------------------------------------
827     //! @fn           ResPixelBasedImage GetImage()
828     //! @brief        ピクセルデータを取得します。
829     //---------------------------------------------------------------------------
830     NW_RES_FIELD_CLASS_DECL( ResPixelBasedImage, Image ) // GetImage()
831     NW_RES_FIELD_BOOL_PRIMITIVE_DECL( PerspectiveShadow )      // IsPerspectiveShadow(), SetPerspectiveShadow()
832     NW_RES_FIELD_PRIMITIVE_DECL( f32, ShadowZBias )    // GetShadowZBias(), SetShadowZBias()
833     NW_RES_FIELD_PRIMITIVE_DECL( f32, ShadowZScale )    // GetShadowZScale(), SetShadowZScale()
834 
835     //---------------------------------------------------------------------------
836     //! @brief        テクスチャイメージのアドレスを取得します。
837     //!
838     //! @return       テクスチャイメージの論理アドレスです。
839     //---------------------------------------------------------------------------
840     u32 GetLocationAddress() const { return this->GetImage().GetLocationAddress(); }
841 
842     //---------------------------------------------------------------------------
843     //! @brief        展開済みのテクスチャイメージのアドレスを設定します。
844     //!
845     //! @details      Setup 前に設定します。
846     //!               LocationFlag と LocationAddress を両方設定した場合には
847     //!               LocationAddress を優先し、LocationFlag は無効となります。
848     //!               LocationAddress ではテクスチャのコピーはおこないません。
849     //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
850     //!
851     //! @param[in]    address 展開済みのテクスチャイメージの論理アドレスです。VRAM 上のアドレスでも構いません。
852     //---------------------------------------------------------------------------
SetLocationAddress(u32 address)853     void SetLocationAddress(u32 address) { this->GetImage().SetLocationAddress( address ); }
SetLocationAddress(const void * address)854     void SetLocationAddress(const void* address) { this->GetImage().SetLocationAddress( address ); }
855 
856 
857     //! @brief 設定内容です。
858     struct Description
859     {
860         s32 height;                 //!< テクスチャの高さです。
861         s32 width;                  //!< テクスチャの幅です。
862         u32 locationFlag;           //!< テクスチャの配置情報フラグです。
863         u32 locationAddress;        //!< テクスチャの配置アドレスです。
864         bool executingMemoryFill;   //!< 確保したピクセル領域をゼロでクリアするかどうかを表します。
865         bool isPerspectiveShadow;   //!< 投影シャドウかどうかです。
866         f32 shadowZBias;            //!< シャドウテクスチャの Z バイアス値です。
867         f32 shadowZScale;           //!< オフセット値に乗算するスケール値です。
868         bool isDynamicAllocation;   //!< 動的にメモリ確保を行うフラグです。
869 
870         //! @brief コンストラクタです。
DescriptionDescription871         Description()
872          : height(0),
873            width(0),
874            locationFlag(NN_GX_MEM_VRAMA | GL_NO_COPY_FCRAM_DMP),
875            locationAddress(0),
876            executingMemoryFill(false),
877            isPerspectiveShadow(true),
878            shadowZBias(0.0f),
879            shadowZScale(1.0f),
880            isDynamicAllocation(false)
881         {}
882     };
883 
884     //----------------------------------------
885     //! @name 作成/破棄
886     //@{
887 
888     //! @brief テクスチャを動的に構築するためのクラスです。
889     //!        指定したテクスチャの高さ x 幅 x 1ピクセルあたりのバイト数分の
890     //!        メモリが確保されます。
891     //!        ミップマップサイズが指定されている場合は、ミップマップのための
892     //!        領域も併せて確保されます。
893     class DynamicBuilder
894     {
895     public:
896         //! コンストラクタです。
DynamicBuilder()897         DynamicBuilder() {}
898         //! デストラクタです。
~DynamicBuilder()899         ~DynamicBuilder() {}
900 
901         //! テクスチャの高さを設定します。
Height(s32 height)902         DynamicBuilder& Height(s32 height)
903         {
904             m_Description.height = height;
905             return *this;
906         }
907 
908         //! テクスチャの幅を設定します。
Width(s32 width)909         DynamicBuilder& Width(s32 width)
910         {
911             m_Description.width = width;
912             return *this;
913         }
914 
915         //---------------------------------------------------------------------------
916         //! @brief        テクスチャの配置情報フラグを設定します。
917         //!
918         //! @details      LocationFlag と LocationAddress を両方設定した場合には
919         //!               LocationAddress を優先し、LocationFlag は無効となります。
920         //!
921         //! @param[in]    locationFlag イメージの配置メモリを指定します。
922         //!
923         //! @return       自インスタンスへの参照を返します。
924         //---------------------------------------------------------------------------
LocationFlag(u32 locationFlag)925         DynamicBuilder& LocationFlag(u32 locationFlag)
926         {
927             m_Description.locationFlag = locationFlag;
928             return *this;
929         }
930 
931         //---------------------------------------------------------------------------
932         //! @brief        テクスチャの配置アドレスを設定します。
933         //!
934         //! @details      LocationFlag と LocationAddress を両方設定した場合には
935         //!               LocationAddress を優先し、LocationFlag は無効となります。
936         //!               LocationAddress ではテクスチャのコピーはおこないません。
937         //!               nngxAddVramDmaCommand 等を利用して別途イメージの転送をおこなってください。
938         //!
939         //! @param[in]    locationAddress イメージを展開済みのアドレスを指定します。VRAM 上のアドレスでも構いません。
940         //!
941         //! @return       自インスタンスへの参照を返します。
942         //---------------------------------------------------------------------------
LocationAddress(u32 locationAddress)943         DynamicBuilder& LocationAddress(u32 locationAddress)
944         {
945             m_Description.locationAddress = locationAddress;
946             return *this;
947         }
948 
949         //! 確保したピクセル領域をゼロでクリアするかどうかを設定します。
ExecutingMemoryFill(bool executingMemoryFill)950         DynamicBuilder& ExecutingMemoryFill(bool executingMemoryFill)
951         {
952             m_Description.executingMemoryFill = executingMemoryFill;
953             return *this;
954         }
955 
956         //! 投影シャドウかどうかを設定します。
PerspectiveShadow(bool isPerspectiveShadow)957         DynamicBuilder& PerspectiveShadow(bool isPerspectiveShadow)
958         {
959             m_Description.isPerspectiveShadow = isPerspectiveShadow;
960             return *this;
961         }
962 
963         //! シャドウテクスチャの Z バイアス値を設定します。
ShadowZBias(f32 shadowZBias)964         DynamicBuilder& ShadowZBias(f32 shadowZBias)
965         {
966             m_Description.shadowZBias = shadowZBias;
967             return *this;
968         }
969 
970         //! オフセット値に乗算するスケール値を設定します。
ShadowZScale(f32 shadowZScale)971         DynamicBuilder& ShadowZScale(f32 shadowZScale)
972         {
973             m_Description.shadowZScale = shadowZScale;
974             return *this;
975         }
976 
977         //! @brief 動的に FCRAM 上に領域を確保するフラグを設定します。
978         //!        ResShadowTexture ではデフォルトで動的にメモリ確保を行いません。
DynamicAllocation(bool isDynamicAllocation)979         DynamicBuilder& DynamicAllocation(bool isDynamicAllocation)
980         {
981             m_Description.isDynamicAllocation = isDynamicAllocation;
982             return *this;
983         }
984 
985         //! @brief        テクスチャを動的に生成します。
986         //!
987         //! @param[in]    allocator リソース用のメモリを解放するアロケータです。
988         //!
989         //! @return       生成したテクスチャを返します。
990         //!
991         ResShadowTexture Create(os::IAllocator* allocator);
992 
993     private:
994         Description m_Description;
995     };
996 
997     //! @brief 動的に生成したテクスチャを破棄します。
998     //!
999     void DynamicDestroy();
1000 
1001     //@}
1002 };
1003 
1004 //--------------------------------------------------------------------------
1005 //! @brief テクスチャを間接的に参照として表すバイナリリソースクラスです。
1006 //---------------------------------------------------------------------------
1007 class ResReferenceTexture : public ResTexture
1008 {
1009 public:
1010     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResReferenceTexture) };
1011     enum { SIGNATURE = NW_RES_SIGNATURE32('TXRF') };
1012 
1013     NW_RES_CTOR_INHERIT( ResReferenceTexture, ResTexture )
1014 
1015     //---------------------------------------------------------------------------
1016     //! @fn           const char * GetPath() const
1017     //! @brief        参照先テクスチャを表すパスを取得します。
1018     //---------------------------------------------------------------------------
1019     NW_RES_FIELD_STRING_DECL( Path )                     // GetPath()
1020 
1021     //---------------------------------------------------------------------------
1022     //! @fn           ResTexture GetTargetTexture()
1023     //! @brief        参照先テクスチャを取得します。
1024     //---------------------------------------------------------------------------
1025     NW_RES_FIELD_CLASS_DECL( ResTexture, TargetTexture ) // GetTargetTexture()
1026 };
1027 
1028 
1029 
1030 
1031 //----------------------------------------
1032 NW_INLINE ResTexture
Dereference()1033 ResTexture::Dereference()
1034 {
1035     NW_ASSERT( this->IsValid() );
1036 
1037     if ( this->ref().typeInfo == ResReferenceTexture::TYPE_INFO )
1038     {
1039         return ResStaticCast<ResReferenceTexture>(*this).GetTargetTexture();
1040     }
1041     else
1042     {
1043         return *this;
1044     }
1045 }
1046 
1047 //----------------------------------------
1048 NW_INLINE const ResTexture
Dereference()1049 ResTexture::Dereference() const
1050 {
1051     NW_ASSERT( this->IsValid() );
1052 
1053     if ( this->ref().typeInfo == ResReferenceTexture::TYPE_INFO )
1054     {
1055         return ResStaticCast<ResReferenceTexture>(*this).GetTargetTexture();
1056     }
1057     else
1058     {
1059         return *this;
1060     }
1061 }
1062 
1063 } // namespace res
1064 } // namespace gfx
1065 } // namespace nw
1066 
1067 #endif // NW_GFX_RESTEXTURE_H_
1068