Compare commits

..

No commits in common. "9840b6fdee26aca36771e8576d2cc88cbbb87a9f" and "0093778f646d60c0f0a0c2a04d026aac08b63b8e" have entirely different histories.

5 changed files with 56 additions and 88 deletions

View File

@ -101,7 +101,7 @@ Result<FileStat> PassThroughFS::statPath(CRStringView path) const noexcept {
oxTracef("ox.fs.PassThroughFS.statInode", "{} {}", ec.message(), path);
const uint64_t size = type == FileType::Directory ? 0 : std::filesystem::file_size(p, ec);
oxTracef("ox.fs.PassThroughFS.statInode", "{} {}", ec.message(), path);
oxTracef("ox.fs.PassThroughFS.statInode.size", "{} {}", path, size);
oxTracef("ox.fs.PassThroughFS.statInode::size", "{} {}", path, size);
oxReturnError(OxError(static_cast<ox::ErrorCode>(ec.value()), "PassThroughFS: stat failed"));
return FileStat{0, 0, size, type};
}

View File

@ -23,9 +23,6 @@ struct Sprite {
unsigned spriteShape = 0;
unsigned spriteSize = 0;
unsigned flipX = 0;
/**
* Valid priorities: 0-3
*/
unsigned priority = 0;
};
@ -52,8 +49,6 @@ void setBgStatus(Context &ctx, unsigned bg, bool status) noexcept;
void setBgCbb(Context &ctx, unsigned bgIdx, unsigned cbb) noexcept;
void setBgPriority(Context &ctx, uint_t bgIdx, uint_t priority) noexcept;
/**
* @param section describes which section of the selected TileSheetSpace to use (e.g. MEM_PALLETE_BG[section])
*/

View File

