<p><em>&quot;How (hardware) texture compression works&quot;</em> is a large topic. Hopefully I can provide some insights without duplicating the content of <a href="https://computergraphics.stackexchange.com/questions/1523/how-does-hardware-texture-compression-work/1545#1545">Nathan's answer</a>.</p>
<h2>Requirements</h2>
<p>Texture compression typically differs from 'standard' image compression techniques e.g. JPEG/PNG in four main ways, as outlined in Beers et al's <a href="https://graphics.stanford.edu/projects/flashg/papers/texture_compression/" rel="nofollow noreferrer">Rendering from Compressed Textures</a>:</p>
<ol>
<li><p><em>Decoding Speed</em>: You don't want texture compression to be slower (at least not noticeably so) than using uncompressed textures. It should also be relatively simple to decompress since that can help achieve fast decompression without excessive hardware and power costs.</p>
</li>
<li><p><em>Random Access</em>: You can't easily predict which texels will be required during a given render. If some subset, <em>M</em>, of the accessed texels come from, say, the middle of the image, it's essential that you don't have to decode all of the 'previous' lines of the texture in order to determine <em>M</em>; with JPEG and PNG this is necessary as pixel decoding depends on the previously decoded data.<br />
Note, having said this, just because you have &quot;random&quot; access, <a href="https://computergraphics.stackexchange.com/questions/357/is-using-many-texture-maps-bad-for-caching/419#419">doesn't mean you should try to sample completely arbitrarily</a></p>
</li>
<li><p><em>Compression Rate and Visual Quality</em>: Beers et al argue (convincingly) that losing some quality in the compressed result in order to improve compression rate is a worthwhile trade-off. In 3D rendering, the data is probably going to be manipulated (e.g. filtered &amp; shaded etc) and so some loss of quality may well be masked.</p>
</li>
<li><p><em>Asymmetric encoding/decoding</em>: Though perhaps slightly more contentious, they argue that it is acceptable to have the encoding process much slower than the decoding. Given that the decoding needs to be at HW fill rates, this is generally acceptable. (I will admit that compression of PVRTC, ETC2 and some others at maximum quality could be faster)</p>
</li>
</ol>
<h2>Early History &amp; Techniques</h2>
<p>It may surprise some to learn that texture compression has been around for over four decades. Flight simulators from the 70s and 80s needed access to <em>relatively</em> large amounts of texture data and given that <a href="http://www.jcmit.com/memoryprice.htm" rel="nofollow noreferrer">1MB of RAM in 1980 was &gt; $6000</a>, reducing the texture footprint was essential. As another example, in the mid 70s, even a small amount of high speed memory and logic, e.g. enough for <a href="https://en.wikipedia.org/wiki/Framebuffer#History" rel="nofollow noreferrer">a modest 512x512 RGB frame buffer</a>) could set you back the price of small house.</p>
<p>Though, AFAIK, not explicitly referred to as texture compression, in the literature and patents you can find references to techniques including:<br />
a. simple forms of mathematical/procedural texture synthesis,<br />
b. use of a single channel texture (e.g. 4bpp) that is then multiplied by a per-texture RGB value,<br />
c. YUV, and<br />
d. palettes (the literature suggesting use of <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.1334" rel="nofollow noreferrer">Heckbert's approach</a> to do the compression)</p>
<h2>Modelling Image Data</h2>
<p>As noted above, texture compression is nearly always lossy and thus the problem becomes one of trying to represent the important data in a compact way whilst disposing of the less significant information. The various schemes that will be described below all have an implicit 'parameterised' model that approximates the typical behaviour of texture data and of the eye's response.</p>
<p>Further, since texture compression tends to use fixed-rate encoding, the compression process usually includes a search step to find the set of parameters which, when fed in to the model, will generate a good approximation of the original texture. That search step, however, can be time consuming.<br />
<em>(With the possible exception of tools such as <a href="http://optipng.sourceforge.net/" rel="nofollow noreferrer">optipng</a>, this is another area where typical use of PNG &amp; JPEG differ from texture compression schemes)</em></p>
<p>Before progressing further, to help with further understanding of TC it's worth taking a look at <a href="https://en.wikipedia.org/wiki/Principal_component_analysis" rel="nofollow noreferrer">Principal Component Analysis (PCA)</a> - a very useful mathematical tool for data compression.</p>
<h3>Example texture</h3>
<p>To compare the various methods, we'll use the following image:</p>
<p><a href="https://i.sstatic.net/SM000.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SM000.png" alt="small lorikeet+text" /></a><br />
Note that this is a fairly tough image, especially for palette and VQTC methods as it spans much of the RGB colour cube and only 15% of the texels use repeated colours.</p>
<h2>PC and (post mid 90s) Console Texture compression</h2>
<p>To reduce data costs, some PC games and early games consoles also made use of palette images, which is a form of Vector Quantisation (VQ).
Palette-based approaches make the assumption that a given image only uses relatively small portions of the RGB(A) colour cube.  A problem with palette textures is that the compression rates for the achieved quality is generally rather modest. The example texture compressed to &quot;4bpp&quot; (using GIMP) produced<br />
<a href="https://i.sstatic.net/Vpv0t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Vpv0t.png" alt="enter image description here" /></a><br />
Note again that this is a relatively tough image for VQ schemes.</p>
<h3>VQ with larger vectors (e.g. 2bpp ARGB)</h3>
<p>Inspired by Beers et al, the Dreamcast console used VQ to encode 2x2  or even 2x4 pixel blocks with single bytes. While the &quot;vectors&quot; in the palette textures are 3 or 4 dimensional, the 2x2 pixel blocks can be considered to be 16 dimensional. The compression scheme assumes there is sufficient, approximate repetition of these vectors.</p>
<p>Even though VQ can achieve satisfactory quality with ~2bpp, the problem with these schemes is that it requires dependent memory reads: An initial read from the index map to determine the code for the pixel is followed by a second to actually fetch the pixel data associated with that code. Additional caches can help alleviate some of the incurred latency, but adds complexity to the hardware.</p>
<p>The example image compressed with the 2bpp Dreamcast scheme is
<a href="https://i.sstatic.net/8tnag.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8tnag.png" alt="2bpp VQ result" /></a>.
The index map is: <a href="https://i.sstatic.net/YNJww.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YNJww.png" alt="2bpp VQ index map" /></a></p>
<p>Compression of VQ data can be done in various ways however, <em>IIRC</em>, the above was done using PCA to derive and then partition the 16D vectors along the principal vector into 2 sets such that two representative vectors minimised the mean squared error. The process then recursed until 256 candidate vectors were produced.
A global <a href="https://en.wikipedia.org/wiki/K-means_clustering#Standard_algorithm" rel="nofollow noreferrer">k-means/Lloyd's algorithm</a> approach was then applied to improve the representatives.</p>
<h3>Colour Space Transformations</h3>
<p>Colour space transformations also make use of PCA noting that the global distribution of colour is often spread along a major axis with far less spread along the other axes. For YUV representations, the assumptions are that a) the major axis is often in the luma direction and that b) the eye is more sensitive to changes in this direction.</p>
<p>The 3dfx Voodoo system provided <a href="http://www.gamers.org/dEngine/xf3D/glide/glidepgm.htm" rel="nofollow noreferrer">&quot;YAB&quot;</a>, an 8bpp, &quot;Narrow Channel&quot; compression system that split each 8 bit texel into a 422 format, and applied a user selected colour transform to that data to map it into RGB. The main axis thus had 16 levels and the smaller axes, 4 each.</p>
<p>The S3 Virge chip had a slightly simpler, 4bpp, scheme that allowed the user to specify, for <em>the entire texture</em>, two end colours, which should lie on the principal axis, along with a 4bpp monochrome texture. The per-pixel value then blended the end colours with appropriate weights to produce the RGB result.</p>
<h3>BTC-based schemes</h3>
<p>Rewinding some number of years, Delp and Mitchell designed a simple (monochrome) image compression scheme called <a href="http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=1094560" rel="nofollow noreferrer"><em>Block Truncation Coding</em>, (BTC)</a>. This paper also included a compression algorithm but, for our purposes, we are mainly interested in the resulting compressed data and of the decompression process.</p>
<p>In this scheme, images are broken into, typically, 4x4 pixel blocks, which can be compressed independently with, in effect, a localised VQ algorithm. Each block is represented by two &quot;values&quot;, <em>a</em> and <em>b</em>, and a 4x4 set of index bits, which identify which of the two values to use for each pixel.</p>
<p><em>S3TC</em>:  4bpp RGB (+1bit alpha)<br />
Although several colour-variants of BTC for image compression were proposed, of interest to us is <a href="http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&amp;Sect2=HITOFF&amp;d=PALL&amp;p=1&amp;u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&amp;r=1&amp;f=G&amp;l=50&amp;s1=5,956,431.PN.&amp;OS=PN/5,956,431&amp;RS=PN/5,956,431" rel="nofollow noreferrer">Iourcha et al's S3TC</a>, some of which appears to be a rediscovery of the somewhat forgotten work of <a href="http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&amp;Sect2=HITOFF&amp;d=PALL&amp;p=1&amp;u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&amp;r=1&amp;f=G&amp;l=50&amp;s1=5046119.PN.&amp;OS=PN/5046119&amp;RS=PN/5046119" rel="nofollow noreferrer">Hoffert et al</a> that was used in Apple's Quicktime.</p>
<p>The original S3TC, without the DirectX variants, compresses blocks of either RGB or RGB+1bit Alpha to 4bpp. Each 4x4 block in the texture is replaced by two end colours, <em>A</em> and <em>B</em>, from which up to two other colours are derived by fixed-weight, linear blends. Further, each texel in the block has a 2-bit index that determines how to select one of these four colours.</p>
<p>For example the following is a 4x4 pixel section of the test image compressed with the AMD/ATI Compressenator tool. (<em>Technically it's taken from a 512x512 version of the test image but forgive my lack of time to update the examples</em>).<br />
<a href="https://i.sstatic.net/YCI3n.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YCI3n.png" alt="enter image description here" /></a><br />
This illustrates the compression process: The average and the principal axis of the colours are calculated.  A best fit is then performed to find two end points that 'lie on' the axis which, along with the two derived 1:2 and 2:1 blends (or in some cases a 50:50 blend) of those end points, that minimises the error.  Each original pixel is then mapped to one of those colours to produce the result.</p>
<p>If, as in this case, the colours are reasonable approximated by the principal axis, the error will be relatively low. However if, like in the neighbouring 4x4 block shown below, the colours are more diverse, the error will be higher.<br />
<a href="https://i.sstatic.net/d5dZ8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/d5dZ8.png" alt="enter image description here" /></a></p>
<p>The example image, compressed with the <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/the-compressonator/" rel="nofollow noreferrer">AMD Compressonator</a> produces:<br />
<a href="https://i.sstatic.net/ZFrSa.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZFrSa.png" alt="enter image description here" /></a></p>
<p>Since the colours are determined independently per-block, there can be discontinuities at block boundaries but, as long as the resolution is kept sufficiently high, these block artefacts may go unnoticed:<br />
<a href="https://i.sstatic.net/Fa779.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Fa779.png" alt="enter image description here" /></a></p>
<p><em>ETC1</em>:  4bpp RGB<br />
Ericsson Texture Compression also works with 4x4 blocks of texels but makes the assumption that, much like YUV, the principal axis of a local set of texels is often very strongly correlated with &quot;luma&quot;. The set of texels can then be represented by just an average colour and a highly quantised, scalar 'length' of the projection of the texels onto that assumed axis.</p>
<p>Since this reduces the data storage costs relative to say, S3TC, it allows ETC to introduce a partitioning scheme, whereby the 4x4 block is subdivided into a pair of horizontal 4x2 or vertical 2x4 sub-blocks. These each have their own average colour.
The example image produces:<br />
<a href="https://i.sstatic.net/cYOmr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cYOmr.png" alt="enter image description here" /></a><br />
The area around the beak also illustrates the horizontal and vertical partitioning of the 4x4 blocks.
<a href="https://i.sstatic.net/Uwd5F.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Uwd5F.png" alt="enter image description here" /></a></p>
<h3>Global+Local</h3>
<p>There are some texture compression systems that are a cross between global and local schemes, such as that of the distributed palettes of <a href="http://onlinelibrary.wiley.com/doi/10.1111/1467-8659.00420/abstract" rel="nofollow noreferrer">Ivanov and Kuzmin</a> or the method of <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.61.7956" rel="nofollow noreferrer">PVRTC</a>.</p>
<p><em>PVRTC</em>:  4 &amp; 2 bpp RGBA<br />
PVRTC assumes that an (in practice, bilinearly) upscaled image is a good approximation to the full-resolution target and that the difference between the approximation and the target, i.e. the delta image, is locally monochromatic, i.e. has a dominant principal axis. Further, it assumes the local principal axis can be interpolated across the image.</p>
<p>(to do: Add images showing breakdown)</p>
<p>The example texture, compressed with PVRTC1 4bpp produces:<br />
<a href="https://i.sstatic.net/5jKnQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5jKnQ.png" alt="enter image description here" /></a><br />
with the area around the beak:<br />
<a href="https://i.sstatic.net/ODTd4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ODTd4.png" alt="enter image description here" /></a><br />
Compared to BTC-schemes, the block artefacts typically are eliminated but there can sometimes be &quot;overshoot&quot; if there are strong discontinuities in the source image, for example around the silhouette of the lorikeet's head.</p>
<p>The 2bpp variant has, naturally, higher error than the 4bpp (note loss of precision around the blue, high frequency areas near the neck) but arguably still of reasonably quality:<br />
<a href="https://i.sstatic.net/ASiRE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ASiRE.png" alt="enter image description here" /></a></p>
<h3>A note on decompression costs</h3>
<p>Although the compression algorithms for the schemes described above have a moderate to high evaluation cost, the decompression algorithms, especially for hardware implementations, are relatively inexpensive. ETC1, for example, requires little more than a few MUXes and low-precision adders; S3TC effectively slightly more addition units to perform the blending; and PVRTC, slightly more again.  In theory, these simple TC schemes could allow a GPU architecture to avoid decompression until just prior to the filtering stage, thus maximising the effectiveness of internal caches.</p>
<h3>Other Schemes</h3>
<p>Other common TC modes that should be mentioned are:</p>
<ul>
<li><p>ETC2 - is a (4bpp) superset of ETC1 that improves the handling of regions with colour distributions that don't align well with 'luma'. There are also a 4bpp variant that supports 1 bit alpha, and an 8bpp format for RGBA.</p>
</li>
<li><p>ATC - Is effectively a small <a href="http://www.guildsoftware.com/papers/2012.Converting.DXTC.to.ATC.pdf" rel="nofollow noreferrer">variation on S3TC</a>.</p>
</li>
<li><p>FXT1 (3dfx) was a more ambitious <a href="https://www.opengl.org/registry/specs/3DFX/texture_compression_FXT1.txt" rel="nofollow noreferrer">variant of the S3TC theme</a>.</p>
</li>
<li><p>BC6 &amp; BC7 : An 8bpp, block-based system supporting ARGB. Apart from HDR modes, these use a more complex partitioning system than that of ETC to attempt to better model image colour distribution.</p>
</li>
<li><p>PVRTC2 : 2&amp;4bpp ARGB. This introduces additional modes including one to overcome limitations with strong boundaries in the images.</p>
</li>
<li><p>ASTC : This is also a block based system but is somewhat more complicated in that it has a large number of possible block sizes targeting a wide range of bpp. It also includes features such as up to 4 partition regions with a pseudo-random partition generator, and variable resolution for the index data and/or colour precision and colour models.</p>
</li>
</ul>

CC BY-SA 3.0
2015-10-08T18:39:32.033Z
Wow, this should be a blog post somewhere! Great answer!
glampert
CC BY-SA 3.0
2015-10-09T08:01:52.133Z
Glad it's of help. As for a blog, I did write [this a decade ago](http://web.onetel.net.uk/~simonnihal/texcom/texcompcomp.html) but I really don't have time to do them.
Simon F
CC BY-SA 4.0
2020-02-05T01:38:36.773Z
The website hosting the old blog is dead. This is the latest archived version: https://web.archive.org/web/20160322090245/http://web.onetel.net.uk/~simonnihal/texcom/texcompcomp.html
ahcox
CC BY-SA 4.0
2015-09-29T13:36:53.633Z
2023-12-22T11:20:39.470Z
2023-12-22T11:20:39.470Z
Simon F
<p>As Simon's comment alluded to, one major difference between hardware texture compression and other commonly used image compression is that the former does not use entropy coding. Entropy coding is the use of shorter bit-strings to represent commonly-occurring or repeating patterns in the source data&mdash;as seen in container formats like ZIP, many common image formats such as GIF, JPEG, and PNG, and also in many common audio and video formats.</p>

<p>Entropy coding is good at compressing all sorts of data, but it intrinsically produces a variable compression ratio. Some areas of the image may have little detail (or the detail is predicted well by the coding model you're using) and require very few bits, but other areas may have complex detail that requires more bits to encode. This makes it difficult to implement random access, as there is no straightforward way to calculate where in the compressed data you can find the pixel at given (<em>x</em>,&nbsp;<em>y</em>) coordinates. Also, most entropy coding schemes are stateful, so it's not possible to simply start decoding at an arbitrary place in the stream; you have to start from the beginning to build up the correct state. However, random access is necessary for texture sampling, since a shader may sample from any location in a texture at any time.</p>

<p>So, rather than entropy coding, hardware compression uses fixed-ratio, block-based schemes.  For example, in <a href="http://www.reedbeta.com/blog/2012/02/12/understanding-bcn-texture-compression-formats/">DXT / BCn compression</a>, the texture is sliced up into 4&times;4 pixel blocks, each of which is encoded in either 64 or 128 bits (depending on which format is picked); in <a href="https://en.wikipedia.org/wiki/Adaptive_Scalable_Texture_Compression">ASTC</a>, different formats use block sizes from 4&times;4 up to 12&times;12, and all blocks are encoded in 128 bits. The details of how the bits represent the image data vary between formats (and may even vary from one block to the next within the same image), but because the ratio is fixed, it's easy for hardware to calculate where in memory to find the block containing a given (<em>x</em>, <em>y</em>) pixel, and each block is self-contained, so it can be decoded independently of any other blocks.</p>

<p>Another consideration in hardware texture compression is that the decoding should be efficiently implementable in hardware. This means that heavy math operations and complex dataflow are strongly disfavored. The BCn formats, for instance, can be decoded by doing a handful of 8-bit integer math operations per block to populate a small lookup table, then just looking up the appropriate table entry per pixel.  This requires very little area on-chip, which is important because you probably want to decode several blocks in parallel, and thus need several copies of the decode hardware.</p>

<p>In contrast, DCT-based formats like JPEG require a nontrivial amount of math per pixel, not to mention a complex dataflow that swaps and broadcasts various intermediate values across pixels within a block. (Look at <a href="https://fgiesen.wordpress.com/2013/11/04/bink-2-2-integer-dct-design-part-1/">this article</a> for some of the gory details of DCT decoding.) This would be a lot grosser for hardware implementation, which I'm guessing is why AFAICT, no GPU hardware has ever implemented DCT-based or wavelet-based texture compression.</p>

CC BY-SA 3.0
2015-09-28T03:25:52.790Z
excellent answer. also you can add that there are some papers mentioning that in some situations you can do compression yourself and decode it with client code in a pixel shader, rather than depend on dedicated hardware. i know of no real world use of that, that may be only worth for research, but it exists.
v.oddou
CC BY-SA 3.0
2015-09-29T06:08:22.323Z
@Nathan-Reed re transform-based compression, actually, Microsofts's Talisman project used a compression scheme called TREC which (as one of the modes) used DCT but unlike JPEG, allowed random access to blocks (I suspect there must have been a table containing addresses).  This'd then allow variable length data for various blocks, but the indirection is unpleasant for HW - a reason VQ TC fell out of fashion. FWIW I experimented with about a dozen TC ideas B4 PVRTC; some were fixed-rate, transform-based, but "missing" coeffs still use bits. BTC-like fixed coeff locations implies "free" info.
Simon F
CC BY-SA 3.0
2015-09-30T14:06:51.400Z
@Nathan-Reed. From what I have seen, all HW decoders can be implemented with pure logic path (bit decode, some lookup, some math in the data path) but no loop / register necessity. Are you aware of any scheme that add cycle latency to the texture lookup ? (I have for fun implemented a VHDL ETC1 decoder) I was under the impression that each texture unit (TU) had decoders embedded.
Romain Piquois
CC BY-SA 3.0
2015-09-27T02:32:20.067Z
2015-09-27T02:32:20.067Z
Nathan Reed
<p>That it compresses the data compared to the pixel array is obvious.</p>

<p>But what makes it different from from normal compression (like png, jpeg)? </p>

CC BY-SA 3.0
2015-09-21T21:14:09.627Z
What's "normal compression" - things like JPEG and PNG? Are you asking about the differences between those and hardware-supported formats like DXT and ASTC?
Nathan Reed
CC BY-SA 3.0
2015-09-21T22:36:22.550Z
(At last, a subject I know a bit about!)

What makes it different to PNG/JPEG is random access.  Given you want to access Texel(X.Y) you can quickly determine the small footprint of data needed to produce that texel. JPG or PNG might require decompression of up to all of the data!   Sections 1 and 2 of the [Wikipedia article](https://en.wikipedia.org/wiki/Texture_compression) are a good summary.
Simon F
CC BY-SA 3.0
2015-09-22T16:08:01.373Z
As SimonF wrote. This is an extremely broad question, and the answer depends on which type you're interested in. Did you look at the specification for e.g. DXT?
geometrian
<texture><compression>
How does hardware texture compression work?
ratchet freak
# How does hardware texture compression work?

(Asked by: username_0 on 2015-09-21)

That it compresses the data compared to the pixel array is obvious.

But what makes it different from from normal compression (like png, jpeg)?

**Tags:** texture, compression

> **Comments:**
> **username_1**: What's "normal compression" - things like JPEG and PNG? Are you asking about the differences between those and hardware-supported formats like DXT and ASTC?  
> **username_2**: (At last, a subject I know a bit about!)  What makes it different to PNG/JPEG is random access.  Given you want to access Texel(X.Y) you can quickly determine the small footprint of data needed to produce that texel. JPG or PNG might require decompression of up to all of the data!   Sections 1 and 2 of the [Wikipedia article](https://en.wikipedia.org/wiki/Texture_compression) are a good summary.  
> **username_3**: As username_2 wrote. This is an extremely broad question, and the answer depends on which type you're interested in. Did you look at the specification for e.g. DXT?  

---

## 2 answers

### Answer by username_1 on 2015-09-27. Score: 27 | Accepted answer

As username_2's comment alluded to, one major difference between hardware texture compression and other commonly used image compression is that the former does not use entropy coding. Entropy coding is the use of shorter bit-strings to represent commonly-occurring or repeating patterns in the source data—as seen in container formats like ZIP, many common image formats such as GIF, JPEG, and PNG, and also in many common audio and video formats.

Entropy coding is good at compressing all sorts of data, but it intrinsically produces a variable compression ratio. Some areas of the image may have little detail (or the detail is predicted well by the coding model you're using) and require very few bits, but other areas may have complex detail that requires more bits to encode. This makes it difficult to implement random access, as there is no straightforward way to calculate where in the compressed data you can find the pixel at given (*x*, *y*) coordinates. Also, most entropy coding schemes are stateful, so it's not possible to simply start decoding at an arbitrary place in the stream; you have to start from the beginning to build up the correct state. However, random access is necessary for texture sampling, since a shader may sample from any location in a texture at any time.

So, rather than entropy coding, hardware compression uses fixed-ratio, block-based schemes. For example, in [DXT / BCn compression](http://www.reedbeta.com/blog/2012/02/12/understanding-bcn-texture-compression-formats/), the texture is sliced up into 4×4 pixel blocks, each of which is encoded in either 64 or 128 bits (depending on which format is picked); in [ASTC](https://en.wikipedia.org/wiki/Adaptive_Scalable_Texture_Compression), different formats use block sizes from 4×4 up to 12×12, and all blocks are encoded in 128 bits. The details of how the bits represent the image data vary between formats (and may even vary from one block to the next within the same image), but because the ratio is fixed, it's easy for hardware to calculate where in memory to find the block containing a given (*x*, *y*) pixel, and each block is self-contained, so it can be decoded independently of any other blocks.

Another consideration in hardware texture compression is that the decoding should be efficiently implementable in hardware. This means that heavy math operations and complex dataflow are strongly disfavored. The BCn formats, for instance, can be decoded by doing a handful of 8-bit integer math operations per block to populate a small lookup table, then just looking up the appropriate table entry per pixel. This requires very little area on-chip, which is important because you probably want to decode several blocks in parallel, and thus need several copies of the decode hardware.

In contrast, DCT-based formats like JPEG require a nontrivial amount of math per pixel, not to mention a complex dataflow that swaps and broadcasts various intermediate values across pixels within a block. (Look at [this article](https://fgiesen.wordpress.com/2013/11/04/bink-2-2-integer-dct-design-part-1/) for some of the gory details of DCT decoding.) This would be a lot grosser for hardware implementation, which I'm guessing is why AFAICT, no GPU hardware has ever implemented DCT-based or wavelet-based texture compression.

> **Comments:**
> **username_6**: excellent answer. also you can add that there are some papers mentioning that in some situations you can do compression yourself and decode it with client code in a pixel shader, rather than depend on dedicated hardware. i know of no real world use of that, that may be only worth for research, but it exists.  
> **username_2**: @username_1-username_1 re transform-based compression, actually, Microsofts's Talisman project used a compression scheme called TREC which (as one of the modes) used DCT but unlike JPEG, allowed random access to blocks (I suspect there must have been a table containing addresses).  This'd then allow variable length data for various blocks, but the indirection is unpleasant for HW - a reason VQ TC fell out of fashion. FWIW I experimented with about a dozen TC ideas B4 PVRTC; some were fixed-rate, transform-based, but "missing" coeffs still use bits. BTC-like fixed coeff locations implies "free" info.  
> **username_7**: @username_1-username_1. From what I have seen, all HW decoders can be implemented with pure logic path (bit decode, some lookup, some math in the data path) but no loop / register necessity. Are you aware of any scheme that add cycle latency to the texture lookup ? (I have for fun implemented a VHDL ETC1 decoder) I was under the impression that each texture unit (TU) had decoders embedded.  

---

### Answer by username_2 on 2015-09-29. Score: 34

*"How (hardware) texture compression works"* is a large topic. Hopefully I can provide some insights without duplicating the content of [username_1's answer](https://computergraphics.stackexchange.com/questions/1523/how-does-hardware-texture-compression-work/1545#1545).

## Requirements

Texture compression typically differs from 'standard' image compression techniques e.g. JPEG/PNG in four main ways, as outlined in Beers et al's [Rendering from Compressed Textures](https://graphics.stanford.edu/projects/flashg/papers/texture_compression/):

1. *Decoding Speed*: You don't want texture compression to be slower (at least not noticeably so) than using uncompressed textures. It should also be relatively simple to decompress since that can help achieve fast decompression without excessive hardware and power costs.
2. *Random Access*: You can't easily predict which texels will be required during a given render. If some subset, *M*, of the accessed texels come from, say, the middle of the image, it's essential that you don't have to decode all of the 'previous' lines of the texture in order to determine *M*; with JPEG and PNG this is necessary as pixel decoding depends on the previously decoded data.  
   Note, having said this, just because you have "random" access, [doesn't mean you should try to sample completely arbitrarily](https://computergraphics.stackexchange.com/questions/357/is-using-many-texture-maps-bad-for-caching/419#419)
3. *Compression Rate and Visual Quality*: Beers et al argue (convincingly) that losing some quality in the compressed result in order to improve compression rate is a worthwhile trade-off. In 3D rendering, the data is probably going to be manipulated (e.g. filtered & shaded etc) and so some loss of quality may well be masked.
4. *Asymmetric encoding/decoding*: Though perhaps slightly more contentious, they argue that it is acceptable to have the encoding process much slower than the decoding. Given that the decoding needs to be at HW fill rates, this is generally acceptable. (I will admit that compression of PVRTC, ETC2 and some others at maximum quality could be faster)

## Early History & Techniques

It may surprise some to learn that texture compression has been around for over four decades. Flight simulators from the 70s and 80s needed access to *relatively* large amounts of texture data and given that [1MB of RAM in 1980 was > $6000](http://www.jcmit.com/memoryprice.htm), reducing the texture footprint was essential. As another example, in the mid 70s, even a small amount of high speed memory and logic, e.g. enough for [a modest 512x512 RGB frame buffer](https://en.wikipedia.org/wiki/Framebuffer#History)) could set you back the price of small house.

Though, AFAIK, not explicitly referred to as texture compression, in the literature and patents you can find references to techniques including:  
a. simple forms of mathematical/procedural texture synthesis,  
b. use of a single channel texture (e.g. 4bpp) that is then multiplied by a per-texture RGB value,  
c. YUV, and  
d. palettes (the literature suggesting use of [Heckbert's approach](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.1334) to do the compression)

## Modelling Image Data

As noted above, texture compression is nearly always lossy and thus the problem becomes one of trying to represent the important data in a compact way whilst disposing of the less significant information. The various schemes that will be described below all have an implicit 'parameterised' model that approximates the typical behaviour of texture data and of the eye's response.

Further, since texture compression tends to use fixed-rate encoding, the compression process usually includes a search step to find the set of parameters which, when fed in to the model, will generate a good approximation of the original texture. That search step, however, can be time consuming.  
*(With the possible exception of tools such as [optipng](http://optipng.sourceforge.net/), this is another area where typical use of PNG & JPEG differ from texture compression schemes)*

Before progressing further, to help with further understanding of TC it's worth taking a look at [Principal Component Analysis (PCA)](https://en.wikipedia.org/wiki/Principal_component_analysis) - a very useful mathematical tool for data compression.

### Example texture

To compare the various methods, we'll use the following image:

[![small lorikeet+text](https://i.sstatic.net/SM000.png)](https://i.sstatic.net/SM000.png)  
Note that this is a fairly tough image, especially for palette and VQTC methods as it spans much of the RGB colour cube and only 15% of the texels use repeated colours.

## PC and (post mid 90s) Console Texture compression

To reduce data costs, some PC games and early games consoles also made use of palette images, which is a form of Vector Quantisation (VQ).
Palette-based approaches make the assumption that a given image only uses relatively small portions of the RGB(A) colour cube. A problem with palette textures is that the compression rates for the achieved quality is generally rather modest. The example texture compressed to "4bpp" (using GIMP) produced  
[![enter image description here](https://i.sstatic.net/Vpv0t.png)](https://i.sstatic.net/Vpv0t.png)  
Note again that this is a relatively tough image for VQ schemes.

### VQ with larger vectors (e.g. 2bpp ARGB)

Inspired by Beers et al, the Dreamcast console used VQ to encode 2x2 or even 2x4 pixel blocks with single bytes. While the "vectors" in the palette textures are 3 or 4 dimensional, the 2x2 pixel blocks can be considered to be 16 dimensional. The compression scheme assumes there is sufficient, approximate repetition of these vectors.

Even though VQ can achieve satisfactory quality with ~2bpp, the problem with these schemes is that it requires dependent memory reads: An initial read from the index map to determine the code for the pixel is followed by a second to actually fetch the pixel data associated with that code. Additional caches can help alleviate some of the incurred latency, but adds complexity to the hardware.

The example image compressed with the 2bpp Dreamcast scheme is
[![2bpp VQ result](https://i.sstatic.net/8tnag.png)](https://i.sstatic.net/8tnag.png).
The index map is: [![2bpp VQ index map](https://i.sstatic.net/YNJww.png)](https://i.sstatic.net/YNJww.png)

Compression of VQ data can be done in various ways however, *IIRC*, the above was done using PCA to derive and then partition the 16D vectors along the principal vector into 2 sets such that two representative vectors minimised the mean squared error. The process then recursed until 256 candidate vectors were produced.
A global [k-means/Lloyd's algorithm](https://en.wikipedia.org/wiki/K-means_clustering#Standard_algorithm) approach was then applied to improve the representatives.

### Colour Space Transformations

Colour space transformations also make use of PCA noting that the global distribution of colour is often spread along a major axis with far less spread along the other axes. For YUV representations, the assumptions are that a) the major axis is often in the luma direction and that b) the eye is more sensitive to changes in this direction.

The 3dfx Voodoo system provided ["YAB"](http://www.gamers.org/dEngine/xf3D/glide/glidepgm.htm), an 8bpp, "Narrow Channel" compression system that split each 8 bit texel into a 422 format, and applied a user selected colour transform to that data to map it into RGB. The main axis thus had 16 levels and the smaller axes, 4 each.

The S3 Virge chip had a slightly simpler, 4bpp, scheme that allowed the user to specify, for *the entire texture*, two end colours, which should lie on the principal axis, along with a 4bpp monochrome texture. The per-pixel value then blended the end colours with appropriate weights to produce the RGB result.

### BTC-based schemes

Rewinding some number of years, Delp and Mitchell designed a simple (monochrome) image compression scheme called [*Block Truncation Coding*, (BTC)](http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=1094560). This paper also included a compression algorithm but, for our purposes, we are mainly interested in the resulting compressed data and of the decompression process.

In this scheme, images are broken into, typically, 4x4 pixel blocks, which can be compressed independently with, in effect, a localised VQ algorithm. Each block is represented by two "values", *a* and *b*, and a 4x4 set of index bits, which identify which of the two values to use for each pixel.

*S3TC*: 4bpp RGB (+1bit alpha)  
Although several colour-variants of BTC for image compression were proposed, of interest to us is [Iourcha et al's S3TC](http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=5,956,431.PN.&OS=PN/5,956,431&RS=PN/5,956,431), some of which appears to be a rediscovery of the somewhat forgotten work of [Hoffert et al](http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=5046119.PN.&OS=PN/5046119&RS=PN/5046119) that was used in Apple's Quicktime.

The original S3TC, without the DirectX variants, compresses blocks of either RGB or RGB+1bit Alpha to 4bpp. Each 4x4 block in the texture is replaced by two end colours, *A* and *B*, from which up to two other colours are derived by fixed-weight, linear blends. Further, each texel in the block has a 2-bit index that determines how to select one of these four colours.

For example the following is a 4x4 pixel section of the test image compressed with the AMD/ATI Compressenator tool. (*Technically it's taken from a 512x512 version of the test image but forgive my lack of time to update the examples*).  
[![enter image description here](https://i.sstatic.net/YCI3n.png)](https://i.sstatic.net/YCI3n.png)  
This illustrates the compression process: The average and the principal axis of the colours are calculated. A best fit is then performed to find two end points that 'lie on' the axis which, along with the two derived 1:2 and 2:1 blends (or in some cases a 50:50 blend) of those end points, that minimises the error. Each original pixel is then mapped to one of those colours to produce the result.

If, as in this case, the colours are reasonable approximated by the principal axis, the error will be relatively low. However if, like in the neighbouring 4x4 block shown below, the colours are more diverse, the error will be higher.  
[![enter image description here](https://i.sstatic.net/d5dZ8.png)](https://i.sstatic.net/d5dZ8.png)

The example image, compressed with the [AMD Compressonator](http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/the-compressonator/) produces:  
[![enter image description here](https://i.sstatic.net/ZFrSa.png)](https://i.sstatic.net/ZFrSa.png)

Since the colours are determined independently per-block, there can be discontinuities at block boundaries but, as long as the resolution is kept sufficiently high, these block artefacts may go unnoticed:  
[![enter image description here](https://i.sstatic.net/Fa779.png)](https://i.sstatic.net/Fa779.png)

*ETC1*: 4bpp RGB  
Ericsson Texture Compression also works with 4x4 blocks of texels but makes the assumption that, much like YUV, the principal axis of a local set of texels is often very strongly correlated with "luma". The set of texels can then be represented by just an average colour and a highly quantised, scalar 'length' of the projection of the texels onto that assumed axis.

Since this reduces the data storage costs relative to say, S3TC, it allows ETC to introduce a partitioning scheme, whereby the 4x4 block is subdivided into a pair of horizontal 4x2 or vertical 2x4 sub-blocks. These each have their own average colour.
The example image produces:  
[![enter image description here](https://i.sstatic.net/cYOmr.png)](https://i.sstatic.net/cYOmr.png)  
The area around the beak also illustrates the horizontal and vertical partitioning of the 4x4 blocks.
[![enter image description here](https://i.sstatic.net/Uwd5F.png)](https://i.sstatic.net/Uwd5F.png)

### Global+Local

There are some texture compression systems that are a cross between global and local schemes, such as that of the distributed palettes of [Ivanov and Kuzmin](http://onlinelibrary.wiley.com/doi/10.1111/1467-8659.00420/abstract) or the method of [PVRTC](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.61.7956).

*PVRTC*: 4 & 2 bpp RGBA  
PVRTC assumes that an (in practice, bilinearly) upscaled image is a good approximation to the full-resolution target and that the difference between the approximation and the target, i.e. the delta image, is locally monochromatic, i.e. has a dominant principal axis. Further, it assumes the local principal axis can be interpolated across the image.

(to do: Add images showing breakdown)

The example texture, compressed with PVRTC1 4bpp produces:  
[![enter image description here](https://i.sstatic.net/5jKnQ.png)](https://i.sstatic.net/5jKnQ.png)  
with the area around the beak:  
[![enter image description here](https://i.sstatic.net/ODTd4.png)](https://i.sstatic.net/ODTd4.png)  
Compared to BTC-schemes, the block artefacts typically are eliminated but there can sometimes be "overshoot" if there are strong discontinuities in the source image, for example around the silhouette of the lorikeet's head.

The 2bpp variant has, naturally, higher error than the 4bpp (note loss of precision around the blue, high frequency areas near the neck) but arguably still of reasonably quality:  
[![enter image description here](https://i.sstatic.net/ASiRE.png)](https://i.sstatic.net/ASiRE.png)

### A note on decompression costs

Although the compression algorithms for the schemes described above have a moderate to high evaluation cost, the decompression algorithms, especially for hardware implementations, are relatively inexpensive. ETC1, for example, requires little more than a few MUXes and low-precision adders; S3TC effectively slightly more addition units to perform the blending; and PVRTC, slightly more again. In theory, these simple TC schemes could allow a GPU architecture to avoid decompression until just prior to the filtering stage, thus maximising the effectiveness of internal caches.

### Other Schemes

Other common TC modes that should be mentioned are:

* ETC2 - is a (4bpp) superset of ETC1 that improves the handling of regions with colour distributions that don't align well with 'luma'. There are also a 4bpp variant that supports 1 bit alpha, and an 8bpp format for RGBA.
* ATC - Is effectively a small [variation on S3TC](http://www.guildsoftware.com/papers/2012.Converting.DXTC.to.ATC.pdf).
* FXT1 (3dfx) was a more ambitious [variant of the S3TC theme](https://www.opengl.org/registry/specs/3DFX/texture_compression_FXT1.txt).
* BC6 & BC7 : An 8bpp, block-based system supporting ARGB. Apart from HDR modes, these use a more complex partitioning system than that of ETC to attempt to better model image colour distribution.
* PVRTC2 : 2&4bpp ARGB. This introduces additional modes including one to overcome limitations with strong boundaries in the images.
* ASTC : This is also a block based system but is somewhat more complicated in that it has a large number of possible block sizes targeting a wide range of bpp. It also includes features such as up to 4 partition regions with a pseudo-random partition generator, and variable resolution for the index data and/or colour precision and colour models.

> **Comments:**
> **username_4**: Wow, this should be a blog post somewhere! Great answer!  
> **username_2**: Glad it's of help. As for a blog, I did write [this a decade ago](http://web.onetel.net.uk/~simonnihal/texcom/texcompcomp.html) but I really don't have time to do them.  
> **username_5**: The website hosting the old blog is dead. This is the latest archived version: https://web.archive.org/web/20160322090245/http://web.onetel.net.uk/~simonnihal/texcom/texcompcomp.html  

---

