319
319
// Avoiding 16 Common OpenGL Pitfalls
320
320
// http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
321
321
// 7. Watch Your Pixel Store Alignment
322
t_s32 ImageSurface::GetLevelPitch(BitmapFormat format, t_s32 width,
323
t_s32 height, t_s32 miplevel)
325
t_s32 levelwidth = ImageSurface::GetLevelDim(format, width, miplevel);
327
t_s32 bpe = GPixelFormats[format].BlockBytes;
328
t_s32 memalignment = GPixelFormats[format].RowMemoryAlignment;
329
t_s32 block = GPixelFormats[format].BlockSizeX;
330
t_s32 shift = Log2(GPixelFormats[format].BlockSizeX);
331
t_s32 pitch = Align((bpe * ((levelwidth + (block - 1)) >> shift)), memalignment);
336
t_s32 ImageSurface::GetLevelPitchNoMemAlignment(BitmapFormat format, t_s32 width, t_s32 height, t_s32 miplevel)
338
t_s32 levelwidth = ImageSurface::GetLevelDim(format, width, miplevel);
340
t_s32 bpe = GPixelFormats[format].BlockBytes;
341
t_s32 block = GPixelFormats[format].BlockSizeX;
342
t_s32 shift = Log2(GPixelFormats[format].BlockSizeX);
343
t_s32 pitch = Align((bpe * ((levelwidth + (block - 1)) >> shift)), 1);
348
t_s32 ImageSurface::GetLevelSize(BitmapFormat format, t_s32 width, t_s32 height, t_s32 miplevel)
350
t_s32 pitch = ImageSurface::GetLevelPitch(format, width, height, miplevel);
351
t_s32 levelheight = ImageSurface::GetLevelDim(format, height, miplevel);
353
t_s32 block = GPixelFormats[format].BlockSizeY;
354
t_s32 HeightInBlocks = Align(levelheight, block) / block;
356
t_s32 size = pitch * HeightInBlocks;
322
int ImageSurface::GetLevelPitch(BitmapFormat format, int width,
323
int height, int miplevel)
325
int levelwidth = ImageSurface::GetLevelDim(format, width, miplevel);
327
int bpe = GPixelFormats[format].BlockBytes;
328
int memalignment = GPixelFormats[format].RowMemoryAlignment;
329
int block = GPixelFormats[format].BlockSizeX;
330
int shift = Log2(GPixelFormats[format].BlockSizeX);
331
int pitch = Align((bpe * ((levelwidth + (block - 1)) >> shift)), memalignment);
336
int ImageSurface::GetLevelPitchNoMemAlignment(BitmapFormat format, int width, int height, int miplevel)
338
int levelwidth = ImageSurface::GetLevelDim(format, width, miplevel);
340
int bpe = GPixelFormats[format].BlockBytes;
341
int block = GPixelFormats[format].BlockSizeX;
342
int shift = Log2(GPixelFormats[format].BlockSizeX);
343
int pitch = Align((bpe * ((levelwidth + (block - 1)) >> shift)), 1);
348
int ImageSurface::GetLevelSize(BitmapFormat format, int width, int height, int miplevel)
350
int pitch = ImageSurface::GetLevelPitch(format, width, height, miplevel);
351
int levelheight = ImageSurface::GetLevelDim(format, height, miplevel);
353
int block = GPixelFormats[format].BlockSizeY;
354
int HeightInBlocks = Align(levelheight, block) / block;
356
int size = pitch * HeightInBlocks;
360
t_s32 ImageSurface::GetLevelSize(BitmapFormat format, t_s32 width, t_s32 height, t_s32 depth, t_s32 miplevel)
360
int ImageSurface::GetLevelSize(BitmapFormat format, int width, int height, int depth, int miplevel)
362
t_s32 pitch = ImageSurface::GetLevelPitch(format, width, height, miplevel);
363
t_s32 levelheight = ImageSurface::GetLevelDim(format, height, miplevel);
364
t_s32 leveldepth = ImageSurface::GetLevelDim(format, depth, miplevel);
366
t_s32 block = GPixelFormats[format].BlockSizeY;
367
t_s32 HeightInBlocks = Align(levelheight, block) / block;
369
t_s32 size = pitch * HeightInBlocks;
362
int pitch = ImageSurface::GetLevelPitch(format, width, height, miplevel);
363
int levelheight = ImageSurface::GetLevelDim(format, height, miplevel);
364
int leveldepth = ImageSurface::GetLevelDim(format, depth, miplevel);
366
int block = GPixelFormats[format].BlockSizeY;
367
int HeightInBlocks = Align(levelheight, block) / block;
369
int size = pitch * HeightInBlocks;
370
370
return leveldepth * size;
373
t_s32 ImageSurface::GetLevelWidth(BitmapFormat format, t_s32 width, t_s32 miplevel)
375
// return 1 if the mip level does not exist.
376
return Max<t_s32> (1, width >> miplevel);
379
t_s32 ImageSurface::GetLevelHeight(BitmapFormat format, t_s32 height, t_s32 miplevel)
381
// return 1 if the mip level does not exist.
382
return Max<t_s32> (1, height >> miplevel);
385
t_s32 ImageSurface::GetLevelDim(BitmapFormat format, t_s32 length, t_s32 miplevel)
387
// return 1 if the mip level does not exist.
388
return Max<t_s32> (1, length >> miplevel);
391
t_s32 ImageSurface::GetNumMipLevel(BitmapFormat format, t_s32 width, t_s32 height)
393
t_s32 NumTotalMipLevel = 1 + floorf(Log2(Max(width, height)));
373
int ImageSurface::GetLevelWidth(BitmapFormat format, int width, int miplevel)
375
// return 1 if the mip level does not exist.
376
return Max<int> (1, width >> miplevel);
379
int ImageSurface::GetLevelHeight(BitmapFormat format, int height, int miplevel)
381
// return 1 if the mip level does not exist.
382
return Max<int> (1, height >> miplevel);
385
int ImageSurface::GetLevelDim(BitmapFormat format, int length, int miplevel)
387
// return 1 if the mip level does not exist.
388
return Max<int> (1, length >> miplevel);
391
int ImageSurface::GetNumMipLevel(BitmapFormat format, int width, int height)
393
int NumTotalMipLevel = 1 + floorf(Log2(Max(width, height)));
394
394
return NumTotalMipLevel;
397
t_s32 ImageSurface::GetMemAlignment(BitmapFormat format)
397
int ImageSurface::GetMemAlignment(BitmapFormat format)
399
399
return GPixelFormats[format].RowMemoryAlignment;
402
t_s32 ImageSurface::GetLevelBlockWidth(BitmapFormat format, t_s32 width, t_s32 miplevel)
402
int ImageSurface::GetLevelBlockWidth(BitmapFormat format, int width, int miplevel)
404
t_s32 block = GPixelFormats[format].BlockSizeX;
405
t_s32 WidthInBlocks = Align(GetLevelDim(format, width, miplevel), block) / block;
404
int block = GPixelFormats[format].BlockSizeX;
405
int WidthInBlocks = Align(GetLevelDim(format, width, miplevel), block) / block;
406
406
return WidthInBlocks;
409
t_s32 ImageSurface::GetLevelBlockHeight(BitmapFormat format, t_s32 height, t_s32 miplevel)
409
int ImageSurface::GetLevelBlockHeight(BitmapFormat format, int height, int miplevel)
411
t_s32 block = GPixelFormats[format].BlockSizeY;
412
t_s32 HeightInBlocks = Align(GetLevelDim(format, height, miplevel), block) / block;
411
int block = GPixelFormats[format].BlockSizeY;
412
int HeightInBlocks = Align(GetLevelDim(format, height, miplevel), block) / block;
413
413
return HeightInBlocks;
943
943
m_TotalMemorySize = 0;
946
for (t_s32 i = 0; i < m_NumMipmap; i++)
946
for (int i = 0; i < m_NumMipmap; i++)
948
t_s32 w = width >> i;
949
t_s32 h = height >> i;
950
950
m_MipSurfaceArray.push_back(new ImageSurface(format, w, h));
951
951
m_TotalMemorySize += m_MipSurfaceArray[i]->GetSize();
955
void NTextureData::AllocateCheckBoardTexture(t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth, t_s32 TileHeight)
955
void NTextureData::AllocateCheckBoardTexture(int width, int height, int NumMipmap, Color color0, Color color1, int TileWidth, int TileHeight)
957
957
Allocate(BITFMT_R8G8B8A8, width, height, NumMipmap);
959
for (t_s32 i = 0; i < m_NumMipmap; i++)
959
for (int i = 0; i < m_NumMipmap; i++)
961
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
962
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
961
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
962
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
963
963
MakeCheckBoardImage(*m_MipSurfaceArray[i], w, h, color0, color1, TileWidth, TileHeight);
967
void NTextureData::AllocateColorTexture(t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0)
967
void NTextureData::AllocateColorTexture(int width, int height, int NumMipmap, Color color0)
969
969
Allocate(BITFMT_R8G8B8A8, width, height, NumMipmap);
971
for (t_s32 i = 0; i < m_NumMipmap; i++)
971
for (int i = 0; i < m_NumMipmap; i++)
973
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
974
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
973
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
974
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
975
975
MakeCheckBoardImage(*m_MipSurfaceArray[i], w, h, color0, color0);
979
t_s32 NTextureData::GetNumMipmap() const
979
int NTextureData::GetNumMipmap() const
981
981
return m_MipSurfaceArray.size();
984
bool NTextureData::SetSurface(t_s32 MipLevel, const ImageSurface &targetsurface)
984
bool NTextureData::SetSurface(int MipLevel, const ImageSurface &targetsurface)
986
986
nuxAssert(MipLevel >= 0);
987
987
nuxAssert(MipLevel < m_NumMipmap);
1077
1077
m_TotalMemorySize = 0;
1079
for (t_s32 face = 0; face < 6; face++)
1079
for (int face = 0; face < 6; face++)
1081
for (t_s32 i = 0; i < m_NumMipmap; i++)
1081
for (int i = 0; i < m_NumMipmap; i++)
1083
t_s32 w = width >> i;
1084
t_s32 h = height >> i;
1084
int h = height >> i;
1085
1085
m_MipSurfaceArray[face].push_back(new ImageSurface(format, w, h));
1086
1086
m_TotalMemorySize += m_MipSurfaceArray[face][i]->GetSize();
1091
void NCubemapData::AllocateCheckBoardTexture(t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth, t_s32 TileHeight)
1091
void NCubemapData::AllocateCheckBoardTexture(int width, int height, int NumMipmap, Color color0, Color color1, int TileWidth, int TileHeight)
1093
1093
Allocate(BITFMT_R8G8B8A8, width, height, NumMipmap);
1095
for (t_s32 face = 0; face < 6; face++)
1095
for (int face = 0; face < 6; face++)
1097
for (t_s32 i = 0; i < m_NumMipmap; i++)
1097
for (int i = 0; i < m_NumMipmap; i++)
1099
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
1100
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
1099
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
1100
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
1101
1101
MakeCheckBoardImage(*m_MipSurfaceArray[face][i], w, h, color0, color1, TileWidth, TileHeight);
1106
void NCubemapData::AllocateColorTexture(t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0)
1106
void NCubemapData::AllocateColorTexture(int width, int height, int NumMipmap, Color color0)
1108
1108
Allocate(BITFMT_R8G8B8A8, width, height, NumMipmap);
1110
for (t_s32 face = 0; face < 6; face++)
1110
for (int face = 0; face < 6; face++)
1112
for (t_s32 i = 0; i < m_NumMipmap; i++)
1112
for (int i = 0; i < m_NumMipmap; i++)
1114
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
1115
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
1114
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), i);
1115
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), i);
1116
1116
MakeCheckBoardImage(*m_MipSurfaceArray[face][i], w, h, color0, color0);
1121
t_s32 NCubemapData::GetNumMipmap() const
1121
int NCubemapData::GetNumMipmap() const
1123
1123
return m_MipSurfaceArray[0].size();
1126
bool NCubemapData::SetSurface(t_s32 face, t_s32 MipLevel, const ImageSurface &targetsurface)
1126
bool NCubemapData::SetSurface(int face, int MipLevel, const ImageSurface &targetsurface)
1128
1128
nuxAssert(face >= 0);
1129
1129
nuxAssert(face < 6);
1230
1230
m_MipSurfaceArray = new std::vector<ImageSurface *>[m_NumMipmap];
1231
1231
m_TotalMemorySize = 0;
1233
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1233
for (int mip = 0; mip < m_NumMipmap; mip++)
1235
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(format, depth, mip); s++)
1235
for (int s = 0; s < ImageSurface::GetLevelDim(format, depth, mip); s++)
1237
t_s32 w = ImageSurface::GetLevelDim(format, width, mip);
1238
t_s32 h = ImageSurface::GetLevelDim(format, height, mip);
1237
int w = ImageSurface::GetLevelDim(format, width, mip);
1238
int h = ImageSurface::GetLevelDim(format, height, mip);
1239
1239
m_MipSurfaceArray[mip].push_back(new ImageSurface(format, w, h));
1240
1240
m_TotalMemorySize += m_MipSurfaceArray[mip][s]->GetSize();
1245
void NVolumeData::AllocateCheckBoardTexture(t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth, t_s32 TileHeight)
1245
void NVolumeData::AllocateCheckBoardTexture(int width, int height, int slice, int NumMipmap, Color color0, Color color1, int TileWidth, int TileHeight)
1247
1247
Allocate(BITFMT_R8G8B8A8, width, height, slice, NumMipmap);
1249
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1249
for (int mip = 0; mip < m_NumMipmap; mip++)
1251
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1251
for (int s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1253
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1254
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1253
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1254
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1255
1255
MakeCheckBoardImage(* (m_MipSurfaceArray[mip][s]), w, h, color0, color1, TileWidth, TileHeight);
1260
void NVolumeData::AllocateColorTexture(t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0)
1260
void NVolumeData::AllocateColorTexture(int width, int height, int slice, int NumMipmap, Color color0)
1262
1262
Allocate(BITFMT_R8G8B8A8, width, height, slice, NumMipmap);
1264
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1264
for (int mip = 0; mip < m_NumMipmap; mip++)
1266
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1266
for (int s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1268
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1269
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1268
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1269
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1270
1270
MakeCheckBoardImage(* (m_MipSurfaceArray[mip][s]), w, h, color0, color0);
1275
t_s32 NVolumeData::GetNumMipmap() const
1275
int NVolumeData::GetNumMipmap() const
1277
1277
return m_NumMipmap;
1280
bool NVolumeData::SetSurface(t_s32 Slice, t_s32 MipLevel, const ImageSurface &targetsurface)
1280
bool NVolumeData::SetSurface(int Slice, int MipLevel, const ImageSurface &targetsurface)
1282
1282
nuxAssert(Slice >= 0);
1283
1283
nuxAssert(Slice < m_Depth);
1395
1395
m_MipSurfaceArray = new std::vector<ImageSurface *>[m_NumMipmap];
1396
1396
m_TotalMemorySize = 0;
1398
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1398
for (int mip = 0; mip < m_NumMipmap; mip++)
1400
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(format, depth, mip); s++)
1400
for (int s = 0; s < ImageSurface::GetLevelDim(format, depth, mip); s++)
1402
t_s32 w = ImageSurface::GetLevelDim(format, width, mip);
1403
t_s32 h = ImageSurface::GetLevelDim(format, height, mip);
1402
int w = ImageSurface::GetLevelDim(format, width, mip);
1403
int h = ImageSurface::GetLevelDim(format, height, mip);
1404
1404
m_MipSurfaceArray[mip].push_back(new ImageSurface(format, w, h));
1405
1405
m_TotalMemorySize += m_MipSurfaceArray[mip][s]->GetSize();
1410
void NAnimatedTextureData::AllocateCheckBoardTexture(t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth, t_s32 TileHeight)
1410
void NAnimatedTextureData::AllocateCheckBoardTexture(int width, int height, int slice, int NumMipmap, Color color0, Color color1, int TileWidth, int TileHeight)
1412
1412
Allocate(BITFMT_R8G8B8A8, width, height, slice, NumMipmap);
1414
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1414
for (int mip = 0; mip < m_NumMipmap; mip++)
1416
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1416
for (int s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1418
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1419
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1418
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1419
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1420
1420
MakeCheckBoardImage(* (m_MipSurfaceArray[mip][s]), w, h, color0, color1, TileWidth, TileHeight);
1425
void NAnimatedTextureData::AllocateColorTexture(t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0)
1425
void NAnimatedTextureData::AllocateColorTexture(int width, int height, int slice, int NumMipmap, Color color0)
1427
1427
Allocate(BITFMT_R8G8B8A8, width, height, slice, NumMipmap);
1429
for (t_s32 mip = 0; mip < m_NumMipmap; mip++)
1429
for (int mip = 0; mip < m_NumMipmap; mip++)
1431
for (t_s32 s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1431
for (int s = 0; s < ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetDepth(), mip); s++)
1433
t_s32 w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1434
t_s32 h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1433
int w = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetWidth(), mip);
1434
int h = ImageSurface::GetLevelDim(BITFMT_R8G8B8A8, GetHeight(), mip);
1435
1435
MakeCheckBoardImage(* (m_MipSurfaceArray[mip][s]), w, h, color0, color0);
1440
t_s32 NAnimatedTextureData::GetNumMipmap() const
1440
int NAnimatedTextureData::GetNumMipmap() const
1442
1442
return m_NumMipmap;
1445
bool NAnimatedTextureData::SetSurface(t_s32 Slice, t_s32 MipLevel, const ImageSurface &targetsurface)
1445
bool NAnimatedTextureData::SetSurface(int Slice, int MipLevel, const ImageSurface &targetsurface)
1447
1447
nuxAssert(Slice >= 0);
1448
1448
nuxAssert(Slice < m_Depth);