Skip to content

Commit

Permalink
Working buffer serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Nov 11, 2023
1 parent 00c3681 commit 4e86fe4
Show file tree
Hide file tree
Showing 16 changed files with 364 additions and 101 deletions.
24 changes: 12 additions & 12 deletions docs/API/Buffer/HFloatBuffer.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# HFloatBuffer
The class `GL\Buffer\HFloatBuffer` comes with the PHP-GLFW extension to more efficiently
handle large arrays of `GLhalf` values (`float` in php).
handle large arrays of `GLhalf` values (`int` in php).

**Don't confuse these objects with actual GPU buffers.**

Expand All @@ -27,16 +27,16 @@ use GL\Buffer\HFloatBuffer;
$buffer = new HFloatBuffer;

// values can be pushed into the array using subscript
$buffer[] = 42.69;
$buffer[] = 3.14;
$buffer[] = 42;
$buffer[] = 123;

// values can be accessed like an array
echo $buffer[0]; // 42.69
echo $buffer[0]; // 42

// values can be modifed like an array
// just make sure the used index actually exists,
// it will NOT be created implicitly!
$buffer[0] = 1.23;
$buffer[0] = 8;

// dumping the buffer will display the first 127 value
var_dump($buffer);
Expand All @@ -57,7 +57,7 @@ your array contains invalid values.

```php
$buffer = new HFloatBuffer([
42.69, 3.14, 1.23
42, 123, 8
]);
```

Expand All @@ -67,15 +67,15 @@ The `HFloatBuffer` class exposes the following methods to userland.

### `push`

pushes a value into the buffer, this is exactly the same as when you would write `$buffer[] = 3.14`.
pushes a value into the buffer, this is exactly the same as when you would write `$buffer[] = 123`.

```php
function push(float $value) : void
function push(int $value) : void
```

arguments

: 1. `float` `$value` The value to be pushed into the buffer.
: 1. `int` `$value` The value to be pushed into the buffer.

returns

Expand All @@ -94,7 +94,7 @@ function pushArray(array $values) : void

arguments

: 1. `float[]` `$values` The values to be pushed into the buffer.
: 1. `int[]` `$values` The values to be pushed into the buffer.

returns

Expand All @@ -108,13 +108,13 @@ returns
Fills the buffer with `$count` amount of values. The second argument is the value that is filled in.

```php
function fill(int $count, float $value) : void
function fill(int $count, int $value) : void
```

arguments

: 1. `int $count` The number of elements to fill.
2. `float $value` That value that will be filled in.
2. `int $value` That value that will be filled in.

returns

Expand Down
2 changes: 1 addition & 1 deletion generator/build
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $nanovg->process(GEN_PATH_EXT . '/vendor/nanovg/src/nanovg.h', $gen);
// 'GLhalf' => 'HALF_FLOAT',
// 'GLdouble' => 'DOUBLE',
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('FloatBuffer', 'GLfloat', ExtType::T_DOUBLE);
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('HFloatBuffer', 'GLhalf', ExtType::T_DOUBLE);
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('HFloatBuffer', 'GLhalf', ExtType::T_LONG); // we currenly have no way to handle 16bit floats, so we consider them like the "GLhalf" as a unsigned short
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('DoubleBuffer', 'GLdouble', ExtType::T_DOUBLE);
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('IntBuffer', 'GLint', ExtType::T_LONG);
$gen->phpglfwBuffers[] = new PHPGLFWBuffer('UIntBuffer', 'GLuint', ExtType::T_LONG);
Expand Down
16 changes: 12 additions & 4 deletions generator/templates/phpglfw_buffer.c.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@
<?php echo $buffer->getObjectName(); ?> *obj;

size_t exp_item_count, item_count;
char *endptr, *token;
char *endptr, *token, *tmp_copy;

// prints the buffer for debugging
// for (size_t i = 0; i < buf_len; i++) {
Expand All @@ -358,22 +358,30 @@
// skip the ':'
endptr++;

// create a copy of the buffer, we are going to modify it
// @TODO: when using `strtok` we are modifying the entire buffer,
// which will cause an issue when we are using the same buffer for multiple elements
// This copy for sure can be avoided, but im going to tackle that in the future
tmp_copy = estrndup(endptr, buf_len - (endptr - (char *)buf));

// make the object
object_init_ex(object, <?php echo $buffer->getClassEntryName(); ?>);
obj = <?php echo $buffer->objectFromZObjFunctionName(); ?>(Z_OBJ_P(object));

cvector_reserve(obj->vec, exp_item_count);

