这是用户在 2024-6-28 11:14 为 https://www.poiyomi.com/blog/2022-10-17-texture-optimization 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skip to main content

Optimizing Texture Memory (VRAM) & Size

· 12 min read
Razgriz
Poiyomi Docs Author

2023-01-30: Updated with additional info, clarifications, and organization
2023-01-30:更新了附加信息、澄清说明及组织结构

TL;DR
简而言之
  • MEASURE TEXTURE VRAM USAGE AND DOWNLOAD SIZE using lox9973's Assetbundle Stat tool and Thry's VRAM estimator.
    使用 lox9973 的 Assetbundle Stat 工具和 Thry 的 VRAM 估算器测量纹理 VRAM 使用量及下载大小。
  • Reduce Resolution in the texture import settings! Not every texture needs to be 2k or 4k.
    在纹理导入设置中降低分辨率!并非每个纹理都需要 2K 或 4K。
  • Don't disable mipmaps - Your materials will look and run better with them on!
    不要禁用 mipmaps——启用它们会让你的材质看起来和运行得更好!
  • Crunch doesn't change VRAM usage but can reduce download size. Not a magic bullet!
    Crunch 不会改变显存使用量,但能减小下载大小。并非万能良方!
  • For Normal Maps, Use RG Compressed BC5 (From Platform overrides menu1). Normal Quality (DXT5nm) and High Quality (BC7) use the same VRAM but look worse.
    对于法线贴图,请使用 RG 压缩的 BC5(从平台覆盖菜单 1 选择)。普通质量(DXT5nm)和高品质(BC7)虽然占用相同的显存,但视觉效果较差。
  • For textures with Alpha, Use High Quality (BC7). Normal Quality (DXT5) uses the same VRAM but looks worse.
    对于带有 Alpha 通道的纹理,请使用高质量(BC7)。普通质量(DXT5)虽然占用相同的显存,但视觉效果较差。
  • Don't use JPG (or other lossy formats) to store your source textures! Lower quality, with no benefit to VRAM/Download Size.
    不要使用 JPG(或其他有损格式)来存储源纹理!质量降低,对 VRAM/下载大小无益。

Background
背景

In VRChat Creation, texture assets comprise a large portion (often the majority!) of map/avatar video memory (VRAM) usage, and a significant chunk of map/avatar download size (assetbundle size). In the latest update, VRChat has added an estimate of texture memory to the avatar stats menu, as it's an important component of GPU performance.
在 VRChat 创作中,纹理资源占据了地图/虚拟角色视频内存(VRAM)使用的大部分(通常是绝大部分!),同时也是地图/虚拟角色下载大小(资源包大小)的重要组成部分。在最新更新中,VRChat 已在虚拟角色统计菜单中增加了纹理内存的预估值,因为它是 GPU 性能的重要组成部分。

When the GPU can't store more assets in its memory, it has to swap them back and forth to the system memory (RAM). This works fine, but at best slows things down and causes hitching, and at worst, it can cause terribly low framerates and crashing.
当 GPU 无法在其内存中存储更多资源时,它必须将这些资源在系统内存(RAM)之间来回交换。这样做虽然可行,但最佳情况下会减慢速度并导致卡顿,最坏情况下则可能导致极低的帧率和崩溃。

Here's some notes on optimizing texture size, on both worlds and avatars, to reduce VRAM usage and download size. These are geared toward PC optimization, but outside of specific formats, they apply to mobile platforms (like Quest) as well.
以下是关于优化世界和角色纹理大小的一些笔记,旨在减少显存使用和下载大小。这些内容主要针对 PC 优化,但除特定格式外,也适用于移动平台(如 Quest)。

Compression Stages
压缩阶段

In the VRC creation pipeline, textures are compressed multiple times: when they're imported to Unity, to a gpu-readable format (determining the VRAM size), and when an assetbundle is built, with LZMA compression (kind of like a zip archive!), which reduces download size. Technically, there's also the source asset's compression (most formats have some, for efficiency), but as long as it's lossless, we don't need to care about it.
在 VRC 创作流程中,纹理会经历多次压缩:导入 Unity 时,转换为 GPU 可读格式(决定显存大小);构建资源包时,采用 LZMA 压缩(类似 ZIP 压缩包!),以减小下载体积。从技术上讲,源资产本身也存在压缩(多数格式为提高效率而自带),但只要是无损的,我们无需过多关注。

