ImFusion SDK 4.3
ShaderStorageBuffer Class Reference

#include <ImFusion/Core/GL/ShaderStorageBuffer.h>

Specialization of Buffer for OpenGL Shader Storage Buffer Object. More...

+ Inheritance diagram for ShaderStorageBuffer:

Detailed Description

Specialization of Buffer for OpenGL Shader Storage Buffer Object.

SSBOs are the most powerful way to transport data between the CPU and OpenGL shaders and allow for both read and write access.

Example for setting up an SSBO and attaching it to a shader

// define buffer layout of data to be shared with GPU
struct Foo {
int32_t input = 0;
float result = 0.f;
};
std::vector<Foo> foos(4, Foo{});
for (int i = 0; i < 4; ++i)
foos[i].input = i + 1;
// create SSBO and upload data
ssbo.bind();
ssbo.setData(foos);
// bind SSBO to shader
Program prog = ...
prog.enable();
prog.bindBuffer("_fooBuffer", ssbo);
@ DynamicDraw
The data is modified repeatedly and used many times; it is modified by the application,...
Definition Buffer.h:65
OpenGL GLSL program with a fragment and optional vertex and geometry shader.
Definition Program.h:99
void enable()
Enable the shader program now.
bool bindBuffer(const char *name, ShaderStorageBuffer &buffer)
Binds the given SSBO to the interface block of the given name in the shader.
Specialization of Buffer for OpenGL Shader Storage Buffer Object.
Definition ShaderStorageBuffer.h:60

Corresponding GLSL shader code:

// define buffer layout on the GPU side, make sure to match the CPU side including potential padding
struct Foo {
int input;
float result;
};
layout(std430) buffer _fooBuffer {
Foo[] foos;
};
void main() {
// access SSBO
for (int i = 0; i < 4; ++i)
foos[0].result = computeResult(foos[i].input);
}
Note
SSBOs require at least OpenGL 4.3 or the ARB_shader_storage_buffer_object extension.
See also
https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object

Public Member Functions

 ShaderStorageBuffer (Usage usageHint=Buffer::Usage::StaticDraw)
 Instantiate a new Shader Storage Buffer Object.
 
void bindBase (uint32_t index)
 Bind the buffer to the storage block with the given index.
 
template<typename... Ts>
void setSubData (size_t offset, std::vector< Ts >... data)
 Upload data into the buffer starting at the given offset.
 
template<typename T>
void setData (Utils::Span< T > data)
 Upload data to the buffer.
 
template<typename T>
void setData (const std::vector< T > &data)
 Upload data to the buffer.
 
template<typename T>
void getData (std::vector< T > &outData)
 Download all data from the buffer attached to the given target that fits into the given vector.
 
template<typename T>
void getSubData (int64_t offsetBytes, int64_t sizeBytes, std::vector< T > &outData)
 Download the given range of data of the buffer attached to the given target.
 
size_t bufferSize () const
 Returns the number of bytes that were previously allocated on the GPU for this buffer.
 
void allocateBuffer (int64_t numBytes)
 Allocates storage on the GPU for this buffer object.
 
template<typename... Ts>
void setSubData (size_t offset, Utils::Span< Ts >... data)
 Upload data into the buffer starting at the given offset.
 
- Public Member Functions inherited from Buffer
 Buffer (Target target, Usage usage)
 Creates a new Buffer.
 
void bind () const
 Binds the buffer to the target given during construction.
 
Usage usageHint () const
 Returns the buffer usage hint passed to OpenGL during allocation.
 
void setUsageHint (Usage value)
 Sets the buffer usage hint passed to OpenGL during allocation.
 
uint32_t id () const
 Return a handle to the internal OpenGL buffer object.
 

Static Public Member Functions

static void unbind ()
 Unbind any bound shader storage buffer.
 
- Static Public Member Functions inherited from Buffer
static void unbind (Target target)
 Binds 0 to the specified target.
 

Additional Inherited Members

- Public Types inherited from Buffer
enum class  Target {
  Array = 0x8892 , AtomicCounter = 0x92C0 , CopyRead = 0x8F36 , CopyWrite = 0x8F37 ,
  DrawIndirect = 0x8F3F , ElementArray = 0x8893 , PixelPack = 0x88EB , PixelUnpack = 0x88EC ,
  ShaderStorage = 0x90D2 , Texture = 0x8C2A , TransformFeedback = 0x8C8E , Uniform = 0x8A11
}
 Enumeration of the different OpenGL buffer types. More...
 
