常见的 Mipmap 算法总结

Summary of Common Mipmap Algorithms

Aug 19, 25

一、Mipmap 基础

项目说明
目的为纹理预先生成多级低分辨率版本, 减少远距离采样时的锯齿与摩尔纹, 并提升缓存命中与性能。
金字塔层级若原图尺寸为 (W×H)(W \times H), 第 (l)(l) 级通常为 (max(1,W/2l)×max(1,H/2l))(\max(1, \lfloor W/2^l \rfloor) \times \max(1, \lfloor H/2^l \rfloor)), 直到 1x1。
存储开销各级面积总和约为原图的 (13)(\frac{1}{3}) (更精确为 (k=14k=13)(\sum_{k=1}^{\infty} 4^{-k} = \frac{1}{3})) , 因此总纹理内存约为原图的 (43)(\frac{4}{3})

1. Mipmap Basics

ItemDescription
PurposePre-generate multiple lower-resolution versions of textures to reduce aliasing and moiré patterns during distant sampling, and improve cache hits and performance.
Pyramid LevelsIf the original image size is (W×H)(W \times H), the (l)(l)-th level is usually (max(1,W/2l)×max(1,H/2l))(\max(1, \lfloor W/2^l \rfloor) \times \max(1, \lfloor H/2^l \rfloor)), down to 1x1.
Storage OverheadThe total area of all levels is about (13)(\frac{1}{3}) of the original image (more precisely, (k=14k=13)(\sum_{k=1}^{\infty} 4^{-k} = \frac{1}{3})), so the total texture memory is about (43)(\frac{4}{3}) of the original image.

二、常见降采样算法 (Downsampling)

算法/类型做法优点缺点/备注
Box Filter (均值/箱式滤波)每2x2像素取平均生成下一层。公式:C=14i=01j=01C2x+i,2y+jC'=\frac{1}{4}\sum_{i=0}^{1}\sum_{j=0}^{1}C_{2x+i,2y+j}实现简单、速度快频率滚降不理想, 易产生走样; 对高频纹理不够平滑
Triangle/Bilinear Filter (线性核)用线性权重低通滤波后降采样, 或用三角核比box更柔和, 边缘保留略好成本略高于box
Gaussian Filter (高斯滤波)先做高斯模糊再降采样更接近理想低通, 抑制混叠更好计算较重, 可能模糊细节
Lanczos (窗化sinc)使用Lanczos核(常见a=2或3)重采样锐利且抑制混叠优秀成本最高, 可能出现轻微振铃(ringing)
Mitchell-Netravali / Catmull-Rom (三次插值家族)三次卷积核重采样清晰度与平滑度平衡良好, 成本中等常用于离线内容管线、纹理烘焙阶段
Gamma/色彩空间处理线性空间平均, 避免在sRGB空间直接平均导致偏暗。流程:sRGB→线性; 滤波/降采样; 线性→sRGB。Alpha处理:建议用预乘alpha, 再滤波降采样。保证色彩物理一致-
法线贴图专用反变换到切线空间单位向量、累加后归一化、必要时重映射偏移。可加权(如Z分量或曲率权重)减少偏差。保证法线物理一致不能直接对RGB法线做线性平均

2. Common Downsampling Algorithms

Algorithm/TypeApproachAdvantagesDisadvantages/Notes
Box Filter (Mean/Box Filter)Average every 2x2 pixels to generate the next level. Formula: C=14i=01j=01C2x+i,2y+jC' = \frac{1}{4}\sum_{i=0}^{1}\sum_{j=0}^{1} C_{2x+i,2y+j}Simple, fastPoor frequency roll-off, prone to aliasing; not smooth for high-frequency textures
Triangle/Bilinear Filter (Linear Kernel)Downsample after low-pass filtering with linear weights, or use triangle kernelSmoother than box, slightly better edge preservationSlightly higher cost than box
Gaussian FilterApply Gaussian blur before downsamplingCloser to ideal low-pass, better anti-aliasingHeavier computation, may blur details
Lanczos (Windowed sinc)Resample using Lanczos kernel (commonly a=2 or 3)Sharp and excellent anti-aliasingHighest cost, may cause slight ringing
Mitchell-Netravali / Catmull-Rom (Cubic Family)Resample with cubic convolution kernelGood balance of sharpness and smoothness, moderate costCommon in offline pipelines, texture baking
Gamma/Color Space HandlingAverage in linear space to avoid darkening in sRGB. Process: sRGB → linear; filter/downsample; linear → sRGB. Alpha: use premultiplied alpha before filtering/downsampling.Physically correct color-
Normal Map SpecificTransform to tangent space unit vectors, accumulate then normalize, remap offset if needed. Can weight (e.g. Z or curvature) to reduce bias.Physically correct normalsCannot linearly average RGB normals

三、常见采样算法

算法/类型做法优点缺点/备注
Nearest Mipmap Nearest (MMN)选择最近的mip级, 使用最近点采样最快块状感明显
Nearest Mipmap Linear (NML)选择最近mip级, 使用双线性采样比MMN平滑跨级切换仍可能跳变
Linear Mipmap Nearest (LMN)在两个相邻mip级间做最近点采样, 再线性插值-很少单独使用
Trilinear Filtering (三线性过滤)对两级各做双线性采样, 在级间按LOD分数线性插值性价比高, 常用默认-
Anisotropic Filtering (各向异性)倾斜视角下沿主轴拉伸方向多点采样, 抑制斜视模糊与闪烁。与mipmap结合: 每个候选mip层沿各向扩展取样。参数: 各向异性等级(如4x,8x,16x)影响质量与性能。斜视下质量高成本随等级提升
LOD控制与偏移硬件根据屏幕空间导数估计LOD: λ=log2(max((u/x)2+(v/x)2, (u/y)2+(v/y)2))\lambda = \log_2 \left(\max \left(\sqrt{(\partial u/\partial x)^2 + (\partial v/\partial x)^2},\ \sqrt{(\partial u/\partial y)^2 + (\partial v/\partial y)^2}\right) \right)。应用可加偏移(LOD bias)控制锐度与噪点权衡。可调节锐度-

3. Common Runtime Filtering Algorithms

Algorithm/TypeApproachAdvantagesDisadvantages/Notes
Nearest Mipmap Nearest (MMN)Selects the nearest mip level, uses nearest point samplingFastestBlocky appearance
Nearest Mipmap Linear (NML)Selects the nearest mip level, uses bilinear samplingSmoother than MMNMay jump when switching levels
Linear Mipmap Nearest (LMN)Samples two adjacent mip levels with nearest point, then linearly interpolates-Rarely used alone
Trilinear FilteringBilinear sampling on two levels, then linear interpolation by LOD fractionGood balance, commonly default-
Anisotropic FilteringMulti-point sampling along major axis in oblique view, suppresses blur/flicker. With mipmap: each candidate mip level samples along anisotropic direction. Parameter: anisotropy level (e.g. 4x, 8x, 16x) affects quality and performance.High quality for oblique viewsCost increases with level
LOD Control & BiasHardware estimates LOD by screen-space derivatives: λ=log2(max((u/x)2+(v/x)2, (u/y)2+(v/y)2))\lambda = \log_2 \left(\max \left(\sqrt{(\partial u/\partial x)^2 + (\partial v/\partial x)^2},\ \sqrt{(\partial u/\partial y)^2 + (\partial v/\partial y)^2}\right) \right). Bias can be added to control sharpness/noise tradeoff.Adjustable sharpness-

四、特殊 mipmap 变体

类型说明
Ripmaps分别对u、v独立降采样得到矩形金字塔, 适合强各向缩放。
Summed-Area Tables (SAT)用于快速盒式滤波和可变尺寸区域平均, 存储开销大。
Sparse/Virtual Textures分块存储与按需加载mip tiles。MegaTexture、Tiled Resources。
Specular/Gloss mip chain对粗糙度相关贴图按物理模型进行预过滤(如环境反射的prefiltered mipmap, BRDF-Convolved Env Map)。
Alpha Coverage Preserving Mipmaps透明纹理降采样时调整alpha维持覆盖率, 减少远景稀疏闪烁。

4. Special Mipmap Variants

TypeDescription
RipmapsIndependently downsample u and v to get a rectangular pyramid, suitable for strong anisotropic scaling.
Summed-Area Tables (SAT)Used for fast box filtering and variable-size region averaging, high storage cost.
Sparse/Virtual TexturesTile-based storage and on-demand loading of mip tiles. MegaTexture, Tiled Resources.
Specular/Gloss mip chainPre-filter roughness-related maps by physical model (e.g. prefiltered mipmap for environment reflection, BRDF-Convolved Env Map).
Alpha Coverage Preserving MipmapsAdjust alpha during downsampling of transparent textures to preserve coverage, reduce distant flicker.

常见问题

  • sRGB空间做平均: 导致偏色、偏暗。
  • 直接平均法线或高光粗糙度贴图: 产生物理不一致。
  • 透明贴图不做coverage preserving: 远景闪烁。
  • mip级边界无缝处理: 生成时考虑wrap/clamp与边界像素复制, 避免接缝。

Common Issues

  • Averaging in sRGB space: causes color shift and darkening.
  • Directly averaging normal or specular roughness maps: leads to physical inconsistency.
  • Not preserving coverage for transparent textures: distant flicker.
  • Mipmap level boundary seams: handle wrap/clamp and border pixel copy during generation to avoid seams.