Knowing this, we can safely store source assets at their original resolution, losslessly (using formats like PSD, TGA, PNG, etc - don't use lossy ones like JPG, especially with data textures like normal maps!). The source asset file size on disk doesn't matter as the image data will be re-encoded in its entirety.
了解这一点后,我们可以安全地以原始分辨率无损存储源资产(使用 PSD、TGA、PNG 等格式——不要使用 JPG 等有损格式,尤其是对于法线贴图等数据纹理!)。源资产在磁盘上的文件大小并不重要,因为图像数据将完整地重新编码。

Compression Formats
压缩格式

GPU texture formats almost always have a fixed amount of data per pixel when loaded in memory. This means that (for example) a square BC7 texture at 512px with mipmapping on will always be 341.4 KB in VRAM, no matter what the texture data contains. This isn't as true for download sizes - LZMA compression can be more or less effective on different data, but we don't have as much control over that.
GPU 纹理格式在内存中加载时几乎总是具有固定数量的数据每像素。这意味着(例如)一个 512px 的 BC7 方形纹理,启用 mipmapping 后,在 VRAM 中的大小始终是固定的,无论纹理数据包含什么内容。下载大小则并非如此——LZMA 压缩对不同数据的效率可能高低不一,但我们对此控制有限。

For non-crunched textures, the size in the texture preview will give the VRAM usage of an imported texture asset. For crunched textures, the preview size doesn't match VRAM usage! the usage will be the same as "Normal Quality" (DXT1/DXT5) for that texture. VRAM usage can be calculated using the Bits per pixel for a given format, which can be found in the Unity Documentation.
对于非压缩纹理,纹理预览中的大小将给出导入纹理资产的显存使用量。对于压缩纹理,预览大小并不匹配显存使用量!使用量将与该纹理的“正常质量”(DXT1/DXT5)相同。显存使用量可通过给定格式的每像素位数来计算,该信息可在 Unity 文档中找到。

You can use the preview and game build views as a guide for (non-crunched) VRAM, and a very rough indicator of disk size, but verify with lox9973's Assetbundle Stat tool to confirm both VRAM usage contribution and final compressed bundle size. Thry's VRAM estimator and the VRAM estimation built into ThryEditor (used in Poiyomi) are also useful for this.
您可以利用预览和游戏构建视图作为非压缩 VRAM 的参考,以及磁盘大小的大致指示,但务必使用 lox9973 的 Assetbundle Stat 工具来验证 VRAM 使用量和最终压缩包大小的贡献。Thry 的 VRAM 估算器以及 ThryEditor 内置的 VRAM 估算功能(Poiyomi 中使用)对此也非常有用。

Crunch and You
咀嚼与你

Crunch compression is frequently talked about around the topic of avatar and world optimization. When optimizing filesize, you can use crunch compression for things that you can afford to lose quality on, and verify visually that it still looks OK. When using Crunch, test different crunch quality levels and measure the difference it makes in visual quality and filesize, and choose what works out best for your application.
在讨论虚拟形象和世界优化的话题时,常常会提及 Crunch 压缩。在优化文件大小方面,对于那些可以接受质量损失的内容,可以使用 Crunch 压缩,并通过视觉检查确认效果仍然可接受。使用 Crunch 时,应测试不同的压缩质量级别,衡量其在视觉质量和文件大小上产生的差异,并选择最适合您应用的方案。

Crunch compression does not affect VRAM usage.
压缩处理不影响显存使用。

Crunch pre-compresses textures in a way that's easy for the CPU to decompress at runtime, so it brings disk size down a bit (though not as much as it would appear from the preview, since there's assetbundle LZMA compression too!), but it always has to be uncrunched into DXT1 or DXT5 anyway.
Crunch 预压缩纹理,使其在运行时易于 CPU 解压缩,从而略微减小磁盘大小(尽管由于资产包 LZMA 压缩的存在,实际减小幅度不如预览所示),但无论如何,它最终都需要解压缩为 DXT1 或 DXT5 格式。