@ -97,16 +97,15 @@ void setBgStatus(Context&, unsigned bg, bool status) noexcept {
REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
}
void setBgCbb(Context&, unsigned bgIdx, unsigned cbb) noexcept {
static void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
auto &bgCtl = regBgCtl(bgIdx);
const auto &cbbData = g_cbbData[cbb];
teagba::bgSetBpp(&bgCtl, cbbData.bpp);
teagba::bgSetCbb(&bgCtl, cbb);
}
void setBgPriority(Context&, uint_t bgIdx, uint_t priority) noexcept {
auto &bgCtl = regBgCtl(bgIdx);
bgCtl = (bgCtl & 0b1111'1111'1111'1100u) | (priority & 0b11);
void setBgCbb(Context&, unsigned bgIdx, unsigned cbb) noexcept {
setBgCbb(nullptr, bgIdx, cbb);
}
static ox::Error loadBgTileSheet(
@ -216,24 +215,24 @@ void clearTileLayer(Context&, unsigned bgIdx) noexcept {
[[maybe_unused]]
void hideSprite(Context&, unsigned idx) noexcept {
//oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
teagba::addSpriteUpdate({
.attr0 = uint16_t{0b11 << 8},
.idx = static_cast<uint16_t>(idx),
});
teagba::GbaSpriteAttrUpdate oa;
oa.attr0 = uint16_t{0b11 << 8};
oa.idx = static_cast<uint16_t>(idx);
teagba::addSpriteUpdate(oa);
}
[[maybe_unused]]
void showSprite(Context&, unsigned idx) noexcept {
//oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
teagba::addSpriteUpdate({
.attr0 = 0,
.idx = static_cast<uint16_t>(idx),
});
teagba::GbaSpriteAttrUpdate oa;
oa.attr0 &= uint16_t{0b1111'1100'1111'1111};
oa.idx = static_cast<uint16_t>(idx);
teagba::addSpriteUpdate(oa);
}
void setSprite(Context&, uint_t idx, Sprite const&s) noexcept {
//oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
teagba::addSpriteUpdate({
teagba::GbaSpriteAttrUpdate const oa{
.attr0 = static_cast<uint16_t>(
(static_cast<uint16_t>(s.y & ox::onMask<uint8_t>(0b111'1111)))
| (static_cast<uint16_t>(1) << 10) // enable alpha
@ -246,7 +245,8 @@ void setSprite(Context&, uint_t idx, Sprite const&s) noexcept {
(static_cast<uint16_t>(s.tileIdx & ox::onMask<uint16_t>(8)))
| (static_cast<uint16_t>(s.priority & 0b11) << 10)),
.idx = static_cast<uint16_t>(idx),
});
};
teagba::addSpriteUpdate(oa);
}
}

View File

@ -20,7 +20,6 @@
namespace nostalgia::core {
constexpr auto Scale = 1;
constexpr auto PriorityScale = 0.01f;
namespace renderer {
@ -33,19 +32,16 @@ void Drawer::draw(turbine::Context &tctx) noexcept {
constexpr ox::CStringView bgvshadTmpl = R"glsl(
{}
in vec2 vTexCoord;
in vec3 vPosition;
in vec2 vPosition;
in float vTileIdx;
out vec2 fTexCoord;
uniform float vXScale;
uniform float vTileHeight;
uniform float vBgIdx;
void main() {
float xScaleInvert = 1.0 - vXScale;
gl_Position = vec4(
vPosition.x * vXScale - xScaleInvert,
vPosition.y,
vPosition.z - 0.001 * vBgIdx,
1.0);
vPosition.x * vXScale - xScaleInvert, vPosition.y,
0.5, 1.0);
fTexCoord = vec2(
vTexCoord.x,
vTexCoord.y * vTileHeight + vTileIdx * vTileHeight);
@ -59,11 +55,10 @@ constexpr ox::CStringView bgfshadTmpl = R"glsl(
uniform vec2 fSrcImgSz;
uniform vec4 fPalette[256];
void main() {
vec2 pixelSz = vec2(1, 1) / fSrcImgSz;
vec2 pixelCoord = floor(fTexCoord / pixelSz) * pixelSz;
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)];
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
if (outColor.a == 0) {
discard;
}
})glsl";
constexpr ox::CStringView spritevshadTmpl = R"glsl(
@ -77,11 +72,8 @@ constexpr ox::CStringView spritevshadTmpl = R"glsl(
void main() {
float xScaleInvert = 1.0 - vXScale;
gl_Position = vec4(
vPosition.x * vXScale - xScaleInvert,
vPosition.y,
// offset to ensure sprites draw on top of BGs by default
vPosition.z - 0.004,
1.0) * vEnabled;
vPosition.x * vXScale - xScaleInvert, vPosition.y,
vPosition.z, 1.0) * vEnabled;
fTexCoord = vTexCoord * vec2(1, vTileHeight);
})glsl";
@ -93,11 +85,10 @@ constexpr ox::CStringView spritefshadTmpl = R"glsl(
uniform vec2 fSrcImgSz;
uniform vec4 fPalette[256];
void main() {
vec2 pixelSz = vec2(1, 1) / fSrcImgSz;
vec2 pixelCoord = floor(fTexCoord / pixelSz) * pixelSz;
outColor = fPalette[int(texture(image, fTexCoord).rgb.r * 256)];
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
if (outColor.a == 0) {
discard;
}
})glsl";;
[[nodiscard]]
@ -122,7 +113,7 @@ static void setSpriteBufferObject(
y *= -ymod;
x -= 1.f;
y += 1.f - ymod;
auto const prif = static_cast<float>(priority) * PriorityScale;
auto const prif = static_cast<float>(priority) * 0.1f;
auto const textureRowf = static_cast<float>(textureRow);
float const L = flipX ? 1 : 0;
float const R = flipX ? 0 : 1;
@ -146,7 +137,6 @@ static void setTileBufferObject(
float x,
float y,
float textureTileIdx,
float priority,
float *vbo,
GLuint *ebo) noexcept {
// don't worry, this memcpy gets optimized to something much more ideal
@ -156,12 +146,11 @@ static void setTileBufferObject(
y *= -ymod;
x -= 1.0f;
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, 0, 1, textureTileIdx, // bottom left
x + xmod, y, 1, 1, textureTileIdx, // bottom right
x + xmod, y + ymod, 1, 0, textureTileIdx, // top right
x, y + ymod, 0, 0, textureTileIdx, // top left
};
memcpy(vbo, vertices.data(), sizeof(vertices));
ox::Array<GLuint, BgVertexEboLength> const elms {
@ -179,18 +168,17 @@ static void initSpriteBufferObjects(glutils::BufferSet &bs) noexcept {
}
}
static void initBackgroundBufferObjects(glutils::BufferSet &bs, float priority) noexcept {
static void initBackgroundBufferObjects(glutils::BufferSet &bg) noexcept {
for (auto x = 0u; x < TileColumns; ++x) {
for (auto y = 0u; y < TileRows; ++y) {
const auto i = bgVertexRow(x, y);
auto vbo = &bs.vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
auto ebo = &bs.elements[i * static_cast<std::size_t>(BgVertexEboLength)];
auto vbo = &bg.vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
auto ebo = &bg.elements[i * static_cast<std::size_t>(BgVertexEboLength)];
setTileBufferObject(
static_cast<uint_t>(i * BgVertexVboRows),
static_cast<float>(x),
static_cast<float>(y),
0,
priority,
vbo,
ebo);
}
@ -228,31 +216,30 @@ static void initSpritesBufferset(Context &ctx) noexcept {
static void initBackgroundBufferset(
GLuint shader,
glutils::BufferSet &bs,
float priority) noexcept {
glutils::BufferSet &bg) noexcept {
// vao
bs.vao = glutils::generateVertexArrayObject();
glBindVertexArray(bs.vao);
bg.vao = glutils::generateVertexArrayObject();
glBindVertexArray(bg.vao);
// vbo & ebo
bs.vbo = glutils::generateBuffer();
bs.ebo = glutils::generateBuffer();
initBackgroundBufferObjects(bs, priority);
glutils::sendVbo(bs);
glutils::sendEbo(bs);
bg.vbo = glutils::generateBuffer();
bg.ebo = glutils::generateBuffer();
initBackgroundBufferObjects(bg);
glutils::sendVbo(bg);
glutils::sendEbo(bg);
// vbo layout
auto const posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
glEnableVertexAttribArray(posAttr);
glVertexAttribPointer(posAttr, 3, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
auto const texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
glEnableVertexAttribArray(texCoordAttr);
glVertexAttribPointer(
texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
std::bit_cast<void*>(uintptr_t{3 * sizeof(float)}));
std::bit_cast<void*>(uintptr_t{2 * sizeof(float)}));
auto const heightMultAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTileIdx"));
glEnableVertexAttribArray(heightMultAttr);
glVertexAttribPointer(
heightMultAttr, 1, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
std::bit_cast<void*>(uintptr_t{5 * sizeof(float)}));
std::bit_cast<void*>(uintptr_t{4 * sizeof(float)}));
}
static glutils::GLTexture createTexture(
@ -292,12 +279,10 @@ static void drawBackgrounds(
const auto uniformSrcImgSz = glGetUniformLocation(ctx.bgShader, "fSrcImgSz");
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vXScale"));
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vTileHeight"));
const auto uniformBgIdx = static_cast<GLint>(glGetUniformLocation(ctx.bgShader, "vBgIdx"));
const auto [wi, hi] = renderSz;
const auto wf = static_cast<float>(wi);
const auto hf = static_cast<float>(hi);
glUniform1f(uniformXScale, hf / wf);
auto bgIdx = 0.f;
for (const auto &bg : ctx.backgrounds) {
if (bg.enabled) {
auto &cbb = ctx.cbbs[bg.cbbIdx];
@ -307,9 +292,7 @@ static void drawBackgrounds(
uniformSrcImgSz,
static_cast<float>(cbb.tex.width),
static_cast<float>(cbb.tex.height));
glUniform1f(uniformBgIdx, bgIdx);
drawBackground(cbb);
++bgIdx;
}
}
}
@ -340,7 +323,7 @@ static void drawSprites(Context &ctx, ox::Size const&renderSz) noexcept {
static void loadPalette(
GLuint shaderPgrm,
Palette const&pal,
bool firstIsTransparent) noexcept {
bool firstIsTransparent = false) noexcept {
static constexpr std::size_t ColorCnt = 256;
ox::Array<GLfloat, ColorCnt * 4> palette{};
for (auto i = 0u; const auto c : pal.colors) {
@ -358,7 +341,7 @@ static void loadPalette(
}
static void loadBgPalette(Context &ctx, Palette const&pal) noexcept {
loadPalette(ctx.bgShader, pal, true);
loadPalette(ctx.bgShader, pal);
}
static void loadSpritePalette(Context &ctx, Palette const&pal) noexcept {
@ -433,7 +416,7 @@ static void setSprite(
fY,
s.tileIdx + i,
s.flipX,
s.priority & 0b11,
s.priority,
vbo,
ebo);
++i;
@ -473,8 +456,8 @@ ox::Error initGfx(
oxReturnError(glutils::buildShaderProgram(bgVshad, bgFshad).moveTo(ctx.bgShader));
oxReturnError(
glutils::buildShaderProgram(spriteVshad, spriteFshad).moveTo(ctx.spriteShader));
for (auto &cbb : ctx.cbbs) {
initBackgroundBufferset(ctx.bgShader, cbb, 0);
for (auto &bg : ctx.cbbs) {
initBackgroundBufferset(ctx.bgShader, bg);
}
renderer::initSpritesBufferset(ctx);
if (initParams.glInstallDrawer) {
@ -579,11 +562,6 @@ void setBgCbb(Context &ctx, uint_t bgIdx, uint_t cbbIdx) noexcept {
bg.cbbIdx = cbbIdx;
}
void setBgPriority(Context &ctx, uint_t bgIdx, uint_t priority) noexcept {
auto &bg = ctx.backgrounds[bgIdx];
bg.priority = static_cast<float>(priority & 0b11);
}
uint8_t bgStatus(Context &ctx) noexcept {
uint8_t out = 0;
for (uint_t i = 0; i < ctx.cbbs.size(); ++i) {
@ -608,11 +586,9 @@ void setBgStatus(Context &ctx, uint_t bg, bool status) noexcept {
void clearTileLayer(Context &ctx, uint_t bgIdx) noexcept {
auto &cbb = ctx.cbbs[static_cast<std::size_t>(bgIdx)];
initBackgroundBufferObjects(cbb, 0);
cbb.updated = true;
auto &bg = ctx.backgrounds[static_cast<std::size_t>(bgIdx)];
bg.priority = 0;
auto &bg = ctx.cbbs[static_cast<std::size_t>(bgIdx)];
initBackgroundBufferObjects(bg);
bg.updated = true;
}
void hideSprite(Context &ctx, uint_t idx) noexcept {
@ -647,19 +623,17 @@ void setTile(
const auto y = static_cast<uint_t>(row);
const auto x = static_cast<uint_t>(column);
const auto i = renderer::bgVertexRow(x, y);
auto &cbb = ctx.cbbs[z];
const auto vbo = &cbb.vertices[i * renderer::BgVertexVboLength];
const auto ebo = &cbb.elements[i * renderer::BgVertexEboLength];
auto &bg = ctx.backgrounds[bgIdx];
auto &bg = ctx.cbbs[z];
const auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
const auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
renderer::setTileBufferObject(
static_cast<uint_t>(i * renderer::BgVertexVboRows),
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(tile),
bg.priority,
vbo,
ebo);
cbb.updated = true;
bg.updated = true;
}
namespace gl {

View File

@ -19,7 +19,7 @@ constexpr uint64_t TileColumns = 128;
constexpr uint64_t TileCount = TileRows * TileColumns;
constexpr uint64_t SpriteCount = 128;
constexpr uint64_t BgVertexVboRows = 4;
constexpr uint64_t BgVertexVboRowLength = 6;
constexpr uint64_t BgVertexVboRowLength = 5;
constexpr uint64_t BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength;
constexpr uint64_t BgVertexEboLength = 6;
constexpr uint64_t SpriteVertexVboRows = 4;
@ -45,7 +45,6 @@ struct SpriteBlockset: public glutils::BufferSet {
};
struct Background {
float priority = 0;
bool enabled = false;
unsigned cbbIdx = 0;
};