[win32, gl canvas] grow path and element input buffers as needed

This commit is contained in:
martinfouilleul 2023-07-27 11:37:39 +02:00
parent 0caeccd3fb
commit 680deb35b0
3 changed files with 58 additions and 11 deletions

View File

@ -201,7 +201,7 @@ int main()
f32 trackX = mousePos.x/zoom - startX; f32 trackX = mousePos.x/zoom - startX;
f32 trackY = mousePos.y/zoom - startY; f32 trackY = mousePos.y/zoom - startY;
zoom *= 1 + event->move.deltaY * 0.01; zoom *= 1 + event->mouse.deltaY * 0.01;
zoom = Clamp(zoom, 0.2, 10); zoom = Clamp(zoom, 0.2, 10);
startX = mousePos.x/zoom - trackX; startX = mousePos.x/zoom - trackX;

View File

@ -108,12 +108,6 @@ int main()
mp_request_quit(); mp_request_quit();
} break; } break;
case MP_EVENT_WINDOW_RESIZE:
{
mp_rect frame = {0, 0, event->frame.rect.w, event->frame.rect.h};
mg_surface_set_frame(surface, frame);
} break;
case MP_EVENT_MOUSE_BUTTON: case MP_EVENT_MOUSE_BUTTON:
{ {
if(event->key.code == MP_MOUSE_LEFT) if(event->key.code == MP_MOUSE_LEFT)
@ -138,7 +132,7 @@ int main()
f32 pinX = (mousePos.x - startX)/zoom; f32 pinX = (mousePos.x - startX)/zoom;
f32 pinY = (mousePos.y - startY)/zoom; f32 pinY = (mousePos.y - startY)/zoom;
zoom *= 1 + event->move.deltaY * 0.01; zoom *= 1 + event->mouse.deltaY * 0.01;
zoom = Clamp(zoom, 0.5, 5); zoom = Clamp(zoom, 0.5, 5);
startX = mousePos.x - pinX*zoom; startX = mousePos.x - pinX*zoom;

View File

@ -182,9 +182,47 @@ static void mg_update_path_extents(vec4* extents, vec2 p)
extents->w = maximum(extents->w, p.y); extents->w = maximum(extents->w, p.y);
} }
void mg_gl_grow_input_buffer(mg_gl_mapped_buffer* buffer, int copyStart, int copySize, int newSize)
{
mg_gl_mapped_buffer newBuffer = {0};
newBuffer.size = newSize;
glGenBuffers(1, &newBuffer.buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, newBuffer.buffer);
glBufferStorage(GL_SHADER_STORAGE_BUFFER, newBuffer.size, 0, GL_MAP_WRITE_BIT|GL_MAP_PERSISTENT_BIT);
newBuffer.contents = glMapBufferRange(GL_SHADER_STORAGE_BUFFER,
0,
newBuffer.size,
GL_MAP_WRITE_BIT
|GL_MAP_PERSISTENT_BIT
|GL_MAP_FLUSH_EXPLICIT_BIT);
memcpy(newBuffer.contents + copyStart, buffer->contents + copyStart, copySize);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer->buffer);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
glDeleteBuffers(1, &buffer->buffer);
*buffer = newBuffer;
}
void mg_gl_canvas_encode_element(mg_gl_canvas_backend* backend, mg_path_elt_type kind, vec2* p) void mg_gl_canvas_encode_element(mg_gl_canvas_backend* backend, mg_path_elt_type kind, vec2* p)
{ {
mg_gl_path_elt* elementData = (mg_gl_path_elt*)backend->elementBuffer[backend->bufferIndex].contents; int bufferIndex = backend->bufferIndex;
int bufferCap = backend->elementBuffer[bufferIndex].size / sizeof(mg_gl_path_elt);
if(backend->eltCount >= bufferCap)
{
int newBufferCap = (int)(bufferCap * 1.5);
int newBufferSize = newBufferCap * sizeof(mg_gl_path_elt);
log_info("growing element buffer to %i elements\n", newBufferCap);
mg_gl_grow_input_buffer(&backend->elementBuffer[bufferIndex],
backend->eltBatchStart * sizeof(mg_gl_path_elt),
backend->eltCount * sizeof(mg_gl_path_elt),
newBufferSize);
}
mg_gl_path_elt* elementData = (mg_gl_path_elt*)backend->elementBuffer[bufferIndex].contents;
mg_gl_path_elt* elt = &elementData[backend->eltCount]; mg_gl_path_elt* elt = &elementData[backend->eltCount];
backend->eltCount++; backend->eltCount++;
@ -224,6 +262,21 @@ void mg_gl_canvas_encode_element(mg_gl_canvas_backend* backend, mg_path_elt_type
void mg_gl_canvas_encode_path(mg_gl_canvas_backend* backend, mg_primitive* primitive, f32 scale) void mg_gl_canvas_encode_path(mg_gl_canvas_backend* backend, mg_primitive* primitive, f32 scale)
{ {
int bufferIndex = backend->bufferIndex;
int bufferCap = backend->pathBuffer[bufferIndex].size / sizeof(mg_gl_path);
if(backend->pathCount >= bufferCap)
{
int newBufferCap = (int)(bufferCap * 1.5);
int newBufferSize = newBufferCap * sizeof(mg_gl_path);
log_info("growing path buffer to %i elements\n", newBufferCap);
mg_gl_grow_input_buffer(&backend->pathBuffer[bufferIndex],
backend->pathBatchStart * sizeof(mg_gl_path),
backend->eltCount * sizeof(mg_gl_path),
newBufferSize);
}
mg_gl_path* pathData = (mg_gl_path*)backend->pathBuffer[backend->bufferIndex].contents; mg_gl_path* pathData = (mg_gl_path*)backend->pathBuffer[backend->bufferIndex].contents;
mg_gl_path* path = &pathData[backend->pathCount]; mg_gl_path* path = &pathData[backend->pathCount];
backend->pathCount++; backend->pathCount++;
@ -1519,8 +1572,8 @@ int mg_gl_canvas_compile_render_program_named(const char* progName,
#define mg_gl_canvas_compile_render_program(progName, shaderSrc, vertexSrc, out) \ #define mg_gl_canvas_compile_render_program(progName, shaderSrc, vertexSrc, out) \
mg_gl_canvas_compile_render_program_named(progName, #shaderSrc, #vertexSrc, shaderSrc, vertexSrc, out) mg_gl_canvas_compile_render_program_named(progName, #shaderSrc, #vertexSrc, shaderSrc, vertexSrc, out)
const u32 MG_GL_PATH_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_path), const u32 MG_GL_PATH_BUFFER_SIZE = (4<<10)*sizeof(mg_gl_path),
MG_GL_ELEMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_path_elt), MG_GL_ELEMENT_BUFFER_SIZE = (4<<12)*sizeof(mg_gl_path_elt),
MG_GL_SEGMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_segment), MG_GL_SEGMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_segment),
MG_GL_PATH_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_path_queue), MG_GL_PATH_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_path_queue),
MG_GL_TILE_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_tile_queue), MG_GL_TILE_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_gl_tile_queue),