Saturn/PS1/NDS: Use 16bpp bitmaps entirely

This commit is contained in:
UnknownShadow200 2024-06-28 17:21:53 +10:00
parent c17973bd53
commit 1d7a43793a
8 changed files with 32 additions and 64 deletions

View file

@ -21,6 +21,12 @@ struct Stream;
#define BITMAPCOLOR_G_SHIFT 5 #define BITMAPCOLOR_G_SHIFT 5
#define BITMAPCOLOR_R_SHIFT 10 #define BITMAPCOLOR_R_SHIFT 10
#define BITMAPCOLOR_A_SHIFT 15 #define BITMAPCOLOR_A_SHIFT 15
#elif defined CC_BUILD_PS1 || defined CC_BUILD_SATURN || defined CC_BUILD_NDS
#define BITMAPCOLOR_R_SHIFT 0
#define BITMAPCOLOR_G_SHIFT 5
#define BITMAPCOLOR_B_SHIFT 10
#define BITMAPCOLOR_A_SHIFT 15
#define BITMAP_16BPP
#else #else
#define BITMAPCOLOR_B_SHIFT 0 #define BITMAPCOLOR_B_SHIFT 0
#define BITMAPCOLOR_G_SHIFT 8 #define BITMAPCOLOR_G_SHIFT 8

View file

@ -94,28 +94,23 @@ void Gfx_EndFrame(void) {
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------Textures--------------------------------------------------------* *---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
// B8 G8 R8 A8 > R5 G5 B5 A1
#define BGRA8_to_DS(src) \
((src[2] & 0xF8) >> 3) | ((src[1] & 0xF8) << 2) | ((src[0] & 0xF8) << 7) | ((src[3] & 0x80) << 8);
static void ConvertTexture(cc_uint16* dst, struct Bitmap* bmp, int rowWidth) {
for (int y = 0; y < bmp->height; y++)
{
cc_uint8* src = (cc_uint8*)(bmp->scan0 + y * rowWidth);
for (int x = 0; x < bmp->width; x++, src += 4)
{
*dst++ = BGRA8_to_DS(src);
}
}
}
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) { static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) {
vramSetBankA(VRAM_A_TEXTURE); vramSetBankA(VRAM_A_TEXTURE);
cc_uint16* tmp = Mem_TryAlloc(bmp->width * bmp->height, 2); cc_uint16* tmp = Mem_TryAlloc(bmp->width * bmp->height, 2);
if (!tmp) return 0; if (!tmp) return 0;
ConvertTexture(tmp, bmp, rowWidth);
// TODO: Only copy when rowWidth != bmp->width
for (int y = 0; y < bmp->height; y++)
{
cc_uint16* src = bmp->scan0 + y * rowWidth;
cc_uint16* dst = tmp + y * bmp->width;
for (int x = 0; x < bmp->width; x++)
{
dst[x] = src[x];
}
}
int textureID; int textureID;
glGenTextures(1, &textureID); glGenTextures(1, &textureID);
@ -148,11 +143,11 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i
for (int yy = 0; yy < part->height; yy++) for (int yy = 0; yy < part->height; yy++)
{ {
cc_uint16* dst = vram_ptr + width * (y + yy) + x; cc_uint16* dst = vram_ptr + width * (y + yy) + x;
cc_uint8* src = (cc_uint8*)(part->scan0 + rowWidth * yy); cc_uint16* src = part->scan0 + rowWidth * yy;
for (int xx = 0; xx < part->width; xx++, src += 4, dst++) for (int xx = 0; xx < part->width; xx++)
{ {
*dst = BGRA8_to_DS(src); *dst++ = *src++;
} }
} }
} }

View file

