| 1 | // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details |
| 2 | #include "meshoptimizer.h" |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | meshopt_VertexCacheStatistics meshopt_analyzeVertexCache(const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int cache_size, unsigned int warp_size, unsigned int primgroup_size) |
| 8 | { |
| 9 | assert(index_count % 3 == 0); |
| 10 | assert(cache_size >= 3); |
| 11 | assert(warp_size == 0 || warp_size >= 3); |
| 12 | |
| 13 | meshopt_Allocator allocator; |
| 14 | |
| 15 | meshopt_VertexCacheStatistics result = {}; |
| 16 | |
| 17 | unsigned int warp_offset = 0; |
| 18 | unsigned int primgroup_offset = 0; |
| 19 | |
| 20 | unsigned int* cache_timestamps = allocator.allocate<unsigned int>(vertex_count); |
| 21 | memset(cache_timestamps, 0, vertex_count * sizeof(unsigned int)); |
| 22 | |
| 23 | unsigned int timestamp = cache_size + 1; |
| 24 | |
| 25 | for (size_t i = 0; i < index_count; i += 3) |
| 26 | { |
| 27 | unsigned int a = indices[i + 0], b = indices[i + 1], c = indices[i + 2]; |
| 28 | assert(a < vertex_count && b < vertex_count && c < vertex_count); |
| 29 | |
| 30 | bool ac = (timestamp - cache_timestamps[a]) > cache_size; |
| 31 | bool bc = (timestamp - cache_timestamps[b]) > cache_size; |
| 32 | bool cc = (timestamp - cache_timestamps[c]) > cache_size; |
| 33 | |
| 34 | // flush cache if triangle doesn't fit into warp or into the primitive buffer |
| 35 | if ((primgroup_size && primgroup_offset == primgroup_size) || (warp_size && warp_offset + ac + bc + cc > warp_size)) |
| 36 | { |
| 37 | result.warps_executed += warp_offset > 0; |
| 38 | |
| 39 | warp_offset = 0; |
| 40 | primgroup_offset = 0; |
| 41 | |
| 42 | // reset cache |
| 43 | timestamp += cache_size + 1; |
| 44 | } |
| 45 | |
| 46 | // update cache and add vertices to warp |
| 47 | for (int j = 0; j < 3; ++j) |
| 48 | { |
| 49 | unsigned int index = indices[i + j]; |
| 50 | |
| 51 | if (timestamp - cache_timestamps[index] > cache_size) |
| 52 | { |
| 53 | cache_timestamps[index] = timestamp++; |
| 54 | result.vertices_transformed++; |
| 55 | warp_offset++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | primgroup_offset++; |
| 60 | } |
| 61 | |
| 62 | size_t unique_vertex_count = 0; |
| 63 | |
| 64 | for (size_t i = 0; i < vertex_count; ++i) |
| 65 | unique_vertex_count += cache_timestamps[i] > 0; |
| 66 | |
| 67 | result.warps_executed += warp_offset > 0; |
| 68 | |
| 69 | result.acmr = index_count == 0 ? 0 : float(result.vertices_transformed) / float(index_count / 3); |
| 70 | result.atvr = unique_vertex_count == 0 ? 0 : float(result.vertices_transformed) / float(unique_vertex_count); |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const unsigned int* indices, size_t index_count, size_t vertex_count, size_t vertex_size) |
| 76 | { |
| 77 | assert(index_count % 3 == 0); |
| 78 | assert(vertex_size > 0 && vertex_size <= 256); |
| 79 | |
| 80 | meshopt_Allocator allocator; |
| 81 | |
| 82 | meshopt_VertexFetchStatistics result = {}; |
| 83 | |
| 84 | unsigned char* vertex_visited = allocator.allocate<unsigned char>(vertex_count); |
| 85 | memset(vertex_visited, 0, vertex_count); |
| 86 | |
| 87 | const size_t kCacheLine = 64; |
| 88 | const size_t kCacheSize = 128 * 1024; |
| 89 | |
| 90 | // simple direct mapped cache; on typical mesh data this is close to 4-way cache, and this model is a gross approximation anyway |
| 91 | size_t cache[kCacheSize / kCacheLine] = {}; |
| 92 | |
| 93 | for (size_t i = 0; i < index_count; ++i) |
| 94 | { |
| 95 | unsigned int index = indices[i]; |
| 96 | assert(index < vertex_count); |
| 97 | |
| 98 | vertex_visited[index] = 1; |
| 99 | |
| 100 | size_t start_address = index * vertex_size; |
| 101 | size_t end_address = start_address + vertex_size; |
| 102 | |
| 103 | size_t start_tag = start_address / kCacheLine; |
| 104 | size_t end_tag = (end_address + kCacheLine - 1) / kCacheLine; |
| 105 | |
| 106 | assert(start_tag < end_tag); |
| 107 | |
| 108 | for (size_t tag = start_tag; tag < end_tag; ++tag) |
| 109 | { |
| 110 | size_t line = tag % (sizeof(cache) / sizeof(cache[0])); |
| 111 | |
| 112 | // we store +1 since cache is filled with 0 by default |
| 113 | result.bytes_fetched += (cache[line] != tag + 1) * kCacheLine; |
| 114 | cache[line] = tag + 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | size_t unique_vertex_count = 0; |
| 119 | |
| 120 | for (size_t i = 0; i < vertex_count; ++i) |
| 121 | unique_vertex_count += vertex_visited[i]; |
| 122 | |
| 123 | result.overfetch = unique_vertex_count == 0 ? 0 : float(result.bytes_fetched) / float(unique_vertex_count * vertex_size); |
| 124 | |
| 125 | return result; |
| 126 | } |