Showing posts with label Texture-Compression. Show all posts
Showing posts with label Texture-Compression. Show all posts

Thursday, September 23, 2010

Mr Elusive strikes again!

Mr Elusive, or I should say J.M.P. van Waveren from Id Software, has released a new presentation about virtual texturing called "Using Virtual Texturing to Handle Massive Texture Data".

Most of it has been covered in previous presentations, but there are a couple of interesting tidbits which give some hints on how Id Software's virtual texture implementation works.

  • Their "sparse texture quad tree" is implemented as a MIP mapped texture, just like everybody else. I wondered about this because they never explicitly mentioned it before, and only mentioned a quad tree.
  • The presentation mentions about feedback rendering that "factor 10 smaller is OK" and "~ .5 msec on CPU for 80 x 60" which surprises me, because it's definitely not okay in my quake 4 test levels. But I suppose if the artwork has been made to work with virtual texturing it might actually work.
  • It also mentions that
      diffuse + specular + normal + alpha + power = 10 channels - notice the 10
      • 128k x 128k x 12 channels = 256 GB - now where did that 12 come from??
      • 53 GB DXT compressed (1 x DXT1 + 2 x DXT5)
    • use brute force scene visibility to throw away data - I was wondering about that, I could only think of brute force solutions, I guess the same goes for Id!
      • down to 20 – 50 GB (uncompressed) - It says uncompressed but it's still DXT compressed otherwise the numbers simply don't make any sense
      • 4 – 10 GB DXT compressed - this must be simply 'compressed' then.
So 128x128x12 texels per page = 192kb per page.
192kb / 6 = 21.5x compression. (their worst case compression ratio)

(50 GB / 10 GB = 5x compression, which explains why that 50 GB must be DXT compressed)

It also mentions somewhere "lossy compression is perfectly acceptable", so I'm guessing there are probably a lot of artifacts in their textures, otherwise they won't be able to achieve such high compression ratios.

They also mention that decompressing costs "1 to 2 milliseconds per page on a single CPU core",
which I'm assuming for their entire 12 channel page.

My results are about 3ms per 4 channel texture, which would be 3*3=9ms.
Of course my compression stuff is rather unoptimized, I chose to focus on compression ratio and image quality, so I'm not at all surprised that they have far better results than my part-time experimental efforts.

Another thing is that they specifically mention read back buffers, so their implementation is not analytical after all.
Which makes me wonder about this screenshot ...


... which incidentally uses the exact same screenshot as the previous presentation, because the MIP boundaries always lie on the page boundaries, which is completely not how a normal MIP boundary would look like.
So I'm thinking it's probably just an artificial representation of the pages, not at all representative of their technology.

Afterwards it presents an high level overview of their decompression pipeline, which I haven't had any time yet to look in detail.

Now if you'll excuse me I have to go back to my 2 simultaneous (and impossible) deadlines, thank you.

Thursday, June 17, 2010

Texture compression II

I've made some more progress on the compression code.
I have >6000 128x128 pages, created from Quake 4 textures that I'm using for testing purposes.
Right now the average page size is roughly 2kb.
There's one page of 10kb, and two more that are almost 9kb, other than that none gets higher than 6kb.
I could get more compression out of this, but not without degrading quality beyond where I would like to go.
Average decompression time is 8ms, but I haven't made any attempt at optimizing.
I can compress all the pages combined to 31x their original size.

I'm wondering how id software do their compression though..
According to their latest presentation (id Tech 5 Challenges: From Texture Virtualization to Massive Parallelization) they compress each page to about 2-6kb, but that's diffuse, normal + specular.

But I'm only compressing diffuse here.

I know they're using DCT, they even have some papers published about older versions of their compression routines, but so far I haven't been able to get it down to these sizes without seriously degrading quality.

Perhaps, because of all the real-time lighting etc., it's perfectly fine to really compress textures all the way down to the point where the artefacts are really obvious, simply because they won't be as visible in a scene as it would be in a 2d picture.

Perhaps, I'm nitpicking too much when it comes to quality.

I'm also using SSIM for texture quality assessments now.
It's not perfect, but better than PSNR.

Monday, June 14, 2010

Texture compression

So the last couple of days I've been playing around with texture compression again, just to see how far I could take it using wavelet compression.

I'm pretty pleased with the results. It's not very fast, but then again I haven't made any attempt to optimizing this yet.
I don't think I can get it as fast as DCT, but I can definitely get far more compression for the same quality.


