mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 12:53:50 -10:00
Material::depthOffsetSlope / depthOffsetUnits (glPolygonOffset, toward the camera): with anti-aliasing a target sharing the frame's depth gets the depth resolved from one sample point off each pixel's centre, which on a steep surface can lie nearer than a decal's centre depth, so marks laid at the surface's own depth all failed (Dark Cloud's character shadows vanished at 4x). 0, 0 (the default) draws as before. native_render_targets: a decal on a steep slope through a shared-depth target lands with no anti-aliasing (with and without the offset) and, with the offset, at 4x and 8x. Without the offset at 4x every mark failed (checked, not kept as a case: which sample the resolve takes is the driver's). Co-Authored-By: Claude Opus 5.5 <[email protected]>
655 lines
26 KiB
C++
655 lines
26 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// GPU readback fixtures for native render-target operations. No game data.
|
|
#include "bozo/render/gl.h"
|
|
#include "bozo/render/renderer.h"
|
|
#include <SDL3/SDL.h>
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
using namespace bozo::render;
|
|
namespace
|
|
{
|
|
int failures = 0;
|
|
constexpr float identity[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
|
Screen screen{0, 0, 16, 16};
|
|
Renderer &renderer = Renderer::instance();
|
|
void quad(Material m, uint32_t colour, float y0 = 0, float y1 = 16, float z = 1)
|
|
{
|
|
Vertex v[6];
|
|
const float xy[6][2] = {{0, y0}, {16, y0}, {0, y1}, {16, y0}, {16, y1}, {0, y1}};
|
|
for (int i = 0; i < 6; ++i)
|
|
{
|
|
v[i].x = xy[i][0];
|
|
v[i].y = xy[i][1];
|
|
v[i].z = z;
|
|
v[i].u = xy[i][0] / 16;
|
|
v[i].v = xy[i][1] / 16;
|
|
v[i].rgba = colour;
|
|
}
|
|
renderer.draw(m, identity, v, 6);
|
|
}
|
|
void pixel(const char *name, int y, std::array<int, 3> expected)
|
|
{
|
|
uint8_t rgba[4]{};
|
|
gl::api().ReadPixels(16, y, 1, 1, gl::kRgba, gl::kUnsignedByte, rgba);
|
|
for (int c = 0; c < 3; ++c)
|
|
if (std::abs(int(rgba[c]) - expected[c]) > 3)
|
|
{
|
|
std::printf("FAIL %s: RGB %u,%u,%u expected %d,%d,%d\n", name, rgba[0], rgba[1], rgba[2], expected[0],
|
|
expected[1], expected[2]);
|
|
++failures;
|
|
break;
|
|
}
|
|
}
|
|
void present()
|
|
{
|
|
renderer.endFrame();
|
|
if (!renderer.present(0, 0, 32, 32))
|
|
{
|
|
++failures;
|
|
std::puts("FAIL present");
|
|
}
|
|
}
|
|
} // namespace
|
|
int main()
|
|
{
|
|
if (!SDL_Init(SDL_INIT_VIDEO))
|
|
{
|
|
std::puts(SDL_GetError());
|
|
return 1;
|
|
}
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
auto *window = SDL_CreateWindow("Bozo native render tests", 32, 32, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
|
|
if (!window)
|
|
{
|
|
std::puts(SDL_GetError());
|
|
return 1;
|
|
}
|
|
const auto context = SDL_GL_CreateContext(window);
|
|
if (!context || !gl::load([](const char *name) { return reinterpret_cast<void *>(SDL_GL_GetProcAddress(name)); }))
|
|
return 1;
|
|
gl::api().Viewport(0, 0, 32, 32);
|
|
renderer.setEnabled(true);
|
|
renderer.setPresentsAlone(true);
|
|
Material plain;
|
|
plain.textured = false;
|
|
plain.depthTest = false;
|
|
plain.depthWrite = false;
|
|
plain.blend = Material::Blend::Opaque;
|
|
Material copy = plain;
|
|
copy.fromFrame = 1;
|
|
copy.nearest = true;
|
|
renderer.setTargetSize(1, 8, 8);
|
|
renderer.beginFrame(screen);
|
|
auto target = plain;
|
|
target.target = 1;
|
|
quad(target, 0x80000080, 0, 8);
|
|
quad(target, 0x80800000, 8, 16);
|
|
copy.source = 1;
|
|
quad(copy, 0x80808080);
|
|
present();
|
|
pixel("auxiliary top stays top", 24, {255, 0, 0});
|
|
pixel("auxiliary bottom stays bottom", 8, {0, 0, 255});
|
|
renderer.beginFrame(screen);
|
|
copy.source = Material::kPreviousFrame;
|
|
quad(copy, 0x80808080);
|
|
present();
|
|
pixel("previous frame", 24, {255, 0, 0});
|
|
renderer.present(0, 0, 32, 32);
|
|
pixel("repeated presentation preserves history", 8, {0, 0, 255});
|
|
// A kept image (9..15): copied from the previous presentation once, sampled by later frames that do not draw
|
|
// into it. Its pixels stay, through new frames and a repeated presentation.
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80008000);
|
|
auto keep = copy;
|
|
keep.target = 12;
|
|
keep.source = Material::kPreviousFrame;
|
|
renderer.setTargetSize(12, 16, 16);
|
|
quad(keep, 0x80808080);
|
|
present();
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80000000);
|
|
auto kept = copy;
|
|
kept.source = 12;
|
|
quad(kept, 0x80808080);
|
|
present();
|
|
pixel("kept image top", 24, {255, 0, 0});
|
|
pixel("kept image bottom", 8, {0, 0, 255});
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80000000);
|
|
quad(kept, 0x80808080, 8, 16);
|
|
present();
|
|
renderer.present(0, 0, 32, 32);
|
|
pixel("kept image in a later frame", 8, {0, 0, 255});
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80008000);
|
|
copy.source = 0;
|
|
quad(copy, 0x80808080);
|
|
present();
|
|
pixel("copy current target", 16, {0, 255, 0});
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80000040);
|
|
auto alpha = plain;
|
|
alpha.blend = Material::Blend::AlphaOnly;
|
|
quad(alpha, 0x40000000);
|
|
auto add = plain;
|
|
add.blend = Material::Blend::AdditiveByDestination;
|
|
quad(add, 0x80008000);
|
|
present();
|
|
pixel("alpha-only then destination-alpha blend", 16, {128, 128, 0});
|
|
renderer.beginFrame(screen);
|
|
quad(plain, 0x80000040);
|
|
auto destination = plain;
|
|
destination.blend = Material::Blend::DestinationBySource;
|
|
quad(destination, 0x40004000);
|
|
present();
|
|
pixel("source plus destination times source alpha", 16, {64, 128, 0});
|
|
// The same asynchronous native depth cache used by game visibility queries. A real
|
|
// depth-writing primitive must be distinguishable from the next frame's empty depth.
|
|
Screen depthScreen = screen;
|
|
depthScreen.depthMax = 1000;
|
|
auto depthMaterial = plain;
|
|
depthMaterial.depthTest = depthMaterial.depthWrite = true;
|
|
renderer.beginFrame(depthScreen);
|
|
quad(depthMaterial, 0x80808080, 0, 16, 500);
|
|
renderer.wantDepth();
|
|
present();
|
|
auto expectDepth = [&](uint32_t expected)
|
|
{
|
|
uint32_t z = ~0u;
|
|
for (int attempt = 0; attempt < 100; ++attempt)
|
|
{
|
|
renderer.wantDepth();
|
|
const bool available = renderer.depthAt(8, 8, z);
|
|
if (available && std::abs(int(z) - int(expected)) <= 1)
|
|
return;
|
|
renderer.present(0, 0, 32, 32);
|
|
SDL_GL_SwapWindow(window);
|
|
SDL_Delay(2);
|
|
}
|
|
std::printf("FAIL native visibility depth: %u, expected %u\n", z, expected);
|
|
++failures;
|
|
};
|
|
expectDepth(500);
|
|
renderer.beginFrame(depthScreen);
|
|
quad(plain, 0x80808080);
|
|
present();
|
|
expectDepth(0);
|
|
// The GS's own blend, (A - B) * C + D, with FIX and on the frame so far.
|
|
const auto gsBlend = [](uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t fix = 0)
|
|
{
|
|
Material m = Material{};
|
|
m.textured = false;
|
|
m.depthTest = m.depthWrite = false;
|
|
m.gsBlend = true;
|
|
m.blendA = a, m.blendB = b, m.blendC = c, m.blendD = d, m.blendFix = fix;
|
|
return m;
|
|
};
|
|
Material flat;
|
|
flat.textured = false;
|
|
flat.blend = Material::Blend::Opaque;
|
|
flat.depthTest = flat.depthWrite = false;
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80404040); // 0x40 is half of 1.0: 128 on the target
|
|
quad(gsBlend(0, 2, 2, 1, 0x40), 0x80404040);
|
|
present();
|
|
pixel("GS blend Cs * fix + Cd", 8, {192, 192, 192});
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80808080);
|
|
quad(gsBlend(2, 0, 2, 1, 0x40), 0x80808080);
|
|
present();
|
|
pixel("GS blend Cd - Cs * fix", 8, {128, 128, 128});
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80808080);
|
|
quad(gsBlend(1, 2, 0, 2), 0x40FFFFFF);
|
|
present();
|
|
pixel("GS blend Cd * As", 8, {128, 128, 128});
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80404040);
|
|
quad(gsBlend(0, 1, 0, 1), 0x40808080);
|
|
present();
|
|
pixel("GS blend (Cs - Cd) * As + Cd", 8, {192, 192, 192});
|
|
|
|
// TEX0.TFX and TCC, on the frame so far as the texture: 128 on the target, Cf 0x40 (0.5), Af 0x20.
|
|
const auto sampled = [&flat](Material::TextureFunction function, bool textureAlpha = true)
|
|
{
|
|
Material m = flat;
|
|
m.textured = true;
|
|
m.fromFrame = 1u;
|
|
m.nearest = true;
|
|
m.textureFunction = function;
|
|
m.textureAlpha = textureAlpha;
|
|
return m;
|
|
};
|
|
const auto function = [&](const char *name, Material m, uint32_t colour, std::array<int, 3> expected)
|
|
{
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80404040);
|
|
quad(m, colour);
|
|
present();
|
|
pixel(name, 8, expected);
|
|
};
|
|
function("TFX modulate", sampled(Material::TextureFunction::Modulate), 0x20404040, {64, 64, 64});
|
|
function("TFX decal", sampled(Material::TextureFunction::Decal), 0x20404040, {128, 128, 128});
|
|
function("TFX highlight", sampled(Material::TextureFunction::Highlight), 0x20404040, {96, 96, 96});
|
|
function("TFX highlight2, per channel", sampled(Material::TextureFunction::Highlight2), 0x20802040, {96, 64, 160});
|
|
{
|
|
Material m = sampled(Material::TextureFunction::Highlight2, false);
|
|
m.blend = Material::Blend::Alpha;
|
|
m.gsBlend = true;
|
|
m.blendA = 0, m.blendB = 1, m.blendC = 0, m.blendD = 1;
|
|
function("TCC off: the vertex's alpha blends", m, 0x20808080, {128 + 8, 128 + 8, 128 + 8});
|
|
}
|
|
|
|
// FBMSK for the channels: only green written.
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80000000);
|
|
{
|
|
Material m = flat;
|
|
m.colourMask = 2u;
|
|
quad(m, 0x80808080);
|
|
}
|
|
present();
|
|
pixel("colour mask", 8, {0, 255, 0});
|
|
|
|
// TEST: a draw passes by alpha as its function says, and by GREATER depth.
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80000000);
|
|
{
|
|
Material m = flat;
|
|
m.alphaTest = true;
|
|
m.alphaFunc = Material::AlphaFunc::Less;
|
|
m.alphaRef = 0x40;
|
|
quad(m, 0x80808080);
|
|
quad(m, 0x20008000, 0, 8);
|
|
}
|
|
present();
|
|
pixel("alpha test LESS: the alpha above the reference fails", 8, {0, 0, 0});
|
|
pixel("alpha test LESS: the alpha below the reference passes", 24, {0, 255, 0});
|
|
|
|
Screen screenDepth = screen;
|
|
screenDepth.depthMax = 1000.0f;
|
|
renderer.beginFrame(screenDepth);
|
|
{
|
|
Material m = flat;
|
|
m.depthTest = m.depthWrite = true;
|
|
quad(m, 0x80000080, 0, 16, 500.0f);
|
|
m.depthGreater = true;
|
|
quad(m, 0x80008000, 0, 16, 500.0f);
|
|
}
|
|
present();
|
|
pixel("depth GREATER: an equal Z fails", 8, {255, 0, 0});
|
|
|
|
// The stencil: a volume counted up and down marks where the counts differ; a draw then lands only there.
|
|
renderer.beginFrame(screen);
|
|
quad(flat, 0x80000000);
|
|
{
|
|
Material volume = flat;
|
|
volume.colourMask = 0u;
|
|
volume.stencilClear = true;
|
|
volume.stencilOp = Material::StencilOp::IncrementWrap;
|
|
quad(volume, 0x80808080);
|
|
volume.stencilClear = false;
|
|
volume.stencilOp = Material::StencilOp::DecrementWrap;
|
|
quad(volume, 0x80808080, 0, 8);
|
|
Material marked = flat;
|
|
marked.stencilNotZero = true;
|
|
quad(marked, 0x80008000);
|
|
}
|
|
present();
|
|
pixel("stencil 0: not drawn", 24, {0, 0, 0});
|
|
pixel("stencil not 0: drawn", 8, {0, 255, 0});
|
|
|
|
// Q: a screen-space triangle's texture coordinates in perspective, its colour across the screen.
|
|
{
|
|
auto checker = std::make_shared<Texture>();
|
|
checker->width = 2;
|
|
checker->height = 1;
|
|
checker->rgba = {0x800000FFu, 0x80FF0000u};
|
|
checker->key = 0x5151u;
|
|
Material m;
|
|
m.depthTest = m.depthWrite = false;
|
|
m.blend = Material::Blend::Opaque;
|
|
m.useQ = true;
|
|
m.nearest = true;
|
|
m.clampU = m.clampV = true;
|
|
renderer.beginFrame(screen);
|
|
Vertex v[3];
|
|
v[0].x = 0, v[0].y = 0, v[0].u = 0, v[0].q = 0.125f;
|
|
v[1].x = 16, v[1].y = 0, v[1].u = 1, v[1].q = 1.0f;
|
|
v[2].x = 0, v[2].y = 16, v[2].u = 0, v[2].q = 0.125f;
|
|
for (Vertex &vertex : v)
|
|
vertex.rgba = 0x80808080;
|
|
// With Q 1/8 at the left and 1 at the right, u = 0.5 is a ninth of the way along the top edge (3.5 window pixels):
|
|
// an affine interpolation would have it in the middle.
|
|
renderer.draw(m, identity, v, 3, checker);
|
|
present();
|
|
}
|
|
{
|
|
uint8_t rgba[4] = {};
|
|
const int left = 1, right = 8, y = 31;
|
|
gl::api().ReadPixels(left, y, 1, 1, gl::kRgba, gl::kUnsignedByte, rgba);
|
|
if (rgba[0] < 200)
|
|
{
|
|
std::printf("FAIL Q left of the middle: red %u\n", rgba[0]);
|
|
++failures;
|
|
}
|
|
gl::api().ReadPixels(right, y, 1, 1, gl::kRgba, gl::kUnsignedByte, rgba);
|
|
if (rgba[2] < 200)
|
|
{
|
|
std::printf("FAIL Q right of the middle: blue %u\n", rgba[2]);
|
|
++failures;
|
|
}
|
|
}
|
|
|
|
// Mip levels: the texture's own (level 0 red, 1 green, 2 blue), or made from level 0 (half red, half blue: 2 is purple).
|
|
{
|
|
auto own = std::make_shared<Texture>();
|
|
own->width = own->height = 4;
|
|
own->rgba.assign(16, 0x800000FFu);
|
|
own->levels = {std::vector<uint32_t>(4, 0x8000FF00u), std::vector<uint32_t>(1, 0x80FF0000u)};
|
|
own->key = 0x3170u;
|
|
auto made = std::make_shared<Texture>();
|
|
made->width = made->height = 4;
|
|
for (int i = 0; i < 16; ++i)
|
|
made->rgba.push_back((i & 3) < 2 ? 0x800000FFu : 0x80FF0000u);
|
|
made->key = 0x3171u;
|
|
Material m;
|
|
m.depthTest = m.depthWrite = false;
|
|
m.blend = Material::Blend::Opaque;
|
|
const auto mipQuad = [&](const Material &material, const std::shared_ptr<const Texture> &texture, float q = 1.0f)
|
|
{
|
|
Vertex v[6];
|
|
const float xy[6][2] = {{0, 0}, {16, 0}, {0, 16}, {16, 0}, {16, 16}, {0, 16}};
|
|
for (int i = 0; i < 6; ++i)
|
|
{
|
|
v[i].x = xy[i][0];
|
|
v[i].y = xy[i][1];
|
|
v[i].z = 1;
|
|
v[i].u = xy[i][0] / 16;
|
|
v[i].v = xy[i][1] / 16;
|
|
v[i].q = q;
|
|
v[i].rgba = 0x80808080;
|
|
}
|
|
renderer.beginFrame(screen);
|
|
renderer.draw(material, identity, v, 6, texture);
|
|
present();
|
|
};
|
|
mipQuad(m, own);
|
|
pixel("mipmap off: level 0", 16, {255, 0, 0});
|
|
Material fixed = m;
|
|
fixed.mipmap.levels = 2;
|
|
fixed.mipmap.linearTexel = false;
|
|
fixed.mipmap.lod = Material::Mipmap::Lod::Fixed;
|
|
fixed.mipmap.bias = 1.0f;
|
|
mipQuad(fixed, own);
|
|
pixel("fixed LOD 1: the texture's level 1", 16, {0, 255, 0});
|
|
fixed.mipmap.bias = 2.0f;
|
|
mipQuad(fixed, own);
|
|
pixel("fixed LOD 2: the texture's level 2", 16, {0, 0, 255});
|
|
fixed.mipmap.levels = 1;
|
|
mipQuad(fixed, own);
|
|
pixel("LOD past MXL: the last level allowed", 16, {0, 255, 0});
|
|
Material fromQ = m;
|
|
fromQ.useQ = true;
|
|
fromQ.mipmap = mipmapOf((1ull << 2 | 1ull << 3) | (2ull << 6) | (uint64_t(0x1000 - 16) << 32));
|
|
mipQuad(fromQ, own, 0.25f);
|
|
pixel("Q 1/4, K -1: LOD 1", 16, {0, 255, 0});
|
|
mipQuad(fromQ, own, 1.0f);
|
|
pixel("Q 1, K -1: level 0", 16, {255, 0, 0});
|
|
Material screenLod = m;
|
|
screenLod.mipmap.levels = 2;
|
|
screenLod.mipmap.bias = 8.0f;
|
|
mipQuad(screenLod, own);
|
|
pixel("screen LOD with a large bias: the coarsest level", 16, {0, 0, 255});
|
|
fixed.mipmap.levels = 2;
|
|
fixed.mipmap.bias = 2.0f;
|
|
mipQuad(fixed, made);
|
|
pixel("levels made at upload: level 2 the average", 16, {128, 0, 128});
|
|
}
|
|
|
|
// A native depth mask writes independently of testing. The far half must
|
|
// overwrite a previous near clear; the tested draw must then keep its usual
|
|
// comparison. Cover the main and auxiliary targets and an inverted reuse.
|
|
auto writeOnly = plain;
|
|
writeOnly.depthWrite = true;
|
|
for (uint8_t targetId : {uint8_t(0), uint8_t(2)})
|
|
for (int inverted = 0; inverted < 2; ++inverted)
|
|
{
|
|
renderer.beginFrame(depthScreen);
|
|
auto clearMask = writeOnly;
|
|
clearMask.target = targetId;
|
|
quad(clearMask, 0x80000080, 0, 16, 1000);
|
|
quad(clearMask, 0x80800000, inverted ? 8 : 0, inverted ? 16 : 8, 0);
|
|
auto tested = depthMaterial;
|
|
tested.target = targetId;
|
|
quad(tested, 0x80008000, 0, 16, 0);
|
|
if (targetId)
|
|
{
|
|
copy.source = targetId;
|
|
quad(copy, 0x80808080);
|
|
}
|
|
present();
|
|
pixel("unconditional far write opens only the mask", inverted ? 8 : 24, {0,255,0});
|
|
pixel("tested draw preserves the near clear outside mask", inverted ? 24 : 8, {255,0,0});
|
|
}
|
|
// An ordered native full-frame colour correction, sampling the current
|
|
// frame. Different channels cover high clamp, low clamp and saturation.
|
|
renderer.beginFrame(screen);
|
|
quad(plain,0x80604020);
|
|
auto levels=copy;levels.source=0;levels.levels.enabled=true;
|
|
levels.levels.gain[0]=2;levels.levels.gain[1]=3;levels.levels.gain[2]=4;
|
|
levels.levels.high[0]=0.2f;levels.levels.high[1]=1;levels.levels.high[2]=1;
|
|
levels.levels.low[0]=0.05f;levels.levels.low[1]=0.6f;levels.levels.low[2]=0.25f;
|
|
quad(levels,0x80808080);present();
|
|
pixel("native levels clamp subtract gain",16,{77,0,255});
|
|
// Disabled next batch must not inherit uniforms from the previous pass.
|
|
renderer.beginFrame(screen);quad(plain,0x80004000);present();
|
|
pixel("colour levels do not leak",16,{0,128,0});
|
|
// Auxiliary alpha must survive RGB correction. A following destination-
|
|
// alpha blend observes it independently from the corrected colour.
|
|
renderer.beginFrame(screen);auto aux=plain;aux.target=1;
|
|
quad(aux,0x80000040);auto alphaLevels=levels;alphaLevels.target=1;alphaLevels.source=1;
|
|
auto alphaWrite=plain;alphaWrite.target=1;alphaWrite.blend=Material::Blend::AlphaOnly;
|
|
quad(alphaWrite,0x40000000);
|
|
alphaLevels.blend=Material::Blend::AlphaOnly;quad(alphaLevels,0x80808080);
|
|
auto fromAux=copy;fromAux.source=1;fromAux.blend=Material::Blend::AdditiveByDestination;
|
|
quad(plain,0x80000000);auto mainAlpha=alphaWrite;mainAlpha.target=0;
|
|
quad(mainAlpha,0x40000000);quad(fromAux,0x80808080);present();
|
|
pixel("levels preserve auxiliary alpha",16,{64,0,0});
|
|
// Colour is interpolated in screen space, independently of reciprocal
|
|
// depth. ST texture coordinates still need perspective correction.
|
|
auto gradient = [&](float rightW, std::shared_ptr<const Texture> texture = {}) {
|
|
float project[16];
|
|
std::copy(identity, identity + 16, project);
|
|
project[11] = 1; // homogeneous w = object z + 1
|
|
project[10] = 0;
|
|
Vertex vertices[6];
|
|
const float xy[6][2] = {{0,0},{16,0},{0,16},{16,0},{16,16},{0,16}};
|
|
for (int i = 0; i < 6; ++i) {
|
|
const bool right = xy[i][0] != 0;
|
|
const float w = right ? rightW : 1;
|
|
auto &v = vertices[i];
|
|
v.x = xy[i][0] * w; v.y = xy[i][1] * w; v.z = w - 1;
|
|
v.u = right ? 1 : 0; v.v = 0;
|
|
v.rgba = texture ? 0x80808080 : right ? 0x80800000 : 0x80000080;
|
|
}
|
|
auto material = plain;
|
|
material.textured = bool(texture); material.nearest = true;
|
|
renderer.beginFrame(screen);
|
|
renderer.draw(material, project, vertices, 6, texture);
|
|
present();
|
|
};
|
|
gradient(1);
|
|
pixel("colour gradient with uniform w",16,{124,0,131});
|
|
gradient(4);
|
|
pixel("colour gradient ignores varying w",16,{124,0,131});
|
|
auto twoTexels = std::make_shared<Texture>();
|
|
twoTexels->width = 2; twoTexels->height = 1;
|
|
twoTexels->rgba = {0x80000080,0x80008000};twoTexels->key = 0x534b59;twoTexels->version = 1;
|
|
gradient(4,twoTexels);
|
|
pixel("texture coordinates retain perspective correction",16,{128,0,0});
|
|
// Texture upscaling: an asset drawn from its scaled copy once it is made, anything else as it is; the game's levels k
|
|
// levels down.
|
|
{
|
|
auto diagonal = std::make_shared<Texture>();
|
|
diagonal->width = diagonal->height = 4;
|
|
for (int y = 0; y < 4; ++y)
|
|
for (int x = 0; x < 4; ++x)
|
|
diagonal->rgba.push_back(x > y ? 0x80FF0000u : 0x800000FFu);
|
|
diagonal->asset = true;
|
|
diagonal->key = 0x5ca1e;
|
|
auto notAsset = std::make_shared<Texture>(*diagonal);
|
|
notAsset->asset = false;
|
|
notAsset->key = 0x5ca1f;
|
|
Material m;
|
|
m.depthTest = m.depthWrite = false;
|
|
m.blend = Material::Blend::Opaque;
|
|
m.nearest = true;
|
|
m.clampU = m.clampV = true;
|
|
const TextureUpscale twice{2, UpscaleFilter::Edge};
|
|
const Texture expected = upscaleTexture(*diagonal, twice, false, false);
|
|
const auto texelQuad = [&](const Material &material, const std::shared_ptr<const Texture> &texture)
|
|
{
|
|
Vertex v[6];
|
|
const float xy[6][2] = {{0, 0}, {16, 0}, {0, 16}, {16, 0}, {16, 16}, {0, 16}};
|
|
for (int i = 0; i < 6; ++i)
|
|
{
|
|
v[i].x = xy[i][0];
|
|
v[i].y = xy[i][1];
|
|
v[i].z = 1;
|
|
v[i].u = xy[i][0] / 16;
|
|
v[i].v = xy[i][1] / 16;
|
|
v[i].rgba = 0x80808080;
|
|
}
|
|
renderer.beginFrame(screen);
|
|
renderer.draw(material, identity, v, 6, texture);
|
|
present();
|
|
};
|
|
const auto texels = [&](const char *name, const std::vector<uint32_t> &want, int side)
|
|
{
|
|
const int step = 32 / side;
|
|
for (int ty = 0; ty < side; ++ty)
|
|
for (int tx = 0; tx < side; ++tx)
|
|
{
|
|
uint8_t rgba[4]{};
|
|
gl::api().ReadPixels(tx * step + step / 2, 31 - (ty * step + step / 2), 1, 1, gl::kRgba, gl::kUnsignedByte, rgba);
|
|
const uint32_t c = want[static_cast<size_t>(ty) * side + tx];
|
|
for (int k = 0; k < 3; ++k)
|
|
if (std::abs(int(rgba[k]) - int((c >> (8 * k)) & 0xffu)) > 3)
|
|
{
|
|
std::printf("FAIL %s: texel %d,%d RGB %u,%u,%u\n", name, tx, ty, rgba[0], rgba[1], rgba[2]);
|
|
++failures;
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
texelQuad(m, diagonal);
|
|
texels("upscaling off: the texture as it is", diagonal->rgba, 4);
|
|
renderer.setTextureUpscale(twice);
|
|
texelQuad(m, diagonal);
|
|
texels("upscaling on, the copy not made yet: the texture as it is", diagonal->rgba, 4);
|
|
TextureUpscaler::instance().waitIdle();
|
|
texelQuad(m, diagonal);
|
|
texels("upscaling on: the scaled copy", expected.rgba, 8);
|
|
texelQuad(m, notAsset);
|
|
texels("not an asset: never scaled", notAsset->rgba, 4);
|
|
|
|
auto own = std::make_shared<Texture>();
|
|
own->width = own->height = 4;
|
|
own->rgba.assign(16, 0x800000FFu);
|
|
own->levels = {std::vector<uint32_t>(4, 0x8000FF00u), std::vector<uint32_t>(1, 0x80FF0000u)};
|
|
own->key = 0x3172u;
|
|
own->asset = true;
|
|
auto made = std::make_shared<Texture>();
|
|
made->width = made->height = 4;
|
|
for (int i = 0; i < 16; ++i)
|
|
made->rgba.push_back((i & 3) < 2 ? 0x800000FFu : 0x80FF0000u);
|
|
made->key = 0x3173u;
|
|
made->asset = true;
|
|
Material fixed;
|
|
fixed.depthTest = fixed.depthWrite = false;
|
|
fixed.blend = Material::Blend::Opaque;
|
|
fixed.mipmap.levels = 2;
|
|
fixed.mipmap.linearTexel = false;
|
|
fixed.mipmap.lod = Material::Mipmap::Lod::Fixed;
|
|
for (const uint8_t factor : {uint8_t(2), uint8_t(4)})
|
|
{
|
|
renderer.setTextureUpscale(TextureUpscale{factor, UpscaleFilter::Edge});
|
|
fixed.mipmap.bias = 1.0f;
|
|
texelQuad(fixed, own);
|
|
TextureUpscaler::instance().waitIdle();
|
|
texelQuad(fixed, own);
|
|
pixel("upscaled, fixed LOD 1: the texture's level 1", 16, {0, 255, 0});
|
|
fixed.mipmap.bias = 2.0f;
|
|
texelQuad(fixed, own);
|
|
pixel("upscaled, fixed LOD 2: the texture's level 2", 16, {0, 0, 255});
|
|
fixed.mipmap.levels = 1;
|
|
texelQuad(fixed, own);
|
|
pixel("upscaled, LOD past MXL: the last level allowed", 16, {0, 255, 0});
|
|
fixed.mipmap.levels = 2;
|
|
texelQuad(fixed, made);
|
|
TextureUpscaler::instance().waitIdle();
|
|
texelQuad(fixed, made);
|
|
pixel("upscaled, levels made at upload from the texture's level 0: level 2 the average", 16, {128, 0, 128});
|
|
}
|
|
renderer.setTextureUpscale(TextureUpscale{});
|
|
}
|
|
{
|
|
// A decal (a port's shadow marks) laid on a steep surface through a target that shares the frame's depth, then
|
|
// laid back over the frame. Without anti-aliasing the marks land at the surface's own depth (LEQUAL passes).
|
|
// With anti-aliasing the shared depth is resolved from one sample point off each pixel's centre, which on a
|
|
// steep surface can be nearer than the mark's centre depth: Material::depthOffset* moves the marks toward the
|
|
// camera by that much, and they land again.
|
|
auto slope = [&](Material m, uint32_t colour) {
|
|
Vertex v[6];
|
|
const float xy[6][2] = {{0, 0}, {16, 0}, {0, 16}, {16, 0}, {16, 16}, {0, 16}};
|
|
for (int i = 0; i < 6; ++i)
|
|
{
|
|
v[i].x = xy[i][0];
|
|
v[i].y = xy[i][1];
|
|
v[i].z = screen.depthMax * (0.2f + 0.6f * xy[i][1] / 16.0f); // steep: 0.6 of the range over 16 rows
|
|
v[i].u = v[i].v = 0;
|
|
v[i].rgba = colour;
|
|
}
|
|
renderer.draw(m, identity, v, 6);
|
|
};
|
|
auto decal = [&](int samples, float offset, const char *name) {
|
|
renderer.setAntiAliasing(samples);
|
|
renderer.setTargetSharesDepth(2, true);
|
|
renderer.beginFrame(screen);
|
|
auto ground = plain;
|
|
ground.depthTest = ground.depthWrite = true;
|
|
slope(ground, 0x80202020);
|
|
auto mark = plain;
|
|
mark.target = 2;
|
|
mark.depthTest = true;
|
|
mark.depthWrite = false;
|
|
mark.depthOffsetSlope = mark.depthOffsetUnits = offset;
|
|
slope(mark, 0x80808080);
|
|
auto lay = copy;
|
|
lay.source = 2;
|
|
quad(lay, 0x80808080);
|
|
present();
|
|
for (int y : {6, 16, 26})
|
|
pixel(name, y, {255, 255, 255});
|
|
renderer.setTargetSharesDepth(2, false);
|
|
renderer.setAntiAliasing(0);
|
|
};
|
|
decal(0, 0.0f, "decal at the surface's depth, no anti-aliasing");
|
|
decal(0, 1.0f, "decal moved toward the camera, no anti-aliasing");
|
|
decal(4, 1.0f, "decal moved toward the camera, 4x anti-aliasing");
|
|
decal(8, 1.0f, "decal moved toward the camera, 8x anti-aliasing");
|
|
}
|
|
renderer.releaseGpu();
|
|
SDL_GL_DestroyContext(context);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
std::printf("native render targets: %s\n", failures ? "FAILED" : "passed");
|
|
return failures ? 1 : 0;
|
|
}
|