Files
EVEngine/test/AntiAliasing.cpp
T
西风逍遥游 34d1fdb63b refactor(modules): clear remaining back-edges and split the largest TUs (#127)
* chore(ci): add clang-format gate, CODEOWNERS and agent collaboration rules

* refactor(modules): clear remaining back-edges and split the largest TUs

Three known dependency back-edges are gone (module_depgraph.py KNOWN_BACK_EDGES
is now empty; --check passes with 0 violations, 0 cycles):

- window -> graphics: window queries the new IWindowSurfaceHost capability
  (common/WindowSurfaceHost.h) instead of holding a Graphics pointer;
  Window::setGraphics was removed and all call sites updated.
- window -> image: Window::setIcon was replaced by setIconRGBA(const uint8_t*,
  w, h); the window no longer decodes ImageData.
- scene -> graphics: pickScreenAt / collectFrustumIdsAt implementations moved
  to graphics/ScenePicking.cpp (graphics -> scene is a legal downward edge);
  scene now depends only on spatial.

TU splits (pure moves, verified identical method sets and brace balance):

- vulkan/Graphics.cpp (5118 lines) -> Graphics.cpp (lifecycle), GraphicsPipeline,
  Graphics2D, Graphics3D, GraphicsMesh + shared GraphicsInternal.h.
- scene/Scene.cpp -> Scene.cpp (script layer / host lifecycle / serialization)
  + SceneNodes.cpp + shared SceneInternal.h.

Verified with a real build (MSVC 19.51, prebuilt third-party): full build green;
window.* and Scene.* suites pass, including scene picking/culling and GPU cases.

* refactor(graphics): narrow Graphics interfaces and clear public-header leaks

Step 4 — interface segregation:
- New lean interfaces: IGraphics2D, IGraphics3D, IResourceFactory, IPostFX
  (pure virtuals, forward-declared types only; graphics/Color.h extracted from
  Canvas.h so modules that only need the color type stop pulling Canvas.h).
- Graphics now implements all four facets; consumers can depend on the narrow
  interface they use instead of the 129-method god class.
- Migrated voxel (VoxelWorld::drawVisible -> IGraphics3D*) and model3d
  (ModelRenderer::buildRenderable(s) -> IResourceFactory&).
- Graphics.h slims from 24 sub-header includes to a handful of small type
  headers + forward declarations; the destructor moved out-of-line so
  unique_ptr effect members are destroyed where types are complete.

Step 5 — header leaks (module_depgraph.py '*' marks: 14 -> 7):
- map/TileLayer.h, particles/ParticleEmitter.h, avatar/AvatarInstance.h,
  animation/SpineAnim.h, map/TileSystem.h, model3d/ModelData.h,
  model3d/ModelRenderer.h, audio/Audio.h + Source.h, image/ImageData.h +
  Image.h, dialogue/Dialogue.h, procgen Palette/OutputSpec/TextureRecipe/
  PbrMaterial -> forward declarations, includes moved to .cpp.
- voxel/VoxelWorld.h: terrain sampler Pimpl'd (unique_ptr + out-of-line
  destructor), removing the procgen header dependency.
- gpgpu/vulkan backend headers stop pulling graphics/vulkan/Graphics.h.
- Remaining '*' marks are deliberate: tiny POD/enum headers (GridConfig,
  MapObject, BlendMode, Color), an internal backend header
  (GraphicsInternal.h -> filesystem), and lean interface references.

Verified with a full clean build (MSVC) and tests: window/Scene/voxel/
RenderSystem/Quad/shader/TextureSampler/resourceLifetime pass; broad suite
only shows the pre-existing PHYSFS-init filesystem-environment failures.

* feat(resources): revive ResourceManager as unified IAssetReloader-backed cache

* fix(resources): leak ResourceManager singleton to avoid exit-time teardown crashes

