GteTexture.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
11 #include <Graphics/GteTexture.h>
12 using namespace gte;
13 
14 
15 Texture::Texture(unsigned int numItems, DFType format,
16  unsigned int numDimensions, unsigned int dim0, unsigned int dim1,
17  unsigned int dim2, bool hasMipmaps, bool createStorage)
18  :
19  Resource(GetTotalElements(numItems, dim0, dim1, dim2, hasMipmaps),
20  DataFormat::GetNumBytesPerStruct(format), createStorage),
21  mNumItems(numItems),
22  mFormat(format),
23  mNumDimensions(numDimensions),
24  mNumLevels(1),
25  mLOffset(numItems),
26  mHasMipmaps(hasMipmaps),
27  mAutogenerateMipmaps(false)
28 {
29  mType = GT_TEXTURE;
30 
31  if (mNumDimensions < 1 || mNumDimensions > 3)
32  {
33  LogError("Invalid number of dimensions.");
34  mNumDimensions = 1;
35  }
36 
37  // Zero-out all the level information.
38  for (unsigned int level = 0; level < MAX_MIPMAP_LEVELS; ++level)
39  {
40  mLDimension[level][0] = 0;
41  mLDimension[level][1] = 0;
42  mLDimension[level][2] = 0;
43  mLNumBytes[level] = 0;
44  }
45 
46  for (unsigned int item = 0; item < mNumItems; ++item)
47  {
48  for (unsigned int level = 0; level < MAX_MIPMAP_LEVELS; ++level)
49  {
50  mLOffset[item][level] = 0;
51  }
52  }
53 
54  // The base level is always used.
55  mLDimension[0][0] = dim0;
56  mLDimension[0][1] = dim1;
57  mLDimension[0][2] = dim2;
58  mLNumBytes[0] = dim0*dim1*dim2*mElementSize;
59 
60  if (mHasMipmaps)
61  {
62  unsigned int log0 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim0));
63  unsigned int log1 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim1));
64  unsigned int log2 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim2));
65  mNumLevels = 1 + std::max(log0, std::max(log1, log2));
66  for (unsigned int level = 1; level < mNumLevels; ++level)
67  {
68  if (dim0 > 1) { dim0 >>= 1; }
69  if (dim1 > 1) { dim1 >>= 1; }
70  if (dim2 > 1) { dim2 >>= 1; }
71 
72  mLNumBytes[level] = dim0*dim1*dim2*mElementSize;
73  mLDimension[level][0] = dim0;
74  mLDimension[level][1] = dim1;
75  mLDimension[level][2] = dim2;
76  }
77 
78  unsigned int numBytes = 0;
79  for (unsigned int item = 0; item < mNumItems; ++item)
80  {
81  for (unsigned int level = 0; level < mNumLevels; ++level)
82  {
83  mLOffset[item][level] = numBytes;
84  numBytes += mLNumBytes[level];
85  }
86  }
87  }
88  else
89  {
90  for (unsigned int item = 1; item < mNumItems; ++item)
91  {
92  mLOffset[item][0] = item * mLNumBytes[0];
93  }
94  }
95 }
96 
97 unsigned int Texture::GetIndex(unsigned int item, unsigned int level) const
98 {
99  if (item < mNumItems && level < mNumLevels)
100  {
101  return mNumLevels * item + level;
102  }
103  else
104  {
105  LogError("Invalid input.");
106  return 0;
107  }
108 }
109 
111 {
112  Subresource sr;
113  if (index < GetNumSubresources())
114  {
115  sr.item = index / mNumLevels;
116  sr.level = index % mNumLevels;
117  sr.data = const_cast<char*>(GetDataFor(sr.item, sr.level));
118  sr.rowPitch = mLDimension[sr.level][0] * mElementSize;
119  sr.slicePitch = mLDimension[sr.level][1] * sr.rowPitch;
120  }
121  else
122  {
123  LogError("Invalid input.");
124  sr.item = 0;
125  sr.level = 0;
126  sr.data = nullptr;
127  sr.rowPitch = 0;
128  sr.slicePitch = 0;
129  }
130  return sr;
131 }
132 
134 {
135  if (mHasMipmaps)
136  {
137  mAutogenerateMipmaps = true;
138  //mUsage = SHADER_OUTPUT;
139  }
140 }
141 
142 unsigned int Texture::GetTotalElements(unsigned int numItems,
143  unsigned int dim0, unsigned int dim1, unsigned int dim2, bool hasMipmaps)
144 {
145  unsigned int numElementsPerItem = dim0 * dim1 * dim2;
146  if (hasMipmaps)
147  {
148  unsigned int log0 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim0));
149  unsigned int log1 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim1));
150  unsigned int log2 = Log2OfPowerOfTwo(RoundDownToPowerOfTwo(dim2));
151  unsigned int numLevels = 1 + std::max(log0, std::max(log1, log2));
152  for (unsigned int level = 1; level < numLevels; ++level)
153  {
154  if (dim0 > 1) { dim0 >>= 1; }
155  if (dim1 > 1) { dim1 >>= 1; }
156  if (dim2 > 1) { dim2 >>= 1; }
157 
158  numElementsPerItem += dim0*dim1*dim2;
159  }
160  }
161  return numItems*numElementsPerItem;
162 }
163 
std::array< unsigned int, MAX_MIPMAP_LEVELS > mLNumBytes
Definition: GteTexture.h:86
bool mHasMipmaps
Definition: GteTexture.h:88
unsigned int GetIndex(unsigned int item, unsigned int level) const
Definition: GteTexture.cpp:97
unsigned int mNumDimensions
Definition: GteTexture.h:83
bool mAutogenerateMipmaps
Definition: GteTexture.h:89
GTE_IMPEXP uint32_t RoundDownToPowerOfTwo(uint32_t value)
Texture(unsigned int numItems, DFType format, unsigned int numDimensions, unsigned int dim0, unsigned int dim1, unsigned int dim2, bool hasMipmaps, bool createStorage)
Definition: GteTexture.cpp:15
GLint level
Definition: glcorearb.h:103
GraphicsObjectType mType
std::vector< std::array< unsigned int, MAX_MIPMAP_LEVELS > > mLOffset
Definition: GteTexture.h:87
GT_TEXTURE
char const * GetDataFor(unsigned int item, unsigned int level) const
Definition: GteTexture.h:147
unsigned int mElementSize
Definition: GteResource.h:95
#define LogError(message)
Definition: GteLogger.h:92
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:103
static unsigned int GetTotalElements(unsigned int numItems, unsigned int dim0, unsigned int dim1, unsigned int dim2, bool hasMipmaps)
Definition: GteTexture.cpp:142
Subresource GetSubresource(unsigned int index) const
Definition: GteTexture.cpp:110
GTE_IMPEXP uint32_t Log2OfPowerOfTwo(uint32_t powerOfTwo)
Definition: GteBitHacks.cpp:41
unsigned int slicePitch
Definition: GteTexture.h:42
GLuint index
Definition: glcorearb.h:781
void AutogenerateMipmaps()
Definition: GteTexture.cpp:133
unsigned int mNumItems
Definition: GteTexture.h:81
unsigned int mNumLevels
Definition: GteTexture.h:84
unsigned int GetNumSubresources() const
Definition: GteTexture.h:170
std::array< std::array< unsigned int, 3 >, MAX_MIPMAP_LEVELS > mLDimension
Definition: GteTexture.h:85


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01