Merge branch 'master' of github.com:fleroviux/Panda3DS into pica-tev-emulation

This commit is contained in:
fleroviux 2023-06-20 22:47:47 +02:00
commit 4cb7e3625b
13 changed files with 427 additions and 412 deletions

View file

@ -47,7 +47,7 @@ class GPU {
};
u64 getVertexShaderInputConfig() {
return u64(regs[PICAInternalRegs::VertexShaderInputCfgLow]) | (u64(regs[PICAInternalRegs::VertexShaderInputCfgHigh]) << 32);
return u64(regs[PICA::InternalRegs::VertexShaderInputCfgLow]) | (u64(regs[PICA::InternalRegs::VertexShaderInputCfgHigh]) << 32);
}
std::array<AttribInfo, maxAttribCount> attributeInfo; // Info for each of the 12 attributes

View file

@ -1,131 +1,224 @@
#pragma once
#include "helpers.hpp"
namespace PICAInternalRegs {
enum : u32 {
// Rasterizer registers
ViewportWidth = 0x41,
ViewportInvw = 0x42,
ViewportHeight = 0x43,
ViewportInvh = 0x44,
namespace PICA {
namespace InternalRegs {
enum : u32 {
// Rasterizer registers
ViewportWidth = 0x41,
ViewportInvw = 0x42,
ViewportHeight = 0x43,
ViewportInvh = 0x44,
DepthScale = 0x4D,
DepthOffset = 0x4E,
ShaderOutputCount = 0x4F,
ShaderOutmap0 = 0x50,
DepthScale = 0x4D,
DepthOffset = 0x4E,
ShaderOutputCount = 0x4F,
ShaderOutmap0 = 0x50,
DepthmapEnable = 0x6D,
TexUnitCfg = 0x80,
DepthmapEnable = 0x6D,
TexUnitCfg = 0x80,
// Framebuffer registers
ColourOperation = 0x100,
BlendFunc = 0x101,
BlendColour = 0x103,
AlphaTestConfig = 0x104,
DepthAndColorMask = 0x107,
DepthBufferFormat = 0x116,
ColourBufferFormat = 0x117,
DepthBufferLoc = 0x11C,
ColourBufferLoc = 0x11D,
FramebufferSize = 0x11E,
// Framebuffer registers
ColourOperation = 0x100,
BlendFunc = 0x101,
BlendColour = 0x103,
AlphaTestConfig = 0x104,
DepthAndColorMask = 0x107,
DepthBufferFormat = 0x116,
ColourBufferFormat = 0x117,
DepthBufferLoc = 0x11C,
ColourBufferLoc = 0x11D,
FramebufferSize = 0x11E,
// Geometry pipeline registers
VertexAttribLoc = 0x200,
AttribFormatLow = 0x201,
AttribFormatHigh = 0x202,
IndexBufferConfig = 0x227,
VertexCountReg = 0x228,
VertexOffsetReg = 0x22A,
SignalDrawArrays = 0x22E,
SignalDrawElements = 0x22F,
// Geometry pipeline registers
VertexAttribLoc = 0x200,
AttribFormatLow = 0x201,
AttribFormatHigh = 0x202,
IndexBufferConfig = 0x227,
VertexCountReg = 0x228,
VertexOffsetReg = 0x22A,
SignalDrawArrays = 0x22E,
SignalDrawElements = 0x22F,
Attrib0Offset = 0x203,
Attrib1Offset = 0x206,
Attrib2Offset = 0x209,
Attrib3Offset = 0x20C,
Attrib4Offset = 0x20F,
Attrib5Offset = 0x212,
Attrib6Offset = 0x215,
Attrib7Offset = 0x218,
Attrib8Offset = 0x21B,
Attrib9Offset = 0x21E,
Attrib10Offset = 0x221,
Attrib11Offset = 0x224,
Attrib0Offset = 0x203,
Attrib1Offset = 0x206,
Attrib2Offset = 0x209,
Attrib3Offset = 0x20C,
Attrib4Offset = 0x20F,
Attrib5Offset = 0x212,
Attrib6Offset = 0x215,
Attrib7Offset = 0x218,
Attrib8Offset = 0x21B,
Attrib9Offset = 0x21E,
Attrib10Offset = 0x221,
Attrib11Offset = 0x224,
Attrib0Config2 = 0x205,
Attrib1Config2 = 0x208,
Attrib2Config2 = 0x20B,
Attrib3Config2 = 0x20E,
Attrib4Config2 = 0x211,
Attrib5Config2 = 0x214,
Attrib6Config2 = 0x217,
Attrib7Config2 = 0x21A,
Attrib8Config2 = 0x21D,
Attrib9Config2 = 0x220,
Attrib10Config2 = 0x223,
Attrib11Config2 = 0x226,
Attrib0Config2 = 0x205,
Attrib1Config2 = 0x208,
Attrib2Config2 = 0x20B,
Attrib3Config2 = 0x20E,
Attrib4Config2 = 0x211,
Attrib5Config2 = 0x214,
Attrib6Config2 = 0x217,
Attrib7Config2 = 0x21A,
Attrib8Config2 = 0x21D,
Attrib9Config2 = 0x220,
Attrib10Config2 = 0x223,
Attrib11Config2 = 0x226,
AttribInfoStart = Attrib0Offset,
AttribInfoEnd = Attrib11Config2,
AttribInfoStart = Attrib0Offset,
AttribInfoEnd = Attrib11Config2,
// Fixed attribute registers
FixedAttribIndex = 0x232,
FixedAttribData0 = 0x233,
FixedAttribData1 = 0x234,
FixedAttribData2 = 0x235,
// Fixed attribute registers
FixedAttribIndex = 0x232,
FixedAttribData0 = 0x233,
FixedAttribData1 = 0x234,
FixedAttribData2 = 0x235,
// Command processor registers
CmdBufSize0 = 0x238,
CmdBufSize1 = 0x239,
CmdBufAddr0 = 0x23A,
CmdBufAddr1 = 0x23B,
CmdBufTrigger0 = 0x23C,
CmdBufTrigger1 = 0x23D,
// Command processor registers
CmdBufSize0 = 0x238,
CmdBufSize1 = 0x239,
CmdBufAddr0 = 0x23A,
CmdBufAddr1 = 0x23B,
CmdBufTrigger0 = 0x23C,
CmdBufTrigger1 = 0x23D,
PrimitiveConfig = 0x25E,
PrimitiveRestart = 0x25F,
PrimitiveConfig = 0x25E,
PrimitiveRestart = 0x25F,
// Vertex shader registers
VertexShaderAttrNum = 0x242,
VertexBoolUniform = 0x2B0,
VertexIntUniform0 = 0x2B1,
VertexIntUniform1 = 0x2B2,
VertexIntUniform2 = 0x2B3,
VertexIntUniform3 = 0x2B4,
// Vertex shader registers
VertexShaderAttrNum = 0x242,
VertexBoolUniform = 0x2B0,
VertexIntUniform0 = 0x2B1,
VertexIntUniform1 = 0x2B2,
VertexIntUniform2 = 0x2B3,
VertexIntUniform3 = 0x2B4,
VertexShaderEntrypoint = 0x2BA,
VertexShaderTransferEnd = 0x2BF,
VertexFloatUniformIndex = 0x2C0,
VertexFloatUniformData0 = 0x2C1,
VertexFloatUniformData1 = 0x2C2,
VertexFloatUniformData2 = 0x2C3,
VertexFloatUniformData3 = 0x2C4,
VertexFloatUniformData4 = 0x2C5,
VertexFloatUniformData5 = 0x2C6,
VertexFloatUniformData6 = 0x2C7,
VertexFloatUniformData7 = 0x2C8,
VertexShaderEntrypoint = 0x2BA,
VertexShaderTransferEnd = 0x2BF,
VertexFloatUniformIndex = 0x2C0,
VertexFloatUniformData0 = 0x2C1,
VertexFloatUniformData1 = 0x2C2,
VertexFloatUniformData2 = 0x2C3,
VertexFloatUniformData3 = 0x2C4,
VertexFloatUniformData4 = 0x2C5,
VertexFloatUniformData5 = 0x2C6,
VertexFloatUniformData6 = 0x2C7,
VertexFloatUniformData7 = 0x2C8,
VertexShaderInputBufferCfg = 0x2B9,
VertexShaderInputCfgLow = 0x2BB,
VertexShaderInputCfgHigh = 0x2BC,
VertexShaderInputBufferCfg = 0x2B9,
VertexShaderInputCfgLow = 0x2BB,
VertexShaderInputCfgHigh = 0x2BC,
VertexShaderTransferIndex = 0x2CB,
VertexShaderData0 = 0x2CC,
VertexShaderData1 = 0x2CD,
VertexShaderData2 = 0x2CE,
VertexShaderData3 = 0x2CF,
VertexShaderData4 = 0x2D0,
VertexShaderData5 = 0x2D1,
VertexShaderData6 = 0x2D2,
VertexShaderData7 = 0x2D3,
VertexShaderOpDescriptorIndex = 0x2D5,
VertexShaderOpDescriptorData0 = 0x2D6,
VertexShaderOpDescriptorData1 = 0x2D7,
VertexShaderOpDescriptorData2 = 0x2D8,
VertexShaderOpDescriptorData3 = 0x2D9,
VertexShaderOpDescriptorData4 = 0x2DA,
VertexShaderOpDescriptorData5 = 0x2DB,
VertexShaderOpDescriptorData6 = 0x2DC,
VertexShaderOpDescriptorData7 = 0x2DD,
VertexShaderTransferIndex = 0x2CB,
VertexShaderData0 = 0x2CC,
VertexShaderData1 = 0x2CD,
VertexShaderData2 = 0x2CE,
VertexShaderData3 = 0x2CF,
VertexShaderData4 = 0x2D0,
VertexShaderData5 = 0x2D1,
VertexShaderData6 = 0x2D2,
VertexShaderData7 = 0x2D3,
VertexShaderOpDescriptorIndex = 0x2D5,
VertexShaderOpDescriptorData0 = 0x2D6,
VertexShaderOpDescriptorData1 = 0x2D7,
VertexShaderOpDescriptorData2 = 0x2D8,
VertexShaderOpDescriptorData3 = 0x2D9,
VertexShaderOpDescriptorData4 = 0x2DA,
VertexShaderOpDescriptorData5 = 0x2DB,
VertexShaderOpDescriptorData6 = 0x2DC,
VertexShaderOpDescriptorData7 = 0x2DD,
};
}
enum class TextureFmt : u32 {
RGBA8 = 0x0,
RGB8 = 0x1,
RGBA5551 = 0x2,
RGB565 = 0x3,
RGBA4 = 0x4,
IA8 = 0x5,
RG8 = 0x6,
I8 = 0x7,
A8 = 0x8,
IA4 = 0x9,
I4 = 0xA,
A4 = 0xB,
ETC1 = 0xC,
ETC1A4 = 0xD,
};
}
enum class ColorFmt : u32 {
RGBA8 = 0x0,
RGB8 = 0x1,
RGBA5551 = 0x2,
RGB565 = 0x3,
RGBA4 = 0x4,
};
enum class DepthFmt : u32 {
Depth16 = 0,
Unknown1 = 1, // Technically selectable, but function is unknown
Depth24 = 2,
Depth24Stencil8 = 3,
};
// Returns the string representation of a texture format
inline constexpr const char* textureFormatToString(TextureFmt fmt) {
switch (fmt) {
case TextureFmt::RGBA8: return "RGBA8";
case TextureFmt::RGB8: return "RGB8";
case TextureFmt::RGBA5551: return "RGBA5551";
case TextureFmt::RGB565: return "RGB565";
case TextureFmt::RGBA4: return "RGBA4";
case TextureFmt::IA8: return "IA8";
case TextureFmt::RG8: return "RG8";
case TextureFmt::I8: return "I8";
case TextureFmt::A8: return "A8";
case TextureFmt::IA4: return "IA4";
case TextureFmt::I4: return "I4";
case TextureFmt::A4: return "A4";
case TextureFmt::ETC1: return "ETC1";
case TextureFmt::ETC1A4: return "ETC1A4";
default: return "Unknown";
}
}
inline constexpr const char* textureFormatToString(ColorFmt fmt) {
return textureFormatToString(static_cast<TextureFmt>(fmt));
}
inline constexpr bool hasStencil(DepthFmt format) { return format == PICA::DepthFmt::Depth24Stencil8; }
// Size occupied by each pixel in bytes
// All formats are 16BPP except for RGBA8 (32BPP) and BGR8 (24BPP)
inline constexpr usize sizePerPixel(TextureFmt format) {
switch (format) {
case TextureFmt::RGB8: return 3;
case TextureFmt::RGBA8: return 4;
default: return 2;
}
}
inline constexpr usize sizePerPixel(ColorFmt format) {
return sizePerPixel(static_cast<TextureFmt>(format));
}
inline constexpr usize sizePerPixel(DepthFmt format) {
switch (format) {
case DepthFmt::Depth16: return 2;
case DepthFmt::Depth24: return 3;
case DepthFmt::Depth24Stencil8: return 4;
default: return 1; // Invalid format
}
}
enum class PrimType : u32 {
TriangleList = 0,
TriangleStrip = 1,
TriangleFan = 2,
GeometryPrimitive = 3,
};
} // namespace PICA

View file

@ -1,6 +1,6 @@
#pragma once
#include <cstdarg>
#include <climits>
#include <cstdarg>
#include <cstdint>
#include <fstream>
#include <iostream>
@ -8,8 +8,16 @@
#include <type_traits>
#include <utility>
#include <vector>
#include "termcolor.hpp"
// We have to detect and special-case AppleClang at the moment since its C++20 support is finicky and doesn't quite support std::bit_cast
#if defined(__clang__) && defined(__apple_build_version__)
#define HELPERS_APPLE_CLANG
#else
#include <bit>
#endif
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
@ -23,78 +31,74 @@ using s32 = std::int32_t;
using s64 = std::int64_t;
namespace Helpers {
[[noreturn]] static void panic(const char* fmt, ...) {
std::va_list args;
va_start(args, fmt);
std::cout << termcolor::on_red << "[FATAL] ";
std::vprintf (fmt, args);
std::cout << termcolor::reset << "\n";
va_end(args);
[[noreturn]] static void panic(const char* fmt, ...) {
std::va_list args;
va_start(args, fmt);
std::cout << termcolor::on_red << "[FATAL] ";
std::vprintf(fmt, args);
std::cout << termcolor::reset << "\n";
va_end(args);
exit(1);
}
exit(1);
}
static void warn(const char* fmt, ...) {
std::va_list args;
va_start(args, fmt);
std::cout << termcolor::on_red << "[Warning] ";
std::vprintf (fmt, args);
std::cout << termcolor::reset << "\n";
va_end(args);
}
static void warn(const char* fmt, ...) {
std::va_list args;
va_start(args, fmt);
std::cout << termcolor::on_red << "[Warning] ";
std::vprintf(fmt, args);
std::cout << termcolor::reset << "\n";
va_end(args);
}
static std::vector <u8> loadROM(std::string directory) {
std::ifstream file (directory, std::ios::binary);
if (file.fail())
panic("Couldn't read %s", directory.c_str());
static std::vector<u8> loadROM(std::string directory) {
std::ifstream file(directory, std::ios::binary);
if (file.fail()) panic("Couldn't read %s", directory.c_str());
std::vector<u8> ROM;
std::vector<u8> ROM;
file.unsetf(std::ios::skipws);
ROM.insert(ROM.begin(),
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());
file.unsetf(std::ios::skipws);
ROM.insert(ROM.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
file.close();
file.close();
printf ("%s loaded successfully\n", directory.c_str());
return ROM;
}
printf("%s loaded successfully\n", directory.c_str());
return ROM;
}
static constexpr bool buildingInDebugMode() {
#ifdef NDEBUG
return false;
#endif
static constexpr bool buildingInDebugMode() {
#ifdef NDEBUG
return false;
#endif
return true;
}
return true;
}
static void debug_printf (const char* fmt, ...) {
if constexpr (buildingInDebugMode()) {
std::va_list args;
va_start(args, fmt);
std::vprintf (fmt, args);
va_end(args);
}
}
static void debug_printf(const char* fmt, ...) {
if constexpr (buildingInDebugMode()) {
std::va_list args;
va_start(args, fmt);
std::vprintf(fmt, args);
va_end(args);
}
}
/// Sign extend an arbitrary-size value to 32 bits
static constexpr u32 inline signExtend32 (u32 value, u32 startingSize) {
auto temp = (s32) value;
auto bitsToShift = 32 - startingSize;
return (u32) (temp << bitsToShift >> bitsToShift);
}
/// Sign extend an arbitrary-size value to 32 bits
static constexpr u32 inline signExtend32(u32 value, u32 startingSize) {
auto temp = (s32)value;
auto bitsToShift = 32 - startingSize;
return (u32)(temp << bitsToShift >> bitsToShift);
}
/// Sign extend an arbitrary-size value to 16 bits
static constexpr u16 signExtend16 (u16 value, u32 startingSize) {
auto temp = (s16) value;
auto bitsToShift = 16 - startingSize;
return (u16) (temp << bitsToShift >> bitsToShift);
}
/// Sign extend an arbitrary-size value to 16 bits
static constexpr u16 signExtend16(u16 value, u32 startingSize) {
auto temp = (s16)value;
auto bitsToShift = 16 - startingSize;
return (u16)(temp << bitsToShift >> bitsToShift);
}
/// Create a mask with `count` number of one bits.
template<typename T, usize count>
static constexpr T ones () {
template <typename T, usize count>
static constexpr T ones() {
constexpr usize bitsize = CHAR_BIT * sizeof(T);
static_assert(count <= bitsize, "count larger than bitsize of T");
@ -105,74 +109,74 @@ namespace Helpers {
}
/// Extract bits from an integer-type
template<usize offset, typename T>
static constexpr T getBit (T value) {
return (value >> offset) & T(1);
template <usize offset, typename T>
static constexpr T getBit(T value) {
return (value >> offset) & T(1);
}
/// Extract bits from an integer-type
template<usize offset, usize bits, typename T>
static constexpr T getBits (T value) {
return (value >> offset) & ones<T, bits>();
template <usize offset, usize bits, typename T>
static constexpr T getBits(T value) {
return (value >> offset) & ones<T, bits>();
}
/// Check if a bit "bit" of value is set
static constexpr bool isBitSet (u32 value, int bit) {
return (value >> bit) & 1;
}
/// Check if a bit "bit" of value is set
static constexpr bool isBitSet(u32 value, int bit) { return (value >> bit) & 1; }
/// rotate number right
template <typename T>
static constexpr T rotr (T value, int bits) {
constexpr auto bitWidth = sizeof(T) * 8;
bits &= bitWidth - 1;
return (value >> bits) | (value << (bitWidth - bits));
}
/// rotate number right
template <typename T>
static constexpr T rotr(T value, int bits) {
constexpr auto bitWidth = sizeof(T) * 8;
bits &= bitWidth - 1;
return (value >> bits) | (value << (bitWidth - bits));
}
// rotate number left
template <typename T>
static constexpr T rotl (T value, int bits) {
constexpr auto bitWidth = sizeof(T) * 8;
bits &= bitWidth - 1;
return (value << bits) | (value >> (bitWidth - bits));
}
// rotate number left
template <typename T>
static constexpr T rotl(T value, int bits) {
constexpr auto bitWidth = sizeof(T) * 8;
bits &= bitWidth - 1;
return (value << bits) | (value >> (bitWidth - bits));
}
/// Used to make the compiler evaluate beeg loops at compile time for the tablegen
template <typename T, T Begin, class Func, T ...Is>
static constexpr void static_for_impl( Func&& f, std::integer_sequence<T, Is...> ) {
( f( std::integral_constant<T, Begin + Is>{ } ),... );
}
/// Used to make the compiler evaluate beeg loops at compile time for the tablegen
template <typename T, T Begin, class Func, T... Is>
static constexpr void static_for_impl(Func&& f, std::integer_sequence<T, Is...>) {
(f(std::integral_constant<T, Begin + Is>{}), ...);
}
template <typename T, T Begin, T End, class Func>
static constexpr void static_for(Func&& f) {
static_for_impl<T, Begin>( std::forward<Func>(f), std::make_integer_sequence<T, End - Begin>{ } );
}
template <typename T, T Begin, T End, class Func>
static constexpr void static_for(Func&& f) {
static_for_impl<T, Begin>(std::forward<Func>(f), std::make_integer_sequence<T, End - Begin>{});
}
// For values < 0x99
static constexpr inline u8 incBCDByte(u8 value) {
return ((value & 0xf) == 0x9) ? value + 7 : value + 1;
}
};
// For values < 0x99
static constexpr inline u8 incBCDByte(u8 value) { return ((value & 0xf) == 0x9) ? value + 7 : value + 1; }
#ifdef HELPERS_APPLE_CLANG
template <class To, class From>
constexpr To bit_cast(const From& from) noexcept {
return *reinterpret_cast<const To*>(&from);
}
#else
template <class To, class From>
constexpr To bit_cast(const From& from) noexcept {
return std::bit_cast<To, From>(from);
}
#endif
}; // namespace Helpers
// UDLs for memory size values
constexpr size_t operator""_KB(unsigned long long int x) {
return 1024ULL * x;
}
constexpr size_t operator""_MB(unsigned long long int x) {
return 1024_KB * x;
}
constexpr size_t operator""_GB(unsigned long long int x) {
return 1024_MB * x;
}
constexpr size_t operator""_KB(unsigned long long int x) { return 1024ULL * x; }
constexpr size_t operator""_MB(unsigned long long int x) { return 1024_KB * x; }
constexpr size_t operator""_GB(unsigned long long int x) { return 1024_MB * x; }
// useful macros
// likely/unlikely
#ifdef __GNUC__
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif

View file

@ -8,6 +8,7 @@
#include "opengl.hpp"
#include "surface_cache.hpp"
#include "textures.hpp"
#include "PICA/regs.hpp"
// More circular dependencies!
class GPU;
@ -59,12 +60,12 @@ class Renderer {
OpenGL::uvec2 fbSize; // The size of the framebuffer (ie both the colour and depth buffer)'
u32 colourBufferLoc; // Location in 3DS VRAM for the colour buffer
ColourBuffer::Formats colourBufferFormat; // Format of the colours stored in the colour buffer
u32 colourBufferLoc; // Location in 3DS VRAM for the colour buffer
PICA::ColorFmt colourBufferFormat; // Format of the colours stored in the colour buffer
// Same for the depth/stencil buffer
u32 depthBufferLoc;
DepthBuffer::Formats depthBufferFormat;
PICA::DepthFmt depthBufferFormat;
// Dummy VAO/VBO for blitting the final output
OpenGL::VertexArray dummyVAO;
@ -93,23 +94,19 @@ class Renderer {
void getGraphicsContext(); // Set up graphics context for rendering
void clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control); // Clear a GPU buffer in VRAM
void displayTransfer(u32 inputAddr, u32 outputAddr, u32 inputSize, u32 outputSize, u32 flags); // Perform display transfer
void drawVertices(OpenGL::Primitives primType, std::span<const Vertex> vertices); // Draw the given vertices
void drawVertices(PICA::PrimType primType, std::span<const Vertex> vertices); // Draw the given vertices
void setFBSize(u32 width, u32 height) {
fbSize.x() = width;
fbSize.y() = height;
}
void setColourFormat(ColourBuffer::Formats format) { colourBufferFormat = format; }
void setColourFormat(u32 format) { colourBufferFormat = static_cast<ColourBuffer::Formats>(format); }
void setDepthFormat(DepthBuffer::Formats format) { depthBufferFormat = format; }
void setDepthFormat(u32 format) {
if (format == 1) {
void setColourFormat(PICA::ColorFmt format) { colourBufferFormat = format; }
void setDepthFormat(PICA::DepthFmt format) {
if (format == PICA::DepthFmt::Unknown1) {
Helpers::panic("[PICA] Undocumented depth-stencil mode!");
}
depthBufferFormat = static_cast<DepthBuffer::Formats>(format);
depthBufferFormat = format;
}
void setColourBufferLoc(u32 loc) { colourBufferLoc = loc; }

View file

@ -1,4 +1,5 @@
#pragma once
#include "PICA/regs.hpp"
#include "boost/icl/interval.hpp"
#include "helpers.hpp"
#include "opengl.hpp"
@ -7,18 +8,8 @@ template <typename T>
using Interval = boost::icl::right_open_interval<T>;
struct ColourBuffer {
enum class Formats : u32 {
RGBA8 = 0,
BGR8 = 1,
RGB5A1 = 2,
RGB565 = 3,
RGBA4 = 4,
Trash1 = 5, Trash2 = 6, Trash3 = 7 // Technically selectable, but their function is unknown
};
u32 location;
Formats format;
PICA::ColorFmt format;
OpenGL::uvec2 size;
bool valid;
@ -30,7 +21,7 @@ struct ColourBuffer {
ColourBuffer() : valid(false) {}
ColourBuffer(u32 loc, Formats format, u32 x, u32 y, bool valid = true)
ColourBuffer(u32 loc, PICA::ColorFmt format, u32 x, u32 y, bool valid = true)
: location(loc), format(format), size({x, y}), valid(valid) {
u64 endLoc = (u64)loc + sizeInBytes();
@ -78,31 +69,14 @@ struct ColourBuffer {
size.x() == other.size.x() && size.y() == other.size.y();
}
// Size occupied by each pixel in bytes
// All formats are 16BPP except for RGBA8 (32BPP) and BGR8 (24BPP)
size_t sizePerPixel() {
switch (format) {
case Formats::BGR8: return 3;
case Formats::RGBA8: return 4;
default: return 2;
}
}
size_t sizeInBytes() {
return (size_t)size.x() * (size_t)size.y() * sizePerPixel();
return (size_t)size.x() * (size_t)size.y() * PICA::sizePerPixel(format);
}
};
struct DepthBuffer {
enum class Formats : u32 {
Depth16 = 0,
Garbage = 1,
Depth24 = 2,
Depth24Stencil8 = 3
};
u32 location;
Formats format;
PICA::DepthFmt format;
OpenGL::uvec2 size; // Implicitly set to the size of the framebuffer
bool valid;
@ -113,7 +87,7 @@ struct DepthBuffer {
DepthBuffer() : valid(false) {}
DepthBuffer(u32 loc, Formats format, u32 x, u32 y, bool valid = true) :
DepthBuffer(u32 loc, PICA::DepthFmt format, u32 x, u32 y, bool valid = true) :
location(loc), format(format), size({x, y}), valid(valid) {
u64 endLoc = (u64)loc + sizeInBytes();
@ -121,10 +95,6 @@ struct DepthBuffer {
range = Interval<u32>(loc, (u32)endLoc);
}
bool hasStencil() {
return format == Formats::Depth24Stencil8;
}
void allocate() {
// Create texture for the FBO, setting up filters and the like
// Reading back the current texture is slow, but allocate calls should be few and far between.
@ -167,18 +137,7 @@ struct DepthBuffer {
size.x() == other.size.x() && size.y() == other.size.y();
}
// Size occupied by each pixel in bytes
size_t sizePerPixel() {
switch (format) {
case Formats::Depth16: return 2;
case Formats::Depth24: return 3;
case Formats::Depth24Stencil8: return 4;
default: return 1; // Invalid format
}
}
size_t sizeInBytes() {
return (size_t)size.x() * (size_t)size.y() * sizePerPixel();
return (size_t)size.x() * (size_t)size.y() * PICA::sizePerPixel(format);
}
};

View file

@ -1,6 +1,7 @@
#pragma once
#include <array>
#include <string>
#include "PICA/regs.hpp"
#include "boost/icl/interval.hpp"
#include "helpers.hpp"
#include "opengl.hpp"
@ -9,28 +10,9 @@ template <typename T>
using Interval = boost::icl::right_open_interval<T>;
struct Texture {
enum class Formats : u32 {
RGBA8 = 0,
RGB8 = 1,
RGBA5551 = 2,
RGB565 = 3,
RGBA4 = 4,
IA8 = 5,
RG8 = 6,
I8 = 7,
A8 = 8,
IA4 = 9,
I4 = 10,
A4 = 11,
ETC1 = 12,
ETC1A4 = 13,
Trash1 = 14, Trash2 = 15 // TODO: What are these?
};
u32 location;
u32 config; // Magnification/minification filter, wrapping configs, etc
Formats format;
PICA::TextureFmt format;
OpenGL::uvec2 size;
bool valid;
@ -41,7 +23,7 @@ struct Texture {
Texture() : valid(false) {}
Texture(u32 loc, Formats format, u32 x, u32 y, u32 config, bool valid = true)
Texture(u32 loc, PICA::TextureFmt format, u32 x, u32 y, u32 config, bool valid = true)
: location(loc), format(format), size({x, y}), config(config), valid(valid) {
u64 endLoc = (u64)loc + sizeInBytes();
@ -62,7 +44,7 @@ struct Texture {
void free();
u64 sizeInBytes();
u32 decodeTexel(u32 u, u32 v, Formats fmt, const void* data);
u32 decodeTexel(u32 u, u32 v, PICA::TextureFmt fmt, const void* data);
// Get the morton interleave offset of a texel based on its U and V values
static u32 mortonInterleave(u32 u, u32 v);
@ -70,12 +52,9 @@ struct Texture {
static u32 getSwizzledOffset(u32 u, u32 v, u32 width, u32 bytesPerPixel);
static u32 getSwizzledOffset_4bpp(u32 u, u32 v, u32 width);
// Returns the string representation of a texture format
static std::string textureFormatToString(Formats fmt);
// Returns the format of this texture as a string
std::string formatToString() {
return textureFormatToString(format);
return PICA::textureFormatToString(format);
}
// Returns the texel at coordinates (u, v) of an ETC1(A4) texture