Namespace: dmBuffer
Language: C++
Type: Defold C++
File: buffer.h
Source: engine/dlib/src/dmsdk/dlib/buffer.h
Include: dmsdk/dlib/buffer.h
Buffer API for data buffers as the main way to communicate between systems.
Type: FUNCTION Copies the data from one buffer to another buffer. The stream declaration needs to be the same in both buffers.
Parameters
dst_buffer_handle (dmBuffer::HBuffer*) - Pointer to HBuffer from where to copy buffer data.src_buffer_handle (dmBuffer::HBuffer*) - Pointer to HBuffer where to copy the buffer data.Returns
result (dmBuffer::Result) - BUFFER_OK if buffer was copied successfullyExamples
dmBuffer::Result r = dmBuffer::Copy(buffer_a, buffer_b);
if (r == dmBuffer::RESULT_OK) {
// success
} else {
// handle error
}
Type: FUNCTION Creates a new HBuffer with a number of different streams.
Parameters
count (uint32_t) - The number of “structs” the buffer should hold (e.g. vertex count)streams_decl (const dmBuffer::StreamDeclaration*) - Array of stream declarationsstreams_decl_count (uint8_t) - Number of stream declarations inside the decl array (max 256)out_buffer (dmBuffer::HBuffer*) - Pointer to HBuffer where to store the newly allocated bufferReturns
result (dmBuffer::Result) - BUFFER_OK if buffer was allocated successfullyExamples
const dmBuffer::StreamDeclaration streams_decl[] = {
{dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3},
{dmHashString64("texcoord0"), dmBuffer::VALUE_TYPE_UINT16, 2},
{dmHashString64("color"), dmBuffer::VALUE_TYPE_UINT8, 4},
};
dmBuffer::HBuffer buffer = 0x0;
dmBuffer::Result r = dmBuffer::Create(1024, streams_decl, 3, &buffer);
if (r == dmBuffer::RESULT_OK) {
// success
} else {
// handle error
}
Type: FUNCTION Destroys a HBuffer and it’s streams.
Parameters
buffer (dmBuffer::HBuffer) - Buffer handle to the buffer to freeExamples
const dmBuffer::StreamDeclaration streams_decl[] = {
{dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3},
};
dmBuffer::HBuffer buffer = 0x0;
dmBuffer::Result r = dmBuffer::Create(4, streams_decl, 1, &buffer);
if (r == dmBuffer::RESULT_OK) {
dmBuffer::Destroy(buffer);
} else {
// handle error
}
Type: FUNCTION Gets the buffer as a byte array. If the buffer is interleaved (default), a pointer to the whole memory is returned.
Parameters
buffer (dmBuffer::HBuffer) - buffer handle.out_bytes (void*) - Pointer to void where to store the bytesout_size (uint32_t*) - Pointer to uint32_t where to store the array sizeReturns
result (dmBuffer::Result) - BUFFER_OK if the buffer was successfully accessedExamples
uint8_t* bytes = 0x0;
uint32_t size = 0;
dmBuffer::Result r = dmBuffer::GetBytes(buffer, (void**)&bytes, &size);
if (r == dmBuffer::RESULT_OK) {
for (int i = 0; i < size; ++i)
{
stream[i] = (uint8_t)(i & 0xFF);
}
} else {
// handle error
}
Type: FUNCTION Gets the current update number
Parameters
type (dmBuffer::HBuffer) - The value typeversion (uint32_t*) - The current version numberReturns
result (dmBuffer::Result) - Returns BUFFER_OK if all went okType: FUNCTION Get (struct) count for a buffer.
Parameters
buffer (dmBuffer::HBuffer) - buffer handle.count (uint32_t*) - Pointer to uint32_t where to store the element countReturns
result (dmBuffer::Result) - BUFFER_OK if the element count was successfully accessedExamples
uint32_t count = 0;
dmBuffer::Result r = dmBuffer::GetCount(buffer, &count);
if (r == dmBuffer::RESULT_OK) {
printf("buffer %p has %d number of elements", buffer, count);
} else {
// handle error
}
Type: FUNCTION Retrieve metadata entry information
Parameters
hbuffer (dmBuffer::HBuffer) - A buffer handlename_hash (dmhash_t) - The entry name as a hashdata (void**) - Gets the internal address of metadata valuescount (uint32_t) - Gets the number of metadata values storedtype (dmBuffer::ValueType) - Gets the type of values of the metadataType: FUNCTION Converts result to string
Parameters
result (dmBuffer::Result) - The resultReturns
result (const char*) - The result as a stringType: FUNCTION Gets the size of a value type
Parameters
type (dmBuffer::ValueType) - The value typeReturns
size (uint32_t) - The size in bytesType: FUNCTION Get a stream from a buffer. Output stream is 16 byte aligned.
Parameters
buffer (dmBuffer::HBuffer) - buffer handle.stream_name (dmhash_t) - Hash of stream name to getstream (void**) - Where to store the streamcount (uint32_t*) - Where to store the count (e.g. vertex count). May be null.components (uint32_t*) - Where to store the number of components (e.g. 3 for a Vector3). May be null.stride (uint32_t*) - Where to store the (struct) stride. The stride can be added to the value pointer. May be null.
E.g. for a float array, the stride is (sizeof(Struct) / sizeof(float))Returns
result (dmBuffer::Result) - BUFFER_OK if the stream was successfully accessedExamples
float* positions = 0x0;
uint32_t size = 0;
uint32_t components = 0;
uint32_t stride = 0;
dmBuffer::Result r = dmBuffer::GetStream(buffer, dmHashString64("position"), (void**)&positions, &count, &components, &stride);
if (r == dmBuffer::RESULT_OK) {
for (int i = 0; i < count; ++i)
{
for (int c = 0; c < components; ++c)
{
positions[c] *= 1.1f;
}
positions += stride;
}
} else {
// handle error
}
Type: FUNCTION Gets the stream type
Parameters
buffer (dmBuffer::HBuffer) - Pointer to a buffer.stream_name (dmhash_t) - Hash of stream name to gettype (dmBuffer::ValueType*) - The value typecomponents (uint32_t*) - The number of values (E.g. 3 for a Vector3)Returns
result (dmBuffer::Result) - Returns BUFFER_OK if all went okType: FUNCTION Converts a value type to string
Parameters
result (dmBuffer::ValueType) - The value typeReturns
result (const char*) - The value type as a stringType: TYPEDEF
typedef uint32_t HBuffer;
Type: FUNCTION Checks if a handle is still valid
Parameters
buffer (dmBuffer::HBuffer) - The bufferReturns
result (bool) - True if the handle is validType: FUNCTION Create or update a new metadata entry with a number of values of a specific type. It will allocate space to store these values.
Parameters
hbuffer (dmBuffer::HBuffer) - A buffer handlename_hash (dmhash_t) - The entry name as a hashdata (void*) - A pointer to an array of the valuescount (uint32_t) - Number of values in the arraytype (dmBuffer::ValueType) - The type of the valuesReturns
result (dmBuffer::Result) - RESULT_OK if the metadata entry was successfully storedType: STRUCT Buffer stream declaration structure
Members
m_Name (dmhash_t) - Hash of stream namem_Type (dmBuffer::ValueType) - Stream ValueType typem_Count (uint8_t) - Component count of stream type. E.g. 3 for a Vector3m_Flags (uint32_t) - Flags for a stream.m_Reserved (uint32_t) - Reserved for future use.Examples
Declare a typical position stream:
const dmBuffer::StreamDeclaration streams_decl[] = {
{dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3}
};
Type: FUNCTION Used to know if a buffer has been updated.
Parameters
type (dmBuffer::HBuffer) - The value typeReturns
result (dmBuffer::Result) - Returns BUFFER_OK if all went okType: FUNCTION Validate a buffer and it’s streams.
Parameters
buffer (dmBuffer::HBuffer) - Buffer handle to the buffer to validateExamples
// Pass buffer to third party library that does operations on the buffer or streams.
ThirdPartyLib::PerformOperation(buffer);
r = dmBuffer::ValidateBuffer(buffer);
if (r == dmBuffer::RESULT_OK) {
// buffer and streams are valid
} else {
// the third party lib made the buffer invalid
}
Type: ENUM Result enumeration.
Members
dmBuffer::RESULT_OKdmBuffer::RESULT_GUARD_INVALIDdmBuffer::RESULT_ALLOCATION_ERRORdmBuffer::RESULT_BUFFER_INVALIDdmBuffer::RESULT_BUFFER_SIZE_ERRORdmBuffer::RESULT_STREAM_SIZE_ERRORdmBuffer::RESULT_STREAM_MISSINGdmBuffer::RESULT_STREAM_TYPE_MISMATCHdmBuffer::RESULT_STREAM_COUNT_MISMATCHdmBuffer::RESULT_METADATA_INVALIDdmBuffer::RESULT_METADATA_MISSINGType: ENUM ValueType enumeration.
Members
dmBuffer::VALUE_TYPE_UINT8dmBuffer::VALUE_TYPE_UINT16dmBuffer::VALUE_TYPE_UINT32dmBuffer::VALUE_TYPE_UINT64dmBuffer::VALUE_TYPE_INT8dmBuffer::VALUE_TYPE_INT16dmBuffer::VALUE_TYPE_INT32dmBuffer::VALUE_TYPE_INT64dmBuffer::VALUE_TYPE_FLOAT32dmBuffer::MAX_VALUE_TYPE_COUNT