enum class  Usage {
  StreamDraw = 0x88E0 , StreamRead = 0x88E1 , StreamCopy = 0x88E2 , StaticDraw = 0x88E4 ,
  StaticRead = 0x88E5 , StaticCopy = 0x88E6 , DynamicDraw = 0x88E8 , DynamicRead = 0x88E9 ,
  DynamicCopy = 0x88EA
}
 Enumeration for usage pattern hints to provide to the GPU when allocating data. More...
 
- Protected Member Functions inherited from Buffer
void allocateBuffer (int64_t numBytes)
 Allocates storage on the GPU for this buffer object.
 
template<typename... Ts>
void setSubData (size_t offset, Utils::Span< Ts >... data)
 Upload data into the buffer starting at the given offset.
 
template<typename... Ts>
size_t getBufferSize (Utils::Span< Ts >... data) const
 Compute the total number of bytes used by all provided data together.
 
- Static Protected Member Functions inherited from Buffer
static void glBufferSubDataImpl (Target target, int64_t offset, int64_t size, const void *data)
 forwards to OpenGL's glBufferSubData().
 
static void glGetBufferSubDataImpl (Target target, int64_t offset, int64_t size, void *data)
 forwards to OpenGL's glGetBufferSubData().
 
- Protected Attributes inherited from Buffer
const Target m_target
 Target binding point for the buffer, will never change for a Buffer instance.
 
Usage m_usageHint
 Buffer usage hint passed to OpenGL unless explicitly specified otherwise.
 
uint32_t m_id = 0
 Internal OpenGL handle to buffer object.
 
int64_t m_bufferSize = 0
 Number of bytes allocated on the GPU.
 

Constructor & Destructor Documentation

◆ ShaderStorageBuffer()

Instantiate a new Shader Storage Buffer Object.

Parameters
usageHintExpected usage pattern of the buffer
Exceptions
std::runtime_errorif the corresponding OpenGL buffer object could not be created.

Member Function Documentation

◆ setSubData() [1/2]

template<typename... Ts>
void setSubData ( size_t offset,
std::vector< Ts >... data )
inline

Upload data into the buffer starting at the given offset.

Parameters
offsetOffset in bytes into the buffer object's data store where start writing data.
dataPack of data arrays that should be copied to the buffer. If you provide multiple spans they will be concatenated and end up next to each other in the buffer without any padding.

◆ setData() [1/2]

template<typename T>
void setData ( Utils::Span< T > data)

Upload data to the buffer.

Note
The buffer needs to be bound before this method is called. Any previous data will be deleted.
Parameters
dataContiguous sequence of elements that shall be uploaded to the buffer.

◆ setData() [2/2]

template<typename T>
void setData ( const std::vector< T > & data)

Upload data to the buffer.

Note
The buffer needs to be bound before this method is called. Any previous data will be deleted.
Parameters
dataVector of elements that shall be uploaded to the buffer.

◆ getData()

template<typename T>
void getData ( std::vector< T > & outData)

Download all data from the buffer attached to the given target that fits into the given vector.

Note
The buffer needs to be bound before this method is called.
Parameters
outDataVector in which the data should be downloaded. The size of the vector must correspond the number of bytes to be downloaded.

◆ getSubData()

template<typename T>
void getSubData ( int64_t offsetBytes,
int64_t sizeBytes,
std::vector< T > & outData )

Download the given range of data of the buffer attached to the given target.

Note
The buffer needs to be bound before this method is called.
Parameters
offsetBytesSpecifies the offset into the buffer object's data store from which data will be returned, measured in bytes.
sizeBytesSpecifies the size in bytes of the data store region being returned.
outDataVector into which the data should be downloaded, will be resized to fit the data.

◆ allocateBuffer()

void allocateBuffer ( int64_t numBytes)

Allocates storage on the GPU for this buffer object.

Parameters
numBytesNumber of bytes to allocate.

◆ setSubData() [2/2]

template<typename... Ts>
void setSubData ( size_t offset,
Utils::Span< Ts >... data )

Upload data into the buffer starting at the given offset.

Parameters
offsetOffset in bytes into the buffer object's data store where start writing data.
dataPack of data arrays that should be copied to the buffer. If you provide multiple spans they will be concatenated and end up next to each other in the buffer without any padding.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close