Crunch Compression Comparison

Left-to-Right: DXT1 Crunch (25 Quality), DXT1 Crunch (75 Quality), DXT1, BC7 (Open for full quality)
从左至右: DXT1 嘎吱脆(25 品质), DXT1 嘎吱脆(75 品质), DXT1BC7 (展开查看完整品质)

Recommendations
推荐建议

Different textures have different requirements. Here's some recommendations of what to use for what situation:
不同材质有不同要求。以下是针对不同情况的一些建议:

Color Textures
彩色纹理

  • DXT1Crunch/DXT5Crunch ("Use Crunch Compression") for simple colors that can take it, if you need the disk space. Always look at the visual quality, and again, Crunch does not affect VRAM usage, so this will use the same memory as "Normal Quality".
    对于能承受压缩的简单颜色,若需节省磁盘空间,可使用“使用 Crunch 压缩”功能。务必关注视觉效果,再次强调,Crunch 不影响显存使用,因此其内存消耗与“正常质量”相同。
  • DXT1 ("Normal Quality") for simple color textures. This setting should not be used if the source has an alpha channel, or if it contains smooth gradients - skin textures often look especially bad, as well as anything that's being hue shifted.
    DXT1 (“普通质量”)适用于简单的彩色纹理。如果源文件包含 Alpha 通道或平滑渐变,则不应使用此设置——皮肤纹理以及任何色调调整的内容通常看起来效果很差。
  • BC7 ("High Quality") for anything else, especially if it has an alpha channel. It's higher quality and uses 8 bpp instead of 4, so textures using BC7 have twice the VRAM usage of ones using DXT1, so as noted above, consider using High Quality at half the resolution instead of Normal Quality at the "full" resolution, as it may be an acceptable tradeoff.
    对于其他任何情况,尤其是包含 Alpha 通道的图像,推荐使用“高质量”( BC7 )。它采用 8 位每像素而非 4 位,因此使用 BC7 的纹理在显存占用上是使用 DXT1 的两倍。如上所述,考虑使用高质量模式下的半分辨率,而非正常质量模式下的“全”分辨率,这可能是一个可接受的权衡。

Note that setting a texture to "High Quality" will override the crunch compression setting.
请注意,将纹理设置为“高质量”将覆盖压缩设置。

If you are using an alpha channel, either use BC7 ("High Quality") or crunch it (DXT5), depending on the application. using "Normal Quality" on a texture with an alpha channel is just a waste, as the DXT5 format it uses has the same VRAM usage as BC7. If your source texture has an alpha channel, but you don't need alpha, you can set "Alpha Source" to "None" in the texture import settings, which will let you either use DXT1 ("Normal quality") or BC7 with higher quality.
若使用 Alpha 通道,根据应用场景,可选择 BC7 (“高质量”)或进行压缩( DXT5 )。对含 Alpha 通道的纹理使用“普通质量”实属浪费,因其采用的 DXT5 格式与 BC7 在显存占用上相同。若源纹理含 Alpha 通道但无需透明度,可在纹理导入设置中将“Alpha 来源”设为“无”,这样便可选用 DXT1 (“普通质量”)或更高品质的 BC7

Normal Maps
法线贴图

  • DXTnm Crunch ("Use Crunch Compression") for normal maps that do not need to be high quality. Crunch is not ideal for normal maps, so it often results in low quality results.
    对于不需要高质量的普通贴图,可使用 DXTnm Crunch (“使用 Crunch 压缩”)。Crunch 并非普通贴图的理想选择,因此往往会导致质量较低的结果。
  • When not crunching, always use BC51 or alternatively BC7 ("High Quality") for Normal maps. BC7 can cause some artfiacts and banding that BC51, which stores the R and G channels as two separate BC4 channels, can help reduce. The default setting of DXT5/DXTnm ("Normal Quality") uses the same memory but looks worse!
    在非压缩状态下,始终使用 BC5 1 或可选的 BC7 (“高质量”)来处理法线贴图。 BC7 可能导致一些艺术瑕疵和色带问题,而 BC5 1 ,它将 R 和 G 通道作为两个独立的 BC4 通道存储,有助于减少这些问题。 DXT5/DXTnm (“普通质量”)的默认设置虽然占用相同内存,但效果较差!

