mirror of
				https://git.suyu.dev/suyu/suyu.git
				synced 2025-11-04 12:34:39 +08:00 
			
		
		
		
	GPU: Added RGB565/RGB8 framebuffer support and various cleanups.
- Centralizes color format encode/decode functions. - Fixes endianness issues. - Implements remaining framebuffer formats in the debugger.
This commit is contained in:
		
							parent
							
								
									44f46254dc
								
							
						
					
					
						commit
						34c31db14a
					
				@ -66,7 +66,7 @@ TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo
 | 
			
		||||
    QComboBox* format_choice = new QComboBox;
 | 
			
		||||
    format_choice->addItem(tr("RGBA8"));
 | 
			
		||||
    format_choice->addItem(tr("RGB8"));
 | 
			
		||||
    format_choice->addItem(tr("RGBA5551"));
 | 
			
		||||
    format_choice->addItem(tr("RGB5A1"));
 | 
			
		||||
    format_choice->addItem(tr("RGB565"));
 | 
			
		||||
    format_choice->addItem(tr("RGBA4"));
 | 
			
		||||
    format_choice->addItem(tr("IA8"));
 | 
			
		||||
 | 
			
		||||
@ -46,7 +46,7 @@ GraphicsFramebufferWidget::GraphicsFramebufferWidget(std::shared_ptr<Pica::Debug
 | 
			
		||||
    framebuffer_format_control = new QComboBox;
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGBA8"));
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGB8"));
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGBA5551"));
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGB5A1"));
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGB565"));
 | 
			
		||||
    framebuffer_format_control->addItem(tr("RGBA4"));
 | 
			
		||||
 | 
			
		||||
@ -199,66 +199,40 @@ void GraphicsFramebufferWidget::OnUpdate()
 | 
			
		||||
    // 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);
 | 
			
		||||
        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) {
 | 
			
		||||
                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;
 | 
			
		||||
    QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
 | 
			
		||||
    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) {
 | 
			
		||||
            const u32 coarse_y = y & ~7;
 | 
			
		||||
            u32 offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * framebuffer_width * bytes_per_pixel;
 | 
			
		||||
            const u8* pixel = color_buffer + offset;
 | 
			
		||||
            Math::Vec4<u8> color = { 0, 0, 0, 0 };
 | 
			
		||||
 | 
			
		||||
                decoded_image.setPixel(x, y, qRgba(value[3], value[2], value[1], 255/*value >> 24*/));
 | 
			
		||||
            switch (framebuffer_format) {
 | 
			
		||||
            case Format::RGBA8:
 | 
			
		||||
                color = Color::DecodeRGBA8(pixel);
 | 
			
		||||
                break;
 | 
			
		||||
            case Format::RGB8:
 | 
			
		||||
                color = Color::DecodeRGB8(pixel);
 | 
			
		||||
                break;
 | 
			
		||||
            case Format::RGB5A1:
 | 
			
		||||
                color = Color::DecodeRGB5A1(pixel);
 | 
			
		||||
                break;
 | 
			
		||||
            case Format::RGB565:
 | 
			
		||||
                color = Color::DecodeRGB565(pixel);
 | 
			
		||||
                break;
 | 
			
		||||
            case Format::RGBA4:
 | 
			
		||||
                color = Color::DecodeRGBA4(pixel);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format);
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            decoded_image.setPixel(x, y, qRgba(color.r(), color.g(), color.b(), 255));
 | 
			
		||||
        }
 | 
			
		||||
        pixmap = QPixmap::fromImage(decoded_image);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Format::RGB8:
 | 
			
		||||
    {
 | 
			
		||||
        QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
 | 
			
		||||
        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) {
 | 
			
		||||
                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*/));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        pixmap = QPixmap::fromImage(decoded_image);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Format::RGBA5551:
 | 
			
		||||
    {
 | 
			
		||||
        QImage decoded_image(framebuffer_width, framebuffer_height, QImage::Format_ARGB32);
 | 
			
		||||
        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) {
 | 
			
		||||
                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);
 | 
			
		||||
                u8 a = Color::Convert1To8(value & 1);
 | 
			
		||||
 | 
			
		||||
                decoded_image.setPixel(x, y, qRgba(r, g, b, 255/*a*/));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        pixmap = QPixmap::fromImage(decoded_image);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    pixmap = QPixmap::fromImage(decoded_image);
 | 
			
		||||
 | 
			
		||||
    framebuffer_address_control->SetValue(framebuffer_address);
 | 
			
		||||
    framebuffer_width_control->setValue(framebuffer_width);
 | 
			
		||||
 | 
			
		||||
