From f84d6a6e18f8fffbfc619eb332ef3fbe0a0dd98b Mon Sep 17 00:00:00 2001 From: dmaivel Date: Mon, 15 Jul 2024 00:20:39 -0400 Subject: [PATCH] Implement glMapBuffer, glMapNamedBuffer --- src/client/glimpl.c | 59 ++++++++++++++++++++++++++---------------- src/server/processor.c | 11 ++++++++ 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/client/glimpl.c b/src/client/glimpl.c index 422487e..75ba1d2 100644 --- a/src/client/glimpl.c +++ b/src/client/glimpl.c @@ -981,13 +981,34 @@ static void glimpl_buffer_clear_data(int cmd, bool is_subdata, GLenum buffer, GL pb_push(*(unsigned int*)data); // to-do: technically should look at type first, dont care } +static void glimpl_get_buffer_parameter(int cmd, GLuint buffer, GLenum pname, GLint* params) +{ + pb_push(cmd); + pb_push(buffer); + pb_push(pname); + + glimpl_submit(); + *params = pb_read(SGL_OFFSET_REGISTER_RETVAL); +} + static void *glimpl_map_buffer_range(int cmd, GLenum buffer, GLintptr offset, GLsizeiptr length, GLbitfield access) { + bool ranged = true; if (glimpl_map_buffer.in_use) { fprintf(stderr, "glimpl_map_buffer_range: map buffer already in use, returning NULL\n"); return NULL; } + /* + * length is 0 when this function is called by glMapBuffer, glMapNamedBuffer + */ + if (length == 0) { + bool is_named = cmd == SGL_CMD_MAPNAMEDBUFFER; + int get_param_cmd = !is_named ? SGL_CMD_GETBUFFERPARAMETERIV : SGL_CMD_GETNAMEDBUFFERPARAMETERIV; + glimpl_get_buffer_parameter(get_param_cmd, buffer, GL_BUFFER_SIZE, (GLint*)&length); + ranged = false; + } + glimpl_map_buffer = (struct gl_map_buffer){ /* target = */ buffer, /* mem = */ scratch_buffer_get(length), // calloc(length, 1), // glimpl_map_buffer.mem, @@ -997,11 +1018,21 @@ static void *glimpl_map_buffer_range(int cmd, GLenum buffer, GLintptr offset, GL /* in_use = */ true }; + /* + * this function supports both glMapBuffer and glMapBufferRange; as such, these functions have + * different parameters to be pushed + */ pb_push(cmd); - pb_push(buffer); - pb_push(offset); - pb_push(length); - pb_push(access); + if (ranged) { + pb_push(buffer); + pb_push(offset); + pb_push(length); + pb_push(access); + } + else { + pb_push(buffer); + pb_push(access); + } glimpl_submit(); @@ -1010,16 +1041,6 @@ static void *glimpl_map_buffer_range(int cmd, GLenum buffer, GLintptr offset, GL return glimpl_map_buffer.mem; } -static void glimpl_get_buffer_parameter(int cmd, GLuint buffer, GLenum pname, GLint* params) -{ - pb_push(cmd); - pb_push(buffer); - pb_push(pname); - - glimpl_submit(); - *params = pb_read(SGL_OFFSET_REGISTER_RETVAL); -} - static void glimpl_invalidate_framebuffer(int cmd, bool is_subframebuffer, GLenum framebuffer, GLsizei n_attachments, const GLenum *attachments, int x, int y, int width, int height) { @@ -7303,8 +7324,7 @@ void glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, void* d void* glMapBuffer(GLenum target, GLenum access) { - STUB(); - return NULL; + return glimpl_map_buffer_range(SGL_CMD_MAPBUFFER, target, 0, 0, access); } void glGetBufferParameteriv(GLenum target, GLenum pname, GLint* params) @@ -9459,12 +9479,7 @@ void glClearNamedBufferSubData(GLuint buffer, GLenum internalformat, GLintptr of void* glMapNamedBuffer(GLuint buffer, GLenum access) { - /* - * stub, refer to glMapBuffer - * to-do: print stubs to stderr? - */ - STUB(); - return NULL; + return glimpl_map_buffer_range(SGL_CMD_MAPNAMEDBUFFER, buffer, 0, 0, access); } void* glMapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length, GLbitfield access) diff --git a/src/server/processor.c b/src/server/processor.c index b56e4de..023535e 100644 --- a/src/server/processor.c +++ b/src/server/processor.c @@ -4781,6 +4781,13 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args) glVertexAttribIPointer(index, size, type, stride, !is_value_likely_an_offset((void*)(uintptr_t)ptr) ? uploaded : (void*)(uintptr_t)ptr); break; } + case SGL_CMD_MAPBUFFER: { + int target = *pb++, + access = *pb++; + map_buffer = glMapBuffer(target, access); + map_buffer_offset = 0; + break; + } case SGL_CMD_MAPBUFFERRANGE: { int target = *pb++, offset = *pb++, @@ -5504,6 +5511,10 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args) break; } case SGL_CMD_MAPNAMEDBUFFER: { + int target = *pb++, + access = *pb++; + map_buffer = glMapNamedBuffer(target, access); + map_buffer_offset = 0; break; } case SGL_CMD_MAPNAMEDBUFFERRANGE: {