Path: blob/master/thirdparty/meshoptimizer/indexanalyzer.cpp
9903 views
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details1#include "meshoptimizer.h"23#include <assert.h>4#include <string.h>56meshopt_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)7{8assert(index_count % 3 == 0);9assert(cache_size >= 3);10assert(warp_size == 0 || warp_size >= 3);1112meshopt_Allocator allocator;1314meshopt_VertexCacheStatistics result = {};1516unsigned int warp_offset = 0;17unsigned int primgroup_offset = 0;1819unsigned int* cache_timestamps = allocator.allocate<unsigned int>(vertex_count);20memset(cache_timestamps, 0, vertex_count * sizeof(unsigned int));2122unsigned int timestamp = cache_size + 1;2324for (size_t i = 0; i < index_count; i += 3)25{26unsigned int a = indices[i + 0], b = indices[i + 1], c = indices[i + 2];27assert(a < vertex_count && b < vertex_count && c < vertex_count);2829bool ac = (timestamp - cache_timestamps[a]) > cache_size;30bool bc = (timestamp - cache_timestamps[b]) > cache_size;31bool cc = (timestamp - cache_timestamps[c]) > cache_size;3233// flush cache if triangle doesn't fit into warp or into the primitive buffer34if ((primgroup_size && primgroup_offset == primgroup_size) || (warp_size && warp_offset + ac + bc + cc > warp_size))35{36result.warps_executed += warp_offset > 0;3738warp_offset = 0;39primgroup_offset = 0;4041// reset cache42timestamp += cache_size + 1;43}4445// update cache and add vertices to warp46for (int j = 0; j < 3; ++j)47{48unsigned int index = indices[i + j];4950if (timestamp - cache_timestamps[index] > cache_size)51{52cache_timestamps[index] = timestamp++;53result.vertices_transformed++;54warp_offset++;55}56}5758primgroup_offset++;59}6061size_t unique_vertex_count = 0;6263for (size_t i = 0; i < vertex_count; ++i)64unique_vertex_count += cache_timestamps[i] > 0;6566result.warps_executed += warp_offset > 0;6768result.acmr = index_count == 0 ? 0 : float(result.vertices_transformed) / float(index_count / 3);69result.atvr = unique_vertex_count == 0 ? 0 : float(result.vertices_transformed) / float(unique_vertex_count);7071return result;72}7374meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const unsigned int* indices, size_t index_count, size_t vertex_count, size_t vertex_size)75{76assert(index_count % 3 == 0);77assert(vertex_size > 0 && vertex_size <= 256);7879meshopt_Allocator allocator;8081meshopt_VertexFetchStatistics result = {};8283unsigned char* vertex_visited = allocator.allocate<unsigned char>(vertex_count);84memset(vertex_visited, 0, vertex_count);8586const size_t kCacheLine = 64;87const size_t kCacheSize = 128 * 1024;8889// simple direct mapped cache; on typical mesh data this is close to 4-way cache, and this model is a gross approximation anyway90size_t cache[kCacheSize / kCacheLine] = {};9192for (size_t i = 0; i < index_count; ++i)93{94unsigned int index = indices[i];95assert(index < vertex_count);9697vertex_visited[index] = 1;9899size_t start_address = index * vertex_size;100size_t end_address = start_address + vertex_size;101102size_t start_tag = start_address / kCacheLine;103size_t end_tag = (end_address + kCacheLine - 1) / kCacheLine;104105assert(start_tag < end_tag);106107for (size_t tag = start_tag; tag < end_tag; ++tag)108{109size_t line = tag % (sizeof(cache) / sizeof(cache[0]));110111// we store +1 since cache is filled with 0 by default112result.bytes_fetched += (cache[line] != tag + 1) * kCacheLine;113cache[line] = tag + 1;114}115}116117size_t unique_vertex_count = 0;118119for (size_t i = 0; i < vertex_count; ++i)120unique_vertex_count += vertex_visited[i];121122result.overfetch = unique_vertex_count == 0 ? 0 : float(result.bytes_fetched) / float(unique_vertex_count * vertex_size);123124return result;125}126127128