Data/Other textures
数据/其他纹理

  • DXT1 ("Normal Quality") for simple masks that don't have a lot of edges or high frequency detail.
    DXT1 (“普通质量”)适用于边缘不多或高频细节较少的简单面具。
  • BC7 for high-detail masks
    高细节面罩专用 BC7
  • BC4/BC51 can be used for specific use cases, like single and dual channel masks. These formats can give high quality when you only need specific channels.
    BC4 / BC5 1 可用于特定用例,如单通道和双通道掩模。当您仅需特定通道时,这些格式能提供高质量效果。
  • "None" for very small (smaller than 256px) textures that benefit from storing exact colors, like color ramps used for lighting and small gradients. These formats tend to be large, so be careful!
    对于极小(小于 256 像素)且能从存储精确颜色中获益的纹理,如用于照明和微小渐变的色阶,建议使用"无"格式。这些格式往往体积较大,使用时需谨慎!

Reducing Resolution
降低分辨率

info
信息

To optimize both VRAM usage and download size, reduce resolution as much as possible for your application.
为了优化 VRAM 使用和下载大小,请尽可能降低应用程序的分辨率。

Textures in Unity are always re-encoded on import according to the texture import settings. That means you can non-destructively change the resolution size - you can always set the resolution in the texture import settings lower to optimize, without modifying the source texture. The vast majority of materials don't need high resolution textures (4k+) to look good.
Unity 中的纹理在导入时总是会根据纹理导入设置重新编码。这意味着你可以非破坏性地改变分辨率大小——你始终可以在纹理导入设置中将分辨率调低以优化,而不会修改源纹理。绝大多数材质并不需要高分辨率纹理(4k+)就能看起来很好。

Every time you halve the resolution of an image, you reduce the amount of pixels it has by a quarter - that means that while a square 4k BC7-encoded texture may take over 21MB in memory, the 2k version would be just a bit over 5MB!
每次将图像分辨率减半,其像素数量就会减少四分之一——这意味着一个 4k 的 BC7 编码纹理在内存中可能占用超过 21MB,而 2k 版本则仅略超 5MB!

It's important to note that while reducing resolution makes textures lower-fidelity up close, they will often look exactly the same at from even a little distance away, due to screen/headset resolution being limited. This is why AAA games don't use super high resolution textures everywhere - even in the most advanced modern games. Zoom out a bit when adjusting resolution - it might have less of an impact than you think!
值得注意的是,虽然降低分辨率会使近距离的纹理保真度下降,但由于屏幕/头显分辨率的限制,这些纹理在稍远距离处往往看起来完全相同。这也是为什么 AAA 级游戏并不会在所有地方都使用超高分辨率纹理,即使在最先进的现代游戏中也是如此。在调整分辨率时,不妨稍微拉远视角——其影响可能没有你想象的那么大!

If choosing between lower quality compression at higher resolution, and higher quality compression at lower resolution (ex. Normal Quality/Crunched at 2k vs High Quality at 1k), the latter option will often look better at all but the closest distance, due to how mipmapping works. This reduces VRAM usage, keeps download size roughly the same, and can improve the relative quality! Tools like the texture compression analysis tool included with d4rk's Avatar Optimizer can make this easier to analyze.
若在低质量高分辨率压缩与高质量低分辨率压缩(例如,2k 分辨率下正常质量/压缩与 1k 分辨率下高质量)之间选择,由于 mip 映射的工作原理,后者在除极近距离外的所有距离上通常看起来更佳。此举能减少显存使用,保持下载大小大致不变,并可能提升相对质量!诸如 d4rk 的 Avatar 优化器附带的纹理压缩分析工具等工具,可让这一分析过程更为简便。

