2014-04-09 04:34:25 +05:30
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 11:08:14 +05:30
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-09 04:34:25 +05:30
|
|
|
// Refer to the license.txt file included.
|
2014-04-06 01:34:25 +05:30
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2014-04-09 05:45:08 +05:30
|
|
|
#include "common/common_types.h"
|
2014-04-09 04:34:25 +05:30
|
|
|
|
2014-04-06 01:34:25 +05:30
|
|
|
namespace VideoCore {
|
|
|
|
|
|
|
|
/// Structure for the TGA texture format (for dumping)
|
|
|
|
struct TGAHeader {
|
|
|
|
char idlength;
|
2015-04-05 12:06:43 +05:30
|
|
|
char colormaptype;
|
2014-04-06 01:34:25 +05:30
|
|
|
char datatypecode;
|
2015-04-05 12:06:43 +05:30
|
|
|
short int colormaporigin;
|
|
|
|
short int colormaplength;
|
2014-04-06 01:34:25 +05:30
|
|
|
short int x_origin;
|
|
|
|
short int y_origin;
|
|
|
|
short width;
|
|
|
|
short height;
|
|
|
|
char bitsperpixel;
|
|
|
|
char imagedescriptor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumps a texture to TGA
|
|
|
|
* @param filename String filename to dump texture to
|
|
|
|
* @param width Width of texture in pixels
|
|
|
|
* @param height Height of texture in pixels
|
|
|
|
* @param raw_data Raw RGBA8 texture data to dump
|
|
|
|
* @todo This should be moved to some general purpose/common code
|
|
|
|
*/
|
2014-09-07 08:34:13 +05:30
|
|
|
void DumpTGA(std::string filename, short width, short height, u8* raw_data);
|
2014-04-06 01:34:25 +05:30
|
|
|
|
2015-02-24 04:54:35 +05:30
|
|
|
/**
|
|
|
|
* Interleave the lower 3 bits of each coordinate to get the intra-block offsets, which are
|
|
|
|
* arranged in a Z-order curve. More details on the bit manipulation at:
|
|
|
|
* https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
|
|
|
|
*/
|
|
|
|
static inline u32 MortonInterleave(u32 x, u32 y) {
|
|
|
|
u32 i = (x & 7) | ((y & 7) << 8); // ---- -210
|
|
|
|
i = (i ^ (i << 2)) & 0x1313; // ---2 --10
|
|
|
|
i = (i ^ (i << 1)) & 0x1515; // ---2 -1-0
|
|
|
|
i = (i | (i >> 7)) & 0x3F;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the offset of the position of the pixel in Morton order
|
|
|
|
*/
|
|
|
|
static inline u32 GetMortonOffset(u32 x, u32 y, u32 bytes_per_pixel) {
|
|
|
|
// Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each
|
|
|
|
// of which is composed of four 2x2 subtiles each of which is composed of four texels.
|
|
|
|
// Each structure is embedded into the next-bigger one in a diagonal pattern, e.g.
|
|
|
|
// texels are laid out in a 2x2 subtile like this:
|
|
|
|
// 2 3
|
|
|
|
// 0 1
|
|
|
|
//
|
|
|
|
// The full 8x8 tile has the texels arranged like this:
|
|
|
|
//
|
|
|
|
// 42 43 46 47 58 59 62 63
|
|
|
|
// 40 41 44 45 56 57 60 61
|
|
|
|
// 34 35 38 39 50 51 54 55
|
|
|
|
// 32 33 36 37 48 49 52 53
|
|
|
|
// 10 11 14 15 26 27 30 31
|
|
|
|
// 08 09 12 13 24 25 28 29
|
|
|
|
// 02 03 06 07 18 19 22 23
|
|
|
|
// 00 01 04 05 16 17 20 21
|
|
|
|
//
|
|
|
|
// This pattern is what's called Z-order curve, or Morton order.
|
|
|
|
|
|
|
|
const unsigned int block_height = 8;
|
|
|
|
const unsigned int coarse_x = x & ~7;
|
|
|
|
|
|
|
|
u32 i = VideoCore::MortonInterleave(x, y);
|
|
|
|
|
|
|
|
const unsigned int offset = coarse_x * block_height;
|
|
|
|
|
|
|
|
return (i + offset) * bytes_per_pixel;
|
|
|
|
}
|
|
|
|
|
2014-04-06 01:34:25 +05:30
|
|
|
} // namespace
|