GteGL4Buffer.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 {
16 }
17 
19  :
20  GL4Resource(buffer),
21  mType(type)
22 {
24 
25  Resource::Usage usage = buffer->GetUsage();
26  if (usage == Resource::IMMUTABLE)
27  {
29  }
30  else if (usage == Resource::DYNAMIC_UPDATE)
31  {
33  }
34  else // usage == Resource::SHADER_OUTPUT
35  {
36  // TODO: In GLSL, is it possible to write to a buffer other than a
37  // vertex buffer?
38  if (mType == GL_ARRAY_BUFFER)
39  {
41  }
42  else if (mType == GL_SHADER_STORAGE_BUFFER)
43  {
45  }
46  else
47  {
48  LogWarning("Can this buffer type be a shader output?");
50  }
51  }
52 }
53 
55 {
57 
58  // Access the buffer.
59  auto buffer = GetBuffer();
60 
61  // Create and initialize a buffer.
62  glBufferData(mType, buffer->GetNumBytes(), buffer->GetData(), mUsage);
63 
64  glBindBuffer(mType, 0);
65 }
66 
68 {
69  Buffer* buffer = GetBuffer();
70  if (buffer->GetUsage() != Resource::DYNAMIC_UPDATE)
71  {
72  LogWarning("Buffer usage is not DYNAMIC_UPDATE.");
73  return false;
74  }
75 
76  GLuint numActiveBytes = buffer->GetNumActiveBytes();
77  if (numActiveBytes > 0)
78  {
79  // Copy from CPU memory to GPU memory.
80  GLintptr offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
81  char const* source = buffer->GetData() + offsetInBytes;
83  glBufferSubData(mType, offsetInBytes, numActiveBytes, source);
84  glBindBuffer(mType, 0);
85  }
86  else
87  {
88  LogInformation("Buffer has zero active bytes.");
89  }
90  return true;
91 }
92 
94 {
96  {
97  return false;
98  }
99 
100  Buffer* buffer = GetBuffer();
101  GLuint numActiveBytes = buffer->GetNumActiveBytes();
102  if (numActiveBytes > 0)
103  {
104  // Copy from CPU memory to GPU memory.
105  GLintptr offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
106  char const* source = buffer->GetData() + offsetInBytes;
108  glBufferSubData(mType, offsetInBytes, numActiveBytes, source);
109  glBindBuffer(mType, 0);
110  }
111  else
112  {
113  LogInformation("Buffer has zero active bytes.");
114  }
115  return true;
116 }
117 
119 {
121  {
122  return false;
123  }
124 
125  Buffer* buffer = GetBuffer();
126  GLuint numActiveBytes = buffer->GetNumActiveBytes();
127  if (numActiveBytes > 0)
128  {
129  // Copy from GPU memory to CPU memory.
130  GLintptr offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
131  char* target = buffer->GetData() + offsetInBytes;
133  glGetBufferSubData(mType, offsetInBytes, numActiveBytes, target);
134  glBindBuffer(mType, 0);
135  }
136  else
137  {
138  LogInformation("Buffer has zero active bytes.");
139  }
140  return true;
141 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
virtual ~GL4Buffer()
unsigned int GLuint
Definition: glcorearb.h:89
void APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
Definition: GteOpenGL.cpp:1547
unsigned int GetOffset() const
Definition: GteResource.h:173
IMMUTABLE
Definition: GteResource.h:42
#define GL_SHADER_STORAGE_BUFFER
Definition: glcorearb.h:2468
#define LogInformation(message)
Definition: GteLogger.h:98
Usage GetUsage() const
Definition: GteResource.h:126
#define GL_DYNAMIC_DRAW
Definition: glcorearb.h:642
#define GL_STREAM_DRAW
Definition: glcorearb.h:636
#define GL_ARRAY_BUFFER
Definition: glcorearb.h:625
bool PreparedForCopy(GLenum access) const
GLenum target
Definition: glcorearb.h:1662
void APIENTRY glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, void *data)
Definition: GteOpenGL.cpp:1615
Buffer * GetBuffer() const
Definition: GteGL4Buffer.h:44
unsigned int GetElementSize() const
Definition: GteResource.h:111
#define GL_STATIC_DRAW
Definition: glcorearb.h:639
virtual bool CopyCpuToGpu()
void APIENTRY glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage)
Definition: GteOpenGL.cpp:1589
unsigned int GLenum
Definition: glcorearb.h:83
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:798
void APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
Definition: GteOpenGL.cpp:1560
GL4Buffer(Buffer const *buffer, GLenum type)
#define LogWarning(message)
Definition: GteLogger.h:95
char const * GetData() const
Definition: GteResource.h:151
void APIENTRY glBindBuffer(GLenum target, GLuint buffer)
Definition: GteOpenGL.cpp:1534
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
#define GL_READ_ONLY
Definition: glcorearb.h:630
void APIENTRY glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data)
Definition: GteOpenGL.cpp:1602
GLuint buffer
Definition: glcorearb.h:655
virtual bool Update()
#define GL_WRITE_ONLY
Definition: glcorearb.h:631
unsigned int GetNumActiveBytes() const
Definition: GteResource.h:183
ptrdiff_t GLintptr
Definition: glcorearb.h:618
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:103
virtual bool CopyGpuToCpu()
virtual void Initialize()


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59