all repos — mgba @ b0726d018582c86a0dbf69a37e494cbce0562f4e

mGBA Game Boy Advance Emulator

Replace bit packed structs in sprites with bitfields
Jeffrey Pfau jeffrey@endrift.com
Thu, 02 Oct 2014 00:19:50 -0700
commit

b0726d018582c86a0dbf69a37e494cbce0562f4e

parent

235141f1f364cd224ac7eb4fbf7a83afefe856f5

3 files changed, 75 insertions(+), 102 deletions(-)

jump to
M src/gba/gba-video.hsrc/gba/gba-video.h

@@ -43,69 +43,45 @@ OBJ_SHAPE_HORIZONTAL = 1,

OBJ_SHAPE_VERTICAL = 2 }; -struct GBAObj { - unsigned y : 8; - unsigned transformed : 1; - unsigned disable : 1; - enum ObjMode mode : 2; - unsigned mosaic : 1; - unsigned multipalette : 1; - enum ObjShape shape : 2; - - int x : 9; - int : 3; - unsigned hflip : 1; - unsigned vflip : 1; - unsigned size : 2; - - unsigned tile : 10; - unsigned priority : 2; - unsigned palette : 4; - - int : 16; -}; - -struct GBATransformedObj { - unsigned y : 8; - unsigned transformed : 1; - unsigned doublesize : 1; - enum ObjMode mode : 2; - unsigned mosaic : 1; - unsigned multipalette : 1; - enum ObjShape shape : 2; +DECL_BITFIELD(GBAObjAttributes, uint32_t); +DECL_BITS(GBAObjAttributes, Y, 0, 8); +DECL_BIT(GBAObjAttributes, Transformed, 8); +DECL_BIT(GBAObjAttributes, Disable, 9); +DECL_BIT(GBAObjAttributes, DoubleSize, 9); +DECL_BITS(GBAObjAttributes, Mode, 10, 2); +DECL_BIT(GBAObjAttributes, Mosaic, 12); +DECL_BIT(GBAObjAttributes, 256Color, 13); +DECL_BITS(GBAObjAttributes, Shape, 14, 2); +DECL_BITS(GBAObjAttributes, X, 16, 9); +DECL_BITS(GBAObjAttributes, MatIndex, 25, 5); +DECL_BIT(GBAObjAttributes, HFlip, 28); +DECL_BIT(GBAObjAttributes, VFlip, 29); +DECL_BITS(GBAObjAttributes, Size, 30, 2); - int x : 9; - unsigned matIndex : 5; - unsigned size : 2; +DECL_BITFIELD(GBAObjExtraAttributes, uint16_t); +DECL_BITS(GBAObjExtraAttributes, Tile, 0, 10); +DECL_BITS(GBAObjExtraAttributes, Priority, 10, 2); +DECL_BITS(GBAObjExtraAttributes, Palette, 12, 4); - unsigned tile : 10; - unsigned priority : 2; - unsigned palette : 4; +struct GBAObj { + GBAObjAttributes attr; + GBAObjExtraAttributes attr2; - int : 16; + uint16_t d; }; union GBAOAM { struct GBAObj obj[128]; - struct GBATransformedObj tobj[128]; struct GBAOAMMatrix { - int : 16; - int : 16; - int : 16; - int a : 16; - int : 16; - int : 16; - int : 16; - int b : 16; - int : 16; - int : 16; - int : 16; - int c : 16; - int : 16; - int : 16; - int : 16; - int d : 16; + int16_t padding0[3]; + int16_t a; + int16_t padding1[3]; + int16_t b; + int16_t padding2[3]; + int16_t c; + int16_t padding3[3]; + int16_t d; } mat[32]; uint16_t raw[512];
M src/gba/renderers/video-software.csrc/gba/renderers/video-software.c

@@ -51,7 +51,7 @@ static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);

static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y); static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y); static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer); -static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBATransformedObj* sprite, int y); +static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y); static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y); static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority);

@@ -395,14 +395,14 @@ int i;

