xdg_surface_handle_preferred_scale set Window::scale and did nothing
else (#165). The buffers' size and the surface's follow the scale only
where the window works one out from the other, which it did when it
made a swapchain and when a frame took a configured size. So a scale
that changed by itself, with no configure (the issue's case is a
window moved to an output of another scale), reached them only at the
next of those, and until then the window drew at the old scale. The
pointer's and the touches' positions are turned into pixels with
Window::scale, and the text input's caret back into surface-local
units with it, so those were converted with the new scale while the
frames were still drawn at the old one.
The issue suggests making the swapchain again in the listener. That
predates 89111e0, which moved the configure's rebuild out of its
listener into Render() for #199 and #200; the scale takes the same
way.
Window: the listener keeps the scale in a new member, preferredScale,
as wp_fractional_scale_v1 sends it, in 120ths, and Window::scale is
the scale the window draws at. MatchSizesToScale, where the window
works out the size it was not given from the one it was, sets it from
preferredScale where it takes a new scale, and nothing else in the
library sets it. TakeDeferredSize takes a preferredScale that is not
the scale the window draws at, at a frame boundary of Render(), as it
takes a configured size, and with one where one waits. A window the
compositor sized keeps its surface's size and takes the pixels for it
at the new scale, so Render() makes the swapchain again there and
onResize fires where those pixels differ. One the application sized
keeps its pixels and makes no swapchain, and its surface takes the
size they come to at the new scale, which the next frame sends with
its buffer (SendFrameSurfaceState). Before the start of the next frame
the scale is taken only with a swapchain made where it gives the
window new pixels: a window the compositor sized whose surface comes
to other pixels at it, as after a present or with the swapchain a
SetPresentMode makes outside a frame. Anywhere else it would change
only the conversions and the size the surface is to take, which goes
out with the next frame, and until that frame's commit the compositor
maps the surface, and the pointer and the touches on it, by the
destination it has: a click in between would be converted for a frame
not drawn yet, landing where the new scale puts it on the one drawn at
the old scale, and the onResize of a swapchain made for another reason
at the pixels the window has would not tell the application of the
scale. So a scale that leaves the pixels as they are waits for the
start of the next frame (for a window the application sized, whose
pixels are its own, and one the compositor sized that it leaves at its
pixels), also where a present mode, a rebuild that waited or a Resize
makes a swapchain before then, and so does a configured size that
comes to the pixels the window has with it (an output's scale changed
under a window that fills it). TakeDeferredSize and RebuildIfDeferred
are told which of the two boundaries they run at (FrameBoundary), and
MatchSizesToScale whether it takes the scale whatever it does to the
pixels (takeScale), as at a frame's start and in the constructor. The
listener sends no request and makes no Vulkan call, so the window goes
on drawing at the old scale, and converting with it, until then; a
scale that a listener of a frame dispatched is taken at the boundary
after its present where it gives the window new pixels, and at the
start of the next frame otherwise. From the window's first swapchain
on, the listener fires onFrameOwed where the scale is not the one the
window draws at, so that a window that draws on demand learns that a
frame is owed, as it does from a configure; for a window the
application sized no onResize comes. Before the first swapchain the
listener only keeps the scale, which the constructor takes before it
makes the swapchain, as it did. A scale of 0, with which no size can
be worked out, is not taken. Where MatchSizesToScale changes the
scale, it takes what the window holds in pixels of the scale before to
the new one: currentMousePos, lastMousePos and mouseDelta, and the
touch slots of the window's fingers. The compositor gives those in
surface-local units and sends nothing for a pointer or a finger that
rests, so a pointer resting over the window would otherwise stand at
the old scale's pixels in the frames drawn at the new one until it
moved, and the next delta would be the change of grid. The pointer's
position as the compositor gave it is kept beside, in surface-local
units, in a new member, surfaceMousePos, which no scale moves. And it
sends the caret SetTextInput was given again, converted with the new
scale, as the window does when the keyboard focus comes back: for a
window the application sized, the surface changes size around pixels
that stay, and the rectangle the input method holds would otherwise
stay in the old units.
The configure listener's test for a configure at the size the window
has compares the buffers with the pixels for that size at
preferredScale, the scale the frame takes with the size, as it
compared them at Window::scale while the listener set that. While a
new scale waits it finds no configure at the size the window has: one
for the size the surface has waits for the frame that takes the scale
and takes its size with it, so that the window is not made one the
compositor sized while it draws at the scale before, at pixels that
are not that size's at the scale it draws at.
Input: Map took its delta for MouseDeltaBind from currentMousePos and
a last position of its own, which the window's conversion does not
reach, so a pointer that rests would read the change of grid as a
delta at the Map's next Tick, which a camera bound to MouseDeltaBind
turns by. On Wayland it takes it from surfaceMousePos, as the
surface-local motion since its last Tick in the window's pixels of
now: none for a pointer that rests, whatever scales the window took in
between, where a last position taken along to each new scale would
come back with a float residue through two of them. A position the
application writes into currentMousePos itself does not reach it
there.
Device: the pointer's and the touches' handlers keep the position the
compositor gave in surfaceMousePos (the first finger's for the pointer
it stands in for), and touch_down, touch_motion and an enter's move
work it into the pixels they store with the scale in force at the
store. They store after listeners, of onTouchDown, onTouchMove and
onMouseEnter, that may take a new scale (a SetPresentMode outside a
frame for a window the compositor sized, the Render() of a frame
callback one of them dispatched), and a position worked out before
those listeners would be in the pixels of the scale before. The
conversions read Window::scale, which is now the scale of the frames
drawn since the boundary that took it.
Docs: the README, the consumer notes and the guide's gotchas say what
scale and preferredScale are and when a scale is taken; the first two
said that scale could be newer than the sizes (#165). The README, the
consumer notes and the comment on SetTextInput say what goes to the
new scale with it, and that the caret is sent again; the consumer
notes name surfaceMousePos, the Map's delta from it, and that a
position the application writes into currentMousePos does not reach
the Map. The bug-prover lens section names preferredScale as what
changes in any dispatch, and the scale among what waits for a frame
boundary, and that before the next frame's start it is taken only
where it gives the window new pixels; the consumer notes, that a size
configured during a frame waits with a new scale where the two come to
the pixels the window has.
Tests: ConfigureResize names the scale in 120ths, as the compositor
does, and drives the scale's listener on a window whose surfaces are
proxies. The listener sends nothing, leaves the scale and both sizes
as they are, and fires onFrameOwed. Until a frame boundary takes the
scale, a pointer motion to the surface's middle is at the middle of
the buffers drawn at the scale before, and so is a finger that goes
down there on the window the application sized; a caret given in the
pixels of the window the compositor sized goes to a text input proxy
converted with that scale. The next boundary takes it: a window the
compositor sized keeps 400x300 and goes from 800x600 to 600x450 pixels
at 1.5, which makes the swapchain, and its surface is sent nothing;
one the application sized keeps its 640x480 pixels and makes no
swapchain, and its surface goes from 320x240 to 640x480 at 1, sent
with the frame. After it the motion, the finger's move and the caret
are converted with the new scale. And before any new motion or call,
the boundary has taken what the window held to the new scale: the
pointer of the window the compositor sized is at the middle of the new
buffers, the one the first finger stands in for on the other window is
where its surface-local place comes to, a second finger that went down
before the boundary lifts after it where its surface-local place comes
to, and each boundary sends the caret again, converted, with no
SetTextInput. An Input::Map attached to the window and ticked across a
boundary that takes 1.5 reads no delta for a pointer that rests, and a
motion of 10 units after it as 15 pixels; ticked across two boundaries
between two Ticks on a window the compositor sized, 1.75 taken by a
swapchain made outside a frame at the pixels it gives the window and 1
again by a frame's start, with the pointer resting at surface 100.25,
it reads no delta and fires no event. On a window the compositor
sized, a listener of onTouchDown, of onTouchMove and of onMouseEnter
that takes a scale that waits has the click the finger stands in for,
the finger's move and the move after the enter in the pixels of that
scale. A scale that comes back before a frame owes nothing; one that
comes while a frame is being recorded leaves that frame at the scale
it began with, and the window the compositor sized takes it at the
boundary after the present, with its new pixels, where the one the
application sized takes it at the next frame's start: the boundary
after the present takes nothing, a motion after it is still at the
pixels of the frame on screen, and the caret is sent again only at the
next frame's start (the fixture steps the boundary after the present,
End, apart from the next frame's start, Begin); a configure that comes
with it to the pixels the window has waits for that start as well,
which takes the two together, the frame bringing the surface its size
and the acknowledgement, and so does a scale that leaves a small
window the compositor sized at its pixels; a configure without a size
while a scale waits is acknowledged in the listener, and one for the
size the surface has waits for the frame that takes the scale, also
where its pixels at that scale are the window's (101x101 pixels at 2,
a surface of 51x51, a scale of 237/120); before the first swapchain
the listener only keeps the scale, which MatchSizesToScale takes, and
a scale of 0 is not taken. In the cases there were, the check that
nothing is owed counts a scale that waits as well, the frame whose
scale changes while it is recorded is followed to the boundary that
takes it, a swapchain made between frames is checked to leave no scale
waiting for a window the compositor sized, and for one the application
sized to take none, a motion until the next frame's start landing at
the pixels of the frame on screen, also where the swapchain is made
after a present that found it out of date, with a configure that comes
with the scale to the window's pixels waiting too, and the case of a
scale and then a configure counts onFrameOwed for both. ResizeViewport
drives the scale against the compositor, where it has the fractional
scale and the viewporter: on its window as the application sized it,
and maximized where the compositor configures a size for that, it
calls the listener with a scale the window does not draw at and then
with the compositor's own. Each time it checks that the listener
rebuilds nothing and fires onFrameOwed, that the next Render() takes
the scale, with no swapchain and no onResize for the application's
size and with a new swapchain and one onResize from inside Render()
for the maximized one, and that a pointer motion to the surface's
middle is at the buffers' middle, to within the rounding of one size
from the other, before that Render() and after it, and that the
pointer, resting across that Render(), is where its surface-local
place comes to at the new scale. The log's checks hold each of those
commits to its buffer at the scales the marks name.
2618 lines
No EOL
117 KiB
C++
2618 lines
No EOL
117 KiB
C++
//SPDX-License-Identifier: LGPL-3.0-only
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <linux/input-event-codes.h>
|
|
#include "../lib/xdg-shell-client-protocol.h"
|
|
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
|
#include "../lib/fractional-scale-v1.h"
|
|
#include "../lib/viewporter.h"
|
|
#include "../lib/text-input-unstable-v3-client-protocol.h"
|
|
#include <string.h>
|
|
#include <cassert>
|
|
#include <linux/input.h>
|
|
#include <sys/mman.h>
|
|
#include <wayland-cursor.h>
|
|
#include <xkbcommon/xkbcommon.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <print>
|
|
#include <wayland-client.h>
|
|
#include <wayland-client-protocol.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <poll.h>
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
#include <windows.h>
|
|
#include <windowsx.h>
|
|
#include <cassert>
|
|
#endif
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
#include "vulkan/vulkan.h"
|
|
#include "vulkan/vk_enum_string_helper.h"
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
#include "vulkan/vulkan_wayland.h"
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
#include "vulkan/vulkan_win32.h"
|
|
#endif
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
#include "../lib/stb_image_write.h"
|
|
#endif
|
|
module Crafter.Graphics:Window_impl;
|
|
import :Window;
|
|
import :Device;
|
|
import :Gamepad;
|
|
// The Vulkan-typed partitions exist as empty stubs in DOM builds (the
|
|
// build system scans `import :X` statements pre-preprocessor, so even
|
|
// guarded imports must resolve to a real partition). Their bodies are
|
|
// gated under !CRAFTER_GRAPHICS_WINDOW_DOM so DOM compiles see empty
|
|
// modules. Cheap.
|
|
import :VulkanTransition;
|
|
import :DescriptorHeapVulkan;
|
|
import :RenderPass;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
import :WebGPU;
|
|
import :DescriptorHeapWebGPU;
|
|
#endif
|
|
import std;
|
|
|
|
using namespace Crafter;
|
|
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
void randname(char *buf) {
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
long r = ts.tv_nsec;
|
|
for (int i = 0; i < 6; ++i) {
|
|
buf[i] = 'A'+(r&15)+(r&16)*2;
|
|
r >>= 5;
|
|
}
|
|
}
|
|
|
|
int anonymous_shm_open(void) {
|
|
char name[] = "/hello-wayland-XXXXXX";
|
|
int retries = 100;
|
|
|
|
do {
|
|
randname(name + strlen(name) - 6);
|
|
|
|
--retries;
|
|
// shm_open guarantees that O_CLOEXEC is set
|
|
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
if (fd >= 0) {
|
|
shm_unlink(name);
|
|
return fd;
|
|
}
|
|
} while (retries > 0 && errno == EEXIST);
|
|
|
|
return -1;
|
|
}
|
|
|
|
int create_shm_file(off_t size) {
|
|
int fd = anonymous_shm_open();
|
|
if (fd < 0) {
|
|
return fd;
|
|
}
|
|
|
|
if (ftruncate(fd, size) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
#endif
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
// Extract the layout-independent raw key code from a WM_KEY* lParam. Bits
|
|
// 16-23 hold the PS/2 set-1 scancode byte; bit 24 is the extended-key flag
|
|
// (the 0xE0-prefixed variants — RightCtrl, RightAlt, the cursor cluster,
|
|
// keypad Enter/Slash, the Windows keys). We pack the extended flag into bit
|
|
// 8 of the returned KeyCode so it round-trips with the compile-time
|
|
// `Key(CrafterKeys::...)` table in :Keys.
|
|
static inline KeyCode KeyCodeFromLParam(LPARAM lParam) {
|
|
return ((KeyCode)((lParam >> 16) & 0xFF))
|
|
| (((lParam >> 24) & 1u) << 8);
|
|
}
|
|
|
|
// The library's strings are UTF-8, and the W functions take UTF-16.
|
|
// Malformed sequences come out as U+FFFD.
|
|
static std::wstring Utf16FromUtf8(std::string_view text) {
|
|
if (text.empty()) return {};
|
|
const int length = MultiByteToWideChar(CP_UTF8, 0, text.data(), static_cast<int>(text.size()), nullptr, 0);
|
|
if (length <= 0) return {};
|
|
std::wstring wide(static_cast<std::size_t>(length), L'\0');
|
|
MultiByteToWideChar(CP_UTF8, 0, text.data(), static_cast<int>(text.size()), wide.data(), length);
|
|
return wide;
|
|
}
|
|
|
|
// Define a window class name
|
|
const wchar_t g_szClassName[] = L"myWindowClass";
|
|
|
|
// The Window an HWND belongs to, or nullptr once ~Window has detached it.
|
|
// Looked up again after an Invoke that more work follows: a listener may
|
|
// have destroyed the Window.
|
|
static Window* WindowOf(HWND hwnd) {
|
|
return reinterpret_cast<Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
|
|
}
|
|
|
|
// Window procedure function that processes messages
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
|
|
if (msg == WM_NCCREATE)
|
|
{
|
|
CREATESTRUCTW* pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
|
|
Window* window = static_cast<Window*>(pCreate->lpCreateParams);
|
|
|
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(window));
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// Messages before WM_NCCREATE, and those DestroyWindow sends from
|
|
// ~Window, have no Window to go to.
|
|
Window* window = WindowOf(hwnd);
|
|
if (window == nullptr) {
|
|
return DefWindowProcW(hwnd, msg, wParam, lParam);
|
|
}
|
|
|
|
switch (msg) {
|
|
case WM_CLOSE: {
|
|
// A request, as xdg_toplevel.close is: StartSync's loop ends
|
|
// and the HWND lives on until ~Window destroys it, so the
|
|
// swapchain never presents to a window that is gone.
|
|
// DefWindowProc would destroy it here.
|
|
window->onClose.Invoke();
|
|
window = WindowOf(hwnd);
|
|
if (window != nullptr) {
|
|
window->open = false;
|
|
}
|
|
return 0;
|
|
}
|
|
case WM_SIZE: {
|
|
// SIZE_MINIMIZED reports (0, 0) — Resize() short-circuits, so
|
|
// we just propagate the values directly. WM_SIZE fires
|
|
// synchronously during a drag-resize loop; the StartSync
|
|
// pump runs WndProc between frames, so the swapchain is
|
|
// never touched mid-Render.
|
|
if (window) {
|
|
window->Resize(LOWORD(lParam), HIWORD(lParam));
|
|
}
|
|
break;
|
|
}
|
|
case WM_KEYDOWN:
|
|
case WM_SYSKEYDOWN: { // SYSKEYDOWN catches Alt combos, F10, etc.
|
|
KeyCode code = KeyCodeFromLParam(lParam);
|
|
bool isRepeat = (lParam & (1 << 30)) != 0;
|
|
|
|
if (isRepeat) {
|
|
window->onRawKeyHold.Invoke(code);
|
|
} else {
|
|
window->heldKeys.insert(code);
|
|
window->onRawKeyDown.Invoke(code);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WM_KEYUP:
|
|
case WM_SYSKEYUP: {
|
|
KeyCode code = KeyCodeFromLParam(lParam);
|
|
window->heldKeys.erase(code);
|
|
window->onRawKeyUp.Invoke(code);
|
|
break;
|
|
}
|
|
|
|
case WM_CHAR: {
|
|
// wParam is a UTF-16 code unit. May be a surrogate — buffer until we have a pair.
|
|
wchar_t wc = (wchar_t)wParam;
|
|
|
|
// Filter control characters (backspace=0x08, tab=0x09, enter=0x0D, escape=0x1B, etc.)
|
|
if (wc < 0x20 || wc == 0x7f) break;
|
|
|
|
// Handle UTF-16 surrogate pairs (characters outside the BMP, e.g. emoji).
|
|
static wchar_t highSurrogate = 0;
|
|
wchar_t utf16[2];
|
|
int utf16Len;
|
|
|
|
if (wc >= 0xD800 && wc <= 0xDBFF) {
|
|
// High surrogate — stash it and wait for the low surrogate.
|
|
highSurrogate = wc;
|
|
break;
|
|
} else if (wc >= 0xDC00 && wc <= 0xDFFF) {
|
|
// Low surrogate — pair with the stashed high surrogate.
|
|
if (highSurrogate == 0) break; // orphaned low surrogate, ignore
|
|
utf16[0] = highSurrogate;
|
|
utf16[1] = wc;
|
|
utf16Len = 2;
|
|
highSurrogate = 0;
|
|
} else {
|
|
utf16[0] = wc;
|
|
utf16Len = 1;
|
|
}
|
|
|
|
// Convert UTF-16 to UTF-8.
|
|
char utf8[8];
|
|
int n = WideCharToMultiByte(CP_UTF8, 0, utf16, utf16Len, utf8, sizeof(utf8), nullptr, nullptr);
|
|
if (n > 0) {
|
|
window->onTextInput.Invoke(std::string(utf8, n));
|
|
}
|
|
break;
|
|
}
|
|
case WM_MOUSEMOVE: {
|
|
// Signed: under capture, a drag that has left the window keeps
|
|
// reporting positions past the client area.
|
|
const POINT point{GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
|
|
const Vector<float, 2> position(static_cast<float>(point.x), static_cast<float>(point.y));
|
|
// Win32 has no enter message: the first move over the client
|
|
// area after a leave is the enter. TME_LEAVE is one-shot, so it
|
|
// is armed again for each enter, and the enter only counts once
|
|
// it is armed, or its leave would never come.
|
|
RECT client;
|
|
if (!window->mouseInside && GetClientRect(hwnd, &client) && PtInRect(&client, point)) {
|
|
TRACKMOUSEEVENT track{sizeof(track), TME_LEAVE, hwnd, 0};
|
|
if (TrackMouseEvent(&track)) {
|
|
window->PointerEnter(position);
|
|
window = WindowOf(hwnd);
|
|
if (window == nullptr) break;
|
|
}
|
|
}
|
|
window->PointerMove(position);
|
|
break;
|
|
}
|
|
case WM_MOUSELEAVE: {
|
|
// Also sent when the pointer moves onto the non-client area
|
|
// (title bar, borders) — it has left the drawable surface.
|
|
window->PointerLeave();
|
|
break;
|
|
}
|
|
// A press captures the mouse, so a drag that leaves the window
|
|
// still sends its moves and its release here, as Wayland's
|
|
// implicit grab does. The capture goes when no button is held.
|
|
case WM_LBUTTONDOWN: {
|
|
SetCapture(hwnd);
|
|
window->mouseLeftHeld = true;
|
|
window->onMouseLeftClick.Invoke();
|
|
break;
|
|
}
|
|
|
|
case WM_LBUTTONUP: {
|
|
// Not held: the press went to another window, or losing the
|
|
// capture has released it already.
|
|
if (!window->mouseLeftHeld) break;
|
|
window->mouseLeftHeld = false;
|
|
if (!window->mouseRightHeld && GetCapture() == hwnd) ReleaseCapture();
|
|
window->onMouseLeftRelease.Invoke();
|
|
break;
|
|
}
|
|
|
|
case WM_RBUTTONDOWN: {
|
|
SetCapture(hwnd);
|
|
window->mouseRightHeld = true;
|
|
window->onMouseRightClick.Invoke();
|
|
break;
|
|
}
|
|
|
|
case WM_RBUTTONUP: {
|
|
if (!window->mouseRightHeld) break;
|
|
window->mouseRightHeld = false;
|
|
if (!window->mouseLeftHeld && GetCapture() == hwnd) ReleaseCapture();
|
|
window->onMouseRightRelease.Invoke();
|
|
break;
|
|
}
|
|
|
|
case WM_CAPTURECHANGED: {
|
|
// The capture was taken mid-drag (another window took it, the
|
|
// window lost activation), so the release will not come here:
|
|
// release what is still held. The ReleaseCapture above lands
|
|
// here with nothing held.
|
|
if (reinterpret_cast<HWND>(lParam) == hwnd) break;
|
|
if (window->mouseLeftHeld) {
|
|
window->mouseLeftHeld = false;
|
|
window->onMouseLeftRelease.Invoke();
|
|
window = WindowOf(hwnd);
|
|
if (window == nullptr) break;
|
|
}
|
|
if (window->mouseRightHeld) {
|
|
window->mouseRightHeld = false;
|
|
window->onMouseRightRelease.Invoke();
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WM_MOUSEWHEEL: {
|
|
// High word of wParam is the signed wheel movement, WHEEL_DELTA
|
|
// (120) per detent, positive = wheel away from the user (scroll
|
|
// up). Window::onMouseScroll speaks detents with +1 = wheel down
|
|
// (the DOM deltaY / Wayland axis sign), so negate. Free-spinning
|
|
// wheels report sub-WHEEL_DELTA steps, which PointerScroll adds
|
|
// up instead of each truncating to zero.
|
|
window->PointerScroll(-GET_WHEEL_DELTA_WPARAM(wParam) / static_cast<double>(WHEEL_DELTA));
|
|
break;
|
|
}
|
|
|
|
case WM_SETCURSOR: {
|
|
if (LOWORD(lParam) == HTCLIENT && window->cursorHandle) {
|
|
SetCursor(window->cursorHandle);
|
|
return TRUE;
|
|
}
|
|
break;
|
|
}
|
|
|
|
default: return DefWindowProcW(hwnd, msg, wParam, lParam);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
Window::Window(std::uint32_t width, std::uint32_t height, const std::string_view title) : Window(width, height) {
|
|
SetTitle(title);
|
|
}
|
|
|
|
// Delegates to the default constructor, which makes nothing, so that the
|
|
// window is a complete object before the first step below. When a step
|
|
// throws (a Vulkan error, a surface that offers no storage-writable format),
|
|
// ~Window then runs before the exception leaves the constructor
|
|
// ([except.ctor]) and releases what had been made by then, as it does for a
|
|
// window made whole: a member not made by then is null or empty, and
|
|
// releases nothing. Without that, Device::windows, the HWND's GWLP_USERDATA
|
|
// and the proxies' listener data went on pointing at the window after the
|
|
// exception (#169). A Vulkan command that fails leaves its outputs without a
|
|
// defined value, so each handle goes into its member only once the call that
|
|
// makes it has succeeded, the command buffers excepted: an allocation that
|
|
// fails sets them to null.
|
|
Window::Window(std::uint32_t width, std::uint32_t height) : Window() {
|
|
this->width = width;
|
|
this->height = height;
|
|
// Before the platform code below, which may report the pointer.
|
|
lastMousePos = {0,0};
|
|
mouseDelta = {0,0};
|
|
currentMousePos = {0,0};
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
Device::windows.push_back(this);
|
|
surface = wl_compositor_create_surface(Device::compositor);
|
|
xdgSurface = xdg_wm_base_get_xdg_surface(Device::xdgWmBase, surface);
|
|
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
|
|
|
|
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
|
|
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
|
|
wl_surface_commit(surface);
|
|
|
|
// The fractional scale, the decoration and the viewport come from
|
|
// optional protocols: Device::handle_global binds a global only when the
|
|
// compositor advertises it, and a request on the null proxy it leaves
|
|
// otherwise crashes in libwayland (#162). The preferred scale is applied
|
|
// through the viewport, which shows the width x height physical pixels of
|
|
// the buffer at width/scale x height/scale, so without a viewporter there
|
|
// is no fractional scale either: scale stays 1, the surface is as large as
|
|
// its buffer, and the compositor scales it up on a HiDPI output. The
|
|
// scale the compositor asks for while the loop below waits is taken by
|
|
// MatchSizesToScale before the first swapchain; one it asks for later,
|
|
// by a frame boundary of Render() (xdg_surface_handle_preferred_scale).
|
|
if (Device::fractionalScaleManager != nullptr && Device::wpViewporter != nullptr) {
|
|
wp_scale = wp_fractional_scale_manager_v1_get_fractional_scale(Device::fractionalScaleManager, surface);
|
|
wp_fractional_scale_v1_add_listener(wp_scale, &wp_fractional_scale_v1_listener, this);
|
|
}
|
|
|
|
while (wl_display_dispatch(Device::display) != -1 && !configured) {}
|
|
|
|
wl_surface_commit(surface);
|
|
|
|
// GNOME's Mutter has no xdg-decoration: it leaves decorations to the
|
|
// client, and the library draws none, so there the window has no title
|
|
// bar.
|
|
if (Device::manager != nullptr) {
|
|
decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(Device::manager, xdgToplevel);
|
|
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
|
}
|
|
|
|
// The viewport's destination is not set here: it is the size the surface
|
|
// takes with a buffer, so the window's first frame brings it
|
|
// (SendFrameSurfaceState). What it is to be is settled now, from the
|
|
// constructor's size in pixels and the scale the compositor has named so
|
|
// far, as it used to be sent from here.
|
|
if (Device::wpViewporter != nullptr) {
|
|
wpViewport = wp_viewporter_get_viewport(Device::wpViewporter, surface);
|
|
}
|
|
MatchSizesToScale(true);
|
|
|
|
wl_surface_commit(surface);
|
|
#endif
|
|
|
|
VkSurfaceKHR madeSurface = VK_NULL_HANDLE;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
// A Unicode class, so the text in WndProc's messages is UTF-16: WM_CHAR
|
|
// brings UTF-16 code units, and SetTitle's WM_SETTEXT reaches
|
|
// DefWindowProcW as it was sent. A class from RegisterClassA gets its
|
|
// text in the process code page, CP1252 on a Western system, where a
|
|
// typed € would arrive as 0x80 and a 日, typed or in a title, as ?. The
|
|
// pumps use PeekMessageW and DispatchMessageW too: PeekMessageA hands
|
|
// WM_CHAR over in the code page whatever the window's class.
|
|
WNDCLASSW wc = {0};
|
|
wc.lpfnWndProc = WndProc; // Set window procedure
|
|
wc.hInstance = GetModuleHandle(NULL); // Get instance handle
|
|
wc.lpszClassName = g_szClassName;
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
// The first window registers the class; every later one finds it there.
|
|
if (!RegisterClassW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
|
|
MessageBox(NULL, "Window Class Registration Failed!", "Error", MB_ICONERROR);
|
|
}
|
|
|
|
RECT rc = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
|
|
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
|
|
|
|
hwnd = CreateWindowExW(
|
|
0,
|
|
g_szClassName,
|
|
L"",
|
|
WS_OVERLAPPEDWINDOW,
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
rc.right - rc.left,
|
|
rc.bottom - rc.top,
|
|
NULL, NULL, wc.hInstance, this
|
|
);
|
|
|
|
|
|
if (hwnd == NULL) {
|
|
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONERROR);
|
|
}
|
|
|
|
// Show the window
|
|
ShowWindow(hwnd, SW_SHOWNORMAL);
|
|
UpdateWindow(hwnd);
|
|
|
|
// Input stays queued for StartSync. Dispatched here, it would reach the
|
|
// window before anyone could listen: the WM_MOUSEMOVE Windows sends
|
|
// when a window appears under the cursor would make an onMouseEnter
|
|
// nobody hears, and later an onMouseLeave without one.
|
|
MSG msg;
|
|
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE | PM_QS_PAINT | PM_QS_SENDMESSAGE)) {
|
|
TranslateMessage(&msg);
|
|
DispatchMessageW(&msg);
|
|
}
|
|
|
|
VkWin32SurfaceCreateInfoKHR createInfo = {};
|
|
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
|
|
createInfo.hinstance = wc.hInstance;
|
|
createInfo.hwnd = hwnd;
|
|
Device::CheckVkResult(vkCreateWin32SurfaceKHR(Device::instance, &createInfo, NULL, &madeSurface));
|
|
#endif
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
VkWaylandSurfaceCreateInfoKHR createInfo = {};
|
|
createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
|
|
createInfo.display = Device::display;
|
|
createInfo.surface = surface;
|
|
Device::CheckVkResult(vkCreateWaylandSurfaceKHR(Device::instance, &createInfo, NULL, &madeSurface));
|
|
#endif
|
|
vulkanSurface = madeSurface;
|
|
|
|
// Get list of supported surface formats
|
|
std::uint32_t formatCount;
|
|
Device::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(Device::physDevice, vulkanSurface, &formatCount, NULL));
|
|
assert(formatCount > 0);
|
|
|
|
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
|
|
Device::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(Device::physDevice, vulkanSurface, &formatCount, surfaceFormats.data()));
|
|
|
|
// The swapchain image is a storage image that compute and ray tracing
|
|
// shaders write, and the UI kernels also read, without a format. From
|
|
// Vulkan 1.3 that is up to each format
|
|
// (VK_FORMAT_FEATURE_2_STORAGE_READ/WRITE_WITHOUT_FORMAT_BIT), not a device
|
|
// feature. Take the first surface format that allows both, R8G8B8A8_UNORM
|
|
// or B8G8R8A8_UNORM before others; failing that, one that allows writing.
|
|
const auto formatFeatures = [](VkFormat format) {
|
|
VkFormatProperties3 properties3 { .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3 };
|
|
VkFormatProperties2 properties2 { .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &properties3 };
|
|
vkGetPhysicalDeviceFormatProperties2(Device::physDevice, format, &properties2);
|
|
return properties3.optimalTilingFeatures;
|
|
};
|
|
constexpr VkFormatFeatureFlags2 writable = VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
|
|
constexpr VkFormatFeatureFlags2 readWritable = writable | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
|
|
const VkSurfaceFormatKHR* selectedFormat = nullptr;
|
|
int selectedScore = -1;
|
|
for (const VkSurfaceFormatKHR& available : surfaceFormats) {
|
|
const VkFormatFeatureFlags2 features = formatFeatures(available.format);
|
|
if ((features & writable) != writable) {
|
|
continue;
|
|
}
|
|
const bool preferred = available.format == VK_FORMAT_R8G8B8A8_UNORM || available.format == VK_FORMAT_B8G8R8A8_UNORM;
|
|
const int score = ((features & readWritable) == readWritable ? 2 : 0) + (preferred ? 1 : 0);
|
|
if (score > selectedScore) {
|
|
selectedFormat = &available;
|
|
selectedScore = score;
|
|
}
|
|
}
|
|
if (selectedFormat == nullptr) {
|
|
throw std::runtime_error("Crafter.Graphics: no format this window's surface offers can be written as a storage image "
|
|
"without a format (VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT), which drawing into "
|
|
"the swapchain needs");
|
|
}
|
|
if (selectedScore < 2) {
|
|
std::println(stderr, "Crafter.Graphics: the swapchain format {} can't be read as a storage image without a format, "
|
|
"which the UI kernels do", string_VkFormat(selectedFormat->format));
|
|
}
|
|
|
|
colorFormat = selectedFormat->format;
|
|
colorSpace = selectedFormat->colorSpace;
|
|
|
|
// Select a supported composite alpha format (not all devices support alpha
|
|
// opaque). This is a surface property, so pick it once here rather than on
|
|
// every CreateSwapchain. Simply select the first available.
|
|
{
|
|
VkSurfaceCapabilitiesKHR surfCaps;
|
|
Device::CheckVkResult(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(Device::physDevice, vulkanSurface, &surfCaps));
|
|
const VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[] = {
|
|
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
|
};
|
|
for (const VkCompositeAlphaFlagBitsKHR compositeAlphaFlag : compositeAlphaFlags) {
|
|
if (surfCaps.supportedCompositeAlpha & compositeAlphaFlag) {
|
|
compositeAlpha = compositeAlphaFlag;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
CreateSwapchain();
|
|
|
|
VkCommandBufferAllocateInfo cmdBufAllocateInfo {};
|
|
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
cmdBufAllocateInfo.commandPool = Device::commandPool;
|
|
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
cmdBufAllocateInfo.commandBufferCount = numFrames;
|
|
// Straight into the member: an allocation that fails frees what it had
|
|
// made and sets every entry to null.
|
|
Device::CheckVkResult(vkAllocateCommandBuffers(Device::device, &cmdBufAllocateInfo, drawCmdBuffers));
|
|
|
|
// Per-frame-in-flight synchronisation objects (issue #40). The fences are
|
|
// created signaled so the first wait on each frame slot's fence — before
|
|
// that slot has ever been submitted — returns immediately instead of
|
|
// deadlocking. The submitInfo is rebuilt per frame in Render() now that
|
|
// the wait/signal semaphores vary by frame. The per-image render-finished
|
|
// semaphores come with the images, in CreateSwapchain.
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo {};
|
|
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
VkFenceCreateInfo fenceCreateInfo {};
|
|
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
|
for (std::uint8_t i = 0; i < numFrames; i++) {
|
|
VkFence fence = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateFence(Device::device, &fenceCreateInfo, nullptr, &fence));
|
|
waitFences[i] = fence;
|
|
VkSemaphore semaphore = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateSemaphore(Device::device, &semaphoreCreateInfo, nullptr, &semaphore));
|
|
imageAcquiredSemaphores[i] = semaphore;
|
|
}
|
|
|
|
// The deferred-deletion queue (issue #101) retires a resource after this
|
|
// many recorded frames — exactly the depth the per-frame fences (#40)
|
|
// allow in flight. Set it here, where numFrames is in scope.
|
|
Device::framesInFlight = numFrames;
|
|
|
|
// Per-frame info structs: everything that never varies between frames is
|
|
// set here once. Render() patches the barriers' image/oldLayout and the
|
|
// sync handles that follow currentFrame/currentImage (submitInfo's
|
|
// wait/signal semaphores + command buffer, presentInfo's wait semaphore).
|
|
cmdBufInfo = {};
|
|
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
// Submit: stage mask + the 1/1/1 counts are invariant; the wait/signal
|
|
// semaphore handles and the command buffer are patched per frame.
|
|
submitInfo = {};
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
submitInfo.pNext = VK_NULL_HANDLE;
|
|
submitInfo.pWaitDstStageMask = &submitPipelineStages;
|
|
submitInfo.waitSemaphoreCount = 1;
|
|
submitInfo.signalSemaphoreCount = 1;
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
const VkImageSubresourceRange range {
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
.baseMipLevel = 0,
|
|
.levelCount = VK_REMAINING_MIP_LEVELS,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = VK_REMAINING_ARRAY_LAYERS,
|
|
};
|
|
|
|
// Transition into VK_IMAGE_LAYOUT_GENERAL before recording passes. image,
|
|
// oldLayout (depends on first use) and dstAccessMask (derived from the
|
|
// per-pass stage union) are patched per frame in Render(); the dstAccessMask
|
|
// here is just a well-formed default.
|
|
acquireBarrier = {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
.srcAccessMask = 0,
|
|
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
|
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.image = VK_NULL_HANDLE,
|
|
.subresourceRange = range,
|
|
};
|
|
|
|
// Transition back to PRESENT_SRC_KHR after the passes. image and
|
|
// srcAccessMask (derived from the per-pass stage union) are patched per
|
|
// frame in Render(); the srcAccessMask here is just a well-formed default.
|
|
presentBarrier = {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
.dstAccessMask = 0,
|
|
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.image = VK_NULL_HANDLE,
|
|
.subresourceRange = range,
|
|
};
|
|
|
|
// pImageIndices/pSwapchains point at members, so they follow value changes
|
|
// (currentImage each frame, swapChain across recreation) on their own. The
|
|
// wait semaphore is the per-image render-finished semaphore, patched per
|
|
// frame in Render() (it follows currentImage).
|
|
presentInfo = {};
|
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
presentInfo.pNext = NULL;
|
|
presentInfo.swapchainCount = 1;
|
|
presentInfo.pSwapchains = &swapChain;
|
|
presentInfo.pImageIndices = ¤tImage;
|
|
presentInfo.waitSemaphoreCount = 1;
|
|
}
|
|
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
Window::~Window() {
|
|
// Nothing may still run against the semaphores, fences or images below.
|
|
if (Device::device != VK_NULL_HANDLE) vkDeviceWaitIdle(Device::device);
|
|
if (Device::device != VK_NULL_HANDLE) {
|
|
if (drawCmdBuffers[0] != VK_NULL_HANDLE) vkFreeCommandBuffers(Device::device, Device::commandPool, numFrames, drawCmdBuffers);
|
|
for (std::uint8_t i = 0; i < numFrames; i++) {
|
|
if (waitFences[i]) vkDestroyFence(Device::device, waitFences[i], nullptr);
|
|
if (imageAcquiredSemaphores[i]) vkDestroySemaphore(Device::device, imageAcquiredSemaphores[i], nullptr);
|
|
}
|
|
for (VkSemaphore semaphore : renderFinishedSemaphores) {
|
|
vkDestroySemaphore(Device::device, semaphore, nullptr);
|
|
}
|
|
renderFinishedSemaphores.clear();
|
|
for (VkImageView view : imageViews) {
|
|
vkDestroyImageView(Device::device, view, nullptr);
|
|
}
|
|
imageViews.clear();
|
|
if (swapChain != VK_NULL_HANDLE) vkDestroySwapchainKHR(Device::device, swapChain, nullptr);
|
|
swapChain = VK_NULL_HANDLE;
|
|
}
|
|
if (vulkanSurface != VK_NULL_HANDLE) vkDestroySurfaceKHR(Device::instance, vulkanSurface, nullptr);
|
|
vulkanSurface = VK_NULL_HANDLE;
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
SetDefaultCursor();
|
|
// A pending frame callback would still dispatch into this window;
|
|
// destroying the proxy makes libwayland drop its event.
|
|
if (cb) wl_callback_destroy(cb);
|
|
cb = nullptr;
|
|
if (wpViewport) wp_viewport_destroy(wpViewport);
|
|
if (wp_scale) {
|
|
wp_fractional_scale_v1_destroy(wp_scale);
|
|
wp_scale = nullptr;
|
|
}
|
|
if (decoration) zxdg_toplevel_decoration_v1_destroy(decoration);
|
|
if (xdgToplevel) xdg_toplevel_destroy(xdgToplevel);
|
|
if (xdgSurface) xdg_surface_destroy(xdgSurface);
|
|
if (surface) wl_surface_destroy(surface);
|
|
wpViewport = nullptr;
|
|
decoration = nullptr;
|
|
xdgToplevel = nullptr;
|
|
xdgSurface = nullptr;
|
|
surface = nullptr;
|
|
if (Device::display) wl_display_flush(Device::display);
|
|
std::erase(Device::windows, this);
|
|
if (Device::focusedWindow == this) Device::focusedWindow = nullptr;
|
|
if (Device::keyboardWindow == this) Device::keyboardWindow = nullptr;
|
|
if (Device::touchWindow == this) {
|
|
Device::touchWindow = nullptr;
|
|
Device::ForgetTouches();
|
|
}
|
|
if (Device::textInputWindow == this) Device::textInputWindow = nullptr;
|
|
Device::ForgetTextInputDones(this);
|
|
#endif
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
// Detached first: what DestroyWindow sends (WM_CAPTURECHANGED,
|
|
// WM_DESTROY) and anything still queued for the HWND must not reach
|
|
// this Window.
|
|
if (hwnd != nullptr) {
|
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
|
|
DestroyWindow(hwnd);
|
|
hwnd = nullptr;
|
|
}
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
void Window::PointerEnter(Vector<float, 2> position) {
|
|
currentMousePos = position;
|
|
if (mouseInside) return;
|
|
mouseInside = true;
|
|
onMouseEnter.Invoke();
|
|
}
|
|
|
|
void Window::PointerMove(Vector<float, 2> position) {
|
|
currentMousePos = position;
|
|
onMouseMove.Invoke();
|
|
}
|
|
|
|
void Window::PointerLeave() {
|
|
// A partial detent from before must not count towards the next scroll,
|
|
// after a leave without an enter too.
|
|
scrollRemainder = 0.0;
|
|
if (!mouseInside) return;
|
|
mouseInside = false;
|
|
onMouseLeave.Invoke();
|
|
}
|
|
|
|
void Window::PointerScroll(double detents) {
|
|
scrollRemainder += detents;
|
|
const double whole = std::trunc(scrollRemainder);
|
|
if (whole == 0.0) return;
|
|
scrollRemainder -= whole;
|
|
// Event payload is uint32 — preserve sign via two's complement.
|
|
onMouseScroll.Invoke(static_cast<std::uint32_t>(static_cast<std::int32_t>(whole)));
|
|
}
|
|
|
|
void Window::Resize(std::uint32_t newWidth, std::uint32_t newHeight) {
|
|
// Skip degenerate resizes: Win32 minimised windows give (0, 0). A
|
|
// Wayland configure does not come through here: its size waits for the
|
|
// next Render() (xdg_surface_handle_configure).
|
|
if (newWidth == 0 || newHeight == 0) return;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The last size asked for wins, whoever asked. A size the compositor
|
|
// configured that still waits for its frame gives way to this later
|
|
// one, the size the window has included, as it did when the configure's
|
|
// listener resized on the spot and this call came after it. The
|
|
// configure's serial goes on waiting for a frame.
|
|
deferredLogicalWidth = 0;
|
|
deferredLogicalHeight = 0;
|
|
#endif
|
|
// A listener of the frame being recorded asked: the rest of that frame
|
|
// still draws into an image of the current swapchain at the current
|
|
// size, so the size changes once the frame has been presented. The last
|
|
// size asked for wins, the current one too.
|
|
if (recording) {
|
|
deferredWidth = newWidth;
|
|
deferredHeight = newHeight;
|
|
return;
|
|
}
|
|
if (newWidth == width && newHeight == height) return;
|
|
// Win32 fires WM_SIZE synchronously inside CreateWindowEx (before the
|
|
// constructor's CreateSwapchain). Defer the first resize to that
|
|
// CreateSwapchain call instead of trying to recreate a non-existent
|
|
// swapchain.
|
|
if (swapChain == VK_NULL_HANDLE) {
|
|
width = newWidth;
|
|
height = newHeight;
|
|
return;
|
|
}
|
|
|
|
width = newWidth;
|
|
height = newHeight;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The application's size, in pixels: the surface's follows from it.
|
|
logicalConfigured = false;
|
|
#endif
|
|
RebuildSwapchain();
|
|
}
|
|
|
|
void Window::RebuildSwapchain() {
|
|
// Pending from here until the swapchain is made, whoever called: a
|
|
// rebuild that throws below is then tried again by the next Render().
|
|
swapchainRebuildPending = true;
|
|
// WM_SIZE and a Resize or SetPresentMode from outside a frame run
|
|
// between frames, and what a frame or a configure asked for waited for a
|
|
// frame boundary (RebuildIfDeferred), but frames may still be in flight:
|
|
// none may still use the old swapchain.
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
// The queue is idle, so every deferred resource is now safe to free
|
|
// regardless of its retire frame (issue #101).
|
|
Device::DrainDeletions();
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The buffers' size and the surface's, at the scale the window draws
|
|
// at, which takes one its listener kept only where it gives the window
|
|
// new pixels (MatchSizesToScale). The surface itself is told nothing
|
|
// here. Its size is the viewport's destination, which the next commit
|
|
// applies whatever buffer is attached then: sent now, the old frame
|
|
// would be shown scaled to the new size until the first new one (#199).
|
|
// SendFrameSurfaceState sends it with that frame.
|
|
MatchSizesToScale(false);
|
|
#endif
|
|
RecreateSwapchainAndImages();
|
|
// Made, so nothing waits. Cleared only here: a rebuild that threw above
|
|
// still waits, and the next Render() tries it again instead of drawing
|
|
// into a swapchain whose views CreateSwapchain has already destroyed.
|
|
// And before the listeners, which may render, or ask for another.
|
|
swapchainRebuildPending = false;
|
|
onResize.Invoke();
|
|
}
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
namespace {
|
|
// The scale a preferred_scale of `preferred` 120ths comes to, as `scale`
|
|
// holds it. MatchSizesToScale is the one place that stores it, so a
|
|
// scale the window draws at compares equal to this, exactly.
|
|
float ScaleOf(std::uint32_t preferred) {
|
|
return preferred / 120.0f;
|
|
}
|
|
|
|
// The pixels a surface-local length takes at `scale`, as a size the
|
|
// compositor configured becomes the swapchain's. Rounded up, in float,
|
|
// where fractional-scale-v1 rounds half away from zero: that much of
|
|
// #201 is as it was.
|
|
std::uint32_t ScaledPixels(std::int32_t logical, float scale) {
|
|
return static_cast<std::uint32_t>(std::ceil(logical * scale));
|
|
}
|
|
|
|
// The other way: the surface-local length `pixels` come to at `scale`,
|
|
// for a window the application sized.
|
|
std::int32_t SurfaceLength(std::uint32_t pixels, float scale) {
|
|
return static_cast<std::int32_t>(std::ceil(pixels / scale));
|
|
}
|
|
}
|
|
|
|
void Window::MatchSizesToScale(bool takeScale) {
|
|
// The scale the compositor asked for last becomes the one the window
|
|
// draws at, and converts the pointer, the touches and the caret with,
|
|
// at the start of a frame and in the constructor (takeScale), and
|
|
// elsewhere only where it gives the window new pixels: a window the
|
|
// compositor sized whose surface comes to other pixels at it, with no
|
|
// configured size waiting, which the next frame's start takes together
|
|
// with the scale. The swapchain made at those pixels is the one the
|
|
// application lays its next frame out for, from onResize. Anywhere else
|
|
// the scale would change only the conversions and the size the surface
|
|
// is to take, which goes out with the next frame, while until that
|
|
// frame's commit the compositor maps the surface, and the pointer and
|
|
// the touches on it, by the destination it has: so a swapchain made for
|
|
// a present mode, for a rebuild that waited or for a Resize of a window
|
|
// the application sized, whose pixels are its own, leaves the scale for
|
|
// the start of that frame.
|
|
const float before = scale;
|
|
const float newest = ScaleOf(preferredScale);
|
|
const bool newPixels = logicalConfigured && deferredLogicalWidth == 0
|
|
&& (ScaledPixels(logicalWidth, newest) != width || ScaledPixels(logicalHeight, newest) != height);
|
|
if (takeScale || newPixels) scale = newest;
|
|
if (logicalConfigured) {
|
|
width = ScaledPixels(logicalWidth, scale);
|
|
height = ScaledPixels(logicalHeight, scale);
|
|
} else {
|
|
logicalWidth = SurfaceLength(width, scale);
|
|
logicalHeight = SurfaceLength(height, scale);
|
|
}
|
|
// What the window holds in pixels of the scale before goes to the new
|
|
// one. The compositor gives the pointer and the fingers in surface-local
|
|
// units and sends nothing for one that rests, so a position kept in the
|
|
// old pixels would be off by new/old in the frames drawn at the new
|
|
// scale until it moved (#165); lastMousePos and mouseDelta go with
|
|
// currentMousePos, so that the next delta is no jump; surfaceMousePos,
|
|
// the compositor's, which Input::Map takes its delta from, stays. The
|
|
// caret the
|
|
// application gave SetTextInput in pixels goes to the input method
|
|
// again, converted with the new scale, as it does when the keyboard
|
|
// focus comes back. In the constructor, which takes the first scale,
|
|
// the window holds none of these yet.
|
|
if (scale != before && before > 0.0f) {
|
|
const float ratio = scale / before;
|
|
currentMousePos = currentMousePos * ratio;
|
|
lastMousePos = lastMousePos * ratio;
|
|
mouseDelta = mouseDelta * ratio;
|
|
for (Device::TouchSlot& slot : Device::touchSlots) {
|
|
if (slot.used && slot.window == this) {
|
|
slot.x *= ratio;
|
|
slot.y *= ratio;
|
|
}
|
|
}
|
|
if (textInputActive_) Device::ApplyTextInput(this);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
bool Window::TakeDeferredSize(FrameBoundary boundary) {
|
|
const std::uint32_t widthBefore = width;
|
|
const std::uint32_t heightBefore = height;
|
|
if (deferredWidth != 0) {
|
|
width = deferredWidth;
|
|
height = deferredHeight;
|
|
deferredWidth = 0;
|
|
deferredHeight = 0;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The application's size, in pixels: the surface's follows from it
|
|
// at the rebuild. Asking for the size the window has changes
|
|
// nothing, as in Resize.
|
|
if (width != widthBefore || height != heightBefore) logicalConfigured = false;
|
|
#endif
|
|
}
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The compositor's size, when that is what waits (Resize and the
|
|
// configure's listener each clear the other's): the surface's size from
|
|
// here on, unchanged, and the pixels for it at the scale the compositor
|
|
// asked for last. Those may be the pixels the swapchain has, where the
|
|
// scale changed with the size. Then nothing is rebuilt, and the frame
|
|
// still brings the surface its new size. And a scale the compositor
|
|
// asked for with no size, which its listener only kept (#165): taken
|
|
// here as a configured size is, so the window draws at it from the next
|
|
// frame it records, and the pointer, the touches and the caret are
|
|
// converted with it from here. A window the compositor sized keeps the
|
|
// surface's size and takes new pixels for it, which the caller makes
|
|
// the swapchain at. One the application sized keeps its pixels, and its
|
|
// surface takes the size they come to at the new scale, which the next
|
|
// frame sends (SendFrameSurfaceState).
|
|
const float newest = ScaleOf(preferredScale);
|
|
const bool sized = deferredLogicalWidth != 0;
|
|
const bool rescaled = newest != scale;
|
|
if (sized || rescaled) {
|
|
// After a present, a new scale is taken only where it gives the
|
|
// window new pixels (see MatchSizesToScale), and a configured size
|
|
// that comes with it to the pixels the window has waits with it for
|
|
// the start of the next frame, the one that sends the surface that
|
|
// size: taken here, the scale would convert the pointer and the
|
|
// touches for a frame not drawn yet, while the compositor maps them
|
|
// by the destination it has and the application hit-tests in the
|
|
// layout of the frame on screen, and no onResize would say so.
|
|
const bool configured = sized || logicalConfigured;
|
|
const std::int32_t surfaceWidth = sized ? deferredLogicalWidth : logicalWidth;
|
|
const std::int32_t surfaceHeight = sized ? deferredLogicalHeight : logicalHeight;
|
|
const bool newPixels = configured && (ScaledPixels(surfaceWidth, newest) != width || ScaledPixels(surfaceHeight, newest) != height);
|
|
if (boundary == FrameBoundary::Start || !rescaled || newPixels) {
|
|
if (sized) {
|
|
logicalWidth = deferredLogicalWidth;
|
|
logicalHeight = deferredLogicalHeight;
|
|
logicalConfigured = true;
|
|
deferredLogicalWidth = 0;
|
|
deferredLogicalHeight = 0;
|
|
}
|
|
MatchSizesToScale(boundary == FrameBoundary::Start);
|
|
}
|
|
}
|
|
#endif
|
|
return width != widthBefore || height != heightBefore;
|
|
}
|
|
|
|
void Window::RebuildIfDeferred(FrameBoundary boundary) {
|
|
// The sizes are taken before the rebuild, so an onResize listener that
|
|
// renders finds none waiting.
|
|
if (TakeDeferredSize(boundary) || swapchainRebuildPending) RebuildSwapchain();
|
|
}
|
|
|
|
void Window::RecreateSwapchainAndImages() {
|
|
// CreateSwapchain leaves the new swapchain images in
|
|
// VK_IMAGE_LAYOUT_UNDEFINED and resets imageInitialised. The images are
|
|
// NOT transitioned here: a presentable image may only be touched after
|
|
// vkAcquireNextImageKHR returns it, so Render() performs each image's
|
|
// first layout transition lazily (from UNDEFINED) once it has been
|
|
// acquired. Pre-transitioning unacquired images is a validation error.
|
|
CreateSwapchain();
|
|
}
|
|
|
|
void Window::SetPresentMode(PresentMode mode) {
|
|
presentMode = mode;
|
|
// A window without a swapchain yet picks the mode up when it makes one.
|
|
if (swapChain == VK_NULL_HANDLE) return;
|
|
// The mode is fixed when a swapchain is made, so make a new one, as
|
|
// Resize does: after the present when a frame is being recorded (a
|
|
// VSync key ticked from onUpdate), since the rest of that frame draws
|
|
// into the old swapchain's image.
|
|
if (recording) {
|
|
swapchainRebuildPending = true;
|
|
return;
|
|
}
|
|
RebuildSwapchain();
|
|
}
|
|
|
|
void Window::SetTitle(const std::string_view title) {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// A copy: set_title reads up to a NUL, which a view need not have, and
|
|
// an empty view's null data() is a fatal error on the display.
|
|
xdg_toplevel_set_title(xdgToplevel, std::string(title).c_str());
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
SetWindowTextW(hwnd, Utf16FromUtf8(title).c_str());
|
|
#endif
|
|
}
|
|
|
|
void Window::SetAppId(const std::string_view id) {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
xdg_toplevel_set_app_id(xdgToplevel, std::string(id).c_str());
|
|
#endif
|
|
}
|
|
|
|
void Window::SetTextInput(bool active, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, TextInputPurpose purpose, std::optional<std::uint32_t> hints) {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
textInputActive_ = active;
|
|
textInputRect_[0] = x;
|
|
textInputRect_[1] = y;
|
|
textInputRect_[2] = width;
|
|
textInputRect_[3] = height;
|
|
textInputPurpose_ = purpose;
|
|
// Only version 1's hints: a later one's is not a value of the enum the
|
|
// bound object knows.
|
|
textInputHints_ = hints.value_or(TextInputDefaultHints(purpose)) & kTextInputHintMask;
|
|
Device::ApplyTextInput(this);
|
|
#endif
|
|
}
|
|
|
|
void Window::SetCursorImage(std::uint16_t width, std::uint16_t height,
|
|
std::uint16_t hotspotX, std::uint16_t hotspotY,
|
|
const std::uint8_t* pixels) {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
if (width == 0 || height == 0 || pixels == nullptr) {
|
|
SetDefaultCursor();
|
|
return;
|
|
}
|
|
|
|
if (cursorSurface == nullptr) {
|
|
cursorSurface = wl_compositor_create_surface(Device::compositor);
|
|
}
|
|
|
|
int stride = width * 4;
|
|
int size = stride * height;
|
|
|
|
// Reuse the existing mmap+buffer if the size is unchanged; otherwise
|
|
// tear down and re-allocate.
|
|
if (cursorWlBuffer != nullptr &&
|
|
cursorBufferOldSize == static_cast<std::uint32_t>(size)) {
|
|
// size unchanged — keep the buffer and mmap.
|
|
} else {
|
|
if (cursorMmap_) {
|
|
munmap(cursorMmap_, cursorBufferOldSize);
|
|
cursorMmap_ = nullptr;
|
|
}
|
|
if (cursorWlBuffer) {
|
|
wl_buffer_destroy(cursorWlBuffer);
|
|
cursorWlBuffer = nullptr;
|
|
}
|
|
|
|
int fd = create_shm_file(size);
|
|
if (fd < 0) {
|
|
throw std::runtime_error(std::format(
|
|
"Window::SetCursorImage: shm allocation for {}B failed", size));
|
|
}
|
|
void* mapped = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
if (mapped == MAP_FAILED) {
|
|
close(fd);
|
|
throw std::runtime_error("Window::SetCursorImage: mmap failed");
|
|
}
|
|
cursorMmap_ = static_cast<std::uint8_t*>(mapped);
|
|
|
|
wl_shm_pool* pool = wl_shm_create_pool(Device::shm, fd, size);
|
|
cursorWlBuffer = wl_shm_pool_create_buffer(
|
|
pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
|
|
wl_shm_pool_destroy(pool);
|
|
close(fd);
|
|
|
|
cursorBufferOldSize = static_cast<std::uint32_t>(size);
|
|
}
|
|
|
|
// Convert the user's straight-alpha RGBA8 pixels into the compositor's
|
|
// expected premultiplied BGRA8 (= ARGB8888 little-endian byte order).
|
|
for (int i = 0; i < width * height; ++i) {
|
|
std::uint8_t r = pixels[i * 4 + 0];
|
|
std::uint8_t g = pixels[i * 4 + 1];
|
|
std::uint8_t b = pixels[i * 4 + 2];
|
|
std::uint8_t a = pixels[i * 4 + 3];
|
|
cursorMmap_[i * 4 + 0] = static_cast<std::uint8_t>((b * a) / 255);
|
|
cursorMmap_[i * 4 + 1] = static_cast<std::uint8_t>((g * a) / 255);
|
|
cursorMmap_[i * 4 + 2] = static_cast<std::uint8_t>((r * a) / 255);
|
|
cursorMmap_[i * 4 + 3] = a;
|
|
}
|
|
|
|
cursorHotspotX_ = hotspotX;
|
|
cursorHotspotY_ = hotspotY;
|
|
|
|
wl_surface_attach(cursorSurface, cursorWlBuffer, 0, 0);
|
|
wl_surface_damage(cursorSurface, 0, 0, width, height);
|
|
wl_surface_commit(cursorSurface);
|
|
|
|
// If the pointer is currently inside our window, re-apply the cursor
|
|
// so the new hotspot takes effect immediately. Otherwise the next
|
|
// pointer-enter event will pick it up.
|
|
if (Device::wlPointer && Device::focusedWindow == this && lastPointerSerial_) {
|
|
wl_pointer_set_cursor(Device::wlPointer, lastPointerSerial_,
|
|
cursorSurface, hotspotX, hotspotY);
|
|
}
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
// Win32 cursor support is not implemented for the v2 Window.
|
|
(void)width; (void)height; (void)hotspotX; (void)hotspotY; (void)pixels;
|
|
#endif
|
|
}
|
|
|
|
void Window::SetDefaultCursor() {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
if (cursorMmap_) {
|
|
munmap(cursorMmap_, cursorBufferOldSize);
|
|
cursorMmap_ = nullptr;
|
|
}
|
|
if (cursorWlBuffer) {
|
|
wl_buffer_destroy(cursorWlBuffer);
|
|
cursorWlBuffer = nullptr;
|
|
}
|
|
if (cursorSurface) {
|
|
wl_surface_destroy(cursorSurface);
|
|
cursorSurface = nullptr;
|
|
}
|
|
cursorBufferOldSize = 0;
|
|
cursorHotspotX_ = 0;
|
|
cursorHotspotY_ = 0;
|
|
// Tell the compositor to drop our cursor surface — passing nullptr
|
|
// makes it fall back to the system default.
|
|
if (Device::wlPointer && Device::focusedWindow == this && lastPointerSerial_) {
|
|
wl_pointer_set_cursor(Device::wlPointer, lastPointerSerial_, nullptr, 0, 0);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
namespace {
|
|
// Milliseconds until the first window that updates on its own clock is
|
|
// due, rounded up so the wait doesn't end just short of it; -1 when no
|
|
// window does, so the loop waits for events alone.
|
|
int MillisecondsToNextClockedUpdate() {
|
|
const auto now = std::chrono::high_resolution_clock::now();
|
|
std::optional<std::chrono::high_resolution_clock::duration> soonest;
|
|
for (const Window* window : Device::windows) {
|
|
if (!window->updating || window->updateInterval <= std::chrono::nanoseconds::zero()) continue;
|
|
const auto wait = window->lastFrameBegin + window->updateInterval - now;
|
|
if (!soonest || wait < *soonest) soonest = wait;
|
|
}
|
|
if (!soonest) return -1;
|
|
if (*soonest <= std::chrono::high_resolution_clock::duration::zero()) return 0;
|
|
return static_cast<int>(std::chrono::ceil<std::chrono::milliseconds>(*soonest).count());
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void Window::StartSync() {
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// libwayland's read sequence, as wl_display_dispatch runs it, but the
|
|
// poll also watches the descriptors windows registered with AddPollFd,
|
|
// and times out when a window that updates on its own clock is due.
|
|
std::vector<pollfd> fds;
|
|
std::vector<int> ready;
|
|
bool flushPending = false;
|
|
while (open) {
|
|
while (wl_display_prepare_read(Device::display) != 0) {
|
|
if (wl_display_dispatch_pending(Device::display) == -1) return;
|
|
}
|
|
// A full socket (EAGAIN) is flushed again once poll says it is
|
|
// writable. EPIPE goes on to the read, which reports the
|
|
// compositor's error.
|
|
flushPending = false;
|
|
if (wl_display_flush(Device::display) == -1) {
|
|
if (errno == EAGAIN) {
|
|
flushPending = true;
|
|
} else if (errno != EPIPE) {
|
|
wl_display_cancel_read(Device::display);
|
|
return;
|
|
}
|
|
}
|
|
|
|
fds.clear();
|
|
fds.push_back({ .fd = wl_display_get_fd(Device::display), .events = static_cast<short>(POLLIN | (flushPending ? POLLOUT : 0)), .revents = 0 });
|
|
for (const Window* window : Device::windows) {
|
|
for (const PollFd& entry : window->pollFds) {
|
|
fds.push_back({ .fd = entry.fd, .events = POLLIN, .revents = 0 });
|
|
}
|
|
}
|
|
if (poll(fds.data(), fds.size(), MillisecondsToNextClockedUpdate()) == -1) {
|
|
wl_display_cancel_read(Device::display);
|
|
if (errno == EINTR) continue;
|
|
return;
|
|
}
|
|
if (fds[0].revents & (POLLIN | POLLERR | POLLHUP)) {
|
|
if (wl_display_read_events(Device::display) == -1) return;
|
|
} else {
|
|
wl_display_cancel_read(Device::display);
|
|
}
|
|
// Frame callbacks run Update here for windows on the compositor's clock.
|
|
if (wl_display_dispatch_pending(Device::display) == -1) return;
|
|
|
|
// A callback may add or remove descriptors, so each ready one is
|
|
// looked up again, and its callback copied, before it is called.
|
|
ready.clear();
|
|
for (std::size_t i = 1; i < fds.size(); ++i) {
|
|
if (fds[i].revents != 0) ready.push_back(fds[i].fd);
|
|
}
|
|
for (int fd : ready) {
|
|
for (const Window* window : Device::windows) {
|
|
auto entry = std::ranges::find(window->pollFds, fd, &PollFd::fd);
|
|
if (entry != window->pollFds.end()) {
|
|
std::function<void()> onReadable = entry->onReadable;
|
|
onReadable();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Gamepad::Tick();
|
|
onBeforeUpdate.Invoke();
|
|
|
|
// By index: an Update may open or close a window.
|
|
const auto now = std::chrono::high_resolution_clock::now();
|
|
for (std::size_t i = 0; i < Device::windows.size(); ++i) {
|
|
Window* window = Device::windows[i];
|
|
if (window->updating && window->updateInterval > std::chrono::nanoseconds::zero()
|
|
&& now >= window->lastFrameBegin + window->updateInterval) {
|
|
window->Update();
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
|
// While updating, the FIFO present inside Update() paces this loop to the
|
|
// vblank, so the PeekMessage poll is vsync-gated. While NOT updating
|
|
// (paused / minimized / stopped) nothing throttles the loop, and a bare
|
|
// PeekMessage(PM_REMOVE) spin pins a core at 100% while uselessly re-running
|
|
// Gamepad::Tick() (mutex + per-pad COM reads) and onBeforeUpdate every
|
|
// iteration. Block on the message queue instead, waking on input or after a
|
|
// short timeout so gamepad polling / onBeforeUpdate still tick at a sane
|
|
// rate (e.g. to detect a controller button asking to resume).
|
|
//
|
|
// A window on its own clock (updateInterval) sleeps the same way until
|
|
// its next update is due, instead of updating every pass.
|
|
constexpr DWORD kPausedPollIntervalMs = 16;
|
|
while(open) {
|
|
// Read each pass, as the Wayland loop does: the clock may be set or
|
|
// changed from inside the loop.
|
|
const bool clocked = updateInterval > std::chrono::nanoseconds::zero();
|
|
if (!updating) {
|
|
MsgWaitForMultipleObjectsEx(0, nullptr, kPausedPollIntervalMs,
|
|
QS_ALLINPUT, MWMO_INPUTAVAILABLE);
|
|
} else if (clocked) {
|
|
const auto wait = lastFrameBegin + updateInterval - std::chrono::high_resolution_clock::now();
|
|
if (wait > std::chrono::high_resolution_clock::duration::zero()) {
|
|
MsgWaitForMultipleObjectsEx(0, nullptr, static_cast<DWORD>(std::chrono::ceil<std::chrono::milliseconds>(wait).count()),
|
|
QS_ALLINPUT, MWMO_INPUTAVAILABLE);
|
|
}
|
|
}
|
|
// W, as the window class is: PeekMessageA would hand WM_CHAR over
|
|
// in the code page (see the constructor).
|
|
MSG msg;
|
|
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
TranslateMessage(&msg);
|
|
DispatchMessageW(&msg);
|
|
}
|
|
Gamepad::Tick();
|
|
onBeforeUpdate.Invoke();
|
|
if(updating && (!clocked || std::chrono::high_resolution_clock::now() >= lastFrameBegin + updateInterval)) {
|
|
Update();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Window::StayAlive() {
|
|
// Native has no equivalent of the browser's "return from main but keep
|
|
// the instance alive" trick — the process simply ends. The event loop
|
|
// IS what keeps a native window alive, so the only correct native
|
|
// behaviour is StartSync's loop. Delegating keeps a single source that
|
|
// compiles identically on both targets: an app can call StayAlive
|
|
// unconditionally and get the cheap path in the browser without a
|
|
// #ifdef at the call site.
|
|
StartSync();
|
|
}
|
|
|
|
void Window::StartUpdate() {
|
|
lastFrameBegin = std::chrono::high_resolution_clock::now();
|
|
updating = true;
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// A window on its own clock is updated by StartSync's loop instead.
|
|
if (updateInterval == std::chrono::nanoseconds::zero()) {
|
|
cb = wl_surface_frame(surface);
|
|
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Window::StopUpdate() {
|
|
updating = false;
|
|
}
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
void Window::AddPollFd(int fd, std::function<void()> onReadable) {
|
|
pollFds.push_back({ .fd = fd, .onReadable = std::move(onReadable) });
|
|
}
|
|
|
|
void Window::RemovePollFd(int fd) {
|
|
std::erase_if(pollFds, [fd](const PollFd& entry) { return entry.fd == fd; });
|
|
}
|
|
#endif
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
|
|
|
|
void Window::Update() {
|
|
startTime = std::chrono::high_resolution_clock::now();
|
|
#ifdef CRAFTER_TIMING
|
|
vblank = duration_cast<std::chrono::milliseconds>(startTime - frameEnd);
|
|
#endif
|
|
|
|
mouseDelta = {currentMousePos.x-lastMousePos.x, currentMousePos.y-lastMousePos.y};
|
|
currentFrameTime = {startTime, startTime-lastFrameBegin};
|
|
#ifdef CRAFTER_TIMING
|
|
auto renderStart = std::chrono::high_resolution_clock::now();
|
|
renderTimings.clear();
|
|
#endif
|
|
Render();
|
|
#ifdef CRAFTER_TIMING
|
|
auto renderEnd = std::chrono::high_resolution_clock::now();
|
|
totalRender = renderEnd - renderStart;
|
|
#endif
|
|
|
|
lastMousePos = currentMousePos;
|
|
|
|
#ifdef CRAFTER_TIMING
|
|
frameEnd = std::chrono::high_resolution_clock::now();
|
|
|
|
// Ring buffer: overwrite the oldest entry once full. Order doesn't matter
|
|
// to LogTiming, so we never need to shift elements.
|
|
frameTimes[frameTimesHead] = totalUpdate+totalRender;
|
|
frameTimesHead = (frameTimesHead + 1) % frameTimeCapacity;
|
|
if (frameTimesCount < frameTimeCapacity) {
|
|
++frameTimesCount;
|
|
}
|
|
#endif
|
|
lastFrameBegin = startTime;
|
|
}
|
|
|
|
namespace {
|
|
// A frame's readback for CaptureNextFrame: the buffer the swapchain image
|
|
// is copied into, recorded into the frame's own command buffer.
|
|
// It owns the buffer and its memory, which go through the deletion queue
|
|
// however the capture ends: written, or unwound past (a failed
|
|
// allocation, a lost device), as the frame may be copying into it.
|
|
struct FrameCapture {
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
|
VkMemoryPropertyFlags memoryFlags = 0;
|
|
std::uint32_t width = 0;
|
|
std::uint32_t height = 0;
|
|
// Where it goes: taken from Window::pendingCapture as the copy is
|
|
// recorded, so a capture asked for meanwhile (from onResize, say) is
|
|
// the next frame's.
|
|
std::filesystem::path path;
|
|
|
|
FrameCapture(std::uint32_t width, std::uint32_t height) : width(width), height(height) {}
|
|
FrameCapture(FrameCapture&& other) noexcept
|
|
: buffer(std::exchange(other.buffer, VK_NULL_HANDLE)), memory(std::exchange(other.memory, VK_NULL_HANDLE)),
|
|
memoryFlags(other.memoryFlags), width(other.width), height(other.height), path(std::move(other.path)) {}
|
|
FrameCapture& operator=(FrameCapture&&) = delete;
|
|
~FrameCapture() {
|
|
Device::EnqueueDeletion(buffer, memory);
|
|
}
|
|
};
|
|
|
|
// Records the copy of `image`, in GENERAL and last written at `writers`
|
|
// with `writerAccess`, into a new host-visible buffer.
|
|
FrameCapture RecordFrameCapture(VkCommandBuffer cmd, VkImage image, std::uint32_t width, std::uint32_t height, VkPipelineStageFlags writers, VkAccessFlags writerAccess) {
|
|
FrameCapture capture(width, height);
|
|
const VkBufferCreateInfo bufferInfo {
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
.size = static_cast<VkDeviceSize>(width) * height * 4,
|
|
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
|
};
|
|
// Into a local first: a call that fails leaves its output undefined.
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateBuffer(Device::device, &bufferInfo, nullptr, &buffer));
|
|
capture.buffer = buffer;
|
|
VkMemoryRequirements requirements;
|
|
vkGetBufferMemoryRequirements(Device::device, capture.buffer, &requirements);
|
|
// The CPU reads all of it: cached memory where there is some, as
|
|
// write-combined memory is very slow to read (#59, #89).
|
|
std::uint32_t memoryType;
|
|
try {
|
|
memoryType = Device::GetMemoryType(requirements.memoryTypeBits,
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
} catch (const std::runtime_error&) {
|
|
memoryType = Device::GetMemoryType(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
|
}
|
|
capture.memoryFlags = Device::memoryProperties.memoryTypes[memoryType].propertyFlags;
|
|
const VkMemoryAllocateInfo allocation {
|
|
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
|
.allocationSize = requirements.size,
|
|
.memoryTypeIndex = memoryType,
|
|
};
|
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkAllocateMemory(Device::device, &allocation, nullptr, &memory));
|
|
capture.memory = memory;
|
|
Device::CheckVkResult(vkBindBufferMemory(Device::device, capture.buffer, capture.memory, 0));
|
|
|
|
// The passes' writes, before the copy reads them.
|
|
const VkImageMemoryBarrier written {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
.srcAccessMask = writerAccess,
|
|
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
|
|
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.image = image,
|
|
.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
|
|
};
|
|
vkCmdPipelineBarrier(cmd, writers, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &written);
|
|
const VkBufferImageCopy region {
|
|
.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
|
|
.imageExtent = { width, height, 1 },
|
|
};
|
|
vkCmdCopyImageToBuffer(cmd, image, VK_IMAGE_LAYOUT_GENERAL, capture.buffer, 1, ®ion);
|
|
const VkBufferMemoryBarrier copied {
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
|
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
|
.dstAccessMask = VK_ACCESS_HOST_READ_BIT,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.buffer = capture.buffer,
|
|
.offset = 0,
|
|
.size = VK_WHOLE_SIZE,
|
|
};
|
|
vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &copied, 0, nullptr);
|
|
return capture;
|
|
}
|
|
|
|
// Once the frame has finished: the readback as an RGBA PNG at `path`.
|
|
void WriteFrameCapture(const FrameCapture& capture, VkFormat format) {
|
|
const std::filesystem::path& path = capture.path;
|
|
void* mapped = nullptr;
|
|
Device::CheckVkResult(vkMapMemory(Device::device, capture.memory, 0, VK_WHOLE_SIZE, 0, &mapped));
|
|
// Cached but not coherent: the copy's writes have to be pulled in.
|
|
if ((capture.memoryFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0) {
|
|
const VkMappedMemoryRange range {
|
|
.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
|
|
.memory = capture.memory,
|
|
.offset = 0,
|
|
.size = VK_WHOLE_SIZE,
|
|
};
|
|
Device::CheckVkResult(vkInvalidateMappedMemoryRanges(Device::device, 1, &range));
|
|
}
|
|
const std::uint8_t* src = static_cast<const std::uint8_t*>(mapped);
|
|
const std::size_t texels = static_cast<std::size_t>(capture.width) * capture.height;
|
|
std::vector<std::uint8_t> rgba(texels * 4);
|
|
const bool bgr = format == VK_FORMAT_B8G8R8A8_UNORM;
|
|
for (std::size_t i = 0; i < texels; ++i) {
|
|
rgba[i * 4 + 0] = src[i * 4 + (bgr ? 2 : 0)];
|
|
rgba[i * 4 + 1] = src[i * 4 + 1];
|
|
rgba[i * 4 + 2] = src[i * 4 + (bgr ? 0 : 2)];
|
|
rgba[i * 4 + 3] = src[i * 4 + 3];
|
|
}
|
|
vkUnmapMemory(Device::device, capture.memory);
|
|
if (stbi_write_png(path.string().c_str(), static_cast<int>(capture.width), static_cast<int>(capture.height), 4, rgba.data(), static_cast<int>(capture.width) * 4) == 0) {
|
|
throw std::runtime_error(std::format("CaptureNextFrame: could not write {}", path.string()));
|
|
}
|
|
}
|
|
}
|
|
|
|
void Window::Render() {
|
|
// A rebuild that waited for a frame boundary and came since the last
|
|
// one: on Wayland the size the compositor configured, which its listener
|
|
// only keeps (#199, #200), and a rebuild that threw. Here, not in that
|
|
// listener, a consumer that calls Update() itself has its `try` around
|
|
// it cover the Vulkan calls, and configures that came since the last
|
|
// frame cost one rebuild between them. onResize fires from it, with the
|
|
// queue idle and no frame being recorded, as it does from Resize. And
|
|
// on Wayland a scale its listener kept (#165) is taken here, also one
|
|
// that the end of the frame before, or a swapchain made since, left for
|
|
// this frame, as it gave the window no new pixels (TakeDeferredSize).
|
|
RebuildIfDeferred(FrameBoundary::Start);
|
|
|
|
// This frame's slot among the numFrames in flight. Its command buffer,
|
|
// descriptor-heap copy and acquire semaphore were last used numFrames
|
|
// frames ago: wait for that submission's fence before touching any of
|
|
// them. This — not a full-queue wait-idle — is what bounds
|
|
// frames-in-flight and gives the CPU/GPU overlap. The wait comes before
|
|
// the acquire and is keyed by frame, not by the acquired image as when
|
|
// the two were one index, so the swapchain may have any number of images
|
|
// (#160) and a frame still waits for exactly the one numFrames before it,
|
|
// which the deletion queue and the staging rings count on.
|
|
currentFrame = static_cast<std::uint32_t>(frameCounter % numFrames);
|
|
Device::CheckVkResult(vkWaitForFences(Device::device, 1, &waitFences[currentFrame], VK_TRUE, UINT64_MAX));
|
|
|
|
// Acquire the next image from the swap chain. If the surface has
|
|
// changed size out from under us (compositor/Win32 resize delivered
|
|
// between Render calls), recreate and retry once.
|
|
VkSemaphore imageAcquired = imageAcquiredSemaphores[currentFrame];
|
|
{
|
|
VkResult acquire = vkAcquireNextImageKHR(Device::device, swapChain, UINT64_MAX,
|
|
imageAcquired, (VkFence)nullptr, ¤tImage);
|
|
if (acquire == VK_ERROR_OUT_OF_DATE_KHR) {
|
|
RebuildSwapchain();
|
|
acquire = vkAcquireNextImageKHR(Device::device, swapChain, UINT64_MAX,
|
|
imageAcquired, (VkFence)nullptr, ¤tImage);
|
|
}
|
|
if (acquire != VK_SUBOPTIMAL_KHR) {
|
|
Device::CheckVkResult(acquire);
|
|
}
|
|
}
|
|
|
|
// From here to the present the frame owns images[currentImage] and its
|
|
// heap copy: a Resize or SetPresentMode from a listener waits for the
|
|
// present (see the end of this function), and a swapchain slot listed
|
|
// now is written at once. The guard clears it on the way out of a throw
|
|
// too, so a failed frame doesn't hold every later rebuild back.
|
|
recording = true;
|
|
struct RecordingGuard {
|
|
bool& recording;
|
|
~RecordingGuard() { recording = false; }
|
|
} recordingGuard { recording };
|
|
|
|
// The image this frame draws into, at this window's swapchain slots, in
|
|
// this frame's copy of the heap (or its descriptor set), which the fence
|
|
// wait above freed. Before onUpdate and the passes, which record against
|
|
// it.
|
|
if (descriptorHeap) {
|
|
descriptorHeap->WriteSwapchainImage(*this);
|
|
}
|
|
|
|
// Reset for this frame's submit after the acquire, which can fail (and
|
|
// recreate the swapchain on the way), and the slot writes: a fence reset
|
|
// and then never submitted would hang the next wait on this slot.
|
|
Device::CheckVkResult(vkResetFences(Device::device, 1, &waitFences[currentFrame]));
|
|
|
|
// Now that a frame's fence has been waited, free any deferred resources
|
|
// whose retire frame has been reached (issue #101). frameCounter is bumped
|
|
// at end of frame, so it still holds this frame's index here — exactly the
|
|
// value entries enqueued this frame are tagged with.
|
|
Device::ReclaimDeletions();
|
|
|
|
Device::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentFrame], &cmdBufInfo));
|
|
|
|
// On an image's first use after (re)creating the swapchain it is still in
|
|
// VK_IMAGE_LAYOUT_UNDEFINED; every subsequent frame leaves it in
|
|
// PRESENT_SRC_KHR. Transitioning from the wrong oldLayout is a validation
|
|
// error, so pick based on whether this image has been rendered before.
|
|
// The image was just acquired above, so touching it here is legal.
|
|
const bool firstUse = !imageInitialised[currentImage];
|
|
imageInitialised[currentImage] = true;
|
|
|
|
acquireBarrier.oldLayout = firstUse ? VK_IMAGE_LAYOUT_UNDEFINED
|
|
: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
|
acquireBarrier.image = images[currentImage];
|
|
|
|
// Real per-pass stage union for this frame's passes — the swapchain image
|
|
// is only ever written by compute/RT (and possibly transfer), never by the
|
|
// whole pipeline, so the frame-edge barriers wait on exactly those stages
|
|
// instead of ALL_COMMANDS. Derived once here and reused for the present
|
|
// barrier below (passes don't change within a Render()).
|
|
// Device::stageMask drops RAY_TRACING_SHADER on devices without ray
|
|
// tracing: the no-pass fallback and any un-narrowed pass report the
|
|
// conservative writer union, which names that stage, and a stage whose
|
|
// feature is not enabled is invalid in a barrier.
|
|
const VkPipelineStageFlags swapWriterStages = SwapchainStageUnion(passes) & Device::stageMask;
|
|
|
|
// dstAccessMask must only contain access flags supported by the dst stage
|
|
// mask (VUID-vkCmdPipelineBarrier-pImageMemoryBarriers-02820). Derive it
|
|
// from the same per-pass stage union the barrier uses as its dst stage mask
|
|
// instead of hardcoding SHADER_WRITE|TRANSFER_WRITE, which would carry
|
|
// TRANSFER_WRITE into a compute-only frame's COMPUTE_SHADER dst stage.
|
|
acquireBarrier.dstAccessMask = SwapchainWriterAccess(swapWriterStages);
|
|
|
|
// The acquire->GENERAL transition only needs to be visible before the first
|
|
// pass writes the image, i.e. at the writer stages — not ALL_COMMANDS.
|
|
vkCmdPipelineBarrier(drawCmdBuffers[currentFrame], VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, swapWriterStages, 0, 0, nullptr, 0, nullptr, 1, &acquireBarrier);
|
|
|
|
// Synthesise key-repeat events before listeners run, so the focused
|
|
// widget's OnTextInput / OnKeyDown sees them in the same frame.
|
|
Device::TickKeyRepeats();
|
|
|
|
// Bind the descriptor heaps BEFORE the user's update event fires.
|
|
// Any compute work the update lambda records (e.g. physics dispatches)
|
|
// needs the heaps bound at execution time; recording order in the cmd
|
|
// buffer dictates GPU execution order, so the bind must come first.
|
|
// Pass-side dispatches still run with the same heaps bound — moving
|
|
// the bind earlier doesn't change anything for them.
|
|
if (descriptorHeap) {
|
|
descriptorHeap->Bind(drawCmdBuffers[currentFrame], currentFrame);
|
|
}
|
|
|
|
onUpdate.Invoke({startTime, startTime-lastFrameBegin});
|
|
#ifdef CRAFTER_TIMING
|
|
totalUpdate = std::chrono::nanoseconds(0);
|
|
updateTimings.clear();
|
|
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : onUpdate.listenerTimes) {
|
|
updateTimings.push_back(entry);
|
|
totalUpdate += entry.second;
|
|
}
|
|
#endif
|
|
|
|
// The window doesn't clear the image: not every surface allows the
|
|
// swapchain TRANSFER_DST (swapchainUsage says whether this one does), so
|
|
// passes that need a background write one themselves, by a clear where
|
|
// the usage allows it or from a shader (UIScene's `background()`).
|
|
(void)clearColor;
|
|
|
|
for (std::size_t i = 0; i < passes.size(); ++i) {
|
|
passes[i]->Record(drawCmdBuffers[currentFrame], currentFrame, *this);
|
|
|
|
if (i + 1 < passes.size()) {
|
|
// Make pass i's writes to the swapchain image visible to pass i+1.
|
|
// The only resource the inter-pass hazard touches is that one image
|
|
// (later passes overdraw earlier ones), so use an image memory
|
|
// barrier scoped to its subresource instead of a queue-wide
|
|
// VkMemoryBarrier — only this image's caches round-trip, exactly as
|
|
// the intra-pass UI barrier already does. The stage masks come from
|
|
// each pass's own SwapchainStage(), so a compute->compute edge only
|
|
// serialises COMPUTE_SHADER while an RT pass on either side pulls in
|
|
// RAY_TRACING_SHADER — never the ALL_COMMANDS full-pipeline stall.
|
|
// Masked like swapWriterStages above: a pass that keeps the
|
|
// default stage reports a union that names RAY_TRACING_SHADER.
|
|
const VkPipelineStageFlags srcStages = passes[i]->SwapchainStage() & Device::stageMask;
|
|
const VkPipelineStageFlags dstStages = passes[i + 1]->SwapchainStage() & Device::stageMask;
|
|
VkImageMemoryBarrier ib = BuildSwapchainInterPassBarrier(images[currentImage], srcStages, dstStages);
|
|
vkCmdPipelineBarrier(drawCmdBuffers[currentFrame], srcStages, dstStages,
|
|
0, 0, nullptr, 0, nullptr, 1, &ib);
|
|
}
|
|
}
|
|
|
|
presentBarrier.image = images[currentImage];
|
|
|
|
// Mirror of the acquire barrier: srcAccessMask must be supported by the src
|
|
// stage mask (swapWriterStages). SHADER_WRITE alone is valid for COMPUTE/RT
|
|
// and so never fires today, but a transfer-stage writer would need
|
|
// TRANSFER_WRITE here too — derive both from the same union for symmetry.
|
|
presentBarrier.srcAccessMask = SwapchainWriterAccess(swapWriterStages);
|
|
|
|
// CaptureNextFrame: the copy goes here, while the image is still this
|
|
// frame's, and the transition to present waits for it.
|
|
std::optional<FrameCapture> capture;
|
|
VkPipelineStageFlags presentWaitStages = swapWriterStages;
|
|
if (pendingCapture) {
|
|
capture.emplace(RecordFrameCapture(drawCmdBuffers[currentFrame], images[currentImage], width, height, swapWriterStages, presentBarrier.srcAccessMask));
|
|
presentWaitStages |= VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
capture->path = std::move(*pendingCapture);
|
|
pendingCapture.reset();
|
|
}
|
|
|
|
// The ->PRESENT_SRC transition only needs to wait on the stages that
|
|
// actually wrote the image (the per-pass union), not ALL_COMMANDS; present
|
|
// itself is gated by the render-finished semaphore, so dst stays BOTTOM.
|
|
vkCmdPipelineBarrier(drawCmdBuffers[currentFrame], presentWaitStages, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &presentBarrier);
|
|
|
|
Device::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentFrame]));
|
|
|
|
// Patch the per-frame fields of the cached submitInfo/presentInfo (the
|
|
// invariant fields were set once in the constructor): submit waits on this
|
|
// frame's acquire semaphore and signals this image's render-finished
|
|
// semaphore, present waits on that same render-finished semaphore. The
|
|
// fence (keyed by frame slot) lets the Render() numFrames later reuse this
|
|
// slot's command buffer, heap copy and acquire semaphore safely. The
|
|
// semaphore handles live in member arrays, so taking their addresses here
|
|
// is stable for the call.
|
|
submitInfo.pWaitSemaphores = &imageAcquiredSemaphores[currentFrame];
|
|
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[currentImage];
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentFrame];
|
|
Device::CheckVkResult(vkQueueSubmit(Device::queue, 1, &submitInfo, waitFences[currentFrame]));
|
|
|
|
presentInfo.pWaitSemaphores = &renderFinishedSemaphores[currentImage];
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The present attaches this frame's buffer and commits the surface. What
|
|
// the surface is to take with that buffer goes right before it, so that
|
|
// one commit applies all of it: the surface's size, where this frame is
|
|
// the first drawn for it, and the acknowledgement of the configure the
|
|
// frame answers (#199). Nothing between here and the present commits the
|
|
// surface.
|
|
SendFrameSurfaceState();
|
|
#endif
|
|
|
|
VkResult result = vkQueuePresentKHR(Device::queue, &presentInfo);
|
|
// The frame is handed over: a swapchain rebuild may go ahead now.
|
|
recording = false;
|
|
if (result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_OUT_OF_DATE_KHR) {
|
|
// Surface size changed mid-present: rebuild below.
|
|
swapchainRebuildPending = true;
|
|
} else {
|
|
Device::CheckVkResult(result);
|
|
}
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
// The surface shows this swapchain from here, unless the present found
|
|
// it out of date and queued nothing. What SendFrameSurfaceState sent is
|
|
// with the compositor all the same then, pending until the surface's
|
|
// next commit. That is the first frame of the swapchain made below,
|
|
// unless a commit that is no frame's comes before it (the configure
|
|
// listener's, a consumer's request for a frame callback), which would
|
|
// show the last buffer at the new size after all: nothing sent can be
|
|
// taken back, and the present is where the driver commits.
|
|
if (result != VK_ERROR_OUT_OF_DATE_KHR) {
|
|
presentedGeneration = swapchainGeneration;
|
|
}
|
|
#endif
|
|
// No vkQueueWaitIdle in the steady-state path: per-frame fences now gate
|
|
// command-buffer reuse, so the CPU is free to acquire/record frame N+1
|
|
// while the GPU is still executing frame N.
|
|
frameCounter++;
|
|
// Advance the global deferred-deletion clock too (issue #101). Kept
|
|
// separate from the per-window frameCounter above (which also picks the
|
|
// frame slot) so non-window code can enqueue against it.
|
|
Device::frameCounter++;
|
|
|
|
if (capture) {
|
|
// A capture is for tests and screenshots: waiting for the frame here
|
|
// is fine.
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
WriteFrameCapture(*capture, colorFormat);
|
|
}
|
|
|
|
// The rebuild the frame held back: the present's, or a Resize or
|
|
// SetPresentMode from one of its listeners, or on Wayland a size
|
|
// configured while they ran (a listener that dispatches events), and a
|
|
// scale that came then, where it gives the window new pixels: otherwise
|
|
// it waits for the start of the next frame (TakeDeferredSize).
|
|
// RebuildSwapchain drains the queue and fires onResize before the next
|
|
// frame; the wait also leaves every per-frame fence signaled, so the next
|
|
// Render()'s fence wait passes through.
|
|
RebuildIfDeferred(FrameBoundary::AfterPresent);
|
|
}
|
|
|
|
#ifdef CRAFTER_TIMING
|
|
void Window::LogTiming() {
|
|
std::cout << std::format("Update: {}", duration_cast<std::chrono::milliseconds>(totalUpdate)) << std::endl;
|
|
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : updateTimings) {
|
|
std::cout << std::format("\t{} {}", reinterpret_cast<const void*>(entry.first), duration_cast<std::chrono::microseconds>(entry.second)) << std::endl;
|
|
}
|
|
std::cout << std::format("Render: {}", duration_cast<std::chrono::milliseconds>(totalRender)) << std::endl;
|
|
for (const std::tuple<const RenderingElement*, std::uint32_t, std::uint32_t, std::chrono::nanoseconds>& entry : renderer.renderTimings) {
|
|
std::cout << std::format("\t{} {}x{} {}", reinterpret_cast<const void*>(std::get<0>(entry)), std::get<1>(entry), std::get<2>(entry), duration_cast<std::chrono::microseconds>(std::get<3>(entry))) << std::endl;
|
|
}
|
|
std::cout << std::format("Total: {}", duration_cast<std::chrono::milliseconds>(totalUpdate+totalRender)) << std::endl;
|
|
std::cout << std::format("Vblank: {}", duration_cast<std::chrono::milliseconds>(vblank)) << std::endl;
|
|
|
|
// Add 100-frame average and min-max timing info. Only the first
|
|
// frameTimesCount entries of the ring buffer hold valid samples.
|
|
if (frameTimesCount != 0) {
|
|
// Calculate average
|
|
std::chrono::nanoseconds sum(0);
|
|
for (std::size_t i = 0; i < frameTimesCount; ++i) {
|
|
sum += frameTimes[i];
|
|
}
|
|
auto average = sum / frameTimesCount;
|
|
|
|
// Find min and max
|
|
auto min = frameTimes[0];
|
|
auto max = frameTimes[0];
|
|
for (std::size_t i = 0; i < frameTimesCount; ++i) {
|
|
if (frameTimes[i] < min) min = frameTimes[i];
|
|
if (frameTimes[i] > max) max = frameTimes[i];
|
|
}
|
|
|
|
std::cout << std::format("Last 100 Frame Times - Avg: {}, Min: {}, Max: {}",
|
|
duration_cast<std::chrono::milliseconds>(average),
|
|
duration_cast<std::chrono::milliseconds>(min),
|
|
duration_cast<std::chrono::milliseconds>(max)) << std::endl;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
VkSurfaceCapabilitiesKHR Window::SwapchainCapabilities(VkPhysicalDevice gpu, VkSurfaceKHR surface,
|
|
VkPresentModeKHR mode, bool modePassedAlong) {
|
|
VkSurfaceCapabilitiesKHR allModes {};
|
|
if (!Device::surfaceMaintenance1Supported || !modePassedAlong) {
|
|
Device::CheckVkResult(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &allModes));
|
|
if (!Device::surfaceMaintenance1Supported) {
|
|
return allModes;
|
|
}
|
|
}
|
|
VkSurfacePresentModeKHR modeInfo {
|
|
.sType = VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_KHR,
|
|
.presentMode = mode,
|
|
};
|
|
const VkPhysicalDeviceSurfaceInfo2KHR surfaceInfo {
|
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR,
|
|
.pNext = &modeInfo,
|
|
.surface = surface,
|
|
};
|
|
VkSurfaceCapabilities2KHR forMode { .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR };
|
|
Device::CheckVkResult(Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR(gpu, &surfaceInfo, &forMode));
|
|
VkSurfaceCapabilitiesKHR capabilities = forMode.surfaceCapabilities;
|
|
if (!modePassedAlong) {
|
|
// VUID-VkSwapchainCreateInfoKHR-presentMode-02839 and the
|
|
// forward-progress rule of a UINT64_MAX acquire count against the
|
|
// surface's minimum for all modes unless the swapchain names its
|
|
// mode, so that minimum binds too.
|
|
capabilities.minImageCount = std::max(capabilities.minImageCount, allModes.minImageCount);
|
|
if (allModes.maxImageCount != 0) {
|
|
capabilities.maxImageCount = capabilities.maxImageCount == 0
|
|
? allModes.maxImageCount
|
|
: std::min(capabilities.maxImageCount, allModes.maxImageCount);
|
|
}
|
|
}
|
|
return capabilities;
|
|
}
|
|
|
|
std::uint32_t Window::SwapchainMinImageCount(const VkSurfaceCapabilitiesKHR& capabilities) {
|
|
std::uint32_t count = std::max<std::uint32_t>(numFrames, capabilities.minImageCount);
|
|
if (capabilities.maxImageCount != 0) {
|
|
count = std::min(count, capabilities.maxImageCount);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
void Window::CreateSwapchain()
|
|
{
|
|
// Store the current swap chain handle so we can use it later on to ease up recreation
|
|
VkSwapchainKHR oldSwapchain = swapChain;
|
|
|
|
// VK_PRESENT_MODE_FIFO_KHR ("v-sync") is guaranteed available per spec, so
|
|
// it is the fallback for a mode the surface lacks. For benchmarking we
|
|
// want frame pacing uncapped from the display refresh, so when
|
|
// CRAFTER_PRESENT_IMMEDIATE is set in the environment we prefer IMMEDIATE
|
|
// (then MAILBOX) whatever presentMode asks for. Chosen first, because how
|
|
// many images the swapchain needs depends on it.
|
|
{
|
|
std::uint32_t presentModeCount = 0;
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(Device::physDevice, vulkanSurface, &presentModeCount, nullptr);
|
|
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(Device::physDevice, vulkanSurface, &presentModeCount, presentModes.data());
|
|
auto offered = [&](VkPresentModeKHR mode) { return std::ranges::find(presentModes, mode) != presentModes.end(); };
|
|
swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
if (std::getenv("CRAFTER_PRESENT_IMMEDIATE")) {
|
|
if (offered(VK_PRESENT_MODE_IMMEDIATE_KHR)) swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
|
else if (offered(VK_PRESENT_MODE_MAILBOX_KHR)) swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
|
|
} else if (presentMode == PresentMode::Mailbox && offered(VK_PRESENT_MODE_MAILBOX_KHR)) {
|
|
swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
|
|
} else if (presentMode == PresentMode::Immediate && offered(VK_PRESENT_MODE_IMMEDIATE_KHR)) {
|
|
swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
|
}
|
|
}
|
|
|
|
// Get physical device surface properties, for that present mode. With
|
|
// swapchain_maintenance1 the swapchain names its mode, so that mode's
|
|
// image counts are the ones that apply, which on Mesa's Wayland are below
|
|
// the surface's for all modes (FIFO three where MAILBOX needs four, #160).
|
|
// This is the last capabilities query before vkCreateSwapchainKHR, as the
|
|
// validation layer checks minImageCount against the surface's last answer.
|
|
const VkSurfaceCapabilitiesKHR surfCaps = SwapchainCapabilities(Device::physDevice, vulkanSurface,
|
|
swapchainPresentMode, Device::swapchainMaintenance1Supported);
|
|
|
|
VkExtent2D swapchainExtent = {};
|
|
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
|
|
if (surfCaps.currentExtent.width == (uint32_t)-1)
|
|
{
|
|
// If the surface size is undefined, the size is set to the size of the images requested
|
|
swapchainExtent.width = width;
|
|
swapchainExtent.height = height;
|
|
}
|
|
else
|
|
{
|
|
// If the surface size is defined, the swap chain size must match
|
|
swapchainExtent = surfCaps.currentExtent;
|
|
width = surfCaps.currentExtent.width;
|
|
height = surfCaps.currentExtent.height;
|
|
}
|
|
|
|
|
|
// Find the transformation of the surface
|
|
VkSurfaceTransformFlagsKHR preTransform;
|
|
if (surfCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
|
|
{
|
|
// We prefer a non-rotated transform
|
|
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
|
}
|
|
else
|
|
{
|
|
preTransform = surfCaps.currentTransform;
|
|
}
|
|
|
|
VkSwapchainCreateInfoKHR swapchainCI = {};
|
|
swapchainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
swapchainCI.surface = vulkanSurface;
|
|
swapchainCI.minImageCount = SwapchainMinImageCount(surfCaps);
|
|
swapchainCI.imageFormat = colorFormat;
|
|
swapchainCI.imageColorSpace = colorSpace;
|
|
swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
|
|
swapchainUsage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
|
| (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
|
swapchainCI.imageUsage = swapchainUsage;
|
|
swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
|
|
swapchainCI.imageArrayLayers = 1;
|
|
swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
swapchainCI.queueFamilyIndexCount = 0;
|
|
swapchainCI.presentMode = swapchainPresentMode;
|
|
// The mode, named again for swapchain_maintenance1: what makes the mode's
|
|
// own image counts the ones that apply, at creation and to the acquires.
|
|
const VkSwapchainPresentModesCreateInfoKHR presentModes {
|
|
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_KHR,
|
|
.presentModeCount = 1,
|
|
.pPresentModes = &swapchainPresentMode,
|
|
};
|
|
if (Device::swapchainMaintenance1Supported) {
|
|
swapchainCI.pNext = &presentModes;
|
|
}
|
|
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
|
|
swapchainCI.oldSwapchain = oldSwapchain;
|
|
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
|
|
swapchainCI.clipped = VK_TRUE;
|
|
swapchainCI.compositeAlpha = compositeAlpha;
|
|
|
|
// The old images' views go before the images do. Nothing uses them: the
|
|
// caller waited for the queue, and a heap copy or descriptor set that
|
|
// still names one is written again before its frame binds it.
|
|
for (VkImageView view : imageViews) {
|
|
vkDestroyImageView(Device::device, view, nullptr);
|
|
}
|
|
imageViews.clear();
|
|
|
|
// Each handle reaches its member only once it is made, here and for the
|
|
// views below: ~Window releases what the members name, also after a
|
|
// constructor whose CreateSwapchain threw.
|
|
VkSwapchainKHR madeSwapchain = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateSwapchainKHR(Device::device, &swapchainCI, nullptr, &madeSwapchain));
|
|
swapChain = madeSwapchain;
|
|
|
|
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
|
|
if (oldSwapchain != VK_NULL_HANDLE) {
|
|
vkDestroySwapchainKHR(Device::device, oldSwapchain, nullptr);
|
|
}
|
|
// The swap chain images: as many as the driver made, which may be more
|
|
// than minImageCount asked for. Everything kept per image is sized from
|
|
// this count; what is kept per frame in flight stays numFrames, so any
|
|
// count works (#160).
|
|
uint32_t imageCount{ 0 };
|
|
Device::CheckVkResult(vkGetSwapchainImagesKHR(Device::device, swapChain, &imageCount, nullptr));
|
|
images.resize(imageCount);
|
|
Device::CheckVkResult(vkGetSwapchainImagesKHR(Device::device, swapChain, &imageCount, images.data()));
|
|
|
|
// Brand-new swapchain images are in VK_IMAGE_LAYOUT_UNDEFINED; none have
|
|
// been rendered/presented yet. Render() consults this to pick the correct
|
|
// oldLayout for each image's first post-acquire transition.
|
|
imageInitialised.assign(imageCount, false);
|
|
|
|
// A render-finished semaphore per image. The ones the old swapchain's
|
|
// images used are kept: its last presents may still be waiting on them.
|
|
// The vector has room for all of them before the first is made: a
|
|
// push_back that grew it, and threw, would leave the semaphore just made
|
|
// in no member, where ~Window does not find it.
|
|
const VkSemaphoreCreateInfo semaphoreCreateInfo { .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
|
renderFinishedSemaphores.reserve(imageCount);
|
|
while (renderFinishedSemaphores.size() < imageCount) {
|
|
VkSemaphore semaphore = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateSemaphore(Device::device, &semaphoreCreateInfo, nullptr, &semaphore));
|
|
renderFinishedSemaphores.push_back(semaphore);
|
|
}
|
|
|
|
static std::atomic<std::uint64_t> swapchainsMade = 0;
|
|
swapchainGeneration = ++swapchainsMade;
|
|
|
|
imageViewInfos.resize(imageCount);
|
|
for (std::uint32_t i = 0; i < imageCount; i++) {
|
|
imageViewInfos[i] = {
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
.flags = 0,
|
|
.image = images[i],
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
|
.format = colorFormat,
|
|
.components = {
|
|
VK_COMPONENT_SWIZZLE_R,
|
|
VK_COMPONENT_SWIZZLE_G,
|
|
VK_COMPONENT_SWIZZLE_B,
|
|
VK_COMPONENT_SWIZZLE_A
|
|
},
|
|
.subresourceRange = {
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
.baseMipLevel = 0,
|
|
.levelCount = 1,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = 1,
|
|
},
|
|
};
|
|
}
|
|
// The views made, for descriptor sets, once per swapchain rather than
|
|
// once per frame whose image changed.
|
|
imageViews.resize(imageCount);
|
|
for (std::uint32_t i = 0; i < imageCount; i++) {
|
|
VkImageView view = VK_NULL_HANDLE;
|
|
Device::CheckVkResult(vkCreateImageView(Device::device, &imageViewInfos[i], nullptr, &view));
|
|
imageViews[i] = view;
|
|
}
|
|
}
|
|
|
|
VkCommandBuffer Window::StartInit() {
|
|
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
// StartInit reuses drawCmdBuffers[currentFrame] — the same buffer the
|
|
// steady-state Render() loop submits, guarded by waitFences[currentFrame].
|
|
// When setup runs mid-session (e.g. building map meshes / acceleration
|
|
// structures on a scene transition) the loop's last submission of this
|
|
// buffer may still be in flight: re-beginning it would trip
|
|
// VUID-vkBeginCommandBuffer-commandBuffer-00049, re-submitting it
|
|
// VUID-vkQueueSubmit-pCommandBuffers-00071, and any resource the caller
|
|
// destroys between here and FinishInit could still be referenced by that
|
|
// pending submission (vkDestroyBuffer-in-use). Drain the queue first so the
|
|
// buffer is free to re-record and no in-flight frame still references the
|
|
// resources this setup is about to replace (issue #153). Setup is rare
|
|
// (not the per-frame path), so a full wait-idle here is fine — FinishInit
|
|
// already wait-idles at the end.
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
Device::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentFrame], &cmdBufInfo));
|
|
|
|
// The swapchain images are deliberately NOT transitioned here. They are
|
|
// presentable images and may only be touched after vkAcquireNextImageKHR
|
|
// returns them; transitioning an unacquired presentable image is a
|
|
// validation error. Render() instead transitions each image from
|
|
// VK_IMAGE_LAYOUT_UNDEFINED on its first acquired use (see
|
|
// imageInitialised). This command buffer is for the caller's one-time
|
|
// setup work (uploads, acceleration-structure builds, etc.).
|
|
return drawCmdBuffers[currentFrame];
|
|
}
|
|
|
|
void Window::FinishInit() {
|
|
VkSubmitInfo submitInfo{};
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
submitInfo.commandBufferCount = 1;
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentFrame];
|
|
Device::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentFrame]));
|
|
Device::CheckVkResult(vkQueueSubmit(Device::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
}
|
|
|
|
VkCommandBuffer Window::GetCmd() {
|
|
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
// Same lifecycle hazard as StartInit: GetCmd re-begins and EndCmd
|
|
// re-submits the shared drawCmdBuffers[currentFrame], so drain any
|
|
// in-flight submission of it (and free the resources it references) before
|
|
// re-recording (issue #153).
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
Device::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentFrame], &cmdBufInfo));
|
|
|
|
VkImageSubresourceRange range{};
|
|
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
range.baseMipLevel = 0;
|
|
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
range.baseArrayLayer = 0;
|
|
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
|
|
return drawCmdBuffers[currentFrame];
|
|
}
|
|
|
|
void Window::EndCmd(VkCommandBuffer cmd) {
|
|
VkSubmitInfo submitInfo{};
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
submitInfo.commandBufferCount = 1;
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentFrame];
|
|
Device::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentFrame]));
|
|
Device::CheckVkResult(vkQueueSubmit(Device::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
|
}
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
|
void Window::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) {
|
|
wl_callback_destroy(cb);
|
|
Window* window = reinterpret_cast<Window*>(data);
|
|
|
|
// The member names the live callback, so the destructor can cancel it.
|
|
// A window that has since moved to its own clock lets the chain end.
|
|
if(window->updating && window->updateInterval == std::chrono::nanoseconds::zero()) {
|
|
window->cb = wl_surface_frame(window->surface);
|
|
wl_callback_add_listener(window->cb, &Window::wl_callback_listener, window);
|
|
window->Update();
|
|
} else {
|
|
window->cb = nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
void Window::xdg_toplevel_configure(void* data, xdg_toplevel*, std::int32_t width, std::int32_t height, wl_array* states){
|
|
// xdg-shell batches state — width/height are pending until the matching
|
|
// xdg_surface.configure arrives. Width/height are in surface-local
|
|
// (logical DP) units; (0, 0) means "compositor has no preference".
|
|
Window* window = reinterpret_cast<Window*>(data);
|
|
window->pendingLogicalWidth = width;
|
|
window->pendingLogicalHeight = height;
|
|
// The states are batched with them. The array lists the ones that are
|
|
// set: one it leaves out is not.
|
|
window->pendingActivated = false;
|
|
window->pendingSuspended = false;
|
|
const std::uint32_t* state = static_cast<const std::uint32_t*>(states->data);
|
|
for (std::size_t i = 0; i < states->size / sizeof(std::uint32_t); ++i) {
|
|
if (state[i] == XDG_TOPLEVEL_STATE_ACTIVATED) window->pendingActivated = true;
|
|
else if (state[i] == XDG_TOPLEVEL_STATE_SUSPENDED) window->pendingSuspended = true;
|
|
}
|
|
}
|
|
|
|
void Window::xdg_toplevel_configure_bounds(void*, xdg_toplevel*, std::int32_t, std::int32_t) {}
|
|
|
|
void Window::xdg_toplevel_wm_capabilities(void*, xdg_toplevel*, wl_array*) {}
|
|
|
|
void Window::ApplyToplevelState() {
|
|
if (activated == pendingActivated && suspended == pendingSuspended) return;
|
|
activated = pendingActivated;
|
|
suspended = pendingSuspended;
|
|
onToplevelState.Invoke();
|
|
}
|
|
|
|
void Window::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
|
Window* window = reinterpret_cast<Window*>(data);
|
|
window->onClose.Invoke();
|
|
window->open = false;
|
|
}
|
|
|
|
void Window::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
|
Window* window = reinterpret_cast<Window*>(data);
|
|
|
|
// The size this batch asks for. The listener only keeps it: the next
|
|
// Render() makes the swapchain again (RebuildIfDeferred) and answers the
|
|
// configure with its frame. Rebuilt here, a Vulkan error would be thrown
|
|
// through libwayland's dispatch (#200), the consumer's loop would stand
|
|
// still in it for the rebuild, and each of several configures before a
|
|
// frame would cost one. (0, 0) means the compositor has no preference,
|
|
// and a size that waited goes on waiting. Before the swapchain exists a
|
|
// configure's size is not taken (the first configure's, #166).
|
|
const bool hasSwapchain = window->configured && window->swapChain != VK_NULL_HANDLE;
|
|
if (hasSwapchain && window->pendingLogicalWidth > 0 && window->pendingLogicalHeight > 0) {
|
|
// The last size asked for wins, whoever asked: a Resize from a
|
|
// listener of the frame being recorded, which waits for that frame's
|
|
// present, gives way to this later one, as it did when this listener
|
|
// called Resize itself.
|
|
window->deferredWidth = 0;
|
|
window->deferredHeight = 0;
|
|
// The size the window has means that nothing waits any more,
|
|
// whatever did. It has it when the surface's size is this one and
|
|
// the buffers are the pixels for it at the scale the compositor
|
|
// asked for last, which the frame takes with the size: a scale that
|
|
// changed since they were made has the frame make them again, and a
|
|
// size that only comes to the same pixels as the one before (the
|
|
// scale changed with it) is still another size for the surface. And
|
|
// only where the window draws at that scale already: while a new
|
|
// one waits, the configure waits for the frame that takes the scale
|
|
// and the size together, so that a window the compositor sized
|
|
// always has the pixels its surface's size comes to at the scale it
|
|
// draws at.
|
|
const float newest = ScaleOf(window->preferredScale);
|
|
const bool current = window->pendingLogicalWidth == window->logicalWidth
|
|
&& window->pendingLogicalHeight == window->logicalHeight
|
|
&& newest == window->scale
|
|
&& ScaledPixels(window->pendingLogicalWidth, newest) == window->width
|
|
&& ScaledPixels(window->pendingLogicalHeight, newest) == window->height;
|
|
window->deferredLogicalWidth = current ? 0 : window->pendingLogicalWidth;
|
|
window->deferredLogicalHeight = current ? 0 : window->pendingLogicalHeight;
|
|
// And the size is the compositor's from here, also where the
|
|
// application's pixels had come to it.
|
|
if (current) window->logicalConfigured = true;
|
|
}
|
|
|
|
// An acknowledgement says that the next commit answers the configure.
|
|
// While a frame is owed, that commit is the frame's and not the one
|
|
// below or a consumer's request for a frame callback, which show the old
|
|
// frame: the serial waits for it, the last one when several come
|
|
// (SendFrameSurfaceState). A frame is owed while a size waits, and while
|
|
// the surface does not show the window as it is: a swapchain still to
|
|
// be made (a rebuild that threw, after which width and height are ahead
|
|
// of it), one no frame has been presented from yet, a size the surface
|
|
// has not been sent. Otherwise what the surface shows answers the
|
|
// configure already, and this acknowledgement takes with it any serial
|
|
// that waited: one older than the last acknowledged is a protocol error.
|
|
const bool sizeUnsent = window->wpViewport != nullptr
|
|
&& (window->logicalWidth != window->sentLogicalWidth || window->logicalHeight != window->sentLogicalHeight);
|
|
const bool frameOwed = window->deferredLogicalWidth != 0
|
|
|| (hasSwapchain && (window->swapchainRebuildPending || window->presentedGeneration != window->swapchainGeneration || sizeUnsent));
|
|
if (frameOwed) {
|
|
window->unackedConfigure = serial;
|
|
} else {
|
|
window->unackedConfigure.reset();
|
|
xdg_surface_ack_configure(xdg_surface, serial);
|
|
}
|
|
|
|
// Not the answer to a configure that waits, and it brings the surface no
|
|
// new size, since the viewport's destination comes with the frame too
|
|
// (#199). It applies what else is pending: a frame callback StartUpdate
|
|
// asked for, which is what starts its loop.
|
|
if (window->configured) {
|
|
wl_surface_commit(window->surface);
|
|
}
|
|
|
|
window->configured = true;
|
|
// The events last, since a listener may destroy the window: the second
|
|
// fires only for one that is still there, and nothing of the window is
|
|
// used after it.
|
|
window->ApplyToplevelState();
|
|
if (frameOwed && std::ranges::contains(Device::windows, window)) {
|
|
window->onFrameOwed.Invoke();
|
|
}
|
|
}
|
|
|
|
void Window::SendFrameSurfaceState() {
|
|
// The surface's size, when this frame is the first to be drawn for it
|
|
// (the window's first frame among them). It is the size that was settled
|
|
// when the buffers' was, and not worked out from the scale of this
|
|
// moment: a listener of this frame may have dispatched a new one since.
|
|
// A swapchain made again for the size the surface has sends nothing.
|
|
if (wpViewport != nullptr && (logicalWidth != sentLogicalWidth || logicalHeight != sentLogicalHeight)) {
|
|
wp_viewport_set_destination(wpViewport, logicalWidth, logicalHeight);
|
|
sentLogicalWidth = logicalWidth;
|
|
sentLogicalHeight = logicalHeight;
|
|
}
|
|
// A frame recorded before a size came does not honour it: that
|
|
// configure, and any after it, waits for the frame at that size.
|
|
if (unackedConfigure && deferredLogicalWidth == 0) {
|
|
xdg_surface_ack_configure(xdgSurface, *unackedConfigure);
|
|
unackedConfigure.reset();
|
|
}
|
|
}
|
|
|
|
void Window::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale) {
|
|
Window* window = reinterpret_cast<Window*>(data);
|
|
// A scale of 0 is none: the pixels worked out with it would be 0.
|
|
if (scale == 0) return;
|
|
// Kept, and nothing else, as a configure's size is: the window goes on
|
|
// drawing at the scale it draws at, and converting the pointer, the
|
|
// touches and the caret with it, until a frame boundary of Render()
|
|
// takes this one with the swapchain it may need (TakeDeferredSize). Set
|
|
// here, `scale` would convert positions for frames still drawn at the
|
|
// old one (#165), and would change in the middle of a frame whose
|
|
// listener dispatched this event. And no Vulkan call is made from inside
|
|
// the dispatch (#200). Before the window has its first swapchain the
|
|
// constructor takes the scale (MatchSizesToScale).
|
|
window->preferredScale = scale;
|
|
const bool hasSwapchain = window->configured && window->swapChain != VK_NULL_HANDLE;
|
|
// A scale the window draws at owes nothing, also one that comes back to
|
|
// it before a frame took the one between.
|
|
if (!hasSwapchain || ScaleOf(scale) == window->scale) return;
|
|
// A frame is owed, which a window that draws on demand learns of here,
|
|
// as of a configure's size: it gets no onResize where its pixels stay.
|
|
// The event last, since a listener may destroy the window.
|
|
window->onFrameOwed.Invoke();
|
|
}
|
|
|
|
#endif
|
|
|
|
void Window::CaptureNextFrame(std::filesystem::path path) {
|
|
pendingCapture = std::move(path);
|
|
}
|
|
#endif // !CRAFTER_GRAPHICS_WINDOW_DOM
|
|
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
// DOM backend
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
//
|
|
// In DOM mode the "window" IS the browser page; there is no separate
|
|
// surface to create, no swapchain to manage, no GPU pipeline to wait on.
|
|
// All Window does here is:
|
|
// - mirror requested title onto document.title
|
|
// - register itself with the JS bridge so DOM-level events route into
|
|
// its event objects
|
|
// - hand the frame loop to requestAnimationFrame; `Update` runs on
|
|
// each rAF tick when `updating` is true
|
|
//
|
|
// The C exports (__crafterDom_*) below are how the JS bridge reaches
|
|
// back into the live Window instance. We keep a process-global pointer
|
|
// for V1 — only one Window per page — and lookups are O(1).
|
|
|
|
#ifdef CRAFTER_GRAPHICS_WINDOW_DOM
|
|
|
|
// The JS runtime initializes itself before main() runs, so there's
|
|
// nothing to do here. Defined as a no-op so user code calling
|
|
// `Device::Initialize()` links the same way it does on native.
|
|
void Device::Initialize() {}
|
|
|
|
namespace {
|
|
Window* g_domWindow = nullptr;
|
|
}
|
|
|
|
namespace Crafter::DomEnv {
|
|
__attribute__((import_module("env"), import_name("domAttachWindow")))
|
|
void domAttachWindow(std::int32_t handle);
|
|
__attribute__((import_module("env"), import_name("domSetTitle")))
|
|
void domSetTitle(const char* title, std::int32_t titleLen);
|
|
__attribute__((import_module("env"), import_name("domGetInnerWidth")))
|
|
std::int32_t domGetInnerWidth();
|
|
__attribute__((import_module("env"), import_name("domGetInnerHeight")))
|
|
std::int32_t domGetInnerHeight();
|
|
__attribute__((import_module("env"), import_name("domStartFrameLoop")))
|
|
void domStartFrameLoop();
|
|
__attribute__((import_module("env"), import_name("domStopFrameLoop")))
|
|
void domStopFrameLoop();
|
|
}
|
|
|
|
// Compile-time string hash matching what dom-env.js sends through. The
|
|
// JS bridge marshals `KeyboardEvent.code` as a UTF-8 string; we hash it
|
|
// to a 32-bit KeyCode here so the same value compares equal against the
|
|
// table in :Keys (DOM branch). FNV-1a, deterministic, no allocation.
|
|
namespace {
|
|
constexpr KeyCode HashKeyCode(const char* p, std::size_t n) {
|
|
std::uint32_t h = 2166136261u;
|
|
for (std::size_t i = 0; i < n; ++i) {
|
|
h ^= static_cast<std::uint8_t>(p[i]);
|
|
h *= 16777619u;
|
|
}
|
|
return h;
|
|
}
|
|
}
|
|
|
|
Window::Window(std::uint32_t w, std::uint32_t h, const std::string_view title)
|
|
: Window(w, h) {
|
|
SetTitle(title);
|
|
}
|
|
|
|
Window::Window(std::uint32_t w, std::uint32_t h) : width(w), height(h) {
|
|
if (g_domWindow != nullptr) {
|
|
std::println("Crafter::Window: only one DOM Window per page; "
|
|
"overwriting the previous instance.");
|
|
}
|
|
g_domWindow = this;
|
|
|
|
// Browser owns the real surface size. The width/height passed in are
|
|
// advisory only — useful as a native-side hint, ignored on DOM. We
|
|
// always sync to innerWidth/innerHeight so:
|
|
// - window.width/.height match the canvas's CSS pixel size,
|
|
// - MouseEvent.clientX/.clientY (CSS pixels) compare correctly
|
|
// against any layout done with window.width/.height,
|
|
// - the dispatch group count from window.width/8 covers the
|
|
// canvas exactly.
|
|
(void)w; (void)h;
|
|
width = static_cast<std::uint32_t>(Crafter::DomEnv::domGetInnerWidth());
|
|
height = static_cast<std::uint32_t>(Crafter::DomEnv::domGetInnerHeight());
|
|
|
|
// The handle passed to attach is just a non-zero token the JS side
|
|
// includes back in every dispatcher call. We don't use it on the
|
|
// C++ side (g_domWindow is the lookup) but it has to be non-zero so
|
|
// the JS bridge treats the window as "attached".
|
|
Crafter::DomEnv::domAttachWindow(1);
|
|
|
|
lastMousePos = {0, 0};
|
|
currentMousePos = {0, 0};
|
|
mouseDelta = {0, 0};
|
|
}
|
|
|
|
Window::~Window() {
|
|
// Clear the global pointer iff it still references us — defensive
|
|
// against a stack-allocated Window in main() that goes out of scope
|
|
// while rAF / DOM event callbacks are still queued. After this, the
|
|
// JS-side dispatchers (__crafterDom_*) early-return harmlessly. A
|
|
// shrill warning to the console flags the (almost certainly
|
|
// unintended) lifetime mistake so the user notices before everything
|
|
// mysteriously stops working.
|
|
if (g_domWindow == this) {
|
|
g_domWindow = nullptr;
|
|
std::println("Crafter::Window: destroyed while DOM mode is active. "
|
|
"Browser events will no-op until a new Window is constructed. "
|
|
"Did you forget to put the Window in `static` / `new`d storage?");
|
|
}
|
|
}
|
|
|
|
void Window::SetTitle(const std::string_view title) {
|
|
Crafter::DomEnv::domSetTitle(title.data(), static_cast<std::int32_t>(title.size()));
|
|
}
|
|
|
|
void Window::SetAppId(const std::string_view) {}
|
|
|
|
void Window::SetTextInput(bool, std::int32_t, std::int32_t, std::int32_t, std::int32_t, TextInputPurpose, std::optional<std::uint32_t>) {}
|
|
|
|
void Window::Resize(std::uint32_t newWidth, std::uint32_t newHeight) {
|
|
if (newWidth == 0 || newHeight == 0) return;
|
|
if (newWidth == width && newHeight == height) return;
|
|
width = newWidth;
|
|
height = newHeight;
|
|
onResize.Invoke();
|
|
}
|
|
|
|
void Window::SetCursorImage(std::uint16_t /*cw*/, std::uint16_t /*ch*/,
|
|
std::uint16_t /*hx*/, std::uint16_t /*hy*/,
|
|
const std::uint8_t* /*pixels*/) {
|
|
// V1: not wired. The natural impl is to base64-encode an inline PNG
|
|
// and assign it via document.body.style.cursor = `url(data:...) hx hy, auto`.
|
|
// Left for a follow-up so the first DOM build can ship without an
|
|
// inline PNG encoder.
|
|
}
|
|
|
|
void Window::SetDefaultCursor() {
|
|
// Mirror SetCursorImage stub. Future impl: clear body.style.cursor.
|
|
}
|
|
|
|
void Window::StartSync() {
|
|
// Hand the loop to rAF, then exit the wasm via _Exit so wasi-libc
|
|
// skips __wasm_call_dtors. If we let main return normally, _start
|
|
// calls __wasm_call_dtors → static destructors fire (including
|
|
// Window's own), then __wasi_proc_exit → wasm trap. Subsequent rAF
|
|
// calls into the wasm would then trap too, killing rendering.
|
|
// _Exit jumps straight to __wasi_proc_exit, which our runtime.js
|
|
// catches via a thrown sentinel so the instance stays alive while
|
|
// every static-allocated object (Window, UIRenderer, GPU buffers,
|
|
// event listeners) remains untouched. Callers' code after
|
|
// StartSync() never runs — match that contract on native too.
|
|
Crafter::DomEnv::domStartFrameLoop();
|
|
std::_Exit(0);
|
|
}
|
|
|
|
void Window::StayAlive() {
|
|
// As StartSync, minus the rAF loop.
|
|
//
|
|
// The _Exit(0) is the part that keeps the instance alive: it skips
|
|
// __wasm_call_dtors so every statically allocated object (event
|
|
// listeners, HtmlElementPtr handles, the Window itself) survives, and
|
|
// runtime.js catches the resulting __wasi_proc_exit via a sentinel.
|
|
// The frame loop is a separate concern, and a page that only renders
|
|
// DOM has no use for it — a rAF tick that runs forever to do nothing
|
|
// costs battery on every open tab.
|
|
//
|
|
// Use this for event-driven DOM pages; use StartSync when something
|
|
// actually animates or draws. As with StartSync, no caller code after
|
|
// this point ever runs.
|
|
std::_Exit(0);
|
|
}
|
|
|
|
void Window::StartUpdate() {
|
|
lastFrameBegin = std::chrono::high_resolution_clock::now();
|
|
updating = true;
|
|
}
|
|
|
|
void Window::StopUpdate() {
|
|
updating = false;
|
|
Crafter::DomEnv::domStopFrameLoop();
|
|
}
|
|
|
|
void Window::SetPresentMode(PresentMode mode) {
|
|
// The page presents the canvas; there is no mode to pick.
|
|
presentMode = mode;
|
|
}
|
|
|
|
void Window::Update() {
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
mouseDelta = {currentMousePos.x - lastMousePos.x,
|
|
currentMousePos.y - lastMousePos.y};
|
|
currentFrameTime = {now, now - lastFrameBegin};
|
|
onUpdate.Invoke(currentFrameTime);
|
|
lastMousePos = currentMousePos;
|
|
lastFrameBegin = now;
|
|
}
|
|
|
|
void Window::Render() {
|
|
if (!open) return;
|
|
Crafter::WebGPU::wgpuFrameBegin();
|
|
for (RenderPass* p : passes) {
|
|
if (p) p->Record(/*cmd*/ 0u, currentFrame, *this);
|
|
}
|
|
Crafter::WebGPU::wgpuFrameEnd();
|
|
// After the submit, as on native: what this frame drew is now behind
|
|
// every atlas write to come.
|
|
Device::frameCounter++;
|
|
}
|
|
|
|
WebGPUCommandEncoderRef Window::StartInit() {
|
|
// DOM init: no command buffer needed — texture / buffer creation goes
|
|
// through synchronous wgpu* imports. Return 0 as a placeholder; the
|
|
// value is opaque to user code (auto-typed in HelloUI).
|
|
Crafter::WebGPU::wgpuInit();
|
|
return 0;
|
|
}
|
|
|
|
void Window::FinishInit() {
|
|
// Nothing to submit in DOM mode; all init writes are queued at call
|
|
// time via queue.writeBuffer / writeTexture.
|
|
}
|
|
|
|
// ─── C exports the JS bridge calls back into ──────────────────────────
|
|
|
|
extern "C" {
|
|
__attribute__((export_name("__crafterDom_frame")))
|
|
void __crafterDom_frame(std::int32_t /*handle*/) {
|
|
if (!g_domWindow) return;
|
|
Gamepad::Tick();
|
|
g_domWindow->onBeforeUpdate.Invoke();
|
|
if (g_domWindow->updating) {
|
|
g_domWindow->Update();
|
|
g_domWindow->Render();
|
|
}
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_mouseMove")))
|
|
void __crafterDom_mouseMove(std::int32_t /*handle*/, double x, double y) {
|
|
if (!g_domWindow) return;
|
|
g_domWindow->currentMousePos = {static_cast<float>(x), static_cast<float>(y)};
|
|
g_domWindow->onMouseMove.Invoke();
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_mouseDown")))
|
|
void __crafterDom_mouseDown(std::int32_t /*handle*/, std::int32_t button) {
|
|
if (!g_domWindow) return;
|
|
// MouseEvent.button: 0=left, 1=middle, 2=right
|
|
if (button == 0) {
|
|
g_domWindow->mouseLeftHeld = true;
|
|
g_domWindow->onMouseLeftClick.Invoke();
|
|
} else if (button == 2) {
|
|
g_domWindow->mouseRightHeld = true;
|
|
g_domWindow->onMouseRightClick.Invoke();
|
|
}
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_mouseUp")))
|
|
void __crafterDom_mouseUp(std::int32_t /*handle*/, std::int32_t button) {
|
|
if (!g_domWindow) return;
|
|
if (button == 0) {
|
|
g_domWindow->mouseLeftHeld = false;
|
|
g_domWindow->onMouseLeftRelease.Invoke();
|
|
} else if (button == 2) {
|
|
g_domWindow->mouseRightHeld = false;
|
|
g_domWindow->onMouseRightRelease.Invoke();
|
|
}
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_wheel")))
|
|
void __crafterDom_wheel(std::int32_t /*handle*/, double detents) {
|
|
if (!g_domWindow) return;
|
|
// dom-env.js has already normalized WheelEvent deltas to whole
|
|
// detents (+1 = wheel down) — see __windowListeners.wheel.
|
|
// Window::onMouseScroll is uint32 — preserve sign via two's complement.
|
|
g_domWindow->onMouseScroll.Invoke(static_cast<std::uint32_t>(static_cast<std::int32_t>(detents)));
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_keyDown")))
|
|
void __crafterDom_keyDown(std::int32_t /*handle*/,
|
|
const char* codePtr, std::int32_t codeLen,
|
|
const char* keyPtr, std::int32_t keyLen,
|
|
bool repeat) {
|
|
if (!g_domWindow) return;
|
|
KeyCode code = HashKeyCode(codePtr, static_cast<std::size_t>(codeLen));
|
|
if (repeat) {
|
|
g_domWindow->onRawKeyHold.Invoke(code);
|
|
} else {
|
|
g_domWindow->heldKeys.insert(code);
|
|
g_domWindow->onRawKeyDown.Invoke(code);
|
|
}
|
|
// KeyboardEvent.key is the printable form for character keys, BUT
|
|
// it's also the named identifier for non-character keys —
|
|
// "Backspace", "Shift", "ArrowLeft", "Enter", … Those are
|
|
// multi-byte ASCII strings, and a naive `keyLen > 1` check
|
|
// forwards them straight into input fields as literal text.
|
|
//
|
|
// The reliable discriminator: multi-byte UTF-8 has the high bit
|
|
// set on its first byte (lead byte ≥ 0xC0; continuation bytes
|
|
// ≥ 0x80). Named keys are pure ASCII letters → first byte < 0x80.
|
|
// So forward when:
|
|
// • keyLen == 1 and the byte is a printable ASCII char, OR
|
|
// • keyLen > 1 and the first byte is ≥ 0x80 (true UTF-8).
|
|
// The onRawKeyDown event above already carries the key code for
|
|
// control-key consumers (InputField_OnKey handles Backspace/etc
|
|
// through that channel).
|
|
const auto first = static_cast<unsigned char>(keyPtr[0]);
|
|
if (keyLen == 1 && first >= 0x20 && first != 0x7F) {
|
|
g_domWindow->onTextInput.Invoke(std::string_view(keyPtr, static_cast<std::size_t>(keyLen)));
|
|
} else if (keyLen > 1 && first >= 0x80) {
|
|
// Real multi-byte UTF-8 (non-ASCII printable). Forward as-is —
|
|
// dom-env.js always sends valid UTF-8.
|
|
g_domWindow->onTextInput.Invoke(std::string_view(keyPtr, static_cast<std::size_t>(keyLen)));
|
|
}
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_keyUp")))
|
|
void __crafterDom_keyUp(std::int32_t /*handle*/, const char* codePtr, std::int32_t codeLen) {
|
|
if (!g_domWindow) return;
|
|
KeyCode code = HashKeyCode(codePtr, static_cast<std::size_t>(codeLen));
|
|
g_domWindow->heldKeys.erase(code);
|
|
g_domWindow->onRawKeyUp.Invoke(code);
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_resize")))
|
|
void __crafterDom_resize(std::int32_t /*handle*/, std::int32_t newW, std::int32_t newH) {
|
|
if (!g_domWindow) return;
|
|
g_domWindow->Resize(static_cast<std::uint32_t>(newW),
|
|
static_cast<std::uint32_t>(newH));
|
|
}
|
|
|
|
__attribute__((export_name("__crafterDom_close")))
|
|
void __crafterDom_close(std::int32_t /*handle*/) {
|
|
if (!g_domWindow) return;
|
|
g_domWindow->open = false;
|
|
g_domWindow->onClose.Invoke();
|
|
}
|
|
}
|
|
|
|
#endif // CRAFTER_GRAPHICS_WINDOW_DOM
|