From dc12a3e12df71e89c3a4040d4058c45857958470 Mon Sep 17 00:00:00 2001 From: Babyhamsta <22938086+Babyhamsta@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:03:42 -0600 Subject: [PATCH 1/3] Give a paced frame up to two refreshes, and widen the margin for one that ran long With display.low_latency a frame starts its budget before its refresh: the longest of the last 128 frames, and a margin. The budget left out any frame that took more than a refresh and a half. On a Raspberry Pi 5 in a firefight the game writes a frame in 9 ms while the renderer still draws the one before, 17 ms: 26 ms from input to done, and yet a frame each refresh. Those frames were given too little, started too late for their refresh and were shown at the next one, so the game sat at 30 for as long as the fight lasted. The limit is now the refreshes a frame can be started before its own (FRAME_PACING_QUEUED + 1). A frame that started on time, ran past the margin and missed its refresh now widens the margin as well. Counting its cost alone gives the frames after it exactly that much and no more: where the costs keep creeping up, each new longest frame missed (about one in two hundred on the Pi). This is the shared pacing, so it changes every platform that paces by the display, not only the Pi. frame_pacing_test.c has the new limit's cases. Checked on a Raspberry Pi 5 at 60 Hz; not run on other hardware. --- port/android/host/host_render_stream.c | 29 ++++++++++++++------------ port/android/tests/frame_pacing_test.c | 7 +++++++ port/linux/README.md | 9 +++++--- port/linux/src/frame_pacing.h | 21 ++++++++++++++----- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/port/android/host/host_render_stream.c b/port/android/host/host_render_stream.c index 08ede17255..d2c7faab1b 100644 --- a/port/android/host/host_render_stream.c +++ b/port/android/host/host_render_stream.c @@ -1621,29 +1621,32 @@ void host_render_stream_frame_displayed(uint64_t input, uint64_t finished, uint6 if (latched) { /* (taken a refresh or more after the one it was started for: a - miss. The margin is widened for a frame that started at its time and - whose work was done by the margin before that refresh (or not - known): the display wants its frames sooner than the margin gives. - A frame whose work ran past it took longer than any of the last - ones, which its cost, now counted, gives the frames after it) */ + miss. The margin is widened for a frame that started at its time: + one whose work was done by the margin before that refresh (or not + known), as the display wants its frames sooner than the margin gives; + and one whose work ran past it, which took longer than any of the + last ones. Its cost, now counted, gives the frames after it that + much, and no more: where the frames' costs keep rising past the last + ones' by a little (a firefight growing), each new longest frame would + miss its refresh without the wider margin, one in two hundred of a + Raspberry Pi 5's) */ if (render.targets[frame % 8].frame == frame && render.targets[frame % 8].target && period && latched < render.targets[frame % 8].target + 4 * period) { if (latched > render.targets[frame % 8].target + period / 2) { render.pacing_statistics.missed++; - if (render.targets[frame % 8].waited && (!finished || finished + render.targets[frame % 8].margin <= + if (!render.targets[frame % 8].waited || (finished && finished + render.targets[frame % 8].margin > render.targets[frame % 8].target + FRAME_PACING_MARGIN_STEP_NS / 4)) { - /* (once for the margin it started with: the frames started - before a miss widened it, already on their way, miss too) */ - if (!render.margin_fixed && render.targets[frame % 8].margin >= - (render.costs.margin ? render.costs.margin : FRAME_PACING_MARGIN_NS)) - frame_pacing_margin_missed(&render.costs, period); + render.pacing_statistics.overran++; } - else + /* (once for the margin it started with: the frames started + before a miss widened it, already on their way, miss too) */ + if (render.targets[frame % 8].waited && !render.margin_fixed && render.targets[frame % 8].margin >= + (render.costs.margin ? render.costs.margin : FRAME_PACING_MARGIN_NS)) { - render.pacing_statistics.overran++; + frame_pacing_margin_missed(&render.costs, period); } } else diff --git a/port/android/tests/frame_pacing_test.c b/port/android/tests/frame_pacing_test.c index d849f31c1c..89de76440d 100644 --- a/port/android/tests/frame_pacing_test.c +++ b/port/android/tests/frame_pacing_test.c @@ -74,6 +74,13 @@ static void margin_check(void) /* (and the budget is the costs' most and the margin) */ costs.nanoseconds[3] = 4000000ull; margin_expect("the budget", frame_pacing_budget(&costs, period), 4000000ull + period / 2); + /* (a frame that took more than a refresh, the game's work and the + renderer's overlapping, is given its time: up to the refreshes a frame + can be started before its own; one that took longer than those is not) */ + costs.nanoseconds[4] = period * 7 / 4; + margin_expect("a frame of more than a refresh", frame_pacing_budget(&costs, period), period * 7 / 4 + period / 2); + costs.nanoseconds[5] = period * (FRAME_PACING_QUEUED + 1) + 1; + margin_expect("a frame too long to pace", frame_pacing_budget(&costs, period), period * 7 / 4 + period / 2); } int main(void) diff --git a/port/linux/README.md b/port/linux/README.md index 5c4e3b4266..c5bd41824a 100644 --- a/port/linux/README.md +++ b/port/linux/README.md @@ -2235,9 +2235,12 @@ Windows and Android) paces the game by the display instead (`vulkan/xgpu_vulkan_ waiting, one drawn into. - And not before the time it takes ahead of the refresh it is for: the most a frame took of the last 128, from its input read to its work done - on the GPU, and a margin. A frame that started at its time and was done - that margin early, but still missed its refresh, widens the margin by - 1 ms (a display that takes its frames sooner than the margin gives); 120 + on the GPU, and a margin. A frame may take up to two refreshes so (the + game writes it while the renderer draws the one before, and it starts + that much before its own refresh); one that took longer is not counted. + A frame that started at its time but still missed its refresh widens the + margin by 1 ms (a display that takes its frames sooner than the margin + gives, or a frame that took a little longer than any of the last); 120 frames in time narrow it by 0.1 ms, down to 0.25 ms over the margin that last missed, which itself comes down 0.1 ms a minute. The margin starts at 1.5 ms. diff --git a/port/linux/src/frame_pacing.h b/port/linux/src/frame_pacing.h index f58e6c0c85..a34512f417 100644 --- a/port/linux/src/frame_pacing.h +++ b/port/linux/src/frame_pacing.h @@ -27,7 +27,11 @@ more between the player's hand and the picture. already wait for. The budget is the most a frame took from its start to ready for the display (its renderer done) of the last FRAME_PACING_COSTS, and FRAME_PACING_MARGIN_NS more: frame_pacing_cost_add, - frame_pacing_budget. + frame_pacing_budget. A frame may take longer than a refresh and still be + one of a frame each refresh: the game writes it while the renderer draws + the one before (a Raspberry Pi 5 in a firefight: 9 ms of the game, then + 17 of the renderer, 70 frames a second unpaced), started up to + FRAME_PACING_QUEUED + 1 refreshes before its own. - A display that says when each frame reached the screen (Vulkan's, xgpu_vulkan_pacing.c) has the margin learned: a frame shown a refresh or more after the one it was started for, though it started at its time, @@ -132,9 +136,15 @@ static FRAME_PACING_UNUSED void frame_pacing_cost_add(struct frame_pacing_costs } /* the time to give a frame: the most of the last FRAME_PACING_COSTS, and -the margin; but not more than a refresh and a half (period): a frame that -took longer missed its refresh whenever it started, and an earlier start -for every frame after it would only add to their latency */ +the margin; but not more than the refreshes (period) a frame can be started +before its own, FRAME_PACING_QUEUED + 1 (frame_pacing_may_start: no sooner +than the latch that leaves one frame waiting): a frame that took longer +missed its refresh whenever it started, and an earlier start for every +frame after it would only add to their latency. A frame that takes between +a refresh and a half and two (the game's and the renderer's work +overlapping) is within it: given less, each such frame starts too late for +its refresh and is shown at the next, half the frames a second for as long +as the scene stays so. */ static FRAME_PACING_UNUSED uint64_t frame_pacing_budget(const struct frame_pacing_costs *costs, uint64_t period) { uint64_t most = 0; @@ -142,7 +152,8 @@ static FRAME_PACING_UNUSED uint64_t frame_pacing_budget(const struct frame_pacin for (index = 0; index < FRAME_PACING_COSTS; index++) { - if (costs->nanoseconds[index] > most && (!period || costs->nanoseconds[index] <= period * 3 / 2)) + if (costs->nanoseconds[index] > most && + (!period || costs->nanoseconds[index] <= period * (FRAME_PACING_QUEUED + 1))) most = costs->nanoseconds[index]; } return most + (costs->margin ? costs->margin : FRAME_PACING_MARGIN_NS); From a15bbb0883eb440712bbf892f5dfca426568f941 Mon Sep 17 00:00:00 2001 From: Babyhamsta <22938086+Babyhamsta@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:03:42 -0600 Subject: [PATCH 2/3] Bound a light's and a shadow's draws, and make no alpha test that cannot fail, where a backend asks A tile-based GPU pays for a pass by the pixels it shades. On a Raspberry Pi 5 (Broadcom's V3D) in a firefight most of a frame went to pixels that could not change: a plasma bolt's light and an object's shadow are drawn over whole surfaces of the level, the enhanced lights' shader runs its loop's set-up for every pixel of the level whether a light is near or not, and V3D makes no early depth test for a shader that may discard, so every alpha-tested draw shades its hidden pixels too. This is the front end's and the game's half of what the Pi needed. Each part is behind a capability of the backend's (xgpu_backend_capabilities), and none is set here: the next commit sets them in the Vulkan backend, on V3D alone. - draw_bounds: a dynamic light's and a shadow's draws over the level say what part of the view they can change (port/linux/game/draw_bounds.c: the light's radius, the shadow's volume, and how far past them the textures' borders are still sampled), and the front end scissors them to it. With display.dynamic_lights enhanced, the lightmap pass's triangles are drawn in two parts, those in some light's reach by the lit shader and the rest by the shader without the lights' loop; a model no light reaches is drawn so too (halo_model_bounds, from the model's sphere at the tick and where the frame draws it). - alpha_test_drop: a draw whose alpha is a texture's alone, no texel of which fails the test, is drawn without the test. A texture's least alpha is read from its texels (xgpu_texture_minimum_alpha: DXT1, DXT3, DXT5, A8R8G8B8; not known for the others, and for Custom Edition's channel orders) and kept while they are unchanged. - clear_alone: each region's cell of the shadow atlas is cleared as its turn comes, the rest of the atlas left as it was. - cutout_prepass: an alpha-tested model's depth first, then its colour where the depth is its own. This one is not exact, a pixel the test cuts away at exactly the depth of the one seen there is drawn too, so no backend asks for it by itself (HALO_VULKAN_CUTOUT_PREPASS=1). The first three draw the same pixels; the frames compared are in the next commit's message, which turns them on. Checked: tools/test_texture_alpha.py (new: the least alpha against the decoder's own alphas of the same texels, random and shaped blocks of each format) passes on a Raspberry Pi 5. This commit alone builds there (ninja linux_arm64). Not built for Windows, Android, macOS or iOS. --- AGENTS.md | 1 + port/linux/README.md | 3 + port/linux/game/draw_bounds.c | 155 ++++ port/linux/game/draw_bounds.h | 32 + port/linux/game/render_interpolation.c | 17 + port/linux/game/render_prepared.c | 6 +- port/linux/include/halo_linux_source_fixups.h | 24 + port/linux/src/d3d8_device.c | 672 +++++++++++++++++- port/linux/src/xbox_texture_decode.c | 99 +++ port/linux/src/xgpu.h | 8 + port/linux/src/xgpu_backend.h | 27 +- port/linux/src/xgpu_stream.h | 4 +- port/linux/src/xgpu_stream_encode.c | 4 + port/linux/tests/texture_alpha_test.c | 193 +++++ .../xbox/rasterizer_xbox_active_camouflage.c | 3 + .../xbox/rasterizer_xbox_environment.c | 27 + .../rasterizer/xbox/rasterizer_xbox_models.c | 32 +- .../rasterizer/xbox/rasterizer_xbox_shadows.c | 76 +- tools/test_texture_alpha.py | 46 ++ 19 files changed, 1418 insertions(+), 11 deletions(-) create mode 100644 port/linux/game/draw_bounds.c create mode 100644 port/linux/game/draw_bounds.h create mode 100644 port/linux/tests/texture_alpha_test.c create mode 100644 tools/test_texture_alpha.py diff --git a/AGENTS.md b/AGENTS.md index d65f21b844..c313b4da93 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -236,6 +236,7 @@ python -m pytest -q tools/test_hdr_backends.py # display.hdr across python -m pytest -q tools/test_macos_app.py # the macOS app's Info.plist, entitlements, signature python -m pytest -q tools/test_hud_assets.py # the high-res HUD's assets: their sources, credit, the classic buttons' cut-out letters python -m pytest -q tools/test_astc.py # the DXT textures as ASTC: the transcoding, the encoder's bounds, the disk cache +python -m pytest -q tools/test_texture_alpha.py # a texture's least alpha, which an alpha test is dropped by, against the decoder's alphas python -m pytest -q tools/test_ios_app.py # the iOS app's Info.plist, entitlements, the guest's code section build/macos/halo_replay --shots # a recorded render stream replayed (ninja macos_replay; # HALO_RENDER_RECORD: port/macos/README.md) diff --git a/port/linux/README.md b/port/linux/README.md index c5bd41824a..fa49b0f492 100644 --- a/port/linux/README.md +++ b/port/linux/README.md @@ -7682,6 +7682,9 @@ Other changes: | `sound/sound_manager.c`, `interface/hud.c`, `game/game_engine.c` | The music's and the other sounds' volumes; the HUD's and the scoreboard's settings are read again when Settings changes them. | | `rasterizer/xbox/rasterizer_xbox_shadows.c` | With larger shadow maps (`display.shadow_resolution`), the blur's taps stay half a texel apart, and more passes widen it to cover the same part of the map as on the Xbox. | | `rasterizer/xbox/rasterizer_xbox_shadows.c`, `render/render_objects.c`, `rasterizer/rasterizer.h` | The shadow atlas: a frame's shadow casters (up to 9, with maps up to 512) are drawn into regions of an atlas first, the blur's passes run once over all of them (one quad a region, the guard bands between them black, as a map's border reads), the last into a map of each region's own, and then each object's receivers are drawn sampling its map. A frame's N shadows take 6 + N passes at 512 rather than 7N; they look the same. More casters, 1024 maps and the debugging options draw them one at a time as before. | +| `rasterizer/xbox/rasterizer_xbox_shadows.c` | Where a pass costs by the area it covers (a tile-based GPU: `HALO_VULKAN_CLEAR_ALONE`), each region's cell of the shadow atlas is emptied as its turn comes, and the rest of the atlas left as it was, which nothing samples. The pixels drawn are the same. | +| `rasterizer/xbox/rasterizer_xbox_environment.c`, `rasterizer/xbox/rasterizer_xbox_shadows.c` | A dynamic light's and a shadow's draws over the level say what part of the view they can change (`port/linux/game/draw_bounds.c`: the light's radius, the shadow's volume, and how far past them the textures' borders are still sampled), and the front end scissors them to it where the pixels shaded are the cost (`HALO_VULKAN_DRAW_BOUNDS`). The same pixels are lit and shadowed. | +| `rasterizer/xbox/rasterizer_xbox_models.c`, `rasterizer/xbox/rasterizer_xbox_active_camouflage.c` | A model's bounding sphere is given to the front end while the model is drawn (`halo_model_bounds`): with the enhanced dynamic lights a model none of them reaches is given no point lights and drawn by the shader without the lights' loop, and with `HALO_VULKAN_CUTOUT_PREPASS=1` a large alpha-tested model's depth is drawn first (`HALO_VULKAN_DRAW_BOUNDS`). A camouflaged model has no sphere: its radius is 0, not what the stack held. | | `rasterizer/rasterizer.c` | The 128 KB of vertices of the debug display of a model's vertices are static, not on the stack: the compiler inlines the function in `render_model`, and the Windows build probed the stack a page at a time for every model drawn. | | `render/render.c` | The 3D view of each window is tone mapped (`display.hdr`) and antialiased (`display.anti_aliasing`) before the HUD is drawn. The projectiles' lights' glows are drawn after the transparent geometry (`display.dynamic_lights`). | | `render/render.c` | `render_initialize` reads the structure culling tests from `display.structure_culling`. | diff --git a/port/linux/game/draw_bounds.c b/port/linux/game/draw_bounds.c new file mode 100644 index 0000000000..365fedd225 --- /dev/null +++ b/port/linux/game/draw_bounds.c @@ -0,0 +1,155 @@ +/* +DRAW_BOUNDS.C + +The part of a view that a pass over the level can change. + +A dynamic light and an object's shadow are drawn onto the level as the +level's own surfaces, whole: every surface the light's sphere or the +shadow's volume touches (structure_render.c). A surface of the level is +often many times the size of the light or the shadow on it, and what the +pass draws beyond them changes nothing: the light's distance attenuation +and the shadow's map are sampled by their border there, which adds no light +and takes none away. Those pixels are still shaded and blended, and in a +fight, where every plasma bolt is a light and every body casts a shadow, +they were a quarter of a Raspberry Pi 5's frame. + +So the pass says what it is bounded by (the light's reach as a cube, the +shadow's volume as a box) and its draws are scissored to the box's place in +the view (halo_draw_bounds, port/linux/src/d3d8_device.c): the pixels drawn +are the same ones, only fewer are shaded. A box that reaches behind the +eye bounds nothing, and its pass is drawn whole as before. +*/ + +#include "cseries.h" +#include "math/real_math.h" +#include "draw_bounds.h" +#include "rasterizer/rasterizer.h" +#include "render/render_cameras.h" + +/* ---------- constants */ + +enum +{ + NUMBER_OF_BOX_CORNERS = 8 +}; + +enum +{ + /* (the rasterizer's files' own, each: rasterizer_xbox.c) */ + _rasterizer_target_render_primary = 0 +}; + +/* how near the eye's plane a corner of a box may be for its place in the +view to bound anything (the projection's w, world units) */ +#define DRAW_BOUNDS_NEAREST_CORNER 0.001f + +/* ---------- public code */ + +void draw_bounds_box( + real_point3d const *centre, + real_vector3d const *forward, + real_vector3d const *left, + real_vector3d const *up, + real const low[3], + real const high[3]) +{ + real const width = (real)(global_window_parameters.camera.viewport_bounds.x1 - + global_window_parameters.camera.viewport_bounds.x0); + real const height = (real)(global_window_parameters.camera.viewport_bounds.y1 - + global_window_parameters.camera.viewport_bounds.y0); + float rectangle[4]; + short corner; + + /* (the window's view of the world alone, not a mirror's: the others + are drawn through projections of their own) */ + if (global_window_parameters.rasterizer_target != _rasterizer_target_render_primary || + global_window_parameters.camera.mirrored) + { + halo_draw_bounds(NULL); + return; + } + + rectangle[0] = width; + rectangle[1] = height; + rectangle[2] = 0.0f; + rectangle[3] = 0.0f; + for (corner = 0; corner < NUMBER_OF_BOX_CORNERS; corner++) + { + real const along_forward = (corner & 1) ? high[0] : low[0]; + real const along_left = (corner & 2) ? high[1] : low[1]; + real const along_up = (corner & 4) ? high[2] : low[2]; + real_point3d point; + real_point3d view_point; + real w; + real x; + real y; + + point.x = centre->x + forward->i*along_forward + left->i*along_left + up->i*along_up; + point.y = centre->y + forward->j*along_forward + left->j*along_left + up->j*along_up; + point.z = centre->z + forward->k*along_forward + left->k*along_left + up->k*along_up; + matrix4x3_transform_point( + &global_window_parameters.frustum.world_to_view, + &point, + &view_point); + w = + global_window_parameters.frustum.projection_matrix[0][3] * view_point.x + + global_window_parameters.frustum.projection_matrix[1][3] * view_point.y + + global_window_parameters.frustum.projection_matrix[2][3] * view_point.z + + global_window_parameters.frustum.projection_matrix[3][3]; + if (!(w > DRAW_BOUNDS_NEAREST_CORNER)) + { + halo_draw_bounds(NULL); + return; + } + + /* (the viewport's pixels from its left and top, by their edges; as + rasterizer_project_billboard has a point's, by their middles) */ + x = (( + global_window_parameters.frustum.projection_matrix[0][0] * view_point.x + + global_window_parameters.frustum.projection_matrix[1][0] * view_point.y + + global_window_parameters.frustum.projection_matrix[2][0] * view_point.z + + global_window_parameters.frustum.projection_matrix[3][0]) / w + 1.0f) * width * 0.5f; + y = (1.0f - ( + global_window_parameters.frustum.projection_matrix[0][1] * view_point.x + + global_window_parameters.frustum.projection_matrix[1][1] * view_point.y + + global_window_parameters.frustum.projection_matrix[2][1] * view_point.z + + global_window_parameters.frustum.projection_matrix[3][1]) / w) * height * 0.5f; + rectangle[0] = MIN(rectangle[0], x); + rectangle[1] = MIN(rectangle[1], y); + rectangle[2] = MAX(rectangle[2], x); + rectangle[3] = MAX(rectangle[3], y); + } + + /* (the part of it in the viewport and a pixel around it, which the + front end's own placing of the pixels may still bring in: none, if the + right is not past the left or the bottom past the top) */ + rectangle[0] = MAX(rectangle[0], -1.0f); + rectangle[1] = MAX(rectangle[1], -1.0f); + rectangle[2] = MIN(rectangle[2], width + 1.0f); + rectangle[3] = MIN(rectangle[3], height + 1.0f); + halo_draw_bounds(rectangle); + + return; +} + +void draw_bounds_cube( + real_point3d const *centre, + real half_width) +{ + real low[3]; + real high[3]; + + low[0] = low[1] = low[2] = -half_width; + high[0] = high[1] = high[2] = half_width; + draw_bounds_box(centre, global_forward3d, global_left3d, global_up3d, low, high); + + return; +} + +void draw_bounds_none( + void) +{ + halo_draw_bounds(NULL); + + return; +} diff --git a/port/linux/game/draw_bounds.h b/port/linux/game/draw_bounds.h new file mode 100644 index 0000000000..f607e517b1 --- /dev/null +++ b/port/linux/game/draw_bounds.h @@ -0,0 +1,32 @@ +/* +DRAW_BOUNDS.H + +The part of a view that a pass over the level can change (draw_bounds.c). +*/ + +#ifndef DRAW_BOUNDS_H +#define DRAW_BOUNDS_H + +#include "math/real_math.h" + +/* the draws from now on change no pixel of the view outside a box (world +units): its middle, its three axes (unit vectors), and how far it reaches +along each of them, from low to high */ +void draw_bounds_box( + real_point3d const *centre, + real_vector3d const *forward, + real_vector3d const *left, + real_vector3d const *up, + real const low[3], + real const high[3]); + +/* ... outside a cube about a point, the world's axes its own */ +void draw_bounds_cube( + real_point3d const *centre, + real half_width); + +/* the draws from now on are not bounded */ +void draw_bounds_none( + void); + +#endif diff --git a/port/linux/game/render_interpolation.c b/port/linux/game/render_interpolation.c index 4af5c62cc5..e6fe38b9d7 100644 --- a/port/linux/game/render_interpolation.c +++ b/port/linux/game/render_interpolation.c @@ -2584,6 +2584,23 @@ void render_interpolation_object_point(long object_index, real_point3d *point) matrix4x3_transform_point(&drawn[0], &local, point); } +/* the middle of a model's bounding sphere where the frame draws the model: +moved with the object (render_interpolation_object_point) if the model is +an object's own, drawn about its sphere (render_objects.c gives render_model +the object's index and its sphere's centre); as given for any other, whose +identifier need be no object's (a flag's cloth, a first-person weapon) */ +void render_interpolation_model_centre(long unique_identifier, real_point3d const *centre, real_point3d *drawn) +{ + struct object_datum *object; + + *drawn = *centre; + if (unique_identifier == NONE || !DATUM_INDEX_TO_IDENTIFIER(unique_identifier)) + return; + object = object_try_and_get(unique_identifier); + if (object && !csmemcmp(&object->object.bounding_sphere_center, centre, sizeof(*centre))) + render_interpolation_object_point(unique_identifier, drawn); +} + /* A point the game works out from an object's state reads some of it as the tick left it (its position, its bounding sphere) and some as a frame draws it (its nodes and markers, object_get_node_matrices): a unit's camera on foot diff --git a/port/linux/game/render_prepared.c b/port/linux/game/render_prepared.c index 26833540ef..6b05ae2492 100644 --- a/port/linux/game/render_prepared.c +++ b/port/linux/game/render_prepared.c @@ -21,7 +21,11 @@ front end turns into pipelines and targets as the settings and the screen are now (prepared_settings says of each setting why), and the walk draws, as it records, what every setting's would (the enhanced lights' passes, the ambient occlusion's pre-pass and Panini's pass, whatever the settings: -the front end plays back what the settings now want of them). Played back +the front end plays back what the settings now want of them). (One choice +of the backend's is in what the walk draws: a cut-out's depth first, beside +the cut-out as one draw, where xgpu_backend_capabilities.cutout_prepass +(HALO_VULKAN_CUTOUT_PREPASS=1). A record made without it leaves those two +draws' pipelines to be made in play.) Played back under the settings, backend and GPU it was made with (its derivation), the map's enumeration (the set of distinct draws as the front end describes them: xgpu_manifest_enumeration) must be the record's; under others, which diff --git a/port/linux/include/halo_linux_source_fixups.h b/port/linux/include/halo_linux_source_fixups.h index 2b11b1c6eb..4924fc98f9 100644 --- a/port/linux/include/halo_linux_source_fixups.h +++ b/port/linux/include/halo_linux_source_fixups.h @@ -79,6 +79,10 @@ void render_interpolation_frame_end(void); float render_interpolation_fraction(void); struct real_matrix4x3 *render_interpolation_object_node_matrices(long object_index); void render_interpolation_object_point(long object_index, union real_point3d *point); +/* a model's bounding sphere's centre where the frame draws the model, if it +is an object's own (render_interpolation.c) */ +void render_interpolation_model_centre(long unique_identifier, union real_point3d const *centre, + union real_point3d *drawn); /* an object's origin and a unit's camera where a frame draws them, not mixed of the tick's state and the frame's (render_interpolation.c) */ void render_interpolation_object_origin(long object_index, union real_point3d *origin); @@ -279,12 +283,24 @@ it kept, and the range it had in previous (NULL: not wanted): a range in the last step before 1 draws behind everything drawn, still clipped at the far plane (port/linux/src/d3d8_device.c) */ void d3d8_viewport_depth_range(float minimum, float maximum, float previous[2]); +/* the rectangle of the viewport that the draws from now on can change +pixels in (left, top, right, bottom, in the viewport's pixels from its left +and top; none of it, if the right is not past the left or the bottom past +the top), to which they are scissored where the backend gains by it; NULL: +the whole viewport, as before (port/linux/src/d3d8_device.c, "the draws' +bounds"; from port/linux/game/draw_bounds.c) */ +void halo_draw_bounds(const float *rectangle); /* the stage samples a region of its texture as a texture of the region's alone, addressed by its border beyond it (D3DTADDRESS_BORDER, whatever the stage's address modes): region its width and height, then its left and top, in the texture's coordinates (0 to 1); NULL: the whole texture, as the stage's address modes have it (port/linux/src/d3d8_device.c) */ void d3d8_texture_region(unsigned long stage, const float *region); +/* the viewport's rectangle of the colour target cleared, the rest of the +target of no use from now on; and whether the renderer gains by it: a pass +then costs by the part of the target it covers (port/linux/src/d3d8_device.c) */ +int d3d8_clears_alone(void); +void d3d8_clear_viewport_alone(unsigned long color); /* takes up a new width between frames (F11); returns the width */ long halo_screen_commit(void); /* the shadow maps' pixels for each of their 128 texels each way, a power of @@ -321,6 +337,14 @@ preparation's record) */ void halo_screen_panini(short x0, short y0, short x1, short y1, const float projection[8]); /* the enhanced dynamic lights (display.dynamic_lights) */ #include "halo_dynamic_lights.h" + +/* the model the game's draws are of from now on (port/linux/src/d3d8_device.c): +its bounding sphere (the tick's), its middle as the frame draws it, the +middle's distance from the camera, whether it is a first-person model; a +NULL centre or no radius: not known (no model, or one without a sphere). +Nonzero if no enhanced dynamic light reaches it: it is drawn as with no +light, and the game gives it no point lights */ +int halo_model_bounds(const float centre[3], const float drawn[3], float radius, float distance, int first_person); /* display.hdr's tone map of a window's 3D view, before its anti-aliasing (source/render/render.c), and what the game's renderer tells it of its draws (port/linux/src/d3d8_device.c, xgpu_hdr.h): the category drawing now diff --git a/port/linux/src/d3d8_device.c b/port/linux/src/d3d8_device.c index 6caf15cddb..a2479d203c 100644 --- a/port/linux/src/d3d8_device.c +++ b/port/linux/src/d3d8_device.c @@ -28,6 +28,7 @@ Conventions carried over from the Xbox: #include "xgpu_shader.h" #include "xgpu_backend.h" #include "xgpu_hdr.h" +#include "xgpu_images.h" #include "xgpu_manifest.h" #include "xgpu_stream.h" #include "frame_pacing.h" @@ -3417,12 +3418,340 @@ the game renders each level as a surface of its own (the water's ripple map: the backend copies the levels' targets into a mipmapped texture, again only once a level's target has been drawn into since). */ +/* ---------- an alpha test that cannot fail + +The game draws nearly every model with the alpha test on (alpha greater +than 0x7f: rasterizer_xbox_models.c), its alpha the base map's, and most +base maps have no texel that fails it. A pixel shader that may discard +keeps a tile-based GPU from rejecting hidden pixels by their depth before +it shades them, and from recording the depth of the ones it draws until +after: Broadcom's V3D shaded a campaign view a third longer for it. So +where the draw's alpha is a texture's alone and the least alpha of that +texture's texels passes the test, the draw's key has no alpha test +(prepare_draw): what is drawn is the same. Only for such a GPU +(xgpu_backend_capabilities.alpha_test_drop): elsewhere the keys are the +game's, as the renderers' manifests have them. A texture's least alpha is +kept while its texels are unchanged (memory_watch.c). */ + +#define ALPHA_TEXTURES 1024 +/* how far above the reference a texture's least alpha must be, when it is +not 255 (a filter's weights of 255s make 255 exactly; of less, a GPU's +fixed point and its DXT5 alphas may make a little less than the least) */ +#define ALPHA_MARGIN 4 + +static struct +{ + DWORD data, format, size; + unsigned long generation; + BOOL known; + int minimum; +} alpha_textures[ALPHA_TEXTURES]; + +/* the least alpha of each stage's texture, -1 not known, of the draw being +prepared (bind_textures) */ +static int stage_alpha_minimum[D3DTSS_MAXSTAGES]; + +static int texture_alpha_minimum(const D3DBaseTexture *texture, int stage) +{ + struct xgpu_texture_description description; + unsigned long address, bytes, generation, slot; + + xgpu_texture_describe(texture->Format, texture->Size, &description); + if (description.cube_map || description.depth > 1) + return -1; + address = (unsigned long)PLATFORM_PHYSICAL_TO_VIRTUAL(texture->Data); + /* (a Custom Edition map's texels sampled in another order of channels: + the alpha sampled is not the one stored, xbox_textures.c) */ + if (xgpu_texture_channels(address, stage)) + return -1; + bytes = xgpu_texture_face_size(&description); + if (!bytes || !platform_is_contiguous((void *)address) || + bytes > PLATFORM_CONTIGUOUS_SIZE - (address - PLATFORM_CONTIGUOUS_BASE)) + return -1; + /* (the product's high bits: the texels' addresses are multiples of a page) */ + slot = ((DWORD)((texture->Data ^ texture->Format * 0x85ebca6bu ^ texture->Size * 0xc2b2ae35u) * 0x9e3779b1u) >> 16) % + ALPHA_TEXTURES; + generation = memory_watch_generation(address, bytes); + if (alpha_textures[slot].known && alpha_textures[slot].data == texture->Data && + alpha_textures[slot].format == texture->Format && alpha_textures[slot].size == texture->Size && + alpha_textures[slot].generation == generation) + return alpha_textures[slot].minimum; + /* (its texels watched from here: a write makes it be read again) */ + memory_watch_protect(address, bytes); + alpha_textures[slot].known = TRUE; + alpha_textures[slot].data = texture->Data; + alpha_textures[slot].format = texture->Format; + alpha_textures[slot].size = texture->Size; + alpha_textures[slot].generation = memory_watch_generation(address, bytes); + alpha_textures[slot].minimum = xgpu_texture_minimum_alpha(&description, (const unsigned char *)address); + return alpha_textures[slot].minimum; +} + +/* whether the draw's alpha test passes every pixel: its alpha (the final +combiner's G) is a texture stage's alpha as sampled, which no combiner +stage writes, and the least alpha of the texture's texels +(stage_alpha_minimum) passes the test */ +static int alpha_test_stage(void) +{ + const DWORD *state = D3D__RenderState; + DWORD final_abcd = state[D3DRS_PSFINALCOMBINERINPUTSABCD], final_efg = state[D3DRS_PSFINALCOMBINERINPUTSEFG]; + unsigned long source = (final_efg >> 8) & 0xff, reg = source & 0x0f; + int count = (int)(state[D3DRS_PSCOMBINERCOUNT] & 0xff), stage; + + /* (no final combiner: r0's alpha, which the stages make) */ + if (!final_abcd && !final_efg) + return -1; + /* (the register's alpha, not its blue, as it is: not inverted) */ + if ((source & 0xf0) != 0x10 || reg < XGPU_REGISTER_T0 || reg > XGPU_REGISTER_T3) + return -1; + if (count > 8) + count = 8; + for (stage = 0; stage < count; stage++) + { + DWORD alpha = state[D3DRS_PSALPHAOUTPUTS0 + stage], rgb = state[D3DRS_PSRGBOUTPUTS0 + stage]; + + if (((alpha >> 4) & 0x0f) == reg || (alpha & 0x0f) == reg || ((alpha >> 8) & 0x0f) == reg) + return -1; + /* (a colour portion's blue copied to its destination's alpha) */ + if ((((rgb >> 12) & 0x80) && ((rgb >> 4) & 0x0f) == reg) || (((rgb >> 12) & 0x40) && (rgb & 0x0f) == reg)) + return -1; + } + return (int)(reg - XGPU_REGISTER_T0); +} + +/* whether an alpha that is never less than this passes the draw's test */ +static BOOL alpha_minimum_passes(int minimum) +{ + const DWORD *state = D3D__RenderState; + int reference = (int)(state[D3DRS_ALPHAREF] & 0xff); + + if (minimum < 0) + return FALSE; + /* (every texel 255: the alpha is 255 exactly) */ + if (minimum == 255) + { + switch (state[D3DRS_ALPHAFUNC]) + { + case D3DCMP_GREATER: return reference < 255; + case D3DCMP_GREATEREQUAL: return TRUE; + case D3DCMP_NOTEQUAL: return reference != 255; + case D3DCMP_EQUAL: case D3DCMP_LESSEQUAL: return reference == 255; + default: return FALSE; + } + } + switch (state[D3DRS_ALPHAFUNC]) + { + case D3DCMP_GREATER: case D3DCMP_GREATEREQUAL: return minimum - ALPHA_MARGIN > reference; + default: return FALSE; + } +} + +static BOOL alpha_test_always_passes(void) +{ + int stage = alpha_test_stage(); + + return stage >= 0 && alpha_minimum_passes(stage_alpha_minimum[stage]); +} + +/* ---------- the model being drawn + +The game says what model its draws are of while it draws one +(halo_model_bounds, from rasterizer_xbox_models.c): its bounding sphere, +how far its middle is from the camera, whether it is a first-person model +(drawn about the camera whatever its sphere says). The sphere is the +object's as the last tick left it; a frame draws the object between two +ticks, or gliding to where the host has it, so its middle as drawn comes +too, and what is asked of the bounds is asked about both. The draws between are +decided by it: whether a cut-out's depth is drawn first (below), whether +any dynamic light reaches it (dynamic_lights_model_unreached). */ + +static struct +{ + BOOL known, first_person; + /* (the middle: the tick's, the frame's) */ + float centre[2][3], radius, distance; +} model_bounds; + +/* how far from the camera a first-person model reaches at most (world units) */ +#define MODEL_BOUNDS_FIRST_PERSON_REACH 1.5f +/* how far from its middle a model reaches at most: its sphere's radius (the +object's tag's, which the game itself takes the object's extent from) and a +margin, the frame's pose not the tick's the sphere was made about */ +#define MODEL_BOUNDS_REACH(radius) ((radius) * 1.25f + 0.25f) + +/* ---------- the draws' bounds + +A pass that lights or shadows a part of the level draws whole surfaces of +it, and leaves most of their pixels as they were: beyond a dynamic light's +reach, beside a shadow's map (port/linux/game/draw_bounds.c). The game says +which rectangle of the viewport the pass can change (halo_draw_bounds), and +the draws until it says otherwise are scissored to it +(xgpu_backend_capabilities.draw_bounds): the same pixels come out, and +those left as they were are not shaded and blended to be. Not in VR's eye +pass, where each eye sees the level from a place of its own, not the +game's camera's. */ + +static struct +{ + BOOL known; + /* (left, top, right, bottom: the viewport's pixels, in the game's units) */ + float rectangle[4]; +} draw_bounds; + +void halo_draw_bounds(const float *rectangle) +{ + draw_bounds.known = rectangle != NULL; + if (rectangle) + memcpy(draw_bounds.rectangle, rectangle, sizeof(draw_bounds.rectangle)); +} + +/* a draw's scissor rectangle (the viewport's: left, top, width, height in +the targets' pixels) brought in to the draws' bounds. The vertex shaders +draw half a unit of the game's right of and below the game's projection +(pixel centres at half-integers: xgpu_spirv.c's clip position), which is +more than a pixel of a target of more than two pixels a unit: the right and +bottom edges are taken that half unit farther, the left and top not, and +each edge a pixel farther out than its place rounds to */ +static void draw_bounds_scissor(long scissor[4]) +{ + long left, top, right, bottom; + + if (!draw_bounds.known || !xgpu_backend_capabilities.draw_bounds || scissor[2] <= 0 || scissor[3] <= 0) + return; +#ifdef HALO_VR + if (vr_gl.pass == HALO_VR_PASS_EYES) + return; +#endif + left = target_pixel((float)(device.viewport.X + UI_OFFSET) + draw_bounds.rectangle[0], 0) - 1; + top = target_pixel((float)device.viewport.Y + draw_bounds.rectangle[1], 1) - 1; + right = target_pixel((float)(device.viewport.X + UI_OFFSET) + draw_bounds.rectangle[2] + 0.5f, 0) + 1; + bottom = target_pixel((float)device.viewport.Y + draw_bounds.rectangle[3] + 0.5f, 1) + 1; + if (left < scissor[0]) + left = scissor[0]; + if (top < scissor[1]) + top = scissor[1]; + if (right > scissor[0] + scissor[2]) + right = scissor[0] + scissor[2]; + if (bottom > scissor[1] + scissor[3]) + bottom = scissor[1] + scissor[3]; + /* (none of the bounds in the viewport: one pixel of it, which the draws + leave as it was like the rest; an API may refuse an empty rectangle) */ + if (draw_bounds.rectangle[2] <= draw_bounds.rectangle[0] || draw_bounds.rectangle[3] <= draw_bounds.rectangle[1] || + right <= left || bottom <= top) + { + left = scissor[0]; + top = scissor[1]; + right = left + 1; + bottom = top + 1; + } + scissor[0] = left; + scissor[1] = top; + scissor[2] = right - left; + scissor[3] = bottom - top; +} + +/* ---------- a cut-out's depth first + +Where a pixel shader that may discard is not tested by its depth before it +runs (Broadcom's V3D), every +pixel of an alpha-tested model is shaded whole, the hidden ones too, and +the ones it draws hide nothing after them until they are shaded. An opaque +alpha-tested draw that writes its depth, whose alpha is a texture's alone +(alpha_test_stage), is drawn twice instead: its depth alone, by a pixel +shader that samples that texture and tests its alpha and no more; then +itself with no alpha test, where the depth is equal to what was written: +the pixels the test passed that nothing nearer hides, each shaded once. + +The two are the one draw where its depth test passes an equal depth +(D3DCMP_LESSEQUAL, the game's: the last of two of its pixels at one depth +is seen either way) and its stencil is the same for the second as for the +first (kept, or replaced where the test is passed by a test the replaced +value passes too): any other draw is drawn as it is. One case is left: a +pixel the test cuts away that lies at exactly the depth of the pixel seen +there (two of the draw's triangles crossing, or lying in one plane with +other texels) is drawn by the second draw. A fight among pines on a +Raspberry Pi 5 had up to five such pixels in half of its frames: so no +backend asks for the two draws by itself, only who sets +HALO_VULKAN_CUTOUT_PREPASS=1 (xgpu_backend_capabilities.cutout_prepass). */ + +/* The depth's draw costs the model's vertices again and a pass over its +pixels, so it is made only where it saves more: for a model lit for each +pixel (the costly shader) that is large in the view, its radius at least +this much of its distance (model_bounds). Measured on a Raspberry Pi 5: +from 0.15 to 0.3 a fight among pines keeps all of the gain (its worst +second 53 frames a second to 65) and the levels' calm views lose under 2%; +with no least size they lose up to 6%, and past 0.6 the gain is gone. */ +#define CUTOUT_LEAST_SIZE 0.25f + +/* the first of the two is being prepared (prepare_draw: lit by nothing) */ +static BOOL cutout_depth_drawing; + +/* whether the stencil's test and writes are the same made twice */ +static BOOL cutout_stencil_repeatable(void) +{ + const DWORD *state = D3D__RenderState; + + if (!state[D3DRS_STENCILENABLE]) + return TRUE; + if (state[D3DRS_STENCILFAIL] != D3DSTENCILOP_KEEP || state[D3DRS_STENCILZFAIL] != D3DSTENCILOP_KEEP) + return FALSE; + return state[D3DRS_STENCILPASS] == D3DSTENCILOP_KEEP || (state[D3DRS_STENCILPASS] == D3DSTENCILOP_REPLACE && + (state[D3DRS_STENCILFUNC] == D3DCMP_ALWAYS || state[D3DRS_STENCILFUNC] == D3DCMP_EQUAL)); +} + +static BOOL per_pixel_lighting(void); + +/* the texture stage of the draw's alpha, where it is drawn so; -1 */ +static int cutout_stage(void) +{ + const DWORD *state = D3D__RenderState; + D3DBaseTexture *texture; + int stage, other; + + if (!xgpu_backend_capabilities.cutout_prepass || !state[D3DRS_ALPHATESTENABLE] || state[D3DRS_ALPHABLENDENABLE] || + !state[D3DRS_ZENABLE] || !state[D3DRS_ZWRITEENABLE] || device.visibility_test_active || sampled_view || + state[D3DRS_ZFUNC] != D3DCMP_LESSEQUAL || + (state[D3DRS_ALPHAFUNC] != D3DCMP_GREATER && state[D3DRS_ALPHAFUNC] != D3DCMP_GREATEREQUAL) || + !(state[D3DRS_COLORWRITEENABLE] & D3DCOLORWRITEENABLE_ALL) || !cutout_stencil_repeatable()) + return -1; + for (other = 0; other < D3DTSS_MAXSTAGES; other++) + { + if (D3D__TextureState[other][D3DTSS_ALPHAKILL] == D3DTALPHAKILL_ENABLE) + return -1; + } + stage = alpha_test_stage(); + if (stage < 0 || stage_texture_mode(stage) != 0x01) + return -1; + texture = device.textures[stage]; + if (!texture || !texture->Data || xgpu_render_target_find(texture->Data)) + return -1; + /* (a test that cannot fail is not made at all: prepare_draw) */ + if (alpha_minimum_passes(texture_alpha_minimum(texture, stage))) + return -1; + { + struct vertex_shader_object *program = current_program(); + + if (!program || !program->lighting.lights || program->lighting_failed || !per_pixel_lighting()) + return -1; + /* (a map's preparation draws it both ways whatever its size: + draw_indexed_cutout) */ + if (!preparation.active && (!model_bounds.known || (!model_bounds.first_person && + model_bounds.radius < model_bounds.distance * CUTOUT_LEAST_SIZE))) + { + return -1; + } + } + return stage; +} + static void bind_textures(struct nv2a_pixel_shader_key *key, float texture_scale[4][4]) { struct xgpu_texture_source sources[D3DTSS_MAXSTAGES]; int stage; memset(sources, 0, sizeof(sources)); + for (stage = 0; stage < D3DTSS_MAXSTAGES; stage++) + stage_alpha_minimum[stage] = -1; if (preparation.active) { memset(&preparation.key, 0, sizeof(preparation.key)); @@ -3532,6 +3861,12 @@ static void bind_textures(struct nv2a_pixel_shader_key *key, float texture_scale } } sampler_describe(stage, description.levels > 1, description.hires, target != NULL, &source->sampler); + /* (alpha_test_always_passes: the game's own texture, sampled as a 2D + texture, never past its edge into a border colour) */ + if (!target && D3D__RenderState[D3DRS_ALPHATESTENABLE] && mode == 0x01 && kind == XGPU_TEXTURE_KIND_2D && + !description.hires && source->sampler.address[0] < XGPU_ADDRESS_BORDER && + source->sampler.address[1] < XGPU_ADDRESS_BORDER) + stage_alpha_minimum[stage] = texture_alpha_minimum(texture, stage); if (device.texture_regions[stage][0] > 0.0f) memcpy(texture_scale[stage], device.texture_regions[stage], sizeof(texture_scale[stage])); /* (the high-res text's distance fields: text_hires.c) */ @@ -3677,6 +4012,8 @@ static void pipeline_state_describe(BOOL has_depth, struct xgpu_pipeline_key *ke defaults to the viewport, so fragment clipping follows the viewport: this is what keeps a split-screen window's geometry from bleeding across the divider */ memcpy(dynamic->scissor, dynamic->viewport, sizeof(dynamic->scissor)); + /* (but a pass the game bounds, to its bounds in the viewport) */ + draw_bounds_scissor(dynamic->scissor); dynamic->depth_range[0] = device.viewport.MinZ; dynamic->depth_range[1] = device.viewport.MaxZ; @@ -3890,6 +4227,17 @@ static struct BOOL environment; BOOL failed; struct xgpu_dynamic_lights_block data; + /* the lights that light the structure: each one's middle and its reach + (its radius and a margin), the box about them all; -1: not known (all + the lightmap pass is lit) */ + float reach[XGPU_DYNAMIC_LIGHTS_MAXIMUM][4]; + float reach_box[2][3]; + int reach_count; + /* every light's (the models' only too), as those; and whether the model + being drawn is one none of them reaches (halo_model_bounds) */ + float model_reach[XGPU_DYNAMIC_LIGHTS_MAXIMUM][4]; + int model_reach_count; + BOOL model_unreached; /* the cells' masks, and past them a slice of the clusters' (xgpu.h) */ unsigned int mask[XGPU_DYNAMIC_LIGHTS_GRID_Z + 1][XGPU_DYNAMIC_LIGHTS_GRID_Y][XGPU_DYNAMIC_LIGHTS_GRID_X][4]; /* the world (halo_dynamic_lights_world, malloc's), and whether the @@ -3906,6 +4254,11 @@ dynamic lights be enhanced (a Pro 2v2 game's: classic for every player, whatever their display.dynamic_lights) */ int network_standard_enhanced_lights_allowed(void); +/* how far a light is taken to reach: its radius, past which the shaders add +nothing of it (xgpu_spirv.c's dynamic_light_sum), and a margin for the +arithmetic's rounding */ +#define LIGHT_REACH(radius) ((radius) * 1.01f + 0.01f) + static void dynamic_lights_read_settings(void) { static const float defaults[2][4] = { { 1.7f, 12.0f, 0.18f, 0.25f }, { 0.22f, 1.2f, 0.12f, 0.035f } }; @@ -4009,6 +4362,9 @@ void halo_dynamic_lights_set(const struct halo_dynamic_light *lights, int count, int index, axis; dynamic_lights.count = 0; + dynamic_lights.reach_count = 0; + dynamic_lights.model_reach_count = 0; + dynamic_lights.model_unreached = FALSE; halos->count = 0; if (!halo_dynamic_lights_walked()) return; @@ -4047,6 +4403,25 @@ void halo_dynamic_lights_set(const struct halo_dynamic_light *lights, int count, if (!(light->radius > 0.0f)) continue; + memcpy(dynamic_lights.model_reach[dynamic_lights.model_reach_count], light->position, 3 * sizeof(float)); + dynamic_lights.model_reach[dynamic_lights.model_reach_count++][3] = LIGHT_REACH(light->radius); + if (!(light->flags & HALO_DYNAMIC_LIGHT_MODELS_ONLY)) + { + float *reach = dynamic_lights.reach[dynamic_lights.reach_count]; + + reach[3] = LIGHT_REACH(light->radius); + for (axis = 0; axis < 3; axis++) + { + float low = light->position[axis] - reach[3], high = light->position[axis] + reach[3]; + + reach[axis] = light->position[axis]; + if (!dynamic_lights.reach_count || low < dynamic_lights.reach_box[0][axis]) + dynamic_lights.reach_box[0][axis] = low; + if (!dynamic_lights.reach_count || high > dynamic_lights.reach_box[1][axis]) + dynamic_lights.reach_box[1][axis] = high; + } + dynamic_lights.reach_count++; + } dynamic_lights.data.position[index][0] = light->position[0]; dynamic_lights.data.position[index][1] = light->position[1]; dynamic_lights.data.position[index][2] = light->position[2]; @@ -4123,6 +4498,70 @@ backends read them from) */ typedef char dynamic_lights_own_lights_register[XGPU_MODEL_LIGHT_REGISTER(XGPU_MODEL_LIGHT_OWN_LIGHTS) == XGPU_VERTEX_CONSTANT_BIAS + HALO_DYNAMIC_LIGHTS_OWN_LIGHTS_REGISTER ? 1 : -1]; +/* whether the draws are parted by the lights' reach at all: where the +backend has the lights' draws kept to what they can change +(xgpu_backend_capabilities.draw_bounds); not a map's preparation's +(render_enumerate.c), whose draws make the pipelines of the lit shaders +whatever its one light reaches, nor a recorded one's */ +static BOOL dynamic_lights_active(void); + +static BOOL lights_reach_parted(void) +{ + return xgpu_backend_capabilities.draw_bounds && !preparation.active && !journal_recording && dynamic_lights_active(); +} + +/* the lightmap pass's triangles being drawn are ones no light reaches +(lights_reach_split): drawn as with no light, the same pixels (the lights' +sum is nothing there), by the shader without their loop */ +static BOOL lights_unreached_drawing; + +/* whether no light of the frame's reaches the model being drawn +(halo_model_bounds: its sphere, grown by MODEL_BOUNDS_REACH, about its +middle as the tick left it and as the frame draws it, both; a first-person +model's, the weapon and the arms, about the camera). Such a model is drawn +as with no light, the same pixels, by the shader without the lights' loop; +the game gives it no point lights then (rasterizer_xbox_models.c) */ +static BOOL dynamic_lights_model_unreached(void) +{ + float radius = model_bounds.first_person ? MODEL_BOUNDS_FIRST_PERSON_REACH : MODEL_BOUNDS_REACH(model_bounds.radius); + int light, centre; + + if (!model_bounds.known || dynamic_lights.model_reach_count < 0 || !lights_reach_parted()) + return FALSE; + for (light = 0; light < dynamic_lights.model_reach_count; light++) + { + const float *reach = dynamic_lights.model_reach[light]; + float both = reach[3] + radius; + + for (centre = 0; centre < 2; centre++) + { + const float *middle = model_bounds.first_person ? dynamic_lights.data.grid[0] : model_bounds.centre[centre]; + float x = reach[0] - middle[0], y = reach[1] - middle[1], z = reach[2] - middle[2]; + + if (!(x * x + y * y + z * z > both * both)) + return FALSE; + } + } + return TRUE; +} + +int halo_model_bounds(const float centre[3], const float drawn[3], float radius, float distance, int first_person) +{ + /* (a caller with no sphere of its model gives no radius: a flag's cloth, + the sky; a first-person model's is not needed) */ + model_bounds.known = centre && drawn && (first_person || radius > 0.0f) && distance >= 0.0f; + dynamic_lights.model_unreached = FALSE; + if (!model_bounds.known) + return 0; + memcpy(model_bounds.centre[0], centre, sizeof(model_bounds.centre[0])); + memcpy(model_bounds.centre[1], drawn, sizeof(model_bounds.centre[1])); + model_bounds.radius = radius; + model_bounds.distance = distance; + model_bounds.first_person = first_person != 0; + dynamic_lights.model_unreached = dynamic_lights_model_unreached(); + return dynamic_lights.model_unreached; +} + /* the frame's draws are lit by the enhanced dynamic lights */ static BOOL dynamic_lights_active(void) { @@ -4414,7 +4853,7 @@ static void manifest_note_basis(const struct vertex_shader_object *program, cons context.screen_colour = target_screen_colour ? 1 : 0; /* (the lights given to the front end whatever the setting where the map's preparation is recorded: halo_dynamic_lights_walked) */ - context.lights_present = dynamic_lights.count > 0; + context.lights_present = dynamic_lights.count > 0 && !lights_unreached_drawing && !dynamic_lights.model_unreached; context.environment = dynamic_lights.environment ? 1 : 0; context.hdr_category = (uint8_t)hdr.category; context.hdr_view = hdr.view ? 1 : 0; @@ -4581,6 +5020,13 @@ static BOOL prepare_draw(BOOL immediate, unsigned long primitive) D3D__RenderState[D3DRS_DESTBLEND] == D3DBLEND_SRCALPHA; key.point_threshold = key.point_threshold && key.coverage_alpha; key.alpha_test_function = D3D__RenderState[D3DRS_ALPHATESTENABLE] ? D3D__RenderState[D3DRS_ALPHAFUNC] : 0; + /* (a test no pixel of the draw fails is not made: above, "an alpha test + that cannot fail") */ + if (key.alpha_test_function && !key.coverage_alpha && xgpu_backend_capabilities.alpha_test_drop && + alpha_test_always_passes()) + { + key.alpha_test_function = 0; + } key.fog_enable = D3D__RenderState[D3DRS_FOGENABLE] != 0; key.source_blend = source_blend_in_shader(); key.fog_table_mode = (unsigned char)D3D__RenderState[D3DRS_FOGTABLEMODE]; @@ -4634,8 +5080,10 @@ static BOOL prepare_draw(BOOL immediate, unsigned long primitive) facts.samples = target_samples; facts.hdr_target = hdr.target; facts.hdr = hdr_tag(&pipeline_key); - facts.per_pixel = per_pixel_lighting(); - facts.lights_active = dynamic_lights_active(); + /* (a cut-out's depth: no light) */ + facts.per_pixel = !cutout_depth_drawing && per_pixel_lighting(); + facts.lights_active = !cutout_depth_drawing && !lights_unreached_drawing && !dynamic_lights.model_unreached && + dynamic_lights_active(); facts.environment = dynamic_lights.environment; facts.sample_mask = xgpu_backend_capabilities.sample_mask; /* (display.ambient_occlusion "world": its light, where its window is @@ -5354,7 +5802,195 @@ void WINAPI D3DDevice_DrawVertices(D3DPRIMITIVETYPE primitive_type, UINT start_v preparation_draw_complete(); } +static void draw_indexed_vertices(D3DPRIMITIVETYPE primitive_type, UINT vertex_count, CONST WORD *index_data); + +static void draw_indexed_cutout(D3DPRIMITIVETYPE primitive_type, UINT vertex_count, CONST WORD *index_data); + +/* ---------- the lightmap pass's triangles the dynamic lights reach + +The shader that adds the enhanced dynamic lights costs each pixel its loop's +set-up whether a light is near or not (on a tile-based GPU of few registers +it also runs fewer pixels at once). The lightmap pass's draws are whole +materials, a valley's ground in one: its triangles are drawn in two parts, +those in some light's reach with that shader, the rest as with no light. +The pixels are the same: past a light's radius the shader adds nothing. +(The unreached part is drawn first: where two triangles of one material +lie in one plane, which of them is seen may change.) */ + +/* the triangles of a list apart: FALSE if they cannot be told apart (all +are drawn lit) */ +static BOOL lights_reach_split(const WORD *index_data, unsigned long count, WORD **reached, unsigned long *reached_count, + WORD **unreached, unsigned long *unreached_count) +{ + static WORD *buffer; + static unsigned long capacity; + struct vertex_shader_object *declaration = device.vertex_shader; + const struct xgpu_vertex_element *position = NULL; + const unsigned char *vertices; + unsigned long stride, index, lit = 0, unlit = 0; + + if (!declaration) + return FALSE; + for (index = 0; index < declaration->element_count; index++) + { + if (declaration->elements[index].reg == 0) + position = &declaration->elements[index]; + } + if (!position || position->format != XGPU_VERTEX_FLOAT3 || !device.streams[position->stream].data || + !device.streams[position->stream].stride) + { + return FALSE; + } + if (count * 2 > capacity) + { + WORD *grown = realloc(buffer, count * 2 * sizeof(WORD)); + + if (!grown) + return FALSE; + buffer = grown; + capacity = count * 2; + } + stride = device.streams[position->stream].stride; + vertices = (const unsigned char *)PLATFORM_PHYSICAL_TO_VIRTUAL(device.streams[position->stream].data) + + device.base_vertex_index * stride + position->offset; + *reached = buffer; + *unreached = buffer + count; + for (index = 0; index + 3 <= count; index += 3) + { + float low[3], high[3]; + BOOL touched = TRUE; + int corner, axis, light; + + for (corner = 0; corner < 3; corner++) + { + float point[3]; + + memcpy(point, vertices + index_data[index + corner] * stride, sizeof(point)); + for (axis = 0; axis < 3; axis++) + { + if (!corner || point[axis] < low[axis]) + low[axis] = point[axis]; + if (!corner || point[axis] > high[axis]) + high[axis] = point[axis]; + } + } + for (axis = 0; axis < 3; axis++) + { + if (high[axis] < dynamic_lights.reach_box[0][axis] || low[axis] > dynamic_lights.reach_box[1][axis]) + touched = FALSE; + } + if (touched) + { + touched = FALSE; + for (light = 0; light < dynamic_lights.reach_count && !touched; light++) + { + const float *reach = dynamic_lights.reach[light]; + float distance = 0.0f; + + for (axis = 0; axis < 3; axis++) + { + float away = reach[axis] < low[axis] ? low[axis] - reach[axis] : + reach[axis] > high[axis] ? reach[axis] - high[axis] : 0.0f; + + distance += away * away; + } + touched = !(distance > reach[3] * reach[3]); + } + } + if (touched) + { + memcpy(*reached + lit, index_data + index, 3 * sizeof(WORD)); + lit += 3; + } + else + { + memcpy(*unreached + unlit, index_data + index, 3 * sizeof(WORD)); + unlit += 3; + } + } + *reached_count = lit; + *unreached_count = unlit; + return TRUE; +} + void WINAPI D3DDevice_DrawIndexedVertices(D3DPRIMITIVETYPE primitive_type, UINT vertex_count, CONST WORD *index_data) +{ + WORD *reached, *unreached; + unsigned long reached_count, unreached_count; + + if (!dynamic_lights.environment || !vertex_count || !index_data || dynamic_lights.reach_count < 0 || + !lights_reach_parted()) + { + draw_indexed_cutout(primitive_type, vertex_count, index_data); + return; + } + if (!dynamic_lights.reach_count) + { + lights_unreached_drawing = TRUE; + draw_indexed_cutout(primitive_type, vertex_count, index_data); + lights_unreached_drawing = FALSE; + return; + } + if (primitive_type != D3DPT_TRIANGLELIST || + !lights_reach_split(index_data, vertex_count, &reached, &reached_count, &unreached, &unreached_count) || + !unreached_count) + { + draw_indexed_cutout(primitive_type, vertex_count, index_data); + return; + } + lights_unreached_drawing = TRUE; + draw_indexed_cutout(primitive_type, reached_count ? (UINT)unreached_count : vertex_count, + reached_count ? unreached : index_data); + lights_unreached_drawing = FALSE; + if (reached_count) + draw_indexed_cutout(primitive_type, (UINT)reached_count, reached); +} + +static void draw_indexed_cutout(D3DPRIMITIVETYPE primitive_type, UINT vertex_count, CONST WORD *index_data) +{ + /* (the render states the depth's draw and the colour's change) */ + static const int changed[] = { D3DRS_COLORWRITEENABLE, D3DRS_FOGENABLE, D3DRS_PSTEXTUREMODES, D3DRS_PSCOMBINERCOUNT, + D3DRS_PSRGBINPUTS0, D3DRS_PSALPHAINPUTS0, D3DRS_PSRGBOUTPUTS0, D3DRS_PSALPHAOUTPUTS0, + D3DRS_PSFINALCOMBINERINPUTSABCD, D3DRS_PSFINALCOMBINERINPUTSEFG, D3DRS_ALPHATESTENABLE, D3DRS_ZFUNC }; + DWORD *state = D3D__RenderState, kept[sizeof(changed) / sizeof(changed[0])]; + int stage = vertex_count && index_data ? cutout_stage() : -1, index; + + if (stage < 0) + { + draw_indexed_vertices(primitive_type, vertex_count, index_data); + return; + } + /* (a map's preparation: as one draw too, a model's small in the view, so + that neither way's pipelines are first made in play) */ + if (preparation.active) + draw_indexed_vertices(primitive_type, vertex_count, index_data); + for (index = 0; index < (int)(sizeof(changed) / sizeof(changed[0])); index++) + kept[index] = state[changed[index]]; + /* its depth (and stencil): the texture's alpha, tested */ + state[D3DRS_COLORWRITEENABLE] = 0; + state[D3DRS_FOGENABLE] = FALSE; + state[D3DRS_PSTEXTUREMODES] &= 0x1fUL << (5 * stage); + state[D3DRS_PSCOMBINERCOUNT] = 1; + state[D3DRS_PSRGBINPUTS0] = state[D3DRS_PSALPHAINPUTS0] = 0; + state[D3DRS_PSRGBOUTPUTS0] = state[D3DRS_PSALPHAOUTPUTS0] = 0; + state[D3DRS_PSFINALCOMBINERINPUTSABCD] = 0; + state[D3DRS_PSFINALCOMBINERINPUTSEFG] &= 0x0000ff00UL; + cutout_depth_drawing = TRUE; + draw_indexed_vertices(primitive_type, vertex_count, index_data); + cutout_depth_drawing = FALSE; + for (index = 0; index < (int)(sizeof(changed) / sizeof(changed[0])); index++) + state[changed[index]] = kept[index]; + /* its colour, where the depth is its own (written again, the same: a + tile-based GPU's early test learns a depth from the draws that write + it without a discard, and rejects by it what is drawn behind after) */ + state[D3DRS_ALPHATESTENABLE] = FALSE; + state[D3DRS_ZFUNC] = D3DCMP_EQUAL; + draw_indexed_vertices(primitive_type, vertex_count, index_data); + for (index = 0; index < (int)(sizeof(changed) / sizeof(changed[0])); index++) + state[changed[index]] = kept[index]; +} + +static void draw_indexed_vertices(D3DPRIMITIVETYPE primitive_type, UINT vertex_count, CONST WORD *index_data) { unsigned long minimum, maximum, count, generation = 0, index_offset = 0; WORD *indices = NULL; @@ -5529,6 +6165,9 @@ void WINAPI D3DDevice_SetVertexDataColor(INT reg, D3DCOLOR color) /* ---------- clearing */ +/* the clear being made is d3d8_clear_viewport_alone's */ +static BOOL clear_alone; + void WINAPI D3DDevice_Clear(DWORD count, CONST D3DRECT *rectangles, DWORD flags, D3DCOLOR color, float z, DWORD stencil) { float rgba[4]; @@ -5576,7 +6215,7 @@ void WINAPI D3DDevice_Clear(DWORD count, CONST D3DRECT *rectangles, DWORD flags, rectangle[0][1] = target_pixel((float)device.viewport.Y, 1); rectangle[0][2] = target_pixel((float)(device.viewport.X + device.viewport.Width), 0) - rectangle[0][0]; rectangle[0][3] = target_pixel((float)(device.viewport.Y + device.viewport.Height), 1) - rectangle[0][1]; - xgpu_backend_clear(clear, rgba, z, stencil, (const long (*)[4])rectangle, 1); + xgpu_backend_clear(clear | (clear_alone ? XGPU_CLEAR_ALONE : 0), rgba, z, stencil, (const long (*)[4])rectangle, 1); return; } { @@ -5608,6 +6247,25 @@ void WINAPI D3DDevice_Clear(DWORD count, CONST D3DRECT *rectangles, DWORD flags, } } +/* whether a target drawn in part is better cleared in part +(d3d8_clear_viewport_alone): where a pass costs by the area it covers, a +tile-based GPU's */ +int d3d8_clears_alone(void) +{ + return device.gl_ready && xgpu_backend_capabilities.clear_alone; +} + +/* the viewport's rectangle of the colour target cleared, as +IDirect3DDevice8_Clear clears it with no rectangles; and what the target +holds outside it is of no use from now on (XGPU_CLEAR_ALONE): the caller +draws, and clears, each part of the target before it is sampled */ +void d3d8_clear_viewport_alone(D3DCOLOR color) +{ + clear_alone = TRUE; + D3DDevice_Clear(0, NULL, D3DCLEAR_TARGET, color, 1.0f, 0); + clear_alone = FALSE; +} + /* ---------- the anti-aliasing passes */ /* display.anti_aliasing's pass over a window's 3D view, before the HUD and @@ -7339,6 +7997,9 @@ static void journal_restore(const struct journal_state *state) hdr.view = state->hdr_view != 0; dynamic_lights.environment = state->lights_environment != 0; dynamic_lights.count = (int)state->lights_count; + dynamic_lights.reach_count = -1; + dynamic_lights.model_reach_count = -1; + dynamic_lights.model_unreached = FALSE; dynamic_lights.halos.count = (int)state->halos_count; preparation.sky = state->sky; device.visibility_test_active = state->visibility != 0; @@ -7575,6 +8236,9 @@ int halo_gpu_journal_replay(const unsigned int *words, unsigned int count, const hdr.view = saved.hdr_view; dynamic_lights.environment = saved.lights_environment; dynamic_lights.count = saved.lights_count; + dynamic_lights.reach_count = -1; + dynamic_lights.model_reach_count = -1; + dynamic_lights.model_unreached = FALSE; dynamic_lights.halos.count = saved.halos_count; preparation.sky = saved.sky; device.visibility_test_active = saved.visibility; diff --git a/port/linux/src/xbox_texture_decode.c b/port/linux/src/xbox_texture_decode.c index d8cdb5c47c..11a646f239 100644 --- a/port/linux/src/xbox_texture_decode.c +++ b/port/linux/src/xbox_texture_decode.c @@ -192,6 +192,105 @@ unsigned long xgpu_texture_face_size(const struct xgpu_texture_description *desc return size; } +int xgpu_texture_minimum_alpha(const struct xgpu_texture_description *description, const unsigned char *texels) +{ + struct format_information information = format_information(description->format); + unsigned long level, least = 255; + + switch (information.kind) + { + case _texel_x8r8g8b8: + case _texel_r5g6b5: + case _texel_x1r5g5b5: + return 255; + case _texel_dxt1: + case _texel_dxt3: + case _texel_dxt5: + case _texel_a8r8g8b8: + break; + default: + return -1; + } + if (!texels || description->cube_map || description->depth > 1) + return -1; + for (level = 0; level < description->levels && least; level++) + { + const unsigned char *data = texels + xgpu_texture_level_offset(description, level); + unsigned long size = xgpu_texture_level_size(description, level), offset; + + if (information.kind == _texel_a8r8g8b8) + { + /* (a linear texture's rows' padding is read with them: no more than + the least) */ + for (offset = 3; offset < size; offset += 4) + { + if (data[offset] < least) + least = data[offset]; + } + } + else if (information.kind == _texel_dxt1) + { + for (offset = 0; offset + 8 <= size; offset += 8) + { + const unsigned char *block = data + offset; + unsigned long c0 = block[0] | (unsigned long)block[1] << 8, c1 = block[2] | (unsigned long)block[3] << 8; + unsigned long indices = block[4] | (unsigned long)block[5] << 8 | (unsigned long)block[6] << 16 | + (unsigned long)block[7] << 24; + + /* (three colours and transparent: a texel whose index is 3) */ + if (c0 <= c1 && (indices & (indices >> 1) & 0x55555555UL)) + return 0; + } + } + else if (information.kind == _texel_dxt3) + { + /* (a block's first eight bytes: its texels' alphas, four bits each) */ + for (offset = 0; offset + 16 <= size; offset += 16) + { + int byte; + + for (byte = 0; byte < 8; byte++) + { + unsigned long low = data[offset + byte] & 0x0f, high = data[offset + byte] >> 4; + + if (low * 17 < least) + least = low * 17; + if (high * 17 < least) + least = high * 17; + } + } + } + else + { + /* (a block's two alphas, then its texels' indices, three bits each: + eight alphas from the first down to the second; or, the first not + the greater, six from it up to the second, then 0 and 255) */ + for (offset = 0; offset + 16 <= size; offset += 16) + { + const unsigned char *block = data + offset; + unsigned long a0 = block[0], a1 = block[1], lower = a0 > a1 ? a1 : a0; + + if (a0 <= a1) + { + unsigned long long indices = 0; + int texel; + + for (texel = 0; texel < 6; texel++) + indices |= (unsigned long long)block[2 + texel] << (8 * texel); + for (texel = 0; texel < 16; texel++) + { + if (((indices >> (3 * texel)) & 7) == 6) + return 0; + } + } + if (lower < least) + least = lower; + } + } + } + return (int)least; +} + /* whether the size is one D3DDevice_GetDeviceCaps allows (d3d8_device.c): up to 4096 by 4096, and 512 each way for a volume */ BOOL xgpu_texture_size_supported(const struct xgpu_texture_description *description) diff --git a/port/linux/src/xgpu.h b/port/linux/src/xgpu.h index d9f0dae4d8..2e7085c72e 100644 --- a/port/linux/src/xgpu.h +++ b/port/linux/src/xgpu.h @@ -489,6 +489,14 @@ unsigned long xgpu_texture_level_pitch(const struct xgpu_texture_description *de or depth from the first level's */ unsigned long xgpu_texture_level_size(const struct xgpu_texture_description *description, unsigned long level); unsigned long xgpu_texture_level_dimension(unsigned long base, unsigned long level); +/* the least alpha any texel of any level reads, 0 to 255, from its texels +(a 2D texture's): 255 for a format with no alpha; a DXT1 texture's 255, or +0 with a block of three colours that uses the fourth index, the +transparent one; a DXT3 texture's least alpha; a DXT5 texture's blocks' +lower end points (0 where a block of six alphas uses its 0); an +A8R8G8B8 texture's. Never more than the least: -1 where it is not known +(the other formats are not read) */ +int xgpu_texture_minimum_alpha(const struct xgpu_texture_description *description, const unsigned char *texels); /* the size is one D3DDevice_GetDeviceCaps allows (d3d8_device.c) */ BOOL xgpu_texture_size_supported(const struct xgpu_texture_description *description); /* a level of a texture that is not compressed (a volume's slices one after diff --git a/port/linux/src/xgpu_backend.h b/port/linux/src/xgpu_backend.h index efb4b953d7..5c5204764a 100644 --- a/port/linux/src/xgpu_backend.h +++ b/port/linux/src/xgpu_backend.h @@ -52,6 +52,26 @@ struct xgpu_backend_capabilities /* visibility tests counted by the pixel shaders (atomic counters) rather than by queries */ BOOL sample_counting; + /* a pixel shader that may discard is not tested by its depth before it + runs (a tile-based GPU's early test): a draw is better made without its + alpha test where no texel fails it (d3d8_device.c, "an alpha test that + cannot fail") */ + BOOL alpha_test_drop; + /* ... and an alpha-tested opaque draw as its depth, then its colour + where the depth is its own ("a cut-out's depth first": asked for by a + switch alone, as a few of its pixels can differ) */ + BOOL cutout_prepass; + /* a pass costs by the part of its target it clears and draws, not by the + target (XGPU_CLEAR_ALONE is of use: d3d8_clear_viewport_alone) */ + BOOL clear_alone; + /* the lights' and shadows' draws are kept to what they can change, where + the pixels shaded are the cost: draws the game bounds (halo_draw_bounds: + a dynamic light's, a shadow's over the level) are scissored to their + bounds (d3d8_device.c, "the draws' bounds"), and with the enhanced + dynamic lights the triangles and models no light reaches are drawn by + the shader without the lights' loop ("the lightmap pass's triangles the + dynamic lights reach", halo_model_bounds) */ + BOOL draw_bounds; /* vertex streams' D3DCOLOR elements read as the Xbox has them (BGRA); else streamed with their bytes swapped, never from the mirror */ @@ -216,11 +236,14 @@ their multisampled attachments (0: their textures) */ void xgpu_backend_set_targets(xgpu_target color, int hud_group, xgpu_target depth, int views, int base, int samples); /* clears the bound targets' rectangles (left, top from the first row, -width, height, in their pixels; count of them): XGPU_CLEAR_* */ +width, height, in their pixels; count of them): XGPU_CLEAR_*. +XGPU_CLEAR_ALONE: what the colour target holds outside the rectangles is of +no use from now on, and the backend may leave anything there (or clear it +too: a backend that knows nothing of the flag clears the rectangles) */ enum { XGPU_CLEAR_RED = 1, XGPU_CLEAR_GREEN = 2, XGPU_CLEAR_BLUE = 4, XGPU_CLEAR_ALPHA = 8, - XGPU_CLEAR_COLOR = 16, XGPU_CLEAR_DEPTH = 32, XGPU_CLEAR_STENCIL = 64, + XGPU_CLEAR_COLOR = 16, XGPU_CLEAR_DEPTH = 32, XGPU_CLEAR_STENCIL = 64, XGPU_CLEAR_ALONE = 128, }; void xgpu_backend_clear(unsigned long flags, const float color[4], float depth, unsigned long stencil, const long (*rectangles)[4], unsigned long count); diff --git a/port/linux/src/xgpu_stream.h b/port/linux/src/xgpu_stream.h index 618bdf9ef2..dfe836b942 100644 --- a/port/linux/src/xgpu_stream.h +++ b/port/linux/src/xgpu_stream.h @@ -422,7 +422,9 @@ struct xgpu_stream_capabilities /* display.ambient_occlusion "ray_traced": the backend traces the occlusion's rays by the GPU's ray queries (xgpu_backend_world_ao_mesh, _instances: Vulkan's VK_KHR_ray_query) */ - uint8_t ray_queries, pad[3]; + /* (and xgpu_backend_capabilities' clear_alone and draw_bounds, and in + cutout_prepass its alpha_test_drop, 1, and cutout_prepass, 2) */ + uint8_t ray_queries, cutout_prepass, clear_alone, draw_bounds; /* the GPU, its API and driver, in a few words (a report's: port/linux/src/report.c), NUL terminated */ char device[96]; diff --git a/port/linux/src/xgpu_stream_encode.c b/port/linux/src/xgpu_stream_encode.c index 47b3157c01..8cdb9ec26a 100644 --- a/port/linux/src/xgpu_stream_encode.c +++ b/port/linux/src/xgpu_stream_encode.c @@ -652,6 +652,10 @@ void xgpu_backend_initialize(void) xgpu_backend_capabilities.most_samples = stream.capabilities.most_samples; xgpu_backend_capabilities.most_anisotropy = stream.capabilities.most_anisotropy; xgpu_backend_capabilities.sample_counting = stream.capabilities.sample_counting; + xgpu_backend_capabilities.alpha_test_drop = (stream.capabilities.cutout_prepass & 1) != 0; + xgpu_backend_capabilities.cutout_prepass = (stream.capabilities.cutout_prepass & 2) != 0; + xgpu_backend_capabilities.clear_alone = stream.capabilities.clear_alone; + xgpu_backend_capabilities.draw_bounds = stream.capabilities.draw_bounds; xgpu_backend_capabilities.bgra_vertex_colors = stream.capabilities.bgra_vertex_colors; xgpu_backend_capabilities.base_vertex = stream.capabilities.base_vertex; xgpu_backend_capabilities.sample_mask = stream.capabilities.sample_mask; diff --git a/port/linux/tests/texture_alpha_test.c b/port/linux/tests/texture_alpha_test.c new file mode 100644 index 0000000000..be481169eb --- /dev/null +++ b/port/linux/tests/texture_alpha_test.c @@ -0,0 +1,193 @@ +/* +TEXTURE_ALPHA_TEST.C + +A texture's least alpha (xgpu_texture_minimum_alpha, xbox_texture_decode.c), +which the front end drops a draw's alpha test by (d3d8_device.c, "an alpha +test that cannot fail"), against the alphas the decoder gives the same +texels: never more than the least of them, and the least itself where the +format lets it be told. On the build machine, without a GPU or game data +(tools/test_texture_alpha.py). +*/ + +#include "xgpu.h" + +#include +#include +#include + +#define CHECK(condition, ...) \ + do { if (!(condition)) { fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); \ + fputc('\n', stderr); exit(1); } } while (0) + +enum +{ + FORMAT_A4R4G4B4 = 0x04, FORMAT_A8R8G8B8 = 0x06, FORMAT_X8R8G8B8 = 0x07, FORMAT_DXT1 = 0x0c, FORMAT_DXT3 = 0x0e, + FORMAT_DXT5 = 0x0f, FORMAT_LINEAR_A8R8G8B8 = 0x12 +}; + +static uint32_t random_state = 2342; + +static uint32_t random_next(void) +{ + random_state = random_state * 1664525u + 1013904223u; + return random_state >> 8; +} + +static struct xgpu_texture_description describe(DWORD format, unsigned long size, unsigned long levels) +{ + struct xgpu_texture_description description; + + memset(&description, 0, sizeof(description)); + description.format = format; + description.width = description.height = size; + description.depth = 1; + description.levels = levels; + description.linear = format == FORMAT_LINEAR_A8R8G8B8; + description.compressed = format == FORMAT_DXT1 || format == FORMAT_DXT3 || format == FORMAT_DXT5; + description.pitch = description.linear ? size * 4 : 0; + return description; +} + +/* the least alpha of the texels as the decoder reads them, every level's */ +static int decoded_minimum(const struct xgpu_texture_description *description, const unsigned char *texels) +{ + unsigned long level, least = 255; + + for (level = 0; level < description->levels; level++) + { + unsigned long width = xgpu_texture_level_dimension(description->width, level); + unsigned long height = xgpu_texture_level_dimension(description->height, level), index; + const unsigned char *source = texels + xgpu_texture_level_offset(description, level); + DWORD *pixels = calloc(width * height, sizeof(DWORD)); + + CHECK(pixels, "no memory"); + if (description->compressed) + xgpu_texture_decode_dxt(description, level, source, pixels); + else + CHECK(xgpu_texture_decode_level(description, level, source, NULL, pixels), "a level not decoded"); + for (index = 0; index < width * height; index++) + { + if (pixels[index] >> 24 < least) + least = pixels[index] >> 24; + } + free(pixels); + } + return (int)least; +} + +/* random texels: all of them, or (shaped) each block's alphas kept away +from nothing, so that the least is not always 0 */ +static void fill(const struct xgpu_texture_description *description, unsigned char *texels, unsigned long bytes, int shaped) +{ + unsigned long offset; + + for (offset = 0; offset < bytes; offset++) + texels[offset] = (unsigned char)random_next(); + if (!shaped) + return; + switch (description->format) + { + case FORMAT_DXT1: + /* (four colours: the first the greater, no transparent index) */ + for (offset = 0; offset + 8 <= bytes; offset += 8) + { + texels[offset] = texels[offset + 1] = 0xff; + texels[offset + 2] = texels[offset + 3] = 0x00; + } + break; + case FORMAT_DXT3: + for (offset = 0; offset + 16 <= bytes; offset += 16) + { + int byte; + + for (byte = 0; byte < 8; byte++) + texels[offset + byte] |= 0x88; + } + break; + case FORMAT_DXT5: + /* (eight alphas, from the first down to the second) */ + for (offset = 0; offset + 16 <= bytes; offset += 16) + { + texels[offset] = (unsigned char)(200 + random_next() % 56); + texels[offset + 1] = (unsigned char)(100 + random_next() % 100); + } + break; + default: + for (offset = 3; offset < bytes; offset += 4) + texels[offset] |= 0x90; + break; + } +} + +static void format_check(DWORD format, const char *name, int exact) +{ + int trial; + + for (trial = 0; trial < 400; trial++) + { + /* (a first level alone, of whole blocks: every texel read is one of + the texture's; with the levels below, a level smaller than a block + reads part of one) */ + unsigned long levels = format == FORMAT_LINEAR_A8R8G8B8 || trial % 2 ? 1 : 5; + struct xgpu_texture_description description = describe(format, 16, levels); + unsigned long bytes = xgpu_texture_face_size(&description); + unsigned char *texels = malloc(bytes); + int minimum, decoded; + + CHECK(texels && bytes, "%s: no texels", name); + fill(&description, texels, bytes, trial % 4 >= 2); + minimum = xgpu_texture_minimum_alpha(&description, texels); + decoded = decoded_minimum(&description, texels); + CHECK(minimum >= 0 && minimum <= decoded, "%s, trial %d: least alpha %d, the decoder's %d", name, trial, minimum, + decoded); + if (exact && levels == 1) + CHECK(minimum == decoded, "%s, trial %d: least alpha %d, not the decoder's %d", name, trial, minimum, decoded); + free(texels); + } +} + +int main(void) +{ + struct xgpu_texture_description description; + unsigned char block[16]; + + format_check(FORMAT_DXT1, "DXT1", 1); + format_check(FORMAT_DXT3, "DXT3", 1); + format_check(FORMAT_DXT5, "DXT5", 0); + format_check(FORMAT_A8R8G8B8, "A8R8G8B8", 1); + format_check(FORMAT_LINEAR_A8R8G8B8, "linear A8R8G8B8", 1); + + /* a DXT5 block of one alpha: that alpha */ + description = describe(FORMAT_DXT5, 4, 1); + memset(block, 0, sizeof(block)); + block[0] = block[1] = 200; + CHECK(xgpu_texture_minimum_alpha(&description, block) == 200 && decoded_minimum(&description, block) == 200, + "a DXT5 block of one alpha"); + /* ... of six alphas that uses its 0 (index 6): nothing */ + block[0] = 100; + block[2] = 6; + CHECK(xgpu_texture_minimum_alpha(&description, block) == 0 && decoded_minimum(&description, block) == 0, + "a DXT5 block of six alphas and nothing"); + /* a DXT1 block of three colours that uses the transparent index */ + description = describe(FORMAT_DXT1, 4, 1); + memset(block, 0, sizeof(block)); + block[4] = 0x03; + CHECK(xgpu_texture_minimum_alpha(&description, block) == 0 && decoded_minimum(&description, block) == 0, + "a DXT1 block with a transparent texel"); + /* ... and of four colours with the same index: opaque */ + block[0] = 1; + CHECK(xgpu_texture_minimum_alpha(&description, block) == 255 && decoded_minimum(&description, block) == 255, + "a DXT1 block of four colours"); + + /* a format with no alpha passes whatever its texels; one not read, and a + cube map, are not known */ + description = describe(FORMAT_X8R8G8B8, 4, 1); + CHECK(xgpu_texture_minimum_alpha(&description, block) == 255, "a format with no alpha"); + description = describe(FORMAT_A4R4G4B4, 4, 1); + CHECK(xgpu_texture_minimum_alpha(&description, block) == -1, "a format that is not read"); + description = describe(FORMAT_A8R8G8B8, 4, 1); + description.cube_map = TRUE; + CHECK(xgpu_texture_minimum_alpha(&description, block) == -1, "a cube map"); + printf("texture_alpha_test: passed\n"); + return 0; +} diff --git a/source/rasterizer/xbox/rasterizer_xbox_active_camouflage.c b/source/rasterizer/xbox/rasterizer_xbox_active_camouflage.c index c7305e7aac..c23208e2a3 100644 --- a/source/rasterizer/xbox/rasterizer_xbox_active_camouflage.c +++ b/source/rasterizer/xbox/rasterizer_xbox_active_camouflage.c @@ -671,6 +671,9 @@ void rasterizer_active_camouflage_draw( parameters.skinning.node_matrices = group->node_matrices; parameters.skinning.node_matrix_count = group->node_matrix_count; parameters.centroid = group->centroid; + /* port: (no sphere of the model: the original left the radius as + the stack had it, and did not read it; halo_model_bounds does) */ + parameters.radius = 0.0f; csmemset(¶meters.effect, 0, sizeof(parameters.effect)); parameters.base_map_scale = group->model_base_map_scale; if (group->lighting) diff --git a/source/rasterizer/xbox/rasterizer_xbox_environment.c b/source/rasterizer/xbox/rasterizer_xbox_environment.c index 7c138a9a18..04d4c4217c 100644 --- a/source/rasterizer/xbox/rasterizer_xbox_environment.c +++ b/source/rasterizer/xbox/rasterizer_xbox_environment.c @@ -167,9 +167,19 @@ symbols in this file: #include "rasterizer/xbox/rasterizer_xbox_pixel_shader.h" #include "rasterizer_xbox_draw_primitives.h" #include "rasterizer_xbox_internal.h" +#include "draw_bounds.h" /* port: port/linux/game/draw_bounds.c */ /* ---------- constants */ +/* port: how far past a dynamic light's radius its distance attenuation is +still sampled inside its border, as a multiple of the radius: the diffuse +pass magnifies the texture's first level alone (half a texel of it, of 8 +or more across; here a quarter of the radius), the specular pass filters +between its mipmaps, the last of which is one texel (half of it: as far +again as the radius) */ +#define DIFFUSE_LIGHT_BORDER_REACH 1.25f +#define SPECULAR_LIGHT_BORDER_REACH 2.0f + enum { _rasterizer_drawing_mode_normal = 0, @@ -1152,6 +1162,8 @@ void _rasterizer_environment_lightmap_begin( void _rasterizer_environment_diffuse_light_end( void) { + draw_bounds_none(); /* port: (the light's surfaces are drawn) */ + return; } @@ -1251,6 +1263,14 @@ void _rasterizer_environment_diffuse_light_begin( D3DDevice_SetRenderState( D3DRS_PSCONSTANT0_0, real_rgb_color_to_pixel32(&light->color)); + + /* port: the light's surfaces are drawn whole, and lit only where the + distance attenuation is sampled inside its border: the cube of the + light's radius about it, and the border's blend with the edge's + texels beyond (DIFFUSE_LIGHT_BORDER_REACH). The draws are kept to its + place in the view (port/linux/game/draw_bounds.c): the same pixels + are lit */ + draw_bounds_cube(&light->position, light->radius * DIFFUSE_LIGHT_BORDER_REACH); } return; } @@ -1767,6 +1787,8 @@ void _rasterizer_environment_diffuse_texture_draw( void _rasterizer_environment_specular_light_end( void) { + draw_bounds_none(); /* port: (the light's surfaces are drawn) */ + return; } @@ -1860,6 +1882,11 @@ void _rasterizer_environment_specular_light_begin( real_rgb_color_brightness(&light->color); inverse_radius = 1.0f / (light->definition->geometry.specular_radius_multiplier * light->radius); + /* port: as the diffuse light's surfaces (_rasterizer_environment_ + diffuse_light_begin), about the specular radius */ + draw_bounds_cube( + &light->position, + light->definition->geometry.specular_radius_multiplier * light->radius * SPECULAR_LIGHT_BORDER_REACH); vertex_constants[0].i = light->position.x; vertex_constants[0].j = light->position.y; vertex_constants[0].k = light->position.z; diff --git a/source/rasterizer/xbox/rasterizer_xbox_models.c b/source/rasterizer/xbox/rasterizer_xbox_models.c index 6484c44651..43e2aa3a78 100644 --- a/source/rasterizer/xbox/rasterizer_xbox_models.c +++ b/source/rasterizer/xbox/rasterizer_xbox_models.c @@ -620,6 +620,7 @@ void _rasterizer_model_end( viewmodel_projection_end(); /* port: (view_fov.c) */ rasterizer_set_frustum_z(0.0f, 0.0f); } + halo_model_bounds(NULL, NULL, 0.0f, 0.0f, FALSE); /* port: (the model is drawn) */ local_parameters = NULL; } #ifdef HALO_VR @@ -689,7 +690,36 @@ void _rasterizer_model_begin( skinning_work = rasterizer_frame_statistics.vertex_shader_skinning_constant_bytes - skinning_work; lighting_work = rasterizer_frame_statistics.vertex_shader_lighting_constant_bytes; - rasterizer_set_model_lighting(¶meters->lighting); + /* port: the model's bounds, for its draws (port/linux/src/d3d8_device.c): + its sphere, and its middle where the frame draws its object + (port/linux/game/render_interpolation.c). With the enhanced dynamic + lights, a model none of them reaches is drawn as with no light */ + { + real_point3d drawn = parameters->centroid; + + /* (a caller that gives no sphere gives no object either: + rasterizer_xbox_active_camouflage.c) */ + if (parameters->radius > 0.0f) + { + render_interpolation_model_centre(parameters->unique_identifier, ¶meters->centroid, &drawn); + } + if (halo_model_bounds( + (const float *)¶meters->centroid, + (const float *)&drawn, + parameters->radius, + distance3d(&global_window_parameters.camera.position, ¶meters->centroid), + TEST_FLAG(parameters->geometry_flags, _rasterizer_geometry_first_person_bit))) + { + struct render_lighting unreached = parameters->lighting; + + unreached.point_light_count = 0; + rasterizer_set_model_lighting(&unreached); + } + else + { + rasterizer_set_model_lighting(¶meters->lighting); + } + } rasterizer_frame_statistics.model_skinning_constant_bytes += skinning_work; rasterizer_frame_statistics.model_lighting_constant_bytes += diff --git a/source/rasterizer/xbox/rasterizer_xbox_shadows.c b/source/rasterizer/xbox/rasterizer_xbox_shadows.c index a49057c759..494610f128 100644 --- a/source/rasterizer/xbox/rasterizer_xbox_shadows.c +++ b/source/rasterizer/xbox/rasterizer_xbox_shadows.c @@ -89,9 +89,17 @@ symbols in this file: #include "rasterizer/xbox/rasterizer_xbox.h" #include "rasterizer/xbox/rasterizer_xbox_pixel_shader.h" #include "rasterizer/xbox/rasterizer_xbox_draw_primitives.h" +#include "draw_bounds.h" /* port: port/linux/game/draw_bounds.c */ /* ---------- constants */ +/* port: how far past a shadow's volume its map is still sampled inside its +border, as a multiple of the volume's reach: the border's blend with the +map's edge texels, of a map of 128 or more across, filtered over as many as +16 of them where the view of the surface is steep (anisotropic filtering); +here a quarter */ +#define SHADOW_BORDER_REACH 1.25f + enum { _rasterizer_target_render_primary = 0, @@ -639,6 +647,8 @@ void _rasterizer_environment_shadow_end( 563, global_d3d_device); + draw_bounds_none(); /* port: (the shadow's receivers are drawn) */ + if (global_window_parameters.rasterizer_target == _rasterizer_target_render_primary && rasterizer_debug_options.draw_environment_shadows) @@ -946,6 +956,31 @@ void _rasterizer_environment_shadow_draw( shadow_restored = TRUE; } + /* port: the shadow's receivers are the level's surfaces, whole, and + shadowed only inside the shadow's volume (render_object_shadow_end's + planes: the radius to each side, half of it toward the light and + four away), beside which the map is sampled by its border, and a + little beyond, where the border blends with the map's edge + (SHADOW_BORDER_REACH). The draws are kept to its place in the view + (port/linux/game/draw_bounds.c): the same pixels are shadowed */ + { + real const radius = local_object_bounding_radius; + real low[3]; + real high[3]; + + low[0] = low[1] = -radius * SHADOW_BORDER_REACH; + high[0] = high[1] = radius * SHADOW_BORDER_REACH; + low[2] = -radius * 0.5f * SHADOW_BORDER_REACH; + high[2] = radius * 4.0f * SHADOW_BORDER_REACH; + draw_bounds_box( + &local_shadow_matrix.position, + &local_shadow_matrix.forward, + &local_shadow_matrix.left, + &local_shadow_matrix.up, + low, + high); + } + shadow_setup = TRUE; } @@ -1294,8 +1329,30 @@ static boolean shadow_atlas_make( return TRUE; } +/* a region's cell of an atlas: the region and its half of the guard bands +about it, the cells side by side with nothing between */ +static void shadow_atlas_cell( + short region, + D3DVIEWPORT8 *viewport) +{ + viewport->X = shadow_atlas_x(region) - SHADOW_ATLAS_MARGIN; + viewport->Y = shadow_atlas_y(region) - SHADOW_ATLAS_MARGIN; + viewport->Width = SHADOW_ATLAS_SPACING; + viewport->Height = SHADOW_ATLAS_SPACING; + + return; +} + /* an atlas of the frame's class drawn into, all of it emptied first if -clear, the viewport a region's (NONE: all of it) */ +clear, the viewport a region's (NONE: all of it). + +Where a pass costs by the area it covers (d3d8_clears_alone: a tile-based +GPU, for which a class's 1024 by 256 atlas is most of the screen's tiles, +twice a frame, for one caster), each region's cell is emptied instead as +its turn comes, and the rest of the atlas left as it was: nothing samples +it. A pass's taps reach no further than the guard band (SHADOW_ATLAS_GUARD), +which is the cells', and a receiver samples its region alone +(d3d8_texture_region). The pixels drawn are the same. */ static void shadow_atlas_set_target( short atlas, short region, @@ -1310,7 +1367,22 @@ static void shadow_atlas_set_target( viewport.Height = shadow_atlas_heights[shadow_atlas_class]; viewport.MinZ = 0.0f; viewport.MaxZ = 1.0f; - if (clear) + if (region != NONE && d3d8_clears_alone()) + { + D3DVIEWPORT8 cell = viewport; + + shadow_atlas_cell(region, &cell); + IDirect3DDevice8_SetViewport(global_d3d_device, &cell); + if (clear) + { + d3d8_clear_viewport_alone(0); + } + else + { + IDirect3DDevice8_Clear(global_d3d_device, 0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); + } + } + else if (clear) { IDirect3DDevice8_SetViewport(global_d3d_device, &viewport); IDirect3DDevice8_Clear(global_d3d_device, 0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); diff --git a/tools/test_texture_alpha.py b/tools/test_texture_alpha.py new file mode 100644 index 0000000000..6ca16d507f --- /dev/null +++ b/tools/test_texture_alpha.py @@ -0,0 +1,46 @@ +"""A texture's least alpha (xgpu_texture_minimum_alpha), which the front end +drops a draw's alpha test by, against the decoder's own alphas of the same +texels (port/linux/tests/texture_alpha_test.c): the decoder built as the +Vulkan build's host builds it, beside the stand-in for the guest's +platform.h (tools/vulkan_build.py). No GPU or game data.""" +import re +import shutil +import subprocess +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parent.parent +SOURCES = ROOT / "port" / "linux" / "src" +XDK = ROOT / "port" / "include" / "xdk" + + +def xdk_enums() -> str: + """the XDK's enumerations and the texture words' fields, as the build's xdk_enums.h has them""" + pdb = (XDK / "xdk_pdb.h").read_text(encoding="utf-8") + start = pdb.index("/* ---------- enumerations") + end = pdb.index("/* ---------- structures and unions", start) + defines = [line for line in (XDK / "xdk_d3d8.h").read_text(encoding="utf-8").splitlines() + if re.match(r"#define D3D(FORMAT|SIZE|TEXTURE)_", line)] + return pdb[start:end] + "\n".join(defines) + "\n" + + +def test_texture_least_alpha(tmp_path): + cc = shutil.which("clang") or shutil.which("cc") + if not cc: + pytest.skip("needs a C compiler") + shared = tmp_path / "shared" + shared.mkdir() + for name in ("xgpu.h", "xgpu_coverage.h", "xbox_texture_decode.c"): + shutil.copy(SOURCES / name, shared / name) + shutil.copy(ROOT / "port/linux/vulkan/xgpu_vulkan_platform.h", shared / "platform.h") + (shared / "xdk_enums.h").write_text(xdk_enums(), encoding="utf-8") + binary = tmp_path / "texture_alpha_test" + decoder = tmp_path / "xbox_texture_decode.o" + subprocess.run([cc, "-std=gnu11", "-O1", "-w", f"-I{shared}", "-c", str(shared / "xbox_texture_decode.c"), "-o", + str(decoder)], check=True) + subprocess.run([cc, "-std=gnu11", "-O1", "-Wall", "-Wextra", "-Werror", f"-I{shared}", + str(ROOT / "port/linux/tests/texture_alpha_test.c"), str(decoder), "-o", str(binary), "-lm"], + check=True) + run = subprocess.run([str(binary)], capture_output=True, text=True) + assert run.returncode == 0, run.stdout + run.stderr From f3db1928759396de91db8dc5340d596c35cef5c5 Mon Sep 17 00:00:00 2001 From: Babyhamsta <22938086+Babyhamsta@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:03:42 -0600 Subject: [PATCH 3/3] Vulkan on Broadcom's V3D: passes in the main command buffer, kept to what they draw, waiting only for what they use What a Raspberry Pi 5 needed from the Vulkan backend for 60 frames a second. Each part has a switch (port/linux/vulkan/README.md) and is on by default on V3D alone (vendor 0x14e4): elsewhere the renderer does as it did. - HALO_VULKAN_VISIBILITY=shader: the lens flares' visibility counted by the pixel shader. v3dv ends each occlusion query with a compute job and a kernel cache clean; with discards on, the game crashed there now and then (v3dv_cmd_buffer_emit_set_query_availability) or the kernel reset the GPU (v3d_clean_caches), which is why the Pi had to run HALO_VULKAN_DISCARDS=0. - HALO_VULKAN_FENCE_WAIT=0: a thread tells the render thread when the GPU has done a frame, and it plays the next frame's commands meanwhile. - HALO_VULKAN_SOFT_PASSES: a pass's draws kept as a list and recorded into the main command buffer. v3dv tests no draw of a secondary command buffer by its depth before shading it. - HALO_VULKAN_CLEAR_ALONE: a pass kept to the part of its target it clears and draws. - HALO_VULKAN_NARROW_BARRIERS, HALO_VULKAN_PASS_TRANSFERS, HALO_VULKAN_OWN_MEMORY: a barrier names the vertex stages only after a write they read, a pass waits only for the copies whose images it uses, and the targets have memory of their own. v3dv makes a frame's binning wait for the frame before only when a barrier names the vertex stages, and the kernel's driver makes a job wait for every earlier job that uses a block of memory it uses. - HALO_VULKAN_ALPHA_TEST_DROP, HALO_VULKAN_DRAW_BOUNDS: the front end's capabilities of the commit before. - HALO_VULKAN_FRAMES_AHEAD=1 (off by default everywhere): under FIFO, a frame started while the one before is still on the GPU. Measured on a Raspberry Pi 5 (8 GB, Mesa 26.2.6, 640x480, no anti-aliasing), a recorded fight among the pines of Halo (a30), 36 seconds, no frame limit; the least second, the mean of the ten worst and the mean, in frames a second. The first line is the switches off with HALO_VULKAN_DISCARDS=0, which measures as master does (classic: 52.3, 59.2, 70.7); each line adds to the one before. classic lights enhanced lights as master 52.5 59.5 71.0 35.5 39.0 47.3 discards, VISIBILITY=shader 54.5 61.8 75.9 36.5 39.5 49.1 FENCE_WAIT=0 56.0 64.2 78.6 37.3 40.6 50.5 SOFT_PASSES 56.8 65.7 81.4 38.0 41.4 52.0 CLEAR_ALONE 57.0 66.6 82.7 37.5 41.8 52.5 NARROW_BARRIERS, PASS_TRANSFERS, OWN_MEMORY 62.0 70.9 88.2 38.4 42.4 53.4 ALPHA_TEST_DROP 63.0 72.7 90.6 41.0 44.3 55.6 DRAW_BOUNDS (the defaults) 65.9 77.0 95.9 45.8 56.6 75.4 Limited to 60 under FIFO with HALO_VULKAN_FRAMES_AHEAD=1, classic lights, the same fight: master 37.9 frames a second, every second under 59; this branch 59.6 to 59.8, one to four of the 36 seconds under 59 (the least 54.4). Checked: the fight's frames at a fixed frame time, lens flares off, are byte for byte master's (8a21fee4a) with classic lights (483 of 484) and with enhanced (409 of 410), and the branch's own with each switch the other way (459 to 475 frames each). The one frame that differs is the first one captured, a needle's trail just after the demo's seek, which differs between two runs of master too (394 of 395). With HALO_VULKAN_CUTOUT_PREPASS=1, 214 of 483 frames are identical and the rest differ in up to five pixels: it stays off. No crash or GPU reset in any of the runs. tools/test_vulkan_passes.py passes (new cases: the narrow barriers, a rectangle cleared alone, the transfers a pass waits for). Only the Linux arm64 build was built and run; not Windows, Android, x86-64 or VR. --- port/linux/vulkan/README.md | 46 +- port/linux/vulkan/tests/passes_test.c | 158 +++ port/linux/vulkan/xgpu_vulkan.c | 1369 +++++++++++++++++++++- port/linux/vulkan/xgpu_vulkan.h | 32 + port/linux/vulkan/xgpu_vulkan_stream.c | 27 +- port/linux/vulkan/xgpu_vulkan_world_ao.c | 6 +- 6 files changed, 1586 insertions(+), 52 deletions(-) diff --git a/port/linux/vulkan/README.md b/port/linux/vulkan/README.md index faf874a40e..9cf2628431 100644 --- a/port/linux/vulkan/README.md +++ b/port/linux/vulkan/README.md @@ -64,7 +64,10 @@ secondary command buffer begun, ended and executed for each pass and each piece of work between passes, and the draws' state bound again in each (on an RTX 3090, 22 secondary command buffers and 92 pipeline binds a frame of 250 draws), so the passes are recorded into the main command buffer as they -are drawn, the state bound in it kept from pass to pass. Each pass's draws go into a secondary command buffer, and the +are drawn, the state bound in it kept from pass to pass. Each pass's draws go into a secondary command buffer (on +Broadcom's V3D into a list of the draws, recorded into the main command +buffer when the pass is played: its driver tests no draw of a secondary +command buffer by its depth before shading it, `HALO_VULKAN_SOFT_PASSES`), and the pass's `vkCmdBeginRendering` is made at the submission, when what the later work does with its attachments is known (`xgpu_vulkan.c`, "the main command buffer in segments"): @@ -168,6 +171,17 @@ four multiview views and fragment storage-buffer atomics. | `HALO_VULKAN_DISCARDS=0`, `=1` | Every attachment stored, passes recorded in the main command buffer as drawn: discards off (they are on by default on a tiler, off on an immediate GPU: attachments stored only where something later reads them, multisampled targets resolved in their pass and, with lazily allocated memory, transient, passes put aside; above); `=1`: on, whatever the GPU | | `HALO_VULKAN_ASIDE=0` | With `HALO_VULKAN_DISCARDS`: no passes put aside, no buffers renamed (above) | | `HALO_VULKAN_TRANSIENT=0`, `=force` | With `HALO_VULKAN_DISCARDS`: no transient multisampled targets, or transient ones in ordinary memory where the device has no lazily allocated memory | +| `HALO_VULKAN_NARROW_BARRIERS=0`, `=1` | Off, every barrier names the vertex stages, as before; on (the default on Broadcom's V3D, where it was measured), one names them only after a write a vertex stage reads (a buffer's, a world ambient occlusion field's), so that a tile-based GPU bins a pass, and the next frame, while it renders the one before (`xgpu_vulkan.c`'s `AFTER_IMAGE_STAGES`). The same pixels | +| `HALO_VULKAN_PASS_TRANSFERS=0`, `=1` | With discards and `HALO_VULKAN_NARROW_BARRIERS`. Off, every pass after a copy between images waits for it, and a frame's first pass for the frame before to be presented, as before. On (the default on Broadcom's V3D) a pass waits only for the copies whose images it uses, so that a tile-based GPU draws a frame's shadows and other small passes while the frame before is presented (`xgpu_vulkan.c`, "the transfers a pass waits for"). The same pixels | +| `HALO_VULKAN_OWN_MEMORY=0`, `=1` | Off, the targets and the textures copied into while the frames are drawn share the other images' blocks of memory, as before. On (the default on Broadcom's V3D) each has memory of its own: the kernel's V3D driver has a job wait for every earlier job that uses a block it uses, whichever images they are (`xgpu_vulkan.c`, "memory of its own"). The same pixels | +| `HALO_VULKAN_DRAW_BOUNDS=0`, `=1` | On (the default on Broadcom's V3D), a dynamic light's and a shadow's draws over the level are scissored to the part of the view they can change (`d3d8_device.c`'s "the draws' bounds", `port/linux/game/draw_bounds.c`), and with the enhanced dynamic lights the level's triangles and the models no light reaches are drawn by the shader without the lights' loop ("the lightmap pass's triangles the dynamic lights reach"); off, the whole surfaces are shaded, each by the lit shader, as before. The same pixels | +| `HALO_VULKAN_CLEAR_ALONE=0`, `=1` | On (the default on Broadcom's V3D), a pass is kept to the part of its target it clears and draws (`pass_clear_end`; the shadow atlas's regions each cleared as its turn comes); off, a pass's clear is the whole target's, as before. The same pixels | +| `HALO_VULKAN_ALPHA_TEST_DROP=0`, `=1` | A draw whose alpha is a texture's, no texel of which fails its alpha test, is drawn without the test (`d3d8_device.c`, "an alpha test that cannot fail"): on by default on Broadcom's V3D, whose early depth test is not made for a shader that may discard, off elsewhere. The same pixels | +| `HALO_VULKAN_CUTOUT_PREPASS=1` | An alpha-tested model's depth drawn first, its colour then only where it shows (`d3d8_device.c`, "a cut-out's depth first"). Off unless asked for: a pixel the test cuts away at exactly the depth of the one seen there is drawn too (a fight among pines on a Raspberry Pi 5: up to five pixels in half of its frames) | +| `HALO_VULKAN_SOFT_PASSES=0`, `=1` | With `HALO_VULKAN_DISCARDS`: a pass's draws kept as a list and recorded into the main command buffer, not into a secondary one (on by default on Broadcom's V3D, whose driver tests no draw of a secondary command buffer early) | +| `HALO_VULKAN_VISIBILITY=queries`, `=shader` | The lens flares' visibility counted by occlusion queries, or by the pixel shader into a buffer (the default on Broadcom's V3D, whose driver ends each query with a compute job and a cache clean) | +| `HALO_VULKAN_FENCE_WAIT=1`, `=0` | `1`, the render thread waits for each frame's fence, as before; `0` (the default on Broadcom's V3D), a thread of the backend's tells it of the frame's completion, and it plays the next frame's commands meanwhile | +| `HALO_VULKAN_FRAMES_AHEAD=1` | Under FIFO, a frame started while the one before is still on the GPU (and no more than that one) | | `HALO_NO_VSYNC=1` | `display.vsync = false`: presented without waiting for the display (mailbox where the surface has it, else immediate) instead of FIFO | | `HALO_VULKAN_FRAME_LOG=` | A line for each frame, paced or not (`xgpu_vulkan_pacing.c`): `frame `, nanoseconds of `CLOCK_MONOTONIC`: the frame's input read, its present's return, its work done on the GPU, its time on screen (display timing's, else present wait's return), the refreshes since the frame before reached the screen (more than one: a refresh showed a frame again), whether the game was paced, display timing's earliest time and margin, and present wait's return (0: not known). `tools/latency_summary.py --frames` joins it to `debug.latency_log` | | `HALO_VULKAN_NO_PRESENT_WAIT=1`, `HALO_VULKAN_NO_DISPLAY_TIMING=1`, `HALO_VULKAN_DISPLAY_TIMING=1` | `display.low_latency` without `VK_KHR_present_wait` (as on Android: display timing asked each millisecond), or without `VK_GOOGLE_display_timing` (the frames' times present wait's returns). Where the device has present wait, display timing is not used unless `HALO_VULKAN_DISPLAY_TIMING=1` asks for both: gamescope's layer on the Steam Deck answered its past times `VK_INCOMPLETE` without end (the game showed no frame) and crashed as a swapchain was made from one whose times had been asked for | @@ -393,6 +407,36 @@ compiler does (explicit `Fma`) changed no pixel of Blood Gulch, so it is not done: the remaining differences (a few thousand pixels a frame, most by one level) are rounding in the shaders. +## Broadcom's V3D (a Raspberry Pi 5) + +What a Raspberry Pi 5 needed for a frame each refresh of a 60 Hz display is +on by default on its GPU alone (vendor 0x14e4), where each was measured and +its frames compared with the frames without it; elsewhere the renderer does +as it did, and the switches above turn each on or off on any GPU: + +- A pass's draws are recorded into the main command buffer + (`HALO_VULKAN_SOFT_PASSES`), and a pass is kept to the part of its target + it clears and draws (`HALO_VULKAN_CLEAR_ALONE`): the driver tests no draw + of a secondary command buffer early, and clears, draws and stores a pass + tile by tile over its whole area. +- A barrier names the vertex stages only after a write they read + (`HALO_VULKAN_NARROW_BARRIERS`), a pass waits only for the copies whose + images it uses (`HALO_VULKAN_PASS_TRANSFERS`), and the targets and the + textures copied into have memory of their own (`HALO_VULKAN_OWN_MEMORY`): + the driver's binning of a frame runs while the frame before is rendered, + and the kernel's driver makes a job wait for every earlier job that uses + a block of memory it uses, whichever images they are. +- The lens flares' visibility is counted by the pixel shader + (`HALO_VULKAN_VISIBILITY`): each occlusion query ends with a compute job + and a cache clean. +- An alpha test that no texel of the draw's texture fails is not made + (`HALO_VULKAN_ALPHA_TEST_DROP`): a shader that may discard is not tested + by its depth before it runs. +- A dynamic light's and a shadow's draws are kept to what they can change + (`HALO_VULKAN_DRAW_BOUNDS`). +- The render thread is told when the GPU has done a frame + (`HALO_VULKAN_FENCE_WAIT`) and plays the next frame's commands meanwhile. + ## The enhanced dynamic lights The front end's lights block and grid of cells (`xgpu.h`) are the draw set's diff --git a/port/linux/vulkan/tests/passes_test.c b/port/linux/vulkan/tests/passes_test.c index 2bf459cdd0..4507be0f7b 100644 --- a/port/linux/vulkan/tests/passes_test.c +++ b/port/linux/vulkan/tests/passes_test.c @@ -691,6 +691,18 @@ static void uploads_test(void) xgpu_vulkan_buffer_write(&buffer, 0, data, sizeof(data), 0); assert(copies == 3 && barriers == 3 && last_destination == AFTER_STAGES); assert(last_memory.dstAccessMask == AFTER_ACCESS); + /* (narrow barriers: a write no vertex stage reads, an image's, is not + put before the vertex stages, for a tile-based GPU to bin the next pass + during it; a buffer's, or a marked image's, still is) */ + vk.narrow_barriers = 1; + transfer_done(vk.slot->main, 0); + assert(last_destination == AFTER_IMAGE_STAGES && last_memory.dstAccessMask == AFTER_IMAGE_ACCESS); + assert(!(last_destination & (VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | VK_PIPELINE_STAGE_VERTEX_SHADER_BIT))); + transfer_done(vk.slot->main, 1); + assert(last_destination == AFTER_STAGES && last_memory.dstAccessMask == AFTER_ACCESS); + vk.narrow_barriers = 0; + transfer_done(vk.slot->main, 0); + assert(last_destination == AFTER_STAGES); } static VkResult VKAPI_CALL fake_reset_pool(VkDevice device, VkCommandPool pool, VkCommandPoolResetFlags flags) @@ -716,6 +728,16 @@ static void submissions_test(void) assert(last_memory.dstAccessMask == (VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT)); recording_begin(); assert(barriers == 1); + /* (narrow barriers: the frame's vertex stages wait for nothing of the + frame before, its target's writes still for the presentation blit) */ + vk.recording = 0; + vk.slots[vk.serial % SLOTS].serial = 0; + vk.narrow_barriers = 1; + barriers = 0; + recording_begin(); + assert(barriers == 1 && last_source == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); + assert(last_destination == AFTER_IMAGE_STAGES && (last_destination & VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)); + vk.narrow_barriers = 0; } static VkResult VKAPI_CALL fake_reset_descriptors(VkDevice device, VkDescriptorPool pool, VkDescriptorPoolResetFlags flags) @@ -1366,6 +1388,9 @@ static void masked_clear_test(void) static const int32_t full[1][4] = { { 0, 0, 64, 48 } }; static const int32_t two[2][4] = { { 4, 5, 10, 12 }, { -20, -20, 100, 100 } }; static const int32_t outside[1][4] = { { 70, 0, 8, 8 } }; + static const int32_t apart[1][4] = { { 32, 24, 8, 8 } }; + static const int32_t below[1][4] = { { 8, 34, 8, 6 } }, between[1][4] = { { 8, 24, 8, 8 } }; + static const float other[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; static const float color[4] = { 0.25f, 0.5f, 0.75f, 0.125f }; struct query_frame queries; const float *uniforms; @@ -1432,6 +1457,27 @@ static void masked_clear_test(void) xgpu_vulkan_clear(outside, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_ALPHA, color, 1.0f, 0); assert(!strcmp(masked_events, "BA") && !masked_draws); + /* a rectangle alone (XGPU_VULKAN_CLEAR_ALONE, a colour target with no + depth): all the target cleared as the pass begins; a rectangle cleared to + that colour after it, undrawn since, is not cleared again; to another + colour it is, and to the first over that */ + masked_reset(); + vk.depth = NULL; + xgpu_vulkan_clear(two, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS | XGPU_VULKAN_CLEAR_ALONE, color, 1.0f, 0); + assert(!strcmp(masked_events, "B") && began_color.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR); + xgpu_vulkan_clear(apart, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, color, 1.0f, 0); + assert(!strcmp(masked_events, "B")); + xgpu_vulkan_clear(apart, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, other, 1.0f, 0); + xgpu_vulkan_clear(apart, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, color, 1.0f, 0); + assert(!strcmp(masked_events, "BAA") && !masked_draws); + /* (the rectangles drawn are known each apart: one between two of them, + inside the box about both, is as the pass's clear left it) */ + xgpu_vulkan_clear(below, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, other, 1.0f, 0); + xgpu_vulkan_clear(between, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, color, 1.0f, 0); + assert(!strcmp(masked_events, "BAAA") && !masked_draws); + xgpu_vulkan_clear(below, 1, XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS, color, 1.0f, 0); + assert(!strcmp(masked_events, "BAAAA") && !masked_draws); + /* a layered target's two views (multiview), and a multisampled one */ masked_reset(); xgpu_vulkan_set_target_views(&color_target, &depth_target, 2, 2); @@ -3747,6 +3793,117 @@ static void aside_test(void) memset(&aside_other_depth, 0, sizeof(aside_other_depth)); } +/* ---------- the transfers a pass waits for */ + +/* the main command buffer's barriers that a frame or a pass begins with, in +the aside test's log: O a frame's first that names no transfer, W the one +of before (after everything, for everything), T a pass after the transfers +it uses */ +static void VKAPI_CALL transfers_barrier(VkCommandBuffer command, VkPipelineStageFlags source, VkPipelineStageFlags destination, + VkDependencyFlags dependency, uint32_t memory_count, const VkMemoryBarrier *memory, uint32_t buffer_count, + const VkBufferMemoryBarrier *buffers, uint32_t image_count, const VkImageMemoryBarrier *images) +{ + char seen = 0; + + fake_barrier(command, source, destination, dependency, memory_count, memory, buffer_count, buffers, image_count, images); + if (command != DISCARDS_MAIN) + return; + if (source == VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT && destination == AFTER_IMAGE_STAGES) + seen = 'O'; + else if (source == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT && destination == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) + seen = 'W'; + else if (source == VK_PIPELINE_STAGE_TRANSFER_BIT && destination == PASS_STAGES) + seen = 'T'; + if (seen) + { + assert(aside_log_count < sizeof(aside_log) - 1); + aside_log[aside_log_count++] = seen; + } +} + +static void transfers_log_reset(void) +{ + memset(aside_log, 0, sizeof(aside_log)); + aside_log_count = aside_executed_count = 0; +} + +/* the view, a pass of the aside target, that target copied into the other +image, the view, a pass of the other image, the view */ +static void transfers_frame(void) +{ + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + aside_draw(&aside_target, NULL); + xgpu_vulkan_image_copy(&aside_target, 0, 0, &aside_other, 0, 0); + aside_draw(&color_target, &depth_target); + aside_draw(&aside_other, NULL); + aside_draw(&color_target, &depth_target); + xgpu_vulkan_fence(); +} + +static void transfers_test(void) +{ + /* the copy's barrier is put before the first pass that uses one of its + images (the other image's, not the view's before it), once; the frame's + first barrier names no transfer */ + aside_reset(0); + vk.narrow_barriers = vk.pass_transfers = 1; + vkCmdPipelineBarrier = transfers_barrier; + transfers_frame(); + assert(!strcmp(aside_log, "OBXEBXEXBXETBXEBXE")); + assert(aside_other.transfer && aside_other.transfer == aside_target.transfer); + assert(vk.transfers_ordered == vk.transfers && !vk.barrier_carried); + + /* a pass that samples a copied image waits as one that draws into it */ + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + aside_draw(&aside_target, NULL); + xgpu_vulkan_image_copy(&aside_target, 0, 0, &aside_other, 0, 0); + aside_draw(&color_target, &depth_target); + image_use(&aside_other); + xgpu_vulkan_fence(); + assert(!strcmp(aside_log, "OBXEBXEXTBXE")); + + /* a later frame's pass does not wait again for a transfer already put + before the passes */ + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + aside_draw(&aside_other, NULL); + xgpu_vulkan_fence(); + assert(!strcmp(aside_log, "OBXEBXE")); + + /* a transfer with no number: the next frame's first barrier is the one + of before, and the frame's after it names no transfer again */ + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + vk.transfer_unnumbered = 1; + xgpu_vulkan_fence(); + assert(vk.barrier_carried); + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + xgpu_vulkan_fence(); + assert(!strcmp(aside_log, "WBXE") && !vk.barrier_carried && vk.transfers_ordered == vk.transfers); + transfers_log_reset(); + discards_clear(DISCARDS_ALL); + xgpu_vulkan_fence(); + assert(!strcmp(aside_log, "OBXE")); + + /* the negative control: without the numbers no pass is given a barrier + of its own (the copy's is for every pass after it) */ + aside_reset(0); + vk.narrow_barriers = 1; + vkCmdPipelineBarrier = transfers_barrier; + transfers_frame(); + assert(!strcmp(aside_log, "BXEBXEXBXEBXEBXE")); + assert(!aside_other.transfer && !vk.transfers); + free(aside_target.attachments); + free(aside_other.attachments); + free(aside_other_depth.attachments); + memset(&aside_target, 0, sizeof(aside_target)); + memset(&aside_other, 0, sizeof(aside_other)); + memset(&aside_other_depth, 0, sizeof(aside_other_depth)); +} + int main(void) { static const int32_t full[1][4] = { { 0, 0, 64, 48 } }; @@ -3776,6 +3933,7 @@ int main(void) half_test(); discards_test(); aside_test(); + transfers_test(); reset(); xgpu_vulkan_clear(full, 0, all, color, 0.75f, 0x123); assert(!begins && !clears); diff --git a/port/linux/vulkan/xgpu_vulkan.c b/port/linux/vulkan/xgpu_vulkan.c index 1c5ff94d35..1c74ad9468 100644 --- a/port/linux/vulkan/xgpu_vulkan.c +++ b/port/linux/vulkan/xgpu_vulkan.c @@ -333,6 +333,598 @@ void xgpu_vulkan_calls_watch(uint64_t *ticks, uint64_t *counts, uint64_t (*clock } } +/* ---------- HALO_VULKAN_SOFT_PASSES: a pass's draws kept as a list + +With discards a pass's draws are recorded into a secondary command buffer, +which is all its rendering says of them when it is begun, at the +submission. A driver that decides a pass's early depth test as the draws +are recorded cannot there: Broadcom's v3dv (Mesa 26.2 and after) tests no +draw of a secondary command buffer early, where it does not know the +framebuffer's size, and every hidden pixel of the pass is shaded. So on +such a device the pass's commands are kept in a list of the backend's own, +which stands in for the command buffer (its address as the VkCommandBuffer: +soft_of), and are recorded into the main command buffer at the submission, +between the rendering's begin and end (pass_play): the same commands in the +same order, in a primary command buffer. The commands a pass records are +the ones below; each is the driver's when the command buffer is one of its +own. */ + +enum +{ + SOFT_BIND_PIPELINE = 1, + SOFT_VIEWPORT, + SOFT_SCISSOR, + SOFT_DEPTH_BIAS, + SOFT_BLEND_CONSTANTS, + SOFT_STENCIL_REFERENCE, + SOFT_TOPOLOGY, + SOFT_VERTEX_BUFFERS, + SOFT_INDEX_BUFFER, + SOFT_DESCRIPTOR_SETS, + SOFT_PUSH_CONSTANTS, + SOFT_DRAW, + SOFT_DRAW_INDEXED, + SOFT_CLEAR_ATTACHMENTS, + SOFT_BEGIN_QUERY, + SOFT_END_QUERY, + SOFT_WRITE_TIMESTAMP, + SOFT_BARRIER +}; + +struct soft_commands +{ + /* the commands: each two words (which, and its bytes with these), then + its record, whose arrays follow it */ + unsigned char *bytes; + size_t used, size; +}; + +/* the lists of a submission's passes (all played, and free again, at the +submission: segments_play) */ +#define SOFT_MOST 4096 +static struct soft_commands soft_pool[SOFT_MOST]; +static uint32_t soft_used; + +static struct soft_commands *soft_of(VkCommandBuffer commands) +{ + uintptr_t at = (uintptr_t)commands - (uintptr_t)soft_pool; + + return at < sizeof(soft_pool) ? (struct soft_commands *)commands : NULL; +} + +static VkCommandBuffer soft_begin(void) +{ + struct soft_commands *soft; + + if (soft_used == SOFT_MOST) + host_fatal("vulkan: too many passes in a submission"); + soft = &soft_pool[soft_used++]; + soft->used = 0; + return (VkCommandBuffer)soft; +} + +static void *soft_put(struct soft_commands *soft, uint32_t which, size_t bytes) +{ + size_t total = (2 * sizeof(uint32_t) + bytes + 7) & ~(size_t)7; + uint32_t *header; + + if (soft->used + total > soft->size) + { + size_t size = soft->size ? soft->size * 2 : 16384; + unsigned char *grown; + + while (soft->used + total > size) + size *= 2; + grown = realloc(soft->bytes, size); + if (!grown) + host_fatal("vulkan: no memory for a pass's commands"); + soft->bytes = grown; + soft->size = size; + } + header = (uint32_t *)(soft->bytes + soft->used); + header[0] = which; + header[1] = (uint32_t)total; + soft->used += total; + return header + 2; +} + +struct soft_bind_pipeline { VkPipeline pipeline; VkPipelineBindPoint point; }; +struct soft_array { uint32_t first, count; }; +struct soft_depth_bias { float constant, clamp, slope; }; +struct soft_blend_constants { float constants[4]; }; +struct soft_stencil_reference { VkStencilFaceFlags faces; uint32_t reference; }; +struct soft_topology { VkPrimitiveTopology topology; }; +struct soft_index_buffer { VkBuffer buffer; VkDeviceSize offset; VkIndexType type; }; +struct soft_descriptor_sets { VkPipelineLayout layout; VkPipelineBindPoint point; uint32_t first, count, offsets; }; +struct soft_push_constants { VkPipelineLayout layout; VkShaderStageFlags stages; uint32_t offset, size; }; +struct soft_draw { uint32_t vertices, instances, first_vertex, first_instance; }; +struct soft_draw_indexed { uint32_t indices, instances, first_index; int32_t vertex_offset; uint32_t first_instance; }; +struct soft_clear_attachments { uint32_t attachments, rects; }; +struct soft_query { VkQueryPool pool; uint32_t query; VkQueryControlFlags flags; }; +struct soft_timestamp { VkQueryPool pool; VkPipelineStageFlagBits stage; uint32_t query; }; +struct soft_barrier { VkPipelineStageFlags from, to; VkDependencyFlags dependency; uint32_t count; }; + +static void soft_vkCmdBindPipeline(VkCommandBuffer commands, VkPipelineBindPoint point, VkPipeline pipeline) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_bind_pipeline *record; + + if (!soft) + { + vkCmdBindPipeline(commands, point, pipeline); + return; + } + record = soft_put(soft, SOFT_BIND_PIPELINE, sizeof(*record)); + record->pipeline = pipeline; + record->point = point; +} + +static void soft_vkCmdSetViewport(VkCommandBuffer commands, uint32_t first, uint32_t count, const VkViewport *viewports) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_array *record; + + if (!soft) + { + vkCmdSetViewport(commands, first, count, viewports); + return; + } + record = soft_put(soft, SOFT_VIEWPORT, sizeof(*record) + count * sizeof(*viewports)); + record->first = first; + record->count = count; + memcpy(record + 1, viewports, count * sizeof(*viewports)); +} + +static void soft_vkCmdSetScissor(VkCommandBuffer commands, uint32_t first, uint32_t count, const VkRect2D *scissors) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_array *record; + + if (!soft) + { + vkCmdSetScissor(commands, first, count, scissors); + return; + } + record = soft_put(soft, SOFT_SCISSOR, sizeof(*record) + count * sizeof(*scissors)); + record->first = first; + record->count = count; + memcpy(record + 1, scissors, count * sizeof(*scissors)); +} + +static void soft_vkCmdSetDepthBias(VkCommandBuffer commands, float constant, float clamp, float slope) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_depth_bias *record; + + if (!soft) + { + vkCmdSetDepthBias(commands, constant, clamp, slope); + return; + } + record = soft_put(soft, SOFT_DEPTH_BIAS, sizeof(*record)); + record->constant = constant; + record->clamp = clamp; + record->slope = slope; +} + +static void soft_vkCmdSetBlendConstants(VkCommandBuffer commands, const float constants[4]) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_blend_constants *record; + + if (!soft) + { + vkCmdSetBlendConstants(commands, constants); + return; + } + record = soft_put(soft, SOFT_BLEND_CONSTANTS, sizeof(*record)); + memcpy(record->constants, constants, sizeof(record->constants)); +} + +static void soft_vkCmdSetStencilReference(VkCommandBuffer commands, VkStencilFaceFlags faces, uint32_t reference) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_stencil_reference *record; + + if (!soft) + { + vkCmdSetStencilReference(commands, faces, reference); + return; + } + record = soft_put(soft, SOFT_STENCIL_REFERENCE, sizeof(*record)); + record->faces = faces; + record->reference = reference; +} + +static void soft_vkCmdSetPrimitiveTopology(VkCommandBuffer commands, VkPrimitiveTopology topology) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_topology *record; + + if (!soft) + { + vkCmdSetPrimitiveTopology(commands, topology); + return; + } + record = soft_put(soft, SOFT_TOPOLOGY, sizeof(*record)); + record->topology = topology; +} + +static void soft_vkCmdBindVertexBuffers(VkCommandBuffer commands, uint32_t first, uint32_t count, const VkBuffer *buffers, + const VkDeviceSize *offsets) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_array *record; + + if (!soft) + { + vkCmdBindVertexBuffers(commands, first, count, buffers, offsets); + return; + } + record = soft_put(soft, SOFT_VERTEX_BUFFERS, sizeof(*record) + count * (sizeof(*buffers) + sizeof(*offsets))); + record->first = first; + record->count = count; + memcpy(record + 1, buffers, count * sizeof(*buffers)); + memcpy((unsigned char *)(record + 1) + count * sizeof(*buffers), offsets, count * sizeof(*offsets)); +} + +static void soft_vkCmdBindIndexBuffer(VkCommandBuffer commands, VkBuffer buffer, VkDeviceSize offset, VkIndexType type) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_index_buffer *record; + + if (!soft) + { + vkCmdBindIndexBuffer(commands, buffer, offset, type); + return; + } + record = soft_put(soft, SOFT_INDEX_BUFFER, sizeof(*record)); + record->buffer = buffer; + record->offset = offset; + record->type = type; +} + +static void soft_vkCmdBindDescriptorSets(VkCommandBuffer commands, VkPipelineBindPoint point, VkPipelineLayout layout, + uint32_t first, uint32_t count, const VkDescriptorSet *sets, uint32_t offset_count, const uint32_t *offsets) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_descriptor_sets *record; + + if (!soft) + { + vkCmdBindDescriptorSets(commands, point, layout, first, count, sets, offset_count, offsets); + return; + } + record = soft_put(soft, SOFT_DESCRIPTOR_SETS, sizeof(*record) + count * sizeof(*sets) + offset_count * sizeof(*offsets)); + record->layout = layout; + record->point = point; + record->first = first; + record->count = count; + record->offsets = offset_count; + memcpy(record + 1, sets, count * sizeof(*sets)); + if (offset_count) + memcpy((unsigned char *)(record + 1) + count * sizeof(*sets), offsets, offset_count * sizeof(*offsets)); +} + +static void soft_vkCmdPushConstants(VkCommandBuffer commands, VkPipelineLayout layout, VkShaderStageFlags stages, + uint32_t offset, uint32_t size, const void *values) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_push_constants *record; + + if (!soft) + { + vkCmdPushConstants(commands, layout, stages, offset, size, values); + return; + } + record = soft_put(soft, SOFT_PUSH_CONSTANTS, sizeof(*record) + size); + record->layout = layout; + record->stages = stages; + record->offset = offset; + record->size = size; + memcpy(record + 1, values, size); +} + +static void soft_vkCmdDraw(VkCommandBuffer commands, uint32_t vertices, uint32_t instances, uint32_t first_vertex, + uint32_t first_instance) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_draw *record; + + if (!soft) + { + vkCmdDraw(commands, vertices, instances, first_vertex, first_instance); + return; + } + record = soft_put(soft, SOFT_DRAW, sizeof(*record)); + record->vertices = vertices; + record->instances = instances; + record->first_vertex = first_vertex; + record->first_instance = first_instance; +} + +static void soft_vkCmdDrawIndexed(VkCommandBuffer commands, uint32_t indices, uint32_t instances, uint32_t first_index, + int32_t vertex_offset, uint32_t first_instance) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_draw_indexed *record; + + if (!soft) + { + vkCmdDrawIndexed(commands, indices, instances, first_index, vertex_offset, first_instance); + return; + } + record = soft_put(soft, SOFT_DRAW_INDEXED, sizeof(*record)); + record->indices = indices; + record->instances = instances; + record->first_index = first_index; + record->vertex_offset = vertex_offset; + record->first_instance = first_instance; +} + +static void soft_vkCmdClearAttachments(VkCommandBuffer commands, uint32_t attachment_count, + const VkClearAttachment *attachments, uint32_t rect_count, const VkClearRect *rects) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_clear_attachments *record; + + if (!soft) + { + vkCmdClearAttachments(commands, attachment_count, attachments, rect_count, rects); + return; + } + record = soft_put(soft, SOFT_CLEAR_ATTACHMENTS, sizeof(*record) + attachment_count * sizeof(*attachments) + + rect_count * sizeof(*rects)); + record->attachments = attachment_count; + record->rects = rect_count; + memcpy(record + 1, attachments, attachment_count * sizeof(*attachments)); + memcpy((unsigned char *)(record + 1) + attachment_count * sizeof(*attachments), rects, rect_count * sizeof(*rects)); +} + +static void soft_vkCmdBeginQuery(VkCommandBuffer commands, VkQueryPool pool, uint32_t query, VkQueryControlFlags flags) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_query *record; + + if (!soft) + { + vkCmdBeginQuery(commands, pool, query, flags); + return; + } + record = soft_put(soft, SOFT_BEGIN_QUERY, sizeof(*record)); + record->pool = pool; + record->query = query; + record->flags = flags; +} + +static void soft_vkCmdEndQuery(VkCommandBuffer commands, VkQueryPool pool, uint32_t query) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_query *record; + + if (!soft) + { + vkCmdEndQuery(commands, pool, query); + return; + } + record = soft_put(soft, SOFT_END_QUERY, sizeof(*record)); + record->pool = pool; + record->query = query; + record->flags = 0; +} + +static void soft_vkCmdWriteTimestamp(VkCommandBuffer commands, VkPipelineStageFlagBits stage, VkQueryPool pool, + uint32_t query) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_timestamp *record; + + if (!soft) + { + vkCmdWriteTimestamp(commands, stage, pool, query); + return; + } + record = soft_put(soft, SOFT_WRITE_TIMESTAMP, sizeof(*record)); + record->pool = pool; + record->stage = stage; + record->query = query; +} + +/* (a pass's own barriers are of memory alone: color_stores_seen) */ +static void soft_vkCmdPipelineBarrier(VkCommandBuffer commands, VkPipelineStageFlags from, VkPipelineStageFlags to, + VkDependencyFlags dependency, uint32_t count, const VkMemoryBarrier *barriers, uint32_t buffer_count, + const VkBufferMemoryBarrier *buffers, uint32_t image_count, const VkImageMemoryBarrier *images) +{ + struct soft_commands *soft = soft_of(commands); + struct soft_barrier *record; + + if (!soft) + { + vkCmdPipelineBarrier(commands, from, to, dependency, count, barriers, buffer_count, buffers, image_count, images); + return; + } + if (buffer_count || image_count) + host_fatal("vulkan: a buffer's or an image's barrier in a pass's commands"); + record = soft_put(soft, SOFT_BARRIER, sizeof(*record) + count * sizeof(*barriers)); + record->from = from; + record->to = to; + record->dependency = dependency; + record->count = count; + memcpy(record + 1, barriers, count * sizeof(*barriers)); +} + +/* the list's commands recorded into a command buffer, in a rendering */ +static void soft_play(VkCommandBuffer commands, const struct soft_commands *soft) +{ + size_t at = 0; + + while (at < soft->used) + { + const uint32_t *header = (const uint32_t *)(soft->bytes + at); + const void *body = header + 2; + + switch (header[0]) + { + case SOFT_BIND_PIPELINE: + { + const struct soft_bind_pipeline *record = body; + + vkCmdBindPipeline(commands, record->point, record->pipeline); + break; + } + case SOFT_VIEWPORT: + { + const struct soft_array *record = body; + + vkCmdSetViewport(commands, record->first, record->count, (const VkViewport *)(record + 1)); + break; + } + case SOFT_SCISSOR: + { + const struct soft_array *record = body; + + vkCmdSetScissor(commands, record->first, record->count, (const VkRect2D *)(record + 1)); + break; + } + case SOFT_DEPTH_BIAS: + { + const struct soft_depth_bias *record = body; + + vkCmdSetDepthBias(commands, record->constant, record->clamp, record->slope); + break; + } + case SOFT_BLEND_CONSTANTS: + { + const struct soft_blend_constants *record = body; + + vkCmdSetBlendConstants(commands, record->constants); + break; + } + case SOFT_STENCIL_REFERENCE: + { + const struct soft_stencil_reference *record = body; + + vkCmdSetStencilReference(commands, record->faces, record->reference); + break; + } + case SOFT_TOPOLOGY: + { + const struct soft_topology *record = body; + + vkCmdSetPrimitiveTopology(commands, record->topology); + break; + } + case SOFT_VERTEX_BUFFERS: + { + const struct soft_array *record = body; + const VkBuffer *buffers = (const VkBuffer *)(record + 1); + + vkCmdBindVertexBuffers(commands, record->first, record->count, buffers, + (const VkDeviceSize *)(buffers + record->count)); + break; + } + case SOFT_INDEX_BUFFER: + { + const struct soft_index_buffer *record = body; + + vkCmdBindIndexBuffer(commands, record->buffer, record->offset, record->type); + break; + } + case SOFT_DESCRIPTOR_SETS: + { + const struct soft_descriptor_sets *record = body; + const VkDescriptorSet *sets = (const VkDescriptorSet *)(record + 1); + + vkCmdBindDescriptorSets(commands, record->point, record->layout, record->first, record->count, sets, + record->offsets, record->offsets ? (const uint32_t *)(sets + record->count) : NULL); + break; + } + case SOFT_PUSH_CONSTANTS: + { + const struct soft_push_constants *record = body; + + vkCmdPushConstants(commands, record->layout, record->stages, record->offset, record->size, record + 1); + break; + } + case SOFT_DRAW: + { + const struct soft_draw *record = body; + + vkCmdDraw(commands, record->vertices, record->instances, record->first_vertex, record->first_instance); + break; + } + case SOFT_DRAW_INDEXED: + { + const struct soft_draw_indexed *record = body; + + vkCmdDrawIndexed(commands, record->indices, record->instances, record->first_index, record->vertex_offset, + record->first_instance); + break; + } + case SOFT_CLEAR_ATTACHMENTS: + { + const struct soft_clear_attachments *record = body; + const VkClearAttachment *attachments = (const VkClearAttachment *)(record + 1); + + vkCmdClearAttachments(commands, record->attachments, attachments, record->rects, + (const VkClearRect *)(attachments + record->attachments)); + break; + } + case SOFT_BEGIN_QUERY: + { + const struct soft_query *record = body; + + vkCmdBeginQuery(commands, record->pool, record->query, record->flags); + break; + } + case SOFT_END_QUERY: + { + const struct soft_query *record = body; + + vkCmdEndQuery(commands, record->pool, record->query); + break; + } + case SOFT_WRITE_TIMESTAMP: + { + const struct soft_timestamp *record = body; + + vkCmdWriteTimestamp(commands, record->stage, record->pool, record->query); + break; + } + case SOFT_BARRIER: + { + const struct soft_barrier *record = body; + + vkCmdPipelineBarrier(commands, record->from, record->to, record->dependency, record->count, + (const VkMemoryBarrier *)(record + 1), 0, NULL, 0, NULL); + break; + } + } + at += header[1]; + } +} + +/* (from here a pass's commands may be for a list: the driver's functions +by these) */ +#define vkCmdBindPipeline(...) soft_vkCmdBindPipeline(__VA_ARGS__) +#define vkCmdSetViewport(...) soft_vkCmdSetViewport(__VA_ARGS__) +#define vkCmdSetScissor(...) soft_vkCmdSetScissor(__VA_ARGS__) +#define vkCmdSetDepthBias(...) soft_vkCmdSetDepthBias(__VA_ARGS__) +#define vkCmdSetBlendConstants(...) soft_vkCmdSetBlendConstants(__VA_ARGS__) +#define vkCmdSetStencilReference(...) soft_vkCmdSetStencilReference(__VA_ARGS__) +#define vkCmdSetPrimitiveTopology(...) soft_vkCmdSetPrimitiveTopology(__VA_ARGS__) +#define vkCmdBindVertexBuffers(...) soft_vkCmdBindVertexBuffers(__VA_ARGS__) +#define vkCmdBindIndexBuffer(...) soft_vkCmdBindIndexBuffer(__VA_ARGS__) +#define vkCmdBindDescriptorSets(...) soft_vkCmdBindDescriptorSets(__VA_ARGS__) +#define vkCmdPushConstants(...) soft_vkCmdPushConstants(__VA_ARGS__) +#define vkCmdDraw(...) soft_vkCmdDraw(__VA_ARGS__) +#define vkCmdDrawIndexed(...) soft_vkCmdDrawIndexed(__VA_ARGS__) +#define vkCmdClearAttachments(...) soft_vkCmdClearAttachments(__VA_ARGS__) +#define vkCmdBeginQuery(...) soft_vkCmdBeginQuery(__VA_ARGS__) +#define vkCmdEndQuery(...) soft_vkCmdEndQuery(__VA_ARGS__) +#define vkCmdWriteTimestamp(...) soft_vkCmdWriteTimestamp(__VA_ARGS__) +#define vkCmdPipelineBarrier(...) soft_vkCmdPipelineBarrier(__VA_ARGS__) + /* (VK_EXT_hdr_metadata's, where the device has it) */ static PFN_vkSetHdrMetadataEXT vkSetHdrMetadataEXT; @@ -416,6 +1008,11 @@ struct xgpu_vulkan_image VkImageView sampled, sampled_2d; VkImageView *attachments; uint32_t attachment_count; + /* a vertex stage samples it (xgpu_vulkan_image_vertex_read) */ + int vertex_read; + /* the number of the main command buffer's last transfer that read or + wrote it (0: none; "the transfers a pass waits for") */ + uint64_t transfer; /* the submission whose recording last used it (draws, copies) */ uint64_t used; /* its number, never given to another image: a texture set is cached @@ -552,6 +1149,9 @@ struct segment plus one; segments_play) */ int piece; uint32_t chain_last; + /* the greatest number of the transfers recorded in it or, a pass's, of + the images the pass uses ("the transfers a pass waits for") */ + uint64_t transfer; /* a pass's */ VkRect2D area; uint32_t view_mask; @@ -608,6 +1208,11 @@ struct shader_entry VkShaderModule module; }; +/* the rectangles a pass kept to the part of its target it covers remembers +as drawn, each apart (pass_clear_drawn): the shadow atlas's cells, 49 at +most */ +#define PASS_CLEAR_DRAWN 64 + static struct { void *library; @@ -646,6 +1251,27 @@ static struct struct slot slots[SLOTS]; struct slot *slot; int recording, early_recording; + /* the early command buffer holds work of this submission's (not only + the frame's GPU time's first timestamp) */ + int early_work; + /* a barrier names the vertex stages only after a write they can read + (AFTER_IMAGE_STAGES; HALO_VULKAN_NARROW_BARRIERS=0: after every write) */ + int narrow_barriers; + /* the transfers a pass waits for (HALO_VULKAN_PASS_TRANSFERS=0: each + transfer's own barrier, for every pass after it): the main command + buffer's transfers numbered as they are recorded, the greatest number + played into it, and the greatest a barrier has put before the passes */ + int pass_transfers; + uint64_t transfers, transfers_played, transfers_ordered; + /* the targets' and the copied textures' memory their own ("memory of its + own"; HALO_VULKAN_OWN_MEMORY=0: in the blocks, as any image's) */ + int own_memory; + /* what the next submission's first barrier must still be for, as it was + without the numbers: a barrier for the vertex stages that no pass of its + submission came after (a buffer's upload; the early commands' end, in a + submission of no pass), a transfer with no number */ + int vertex_barrier_unused, transfer_unnumbered, barrier_carried; + uint32_t passes_recorded; /* the upload ring: its buffer, mapped, and the running count of bytes given out (its position: modulo its size) */ @@ -716,6 +1342,20 @@ static struct uint32_t layer, views; int pass_active; uint32_t pass_width, pass_height; + /* the pass began by a clear of XGPU_VULKAN_CLEAR_ALONE, its colour all + cleared to this: the rectangles drawn or cleared again in it since (left, + top, right, bottom), each apart, as the shadow atlas's cells are, and past + PASS_CLEAR_DRAWN of them the box about the rest (none: right 0); the box + about all those and the rectangles a later clear found as the pass's own + clear left them, to which the pass (its segment) is kept */ + struct + { + int active; + uint32_t segment; + float color[4]; + int32_t drawn[PASS_CLEAR_DRAWN][4], drawn_rest[4], covered[4]; + uint32_t drawn_count; + } pass_clear; /* the pass's draws sample its depth attachment (xgpu_vulkan_draw's reads_depth): it writes neither depth nor stencil, nor stores them */ int pass_reads_depth; @@ -884,6 +1524,9 @@ static struct (segment_open); where the pass's commands go (the main command buffer without discards); a discarded attachment read, logged once */ int deferred, depth_resolve, transient, frame_ending; + /* HALO_VULKAN_SOFT_PASSES: with discards, the passes' draws kept as lists + (soft_commands), not recorded into secondary command buffers */ + int soft_passes; /* (not the VR mode's, which times its own frames) each frame's GPU time and the time between frames' ends counted into the statistics (debug.gpu_stats logs them), and the GPU's time so far (nanoseconds: @@ -1225,6 +1868,121 @@ static void serial_wait(uint64_t serial) ring_reclaim(); } +/* ---------- the timeline's watcher + +The render thread does not wait for the GPU at a frame's end where it can +ask whether a fence has passed (xgpu_vulkan_fence_ready) and be told when +the GPU has done more (xgpu_vulkan_completion_notify): it plays the next +frame's records while the GPU draws this one. Vulkan has no completion +callback, so a thread waits on the timeline for whatever is submitted and +calls the function given after each submission the GPU has passed. */ + +static struct +{ + pthread_t thread; + pthread_mutex_t lock; + pthread_cond_t wake; + /* the newest serial submitted, and the newest the thread has seen done */ + uint64_t wanted, seen; + int started, stopping; + void (*completed)(void); +} watcher = { .lock = PTHREAD_MUTEX_INITIALIZER, .wake = PTHREAD_COND_INITIALIZER }; + +static void *watcher_main(void *unused) +{ + (void)unused; + pthread_mutex_lock(&watcher.lock); + while (!watcher.stopping) + { + VkSemaphoreWaitInfo info; + uint64_t next, value = 0; + VkResult result; + + if (watcher.wanted <= watcher.seen) + { + pthread_cond_wait(&watcher.wake, &watcher.lock); + continue; + } + next = watcher.seen + 1; + pthread_mutex_unlock(&watcher.lock); + memset(&info, 0, sizeof(info)); + info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO; + info.semaphoreCount = 1; + info.pSemaphores = &vk.timeline; + info.pValues = &next; + /* (a wait that ends by itself: the thread is stopped between two. + Not through the calls' watch, which counts the render thread's: + xgpu_vulkan_calls_watch) */ + result = (calls_installed ? vkWaitSemaphores_unwatched : vkWaitSemaphores)(vk.device, &info, 50000000ULL); + if (result == VK_SUCCESS && (calls_installed ? vkGetSemaphoreCounterValue_unwatched : vkGetSemaphoreCounterValue)( + vk.device, vk.timeline, &value) != VK_SUCCESS) + { + value = next; + } + pthread_mutex_lock(&watcher.lock); + if (result == VK_SUCCESS) + { + if (value > watcher.seen) + watcher.seen = value; + pthread_mutex_unlock(&watcher.lock); + watcher.completed(); + pthread_mutex_lock(&watcher.lock); + } + else if (result != VK_TIMEOUT) + { + /* (a lost device: nothing more is waited for; the render thread's + own wait reports it) */ + watcher.seen = watcher.wanted; + pthread_mutex_unlock(&watcher.lock); + watcher.completed(); + pthread_mutex_lock(&watcher.lock); + } + } + pthread_mutex_unlock(&watcher.lock); + return NULL; +} + +static void watcher_submitted(uint64_t serial) +{ + if (!watcher.started) + return; + pthread_mutex_lock(&watcher.lock); + if (serial > watcher.wanted) + { + watcher.wanted = serial; + pthread_cond_signal(&watcher.wake); + } + pthread_mutex_unlock(&watcher.lock); +} + +static void watcher_stop(void) +{ + if (!watcher.started) + return; + pthread_mutex_lock(&watcher.lock); + watcher.stopping = 1; + pthread_cond_signal(&watcher.wake); + pthread_mutex_unlock(&watcher.lock); + pthread_join(watcher.thread, NULL); + watcher.started = 0; +} + +int xgpu_vulkan_completion_notify(void (*completed)(void)) +{ + if (watcher.started || !completed || !vk.device) + return watcher.started; + watcher.completed = completed; + watcher.stopping = 0; + watcher.seen = watcher.wanted = completed_serial(); + watcher.started = pthread_create(&watcher.thread, NULL, watcher_main, NULL) == 0; + return watcher.started; +} + +int xgpu_vulkan_fence_ready(uint64_t fence) +{ + return fence <= completed_serial(); +} + /* ---------- garbage */ static void garbage_add(const struct garbage *item) @@ -1302,6 +2060,28 @@ static void barrier(VkCommandBuffer commands, VkPipelineStageFlags source_stages VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | \ VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT) +/* ... with what was written to an image (a pass's attachments, a copy, a +blit or an upload into a texture): all of the above but the vertex stages, +which read the buffers and, of the images, the world ambient occlusion's +fields alone (XGPU_VULKAN_BINDING_WORLD_SDF and its table: marked, +xgpu_vulkan_image_vertex_read); a write of either still names them +(transfer_done). + +Naming them where nothing they read was written costs a tile-based GPU its +pipeline. Its driver starts a pass's geometry (the binning) while the pass +before is still being rendered, unless a barrier says that the geometry +reads what that pass writes (Mesa's V3DV: a destination with a vertex stage +has the next binning job wait for the source's last job). With every barrier +naming them, each pass's binning waited for the pass before, and each +frame's for the frame before to be rendered and copied to the swapchain: +on a Raspberry Pi 5, a firefight's frame of 8 ms had the renderer idle for +1.8 of them. The pixels are the same: only what waits for what changes. */ +#define AFTER_IMAGE_STAGES (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | \ + VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT) +#define AFTER_IMAGE_ACCESS (VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | \ + VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | \ + VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT) + /* the current submission's slot, its command buffers begun: its earlier use done first */ static void gpu_time_start(void); @@ -1310,6 +2090,93 @@ static void pass_times_mark(VkCommandBuffer main, const char *label); static const char *pass_times_label(const struct segment *segment, int aside); static void timing_mark(VkCommandBuffer main, const struct segment *segment, int aside, int outside); +/* the barrier after a write to an image, or (vertex_read) to what a vertex +stage reads */ +static void written_barrier(VkCommandBuffer commands, VkPipelineStageFlags stages, VkAccessFlags access, int vertex_read) +{ + if (vertex_read || !vk.narrow_barriers) + barrier(commands, stages, access, AFTER_STAGES, AFTER_ACCESS); + else + barrier(commands, stages, access, AFTER_IMAGE_STAGES, AFTER_IMAGE_ACCESS); +} + +/* ---------- the transfers a pass waits for + +A transfer of the main command buffer (a copy between images, an image's +levels made, the presentation's blit) is to be done before the pass that +samples or draws into its images. The barrier that says so was the +transfer's own, after it, and for the presentation's blit the next frame's +first. But a barrier names no image to a tile-based GPU's driver (Mesa's +V3DV): the next pass to begin waits for the last transfer queued, whichever +pass it is and whatever the transfer. So each frame's first pass waited for +the frame before to be presented (its blit, and the window system's copy +after it), and the passes after a copy for that copy behind them, though +only the frame's main pass uses any of them: on a Raspberry Pi 5 the +renderer sat idle for most of a millisecond of every frame, with the +shadows' and the other small passes ready to be drawn. + +Where the frame's passes are recorded in segments (vk.deferred), the barrier +is put before the pass that needs it instead. The transfers are numbered in +the order they are recorded. An image holds the number of its last one, a +pass's segment the greatest of the images it uses (image_use, +attachment_use), a segment of work outside passes the greatest it holds. +As the segments are played, a pass that uses a transfer no barrier has put +before the passes yet is given that barrier, which puts every transfer +played so far before every pass after it. The frame's first barrier then +names no transfer as its source. The pixels are the same: only what waits +for what changes. + +The early commands' transfers and the buffers' have no numbers: a vertex +stage may read what they write, and their own barriers put them before +everything, as before. Such a barrier is used by the first pass after it, +and where its submission had none the next one's first barrier is the one +of before (barrier_carried), as it is after a transfer that took no number +(the inspector's). */ + +/* the stages and accesses of a pass's fragments */ +#define PASS_STAGES (AFTER_IMAGE_STAGES & ~(VkPipelineStageFlags)VK_PIPELINE_STAGE_TRANSFER_BIT) +#define PASS_ACCESS (AFTER_IMAGE_ACCESS & ~(VkAccessFlags)(VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT)) + +static int transfers_tracked(void) +{ + return vk.narrow_barriers && vk.pass_transfers && vk.deferred; +} + +/* a transfer recorded into these commands, which wrote and read the images +(either may be none): given its number, theirs and its segment's now; 0 +where it has none (the early commands', or without segments) and its own +barrier must put it before the passes */ +static int transfer_numbered(VkCommandBuffer commands, struct xgpu_vulkan_image *written, struct xgpu_vulkan_image *read) +{ + struct segment *segment = vk.segment_open && vk.segment_count ? &vk.segments[vk.segment_count - 1] : NULL; + + if (!transfers_tracked() || !segment || segment->pass || segment->commands != commands) + return 0; + segment->transfer = ++vk.transfers; + if (written) + written->transfer = vk.transfers; + if (read) + read->transfer = vk.transfers; + return 1; +} + +/* the pass of this segment uses the image: after the image's last transfer */ +static void pass_uses_image(uint32_t segment, const struct xgpu_vulkan_image *image) +{ + if (image->transfer > vk.segments[segment].transfer) + vk.segments[segment].transfer = image->transfer; +} + +/* before a pass recorded with the work outside passes (a transient image's +clear, a resolve): after the transfers of what it draws into */ +static void pass_after_transfers(VkCommandBuffer commands, const struct xgpu_vulkan_image *image, + const struct xgpu_vulkan_image *other) +{ + if (image->transfer || (other && other->transfer)) + barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, + PASS_STAGES, PASS_ACCESS); +} + static void recording_begin(void) { struct slot *slot; @@ -1344,12 +2211,31 @@ static void recording_begin(void) /* A submission can have no early uploads. Its draws still need to wait for prior reads, notably the previous frame's presentation blit from the same color target. The timeline tracks completion for the - CPU; signaling it alone does not order the next submission's work. */ - barrier(slot->main, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT); + CPU; signaling it alone does not order the next submission's work. + Not its vertex stages (AFTER_IMAGE_STAGES): what they read was + ordered before them by its own write's barrier, in whichever + submission it was (transfer_done, the early commands' end). And with + the transfers numbered, each pass waits for those it uses itself + ("the transfers a pass waits for"): this barrier is then for the + passes before alone. */ + if (vk.barrier_carried || !vk.narrow_barriers) + { + barrier(slot->main, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT); + vk.transfers_ordered = vk.transfers; + } + else + barrier(slot->main, transfers_tracked() ? VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT : VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, + VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, AFTER_IMAGE_STAGES, + VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT); + vk.barrier_carried = 0; + vk.vertex_barrier_unused = 0; + vk.passes_recorded = 0; vk.recording = 1; vk.early_recording = 0; + vk.early_work = 0; slot->secondary_used = 0; + soft_used = 0; vk.segment_count = 0; vk.segment_open = 0; vk.bound_pipeline = VK_NULL_HANDLE; @@ -1367,7 +2253,7 @@ static void recording_begin(void) } /* the early command buffer, begun, after everything submitted before */ -static VkCommandBuffer early_commands(void) +static VkCommandBuffer early_commands_begun(void) { recording_begin(); if (!vk.early_recording) @@ -1385,6 +2271,15 @@ static VkCommandBuffer early_commands(void) return vk.slot->early; } +/* ... for work the main command buffer's comes after */ +static VkCommandBuffer early_commands(void) +{ + VkCommandBuffer commands = early_commands_begun(); + + vk.early_work = 1; + return commands; +} + static void pass_end_from(const char *caller); /* (each pass's ender named, for debug.pass_times) */ #define pass_end() pass_end_from(__func__) @@ -1575,6 +2470,9 @@ static void image_use(struct xgpu_vulkan_image *image) { if (!vk.deferred || !image) return; + /* (sampled by the pass being recorded: after its last transfer) */ + if (vk.pass_active && vk.segment_count && vk.segments[vk.segment_count - 1].pass) + pass_uses_image(vk.segment_count - 1, image); aside_image_read(image); image_first_use(image, 0, "a use outside a pass"); if (image->pass_serial == vk.serial && image->pass_segment) @@ -1605,6 +2503,7 @@ static void attachment_use(uint32_t segment, int slot, struct xgpu_vulkan_image { int aspect; + pass_uses_image(segment, image); image_first_use(image, cleared, "a pass that loads it"); image->writes++; if (image->pass_serial == vk.serial && image->pass_segment) @@ -1771,9 +2670,9 @@ static void aside_pass(uint32_t index, struct segment *segment) after it */ static void pass_barrier(VkCommandBuffer commands) { - barrier(commands, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | + written_barrier(commands, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | - VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, AFTER_STAGES, AFTER_ACCESS); + VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, 0); } /* a pass played: the first piece's to the last's (the same segment where @@ -1788,7 +2687,8 @@ static void pass_play(VkCommandBuffer main, uint32_t first, uint32_t last) memset(&rendering, 0, sizeof(rendering)); rendering.sType = VK_STRUCTURE_TYPE_RENDERING_INFO; - rendering.flags = VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT; + /* (a chain's pieces are all lists or all secondary command buffers) */ + rendering.flags = soft_of(segment->commands) ? 0 : VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT; rendering.renderArea = segment->area; rendering.layerCount = 1; rendering.viewMask = segment->view_mask; @@ -1821,11 +2721,36 @@ static void pass_play(VkCommandBuffer main, uint32_t first, uint32_t last) rendering.pDepthAttachment = &depth; rendering.pStencilAttachment = &stencil; } + /* (after the transfers its pieces use, where no barrier has put them + before the passes yet: "the transfers a pass waits for") */ + if (transfers_tracked()) + { + uint64_t transfer = 0; + + for (index = first; index <= last; index++) + { + if ((index == first || vk.segments[index].piece) && vk.segments[index].transfer > transfer) + transfer = vk.segments[index].transfer; + } + if (transfer > vk.transfers_ordered) + { + barrier(main, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, + PASS_STAGES, PASS_ACCESS); + vk.transfers_ordered = vk.transfers_played; + } + } vkCmdBeginRendering(main, &rendering); for (index = first; index <= last; index++) { if (index == first || vk.segments[index].piece) - vkCmdExecuteCommands(main, 1, &vk.segments[index].commands); + { + const struct soft_commands *soft = soft_of(vk.segments[index].commands); + + if (soft) + soft_play(main, soft); + else + vkCmdExecuteCommands(main, 1, &vk.segments[index].commands); + } } vkCmdEndRendering(main); pass_barrier(main); @@ -1849,6 +2774,8 @@ static void segments_play(void) if (!segment->pass) { vkCmdExecuteCommands(main, 1, &segment->commands); + if (segment->transfer > vk.transfers_played) + vk.transfers_played = segment->transfer; pass_times_mark(main, "outside passes"); timing_mark(main, NULL, 0, 1); continue; @@ -1873,6 +2800,8 @@ static void segments_play(void) else { vkCmdExecuteCommands(main, 1, &aside->commands); + if (aside->transfer > vk.transfers_played) + vk.transfers_played = aside->transfer; pass_times_mark(main, "outside passes"); timing_mark(main, NULL, 0, 1); } @@ -1888,6 +2817,7 @@ static void segments_play(void) timing_mark(main, segment, 0, 0); } vk.segment_count = 0; + soft_used = 0; aside_break(); } @@ -1971,7 +2901,8 @@ static void gpu_time_start(void) ; if (index == GPU_TIME_FRAMES) return; - commands = early_commands(); + /* (a timestamp is no work the main commands wait for) */ + commands = early_commands_begun(); vkResetQueryPool(vk.device, vk.gpu_time_pool, index * 2, 2); vkCmdWriteTimestamp(commands, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, vk.gpu_time_pool, index * 2); vk.gpu_times[index].used = 1; @@ -3061,6 +3992,36 @@ static VkImageView view_make(const struct xgpu_vulkan_image *image, VkImageViewT return view; } +/* ---------- memory of its own + +The images share blocks of memory (memory_for). To the kernel's driver of a +tile-based GPU (V3D's) a block is one object, and a job waits for every +earlier job that uses an object it uses, whatever the Vulkan barriers say: +its passes are one queue, its copies between images another, and a copy and +a pass that touch the same block run one after the other though their images +differ. So a pass that sampled any texture of a block waited for the copy +that presented the frame before, where that frame's target was in the block. + +The images that copies read and write while the frames are drawn (the +targets: presented, resolved, copied from; the textures copied into, +XGPU_VULKAN_USAGE_COPIED) therefore have memory of their own: a few dozen +allocations, and the textures still share theirs. With "the transfers a pass +waits for", a frame's first passes are then drawn while the frame before is +presented. Nothing drawn changes. */ +static int memory_own(const struct xgpu_vulkan_image_description *description) +{ + return vk.own_memory && (description->usage & (XGPU_VULKAN_USAGE_TARGET | XGPU_VULKAN_USAGE_COPIED)) != 0; +} + +static int memory_of_its_own(const VkMemoryRequirements *requirements, struct allocation *allocation) +{ + int type = memory_type(requirements->memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0); + + if (type >= 0 && memory_dedicated(requirements->size, (uint32_t)type, allocation)) + return 1; + return memory_for(requirements, allocation); +} + /* With discards: memory for a transient image, lazily allocated and its own where the device has such memory (vk.transient 1), else as any image's */ static int memory_transient(const VkMemoryRequirements *requirements, struct allocation *allocation) @@ -3125,6 +4086,7 @@ static void transient_clear(struct xgpu_vulkan_image *image, uint32_t layers) rendering.colorAttachmentCount = 1; rendering.pColorAttachments = &attachment; } + pass_after_transfers(commands, image, NULL); vkCmdBeginRendering(commands, &rendering); vkCmdEndRendering(commands); /* (before everything after it: the early work's end orders only its @@ -3213,7 +4175,9 @@ struct xgpu_vulkan_image *xgpu_vulkan_image_create(const struct xgpu_vulkan_imag return NULL; } vkGetImageMemoryRequirements(vk.device, image->image, &requirements); - if (!(image->transient ? memory_transient(&requirements, &image->memory) : memory_for(&requirements, &image->memory)) || + if (!(image->transient ? memory_transient(&requirements, &image->memory) : + memory_own(description) ? memory_of_its_own(&requirements, &image->memory) : + memory_for(&requirements, &image->memory)) || !check(vkBindImageMemory(vk.device, image->image, image->memory.memory, image->memory.offset), "vkBindImageMemory")) { vkDestroyImage(vk.device, image->image, NULL); @@ -3345,6 +4309,13 @@ const struct xgpu_vulkan_image_description *xgpu_vulkan_image_description(const return &image->description; } +struct xgpu_vulkan_image *xgpu_vulkan_image_vertex_read(struct xgpu_vulkan_image *image) +{ + if (image) + image->vertex_read = 1; + return image; +} + /* the commands an upload into an image goes in: the early ones where no work recorded so far in this submission uses it */ static VkCommandBuffer upload_commands(uint64_t used, int image) @@ -3360,19 +4331,50 @@ static VkCommandBuffer upload_commands(uint64_t used, int image) return early_commands(); } -/* Main transfers must be visible to draws. Early transfers also need an -order among themselves: a watched buffer page can be uploaded more than -once before any draw uses it. */ -static void transfer_done(VkCommandBuffer commands) +/* Main transfers must be visible to draws: to their vertex stages too +where those read what was written (vertex_read: a buffer, an image +marked so, AFTER_IMAGE_STAGES). Early transfers also need an order among +themselves: a watched buffer page can be uploaded more than once before +any draw uses it. */ +static void transfer_done(VkCommandBuffer commands, int vertex_read) { if (commands != vk.slot->early) - barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, - AFTER_STAGES, AFTER_ACCESS); + { + written_barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, + vertex_read); + /* (for the first pass after it: pass_begin) */ + vk.vertex_barrier_unused = 1; + } else barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT); } +/* ... of images, one written and one (or none) read: with its number, the +passes that use them wait for it themselves ("the transfers a pass waits +for"), and the barrier is for the transfers after it alone */ +static void image_transfer_done(VkCommandBuffer commands, struct xgpu_vulkan_image *written, struct xgpu_vulkan_image *read) +{ + if (!written->vertex_read && transfer_numbered(commands, written, read)) + barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT); + else + transfer_done(commands, written->vertex_read); +} + +/* ... that read an image and wrote none of the renderer's (into the +swapchain's, a buffer): a pass that draws into the image again waits by its +number, a transfer into it (the next frame's resolve) by this barrier. +Without a number, the next frame's first barrier is the wide one */ +static void image_read_done(VkCommandBuffer commands, struct xgpu_vulkan_image *read) +{ + if (transfer_numbered(commands, NULL, read)) + barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + else + vk.transfer_unnumbered = 1; +} + void xgpu_vulkan_image_upload(struct xgpu_vulkan_image *image, uint32_t level, uint32_t layer, uint32_t x, uint32_t y, uint32_t z, uint32_t width, uint32_t height, uint32_t depth, const void *data, uint32_t bytes_per_row, uint32_t bytes_per_image) @@ -3441,7 +4443,7 @@ void xgpu_vulkan_image_upload(struct xgpu_vulkan_image *image, uint32_t level, u copy.imageExtent.height = height; copy.imageExtent.depth = depth; vkCmdCopyBufferToImage(commands, range.buffer, image->image, VK_IMAGE_LAYOUT_GENERAL, 1, ©); - transfer_done(commands); + image_transfer_done(commands, image, NULL); } void xgpu_vulkan_image_make_levels(struct xgpu_vulkan_image *image, uint32_t from) @@ -3477,7 +4479,7 @@ void xgpu_vulkan_image_make_levels(struct xgpu_vulkan_image *image, uint32_t fro vkCmdBlitImage(commands, image->image, VK_IMAGE_LAYOUT_GENERAL, image->image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_LINEAR); } - transfer_done(commands); + image_transfer_done(commands, image, NULL); } void xgpu_vulkan_image_copy(struct xgpu_vulkan_image *source, uint32_t source_level, uint32_t source_layer, @@ -3519,7 +4521,7 @@ void xgpu_vulkan_image_copy(struct xgpu_vulkan_image *source, uint32_t source_le copy.srcSubresource.aspectMask = copy.dstSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; } vkCmdCopyImage(commands, source->image, VK_IMAGE_LAYOUT_GENERAL, destination->image, VK_IMAGE_LAYOUT_GENERAL, 1, ©); - transfer_done(commands); + image_transfer_done(commands, destination, source); } void xgpu_vulkan_image_copy_rectangle(struct xgpu_vulkan_image *source, uint32_t source_layer, @@ -3557,7 +4559,7 @@ void xgpu_vulkan_image_copy_rectangle(struct xgpu_vulkan_image *source, uint32_t image_use(destination); image_written(destination); vkCmdCopyImage(commands, source->image, VK_IMAGE_LAYOUT_GENERAL, destination->image, VK_IMAGE_LAYOUT_GENERAL, 1, ©); - transfer_done(commands); + image_transfer_done(commands, destination, source); } /* OpenXR guarantees this layout after xrWaitSwapchainImage and requires @@ -3610,6 +4612,8 @@ void xgpu_vulkan_image_to_runtime(struct xgpu_vulkan_image *source, VkImage dest vkCmdCopyImage(commands, source->image, VK_IMAGE_LAYOUT_GENERAL, destination, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©); } + /* (what draws or copies into the source again comes after this) */ + image_read_done(commands, source); transition.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; transition.dstAccessMask = attachment_access; transition.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; @@ -3659,6 +4663,7 @@ static int resolve_in_pass(struct xgpu_vulkan_image *multisampled, struct xgpu_v if (!view) return 0; attachment->resolve_view = view; + pass_uses_image(vk.segment_count - 1, image); multisampled->used = image->used = vk.serial; image_use(image); /* (a pass put aside resolving: what it writes checked; the chain's @@ -3722,6 +4727,7 @@ void xgpu_vulkan_image_resolve(struct xgpu_vulkan_image *multisampled, struct xg rendering.layerCount = 1; rendering.colorAttachmentCount = 1; rendering.pColorAttachments = &color; + pass_after_transfers(commands, image, multisampled); vkCmdBeginRendering(commands, &rendering); vkCmdEndRendering(commands); pass_barrier(commands); @@ -3754,10 +4760,11 @@ void xgpu_vulkan_image_resolve(struct xgpu_vulkan_image *multisampled, struct xg rendering.layerCount = 1; rendering.pDepthAttachment = &depth; rendering.pStencilAttachment = &depth; + pass_after_transfers(commands, image, multisampled); vkCmdBeginRendering(commands, &rendering); vkCmdEndRendering(commands); - barrier(commands, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, - VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, AFTER_STAGES, AFTER_ACCESS); + written_barrier(commands, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, + VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, 0); } { struct garbage item; @@ -3789,7 +4796,7 @@ void xgpu_vulkan_image_resolve(struct xgpu_vulkan_image *multisampled, struct xg resolve.extent.depth = 1; vkCmdResolveImage(commands, multisampled->image, VK_IMAGE_LAYOUT_GENERAL, image->image, VK_IMAGE_LAYOUT_GENERAL, 1, &resolve); - transfer_done(commands); + image_transfer_done(commands, image, multisampled); } } @@ -3998,7 +5005,7 @@ static int buffer_rename(struct xgpu_vulkan_buffer *buffer) memset(©, 0, sizeof(copy)); copy.size = buffer->size; vkCmdCopyBuffer(commands, old.buffer, fresh.buffer, 1, ©); - transfer_done(commands); + transfer_done(commands, 1); buffer->buffer = fresh.buffer; buffer->memory = fresh.memory; buffer->mapped = fresh.mapped; @@ -4062,7 +5069,7 @@ void xgpu_vulkan_buffer_write(struct xgpu_vulkan_buffer *buffer, uint32_t offset copy.dstOffset = offset; copy.size = size; vkCmdCopyBuffer(commands, range.buffer, buffer->buffer, 1, ©); - transfer_done(commands); + transfer_done(commands, 1); } /* ---------- samplers */ @@ -4890,13 +5897,139 @@ static void pass_segment_begin(const VkRenderingInfo *rendering) (rendering->pStencilAttachment->loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) << 1); } aside_pass(index, segment); - segment->commands = secondary_begin(&inheritance); + segment->commands = vk.soft_passes ? soft_begin() : secondary_begin(&inheritance); vk.pass_commands = segment->commands; - /* (a secondary command buffer begins with no state bound) */ + /* (a secondary command buffer begins with no state bound, and a list is + played after other passes' commands) */ vk.bound_pipeline = VK_NULL_HANDLE; memset(&vk.bound, 0, sizeof(vk.bound)); } +/* ---------- a pass kept to the part of its target it covers + +A tile-based GPU clears, draws and stores a pass tile by tile over the +pass's area, the whole target's if no one says less: the shadow atlas, 1024 +by 256 for one caster's 128 by 128, cost a Raspberry Pi 5 more than the +caster. A pass that begins with a clear of XGPU_VULKAN_CLEAR_ALONE needs +nothing of its target outside what it clears and draws: with discards (the +pass's rendering made at the submission) its area is the box about those, +out to the tiles' edges. And a rectangle cleared in such a pass to the +colour its own clear left there, undrawn since, is cleared already: no +clear in the pass, for which Broadcom's driver makes a pipeline each time +and destroys it with the frame's commands (the atlas's cells: the +rectangles drawn are kept each apart, as a box about them all meets the +cells between them, PASS_CLEAR_DRAWN). */ + +/* the largest tiles of the GPUs with tiles: an area whose edges are theirs +is cleared and stored whole tiles at a time (Broadcom's V3D loads the tiles +an area cuts) */ +#define PASS_AREA_TILE 64 + +/* a rectangle added to a box (none yet: its right not past its left); an +empty rectangle adds nothing */ +static void box_add(int32_t box[4], int32_t left, int32_t top, int32_t right, int32_t bottom) +{ + if (right <= left || bottom <= top) + return; + if (box[2] <= box[0]) + { + box[0] = left; + box[1] = top; + box[2] = right; + box[3] = bottom; + return; + } + if (left < box[0]) + box[0] = left; + if (top < box[1]) + box[1] = top; + if (right > box[2]) + box[2] = right; + if (bottom > box[3]) + box[3] = bottom; +} + +/* the pass draws, or clears again, a rectangle of its targets */ +static void pass_clear_drawn(int32_t left, int32_t top, int32_t right, int32_t bottom) +{ + uint32_t index; + + if (!vk.pass_clear.active || right <= left || bottom <= top) + return; + box_add(vk.pass_clear.covered, left, top, right, bottom); + /* (the last first: a model's draws have one scissor) */ + for (index = vk.pass_clear.drawn_count; index-- > 0;) + { + const int32_t *drawn = vk.pass_clear.drawn[index]; + + if (left >= drawn[0] && top >= drawn[1] && right <= drawn[2] && bottom <= drawn[3]) + return; + } + if (vk.pass_clear.drawn_count < PASS_CLEAR_DRAWN) + { + int32_t *drawn = vk.pass_clear.drawn[vk.pass_clear.drawn_count++]; + + drawn[0] = left; + drawn[1] = top; + drawn[2] = right; + drawn[3] = bottom; + } + else + { + box_add(vk.pass_clear.drawn_rest, left, top, right, bottom); + } +} + +static int box_meets(const int32_t box[4], int32_t left, int32_t top, int32_t right, int32_t bottom) +{ + return box[2] > box[0] && left < box[2] && right > box[0] && top < box[3] && bottom > box[1]; +} + +/* whether the pass's own clear left the rectangle as a clear of all the +colour's channels to color would: it is noted as covered */ +static int pass_clear_left(const float color[4], int32_t left, int32_t top, int32_t right, int32_t bottom) +{ + uint32_t index; + + if (!vk.pass_clear.active || memcmp(color, vk.pass_clear.color, sizeof(vk.pass_clear.color))) + return 0; + if (box_meets(vk.pass_clear.drawn_rest, left, top, right, bottom)) + return 0; + for (index = 0; index < vk.pass_clear.drawn_count; index++) + { + if (box_meets(vk.pass_clear.drawn[index], left, top, right, bottom)) + return 0; + } + box_add(vk.pass_clear.covered, left, top, right, bottom); + return 1; +} + +/* the pass ends: kept to what it covered (with discards: its segment's +area, which every rectangle it drew or cleared is inside) */ +static void pass_clear_end(void) +{ + const int32_t *covered = vk.pass_clear.covered; + + if (vk.pass_clear.active && vk.deferred && covered[2] > covered[0] && + vk.pass_clear.segment < vk.segment_count) + { + VkRect2D *area = &vk.segments[vk.pass_clear.segment].area; + int32_t left = covered[0] / PASS_AREA_TILE * PASS_AREA_TILE, top = covered[1] / PASS_AREA_TILE * PASS_AREA_TILE; + int32_t right = (covered[2] + PASS_AREA_TILE - 1) / PASS_AREA_TILE * PASS_AREA_TILE; + int32_t bottom = (covered[3] + PASS_AREA_TILE - 1) / PASS_AREA_TILE * PASS_AREA_TILE; + + if (right > (int32_t)vk.pass_width) + right = (int32_t)vk.pass_width; + if (bottom > (int32_t)vk.pass_height) + bottom = (int32_t)vk.pass_height; + area->offset.x = left; + area->offset.y = top; + area->extent.width = (uint32_t)(right - left); + area->extent.height = (uint32_t)(bottom - top); + } + vk.pass_clear.active = 0; +} + /* the pass drawing into the bound targets, begun */ static int pass_begin(uint32_t clear_parts, const float clear_color[4], float clear_depth, uint32_t clear_stencil) { @@ -4992,6 +6125,8 @@ static int pass_begin(uint32_t clear_parts, const float clear_color[4], float cl vk.pass_width = rendering.renderArea.extent.width; vk.pass_height = rendering.renderArea.extent.height; vk.statistics.passes++; + vk.passes_recorded++; + vk.vertex_barrier_unused = 0; if (vk.test_active) test_query_begin(); return 1; @@ -5022,12 +6157,16 @@ static void pass_end_body(void) { if (!vk.pass_active) return; + pass_clear_end(); color_stores_seen(vk.pass_commands); test_query_end(); /* (with discards, the pass's draws ended: its rendering, and the barrier after it, are made at the submission, segments_play) */ if (vk.deferred) - check(vkEndCommandBuffer(vk.pass_commands), "vkEndCommandBuffer (pass)"); + { + if (!soft_of(vk.pass_commands)) + check(vkEndCommandBuffer(vk.pass_commands), "vkEndCommandBuffer (pass)"); + } else { vkCmdEndRendering(vk.slot->main); @@ -5118,7 +6257,13 @@ void xgpu_vulkan_clear(const int32_t (*rectangles)[4], uint32_t count, uint32_t uint32_t attachment_count = 0, rect_count = 0, index, clear_parts = 0; struct xgpu_vulkan_pipeline_description description; struct xgpu_vulkan_draw draw; + /* (the colour's rectangle alone needed: of a colour target with no depth, + all its channels, one rectangle) */ + int alone = (parts & XGPU_VULKAN_CLEAR_ALONE) && count == 1 && vk.color && !vk.depth && + (parts & (XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS)) == (XGPU_VULKAN_CLEAR_COLOR | XGPU_VULKAN_CLEAR_CHANNELS); + int began = !vk.pass_active; + parts &= ~(uint32_t)XGPU_VULKAN_CLEAR_ALONE; if (!count || !rectangles || (!vk.color && !vk.depth)) return; if (XGPU_VULKAN_REVERSED_DEPTH) @@ -5170,9 +6315,33 @@ void xgpu_vulkan_clear(const int32_t (*rectangles)[4], uint32_t count, uint32_t break; } } + /* (the rest of the colour of no use: all of it cleared, as the pass's + load; the pass kept to what it covers, pass_clear_end) */ + if (alone) + clear_parts |= XGPU_VULKAN_CLEAR_COLOR; } if (!pass_begin(clear_parts, color, depth, stencil)) return; + if (began) + { + memset(&vk.pass_clear, 0, sizeof(vk.pass_clear)); + if (alone) + { + int64_t right = (int64_t)rectangles[0][0] + rectangles[0][2], bottom = (int64_t)rectangles[0][1] + rectangles[0][3]; + int32_t left = rectangles[0][0] > 0 ? rectangles[0][0] : 0, top = rectangles[0][1] > 0 ? rectangles[0][1] : 0; + + vk.pass_clear.active = 1; + vk.pass_clear.segment = vk.segment_count - 1; + memcpy(vk.pass_clear.color, color, sizeof(vk.pass_clear.color)); + if (right > (int64_t)vk.pass_width) + right = (int64_t)vk.pass_width; + if (bottom > (int64_t)vk.pass_height) + bottom = (int64_t)vk.pass_height; + /* (a rectangle outside the targets covers nothing: box_add) */ + if (right > left && bottom > top) + box_add(vk.pass_clear.covered, left, top, (int32_t)right, (int32_t)bottom); + } + } vk.statistics.clears++; parts &= ~clear_parts; if ((parts & XGPU_VULKAN_CLEAR_COLOR) && vk.color) @@ -5216,6 +6385,14 @@ void xgpu_vulkan_clear(const int32_t (*rectangles)[4], uint32_t count, uint32_t bottom = (int32_t)vk.pass_height; if (right <= left || bottom <= top) continue; + /* (all the colour's channels alone, where the pass's own clear left + them so: cleared already; else the pass draws there) */ + if (attachment_count == 1 && !draw.pipeline && attachments[0].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT && + pass_clear_left(color, left, top, (int32_t)right, (int32_t)bottom)) + { + continue; + } + pass_clear_drawn(left, top, (int32_t)right, (int32_t)bottom); rects[rect_count].rect.offset.x = left; rects[rect_count].rect.offset.y = top; rects[rect_count].rect.extent.width = (uint32_t)(right - left); @@ -5975,6 +7152,7 @@ static struct xgpu_vulkan_image *inspect_looked(void) /* the image resolved into, or copied into, to */ static void inspect_copy(VkCommandBuffer commands, struct xgpu_vulkan_image *image, struct xgpu_vulkan_image *to) { + vk.transfer_unnumbered = 1; if (image->description.samples > 1) { VkImageResolve resolve; @@ -6043,6 +7221,8 @@ static void inspect_after(struct xgpu_vulkan_image *image) VkDescriptorSet set; uint32_t push[6]; + /* (its compute pass and copies take no numbers: inspect_copy) */ + vk.transfer_unnumbered = 1; if (inspect.failed) return; commands = main_commands(); @@ -6266,6 +7446,7 @@ static void draw_body(const struct xgpu_vulkan_draw *draw) bottom = (int32_t)vk.pass_height; if (right <= left || bottom <= top) return; + pass_clear_drawn(left, top, right, bottom); scissor.offset.x = left; scissor.offset.y = top; scissor.extent.width = (uint32_t)(right - left); @@ -6459,13 +7640,23 @@ static uint64_t submit(VkSemaphore wait_semaphore, VkSemaphore signal_semaphore) direct_outside_mark(); if (vk.early_recording) { - barrier(vk.slot->early, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, - VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT); + /* (its transfers before the main commands' work: none, where it + holds a timestamp alone, and nothing for that work to wait for) */ + if (vk.early_work || !vk.narrow_barriers) + barrier(vk.slot->early, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT); + if (vk.early_work && !vk.passes_recorded) + vk.vertex_barrier_unused = 1; check(vkEndCommandBuffer(vk.slot->early), "vkEndCommandBuffer"); buffers[count++] = vk.slot->early; } check(vkEndCommandBuffer(vk.slot->main), "vkEndCommandBuffer"); buffers[count++] = vk.slot->main; + /* (a barrier no pass came after, a transfer with no number: the next + submission's first barrier is for them, "the transfers a pass waits + for") */ + vk.barrier_carried = vk.vertex_barrier_unused || vk.transfer_unnumbered; + vk.transfer_unnumbered = 0; serial = vk.serial; signals[0] = vk.timeline; values[0] = serial; @@ -6502,7 +7693,10 @@ uint64_t xgpu_vulkan_serial(void) uint64_t xgpu_vulkan_fence(void) { - return submit(VK_NULL_HANDLE, VK_NULL_HANDLE); + uint64_t fence = submit(VK_NULL_HANDLE, VK_NULL_HANDLE); + + watcher_submitted(fence); + return fence; } /* the upload ring's bytes the done submissions read, free again */ @@ -7356,7 +8550,7 @@ static uint64_t present(struct xgpu_vulkan_image *image, int frame) VkImageBlit blit; VkPresentInfoKHR info; VkResult result; - uint32_t index, width, height, x, y, window_width, window_height, image_width, image_height; + uint32_t index, width = 0, height = 0, x = 0, y = 0, window_width, window_height, image_width, image_height; int turns = 0, top; uint64_t serial; @@ -7451,11 +8645,6 @@ static uint64_t present(struct xgpu_vulkan_image *image, int frame) transition.subresourceRange = whole; vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &transition); - memset(&black, 0, sizeof(black)); - black.float32[3] = 1.0f; - vkCmdClearColorImage(commands, vk.swapchain_images[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &whole); - barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_ACCESS_TRANSFER_WRITE_BIT); if (image) { /* letterboxed to the image's shape, in the window as it is seen @@ -7479,6 +8668,20 @@ static uint64_t present(struct xgpu_vulkan_image *image, int frame) y = !top ? (window_height - height) / 2 : (uint32_t)(top - 1) < window_height - height ? (uint32_t)(top - 1) : window_height - height; rectangle_turned(turns, window_width, window_height, &x, &y, &width, &height); + } + /* black about the picture: not where the picture covers the window (a + clear is a job of its own on a tile-based GPU, of every tile, each + frame) */ + if (!image || x || y || width != vk.swapchain_extent.width || height != vk.swapchain_extent.height) + { + memset(&black, 0, sizeof(black)); + black.float32[3] = 1.0f; + vkCmdClearColorImage(commands, vk.swapchain_images[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &whole); + barrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_WRITE_BIT); + } + if (image) + { memset(&blit, 0, sizeof(blit)); blit.srcSubresource.aspectMask = blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; blit.srcSubresource.layerCount = blit.dstSubresource.layerCount = 1; @@ -7494,6 +8697,9 @@ static uint64_t present(struct xgpu_vulkan_image *image, int frame) image_use(image); vkCmdBlitImage(commands, image->image, VK_IMAGE_LAYOUT_GENERAL, vk.swapchain_images[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_LINEAR); + /* (what draws or copies into the image again, the next frame's, + comes after this: "the transfers a pass waits for") */ + image_read_done(commands, image); } transition.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; transition.dstAccessMask = 0; @@ -7676,7 +8882,7 @@ static void loading_caption_draw(const uint8_t *pixels, uint32_t x, uint32_t y, image_written(vk.loading_image); vkCmdBlitImage(commands, vk.loading_caption_image->image, VK_IMAGE_LAYOUT_GENERAL, vk.loading_image->image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_NEAREST); - transfer_done(commands); + image_transfer_done(commands, vk.loading_image, vk.loading_caption_image); } struct xgpu_vulkan_image *xgpu_vulkan_loading_draw(const struct loading_presentation_frame *frame, @@ -7815,7 +9021,7 @@ struct xgpu_vulkan_image *xgpu_vulkan_loading_draw(const struct loading_presenta image_written(vk.loading_image); vkCmdBlitImage(commands, vk.loading_art_image->image, VK_IMAGE_LAYOUT_GENERAL, vk.loading_image->image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_LINEAR); - transfer_done(commands); + image_transfer_done(commands, vk.loading_image, vk.loading_art_image); vk.loading_stereo_drawn = stereo; /* (the caption over it, at the image's pixels: the frame under it as the blit scaled it) */ @@ -7876,14 +9082,29 @@ presents, on gamescope's headless 60 Hz, the press reached the screen in 57 ms, against 96 ms unbounded (68 ms with one frame more allowed) */ static void frames_ahead_update(int present_window) { - int ahead = present_window && vk.swapchain && vk.present_mode == VK_PRESENT_MODE_FIFO_KHR ? 0 : -1; + /* (HALO_VULKAN_FRAMES_AHEAD=1: with FIFO the game starts a frame while + the frame before is still on the GPU, and no more than that one. A GPU + that takes most of a refresh to draw a frame otherwise idles while the + game and the render thread make the next, and the frame's time is the + sum of theirs and its own, not the longer of them) */ + static int fifo_ahead = -1; + int ahead; + + if (fifo_ahead < 0) + { + const char *asked = getenv("HALO_VULKAN_FRAMES_AHEAD"); + fifo_ahead = asked && atoi(asked) > 0 ? 1 : 0; + } + ahead = present_window && vk.swapchain && vk.present_mode == VK_PRESENT_MODE_FIFO_KHR ? fifo_ahead : -1; if (ahead + 2 == vk.frames_ahead) return; vk.frames_ahead = ahead + 2; xgpu_vulkan_pacing_frames_ahead(ahead); host_logf(HOST_LOG_INFO, "vulkan: the game's frames %s", !ahead ? - "start once the frame before is done on the GPU (FIFO)" : "ahead of the GPU as the stream allows"); + "start once the frame before is done on the GPU (FIFO)" : ahead > 0 ? + "start with at most one frame before still on the GPU (FIFO, HALO_VULKAN_FRAMES_AHEAD)" : + "ahead of the GPU as the stream allows"); } void xgpu_vulkan_frame_end(struct xgpu_vulkan_image *image, int present_window) @@ -8658,6 +9879,56 @@ static int device_make(const struct xgpu_vulkan_options *options) VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT); vk.capabilities.fragment_stores = available.features.fragmentStoresAndAtomics; + /* (Broadcom's v3dv marks each occlusion query available with a compute + job, and the kernel cleans the GPU's caches after each: a view of lens + flares had seventeen of each a frame. Mesa 26.2 crashes ending a pass of + secondary command buffers with such a query in it, HALO_VULKAN_DISCARDS's + passes. HALO_VULKAN_VISIBILITY=queries or shader: whatever the GPU) */ + { + const char *visibility = getenv("HALO_VULKAN_VISIBILITY"); + + vk.capabilities.shader_visibility = vk.capabilities.fragment_stores && + (visibility && *visibility ? !strcmp(visibility, "shader") : vk.properties.vendorID == 0x14e4); + } + /* (a GPU that shades the hidden pixels of an alpha-tested draw: Broadcom's + V3D, whose early depth test is not made for a shader that may discard + and learns nothing from one. The front end makes no alpha test that no + texel fails, the same pixels: a campaign view cost a Raspberry Pi 5 a + third more with the tests. An alpha-tested model's depth drawn first is + for who asks: a few of its pixels can differ, d3d8_device.c's "a + cut-out's depth first". HALO_VULKAN_ALPHA_TEST_DROP=0 or 1: whatever the + GPU; HALO_VULKAN_CUTOUT_PREPASS=1) */ + { + const char *drop = getenv("HALO_VULKAN_ALPHA_TEST_DROP"), *prepass = getenv("HALO_VULKAN_CUTOUT_PREPASS"); + + vk.capabilities.alpha_test_drop = drop && *drop ? atoi(drop) != 0 : vk.properties.vendorID == 0x14e4; + vk.capabilities.cutout_prepass = prepass && *prepass && atoi(prepass) != 0; + } + /* (a pass kept to the part of its target it covers, pass_clear_end; a + dynamic light's and a shadow's draws over the level scissored to where + they can change pixels, d3d8_device.c's "the draws' bounds"; a barrier + that names the vertex stages only after a write they can read, + AFTER_IMAGE_STAGES; "the transfers a pass waits for"; "memory of its + own"; the render thread told of a frame's completion, + xgpu_vulkan_completion_notify. Each is on where it was measured and its + pictures compared, Broadcom's V3D, and off elsewhere, where the renderer + does as it did. HALO_VULKAN_CLEAR_ALONE, HALO_VULKAN_DRAW_BOUNDS, + HALO_VULKAN_NARROW_BARRIERS, HALO_VULKAN_PASS_TRANSFERS, + HALO_VULKAN_OWN_MEMORY =0 or =1, HALO_VULKAN_FENCE_WAIT=1 or =0: + whatever the GPU) */ + { + const char *alone = getenv("HALO_VULKAN_CLEAR_ALONE"), *bounds = getenv("HALO_VULKAN_DRAW_BOUNDS"); + const char *narrow = getenv("HALO_VULKAN_NARROW_BARRIERS"), *pass_transfers = getenv("HALO_VULKAN_PASS_TRANSFERS"); + const char *own_memory = getenv("HALO_VULKAN_OWN_MEMORY"), *fence_wait = getenv("HALO_VULKAN_FENCE_WAIT"); + int measured = vk.properties.vendorID == 0x14e4; + + vk.capabilities.clear_alone = alone && *alone ? atoi(alone) != 0 : measured; + vk.capabilities.draw_bounds = bounds && *bounds ? atoi(bounds) != 0 : measured; + vk.narrow_barriers = narrow && *narrow ? atoi(narrow) != 0 : measured; + vk.pass_transfers = pass_transfers && *pass_transfers ? atoi(pass_transfers) != 0 : measured; + vk.own_memory = own_memory && *own_memory ? atoi(own_memory) != 0 : measured; + vk.capabilities.completion_told = fence_wait && *fence_wait ? atoi(fence_wait) == 0 : measured; + } vk.capabilities.scaled_shorts = format_has(VK_FORMAT_R16G16_SSCALED, 1, VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) && format_has(VK_FORMAT_R16G16B16_SSCALED, 1, VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) && format_has(VK_FORMAT_R16G16B16A16_SSCALED, 1, VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) && @@ -9145,7 +10416,7 @@ static void discards_choose(void) for (index = 0; index < vk.memory_properties.memoryTypeCount; index++) lazily |= (vk.memory_properties.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) != 0; vk.deferred = chosen && *chosen ? atoi(chosen) != 0 : device_tiles(lazily); - vk.depth_resolve = vk.transient = vk.aside = 0; + vk.depth_resolve = vk.transient = vk.aside = vk.soft_passes = 0; if (!vk.deferred) { host_logf(HOST_LOG_INFO, "vulkan: discards off: passes recorded into the main command buffer as drawn (%s)", @@ -9153,6 +10424,14 @@ static void discards_choose(void) return; } vk.aside = !aside || !*aside || atoi(aside) != 0; + /* (Broadcom's v3dv tests no draw of a secondary command buffer early: + "a pass's draws kept as a list", above. HALO_VULKAN_SOFT_PASSES=1 or 0: + whatever the GPU) */ + { + const char *soft = getenv("HALO_VULKAN_SOFT_PASSES"); + + vk.soft_passes = soft && *soft ? atoi(soft) != 0 : vk.properties.vendorID == 0x14e4; + } memset(&resolve, 0, sizeof(resolve)); resolve.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES; memset(&properties, 0, sizeof(properties)); @@ -9165,8 +10444,9 @@ static void discards_choose(void) vk.transient = 2; else if (lazily && !(transient && *transient && !atoi(transient))) vk.transient = 1; - host_logf(HOST_LOG_INFO, "vulkan: discards on: stores chosen at the submission; passes put aside: %s; " - "depth resolved in passes: %s; transient multisampled targets: %s", + host_logf(HOST_LOG_INFO, "vulkan: discards on: stores chosen at the submission; passes' draws: %s; " + "passes put aside: %s; depth resolved in passes: %s; transient multisampled targets: %s", + vk.soft_passes ? "lists (HALO_VULKAN_SOFT_PASSES)" : "secondary command buffers", vk.aside ? "yes" : "no (HALO_VULKAN_ASIDE=0)", vk.depth_resolve ? "yes" : "no (no SAMPLE_ZERO resolve)", vk.transient == 1 ? "lazily allocated" : vk.transient == 2 ? "forced, in ordinary memory" : lazily ? "no (HALO_VULKAN_TRANSIENT=0)" : "no (no lazily allocated memory)"); @@ -9328,6 +10608,7 @@ int xgpu_vulkan_initialize(const struct xgpu_vulkan_options *options) void xgpu_vulkan_shutdown(void) { pipeline_compiler_stop(); + watcher_stop(); if (!vk.device) return; xgpu_vulkan_image_destroy(vk.loading_image); diff --git a/port/linux/vulkan/xgpu_vulkan.h b/port/linux/vulkan/xgpu_vulkan.h index f33378f5dc..1077ebb719 100644 --- a/port/linux/vulkan/xgpu_vulkan.h +++ b/port/linux/vulkan/xgpu_vulkan.h @@ -176,6 +176,21 @@ struct xgpu_vulkan_capabilities /* pixel shaders can write storage buffers (the visibility tests' counts in the shader) */ int fragment_stores; + /* the visibility tests' counts are better made in the shader than by + occlusion queries, outside VR too (a driver whose queries are costly) */ + int shader_visibility; + /* a pixel shader that may discard gets no early depth test: the front + end makes no alpha test that cannot fail, and draws a cut-out's depth + first (xgpu_backend.h) */ + int alpha_test_drop, cutout_prepass; + /* a pass is kept to the part of its target it clears and draws + (xgpu_backend.h) */ + int clear_alone; + /* the render thread is told when the GPU has done a frame rather than + waiting for it (xgpu_vulkan_completion_notify) */ + int completion_told; + /* the front end scissors the draws the game bounds (xgpu_backend.h) */ + int draw_bounds; uint32_t multiview_views; int depth_clamp; /* Timestamp queries on the selected graphics queue. */ @@ -267,6 +282,10 @@ enum /* and read where it is drawn (an input attachment: display.hdr's back buffer) */ XGPU_VULKAN_USAGE_INPUT = 4, + /* copied into while the frames are drawn (the levels of a texture the + game renders): with a target's, its memory is its own ("memory of its + own", xgpu_vulkan.c) */ + XGPU_VULKAN_USAGE_COPIED = 8, }; struct xgpu_vulkan_image_description @@ -291,6 +310,11 @@ struct xgpu_vulkan_image *xgpu_vulkan_image_create(const struct xgpu_vulkan_imag /* (after the work recorded so far that uses it) */ void xgpu_vulkan_image_destroy(struct xgpu_vulkan_image *image); const struct xgpu_vulkan_image_description *xgpu_vulkan_image_description(const struct xgpu_vulkan_image *image); +/* marks an image (NULL: none) as one a vertex stage samples, before +anything is written to it, and returns it: what is written to it is then +ordered before the draws' vertex stages too, as a buffer's is, not before +their fragment stages alone (xgpu_vulkan.c's AFTER_IMAGE_STAGES) */ +struct xgpu_vulkan_image *xgpu_vulkan_image_vertex_read(struct xgpu_vulkan_image *image); /* writes a region of a level and layer (a cube's face, a 3D image's slices z to z + depth); bytes_per_row is a compressed format's row of blocks. In the stream's order: draws recorded before it see the old texels, draws after @@ -493,6 +517,7 @@ enum { XGPU_VULKAN_CLEAR_RED = 1, XGPU_VULKAN_CLEAR_GREEN = 2, XGPU_VULKAN_CLEAR_BLUE = 4, XGPU_VULKAN_CLEAR_ALPHA = 8, XGPU_VULKAN_CLEAR_COLOR = 16, XGPU_VULKAN_CLEAR_DEPTH = 32, XGPU_VULKAN_CLEAR_STENCIL = 64, + XGPU_VULKAN_CLEAR_ALONE = 128, XGPU_VULKAN_CLEAR_CHANNELS = 15, }; @@ -738,6 +763,13 @@ uint64_t xgpu_vulkan_fence(void); /* returns once the GPU has done the submission of that number and those before (the visibility tests' counts of the frames they end are written) */ void xgpu_vulkan_wait(uint64_t fence); +/* whether the GPU has done that submission, without waiting; and a +function called (from a thread of the backend's) after each submission of +xgpu_vulkan_fence's the GPU has done, from then on: nonzero if it will be +(the render thread plays the next frame while the GPU draws this one: +host_render_stream.h's fence_ready) */ +int xgpu_vulkan_fence_ready(uint64_t fence); +int xgpu_vulkan_completion_notify(void (*completed)(void)); /* the window's size in pixels changed (any thread): the swapchain is made again */ diff --git a/port/linux/vulkan/xgpu_vulkan_stream.c b/port/linux/vulkan/xgpu_vulkan_stream.c index 0b354313eb..f73c83ff8a 100644 --- a/port/linux/vulkan/xgpu_vulkan_stream.c +++ b/port/linux/vulkan/xgpu_vulkan_stream.c @@ -780,7 +780,7 @@ static void stream_set_targets(void *context, const struct xgpu_stream_set_targe static void stream_clear(void *context, const struct xgpu_stream_clear *record) { - uint32_t parts = record->flags & 0x7f; + uint32_t parts = record->flags & 0xff; uint64_t before; (void)context; @@ -959,7 +959,7 @@ static struct xgpu_vulkan_image *feedback_copy(uint32_t number, struct stream_ta memset(&made, 0, sizeof(made)); made.type = target->layers > 1 ? XGPU_VULKAN_IMAGE_2D_ARRAY : XGPU_VULKAN_IMAGE_2D; made.format = source->format; - made.usage = XGPU_VULKAN_USAGE_SAMPLED; + made.usage = XGPU_VULKAN_USAGE_SAMPLED | XGPU_VULKAN_USAGE_COPIED; made.width = target->width; made.height = target->height; made.depth_or_layers = target->layers; @@ -1018,7 +1018,7 @@ static struct xgpu_vulkan_image *composite_get(const struct host_render_texture_ memset(&made, 0, sizeof(made)); made.type = layers > 1 ? XGPU_VULKAN_IMAGE_2D_ARRAY : XGPU_VULKAN_IMAGE_2D; made.format = format; - made.usage = XGPU_VULKAN_USAGE_SAMPLED; + made.usage = XGPU_VULKAN_USAGE_SAMPLED | XGPU_VULKAN_USAGE_COPIED; made.width = stage->width; made.height = stage->height; made.depth_or_layers = layers; @@ -3669,6 +3669,12 @@ static void stream_wait(void *context, uint64_t fence) xgpu_vulkan_wait(fence); } +static int stream_fence_ready(void *context, uint64_t fence) +{ + (void)context; + return xgpu_vulkan_fence_ready(fence); +} + static void stream_initialize(void *context, struct xgpu_stream_capabilities *capabilities) { (void)context; @@ -3677,8 +3683,15 @@ static void stream_initialize(void *context, struct xgpu_stream_capabilities *ca capabilities->largest_target = (int32_t)stream.capabilities.largest_target; capabilities->most_samples = (int32_t)stream.capabilities.samples; capabilities->most_anisotropy = (int32_t)stream.capabilities.anisotropy; - /* (visibility counts from occlusion queries, not the pixel shaders) */ - capabilities->sample_counting = 0; + /* (visibility counts from occlusion queries, not the pixel shaders, but + where the device's queries are the costlier) */ + capabilities->sample_counting = stream.capabilities.shader_visibility != 0; + if (capabilities->sample_counting) + xgpu_vulkan_visibility_count_samples(1); + capabilities->cutout_prepass = (uint8_t)((stream.capabilities.alpha_test_drop ? 1 : 0) | + (stream.capabilities.cutout_prepass ? 2 : 0)); + capabilities->clear_alone = stream.capabilities.clear_alone != 0; + capabilities->draw_bounds = stream.capabilities.draw_bounds != 0; capabilities->bgra_vertex_colors = 1; capabilities->base_vertex = 1; capabilities->sample_mask = 1; @@ -3777,6 +3790,10 @@ const struct host_render_backend *xgpu_vulkan_stream_backend(void) backend.open = stream_open; backend.fence = stream_fence; backend.wait = stream_wait; + /* (else the render thread waits for each frame's fence: + HALO_VULKAN_FENCE_WAIT) */ + if (stream.capabilities.completion_told && xgpu_vulkan_completion_notify(host_render_stream_gpu_completed)) + backend.fence_ready = stream_fence_ready; backend.initialize = stream_initialize; backend.target_create = stream_target_create; backend.target_delete = stream_target_delete; diff --git a/port/linux/vulkan/xgpu_vulkan_world_ao.c b/port/linux/vulkan/xgpu_vulkan_world_ao.c index 4b10219208..a6d150b698 100644 --- a/port/linux/vulkan/xgpu_vulkan_world_ao.c +++ b/port/linux/vulkan/xgpu_vulkan_world_ao.c @@ -81,7 +81,8 @@ static struct xgpu_vulkan_image *world_ao_image(uint32_t part, uint32_t width, u description.height = height; description.depth_or_layers = depth; description.levels = 1; - return xgpu_vulkan_image_create(&description); + /* (the vertex stages sample the fields: XGPU_VULKAN_SET_OCCLUSION) */ + return xgpu_vulkan_image_vertex_read(xgpu_vulkan_image_create(&description)); } /* the distance-field scene's cascades: two signed bytes a texel */ @@ -97,7 +98,8 @@ static struct xgpu_vulkan_image *world_ao_cascades_image(uint32_t width, uint32_ description.height = height; description.depth_or_layers = depth; description.levels = 1; - return xgpu_vulkan_image_create(&description); + /* (the vertex stages sample the fields: XGPU_VULKAN_SET_OCCLUSION) */ + return xgpu_vulkan_image_vertex_read(xgpu_vulkan_image_create(&description)); } int xgpu_vulkan_world_ao_create(void)