To maintain close-range details, one technique is to use low resolution base textures and tiled, low-resolution detail textures (especially normals). This lets you use multiple much smaller textures instead of a single larger one.
为了保持近距离细节,一种技术是使用低分辨率基础纹理和拼贴的低分辨率细节纹理(尤其是法线贴图)。这样可以让您使用多个小得多的纹理,而不是单一的大纹理。

If you're having trouble maintaining details (lines or designs get blurry when lowering resolution), the mesh amy simply not have good texel density. This can be improved by improving the UV mapping of the mesh. Read more about this topic here and here.
若您在降低分辨率时发现细节(线条或图案)变得模糊,这可能是因为网格的纹理像素密度不佳。改善这一问题可通过优化网格的 UV 贴图来实现。更多相关信息,请参阅此处和此处。

PBR Textures
PBR 材质纹理

Textures used for PBR (Physically Based Rendering) materials often fit into 3 categories: Color, Normal, and Data. While it's beneficial to reduce res in general, PBR data textures can be an especially good place to reduce resolution.
PBR(基于物理的渲染)材料所用的纹理通常分为三类:颜色、法线和数据。虽然总体上降低分辨率是有益的,但 PBR 数据纹理尤其适合降低分辨率。

  • Color: textures that directly use the color for the material appearance, like Base Color (diffuse/albedo) and emission map textures.
    颜色:直接使用颜色来表现材质外观的纹理,如基础色(漫反射/反照率)和发射贴图纹理。
  • Normal: Normal maps, used to modify how the surface responds to light.
    普通:法线贴图,用于修改表面如何响应光线。
  • Data: textures that contain data, like Metallic, Smoothness/Glossiness/Glossiness, Ambient Occlusion (AO) maps, and Height maps.
    数据:包含数据的纹理,如金属度、光滑度/光泽度/光泽、环境光遮蔽(AO)贴图和高度贴图。

Color and normal textures often benefit directly from higher resolution, but you can often get away with a lot less resolution for data textures - try half (or a quarter) of the res of the main (color/normal) textures.
颜色和常规纹理通常直接受益于更高的分辨率,但对于数据纹理,你往往可以用更低的分辨率——尝试使用主纹理(颜色/法线)分辨率的一半(或四分之一)。

Overall Usage Guidelines
总体使用指南

In terms of what overall usage to shoot for, we can think in terms of average GPU VRAM. According to the September 2022 Steam Hardware Survey, the most common VRAM amount for steam users is 8 GB, with about 35-40% of users having 8 GB or more - we'll use 8 GB as our reference number.
在考虑整体使用目标时,我们可以以平均 GPU 显存为参考。根据 2022 年 9 月的 Steam 硬件调查,Steam 用户中最常见的显存容量是 8GB,约有 35-40%的用户拥有 8GB 或更多显存——我们将以 8GB 作为参考数值。

Doing some quick-and-dirty math, in a an instance with 80 avatars, if someone were to show every avatar and have it in view, each model would need to be under 100 MB of VRAM usage to stay below 8 GB total. That's before accounting for overhead from the operating system, the game itself, and the world hosting the instance. With that in mind, optimized avatars should shoot for under this - I usually aim for 80 MB or less on optimized avatars, but higher can be ok on models that will be used in less crowded spaces.
快速粗略计算一下,在 80 个虚拟形象的场景中,若要展示所有形象并使其可见,每个模型需控制在 100MB 以内的显存使用量,才能确保总显存不超过 8GB。这还未计入操作系统、游戏本身及实例托管世界的额外开销。考虑到这些因素,优化后的虚拟形象应力求低于此标准——我通常将优化后的形象设定在 80 MB 或更少,但在人流量较少的区域,模型占用稍高一些也是可以接受的。

