Skip to content

Commit

Permalink
Implement glMapBuffer, glMapNamedBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaivel committed Jul 15, 2024
1 parent 5354488 commit f84d6a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/client/glimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/server/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit f84d6a6

Please sign in to comment.