[N-Gage] Remove optimisations except for native texture handling prior to some rework of the rendering back-end
Build (All) / Create test plan (push) Has been cancelled
Build (All) / level1 (push) Has been cancelled
Build (All) / level2 (push) Has been cancelled

[N-Gage] Set proper brush style to draw filled rects properly.

[N-Gage] Add persistent buffers to avoid per-frame memory allocations (which are expensive)

[N-Gage] Add support for SDL_TEXTURE_ACCESS_TARGET, fixes #13165

[N-Gage] Update README, add hint that the compiler does not support aggregate initializations for structs (knowing this, avoids a lot of headache during debugging)

[N-Gage] Add basic fast-path optimisations for render operations.

[N-Gage] Fix line drawing.
This commit is contained in:
Michael Fitzmayer
2026-04-18 12:52:22 +02:00
parent b181eb4ed0
commit 6e65c3fac4
7 changed files with 509 additions and 867 deletions
+3 -8
View File
@@ -33,14 +33,6 @@ software renderer has been removed.
The outcome is a significantly leaner and more efficient SDL port, which we hope
will breathe new life into this beloved yet obscure platform.
## To the Stubborn Legends of the DC Scene
This port is lovingly dedicated to the ever-nostalgic Dreamcast homebrew scene --
because if we managed to pull this off for the N-Gage (yes, the N-Gage), surely
you guys can stop clinging to SDL2 like it's a rare Shenmue prototype and finally
make the leap to SDL3. It's 2025, not 1999 -- and let's be honest, you're rocking
a state-of-the-art C23 compiler. The irony writes itself.
## Existing Issues and Limitations
- For now, the new
@@ -62,3 +54,6 @@ a state-of-the-art C23 compiler. The irony writes itself.
expected to be resolved in a future update.
- Dependency tracking is currently non-functional.
- The compiler doesn't support aggregate initialization for structs, so
each field must be assigned explicitly.
+36 -30
View File
@@ -155,7 +155,7 @@ static bool NGAGE_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
return false;
}
if (!NGAGE_CreateTextureData(data, texture->w, texture->h)) {
if (!NGAGE_CreateTextureData(data, texture->w, texture->h, texture->access)) {
SDL_free(data);
return false;
}
@@ -283,8 +283,12 @@ static bool NGAGE_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SD
verts->dstrect.h = (int)dstrect->h;
verts->angle = Real2Fix(angle);
verts->center.x = Real2Fix(center->x);
verts->center.y = Real2Fix(center->y);
// Convert center from destination-space to source-space.
// Center is relative to dstrect, but rotation is applied in source texture space.
float center_x_src = (center->x / dstrect->w) * srcquad->w;
float center_y_src = (center->y / dstrect->h) * srcquad->h;
verts->center.x = Real2Fix(center_x_src);
verts->center.y = Real2Fix(center_y_src);
verts->scale_x = Real2Fix(scale_x);
verts->scale_y = Real2Fix(scale_y);
@@ -444,27 +448,24 @@ static bool NGAGE_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, co
return false;
}
void *bitmapData = NGAGE_GetBitmapDataAddress(phdata);
int bitmapPitch = NGAGE_GetBitmapPitch(phdata);
if (!bitmapData || bitmapPitch == 0) {
Uint8 *dst = (Uint8 *)NGAGE_GetBitmapDataAddress(phdata);
if (!dst) {
return false;
}
Uint8 *src = (Uint8 *)pixels;
Uint8 *dst = (Uint8 *)bitmapData + rect->y * bitmapPitch + rect->x * 2; // 2 bytes per pixel for EColor4K
const int bytes_per_pixel = 2;
const int bitmap_pitch = texture->w * bytes_per_pixel;
size_t length = (size_t)rect->w * 2; // 2 bytes per pixel for EColor4K
const Uint8 *src = (const Uint8 *)pixels;
dst += rect->y * bitmap_pitch + rect->x * bytes_per_pixel;
const size_t length = (size_t)rect->w * bytes_per_pixel;
for (int row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += bitmapPitch;
dst += bitmap_pitch;
}
// Mark texture as dirty.
phdata->isDirty = true;
phdata->dirtyRect = *rect;
return true;
}
@@ -476,34 +477,39 @@ static bool NGAGE_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, cons
return false;
}
void *bitmapData = NGAGE_GetBitmapDataAddress(phdata);
int bitmapPitch = NGAGE_GetBitmapPitch(phdata);
if (!bitmapData || bitmapPitch == 0) {
Uint8 *data = (Uint8 *)NGAGE_GetBitmapDataAddress(phdata);
if (!data) {
return false;
}
*pixels = (void *)((Uint8 *)bitmapData + rect->y * bitmapPitch + rect->x * 2); // 2 bytes per pixel for EColor4K
*pitch = bitmapPitch;
// Store the lock rectangle for dirty tracking.
phdata->dirtyRect = *rect;
const int bytes_per_pixel = 2;
const int bitmap_pitch = texture->w * bytes_per_pixel;
*pixels = (void *)(data + rect->y * bitmap_pitch + rect->x * bytes_per_pixel);
*pitch = bitmap_pitch;
return true;
}
static void NGAGE_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
NGAGE_TextureData *phdata = (NGAGE_TextureData *)texture->internal;
if (phdata) {
// Mark texture as dirty after unlock (assume it was modified).
phdata->isDirty = true;
}
}
static bool NGAGE_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
{
NGAGE_RendererData *data = (NGAGE_RendererData *)renderer->internal;
if (texture) {
NGAGE_TextureData *texturedata = (NGAGE_TextureData *)texture->internal;
if (!texturedata || !texturedata->gc) {
return SDL_SetError("Texture is not a render target");
}
data->current_target = texture;
NGAGE_SetRenderTargetInternal(texturedata);
} else {
data->current_target = NULL;
NGAGE_SetRenderTargetInternal(NULL);
}
return true;
}
File diff suppressed because it is too large Load Diff
+8 -18
View File
@@ -34,6 +34,7 @@ extern "C" {
typedef struct NGAGE_RendererData
{
SDL_Rect *viewport;
SDL_Texture *current_target;
} NGAGE_RendererData;
@@ -54,23 +55,14 @@ typedef struct NGAGE_Vertex
} NGAGE_Vertex;
typedef struct CFbsBitmap CFbsBitmap;
typedef struct CFbsBitGc CFbsBitGc;
typedef struct CFbsDevice CFbsDevice;
typedef struct NGAGE_TextureData
{
CFbsBitmap *bitmap;
// Cached properties to avoid repeated API calls.
int cachedWidth;
int cachedHeight;
int cachedPitch;
void *cachedDataAddress;
// Cardinal rotation cache (0°, 90°, 180°, 270°) - created on demand.
CFbsBitmap *cardinalRotations[4];
// Dirty tracking to avoid redundant rendering.
bool isDirty;
SDL_Rect dirtyRect;
CFbsBitGc *gc;
CFbsDevice *device;
} NGAGE_TextureData;
@@ -99,12 +91,9 @@ void NGAGE_Clear(const Uint32 color);
Uint32 NGAGE_ConvertColor(float r, float g, float b, float a, float color_scale);
bool NGAGE_Copy(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcrect, SDL_Rect *dstrect);
bool NGAGE_CopyEx(SDL_Renderer *renderer, SDL_Texture *texture, NGAGE_CopyExData *copydata);
bool NGAGE_CreateTextureData(NGAGE_TextureData *data, const int width, const int height);
bool NGAGE_CreateTextureData(NGAGE_TextureData *data, const int width, const int height, const int access);
void NGAGE_DestroyTextureData(NGAGE_TextureData *data);
void *NGAGE_GetBitmapDataAddress(NGAGE_TextureData *data);
int NGAGE_GetBitmapPitch(NGAGE_TextureData *data);
int NGAGE_GetBitmapWidth(NGAGE_TextureData *data);
int NGAGE_GetBitmapHeight(NGAGE_TextureData *data);
void* NGAGE_GetBitmapDataAddress(NGAGE_TextureData *data);
void NGAGE_DrawLines(NGAGE_Vertex *verts, const int count);
void NGAGE_DrawPoints(NGAGE_Vertex *verts, const int count);
void NGAGE_FillRects(NGAGE_Vertex *verts, const int count);
@@ -113,6 +102,7 @@ void NGAGE_SetClipRect(const SDL_Rect *rect);
void NGAGE_SetDrawColor(const Uint32 color);
void NGAGE_PumpEventsInternal(void);
void NGAGE_SuspendScreenSaverInternal(bool suspend);
void NGAGE_SetRenderTargetInternal(NGAGE_TextureData *target);
#ifdef __cplusplus
}
+13 -33
View File
@@ -23,7 +23,6 @@
#define ngage_video_render_ngage_c_hpp
#include "SDL_render_ngage_c.h"
#include <3dtypes.h>
#include <NRenderer.h>
#include <e32std.h>
#include <w32std.h>
@@ -38,7 +37,7 @@ class CRenderer : public MDirectScreenAccess
void Clear(TUint32 iColor);
bool Copy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_Rect *dstrect);
bool CopyEx(SDL_Renderer *renderer, SDL_Texture *texture, const NGAGE_CopyExData *copydata);
bool CreateTextureData(NGAGE_TextureData *aTextureData, const TInt aWidth, const TInt aHeight);
bool CreateTextureData(NGAGE_TextureData *aTextureData, const TInt aWidth, const TInt aHeight, const TInt aAccess);
void DrawLines(NGAGE_Vertex *aVerts, const TInt aCount);
void DrawPoints(NGAGE_Vertex *aVerts, const TInt aCount);
void FillRects(NGAGE_Vertex *aVerts, const TInt aCount);
@@ -48,6 +47,10 @@ class CRenderer : public MDirectScreenAccess
void UpdateFPS();
void SuspendScreenSaver(TBool aSuspend);
// Render target management.
void SetRenderTarget(NGAGE_TextureData *aTarget);
CFbsBitGc* GetCurrentGc();
// Event handling.
void DisableKeyBlocking();
void HandleEvent(const TWsEvent &aWsEvent);
@@ -88,38 +91,15 @@ class CRenderer : public MDirectScreenAccess
// Screen saver.
TBool iSuspendScreenSaver;
// Work buffers for texture transformations (reusable to avoid per-frame allocations).
void *iWorkBuffer1;
void *iWorkBuffer2;
TInt iWorkBufferSize;
// Render target.
NGAGE_TextureData *iCurrentRenderTarget;
// Temporary render bitmap to avoid destroying source textures.
CFbsBitmap *iTempRenderBitmap;
TInt iTempRenderBitmapWidth;
TInt iTempRenderBitmapHeight;
// Color modulation lookup tables (pre-calculated to avoid per-pixel FixMul).
TUint8 iColorModLUT[768]; // 256 entries each for R, G, B
TFixed iLastColorR;
TFixed iLastColorG;
TFixed iLastColorB;
// Reusable line points buffer to avoid per-frame allocations in DrawLines.
TPoint *iLinePointsBuffer;
TInt iLinePointsBufferCapacity;
// Cached draw color to avoid redundant SetPenColor/SetBrushColor calls.
TUint32 iLastDrawColor;
// Cached clear color to avoid redundant SetBrushColor calls.
TUint32 iLastClearColor;
// Helper methods.
bool EnsureWorkBufferCapacity(TInt aRequiredSize);
bool EnsureTempBitmapCapacity(TInt aWidth, TInt aHeight);
bool EnsureLinePointsCapacity(TInt aRequiredCount);
void BuildColorModLUT(TFixed rf, TFixed gf, TFixed bf);
CFbsBitmap *GetCardinalRotation(NGAGE_TextureData *aTextureData, TInt aAngleIndex);
// Persistent buffers to avoid per-frame allocations.
void *iPixelBufferA;
void *iPixelBufferB;
TInt iPixelBufferSize;
TPoint *iPointsBuffer;
TInt iPointsBufferSize;
};
#endif // ngage_video_render_ngage_c_hpp
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -24,7 +24,7 @@
#include <3dtypes.h>
void ApplyColorMod(void *dest, void *source, int pitch, int width, int height, SDL_FColor color, const TUint8 *colorLUT);
void ApplyColorMod(void *dest, void *source, int pitch, int width, int height, SDL_FColor color);
void ApplyFlip(void *dest, void *source, int pitch, int width, int height, SDL_FlipMode flip);
void ApplyRotation(void *dest, void *source, int pitch, int width, int height, TFixed center_x, TFixed center_y, TFixed angle);
void ApplyScale(void *dest, void *source, int pitch, int width, int height, TFixed center_x, TFixed center_y, TFixed scale_x, TFixed scale_y);