@ -210,21 +210,18 @@ typedef struct GPUTexture {
static GPUTexture textures[TEXTURES_MAX_COUNT]; static GPUTexture textures[TEXTURES_MAX_COUNT];
static GPUTexture* curTex; static GPUTexture* curTex;
#define BGRA8_to_PS1(src) \
((src[2] & 0xF8) >> 3) | ((src[1] & 0xF8) << 2) | ((src[0] & 0xF8) << 7) | ((src[3] & 0x80) << 8)
static void* AllocTextureAt(int i, struct Bitmap* bmp, int rowWidth) { static void* AllocTextureAt(int i, struct Bitmap* bmp, int rowWidth) {
cc_uint16* tmp = Mem_TryAlloc(bmp->width * bmp->height, 2); cc_uint16* tmp = Mem_TryAlloc(bmp->width * bmp->height, 2);
if (!tmp) return NULL; if (!tmp) return NULL;
// TODO: Only copy when rowWidth != bmp->width
for (int y = 0; y < bmp->height; y++) for (int y = 0; y < bmp->height; y++)
{ {
cc_uint32* src = bmp->scan0 + y * rowWidth; cc_uint16* src = bmp->scan0 + y * rowWidth;
cc_uint16* dst = tmp + y * bmp->width; cc_uint16* dst = tmp + y * bmp->width;
for (int x = 0; x < bmp->width; x++) { for (int x = 0; x < bmp->width; x++) {
cc_uint8* color = (cc_uint8*)&src[x]; dst[x] = src[x];
dst[x] = BGRA8_to_PS1(color);
} }
} }

View file

@ -122,11 +122,6 @@ void Gfx_Free(void) {
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------Textures--------------------------------------------------------* *---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#define BGRA8_to_SATURN(src) \
((src[1] & 0xF8) >> 3) | ((src[2] & 0xF8) << 2) | ((src[3] & 0xF8) << 7) | 0x8000
// ((src[2] & 0xF8) >> 3) | ((src[1] & 0xF8) << 2) | ((src[0] & 0xF8) << 7) | ((src[3] & 0x80) << 8)
typedef struct CCTexture { typedef struct CCTexture {
int width, height; int width, height;
void* addr; void* addr;
@ -145,15 +140,15 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8
tex_vram_addr += tex->width * tex->height * 2; tex_vram_addr += tex->width * tex->height * 2;
// TODO: Only copy when rowWidth != bmp->width
for (int y = 0; y < bmp->height; y++) for (int y = 0; y < bmp->height; y++)
{ {
cc_uint32* src = bmp->scan0 + y * rowWidth; cc_uint16* src = bmp->scan0 + y * rowWidth;
cc_uint16* dst = tmp + y * bmp->width; cc_uint16* dst = tmp + y * bmp->width;
for (int x = 0; x < bmp->width; x++) for (int x = 0; x < bmp->width; x++)
{ {
cc_uint8* color = (cc_uint8*)&src[x]; dst[x] = src[x];
dst[x] = BGRA8_to_SATURN(color);
} }
} }

View file

@ -91,7 +91,7 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* path) {
} }
cc_result Directory_Create(const cc_filepath* path) { cc_result Directory_Create(const cc_filepath* path) {
return ERR_NOT_SUPPORTED; return ReturnCode_DirectoryExists;
} }
int File_Exists(const cc_string* path) { int File_Exists(const cc_string* path) {
@ -103,7 +103,7 @@ cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCall
} }
cc_result File_Open(cc_file* file, const cc_filepath* path) { cc_result File_Open(cc_file* file, const cc_filepath* path) {
return ERR_NOT_SUPPORTED; return ReturnCode_FileNotFound;
} }
cc_result File_Create(cc_file* file, const cc_filepath* path) { cc_result File_Create(cc_file* file, const cc_filepath* path) {

View file

@ -272,10 +272,7 @@ void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
for (int x = r.x; x < r.x + r.width; x++) for (int x = r.x; x < r.x + r.width; x++)
{ {
BitmapCol color = src[x]; dst[x] = src[x];
// 888 to 565 (discard least significant bits)
// quoting libDNS: < Bitmap background with 16 bit color values of the form aBBBBBGGGGGRRRRR (if 'a' is not set, the pixel will be transparent)
dst[x] = 0x8000 | ((BitmapCol_B(color) & 0xF8) << 7) | ((BitmapCol_G(color) & 0xF8) << 2) | (BitmapCol_R(color) >> 3);
} }
} }

View file

@ -154,7 +154,6 @@ void Gamepads_Process(float delta) {
*------------------------------------------------------Framebuffer--------------------------------------------------------* *------------------------------------------------------Framebuffer--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static DISPENV disp; static DISPENV disp;
static cc_uint16* fb;
void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) { void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) {
SetDefDispEnv(&disp, 0, 0, SCREEN_XRES, SCREEN_YRES); SetDefDispEnv(&disp, 0, 0, SCREEN_XRES, SCREEN_YRES);
@ -166,13 +165,8 @@ void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) {
bmp->scan0 = (BitmapCol*)Mem_Alloc(width * height, BITMAPCOLOR_SIZE, "window pixels"); bmp->scan0 = (BitmapCol*)Mem_Alloc(width * height, BITMAPCOLOR_SIZE, "window pixels");
bmp->width = width; bmp->width = width;
bmp->height = height; bmp->height = height;
fb = Mem_Alloc(width * height, 2, "real surface");
} }
#define BGRA8_to_PS1(src) \
((src[2] & 0xF8) >> 3) | ((src[1] & 0xF8) << 2) | ((src[0] & 0xF8) << 7) | 0x8000
void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) { void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
RECT rect; RECT rect;
rect.x = 0; rect.x = 0;
@ -180,24 +174,12 @@ void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
rect.w = SCREEN_XRES; rect.w = SCREEN_XRES;
rect.h = SCREEN_YRES; rect.h = SCREEN_YRES;
for (int y = r.y; y < r.y + r.height; y++) LoadImage(&rect, bmp->scan0);
{
cc_uint32* src = Bitmap_GetRow(bmp, y);
cc_uint16* dst = fb + y * bmp->width;
for (int x = r.x; x < r.x + r.width; x++) {
cc_uint8* color = (cc_uint8*)&src[x];
dst[x] = BGRA8_to_PS1(color);
}
}
LoadImage(&rect, fb);
DrawSync(0); DrawSync(0);
} }
void Window_FreeFramebuffer(struct Bitmap* bmp) { void Window_FreeFramebuffer(struct Bitmap* bmp) {
Mem_Free(bmp->scan0); Mem_Free(bmp->scan0);
Mem_Free(fb);
} }

View file

@ -210,11 +210,7 @@ void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
for (int x = r.x; x < r.x + r.width; x++) for (int x = r.x; x < r.x + r.width; x++)
{ {
// TODO optimise // TODO optimise
BitmapCol col = row[x]; vram[x + (y * 512)].raw = row[x];
cc_uint8 R = BitmapCol_R(col);
cc_uint8 G = BitmapCol_G(col);
cc_uint8 B = BitmapCol_B(col);
vram[x + (y * 512)] = RGB1555(0, R >> 3, G >> 3, B >> 3);
} }
} }