Viskores  1.0
BufferState.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_interop_BufferState_h
19 #define viskores_interop_BufferState_h
20 
21 //gl headers needs to be buffer anything to do with buffer's
24 
26 
27 #include <memory>
28 
29 namespace viskores
30 {
31 namespace interop
32 {
33 
34 namespace internal
35 {
36 
38 
45 class TransferResource
46 {
47 public:
48  virtual ~TransferResource() {}
49 };
50 
52 }
53 
65 {
66 public:
68  BufferState(GLuint& gLHandle)
69  : OpenGLHandle(&gLHandle)
70  , BufferType(GL_INVALID_VALUE)
72  , CapacityOfBuffer(0)
73  , DefaultGLHandle(0)
74  , Resource()
75  {
76  }
77 
79  BufferState(GLuint& gLHandle, GLenum type)
80  : OpenGLHandle(&gLHandle)
81  , BufferType(type)
83  , CapacityOfBuffer(0)
84  , DefaultGLHandle(0)
85  , Resource()
86  {
87  }
88 
90  : OpenGLHandle(nullptr)
91  , BufferType(GL_INVALID_VALUE)
93  , CapacityOfBuffer(0)
94  , DefaultGLHandle(0)
95  , Resource()
96  {
97  this->OpenGLHandle = &this->DefaultGLHandle;
98  }
99 
101  {
102  //don't delete this as it points to user memory, or stack allocated
103  //memory inside this object instance
104  this->OpenGLHandle = nullptr;
105  }
106 
109  GLuint* GetHandle() const { return this->OpenGLHandle; }
110 
113  bool HasType() const { return this->BufferType != GL_INVALID_VALUE; }
114 
118  GLenum GetType() const { return this->BufferType; }
119 
122  void SetType(GLenum type) { this->BufferType = type; }
123 
130  template <typename T>
132  {
133  this->BufferType = viskores::interop::internal::BufferTypePicker(t);
134  }
135 
140  viskores::Int64 GetSize() const { return this->SizeOfActiveSection; }
141 
142  //Set the size of buffer in bytes
143  //This will always needs to be <= the capacity of the buffer
144  //Note: This call should only be used internally by viskores
145  void SetSize(viskores::Int64 size) { this->SizeOfActiveSection = size; }
146 
154  viskores::Int64 GetCapacity() const { return this->CapacityOfBuffer; }
155 
156  // Helper function to compute when we should resize the capacity of the
157  // buffer
158  bool ShouldRealloc(viskores::Int64 desiredSize) const
159  {
160  const bool haveNotEnoughRoom = this->GetCapacity() < desiredSize;
161  const bool haveTooMuchRoom = this->GetCapacity() > (desiredSize * 2);
162  return (haveNotEnoughRoom || haveTooMuchRoom);
163  }
164 
165  //Set the capacity of buffer in bytes
166  //The capacity of a buffer can be larger than the active size of buffer
167  //Note: This call should only be used internally by viskores
168  void SetCapacity(viskores::Int64 capacity) { this->CapacityOfBuffer = capacity; }
169 
170  //Note: This call should only be used internally by viskores
171  viskores::interop::internal::TransferResource* GetResource() { return this->Resource.get(); }
172 
173  //Note: This call should only be used internally by viskores
174  void SetResource(viskores::interop::internal::TransferResource* resource)
175  {
176  this->Resource.reset(resource);
177  }
178 
179 private:
180  // BufferState doesn't support copy or move semantics
181  BufferState(const BufferState&) = delete;
182  void operator=(const BufferState&) = delete;
183 
184  GLuint* OpenGLHandle;
185  GLenum BufferType;
186  viskores::Int64 SizeOfActiveSection; //must be Int64 as size can be over 2billion
187  viskores::Int64 CapacityOfBuffer; //must be Int64 as size can be over 2billion
189  std::unique_ptr<viskores::interop::internal::TransferResource> Resource;
190 };
191 }
192 }
193 
194 #endif //viskores_interop_BufferState_h
viskores::interop::BufferState::SetSize
void SetSize(viskores::Int64 size)
Definition: BufferState.h:145
viskores::interop::BufferState::DefaultGLHandle
GLuint DefaultGLHandle
Definition: BufferState.h:188
viskores::interop::BufferState::GetHandle
GLuint * GetHandle() const
get the OpenGL buffer handle
Definition: BufferState.h:109
viskores::interop::BufferState::GetResource
viskores::interop::internal::TransferResource * GetResource()
Definition: BufferState.h:171
viskores::interop::BufferState::GetType
GLenum GetType() const
return what OpenGL buffer type we are bound to
Definition: BufferState.h:118
viskores::interop::BufferState::Resource
std::unique_ptr< viskores::interop::internal::TransferResource > Resource
Definition: BufferState.h:189
OpenGLHeaders.h
viskores::interop::BufferState::operator=
void operator=(const BufferState &)=delete
BufferTypePicker.h
viskores::interop::BufferState::BufferState
BufferState()
Definition: BufferState.h:89
viskores::interop::BufferState::BufferType
GLenum BufferType
Definition: BufferState.h:185
viskores::Int64
signed long long Int64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:212
ExportMacros.h
viskores::interop::BufferState::SetType
void SetType(GLenum type)
Set what type of OpenGL buffer type we should bind as.
Definition: BufferState.h:122
viskores::interop::BufferState::DeduceAndSetType
void DeduceAndSetType(T t)
deduce the buffer type from the template value type that was passed in, and set that as our type
Definition: BufferState.h:131
viskores::interop::BufferState::GetCapacity
viskores::Int64 GetCapacity() const
Get the capacity of the buffer in bytes.
Definition: BufferState.h:154
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::interop::BufferState::OpenGLHandle
GLuint * OpenGLHandle
Definition: BufferState.h:184
viskores::interop::BufferState
Manages the state for transferring an ArrayHandle to opengl.
Definition: BufferState.h:64
viskores::interop::BufferState::SetCapacity
void SetCapacity(viskores::Int64 capacity)
Definition: BufferState.h:168
viskores::interop::BufferState::BufferState
BufferState(GLuint &gLHandle)
Construct a BufferState using an existing GLHandle.
Definition: BufferState.h:68
viskores::interop::BufferState::HasType
bool HasType() const
return if this buffer has a valid OpenGL buffer type
Definition: BufferState.h:113
VISKORES_SILENCE_WEAK_VTABLE_WARNING_END
#define VISKORES_SILENCE_WEAK_VTABLE_WARNING_END
Definition: ExportMacros.h:128
VISKORES_SILENCE_WEAK_VTABLE_WARNING_START
#define VISKORES_SILENCE_WEAK_VTABLE_WARNING_START
Definition: ExportMacros.h:127
viskores::interop::BufferState::BufferState
BufferState(GLuint &gLHandle, GLenum type)
Construct a BufferState using an existing GLHandle and type.
Definition: BufferState.h:79
viskores::interop::BufferState::SizeOfActiveSection
viskores::Int64 SizeOfActiveSection
Definition: BufferState.h:186
viskores::interop::BufferState::SetResource
void SetResource(viskores::interop::internal::TransferResource *resource)
Definition: BufferState.h:174
viskores::interop::BufferState::ShouldRealloc
bool ShouldRealloc(viskores::Int64 desiredSize) const
Definition: BufferState.h:158
viskores::interop::BufferState::CapacityOfBuffer
viskores::Int64 CapacityOfBuffer
Definition: BufferState.h:187
viskores::interop::BufferState::~BufferState
~BufferState()
Definition: BufferState.h:100
viskores::interop::BufferState::GetSize
viskores::Int64 GetSize() const
Get the size of the buffer in bytes.
Definition: BufferState.h:140