Cached CPU resources may own third-party handles (FreeType faces, Assimp
scenes, image decode handlers) whose libraries are torn down at process
exit in an unspecified TU order; destroying cached entries from the
singleton destructor can segfault at exit (seen on CI Windows in
model3d.materialApi.objMtl). Keep the singleton alive until OS reclaim;
explicit unload()/clear() still release entries during the run.

* refactor(devtools): consume business modules through capability interfaces

DevTools / cmdline no longer include business-module headers, so trimming
scene / physics / procgen / particles / audio / ui / graphics no longer
requires disabling devtools, and devtools can be switched off with
-DEVENGINE_MODULE_DEVTOOLS=OFF.

New capability interfaces in common/:
- ISceneQuery (scene) - host/node inspection + mutation + transform sync
- IRenderCapture (graphics) - render status, PNG capture, camera control,
  visible-entity snapshot, entity-ID mask, G-buffer PNGs
- IPhysicsQuery (physics) - 2D world lifecycle + ray casts
- IProcgenQuery (procgen) - recipe listing, map/mesh generation
- IParticlesQuery (particles) - emitter status + one-shot emit
- IAudioQuery (audio) - volume / stop-all
- IEditorHost (ui) - declarative editor host (used by cmdline too)

Providers register in their module constructors. DevTools rewires McpServer
(render/scene/procgen/physics/particles/audio/host tools), SceneInspect
(now delegates to ISceneQuery + IRenderCapture) and RenderVision (uses
IRenderCapture::capturePngDataUrl); cmdline/Mcp.cpp uses IEditorHost.

manifest: devtools DEPS graphics scene physics procgen particles audio ui
image event filesystem -> event; cmdline DEPS filesystem ui -> filesystem.

Verified: full build green; devtools.mcp.* tests pass; broad suite shows
only the pre-existing PHYSFS-init filesystem-environment failures.

* style: apply clang-format to changed lines after dev merge

* fix(webgpu): include RenderControl/TextureSampler explicitly after Graphics.h slimming

* fix(webgpu): provide newFont/print stubs to satisfy IGraphics2D/IResourceFactory vtables

* fix(common): make ProcgenQuery.h self-sufficient (cstdint/utility for GCC)
2026-08-20 05:53:33 +08:00

278 lines
8.4 KiB
C++

