Merge commit 'cf0d078a1e13fc9a3f0e290126e738976744c715'

This commit is contained in:
Gary Talent 2023-12-26 12:15:45 -06:00
commit bc6333aedf
13 changed files with 168 additions and 83 deletions

View File

@ -0,0 +1,19 @@
name: Build
run-name: ${{ gitea.actor }} build and test
on: [push]
jobs:
build:
runs-on: nostalgia
steps:
- name: Check out repository code
uses: actions/checkout@v3
- run: make purge configure-debug
- run: make build
- run: make test
- run: make purge configure-asan
- run: make build
- run: make test
- run: make purge configure-release
- run: make build
- run: make test

View File

@ -1,24 +0,0 @@
# This file is a template, and might need editing before it works on your project.
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/C++.gitlab-ci.yml
# use the official gcc image, based on debian
# can use versions as well, like gcc:5.2
# see https://hub.docker.com/_/gcc/
image: gcc
build:
stage: build
variables:
OX_NODEBUG: 1
before_script:
- apt update && apt -y install make cmake ninja-build pkg-config xorg-dev libgtk-3-dev python3 python3-mypy
script:
- make purge configure-release test install
- make purge configure-asan test install

View File

@ -31,7 +31,6 @@ RUN dnf install -y clang \
python3-pip \
libglvnd-devel \
gtk3-devel
RUN pip install conan
###############################################################################
# Install devkitARM

View File

