Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(lighting): the reach a lit quad carries and the tiles the loop's lights are marked in - which lights a box is within the radius of, the lane round-tripping every layer with every reach, the fourth mask word under an orthographic view, a reflected one and a perspective one

yhh1a0e022afe21
diff --git a/tests/renderer/test_light_layers.cpp b/tests/renderer/test_light_layers.cpp
index 0a6db8afe..b83ee660f 100644
--- a/tests/renderer/test_light_layers.cpp
+++ b/tests/renderer/test_light_layers.cpp
@@ -90,6 +90,56 @@ int main() {
"a light whose map is taken away keeps its range");
}
+ // --- which of the loop's lights can reach a lit quad (lightReachMask2D)
+ {
+ GpuLight lamp;
+ lamp.posDir = glm::vec4(0.0f, 0.0f, 0.0f, 10.0f);
+ lamp.color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
+ GpuLight away = lamp;
+ away.posDir.x = 100.0f;
+ GpuLight sun;
+ sun.posDir = glm::vec4(0.0f, -1.0f, 1.0f, 0.0f);
+ sun.color = glm::vec4(1.0f);
+ GpuLight dark = lamp;
+ dark.color.a = 0.0f;
+ const GpuLight lights[4] = {lamp, away, sun, dark};
+ CHECK(lightReachMask2D(lights, 4, {-1.0f, -1.0f}, {1.0f, 1.0f}) == 0b0101u,
+ "a quad under a lamp is reached by it and by the sun, not by a lamp across the level nor one of no intensity");
+ CHECK(lightReachMask2D(lights, 4, {9.5f, -1.0f}, {12.0f, 1.0f}) == 0b0101u, "a quad whose near edge is inside the radius is reached");
+ CHECK(lightReachMask2D(lights, 4, {10.5f, -1.0f}, {12.0f, 1.0f}) == 0b0100u, "one wholly past the radius is not");
+ CHECK(lightReachMask2D(lights, 4, {8.0f, 8.0f}, {9.0f, 9.0f}) == 0b0100u,
+ "nor one past it on the diagonal, though it is inside the radius along each axis");
+ CHECK(lightReachMask2D(lights, 4, {95.0f, -1.0f}, {96.0f, 1.0f}) == 0b0110u, "the far lamp reaches what stands by it");
+ CHECK(lightReachMask2D(lights, 2, {-1.0f, -1.0f}, {1.0f, 1.0f}) == 0b0001u, "lights past the count are not asked");
+
+ GpuLight sixteen[MAX_LIGHTS];
+ for (GpuLight& l : sixteen) l = lamp;
+ CHECK(lightReachMask2D(sixteen, MAX_LIGHTS, {-1.0f, -1.0f}, {1.0f, 1.0f}) == 0x7fffu,
+ "fifteen lights have a bit; the sixteenth has none and is always worked out");
+ }
+
+ // --- the reach rides beside the layer in one lane, and comes apart whole (the decode is applyLighting2DLayer's, line for line)
+ {
+ const auto layerOf = [](f32 lane) { const f32 a = std::fabs(lane); return a - std::floor(a * 0.001953125f) * 512.0f; };
+ const auto reachOf = [](f32 lane) {
+ return lane < 0.0f ? static_cast<u32>(std::floor(std::fabs(lane) * 0.001953125f)) + 32768u : 65535u;
+ };
+ bool ok = true;
+ const u32 masks[] = {0u, 1u, 0x4000u, 0x2aaau, 0x7fffu};
+ for (i32 layer = -128; layer <= 127; ++layer) {
+ for (u32 mask : masks) {
+ const f32 lane = packSpriteLightLane(layer, mask);
+ if (!(lane < 0.0f) || layerOf(lane) != static_cast<f32>(layer) + SPRITE_LAYER_BIAS || reachOf(lane) != (mask | 0x8000u)) ok = false;
+ }
+ }
+ CHECK(ok, "every layer with every reach round-trips, and the sixteenth light's bit is always set");
+ CHECK(std::fabs(packSpriteLightLane(127, 0x7fffu)) < 16777216.0f, "the largest lane fits a float exactly");
+ CHECK(packSpriteLightLane(0, 0xffffu) == packSpriteLightLane(0, 0x7fffu), "a bit past the fifteen is dropped, not carried into the layer");
+ const f32 bare = 5.0f + SPRITE_LAYER_BIAS;
+ CHECK(layerOf(bare) == bare && reachOf(bare) == 65535u, "a lane that is the bare layer, as it was once written, means every light");
+ CHECK(layerOf(0.0f) == 0.0f && reachOf(0.0f) == 65535u, "and a zero lane (a tilemap's) is no layer and every light");
+ }
+
std::printf("%s\n", g_failures == 0 ? "all passed" : "FAILED");
return g_failures == 0 ? 0 : 1;
}
diff --git a/tests/renderer/test_tiled_lights.cpp b/tests/renderer/test_tiled_lights.cpp
index 1bb4ab0a7..5abe4217d 100644
--- a/tests/renderer/test_tiled_lights.cpp
+++ b/tests/renderer/test_tiled_lights.cpp
@@ -178,6 +178,56 @@ int main() {
"restoring the source view recovers its exact tile masks");
}
+ // --- the loop's own sixteen are marked per tile too, in the fourth word, for the flat lit surfaces
+ {
+ LightStore store;
+ store.clear();
+ const glm::mat4 source = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 100.0f);
+ const glm::mat4 reflected = source * glm::scale(glm::mat4(1.0f), glm::vec3(-1.0f, 1.0f, 1.0f));
+ GpuLight lamp;
+ lamp.posDir = {-5.0f, -5.0f, 0.0f, 0.25f};
+ lamp.color = {1.0f, 0.0f, 0.0f, 2.0f};
+ GpuLight sun;
+ sun.posDir = {0.0f, -1.0f, 1.0f, 0.0f};
+ sun.color = {1.0f, 1.0f, 1.0f, 1.0f};
+ store.addLight(lamp);
+ store.addLight(sun);
+ CHECK(store.data().tileInfo.w == 0.0f, "until the loop's lights are marked the block says the word is not there");
+ store.markLoopLights(source);
+ const LightConstants marked = store.data();
+ CHECK(marked.tileInfo.w == 1.0f && marked.tileInfo.y == static_cast<f32>(TILE_COLUMNS) && marked.tileInfo.z == static_cast<f32>(TILE_ROWS),
+ "marking says the word is there and how big the grid is, with no tiled light in the frame");
+ CHECK((marked.tileMask[2 * TILE_COLUMNS + 3].w & 1u) != 0, "the lamp is marked in the lower-left tiles it stands in");
+ CHECK((marked.tileMask[8 * TILE_COLUMNS + 11].w & 1u) == 0, "and not across the picture");
+ u32 lampTiles = 0;
+ bool sunEverywhere = true, othersUntouched = true;
+ for (const auto& mask : marked.tileMask) {
+ lampTiles += mask.w & 1u;
+ sunEverywhere &= (mask.w & 2u) != 0;
+ othersUntouched &= mask.x == 0u && mask.y == 0u && mask.z == 0u && (mask.w >> 2) == 0u;
+ }
+ CHECK(lampTiles > 0 && lampTiles <= 4, "a lamp a quarter of a unit wide is in the few tiles round it");
+ CHECK(sunEverywhere, "a directional light is marked in every tile");
+ CHECK(othersUntouched, "the tiled lights' three words and the unused bits stay clear");
+ CHECK(store.loopTileBits() == lampTiles + kTiles, "and the count of pairs left set is what a gate reads");
+
+ store.setTiledView(reflected);
+ CHECK((store.data().tileMask[2 * TILE_COLUMNS + 12].w & 1u) != 0 && (store.data().tileMask[2 * TILE_COLUMNS + 3].w & 1u) == 0,
+ "another view moves the lamp's tiles with it");
+ store.setTiledView(source);
+ CHECK(std::memcmp(marked.tileMask, store.data().tileMask, sizeof(marked.tileMask)) == 0, "and the source view brings them back");
+
+ // A view that lets depth into the picture's x and y cannot say which tile a flat surface's pixel is lit in.
+ store.markLoopLights(glm::perspective(glm::radians(60.0f), 1.0f, 0.1f, 100.0f)
+ * glm::lookAt(glm::vec3(0.0f, 0.0f, 20.0f), glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
+ bool all = true;
+ for (const auto& mask : store.data().tileMask) all &= (mask.w & 3u) == 3u;
+ CHECK(all && store.loopTileBits() == 2 * kTiles, "through a perspective camera every light is marked everywhere: nothing is skipped");
+
+ store.clear();
+ CHECK(store.data().tileInfo.w == 0.0f && store.loopTileBits() == 0, "a new frame starts unmarked");
+ }
+
CHECK(sizeof(LightConstants) <= 16384, "the whole block fits the 16 KB a uniform block is guaranteed");
std::printf(g_failures == 0 ? "\nALL PASS\n" : "\n%d FAILURE(S)\n", g_failures);