VTK-m  2.0
BufferState.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_interop_BufferState_h
11 #define vtk_m_interop_BufferState_h
12 
13 //gl headers needs to be buffer anything to do with buffer's
16 
18 
19 #include <memory>
20 
21 namespace vtkm
22 {
23 namespace interop
24 {
25 
26 namespace internal
27 {
28 
30 
37 class TransferResource
38 {
39 public:
40  virtual ~TransferResource() {}
41 };
42 
44 }
45 
57 {
58 public:
60  BufferState(GLuint& gLHandle)
61  : OpenGLHandle(&gLHandle)
62  , BufferType(GL_INVALID_VALUE)
64  , CapacityOfBuffer(0)
65  , DefaultGLHandle(0)
66  , Resource()
67  {
68  }
69 
71  BufferState(GLuint& gLHandle, GLenum type)
72  : OpenGLHandle(&gLHandle)
73  , BufferType(type)
75  , CapacityOfBuffer(0)
76  , DefaultGLHandle(0)
77  , Resource()
78  {
79  }
80 
82  : OpenGLHandle(nullptr)
83  , BufferType(GL_INVALID_VALUE)
85  , CapacityOfBuffer(0)
86  , DefaultGLHandle(0)
87  , Resource()
88  {
89  this->OpenGLHandle = &this->DefaultGLHandle;
90  }
91 
93  {
94  //don't delete this as it points to user memory, or stack allocated
95  //memory inside this object instance
96  this->OpenGLHandle = nullptr;
97  }
98 
101  GLuint* GetHandle() const { return this->OpenGLHandle; }
102 
105  bool HasType() const { return this->BufferType != GL_INVALID_VALUE; }
106 
110  GLenum GetType() const { return this->BufferType; }
111 
114  void SetType(GLenum type) { this->BufferType = type; }
115 
122  template <typename T>
124  {
125  this->BufferType = vtkm::interop::internal::BufferTypePicker(t);
126  }
127 
132  vtkm::Int64 GetSize() const { return this->SizeOfActiveSection; }
133 
134  //Set the size of buffer in bytes
135  //This will always needs to be <= the capacity of the buffer
136  //Note: This call should only be used internally by vtk-m
137  void SetSize(vtkm::Int64 size) { this->SizeOfActiveSection = size; }
138 
146  vtkm::Int64 GetCapacity() const { return this->CapacityOfBuffer; }
147 
148  // Helper function to compute when we should resize the capacity of the
149  // buffer
150  bool ShouldRealloc(vtkm::Int64 desiredSize) const
151  {
152  const bool haveNotEnoughRoom = this->GetCapacity() < desiredSize;
153  const bool haveTooMuchRoom = this->GetCapacity() > (desiredSize * 2);
154  return (haveNotEnoughRoom || haveTooMuchRoom);
155  }
156 
157  //Set the capacity of buffer in bytes
158  //The capacity of a buffer can be larger than the active size of buffer
159  //Note: This call should only be used internally by vtk-m
160  void SetCapacity(vtkm::Int64 capacity) { this->CapacityOfBuffer = capacity; }
161 
162  //Note: This call should only be used internally by vtk-m
163  vtkm::interop::internal::TransferResource* GetResource() { return this->Resource.get(); }
164 
165  //Note: This call should only be used internally by vtk-m
166  void SetResource(vtkm::interop::internal::TransferResource* resource)
167  {
168  this->Resource.reset(resource);
169  }
170 
171 private:
172  // BufferState doesn't support copy or move semantics
173  BufferState(const BufferState&) = delete;
174  void operator=(const BufferState&) = delete;
175 
176  GLuint* OpenGLHandle;
177  GLenum BufferType;
178  vtkm::Int64 SizeOfActiveSection; //must be Int64 as size can be over 2billion
179  vtkm::Int64 CapacityOfBuffer; //must be Int64 as size can be over 2billion
181  std::unique_ptr<vtkm::interop::internal::TransferResource> Resource;
182 };
183 }
184 }
185 
186 #endif //vtk_m_interop_BufferState_h
VTKM_SILENCE_WEAK_VTABLE_WARNING_START
#define VTKM_SILENCE_WEAK_VTABLE_WARNING_START
Definition: ExportMacros.h:119
vtkm::interop::BufferState::GetCapacity
vtkm::Int64 GetCapacity() const
Get the capacity of the buffer in bytes.
Definition: BufferState.h:146
vtkm::interop::BufferState
Manages the state for transferring an ArrayHandle to opengl.
Definition: BufferState.h:56
vtkm::interop::BufferState::GetHandle
GLuint * GetHandle() const
get the OpenGL buffer handle
Definition: BufferState.h:101
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::interop::BufferState::BufferState
BufferState(GLuint &gLHandle, GLenum type)
Construct a BufferState using an existing GLHandle and type.
Definition: BufferState.h:71
VTKM_SILENCE_WEAK_VTABLE_WARNING_END
#define VTKM_SILENCE_WEAK_VTABLE_WARNING_END
Definition: ExportMacros.h:120
OpenGLHeaders.h
vtkm::interop::BufferState::SizeOfActiveSection
vtkm::Int64 SizeOfActiveSection
Definition: BufferState.h:178
BufferTypePicker.h
vtkm::interop::BufferState::OpenGLHandle
GLuint * OpenGLHandle
Definition: BufferState.h:176
vtkm::interop::BufferState::ShouldRealloc
bool ShouldRealloc(vtkm::Int64 desiredSize) const
Definition: BufferState.h:150
vtkm::interop::BufferState::CapacityOfBuffer
vtkm::Int64 CapacityOfBuffer
Definition: BufferState.h:179
ExportMacros.h
vtkm::interop::BufferState::Resource
std::unique_ptr< vtkm::interop::internal::TransferResource > Resource
Definition: BufferState.h:181
vtkm::interop::BufferState::BufferState
BufferState()
Definition: BufferState.h:81
vtkm::interop::BufferState::GetSize
vtkm::Int64 GetSize() const
Get the size of the buffer in bytes.
Definition: BufferState.h:132
vtkm::interop::BufferState::BufferType
GLenum BufferType
Definition: BufferState.h:177
vtkm::interop::BufferState::DefaultGLHandle
GLuint DefaultGLHandle
Definition: BufferState.h:180
vtkm::interop::BufferState::GetType
GLenum GetType() const
return what OpenGL buffer type we are bound to
Definition: BufferState.h:110
vtkm::interop::BufferState::GetResource
vtkm::interop::internal::TransferResource * GetResource()
Definition: BufferState.h:163
vtkm::interop::BufferState::~BufferState
~BufferState()
Definition: BufferState.h:92
vtkm::interop::BufferState::BufferState
BufferState(GLuint &gLHandle)
Construct a BufferState using an existing GLHandle.
Definition: BufferState.h:60
vtkm::interop::BufferState::SetType
void SetType(GLenum type)
Set what type of OpenGL buffer type we should bind as.
Definition: BufferState.h:114
vtkm::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:123
vtkm::interop::BufferState::operator=
void operator=(const BufferState &)=delete
vtkm::interop::BufferState::SetCapacity
void SetCapacity(vtkm::Int64 capacity)
Definition: BufferState.h:160
vtkm::interop::BufferState::HasType
bool HasType() const
return if this buffer has a valid OpenGL buffer type
Definition: BufferState.h:105
vtkm::interop::BufferState::SetResource
void SetResource(vtkm::interop::internal::TransferResource *resource)
Definition: BufferState.h:166
vtkm::interop::BufferState::SetSize
void SetSize(vtkm::Int64 size)
Definition: BufferState.h:137