GPU: Implemented bits 3 and 1 from the display transfer flags.

Bit 3 is used to specify a raw copy, where no processing is done to the data, seems to behave exactly as a DMA.
Bit 1 is used to specify whether to convert from a tiled format to a linear format or viceversa.
This commit is contained in:
Subv 2015-02-23 18:24:35 -05:00
parent c7dac73b0c
commit c564c21668
6 changed files with 169 additions and 81 deletions

View File

@ -9,8 +9,10 @@
#include <QPushButton>
#include <QSpinBox>
#include "core/hw/gpu.h"
#include "video_core/color.h"
#include "video_core/pica.h"
#include "video_core/utils.h"
#include "graphics_framebuffer.h"
@ -195,16 +197,20 @@ void GraphicsFramebufferWidget::OnUpdate()
// TODO: Implement a good way to visualize alpha components!
// TODO: Unify this decoding code with the texture decoder
u32 bytes_per_pixel = GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(framebuffer_format));
switch (framebuffer_format) {
case Format::RGBA8:
{
QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
u32* color_buffer = (u32*)Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
u8* color_buffer = Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
for (unsigned int y = 0; y < framebuffer_height; ++y) {
for (unsigned int x = 0; x < framebuffer_width; ++x) {
u32 value = *(color_buffer + x + y * framebuffer_width);
const u32 coarse_y = y & ~7;
u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * framebuffer_width * bytes_per_pixel;
u8* value = color_buffer + offset;
decoded_image.setPixel(x, y, qRgba((value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, 255/*value >> 24*/));
decoded_image.setPixel(x, y, qRgba(value[3], value[2], value[1], 255/*value >> 24*/));
}
}
pixmap = QPixmap::fromImage(decoded_image);
@ -217,7 +223,9 @@ void GraphicsFramebufferWidget::OnUpdate()
u8* color_buffer = Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
for (unsigned int y = 0; y < framebuffer_height; ++y) {
for (unsigned int x = 0; x < framebuffer_width; ++x) {
u8* pixel_pointer = color_buffer + x * 3 + y * 3 * framebuffer_width;
const u32 coarse_y = y & ~7;
u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * framebuffer_width * bytes_per_pixel;
u8* pixel_pointer = color_buffer + offset;
decoded_image.setPixel(x, y, qRgba(pixel_pointer[0], pixel_pointer[1], pixel_pointer[2], 255/*value >> 24*/));
}
@ -229,10 +237,12 @@ void GraphicsFramebufferWidget::OnUpdate()
case Format::RGBA5551:
{
QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
u32* color_buffer = (u32*)Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
u8* color_buffer = Memory::GetPointer(Pica::PAddrToVAddr(framebuffer_address));
for (unsigned int y = 0; y < framebuffer_height; ++y) {
for (unsigned int x = 0; x < framebuffer_width; ++x) {
u16 value = *(u16*)(((u8*)color_buffer) + x * 2 + y * framebuffer_width * 2);
const u32 coarse_y = y & ~7;
u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * framebuffer_width * bytes_per_pixel;
u16 value = *(u16*)(color_buffer + offset);
u8 r = Color::Convert5To8((value >> 11) & 0x1F);
u8 g = Color::Convert5To8((value >> 6) & 0x1F);
u8 b = Color::Convert5To8((value >> 1) & 0x1F);

View File

@ -18,10 +18,10 @@
#include "core/hw/gpu.h"
#include "video_core/command_processor.h"
#include "video_core/utils.h"
#include "video_core/video_core.h"
#include <video_core/color.h>
namespace GPU {
Regs g_regs;
@ -116,24 +116,64 @@ inline void Write(u32 addr, const T data) {
u8* source_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalInputAddress()));
u8* dest_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalOutputAddress()));
// Cheap emulation of horizontal scaling: Just skip each second pixel of the
// input framebuffer. We keep track of this in the pixel_skip variable.
unsigned pixel_skip = (config.scale_horizontally != 0) ? 2 : 1;
unsigned horizontal_scale = (config.scale_horizontally != 0) ? 2 : 1;
unsigned vertical_scale = (config.scale_vertically != 0) ? 2 : 1;
u32 output_width = config.output_width / pixel_skip;
u32 output_width = config.output_width / horizontal_scale;
u32 output_height = config.output_height / vertical_scale;
for (u32 y = 0; y < config.output_height; ++y) {
// TODO: Why does the register seem to hold twice the framebuffer width?
if (config.raw_copy) {
// Raw copies do not perform color conversion nor tiled->linear / linear->tiled conversions
// TODO(Subv): Verify if raw copies perform scaling
memcpy(dest_pointer, source_pointer, config.output_width * config.output_height *
GPU::Regs::BytesPerPixel(config.output_format));
LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), flags 0x%08X, Raw copy",
config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format),
config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(),
config.GetPhysicalOutputAddress(), config.output_width.Value(), config.output_height.Value(),
config.output_format.Value(), config.flags);
GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
break;
}
// TODO(Subv): Blend the pixels when horizontal / vertical scaling is enabled,
// right now we're just skipping the extra pixels.
for (u32 y = 0; y < output_height; ++y) {
for (u32 x = 0; x < output_width; ++x) {
struct {
int r, g, b, a;
} source_color = { 0, 0, 0, 0 };
u32 scaled_x = x * horizontal_scale;
u32 scaled_y = y * vertical_scale;
u32 dst_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.output_format);
u32 src_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.input_format);
u32 src_offset;
u32 dst_offset;
if (config.output_tiled) {
// Interpret the input as linear and the output as tiled
u32 coarse_y = y & ~7;
u32 stride = output_width * dst_bytes_per_pixel;
src_offset = (scaled_x + scaled_y * config.input_width) * src_bytes_per_pixel;
dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + coarse_y * stride;
} else {
// Interpret the input as tiled and the output as linear
u32 coarse_y = scaled_y & ~7;
u32 stride = config.input_width * src_bytes_per_pixel;
src_offset = VideoCore::GetMortonOffset(scaled_x, scaled_y, src_bytes_per_pixel) + coarse_y * stride;
dst_offset = (x + y * output_width) * dst_bytes_per_pixel;
}
switch (config.input_format) {
case Regs::PixelFormat::RGBA8:
{
u8* srcptr = source_pointer + (x * pixel_skip + y * config.input_width) * 4;
u8* srcptr = source_pointer + src_offset;
source_color.r = srcptr[3]; // red
source_color.g = srcptr[2]; // green
source_color.b = srcptr[1]; // blue
@ -143,7 +183,7 @@ inline void Write(u32 addr, const T data) {
case Regs::PixelFormat::RGB5A1:
{
u16 srcval = *(u16*)(source_pointer + x * 4 * pixel_skip + y * config.input_width * 4 * pixel_skip);
u16 srcval = *(u16*)(source_pointer + src_offset);
source_color.r = Color::Convert5To8((srcval >> 11) & 0x1F); // red
source_color.g = Color::Convert5To8((srcval >> 6) & 0x1F); // green
source_color.b = Color::Convert5To8((srcval >> 1) & 0x1F); // blue
@ -153,7 +193,7 @@ inline void Write(u32 addr, const T data) {
case Regs::PixelFormat::RGBA4:
{
u16 srcval = *(u16*)(source_pointer + x * 4 * pixel_skip + y * config.input_width * 4 * pixel_skip);
u16 srcval = *(u16*)(source_pointer + src_offset);
source_color.r = Color::Convert4To8((srcval >> 12) & 0xF); // red
source_color.g = Color::Convert4To8((srcval >> 8) & 0xF); // green
source_color.b = Color::Convert4To8((srcval >> 4) & 0xF); // blue
@ -169,7 +209,7 @@ inline void Write(u32 addr, const T data) {
switch (config.output_format) {
case Regs::PixelFormat::RGBA8:
{
u8* dstptr = dest_pointer + (x * pixel_skip + y * config.output_width) * 4;
u8* dstptr = dest_pointer + dst_offset;
dstptr[3] = source_color.r;
dstptr[2] = source_color.g;
dstptr[1] = source_color.b;
@ -179,7 +219,7 @@ inline void Write(u32 addr, const T data) {
case Regs::PixelFormat::RGB8:
{
u8* dstptr = dest_pointer + (x + y * output_width) * 3;
u8* dstptr = dest_pointer + dst_offset;
dstptr[2] = source_color.r; // red
dstptr[1] = source_color.g; // green
dstptr[0] = source_color.b; // blue
@ -188,7 +228,7 @@ inline void Write(u32 addr, const T data) {
case Regs::PixelFormat::RGB5A1:
{
u16* dstptr = (u16*)(dest_pointer + x * 2 + y * config.output_width * 2);
u16* dstptr = (u16*)(dest_pointer + dst_offset);
*dstptr = ((source_color.r >> 3) << 11) | ((source_color.g >> 3) << 6)
| ((source_color.b >> 3) << 1) | ( source_color.a >> 7);
break;
@ -196,7 +236,7 @@ inline void Write(u32 addr, const T data) {
case Regs::PixelFormat::RGBA4:
{
u16* dstptr = (u16*)(dest_pointer + x * 2 + y * config.output_width * 2);
u16* dstptr = (u16*)(dest_pointer + dst_offset);
*dstptr = ((source_color.r >> 4) << 12) | ((source_color.g >> 4) << 8)
| ((source_color.b >> 4) << 4) | ( source_color.a >> 4);
break;
@ -209,11 +249,11 @@ inline void Write(u32 addr, const T data) {
}
}
LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), dst format %x",
config.output_height * output_width * 4,
config.GetPhysicalInputAddress(), (u32)config.input_width, (u32)config.input_height,
config.GetPhysicalOutputAddress(), (u32)output_width, (u32)config.output_height,
config.output_format.Value());
LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), dst format %x, flags 0x%08X",
config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format),
config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(),
config.GetPhysicalOutputAddress(), output_width, output_height,
config.output_format.Value(), config.flags);
GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
}

View File

@ -192,12 +192,13 @@ struct Regs {
u32 flags;
BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true
BitField< 1, 1, u32> output_tiled; // Converts from linear to tiled format
BitField< 3, 1, u32> raw_copy; // Copies the data without performing any processing
BitField< 8, 3, PixelFormat> input_format;
BitField<12, 3, PixelFormat> output_format;
BitField<16, 1, u32> output_tiled; // stores output in a tiled format
// TODO: Not really sure if this actually scales, or even resizes at all.
BitField<24, 1, u32> scale_horizontally;
BitField<25, 1, u32> scale_vertically;
};
INSERT_PADDING_WORDS(0x1);

View File

@ -23,6 +23,7 @@
#include "video_core/color.h"
#include "video_core/math.h"
#include "video_core/pica.h"
#include "video_core/utils.h"
#include "debug_utils.h"
@ -306,63 +307,33 @@ std::unique_ptr<PicaTrace> FinishPicaTracing()
}
const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const TextureInfo& info, bool disable_alpha) {
// 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
const unsigned int block_width = 8;
const unsigned int block_height = 8;
const unsigned int coarse_x = x & ~7;
const unsigned int coarse_y = y & ~7;
// 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/
unsigned int 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;
if (info.format != Regs::TextureFormat::ETC1 &&
info.format != Regs::TextureFormat::ETC1A4) {
// TODO(neobrain): Fix code design to unify vertical block offsets!
source += coarse_y * info.stride;
}
const unsigned int offset = coarse_x * block_height;
// TODO: Assert that width/height are multiples of block dimensions
switch (info.format) {
case Regs::TextureFormat::RGBA8:
{
const u8* source_ptr = source + offset * 4 + i * 4;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 4);
return { source_ptr[3], source_ptr[2], source_ptr[1], disable_alpha ? (u8)255 : source_ptr[0] };
}
case Regs::TextureFormat::RGB8:
{
const u8* source_ptr = source + offset * 3 + i * 3;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 3);
return { source_ptr[2], source_ptr[1], source_ptr[0], 255 };
}
case Regs::TextureFormat::RGBA5551:
{
const u16 source_ptr = *(const u16*)(source + offset * 2 + i * 2);
const u16 source_ptr = *(const u16*)(source + VideoCore::GetMortonOffset(x, y, 2));
u8 r = (source_ptr >> 11) & 0x1F;
u8 g = ((source_ptr) >> 6) & 0x1F;
u8 b = (source_ptr >> 1) & 0x1F;
@ -373,7 +344,7 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::RGB565:
{
const u16 source_ptr = *(const u16*)(source + offset * 2 + i * 2);
const u16 source_ptr = *(const u16*)(source + VideoCore::GetMortonOffset(x, y, 2));
u8 r = Color::Convert5To8((source_ptr >> 11) & 0x1F);
u8 g = Color::Convert6To8(((source_ptr) >> 5) & 0x3F);
u8 b = Color::Convert5To8((source_ptr) & 0x1F);
@ -382,7 +353,7 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::RGBA4:
{
const u8* source_ptr = source + offset * 2 + i * 2;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 2);
u8 r = Color::Convert4To8(source_ptr[1] >> 4);
u8 g = Color::Convert4To8(source_ptr[1] & 0xF);
u8 b = Color::Convert4To8(source_ptr[0] >> 4);
@ -392,7 +363,7 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::IA8:
{
const u8* source_ptr = source + offset * 2 + i * 2;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 2);
if (disable_alpha) {
// Show intensity as red, alpha as green
@ -404,13 +375,13 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::I8:
{
const u8* source_ptr = source + offset + i;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
return { *source_ptr, *source_ptr, *source_ptr, 255 };
}
case Regs::TextureFormat::A8:
{
const u8* source_ptr = source + offset + i;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
if (disable_alpha) {
return { *source_ptr, *source_ptr, *source_ptr, 255 };
@ -421,7 +392,7 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::IA4:
{
const u8* source_ptr = source + offset + i;
const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
u8 a = Color::Convert4To8((*source_ptr) & 0xF);
@ -436,9 +407,10 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
case Regs::TextureFormat::A4:
{
const u8* source_ptr = source + (offset + i) / 2;
u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
const u8* source_ptr = source + morton_offset / 2;
u8 a = (i % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
a = Color::Convert4To8(a);
if (disable_alpha) {

View File

@ -7,13 +7,14 @@
#include "common/common_types.h"
#include "common/math_util.h"
#include "core/hw/gpu.h"
#include "debug_utils/debug_utils.h"
#include "math.h"
#include "color.h"
#include "pica.h"
#include "rasterizer.h"
#include "vertex_shader.h"
#include "debug_utils/debug_utils.h"
#include "video_core/utils.h"
namespace Pica {
@ -27,10 +28,14 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
// NOTE: The framebuffer height register contains the actual FB height minus one.
y = (registers.framebuffer.height - y);
const u32 coarse_y = y & ~7;
u32 bytes_per_pixel = GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(registers.framebuffer.color_format.Value()));
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * registers.framebuffer.width * bytes_per_pixel;
switch (registers.framebuffer.color_format) {
case registers.framebuffer.RGBA8:
{
u8* pixel = color_buffer + (x + y * registers.framebuffer.GetWidth()) * 4;
u8* pixel = color_buffer + dst_offset;
pixel[3] = color.r();
pixel[2] = color.g();
pixel[1] = color.b();
@ -40,14 +45,14 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
case registers.framebuffer.RGBA4:
{
u8* pixel = color_buffer + (x + y * registers.framebuffer.GetWidth()) * 2;
u8* pixel = color_buffer + dst_offset;
pixel[1] = (color.r() & 0xF0) | (color.g() >> 4);
pixel[0] = (color.b() & 0xF0) | (color.a() >> 4);
break;
}
default:
LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format);
LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format.Value());
UNIMPLEMENTED();
}
}
@ -58,11 +63,15 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
y = (registers.framebuffer.height - y);
const u32 coarse_y = y & ~7;
u32 bytes_per_pixel = GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(registers.framebuffer.color_format.Value()));
u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * registers.framebuffer.width * bytes_per_pixel;
switch (registers.framebuffer.color_format) {
case registers.framebuffer.RGBA8:
{
Math::Vec4<u8> ret;
u8* pixel = color_buffer + (x + y * registers.framebuffer.GetWidth()) * 4;
u8* pixel = color_buffer + src_offset;
ret.r() = pixel[3];
ret.g() = pixel[2];
ret.b() = pixel[1];
@ -73,7 +82,7 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
case registers.framebuffer.RGBA4:
{
Math::Vec4<u8> ret;
u8* pixel = color_buffer + (x + y * registers.framebuffer.GetWidth()) * 2;
u8* pixel = color_buffer + src_offset;
ret.r() = Color::Convert4To8(pixel[1] >> 4);
ret.g() = Color::Convert4To8(pixel[1] & 0x0F);
ret.b() = Color::Convert4To8(pixel[0] >> 4);
@ -82,7 +91,7 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
}
default:
LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format);
LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format.Value());
UNIMPLEMENTED();
}
@ -91,22 +100,28 @@ static const Math::Vec4<u8> GetPixel(int x, int y) {
static u32 GetDepth(int x, int y) {
const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
u16* depth_buffer = reinterpret_cast<u16*>(Memory::GetPointer(PAddrToVAddr(addr)));
u8* depth_buffer = Memory::GetPointer(PAddrToVAddr(addr));
y = (registers.framebuffer.height - y);
const u32 coarse_y = y & ~7;
u32 stride = registers.framebuffer.width * 2;
// Assuming 16-bit depth buffer format until actual format handling is implemented
return *(depth_buffer + x + y * registers.framebuffer.GetWidth());
return *(u16*)(depth_buffer + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * stride);
}
static void SetDepth(int x, int y, u16 value) {
const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress();
u16* depth_buffer = reinterpret_cast<u16*>(Memory::GetPointer(PAddrToVAddr(addr)));
u8* depth_buffer = Memory::GetPointer(PAddrToVAddr(addr));
y = (registers.framebuffer.height - y);
const u32 coarse_y = y & ~7;
u32 stride = registers.framebuffer.width * 2;
// Assuming 16-bit depth buffer format until actual format handling is implemented
*(depth_buffer + x + y * registers.framebuffer.GetWidth()) = value;
*(u16*)(depth_buffer + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * stride) = value;
}
// NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values

View File

@ -35,4 +35,54 @@ struct TGAHeader {
*/
void DumpTGA(std::string filename, short width, short height, u8* raw_data);
/**
* 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_width = 8;
const unsigned int block_height = 8;
const unsigned int coarse_x = x & ~7;
const unsigned int coarse_y = y & ~7;
u32 i = VideoCore::MortonInterleave(x, y);
const unsigned int offset = coarse_x * block_height;
return (i + offset) * bytes_per_pixel;
}
} // namespace