Beatmup
Beatmup::GL::StorageBuffer Class Reference

#include <storage_buffer.h>

Public Member Functions

 StorageBuffer (GL::RecycleBin &recycleBin)
 
 ~StorageBuffer ()
 
void allocate (GraphicPipeline &gpu, const size_t sizeBytes, const void *data=nullptr)
 
void allocateStatic (GraphicPipeline &gpu, const size_t sizeBytes, const void *data)
 
void bind (GraphicPipeline &gpu, int unit) const
 
void fetch (GraphicPipeline &gpu, void *data, size_t limit)
 
void fetchToBitmap (GraphicPipeline &gpu, size_t offset, size_t stride, AbstractBitmap &bitmap)
 Copies buffer content to a bitmap. More...
 
size_t getCurrentCapacity () const
 

Private Attributes

size_t sizeBytes
 
GL::RecycleBinrecycleBin
 
handle_t handle
 

Friends

class AbstractProgram
 

Detailed Description

Definition at line 26 of file storage_buffer.h.

Constructor & Destructor Documentation

◆ StorageBuffer()

StorageBuffer::StorageBuffer ( GL::RecycleBin recycleBin)

Definition at line 30 of file storage_buffer.cpp.

◆ ~StorageBuffer()

StorageBuffer::~StorageBuffer ( )

Definition at line 35 of file storage_buffer.cpp.

35  {
36  class Deleter : public GL::RecycleBin::Item {
37  private:
38  const handle_t handle;
39  public:
40  Deleter(handle_t handle) : handle(handle) {}
41  ~Deleter() {
42  glDeleteBuffers(1, &handle);
43  }
44  };
45 
46  if (handle)
47  recycleBin.put(new Deleter(handle));
48 }
A wrapper for a GPU resource.
Definition: recycle_bin.h:39
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73
unsigned int handle_t
A reference to a GPU resource.
Definition: basic_types.h:61

Member Function Documentation

◆ allocate()

void StorageBuffer::allocate ( GraphicPipeline gpu,
const size_t  sizeBytes,
const void *  data = nullptr 
)

Definition at line 51 of file storage_buffer.cpp.

51  {
52  if (handle && this->sizeBytes != sizeBytes) {
53  glDeleteBuffers(1, &handle);
54  handle = 0;
55  }
56 
57  if (!handle && sizeBytes > 0) {
58  glGenBuffers(1, &handle);
59  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
60  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeBytes, data, GL_DYNAMIC_COPY);
61  }
62 
63  this->sizeBytes = sizeBytes;
64 }

◆ allocateStatic()

void Beatmup::GL::StorageBuffer::allocateStatic ( GraphicPipeline gpu,
const size_t  sizeBytes,
const void *  data 
)

Definition at line 66 of file storage_buffer.cpp.

66  {
67  if (handle && this->sizeBytes != sizeBytes) {
68  glDeleteBuffers(1, &handle);
69  handle = 0;
70  }
71 
72  if (!handle && sizeBytes > 0) {
73  glGenBuffers(1, &handle);
74  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
75  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeBytes, data, GL_STATIC_DRAW);
76  }
77 
78  this->sizeBytes = sizeBytes;
79 }

◆ bind()

void StorageBuffer::bind ( GraphicPipeline gpu,
int  unit 
) const

Definition at line 82 of file storage_buffer.cpp.

82  {
83  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, unit, handle);
84  GLException::check("binding storage buffer");
85 }
static void check(const std::string &info)
Definition: bgl.h:62

◆ fetch()

void StorageBuffer::fetch ( GraphicPipeline gpu,
void *  data,
size_t  limit 
)

Definition at line 88 of file storage_buffer.cpp.

88  {
89  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
90  void* buffer = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, limit, GL_MAP_READ_BIT);
91  const bool okay = buffer != nullptr;
92  if (okay)
93  memcpy(data, buffer, limit);
94  glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
95  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
96  GLException::check("reading storage buffer");
97  if (!okay)
98  throw RuntimeError("Buffer data not available");
99 }

◆ fetchToBitmap()

void Beatmup::GL::StorageBuffer::fetchToBitmap ( GraphicPipeline gpu,
size_t  offset,
size_t  stride,
AbstractBitmap bitmap 
)

Copies buffer content to a bitmap.

Parameters
[in]gpuGraphic pipeline instance
[in]offsetOffset in the buffer in bytes
[in]strideStride between entries to be copied to the bitmap in the buffer in bytes
[out]bitmapThe bitmap to fill

Definition at line 101 of file storage_buffer.cpp.

101  {
102  Beatmup::RuntimeError::check(!bitmap.isMask(), "Mask bitmaps are not supported");
103 
104  const size_t limit = offset + stride * (bitmap.getSize().numPixels() - 1) + bitmap.getBitsPerPixel() / 8;
105  Beatmup::RuntimeError::check(getCurrentCapacity() >= limit, "Bitmap does not fit the buffer content");
107 
108  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
109  const pixbyte* buffer = (const pixbyte*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, offset, limit - offset, GL_MAP_READ_BIT);
110  const bool okay = buffer != nullptr;
111  if (okay) {
112  pixbyte* ptr = bitmap.getData(0, 0);
113  const int
114  nPix = bitmap.getSize().numPixels(),
115  step = bitmap.getBitsPerPixel() / 8;
116  for (int i = 0; i < nPix; ++i, ptr += step, buffer += stride)
117  memcpy(ptr, buffer, step);
118  }
119  glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
120  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
121 }
Makes a bitmap writable for a specific target device.
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
const unsigned char getBitsPerPixel() const
Returns number of bits per pixel stored in each bitmap.
bool isMask() const
Returns true if the bitmap is a mask, false otherwise.
size_t getCurrentCapacity() const
const pixbyte * getData(int x, int y) const
Returns a pointer to given pixel.
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
uint8_t pixbyte
Definition: basic_types.h:34
Beatmup::InternalBitmap * bitmap
jlong jint jfloat step

◆ getCurrentCapacity()

size_t Beatmup::GL::StorageBuffer::getCurrentCapacity ( ) const
inline

Definition at line 49 of file storage_buffer.h.

49 { return sizeBytes; }

Friends And Related Function Documentation

◆ AbstractProgram

friend class AbstractProgram
friend

Definition at line 27 of file storage_buffer.h.

Member Data Documentation

◆ sizeBytes

size_t Beatmup::GL::StorageBuffer::sizeBytes
private

Definition at line 29 of file storage_buffer.h.

◆ recycleBin

GL::RecycleBin& Beatmup::GL::StorageBuffer::recycleBin
private

Definition at line 30 of file storage_buffer.h.

◆ handle

handle_t Beatmup::GL::StorageBuffer::handle
private

Definition at line 31 of file storage_buffer.h.


The documentation for this class was generated from the following files: