一、Mipmap 基础
项目 | 说明 |
---|---|
目的 | 为纹理预先生成多级低分辨率版本, 减少远距离采样时的锯齿与摩尔纹, 并提升缓存命中与性能。 |
金字塔层级 | 若原图尺寸为 , 第 级通常为 , 直到 1x1。 |
存储开销 | 各级面积总和约为原图的 (更精确为 ) , 因此总纹理内存约为原图的 。 |
1. Mipmap Basics
Item | Description |
---|---|
Purpose | Pre-generate multiple lower-resolution versions of textures to reduce aliasing and moiré patterns during distant sampling, and improve cache hits and performance. |
Pyramid Levels | If the original image size is , the -th level is usually , down to 1x1. |
Storage Overhead | The total area of all levels is about of the original image (more precisely, ), so the total texture memory is about of the original image. |
二、常见降采样算法 (Downsampling)
算法/类型 | 做法 | 优点 | 缺点/备注 |
---|---|---|---|
Box Filter (均值/箱式滤波) | 每2x2像素取平均生成下一层。公式: | 实现简单、速度快 | 频率滚降不理想, 易产生走样; 对高频纹理不够平滑 |
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/Type | Approach | Advantages | Disadvantages/Notes |
---|---|---|---|
Box Filter (Mean/Box Filter) | Average every 2x2 pixels to generate the next level. Formula: | Simple, fast | Poor 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 kernel | Smoother than box, slightly better edge preservation | Slightly higher cost than box |
Gaussian Filter | Apply Gaussian blur before downsampling | Closer to ideal low-pass, better anti-aliasing | Heavier computation, may blur details |
Lanczos (Windowed sinc) | Resample using Lanczos kernel (commonly a=2 or 3) | Sharp and excellent anti-aliasing | Highest cost, may cause slight ringing |
Mitchell-Netravali / Catmull-Rom (Cubic Family) | Resample with cubic convolution kernel | Good balance of sharpness and smoothness, moderate cost | Common in offline pipelines, texture baking |
Gamma/Color Space Handling | Average 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 Specific | Transform to tangent space unit vectors, accumulate then normalize, remap offset if needed. Can weight (e.g. Z or curvature) to reduce bias. | Physically correct normals | Cannot 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: 。应用可加偏移(LOD bias)控制锐度与噪点权衡。 | 可调节锐度 | - |
3. Common Runtime Filtering Algorithms
Algorithm/Type | Approach | Advantages | Disadvantages/Notes |
---|---|---|---|
Nearest Mipmap Nearest (MMN) | Selects the nearest mip level, uses nearest point sampling | Fastest | Blocky appearance |
Nearest Mipmap Linear (NML) | Selects the nearest mip level, uses bilinear sampling | Smoother than MMN | May jump when switching levels |
Linear Mipmap Nearest (LMN) | Samples two adjacent mip levels with nearest point, then linearly interpolates | - | Rarely used alone |
Trilinear Filtering | Bilinear sampling on two levels, then linear interpolation by LOD fraction | Good balance, commonly default | - |
Anisotropic Filtering | Multi-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 views | Cost increases with level |
LOD Control & Bias | Hardware estimates LOD by screen-space derivatives: . 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
Type | Description |
---|---|
Ripmaps | Independently 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 Textures | Tile-based storage and on-demand loading of mip tiles. MegaTexture, Tiled Resources. |
Specular/Gloss mip chain | Pre-filter roughness-related maps by physical model (e.g. prefiltered mipmap for environment reflection, BRDF-Convolved Env Map). |
Alpha Coverage Preserving Mipmaps | Adjust alpha during downsampling of transparent textures to preserve coverage, reduce distant flicker. |
五、工程实践建议
实时渲染(游戏引擎)
- 默认三线性+各向异性过滤(8x/16x)
- LOD bias适度负向增强锐度但注意闪烁
- 生成链: 线性空间、预乘alpha、至少使用box/triangle, 重要资产用Gaussian/Mitchell
- 法线贴图用“单位向量平均后归一化”策略
移动/嵌入式
- 优先box/triangle以控成本
- 必要时提高各向异性等级替代昂贵生成核
电影/离线
- 使用Lanczos或Mitchell-Netravali
- 严格在线性空间处理, 颜色管理到位
5. Engineering Practice Recommendations
Real-time Rendering (Game Engines)
- Default: trilinear + anisotropic filtering (8x/16x)
- LOD bias: slightly negative for sharpness, beware of flicker
- Generation chain: linear space, premultiplied alpha, at least box/triangle; important assets use Gaussian/Mitchell
- Normal maps: use "average unit vector then normalize" strategy
Mobile/Embedded
- Prefer box/triangle to control cost
- Increase anisotropy level if needed instead of expensive generation kernels
Film/Offline
- Use Lanczos or Mitchell-Netravali
- Strictly process in linear space, ensure color management
常见问题
- 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.