Files
EVEngine/test/AmbientOcclusion.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

298 lines
9.3 KiB
C++

#include "zeroerr/assert.h"
#include "zeroerr/unittest.h"
#include <SDL2/SDL.h>
#include <cmath>
#include <memory>
#include <string>
#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));
}
float luma(const Color &c) { return (c.r + c.g + c.b) / 3.f; }
void previewTexture(Graphics *gfx, Texture *tex, int ms = 800) {
gfx->setBackgroundColorRGBA(0.08f, 0.09f, 0.12f, 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);
}
}
struct CornerDepth {
int w = 0, h = 0;
// Two walls meeting at a corner near center: closer walls → AO in the crease.
float wallDepth = 0.35f;
float floorDepth = 0.85f;
};
float cornerDepth01(int x, int y, void *userdata) {
auto *b = static_cast<CornerDepth *>(userdata);
const float u = (float(x) + 0.5f) / float(b->w);
const float v = (float(y) + 0.5f) / float(b->h);
// Vertical wall strip + horizontal wall strip (L-shaped occluder / crease).
const bool vertWall = (u >= 0.45f && u <= 0.55f && v >= 0.25f && v <= 0.75f);
const bool horizWall = (v >= 0.45f && v <= 0.55f && u >= 0.25f && u <= 0.75f);
if (vertWall || horizWall) return b->wallDepth;
return b->floorDepth;
}
} // namespace
TEST_CASE("ao.qualityAndModePresets") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx);
AmbientOcclusion *raw = gfx->newAmbientOcclusion();
REQUIRE(raw != nullptr);
std::unique_ptr<AmbientOcclusion> ao(raw);
REQUIRE(ao->getSsaoShader() != nullptr);
REQUIRE(ao->getHbaoShader() != nullptr);
REQUIRE(ao->getGtaoShader() != nullptr);
REQUIRE(ao->getBlurShader() != nullptr);
REQUIRE(ao->getOverlayShader() != nullptr);
CHECK(ao->getMode() == "ssao");
ao->setQuality("low");
CHECK(ao->getQuality() == "low");
CHECK(ao->getSampleCount() == 8);
CHECK(ao->getDownscale() == 4.f);
CHECK(ao->resolutionFor(400) == 100);
ao->setQuality("medium");
CHECK(ao->getSampleCount() == 16);
CHECK(ao->getDownscale() == 2.f);
ao->setQuality("high");
CHECK(ao->getSampleCount() == 24);
CHECK(ao->getDownscale() == 1.f);
ao->setMode("hbao");
CHECK(ao->getMode() == "hbao");
ao->setQuality("medium");
CHECK(ao->getSampleCount() == 36); // 6 dirs * 6 steps
ao->setMode("gtao");
ao->setQuality("low");
CHECK(ao->getSampleCount() == 16); // 4*4
ao->setMode("unknown");
CHECK(ao->getMode() == "ssao");
ao->setQuality("unknown");
CHECK(ao->getQuality() == "medium");
win->close();
}
TEST_CASE("ao.paramRoundTrip") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx);
std::unique_ptr<AmbientOcclusion> ao(gfx->newAmbientOcclusion());
ao->setRadius(0.9f);
ao->setBias(0.04f);
ao->setIntensity(1.25f);
ao->setPower(1.8f);
CHECK(std::fabs(ao->getRadius() - 0.9f) < 1e-5f);
CHECK(std::fabs(ao->getBias() - 0.04f) < 1e-5f);
CHECK(std::fabs(ao->getIntensity() - 1.25f) < 1e-5f);
CHECK(std::fabs(ao->getPower() - 1.8f) < 1e-5f);
CHECK(ao->hasParam("invViewProj") == true);
CHECK(ao->hasParam("sampleCount") == true);
CHECK(ao->hasParam("dirCount") == true);
ao->setMode("gtao");
ao->setThickness(0.7f);
CHECK(std::fabs(ao->getFloat("thickness") - 0.7f) < 1e-5f);
win->close();
}
TEST_CASE("ao.ssaoDarkensCrease") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 256, 192);
gfx->setScreenReadbackEnabled(true);
std::unique_ptr<AmbientOcclusion> ao(gfx->newAmbientOcclusion());
ao->setMode("ssao");
ao->setQuality("high");
ao->setIntensity(1.5f);
ao->setRadius(1.2f);
ao->setBias(0.02f);
ao->setPower(1.2f);
ao->setCamera(0.f, 1.5f, 4.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 50.f, 256.f / 192.f, 0.1f, 40.f);
CornerDepth cd;
cd.w = 128;
cd.h = 96;
Texture *depth = ao->newLinearDepthTexture(gfx, cd.w, cd.h, cornerDepth01, &cd);
REQUIRE(depth != nullptr);
Canvas *out = gfx->newCanvas(cd.w, cd.h);
ao->computeTo(gfx, depth, out);
// Crease center vs open floor corner.
const float crease = luma(out->getPixel(cd.w / 2, cd.h / 2));
const float open = luma(out->getPixel(int(cd.w * 0.12f), int(cd.h * 0.12f)));
CHECK(open > crease);
CHECK(crease < 0.98f);
CHECK(open > 0.5f);
previewTexture(gfx, out->getTexture());
win->close();
}
TEST_CASE("ao.hbaoAndGtaoProduceOcclusion") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 200, 150);
CornerDepth cd;
cd.w = 100;
cd.h = 75;
for (const char *mode : {"hbao", "gtao"}) {
std::unique_ptr<AmbientOcclusion> ao(gfx->newAmbientOcclusion());
ao->setMode(mode);
ao->setQuality("medium");
ao->setIntensity(1.4f);
ao->setRadius(1.0f);
ao->setCamera(0.f, 2.f, 5.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 55.f, 200.f / 150.f, 0.1f, 50.f);
Texture *depth = ao->newLinearDepthTexture(gfx, cd.w, cd.h, cornerDepth01, &cd);
Canvas *out = gfx->newCanvas(cd.w, cd.h);
ao->computeTo(gfx, depth, out);
float minL = 1.f;
float maxL = 0.f;
for (int y = 0; y < cd.h; y += 3) {
for (int x = 0; x < cd.w; x += 3) {
const float L = luma(out->getPixel(x, y));
minL = std::min(minL, L);
maxL = std::max(maxL, L);
}
}
CHECK(maxL > minL);
CHECK(minL < 0.95f);
}
win->close();
}
TEST_CASE("ao.blurPreservesMeanRoughly") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 160, 120);
std::unique_ptr<AmbientOcclusion> ao(gfx->newAmbientOcclusion());
ao->setMode("ssao");
ao->setQuality("medium");
ao->setCamera(0.f, 1.f, 3.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 60.f, 160.f / 120.f, 0.1f, 30.f);
CornerDepth cd;
cd.w = 80;
cd.h = 60;
Texture *depth = ao->newLinearDepthTexture(gfx, cd.w, cd.h, cornerDepth01, &cd);
Canvas *raw = gfx->newCanvas(cd.w, cd.h);
ao->computeTo(gfx, depth, raw);
Canvas *blurred = gfx->newCanvas(cd.w, cd.h);
ao->blurTo(gfx, raw->getTexture(), blurred);
float sumRaw = 0.f, sumBlur = 0.f;
int n = 0;
for (int y = 0; y < cd.h; y += 2) {
for (int x = 0; x < cd.w; x += 2) {
sumRaw += luma(raw->getPixel(x, y));
sumBlur += luma(blurred->getPixel(x, y));
++n;
}
}
const float meanRaw = sumRaw / float(std::max(n, 1));
const float meanBlur = sumBlur / float(std::max(n, 1));
CHECK(std::fabs(meanRaw - meanBlur) < 0.25f);
CHECK(meanBlur > 0.05f);
win->close();
}
TEST_CASE("ao.overlayDarkensLitScene") {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
openGfxWindow(win, gfx, 160, 120);
gfx->setScreenReadbackEnabled(true);
std::unique_ptr<AmbientOcclusion> ao(gfx->newAmbientOcclusion());
ao->setMode("ssao");
ao->setQuality("high");
ao->setIntensity(1.0f);
ao->setCamera(0.f, 1.2f, 3.5f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 50.f, 160.f / 120.f, 0.1f, 35.f);
CornerDepth cd;
cd.w = 80;
cd.h = 60;
Texture *depth = ao->newLinearDepthTexture(gfx, cd.w, cd.h, cornerDepth01, &cd);
Canvas *aoMap = gfx->newCanvas(cd.w, cd.h);
ao->computeTo(gfx, depth, aoMap);
Canvas *scene = gfx->newCanvas(cd.w, cd.h);
gfx->setCanvas(scene);
gfx->clear(Color(0.85f, 0.85f, 0.88f, 1.f), std::nullopt, std::nullopt);
const float before = luma(scene->getPixel(cd.w / 2, cd.h / 2));
ao->applyOverlay(gfx, aoMap->getTexture());
gfx->setCanvas(nullptr);
const float after = luma(scene->getPixel(cd.w / 2, cd.h / 2));
const float openAfter = luma(scene->getPixel(int(cd.w * 0.1f), int(cd.h * 0.1f)));
CHECK(after < before);
CHECK(openAfter + 0.02f >= after);
previewTexture(gfx, scene->getTexture());
win->close();
}