Just to be able to measure my results more accurately I've created a tool which crawls through my page textures, compresses them, and measures the difference between the compressed/ and the original texture and determines the compressed size.

I should note that the third displayed bitmap in the tool is the exaggerated difference between the two textures.

The cool thing about wavelet compression is that it implicitly has it's mip-map encoded within it.
With virtual texturing you should always permanently cache your lowest resolution mip-maps, which means that if you use wavelet compression that you already have part of the compressed page in memory.

The idea is to use the mip maps that are already in memory to decompress the higher resolution mip maps, which helps decrease the amount of data that needs to be loaded from disk.
(Something I'm not yet doing right now)

Of course this will only be useful if the decompression can be made fast enough.

PS.
Texture page size is 128 x 128, 'diffuse' includes alpha channel here.

PPS.
Noticed that the textures that compress the worst are simple greyish textures with alpha.
It seems that these textures are using pre-multiplied alpha and as such have each color multiplied with their alpha.
This causes lots of variability in the color channels, even in the areas which you can't really see, which hurts compression.
The best solution would be to pre-multiply the alpha at load time, and not pre-multiply it on disk. For now I'm setting all the alpha==0 pixels to the average color, and this seem to work fairly well.

PPPS.
After interpolating the average color with the actual pixel color using the alpha as the interpolation factor, the compression ratio for the worst cases halved!
Obviously this shouldn't be done in production code, but it does show that non pre-multiplied alpha textures compress better than pre-multiplied alpha textures, which means that pre-multiplying the alpha should be done after decompression.

Also, can't help but notice that difference images kind of look like edge detection filters, which might mean sharpening the image with a filter might increase the overall quality of the images (maybe).

I could definitely use some better down/up scaling code for the Co/Cg channels.
(I'm using YCoCg internally as a color representation)

Wednesday, June 2, 2010

Some interesting links...

I've got 2 interesting links to share, both are useful for virtual texturing.

The first one "Wavelet image compression - IƱigo Quilez" is a nice article about wavelet compression, which makes me think all the mip maps of a texture page could be compressed together.
This would mean that lower resolution mips would need to be build up dynamically from several pages, which is not necessarily a bad thing because a lower resolution mip's texture area might not be fully utilized anyway.
However, you don't want to require too many compressed pages to be in memory at the same time when you look at a very low resolution page, so there needs to be some sort of trade of.
The very lowest mips should be pre-cached in memory anyway, so that you always have something to display when you can't load in your pages fast enough.

The other link "Bump Mapping Unparametrized Surfaces on the GPU - Morten S. Mikkelsen" is very interesting.
Bryan McNett actually sums it up:
The guy who wrote this paper sits five feet away from me. The paper doesn't say so explicitly, but this finally makes normal maps obsolete for games. Implement the paper, and you can replace 2-channel normal maps with 1-channel height maps. You can also throw away all per-vertex tangent/binormal data. Cool stuff, if you ask me!

The primary flaw of the technique is that, since the derivative of the height map is taken with linear-filtering texture sampling hardware, when magnified it looks like "nearest neighbor" filtering. Fixing this requires adding bicubic-filtering to the hardware.

This can actually be used as a texture compression of sorts, by rendering the generated normal map into the normal page cache texture. The linear filtering won't be an issue there.
Of course this requires per pixel position & per pixel normal, something I would need to do lighting into the page cache anyway.

Thursday, February 4, 2010

"Efficient Irradiance Normal Mapping"

Quote:
"Irradiance normal mapping is a method to combine two popular techniques, light mapping and normal mapping, and is used in games such as Half-Life 2 or Halo 3. This combination allows using low-resolution light caching on surfaces with only a few coefficients which are evaluated by normal maps to render spatial high-frequency changes in the lighting."

Now that sounds like it might work in combination with the virtual texturing surface caching idea!
Not sure about it's performance though, which is rather crucial.

You can find the paper here.

Wednesday, October 28, 2009

Adventures in virtual texture space, part 12

So the last time I mentioned that I managed to compress my textures to about 6Kb, and theorizing that it's about the same compression ratio that Id software has.
Obviously Id software, next to simply being awesome, have many more resources compared to me to get their texture compression just perfect.
So I was quite happy with myself that, using their published papers, I was able to get it the same results.

Unfortunately, as Brian Karis mentioned in the comments, I was wrong.

6Kb wasn't per texture but for all the different material channels, diffuse/bump/specular, combined.

To get the same compression ratio I would need to get it down to 2Kb.

Of course I could just crank up the compression ratio, but I didn't really like the results if I did.
That being said, if I convert the results to dxt5 after decompression I'd lose some more quality anyway, so in the end cranking up the compression ratio might be acceptable.
But I really wanted to see how far I could go without decreasing the quality.

So I started to experiment with Burrows Wheeler transformations, which do some clever stuff to change the order of bytes so that when you perform RLE (Run Length Encoding) on them, the compression ratio ends up being better.
And look at that, it worked like a charm!

The file size got down to 2Kb.

But it's slow. REALLY slow.

The algorithm scales horribly, and on a 64Kb buffer it took seconds to finish.
Decompression took >10ms, which is a lot faster than compression, but still not good enough.

Sometime later, after trying all kinds of things, I tried doing RLE both before & after the transformation, just to see what would happen.
The reasoning was that if I first decrease the size of the buffer, by compressing it a little with RLE, then the Burrows Wheeler process would be faster.
After that I would then do another RLE over the resulting buffer to increase my compression ratio somewhat.
I expected this to be a little faster, and probably have somewhat worse compression overall.

To my surprise now the total compression process was MUCH FASTER only taking 14ms.
Decompression only took 3.2ms overall, and the file got about 100 bytes SMALLER, which is somewhat counter-intuitive.

Result: 1899 bytes for an 128x128 RGBA texture.
(the alpha channel is a solid color though)

Friday, October 23, 2009

Adventures in virtual texture space, part 11

I've been working on implementing the texture compression ideas from the  "Real-Time Texture Streaming & Decompression" paper in my virtual texture implementation.
I couldn't just use the code and port it this time because this paper only has the decompression code, and I figured it would probably be easier to write everything from scratch then to try to figure out how the compression side would look to perfectly work with the given decompression code.
It's not implemented into my VT demo (yet), but I have some results.



Right now I can decompress an 128x128 texture in 2.5ms, and it ends up about 5-6kb, and that includes an alpha channel.
I don't think i can get it much smaller, at least i don't know how to do that without seriously degrading performance, and the quality is already less than i would like.
Eventually i'll see how much of a difference it makes when i turn this into a YCoCg/DXT5 texture, in which case there might not be much difference in quality when comparing the original and the compressed one (since DXT5 already reduces the quality)
The speed can probably be increased several times considering this is written in plain vanilla C#. Languages like C or C++ are much better at this sort of thing because you can do all kinds of pointer tricks that you cannot do easily in a managed language, although i might try C++/CLI eventually.

Like the paper I'm converting my RGB(A) texture into YCoCg(A), separate the channels, and then downsample the Co and Cg channels (which actually makes no real visual impact).
I then splice the buffers up into 8x8 blocks and pass them trough an DCT converter and quantize it all.
Unlike the paper I'm not doing run-length and Huffman encoding on a block basis, but i'm doing this over all blocks. Eventually it might make sense to do the encoding on a block basis, to make it easier to multi thread the decompression, but I'm not so sure it's a good idea to have a Huffman header on a per block basis, I'm think it would be more efficient on a per texture basis.

In the "From Texture Virtualization to Massive Parallelization" paper they mention they have "diffuse, specular, bump and cover/alpha" channels for their tiles, and that they have "Typically 2-6kB input, 40kB output".
Which makes me wonder if they have this 2-6Kb input per 'texture', or for all those channels combined.
I can't imagine that it's for all the channels combined, because 40Kb is a 128x128 texture with 2.5 bytes per texel, and they use 128x128 tile sizes as an example in the same PDF.
So it seems to me they have 2-6Kb per tile, which is about the same I have right now.
The lower limit of 2Kb is probably for grey textures that only have 1 channel.

Monday, October 12, 2009

Adventures in virtual texture space, part 10B

Like I promised in my last post, here are some images.
On the left you can see the original texture, the other two are the result of YCoCg/DXT5 compression.
The middle one is with the simplification used in the
"Real-Time DXT Compression" paper I talked about, the right one is without it.



Here you can see that there's an orange border around
all the yellow edges. In the middle one there are also some
bright green blocks.



Everything is blocky after compression, has a
washed out red appearance, and in the middle
there are some purple smudges here and there.


In the middle image there's a red glow around the top of
the yellow stripes, while the black stripes have a redish glow.
The right one isn't that much better, but the glow is more yellow,
which makes more sense since there's no red in the original.
 When you look at the yellow stripes from some distance,
the middle one looks green, unlike the left and right
 one which remain yellow.

I'm pretty sure these artifacts would be far less visible on higher resolution textures, but here all these artifacts are rather visible.

Update: Updated images with higher resolution versions.