2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* DDS (DirectDraw Surface) codec.
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "lib/byte_order.h"
|
2007-05-09 23:01:11 +02:00
|
|
|
#include "lib/bits.h"
|
2011-02-25 02:28:46 +01:00
|
|
|
#include "lib/timer.h"
|
2011-04-29 21:10:34 +02:00
|
|
|
#include "lib/allocators/shared_ptr.h"
|
|
|
|
#include "tex_codec.h"
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// NOTE: the convention is bottom-up for DDS, but there's no way to tell.
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// S3TC decompression
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// note: this code may not be terribly efficient. it's only used to
|
|
|
|
// emulate hardware S3TC support - if that isn't available, performance
|
|
|
|
// will suffer anyway due to increased video memory usage.
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
// for efficiency, we precalculate as much as possible about a block
|
|
|
|
// and store it here.
|
2010-01-11 21:53:12 +01:00
|
|
|
class S3tcBlock
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
public:
|
|
|
|
S3tcBlock(size_t dxt, const u8* RESTRICT block)
|
|
|
|
: dxt(dxt)
|
|
|
|
{
|
|
|
|
// (careful, 'dxt != 1' doesn't work - there's also DXT1a)
|
|
|
|
const u8* a_block = block;
|
|
|
|
const u8* c_block = (dxt == 3 || dxt == 5)? block+8 : block;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
PrecalculateAlpha(dxt, a_block);
|
|
|
|
PrecalculateColor(dxt, c_block);
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
void WritePixel(size_t pixel_idx, u8* RESTRICT out) const
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(pixel_idx < 16);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// pixel index -> color selector (2 bit) -> color
|
|
|
|
const size_t c_selector = access_bit_tbl(c_selectors, pixel_idx, 2);
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
|
|
out[i] = (u8)c[c_selector][i];
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// if no alpha, done
|
|
|
|
if(dxt == 1)
|
|
|
|
return;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
size_t a;
|
|
|
|
if(dxt == 3)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// table of 4-bit alpha entries
|
2011-04-28 19:13:55 +02:00
|
|
|
a = access_bit_tbl(a_bits, pixel_idx, 4);
|
2010-01-11 21:53:12 +01:00
|
|
|
a |= a << 4; // expand to 8 bits (replicate high into low!)
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2010-01-11 21:53:12 +01:00
|
|
|
else if(dxt == 5)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// pixel index -> alpha selector (3 bit) -> alpha
|
2011-04-28 19:13:55 +02:00
|
|
|
const size_t a_selector = access_bit_tbl(a_bits, pixel_idx, 3);
|
2010-01-11 21:53:12 +01:00
|
|
|
a = dxt5_a_tbl[a_selector];
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2010-01-11 21:53:12 +01:00
|
|
|
// (dxt == DXT1A)
|
|
|
|
else
|
|
|
|
a = c[c_selector][A];
|
2011-07-17 16:48:23 +02:00
|
|
|
out[A] = (u8)(a & 0xFF);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
private:
|
|
|
|
// pixel colors are stored as size_t[4]. size_t rather than u8 protects from
|
|
|
|
// overflow during calculations, and padding to an even size is a bit
|
|
|
|
// more efficient (even though we don't need the alpha component).
|
|
|
|
enum RGBA { R, G, B, A };
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
static inline void mix_2_3(size_t dst[4], size_t c0[4], size_t c1[4])
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
for(int i = 0; i < 3; i++) dst[i] = (c0[i]*2 + c1[i] + 1)/3;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
static inline void mix_avg(size_t dst[4], size_t c0[4], size_t c1[4])
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
for(int i = 0; i < 3; i++) dst[i] = (c0[i]+c1[i])/2;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2010-01-11 21:53:12 +01:00
|
|
|
|
2011-04-28 19:13:55 +02:00
|
|
|
template<typename T>
|
|
|
|
static inline size_t access_bit_tbl(T tbl, size_t idx, size_t bit_width)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-04-28 19:13:55 +02:00
|
|
|
size_t val = (tbl >> (idx*bit_width)) & bit_mask<T>(bit_width);
|
2010-01-11 21:53:12 +01:00
|
|
|
return val;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// extract a range of bits and expand to 8 bits (by replicating
|
|
|
|
// MS bits - see http://www.mindcontrol.org/~hplus/graphics/expand-bits.html ;
|
|
|
|
// this is also the algorithm used by graphics cards when decompressing S3TC).
|
|
|
|
// used to convert 565 to 32bpp RGB.
|
|
|
|
static inline size_t unpack_to_8(u16 c, size_t bits_below, size_t num_bits)
|
|
|
|
{
|
|
|
|
const size_t num_filler_bits = 8-num_bits;
|
|
|
|
const size_t field = (size_t)bits(c, bits_below, bits_below+num_bits-1);
|
2010-09-10 22:25:23 +02:00
|
|
|
const size_t filler = field >> (num_bits-num_filler_bits);
|
2010-01-11 21:53:12 +01:00
|
|
|
return (field << num_filler_bits) | filler;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
void PrecalculateAlpha(size_t dxt, const u8* RESTRICT a_block)
|
|
|
|
{
|
|
|
|
// read block contents
|
|
|
|
const u8 a0 = a_block[0], a1 = a_block[1];
|
|
|
|
a_bits = read_le64(a_block); // see below
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
if(dxt == 5)
|
|
|
|
{
|
|
|
|
// skip a0,a1 bytes (data is little endian)
|
|
|
|
a_bits >>= 16;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
const bool is_dxt5_special_combination = (a0 <= a1);
|
|
|
|
u8* a = dxt5_a_tbl; // shorthand
|
|
|
|
if(is_dxt5_special_combination)
|
|
|
|
{
|
|
|
|
a[0] = a0;
|
|
|
|
a[1] = a1;
|
|
|
|
a[2] = (4*a0 + 1*a1 + 2)/5;
|
|
|
|
a[3] = (3*a0 + 2*a1 + 2)/5;
|
|
|
|
a[4] = (2*a0 + 3*a1 + 2)/5;
|
|
|
|
a[5] = (1*a0 + 4*a1 + 2)/5;
|
|
|
|
a[6] = 0;
|
|
|
|
a[7] = 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a[0] = a0;
|
|
|
|
a[1] = a1;
|
|
|
|
a[2] = (6*a0 + 1*a1 + 3)/7;
|
|
|
|
a[3] = (5*a0 + 2*a1 + 3)/7;
|
|
|
|
a[4] = (4*a0 + 3*a1 + 3)/7;
|
|
|
|
a[5] = (3*a0 + 4*a1 + 3)/7;
|
|
|
|
a[6] = (2*a0 + 5*a1 + 3)/7;
|
|
|
|
a[7] = (1*a0 + 6*a1 + 3)/7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
void PrecalculateColor(size_t dxt, const u8* RESTRICT c_block)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// read block contents
|
|
|
|
// .. S3TC reference colors (565 format). the color table is generated
|
|
|
|
// from some combination of these, depending on their ordering.
|
|
|
|
u16 rc[2];
|
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
rc[i] = read_le16(c_block + 2*i);
|
|
|
|
// .. table of 2-bit color selectors
|
|
|
|
c_selectors = read_le32(c_block+4);
|
|
|
|
|
|
|
|
const bool is_dxt1_special_combination = (dxt == 1 || dxt == DXT1A) && rc[0] <= rc[1];
|
|
|
|
|
|
|
|
// c0 and c1 are the values of rc[], converted to 32bpp
|
|
|
|
for(int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
c[i][R] = unpack_to_8(rc[i], 11, 5);
|
|
|
|
c[i][G] = unpack_to_8(rc[i], 5, 6);
|
|
|
|
c[i][B] = unpack_to_8(rc[i], 0, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
// c2 and c3 are combinations of c0 and c1:
|
|
|
|
if(is_dxt1_special_combination)
|
|
|
|
{
|
|
|
|
mix_avg(c[2], c[0], c[1]); // c2 = (c0+c1)/2
|
|
|
|
for(int i = 0; i < 3; i++) c[3][i] = 0; // c3 = black
|
|
|
|
c[3][A] = (dxt == DXT1A)? 0 : 255; // (transparent iff DXT1a)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mix_2_3(c[2], c[0], c[1]); // c2 = 2/3*c0 + 1/3*c1
|
|
|
|
mix_2_3(c[3], c[1], c[0]); // c3 = 1/3*c0 + 2/3*c1
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2010-01-11 21:53:12 +01:00
|
|
|
|
|
|
|
// the 4 color choices for each pixel (RGBA)
|
|
|
|
size_t c[4][4]; // c[i][RGBA_component]
|
|
|
|
|
|
|
|
// (DXT5 only) the 8 alpha choices
|
|
|
|
u8 dxt5_a_tbl[8];
|
|
|
|
|
|
|
|
// alpha block; interpretation depends on dxt.
|
|
|
|
u64 a_bits;
|
|
|
|
|
|
|
|
// table of 2-bit color selectors
|
|
|
|
u32 c_selectors;
|
|
|
|
|
|
|
|
size_t dxt;
|
|
|
|
};
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
struct S3tcDecompressInfo
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t dxt;
|
|
|
|
size_t s3tc_block_size;
|
|
|
|
size_t out_Bpp;
|
2006-04-24 01:14:18 +02:00
|
|
|
u8* out;
|
|
|
|
};
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static void s3tc_decompress_level(size_t UNUSED(level), size_t level_w, size_t level_h,
|
2007-09-25 12:43:11 +02:00
|
|
|
const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT cbData)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2007-09-25 12:43:11 +02:00
|
|
|
S3tcDecompressInfo* di = (S3tcDecompressInfo*)cbData;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t dxt = di->dxt;
|
|
|
|
const size_t s3tc_block_size = di->s3tc_block_size;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// note: 1x1 images are legitimate (e.g. in mipmaps). they report their
|
|
|
|
// width as such for glTexImage, but the S3TC data is padded to
|
|
|
|
// 4x4 pixel block boundaries.
|
2011-04-30 14:34:28 +02:00
|
|
|
const size_t blocks_w = DivideRoundUp(level_w, size_t(4));
|
|
|
|
const size_t blocks_h = DivideRoundUp(level_h, size_t(4));
|
2006-04-24 01:14:18 +02:00
|
|
|
const u8* s3tc_data = level_data;
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(level_data_size % s3tc_block_size == 0);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for(size_t block_y = 0; block_y < blocks_h; block_y++)
|
2010-01-11 21:53:12 +01:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for(size_t block_x = 0; block_x < blocks_w; block_x++)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
S3tcBlock block(dxt, s3tc_data);
|
2006-04-24 01:14:18 +02:00
|
|
|
s3tc_data += s3tc_block_size;
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t pixel_idx = 0;
|
2006-04-24 01:14:18 +02:00
|
|
|
for(int y = 0; y < 4; y++)
|
|
|
|
{
|
|
|
|
// this is ugly, but advancing after x, y and block_y loops
|
|
|
|
// is no better.
|
|
|
|
u8* out = (u8*)di->out + ((block_y*4+y)*blocks_w*4 + block_x*4) * di->out_Bpp;
|
|
|
|
for(int x = 0; x < 4; x++)
|
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
block.WritePixel(pixel_idx, out);
|
2006-04-24 01:14:18 +02:00
|
|
|
out += di->out_Bpp;
|
|
|
|
pixel_idx++;
|
|
|
|
}
|
|
|
|
}
|
2010-01-11 21:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(s3tc_data == level_data + level_data_size);
|
2006-04-24 01:14:18 +02:00
|
|
|
di->out += blocks_w*blocks_h * 16 * di->out_Bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// decompress the given image (which is known to be stored as DXTn)
|
|
|
|
// effectively in-place. updates Tex fields.
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status s3tc_decompress(Tex* t)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// alloc new image memory
|
|
|
|
// notes:
|
|
|
|
// - dxt == 1 is the only non-alpha case.
|
|
|
|
// - adding or stripping alpha channels during transform is not
|
|
|
|
// our job; we merely output the same pixel format as given
|
|
|
|
// (tex.cpp's plain transform could cover it, if ever needed).
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t dxt = t->flags & TEX_DXT;
|
|
|
|
const size_t out_bpp = (dxt != 1)? 32 : 24;
|
2006-04-24 01:14:18 +02:00
|
|
|
const size_t out_size = tex_img_size(t) * out_bpp / t->bpp;
|
2011-04-29 21:10:34 +02:00
|
|
|
shared_ptr<u8> decompressedData;
|
|
|
|
AllocateAligned(decompressedData, out_size, pageSize);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t s3tc_block_size = (dxt == 3 || dxt == 5)? 16 : 8;
|
2007-12-20 21:14:21 +01:00
|
|
|
S3tcDecompressInfo di = { dxt, s3tc_block_size, out_bpp/8, decompressedData.get() };
|
2006-04-24 01:14:18 +02:00
|
|
|
const u8* s3tc_data = tex_get_data(t);
|
|
|
|
const int levels_to_skip = (t->flags & TEX_MIPMAPS)? 0 : TEX_BASE_LEVEL_ONLY;
|
|
|
|
tex_util_foreach_mipmap(t->w, t->h, t->bpp, s3tc_data, levels_to_skip, 4, s3tc_decompress_level, &di);
|
2007-12-20 21:14:21 +01:00
|
|
|
t->data = decompressedData;
|
|
|
|
t->dataSize = out_size;
|
2006-04-24 01:14:18 +02:00
|
|
|
t->ofs = 0;
|
|
|
|
t->bpp = out_bpp;
|
|
|
|
t->flags &= ~TEX_DXT;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// DDS file format
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// bit values and structure definitions taken from
|
2010-01-11 21:53:12 +01:00
|
|
|
// http://msdn.microsoft.com/en-us/library/ee417785(VS.85).aspx
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// DDS_PIXELFORMAT.dwFlags
|
2006-04-24 01:14:18 +02:00
|
|
|
// we've seen some DXT3 files that don't have this set (which is nonsense;
|
|
|
|
// any image lacking alpha should be stored as DXT1). it's authoritative
|
|
|
|
// if fourcc is DXT1 (there's no other way to tell DXT1 and DXT1a apart)
|
|
|
|
// and ignored otherwise.
|
|
|
|
#define DDPF_ALPHAPIXELS 0x00000001
|
|
|
|
#define DDPF_FOURCC 0x00000004
|
|
|
|
#define DDPF_RGB 0x00000040
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
struct DDS_PIXELFORMAT
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
u32 dwSize; // size of structure (32)
|
|
|
|
u32 dwFlags; // indicates which fields are valid
|
|
|
|
u32 dwFourCC; // (DDPF_FOURCC) FOURCC code, "DXTn"
|
|
|
|
u32 dwRGBBitCount; // (DDPF_RGB) bits per pixel
|
|
|
|
u32 dwRBitMask;
|
|
|
|
u32 dwGBitMask;
|
|
|
|
u32 dwBBitMask;
|
2010-01-11 21:53:12 +01:00
|
|
|
u32 dwABitMask; // (DDPF_ALPHAPIXELS)
|
|
|
|
};
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// DDS_HEADER.dwFlags (none are optional)
|
2006-04-24 01:14:18 +02:00
|
|
|
#define DDSD_CAPS 0x00000001
|
|
|
|
#define DDSD_HEIGHT 0x00000002
|
|
|
|
#define DDSD_WIDTH 0x00000004
|
2010-01-11 21:53:12 +01:00
|
|
|
#define DDSD_PITCH 0x00000008 // used when texture is uncompressed
|
2006-04-24 01:14:18 +02:00
|
|
|
#define DDSD_PIXELFORMAT 0x00001000
|
|
|
|
#define DDSD_MIPMAPCOUNT 0x00020000
|
2010-01-11 21:53:12 +01:00
|
|
|
#define DDSD_LINEARSIZE 0x00080000 // used when texture is compressed
|
2006-04-24 01:14:18 +02:00
|
|
|
#define DDSD_DEPTH 0x00800000
|
|
|
|
|
2010-01-11 21:53:12 +01:00
|
|
|
// DDS_HEADER.dwCaps
|
|
|
|
#define DDSCAPS_MIPMAP 0x00400000 // optional
|
|
|
|
#define DDSCAPS_TEXTURE 0x00001000 // required
|
|
|
|
|
|
|
|
struct DDS_HEADER
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// (preceded by the FOURCC "DDS ")
|
2006-04-24 01:14:18 +02:00
|
|
|
u32 dwSize; // size of structure (124)
|
|
|
|
u32 dwFlags; // indicates which fields are valid
|
|
|
|
u32 dwHeight; // (DDSD_HEIGHT) height of main image (pixels)
|
|
|
|
u32 dwWidth; // (DDSD_WIDTH ) width of main image (pixels)
|
2010-01-11 21:53:12 +01:00
|
|
|
u32 dwPitchOrLinearSize; // (DDSD_LINEARSIZE) size [bytes] of top level
|
2006-04-24 01:14:18 +02:00
|
|
|
// (DDSD_PITCH) bytes per row (%4 = 0)
|
|
|
|
u32 dwDepth; // (DDSD_DEPTH) vol. textures: vol. depth
|
|
|
|
u32 dwMipMapCount; // (DDSD_MIPMAPCOUNT) total # levels
|
|
|
|
u32 dwReserved1[11]; // reserved
|
2010-01-11 21:53:12 +01:00
|
|
|
DDS_PIXELFORMAT ddpf; // (DDSD_PIXELFORMAT) surface description
|
|
|
|
u32 dwCaps; // (DDSD_CAPS) misc. surface flags
|
|
|
|
u32 dwCaps2;
|
|
|
|
u32 dwCaps3;
|
|
|
|
u32 dwCaps4;
|
2006-04-24 01:14:18 +02:00
|
|
|
u32 dwReserved2; // reserved
|
2010-01-11 21:53:12 +01:00
|
|
|
};
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static bool is_valid_dxt(size_t dxt)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
switch(dxt)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case DXT1A:
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// extract all information from DDS pixel format and store in bpp, flags.
|
|
|
|
// pf points to the DDS file's header; all fields must be endian-converted
|
|
|
|
// before use.
|
|
|
|
// output parameters invalid on failure.
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status decode_pf(const DDS_PIXELFORMAT* pf, size_t& bpp, size_t& flags)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
bpp = 0;
|
|
|
|
flags = 0;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// check struct size
|
2010-01-11 21:53:12 +01:00
|
|
|
if(read_le32(&pf->dwSize) != sizeof(DDS_PIXELFORMAT))
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_INVALID_SIZE);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// determine type
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t pf_flags = (size_t)read_le32(&pf->dwFlags);
|
2006-04-24 01:14:18 +02:00
|
|
|
// .. uncompressed
|
|
|
|
if(pf_flags & DDPF_RGB)
|
|
|
|
{
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t pf_bpp = (size_t)read_le32(&pf->dwRGBBitCount);
|
|
|
|
const size_t pf_r_mask = (size_t)read_le32(&pf->dwRBitMask);
|
|
|
|
const size_t pf_g_mask = (size_t)read_le32(&pf->dwGBitMask);
|
|
|
|
const size_t pf_b_mask = (size_t)read_le32(&pf->dwBBitMask);
|
2010-01-11 21:53:12 +01:00
|
|
|
const size_t pf_a_mask = (size_t)read_le32(&pf->dwABitMask);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// (checked below; must be set in case below warning is to be
|
|
|
|
// skipped)
|
|
|
|
bpp = pf_bpp;
|
|
|
|
|
|
|
|
if(pf_flags & DDPF_ALPHAPIXELS)
|
|
|
|
{
|
|
|
|
// something weird other than RGBA or BGRA
|
|
|
|
if(pf_a_mask != 0xFF000000)
|
|
|
|
goto unsupported_component_ordering;
|
|
|
|
flags |= TEX_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure component ordering is 0xBBGGRR = RGB (see below)
|
|
|
|
if(pf_r_mask != 0xFF || pf_g_mask != 0xFF00 || pf_b_mask != 0xFF0000)
|
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// DDS_PIXELFORMAT in theory supports any ordering of R,G,B,A.
|
2006-04-24 01:14:18 +02:00
|
|
|
// we need to upload to OpenGL, which can only receive BGR(A) or
|
|
|
|
// RGB(A). the former still requires conversion (done by driver),
|
|
|
|
// so it's slower. since the very purpose of supporting uncompressed
|
|
|
|
// DDS is storing images in a format that requires no processing,
|
|
|
|
// we do not allow any weird orderings that require runtime work.
|
|
|
|
// instead, the artists must export with the correct settings.
|
|
|
|
unsupported_component_ordering:
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_FMT_INVALID);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(tex_validate_plain_format(bpp, (int)flags));
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
// .. compressed
|
|
|
|
else if(pf_flags & DDPF_FOURCC)
|
|
|
|
{
|
|
|
|
// set effective bpp and store DXT format in flags & TEX_DXT.
|
|
|
|
// no endian conversion necessary - FOURCC() takes care of that.
|
|
|
|
switch(pf->dwFourCC)
|
|
|
|
{
|
|
|
|
case FOURCC('D','X','T','1'):
|
|
|
|
bpp = 4;
|
|
|
|
if(pf_flags & DDPF_ALPHAPIXELS)
|
|
|
|
flags |= DXT1A | TEX_ALPHA;
|
|
|
|
else
|
|
|
|
flags |= 1;
|
|
|
|
break;
|
|
|
|
case FOURCC('D','X','T','3'):
|
|
|
|
bpp = 8;
|
|
|
|
flags |= 3;
|
|
|
|
flags |= TEX_ALPHA; // see DDPF_ALPHAPIXELS decl
|
|
|
|
break;
|
|
|
|
case FOURCC('D','X','T','5'):
|
|
|
|
bpp = 8;
|
|
|
|
flags |= 5;
|
|
|
|
flags |= TEX_ALPHA; // see DDPF_ALPHAPIXELS decl
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_FMT_INVALID);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// .. neither uncompressed nor compressed - invalid
|
|
|
|
else
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_FMT_INVALID);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// extract all information from DDS header and store in w, h, bpp, flags.
|
|
|
|
// sd points to the DDS file's header; all fields must be endian-converted
|
|
|
|
// before use.
|
|
|
|
// output parameters invalid on failure.
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status decode_sd(const DDS_HEADER* sd, size_t& w, size_t& h, size_t& bpp, size_t& flags)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// check header size
|
|
|
|
if(read_le32(&sd->dwSize) != sizeof(*sd))
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::CORRUPTED);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// flags (indicate which fields are valid)
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t sd_flags = (size_t)read_le32(&sd->dwFlags);
|
2006-04-24 01:14:18 +02:00
|
|
|
// .. not all required fields are present
|
|
|
|
// note: we can't guess dimensions - the image may not be square.
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t sd_req_flags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
|
2006-04-24 01:14:18 +02:00
|
|
|
if((sd_flags & sd_req_flags) != sd_req_flags)
|
2007-12-20 21:14:21 +01:00
|
|
|
WARN_RETURN(ERR::TEX_INCOMPLETE_HEADER);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// image dimensions
|
2010-01-11 21:53:12 +01:00
|
|
|
h = (size_t)read_le32(&sd->dwHeight);
|
|
|
|
w = (size_t)read_le32(&sd->dwWidth);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// pixel format
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(decode_pf(&sd->ddpf, bpp, flags));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-12-15 17:09:30 +01:00
|
|
|
// if the image is not aligned with the S3TC block size, it is stored
|
|
|
|
// with extra pixels on the bottom left to fill up the space, so we need
|
|
|
|
// to account for those when calculating how big it should be
|
2008-05-01 17:41:42 +02:00
|
|
|
size_t stored_h, stored_w;
|
2006-12-15 17:09:30 +01:00
|
|
|
if(flags & TEX_DXT)
|
|
|
|
{
|
2011-04-30 14:34:28 +02:00
|
|
|
stored_h = Align<4>(h);
|
|
|
|
stored_w = Align<4>(w);
|
2006-12-15 17:09:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stored_h = h;
|
|
|
|
stored_w = w;
|
|
|
|
}
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// verify pitch or linear size, if given
|
2006-12-15 17:09:30 +01:00
|
|
|
const size_t pitch = stored_w*bpp/8;
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t sd_pitch_or_size = (size_t)read_le32(&sd->dwPitchOrLinearSize);
|
2006-04-24 01:14:18 +02:00
|
|
|
if(sd_flags & DDSD_PITCH)
|
|
|
|
{
|
2011-04-30 14:34:28 +02:00
|
|
|
if(sd_pitch_or_size != Align<4>(pitch))
|
2010-01-11 21:53:12 +01:00
|
|
|
DEBUG_WARN_ERR(ERR::CORRUPTED);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
if(sd_flags & DDSD_LINEARSIZE)
|
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
// some DDS tools mistakenly store the total size of all levels,
|
|
|
|
// so allow values close to that as well
|
|
|
|
const ssize_t totalSize = ssize_t(pitch*stored_h*1.333333f);
|
|
|
|
if(sd_pitch_or_size != pitch*stored_h && abs(ssize_t(sd_pitch_or_size)-totalSize) > 64)
|
|
|
|
DEBUG_WARN_ERR(ERR::CORRUPTED);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
// note: both flags set would be invalid; no need to check for that,
|
|
|
|
// though, since one of the above tests would fail.
|
|
|
|
|
|
|
|
// mipmaps
|
|
|
|
if(sd_flags & DDSD_MIPMAPCOUNT)
|
|
|
|
{
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t mipmap_count = (size_t)read_le32(&sd->dwMipMapCount);
|
2006-04-24 01:14:18 +02:00
|
|
|
if(mipmap_count)
|
|
|
|
{
|
|
|
|
// mipmap chain is incomplete
|
|
|
|
// note: DDS includes the base level in its count, hence +1.
|
2007-05-09 23:01:11 +02:00
|
|
|
if(mipmap_count != ceil_log2(std::max(w,h))+1)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_FMT_INVALID);
|
2006-04-24 01:14:18 +02:00
|
|
|
flags |= TEX_MIPMAPS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for volume textures
|
|
|
|
if(sd_flags & DDSD_DEPTH)
|
|
|
|
{
|
2008-05-01 17:41:42 +02:00
|
|
|
const size_t depth = (size_t)read_le32(&sd->dwDepth);
|
2006-04-24 01:14:18 +02:00
|
|
|
if(depth)
|
2011-05-05 15:03:34 +02:00
|
|
|
WARN_RETURN(ERR::NOT_SUPPORTED);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check caps
|
|
|
|
// .. this is supposed to be set, but don't bail if not (pointless)
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(sd->dwCaps & DDSCAPS_TEXTURE);
|
2006-04-24 01:14:18 +02:00
|
|
|
// .. sanity check: warn if mipmap flag not set (don't bail if not
|
|
|
|
// because we've already made the decision).
|
2010-01-11 21:53:12 +01:00
|
|
|
const bool mipmap_cap = (sd->dwCaps & DDSCAPS_MIPMAP) != 0;
|
2006-04-24 01:14:18 +02:00
|
|
|
const bool mipmap_flag = (flags & TEX_MIPMAPS) != 0;
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(mipmap_cap == mipmap_flag);
|
2006-04-24 01:14:18 +02:00
|
|
|
// note: we do not check for cubemaps and volume textures (not supported)
|
|
|
|
// because the file may still have useful data we can read.
|
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool dds_is_hdr(const u8* file)
|
|
|
|
{
|
|
|
|
return *(u32*)file == FOURCC('D','D','S',' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
static bool dds_is_ext(const OsPath& extension)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
return extension == L".dds";
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t dds_hdr_size(const u8* UNUSED(file))
|
|
|
|
{
|
2010-01-11 21:53:12 +01:00
|
|
|
return 4+sizeof(DDS_HEADER);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-17 10:38:53 +02:00
|
|
|
static Status dds_decode(rpU8 data, size_t UNUSED(size), Tex* RESTRICT t)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-08-17 10:38:53 +02:00
|
|
|
const DDS_HEADER* sd = (const DDS_HEADER*)(data+4);
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(decode_sd(sd, t->w, t->h, t->bpp, t->flags));
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status dds_encode(Tex* RESTRICT UNUSED(t), DynArray* RESTRICT UNUSED(da))
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-05-05 15:03:34 +02:00
|
|
|
// note: do not return ERR::NOT_SUPPORTED et al. because that would
|
2006-04-24 01:14:18 +02:00
|
|
|
// break tex_write (which assumes either this, 0 or errors are returned).
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::TEX_CODEC_CANNOT_HANDLE;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-25 02:28:46 +01:00
|
|
|
TIMER_ADD_CLIENT(tc_dds_transform);
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status dds_transform(Tex* t, size_t transforms)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-02-25 02:28:46 +01:00
|
|
|
TIMER_ACCRUE(tc_dds_transform);
|
|
|
|
|
2010-09-10 22:25:23 +02:00
|
|
|
size_t mipmaps = t->flags & TEX_MIPMAPS;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t dxt = t->flags & TEX_DXT;
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(is_valid_dxt(dxt));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-09-10 22:25:23 +02:00
|
|
|
const size_t transform_mipmaps = transforms & TEX_MIPMAPS;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t transform_dxt = transforms & TEX_DXT;
|
2010-09-10 22:25:23 +02:00
|
|
|
// requesting removal of mipmaps
|
|
|
|
if(mipmaps && transform_mipmaps)
|
|
|
|
{
|
|
|
|
// we don't need to actually change anything except the flag - the
|
|
|
|
// mipmap levels will just be treated as trailing junk
|
|
|
|
t->flags &= ~TEX_MIPMAPS;
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
// requesting decompression
|
|
|
|
if(dxt && transform_dxt)
|
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(s3tc_decompress(t));
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
// both are DXT (unsupported; there are no flags we can change while
|
|
|
|
// compressed) or requesting compression (not implemented) or
|
|
|
|
// both not DXT (nothing we can do) - bail.
|
2010-09-10 22:25:23 +02:00
|
|
|
return INFO::TEX_CODEC_CANNOT_HANDLE;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEX_CODEC_REGISTER(dds);
|