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

158 lines
5.5 KiB
C++

#include "zeroerr/assert.h"
#include "zeroerr/unittest.h"
#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/RenderSystem3D.h"
#include "graphics/ScreenSpaceReflection.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/TextureSampler.h"
#include "graphics/Volumetric.h"
#include "graphics/Water.h"
#include "graphics/Waterfall.h"
#include "image/ImageData.h"
#include "window/Window.h"
#include <SDL2/SDL.h>
#include <cmath>
#include <cstdint>
#include <memory>
#include <optional>
#include <vector>
// Color lives in eve::graphics (see graphics/Canvas.h); keep the unqualified form.
using eve::graphics::Color;
using namespace eve::graphics;
namespace {
struct GraphicsFixture {
eve::window::Window *win = nullptr;
Graphics *gfx = nullptr;
GraphicsFixture(int w = 320, int h = 240) {
win = eve::window::Window::create();
gfx = Graphics::create();
eve::window::WindowSettings s;
s.width = static_cast<uint16_t>(w);
s.height = static_cast<uint16_t>(h);
s.centered = true;
win->setWindowSettings(s);
}
~GraphicsFixture() { win->close(); }
};
std::vector<uint8_t> makeSolid(int w, int h, uint8_t r, uint8_t g, uint8_t b) {
std::vector<uint8_t> px(size_t(w) * size_t(h) * 4);
for (size_t i = 0; i < px.size(); i += 4) {
px[i + 0] = r;
px[i + 1] = g;
px[i + 2] = b;
px[i + 3] = 255;
}
return px;
}
} // namespace
TEST_CASE("TextureSampler.mipmapCountForSize") {
CHECK_EQ(mipmapCountForSize(1, 1), 1);
CHECK_EQ(mipmapCountForSize(2, 2), 2);
CHECK_EQ(mipmapCountForSize(256, 128), 9);
CHECK_EQ(mipmapCountForSize(64, 64), 7);
}
TEST_CASE("TextureSampler.parseFilterAndMipmap") {
CHECK_EQ(static_cast<int>(TextureSampler::parseFilter("nearest")),
static_cast<int>(FilterMode::Nearest));
CHECK_EQ(static_cast<int>(TextureSampler::parseFilter("linear")),
static_cast<int>(FilterMode::Linear));
CHECK_EQ(static_cast<int>(TextureSampler::parseMipmap("none")),
static_cast<int>(MipmapMode::Disabled));
CHECK_EQ(static_cast<int>(TextureSampler::parseMipmap("linear")),
static_cast<int>(MipmapMode::Linear));
CHECK_EQ(static_cast<int>(TextureSampler::parseMipmap("nearest")),
static_cast<int>(MipmapMode::Nearest));
}
TEST_CASE("GraphicsSmoke.textureMipmapsAndAnisotropy") {
GraphicsFixture fx;
auto px = makeSolid(64, 64, 200, 40, 40);
TextureCreateInfo info = TextureCreateInfo::withMipmaps(true, 8.f);
Texture *tex = fx.gfx->newTexture(64, 64, px.data(), info);
REQUIRE(tex != nullptr);
CHECK_EQ(tex->getMipmapCount(), mipmapCountForSize(64, 64));
CHECK_EQ(static_cast<int>(tex->getSampler().mipmap), static_cast<int>(MipmapMode::Linear));
CHECK(tex->getSampler().maxAnisotropy >= 8.f - 1e-3f);
const float deviceMax = fx.gfx->getMaxAnisotropy();
CHECK(deviceMax >= 1.f);
// Nearest pixel-art style sampler without rebuilding image.
fx.gfx->setTextureSampler(tex, TextureSampler::nearest());
CHECK_EQ(static_cast<int>(tex->getSampler().min), static_cast<int>(FilterMode::Nearest));
CHECK_EQ(static_cast<int>(tex->getSampler().mag), static_cast<int>(FilterMode::Nearest));
// Draw once to exercise descriptor rewrite path.
fx.gfx->setBackgroundColor(Color(0.05f, 0.05f, 0.08f, 1.f));
fx.gfx->clear(std::nullopt, std::nullopt, std::nullopt);
fx.gfx->drawTexturedRect(tex, 10.f, 10.f, 64.f, 64.f, Color(1, 1, 1, 1));
fx.gfx->present();
}
TEST_CASE("GraphicsSmoke.cubemapGeneratesMipChain") {
GraphicsFixture fx;
const int face = 16;
std::vector<uint8_t> faces(size_t(face) * face * 4 * 6, 128);
Texture *cube = fx.gfx->newCubemap(face, faces.data());
REQUIRE(cube != nullptr);
CHECK_EQ(cube->getMipmapCount(), mipmapCountForSize(face, face));
CHECK_EQ(static_cast<int>(cube->getSampler().mipmap), static_cast<int>(MipmapMode::Linear));
}
TEST_CASE("Renderable3D.meshLodSelectsByDistance") {
auto *ent = Renderable3D::create();
REQUIRE(ent != nullptr);
// Use distinct non-null-ish pointers via cast of integers — only identity matters.
Mesh *hi = reinterpret_cast<Mesh *>(uintptr_t(0x1000));
Mesh *mid = reinterpret_cast<Mesh *>(uintptr_t(0x2000));
Mesh *lo = reinterpret_cast<Mesh *>(uintptr_t(0x3000));
ent->setMeshLod(0, hi);
ent->setMeshLod(1, mid, 20.f);
ent->setMeshLod(2, lo, 50.f);
CHECK_EQ(ent->getMeshLodCount(), 3);
CHECK_EQ(ent->getMeshLodLevelAtDistance(0.f), 0);
CHECK_EQ(ent->getMeshLodLevelAtDistance(19.9f), 0);
CHECK_EQ(ent->getMeshLodLevelAtDistance(20.f), 1);
CHECK_EQ(ent->getMeshLodLevelAtDistance(49.9f), 1);
CHECK_EQ(ent->getMeshLodLevelAtDistance(50.f), 2);
CHECK_EQ(ent->getMeshLodLevelAtDistance(999.f), 2);
auto mr = ent->meshRenderer();
CHECK(mr->meshForDistance(5.f) == hi);
CHECK(mr->meshForDistance(25.f) == mid);
CHECK(mr->meshForDistance(80.f) == lo);
ent->clearMeshLod();
CHECK_EQ(ent->getMeshLodCount(), 0);
CHECK(mr->meshForDistance(80.f) == hi); // falls back to primary mesh
}