#include "zeroerr/assert.h"
#include "zeroerr/unittest.h"
#include <SDL2/SDL.h>
#include <cmath>
#include <memory>
#include <string>
#include <vector>
#include "graphics/AmbientOcclusion.h"
#include "graphics/AntiAliasing.h"
#include "graphics/Canvas.h"
#include "graphics/DrawItem2D.h"
#include "graphics/Font.h"
#include "graphics/GBuffer.h"
#include "graphics/GlobalIllumination.h"
#include "graphics/Graphics.h"
#include "graphics/Grass.h"
#include "graphics/Light.h"
#include "graphics/Material.h"
#include "graphics/Mesh.h"
#include "graphics/Outline.h"
#include "graphics/Quad.h"
#include "graphics/RenderControl.h"
#include "graphics/ScreenSpaceReflection.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Volumetric.h"
#include "graphics/Water.h"
#include "graphics/Waterfall.h"
#include "window/Window.h"
// Color lives in eve::graphics (see graphics/Canvas.h); keep the unqualified form.
using eve::graphics::Color;
using namespace eve::graphics;
namespace {
void openGfxWindow(eve::window::Window *&win, Graphics *&gfx, int w = 320, int h = 240) {
win = eve::window::Window::create();
gfx = Graphics::create();
REQUIRE(win != nullptr);
REQUIRE(gfx != nullptr);
eve::window::WindowSettings s;
s.width = w;
s.height = h;
s.centered = true;
REQUIRE(win->setWindowSettings(s));
}
Texture *makeChecker(Graphics *gfx, int w, int h) {
std::vector<uint8_t> px(size_t(w * h * 4));
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
const bool on = ((x / 4) + (y / 4)) & 1;
const size_t i = size_t((y * w + x) * 4);
px[i + 0] = on ? 255 : 16;
px[i + 1] = on ? 255 : 16;
px[i + 2] = on ? 255 : 16;
px[i + 3] = 255;
}
}
return gfx->newTexture(w, h, px.data());
}
/** Hold a textured canvas / texture on the swapchain briefly. */
void previewTexture(Graphics *gfx, Texture *tex, int ms = 400) {
gfx->setBackgroundColorRGBA(0.05f, 0.05f, 0.08f, 1.f);
const int frames = (ms >= 16) ? (ms / 16) : 1;
for (int i = 0; i < frames; ++i) {
gfx->clearScreen();
gfx->drawTexturedRectRGBA(tex, 0, 0, float(gfx->getWidth()), float(gfx->getHeight()), 1, 1, 1,
1);
gfx->present();
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) return;
}
SDL_Delay(16);
}
}
} // namespace
TEST_CASE("aa.qualityAndModes") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx);
AntiAliasing *raw = gfx->newAntiAliasing();
REQUIRE(raw != nullptr);
REQUIRE(raw->getFxaaShader() != nullptr);
REQUIRE(raw->getSmaaShader() != nullptr);
REQUIRE(raw->getSsaaShader() != nullptr);
REQUIRE(raw->getNfaaShader() != nullptr);
std::unique_ptr<AntiAliasing> aa(raw);
CHECK(aa->getMode() == "fxaa");
CHECK(aa->getQuality() == "medium");
CHECK(aa->getShader() == aa->getFxaaShader());
aa->setMode("smaa");
CHECK(aa->getMode() == "smaa");
CHECK(aa->getShader() == aa->getSmaaShader());
CHECK(aa->hasParam("threshold"));
CHECK(aa->hasParam("maxSearch"));
aa->setMode("nfaa");
CHECK(aa->getShader() == aa->getNfaaShader());
CHECK(aa->hasParam("strength"));
aa->setMode("ssaa");
CHECK(aa->getShader() == aa->getSsaaShader());
CHECK(aa->suggestScale() == 2.f);
CHECK(aa->resolutionFor(100) == 200);
aa->setQuality("high");
CHECK(aa->getQuality() == "high");
CHECK(aa->suggestScale() == 4.f);
CHECK(aa->resolutionFor(100) == 400);
CHECK(aa->getFloat("scale") == 4.f);
aa->setQuality("low");
CHECK(aa->suggestScale() == 2.f);
CHECK(aa->getFloat("kernel") == 0.f);
aa->setMode("unknown");
CHECK(aa->getMode() == "fxaa");
aa->setQuality("bogus");
CHECK(aa->getQuality() == "medium");
}
TEST_CASE("aa.fxaaApplyToCanvas") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 256, 192);
std::unique_ptr<AntiAliasing> aa(gfx->newAntiAliasing());
aa->setMode("fxaa");
aa->setQuality("medium");
Texture *src = makeChecker(gfx, 128, 96);
REQUIRE(src != nullptr);
Canvas *dest = gfx->newCanvas(128, 96);
REQUIRE(dest != nullptr);
REQUIRE(dest->getTexture() != nullptr);
gfx->setBackgroundColorRGBA(0, 0, 0, 1);
aa->applyTo(gfx, src, dest);
previewTexture(gfx, dest->getTexture(), 300);
// Flat region should stay near black/white extremes after AA (no crash / blackout).
gfx->setCanvas(dest);
// Just ensure we can draw the result to screen without throwing.
gfx->setCanvas(nullptr);
gfx->clearScreen();
gfx->drawTexturedRectRGBA(dest->getTexture(), 0, 0, float(gfx->getWidth()),
float(gfx->getHeight()), 1, 1, 1, 1);
gfx->present();
}
TEST_CASE("aa.allModesSmoke") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 320, 240);
std::unique_ptr<AntiAliasing> aa(gfx->newAntiAliasing());
Texture *src = makeChecker(gfx, 160, 120);
Canvas *dest = gfx->newCanvas(160, 120);
REQUIRE(src != nullptr);
REQUIRE(dest != nullptr);
const char *modes[] = {"fxaa", "smaa", "ssaa", "nfaa"};
for (const char *mode : modes) {
aa->setMode(mode);
aa->setQuality("medium");
aa->applyTo(gfx, src, dest);
CHECK(aa->getMode() == mode);
previewTexture(gfx, dest->getTexture(), 200);
}
}
TEST_CASE("aa.ssaaFromHiResCanvas") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 256, 192);
std::unique_ptr<AntiAliasing> aa(gfx->newAntiAliasing());
aa->setMode("ssaa");
aa->setQuality("medium");
const int dw = 128;
const int dh = 96;
const int sw = aa->resolutionFor(dw);
const int sh = aa->resolutionFor(dh);
CHECK(sw == 256);
CHECK(sh == 192);
Canvas *hi = gfx->newCanvas(sw, sh);
Canvas *lo = gfx->newCanvas(dw, dh);
REQUIRE(hi != nullptr);
REQUIRE(lo != nullptr);
// Draw a high-contrast diagonal into the supersampled canvas.
gfx->setCanvas(hi);
gfx->setBackgroundColorRGBA(0.05f, 0.05f, 0.08f, 1.f);
gfx->clearScreen();
Texture *white = nullptr;
{
const uint8_t px[4] = {240, 240, 240, 255};
white = gfx->newTexture(1, 1, px);
}
// Staircase of thin rectangles approximating a diagonal.
for (int i = 0; i < 32; ++i) {
const float x = float(i * (sw / 32));
const float y = float(i * (sh / 32));
gfx->drawTexturedRectRGBA(white, x, y, 6.f, 6.f, 1, 1, 1, 1);
}
gfx->setCanvas(nullptr);
aa->applyCanvasTo(gfx, hi, lo);
previewTexture(gfx, lo->getTexture(), 400);
}
TEST_CASE("aa.paramOverrides") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx);
std::unique_ptr<AntiAliasing> aa(gfx->newAntiAliasing());
aa->setMode("fxaa");
aa->setFloat("subpix", 0.25f);
CHECK(std::fabs(aa->getFloat("subpix") - 0.25f) < 1e-5f);
aa->setMode("smaa");
aa->setFloat("threshold", 0.2f);
CHECK(std::fabs(aa->getFloat("threshold") - 0.2f) < 1e-5f);
aa->setMode("nfaa");
aa->setFloat("strength", 1.5f);
CHECK(std::fabs(aa->getFloat("strength") - 1.5f) < 1e-5f);
}
TEST_CASE("aa.msaaSamplesApi") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 256, 192);
// Defaults: hardware MSAA on at 4 samples via the render control feature.
RenderControl *rc = gfx->getRenderControl();
REQUIRE(rc != nullptr);
CHECK(rc->isEnabled("msaa"));
CHECK(gfx->getMsaaSamples() == 4);
gfx->setMsaaSamples(8);
CHECK(gfx->getMsaaSamples() == 8);
gfx->setMsaaSamples(2);
CHECK(gfx->getMsaaSamples() == 2);
gfx->setMsaaSamples(0);
CHECK(gfx->getMsaaSamples() == 0);
gfx->setMsaaSamples(1); // below the MSAA threshold → treated as off
CHECK(gfx->getMsaaSamples() == 0);
gfx->setMsaaSamples(-2);
CHECK(gfx->getMsaaSamples() == 0);
gfx->setMsaaSamples(4);
CHECK(gfx->getMsaaSamples() == 4);
// 3D smoke frame with MSAA enabled: draw a mesh + resolve, no crash/blackout.
gfx->setBackgroundColorRGBA(0.05f, 0.05f, 0.08f, 1.f);
Mesh *mesh = gfx->newMeshSphere(24, 16);
REQUIRE(mesh != nullptr);
gfx->begin3DFrame();
CHECK(gfx->had3DThisFrame());
gfx->drawMesh(mesh, glm::mat4(1.f), nullptr, Color(0.9f, 0.6f, 0.3f, 1.f));
gfx->present();
}