util_bump_cache.c (2030B)
1 ////////////////////////////////////////////////////////////////// 2 // render_vertex_cache.c 3 4 RV_GLOBAL void rv_bump_cache_create(rv_bump_cache_ctx_t* bc, s64 element_size) 5 { 6 rv_bump_cache_destroy(bc); 7 bc->element_size = element_size; 8 9 // TODO(Samdal): 10 // Check if the arena implementation supports virtual paging first... 11 bc->arena = rv_arena_alloc(.flags = rv_arena_flag_no_chain, .reserve_size = GB(1)); 12 } 13 14 RV_GLOBAL void rv_bump_cache_destroy(rv_bump_cache_ctx_t* bc) 15 { 16 if (bc->arena) { 17 rv_arena_release(bc->arena); 18 } 19 *bc = (rv_bump_cache_ctx_t){0}; 20 } 21 22 RV_GLOBAL void* rv_bump_cache_push(rv_bump_cache_ctx_t* bc, const void* new) 23 { 24 return rv_bump_cache_push_many(bc, new, 1); 25 } 26 RV_GLOBAL void* rv_bump_cache_push_many(rv_bump_cache_ctx_t* bc, const void* new, s64 count) 27 { 28 rv_assert(bc->element_size > 0); 29 30 void* res = rv_arena_push(bc->arena, bc->element_size * count, 1); 31 rv_mem_copy(res, new, bc->element_size * count); 32 33 if (!bc->begin) { 34 bc->begin = res; 35 rv_assert(bc->total_count == 0); 36 rv_assert(bc->committed_so_far == 0); 37 } 38 39 bc->current_count += count; 40 bc->total_count += count; 41 42 return res; 43 } 44 45 RV_GLOBAL void rv_bump_cache_break(rv_bump_cache_ctx_t* bc, s64* offset_out, s64* count_out) { 46 if (offset_out) *offset_out = bc->committed_so_far; 47 if (count_out) *count_out = bc->current_count; 48 49 rv_command_t res = {0}; 50 51 bc->committed_so_far += bc->current_count; 52 53 bc->current_count = 0; 54 } 55 56 RV_GLOBAL void rv_bump_cache_upload_and_reset(rv_bump_cache_ctx_t* bc, rv_arena* arena, void** data_out, s64* size_out) 57 { 58 // copy over data into new arena 59 *data_out = rv_arena_push(arena, bc->total_count * bc->element_size, 8); 60 rv_mem_copy(*data_out, bc->begin, bc->total_count * bc->element_size); 61 *size_out = bc->total_count * bc->element_size; 62 63 // reset 64 rv_arena_clear(bc->arena); 65 bc->begin = NULL; 66 bc->current_count = 0; 67 bc->committed_so_far = 0; 68 bc->total_count = 0; 69 }