// read the elements, check if our endptr is still valid and we have not reached the end of the buffer
while ((token = strtok(endptr, " ")) && item_count > 0 && endptr < (char *)buf + buf_len) {
<?php if ($buffer->type == "GLfloat" || $buffer->type == "GLhalf" || $buffer->type == "GLdouble"): ?>
token = strtok(tmp_copy, " ");
while (token != NULL && item_count > 0) {
<?php if ($buffer->type == "GLfloat" || $buffer->type == "GLdouble"): ?>
cvector_push_back(obj->vec, atof(token));
<?php elseif ($buffer->type == "GLint" || $buffer->type == "GLuint" || $buffer->type == "GLshort" || $buffer->type == "GLushort" || $buffer->type == "GLbyte" || $buffer->type == "GLubyte"): ?>
<?php elseif ($buffer->type == "GLint" || $buffer->type == "GLuint" || $buffer->type == "GLshort" || $buffer->type == "GLushort" || $buffer->type == "GLhalf" || $buffer->type == "GLbyte" || $buffer->type == "GLubyte"): ?>
cvector_push_back(obj->vec, strtol(token, NULL, 10));
<?php else: ?>
zend_throw_error(NULL, "Unknown buffer type for deserialization.");
return FAILURE;
<?php endif; ?>
token = strtok(NULL, " ");
item_count--;
}

Expand Down
4 changes: 2 additions & 2 deletions phpglfw.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ public function reserve(int $size) : void {}
class HFloatBuffer implements BufferInterface {
public function __construct(?array $initalData = null) {}
public function __toString() : string {}
public function push(float $value) : void {}
public function push(int $value) : void {}
public function pushArray(array $values) : void {}
public function fill(int $count, float $value) : void {}
public function fill(int $count, int $value) : void {}
public function clear() : void {}
public function size() : int {}
public function capacity() : int {}
Expand Down
40 changes: 20 additions & 20 deletions phpglfw_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 5534d8a667da4be66ea9c03ef9a48c4f6eacc0b4 */
* Stub hash: 6d737f2c1a2752aa1b7120b6e4585c4de706881a */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_glCullFace, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
Expand Down Expand Up @@ -2643,11 +2643,16 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_HFloatBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_HFloatBuffer_push arginfo_glMinSampleShading
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_GL_Buffer_HFloatBuffer_push, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_HFloatBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_HFloatBuffer_fill arginfo_class_GL_Buffer_FloatBuffer_fill
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_GL_Buffer_HFloatBuffer_fill, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, count, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_HFloatBuffer_clear arginfo_glFinish

Expand Down Expand Up @@ -2679,16 +2684,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_IntBuffer___toString arginfo_glfwGetVersionString

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_GL_Buffer_IntBuffer_push, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_GL_Buffer_IntBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_IntBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_GL_Buffer_IntBuffer_fill, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, count, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_GL_Buffer_IntBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_IntBuffer_clear arginfo_glFinish

Expand All @@ -2702,11 +2702,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_UIntBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_UIntBuffer_push arginfo_class_GL_Buffer_IntBuffer_push
#define arginfo_class_GL_Buffer_UIntBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_UIntBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_UIntBuffer_fill arginfo_class_GL_Buffer_IntBuffer_fill
#define arginfo_class_GL_Buffer_UIntBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_UIntBuffer_clear arginfo_glFinish

Expand All @@ -2720,11 +2720,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_ShortBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_ShortBuffer_push arginfo_class_GL_Buffer_IntBuffer_push
#define arginfo_class_GL_Buffer_ShortBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_ShortBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_ShortBuffer_fill arginfo_class_GL_Buffer_IntBuffer_fill
#define arginfo_class_GL_Buffer_ShortBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_ShortBuffer_clear arginfo_glFinish

Expand All @@ -2738,11 +2738,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_UShortBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_UShortBuffer_push arginfo_class_GL_Buffer_IntBuffer_push
#define arginfo_class_GL_Buffer_UShortBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_UShortBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_UShortBuffer_fill arginfo_class_GL_Buffer_IntBuffer_fill
#define arginfo_class_GL_Buffer_UShortBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_UShortBuffer_clear arginfo_glFinish

Expand All @@ -2756,11 +2756,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_ByteBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_ByteBuffer_push arginfo_class_GL_Buffer_IntBuffer_push
#define arginfo_class_GL_Buffer_ByteBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_ByteBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_ByteBuffer_fill arginfo_class_GL_Buffer_IntBuffer_fill
#define arginfo_class_GL_Buffer_ByteBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_ByteBuffer_clear arginfo_glFinish

Expand All @@ -2774,11 +2774,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_GL_Buffer_UByteBuffer___toString arginfo_glfwGetVersionString

#define arginfo_class_GL_Buffer_UByteBuffer_push arginfo_class_GL_Buffer_IntBuffer_push
#define arginfo_class_GL_Buffer_UByteBuffer_push arginfo_class_GL_Buffer_HFloatBuffer_push

#define arginfo_class_GL_Buffer_UByteBuffer_pushArray arginfo_class_GL_Buffer_FloatBuffer_pushArray

#define arginfo_class_GL_Buffer_UByteBuffer_fill arginfo_class_GL_Buffer_IntBuffer_fill
#define arginfo_class_GL_Buffer_UByteBuffer_fill arginfo_class_GL_Buffer_HFloatBuffer_fill

#define arginfo_class_GL_Buffer_UByteBuffer_clear arginfo_glFinish

Expand Down
Loading

0 comments on commit 4e86fe4

Please sign in to comment.