GteGL4TextureSingle.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  if (texture->GetData())
52  {
53  // Initialize with first mipmap level and then generate mipmaps.
55  {
56  auto data = texture->GetDataFor(0);
57  if (data)
58  {
61  }
62  }
63  // Initialize with each mipmap level.
64  else
65  {
66  for (int level = 0; level < mNumLevels; ++level)
67  {
68  auto data = texture->GetDataFor(level);
69  if (data)
70  {
72  }
73  }
74  }
75  }
76 
77  glBindTexture(mTarget, prevBinding);
78 }
79 
81 {
82  auto texture = GetTexture();
83 
84  // Only update the level 0 texture and then generate the mipmaps from there.
86  {
87  if (!Update(0))
88  {
89  return false;
90  }
92  }
93 
94  // No auto gen mipmaps, so need to copy all of them to GPU.
95  else
96  {
97  auto const numLevels = texture->GetNumLevels();
98  for (unsigned int level = 0; level < numLevels; ++level)
99  {
100  if (!Update(level))
101  {
102  return false;
103  }
104  }
105  }
106 
107  return true;
108 }
109 
111 {
112  auto texture = GetTexture();
113 
114  // Only update the level 0 texture and then generate the mipmaps from there.
116  {
117  if (!CopyCpuToGpu(0))
118  {
119  return false;
120  }
121  GenerateMipmaps();
122  }
123 
124  // No auto gen mipmaps, so need to copy all of them to GPU.
125  else
126  {
127  auto const numLevels = texture->GetNumLevels();
128  for (unsigned int level = 0; level < numLevels; ++level)
129  {
130  if (!CopyCpuToGpu(level))
131  {
132  return false;
133  }
134  }
135  }
136 
137  return true;
138 }
139 
141 {
142  auto texture = GetTexture();
143  auto const numLevels = texture->GetNumLevels();
144  for (unsigned int level = 0; level < numLevels; ++level)
145  {
146  if (!CopyGpuToCpu(level))
147  {
148  return false;
149  }
150  }
151 
152  return true;
153 }
154 
155 bool GL4TextureSingle::Update(unsigned int level)
156 {
157  auto texture = GetTexture();
158  if (texture->GetUsage() != Resource::DYNAMIC_UPDATE)
159  {
160  LogWarning("Texture usage is not DYNAMIC_UPDATE.");
161  return false;
162  }
163 
164  return DoCopyCpuToGpu(level);
165 }
166 
168 {
170  {
171  return false;
172  }
173 
174  return DoCopyCpuToGpu(level);
175 }
176 
178 {
180  {
181  return false;
182  }
183 
184  auto texture = GetTexture();
185 
186  // Make sure level is valid.
187  auto const numLevels = texture->GetNumLevels();
188  if (level >= numLevels)
189  {
190  LogWarning("Level for Texture is out of range");
191  return false;
192  }
193 
194  auto pixBuffer = mLevelPixelPackBuffer[level];
195  if (0 == pixBuffer)
196  {
197  LogWarning("Staging buffer not defined for level=" + level);
198  return false;
199  }
200 
201  auto data = texture->GetDataFor(level);
202  auto numBytes = texture->GetNumBytesFor(level);
203  if ((nullptr == data) || (0 == numBytes))
204  {
205  LogWarning("No target data for Texture level=" + level);
206  return false;
207  }
208 
209  auto const target = GetTarget();
211 
216 
217  glBindTexture(target, 0);
218 
219  return true;
220 }
221 
223 {
224  auto texture = GetTexture();
225 
226  // Cannot update automatically generated mipmaps in GPU.
227  if (CanAutoGenerateMipmaps() && (level > 0))
228  {
229  LogWarning("Cannot update automatically generated mipmaps in GPU");
230  return false;
231  }
232 
233  // Make sure level is valid.
234  auto const numLevels = texture->GetNumLevels();
235  if (level >= numLevels)
236  {
237  LogWarning("Level for Texture is out of range");
238  return false;
239  }
240 
241  auto data = texture->GetDataFor(level);
242  auto numBytes = texture->GetNumBytesFor(level);
243  if ((nullptr == data) || (0 == numBytes))
244  {
245  LogWarning("No source data for Texture level=" + level);
246  return false;
247  }
248 
249  auto const target = GetTarget();
251 
252  // Use staging buffer if present.
253  auto pixBuffer = mLevelPixelUnpackBuffer[level];
254  if (0 != pixBuffer)
255  {
258  LoadTextureLevel(level, 0);
260  }
261  else
262  {
263  LoadTextureLevel(level, data);
264  }
265 
266  glBindTexture(target, 0);
267 
268  return true;
269 }
270 
272 {
274  {
275  // Save current binding for this texture target in order to restore it when done
276  // since the gl texture object is needed to be bound to this texture target for
277  // the operations to follow.
278  GLint prevBinding;
279  glGetIntegerv(mTargetBinding, &prevBinding);
281 
282  // Generate the mipmaps.
283  // All of this binding save and restore is not necessary in OpenGL 4.5
284  // where glGenerateTextureMipamap(mGLHandle) can simply be used.
286 
287  glBindTexture(mTarget, prevBinding);
288 
289  return true;
290  }
291  return false;
292 }
293 
295 {
296  auto texture = GetTexture();
297  auto const copyType = texture->GetCopyType();
298 
299  auto const createPixelUnpackBuffers =
300  (copyType == Resource::COPY_CPU_TO_STAGING) ||
301  (copyType == Resource::COPY_BIDIRECTIONAL);
302 
303  auto const createPixelPackBuffers =
304  (copyType == Resource::COPY_STAGING_TO_CPU) ||
305  (copyType == Resource::COPY_BIDIRECTIONAL);
306 
307  // TODO
308  // Determine frequency and nature of usage for this staging
309  // buffer when created by calling glBufferData.
311 
312  if (createPixelUnpackBuffers)
313  {
314  for (int level = 0; level < mNumLevels; ++level)
315  {
316  auto& pixBuffer = mLevelPixelUnpackBuffer[level];
317  if (0 == pixBuffer)
318  {
319  auto numBytes = texture->GetNumBytesFor(level);
320 
321  // Create pixel buffer for staging.
322  glGenBuffers(1, &pixBuffer);
324  glBufferData(GL_PIXEL_UNPACK_BUFFER, numBytes, 0, usage);
326  }
327  }
328  }
329 
330  if (createPixelPackBuffers)
331  {
332  for (int level = 0; level < mNumLevels; ++level)
333  {
334  auto& pixBuffer = mLevelPixelPackBuffer[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_PACK_BUFFER, numBytes, 0, usage);
344  }
345  }
346  }
347 }
void APIENTRY glGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, void *pixels)
Definition: GteOpenGL.cpp:661
DYNAMIC_UPDATE
Definition: GteResource.h:42
virtual bool CanAutoGenerateMipmaps() const =0
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 mLevelPixelPackBuffer[Texture::MAX_MIPMAP_LEVELS]
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_DYNAMIC_DRAW
Definition: glcorearb.h:642
#define GL_PIXEL_PACK_BUFFER
Definition: glcorearb.h:967
bool PreparedForCopy(GLenum access) const
GL4TextureSingle(TextureSingle const *gtTexture, GLenum target, GLenum targetBinding)
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
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
#define GL_TEXTURE_MAX_LEVEL
Definition: glcorearb.h:464
virtual bool CopyGpuToCpu() override
void APIENTRY glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage)
Definition: GteOpenGL.cpp:1589
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
TextureSingle * GetTexture() const
virtual void LoadTextureLevel(unsigned int level, void const *data)=0
virtual bool Update() override
#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
#define GL_READ_ONLY
Definition: glcorearb.h:630
void APIENTRY glGetIntegerv(GLenum pname, GLint *data)
Definition: GteOpenGL.cpp:632
GLuint mLevelPixelUnpackBuffer[Texture::MAX_MIPMAP_LEVELS]
GLuint mExternalFormat
Definition: GteGL4Texture.h:38
void APIENTRY glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data)
Definition: GteOpenGL.cpp:1602
bool DoCopyCpuToGpu(unsigned int level)
#define GL_WRITE_ONLY
Definition: glcorearb.h:631
void APIENTRY glGenerateMipmap(GLenum target)
Definition: GteOpenGL.cpp:4302
virtual bool CopyCpuToGpu() override


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