int oamMax = 0; for (i = 0; i < 128; ++i) { struct GBAObj* obj = &renderer->d.oam->obj[i]; - if (obj->transformed || !obj->disable) { - int height = _objSizes[obj->shape * 8 + obj->size * 2 + 1]; - if (obj->transformed) { - height <<= ((struct GBATransformedObj*) obj)->doublesize; + if (GBAObjAttributesIsTransformed(obj->attr) || !GBAObjAttributesIsDisable(obj->attr)) { + int height = _objSizes[GBAObjAttributesGetShape(obj->attr) * 8 + GBAObjAttributesGetSize(obj->attr) * 2 + 1]; + if (GBAObjAttributesIsTransformed(obj->attr)) { + height <<= GBAObjAttributesGetDoubleSize(obj->attr); } - if (obj->y < VIDEO_VERTICAL_PIXELS || obj->y + height >= VIDEO_VERTICAL_TOTAL_PIXELS) { - renderer->sprites[oamMax].y = obj->y; - renderer->sprites[oamMax].endY = obj->y + height; + if (GBAObjAttributesGetY(obj->attr) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesGetY(obj->attr) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) { + renderer->sprites[oamMax].y = GBAObjAttributesGetY(obj->attr); + renderer->sprites[oamMax].endY = GBAObjAttributesGetY(obj->attr) + height; renderer->sprites[oamMax].obj = *obj; ++oamMax; }

@@ -653,18 +653,18 @@ int drawn;

for (i = 0; i < renderer->oamMax; ++i) { int localY = y; struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i]; - if (sprite->obj.mosaic) { + if (GBAObjAttributesIsMosaic(sprite->obj.attr)) { localY = mosaicY; } if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) { continue; } - if (sprite->obj.transformed) { - drawn = _preprocessTransformedSprite(renderer, &sprite->tobj, localY); + if (GBAObjAttributesIsTransformed(sprite->obj.attr)) { + drawn = _preprocessTransformedSprite(renderer, &sprite->obj, localY); } else { drawn = _preprocessSprite(renderer, &sprite->obj, localY); } - spriteLayers |= drawn << sprite->obj.priority; + spriteLayers |= drawn << GBAObjExtraAttributesGetPriority(sprite->obj.attr2); } } }

@@ -1514,18 +1514,18 @@ renderer->row[outX] |= FLAG_OBJWIN; \

} static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) { - int width = _objSizes[sprite->shape * 8 + sprite->size * 2]; - int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1]; + int width = _objSizes[GBAObjAttributesGetShape(sprite->attr) * 8 + GBAObjAttributesGetSize(sprite->attr) * 2]; + int height = _objSizes[GBAObjAttributesGetShape(sprite->attr) * 8 + GBAObjAttributesGetSize(sprite->attr) * 2 + 1]; int start = renderer->start; int end = renderer->end; - uint32_t flags = sprite->priority << OFFSET_PRIORITY; - flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT); - flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN); - int x = sprite->x; + uint32_t flags = GBAObjExtraAttributesGetPriority(sprite->attr2) << OFFSET_PRIORITY; + flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_SEMITRANSPARENT); + flags |= FLAG_OBJWIN * (GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_OBJWIN); + int x = GBAObjAttributesGetX(sprite->attr); uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1]; - unsigned charBase = sprite->tile * 0x20; + unsigned charBase = GBAObjExtraAttributesGetTile(sprite->attr2) * 0x20; int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); - if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) { + if (GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) { // Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance variant = 0; }

@@ -1537,17 +1537,17 @@

int outX = x >= start ? x : start; int condition = x + width; int mosaicH = 1; - if (sprite->mosaic) { + if (GBAObjAttributesIsMosaic(sprite->attr)) { mosaicH = renderer->mosaic.objH + 1; if (condition % mosaicH) { condition += mosaicH - (condition % mosaicH); } } - int inY = y - sprite->y; - if (sprite->y + height - 256 >= 0) { + int inY = y - (int) GBAObjAttributesGetY(sprite->attr); + if ((int) GBAObjAttributesGetY(sprite->attr) + height - 256 >= 0) { inY += 256; } - if (sprite->vflip) { + if (GBAObjAttributesIsVFlip(sprite->attr)) { inY = height - inY - 1; } if (end < condition) {

@@ -1555,15 +1555,15 @@ condition = end;

} int inX = outX - x; int xOffset = 1; - if (sprite->hflip) { + if (GBAObjAttributesIsHFlip(sprite->attr)) { inX = width - inX - 1; xOffset = -1; } - if (!sprite->multipalette) { - palette = &palette[sprite->palette << 4]; + if (!GBAObjAttributesIs256Color(sprite->attr)) { + palette = &palette[GBAObjExtraAttributesGetPalette(sprite->attr2) << 4]; if (flags & FLAG_OBJWIN) { SPRITE_NORMAL_LOOP(16, OBJWIN); - } else if (sprite->mosaic) { + } else if (GBAObjAttributesIsMosaic(sprite->attr)) { SPRITE_MOSAIC_LOOP(16, NORMAL); } else { SPRITE_NORMAL_LOOP(16, NORMAL);

@@ -1571,7 +1571,7 @@ }

} else { if (flags & FLAG_OBJWIN) { SPRITE_NORMAL_LOOP(256, OBJWIN); - } else if (sprite->mosaic) { + } else if (GBAObjAttributesIsMosaic(sprite->attr)) { SPRITE_MOSAIC_LOOP(256, NORMAL); } else { SPRITE_NORMAL_LOOP(256, NORMAL);

@@ -1580,22 +1580,22 @@ }

return 1; } -static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBATransformedObj* sprite, int y) { - int width = _objSizes[sprite->shape * 8 + sprite->size * 2]; - int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1]; - int totalWidth = width << sprite->doublesize; - int totalHeight = height << sprite->doublesize; +static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) { + int width = _objSizes[GBAObjAttributesGetShape(sprite->attr) * 8 + GBAObjAttributesGetSize(sprite->attr) * 2]; + int height = _objSizes[GBAObjAttributesGetShape(sprite->attr) * 8 + GBAObjAttributesGetSize(sprite->attr) * 2 + 1]; + int totalWidth = width << GBAObjAttributesGetDoubleSize(sprite->attr); + int totalHeight = height << GBAObjAttributesGetDoubleSize(sprite->attr); int start = renderer->start; int end = renderer->end; - uint32_t flags = sprite->priority << OFFSET_PRIORITY; - flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT); - flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN); - int x = sprite->x; + uint32_t flags = GBAObjExtraAttributesGetPriority(sprite->attr2) << OFFSET_PRIORITY; + flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_SEMITRANSPARENT); + flags |= FLAG_OBJWIN * (GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_OBJWIN); + int x = GBAObjAttributesGetX(sprite->attr); uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1]; - unsigned charBase = sprite->tile * 0x20; - struct GBAOAMMatrix* mat = &renderer->d.oam->mat[sprite->matIndex]; + unsigned charBase = GBAObjExtraAttributesGetTile(sprite->attr2) * 0x20; + struct GBAOAMMatrix* mat = &renderer->d.oam->mat[GBAObjAttributesGetMatIndex(sprite->attr)]; int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); - if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) { + if (GBAObjAttributesGetMode(sprite->attr) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) { // Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance variant = 0; }

@@ -1603,12 +1603,12 @@ color_t* palette = &renderer->normalPalette[0x100];

if (variant) { palette = &renderer->variantPalette[0x100]; } - int inY = y - sprite->y; + int inY = y - (int) GBAObjAttributesGetY(sprite->attr); if (inY < 0) { inY += 256; } - if (!sprite->multipalette) { - palette = &palette[sprite->palette << 4]; + if (!GBAObjAttributesIs256Color(sprite->attr)) { + palette = &palette[GBAObjExtraAttributesGetPalette(sprite->attr2) << 4]; if (flags & FLAG_OBJWIN) { SPRITE_TRANSFORMED_LOOP(16, OBJWIN); } else {
M src/gba/renderers/video-software.hsrc/gba/renderers/video-software.h

@@ -12,10 +12,7 @@ typedef uint32_t color_t;

#endif struct GBAVideoSoftwareSprite { - union { - struct GBAObj obj; - struct GBATransformedObj tobj; - }; + struct GBAObj obj; int y; int endY; };