Skip to content

view_set_viewport follow-ups: polygon off from a Draw event, camera fields, bad values - #807

Merged
rwkay merged 2 commits into
developfrom
features/view-set-viewport-followup
Nov 21, 2026
Merged

rwkay merged 2 commits into
developfrom
features/view-set-viewport-followup

Conversation

@rwkay

@rwkay rwkay commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #806 (view_set_viewport, non-rectangular viewports, https://github.com/YoYoGames/GMS2-Runner-Main/issues/1076). YoYoGames/GM-TestFramework#386 at 3cd731d added cases that HTML5 develop (3e6e1ef, with #806) fails. In headless Chrome, 4 of the 6 tests reported: the keeps-fields test failed, and the Draw-event test ended the game before the errors test could run.

Fixes:

  • Polygon off from a Draw event. view_set_viewport([{ polygons: [] }]) while the view's surface is the render target used to end the game with "Unbalanced surface stack". It freed the view's surface and set it to -1, and DrawViews then skipped surface_reset_target() because it checked the view's surface again after the Draw event.
    • DrawViews now resets the target only if it set it.
    • surface_free won't free a surface that is still a target, so the surface view_set_viewport made is queued and freed once it's off the stack, right after the view is drawn. Before, it leaked.
  • Camera fields. The room rectangle now changes only the camera fields that are given. roomRectX on its own used to reset the camera's y to view_yview, undoing a camera_set_view_pos. A call without any roomRect* fields leaves the camera alone, as before.
  • Bad values. polygons with an odd number of values raises an error, and so do non-finite values (NaN, infinity) in polygons or in the screen*/roomRect* fields.

Tests: BasicViewSetViewportTestSuite from GM-TestFramework#386 at 3cd731d, raw build, headless Chrome:

App surface Screen Keeps left-out fields Surface port-sized Polygon off from Draw Errors
WebGL, this branch Passed (11) Passed (7) Passed (19) Passed (5) Passed (2) Passed (17)
Canvas, this branch Passed (11) Passed (7) Passed (19) Passed (5) Passed (2) Passed (17)
WebGL and Canvas, develop 3e6e1ef Passed Passed Failed (roomRectX moved the camera's y) Passed Game ended ("Unbalanced surface stack") Didn't run

There were no console errors on this branch.

Deferred free, checked over CDP in the issue's ViewportTriangles project:

  • With the view's surface set as the target, polygons: [] set view_surface_id to -1 and queued the surface.
  • After surface_reset_target(), it was freed: 4 live surfaces went down to 3, with nothing left queued.

…nt, camera fields, bad values

- Turning a polygon off (polygons : []) from a Draw event, while the view's surface is the render
  target, no longer unbalances the surface stack. DrawViews resets the target only if it set it,
  rather than checking the view's surface again after the Draw event. The surface
  view_set_viewport made is freed once it's off the surface stack (surface_free won't free a
  target), instead of leaking.
- The room rectangle only changes the camera fields that are given, so roomRectX alone doesn't
  undo a camera_set_view_pos y, and a call without roomRect fields leaves the camera alone.
- polygons with an odd number of values, and non-finite values (NaN, infinity) in polygons or
  the screen/roomRect fields, raise errors.

Found by GM-TestFramework#386 at 3cd731d.

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
@rwkay
rwkay requested a review from iampremo November 21, 2026 18:32

@iampremo iampremo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 8ef1195.

Looks good to me. The DrawViews flag fixes the unbalanced-stack crash, and it also covers the opposite case where a Draw event creates the view's surface after the target was chosen. CheckForSurface sees the surface_set_target_system push, so the view's surface gets queued instead of hitting surface_free's "in use" refusal, and the identity check in the flush keeps a reused ID safe. Applying only the given roomRect* fields to the camera fixes the camera_set_view_pos regression. I have two small optional nits below.

Comment thread scripts/functions/Function_Graphics.js Resolved
Comment thread scripts/yyRoom.js Resolved

@gm-auto-review gm-auto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 8ef1195. The summary is in the Automated review comment on this PR.

Reviewed 8ef1195

Comment thread scripts/functions/Function_Graphics.js Resolved
Comment thread scripts/yyRoom.js Resolved
@gm-auto-review

gm-auto-review Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Automated review

This PR follows up on view_set_viewport in three areas. DrawViews now resets the view's surface target only if it set that target itself. A surface made by view_set_viewport that is let go while it is still a render target is now queued and freed once it is off the surface stack. Previously, turning the polygon off from a Draw event ended the game with "Unbalanced surface stack" and leaked that surface. The camera now takes only the roomRect* fields that are given. Odd-length polygons and non-finite numbers are now rejected. The deferred free checks the surface object as well as the ID, so a reused ID is never freed by mistake. Both flush calls are cheap when the queue is empty and sit inside the same @if function("view_set_viewport") guard as their definition. The changes look correct; one nit is about a comment that claims more than the code does.

Open: 1 nit.

New in this review: the review comments.

Reviewed 75a27d3

Advisory: this check doesn't block merging. It runs again on each push. Reply in a comment's thread if it is wrong, and resolve the thread once it is dealt with: the next review reads both.

…thing, flush after the views, exact target flag

- The polygon's values are checked (finite) and the polygon triangulated before any of the entry
  is applied, next to the odd-length check, so a rejected call changes nothing.
- DrawViews also frees queued view_set_viewport surfaces once after the views loop, so a surface
  let go while the game had it as the target outside DrawViews is freed even if no view is visible.
- viewSurfaceTargetSet is true only when surface_set_target_system actually pushed the surface
  (it pushes nothing for a surface that no longer exists).

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
@rwkay
rwkay requested a review from iampremo November 21, 2026 18:48

@iampremo iampremo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 75a27d3.

Re-reviewed at 75a27d3. Both of my nits are fixed, and the code matches the replies.

  • Rejected call: the finite check on polygons and the ear-clip now run next to the odd-length check, before the port, polygon, callbacks or camera are written. So a non-finite value or a polygon that can't be triangulated now leaves the entry unchanged.
  • Flush after the loop: ViewPolygon_FlushSurfaceFrees() now also runs once after the views loop in DrawViews, inside the same @if. A surface queued outside DrawViews is freed on the next frame even if no view is visible.

The bot's two nits are also fixed. The polygon check is the same fix as mine. viewSurfaceTargetSet is now true only if g_SurfaceStack actually grew. That matches surface_set_target_system_RELEASE, which pushes nothing when the surface or its depth surface is missing. So a stale view_surface_id no longer leads to a surface_reset_target() with nothing to pop.

Looks good to me.

@rwkay
rwkay merged commit 21d8f87 into develop Nov 21, 2026
1 check passed

@gm-auto-review gm-auto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 75a27d3. The summary is in the Automated review comment on this PR.

Reviewed 75a27d3

yyError("view_set_viewport() - polygons in entry " + index + " has an odd number of values; it needs x,y pairs");
return;
}
// check and triangulate the polygon before anything is changed, so a call that is rejected changes nothing

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The comment says "a call that is rejected changes nothing", but that only holds within one entry. Entries are checked and applied one at a time in the outer loop, so view_set_viewport([{ screenX: 10, polygons: [...] }, { polygons: [0, 0, 1] }]) applies entry 0 in full (port, polygon, surface, camera) before entry 1 raises the odd-length error. A game that catches the error is left with some entries applied and others not. Either check every entry in a first pass (fields, polygon, earclip, callables) and apply them in a second, or change the comment to say "an entry that is rejected changes nothing for that entry".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants