Path: blob/master/thirdparty/meshoptimizer/vfetchoptimizer.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>56size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count)7{8assert(index_count % 3 == 0);910memset(destination, -1, vertex_count * sizeof(unsigned int));1112unsigned int next_vertex = 0;1314for (size_t i = 0; i < index_count; ++i)15{16unsigned int index = indices[i];17assert(index < vertex_count);1819if (destination[index] == ~0u)20{21destination[index] = next_vertex++;22}23}2425assert(next_vertex <= vertex_count);2627return next_vertex;28}2930size_t meshopt_optimizeVertexFetch(void* destination, unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)31{32assert(index_count % 3 == 0);33assert(vertex_size > 0 && vertex_size <= 256);3435meshopt_Allocator allocator;3637// support in-place optimization38if (destination == vertices)39{40unsigned char* vertices_copy = allocator.allocate<unsigned char>(vertex_count * vertex_size);41memcpy(vertices_copy, vertices, vertex_count * vertex_size);42vertices = vertices_copy;43}4445// build vertex remap table46unsigned int* vertex_remap = allocator.allocate<unsigned int>(vertex_count);47memset(vertex_remap, -1, vertex_count * sizeof(unsigned int));4849unsigned int next_vertex = 0;5051for (size_t i = 0; i < index_count; ++i)52{53unsigned int index = indices[i];54assert(index < vertex_count);5556unsigned int& remap = vertex_remap[index];5758if (remap == ~0u) // vertex was not added to destination VB59{60// add vertex61memcpy(static_cast<unsigned char*>(destination) + next_vertex * vertex_size, static_cast<const unsigned char*>(vertices) + index * vertex_size, vertex_size);6263remap = next_vertex++;64}6566// modify indices in place67indices[i] = remap;68}6970assert(next_vertex <= vertex_count);7172return next_vertex;73}747576