2015-11-03 15:22:32 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2015-11-03 15:22:32 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
2016-04-13 19:52:15 +00:00
|
|
|
#ifndef vtk_m_interop_BufferState_h
|
|
|
|
#define vtk_m_interop_BufferState_h
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
//gl headers needs to be buffer anything to do with buffer's
|
2016-04-13 19:52:15 +00:00
|
|
|
#include <vtkm/interop/internal/BufferTypePicker.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/interop/internal/OpenGLHeaders.h>
|
2015-11-03 15:22:32 +00:00
|
|
|
|
2017-04-13 18:06:33 +00:00
|
|
|
#include <vtkm/internal/ExportMacros.h>
|
|
|
|
|
2016-08-30 13:25:52 +00:00
|
|
|
#include <memory>
|
2015-11-03 15:22:32 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace interop
|
|
|
|
{
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
namespace internal
|
|
|
|
{
|
2017-04-13 18:06:33 +00:00
|
|
|
|
|
|
|
VTKM_SILENCE_WEAK_VTABLE_WARNING_START
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
/// \brief Device backend and opengl interop resources management
|
|
|
|
///
|
|
|
|
/// \c TransferResource manages a context for a given device backend and a
|
|
|
|
/// single OpenGL buffer as efficiently as possible.
|
|
|
|
///
|
|
|
|
/// Default implementation is a no-op
|
|
|
|
class TransferResource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~TransferResource() {}
|
|
|
|
};
|
2017-04-13 18:06:33 +00:00
|
|
|
|
|
|
|
VTKM_SILENCE_WEAK_VTABLE_WARNING_END
|
2015-11-03 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Manages the state for transferring an ArrayHandle to opengl.
|
|
|
|
///
|
|
|
|
/// \c BufferState holds all the relevant data information for a given ArrayHandle
|
|
|
|
/// mapping into OpenGL. Reusing the state information for all renders of an
|
|
|
|
/// ArrayHandle will allow for the most efficient interop between backends and
|
|
|
|
/// OpenGL ( especially for CUDA ).
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// The interop code in vtk-m uses a lazy buffer re-allocation.
|
|
|
|
///
|
|
|
|
class BufferState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Construct a BufferState using an existing GLHandle
|
2017-05-18 14:29:41 +00:00
|
|
|
BufferState(GLuint& gLHandle)
|
|
|
|
: OpenGLHandle(&gLHandle)
|
|
|
|
, BufferType(GL_INVALID_VALUE)
|
|
|
|
, SizeOfActiveSection(0)
|
|
|
|
, CapacityOfBuffer(0)
|
|
|
|
, DefaultGLHandle(0)
|
|
|
|
, Resource()
|
2015-11-03 15:22:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a BufferState using an existing GLHandle and type
|
2017-05-18 14:29:41 +00:00
|
|
|
BufferState(GLuint& gLHandle, GLenum type)
|
|
|
|
: OpenGLHandle(&gLHandle)
|
|
|
|
, BufferType(type)
|
|
|
|
, SizeOfActiveSection(0)
|
|
|
|
, CapacityOfBuffer(0)
|
|
|
|
, DefaultGLHandle(0)
|
|
|
|
, Resource()
|
2015-11-03 15:22:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
BufferState()
|
|
|
|
: OpenGLHandle(nullptr)
|
|
|
|
, BufferType(GL_INVALID_VALUE)
|
|
|
|
, SizeOfActiveSection(0)
|
|
|
|
, CapacityOfBuffer(0)
|
|
|
|
, DefaultGLHandle(0)
|
|
|
|
, Resource()
|
2015-11-03 15:22:32 +00:00
|
|
|
{
|
|
|
|
this->OpenGLHandle = &this->DefaultGLHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BufferState()
|
|
|
|
{
|
|
|
|
//don't delete this as it points to user memory, or stack allocated
|
|
|
|
//memory inside this object instance
|
2016-08-30 13:44:04 +00:00
|
|
|
this->OpenGLHandle = nullptr;
|
2015-11-03 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief get the OpenGL buffer handle
|
|
|
|
///
|
|
|
|
GLuint* GetHandle() const { return this->OpenGLHandle; }
|
|
|
|
|
|
|
|
/// \brief return if this buffer has a valid OpenGL buffer type
|
|
|
|
///
|
|
|
|
bool HasType() const { return this->BufferType != GL_INVALID_VALUE; }
|
|
|
|
|
|
|
|
/// \brief return what OpenGL buffer type we are bound to
|
|
|
|
///
|
|
|
|
/// will return GL_INVALID_VALUE if we don't have a valid type set
|
|
|
|
GLenum GetType() const { return this->BufferType; }
|
|
|
|
|
|
|
|
/// \brief Set what type of OpenGL buffer type we should bind as
|
|
|
|
///
|
|
|
|
void SetType(GLenum type) { this->BufferType = type; }
|
|
|
|
|
|
|
|
/// \brief deduce the buffer type from the template value type that
|
|
|
|
/// was passed in, and set that as our type
|
|
|
|
///
|
|
|
|
/// Will be GL_ELEMENT_ARRAY_BUFFER for
|
|
|
|
/// vtkm::Int32, vtkm::UInt32, vtkm::Int64, vtkm::UInt64, vtkm::Id, and vtkm::IdComponent
|
|
|
|
/// will be GL_ARRAY_BUFFER for everything else.
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T>
|
2015-11-03 15:22:32 +00:00
|
|
|
void DeduceAndSetType(T t)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
this->BufferType = vtkm::interop::internal::BufferTypePicker(t);
|
|
|
|
}
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
/// \brief Get the size of the buffer in bytes
|
|
|
|
///
|
|
|
|
/// Get the size of the active section of the buffer
|
|
|
|
///This will always be <= the capacity of the buffer
|
|
|
|
vtkm::Int64 GetSize() const { return this->SizeOfActiveSection; }
|
|
|
|
|
|
|
|
//Set the size of buffer in bytes
|
|
|
|
//This will always needs to be <= the capacity of the buffer
|
|
|
|
//Note: This call should only be used internally by vtk-m
|
|
|
|
void SetSize(vtkm::Int64 size) { this->SizeOfActiveSection = size; }
|
|
|
|
|
|
|
|
/// \brief Get the capacity of the buffer in bytes
|
|
|
|
///
|
|
|
|
/// The buffers that vtk-m allocate in OpenGL use lazy resizing. This allows
|
|
|
|
/// vtk-m to not have to reallocate a buffer while the size stays the same
|
|
|
|
/// or shrinks. This allows allows the cuda to OpenGL to perform significantly
|
|
|
|
/// better as we than don't need to call cudaGraphicsGLRegisterBuffer as
|
|
|
|
/// often
|
|
|
|
vtkm::Int64 GetCapacity() const { return this->CapacityOfBuffer; }
|
|
|
|
|
|
|
|
// Helper function to compute when we should resize the capacity of the
|
|
|
|
// buffer
|
|
|
|
bool ShouldRealloc(vtkm::Int64 desiredSize) const
|
|
|
|
{
|
|
|
|
const bool haveNotEnoughRoom = this->GetCapacity() < desiredSize;
|
2017-05-18 14:29:41 +00:00
|
|
|
const bool haveTooMuchRoom = this->GetCapacity() > (desiredSize * 2);
|
2015-11-03 15:22:32 +00:00
|
|
|
return (haveNotEnoughRoom || haveTooMuchRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set the capacity of buffer in bytes
|
|
|
|
//The capacity of a buffer can be larger than the active size of buffer
|
|
|
|
//Note: This call should only be used internally by vtk-m
|
|
|
|
void SetCapacity(vtkm::Int64 capacity) { this->CapacityOfBuffer = capacity; }
|
|
|
|
|
|
|
|
//Note: This call should only be used internally by vtk-m
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::interop::internal::TransferResource* GetResource() { return this->Resource.get(); }
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
//Note: This call should only be used internally by vtk-m
|
2017-05-18 14:29:41 +00:00
|
|
|
void SetResource(vtkm::interop::internal::TransferResource* resource)
|
|
|
|
{
|
|
|
|
this->Resource.reset(resource);
|
|
|
|
}
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
private:
|
2017-02-23 17:12:38 +00:00
|
|
|
// BufferState doesn't support copy or move semantics
|
|
|
|
BufferState(const BufferState&) = delete;
|
|
|
|
void operator=(const BufferState&) = delete;
|
2015-11-03 15:22:32 +00:00
|
|
|
|
|
|
|
GLuint* OpenGLHandle;
|
|
|
|
GLenum BufferType;
|
|
|
|
vtkm::Int64 SizeOfActiveSection; //must be Int64 as size can be over 2billion
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Int64 CapacityOfBuffer; //must be Int64 as size can be over 2billion
|
2015-11-03 15:22:32 +00:00
|
|
|
GLuint DefaultGLHandle;
|
2016-08-30 13:25:52 +00:00
|
|
|
std::unique_ptr<vtkm::interop::internal::TransferResource> Resource;
|
2015-11-03 15:22:32 +00:00
|
|
|
};
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-03 15:22:32 +00:00
|
|
|
|
2016-04-13 19:52:15 +00:00
|
|
|
#endif //vtk_m_interop_BufferState_h
|