@ -29,7 +29,7 @@ class GraphicsFramebufferWidget : public BreakPointObserverDock {
 | 
			
		||||
    enum class Format {
 | 
			
		||||
        RGBA8    = 0,
 | 
			
		||||
        RGB8     = 1,
 | 
			
		||||
        RGBA5551 = 2,
 | 
			
		||||
        RGB5A1   = 2,
 | 
			
		||||
        RGB565   = 3,
 | 
			
		||||
        RGBA4    = 4,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
@ -113,8 +113,8 @@ inline void Write(u32 addr, const T data) {
 | 
			
		||||
    {
 | 
			
		||||
        const auto& config = g_regs.display_transfer_config;
 | 
			
		||||
        if (config.trigger & 1) {
 | 
			
		||||
            u8* source_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalInputAddress()));
 | 
			
		||||
            u8* dest_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalOutputAddress()));
 | 
			
		||||
            u8* src_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalInputAddress()));
 | 
			
		||||
            u8* dst_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalOutputAddress()));
 | 
			
		||||
 | 
			
		||||
            unsigned horizontal_scale = (config.scale_horizontally != 0) ? 2 : 1;
 | 
			
		||||
            unsigned vertical_scale = (config.scale_vertically != 0) ? 2 : 1;
 | 
			
		||||
