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