Skip to content

Commit

Permalink
Add glimpl_download_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaivel committed Jul 10, 2024
1 parent 7442671 commit 657e1de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
6 changes: 6 additions & 0 deletions inc/sharedgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
#define UNPACK_A(packed) (((packed) >> 16) & 0xFFFF)
#define UNPACK_B(packed) ((packed) & 0xFFFF)

#ifdef _WIN32
# define FORCEINLINE __forceinline
#else
# define FORCEINLINE __attribute__((always_inline))
#endif

inline bool is_value_likely_an_offset(const void *p)
{
uintptr_t v = (uintptr_t)p;
Expand Down
57 changes: 30 additions & 27 deletions src/client/glimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,34 @@ static inline size_t glimpl_get_pixel_size(GLenum format)
}
}

static inline void glimpl_upload_buffer(const void *data, size_t size)
{
pb_push(SGL_CMD_VP_UPLOAD);
pb_push(CEIL_DIV(size, 4));
pb_memcpy((void*)data, size);
}

static inline void glimpl_download_buffer(void *dst, size_t size)
{
int blocks = CEIL_DIV(size, SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES);
size_t count = size;

void *retval_v = pb_ptr(SGL_OFFSET_REGISTER_RETVAL_V);

for (int i = 0; i < blocks; i++) {
size_t true_count = count > SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES ? SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES : count;

pb_push(SGL_CMD_VP_DOWNLOAD);
pb_push(true_count);

glimpl_submit();

memcpy((char*)dst + (i * SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES), retval_v, true_count);

count -= SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES;
}
}

static void glimpl_upload_texture(GLsizei width, GLsizei height, GLsizei depth, GLenum format, const void* pixels)
{
if (pixels == NULL) {
Expand All @@ -692,9 +720,7 @@ static void glimpl_upload_texture(GLsizei width, GLsizei height, GLsizei depth,

size_t total_size = width * height * depth * glimpl_get_pixel_size(format);

pb_push(SGL_CMD_VP_UPLOAD);
pb_push(CEIL_DIV(total_size, 4));
pb_memcpy((void*)pixels, total_size);
glimpl_upload_buffer(pixels, total_size);
}

static inline void glimpl_push_client_pointers(int mode, int max_index)
Expand Down Expand Up @@ -866,13 +892,6 @@ static inline void glimpl_draw_elements(int mode, int type, int start, int end,

#undef GET_MAX_INDEX

static inline void glimpl_upload_buffer(const void *data, size_t size)
{
pb_push(SGL_CMD_VP_UPLOAD);
pb_push(CEIL_DIV(size, 4));
pb_memcpy((void*)data, size);
}

static void glimpl_texture_subimage(int cmd, int n_dims, GLuint texture, GLint level, GLint xoffset, GLint yoffset,
GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels)
{
Expand Down Expand Up @@ -990,23 +1009,7 @@ static void *glimpl_map_buffer_range(int cmd, GLenum buffer, GLintptr offset, GL

glimpl_submit();

int blocks = length / SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES + (length % SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES != 0);
size_t count = length;

void *retval_v = pb_ptr(SGL_OFFSET_REGISTER_RETVAL_V);

for (int i = 0; i < blocks; i++) {
size_t true_count = count > SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES ? SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES : count;

pb_push(SGL_CMD_VP_DOWNLOAD);
pb_push(true_count);

glimpl_submit();

memcpy((char*)glimpl_map_buffer.mem + (i * SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES), retval_v, true_count);

count -= SGL_VP_DOWNLOAD_BLOCK_SIZE_IN_BYTES;
}
glimpl_download_buffer(glimpl_map_buffer.mem, length);

return glimpl_map_buffer.mem;
}
Expand Down

0 comments on commit 657e1de

Please sign in to comment.