@ -125,7 +125,7 @@ inline void Write(u32 addr, const T data) {
 | 
			
		||||
            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 * 
 | 
			
		||||
                memcpy(dst_pointer, src_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",
 | 
			
		||||
@ -142,9 +142,7 @@ inline void Write(u32 addr, const T data) {
 | 
			
		||||
            // 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 };
 | 
			
		||||
                    Math::Vec4<u8> src_color = { 0, 0, 0, 0 };
 | 
			
		||||
 | 
			
		||||
                    u32 scaled_x = x * horizontal_scale;
 | 
			
		||||
                    u32 scaled_y = y * vertical_scale;
 | 
			
		||||
@ -170,77 +168,54 @@ inline void Write(u32 addr, const T data) {
 | 
			
		||||
                        dst_offset = (x + y * output_width) * dst_bytes_per_pixel;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    const u8* src_pixel = src_pointer + src_offset;
 | 
			
		||||
                    switch (config.input_format) {
 | 
			
		||||
                    case Regs::PixelFormat::RGBA8:
 | 
			
		||||
                    {
 | 
			
		||||
                        u8* srcptr = source_pointer + src_offset;
 | 
			
		||||
                        source_color.r = srcptr[3]; // red
 | 
			
		||||
                        source_color.g = srcptr[2]; // green
 | 
			
		||||
                        source_color.b = srcptr[1]; // blue
 | 
			
		||||
                        source_color.a = srcptr[0]; // alpha
 | 
			
		||||
                        src_color = Color::DecodeRGBA8(src_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB8:
 | 
			
		||||
                        src_color = Color::DecodeRGB8(src_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB565:
 | 
			
		||||
                        src_color = Color::DecodeRGB565(src_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB5A1:
 | 
			
		||||
                    {
 | 
			
		||||
                        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
 | 
			
		||||
                        source_color.a = Color::Convert1To8(srcval & 0x1);          // alpha
 | 
			
		||||
                        src_color = Color::DecodeRGB5A1(src_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGBA4:
 | 
			
		||||
                    {
 | 
			
		||||
                        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
 | 
			
		||||
                        source_color.a = Color::Convert4To8( srcval        & 0xF); // alpha
 | 
			
		||||
                        src_color = Color::DecodeRGBA4(src_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    default:
 | 
			
		||||
                        LOG_ERROR(HW_GPU, "Unknown source framebuffer format %x", config.input_format.Value());
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    u8* dst_pixel = dst_pointer + dst_offset;
 | 
			
		||||
                    switch (config.output_format) {
 | 
			
		||||
                    case Regs::PixelFormat::RGBA8:
 | 
			
		||||
                    {
 | 
			
		||||
                        u8* dstptr = dest_pointer + dst_offset;
 | 
			
		||||
                        dstptr[3] = source_color.r;
 | 
			
		||||
                        dstptr[2] = source_color.g;
 | 
			
		||||
                        dstptr[1] = source_color.b;
 | 
			
		||||
                        dstptr[0] = source_color.a;
 | 
			
		||||
                        Color::EncodeRGBA8(src_color, dst_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB8:
 | 
			
		||||
                    {
 | 
			
		||||
                        u8* dstptr = dest_pointer + dst_offset;
 | 
			
		||||
                        dstptr[2] = source_color.r; // red
 | 
			
		||||
                        dstptr[1] = source_color.g; // green
 | 
			
		||||
                        dstptr[0] = source_color.b; // blue
 | 
			
		||||
                        Color::EncodeRGB8(src_color, dst_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB565:
 | 
			
		||||
                        Color::EncodeRGB565(src_color, dst_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGB5A1:
 | 
			
		||||
                    {
 | 
			
		||||
                        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);
 | 
			
		||||
                        Color::EncodeRGB5A1(src_color, dst_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    case Regs::PixelFormat::RGBA4:
 | 
			
		||||
                    {
 | 
			
		||||
                        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);
 | 
			
		||||
                        Color::EncodeRGBA4(src_color, dst_pixel);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    default:
 | 
			
		||||
                        LOG_ERROR(HW_GPU, "Unknown destination framebuffer format %x", config.output_format.Value());
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,7 @@ set(HEADERS
 | 
			
		||||
            renderer_opengl/gl_shaders.h
 | 
			
		||||
            renderer_opengl/renderer_opengl.h
 | 
			
		||||
            clipper.h
 | 
			
		||||
            color.h
 | 
			
		||||
            command_processor.h
 | 
			
		||||
            gpu_debugger.h
 | 
			
		||||
            math.h
 | 
			
		||||
 | 
			
		||||
@ -5,47 +5,152 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "common/common_types.h"
 | 
			
		||||
#include "video_core/math.h"
 | 
			
		||||
 | 
			
		||||
namespace Color {
 | 
			
		||||
 | 
			
		||||
/// Convert a 1-bit color component to 8 bit
 | 
			
		||||
static inline u8 Convert1To8(u8 value) {
 | 
			
		||||
inline u8 Convert1To8(u8 value) {
 | 
			
		||||
    return value * 255;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 4-bit color component to 8 bit
 | 
			
		||||
static inline u8 Convert4To8(u8 value) {
 | 
			
		||||
inline u8 Convert4To8(u8 value) {
 | 
			
		||||
    return (value << 4) | value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 5-bit color component to 8 bit
 | 
			
		||||
static inline u8 Convert5To8(u8 value) {
 | 
			
		||||
inline u8 Convert5To8(u8 value) {
 | 
			
		||||
    return (value << 3) | (value >> 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 6-bit color component to 8 bit
 | 
			
		||||
static inline u8 Convert6To8(u8 value) {
 | 
			
		||||
inline u8 Convert6To8(u8 value) {
 | 
			
		||||
    return (value << 2) | (value >> 4);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 8-bit color component to 1 bit
 | 
			
		||||
static inline u8 Convert8To1(u8 value) {
 | 
			
		||||
inline u8 Convert8To1(u8 value) {
 | 
			
		||||
    return value >> 7;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 8-bit color component to 4 bit
 | 
			
		||||
static inline u8 Convert8To4(u8 value) {
 | 
			
		||||
inline u8 Convert8To4(u8 value) {
 | 
			
		||||
    return value >> 4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 8-bit color component to 5 bit
 | 
			
		||||
static inline u8 Convert8To5(u8 value) {
 | 
			
		||||
inline u8 Convert8To5(u8 value) {
 | 
			
		||||
    return value >> 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Convert a 8-bit color component to 6 bit
 | 
			
		||||
static inline u8 Convert8To6(u8 value) {
 | 
			
		||||
inline u8 Convert8To6(u8 value) {
 | 
			
		||||
    return value >> 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decode a color stored in RGBA8 format
 | 
			
		||||
 * @param bytes Pointer to encoded source color
 | 
			
		||||
 * @return Result color decoded as Math::Vec4<u8>
 | 
			
		||||
 */
 | 
			
		||||
inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) {
 | 
			
		||||
    return { bytes[3], bytes[2], bytes[1], bytes[0] };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decode a color stored in RGB8 format
 | 
			
		||||
 * @param bytes Pointer to encoded source color
 | 
			
		||||
 * @return Result color decoded as Math::Vec4<u8>
 | 
			
		||||
 */
 | 
			
		||||
inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) {
 | 
			
		||||
    return { bytes[2], bytes[1], bytes[0], 255 };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decode a color stored in RGB565 format
 | 
			
		||||
 * @param bytes Pointer to encoded source color
 | 
			
		||||
 * @return Result color decoded as Math::Vec4<u8>
 | 
			
		||||
 */
 | 
			
		||||
inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) {
 | 
			
		||||
    const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
 | 
			
		||||
    return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F),
 | 
			
		||||
        Convert5To8(pixel & 0x1F), 255 };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decode a color stored in RGB5A1 format
 | 
			
		||||
 * @param bytes Pointer to encoded source color
 | 
			
		||||
 * @return Result color decoded as Math::Vec4<u8>
 | 
			
		||||
 */
 | 
			
		||||
inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) {
 | 
			
		||||
    const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
 | 
			
		||||
    return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F),
 | 
			
		||||
        Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decode a color stored in RGBA4 format
 | 
			
		||||
 * @param bytes Pointer to encoded source color
 | 
			
		||||
 * @return Result color decoded as Math::Vec4<u8>
 | 
			
		||||
 */
 | 
			
		||||
inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) {
 | 
			
		||||
    const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
 | 
			
		||||
    return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF),
 | 
			
		||||
        Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encode a color as RGBA8 format
 | 
			
		||||
 * @param color Source color to encode
 | 
			
		||||
 * @param bytes Destination pointer to store encoded color
 | 
			
		||||
 */
 | 
			
		||||
inline void EncodeRGBA8(const Math::Vec4<u8>& color, u8* bytes) {
 | 
			
		||||
    bytes[3] = color.r();
 | 
			
		||||
    bytes[2] = color.g();
 | 
			
		||||
    bytes[1] = color.b();
 | 
			
		||||
    bytes[0] = color.a();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encode a color as RGB8 format
 | 
			
		||||
 * @param color Source color to encode
 | 
			
		||||
 * @param bytes Destination pointer to store encoded color
 | 
			
		||||
 */
 | 
			
		||||
inline void EncodeRGB8(const Math::Vec4<u8>& color, u8* bytes) {
 | 
			
		||||
    bytes[2] = color.r();
 | 
			
		||||
    bytes[1] = color.g();
 | 
			
		||||
    bytes[0] = color.b();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encode a color as RGB565 format
 | 
			
		||||
 * @param color Source color to encode
 | 
			
		||||
 * @param bytes Destination pointer to store encoded color
 | 
			
		||||
 */
 | 
			
		||||
inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) {
 | 
			
		||||
    *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) |
 | 
			
		||||
        (Convert8To6(color.g()) << 5) | Convert8To5(color.b());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encode a color as RGB5A1 format
 | 
			
		||||
 * @param color Source color to encode
 | 
			
		||||
 * @param bytes Destination pointer to store encoded color
 | 
			
		||||
 */
 | 
			
		||||
inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) {
 | 
			
		||||
    *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) |
 | 
			
		||||
        (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encode a color as RGBA4 format
 | 
			
		||||
 * @param color Source color to encode
 | 
			
		||||
 * @param bytes Destination pointer to store encoded color
 | 
			
		||||
 */
 | 
			
		||||
inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) {
 | 
			
		||||
    *reinterpret_cast<u16_le*>(bytes) = (Convert8To4(color.r()) << 12) |
 | 
			
		||||
        (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace
 | 
			
		||||
 | 
			
		||||
@ -321,44 +321,32 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
 | 
			
		||||
    switch (info.format) {
 | 
			
		||||
    case Regs::TextureFormat::RGBA8:
 | 
			
		||||
    {
 | 
			
		||||
        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] };
 | 
			
		||||
        auto res = Color::DecodeRGBA8(source + VideoCore::GetMortonOffset(x, y, 4));
 | 
			
		||||
        return { res.r(), res.g(), res.b(), disable_alpha ? 255 : res.a() };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Regs::TextureFormat::RGB8:
 | 
			
		||||
    {
 | 
			
		||||
        const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 3);
 | 
			
		||||
        return { source_ptr[2], source_ptr[1], source_ptr[0], 255 };
 | 
			
		||||
        auto res = Color::DecodeRGB8(source + VideoCore::GetMortonOffset(x, y, 3));
 | 
			
		||||
        return { res.r(), res.g(), res.b(), 255 };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Regs::TextureFormat::RGBA5551:
 | 
			
		||||
    case Regs::TextureFormat::RGB5A1:
 | 
			
		||||
    {
 | 
			
		||||
        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;
 | 
			
		||||
        u8 a = source_ptr & 1;
 | 
			
		||||
        return Math::MakeVec<u8>(Color::Convert5To8(r), Color::Convert5To8(g),
 | 
			
		||||
                                 Color::Convert5To8(b), disable_alpha ? 255 : Color::Convert1To8(a));
 | 
			
		||||
        auto res = Color::DecodeRGB5A1(source + VideoCore::GetMortonOffset(x, y, 2));
 | 
			
		||||
        return { res.r(), res.g(), res.b(), disable_alpha ? 255 : res.a() };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Regs::TextureFormat::RGB565:
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
        return Math::MakeVec<u8>(r, g, b, 255);
 | 
			
		||||
        auto res = Color::DecodeRGB565(source + VideoCore::GetMortonOffset(x, y, 2));
 | 
			
		||||
        return { res.r(), res.g(), res.b(), 255 };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Regs::TextureFormat::RGBA4:
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
        u8 a = Color::Convert4To8(source_ptr[0] & 0xF);
 | 
			
		||||
        return { r, g, b, disable_alpha ? (u8)255 : a };
 | 
			
		||||
        auto res = Color::DecodeRGBA4(source + VideoCore::GetMortonOffset(x, y, 2));
 | 
			
		||||
        return { res.r(), res.g(), res.b(), disable_alpha ? 255 : res.a() };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case Regs::TextureFormat::IA8:
 | 
			
		||||
@ -369,7 +357,7 @@ const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const Texture
 | 
			
		||||
            // Show intensity as red, alpha as green
 | 
			
		||||
            return { source_ptr[1], source_ptr[0], 0, 255 };
 | 
			
		||||
        } else {
 | 
			
		||||
            return { source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
 | 
			
		||||
            return { source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0] };
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -145,7 +145,7 @@ struct Regs {
 | 
			
		||||
    enum class TextureFormat : u32 {
 | 
			
		||||
        RGBA8        =  0,
 | 
			
		||||
        RGB8         =  1,
 | 
			
		||||
        RGBA5551     =  2,
 | 
			
		||||
        RGB5A1       =  2,
 | 
			
		||||
        RGB565       =  3,
 | 
			
		||||
        RGBA4        =  4,
 | 
			
		||||
        IA8          =  5,
 | 
			
		||||
@ -167,7 +167,7 @@ struct Regs {
 | 
			
		||||
        case TextureFormat::RGB8:
 | 
			
		||||
            return 6;
 | 
			
		||||
 | 
			
		||||
        case TextureFormat::RGBA5551:
 | 
			
		||||
        case TextureFormat::RGB5A1:
 | 
			
		||||
        case TextureFormat::RGB565:
 | 
			
		||||
        case TextureFormat::RGBA4:
 | 
			
		||||
        case TextureFormat::IA8:
 | 
			
		||||
@ -413,7 +413,7 @@ struct Regs {
 | 
			
		||||
        enum ColorFormat : u32 {
 | 
			
		||||
            RGBA8    = 0,
 | 
			
		||||
            RGB8     = 1,
 | 
			
		||||
            RGBA5551 = 2,
 | 
			
		||||
            RGB5A1   = 2,
 | 
			
		||||
            RGB565   = 3,
 | 
			
		||||
            RGBA4    = 4,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,6 @@ namespace Rasterizer {
 | 
			
		||||
 | 
			
		||||
static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
 | 
			
		||||
    const PAddr addr = registers.framebuffer.GetColorBufferPhysicalAddress();
 | 
			
		||||
    u8* color_buffer = Memory::GetPointer(PAddrToVAddr(addr));
 | 
			
		||||
 | 
			
		||||
    // Similarly to textures, the render framebuffer is laid out from bottom to top, too.
 | 
			
		||||
    // NOTE: The framebuffer height register contains the actual FB height minus one.
 | 
			
		||||
@ -31,35 +30,28 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
 | 
			
		||||
    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;
 | 
			
		||||
    u8* dst_pixel = Memory::GetPointer(PAddrToVAddr(addr)) + dst_offset;
 | 
			
		||||
 | 
			
		||||
    switch (registers.framebuffer.color_format) {
 | 
			
		||||
    case registers.framebuffer.RGBA8:
 | 
			
		||||
    {
 | 
			
		||||
        u8* pixel = color_buffer + dst_offset;
 | 
			
		||||
        pixel[3] = color.r();
 | 
			
		||||
        pixel[2] = color.g();
 | 
			
		||||
        pixel[1] = color.b();
 | 
			
		||||
        pixel[0] = color.a();
 | 
			
		||||
        Color::EncodeRGBA8(color, dst_pixel);
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB8:
 | 
			
		||||
        Color::EncodeRGB8(color, dst_pixel);
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB5A1:
 | 
			
		||||
        Color::EncodeRGB5A1(color, dst_pixel);
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB565:
 | 
			
		||||
        Color::EncodeRGB565(color, dst_pixel);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGBA4:
 | 
			
		||||
    {
 | 
			
		||||
        u8* pixel = color_buffer + dst_offset;
 | 
			
		||||
        pixel[1] = (color.r() & 0xF0) | (color.g() >> 4);
 | 
			
		||||
        pixel[0] = (color.b() & 0xF0) | (color.a() >> 4);
 | 
			
		||||
        Color::EncodeRGBA4(color, dst_pixel);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGBA5551:
 | 
			
		||||
    {
 | 
			
		||||
        u16_le* pixel = (u16_le*)(color_buffer + dst_offset);
 | 
			
		||||
        *pixel = (Color::Convert8To5(color.r()) << 11) |
 | 
			
		||||
                 (Color::Convert8To5(color.g()) << 6)  |
 | 
			
		||||
                 (Color::Convert8To5(color.b()) << 1)  |
 | 
			
		||||
                 Color::Convert8To1(color.a());
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format.Value());
 | 
			
		||||
@ -69,45 +61,29 @@ static void DrawPixel(int x, int y, const Math::Vec4<u8>& color) {
 | 
			
		||||
 | 
			
		||||
static const Math::Vec4<u8> GetPixel(int x, int y) {
 | 
			
		||||
    const PAddr addr = registers.framebuffer.GetColorBufferPhysicalAddress();
 | 
			
		||||
    u8* color_buffer = Memory::GetPointer(PAddrToVAddr(addr));
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
    Math::Vec4<u8> ret;
 | 
			
		||||
    u8* src_pixel = Memory::GetPointer(PAddrToVAddr(addr)) + src_offset;
 | 
			
		||||
 | 
			
		||||
    switch (registers.framebuffer.color_format) {
 | 
			
		||||
    case registers.framebuffer.RGBA8:
 | 
			
		||||
    {
 | 
			
		||||
        u8* pixel = color_buffer + src_offset;
 | 
			
		||||
        ret.r() = pixel[3];
 | 
			
		||||
        ret.g() = pixel[2];
 | 
			
		||||
        ret.b() = pixel[1];
 | 
			
		||||
        ret.a() = pixel[0];
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
        return Color::DecodeRGBA8(src_pixel);
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB8:
 | 
			
		||||
        return Color::DecodeRGB8(src_pixel);
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB5A1:
 | 
			
		||||
        return Color::DecodeRGB5A1(src_pixel);
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGB565:
 | 
			
		||||
        return Color::DecodeRGB565(src_pixel);
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGBA4:
 | 
			
		||||
    {
 | 
			
		||||
        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);
 | 
			
		||||
        ret.a() = Color::Convert4To8(pixel[0] & 0x0F);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case registers.framebuffer.RGBA5551:
 | 
			
		||||
    {
 | 
			
		||||
        u16_le pixel = *(u16_le*)(color_buffer + src_offset);
 | 
			
		||||
        ret.r() = Color::Convert5To8((pixel >> 11) & 0x1F);
 | 
			
		||||
        ret.g() = Color::Convert5To8((pixel >>  6) & 0x1F);
 | 
			
		||||
        ret.b() = Color::Convert5To8((pixel >>  1) & 0x1F);
 | 
			
		||||
        ret.a() = Color::Convert1To8(pixel & 0x1);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
        return Color::DecodeRGBA4(src_pixel);
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", registers.framebuffer.color_format.Value());
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user