@ -165,21 +165,7 @@ struct [[nodiscard]] Result {
return error == 0;
}
constexpr Error copyTo(type &val) const & noexcept {
if (!error) [[likely]] {
val = value;
}
return error;
}
constexpr Error copyTo(type &val) const && noexcept {
if (!error) [[likely]] {
val = value;
}
return error;
}
constexpr Error copyTo(type &val) & noexcept {
constexpr Error copyTo(type &val) const& noexcept {
if (!error) [[likely]] {
val = value;
}
@ -269,7 +255,7 @@ struct [[nodiscard]] Result {
* @param alt
* @return value of Result or alt
*/
constexpr T orVal(T &&alt) & noexcept {
constexpr T or_value(T &&alt) const& noexcept {
if (error) {
return std::move(alt);
}
@ -281,7 +267,7 @@ struct [[nodiscard]] Result {
* @param alt
* @return value of Result or alt
*/
constexpr T orVal(T &&alt) && noexcept {
constexpr T or_value(T &&alt) && noexcept {
if (error) {
return std::move(alt);
}
@ -293,7 +279,7 @@ struct [[nodiscard]] Result {
* @param alt
* @return value of Result or alt
*/
constexpr T orVal(T const&alt) & noexcept {
constexpr T or_value(T const&alt) const& noexcept {
if (error) {
return alt;
}
@ -305,7 +291,7 @@ struct [[nodiscard]] Result {
* @param alt
* @return value of Result or alt
*/
constexpr T orVal(T const&alt) && noexcept {
constexpr T or_value(T const&alt) && noexcept {
if (error) {
return alt;
}

View File

@ -64,6 +64,54 @@ class Optional {
return *m_ptr;
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T &&alt) const& noexcept {
if (!m_ptr) {
return std::move(alt);
}
return *m_ptr;
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T &&alt) && noexcept {
if (!m_ptr) {
return std::move(alt);
}
return std::move(*m_ptr);
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T const&alt) const& noexcept {
if (!m_ptr) {
return alt;
}
return *m_ptr;
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T const&alt) && noexcept {
if (!m_ptr) {
return alt;
}
return std::move(*m_ptr);
}
constexpr T &operator*() & noexcept {
return *m_ptr;
}

View File

@ -43,6 +43,22 @@ oxModelBegin(Sprite)
oxModelField(priority)
oxModelEnd()
struct BgTile {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.BgTile";
static constexpr auto TypeVersion = 1;
unsigned tileIdx = 0;
unsigned palBank = 0;
unsigned horizontalFlip = false;
unsigned verticalFlip = false;
};
oxModelBegin(BgTile)
oxModelField(tileIdx)
oxModelField(palBank)
oxModelField(horizontalFlip)
oxModelField(verticalFlip)
oxModelEnd()
struct TileSheetSetEntrySection {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.TileSheetSetEntrySection";
static constexpr auto TypeVersion = 1;
@ -86,6 +102,7 @@ oxModelEnd()
ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
ox::FileAddress const&paletteAddr) noexcept;
ox::Error loadSpritePalette(
@ -101,7 +118,7 @@ ox::Error loadBgTileSheet(
Context &ctx,
unsigned cbb,
ox::FileAddress const&tilesheetAddr,
bool loadDefaultPalette = false) noexcept;
ox::Optional<unsigned> const&paletteBank = {}) noexcept;
ox::Error loadSpriteTileSheet(
Context &ctx,
@ -112,7 +129,9 @@ ox::Error loadSpriteTileSheet(
Context &ctx,
TileSheetSet const&set) noexcept;
void setBgTile(Context &ctx, uint_t bgIdx, int column, int row, uint8_t tile) noexcept;
void setBgTile(Context &ctx, uint_t bgIdx, int column, int row, unsigned tile, unsigned palBank = 0) noexcept;
void setBgTile(Context &ctx, uint_t bgIdx, int column, int row, BgTile const&tile) noexcept;
void clearBg(Context &ctx, uint_t bgIdx) noexcept;

View File

@ -158,9 +158,10 @@ ox::Error initGfx(Context&, InitParams const&) noexcept {
ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
ox::FileAddress const&paletteAddr) noexcept {
auto &rom = ctx.rom();
GbaPaletteTarget const palTarget{.palette = MEM_BG_PALETTE};
GbaPaletteTarget const palTarget{.palette = MEM_BG_PALETTE + palBank * 16};
oxRequire(palStat, rom.stat(paletteAddr));
oxRequire(pal, rom.directAccess(paletteAddr));
oxReturnError(ox::readMC(pal, static_cast<std::size_t>(palStat.size), &palTarget));
@ -189,11 +190,11 @@ static ox::Error loadTileSheetSet(
oxRequire(ts, rom.directAccess(entry.tilesheet));
unsigned tilesheetBpp{};
GbaTileMapTarget target{
.bpp = tilesheetBpp,
.defaultPalette = {},
.tileMap = tileMapTargetMem + tileWriteIdx,
.targetBpp = static_cast<unsigned>(set.bpp),
.setEntry = &entry,
.bpp = tilesheetBpp,
.defaultPalette = {},
.tileMap = tileMapTargetMem + tileWriteIdx,
.targetBpp = static_cast<unsigned>(set.bpp),
.setEntry = &entry,
};
oxReturnError(ox::readMC(ts, static_cast<std::size_t>(tsStat.size), &target));
tileWriteIdx += target.tileWriteIdx;
@ -205,7 +206,7 @@ ox::Error loadBgTileSheet(
Context &ctx,
unsigned cbb,
ox::FileAddress const&tilesheetAddr,
bool loadDefaultPalette) noexcept {
ox::Optional<unsigned> const&paletteBank) noexcept {
auto &rom = ctx.rom();
oxRequire(tsStat, rom.stat(tilesheetAddr));
oxRequire(ts, rom.directAccess(tilesheetAddr));
@ -222,8 +223,8 @@ ox::Error loadBgTileSheet(
teagba::bgSetBpp(bgCtl, bpp);
}
});
if (loadDefaultPalette && target.defaultPalette) {
oxReturnError(loadBgPalette(ctx, target.defaultPalette));
if (paletteBank.has_value() && target.defaultPalette) {
oxReturnError(loadBgPalette(ctx, *paletteBank, target.defaultPalette));
}
return {};
}
@ -283,9 +284,14 @@ ox::Error loadSpriteTileSheet(
return {};
}
void setBgTile(Context&, uint_t bgIdx, int column, int row, uint8_t tile) noexcept {
const auto tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column);
MEM_BG_MAP[bgIdx][tileIdx] = tile;
void setBgTile(Context&, uint_t bgIdx, int column, int row, BgTile const&tile) noexcept {
auto const tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column);
// see Tonc 9.3
MEM_BG_MAP[bgIdx][tileIdx] =
static_cast<uint16_t>(tile.tileIdx & 0b1'1111'1111) |
static_cast<uint16_t>(tile.horizontalFlip << 0xa) |
static_cast<uint16_t>(tile.verticalFlip << 0xb) |
static_cast<uint16_t>(tile.palBank << 0xc);
}
void clearBg(Context&, uint_t bgIdx) noexcept {
@ -311,6 +317,11 @@ void setBgStatus(Context&, unsigned bg, bool status) noexcept {
REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
}
void setBgBpp(Context&, unsigned bgIdx, unsigned bpp) noexcept {
auto &bgCtl = regBgCtl(bgIdx);
teagba::bgSetBpp(bgCtl, bpp);
}
void setBgCbb(Context &ctx, unsigned bgIdx, unsigned cbb) noexcept {
auto &bgCtl = regBgCtl(bgIdx);
const auto &cbbData = ctx.cbbData[cbb];

View File

@ -137,13 +137,20 @@ constexpr ox::Array<char, 128> charMap = {
50, // ~
};
void setBgTile(Context &ctx, uint_t bgIdx, int column, int row, unsigned tile, unsigned palBank) noexcept {
setBgTile(ctx, bgIdx, column, row, {
.tileIdx = tile,
.palBank = palBank,
});
}
ox::Error initConsole(Context &ctx) noexcept {
constexpr ox::FileAddress TilesheetAddr = ox::StringLiteral("/TileSheets/Charset.ng");
constexpr ox::FileAddress PaletteAddr = ox::StringLiteral("/Palettes/Charset.npal");
setBgStatus(ctx, 0b0001);
setBgCbb(ctx, 0, 0);
oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr));
return loadBgPalette(ctx, PaletteAddr);
return loadBgPalette(ctx, 0, PaletteAddr);
}
void puts(

View File

@ -24,6 +24,7 @@ class Context {
ox::Array<renderer::CBB, 4> cbbs;
renderer::SpriteBlockset spriteBlocks;
ox::Array<Sprite, 128> spriteStates;
ox::Array<GLfloat, 1024> bgPalette;
ox::Array<renderer::Background, 4> backgrounds;
renderer::Drawer drawer;
uint_t spriteCount = 0;

View File

@ -35,7 +35,9 @@ constexpr ox::CStringView bgvshadTmpl = R"glsl(
in vec2 vTexCoord;
in vec3 vPosition;
in float vTileIdx;
in float vPalOffset;
out vec2 fTexCoord;
out float fPalOffset;
uniform float vXScale;
uniform float vTileHeight;
uniform float vBgIdx;
@ -49,17 +51,19 @@ constexpr ox::CStringView bgvshadTmpl = R"glsl(
fTexCoord = vec2(
vTexCoord.x,
vTexCoord.y * vTileHeight + vTileIdx * vTileHeight);
fPalOffset = vPalOffset;
})glsl";
constexpr ox::CStringView bgfshadTmpl = R"glsl(
{}
out vec4 outColor;
in float fPalOffset;
in vec2 fTexCoord;
uniform sampler2D image;
uniform vec2 fSrcImgSz;
uniform vec4 fPalette[256];
void main() {
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)];
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256) + int(fPalOffset)];
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
if (outColor.a == 0) {
discard;
@ -147,6 +151,7 @@ static void setTileBufferObject(
float y,
float textureTileIdx,
float priority,
float palOffset,
float *vbo,
GLuint *ebo) noexcept {
// don't worry, this memcpy gets optimized to something much more ideal
@ -158,10 +163,10 @@ static void setTileBufferObject(
y += 1.0f - ymod;
auto const prif = priority * PriorityScale;
ox::Array<float, BgVertexVboLength> const vertices {
x, y, prif, 0, 1, textureTileIdx, // bottom left
x + xmod, y, prif, 1, 1, textureTileIdx, // bottom right
x + xmod, y + ymod, prif, 1, 0, textureTileIdx, // top right
x, y + ymod, prif, 0, 0, textureTileIdx, // top left
x, y, prif, 0, 1, textureTileIdx, palOffset, // bottom left
x + xmod, y, prif, 1, 1, textureTileIdx, palOffset, // bottom right
x + xmod, y + ymod, prif, 1, 0, textureTileIdx, palOffset, // top right
x, y + ymod, prif, 0, 0, textureTileIdx, palOffset, // top left
};
memcpy(vbo, vertices.data(), sizeof(vertices));
ox::Array<GLuint, BgVertexEboLength> const elms {
@ -191,6 +196,7 @@ static void initBackgroundBufferObjects(glutils::BufferSet &bs) noexcept {
static_cast<float>(y),
0,
0,
0,
vbo,
ebo);
}
@ -252,6 +258,11 @@ static void initBackgroundBufferset(
glVertexAttribPointer(
heightMultAttr, 1, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
std::bit_cast<void*>(uintptr_t{5 * sizeof(float)}));
auto const palBankAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPalOffset"));
glEnableVertexAttribArray(palBankAttr);
glVertexAttribPointer(
palBankAttr, 1, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
std::bit_cast<void*>(uintptr_t{6 * sizeof(float)}));
}
static glutils::GLTexture createTexture(
@ -337,18 +348,19 @@ static void drawSprites(Context &ctx, ox::Size const&renderSz) noexcept {
}
static void loadPalette(
ox::Array<GLfloat, 1024> &palette,
size_t palOffset,
GLuint shaderPgrm,
Palette const&pal) noexcept {
static constexpr std::size_t ColorCnt = 256;
ox::Array<GLfloat, ColorCnt * 4> palette{};
for (auto i = 0u; const auto c : pal.colors) {
for (auto i = palOffset; auto const c : pal.colors) {
palette[i++] = redf(c);
palette[i++] = greenf(c);
palette[i++] = bluef(c);
palette[i++] = 255;
}
// make first color transparent
palette[3] = 0;
palette[palOffset + 3] = 0;
glUseProgram(shaderPgrm);
const auto uniformPalette = static_cast<GLint>(glGetUniformLocation(shaderPgrm, "fPalette"));
glUniform4fv(uniformPalette, ColorCnt, palette.data());
@ -488,10 +500,11 @@ static ox::Result<TileSheetData> normalizeTileSheet(
ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
ox::FileAddress const&paletteAddr) noexcept {
auto &kctx = keelCtx(ctx.turbineCtx);
oxRequire(palette, readObj<Palette>(kctx, paletteAddr));
renderer::loadPalette(ctx.bgShader, *palette);
renderer::loadPalette(ctx.bgPalette, palBank * 16 * 4, ctx.bgShader, *palette);
return {};
}
@ -500,7 +513,8 @@ ox::Error loadSpritePalette(
ox::FileAddress const&paletteAddr) noexcept {
auto &kctx = keelCtx(ctx.turbineCtx);
oxRequire(palette, readObj<Palette>(kctx, paletteAddr));
renderer::loadPalette(ctx.spriteShader, *palette);
ox::Array<GLfloat, 1024> pal;
renderer::loadPalette(pal, 0, ctx.spriteShader, *palette);
return {};
}
@ -529,14 +543,14 @@ ox::Error loadBgTileSheet(
Context &ctx,
uint_t cbb,
ox::FileAddress const&tilesheetAddr,
bool loadDefaultPalette) noexcept {
ox::Optional<unsigned> const&paletteBank) noexcept {
auto &kctx = keelCtx(ctx.turbineCtx);
oxRequire(tilesheet, readObj<CompactTileSheet>(kctx, tilesheetAddr));
oxRequire(tsd, normalizeTileSheet(*tilesheet));
oxTracef("nostalgia.core.gfx.gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbb, tsd.width, tsd.height);
ctx.cbbs[cbb].tex = renderer::createTexture(tsd.width, tsd.height, tsd.pixels.data());
if (loadDefaultPalette) {
oxReturnError(loadBgPalette(ctx, tilesheet->defaultPalette));
if (paletteBank.has_value() && tilesheet->defaultPalette) {
oxReturnError(loadBgPalette(ctx, *paletteBank, tilesheet->defaultPalette));
}
return {};
}
@ -578,11 +592,11 @@ void setBgTile(
uint_t bgIdx,
int column,
int row,
uint8_t tile) noexcept {
BgTile const&tile) noexcept {
oxTracef(
"nostalgia.core.gfx.setBgTile",
"bgIdx: {}, column: {}, row: {}, tile: {}",
bgIdx, column, row, tile);
"bgIdx: {}, column: {}, row: {}, tile: {}, palBank: {}",
bgIdx, column, row, tile.tileIdx, tile.palBank);
const auto z = static_cast<uint_t>(bgIdx);
const auto y = static_cast<uint_t>(row);
const auto x = static_cast<uint_t>(column);
@ -595,8 +609,9 @@ void setBgTile(
static_cast<uint_t>(i * renderer::BgVertexVboRows),
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(tile),
static_cast<float>(tile.tileIdx),
bg.priority,
static_cast<float>(tile.palBank * 16),
vbo,
ebo);
cbb.updated = true;
@ -628,10 +643,12 @@ bool bgStatus(Context &ctx, uint_t bg) noexcept {
return ctx.backgrounds[bg].enabled;
}
void setBgStatus(Context &ctx, uint_t bg, bool status) noexcept {
void setBgStatus(Context&ctx, uint_t bg, bool status) noexcept {
ctx.backgrounds[bg].enabled = status;
}
void setBgBpp(Context&, unsigned, unsigned) noexcept {}
void setBgCbb(Context &ctx, uint_t bgIdx, uint_t cbbIdx) noexcept {
auto &bg = ctx.backgrounds[bgIdx];
bg.cbbIdx = cbbIdx;

View File

@ -18,7 +18,7 @@ constexpr uint64_t TileRows = 128;
constexpr uint64_t TileColumns = 128;
constexpr uint64_t TileCount = TileRows * TileColumns;
constexpr uint64_t BgVertexVboRows = 4;
constexpr uint64_t BgVertexVboRowLength = 6;
constexpr uint64_t BgVertexVboRowLength = 7;
constexpr uint64_t BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength;
constexpr uint64_t BgVertexEboLength = 6;
constexpr uint64_t SpriteVertexVboRows = 4;

View File

@ -18,7 +18,7 @@ ox::Error Scene::setupDisplay(core::Context &ctx) const noexcept {
}
auto const&palette = m_sceneStatic.palettes[0];
oxReturnError(core::loadBgTileSheet(ctx, 0, m_sceneStatic.tilesheet));
oxReturnError(core::loadBgPalette(ctx, palette));
oxReturnError(core::loadBgPalette(ctx, 0, palette));
// disable all backgrounds
core::setBgStatus(ctx, 0);
for (auto layerNo = 0u; auto const&layer : m_sceneStatic.tileMapIdx) {

View File

@ -96,14 +96,16 @@ static ox::Error runTileSheetSetTest(turbine::Context &tctx) {
{ .tilesheet = ox::StringLiteral("/TileSheets/AB.ng"), .sections{{.begin = 1, .tiles = 1}} },
},
};
constexpr auto bgPalBank = 1;
oxReturnError(core::loadBgTileSheet(*cctx, 0, set));
oxReturnError(core::loadSpriteTileSheet(*cctx, set));
oxReturnError(core::loadBgPalette(*cctx, PaletteAddr));
oxReturnError(core::loadBgPalette(*cctx, bgPalBank, PaletteAddr));
oxReturnError(core::loadBgPalette(*cctx, 0, ox::StringLiteral("/Palettes/Chester.npal")));
oxReturnError(core::loadSpritePalette(*cctx, PaletteAddr));
core::setBgStatus(*cctx, 0, true);
core::setBgTile(*cctx, 0, 10, 9, 1);
core::setBgTile(*cctx, 0, 11, 9, 2);
core::setBgTile(*cctx, 0, 13, 9, 4);
core::setBgTile(*cctx, 0, 10, 9, 1, bgPalBank);
core::setBgTile(*cctx, 0, 11, 9, 2, bgPalBank);
core::setBgTile(*cctx, 0, 13, 9, 4, bgPalBank);
core::setSprite(*cctx, 16, {
.enabled = true,
.x = 12 * 8,