For worlds, it depends entirely on the content and intended usecase of the world. If you're building an event or party venue that's intended to host 80 people and heavy effects, you'll want to drop VRAM usage as low as it can go. The same is true for mobile-optimized worlds. However, for a more atmospheric world, intended to be experienced in small groups or alone, or where the focus is on beautiful, rich environments, higher resource usage can be justified. Exercise judgement and keep low-spec users in mind.
对于虚拟世界而言,完全取决于其内容和预期用途。若您正在构建一个旨在容纳 80 人并伴有强烈效果的活动或派对场所,应尽可能降低显存使用。移动端优化的世界亦是如此。然而,对于更注重氛围、适合小团体或个人体验,或以美丽丰富的环境为重点的世界,较高的资源使用则可被接受。运用判断力,并考虑低配置用户的需求。

Don'ts
注意事项

Don't Disable Mipmaps
不要禁用 Mipmaps

Seriously, don't disable mipmaps!
真的,千万别禁用 MIP 贴图!

Mipmaps improve both visual quality and performance. Disabling them isn't actually beneficial.
Mipmaps 提升视觉质量和性能,禁用它们实际上并无益处。

You should only disable mipmaps if it's something like a lookup table (LUT) or data texture (ex. font textures) that always needs the full texture resolution. While disabling them may reduce VRAM usage, it's not worth the hit to performance, and will make things look worse.
仅应在类似查找表(LUT)或数据纹理(如字体纹理)这类始终需要全纹理分辨率的情况下禁用 mipmap。虽然禁用它们可能会减少显存使用,但性能损失不值得,且会使画面效果变差。

Please don't turn off mipmaps. Image courtesy Ben Golus
请勿关闭 mip 映射。图片由 Ben Golus 提供

Don't just look at the build report
不要只看构建报告

Unity's editor log will contain a breakdown of the build process, which some tools break out into a nice interface. While this can be useful, it won't be entirely accurate in describing how much space assets take up in the final bundle.
Unity 编辑器的日志将包含构建过程的详细分解,某些工具将其呈现为友好的界面。虽然这可能很有用,但它并不能完全准确地描述资源在最终包中占用的空间大小。

Don't use Hue Shifts with DXT1
不要使用 DXT1 的色调偏移

This comes from a subtle technical detail - due to how DXT1 represents colors, it ends up having a limited hue palette in the red and blue channels. As a result, hue shifts applied to DXT1 encoded color textures can end up with blocky and bad results. (thanks to d4rkplayer for pointing this out!)
这源于一个微妙的技术细节——由于 DXT1 表示颜色的方式,它在红色和蓝色通道中最终拥有有限的色调调色板。因此,应用于 DXT1 编码颜色纹理的色调偏移可能会导致块状且效果不佳。(感谢 d4rkplayer 指出这一点!)

Conclusions
结论

Choosing to optimize your creations benefits everyone that uses and experiences them, and improves the platform in general - when everything runs better, everything feels better.
选择优化您的创作,惠及每一位使用和体验它们的人,并普遍提升平台质量——当一切运行得更顺畅时,一切感受也更美好。

Always measure and profile your usage. Tools like Lox9973's Assetbundle Stat tool, Thry's VRAM estimator, and d4rk's Avatar Optimizer (and the included texture compression analysis tool) are massively helpful in creating more optimized content.
始终测量和分析您的使用情况。诸如 Lox9973 的 Assetbundle Stat 工具、Thry 的 VRAM 估算器以及 d4rk 的 Avatar 优化器(及其包含的纹理压缩分析工具)等工具,在创建更优化内容方面极为有用。

Recognize that how materials, and their textures, are viewed is different between the creator building it (and often heavily self-scrutinizing) and other people that experience it. Make sure to step back and recognize that not everyone is going to be looking at the tiny details - focus on what matters in reasonable use cases.
认识到材料及其质感在创作者构建时(通常会进行严格的自我审视)与他人体验时的看法是不同的。务必退后一步,意识到并非每个人都会关注微小的细节——应专注于合理使用场景中真正重要的方面。


Footnotes
脚注

  1. Some formats will need to be set using the platform-specific overrides. To do this, use the platform-specific overrides dialog in the texture importer. Note that this (as the title implies) overrides the default import settings. 2 3 4
    某些格式需通过平台特定覆盖来设置。为此,请在纹理导入器中使用平台特定覆盖对话框。请注意,这(正如标题所示)会覆盖默认的导入设置。