zeux / meshoptimizer
zeux / meshoptimizer
Purpose
When a GPU renders triangle meshes, various stages of the GPU pipeline have to process vertex and index data. The efficiency of these stages depends on the data you feed to them; this library provides algorithms to help optimize meshes for these stages, as well as algorithms to reduce the mesh complexity and storage overhead. The library provides a C and C++ interface for all algorithms; you can use it from C/C++ or from other languages via FFI (such as P/Invoke). If you want to use this library from Rust, you should use meshopt crate. JavaScript interface for some algorithms is available through meshoptimizer.js. Two companion projects are developed and distributed alongside the library: gltfpack, a command-line tool that automatically optimizes glTF files, and clusterlod.h, a single-header C/C++ library for continuous level of detail using clustered simplification.
目的
当 GPU 渲染三角网格时,GPU 流水线的各个阶段都需要处理顶点和索引数据。这些阶段的效率取决于你输入的数据;该库提供了一系列算法来帮助优化这些阶段的网格,以及减少网格复杂度和存储开销的算法。该库为所有算法提供了 C 和 C++ 接口;你可以通过 C/C++ 使用它,也可以通过 FFI(如 P/Invoke)在其他语言中使用。如果你想在 Rust 中使用此库,应使用 meshopt crate。部分算法的 JavaScript 接口可通过 meshoptimizer.js 获取。该库还附带了两个配套项目:gltfpack(一个自动优化 glTF 文件的命令行工具)和 clusterlod.h(一个用于基于聚类简化的连续细节层次(LOD)的单头文件 C/C++ 库)。
Installing
meshoptimizer is hosted on GitHub; you can download the latest release using git: git clone -b v1.2 https://github.com/zeux/meshoptimizer.git Alternatively you can download the .zip archive from GitHub. The library is also available as a Linux package in several distributions (ArchLinux, Debian, FreeBSD, Nix, Ubuntu), as well as a Vcpkg port (see installation instructions) and a Conan package. gltfpack is available as a pre-built binary on Releases page or via npm package. Native binaries are recommended since they are more efficient and support texture compression.
安装
meshoptimizer 托管在 GitHub 上;你可以使用 git 下载最新版本:git clone -b v1.2 https://github.com/zeux/meshoptimizer.git。或者,你也可以从 GitHub 下载 .zip 压缩包。该库还作为 Linux 软件包存在于多个发行版中(ArchLinux、Debian、FreeBSD、Nix、Ubuntu),同时也支持 Vcpkg 移植(参见安装说明)和 Conan 软件包。gltfpack 可在 Releases 页面获取预编译二进制文件,或通过 npm 包安装。建议使用原生二进制文件,因为它们效率更高且支持纹理压缩。
Building
meshoptimizer is distributed as a C/C++ header (src/meshoptimizer.h) and a set of C++ source files (src/*.cpp). To include it in your project, you can use one of two options: Use CMake to build the library (either as a standalone project or as part of your project) Add source files to your project’s build system. The source files are organized in such a way that you don’t need to change your build-system settings, and you only need to add the source files for the algorithms you use. They should build without warnings or special compilation options on all major compilers. If you prefer amalgamated builds, you can also concatenate the source files into a single .cpp file and build that instead. To use meshoptimizer functions, simply #include the header meshoptimizer.h; the library source is C++, but the header is C-compatible.
构建
meshoptimizer 以 C/C++ 头文件 (src/meshoptimizer.h) 和一组 C++ 源文件 (src/*.cpp) 的形式分发。要将其包含在你的项目中,可以使用以下两种方式之一:使用 CMake 构建库(作为独立项目或作为你项目的一部分),或者将源文件添加到你项目的构建系统中。源文件的组织方式使得你无需更改构建系统设置,只需添加你所使用的算法对应的源文件即可。它们在所有主流编译器上都应能无警告且无需特殊编译选项地进行构建。如果你更喜欢合并构建,也可以将源文件连接成一个单独的 .cpp 文件进行构建。要使用 meshoptimizer 函数,只需 #include 头文件 meshoptimizer.h;库源码是 C++,但头文件是 C 兼容的。
Core pipeline
When optimizing a mesh, to maximize rendering efficiency you should typically feed it through a set of optimizations (the order is important!): Indexing, Vertex cache optimization (optional), Overdraw optimization, Vertex fetch optimization, Vertex quantization, Index filtering (optional), Shadow indexing.
核心流水线 在优化网格时,为了最大化渲染效率,通常需要通过一系列优化步骤(顺序很重要!):索引化、顶点缓存优化(可选)、过度绘制(Overdraw)优化、顶点获取优化、顶点量化、索引过滤(可选)、阴影索引化。
Indexing
Most algorithms in this library assume that a mesh has a vertex buffer and an index buffer. For algorithms to work well and also for GPU to render your mesh efficiently, the vertex buffer has to have no redundant vertices; you can generate an index buffer from an unindexed vertex buffer or reindex an existing (potentially redundant) index buffer as follows: Note: meshoptimizer generally works with 32-bit (unsigned int) indices, however when using C++ APIs you can use any integer type for index data by using the provided template overloads. By convention, remap tables always use unsigned int.
索引化
该库中的大多数算法都假设网格具有顶点缓冲区和索引缓冲区。为了使算法良好运行并让 GPU 高效渲染网格,顶点缓冲区必须没有冗余顶点;你可以从非索引顶点缓冲区生成索引缓冲区,或者按如下方式对现有的(可能存在冗余的)索引缓冲区进行重新索引:注意:meshoptimizer 通常使用 32 位(unsigned int)索引,但在使用 C++ API 时,你可以通过提供的模板重载为索引数据使用任何整数类型。按照惯例,重映射表始终使用 unsigned int。
Vertex cache optimization
When the GPU renders the mesh, it runs the vertex shader for each vertex. Historically, GPUs used a small fixed-size post-transform cache (16-32 vertices) with different replacement policies to store the shader output and avoid redundant shader invocations. Modern GPUs still perform vertex reuse, but with substantially different mechanics: vertex invocations are batched into thread groups based on the input indices, and effective reuse depends on factors like vertex shader outputs and rasterizer through.
顶点缓存优化 当 GPU 渲染网格时,它会为每个顶点运行顶点着色器。从历史上看,GPU 使用一个小型固定大小的变换后缓存(16-32 个顶点)以及不同的替换策略来存储着色器输出,从而避免冗余的着色器调用。现代 GPU 仍然执行顶点重用,但机制大不相同:顶点调用根据输入索引被分批处理到线程组中,有效的重用取决于顶点着色器输出和光栅化器吞吐量等因素。