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