|
51 | 51 | #define COLINEAR_THRESHOLD (.1f) |
52 | 52 | #define BUFFER_FRAME_COUNT (3) |
53 | 53 | #define TILE_SIZE (16) |
54 | | -#define MAX_NODES_COUNT (1U<<22) |
| 54 | +#define MAX_NODES_COUNT (1U<<20) |
55 | 55 | #define MAX_COMMANDS (1U<<16) |
56 | 56 | #define MAX_DRAWDATA (MAX_COMMANDS*4) |
57 | 57 | #define MAX_CLIPS (256) |
@@ -518,6 +518,12 @@ void dynamic_buffer_upload(WGPUQueue queue, dynamic_buffer* b, uint32_t index) |
518 | 518 | wgpuQueueWriteBuffer(queue, b->buffers[index], 0, b->cpu_buffer, b->num_elements * b->element_size); |
519 | 519 | } |
520 | 520 |
|
| 521 | +//----------------------------------------------------------------------------------------------------------------------------- |
| 522 | +size_t dynamic_buffer_get_gpu_mem(const dynamic_buffer* b) |
| 523 | +{ |
| 524 | + return BUFFER_FRAME_COUNT * b->element_size * b->num_elements_max; |
| 525 | +} |
| 526 | + |
521 | 527 | //----------------------------------------------------------------------------------------------------------------------------- |
522 | 528 | void dynamic_buffer_terminate(struct onedraw* r, dynamic_buffer* b) |
523 | 529 | { |
@@ -1155,17 +1161,20 @@ struct onedraw* od_init(const onedraw_def* def) |
1155 | 1161 | struct onedraw* r = mem_interface.malloc_fn(sizeof(struct onedraw), mem_interface.user); |
1156 | 1162 | assert(def->device != NULL); |
1157 | 1163 |
|
1158 | | - // clear everything to zero |
1159 | | - *r = (struct onedraw) {0}; |
1160 | | - |
1161 | | - r->mem_interface = mem_interface; |
1162 | | - r->custom_log = def->log_func; |
1163 | | - r->device = def->device; |
1164 | | - r->rasterizer.srgb_rendertarget = def->srgb_rendertarget; |
1165 | | - r->rasterizer.aa_width = VEC2_SQR2; |
1166 | | - r->rasterizer.clear_rendertarget = def->clear_rendertarget; |
1167 | | - r->rasterizer.clear_color = (vec4) {.x = 0.f, .y = 0.f, .z = 0.f, .w = 1.f}; |
1168 | | - r->queue = wgpuDeviceGetQueue(r->device); |
| 1164 | + *r = (struct onedraw) |
| 1165 | + { |
| 1166 | + .mem_interface = mem_interface, |
| 1167 | + .custom_log = def->log_func, |
| 1168 | + .device = def->device, |
| 1169 | + .rasterizer = |
| 1170 | + { |
| 1171 | + .srgb_rendertarget = def->srgb_rendertarget, |
| 1172 | + .aa_width = VEC2_SQR2, |
| 1173 | + .clear_rendertarget = def->clear_rendertarget, |
| 1174 | + .clear_color = (vec4) {.x = 0.f, .y = 0.f, .z = 0.f, .w = 1.f} |
| 1175 | + }, |
| 1176 | + .queue = wgpuDeviceGetQueue(def->device) |
| 1177 | + }; |
1169 | 1178 | assert_msg(r->queue != NULL, "cannot create queue from device"); |
1170 | 1179 |
|
1171 | 1180 | // resource creation |
@@ -1203,7 +1212,6 @@ struct onedraw* od_init(const onedraw_def* def) |
1203 | 1212 | build_layout(r); |
1204 | 1213 | build_bind_groups(r); |
1205 | 1214 | build_pso(r, def->surface_format); |
1206 | | - // od_build_depthstencil_state(r); |
1207 | 1215 |
|
1208 | 1216 | return r; |
1209 | 1217 | } |
@@ -1397,17 +1405,32 @@ void od_terminate(struct onedraw* r) |
1397 | 1405 | SAFE_RELEASE(r->tiles.counters, wgpuBufferRelease); |
1398 | 1406 | SAFE_RELEASE(r->tiles.nodes, wgpuBufferRelease); |
1399 | 1407 | SAFE_RELEASE(r->tiles.heads, wgpuBufferRelease); |
| 1408 | + SAFE_RELEASE(r->tiles.indices, wgpuBufferRelease); |
| 1409 | + SAFE_RELEASE(r->tiles.indirect_draw_params, wgpuBufferRelease); |
1400 | 1410 | SAFE_RELEASE(r->rasterizer.pso, wgpuRenderPipelineRelease); |
1401 | 1411 | SAFE_RELEASE(r->tiles.binning_pso, wgpuComputePipelineRelease); |
1402 | 1412 | SAFE_RELEASE(r->tiles.write_indirect_buffer_pso, wgpuComputePipelineRelease); |
1403 | 1413 | r->mem_interface.free_fn(r, r->mem_interface.user); |
1404 | 1414 | } |
1405 | 1415 |
|
1406 | 1416 | //---------------------------------------------------------------------------------------------------------------------------- |
1407 | | -void od_get_stats(struct onedraw* r, od_stats* stats) |
1408 | | -{ |
1409 | | - UNUSED_VARIABLE(r); |
1410 | | - UNUSED_VARIABLE(stats); |
| 1417 | +void od_get_stats(const struct onedraw* r, od_stats* stats) |
| 1418 | +{ |
| 1419 | + stats->frame_index = r->stats.frame_index; |
| 1420 | + stats->gpu_memory_usage = dynamic_buffer_get_gpu_mem(&r->commands.aabb); |
| 1421 | + stats->gpu_memory_usage += dynamic_buffer_get_gpu_mem(&r->commands.clipshapes); |
| 1422 | + stats->gpu_memory_usage += dynamic_buffer_get_gpu_mem(&r->commands.colors); |
| 1423 | + stats->gpu_memory_usage += dynamic_buffer_get_gpu_mem(&r->commands.float_data); |
| 1424 | + stats->gpu_memory_usage += dynamic_buffer_get_gpu_mem(&r->commands.list); |
| 1425 | + stats->gpu_memory_usage += dynamic_buffer_get_gpu_mem(&r->commands.draw_args); |
| 1426 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->font.glyphs); |
| 1427 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->tiles.counters); |
| 1428 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->tiles.heads); |
| 1429 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->tiles.indices); |
| 1430 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->tiles.indirect_draw_params); |
| 1431 | + stats->gpu_memory_usage += wgpuBufferGetSize(r->tiles.nodes); |
| 1432 | + stats->gpu_memory_usage += r->font.desc.texture_width * r->font.desc.texture_height; |
| 1433 | + stats->gpu_memory_usage += r->atlas.width * r->atlas.height * r->atlas.num_slices * 4; // assuming atlas is RGBA8 |
1411 | 1434 | } |
1412 | 1435 |
|
1413 | 1436 | //---------------------------------------------------------------------------------------------------------------------------- |
@@ -1471,6 +1494,26 @@ void private_draw_disc(struct onedraw* r, vec2 center, float radius, float thick |
1471 | 1494 | DB_PUSH(&r->commands.aabb, quantized_aabb, aabb); |
1472 | 1495 | } |
1473 | 1496 |
|
| 1497 | +//----------------------------------------------------------------------------------------------------------------------------- |
| 1498 | +float od_get_text_height(const struct onedraw* r) |
| 1499 | +{ |
| 1500 | + return r->font.desc.font_height; |
| 1501 | +} |
| 1502 | + |
| 1503 | +//----------------------------------------------------------------------------------------------------------------------------- |
| 1504 | +float od_get_text_width(const struct onedraw* r, const char* text) |
| 1505 | +{ |
| 1506 | + float width = 0.f; |
| 1507 | + for(const char *c = text; *c != 0; c++) |
| 1508 | + { |
| 1509 | + if (*c >= r->font.desc.first_glyph && *c <= (r->font.desc.first_glyph + r->font.desc.num_glyphs)) |
| 1510 | + width += r->font.desc.glyphs[*c - r->font.desc.first_glyph].advance_x; |
| 1511 | + else |
| 1512 | + width += r->font.desc.glyphs['_'- r->font.desc.first_glyph].advance_x * .65f; |
| 1513 | + } |
| 1514 | + return width; |
| 1515 | +} |
| 1516 | + |
1474 | 1517 | //---------------------------------------------------------------------------------------------------------------------------- |
1475 | 1518 | void od_draw_ring(struct onedraw* r, float cx, float cy, float radius, float thickness, draw_color color) |
1476 | 1519 | { |
@@ -1748,10 +1791,7 @@ void od_draw_arc(struct onedraw* r, float cx, float cy, float dx, float dy, floa |
1748 | 1791 | //----------------------------------------------------------------------------------------------------------------------------- |
1749 | 1792 | void od_draw_box(struct onedraw* r, float x0, float y0, float x1, float y1, float radius, draw_color srgb_color) |
1750 | 1793 | { |
1751 | | - if ((r->commands.float_data.num_elements + 5 >= r->commands.float_data.num_elements_max) || |
1752 | | - (r->commands.colors.num_elements >= r->commands.colors.num_elements_max) || |
1753 | | - (r->commands.list.num_elements >= r->commands.list.num_elements_max) || |
1754 | | - (r->commands.aabb.num_elements >= r->commands.aabb.num_elements_max)) |
| 1794 | + if (buffers_are_full(r, 5)) |
1755 | 1795 | { |
1756 | 1796 | od_log(r, "buffers for primitive are full, expect graphical artefacts"); |
1757 | 1797 | return; |
@@ -1784,10 +1824,7 @@ void od_draw_box(struct onedraw* r, float x0, float y0, float x1, float y1, floa |
1784 | 1824 | //----------------------------------------------------------------------------------------------------------------------------- |
1785 | 1825 | void od_draw_blurred_box(struct onedraw* r, float cx, float cy, float width, float height, float roundness, draw_color srgb_color) |
1786 | 1826 | { |
1787 | | - if ((r->commands.float_data.num_elements + 5 >= r->commands.float_data.num_elements_max) || |
1788 | | - (r->commands.colors.num_elements >= r->commands.colors.num_elements_max) || |
1789 | | - (r->commands.list.num_elements >= r->commands.list.num_elements_max) || |
1790 | | - (r->commands.aabb.num_elements >= r->commands.aabb.num_elements_max)) |
| 1827 | + if (buffers_are_full(r, 5)) |
1791 | 1828 | { |
1792 | 1829 | od_log(r, "buffers for primitive are full, expect graphical artefacts"); |
1793 | 1830 | return; |
@@ -1822,10 +1859,7 @@ void od_draw_char(struct onedraw* r, float x, float y, char c, draw_color srgb_c |
1822 | 1859 | if (c < r->font.desc.first_glyph || c > (r->font.desc.first_glyph + r->font.desc.num_glyphs)) |
1823 | 1860 | return; |
1824 | 1861 |
|
1825 | | - if ((r->commands.float_data.num_elements + 2 >= r->commands.float_data.num_elements_max) || |
1826 | | - (r->commands.colors.num_elements >= r->commands.colors.num_elements_max) || |
1827 | | - (r->commands.list.num_elements >= r->commands.list.num_elements_max) || |
1828 | | - (r->commands.aabb.num_elements >= r->commands.aabb.num_elements_max)) |
| 1862 | + if (buffers_are_full(r, 2)) |
1829 | 1863 | { |
1830 | 1864 | od_log(r, "buffers for primitive are full, expect graphical artefacts"); |
1831 | 1865 | return; |
|
0 commit comments