GteGL4TextureArray.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 using namespace gte;
12 
14 {
15  for (int level = 0; level < mNumLevels; ++level)
16  {
19  }
20 }
21 
23  :
24  GL4Texture(gtTexture, target, targetBinding)
25 {
26  // Initially no staging buffers.
27  std::fill(std::begin(mLevelPixelUnpackBuffer), std::end(mLevelPixelUnpackBuffer), 0);
28  std::fill(std::begin(mLevelPixelPackBuffer), std::end(mLevelPixelPackBuffer), 0);
29 }
30 
32 {
33  // Save current binding for this texture target in order to restore it when done
34  // since the gl texture object is needed to be bound to this texture target for
35  // the operations to follow.
36  GLint prevBinding;
37  glGetIntegerv(mTargetBinding, &prevBinding);
39 
40  // The default is 4-byte alignment. This allows byte alignment when data
41  // from user buffers into textures and vice versa.
44 
45  // Set the range of levels.
48 
49  // Initialize with data?
50  auto texture = GetTexture();
51  auto const numItems = texture->GetNumItems();
52  if (texture->GetData())
53  {
54  // Initialize with first mipmap level and then generate mipmaps.
56  {
57  for (unsigned int item = 0; item < numItems; ++item)
58  {
59  auto data = texture->GetDataFor(item, 0);
60  if (data)
61  {
62  LoadTextureLevel(item, 0, data);
63  }
64  }
66  }
67  // Initialize with each mipmap level.
68  else
69  {
70  for (unsigned int item = 0; item < numItems; ++item)
71  {
72  for (int level = 0; level < mNumLevels; ++level)
73  {
74  auto data = texture->GetDataFor(item, level);
75  if (data)
76  {
77  LoadTextureLevel(item, level, data);
78  }
79  }
80  }
81  }
82  }
83 
84  glBindTexture(mTarget, prevBinding);
85 }
86 
88 {
89  auto texture = GetTexture();
90  auto const numItems = texture->GetNumItems();
91 
92  // Only update the level 0 texture and then generate the mipmaps from there.
94  {
95  for (unsigned int item = 0; item < numItems; ++item)
96  {
97  if (!Update(item, 0))
98  {
99  return false;
100  }
101  }
102  GenerateMipmaps();
103  }
104 
105  // No auto gen mipmaps, so need to copy all of them to GPU.
106  else
107  {
108  auto const numLevels = texture->GetNumLevels();
109  for (unsigned int item = 0; item < numItems; ++item)
110  {
111  for (unsigned int level = 0; level < numLevels; ++level)
112  {
113  if (!Update(item, level))
114  {
115  return false;
116  }
117  }
118  }
119  }
120 
121  return true;
122 }
123 
125 {
126  auto texture = GetTexture();
127  auto const numItems = texture->GetNumItems();
128 
129  // Only update the level 0 texture and then generate the mipmaps from there.
131  {
132  for (unsigned int item = 0; item < numItems; ++item)
133  {
134  if (!CopyCpuToGpu(item, 0))
135  {
136  return false;
137  }
138  }
139  GenerateMipmaps();
140  }
141 
142  // No auto gen mipmaps, so need to copy all of them to GPU.
143  else
144  {
145  auto const numLevels = texture->GetNumLevels();
146  for (unsigned int item = 0; item < numItems; ++item)
147  {
148  for (unsigned int level = 0; level < numLevels; ++level)
149  {
150  if (!CopyCpuToGpu(item, level))
151  {
152  return false;
153  }
154  }
155  }
156  }
157 
158  return true;
159 }
160 
162 {
163  auto texture = GetTexture();
164  auto const numItems = texture->GetNumItems();
165  auto const numLevels = texture->GetNumLevels();
166  for (unsigned int item = 0; item < numItems; ++item)
167  {
168  for (unsigned int level = 0; level < numLevels; ++level)
169  {
170  if (!CopyGpuToCpu(item, level))
171  {
172  return false;
173  }
174  }
175  }
176 
177  return true;
178 }
179 
180 bool GL4TextureArray::Update(unsigned int item, unsigned int level)
181 {
182  auto texture = GetTexture();
183  if (texture->GetUsage() != Resource::DYNAMIC_UPDATE)
184  {
185  LogWarning("Texture usage is not DYNAMIC_UPDATE.");
186  return false;
187  }
188 
189  return DoCopyCpuToGpu(item, level);
190 }
191 
192 bool GL4TextureArray::CopyCpuToGpu(unsigned int item, unsigned int level)
193 {
195  {
196  return false;
197  }
198 
199  return DoCopyCpuToGpu(item, level);
200 }
201 
202 bool GL4TextureArray::CopyGpuToCpu(unsigned int item, unsigned int level)
203 {
205  {
206  return false;
207  }
208 
209  auto texture = GetTexture();
210 
211  // Make sure item is valid.
212  auto const numItems = texture->GetNumItems();
213  if (item >= numItems)
214  {
215  LogWarning("Item for Texture is out of range");
216  return false;
217  }
218 
219  // Make sure level is valid.
220  auto const numLevels = texture->GetNumLevels();
221  if (level >= numLevels)
222  {
223  LogWarning("Level for Texture is out of range");
224  return false;
225  }
226 
227  auto pixBuffer = mLevelPixelPackBuffer[level];
228  if (0 == pixBuffer)
229  {
230  LogWarning("Staging buffer not defined for level=" + level);
231  return false;
232  }
233 
234  auto data = texture->GetDataFor(item, level);
235  auto numBytes = texture->GetNumBytesFor(level);
236  if ((nullptr == data) || (0 == numBytes))
237  {
238  LogWarning("No target data for Texture level=" + level);
239  return false;
240  }
241 
242  auto const target = GetTarget();
244 
249 
250  glBindTexture(target, 0);
251 
252  return true;
253 }
254 
255 bool GL4TextureArray::DoCopyCpuToGpu(unsigned int item, unsigned int level)
256 {
257  auto texture = GetTexture();
258 
259  // Cannot update automatically generated mipmaps in GPU.
260  if (CanAutoGenerateMipmaps() && (level > 0))
261  {
262  LogWarning("Cannot update automatically generated mipmaps in GPU");
263  return false;
264  }
265 
266  // Make sure item is valid.
267  auto const numItems = texture->GetNumItems();
268  if (item >= numItems)
269  {
270  LogWarning("Item for TextureArray is out of range");
271  return false;
272  }
273 
274  // Make sure level is valid.
275  auto const numLevels = texture->GetNumLevels();
276  if (level >= numLevels)
277  {
278  LogWarning("Level for TextureArray is out of range");
279  return false;
280  }
281 
282  auto data = texture->GetDataFor(item, level);
283  auto numBytes = texture->GetNumBytesFor(level);
284  if ((nullptr == data) || (0 == numBytes))
285  {
286  LogWarning("No source data for TextureArray level=" + level);
287  return false;
288  }
289 
290  auto const target = GetTarget();
292 
293  // Use staging buffer if present.
294  auto pixBuffer = mLevelPixelUnpackBuffer[level];
295  if (0 != pixBuffer)
296  {
299  LoadTextureLevel(item, level, 0);
301  }
302  else
303  {
304  LoadTextureLevel(item, level, data);
305  }
306 
307  glBindTexture(target, 0);
308 
309  return true;
310 }
311 
313 {
314  auto texture = GetTexture();
315  auto const copyType = texture->GetCopyType();
316 
317  auto const createPixelUnpackBuffers =
318  (copyType == Resource::COPY_CPU_TO_STAGING) ||
319  (copyType == Resource::COPY_BIDIRECTIONAL);
320 
321  auto const createPixelPackBuffers =
322  (copyType == Resource::COPY_STAGING_TO_CPU) ||
323  (copyType == Resource::COPY_BIDIRECTIONAL);
324 
325  // TODO
326  // Determine frequency and nature of usage for this staging
327  // buffer when created by calling glBufferData.
329 
330  if (createPixelUnpackBuffers)
331  {
332  for (int level = 0; level < mNumLevels; ++level)
333  {
334  auto& pixBuffer = mLevelPixelUnpackBuffer[level];
335  if (0 == pixBuffer)
336  {
337  auto numBytes = texture->GetNumBytesFor(level);
338 
339  // Create pixel buffer for staging.
340  glGenBuffers(1, &pixBuffer);
342  glBufferData(GL_PIXEL_UNPACK_BUFFER, numBytes, 0, usage);
344  }
345  }
346  }
347 
348  if (createPixelPackBuffers)
349  {
350  for (int level = 0; level < mNumLevels; ++level)
351  {
352  auto& pixBuffer = mLevelPixelPackBuffer[level];
353  if (0 == pixBuffer)
354  {
355  auto numBytes = texture->GetNumBytesFor(level);
356 
357  // Create pixel buffer for staging.
358  glGenBuffers(1, &pixBuffer);
360  glBufferData(GL_PIXEL_PACK_BUFFER, numBytes, 0, usage);
362  }
363  }
364  }
365 }
366 
368 {
370  {
371  // Save current binding for this texture target in order to restore it when done
372  // since the gl texture object is needed to be bound to this texture target for
373  // the operations to follow.
374  GLint prevBinding;
375  glGetIntegerv(mTargetBinding, &prevBinding);
377 
378  // Generate the mipmaps.
379  // All of this binding save and restore is not necessary in OpenGL 4.5
380  // where glGenerateTextureMipamap(mGLHandle) can simply be used.
382 
383  glBindTexture(mTarget, prevBinding);
384 
385  return true;
386  }
387  return false;
388 }
389 
390 
392 {
393  GL_TEXTURE_CUBE_MAP_POSITIVE_X, // CubeFacePositiveX
394  GL_TEXTURE_CUBE_MAP_NEGATIVE_X, // CubeFaceNegativeX
395  GL_TEXTURE_CUBE_MAP_POSITIVE_Y, // CubeFacePositiveY
396  GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, // CubeFaceNegativeY
397  GL_TEXTURE_CUBE_MAP_POSITIVE_Z, // CubeFacePositiveZ
398  GL_TEXTURE_CUBE_MAP_NEGATIVE_Z // CubeFaceNegativeZ
399 };
void APIENTRY glGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, void *pixels)
Definition: GteOpenGL.cpp:661
DYNAMIC_UPDATE
Definition: GteResource.h:42
int GLint
Definition: glcorearb.h:85
void APIENTRY glTexParameteri(GLenum target, GLenum pname, GLint param)
Definition: GteOpenGL.cpp:253
void APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
Definition: GteOpenGL.cpp:1547
COPY_STAGING_TO_CPU
Definition: GteResource.h:55
GLuint mLevelPixelUnpackBuffer[Texture::MAX_MIPMAP_LEVELS]
virtual bool CopyCpuToGpu() override
GLenum GetTarget() const
Definition: GteGL4Texture.h:57
#define GL_PIXEL_UNPACK_BUFFER
Definition: glcorearb.h:968
GLuint mExternalType
Definition: GteGL4Texture.h:39
GLint level
Definition: glcorearb.h:103
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
Definition: glcorearb.h:532
#define GL_DYNAMIC_DRAW
Definition: glcorearb.h:642
bool DoCopyCpuToGpu(unsigned int item, unsigned int level)
static GLenum const msCubeFaceTarget[6]
#define GL_PIXEL_PACK_BUFFER
Definition: glcorearb.h:967
bool PreparedForCopy(GLenum access) const
GLenum target
Definition: glcorearb.h:1662
void APIENTRY glPixelStorei(GLenum pname, GLint param)
Definition: GteOpenGL.cpp:539
#define GL_PACK_ALIGNMENT
Definition: glcorearb.h:299
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z
Definition: glcorearb.h:531
void APIENTRY glBindTexture(GLenum target, GLuint texture)
Definition: GteOpenGL.cpp:967
void APIENTRY glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, void *data)
Definition: GteOpenGL.cpp:1615
GLenum mTargetBinding
Definition: GteGL4Texture.h:33
#define GL_TEXTURE_BASE_LEVEL
Definition: glcorearb.h:463
virtual bool CopyGpuToCpu() override
#define GL_TEXTURE_MAX_LEVEL
Definition: glcorearb.h:464
void APIENTRY glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage)
Definition: GteOpenGL.cpp:1589
GLuint mLevelPixelPackBuffer[Texture::MAX_MIPMAP_LEVELS]
unsigned int GLenum
Definition: glcorearb.h:83
#define GL_UNPACK_ALIGNMENT
Definition: glcorearb.h:293
GLuint GLuint end
Definition: glcorearb.h:470
GLboolean * data
Definition: glcorearb.h:126
void APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
Definition: GteOpenGL.cpp:1560
GLuint texture
Definition: glcorearb.h:410
virtual void LoadTextureLevel(unsigned int item, unsigned int level, void const *data)=0
#define LogWarning(message)
Definition: GteLogger.h:95
void APIENTRY glBindBuffer(GLenum target, GLuint buffer)
Definition: GteOpenGL.cpp:1534
COPY_CPU_TO_STAGING
Definition: GteResource.h:55
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
GL4TextureArray(TextureArray const *gtTexture, GLenum target, GLenum targetBinding)
#define GL_READ_ONLY
Definition: glcorearb.h:630
void APIENTRY glGetIntegerv(GLenum pname, GLint *data)
Definition: GteOpenGL.cpp:632
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
Definition: glcorearb.h:530
virtual bool Update() override
TextureArray * GetTexture() const
GLuint mExternalFormat
Definition: GteGL4Texture.h:38
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X
Definition: glcorearb.h:527
void APIENTRY glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data)
Definition: GteOpenGL.cpp:1602
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X
Definition: glcorearb.h:528
virtual bool GenerateMipmaps()
#define GL_WRITE_ONLY
Definition: glcorearb.h:631
virtual bool CanAutoGenerateMipmaps() const =0
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y
Definition: glcorearb.h:529
void APIENTRY glGenerateMipmap(GLenum target)
Definition: GteOpenGL.cpp:4302


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