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.

feat(render): the present rect decides the filter, so nothing carries a second answer

yhh931ca26af6df
diff --git a/sdk/src/camera/presentPlan.ts b/sdk/src/camera/presentPlan.ts
index 3a48e151a..e543ad91d 100644
--- a/sdk/src/camera/presentPlan.ts
+++ b/sdk/src/camera/presentPlan.ts
@@ -33,8 +33,9 @@ export enum RenderResolution {
Design = 1,
/**
* As {@link Design}, but the image is presented at a WHOLE multiple of its
- * own size and unfiltered, so no resampling happens at all. Costs black bars
- * wherever a whole multiple does not fill the surface.
+ * own size. That rect IS the instruction — the engine reads whole-multiple-ness
+ * off it and copies rather than interpolates, so nothing carries a second
+ * answer. Costs black bars wherever a whole multiple does not fill the surface.
*/
IntegerMultiple = 2,
}
@@ -49,8 +50,6 @@ export interface PresentPlan {
y: number;
width: number;
height: number;
- /** Whether the present may interpolate. False = a whole-pixel copy. */
- linear: boolean;
/** True when render size equals the destination rect — the present is a no-op copy. */
oneToOne: boolean;
}
@@ -108,7 +107,7 @@ export function planPresent(
// same answer: draw where you present. Also the fallback for a nonsense
// world height, because a zero-tall render target is not a picture.
if (policy === RenderResolution.Surface || !(worldHeight > 0)) {
- return { renderWidth: sw, renderHeight: sh, x: 0, y: 0, width: sw, height: sh, linear: true, oneToOne: true };
+ return { renderWidth: sw, renderHeight: sh, x: 0, y: 0, width: sw, height: sh, oneToOne: true };
}
// The render target keeps the SURFACE's aspect, not the design resolution's:
@@ -118,9 +117,9 @@ export function planPresent(
const renderWidth = clampDim(renderHeight * (sw / sh));
if (policy === RenderResolution.IntegerMultiple) {
- // The largest whole multiple that still fits. Below 1 there is none, and
- // a downscale cannot be whole-pixel — so it falls back to filling, and
- // says so through `linear` rather than pretending to be crisp.
+ // The largest whole multiple that still fits. Below 1 there is none and a
+ // downscale cannot be whole-pixel, so it falls back to filling — the rect
+ // stops being a whole multiple, which is the engine reading the truth.
const k = Math.floor(Math.min(sw / renderWidth, sh / renderHeight));
if (k >= 1) {
const w = renderWidth * k;
@@ -129,7 +128,6 @@ export function planPresent(
renderWidth, renderHeight,
x: Math.floor((sw - w) / 2), y: Math.floor((sh - h) / 2),
width: w, height: h,
- linear: false,
oneToOne: k === 1,
};
}
@@ -138,7 +136,6 @@ export function planPresent(
return {
renderWidth, renderHeight,
x: 0, y: 0, width: sw, height: sh,
- linear: true,
oneToOne: renderWidth === sw && renderHeight === sh,
};
}
@@ -150,3 +147,15 @@ export function planPresent(
export function worldPerRenderedPixel(plan: PresentPlan, worldHeight: number): number {
return plan.renderHeight > 0 ? worldHeight / plan.renderHeight : 0;
}
+
+/**
+ * Whether a plan's present is a whole multiple of its render size — the property
+ * the engine derives from the same two rects to decide whether to interpolate.
+ * Here so a test can assert the rect rather than a claim about it.
+ */
+export function presentIsWholeMultiple(plan: PresentPlan): boolean {
+ if (plan.oneToOne) return false;
+ const kx = plan.width / plan.renderWidth;
+ const ky = plan.height / plan.renderHeight;
+ return Number.isInteger(kx) && kx === ky;
+}
diff --git a/sdk/tests/present-plan.test.ts b/sdk/tests/present-plan.test.ts
index d94f40867..dc8cbf584 100644
--- a/sdk/tests/present-plan.test.ts
+++ b/sdk/tests/present-plan.test.ts
@@ -9,7 +9,7 @@
*/
import { describe, it, expect } from 'vitest';
import {
- planPresent, worldPerRenderedPixel, viewportPixels, RenderResolution,
+ planPresent, worldPerRenderedPixel, viewportPixels, presentIsWholeMultiple, RenderResolution,
} from '../src/camera/presentPlan';
/** The surfaces a 1080-tall design actually meets: a panel, a window, a big screen. */
@@ -98,7 +98,7 @@ describe('planPresent — IntegerMultiple', () => {
expect(plan.renderWidth).toBe(960);
expect(plan.height).toBe(1080);
expect(plan.width).toBe(1920);
- expect(plan.linear).toBe(false);
+ expect(presentIsWholeMultiple(plan)).toBe(true);
expect(plan.x).toBe(0);
expect(plan.y).toBe(0);
});
@@ -110,7 +110,9 @@ describe('planPresent — IntegerMultiple', () => {
expect(plan.height).toBe(540);
expect(plan.y).toBe(180);
expect(plan.x).toBe((1600 - plan.width) / 2);
- expect(plan.linear).toBe(false);
+ // At 1x the present does not scale at all, so no filter question arises —
+ // a 1:1 blit lands on texel centres whatever the sampler says.
+ expect(plan.oneToOne).toBe(true);
});
it('the whole-multiple rect is exactly k times the render size, both axes', () => {
@@ -118,20 +120,28 @@ describe('planPresent — IntegerMultiple', () => {
// resample, which is the thing this policy promises never to do.
for (const [w, h] of SURFACES) {
const plan = planPresent(RenderResolution.IntegerMultiple, 360, w, h);
- if (!plan.linear) {
+ if (presentIsWholeMultiple(plan)) {
expect(plan.width / plan.renderWidth).toBe(plan.height / plan.renderHeight);
expect(Number.isInteger(plan.width / plan.renderWidth)).toBe(true);
}
}
});
- it('says linear rather than pretending, when it must shrink', () => {
- // A render taller than the surface has no whole multiple. Claiming crisp
- // here would be the promise `pixelPerfect` already makes and cannot keep.
+ it('stops being a whole multiple when it must shrink, rather than pretending', () => {
+ // A render taller than the surface has no whole multiple, so the rect says
+ // so and the engine interpolates. Claiming crisp here would be the promise
+ // `pixelPerfect` already makes and cannot keep.
const plan = planPresent(RenderResolution.IntegerMultiple, 2160, 1244, 700);
- expect(plan.linear).toBe(true);
+ expect(presentIsWholeMultiple(plan)).toBe(false);
expect(worldPerRenderedPixel(plan, 2160)).toBe(1);
});
+
+ it('Design can land on a whole multiple too, and the engine will see it', () => {
+ // The filter follows the RECT, not the policy. A smooth-scaling project
+ // whose surface happens to be an exact 2x gets the exact copy for free.
+ const plan = planPresent(RenderResolution.Design, 540, 1920, 1080);
+ expect(presentIsWholeMultiple(plan)).toBe(true);
+ });
});
describe('viewportPixels', () => {
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index a69f461de..bcabfc255 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -138,6 +138,18 @@ rg::TargetDesc PostProcessPipeline::chainTarget(bool withDepth) const {
return desc;
}
+/**
+ * The last target in the chain, which only the blit samples. Bilinear everywhere
+ * else for the reason above; here the present decides, because a whole-multiple
+ * upscale wants an exact copy and interpolating it blurs a picture that needed
+ * no resampling.
+ */
+rg::TargetDesc PostProcessPipeline::blitSourceTarget(bool withDepth) const {
+ rg::TargetDesc desc = chainTarget(withDepth);
+ if (presentIsWholeMultiple()) desc.linearFilter = false;
+ return desc;
+}
+
void PostProcessPipeline::shutdown() {
if (!initialized_) return;
@@ -336,7 +348,11 @@ void PostProcessPipeline::begin(const f32* clearColor) {
// The graph opens HERE and not at end(), because the scene target is its
// first resource and the scene is drawn before any pass can be declared.
graph_->begin(width_, height_);
- sceneResource_ = graph_->createExternalTarget(chainTarget(scene_needs_depth_));
+ // With no pass to run, the blit samples the scene target itself, so the
+ // present's filter is the one that applies to it.
+ const bool sceneIsBlitSource = bypass_ || passes_.empty();
+ sceneResource_ = graph_->createExternalTarget(
+ sceneIsBlitSource ? blitSourceTarget(scene_needs_depth_) : chainTarget(scene_needs_depth_));
if (sceneResource_ == rg::kNoResource) {
ES_LOG_ERROR("PostProcessPipeline: no scene target this frame");
return;
@@ -531,9 +547,17 @@ void PostProcessPipeline::runChain(std::vector<PostProcessPass>& passes, rg::Res
const u32 outH = std::max(height_, output_vp_y_ + output_vp_h_);
const rg::ResourceId out = graph_->importTarget(output_target_fbo_, outW, outH);
const rg::TargetDesc desc = chainTarget(/*withDepth=*/false);
+ const rg::TargetDesc blitDesc = blitSourceTarget(/*withDepth=*/false);
+
+ // Which enabled pass is last decides which target the blit will sample.
+ usize lastEnabled = passes.size();
+ for (usize i = passes.size(); i-- > 0;) {
+ if (passes[i].enabled) { lastEnabled = i; break; }
+ }
rg::ResourceId input = scene;
- for (auto& pass : passes) {
+ for (usize i = 0; i < passes.size(); ++i) {
+ auto& pass = passes[i];
if (!pass.enabled) continue;
rg::PassDesc node;
node.name = pass.name;
@@ -541,7 +565,7 @@ void PostProcessPipeline::runChain(std::vector<PostProcessPass>& passes, rg::Res
// composite (bloom's last link) needs the un-blurred image, and the
// shaders address it as unit 1 — the declaration IS that wiring.
node.reads = {input, scene};
- node.write = graph_->createTarget(desc);
+ node.write = graph_->createTarget(i == lastEnabled ? blitDesc : desc);
node.execute = [this, &pass](const rg::PassContext& ctx) { renderPass(pass, ctx); };
input = node.write;
graph_->addPass(std::move(node));
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.hpp b/src/esengine/renderer/frame/PostProcessPipeline.hpp
index 5fdfe8a74..f1fbf8e9a 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.hpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.hpp
@@ -217,6 +217,20 @@ public:
&& (output_vp_w_ != width_ || output_vp_h_ != height_);
}
+ /**
+ * @brief Whether the present is a whole-multiple upscale of the chain.
+ *
+ * @details Inferred from the two rects, for the reason presentScales() is:
+ * the rect is the one authority, and a filter beside it is a second
+ * one to disagree with. A whole multiple copies exactly, and
+ * interpolating it blurs a picture that needed no resampling.
+ */
+ bool presentIsWholeMultiple() const {
+ if (!presentScales() || width_ == 0 || height_ == 0) return false;
+ if (output_vp_w_ % width_ || output_vp_h_ % height_) return false;
+ return output_vp_w_ / width_ == output_vp_h_ / height_;
+ }
+
/**
* @brief Skip the EFFECTS, not the chain.
*
@@ -331,6 +345,7 @@ private:
GfxPixelFormat interFormat() const;
/** The shape of every target in a chain, the scene target included. */
rg::TargetDesc chainTarget(bool withDepth) const;
+ rg::TargetDesc blitSourceTarget(bool withDepth) const;
void ensureGraph();
void ensureScreenQuad();
void drawScreenQuad();