diff --git a/.cursor/plans/jackdaw_feature_roadmap_2026-06-06.plan.md b/.cursor/plans/jackdaw_feature_roadmap_2026-06-06.plan.md new file mode 100644 index 0000000..6c2fb29 --- /dev/null +++ b/.cursor/plans/jackdaw_feature_roadmap_2026-06-06.plan.md @@ -0,0 +1,1406 @@ +# Blacksite Editor Roadmap: Jackdaw-Inspired Feature Tickets + +Date: 2026-06-06 + +Target repository: `Falling-Metal-Interactive/Blacksite` on `main`. + +Purpose: turn Blacksite from a game-specific editor scaffold into a more functional Bevy editor sandbox for a future game, while preserving Blacksite's existing strengths: authoring components, hydration, schema validation, in-process PIE, and a high-fidelity unified viewport. + +This document is inspired by Jackdaw's public README and book, especially its brush geometry, material browser, terrain tools, physics placement workflow, project launcher, extension model, and open challenges. It is not a recommendation to clone Jackdaw wholesale. + +## Executive summary + +Blacksite already has the hard foundation pieces: an editor crate, ordered plugin group, project settings, scene schema validation, authoring/hydration split, docked egui UI, asset browser, material/prefab/static-mesh systems, command palette, BRP, and PIE. The biggest functional gaps for an editor sandbox are native geometry authoring, better material workflow, terrain, physics placement, and a more general extension/project story. + +Recommended sequence: +- **M0 - Foundation and polish seams:** Make the sandbox easy to start, easy to extend internally, and safe to mutate. This is the prerequisite for bigger tools. +- **M1 - Brush-based blockout:** Add the Jackdaw-inspired level-blockout workflow: brush schema, draw/edit modes, CSG, per-face materials. +- **M2 - Materials and asset pipeline:** Make assets feel live and production-like: material browser, PBR grouping, drag/drop assignment, reference repair, headless processing. +- **M3 - Terrain and physics placement:** Add larger-world authoring and practical prop placement: terrain sculpting plus physics drop/settle. +- **M4 - Extensibility and runtime integration:** Turn Blacksite from a single editor crate into a future-game editor platform through static extensions and narrow remote prototypes. +- **M5 - Regression, docs, and first-hour UX:** Lock in quality: samples, tests, profiling, docs, and first-run guidance. + +## Guiding principles + +- Keep Blacksite game-ready: preserve the authoring/hydration model instead of serializing runtime ECS directly. +- Adopt Jackdaw ideas as patterns, not a wholesale format or architecture migration. +- Use one operator path for every user mutation: preview, commit, cancel, undo, redo, dirty flag, status message. +- Prefer statically linked extension APIs first. Dynamic dylib loading should be an experiment, not a foundation. +- Every modal tool needs visual preview, clear status text, Escape cancel, and grouped undo. +- Every new authoring component needs schema migration, save allowlist, validation, hydration stripping, and docs. +- Every visible workflow change updates root README controls and docs/editor indexes in the same task. + +## Feature gap map + +| Jackdaw-inspired area | Current Blacksite state | Roadmap decision | Tickets | +|---|---|---|---| +| Project launcher and scaffolds | Blacksite has ProjectWorkspace, user prefs, project open/new, and window titles. | Add launcher and templates as optional front door. | BS-JD-001, BS-JD-402 | +| JSN / future BSN scene-document model | Blacksite uses Bevy DynamicScene RON with schema stamping, migration, and authoring-only validation. | Keep RON now; add SceneDocument abstraction to make future format changes mechanical. | BS-JD-002 | +| Component metadata and reflected inspector categories | Blacksite has component registry and typed actor inspector. | Formalize metadata and use it across inspector, palette, docs, and dependency validation. | BS-JD-003 | +| Operator framework | Blacksite has EditorHistory and EditorCommandRegistry. | Unify all mutating actions behind preview/commit/cancel/undo operators. | BS-JD-004 | +| Brushes and CSG | Blacksite has primitives/static meshes, no native brush geometry yet. | Add authoring brush data, draw/edit modes, CSG, per-face material/UV. | BS-JD-101 to BS-JD-106 | +| Material browser and PBR auto-detection | Blacksite has MaterialAsset, material overrides, asset browser, thumbnails. | Add dedicated Material Browser, texture-set detection, drag/drop material assignment. | BS-JD-201 to BS-JD-203 | +| Terrain sculpting | No native terrain authoring found in inspected Blacksite editor modules. | Add heightmap/chunk terrain MVP and brush tools. | BS-JD-301 to BS-JD-303 | +| Physics placement tool | Blacksite has Avian, ColliderDesc/RigidBodyDesc, visualizers, PIE sim. | Add selected-body drop/settle/commit tool and collider diagnostics. | BS-JD-304 to BS-JD-305 | +| Dylib extensions | Blacksite has EditorPlugin, command palette, ActorInspectorSection, optional game hot reload. | Stabilize static extension API first; treat dylib as later experimental. | BS-JD-401 to BS-JD-403 | +| Remote PIE/inspector | Blacksite has in-process PIE and BRP plugin. | Prototype narrow BRP write path before out-of-process game preview. | BS-JD-404 to BS-JD-405 | + +## Milestone plan + +### M0: Foundation and polish seams + +Make the sandbox easy to start, easy to extend internally, and safe to mutate. This is the prerequisite for bigger tools. + +- **BS-JD-000 [P0] Adoption policy, license notes, and source attribution** - Document which Jackdaw ideas are being adapted, which code may be referenced, and the license/attribution rules for any direct copying. +- **BS-JD-001 [P0] Project launcher and sandbox profiles** - Add an optional launcher/start screen for recent projects, new sandbox project, open project, and last scene resume. +- **BS-JD-002 [P0] Scene document abstraction above DynamicScene RON** - Keep Blacksite's Bevy DynamicScene RON for now, but introduce a SceneDocument layer that tools edit instead of touching Bevy serialization directly. +- **BS-JD-003 [P0] Editor metadata for components** - Add explicit component metadata for category, description, hidden state, icon, dependencies, and authoring/runtime behavior. +- **BS-JD-004 [P0] Unified operator/action framework** - Unify command palette actions, history commands, asset drag/drop, and modal tools behind one operator interface with preview, commit, cancel, and undo. +- **BS-JD-005 [P1] Command palette polish and fuzzy matching** - Upgrade Ctrl+P into a fast launcher for commands, tools, panels, assets, and help. +- **BS-JD-006 [P1] Selection and editing shortcut polish** - Add box select, component copy/paste, visibility shortcut, reset transforms, and clearer selection state. +- **BS-JD-007 [P1] Viewport productivity controls** - Expand camera bookmarks, grid controls, speed controls, nudge keys, and transform constraints. + +### M1: Brush-based blockout + +Add the Jackdaw-inspired level-blockout workflow: brush schema, draw/edit modes, CSG, per-face materials. + +- **BS-JD-101 [P0] Brush authoring schema and hydration MVP** - Add a convex brush authoring component that serializes as planes/faces and hydrates into generated mesh/collider children. +- **BS-JD-102 [P0] Quick add and draw-brush tool** - Add brush creation via menu/context actions and a modal draw tool that places vertices on a face or floor, then extrudes to a convex brush. +- **BS-JD-103 [P0] Brush element edit modes** - Add vertex, edge, face, and clip edit modes for selected brushes. +- **BS-JD-104 [P1] Brush CSG operations** - Implement subtract, intersect, and convex merge for selected brushes. +- **BS-JD-105 [P1] Per-face material and UV controls** - Allow brush faces to reference textures/materials and edit UV offset, scale, and rotation. +- **BS-JD-106 [P2] Brush validation, repair, and diagnostics** - Add robust error handling for degenerate brushes, inverted normals, zero-area faces, and non-manifold results. + +### M2: Materials and asset pipeline + +Make assets feel live and production-like: material browser, PBR grouping, drag/drop assignment, reference repair, headless processing. + +- **BS-JD-201 [P0] Material Browser and shared material catalog** - Create a dedicated Material Browser panel for named PBR materials, scene-local materials, and project-wide material assets. +- **BS-JD-202 [P1] PBR texture-set auto-detection** - Detect texture sets from filenames and generate draft materials automatically. +- **BS-JD-203 [P0] Drag/drop material and texture application** - Make texture/material drag/drop work consistently for static mesh actors, primitive actors, and brush faces. +- **BS-JD-204 [P1] Live asset watcher and refresh** - Watch assets/ and refresh asset catalog, thumbnails, and material registry without manual refresh. +- **BS-JD-205 [P1] Asset rename/move reference repair** - Track asset moves/renames and repair scene/material/prefab references. +- **BS-JD-206 [P2] Headless asset processing command** - Add xtask/cargo-style command to process assets without opening the editor UI. + +### M3: Terrain and physics placement + +Add larger-world authoring and practical prop placement: terrain sculpting plus physics drop/settle. + +- **BS-JD-301 [P1] Terrain authoring schema and chunked mesh hydration** - Add a heightmap-backed TerrainDesc authoring component with chunked generated meshes. +- **BS-JD-302 [P1] Terrain sculpt tools with stroke undo** - Implement raise/lower, flatten, smooth, and noise sculpt brushes with one undo entry per stroke. +- **BS-JD-303 [P2] Terrain material layers and texture painting MVP** - Add terrain material layers with simple weight painting. +- **BS-JD-304 [P1] Physics placement tool** - Let users select props, release them into simulated gravity, then commit or cancel settled transforms. +- **BS-JD-305 [P1] Collider authoring diagnostics and overlays** - Improve collider visualization, shape switching, and error reporting. + +### M4: Extensibility and runtime integration + +Turn Blacksite from a single editor crate into a future-game editor platform through static extensions and narrow remote prototypes. + +- **BS-JD-401 [P1] Static extension API v0** - Formalize a stable, statically linked extension API before attempting dynamic loading. +- **BS-JD-402 [P2] Extension and game prototype scaffolds** - Add editor actions to create new game prototype or editor extension crates from templates. +- **BS-JD-403 [P3] Optional dylib extension exploration** - Explore dynamic extension loading only after static extension API is stable. +- **BS-JD-404 [P2] BRP remote inspector write prototype** - Prototype editing one Transform over BRP against a running game/editor world. +- **BS-JD-405 [P3] Out-of-process game preview spike** - Investigate spawning the game as a separate process and syncing selected state back to the editor. + +### M5: Regression, docs, and first-hour UX + +Lock in quality: samples, tests, profiling, docs, and first-run guidance. + +- **BS-JD-501 [P0] Editor sample scenes and regression pack** - Create curated sample scenes for brushes, materials, terrain, physics placement, and rendering. +- **BS-JD-502 [P0] Tool regression tests and undo invariants** - Add reusable tests for every modal tool and every editor operator. +- **BS-JD-503 [P2] Performance budgets and editor profiler panel** - Add performance budgets for expensive editor systems and a panel that surfaces hot operations. +- **BS-JD-504 [P0] Documentation and issue board structure** - Create docs pages and issue labels/milestones that match this roadmap. +- **BS-JD-505 [P1] First-hour usability polish pass** - Make the first hour in the editor feel guided: starter scene, tooltips, shortcut help, empty-state actions, and recovery messages. + +## Cross-cutting polish checklist + +| Checklist item | Required behavior | +|---|---| +| Tool preview | Ghost geometry, highlighted drop targets, brush rings, CSG previews, collider overlays, or material swatches before commit. | +| Commit/cancel | Space/Enter commits when appropriate; Esc/right-click cancels; cancel leaves no dirty state. | +| Undo grouping | Continuous drags, sculpt strokes, physics settle sessions, and asset drops create one meaningful undo entry. | +| Status and errors | Status bar names the active tool and next action. Failures explain cause and recovery. | +| Selection clarity | Generated/editor-only proxies resolve to source entities; active mode badge prevents confusion. | +| Performance | Large rebuilds are dirty-region based; thumbnails/assets are debounced; slow operations report diagnostics. | +| Save discipline | Generated meshes, colliders, handles, and helper entities never leak into saved scenes. | +| Docs and samples | Each feature ships with README controls, docs/editor page, and a sample scene or fixture. | + +## Issue tickets + +### BS-JD-000 - Adoption policy, license notes, and source attribution + +**Milestone:** M0 +**Priority:** P0 +**Area:** Governance + +**Summary:** Document which Jackdaw ideas are being adapted, which code may be referenced, and the license/attribution rules for any direct copying. + +**Why this matters:** Blacksite should borrow design lessons without accidentally becoming an untracked fork. Jackdaw is MIT/Apache-2.0, but copied code still needs correct attribution and review. + +**Implementation notes:** +- Create docs/adr/NNNN-jackdaw-inspired-editor-roadmap.md or docs/editor/jackdaw-roadmap.md. +- Add a short 'Inspired by' source note in docs/editor/README.md. +- If direct code is copied, preserve file-level license notes and commit with a source URL plus upstream commit hash. +- Prefer clean-room reimplementation for geometry, terrain, and operator APIs because Blacksite's authoring/hydration model differs from Jackdaw's JSN model. + +**Acceptance criteria:** +- Repo has one canonical document explaining the adoption policy. +- Each future ticket references whether it is design-inspired, API-inspired, or code-derived. +- No copied code lands without license header and source commit note. + +**Polish details:** +- Keep the document terse enough to be read before a PR. +- Add a PR checklist checkbox: 'If inspired by Jackdaw, source and license notes are included.' + +**Tests:** +- N/A - documentation and review policy. + +**Docs to update:** +- docs/editor/README.md +- root README if a visible workflow changes. + +### BS-JD-001 - Project launcher and sandbox profiles + +**Milestone:** M0 +**Priority:** P0 +**Area:** Project UX + +**Summary:** Add an optional launcher/start screen for recent projects, new sandbox project, open project, and last scene resume. + +**Why this matters:** Jackdaw starts with a project select screen. Blacksite currently behaves like an editor for one workspace; because this repo is an editor sandbox for a future game, project switching and sandbox creation should become first-class. + +**Implementation notes:** +- Add an EditorStartupState or LauncherState before full editor mode. Keep the current direct-open behavior behind a CLI flag or saved preference. +- Extend ProjectWorkspace with project_id, last_opened_scene, and project_kind: Sandbox, GamePrototype, Imported. +- Add assets/project.ron fields for display_name, default_scene, project_template_version, and editor_capabilities. +- Implement UI under crates/editor/src/project/launcher.rs and register it before EditorUiPlugin shows the dock layout. +- Add File > Switch Project and File > Reveal Project Folder. + +**Acceptance criteria:** +- First launch can create a new sandbox project with assets/, assets/levels/, assets/materials/, assets/models/, assets/textures/. +- Recent projects list filters missing folders and shows last opened scene. +- Existing Blacksite behavior still works when opening the default repo project. +- Window title and SceneIo active path update after switching. + +**Polish details:** +- Use large cards: New Sandbox, Open Project, Recent Projects. +- Show validation badges for missing Cargo.toml, missing assets/project.ron, or missing default scene. +- Do not load the game/editor world until a project is selected, or clearly show a loading overlay if startup still preloads systems. + +**Tests:** +- Unit test project manifest read/write defaults. +- Integration smoke: create temp project, open it, create scene, save, restart preference load. + +**Docs to update:** +- README Running +- docs/editor/architecture.md project workspace section. + +### BS-JD-002 - Scene document abstraction above DynamicScene RON + +**Milestone:** M0 +**Priority:** P0 +**Area:** Scene I/O + +**Summary:** Keep Blacksite's Bevy DynamicScene RON for now, but introduce a SceneDocument layer that tools edit instead of touching Bevy serialization directly. + +**Why this matters:** Jackdaw's JSN docs emphasize line-diffable scene data and future BSN migration. Blacksite already has schema stamping and authoring-only validation; a document layer lets future tools, diffs, prefab overrides, and possible BSN migration avoid rewriting every tool. + +**Implementation notes:** +- Create crates/scene/src/document.rs with SceneDocument, SceneEntity, SceneComponentBlob, and ScenePatch. +- SceneDocument initially round-trips to/from current DynamicScene RON, including schema header. +- Update SceneIo save/load path to: World -> SceneDocument -> DynamicScene RON text, and RON text -> SceneDocument -> World. +- Add patch operations: set_component, remove_component, add_entity, delete_entity, reparent, set_name, set_transform. +- Keep raw Bevy Entity IDs out of SceneDocument; use ActorId and ComponentInstanceId. + +**Acceptance criteria:** +- Existing levels save byte-equivalent enough for current validator, or a documented one-time formatting migration occurs. +- All save validation still rejects hydrated runtime components. +- At least transform edit, rename, and component add/remove can be expressed as ScenePatch. + +**Polish details:** +- Add a small debug panel showing the selected entity's document identity and dirty patches. +- Make scene diff output readable in logs for failed migrations. +- Do not expose SceneDocument to normal users yet; this is an internal seam. + +**Tests:** +- Round-trip current assets/levels/editor_scene.scn.ron through SceneDocument. +- Golden tests for schema v1->v2 migration still pass. +- Patch tests verify stable ActorId and child order. + +**Docs to update:** +- docs/editor/architecture.md scene authoring flow +- docs/adr if BSN direction is adopted. + +### BS-JD-003 - Editor metadata for components + +**Milestone:** M0 +**Priority:** P0 +**Area:** Inspector + +**Summary:** Add explicit component metadata for category, description, hidden state, icon, dependencies, and authoring/runtime behavior. + +**Why this matters:** Jackdaw uses editor metadata and registry filtering for custom components and picker UX. Blacksite already has a registry-driven Add Component footer, but metadata should become the single source for inspector/palette/hierarchy presentation. + +**Implementation notes:** +- Extend ui/component_registry.rs or add shared::EditorComponentMeta. +- Fields: type_path, display_name, category, description, icon, default_factory, hidden, requires, conflicts_with, hydration_effect. +- Register metadata from shared authoring components at startup; allow game/editor extensions to register more. +- Use metadata in Inspector Add Component, component card header, command palette, and docs generation. +- Add 'why disabled' messages when required dependencies are missing or conflicts exist. + +**Acceptance criteria:** +- Add Component palette groups components by Authoring, Rendering, Physics, Gameplay, Volumes, Experimental. +- Hidden/runtime-only components never appear in the add list. +- Selecting a component shows one-line docs and dependency/conflict hints. +- Adding ColliderDesc can optionally add RigidBodyDesc based on metadata requirements. + +**Polish details:** +- Use icons and short descriptions; avoid raw Rust type paths in normal UI. +- Add search aliases like 'sun' -> LightDesc, 'spawn' -> PlayerSpawn. +- Show component active/inactive state consistently with existing InspectorOrder behavior. + +**Tests:** +- Registry snapshot test ensures all shared authoring components have metadata. +- UI-independent tests for dependency/conflict logic. + +**Docs to update:** +- docs/editor/README.md subsystem table +- README Inspector controls. + +### BS-JD-004 - Unified operator/action framework + +**Milestone:** M0 +**Priority:** P0 +**Area:** Undo/Redo + +**Summary:** Unify command palette actions, history commands, asset drag/drop, and modal tools behind one operator interface with preview, commit, cancel, and undo. + +**Why this matters:** Jackdaw routes editor actions such as ApplyTextureOp through an operator stack. Blacksite currently has EditorCommandRegistry and EditorHistory, but tools can be cleaner if every user action follows a standard lifecycle. + +**Implementation notes:** +- Add crates/editor/src/action or crates/editor/src/tools/operator.rs. +- Define trait EditorOperator { id, label, can_start, begin, update, preview, commit, cancel, undo_label }. +- Bridge existing EditorHistory commands into operators: Spawn, Despawn, SetTransform, SetMaterial, AddComponent. +- Command palette invokes operators by ID, not ad hoc closures. +- Modal tools store active operator state in an ActiveOperator resource and show status bar help. + +**Acceptance criteria:** +- Asset drag/drop, component add/remove, transform gizmo commit, and material apply all create consistent undo entries. +- Active operator can be canceled with Esc without dirtying the scene. +- Status bar shows the active tool, shortcut hints, and commit/cancel actions. + +**Polish details:** +- Every operator has a user-facing label; undo menu says 'Undo Move Selection' not 'Undo command'. +- Operators can produce non-blocking warnings rendered in the viewport or status bar. +- Use grouping: drag strokes and gizmo drags produce one undo entry, not per-frame spam. + +**Tests:** +- Operator lifecycle unit tests: begin -> preview -> cancel leaves world unchanged. +- Commit -> undo -> redo snapshot test for transform and material. + +**Docs to update:** +- docs/editor/architecture.md Undo/history section. + +### BS-JD-005 - Command palette polish and fuzzy matching + +**Milestone:** M0 +**Priority:** P1 +**Area:** UI + +**Summary:** Upgrade Ctrl+P into a fast launcher for commands, tools, panels, assets, and help. + +**Why this matters:** Blacksite already has a command palette; Jackdaw has fuzzy picker infrastructure and many editor actions exposed through shortcuts and menus. + +**Implementation notes:** +- Add fuzzy scoring module or small dependency-free scorer similar to jackdaw_fuzzy concept. +- Command entries include category, keywords, shortcut, disabled reason, and optional icon. +- Palette sources: EditorCommandRegistry, EditorOperator registry, panels, recent assets, recent scenes. +- Support prefix filters: > commands, @ assets, # panels, ? help. + +**Acceptance criteria:** +- Ctrl+P opens focused input; Enter runs top match. +- Disabled commands show why they are disabled. +- Recently used commands float slightly higher. +- Help entries can open docs or the shortcuts window. + +**Polish details:** +- Keyboard-only navigation with up/down/enter/escape. +- Highlight matched characters. +- Keep palette width stable and avoid reflow flicker as results update. + +**Tests:** +- Fuzzy scorer deterministic tests. +- Command registry tests for duplicate IDs. + +**Docs to update:** +- README controls +- docs/editor/README.md command palette. + +### BS-JD-006 - Selection and editing shortcut polish + +**Milestone:** M0 +**Priority:** P1 +**Area:** Viewport + +**Summary:** Add box select, component copy/paste, visibility shortcut, reset transforms, and clearer selection state. + +**Why this matters:** Jackdaw documents Shift+drag box select, Ctrl+C/Ctrl+V component copy/paste, H visibility toggle, and Alt+G/R/S transform resets. Blacksite has multi-select and selection cycling; this rounds out editor muscle memory. + +**Implementation notes:** +- Add ViewportMarquee resource and draw overlay rectangle in viewport_chrome.rs. +- Use scene_view_ray to form a selection frustum for authored LevelObject bounds. +- Add InspectorClipboard support for selected component copies, building on existing inspector clipboard. +- Add shortcut handlers in ui/selection_ops.rs or new tools/selection_shortcuts.rs. +- Visibility shortcut writes EditorVisibility through history. + +**Acceptance criteria:** +- Shift+LMB drag selects authored objects inside marquee. +- Ctrl+C copies selected component values; Ctrl+V pastes compatible components to selected entity/entities. +- H toggles editor visibility through undoable command. +- Alt+G/Alt+R/Alt+S reset position/rotation/scale with undo. + +**Polish details:** +- Marquee draws even over dark scenes and shows selection count before release. +- Clipboard paste shows '3 components pasted to 4 actors' status. +- Conflicting component paste warns instead of silently doing nothing. + +**Tests:** +- Selection frustum math tests using sample bounds. +- Clipboard round-trip for MaterialDesc, LightDesc, ColliderDesc. + +**Docs to update:** +- README Editor Controls. + +### BS-JD-007 - Viewport productivity controls + +**Milestone:** M0 +**Priority:** P1 +**Area:** Viewport + +**Summary:** Expand camera bookmarks, grid controls, speed controls, nudge keys, and transform constraints. + +**Why this matters:** Blacksite has focus, grid, clean view, and two camera bookmark shortcuts. Jackdaw exposes 1-9 bookmarks, RMB+scroll speed, grid step changes, arrow nudge, and axis constraints. + +**Implementation notes:** +- Extend CameraBookmarks to map scene_key + slot 1..9 -> Transform. +- Ctrl+1..9 saves, 1..9 restores when viewport focused and no text input is active. +- RMB+scroll adjusts EditorCamera.fly_speed and persists to user prefs as optional override. +- Add grid size up/down controls with [ and ], tied to ViewportSettings.snap_increment. +- Add nudge operators for selected transforms: Arrow keys, PageUp/PageDown, Alt+Arrows rotate 90 degrees. +- During transform gizmo drag, Ctrl toggles snap and X/Y/Z constrains axis if supported by transform-gizmo-bevy; otherwise add manual nudge constraints first. + +**Acceptance criteria:** +- Nine camera bookmarks work per scene. +- Grid size visible in viewport status HUD. +- Nudge movement uses current snap increment and creates one undo entry per keypress. +- RMB+scroll speed changes are visible in status bar. + +**Polish details:** +- Small bottom-right HUD: Grid 1.0m | Snap On | Speed 14. +- Do not steal number keys when renaming or typing in inspector fields. +- Add reset speed button in shortcuts/help overlay. + +**Tests:** +- Bookmark save/restore tests. +- Nudge transform history test. + +**Docs to update:** +- README Editor Controls +- docs/editor/README.md viewport. + +### BS-JD-101 - Brush authoring schema and hydration MVP + +**Milestone:** M1 +**Priority:** P0 +**Area:** Brushes + +**Summary:** Add a convex brush authoring component that serializes as planes/faces and hydrates into generated mesh/collider children. + +**Why this matters:** Jackdaw's strongest worldbuilding feature is brush-based geometry similar to TrenchBroom/Hammer. For a sandbox with no game yet, this gives fast blockout without external DCC tools. + +**Implementation notes:** +- Add shared::BrushDesc with faces: Vec; each face stores plane, material_ref, uv_offset, uv_scale, uv_rotation, smoothing_group. +- Add shared::BrushKind: Additive, SubtractiveMarker (subtractive can be inactive until CSG ticket). +- Create new crate crates/geometry or module shared::brush_math for convex validation, face polygon reconstruction, triangulation, normal generation. +- Hydration spawns EditorOnly generated mesh children or uses hydrated components on the brush entity, but save strips all generated mesh/collider runtime data. +- Add ActorKind::Brush or treat as StaticMesh with BrushDesc; prefer ActorKind::Brush for validation clarity. +- Register BrushDesc reflection and scene allowlist in SceneIo. + +**Acceptance criteria:** +- Add cube brush creates an authored LevelObject with BrushDesc and visible mesh. +- Save/load round-trips brush data without serializing generated Mesh3d/MeshMaterial3d. +- Invalid brush data displays a warning marker and does not panic. +- Brush can generate a static collider when ColliderDesc is enabled. + +**Polish details:** +- Generated face children must be hidden from hierarchy and scene save. +- Selection should resolve generated face clicks back to the brush until face-edit mode exists. +- Show brush wireframe and face normals when selected. + +**Tests:** +- Geometry tests: cube face reconstruction, triangulation, normals. +- Scene save test: no hydrated brush mesh markers in .scn.ron. +- Load invalid face data should log warning not panic. + +**Docs to update:** +- docs/editor/brushes.md new page +- docs/editor/README.md index. + +### BS-JD-102 - Quick add and draw-brush tool + +**Milestone:** M1 +**Priority:** P0 +**Area:** Brushes + +**Summary:** Add brush creation via menu/context actions and a modal draw tool that places vertices on a face or floor, then extrudes to a convex brush. + +**Why this matters:** Jackdaw's B/C draw workflow is a fast path for blockout. Blacksite already supports asset placement by viewport rays; reuse that for brush drawing. + +**Implementation notes:** +- Add BrushToolState: Idle, DrawingPolygon, Extruding, Preview. +- LMB places vertices on plane selected by raycast: closest brush/static mesh face under cursor or y=0 floor. +- Enter closes polygon and creates extrusion preview; drag or default height sets depth. +- Esc/right-click cancels; Backspace removes last vertex; Tab toggles additive/subtractive mode if subtractive is implemented later. +- Create brush snapshot through EditorHistory/operator framework. +- Add toolbar button and B shortcut; C can create subtractive brush marker after CSG support. + +**Acceptance criteria:** +- B enters draw-brush mode and status bar shows next action. +- Click-click-click-Enter creates a visible brush from a floor polygon. +- Cancel leaves no entity and no dirty scene state. +- Undo removes the created brush. + +**Polish details:** +- Draw preview uses ghost lines and filled translucent polygon. +- Invalid polygon self-intersection is detected before commit. +- Plane snap and grid snap are visible and consistent with ViewportSettings. + +**Tests:** +- Polygon validation unit tests. +- Operator cancel/commit history tests. + +**Docs to update:** +- README shortcuts +- docs/editor/brushes.md. + +### BS-JD-103 - Brush element edit modes + +**Milestone:** M1 +**Priority:** P0 +**Area:** Brushes + +**Summary:** Add vertex, edge, face, and clip edit modes for selected brushes. + +**Why this matters:** Jackdaw exposes 1-4 brush edit modes. This turns brushes from primitives into real level geometry tools. + +**Implementation notes:** +- Add BrushEditMode resource: Object, Vertex, Edge, Face, Clip. +- Create selectable handles/proxies for vertices, edges, and faces; resolve picks to brush + element ID. +- Dragging a vertex/edge/face edits BrushDesc, validates convexity, rebuilds mesh preview, and commits one undo entry when drag ends. +- Clip mode stores a preview plane; Enter applies split into one or two BrushDesc entities. +- Disable normal object transform gizmo or scope it to selected elements while in brush mode. + +**Acceptance criteria:** +- 1/2/3/4 enter vertex/edge/face/clip modes when a brush is selected. +- Element highlighting and selection work with Shift+Click multi-select. +- Element edits preserve valid convex brush geometry or reject with visible reason. +- Esc returns to object mode. + +**Polish details:** +- Viewport badge: Brush: Face Mode. +- Selected elements use high-contrast colors distinct from entity selection. +- Inspector shows selected face material/UV controls in face mode. + +**Tests:** +- Convexity validation tests for vertex moves. +- Undo/redo tests for vertex and face drag. +- Selection proxy save filter tests. + +**Docs to update:** +- docs/editor/brushes.md edit mode section. + +### BS-JD-104 - Brush CSG operations + +**Milestone:** M1 +**Priority:** P1 +**Area:** Brushes + +**Summary:** Implement subtract, intersect, and convex merge for selected brushes. + +**Why this matters:** Jackdaw has CSG Subtract, CSG Intersect, and Join/Convex Merge. For Blacksite, this is the difference between block placement and useful greybox geometry. + +**Implementation notes:** +- Use a geometry crate boundary from BS-JD-101 so CSG can be tested independently. +- Inputs: selected BrushDesc entities ordered by selection time; first entity is minuend for subtract. +- Outputs: replace inputs with new BrushDesc snapshots, preserving names where possible and pushing one history command. +- Reject degenerate zero-volume results with a status warning and no scene mutation. +- Add menu entries under Edit > Brush and command palette IDs brush.subtract, brush.intersect, brush.merge_convex. + +**Acceptance criteria:** +- Subtract cuts one brush volume from another. +- Intersect keeps overlapping volume. +- Convex merge succeeds only when result is convex and rejects otherwise. +- Undo restores original brushes and selection. + +**Polish details:** +- Before commit, show preview of output faces. +- CSG failure messages are specific: no overlap, non-convex merge, degenerate face. +- Retain material assignment on surviving faces where face plane/material can be matched. + +**Tests:** +- Golden geometry cases: cube subtract cube, cube intersect cube, non-overlap, non-convex merge. +- Scene save after CSG authoring-only check. + +**Docs to update:** +- docs/editor/brushes.md boolean operations. + +### BS-JD-105 - Per-face material and UV controls + +**Milestone:** M1 +**Priority:** P1 +**Area:** Materials + +**Summary:** Allow brush faces to reference textures/materials and edit UV offset, scale, and rotation. + +**Why this matters:** Jackdaw stores material and UV data per brush face. Blacksite already has material assets and material overrides; brush faces need a parallel authoring path. + +**Implementation notes:** +- Extend BrushFaceDesc with material: Option, texture: Option, uv_offset: Vec2, uv_scale: Vec2, uv_rotation: f32. +- Face inspector appears when BrushEditMode::Face has one or more selected faces. +- Drag texture/material from asset browser onto face proxy to set face material via an operator. +- Hydration builds per-face mesh sections or material slots; keep static mesh renderer path separate from brush renderer path initially. + +**Acceptance criteria:** +- Selecting a face shows material and UV controls. +- Dragging a texture or material onto a face applies it with undo. +- Save/load preserves per-face material and UV data. +- Missing material displays fallback checker and warning. + +**Polish details:** +- Add Align, Fit, Rotate 90, Flip U, Flip V buttons. +- Show material swatch and asset path with Locate/Clear actions. +- Face material drop target highlights only the face under cursor. + +**Tests:** +- UV transform math tests. +- Material ref round-trip tests. +- Undo/redo material apply to multiple faces. + +**Docs to update:** +- docs/editor/materials.md +- docs/editor/brushes.md face materials. + +### BS-JD-106 - Brush validation, repair, and diagnostics + +**Milestone:** M1 +**Priority:** P2 +**Area:** Brushes + +**Summary:** Add robust error handling for degenerate brushes, inverted normals, zero-area faces, and non-manifold results. + +**Why this matters:** CSG and direct vertex editing create edge cases. Good diagnostics prevent the editor sandbox from feeling brittle. + +**Implementation notes:** +- Add BrushValidationReport with warnings/errors and suggested repair actions. +- Run validation after edits, after load, and before save. +- Repair actions: flip normals, remove zero-area faces, weld close vertices, recenter origin. +- Expose Window > Brush Diagnostics and per-actor warning icon. + +**Acceptance criteria:** +- Invalid brush cannot crash hydration or save. +- Diagnostics panel lists brush entity, issue, severity, and fix action. +- Repair actions are undoable. + +**Polish details:** +- Viewport warning icon at brush origin. +- Error messages use level-designer language, not geometry jargon. +- Add 'copy diagnostics' for bug reports. + +**Tests:** +- Invalid brush fixtures. +- Repair action snapshot tests. + +**Docs to update:** +- docs/editor/brushes.md common gotchas. + +### BS-JD-201 - Material Browser and shared material catalog + +**Milestone:** M2 +**Priority:** P0 +**Area:** Materials + +**Summary:** Create a dedicated Material Browser panel for named PBR materials, scene-local materials, and project-wide material assets. + +**Why this matters:** Jackdaw separates the Asset Browser from a Material Browser with named material definitions. Blacksite already scans assets/materials/*.ron and has shader-aware MaterialAsset, but a focused material workflow will make the sandbox much more useful. + +**Implementation notes:** +- Add crates/editor/src/ui/material_browser/ with grid/list/detail views. +- Read/write MaterialAsset RON under assets/materials/. +- Define material refs: scene-local (#Name) and project-wide (@Name) or use existing EditorAssetRef consistently. +- Add Create Material, Duplicate, Rename, Delete, Locate Usages. +- Preview material on sphere using existing thumbnail/offscreen infrastructure or new material preview target. + +**Acceptance criteria:** +- Window > Material Browser opens a panel with all project materials. +- User can create a material, assign base color/metallic/roughness/texture, save to RON, and drag it onto a selected actor or brush face. +- Material usage list shows actors/faces using a material. +- Missing material refs show warnings but scenes still load. + +**Polish details:** +- Live preview sphere updates as sliders move. +- Dirty indicator on unsaved material edits. +- Clear distinction between shared material asset edits and per-actor overrides. + +**Tests:** +- MaterialAsset read/write round-trip. +- Preview cache invalidation on material edit. +- Assign material with undo to actor and brush face. + +**Docs to update:** +- docs/editor/materials.md new page +- README Scene Workflow. + +### BS-JD-202 - PBR texture-set auto-detection + +**Milestone:** M2 +**Priority:** P1 +**Area:** Materials + +**Summary:** Detect texture sets from filenames and generate draft materials automatically. + +**Why this matters:** Jackdaw groups albedo/normal/roughness/metallic/AO/height textures by common suffixes. This makes material authoring fast when importing texture packs. + +**Implementation notes:** +- Add pbr_filename_regex-like matcher in assets/materials.rs or material_browser/import.rs. +- Recognize suffixes: albedo, basecolor, diffuse, normal, n, roughness, r, metallic, m, ao, orm, height, displacement. +- Group files by stem base and propose MaterialAsset drafts. +- ORM auto-detection: if texture name contains orm or occlusion_roughness_metallic, map channels R=AO, G=roughness, B=metallic. +- Add 'Create materials from folder' action in Asset Browser and Material Browser. + +**Acceptance criteria:** +- Dropping a PBR texture folder under assets/textures lets user generate one or more MaterialAsset files. +- Generated material has correct texture bindings and editable values. +- User can split/merge mistakenly grouped textures before saving. + +**Polish details:** +- Preview detected groups before commit. +- Show confidence labels and unresolved files. +- Never overwrite existing material without confirmation. + +**Tests:** +- Filename grouping fixtures. +- ORM channel mapping metadata tests. + +**Docs to update:** +- docs/editor/materials.md auto-detection section. + +### BS-JD-203 - Drag/drop material and texture application + +**Milestone:** M2 +**Priority:** P0 +**Area:** Assets + +**Summary:** Make texture/material drag/drop work consistently for static mesh actors, primitive actors, and brush faces. + +**Why this matters:** Jackdaw's asset browser can drag an image onto a brush face and route through undo. Blacksite has asset drag placement and texture assignment; unify this under operator semantics. + +**Implementation notes:** +- Detect drop target: selected actor, actor under viewport cursor, brush face proxy, static mesh slot. +- If image dropped onto material slot, create or update MaterialOverride base color texture. +- If material dropped, assign material ref to appropriate slot/face. +- If no valid target, show ghost invalid drop cursor and status message. +- All changes use EditorOperator and EditorHistory. + +**Acceptance criteria:** +- Drag texture from Asset Browser onto selected primitive changes its visible material. +- Drag material onto static mesh slot sets override or ref. +- Drag material onto brush face sets face material. +- Undo restores previous material state. + +**Polish details:** +- Target under cursor highlights before release. +- Drop popup if target has multiple slots: choose slot or apply to all. +- Status message includes material name and target actor. + +**Tests:** +- Drop target resolution tests. +- Undo tests for primitive, static mesh, brush face. + +**Docs to update:** +- README Asset Browser controls. + +### BS-JD-204 - Live asset watcher and refresh + +**Milestone:** M2 +**Priority:** P1 +**Area:** Assets + +**Summary:** Watch assets/ and refresh asset catalog, thumbnails, and material registry without manual refresh. + +**Why this matters:** Jackdaw templates rely on Bevy's file watcher so dropped files appear automatically. Blacksite scans assets and refreshes on settings changes or import; live watching improves iteration. + +**Implementation notes:** +- Add notify watcher to EditorAssetsPlugin or rely on Bevy AssetServer watcher if configured; notify is already optional for hot reload, but this should be editor feature-gated or always available. +- Debounce file events and refresh only affected folders where possible. +- Invalidate thumbnail cache for changed paths. +- Refresh material browser groups on texture/material changes. +- Status bar shows 'Assets refreshed: N changed'. + +**Acceptance criteria:** +- Copying a PNG into assets/textures appears in Asset Browser without restart. +- Editing a MaterialAsset RON refreshes Material Browser and assigned material preview. +- Deleting an asset marks stale references with warnings. + +**Polish details:** +- Avoid UI stalls on large folder changes with debounce and incremental refresh. +- Show spinner or small activity icon during refresh. +- Recover gracefully from partial files being copied. + +**Tests:** +- Unit tests for debouncer. +- Manual smoke script with temp asset dir. + +**Docs to update:** +- docs/editor/assets.md. + +### BS-JD-205 - Asset rename/move reference repair + +**Milestone:** M2 +**Priority:** P1 +**Area:** Assets + +**Summary:** Track asset moves/renames and repair scene/material/prefab references. + +**Why this matters:** General editors need robust asset references. Jackdaw has project-wide catalog concepts; Blacksite already has stable UUIDs in AssetRegistry, so this is a natural improvement. + +**Implementation notes:** +- Treat AssetId as source of truth and path as mutable metadata. +- On file watcher rename or Asset Browser rename, update AssetRecord.path and all EditorAssetRef paths where asset_id matches. +- Add a missing-reference resolver dialog for files changed outside the editor. +- Add Asset Browser actions: Rename, Move, Duplicate, Delete with reference impact preview. + +**Acceptance criteria:** +- Renaming a material or model inside editor updates scene refs and registry. +- Opening a scene with missing path but matching AssetId repairs path if registry knows it. +- Deleting an asset warns about usages before commit. + +**Polish details:** +- Reference impact dialog lists affected scenes/entities/material slots. +- Offer 'Keep broken reference' for deliberate missing assets. +- Add undo for in-editor rename/move where practical. + +**Tests:** +- Registry path update tests. +- Scene ref repair tests with missing asset path. + +**Docs to update:** +- docs/editor/assets.md reference model. + +### BS-JD-206 - Headless asset processing command + +**Milestone:** M2 +**Priority:** P2 +**Area:** Pipeline + +**Summary:** Add xtask/cargo-style command to process assets without opening the editor UI. + +**Why this matters:** Jackdaw calls out asset processing as an open challenge. Blacksite already has xtask and generated static mesh artifacts; processing outside the UI will help CI and future builds. + +**Implementation notes:** +- Add xtask process-assets with options: --check, --write, --changed-only, --project assets/project.ron. +- Move static mesh artifact refresh logic into a reusable library function not tied to UI resources. +- Process material auto-detection, static mesh manifests, thumbnail metadata, and registry consistency checks. +- CI can run xtask process-assets --check to fail stale generated artifacts. + +**Acceptance criteria:** +- Running cargo run -p xtask -- process-assets --check reports stale/missing generated mesh manifests. +- Running --write updates assets/.index/registry.ron and generated manifests. +- No Bevy window or egui initialization is required. + +**Polish details:** +- Output concise summaries and actionable errors. +- Use stable ordering for deterministic generated files. +- Add --json for future automation. + +**Tests:** +- Temp project fixture process-assets test. +- Golden generated manifest comparison. + +**Docs to update:** +- README Target/Asset tasks +- docs/editor/assets.md. + +### BS-JD-301 - Terrain authoring schema and chunked mesh hydration + +**Milestone:** M3 +**Priority:** P1 +**Area:** Terrain + +**Summary:** Add a heightmap-backed TerrainDesc authoring component with chunked generated meshes. + +**Why this matters:** Jackdaw terrain supports heightmap sculpting with chunks. Blacksite currently has strong static mesh/primitive workflows but no native terrain authoring. + +**Implementation notes:** +- Add shared::TerrainDesc: resolution, size, height_scale, heights storage reference, material/layer refs. +- Store height data either inline for small terrains or as assets/terrain/*.terrain.ron/bin for larger maps. +- Add crates/terrain or shared::terrain_math with chunk mesh generation and normal computation. +- Hydration spawns EditorOnly mesh chunk children and optional collider chunks. +- Chunk size starts at 32 cells, configurable later. + +**Acceptance criteria:** +- Add > Terrain creates flat terrain visible in viewport. +- Terrain saves/loads without generated chunk meshes in scene. +- Changing size/resolution rebuilds chunks and preserves existing heights where possible. +- Terrain can generate static collision. + +**Polish details:** +- Inspector warns before destructive resolution changes. +- Viewport shows chunk boundaries when selected. +- Initial terrain appears centered under camera/selection, not at an unexpected origin. + +**Tests:** +- Heightmap mesh generation tests. +- Chunk dirty-region tests. +- Save filter tests for generated chunks. + +**Docs to update:** +- docs/editor/terrain.md new page. + +### BS-JD-302 - Terrain sculpt tools with stroke undo + +**Milestone:** M3 +**Priority:** P1 +**Area:** Terrain + +**Summary:** Implement raise/lower, flatten, smooth, and noise sculpt brushes with one undo entry per stroke. + +**Why this matters:** Jackdaw's terrain tools emphasize brush radius, strength, preview ring, and stroke-based undo. This is the minimum useful terrain workflow. + +**Implementation notes:** +- Add TerrainToolState with active terrain, brush mode, radius, strength, falloff. +- Viewport ray picks terrain surface and shows brush ring projected onto terrain. +- During drag, apply brush to height samples, mark affected chunks dirty, and rebuild only overlapping chunks. +- Store pre-stroke height patch and post-stroke patch for undo/redo. +- Add toolbar controls and shortcuts. + +**Acceptance criteria:** +- Raise/lower, flatten, smooth, and noise tools work on selected terrain. +- Each drag stroke is one undo entry. +- Affected chunks update without rebuilding the whole terrain. +- Brush preview ring tracks cursor and radius. + +**Polish details:** +- Tablet-like controls are not required, but sliders should feel responsive. +- Status HUD shows tool, radius, strength, falloff. +- Show a warning for very high resolution terrains if stroke cost is high. + +**Tests:** +- Brush application math tests. +- Undo patch tests. +- Dirty chunk overlap tests. + +**Docs to update:** +- docs/editor/terrain.md sculpt section. + +### BS-JD-303 - Terrain material layers and texture painting MVP + +**Milestone:** M3 +**Priority:** P2 +**Area:** Terrain + +**Summary:** Add terrain material layers with simple weight painting. + +**Why this matters:** Jackdaw docs note texture painting as roadmap/not built. Blacksite can start with a pragmatic MVP after terrain sculpting lands. + +**Implementation notes:** +- TerrainDesc stores layers: material ref + weightmap channel/index. +- Weight data can be stored as small RON for MVP, binary later. +- Painting tool adjusts selected layer weight with radius/strength and normalizes weights. +- Shader path: simple splat map material or generate mesh vertex colors as interim visualization. +- Provide fallback single material for runtime if layer shader is not ready. + +**Acceptance criteria:** +- User can add at least four terrain material layers. +- Paint tool visibly blends layer preview in editor. +- Save/load preserves layer weights. +- Runtime fallback is documented. + +**Polish details:** +- Layer list supports reorder, rename, clear layer, fill layer. +- Brush preview uses selected material swatch. +- Warn if renderer fallback cannot show all layers. + +**Tests:** +- Weight normalization tests. +- Layer serialization tests. + +**Docs to update:** +- docs/editor/terrain.md material layers. + +### BS-JD-304 - Physics placement tool + +**Milestone:** M3 +**Priority:** P1 +**Area:** Physics + +**Summary:** Let users select props, release them into simulated gravity, then commit or cancel settled transforms. + +**Why this matters:** Jackdaw's Physics Tool is a practical level-design feature: drop dynamic props into place instead of hand-positioning them. + +**Implementation notes:** +- Add PhysicsPlacementState: Idle, Holding, Simulating, Settled. +- Shortcut Shift+P enters tool if selected entities have RigidBodyDesc/ColliderDesc. +- Temporarily enable sim for selected entities only; freeze or disable non-selected dynamic bodies while retaining static colliders. +- On Space commit, capture final transforms and write SetTransformGroup history command. +- On Esc cancel, restore pre-tool transforms and simulation state. +- Use Avian components through hydration; do not persist runtime physics state. + +**Acceptance criteria:** +- Selected dynamic props can be dropped onto static colliders and settle. +- Space commits final transforms; Esc restores original transforms. +- Undo after commit restores original transforms. +- Non-selected dynamic props are not disturbed. + +**Polish details:** +- Status bar: Physics Tool | drag selected to release | Space commit | Esc cancel. +- Selected bodies show orange collider overlay; static obstacles green. +- If selection lacks colliders, offer Add Collider action. + +**Tests:** +- State machine unit tests. +- Transform commit/cancel history tests. +- Manual scene fixture for physics placement. + +**Docs to update:** +- docs/editor/physics.md new page +- README controls. + +### BS-JD-305 - Collider authoring diagnostics and overlays + +**Milestone:** M3 +**Priority:** P1 +**Area:** Physics + +**Summary:** Improve collider visualization, shape switching, and error reporting. + +**Why this matters:** Blacksite already has collider descriptors and visualizers. Jackdaw's physics docs emphasize green/orange/cyan overlays, collider shape selection, and common gotchas. + +**Implementation notes:** +- Extend visualizers.rs to color colliders by body/sensor/selection state. +- Inspector card for ColliderDesc shows shape dropdown, dimensions, mesh source, cooking options, and warnings. +- Add validation: dynamic trimesh warning, missing mesh source, invalid scale, zero extents. +- Add 'Fit Collider To Mesh Bounds' and 'Copy Collider From Renderer' actions. + +**Acceptance criteria:** +- Collider overlays communicate selected/static/dynamic/sensor states. +- Invalid collider choices display warnings before hydration fails. +- Fit-to-bounds creates undoable collider changes. + +**Polish details:** +- Overlay opacity controlled from viewport options. +- Warnings use actionable language. +- Collider card shows estimated complexity for mesh collider. + +**Tests:** +- Collider validation tests. +- Fit-to-bounds math tests. + +**Docs to update:** +- docs/editor/physics.md collider section. + +### BS-JD-401 - Static extension API v0 + +**Milestone:** M4 +**Priority:** P1 +**Area:** Extensibility + +**Summary:** Formalize a stable, statically linked extension API before attempting dynamic loading. + +**Why this matters:** Jackdaw has a larger extension/dylib ecosystem, but its docs also call out dylib challenges. Blacksite already has EditorPlugin, ActorInspectorSection, and command registry. Stabilize that first. + +**Implementation notes:** +- Create editor::api module that re-exports only stable extension traits: EditorPlugin, EditorCommand, ActorInspectorSection, EditorComponentMeta registration, panel registration. +- Add PanelRegistration: id, title, icon, default_dock, ui callback. +- Add menu registration and command registration under one extension context. +- Move internal resources out of the public API or mark as unstable. +- Dogfood with game::editor_ext and a sample plugin crate inside examples/ or crates/editor_example_ext. + +**Acceptance criteria:** +- A separate in-workspace crate can add a panel, command, component metadata, and inspector section without editing ui/mod.rs. +- API docs clearly mark stable vs internal. +- Existing game demo panel uses the API. + +**Polish details:** +- Extensions should not need to know dock internals for simple panels. +- Extension errors should surface in Diagnostics panel. +- API names should be generic for future games, not FPS-specific. + +**Tests:** +- Compile-check sample extension crate. +- Registry duplicate ID tests. + +**Docs to update:** +- docs/editor/extensibility.md new page. + +### BS-JD-402 - Extension and game prototype scaffolds + +**Milestone:** M4 +**Priority:** P2 +**Area:** Extensibility + +**Summary:** Add editor actions to create new game prototype or editor extension crates from templates. + +**Why this matters:** Jackdaw can scaffold new games/extensions from its launcher. Since Blacksite is a sandbox for a future Bevy game, scaffolding will help transition from editor sandbox to real project. + +**Implementation notes:** +- Add templates/blacksite_game and templates/blacksite_editor_extension. +- Launcher buttons: New Game Prototype, New Editor Extension. +- Template creates Cargo.toml, src/lib.rs, src/bin/editor.rs or extension lib, assets/project.ron. +- For now use static linking path; generated editor binary adds editor::EditorPluginGroup and user's game plugin. +- Optional: xtask new-game and xtask new-extension for non-UI flow. + +**Acceptance criteria:** +- User can create a new prototype project from launcher and open it. +- Generated project builds with cargo check. +- Generated extension crate registers a sample panel. + +**Polish details:** +- Show template version and upgrade note. +- Validate crate name and destination before creating files. +- After scaffold, offer 'Open in editor' and 'Open folder'. + +**Tests:** +- Template generation tests in temp directory. +- cargo check generated minimal project in CI if feasible. + +**Docs to update:** +- README Getting started +- docs/editor/extensibility.md. + +### BS-JD-403 - Optional dylib extension exploration + +**Milestone:** M4 +**Priority:** P3 +**Area:** Extensibility + +**Summary:** Explore dynamic extension loading only after static extension API is stable. + +**Why this matters:** Jackdaw supports dylib extensions but documents Windows limitations. Blacksite should not make dynamic loading a foundation until the simpler static model works. + +**Implementation notes:** +- Write ADR comparing static plugins, hot game dylib, BRP/out-of-process tools, and dylib extension loading. +- Prototype Linux/macOS-only dylib loading with one tiny extension that registers a command. +- Reuse hot_reload concepts only if Bevy type identity is safe. +- Explicitly defer Windows support unless a clean ABI/type-sharing plan exists. +- Do not expose to normal users until unload/reload failure modes are understood. + +**Acceptance criteria:** +- ADR states whether to pursue, defer, or reject dynamic extension loading. +- Prototype can be enabled behind experimental feature and is off by default. +- Failure to load extension does not corrupt project state. + +**Polish details:** +- Experimental UI clearly labels risk. +- Loaded extension list shows path, status, error message, and reload button. + +**Tests:** +- Feature-gated smoke test on supported OS only. +- Error-path tests for missing symbols if practical. + +**Docs to update:** +- ADR +- docs/editor/extensibility.md experimental section. + +### BS-JD-404 - BRP remote inspector write prototype + +**Milestone:** M4 +**Priority:** P2 +**Area:** Remote/PIE + +**Summary:** Prototype editing one Transform over BRP against a running game/editor world. + +**Why this matters:** Jackdaw identifies remote inspector round-trip editing as an open PIE maturity gap. Blacksite already enables BRP in the editor; a narrow write path will clarify whether future game preview should be in-process or out-of-process. + +**Implementation notes:** +- Add editor command brp.transform.set for selected entity or ActorId. +- Wire a small BRP client panel to connect to localhost editor/game endpoint. +- Start with Transform only, using ActorId mapping rather than raw Entity when possible. +- Round-trip: fetch entity list, display selected transform, edit value, send patch, verify update. + +**Acceptance criteria:** +- Remote inspector can read and write Transform for a test entity. +- Errors are shown for stale entity IDs. +- The prototype is clearly marked experimental and does not replace in-process inspector. + +**Polish details:** +- Show connection status, endpoint, latency/polling interval. +- Add 'copy BRP request' for debugging. +- Use value diff highlighting after write. + +**Tests:** +- Mock command serialization tests. +- Manual two-process smoke instructions. + +**Docs to update:** +- docs/editor/brp.md. + +### BS-JD-405 - Out-of-process game preview spike + +**Milestone:** M4 +**Priority:** P3 +**Area:** PIE + +**Summary:** Investigate spawning the game as a separate process and syncing selected state back to the editor. + +**Why this matters:** Blacksite currently has in-process PIE. Jackdaw's open challenges note the tradeoff: in-process is cheap but a game panic can take down the editor; out-of-process is safer but needs protocol work. + +**Implementation notes:** +- Create a spike branch only. Do not merge half-finished protocol into main editor flow. +- Spawn game binary with --editor-remote and project path. +- Use BRP or a small protocol to request entity list, selected transform, play/stop state. +- Document latency, crash isolation, asset reload behavior, and complexity. +- Decide whether Blacksite needs this before a real game exists. + +**Acceptance criteria:** +- Spike report with recommendation: keep in-process, adopt out-of-process later, or hybrid. +- If prototype exists, it can launch a game process and read a scene entity list. + +**Polish details:** +- User-facing UI is not required for spike beyond diagnostics. +- Capture logs from child process in a panel if implemented. + +**Tests:** +- N/A for spike; document manual reproduction. + +**Docs to update:** +- ADR if decision affects architecture. + +### BS-JD-501 - Editor sample scenes and regression pack + +**Milestone:** M5 +**Priority:** P0 +**Area:** QA + +**Summary:** Create curated sample scenes for brushes, materials, terrain, physics placement, and rendering. + +**Why this matters:** Feature work becomes hard to evaluate without stable scenes. Blacksite already has showcase levels; expand this into a deliberate regression/demo pack. + +**Implementation notes:** +- Create assets/levels/samples/brush_blockout.scn.ron, material_lab.scn.ron, terrain_sculpt.scn.ron, physics_drop.scn.ron. +- Add small source assets under assets/samples/ with license-safe primitives/textures. +- Add xtask validate-samples that loads scenes, validates authoring-only save, and checks missing asset refs. +- Add README section listing what each sample tests. + +**Acceptance criteria:** +- Each major editor feature has a sample scene. +- CI validates sample scenes are authoring-only and references resolve. +- New contributors can open samples from File > Open Sample. + +**Polish details:** +- Samples should be visually clear, not just technical fixtures. +- Add explanatory text objects or notes if supported; otherwise include docs screenshots later. + +**Tests:** +- CI validate-levels includes samples. +- Asset ref resolver test over samples. + +**Docs to update:** +- README sample scenes +- docs/editor/README.md. + +### BS-JD-502 - Tool regression tests and undo invariants + +**Milestone:** M5 +**Priority:** P0 +**Area:** QA + +**Summary:** Add reusable tests for every modal tool and every editor operator. + +**Why this matters:** Brushes, terrain, materials, and physics tools are stateful. Without undo/redo/cancel invariants, editor regressions will feel random. + +**Implementation notes:** +- Create editor test helpers for spawning a minimal App with SharedTypesPlugin, EditorHistoryPlugin, and tool resources. +- Define invariant helpers: cancel_no_change, commit_marks_dirty, undo_restores_snapshot, redo_restores_commit, save_authoring_only. +- Apply to new operators as they land. + +**Acceptance criteria:** +- Every new ticket that mutates scene data adds at least one operator invariant test. +- Tests run in cargo test -p editor or relevant crate without opening a window. +- A failing test includes operator ID and scenario. + +**Polish details:** +- Keep tests fast; geometry-heavy tests use small fixtures. +- Add comments explaining why each invariant matters for editor UX. + +**Tests:** +- This ticket creates the test harness. + +**Docs to update:** +- docs/editor/debt-audit.md. + +### BS-JD-503 - Performance budgets and editor profiler panel + +**Milestone:** M5 +**Priority:** P2 +**Area:** Diagnostics + +**Summary:** Add performance budgets for expensive editor systems and a panel that surfaces hot operations. + +**Why this matters:** Brush mesh rebuilds, terrain chunk rebuilds, thumbnails, and asset scans can stall the editor. Blacksite already has diagnostics; extend it with editor-specific budgets. + +**Implementation notes:** +- Instrument asset refresh, thumbnail generation, brush rebuild, terrain sculpt, scene save/load, and render target resize. +- Add EditorPerfStats resource with rolling timings and counts. +- Diagnostics panel shows slowest recent editor operations. +- Add optional log warnings when operation exceeds budget. + +**Acceptance criteria:** +- Window > Diagnostics shows editor operation timings. +- Terrain/brush rebuilds report dirty chunks/faces count. +- Asset refresh reports changed files and elapsed time. + +**Polish details:** +- Use simple green/yellow/red indicators. +- Do not spam logs during continuous sculpt/drag; aggregate per stroke. +- Add 'copy perf snapshot' for bug reports. + +**Tests:** +- Stats aggregation unit tests. +- No strict timing tests in CI. + +**Docs to update:** +- docs/editor/architecture.md diagnostics. + +### BS-JD-504 - Documentation and issue board structure + +**Milestone:** M5 +**Priority:** P0 +**Area:** Docs + +**Summary:** Create docs pages and issue labels/milestones that match this roadmap. + +**Why this matters:** Blacksite's AGENTS.md treats stale docs as bugs. This roadmap will only help if tickets, labels, and docs stay synchronized. + +**Implementation notes:** +- Add docs/editor/jackdaw-inspired-roadmap.md based on this document. +- Add docs/editor/brushes.md, materials.md, terrain.md, physics.md as features land. +- Create labels: area/brushes, area/materials, area/terrain, area/physics, area/extensibility, priority/P0-P3, milestone/M0-M5, polish, tests. +- Create issue template with fields from this document. + +**Acceptance criteria:** +- Every roadmap ticket has a matching issue or is explicitly deferred. +- Docs index links all new pages. +- Root README controls updated when shortcuts ship. + +**Polish details:** +- Keep issue titles user-facing and outcome-focused. +- Add screenshots/gifs once features are visual. +- Add 'Known limitations' sections rather than hiding incomplete edges. + +**Tests:** +- Docs link check if available; otherwise manual checklist. + +**Docs to update:** +- This ticket is docs-focused. + +### BS-JD-505 - First-hour usability polish pass + +**Milestone:** M5 +**Priority:** P1 +**Area:** UX + +**Summary:** Make the first hour in the editor feel guided: starter scene, tooltips, shortcut help, empty-state actions, and recovery messages. + +**Why this matters:** A sandbox editor needs confidence-building. Jackdaw's docs and launcher-oriented flow make it clear how to start; Blacksite should do the same before the future game exists. + +**Implementation notes:** +- Add first-run starter scene or guided empty-state overlay. +- Improve F1 shortcuts window with context-sensitive sections. +- Add empty-state buttons in Hierarchy, Asset Browser, Material Browser, and Viewport. +- Add consistent toast/status messages for successful saves, imports, tool commits, and failed operations. +- Add 'Reset editor layout', 'Open sample scene', and 'Create first brush' affordances. + +**Acceptance criteria:** +- Fresh clone -> cargo run -p editor leads to visible useful scene or clear next action. +- F1 explains active tool shortcuts. +- Empty panels suggest actions instead of looking broken. +- Failed operations explain how to recover. + +**Polish details:** +- Use concise language and avoid modal dialogs for routine hints. +- Hints disappear once user performs the action but can be restored. +- All messages include object names when possible. + +**Tests:** +- Manual first-run checklist. +- Preference reset test if first-run state is persisted. + +**Docs to update:** +- README Getting started. + +## Suggested issue labels + +`priority/P0`, `priority/P1`, `priority/P2`, `priority/P3`, `milestone/M0`, `milestone/M1`, `milestone/M2`, `milestone/M3`, `milestone/M4`, `milestone/M5`, `area/project`, `area/scene-io`, `area/inspector`, `area/viewport`, `area/brushes`, `area/materials`, `area/assets`, `area/terrain`, `area/physics`, `area/extensibility`, `area/remote`, `area/qa`, `polish`, `tests`, `docs`, `adr-needed`, `experimental` + +## Issue template + +```markdown +## Goal + +## Current Blacksite context + +## Jackdaw-inspired reference + +## Implementation notes +- Files/modules: +- Data model changes: +- UI/UX behavior: +- Save/load/hydration impact: + +## Acceptance criteria +- [ ] + +## Polish checklist +- [ ] Preview/ghost/overlay +- [ ] Commit/cancel +- [ ] Undo/redo +- [ ] Status/error messages +- [ ] Docs updated +- [ ] Tests or sample scene + +## Risks + +## Out of scope +``` + +## Source inventory + +Blacksite source paths are cited as `Falling-Metal-Interactive/Blacksite@main:` because they were inspected through the Gitea repository tools. Jackdaw sources are public URLs inspected through web browsing. + +- Blacksite: Falling-Metal-Interactive/Blacksite@main: Cargo.toml +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/Cargo.toml +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/lib.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/bin/main.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/ui/mod.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/viewport/* +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/scene/scene_io.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/assets/* +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/play/* +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/editor/src/ext/* +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/shared/src/lib.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: crates/scene/src/lib.rs +- Blacksite: Falling-Metal-Interactive/Blacksite@main: docs/editor/README.md +- Blacksite: Falling-Metal-Interactive/Blacksite@main: docs/editor/architecture.md +- Blacksite: Falling-Metal-Interactive/Blacksite@main: docs/editor/roadmap.md +- Jackdaw: https://github.com/jbuehler23/jackdaw +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/README.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/developer-guide/architecture.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/developer-guide/crate-structure.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/developer-guide/extending-the-editor.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/developer-guide/open-challenges.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/brushes.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/materials-textures.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/terrain.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/physics.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/scene-management.md +- Jackdaw: https://raw.githubusercontent.com/jbuehler23/jackdaw/main/book/src/user-guide/keyboard-shortcuts.md diff --git a/.gitignore b/.gitignore index 5af6dee..4cbe9e4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # Editor/runtime generated session state /assets/levels/.pie_session.scn.ron +/assets/.trash/ # Backup, temporary, and log files **/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock index caefc33..30c4b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1478,8 +1478,6 @@ dependencies = [ [[package]] name = "bevy_render" version = "0.18.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243523e33fe5dfcebc4240b1eb2fc16e855c5d4c0ea6a8393910740956770f44" dependencies = [ "async-channel", "bevy_app", diff --git a/Cargo.toml b/Cargo.toml index 5efe988..6f0f892 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,8 @@ settings = { path = "crates/settings" } scene = { path = "crates/scene" } [patch.crates-io] +# Linux surface acquire timeouts can be transient on Wayland/Xwayland drivers. +bevy_render = { path = "third_party/bevy_render" } # Atmosphere-aware mesh view bind groups (WYSIWYG editor + transform gizmos). transform-gizmo-bevy = { path = "third_party/transform-gizmo-bevy" } diff --git a/README.md b/README.md index f5a8c7f..36f78d7 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ VS Code tasks: **target cleanup (dry run)** and **target cleanup (safe apply)**. - The native game/editor windows force an opaque Wayland surface and opaque camera clears to avoid compositor alpha issues on mixed HDR/SDR desktops. - If the window maps but appears transparent on Hyprland or another Wayland compositor, launch with `BEVY_FPS_HDR=0` to force the SDR camera path while debugging monitor/compositor behavior. +- The workspace patches `bevy_render` locally so transient Linux swapchain acquire timeouts skip one frame instead of panicking in `prepare_windows`. - Debug launch configs and run tasks set `WGPU_VALIDATION=0` to quiet the known wgpu/Vulkan validation-layer warning `VUID-StandaloneSpirv-MemorySemantics-10871`. This only disables the Vulkan validation layer for those launches; Rust panics and application errors still surface normally. - If **CodeLLDB / mold** fails with hundreds of `undefined symbol` linker errors, the incremental `target/` cache is stale. Run the VS Code task **clean build editor (dev)** or `cargo clean -p editor && cargo build -p editor --features dev`, then launch **Debug editor** again. Use `cargo run -p editor --features dev` from the terminal if you need `libbevy_dylib` on `LD_LIBRARY_PATH` automatically. @@ -110,12 +111,12 @@ VS Code tasks: **target cleanup (dry run)** and **target cleanup (safe apply)**. | `W` / `E` / `R` | Translate / rotate / scale gizmo | | `X` | Toggle world/local gizmo orientation | | Viewport toolbar (sun / brush / box icons) | Shading: Lit, Unlit (albedo), Colliders (mesh off) | -| Viewport eye/options | Toggle actor root icon categories, colliders, lights, spawns, prefab/model anchors, and runtime player/camera visualizers | +| Viewport eye/options | Toggle actor root icon categories, adjust icon/gizmo size, and control colliders, lights, spawns, prefab/model anchors, and runtime player/camera visualizers | | `Tab` in viewport | Cycle selection through overlapping objects at last click | | Click Player visualizer in Edit mode | Select or create the authored `PlayerSpawn` (`Player Start`) | | Select Project Sun | Inspect project default lighting; create a scene sun override | -| Asset Browser project/file views | Browse `assets/`, search/filter/sort, switch grid/list, inspect import settings; drag assets into the viewport to place | -| Asset Browser texture action | Assign base-color texture to selection | +| Asset Browser project/file views | Browse `assets/`, search/filter/sort, switch grid/list, expand model subassets with generated thumbnails, inspect staged import/material settings, drag assets/submeshes into the viewport | +| Asset Browser context/details actions | Apply textures/materials, regenerate thumbnails, reimport models, place assets/submeshes, or move file assets to `assets/.trash/` | | `Ctrl+P` | Command palette (`scene.reset_lighting`, `selection.group`, `selection.focus`, play commands) | | `F7` | While paused in Play: advance one sim tick | | Shift/Ctrl + click (Hierarchy) | Additive selection | @@ -141,6 +142,7 @@ VS Code tasks: **target cleanup (dry run)** and **target cleanup (safe apply)**. respected. - Add a `PlayerSpawn` marker component (via Inspector) on a level object to choose where Play starts; otherwise the default game spawn `(0, 1.5, 10)` is used. +- Actor root icons draw over scene meshes, stay screen-sized while zooming, and are prioritized when clicked. - Clean game-view overlay (`G`) hides editor widgets, actor root icons, visualizers, selectable proxies, gizmos, selection outlines, and grid without changing the active camera. - **F5** stops Play and restores **player sim state** only; authored scene edits made during PIE @@ -174,7 +176,9 @@ VS Code tasks: **target cleanup (dry run)** and **target cleanup (safe apply)**. artifacts in `assets/meshes/generated/`. Renderer slots reference imported content-browser mesh and material assets, while generated collision is stored separately in `ColliderDesc` plus `RigidBodyDesc`. Switch a model asset's placement mode to **Scene Instance** in Asset Browser - details when you need full `SceneRoot` loading for animation/skinning/scene data. + details when you need full `SceneRoot` loading for animation/skinning/scene data. Expanding a + model in the Asset Browser exposes normalized mesh/material/texture subassets; dragging a mesh + subasset places that part through the same static mesh renderer path. - Runtime-only handles/colliders are not serialized directly, keeping scenes stable and portable. - Editor-only cameras and helper roots are filtered from selection, hierarchy, and scene save. - **PIE restores player sim only** (transform, velocity, jump state) when you stop Play; authored @@ -252,7 +256,7 @@ crates/ - [x] Mesh picking selection + highlights - [x] Transform gizmos - [x] Editor-side scene visualizers for colliders, lights, player spawns, prefab/model anchors, and runtime player/cameras -- [x] Selectable actor root icons in the 3D viewport with per-category visibility controls +- [x] Selectable, screen-sized actor root icons in the 3D viewport with per-category visibility controls - [x] Filtered hierarchy, inspector, viewport, toolbar, asset browser, and status panels - [x] Delete, duplicate, rename, and structural/material undo-redo - [x] Native Bevy scene New/Open/Save/Save As with dirty title tracking @@ -268,6 +272,7 @@ crates/ - [x] Determinism harness: same inputs over same ticks produce the same state summary/hash - [x] Verify: `cargo fmt --check` / `cargo check --workspace` / `cargo clippy --workspace` / strict foundation clippy / `cargo test -p sim` - [x] Stable asset registry with UUIDs + import settings in asset browser details +- [x] Asset Browser expandable model subasset shelves, independent mesh/material/texture thumbnails, staged import/material details with shader-schema parameters, context actions, and trash-first file removal - [x] Prefab instances (`PrefabInstance`) + save-as-prefab + unpack - [x] Unsaved-scene confirm on New/Open/Recent - [x] Hierarchy multi-select, reparent undo, multi-entity gizmo transform @@ -294,14 +299,17 @@ crates/ - Crouch lowers the camera and movement speed; the collider stays full-height for stability. - The editor asset browser is filesystem-backed with folder/tree navigation, grid/list views, texture and model thumbnails (glTF albedo fast-path; offscreen render studio for FBX and - untextured models), search/filter/sort controls, and a details pane. **File → Import Assets** + untextured models), material sphere thumbnails, search/filter/sort controls, expandable model + subasset shelves, and a staged details pane. **File → Import Assets** accepts glTF/GLB and **FBX** (binary; copies sibling `.fbm` texture folders when present). Model assets generate normalized static mesh manifests under `assets/meshes/generated/`; drag/drop uses `StaticMeshRenderer` by default with imported asset refs and optional separate static mesh - collider components. Asset details can switch placement to **Scene Instance** for `ModelRef`/`SceneRoot` - playback. Skeletal animation playback is not supported by the static mesh path yet. + collider components. Expanded mesh subassets generate independent thumbnails and can be placed independently. Asset details can switch + placement to **Scene Instance** for `ModelRef`/`SceneRoot` playback, shared material assets can be + edited from the browser, and delete actions move files to `assets/.trash/`. Skeletal animation + playback is not supported by the static mesh path yet. - Prefab instances use stable asset IDs (`PrefabInstance` + registry) with serialized `PrefabOverrides` (transform, material, per-child visibility by name). Inspector **Apply/Revert** per field group; **Unpack** removes the instance link. -- Material assets are RON files under `assets/materials/`; shader schemas live under `assets/shaders/`. `MaterialDesc` stores shader kind, typed parameters, texture bindings, and StandardMaterial fields. Actor inspector edits create per-actor `MaterialOverride` data for static mesh slots instead of mutating shared material assets. +- Material assets are RON files under `assets/materials/`; shader schemas live under `assets/shaders/`. `MaterialDesc` stores shader kind, typed parameters, texture bindings, and StandardMaterial fields. The Asset Browser material details editor can apply shader schemas, edit typed parameters/textures, and regenerate sphere thumbnails. Actor inspector edits create per-actor `MaterialOverride` data for static mesh slots instead of mutating shared material assets. - Per-field reflect undo for all components remains future work; typed `shared` inspectors cover the common authoring path. - The authoring/hydration layer is intentionally small so richer asset workflows (terrain, material graphs, lighting profiles) can be added without changing the scene format foundation. diff --git a/assets/.index/registry.ron b/assets/.index/registry.ron index 51827bb..f62413c 100644 --- a/assets/.index/registry.ron +++ b/assets/.index/registry.ron @@ -32,34 +32,87 @@ dependencies: [], ), ( - id: ("b8b43f26-0eee-4ee8-9e16-53aedbc69205"), - path: "assets/models/Room3.fbx", - label: "Room3", - kind_tag: "Model", - import_settings: ( - scale: 1.7, - generate_collider: true, - lod0_only: true, - placement_mode: StaticAsset, - hierarchy_mode: SingleActor, - material_policy: AuthoringOverride, - static_mesh_manifest_path: Some("assets/meshes/generated/b8b43f26-0eee-4ee8-9e16-53aedbc69205.static_mesh.ron"), - ), - dependencies: [], - ), - ( - id: ("582cd326-c32d-49da-9424-ae38b3d60648"), - path: "assets/models/Dumpster.fbx", - label: "Dumpster", + id: ("b98ef565-3500-49e7-9935-f685fa9b2594"), + path: "assets/models/painted_wooden_chair_02_2k.fbx", + label: "painted_wooden_chair_02_2k", kind_tag: "Model", import_settings: ( scale: 1.0, generate_collider: true, lod0_only: true, placement_mode: StaticAsset, - hierarchy_mode: SourceHierarchy, - material_policy: AuthoringOverride, - static_mesh_manifest_path: Some("assets/meshes/generated/582cd326-c32d-49da-9424-ae38b3d60648.static_mesh.ron"), + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + static_mesh_manifest_path: Some("assets/meshes/generated/b98ef565-3500-49e7-9935-f685fa9b2594.static_mesh.ron"), + ), + dependencies: [], + ), + ( + id: ("71071fc3-7a7f-454d-b9ab-93ebf0feccbe"), + path: "assets/models/metal_stool_01_2k.gltf", + label: "metal_stool_01_2k", + kind_tag: "Model", + import_settings: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: SceneInstance, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + static_mesh_manifest_path: Some("assets/meshes/generated/71071fc3-7a7f-454d-b9ab-93ebf0feccbe.static_mesh.ron"), + ), + dependencies: [ + "assets/models/metal_stool_01.bin", + "assets/models/textures/metal_stool_01_arm_2k.jpg", + "assets/models/textures/metal_stool_01_diff_2k.jpg", + "assets/models/textures/metal_stool_01_nor_gl_2k.jpg", + ], + ), + ( + id: ("c4abe41c-ac88-4aec-8168-27e8a7c90583"), + path: "assets/textures/metal_stool_01_nor_gl_2k.jpg", + label: "metal_stool_01_nor_gl_2k", + kind_tag: "Texture", + import_settings: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: StaticAsset, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + static_mesh_manifest_path: None, + ), + dependencies: [], + ), + ( + id: ("0496e62e-1837-4940-a9c9-b2b4ffe9ed90"), + path: "assets/textures/metal_stool_01_arm_2k.jpg", + label: "metal_stool_01_arm_2k", + kind_tag: "Texture", + import_settings: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: StaticAsset, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + static_mesh_manifest_path: None, + ), + dependencies: [], + ), + ( + id: ("bddaa4d4-5097-42bf-8d51-dd9a3580442a"), + path: "assets/textures/metal_stool_01_diff_2k.jpg", + label: "metal_stool_01_diff_2k", + kind_tag: "Texture", + import_settings: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: StaticAsset, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + static_mesh_manifest_path: None, ), dependencies: [], ), diff --git a/assets/meshes/generated/582cd326-c32d-49da-9424-ae38b3d60648.static_mesh.ron b/assets/meshes/generated/582cd326-c32d-49da-9424-ae38b3d60648.static_mesh.ron deleted file mode 100644 index e9fc995..0000000 --- a/assets/meshes/generated/582cd326-c32d-49da-9424-ae38b3d60648.static_mesh.ron +++ /dev/null @@ -1,82 +0,0 @@ -( - schema_version: 1, - asset_id: "582cd326-c32d-49da-9424-ae38b3d60648", - label: "Dumpster", - source: ( - path: "assets/models/Dumpster.fbx", - format: "fbx", - fingerprint: ( - byte_len: 50940, - modified_unix_secs: 1780338936, - ), - dependencies: [], - ), - import: ( - scale: 1.0, - generate_collider: true, - lod0_only: true, - placement_mode: StaticAsset, - hierarchy_mode: SourceHierarchy, - material_policy: AuthoringOverride, - ), - metadata: ( - mesh_count: 3, - material_count: 2, - node_count: 4, - animation_count: 0, - skin_count: 0, - light_count: 0, - camera_count: 0, - ), - parts: [ - ( - id: "mesh:mesh1000", - name: "Dumpster / Material 0", - mesh_label: "Mesh1000", - material_id: Some("material:material0"), - material_slot_name: "Dumpster", - material_label: Some("Material0"), - local_transform: ( - translation: (8.342552, 0.06418517, -6.7325354), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node1"), - source_mesh: Some("Mesh1"), - source_material: Some("Dumpster"), - ), - ( - id: "mesh:mesh2000", - name: "Dumpster Lid / Material 0", - mesh_label: "Mesh2000", - material_id: Some("material:material1"), - material_slot_name: "Dumpster Lid", - material_label: Some("Material1"), - local_transform: ( - translation: (7.362827, 2.5433145, -7.784438), - rotation: (0.70710677, 0.000000000082318055, -0.00000000008231806, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node2"), - source_mesh: Some("Mesh2"), - source_material: Some("Dumpster Lid"), - ), - ( - id: "mesh:mesh3000", - name: "Dumpster Lid_02 / Material 0", - mesh_label: "Mesh3000", - material_id: Some("material:material1"), - material_slot_name: "Dumpster Lid", - material_label: Some("Material1"), - local_transform: ( - translation: (9.351767, 2.5433145, -7.784438), - rotation: (0.70710677, 0.000000000082318055, -0.00000000008231806, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node3"), - source_mesh: Some("Mesh3"), - source_material: Some("Dumpster Lid"), - ), - ], - warnings: [], -) \ No newline at end of file diff --git a/assets/meshes/generated/71071fc3-7a7f-454d-b9ab-93ebf0feccbe.static_mesh.ron b/assets/meshes/generated/71071fc3-7a7f-454d-b9ab-93ebf0feccbe.static_mesh.ron new file mode 100644 index 0000000..ca1db68 --- /dev/null +++ b/assets/meshes/generated/71071fc3-7a7f-454d-b9ab-93ebf0feccbe.static_mesh.ron @@ -0,0 +1,55 @@ +( + schema_version: 1, + asset_id: "71071fc3-7a7f-454d-b9ab-93ebf0feccbe", + label: "metal_stool_01_2k", + source: ( + path: "assets/models/metal_stool_01_2k.gltf", + format: "gltf", + fingerprint: ( + byte_len: 2790, + modified_unix_secs: 1780712587, + ), + dependencies: [ + "assets/models/metal_stool_01.bin", + "assets/models/textures/metal_stool_01_arm_2k.jpg", + "assets/models/textures/metal_stool_01_diff_2k.jpg", + "assets/models/textures/metal_stool_01_nor_gl_2k.jpg", + ], + ), + import: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: SceneInstance, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + ), + metadata: ( + mesh_count: 1, + material_count: 1, + node_count: 1, + animation_count: 0, + skin_count: 0, + light_count: 0, + camera_count: 0, + ), + parts: [ + ( + id: "mesh:mesh0_primitive0", + name: "metal_stool_01 / Primitive 0", + mesh_label: "Mesh0/Primitive0", + material_id: Some("material:material0"), + material_slot_name: "metal_stool_01", + material_label: Some("Material0"), + local_transform: ( + translation: (0.0, 0.0, 0.0), + rotation: (0.0, 0.0, 0.0, 1.0), + scale: (1.0, 1.0, 1.0), + ), + source_node: Some("Node0"), + source_mesh: Some("Mesh0"), + source_material: Some("metal_stool_01"), + ), + ], + warnings: [], +) \ No newline at end of file diff --git a/assets/meshes/generated/b8b43f26-0eee-4ee8-9e16-53aedbc69205.static_mesh.ron b/assets/meshes/generated/b8b43f26-0eee-4ee8-9e16-53aedbc69205.static_mesh.ron deleted file mode 100644 index 9d9b032..0000000 --- a/assets/meshes/generated/b8b43f26-0eee-4ee8-9e16-53aedbc69205.static_mesh.ron +++ /dev/null @@ -1,770 +0,0 @@ -( - schema_version: 1, - asset_id: "b8b43f26-0eee-4ee8-9e16-53aedbc69205", - label: "Room3", - source: ( - path: "assets/models/Room3.fbx", - format: "fbx", - fingerprint: ( - byte_len: 159836, - modified_unix_secs: 1780196194, - ), - dependencies: [], - ), - import: ( - scale: 1.7, - generate_collider: true, - lod0_only: true, - placement_mode: StaticAsset, - hierarchy_mode: SingleActor, - material_policy: AuthoringOverride, - ), - metadata: ( - mesh_count: 23, - material_count: 11, - node_count: 24, - animation_count: 0, - skin_count: 0, - light_count: 0, - camera_count: 0, - ), - parts: [ - ( - id: "mesh:mesh1000", - name: "Windows Small.000 / Material 0", - mesh_label: "Mesh1000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (2.9808052, 1.9115452, -0.40523553), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248338, 1.0), - ), - source_node: Some("Node1"), - source_mesh: Some("Mesh1"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh10000", - name: "Window Right Up.000 / Material 0", - mesh_label: "Mesh10000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (-0.97121567, 1.8132615, 3.0487669), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node10"), - source_mesh: Some("Mesh10"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh10001", - name: "Window Right Up.000 / Material 1", - mesh_label: "Mesh10001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (-0.97121567, 1.8132615, 3.0487669), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node10"), - source_mesh: Some("Mesh10"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh1001", - name: "Windows Small.000 / Material 1", - mesh_label: "Mesh1001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (2.9808052, 1.9115452, -0.40523553), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248338, 1.0), - ), - source_node: Some("Node1"), - source_mesh: Some("Mesh1"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh11000", - name: "Window Right Low.000 / Material 0", - mesh_label: "Mesh11000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (-0.9734232, 1.4140832, 3.0185344), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node11"), - source_mesh: Some("Mesh11"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh11001", - name: "Window Right Low.000 / Material 1", - mesh_label: "Mesh11001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (-0.9734232, 1.4140832, 3.0185344), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node11"), - source_mesh: Some("Mesh11"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh12000", - name: "Window Left Up.000 / Material 0", - mesh_label: "Mesh12000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (1.583923, 1.8132616, 3.0487669), - rotation: (0.70710677, 0.000000034125748, -0.000000025061802, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node12"), - source_mesh: Some("Mesh12"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh12001", - name: "Window Left Up.000 / Material 1", - mesh_label: "Mesh12001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (1.583923, 1.8132616, 3.0487669), - rotation: (0.70710677, 0.000000034125748, -0.000000025061802, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node12"), - source_mesh: Some("Mesh12"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh13000", - name: "Window Left Low.000 / Material 0", - mesh_label: "Mesh13000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (1.5816854, 1.414083, 3.0185344), - rotation: (0.70710677, 0.000000034125748, -0.000000025061802, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node13"), - source_mesh: Some("Mesh13"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh13001", - name: "Window Left Low.000 / Material 1", - mesh_label: "Mesh13001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (1.5816854, 1.414083, 3.0185344), - rotation: (0.70710677, 0.000000034125748, -0.000000025061802, -0.7071067), - scale: (1.2387476, 0.9248341, 1.0), - ), - source_node: Some("Node13"), - source_mesh: Some("Mesh13"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh14000", - name: "Window Center Low.000 / Material 0", - mesh_label: "Mesh14000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (0.29577515, 1.4927816, 3.0203688), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248343, 1.0), - ), - source_node: Some("Node14"), - source_mesh: Some("Mesh14"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh14001", - name: "Window Center Low.000 / Material 1", - mesh_label: "Mesh14001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (0.29577515, 1.4927816, 3.0203688), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248343, 1.0), - ), - source_node: Some("Node14"), - source_mesh: Some("Mesh14"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh15000", - name: "Window Cente Up.000 / Material 0", - mesh_label: "Mesh15000", - material_id: Some("material:material0"), - material_slot_name: "Window Frame Metal.002", - material_label: Some("Material0"), - local_transform: ( - translation: (0.2986794, 1.7560294, 3.0472102), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248343, 1.0), - ), - source_node: Some("Node15"), - source_mesh: Some("Mesh15"), - source_material: Some("Window Frame Metal.002"), - ), - ( - id: "mesh:mesh15001", - name: "Window Cente Up.000 / Material 1", - mesh_label: "Mesh15001", - material_id: Some("material:material1"), - material_slot_name: "Window Glass.002", - material_label: Some("Material1"), - local_transform: ( - translation: (0.2986794, 1.7560294, 3.0472102), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248343, 1.0), - ), - source_node: Some("Node15"), - source_mesh: Some("Mesh15"), - source_material: Some("Window Glass.002"), - ), - ( - id: "mesh:mesh16000", - name: "Lights.Potlight.004 / Material 0", - mesh_label: "Mesh16000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (1.9999996, 2.9877658, 1.1368884), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node16"), - source_mesh: Some("Mesh16"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh16002", - name: "Lights.Potlight.004 / Material 2", - mesh_label: "Mesh16002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (1.9999996, 2.9877658, 1.1368884), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node16"), - source_mesh: Some("Mesh16"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh17000", - name: "Lights.Potlight.003 / Material 0", - mesh_label: "Mesh17000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (1.9999996, 2.9877653, -1.8631115), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node17"), - source_mesh: Some("Mesh17"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh17002", - name: "Lights.Potlight.003 / Material 2", - mesh_label: "Mesh17002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (1.9999996, 2.9877653, -1.8631115), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node17"), - source_mesh: Some("Mesh17"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh18000", - name: "Lights.Potlight.002 / Material 0", - mesh_label: "Mesh18000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (-1.8000002, 2.9877653, -1.8631115), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node18"), - source_mesh: Some("Mesh18"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh18002", - name: "Lights.Potlight.002 / Material 2", - mesh_label: "Mesh18002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (-1.8000002, 2.9877653, -1.8631115), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node18"), - source_mesh: Some("Mesh18"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh19000", - name: "Lights.Potlight.001 / Material 0", - mesh_label: "Mesh19000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (-1.8000002, 2.9877658, 1.1368884), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node19"), - source_mesh: Some("Mesh19"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh19002", - name: "Lights.Potlight.001 / Material 2", - mesh_label: "Mesh19002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (-1.8000002, 2.9877658, 1.1368884), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node19"), - source_mesh: Some("Mesh19"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh2000", - name: "Window Right.000 / Material 0", - mesh_label: "Mesh2000", - material_id: Some("material:defaultmaterial"), - material_slot_name: "Default Material", - material_label: Some("DefaultMaterial"), - local_transform: ( - translation: (-0.9659626, 1.613703, 3.0355246), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248338, 1.0), - ), - source_node: Some("Node2"), - source_mesh: Some("Mesh2"), - source_material: Some("Default Material"), - ), - ( - id: "mesh:mesh20000", - name: "Lights.Center.003 / Material 0", - mesh_label: "Mesh20000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (0.24156909, 2.792474, -0.63835967), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node20"), - source_mesh: Some("Mesh20"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh20001", - name: "Lights.Center.003 / Material 1", - mesh_label: "Mesh20001", - material_id: Some("material:material8"), - material_slot_name: "Matte Black.002", - material_label: Some("Material8"), - local_transform: ( - translation: (0.24156909, 2.792474, -0.63835967), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node20"), - source_mesh: Some("Mesh20"), - source_material: Some("Matte Black.002"), - ), - ( - id: "mesh:mesh20002", - name: "Lights.Center.003 / Material 2", - mesh_label: "Mesh20002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (0.24156909, 2.792474, -0.63835967), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node20"), - source_mesh: Some("Mesh20"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh21000", - name: "Lights.Center.002 / Material 0", - mesh_label: "Mesh21000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (0.25059184, 2.7924743, 0.36630905), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node21"), - source_mesh: Some("Mesh21"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh21001", - name: "Lights.Center.002 / Material 1", - mesh_label: "Mesh21001", - material_id: Some("material:material8"), - material_slot_name: "Matte Black.002", - material_label: Some("Material8"), - local_transform: ( - translation: (0.25059184, 2.7924743, 0.36630905), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node21"), - source_mesh: Some("Mesh21"), - source_material: Some("Matte Black.002"), - ), - ( - id: "mesh:mesh21002", - name: "Lights.Center.002 / Material 2", - mesh_label: "Mesh21002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (0.25059184, 2.7924743, 0.36630905), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node21"), - source_mesh: Some("Mesh21"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh22000", - name: "Lights.Center.001 / Material 0", - mesh_label: "Mesh22000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (0.21980214, 2.7924743, -0.15153252), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node22"), - source_mesh: Some("Mesh22"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh22001", - name: "Lights.Center.001 / Material 1", - mesh_label: "Mesh22001", - material_id: Some("material:material8"), - material_slot_name: "Matte Black.002", - material_label: Some("Material8"), - local_transform: ( - translation: (0.21980214, 2.7924743, -0.15153252), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node22"), - source_mesh: Some("Mesh22"), - source_material: Some("Matte Black.002"), - ), - ( - id: "mesh:mesh22002", - name: "Lights.Center.001 / Material 2", - mesh_label: "Mesh22002", - material_id: Some("material:material9"), - material_slot_name: "Light.002", - material_label: Some("Material9"), - local_transform: ( - translation: (0.21980214, 2.7924743, -0.15153252), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node22"), - source_mesh: Some("Mesh22"), - source_material: Some("Light.002"), - ), - ( - id: "mesh:mesh23002", - name: "Door_Knob.000 / Material 2", - mesh_label: "Mesh23002", - material_id: Some("material:material10"), - material_slot_name: "Door Knob Gold.002", - material_label: Some("Material10"), - local_transform: ( - translation: (-3.0024662, 1.0415587, -1.6272027), - rotation: (0.70710677, -0.0000000023899738, 0.00000000007193627, -0.7071067), - scale: (0.07870353, 0.45364702, 0.45364702), - ), - source_node: Some("Node23"), - source_mesh: Some("Mesh23"), - source_material: Some("Door Knob Gold.002"), - ), - ( - id: "mesh:mesh3000", - name: "Window Left.000 / Material 0", - mesh_label: "Mesh3000", - material_id: Some("material:defaultmaterial"), - material_slot_name: "Default Material", - material_label: Some("DefaultMaterial"), - local_transform: ( - translation: (1.5851622, 1.613703, 3.0355246), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248338, 1.0), - ), - source_node: Some("Node3"), - source_mesh: Some("Mesh3"), - source_material: Some("Default Material"), - ), - ( - id: "mesh:mesh4000", - name: "Window Frames.000 / Material 0", - mesh_label: "Mesh4000", - material_id: Some("material:material2"), - material_slot_name: "Painted Wooden Frame.002", - material_label: Some("Material2"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.5000001, -0.49999994, -0.5, -0.50000006), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node4"), - source_mesh: Some("Mesh4"), - source_material: Some("Painted Wooden Frame.002"), - ), - ( - id: "mesh:mesh5000", - name: "Window Center.000 / Material 0", - mesh_label: "Mesh5000", - material_id: Some("material:defaultmaterial"), - material_slot_name: "Default Material", - material_label: Some("DefaultMaterial"), - local_transform: ( - translation: (0.3107944, 1.613703, 3.0355246), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2387476, 0.9248338, 1.0), - ), - source_node: Some("Node5"), - source_mesh: Some("Mesh5"), - source_material: Some("Default Material"), - ), - ( - id: "mesh:mesh6000", - name: "Room.000 / Material 0", - mesh_label: "Mesh6000", - material_id: Some("material:material2"), - material_slot_name: "Painted Wooden Frame.002", - material_label: Some("Material2"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node6"), - source_mesh: Some("Mesh6"), - source_material: Some("Painted Wooden Frame.002"), - ), - ( - id: "mesh:mesh6001", - name: "Room.000 / Material 1", - mesh_label: "Mesh6001", - material_id: Some("material:material3"), - material_slot_name: "Ceiling.002", - material_label: Some("Material3"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node6"), - source_mesh: Some("Mesh6"), - source_material: Some("Ceiling.002"), - ), - ( - id: "mesh:mesh6002", - name: "Room.000 / Material 2", - mesh_label: "Mesh6002", - material_id: Some("material:material4"), - material_slot_name: "Hardwood Floor.002", - material_label: Some("Material4"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node6"), - source_mesh: Some("Mesh6"), - source_material: Some("Hardwood Floor.002"), - ), - ( - id: "mesh:mesh6003", - name: "Room.000 / Material 3", - mesh_label: "Mesh6003", - material_id: Some("material:material5"), - material_slot_name: "Painted Wall.002", - material_label: Some("Material5"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node6"), - source_mesh: Some("Mesh6"), - source_material: Some("Painted Wall.002"), - ), - ( - id: "mesh:mesh6004", - name: "Room.000 / Material 4", - mesh_label: "Mesh6004", - material_id: Some("material:material6"), - material_slot_name: "Brick Accent Wall.002", - material_label: Some("Material6"), - local_transform: ( - translation: (0.0, 0.0027079582, 0.0), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node6"), - source_mesh: Some("Mesh6"), - source_material: Some("Brick Accent Wall.002"), - ), - ( - id: "mesh:mesh7000", - name: "Lights.000 / Material 0", - mesh_label: "Mesh7000", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (0.24510838, 2.9453187, -0.14346208), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.0, 1.0, 1.0), - ), - source_node: Some("Node7"), - source_mesh: Some("Mesh7"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh8000", - name: "Door Frame.000 / Material 0", - mesh_label: "Mesh8000", - material_id: Some("material:material2"), - material_slot_name: "Painted Wooden Frame.002", - material_label: Some("Material2"), - local_transform: ( - translation: (-3.0060542, 0.0027079582, -2.1066966), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (1.2365791, 1.0, 1.0), - ), - source_node: Some("Node8"), - source_mesh: Some("Mesh8"), - source_material: Some("Painted Wooden Frame.002"), - ), - ( - id: "mesh:mesh9000", - name: "Door.000 / Material 0", - mesh_label: "Mesh9000", - material_id: Some("material:material2"), - material_slot_name: "Painted Wooden Frame.002", - material_label: Some("Material2"), - local_transform: ( - translation: (-2.9729986, 0.22508162, -2.6792114), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (0.07870354, 0.45364702, 0.45364702), - ), - source_node: Some("Node9"), - source_mesh: Some("Mesh9"), - source_material: Some("Painted Wooden Frame.002"), - ), - ( - id: "mesh:mesh9001", - name: "Door.000 / Material 1", - mesh_label: "Mesh9001", - material_id: Some("material:material7"), - material_slot_name: "Light Metal.002", - material_label: Some("Material7"), - local_transform: ( - translation: (-2.9729986, 0.22508162, -2.6792114), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (0.07870354, 0.45364702, 0.45364702), - ), - source_node: Some("Node9"), - source_mesh: Some("Mesh9"), - source_material: Some("Light Metal.002"), - ), - ( - id: "mesh:mesh9002", - name: "Door.000 / Material 2", - mesh_label: "Mesh9002", - material_id: Some("material:material10"), - material_slot_name: "Door Knob Gold.002", - material_label: Some("Material10"), - local_transform: ( - translation: (-2.9729986, 0.22508162, -2.6792114), - rotation: (0.70710677, 0.0, 0.0, -0.7071067), - scale: (0.07870354, 0.45364702, 0.45364702), - ), - source_node: Some("Node9"), - source_mesh: Some("Mesh9"), - source_material: Some("Door Knob Gold.002"), - ), - ], - warnings: [], -) \ No newline at end of file diff --git a/assets/meshes/generated/b98ef565-3500-49e7-9935-f685fa9b2594.static_mesh.ron b/assets/meshes/generated/b98ef565-3500-49e7-9935-f685fa9b2594.static_mesh.ron new file mode 100644 index 0000000..fbcee12 --- /dev/null +++ b/assets/meshes/generated/b98ef565-3500-49e7-9935-f685fa9b2594.static_mesh.ron @@ -0,0 +1,50 @@ +( + schema_version: 1, + asset_id: "b98ef565-3500-49e7-9935-f685fa9b2594", + label: "painted_wooden_chair_02_2k", + source: ( + path: "assets/models/painted_wooden_chair_02_2k.fbx", + format: "fbx", + fingerprint: ( + byte_len: 59964, + modified_unix_secs: 1780713434, + ), + dependencies: [], + ), + import: ( + scale: 1.0, + generate_collider: true, + lod0_only: true, + placement_mode: StaticAsset, + hierarchy_mode: SingleActor, + material_policy: SourceMaterials, + ), + metadata: ( + mesh_count: 1, + material_count: 1, + node_count: 2, + animation_count: 0, + skin_count: 0, + light_count: 0, + camera_count: 0, + ), + parts: [ + ( + id: "mesh:mesh1000", + name: "painted_wooden_chair_02 / Material 0", + mesh_label: "Mesh1000", + material_id: Some("material:material0"), + material_slot_name: "painted_wooden_chair_02", + material_label: Some("Material0"), + local_transform: ( + translation: (0.0, 0.0, 0.0), + rotation: (0.5, 0.50000006, 0.5, -0.50000006), + scale: (1.0, 1.0, 1.0), + ), + source_node: Some("Node1"), + source_mesh: Some("Mesh1"), + source_material: Some("painted_wooden_chair_02"), + ), + ], + warnings: [], +) \ No newline at end of file diff --git a/assets/models/Dumpster.fbx b/assets/models/Dumpster.fbx deleted file mode 100755 index 451b265..0000000 --- a/assets/models/Dumpster.fbx +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cf8b0fb7877299eca36b8d7735ee4716395add8215c9c0760ff43bed2fb8800e -size 50940 diff --git a/assets/models/Room3.fbx b/assets/models/Room3.fbx deleted file mode 100755 index b4103e2..0000000 --- a/assets/models/Room3.fbx +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cf7fedc052323c383164d79b7efafce890bf3e7436933b554e7baecf19add196 -size 159836 diff --git a/assets/models/metal_stool_01_2k.gltf b/assets/models/metal_stool_01_2k.gltf new file mode 100644 index 0000000..88d9e41 --- /dev/null +++ b/assets/models/metal_stool_01_2k.gltf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5886418d98b7ec4e932f54a2ba44896dd9e61276bf62d21697b9136f2b0803e +size 2790 diff --git a/assets/models/painted_wooden_chair_02_2k.fbx b/assets/models/painted_wooden_chair_02_2k.fbx new file mode 100644 index 0000000..8ca0cba --- /dev/null +++ b/assets/models/painted_wooden_chair_02_2k.fbx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:799a26c5fb7ae3e3b87a779e6e15e8b0ae0e833efd7f1202e6a702e3fca6da5e +size 59964 diff --git a/assets/textures/metal_stool_01_arm_2k.jpg b/assets/textures/metal_stool_01_arm_2k.jpg new file mode 100644 index 0000000..041030d --- /dev/null +++ b/assets/textures/metal_stool_01_arm_2k.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:37d48a9d05a33c47554d86836451c9d9cdc7fd4eeed52faa19ae501ee6858efd +size 3152060 diff --git a/assets/textures/metal_stool_01_diff_2k.jpg b/assets/textures/metal_stool_01_diff_2k.jpg new file mode 100644 index 0000000..2833641 --- /dev/null +++ b/assets/textures/metal_stool_01_diff_2k.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f77669e6a7a94bc59037113f2d32b5e4415bb115a20be0561d6121cb753cffdd +size 2603921 diff --git a/assets/textures/metal_stool_01_nor_gl_2k.jpg b/assets/textures/metal_stool_01_nor_gl_2k.jpg new file mode 100644 index 0000000..66bef14 --- /dev/null +++ b/assets/textures/metal_stool_01_nor_gl_2k.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2a2f7e8bae076d291a9edb8e3907a88ef51c0dc4e841d0d831441a9ac3f838d +size 2779150 diff --git a/crates/editor/assets/shaders/actor_icon_overlay.wgsl b/crates/editor/assets/shaders/actor_icon_overlay.wgsl new file mode 100644 index 0000000..a247b19 --- /dev/null +++ b/crates/editor/assets/shaders/actor_icon_overlay.wgsl @@ -0,0 +1,24 @@ +#import bevy_pbr::forward_io::VertexOutput + +struct ActorIconOverlayMaterial { + brightness: f32, +} + +@group(#{MATERIAL_BIND_GROUP}) @binding(0) +var icon_texture: texture_2d; + +@group(#{MATERIAL_BIND_GROUP}) @binding(1) +var icon_sampler: sampler; + +@group(#{MATERIAL_BIND_GROUP}) @binding(2) +var material: ActorIconOverlayMaterial; + +@fragment +fn fragment(in: VertexOutput) -> @location(0) vec4 { + let texel = textureSample(icon_texture, icon_sampler, in.uv); + if texel.a < 0.01 { + discard; + } + + return vec4(texel.rgb * material.brightness, texel.a); +} diff --git a/crates/editor/src/assets/catalog.rs b/crates/editor/src/assets/catalog.rs index 3666ed9..406afcf 100644 --- a/crates/editor/src/assets/catalog.rs +++ b/crates/editor/src/assets/catalog.rs @@ -11,7 +11,8 @@ use shared::{ use walkdir::WalkDir; use crate::asset_db::{ - find_asset_by_path, find_asset_mut_by_path, AssetRegistry, ModelPlacementMode, + find_asset_by_path, find_asset_mut_by_path, AssetRegistry, MaterialImportPolicy, + ModelPlacementMode, }; use crate::assets::static_mesh::{ load_static_mesh_manifest, refresh_static_mesh_artifact, renderer_from_manifest, @@ -21,10 +22,24 @@ use crate::history::{spawn_with_history, EditorEntitySnapshot}; pub const BUILTINS_FOLDER: &str = "__builtins__"; pub const ASSETS_ROOT: &str = "assets"; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum AssetSubAssetKind { + Mesh, + Material, + Texture, +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum AssetSelection { Builtin(String), File(String), + SubAsset { + parent_path: String, + sub_asset_id: String, + label: String, + kind: AssetSubAssetKind, + source_path: Option, + }, } impl AssetSelection { @@ -34,6 +49,21 @@ impl AssetSelection { None => AssetSelection::Builtin(asset.label.clone()), } } + + pub fn parent_path(&self) -> Option<&str> { + match self { + AssetSelection::Builtin(_) => None, + AssetSelection::File(path) => Some(path), + AssetSelection::SubAsset { parent_path, .. } => Some(parent_path), + } + } + + pub fn display_label(&self) -> &str { + match self { + AssetSelection::Builtin(label) | AssetSelection::File(label) => label, + AssetSelection::SubAsset { label, .. } => label, + } + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -137,6 +167,10 @@ impl EditorAssets { .assets .iter() .find(|asset| asset.path.as_deref() == Some(path.as_str())), + AssetSelection::SubAsset { parent_path, .. } => self + .assets + .iter() + .find(|asset| asset.path.as_deref() == Some(parent_path.as_str())), } } @@ -152,10 +186,20 @@ impl EditorAssets { .and_then(|selection| self.asset_for_selection(selection)) } + pub fn dragging_selection(&self) -> Option<&AssetSelection> { + self.dragging.as_ref() + } + pub fn select(&mut self, selection: AssetSelection) { if let Some(asset) = self.asset_for_selection(&selection).cloned() { + let selected_label = match &selection { + AssetSelection::SubAsset { label, kind, .. } => { + format!("{kind:?}: {label}") + } + _ => asset.label.clone(), + }; self.selected = Some(selection); - self.status = format!("Selected {}", asset.label); + self.status = format!("Selected {selected_label}"); } } @@ -302,6 +346,7 @@ fn should_skip_asset_path(path: &Path) -> bool { let normalized = normalize_asset_path(path); normalized == "assets/project.ron" || normalized.starts_with("assets/.index/") + || normalized.starts_with("assets/.trash/") || normalized.starts_with(crate::assets::static_mesh::STATIC_MESH_ARTIFACT_DIR) } @@ -543,6 +588,101 @@ pub fn spawn_asset_at(world: &mut World, asset: &EditorAsset, translation: Vec3) Some(spawn_with_history(world, snapshot)) } +pub fn spawn_subasset_at( + world: &mut World, + selection: &AssetSelection, + translation: Vec3, +) -> Option { + let AssetSelection::SubAsset { + parent_path, + sub_asset_id, + label, + kind: AssetSubAssetKind::Mesh, + .. + } = selection + else { + return None; + }; + + let record = world + .get_resource::() + .and_then(|registry| find_asset_by_path(registry, parent_path))?; + let manifest_path = record + .import_settings + .static_mesh_manifest_path + .as_deref()?; + let manifest = load_static_mesh_manifest(manifest_path).ok()?; + let part = manifest + .parts + .iter() + .find(|part| part_effective_id_for_selection(part) == *sub_asset_id)?; + + let mesh_ref = shared::EditorAssetRef::new( + manifest.asset_id.clone(), + sub_asset_id.clone(), + label.clone(), + ); + let material = matches!( + record.import_settings.material_policy, + MaterialImportPolicy::SourceMaterials + ) + .then(|| { + part_effective_material_id_for_selection(part).map(|id| { + shared::EditorAssetRef::new( + manifest.asset_id.clone(), + id, + part.material_slot_name.clone(), + ) + }) + }) + .flatten(); + let slot = shared::StaticMeshRendererEntry { + id: shared::ComponentInstanceId::new("slot:0"), + name: label.clone(), + mesh: mesh_ref.clone(), + material, + local_transform: part.local_transform, + visible: true, + cast_shadows: true, + receive_shadows: true, + }; + + let mut snapshot = EditorEntitySnapshot { + actor_id: None, + actor_kind: ActorKind::StaticMesh, + actor_name: None, + name: Some(label.clone()), + transform: Transform::from_translation(translation + Vec3::Y * 0.1), + primitive: None, + static_mesh_renderer: Some(shared::StaticMeshRenderer::single(slot)), + material: None, + material_override: None, + rigid_body: None, + collider: None, + physics: None, + light: None, + player_spawn: false, + model: None, + prefab: None, + prefab_instance: None, + weapon_spawn: None, + trigger_volume: None, + post_process_volume: None, + team_spawn: None, + objective: None, + hierarchy_sibling_index: 0, + editor_visibility: EditorVisibility::default(), + children: Vec::new(), + }; + snapshot.transform.scale *= record.import_settings.scale; + if record.import_settings.generate_collider { + snapshot.rigid_body = Some(RigidBodyDesc::default()); + snapshot.collider = Some(ColliderDesc::static_mesh(vec![mesh_ref])); + } + + Some(spawn_with_history(world, snapshot)) +} + fn apply_static_mesh_placement_mode( snapshot: &mut EditorEntitySnapshot, renderer: shared::StaticMeshRenderer, @@ -619,6 +759,24 @@ fn static_mesh_collider_for_renderer(renderer: &shared::StaticMeshRenderer) -> C ) } +fn part_effective_id_for_selection(part: &crate::assets::static_mesh::StaticMeshPart) -> String { + if part.id.trim().is_empty() { + crate::assets::static_mesh::part_id_from_label(&part.mesh_label) + } else { + part.id.clone() + } +} + +fn part_effective_material_id_for_selection( + part: &crate::assets::static_mesh::StaticMeshPart, +) -> Option { + part.material_id.clone().or_else(|| { + part.material_label + .as_ref() + .map(|label| crate::assets::static_mesh::material_id_from_label(label)) + }) +} + fn static_mesh_renderer_for_asset( world: &mut World, path: &str, diff --git a/crates/editor/src/assets/mod.rs b/crates/editor/src/assets/mod.rs index e91ff9a..965e241 100644 --- a/crates/editor/src/assets/mod.rs +++ b/crates/editor/src/assets/mod.rs @@ -11,5 +11,5 @@ pub mod thumbnails; pub use catalog::*; pub use thumbnails::{ draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, prefetch_folder_thumbnails, - AssetThumbnailCache, ThumbnailCacheSnapshot, ThumbnailState, ThumbnailsPlugin, + AssetThumbnailCache, ThumbnailCacheSnapshot, ThumbnailState, ThumbnailStudio, ThumbnailsPlugin, }; diff --git a/crates/editor/src/assets/thumbnails/cache.rs b/crates/editor/src/assets/thumbnails/cache.rs index bb16a3a..c6e9726 100644 --- a/crates/editor/src/assets/thumbnails/cache.rs +++ b/crates/editor/src/assets/thumbnails/cache.rs @@ -8,6 +8,7 @@ use egui_phosphor_icons::icons; use super::sources::gltf::gltf_base_color_texture_path; use super::studio::{model_file_exists, ThumbnailStudio}; +use super::ThumbnailJobSource; use crate::assets::{ asset_cache_key, asset_server_path, EditorAsset, EditorAssetKind, EditorAssets, }; @@ -114,6 +115,97 @@ impl AssetThumbnailCache { } } + pub fn request_mesh_subasset( + &mut self, + key: String, + model_path: String, + mesh_label: String, + material_label: Option, + studio: &mut ThumbnailStudio, + ) { + if matches!( + self.state(&key), + Some(ThumbnailState::Ready | ThumbnailState::Pending) + ) || self.failed.contains_key(&key) + { + return; + } + if !model_file_exists(&model_path) { + self.mark_studio_failed(&key, "file not found", false); + return; + } + if studio.enqueue_source( + key.clone(), + ThumbnailJobSource::MeshSubAsset { + model_path, + mesh_label, + material_label, + }, + ) { + self.studio_pending.insert(key); + } + } + + pub fn request_source_material( + &mut self, + key: String, + model_path: String, + material_label: String, + studio: &mut ThumbnailStudio, + ) { + if matches!( + self.state(&key), + Some(ThumbnailState::Ready | ThumbnailState::Pending) + ) || self.failed.contains_key(&key) + { + return; + } + if !model_file_exists(&model_path) { + self.mark_studio_failed(&key, "file not found", false); + return; + } + if studio.enqueue_source( + key.clone(), + ThumbnailJobSource::SourceMaterial { + model_path, + material_label, + }, + ) { + self.studio_pending.insert(key); + } + } + + pub fn request_material_asset( + &mut self, + key: String, + path: String, + studio: &mut ThumbnailStudio, + ) { + if matches!( + self.state(&key), + Some(ThumbnailState::Ready | ThumbnailState::Pending) + ) || self.failed.contains_key(&key) + { + return; + } + let material = match shared::MaterialAsset::load_from_path(&path) { + Ok(asset) => asset, + Err(error) => { + self.mark_studio_failed(&key, &error, true); + return; + } + }; + if studio.enqueue_source( + key.clone(), + ThumbnailJobSource::MaterialAsset { + label: material.label.clone(), + material: material.material, + }, + ) { + self.studio_pending.insert(key); + } + } + pub(crate) fn complete_studio_thumbnail( &mut self, key: &str, @@ -297,6 +389,18 @@ pub struct ThumbnailCacheSnapshot { } impl ThumbnailCacheSnapshot { + pub fn texture_for_key(&self, key: &str) -> Option { + self.texture_ids.get(key).copied() + } + + pub fn is_pending_key(&self, key: &str) -> bool { + self.pending_keys.iter().any(|pending| pending == key) + } + + pub fn is_failed_key(&self, key: &str) -> bool { + self.failed_keys.iter().any(|failed| failed == key) + } + pub fn texture_for(&self, asset: &EditorAsset) -> Option { self.texture_ids.get(&asset_cache_key(asset)).copied() } @@ -331,7 +435,7 @@ pub fn prefetch_folder_thumbnails(world: &mut World, folder: &str) { .filter(|asset| { matches!( asset.kind, - EditorAssetKind::Texture | EditorAssetKind::Model + EditorAssetKind::Texture | EditorAssetKind::Model | EditorAssetKind::Material ) }) .filter_map(|asset| { @@ -352,6 +456,9 @@ pub fn prefetch_folder_thumbnails(world: &mut World, folder: &str) { EditorAssetKind::Model => { cache.request_model(key, path, &asset_server, &mut studio) } + EditorAssetKind::Material => { + cache.request_material_asset(key, path, &mut studio) + } _ => {} } } diff --git a/crates/editor/src/assets/thumbnails/job.rs b/crates/editor/src/assets/thumbnails/job.rs index 910d7f2..365d5da 100644 --- a/crates/editor/src/assets/thumbnails/job.rs +++ b/crates/editor/src/assets/thumbnails/job.rs @@ -1,7 +1,40 @@ //! Thumbnail generation job descriptor. +use shared::MaterialDesc; + #[derive(Debug, Clone)] pub struct ThumbnailJob { pub cache_key: String, - pub model_path: String, + pub source: ThumbnailJobSource, +} + +#[derive(Debug, Clone)] +pub enum ThumbnailJobSource { + Model { + model_path: String, + }, + MeshSubAsset { + model_path: String, + mesh_label: String, + material_label: Option, + }, + SourceMaterial { + model_path: String, + material_label: String, + }, + MaterialAsset { + label: String, + material: MaterialDesc, + }, +} + +impl ThumbnailJobSource { + pub fn label(&self) -> &str { + match self { + Self::Model { model_path } + | Self::MeshSubAsset { model_path, .. } + | Self::SourceMaterial { model_path, .. } => model_path, + Self::MaterialAsset { label, .. } => label, + } + } } diff --git a/crates/editor/src/assets/thumbnails/mod.rs b/crates/editor/src/assets/thumbnails/mod.rs index 71f3235..988e388 100644 --- a/crates/editor/src/assets/thumbnails/mod.rs +++ b/crates/editor/src/assets/thumbnails/mod.rs @@ -11,7 +11,7 @@ pub use cache::{ draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, prefetch_folder_thumbnails, AssetThumbnailCache, AssetThumbnailsPlugin, ThumbnailCacheSnapshot, ThumbnailState, }; -pub use job::ThumbnailJob; +pub use job::{ThumbnailJob, ThumbnailJobSource}; pub use sources::{FbxThumbnailSource, GltfThumbnailSource, ThumbnailModelSource}; pub use studio::{ model_file_exists, model_scene_asset_path, ThumbnailStudio, ThumbnailStudioPlugin, diff --git a/crates/editor/src/assets/thumbnails/studio.rs b/crates/editor/src/assets/thumbnails/studio.rs index a76b359..fac4b68 100644 --- a/crates/editor/src/assets/thumbnails/studio.rs +++ b/crates/editor/src/assets/thumbnails/studio.rs @@ -12,10 +12,10 @@ use bevy::prelude::*; use bevy::render::render_resource::TextureFormat; use bevy::scene::SceneRoot; use bevy_egui::EguiUserTextures; -use shared::ModelRef; +use shared::{material_from_desc, ModelRef}; use super::cache::AssetThumbnailCache; -use super::job::ThumbnailJob; +use super::job::{ThumbnailJob, ThumbnailJobSource}; use super::sources::{source_for_extension, uses_scene_root}; use crate::assets::asset_server_path; use crate::infra::EditorOnly; @@ -52,7 +52,8 @@ pub struct ThumbnailStudio { struct ActiveModelThumbnail { cache_key: String, - model_path: String, + label: String, + render_image: Handle, root: Entity, meshes_ready: bool, mesh_warmup_frames: u8, @@ -75,6 +76,10 @@ impl Plugin for ThumbnailStudioPlugin { impl ThumbnailStudio { pub fn enqueue(&mut self, cache_key: String, model_path: String) -> bool { + self.enqueue_source(cache_key, ThumbnailJobSource::Model { model_path }) + } + + pub fn enqueue_source(&mut self, cache_key: String, source: ThumbnailJobSource) -> bool { if self .active .as_ref() @@ -85,10 +90,7 @@ impl ThumbnailStudio { if self.queue.iter().any(|job| job.cache_key == cache_key) { return false; } - self.queue.push_back(ThumbnailJob { - cache_key, - model_path, - }); + self.queue.push_back(ThumbnailJob { cache_key, source }); true } @@ -122,12 +124,7 @@ pub fn model_scene_asset_path(path: &str, scene_index: usize) -> String { } fn setup_thumbnail_studio(mut commands: Commands, mut images: ResMut>) { - let render_image = images.add(Image::new_target_texture( - THUMB_SIZE, - THUMB_SIZE, - TextureFormat::Rgba8UnormSrgb, - None, - )); + let render_image = images.add(studio_render_image()); let camera = commands .spawn(( @@ -200,8 +197,10 @@ fn process_thumbnail_studio( mut textures: ResMut, mut materials: ResMut>, mut mesh_storage: ResMut>, + mut images: ResMut>, asset_server: Res, mut cameras: Query<&mut Camera, With>, + mut camera_targets: Query<&mut RenderTarget, With>, mut camera_transforms: Query<&mut Transform, With>, children: Query<&Children>, transforms: Query<&GlobalTransform>, @@ -240,13 +239,14 @@ fn process_thumbnail_studio( if active.wait_frames >= LOAD_TIMEOUT_FRAMES { warn!( "Model thumbnail timed out framing {} (meshes may still be loading)", - active.model_path + active.label ); fail_active_thumbnail( &mut commands, &mut studio, &mut cache, &mut cameras, + &mut camera_targets, active, "load timeout", true, @@ -277,10 +277,16 @@ fn process_thumbnail_studio( } else { cache.complete_studio_thumbnail( &active.cache_key, - studio.render_image.clone(), + active.render_image.clone(), &mut textures, ); - finish_active_thumbnail(&mut commands, &mut studio, &mut cameras, active); + finish_active_thumbnail( + &mut commands, + &mut studio, + &mut cameras, + &mut camera_targets, + active, + ); } return; } @@ -294,43 +300,45 @@ fn process_thumbnail_studio( return; }; - let mut root_entity = commands.spawn(( - EditorOnly, - ThumbnailSceneRoot, - ThumbnailStudioLayer, - RenderLayers::layer(THUMBNAIL_LAYER), - Transform::default(), - )); - - if uses_scene_root(&job.model_path) { - root_entity.insert(SceneRoot( - asset_server.load(model_scene_asset_path(&job.model_path, 0)), - )); + let render_image = images.add(studio_render_image()); + if let Ok(mut target) = camera_targets.get_mut(studio.camera) { + *target = RenderTarget::Image(render_image.clone().into()); + } + if let Ok(mut camera) = cameras.get_mut(studio.camera) { + camera.is_active = false; } - let root = root_entity.id(); + let root = commands + .spawn(( + EditorOnly, + ThumbnailSceneRoot, + ThumbnailStudioLayer, + RenderLayers::layer(THUMBNAIL_LAYER), + Transform::default(), + )) + .id(); - let content_spawned = if let Some(source) = source_for_extension(&job.model_path) { - source.spawn_preview( - &mut commands, - &mut mesh_storage, - &mut materials, - root, - &job.model_path, - ) - } else { - uses_scene_root(&job.model_path) - }; + let label = job.source.label().to_string(); + let content_spawned = spawn_thumbnail_job_content( + &mut commands, + &mut mesh_storage, + &mut materials, + &asset_server, + root, + &job.source, + ); - if source_for_extension(&job.model_path).is_some() && !content_spawned { + if !content_spawned { fail_active_thumbnail( &mut commands, &mut studio, &mut cache, &mut cameras, + &mut camera_targets, ActiveModelThumbnail { cache_key: job.cache_key, - model_path: job.model_path, + label, + render_image, root, meshes_ready: false, mesh_warmup_frames: 0, @@ -348,7 +356,8 @@ fn process_thumbnail_studio( studio.active = Some(ActiveModelThumbnail { cache_key: job.cache_key, - model_path: job.model_path, + label, + render_image, root, meshes_ready: false, mesh_warmup_frames: 0, @@ -360,13 +369,127 @@ fn process_thumbnail_studio( }); } +fn spawn_thumbnail_job_content( + commands: &mut Commands, + mesh_storage: &mut Assets, + materials: &mut Assets, + asset_server: &AssetServer, + root: Entity, + source: &ThumbnailJobSource, +) -> bool { + match source { + ThumbnailJobSource::Model { model_path } => { + if uses_scene_root(model_path) { + commands.entity(root).insert(SceneRoot( + asset_server.load(model_scene_asset_path(model_path, 0)), + )); + } + if let Some(source) = source_for_extension(model_path) { + source.spawn_preview(commands, mesh_storage, materials, root, model_path) + } else { + uses_scene_root(model_path) + } + } + ThumbnailJobSource::MeshSubAsset { + model_path, + mesh_label, + material_label, + } => spawn_mesh_subasset_preview( + commands, + materials, + asset_server, + root, + model_path, + mesh_label, + material_label.as_deref(), + ), + ThumbnailJobSource::SourceMaterial { + model_path, + material_label, + } => spawn_material_sphere( + commands, + mesh_storage, + root, + asset_server.load(labeled_asset_path( + &asset_server_path(model_path), + material_label, + )), + ), + ThumbnailJobSource::MaterialAsset { material, .. } => { + let material = materials.add(material_from_desc(asset_server, material)); + spawn_material_sphere(commands, mesh_storage, root, material) + } + } +} + +fn spawn_mesh_subasset_preview( + commands: &mut Commands, + materials: &mut Assets, + asset_server: &AssetServer, + root: Entity, + model_path: &str, + mesh_label: &str, + material_label: Option<&str>, +) -> bool { + let source_path = asset_server_path(model_path); + let mesh: Handle = asset_server.load(labeled_asset_path(&source_path, mesh_label)); + let material = if let Some(material_label) = material_label { + asset_server.load(labeled_asset_path(&source_path, material_label)) + } else { + materials.add(StandardMaterial { + base_color: Color::srgb(0.72, 0.72, 0.75), + perceptual_roughness: 0.65, + ..default() + }) + }; + commands.entity(root).with_children(|parent| { + parent.spawn(( + RenderLayers::layer(THUMBNAIL_LAYER), + Mesh3d(mesh), + MeshMaterial3d(material), + Transform::default(), + )); + }); + true +} + +fn spawn_material_sphere( + commands: &mut Commands, + mesh_storage: &mut Assets, + root: Entity, + material: Handle, +) -> bool { + let mesh = mesh_storage.add(Sphere::new(0.65).mesh().uv(48, 24)); + commands.entity(root).with_children(|parent| { + parent.spawn(( + RenderLayers::layer(THUMBNAIL_LAYER), + Mesh3d(mesh), + MeshMaterial3d(material), + Transform::default(), + )); + }); + true +} + +fn labeled_asset_path(path: &str, label: &str) -> String { + format!("{path}#{label}") +} + +fn studio_render_image() -> Image { + Image::new_target_texture(THUMB_SIZE, THUMB_SIZE, TextureFormat::Rgba8UnormSrgb, None) +} + fn finish_active_thumbnail( commands: &mut Commands, studio: &mut ThumbnailStudio, cameras: &mut Query<&mut Camera, With>, + camera_targets: &mut Query<&mut RenderTarget, With>, active: ActiveModelThumbnail, ) { despawn_thumbnail_root(commands, active.root); + if let Ok(mut target) = camera_targets.get_mut(studio.camera) { + *target = RenderTarget::Image(studio.render_image.clone().into()); + } deactivate_studio_camera(cameras, studio.camera); studio.cooldown_frames = COOLDOWN_FRAMES; } @@ -376,12 +499,13 @@ fn fail_active_thumbnail( studio: &mut ThumbnailStudio, cache: &mut AssetThumbnailCache, cameras: &mut Query<&mut Camera, With>, + camera_targets: &mut Query<&mut RenderTarget, With>, active: ActiveModelThumbnail, reason: &str, retryable: bool, ) { cache.mark_studio_failed(&active.cache_key, reason, retryable); - finish_active_thumbnail(commands, studio, cameras, active); + finish_active_thumbnail(commands, studio, cameras, camera_targets, active); } fn apply_thumbnail_studio_layers( diff --git a/crates/editor/src/ui/asset_browser/panel.rs b/crates/editor/src/ui/asset_browser/panel.rs index 6f149de..4be2d5e 100644 --- a/crates/editor/src/ui/asset_browser/panel.rs +++ b/crates/editor/src/ui/asset_browser/panel.rs @@ -1,25 +1,41 @@ //! Asset browser dock panel: project tree, file grid/list, and asset details. +use std::collections::HashSet; use std::fs; -use std::path::Path; -use std::time::SystemTime; +use std::path::{Path, PathBuf}; +use std::time::{SystemTime, UNIX_EPOCH}; use bevy::prelude::*; use bevy_egui::egui; use bevy_inspector_egui::bevy_inspector::hierarchy::SelectedEntities; use egui_phosphor_icons::{icons, Icon}; +use shared::{ + ColorDesc, EditorAssetRef, MaterialAsset, MaterialDesc, MaterialParameter, + MaterialParameterValue, MaterialShaderKind, MaterialTextureBinding, ShaderPropertyDesc, + ShaderPropertyType, ShaderSchemaAsset, +}; use crate::assets::{ - spawn_asset_at, AssetSelection, EditorAsset, EditorAssetKind, EditorAssets, ASSETS_ROOT, - BUILTINS_FOLDER, + spawn_asset_at, spawn_subasset_at, AssetSelection, AssetSubAssetKind, EditorAsset, + EditorAssetKind, EditorAssets, ASSETS_ROOT, BUILTINS_FOLDER, }; use crate::scene_io::{SceneIo, SceneIoRequest}; -use super::state::{AssetBrowserUiState, AssetBrowserView, AssetKindFilter, AssetSort}; -use crate::asset_db::{MaterialImportPolicy, ModelHierarchyMode, ModelPlacementMode}; +use super::state::{ + AssetBrowserUiState, AssetBrowserView, AssetDeleteRequest, AssetKindFilter, AssetSort, + ImportSettingsDraft, MaterialAssetDraft, +}; +use crate::asset_db::{ + find_asset_by_path, find_asset_mut_by_path, save_registry, update_import_settings, + ImportSettings, MaterialImportPolicy, ModelHierarchyMode, ModelPlacementMode, +}; +use crate::assets::static_mesh::{ + load_static_mesh_manifest, material_id_from_label, part_id_from_label, + refresh_static_mesh_artifact, +}; use crate::assets::{ - draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, prefetch_folder_thumbnails, - AssetThumbnailCache, ThumbnailCacheSnapshot, + asset_cache_key, draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, + prefetch_folder_thumbnails, AssetThumbnailCache, ThumbnailCacheSnapshot, ThumbnailStudio, }; use crate::ui::helpers::{ apply_material_asset_to_selection, apply_texture_to_selection, asset_label, @@ -28,7 +44,8 @@ use crate::ui::theme::{panel_heading, ACCENT, BORDER, ELEVATED_BG, TEXT, TEXT_DI use crate::ui::widgets::{icon_button_small, tool_button}; const FOOTER_HEIGHT: f32 = 64.0; -const DETAILS_WIDTH: f32 = 240.0; +const TOOLBAR_HEIGHT: f32 = 40.0; +const DETAILS_WIDTH: f32 = 260.0; const DETAILS_MIN_PANEL_WIDTH: f32 = 760.0; const DETAILS_MIN_WIDTH: f32 = 200.0; const TREE_MIN_PANEL_WIDTH: f32 = 540.0; @@ -53,6 +70,18 @@ struct AssetRow { modified: Option, } +#[derive(Clone)] +struct EmbeddedAsset { + selection: AssetSelection, + label: String, + kind: AssetSubAssetKind, + detail: String, + texture_path: Option, + thumbnail_key: String, + mesh_label: Option, + material_label: Option, +} + fn fit_width(ui: &egui::Ui, min: f32, max: f32) -> f32 { let available = ui.available_width().max(1.0); available.min(max).max(min.min(available)) @@ -81,178 +110,200 @@ pub fn asset_browser_ui( ui: &mut egui::Ui, selected_entities: &SelectedEntities, ) { - ui.vertical(|ui| { - asset_toolbar(world, ui); + let available_width = ui.available_width().max(1.0); + let available_height = ui.available_height().max(1.0); + let body_height = (available_height - TOOLBAR_HEIGHT - FOOTER_HEIGHT - 4.0).max(40.0); - ui.separator(); + exact_region( + ui, + egui::vec2(available_width, available_height), + egui::Layout::top_down(egui::Align::Min), + |ui| { + exact_region( + ui, + egui::vec2(available_width, TOOLBAR_HEIGHT), + egui::Layout::left_to_right(egui::Align::Center), + |ui| { + asset_toolbar(world, ui); + }, + ); + ui.separator(); - let (current_folder, selected, folders, assets, cache_snapshot, state_snapshot) = { - let assets = world.resource::(); - let cache = world.resource::(); - let state = world.resource::(); - let folders: Vec = assets - .folders - .iter() - .map(|folder| FolderSnapshot { - path: folder.path.clone(), - name: folder.name.clone(), - parent: folder.parent.clone(), - }) - .collect(); - let asset_rows: Vec = assets - .assets - .iter() - .map(|asset| AssetRow { - selection: AssetSelection::from_asset(asset), - asset: asset.clone(), - file_size: asset.path.as_deref().and_then(file_size), - modified: asset.path.as_deref().and_then(modified_time), - }) - .collect(); - ( - assets.current_folder.clone(), - assets.selected.clone(), - folders, - asset_rows, - cache.snapshot(), - state_snapshot(state), - ) - }; + let (current_folder, selected, folders, assets, cache_snapshot, state_snapshot) = { + let assets = world.resource::(); + let cache = world.resource::(); + let state = world.resource::(); + let folders: Vec = assets + .folders + .iter() + .map(|folder| FolderSnapshot { + path: folder.path.clone(), + name: folder.name.clone(), + parent: folder.parent.clone(), + }) + .collect(); + let asset_rows: Vec = assets + .assets + .iter() + .map(|asset| AssetRow { + selection: AssetSelection::from_asset(asset), + asset: asset.clone(), + file_size: asset.path.as_deref().and_then(file_size), + modified: asset.path.as_deref().and_then(modified_time), + }) + .collect(); + ( + assets.current_folder.clone(), + assets.selected.clone(), + folders, + asset_rows, + cache.snapshot(), + state_snapshot(state), + ) + }; - prefetch_folder_thumbnails(world, ¤t_folder); + prefetch_folder_thumbnails(world, ¤t_folder); - let footer_reserved_height = FOOTER_HEIGHT + ui.spacing().item_spacing.y + 2.0; - let body_height = (ui.available_height() - footer_reserved_height).max(40.0); - let available_width = ui.available_width().max(1.0); - let show_tree = available_width >= TREE_MIN_PANEL_WIDTH; - let show_details = - state_snapshot.show_details && available_width >= DETAILS_MIN_PANEL_WIDTH; - let tree_width = if show_tree { - (available_width * 0.22).clamp(TREE_MIN_WIDTH, TREE_MAX_WIDTH) - } else { - 0.0 - }; - let details_width = if show_details { - DETAILS_WIDTH.min((available_width * 0.32).max(DETAILS_MIN_WIDTH)) - } else { - 0.0 - }; - let separator_budget = (show_tree as u8 as f32 + show_details as u8 as f32) * 10.0; - let content_width = (available_width - tree_width - details_width - separator_budget) - .max(MIN_CONTENT_WIDTH.min(available_width)); + let show_tree = available_width >= TREE_MIN_PANEL_WIDTH; + let show_details = + state_snapshot.show_details && available_width >= DETAILS_MIN_PANEL_WIDTH; + let tree_width = if show_tree { + (available_width * 0.22).clamp(TREE_MIN_WIDTH, TREE_MAX_WIDTH) + } else { + 0.0 + }; + let details_width = if show_details { + DETAILS_WIDTH.min((available_width * 0.28).max(DETAILS_MIN_WIDTH)) + } else { + 0.0 + }; + let separator_budget = (show_tree as u8 as f32 + show_details as u8 as f32) * 10.0; + let content_width = (available_width - tree_width - details_width - separator_budget) + .max(MIN_CONTENT_WIDTH.min(available_width)); + + exact_region( + ui, + egui::vec2(available_width, body_height), + egui::Layout::left_to_right(egui::Align::Min), + |ui| { + if show_tree { + exact_region( + ui, + egui::vec2(tree_width, body_height), + egui::Layout::top_down(egui::Align::Min), + |ui| { + ui.label(panel_heading("Project")); + egui::ScrollArea::vertical() + .id_salt("asset_folder_tree") + .max_height(ui.available_height()) + .auto_shrink([false, false]) + .show(ui, |ui| { + for root in [BUILTINS_FOLDER, ASSETS_ROOT] { + folder_tree_branch( + world, + ui, + &folders, + root, + 0, + ¤t_folder, + ); + } + }); + }, + ); + ui.separator(); + } - exact_region( - ui, - egui::vec2(available_width, body_height), - egui::Layout::left_to_right(egui::Align::Min), - |ui| { - if show_tree { exact_region( ui, - egui::vec2(tree_width, body_height), + egui::vec2(content_width, body_height), egui::Layout::top_down(egui::Align::Min), |ui| { - ui.label(panel_heading("Project")); + content_header(world, ui, ¤t_folder, &folders, &assets); egui::ScrollArea::vertical() - .id_salt("asset_folder_tree") + .id_salt("asset_content_area") .max_height(ui.available_height()) .auto_shrink([false, false]) .show(ui, |ui| { - for root in [BUILTINS_FOLDER, ASSETS_ROOT] { - folder_tree_branch( + let child_folders = child_folders_for_content( + &folders, + ¤t_folder, + &state_snapshot.search, + ); + let visible_assets = + visible_assets(&assets, ¤t_folder, &state_snapshot); + prefetch_asset_row_thumbnails(world, &visible_assets); + + if child_folders.is_empty() && visible_assets.is_empty() { + empty_content(ui, &state_snapshot.search); + } else if state_snapshot.view == AssetBrowserView::Grid { + let thumbnail_size = state_snapshot + .thumbnail_size + .min((content_width - 26.0).max(48.0)); + asset_grid( world, ui, - &folders, - root, - 0, - ¤t_folder, + selected_entities, + &selected, + &child_folders, + &visible_assets, + &cache_snapshot, + thumbnail_size, + ); + } else { + asset_list( + world, + ui, + selected_entities, + &selected, + &child_folders, + &visible_assets, + &cache_snapshot, ); } }); }, ); - ui.separator(); - } - exact_region( - ui, - egui::vec2(content_width, body_height), - egui::Layout::top_down(egui::Align::Min), - |ui| { - content_header(world, ui, ¤t_folder, &folders, &assets); - egui::ScrollArea::vertical() - .id_salt("asset_content_area") - .max_height(ui.available_height()) - .auto_shrink([false, false]) - .show(ui, |ui| { - let child_folders = child_folders_for_content( - &folders, - ¤t_folder, - &state_snapshot.search, - ); - let visible_assets = - visible_assets(&assets, ¤t_folder, &state_snapshot); + if show_details { + ui.separator(); + exact_region( + ui, + egui::vec2(details_width, body_height), + egui::Layout::top_down(egui::Align::Min), + |ui| { + egui::ScrollArea::vertical() + .id_salt("asset_details_area") + .max_height(ui.available_height()) + .auto_shrink([false, false]) + .show(ui, |ui| { + details_panel(world, ui, selected_entities); + }); + }, + ); + } + }, + ); - if child_folders.is_empty() && visible_assets.is_empty() { - empty_content(ui, &state_snapshot.search); - } else if state_snapshot.view == AssetBrowserView::Grid { - let thumbnail_size = state_snapshot - .thumbnail_size - .min((content_width - 26.0).max(48.0)); - asset_grid( - world, - ui, - selected_entities, - &selected, - &child_folders, - &visible_assets, - &cache_snapshot, - thumbnail_size, - ); - } else { - asset_list( - world, - ui, - selected_entities, - &selected, - &child_folders, - &visible_assets, - ); - } - }); - }, - ); + ui.separator(); + exact_region( + ui, + egui::vec2(available_width, FOOTER_HEIGHT), + egui::Layout::top_down(egui::Align::Min), + |ui| { + egui::Frame::new() + .fill(ELEVATED_BG.linear_multiply(0.5)) + .inner_margin(egui::Margin::symmetric(8, 6)) + .show(ui, |ui| { + ui.set_max_height(FOOTER_HEIGHT - 12.0); + selection_footer(world, ui, selected_entities); + }); + }, + ); + }, + ); - if show_details { - ui.separator(); - exact_region( - ui, - egui::vec2(details_width, body_height), - egui::Layout::top_down(egui::Align::Min), - |ui| { - details_panel(world, ui, selected_entities); - }, - ); - } - }, - ); - - ui.separator(); - exact_region( - ui, - egui::vec2(available_width, FOOTER_HEIGHT), - egui::Layout::top_down(egui::Align::Min), - |ui| { - egui::Frame::new() - .fill(ELEVATED_BG.linear_multiply(0.5)) - .inner_margin(egui::Margin::symmetric(8, 6)) - .show(ui, |ui| { - ui.set_max_height(FOOTER_HEIGHT - 12.0); - selection_footer(world, ui, selected_entities); - }); - }, - ); - }); + draw_delete_modal(world, ui.ctx()); } #[derive(Clone)] @@ -278,8 +329,210 @@ fn state_snapshot(state: &AssetBrowserUiState) -> AssetBrowserStateSnapshot { } } +fn embedded_assets_for_asset(world: &World, asset: &EditorAsset) -> Vec { + if !matches!(asset.kind, EditorAssetKind::Model) { + return Vec::new(); + } + let Some(parent_path) = asset.path.as_ref() else { + return Vec::new(); + }; + let Some(record) = world + .get_resource::() + .and_then(|registry| find_asset_by_path(registry, parent_path)) + else { + return Vec::new(); + }; + let Some(manifest_path) = record.import_settings.static_mesh_manifest_path.as_deref() else { + return Vec::new(); + }; + let Ok(manifest) = load_static_mesh_manifest(manifest_path) else { + return Vec::new(); + }; + + let mut embedded = Vec::new(); + for part in &manifest.parts { + let mesh_id = part_effective_id(part); + let material_label = part.material_label.clone(); + embedded.push(EmbeddedAsset { + selection: AssetSelection::SubAsset { + parent_path: parent_path.clone(), + sub_asset_id: mesh_id, + label: part.name.clone(), + kind: AssetSubAssetKind::Mesh, + source_path: None, + }, + label: part.name.clone(), + kind: AssetSubAssetKind::Mesh, + detail: part + .source_mesh + .clone() + .unwrap_or_else(|| part.mesh_label.clone()), + texture_path: None, + thumbnail_key: subasset_thumbnail_key( + parent_path, + AssetSubAssetKind::Mesh, + &part_effective_id(part), + ), + mesh_label: Some(part.mesh_label.clone()), + material_label, + }); + } + + let mut material_ids = HashSet::new(); + for part in &manifest.parts { + let Some(material_id) = part_effective_material_id(part) else { + continue; + }; + if !material_ids.insert(material_id.clone()) { + continue; + } + let label = if part.material_slot_name.trim().is_empty() { + part.material_label + .clone() + .unwrap_or_else(|| "Source Material".to_string()) + } else { + part.material_slot_name.clone() + }; + embedded.push(EmbeddedAsset { + selection: AssetSelection::SubAsset { + parent_path: parent_path.clone(), + sub_asset_id: material_id, + label: label.clone(), + kind: AssetSubAssetKind::Material, + source_path: None, + }, + label, + kind: AssetSubAssetKind::Material, + detail: "Embedded source material".to_string(), + texture_path: None, + thumbnail_key: subasset_thumbnail_key( + parent_path, + AssetSubAssetKind::Material, + &part_effective_material_id(part).unwrap_or_else(|| { + material_id_from_label( + part.material_label + .as_deref() + .unwrap_or(&part.material_slot_name), + ) + }), + ), + mesh_label: None, + material_label: part.material_label.clone(), + }); + } + + let mut texture_paths = HashSet::new(); + for dependency in &manifest.source.dependencies { + if !is_texture_path(dependency) || !texture_paths.insert(dependency.clone()) { + continue; + } + let label = Path::new(dependency) + .file_stem() + .and_then(|stem| stem.to_str()) + .unwrap_or(dependency) + .to_string(); + embedded.push(EmbeddedAsset { + selection: AssetSelection::SubAsset { + parent_path: parent_path.clone(), + sub_asset_id: format!("texture:{}", stable_subasset_slug(dependency)), + label: label.clone(), + kind: AssetSubAssetKind::Texture, + source_path: Some(dependency.clone()), + }, + label, + kind: AssetSubAssetKind::Texture, + detail: dependency.clone(), + texture_path: Some(dependency.clone()), + thumbnail_key: subasset_thumbnail_key( + parent_path, + AssetSubAssetKind::Texture, + &stable_subasset_slug(dependency), + ), + mesh_label: None, + material_label: None, + }); + } + + embedded +} + +fn embedded_asset_for_selection( + world: &World, + selection: &AssetSelection, +) -> Option { + let AssetSelection::SubAsset { parent_path, .. } = selection else { + return None; + }; + let asset = world + .resource::() + .assets + .iter() + .find(|asset| asset.path.as_deref() == Some(parent_path.as_str()))? + .clone(); + embedded_assets_for_asset(world, &asset) + .into_iter() + .find(|embedded| &embedded.selection == selection) +} + +fn has_embedded_assets(world: &World, asset: &EditorAsset) -> bool { + !embedded_assets_for_asset(world, asset).is_empty() +} + +fn part_effective_id(part: &crate::assets::static_mesh::StaticMeshPart) -> String { + if part.id.trim().is_empty() { + part_id_from_label(&part.mesh_label) + } else { + part.id.clone() + } +} + +fn part_effective_material_id(part: &crate::assets::static_mesh::StaticMeshPart) -> Option { + part.material_id.clone().or_else(|| { + part.material_label + .as_ref() + .map(|label| material_id_from_label(label)) + }) +} + +fn is_texture_path(path: &str) -> bool { + Path::new(path) + .extension() + .and_then(|extension| extension.to_str()) + .is_some_and(|extension| { + matches!( + extension.to_ascii_lowercase().as_str(), + "png" | "jpg" | "jpeg" | "webp" | "ktx2" + ) + }) +} + +fn stable_subasset_slug(label: &str) -> String { + let mut slug = String::new(); + for ch in label.chars() { + if ch.is_ascii_alphanumeric() { + slug.push(ch.to_ascii_lowercase()); + } else if !slug.ends_with('_') { + slug.push('_'); + } + } + slug.trim_matches('_').to_string() +} + +fn subasset_thumbnail_key( + parent_path: &str, + kind: AssetSubAssetKind, + sub_asset_id: &str, +) -> String { + format!( + "{}#{}:{}", + parent_path, + subasset_kind_label(kind).to_ascii_lowercase(), + sub_asset_id + ) +} + fn asset_toolbar(world: &mut World, ui: &mut egui::Ui) { - ui.horizontal_wrapped(|ui| { + ui.horizontal(|ui| { if icon_button_small(ui, icons::ARROWS_CLOCKWISE, "Refresh").clicked() { world.resource_mut::().refresh(); invalidate_on_catalog_refresh(world); @@ -351,10 +604,13 @@ fn asset_toolbar(world: &mut World, ui: &mut egui::Ui) { { state.view = AssetBrowserView::List; } - ui.add_sized( - [fit_width(ui, 72.0, 120.0), 20.0], - egui::Slider::new(&mut state.thumbnail_size, 48.0..=112.0).show_value(false), - ); + if ui.available_width() > 180.0 { + ui.label(icon_text(icons::IMAGE_SQUARE, 13.0).color(TEXT_DIM)); + ui.add_sized( + [96.0, 20.0], + egui::Slider::new(&mut state.thumbnail_size, 48.0..=112.0).show_value(false), + ); + } ui.checkbox(&mut state.show_details, "Details"); }); } @@ -385,6 +641,47 @@ fn content_header( ); } +fn prefetch_asset_row_thumbnails(world: &mut World, rows: &[AssetRow]) { + let requests: Vec<(String, String, EditorAssetKind)> = rows + .iter() + .filter(|row| { + matches!( + row.asset.kind, + EditorAssetKind::Texture | EditorAssetKind::Model | EditorAssetKind::Material + ) + }) + .filter_map(|row| { + Some(( + asset_cache_key(&row.asset), + row.asset.path.clone()?, + row.asset.kind.clone(), + )) + }) + .collect(); + if requests.is_empty() { + return; + } + let asset_server = world.resource::().clone(); + world.resource_scope(|world, mut cache: Mut| { + world.resource_scope(|_world, mut studio: Mut| { + for (key, path, kind) in &requests { + match kind { + EditorAssetKind::Texture => { + cache.request_texture(key.clone(), path.clone(), &asset_server); + } + EditorAssetKind::Model => { + cache.request_model(key.clone(), path.clone(), &asset_server, &mut studio); + } + EditorAssetKind::Material => { + cache.request_material_asset(key.clone(), path.clone(), &mut studio); + } + _ => {} + } + } + }); + }); +} + fn breadcrumb(world: &mut World, ui: &mut egui::Ui, current_folder: &str) { let mut parts = Vec::new(); let mut cursor = Some(current_folder.to_string()); @@ -413,7 +710,36 @@ fn breadcrumb(world: &mut World, ui: &mut egui::Ui, current_folder: &str) { } fn selection_footer(world: &mut World, ui: &mut egui::Ui, selected_entities: &SelectedEntities) { - if let Some(asset) = world.resource::().selected_asset().cloned() { + let selection = world.resource::().selected.clone(); + if let Some(AssetSelection::SubAsset { label, kind, .. }) = selection.clone() { + ui.add( + egui::Label::new(format!( + "Selected: {}: {}", + subasset_kind_label(kind), + label + )) + .truncate(), + ); + ui.horizontal_wrapped(|ui| match kind { + AssetSubAssetKind::Mesh => { + if ui.button("Place At Origin").clicked() { + if let Some(selection) = selection.as_ref() { + spawn_subasset_at(world, selection, Vec3::ZERO); + } + } + } + AssetSubAssetKind::Texture => { + if ui.button("Apply Texture To Selection").clicked() { + if let Some(asset) = texture_asset_from_subasset_selection(world, &selection) { + apply_texture_to_selection(world, &asset, selected_entities); + } + } + } + AssetSubAssetKind::Material => { + ui.small(egui::RichText::new("Embedded source material").color(TEXT_DIM)); + } + }); + } else if let Some(asset) = world.resource::().selected_asset().cloned() { ui.add(egui::Label::new(format!("Selected: {}", asset_label(&asset))).truncate()); ui.horizontal_wrapped(|ui| { if matches!(asset.kind, EditorAssetKind::Texture) @@ -580,52 +906,441 @@ fn asset_grid( cache_snapshot: &ThumbnailCacheSnapshot, thumbnail_size: f32, ) { - ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing = egui::vec2(6.0, 6.0); - for folder in folders { - let response = draw_folder_cell(ui, folder, thumbnail_size); - if response.double_clicked() || response.clicked() { - world.resource_mut::().current_folder = folder.path.clone(); - } - } - for row in assets { - let selection = &row.selection; - let asset = &row.asset; - let is_selected = selected.as_ref() == Some(selection); - let failed = cache_snapshot.is_failed(asset); - let response = draw_asset_cell_with( - ui, - asset, - cache_snapshot.texture_for(asset), - cache_snapshot.is_pending(asset), - failed.then_some("failed"), - is_selected, - thumbnail_size, - ); - if failed && response.double_clicked() { - let key = crate::assets::asset_cache_key(asset); - world - .resource_mut::() - .retry(&key); - } + let thumb_size = thumbnail_size.clamp(48.0, 112.0); + let cell_width = thumb_size + 20.0; + let gap = 6.0; + let columns = ((ui.available_width() + gap) / (cell_width + gap)) + .floor() + .max(1.0) as usize; - if response.clicked() { - world - .resource_mut::() - .select(selection.clone()); + for folder_row in folders.chunks(columns) { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = egui::vec2(gap, gap); + for folder in folder_row { + let response = draw_folder_cell(ui, folder, thumbnail_size); + if response.double_clicked() || response.clicked() { + world.resource_mut::().current_folder = folder.path.clone(); + } } - if response.drag_started() { - world - .resource_mut::() - .start_drag(selection.clone()); + }); + } + + for asset_row in assets.chunks(columns) { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = egui::vec2(gap, gap); + for row in asset_row { + draw_asset_grid_item( + world, + ui, + selected_entities, + selected, + row, + cache_snapshot, + thumbnail_size, + ); + } + }); + + for row in asset_row { + if row.asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }) { + embedded_asset_shelf( + world, + ui, + selected_entities, + selected, + &row.asset, + cache_snapshot, + thumbnail_size, + ); } - response.context_menu(|ui| { - asset_context_menu(world, ui, asset, selected_entities); - }); } + } +} + +fn draw_asset_grid_item( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + selected: &Option, + row: &AssetRow, + cache_snapshot: &ThumbnailCacheSnapshot, + thumbnail_size: f32, +) { + let selection = &row.selection; + let asset = &row.asset; + let is_selected = selected.as_ref() == Some(selection); + let failed = cache_snapshot.is_failed(asset); + let has_children = has_embedded_assets(world, asset); + let response = draw_asset_cell_with( + ui, + asset, + cache_snapshot.texture_for(asset), + cache_snapshot.is_pending(asset), + failed.then_some("failed"), + is_selected, + thumbnail_size, + ); + + if has_children { + let expanded = asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }); + let icon = if expanded { + icons::CARET_DOWN + } else { + icons::CARET_RIGHT + }; + let button_rect = egui::Rect::from_min_size( + response.rect.min + egui::vec2(6.0, 6.0), + egui::vec2(18.0, 18.0), + ); + let expand_response = ui.interact( + button_rect, + egui::Id::new(( + "asset_expand", + asset.path.as_deref().unwrap_or(&asset.label), + )), + egui::Sense::click(), + ); + ui.painter().rect( + button_rect, + 3.0, + if expand_response.hovered() { + ELEVATED_BG + } else { + WIDGET_BG.linear_multiply(1.15) + }, + egui::Stroke::new(1.0, BORDER), + egui::StrokeKind::Inside, + ); + ui.painter().text( + button_rect.center(), + egui::Align2::CENTER_CENTER, + icon.as_str(), + egui::FontId::new(11.0, egui::FontFamily::Name(PHOSPHOR.into())), + TEXT, + ); + if expand_response.clicked() { + if let Some(path) = asset.path.as_ref() { + toggle_asset_expanded(world, path); + } + } + } + + if failed && response.double_clicked() { + let key = crate::assets::asset_cache_key(asset); + world + .resource_mut::() + .retry(&key); + } + + if response.clicked() { + world + .resource_mut::() + .select(selection.clone()); + } + if response.drag_started() { + world + .resource_mut::() + .start_drag(selection.clone()); + } + response.context_menu(|ui| { + asset_context_menu(world, ui, asset, selected_entities); }); } +fn embedded_asset_shelf( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + selected: &Option, + parent_asset: &EditorAsset, + cache_snapshot: &ThumbnailCacheSnapshot, + thumbnail_size: f32, +) { + let embedded = embedded_assets_for_asset(world, parent_asset); + if embedded.is_empty() { + return; + } + prefetch_embedded_thumbnails(world, parent_asset, &embedded); + let parent_texture = cache_snapshot.texture_for(parent_asset); + + ui.add_space(4.0); + egui::Frame::new() + .fill(WIDGET_BG.linear_multiply(0.72)) + .stroke(egui::Stroke::new(1.0, BORDER.linear_multiply(0.85))) + .corner_radius(egui::CornerRadius::same(4)) + .inner_margin(egui::Margin::symmetric(8, 5)) + .show(ui, |ui| { + ui.set_min_width(ui.available_width()); + ui.horizontal(|ui| { + ui.label(icon_text(icons::TREE_STRUCTURE, 13.0).color(TEXT_DIM)); + ui.small( + egui::RichText::new(format!("{} contents", parent_asset.label)).color(TEXT_DIM), + ); + }); + egui::ScrollArea::horizontal() + .id_salt(format!( + "embedded_shelf_{}", + parent_asset.path.as_deref().unwrap_or(&parent_asset.label) + )) + .max_height(thumbnail_size.clamp(48.0, 88.0) + 56.0) + .auto_shrink([false, true]) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = egui::vec2(7.0, 5.0); + for embedded_asset in &embedded { + draw_embedded_asset_cell( + world, + ui, + selected_entities, + selected, + embedded_asset, + cache_snapshot, + parent_texture, + thumbnail_size, + ); + } + }); + }); + }); + ui.add_space(4.0); +} + +fn prefetch_embedded_thumbnails( + world: &mut World, + parent_asset: &EditorAsset, + embedded: &[EmbeddedAsset], +) { + let Some(parent_path) = parent_asset.path.clone() else { + return; + }; + let asset_server = world.resource::().clone(); + let requests: Vec = embedded.to_vec(); + world.resource_scope(|world, mut cache: Mut| { + world.resource_scope(|_world, mut studio: Mut| { + for embedded_asset in &requests { + match embedded_asset.kind { + AssetSubAssetKind::Mesh => { + let Some(mesh_label) = embedded_asset.mesh_label.clone() else { + continue; + }; + cache.request_mesh_subasset( + embedded_asset.thumbnail_key.clone(), + parent_path.clone(), + mesh_label, + embedded_asset.material_label.clone(), + &mut studio, + ); + } + AssetSubAssetKind::Material => { + let Some(material_label) = embedded_asset.material_label.clone() else { + continue; + }; + cache.request_source_material( + embedded_asset.thumbnail_key.clone(), + parent_path.clone(), + material_label, + &mut studio, + ); + } + AssetSubAssetKind::Texture => { + let Some(texture_path) = embedded_asset.texture_path.clone() else { + continue; + }; + cache.request_texture( + embedded_asset.thumbnail_key.clone(), + texture_path, + &asset_server, + ); + } + } + } + }); + }); +} + +fn draw_embedded_asset_cell( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + selected: &Option, + embedded: &EmbeddedAsset, + cache_snapshot: &ThumbnailCacheSnapshot, + _parent_texture: Option, + thumbnail_size: f32, +) { + let selected = selected.as_ref() == Some(&embedded.selection); + let texture_id = cache_snapshot + .texture_for_key(&embedded.thumbnail_key) + .or_else(|| { + embedded + .texture_path + .as_deref() + .and_then(|path| thumbnail_for_path(world, cache_snapshot, path)) + }); + let pending = cache_snapshot.is_pending_key(&embedded.thumbnail_key); + let failed = cache_snapshot.is_failed_key(&embedded.thumbnail_key); + let thumb_size = thumbnail_size.clamp(44.0, 64.0); + let cell_size = egui::vec2(104.0, thumb_size + 50.0); + let (rect, response) = ui.allocate_exact_size(cell_size, egui::Sense::click_and_drag()); + let fill = if selected { + crate::ui::theme::SELECTION_BG_MUTED + } else if response.hovered() { + WIDGET_BG.linear_multiply(1.2) + } else { + WIDGET_BG + }; + ui.painter().rect( + rect, + 4.0, + fill, + egui::Stroke::new(1.0, if selected { ACCENT } else { BORDER }), + egui::StrokeKind::Inside, + ); + let thumb_rect = egui::Rect::from_min_size( + egui::pos2(rect.center().x - thumb_size * 0.5, rect.min.y + 8.0), + egui::vec2(thumb_size, thumb_size), + ); + if let Some(texture_id) = texture_id { + ui.painter().image( + texture_id, + thumb_rect, + egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), + egui::Color32::WHITE, + ); + } else { + ui.painter().rect( + thumb_rect, + 4.0, + ELEVATED_BG.linear_multiply(0.6), + egui::Stroke::new(1.0, BORDER), + egui::StrokeKind::Inside, + ); + let icon = if pending { + icons::CIRCLE_NOTCH + } else if failed { + icons::WARNING_CIRCLE + } else { + subasset_icon(embedded.kind) + }; + ui.painter().text( + thumb_rect.center(), + egui::Align2::CENTER_CENTER, + icon.as_str(), + egui::FontId::new(24.0, egui::FontFamily::Name(PHOSPHOR.into())), + TEXT, + ); + } + + let label = compact_subasset_label(embedded); + let detail = compact_text(&embedded.detail, 18); + let label_pos = egui::pos2(rect.center().x, thumb_rect.max.y + 7.0); + ui.painter().text( + label_pos, + egui::Align2::CENTER_TOP, + label.as_str(), + egui::FontId::new(12.0, egui::FontFamily::Proportional), + if selected { + crate::ui::theme::TEXT_SELECTED + } else { + TEXT + }, + ); + ui.painter().text( + label_pos + egui::vec2(0.0, 17.0), + egui::Align2::CENTER_TOP, + format!("{} | {}", subasset_kind_label(embedded.kind), detail), + egui::FontId::new(10.0, egui::FontFamily::Proportional), + TEXT_DIM, + ); + + if response.clicked() { + world + .resource_mut::() + .select(embedded.selection.clone()); + } + if response.drag_started() { + world + .resource_mut::() + .start_drag(embedded.selection.clone()); + } + response.context_menu(|ui| { + subasset_context_menu(world, ui, embedded, selected_entities); + }); +} + +fn compact_subasset_label(embedded: &EmbeddedAsset) -> String { + let base = match embedded.kind { + AssetSubAssetKind::Mesh => embedded + .label + .split_once(" / ") + .map(|(head, _)| head) + .unwrap_or(&embedded.label), + AssetSubAssetKind::Material | AssetSubAssetKind::Texture => embedded.label.as_str(), + }; + compact_text(base, 16) +} + +fn compact_text(text: &str, max_chars: usize) -> String { + let mut chars = text.chars(); + let mut compact = String::new(); + for _ in 0..max_chars { + let Some(ch) = chars.next() else { + return text.to_string(); + }; + compact.push(ch); + } + if chars.next().is_some() { + compact.push_str("..."); + } + compact +} + +fn thumbnail_for_path( + world: &World, + cache_snapshot: &ThumbnailCacheSnapshot, + path: &str, +) -> Option { + let assets = world.resource::(); + assets + .assets + .iter() + .find(|asset| asset.path.as_deref() == Some(path)) + .and_then(|asset| cache_snapshot.texture_for(asset)) +} + +fn toggle_asset_expanded(world: &mut World, path: &str) { + let mut state = world.resource_mut::(); + if !state.expanded_assets.insert(path.to_string()) { + state.expanded_assets.remove(path); + } +} + +fn subasset_icon(kind: AssetSubAssetKind) -> Icon { + match kind { + AssetSubAssetKind::Mesh => icons::CUBE, + AssetSubAssetKind::Material => icons::PALETTE, + AssetSubAssetKind::Texture => icons::IMAGE, + } +} + +fn subasset_kind_label(kind: AssetSubAssetKind) -> &'static str { + match kind { + AssetSubAssetKind::Mesh => "Mesh", + AssetSubAssetKind::Material => "Material", + AssetSubAssetKind::Texture => "Texture", + } +} + fn asset_list( world: &mut World, ui: &mut egui::Ui, @@ -633,77 +1348,166 @@ fn asset_list( selected: &Option, folders: &[FolderSnapshot], assets: &[AssetRow], + cache_snapshot: &ThumbnailCacheSnapshot, ) { if ui.available_width() < COMPACT_LIST_WIDTH { - compact_asset_list(world, ui, selected_entities, selected, folders, assets); + compact_asset_list( + world, + ui, + selected_entities, + selected, + folders, + assets, + cache_snapshot, + ); return; } - egui::Grid::new("asset_browser_list") - .num_columns(5) - .striped(true) - .spacing(egui::vec2(16.0, 4.0)) - .show(ui, |ui| { - ui.strong("Name"); - ui.strong("Type"); - ui.strong("Size"); - ui.strong("Modified"); - ui.strong("Path"); - ui.end_row(); + ui.horizontal(|ui| { + ui.add_sized([236.0, 18.0], egui::Label::new("Name")); + ui.add_sized([80.0, 18.0], egui::Label::new("Type")); + ui.add_sized([72.0, 18.0], egui::Label::new("Size")); + ui.add_sized([88.0, 18.0], egui::Label::new("Modified")); + ui.label("Path"); + }); - for folder in folders { - let mut clicked = false; + for folder in folders { + let response = egui::Frame::new() + .fill(WIDGET_BG.linear_multiply(0.9)) + .inner_margin(egui::Margin::symmetric(6, 3)) + .show(ui, |ui| { ui.horizontal(|ui| { + ui.add_sized([20.0, 20.0], egui::Label::new("")); ui.label(icon_text(icons::FOLDER, 14.0).color(TEXT_DIM)); - clicked = ui.selectable_label(false, folder.name.as_str()).clicked(); - }); - ui.label("Folder"); - ui.label("-"); - ui.label("-"); - ui.label(folder.path.as_str()); - ui.end_row(); - if clicked { - world.resource_mut::().current_folder = folder.path.clone(); - } - } + ui.add_sized( + [196.0, 20.0], + egui::Button::selectable(false, folder.name.as_str()), + ) + }) + .inner + }) + .inner; + if response.clicked() { + world.resource_mut::().current_folder = folder.path.clone(); + } + } - for row in assets { - let is_selected = selected.as_ref() == Some(&row.selection); - let mut response = None; - ui.horizontal(|ui| { - ui.label(icon_text(kind_icon(&row.asset.kind), 14.0).color(TEXT_DIM)); - response = Some(ui.selectable_label(is_selected, row.asset.label.as_str())); - }); - let response = response.expect("asset list response"); - ui.label(kind_label(&row.asset.kind)); - ui.label( - row.file_size - .map(format_bytes) - .unwrap_or_else(|| "-".to_string()), - ); - ui.label( - row.modified - .map(format_modified) - .unwrap_or_else(|| "-".to_string()), - ); - ui.label(row.asset.path.as_deref().unwrap_or("Built-in")); - ui.end_row(); + for row in assets { + draw_asset_list_item(world, ui, selected_entities, selected, row); + if row.asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }) { + embedded_asset_shelf( + world, + ui, + selected_entities, + selected, + &row.asset, + cache_snapshot, + 56.0, + ); + } + } +} - if response.clicked() { - world - .resource_mut::() - .select(row.selection.clone()); +fn draw_asset_list_item( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + selected: &Option, + row: &AssetRow, +) { + let has_children = has_embedded_assets(world, &row.asset); + let expanded = row.asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }); + let is_selected = selected.as_ref() == Some(&row.selection); + let response = egui::Frame::new() + .fill(if is_selected { + crate::ui::theme::SELECTION_BG_MUTED + } else { + WIDGET_BG.linear_multiply(0.9) + }) + .stroke(egui::Stroke::new( + 1.0, + if is_selected { ACCENT } else { BORDER }, + )) + .inner_margin(egui::Margin::symmetric(6, 3)) + .show(ui, |ui| { + ui.horizontal(|ui| { + if has_children { + let icon = if expanded { + icons::CARET_DOWN + } else { + icons::CARET_RIGHT + }; + if ui + .add_sized( + [20.0, 20.0], + egui::Button::new(icon_text(icon, 12.0)).frame(false), + ) + .clicked() + { + if let Some(path) = row.asset.path.as_ref() { + toggle_asset_expanded(world, path); + } + } + } else { + ui.add_sized([20.0, 20.0], egui::Label::new("")); } - if response.drag_started() { - world - .resource_mut::() - .start_drag(row.selection.clone()); - } - response.context_menu(|ui| { + ui.label(icon_text(kind_icon(&row.asset.kind), 14.0).color(TEXT_DIM)); + let response = ui.add_sized( + [196.0, 20.0], + egui::Button::selectable(is_selected, row.asset.label.as_str()), + ); + ui.add_sized([80.0, 20.0], egui::Label::new(kind_label(&row.asset.kind))); + ui.add_sized( + [72.0, 20.0], + egui::Label::new( + row.file_size + .map(format_bytes) + .unwrap_or_else(|| "-".to_string()), + ), + ); + ui.add_sized( + [88.0, 20.0], + egui::Label::new( + row.modified + .map(format_modified) + .unwrap_or_else(|| "-".to_string()), + ), + ); + ui.add( + egui::Label::new(row.asset.path.as_deref().unwrap_or("Built-in")).truncate(), + ); + ui.menu_button(icon_text(icons::DOTS_THREE_VERTICAL, 13.0), |ui| { asset_context_menu(world, ui, &row.asset, selected_entities); }); - } - }); + response + }) + .inner + }) + .inner; + + if response.clicked() { + world + .resource_mut::() + .select(row.selection.clone()); + } + if response.drag_started() { + world + .resource_mut::() + .start_drag(row.selection.clone()); + } + response.context_menu(|ui| { + asset_context_menu(world, ui, &row.asset, selected_entities); + }); } fn compact_asset_list( @@ -713,6 +1517,7 @@ fn compact_asset_list( selected: &Option, folders: &[FolderSnapshot], assets: &[AssetRow], + cache_snapshot: &ThumbnailCacheSnapshot, ) { for folder in folders { let mut clicked = false; @@ -734,13 +1539,41 @@ fn compact_asset_list( for row in assets { let is_selected = selected.as_ref() == Some(&row.selection); + let has_children = has_embedded_assets(world, &row.asset); + let expanded = row.asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }); let mut response = None; ui.horizontal(|ui| { + if has_children { + let icon = if expanded { + icons::CARET_DOWN + } else { + icons::CARET_RIGHT + }; + if ui + .add_sized( + [20.0, 20.0], + egui::Button::new(icon_text(icon, 12.0)).frame(false), + ) + .clicked() + { + if let Some(path) = row.asset.path.as_ref() { + toggle_asset_expanded(world, path); + } + } + } ui.label(icon_text(kind_icon(&row.asset.kind), 14.0).color(TEXT_DIM)); response = Some(ui.add_sized( [fit_width(ui, 120.0, f32::INFINITY), 22.0], egui::Button::selectable(is_selected, row.asset.label.as_str()), )); + ui.menu_button(icon_text(icons::DOTS_THREE_VERTICAL, 13.0), |ui| { + asset_context_menu(world, ui, &row.asset, selected_entities); + }); }); let response = response.expect("compact asset list response"); let size = row @@ -780,6 +1613,17 @@ fn compact_asset_list( response.context_menu(|ui| { asset_context_menu(world, ui, &row.asset, selected_entities); }); + if expanded { + embedded_asset_shelf( + world, + ui, + selected_entities, + selected, + &row.asset, + cache_snapshot, + 56.0, + ); + } } } @@ -826,169 +1670,173 @@ fn draw_folder_cell( fn details_panel(world: &mut World, ui: &mut egui::Ui, selected_entities: &SelectedEntities) { ui.label(panel_heading("Details")); ui.separator(); - if let Some(asset) = world.resource::().selected_asset().cloned() { - ui.horizontal_wrapped(|ui| { - ui.label( - egui::RichText::new(kind_icon(&asset.kind).as_str()) - .font(egui::FontId::new( - 30.0, - egui::FontFamily::Name("phosphor-regular".into()), - )) - .color(ACCENT), - ); - ui.vertical(|ui| { - ui.strong(asset.label.as_str()); - ui.small(egui::RichText::new(kind_label(&asset.kind)).color(TEXT_DIM)); - }); - }); - ui.add_space(8.0); - detail_row(ui, "Path", asset.path.as_deref().unwrap_or("Built-in")); - detail_row(ui, "Folder", asset.folder_path.as_str()); - if let Some(path) = asset.path.as_deref() { - if let Some(record) = world - .get_resource::() - .and_then(|registry| crate::asset_db::find_asset_by_path(registry, path)) - { - detail_row(ui, "Asset ID", &record.id.as_string()); + let selection = world.resource::().selected.clone(); + if let Some(selection @ AssetSelection::SubAsset { .. }) = selection { + subasset_details_panel(world, ui, selected_entities, &selection); + } else if let Some(asset) = world.resource::().selected_asset().cloned() { + top_level_asset_details_panel(world, ui, selected_entities, &asset); + } else { + ui.label(egui::RichText::new("No asset selected").color(TEXT_DIM)); + } +} + +fn top_level_asset_details_panel( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + asset: &EditorAsset, +) { + asset_details_header(ui, asset); + ui.add_space(8.0); + detail_row(ui, "Path", asset.path.as_deref().unwrap_or("Built-in")); + detail_row(ui, "Folder", asset.folder_path.as_str()); + + if let Some(path) = asset.path.as_deref() { + if let Some(record) = world + .get_resource::() + .and_then(|registry| find_asset_by_path(registry, path)) + { + detail_row(ui, "Asset ID", &record.id.as_string()); + if matches!(asset.kind, EditorAssetKind::Model) { ui.separator(); ui.label(panel_heading("Import Settings")); - let mut settings = record.import_settings.clone(); - let mut dirty = false; - if ui - .add(egui::Slider::new(&mut settings.scale, 0.01..=10.0).text("Scale")) - .changed() + import_settings_editor(world, ui, asset, path, &record.import_settings); + if let Some(manifest) = record.import_settings.static_mesh_manifest_path.as_deref() { - dirty = true; - } - if ui - .checkbox(&mut settings.generate_collider, "Generate collider") - .changed() - { - dirty = true; - } - if ui.checkbox(&mut settings.lod0_only, "LOD0 only").changed() { - dirty = true; - } - if matches!(asset.kind, EditorAssetKind::Model) { - egui::ComboBox::from_id_salt("model_placement_mode") - .selected_text(match settings.placement_mode { - ModelPlacementMode::StaticAsset => "Static Asset", - ModelPlacementMode::SceneInstance => "Scene Instance", - }) - .show_ui(ui, |ui| { - dirty |= ui - .selectable_value( - &mut settings.placement_mode, - ModelPlacementMode::StaticAsset, - "Static Asset", - ) - .changed(); - dirty |= ui - .selectable_value( - &mut settings.placement_mode, - ModelPlacementMode::SceneInstance, - "Scene Instance", - ) - .changed(); - }); - egui::ComboBox::from_id_salt("model_hierarchy_mode") - .selected_text(match settings.hierarchy_mode { - ModelHierarchyMode::SingleActor => "One Actor", - ModelHierarchyMode::SourceHierarchy => "Source Hierarchy", - }) - .show_ui(ui, |ui| { - dirty |= ui - .selectable_value( - &mut settings.hierarchy_mode, - ModelHierarchyMode::SingleActor, - "One Actor", - ) - .changed(); - dirty |= ui - .selectable_value( - &mut settings.hierarchy_mode, - ModelHierarchyMode::SourceHierarchy, - "Source Hierarchy", - ) - .changed(); - }); - egui::ComboBox::from_id_salt("model_material_policy") - .selected_text(match settings.material_policy { - MaterialImportPolicy::SourceMaterials => "Source Materials", - MaterialImportPolicy::AuthoringOverride => "Authoring Override", - }) - .show_ui(ui, |ui| { - dirty |= ui - .selectable_value( - &mut settings.material_policy, - MaterialImportPolicy::SourceMaterials, - "Source Materials", - ) - .changed(); - dirty |= ui - .selectable_value( - &mut settings.material_policy, - MaterialImportPolicy::AuthoringOverride, - "Authoring Override", - ) - .changed(); - }); - if let Some(manifest) = settings.static_mesh_manifest_path.as_deref() { - detail_row(ui, "Static mesh artifact", manifest); - } - } - if dirty { - if let Some(mut registry) = - world.get_resource_mut::() - { - crate::asset_db::update_import_settings(&mut registry, path, settings); - if matches!(asset.kind, EditorAssetKind::Model) { - if let Some(record) = - crate::asset_db::find_asset_mut_by_path(&mut registry, path) - { - if let Err(error) = - crate::assets::static_mesh::refresh_static_mesh_artifact(record) - { - warn!("Static mesh import settings refresh failed: {error}"); - } - } - } - let _ = crate::asset_db::save_registry(®istry); - registry.index_dirty = false; - } - } - if !record.dependencies.is_empty() { - ui.separator(); - ui.label(panel_heading("Dependencies")); - for dep in &record.dependencies { - ui.small(dep); - } + detail_row(ui, "Static mesh artifact", manifest); } } - detail_row( - ui, - "Size", - file_size(path).map(format_bytes).as_deref().unwrap_or("-"), - ); - detail_row( - ui, - "Modified", - modified_time(path) - .map(format_modified) - .as_deref() - .unwrap_or("-"), + if !record.dependencies.is_empty() { + ui.separator(); + ui.label(panel_heading("Dependencies")); + for dep in &record.dependencies { + ui.add(egui::Label::new(dep).truncate()); + } + } + } + detail_row( + ui, + "Size", + file_size(path).map(format_bytes).as_deref().unwrap_or("-"), + ); + detail_row( + ui, + "Modified", + modified_time(path) + .map(format_modified) + .as_deref() + .unwrap_or("-"), + ); + if matches!(asset.kind, EditorAssetKind::Material) { + ui.separator(); + ui.label(panel_heading("Material")); + material_asset_editor(world, ui, asset, path); + } + } + + ui.separator(); + asset_action_buttons(world, ui, selected_entities, asset); +} + +fn asset_details_header(ui: &mut egui::Ui, asset: &EditorAsset) { + ui.horizontal_wrapped(|ui| { + ui.label( + egui::RichText::new(kind_icon(&asset.kind).as_str()) + .font(egui::FontId::new( + 30.0, + egui::FontFamily::Name(PHOSPHOR.into()), + )) + .color(ACCENT), + ); + ui.vertical(|ui| { + ui.strong(asset.label.as_str()); + ui.small(egui::RichText::new(kind_label(&asset.kind)).color(TEXT_DIM)); + }); + }); +} + +fn subasset_details_panel( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + selection: &AssetSelection, +) { + let Some(embedded) = embedded_asset_for_selection(world, selection) else { + ui.colored_label(egui::Color32::YELLOW, "Embedded asset is not available."); + return; + }; + let AssetSelection::SubAsset { + parent_path, + sub_asset_id, + .. + } = selection + else { + return; + }; + ui.horizontal_wrapped(|ui| { + ui.label( + egui::RichText::new(subasset_icon(embedded.kind).as_str()) + .font(egui::FontId::new( + 30.0, + egui::FontFamily::Name(PHOSPHOR.into()), + )) + .color(ACCENT), + ); + ui.vertical(|ui| { + ui.strong(embedded.label.as_str()); + ui.small(egui::RichText::new(subasset_kind_label(embedded.kind)).color(TEXT_DIM)); + }); + }); + ui.add_space(8.0); + detail_row(ui, "Parent", parent_path); + detail_row(ui, "ID", sub_asset_id); + detail_row(ui, "Source", &embedded.detail); + + ui.separator(); + match embedded.kind { + AssetSubAssetKind::Mesh => { + if ui.button("Place At Origin").clicked() { + spawn_subasset_at(world, selection, Vec3::ZERO); + } + } + AssetSubAssetKind::Texture => { + if ui.button("Apply Texture To Selection").clicked() { + if let Some(asset) = + texture_asset_from_subasset_selection(world, &Some(selection.clone())) + { + apply_texture_to_selection(world, &asset, selected_entities); + } + } + } + AssetSubAssetKind::Material => { + ui.small( + egui::RichText::new( + "Source materials are assigned through static mesh renderer slots.", + ) + .color(TEXT_DIM), ); } - ui.separator(); - if matches!(asset.kind, EditorAssetKind::Texture) - && ui.button("Apply Texture To Selection").clicked() - { - apply_texture_to_selection(world, &asset, selected_entities); + } + if ui.button("Select Parent Asset").clicked() { + world + .resource_mut::() + .select(AssetSelection::File(parent_path.clone())); + } +} + +fn asset_action_buttons( + world: &mut World, + ui: &mut egui::Ui, + selected_entities: &SelectedEntities, + asset: &EditorAsset, +) { + ui.horizontal_wrapped(|ui| { + if matches!(asset.kind, EditorAssetKind::Texture) && ui.button("Apply Texture").clicked() { + apply_texture_to_selection(world, asset, selected_entities); } - if matches!(asset.kind, EditorAssetKind::Material) - && ui.button("Apply Material To Selection").clicked() + if matches!(asset.kind, EditorAssetKind::Material) && ui.button("Apply Material").clicked() { - apply_material_asset_to_selection(world, &asset, selected_entities); + apply_material_asset_to_selection(world, asset, selected_entities); } if matches!( asset.kind, @@ -999,11 +1847,816 @@ fn details_panel(world: &mut World, ui: &mut egui::Ui, selected_entities: &Selec | EditorAssetKind::Level ) && ui.button("Place At Origin").clicked() { - spawn_asset_at(world, &asset, Vec3::ZERO); + spawn_asset_at(world, asset, Vec3::ZERO); + } + if matches!(asset.kind, EditorAssetKind::Model) + && has_embedded_assets(world, asset) + && ui.button("Toggle Contents").clicked() + { + if let Some(path) = asset.path.as_ref() { + toggle_asset_expanded(world, path); + } + } + if matches!(asset.kind, EditorAssetKind::Model) && ui.button("Reimport").clicked() { + reimport_asset(world, asset); + } + if asset.path.is_some() && ui.button("Move To Trash").clicked() { + request_delete_for_asset(world, asset); + } + }); +} + +fn import_settings_editor( + world: &mut World, + ui: &mut egui::Ui, + asset: &EditorAsset, + path: &str, + current: &ImportSettings, +) { + { + let mut state = world.resource_mut::(); + let reset = state + .import_draft + .as_ref() + .is_none_or(|draft| draft.path != path); + if reset { + state.import_draft = Some(ImportSettingsDraft { + path: path.to_string(), + settings: current.clone(), + }); + } + } + + let mut apply = None; + let mut revert = false; + { + let mut state = world.resource_mut::(); + let Some(draft) = state.import_draft.as_mut() else { + return; + }; + ui.add(egui::Slider::new(&mut draft.settings.scale, 0.01..=10.0).text("Scale")); + ui.checkbox(&mut draft.settings.generate_collider, "Generate collider"); + ui.checkbox(&mut draft.settings.lod0_only, "LOD0 only"); + egui::ComboBox::from_id_salt("model_placement_mode") + .selected_text(match draft.settings.placement_mode { + ModelPlacementMode::StaticAsset => "Static Asset", + ModelPlacementMode::SceneInstance => "Scene Instance", + }) + .show_ui(ui, |ui| { + ui.selectable_value( + &mut draft.settings.placement_mode, + ModelPlacementMode::StaticAsset, + "Static Asset", + ); + ui.selectable_value( + &mut draft.settings.placement_mode, + ModelPlacementMode::SceneInstance, + "Scene Instance", + ); + }); + egui::ComboBox::from_id_salt("model_hierarchy_mode") + .selected_text(match draft.settings.hierarchy_mode { + ModelHierarchyMode::SingleActor => "One Actor", + ModelHierarchyMode::SourceHierarchy => "Source Hierarchy", + }) + .show_ui(ui, |ui| { + ui.selectable_value( + &mut draft.settings.hierarchy_mode, + ModelHierarchyMode::SingleActor, + "One Actor", + ); + ui.selectable_value( + &mut draft.settings.hierarchy_mode, + ModelHierarchyMode::SourceHierarchy, + "Source Hierarchy", + ); + }); + egui::ComboBox::from_id_salt("model_material_policy") + .selected_text(match draft.settings.material_policy { + MaterialImportPolicy::SourceMaterials => "Source Materials", + MaterialImportPolicy::AuthoringOverride => "Authoring Override", + }) + .show_ui(ui, |ui| { + ui.selectable_value( + &mut draft.settings.material_policy, + MaterialImportPolicy::SourceMaterials, + "Source Materials", + ); + ui.selectable_value( + &mut draft.settings.material_policy, + MaterialImportPolicy::AuthoringOverride, + "Authoring Override", + ); + }); + ui.horizontal(|ui| { + if ui.button("Apply").clicked() { + apply = Some(draft.settings.clone()); + } + if ui.button("Revert").clicked() { + revert = true; + } + }); + } + + if revert { + world.resource_mut::().import_draft = Some(ImportSettingsDraft { + path: path.to_string(), + settings: current.clone(), + }); + } + if let Some(settings) = apply { + apply_import_settings(world, asset, path, settings); + } +} + +fn apply_import_settings( + world: &mut World, + asset: &EditorAsset, + path: &str, + settings: ImportSettings, +) { + let mut status = format!("Updated import settings for {}", asset.label); + if let Some(mut registry) = world.get_resource_mut::() { + update_import_settings(&mut registry, path, settings); + if matches!(asset.kind, EditorAssetKind::Model) { + if let Some(record) = find_asset_mut_by_path(&mut registry, path) { + if let Err(error) = refresh_static_mesh_artifact(record) { + status = format!("Static mesh refresh failed for {}: {error}", asset.label); + warn!("{status}"); + } + } + } + if let Err(error) = save_registry(®istry) { + status = format!("Asset registry save failed: {error}"); + warn!("{status}"); + } else { + registry.index_dirty = false; + } + } + invalidate_on_catalog_refresh(world); + world.resource_mut::().status = status; +} + +fn material_asset_editor(world: &mut World, ui: &mut egui::Ui, asset: &EditorAsset, path: &str) { + ensure_material_draft(world, path, &asset.label); + let shader_schemas = shader_schema_assets(world); + let texture_assets = texture_asset_refs(world); + let mut apply = false; + let mut revert = false; + { + let mut state = world.resource_mut::(); + let Some(draft) = state.material_draft.as_mut() else { + return; + }; + if let Some(error) = draft.error.as_ref() { + ui.colored_label(egui::Color32::YELLOW, error); + } + compact_text_edit(ui, "Label", &mut draft.asset.label); + material_shader_interface(ui, &mut draft.asset, &shader_schemas, &texture_assets); + ui.separator(); + ui.label(panel_heading("Standard Surface")); + color_desc_edit(ui, "Base color", &mut draft.asset.material.base_color); + ui.add(egui::Slider::new(&mut draft.asset.material.metallic, 0.0..=1.0).text("Metallic")); + ui.add(egui::Slider::new(&mut draft.asset.material.roughness, 0.0..=1.0).text("Roughness")); + color_desc_edit(ui, "Emissive", &mut draft.asset.material.emissive_color); + ui.add( + egui::Slider::new(&mut draft.asset.material.emissive_intensity, 0.0..=50_000.0) + .text("Emissive nits"), + ); + optional_path_edit( + ui, + "Base texture", + &mut draft.asset.material.base_color_texture, + ); + optional_path_edit( + ui, + "Emissive texture", + &mut draft.asset.material.emissive_texture, + ); + optional_path_edit( + ui, + "Normal map", + &mut draft.asset.material.normal_map_texture, + ); + optional_path_edit( + ui, + "Metal/rough", + &mut draft.asset.material.metallic_roughness_texture, + ); + ui.horizontal(|ui| { + if ui.button("Apply").clicked() { + apply = true; + } + if ui.button("Revert").clicked() { + revert = true; + } + }); + } + + if revert { + world.resource_mut::().material_draft = None; + ensure_material_draft(world, path, &asset.label); + } + if apply { + save_material_draft(world); + } +} + +fn ensure_material_draft(world: &mut World, path: &str, fallback_label: &str) { + let needs_load = world + .resource::() + .material_draft + .as_ref() + .is_none_or(|draft| draft.path != path); + if !needs_load { + return; + } + let draft = match MaterialAsset::load_from_path(path) { + Ok(asset) => MaterialAssetDraft { + path: path.to_string(), + asset, + error: None, + }, + Err(error) => MaterialAssetDraft { + path: path.to_string(), + asset: MaterialAsset { + label: fallback_label.to_string(), + shader: None, + material: MaterialDesc::default(), + }, + error: Some(error), + }, + }; + world.resource_mut::().material_draft = Some(draft); +} + +fn save_material_draft(world: &mut World) { + let Some(draft) = world + .resource::() + .material_draft + .clone() + else { + return; + }; + let result = ron::ser::to_string_pretty(&draft.asset, ron::ser::PrettyConfig::default()) + .map_err(|error| format!("could not serialize material {}: {error}", draft.path)) + .and_then(|text| { + fs::write(&draft.path, text) + .map_err(|error| format!("could not write material {}: {error}", draft.path)) + }); + match result { + Ok(()) => { + if let Some(current) = world + .resource_mut::() + .material_draft + .as_mut() + { + current.error = None; + } + world.resource_mut::().refresh(); + invalidate_on_catalog_refresh(world); + world.resource_mut::().status = format!("Saved material {}", draft.path); + } + Err(error) => { + if let Some(current) = world + .resource_mut::() + .material_draft + .as_mut() + { + current.error = Some(error.clone()); + } + world.resource_mut::().status = error; + } + } +} + +fn shader_schema_assets(world: &World) -> Vec<(String, String)> { + let mut schemas: Vec<(String, String)> = world + .resource::() + .assets + .iter() + .filter(|asset| matches!(asset.kind, EditorAssetKind::ShaderSchema)) + .filter_map(|asset| Some((asset.label.clone(), asset.path.clone()?))) + .collect(); + schemas.sort_by(|a, b| natural_cmp(&a.0, &b.0)); + schemas +} + +fn texture_asset_refs(world: &World) -> Vec<(String, EditorAssetRef)> { + let registry = world.get_resource::(); + let mut textures: Vec<(String, EditorAssetRef)> = world + .resource::() + .assets + .iter() + .filter(|asset| matches!(asset.kind, EditorAssetKind::Texture)) + .filter_map(|asset| { + let path = asset.path.as_deref()?; + let record = registry.and_then(|registry| find_asset_by_path(registry, path))?; + Some(( + asset.label.clone(), + EditorAssetRef::new(record.id.as_string(), "texture:source", asset.label.clone()), + )) + }) + .collect(); + textures.sort_by(|a, b| natural_cmp(&a.0, &b.0)); + textures +} + +fn material_shader_interface( + ui: &mut egui::Ui, + asset: &mut MaterialAsset, + shader_schemas: &[(String, String)], + texture_assets: &[(String, EditorAssetRef)], +) { + shader_kind_picker(ui, &mut asset.material.shader.kind); + if matches!(asset.material.shader.kind, MaterialShaderKind::Custom) { + let mut selected_schema = asset.material.shader.schema_path.clone(); + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new("Schema")); + egui::ComboBox::from_id_salt("asset_material_shader_schema") + .selected_text( + selected_schema + .as_deref() + .and_then(|path| { + shader_schemas + .iter() + .find(|(_, schema_path)| schema_path == path) + .map(|(label, _)| label.as_str()) + }) + .unwrap_or("(none)"), + ) + .show_ui(ui, |ui| { + if ui + .selectable_label(selected_schema.is_none(), "(none)") + .clicked() + { + selected_schema = None; + } + for (label, path) in shader_schemas { + if ui + .selectable_label(selected_schema.as_deref() == Some(path), label) + .clicked() + { + selected_schema = Some(path.clone()); + } + } + }); + }); + if selected_schema != asset.material.shader.schema_path { + asset.material.shader.schema_path = selected_schema.clone(); + if let Some(path) = selected_schema.as_deref() { + if let Err(error) = apply_shader_schema(asset, path) { + ui.colored_label(egui::Color32::YELLOW, error); + } + } + } + optional_path_edit(ui, "WGSL shader", &mut asset.material.shader.shader_path); + if let Some(path) = asset.material.shader.schema_path.clone() { + ui.horizontal(|ui| { + if ui.button("Reload Schema").clicked() { + if let Err(error) = apply_shader_schema(asset, &path) { + ui.colored_label(egui::Color32::YELLOW, error); + } + } + ui.small(egui::RichText::new(path).color(TEXT_DIM)); + }); } } else { - ui.label(egui::RichText::new("No asset selected").color(TEXT_DIM)); + asset.material.shader.schema_path = None; + asset.material.shader.shader_path = None; } + + let schema = asset + .material + .shader + .schema_path + .as_deref() + .and_then(|path| ShaderSchemaAsset::load_from_path(path).ok()); + if let Some(schema) = schema.as_ref() { + ui.separator(); + ui.label(panel_heading("Shader Parameters")); + shader_schema_parameter_editor(ui, &schema.parameters, &mut asset.material.parameters); + shader_schema_texture_editor( + ui, + &schema.parameters, + &mut asset.material.textures, + texture_assets, + ); + } else if !asset.material.parameters.is_empty() || !asset.material.textures.is_empty() { + ui.separator(); + ui.label(panel_heading("Stored Shader Values")); + raw_material_parameter_editor(ui, &mut asset.material.parameters); + raw_material_texture_editor(ui, &mut asset.material.textures, texture_assets); + } +} + +fn apply_shader_schema(asset: &mut MaterialAsset, path: &str) -> Result<(), String> { + let schema = ShaderSchemaAsset::load_from_path(path)?; + asset.shader = Some(schema.label.clone()); + asset.material.shader.kind = schema.kind; + asset.material.shader.schema_path = Some(path.to_string()); + if asset.material.shader.shader_path.is_none() { + asset.material.shader.shader_path = schema.wgsl_path.clone(); + } + + for property in &schema.parameters { + if matches!(property.property_type, ShaderPropertyType::Texture) { + if !asset + .material + .textures + .iter() + .any(|texture| texture.name == property.name) + { + let default = schema + .default_textures + .iter() + .find(|texture| texture.name == property.name) + .cloned() + .unwrap_or(MaterialTextureBinding { + name: property.name.clone(), + texture: None, + }); + asset.material.textures.push(default); + } + continue; + } + + let default = schema + .default_values + .iter() + .find(|parameter| parameter.name == property.name) + .map(|parameter| parameter.value.clone()) + .unwrap_or_else(|| default_value_for_shader_property(&property.property_type)); + match asset + .material + .parameters + .iter_mut() + .find(|parameter| parameter.name == property.name) + { + Some(parameter) => { + if !parameter_value_matches_property(¶meter.value, &property.property_type) { + parameter.value = default; + } + } + None => asset.material.parameters.push(MaterialParameter { + name: property.name.clone(), + value: default, + }), + } + } + + Ok(()) +} + +fn shader_schema_parameter_editor( + ui: &mut egui::Ui, + properties: &[ShaderPropertyDesc], + parameters: &mut Vec, +) { + for property in properties + .iter() + .filter(|property| !matches!(property.property_type, ShaderPropertyType::Texture)) + { + let value = ensure_material_parameter(parameters, property); + material_parameter_value_ui(ui, &property.display_name, &property.property_type, value); + } +} + +fn shader_schema_texture_editor( + ui: &mut egui::Ui, + properties: &[ShaderPropertyDesc], + textures: &mut Vec, + texture_assets: &[(String, EditorAssetRef)], +) { + let texture_props: Vec<&ShaderPropertyDesc> = properties + .iter() + .filter(|property| matches!(property.property_type, ShaderPropertyType::Texture)) + .collect(); + if texture_props.is_empty() { + return; + } + ui.separator(); + ui.label(panel_heading("Shader Textures")); + for property in texture_props { + let texture = ensure_material_texture_binding(textures, &property.name); + texture_binding_ui(ui, &property.display_name, texture, texture_assets); + } +} + +fn raw_material_parameter_editor(ui: &mut egui::Ui, parameters: &mut [MaterialParameter]) { + for parameter in parameters { + let property_type = property_type_for_value(¶meter.value); + material_parameter_value_ui(ui, ¶meter.name, &property_type, &mut parameter.value); + } +} + +fn raw_material_texture_editor( + ui: &mut egui::Ui, + textures: &mut [MaterialTextureBinding], + texture_assets: &[(String, EditorAssetRef)], +) { + if textures.is_empty() { + return; + } + ui.separator(); + ui.label(panel_heading("Stored Shader Textures")); + for texture in textures { + let label = texture.name.clone(); + texture_binding_ui(ui, &label, texture, texture_assets); + } +} + +fn ensure_material_parameter<'a>( + parameters: &'a mut Vec, + property: &ShaderPropertyDesc, +) -> &'a mut MaterialParameterValue { + let index = parameters + .iter() + .position(|parameter| parameter.name == property.name) + .unwrap_or_else(|| { + parameters.push(MaterialParameter { + name: property.name.clone(), + value: default_value_for_shader_property(&property.property_type), + }); + parameters.len() - 1 + }); + if !parameter_value_matches_property(¶meters[index].value, &property.property_type) { + parameters[index].value = default_value_for_shader_property(&property.property_type); + } + &mut parameters[index].value +} + +fn ensure_material_texture_binding<'a>( + textures: &'a mut Vec, + name: &str, +) -> &'a mut MaterialTextureBinding { + let index = textures + .iter() + .position(|texture| texture.name == name) + .unwrap_or_else(|| { + textures.push(MaterialTextureBinding { + name: name.to_string(), + texture: None, + }); + textures.len() - 1 + }); + &mut textures[index] +} + +fn material_parameter_value_ui( + ui: &mut egui::Ui, + label: &str, + property_type: &ShaderPropertyType, + value: &mut MaterialParameterValue, +) { + if !parameter_value_matches_property(value, property_type) { + *value = default_value_for_shader_property(property_type); + } + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new(label)); + match value { + MaterialParameterValue::Bool(value) => { + ui.checkbox(value, ""); + } + MaterialParameterValue::Float(value) => { + let (min, max) = match property_type { + ShaderPropertyType::Float { min, max } => (*min, *max), + _ => (None, None), + }; + match (min, max) { + (Some(min), Some(max)) => { + ui.add(egui::Slider::new(value, min..=max)); + } + _ => { + ui.add(egui::DragValue::new(value).speed(0.01)); + } + } + } + MaterialParameterValue::Vec2(value) => { + ui.add_sized([64.0, 20.0], egui::DragValue::new(&mut value.x).speed(0.01)); + ui.add_sized([64.0, 20.0], egui::DragValue::new(&mut value.y).speed(0.01)); + } + MaterialParameterValue::Vec3(value) => { + ui.add_sized([52.0, 20.0], egui::DragValue::new(&mut value.x).speed(0.01)); + ui.add_sized([52.0, 20.0], egui::DragValue::new(&mut value.y).speed(0.01)); + ui.add_sized([52.0, 20.0], egui::DragValue::new(&mut value.z).speed(0.01)); + } + MaterialParameterValue::Color(value) => { + let mut rgba = [value.r, value.g, value.b, value.a]; + if ui.color_edit_button_rgba_unmultiplied(&mut rgba).changed() { + *value = ColorDesc { + r: rgba[0], + g: rgba[1], + b: rgba[2], + a: rgba[3], + }; + } + } + MaterialParameterValue::Enum(value) => { + let options = match property_type { + ShaderPropertyType::Enum { options } => options.as_slice(), + _ => &[], + }; + egui::ComboBox::from_id_salt(format!("material_enum_{label}")) + .selected_text(value.as_str()) + .show_ui(ui, |ui| { + for option in options { + ui.selectable_value(value, option.clone(), option); + } + }); + } + } + }); +} + +fn texture_binding_ui( + ui: &mut egui::Ui, + label: &str, + binding: &mut MaterialTextureBinding, + texture_assets: &[(String, EditorAssetRef)], +) { + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new(label)); + let selected = binding + .texture + .as_ref() + .and_then(|current| { + texture_assets + .iter() + .find(|(_, candidate)| *candidate == *current) + .map(|(label, _)| label.as_str()) + }) + .unwrap_or("(none)"); + egui::ComboBox::from_id_salt(format!("texture_binding_{}", binding.name)) + .selected_text(selected) + .show_ui(ui, |ui| { + if ui + .selectable_label(binding.texture.is_none(), "(none)") + .clicked() + { + binding.texture = None; + } + for (label, reference) in texture_assets { + if ui + .selectable_label(binding.texture.as_ref() == Some(reference), label) + .clicked() + { + binding.texture = Some(reference.clone()); + } + } + }); + if ui.button("Clear").clicked() { + binding.texture = None; + } + }); +} + +fn default_value_for_shader_property(property_type: &ShaderPropertyType) -> MaterialParameterValue { + match property_type { + ShaderPropertyType::Bool => MaterialParameterValue::Bool(false), + ShaderPropertyType::Float { .. } => MaterialParameterValue::Float(0.0), + ShaderPropertyType::Vec2 => MaterialParameterValue::Vec2(Vec2::ZERO), + ShaderPropertyType::Vec3 => MaterialParameterValue::Vec3(Vec3::ZERO), + ShaderPropertyType::Color => MaterialParameterValue::Color(ColorDesc::default()), + ShaderPropertyType::Enum { options } => { + MaterialParameterValue::Enum(options.first().cloned().unwrap_or_default()) + } + ShaderPropertyType::Texture => MaterialParameterValue::Float(0.0), + } +} + +fn parameter_value_matches_property( + value: &MaterialParameterValue, + property_type: &ShaderPropertyType, +) -> bool { + matches!( + (value, property_type), + (MaterialParameterValue::Bool(_), ShaderPropertyType::Bool) + | ( + MaterialParameterValue::Float(_), + ShaderPropertyType::Float { .. } + ) + | (MaterialParameterValue::Vec2(_), ShaderPropertyType::Vec2) + | (MaterialParameterValue::Vec3(_), ShaderPropertyType::Vec3) + | (MaterialParameterValue::Color(_), ShaderPropertyType::Color) + | ( + MaterialParameterValue::Enum(_), + ShaderPropertyType::Enum { .. } + ) + ) +} + +fn property_type_for_value(value: &MaterialParameterValue) -> ShaderPropertyType { + match value { + MaterialParameterValue::Bool(_) => ShaderPropertyType::Bool, + MaterialParameterValue::Float(_) => ShaderPropertyType::Float { + min: None, + max: None, + }, + MaterialParameterValue::Vec2(_) => ShaderPropertyType::Vec2, + MaterialParameterValue::Vec3(_) => ShaderPropertyType::Vec3, + MaterialParameterValue::Color(_) => ShaderPropertyType::Color, + MaterialParameterValue::Enum(_) => ShaderPropertyType::Enum { + options: Vec::new(), + }, + } +} + +fn compact_text_edit(ui: &mut egui::Ui, label: &str, value: &mut String) { + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new(label)); + ui.add_sized( + [fit_width(ui, 100.0, f32::INFINITY), 22.0], + egui::TextEdit::singleline(value), + ); + }); +} + +fn shader_kind_picker(ui: &mut egui::Ui, kind: &mut MaterialShaderKind) { + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new("Shader")); + egui::ComboBox::from_id_salt("asset_material_shader_kind") + .selected_text(match kind { + MaterialShaderKind::StandardLit => "Standard Lit", + MaterialShaderKind::Unlit => "Unlit", + MaterialShaderKind::Custom => "Custom", + }) + .show_ui(ui, |ui| { + ui.selectable_value(kind, MaterialShaderKind::StandardLit, "Standard Lit"); + ui.selectable_value(kind, MaterialShaderKind::Unlit, "Unlit"); + ui.selectable_value(kind, MaterialShaderKind::Custom, "Custom"); + }); + }); +} + +fn color_desc_edit(ui: &mut egui::Ui, label: &str, color: &mut ColorDesc) { + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new(label)); + let mut rgba = [color.r, color.g, color.b, color.a]; + if ui.color_edit_button_rgba_unmultiplied(&mut rgba).changed() { + *color = ColorDesc { + r: rgba[0], + g: rgba[1], + b: rgba[2], + a: rgba[3], + }; + } + }); +} + +fn optional_path_edit(ui: &mut egui::Ui, label: &str, value: &mut Option) { + ui.horizontal(|ui| { + ui.add_sized([92.0, 20.0], egui::Label::new(label)); + let mut text = value.clone().unwrap_or_default(); + if ui + .add_sized( + [fit_width(ui, 80.0, f32::INFINITY), 22.0], + egui::TextEdit::singleline(&mut text), + ) + .changed() + { + *value = if text.trim().is_empty() { + None + } else { + Some(text) + }; + } + if ui.button("Clear").clicked() { + *value = None; + } + }); +} + +fn texture_asset_from_subasset_selection( + world: &World, + selection: &Option, +) -> Option { + let Some(AssetSelection::SubAsset { + label, + kind: AssetSubAssetKind::Texture, + source_path: Some(path), + .. + }) = selection + else { + return None; + }; + if let Some(asset) = world + .resource::() + .assets + .iter() + .find(|asset| asset.path.as_deref() == Some(path.as_str())) + { + return Some(asset.clone()); + } + Some(EditorAsset { + label: label.clone(), + path: Some(path.clone()), + folder_path: Path::new(path) + .parent() + .map(|parent| parent.to_string_lossy().replace('\\', "/")) + .unwrap_or_else(|| ASSETS_ROOT.to_string()), + kind: EditorAssetKind::Texture, + }) } fn detail_row(ui: &mut egui::Ui, label: &str, value: &str) { @@ -1140,6 +2793,12 @@ fn asset_context_menu( asset: &EditorAsset, selected_entities: &SelectedEntities, ) { + if ui.button("Select").clicked() { + world + .resource_mut::() + .select(AssetSelection::from_asset(asset)); + ui.close(); + } if matches!( asset.kind, EditorAssetKind::Primitive(_) @@ -1152,6 +2811,27 @@ fn asset_context_menu( spawn_asset_at(world, asset, Vec3::ZERO); ui.close(); } + if matches!(asset.kind, EditorAssetKind::Model) && has_embedded_assets(world, asset) { + let expanded = asset.path.as_deref().is_some_and(|path| { + world + .resource::() + .expanded_assets + .contains(path) + }); + if ui + .button(if expanded { + "Collapse Contents" + } else { + "Expand Contents" + }) + .clicked() + { + if let Some(path) = asset.path.as_ref() { + toggle_asset_expanded(world, path); + } + ui.close(); + } + } if matches!(asset.kind, EditorAssetKind::Texture) && ui.button("Apply as Base Color Texture").clicked() { @@ -1164,4 +2844,340 @@ fn asset_context_menu( apply_material_asset_to_selection(world, asset, selected_entities); ui.close(); } + if matches!(asset.kind, EditorAssetKind::Model) && ui.button("Reimport").clicked() { + reimport_asset(world, asset); + ui.close(); + } + if matches!( + asset.kind, + EditorAssetKind::Texture | EditorAssetKind::Model | EditorAssetKind::Material + ) && ui.button("Regenerate Thumbnail").clicked() + { + regenerate_asset_thumbnail(world, asset); + ui.close(); + } + if asset.path.is_some() { + ui.separator(); + if ui.button("Move To Trash").clicked() { + request_delete_for_asset(world, asset); + ui.close(); + } + } +} + +fn subasset_context_menu( + world: &mut World, + ui: &mut egui::Ui, + embedded: &EmbeddedAsset, + selected_entities: &SelectedEntities, +) { + if ui.button("Select").clicked() { + world + .resource_mut::() + .select(embedded.selection.clone()); + ui.close(); + } + match embedded.kind { + AssetSubAssetKind::Mesh => { + if ui.button("Place at Origin").clicked() { + spawn_subasset_at(world, &embedded.selection, Vec3::ZERO); + ui.close(); + } + } + AssetSubAssetKind::Texture => { + if ui.button("Apply as Base Color Texture").clicked() { + if let Some(asset) = + texture_asset_from_subasset_selection(world, &Some(embedded.selection.clone())) + { + apply_texture_to_selection(world, &asset, selected_entities); + } + ui.close(); + } + } + AssetSubAssetKind::Material => { + ui.label(egui::RichText::new("Embedded source material").color(TEXT_DIM)); + } + } + if ui.button("Regenerate Thumbnail").clicked() { + regenerate_subasset_thumbnail(world, embedded); + ui.close(); + } + if let AssetSelection::SubAsset { parent_path, .. } = &embedded.selection { + if ui.button("Select Parent Asset").clicked() { + world + .resource_mut::() + .select(AssetSelection::File(parent_path.clone())); + ui.close(); + } + } +} + +fn regenerate_asset_thumbnail(world: &mut World, asset: &EditorAsset) { + let Some(path) = asset.path.clone() else { + return; + }; + let key = asset_cache_key(asset); + let kind = asset.kind.clone(); + let asset_server = world.resource::().clone(); + world.resource_scope(|world, mut cache: Mut| { + cache.retry(&key); + match kind { + EditorAssetKind::Texture => { + cache.request_texture(key.clone(), path.clone(), &asset_server); + } + EditorAssetKind::Model => { + world.resource_scope(|_world, mut studio: Mut| { + cache.request_model(key.clone(), path.clone(), &asset_server, &mut studio); + }); + } + EditorAssetKind::Material => { + world.resource_scope(|_world, mut studio: Mut| { + cache.request_material_asset(key.clone(), path.clone(), &mut studio); + }); + } + _ => {} + } + }); + world.resource_mut::().status = format!("Regenerating thumbnail for {}", asset.label); +} + +fn regenerate_subasset_thumbnail(world: &mut World, embedded: &EmbeddedAsset) { + let AssetSelection::SubAsset { parent_path, .. } = &embedded.selection else { + return; + }; + let key = embedded.thumbnail_key.clone(); + let asset_server = world.resource::().clone(); + world.resource_scope(|world, mut cache: Mut| { + cache.retry(&key); + match embedded.kind { + AssetSubAssetKind::Mesh => { + let Some(mesh_label) = embedded.mesh_label.clone() else { + return; + }; + world.resource_scope(|_world, mut studio: Mut| { + cache.request_mesh_subasset( + key.clone(), + parent_path.clone(), + mesh_label, + embedded.material_label.clone(), + &mut studio, + ); + }); + } + AssetSubAssetKind::Material => { + let Some(material_label) = embedded.material_label.clone() else { + return; + }; + world.resource_scope(|_world, mut studio: Mut| { + cache.request_source_material( + key.clone(), + parent_path.clone(), + material_label, + &mut studio, + ); + }); + } + AssetSubAssetKind::Texture => { + if let Some(texture_path) = embedded.texture_path.clone() { + cache.request_texture(key.clone(), texture_path, &asset_server); + } + } + } + }); + world.resource_mut::().status = + format!("Regenerating thumbnail for {}", embedded.label); +} + +fn reimport_asset(world: &mut World, asset: &EditorAsset) { + let Some(path) = asset.path.as_deref() else { + return; + }; + let mut status = format!("Reimported {}", asset.label); + if let Some(mut registry) = world.get_resource_mut::() { + if let Some(record) = find_asset_mut_by_path(&mut registry, path) { + if let Err(error) = refresh_static_mesh_artifact(record) { + status = format!("Reimport failed for {}: {error}", asset.label); + warn!("{status}"); + } + } + if let Err(error) = save_registry(®istry) { + status = format!("Asset registry save failed: {error}"); + warn!("{status}"); + } else { + registry.index_dirty = false; + } + } + invalidate_on_catalog_refresh(world); + world.resource_mut::().status = status; +} + +fn request_delete_for_asset(world: &mut World, asset: &EditorAsset) { + if let Some(request) = build_delete_request(world, asset) { + world.resource_mut::().pending_delete = Some(request); + } +} + +fn build_delete_request(world: &World, asset: &EditorAsset) -> Option { + let path = asset.path.clone()?; + let mut files = vec![path.clone()]; + let mut warnings = Vec::new(); + if matches!(asset.kind, EditorAssetKind::Model) { + if let Some(record) = world + .get_resource::() + .and_then(|registry| find_asset_by_path(registry, &path)) + { + if let Some(manifest) = record.import_settings.static_mesh_manifest_path { + files.push(manifest); + } + if !record.dependencies.is_empty() { + warnings.push(format!( + "{} imported dependencies will be kept because they may be shared.", + record.dependencies.len() + )); + } + } + } + warnings.push("Scene and prefab references are not rewritten automatically.".to_string()); + dedup_strings(&mut files); + Some(AssetDeleteRequest { + selection: AssetSelection::from_asset(asset), + label: asset.label.clone(), + files, + warnings, + }) +} + +fn draw_delete_modal(world: &mut World, ctx: &egui::Context) { + let pending = world + .resource::() + .pending_delete + .clone(); + let Some(request) = pending else { + return; + }; + let mut action = None; + egui::Window::new("Move Asset To Trash") + .collapsible(false) + .resizable(false) + .default_width(360.0) + .show(ctx, |ui| { + ui.label(format!("Move {} to assets/.trash?", request.label)); + ui.separator(); + ui.label(panel_heading("Files")); + for file in &request.files { + ui.add(egui::Label::new(file).wrap()); + } + if !request.warnings.is_empty() { + ui.separator(); + ui.label(panel_heading("Warnings")); + for warning in &request.warnings { + ui.small(egui::RichText::new(warning).color(TEXT_DIM)); + } + } + ui.separator(); + ui.horizontal(|ui| { + if ui.button("Cancel").clicked() { + action = Some(false); + } + if ui.button("Move To Trash").clicked() { + action = Some(true); + } + }); + }); + + match action { + Some(false) => { + world.resource_mut::().pending_delete = None; + } + Some(true) => { + execute_delete_request(world, &request); + world.resource_mut::().pending_delete = None; + } + None => {} + } +} + +fn execute_delete_request(world: &mut World, request: &AssetDeleteRequest) { + let trash_root = PathBuf::from("assets/.trash").join(trash_timestamp()); + let mut moved = 0usize; + let mut errors = Vec::new(); + for file in &request.files { + let source = Path::new(file); + if !source.exists() { + continue; + } + match move_file_to_trash(source, &trash_root) { + Ok(()) => moved += 1, + Err(error) => errors.push(format!("{file}: {error}")), + } + } + if !errors.is_empty() { + world.resource_mut::().status = format!( + "Could not move {} file(s) to trash: {}", + errors.len(), + errors.join("; ") + ); + return; + } + + if let Some(parent_path) = request.selection.parent_path().map(str::to_string) { + if let Some(mut registry) = world.get_resource_mut::() { + registry.records.retain(|record| record.path != parent_path); + registry.index_dirty = true; + if let Err(error) = save_registry(®istry) { + warn!("Asset registry save failed after delete: {error}"); + } else { + registry.index_dirty = false; + } + } + } + + { + let mut assets = world.resource_mut::(); + if let Some(parent_path) = request.selection.parent_path() { + if assets + .selected + .as_ref() + .and_then(AssetSelection::parent_path) + == Some(parent_path) + { + assets.selected = None; + } + } + assets.refresh(); + } + invalidate_on_catalog_refresh(world); + world.resource_mut::().status = format!( + "Moved {} file(s) for {} to {}", + moved, + request.label, + trash_root.to_string_lossy() + ); +} + +fn move_file_to_trash(source: &Path, trash_root: &Path) -> Result<(), String> { + let target = trash_root.join(source); + if let Some(parent) = target.parent() { + fs::create_dir_all(parent) + .map_err(|error| format!("could not create {}: {error}", parent.display()))?; + } + fs::rename(source, &target) + .or_else(|_| { + fs::copy(source, &target)?; + fs::remove_file(source) + }) + .map_err(|error| format!("could not move to {}: {error}", target.display()))?; + Ok(()) +} + +fn trash_timestamp() -> String { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|duration| duration.as_secs().to_string()) + .unwrap_or_else(|_| "now".to_string()) +} + +fn dedup_strings(values: &mut Vec) { + let mut seen = HashSet::new(); + values.retain(|value| seen.insert(value.clone())); } diff --git a/crates/editor/src/ui/asset_browser/state.rs b/crates/editor/src/ui/asset_browser/state.rs index b34468c..b029d4e 100644 --- a/crates/editor/src/ui/asset_browser/state.rs +++ b/crates/editor/src/ui/asset_browser/state.rs @@ -3,8 +3,10 @@ use std::collections::HashSet; use bevy::prelude::*; +use shared::MaterialAsset; -use crate::assets::{ASSETS_ROOT, BUILTINS_FOLDER}; +use crate::asset_db::ImportSettings; +use crate::assets::{AssetSelection, ASSETS_ROOT, BUILTINS_FOLDER}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum AssetBrowserView { @@ -41,6 +43,31 @@ pub struct AssetBrowserUiState { pub(crate) recursive: bool, pub(crate) show_details: bool, pub(crate) expanded_folders: HashSet, + pub(crate) expanded_assets: HashSet, + pub(crate) pending_delete: Option, + pub(crate) import_draft: Option, + pub(crate) material_draft: Option, +} + +#[derive(Debug, Clone)] +pub(crate) struct AssetDeleteRequest { + pub(crate) selection: AssetSelection, + pub(crate) label: String, + pub(crate) files: Vec, + pub(crate) warnings: Vec, +} + +#[derive(Debug, Clone)] +pub(crate) struct ImportSettingsDraft { + pub(crate) path: String, + pub(crate) settings: ImportSettings, +} + +#[derive(Debug, Clone)] +pub(crate) struct MaterialAssetDraft { + pub(crate) path: String, + pub(crate) asset: MaterialAsset, + pub(crate) error: Option, } impl Default for AssetBrowserUiState { @@ -57,6 +84,10 @@ impl Default for AssetBrowserUiState { recursive: false, show_details: true, expanded_folders, + expanded_assets: HashSet::new(), + pending_delete: None, + import_draft: None, + material_draft: None, } } } diff --git a/crates/editor/src/ui/viewport_chrome.rs b/crates/editor/src/ui/viewport_chrome.rs index 25ccaca..1b0a50c 100644 --- a/crates/editor/src/ui/viewport_chrome.rs +++ b/crates/editor/src/ui/viewport_chrome.rs @@ -4,12 +4,14 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiTextureHandle, EguiUserTextures}; use bevy_inspector_egui::bevy_inspector::hierarchy::SelectedEntities; use egui_phosphor_icons::icons; +use transform_gizmo_bevy::prelude::GizmoOptions; -use crate::assets::EditorAssets; +use crate::assets::{AssetSelection, AssetSubAssetKind, EditorAssets}; use crate::gizmos::{EditorGizmoMode, EditorGizmoSpace}; use crate::render_target::ViewportRenderTarget; use crate::selection::ViewportClick; use crate::state::PlayPossession; +use crate::viewport::actor_icons::ActorIconSettings; use crate::viewport::{ snap_translation, viewport_ground_position, EditorViewportMode, ViewportDisplayMode, ViewportSettings, @@ -378,6 +380,13 @@ fn scene_options_popover( .text("Snap step"), ); } + if let Some(mut gizmo_options) = world.get_resource_mut::() { + ui.separator(); + ui.add( + egui::Slider::new(&mut gizmo_options.visuals.gizmo_size, 40.0..=160.0) + .text("Gizmo size"), + ); + } if let Some(mut mode) = world.get_resource_mut::() { ui.separator(); ui.label("Shading"); @@ -387,6 +396,7 @@ fn scene_options_popover( ui.selectable_value(&mut *mode, EditorViewportMode::Collider, "Colliders"); }); } + let mut actor_icons_active = false; if let Some(mut visualizers) = world.get_resource_mut::() { @@ -409,6 +419,7 @@ fn scene_options_popover( ui.checkbox(&mut visualizers.show_actor_icon_debug, "Warnings"); }); }); + actor_icons_active = visualizers.enabled && visualizers.show_actor_icons; ui.separator(); ui.checkbox(&mut visualizers.show_colliders, "Colliders"); ui.checkbox(&mut visualizers.show_lights, "Lights"); @@ -421,6 +432,18 @@ fn scene_options_popover( ui.checkbox(&mut visualizers.pickable_proxies, "Selectable proxies"); }); } + if actor_icons_active { + if let Some(mut icon_settings) = world.get_resource_mut::() { + ui.add( + egui::Slider::new( + &mut icon_settings.size_pixels, + ActorIconSettings::MIN_SIZE_PIXELS + ..=ActorIconSettings::MAX_SIZE_PIXELS, + ) + .text("Icon size"), + ); + } + } ui.horizontal(|ui| { if ui.button("Save bookmark").clicked() { viewport_save_bookmark(world); @@ -502,12 +525,53 @@ fn viewport_asset_drop_ui( return; } - let asset = { + let selection = { let assets = world.resource::(); - assets.dragging_asset().cloned() + assets.dragging_selection().cloned() }; world.resource_mut::().clear_drag(); + let Some(selection) = selection else { + return; + }; + let placement = viewport_ground_position(world, pointer_pos.unwrap(), viewport_rect) + .map(|pos| snap_translation(pos, world.resource::())) + .unwrap_or(Vec3::ZERO); + if matches!( + selection, + AssetSelection::SubAsset { + kind: AssetSubAssetKind::Mesh, + .. + } + ) { + crate::assets::spawn_subasset_at(world, &selection, placement); + return; + } + + if let AssetSelection::SubAsset { + kind: AssetSubAssetKind::Texture, + source_path: Some(path), + label, + .. + } = &selection + { + let asset = crate::assets::EditorAsset { + label: label.clone(), + path: Some(path.clone()), + folder_path: std::path::Path::new(path) + .parent() + .map(|parent| parent.to_string_lossy().replace('\\', "/")) + .unwrap_or_else(|| crate::assets::ASSETS_ROOT.to_string()), + kind: crate::assets::EditorAssetKind::Texture, + }; + crate::ui::helpers::apply_texture_to_selection(world, &asset, selected); + return; + } + + let asset = { + let assets = world.resource::(); + assets.asset_for_selection(&selection).cloned() + }; let Some(asset) = asset else { return; }; @@ -523,8 +587,5 @@ fn viewport_asset_drop_ui( _ => {} } - let placement = viewport_ground_position(world, pointer_pos.unwrap(), viewport_rect) - .map(|pos| snap_translation(pos, world.resource::())) - .unwrap_or(Vec3::ZERO); crate::assets::spawn_asset_at(world, &asset, placement); } diff --git a/crates/editor/src/viewport/actor_icons.rs b/crates/editor/src/viewport/actor_icons.rs index a8f3cfa..53fb26d 100644 --- a/crates/editor/src/viewport/actor_icons.rs +++ b/crates/editor/src/viewport/actor_icons.rs @@ -2,10 +2,17 @@ use std::collections::{HashMap, HashSet}; -use bevy::asset::{load_internal_binary_asset, uuid_handle, RenderAssetUsages}; +use bevy::asset::{ + load_internal_asset, load_internal_binary_asset, uuid_handle, RenderAssetUsages, +}; use bevy::camera::visibility::NoFrustumCulling; use bevy::image::{CompressedImageFormats, ImageSampler, ImageType}; +use bevy::pbr::{Material, MaterialPlugin}; use bevy::prelude::*; +use bevy::render::render_resource::{ + AsBindGroup, BlendState, CompareFunction, RenderPipelineDescriptor, ShaderType, +}; +use bevy::shader::ShaderRef; use shared::{ ActorKind, AuthoringLightKind, ColliderDesc, LevelObject, LightDesc, ModelRef, ObjectiveMarker, PhysicsBody, PlayerSpawn, PostProcessVolumeDesc, PrefabInstance, PrefabRef, Primitive, @@ -19,11 +26,13 @@ use crate::ui::UiState; use crate::viewport::ViewportDisplayMode; use crate::visualizers::EditorVisualizationSettings; -const ICON_TARGET_PIXELS: f32 = 30.0; -const ICON_MIN_WORLD_SIZE: f32 = 0.2; -const ICON_MAX_WORLD_SIZE: f32 = 3.0; +const ICON_MIN_WORLD_SIZE: f32 = 0.001; +const ICON_MAX_WORLD_SIZE: f32 = 10_000.0; +const ICON_BRIGHTNESS: f32 = 1.6; const DEFAULT_PERSPECTIVE_FOV: f32 = std::f32::consts::FRAC_PI_4; +const ACTOR_ICON_OVERLAY_SHADER: Handle = + uuid_handle!("2edfe544-3ed1-4e13-a880-f56b09804b52"); const ICON_01_ACTOR_EMPTY: Handle = uuid_handle!("d87c7796-b219-4011-8305-4db55422f1c7"); const ICON_02_ACTOR_GENERIC: Handle = uuid_handle!("c9a1a84f-4dcd-4a2b-a5b6-c365dc7d6795"); const ICON_03_ACTOR_MISSING: Handle = uuid_handle!("cca7cdf9-0c7d-4546-9bb9-1176e49a635c"); @@ -69,6 +78,85 @@ pub struct ActorIconProxy { pub target: Entity, } +#[derive(Resource, Debug, Clone)] +pub struct ActorIconSettings { + pub size_pixels: f32, +} + +impl ActorIconSettings { + pub const DEFAULT_SIZE_PIXELS: f32 = 34.0; + pub const MIN_SIZE_PIXELS: f32 = 16.0; + pub const MAX_SIZE_PIXELS: f32 = 96.0; + + pub fn clamped_size_pixels(&self) -> f32 { + self.size_pixels + .clamp(Self::MIN_SIZE_PIXELS, Self::MAX_SIZE_PIXELS) + } +} + +impl Default for ActorIconSettings { + fn default() -> Self { + Self { + size_pixels: Self::DEFAULT_SIZE_PIXELS, + } + } +} + +#[derive(Clone, Copy, ShaderType)] +struct ActorIconOverlayUniform { + brightness: f32, +} + +#[derive(Asset, TypePath, AsBindGroup, Clone)] +struct ActorIconOverlayMaterial { + #[texture(0)] + #[sampler(1)] + texture: Handle, + #[uniform(2)] + uniform: ActorIconOverlayUniform, +} + +impl Material for ActorIconOverlayMaterial { + fn fragment_shader() -> ShaderRef { + ACTOR_ICON_OVERLAY_SHADER.into() + } + + fn alpha_mode(&self) -> AlphaMode { + AlphaMode::Blend + } + + fn enable_prepass() -> bool { + false + } + + fn enable_shadows() -> bool { + false + } + + fn specialize( + _pipeline: &bevy::pbr::MaterialPipeline, + descriptor: &mut RenderPipelineDescriptor, + _layout: &bevy::mesh::MeshVertexBufferLayoutRef, + _key: bevy::pbr::MaterialPipelineKey, + ) -> Result<(), bevy::render::render_resource::SpecializedMeshPipelineError> { + descriptor.primitive.cull_mode = None; + if let Some(depth) = &mut descriptor.depth_stencil { + depth.depth_write_enabled = false; + depth.depth_compare = CompareFunction::Always; + } + if let Some(fragment) = &mut descriptor.fragment { + if let Some(target) = fragment + .targets + .first_mut() + .and_then(|target| target.as_mut()) + { + target.blend = Some(BlendState::ALPHA_BLENDING); + } + } + Ok(()) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] enum ActorIconImage { Empty, @@ -126,7 +214,7 @@ struct ActorIconComponents { #[derive(Resource)] struct ActorIconAssets { mesh: Handle, - materials: HashMap>, + materials: HashMap>, } pub struct ActorIconsPlugin; @@ -134,7 +222,16 @@ pub struct ActorIconsPlugin; impl Plugin for ActorIconsPlugin { fn build(&self, app: &mut App) { register_actor_icon_images(app); - app.add_systems(Startup, init_actor_icon_assets) + load_internal_asset!( + app, + ACTOR_ICON_OVERLAY_SHADER, + "../../assets/shaders/actor_icon_overlay.wgsl", + Shader::from_wgsl + ); + + app.init_resource::() + .add_plugins(MaterialPlugin::::default()) + .add_systems(Startup, init_actor_icon_assets) .add_systems(Update, sync_actor_icons.run_if(scene_tools_active)); } } @@ -204,7 +301,7 @@ fn register_actor_icon_images(app: &mut App) { fn init_actor_icon_assets( mut commands: Commands, mut meshes: ResMut>, - mut materials: ResMut>, + mut materials: ResMut>, ) { let mesh = meshes.add(Rectangle::new(1.0, 1.0)); let mut actor_materials = HashMap::new(); @@ -229,14 +326,11 @@ fn init_actor_icon_assets( ] { actor_materials.insert( image, - materials.add(StandardMaterial { - base_color_texture: Some(texture.clone()), - alpha_mode: AlphaMode::Blend, - unlit: true, - cull_mode: None, - fog_enabled: false, - depth_bias: -40.0, - ..default() + materials.add(ActorIconOverlayMaterial { + texture: texture.clone(), + uniform: ActorIconOverlayUniform { + brightness: ICON_BRIGHTNESS, + }, }), ); } @@ -251,13 +345,14 @@ fn init_actor_icon_assets( fn sync_actor_icons( display: Res, settings: Res, + icon_settings: Res, ui_state: Res, assets: Res, mut commands: Commands, mut icons: Query<( Entity, &ActorIconProxy, - &MeshMaterial3d, + &MeshMaterial3d, &mut Transform, )>, camera_query: Query<(&Camera, &GlobalTransform, Option<&Projection>), With>, @@ -325,7 +420,12 @@ fn sync_actor_icons( if !icon_category_enabled(&settings, spec.category) { continue; } - let transform = desired_icon_transform(global, camera, ui_state.viewport_rect.height()); + let transform = desired_icon_transform( + global, + camera, + ui_state.viewport_rect.height(), + icon_settings.clamped_size_pixels(), + ); desired.insert(target, (spec.image, transform)); } } @@ -390,7 +490,12 @@ fn sync_actor_icons( let Some(material) = assets.materials.get(&spec.image).cloned() else { continue; }; - let transform = desired_icon_transform(global, camera, ui_state.viewport_rect.height()); + let transform = desired_icon_transform( + global, + camera, + ui_state.viewport_rect.height(), + icon_settings.clamped_size_pixels(), + ); commands.spawn(( EditorOnly, RaytracingExcluded, @@ -419,6 +524,7 @@ fn desired_icon_transform( parent_global: &GlobalTransform, camera: Option<(&Camera, &GlobalTransform, Option<&Projection>)>, viewport_height: f32, + target_pixels: f32, ) -> Transform { let translation = parent_global.translation(); let Some((_, camera_global, projection)) = camera else { @@ -429,6 +535,7 @@ fn desired_icon_transform( camera_global, projection, viewport_height.max(1.0), + target_pixels, ); let desired_global = Transform { translation, @@ -444,18 +551,20 @@ fn icon_world_size( camera_global: &GlobalTransform, projection: Option<&Projection>, viewport_height: f32, + target_pixels: f32, ) -> f32 { - let camera_position = camera_global.translation(); - let pixels_to_view = ICON_TARGET_PIXELS / viewport_height; + let pixels_to_view = target_pixels / viewport_height; let world_height = match projection { Some(Projection::Perspective(projection)) => { - let distance = camera_position.distance(target).max(0.1); - 2.0 * distance * (projection.fov * 0.5).tan() + let target_view = camera_global.affine().inverse().transform_point3(target); + let depth = (-target_view.z).abs().max(0.1); + 2.0 * depth * (projection.fov * 0.5).tan() } Some(Projection::Orthographic(projection)) => projection.area.height().abs().max(0.1), _ => { - let distance = camera_position.distance(target).max(0.1); - 2.0 * distance * (DEFAULT_PERSPECTIVE_FOV * 0.5).tan() + let target_view = camera_global.affine().inverse().transform_point3(target); + let depth = (-target_view.z).abs().max(0.1); + 2.0 * depth * (DEFAULT_PERSPECTIVE_FOV * 0.5).tan() } }; (world_height * pixels_to_view).clamp(ICON_MIN_WORLD_SIZE, ICON_MAX_WORLD_SIZE) @@ -653,4 +762,29 @@ mod tests { assert_eq!(spec.image, ActorIconImage::PhysicsCollider); assert_eq!(spec.category, ActorIconCategory::Physics); } + + #[test] + fn perspective_icon_size_uses_view_depth() { + let camera = GlobalTransform::IDENTITY; + let projection = Projection::Perspective(PerspectiveProjection { + fov: DEFAULT_PERSPECTIVE_FOV, + ..default() + }); + let centered = icon_world_size( + Vec3::new(0.0, 0.0, -12.0), + &camera, + Some(&projection), + 900.0, + ActorIconSettings::DEFAULT_SIZE_PIXELS, + ); + let offset = icon_world_size( + Vec3::new(8.0, 0.0, -12.0), + &camera, + Some(&projection), + 900.0, + ActorIconSettings::DEFAULT_SIZE_PIXELS, + ); + + assert_eq!(centered, offset); + } } diff --git a/crates/editor/src/viewport/selection.rs b/crates/editor/src/viewport/selection.rs index 01837b9..fbd84ab 100644 --- a/crates/editor/src/viewport/selection.rs +++ b/crates/editor/src/viewport/selection.rs @@ -306,13 +306,20 @@ fn collect_viewport_pick_hits( let mut seen = std::collections::HashSet::new(); let mut ordered = Vec::new(); - for (entity, _) in hits { - let Some(target) = pick_target_for_entity(*entity, level_objects, proxies, icon_proxies) - else { - continue; - }; - if seen.insert(target) { - ordered.push(target); + for icon_priority in [true, false] { + for (entity, _) in hits { + let is_icon = icon_proxies.get(*entity).is_ok(); + if is_icon != icon_priority { + continue; + } + let Some(target) = + pick_target_for_entity(*entity, level_objects, proxies, icon_proxies) + else { + continue; + }; + if seen.insert(target) { + ordered.push(target); + } } } ordered diff --git a/crates/shared/src/hydration/mod.rs b/crates/shared/src/hydration/mod.rs index 9282eaa..a81a30f 100644 --- a/crates/shared/src/hydration/mod.rs +++ b/crates/shared/src/hydration/mod.rs @@ -17,7 +17,8 @@ use bevy::ecs::system::SystemState; use bevy::prelude::*; use lights::{hydrate_lights, reconcile_missing_runtime_lights}; -use materials::{hydrate_materials, material_from_desc}; +use materials::hydrate_materials; +pub use materials::material_from_desc; use models::hydrate_models; use physics::hydrate_physics; use prefabs::hydrate_prefabs; diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index d050b67..28e6277 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -14,8 +14,8 @@ mod rendering_profile_asset; pub use actor::{infer_actor_kind, validate_actor, ActorValidationError}; pub use components::*; pub use hydration::{ - cascade_config_from_rendering, flush_level_object_hydration, strip_hydrated, - strip_hydrated_entity, HydrationPlugin, + cascade_config_from_rendering, flush_level_object_hydration, material_from_desc, + strip_hydrated, strip_hydrated_entity, HydrationPlugin, }; pub use material_asset::{ MaterialAsset, ShaderPropertyDesc, ShaderPropertyType, ShaderSchemaAsset, diff --git a/docs/README.md b/docs/README.md index 3856576..3a3ea17 100644 --- a/docs/README.md +++ b/docs/README.md @@ -33,6 +33,7 @@ Immutable-style log of significant decisions. Add a new numbered ADR when changi | [0016](adr/0016-unified-rendering-contract.md) | Requested/effective render stack, Solari eligibility, emissive materials | | [0017](adr/0017-normalized-static-mesh-assets.md) | Normalized static mesh assets and renderer placement | | [0018](adr/0018-componentized-actor-inspector-and-materials.md) | Componentized actor inspector, imported asset refs, collider split, material overrides | +| [0019](adr/0019-local-bevy-render-timeout-patch.md) | Local `bevy_render` patch for transient Linux swapchain timeouts | ## Editor framework diff --git a/docs/adr/0019-local-bevy-render-timeout-patch.md b/docs/adr/0019-local-bevy-render-timeout-patch.md new file mode 100644 index 0000000..fd65647 --- /dev/null +++ b/docs/adr/0019-local-bevy-render-timeout-patch.md @@ -0,0 +1,28 @@ +Status: Accepted + +Context +======= + +The editor targets Linux desktop sessions where transient swapchain acquire +timeouts can occur during heavy GPU work, compositor stalls, or driver hiccups. +In upstream `bevy_render` 0.18.1, `prepare_windows` treats +`wgpu::SurfaceError::Timeout` as a fatal path. That can crash the editor even +though the next frame can often recover. + +Decision +======== + +Patch `bevy_render` locally through `[patch.crates-io]` and vendor the scoped +crate copy under `third_party/bevy_render`. The patch is limited to transient +surface acquire timeouts in `prepare_windows`: log the timeout and skip the +frame instead of panicking. Other surface errors remain on their existing paths. + +Consequences +============ + +The editor is more tolerant of transient Linux swapchain stalls while preserving +the current Bevy API surface for the rest of the workspace. + +The vendor patch must be reviewed during every Bevy upgrade and removed once +upstream behavior is sufficient. Keep the diff narrow; do not use the vendored +crate for unrelated render changes. diff --git a/docs/editor/README.md b/docs/editor/README.md index 6e11e31..57afbc2 100644 --- a/docs/editor/README.md +++ b/docs/editor/README.md @@ -37,7 +37,7 @@ Docs for the in-process egui editor (`crates/editor/`). Update this index when a - **Dedicated egui `Camera2d`** at full window — never attach `PrimaryEguiContext` to a viewport-cropped 3D camera (NaN layout panic). - **Unified viewport** uses one render-to-texture target for both the editor fly camera and the possessed player camera so HDR/atmosphere is not broken by sub-viewport cropping. - **PIE:** F8 possess/eject while sim runs; **F6** pauses/resumes simulation in Play; project settings drive shared rendering for the active viewport camera. -- **Asset browser** mirrors the on-disk `assets/` directory with project tree, breadcrumb, search/filter/sort, grid/list views, texture and model thumbnails (`AssetThumbnailCache::request_model`), and a compact details pane; narrow docks prioritize content, wrap toolbar/footer controls, switch list view to a compact single-column layout, and hide tree/details panes when they would crowd the content area. The footer stays pinned while only the project tree and asset content panels scroll. **Built-ins** holds spawnable primitives and lights. **Materials** folder scans `assets/materials/*.ron`; **Shaders** holds shader schema RON files. glTF/GLB/FBX details expose static asset vs scene-instance placement, hierarchy mode, material policy, collider generation, and the generated static mesh artifact path. +- **Asset browser** mirrors the on-disk `assets/` directory with project tree, breadcrumb, search/filter/sort, grid/list views, texture/model/material thumbnails, a details pane, and context-aware row/menu actions; narrow docks prioritize content, keep the root panel fixed, switch list view to a compact single-column layout, and hide tree/details panes when they would crowd the content area. The footer stays pinned while only the project tree, asset content, and details panels scroll. **Built-ins** holds spawnable primitives and lights. **Materials** folder scans `assets/materials/*.ron`, renders material thumbnails on a sphere using `MaterialDesc`, and exposes shader-schema-driven parameters/textures in the details editor; **Shaders** holds shader schema RON files. glTF/GLB/FBX rows can expand into a shelf of normalized embedded mesh, material, and texture subassets with independent generated thumbnails. Mesh subassets can be selected, dragged into the viewport, or placed from details/context menus; material subassets render source-material spheres; texture subassets can be applied to the selected actor. Model import settings are staged with **Apply** / **Revert**, asset context menus can regenerate thumbnails, material asset details edit shared `MaterialAsset` fields, and file asset deletion moves sources/generated artifacts into `assets/.trash/`. - **Static mesh renderer** — model drag/drop defaults to `ActorKind::StaticMesh + StaticMeshRenderer` using normalized artifacts under `assets/meshes/generated/`. Renderer slots store imported `EditorAssetRef` mesh/material references, not source FBX/glTF paths. `SceneInstance` placement keeps the legacy `ImportedModel + ModelRef` path for full scene playback. - **Collider split** — imported mesh collision lives in a separate `ColliderDesc::StaticMesh` plus optional `RigidBodyDesc`; renderer slots own only render visibility, shadows, mesh, and material references. - **Material assets** — `MaterialAsset` RON (`label` + `MaterialDesc`) is shader-schema aware. Actor material edits write `MaterialOverride` entries keyed by renderer slot ID; content-browser asset inspectors are the path for editing shared material assets. @@ -50,11 +50,11 @@ Docs for the in-process egui editor (`crates/editor/`). Update this index when a - **Inspector** shows a responsive header, **Transform**, registered authoring component cards, and a pinned bottom Add Component footer (`actor_inspector`); only the component body scrolls. Rows wrap or stack inside narrow docks instead of forcing the panel wider, and legacy authoring sections use the same component-card shell. Component card carets collapse/expand for the current editor session, status dots toggle persisted active state, and triple-dot menus provide reset, copy/paste values, move, remove, and documentation slots. Inactive authoring components remain in the scene but are skipped by hydration/runtime queries, stripping their generated meshes, lights, physics, or post-process effects. `StaticMeshRenderer` uses concept-style slot cards with model thumbnails, imported mesh/material selectors, visibility, shadow flags, and per-actor material overrides. Empty actor material refs display the inherited source material from the generated static mesh artifact as **Source default**; selector **Browse** assigns explicit normalized imported sub-assets, **Locate** focuses the owning asset in the Asset Browser, and **Clear** returns optional material slots to the inherited source default. `ColliderDesc` and `RigidBodyDesc` are separate component cards. Game crates add sections via `ActorInspectorSection` + `game::editor_ext::actor_inspector_section_ids()`. - **Command palette** (`Ctrl+P`): `scene.reset_lighting`, `rendering.create_volume`, `rendering.select_volumes_at_camera`, `rendering.focus_active_volumes`, `selection.group`, `selection.focus`, and play commands. - **ActorKind** is required on saved level objects (schema v2). Save strips hydrated ECS before writing `.scn.ron`. -- **Scene visualizers** expose actor root icons, colliders, lights, player spawns, prefab/model anchors, Project Sun, and runtime player/camera markers without changing saved scene data. +- **Scene visualizers** expose actor root icons, colliders, lights, player spawns, prefab/model anchors, Project Sun, and runtime player/camera markers without changing saved scene data. Viewport options include actor icon size and transform gizmo size sliders. - **Clean game-view overlay** (`G`) hides editor chrome, grid, selection outlines, transform gizmos, actor root icons, visualizers, and selectable proxies without changing camera ownership; `Ctrl+G` toggles grid. - **Viewport shading** (toolbar): **Lit** (default WYSIWYG), **Unlit** (base-color debug), **Colliders** (hide meshes, emphasize collider gizmos). Light gizmos show range spheres, spot cones, and directional sun arrows. A **mode badge** (bottom-right of the viewport) shows the active mode; Collider mode displays a warning banner. - **Hierarchy** sort modes (**Manual**, **Name**, **Type**) use stable tie-breakers so duplicate names (e.g. several `Pillar` rows) do not reorder on every click. Drag-and-drop reparenting disables panel scroll-to-drag; drop indicators appear only while dragging. -- **Viewport picking**: click selects nearest object, actor root icon, or visualizer hit; **Tab** cycles overlapping targets at the last click (selection pin deferred). +- **Viewport picking**: click selects nearest object, actor root icon, or visualizer hit, with actor icons preferred when clicked over meshes; **Tab** cycles overlapping targets at the last click (selection pin deferred). - **Settings data** lives in `crates/settings`; **settings UI** lives in `settings_ui.rs`. When implementing or changing any of the above, update this file and the root README in the same change. diff --git a/docs/editor/architecture.md b/docs/editor/architecture.md index a6004df..d065e90 100644 --- a/docs/editor/architecture.md +++ b/docs/editor/architecture.md @@ -76,13 +76,13 @@ The viewport uses **render-to-texture**: the active camera renders to an offscre **HDR invariant:** The atmosphere/HDR stack (`Atmosphere`, `Hdr`, etc.) must never render to the primary window swapchain (`Rgba8UnormSrgb`). When the HDR offscreen target is missing, `scene_view` / `play` deactivate cameras; `render_view` skips stack sync until a target exists. -**Viewport picking:** LMB selection maps the egui panel UV through `scene_view_ray` and raycasts with `MeshRayCast`. Object pick runs in `Last` after `transform_gizmo_bevy` so gizmo drags take priority. `GizmoOptions.viewport_rect` is synced from the viewport panel rect so gizmo hit-testing matches render-to-texture layout. Actor root icon and visualizer proxy hits resolve to their source entity, so clicking an icon or light/spawn/collider marker selects the authored or runtime object, not the helper. In Edit mode, selecting the runtime Player redirects to an authored `PlayerSpawn` (`Player Start`), creating one if needed. +**Viewport picking:** LMB selection maps the egui panel UV through `scene_view_ray` and raycasts with `MeshRayCast`. Object pick runs in `Last` after `transform_gizmo_bevy` so gizmo drags take priority. `GizmoOptions.viewport_rect` is synced from the viewport panel rect so gizmo hit-testing matches render-to-texture layout. Actor root icon and visualizer proxy hits resolve to their source entity, so clicking an icon or light/spawn/collider marker selects the authored or runtime object, not the helper. Actor icon hits are ordered ahead of ordinary mesh hits when both are under the cursor. In Edit mode, selecting the runtime Player redirects to an authored `PlayerSpawn` (`Player Start`), creating one if needed. **Selection overlay:** `selection_outline.rs` draws a shared unit-box shell per selected `LevelObject` (bounds from `Primitive`, physics collider, or mesh AABB), scaled to fit. Custom shader (`assets/shaders/selection_outline.wgsl`) uses rim-only fresnel with two cheap depth passes: `Greater` (soft cyan x-ray) and `LessEqual` (amber edge). Sync runs only when selection changes. Editor-only. **Clean game-view overlay:** `ViewportDisplayMode.clean_game_view` hides editor chrome, grid, selection outlines, transform gizmos, actor root icons, visualizers, and selectable proxy meshes without changing camera ownership. `G` toggles this mode; `Ctrl+G` toggles the grid. -**Actor root icons:** `actor_icons.rs` registers the editor PNG icon set as internal binary image assets and spawns screen-stable, camera-facing editor-only billboards at `LevelObject` roots. The billboards are `RaytracingExcluded`, hidden with clean game-view, filtered from hierarchy and scene serialization, and expose per-category viewport options for actors, meshes, lights, physics, gameplay, volumes, prefab/model anchors, and warnings. +**Actor root icons:** `actor_icons.rs` registers the editor PNG icon set as internal binary image assets and spawns screen-stable, camera-facing editor-only billboards at `LevelObject` roots. A custom overlay material draws them unlit with always-pass depth so opaque meshes do not obstruct them. The billboards are `RaytracingExcluded`, hidden with clean game-view, filtered from hierarchy and scene serialization, and expose per-category viewport options for actors, meshes, lights, physics, gameplay, volumes, prefab/model anchors, warnings, and icon size. **Scene visualizers:** `visualizers.rs` draws transient gizmo lines for authoring components without obvious meshes: colliders, lights, `PlayerSpawn`, prefab/model anchors, Project Sun, and runtime player/camera markers. Small editor-only proxy meshes make those markers selectable but are filtered from hierarchy and scene serialization. Player/start markers use the runtime capsule constants from `sim::tuning`. @@ -98,7 +98,7 @@ The egui layer lives under `crates/editor/src/ui/`: | `theme.rs` | Unity-like dark pro palette + `egui_dock` chrome | | `fonts.rs` | Phosphor icon font on `PrimaryEguiContext` | | `widgets.rs` | Icon buttons, tool toggles, menu helpers | -| `asset_browser.rs` | Project/file browser with tree, breadcrumb, filters, list/grid modes, details/import settings | +| `asset_browser.rs` | Project/file browser with tree, breadcrumb, filters, list/grid modes, embedded model shelves, details/import settings | | `menu.rs` | Top menu bar with shortcuts | | `viewport_chrome.rs` | Unified viewport RTT + overlay toolbars | | `actor_icons.rs` | Screen-stable actor root icons and selectable icon proxies | @@ -146,6 +146,7 @@ On play enter/exit the editor snapshots **player sim state only** (transform, ve - **Import:** File → Import Assets copies glTF/GLB/FBX into `assets/models/`. FBX imports also copy a sibling `{name}.fbm/` folder when present (embedded textures). - **Processing:** the asset registry generates normalized static mesh manifests under `assets/meshes/generated/`. The manifests store stable part IDs, glTF/FBX mesh/material subasset labels, source metadata, dependencies, and import settings. These artifacts are hidden from the Asset Browser catalog. +- **Browser subassets:** model rows can expand into a content shelf backed by the generated manifest. Mesh subassets can be selected or dragged into the viewport as `StaticMeshRenderer` actors, material subassets expose source defaults, and texture dependencies can be applied to selected actors without treating glTF and FBX differently. The thumbnail studio renders each generated model/subasset/material preview into its own render target before registering it in the cache so later thumbnails cannot overwrite earlier cache entries. - **Default placement:** model drag/drop creates `ActorKind::StaticMesh + StaticMeshRenderer`. Renderer slots reference imported `EditorAssetRef` values (`asset_id` + `sub_asset_id`) and hydration resolves those through the generated artifact. `SingleActor` hierarchy mode stores all parts in one renderer; `SourceHierarchy` creates a root with child static mesh actors. - **Collision:** when collider generation is enabled, placement adds `RigidBodyDesc` and `ColliderDesc::StaticMesh` using the same imported mesh refs. Renderer slots no longer own collider state. - **Scene-instance placement:** asset details can switch placement to `SceneInstance`; that keeps `ImportedModel + ModelRef`, which hydrates to `SceneRoot`. glTF uses `GltfAssetLabel::Scene`; FBX uses `bevy_ufbx` (`path#SceneN`). `FbxPlugin` is registered in `GamePlugin`. @@ -178,6 +179,8 @@ Shared scene schema lives in `crates/scene` (stamp/migrate/validate on save, loa The unified viewport renders through an HDR offscreen target (`render_target.rs`). When the panel target is missing (egui reflow) or settings disable HDR, cameras must strip atmosphere/post-FX before targeting the swapchain. `render_view::sync_project_render_view` runs in **PostUpdate** after RTT target assignment and reapplies the project FX stack whenever the active panel target handle changes (startup, resize), not only on settings Apply. Project Settings uses a **draft buffer**; **Apply** commits after render targets resync. +The workspace patches `bevy_render` under `third_party/bevy_render` so Linux `wgpu::SurfaceError::Timeout` during swapchain texture acquisition logs and skips the frame instead of panicking in `prepare_windows`. Keep this patch scoped to transient surface acquire timeouts and re-evaluate it during Bevy upgrades. + ## Extensibility (phase 6) `extensibility.rs` provides `EditorCommand`, `EditorPlugin`, and a working command palette (Ctrl+P). Game crates register panel hooks via `game::editor_ext::editor_panel_setups()`; the editor binary wraps them without editing `ui/mod.rs` (ADR 0007, dogfood: FPS Demo panel, Ctrl+Shift+G). diff --git a/third_party/bevy_render/Cargo.toml b/third_party/bevy_render/Cargo.toml new file mode 100644 index 0000000..86032bb --- /dev/null +++ b/third_party/bevy_render/Cargo.toml @@ -0,0 +1,296 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2024" +name = "bevy_render" +version = "0.18.1" +build = false +autolib = false +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "Provides rendering functionality for Bevy Engine" +homepage = "https://bevy.org" +readme = "README.md" +keywords = ["bevy"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/bevyengine/bevy" +resolver = "2" + +[package.metadata.docs.rs] +rustdoc-args = [ + "-Zunstable-options", + "--generate-link-to-definition", +] +all-features = true + +[features] +ci_limits = [] +decoupled_naga = ["bevy_shader/decoupled_naga"] +detailed_trace = [] +gles = ["wgpu/gles"] +morph = ["bevy_mesh/morph"] +multi_threaded = ["bevy_tasks/multi_threaded"] +raw_vulkan_init = ["wgpu/vulkan"] +serialize = ["bevy_mesh/serialize"] +shader_format_spirv = [ + "bevy_shader/shader_format_spirv", + "wgpu/spirv", +] +spirv_shader_passthrough = ["wgpu/spirv"] +statically-linked-dxc = ["wgpu/static-dxc"] +trace = ["profiling"] +tracing-tracy = ["dep:tracy-client"] +vulkan-portability = ["wgpu/vulkan-portability"] +webgl = ["wgpu/webgl"] +webgpu = ["wgpu/webgpu"] + +[lib] +name = "bevy_render" +path = "src/lib.rs" + +[dependencies.async-channel] +version = "2.3.0" + +[dependencies.bevy_app] +version = "0.18.0" + +[dependencies.bevy_asset] +version = "0.18.0" + +[dependencies.bevy_camera] +version = "0.18.0" + +[dependencies.bevy_color] +version = "0.18.0" +features = [ + "serialize", + "wgpu-types", +] + +[dependencies.bevy_derive] +version = "0.18.0" + +[dependencies.bevy_diagnostic] +version = "0.18.0" + +[dependencies.bevy_ecs] +version = "0.18.0" + +[dependencies.bevy_encase_derive] +version = "0.18.0" + +[dependencies.bevy_image] +version = "0.18.0" + +[dependencies.bevy_math] +version = "0.18.0" + +[dependencies.bevy_mesh] +version = "0.18.0" + +[dependencies.bevy_platform] +version = "0.18.0" +features = [ + "std", + "serialize", +] +default-features = false + +[dependencies.bevy_reflect] +version = "0.18.0" + +[dependencies.bevy_render_macros] +version = "0.18.0" + +[dependencies.bevy_shader] +version = "0.18.0" + +[dependencies.bevy_tasks] +version = "0.18.0" + +[dependencies.bevy_time] +version = "0.18.0" + +[dependencies.bevy_transform] +version = "0.18.0" + +[dependencies.bevy_utils] +version = "0.18.0" + +[dependencies.bevy_window] +version = "0.18.0" + +[dependencies.bitflags] +version = "2" + +[dependencies.bytemuck] +version = "1.5" +features = [ + "derive", + "must_cast", +] + +[dependencies.derive_more] +version = "2" +features = ["from"] +default-features = false + +[dependencies.downcast-rs] +version = "2" +features = ["std"] +default-features = false + +[dependencies.encase] +version = "0.12" + +[dependencies.fixedbitset] +version = "0.5" + +[dependencies.glam] +version = "0.30.7" +features = [ + "std", + "encase", +] +default-features = false + +[dependencies.image] +version = "0.25.2" +default-features = false + +[dependencies.indexmap] +version = "2" + +[dependencies.naga] +version = "27" +features = ["wgsl-in"] + +[dependencies.nonmax] +version = "0.5" + +[dependencies.offset-allocator] +version = "0.2" + +[dependencies.profiling] +version = "1" +features = ["profile-with-tracing"] +optional = true + +[dependencies.smallvec] +version = "1" +features = ["const_new"] +default-features = false + +[dependencies.thiserror] +version = "2" +default-features = false + +[dependencies.tracing] +version = "0.1" +features = ["std"] +default-features = false + +[dependencies.tracy-client] +version = "0.18.3" +optional = true + +[dependencies.variadics_please] +version = "1.1" + +[dependencies.wgpu] +version = "27" +features = [ + "wgsl", + "dx12", + "metal", + "vulkan", + "naga-ir", + "fragile-send-sync-non-atomic-wasm", +] +default-features = false + +[dev-dependencies.proptest] +version = "1" + +[target.'cfg(all(target_arch = "wasm32", target_feature = "atomics"))'.dependencies.send_wrapper] +version = "0.6.0" + +[target.'cfg(target_arch = "wasm32")'.dependencies.bevy_app] +version = "0.18.0" +features = ["web"] +default-features = false + +[target.'cfg(target_arch = "wasm32")'.dependencies.bevy_platform] +version = "0.18.0" +features = ["web"] +default-features = false + +[target.'cfg(target_arch = "wasm32")'.dependencies.bevy_reflect] +version = "0.18.0" +features = ["web"] +default-features = false + +[target.'cfg(target_arch = "wasm32")'.dependencies.js-sys] +version = "0.3.83" + +[target.'cfg(target_arch = "wasm32")'.dependencies.wasm-bindgen] +version = "0.2" + +[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] +version = "0.3.67" +features = [ + "Blob", + "Document", + "Element", + "HtmlElement", + "Node", + "Url", + "Window", +] + +[lints.clippy] +alloc_instead_of_core = "warn" +allow_attributes = "warn" +allow_attributes_without_reason = "warn" +doc_markdown = "warn" +manual_let_else = "warn" +match_same_arms = "warn" +needless_lifetimes = "allow" +nonstandard_macro_braces = "warn" +print_stderr = "warn" +print_stdout = "warn" +ptr_as_ptr = "warn" +ptr_cast_constness = "warn" +redundant_closure_for_method_calls = "warn" +redundant_else = "warn" +ref_as_ptr = "warn" +semicolon_if_nothing_returned = "warn" +std_instead_of_alloc = "warn" +std_instead_of_core = "warn" +too_long_first_doc_paragraph = "allow" +too_many_arguments = "allow" +type_complexity = "allow" +undocumented_unsafe_blocks = "warn" +unwrap_or_default = "warn" + +[lints.rust] +missing_docs = "warn" +unsafe_code = "deny" +unsafe_op_in_unsafe_fn = "warn" +unused_qualifications = "warn" + +[lints.rust.unexpected_cfgs] +level = "warn" +priority = 0 +check-cfg = ["cfg(docsrs_dep)"] diff --git a/third_party/bevy_render/Cargo.toml.orig b/third_party/bevy_render/Cargo.toml.orig new file mode 100644 index 0000000..e20e2e5 --- /dev/null +++ b/third_party/bevy_render/Cargo.toml.orig @@ -0,0 +1,155 @@ +[package] +name = "bevy_render" +version = "0.18.1" +edition = "2024" +description = "Provides rendering functionality for Bevy Engine" +homepage = "https://bevy.org" +repository = "https://github.com/bevyengine/bevy" +license = "MIT OR Apache-2.0" +keywords = ["bevy"] + +[features] +# Bevy users should _never_ turn this feature on. +# +# Bevy/wgpu developers can turn this feature on to test a newer version of wgpu without needing to also update naga_oil. +# +# When turning this feature on, you can add the following to bevy/Cargo.toml (not this file), and then run `cargo update`: +# [patch.crates-io] +# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "..." } +# wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "..." } +# wgpu-hal = { git = "https://github.com/gfx-rs/wgpu", rev = "..." } +# wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "..." } +decoupled_naga = ["bevy_shader/decoupled_naga"] + +multi_threaded = ["bevy_tasks/multi_threaded"] + +morph = ["bevy_mesh/morph"] + +shader_format_spirv = ["bevy_shader/shader_format_spirv", "wgpu/spirv"] + +# Enable SPIR-V shader passthrough +spirv_shader_passthrough = ["wgpu/spirv"] + +# Statically linked DXC shader compiler for DirectX 12 +# TODO: When wgpu switches to DirectX 12 instead of Vulkan by default on windows, make this a default feature +statically-linked-dxc = ["wgpu/static-dxc"] + +# Forces the wgpu instance to be initialized using the raw Vulkan HAL, enabling additional configuration +raw_vulkan_init = ["wgpu/vulkan"] + +trace = ["profiling"] +tracing-tracy = ["dep:tracy-client"] +ci_limits = [] +webgl = ["wgpu/webgl"] +webgpu = ["wgpu/webgpu"] +vulkan-portability = ["wgpu/vulkan-portability"] +gles = ["wgpu/gles"] +detailed_trace = [] +## Adds serialization support through `serde`. +serialize = ["bevy_mesh/serialize"] + +[dependencies] +# bevy +bevy_app = { path = "../bevy_app", version = "0.18.0" } +bevy_asset = { path = "../bevy_asset", version = "0.18.0" } +bevy_color = { path = "../bevy_color", version = "0.18.0", features = [ + "serialize", + "wgpu-types", +] } +bevy_derive = { path = "../bevy_derive", version = "0.18.0" } +bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.18.0" } +bevy_ecs = { path = "../bevy_ecs", version = "0.18.0" } +bevy_encase_derive = { path = "../bevy_encase_derive", version = "0.18.0" } +bevy_math = { path = "../bevy_math", version = "0.18.0" } +bevy_reflect = { path = "../bevy_reflect", version = "0.18.0" } +bevy_render_macros = { path = "macros", version = "0.18.0" } +bevy_time = { path = "../bevy_time", version = "0.18.0" } +bevy_transform = { path = "../bevy_transform", version = "0.18.0" } +bevy_window = { path = "../bevy_window", version = "0.18.0" } +bevy_utils = { path = "../bevy_utils", version = "0.18.0" } +bevy_tasks = { path = "../bevy_tasks", version = "0.18.0" } +bevy_image = { path = "../bevy_image", version = "0.18.0" } +bevy_mesh = { path = "../bevy_mesh", version = "0.18.0" } +bevy_camera = { path = "../bevy_camera", version = "0.18.0" } +bevy_shader = { path = "../bevy_shader", version = "0.18.0" } +bevy_platform = { path = "../bevy_platform", version = "0.18.0", default-features = false, features = [ + "std", + "serialize", +] } + +# rendering +image = { version = "0.25.2", default-features = false } + +# misc +# `fragile-send-sync-non-atomic-wasm` feature means we can't use Wasm threads for rendering +# It is enabled for now to avoid having to do a significant overhaul of the renderer just for wasm. +# When the 'atomics' feature is enabled `fragile-send-sync-non-atomic` does nothing +# and Bevy instead wraps `wgpu` types to verify they are not used off their origin thread. +wgpu = { version = "27", default-features = false, features = [ + "wgsl", + "dx12", + "metal", + "vulkan", + "naga-ir", + "fragile-send-sync-non-atomic-wasm", +] } +naga = { version = "27", features = ["wgsl-in"] } +bytemuck = { version = "1.5", features = ["derive", "must_cast"] } +downcast-rs = { version = "2", default-features = false, features = ["std"] } +thiserror = { version = "2", default-features = false } +derive_more = { version = "2", default-features = false, features = ["from"] } +encase = "0.12" +glam = { version = "0.30.7", default-features = false, features = [ + "std", + "encase", +] } +# For wgpu profiling using tracing. Use `RUST_LOG=info` to also capture the wgpu spans. +profiling = { version = "1", features = [ + "profile-with-tracing", +], optional = true } +async-channel = "2.3.0" +nonmax = "0.5" +smallvec = { version = "1", default-features = false, features = ["const_new"] } +offset-allocator = "0.2" +variadics_please = "1.1" +tracing = { version = "0.1", default-features = false, features = ["std"] } +tracy-client = { version = "0.18.3", optional = true } +indexmap = { version = "2" } +fixedbitset = { version = "0.5" } +bitflags = "2" + +[target.'cfg(all(target_arch = "wasm32", target_feature = "atomics"))'.dependencies] +send_wrapper = { version = "0.6.0" } + +[dev-dependencies] +proptest = "1" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3.83" +web-sys = { version = "0.3.67", features = [ + 'Blob', + 'Document', + 'Element', + 'HtmlElement', + 'Node', + 'Url', + 'Window', +] } +wasm-bindgen = "0.2" +# TODO: Assuming all wasm builds are for the browser. Require `no_std` support to break assumption. +bevy_app = { path = "../bevy_app", version = "0.18.0", default-features = false, features = [ + "web", +] } +bevy_platform = { path = "../bevy_platform", version = "0.18.0", default-features = false, features = [ + "web", +] } +bevy_reflect = { path = "../bevy_reflect", version = "0.18.0", default-features = false, features = [ + "web", +] } + +[lints] +workspace = true + +[package.metadata.docs.rs] +rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"] +all-features = true diff --git a/third_party/bevy_render/LICENSE-APACHE b/third_party/bevy_render/LICENSE-APACHE new file mode 100644 index 0000000..d9a10c0 --- /dev/null +++ b/third_party/bevy_render/LICENSE-APACHE @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/third_party/bevy_render/LICENSE-MIT b/third_party/bevy_render/LICENSE-MIT new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/third_party/bevy_render/LICENSE-MIT @@ -0,0 +1,19 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/bevy_render/README.md b/third_party/bevy_render/README.md new file mode 100644 index 0000000..0106a5c --- /dev/null +++ b/third_party/bevy_render/README.md @@ -0,0 +1,7 @@ +# Bevy Render + +[![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/bevyengine/bevy#license) +[![Crates.io](https://img.shields.io/crates/v/bevy_render.svg)](https://crates.io/crates/bevy_render) +[![Downloads](https://img.shields.io/crates/d/bevy_render.svg)](https://crates.io/crates/bevy_render) +[![Docs](https://docs.rs/bevy_render/badge.svg)](https://docs.rs/bevy_render/latest/bevy_render/) +[![Discord](https://img.shields.io/discord/691052431525675048.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/bevy) diff --git a/third_party/bevy_render/src/alpha.rs b/third_party/bevy_render/src/alpha.rs new file mode 100644 index 0000000..dd74881 --- /dev/null +++ b/third_party/bevy_render/src/alpha.rs @@ -0,0 +1,62 @@ +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; + +// TODO: add discussion about performance. +/// Sets how a material's base color alpha channel is used for transparency. +#[derive(Debug, Default, Reflect, Copy, Clone, PartialEq)] +#[reflect(Default, Debug, Clone)] +pub enum AlphaMode { + /// Base color alpha values are overridden to be fully opaque (1.0). + #[default] + Opaque, + /// Reduce transparency to fully opaque or fully transparent + /// based on a threshold. + /// + /// Compares the base color alpha value to the specified threshold. + /// If the value is below the threshold, + /// considers the color to be fully transparent (alpha is set to 0.0). + /// If it is equal to or above the threshold, + /// considers the color to be fully opaque (alpha is set to 1.0). + Mask(f32), + /// The base color alpha value defines the opacity of the color. + /// Standard alpha-blending is used to blend the fragment's color + /// with the color behind it. + Blend, + /// Similar to [`AlphaMode::Blend`], however assumes RGB channel values are + /// [premultiplied](https://en.wikipedia.org/wiki/Alpha_compositing#Straight_versus_premultiplied). + /// + /// For otherwise constant RGB values, behaves more like [`AlphaMode::Blend`] for + /// alpha values closer to 1.0, and more like [`AlphaMode::Add`] for + /// alpha values closer to 0.0. + /// + /// Can be used to avoid “border” or “outline” artifacts that can occur + /// when using plain alpha-blended textures. + Premultiplied, + /// Spreads the fragment out over a hardware-dependent number of sample + /// locations proportional to the alpha value. This requires multisample + /// antialiasing; if MSAA isn't on, this is identical to + /// [`AlphaMode::Mask`] with a value of 0.5. + /// + /// Alpha to coverage provides improved performance and better visual + /// fidelity over [`AlphaMode::Blend`], as Bevy doesn't have to sort objects + /// when it's in use. It's especially useful for complex transparent objects + /// like foliage. + /// + /// [alpha to coverage]: https://en.wikipedia.org/wiki/Alpha_to_coverage + AlphaToCoverage, + /// Combines the color of the fragments with the colors behind them in an + /// additive process, (i.e. like light) producing lighter results. + /// + /// Black produces no effect. Alpha values can be used to modulate the result. + /// + /// Useful for effects like holograms, ghosts, lasers and other energy beams. + Add, + /// Combines the color of the fragments with the colors behind them in a + /// multiplicative process, (i.e. like pigments) producing darker results. + /// + /// White produces no effect. Alpha values can be used to modulate the result. + /// + /// Useful for effects like stained glass, window tint film and some colored liquids. + Multiply, +} + +impl Eq for AlphaMode {} diff --git a/third_party/bevy_render/src/batching/gpu_preprocessing.rs b/third_party/bevy_render/src/batching/gpu_preprocessing.rs new file mode 100644 index 0000000..71ba6a8 --- /dev/null +++ b/third_party/bevy_render/src/batching/gpu_preprocessing.rs @@ -0,0 +1,2187 @@ +//! Batching functionality when GPU preprocessing is in use. + +use core::{any::TypeId, marker::PhantomData, mem}; + +use bevy_app::{App, Plugin}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + prelude::Entity, + query::{Has, With}, + resource::Resource, + schedule::IntoScheduleConfigs as _, + system::{Query, Res, ResMut, StaticSystemParam}, + world::{FromWorld, World}, +}; +use bevy_encase_derive::ShaderType; +use bevy_math::UVec4; +use bevy_platform::collections::{hash_map::Entry, HashMap, HashSet}; +use bevy_tasks::ComputeTaskPool; +use bevy_utils::{default, TypeIdMap}; +use bytemuck::{Pod, Zeroable}; +use encase::{internal::WriteInto, ShaderSize}; +use indexmap::IndexMap; +use nonmax::NonMaxU32; +use tracing::{error, info}; +use wgpu::{BindingResource, BufferUsages, DownlevelFlags, Features}; + +use crate::{ + experimental::occlusion_culling::OcclusionCulling, + render_phase::{ + BinnedPhaseItem, BinnedRenderPhaseBatch, BinnedRenderPhaseBatchSet, + BinnedRenderPhaseBatchSets, CachedRenderPipelinePhaseItem, PhaseItem, + PhaseItemBatchSetKey as _, PhaseItemExtraIndex, RenderBin, SortedPhaseItem, + SortedRenderPhase, UnbatchableBinnedEntityIndices, ViewBinnedRenderPhases, + ViewSortedRenderPhases, + }, + render_resource::{Buffer, GpuArrayBufferable, RawBufferVec, UninitBufferVec}, + renderer::{RenderAdapter, RenderAdapterInfo, RenderDevice, RenderQueue, WgpuWrapper}, + sync_world::MainEntity, + view::{ExtractedView, NoIndirectDrawing, RetainedViewEntity}, + Render, RenderApp, RenderDebugFlags, RenderSystems, +}; + +use super::{BatchMeta, GetBatchData, GetFullBatchData}; + +#[derive(Default)] +pub struct BatchingPlugin { + /// Debugging flags that can optionally be set when constructing the renderer. + pub debug_flags: RenderDebugFlags, +} + +impl Plugin for BatchingPlugin { + fn build(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .insert_resource(IndirectParametersBuffers::new( + self.debug_flags + .contains(RenderDebugFlags::ALLOW_COPIES_FROM_INDIRECT_PARAMETERS), + )) + .add_systems( + Render, + write_indirect_parameters_buffers.in_set(RenderSystems::PrepareResourcesFlush), + ) + .add_systems( + Render, + clear_indirect_parameters_buffers.in_set(RenderSystems::ManageViews), + ); + } + + fn finish(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app.init_resource::(); + } +} + +/// Records whether GPU preprocessing and/or GPU culling are supported on the +/// device. +/// +/// No GPU preprocessing is supported on WebGL because of the lack of compute +/// shader support. GPU preprocessing is supported on DirectX 12, but due to [a +/// `wgpu` limitation] GPU culling is not. +/// +/// [a `wgpu` limitation]: https://github.com/gfx-rs/wgpu/issues/2471 +#[derive(Clone, Copy, PartialEq, Resource)] +pub struct GpuPreprocessingSupport { + /// The maximum amount of GPU preprocessing available on this platform. + pub max_supported_mode: GpuPreprocessingMode, +} + +impl GpuPreprocessingSupport { + /// Returns true if this GPU preprocessing support level isn't `None`. + #[inline] + pub fn is_available(&self) -> bool { + self.max_supported_mode != GpuPreprocessingMode::None + } + + /// Returns the given GPU preprocessing mode, capped to the current + /// preprocessing mode. + pub fn min(&self, mode: GpuPreprocessingMode) -> GpuPreprocessingMode { + match (self.max_supported_mode, mode) { + (GpuPreprocessingMode::None, _) | (_, GpuPreprocessingMode::None) => { + GpuPreprocessingMode::None + } + (mode, GpuPreprocessingMode::Culling) | (GpuPreprocessingMode::Culling, mode) => mode, + (GpuPreprocessingMode::PreprocessingOnly, GpuPreprocessingMode::PreprocessingOnly) => { + GpuPreprocessingMode::PreprocessingOnly + } + } + } + + /// Returns true if GPU culling is supported on this platform. + pub fn is_culling_supported(&self) -> bool { + self.max_supported_mode == GpuPreprocessingMode::Culling + } +} + +/// The amount of GPU preprocessing (compute and indirect draw) that we do. +#[derive(Clone, Copy, PartialEq)] +pub enum GpuPreprocessingMode { + /// No GPU preprocessing is in use at all. + /// + /// This is used when GPU compute isn't available. + None, + + /// GPU preprocessing is in use, but GPU culling isn't. + /// + /// This is used when the [`NoIndirectDrawing`] component is present on the + /// camera. + PreprocessingOnly, + + /// Both GPU preprocessing and GPU culling are in use. + /// + /// This is used by default. + Culling, +} + +/// The GPU buffers holding the data needed to render batches. +/// +/// For example, in the 3D PBR pipeline this holds `MeshUniform`s, which are the +/// `BD` type parameter in that mode. +/// +/// We have a separate *buffer data input* type (`BDI`) here, which a compute +/// shader is expected to expand to the full buffer data (`BD`) type. GPU +/// uniform building is generally faster and uses less system RAM to VRAM bus +/// bandwidth, but only implemented for some pipelines (for example, not in the +/// 2D pipeline at present) and only when compute shader is available. +#[derive(Resource)] +pub struct BatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, + BDI: Pod + Default, +{ + /// The uniform data inputs for the current frame. + /// + /// These are uploaded during the extraction phase. + pub current_input_buffer: InstanceInputUniformBuffer, + + /// The uniform data inputs for the previous frame. + /// + /// The indices don't generally line up between `current_input_buffer` + /// and `previous_input_buffer`, because, among other reasons, entities + /// can spawn or despawn between frames. Instead, each current buffer + /// data input uniform is expected to contain the index of the + /// corresponding buffer data input uniform in this list. + pub previous_input_buffer: InstanceInputUniformBuffer, + + /// The data needed to render buffers for each phase. + /// + /// The keys of this map are the type IDs of each phase: e.g. `Opaque3d`, + /// `AlphaMask3d`, etc. + pub phase_instance_buffers: TypeIdMap>, +} + +impl Default for BatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, + BDI: Pod + Sync + Send + Default + 'static, +{ + fn default() -> Self { + BatchedInstanceBuffers { + current_input_buffer: InstanceInputUniformBuffer::new(), + previous_input_buffer: InstanceInputUniformBuffer::new(), + phase_instance_buffers: HashMap::default(), + } + } +} + +/// The GPU buffers holding the data needed to render batches for a single +/// phase. +/// +/// These are split out per phase so that we can run the phases in parallel. +/// This is the version of the structure that has a type parameter, which +/// enables Bevy's scheduler to run the batching operations for the different +/// phases in parallel. +/// +/// See the documentation for [`BatchedInstanceBuffers`] for more information. +#[derive(Resource)] +pub struct PhaseBatchedInstanceBuffers +where + PI: PhaseItem, + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + /// The buffers for this phase. + pub buffers: UntypedPhaseBatchedInstanceBuffers, + phantom: PhantomData, +} + +impl Default for PhaseBatchedInstanceBuffers +where + PI: PhaseItem, + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + fn default() -> Self { + PhaseBatchedInstanceBuffers { + buffers: UntypedPhaseBatchedInstanceBuffers::default(), + phantom: PhantomData, + } + } +} + +/// The GPU buffers holding the data needed to render batches for a single +/// phase, without a type parameter for that phase. +/// +/// Since this structure doesn't have a type parameter, it can be placed in +/// [`BatchedInstanceBuffers::phase_instance_buffers`]. +pub struct UntypedPhaseBatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + /// A storage area for the buffer data that the GPU compute shader is + /// expected to write to. + /// + /// There will be one entry for each index. + pub data_buffer: UninitBufferVec, + + /// The index of the buffer data in the current input buffer that + /// corresponds to each instance. + /// + /// This is keyed off each view. Each view has a separate buffer. + pub work_item_buffers: HashMap, + + /// A buffer that holds the number of indexed meshes that weren't visible in + /// the previous frame, when GPU occlusion culling is in use. + /// + /// There's one set of [`LatePreprocessWorkItemIndirectParameters`] per + /// view. Bevy uses this value to determine how many threads to dispatch to + /// check meshes that weren't visible next frame to see if they became newly + /// visible this frame. + pub late_indexed_indirect_parameters_buffer: + RawBufferVec, + + /// A buffer that holds the number of non-indexed meshes that weren't + /// visible in the previous frame, when GPU occlusion culling is in use. + /// + /// There's one set of [`LatePreprocessWorkItemIndirectParameters`] per + /// view. Bevy uses this value to determine how many threads to dispatch to + /// check meshes that weren't visible next frame to see if they became newly + /// visible this frame. + pub late_non_indexed_indirect_parameters_buffer: + RawBufferVec, +} + +/// Holds the GPU buffer of instance input data, which is the data about each +/// mesh instance that the CPU provides. +/// +/// `BDI` is the *buffer data input* type, which the GPU mesh preprocessing +/// shader is expected to expand to the full *buffer data* type. +pub struct InstanceInputUniformBuffer +where + BDI: Pod + Default, +{ + /// The buffer containing the data that will be uploaded to the GPU. + buffer: RawBufferVec, + + /// Indices of slots that are free within the buffer. + /// + /// When adding data, we preferentially overwrite these slots first before + /// growing the buffer itself. + free_uniform_indices: Vec, +} + +impl InstanceInputUniformBuffer +where + BDI: Pod + Default, +{ + /// Creates a new, empty buffer. + pub fn new() -> InstanceInputUniformBuffer { + InstanceInputUniformBuffer { + buffer: RawBufferVec::new(BufferUsages::STORAGE), + free_uniform_indices: vec![], + } + } + + /// Clears the buffer and entity list out. + pub fn clear(&mut self) { + self.buffer.clear(); + self.free_uniform_indices.clear(); + } + + /// Returns the [`RawBufferVec`] corresponding to this input uniform buffer. + #[inline] + pub fn buffer(&self) -> &RawBufferVec { + &self.buffer + } + + /// Adds a new piece of buffered data to the uniform buffer and returns its + /// index. + pub fn add(&mut self, element: BDI) -> u32 { + match self.free_uniform_indices.pop() { + Some(uniform_index) => { + self.buffer.values_mut()[uniform_index as usize] = element; + uniform_index + } + None => self.buffer.push(element) as u32, + } + } + + /// Removes a piece of buffered data from the uniform buffer. + /// + /// This simply marks the data as free. + pub fn remove(&mut self, uniform_index: u32) { + self.free_uniform_indices.push(uniform_index); + } + + /// Returns the piece of buffered data at the given index. + /// + /// Returns [`None`] if the index is out of bounds or the data is removed. + pub fn get(&self, uniform_index: u32) -> Option { + if (uniform_index as usize) >= self.buffer.len() + || self.free_uniform_indices.contains(&uniform_index) + { + None + } else { + Some(self.get_unchecked(uniform_index)) + } + } + + /// Returns the piece of buffered data at the given index. + /// Can return data that has previously been removed. + /// + /// # Panics + /// if `uniform_index` is not in bounds of [`Self::buffer`]. + pub fn get_unchecked(&self, uniform_index: u32) -> BDI { + self.buffer.values()[uniform_index as usize] + } + + /// Stores a piece of buffered data at the given index. + /// + /// # Panics + /// if `uniform_index` is not in bounds of [`Self::buffer`]. + pub fn set(&mut self, uniform_index: u32, element: BDI) { + self.buffer.values_mut()[uniform_index as usize] = element; + } + + // Ensures that the buffers are nonempty, which the GPU requires before an + // upload can take place. + pub fn ensure_nonempty(&mut self) { + if self.buffer.is_empty() { + self.buffer.push(default()); + } + } + + /// Returns the number of instances in this buffer. + pub fn len(&self) -> usize { + self.buffer.len() + } + + /// Returns true if this buffer has no instances or false if it contains any + /// instances. + pub fn is_empty(&self) -> bool { + self.buffer.is_empty() + } + + /// Consumes this [`InstanceInputUniformBuffer`] and returns the raw buffer + /// ready to be uploaded to the GPU. + pub fn into_buffer(self) -> RawBufferVec { + self.buffer + } +} + +impl Default for InstanceInputUniformBuffer +where + BDI: Pod + Default, +{ + fn default() -> Self { + Self::new() + } +} + +/// The buffer of GPU preprocessing work items for a single view. +#[cfg_attr( + not(target_arch = "wasm32"), + expect( + clippy::large_enum_variant, + reason = "See https://github.com/bevyengine/bevy/issues/19220" + ) +)] +pub enum PreprocessWorkItemBuffers { + /// The work items we use if we aren't using indirect drawing. + /// + /// Because we don't have to separate indexed from non-indexed meshes in + /// direct mode, we only have a single buffer here. + Direct(RawBufferVec), + + /// The buffer of work items we use if we are using indirect drawing. + /// + /// We need to separate out indexed meshes from non-indexed meshes in this + /// case because the indirect parameters for these two types of meshes have + /// different sizes. + Indirect { + /// The buffer of work items corresponding to indexed meshes. + indexed: RawBufferVec, + /// The buffer of work items corresponding to non-indexed meshes. + non_indexed: RawBufferVec, + /// The work item buffers we use when GPU occlusion culling is in use. + gpu_occlusion_culling: Option, + }, +} + +/// The work item buffers we use when GPU occlusion culling is in use. +pub struct GpuOcclusionCullingWorkItemBuffers { + /// The buffer of work items corresponding to indexed meshes. + pub late_indexed: UninitBufferVec, + /// The buffer of work items corresponding to non-indexed meshes. + pub late_non_indexed: UninitBufferVec, + /// The offset into the + /// [`UntypedPhaseBatchedInstanceBuffers::late_indexed_indirect_parameters_buffer`] + /// where this view's indirect dispatch counts for indexed meshes live. + pub late_indirect_parameters_indexed_offset: u32, + /// The offset into the + /// [`UntypedPhaseBatchedInstanceBuffers::late_non_indexed_indirect_parameters_buffer`] + /// where this view's indirect dispatch counts for non-indexed meshes live. + pub late_indirect_parameters_non_indexed_offset: u32, +} + +/// A GPU-side data structure that stores the number of workgroups to dispatch +/// for the second phase of GPU occlusion culling. +/// +/// The late mesh preprocessing phase checks meshes that weren't visible frame +/// to determine if they're potentially visible this frame. +#[derive(Clone, Copy, ShaderType, Pod, Zeroable)] +#[repr(C)] +pub struct LatePreprocessWorkItemIndirectParameters { + /// The number of workgroups to dispatch. + /// + /// This will be equal to `work_item_count / 64`, rounded *up*. + dispatch_x: u32, + /// The number of workgroups along the abstract Y axis to dispatch: always + /// 1. + dispatch_y: u32, + /// The number of workgroups along the abstract Z axis to dispatch: always + /// 1. + dispatch_z: u32, + /// The actual number of work items. + /// + /// The GPU indirect dispatch doesn't read this, but it's used internally to + /// determine the actual number of work items that exist in the late + /// preprocessing work item buffer. + work_item_count: u32, + /// Padding to 64-byte boundaries for some hardware. + pad: UVec4, +} + +impl Default for LatePreprocessWorkItemIndirectParameters { + fn default() -> LatePreprocessWorkItemIndirectParameters { + LatePreprocessWorkItemIndirectParameters { + dispatch_x: 0, + dispatch_y: 1, + dispatch_z: 1, + work_item_count: 0, + pad: default(), + } + } +} + +/// Returns the set of work item buffers for the given view, first creating it +/// if necessary. +/// +/// Bevy uses work item buffers to tell the mesh preprocessing compute shader +/// which meshes are to be drawn. +/// +/// You may need to call this function if you're implementing your own custom +/// render phases. See the `specialized_mesh_pipeline` example. +pub fn get_or_create_work_item_buffer<'a, I>( + work_item_buffers: &'a mut HashMap, + view: RetainedViewEntity, + no_indirect_drawing: bool, + enable_gpu_occlusion_culling: bool, +) -> &'a mut PreprocessWorkItemBuffers +where + I: 'static, +{ + let preprocess_work_item_buffers = match work_item_buffers.entry(view) { + Entry::Occupied(occupied_entry) => occupied_entry.into_mut(), + Entry::Vacant(vacant_entry) => { + if no_indirect_drawing { + vacant_entry.insert(PreprocessWorkItemBuffers::Direct(RawBufferVec::new( + BufferUsages::STORAGE, + ))) + } else { + vacant_entry.insert(PreprocessWorkItemBuffers::Indirect { + indexed: RawBufferVec::new(BufferUsages::STORAGE), + non_indexed: RawBufferVec::new(BufferUsages::STORAGE), + // We fill this in below if `enable_gpu_occlusion_culling` + // is set. + gpu_occlusion_culling: None, + }) + } + } + }; + + // Initialize the GPU occlusion culling buffers if necessary. + if let PreprocessWorkItemBuffers::Indirect { + ref mut gpu_occlusion_culling, + .. + } = *preprocess_work_item_buffers + { + match ( + enable_gpu_occlusion_culling, + gpu_occlusion_culling.is_some(), + ) { + (false, false) | (true, true) => {} + (false, true) => { + *gpu_occlusion_culling = None; + } + (true, false) => { + *gpu_occlusion_culling = Some(GpuOcclusionCullingWorkItemBuffers { + late_indexed: UninitBufferVec::new(BufferUsages::STORAGE), + late_non_indexed: UninitBufferVec::new(BufferUsages::STORAGE), + late_indirect_parameters_indexed_offset: 0, + late_indirect_parameters_non_indexed_offset: 0, + }); + } + } + } + + preprocess_work_item_buffers +} + +/// Initializes work item buffers for a phase in preparation for a new frame. +pub fn init_work_item_buffers( + work_item_buffers: &mut PreprocessWorkItemBuffers, + late_indexed_indirect_parameters_buffer: &'_ mut RawBufferVec< + LatePreprocessWorkItemIndirectParameters, + >, + late_non_indexed_indirect_parameters_buffer: &'_ mut RawBufferVec< + LatePreprocessWorkItemIndirectParameters, + >, +) { + // Add the offsets for indirect parameters that the late phase of mesh + // preprocessing writes to. + if let PreprocessWorkItemBuffers::Indirect { + gpu_occlusion_culling: + Some(GpuOcclusionCullingWorkItemBuffers { + ref mut late_indirect_parameters_indexed_offset, + ref mut late_indirect_parameters_non_indexed_offset, + .. + }), + .. + } = *work_item_buffers + { + *late_indirect_parameters_indexed_offset = late_indexed_indirect_parameters_buffer + .push(LatePreprocessWorkItemIndirectParameters::default()) + as u32; + *late_indirect_parameters_non_indexed_offset = late_non_indexed_indirect_parameters_buffer + .push(LatePreprocessWorkItemIndirectParameters::default()) + as u32; + } +} + +impl PreprocessWorkItemBuffers { + /// Adds a new work item to the appropriate buffer. + /// + /// `indexed` specifies whether the work item corresponds to an indexed + /// mesh. + pub fn push(&mut self, indexed: bool, preprocess_work_item: PreprocessWorkItem) { + match *self { + PreprocessWorkItemBuffers::Direct(ref mut buffer) => { + buffer.push(preprocess_work_item); + } + PreprocessWorkItemBuffers::Indirect { + indexed: ref mut indexed_buffer, + non_indexed: ref mut non_indexed_buffer, + ref mut gpu_occlusion_culling, + } => { + if indexed { + indexed_buffer.push(preprocess_work_item); + } else { + non_indexed_buffer.push(preprocess_work_item); + } + + if let Some(ref mut gpu_occlusion_culling) = *gpu_occlusion_culling { + if indexed { + gpu_occlusion_culling.late_indexed.add(); + } else { + gpu_occlusion_culling.late_non_indexed.add(); + } + } + } + } + } + + /// Clears out the GPU work item buffers in preparation for a new frame. + pub fn clear(&mut self) { + match *self { + PreprocessWorkItemBuffers::Direct(ref mut buffer) => { + buffer.clear(); + } + PreprocessWorkItemBuffers::Indirect { + indexed: ref mut indexed_buffer, + non_indexed: ref mut non_indexed_buffer, + ref mut gpu_occlusion_culling, + } => { + indexed_buffer.clear(); + non_indexed_buffer.clear(); + + if let Some(ref mut gpu_occlusion_culling) = *gpu_occlusion_culling { + gpu_occlusion_culling.late_indexed.clear(); + gpu_occlusion_culling.late_non_indexed.clear(); + gpu_occlusion_culling.late_indirect_parameters_indexed_offset = 0; + gpu_occlusion_culling.late_indirect_parameters_non_indexed_offset = 0; + } + } + } + } +} + +/// One invocation of the preprocessing shader: i.e. one mesh instance in a +/// view. +#[derive(Clone, Copy, Default, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct PreprocessWorkItem { + /// The index of the batch input data in the input buffer that the shader + /// reads from. + pub input_index: u32, + + /// In direct mode, the index of the mesh uniform; in indirect mode, the + /// index of the [`IndirectParametersGpuMetadata`]. + /// + /// In indirect mode, this is the index of the + /// [`IndirectParametersGpuMetadata`] in the + /// `IndirectParametersBuffers::indexed_metadata` or + /// `IndirectParametersBuffers::non_indexed_metadata`. + pub output_or_indirect_parameters_index: u32, +} + +/// The `wgpu` indirect parameters structure that specifies a GPU draw command. +/// +/// This is the variant for indexed meshes. We generate the instances of this +/// structure in the `build_indirect_params.wgsl` compute shader. +#[derive(Clone, Copy, Debug, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct IndirectParametersIndexed { + /// The number of indices that this mesh has. + pub index_count: u32, + /// The number of instances we are to draw. + pub instance_count: u32, + /// The offset of the first index for this mesh in the index buffer slab. + pub first_index: u32, + /// The offset of the first vertex for this mesh in the vertex buffer slab. + pub base_vertex: u32, + /// The index of the first mesh instance in the `MeshUniform` buffer. + pub first_instance: u32, +} + +/// The `wgpu` indirect parameters structure that specifies a GPU draw command. +/// +/// This is the variant for non-indexed meshes. We generate the instances of +/// this structure in the `build_indirect_params.wgsl` compute shader. +#[derive(Clone, Copy, Debug, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct IndirectParametersNonIndexed { + /// The number of vertices that this mesh has. + pub vertex_count: u32, + /// The number of instances we are to draw. + pub instance_count: u32, + /// The offset of the first vertex for this mesh in the vertex buffer slab. + pub base_vertex: u32, + /// The index of the first mesh instance in the `Mesh` buffer. + pub first_instance: u32, +} + +/// A structure, initialized on CPU and read on GPU, that contains metadata +/// about each batch. +/// +/// Each batch will have one instance of this structure. +#[derive(Clone, Copy, Default, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct IndirectParametersCpuMetadata { + /// The index of the first instance of this mesh in the array of + /// `MeshUniform`s. + /// + /// Note that this is the *first* output index in this batch. Since each + /// instance of this structure refers to arbitrarily many instances, the + /// `MeshUniform`s corresponding to this batch span the indices + /// `base_output_index..(base_output_index + instance_count)`. + pub base_output_index: u32, + + /// The index of the batch set that this batch belongs to in the + /// [`IndirectBatchSet`] buffer. + /// + /// A *batch set* is a set of meshes that may be multi-drawn together. + /// Multiple batches (and therefore multiple instances of + /// [`IndirectParametersGpuMetadata`] structures) can be part of the same + /// batch set. + pub batch_set_index: u32, +} + +/// A structure, written and read GPU, that records how many instances of each +/// mesh are actually to be drawn. +/// +/// The GPU mesh preprocessing shader increments the +/// [`Self::early_instance_count`] and [`Self::late_instance_count`] as it +/// determines that meshes are visible. The indirect parameter building shader +/// reads this metadata in order to construct the indirect draw parameters. +/// +/// Each batch will have one instance of this structure. +#[derive(Clone, Copy, Default, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct IndirectParametersGpuMetadata { + /// The index of the first mesh in this batch in the array of + /// `MeshInputUniform`s. + pub mesh_index: u32, + + /// The number of instances that were judged visible last frame. + /// + /// The CPU sets this value to 0, and the GPU mesh preprocessing shader + /// increments it as it culls mesh instances. + pub early_instance_count: u32, + + /// The number of instances that have been judged potentially visible this + /// frame that weren't in the last frame's potentially visible set. + /// + /// The CPU sets this value to 0, and the GPU mesh preprocessing shader + /// increments it as it culls mesh instances. + pub late_instance_count: u32, +} + +/// A structure, shared between CPU and GPU, that holds the number of on-GPU +/// indirect draw commands for each *batch set*. +/// +/// A *batch set* is a set of meshes that may be multi-drawn together. +/// +/// If the current hardware and driver support `multi_draw_indirect_count`, the +/// indirect parameters building shader increments +/// [`Self::indirect_parameters_count`] as it generates indirect parameters. The +/// `multi_draw_indirect_count` command reads +/// [`Self::indirect_parameters_count`] in order to determine how many commands +/// belong to each batch set. +#[derive(Clone, Copy, Default, Pod, Zeroable, ShaderType)] +#[repr(C)] +pub struct IndirectBatchSet { + /// The number of indirect parameter commands (i.e. batches) in this batch + /// set. + /// + /// The CPU sets this value to 0 before uploading this structure to GPU. The + /// indirect parameters building shader increments this value as it creates + /// indirect parameters. Then the `multi_draw_indirect_count` command reads + /// this value in order to determine how many indirect draw commands to + /// process. + pub indirect_parameters_count: u32, + + /// The offset within the `IndirectParametersBuffers::indexed_data` or + /// `IndirectParametersBuffers::non_indexed_data` of the first indirect draw + /// command for this batch set. + /// + /// The CPU fills out this value. + pub indirect_parameters_base: u32, +} + +/// The buffers containing all the information that indirect draw commands +/// (`multi_draw_indirect`, `multi_draw_indirect_count`) use to draw the scene. +/// +/// In addition to the indirect draw buffers themselves, this structure contains +/// the buffers that store [`IndirectParametersGpuMetadata`], which are the +/// structures that culling writes to so that the indirect parameter building +/// pass can determine how many meshes are actually to be drawn. +/// +/// These buffers will remain empty if indirect drawing isn't in use. +#[derive(Resource, Deref, DerefMut)] +pub struct IndirectParametersBuffers { + /// A mapping from a phase type ID to the indirect parameters buffers for + /// that phase. + /// + /// Examples of phase type IDs are `Opaque3d` and `AlphaMask3d`. + #[deref] + pub buffers: TypeIdMap, + /// If true, this sets the `COPY_SRC` flag on indirect draw parameters so + /// that they can be read back to CPU. + /// + /// This is a debugging feature that may reduce performance. It primarily + /// exists for the `occlusion_culling` example. + pub allow_copies_from_indirect_parameter_buffers: bool, +} + +impl IndirectParametersBuffers { + /// Initializes a new [`IndirectParametersBuffers`] resource. + pub fn new(allow_copies_from_indirect_parameter_buffers: bool) -> IndirectParametersBuffers { + IndirectParametersBuffers { + buffers: TypeIdMap::default(), + allow_copies_from_indirect_parameter_buffers, + } + } +} + +/// The buffers containing all the information that indirect draw commands use +/// to draw the scene, for a single phase. +/// +/// This is the version of the structure that has a type parameter, so that the +/// batching for different phases can run in parallel. +/// +/// See the [`IndirectParametersBuffers`] documentation for more information. +#[derive(Resource)] +pub struct PhaseIndirectParametersBuffers +where + PI: PhaseItem, +{ + /// The indirect draw buffers for the phase. + pub buffers: UntypedPhaseIndirectParametersBuffers, + phantom: PhantomData, +} + +impl PhaseIndirectParametersBuffers +where + PI: PhaseItem, +{ + pub fn new(allow_copies_from_indirect_parameter_buffers: bool) -> Self { + PhaseIndirectParametersBuffers { + buffers: UntypedPhaseIndirectParametersBuffers::new( + allow_copies_from_indirect_parameter_buffers, + ), + phantom: PhantomData, + } + } +} + +/// The buffers containing all the information that indirect draw commands use +/// to draw the scene, for a single phase. +/// +/// This is the version of the structure that doesn't have a type parameter, so +/// that it can be inserted into [`IndirectParametersBuffers::buffers`] +/// +/// See the [`IndirectParametersBuffers`] documentation for more information. +pub struct UntypedPhaseIndirectParametersBuffers { + /// Information that indirect draw commands use to draw indexed meshes in + /// the scene. + pub indexed: MeshClassIndirectParametersBuffers, + /// Information that indirect draw commands use to draw non-indexed meshes + /// in the scene. + pub non_indexed: MeshClassIndirectParametersBuffers, +} + +impl UntypedPhaseIndirectParametersBuffers { + /// Creates the indirect parameters buffers. + pub fn new( + allow_copies_from_indirect_parameter_buffers: bool, + ) -> UntypedPhaseIndirectParametersBuffers { + let mut indirect_parameter_buffer_usages = BufferUsages::STORAGE | BufferUsages::INDIRECT; + if allow_copies_from_indirect_parameter_buffers { + indirect_parameter_buffer_usages |= BufferUsages::COPY_SRC; + } + + UntypedPhaseIndirectParametersBuffers { + non_indexed: MeshClassIndirectParametersBuffers::new( + allow_copies_from_indirect_parameter_buffers, + ), + indexed: MeshClassIndirectParametersBuffers::new( + allow_copies_from_indirect_parameter_buffers, + ), + } + } + + /// Reserves space for `count` new batches. + /// + /// The `indexed` parameter specifies whether the meshes that these batches + /// correspond to are indexed or not. + pub fn allocate(&mut self, indexed: bool, count: u32) -> u32 { + if indexed { + self.indexed.allocate(count) + } else { + self.non_indexed.allocate(count) + } + } + + /// Returns the number of batches currently allocated. + /// + /// The `indexed` parameter specifies whether the meshes that these batches + /// correspond to are indexed or not. + fn batch_count(&self, indexed: bool) -> usize { + if indexed { + self.indexed.batch_count() + } else { + self.non_indexed.batch_count() + } + } + + /// Returns the number of batch sets currently allocated. + /// + /// The `indexed` parameter specifies whether the meshes that these batch + /// sets correspond to are indexed or not. + pub fn batch_set_count(&self, indexed: bool) -> usize { + if indexed { + self.indexed.batch_sets.len() + } else { + self.non_indexed.batch_sets.len() + } + } + + /// Adds a new batch set to `Self::indexed_batch_sets` or + /// `Self::non_indexed_batch_sets` as appropriate. + /// + /// `indexed` specifies whether the meshes that these batch sets correspond + /// to are indexed or not. `indirect_parameters_base` specifies the offset + /// within `Self::indexed_data` or `Self::non_indexed_data` of the first + /// batch in this batch set. + #[inline] + pub fn add_batch_set(&mut self, indexed: bool, indirect_parameters_base: u32) { + if indexed { + self.indexed.batch_sets.push(IndirectBatchSet { + indirect_parameters_base, + indirect_parameters_count: 0, + }); + } else { + self.non_indexed.batch_sets.push(IndirectBatchSet { + indirect_parameters_base, + indirect_parameters_count: 0, + }); + } + } + + /// Returns the index that a newly-added batch set will have. + /// + /// The `indexed` parameter specifies whether the meshes in such a batch set + /// are indexed or not. + pub fn get_next_batch_set_index(&self, indexed: bool) -> Option { + NonMaxU32::new(self.batch_set_count(indexed) as u32) + } + + /// Clears out the buffers in preparation for a new frame. + pub fn clear(&mut self) { + self.indexed.clear(); + self.non_indexed.clear(); + } +} + +/// The buffers containing all the information that indirect draw commands use +/// to draw the scene, for a single mesh class (indexed or non-indexed), for a +/// single phase. +pub struct MeshClassIndirectParametersBuffers +where + IP: Clone + ShaderSize + WriteInto, +{ + /// The GPU buffer that stores the indirect draw parameters for the meshes. + /// + /// The indirect parameters building shader writes to this buffer, while the + /// `multi_draw_indirect` or `multi_draw_indirect_count` commands read from + /// it to perform the draws. + data: UninitBufferVec, + + /// The GPU buffer that holds the data used to construct indirect draw + /// parameters for meshes. + /// + /// The GPU mesh preprocessing shader writes to this buffer, and the + /// indirect parameters building shader reads this buffer to construct the + /// indirect draw parameters. + cpu_metadata: RawBufferVec, + + /// The GPU buffer that holds data built by the GPU used to construct + /// indirect draw parameters for meshes. + /// + /// The GPU mesh preprocessing shader writes to this buffer, and the + /// indirect parameters building shader reads this buffer to construct the + /// indirect draw parameters. + gpu_metadata: UninitBufferVec, + + /// The GPU buffer that holds the number of indirect draw commands for each + /// phase of each view, for meshes. + /// + /// The indirect parameters building shader writes to this buffer, and the + /// `multi_draw_indirect_count` command reads from it in order to know how + /// many indirect draw commands to process. + batch_sets: RawBufferVec, +} + +impl MeshClassIndirectParametersBuffers +where + IP: Clone + ShaderSize + WriteInto, +{ + fn new( + allow_copies_from_indirect_parameter_buffers: bool, + ) -> MeshClassIndirectParametersBuffers { + let mut indirect_parameter_buffer_usages = BufferUsages::STORAGE | BufferUsages::INDIRECT; + if allow_copies_from_indirect_parameter_buffers { + indirect_parameter_buffer_usages |= BufferUsages::COPY_SRC; + } + + MeshClassIndirectParametersBuffers { + data: UninitBufferVec::new(indirect_parameter_buffer_usages), + cpu_metadata: RawBufferVec::new(BufferUsages::STORAGE), + gpu_metadata: UninitBufferVec::new(BufferUsages::STORAGE), + batch_sets: RawBufferVec::new(indirect_parameter_buffer_usages), + } + } + + /// Returns the GPU buffer that stores the indirect draw parameters for + /// indexed meshes. + /// + /// The indirect parameters building shader writes to this buffer, while the + /// `multi_draw_indirect` or `multi_draw_indirect_count` commands read from + /// it to perform the draws. + #[inline] + pub fn data_buffer(&self) -> Option<&Buffer> { + self.data.buffer() + } + + /// Returns the GPU buffer that holds the CPU-constructed data used to + /// construct indirect draw parameters for meshes. + /// + /// The CPU writes to this buffer, and the indirect parameters building + /// shader reads this buffer to construct the indirect draw parameters. + #[inline] + pub fn cpu_metadata_buffer(&self) -> Option<&Buffer> { + self.cpu_metadata.buffer() + } + + /// Returns the GPU buffer that holds the GPU-constructed data used to + /// construct indirect draw parameters for meshes. + /// + /// The GPU mesh preprocessing shader writes to this buffer, and the + /// indirect parameters building shader reads this buffer to construct the + /// indirect draw parameters. + #[inline] + pub fn gpu_metadata_buffer(&self) -> Option<&Buffer> { + self.gpu_metadata.buffer() + } + + /// Returns the GPU buffer that holds the number of indirect draw commands + /// for each phase of each view. + /// + /// The indirect parameters building shader writes to this buffer, and the + /// `multi_draw_indirect_count` command reads from it in order to know how + /// many indirect draw commands to process. + #[inline] + pub fn batch_sets_buffer(&self) -> Option<&Buffer> { + self.batch_sets.buffer() + } + + /// Reserves space for `count` new batches. + /// + /// This allocates in the [`Self::cpu_metadata`], [`Self::gpu_metadata`], + /// and [`Self::data`] buffers. + fn allocate(&mut self, count: u32) -> u32 { + let length = self.data.len(); + self.cpu_metadata.reserve_internal(count as usize); + self.gpu_metadata.add_multiple(count as usize); + for _ in 0..count { + self.data.add(); + self.cpu_metadata + .push(IndirectParametersCpuMetadata::default()); + } + length as u32 + } + + /// Sets the [`IndirectParametersCpuMetadata`] for the mesh at the given + /// index. + pub fn set(&mut self, index: u32, value: IndirectParametersCpuMetadata) { + self.cpu_metadata.set(index, value); + } + + /// Returns the number of batches corresponding to meshes that are currently + /// allocated. + #[inline] + pub fn batch_count(&self) -> usize { + self.data.len() + } + + /// Clears out all the buffers in preparation for a new frame. + pub fn clear(&mut self) { + self.data.clear(); + self.cpu_metadata.clear(); + self.gpu_metadata.clear(); + self.batch_sets.clear(); + } +} + +impl Default for IndirectParametersBuffers { + fn default() -> Self { + // By default, we don't allow GPU indirect parameter mapping, since + // that's a debugging option. + Self::new(false) + } +} + +impl FromWorld for GpuPreprocessingSupport { + fn from_world(world: &mut World) -> Self { + let adapter = world.resource::(); + let device = world.resource::(); + + // Filter Android drivers that are incompatible with GPU preprocessing: + // - We filter out Adreno 730 and earlier GPUs (except 720, as it's newer + // than 730). + // - We filter out Mali GPUs with driver versions lower than 48. + fn is_non_supported_android_device(adapter_info: &RenderAdapterInfo) -> bool { + crate::get_adreno_model(adapter_info).is_some_and(|model| model != 720 && model <= 730) + || crate::get_mali_driver_version(adapter_info).is_some_and(|version| version < 48) + } + + let culling_feature_support = device + .features() + .contains(Features::INDIRECT_FIRST_INSTANCE | Features::PUSH_CONSTANTS); + // Depth downsampling for occlusion culling requires 12 textures + let limit_support = device.limits().max_storage_textures_per_shader_stage >= 12 && + // Even if the adapter supports compute, we might be simulating a lack of + // compute via device limits (see `WgpuSettingsPriority::WebGL2` and + // `wgpu::Limits::downlevel_webgl2_defaults()`). This will have set all the + // `max_compute_*` limits to zero, so we arbitrarily pick one as a canary. + device.limits().max_compute_workgroup_storage_size != 0; + + let downlevel_support = adapter + .get_downlevel_capabilities() + .flags + .contains(DownlevelFlags::COMPUTE_SHADERS); + + let adapter_info = RenderAdapterInfo(WgpuWrapper::new(adapter.get_info())); + + let max_supported_mode = if device.limits().max_compute_workgroup_size_x == 0 + || is_non_supported_android_device(&adapter_info) + || adapter_info.backend == wgpu::Backend::Gl + { + info!( + "GPU preprocessing is not supported on this device. \ + Falling back to CPU preprocessing.", + ); + GpuPreprocessingMode::None + } else if !(culling_feature_support && limit_support && downlevel_support) { + info!("Some GPU preprocessing are limited on this device."); + GpuPreprocessingMode::PreprocessingOnly + } else { + info!("GPU preprocessing is fully supported on this device."); + GpuPreprocessingMode::Culling + }; + + GpuPreprocessingSupport { max_supported_mode } + } +} + +impl BatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, + BDI: Pod + Sync + Send + Default + 'static, +{ + /// Creates new buffers. + pub fn new() -> Self { + Self::default() + } + + /// Clears out the buffers in preparation for a new frame. + pub fn clear(&mut self) { + for phase_instance_buffer in self.phase_instance_buffers.values_mut() { + phase_instance_buffer.clear(); + } + } +} + +impl UntypedPhaseBatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + pub fn new() -> Self { + UntypedPhaseBatchedInstanceBuffers { + data_buffer: UninitBufferVec::new(BufferUsages::STORAGE), + work_item_buffers: HashMap::default(), + late_indexed_indirect_parameters_buffer: RawBufferVec::new( + BufferUsages::STORAGE | BufferUsages::INDIRECT, + ), + late_non_indexed_indirect_parameters_buffer: RawBufferVec::new( + BufferUsages::STORAGE | BufferUsages::INDIRECT, + ), + } + } + + /// Returns the binding of the buffer that contains the per-instance data. + /// + /// This buffer needs to be filled in via a compute shader. + pub fn instance_data_binding(&self) -> Option> { + self.data_buffer + .buffer() + .map(|buffer| buffer.as_entire_binding()) + } + + /// Clears out the buffers in preparation for a new frame. + pub fn clear(&mut self) { + self.data_buffer.clear(); + self.late_indexed_indirect_parameters_buffer.clear(); + self.late_non_indexed_indirect_parameters_buffer.clear(); + + // Clear each individual set of buffers, but don't depopulate the hash + // table. We want to avoid reallocating these vectors every frame. + for view_work_item_buffers in self.work_item_buffers.values_mut() { + view_work_item_buffers.clear(); + } + } +} + +impl Default for UntypedPhaseBatchedInstanceBuffers +where + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + fn default() -> Self { + Self::new() + } +} + +/// Information about a render batch that we're building up during a sorted +/// render phase. +struct SortedRenderBatch +where + F: GetBatchData, +{ + /// The index of the first phase item in this batch in the list of phase + /// items. + phase_item_start_index: u32, + + /// The index of the first instance in this batch in the instance buffer. + instance_start_index: u32, + + /// True if the mesh in question has an index buffer; false otherwise. + indexed: bool, + + /// The index of the indirect parameters for this batch in the + /// [`IndirectParametersBuffers`]. + /// + /// If CPU culling is being used, then this will be `None`. + indirect_parameters_index: Option, + + /// Metadata that can be used to determine whether an instance can be placed + /// into this batch. + /// + /// If `None`, the item inside is unbatchable. + meta: Option>, +} + +impl SortedRenderBatch +where + F: GetBatchData, +{ + /// Finalizes this batch and updates the [`SortedRenderPhase`] with the + /// appropriate indices. + /// + /// `instance_end_index` is the index of the last instance in this batch + /// plus one. + fn flush( + self, + instance_end_index: u32, + phase: &mut SortedRenderPhase, + phase_indirect_parameters_buffers: &mut UntypedPhaseIndirectParametersBuffers, + ) where + I: CachedRenderPipelinePhaseItem + SortedPhaseItem, + { + let (batch_range, batch_extra_index) = + phase.items[self.phase_item_start_index as usize].batch_range_and_extra_index_mut(); + *batch_range = self.instance_start_index..instance_end_index; + *batch_extra_index = match self.indirect_parameters_index { + Some(indirect_parameters_index) => PhaseItemExtraIndex::IndirectParametersIndex { + range: u32::from(indirect_parameters_index) + ..(u32::from(indirect_parameters_index) + 1), + batch_set_index: None, + }, + None => PhaseItemExtraIndex::None, + }; + if let Some(indirect_parameters_index) = self.indirect_parameters_index { + phase_indirect_parameters_buffers + .add_batch_set(self.indexed, indirect_parameters_index.into()); + } + } +} + +/// A system that runs early in extraction and clears out all the +/// [`BatchedInstanceBuffers`] for the frame. +/// +/// We have to run this during extraction because, if GPU preprocessing is in +/// use, the extraction phase will write to the mesh input uniform buffers +/// directly, so the buffers need to be cleared before then. +pub fn clear_batched_gpu_instance_buffers( + gpu_batched_instance_buffers: Option< + ResMut>, + >, +) where + GFBD: GetFullBatchData, +{ + // Don't clear the entire table, because that would delete the buffers, and + // we want to reuse those allocations. + if let Some(mut gpu_batched_instance_buffers) = gpu_batched_instance_buffers { + gpu_batched_instance_buffers.clear(); + } +} + +/// A system that removes GPU preprocessing work item buffers that correspond to +/// deleted [`ExtractedView`]s. +/// +/// This is a separate system from [`clear_batched_gpu_instance_buffers`] +/// because [`ExtractedView`]s aren't created until after the extraction phase +/// is completed. +pub fn delete_old_work_item_buffers( + mut gpu_batched_instance_buffers: ResMut< + BatchedInstanceBuffers, + >, + extracted_views: Query<&ExtractedView>, +) where + GFBD: GetFullBatchData, +{ + let retained_view_entities: HashSet<_> = extracted_views + .iter() + .map(|extracted_view| extracted_view.retained_view_entity) + .collect(); + for phase_instance_buffers in gpu_batched_instance_buffers + .phase_instance_buffers + .values_mut() + { + phase_instance_buffers + .work_item_buffers + .retain(|retained_view_entity, _| { + retained_view_entities.contains(retained_view_entity) + }); + } +} + +/// Batch the items in a sorted render phase, when GPU instance buffer building +/// is in use. This means comparing metadata needed to draw each phase item and +/// trying to combine the draws into a batch. +pub fn batch_and_prepare_sorted_render_phase( + mut phase_batched_instance_buffers: ResMut>, + mut phase_indirect_parameters_buffers: ResMut>, + mut sorted_render_phases: ResMut>, + mut views: Query<( + &ExtractedView, + Has, + Has, + )>, + system_param_item: StaticSystemParam, +) where + I: CachedRenderPipelinePhaseItem + SortedPhaseItem, + GFBD: GetFullBatchData, +{ + // We only process GPU-built batch data in this function. + let UntypedPhaseBatchedInstanceBuffers { + ref mut data_buffer, + ref mut work_item_buffers, + ref mut late_indexed_indirect_parameters_buffer, + ref mut late_non_indexed_indirect_parameters_buffer, + } = phase_batched_instance_buffers.buffers; + + for (extracted_view, no_indirect_drawing, gpu_occlusion_culling) in &mut views { + let Some(phase) = sorted_render_phases.get_mut(&extracted_view.retained_view_entity) else { + continue; + }; + + // Create the work item buffer if necessary. + let work_item_buffer = get_or_create_work_item_buffer::( + work_item_buffers, + extracted_view.retained_view_entity, + no_indirect_drawing, + gpu_occlusion_culling, + ); + + // Initialize those work item buffers in preparation for this new frame. + init_work_item_buffers( + work_item_buffer, + late_indexed_indirect_parameters_buffer, + late_non_indexed_indirect_parameters_buffer, + ); + + // Walk through the list of phase items, building up batches as we go. + let mut batch: Option> = None; + + for current_index in 0..phase.items.len() { + // Get the index of the input data, and comparison metadata, for + // this entity. + let item = &phase.items[current_index]; + let entity = item.main_entity(); + let item_is_indexed = item.indexed(); + let current_batch_input_index = + GFBD::get_index_and_compare_data(&system_param_item, entity); + + // Unpack that index and metadata. Note that it's possible for index + // and/or metadata to not be present, which signifies that this + // entity is unbatchable. In that case, we break the batch here. + // If the index isn't present the item is not part of this pipeline and so will be skipped. + let Some((current_input_index, current_meta)) = current_batch_input_index else { + // Break a batch if we need to. + if let Some(batch) = batch.take() { + batch.flush( + data_buffer.len() as u32, + phase, + &mut phase_indirect_parameters_buffers.buffers, + ); + } + + continue; + }; + let current_meta = + current_meta.map(|meta| BatchMeta::new(&phase.items[current_index], meta)); + + // Determine if this entity can be included in the batch we're + // building up. + let can_batch = batch.as_ref().is_some_and(|batch| { + // `None` for metadata indicates that the items are unbatchable. + match (¤t_meta, &batch.meta) { + (Some(current_meta), Some(batch_meta)) => current_meta == batch_meta, + (_, _) => false, + } + }); + + // Make space in the data buffer for this instance. + let output_index = data_buffer.add() as u32; + + // If we can't batch, break the existing batch and make a new one. + if !can_batch { + // Break a batch if we need to. + if let Some(batch) = batch.take() { + batch.flush( + output_index, + phase, + &mut phase_indirect_parameters_buffers.buffers, + ); + } + + let indirect_parameters_index = if no_indirect_drawing { + None + } else if item_is_indexed { + Some( + phase_indirect_parameters_buffers + .buffers + .indexed + .allocate(1), + ) + } else { + Some( + phase_indirect_parameters_buffers + .buffers + .non_indexed + .allocate(1), + ) + }; + + // Start a new batch. + if let Some(indirect_parameters_index) = indirect_parameters_index { + GFBD::write_batch_indirect_parameters_metadata( + item_is_indexed, + output_index, + None, + &mut phase_indirect_parameters_buffers.buffers, + indirect_parameters_index, + ); + }; + + batch = Some(SortedRenderBatch { + phase_item_start_index: current_index as u32, + instance_start_index: output_index, + indexed: item_is_indexed, + indirect_parameters_index: indirect_parameters_index.and_then(NonMaxU32::new), + meta: current_meta, + }); + } + + // Add a new preprocessing work item so that the preprocessing + // shader will copy the per-instance data over. + if let Some(batch) = batch.as_ref() { + work_item_buffer.push( + item_is_indexed, + PreprocessWorkItem { + input_index: current_input_index.into(), + output_or_indirect_parameters_index: match ( + no_indirect_drawing, + batch.indirect_parameters_index, + ) { + (true, _) => output_index, + (false, Some(indirect_parameters_index)) => { + indirect_parameters_index.into() + } + (false, None) => 0, + }, + }, + ); + } + } + + // Flush the final batch if necessary. + if let Some(batch) = batch.take() { + batch.flush( + data_buffer.len() as u32, + phase, + &mut phase_indirect_parameters_buffers.buffers, + ); + } + } +} + +/// Creates batches for a render phase that uses bins. +pub fn batch_and_prepare_binned_render_phase( + mut phase_batched_instance_buffers: ResMut>, + phase_indirect_parameters_buffers: ResMut>, + mut binned_render_phases: ResMut>, + mut views: Query< + ( + &ExtractedView, + Has, + Has, + ), + With, + >, + param: StaticSystemParam, +) where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + let system_param_item = param.into_inner(); + + let phase_indirect_parameters_buffers = phase_indirect_parameters_buffers.into_inner(); + + let UntypedPhaseBatchedInstanceBuffers { + ref mut data_buffer, + ref mut work_item_buffers, + ref mut late_indexed_indirect_parameters_buffer, + ref mut late_non_indexed_indirect_parameters_buffer, + } = phase_batched_instance_buffers.buffers; + + for (extracted_view, no_indirect_drawing, gpu_occlusion_culling) in &mut views { + let Some(phase) = binned_render_phases.get_mut(&extracted_view.retained_view_entity) else { + continue; + }; + + // Create the work item buffer if necessary; otherwise, just mark it as + // used this frame. + let work_item_buffer = get_or_create_work_item_buffer::( + work_item_buffers, + extracted_view.retained_view_entity, + no_indirect_drawing, + gpu_occlusion_culling, + ); + + // Initialize those work item buffers in preparation for this new frame. + init_work_item_buffers( + work_item_buffer, + late_indexed_indirect_parameters_buffer, + late_non_indexed_indirect_parameters_buffer, + ); + + // Prepare multidrawables. + + if let ( + &mut BinnedRenderPhaseBatchSets::MultidrawIndirect(ref mut batch_sets), + &mut PreprocessWorkItemBuffers::Indirect { + indexed: ref mut indexed_work_item_buffer, + non_indexed: ref mut non_indexed_work_item_buffer, + gpu_occlusion_culling: ref mut gpu_occlusion_culling_buffers, + }, + ) = (&mut phase.batch_sets, &mut *work_item_buffer) + { + let mut output_index = data_buffer.len() as u32; + + // Initialize the state for both indexed and non-indexed meshes. + let mut indexed_preparer: MultidrawableBatchSetPreparer = + MultidrawableBatchSetPreparer::new( + phase_indirect_parameters_buffers.buffers.batch_count(true) as u32, + phase_indirect_parameters_buffers + .buffers + .indexed + .batch_sets + .len() as u32, + ); + let mut non_indexed_preparer: MultidrawableBatchSetPreparer = + MultidrawableBatchSetPreparer::new( + phase_indirect_parameters_buffers.buffers.batch_count(false) as u32, + phase_indirect_parameters_buffers + .buffers + .non_indexed + .batch_sets + .len() as u32, + ); + + // Prepare each batch set. + for (batch_set_key, bins) in &phase.multidrawable_meshes { + if batch_set_key.indexed() { + indexed_preparer.prepare_multidrawable_binned_batch_set( + bins, + &mut output_index, + data_buffer, + indexed_work_item_buffer, + &mut phase_indirect_parameters_buffers.buffers.indexed, + batch_sets, + ); + } else { + non_indexed_preparer.prepare_multidrawable_binned_batch_set( + bins, + &mut output_index, + data_buffer, + non_indexed_work_item_buffer, + &mut phase_indirect_parameters_buffers.buffers.non_indexed, + batch_sets, + ); + } + } + + // Reserve space in the occlusion culling buffers, if necessary. + if let Some(gpu_occlusion_culling_buffers) = gpu_occlusion_culling_buffers { + gpu_occlusion_culling_buffers + .late_indexed + .add_multiple(indexed_preparer.work_item_count); + gpu_occlusion_culling_buffers + .late_non_indexed + .add_multiple(non_indexed_preparer.work_item_count); + } + } + + // Prepare batchables. + + for (key, bin) in &phase.batchable_meshes { + let mut batch: Option = None; + for (&main_entity, &input_index) in bin.entities() { + let output_index = data_buffer.add() as u32; + + match batch { + Some(ref mut batch) => { + batch.instance_range.end = output_index + 1; + + // Append to the current batch. + // + // If we're in indirect mode, then we write the first + // output index of this batch, so that we have a + // tightly-packed buffer if GPU culling discards some of + // the instances. Otherwise, we can just write the + // output index directly. + work_item_buffer.push( + key.0.indexed(), + PreprocessWorkItem { + input_index: *input_index, + output_or_indirect_parameters_index: match ( + no_indirect_drawing, + &batch.extra_index, + ) { + (true, _) => output_index, + ( + false, + PhaseItemExtraIndex::IndirectParametersIndex { + range: indirect_parameters_range, + .. + }, + ) => indirect_parameters_range.start, + (false, &PhaseItemExtraIndex::DynamicOffset(_)) + | (false, &PhaseItemExtraIndex::None) => 0, + }, + }, + ); + } + + None if !no_indirect_drawing => { + // Start a new batch, in indirect mode. + let indirect_parameters_index = phase_indirect_parameters_buffers + .buffers + .allocate(key.0.indexed(), 1); + let batch_set_index = phase_indirect_parameters_buffers + .buffers + .get_next_batch_set_index(key.0.indexed()); + + GFBD::write_batch_indirect_parameters_metadata( + key.0.indexed(), + output_index, + batch_set_index, + &mut phase_indirect_parameters_buffers.buffers, + indirect_parameters_index, + ); + work_item_buffer.push( + key.0.indexed(), + PreprocessWorkItem { + input_index: *input_index, + output_or_indirect_parameters_index: indirect_parameters_index, + }, + ); + batch = Some(BinnedRenderPhaseBatch { + representative_entity: (Entity::PLACEHOLDER, main_entity), + instance_range: output_index..output_index + 1, + extra_index: PhaseItemExtraIndex::IndirectParametersIndex { + range: indirect_parameters_index..(indirect_parameters_index + 1), + batch_set_index: None, + }, + }); + } + + None => { + // Start a new batch, in direct mode. + work_item_buffer.push( + key.0.indexed(), + PreprocessWorkItem { + input_index: *input_index, + output_or_indirect_parameters_index: output_index, + }, + ); + batch = Some(BinnedRenderPhaseBatch { + representative_entity: (Entity::PLACEHOLDER, main_entity), + instance_range: output_index..output_index + 1, + extra_index: PhaseItemExtraIndex::None, + }); + } + } + } + + if let Some(batch) = batch { + match phase.batch_sets { + BinnedRenderPhaseBatchSets::DynamicUniforms(_) => { + error!("Dynamic uniform batch sets shouldn't be used here"); + } + BinnedRenderPhaseBatchSets::Direct(ref mut vec) => { + vec.push(batch); + } + BinnedRenderPhaseBatchSets::MultidrawIndirect(ref mut vec) => { + // The Bevy renderer will never mark a mesh as batchable + // but not multidrawable if multidraw is in use. + // However, custom render pipelines might do so, such as + // the `specialized_mesh_pipeline` example. + vec.push(BinnedRenderPhaseBatchSet { + first_batch: batch, + batch_count: 1, + bin_key: key.1.clone(), + index: phase_indirect_parameters_buffers + .buffers + .batch_set_count(key.0.indexed()) + as u32, + }); + } + } + } + } + + // Prepare unbatchables. + for (key, unbatchables) in &mut phase.unbatchable_meshes { + // Allocate the indirect parameters if necessary. + let mut indirect_parameters_offset = if no_indirect_drawing { + None + } else if key.0.indexed() { + Some( + phase_indirect_parameters_buffers + .buffers + .indexed + .allocate(unbatchables.entities.len() as u32), + ) + } else { + Some( + phase_indirect_parameters_buffers + .buffers + .non_indexed + .allocate(unbatchables.entities.len() as u32), + ) + }; + + for main_entity in unbatchables.entities.keys() { + let Some(input_index) = GFBD::get_binned_index(&system_param_item, *main_entity) + else { + continue; + }; + let output_index = data_buffer.add() as u32; + + if let Some(ref mut indirect_parameters_index) = indirect_parameters_offset { + // We're in indirect mode, so add an indirect parameters + // index. + GFBD::write_batch_indirect_parameters_metadata( + key.0.indexed(), + output_index, + None, + &mut phase_indirect_parameters_buffers.buffers, + *indirect_parameters_index, + ); + work_item_buffer.push( + key.0.indexed(), + PreprocessWorkItem { + input_index: input_index.into(), + output_or_indirect_parameters_index: *indirect_parameters_index, + }, + ); + unbatchables + .buffer_indices + .add(UnbatchableBinnedEntityIndices { + instance_index: *indirect_parameters_index, + extra_index: PhaseItemExtraIndex::IndirectParametersIndex { + range: *indirect_parameters_index..(*indirect_parameters_index + 1), + batch_set_index: None, + }, + }); + phase_indirect_parameters_buffers + .buffers + .add_batch_set(key.0.indexed(), *indirect_parameters_index); + *indirect_parameters_index += 1; + } else { + work_item_buffer.push( + key.0.indexed(), + PreprocessWorkItem { + input_index: input_index.into(), + output_or_indirect_parameters_index: output_index, + }, + ); + unbatchables + .buffer_indices + .add(UnbatchableBinnedEntityIndices { + instance_index: output_index, + extra_index: PhaseItemExtraIndex::None, + }); + } + } + } + } +} + +/// The state that [`batch_and_prepare_binned_render_phase`] uses to construct +/// multidrawable batch sets. +/// +/// The [`batch_and_prepare_binned_render_phase`] system maintains two of these: +/// one for indexed meshes and one for non-indexed meshes. +struct MultidrawableBatchSetPreparer +where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + /// The offset in the indirect parameters buffer at which the next indirect + /// parameters will be written. + indirect_parameters_index: u32, + /// The number of batch sets we've built so far for this mesh class. + batch_set_index: u32, + /// The number of work items we've emitted so far for this mesh class. + work_item_count: usize, + phantom: PhantomData<(BPI, GFBD)>, +} + +impl MultidrawableBatchSetPreparer +where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + /// Creates a new [`MultidrawableBatchSetPreparer`] that will start writing + /// indirect parameters and batch sets at the given indices. + #[inline] + fn new(initial_indirect_parameters_index: u32, initial_batch_set_index: u32) -> Self { + MultidrawableBatchSetPreparer { + indirect_parameters_index: initial_indirect_parameters_index, + batch_set_index: initial_batch_set_index, + work_item_count: 0, + phantom: PhantomData, + } + } + + /// Creates batch sets and writes the GPU data needed to draw all visible + /// entities of one mesh class in the given batch set. + /// + /// The *mesh class* represents whether the mesh has indices or not. + #[inline] + fn prepare_multidrawable_binned_batch_set( + &mut self, + bins: &IndexMap, + output_index: &mut u32, + data_buffer: &mut UninitBufferVec, + indexed_work_item_buffer: &mut RawBufferVec, + mesh_class_buffers: &mut MeshClassIndirectParametersBuffers, + batch_sets: &mut Vec>, + ) where + IP: Clone + ShaderSize + WriteInto, + { + let current_indexed_batch_set_index = self.batch_set_index; + let current_output_index = *output_index; + + let indirect_parameters_base = self.indirect_parameters_index; + + // We're going to write the first entity into the batch set. Do this + // here so that we can preload the bin into cache as a side effect. + let Some((first_bin_key, first_bin)) = bins.iter().next() else { + return; + }; + let first_bin_len = first_bin.entities().len(); + let first_bin_entity = first_bin + .entities() + .keys() + .next() + .copied() + .unwrap_or(MainEntity::from(Entity::PLACEHOLDER)); + + // Traverse the batch set, processing each bin. + for bin in bins.values() { + // Record the first output index for this batch, as well as its own + // index. + mesh_class_buffers + .cpu_metadata + .push(IndirectParametersCpuMetadata { + base_output_index: *output_index, + batch_set_index: self.batch_set_index, + }); + + // Traverse the bin, pushing `PreprocessWorkItem`s for each entity + // within it. This is a hot loop, so make it as fast as possible. + for &input_index in bin.entities().values() { + indexed_work_item_buffer.push(PreprocessWorkItem { + input_index: *input_index, + output_or_indirect_parameters_index: self.indirect_parameters_index, + }); + } + + // Reserve space for the appropriate number of entities in the data + // buffer. Also, advance the output index and work item count. + let bin_entity_count = bin.entities().len(); + data_buffer.add_multiple(bin_entity_count); + *output_index += bin_entity_count as u32; + self.work_item_count += bin_entity_count; + + self.indirect_parameters_index += 1; + } + + // Reserve space for the bins in this batch set in the GPU buffers. + let bin_count = bins.len(); + mesh_class_buffers.gpu_metadata.add_multiple(bin_count); + mesh_class_buffers.data.add_multiple(bin_count); + + // Write the information the GPU will need about this batch set. + mesh_class_buffers.batch_sets.push(IndirectBatchSet { + indirect_parameters_base, + indirect_parameters_count: 0, + }); + + self.batch_set_index += 1; + + // Record the batch set. The render node later processes this record to + // render the batches. + batch_sets.push(BinnedRenderPhaseBatchSet { + first_batch: BinnedRenderPhaseBatch { + representative_entity: (Entity::PLACEHOLDER, first_bin_entity), + instance_range: current_output_index..(current_output_index + first_bin_len as u32), + extra_index: PhaseItemExtraIndex::maybe_indirect_parameters_index(NonMaxU32::new( + indirect_parameters_base, + )), + }, + bin_key: (*first_bin_key).clone(), + batch_count: self.indirect_parameters_index - indirect_parameters_base, + index: current_indexed_batch_set_index, + }); + } +} + +/// A system that gathers up the per-phase GPU buffers and inserts them into the +/// [`BatchedInstanceBuffers`] and [`IndirectParametersBuffers`] tables. +/// +/// This runs after the [`batch_and_prepare_binned_render_phase`] or +/// [`batch_and_prepare_sorted_render_phase`] systems. It takes the per-phase +/// [`PhaseBatchedInstanceBuffers`] and [`PhaseIndirectParametersBuffers`] +/// resources and inserts them into the global [`BatchedInstanceBuffers`] and +/// [`IndirectParametersBuffers`] tables. +/// +/// This system exists so that the [`batch_and_prepare_binned_render_phase`] and +/// [`batch_and_prepare_sorted_render_phase`] can run in parallel with one +/// another. If those two systems manipulated [`BatchedInstanceBuffers`] and +/// [`IndirectParametersBuffers`] directly, then they wouldn't be able to run in +/// parallel. +pub fn collect_buffers_for_phase( + mut phase_batched_instance_buffers: ResMut>, + mut phase_indirect_parameters_buffers: ResMut>, + mut batched_instance_buffers: ResMut< + BatchedInstanceBuffers, + >, + mut indirect_parameters_buffers: ResMut, +) where + PI: PhaseItem, + GFBD: GetFullBatchData + Send + Sync + 'static, +{ + // Insert the `PhaseBatchedInstanceBuffers` into the global table. Replace + // the contents of the per-phase resource with the old batched instance + // buffers in order to reuse allocations. + let untyped_phase_batched_instance_buffers = + mem::take(&mut phase_batched_instance_buffers.buffers); + if let Some(mut old_untyped_phase_batched_instance_buffers) = batched_instance_buffers + .phase_instance_buffers + .insert(TypeId::of::(), untyped_phase_batched_instance_buffers) + { + old_untyped_phase_batched_instance_buffers.clear(); + phase_batched_instance_buffers.buffers = old_untyped_phase_batched_instance_buffers; + } + + // Insert the `PhaseIndirectParametersBuffers` into the global table. + // Replace the contents of the per-phase resource with the old indirect + // parameters buffers in order to reuse allocations. + let untyped_phase_indirect_parameters_buffers = mem::replace( + &mut phase_indirect_parameters_buffers.buffers, + UntypedPhaseIndirectParametersBuffers::new( + indirect_parameters_buffers.allow_copies_from_indirect_parameter_buffers, + ), + ); + if let Some(mut old_untyped_phase_indirect_parameters_buffers) = indirect_parameters_buffers + .insert( + TypeId::of::(), + untyped_phase_indirect_parameters_buffers, + ) + { + old_untyped_phase_indirect_parameters_buffers.clear(); + phase_indirect_parameters_buffers.buffers = old_untyped_phase_indirect_parameters_buffers; + } +} + +/// A system that writes all instance buffers to the GPU. +pub fn write_batched_instance_buffers( + render_device: Res, + render_queue: Res, + gpu_array_buffer: ResMut>, +) where + GFBD: GetFullBatchData, +{ + let BatchedInstanceBuffers { + current_input_buffer, + previous_input_buffer, + phase_instance_buffers, + } = gpu_array_buffer.into_inner(); + + let render_device = &*render_device; + let render_queue = &*render_queue; + + ComputeTaskPool::get().scope(|scope| { + scope.spawn(async { + let _span = tracing::info_span!("write_current_input_buffers").entered(); + current_input_buffer + .buffer + .write_buffer(render_device, render_queue); + }); + scope.spawn(async { + let _span = tracing::info_span!("write_previous_input_buffers").entered(); + previous_input_buffer + .buffer + .write_buffer(render_device, render_queue); + }); + + for phase_instance_buffers in phase_instance_buffers.values_mut() { + let UntypedPhaseBatchedInstanceBuffers { + ref mut data_buffer, + ref mut work_item_buffers, + ref mut late_indexed_indirect_parameters_buffer, + ref mut late_non_indexed_indirect_parameters_buffer, + } = *phase_instance_buffers; + + scope.spawn(async { + let _span = tracing::info_span!("write_phase_instance_buffers").entered(); + data_buffer.write_buffer(render_device); + late_indexed_indirect_parameters_buffer.write_buffer(render_device, render_queue); + late_non_indexed_indirect_parameters_buffer + .write_buffer(render_device, render_queue); + }); + + for phase_work_item_buffers in work_item_buffers.values_mut() { + scope.spawn(async { + let _span = tracing::info_span!("write_work_item_buffers").entered(); + match *phase_work_item_buffers { + PreprocessWorkItemBuffers::Direct(ref mut buffer_vec) => { + buffer_vec.write_buffer(render_device, render_queue); + } + PreprocessWorkItemBuffers::Indirect { + ref mut indexed, + ref mut non_indexed, + ref mut gpu_occlusion_culling, + } => { + indexed.write_buffer(render_device, render_queue); + non_indexed.write_buffer(render_device, render_queue); + + if let Some(GpuOcclusionCullingWorkItemBuffers { + ref mut late_indexed, + ref mut late_non_indexed, + late_indirect_parameters_indexed_offset: _, + late_indirect_parameters_non_indexed_offset: _, + }) = *gpu_occlusion_culling + { + if !late_indexed.is_empty() { + late_indexed.write_buffer(render_device); + } + if !late_non_indexed.is_empty() { + late_non_indexed.write_buffer(render_device); + } + } + } + } + }); + } + } + }); +} + +pub fn clear_indirect_parameters_buffers( + mut indirect_parameters_buffers: ResMut, +) { + for phase_indirect_parameters_buffers in indirect_parameters_buffers.values_mut() { + phase_indirect_parameters_buffers.clear(); + } +} + +pub fn write_indirect_parameters_buffers( + render_device: Res, + render_queue: Res, + mut indirect_parameters_buffers: ResMut, +) { + let render_device = &*render_device; + let render_queue = &*render_queue; + ComputeTaskPool::get().scope(|scope| { + for phase_indirect_parameters_buffers in indirect_parameters_buffers.values_mut() { + scope.spawn(async { + let _span = tracing::info_span!("indexed_data").entered(); + phase_indirect_parameters_buffers + .indexed + .data + .write_buffer(render_device); + }); + scope.spawn(async { + let _span = tracing::info_span!("non_indexed_data").entered(); + phase_indirect_parameters_buffers + .non_indexed + .data + .write_buffer(render_device); + }); + + scope.spawn(async { + let _span = tracing::info_span!("indexed_cpu_metadata").entered(); + phase_indirect_parameters_buffers + .indexed + .cpu_metadata + .write_buffer(render_device, render_queue); + }); + scope.spawn(async { + let _span = tracing::info_span!("non_indexed_cpu_metadata").entered(); + phase_indirect_parameters_buffers + .non_indexed + .cpu_metadata + .write_buffer(render_device, render_queue); + }); + + scope.spawn(async { + let _span = tracing::info_span!("non_indexed_gpu_metadata").entered(); + phase_indirect_parameters_buffers + .non_indexed + .gpu_metadata + .write_buffer(render_device); + }); + scope.spawn(async { + let _span = tracing::info_span!("indexed_gpu_metadata").entered(); + phase_indirect_parameters_buffers + .indexed + .gpu_metadata + .write_buffer(render_device); + }); + + scope.spawn(async { + let _span = tracing::info_span!("indexed_batch_sets").entered(); + phase_indirect_parameters_buffers + .indexed + .batch_sets + .write_buffer(render_device, render_queue); + }); + scope.spawn(async { + let _span = tracing::info_span!("non_indexed_batch_sets").entered(); + phase_indirect_parameters_buffers + .non_indexed + .batch_sets + .write_buffer(render_device, render_queue); + }); + } + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn instance_buffer_correct_behavior() { + let mut instance_buffer = InstanceInputUniformBuffer::new(); + + let index = instance_buffer.add(2); + instance_buffer.remove(index); + assert_eq!(instance_buffer.get_unchecked(index), 2); + assert_eq!(instance_buffer.get(index), None); + + instance_buffer.add(5); + assert_eq!(instance_buffer.buffer().len(), 1); + } +} diff --git a/third_party/bevy_render/src/batching/mod.rs b/third_party/bevy_render/src/batching/mod.rs new file mode 100644 index 0000000..bceca62 --- /dev/null +++ b/third_party/bevy_render/src/batching/mod.rs @@ -0,0 +1,225 @@ +use bevy_ecs::{ + component::Component, + entity::Entity, + system::{ResMut, SystemParam, SystemParamItem}, +}; +use bytemuck::Pod; +use gpu_preprocessing::UntypedPhaseIndirectParametersBuffers; +use nonmax::NonMaxU32; + +use crate::{ + render_phase::{ + BinnedPhaseItem, CachedRenderPipelinePhaseItem, DrawFunctionId, PhaseItemExtraIndex, + SortedPhaseItem, SortedRenderPhase, ViewBinnedRenderPhases, + }, + render_resource::{CachedRenderPipelineId, GpuArrayBufferable}, + sync_world::MainEntity, +}; + +pub mod gpu_preprocessing; +pub mod no_gpu_preprocessing; + +/// Add this component to mesh entities to disable automatic batching +#[derive(Component, Default, Clone, Copy)] +pub struct NoAutomaticBatching; + +/// Data necessary to be equal for two draw commands to be mergeable +/// +/// This is based on the following assumptions: +/// - Only entities with prepared assets (pipelines, materials, meshes) are +/// queued to phases +/// - View bindings are constant across a phase for a given draw function as +/// phases are per-view +/// - `batch_and_prepare_render_phase` is the only system that performs this +/// batching and has sole responsibility for preparing the per-object data. +/// As such the mesh binding and dynamic offsets are assumed to only be +/// variable as a result of the `batch_and_prepare_render_phase` system, e.g. +/// due to having to split data across separate uniform bindings within the +/// same buffer due to the maximum uniform buffer binding size. +#[derive(PartialEq)] +struct BatchMeta { + /// The pipeline id encompasses all pipeline configuration including vertex + /// buffers and layouts, shaders and their specializations, bind group + /// layouts, etc. + pipeline_id: CachedRenderPipelineId, + /// The draw function id defines the `RenderCommands` that are called to + /// set the pipeline and bindings, and make the draw command + draw_function_id: DrawFunctionId, + dynamic_offset: Option, + user_data: T, +} + +impl BatchMeta { + fn new(item: &impl CachedRenderPipelinePhaseItem, user_data: T) -> Self { + BatchMeta { + pipeline_id: item.cached_pipeline(), + draw_function_id: item.draw_function(), + dynamic_offset: match item.extra_index() { + PhaseItemExtraIndex::DynamicOffset(dynamic_offset) => { + NonMaxU32::new(dynamic_offset) + } + PhaseItemExtraIndex::None | PhaseItemExtraIndex::IndirectParametersIndex { .. } => { + None + } + }, + user_data, + } + } +} + +/// A trait to support getting data used for batching draw commands via phase +/// items. +/// +/// This is a simple version that only allows for sorting, not binning, as well +/// as only CPU processing, not GPU preprocessing. For these fancier features, +/// see [`GetFullBatchData`]. +pub trait GetBatchData { + /// The system parameters [`GetBatchData::get_batch_data`] needs in + /// order to compute the batch data. + type Param: SystemParam + 'static; + /// Data used for comparison between phase items. If the pipeline id, draw + /// function id, per-instance data buffer dynamic offset and this data + /// matches, the draws can be batched. + type CompareData: PartialEq; + /// The per-instance data to be inserted into the + /// [`crate::render_resource::GpuArrayBuffer`] containing these data for all + /// instances. + type BufferData: GpuArrayBufferable + Sync + Send + 'static; + /// Get the per-instance data to be inserted into the + /// [`crate::render_resource::GpuArrayBuffer`]. If the instance can be + /// batched, also return the data used for comparison when deciding whether + /// draws can be batched, else return None for the `CompareData`. + /// + /// This is only called when building instance data on CPU. In the GPU + /// instance data building path, we use + /// [`GetFullBatchData::get_index_and_compare_data`] instead. + fn get_batch_data( + param: &SystemParamItem, + query_item: (Entity, MainEntity), + ) -> Option<(Self::BufferData, Option)>; +} + +/// A trait to support getting data used for batching draw commands via phase +/// items. +/// +/// This version allows for binning and GPU preprocessing. +pub trait GetFullBatchData: GetBatchData { + /// The per-instance data that was inserted into the + /// [`crate::render_resource::BufferVec`] during extraction. + type BufferInputData: Pod + Default + Sync + Send; + + /// Get the per-instance data to be inserted into the + /// [`crate::render_resource::GpuArrayBuffer`]. + /// + /// This is only called when building uniforms on CPU. In the GPU instance + /// buffer building path, we use + /// [`GetFullBatchData::get_index_and_compare_data`] instead. + fn get_binned_batch_data( + param: &SystemParamItem, + query_item: MainEntity, + ) -> Option; + + /// Returns the index of the [`GetFullBatchData::BufferInputData`] that the + /// GPU preprocessing phase will use. + /// + /// We already inserted the [`GetFullBatchData::BufferInputData`] during the + /// extraction phase before we got here, so this function shouldn't need to + /// look up any render data. If CPU instance buffer building is in use, this + /// function will never be called. + fn get_index_and_compare_data( + param: &SystemParamItem, + query_item: MainEntity, + ) -> Option<(NonMaxU32, Option)>; + + /// Returns the index of the [`GetFullBatchData::BufferInputData`] that the + /// GPU preprocessing phase will use. + /// + /// We already inserted the [`GetFullBatchData::BufferInputData`] during the + /// extraction phase before we got here, so this function shouldn't need to + /// look up any render data. + /// + /// This function is currently only called for unbatchable entities when GPU + /// instance buffer building is in use. For batchable entities, the uniform + /// index is written during queuing (e.g. in `queue_material_meshes`). In + /// the case of CPU instance buffer building, the CPU writes the uniforms, + /// so there's no index to return. + fn get_binned_index( + param: &SystemParamItem, + query_item: MainEntity, + ) -> Option; + + /// Writes the [`gpu_preprocessing::IndirectParametersGpuMetadata`] + /// necessary to draw this batch into the given metadata buffer at the given + /// index. + /// + /// This is only used if GPU culling is enabled (which requires GPU + /// preprocessing). + /// + /// * `indexed` is true if the mesh is indexed or false if it's non-indexed. + /// + /// * `base_output_index` is the index of the first mesh instance in this + /// batch in the `MeshUniform` output buffer. + /// + /// * `batch_set_index` is the index of the batch set in the + /// [`gpu_preprocessing::IndirectBatchSet`] buffer, if this batch belongs to + /// a batch set. + /// + /// * `indirect_parameters_buffers` is the buffer in which to write the + /// metadata. + /// + /// * `indirect_parameters_offset` is the index in that buffer at which to + /// write the metadata. + fn write_batch_indirect_parameters_metadata( + indexed: bool, + base_output_index: u32, + batch_set_index: Option, + indirect_parameters_buffers: &mut UntypedPhaseIndirectParametersBuffers, + indirect_parameters_offset: u32, + ); +} + +/// Sorts a render phase that uses bins. +pub fn sort_binned_render_phase(mut phases: ResMut>) +where + BPI: BinnedPhaseItem, +{ + for phase in phases.values_mut() { + phase.multidrawable_meshes.sort_unstable_keys(); + phase.batchable_meshes.sort_unstable_keys(); + phase.unbatchable_meshes.sort_unstable_keys(); + phase.non_mesh_items.sort_unstable_keys(); + } +} + +/// Batches the items in a sorted render phase. +/// +/// This means comparing metadata needed to draw each phase item and trying to +/// combine the draws into a batch. +/// +/// This is common code factored out from +/// [`gpu_preprocessing::batch_and_prepare_sorted_render_phase`] and +/// [`no_gpu_preprocessing::batch_and_prepare_sorted_render_phase`]. +fn batch_and_prepare_sorted_render_phase( + phase: &mut SortedRenderPhase, + mut process_item: impl FnMut(&mut I) -> Option, +) where + I: CachedRenderPipelinePhaseItem + SortedPhaseItem, + GBD: GetBatchData, +{ + let items = phase.items.iter_mut().map(|item| { + let batch_data = match process_item(item) { + Some(compare_data) if I::AUTOMATIC_BATCHING => Some(BatchMeta::new(item, compare_data)), + _ => None, + }; + (item.batch_range_mut(), batch_data) + }); + + items.reduce(|(start_range, prev_batch_meta), (range, batch_meta)| { + if batch_meta.is_some() && prev_batch_meta == batch_meta { + start_range.end = range.end; + (start_range, prev_batch_meta) + } else { + (range, batch_meta) + } + }); +} diff --git a/third_party/bevy_render/src/batching/no_gpu_preprocessing.rs b/third_party/bevy_render/src/batching/no_gpu_preprocessing.rs new file mode 100644 index 0000000..66ba078 --- /dev/null +++ b/third_party/bevy_render/src/batching/no_gpu_preprocessing.rs @@ -0,0 +1,182 @@ +//! Batching functionality when GPU preprocessing isn't in use. + +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::entity::Entity; +use bevy_ecs::resource::Resource; +use bevy_ecs::system::{Res, ResMut, StaticSystemParam}; +use smallvec::{smallvec, SmallVec}; +use tracing::error; +use wgpu::{BindingResource, Limits}; + +use crate::{ + render_phase::{ + BinnedPhaseItem, BinnedRenderPhaseBatch, BinnedRenderPhaseBatchSets, + CachedRenderPipelinePhaseItem, PhaseItemExtraIndex, SortedPhaseItem, + ViewBinnedRenderPhases, ViewSortedRenderPhases, + }, + render_resource::{GpuArrayBuffer, GpuArrayBufferable}, + renderer::{RenderDevice, RenderQueue}, +}; + +use super::{GetBatchData, GetFullBatchData}; + +/// The GPU buffers holding the data needed to render batches. +/// +/// For example, in the 3D PBR pipeline this holds `MeshUniform`s, which are the +/// `BD` type parameter in that mode. +#[derive(Resource, Deref, DerefMut)] +pub struct BatchedInstanceBuffer(pub GpuArrayBuffer) +where + BD: GpuArrayBufferable + Sync + Send + 'static; + +impl BatchedInstanceBuffer +where + BD: GpuArrayBufferable + Sync + Send + 'static, +{ + /// Creates a new buffer. + pub fn new(limits: &Limits) -> Self { + BatchedInstanceBuffer(GpuArrayBuffer::new(limits)) + } + + /// Returns the binding of the buffer that contains the per-instance data. + /// + /// If we're in the GPU instance buffer building mode, this buffer needs to + /// be filled in via a compute shader. + pub fn instance_data_binding(&self) -> Option> { + self.binding() + } +} + +/// A system that clears out the [`BatchedInstanceBuffer`] for the frame. +/// +/// This needs to run before the CPU batched instance buffers are used. +pub fn clear_batched_cpu_instance_buffers( + cpu_batched_instance_buffer: Option>>, +) where + GBD: GetBatchData, +{ + if let Some(mut cpu_batched_instance_buffer) = cpu_batched_instance_buffer { + cpu_batched_instance_buffer.clear(); + } +} + +/// Batch the items in a sorted render phase, when GPU instance buffer building +/// isn't in use. This means comparing metadata needed to draw each phase item +/// and trying to combine the draws into a batch. +pub fn batch_and_prepare_sorted_render_phase( + batched_instance_buffer: ResMut>, + mut phases: ResMut>, + param: StaticSystemParam, +) where + I: CachedRenderPipelinePhaseItem + SortedPhaseItem, + GBD: GetBatchData, +{ + let system_param_item = param.into_inner(); + + // We only process CPU-built batch data in this function. + let batched_instance_buffer = batched_instance_buffer.into_inner(); + + for phase in phases.values_mut() { + super::batch_and_prepare_sorted_render_phase::(phase, |item| { + let (buffer_data, compare_data) = + GBD::get_batch_data(&system_param_item, (item.entity(), item.main_entity()))?; + let buffer_index = batched_instance_buffer.push(buffer_data); + + let index = buffer_index.index; + let (batch_range, extra_index) = item.batch_range_and_extra_index_mut(); + *batch_range = index..index + 1; + *extra_index = PhaseItemExtraIndex::maybe_dynamic_offset(buffer_index.dynamic_offset); + + compare_data + }); + } +} + +/// Creates batches for a render phase that uses bins, when GPU batch data +/// building isn't in use. +pub fn batch_and_prepare_binned_render_phase( + gpu_array_buffer: ResMut>, + mut phases: ResMut>, + param: StaticSystemParam, +) where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + let gpu_array_buffer = gpu_array_buffer.into_inner(); + let system_param_item = param.into_inner(); + + for phase in phases.values_mut() { + // Prepare batchables. + + for bin in phase.batchable_meshes.values_mut() { + let mut batch_set: SmallVec<[BinnedRenderPhaseBatch; 1]> = smallvec![]; + for main_entity in bin.entities().keys() { + let Some(buffer_data) = + GFBD::get_binned_batch_data(&system_param_item, *main_entity) + else { + continue; + }; + let instance = gpu_array_buffer.push(buffer_data); + + // If the dynamic offset has changed, flush the batch. + // + // This is the only time we ever have more than one batch per + // bin. Note that dynamic offsets are only used on platforms + // with no storage buffers. + if !batch_set.last().is_some_and(|batch| { + batch.instance_range.end == instance.index + && batch.extra_index + == PhaseItemExtraIndex::maybe_dynamic_offset(instance.dynamic_offset) + }) { + batch_set.push(BinnedRenderPhaseBatch { + representative_entity: (Entity::PLACEHOLDER, *main_entity), + instance_range: instance.index..instance.index, + extra_index: PhaseItemExtraIndex::maybe_dynamic_offset( + instance.dynamic_offset, + ), + }); + } + + if let Some(batch) = batch_set.last_mut() { + batch.instance_range.end = instance.index + 1; + } + } + + match phase.batch_sets { + BinnedRenderPhaseBatchSets::DynamicUniforms(ref mut batch_sets) => { + batch_sets.push(batch_set); + } + BinnedRenderPhaseBatchSets::Direct(_) + | BinnedRenderPhaseBatchSets::MultidrawIndirect { .. } => { + error!( + "Dynamic uniform batch sets should be used when GPU preprocessing is off" + ); + } + } + } + + // Prepare unbatchables. + for unbatchables in phase.unbatchable_meshes.values_mut() { + for main_entity in unbatchables.entities.keys() { + let Some(buffer_data) = + GFBD::get_binned_batch_data(&system_param_item, *main_entity) + else { + continue; + }; + let instance = gpu_array_buffer.push(buffer_data); + unbatchables.buffer_indices.add(instance.into()); + } + } + } +} + +/// Writes the instance buffer data to the GPU. +pub fn write_batched_instance_buffer( + render_device: Res, + render_queue: Res, + mut cpu_batched_instance_buffer: ResMut>, +) where + GBD: GetBatchData, +{ + cpu_batched_instance_buffer.write_buffer(&render_device, &render_queue); +} diff --git a/third_party/bevy_render/src/bindless.wgsl b/third_party/bevy_render/src/bindless.wgsl new file mode 100644 index 0000000..6c8eff1 --- /dev/null +++ b/third_party/bevy_render/src/bindless.wgsl @@ -0,0 +1,37 @@ +// Defines the common arrays used to access bindless resources. +// +// This need to be kept up to date with the `BINDING_NUMBERS` table in +// `bindless.rs`. +// +// You access these by indexing into the bindless index table, and from there +// indexing into the appropriate binding array. For example, to access the base +// color texture of a `StandardMaterial` in bindless mode, write +// `bindless_textures_2d[materials[slot].base_color_texture]`, where +// `materials` is the bindless index table and `slot` is the index into that +// table (which can be found in the `Mesh`). + +#define_import_path bevy_render::bindless + +#ifdef BINDLESS + +// Binding 0 is the bindless index table. +// Filtering samplers. +@group(#{MATERIAL_BIND_GROUP}) @binding(1) var bindless_samplers_filtering: binding_array; +// Non-filtering samplers (nearest neighbor). +@group(#{MATERIAL_BIND_GROUP}) @binding(2) var bindless_samplers_non_filtering: binding_array; +// Comparison samplers (typically for shadow mapping). +@group(#{MATERIAL_BIND_GROUP}) @binding(3) var bindless_samplers_comparison: binding_array; +// 1D textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(4) var bindless_textures_1d: binding_array>; +// 2D textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(5) var bindless_textures_2d: binding_array>; +// 2D array textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(6) var bindless_textures_2d_array: binding_array>; +// 3D textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(7) var bindless_textures_3d: binding_array>; +// Cubemap textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(8) var bindless_textures_cube: binding_array>; +// Cubemap array textures. +@group(#{MATERIAL_BIND_GROUP}) @binding(9) var bindless_textures_cube_array: binding_array>; + +#endif // BINDLESS diff --git a/third_party/bevy_render/src/camera.rs b/third_party/bevy_render/src/camera.rs new file mode 100644 index 0000000..27dac44 --- /dev/null +++ b/third_party/bevy_render/src/camera.rs @@ -0,0 +1,702 @@ +use crate::{ + batching::gpu_preprocessing::{GpuPreprocessingMode, GpuPreprocessingSupport}, + extract_component::{ExtractComponent, ExtractComponentPlugin}, + extract_resource::{ExtractResource, ExtractResourcePlugin}, + render_asset::RenderAssets, + render_graph::{CameraDriverNode, InternedRenderSubGraph, RenderGraph, RenderSubGraph}, + render_resource::TextureView, + sync_world::{RenderEntity, SyncToRenderWorld}, + texture::{GpuImage, ManualTextureViews}, + view::{ + ColorGrading, ExtractedView, ExtractedWindows, Hdr, Msaa, NoIndirectDrawing, + RenderVisibleEntities, RetainedViewEntity, ViewUniformOffset, + }, + Extract, ExtractSchedule, Render, RenderApp, RenderSystems, +}; + +use bevy_app::{App, Plugin, PostStartup, PostUpdate}; +use bevy_asset::{AssetEvent, AssetEventSystems, AssetId, Assets}; +use bevy_camera::{ + primitives::Frustum, + visibility::{self, RenderLayers, VisibleEntities}, + Camera, Camera2d, Camera3d, CameraMainTextureUsages, CameraOutputMode, CameraUpdateSystems, + ClearColor, ClearColorConfig, Exposure, ManualTextureViewHandle, MsaaWriteback, + NormalizedRenderTarget, Projection, RenderTarget, RenderTargetInfo, Viewport, +}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + change_detection::DetectChanges, + component::Component, + entity::{ContainsEntity, Entity}, + error::BevyError, + lifecycle::HookContext, + message::MessageReader, + prelude::With, + query::{Has, QueryItem}, + reflect::ReflectComponent, + resource::Resource, + schedule::IntoScheduleConfigs, + system::{Commands, Query, Res, ResMut}, + world::DeferredWorld, +}; +use bevy_image::Image; +use bevy_math::{uvec2, vec2, Mat4, URect, UVec2, UVec4, Vec2}; +use bevy_platform::collections::{HashMap, HashSet}; +use bevy_reflect::prelude::*; +use bevy_transform::components::GlobalTransform; +use bevy_window::{PrimaryWindow, Window, WindowCreated, WindowResized, WindowScaleFactorChanged}; +use tracing::warn; +use wgpu::TextureFormat; + +#[derive(Default)] +pub struct CameraPlugin; + +impl Plugin for CameraPlugin { + fn build(&self, app: &mut App) { + app.register_required_components::() + .register_required_components::() + .register_required_components::() + .register_required_components::() + .add_plugins(( + ExtractResourcePlugin::::default(), + ExtractComponentPlugin::::default(), + )) + .add_systems(PostStartup, camera_system.in_set(CameraUpdateSystems)) + .add_systems( + PostUpdate, + camera_system + .in_set(CameraUpdateSystems) + .before(AssetEventSystems) + .before(visibility::update_frusta), + ); + app.world_mut() + .register_component_hooks::() + .on_add(warn_on_no_render_graph); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .init_resource::() + .add_systems(ExtractSchedule, extract_cameras) + .add_systems(Render, sort_cameras.in_set(RenderSystems::ManageViews)); + let camera_driver_node = CameraDriverNode::new(render_app.world_mut()); + let mut render_graph = render_app.world_mut().resource_mut::(); + render_graph.add_node(crate::graph::CameraDriverLabel, camera_driver_node); + } + } +} + +fn warn_on_no_render_graph(world: DeferredWorld, HookContext { entity, caller, .. }: HookContext) { + if !world.entity(entity).contains::() { + warn!("{}Entity {entity} has a `Camera` component, but it doesn't have a render graph configured. Usually, adding a `Camera2d` or `Camera3d` component will work. + However, you may instead need to enable `bevy_core_pipeline`, or may want to manually add a `CameraRenderGraph` component to create a custom render graph.", caller.map(|location|format!("{location}: ")).unwrap_or_default()); + } +} + +impl ExtractResource for ClearColor { + type Source = Self; + + fn extract_resource(source: &Self::Source) -> Self { + source.clone() + } +} +impl ExtractComponent for CameraMainTextureUsages { + type QueryData = &'static Self; + type QueryFilter = (); + type Out = Self; + + fn extract_component(item: QueryItem) -> Option { + Some(*item) + } +} +impl ExtractComponent for Camera2d { + type QueryData = &'static Self; + type QueryFilter = With; + type Out = Self; + + fn extract_component(item: QueryItem) -> Option { + Some(item.clone()) + } +} +impl ExtractComponent for Camera3d { + type QueryData = &'static Self; + type QueryFilter = With; + type Out = Self; + + fn extract_component(item: QueryItem) -> Option { + Some(item.clone()) + } +} + +/// Configures the [`RenderGraph`] name assigned to be run for a given [`Camera`] entity. +#[derive(Component, Debug, Deref, DerefMut, Reflect, Clone)] +#[reflect(opaque)] +#[reflect(Component, Debug, Clone)] +pub struct CameraRenderGraph(InternedRenderSubGraph); + +impl CameraRenderGraph { + /// Creates a new [`CameraRenderGraph`] from any string-like type. + #[inline] + pub fn new(name: T) -> Self { + Self(name.intern()) + } + + /// Sets the graph name. + #[inline] + pub fn set(&mut self, name: T) { + self.0 = name.intern(); + } +} + +pub trait NormalizedRenderTargetExt { + fn get_texture_view<'a>( + &self, + windows: &'a ExtractedWindows, + images: &'a RenderAssets, + manual_texture_views: &'a ManualTextureViews, + ) -> Option<&'a TextureView>; + + /// Retrieves the [`TextureFormat`] of this render target, if it exists. + fn get_texture_view_format<'a>( + &self, + windows: &'a ExtractedWindows, + images: &'a RenderAssets, + manual_texture_views: &'a ManualTextureViews, + ) -> Option; + + fn get_render_target_info<'a>( + &self, + resolutions: impl IntoIterator, + images: &Assets, + manual_texture_views: &ManualTextureViews, + ) -> Result; + + // Check if this render target is contained in the given changed windows or images. + fn is_changed( + &self, + changed_window_ids: &HashSet, + changed_image_handles: &HashSet<&AssetId>, + ) -> bool; +} + +impl NormalizedRenderTargetExt for NormalizedRenderTarget { + fn get_texture_view<'a>( + &self, + windows: &'a ExtractedWindows, + images: &'a RenderAssets, + manual_texture_views: &'a ManualTextureViews, + ) -> Option<&'a TextureView> { + match self { + NormalizedRenderTarget::Window(window_ref) => windows + .get(&window_ref.entity()) + .and_then(|window| window.swap_chain_texture_view.as_ref()), + NormalizedRenderTarget::Image(image_target) => images + .get(&image_target.handle) + .map(|image| &image.texture_view), + NormalizedRenderTarget::TextureView(id) => { + manual_texture_views.get(id).map(|tex| &tex.texture_view) + } + NormalizedRenderTarget::None { .. } => None, + } + } + + /// Retrieves the texture view's [`TextureFormat`] of this render target, if it exists. + fn get_texture_view_format<'a>( + &self, + windows: &'a ExtractedWindows, + images: &'a RenderAssets, + manual_texture_views: &'a ManualTextureViews, + ) -> Option { + match self { + NormalizedRenderTarget::Window(window_ref) => windows + .get(&window_ref.entity()) + .and_then(|window| window.swap_chain_texture_view_format), + NormalizedRenderTarget::Image(image_target) => images + .get(&image_target.handle) + .map(|image| image.texture_view_format.unwrap_or(image.texture_format)), + NormalizedRenderTarget::TextureView(id) => { + manual_texture_views.get(id).map(|tex| tex.view_format) + } + NormalizedRenderTarget::None { .. } => None, + } + } + + fn get_render_target_info<'a>( + &self, + resolutions: impl IntoIterator, + images: &Assets, + manual_texture_views: &ManualTextureViews, + ) -> Result { + match self { + NormalizedRenderTarget::Window(window_ref) => resolutions + .into_iter() + .find(|(entity, _)| *entity == window_ref.entity()) + .map(|(_, window)| RenderTargetInfo { + physical_size: window.physical_size(), + scale_factor: window.resolution.scale_factor(), + }) + .ok_or(MissingRenderTargetInfoError::Window { + window: window_ref.entity(), + }), + NormalizedRenderTarget::Image(image_target) => images + .get(&image_target.handle) + .map(|image| RenderTargetInfo { + physical_size: image.size(), + scale_factor: image_target.scale_factor, + }) + .ok_or(MissingRenderTargetInfoError::Image { + image: image_target.handle.id(), + }), + NormalizedRenderTarget::TextureView(id) => manual_texture_views + .get(id) + .map(|tex| RenderTargetInfo { + physical_size: tex.size, + scale_factor: 1.0, + }) + .ok_or(MissingRenderTargetInfoError::TextureView { texture_view: *id }), + NormalizedRenderTarget::None { width, height } => Ok(RenderTargetInfo { + physical_size: uvec2(*width, *height), + scale_factor: 1.0, + }), + } + } + + // Check if this render target is contained in the given changed windows or images. + fn is_changed( + &self, + changed_window_ids: &HashSet, + changed_image_handles: &HashSet<&AssetId>, + ) -> bool { + match self { + NormalizedRenderTarget::Window(window_ref) => { + changed_window_ids.contains(&window_ref.entity()) + } + NormalizedRenderTarget::Image(image_target) => { + changed_image_handles.contains(&image_target.handle.id()) + } + NormalizedRenderTarget::TextureView(_) => true, + NormalizedRenderTarget::None { .. } => false, + } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum MissingRenderTargetInfoError { + #[error("RenderTarget::Window missing ({window:?}): Make sure the provided entity has a Window component.")] + Window { window: Entity }, + #[error("RenderTarget::Image missing ({image:?}): Make sure the Image's usages include RenderAssetUsages::MAIN_WORLD.")] + Image { image: AssetId }, + #[error("RenderTarget::TextureView missing ({texture_view:?}): make sure the texture view handle was not removed.")] + TextureView { + texture_view: ManualTextureViewHandle, + }, +} + +/// System in charge of updating a [`Camera`] when its window or projection changes. +/// +/// The system detects window creation, resize, and scale factor change events to update the camera +/// [`Projection`] if needed. +/// +/// ## World Resources +/// +/// [`Res>`](Assets) -- For cameras that render to an image, this resource is used to +/// inspect information about the render target. This system will not access any other image assets. +/// +/// [`OrthographicProjection`]: bevy_camera::OrthographicProjection +/// [`PerspectiveProjection`]: bevy_camera::PerspectiveProjection +pub fn camera_system( + mut window_resized_reader: MessageReader, + mut window_created_reader: MessageReader, + mut window_scale_factor_changed_reader: MessageReader, + mut image_asset_event_reader: MessageReader>, + primary_window: Query>, + windows: Query<(Entity, &Window)>, + images: Res>, + manual_texture_views: Res, + mut cameras: Query<(&mut Camera, &RenderTarget, &mut Projection)>, +) -> Result<(), BevyError> { + let primary_window = primary_window.iter().next(); + + let mut changed_window_ids = >::default(); + changed_window_ids.extend(window_created_reader.read().map(|event| event.window)); + changed_window_ids.extend(window_resized_reader.read().map(|event| event.window)); + let scale_factor_changed_window_ids: HashSet<_> = window_scale_factor_changed_reader + .read() + .map(|event| event.window) + .collect(); + changed_window_ids.extend(scale_factor_changed_window_ids.clone()); + + let changed_image_handles: HashSet<&AssetId> = image_asset_event_reader + .read() + .filter_map(|event| match event { + AssetEvent::Modified { id } | AssetEvent::Added { id } => Some(id), + _ => None, + }) + .collect(); + + for (mut camera, render_target, mut camera_projection) in &mut cameras { + let mut viewport_size = camera + .viewport + .as_ref() + .map(|viewport| viewport.physical_size); + + if let Some(normalized_target) = render_target.normalize(primary_window) + && (normalized_target.is_changed(&changed_window_ids, &changed_image_handles) + || camera.is_added() + || camera_projection.is_changed() + || camera.computed.old_viewport_size != viewport_size + || camera.computed.old_sub_camera_view != camera.sub_camera_view) + { + let new_computed_target_info = normalized_target.get_render_target_info( + windows, + &images, + &manual_texture_views, + )?; + // Check for the scale factor changing, and resize the viewport if needed. + // This can happen when the window is moved between monitors with different DPIs. + // Without this, the viewport will take a smaller portion of the window moved to + // a higher DPI monitor. + if normalized_target.is_changed(&scale_factor_changed_window_ids, &HashSet::default()) + && let Some(old_scale_factor) = camera + .computed + .target_info + .as_ref() + .map(|info| info.scale_factor) + { + let resize_factor = new_computed_target_info.scale_factor / old_scale_factor; + if let Some(ref mut viewport) = camera.viewport { + let resize = |vec: UVec2| (vec.as_vec2() * resize_factor).as_uvec2(); + viewport.physical_position = resize(viewport.physical_position); + viewport.physical_size = resize(viewport.physical_size); + viewport_size = Some(viewport.physical_size); + } + } + // This check is needed because when changing WindowMode to Fullscreen, the viewport may have invalid + // arguments due to a sudden change on the window size to a lower value. + // If the size of the window is lower, the viewport will match that lower value. + if let Some(viewport) = &mut camera.viewport { + viewport.clamp_to_size(new_computed_target_info.physical_size); + } + camera.computed.target_info = Some(new_computed_target_info); + if let Some(size) = camera.logical_viewport_size() + && size.x != 0.0 + && size.y != 0.0 + { + camera_projection.update(size.x, size.y); + camera.computed.clip_from_view = match &camera.sub_camera_view { + Some(sub_view) => camera_projection.get_clip_from_view_for_sub(sub_view), + None => camera_projection.get_clip_from_view(), + } + } + } + + if camera.computed.old_viewport_size != viewport_size { + camera.computed.old_viewport_size = viewport_size; + } + + if camera.computed.old_sub_camera_view != camera.sub_camera_view { + camera.computed.old_sub_camera_view = camera.sub_camera_view; + } + } + Ok(()) +} + +#[derive(Component, Debug)] +pub struct ExtractedCamera { + pub target: Option, + pub physical_viewport_size: Option, + pub physical_target_size: Option, + pub viewport: Option, + pub render_graph: InternedRenderSubGraph, + pub order: isize, + pub output_mode: CameraOutputMode, + pub msaa_writeback: MsaaWriteback, + pub clear_color: ClearColorConfig, + pub sorted_camera_index_for_target: usize, + pub exposure: f32, + pub hdr: bool, +} + +pub fn extract_cameras( + mut commands: Commands, + query: Extract< + Query<( + Entity, + RenderEntity, + &Camera, + &RenderTarget, + &CameraRenderGraph, + &GlobalTransform, + &VisibleEntities, + &Frustum, + ( + Has, + Option<&ColorGrading>, + Option<&Exposure>, + Option<&TemporalJitter>, + Option<&MipBias>, + Option<&RenderLayers>, + Option<&Projection>, + Has, + ), + )>, + >, + primary_window: Extract>>, + gpu_preprocessing_support: Res, + mapper: Extract>, +) { + let primary_window = primary_window.iter().next(); + type ExtractedCameraComponents = ( + ExtractedCamera, + ExtractedView, + RenderVisibleEntities, + TemporalJitter, + MipBias, + RenderLayers, + Projection, + NoIndirectDrawing, + ViewUniformOffset, + ); + for ( + main_entity, + render_entity, + camera, + render_target, + camera_render_graph, + transform, + visible_entities, + frustum, + ( + hdr, + color_grading, + exposure, + temporal_jitter, + mip_bias, + render_layers, + projection, + no_indirect_drawing, + ), + ) in query.iter() + { + if !camera.is_active { + commands + .entity(render_entity) + .remove::(); + continue; + } + + let color_grading = color_grading.unwrap_or(&ColorGrading::default()).clone(); + + if let ( + Some(URect { + min: viewport_origin, + .. + }), + Some(viewport_size), + Some(target_size), + ) = ( + camera.physical_viewport_rect(), + camera.physical_viewport_size(), + camera.physical_target_size(), + ) { + if target_size.x == 0 || target_size.y == 0 { + commands + .entity(render_entity) + .remove::(); + continue; + } + + let render_visible_entities = RenderVisibleEntities { + entities: visible_entities + .entities + .iter() + .map(|(type_id, entities)| { + let entities = entities + .iter() + .map(|entity| { + let render_entity = mapper + .get(*entity) + .cloned() + .map(|entity| entity.id()) + .unwrap_or(Entity::PLACEHOLDER); + (render_entity, (*entity).into()) + }) + .collect(); + (*type_id, entities) + }) + .collect(), + }; + + let mut commands = commands.entity(render_entity); + commands.insert(( + ExtractedCamera { + target: render_target.normalize(primary_window), + viewport: camera.viewport.clone(), + physical_viewport_size: Some(viewport_size), + physical_target_size: Some(target_size), + render_graph: camera_render_graph.0, + order: camera.order, + output_mode: camera.output_mode, + msaa_writeback: camera.msaa_writeback, + clear_color: camera.clear_color, + // this will be set in sort_cameras + sorted_camera_index_for_target: 0, + exposure: exposure + .map(Exposure::exposure) + .unwrap_or_else(|| Exposure::default().exposure()), + hdr, + }, + ExtractedView { + retained_view_entity: RetainedViewEntity::new(main_entity.into(), None, 0), + clip_from_view: camera.clip_from_view(), + world_from_view: *transform, + clip_from_world: None, + hdr, + viewport: UVec4::new( + viewport_origin.x, + viewport_origin.y, + viewport_size.x, + viewport_size.y, + ), + color_grading, + invert_culling: camera.invert_culling, + }, + render_visible_entities, + *frustum, + )); + + if let Some(temporal_jitter) = temporal_jitter { + commands.insert(temporal_jitter.clone()); + } else { + commands.remove::(); + } + + if let Some(mip_bias) = mip_bias { + commands.insert(mip_bias.clone()); + } else { + commands.remove::(); + } + + if let Some(render_layers) = render_layers { + commands.insert(render_layers.clone()); + } else { + commands.remove::(); + } + + if let Some(projection) = projection { + commands.insert(projection.clone()); + } else { + commands.remove::(); + } + + if no_indirect_drawing + || !matches!( + gpu_preprocessing_support.max_supported_mode, + GpuPreprocessingMode::Culling + ) + { + commands.insert(NoIndirectDrawing); + } else { + commands.remove::(); + } + }; + } +} + +/// Cameras sorted by their order field. This is updated in the [`sort_cameras`] system. +#[derive(Resource, Default)] +pub struct SortedCameras(pub Vec); + +pub struct SortedCamera { + pub entity: Entity, + pub order: isize, + pub target: Option, + pub hdr: bool, +} + +pub fn sort_cameras( + mut sorted_cameras: ResMut, + mut cameras: Query<(Entity, &mut ExtractedCamera)>, +) { + sorted_cameras.0.clear(); + for (entity, camera) in cameras.iter() { + sorted_cameras.0.push(SortedCamera { + entity, + order: camera.order, + target: camera.target.clone(), + hdr: camera.hdr, + }); + } + // sort by order and ensure within an order, RenderTargets of the same type are packed together + sorted_cameras + .0 + .sort_by(|c1, c2| (c1.order, &c1.target).cmp(&(c2.order, &c2.target))); + let mut previous_order_target = None; + let mut ambiguities = >::default(); + let mut target_counts = >::default(); + for sorted_camera in &mut sorted_cameras.0 { + let new_order_target = (sorted_camera.order, sorted_camera.target.clone()); + if let Some(previous_order_target) = previous_order_target + && previous_order_target == new_order_target + { + ambiguities.insert(new_order_target.clone()); + } + if let Some(target) = &sorted_camera.target { + let count = target_counts + .entry((target.clone(), sorted_camera.hdr)) + .or_insert(0usize); + let (_, mut camera) = cameras.get_mut(sorted_camera.entity).unwrap(); + camera.sorted_camera_index_for_target = *count; + *count += 1; + } + previous_order_target = Some(new_order_target); + } + + if !ambiguities.is_empty() { + warn!( + "Camera order ambiguities detected for active cameras with the following priorities: {:?}. \ + To fix this, ensure there is exactly one Camera entity spawned with a given order for a given RenderTarget. \ + Ambiguities should be resolved because either (1) multiple active cameras were spawned accidentally, which will \ + result in rendering multiple instances of the scene or (2) for cases where multiple active cameras is intentional, \ + ambiguities could result in unpredictable render results.", + ambiguities + ); + } +} + +/// A subpixel offset to jitter a perspective camera's frustum by. +/// +/// Useful for temporal rendering techniques. +#[derive(Component, Clone, Default, Reflect)] +#[reflect(Default, Component, Clone)] +pub struct TemporalJitter { + /// Offset is in range [-0.5, 0.5]. + pub offset: Vec2, +} + +impl TemporalJitter { + pub fn jitter_projection(&self, clip_from_view: &mut Mat4, view_size: Vec2) { + // https://github.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK/blob/d7531ae47d8b36a5d4025663e731a47a38be882f/docs/techniques/media/super-resolution-temporal/jitter-space.svg + let mut jitter = (self.offset * vec2(2.0, -2.0)) / view_size; + + // orthographic + if clip_from_view.w_axis.w == 1.0 { + jitter *= vec2(clip_from_view.x_axis.x, clip_from_view.y_axis.y) * 0.5; + } + + clip_from_view.z_axis.x += jitter.x; + clip_from_view.z_axis.y += jitter.y; + } +} + +/// Camera component specifying a mip bias to apply when sampling from material textures. +/// +/// Often used in conjunction with antialiasing post-process effects to reduce textures blurriness. +#[derive(Component, Reflect, Clone)] +#[reflect(Default, Component)] +pub struct MipBias(pub f32); + +impl Default for MipBias { + fn default() -> Self { + Self(-1.0) + } +} diff --git a/third_party/bevy_render/src/color_operations.wgsl b/third_party/bevy_render/src/color_operations.wgsl new file mode 100644 index 0000000..b68ad2a --- /dev/null +++ b/third_party/bevy_render/src/color_operations.wgsl @@ -0,0 +1,47 @@ +#define_import_path bevy_render::color_operations + +#import bevy_render::maths::FRAC_PI_3 + +// Converts HSV to RGB. +// +// Input: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1]. +// Output: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1]. +// +// +fn hsv_to_rgb(hsv: vec3) -> vec3 { + let n = vec3(5.0, 3.0, 1.0); + let k = (n + hsv.x / FRAC_PI_3) % 6.0; + return hsv.z - hsv.z * hsv.y * max(vec3(0.0), min(k, min(4.0 - k, vec3(1.0)))); +} + +// Converts RGB to HSV. +// +// Input: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1]. +// Output: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1]. +// +// +fn rgb_to_hsv(rgb: vec3) -> vec3 { + let x_max = max(rgb.r, max(rgb.g, rgb.b)); // i.e. V + let x_min = min(rgb.r, min(rgb.g, rgb.b)); + let c = x_max - x_min; // chroma + + var swizzle = vec3(0.0); + if (x_max == rgb.r) { + swizzle = vec3(rgb.gb, 0.0); + } else if (x_max == rgb.g) { + swizzle = vec3(rgb.br, 2.0); + } else { + swizzle = vec3(rgb.rg, 4.0); + } + + let h = FRAC_PI_3 * (((swizzle.x - swizzle.y) / c + swizzle.z) % 6.0); + + // Avoid division by zero. + var s = 0.0; + if (x_max > 0.0) { + s = c / x_max; + } + + return vec3(h, s, x_max); +} + diff --git a/third_party/bevy_render/src/diagnostic/erased_render_asset_diagnostic_plugin.rs b/third_party/bevy_render/src/diagnostic/erased_render_asset_diagnostic_plugin.rs new file mode 100644 index 0000000..62cfa68 --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/erased_render_asset_diagnostic_plugin.rs @@ -0,0 +1,81 @@ +use core::{any::type_name, marker::PhantomData}; + +use bevy_app::{Plugin, PreUpdate}; +use bevy_diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic}; +use bevy_ecs::{resource::Resource, system::Res}; +use bevy_platform::sync::atomic::{AtomicUsize, Ordering}; + +use crate::{ + erased_render_asset::{ErasedRenderAsset, ErasedRenderAssets}, + Extract, ExtractSchedule, RenderApp, +}; + +/// Collects diagnostics for a [`ErasedRenderAsset`]. +/// +/// If the [`ErasedRenderAsset::ErasedAsset`] is shared between other +/// [`ErasedRenderAsset`], they all will report the same number. +pub struct ErasedRenderAssetDiagnosticPlugin { + suffix: &'static str, + _phantom: PhantomData, +} + +impl ErasedRenderAssetDiagnosticPlugin { + pub fn new(suffix: &'static str) -> Self { + Self { + suffix, + _phantom: PhantomData, + } + } + + pub fn render_asset_diagnostic_path() -> DiagnosticPath { + DiagnosticPath::from_components(["erased_render_asset", type_name::()]) + } +} + +impl Plugin for ErasedRenderAssetDiagnosticPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.register_diagnostic( + Diagnostic::new(Self::render_asset_diagnostic_path()).with_suffix(self.suffix), + ) + .init_resource::>() + .add_systems(PreUpdate, add_erased_render_asset_measurement::); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems(ExtractSchedule, measure_erased_render_asset::); + } + } +} + +#[derive(Debug, Resource)] +struct ErasedRenderAssetMeasurements { + assets: AtomicUsize, + _phantom: PhantomData, +} + +impl Default for ErasedRenderAssetMeasurements { + fn default() -> Self { + Self { + assets: AtomicUsize::default(), + _phantom: PhantomData, + } + } +} + +fn add_erased_render_asset_measurement( + mut diagnostics: Diagnostics, + measurements: Res>, +) { + diagnostics.add_measurement( + &ErasedRenderAssetDiagnosticPlugin::::render_asset_diagnostic_path(), + || measurements.assets.load(Ordering::Relaxed) as f64, + ); +} + +fn measure_erased_render_asset( + measurements: Extract>>, + assets: Res>, +) { + measurements + .assets + .store(assets.iter().count(), Ordering::Relaxed); +} diff --git a/third_party/bevy_render/src/diagnostic/internal.rs b/third_party/bevy_render/src/diagnostic/internal.rs new file mode 100644 index 0000000..641f5a9 --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/internal.rs @@ -0,0 +1,711 @@ +use alloc::{borrow::Cow, sync::Arc}; +use core::{ + ops::{DerefMut, Range}, + sync::atomic::{AtomicBool, Ordering}, +}; +use std::thread::{self, ThreadId}; + +use bevy_diagnostic::{Diagnostic, DiagnosticMeasurement, DiagnosticPath, DiagnosticsStore}; +use bevy_ecs::resource::Resource; +use bevy_ecs::system::{Res, ResMut}; +use bevy_platform::time::Instant; +use std::sync::Mutex; +use wgpu::{ + Buffer, BufferDescriptor, BufferUsages, CommandEncoder, ComputePass, Features, MapMode, + PipelineStatisticsTypes, QuerySet, QuerySetDescriptor, QueryType, RenderPass, +}; + +use crate::renderer::{RenderAdapterInfo, RenderDevice, RenderQueue, WgpuWrapper}; + +use super::RecordDiagnostics; + +// buffer offset must be divisible by 256, so this constant must be divisible by 32 (=256/8) +const MAX_TIMESTAMP_QUERIES: u32 = 256; +const MAX_PIPELINE_STATISTICS: u32 = 128; + +const TIMESTAMP_SIZE: u64 = 8; +const PIPELINE_STATISTICS_SIZE: u64 = 40; + +struct DiagnosticsRecorderInternal { + timestamp_period_ns: f32, + features: Features, + current_frame: Mutex, + submitted_frames: Vec, + finished_frames: Vec, + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context: tracy_client::GpuContext, +} + +/// Records diagnostics into [`QuerySet`]'s keeping track of the mapping between +/// spans and indices to the corresponding entries in the [`QuerySet`]. +#[derive(Resource)] +pub struct DiagnosticsRecorder(WgpuWrapper); + +impl DiagnosticsRecorder { + /// Creates the new `DiagnosticsRecorder`. + pub fn new( + adapter_info: &RenderAdapterInfo, + device: &RenderDevice, + queue: &RenderQueue, + ) -> DiagnosticsRecorder { + let features = device.features(); + + #[cfg(feature = "tracing-tracy")] + let tracy_gpu_context = + super::tracy_gpu::new_tracy_gpu_context(adapter_info, device, queue); + let _ = adapter_info; // Prevent unused variable warnings when tracing-tracy is not enabled + + DiagnosticsRecorder(WgpuWrapper::new(DiagnosticsRecorderInternal { + timestamp_period_ns: queue.get_timestamp_period(), + features, + current_frame: Mutex::new(FrameData::new( + device, + features, + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context.clone(), + )), + submitted_frames: Vec::new(), + finished_frames: Vec::new(), + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context, + })) + } + + fn current_frame_mut(&mut self) -> &mut FrameData { + self.0.current_frame.get_mut().expect("lock poisoned") + } + + fn current_frame_lock(&self) -> impl DerefMut + '_ { + self.0.current_frame.lock().expect("lock poisoned") + } + + /// Begins recording diagnostics for a new frame. + pub fn begin_frame(&mut self) { + let internal = &mut self.0; + let mut idx = 0; + while idx < internal.submitted_frames.len() { + let timestamp = internal.timestamp_period_ns; + if internal.submitted_frames[idx].run_mapped_callback(timestamp) { + let removed = internal.submitted_frames.swap_remove(idx); + internal.finished_frames.push(removed); + } else { + idx += 1; + } + } + + self.current_frame_mut().begin(); + } + + /// Copies data from [`QuerySet`]'s to a [`Buffer`], after which it can be downloaded to CPU. + /// + /// Should be called before [`DiagnosticsRecorder::finish_frame`]. + pub fn resolve(&mut self, encoder: &mut CommandEncoder) { + self.current_frame_mut().resolve(encoder); + } + + /// Finishes recording diagnostics for the current frame. + /// + /// The specified `callback` will be invoked when diagnostics become available. + /// + /// Should be called after [`DiagnosticsRecorder::resolve`], + /// and **after** all commands buffers have been queued. + pub fn finish_frame( + &mut self, + device: &RenderDevice, + callback: impl FnOnce(RenderDiagnostics) + Send + Sync + 'static, + ) { + #[cfg(feature = "tracing-tracy")] + let tracy_gpu_context = self.0.tracy_gpu_context.clone(); + + let internal = &mut self.0; + internal + .current_frame + .get_mut() + .expect("lock poisoned") + .finish(callback); + + // reuse one of the finished frames, if we can + let new_frame = match internal.finished_frames.pop() { + Some(frame) => frame, + None => FrameData::new( + device, + internal.features, + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context, + ), + }; + + let old_frame = core::mem::replace( + internal.current_frame.get_mut().expect("lock poisoned"), + new_frame, + ); + internal.submitted_frames.push(old_frame); + } +} + +impl RecordDiagnostics for DiagnosticsRecorder { + fn begin_time_span(&self, encoder: &mut E, span_name: Cow<'static, str>) { + self.current_frame_lock() + .begin_time_span(encoder, span_name); + } + + fn end_time_span(&self, encoder: &mut E) { + self.current_frame_lock().end_time_span(encoder); + } + + fn begin_pass_span(&self, pass: &mut P, span_name: Cow<'static, str>) { + self.current_frame_lock().begin_pass(pass, span_name); + } + + fn end_pass_span(&self, pass: &mut P) { + self.current_frame_lock().end_pass(pass); + } +} + +struct SpanRecord { + thread_id: ThreadId, + path_range: Range, + pass_kind: Option, + begin_timestamp_index: Option, + end_timestamp_index: Option, + begin_instant: Option, + end_instant: Option, + pipeline_statistics_index: Option, +} + +struct FrameData { + timestamps_query_set: Option, + num_timestamps: u32, + supports_timestamps_inside_passes: bool, + supports_timestamps_inside_encoders: bool, + pipeline_statistics_query_set: Option, + num_pipeline_statistics: u32, + buffer_size: u64, + pipeline_statistics_buffer_offset: u64, + resolve_buffer: Option, + read_buffer: Option, + path_components: Vec>, + open_spans: Vec, + closed_spans: Vec, + is_mapped: Arc, + callback: Option>, + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context: tracy_client::GpuContext, +} + +impl FrameData { + fn new( + device: &RenderDevice, + features: Features, + #[cfg(feature = "tracing-tracy")] tracy_gpu_context: tracy_client::GpuContext, + ) -> FrameData { + let wgpu_device = device.wgpu_device(); + let mut buffer_size = 0; + + let timestamps_query_set = if features.contains(Features::TIMESTAMP_QUERY) { + buffer_size += u64::from(MAX_TIMESTAMP_QUERIES) * TIMESTAMP_SIZE; + Some(wgpu_device.create_query_set(&QuerySetDescriptor { + label: Some("timestamps_query_set"), + ty: QueryType::Timestamp, + count: MAX_TIMESTAMP_QUERIES, + })) + } else { + None + }; + + let pipeline_statistics_buffer_offset = buffer_size; + + let pipeline_statistics_query_set = + if features.contains(Features::PIPELINE_STATISTICS_QUERY) { + buffer_size += u64::from(MAX_PIPELINE_STATISTICS) * PIPELINE_STATISTICS_SIZE; + Some(wgpu_device.create_query_set(&QuerySetDescriptor { + label: Some("pipeline_statistics_query_set"), + ty: QueryType::PipelineStatistics(PipelineStatisticsTypes::all()), + count: MAX_PIPELINE_STATISTICS, + })) + } else { + None + }; + + let (resolve_buffer, read_buffer) = if buffer_size > 0 { + let resolve_buffer = wgpu_device.create_buffer(&BufferDescriptor { + label: Some("render_statistics_resolve_buffer"), + size: buffer_size, + usage: BufferUsages::QUERY_RESOLVE | BufferUsages::COPY_SRC, + mapped_at_creation: false, + }); + let read_buffer = wgpu_device.create_buffer(&BufferDescriptor { + label: Some("render_statistics_read_buffer"), + size: buffer_size, + usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + (Some(resolve_buffer), Some(read_buffer)) + } else { + (None, None) + }; + + FrameData { + timestamps_query_set, + num_timestamps: 0, + supports_timestamps_inside_passes: features + .contains(Features::TIMESTAMP_QUERY_INSIDE_PASSES), + supports_timestamps_inside_encoders: features + .contains(Features::TIMESTAMP_QUERY_INSIDE_ENCODERS), + pipeline_statistics_query_set, + num_pipeline_statistics: 0, + buffer_size, + pipeline_statistics_buffer_offset, + resolve_buffer, + read_buffer, + path_components: Vec::new(), + open_spans: Vec::new(), + closed_spans: Vec::new(), + is_mapped: Arc::new(AtomicBool::new(false)), + callback: None, + #[cfg(feature = "tracing-tracy")] + tracy_gpu_context, + } + } + + fn begin(&mut self) { + self.num_timestamps = 0; + self.num_pipeline_statistics = 0; + self.path_components.clear(); + self.open_spans.clear(); + self.closed_spans.clear(); + } + + fn write_timestamp( + &mut self, + encoder: &mut impl WriteTimestamp, + is_inside_pass: bool, + ) -> Option { + // `encoder.write_timestamp` is unsupported on WebGPU. + if !self.supports_timestamps_inside_encoders { + return None; + } + + if is_inside_pass && !self.supports_timestamps_inside_passes { + return None; + } + + if self.num_timestamps >= MAX_TIMESTAMP_QUERIES { + return None; + } + + let set = self.timestamps_query_set.as_ref()?; + let index = self.num_timestamps; + encoder.write_timestamp(set, index); + self.num_timestamps += 1; + Some(index) + } + + fn write_pipeline_statistics( + &mut self, + encoder: &mut impl WritePipelineStatistics, + ) -> Option { + if self.num_pipeline_statistics >= MAX_PIPELINE_STATISTICS { + return None; + } + + let set = self.pipeline_statistics_query_set.as_ref()?; + let index = self.num_pipeline_statistics; + encoder.begin_pipeline_statistics_query(set, index); + self.num_pipeline_statistics += 1; + Some(index) + } + + fn open_span( + &mut self, + pass_kind: Option, + name: Cow<'static, str>, + ) -> &mut SpanRecord { + let thread_id = thread::current().id(); + + let parent = self.open_spans.iter().rfind(|v| v.thread_id == thread_id); + + let path_range = match &parent { + Some(parent) if parent.path_range.end == self.path_components.len() => { + parent.path_range.start..parent.path_range.end + 1 + } + Some(parent) => { + self.path_components + .extend_from_within(parent.path_range.clone()); + self.path_components.len() - parent.path_range.len()..self.path_components.len() + 1 + } + None => self.path_components.len()..self.path_components.len() + 1, + }; + + self.path_components.push(name); + + self.open_spans.push(SpanRecord { + thread_id, + path_range, + pass_kind, + begin_timestamp_index: None, + end_timestamp_index: None, + begin_instant: None, + end_instant: None, + pipeline_statistics_index: None, + }); + + self.open_spans.last_mut().unwrap() + } + + fn close_span(&mut self) -> &mut SpanRecord { + let thread_id = thread::current().id(); + + let iter = self.open_spans.iter(); + let (index, _) = iter + .enumerate() + .rfind(|(_, v)| v.thread_id == thread_id) + .unwrap(); + + let span = self.open_spans.swap_remove(index); + self.closed_spans.push(span); + self.closed_spans.last_mut().unwrap() + } + + fn begin_time_span(&mut self, encoder: &mut impl WriteTimestamp, name: Cow<'static, str>) { + let begin_instant = Instant::now(); + let begin_timestamp_index = self.write_timestamp(encoder, false); + + let span = self.open_span(None, name); + span.begin_instant = Some(begin_instant); + span.begin_timestamp_index = begin_timestamp_index; + } + + fn end_time_span(&mut self, encoder: &mut impl WriteTimestamp) { + let end_timestamp_index = self.write_timestamp(encoder, false); + + let span = self.close_span(); + span.end_timestamp_index = end_timestamp_index; + span.end_instant = Some(Instant::now()); + } + + fn begin_pass(&mut self, pass: &mut P, name: Cow<'static, str>) { + let begin_instant = Instant::now(); + + let begin_timestamp_index = self.write_timestamp(pass, true); + let pipeline_statistics_index = self.write_pipeline_statistics(pass); + + let span = self.open_span(Some(P::KIND), name); + span.begin_instant = Some(begin_instant); + span.begin_timestamp_index = begin_timestamp_index; + span.pipeline_statistics_index = pipeline_statistics_index; + } + + fn end_pass(&mut self, pass: &mut impl Pass) { + let end_timestamp_index = self.write_timestamp(pass, true); + + let span = self.close_span(); + span.end_timestamp_index = end_timestamp_index; + + if span.pipeline_statistics_index.is_some() { + pass.end_pipeline_statistics_query(); + } + + span.end_instant = Some(Instant::now()); + } + + fn resolve(&mut self, encoder: &mut CommandEncoder) { + let Some(resolve_buffer) = &self.resolve_buffer else { + return; + }; + + match &self.timestamps_query_set { + Some(set) if self.num_timestamps > 0 => { + encoder.resolve_query_set(set, 0..self.num_timestamps, resolve_buffer, 0); + } + _ => {} + } + + match &self.pipeline_statistics_query_set { + Some(set) if self.num_pipeline_statistics > 0 => { + encoder.resolve_query_set( + set, + 0..self.num_pipeline_statistics, + resolve_buffer, + self.pipeline_statistics_buffer_offset, + ); + } + _ => {} + } + + let Some(read_buffer) = &self.read_buffer else { + return; + }; + + encoder.copy_buffer_to_buffer(resolve_buffer, 0, read_buffer, 0, self.buffer_size); + } + + fn diagnostic_path(&self, range: &Range, field: &str) -> DiagnosticPath { + DiagnosticPath::from_components( + core::iter::once("render") + .chain(self.path_components[range.clone()].iter().map(|v| &**v)) + .chain(core::iter::once(field)), + ) + } + + fn finish(&mut self, callback: impl FnOnce(RenderDiagnostics) + Send + Sync + 'static) { + let Some(read_buffer) = &self.read_buffer else { + // we still have cpu timings, so let's use them + + let mut diagnostics = Vec::new(); + + for span in &self.closed_spans { + if let (Some(begin), Some(end)) = (span.begin_instant, span.end_instant) { + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "elapsed_cpu"), + suffix: "ms", + value: (end - begin).as_secs_f64() * 1000.0, + }); + } + } + + callback(RenderDiagnostics(diagnostics)); + return; + }; + + self.callback = Some(Box::new(callback)); + + let is_mapped = self.is_mapped.clone(); + read_buffer.slice(..).map_async(MapMode::Read, move |res| { + if let Err(e) = res { + tracing::warn!("Failed to download render statistics buffer: {e}"); + return; + } + + is_mapped.store(true, Ordering::Release); + }); + } + + // returns true if the frame is considered finished, false otherwise + fn run_mapped_callback(&mut self, timestamp_period_ns: f32) -> bool { + let Some(read_buffer) = &self.read_buffer else { + return true; + }; + if !self.is_mapped.load(Ordering::Acquire) { + // need to wait more + return false; + } + let Some(callback) = self.callback.take() else { + return true; + }; + + let data = read_buffer.slice(..).get_mapped_range(); + + let timestamps = data[..(self.num_timestamps * 8) as usize] + .chunks(8) + .map(|v| u64::from_le_bytes(v.try_into().unwrap())) + .collect::>(); + + let start = self.pipeline_statistics_buffer_offset as usize; + let len = (self.num_pipeline_statistics as usize) * 40; + let pipeline_statistics = data[start..start + len] + .chunks(8) + .map(|v| u64::from_le_bytes(v.try_into().unwrap())) + .collect::>(); + + let mut diagnostics = Vec::new(); + + for span in &self.closed_spans { + if let (Some(begin), Some(end)) = (span.begin_instant, span.end_instant) { + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "elapsed_cpu"), + suffix: "ms", + value: (end - begin).as_secs_f64() * 1000.0, + }); + } + + if let (Some(begin), Some(end)) = (span.begin_timestamp_index, span.end_timestamp_index) + { + let begin = timestamps[begin as usize] as f64; + let end = timestamps[end as usize] as f64; + let value = (end - begin) * (timestamp_period_ns as f64) / 1e6; + + #[cfg(feature = "tracing-tracy")] + { + // Calling span_alloc() and end_zone() here instead of in open_span() and close_span() means that tracy does not know where each GPU command was recorded on the CPU timeline. + // Unfortunately we must do it this way, because tracy does not play nicely with multithreaded command recording. The start/end pairs would get all mixed up. + // The GPU spans themselves are still accurate though, and it's probably safe to assume that each GPU span in frame N belongs to the corresponding CPU render node span from frame N-1. + let name = &self.path_components[span.path_range.clone()].join("/"); + let mut tracy_gpu_span = + self.tracy_gpu_context.span_alloc(name, "", "", 0).unwrap(); + tracy_gpu_span.end_zone(); + tracy_gpu_span.upload_timestamp_start(begin as i64); + tracy_gpu_span.upload_timestamp_end(end as i64); + } + + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "elapsed_gpu"), + suffix: "ms", + value, + }); + } + + if let Some(index) = span.pipeline_statistics_index { + let index = (index as usize) * 5; + + if span.pass_kind == Some(PassKind::Render) { + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "vertex_shader_invocations"), + suffix: "", + value: pipeline_statistics[index] as f64, + }); + + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "clipper_invocations"), + suffix: "", + value: pipeline_statistics[index + 1] as f64, + }); + + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "clipper_primitives_out"), + suffix: "", + value: pipeline_statistics[index + 2] as f64, + }); + + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "fragment_shader_invocations"), + suffix: "", + value: pipeline_statistics[index + 3] as f64, + }); + } + + if span.pass_kind == Some(PassKind::Compute) { + diagnostics.push(RenderDiagnostic { + path: self.diagnostic_path(&span.path_range, "compute_shader_invocations"), + suffix: "", + value: pipeline_statistics[index + 4] as f64, + }); + } + } + } + + callback(RenderDiagnostics(diagnostics)); + + drop(data); + read_buffer.unmap(); + self.is_mapped.store(false, Ordering::Release); + + true + } +} + +/// Resource which stores render diagnostics of the most recent frame. +#[derive(Debug, Default, Clone, Resource)] +pub struct RenderDiagnostics(Vec); + +/// A render diagnostic which has been recorded, but not yet stored in [`DiagnosticsStore`]. +#[derive(Debug, Clone, Resource)] +pub struct RenderDiagnostic { + pub path: DiagnosticPath, + pub suffix: &'static str, + pub value: f64, +} + +/// Stores render diagnostics before they can be synced with the main app. +/// +/// This mutex is locked twice per frame: +/// 1. in `PreUpdate`, during [`sync_diagnostics`], +/// 2. after rendering has finished and statistics have been downloaded from GPU. +#[derive(Debug, Default, Clone, Resource)] +pub struct RenderDiagnosticsMutex(pub(crate) Arc>>); + +/// Updates render diagnostics measurements. +pub fn sync_diagnostics(mutex: Res, mut store: ResMut) { + let Some(diagnostics) = mutex.0.lock().ok().and_then(|mut v| v.take()) else { + return; + }; + + let time = Instant::now(); + + for diagnostic in &diagnostics.0 { + if store.get(&diagnostic.path).is_none() { + store.add(Diagnostic::new(diagnostic.path.clone()).with_suffix(diagnostic.suffix)); + } + + store + .get_mut(&diagnostic.path) + .unwrap() + .add_measurement(DiagnosticMeasurement { + time, + value: diagnostic.value, + }); + } +} + +pub trait WriteTimestamp { + fn write_timestamp(&mut self, query_set: &QuerySet, index: u32); +} + +impl WriteTimestamp for CommandEncoder { + fn write_timestamp(&mut self, query_set: &QuerySet, index: u32) { + if cfg!(target_os = "macos") { + // When using tracy (and thus this function), rendering was flickering on macOS Tahoe. + // See: https://github.com/bevyengine/bevy/issues/22257 + // The issue seems to be triggered when `write_timestamp` is called very close to frame + // presentation. + return; + } + CommandEncoder::write_timestamp(self, query_set, index); + } +} + +impl WriteTimestamp for RenderPass<'_> { + fn write_timestamp(&mut self, query_set: &QuerySet, index: u32) { + RenderPass::write_timestamp(self, query_set, index); + } +} + +impl WriteTimestamp for ComputePass<'_> { + fn write_timestamp(&mut self, query_set: &QuerySet, index: u32) { + ComputePass::write_timestamp(self, query_set, index); + } +} + +pub trait WritePipelineStatistics { + fn begin_pipeline_statistics_query(&mut self, query_set: &QuerySet, index: u32); + + fn end_pipeline_statistics_query(&mut self); +} + +impl WritePipelineStatistics for RenderPass<'_> { + fn begin_pipeline_statistics_query(&mut self, query_set: &QuerySet, index: u32) { + RenderPass::begin_pipeline_statistics_query(self, query_set, index); + } + + fn end_pipeline_statistics_query(&mut self) { + RenderPass::end_pipeline_statistics_query(self); + } +} + +impl WritePipelineStatistics for ComputePass<'_> { + fn begin_pipeline_statistics_query(&mut self, query_set: &QuerySet, index: u32) { + ComputePass::begin_pipeline_statistics_query(self, query_set, index); + } + + fn end_pipeline_statistics_query(&mut self) { + ComputePass::end_pipeline_statistics_query(self); + } +} + +pub trait Pass: WritePipelineStatistics + WriteTimestamp { + const KIND: PassKind; +} + +impl Pass for RenderPass<'_> { + const KIND: PassKind = PassKind::Render; +} + +impl Pass for ComputePass<'_> { + const KIND: PassKind = PassKind::Compute; +} + +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub enum PassKind { + Render, + Compute, +} diff --git a/third_party/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs b/third_party/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs new file mode 100644 index 0000000..cd0b70d --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs @@ -0,0 +1,91 @@ +use bevy_app::{Plugin, PreUpdate}; +use bevy_diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic}; +use bevy_ecs::{resource::Resource, system::Res}; +use bevy_platform::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; + +use crate::{mesh::allocator::MeshAllocator, Extract, ExtractSchedule, RenderApp}; + +/// Number of meshes allocated by the allocator +static MESH_ALLOCATOR_SLABS: DiagnosticPath = DiagnosticPath::const_new("mesh_allocator_slabs"); + +/// Total size of all slabs +static MESH_ALLOCATOR_SLABS_SIZE: DiagnosticPath = + DiagnosticPath::const_new("mesh_allocator_slabs_size"); + +/// Number of meshes allocated into slabs +static MESH_ALLOCATOR_ALLOCATIONS: DiagnosticPath = + DiagnosticPath::const_new("mesh_allocator_allocations"); + +pub struct MeshAllocatorDiagnosticPlugin; + +impl MeshAllocatorDiagnosticPlugin { + /// Get the [`DiagnosticPath`] for slab count + pub fn slabs_diagnostic_path() -> &'static DiagnosticPath { + &MESH_ALLOCATOR_SLABS + } + /// Get the [`DiagnosticPath`] for total slabs size + pub fn slabs_size_diagnostic_path() -> &'static DiagnosticPath { + &MESH_ALLOCATOR_SLABS_SIZE + } + /// Get the [`DiagnosticPath`] for mesh allocations + pub fn allocations_diagnostic_path() -> &'static DiagnosticPath { + &MESH_ALLOCATOR_ALLOCATIONS + } +} + +impl Plugin for MeshAllocatorDiagnosticPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.register_diagnostic( + Diagnostic::new(MESH_ALLOCATOR_SLABS.clone()).with_suffix(" slabs"), + ) + .register_diagnostic( + Diagnostic::new(MESH_ALLOCATOR_SLABS_SIZE.clone()).with_suffix(" bytes"), + ) + .register_diagnostic( + Diagnostic::new(MESH_ALLOCATOR_ALLOCATIONS.clone()).with_suffix(" meshes"), + ) + .init_resource::() + .add_systems(PreUpdate, add_mesh_allocator_measurement); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems(ExtractSchedule, measure_allocator); + } + } +} + +#[derive(Debug, Default, Resource)] +struct MeshAllocatorMeasurements { + slabs: AtomicUsize, + slabs_size: AtomicU64, + allocations: AtomicUsize, +} + +fn add_mesh_allocator_measurement( + mut diagnostics: Diagnostics, + measurements: Res, +) { + diagnostics.add_measurement(&MESH_ALLOCATOR_SLABS, || { + measurements.slabs.load(Ordering::Relaxed) as f64 + }); + diagnostics.add_measurement(&MESH_ALLOCATOR_SLABS_SIZE, || { + measurements.slabs_size.load(Ordering::Relaxed) as f64 + }); + diagnostics.add_measurement(&MESH_ALLOCATOR_ALLOCATIONS, || { + measurements.allocations.load(Ordering::Relaxed) as f64 + }); +} + +fn measure_allocator( + measurements: Extract>, + allocator: Res, +) { + measurements + .slabs + .store(allocator.slab_count(), Ordering::Relaxed); + measurements + .slabs_size + .store(allocator.slabs_size(), Ordering::Relaxed); + measurements + .allocations + .store(allocator.allocations(), Ordering::Relaxed); +} diff --git a/third_party/bevy_render/src/diagnostic/mod.rs b/third_party/bevy_render/src/diagnostic/mod.rs new file mode 100644 index 0000000..d2425d3 --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/mod.rs @@ -0,0 +1,199 @@ +//! Infrastructure for recording render diagnostics. +//! +//! For more info, see [`RenderDiagnosticsPlugin`]. + +mod erased_render_asset_diagnostic_plugin; +pub(crate) mod internal; +mod mesh_allocator_diagnostic_plugin; +mod render_asset_diagnostic_plugin; +#[cfg(feature = "tracing-tracy")] +mod tracy_gpu; + +use alloc::{borrow::Cow, sync::Arc}; +use core::marker::PhantomData; + +use bevy_app::{App, Plugin, PreUpdate}; + +use crate::{renderer::RenderAdapterInfo, RenderApp}; + +use self::internal::{ + sync_diagnostics, DiagnosticsRecorder, Pass, RenderDiagnosticsMutex, WriteTimestamp, +}; +pub use self::{ + erased_render_asset_diagnostic_plugin::ErasedRenderAssetDiagnosticPlugin, + mesh_allocator_diagnostic_plugin::MeshAllocatorDiagnosticPlugin, + render_asset_diagnostic_plugin::RenderAssetDiagnosticPlugin, +}; + +use crate::renderer::{RenderDevice, RenderQueue}; + +/// Enables collecting render diagnostics, such as CPU/GPU elapsed time per render pass, +/// as well as pipeline statistics (number of primitives, number of shader invocations, etc). +/// +/// To access the diagnostics, you can use the [`DiagnosticsStore`](bevy_diagnostic::DiagnosticsStore) resource, +/// add [`LogDiagnosticsPlugin`](bevy_diagnostic::LogDiagnosticsPlugin), or use [Tracy](https://github.com/bevyengine/bevy/blob/main/docs/profiling.md#tracy-renderqueue). +/// +/// To record diagnostics in your own passes: +/// 1. First, obtain the diagnostic recorder using [`RenderContext::diagnostic_recorder`](crate::renderer::RenderContext::diagnostic_recorder). +/// +/// It won't do anything unless [`RenderDiagnosticsPlugin`] is present, +/// so you're free to omit `#[cfg]` clauses. +/// ```ignore +/// let diagnostics = render_context.diagnostic_recorder(); +/// ``` +/// 2. Begin the span inside a command encoder, or a render/compute pass encoder. +/// ```ignore +/// let time_span = diagnostics.time_span(render_context.command_encoder(), "shadows"); +/// ``` +/// 3. End the span, providing the same encoder. +/// ```ignore +/// time_span.end(render_context.command_encoder()); +/// ``` +/// +/// # Supported platforms +/// Timestamp queries and pipeline statistics are currently supported only on Vulkan and DX12. +/// On other platforms (Metal, WebGPU, WebGL2) only CPU time will be recorded. +#[derive(Default)] +pub struct RenderDiagnosticsPlugin; + +impl Plugin for RenderDiagnosticsPlugin { + fn build(&self, app: &mut App) { + let render_diagnostics_mutex = RenderDiagnosticsMutex::default(); + app.insert_resource(render_diagnostics_mutex.clone()) + .add_systems(PreUpdate, sync_diagnostics); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.insert_resource(render_diagnostics_mutex); + } + } + + fn finish(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + let adapter_info = render_app.world().resource::(); + let device = render_app.world().resource::(); + let queue = render_app.world().resource::(); + render_app.insert_resource(DiagnosticsRecorder::new(adapter_info, device, queue)); + } +} + +/// Allows recording diagnostic spans. +pub trait RecordDiagnostics: Send + Sync { + /// Begin a time span, which will record elapsed CPU and GPU time. + /// + /// Returns a guard, which will panic on drop unless you end the span. + fn time_span(&self, encoder: &mut E, name: N) -> TimeSpanGuard<'_, Self, E> + where + E: WriteTimestamp, + N: Into>, + { + self.begin_time_span(encoder, name.into()); + TimeSpanGuard { + recorder: self, + marker: PhantomData, + } + } + + /// Begin a pass span, which will record elapsed CPU and GPU time, + /// as well as pipeline statistics on supported platforms. + /// + /// Returns a guard, which will panic on drop unless you end the span. + fn pass_span(&self, pass: &mut P, name: N) -> PassSpanGuard<'_, Self, P> + where + P: Pass, + N: Into>, + { + let name = name.into(); + self.begin_pass_span(pass, name.clone()); + PassSpanGuard { + recorder: self, + name, + marker: PhantomData, + } + } + + #[doc(hidden)] + fn begin_time_span(&self, encoder: &mut E, name: Cow<'static, str>); + + #[doc(hidden)] + fn end_time_span(&self, encoder: &mut E); + + #[doc(hidden)] + fn begin_pass_span(&self, pass: &mut P, name: Cow<'static, str>); + + #[doc(hidden)] + fn end_pass_span(&self, pass: &mut P); +} + +/// Guard returned by [`RecordDiagnostics::time_span`]. +/// +/// Will panic on drop unless [`TimeSpanGuard::end`] is called. +pub struct TimeSpanGuard<'a, R: ?Sized, E> { + recorder: &'a R, + marker: PhantomData, +} + +impl TimeSpanGuard<'_, R, E> { + /// End the span. You have to provide the same encoder which was used to begin the span. + pub fn end(self, encoder: &mut E) { + self.recorder.end_time_span(encoder); + core::mem::forget(self); + } +} + +impl Drop for TimeSpanGuard<'_, R, E> { + fn drop(&mut self) { + panic!("TimeSpanScope::end was never called") + } +} + +/// Guard returned by [`RecordDiagnostics::pass_span`]. +/// +/// Will panic on drop unless [`PassSpanGuard::end`] is called. +pub struct PassSpanGuard<'a, R: ?Sized, P> { + recorder: &'a R, + name: Cow<'static, str>, + marker: PhantomData

, +} + +impl PassSpanGuard<'_, R, P> { + /// End the span. You have to provide the same pass which was used to begin the span. + pub fn end(self, pass: &mut P) { + self.recorder.end_pass_span(pass); + core::mem::forget(self); + } +} + +impl Drop for PassSpanGuard<'_, R, P> { + fn drop(&mut self) { + panic!("PassSpanGuard::end was never called for {}", self.name) + } +} + +impl RecordDiagnostics for Option> { + fn begin_time_span(&self, encoder: &mut E, name: Cow<'static, str>) { + if let Some(recorder) = &self { + recorder.begin_time_span(encoder, name); + } + } + + fn end_time_span(&self, encoder: &mut E) { + if let Some(recorder) = &self { + recorder.end_time_span(encoder); + } + } + + fn begin_pass_span(&self, pass: &mut P, name: Cow<'static, str>) { + if let Some(recorder) = &self { + recorder.begin_pass_span(pass, name); + } + } + + fn end_pass_span(&self, pass: &mut P) { + if let Some(recorder) = &self { + recorder.end_pass_span(pass); + } + } +} diff --git a/third_party/bevy_render/src/diagnostic/render_asset_diagnostic_plugin.rs b/third_party/bevy_render/src/diagnostic/render_asset_diagnostic_plugin.rs new file mode 100644 index 0000000..347bae7 --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/render_asset_diagnostic_plugin.rs @@ -0,0 +1,77 @@ +use core::{any::type_name, marker::PhantomData}; + +use bevy_app::{Plugin, PreUpdate}; +use bevy_diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic}; +use bevy_ecs::{resource::Resource, system::Res}; +use bevy_platform::sync::atomic::{AtomicUsize, Ordering}; + +use crate::{ + render_asset::{RenderAsset, RenderAssets}, + Extract, ExtractSchedule, RenderApp, +}; + +pub struct RenderAssetDiagnosticPlugin { + suffix: &'static str, + _phantom: PhantomData, +} + +impl RenderAssetDiagnosticPlugin { + pub fn new(suffix: &'static str) -> Self { + Self { + suffix, + _phantom: PhantomData, + } + } + + pub fn render_asset_diagnostic_path() -> DiagnosticPath { + DiagnosticPath::from_components(["render_asset", type_name::()]) + } +} + +impl Plugin for RenderAssetDiagnosticPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.register_diagnostic( + Diagnostic::new(Self::render_asset_diagnostic_path()).with_suffix(self.suffix), + ) + .init_resource::>() + .add_systems(PreUpdate, add_render_asset_measurement::); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems(ExtractSchedule, measure_render_asset::); + } + } +} + +#[derive(Debug, Resource)] +struct RenderAssetMeasurements { + assets: AtomicUsize, + _phantom: PhantomData, +} + +impl Default for RenderAssetMeasurements { + fn default() -> Self { + Self { + assets: AtomicUsize::default(), + _phantom: PhantomData, + } + } +} + +fn add_render_asset_measurement( + mut diagnostics: Diagnostics, + measurements: Res>, +) { + diagnostics.add_measurement( + &RenderAssetDiagnosticPlugin::::render_asset_diagnostic_path(), + || measurements.assets.load(Ordering::Relaxed) as f64, + ); +} + +fn measure_render_asset( + measurements: Extract>>, + assets: Res>, +) { + measurements + .assets + .store(assets.iter().count(), Ordering::Relaxed); +} diff --git a/third_party/bevy_render/src/diagnostic/tracy_gpu.rs b/third_party/bevy_render/src/diagnostic/tracy_gpu.rs new file mode 100644 index 0000000..488e415 --- /dev/null +++ b/third_party/bevy_render/src/diagnostic/tracy_gpu.rs @@ -0,0 +1,69 @@ +use crate::renderer::{RenderAdapterInfo, RenderDevice, RenderQueue}; +use tracy_client::{Client, GpuContext, GpuContextType}; +use wgpu::{ + Backend, BufferDescriptor, BufferUsages, CommandEncoderDescriptor, MapMode, PollType, + QuerySetDescriptor, QueryType, QUERY_SIZE, +}; + +pub fn new_tracy_gpu_context( + adapter_info: &RenderAdapterInfo, + device: &RenderDevice, + queue: &RenderQueue, +) -> GpuContext { + let tracy_gpu_backend = match adapter_info.backend { + Backend::Vulkan => GpuContextType::Vulkan, + Backend::Dx12 => GpuContextType::Direct3D12, + Backend::Gl => GpuContextType::OpenGL, + Backend::Metal | Backend::BrowserWebGpu | Backend::Noop => GpuContextType::Invalid, + }; + + let tracy_client = Client::running().unwrap(); + tracy_client + .new_gpu_context( + Some("RenderQueue"), + tracy_gpu_backend, + initial_timestamp(device, queue), + queue.get_timestamp_period(), + ) + .unwrap() +} + +// Code copied from https://github.com/Wumpf/wgpu-profiler/blob/f9de342a62cb75f50904a98d11dd2bbeb40ceab8/src/tracy.rs +fn initial_timestamp(device: &RenderDevice, queue: &RenderQueue) -> i64 { + let query_set = device.wgpu_device().create_query_set(&QuerySetDescriptor { + label: None, + ty: QueryType::Timestamp, + count: 1, + }); + + let resolve_buffer = device.create_buffer(&BufferDescriptor { + label: None, + size: QUERY_SIZE as _, + usage: BufferUsages::QUERY_RESOLVE | BufferUsages::COPY_SRC, + mapped_at_creation: false, + }); + + let map_buffer = device.create_buffer(&BufferDescriptor { + label: None, + size: QUERY_SIZE as _, + usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + + let mut timestamp_encoder = device.create_command_encoder(&CommandEncoderDescriptor::default()); + timestamp_encoder.write_timestamp(&query_set, 0); + timestamp_encoder.resolve_query_set(&query_set, 0..1, &resolve_buffer, 0); + // Workaround for https://github.com/gfx-rs/wgpu/issues/6406 + // TODO when that bug is fixed, merge these encoders together again + let mut copy_encoder = device.create_command_encoder(&CommandEncoderDescriptor::default()); + copy_encoder.copy_buffer_to_buffer(&resolve_buffer, 0, &map_buffer, 0, Some(QUERY_SIZE as _)); + queue.submit([timestamp_encoder.finish(), copy_encoder.finish()]); + + map_buffer.slice(..).map_async(MapMode::Read, |_| ()); + device + .poll(PollType::wait_indefinitely()) + .expect("Failed to poll device for map async"); + + let view = map_buffer.slice(..).get_mapped_range(); + i64::from_le_bytes((*view).try_into().unwrap()) +} diff --git a/third_party/bevy_render/src/erased_render_asset.rs b/third_party/bevy_render/src/erased_render_asset.rs new file mode 100644 index 0000000..9c56ba4 --- /dev/null +++ b/third_party/bevy_render/src/erased_render_asset.rs @@ -0,0 +1,427 @@ +use crate::{ + render_resource::AsBindGroupError, ExtractSchedule, MainWorld, Render, RenderApp, + RenderSystems, Res, +}; +use bevy_app::{App, Plugin, SubApp}; +use bevy_asset::RenderAssetUsages; +use bevy_asset::{Asset, AssetEvent, AssetId, Assets, UntypedAssetId}; +use bevy_ecs::{ + prelude::{Commands, IntoScheduleConfigs, MessageReader, ResMut, Resource}, + schedule::{ScheduleConfigs, SystemSet}, + system::{ScheduleSystem, StaticSystemParam, SystemParam, SystemParamItem, SystemState}, + world::{FromWorld, Mut}, +}; +use bevy_platform::collections::{HashMap, HashSet}; +use bevy_render::render_asset::RenderAssetBytesPerFrameLimiter; +use core::marker::PhantomData; +use thiserror::Error; +use tracing::{debug, error}; + +#[derive(Debug, Error)] +pub enum PrepareAssetError { + #[error("Failed to prepare asset")] + RetryNextUpdate(E), + #[error("Failed to build bind group: {0}")] + AsBindGroupError(AsBindGroupError), +} + +/// The system set during which we extract modified assets to the render world. +#[derive(SystemSet, Clone, PartialEq, Eq, Debug, Hash)] +pub struct AssetExtractionSystems; + +/// Describes how an asset gets extracted and prepared for rendering. +/// +/// In the [`ExtractSchedule`] step the [`ErasedRenderAsset::SourceAsset`] is transferred +/// from the "main world" into the "render world". +/// +/// After that in the [`RenderSystems::PrepareAssets`] step the extracted asset +/// is transformed into its GPU-representation of type [`ErasedRenderAsset`]. +pub trait ErasedRenderAsset: Send + Sync + 'static { + /// The representation of the asset in the "main world". + type SourceAsset: Asset + Clone; + /// The target representation of the asset in the "render world". + type ErasedAsset: Send + Sync + 'static + Sized; + + /// Specifies all ECS data required by [`ErasedRenderAsset::prepare_asset`]. + /// + /// For convenience use the [`lifetimeless`](bevy_ecs::system::lifetimeless) [`SystemParam`]. + type Param: SystemParam; + + /// Whether or not to unload the asset after extracting it to the render world. + #[inline] + fn asset_usage(_source_asset: &Self::SourceAsset) -> RenderAssetUsages { + RenderAssetUsages::default() + } + + /// Size of the data the asset will upload to the gpu. Specifying a return value + /// will allow the asset to be throttled via [`RenderAssetBytesPerFrameLimiter`]. + #[inline] + #[expect( + unused_variables, + reason = "The parameters here are intentionally unused by the default implementation; however, putting underscores here will result in the underscores being copied by rust-analyzer's tab completion." + )] + fn byte_len(erased_asset: &Self::SourceAsset) -> Option { + None + } + + /// Prepares the [`ErasedRenderAsset::SourceAsset`] for the GPU by transforming it into a [`ErasedRenderAsset`]. + /// + /// ECS data may be accessed via `param`. + fn prepare_asset( + source_asset: Self::SourceAsset, + asset_id: AssetId, + param: &mut SystemParamItem, + ) -> Result>; + + /// Called whenever the [`ErasedRenderAsset::SourceAsset`] has been removed. + /// + /// You can implement this method if you need to access ECS data (via + /// `_param`) in order to perform cleanup tasks when the asset is removed. + /// + /// The default implementation does nothing. + fn unload_asset( + _source_asset: AssetId, + _param: &mut SystemParamItem, + ) { + } +} + +/// This plugin extracts the changed assets from the "app world" into the "render world" +/// and prepares them for the GPU. They can then be accessed from the [`ErasedRenderAssets`] resource. +/// +/// Therefore it sets up the [`ExtractSchedule`] and +/// [`RenderSystems::PrepareAssets`] steps for the specified [`ErasedRenderAsset`]. +/// +/// The `AFTER` generic parameter can be used to specify that `A::prepare_asset` should not be run until +/// `prepare_assets::` has completed. This allows the `prepare_asset` function to depend on another +/// prepared [`ErasedRenderAsset`], for example `Mesh::prepare_asset` relies on `ErasedRenderAssets::` for morph +/// targets, so the plugin is created as `ErasedRenderAssetPlugin::::default()`. +pub struct ErasedRenderAssetPlugin< + A: ErasedRenderAsset, + AFTER: ErasedRenderAssetDependency + 'static = (), +> { + phantom: PhantomData (A, AFTER)>, +} + +impl Default + for ErasedRenderAssetPlugin +{ + fn default() -> Self { + Self { + phantom: Default::default(), + } + } +} + +impl Plugin + for ErasedRenderAssetPlugin +{ + fn build(&self, app: &mut App) { + app.init_resource::>(); + } + + fn finish(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .init_resource::>() + .init_resource::>() + .init_resource::>() + .add_systems( + ExtractSchedule, + extract_erased_render_asset::.in_set(AssetExtractionSystems), + ); + AFTER::register_system( + render_app, + prepare_erased_assets::.in_set(RenderSystems::PrepareAssets), + ); + } + } +} + +// helper to allow specifying dependencies between render assets +pub trait ErasedRenderAssetDependency { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs); +} + +impl ErasedRenderAssetDependency for () { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs) { + render_app.add_systems(Render, system); + } +} + +impl ErasedRenderAssetDependency for A { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs) { + render_app.add_systems(Render, system.after(prepare_erased_assets::)); + } +} + +/// Temporarily stores the extracted and removed assets of the current frame. +#[derive(Resource)] +pub struct ExtractedAssets { + /// The assets extracted this frame. + /// + /// These are assets that were either added or modified this frame. + pub extracted: Vec<(AssetId, A::SourceAsset)>, + + /// IDs of the assets that were removed this frame. + /// + /// These assets will not be present in [`ExtractedAssets::extracted`]. + pub removed: HashSet>, + + /// IDs of the assets that were modified this frame. + pub modified: HashSet>, + + /// IDs of the assets that were added this frame. + pub added: HashSet>, +} + +impl Default for ExtractedAssets { + fn default() -> Self { + Self { + extracted: Default::default(), + removed: Default::default(), + modified: Default::default(), + added: Default::default(), + } + } +} + +/// Stores all GPU representations ([`ErasedRenderAsset`]) +/// of [`ErasedRenderAsset::SourceAsset`] as long as they exist. +#[derive(Resource)] +pub struct ErasedRenderAssets(HashMap); + +impl Default for ErasedRenderAssets { + fn default() -> Self { + Self(Default::default()) + } +} + +impl ErasedRenderAssets { + pub fn get(&self, id: impl Into) -> Option<&ERA> { + self.0.get(&id.into()) + } + + pub fn get_mut(&mut self, id: impl Into) -> Option<&mut ERA> { + self.0.get_mut(&id.into()) + } + + pub fn insert(&mut self, id: impl Into, value: ERA) -> Option { + self.0.insert(id.into(), value) + } + + pub fn remove(&mut self, id: impl Into) -> Option { + self.0.remove(&id.into()) + } + + pub fn iter(&self) -> impl Iterator { + self.0.iter().map(|(k, v)| (*k, v)) + } + + pub fn iter_mut(&mut self) -> impl Iterator { + self.0.iter_mut().map(|(k, v)| (*k, v)) + } +} + +#[derive(Resource)] +struct CachedExtractErasedRenderAssetSystemState { + state: SystemState<( + MessageReader<'static, 'static, AssetEvent>, + ResMut<'static, Assets>, + )>, +} + +impl FromWorld for CachedExtractErasedRenderAssetSystemState { + fn from_world(world: &mut bevy_ecs::world::World) -> Self { + Self { + state: SystemState::new(world), + } + } +} + +/// This system extracts all created or modified assets of the corresponding [`ErasedRenderAsset::SourceAsset`] type +/// into the "render world". +pub(crate) fn extract_erased_render_asset( + mut commands: Commands, + mut main_world: ResMut, +) { + main_world.resource_scope( + |world, mut cached_state: Mut>| { + let (mut events, mut assets) = cached_state.state.get_mut(world); + + let mut needs_extracting = >::default(); + let mut removed = >::default(); + let mut modified = >::default(); + + for event in events.read() { + #[expect( + clippy::match_same_arms, + reason = "LoadedWithDependencies is marked as a TODO, so it's likely this will no longer lint soon." + )] + match event { + AssetEvent::Added { id } => { + needs_extracting.insert(*id); + } + AssetEvent::Modified { id } => { + needs_extracting.insert(*id); + modified.insert(*id); + } + AssetEvent::Removed { .. } => { + // We don't care that the asset was removed from Assets in the main world. + // An asset is only removed from ErasedRenderAssets when its last handle is dropped (AssetEvent::Unused). + } + AssetEvent::Unused { id } => { + needs_extracting.remove(id); + modified.remove(id); + removed.insert(*id); + } + AssetEvent::LoadedWithDependencies { .. } => { + // TODO: handle this + } + } + } + + let mut extracted_assets = Vec::new(); + let mut added = >::default(); + for id in needs_extracting.drain() { + if let Some(asset) = assets.get(id) { + let asset_usage = A::asset_usage(asset); + if asset_usage.contains(RenderAssetUsages::RENDER_WORLD) { + if asset_usage == RenderAssetUsages::RENDER_WORLD { + if let Some(asset) = assets.remove(id) { + extracted_assets.push((id, asset)); + added.insert(id); + } + } else { + extracted_assets.push((id, asset.clone())); + added.insert(id); + } + } + } + } + + commands.insert_resource(ExtractedAssets:: { + extracted: extracted_assets, + removed, + modified, + added, + }); + cached_state.state.apply(world); + }, + ); +} + +// TODO: consider storing inside system? +/// All assets that should be prepared next frame. +#[derive(Resource)] +pub struct PrepareNextFrameAssets { + assets: Vec<(AssetId, A::SourceAsset)>, +} + +impl Default for PrepareNextFrameAssets { + fn default() -> Self { + Self { + assets: Default::default(), + } + } +} + +/// This system prepares all assets of the corresponding [`ErasedRenderAsset::SourceAsset`] type +/// which where extracted this frame for the GPU. +pub fn prepare_erased_assets( + mut extracted_assets: ResMut>, + mut render_assets: ResMut>, + mut prepare_next_frame: ResMut>, + param: StaticSystemParam<::Param>, + bpf: Res, +) { + let mut wrote_asset_count = 0; + + let mut param = param.into_inner(); + let queued_assets = core::mem::take(&mut prepare_next_frame.assets); + for (id, extracted_asset) in queued_assets { + if extracted_assets.removed.contains(&id) || extracted_assets.added.contains(&id) { + // skip previous frame's assets that have been removed or updated + continue; + } + + let write_bytes = if let Some(size) = A::byte_len(&extracted_asset) { + // we could check if available bytes > byte_len here, but we want to make some + // forward progress even if the asset is larger than the max bytes per frame. + // this way we always write at least one (sized) asset per frame. + // in future we could also consider partial asset uploads. + if bpf.exhausted() { + prepare_next_frame.assets.push((id, extracted_asset)); + continue; + } + size + } else { + 0 + }; + + match A::prepare_asset(extracted_asset, id, &mut param) { + Ok(prepared_asset) => { + render_assets.insert(id, prepared_asset); + bpf.write_bytes(write_bytes); + wrote_asset_count += 1; + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((id, extracted_asset)); + } + Err(PrepareAssetError::AsBindGroupError(e)) => { + error!( + "{} Bind group construction failed: {e}", + core::any::type_name::() + ); + } + } + } + + for removed in extracted_assets.removed.drain() { + render_assets.remove(removed); + A::unload_asset(removed, &mut param); + } + + for (id, extracted_asset) in extracted_assets.extracted.drain(..) { + // we remove previous here to ensure that if we are updating the asset then + // any users will not see the old asset after a new asset is extracted, + // even if the new asset is not yet ready or we are out of bytes to write. + render_assets.remove(id); + + let write_bytes = if let Some(size) = A::byte_len(&extracted_asset) { + if bpf.exhausted() { + prepare_next_frame.assets.push((id, extracted_asset)); + continue; + } + size + } else { + 0 + }; + + match A::prepare_asset(extracted_asset, id, &mut param) { + Ok(prepared_asset) => { + render_assets.insert(id, prepared_asset); + bpf.write_bytes(write_bytes); + wrote_asset_count += 1; + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((id, extracted_asset)); + } + Err(PrepareAssetError::AsBindGroupError(e)) => { + error!( + "{} Bind group construction failed: {e}", + core::any::type_name::() + ); + } + } + } + + if bpf.exhausted() && !prepare_next_frame.assets.is_empty() { + debug!( + "{} write budget exhausted with {} assets remaining (wrote {})", + core::any::type_name::(), + prepare_next_frame.assets.len(), + wrote_asset_count + ); + } +} diff --git a/third_party/bevy_render/src/experimental/mod.rs b/third_party/bevy_render/src/experimental/mod.rs new file mode 100644 index 0000000..40bb6cf --- /dev/null +++ b/third_party/bevy_render/src/experimental/mod.rs @@ -0,0 +1,6 @@ +//! Experimental rendering features. +//! +//! Experimental features are features with known problems, but are included +//! nonetheless for testing purposes. + +pub mod occlusion_culling; diff --git a/third_party/bevy_render/src/experimental/occlusion_culling/mesh_preprocess_types.wgsl b/third_party/bevy_render/src/experimental/occlusion_culling/mesh_preprocess_types.wgsl new file mode 100644 index 0000000..a597fb0 --- /dev/null +++ b/third_party/bevy_render/src/experimental/occlusion_culling/mesh_preprocess_types.wgsl @@ -0,0 +1,69 @@ +// Types needed for GPU mesh uniform building. + +#define_import_path bevy_pbr::mesh_preprocess_types + +// Per-frame data that the CPU supplies to the GPU. +struct MeshInput { + // The model transform. + world_from_local: mat3x4, + // The lightmap UV rect, packed into 64 bits. + lightmap_uv_rect: vec2, + // Various flags. + flags: u32, + previous_input_index: u32, + first_vertex_index: u32, + first_index_index: u32, + index_count: u32, + current_skin_index: u32, + // Low 16 bits: index of the material inside the bind group data. + // High 16 bits: index of the lightmap in the binding array. + material_and_lightmap_bind_group_slot: u32, + timestamp: u32, + // User supplied index to identify the mesh instance + tag: u32, + pad: u32, +} + +// The `wgpu` indirect parameters structure. This is a union of two structures. +// For more information, see the corresponding comment in +// `gpu_preprocessing.rs`. +struct IndirectParametersIndexed { + // `vertex_count` or `index_count`. + index_count: u32, + // `instance_count` in both structures. + instance_count: u32, + // `first_vertex` or `first_index`. + first_index: u32, + // `base_vertex` or `first_instance`. + base_vertex: u32, + // A read-only copy of `instance_index`. + first_instance: u32, +} + +struct IndirectParametersNonIndexed { + vertex_count: u32, + instance_count: u32, + base_vertex: u32, + first_instance: u32, +} + +struct IndirectParametersCpuMetadata { + base_output_index: u32, + batch_set_index: u32, +} + +struct IndirectParametersGpuMetadata { + mesh_index: u32, +#ifdef WRITE_INDIRECT_PARAMETERS_METADATA + early_instance_count: atomic, + late_instance_count: atomic, +#else // WRITE_INDIRECT_PARAMETERS_METADATA + early_instance_count: u32, + late_instance_count: u32, +#endif // WRITE_INDIRECT_PARAMETERS_METADATA +} + +struct IndirectBatchSet { + indirect_parameters_count: atomic, + indirect_parameters_base: u32, +} diff --git a/third_party/bevy_render/src/experimental/occlusion_culling/mod.rs b/third_party/bevy_render/src/experimental/occlusion_culling/mod.rs new file mode 100644 index 0000000..ff76b33 --- /dev/null +++ b/third_party/bevy_render/src/experimental/occlusion_culling/mod.rs @@ -0,0 +1,104 @@ +//! GPU occlusion culling. +//! +//! See [`OcclusionCulling`] for a detailed description of occlusion culling in +//! Bevy. + +use bevy_app::{App, Plugin}; +use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent}; +use bevy_reflect::{prelude::ReflectDefault, Reflect}; +use bevy_shader::load_shader_library; + +use crate::{extract_component::ExtractComponent, render_resource::TextureView}; + +/// Enables GPU occlusion culling. +/// +/// See [`OcclusionCulling`] for a detailed description of occlusion culling in +/// Bevy. +pub struct OcclusionCullingPlugin; + +impl Plugin for OcclusionCullingPlugin { + fn build(&self, app: &mut App) { + load_shader_library!(app, "mesh_preprocess_types.wgsl"); + } +} + +/// Add this component to a view in order to enable experimental GPU occlusion +/// culling. +/// +/// *Bevy's occlusion culling is currently marked as experimental.* There are +/// known issues whereby, in rare circumstances, occlusion culling can result in +/// meshes being culled that shouldn't be (i.e. meshes that turn invisible). +/// Please try it out and report issues. +/// +/// *Occlusion culling* allows Bevy to avoid rendering objects that are fully +/// behind other opaque or alpha tested objects. This is different from, and +/// complements, depth fragment rejection as the `DepthPrepass` enables. While +/// depth rejection allows Bevy to avoid rendering *pixels* that are behind +/// other objects, the GPU still has to examine those pixels to reject them, +/// which requires transforming the vertices of the objects and performing +/// skinning if the objects were skinned. Occlusion culling allows the GPU to go +/// a step further, avoiding even transforming the vertices of objects that it +/// can quickly prove to be behind other objects. +/// +/// Occlusion culling inherently has some overhead, because Bevy must examine +/// the objects' bounding boxes, and create an acceleration structure +/// (hierarchical Z-buffer) to perform the occlusion tests. Therefore, occlusion +/// culling is disabled by default. Only enable it if you measure it to be a +/// speedup on your scene. Note that, because Bevy's occlusion culling runs on +/// the GPU and is quite efficient, it's rare for occlusion culling to result in +/// a significant slowdown. +/// +/// Occlusion culling currently requires a `DepthPrepass`. If no depth prepass +/// is present on the view, the [`OcclusionCulling`] component will be ignored. +/// Additionally, occlusion culling is currently incompatible with deferred +/// shading; including both `DeferredPrepass` and [`OcclusionCulling`] results +/// in unspecified behavior. +/// +/// The algorithm that Bevy uses is known as [*two-phase occlusion culling*]. +/// When you enable occlusion culling, Bevy splits the depth prepass into two: +/// an *early* depth prepass and a *late* depth prepass. The early depth prepass +/// renders all the meshes that were visible last frame to produce a +/// conservative approximation of the depth buffer. Then, after producing an +/// acceleration structure known as a hierarchical Z-buffer or depth pyramid, +/// Bevy tests the bounding boxes of all meshes against that depth buffer. Those +/// that can be quickly proven to be behind the geometry rendered during the +/// early depth prepass are skipped entirely. The other potentially-visible +/// meshes are rendered during the late prepass, and finally all the visible +/// meshes are rendered as usual during the opaque, transparent, etc. passes. +/// +/// Unlike other occlusion culling systems you may be familiar with, Bevy's +/// occlusion culling is fully dynamic and requires no baking step. The CPU +/// overhead is minimal. Large skinned meshes and other dynamic objects can +/// occlude other objects. +/// +/// [*two-phase occlusion culling*]: +/// https://medium.com/@mil_kru/two-pass-occlusion-culling-4100edcad501 +#[derive(Component, ExtractComponent, Clone, Copy, Default, Reflect)] +#[reflect(Component, Default, Clone)] +pub struct OcclusionCulling; + +/// A render-world component that contains resources necessary to perform +/// occlusion culling on any view other than a camera. +/// +/// Bevy automatically places this component on views created for shadow +/// mapping. You don't ordinarily need to add this component yourself. +#[derive(Clone, Component)] +pub struct OcclusionCullingSubview { + /// A texture view of the Z-buffer. + pub depth_texture_view: TextureView, + /// The size of the texture along both dimensions. + /// + /// Because [`OcclusionCullingSubview`] is only currently used for shadow + /// maps, they're guaranteed to have sizes equal to a power of two, so we + /// don't have to store the two dimensions individually here. + pub depth_texture_size: u32, +} + +/// A render-world component placed on each camera that stores references to all +/// entities other than cameras that need occlusion culling. +/// +/// Bevy automatically places this component on cameras that are drawing +/// shadows, when those shadows come from lights with occlusion culling enabled. +/// You don't ordinarily need to add this component yourself. +#[derive(Clone, Component)] +pub struct OcclusionCullingSubviewEntities(pub Vec); diff --git a/third_party/bevy_render/src/extract_component.rs b/third_party/bevy_render/src/extract_component.rs new file mode 100644 index 0000000..ce656c3 --- /dev/null +++ b/third_party/bevy_render/src/extract_component.rs @@ -0,0 +1,236 @@ +use crate::{ + render_resource::{encase::internal::WriteInto, DynamicUniformBuffer, ShaderType}, + renderer::{RenderDevice, RenderQueue}, + sync_component::SyncComponentPlugin, + sync_world::RenderEntity, + Extract, ExtractSchedule, Render, RenderApp, RenderSystems, +}; +use bevy_app::{App, Plugin}; +use bevy_camera::visibility::ViewVisibility; +use bevy_ecs::{ + bundle::NoBundleEffect, + component::Component, + prelude::*, + query::{QueryFilter, QueryItem, ReadOnlyQueryData}, +}; +use core::{marker::PhantomData, ops::Deref}; + +pub use bevy_render_macros::ExtractComponent; + +/// Stores the index of a uniform inside of [`ComponentUniforms`]. +#[derive(Component)] +pub struct DynamicUniformIndex { + index: u32, + marker: PhantomData, +} + +impl DynamicUniformIndex { + #[inline] + pub fn index(&self) -> u32 { + self.index + } +} + +/// Describes how a component gets extracted for rendering. +/// +/// Therefore the component is transferred from the "app world" into the "render world" +/// in the [`ExtractSchedule`] step. +pub trait ExtractComponent: Component { + /// ECS [`ReadOnlyQueryData`] to fetch the components to extract. + type QueryData: ReadOnlyQueryData; + /// Filters the entities with additional constraints. + type QueryFilter: QueryFilter; + + /// The output from extraction. + /// + /// Returning `None` based on the queried item will remove the component from the entity in + /// the render world. This can be used, for example, to conditionally extract camera settings + /// in order to disable a rendering feature on the basis of those settings, without removing + /// the component from the entity in the main world. + /// + /// The output may be different from the queried component. + /// This can be useful for example if only a subset of the fields are useful + /// in the render world. + /// + /// `Out` has a [`Bundle`] trait bound instead of a [`Component`] trait bound in order to allow use cases + /// such as tuples of components as output. + type Out: Bundle; + + // TODO: https://github.com/rust-lang/rust/issues/29661 + // type Out: Component = Self; + + /// Defines how the component is transferred into the "render world". + fn extract_component(item: QueryItem<'_, '_, Self::QueryData>) -> Option; +} + +/// This plugin prepares the components of the corresponding type for the GPU +/// by transforming them into uniforms. +/// +/// They can then be accessed from the [`ComponentUniforms`] resource. +/// For referencing the newly created uniforms a [`DynamicUniformIndex`] is inserted +/// for every processed entity. +/// +/// Therefore it sets up the [`RenderSystems::Prepare`] step +/// for the specified [`ExtractComponent`]. +pub struct UniformComponentPlugin(PhantomData C>); + +impl Default for UniformComponentPlugin { + fn default() -> Self { + Self(PhantomData) + } +} + +impl Plugin for UniformComponentPlugin { + fn build(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .insert_resource(ComponentUniforms::::default()) + .add_systems( + Render, + prepare_uniform_components::.in_set(RenderSystems::PrepareResources), + ); + } + } +} + +/// Stores all uniforms of the component type. +#[derive(Resource)] +pub struct ComponentUniforms { + uniforms: DynamicUniformBuffer, +} + +impl Deref for ComponentUniforms { + type Target = DynamicUniformBuffer; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.uniforms + } +} + +impl ComponentUniforms { + #[inline] + pub fn uniforms(&self) -> &DynamicUniformBuffer { + &self.uniforms + } +} + +impl Default for ComponentUniforms { + fn default() -> Self { + Self { + uniforms: Default::default(), + } + } +} + +/// This system prepares all components of the corresponding component type. +/// They are transformed into uniforms and stored in the [`ComponentUniforms`] resource. +fn prepare_uniform_components( + mut commands: Commands, + render_device: Res, + render_queue: Res, + mut component_uniforms: ResMut>, + components: Query<(Entity, &C)>, +) where + C: Component + ShaderType + WriteInto + Clone, +{ + let components_iter = components.iter(); + let count = components_iter.len(); + let Some(mut writer) = + component_uniforms + .uniforms + .get_writer(count, &render_device, &render_queue) + else { + return; + }; + let entities = components_iter + .map(|(entity, component)| { + ( + entity, + DynamicUniformIndex:: { + index: writer.write(component), + marker: PhantomData, + }, + ) + }) + .collect::>(); + commands.try_insert_batch(entities); +} + +/// This plugin extracts the components into the render world for synced entities. +/// +/// To do so, it sets up the [`ExtractSchedule`] step for the specified [`ExtractComponent`]. +pub struct ExtractComponentPlugin { + only_extract_visible: bool, + marker: PhantomData (C, F)>, +} + +impl Default for ExtractComponentPlugin { + fn default() -> Self { + Self { + only_extract_visible: false, + marker: PhantomData, + } + } +} + +impl ExtractComponentPlugin { + pub fn extract_visible() -> Self { + Self { + only_extract_visible: true, + marker: PhantomData, + } + } +} + +impl Plugin for ExtractComponentPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(SyncComponentPlugin::::default()); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + if self.only_extract_visible { + render_app.add_systems(ExtractSchedule, extract_visible_components::); + } else { + render_app.add_systems(ExtractSchedule, extract_components::); + } + } + } +} + +/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are synced via [`crate::sync_world::SyncToRenderWorld`]. +fn extract_components( + mut commands: Commands, + mut previous_len: Local, + query: Extract>, +) { + let mut values = Vec::with_capacity(*previous_len); + for (entity, query_item) in &query { + if let Some(component) = C::extract_component(query_item) { + values.push((entity, component)); + } else { + commands.entity(entity).remove::(); + } + } + *previous_len = values.len(); + commands.try_insert_batch(values); +} + +/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are visible and synced via [`crate::sync_world::SyncToRenderWorld`]. +fn extract_visible_components( + mut commands: Commands, + mut previous_len: Local, + query: Extract>, +) { + let mut values = Vec::with_capacity(*previous_len); + for (entity, view_visibility, query_item) in &query { + if view_visibility.get() { + if let Some(component) = C::extract_component(query_item) { + values.push((entity, component)); + } else { + commands.entity(entity).remove::(); + } + } + } + *previous_len = values.len(); + commands.try_insert_batch(values); +} diff --git a/third_party/bevy_render/src/extract_instances.rs b/third_party/bevy_render/src/extract_instances.rs new file mode 100644 index 0000000..d85f8fa --- /dev/null +++ b/third_party/bevy_render/src/extract_instances.rs @@ -0,0 +1,137 @@ +//! Convenience logic for turning components from the main world into extracted +//! instances in the render world. +//! +//! This is essentially the same as the `extract_component` module, but +//! higher-performance because it avoids the ECS overhead. + +use core::marker::PhantomData; + +use bevy_app::{App, Plugin}; +use bevy_camera::visibility::ViewVisibility; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + prelude::Entity, + query::{QueryFilter, QueryItem, ReadOnlyQueryData}, + resource::Resource, + system::{Query, ResMut}, +}; + +use crate::sync_world::MainEntityHashMap; +use crate::{Extract, ExtractSchedule, RenderApp}; + +/// Describes how to extract data needed for rendering from a component or +/// components. +/// +/// Before rendering, any applicable components will be transferred from the +/// main world to the render world in the [`ExtractSchedule`] step. +/// +/// This is essentially the same as +/// [`ExtractComponent`](crate::extract_component::ExtractComponent), but +/// higher-performance because it avoids the ECS overhead. +pub trait ExtractInstance: Send + Sync + Sized + 'static { + /// ECS [`ReadOnlyQueryData`] to fetch the components to extract. + type QueryData: ReadOnlyQueryData; + /// Filters the entities with additional constraints. + type QueryFilter: QueryFilter; + + /// Defines how the component is transferred into the "render world". + fn extract(item: QueryItem<'_, '_, Self::QueryData>) -> Option; +} + +/// This plugin extracts one or more components into the "render world" as +/// extracted instances. +/// +/// Therefore it sets up the [`ExtractSchedule`] step for the specified +/// [`ExtractedInstances`]. +#[derive(Default)] +pub struct ExtractInstancesPlugin +where + EI: ExtractInstance, +{ + only_extract_visible: bool, + marker: PhantomData EI>, +} + +/// Stores all extract instances of a type in the render world. +#[derive(Resource, Deref, DerefMut)] +pub struct ExtractedInstances(MainEntityHashMap) +where + EI: ExtractInstance; + +impl Default for ExtractedInstances +where + EI: ExtractInstance, +{ + fn default() -> Self { + Self(Default::default()) + } +} + +impl ExtractInstancesPlugin +where + EI: ExtractInstance, +{ + /// Creates a new [`ExtractInstancesPlugin`] that unconditionally extracts to + /// the render world, whether the entity is visible or not. + pub fn new() -> Self { + Self { + only_extract_visible: false, + marker: PhantomData, + } + } + + /// Creates a new [`ExtractInstancesPlugin`] that extracts to the render world + /// if and only if the entity it's attached to is visible. + pub fn extract_visible() -> Self { + Self { + only_extract_visible: true, + marker: PhantomData, + } + } +} + +impl Plugin for ExtractInstancesPlugin +where + EI: ExtractInstance, +{ + fn build(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.init_resource::>(); + if self.only_extract_visible { + render_app.add_systems(ExtractSchedule, extract_visible::); + } else { + render_app.add_systems(ExtractSchedule, extract_all::); + } + } + } +} + +fn extract_all( + mut extracted_instances: ResMut>, + query: Extract>, +) where + EI: ExtractInstance, +{ + extracted_instances.clear(); + for (entity, other) in &query { + if let Some(extract_instance) = EI::extract(other) { + extracted_instances.insert(entity.into(), extract_instance); + } + } +} + +fn extract_visible( + mut extracted_instances: ResMut>, + query: Extract>, +) where + EI: ExtractInstance, +{ + extracted_instances.clear(); + for (entity, view_visibility, other) in &query { + if view_visibility.get() + && let Some(extract_instance) = EI::extract(other) + { + extracted_instances.insert(entity.into(), extract_instance); + } + } +} diff --git a/third_party/bevy_render/src/extract_param.rs b/third_party/bevy_render/src/extract_param.rs new file mode 100644 index 0000000..4d81fb9 --- /dev/null +++ b/third_party/bevy_render/src/extract_param.rs @@ -0,0 +1,177 @@ +use crate::MainWorld; +use bevy_ecs::{ + change_detection::Tick, + prelude::*, + query::FilteredAccessSet, + system::{ + ReadOnlySystemParam, SystemMeta, SystemParam, SystemParamItem, SystemParamValidationError, + SystemState, + }, + world::unsafe_world_cell::UnsafeWorldCell, +}; +use core::ops::{Deref, DerefMut}; + +/// A helper for accessing [`MainWorld`] content using a system parameter. +/// +/// A [`SystemParam`] adapter which applies the contained `SystemParam` to the [`World`] +/// contained in [`MainWorld`]. This parameter only works for systems run +/// during the [`ExtractSchedule`](crate::ExtractSchedule). +/// +/// This requires that the contained [`SystemParam`] does not mutate the world, as it +/// uses a read-only reference to [`MainWorld`] internally. +/// +/// ## Context +/// +/// [`ExtractSchedule`] is used to extract (move) data from the simulation world ([`MainWorld`]) to the +/// render world. The render world drives rendering each frame (generally to a `Window`). +/// This design is used to allow performing calculations related to rendering a prior frame at the same +/// time as the next frame is simulated, which increases throughput (FPS). +/// +/// [`Extract`] is used to get data from the main world during [`ExtractSchedule`]. +/// +/// ## Examples +/// +/// ``` +/// use bevy_ecs::prelude::*; +/// use bevy_render::Extract; +/// use bevy_render::sync_world::RenderEntity; +/// # #[derive(Component)] +/// // Do make sure to sync the cloud entities before extracting them. +/// # struct Cloud; +/// fn extract_clouds(mut commands: Commands, clouds: Extract>>) { +/// for cloud in &clouds { +/// commands.entity(cloud).insert(Cloud); +/// } +/// } +/// ``` +/// +/// [`ExtractSchedule`]: crate::ExtractSchedule +/// [Window]: bevy_window::Window +pub struct Extract<'w, 's, P> +where + P: ReadOnlySystemParam + 'static, +{ + item: SystemParamItem<'w, 's, P>, +} + +#[doc(hidden)] +pub struct ExtractState { + state: SystemState

, + main_world_state: as SystemParam>::State, +} + +// SAFETY: The only `World` access (`Res`) is read-only. +unsafe impl

ReadOnlySystemParam for Extract<'_, '_, P> where P: ReadOnlySystemParam {} + +// SAFETY: The only `World` access is properly registered by `Res::init_state`. +// This call will also ensure that there are no conflicts with prior params. +unsafe impl

SystemParam for Extract<'_, '_, P> +where + P: ReadOnlySystemParam, +{ + type State = ExtractState

; + type Item<'w, 's> = Extract<'w, 's, P>; + + fn init_state(world: &mut World) -> Self::State { + let mut main_world = world.resource_mut::(); + ExtractState { + state: SystemState::new(&mut main_world), + main_world_state: Res::::init_state(world), + } + } + + fn init_access( + state: &Self::State, + system_meta: &mut SystemMeta, + component_access_set: &mut FilteredAccessSet, + world: &mut World, + ) { + Res::::init_access( + &state.main_world_state, + system_meta, + component_access_set, + world, + ); + } + + #[inline] + unsafe fn validate_param( + state: &mut Self::State, + _system_meta: &SystemMeta, + world: UnsafeWorldCell, + ) -> Result<(), SystemParamValidationError> { + // SAFETY: Read-only access to world data registered in `init_state`. + let result = unsafe { world.get_resource_by_id(state.main_world_state) }; + let Some(main_world) = result else { + return Err(SystemParamValidationError::invalid::( + "`MainWorld` resource does not exist", + )); + }; + // SAFETY: Type is guaranteed by `SystemState`. + let main_world: &World = unsafe { main_world.deref() }; + // SAFETY: We provide the main world on which this system state was initialized on. + unsafe { + SystemState::

::validate_param( + &mut state.state, + main_world.as_unsafe_world_cell_readonly(), + ) + } + } + + #[inline] + unsafe fn get_param<'w, 's>( + state: &'s mut Self::State, + system_meta: &SystemMeta, + world: UnsafeWorldCell<'w>, + change_tick: Tick, + ) -> Self::Item<'w, 's> { + // SAFETY: + // - The caller ensures that `world` is the same one that `init_state` was called with. + // - The caller ensures that no other `SystemParam`s will conflict with the accesses we have registered. + let main_world = unsafe { + Res::::get_param( + &mut state.main_world_state, + system_meta, + world, + change_tick, + ) + }; + let item = state.state.get(main_world.into_inner()); + Extract { item } + } +} + +impl<'w, 's, P> Deref for Extract<'w, 's, P> +where + P: ReadOnlySystemParam, +{ + type Target = SystemParamItem<'w, 's, P>; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.item + } +} + +impl<'w, 's, P> DerefMut for Extract<'w, 's, P> +where + P: ReadOnlySystemParam, +{ + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.item + } +} + +impl<'a, 'w, 's, P> IntoIterator for &'a Extract<'w, 's, P> +where + P: ReadOnlySystemParam, + &'a SystemParamItem<'w, 's, P>: IntoIterator, +{ + type Item = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::Item; + type IntoIter = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + (&self.item).into_iter() + } +} diff --git a/third_party/bevy_render/src/extract_resource.rs b/third_party/bevy_render/src/extract_resource.rs new file mode 100644 index 0000000..cec8647 --- /dev/null +++ b/third_party/bevy_render/src/extract_resource.rs @@ -0,0 +1,70 @@ +use core::marker::PhantomData; + +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::*; +pub use bevy_render_macros::ExtractResource; +use bevy_utils::once; + +use crate::{Extract, ExtractSchedule, RenderApp}; + +/// Describes how a resource gets extracted for rendering. +/// +/// Therefore the resource is transferred from the "main world" into the "render world" +/// in the [`ExtractSchedule`] step. +pub trait ExtractResource: Resource { + type Source: Resource; + + /// Defines how the resource is transferred into the "render world". + fn extract_resource(source: &Self::Source) -> Self; +} + +/// This plugin extracts the resources into the "render world". +/// +/// Therefore it sets up the[`ExtractSchedule`] step +/// for the specified [`Resource`]. +pub struct ExtractResourcePlugin(PhantomData); + +impl Default for ExtractResourcePlugin { + fn default() -> Self { + Self(PhantomData) + } +} + +impl Plugin for ExtractResourcePlugin { + fn build(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems(ExtractSchedule, extract_resource::); + } else { + once!(tracing::error!( + "Render app did not exist when trying to add `extract_resource` for <{}>.", + core::any::type_name::() + )); + } + } +} + +/// This system extracts the resource of the corresponding [`Resource`] type +pub fn extract_resource( + mut commands: Commands, + main_resource: Extract>>, + target_resource: Option>, +) { + if let Some(main_resource) = main_resource.as_ref() { + if let Some(mut target_resource) = target_resource { + if main_resource.is_changed() { + *target_resource = R::extract_resource(main_resource); + } + } else { + #[cfg(debug_assertions)] + if !main_resource.is_added() { + once!(tracing::warn!( + "Removing resource {} from render world not expected, adding using `Commands`. + This may decrease performance", + core::any::type_name::() + )); + } + + commands.insert_resource(R::extract_resource(main_resource)); + } + } +} diff --git a/third_party/bevy_render/src/globals.rs b/third_party/bevy_render/src/globals.rs new file mode 100644 index 0000000..9b643d5 --- /dev/null +++ b/third_party/bevy_render/src/globals.rs @@ -0,0 +1,79 @@ +use crate::{ + extract_resource::ExtractResource, + render_resource::{ShaderType, UniformBuffer}, + renderer::{RenderDevice, RenderQueue}, + Extract, ExtractSchedule, Render, RenderApp, RenderSystems, +}; +use bevy_app::{App, Plugin}; +use bevy_diagnostic::FrameCount; +use bevy_ecs::prelude::*; +use bevy_reflect::prelude::*; +use bevy_shader::load_shader_library; +use bevy_time::Time; + +pub struct GlobalsPlugin; + +impl Plugin for GlobalsPlugin { + fn build(&self, app: &mut App) { + load_shader_library!(app, "globals.wgsl"); + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .init_resource::() + .init_resource::.in_set(AssetExtractionSystems), + ); + AFTER::register_system( + render_app, + prepare_assets::.in_set(RenderSystems::PrepareAssets), + ); + } + } +} + +// helper to allow specifying dependencies between render assets +pub trait RenderAssetDependency { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs); +} + +impl RenderAssetDependency for () { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs) { + render_app.add_systems(Render, system); + } +} + +impl RenderAssetDependency for A { + fn register_system(render_app: &mut SubApp, system: ScheduleConfigs) { + render_app.add_systems(Render, system.after(prepare_assets::)); + } +} + +/// Temporarily stores the extracted and removed assets of the current frame. +#[derive(Resource)] +pub struct ExtractedAssets { + /// The assets extracted this frame. + /// + /// These are assets that were either added or modified this frame. + pub extracted: Vec<(AssetId, A::SourceAsset)>, + + /// IDs of the assets that were removed this frame. + /// + /// These assets will not be present in [`ExtractedAssets::extracted`]. + pub removed: HashSet>, + + /// IDs of the assets that were modified this frame. + pub modified: HashSet>, + + /// IDs of the assets that were added this frame. + pub added: HashSet>, +} + +impl Default for ExtractedAssets { + fn default() -> Self { + Self { + extracted: Default::default(), + removed: Default::default(), + modified: Default::default(), + added: Default::default(), + } + } +} + +/// Stores all GPU representations ([`RenderAsset`]) +/// of [`RenderAsset::SourceAsset`] as long as they exist. +#[derive(Resource)] +pub struct RenderAssets(HashMap, A>); + +impl Default for RenderAssets { + fn default() -> Self { + Self(Default::default()) + } +} + +impl RenderAssets { + pub fn get(&self, id: impl Into>) -> Option<&A> { + self.0.get(&id.into()) + } + + pub fn get_mut(&mut self, id: impl Into>) -> Option<&mut A> { + self.0.get_mut(&id.into()) + } + + pub fn insert(&mut self, id: impl Into>, value: A) -> Option { + self.0.insert(id.into(), value) + } + + pub fn remove(&mut self, id: impl Into>) -> Option { + self.0.remove(&id.into()) + } + + pub fn iter(&self) -> impl Iterator, &A)> { + self.0.iter().map(|(k, v)| (*k, v)) + } + + pub fn iter_mut(&mut self) -> impl Iterator, &mut A)> { + self.0.iter_mut().map(|(k, v)| (*k, v)) + } +} + +#[derive(Resource)] +struct CachedExtractRenderAssetSystemState { + state: SystemState<( + MessageReader<'static, 'static, AssetEvent>, + ResMut<'static, Assets>, + Option>>, + )>, +} + +impl FromWorld for CachedExtractRenderAssetSystemState { + fn from_world(world: &mut bevy_ecs::world::World) -> Self { + Self { + state: SystemState::new(world), + } + } +} + +/// This system extracts all created or modified assets of the corresponding [`RenderAsset::SourceAsset`] type +/// into the "render world". +pub(crate) fn extract_render_asset( + mut commands: Commands, + mut main_world: ResMut, +) { + main_world.resource_scope( + |world, mut cached_state: Mut>| { + let (mut events, mut assets, maybe_render_assets) = cached_state.state.get_mut(world); + + let mut needs_extracting = >::default(); + let mut removed = >::default(); + let mut modified = >::default(); + + for event in events.read() { + #[expect( + clippy::match_same_arms, + reason = "LoadedWithDependencies is marked as a TODO, so it's likely this will no longer lint soon." + )] + match event { + AssetEvent::Added { id } => { + needs_extracting.insert(*id); + } + AssetEvent::Modified { id } => { + needs_extracting.insert(*id); + modified.insert(*id); + } + AssetEvent::Removed { .. } => { + // We don't care that the asset was removed from Assets in the main world. + // An asset is only removed from RenderAssets when its last handle is dropped (AssetEvent::Unused). + } + AssetEvent::Unused { id } => { + needs_extracting.remove(id); + modified.remove(id); + removed.insert(*id); + } + AssetEvent::LoadedWithDependencies { .. } => { + // TODO: handle this + } + } + } + + let mut extracted_assets = Vec::new(); + let mut added = >::default(); + for id in needs_extracting.drain() { + if let Some(asset) = assets.get(id) { + let asset_usage = A::asset_usage(asset); + if asset_usage.contains(RenderAssetUsages::RENDER_WORLD) { + if asset_usage == RenderAssetUsages::RENDER_WORLD { + if let Some(asset) = assets.get_mut_untracked(id) { + let previous_asset = maybe_render_assets.as_ref().and_then(|render_assets| render_assets.get(id)); + match A::take_gpu_data(asset, previous_asset) { + Ok(gpu_data_asset) => { + extracted_assets.push((id, gpu_data_asset)); + added.insert(id); + } + Err(e) => { + error!("{} with RenderAssetUsages == RENDER_WORLD cannot be extracted: {e}", core::any::type_name::()); + } + }; + } + } else { + extracted_assets.push((id, asset.clone())); + added.insert(id); + } + } + } + } + + commands.insert_resource(ExtractedAssets:: { + extracted: extracted_assets, + removed, + modified, + added, + }); + cached_state.state.apply(world); + }, + ); +} + +// TODO: consider storing inside system? +/// All assets that should be prepared next frame. +#[derive(Resource)] +pub struct PrepareNextFrameAssets { + assets: Vec<(AssetId, A::SourceAsset)>, +} + +impl Default for PrepareNextFrameAssets { + fn default() -> Self { + Self { + assets: Default::default(), + } + } +} + +/// This system prepares all assets of the corresponding [`RenderAsset::SourceAsset`] type +/// which where extracted this frame for the GPU. +pub fn prepare_assets( + mut extracted_assets: ResMut>, + mut render_assets: ResMut>, + mut prepare_next_frame: ResMut>, + param: StaticSystemParam<::Param>, + bpf: Res, +) { + let mut wrote_asset_count = 0; + + let mut param = param.into_inner(); + let queued_assets = core::mem::take(&mut prepare_next_frame.assets); + for (id, extracted_asset) in queued_assets { + if extracted_assets.removed.contains(&id) || extracted_assets.added.contains(&id) { + // skip previous frame's assets that have been removed or updated + continue; + } + + let write_bytes = if let Some(size) = A::byte_len(&extracted_asset) { + // we could check if available bytes > byte_len here, but we want to make some + // forward progress even if the asset is larger than the max bytes per frame. + // this way we always write at least one (sized) asset per frame. + // in future we could also consider partial asset uploads. + if bpf.exhausted() { + prepare_next_frame.assets.push((id, extracted_asset)); + continue; + } + size + } else { + 0 + }; + + let previous_asset = render_assets.get(id); + match A::prepare_asset(extracted_asset, id, &mut param, previous_asset) { + Ok(prepared_asset) => { + render_assets.insert(id, prepared_asset); + bpf.write_bytes(write_bytes); + wrote_asset_count += 1; + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((id, extracted_asset)); + } + Err(PrepareAssetError::AsBindGroupError(e)) => { + error!( + "{} Bind group construction failed: {e}", + core::any::type_name::() + ); + } + } + } + + for removed in extracted_assets.removed.drain() { + render_assets.remove(removed); + A::unload_asset(removed, &mut param); + } + + for (id, extracted_asset) in extracted_assets.extracted.drain(..) { + // we remove previous here to ensure that if we are updating the asset then + // any users will not see the old asset after a new asset is extracted, + // even if the new asset is not yet ready or we are out of bytes to write. + let previous_asset = render_assets.remove(id); + + let write_bytes = if let Some(size) = A::byte_len(&extracted_asset) { + if bpf.exhausted() { + prepare_next_frame.assets.push((id, extracted_asset)); + continue; + } + size + } else { + 0 + }; + + match A::prepare_asset(extracted_asset, id, &mut param, previous_asset.as_ref()) { + Ok(prepared_asset) => { + render_assets.insert(id, prepared_asset); + bpf.write_bytes(write_bytes); + wrote_asset_count += 1; + } + Err(PrepareAssetError::RetryNextUpdate(extracted_asset)) => { + prepare_next_frame.assets.push((id, extracted_asset)); + } + Err(PrepareAssetError::AsBindGroupError(e)) => { + error!( + "{} Bind group construction failed: {e}", + core::any::type_name::() + ); + } + } + } + + if bpf.exhausted() && !prepare_next_frame.assets.is_empty() { + debug!( + "{} write budget exhausted with {} assets remaining (wrote {})", + core::any::type_name::(), + prepare_next_frame.assets.len(), + wrote_asset_count + ); + } +} + +pub fn reset_render_asset_bytes_per_frame( + mut bpf_limiter: ResMut, +) { + bpf_limiter.reset(); +} + +pub fn extract_render_asset_bytes_per_frame( + bpf: Extract>, + mut bpf_limiter: ResMut, +) { + bpf_limiter.max_bytes = bpf.max_bytes; +} + +/// A resource that defines the amount of data allowed to be transferred from CPU to GPU +/// each frame, preventing choppy frames at the cost of waiting longer for GPU assets +/// to become available. +#[derive(Resource, Default)] +pub struct RenderAssetBytesPerFrame { + pub max_bytes: Option, +} + +impl RenderAssetBytesPerFrame { + /// `max_bytes`: the number of bytes to write per frame. + /// + /// This is a soft limit: only full assets are written currently, uploading stops + /// after the first asset that exceeds the limit. + /// + /// To participate, assets should implement [`RenderAsset::byte_len`]. If the default + /// is not overridden, the assets are assumed to be small enough to upload without restriction. + pub fn new(max_bytes: usize) -> Self { + Self { + max_bytes: Some(max_bytes), + } + } +} + +/// A render-world resource that facilitates limiting the data transferred from CPU to GPU +/// each frame, preventing choppy frames at the cost of waiting longer for GPU assets +/// to become available. +#[derive(Resource, Default)] +pub struct RenderAssetBytesPerFrameLimiter { + /// Populated by [`RenderAssetBytesPerFrame`] during extraction. + pub max_bytes: Option, + /// Bytes written this frame. + pub bytes_written: AtomicUsize, +} + +impl RenderAssetBytesPerFrameLimiter { + /// Reset the available bytes. Called once per frame during extraction by [`crate::RenderPlugin`]. + pub fn reset(&mut self) { + if self.max_bytes.is_none() { + return; + } + self.bytes_written.store(0, Ordering::Relaxed); + } + + /// Check how many bytes are available for writing. + pub fn available_bytes(&self, required_bytes: usize) -> usize { + if let Some(max_bytes) = self.max_bytes { + let total_bytes = self + .bytes_written + .fetch_add(required_bytes, Ordering::Relaxed); + + // The bytes available is the inverse of the amount we overshot max_bytes + if total_bytes >= max_bytes { + required_bytes.saturating_sub(total_bytes - max_bytes) + } else { + required_bytes + } + } else { + required_bytes + } + } + + /// Decreases the available bytes for the current frame. + pub(crate) fn write_bytes(&self, bytes: usize) { + if self.max_bytes.is_some() && bytes > 0 { + self.bytes_written.fetch_add(bytes, Ordering::Relaxed); + } + } + + /// Returns `true` if there are no remaining bytes available for writing this frame. + pub(crate) fn exhausted(&self) -> bool { + if let Some(max_bytes) = self.max_bytes { + let bytes_written = self.bytes_written.load(Ordering::Relaxed); + bytes_written >= max_bytes + } else { + false + } + } +} diff --git a/third_party/bevy_render/src/render_graph/app.rs b/third_party/bevy_render/src/render_graph/app.rs new file mode 100644 index 0000000..879f28f --- /dev/null +++ b/third_party/bevy_render/src/render_graph/app.rs @@ -0,0 +1,174 @@ +use bevy_app::{App, SubApp}; +use bevy_ecs::world::{FromWorld, World}; +use tracing::warn; + +use super::{IntoRenderNodeArray, Node, RenderGraph, RenderLabel, RenderSubGraph}; + +/// Adds common [`RenderGraph`] operations to [`SubApp`] (and [`App`]). +pub trait RenderGraphExt { + // Add a sub graph to the [`RenderGraph`] + fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self; + /// Add a [`Node`] to the [`RenderGraph`]: + /// * Create the [`Node`] using the [`FromWorld`] implementation + /// * Add it to the graph + fn add_render_graph_node( + &mut self, + sub_graph: impl RenderSubGraph, + node_label: impl RenderLabel, + ) -> &mut Self; + /// Automatically add the required node edges based on the given ordering + fn add_render_graph_edges( + &mut self, + sub_graph: impl RenderSubGraph, + edges: impl IntoRenderNodeArray, + ) -> &mut Self; + + /// Add node edge to the specified graph + fn add_render_graph_edge( + &mut self, + sub_graph: impl RenderSubGraph, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> &mut Self; +} + +impl RenderGraphExt for World { + fn add_render_graph_node( + &mut self, + sub_graph: impl RenderSubGraph, + node_label: impl RenderLabel, + ) -> &mut Self { + let sub_graph = sub_graph.intern(); + let node = T::from_world(self); + let mut render_graph = self.get_resource_mut::().expect( + "RenderGraph not found. Make sure you are using add_render_graph_node on the RenderApp", + ); + if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) { + graph.add_node(node_label, node); + } else { + warn!( + "Tried adding a render graph node to {sub_graph:?} but the sub graph doesn't exist" + ); + } + self + } + + #[track_caller] + fn add_render_graph_edges( + &mut self, + sub_graph: impl RenderSubGraph, + edges: impl IntoRenderNodeArray, + ) -> &mut Self { + let sub_graph = sub_graph.intern(); + let mut render_graph = self.get_resource_mut::().expect( + "RenderGraph not found. Make sure you are using add_render_graph_edges on the RenderApp", + ); + if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) { + graph.add_node_edges(edges); + } else { + warn!( + "Tried adding render graph edges to {sub_graph:?} but the sub graph doesn't exist" + ); + } + self + } + + fn add_render_graph_edge( + &mut self, + sub_graph: impl RenderSubGraph, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> &mut Self { + let sub_graph = sub_graph.intern(); + let mut render_graph = self.get_resource_mut::().expect( + "RenderGraph not found. Make sure you are using add_render_graph_edge on the RenderApp", + ); + if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) { + graph.add_node_edge(output_node, input_node); + } else { + warn!( + "Tried adding a render graph edge to {sub_graph:?} but the sub graph doesn't exist" + ); + } + self + } + + fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self { + let mut render_graph = self.get_resource_mut::().expect( + "RenderGraph not found. Make sure you are using add_render_sub_graph on the RenderApp", + ); + render_graph.add_sub_graph(sub_graph, RenderGraph::default()); + self + } +} + +impl RenderGraphExt for SubApp { + fn add_render_graph_node( + &mut self, + sub_graph: impl RenderSubGraph, + node_label: impl RenderLabel, + ) -> &mut Self { + World::add_render_graph_node::(self.world_mut(), sub_graph, node_label); + self + } + + fn add_render_graph_edge( + &mut self, + sub_graph: impl RenderSubGraph, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> &mut Self { + World::add_render_graph_edge(self.world_mut(), sub_graph, output_node, input_node); + self + } + + #[track_caller] + fn add_render_graph_edges( + &mut self, + sub_graph: impl RenderSubGraph, + edges: impl IntoRenderNodeArray, + ) -> &mut Self { + World::add_render_graph_edges(self.world_mut(), sub_graph, edges); + self + } + + fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self { + World::add_render_sub_graph(self.world_mut(), sub_graph); + self + } +} + +impl RenderGraphExt for App { + fn add_render_graph_node( + &mut self, + sub_graph: impl RenderSubGraph, + node_label: impl RenderLabel, + ) -> &mut Self { + World::add_render_graph_node::(self.world_mut(), sub_graph, node_label); + self + } + + fn add_render_graph_edge( + &mut self, + sub_graph: impl RenderSubGraph, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> &mut Self { + World::add_render_graph_edge(self.world_mut(), sub_graph, output_node, input_node); + self + } + + fn add_render_graph_edges( + &mut self, + sub_graph: impl RenderSubGraph, + edges: impl IntoRenderNodeArray, + ) -> &mut Self { + World::add_render_graph_edges(self.world_mut(), sub_graph, edges); + self + } + + fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self { + World::add_render_sub_graph(self.world_mut(), sub_graph); + self + } +} diff --git a/third_party/bevy_render/src/render_graph/camera_driver_node.rs b/third_party/bevy_render/src/render_graph/camera_driver_node.rs new file mode 100644 index 0000000..4ba4e93 --- /dev/null +++ b/third_party/bevy_render/src/render_graph/camera_driver_node.rs @@ -0,0 +1,107 @@ +use crate::{ + camera::{ExtractedCamera, SortedCameras}, + render_graph::{Node, NodeRunError, RenderGraphContext}, + renderer::RenderContext, + view::ExtractedWindows, +}; +use bevy_camera::{ClearColor, NormalizedRenderTarget}; +use bevy_ecs::{entity::ContainsEntity, prelude::QueryState, world::World}; +use bevy_platform::collections::HashSet; +use wgpu::{LoadOp, Operations, RenderPassColorAttachment, RenderPassDescriptor, StoreOp}; + +pub struct CameraDriverNode { + cameras: QueryState<&'static ExtractedCamera>, +} + +impl CameraDriverNode { + pub fn new(world: &mut World) -> Self { + Self { + cameras: world.query(), + } + } +} + +impl Node for CameraDriverNode { + fn update(&mut self, world: &mut World) { + self.cameras.update_archetypes(world); + } + fn run( + &self, + graph: &mut RenderGraphContext, + render_context: &mut RenderContext, + world: &World, + ) -> Result<(), NodeRunError> { + let sorted_cameras = world.resource::(); + let windows = world.resource::(); + let mut camera_windows = >::default(); + for sorted_camera in &sorted_cameras.0 { + let Ok(camera) = self.cameras.get_manual(world, sorted_camera.entity) else { + continue; + }; + + let mut run_graph = true; + if let Some(NormalizedRenderTarget::Window(window_ref)) = camera.target { + let window_entity = window_ref.entity(); + if windows + .windows + .get(&window_entity) + .is_some_and(|w| w.physical_width > 0 && w.physical_height > 0) + { + camera_windows.insert(window_entity); + } else { + // The window doesn't exist anymore or zero-sized so we don't need to run the graph + run_graph = false; + } + } + if run_graph { + graph.run_sub_graph( + camera.render_graph, + vec![], + Some(sorted_camera.entity), + Some(format!( + "Camera {} ({})", + sorted_camera.order, sorted_camera.entity + )), + )?; + } + } + + let clear_color_global = world.resource::(); + + // wgpu (and some backends) require doing work for swap chains if you call `get_current_texture()` and `present()` + // This ensures that Bevy doesn't crash, even when there are no cameras (and therefore no work submitted). + for (id, window) in world.resource::().iter() { + if camera_windows.contains(id) && render_context.has_commands() { + continue; + } + + let Some(swap_chain_texture) = &window.swap_chain_texture_view else { + continue; + }; + + #[cfg(feature = "trace")] + let _span = tracing::info_span!("no_camera_clear_pass").entered(); + let pass_descriptor = RenderPassDescriptor { + label: Some("no_camera_clear_pass"), + color_attachments: &[Some(RenderPassColorAttachment { + view: swap_chain_texture, + depth_slice: None, + resolve_target: None, + ops: Operations { + load: LoadOp::Clear(clear_color_global.to_linear().into()), + store: StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }; + + render_context + .command_encoder() + .begin_render_pass(&pass_descriptor); + } + + Ok(()) + } +} diff --git a/third_party/bevy_render/src/render_graph/context.rs b/third_party/bevy_render/src/render_graph/context.rs new file mode 100644 index 0000000..b1f35ed --- /dev/null +++ b/third_party/bevy_render/src/render_graph/context.rs @@ -0,0 +1,286 @@ +use crate::{ + render_graph::{NodeState, RenderGraph, SlotInfos, SlotLabel, SlotType, SlotValue}, + render_resource::{Buffer, Sampler, TextureView}, +}; +use alloc::borrow::Cow; +use bevy_ecs::{entity::Entity, intern::Interned}; +use thiserror::Error; + +use super::{InternedRenderSubGraph, RenderLabel, RenderSubGraph}; + +/// A command that signals the graph runner to run the sub graph corresponding to the `sub_graph` +/// with the specified `inputs` next. +pub struct RunSubGraph { + pub sub_graph: InternedRenderSubGraph, + pub inputs: Vec, + pub view_entity: Option, + pub debug_group: Option, +} + +/// The context with all graph information required to run a [`Node`](super::Node). +/// This context is created for each node by the render graph runner. +/// +/// The slot input can be read from here and the outputs must be written back to the context for +/// passing them onto the next node. +/// +/// Sub graphs can be queued for running by adding a [`RunSubGraph`] command to the context. +/// After the node has finished running the graph runner is responsible for executing the sub graphs. +pub struct RenderGraphContext<'a> { + graph: &'a RenderGraph, + node: &'a NodeState, + inputs: &'a [SlotValue], + outputs: &'a mut [Option], + run_sub_graphs: Vec, + /// The `view_entity` associated with the render graph being executed + /// This is optional because you aren't required to have a `view_entity` for a node. + /// For example, compute shader nodes don't have one. + /// It should always be set when the [`RenderGraph`] is running on a View. + view_entity: Option, +} + +impl<'a> RenderGraphContext<'a> { + /// Creates a new render graph context for the `node`. + pub fn new( + graph: &'a RenderGraph, + node: &'a NodeState, + inputs: &'a [SlotValue], + outputs: &'a mut [Option], + ) -> Self { + Self { + graph, + node, + inputs, + outputs, + run_sub_graphs: Vec::new(), + view_entity: None, + } + } + + /// Returns the input slot values for the node. + #[inline] + pub fn inputs(&self) -> &[SlotValue] { + self.inputs + } + + /// Returns the [`SlotInfos`] of the inputs. + pub fn input_info(&self) -> &SlotInfos { + &self.node.input_slots + } + + /// Returns the [`SlotInfos`] of the outputs. + pub fn output_info(&self) -> &SlotInfos { + &self.node.output_slots + } + + /// Retrieves the input slot value referenced by the `label`. + pub fn get_input(&self, label: impl Into) -> Result<&SlotValue, InputSlotError> { + let label = label.into(); + let index = self + .input_info() + .get_slot_index(label.clone()) + .ok_or(InputSlotError::InvalidSlot(label))?; + Ok(&self.inputs[index]) + } + + // TODO: should this return an Arc or a reference? + /// Retrieves the input slot value referenced by the `label` as a [`TextureView`]. + pub fn get_input_texture( + &self, + label: impl Into, + ) -> Result<&TextureView, InputSlotError> { + let label = label.into(); + match self.get_input(label.clone())? { + SlotValue::TextureView(value) => Ok(value), + value => Err(InputSlotError::MismatchedSlotType { + label, + actual: value.slot_type(), + expected: SlotType::TextureView, + }), + } + } + + /// Retrieves the input slot value referenced by the `label` as a [`Sampler`]. + pub fn get_input_sampler( + &self, + label: impl Into, + ) -> Result<&Sampler, InputSlotError> { + let label = label.into(); + match self.get_input(label.clone())? { + SlotValue::Sampler(value) => Ok(value), + value => Err(InputSlotError::MismatchedSlotType { + label, + actual: value.slot_type(), + expected: SlotType::Sampler, + }), + } + } + + /// Retrieves the input slot value referenced by the `label` as a [`Buffer`]. + pub fn get_input_buffer(&self, label: impl Into) -> Result<&Buffer, InputSlotError> { + let label = label.into(); + match self.get_input(label.clone())? { + SlotValue::Buffer(value) => Ok(value), + value => Err(InputSlotError::MismatchedSlotType { + label, + actual: value.slot_type(), + expected: SlotType::Buffer, + }), + } + } + + /// Retrieves the input slot value referenced by the `label` as an [`Entity`]. + pub fn get_input_entity(&self, label: impl Into) -> Result { + let label = label.into(); + match self.get_input(label.clone())? { + SlotValue::Entity(value) => Ok(*value), + value => Err(InputSlotError::MismatchedSlotType { + label, + actual: value.slot_type(), + expected: SlotType::Entity, + }), + } + } + + /// Sets the output slot value referenced by the `label`. + pub fn set_output( + &mut self, + label: impl Into, + value: impl Into, + ) -> Result<(), OutputSlotError> { + let label = label.into(); + let value = value.into(); + let slot_index = self + .output_info() + .get_slot_index(label.clone()) + .ok_or_else(|| OutputSlotError::InvalidSlot(label.clone()))?; + let slot = self + .output_info() + .get_slot(slot_index) + .expect("slot is valid"); + if value.slot_type() != slot.slot_type { + return Err(OutputSlotError::MismatchedSlotType { + label, + actual: slot.slot_type, + expected: value.slot_type(), + }); + } + self.outputs[slot_index] = Some(value); + Ok(()) + } + + pub fn view_entity(&self) -> Entity { + self.view_entity.unwrap() + } + + pub fn get_view_entity(&self) -> Option { + self.view_entity + } + + pub fn set_view_entity(&mut self, view_entity: Entity) { + self.view_entity = Some(view_entity); + } + + /// Queues up a sub graph for execution after the node has finished running. + pub fn run_sub_graph( + &mut self, + name: impl RenderSubGraph, + inputs: Vec, + view_entity: Option, + debug_group: Option, + ) -> Result<(), RunSubGraphError> { + let name = name.intern(); + let sub_graph = self + .graph + .get_sub_graph(name) + .ok_or(RunSubGraphError::MissingSubGraph(name))?; + if let Some(input_node) = sub_graph.get_input_node() { + for (i, input_slot) in input_node.input_slots.iter().enumerate() { + if let Some(input_value) = inputs.get(i) { + if input_slot.slot_type != input_value.slot_type() { + return Err(RunSubGraphError::MismatchedInputSlotType { + graph_name: name, + slot_index: i, + actual: input_value.slot_type(), + expected: input_slot.slot_type, + label: input_slot.name.clone().into(), + }); + } + } else { + return Err(RunSubGraphError::MissingInput { + slot_index: i, + slot_name: input_slot.name.clone(), + graph_name: name, + }); + } + } + } else if !inputs.is_empty() { + return Err(RunSubGraphError::SubGraphHasNoInputs(name)); + } + + self.run_sub_graphs.push(RunSubGraph { + sub_graph: name, + inputs, + view_entity, + debug_group, + }); + + Ok(()) + } + + /// Returns a human-readable label for this node, for debugging purposes. + pub fn label(&self) -> Interned { + self.node.label + } + + /// Finishes the context for this [`Node`](super::Node) by + /// returning the sub graphs to run next. + pub fn finish(self) -> Vec { + self.run_sub_graphs + } +} + +#[derive(Error, Debug, Eq, PartialEq)] +pub enum RunSubGraphError { + #[error("attempted to run sub-graph `{0:?}`, but it does not exist")] + MissingSubGraph(InternedRenderSubGraph), + #[error("attempted to pass inputs to sub-graph `{0:?}`, which has no input slots")] + SubGraphHasNoInputs(InternedRenderSubGraph), + #[error("sub graph (name: `{graph_name:?}`) could not be run because slot `{slot_name}` at index {slot_index} has no value")] + MissingInput { + slot_index: usize, + slot_name: Cow<'static, str>, + graph_name: InternedRenderSubGraph, + }, + #[error("attempted to use the wrong type for input slot")] + MismatchedInputSlotType { + graph_name: InternedRenderSubGraph, + slot_index: usize, + label: SlotLabel, + expected: SlotType, + actual: SlotType, + }, +} + +#[derive(Error, Debug, Eq, PartialEq)] +pub enum OutputSlotError { + #[error("output slot `{0:?}` does not exist")] + InvalidSlot(SlotLabel), + #[error("attempted to output a value of type `{actual}` to output slot `{label:?}`, which has type `{expected}`")] + MismatchedSlotType { + label: SlotLabel, + expected: SlotType, + actual: SlotType, + }, +} + +#[derive(Error, Debug, Eq, PartialEq)] +pub enum InputSlotError { + #[error("input slot `{0:?}` does not exist")] + InvalidSlot(SlotLabel), + #[error("attempted to retrieve a value of type `{actual}` from input slot `{label:?}`, which has type `{expected}`")] + MismatchedSlotType { + label: SlotLabel, + expected: SlotType, + actual: SlotType, + }, +} diff --git a/third_party/bevy_render/src/render_graph/edge.rs b/third_party/bevy_render/src/render_graph/edge.rs new file mode 100644 index 0000000..199b7e8 --- /dev/null +++ b/third_party/bevy_render/src/render_graph/edge.rs @@ -0,0 +1,57 @@ +use super::InternedRenderLabel; + +/// An edge, which connects two [`Nodes`](super::Node) in +/// a [`RenderGraph`](crate::render_graph::RenderGraph). +/// +/// They are used to describe the ordering (which node has to run first) +/// and may be of two kinds: [`NodeEdge`](Self::NodeEdge) and [`SlotEdge`](Self::SlotEdge). +/// +/// Edges are added via the [`RenderGraph::add_node_edge`] and the +/// [`RenderGraph::add_slot_edge`] methods. +/// +/// The former simply states that the `output_node` has to be run before the `input_node`, +/// while the later connects an output slot of the `output_node` +/// with an input slot of the `input_node` to pass additional data along. +/// For more information see [`SlotType`](super::SlotType). +/// +/// [`RenderGraph::add_node_edge`]: crate::render_graph::RenderGraph::add_node_edge +/// [`RenderGraph::add_slot_edge`]: crate::render_graph::RenderGraph::add_slot_edge +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Edge { + /// An edge describing to ordering of both nodes (`output_node` before `input_node`) + /// and connecting the output slot at the `output_index` of the `output_node` + /// with the slot at the `input_index` of the `input_node`. + SlotEdge { + input_node: InternedRenderLabel, + input_index: usize, + output_node: InternedRenderLabel, + output_index: usize, + }, + /// An edge describing to ordering of both nodes (`output_node` before `input_node`). + NodeEdge { + input_node: InternedRenderLabel, + output_node: InternedRenderLabel, + }, +} + +impl Edge { + /// Returns the id of the `input_node`. + pub fn get_input_node(&self) -> InternedRenderLabel { + match self { + Edge::SlotEdge { input_node, .. } | Edge::NodeEdge { input_node, .. } => *input_node, + } + } + + /// Returns the id of the `output_node`. + pub fn get_output_node(&self) -> InternedRenderLabel { + match self { + Edge::SlotEdge { output_node, .. } | Edge::NodeEdge { output_node, .. } => *output_node, + } + } +} + +#[derive(PartialEq, Eq)] +pub enum EdgeExistence { + Exists, + DoesNotExist, +} diff --git a/third_party/bevy_render/src/render_graph/graph.rs b/third_party/bevy_render/src/render_graph/graph.rs new file mode 100644 index 0000000..b7f8328 --- /dev/null +++ b/third_party/bevy_render/src/render_graph/graph.rs @@ -0,0 +1,918 @@ +use crate::{ + render_graph::{ + Edge, Node, NodeRunError, NodeState, RenderGraphContext, RenderGraphError, RenderLabel, + SlotInfo, SlotLabel, + }, + renderer::RenderContext, +}; +use bevy_ecs::{define_label, intern::Interned, prelude::World, resource::Resource}; +use bevy_platform::collections::HashMap; +use core::fmt::Debug; + +use super::{EdgeExistence, InternedRenderLabel, IntoRenderNodeArray}; + +pub use bevy_render_macros::RenderSubGraph; + +define_label!( + #[diagnostic::on_unimplemented( + note = "consider annotating `{Self}` with `#[derive(RenderSubGraph)]`" + )] + /// A strongly-typed class of labels used to identify a [`SubGraph`] in a render graph. + RenderSubGraph, + RENDER_SUB_GRAPH_INTERNER +); + +/// A shorthand for `Interned`. +pub type InternedRenderSubGraph = Interned; + +/// The render graph configures the modular and re-usable render logic. +/// +/// It is a retained and stateless (nodes themselves may have their own internal state) structure, +/// which can not be modified while it is executed by the graph runner. +/// +/// The render graph runner is responsible for executing the entire graph each frame. +/// It will execute each node in the graph in the correct order, based on the edges between the nodes. +/// +/// It consists of three main components: [`Nodes`](Node), [`Edges`](Edge) +/// and [`Slots`](super::SlotType). +/// +/// Nodes are responsible for generating draw calls and operating on input and output slots. +/// Edges specify the order of execution for nodes and connect input and output slots together. +/// Slots describe the render resources created or used by the nodes. +/// +/// Additionally a render graph can contain multiple sub graphs, which are run by the +/// corresponding nodes. Every render graph can have its own optional input node. +/// +/// ## Example +/// Here is a simple render graph example with two nodes connected by a node edge. +/// ```ignore +/// # TODO: Remove when #10645 is fixed +/// # use bevy_app::prelude::*; +/// # use bevy_ecs::prelude::World; +/// # use bevy_render::render_graph::{RenderGraph, RenderLabel, Node, RenderGraphContext, NodeRunError}; +/// # use bevy_render::renderer::RenderContext; +/// # +/// #[derive(RenderLabel)] +/// enum Labels { +/// A, +/// B, +/// } +/// +/// # struct MyNode; +/// # +/// # impl Node for MyNode { +/// # fn run(&self, graph: &mut RenderGraphContext, render_context: &mut RenderContext, world: &World) -> Result<(), NodeRunError> { +/// # unimplemented!() +/// # } +/// # } +/// # +/// let mut graph = RenderGraph::default(); +/// graph.add_node(Labels::A, MyNode); +/// graph.add_node(Labels::B, MyNode); +/// graph.add_node_edge(Labels::B, Labels::A); +/// ``` +#[derive(Resource, Default)] +pub struct RenderGraph { + nodes: HashMap, + sub_graphs: HashMap, +} + +/// The label for the input node of a graph. Used to connect other nodes to it. +#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)] +pub struct GraphInput; + +impl RenderGraph { + /// Updates all nodes and sub graphs of the render graph. Should be called before executing it. + pub fn update(&mut self, world: &mut World) { + for node in self.nodes.values_mut() { + node.node.update(world); + } + + for sub_graph in self.sub_graphs.values_mut() { + sub_graph.update(world); + } + } + + /// Creates an [`GraphInputNode`] with the specified slots if not already present. + pub fn set_input(&mut self, inputs: Vec) { + assert!( + matches!( + self.get_node_state(GraphInput), + Err(RenderGraphError::InvalidNode(_)) + ), + "Graph already has an input node" + ); + + self.add_node(GraphInput, GraphInputNode { inputs }); + } + + /// Returns the [`NodeState`] of the input node of this graph. + /// + /// # See also + /// + /// - [`input_node`](Self::input_node) for an unchecked version. + #[inline] + pub fn get_input_node(&self) -> Option<&NodeState> { + self.get_node_state(GraphInput).ok() + } + + /// Returns the [`NodeState`] of the input node of this graph. + /// + /// # Panics + /// + /// Panics if there is no input node set. + /// + /// # See also + /// + /// - [`get_input_node`](Self::get_input_node) for a version which returns an [`Option`] instead. + #[inline] + pub fn input_node(&self) -> &NodeState { + self.get_input_node().unwrap() + } + + /// Adds the `node` with the `label` to the graph. + /// If the label is already present replaces it instead. + pub fn add_node(&mut self, label: impl RenderLabel, node: T) + where + T: Node, + { + let label = label.intern(); + let node_state = NodeState::new(label, node); + self.nodes.insert(label, node_state); + } + + /// Add `node_edge`s based on the order of the given `edges` array. + /// + /// Defining an edge that already exists is not considered an error with this api. + /// It simply won't create a new edge. + #[track_caller] + pub fn add_node_edges(&mut self, edges: impl IntoRenderNodeArray) { + for window in edges.into_array().windows(2) { + let [a, b] = window else { + break; + }; + if let Err(err) = self.try_add_node_edge(*a, *b) { + match err { + // Already existing edges are very easy to produce with this api + // and shouldn't cause a panic + RenderGraphError::EdgeAlreadyExists(_) => {} + _ => panic!("{err}"), + } + } + } + } + + /// Removes the `node` with the `label` from the graph. + /// If the label does not exist, nothing happens. + pub fn remove_node(&mut self, label: impl RenderLabel) -> Result<(), RenderGraphError> { + let label = label.intern(); + if let Some(node_state) = self.nodes.remove(&label) { + // Remove all edges from other nodes to this one. Note that as we're removing this + // node, we don't need to remove its input edges + for input_edge in node_state.edges.input_edges() { + match input_edge { + Edge::SlotEdge { output_node, .. } + | Edge::NodeEdge { + input_node: _, + output_node, + } => { + if let Ok(output_node) = self.get_node_state_mut(*output_node) { + output_node.edges.remove_output_edge(input_edge.clone())?; + } + } + } + } + // Remove all edges from this node to other nodes. Note that as we're removing this + // node, we don't need to remove its output edges + for output_edge in node_state.edges.output_edges() { + match output_edge { + Edge::SlotEdge { + output_node: _, + output_index: _, + input_node, + input_index: _, + } + | Edge::NodeEdge { + output_node: _, + input_node, + } => { + if let Ok(input_node) = self.get_node_state_mut(*input_node) { + input_node.edges.remove_input_edge(output_edge.clone())?; + } + } + } + } + } + + Ok(()) + } + + /// Retrieves the [`NodeState`] referenced by the `label`. + pub fn get_node_state(&self, label: impl RenderLabel) -> Result<&NodeState, RenderGraphError> { + let label = label.intern(); + self.nodes + .get(&label) + .ok_or(RenderGraphError::InvalidNode(label)) + } + + /// Retrieves the [`NodeState`] referenced by the `label` mutably. + pub fn get_node_state_mut( + &mut self, + label: impl RenderLabel, + ) -> Result<&mut NodeState, RenderGraphError> { + let label = label.intern(); + self.nodes + .get_mut(&label) + .ok_or(RenderGraphError::InvalidNode(label)) + } + + /// Retrieves the [`Node`] referenced by the `label`. + pub fn get_node(&self, label: impl RenderLabel) -> Result<&T, RenderGraphError> + where + T: Node, + { + self.get_node_state(label).and_then(|n| n.node()) + } + + /// Retrieves the [`Node`] referenced by the `label` mutably. + pub fn get_node_mut(&mut self, label: impl RenderLabel) -> Result<&mut T, RenderGraphError> + where + T: Node, + { + self.get_node_state_mut(label).and_then(|n| n.node_mut()) + } + + /// Adds the [`Edge::SlotEdge`] to the graph. This guarantees that the `output_node` + /// is run before the `input_node` and also connects the `output_slot` to the `input_slot`. + /// + /// Fails if any invalid [`RenderLabel`]s or [`SlotLabel`]s are given. + /// + /// # See also + /// + /// - [`add_slot_edge`](Self::add_slot_edge) for an infallible version. + pub fn try_add_slot_edge( + &mut self, + output_node: impl RenderLabel, + output_slot: impl Into, + input_node: impl RenderLabel, + input_slot: impl Into, + ) -> Result<(), RenderGraphError> { + let output_slot = output_slot.into(); + let input_slot = input_slot.into(); + + let output_node = output_node.intern(); + let input_node = input_node.intern(); + + let output_index = self + .get_node_state(output_node)? + .output_slots + .get_slot_index(output_slot.clone()) + .ok_or(RenderGraphError::InvalidOutputNodeSlot(output_slot))?; + let input_index = self + .get_node_state(input_node)? + .input_slots + .get_slot_index(input_slot.clone()) + .ok_or(RenderGraphError::InvalidInputNodeSlot(input_slot))?; + + let edge = Edge::SlotEdge { + output_node, + output_index, + input_node, + input_index, + }; + + self.validate_edge(&edge, EdgeExistence::DoesNotExist)?; + + { + let output_node = self.get_node_state_mut(output_node)?; + output_node.edges.add_output_edge(edge.clone())?; + } + let input_node = self.get_node_state_mut(input_node)?; + input_node.edges.add_input_edge(edge)?; + + Ok(()) + } + + /// Adds the [`Edge::SlotEdge`] to the graph. This guarantees that the `output_node` + /// is run before the `input_node` and also connects the `output_slot` to the `input_slot`. + /// + /// # Panics + /// + /// Any invalid [`RenderLabel`]s or [`SlotLabel`]s are given. + /// + /// # See also + /// + /// - [`try_add_slot_edge`](Self::try_add_slot_edge) for a fallible version. + pub fn add_slot_edge( + &mut self, + output_node: impl RenderLabel, + output_slot: impl Into, + input_node: impl RenderLabel, + input_slot: impl Into, + ) { + self.try_add_slot_edge(output_node, output_slot, input_node, input_slot) + .unwrap(); + } + + /// Removes the [`Edge::SlotEdge`] from the graph. If any nodes or slots do not exist then + /// nothing happens. + pub fn remove_slot_edge( + &mut self, + output_node: impl RenderLabel, + output_slot: impl Into, + input_node: impl RenderLabel, + input_slot: impl Into, + ) -> Result<(), RenderGraphError> { + let output_slot = output_slot.into(); + let input_slot = input_slot.into(); + + let output_node = output_node.intern(); + let input_node = input_node.intern(); + + let output_index = self + .get_node_state(output_node)? + .output_slots + .get_slot_index(output_slot.clone()) + .ok_or(RenderGraphError::InvalidOutputNodeSlot(output_slot))?; + let input_index = self + .get_node_state(input_node)? + .input_slots + .get_slot_index(input_slot.clone()) + .ok_or(RenderGraphError::InvalidInputNodeSlot(input_slot))?; + + let edge = Edge::SlotEdge { + output_node, + output_index, + input_node, + input_index, + }; + + self.validate_edge(&edge, EdgeExistence::Exists)?; + + { + let output_node = self.get_node_state_mut(output_node)?; + output_node.edges.remove_output_edge(edge.clone())?; + } + let input_node = self.get_node_state_mut(input_node)?; + input_node.edges.remove_input_edge(edge)?; + + Ok(()) + } + + /// Adds the [`Edge::NodeEdge`] to the graph. This guarantees that the `output_node` + /// is run before the `input_node`. + /// + /// Fails if any invalid [`RenderLabel`] is given. + /// + /// # See also + /// + /// - [`add_node_edge`](Self::add_node_edge) for an infallible version. + pub fn try_add_node_edge( + &mut self, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> Result<(), RenderGraphError> { + let output_node = output_node.intern(); + let input_node = input_node.intern(); + + let edge = Edge::NodeEdge { + output_node, + input_node, + }; + + self.validate_edge(&edge, EdgeExistence::DoesNotExist)?; + + { + let output_node = self.get_node_state_mut(output_node)?; + output_node.edges.add_output_edge(edge.clone())?; + } + let input_node = self.get_node_state_mut(input_node)?; + input_node.edges.add_input_edge(edge)?; + + Ok(()) + } + + /// Adds the [`Edge::NodeEdge`] to the graph. This guarantees that the `output_node` + /// is run before the `input_node`. + /// + /// # Panics + /// + /// Panics if any invalid [`RenderLabel`] is given. + /// + /// # See also + /// + /// - [`try_add_node_edge`](Self::try_add_node_edge) for a fallible version. + pub fn add_node_edge(&mut self, output_node: impl RenderLabel, input_node: impl RenderLabel) { + self.try_add_node_edge(output_node, input_node).unwrap(); + } + + /// Removes the [`Edge::NodeEdge`] from the graph. If either node does not exist then nothing + /// happens. + pub fn remove_node_edge( + &mut self, + output_node: impl RenderLabel, + input_node: impl RenderLabel, + ) -> Result<(), RenderGraphError> { + let output_node = output_node.intern(); + let input_node = input_node.intern(); + + let edge = Edge::NodeEdge { + output_node, + input_node, + }; + + self.validate_edge(&edge, EdgeExistence::Exists)?; + + { + let output_node = self.get_node_state_mut(output_node)?; + output_node.edges.remove_output_edge(edge.clone())?; + } + let input_node = self.get_node_state_mut(input_node)?; + input_node.edges.remove_input_edge(edge)?; + + Ok(()) + } + + /// Verifies that the edge existence is as expected and + /// checks that slot edges are connected correctly. + pub fn validate_edge( + &mut self, + edge: &Edge, + should_exist: EdgeExistence, + ) -> Result<(), RenderGraphError> { + if should_exist == EdgeExistence::Exists && !self.has_edge(edge) { + return Err(RenderGraphError::EdgeDoesNotExist(edge.clone())); + } else if should_exist == EdgeExistence::DoesNotExist && self.has_edge(edge) { + return Err(RenderGraphError::EdgeAlreadyExists(edge.clone())); + } + + match *edge { + Edge::SlotEdge { + output_node, + output_index, + input_node, + input_index, + } => { + let output_node_state = self.get_node_state(output_node)?; + let input_node_state = self.get_node_state(input_node)?; + + let output_slot = output_node_state + .output_slots + .get_slot(output_index) + .ok_or(RenderGraphError::InvalidOutputNodeSlot(SlotLabel::Index( + output_index, + )))?; + let input_slot = input_node_state.input_slots.get_slot(input_index).ok_or( + RenderGraphError::InvalidInputNodeSlot(SlotLabel::Index(input_index)), + )?; + + if let Some(Edge::SlotEdge { + output_node: current_output_node, + .. + }) = input_node_state.edges.input_edges().iter().find(|e| { + if let Edge::SlotEdge { + input_index: current_input_index, + .. + } = e + { + input_index == *current_input_index + } else { + false + } + }) && should_exist == EdgeExistence::DoesNotExist + { + return Err(RenderGraphError::NodeInputSlotAlreadyOccupied { + node: input_node, + input_slot: input_index, + occupied_by_node: *current_output_node, + }); + } + + if output_slot.slot_type != input_slot.slot_type { + return Err(RenderGraphError::MismatchedNodeSlots { + output_node, + output_slot: output_index, + input_node, + input_slot: input_index, + }); + } + } + Edge::NodeEdge { .. } => { /* nothing to validate here */ } + } + + Ok(()) + } + + /// Checks whether the `edge` already exists in the graph. + pub fn has_edge(&self, edge: &Edge) -> bool { + let output_node_state = self.get_node_state(edge.get_output_node()); + let input_node_state = self.get_node_state(edge.get_input_node()); + if let Ok(output_node_state) = output_node_state + && output_node_state.edges.output_edges().contains(edge) + && let Ok(input_node_state) = input_node_state + && input_node_state.edges.input_edges().contains(edge) + { + return true; + } + + false + } + + /// Returns an iterator over the [`NodeStates`](NodeState). + pub fn iter_nodes(&self) -> impl Iterator { + self.nodes.values() + } + + /// Returns an iterator over the [`NodeStates`](NodeState), that allows modifying each value. + pub fn iter_nodes_mut(&mut self) -> impl Iterator { + self.nodes.values_mut() + } + + /// Returns an iterator over the sub graphs. + pub fn iter_sub_graphs(&self) -> impl Iterator { + self.sub_graphs.iter().map(|(name, graph)| (*name, graph)) + } + + /// Returns an iterator over the sub graphs, that allows modifying each value. + pub fn iter_sub_graphs_mut( + &mut self, + ) -> impl Iterator { + self.sub_graphs + .iter_mut() + .map(|(name, graph)| (*name, graph)) + } + + /// Returns an iterator over a tuple of the input edges and the corresponding output nodes + /// for the node referenced by the label. + pub fn iter_node_inputs( + &self, + label: impl RenderLabel, + ) -> Result, RenderGraphError> { + let node = self.get_node_state(label)?; + Ok(node + .edges + .input_edges() + .iter() + .map(|edge| (edge, edge.get_output_node())) + .map(move |(edge, output_node)| (edge, self.get_node_state(output_node).unwrap()))) + } + + /// Returns an iterator over a tuple of the output edges and the corresponding input nodes + /// for the node referenced by the label. + pub fn iter_node_outputs( + &self, + label: impl RenderLabel, + ) -> Result, RenderGraphError> { + let node = self.get_node_state(label)?; + Ok(node + .edges + .output_edges() + .iter() + .map(|edge| (edge, edge.get_input_node())) + .map(move |(edge, input_node)| (edge, self.get_node_state(input_node).unwrap()))) + } + + /// Adds the `sub_graph` with the `label` to the graph. + /// If the label is already present replaces it instead. + pub fn add_sub_graph(&mut self, label: impl RenderSubGraph, sub_graph: RenderGraph) { + self.sub_graphs.insert(label.intern(), sub_graph); + } + + /// Removes the `sub_graph` with the `label` from the graph. + /// If the label does not exist then nothing happens. + pub fn remove_sub_graph(&mut self, label: impl RenderSubGraph) { + self.sub_graphs.remove(&label.intern()); + } + + /// Retrieves the sub graph corresponding to the `label`. + pub fn get_sub_graph(&self, label: impl RenderSubGraph) -> Option<&RenderGraph> { + self.sub_graphs.get(&label.intern()) + } + + /// Retrieves the sub graph corresponding to the `label` mutably. + pub fn get_sub_graph_mut(&mut self, label: impl RenderSubGraph) -> Option<&mut RenderGraph> { + self.sub_graphs.get_mut(&label.intern()) + } + + /// Retrieves the sub graph corresponding to the `label`. + /// + /// # Panics + /// + /// Panics if any invalid subgraph label is given. + /// + /// # See also + /// + /// - [`get_sub_graph`](Self::get_sub_graph) for a fallible version. + pub fn sub_graph(&self, label: impl RenderSubGraph) -> &RenderGraph { + let label = label.intern(); + self.sub_graphs + .get(&label) + .unwrap_or_else(|| panic!("Subgraph {label:?} not found")) + } + + /// Retrieves the sub graph corresponding to the `label` mutably. + /// + /// # Panics + /// + /// Panics if any invalid subgraph label is given. + /// + /// # See also + /// + /// - [`get_sub_graph_mut`](Self::get_sub_graph_mut) for a fallible version. + pub fn sub_graph_mut(&mut self, label: impl RenderSubGraph) -> &mut RenderGraph { + let label = label.intern(); + self.sub_graphs + .get_mut(&label) + .unwrap_or_else(|| panic!("Subgraph {label:?} not found")) + } +} + +impl Debug for RenderGraph { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + for node in self.iter_nodes() { + writeln!(f, "{:?}", node.label)?; + writeln!(f, " in: {:?}", node.input_slots)?; + writeln!(f, " out: {:?}", node.output_slots)?; + } + + Ok(()) + } +} + +/// A [`Node`] which acts as an entry point for a [`RenderGraph`] with custom inputs. +/// It has the same input and output slots and simply copies them over when run. +pub struct GraphInputNode { + inputs: Vec, +} + +impl Node for GraphInputNode { + fn input(&self) -> Vec { + self.inputs.clone() + } + + fn output(&self) -> Vec { + self.inputs.clone() + } + + fn run( + &self, + graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + _world: &World, + ) -> Result<(), NodeRunError> { + for i in 0..graph.inputs().len() { + let input = graph.inputs()[i].clone(); + graph.set_output(i, input)?; + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use crate::{ + render_graph::{ + node::IntoRenderNodeArray, Edge, InternedRenderLabel, Node, NodeRunError, RenderGraph, + RenderGraphContext, RenderGraphError, RenderLabel, SlotInfo, SlotType, + }, + renderer::RenderContext, + }; + use bevy_ecs::world::{FromWorld, World}; + use bevy_platform::collections::HashSet; + + #[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)] + enum TestLabel { + A, + B, + C, + D, + } + + #[derive(Debug)] + struct TestNode { + inputs: Vec, + outputs: Vec, + } + + impl TestNode { + pub fn new(inputs: usize, outputs: usize) -> Self { + TestNode { + inputs: (0..inputs) + .map(|i| SlotInfo::new(format!("in_{i}"), SlotType::TextureView)) + .collect(), + outputs: (0..outputs) + .map(|i| SlotInfo::new(format!("out_{i}"), SlotType::TextureView)) + .collect(), + } + } + } + + impl Node for TestNode { + fn input(&self) -> Vec { + self.inputs.clone() + } + + fn output(&self) -> Vec { + self.outputs.clone() + } + + fn run( + &self, + _: &mut RenderGraphContext, + _: &mut RenderContext, + _: &World, + ) -> Result<(), NodeRunError> { + Ok(()) + } + } + + fn input_nodes(label: impl RenderLabel, graph: &RenderGraph) -> HashSet { + graph + .iter_node_inputs(label) + .unwrap() + .map(|(_edge, node)| node.label) + .collect::>() + } + + fn output_nodes(label: impl RenderLabel, graph: &RenderGraph) -> HashSet { + graph + .iter_node_outputs(label) + .unwrap() + .map(|(_edge, node)| node.label) + .collect::>() + } + + #[test] + fn test_graph_edges() { + let mut graph = RenderGraph::default(); + graph.add_node(TestLabel::A, TestNode::new(0, 1)); + graph.add_node(TestLabel::B, TestNode::new(0, 1)); + graph.add_node(TestLabel::C, TestNode::new(1, 1)); + graph.add_node(TestLabel::D, TestNode::new(1, 0)); + + graph.add_slot_edge(TestLabel::A, "out_0", TestLabel::C, "in_0"); + graph.add_node_edge(TestLabel::B, TestLabel::C); + graph.add_slot_edge(TestLabel::C, 0, TestLabel::D, 0); + + assert!( + input_nodes(TestLabel::A, &graph).is_empty(), + "A has no inputs" + ); + assert_eq!( + output_nodes(TestLabel::A, &graph), + HashSet::from_iter((TestLabel::C,).into_array()), + "A outputs to C" + ); + + assert!( + input_nodes(TestLabel::B, &graph).is_empty(), + "B has no inputs" + ); + assert_eq!( + output_nodes(TestLabel::B, &graph), + HashSet::from_iter((TestLabel::C,).into_array()), + "B outputs to C" + ); + + assert_eq!( + input_nodes(TestLabel::C, &graph), + HashSet::from_iter((TestLabel::A, TestLabel::B).into_array()), + "A and B input to C" + ); + assert_eq!( + output_nodes(TestLabel::C, &graph), + HashSet::from_iter((TestLabel::D,).into_array()), + "C outputs to D" + ); + + assert_eq!( + input_nodes(TestLabel::D, &graph), + HashSet::from_iter((TestLabel::C,).into_array()), + "C inputs to D" + ); + assert!( + output_nodes(TestLabel::D, &graph).is_empty(), + "D has no outputs" + ); + } + + #[test] + fn test_get_node_typed() { + struct MyNode { + value: usize, + } + + impl Node for MyNode { + fn run( + &self, + _: &mut RenderGraphContext, + _: &mut RenderContext, + _: &World, + ) -> Result<(), NodeRunError> { + Ok(()) + } + } + + let mut graph = RenderGraph::default(); + + graph.add_node(TestLabel::A, MyNode { value: 42 }); + + let node: &MyNode = graph.get_node(TestLabel::A).unwrap(); + assert_eq!(node.value, 42, "node value matches"); + + let result: Result<&TestNode, RenderGraphError> = graph.get_node(TestLabel::A); + assert_eq!( + result.unwrap_err(), + RenderGraphError::WrongNodeType, + "expect a wrong node type error" + ); + } + + #[test] + fn test_slot_already_occupied() { + let mut graph = RenderGraph::default(); + + graph.add_node(TestLabel::A, TestNode::new(0, 1)); + graph.add_node(TestLabel::B, TestNode::new(0, 1)); + graph.add_node(TestLabel::C, TestNode::new(1, 1)); + + graph.add_slot_edge(TestLabel::A, 0, TestLabel::C, 0); + assert_eq!( + graph.try_add_slot_edge(TestLabel::B, 0, TestLabel::C, 0), + Err(RenderGraphError::NodeInputSlotAlreadyOccupied { + node: TestLabel::C.intern(), + input_slot: 0, + occupied_by_node: TestLabel::A.intern(), + }), + "Adding to a slot that is already occupied should return an error" + ); + } + + #[test] + fn test_edge_already_exists() { + let mut graph = RenderGraph::default(); + + graph.add_node(TestLabel::A, TestNode::new(0, 1)); + graph.add_node(TestLabel::B, TestNode::new(1, 0)); + + graph.add_slot_edge(TestLabel::A, 0, TestLabel::B, 0); + assert_eq!( + graph.try_add_slot_edge(TestLabel::A, 0, TestLabel::B, 0), + Err(RenderGraphError::EdgeAlreadyExists(Edge::SlotEdge { + output_node: TestLabel::A.intern(), + output_index: 0, + input_node: TestLabel::B.intern(), + input_index: 0, + })), + "Adding to a duplicate edge should return an error" + ); + } + + #[test] + fn test_add_node_edges() { + struct SimpleNode; + impl Node for SimpleNode { + fn run( + &self, + _graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + _world: &World, + ) -> Result<(), NodeRunError> { + Ok(()) + } + } + impl FromWorld for SimpleNode { + fn from_world(_world: &mut World) -> Self { + Self + } + } + + let mut graph = RenderGraph::default(); + graph.add_node(TestLabel::A, SimpleNode); + graph.add_node(TestLabel::B, SimpleNode); + graph.add_node(TestLabel::C, SimpleNode); + + graph.add_node_edges((TestLabel::A, TestLabel::B, TestLabel::C)); + + assert_eq!( + output_nodes(TestLabel::A, &graph), + HashSet::from_iter((TestLabel::B,).into_array()), + "A -> B" + ); + assert_eq!( + input_nodes(TestLabel::B, &graph), + HashSet::from_iter((TestLabel::A,).into_array()), + "A -> B" + ); + assert_eq!( + output_nodes(TestLabel::B, &graph), + HashSet::from_iter((TestLabel::C,).into_array()), + "B -> C" + ); + assert_eq!( + input_nodes(TestLabel::C, &graph), + HashSet::from_iter((TestLabel::B,).into_array()), + "B -> C" + ); + } +} diff --git a/third_party/bevy_render/src/render_graph/mod.rs b/third_party/bevy_render/src/render_graph/mod.rs new file mode 100644 index 0000000..6f98a30 --- /dev/null +++ b/third_party/bevy_render/src/render_graph/mod.rs @@ -0,0 +1,56 @@ +mod app; +mod camera_driver_node; +mod context; +mod edge; +mod graph; +mod node; +mod node_slot; + +pub use app::*; +pub use camera_driver_node::*; +pub use context::*; +pub use edge::*; +pub use graph::*; +pub use node::*; +pub use node_slot::*; + +use thiserror::Error; + +#[derive(Error, Debug, Eq, PartialEq)] +pub enum RenderGraphError { + #[error("node {0:?} does not exist")] + InvalidNode(InternedRenderLabel), + #[error("output node slot does not exist")] + InvalidOutputNodeSlot(SlotLabel), + #[error("input node slot does not exist")] + InvalidInputNodeSlot(SlotLabel), + #[error("node does not match the given type")] + WrongNodeType, + #[error("attempted to connect output slot {output_slot} from node {output_node:?} to incompatible input slot {input_slot} from node {input_node:?}")] + MismatchedNodeSlots { + output_node: InternedRenderLabel, + output_slot: usize, + input_node: InternedRenderLabel, + input_slot: usize, + }, + #[error("attempted to add an edge that already exists")] + EdgeAlreadyExists(Edge), + #[error("attempted to remove an edge that does not exist")] + EdgeDoesNotExist(Edge), + #[error("node {node:?} has an unconnected input slot {input_slot}")] + UnconnectedNodeInputSlot { + node: InternedRenderLabel, + input_slot: usize, + }, + #[error("node {node:?} has an unconnected output slot {output_slot}")] + UnconnectedNodeOutputSlot { + node: InternedRenderLabel, + output_slot: usize, + }, + #[error("node {node:?} input slot {input_slot} already occupied by {occupied_by_node:?}")] + NodeInputSlotAlreadyOccupied { + node: InternedRenderLabel, + input_slot: usize, + occupied_by_node: InternedRenderLabel, + }, +} diff --git a/third_party/bevy_render/src/render_graph/node.rs b/third_party/bevy_render/src/render_graph/node.rs new file mode 100644 index 0000000..bfc007b --- /dev/null +++ b/third_party/bevy_render/src/render_graph/node.rs @@ -0,0 +1,426 @@ +use crate::{ + render_graph::{ + Edge, InputSlotError, OutputSlotError, RenderGraphContext, RenderGraphError, + RunSubGraphError, SlotInfo, SlotInfos, + }, + render_phase::DrawError, + renderer::RenderContext, +}; +pub use bevy_ecs::label::DynEq; +use bevy_ecs::{ + define_label, + intern::Interned, + query::{QueryItem, QueryState, ReadOnlyQueryData}, + world::{FromWorld, World}, +}; +use core::fmt::Debug; +use downcast_rs::{impl_downcast, Downcast}; +use thiserror::Error; +use variadics_please::all_tuples_with_size; + +pub use bevy_render_macros::RenderLabel; + +use super::{InternedRenderSubGraph, RenderSubGraph}; + +define_label!( + #[diagnostic::on_unimplemented( + note = "consider annotating `{Self}` with `#[derive(RenderLabel)]`" + )] + /// A strongly-typed class of labels used to identify a [`Node`] in a render graph. + RenderLabel, + RENDER_LABEL_INTERNER +); + +/// A shorthand for `Interned`. +pub type InternedRenderLabel = Interned; + +pub trait IntoRenderNodeArray { + fn into_array(self) -> [InternedRenderLabel; N]; +} + +impl IntoRenderNodeArray for Vec { + fn into_array(self) -> [InternedRenderLabel; N] { + self.try_into().unwrap() + } +} + +macro_rules! impl_render_label_tuples { + ($N: expr, $(#[$meta:meta])* $(($T: ident, $I: ident)),*) => { + $(#[$meta])* + impl<$($T: RenderLabel),*> IntoRenderNodeArray<$N> for ($($T,)*) { + #[inline] + fn into_array(self) -> [InternedRenderLabel; $N] { + let ($($I,)*) = self; + [$($I.intern(), )*] + } + } + } +} + +all_tuples_with_size!( + #[doc(fake_variadic)] + impl_render_label_tuples, + 1, + 32, + T, + l +); + +/// A render node that can be added to a [`RenderGraph`](super::RenderGraph). +/// +/// Nodes are the fundamental part of the graph and used to extend its functionality, by +/// generating draw calls and/or running subgraphs. +/// They are added via the `render_graph::add_node(my_node)` method. +/// +/// To determine their position in the graph and ensure that all required dependencies (inputs) +/// are already executed, [`Edges`](Edge) are used. +/// +/// A node can produce outputs used as dependencies by other nodes. +/// Those inputs and outputs are called slots and are the default way of passing render data +/// inside the graph. For more information see [`SlotType`](super::SlotType). +pub trait Node: Downcast + Send + Sync + 'static { + /// Specifies the required input slots for this node. + /// They will then be available during the run method inside the [`RenderGraphContext`]. + fn input(&self) -> Vec { + Vec::new() + } + + /// Specifies the produced output slots for this node. + /// They can then be passed one inside [`RenderGraphContext`] during the run method. + fn output(&self) -> Vec { + Vec::new() + } + + /// Updates internal node state using the current render [`World`] prior to the run method. + fn update(&mut self, _world: &mut World) {} + + /// Runs the graph node logic, issues draw calls, updates the output slots and + /// optionally queues up subgraphs for execution. The graph data, input and output values are + /// passed via the [`RenderGraphContext`]. + fn run<'w>( + &self, + graph: &mut RenderGraphContext, + render_context: &mut RenderContext<'w>, + world: &'w World, + ) -> Result<(), NodeRunError>; +} + +impl_downcast!(Node); + +#[derive(Error, Debug, Eq, PartialEq)] +pub enum NodeRunError { + #[error("encountered an input slot error")] + InputSlotError(#[from] InputSlotError), + #[error("encountered an output slot error")] + OutputSlotError(#[from] OutputSlotError), + #[error("encountered an error when running a sub-graph")] + RunSubGraphError(#[from] RunSubGraphError), + #[error("encountered an error when executing draw command")] + DrawError(#[from] DrawError), +} + +/// A collection of input and output [`Edges`](Edge) for a [`Node`]. +#[derive(Debug)] +pub struct Edges { + label: InternedRenderLabel, + input_edges: Vec, + output_edges: Vec, +} + +impl Edges { + /// Returns all "input edges" (edges going "in") for this node . + #[inline] + pub fn input_edges(&self) -> &[Edge] { + &self.input_edges + } + + /// Returns all "output edges" (edges going "out") for this node . + #[inline] + pub fn output_edges(&self) -> &[Edge] { + &self.output_edges + } + + /// Returns this node's label. + #[inline] + pub fn label(&self) -> InternedRenderLabel { + self.label + } + + /// Adds an edge to the `input_edges` if it does not already exist. + pub(crate) fn add_input_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { + if self.has_input_edge(&edge) { + return Err(RenderGraphError::EdgeAlreadyExists(edge)); + } + self.input_edges.push(edge); + Ok(()) + } + + /// Removes an edge from the `input_edges` if it exists. + pub(crate) fn remove_input_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { + if let Some(index) = self.input_edges.iter().position(|e| *e == edge) { + self.input_edges.swap_remove(index); + Ok(()) + } else { + Err(RenderGraphError::EdgeDoesNotExist(edge)) + } + } + + /// Adds an edge to the `output_edges` if it does not already exist. + pub(crate) fn add_output_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { + if self.has_output_edge(&edge) { + return Err(RenderGraphError::EdgeAlreadyExists(edge)); + } + self.output_edges.push(edge); + Ok(()) + } + + /// Removes an edge from the `output_edges` if it exists. + pub(crate) fn remove_output_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { + if let Some(index) = self.output_edges.iter().position(|e| *e == edge) { + self.output_edges.swap_remove(index); + Ok(()) + } else { + Err(RenderGraphError::EdgeDoesNotExist(edge)) + } + } + + /// Checks whether the input edge already exists. + pub fn has_input_edge(&self, edge: &Edge) -> bool { + self.input_edges.contains(edge) + } + + /// Checks whether the output edge already exists. + pub fn has_output_edge(&self, edge: &Edge) -> bool { + self.output_edges.contains(edge) + } + + /// Searches the `input_edges` for a [`Edge::SlotEdge`], + /// which `input_index` matches the `index`; + pub fn get_input_slot_edge(&self, index: usize) -> Result<&Edge, RenderGraphError> { + self.input_edges + .iter() + .find(|e| { + if let Edge::SlotEdge { input_index, .. } = e { + *input_index == index + } else { + false + } + }) + .ok_or(RenderGraphError::UnconnectedNodeInputSlot { + input_slot: index, + node: self.label, + }) + } + + /// Searches the `output_edges` for a [`Edge::SlotEdge`], + /// which `output_index` matches the `index`; + pub fn get_output_slot_edge(&self, index: usize) -> Result<&Edge, RenderGraphError> { + self.output_edges + .iter() + .find(|e| { + if let Edge::SlotEdge { output_index, .. } = e { + *output_index == index + } else { + false + } + }) + .ok_or(RenderGraphError::UnconnectedNodeOutputSlot { + output_slot: index, + node: self.label, + }) + } +} + +/// The internal representation of a [`Node`], with all data required +/// by the [`RenderGraph`](super::RenderGraph). +/// +/// The `input_slots` and `output_slots` are provided by the `node`. +pub struct NodeState { + pub label: InternedRenderLabel, + /// The name of the type that implements [`Node`]. + pub type_name: &'static str, + pub node: Box, + pub input_slots: SlotInfos, + pub output_slots: SlotInfos, + pub edges: Edges, +} + +impl Debug for NodeState { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + writeln!(f, "{:?} ({})", self.label, self.type_name) + } +} + +impl NodeState { + /// Creates an [`NodeState`] without edges, but the `input_slots` and `output_slots` + /// are provided by the `node`. + pub fn new(label: InternedRenderLabel, node: T) -> Self + where + T: Node, + { + NodeState { + label, + input_slots: node.input().into(), + output_slots: node.output().into(), + node: Box::new(node), + type_name: core::any::type_name::(), + edges: Edges { + label, + input_edges: Vec::new(), + output_edges: Vec::new(), + }, + } + } + + /// Retrieves the [`Node`]. + pub fn node(&self) -> Result<&T, RenderGraphError> + where + T: Node, + { + self.node + .downcast_ref::() + .ok_or(RenderGraphError::WrongNodeType) + } + + /// Retrieves the [`Node`] mutably. + pub fn node_mut(&mut self) -> Result<&mut T, RenderGraphError> + where + T: Node, + { + self.node + .downcast_mut::() + .ok_or(RenderGraphError::WrongNodeType) + } + + /// Validates that each input slot corresponds to an input edge. + pub fn validate_input_slots(&self) -> Result<(), RenderGraphError> { + for i in 0..self.input_slots.len() { + self.edges.get_input_slot_edge(i)?; + } + + Ok(()) + } + + /// Validates that each output slot corresponds to an output edge. + pub fn validate_output_slots(&self) -> Result<(), RenderGraphError> { + for i in 0..self.output_slots.len() { + self.edges.get_output_slot_edge(i)?; + } + + Ok(()) + } +} + +/// A [`Node`] without any inputs, outputs and subgraphs, which does nothing when run. +/// Used (as a label) to bundle multiple dependencies into one inside +/// the [`RenderGraph`](super::RenderGraph). +#[derive(Default)] +pub struct EmptyNode; + +impl Node for EmptyNode { + fn run( + &self, + _graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + _world: &World, + ) -> Result<(), NodeRunError> { + Ok(()) + } +} + +/// A [`RenderGraph`](super::RenderGraph) [`Node`] that runs the configured subgraph once. +/// This makes it easier to insert sub-graph runs into a graph. +pub struct RunGraphOnViewNode { + sub_graph: InternedRenderSubGraph, +} + +impl RunGraphOnViewNode { + pub fn new(sub_graph: T) -> Self { + Self { + sub_graph: sub_graph.intern(), + } + } +} + +impl Node for RunGraphOnViewNode { + fn run( + &self, + graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + _world: &World, + ) -> Result<(), NodeRunError> { + graph.run_sub_graph(self.sub_graph, vec![], Some(graph.view_entity()), None)?; + Ok(()) + } +} + +/// This trait should be used instead of the [`Node`] trait when making a render node that runs on a view. +/// +/// It is intended to be used with [`ViewNodeRunner`] +pub trait ViewNode { + /// The query that will be used on the view entity. + /// It is guaranteed to run on the view entity, so there's no need for a filter + type ViewQuery: ReadOnlyQueryData; + + /// Updates internal node state using the current render [`World`] prior to the run method. + fn update(&mut self, _world: &mut World) {} + + /// Runs the graph node logic, issues draw calls, updates the output slots and + /// optionally queues up subgraphs for execution. The graph data, input and output values are + /// passed via the [`RenderGraphContext`]. + fn run<'w>( + &self, + graph: &mut RenderGraphContext, + render_context: &mut RenderContext<'w>, + view_query: QueryItem<'w, '_, Self::ViewQuery>, + world: &'w World, + ) -> Result<(), NodeRunError>; +} + +/// This [`Node`] can be used to run any [`ViewNode`]. +/// It will take care of updating the view query in `update()` and running the query in `run()`. +/// +/// This [`Node`] exists to help reduce boilerplate when making a render node that runs on a view. +pub struct ViewNodeRunner { + view_query: QueryState, + node: N, +} + +impl ViewNodeRunner { + pub fn new(node: N, world: &mut World) -> Self { + Self { + view_query: world.query_filtered(), + node, + } + } +} + +impl FromWorld for ViewNodeRunner { + fn from_world(world: &mut World) -> Self { + Self::new(N::from_world(world), world) + } +} + +impl Node for ViewNodeRunner +where + T: ViewNode + Send + Sync + 'static, +{ + fn update(&mut self, world: &mut World) { + self.view_query.update_archetypes(world); + self.node.update(world); + } + + fn run<'w>( + &self, + graph: &mut RenderGraphContext, + render_context: &mut RenderContext<'w>, + world: &'w World, + ) -> Result<(), NodeRunError> { + let Ok(view) = self.view_query.get_manual(world, graph.view_entity()) else { + return Ok(()); + }; + + ViewNode::run(&self.node, graph, render_context, view, world)?; + Ok(()) + } +} diff --git a/third_party/bevy_render/src/render_graph/node_slot.rs b/third_party/bevy_render/src/render_graph/node_slot.rs new file mode 100644 index 0000000..eb11b36 --- /dev/null +++ b/third_party/bevy_render/src/render_graph/node_slot.rs @@ -0,0 +1,165 @@ +use alloc::borrow::Cow; +use bevy_ecs::entity::Entity; +use core::fmt; +use derive_more::derive::From; + +use crate::render_resource::{Buffer, Sampler, TextureView}; + +/// A value passed between render [`Nodes`](super::Node). +/// Corresponds to the [`SlotType`] specified in the [`RenderGraph`](super::RenderGraph). +/// +/// Slots can have four different types of values: +/// [`Buffer`], [`TextureView`], [`Sampler`] and [`Entity`]. +/// +/// These values do not contain the actual render data, but only the ids to retrieve them. +#[derive(Debug, Clone, From)] +pub enum SlotValue { + /// A GPU-accessible [`Buffer`]. + Buffer(Buffer), + /// A [`TextureView`] describes a texture used in a pipeline. + TextureView(TextureView), + /// A texture [`Sampler`] defines how a pipeline will sample from a [`TextureView`]. + Sampler(Sampler), + /// An entity from the ECS. + Entity(Entity), +} + +impl SlotValue { + /// Returns the [`SlotType`] of this value. + pub fn slot_type(&self) -> SlotType { + match self { + SlotValue::Buffer(_) => SlotType::Buffer, + SlotValue::TextureView(_) => SlotType::TextureView, + SlotValue::Sampler(_) => SlotType::Sampler, + SlotValue::Entity(_) => SlotType::Entity, + } + } +} + +/// Describes the render resources created (output) or used (input) by +/// the render [`Nodes`](super::Node). +/// +/// This should not be confused with [`SlotValue`], which actually contains the passed data. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum SlotType { + /// A GPU-accessible [`Buffer`]. + Buffer, + /// A [`TextureView`] describes a texture used in a pipeline. + TextureView, + /// A texture [`Sampler`] defines how a pipeline will sample from a [`TextureView`]. + Sampler, + /// An entity from the ECS. + Entity, +} + +impl fmt::Display for SlotType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s = match self { + SlotType::Buffer => "Buffer", + SlotType::TextureView => "TextureView", + SlotType::Sampler => "Sampler", + SlotType::Entity => "Entity", + }; + + f.write_str(s) + } +} + +/// A [`SlotLabel`] is used to reference a slot by either its name or index +/// inside the [`RenderGraph`](super::RenderGraph). +#[derive(Debug, Clone, Eq, PartialEq, From)] +pub enum SlotLabel { + Index(usize), + Name(Cow<'static, str>), +} + +impl From<&SlotLabel> for SlotLabel { + fn from(value: &SlotLabel) -> Self { + value.clone() + } +} + +impl From for SlotLabel { + fn from(value: String) -> Self { + SlotLabel::Name(value.into()) + } +} + +impl From<&'static str> for SlotLabel { + fn from(value: &'static str) -> Self { + SlotLabel::Name(value.into()) + } +} + +/// The internal representation of a slot, which specifies its [`SlotType`] and name. +#[derive(Clone, Debug)] +pub struct SlotInfo { + pub name: Cow<'static, str>, + pub slot_type: SlotType, +} + +impl SlotInfo { + pub fn new(name: impl Into>, slot_type: SlotType) -> Self { + SlotInfo { + name: name.into(), + slot_type, + } + } +} + +/// A collection of input or output [`SlotInfos`](SlotInfo) for +/// a [`NodeState`](super::NodeState). +#[derive(Default, Debug)] +pub struct SlotInfos { + slots: Vec, +} + +impl> From for SlotInfos { + fn from(slots: T) -> Self { + SlotInfos { + slots: slots.into_iter().collect(), + } + } +} + +impl SlotInfos { + /// Returns the count of slots. + #[inline] + pub fn len(&self) -> usize { + self.slots.len() + } + + /// Returns true if there are no slots. + #[inline] + pub fn is_empty(&self) -> bool { + self.slots.is_empty() + } + + /// Retrieves the [`SlotInfo`] for the provided label. + pub fn get_slot(&self, label: impl Into) -> Option<&SlotInfo> { + let label = label.into(); + let index = self.get_slot_index(label)?; + self.slots.get(index) + } + + /// Retrieves the [`SlotInfo`] for the provided label mutably. + pub fn get_slot_mut(&mut self, label: impl Into) -> Option<&mut SlotInfo> { + let label = label.into(); + let index = self.get_slot_index(label)?; + self.slots.get_mut(index) + } + + /// Retrieves the index (inside input or output slots) of the slot for the provided label. + pub fn get_slot_index(&self, label: impl Into) -> Option { + let label = label.into(); + match label { + SlotLabel::Index(index) => Some(index), + SlotLabel::Name(ref name) => self.slots.iter().position(|s| s.name == *name), + } + } + + /// Returns an iterator over the slot infos. + pub fn iter(&self) -> impl Iterator { + self.slots.iter() + } +} diff --git a/third_party/bevy_render/src/render_phase/draw.rs b/third_party/bevy_render/src/render_phase/draw.rs new file mode 100644 index 0000000..494074d --- /dev/null +++ b/third_party/bevy_render/src/render_phase/draw.rs @@ -0,0 +1,396 @@ +use crate::render_phase::{PhaseItem, TrackedRenderPass}; +use bevy_app::{App, SubApp}; +use bevy_ecs::{ + entity::Entity, + query::{QueryEntityError, QueryState, ROQueryItem, ReadOnlyQueryData}, + resource::Resource, + system::{ReadOnlySystemParam, SystemParam, SystemParamItem, SystemState}, + world::World, +}; +use bevy_utils::TypeIdMap; +use core::{any::TypeId, fmt::Debug, hash::Hash}; +use std::sync::{PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}; +use thiserror::Error; +use variadics_please::all_tuples; + +/// A draw function used to draw [`PhaseItem`]s. +/// +/// The draw function can retrieve and query the required ECS data from the render world. +/// +/// This trait can either be implemented directly or implicitly composed out of multiple modular +/// [`RenderCommand`]s. For more details and an example see the [`RenderCommand`] documentation. +pub trait Draw: Send + Sync + 'static { + /// Prepares the draw function to be used. This is called once and only once before the phase + /// begins. There may be zero or more [`draw`](Draw::draw) calls following a call to this function. + /// Implementing this is optional. + #[expect( + unused_variables, + reason = "The parameters here are intentionally unused by the default implementation; however, putting underscores here will result in the underscores being copied by rust-analyzer's tab completion." + )] + fn prepare(&mut self, world: &'_ World) {} + + /// Draws a [`PhaseItem`] by issuing zero or more `draw` calls via the [`TrackedRenderPass`]. + fn draw<'w>( + &mut self, + world: &'w World, + pass: &mut TrackedRenderPass<'w>, + view: Entity, + item: &P, + ) -> Result<(), DrawError>; +} + +#[derive(Error, Debug, PartialEq, Eq)] +pub enum DrawError { + #[error("Failed to execute render command {0:?}")] + RenderCommandFailure(&'static str), + #[error("Failed to get execute view query")] + InvalidViewQuery, + #[error("View entity not found")] + ViewEntityNotFound, +} + +// TODO: make this generic? +/// An identifier for a [`Draw`] function stored in [`DrawFunctions`]. +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] +pub struct DrawFunctionId(u32); + +/// Stores all [`Draw`] functions for the [`PhaseItem`] type. +/// +/// For retrieval, the [`Draw`] functions are mapped to their respective [`TypeId`]s. +pub struct DrawFunctionsInternal { + pub draw_functions: Vec>>, + pub indices: TypeIdMap, +} + +impl DrawFunctionsInternal

{ + /// Prepares all draw function. This is called once and only once before the phase begins. + pub fn prepare(&mut self, world: &World) { + for function in &mut self.draw_functions { + function.prepare(world); + } + } + + /// Adds the [`Draw`] function and maps it to its own type. + pub fn add>(&mut self, draw_function: T) -> DrawFunctionId { + self.add_with::(draw_function) + } + + /// Adds the [`Draw`] function and maps it to the type `T` + pub fn add_with>(&mut self, draw_function: D) -> DrawFunctionId { + let id = DrawFunctionId(self.draw_functions.len().try_into().unwrap()); + self.draw_functions.push(Box::new(draw_function)); + self.indices.insert(TypeId::of::(), id); + id + } + + /// Retrieves the [`Draw`] function corresponding to the `id` mutably. + pub fn get_mut(&mut self, id: DrawFunctionId) -> Option<&mut dyn Draw

> { + self.draw_functions.get_mut(id.0 as usize).map(|f| &mut **f) + } + + /// Retrieves the id of the [`Draw`] function corresponding to their associated type `T`. + pub fn get_id(&self) -> Option { + self.indices.get(&TypeId::of::()).copied() + } + + /// Retrieves the id of the [`Draw`] function corresponding to their associated type `T`. + /// + /// Fallible wrapper for [`Self::get_id()`] + /// + /// ## Panics + /// If the id doesn't exist, this function will panic. + pub fn id(&self) -> DrawFunctionId { + self.get_id::().unwrap_or_else(|| { + panic!( + "Draw function {} not found for {}", + core::any::type_name::(), + core::any::type_name::

() + ) + }) + } +} + +/// Stores all draw functions for the [`PhaseItem`] type hidden behind a reader-writer lock. +/// +/// To access them the [`DrawFunctions::read`] and [`DrawFunctions::write`] methods are used. +#[derive(Resource)] +pub struct DrawFunctions { + internal: RwLock>, +} + +impl Default for DrawFunctions

{ + fn default() -> Self { + Self { + internal: RwLock::new(DrawFunctionsInternal { + draw_functions: Vec::new(), + indices: Default::default(), + }), + } + } +} + +impl DrawFunctions

{ + /// Accesses the draw functions in read mode. + pub fn read(&self) -> RwLockReadGuard<'_, DrawFunctionsInternal

> { + self.internal.read().unwrap_or_else(PoisonError::into_inner) + } + + /// Accesses the draw functions in write mode. + pub fn write(&self) -> RwLockWriteGuard<'_, DrawFunctionsInternal

> { + self.internal + .write() + .unwrap_or_else(PoisonError::into_inner) + } +} + +/// [`RenderCommand`]s are modular standardized pieces of render logic that can be composed into +/// [`Draw`] functions. +/// +/// To turn a stateless render command into a usable draw function it has to be wrapped by a +/// [`RenderCommandState`]. +/// This is done automatically when registering a render command as a [`Draw`] function via the +/// [`AddRenderCommand::add_render_command`] method. +/// +/// Compared to the draw function the required ECS data is fetched automatically +/// (by the [`RenderCommandState`]) from the render world. +/// Therefore the three types [`Param`](RenderCommand::Param), +/// [`ViewQuery`](RenderCommand::ViewQuery) and +/// [`ItemQuery`](RenderCommand::ItemQuery) are used. +/// They specify which information is required to execute the render command. +/// +/// Multiple render commands can be combined together by wrapping them in a tuple. +/// +/// # Example +/// +/// The `DrawMaterial` draw function is created from the following render command +/// tuple. Const generics are used to set specific bind group locations: +/// +/// ``` +/// # use bevy_render::render_phase::SetItemPipeline; +/// # struct SetMeshViewBindGroup; +/// # struct SetMeshViewBindingArrayBindGroup; +/// # struct SetMeshBindGroup; +/// # struct SetMaterialBindGroup(std::marker::PhantomData); +/// # struct DrawMesh; +/// pub type DrawMaterial = ( +/// SetItemPipeline, +/// SetMeshViewBindGroup<0>, +/// SetMeshViewBindingArrayBindGroup<1>, +/// SetMeshBindGroup<2>, +/// SetMaterialBindGroup, +/// DrawMesh, +/// ); +/// ``` +pub trait RenderCommand { + /// Specifies the general ECS data (e.g. resources) required by [`RenderCommand::render`]. + /// + /// When fetching resources, note that, due to lifetime limitations of the `Deref` trait, + /// [`SRes::into_inner`] must be called on each [`SRes`] reference in the + /// [`RenderCommand::render`] method, instead of being automatically dereferenced as is the + /// case in normal `systems`. + /// + /// All parameters have to be read only. + /// + /// [`SRes`]: bevy_ecs::system::lifetimeless::SRes + /// [`SRes::into_inner`]: bevy_ecs::system::lifetimeless::SRes::into_inner + type Param: SystemParam + 'static; + /// Specifies the ECS data of the view entity required by [`RenderCommand::render`]. + /// + /// The view entity refers to the camera, or shadow-casting light, etc. from which the phase + /// item will be rendered from. + /// All components have to be accessed read only. + type ViewQuery: ReadOnlyQueryData; + /// Specifies the ECS data of the item entity required by [`RenderCommand::render`]. + /// + /// The item is the entity that will be rendered for the corresponding view. + /// All components have to be accessed read only. + /// + /// For efficiency reasons, Bevy doesn't always extract entities to the + /// render world; for instance, entities that simply consist of meshes are + /// often not extracted. If the entity doesn't exist in the render world, + /// the supplied query data will be `None`. + type ItemQuery: ReadOnlyQueryData; + + /// Renders a [`PhaseItem`] by recording commands (e.g. setting pipelines, binding bind groups, + /// issuing draw calls, etc.) via the [`TrackedRenderPass`]. + fn render<'w>( + item: &P, + view: ROQueryItem<'w, '_, Self::ViewQuery>, + entity: Option>, + param: SystemParamItem<'w, '_, Self::Param>, + pass: &mut TrackedRenderPass<'w>, + ) -> RenderCommandResult; +} + +/// The result of a [`RenderCommand`]. +#[derive(Debug)] +pub enum RenderCommandResult { + Success, + Skip, + Failure(&'static str), +} + +macro_rules! render_command_tuple_impl { + ($(#[$meta:meta])* $(($name: ident, $view: ident, $entity: ident)),*) => { + $(#[$meta])* + impl),*> RenderCommand

for ($($name,)*) { + type Param = ($($name::Param,)*); + type ViewQuery = ($($name::ViewQuery,)*); + type ItemQuery = ($($name::ItemQuery,)*); + + #[expect( + clippy::allow_attributes, + reason = "We are in a macro; as such, `non_snake_case` may not always lint." + )] + #[allow( + non_snake_case, + reason = "Parameter and variable names are provided by the macro invocation, not by us." + )] + fn render<'w>( + _item: &P, + ($($view,)*): ROQueryItem<'w, '_, Self::ViewQuery>, + maybe_entities: Option>, + ($($name,)*): SystemParamItem<'w, '_, Self::Param>, + _pass: &mut TrackedRenderPass<'w>, + ) -> RenderCommandResult { + match maybe_entities { + None => { + $( + match $name::render(_item, $view, None, $name, _pass) { + RenderCommandResult::Skip => return RenderCommandResult::Skip, + RenderCommandResult::Failure(reason) => return RenderCommandResult::Failure(reason), + _ => {}, + } + )* + } + Some(($($entity,)*)) => { + $( + match $name::render(_item, $view, Some($entity), $name, _pass) { + RenderCommandResult::Skip => return RenderCommandResult::Skip, + RenderCommandResult::Failure(reason) => return RenderCommandResult::Failure(reason), + _ => {}, + } + )* + } + } + RenderCommandResult::Success + } + } + }; +} + +all_tuples!( + #[doc(fake_variadic)] + render_command_tuple_impl, + 0, + 15, + C, + V, + E +); + +/// Wraps a [`RenderCommand`] into a state so that it can be used as a [`Draw`] function. +/// +/// The [`RenderCommand::Param`], [`RenderCommand::ViewQuery`] and +/// [`RenderCommand::ItemQuery`] are fetched from the ECS and passed to the command. +pub struct RenderCommandState> { + state: SystemState, + view: QueryState, + entity: QueryState, +} + +impl> RenderCommandState { + /// Creates a new [`RenderCommandState`] for the [`RenderCommand`]. + pub fn new(world: &mut World) -> Self { + Self { + state: SystemState::new(world), + view: world.query(), + entity: world.query(), + } + } +} + +impl + Send + Sync + 'static> Draw

for RenderCommandState +where + C::Param: ReadOnlySystemParam, +{ + /// Prepares the render command to be used. This is called once and only once before the phase + /// begins. There may be zero or more [`draw`](RenderCommandState::draw) calls following a call to this function. + fn prepare(&mut self, world: &'_ World) { + self.view.update_archetypes(world); + self.entity.update_archetypes(world); + } + + /// Fetches the ECS parameters for the wrapped [`RenderCommand`] and then renders it. + fn draw<'w>( + &mut self, + world: &'w World, + pass: &mut TrackedRenderPass<'w>, + view: Entity, + item: &P, + ) -> Result<(), DrawError> { + let param = self.state.get(world); + let view = match self.view.get_manual(world, view) { + Ok(view) => view, + Err(err) => match err { + QueryEntityError::NotSpawned(_) => return Err(DrawError::ViewEntityNotFound), + QueryEntityError::QueryDoesNotMatch(_, _) + | QueryEntityError::AliasedMutability(_) => { + return Err(DrawError::InvalidViewQuery) + } + }, + }; + + let entity = self.entity.get_manual(world, item.entity()).ok(); + match C::render(item, view, entity, param, pass) { + RenderCommandResult::Success | RenderCommandResult::Skip => Ok(()), + RenderCommandResult::Failure(reason) => Err(DrawError::RenderCommandFailure(reason)), + } + } +} + +/// Registers a [`RenderCommand`] as a [`Draw`] function. +/// They are stored inside the [`DrawFunctions`] resource of the app. +pub trait AddRenderCommand { + /// Adds the [`RenderCommand`] for the specified render phase to the app. + fn add_render_command + Send + Sync + 'static>( + &mut self, + ) -> &mut Self + where + C::Param: ReadOnlySystemParam; +} + +impl AddRenderCommand for SubApp { + fn add_render_command + Send + Sync + 'static>( + &mut self, + ) -> &mut Self + where + C::Param: ReadOnlySystemParam, + { + let draw_function = RenderCommandState::::new(self.world_mut()); + let draw_functions = self + .world() + .get_resource::>() + .unwrap_or_else(|| { + panic!( + "DrawFunctions<{}> must be added to the world as a resource \ + before adding render commands to it", + core::any::type_name::

(), + ); + }); + draw_functions.write().add_with::(draw_function); + self + } +} + +impl AddRenderCommand for App { + fn add_render_command + Send + Sync + 'static>( + &mut self, + ) -> &mut Self + where + C::Param: ReadOnlySystemParam, + { + SubApp::add_render_command::(self.main_mut()); + self + } +} diff --git a/third_party/bevy_render/src/render_phase/draw_state.rs b/third_party/bevy_render/src/render_phase/draw_state.rs new file mode 100644 index 0000000..167d493 --- /dev/null +++ b/third_party/bevy_render/src/render_phase/draw_state.rs @@ -0,0 +1,671 @@ +use crate::{ + diagnostic::internal::{Pass, PassKind, WritePipelineStatistics, WriteTimestamp}, + render_resource::{ + BindGroup, BindGroupId, Buffer, BufferId, BufferSlice, RenderPipeline, RenderPipelineId, + ShaderStages, + }, + renderer::RenderDevice, +}; +use bevy_camera::Viewport; +use bevy_color::LinearRgba; +use bevy_utils::default; +use core::ops::Range; +use wgpu::{IndexFormat, QuerySet, RenderPass}; + +#[cfg(feature = "detailed_trace")] +use tracing::trace; + +type BufferSliceKey = (BufferId, wgpu::BufferAddress, wgpu::BufferSize); + +/// Tracks the state of a [`TrackedRenderPass`]. +/// +/// This is used to skip redundant operations on the [`TrackedRenderPass`] (e.g. setting an already +/// set pipeline, binding an already bound bind group). These operations can otherwise be fairly +/// costly due to IO to the GPU, so deduplicating these calls results in a speedup. +#[derive(Debug, Default)] +struct DrawState { + pipeline: Option, + bind_groups: Vec<(Option, Vec)>, + /// List of vertex buffers by [`BufferId`], offset, and size. See [`DrawState::buffer_slice_key`] + vertex_buffers: Vec>, + index_buffer: Option<(BufferSliceKey, IndexFormat)>, + + /// Stores whether this state is populated or empty for quick state invalidation + stores_state: bool, +} + +impl DrawState { + /// Marks the `pipeline` as bound. + fn set_pipeline(&mut self, pipeline: RenderPipelineId) { + // TODO: do these need to be cleared? + // self.bind_groups.clear(); + // self.vertex_buffers.clear(); + // self.index_buffer = None; + self.pipeline = Some(pipeline); + self.stores_state = true; + } + + /// Checks, whether the `pipeline` is already bound. + fn is_pipeline_set(&self, pipeline: RenderPipelineId) -> bool { + self.pipeline == Some(pipeline) + } + + /// Marks the `bind_group` as bound to the `index`. + fn set_bind_group(&mut self, index: usize, bind_group: BindGroupId, dynamic_indices: &[u32]) { + let group = &mut self.bind_groups[index]; + group.0 = Some(bind_group); + group.1.clear(); + group.1.extend(dynamic_indices); + self.stores_state = true; + } + + /// Checks, whether the `bind_group` is already bound to the `index`. + fn is_bind_group_set( + &self, + index: usize, + bind_group: BindGroupId, + dynamic_indices: &[u32], + ) -> bool { + if let Some(current_bind_group) = self.bind_groups.get(index) { + current_bind_group.0 == Some(bind_group) && dynamic_indices == current_bind_group.1 + } else { + false + } + } + + /// Marks the vertex `buffer` as bound to the `index`. + fn set_vertex_buffer(&mut self, index: usize, buffer_slice: BufferSlice) { + self.vertex_buffers[index] = Some(self.buffer_slice_key(&buffer_slice)); + self.stores_state = true; + } + + /// Checks, whether the vertex `buffer` is already bound to the `index`. + fn is_vertex_buffer_set(&self, index: usize, buffer_slice: &BufferSlice) -> bool { + if let Some(current) = self.vertex_buffers.get(index) { + *current == Some(self.buffer_slice_key(buffer_slice)) + } else { + false + } + } + + /// Returns the value used for checking whether `BufferSlice`s are equivalent. + fn buffer_slice_key(&self, buffer_slice: &BufferSlice) -> BufferSliceKey { + ( + buffer_slice.id(), + buffer_slice.offset(), + buffer_slice.size(), + ) + } + + /// Marks the index `buffer` as bound. + fn set_index_buffer(&mut self, buffer_slice: &BufferSlice, index_format: IndexFormat) { + self.index_buffer = Some((self.buffer_slice_key(buffer_slice), index_format)); + self.stores_state = true; + } + + /// Checks, whether the index `buffer` is already bound. + fn is_index_buffer_set(&self, buffer: &BufferSlice, index_format: IndexFormat) -> bool { + self.index_buffer == Some((self.buffer_slice_key(buffer), index_format)) + } + + /// Resets tracking state + pub fn reset_tracking(&mut self) { + if !self.stores_state { + return; + } + self.pipeline = None; + self.bind_groups.iter_mut().for_each(|val| { + val.0 = None; + val.1.clear(); + }); + self.vertex_buffers.iter_mut().for_each(|val| { + *val = None; + }); + self.index_buffer = None; + self.stores_state = false; + } +} + +/// A [`RenderPass`], which tracks the current pipeline state to skip redundant operations. +/// +/// It is used to set the current [`RenderPipeline`], [`BindGroup`]s and [`Buffer`]s. +/// After all requirements are specified, draw calls can be issued. +pub struct TrackedRenderPass<'a> { + pass: RenderPass<'a>, + state: DrawState, +} + +impl<'a> TrackedRenderPass<'a> { + /// Tracks the supplied render pass. + pub fn new(device: &RenderDevice, pass: RenderPass<'a>) -> Self { + let limits = device.limits(); + let max_bind_groups = limits.max_bind_groups as usize; + let max_vertex_buffers = limits.max_vertex_buffers as usize; + Self { + state: DrawState { + bind_groups: vec![(None, Vec::new()); max_bind_groups], + vertex_buffers: vec![None; max_vertex_buffers], + ..default() + }, + pass, + } + } + + /// Returns the wgpu [`RenderPass`]. + /// + /// Function invalidates internal tracking state, + /// some redundant pipeline operations may not be skipped. + pub fn wgpu_pass(&mut self) -> &mut RenderPass<'a> { + self.state.reset_tracking(); + &mut self.pass + } + + /// Sets the active [`RenderPipeline`]. + /// + /// Subsequent draw calls will exhibit the behavior defined by the `pipeline`. + pub fn set_render_pipeline(&mut self, pipeline: &'a RenderPipeline) { + #[cfg(feature = "detailed_trace")] + trace!("set pipeline: {:?}", pipeline); + if self.state.is_pipeline_set(pipeline.id()) { + return; + } + self.pass.set_pipeline(pipeline); + self.state.set_pipeline(pipeline.id()); + } + + /// Sets the active bind group for a given bind group index. The bind group layout + /// in the active pipeline when any `draw()` function is called must match the layout of + /// this bind group. + /// + /// If the bind group have dynamic offsets, provide them in binding order. + /// These offsets have to be aligned to [`WgpuLimits::min_uniform_buffer_offset_alignment`](crate::settings::WgpuLimits::min_uniform_buffer_offset_alignment) + /// or [`WgpuLimits::min_storage_buffer_offset_alignment`](crate::settings::WgpuLimits::min_storage_buffer_offset_alignment) appropriately. + pub fn set_bind_group( + &mut self, + index: usize, + bind_group: &'a BindGroup, + dynamic_uniform_indices: &[u32], + ) { + if self + .state + .is_bind_group_set(index, bind_group.id(), dynamic_uniform_indices) + { + #[cfg(feature = "detailed_trace")] + trace!( + "set bind_group {} (already set): {:?} ({:?})", + index, + bind_group, + dynamic_uniform_indices + ); + return; + } + #[cfg(feature = "detailed_trace")] + trace!( + "set bind_group {}: {:?} ({:?})", + index, + bind_group, + dynamic_uniform_indices + ); + + self.pass + .set_bind_group(index as u32, bind_group, dynamic_uniform_indices); + self.state + .set_bind_group(index, bind_group.id(), dynamic_uniform_indices); + } + + /// Assign a vertex buffer to a slot. + /// + /// Subsequent calls to [`draw`] and [`draw_indexed`] on this + /// [`TrackedRenderPass`] will use `buffer` as one of the source vertex buffers. + /// + /// The `slot_index` refers to the index of the matching descriptor in + /// [`VertexState::buffers`](crate::render_resource::VertexState::buffers). + /// + /// [`draw`]: TrackedRenderPass::draw + /// [`draw_indexed`]: TrackedRenderPass::draw_indexed + pub fn set_vertex_buffer(&mut self, slot_index: usize, buffer_slice: BufferSlice<'a>) { + if self.state.is_vertex_buffer_set(slot_index, &buffer_slice) { + #[cfg(feature = "detailed_trace")] + trace!( + "set vertex buffer {} (already set): {:?} (offset = {}, size = {})", + slot_index, + buffer_slice.id(), + buffer_slice.offset(), + buffer_slice.size(), + ); + return; + } + #[cfg(feature = "detailed_trace")] + trace!( + "set vertex buffer {}: {:?} (offset = {}, size = {})", + slot_index, + buffer_slice.id(), + buffer_slice.offset(), + buffer_slice.size(), + ); + + self.pass + .set_vertex_buffer(slot_index as u32, *buffer_slice); + self.state.set_vertex_buffer(slot_index, buffer_slice); + } + + /// Sets the active index buffer. + /// + /// Subsequent calls to [`TrackedRenderPass::draw_indexed`] will use the buffer referenced by + /// `buffer_slice` as the source index buffer. + pub fn set_index_buffer(&mut self, buffer_slice: BufferSlice<'a>, index_format: IndexFormat) { + let already_set = self.state.is_index_buffer_set(&buffer_slice, index_format); + #[cfg(feature = "detailed_trace")] + trace!( + "set index buffer{}: {:?} (offset = {}, size = {})", + if already_set { " (already set)" } else { "" }, + buffer_slice.id(), + buffer_slice.offset(), + buffer_slice.size(), + ); + if already_set { + return; + } + self.pass.set_index_buffer(*buffer_slice, index_format); + self.state.set_index_buffer(&buffer_slice, index_format); + } + + /// Draws primitives from the active vertex buffer(s). + /// + /// The active vertex buffer(s) can be set with [`TrackedRenderPass::set_vertex_buffer`]. + pub fn draw(&mut self, vertices: Range, instances: Range) { + #[cfg(feature = "detailed_trace")] + trace!("draw: {:?} {:?}", vertices, instances); + self.pass.draw(vertices, instances); + } + + /// Draws indexed primitives using the active index buffer and the active vertex buffer(s). + /// + /// The active index buffer can be set with [`TrackedRenderPass::set_index_buffer`], while the + /// active vertex buffer(s) can be set with [`TrackedRenderPass::set_vertex_buffer`]. + pub fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range) { + #[cfg(feature = "detailed_trace")] + trace!( + "draw indexed: {:?} {} {:?}", + indices, + base_vertex, + instances + ); + self.pass.draw_indexed(indices, base_vertex, instances); + } + + /// Draws primitives from the active vertex buffer(s) based on the contents of the + /// `indirect_buffer`. + /// + /// The active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// The structure expected in `indirect_buffer` is the following: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_vertex: u32, // The Index of the first vertex to draw. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: u64) { + #[cfg(feature = "detailed_trace")] + trace!("draw indirect: {:?} {}", indirect_buffer, indirect_offset); + self.pass.draw_indirect(indirect_buffer, indirect_offset); + } + + /// Draws indexed primitives using the active index buffer and the active vertex buffers, + /// based on the contents of the `indirect_buffer`. + /// + /// The active index buffer can be set with [`TrackedRenderPass::set_index_buffer`], while the + /// active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// The structure expected in `indirect_buffer` is the following: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndexedIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_index: u32, // The base index within the index buffer. + /// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn draw_indexed_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: u64) { + #[cfg(feature = "detailed_trace")] + trace!( + "draw indexed indirect: {:?} {}", + indirect_buffer, + indirect_offset + ); + self.pass + .draw_indexed_indirect(indirect_buffer, indirect_offset); + } + + /// Dispatches multiple draw calls from the active vertex buffer(s) based on the contents of the + /// `indirect_buffer`.`count` draw calls are issued. + /// + /// The active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// `indirect_buffer` should contain `count` tightly packed elements of the following structure: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_vertex: u32, // The Index of the first vertex to draw. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn multi_draw_indirect( + &mut self, + indirect_buffer: &'a Buffer, + indirect_offset: u64, + count: u32, + ) { + #[cfg(feature = "detailed_trace")] + trace!( + "multi draw indirect: {:?} {}, {}x", + indirect_buffer, + indirect_offset, + count + ); + self.pass + .multi_draw_indirect(indirect_buffer, indirect_offset, count); + } + + /// Dispatches multiple draw calls from the active vertex buffer(s) based on the contents of + /// the `indirect_buffer`. + /// The count buffer is read to determine how many draws to issue. + /// + /// The indirect buffer must be long enough to account for `max_count` draws, however only + /// `count` elements will be read, where `count` is the value read from `count_buffer` capped + /// at `max_count`. + /// + /// The active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// `indirect_buffer` should contain `count` tightly packed elements of the following structure: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_vertex: u32, // The Index of the first vertex to draw. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn multi_draw_indirect_count( + &mut self, + indirect_buffer: &'a Buffer, + indirect_offset: u64, + count_buffer: &'a Buffer, + count_offset: u64, + max_count: u32, + ) { + #[cfg(feature = "detailed_trace")] + trace!( + "multi draw indirect count: {:?} {}, ({:?} {})x, max {}x", + indirect_buffer, + indirect_offset, + count_buffer, + count_offset, + max_count + ); + self.pass.multi_draw_indirect_count( + indirect_buffer, + indirect_offset, + count_buffer, + count_offset, + max_count, + ); + } + + /// Dispatches multiple draw calls from the active index buffer and the active vertex buffers, + /// based on the contents of the `indirect_buffer`. `count` draw calls are issued. + /// + /// The active index buffer can be set with [`TrackedRenderPass::set_index_buffer`], while the + /// active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// `indirect_buffer` should contain `count` tightly packed elements of the following structure: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndexedIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_index: u32, // The base index within the index buffer. + /// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn multi_draw_indexed_indirect( + &mut self, + indirect_buffer: &'a Buffer, + indirect_offset: u64, + count: u32, + ) { + #[cfg(feature = "detailed_trace")] + trace!( + "multi draw indexed indirect: {:?} {}, {}x", + indirect_buffer, + indirect_offset, + count + ); + self.pass + .multi_draw_indexed_indirect(indirect_buffer, indirect_offset, count); + } + + /// Dispatches multiple draw calls from the active index buffer and the active vertex buffers, + /// based on the contents of the `indirect_buffer`. + /// The count buffer is read to determine how many draws to issue. + /// + /// The indirect buffer must be long enough to account for `max_count` draws, however only + /// `count` elements will be read, where `count` is the value read from `count_buffer` capped + /// at `max_count`. + /// + /// The active index buffer can be set with [`TrackedRenderPass::set_index_buffer`], while the + /// active vertex buffers can be set with [`TrackedRenderPass::set_vertex_buffer`]. + /// + /// `indirect_buffer` should contain `count` tightly packed elements of the following structure: + /// + /// ``` + /// #[repr(C)] + /// struct DrawIndexedIndirect { + /// vertex_count: u32, // The number of vertices to draw. + /// instance_count: u32, // The number of instances to draw. + /// first_index: u32, // The base index within the index buffer. + /// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer. + /// first_instance: u32, // The instance ID of the first instance to draw. + /// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled. + /// } + /// ``` + pub fn multi_draw_indexed_indirect_count( + &mut self, + indirect_buffer: &'a Buffer, + indirect_offset: u64, + count_buffer: &'a Buffer, + count_offset: u64, + max_count: u32, + ) { + #[cfg(feature = "detailed_trace")] + trace!( + "multi draw indexed indirect count: {:?} {}, ({:?} {})x, max {}x", + indirect_buffer, + indirect_offset, + count_buffer, + count_offset, + max_count + ); + self.pass.multi_draw_indexed_indirect_count( + indirect_buffer, + indirect_offset, + count_buffer, + count_offset, + max_count, + ); + } + + /// Sets the stencil reference. + /// + /// Subsequent stencil tests will test against this value. + pub fn set_stencil_reference(&mut self, reference: u32) { + #[cfg(feature = "detailed_trace")] + trace!("set stencil reference: {}", reference); + self.pass.set_stencil_reference(reference); + } + + /// Sets the scissor region. + /// + /// Subsequent draw calls will discard any fragments that fall outside this region. + pub fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) { + #[cfg(feature = "detailed_trace")] + trace!("set_scissor_rect: {} {} {} {}", x, y, width, height); + self.pass.set_scissor_rect(x, y, width, height); + } + + /// Set push constant data. + /// + /// `Features::PUSH_CONSTANTS` must be enabled on the device in order to call these functions. + pub fn set_push_constants(&mut self, stages: ShaderStages, offset: u32, data: &[u8]) { + #[cfg(feature = "detailed_trace")] + trace!( + "set push constants: {:?} offset: {} data.len: {}", + stages, + offset, + data.len() + ); + self.pass.set_push_constants(stages, offset, data); + } + + /// Set the rendering viewport. + /// + /// Subsequent draw calls will be projected into that viewport. + pub fn set_viewport( + &mut self, + x: f32, + y: f32, + width: f32, + height: f32, + min_depth: f32, + max_depth: f32, + ) { + #[cfg(feature = "detailed_trace")] + trace!( + "set viewport: {} {} {} {} {} {}", + x, + y, + width, + height, + min_depth, + max_depth + ); + self.pass + .set_viewport(x, y, width, height, min_depth, max_depth); + } + + /// Set the rendering viewport to the given camera [`Viewport`]. + /// + /// Subsequent draw calls will be projected into that viewport. + pub fn set_camera_viewport(&mut self, viewport: &Viewport) { + self.set_viewport( + viewport.physical_position.x as f32, + viewport.physical_position.y as f32, + viewport.physical_size.x as f32, + viewport.physical_size.y as f32, + viewport.depth.start, + viewport.depth.end, + ); + } + + /// Insert a single debug marker. + /// + /// This is a GPU debugging feature. This has no effect on the rendering itself. + pub fn insert_debug_marker(&mut self, label: &str) { + #[cfg(feature = "detailed_trace")] + trace!("insert debug marker: {}", label); + self.pass.insert_debug_marker(label); + } + + /// Start a new debug group. + /// + /// Push a new debug group over the internal stack. Subsequent render commands and debug + /// markers are grouped into this new group, until [`pop_debug_group`] is called. + /// + /// ``` + /// # fn example(mut pass: bevy_render::render_phase::TrackedRenderPass<'static>) { + /// pass.push_debug_group("Render the car"); + /// // [setup pipeline etc...] + /// pass.draw(0..64, 0..1); + /// pass.pop_debug_group(); + /// # } + /// ``` + /// + /// Note that [`push_debug_group`] and [`pop_debug_group`] must always be called in pairs. + /// + /// This is a GPU debugging feature. This has no effect on the rendering itself. + /// + /// [`push_debug_group`]: TrackedRenderPass::push_debug_group + /// [`pop_debug_group`]: TrackedRenderPass::pop_debug_group + pub fn push_debug_group(&mut self, label: &str) { + #[cfg(feature = "detailed_trace")] + trace!("push_debug_group marker: {}", label); + self.pass.push_debug_group(label); + } + + /// End the current debug group. + /// + /// Subsequent render commands and debug markers are not grouped anymore in + /// this group, but in the previous one (if any) or the default top-level one + /// if the debug group was the last one on the stack. + /// + /// Note that [`push_debug_group`] and [`pop_debug_group`] must always be called in pairs. + /// + /// This is a GPU debugging feature. This has no effect on the rendering itself. + /// + /// [`push_debug_group`]: TrackedRenderPass::push_debug_group + /// [`pop_debug_group`]: TrackedRenderPass::pop_debug_group + pub fn pop_debug_group(&mut self) { + #[cfg(feature = "detailed_trace")] + trace!("pop_debug_group"); + self.pass.pop_debug_group(); + } + + /// Sets the blend color as used by some of the blending modes. + /// + /// Subsequent blending tests will test against this value. + pub fn set_blend_constant(&mut self, color: LinearRgba) { + #[cfg(feature = "detailed_trace")] + trace!("set blend constant: {:?}", color); + self.pass.set_blend_constant(wgpu::Color::from(color)); + } +} + +impl WriteTimestamp for TrackedRenderPass<'_> { + fn write_timestamp(&mut self, query_set: &QuerySet, index: u32) { + self.pass.write_timestamp(query_set, index); + } +} + +impl WritePipelineStatistics for TrackedRenderPass<'_> { + fn begin_pipeline_statistics_query(&mut self, query_set: &QuerySet, index: u32) { + self.pass.begin_pipeline_statistics_query(query_set, index); + } + + fn end_pipeline_statistics_query(&mut self) { + self.pass.end_pipeline_statistics_query(); + } +} + +impl Pass for TrackedRenderPass<'_> { + const KIND: PassKind = PassKind::Render; +} diff --git a/third_party/bevy_render/src/render_phase/mod.rs b/third_party/bevy_render/src/render_phase/mod.rs new file mode 100644 index 0000000..ea9deb5 --- /dev/null +++ b/third_party/bevy_render/src/render_phase/mod.rs @@ -0,0 +1,1911 @@ +//! The modular rendering abstraction responsible for queuing, preparing, sorting and drawing +//! entities as part of separate render phases. +//! +//! In Bevy each view (camera, or shadow-casting light, etc.) has one or multiple render phases +//! (e.g. opaque, transparent, shadow, etc). +//! They are used to queue entities for rendering. +//! Multiple phases might be required due to different sorting/batching behaviors +//! (e.g. opaque: front to back, transparent: back to front) or because one phase depends on +//! the rendered texture of the previous phase (e.g. for screen-space reflections). +//! +//! To draw an entity, a corresponding [`PhaseItem`] has to be added to one or multiple of these +//! render phases for each view that it is visible in. +//! This must be done in the [`RenderSystems::Queue`]. +//! After that the render phase sorts them in the [`RenderSystems::PhaseSort`]. +//! Finally the items are rendered using a single [`TrackedRenderPass`], during +//! the [`RenderSystems::Render`]. +//! +//! Therefore each phase item is assigned a [`Draw`] function. +//! These set up the state of the [`TrackedRenderPass`] (i.e. select the +//! [`RenderPipeline`](crate::render_resource::RenderPipeline), configure the +//! [`BindGroup`](crate::render_resource::BindGroup)s, etc.) and then issue a draw call, +//! for the corresponding item. +//! +//! The [`Draw`] function trait can either be implemented directly or such a function can be +//! created by composing multiple [`RenderCommand`]s. + +mod draw; +mod draw_state; +mod rangefinder; + +use bevy_app::{App, Plugin}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::change_detection::Tick; +use bevy_ecs::entity::EntityHash; +use bevy_platform::collections::{hash_map::Entry, HashMap}; +use bevy_utils::default; +pub use draw::*; +pub use draw_state::*; +use encase::{internal::WriteInto, ShaderSize}; +use fixedbitset::{Block, FixedBitSet}; +use indexmap::IndexMap; +use nonmax::NonMaxU32; +pub use rangefinder::*; +use wgpu::Features; + +use crate::batching::gpu_preprocessing::{ + GpuPreprocessingMode, GpuPreprocessingSupport, PhaseBatchedInstanceBuffers, + PhaseIndirectParametersBuffers, +}; +use crate::renderer::RenderDevice; +use crate::sync_world::{MainEntity, MainEntityHashMap}; +use crate::view::RetainedViewEntity; +use crate::RenderDebugFlags; +use crate::{ + batching::{ + self, + gpu_preprocessing::{self, BatchedInstanceBuffers}, + no_gpu_preprocessing::{self, BatchedInstanceBuffer}, + GetFullBatchData, + }, + render_resource::{CachedRenderPipelineId, GpuArrayBufferIndex, PipelineCache}, + Render, RenderApp, RenderSystems, +}; +use bevy_ecs::intern::Interned; +use bevy_ecs::{ + define_label, + prelude::*, + system::{lifetimeless::SRes, SystemParamItem}, +}; +use bevy_render::renderer::RenderAdapterInfo; +pub use bevy_render_macros::ShaderLabel; +use core::{fmt::Debug, hash::Hash, iter, marker::PhantomData, ops::Range, slice::SliceIndex}; +use smallvec::SmallVec; +use tracing::warn; + +define_label!( + #[diagnostic::on_unimplemented( + note = "consider annotating `{Self}` with `#[derive(ShaderLabel)]`" + )] + /// Labels used to uniquely identify types of material shaders + ShaderLabel, + SHADER_LABEL_INTERNER +); + +/// A shorthand for `Interned`. +pub type InternedShaderLabel = Interned; + +pub use bevy_render_macros::DrawFunctionLabel; + +define_label!( + #[diagnostic::on_unimplemented( + note = "consider annotating `{Self}` with `#[derive(DrawFunctionLabel)]`" + )] + /// Labels used to uniquely identify types of material shaders + DrawFunctionLabel, + DRAW_FUNCTION_LABEL_INTERNER +); + +pub type InternedDrawFunctionLabel = Interned; + +/// Stores the rendering instructions for a single phase that uses bins in all +/// views. +/// +/// They're cleared out every frame, but storing them in a resource like this +/// allows us to reuse allocations. +#[derive(Resource, Deref, DerefMut)] +pub struct ViewBinnedRenderPhases(pub HashMap>) +where + BPI: BinnedPhaseItem; + +/// A collection of all rendering instructions, that will be executed by the GPU, for a +/// single render phase for a single view. +/// +/// Each view (camera, or shadow-casting light, etc.) can have one or multiple render phases. +/// They are used to queue entities for rendering. +/// Multiple phases might be required due to different sorting/batching behaviors +/// (e.g. opaque: front to back, transparent: back to front) or because one phase depends on +/// the rendered texture of the previous phase (e.g. for screen-space reflections). +/// All [`PhaseItem`]s are then rendered using a single [`TrackedRenderPass`]. +/// The render pass might be reused for multiple phases to reduce GPU overhead. +/// +/// This flavor of render phase is used for phases in which the ordering is less +/// critical: for example, `Opaque3d`. It's generally faster than the +/// alternative [`SortedRenderPhase`]. +pub struct BinnedRenderPhase +where + BPI: BinnedPhaseItem, +{ + /// The multidrawable bins. + /// + /// Each batch set key maps to a *batch set*, which in this case is a set of + /// meshes that can be drawn together in one multidraw call. Each batch set + /// is subdivided into *bins*, each of which represents a particular mesh. + /// Each bin contains the entity IDs of instances of that mesh. + /// + /// So, for example, if there are two cubes and a sphere present in the + /// scene, we would generally have one batch set containing two bins, + /// assuming that the cubes and sphere meshes are allocated together and use + /// the same pipeline. The first bin, corresponding to the cubes, will have + /// two entities in it. The second bin, corresponding to the sphere, will + /// have one entity in it. + pub multidrawable_meshes: IndexMap>, + + /// The bins corresponding to batchable items that aren't multidrawable. + /// + /// For multidrawable entities, use `multidrawable_meshes`; for + /// unbatchable entities, use `unbatchable_values`. + pub batchable_meshes: IndexMap<(BPI::BatchSetKey, BPI::BinKey), RenderBin>, + + /// The unbatchable bins. + /// + /// Each entity here is rendered in a separate drawcall. + pub unbatchable_meshes: IndexMap<(BPI::BatchSetKey, BPI::BinKey), UnbatchableBinnedEntities>, + + /// Items in the bin that aren't meshes at all. + /// + /// Bevy itself doesn't place anything in this list, but plugins or your app + /// can in order to execute custom drawing commands. Draw functions for each + /// entity are simply called in order at rendering time. + /// + /// See the `custom_phase_item` example for an example of how to use this. + pub non_mesh_items: IndexMap<(BPI::BatchSetKey, BPI::BinKey), NonMeshEntities>, + + /// Information on each batch set. + /// + /// A *batch set* is a set of entities that will be batched together unless + /// we're on a platform that doesn't support storage buffers (e.g. WebGL 2) + /// and differing dynamic uniform indices force us to break batches. On + /// platforms that support storage buffers, a batch set always consists of + /// at most one batch. + /// + /// Multidrawable entities come first, then batchable entities, then + /// unbatchable entities. + pub(crate) batch_sets: BinnedRenderPhaseBatchSets, + + /// The batch and bin key for each entity. + /// + /// We retain these so that, when the entity changes, + /// [`Self::sweep_old_entities`] can quickly find the bin it was located in + /// and remove it. + cached_entity_bin_keys: IndexMap, EntityHash>, + + /// The set of indices in [`Self::cached_entity_bin_keys`] that are + /// confirmed to be up to date. + /// + /// Note that each bit in this bit set refers to an *index* in the + /// [`IndexMap`] (i.e. a bucket in the hash table). They aren't entity IDs. + valid_cached_entity_bin_keys: FixedBitSet, + + /// The set of entities that changed bins this frame. + /// + /// An entity will only be present in this list if it was in one bin on the + /// previous frame and is in a new bin on this frame. Each list entry + /// specifies the bin the entity used to be in. We use this in order to + /// remove the entity from the old bin during + /// [`BinnedRenderPhase::sweep_old_entities`]. + entities_that_changed_bins: Vec>, + /// The gpu preprocessing mode configured for the view this phase is associated + /// with. + gpu_preprocessing_mode: GpuPreprocessingMode, +} + +/// All entities that share a mesh and a material and can be batched as part of +/// a [`BinnedRenderPhase`]. +#[derive(Default)] +pub struct RenderBin { + /// A list of the entities in each bin, along with their cached + /// [`InputUniformIndex`]. + entities: IndexMap, +} + +/// Information that we track about an entity that was in one bin on the +/// previous frame and is in a different bin this frame. +struct EntityThatChangedBins +where + BPI: BinnedPhaseItem, +{ + /// The entity. + main_entity: MainEntity, + /// The key that identifies the bin that this entity used to be in. + old_cached_binned_entity: CachedBinnedEntity, +} + +/// Information that we keep about an entity currently within a bin. +pub struct CachedBinnedEntity +where + BPI: BinnedPhaseItem, +{ + /// Information that we use to identify a cached entity in a bin. + pub cached_bin_key: Option>, + /// The last modified tick of the entity. + /// + /// We use this to detect when the entity needs to be invalidated. + pub change_tick: Tick, +} + +/// Information that we use to identify a cached entity in a bin. +pub struct CachedBinKey +where + BPI: BinnedPhaseItem, +{ + /// The key of the batch set containing the entity. + pub batch_set_key: BPI::BatchSetKey, + /// The key of the bin containing the entity. + pub bin_key: BPI::BinKey, + /// The type of render phase that we use to render the entity: multidraw, + /// plain batch, etc. + pub phase_type: BinnedRenderPhaseType, +} + +impl Clone for CachedBinnedEntity +where + BPI: BinnedPhaseItem, +{ + fn clone(&self) -> Self { + CachedBinnedEntity { + cached_bin_key: self.cached_bin_key.clone(), + change_tick: self.change_tick, + } + } +} + +impl Clone for CachedBinKey +where + BPI: BinnedPhaseItem, +{ + fn clone(&self) -> Self { + CachedBinKey { + batch_set_key: self.batch_set_key.clone(), + bin_key: self.bin_key.clone(), + phase_type: self.phase_type, + } + } +} + +impl PartialEq for CachedBinKey +where + BPI: BinnedPhaseItem, +{ + fn eq(&self, other: &Self) -> bool { + self.batch_set_key == other.batch_set_key + && self.bin_key == other.bin_key + && self.phase_type == other.phase_type + } +} + +/// How we store and render the batch sets. +/// +/// Each one of these corresponds to a [`GpuPreprocessingMode`]. +pub enum BinnedRenderPhaseBatchSets { + /// Batches are grouped into batch sets based on dynamic uniforms. + /// + /// This corresponds to [`GpuPreprocessingMode::None`]. + DynamicUniforms(Vec>), + + /// Batches are never grouped into batch sets. + /// + /// This corresponds to [`GpuPreprocessingMode::PreprocessingOnly`]. + Direct(Vec), + + /// Batches are grouped together into batch sets based on their ability to + /// be multi-drawn together. + /// + /// This corresponds to [`GpuPreprocessingMode::Culling`]. + MultidrawIndirect(Vec>), +} + +/// A group of entities that will be batched together into a single multi-draw +/// call. +pub struct BinnedRenderPhaseBatchSet { + /// The first batch in this batch set. + pub(crate) first_batch: BinnedRenderPhaseBatch, + /// The key of the bin that the first batch corresponds to. + pub(crate) bin_key: BK, + /// The number of batches. + pub(crate) batch_count: u32, + /// The index of the batch set in the GPU buffer. + pub(crate) index: u32, +} + +impl BinnedRenderPhaseBatchSets { + fn clear(&mut self) { + match *self { + BinnedRenderPhaseBatchSets::DynamicUniforms(ref mut vec) => vec.clear(), + BinnedRenderPhaseBatchSets::Direct(ref mut vec) => vec.clear(), + BinnedRenderPhaseBatchSets::MultidrawIndirect(ref mut vec) => vec.clear(), + } + } +} + +/// Information about a single batch of entities rendered using binned phase +/// items. +#[derive(Debug)] +pub struct BinnedRenderPhaseBatch { + /// An entity that's *representative* of this batch. + /// + /// Bevy uses this to fetch the mesh. It can be any entity in the batch. + pub representative_entity: (Entity, MainEntity), + /// The range of instance indices in this batch. + pub instance_range: Range, + + /// The dynamic offset of the batch. + /// + /// Note that dynamic offsets are only used on platforms that don't support + /// storage buffers. + pub extra_index: PhaseItemExtraIndex, +} + +/// Information about the unbatchable entities in a bin. +pub struct UnbatchableBinnedEntities { + /// The entities. + pub entities: MainEntityHashMap, + + /// The GPU array buffer indices of each unbatchable binned entity. + pub(crate) buffer_indices: UnbatchableBinnedEntityIndexSet, +} + +/// Information about [`BinnedRenderPhaseType::NonMesh`] entities. +pub struct NonMeshEntities { + /// The entities. + pub entities: MainEntityHashMap, +} + +/// Stores instance indices and dynamic offsets for unbatchable entities in a +/// binned render phase. +/// +/// This is conceptually `Vec`, but it +/// avoids the overhead of storing dynamic offsets on platforms that support +/// them. In other words, this allows a fast path that avoids allocation on +/// platforms that aren't WebGL 2. +#[derive(Default)] + +pub(crate) enum UnbatchableBinnedEntityIndexSet { + /// There are no unbatchable entities in this bin (yet). + #[default] + NoEntities, + + /// The instances for all unbatchable entities in this bin are contiguous, + /// and there are no dynamic uniforms. + /// + /// This is the typical case on platforms other than WebGL 2. We special + /// case this to avoid allocation on those platforms. + Sparse { + /// The range of indices. + instance_range: Range, + /// The index of the first indirect instance parameters. + /// + /// The other indices immediately follow these. + first_indirect_parameters_index: Option, + }, + + /// Dynamic uniforms are present for unbatchable entities in this bin. + /// + /// We fall back to this on WebGL 2. + Dense(Vec), +} + +/// The instance index and dynamic offset (if present) for an unbatchable entity. +/// +/// This is only useful on platforms that don't support storage buffers. +#[derive(Clone)] +pub(crate) struct UnbatchableBinnedEntityIndices { + /// The instance index. + pub(crate) instance_index: u32, + /// The [`PhaseItemExtraIndex`], if present. + pub(crate) extra_index: PhaseItemExtraIndex, +} + +/// Identifies the list within [`BinnedRenderPhase`] that a phase item is to be +/// placed in. +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum BinnedRenderPhaseType { + /// The item is a mesh that's eligible for multi-draw indirect rendering and + /// can be batched with other meshes of the same type. + MultidrawableMesh, + + /// The item is a mesh that can be batched with other meshes of the same type and + /// drawn in a single draw call. + BatchableMesh, + + /// The item is a mesh that's eligible for indirect rendering, but can't be + /// batched with other meshes of the same type. + UnbatchableMesh, + + /// The item isn't a mesh at all. + /// + /// Bevy will simply invoke the drawing commands for such items one after + /// another, with no further processing. + /// + /// The engine itself doesn't enqueue any items of this type, but it's + /// available for use in your application and/or plugins. + NonMesh, +} + +impl From> for UnbatchableBinnedEntityIndices +where + T: Clone + ShaderSize + WriteInto, +{ + fn from(value: GpuArrayBufferIndex) -> Self { + UnbatchableBinnedEntityIndices { + instance_index: value.index, + extra_index: PhaseItemExtraIndex::maybe_dynamic_offset(value.dynamic_offset), + } + } +} + +impl Default for ViewBinnedRenderPhases +where + BPI: BinnedPhaseItem, +{ + fn default() -> Self { + Self(default()) + } +} + +impl ViewBinnedRenderPhases +where + BPI: BinnedPhaseItem, +{ + pub fn prepare_for_new_frame( + &mut self, + retained_view_entity: RetainedViewEntity, + gpu_preprocessing: GpuPreprocessingMode, + ) { + match self.entry(retained_view_entity) { + Entry::Occupied(mut entry) => entry.get_mut().prepare_for_new_frame(), + Entry::Vacant(entry) => { + entry.insert(BinnedRenderPhase::::new(gpu_preprocessing)); + } + } + } +} + +/// The index of the uniform describing this object in the GPU buffer, when GPU +/// preprocessing is enabled. +/// +/// For example, for 3D meshes, this is the index of the `MeshInputUniform` in +/// the buffer. +/// +/// This field is ignored if GPU preprocessing isn't in use, such as (currently) +/// in the case of 2D meshes. In that case, it can be safely set to +/// [`core::default::Default::default`]. +#[derive(Clone, Copy, PartialEq, Default, Deref, DerefMut)] +#[repr(transparent)] +pub struct InputUniformIndex(pub u32); + +impl BinnedRenderPhase +where + BPI: BinnedPhaseItem, +{ + /// Bins a new entity. + /// + /// The `phase_type` parameter specifies whether the entity is a + /// preprocessable mesh and whether it can be binned with meshes of the same + /// type. + pub fn add( + &mut self, + batch_set_key: BPI::BatchSetKey, + bin_key: BPI::BinKey, + (entity, main_entity): (Entity, MainEntity), + input_uniform_index: InputUniformIndex, + mut phase_type: BinnedRenderPhaseType, + change_tick: Tick, + ) { + // If the user has overridden indirect drawing for this view, we need to + // force the phase type to be batchable instead. + if self.gpu_preprocessing_mode == GpuPreprocessingMode::PreprocessingOnly + && phase_type == BinnedRenderPhaseType::MultidrawableMesh + { + phase_type = BinnedRenderPhaseType::BatchableMesh; + } + + match phase_type { + BinnedRenderPhaseType::MultidrawableMesh => { + match self.multidrawable_meshes.entry(batch_set_key.clone()) { + indexmap::map::Entry::Occupied(mut entry) => { + entry + .get_mut() + .entry(bin_key.clone()) + .or_default() + .insert(main_entity, input_uniform_index); + } + indexmap::map::Entry::Vacant(entry) => { + let mut new_batch_set = IndexMap::default(); + new_batch_set.insert( + bin_key.clone(), + RenderBin::from_entity(main_entity, input_uniform_index), + ); + entry.insert(new_batch_set); + } + } + } + + BinnedRenderPhaseType::BatchableMesh => { + match self + .batchable_meshes + .entry((batch_set_key.clone(), bin_key.clone()).clone()) + { + indexmap::map::Entry::Occupied(mut entry) => { + entry.get_mut().insert(main_entity, input_uniform_index); + } + indexmap::map::Entry::Vacant(entry) => { + entry.insert(RenderBin::from_entity(main_entity, input_uniform_index)); + } + } + } + + BinnedRenderPhaseType::UnbatchableMesh => { + match self + .unbatchable_meshes + .entry((batch_set_key.clone(), bin_key.clone())) + { + indexmap::map::Entry::Occupied(mut entry) => { + entry.get_mut().entities.insert(main_entity, entity); + } + indexmap::map::Entry::Vacant(entry) => { + let mut entities = MainEntityHashMap::default(); + entities.insert(main_entity, entity); + entry.insert(UnbatchableBinnedEntities { + entities, + buffer_indices: default(), + }); + } + } + } + + BinnedRenderPhaseType::NonMesh => { + // We don't process these items further. + match self + .non_mesh_items + .entry((batch_set_key.clone(), bin_key.clone()).clone()) + { + indexmap::map::Entry::Occupied(mut entry) => { + entry.get_mut().entities.insert(main_entity, entity); + } + indexmap::map::Entry::Vacant(entry) => { + let mut entities = MainEntityHashMap::default(); + entities.insert(main_entity, entity); + entry.insert(NonMeshEntities { entities }); + } + } + } + } + + // Update the cache. + self.update_cache( + main_entity, + Some(CachedBinKey { + batch_set_key, + bin_key, + phase_type, + }), + change_tick, + ); + } + + /// Inserts an entity into the cache with the given change tick. + pub fn update_cache( + &mut self, + main_entity: MainEntity, + cached_bin_key: Option>, + change_tick: Tick, + ) { + let new_cached_binned_entity = CachedBinnedEntity { + cached_bin_key, + change_tick, + }; + + let (index, old_cached_binned_entity) = self + .cached_entity_bin_keys + .insert_full(main_entity, new_cached_binned_entity.clone()); + + // If the entity changed bins, record its old bin so that we can remove + // the entity from it. + if let Some(old_cached_binned_entity) = old_cached_binned_entity + && old_cached_binned_entity.cached_bin_key != new_cached_binned_entity.cached_bin_key + { + self.entities_that_changed_bins.push(EntityThatChangedBins { + main_entity, + old_cached_binned_entity, + }); + } + + // Mark the entity as valid. + self.valid_cached_entity_bin_keys.grow_and_insert(index); + } + + /// Encodes the GPU commands needed to render all entities in this phase. + pub fn render<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + ) -> Result<(), DrawError> { + { + let draw_functions = world.resource::>(); + let mut draw_functions = draw_functions.write(); + draw_functions.prepare(world); + // Make sure to drop the reader-writer lock here to avoid recursive + // locks. + } + + self.render_batchable_meshes(render_pass, world, view)?; + self.render_unbatchable_meshes(render_pass, world, view)?; + self.render_non_meshes(render_pass, world, view)?; + + Ok(()) + } + + /// Renders all batchable meshes queued in this phase. + fn render_batchable_meshes<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + ) -> Result<(), DrawError> { + let draw_functions = world.resource::>(); + let mut draw_functions = draw_functions.write(); + + let render_device = world.resource::(); + let render_adapter_info = world.resource::(); + let multi_draw_indirect_count_supported = render_device + .features() + .contains(Features::MULTI_DRAW_INDIRECT_COUNT) + // TODO: https://github.com/gfx-rs/wgpu/issues/7974 + && !matches!(render_adapter_info.backend, wgpu::Backend::Dx12); + + match self.batch_sets { + BinnedRenderPhaseBatchSets::DynamicUniforms(ref batch_sets) => { + debug_assert_eq!(self.batchable_meshes.len(), batch_sets.len()); + + for ((batch_set_key, bin_key), batch_set) in + self.batchable_meshes.keys().zip(batch_sets.iter()) + { + for batch in batch_set { + let binned_phase_item = BPI::new( + batch_set_key.clone(), + bin_key.clone(), + batch.representative_entity, + batch.instance_range.clone(), + batch.extra_index.clone(), + ); + + // Fetch the draw function. + let Some(draw_function) = + draw_functions.get_mut(binned_phase_item.draw_function()) + else { + continue; + }; + + draw_function.draw(world, render_pass, view, &binned_phase_item)?; + } + } + } + + BinnedRenderPhaseBatchSets::Direct(ref batch_set) => { + for (batch, (batch_set_key, bin_key)) in + batch_set.iter().zip(self.batchable_meshes.keys()) + { + let binned_phase_item = BPI::new( + batch_set_key.clone(), + bin_key.clone(), + batch.representative_entity, + batch.instance_range.clone(), + batch.extra_index.clone(), + ); + + // Fetch the draw function. + let Some(draw_function) = + draw_functions.get_mut(binned_phase_item.draw_function()) + else { + continue; + }; + + draw_function.draw(world, render_pass, view, &binned_phase_item)?; + } + } + + BinnedRenderPhaseBatchSets::MultidrawIndirect(ref batch_sets) => { + for (batch_set_key, batch_set) in self + .multidrawable_meshes + .keys() + .chain( + self.batchable_meshes + .keys() + .map(|(batch_set_key, _)| batch_set_key), + ) + .zip(batch_sets.iter()) + { + let batch = &batch_set.first_batch; + + let batch_set_index = if multi_draw_indirect_count_supported { + NonMaxU32::new(batch_set.index) + } else { + None + }; + + let binned_phase_item = BPI::new( + batch_set_key.clone(), + batch_set.bin_key.clone(), + batch.representative_entity, + batch.instance_range.clone(), + match batch.extra_index { + PhaseItemExtraIndex::None => PhaseItemExtraIndex::None, + PhaseItemExtraIndex::DynamicOffset(ref dynamic_offset) => { + PhaseItemExtraIndex::DynamicOffset(*dynamic_offset) + } + PhaseItemExtraIndex::IndirectParametersIndex { ref range, .. } => { + PhaseItemExtraIndex::IndirectParametersIndex { + range: range.start..(range.start + batch_set.batch_count), + batch_set_index, + } + } + }, + ); + + // Fetch the draw function. + let Some(draw_function) = + draw_functions.get_mut(binned_phase_item.draw_function()) + else { + continue; + }; + + draw_function.draw(world, render_pass, view, &binned_phase_item)?; + } + } + } + + Ok(()) + } + + /// Renders all unbatchable meshes queued in this phase. + fn render_unbatchable_meshes<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + ) -> Result<(), DrawError> { + let draw_functions = world.resource::>(); + let mut draw_functions = draw_functions.write(); + + for (batch_set_key, bin_key) in self.unbatchable_meshes.keys() { + let unbatchable_entities = + &self.unbatchable_meshes[&(batch_set_key.clone(), bin_key.clone())]; + for (entity_index, entity) in unbatchable_entities.entities.iter().enumerate() { + let unbatchable_dynamic_offset = match &unbatchable_entities.buffer_indices { + UnbatchableBinnedEntityIndexSet::NoEntities => { + // Shouldn't happen… + continue; + } + UnbatchableBinnedEntityIndexSet::Sparse { + instance_range, + first_indirect_parameters_index, + } => UnbatchableBinnedEntityIndices { + instance_index: instance_range.start + entity_index as u32, + extra_index: match first_indirect_parameters_index { + None => PhaseItemExtraIndex::None, + Some(first_indirect_parameters_index) => { + let first_indirect_parameters_index_for_entity = + u32::from(*first_indirect_parameters_index) + + entity_index as u32; + PhaseItemExtraIndex::IndirectParametersIndex { + range: first_indirect_parameters_index_for_entity + ..(first_indirect_parameters_index_for_entity + 1), + batch_set_index: None, + } + } + }, + }, + UnbatchableBinnedEntityIndexSet::Dense(dynamic_offsets) => { + dynamic_offsets[entity_index].clone() + } + }; + + let binned_phase_item = BPI::new( + batch_set_key.clone(), + bin_key.clone(), + (*entity.1, *entity.0), + unbatchable_dynamic_offset.instance_index + ..(unbatchable_dynamic_offset.instance_index + 1), + unbatchable_dynamic_offset.extra_index, + ); + + // Fetch the draw function. + let Some(draw_function) = draw_functions.get_mut(binned_phase_item.draw_function()) + else { + continue; + }; + + draw_function.draw(world, render_pass, view, &binned_phase_item)?; + } + } + Ok(()) + } + + /// Renders all objects of type [`BinnedRenderPhaseType::NonMesh`]. + /// + /// These will have been added by plugins or the application. + fn render_non_meshes<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + ) -> Result<(), DrawError> { + let draw_functions = world.resource::>(); + let mut draw_functions = draw_functions.write(); + + for ((batch_set_key, bin_key), non_mesh_entities) in &self.non_mesh_items { + for (main_entity, entity) in non_mesh_entities.entities.iter() { + // Come up with a fake batch range and extra index. The draw + // function is expected to manage any sort of batching logic itself. + let binned_phase_item = BPI::new( + batch_set_key.clone(), + bin_key.clone(), + (*entity, *main_entity), + 0..1, + PhaseItemExtraIndex::None, + ); + + let Some(draw_function) = draw_functions.get_mut(binned_phase_item.draw_function()) + else { + continue; + }; + + draw_function.draw(world, render_pass, view, &binned_phase_item)?; + } + } + + Ok(()) + } + + pub fn is_empty(&self) -> bool { + self.multidrawable_meshes.is_empty() + && self.batchable_meshes.is_empty() + && self.unbatchable_meshes.is_empty() + && self.non_mesh_items.is_empty() + } + + pub fn prepare_for_new_frame(&mut self) { + self.batch_sets.clear(); + + self.valid_cached_entity_bin_keys.clear(); + self.valid_cached_entity_bin_keys + .grow(self.cached_entity_bin_keys.len()); + self.valid_cached_entity_bin_keys + .set_range(self.cached_entity_bin_keys.len().., true); + + self.entities_that_changed_bins.clear(); + + for unbatchable_bin in self.unbatchable_meshes.values_mut() { + unbatchable_bin.buffer_indices.clear(); + } + } + + /// Checks to see whether the entity is in a bin and returns true if it's + /// both in a bin and up to date. + /// + /// If this function returns true, we also add the entry to the + /// `valid_cached_entity_bin_keys` list. + pub fn validate_cached_entity( + &mut self, + visible_entity: MainEntity, + current_change_tick: Tick, + ) -> bool { + if let indexmap::map::Entry::Occupied(entry) = + self.cached_entity_bin_keys.entry(visible_entity) + && entry.get().change_tick == current_change_tick + { + self.valid_cached_entity_bin_keys.insert(entry.index()); + return true; + } + + false + } + + /// Removes all entities not marked as clean from the bins. + /// + /// During `queue_material_meshes`, we process all visible entities and mark + /// each as clean as we come to it. Then, in [`sweep_old_entities`], we call + /// this method, which removes entities that aren't marked as clean from the + /// bins. + pub fn sweep_old_entities(&mut self) { + // Search for entities not marked as valid. We have to do this in + // reverse order because `swap_remove_index` will potentially invalidate + // all indices after the one we remove. + for index in ReverseFixedBitSetZeroesIterator::new(&self.valid_cached_entity_bin_keys) { + let Some((entity, cached_binned_entity)) = + self.cached_entity_bin_keys.swap_remove_index(index) + else { + continue; + }; + + if let Some(ref cached_bin_key) = cached_binned_entity.cached_bin_key { + remove_entity_from_bin( + entity, + cached_bin_key, + &mut self.multidrawable_meshes, + &mut self.batchable_meshes, + &mut self.unbatchable_meshes, + &mut self.non_mesh_items, + ); + } + } + + // If an entity changed bins, we need to remove it from its old bin. + for entity_that_changed_bins in self.entities_that_changed_bins.drain(..) { + let Some(ref old_cached_bin_key) = entity_that_changed_bins + .old_cached_binned_entity + .cached_bin_key + else { + continue; + }; + remove_entity_from_bin( + entity_that_changed_bins.main_entity, + old_cached_bin_key, + &mut self.multidrawable_meshes, + &mut self.batchable_meshes, + &mut self.unbatchable_meshes, + &mut self.non_mesh_items, + ); + } + } +} + +/// Removes an entity from a bin. +/// +/// If this makes the bin empty, this function removes the bin as well. +/// +/// This is a standalone function instead of a method on [`BinnedRenderPhase`] +/// for borrow check reasons. +fn remove_entity_from_bin( + entity: MainEntity, + entity_bin_key: &CachedBinKey, + multidrawable_meshes: &mut IndexMap>, + batchable_meshes: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), RenderBin>, + unbatchable_meshes: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), UnbatchableBinnedEntities>, + non_mesh_items: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), NonMeshEntities>, +) where + BPI: BinnedPhaseItem, +{ + match entity_bin_key.phase_type { + BinnedRenderPhaseType::MultidrawableMesh => { + if let indexmap::map::Entry::Occupied(mut batch_set_entry) = + multidrawable_meshes.entry(entity_bin_key.batch_set_key.clone()) + { + if let indexmap::map::Entry::Occupied(mut bin_entry) = batch_set_entry + .get_mut() + .entry(entity_bin_key.bin_key.clone()) + { + bin_entry.get_mut().remove(entity); + + // If the bin is now empty, remove the bin. + if bin_entry.get_mut().is_empty() { + bin_entry.swap_remove(); + } + } + + // If the batch set is now empty, remove it. This will perturb + // the order, but that's OK because we're going to sort the bin + // afterwards. + if batch_set_entry.get_mut().is_empty() { + batch_set_entry.swap_remove(); + } + } + } + + BinnedRenderPhaseType::BatchableMesh => { + if let indexmap::map::Entry::Occupied(mut bin_entry) = batchable_meshes.entry(( + entity_bin_key.batch_set_key.clone(), + entity_bin_key.bin_key.clone(), + )) { + bin_entry.get_mut().remove(entity); + + // If the bin is now empty, remove the bin. + if bin_entry.get_mut().is_empty() { + bin_entry.swap_remove(); + } + } + } + + BinnedRenderPhaseType::UnbatchableMesh => { + if let indexmap::map::Entry::Occupied(mut bin_entry) = unbatchable_meshes.entry(( + entity_bin_key.batch_set_key.clone(), + entity_bin_key.bin_key.clone(), + )) { + bin_entry.get_mut().entities.remove(&entity); + + // If the bin is now empty, remove the bin. + if bin_entry.get_mut().entities.is_empty() { + bin_entry.swap_remove(); + } + } + } + + BinnedRenderPhaseType::NonMesh => { + if let indexmap::map::Entry::Occupied(mut bin_entry) = non_mesh_items.entry(( + entity_bin_key.batch_set_key.clone(), + entity_bin_key.bin_key.clone(), + )) { + bin_entry.get_mut().entities.remove(&entity); + + // If the bin is now empty, remove the bin. + if bin_entry.get_mut().entities.is_empty() { + bin_entry.swap_remove(); + } + } + } + } +} + +impl BinnedRenderPhase +where + BPI: BinnedPhaseItem, +{ + fn new(gpu_preprocessing: GpuPreprocessingMode) -> Self { + Self { + multidrawable_meshes: IndexMap::default(), + batchable_meshes: IndexMap::default(), + unbatchable_meshes: IndexMap::default(), + non_mesh_items: IndexMap::default(), + batch_sets: match gpu_preprocessing { + GpuPreprocessingMode::Culling => { + BinnedRenderPhaseBatchSets::MultidrawIndirect(vec![]) + } + GpuPreprocessingMode::PreprocessingOnly => { + BinnedRenderPhaseBatchSets::Direct(vec![]) + } + GpuPreprocessingMode::None => BinnedRenderPhaseBatchSets::DynamicUniforms(vec![]), + }, + cached_entity_bin_keys: IndexMap::default(), + valid_cached_entity_bin_keys: FixedBitSet::new(), + entities_that_changed_bins: vec![], + gpu_preprocessing_mode: gpu_preprocessing, + } + } +} + +impl UnbatchableBinnedEntityIndexSet { + /// Returns the [`UnbatchableBinnedEntityIndices`] for the given entity. + fn indices_for_entity_index( + &self, + entity_index: u32, + ) -> Option { + match self { + UnbatchableBinnedEntityIndexSet::NoEntities => None, + UnbatchableBinnedEntityIndexSet::Sparse { instance_range, .. } + if entity_index >= instance_range.len() as u32 => + { + None + } + UnbatchableBinnedEntityIndexSet::Sparse { + instance_range, + first_indirect_parameters_index: None, + } => Some(UnbatchableBinnedEntityIndices { + instance_index: instance_range.start + entity_index, + extra_index: PhaseItemExtraIndex::None, + }), + UnbatchableBinnedEntityIndexSet::Sparse { + instance_range, + first_indirect_parameters_index: Some(first_indirect_parameters_index), + } => { + let first_indirect_parameters_index_for_this_batch = + u32::from(*first_indirect_parameters_index) + entity_index; + Some(UnbatchableBinnedEntityIndices { + instance_index: instance_range.start + entity_index, + extra_index: PhaseItemExtraIndex::IndirectParametersIndex { + range: first_indirect_parameters_index_for_this_batch + ..(first_indirect_parameters_index_for_this_batch + 1), + batch_set_index: None, + }, + }) + } + UnbatchableBinnedEntityIndexSet::Dense(indices) => { + indices.get(entity_index as usize).cloned() + } + } + } +} + +/// A convenient abstraction for adding all the systems necessary for a binned +/// render phase to the render app. +/// +/// This is the version used when the pipeline supports GPU preprocessing: e.g. +/// 3D PBR meshes. +pub struct BinnedRenderPhasePlugin +where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + /// Debugging flags that can optionally be set when constructing the renderer. + pub debug_flags: RenderDebugFlags, + phantom: PhantomData<(BPI, GFBD)>, +} + +impl BinnedRenderPhasePlugin +where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData, +{ + pub fn new(debug_flags: RenderDebugFlags) -> Self { + Self { + debug_flags, + phantom: PhantomData, + } + } +} + +impl Plugin for BinnedRenderPhasePlugin +where + BPI: BinnedPhaseItem, + GFBD: GetFullBatchData + Sync + Send + 'static, +{ + fn build(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .init_resource::>() + .init_resource::>() + .insert_resource(PhaseIndirectParametersBuffers::::new( + self.debug_flags + .contains(RenderDebugFlags::ALLOW_COPIES_FROM_INDIRECT_PARAMETERS), + )) + .add_systems( + Render, + ( + batching::sort_binned_render_phase::.in_set(RenderSystems::PhaseSort), + ( + no_gpu_preprocessing::batch_and_prepare_binned_render_phase:: + .run_if(resource_exists::>), + gpu_preprocessing::batch_and_prepare_binned_render_phase:: + .run_if( + resource_exists::< + BatchedInstanceBuffers, + >, + ), + ) + .in_set(RenderSystems::PrepareResources), + sweep_old_entities::.in_set(RenderSystems::QueueSweep), + gpu_preprocessing::collect_buffers_for_phase:: + .run_if( + resource_exists::< + BatchedInstanceBuffers, + >, + ) + .in_set(RenderSystems::PrepareResourcesCollectPhaseBuffers), + ), + ); + } +} + +/// Stores the rendering instructions for a single phase that sorts items in all +/// views. +/// +/// They're cleared out every frame, but storing them in a resource like this +/// allows us to reuse allocations. +#[derive(Resource, Deref, DerefMut)] +pub struct ViewSortedRenderPhases(pub HashMap>) +where + SPI: SortedPhaseItem; + +impl Default for ViewSortedRenderPhases +where + SPI: SortedPhaseItem, +{ + fn default() -> Self { + Self(default()) + } +} + +impl ViewSortedRenderPhases +where + SPI: SortedPhaseItem, +{ + pub fn insert_or_clear(&mut self, retained_view_entity: RetainedViewEntity) { + match self.entry(retained_view_entity) { + Entry::Occupied(mut entry) => entry.get_mut().clear(), + Entry::Vacant(entry) => { + entry.insert(default()); + } + } + } +} + +/// A convenient abstraction for adding all the systems necessary for a sorted +/// render phase to the render app. +/// +/// This is the version used when the pipeline supports GPU preprocessing: e.g. +/// 3D PBR meshes. +pub struct SortedRenderPhasePlugin +where + SPI: SortedPhaseItem, + GFBD: GetFullBatchData, +{ + /// Debugging flags that can optionally be set when constructing the renderer. + pub debug_flags: RenderDebugFlags, + phantom: PhantomData<(SPI, GFBD)>, +} + +impl SortedRenderPhasePlugin +where + SPI: SortedPhaseItem, + GFBD: GetFullBatchData, +{ + pub fn new(debug_flags: RenderDebugFlags) -> Self { + Self { + debug_flags, + phantom: PhantomData, + } + } +} + +impl Plugin for SortedRenderPhasePlugin +where + SPI: SortedPhaseItem + CachedRenderPipelinePhaseItem, + GFBD: GetFullBatchData + Sync + Send + 'static, +{ + fn build(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .init_resource::>() + .init_resource::>() + .insert_resource(PhaseIndirectParametersBuffers::::new( + self.debug_flags + .contains(RenderDebugFlags::ALLOW_COPIES_FROM_INDIRECT_PARAMETERS), + )) + .add_systems( + Render, + ( + ( + no_gpu_preprocessing::batch_and_prepare_sorted_render_phase:: + .run_if(resource_exists::>), + gpu_preprocessing::batch_and_prepare_sorted_render_phase:: + .run_if( + resource_exists::< + BatchedInstanceBuffers, + >, + ), + ) + .in_set(RenderSystems::PrepareResources), + gpu_preprocessing::collect_buffers_for_phase:: + .run_if( + resource_exists::< + BatchedInstanceBuffers, + >, + ) + .in_set(RenderSystems::PrepareResourcesCollectPhaseBuffers), + ), + ); + } +} + +impl UnbatchableBinnedEntityIndexSet { + /// Adds a new entity to the list of unbatchable binned entities. + pub fn add(&mut self, indices: UnbatchableBinnedEntityIndices) { + match self { + UnbatchableBinnedEntityIndexSet::NoEntities => { + match indices.extra_index { + PhaseItemExtraIndex::DynamicOffset(_) => { + // This is the first entity we've seen, and we don't have + // compute shaders. Initialize an array. + *self = UnbatchableBinnedEntityIndexSet::Dense(vec![indices]); + } + PhaseItemExtraIndex::None => { + // This is the first entity we've seen, and we have compute + // shaders. Initialize the fast path. + *self = UnbatchableBinnedEntityIndexSet::Sparse { + instance_range: indices.instance_index..indices.instance_index + 1, + first_indirect_parameters_index: None, + } + } + PhaseItemExtraIndex::IndirectParametersIndex { + range: ref indirect_parameters_index, + .. + } => { + // This is the first entity we've seen, and we have compute + // shaders. Initialize the fast path. + *self = UnbatchableBinnedEntityIndexSet::Sparse { + instance_range: indices.instance_index..indices.instance_index + 1, + first_indirect_parameters_index: NonMaxU32::new( + indirect_parameters_index.start, + ), + } + } + } + } + + UnbatchableBinnedEntityIndexSet::Sparse { + instance_range, + first_indirect_parameters_index, + } if instance_range.end == indices.instance_index + && ((first_indirect_parameters_index.is_none() + && indices.extra_index == PhaseItemExtraIndex::None) + || first_indirect_parameters_index.is_some_and( + |first_indirect_parameters_index| match indices.extra_index { + PhaseItemExtraIndex::IndirectParametersIndex { + range: ref this_range, + .. + } => { + u32::from(first_indirect_parameters_index) + instance_range.end + - instance_range.start + == this_range.start + } + PhaseItemExtraIndex::DynamicOffset(_) | PhaseItemExtraIndex::None => { + false + } + }, + )) => + { + // This is the normal case on non-WebGL 2. + instance_range.end += 1; + } + + UnbatchableBinnedEntityIndexSet::Sparse { instance_range, .. } => { + // We thought we were in non-WebGL 2 mode, but we got a dynamic + // offset or non-contiguous index anyway. This shouldn't happen, + // but let's go ahead and do the sensible thing anyhow: demote + // the compressed `NoDynamicOffsets` field to the full + // `DynamicOffsets` array. + warn!( + "Unbatchable binned entity index set was demoted from sparse to dense. \ + This is a bug in the renderer. Please report it.", + ); + let new_dynamic_offsets = (0..instance_range.len() as u32) + .flat_map(|entity_index| self.indices_for_entity_index(entity_index)) + .chain(iter::once(indices)) + .collect(); + *self = UnbatchableBinnedEntityIndexSet::Dense(new_dynamic_offsets); + } + + UnbatchableBinnedEntityIndexSet::Dense(dense_indices) => { + dense_indices.push(indices); + } + } + } + + /// Clears the unbatchable binned entity index set. + fn clear(&mut self) { + match self { + UnbatchableBinnedEntityIndexSet::Dense(dense_indices) => dense_indices.clear(), + UnbatchableBinnedEntityIndexSet::Sparse { .. } => { + *self = UnbatchableBinnedEntityIndexSet::NoEntities; + } + _ => {} + } + } +} + +/// A collection of all items to be rendered that will be encoded to GPU +/// commands for a single render phase for a single view. +/// +/// Each view (camera, or shadow-casting light, etc.) can have one or multiple render phases. +/// They are used to queue entities for rendering. +/// Multiple phases might be required due to different sorting/batching behaviors +/// (e.g. opaque: front to back, transparent: back to front) or because one phase depends on +/// the rendered texture of the previous phase (e.g. for screen-space reflections). +/// All [`PhaseItem`]s are then rendered using a single [`TrackedRenderPass`]. +/// The render pass might be reused for multiple phases to reduce GPU overhead. +/// +/// This flavor of render phase is used only for meshes that need to be sorted +/// back-to-front, such as transparent meshes. For items that don't need strict +/// sorting, [`BinnedRenderPhase`] is preferred, for performance. +pub struct SortedRenderPhase +where + I: SortedPhaseItem, +{ + /// The items within this [`SortedRenderPhase`]. + pub items: Vec, +} + +impl Default for SortedRenderPhase +where + I: SortedPhaseItem, +{ + fn default() -> Self { + Self { items: Vec::new() } + } +} + +impl SortedRenderPhase +where + I: SortedPhaseItem, +{ + /// Adds a [`PhaseItem`] to this render phase. + #[inline] + pub fn add(&mut self, item: I) { + self.items.push(item); + } + + /// Removes all [`PhaseItem`]s from this render phase. + #[inline] + pub fn clear(&mut self) { + self.items.clear(); + } + + /// Sorts all of its [`PhaseItem`]s. + pub fn sort(&mut self) { + I::sort(&mut self.items); + } + + /// An [`Iterator`] through the associated [`Entity`] for each [`PhaseItem`] in order. + #[inline] + pub fn iter_entities(&'_ self) -> impl Iterator + '_ { + self.items.iter().map(PhaseItem::entity) + } + + /// Renders all of its [`PhaseItem`]s using their corresponding draw functions. + pub fn render<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + ) -> Result<(), DrawError> { + self.render_range(render_pass, world, view, ..) + } + + /// Renders all [`PhaseItem`]s in the provided `range` (based on their index in `self.items`) using their corresponding draw functions. + pub fn render_range<'w>( + &self, + render_pass: &mut TrackedRenderPass<'w>, + world: &'w World, + view: Entity, + range: impl SliceIndex<[I], Output = [I]>, + ) -> Result<(), DrawError> { + let items = self + .items + .get(range) + .expect("`Range` provided to `render_range()` is out of bounds"); + + let draw_functions = world.resource::>(); + let mut draw_functions = draw_functions.write(); + draw_functions.prepare(world); + + let mut index = 0; + while index < items.len() { + let item = &items[index]; + let batch_range = item.batch_range(); + if batch_range.is_empty() { + index += 1; + } else { + let draw_function = draw_functions.get_mut(item.draw_function()).unwrap(); + draw_function.draw(world, render_pass, view, item)?; + index += batch_range.len(); + } + } + Ok(()) + } +} + +/// An item (entity of the render world) which will be drawn to a texture or the screen, +/// as part of a render phase. +/// +/// The data required for rendering an entity is extracted from the main world in the +/// [`ExtractSchedule`](crate::ExtractSchedule). +/// Then it has to be queued up for rendering during the [`RenderSystems::Queue`], +/// by adding a corresponding phase item to a render phase. +/// Afterwards it will be possibly sorted and rendered automatically in the +/// [`RenderSystems::PhaseSort`] and [`RenderSystems::Render`], respectively. +/// +/// `PhaseItem`s come in two flavors: [`BinnedPhaseItem`]s and +/// [`SortedPhaseItem`]s. +/// +/// * Binned phase items have a `BinKey` which specifies what bin they're to be +/// placed in. All items in the same bin are eligible to be batched together. +/// The `BinKey`s are sorted, but the individual bin items aren't. Binned phase +/// items are good for opaque meshes, in which the order of rendering isn't +/// important. Generally, binned phase items are faster than sorted phase items. +/// +/// * Sorted phase items, on the other hand, are placed into one large buffer +/// and then sorted all at once. This is needed for transparent meshes, which +/// have to be sorted back-to-front to render with the painter's algorithm. +/// These types of phase items are generally slower than binned phase items. +pub trait PhaseItem: Sized + Send + Sync + 'static { + /// Whether or not this `PhaseItem` should be subjected to automatic batching. (Default: `true`) + const AUTOMATIC_BATCHING: bool = true; + + /// The corresponding entity that will be drawn. + /// + /// This is used to fetch the render data of the entity, required by the draw function, + /// from the render world . + fn entity(&self) -> Entity; + + /// The main world entity represented by this `PhaseItem`. + fn main_entity(&self) -> MainEntity; + + /// Specifies the [`Draw`] function used to render the item. + fn draw_function(&self) -> DrawFunctionId; + + /// The range of instances that the batch covers. After doing a batched draw, batch range + /// length phase items will be skipped. This design is to avoid having to restructure the + /// render phase unnecessarily. + fn batch_range(&self) -> &Range; + fn batch_range_mut(&mut self) -> &mut Range; + + /// Returns the [`PhaseItemExtraIndex`]. + /// + /// If present, this is either a dynamic offset or an indirect parameters + /// index. + fn extra_index(&self) -> PhaseItemExtraIndex; + + /// Returns a pair of mutable references to both the batch range and extra + /// index. + fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range, &mut PhaseItemExtraIndex); +} + +/// The "extra index" associated with some [`PhaseItem`]s, alongside the +/// indirect instance index. +/// +/// Sometimes phase items require another index in addition to the range of +/// instances they already have. These can be: +/// +/// * The *dynamic offset*: a `wgpu` dynamic offset into the uniform buffer of +/// instance data. This is used on platforms that don't support storage +/// buffers, to work around uniform buffer size limitations. +/// +/// * The *indirect parameters index*: an index into the buffer that specifies +/// the indirect parameters for this [`PhaseItem`]'s drawcall. This is used when +/// indirect mode is on (as used for GPU culling). +/// +/// Note that our indirect draw functionality requires storage buffers, so it's +/// impossible to have both a dynamic offset and an indirect parameters index. +/// This convenient fact allows us to pack both indices into a single `u32`. +#[derive(Clone, PartialEq, Eq, Hash, Debug)] +pub enum PhaseItemExtraIndex { + /// No extra index is present. + None, + /// A `wgpu` dynamic offset into the uniform buffer of instance data. This + /// is used on platforms that don't support storage buffers, to work around + /// uniform buffer size limitations. + DynamicOffset(u32), + /// An index into the buffer that specifies the indirect parameters for this + /// [`PhaseItem`]'s drawcall. This is used when indirect mode is on (as used + /// for GPU culling). + IndirectParametersIndex { + /// The range of indirect parameters within the indirect parameters array. + /// + /// If we're using `multi_draw_indirect_count`, this specifies the + /// maximum range of indirect parameters within that array. If batches + /// are ultimately culled out on the GPU, the actual number of draw + /// commands might be lower than the length of this range. + range: Range, + /// If `multi_draw_indirect_count` is in use, and this phase item is + /// part of a batch set, specifies the index of the batch set that this + /// phase item is a part of. + /// + /// If `multi_draw_indirect_count` isn't in use, or this phase item + /// isn't part of a batch set, this is `None`. + batch_set_index: Option, + }, +} + +impl PhaseItemExtraIndex { + /// Returns either an indirect parameters index or + /// [`PhaseItemExtraIndex::None`], as appropriate. + pub fn maybe_indirect_parameters_index( + indirect_parameters_index: Option, + ) -> PhaseItemExtraIndex { + match indirect_parameters_index { + Some(indirect_parameters_index) => PhaseItemExtraIndex::IndirectParametersIndex { + range: u32::from(indirect_parameters_index) + ..(u32::from(indirect_parameters_index) + 1), + batch_set_index: None, + }, + None => PhaseItemExtraIndex::None, + } + } + + /// Returns either a dynamic offset index or [`PhaseItemExtraIndex::None`], + /// as appropriate. + pub fn maybe_dynamic_offset(dynamic_offset: Option) -> PhaseItemExtraIndex { + match dynamic_offset { + Some(dynamic_offset) => PhaseItemExtraIndex::DynamicOffset(dynamic_offset.into()), + None => PhaseItemExtraIndex::None, + } + } +} + +/// Represents phase items that are placed into bins. The `BinKey` specifies +/// which bin they're to be placed in. Bin keys are sorted, and items within the +/// same bin are eligible to be batched together. The elements within the bins +/// aren't themselves sorted. +/// +/// An example of a binned phase item is `Opaque3d`, for which the rendering +/// order isn't critical. +pub trait BinnedPhaseItem: PhaseItem { + /// The key used for binning [`PhaseItem`]s into bins. Order the members of + /// [`BinnedPhaseItem::BinKey`] by the order of binding for best + /// performance. For example, pipeline id, draw function id, mesh asset id, + /// lowest variable bind group id such as the material bind group id, and + /// its dynamic offsets if any, next bind group and offsets, etc. This + /// reduces the need for rebinding between bins and improves performance. + type BinKey: Clone + Send + Sync + PartialEq + Eq + Ord + Hash; + + /// The key used to combine batches into batch sets. + /// + /// A *batch set* is a set of meshes that can potentially be multi-drawn + /// together. + type BatchSetKey: PhaseItemBatchSetKey; + + /// Creates a new binned phase item from the key and per-entity data. + /// + /// Unlike [`SortedPhaseItem`]s, this is generally called "just in time" + /// before rendering. The resulting phase item isn't stored in any data + /// structures, resulting in significant memory savings. + fn new( + batch_set_key: Self::BatchSetKey, + bin_key: Self::BinKey, + representative_entity: (Entity, MainEntity), + batch_range: Range, + extra_index: PhaseItemExtraIndex, + ) -> Self; +} + +/// A key used to combine batches into batch sets. +/// +/// A *batch set* is a set of meshes that can potentially be multi-drawn +/// together. +pub trait PhaseItemBatchSetKey: Clone + Send + Sync + PartialEq + Eq + Ord + Hash { + /// Returns true if this batch set key describes indexed meshes or false if + /// it describes non-indexed meshes. + /// + /// Bevy uses this in order to determine which kind of indirect draw + /// parameters to use, if indirect drawing is enabled. + fn indexed(&self) -> bool; +} + +/// Represents phase items that must be sorted. The `SortKey` specifies the +/// order that these items are drawn in. These are placed into a single array, +/// and the array as a whole is then sorted. +/// +/// An example of a sorted phase item is `Transparent3d`, which must be sorted +/// back to front in order to correctly render with the painter's algorithm. +pub trait SortedPhaseItem: PhaseItem { + /// The type used for ordering the items. The smallest values are drawn first. + /// This order can be calculated using the [`ViewRangefinder3d`], + /// based on the view-space `Z` value of the corresponding view matrix. + type SortKey: Ord; + + /// Determines the order in which the items are drawn. + fn sort_key(&self) -> Self::SortKey; + + /// Sorts a slice of phase items into render order. Generally if the same type + /// is batched this should use a stable sort like [`slice::sort_by_key`]. + /// In almost all other cases, this should not be altered from the default, + /// which uses an unstable sort, as this provides the best balance of CPU and GPU + /// performance. + /// + /// Implementers can optionally not sort the list at all. This is generally advisable if and + /// only if the renderer supports a depth prepass, which is by default not supported by + /// the rest of Bevy's first party rendering crates. Even then, this may have a negative + /// impact on GPU-side performance due to overdraw. + /// + /// It's advised to always profile for performance changes when changing this implementation. + #[inline] + fn sort(items: &mut [Self]) { + items.sort_unstable_by_key(Self::sort_key); + } + + /// Whether this phase item targets indexed meshes (those with both vertex + /// and index buffers as opposed to just vertex buffers). + /// + /// Bevy needs this information in order to properly group phase items + /// together for multi-draw indirect, because the GPU layout of indirect + /// commands differs between indexed and non-indexed meshes. + /// + /// If you're implementing a custom phase item that doesn't describe a mesh, + /// you can safely return false here. + fn indexed(&self) -> bool; +} + +/// A [`PhaseItem`] item, that automatically sets the appropriate render pipeline, +/// cached in the [`PipelineCache`]. +/// +/// You can use the [`SetItemPipeline`] render command to set the pipeline for this item. +pub trait CachedRenderPipelinePhaseItem: PhaseItem { + /// The id of the render pipeline, cached in the [`PipelineCache`], that will be used to draw + /// this phase item. + fn cached_pipeline(&self) -> CachedRenderPipelineId; +} + +/// A [`RenderCommand`] that sets the pipeline for the [`CachedRenderPipelinePhaseItem`]. +pub struct SetItemPipeline; + +impl RenderCommand

for SetItemPipeline { + type Param = SRes; + type ViewQuery = (); + type ItemQuery = (); + #[inline] + fn render<'w>( + item: &P, + _view: (), + _entity: Option<()>, + pipeline_cache: SystemParamItem<'w, '_, Self::Param>, + pass: &mut TrackedRenderPass<'w>, + ) -> RenderCommandResult { + if let Some(pipeline) = pipeline_cache + .into_inner() + .get_render_pipeline(item.cached_pipeline()) + { + pass.set_render_pipeline(pipeline); + RenderCommandResult::Success + } else { + RenderCommandResult::Skip + } + } +} + +/// This system sorts the [`PhaseItem`]s of all [`SortedRenderPhase`]s of this +/// type. +pub fn sort_phase_system(mut render_phases: ResMut>) +where + I: SortedPhaseItem, +{ + for phase in render_phases.values_mut() { + phase.sort(); + } +} + +/// Removes entities that became invisible or changed phases from the bins. +/// +/// This must run after queuing. +pub fn sweep_old_entities(mut render_phases: ResMut>) +where + BPI: BinnedPhaseItem, +{ + for phase in render_phases.0.values_mut() { + phase.sweep_old_entities(); + } +} + +impl BinnedRenderPhaseType { + pub fn mesh( + batchable: bool, + gpu_preprocessing_support: &GpuPreprocessingSupport, + ) -> BinnedRenderPhaseType { + match (batchable, gpu_preprocessing_support.max_supported_mode) { + (true, GpuPreprocessingMode::Culling) => BinnedRenderPhaseType::MultidrawableMesh, + (true, _) => BinnedRenderPhaseType::BatchableMesh, + (false, _) => BinnedRenderPhaseType::UnbatchableMesh, + } + } +} + +impl RenderBin { + /// Creates a [`RenderBin`] containing a single entity. + fn from_entity(entity: MainEntity, uniform_index: InputUniformIndex) -> RenderBin { + let mut entities = IndexMap::default(); + entities.insert(entity, uniform_index); + RenderBin { entities } + } + + /// Inserts an entity into the bin. + fn insert(&mut self, entity: MainEntity, uniform_index: InputUniformIndex) { + self.entities.insert(entity, uniform_index); + } + + /// Removes an entity from the bin. + fn remove(&mut self, entity_to_remove: MainEntity) { + self.entities.swap_remove(&entity_to_remove); + } + + /// Returns true if the bin contains no entities. + fn is_empty(&self) -> bool { + self.entities.is_empty() + } + + /// Returns the [`IndexMap`] containing all the entities in the bin, along + /// with the cached [`InputUniformIndex`] of each. + #[inline] + pub fn entities(&self) -> &IndexMap { + &self.entities + } +} + +/// An iterator that efficiently finds the indices of all zero bits in a +/// [`FixedBitSet`] and returns them in reverse order. +/// +/// [`FixedBitSet`] doesn't natively offer this functionality, so we have to +/// implement it ourselves. +#[derive(Debug)] +struct ReverseFixedBitSetZeroesIterator<'a> { + /// The bit set. + bitset: &'a FixedBitSet, + /// The next bit index we're going to scan when [`Iterator::next`] is + /// called. + bit_index: isize, +} + +impl<'a> ReverseFixedBitSetZeroesIterator<'a> { + fn new(bitset: &'a FixedBitSet) -> ReverseFixedBitSetZeroesIterator<'a> { + ReverseFixedBitSetZeroesIterator { + bitset, + bit_index: (bitset.len() as isize) - 1, + } + } +} + +impl<'a> Iterator for ReverseFixedBitSetZeroesIterator<'a> { + type Item = usize; + + fn next(&mut self) -> Option { + while self.bit_index >= 0 { + // Unpack the bit index into block and bit. + let block_index = self.bit_index / (Block::BITS as isize); + let bit_pos = self.bit_index % (Block::BITS as isize); + + // Grab the block. Mask off all bits above the one we're scanning + // from by setting them all to 1. + let mut block = self.bitset.as_slice()[block_index as usize]; + if bit_pos + 1 < (Block::BITS as isize) { + block |= (!0) << (bit_pos + 1); + } + + // Search for the next unset bit. Note that the `leading_ones` + // function counts from the MSB to the LSB, so we need to flip it to + // get the bit number. + let pos = (Block::BITS as isize) - (block.leading_ones() as isize) - 1; + + // If we found an unset bit, return it. + if pos != -1 { + let result = block_index * (Block::BITS as isize) + pos; + self.bit_index = result - 1; + return Some(result as usize); + } + + // Otherwise, go to the previous block. + self.bit_index = block_index * (Block::BITS as isize) - 1; + } + + None + } +} + +#[cfg(test)] +mod test { + use super::ReverseFixedBitSetZeroesIterator; + use fixedbitset::FixedBitSet; + use proptest::{collection::vec, prop_assert_eq, proptest}; + + proptest! { + #[test] + fn reverse_fixed_bit_set_zeroes_iterator( + bits in vec(0usize..1024usize, 0usize..1024usize), + size in 0usize..1024usize, + ) { + // Build a random bit set. + let mut bitset = FixedBitSet::new(); + bitset.grow(size); + for bit in bits { + if bit < size { + bitset.set(bit, true); + } + } + + // Iterate over the bit set backwards in a naive way, and check that + // that iteration sequence corresponds to the optimized one. + let mut iter = ReverseFixedBitSetZeroesIterator::new(&bitset); + for bit_index in (0..size).rev() { + if !bitset.contains(bit_index) { + prop_assert_eq!(iter.next(), Some(bit_index)); + } + } + + prop_assert_eq!(iter.next(), None); + } + } +} diff --git a/third_party/bevy_render/src/render_phase/rangefinder.rs b/third_party/bevy_render/src/render_phase/rangefinder.rs new file mode 100644 index 0000000..bc4fa30 --- /dev/null +++ b/third_party/bevy_render/src/render_phase/rangefinder.rs @@ -0,0 +1,39 @@ +use bevy_math::{Affine3A, Mat4, Vec3, Vec4}; + +/// A distance calculator for the draw order of [`PhaseItem`](crate::render_phase::PhaseItem)s. +pub struct ViewRangefinder3d { + view_from_world_row_2: Vec4, +} + +impl ViewRangefinder3d { + /// Creates a 3D rangefinder for a view matrix. + pub fn from_world_from_view(world_from_view: &Affine3A) -> ViewRangefinder3d { + let view_from_world = world_from_view.inverse(); + + ViewRangefinder3d { + view_from_world_row_2: Mat4::from(view_from_world).row(2), + } + } + + /// Calculates the distance, or view-space `Z` value, for the given world-space `position`. + #[inline] + pub fn distance(&self, position: &Vec3) -> f32 { + // NOTE: row 2 of the inverse view matrix dotted with the world-space position + // gives the z component of the point in view-space + self.view_from_world_row_2.dot(position.extend(1.0)) + } +} + +#[cfg(test)] +mod tests { + use super::ViewRangefinder3d; + use bevy_math::{Affine3A, Vec3}; + + #[test] + fn distance() { + let view_matrix = Affine3A::from_translation(Vec3::new(0.0, 0.0, -1.0)); + let rangefinder = ViewRangefinder3d::from_world_from_view(&view_matrix); + assert_eq!(rangefinder.distance(&Vec3::new(0., 0., 0.)), 1.0); + assert_eq!(rangefinder.distance(&Vec3::new(0., 0., 1.)), 2.0); + } +} diff --git a/third_party/bevy_render/src/render_resource/batched_uniform_buffer.rs b/third_party/bevy_render/src/render_resource/batched_uniform_buffer.rs new file mode 100644 index 0000000..ad36839 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/batched_uniform_buffer.rs @@ -0,0 +1,157 @@ +use super::{GpuArrayBufferIndex, GpuArrayBufferable}; +use crate::{ + render_resource::DynamicUniformBuffer, + renderer::{RenderDevice, RenderQueue}, +}; +use core::{marker::PhantomData, num::NonZero}; +use encase::{ + private::{ArrayMetadata, BufferMut, Metadata, RuntimeSizedArray, WriteInto, Writer}, + ShaderType, +}; +use nonmax::NonMaxU32; +use wgpu::{BindingResource, Limits}; + +// 1MB else we will make really large arrays on macOS which reports very large +// `max_uniform_buffer_binding_size`. On macOS this ends up being the minimum +// size of the uniform buffer as well as the size of each chunk of data at a +// dynamic offset. +#[cfg(any( + not(feature = "webgl"), + not(target_arch = "wasm32"), + feature = "webgpu" +))] +const MAX_REASONABLE_UNIFORM_BUFFER_BINDING_SIZE: u32 = 1 << 20; + +// WebGL2 quirk: using uniform buffers larger than 4KB will cause extremely +// long shader compilation times, so the limit needs to be lower on WebGL2. +// This is due to older shader compilers/GPUs that don't support dynamically +// indexing uniform buffers, and instead emulate it with large switch statements +// over buffer indices that take a long time to compile. +#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))] +const MAX_REASONABLE_UNIFORM_BUFFER_BINDING_SIZE: u32 = 1 << 12; + +/// Similar to [`DynamicUniformBuffer`], except every N elements (depending on size) +/// are grouped into a batch as an `array` in WGSL. +/// +/// This reduces the number of rebindings required due to having to pass dynamic +/// offsets to bind group commands, and if indices into the array can be passed +/// in via other means, it enables batching of draw commands. +pub struct BatchedUniformBuffer { + // Batches of fixed-size arrays of T are written to this buffer so that + // each batch in a fixed-size array can be bound at a dynamic offset. + uniforms: DynamicUniformBuffer>>, + // A batch of T are gathered into this `MaxCapacityArray` until it is full, + // then it is written into the `DynamicUniformBuffer`, cleared, and new T + // are gathered here, and so on for each batch. + temp: MaxCapacityArray>, + current_offset: u32, + dynamic_offset_alignment: u32, +} + +impl BatchedUniformBuffer { + pub fn batch_size(limits: &Limits) -> usize { + (limits + .max_uniform_buffer_binding_size + .min(MAX_REASONABLE_UNIFORM_BUFFER_BINDING_SIZE) as u64 + / T::min_size().get()) as usize + } + + pub fn new(limits: &Limits) -> Self { + let capacity = Self::batch_size(limits); + let alignment = limits.min_uniform_buffer_offset_alignment; + + Self { + uniforms: DynamicUniformBuffer::new_with_alignment(alignment as u64), + temp: MaxCapacityArray(Vec::with_capacity(capacity), capacity), + current_offset: 0, + dynamic_offset_alignment: alignment, + } + } + + #[inline] + pub fn size(&self) -> NonZero { + self.temp.size() + } + + pub fn clear(&mut self) { + self.uniforms.clear(); + self.current_offset = 0; + self.temp.0.clear(); + } + + pub fn push(&mut self, component: T) -> GpuArrayBufferIndex { + let result = GpuArrayBufferIndex { + index: self.temp.0.len() as u32, + dynamic_offset: NonMaxU32::new(self.current_offset), + element_type: PhantomData, + }; + self.temp.0.push(component); + if self.temp.0.len() == self.temp.1 { + self.flush(); + } + result + } + + pub fn flush(&mut self) { + self.uniforms.push(&self.temp); + + self.current_offset += + align_to_next(self.temp.size().get(), self.dynamic_offset_alignment as u64) as u32; + + self.temp.0.clear(); + } + + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + if !self.temp.0.is_empty() { + self.flush(); + } + self.uniforms.write_buffer(device, queue); + } + + #[inline] + pub fn binding(&self) -> Option> { + let mut binding = self.uniforms.binding(); + if let Some(BindingResource::Buffer(binding)) = &mut binding { + // MaxCapacityArray is runtime-sized so can't use T::min_size() + binding.size = Some(self.size()); + } + binding + } +} + +#[inline] +fn align_to_next(value: u64, alignment: u64) -> u64 { + debug_assert!(alignment.is_power_of_two()); + ((value - 1) | (alignment - 1)) + 1 +} + +// ---------------------------------------------------------------------------- +// MaxCapacityArray was implemented by Teodor Tanasoaia for encase. It was +// copied here as it was not yet included in an encase release and it is +// unclear if it is the correct long-term solution for encase. + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] +struct MaxCapacityArray(T, usize); + +impl ShaderType for MaxCapacityArray +where + T: ShaderType, +{ + type ExtraMetadata = ArrayMetadata; + + const METADATA: Metadata = T::METADATA; + + fn size(&self) -> NonZero { + Self::METADATA.stride().mul(self.1.max(1) as u64).0 + } +} + +impl WriteInto for MaxCapacityArray +where + T: WriteInto + RuntimeSizedArray, +{ + fn write_into(&self, writer: &mut Writer) { + debug_assert!(self.0.len() <= self.1); + self.0.write_into(writer); + } +} diff --git a/third_party/bevy_render/src/render_resource/bind_group.rs b/third_party/bevy_render/src/render_resource/bind_group.rs new file mode 100644 index 0000000..fe750e7 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/bind_group.rs @@ -0,0 +1,740 @@ +use crate::{ + define_atomic_id, + render_asset::RenderAssets, + render_resource::{BindGroupLayout, Buffer, PipelineCache, Sampler, TextureView}, + renderer::{RenderDevice, WgpuWrapper}, + texture::GpuImage, +}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::system::{SystemParam, SystemParamItem}; +use bevy_render::render_resource::BindGroupLayoutDescriptor; +pub use bevy_render_macros::AsBindGroup; +use core::ops::Deref; +use encase::ShaderType; +use thiserror::Error; +use wgpu::{ + BindGroupEntry, BindGroupLayoutEntry, BindingResource, SamplerBindingType, TextureViewDimension, +}; + +use super::{BindlessDescriptor, BindlessSlabResourceLimit}; + +define_atomic_id!(BindGroupId); + +/// Bind groups are responsible for binding render resources (e.g. buffers, textures, samplers) +/// to a [`TrackedRenderPass`](crate::render_phase::TrackedRenderPass). +/// This makes them accessible in the pipeline (shaders) as uniforms. +/// +/// This is a lightweight thread-safe wrapper around wgpu's own [`BindGroup`](wgpu::BindGroup), +/// which can be cloned as needed to workaround lifetime management issues. It may be converted +/// from and dereferences to wgpu's [`BindGroup`](wgpu::BindGroup). +/// +/// Can be created via [`RenderDevice::create_bind_group`](RenderDevice::create_bind_group). +#[derive(Clone, Debug)] +pub struct BindGroup { + id: BindGroupId, + value: WgpuWrapper, +} + +impl BindGroup { + /// Returns the [`BindGroupId`] representing the unique ID of the bind group. + #[inline] + pub fn id(&self) -> BindGroupId { + self.id + } +} + +impl PartialEq for BindGroup { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Eq for BindGroup {} + +impl core::hash::Hash for BindGroup { + fn hash(&self, state: &mut H) { + self.id.0.hash(state); + } +} + +impl From for BindGroup { + fn from(value: wgpu::BindGroup) -> Self { + BindGroup { + id: BindGroupId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl<'a> From<&'a BindGroup> for Option<&'a wgpu::BindGroup> { + fn from(value: &'a BindGroup) -> Self { + Some(value.deref()) + } +} + +impl<'a> From<&'a mut BindGroup> for Option<&'a wgpu::BindGroup> { + fn from(value: &'a mut BindGroup) -> Self { + Some(&*value) + } +} + +impl Deref for BindGroup { + type Target = wgpu::BindGroup; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +/// Converts a value to a [`BindGroup`] with a given [`BindGroupLayout`], which can then be used in Bevy shaders. +/// This trait can be derived (and generally should be). Read on for details and examples. +/// +/// This is an opinionated trait that is intended to make it easy to generically +/// convert a type into a [`BindGroup`]. It provides access to specific render resources, +/// such as [`RenderAssets`] and [`crate::texture::FallbackImage`]. If a type has a [`Handle`](bevy_asset::Handle), +/// these can be used to retrieve the corresponding [`Texture`](crate::render_resource::Texture) resource. +/// +/// [`AsBindGroup::as_bind_group`] is intended to be called once, then the result cached somewhere. It is generally +/// ok to do "expensive" work here, such as creating a [`Buffer`] for a uniform. +/// +/// If for some reason a [`BindGroup`] cannot be created yet (for example, the [`Texture`](crate::render_resource::Texture) +/// for an [`Image`](bevy_image::Image) hasn't loaded yet), just return [`AsBindGroupError::RetryNextUpdate`], which signals that the caller +/// should retry again later. +/// +/// # Deriving +/// +/// This trait can be derived. Field attributes like `uniform` and `texture` are used to define which fields should be bindings, +/// what their binding type is, and what index they should be bound at: +/// +/// ``` +/// # use bevy_render::render_resource::*; +/// # use bevy_image::Image; +/// # use bevy_color::LinearRgba; +/// # use bevy_asset::Handle; +/// # use bevy_render::storage::ShaderStorageBuffer; +/// +/// #[derive(AsBindGroup)] +/// struct CoolMaterial { +/// #[uniform(0)] +/// color: LinearRgba, +/// #[texture(1)] +/// #[sampler(2)] +/// color_texture: Handle, +/// #[storage(3, read_only)] +/// storage_buffer: Handle, +/// #[storage(4, read_only, buffer)] +/// raw_buffer: Buffer, +/// #[storage_texture(5)] +/// storage_texture: Handle, +/// } +/// ``` +/// +/// In WGSL shaders, the binding would look like this: +/// +/// ```wgsl +/// @group(#{MATERIAL_BIND_GROUP}) @binding(0) var color: vec4; +/// @group(#{MATERIAL_BIND_GROUP}) @binding(1) var color_texture: texture_2d; +/// @group(#{MATERIAL_BIND_GROUP}) @binding(2) var color_sampler: sampler; +/// @group(#{MATERIAL_BIND_GROUP}) @binding(3) var storage_buffer: array; +/// @group(#{MATERIAL_BIND_GROUP}) @binding(4) var raw_buffer: array; +/// @group(#{MATERIAL_BIND_GROUP}) @binding(5) var storage_texture: texture_storage_2d; +/// ``` +/// Note that the "group" index is determined by the usage context. It is not defined in [`AsBindGroup`]. For example, in Bevy material bind groups +/// are generally bound to group 2. +/// +/// The following field-level attributes are supported: +/// +/// ## `uniform(BINDING_INDEX)` +/// +/// * The field will be converted to a shader-compatible type using the [`ShaderType`] trait, written to a [`Buffer`], and bound as a uniform. +/// [`ShaderType`] is implemented for most math types already, such as [`f32`], [`Vec4`](bevy_math::Vec4), and +/// [`LinearRgba`](bevy_color::LinearRgba). It can also be derived for custom structs. +/// +/// ## `texture(BINDING_INDEX, arguments)` +/// +/// * This field's [`Handle`](bevy_asset::Handle) will be used to look up the matching [`Texture`](crate::render_resource::Texture) +/// GPU resource, which will be bound as a texture in shaders. The field will be assumed to implement [`Into>>`]. In practice, +/// most fields should be a [`Handle`](bevy_asset::Handle) or [`Option>`]. If the value of an [`Option>`] is +/// [`None`], the [`crate::texture::FallbackImage`] resource will be used instead. This attribute can be used in conjunction with a `sampler` binding attribute +/// (with a different binding index) if a binding of the sampler for the [`Image`](bevy_image::Image) is also required. +/// +/// | Arguments | Values | Default | +/// |-----------------------|-------------------------------------------------------------------------|----------------------| +/// | `dimension` = "..." | `"1d"`, `"2d"`, `"2d_array"`, `"3d"`, `"cube"`, `"cube_array"` | `"2d"` | +/// | `sample_type` = "..." | `"float"`, `"depth"`, `"s_int"` or `"u_int"` | `"float"` | +/// | `filterable` = ... | `true`, `false` | `true` | +/// | `multisampled` = ... | `true`, `false` | `false` | +/// | `visibility(...)` | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `vertex`, `fragment` | +/// +/// ## `storage_texture(BINDING_INDEX, arguments)` +/// +/// * This field's [`Handle`](bevy_asset::Handle) will be used to look up the matching [`Texture`](crate::render_resource::Texture) +/// GPU resource, which will be bound as a storage texture in shaders. The field will be assumed to implement [`Into>>`]. In practice, +/// most fields should be a [`Handle`](bevy_asset::Handle) or [`Option>`]. If the value of an [`Option>`] is +/// [`None`], the [`crate::texture::FallbackImage`] resource will be used instead. +/// +/// | Arguments | Values | Default | +/// |------------------------|--------------------------------------------------------------------------------------------|---------------| +/// | `dimension` = "..." | `"1d"`, `"2d"`, `"2d_array"`, `"3d"`, `"cube"`, `"cube_array"` | `"2d"` | +/// | `image_format` = ... | any member of [`TextureFormat`](crate::render_resource::TextureFormat) | `Rgba8Unorm` | +/// | `access` = ... | any member of [`StorageTextureAccess`](crate::render_resource::StorageTextureAccess) | `ReadWrite` | +/// | `visibility(...)` | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `compute` | +/// +/// ## `sampler(BINDING_INDEX, arguments)` +/// +/// * This field's [`Handle`](bevy_asset::Handle) will be used to look up the matching [`Sampler`] GPU +/// resource, which will be bound as a sampler in shaders. The field will be assumed to implement [`Into>>`]. In practice, +/// most fields should be a [`Handle`](bevy_asset::Handle) or [`Option>`]. If the value of an [`Option>`] is +/// [`None`], the [`crate::texture::FallbackImage`] resource will be used instead. This attribute can be used in conjunction with a `texture` binding attribute +/// (with a different binding index) if a binding of the texture for the [`Image`](bevy_image::Image) is also required. +/// +/// | Arguments | Values | Default | +/// |------------------------|-------------------------------------------------------------------------|------------------------| +/// | `sampler_type` = "..." | `"filtering"`, `"non_filtering"`, `"comparison"`. | `"filtering"` | +/// | `visibility(...)` | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `vertex`, `fragment` | +/// +/// ## `storage(BINDING_INDEX, arguments)` +/// +/// * The field's [`Handle`](bevy_asset::Handle) will be used to look +/// up the matching [`Buffer`] GPU resource, which will be bound as a storage +/// buffer in shaders. If the `storage` attribute is used, the field is expected +/// a raw buffer, and the buffer will be bound as a storage buffer in shaders. +/// In bindless mode, `binding_array()` argument that specifies the binding +/// number of the resulting storage buffer binding array must be present. +/// +/// | Arguments | Values | Default | +/// |------------------------|-------------------------------------------------------------------------|------------------------| +/// | `visibility(...)` | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `vertex`, `fragment` | +/// | `read_only` | if present then value is true, otherwise false | `false` | +/// | `buffer` | if present then the field will be assumed to be a raw wgpu buffer | | +/// | `binding_array(...)` | the binding number of the binding array, for bindless mode | bindless mode disabled | +/// +/// Note that fields without field-level binding attributes will be ignored. +/// ``` +/// # use bevy_render::{render_resource::AsBindGroup}; +/// # use bevy_color::LinearRgba; +/// # use bevy_asset::Handle; +/// #[derive(AsBindGroup)] +/// struct CoolMaterial { +/// #[uniform(0)] +/// color: LinearRgba, +/// this_field_is_ignored: String, +/// } +/// ``` +/// +/// As mentioned above, [`Option>`] is also supported: +/// ``` +/// # use bevy_asset::Handle; +/// # use bevy_color::LinearRgba; +/// # use bevy_image::Image; +/// # use bevy_render::render_resource::AsBindGroup; +/// #[derive(AsBindGroup)] +/// struct CoolMaterial { +/// #[uniform(0)] +/// color: LinearRgba, +/// #[texture(1)] +/// #[sampler(2)] +/// color_texture: Option>, +/// } +/// ``` +/// This is useful if you want a texture to be optional. When the value is [`None`], the [`crate::texture::FallbackImage`] will be used for the binding instead, which defaults +/// to "pure white". +/// +/// Field uniforms with the same index will be combined into a single binding: +/// ``` +/// # use bevy_render::{render_resource::AsBindGroup}; +/// # use bevy_color::LinearRgba; +/// #[derive(AsBindGroup)] +/// struct CoolMaterial { +/// #[uniform(0)] +/// color: LinearRgba, +/// #[uniform(0)] +/// roughness: f32, +/// } +/// ``` +/// +/// In WGSL shaders, the binding would look like this: +/// ```wgsl +/// struct CoolMaterial { +/// color: vec4, +/// roughness: f32, +/// }; +/// +/// @group(#{MATERIAL_BIND_GROUP}) @binding(0) var material: CoolMaterial; +/// ``` +/// +/// Some less common scenarios will require "struct-level" attributes. These are the currently supported struct-level attributes: +/// ## `uniform(BINDING_INDEX, ConvertedShaderType)` +/// +/// * This also creates a [`Buffer`] using [`ShaderType`] and binds it as a +/// uniform, much like the field-level `uniform` attribute. The difference is +/// that the entire [`AsBindGroup`] value is converted to `ConvertedShaderType`, +/// which must implement [`ShaderType`], instead of a specific field +/// implementing [`ShaderType`]. This is useful if more complicated conversion +/// logic is required, or when using bindless mode (see below). The conversion +/// is done using the [`AsBindGroupShaderType`] trait, +/// which is automatically implemented if `&Self` implements +/// [`Into`]. Outside of bindless mode, only use +/// [`AsBindGroupShaderType`] if access to resources like +/// [`RenderAssets`] is required. +/// +/// * In bindless mode (see `bindless(COUNT)`), this attribute becomes +/// `uniform(BINDLESS_INDEX, ConvertedShaderType, +/// binding_array(BINDING_INDEX))`. The resulting uniform buffers will be +/// available in the shader as a binding array at the given `BINDING_INDEX`. The +/// `BINDLESS_INDEX` specifies the offset of the buffer in the bindless index +/// table. +/// +/// For example, suppose that the material slot is stored in a variable named +/// `slot`, the bindless index table is named `material_indices`, and that the +/// first field (index 0) of the bindless index table type is named +/// `material`. Then specifying `#[uniform(0, StandardMaterialUniform, +/// binding_array(10)]` will create a binding array buffer declared in the +/// shader as `var material_array: +/// binding_array` and accessible as +/// `material_array[material_indices[slot].material]`. +/// +/// ## `data(BINDING_INDEX, ConvertedShaderType, binding_array(BINDING_INDEX))` +/// +/// * This is very similar to `uniform(BINDING_INDEX, ConvertedShaderType, +/// binding_array(BINDING_INDEX)` and in fact is identical if bindless mode +/// isn't being used. The difference is that, in bindless mode, the `data` +/// attribute produces a single buffer containing an array, not an array of +/// buffers. For example, suppose you had the following declaration: +/// +/// ```ignore +/// #[uniform(0, StandardMaterialUniform, binding_array(10))] +/// struct StandardMaterial { ... } +/// ``` +/// +/// In bindless mode, this will produce a binding matching the following WGSL +/// declaration: +/// +/// ```wgsl +/// @group(#{MATERIAL_BIND_GROUP}) @binding(10) var material_array: binding_array; +/// ``` +/// +/// On the other hand, if you write this declaration: +/// +/// ```ignore +/// #[data(0, StandardMaterialUniform, binding_array(10))] +/// struct StandardMaterial { ... } +/// ``` +/// +/// Then Bevy produces a binding that matches this WGSL declaration instead: +/// +/// ```wgsl +/// @group(#{MATERIAL_BIND_GROUP}) @binding(10) var material_array: array; +/// ``` +/// +/// * Just as with the structure-level `uniform` attribute, Bevy converts the +/// entire [`AsBindGroup`] to `ConvertedShaderType`, using the +/// [`AsBindGroupShaderType`] trait. +/// +/// * In non-bindless mode, the structure-level `data` attribute is the same as +/// the structure-level `uniform` attribute and produces a single uniform buffer +/// in the shader. The above example would result in a binding that looks like +/// this in WGSL in non-bindless mode: +/// +/// ```wgsl +/// @group(#{MATERIAL_BIND_GROUP}) @binding(0) var material: StandardMaterial; +/// ``` +/// +/// * For efficiency reasons, `data` is generally preferred over `uniform` +/// unless you need to place your data in individual buffers. +/// +/// ## `bind_group_data(DataType)` +/// +/// * The [`AsBindGroup`] type will be converted to some `DataType` using [`Into`] and stored +/// as [`AsBindGroup::Data`] as part of the [`AsBindGroup::as_bind_group`] call. This is useful if data needs to be stored alongside +/// the generated bind group, such as a unique identifier for a material's bind group. The most common use case for this attribute +/// is "shader pipeline specialization". See [`SpecializedRenderPipeline`](crate::render_resource::SpecializedRenderPipeline). +/// +/// ## `bindless` +/// +/// * This switch enables *bindless resources*, which changes the way Bevy +/// supplies resources (textures, and samplers) to the shader. When bindless +/// resources are enabled, and the current platform supports them, Bevy will +/// allocate textures, and samplers into *binding arrays*, separated based on +/// type and will supply your shader with indices into those arrays. +/// * Bindless textures and samplers are placed into the appropriate global +/// array defined in `bevy_render::bindless` (`bindless.wgsl`). +/// * Bevy doesn't currently support bindless buffers, except for those created +/// with the `uniform(BINDLESS_INDEX, ConvertedShaderType, +/// binding_array(BINDING_INDEX))` attribute. If you need to include a buffer in +/// your object, and you can't create the data in that buffer with the `uniform` +/// attribute, consider a non-bindless object instead. +/// * If bindless mode is enabled, the `BINDLESS` definition will be +/// available. Because not all platforms support bindless resources, you +/// should check for the presence of this definition via `#ifdef` and fall +/// back to standard bindings if it isn't present. +/// * By default, in bindless mode, binding 0 becomes the *bindless index +/// table*, which is an array of structures, each of which contains as many +/// fields of type `u32` as the highest binding number in the structure +/// annotated with `#[derive(AsBindGroup)]`. Again by default, the *i*th field +/// of the bindless index table contains the index of the resource with binding +/// *i* within the appropriate binding array. +/// * In the case of materials, the index of the applicable table within the +/// bindless index table list corresponding to the mesh currently being drawn +/// can be retrieved with +/// `mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu`. +/// * You can limit the size of the bindless slabs to N resources with the +/// `limit(N)` declaration. For example, `#[bindless(limit(16))]` ensures that +/// each slab will have no more than 16 total resources in it. If you don't +/// specify a limit, Bevy automatically picks a reasonable one for the current +/// platform. +/// * The `index_table(range(M..N), binding(B))` declaration allows you to +/// customize the layout of the bindless index table. This is useful for +/// materials that are composed of multiple bind groups, such as +/// `ExtendedMaterial`. In such cases, there will be multiple bindless index +/// tables, so they can't both be assigned to binding 0 or their bindings will +/// conflict. +/// - The `binding(B)` attribute of the `index_table` attribute allows you to +/// customize the binding (`@binding(B)`, in the shader) at which the index +/// table will be bound. +/// - The `range(M, N)` attribute of the `index_table` attribute allows you to +/// change the mapping from the field index in the bindless index table to the +/// bindless index. Instead of the field at index $i$ being mapped to the +/// bindless index $i$, with the `range(M, N)` attribute the field at index +/// $i$ in the bindless index table is mapped to the bindless index $i$ + M. +/// The size of the index table will be set to N - M. Note that this may +/// result in the table being too small to contain all the bindless bindings. +/// * The purpose of bindless mode is to improve performance by reducing +/// state changes. By grouping resources together into binding arrays, Bevy +/// doesn't have to modify GPU state as often, decreasing API and driver +/// overhead. +/// * See the `shaders/shader_material_bindless` example for an example of how +/// to use bindless mode. See the `shaders/extended_material_bindless` example +/// for a more exotic example of bindless mode that demonstrates the +/// `index_table` attribute. +/// * The following diagram illustrates how bindless mode works using a subset +/// of `StandardMaterial`: +/// +/// ```text +/// Shader Bindings Sampler Binding Array +/// +----+-----------------------------+ +-----------+-----------+-----+ +/// +---| 0 | material_indices | +->| sampler 0 | sampler 1 | ... | +/// | +----+-----------------------------+ | +-----------+-----------+-----+ +/// | | 1 | bindless_samplers_filtering +--+ ^ +/// | +----+-----------------------------+ +-------------------------------+ +/// | | .. | ... | | +/// | +----+-----------------------------+ Texture Binding Array | +/// | | 5 | bindless_textures_2d +--+ +-----------+-----------+-----+ | +/// | +----+-----------------------------+ +->| texture 0 | texture 1 | ... | | +/// | | .. | ... | +-----------+-----------+-----+ | +/// | +----+-----------------------------+ ^ | +/// | + 10 | material_array +--+ +---------------------------+ | +/// | +----+-----------------------------+ | | | +/// | | Buffer Binding Array | | +/// | | +----------+----------+-----+ | | +/// | +->| buffer 0 | buffer 1 | ... | | | +/// | Material Bindless Indices +----------+----------+-----+ | | +/// | +----+-----------------------------+ ^ | | +/// +-->| 0 | material +----------+ | | +/// +----+-----------------------------+ | | +/// | 1 | base_color_texture +---------------------------------------+ | +/// +----+-----------------------------+ | +/// | 2 | base_color_sampler +-------------------------------------------+ +/// +----+-----------------------------+ +/// | .. | ... | +/// +----+-----------------------------+ +/// ``` +/// +/// The previous `CoolMaterial` example illustrating "combining multiple field-level uniform attributes with the same binding index" can +/// also be equivalently represented with a single struct-level uniform attribute: +/// ``` +/// # use bevy_render::{render_resource::{AsBindGroup, ShaderType}}; +/// # use bevy_color::LinearRgba; +/// #[derive(AsBindGroup)] +/// #[uniform(0, CoolMaterialUniform)] +/// struct CoolMaterial { +/// color: LinearRgba, +/// roughness: f32, +/// } +/// +/// #[derive(ShaderType)] +/// struct CoolMaterialUniform { +/// color: LinearRgba, +/// roughness: f32, +/// } +/// +/// impl From<&CoolMaterial> for CoolMaterialUniform { +/// fn from(material: &CoolMaterial) -> CoolMaterialUniform { +/// CoolMaterialUniform { +/// color: material.color, +/// roughness: material.roughness, +/// } +/// } +/// } +/// ``` +/// +/// Setting `bind_group_data` looks like this: +/// ``` +/// # use bevy_render::{render_resource::AsBindGroup}; +/// # use bevy_color::LinearRgba; +/// #[derive(AsBindGroup)] +/// #[bind_group_data(CoolMaterialKey)] +/// struct CoolMaterial { +/// #[uniform(0)] +/// color: LinearRgba, +/// is_shaded: bool, +/// } +/// +/// // Materials keys are intended to be small, cheap to hash, and +/// // uniquely identify a specific material permutation. +/// #[repr(C)] +/// #[derive(Copy, Clone, Hash, Eq, PartialEq)] +/// struct CoolMaterialKey { +/// is_shaded: bool, +/// } +/// +/// impl From<&CoolMaterial> for CoolMaterialKey { +/// fn from(material: &CoolMaterial) -> CoolMaterialKey { +/// CoolMaterialKey { +/// is_shaded: material.is_shaded, +/// } +/// } +/// } +/// ``` +pub trait AsBindGroup { + /// Data that will be stored alongside the "prepared" bind group. + type Data: Send + Sync; + + type Param: SystemParam + 'static; + + /// The number of slots per bind group, if bindless mode is enabled. + /// + /// If this bind group doesn't use bindless, then this will be `None`. + /// + /// Note that the *actual* slot count may be different from this value, due + /// to platform limitations. For example, if bindless resources aren't + /// supported on this platform, the actual slot count will be 1. + fn bindless_slot_count() -> Option { + None + } + + /// True if the hardware *actually* supports bindless textures for this + /// type, taking the device and driver capabilities into account. + /// + /// If this type doesn't use bindless textures, then the return value from + /// this function is meaningless. + fn bindless_supported(_: &RenderDevice) -> bool { + true + } + + /// label + fn label() -> &'static str; + + /// Creates a bind group for `self` matching the layout defined in [`AsBindGroup::bind_group_layout`]. + fn as_bind_group( + &self, + layout_descriptor: &BindGroupLayoutDescriptor, + render_device: &RenderDevice, + pipeline_cache: &PipelineCache, + param: &mut SystemParamItem<'_, '_, Self::Param>, + ) -> Result { + let layout = &pipeline_cache.get_bind_group_layout(layout_descriptor); + + let UnpreparedBindGroup { bindings } = + Self::unprepared_bind_group(self, layout, render_device, param, false)?; + + let entries = bindings + .iter() + .map(|(index, binding)| BindGroupEntry { + binding: *index, + resource: binding.get_binding(), + }) + .collect::>(); + + let bind_group = render_device.create_bind_group(Self::label(), layout, &entries); + + Ok(PreparedBindGroup { + bindings, + bind_group, + }) + } + + fn bind_group_data(&self) -> Self::Data; + + /// Returns a vec of (binding index, `OwnedBindingResource`). + /// + /// In cases where `OwnedBindingResource` is not available (as for bindless + /// texture arrays currently), an implementor may return + /// `AsBindGroupError::CreateBindGroupDirectly` from this function and + /// instead define `as_bind_group` directly. This may prevent certain + /// features, such as bindless mode, from working correctly. + /// + /// Set `force_no_bindless` to true to require that bindless textures *not* + /// be used. `ExtendedMaterial` uses this in order to ensure that the base + /// material doesn't use bindless mode if the extension doesn't. + fn unprepared_bind_group( + &self, + layout: &BindGroupLayout, + render_device: &RenderDevice, + param: &mut SystemParamItem<'_, '_, Self::Param>, + force_no_bindless: bool, + ) -> Result; + + /// Creates the bind group layout matching all bind groups returned by + /// [`AsBindGroup::as_bind_group`] + fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout + where + Self: Sized, + { + render_device.create_bind_group_layout( + Self::label(), + &Self::bind_group_layout_entries(render_device, false), + ) + } + + /// Creates the bind group layout descriptor matching all bind groups returned by + /// [`AsBindGroup::as_bind_group`] + /// TODO: we only need `RenderDevice` to determine if bindless is supported + fn bind_group_layout_descriptor(render_device: &RenderDevice) -> BindGroupLayoutDescriptor + where + Self: Sized, + { + BindGroupLayoutDescriptor { + label: Self::label().into(), + entries: Self::bind_group_layout_entries(render_device, false), + } + } + + /// Returns a vec of bind group layout entries. + /// + /// Set `force_no_bindless` to true to require that bindless textures *not* + /// be used. `ExtendedMaterial` uses this in order to ensure that the base + /// material doesn't use bindless mode if the extension doesn't. + fn bind_group_layout_entries( + render_device: &RenderDevice, + force_no_bindless: bool, + ) -> Vec + where + Self: Sized; + + fn bindless_descriptor() -> Option { + None + } +} + +/// An error that occurs during [`AsBindGroup::as_bind_group`] calls. +#[derive(Debug, Error)] +pub enum AsBindGroupError { + /// The bind group could not be generated. Try again next frame. + #[error("The bind group could not be generated")] + RetryNextUpdate, + #[error("Create the bind group via `as_bind_group()` instead")] + CreateBindGroupDirectly, + #[error("At binding index {0}, the provided image sampler `{1}` does not match the required sampler type(s) `{2}`.")] + InvalidSamplerType(u32, String, String), +} + +/// A prepared bind group returned as a result of [`AsBindGroup::as_bind_group`]. +pub struct PreparedBindGroup { + pub bindings: BindingResources, + pub bind_group: BindGroup, +} + +/// a map containing `OwnedBindingResource`s, keyed by the target binding index +pub struct UnpreparedBindGroup { + pub bindings: BindingResources, +} + +/// A pair of binding index and binding resource, used as part of +/// [`PreparedBindGroup`] and [`UnpreparedBindGroup`]. +#[derive(Deref, DerefMut)] +pub struct BindingResources(pub Vec<(u32, OwnedBindingResource)>); + +/// An owned binding resource of any type (ex: a [`Buffer`], [`TextureView`], etc). +/// This is used by types like [`PreparedBindGroup`] to hold a single list of all +/// render resources used by bindings. +#[derive(Debug)] +pub enum OwnedBindingResource { + Buffer(Buffer), + TextureView(TextureViewDimension, TextureView), + Sampler(SamplerBindingType, Sampler), + Data(OwnedData), +} + +/// Data that will be copied into a GPU buffer. +/// +/// This corresponds to the `#[data]` attribute in `AsBindGroup`. +#[derive(Debug, Deref, DerefMut)] +pub struct OwnedData(pub Vec); + +impl OwnedBindingResource { + /// Creates a [`BindingResource`] reference to this + /// [`OwnedBindingResource`]. + /// + /// Note that this operation panics if passed a + /// [`OwnedBindingResource::Data`], because [`OwnedData`] doesn't itself + /// correspond to any binding and instead requires the + /// `MaterialBindGroupAllocator` to pack it into a buffer. + pub fn get_binding(&self) -> BindingResource<'_> { + match self { + OwnedBindingResource::Buffer(buffer) => buffer.as_entire_binding(), + OwnedBindingResource::TextureView(_, view) => BindingResource::TextureView(view), + OwnedBindingResource::Sampler(_, sampler) => BindingResource::Sampler(sampler), + OwnedBindingResource::Data(_) => panic!("`OwnedData` has no binding resource"), + } + } +} + +/// Converts a value to a [`ShaderType`] for use in a bind group. +/// +/// This is automatically implemented for references that implement [`Into`]. +/// Generally normal [`Into`] / [`From`] impls should be preferred, but +/// sometimes additional runtime metadata is required. +/// This exists largely to make some [`AsBindGroup`] use cases easier. +pub trait AsBindGroupShaderType { + /// Return the `T` [`ShaderType`] for `self`. When used in [`AsBindGroup`] + /// derives, it is safe to assume that all images in `self` exist. + fn as_bind_group_shader_type(&self, images: &RenderAssets) -> T; +} + +impl AsBindGroupShaderType for T +where + for<'a> &'a T: Into, +{ + #[inline] + fn as_bind_group_shader_type(&self, _images: &RenderAssets) -> U { + self.into() + } +} + +#[cfg(test)] +mod test { + use super::*; + use bevy_asset::Handle; + use bevy_image::Image; + + #[test] + fn texture_visibility() { + #[expect( + dead_code, + reason = "This is a derive macro compilation test. It will not be constructed." + )] + #[derive(AsBindGroup)] + pub struct TextureVisibilityTest { + #[texture(0, visibility(all))] + pub all: Handle, + #[texture(1, visibility(none))] + pub none: Handle, + #[texture(2, visibility(fragment))] + pub fragment: Handle, + #[texture(3, visibility(vertex))] + pub vertex: Handle, + #[texture(4, visibility(compute))] + pub compute: Handle, + #[texture(5, visibility(vertex, fragment))] + pub vertex_fragment: Handle, + #[texture(6, visibility(vertex, compute))] + pub vertex_compute: Handle, + #[texture(7, visibility(fragment, compute))] + pub fragment_compute: Handle, + #[texture(8, visibility(vertex, fragment, compute))] + pub vertex_fragment_compute: Handle, + } + } +} diff --git a/third_party/bevy_render/src/render_resource/bind_group_entries.rs b/third_party/bevy_render/src/render_resource/bind_group_entries.rs new file mode 100644 index 0000000..274aa11 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/bind_group_entries.rs @@ -0,0 +1,322 @@ +use variadics_please::all_tuples_with_size; +use wgpu::{BindGroupEntry, BindingResource}; + +use super::{Sampler, TextureView}; + +/// Helper for constructing bindgroups. +/// +/// Allows constructing the descriptor's entries as: +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &BindGroupEntries::with_indices(( +/// (2, &my_sampler), +/// (3, my_uniform), +/// )), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &[ +/// BindGroupEntry { +/// binding: 2, +/// resource: BindingResource::Sampler(&my_sampler), +/// }, +/// BindGroupEntry { +/// binding: 3, +/// resource: my_uniform, +/// }, +/// ], +/// ); +/// ``` +/// +/// or +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &BindGroupEntries::sequential(( +/// &my_sampler, +/// my_uniform, +/// )), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &[ +/// BindGroupEntry { +/// binding: 0, +/// resource: BindingResource::Sampler(&my_sampler), +/// }, +/// BindGroupEntry { +/// binding: 1, +/// resource: my_uniform, +/// }, +/// ], +/// ); +/// ``` +/// +/// or +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &BindGroupEntries::single(my_uniform), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group( +/// "my_bind_group", +/// &my_layout, +/// &[ +/// BindGroupEntry { +/// binding: 0, +/// resource: my_uniform, +/// }, +/// ], +/// ); +/// ``` +pub struct BindGroupEntries<'b, const N: usize = 1> { + entries: [BindGroupEntry<'b>; N], +} + +impl<'b, const N: usize> BindGroupEntries<'b, N> { + #[inline] + pub fn sequential(resources: impl IntoBindingArray<'b, N>) -> Self { + let mut i = 0; + Self { + entries: resources.into_array().map(|resource| { + let binding = i; + i += 1; + BindGroupEntry { binding, resource } + }), + } + } + + #[inline] + pub fn with_indices(indexed_resources: impl IntoIndexedBindingArray<'b, N>) -> Self { + Self { + entries: indexed_resources + .into_array() + .map(|(binding, resource)| BindGroupEntry { binding, resource }), + } + } +} + +impl<'b> BindGroupEntries<'b, 1> { + pub fn single(resource: impl IntoBinding<'b>) -> [BindGroupEntry<'b>; 1] { + [BindGroupEntry { + binding: 0, + resource: resource.into_binding(), + }] + } +} + +impl<'b, const N: usize> core::ops::Deref for BindGroupEntries<'b, N> { + type Target = [BindGroupEntry<'b>]; + + fn deref(&self) -> &[BindGroupEntry<'b>] { + &self.entries + } +} + +pub trait IntoBinding<'a> { + fn into_binding(self) -> BindingResource<'a>; +} + +impl<'a> IntoBinding<'a> for &'a TextureView { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::TextureView(self) + } +} + +impl<'a> IntoBinding<'a> for &'a wgpu::TextureView { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::TextureView(self) + } +} + +impl<'a> IntoBinding<'a> for &'a [&'a wgpu::TextureView] { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::TextureViewArray(self) + } +} + +impl<'a> IntoBinding<'a> for &'a Sampler { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::Sampler(self) + } +} + +impl<'a> IntoBinding<'a> for &'a [&'a wgpu::Sampler] { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::SamplerArray(self) + } +} + +impl<'a> IntoBinding<'a> for BindingResource<'a> { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + self + } +} + +impl<'a> IntoBinding<'a> for wgpu::BufferBinding<'a> { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::Buffer(self) + } +} + +impl<'a> IntoBinding<'a> for &'a [wgpu::BufferBinding<'a>] { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + BindingResource::BufferArray(self) + } +} + +pub trait IntoBindingArray<'b, const N: usize> { + fn into_array(self) -> [BindingResource<'b>; N]; +} + +macro_rules! impl_to_binding_slice { + ($N: expr, $(#[$meta:meta])* $(($T: ident, $I: ident)),*) => { + $(#[$meta])* + impl<'b, $($T: IntoBinding<'b>),*> IntoBindingArray<'b, $N> for ($($T,)*) { + #[inline] + fn into_array(self) -> [BindingResource<'b>; $N] { + let ($($I,)*) = self; + [$($I.into_binding(), )*] + } + } + } +} + +all_tuples_with_size!( + #[doc(fake_variadic)] + impl_to_binding_slice, + 1, + 32, + T, + s +); + +pub trait IntoIndexedBindingArray<'b, const N: usize> { + fn into_array(self) -> [(u32, BindingResource<'b>); N]; +} + +macro_rules! impl_to_indexed_binding_slice { + ($N: expr, $(($T: ident, $S: ident, $I: ident)),*) => { + impl<'b, $($T: IntoBinding<'b>),*> IntoIndexedBindingArray<'b, $N> for ($((u32, $T),)*) { + #[inline] + fn into_array(self) -> [(u32, BindingResource<'b>); $N] { + let ($(($S, $I),)*) = self; + [$(($S, $I.into_binding())), *] + } + } + } +} + +all_tuples_with_size!(impl_to_indexed_binding_slice, 1, 32, T, n, s); + +pub struct DynamicBindGroupEntries<'b> { + entries: Vec>, +} + +impl<'b> Default for DynamicBindGroupEntries<'b> { + fn default() -> Self { + Self::new() + } +} + +impl<'b> DynamicBindGroupEntries<'b> { + pub fn sequential(entries: impl IntoBindingArray<'b, N>) -> Self { + Self { + entries: entries + .into_array() + .into_iter() + .enumerate() + .map(|(ix, resource)| BindGroupEntry { + binding: ix as u32, + resource, + }) + .collect(), + } + } + + pub fn extend_sequential( + mut self, + entries: impl IntoBindingArray<'b, N>, + ) -> Self { + let start = self.entries.last().unwrap().binding + 1; + self.entries.extend( + entries + .into_array() + .into_iter() + .enumerate() + .map(|(ix, resource)| BindGroupEntry { + binding: start + ix as u32, + resource, + }), + ); + self + } + + pub fn new_with_indices(entries: impl IntoIndexedBindingArray<'b, N>) -> Self { + Self { + entries: entries + .into_array() + .into_iter() + .map(|(binding, resource)| BindGroupEntry { binding, resource }) + .collect(), + } + } + + pub fn new() -> Self { + Self { + entries: Vec::new(), + } + } + + pub fn extend_with_indices( + mut self, + entries: impl IntoIndexedBindingArray<'b, N>, + ) -> Self { + self.entries.extend( + entries + .into_array() + .into_iter() + .map(|(binding, resource)| BindGroupEntry { binding, resource }), + ); + self + } +} + +impl<'b> core::ops::Deref for DynamicBindGroupEntries<'b> { + type Target = [BindGroupEntry<'b>]; + + fn deref(&self) -> &[BindGroupEntry<'b>] { + &self.entries + } +} diff --git a/third_party/bevy_render/src/render_resource/bind_group_layout.rs b/third_party/bevy_render/src/render_resource/bind_group_layout.rs new file mode 100644 index 0000000..3b8cc00 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/bind_group_layout.rs @@ -0,0 +1,63 @@ +use crate::{define_atomic_id, renderer::WgpuWrapper}; +use core::ops::Deref; + +define_atomic_id!(BindGroupLayoutId); + +/// Bind group layouts define the interface of resources (e.g. buffers, textures, samplers) +/// for a shader. The actual resource binding is done via a [`BindGroup`](super::BindGroup). +/// +/// This is a lightweight thread-safe wrapper around wgpu's own [`BindGroupLayout`](wgpu::BindGroupLayout), +/// which can be cloned as needed to workaround lifetime management issues. It may be converted +/// from and dereferences to wgpu's [`BindGroupLayout`](wgpu::BindGroupLayout). +/// +/// Can be created via [`RenderDevice::create_bind_group_layout`](crate::renderer::RenderDevice::create_bind_group_layout). +#[derive(Clone, Debug)] +pub struct BindGroupLayout { + id: BindGroupLayoutId, + value: WgpuWrapper, +} + +impl PartialEq for BindGroupLayout { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Eq for BindGroupLayout {} + +impl core::hash::Hash for BindGroupLayout { + fn hash(&self, state: &mut H) { + self.id.0.hash(state); + } +} + +impl BindGroupLayout { + /// Returns the [`BindGroupLayoutId`] representing the unique ID of the bind group layout. + #[inline] + pub fn id(&self) -> BindGroupLayoutId { + self.id + } + + #[inline] + pub fn value(&self) -> &wgpu::BindGroupLayout { + &self.value + } +} + +impl From for BindGroupLayout { + fn from(value: wgpu::BindGroupLayout) -> Self { + BindGroupLayout { + id: BindGroupLayoutId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for BindGroupLayout { + type Target = wgpu::BindGroupLayout; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} diff --git a/third_party/bevy_render/src/render_resource/bind_group_layout_entries.rs b/third_party/bevy_render/src/render_resource/bind_group_layout_entries.rs new file mode 100644 index 0000000..17630b7 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/bind_group_layout_entries.rs @@ -0,0 +1,592 @@ +use core::num::NonZero; +use variadics_please::all_tuples_with_size; +use wgpu::{BindGroupLayoutEntry, BindingType, ShaderStages}; + +/// Helper for constructing bind group layouts. +/// +/// Allows constructing the layout's entries as: +/// ```ignore (render_device cannot be easily accessed) +/// let layout = render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &BindGroupLayoutEntries::with_indices( +/// // The layout entries will only be visible in the fragment stage +/// ShaderStages::FRAGMENT, +/// ( +/// // Screen texture +/// (2, texture_2d(TextureSampleType::Float { filterable: true })), +/// // Sampler +/// (3, sampler(SamplerBindingType::Filtering)), +/// ), +/// ), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// let layout = render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &[ +/// // Screen texture +/// BindGroupLayoutEntry { +/// binding: 2, +/// visibility: ShaderStages::FRAGMENT, +/// ty: BindingType::Texture { +/// sample_type: TextureSampleType::Float { filterable: true }, +/// view_dimension: TextureViewDimension::D2, +/// multisampled: false, +/// }, +/// count: None, +/// }, +/// // Sampler +/// BindGroupLayoutEntry { +/// binding: 3, +/// visibility: ShaderStages::FRAGMENT, +/// ty: BindingType::Sampler(SamplerBindingType::Filtering), +/// count: None, +/// }, +/// ], +/// ); +/// ``` +/// +/// or +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &BindGroupLayoutEntries::sequential( +/// ShaderStages::FRAGMENT, +/// ( +/// // Screen texture +/// texture_2d(TextureSampleType::Float { filterable: true }), +/// // Sampler +/// sampler(SamplerBindingType::Filtering), +/// ), +/// ), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// let layout = render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &[ +/// // Screen texture +/// BindGroupLayoutEntry { +/// binding: 0, +/// visibility: ShaderStages::FRAGMENT, +/// ty: BindingType::Texture { +/// sample_type: TextureSampleType::Float { filterable: true }, +/// view_dimension: TextureViewDimension::D2, +/// multisampled: false, +/// }, +/// count: None, +/// }, +/// // Sampler +/// BindGroupLayoutEntry { +/// binding: 1, +/// visibility: ShaderStages::FRAGMENT, +/// ty: BindingType::Sampler(SamplerBindingType::Filtering), +/// count: None, +/// }, +/// ], +/// ); +/// ``` +/// +/// or +/// +/// ```ignore (render_device cannot be easily accessed) +/// render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &BindGroupLayoutEntries::single( +/// ShaderStages::FRAGMENT, +/// texture_2d(TextureSampleType::Float { filterable: true }), +/// ), +/// ); +/// ``` +/// +/// instead of +/// +/// ```ignore (render_device cannot be easily accessed) +/// let layout = render_device.create_bind_group_layout( +/// "my_bind_group_layout", +/// &[ +/// BindGroupLayoutEntry { +/// binding: 0, +/// visibility: ShaderStages::FRAGMENT, +/// ty: BindingType::Texture { +/// sample_type: TextureSampleType::Float { filterable: true }, +/// view_dimension: TextureViewDimension::D2, +/// multisampled: false, +/// }, +/// count: None, +/// }, +/// ], +/// ); +/// ``` + +#[derive(Clone, Copy)] +pub struct BindGroupLayoutEntryBuilder { + ty: BindingType, + visibility: Option, + count: Option>, +} + +impl BindGroupLayoutEntryBuilder { + pub fn visibility(mut self, visibility: ShaderStages) -> Self { + self.visibility = Some(visibility); + self + } + + pub fn count(mut self, count: NonZero) -> Self { + self.count = Some(count); + self + } + + pub fn build(&self, binding: u32, default_visibility: ShaderStages) -> BindGroupLayoutEntry { + BindGroupLayoutEntry { + binding, + ty: self.ty, + visibility: self.visibility.unwrap_or(default_visibility), + count: self.count, + } + } +} + +pub struct BindGroupLayoutEntries { + entries: [BindGroupLayoutEntry; N], +} + +impl BindGroupLayoutEntries { + #[inline] + pub fn sequential( + default_visibility: ShaderStages, + entries_ext: impl IntoBindGroupLayoutEntryBuilderArray, + ) -> Self { + let mut i = 0; + Self { + entries: entries_ext.into_array().map(|entry| { + let binding = i; + i += 1; + entry.build(binding, default_visibility) + }), + } + } + + #[inline] + pub fn with_indices( + default_visibility: ShaderStages, + indexed_entries: impl IntoIndexedBindGroupLayoutEntryBuilderArray, + ) -> Self { + Self { + entries: indexed_entries + .into_array() + .map(|(binding, entry)| entry.build(binding, default_visibility)), + } + } +} + +impl BindGroupLayoutEntries<1> { + pub fn single( + visibility: ShaderStages, + resource: impl IntoBindGroupLayoutEntryBuilder, + ) -> [BindGroupLayoutEntry; 1] { + [resource + .into_bind_group_layout_entry_builder() + .build(0, visibility)] + } +} + +impl core::ops::Deref for BindGroupLayoutEntries { + type Target = [BindGroupLayoutEntry]; + fn deref(&self) -> &[BindGroupLayoutEntry] { + &self.entries + } +} + +pub trait IntoBindGroupLayoutEntryBuilder { + fn into_bind_group_layout_entry_builder(self) -> BindGroupLayoutEntryBuilder; +} + +impl IntoBindGroupLayoutEntryBuilder for BindingType { + fn into_bind_group_layout_entry_builder(self) -> BindGroupLayoutEntryBuilder { + BindGroupLayoutEntryBuilder { + ty: self, + visibility: None, + count: None, + } + } +} + +impl IntoBindGroupLayoutEntryBuilder for BindGroupLayoutEntry { + fn into_bind_group_layout_entry_builder(self) -> BindGroupLayoutEntryBuilder { + if self.binding != u32::MAX { + tracing::warn!("The BindGroupLayoutEntries api ignores the binding index when converting a raw wgpu::BindGroupLayoutEntry. You can ignore this warning by setting it to u32::MAX."); + } + BindGroupLayoutEntryBuilder { + ty: self.ty, + visibility: Some(self.visibility), + count: self.count, + } + } +} + +impl IntoBindGroupLayoutEntryBuilder for BindGroupLayoutEntryBuilder { + fn into_bind_group_layout_entry_builder(self) -> BindGroupLayoutEntryBuilder { + self + } +} + +pub trait IntoBindGroupLayoutEntryBuilderArray { + fn into_array(self) -> [BindGroupLayoutEntryBuilder; N]; +} +macro_rules! impl_to_binding_type_slice { + ($N: expr, $(#[$meta:meta])* $(($T: ident, $I: ident)),*) => { + $(#[$meta])* + impl<$($T: IntoBindGroupLayoutEntryBuilder),*> IntoBindGroupLayoutEntryBuilderArray<$N> for ($($T,)*) { + #[inline] + fn into_array(self) -> [BindGroupLayoutEntryBuilder; $N] { + let ($($I,)*) = self; + [$($I.into_bind_group_layout_entry_builder(), )*] + } + } + } +} +all_tuples_with_size!( + #[doc(fake_variadic)] + impl_to_binding_type_slice, + 1, + 32, + T, + s +); + +pub trait IntoIndexedBindGroupLayoutEntryBuilderArray { + fn into_array(self) -> [(u32, BindGroupLayoutEntryBuilder); N]; +} +macro_rules! impl_to_indexed_binding_type_slice { + ($N: expr, $(($T: ident, $S: ident, $I: ident)),*) => { + impl<$($T: IntoBindGroupLayoutEntryBuilder),*> IntoIndexedBindGroupLayoutEntryBuilderArray<$N> for ($((u32, $T),)*) { + #[inline] + fn into_array(self) -> [(u32, BindGroupLayoutEntryBuilder); $N] { + let ($(($S, $I),)*) = self; + [$(($S, $I.into_bind_group_layout_entry_builder())), *] + } + } + } +} +all_tuples_with_size!(impl_to_indexed_binding_type_slice, 1, 32, T, n, s); + +impl IntoBindGroupLayoutEntryBuilderArray for [BindGroupLayoutEntry; N] { + fn into_array(self) -> [BindGroupLayoutEntryBuilder; N] { + self.map(IntoBindGroupLayoutEntryBuilder::into_bind_group_layout_entry_builder) + } +} + +pub struct DynamicBindGroupLayoutEntries { + default_visibility: ShaderStages, + entries: Vec, +} + +impl DynamicBindGroupLayoutEntries { + pub fn sequential( + default_visibility: ShaderStages, + entries: impl IntoBindGroupLayoutEntryBuilderArray, + ) -> Self { + Self { + default_visibility, + entries: entries + .into_array() + .into_iter() + .enumerate() + .map(|(ix, resource)| resource.build(ix as u32, default_visibility)) + .collect(), + } + } + + pub fn extend_sequential( + mut self, + entries: impl IntoBindGroupLayoutEntryBuilderArray, + ) -> Self { + let start = self.entries.last().unwrap().binding + 1; + self.entries.extend( + entries + .into_array() + .into_iter() + .enumerate() + .map(|(ix, resource)| resource.build(start + ix as u32, self.default_visibility)), + ); + self + } + + pub fn new_with_indices( + default_visibility: ShaderStages, + entries: impl IntoIndexedBindGroupLayoutEntryBuilderArray, + ) -> Self { + Self { + default_visibility, + entries: entries + .into_array() + .into_iter() + .map(|(binding, resource)| resource.build(binding, default_visibility)) + .collect(), + } + } + + pub fn new(default_visibility: ShaderStages) -> Self { + Self { + default_visibility, + entries: Vec::new(), + } + } + + pub fn extend_with_indices( + mut self, + entries: impl IntoIndexedBindGroupLayoutEntryBuilderArray, + ) -> Self { + self.entries.extend( + entries + .into_array() + .into_iter() + .map(|(binding, resource)| resource.build(binding, self.default_visibility)), + ); + self + } +} + +impl core::ops::Deref for DynamicBindGroupLayoutEntries { + type Target = [BindGroupLayoutEntry]; + + fn deref(&self) -> &[BindGroupLayoutEntry] { + &self.entries + } +} + +pub mod binding_types { + use crate::render_resource::{ + BufferBindingType, SamplerBindingType, TextureSampleType, TextureViewDimension, + }; + use core::num::NonZero; + use encase::ShaderType; + use wgpu::{StorageTextureAccess, TextureFormat}; + + use super::*; + + pub fn storage_buffer(has_dynamic_offset: bool) -> BindGroupLayoutEntryBuilder { + storage_buffer_sized(has_dynamic_offset, Some(T::min_size())) + } + + pub fn storage_buffer_sized( + has_dynamic_offset: bool, + min_binding_size: Option>, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Buffer { + ty: BufferBindingType::Storage { read_only: false }, + has_dynamic_offset, + min_binding_size, + } + .into_bind_group_layout_entry_builder() + } + + pub fn storage_buffer_read_only( + has_dynamic_offset: bool, + ) -> BindGroupLayoutEntryBuilder { + storage_buffer_read_only_sized(has_dynamic_offset, Some(T::min_size())) + } + + pub fn storage_buffer_read_only_sized( + has_dynamic_offset: bool, + min_binding_size: Option>, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Buffer { + ty: BufferBindingType::Storage { read_only: true }, + has_dynamic_offset, + min_binding_size, + } + .into_bind_group_layout_entry_builder() + } + + pub fn uniform_buffer(has_dynamic_offset: bool) -> BindGroupLayoutEntryBuilder { + uniform_buffer_sized(has_dynamic_offset, Some(T::min_size())) + } + + pub fn uniform_buffer_sized( + has_dynamic_offset: bool, + min_binding_size: Option>, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Buffer { + ty: BufferBindingType::Uniform, + has_dynamic_offset, + min_binding_size, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_1d(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D1, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_2d(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D2, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_2d_multisampled(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D2, + multisampled: true, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_2d_array(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D2Array, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_2d_array_multisampled( + sample_type: TextureSampleType, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D2Array, + multisampled: true, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_depth_2d() -> BindGroupLayoutEntryBuilder { + texture_2d(TextureSampleType::Depth).into_bind_group_layout_entry_builder() + } + + pub fn texture_depth_2d_multisampled() -> BindGroupLayoutEntryBuilder { + texture_2d_multisampled(TextureSampleType::Depth).into_bind_group_layout_entry_builder() + } + + pub fn texture_cube(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::Cube, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_cube_multisampled( + sample_type: TextureSampleType, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::Cube, + multisampled: true, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_cube_array(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::CubeArray, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_cube_array_multisampled( + sample_type: TextureSampleType, + ) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::CubeArray, + multisampled: true, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_3d(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D3, + multisampled: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_3d_multisampled(sample_type: TextureSampleType) -> BindGroupLayoutEntryBuilder { + BindingType::Texture { + sample_type, + view_dimension: TextureViewDimension::D3, + multisampled: true, + } + .into_bind_group_layout_entry_builder() + } + + pub fn sampler(sampler_binding_type: SamplerBindingType) -> BindGroupLayoutEntryBuilder { + BindingType::Sampler(sampler_binding_type).into_bind_group_layout_entry_builder() + } + + pub fn texture_storage_2d( + format: TextureFormat, + access: StorageTextureAccess, + ) -> BindGroupLayoutEntryBuilder { + BindingType::StorageTexture { + access, + format, + view_dimension: TextureViewDimension::D2, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_storage_2d_array( + format: TextureFormat, + access: StorageTextureAccess, + ) -> BindGroupLayoutEntryBuilder { + BindingType::StorageTexture { + access, + format, + view_dimension: TextureViewDimension::D2Array, + } + .into_bind_group_layout_entry_builder() + } + + pub fn texture_storage_3d( + format: TextureFormat, + access: StorageTextureAccess, + ) -> BindGroupLayoutEntryBuilder { + BindingType::StorageTexture { + access, + format, + view_dimension: TextureViewDimension::D3, + } + .into_bind_group_layout_entry_builder() + } + + pub fn acceleration_structure() -> BindGroupLayoutEntryBuilder { + BindingType::AccelerationStructure { + vertex_return: false, + } + .into_bind_group_layout_entry_builder() + } + + pub fn acceleration_structure_vertex_return() -> BindGroupLayoutEntryBuilder { + BindingType::AccelerationStructure { + vertex_return: true, + } + .into_bind_group_layout_entry_builder() + } +} diff --git a/third_party/bevy_render/src/render_resource/bindless.rs b/third_party/bevy_render/src/render_resource/bindless.rs new file mode 100644 index 0000000..dc3bf00 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/bindless.rs @@ -0,0 +1,374 @@ +//! Types and functions relating to bindless resources. + +use alloc::borrow::Cow; +use core::{ + num::{NonZeroU32, NonZeroU64}, + ops::Range, +}; + +use bevy_derive::{Deref, DerefMut}; +use wgpu::{ + BindGroupLayoutEntry, SamplerBindingType, ShaderStages, TextureSampleType, TextureViewDimension, +}; + +use crate::render_resource::binding_types::storage_buffer_read_only_sized; + +use super::binding_types::{ + sampler, texture_1d, texture_2d, texture_2d_array, texture_3d, texture_cube, texture_cube_array, +}; + +/// The default value for the number of resources that can be stored in a slab +/// on this platform. +/// +/// See the documentation for [`BindlessSlabResourceLimit`] for more +/// information. +#[cfg(any(target_os = "macos", target_os = "ios"))] +pub const AUTO_BINDLESS_SLAB_RESOURCE_LIMIT: u32 = 64; +/// The default value for the number of resources that can be stored in a slab +/// on this platform. +/// +/// See the documentation for [`BindlessSlabResourceLimit`] for more +/// information. +#[cfg(not(any(target_os = "macos", target_os = "ios")))] +pub const AUTO_BINDLESS_SLAB_RESOURCE_LIMIT: u32 = 2048; + +/// The binding numbers for the built-in binding arrays of each bindless +/// resource type. +/// +/// In the case of materials, the material allocator manages these binding +/// arrays. +/// +/// `bindless.wgsl` contains declarations of these arrays for use in your +/// shaders. If you change these, make sure to update that file as well. +pub static BINDING_NUMBERS: [(BindlessResourceType, BindingNumber); 9] = [ + (BindlessResourceType::SamplerFiltering, BindingNumber(1)), + (BindlessResourceType::SamplerNonFiltering, BindingNumber(2)), + (BindlessResourceType::SamplerComparison, BindingNumber(3)), + (BindlessResourceType::Texture1d, BindingNumber(4)), + (BindlessResourceType::Texture2d, BindingNumber(5)), + (BindlessResourceType::Texture2dArray, BindingNumber(6)), + (BindlessResourceType::Texture3d, BindingNumber(7)), + (BindlessResourceType::TextureCube, BindingNumber(8)), + (BindlessResourceType::TextureCubeArray, BindingNumber(9)), +]; + +/// The maximum number of resources that can be stored in a slab. +/// +/// This limit primarily exists in order to work around `wgpu` performance +/// problems involving large numbers of bindless resources. Also, some +/// platforms, such as Metal, currently enforce limits on the number of +/// resources in use. +/// +/// This corresponds to `LIMIT` in the `#[bindless(LIMIT)]` attribute when +/// deriving [`crate::render_resource::AsBindGroup`]. +#[derive(Clone, Copy, Default, PartialEq, Debug)] +pub enum BindlessSlabResourceLimit { + /// Allows the renderer to choose a reasonable value for the resource limit + /// based on the platform. + /// + /// This value has been tuned, so you should default to this value unless + /// you have special platform-specific considerations that prevent you from + /// using it. + #[default] + Auto, + + /// A custom value for the resource limit. + /// + /// Bevy will allocate no more than this number of resources in a slab, + /// unless exceeding this value is necessary in order to allocate at all + /// (i.e. unless the number of bindless resources in your bind group exceeds + /// this value), in which case Bevy can exceed it. + Custom(u32), +} + +/// Information about the bindless resources in this object. +/// +/// The material bind group allocator uses this descriptor in order to create +/// and maintain bind groups. The fields within this bindless descriptor are +/// [`Cow`]s in order to support both the common case in which the fields are +/// simply `static` constants and the more unusual case in which the fields are +/// dynamically generated efficiently. An example of the latter case is +/// `ExtendedMaterial`, which needs to assemble a bindless descriptor from those +/// of the base material and the material extension at runtime. +/// +/// This structure will only be present if this object is bindless. +pub struct BindlessDescriptor { + /// The bindless resource types that this object uses, in order of bindless + /// index. + /// + /// The resource assigned to binding index 0 will be at index 0, the + /// resource assigned to binding index will be at index 1 in this array, and + /// so on. Unused binding indices are set to [`BindlessResourceType::None`]. + pub resources: Cow<'static, [BindlessResourceType]>, + /// The [`BindlessBufferDescriptor`] for each bindless buffer that this + /// object uses. + /// + /// The order of this array is irrelevant. + pub buffers: Cow<'static, [BindlessBufferDescriptor]>, + /// The [`BindlessIndexTableDescriptor`]s describing each bindless index + /// table. + /// + /// This list must be sorted by the first bindless index. + pub index_tables: Cow<'static, [BindlessIndexTableDescriptor]>, +} + +/// The type of potentially-bindless resource. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +pub enum BindlessResourceType { + /// No bindless resource. + /// + /// This is used as a placeholder to fill holes in the + /// [`BindlessDescriptor::resources`] list. + None, + /// A storage buffer. + Buffer, + /// A filtering sampler. + SamplerFiltering, + /// A non-filtering sampler (nearest neighbor). + SamplerNonFiltering, + /// A comparison sampler (typically used for shadow maps). + SamplerComparison, + /// A 1D texture. + Texture1d, + /// A 2D texture. + Texture2d, + /// A 2D texture array. + /// + /// Note that this differs from a binding array. 2D texture arrays must all + /// have the same size and format. + Texture2dArray, + /// A 3D texture. + Texture3d, + /// A cubemap texture. + TextureCube, + /// A cubemap texture array. + /// + /// Note that this differs from a binding array. Cubemap texture arrays must + /// all have the same size and format. + TextureCubeArray, + /// Multiple instances of plain old data concatenated into a single buffer. + /// + /// This corresponds to the `#[data]` declaration in + /// [`crate::render_resource::AsBindGroup`]. + /// + /// Note that this resource doesn't itself map to a GPU-level binding + /// resource and instead depends on the `MaterialBindGroupAllocator` to + /// create a binding resource for it. + DataBuffer, +} + +/// Describes a bindless buffer. +/// +/// Unlike samplers and textures, each buffer in a bind group gets its own +/// unique bind group entry. That is, there isn't any `bindless_buffers` binding +/// array to go along with `bindless_textures_2d`, +/// `bindless_samplers_filtering`, etc. Therefore, this descriptor contains two +/// indices: the *binding number* and the *bindless index*. The binding number +/// is the `@binding` number used in the shader, while the bindless index is the +/// index of the buffer in the bindless index table (which is itself +/// conventionally bound to binding number 0). +/// +/// When declaring the buffer in a derived implementation +/// [`crate::render_resource::AsBindGroup`] with syntax like +/// `#[uniform(BINDLESS_INDEX, StandardMaterialUniform, +/// bindless(BINDING_NUMBER)]`, the bindless index is `BINDLESS_INDEX`, and the +/// binding number is `BINDING_NUMBER`. Note the order. +#[derive(Clone, Copy, Debug)] +pub struct BindlessBufferDescriptor { + /// The actual binding number of the buffer. + /// + /// This is declared with `@binding` in WGSL. When deriving + /// [`crate::render_resource::AsBindGroup`], this is the `BINDING_NUMBER` in + /// `#[uniform(BINDLESS_INDEX, StandardMaterialUniform, + /// bindless(BINDING_NUMBER)]`. + pub binding_number: BindingNumber, + /// The index of the buffer in the bindless index table. + /// + /// In the shader, this is the index into the table bound to binding 0. When + /// deriving [`crate::render_resource::AsBindGroup`], this is the + /// `BINDLESS_INDEX` in `#[uniform(BINDLESS_INDEX, StandardMaterialUniform, + /// bindless(BINDING_NUMBER)]`. + pub bindless_index: BindlessIndex, + /// The size of the buffer in bytes, if known. + pub size: Option, +} + +/// Describes the layout of the bindless index table, which maps bindless +/// indices to indices within the binding arrays. +#[derive(Clone)] +pub struct BindlessIndexTableDescriptor { + /// The range of bindless indices that this descriptor covers. + pub indices: Range, + /// The binding at which the index table itself will be bound. + /// + /// By default, this is binding 0, but it can be changed with the + /// `#[bindless(index_table(binding(B)))]` attribute. + pub binding_number: BindingNumber, +} + +/// The index of the actual binding in the bind group. +/// +/// This is the value specified in WGSL as `@binding`. +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Deref, DerefMut)] +pub struct BindingNumber(pub u32); + +/// The index in the bindless index table. +/// +/// This table is conventionally bound to binding number 0. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Hash, Debug, Deref, DerefMut)] +pub struct BindlessIndex(pub u32); + +/// Creates the bind group layout entries common to all shaders that use +/// bindless bind groups. +/// +/// `bindless_resource_count` specifies the total number of bindless resources. +/// `bindless_slab_resource_limit` specifies the resolved +/// [`BindlessSlabResourceLimit`] value. +pub fn create_bindless_bind_group_layout_entries( + bindless_index_table_length: u32, + bindless_slab_resource_limit: u32, + bindless_index_table_binding_number: BindingNumber, +) -> Vec { + let bindless_slab_resource_limit = + NonZeroU32::new(bindless_slab_resource_limit).expect("Bindless slot count must be nonzero"); + + // The maximum size of a binding array is the + // `bindless_slab_resource_limit`, which would occur if all of the bindless + // resources were of the same type. So we create our binding arrays with + // that size. + + vec![ + // Start with the bindless index table, bound to binding number 0. + storage_buffer_read_only_sized( + false, + NonZeroU64::new(bindless_index_table_length as u64 * size_of::() as u64), + ) + .build( + *bindless_index_table_binding_number, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + // Continue with the common bindless resource arrays. + sampler(SamplerBindingType::Filtering) + .count(bindless_slab_resource_limit) + .build( + 1, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + sampler(SamplerBindingType::NonFiltering) + .count(bindless_slab_resource_limit) + .build( + 2, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + sampler(SamplerBindingType::Comparison) + .count(bindless_slab_resource_limit) + .build( + 3, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_1d(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 4, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_2d(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 5, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_2d_array(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 6, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_3d(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 7, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_cube(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 8, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + texture_cube_array(TextureSampleType::Float { filterable: true }) + .count(bindless_slab_resource_limit) + .build( + 9, + ShaderStages::FRAGMENT | ShaderStages::VERTEX | ShaderStages::COMPUTE, + ), + ] +} + +impl BindlessSlabResourceLimit { + /// Determines the actual bindless slab resource limit on this platform. + pub fn resolve(&self) -> u32 { + match *self { + BindlessSlabResourceLimit::Auto => AUTO_BINDLESS_SLAB_RESOURCE_LIMIT, + BindlessSlabResourceLimit::Custom(limit) => limit, + } + } +} + +impl BindlessResourceType { + /// Returns the binding number for the common array of this resource type. + /// + /// For example, if you pass `BindlessResourceType::Texture2d`, this will + /// return 5, in order to match the `@group(2) @binding(5) var + /// bindless_textures_2d: binding_array>` declaration in + /// `bindless.wgsl`. + /// + /// Not all resource types have fixed binding numbers. If you call + /// [`Self::binding_number`] on such a resource type, it returns `None`. + /// + /// Note that this returns a static reference to the binding number, not the + /// binding number itself. This is to conform to an idiosyncratic API in + /// `wgpu` whereby binding numbers for binding arrays are taken by `&u32` + /// *reference*, not by `u32` value. + pub fn binding_number(&self) -> Option<&'static BindingNumber> { + match BINDING_NUMBERS.binary_search_by_key(self, |(key, _)| *key) { + Ok(binding_number) => Some(&BINDING_NUMBERS[binding_number].1), + Err(_) => None, + } + } +} + +impl From for BindlessResourceType { + fn from(texture_view_dimension: TextureViewDimension) -> Self { + match texture_view_dimension { + TextureViewDimension::D1 => BindlessResourceType::Texture1d, + TextureViewDimension::D2 => BindlessResourceType::Texture2d, + TextureViewDimension::D2Array => BindlessResourceType::Texture2dArray, + TextureViewDimension::Cube => BindlessResourceType::TextureCube, + TextureViewDimension::CubeArray => BindlessResourceType::TextureCubeArray, + TextureViewDimension::D3 => BindlessResourceType::Texture3d, + } + } +} + +impl From for BindlessResourceType { + fn from(sampler_binding_type: SamplerBindingType) -> Self { + match sampler_binding_type { + SamplerBindingType::Filtering => BindlessResourceType::SamplerFiltering, + SamplerBindingType::NonFiltering => BindlessResourceType::SamplerNonFiltering, + SamplerBindingType::Comparison => BindlessResourceType::SamplerComparison, + } + } +} + +impl From for BindlessIndex { + fn from(value: u32) -> Self { + Self(value) + } +} + +impl From for BindingNumber { + fn from(value: u32) -> Self { + Self(value) + } +} diff --git a/third_party/bevy_render/src/render_resource/buffer.rs b/third_party/bevy_render/src/render_resource/buffer.rs new file mode 100644 index 0000000..2a3620b --- /dev/null +++ b/third_party/bevy_render/src/render_resource/buffer.rs @@ -0,0 +1,70 @@ +use crate::define_atomic_id; +use crate::renderer::WgpuWrapper; +use core::ops::{Deref, RangeBounds}; + +define_atomic_id!(BufferId); + +#[derive(Clone, Debug)] +pub struct Buffer { + id: BufferId, + value: WgpuWrapper, +} + +impl Buffer { + #[inline] + pub fn id(&self) -> BufferId { + self.id + } + + pub fn slice(&self, bounds: impl RangeBounds) -> BufferSlice<'_> { + BufferSlice { + id: self.id, + value: self.value.slice(bounds), + } + } + + #[inline] + pub fn unmap(&self) { + self.value.unmap(); + } +} + +impl From for Buffer { + fn from(value: wgpu::Buffer) -> Self { + Buffer { + id: BufferId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for Buffer { + type Target = wgpu::Buffer; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +#[derive(Clone, Debug)] +pub struct BufferSlice<'a> { + id: BufferId, + value: wgpu::BufferSlice<'a>, +} + +impl<'a> BufferSlice<'a> { + #[inline] + pub fn id(&self) -> BufferId { + self.id + } +} + +impl<'a> Deref for BufferSlice<'a> { + type Target = wgpu::BufferSlice<'a>; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} diff --git a/third_party/bevy_render/src/render_resource/buffer_vec.rs b/third_party/bevy_render/src/render_resource/buffer_vec.rs new file mode 100644 index 0000000..acd6a15 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/buffer_vec.rs @@ -0,0 +1,587 @@ +use core::{iter, marker::PhantomData}; + +use crate::{ + render_resource::Buffer, + renderer::{RenderDevice, RenderQueue}, +}; +use bytemuck::{must_cast_slice, NoUninit}; +use encase::{ + internal::{WriteInto, Writer}, + ShaderType, +}; +use thiserror::Error; +use wgpu::{BindingResource, BufferAddress, BufferUsages}; + +use super::GpuArrayBufferable; + +/// A structure for storing raw bytes that have already been properly formatted +/// for use by the GPU. +/// +/// "Properly formatted" means that item data already meets the alignment and padding +/// requirements for how it will be used on the GPU. The item type must implement [`NoUninit`] +/// for its data representation to be directly copyable. +/// +/// Index, vertex, and instance-rate vertex buffers have no alignment nor padding requirements and +/// so this helper type is a good choice for them. +/// +/// The contained data is stored in system RAM. Calling [`reserve`](RawBufferVec::reserve) +/// allocates VRAM from the [`RenderDevice`]. +/// [`write_buffer`](RawBufferVec::write_buffer) queues copying of the data +/// from system RAM to VRAM. +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`] +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +pub struct RawBufferVec { + values: Vec, + buffer: Option, + capacity: usize, + item_size: usize, + buffer_usage: BufferUsages, + label: Option, + changed: bool, +} + +impl RawBufferVec { + /// Creates a new [`RawBufferVec`] with the given [`BufferUsages`]. + pub const fn new(buffer_usage: BufferUsages) -> Self { + Self { + values: Vec::new(), + buffer: None, + capacity: 0, + item_size: size_of::(), + buffer_usage, + label: None, + changed: false, + } + } + + /// Returns a handle to the buffer, if the data has been uploaded. + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + + /// Returns the amount of space that the GPU will use before reallocating. + #[inline] + pub fn capacity(&self) -> usize { + self.capacity + } + + /// Returns the number of items that have been pushed to this buffer. + #[inline] + pub fn len(&self) -> usize { + self.values.len() + } + + /// Returns true if the buffer is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.values.is_empty() + } + + /// Adds a new value and returns its index. + pub fn push(&mut self, value: T) -> usize { + let index = self.values.len(); + self.values.push(value); + index + } + + pub fn append(&mut self, other: &mut RawBufferVec) { + self.values.append(&mut other.values); + } + + /// Returns the value at the given index. + pub fn get(&self, index: u32) -> Option<&T> { + self.values.get(index as usize) + } + + /// Sets the value at the given index. + /// + /// The index must be less than [`RawBufferVec::len`]. + pub fn set(&mut self, index: u32, value: T) { + self.values[index as usize] = value; + } + + /// Preallocates space for `count` elements in the internal CPU-side buffer. + /// + /// Unlike [`RawBufferVec::reserve`], this doesn't have any effect on the GPU buffer. + pub fn reserve_internal(&mut self, count: usize) { + self.values.reserve(count); + } + + /// Changes the debugging label of the buffer. + /// + /// The next time the buffer is updated (via [`reserve`](Self::reserve)), Bevy will inform + /// the driver of the new label. + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.changed = true; + } + + self.label = label; + } + + /// Returns the label + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Creates a [`Buffer`] on the [`RenderDevice`] with size + /// at least `size_of::() * capacity`, unless a such a buffer already exists. + /// + /// If a [`Buffer`] exists, but is too small, references to it will be discarded, + /// and a new [`Buffer`] will be created. Any previously created [`Buffer`]s + /// that are no longer referenced will be deleted by the [`RenderDevice`] + /// once it is done using them (typically 1-2 frames). + /// + /// In addition to any [`BufferUsages`] provided when + /// the `RawBufferVec` was created, the buffer on the [`RenderDevice`] + /// is marked as [`BufferUsages::COPY_DST`](BufferUsages). + pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) { + let size = self.item_size * capacity; + if capacity > self.capacity || (self.changed && size > 0) { + self.capacity = capacity; + self.buffer = Some(device.create_buffer(&wgpu::BufferDescriptor { + label: self.label.as_deref(), + size: size as BufferAddress, + usage: BufferUsages::COPY_DST | self.buffer_usage, + mapped_at_creation: false, + })); + self.changed = false; + } + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// Before queuing the write, a [`reserve`](RawBufferVec::reserve) operation + /// is executed. + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + if self.values.is_empty() { + return; + } + self.reserve(self.values.len(), device); + if let Some(buffer) = &self.buffer { + let range = 0..self.item_size * self.values.len(); + let bytes: &[u8] = must_cast_slice(&self.values); + queue.write_buffer(buffer, 0, &bytes[range]); + } + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// If the buffer is not initialized on the GPU or the range is bigger than the capacity it will + /// return an error. You'll need to either reserve a new buffer which will lose data on the GPU + /// or create a new buffer and copy the old data to it. + /// + /// This will only write the data contained in the given range. It is useful if you only want + /// to update a part of the buffer. + pub fn write_buffer_range( + &mut self, + render_queue: &RenderQueue, + range: core::ops::Range, + ) -> Result<(), WriteBufferRangeError> { + if self.values.is_empty() { + return Err(WriteBufferRangeError::NoValuesToUpload); + } + if range.end > self.item_size * self.capacity { + return Err(WriteBufferRangeError::RangeBiggerThanBuffer); + } + if let Some(buffer) = &self.buffer { + // Cast only the bytes we need to write + let bytes: &[u8] = must_cast_slice(&self.values[range.start..range.end]); + render_queue.write_buffer(buffer, (range.start * self.item_size) as u64, bytes); + Ok(()) + } else { + Err(WriteBufferRangeError::BufferNotInitialized) + } + } + + /// Reduces the length of the buffer. + pub fn truncate(&mut self, len: usize) { + self.values.truncate(len); + } + + /// Removes all elements from the buffer. + pub fn clear(&mut self) { + self.values.clear(); + } + + /// Removes and returns the last element in the buffer. + pub fn pop(&mut self) -> Option { + self.values.pop() + } + + pub fn values(&self) -> &Vec { + &self.values + } + + pub fn values_mut(&mut self) -> &mut Vec { + &mut self.values + } +} + +impl RawBufferVec +where + T: NoUninit + Default, +{ + pub fn grow_set(&mut self, index: u32, value: T) { + while index as usize + 1 > self.len() { + self.values.push(T::default()); + } + self.values[index as usize] = value; + } +} + +impl Extend for RawBufferVec { + #[inline] + fn extend>(&mut self, iter: I) { + self.values.extend(iter); + } +} + +/// Like [`RawBufferVec`], but doesn't require that the data type `T` be +/// [`NoUninit`]. +/// +/// This is a high-performance data structure that you should use whenever +/// possible if your data is more complex than is suitable for [`RawBufferVec`]. +/// The [`ShaderType`] trait from the `encase` library is used to ensure that +/// the data is correctly aligned for use by the GPU. +/// +/// For performance reasons, unlike [`RawBufferVec`], this type doesn't allow +/// CPU access to the data after it's been added via [`BufferVec::push`]. If you +/// need CPU access to the data, consider another type, such as +/// [`StorageBuffer`][super::StorageBuffer]. +/// +/// Other options for storing GPU-accessible data are: +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`] +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +pub struct BufferVec +where + T: ShaderType + WriteInto, +{ + data: Vec, + buffer: Option, + capacity: usize, + buffer_usage: BufferUsages, + label: Option, + label_changed: bool, + phantom: PhantomData, +} + +impl BufferVec +where + T: ShaderType + WriteInto, +{ + /// Creates a new [`BufferVec`] with the given [`BufferUsages`]. + pub const fn new(buffer_usage: BufferUsages) -> Self { + Self { + data: vec![], + buffer: None, + capacity: 0, + buffer_usage, + label: None, + label_changed: false, + phantom: PhantomData, + } + } + + /// Returns a handle to the buffer, if the data has been uploaded. + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + + /// Returns the amount of space that the GPU will use before reallocating. + #[inline] + pub fn capacity(&self) -> usize { + self.capacity + } + + /// Returns the number of items that have been pushed to this buffer. + #[inline] + pub fn len(&self) -> usize { + self.data.len() / u64::from(T::min_size()) as usize + } + + /// Returns true if the buffer is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + /// Adds a new value and returns its index. + pub fn push(&mut self, value: T) -> usize { + let element_size = u64::from(T::min_size()) as usize; + let offset = self.data.len(); + + // TODO: Consider using unsafe code to push uninitialized, to prevent + // the zeroing. It shows up in profiles. + self.data.extend(iter::repeat_n(0, element_size)); + + // Take a slice of the new data for `write_into` to use. This is + // important: it hoists the bounds check up here so that the compiler + // can eliminate all the bounds checks that `write_into` will emit. + let mut dest = &mut self.data[offset..(offset + element_size)]; + value.write_into(&mut Writer::new(&value, &mut dest, 0).unwrap()); + + offset / u64::from(T::min_size()) as usize + } + + /// Changes the debugging label of the buffer. + /// + /// The next time the buffer is updated (via [`Self::reserve`]), Bevy will inform + /// the driver of the new label. + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.label_changed = true; + } + + self.label = label; + } + + /// Returns the label + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Creates a [`Buffer`] on the [`RenderDevice`] with size + /// at least `size_of::() * capacity`, unless such a buffer already exists. + /// + /// If a [`Buffer`] exists, but is too small, references to it will be discarded, + /// and a new [`Buffer`] will be created. Any previously created [`Buffer`]s + /// that are no longer referenced will be deleted by the [`RenderDevice`] + /// once it is done using them (typically 1-2 frames). + /// + /// In addition to any [`BufferUsages`] provided when + /// the `BufferVec` was created, the buffer on the [`RenderDevice`] + /// is marked as [`BufferUsages::COPY_DST`](BufferUsages). + pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) { + if capacity <= self.capacity && !self.label_changed { + return; + } + + self.capacity = capacity; + let size = u64::from(T::min_size()) as usize * capacity; + self.buffer = Some(device.create_buffer(&wgpu::BufferDescriptor { + label: self.label.as_deref(), + size: size as BufferAddress, + usage: BufferUsages::COPY_DST | self.buffer_usage, + mapped_at_creation: false, + })); + self.label_changed = false; + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// Before queuing the write, a [`reserve`](BufferVec::reserve) operation is + /// executed. + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + if self.data.is_empty() { + return; + } + + self.reserve(self.data.len() / u64::from(T::min_size()) as usize, device); + + let Some(buffer) = &self.buffer else { return }; + queue.write_buffer(buffer, 0, &self.data); + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// If the buffer is not initialized on the GPU or the range is bigger than the capacity it will + /// return an error. You'll need to either reserve a new buffer which will lose data on the GPU + /// or create a new buffer and copy the old data to it. + /// + /// This will only write the data contained in the given range. It is useful if you only want + /// to update a part of the buffer. + pub fn write_buffer_range( + &mut self, + render_queue: &RenderQueue, + range: core::ops::Range, + ) -> Result<(), WriteBufferRangeError> { + if self.data.is_empty() { + return Err(WriteBufferRangeError::NoValuesToUpload); + } + let item_size = u64::from(T::min_size()) as usize; + if range.end > item_size * self.capacity { + return Err(WriteBufferRangeError::RangeBiggerThanBuffer); + } + if let Some(buffer) = &self.buffer { + let bytes = &self.data[range.start..range.end]; + render_queue.write_buffer(buffer, (range.start * item_size) as u64, bytes); + Ok(()) + } else { + Err(WriteBufferRangeError::BufferNotInitialized) + } + } + + /// Reduces the length of the buffer. + pub fn truncate(&mut self, len: usize) { + self.data.truncate(u64::from(T::min_size()) as usize * len); + } + + /// Removes all elements from the buffer. + pub fn clear(&mut self) { + self.data.clear(); + } +} + +/// Like a [`BufferVec`], but only reserves space on the GPU for elements +/// instead of initializing them CPU-side. +/// +/// This type is useful when you're accumulating "output slots" for a GPU +/// compute shader to write into. +/// +/// The type `T` need not be [`NoUninit`], unlike [`RawBufferVec`]; it only has to +/// be [`GpuArrayBufferable`]. +pub struct UninitBufferVec +where + T: GpuArrayBufferable, +{ + buffer: Option, + len: usize, + capacity: usize, + item_size: usize, + buffer_usage: BufferUsages, + label: Option, + label_changed: bool, + phantom: PhantomData, +} + +impl UninitBufferVec +where + T: GpuArrayBufferable, +{ + /// Creates a new [`UninitBufferVec`] with the given [`BufferUsages`]. + pub const fn new(buffer_usage: BufferUsages) -> Self { + Self { + len: 0, + buffer: None, + capacity: 0, + item_size: size_of::(), + buffer_usage, + label: None, + label_changed: false, + phantom: PhantomData, + } + } + + /// Returns the buffer, if allocated. + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + + /// Reserves space for one more element in the buffer and returns its index. + pub fn add(&mut self) -> usize { + self.add_multiple(1) + } + + /// Reserves space for the given number of elements in the buffer and + /// returns the index of the first one. + pub fn add_multiple(&mut self, count: usize) -> usize { + let index = self.len; + self.len += count; + index + } + + /// Returns true if no elements have been added to this [`UninitBufferVec`]. + pub fn is_empty(&self) -> bool { + self.len == 0 + } + + /// Removes all elements from the buffer. + pub fn clear(&mut self) { + self.len = 0; + } + + /// Returns the length of the buffer. + pub fn len(&self) -> usize { + self.len + } + + /// Materializes the buffer on the GPU with space for `capacity` elements. + /// + /// If the buffer is already big enough, this function doesn't reallocate + /// the buffer. + pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) { + if capacity <= self.capacity && !self.label_changed { + return; + } + + self.capacity = capacity; + let size = self.item_size * capacity; + self.buffer = Some(device.create_buffer(&wgpu::BufferDescriptor { + label: self.label.as_deref(), + size: size as BufferAddress, + usage: BufferUsages::COPY_DST | self.buffer_usage, + mapped_at_creation: false, + })); + + self.label_changed = false; + } + + /// Materializes the buffer on the GPU, with an appropriate size for the + /// elements that have been pushed so far. + pub fn write_buffer(&mut self, device: &RenderDevice) { + if !self.is_empty() { + self.reserve(self.len, device); + } + } +} + +/// Error returned when `write_buffer_range` fails +/// +/// See [`RawBufferVec::write_buffer_range`] [`BufferVec::write_buffer_range`] +#[derive(Debug, Eq, PartialEq, Copy, Clone, Error)] +pub enum WriteBufferRangeError { + #[error("the range is bigger than the capacity of the buffer")] + RangeBiggerThanBuffer, + #[error("the gpu buffer is not initialized")] + BufferNotInitialized, + #[error("there are no values to upload")] + NoValuesToUpload, +} diff --git a/third_party/bevy_render/src/render_resource/gpu_array_buffer.rs b/third_party/bevy_render/src/render_resource/gpu_array_buffer.rs new file mode 100644 index 0000000..c999033 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/gpu_array_buffer.rs @@ -0,0 +1,116 @@ +use super::{ + binding_types::{storage_buffer_read_only, uniform_buffer_sized}, + BindGroupLayoutEntryBuilder, BufferVec, +}; +use crate::{ + render_resource::batched_uniform_buffer::BatchedUniformBuffer, + renderer::{RenderDevice, RenderQueue}, +}; +use bevy_ecs::{prelude::Component, resource::Resource}; +use core::marker::PhantomData; +use encase::{private::WriteInto, ShaderSize, ShaderType}; +use nonmax::NonMaxU32; +use wgpu::{BindingResource, BufferUsages, Limits}; + +/// Trait for types able to go in a [`GpuArrayBuffer`]. +pub trait GpuArrayBufferable: ShaderType + ShaderSize + WriteInto + Clone {} + +impl GpuArrayBufferable for T {} + +/// Stores an array of elements to be transferred to the GPU and made accessible to shaders as a read-only array. +/// +/// On platforms that support storage buffers, this is equivalent to +/// [`BufferVec`]. Otherwise, this falls back to a dynamic offset +/// uniform buffer with the largest array of T that fits within a uniform buffer +/// binding (within reasonable limits). +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`] +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +#[derive(Resource)] +pub enum GpuArrayBuffer { + Uniform(BatchedUniformBuffer), + Storage(BufferVec), +} + +impl GpuArrayBuffer { + pub fn new(limits: &Limits) -> Self { + if limits.max_storage_buffers_per_shader_stage == 0 { + GpuArrayBuffer::Uniform(BatchedUniformBuffer::new(limits)) + } else { + GpuArrayBuffer::Storage(BufferVec::new(BufferUsages::STORAGE)) + } + } + + pub fn clear(&mut self) { + match self { + GpuArrayBuffer::Uniform(buffer) => buffer.clear(), + GpuArrayBuffer::Storage(buffer) => buffer.clear(), + } + } + + pub fn push(&mut self, value: T) -> GpuArrayBufferIndex { + match self { + GpuArrayBuffer::Uniform(buffer) => buffer.push(value), + GpuArrayBuffer::Storage(buffer) => { + let index = buffer.push(value) as u32; + GpuArrayBufferIndex { + index, + dynamic_offset: None, + element_type: PhantomData, + } + } + } + } + + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + match self { + GpuArrayBuffer::Uniform(buffer) => buffer.write_buffer(device, queue), + GpuArrayBuffer::Storage(buffer) => buffer.write_buffer(device, queue), + } + } + + pub fn binding_layout(limits: &Limits) -> BindGroupLayoutEntryBuilder { + if limits.max_storage_buffers_per_shader_stage == 0 { + uniform_buffer_sized( + true, + // BatchedUniformBuffer uses a MaxCapacityArray that is runtime-sized, so we use + // None here and let wgpu figure out the size. + None, + ) + } else { + storage_buffer_read_only::(false) + } + } + + pub fn binding(&self) -> Option> { + match self { + GpuArrayBuffer::Uniform(buffer) => buffer.binding(), + GpuArrayBuffer::Storage(buffer) => buffer.binding(), + } + } + + pub fn batch_size(limits: &Limits) -> Option { + if limits.max_storage_buffers_per_shader_stage == 0 { + Some(BatchedUniformBuffer::::batch_size(limits) as u32) + } else { + None + } + } +} + +/// An index into a [`GpuArrayBuffer`] for a given element. +#[derive(Component, Clone)] +pub struct GpuArrayBufferIndex { + /// The index to use in a shader into the array. + pub index: u32, + /// The dynamic offset to use when setting the bind group in a pass. + /// Only used on platforms that don't support storage buffers. + pub dynamic_offset: Option, + pub element_type: PhantomData, +} diff --git a/third_party/bevy_render/src/render_resource/mod.rs b/third_party/bevy_render/src/render_resource/mod.rs new file mode 100644 index 0000000..0d431b5 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/mod.rs @@ -0,0 +1,74 @@ +mod batched_uniform_buffer; +mod bind_group; +mod bind_group_entries; +mod bind_group_layout; +mod bind_group_layout_entries; +mod bindless; +mod buffer; +mod buffer_vec; +mod gpu_array_buffer; +mod pipeline; +mod pipeline_cache; +mod pipeline_specializer; +pub mod resource_macros; +mod specializer; +mod storage_buffer; +mod texture; +mod uniform_buffer; + +pub use bind_group::*; +pub use bind_group_entries::*; +pub use bind_group_layout::*; +pub use bind_group_layout_entries::*; +pub use bindless::*; +pub use buffer::*; +pub use buffer_vec::*; +pub use gpu_array_buffer::*; +pub use pipeline::*; +pub use pipeline_cache::*; +pub use pipeline_specializer::*; +pub use specializer::*; +pub use storage_buffer::*; +pub use texture::*; +pub use uniform_buffer::*; + +// TODO: decide where re-exports should go +pub use wgpu::{ + util::{ + BufferInitDescriptor, DispatchIndirectArgs, DrawIndexedIndirectArgs, DrawIndirectArgs, + TextureDataOrder, + }, + AccelerationStructureFlags, AccelerationStructureGeometryFlags, + AccelerationStructureUpdateMode, AdapterInfo as WgpuAdapterInfo, AddressMode, AstcBlock, + AstcChannel, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutEntry, BindingResource, + BindingType, Blas, BlasBuildEntry, BlasGeometries, BlasGeometrySizeDescriptors, + BlasTriangleGeometry, BlasTriangleGeometrySizeDescriptor, BlendComponent, BlendFactor, + BlendOperation, BlendState, BufferAddress, BufferAsyncError, BufferBinding, BufferBindingType, + BufferDescriptor, BufferSize, BufferUsages, ColorTargetState, ColorWrites, CommandEncoder, + CommandEncoderDescriptor, CompareFunction, ComputePass, ComputePassDescriptor, + ComputePipelineDescriptor as RawComputePipelineDescriptor, CreateBlasDescriptor, + CreateTlasDescriptor, DepthBiasState, DepthStencilState, DownlevelFlags, Extent3d, Face, + Features as WgpuFeatures, FilterMode, FragmentState as RawFragmentState, FrontFace, + ImageSubresourceRange, IndexFormat, Limits as WgpuLimits, LoadOp, MapMode, MultisampleState, + Operations, Origin3d, PipelineCompilationOptions, PipelineLayout, PipelineLayoutDescriptor, + PollType, PolygonMode, PrimitiveState, PrimitiveTopology, PushConstantRange, + RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor, + RenderPipelineDescriptor as RawRenderPipelineDescriptor, Sampler as WgpuSampler, + SamplerBindingType, SamplerDescriptor, ShaderModule, ShaderModuleDescriptor, ShaderSource, + ShaderStages, StencilFaceState, StencilOperation, StencilState, StorageTextureAccess, StoreOp, + TexelCopyBufferInfo, TexelCopyBufferLayout, TexelCopyTextureInfo, TextureAspect, + TextureDescriptor, TextureDimension, TextureFormat, TextureFormatFeatureFlags, + TextureFormatFeatures, TextureSampleType, TextureUsages, TextureView as WgpuTextureView, + TextureViewDescriptor, TextureViewDimension, Tlas, TlasInstance, VertexAttribute, + VertexBufferLayout as RawVertexBufferLayout, VertexFormat, VertexState as RawVertexState, + VertexStepMode, COPY_BUFFER_ALIGNMENT, +}; + +pub mod encase { + pub use bevy_encase_derive::ShaderType; + pub use encase::*; +} + +pub use self::encase::{ShaderSize, ShaderType}; + +pub use naga::ShaderStage; diff --git a/third_party/bevy_render/src/render_resource/pipeline.rs b/third_party/bevy_render/src/render_resource/pipeline.rs new file mode 100644 index 0000000..f3976f6 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/pipeline.rs @@ -0,0 +1,199 @@ +use crate::define_atomic_id; +use crate::renderer::WgpuWrapper; +use alloc::borrow::Cow; +use bevy_asset::Handle; +use bevy_mesh::VertexBufferLayout; +use bevy_shader::{Shader, ShaderDefVal}; +use core::iter; +use core::ops::Deref; +use thiserror::Error; +use wgpu::{ + BindGroupLayoutEntry, ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState, + PushConstantRange, +}; + +define_atomic_id!(RenderPipelineId); + +/// A [`RenderPipeline`] represents a graphics pipeline and its stages (shaders), bindings and vertex buffers. +/// +/// May be converted from and dereferences to a wgpu [`RenderPipeline`](wgpu::RenderPipeline). +/// Can be created via [`RenderDevice::create_render_pipeline`](crate::renderer::RenderDevice::create_render_pipeline). +#[derive(Clone, Debug)] +pub struct RenderPipeline { + id: RenderPipelineId, + value: WgpuWrapper, +} + +impl RenderPipeline { + #[inline] + pub fn id(&self) -> RenderPipelineId { + self.id + } +} + +impl From for RenderPipeline { + fn from(value: wgpu::RenderPipeline) -> Self { + RenderPipeline { + id: RenderPipelineId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for RenderPipeline { + type Target = wgpu::RenderPipeline; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +define_atomic_id!(ComputePipelineId); + +/// A [`ComputePipeline`] represents a compute pipeline and its single shader stage. +/// +/// May be converted from and dereferences to a wgpu [`ComputePipeline`](wgpu::ComputePipeline). +/// Can be created via [`RenderDevice::create_compute_pipeline`](crate::renderer::RenderDevice::create_compute_pipeline). +#[derive(Clone, Debug)] +pub struct ComputePipeline { + id: ComputePipelineId, + value: WgpuWrapper, +} + +impl ComputePipeline { + /// Returns the [`ComputePipelineId`]. + #[inline] + pub fn id(&self) -> ComputePipelineId { + self.id + } +} + +impl From for ComputePipeline { + fn from(value: wgpu::ComputePipeline) -> Self { + ComputePipeline { + id: ComputePipelineId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for ComputePipeline { + type Target = wgpu::ComputePipeline; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)] +pub struct BindGroupLayoutDescriptor { + /// Debug label of the bind group layout descriptor. This will show up in graphics debuggers for easy identification. + pub label: Cow<'static, str>, + pub entries: Vec, +} + +impl BindGroupLayoutDescriptor { + pub fn new(label: impl Into>, entries: &[BindGroupLayoutEntry]) -> Self { + Self { + label: label.into(), + entries: entries.into(), + } + } +} + +/// Describes a render (graphics) pipeline. +#[derive(Clone, Debug, PartialEq, Default)] +pub struct RenderPipelineDescriptor { + /// Debug label of the pipeline. This will show up in graphics debuggers for easy identification. + pub label: Option>, + /// The layout of bind groups for this pipeline. + pub layout: Vec, + /// The push constant ranges for this pipeline. + /// Supply an empty vector if the pipeline doesn't use push constants. + pub push_constant_ranges: Vec, + /// The compiled vertex stage, its entry point, and the input buffers layout. + pub vertex: VertexState, + /// The properties of the pipeline at the primitive assembly and rasterization level. + pub primitive: PrimitiveState, + /// The effect of draw calls on the depth and stencil aspects of the output target, if any. + pub depth_stencil: Option, + /// The multi-sampling properties of the pipeline. + pub multisample: MultisampleState, + /// The compiled fragment stage, its entry point, and the color targets. + pub fragment: Option, + /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true. + /// If this is false, reading from workgroup variables before writing to them will result in garbage values. + pub zero_initialize_workgroup_memory: bool, +} + +#[derive(Copy, Clone, Debug, Error)] +#[error("RenderPipelineDescriptor has no FragmentState configured")] +pub struct NoFragmentStateError; + +impl RenderPipelineDescriptor { + pub fn fragment_mut(&mut self) -> Result<&mut FragmentState, NoFragmentStateError> { + self.fragment.as_mut().ok_or(NoFragmentStateError) + } + + pub fn set_layout(&mut self, index: usize, layout: BindGroupLayoutDescriptor) { + filling_set_at(&mut self.layout, index, bevy_utils::default(), layout); + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Default)] +pub struct VertexState { + /// The compiled shader module for this stage. + pub shader: Handle, + pub shader_defs: Vec, + /// The name of the entry point in the compiled shader, or `None` if the default entry point + /// is used. + pub entry_point: Option>, + /// The format of any vertex buffers used with this pipeline. + pub buffers: Vec, +} + +/// Describes the fragment process in a render pipeline. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct FragmentState { + /// The compiled shader module for this stage. + pub shader: Handle, + pub shader_defs: Vec, + /// The name of the entry point in the compiled shader, or `None` if the default entry point + /// is used. + pub entry_point: Option>, + /// The color state of the render targets. + pub targets: Vec>, +} + +impl FragmentState { + pub fn set_target(&mut self, index: usize, target: ColorTargetState) { + filling_set_at(&mut self.targets, index, None, Some(target)); + } +} + +/// Describes a compute pipeline. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct ComputePipelineDescriptor { + pub label: Option>, + pub layout: Vec, + pub push_constant_ranges: Vec, + /// The compiled shader module for this stage. + pub shader: Handle, + pub shader_defs: Vec, + /// The name of the entry point in the compiled shader, or `None` if the default entry point + /// is used. + pub entry_point: Option>, + /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true. + /// If this is false, reading from workgroup variables before writing to them will result in garbage values. + pub zero_initialize_workgroup_memory: bool, +} + +// utility function to set a value at the specified index, extending with +// a filler value if the index is out of bounds. +fn filling_set_at(vec: &mut Vec, index: usize, filler: T, value: T) { + let num_to_fill = (index + 1).saturating_sub(vec.len()); + vec.extend(iter::repeat_n(filler, num_to_fill)); + vec[index] = value; +} diff --git a/third_party/bevy_render/src/render_resource/pipeline_cache.rs b/third_party/bevy_render/src/render_resource/pipeline_cache.rs new file mode 100644 index 0000000..e8b6b29 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/pipeline_cache.rs @@ -0,0 +1,882 @@ +use crate::{ + render_resource::*, + renderer::{RenderAdapter, RenderDevice, WgpuWrapper}, + Extract, +}; +use alloc::{borrow::Cow, sync::Arc}; +use bevy_asset::{AssetEvent, AssetId, Assets, Handle}; +use bevy_ecs::{ + message::MessageReader, + resource::Resource, + system::{Res, ResMut}, +}; +use bevy_platform::collections::{HashMap, HashSet}; +use bevy_shader::{ + CachedPipelineId, PipelineCacheError, Shader, ShaderCache, ShaderCacheSource, ShaderDefVal, + ValidateShader, +}; +use bevy_tasks::Task; +use bevy_utils::default; +use core::{future::Future, hash::Hash, mem}; +use std::sync::{Mutex, PoisonError}; +use tracing::error; +use wgpu::{PipelineCompilationOptions, VertexBufferLayout as RawVertexBufferLayout}; + +/// A descriptor for a [`Pipeline`]. +/// +/// Used to store a heterogenous collection of render and compute pipeline descriptors together. +#[derive(Debug)] +pub enum PipelineDescriptor { + RenderPipelineDescriptor(Box), + ComputePipelineDescriptor(Box), +} + +/// A pipeline defining the data layout and shader logic for a specific GPU task. +/// +/// Used to store a heterogenous collection of render and compute pipelines together. +#[derive(Debug)] +pub enum Pipeline { + RenderPipeline(RenderPipeline), + ComputePipeline(ComputePipeline), +} + +/// Index of a cached render pipeline in a [`PipelineCache`]. +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] +pub struct CachedRenderPipelineId(CachedPipelineId); + +impl CachedRenderPipelineId { + /// An invalid cached render pipeline index, often used to initialize a variable. + pub const INVALID: Self = CachedRenderPipelineId(usize::MAX); + + #[inline] + pub fn id(&self) -> usize { + self.0 + } +} + +/// Index of a cached compute pipeline in a [`PipelineCache`]. +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub struct CachedComputePipelineId(CachedPipelineId); + +impl CachedComputePipelineId { + /// An invalid cached compute pipeline index, often used to initialize a variable. + pub const INVALID: Self = CachedComputePipelineId(usize::MAX); + + #[inline] + pub fn id(&self) -> usize { + self.0 + } +} + +pub struct CachedPipeline { + pub descriptor: PipelineDescriptor, + pub state: CachedPipelineState, +} + +/// State of a cached pipeline inserted into a [`PipelineCache`]. +#[cfg_attr( + not(target_arch = "wasm32"), + expect( + clippy::large_enum_variant, + reason = "See https://github.com/bevyengine/bevy/issues/19220" + ) +)] +#[derive(Debug)] +pub enum CachedPipelineState { + /// The pipeline GPU object is queued for creation. + Queued, + /// The pipeline GPU object is being created. + Creating(Task>), + /// The pipeline GPU object was created successfully and is available (allocated on the GPU). + Ok(Pipeline), + /// An error occurred while trying to create the pipeline GPU object. + Err(PipelineCacheError), +} + +impl CachedPipelineState { + /// Convenience method to "unwrap" a pipeline state into its underlying GPU object. + /// + /// # Returns + /// + /// The method returns the allocated pipeline GPU object. + /// + /// # Panics + /// + /// This method panics if the pipeline GPU object is not available, either because it is + /// pending creation or because an error occurred while attempting to create GPU object. + pub fn unwrap(&self) -> &Pipeline { + match self { + CachedPipelineState::Ok(pipeline) => pipeline, + CachedPipelineState::Queued => { + panic!("Pipeline has not been compiled yet. It is still in the 'Queued' state.") + } + CachedPipelineState::Creating(..) => { + panic!("Pipeline has not been compiled yet. It is still in the 'Creating' state.") + } + CachedPipelineState::Err(err) => panic!("{}", err), + } + } +} + +type LayoutCacheKey = (Vec, Vec); +#[derive(Default)] +struct LayoutCache { + layouts: HashMap>>, +} + +impl LayoutCache { + fn get( + &mut self, + render_device: &RenderDevice, + bind_group_layouts: &[BindGroupLayout], + push_constant_ranges: Vec, + ) -> Arc> { + let bind_group_ids = bind_group_layouts.iter().map(BindGroupLayout::id).collect(); + self.layouts + .entry((bind_group_ids, push_constant_ranges)) + .or_insert_with_key(|(_, push_constant_ranges)| { + let bind_group_layouts = bind_group_layouts + .iter() + .map(BindGroupLayout::value) + .collect::>(); + Arc::new(WgpuWrapper::new(render_device.create_pipeline_layout( + &PipelineLayoutDescriptor { + bind_group_layouts: &bind_group_layouts, + push_constant_ranges, + ..default() + }, + ))) + }) + .clone() + } +} + +#[expect( + clippy::result_large_err, + reason = "See https://github.com/bevyengine/bevy/issues/19220" +)] +fn load_module( + render_device: &RenderDevice, + shader_source: ShaderCacheSource, + validate_shader: &ValidateShader, +) -> Result, PipelineCacheError> { + let shader_source = match shader_source { + #[cfg(feature = "shader_format_spirv")] + ShaderCacheSource::SpirV(data) => wgpu::util::make_spirv(data), + #[cfg(not(feature = "shader_format_spirv"))] + ShaderCacheSource::SpirV(_) => { + unimplemented!("Enable feature \"shader_format_spirv\" to use SPIR-V shaders") + } + ShaderCacheSource::Wgsl(src) => ShaderSource::Wgsl(Cow::Owned(src)), + #[cfg(not(feature = "decoupled_naga"))] + ShaderCacheSource::Naga(src) => ShaderSource::Naga(Cow::Owned(src)), + }; + let module_descriptor = ShaderModuleDescriptor { + label: None, + source: shader_source, + }; + + render_device + .wgpu_device() + .push_error_scope(wgpu::ErrorFilter::Validation); + + let shader_module = WgpuWrapper::new(match validate_shader { + ValidateShader::Enabled => { + render_device.create_and_validate_shader_module(module_descriptor) + } + // SAFETY: we are interfacing with shader code, which may contain undefined behavior, + // such as indexing out of bounds. + // The checks required are prohibitively expensive and a poor default for game engines. + ValidateShader::Disabled => unsafe { + render_device.create_shader_module(module_descriptor) + }, + }); + + let error = render_device.wgpu_device().pop_error_scope(); + + // `now_or_never` will return Some if the future is ready and None otherwise. + // On native platforms, wgpu will yield the error immediately while on wasm it may take longer since the browser APIs are asynchronous. + // So to keep the complexity of the ShaderCache low, we will only catch this error early on native platforms, + // and on wasm the error will be handled by wgpu and crash the application. + if let Some(Some(wgpu::Error::Validation { description, .. })) = + bevy_tasks::futures::now_or_never(error) + { + return Err(PipelineCacheError::CreateShaderModule(description)); + } + + Ok(shader_module) +} + +#[derive(Default)] +struct BindGroupLayoutCache { + bgls: HashMap, +} + +impl BindGroupLayoutCache { + fn get( + &mut self, + render_device: &RenderDevice, + descriptor: BindGroupLayoutDescriptor, + ) -> BindGroupLayout { + self.bgls + .entry(descriptor) + .or_insert_with_key(|descriptor| { + render_device + .create_bind_group_layout(descriptor.label.as_ref(), &descriptor.entries) + }) + .clone() + } +} + +/// Cache for render and compute pipelines. +/// +/// The cache stores existing render and compute pipelines allocated on the GPU, as well as +/// pending creation. Pipelines inserted into the cache are identified by a unique ID, which +/// can be used to retrieve the actual GPU object once it's ready. The creation of the GPU +/// pipeline object is deferred to the [`RenderSystems::Render`] step, just before the render +/// graph starts being processed, as this requires access to the GPU. +/// +/// Note that the cache does not perform automatic deduplication of identical pipelines. It is +/// up to the user not to insert the same pipeline twice to avoid wasting GPU resources. +/// +/// [`RenderSystems::Render`]: crate::RenderSystems::Render +#[derive(Resource)] +pub struct PipelineCache { + layout_cache: Arc>, + bindgroup_layout_cache: Arc>, + shader_cache: Arc, RenderDevice>>>, + device: RenderDevice, + pipelines: Vec, + waiting_pipelines: HashSet, + new_pipelines: Mutex>, + global_shader_defs: Vec, + /// If `true`, disables asynchronous pipeline compilation. + /// This has no effect on macOS, wasm, or without the `multi_threaded` feature. + synchronous_pipeline_compilation: bool, +} + +impl PipelineCache { + /// Returns an iterator over the pipelines in the pipeline cache. + pub fn pipelines(&self) -> impl Iterator { + self.pipelines.iter() + } + + /// Returns a iterator of the IDs of all currently waiting pipelines. + pub fn waiting_pipelines(&self) -> impl Iterator + '_ { + self.waiting_pipelines.iter().copied() + } + + /// Create a new pipeline cache associated with the given render device. + pub fn new( + device: RenderDevice, + render_adapter: RenderAdapter, + synchronous_pipeline_compilation: bool, + ) -> Self { + let mut global_shader_defs = Vec::new(); + #[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))] + { + global_shader_defs.push("NO_ARRAY_TEXTURES_SUPPORT".into()); + global_shader_defs.push("NO_CUBE_ARRAY_TEXTURES_SUPPORT".into()); + global_shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into()); + } + + if cfg!(target_abi = "sim") { + global_shader_defs.push("NO_CUBE_ARRAY_TEXTURES_SUPPORT".into()); + } + + global_shader_defs.push(ShaderDefVal::UInt( + String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"), + device.limits().max_storage_buffers_per_shader_stage, + )); + + Self { + shader_cache: Arc::new(Mutex::new(ShaderCache::new( + device.features(), + render_adapter.get_downlevel_capabilities().flags, + load_module, + ))), + device, + layout_cache: default(), + bindgroup_layout_cache: default(), + waiting_pipelines: default(), + new_pipelines: default(), + pipelines: default(), + global_shader_defs, + synchronous_pipeline_compilation, + } + } + + /// Get the state of a cached render pipeline. + /// + /// See [`PipelineCache::queue_render_pipeline()`]. + #[inline] + pub fn get_render_pipeline_state(&self, id: CachedRenderPipelineId) -> &CachedPipelineState { + // If the pipeline id isn't in `pipelines`, it's queued in `new_pipelines` + self.pipelines + .get(id.0) + .map_or(&CachedPipelineState::Queued, |pipeline| &pipeline.state) + } + + /// Get the state of a cached compute pipeline. + /// + /// See [`PipelineCache::queue_compute_pipeline()`]. + #[inline] + pub fn get_compute_pipeline_state(&self, id: CachedComputePipelineId) -> &CachedPipelineState { + // If the pipeline id isn't in `pipelines`, it's queued in `new_pipelines` + self.pipelines + .get(id.0) + .map_or(&CachedPipelineState::Queued, |pipeline| &pipeline.state) + } + + /// Get the render pipeline descriptor a cached render pipeline was inserted from. + /// + /// See [`PipelineCache::queue_render_pipeline()`]. + /// + /// **Note**: Be careful calling this method. It will panic if called with a pipeline that + /// has been queued but has not yet been processed by [`PipelineCache::process_queue()`]. + #[inline] + pub fn get_render_pipeline_descriptor( + &self, + id: CachedRenderPipelineId, + ) -> &RenderPipelineDescriptor { + match &self.pipelines[id.0].descriptor { + PipelineDescriptor::RenderPipelineDescriptor(descriptor) => descriptor, + PipelineDescriptor::ComputePipelineDescriptor(_) => unreachable!(), + } + } + + /// Get the compute pipeline descriptor a cached render pipeline was inserted from. + /// + /// See [`PipelineCache::queue_compute_pipeline()`]. + /// + /// **Note**: Be careful calling this method. It will panic if called with a pipeline that + /// has been queued but has not yet been processed by [`PipelineCache::process_queue()`]. + #[inline] + pub fn get_compute_pipeline_descriptor( + &self, + id: CachedComputePipelineId, + ) -> &ComputePipelineDescriptor { + match &self.pipelines[id.0].descriptor { + PipelineDescriptor::RenderPipelineDescriptor(_) => unreachable!(), + PipelineDescriptor::ComputePipelineDescriptor(descriptor) => descriptor, + } + } + + /// Try to retrieve a render pipeline GPU object from a cached ID. + /// + /// # Returns + /// + /// This method returns a successfully created render pipeline if any, or `None` if the pipeline + /// was not created yet or if there was an error during creation. You can check the actual creation + /// state with [`PipelineCache::get_render_pipeline_state()`]. + #[inline] + pub fn get_render_pipeline(&self, id: CachedRenderPipelineId) -> Option<&RenderPipeline> { + if let CachedPipelineState::Ok(Pipeline::RenderPipeline(pipeline)) = + &self.pipelines.get(id.0)?.state + { + Some(pipeline) + } else { + None + } + } + + /// Wait for a render pipeline to finish compiling. + #[inline] + pub fn block_on_render_pipeline(&mut self, id: CachedRenderPipelineId) { + if self.pipelines.len() <= id.0 { + self.process_queue(); + } + + let state = &mut self.pipelines[id.0].state; + if let CachedPipelineState::Creating(task) = state { + *state = match bevy_tasks::block_on(task) { + Ok(p) => CachedPipelineState::Ok(p), + Err(e) => CachedPipelineState::Err(e), + }; + } + } + + /// Try to retrieve a compute pipeline GPU object from a cached ID. + /// + /// # Returns + /// + /// This method returns a successfully created compute pipeline if any, or `None` if the pipeline + /// was not created yet or if there was an error during creation. You can check the actual creation + /// state with [`PipelineCache::get_compute_pipeline_state()`]. + #[inline] + pub fn get_compute_pipeline(&self, id: CachedComputePipelineId) -> Option<&ComputePipeline> { + if let CachedPipelineState::Ok(Pipeline::ComputePipeline(pipeline)) = + &self.pipelines.get(id.0)?.state + { + Some(pipeline) + } else { + None + } + } + + /// Insert a render pipeline into the cache, and queue its creation. + /// + /// The pipeline is always inserted and queued for creation. There is no attempt to deduplicate it with + /// an already cached pipeline. + /// + /// # Returns + /// + /// This method returns the unique render shader ID of the cached pipeline, which can be used to query + /// the caching state with [`get_render_pipeline_state()`] and to retrieve the created GPU pipeline once + /// it's ready with [`get_render_pipeline()`]. + /// + /// [`get_render_pipeline_state()`]: PipelineCache::get_render_pipeline_state + /// [`get_render_pipeline()`]: PipelineCache::get_render_pipeline + pub fn queue_render_pipeline( + &self, + descriptor: RenderPipelineDescriptor, + ) -> CachedRenderPipelineId { + let mut new_pipelines = self + .new_pipelines + .lock() + .unwrap_or_else(PoisonError::into_inner); + let id = CachedRenderPipelineId(self.pipelines.len() + new_pipelines.len()); + new_pipelines.push(CachedPipeline { + descriptor: PipelineDescriptor::RenderPipelineDescriptor(Box::new(descriptor)), + state: CachedPipelineState::Queued, + }); + id + } + + /// Insert a compute pipeline into the cache, and queue its creation. + /// + /// The pipeline is always inserted and queued for creation. There is no attempt to deduplicate it with + /// an already cached pipeline. + /// + /// # Returns + /// + /// This method returns the unique compute shader ID of the cached pipeline, which can be used to query + /// the caching state with [`get_compute_pipeline_state()`] and to retrieve the created GPU pipeline once + /// it's ready with [`get_compute_pipeline()`]. + /// + /// [`get_compute_pipeline_state()`]: PipelineCache::get_compute_pipeline_state + /// [`get_compute_pipeline()`]: PipelineCache::get_compute_pipeline + pub fn queue_compute_pipeline( + &self, + descriptor: ComputePipelineDescriptor, + ) -> CachedComputePipelineId { + let mut new_pipelines = self + .new_pipelines + .lock() + .unwrap_or_else(PoisonError::into_inner); + let id = CachedComputePipelineId(self.pipelines.len() + new_pipelines.len()); + new_pipelines.push(CachedPipeline { + descriptor: PipelineDescriptor::ComputePipelineDescriptor(Box::new(descriptor)), + state: CachedPipelineState::Queued, + }); + id + } + + pub fn get_bind_group_layout( + &self, + bind_group_layout_descriptor: &BindGroupLayoutDescriptor, + ) -> BindGroupLayout { + self.bindgroup_layout_cache + .lock() + .unwrap() + .get(&self.device, bind_group_layout_descriptor.clone()) + } + + /// Inserts a [`Shader`] into this cache with the provided [`AssetId`]. + pub fn set_shader(&mut self, id: AssetId, shader: Shader) { + let mut shader_cache = self.shader_cache.lock().unwrap(); + let pipelines_to_queue = shader_cache.set_shader(id, shader); + for cached_pipeline in pipelines_to_queue { + self.pipelines[cached_pipeline].state = CachedPipelineState::Queued; + self.waiting_pipelines.insert(cached_pipeline); + } + } + + /// Removes a [`Shader`] from this cache if it exists. + pub fn remove_shader(&mut self, shader: AssetId) { + let mut shader_cache = self.shader_cache.lock().unwrap(); + let pipelines_to_queue = shader_cache.remove(shader); + for cached_pipeline in pipelines_to_queue { + self.pipelines[cached_pipeline].state = CachedPipelineState::Queued; + self.waiting_pipelines.insert(cached_pipeline); + } + } + + fn start_create_render_pipeline( + &mut self, + id: CachedPipelineId, + descriptor: RenderPipelineDescriptor, + ) -> CachedPipelineState { + let device = self.device.clone(); + let shader_cache = self.shader_cache.clone(); + let layout_cache = self.layout_cache.clone(); + let mut bindgroup_layout_cache = self.bindgroup_layout_cache.lock().unwrap(); + let bind_group_layout = descriptor + .layout + .iter() + .map(|bind_group_layout_descriptor| { + bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor.clone()) + }) + .collect::>(); + + create_pipeline_task( + async move { + let mut shader_cache = shader_cache.lock().unwrap(); + let mut layout_cache = layout_cache.lock().unwrap(); + + let vertex_module = match shader_cache.get( + &device, + id, + descriptor.vertex.shader.id(), + &descriptor.vertex.shader_defs, + ) { + Ok(module) => module, + Err(err) => return Err(err), + }; + + let fragment_module = match &descriptor.fragment { + Some(fragment) => { + match shader_cache.get( + &device, + id, + fragment.shader.id(), + &fragment.shader_defs, + ) { + Ok(module) => Some(module), + Err(err) => return Err(err), + } + } + None => None, + }; + + let layout = + if descriptor.layout.is_empty() && descriptor.push_constant_ranges.is_empty() { + None + } else { + Some(layout_cache.get( + &device, + &bind_group_layout, + descriptor.push_constant_ranges.to_vec(), + )) + }; + + drop((shader_cache, layout_cache)); + + let vertex_buffer_layouts = descriptor + .vertex + .buffers + .iter() + .map(|layout| RawVertexBufferLayout { + array_stride: layout.array_stride, + attributes: &layout.attributes, + step_mode: layout.step_mode, + }) + .collect::>(); + + let fragment_data = descriptor.fragment.as_ref().map(|fragment| { + ( + fragment_module.unwrap(), + fragment.entry_point.as_deref(), + fragment.targets.as_slice(), + ) + }); + + // TODO: Expose the rest of this somehow + let compilation_options = PipelineCompilationOptions { + constants: &[], + zero_initialize_workgroup_memory: descriptor.zero_initialize_workgroup_memory, + }; + + let descriptor = RawRenderPipelineDescriptor { + multiview: None, + depth_stencil: descriptor.depth_stencil.clone(), + label: descriptor.label.as_deref(), + layout: layout.as_ref().map(|layout| -> &PipelineLayout { layout }), + multisample: descriptor.multisample, + primitive: descriptor.primitive, + vertex: RawVertexState { + buffers: &vertex_buffer_layouts, + entry_point: descriptor.vertex.entry_point.as_deref(), + module: &vertex_module, + // TODO: Should this be the same as the fragment compilation options? + compilation_options: compilation_options.clone(), + }, + fragment: fragment_data + .as_ref() + .map(|(module, entry_point, targets)| RawFragmentState { + entry_point: entry_point.as_deref(), + module, + targets, + // TODO: Should this be the same as the vertex compilation options? + compilation_options, + }), + cache: None, + }; + + Ok(Pipeline::RenderPipeline( + device.create_render_pipeline(&descriptor), + )) + }, + self.synchronous_pipeline_compilation, + ) + } + + fn start_create_compute_pipeline( + &mut self, + id: CachedPipelineId, + descriptor: ComputePipelineDescriptor, + ) -> CachedPipelineState { + let device = self.device.clone(); + let shader_cache = self.shader_cache.clone(); + let layout_cache = self.layout_cache.clone(); + let mut bindgroup_layout_cache = self.bindgroup_layout_cache.lock().unwrap(); + let bind_group_layout = descriptor + .layout + .iter() + .map(|bind_group_layout_descriptor| { + bindgroup_layout_cache.get(&self.device, bind_group_layout_descriptor.clone()) + }) + .collect::>(); + + create_pipeline_task( + async move { + let mut shader_cache = shader_cache.lock().unwrap(); + let mut layout_cache = layout_cache.lock().unwrap(); + + let compute_module = match shader_cache.get( + &device, + id, + descriptor.shader.id(), + &descriptor.shader_defs, + ) { + Ok(module) => module, + Err(err) => return Err(err), + }; + + let layout = + if descriptor.layout.is_empty() && descriptor.push_constant_ranges.is_empty() { + None + } else { + Some(layout_cache.get( + &device, + &bind_group_layout, + descriptor.push_constant_ranges.to_vec(), + )) + }; + + drop((shader_cache, layout_cache)); + + let descriptor = RawComputePipelineDescriptor { + label: descriptor.label.as_deref(), + layout: layout.as_ref().map(|layout| -> &PipelineLayout { layout }), + module: &compute_module, + entry_point: descriptor.entry_point.as_deref(), + // TODO: Expose the rest of this somehow + compilation_options: PipelineCompilationOptions { + constants: &[], + zero_initialize_workgroup_memory: descriptor + .zero_initialize_workgroup_memory, + }, + cache: None, + }; + + Ok(Pipeline::ComputePipeline( + device.create_compute_pipeline(&descriptor), + )) + }, + self.synchronous_pipeline_compilation, + ) + } + + /// Process the pipeline queue and create all pending pipelines if possible. + /// + /// This is generally called automatically during the [`RenderSystems::Render`] step, but can + /// be called manually to force creation at a different time. + /// + /// [`RenderSystems::Render`]: crate::RenderSystems::Render + pub fn process_queue(&mut self) { + let mut waiting_pipelines = mem::take(&mut self.waiting_pipelines); + let mut pipelines = mem::take(&mut self.pipelines); + + { + let mut new_pipelines = self + .new_pipelines + .lock() + .unwrap_or_else(PoisonError::into_inner); + for new_pipeline in new_pipelines.drain(..) { + let id = pipelines.len(); + pipelines.push(new_pipeline); + waiting_pipelines.insert(id); + } + } + + for id in waiting_pipelines { + self.process_pipeline(&mut pipelines[id], id); + } + + self.pipelines = pipelines; + } + + fn process_pipeline(&mut self, cached_pipeline: &mut CachedPipeline, id: usize) { + match &mut cached_pipeline.state { + CachedPipelineState::Queued => { + cached_pipeline.state = match &cached_pipeline.descriptor { + PipelineDescriptor::RenderPipelineDescriptor(descriptor) => { + self.start_create_render_pipeline(id, *descriptor.clone()) + } + PipelineDescriptor::ComputePipelineDescriptor(descriptor) => { + self.start_create_compute_pipeline(id, *descriptor.clone()) + } + }; + } + + CachedPipelineState::Creating(task) => match bevy_tasks::futures::check_ready(task) { + Some(Ok(pipeline)) => { + cached_pipeline.state = CachedPipelineState::Ok(pipeline); + return; + } + Some(Err(err)) => cached_pipeline.state = CachedPipelineState::Err(err), + _ => (), + }, + + CachedPipelineState::Err(err) => match err { + // Retry + PipelineCacheError::ShaderNotLoaded(_) + | PipelineCacheError::ShaderImportNotYetAvailable => { + cached_pipeline.state = CachedPipelineState::Queued; + } + + // Shader could not be processed ... retrying won't help + PipelineCacheError::ProcessShaderError(err) => { + let error_detail = + err.emit_to_string(&self.shader_cache.lock().unwrap().composer); + if std::env::var("VERBOSE_SHADER_ERROR") + .is_ok_and(|v| !(v.is_empty() || v == "0" || v == "false")) + { + error!("{}", pipeline_error_context(cached_pipeline)); + } + error!("failed to process shader error:\n{}", error_detail); + return; + } + PipelineCacheError::CreateShaderModule(description) => { + error!("failed to create shader module: {}", description); + return; + } + }, + + CachedPipelineState::Ok(_) => return, + } + + // Retry + self.waiting_pipelines.insert(id); + } + + pub(crate) fn process_pipeline_queue_system(mut cache: ResMut) { + cache.process_queue(); + } + + pub(crate) fn extract_shaders( + mut cache: ResMut, + shaders: Extract>>, + mut events: Extract>>, + ) { + for event in events.read() { + #[expect( + clippy::match_same_arms, + reason = "LoadedWithDependencies is marked as a TODO, so it's likely this will no longer lint soon." + )] + match event { + // PERF: Instead of blocking waiting for the shader cache lock, try again next frame if the lock is currently held + AssetEvent::Added { id } | AssetEvent::Modified { id } => { + if let Some(shader) = shaders.get(*id) { + let mut shader = shader.clone(); + shader.shader_defs.extend(cache.global_shader_defs.clone()); + + cache.set_shader(*id, shader); + } + } + AssetEvent::Removed { id } => cache.remove_shader(*id), + AssetEvent::Unused { .. } => {} + AssetEvent::LoadedWithDependencies { .. } => { + // TODO: handle this + } + } + } + } +} + +fn pipeline_error_context(cached_pipeline: &CachedPipeline) -> String { + fn format( + shader: &Handle, + entry: &Option>, + shader_defs: &[ShaderDefVal], + ) -> String { + let source = match shader.path() { + Some(path) => path.path().to_string_lossy().to_string(), + None => String::new(), + }; + let entry = match entry { + Some(entry) => entry.to_string(), + None => String::new(), + }; + let shader_defs = shader_defs + .iter() + .flat_map(|def| match def { + ShaderDefVal::Bool(k, v) if *v => Some(k.to_string()), + ShaderDefVal::Int(k, v) => Some(format!("{k} = {v}")), + ShaderDefVal::UInt(k, v) => Some(format!("{k} = {v}")), + _ => None, + }) + .collect::>() + .join(", "); + format!("{source}:{entry}\nshader defs: {shader_defs}") + } + match &cached_pipeline.descriptor { + PipelineDescriptor::RenderPipelineDescriptor(desc) => { + let vert = &desc.vertex; + let vert_str = format(&vert.shader, &vert.entry_point, &vert.shader_defs); + let Some(frag) = desc.fragment.as_ref() else { + return vert_str; + }; + let frag_str = format(&frag.shader, &frag.entry_point, &frag.shader_defs); + format!("vertex {vert_str}\nfragment {frag_str}") + } + PipelineDescriptor::ComputePipelineDescriptor(desc) => { + format(&desc.shader, &desc.entry_point, &desc.shader_defs) + } + } +} + +#[cfg(all( + not(target_arch = "wasm32"), + not(target_os = "macos"), + feature = "multi_threaded" +))] +fn create_pipeline_task( + task: impl Future> + Send + 'static, + sync: bool, +) -> CachedPipelineState { + if !sync { + return CachedPipelineState::Creating(bevy_tasks::AsyncComputeTaskPool::get().spawn(task)); + } + + match bevy_tasks::block_on(task) { + Ok(pipeline) => CachedPipelineState::Ok(pipeline), + Err(err) => CachedPipelineState::Err(err), + } +} + +#[cfg(any( + target_arch = "wasm32", + target_os = "macos", + not(feature = "multi_threaded") +))] +fn create_pipeline_task( + task: impl Future> + Send + 'static, + _sync: bool, +) -> CachedPipelineState { + match bevy_tasks::block_on(task) { + Ok(pipeline) => CachedPipelineState::Ok(pipeline), + Err(err) => CachedPipelineState::Err(err), + } +} diff --git a/third_party/bevy_render/src/render_resource/pipeline_specializer.rs b/third_party/bevy_render/src/render_resource/pipeline_specializer.rs new file mode 100644 index 0000000..f2e84a2 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/pipeline_specializer.rs @@ -0,0 +1,259 @@ +use crate::render_resource::{ + CachedComputePipelineId, CachedRenderPipelineId, ComputePipelineDescriptor, PipelineCache, + RenderPipelineDescriptor, +}; +use bevy_ecs::resource::Resource; +use bevy_mesh::{MeshVertexBufferLayoutRef, MissingVertexAttributeError, VertexBufferLayout}; +use bevy_platform::{ + collections::{ + hash_map::{Entry, RawEntryMut, VacantEntry}, + HashMap, + }, + hash::FixedHasher, +}; +use bevy_utils::default; +use core::{fmt::Debug, hash::Hash}; +use thiserror::Error; +use tracing::error; + +/// A trait that allows constructing different variants of a render pipeline from a key. +/// +/// Note: This is intended for modifying your pipeline descriptor on the basis of a key. If your key +/// contains no data then you don't need to specialize. For example, if you are using the +/// [`AsBindGroup`](crate::render_resource::AsBindGroup) without the `#[bind_group_data]` attribute, +/// you don't need to specialize. Instead, create the pipeline directly from [`PipelineCache`] and +/// store its ID. +/// +/// See [`SpecializedRenderPipelines`] for more info. +pub trait SpecializedRenderPipeline { + /// The key that defines each "variant" of the render pipeline. + type Key: Clone + Hash + PartialEq + Eq; + + /// Construct a new render pipeline based on the provided key. + fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor; +} + +/// A convenience cache for creating different variants of a render pipeline based on some key. +/// +/// Some render pipelines may need to be configured differently depending on the exact situation. +/// This cache allows constructing different render pipelines for each situation based on a key, +/// making it easy to A) construct the necessary pipelines, and B) reuse already constructed +/// pipelines. +/// +/// Note: This is intended for modifying your pipeline descriptor on the basis of a key. If your key +/// contains no data then you don't need to specialize. For example, if you are using the +/// [`AsBindGroup`](crate::render_resource::AsBindGroup) without the `#[bind_group_data]` attribute, +/// you don't need to specialize. Instead, create the pipeline directly from [`PipelineCache`] and +/// store its ID. +#[derive(Resource)] +pub struct SpecializedRenderPipelines { + cache: HashMap, +} + +impl Default for SpecializedRenderPipelines { + fn default() -> Self { + Self { cache: default() } + } +} + +impl SpecializedRenderPipelines { + /// Get or create a specialized instance of the pipeline corresponding to `key`. + pub fn specialize( + &mut self, + cache: &PipelineCache, + pipeline_specializer: &S, + key: S::Key, + ) -> CachedRenderPipelineId { + *self.cache.entry(key.clone()).or_insert_with(|| { + let descriptor = pipeline_specializer.specialize(key); + cache.queue_render_pipeline(descriptor) + }) + } +} + +/// A trait that allows constructing different variants of a compute pipeline from a key. +/// +/// Note: This is intended for modifying your pipeline descriptor on the basis of a key. If your key +/// contains no data then you don't need to specialize. For example, if you are using the +/// [`AsBindGroup`](crate::render_resource::AsBindGroup) without the `#[bind_group_data]` attribute, +/// you don't need to specialize. Instead, create the pipeline directly from [`PipelineCache`] and +/// store its ID. +/// +/// See [`SpecializedComputePipelines`] for more info. +pub trait SpecializedComputePipeline { + /// The key that defines each "variant" of the compute pipeline. + type Key: Clone + Hash + PartialEq + Eq; + + /// Construct a new compute pipeline based on the provided key. + fn specialize(&self, key: Self::Key) -> ComputePipelineDescriptor; +} + +/// A convenience cache for creating different variants of a compute pipeline based on some key. +/// +/// Some compute pipelines may need to be configured differently depending on the exact situation. +/// This cache allows constructing different compute pipelines for each situation based on a key, +/// making it easy to A) construct the necessary pipelines, and B) reuse already constructed +/// pipelines. +/// +/// Note: This is intended for modifying your pipeline descriptor on the basis of a key. If your key +/// contains no data then you don't need to specialize. For example, if you are using the +/// [`AsBindGroup`](crate::render_resource::AsBindGroup) without the `#[bind_group_data]` attribute, +/// you don't need to specialize. Instead, create the pipeline directly from [`PipelineCache`] and +/// store its ID. +#[derive(Resource)] +pub struct SpecializedComputePipelines { + cache: HashMap, +} + +impl Default for SpecializedComputePipelines { + fn default() -> Self { + Self { cache: default() } + } +} + +impl SpecializedComputePipelines { + /// Get or create a specialized instance of the pipeline corresponding to `key`. + pub fn specialize( + &mut self, + cache: &PipelineCache, + specialize_pipeline: &S, + key: S::Key, + ) -> CachedComputePipelineId { + *self.cache.entry(key.clone()).or_insert_with(|| { + let descriptor = specialize_pipeline.specialize(key); + cache.queue_compute_pipeline(descriptor) + }) + } +} + +/// A trait that allows constructing different variants of a render pipeline from a key and the +/// particular mesh's vertex buffer layout. +/// +/// See [`SpecializedMeshPipelines`] for more info. +pub trait SpecializedMeshPipeline { + /// The key that defines each "variant" of the render pipeline. + type Key: Clone + Hash + PartialEq + Eq; + + /// Construct a new render pipeline based on the provided key and vertex layout. + /// + /// The returned pipeline descriptor should have a single vertex buffer, which is derived from + /// `layout`. + fn specialize( + &self, + key: Self::Key, + layout: &MeshVertexBufferLayoutRef, + ) -> Result; +} + +/// A cache of different variants of a render pipeline based on a key and the particular mesh's +/// vertex buffer layout. +#[derive(Resource)] +pub struct SpecializedMeshPipelines { + mesh_layout_cache: HashMap<(MeshVertexBufferLayoutRef, S::Key), CachedRenderPipelineId>, + vertex_layout_cache: VertexLayoutCache, +} + +type VertexLayoutCache = HashMap< + VertexBufferLayout, + HashMap<::Key, CachedRenderPipelineId>, +>; + +impl Default for SpecializedMeshPipelines { + fn default() -> Self { + Self { + mesh_layout_cache: Default::default(), + vertex_layout_cache: Default::default(), + } + } +} + +impl SpecializedMeshPipelines { + /// Construct a new render pipeline based on the provided key and the mesh's vertex buffer + /// layout. + #[inline] + pub fn specialize( + &mut self, + cache: &PipelineCache, + pipeline_specializer: &S, + key: S::Key, + layout: &MeshVertexBufferLayoutRef, + ) -> Result { + return match self.mesh_layout_cache.entry((layout.clone(), key.clone())) { + Entry::Occupied(entry) => Ok(*entry.into_mut()), + Entry::Vacant(entry) => specialize_slow( + &mut self.vertex_layout_cache, + cache, + pipeline_specializer, + key, + layout, + entry, + ), + }; + + #[cold] + fn specialize_slow( + vertex_layout_cache: &mut VertexLayoutCache, + cache: &PipelineCache, + specialize_pipeline: &S, + key: S::Key, + layout: &MeshVertexBufferLayoutRef, + entry: VacantEntry< + (MeshVertexBufferLayoutRef, S::Key), + CachedRenderPipelineId, + FixedHasher, + >, + ) -> Result + where + S: SpecializedMeshPipeline, + { + let descriptor = specialize_pipeline + .specialize(key.clone(), layout) + .map_err(|mut err| { + { + let SpecializedMeshPipelineError::MissingVertexAttribute(err) = &mut err; + err.pipeline_type = Some(core::any::type_name::()); + } + err + })?; + // Different MeshVertexBufferLayouts can produce the same final VertexBufferLayout + // We want compatible vertex buffer layouts to use the same pipelines, so we must "deduplicate" them + let layout_map = match vertex_layout_cache + .raw_entry_mut() + .from_key(&descriptor.vertex.buffers[0]) + { + RawEntryMut::Occupied(entry) => entry.into_mut(), + RawEntryMut::Vacant(entry) => { + entry + .insert(descriptor.vertex.buffers[0].clone(), Default::default()) + .1 + } + }; + Ok(*entry.insert(match layout_map.entry(key) { + Entry::Occupied(entry) => { + if cfg!(debug_assertions) { + let stored_descriptor = cache.get_render_pipeline_descriptor(*entry.get()); + if stored_descriptor != &descriptor { + error!( + "The cached pipeline descriptor for {} is not \ + equal to the generated descriptor for the given key. \ + This means the SpecializePipeline implementation uses \ + unused' MeshVertexBufferLayout information to specialize \ + the pipeline. This is not allowed because it would invalidate \ + the pipeline cache.", + core::any::type_name::() + ); + } + } + *entry.into_mut() + } + Entry::Vacant(entry) => *entry.insert(cache.queue_render_pipeline(descriptor)), + })) + } + } +} + +#[derive(Error, Debug)] +pub enum SpecializedMeshPipelineError { + #[error(transparent)] + MissingVertexAttribute(#[from] MissingVertexAttributeError), +} diff --git a/third_party/bevy_render/src/render_resource/resource_macros.rs b/third_party/bevy_render/src/render_resource/resource_macros.rs new file mode 100644 index 0000000..6cdf3b6 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/resource_macros.rs @@ -0,0 +1,39 @@ +#[macro_export] +macro_rules! define_atomic_id { + ($atomic_id_type:ident) => { + #[derive(Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)] + pub struct $atomic_id_type(core::num::NonZero); + + impl $atomic_id_type { + #[expect( + clippy::new_without_default, + reason = "Implementing the `Default` trait on atomic IDs would imply that two `::default()` equal each other. By only implementing `new()`, we indicate that each atomic ID created will be unique." + )] + pub fn new() -> Self { + use core::sync::atomic::{AtomicU32, Ordering}; + + static COUNTER: AtomicU32 = AtomicU32::new(1); + + let counter = COUNTER.fetch_add(1, Ordering::Relaxed); + Self(core::num::NonZero::::new(counter).unwrap_or_else(|| { + panic!( + "The system ran out of unique `{}`s.", + stringify!($atomic_id_type) + ); + })) + } + } + + impl From<$atomic_id_type> for core::num::NonZero { + fn from(value: $atomic_id_type) -> Self { + value.0 + } + } + + impl From> for $atomic_id_type { + fn from(value: core::num::NonZero) -> Self { + Self(value) + } + } + }; +} diff --git a/third_party/bevy_render/src/render_resource/specializer.rs b/third_party/bevy_render/src/render_resource/specializer.rs new file mode 100644 index 0000000..2dc5257 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/specializer.rs @@ -0,0 +1,353 @@ +use super::{ + CachedComputePipelineId, CachedRenderPipelineId, ComputePipeline, ComputePipelineDescriptor, + PipelineCache, RenderPipeline, RenderPipelineDescriptor, +}; +use bevy_ecs::error::BevyError; +use bevy_platform::{ + collections::{ + hash_map::{Entry, VacantEntry}, + HashMap, + }, + hash::FixedHasher, +}; +use core::{hash::Hash, marker::PhantomData}; +use tracing::error; +use variadics_please::all_tuples; + +pub use bevy_render_macros::{Specializer, SpecializerKey}; + +/// Defines a type that is able to be "specialized" and cached by creating and transforming +/// its descriptor type. This is implemented for [`RenderPipeline`] and [`ComputePipeline`], and +/// likely will not have much utility for other types. +/// +/// See docs on [`Specializer`] for more info. +pub trait Specializable { + type Descriptor: PartialEq + Clone + Send + Sync; + type CachedId: Clone + Send + Sync; + fn queue(pipeline_cache: &PipelineCache, descriptor: Self::Descriptor) -> Self::CachedId; + fn get_descriptor(pipeline_cache: &PipelineCache, id: Self::CachedId) -> &Self::Descriptor; +} + +impl Specializable for RenderPipeline { + type Descriptor = RenderPipelineDescriptor; + type CachedId = CachedRenderPipelineId; + + fn queue(pipeline_cache: &PipelineCache, descriptor: Self::Descriptor) -> Self::CachedId { + pipeline_cache.queue_render_pipeline(descriptor) + } + + fn get_descriptor( + pipeline_cache: &PipelineCache, + id: CachedRenderPipelineId, + ) -> &Self::Descriptor { + pipeline_cache.get_render_pipeline_descriptor(id) + } +} + +impl Specializable for ComputePipeline { + type Descriptor = ComputePipelineDescriptor; + + type CachedId = CachedComputePipelineId; + + fn queue(pipeline_cache: &PipelineCache, descriptor: Self::Descriptor) -> Self::CachedId { + pipeline_cache.queue_compute_pipeline(descriptor) + } + + fn get_descriptor( + pipeline_cache: &PipelineCache, + id: CachedComputePipelineId, + ) -> &Self::Descriptor { + pipeline_cache.get_compute_pipeline_descriptor(id) + } +} + +/// Defines a type capable of "specializing" values of a type T. +/// +/// Specialization is the process of generating variants of a type T +/// from small hashable keys, and specializers themselves can be +/// thought of as [pure functions] from the key type to `T`, that +/// [memoize] their results based on the key. +/// +///

+/// +/// Since compiling render and compute pipelines can be so slow, +/// specialization allows a Bevy app to detect when it would compile +/// a duplicate pipeline and reuse what's already in the cache. While +/// pipelines could all be memoized hashing each whole descriptor, this +/// would be much slower and could still create duplicates. In contrast, +/// memoizing groups of *related* pipelines based on a small hashable +/// key is much faster. See the docs on [`SpecializerKey`] for more info. +/// +/// ## Composing Specializers +/// +/// This trait can be derived with `#[derive(Specializer)]` for structs whose +/// fields all implement [`Specializer`]. This allows for composing multiple +/// specializers together, and makes encapsulation and separating concerns +/// between specializers much nicer. One could make individual specializers +/// for common operations and place them in entirely separate modules, then +/// compose them together with a single `#[derive]` +/// +/// ```rust +/// # use bevy_ecs::error::BevyError; +/// # use bevy_render::render_resource::Specializer; +/// # use bevy_render::render_resource::SpecializerKey; +/// # use bevy_render::render_resource::RenderPipeline; +/// # use bevy_render::render_resource::RenderPipelineDescriptor; +/// struct A; +/// struct B; +/// #[derive(Copy, Clone, PartialEq, Eq, Hash, SpecializerKey)] +/// struct BKey { contrived_number: u32 }; +/// +/// impl Specializer for A { +/// type Key = (); +/// +/// fn specialize( +/// &self, +/// key: (), +/// descriptor: &mut RenderPipelineDescriptor +/// ) -> Result<(), BevyError> { +/// # let _ = descriptor; +/// // mutate the descriptor here +/// Ok(key) +/// } +/// } +/// +/// impl Specializer for B { +/// type Key = BKey; +/// +/// fn specialize( +/// &self, +/// key: BKey, +/// descriptor: &mut RenderPipelineDescriptor +/// ) -> Result { +/// # let _ = descriptor; +/// // mutate the descriptor here +/// Ok(key) +/// } +/// } +/// +/// #[derive(Specializer)] +/// #[specialize(RenderPipeline)] +/// struct C { +/// #[key(default)] +/// a: A, +/// b: B, +/// } +/// +/// /* +/// The generated implementation: +/// impl Specializer for C { +/// type Key = BKey; +/// fn specialize( +/// &self, +/// key: Self::Key, +/// descriptor: &mut RenderPipelineDescriptor +/// ) -> Result, BevyError> { +/// let _ = self.a.specialize((), descriptor); +/// let key = self.b.specialize(key, descriptor); +/// Ok(key) +/// } +/// } +/// */ +/// ``` +/// +/// The key type for a composed specializer will be a tuple of the keys +/// of each field, and their specialization logic will be applied in field +/// order. Since derive macros can't have generic parameters, the derive macro +/// requires an additional `#[specialize(..targets)]` attribute to specify a +/// list of types to target for the implementation. `#[specialize(all)]` is +/// also allowed, and will generate a fully generic implementation at the cost +/// of slightly worse error messages. +/// +/// Additionally, each field can optionally take a `#[key]` attribute to +/// specify a "key override". This will hide that field's key from being +/// exposed by the wrapper, and always use the value given by the attribute. +/// Values for this attribute may either be `default` which will use the key's +/// [`Default`] implementation, or a valid rust expression of the key type. +/// +/// [pure functions]: https://en.wikipedia.org/wiki/Pure_function +/// [memoize]: https://en.wikipedia.org/wiki/Memoization +pub trait Specializer: Send + Sync + 'static { + type Key: SpecializerKey; + fn specialize( + &self, + key: Self::Key, + descriptor: &mut T::Descriptor, + ) -> Result, BevyError>; +} + +// TODO: update docs for `SpecializerKey` with a more concrete example +// once we've migrated mesh layout specialization + +/// Defines a type that is able to be used as a key for [`Specializer`]s +/// +///
+/// Most types should implement this trait with the included derive macro.
+/// This generates a "canonical" key type, with IS_CANONICAL = true, and Canonical = Self +///
+/// +/// ## What's a "canonical" key? +/// +/// The specialization API memoizes pipelines based on the hash of each key, but this +/// can still produce duplicates. For example, if one used a list of vertex attributes +/// as a key, even if all the same attributes were present they could be in any order. +/// In each case, though the keys would be "different" they would produce the same +/// pipeline. +/// +/// To address this, during specialization keys are processed into a [canonical] +/// (or "standard") form that represents the actual descriptor that was produced. +/// In the previous example, that would be the final `VertexBufferLayout` contained +/// by the pipeline descriptor. This new key is used by [`Variants`] to +/// perform additional checks for duplicates, but only if required. If a key is +/// canonical from the start, then there's no need. +/// +/// For implementors: the main property of a canonical key is that if two keys hash +/// differently, they should nearly always produce different descriptors. +/// +/// [canonical]: https://en.wikipedia.org/wiki/Canonicalization +pub trait SpecializerKey: Clone + Hash + Eq { + /// Denotes whether this key is canonical or not. This should only be `true` + /// if and only if `Canonical = Self`. + const IS_CANONICAL: bool; + + /// The canonical key type to convert this into during specialization. + type Canonical: Hash + Eq; +} + +pub type Canonical = ::Canonical; + +impl Specializer for () { + type Key = (); + + fn specialize( + &self, + _key: Self::Key, + _descriptor: &mut T::Descriptor, + ) -> Result<(), BevyError> { + Ok(()) + } +} + +impl Specializer for PhantomData { + type Key = (); + + fn specialize( + &self, + _key: Self::Key, + _descriptor: &mut T::Descriptor, + ) -> Result<(), BevyError> { + Ok(()) + } +} + +macro_rules! impl_specialization_key_tuple { + ($(#[$meta:meta])* $($T:ident),*) => { + $(#[$meta])* + impl <$($T: SpecializerKey),*> SpecializerKey for ($($T,)*) { + const IS_CANONICAL: bool = true $(&& <$T as SpecializerKey>::IS_CANONICAL)*; + type Canonical = ($(Canonical<$T>,)*); + } + }; +} + +all_tuples!( + #[doc(fake_variadic)] + impl_specialization_key_tuple, + 0, + 12, + T +); + +/// A cache for variants of a resource type created by a specializer. +/// At most one resource will be created for each key. +pub struct Variants> { + specializer: S, + base_descriptor: T::Descriptor, + primary_cache: HashMap, + secondary_cache: HashMap, T::CachedId>, +} + +impl> Variants { + /// Creates a new [`Variants`] from a [`Specializer`] and a base descriptor. + #[inline] + pub fn new(specializer: S, base_descriptor: T::Descriptor) -> Self { + Self { + specializer, + base_descriptor, + primary_cache: Default::default(), + secondary_cache: Default::default(), + } + } + + /// Specializes a resource given the [`Specializer`]'s key type. + #[inline] + pub fn specialize( + &mut self, + pipeline_cache: &PipelineCache, + key: S::Key, + ) -> Result { + let entry = self.primary_cache.entry(key.clone()); + match entry { + Entry::Occupied(entry) => Ok(entry.get().clone()), + Entry::Vacant(entry) => Self::specialize_slow( + &self.specializer, + self.base_descriptor.clone(), + pipeline_cache, + key, + entry, + &mut self.secondary_cache, + ), + } + } + + #[cold] + fn specialize_slow( + specializer: &S, + base_descriptor: T::Descriptor, + pipeline_cache: &PipelineCache, + key: S::Key, + primary_entry: VacantEntry, + secondary_cache: &mut HashMap, T::CachedId>, + ) -> Result { + let mut descriptor = base_descriptor.clone(); + let canonical_key = specializer.specialize(key.clone(), &mut descriptor)?; + + // if the whole key is canonical, the secondary cache isn't needed. + if ::IS_CANONICAL { + return Ok(primary_entry + .insert(::queue(pipeline_cache, descriptor)) + .clone()); + } + + let id = match secondary_cache.entry(canonical_key) { + Entry::Occupied(entry) => { + if cfg!(debug_assertions) { + let stored_descriptor = + ::get_descriptor(pipeline_cache, entry.get().clone()); + if &descriptor != stored_descriptor { + error!( + "Invalid Specializer<{}> impl for {}: the cached descriptor \ + is not equal to the generated descriptor for the given key. \ + This means the Specializer implementation uses unused information \ + from the key to specialize the pipeline. This is not allowed \ + because it would invalidate the cache.", + core::any::type_name::(), + core::any::type_name::() + ); + } + } + entry.into_mut().clone() + } + Entry::Vacant(entry) => entry + .insert(::queue(pipeline_cache, descriptor)) + .clone(), + }; + + primary_entry.insert(id.clone()); + Ok(id) + } +} diff --git a/third_party/bevy_render/src/render_resource/storage_buffer.rs b/third_party/bevy_render/src/render_resource/storage_buffer.rs new file mode 100644 index 0000000..dd76f46 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/storage_buffer.rs @@ -0,0 +1,285 @@ +use core::marker::PhantomData; + +use super::Buffer; +use crate::renderer::{RenderDevice, RenderQueue}; +use encase::{ + internal::WriteInto, DynamicStorageBuffer as DynamicStorageBufferWrapper, ShaderType, + StorageBuffer as StorageBufferWrapper, +}; +use wgpu::{util::BufferInitDescriptor, BindingResource, BufferBinding, BufferSize, BufferUsages}; + +use super::IntoBinding; + +/// Stores data to be transferred to the GPU and made accessible to shaders as a storage buffer. +/// +/// Storage buffers can be made available to shaders in some combination of read/write mode, and can store large amounts of data. +/// Note however that WebGL2 does not support storage buffers, so consider alternative options in this case. +/// +/// Storage buffers can store runtime-sized arrays, but only if they are the last field in a structure. +/// +/// The contained data is stored in system RAM. [`write_buffer`](StorageBuffer::write_buffer) queues +/// copying of the data from system RAM to VRAM. Storage buffers must conform to [std430 alignment/padding requirements], which +/// is automatically enforced by this structure. +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`](crate::render_resource::BufferVec) +/// * [`DynamicStorageBuffer`] +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +/// +/// [std430 alignment/padding requirements]: https://www.w3.org/TR/WGSL/#address-spaces-storage +pub struct StorageBuffer { + value: T, + scratch: StorageBufferWrapper>, + buffer: Option, + label: Option, + changed: bool, + buffer_usage: BufferUsages, + last_written_size: Option, +} + +impl From for StorageBuffer { + fn from(value: T) -> Self { + Self { + value, + scratch: StorageBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::STORAGE, + last_written_size: None, + } + } +} + +impl Default for StorageBuffer { + fn default() -> Self { + Self { + value: T::default(), + scratch: StorageBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::STORAGE, + last_written_size: None, + } + } +} + +impl StorageBuffer { + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer(BufferBinding { + buffer: self.buffer()?, + offset: 0, + size: self.last_written_size, + })) + } + + pub fn set(&mut self, value: T) { + self.value = value; + } + + pub fn get(&self) -> &T { + &self.value + } + + pub fn get_mut(&mut self) -> &mut T { + &mut self.value + } + + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.changed = true; + } + + self.label = label; + } + + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Add more [`BufferUsages`] to the buffer. + /// + /// This method only allows addition of flags to the default usage flags. + /// + /// The default values for buffer usage are `BufferUsages::COPY_DST` and `BufferUsages::STORAGE`. + pub fn add_usages(&mut self, usage: BufferUsages) { + self.buffer_usage |= usage; + self.changed = true; + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// If there is no GPU-side buffer allocated to hold the data currently stored, or if a GPU-side buffer previously + /// allocated does not have enough capacity, a new GPU-side buffer is created. + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + self.scratch.write(&self.value).unwrap(); + + let capacity = self.buffer.as_deref().map(wgpu::Buffer::size).unwrap_or(0); + let size = self.scratch.as_ref().len() as u64; + + if capacity < size || self.changed { + self.buffer = Some(device.create_buffer_with_data(&BufferInitDescriptor { + label: self.label.as_deref(), + usage: self.buffer_usage, + contents: self.scratch.as_ref(), + })); + self.changed = false; + } else if let Some(buffer) = &self.buffer { + queue.write_buffer(buffer, 0, self.scratch.as_ref()); + } + + self.last_written_size = BufferSize::new(size); + } +} + +impl<'a, T: ShaderType + WriteInto> IntoBinding<'a> for &'a StorageBuffer { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + self.binding().expect("Failed to get buffer") + } +} + +/// Stores data to be transferred to the GPU and made accessible to shaders as a dynamic storage buffer. +/// +/// This is just a [`StorageBuffer`], but also allows you to set dynamic offsets. +/// +/// Dynamic storage buffers can be made available to shaders in some combination of read/write mode, and can store large amounts +/// of data. Note however that WebGL2 does not support storage buffers, so consider alternative options in this case. Dynamic +/// storage buffers support multiple separate bindings at dynamic byte offsets and so have a +/// [`push`](DynamicStorageBuffer::push) method. +/// +/// The contained data is stored in system RAM. [`write_buffer`](DynamicStorageBuffer::write_buffer) +/// queues copying of the data from system RAM to VRAM. The data within a storage buffer binding must conform to +/// [std430 alignment/padding requirements]. `DynamicStorageBuffer` takes care of serializing the inner type to conform to +/// these requirements. Each item [`push`](DynamicStorageBuffer::push)ed into this structure +/// will additionally be aligned to meet dynamic offset alignment requirements. +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`](crate::render_resource::BufferVec) +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`StorageBuffer`] +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +/// +/// [std430 alignment/padding requirements]: https://www.w3.org/TR/WGSL/#address-spaces-storage +pub struct DynamicStorageBuffer { + scratch: DynamicStorageBufferWrapper>, + buffer: Option, + label: Option, + changed: bool, + buffer_usage: BufferUsages, + last_written_size: Option, + _marker: PhantomData T>, +} + +impl Default for DynamicStorageBuffer { + fn default() -> Self { + Self { + scratch: DynamicStorageBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::STORAGE, + last_written_size: None, + _marker: PhantomData, + } + } +} + +impl DynamicStorageBuffer { + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer(BufferBinding { + buffer: self.buffer()?, + offset: 0, + size: self.last_written_size, + })) + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.scratch.as_ref().is_empty() + } + + #[inline] + pub fn push(&mut self, value: T) -> u32 { + self.scratch.write(&value).unwrap() as u32 + } + + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.changed = true; + } + + self.label = label; + } + + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Add more [`BufferUsages`] to the buffer. + /// + /// This method only allows addition of flags to the default usage flags. + /// + /// The default values for buffer usage are `BufferUsages::COPY_DST` and `BufferUsages::STORAGE`. + pub fn add_usages(&mut self, usage: BufferUsages) { + self.buffer_usage |= usage; + self.changed = true; + } + + #[inline] + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + let capacity = self.buffer.as_deref().map(wgpu::Buffer::size).unwrap_or(0); + let size = self.scratch.as_ref().len() as u64; + + if capacity < size || (self.changed && size > 0) { + self.buffer = Some(device.create_buffer_with_data(&BufferInitDescriptor { + label: self.label.as_deref(), + usage: self.buffer_usage, + contents: self.scratch.as_ref(), + })); + self.changed = false; + } else if let Some(buffer) = &self.buffer { + queue.write_buffer(buffer, 0, self.scratch.as_ref()); + } + + self.last_written_size = BufferSize::new(size); + } + + #[inline] + pub fn clear(&mut self) { + self.scratch.as_mut().clear(); + self.scratch.set_offset(0); + } +} + +impl<'a, T: ShaderType + WriteInto> IntoBinding<'a> for &'a DynamicStorageBuffer { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + self.binding().expect("Failed to get buffer") + } +} diff --git a/third_party/bevy_render/src/render_resource/texture.rs b/third_party/bevy_render/src/render_resource/texture.rs new file mode 100644 index 0000000..bedbbcd --- /dev/null +++ b/third_party/bevy_render/src/render_resource/texture.rs @@ -0,0 +1,166 @@ +use crate::define_atomic_id; +use crate::renderer::WgpuWrapper; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::resource::Resource; +use core::ops::Deref; + +define_atomic_id!(TextureId); + +/// A GPU-accessible texture. +/// +/// May be converted from and dereferences to a wgpu [`Texture`](wgpu::Texture). +/// Can be created via [`RenderDevice::create_texture`](crate::renderer::RenderDevice::create_texture). +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`](crate::render_resource::BufferVec) +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`UniformBuffer`](crate::render_resource::UniformBuffer) +#[derive(Clone, Debug)] +pub struct Texture { + id: TextureId, + value: WgpuWrapper, +} + +impl Texture { + /// Returns the [`TextureId`]. + #[inline] + pub fn id(&self) -> TextureId { + self.id + } + + /// Creates a view of this texture. + pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView { + TextureView::from(self.value.create_view(desc)) + } +} + +impl From for Texture { + fn from(value: wgpu::Texture) -> Self { + Texture { + id: TextureId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for Texture { + type Target = wgpu::Texture; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +define_atomic_id!(TextureViewId); + +/// Describes a [`Texture`] with its associated metadata required by a pipeline or [`BindGroup`](super::BindGroup). +#[derive(Clone, Debug)] +pub struct TextureView { + id: TextureViewId, + value: WgpuWrapper, +} + +pub struct SurfaceTexture { + value: WgpuWrapper, +} + +impl SurfaceTexture { + pub fn present(self) { + self.value.into_inner().present(); + } +} + +impl TextureView { + /// Returns the [`TextureViewId`]. + #[inline] + pub fn id(&self) -> TextureViewId { + self.id + } +} + +impl From for TextureView { + fn from(value: wgpu::TextureView) -> Self { + TextureView { + id: TextureViewId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl From for SurfaceTexture { + fn from(value: wgpu::SurfaceTexture) -> Self { + SurfaceTexture { + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for TextureView { + type Target = wgpu::TextureView; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +impl Deref for SurfaceTexture { + type Target = wgpu::SurfaceTexture; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +define_atomic_id!(SamplerId); + +/// A Sampler defines how a pipeline will sample from a [`TextureView`]. +/// They define image filters (including anisotropy) and address (wrapping) modes, among other things. +/// +/// May be converted from and dereferences to a wgpu [`Sampler`](wgpu::Sampler). +/// Can be created via [`RenderDevice::create_sampler`](crate::renderer::RenderDevice::create_sampler). +#[derive(Clone, Debug)] +pub struct Sampler { + id: SamplerId, + value: WgpuWrapper, +} + +impl Sampler { + /// Returns the [`SamplerId`]. + #[inline] + pub fn id(&self) -> SamplerId { + self.id + } +} + +impl From for Sampler { + fn from(value: wgpu::Sampler) -> Self { + Sampler { + id: SamplerId::new(), + value: WgpuWrapper::new(value), + } + } +} + +impl Deref for Sampler { + type Target = wgpu::Sampler; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.value + } +} + +/// A rendering resource for the default image sampler which is set during renderer +/// initialization. +/// +/// The [`ImagePlugin`](bevy_image::ImagePlugin) can be set during app initialization to change the default +/// image sampler. +#[derive(Resource, Debug, Clone, Deref, DerefMut)] +pub struct DefaultImageSampler(pub(crate) Sampler); diff --git a/third_party/bevy_render/src/render_resource/uniform_buffer.rs b/third_party/bevy_render/src/render_resource/uniform_buffer.rs new file mode 100644 index 0000000..0fe9350 --- /dev/null +++ b/third_party/bevy_render/src/render_resource/uniform_buffer.rs @@ -0,0 +1,402 @@ +use core::{marker::PhantomData, num::NonZero}; + +use crate::{ + render_resource::Buffer, + renderer::{RenderDevice, RenderQueue}, +}; +use encase::{ + internal::{AlignmentValue, BufferMut, WriteInto}, + DynamicUniformBuffer as DynamicUniformBufferWrapper, ShaderType, + UniformBuffer as UniformBufferWrapper, +}; +use wgpu::{ + util::BufferInitDescriptor, BindingResource, BufferBinding, BufferDescriptor, BufferUsages, +}; + +use super::IntoBinding; + +/// Stores data to be transferred to the GPU and made accessible to shaders as a uniform buffer. +/// +/// Uniform buffers are available to shaders on a read-only basis. Uniform buffers are commonly used to make available to shaders +/// parameters that are constant during shader execution, and are best used for data that is relatively small in size as they are +/// only guaranteed to support up to 16kB per binding. +/// +/// The contained data is stored in system RAM. [`write_buffer`](UniformBuffer::write_buffer) queues +/// copying of the data from system RAM to VRAM. Data in uniform buffers must follow [std140 alignment/padding requirements], +/// which is automatically enforced by this structure. Per the WGPU spec, uniform buffers cannot store runtime-sized array +/// (vectors), or structures with fields that are vectors. +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`](crate::render_resource::BufferVec) +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`DynamicUniformBuffer`] +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`Texture`](crate::render_resource::Texture) +/// +/// [std140 alignment/padding requirements]: https://www.w3.org/TR/WGSL/#address-spaces-uniform +pub struct UniformBuffer { + value: T, + scratch: UniformBufferWrapper>, + buffer: Option, + label: Option, + changed: bool, + buffer_usage: BufferUsages, +} + +impl From for UniformBuffer { + fn from(value: T) -> Self { + Self { + value, + scratch: UniformBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::UNIFORM, + } + } +} + +impl Default for UniformBuffer { + fn default() -> Self { + Self { + value: T::default(), + scratch: UniformBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::UNIFORM, + } + } +} + +impl UniformBuffer { + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + + /// Set the data the buffer stores. + pub fn set(&mut self, value: T) { + self.value = value; + } + + pub fn get(&self) -> &T { + &self.value + } + + pub fn get_mut(&mut self) -> &mut T { + &mut self.value + } + + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.changed = true; + } + + self.label = label; + } + + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Add more [`BufferUsages`] to the buffer. + /// + /// This method only allows addition of flags to the default usage flags. + /// + /// The default values for buffer usage are `BufferUsages::COPY_DST` and `BufferUsages::UNIFORM`. + pub fn add_usages(&mut self, usage: BufferUsages) { + self.buffer_usage |= usage; + self.changed = true; + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`], if a GPU-side backing buffer already exists. + /// + /// If a GPU-side buffer does not already exist for this data, such a buffer is initialized with currently + /// available data. + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + self.scratch.write(&self.value).unwrap(); + + if self.changed || self.buffer.is_none() { + self.buffer = Some(device.create_buffer_with_data(&BufferInitDescriptor { + label: self.label.as_deref(), + usage: self.buffer_usage, + contents: self.scratch.as_ref(), + })); + self.changed = false; + } else if let Some(buffer) = &self.buffer { + queue.write_buffer(buffer, 0, self.scratch.as_ref()); + } + } +} + +impl<'a, T: ShaderType + WriteInto> IntoBinding<'a> for &'a UniformBuffer { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + self.buffer() + .expect("Failed to get buffer") + .as_entire_buffer_binding() + .into_binding() + } +} + +/// Stores data to be transferred to the GPU and made accessible to shaders as a dynamic uniform buffer. +/// +/// Dynamic uniform buffers are available to shaders on a read-only basis. Dynamic uniform buffers are commonly used to make +/// available to shaders runtime-sized arrays of parameters that are otherwise constant during shader execution, and are best +/// suited to data that is relatively small in size as they are only guaranteed to support up to 16kB per binding. +/// +/// The contained data is stored in system RAM. [`write_buffer`](DynamicUniformBuffer::write_buffer) queues +/// copying of the data from system RAM to VRAM. Data in uniform buffers must follow [std140 alignment/padding requirements], +/// which is automatically enforced by this structure. Per the WGPU spec, uniform buffers cannot store runtime-sized array +/// (vectors), or structures with fields that are vectors. +/// +/// Other options for storing GPU-accessible data are: +/// * [`BufferVec`](crate::render_resource::BufferVec) +/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) +/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) +/// * [`RawBufferVec`](crate::render_resource::RawBufferVec) +/// * [`StorageBuffer`](crate::render_resource::StorageBuffer) +/// * [`Texture`](crate::render_resource::Texture) +/// * [`UniformBuffer`] +/// +/// [std140 alignment/padding requirements]: https://www.w3.org/TR/WGSL/#address-spaces-uniform +pub struct DynamicUniformBuffer { + scratch: DynamicUniformBufferWrapper>, + buffer: Option, + label: Option, + changed: bool, + buffer_usage: BufferUsages, + _marker: PhantomData T>, +} + +impl Default for DynamicUniformBuffer { + fn default() -> Self { + Self { + scratch: DynamicUniformBufferWrapper::new(Vec::new()), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::UNIFORM, + _marker: PhantomData, + } + } +} + +impl DynamicUniformBuffer { + pub fn new_with_alignment(alignment: u64) -> Self { + Self { + scratch: DynamicUniformBufferWrapper::new_with_alignment(Vec::new(), alignment), + buffer: None, + label: None, + changed: false, + buffer_usage: BufferUsages::COPY_DST | BufferUsages::UNIFORM, + _marker: PhantomData, + } + } + + #[inline] + pub fn buffer(&self) -> Option<&Buffer> { + self.buffer.as_ref() + } + + #[inline] + pub fn binding(&self) -> Option> { + Some(BindingResource::Buffer(BufferBinding { + buffer: self.buffer()?, + offset: 0, + size: Some(T::min_size()), + })) + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.scratch.as_ref().is_empty() + } + + /// Push data into the `DynamicUniformBuffer`'s internal vector (residing on system RAM). + #[inline] + pub fn push(&mut self, value: &T) -> u32 { + self.scratch.write(value).unwrap() as u32 + } + + pub fn set_label(&mut self, label: Option<&str>) { + let label = label.map(str::to_string); + + if label != self.label { + self.changed = true; + } + + self.label = label; + } + + pub fn get_label(&self) -> Option<&str> { + self.label.as_deref() + } + + /// Add more [`BufferUsages`] to the buffer. + /// + /// This method only allows addition of flags to the default usage flags. + /// + /// The default values for buffer usage are `BufferUsages::COPY_DST` and `BufferUsages::UNIFORM`. + pub fn add_usages(&mut self, usage: BufferUsages) { + self.buffer_usage |= usage; + self.changed = true; + } + + /// Creates a writer that can be used to directly write elements into the target buffer. + /// + /// This method uses less memory and performs fewer memory copies using over [`push`] and [`write_buffer`]. + /// + /// `max_count` *must* be greater than or equal to the number of elements that are to be written to the buffer, or + /// the writer will panic while writing. Dropping the writer will schedule the buffer write into the provided + /// [`RenderQueue`]. + /// + /// If there is no GPU-side buffer allocated to hold the data currently stored, or if a GPU-side buffer previously + /// allocated does not have enough capacity to hold `max_count` elements, a new GPU-side buffer is created. + /// + /// Returns `None` if there is no allocated GPU-side buffer, and `max_count` is 0. + /// + /// [`push`]: Self::push + /// [`write_buffer`]: Self::write_buffer + #[inline] + pub fn get_writer<'a>( + &'a mut self, + max_count: usize, + device: &RenderDevice, + queue: &'a RenderQueue, + ) -> Option> { + let alignment = if cfg!(target_abi = "sim") { + // On iOS simulator on silicon macs, metal validation check that the host OS alignment + // is respected, but the device reports the correct value for iOS, which is smaller. + // Use the larger value. + // See https://github.com/gfx-rs/wgpu/issues/7057 - remove if it's not needed anymore. + AlignmentValue::new(256) + } else { + AlignmentValue::new(device.limits().min_uniform_buffer_offset_alignment as u64) + }; + + let mut capacity = self.buffer.as_deref().map(wgpu::Buffer::size).unwrap_or(0); + let size = alignment + .round_up(T::min_size().get()) + .checked_mul(max_count as u64) + .unwrap(); + + if capacity < size || (self.changed && size > 0) { + let buffer = device.create_buffer(&BufferDescriptor { + label: self.label.as_deref(), + usage: self.buffer_usage, + size, + mapped_at_creation: false, + }); + capacity = buffer.size(); + self.buffer = Some(buffer); + self.changed = false; + } + + if let Some(buffer) = self.buffer.as_deref() { + let buffer_view = queue + .write_buffer_with(buffer, 0, NonZero::::new(buffer.size())?) + .unwrap(); + Some(DynamicUniformBufferWriter { + buffer: encase::DynamicUniformBuffer::new_with_alignment( + QueueWriteBufferViewWrapper { + capacity: capacity as usize, + buffer_view, + }, + alignment.get(), + ), + _marker: PhantomData, + }) + } else { + None + } + } + + /// Queues writing of data from system RAM to VRAM using the [`RenderDevice`] + /// and the provided [`RenderQueue`]. + /// + /// If there is no GPU-side buffer allocated to hold the data currently stored, or if a GPU-side buffer previously + /// allocated does not have enough capacity, a new GPU-side buffer is created. + #[inline] + pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { + let capacity = self.buffer.as_deref().map(wgpu::Buffer::size).unwrap_or(0); + let size = self.scratch.as_ref().len() as u64; + + if capacity < size || (self.changed && size > 0) { + self.buffer = Some(device.create_buffer_with_data(&BufferInitDescriptor { + label: self.label.as_deref(), + usage: self.buffer_usage, + contents: self.scratch.as_ref(), + })); + self.changed = false; + } else if let Some(buffer) = &self.buffer { + queue.write_buffer(buffer, 0, self.scratch.as_ref()); + } + } + + #[inline] + pub fn clear(&mut self) { + self.scratch.as_mut().clear(); + self.scratch.set_offset(0); + } +} + +/// A writer that can be used to directly write elements into the target buffer. +/// +/// For more information, see [`DynamicUniformBuffer::get_writer`]. +pub struct DynamicUniformBufferWriter { + buffer: encase::DynamicUniformBuffer, + _marker: PhantomData T>, +} + +impl DynamicUniformBufferWriter { + pub fn write(&mut self, value: &T) -> u32 { + self.buffer.write(value).unwrap() as u32 + } +} + +/// A wrapper to work around the orphan rule so that [`wgpu::QueueWriteBufferView`] can implement +/// [`BufferMut`]. +struct QueueWriteBufferViewWrapper { + buffer_view: wgpu::QueueWriteBufferView, + // Must be kept separately and cannot be retrieved from buffer_view, as the read-only access will + // invoke a panic. + capacity: usize, +} + +impl BufferMut for QueueWriteBufferViewWrapper { + #[inline] + fn capacity(&self) -> usize { + self.capacity + } + + #[inline] + fn write(&mut self, offset: usize, val: &[u8; N]) { + self.buffer_view.write(offset, val); + } + + #[inline] + fn write_slice(&mut self, offset: usize, val: &[u8]) { + self.buffer_view.write_slice(offset, val); + } +} + +impl<'a, T: ShaderType + WriteInto> IntoBinding<'a> for &'a DynamicUniformBuffer { + #[inline] + fn into_binding(self) -> BindingResource<'a> { + self.binding().unwrap() + } +} diff --git a/third_party/bevy_render/src/renderer/graph_runner.rs b/third_party/bevy_render/src/renderer/graph_runner.rs new file mode 100644 index 0000000..c547372 --- /dev/null +++ b/third_party/bevy_render/src/renderer/graph_runner.rs @@ -0,0 +1,292 @@ +use bevy_ecs::{prelude::Entity, world::World}; +use bevy_platform::collections::HashMap; +#[cfg(feature = "trace")] +use tracing::info_span; + +use alloc::{borrow::Cow, collections::VecDeque}; +use smallvec::{smallvec, SmallVec}; +use thiserror::Error; + +use crate::{ + diagnostic::internal::{DiagnosticsRecorder, RenderDiagnosticsMutex}, + render_graph::{ + Edge, InternedRenderLabel, InternedRenderSubGraph, NodeRunError, NodeState, RenderGraph, + RenderGraphContext, SlotLabel, SlotType, SlotValue, + }, + renderer::{RenderContext, RenderDevice}, +}; + +/// The [`RenderGraphRunner`] is responsible for executing a [`RenderGraph`]. +/// +/// It will run all nodes in the graph sequentially in the correct order (defined by the edges). +/// Each [`Node`](crate::render_graph::Node) can run any arbitrary code, but will generally +/// either send directly a [`CommandBuffer`] or a task that will asynchronously generate a [`CommandBuffer`] +/// +/// After running the graph, the [`RenderGraphRunner`] will execute in parallel all the tasks to get +/// an ordered list of [`CommandBuffer`]s to execute. These [`CommandBuffer`] will be submitted to the GPU +/// sequentially in the order that the tasks were submitted. (which is the order of the [`RenderGraph`]) +/// +/// [`CommandBuffer`]: wgpu::CommandBuffer +pub(crate) struct RenderGraphRunner; + +#[derive(Error, Debug)] +pub enum RenderGraphRunnerError { + #[error(transparent)] + NodeRunError(#[from] NodeRunError), + #[error("node output slot not set (index {slot_index}, name {slot_name})")] + EmptyNodeOutputSlot { + type_name: &'static str, + slot_index: usize, + slot_name: Cow<'static, str>, + }, + #[error("graph '{sub_graph:?}' could not be run because slot '{slot_name}' at index {slot_index} has no value")] + MissingInput { + slot_index: usize, + slot_name: Cow<'static, str>, + sub_graph: Option, + }, + #[error("attempted to use the wrong type for input slot")] + MismatchedInputSlotType { + slot_index: usize, + label: SlotLabel, + expected: SlotType, + actual: SlotType, + }, + #[error( + "node (name: '{node_name:?}') has {slot_count} input slots, but was provided {value_count} values" + )] + MismatchedInputCount { + node_name: InternedRenderLabel, + slot_count: usize, + value_count: usize, + }, +} + +impl RenderGraphRunner { + pub fn run( + graph: &RenderGraph, + render_device: RenderDevice, + mut diagnostics_recorder: Option, + queue: &wgpu::Queue, + world: &World, + finalizer: impl FnOnce(&mut wgpu::CommandEncoder), + ) -> Result, RenderGraphRunnerError> { + if let Some(recorder) = &mut diagnostics_recorder { + recorder.begin_frame(); + } + + let mut render_context = RenderContext::new(render_device, diagnostics_recorder); + Self::run_graph(graph, None, &mut render_context, world, &[], None, None)?; + finalizer(render_context.command_encoder()); + + let (render_device, mut diagnostics_recorder) = { + let (commands, render_device, diagnostics_recorder) = render_context.finish(); + + #[cfg(feature = "trace")] + let _span = info_span!("submit_graph_commands").entered(); + queue.submit(commands); + + (render_device, diagnostics_recorder) + }; + + if let Some(recorder) = &mut diagnostics_recorder { + let render_diagnostics_mutex = world.resource::().0.clone(); + recorder.finish_frame(&render_device, move |diagnostics| { + *render_diagnostics_mutex.lock().expect("lock poisoned") = Some(diagnostics); + }); + } + + Ok(diagnostics_recorder) + } + + /// Runs the [`RenderGraph`] and all its sub-graphs sequentially, making sure that all nodes are + /// run in the correct order. (a node only runs when all its dependencies have finished running) + fn run_graph<'w>( + graph: &RenderGraph, + sub_graph: Option, + render_context: &mut RenderContext<'w>, + world: &'w World, + inputs: &[SlotValue], + view_entity: Option, + debug_group: Option, + ) -> Result<(), RenderGraphRunnerError> { + let mut node_outputs: HashMap> = + HashMap::default(); + #[cfg(feature = "trace")] + let span = if let Some(render_label) = &sub_graph { + let name = format!("{render_label:?}"); + if let Some(debug_group) = debug_group.as_ref() { + info_span!("run_graph", name = name, debug_group = debug_group) + } else { + info_span!("run_graph", name = name) + } + } else { + info_span!("run_graph", name = "main_graph") + }; + #[cfg(feature = "trace")] + let _guard = span.enter(); + + if let Some(debug_group) = debug_group.as_ref() { + // wgpu 27 changed the debug_group validation which makes it impossible to have + // a debug_group that spans multiple command encoders. + // + // + // + // For now, we use a debug_marker as a workaround + render_context + .command_encoder() + .insert_debug_marker(&format!("Start {debug_group}")); + } + + // Queue up nodes without inputs, which can be run immediately + let mut node_queue: VecDeque<&NodeState> = graph + .iter_nodes() + .filter(|node| node.input_slots.is_empty()) + .collect(); + + // pass inputs into the graph + if let Some(input_node) = graph.get_input_node() { + let mut input_values: SmallVec<[SlotValue; 4]> = SmallVec::new(); + for (i, input_slot) in input_node.input_slots.iter().enumerate() { + if let Some(input_value) = inputs.get(i) { + if input_slot.slot_type != input_value.slot_type() { + return Err(RenderGraphRunnerError::MismatchedInputSlotType { + slot_index: i, + actual: input_value.slot_type(), + expected: input_slot.slot_type, + label: input_slot.name.clone().into(), + }); + } + input_values.push(input_value.clone()); + } else { + return Err(RenderGraphRunnerError::MissingInput { + slot_index: i, + slot_name: input_slot.name.clone(), + sub_graph, + }); + } + } + + node_outputs.insert(input_node.label, input_values); + + for (_, node_state) in graph + .iter_node_outputs(input_node.label) + .expect("node exists") + { + node_queue.push_front(node_state); + } + } + + 'handle_node: while let Some(node_state) = node_queue.pop_back() { + // skip nodes that are already processed + if node_outputs.contains_key(&node_state.label) { + continue; + } + + let mut slot_indices_and_inputs: SmallVec<[(usize, SlotValue); 4]> = SmallVec::new(); + // check if all dependencies have finished running + for (edge, input_node) in graph + .iter_node_inputs(node_state.label) + .expect("node is in graph") + { + match edge { + Edge::SlotEdge { + output_index, + input_index, + .. + } => { + if let Some(outputs) = node_outputs.get(&input_node.label) { + slot_indices_and_inputs + .push((*input_index, outputs[*output_index].clone())); + } else { + node_queue.push_front(node_state); + continue 'handle_node; + } + } + Edge::NodeEdge { .. } => { + if !node_outputs.contains_key(&input_node.label) { + node_queue.push_front(node_state); + continue 'handle_node; + } + } + } + } + + // construct final sorted input list + slot_indices_and_inputs.sort_by_key(|(index, _)| *index); + let inputs: SmallVec<[SlotValue; 4]> = slot_indices_and_inputs + .into_iter() + .map(|(_, value)| value) + .collect(); + + if inputs.len() != node_state.input_slots.len() { + return Err(RenderGraphRunnerError::MismatchedInputCount { + node_name: node_state.label, + slot_count: node_state.input_slots.len(), + value_count: inputs.len(), + }); + } + + let mut outputs: SmallVec<[Option; 4]> = + smallvec![None; node_state.output_slots.len()]; + { + let mut context = RenderGraphContext::new(graph, node_state, &inputs, &mut outputs); + if let Some(view_entity) = view_entity { + context.set_view_entity(view_entity); + } + + { + #[cfg(feature = "trace")] + let _span = info_span!("node", name = node_state.type_name).entered(); + + node_state.node.run(&mut context, render_context, world)?; + } + + for run_sub_graph in context.finish() { + let sub_graph = graph + .get_sub_graph(run_sub_graph.sub_graph) + .expect("sub graph exists because it was validated when queued."); + Self::run_graph( + sub_graph, + Some(run_sub_graph.sub_graph), + render_context, + world, + &run_sub_graph.inputs, + run_sub_graph.view_entity, + run_sub_graph.debug_group, + )?; + } + } + + let mut values: SmallVec<[SlotValue; 4]> = SmallVec::new(); + for (i, output) in outputs.into_iter().enumerate() { + if let Some(value) = output { + values.push(value); + } else { + let empty_slot = node_state.output_slots.get_slot(i).unwrap(); + return Err(RenderGraphRunnerError::EmptyNodeOutputSlot { + type_name: node_state.type_name, + slot_index: i, + slot_name: empty_slot.name.clone(), + }); + } + } + node_outputs.insert(node_state.label, values); + + for (_, node_state) in graph + .iter_node_outputs(node_state.label) + .expect("node exists") + { + node_queue.push_front(node_state); + } + } + + if let Some(debug_group) = debug_group { + render_context + .command_encoder() + .insert_debug_marker(&format!("End {debug_group}")); + } + + Ok(()) + } +} diff --git a/third_party/bevy_render/src/renderer/mod.rs b/third_party/bevy_render/src/renderer/mod.rs new file mode 100644 index 0000000..e5ab90f --- /dev/null +++ b/third_party/bevy_render/src/renderer/mod.rs @@ -0,0 +1,689 @@ +mod graph_runner; +#[cfg(feature = "raw_vulkan_init")] +pub mod raw_vulkan_init; +mod render_device; +mod wgpu_wrapper; + +pub use graph_runner::*; +pub use render_device::*; +pub use wgpu_wrapper::WgpuWrapper; + +use crate::{ + diagnostic::{internal::DiagnosticsRecorder, RecordDiagnostics}, + render_graph::RenderGraph, + render_phase::TrackedRenderPass, + render_resource::RenderPassDescriptor, + settings::{RenderResources, WgpuSettings, WgpuSettingsPriority}, + view::{ExtractedWindows, ViewTarget}, +}; +use alloc::sync::Arc; +use bevy_camera::NormalizedRenderTarget; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{prelude::*, system::SystemState}; +use bevy_platform::time::Instant; +use bevy_render::camera::ExtractedCamera; +use bevy_time::TimeSender; +use bevy_window::RawHandleWrapperHolder; +use tracing::{debug, error, info, info_span, warn}; +use wgpu::{ + Adapter, AdapterInfo, Backends, CommandBuffer, CommandEncoder, DeviceType, Instance, Queue, + RequestAdapterOptions, Trace, +}; + +/// Updates the [`RenderGraph`] with all of its nodes and then runs it to render the entire frame. +pub fn render_system( + world: &mut World, + state: &mut SystemState>, +) { + world.resource_scope(|world, mut graph: Mut| { + graph.update(world); + }); + + let diagnostics_recorder = world.remove_resource::(); + + let graph = world.resource::(); + let render_device = world.resource::(); + let render_queue = world.resource::(); + + let res = RenderGraphRunner::run( + graph, + render_device.clone(), // TODO: is this clone really necessary? + diagnostics_recorder, + &render_queue.0, + world, + |encoder| { + crate::view::screenshot::submit_screenshot_commands(world, encoder); + crate::gpu_readback::submit_readback_commands(world, encoder); + }, + ); + + match res { + Ok(Some(diagnostics_recorder)) => { + world.insert_resource(diagnostics_recorder); + } + Ok(None) => {} + Err(e) => { + error!("Error running render graph:"); + { + let mut src: &dyn core::error::Error = &e; + loop { + error!("> {}", src); + match src.source() { + Some(s) => src = s, + None => break, + } + } + } + + panic!("Error running render graph: {e}"); + } + } + + { + let _span = info_span!("present_frames").entered(); + + world.resource_scope(|world, mut windows: Mut| { + let views = state.get(world); + for window in windows.values_mut() { + let view_needs_present = views.iter().any(|(view_target, camera)| { + matches!( + camera.target, + Some(NormalizedRenderTarget::Window(w)) if w.entity() == window.entity + ) && view_target.needs_present() + }); + + if view_needs_present || window.needs_initial_present { + window.present(); + window.needs_initial_present = false; + } + } + }); + + #[cfg(feature = "tracing-tracy")] + tracing::event!( + tracing::Level::INFO, + message = "finished frame", + tracy.frame_mark = true + ); + } + + crate::view::screenshot::collect_screenshots(world); + + // update the time and send it to the app world + let time_sender = world.resource::(); + if let Err(error) = time_sender.0.try_send(Instant::now()) { + match error { + bevy_time::TrySendError::Full(_) => { + panic!("The TimeSender channel should always be empty during render. You might need to add the bevy::core::time_system to your app."); + } + bevy_time::TrySendError::Disconnected(_) => { + // ignore disconnected errors, the main world probably just got dropped during shutdown + } + } + } +} + +/// This queue is used to enqueue tasks for the GPU to execute asynchronously. +#[derive(Resource, Clone, Deref, DerefMut)] +pub struct RenderQueue(pub Arc>); + +/// The handle to the physical device being used for rendering. +/// See [`Adapter`] for more info. +#[derive(Resource, Clone, Debug, Deref, DerefMut)] +pub struct RenderAdapter(pub Arc>); + +/// The GPU instance is used to initialize the [`RenderQueue`] and [`RenderDevice`], +/// as well as to create [`WindowSurfaces`](crate::view::window::WindowSurfaces). +#[derive(Resource, Clone, Deref, DerefMut)] +pub struct RenderInstance(pub Arc>); + +/// The [`AdapterInfo`] of the adapter in use by the renderer. +#[derive(Resource, Clone, Deref, DerefMut)] +pub struct RenderAdapterInfo(pub WgpuWrapper); + +const GPU_NOT_FOUND_ERROR_MESSAGE: &str = if cfg!(target_os = "linux") { + "Unable to find a GPU! Make sure you have installed required drivers! For extra information, see: https://github.com/bevyengine/bevy/blob/latest/docs/linux_dependencies.md" +} else { + "Unable to find a GPU! Make sure you have installed required drivers!" +}; + +#[cfg(not(target_family = "wasm"))] +fn find_adapter_by_name( + instance: &Instance, + options: &WgpuSettings, + compatible_surface: Option<&wgpu::Surface<'_>>, + adapter_name: &str, +) -> Option { + for adapter in + instance.enumerate_adapters(options.backends.expect( + "The `backends` field of `WgpuSettings` must be set to use a specific adapter.", + )) + { + tracing::trace!("Checking adapter: {:?}", adapter.get_info()); + let info = adapter.get_info(); + if let Some(surface) = compatible_surface + && !adapter.is_surface_supported(surface) + { + continue; + } + + if info + .name + .to_lowercase() + .contains(&adapter_name.to_lowercase()) + { + return Some(adapter); + } + } + None +} + +/// Initializes the renderer by retrieving and preparing the GPU instance, device and queue +/// for the specified backend. +pub async fn initialize_renderer( + backends: Backends, + primary_window: Option, + options: &WgpuSettings, + #[cfg(feature = "raw_vulkan_init")] + raw_vulkan_init_settings: raw_vulkan_init::RawVulkanInitSettings, +) -> RenderResources { + let instance_descriptor = wgpu::InstanceDescriptor { + backends, + flags: options.instance_flags, + memory_budget_thresholds: options.instance_memory_budget_thresholds, + backend_options: wgpu::BackendOptions { + gl: wgpu::GlBackendOptions { + gles_minor_version: options.gles3_minor_version, + fence_behavior: wgpu::GlFenceBehavior::Normal, + }, + dx12: wgpu::Dx12BackendOptions { + shader_compiler: options.dx12_shader_compiler.clone(), + presentation_system: wgpu::wgt::Dx12SwapchainKind::from_env().unwrap_or_default(), + latency_waitable_object: wgpu::wgt::Dx12UseFrameLatencyWaitableObject::from_env() + .unwrap_or_default(), + }, + noop: wgpu::NoopBackendOptions { enable: false }, + }, + }; + + #[cfg(not(feature = "raw_vulkan_init"))] + let instance = Instance::new(&instance_descriptor); + #[cfg(feature = "raw_vulkan_init")] + let mut additional_vulkan_features = raw_vulkan_init::AdditionalVulkanFeatures::default(); + #[cfg(feature = "raw_vulkan_init")] + let instance = raw_vulkan_init::create_raw_vulkan_instance( + &instance_descriptor, + &raw_vulkan_init_settings, + &mut additional_vulkan_features, + ); + + let surface = primary_window.and_then(|wrapper| { + let maybe_handle = wrapper + .0 + .lock() + .expect("Couldn't get the window handle in time for renderer initialization"); + if let Some(wrapper) = maybe_handle.as_ref() { + // SAFETY: Plugins should be set up on the main thread. + let handle = unsafe { wrapper.get_handle() }; + Some( + instance + .create_surface(handle) + .expect("Failed to create wgpu surface"), + ) + } else { + None + } + }); + + let force_fallback_adapter = std::env::var("WGPU_FORCE_FALLBACK_ADAPTER") + .map_or(options.force_fallback_adapter, |v| { + !(v.is_empty() || v == "0" || v == "false") + }); + + let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME") + .as_deref() + .map_or(options.adapter_name.clone(), |x| Some(x.to_lowercase())); + + let request_adapter_options = RequestAdapterOptions { + power_preference: options.power_preference, + compatible_surface: surface.as_ref(), + force_fallback_adapter, + }; + + #[cfg(not(target_family = "wasm"))] + let mut selected_adapter = desired_adapter_name.and_then(|adapter_name| { + find_adapter_by_name( + &instance, + options, + request_adapter_options.compatible_surface, + &adapter_name, + ) + }); + #[cfg(target_family = "wasm")] + let mut selected_adapter = None; + + #[cfg(target_family = "wasm")] + if desired_adapter_name.is_some() { + warn!("Choosing an adapter is not supported on wasm."); + } + + if selected_adapter.is_none() { + debug!( + "Searching for adapter with options: {:?}", + request_adapter_options + ); + selected_adapter = instance + .request_adapter(&request_adapter_options) + .await + .ok(); + } + + let adapter = selected_adapter.expect(GPU_NOT_FOUND_ERROR_MESSAGE); + let adapter_info = adapter.get_info(); + info!("{:?}", adapter_info); + + if adapter_info.device_type == DeviceType::Cpu { + warn!( + "The selected adapter is using a driver that only supports software rendering. \ + This is likely to be very slow. See https://bevy.org/learn/errors/b0006/" + ); + } + + // Maybe get features and limits based on what is supported by the adapter/backend + let mut features = wgpu::Features::empty(); + let mut limits = options.limits.clone(); + if matches!(options.priority, WgpuSettingsPriority::Functionality) { + features = adapter.features(); + if adapter_info.device_type == DeviceType::DiscreteGpu { + // `MAPPABLE_PRIMARY_BUFFERS` can have a significant, negative performance impact for + // discrete GPUs due to having to transfer data across the PCI-E bus and so it + // should not be automatically enabled in this case. It is however beneficial for + // integrated GPUs. + features.remove(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS); + } + + limits = adapter.limits(); + } + + // Enforce the disabled features + if let Some(disabled_features) = options.disabled_features { + features.remove(disabled_features); + } + // NOTE: |= is used here to ensure that any explicitly-enabled features are respected. + features |= options.features; + + // Enforce the limit constraints + if let Some(constrained_limits) = options.constrained_limits.as_ref() { + // NOTE: Respect the configured limits as an 'upper bound'. This means for 'max' limits, we + // take the minimum of the calculated limits according to the adapter/backend and the + // specified max_limits. For 'min' limits, take the maximum instead. This is intended to + // err on the side of being conservative. We can't claim 'higher' limits that are supported + // but we can constrain to 'lower' limits. + limits = wgpu::Limits { + max_texture_dimension_1d: limits + .max_texture_dimension_1d + .min(constrained_limits.max_texture_dimension_1d), + max_texture_dimension_2d: limits + .max_texture_dimension_2d + .min(constrained_limits.max_texture_dimension_2d), + max_texture_dimension_3d: limits + .max_texture_dimension_3d + .min(constrained_limits.max_texture_dimension_3d), + max_texture_array_layers: limits + .max_texture_array_layers + .min(constrained_limits.max_texture_array_layers), + max_bind_groups: limits + .max_bind_groups + .min(constrained_limits.max_bind_groups), + max_dynamic_uniform_buffers_per_pipeline_layout: limits + .max_dynamic_uniform_buffers_per_pipeline_layout + .min(constrained_limits.max_dynamic_uniform_buffers_per_pipeline_layout), + max_dynamic_storage_buffers_per_pipeline_layout: limits + .max_dynamic_storage_buffers_per_pipeline_layout + .min(constrained_limits.max_dynamic_storage_buffers_per_pipeline_layout), + max_sampled_textures_per_shader_stage: limits + .max_sampled_textures_per_shader_stage + .min(constrained_limits.max_sampled_textures_per_shader_stage), + max_samplers_per_shader_stage: limits + .max_samplers_per_shader_stage + .min(constrained_limits.max_samplers_per_shader_stage), + max_storage_buffers_per_shader_stage: limits + .max_storage_buffers_per_shader_stage + .min(constrained_limits.max_storage_buffers_per_shader_stage), + max_storage_textures_per_shader_stage: limits + .max_storage_textures_per_shader_stage + .min(constrained_limits.max_storage_textures_per_shader_stage), + max_uniform_buffers_per_shader_stage: limits + .max_uniform_buffers_per_shader_stage + .min(constrained_limits.max_uniform_buffers_per_shader_stage), + max_binding_array_elements_per_shader_stage: limits + .max_binding_array_elements_per_shader_stage + .min(constrained_limits.max_binding_array_elements_per_shader_stage), + max_binding_array_sampler_elements_per_shader_stage: limits + .max_binding_array_sampler_elements_per_shader_stage + .min(constrained_limits.max_binding_array_sampler_elements_per_shader_stage), + max_uniform_buffer_binding_size: limits + .max_uniform_buffer_binding_size + .min(constrained_limits.max_uniform_buffer_binding_size), + max_storage_buffer_binding_size: limits + .max_storage_buffer_binding_size + .min(constrained_limits.max_storage_buffer_binding_size), + max_vertex_buffers: limits + .max_vertex_buffers + .min(constrained_limits.max_vertex_buffers), + max_vertex_attributes: limits + .max_vertex_attributes + .min(constrained_limits.max_vertex_attributes), + max_vertex_buffer_array_stride: limits + .max_vertex_buffer_array_stride + .min(constrained_limits.max_vertex_buffer_array_stride), + max_push_constant_size: limits + .max_push_constant_size + .min(constrained_limits.max_push_constant_size), + min_uniform_buffer_offset_alignment: limits + .min_uniform_buffer_offset_alignment + .max(constrained_limits.min_uniform_buffer_offset_alignment), + min_storage_buffer_offset_alignment: limits + .min_storage_buffer_offset_alignment + .max(constrained_limits.min_storage_buffer_offset_alignment), + max_inter_stage_shader_components: limits + .max_inter_stage_shader_components + .min(constrained_limits.max_inter_stage_shader_components), + max_compute_workgroup_storage_size: limits + .max_compute_workgroup_storage_size + .min(constrained_limits.max_compute_workgroup_storage_size), + max_compute_invocations_per_workgroup: limits + .max_compute_invocations_per_workgroup + .min(constrained_limits.max_compute_invocations_per_workgroup), + max_compute_workgroup_size_x: limits + .max_compute_workgroup_size_x + .min(constrained_limits.max_compute_workgroup_size_x), + max_compute_workgroup_size_y: limits + .max_compute_workgroup_size_y + .min(constrained_limits.max_compute_workgroup_size_y), + max_compute_workgroup_size_z: limits + .max_compute_workgroup_size_z + .min(constrained_limits.max_compute_workgroup_size_z), + max_compute_workgroups_per_dimension: limits + .max_compute_workgroups_per_dimension + .min(constrained_limits.max_compute_workgroups_per_dimension), + max_buffer_size: limits + .max_buffer_size + .min(constrained_limits.max_buffer_size), + max_bindings_per_bind_group: limits + .max_bindings_per_bind_group + .min(constrained_limits.max_bindings_per_bind_group), + max_non_sampler_bindings: limits + .max_non_sampler_bindings + .min(constrained_limits.max_non_sampler_bindings), + max_blas_primitive_count: limits + .max_blas_primitive_count + .min(constrained_limits.max_blas_primitive_count), + max_blas_geometry_count: limits + .max_blas_geometry_count + .min(constrained_limits.max_blas_geometry_count), + max_tlas_instance_count: limits + .max_tlas_instance_count + .min(constrained_limits.max_tlas_instance_count), + max_color_attachments: limits + .max_color_attachments + .min(constrained_limits.max_color_attachments), + max_color_attachment_bytes_per_sample: limits + .max_color_attachment_bytes_per_sample + .min(constrained_limits.max_color_attachment_bytes_per_sample), + min_subgroup_size: limits + .min_subgroup_size + .max(constrained_limits.min_subgroup_size), + max_subgroup_size: limits + .max_subgroup_size + .min(constrained_limits.max_subgroup_size), + max_acceleration_structures_per_shader_stage: limits + .max_acceleration_structures_per_shader_stage + .min(constrained_limits.max_acceleration_structures_per_shader_stage), + max_task_workgroup_total_count: limits + .max_task_workgroup_total_count + .min(constrained_limits.max_task_workgroup_total_count), + max_task_workgroups_per_dimension: limits + .max_task_workgroups_per_dimension + .min(constrained_limits.max_task_workgroups_per_dimension), + max_mesh_output_layers: limits + .max_mesh_output_layers + .min(constrained_limits.max_mesh_output_layers), + max_mesh_multiview_count: limits + .max_mesh_multiview_count + .min(constrained_limits.max_mesh_multiview_count), + }; + } + + let device_descriptor = wgpu::DeviceDescriptor { + label: options.device_label.as_ref().map(AsRef::as_ref), + required_features: features, + required_limits: limits, + // SAFETY: TODO, see https://github.com/bevyengine/bevy/issues/22082 + experimental_features: unsafe { wgpu::ExperimentalFeatures::enabled() }, + memory_hints: options.memory_hints.clone(), + // See https://github.com/gfx-rs/wgpu/issues/5974 + trace: Trace::Off, + }; + + #[cfg(not(feature = "raw_vulkan_init"))] + let (device, queue) = adapter.request_device(&device_descriptor).await.unwrap(); + + #[cfg(feature = "raw_vulkan_init")] + let (device, queue) = raw_vulkan_init::create_raw_device( + &adapter, + &device_descriptor, + &raw_vulkan_init_settings, + &mut additional_vulkan_features, + ) + .await + .unwrap(); + + debug!("Configured wgpu adapter Limits: {:#?}", device.limits()); + debug!("Configured wgpu adapter Features: {:#?}", device.features()); + + RenderResources( + RenderDevice::from(device), + RenderQueue(Arc::new(WgpuWrapper::new(queue))), + RenderAdapterInfo(WgpuWrapper::new(adapter_info)), + RenderAdapter(Arc::new(WgpuWrapper::new(adapter))), + RenderInstance(Arc::new(WgpuWrapper::new(instance))), + #[cfg(feature = "raw_vulkan_init")] + additional_vulkan_features, + ) +} + +/// The context with all information required to interact with the GPU. +/// +/// The [`RenderDevice`] is used to create render resources and the +/// [`CommandEncoder`] is used to record a series of GPU operations. +pub struct RenderContext<'w> { + render_device: RenderDevice, + command_encoder: Option, + command_buffer_queue: Vec>, + diagnostics_recorder: Option>, +} + +impl<'w> RenderContext<'w> { + /// Creates a new [`RenderContext`] from a [`RenderDevice`]. + pub fn new( + render_device: RenderDevice, + diagnostics_recorder: Option, + ) -> Self { + Self { + render_device, + command_encoder: None, + command_buffer_queue: Vec::new(), + diagnostics_recorder: diagnostics_recorder.map(Arc::new), + } + } + + /// Gets the underlying [`RenderDevice`]. + pub fn render_device(&self) -> &RenderDevice { + &self.render_device + } + + /// Gets the diagnostics recorder, used to track elapsed time and pipeline statistics + /// of various render and compute passes. + pub fn diagnostic_recorder(&self) -> impl RecordDiagnostics + use<> { + self.diagnostics_recorder.clone() + } + + /// Gets the current [`CommandEncoder`]. + pub fn command_encoder(&mut self) -> &mut CommandEncoder { + self.command_encoder.get_or_insert_with(|| { + self.render_device + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()) + }) + } + + pub(crate) fn has_commands(&mut self) -> bool { + self.command_encoder.is_some() || !self.command_buffer_queue.is_empty() + } + + /// Creates a new [`TrackedRenderPass`] for the context, + /// configured using the provided `descriptor`. + pub fn begin_tracked_render_pass<'a>( + &'a mut self, + descriptor: RenderPassDescriptor<'_>, + ) -> TrackedRenderPass<'a> { + // Cannot use command_encoder() as we need to split the borrow on self + let command_encoder = self.command_encoder.get_or_insert_with(|| { + self.render_device + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()) + }); + + let render_pass = command_encoder.begin_render_pass(&descriptor); + TrackedRenderPass::new(&self.render_device, render_pass) + } + + /// Append a [`CommandBuffer`] to the command buffer queue. + /// + /// If present, this will flush the currently unflushed [`CommandEncoder`] + /// into a [`CommandBuffer`] into the queue before appending the provided + /// buffer. + pub fn add_command_buffer(&mut self, command_buffer: CommandBuffer) { + self.flush_encoder(); + + self.command_buffer_queue + .push(QueuedCommandBuffer::Ready(command_buffer)); + } + + /// Append a function that will generate a [`CommandBuffer`] to the + /// command buffer queue, to be ran later. + /// + /// If present, this will flush the currently unflushed [`CommandEncoder`] + /// into a [`CommandBuffer`] into the queue before appending the provided + /// buffer. + pub fn add_command_buffer_generation_task( + &mut self, + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] + task: impl FnOnce(RenderDevice) -> CommandBuffer + 'w + Send, + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] + task: impl FnOnce(RenderDevice) -> CommandBuffer + 'w, + ) { + self.flush_encoder(); + + self.command_buffer_queue + .push(QueuedCommandBuffer::Task(Box::new(task))); + } + + /// Finalizes and returns the queue of [`CommandBuffer`]s. + /// + /// This function will wait until all command buffer generation tasks are complete + /// by running them in parallel (where supported). + /// + /// The [`CommandBuffer`]s will be returned in the order that they were added. + pub fn finish( + mut self, + ) -> ( + Vec, + RenderDevice, + Option, + ) { + self.flush_encoder(); + + let mut command_buffers = Vec::with_capacity(self.command_buffer_queue.len()); + + #[cfg(feature = "trace")] + let _command_buffer_generation_tasks_span = + info_span!("command_buffer_generation_tasks").entered(); + + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] + { + let mut task_based_command_buffers = + bevy_tasks::ComputeTaskPool::get().scope(|task_pool| { + for (i, queued_command_buffer) in + self.command_buffer_queue.into_iter().enumerate() + { + match queued_command_buffer { + QueuedCommandBuffer::Ready(command_buffer) => { + command_buffers.push((i, command_buffer)); + } + QueuedCommandBuffer::Task(command_buffer_generation_task) => { + let render_device = self.render_device.clone(); + task_pool.spawn(async move { + (i, command_buffer_generation_task(render_device)) + }); + } + } + } + }); + command_buffers.append(&mut task_based_command_buffers); + } + + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] + for (i, queued_command_buffer) in self.command_buffer_queue.into_iter().enumerate() { + match queued_command_buffer { + QueuedCommandBuffer::Ready(command_buffer) => { + command_buffers.push((i, command_buffer)); + } + QueuedCommandBuffer::Task(command_buffer_generation_task) => { + let render_device = self.render_device.clone(); + command_buffers.push((i, command_buffer_generation_task(render_device))); + } + } + } + + #[cfg(feature = "trace")] + drop(_command_buffer_generation_tasks_span); + + command_buffers.sort_unstable_by_key(|(i, _)| *i); + + let mut command_buffers = command_buffers + .into_iter() + .map(|(_, cb)| cb) + .collect::>(); + + let mut diagnostics_recorder = self.diagnostics_recorder.take().map(|v| { + Arc::try_unwrap(v) + .ok() + .expect("diagnostic recorder shouldn't be held longer than necessary") + }); + + if let Some(recorder) = &mut diagnostics_recorder { + let mut command_encoder = self + .render_device + .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); + recorder.resolve(&mut command_encoder); + command_buffers.push(command_encoder.finish()); + } + + (command_buffers, self.render_device, diagnostics_recorder) + } + + fn flush_encoder(&mut self) { + if let Some(encoder) = self.command_encoder.take() { + self.command_buffer_queue + .push(QueuedCommandBuffer::Ready(encoder.finish())); + } + } +} + +enum QueuedCommandBuffer<'w> { + Ready(CommandBuffer), + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] + Task(Box CommandBuffer + 'w + Send>), + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] + Task(Box CommandBuffer + 'w>), +} diff --git a/third_party/bevy_render/src/renderer/raw_vulkan_init.rs b/third_party/bevy_render/src/renderer/raw_vulkan_init.rs new file mode 100644 index 0000000..9730569 --- /dev/null +++ b/third_party/bevy_render/src/renderer/raw_vulkan_init.rs @@ -0,0 +1,148 @@ +use alloc::sync::Arc; +use bevy_ecs::resource::Resource; +use bevy_platform::collections::HashSet; +use core::any::{Any, TypeId}; +use thiserror::Error; +use wgpu::{ + hal::api::Vulkan, Adapter, Device, DeviceDescriptor, Instance, InstanceDescriptor, Queue, +}; + +/// When the `raw_vulkan_init` feature is enabled, these settings will be used to configure the raw vulkan instance. +#[derive(Resource, Default, Clone)] +pub struct RawVulkanInitSettings { + // SAFETY: this must remain private to ensure that registering callbacks is unsafe + create_instance_callbacks: Vec< + Arc< + dyn Fn( + &mut wgpu::hal::vulkan::CreateInstanceCallbackArgs, + &mut AdditionalVulkanFeatures, + ) + Send + + Sync, + >, + >, + // SAFETY: this must remain private to ensure that registering callbacks is unsafe + create_device_callbacks: Vec< + Arc< + dyn Fn( + &mut wgpu::hal::vulkan::CreateDeviceCallbackArgs, + &wgpu::hal::vulkan::Adapter, + &mut AdditionalVulkanFeatures, + ) + Send + + Sync, + >, + >, +} + +impl RawVulkanInitSettings { + /// Adds a new Vulkan create instance callback. See [`wgpu::hal::vulkan::Instance::init_with_callback`] for details. + /// + /// # Safety + /// - Callback must not remove features. + /// - Callback must not change anything to what the instance does not support. + pub unsafe fn add_create_instance_callback( + &mut self, + callback: impl Fn(&mut wgpu::hal::vulkan::CreateInstanceCallbackArgs, &mut AdditionalVulkanFeatures) + + Send + + Sync + + 'static, + ) { + self.create_instance_callbacks.push(Arc::new(callback)); + } + + /// Adds a new Vulkan create device callback. See [`wgpu::hal::vulkan::Adapter::open_with_callback`] for details. + /// + /// # Safety + /// - Callback must not remove features. + /// - Callback must not change anything to what the device does not support. + pub unsafe fn add_create_device_callback( + &mut self, + callback: impl Fn( + &mut wgpu::hal::vulkan::CreateDeviceCallbackArgs, + &wgpu::hal::vulkan::Adapter, + &mut AdditionalVulkanFeatures, + ) + Send + + Sync + + 'static, + ) { + self.create_device_callbacks.push(Arc::new(callback)); + } +} + +pub(crate) fn create_raw_vulkan_instance( + instance_descriptor: &InstanceDescriptor, + settings: &RawVulkanInitSettings, + additional_features: &mut AdditionalVulkanFeatures, +) -> Instance { + // SAFETY: Registering callbacks is unsafe. Callback authors promise not to remove features + // or change the instance to something it does not support + unsafe { + wgpu::hal::vulkan::Instance::init_with_callback( + &wgpu::hal::InstanceDescriptor { + name: "wgpu", + flags: instance_descriptor.flags, + memory_budget_thresholds: instance_descriptor.memory_budget_thresholds, + backend_options: instance_descriptor.backend_options.clone(), + }, + Some(Box::new(|mut args| { + for callback in &settings.create_instance_callbacks { + (callback)(&mut args, additional_features); + } + })), + ) + .map(|raw_instance| Instance::from_hal::(raw_instance)) + .unwrap_or_else(|_| Instance::new(instance_descriptor)) + } +} + +pub(crate) async fn create_raw_device( + adapter: &Adapter, + device_descriptor: &DeviceDescriptor<'_>, + settings: &RawVulkanInitSettings, + additional_features: &mut AdditionalVulkanFeatures, +) -> Result<(Device, Queue), CreateRawVulkanDeviceError> { + // SAFETY: Registering callbacks is unsafe. Callback authors promise not to remove features + // or change the adapter to something it does not support + unsafe { + let Some(raw_adapter) = adapter.as_hal::() else { + return Ok(adapter.request_device(device_descriptor).await?); + }; + let open_device = raw_adapter.open_with_callback( + device_descriptor.required_features, + &device_descriptor.memory_hints, + Some(Box::new(|mut args| { + for callback in &settings.create_device_callbacks { + (callback)(&mut args, &raw_adapter, additional_features); + } + })), + )?; + + Ok(adapter.create_device_from_hal::(open_device, device_descriptor)?) + } +} + +#[derive(Error, Debug)] +pub(crate) enum CreateRawVulkanDeviceError { + #[error(transparent)] + RequestDeviceError(#[from] wgpu::RequestDeviceError), + #[error(transparent)] + DeviceError(#[from] wgpu::hal::DeviceError), +} + +/// A list of additional Vulkan features that are supported by the current wgpu instance / adapter. This is populated +/// by callbacks defined in [`RawVulkanInitSettings`] +#[derive(Resource, Default, Clone)] +pub struct AdditionalVulkanFeatures(HashSet); + +impl AdditionalVulkanFeatures { + pub fn insert(&mut self) { + self.0.insert(TypeId::of::()); + } + + pub fn has(&self) -> bool { + self.0.contains(&TypeId::of::()) + } + + pub fn remove(&mut self) { + self.0.remove(&TypeId::of::()); + } +} diff --git a/third_party/bevy_render/src/renderer/render_device.rs b/third_party/bevy_render/src/renderer/render_device.rs new file mode 100644 index 0000000..f0334a9 --- /dev/null +++ b/third_party/bevy_render/src/renderer/render_device.rs @@ -0,0 +1,310 @@ +use super::RenderQueue; +use crate::render_resource::{ + BindGroup, BindGroupLayout, Buffer, ComputePipeline, RawRenderPipelineDescriptor, + RenderPipeline, Sampler, Texture, +}; +use crate::renderer::WgpuWrapper; +use bevy_ecs::resource::Resource; +use wgpu::{ + util::DeviceExt, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, + BindGroupLayoutEntry, BufferAsyncError, BufferBindingType, PollError, PollStatus, +}; + +/// This GPU device is responsible for the creation of most rendering and compute resources. +#[derive(Resource, Clone)] +pub struct RenderDevice { + device: WgpuWrapper, +} + +impl From for RenderDevice { + fn from(device: wgpu::Device) -> Self { + Self::new(WgpuWrapper::new(device)) + } +} + +impl RenderDevice { + pub fn new(device: WgpuWrapper) -> Self { + Self { device } + } + + /// List all [`Features`](wgpu::Features) that may be used with this device. + /// + /// Functions may panic if you use unsupported features. + #[inline] + pub fn features(&self) -> wgpu::Features { + self.device.features() + } + + /// List all [`Limits`](wgpu::Limits) that were requested of this device. + /// + /// If any of these limits are exceeded, functions may panic. + #[inline] + pub fn limits(&self) -> wgpu::Limits { + self.device.limits() + } + + /// Creates a [`ShaderModule`](wgpu::ShaderModule) from either SPIR-V or WGSL source code. + /// + /// # Safety + /// + /// Creates a shader module with user-customizable runtime checks which allows shaders to + /// perform operations which can lead to undefined behavior like indexing out of bounds, + /// To avoid UB, ensure any unchecked shaders are sound! + /// This method should never be called for user-supplied shaders. + #[inline] + pub unsafe fn create_shader_module( + &self, + desc: wgpu::ShaderModuleDescriptor, + ) -> wgpu::ShaderModule { + #[cfg(feature = "spirv_shader_passthrough")] + match &desc.source { + wgpu::ShaderSource::SpirV(source) + if self + .features() + .contains(wgpu::Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) => + { + // SAFETY: + // This call passes binary data to the backend as-is and can potentially result in a driver crash or bogus behavior. + // No attempt is made to ensure that data is valid SPIR-V. + unsafe { + self.device.create_shader_module_passthrough( + wgpu::ShaderModuleDescriptorPassthrough { + label: desc.label, + spirv: Some(source.clone()), + ..Default::default() + }, + ) + } + } + // SAFETY: + // + // This call passes binary data to the backend as-is and can potentially result in a driver crash or bogus behavior. + // No attempt is made to ensure that data is valid SPIR-V. + _ => unsafe { + self.device + .create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked()) + }, + } + #[cfg(not(feature = "spirv_shader_passthrough"))] + // SAFETY: the caller is responsible for upholding the safety requirements + unsafe { + self.device + .create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked()) + } + } + + /// Creates and validates a [`ShaderModule`](wgpu::ShaderModule) from either SPIR-V or WGSL source code. + /// + /// See [`ValidateShader`](bevy_shader::ValidateShader) for more information on the tradeoffs involved with shader validation. + #[inline] + pub fn create_and_validate_shader_module( + &self, + desc: wgpu::ShaderModuleDescriptor, + ) -> wgpu::ShaderModule { + #[cfg(feature = "spirv_shader_passthrough")] + match &desc.source { + wgpu::ShaderSource::SpirV(_source) => panic!("no safety checks are performed for spirv shaders. use `create_shader_module` instead"), + _ => self.device.create_shader_module(desc), + } + #[cfg(not(feature = "spirv_shader_passthrough"))] + self.device.create_shader_module(desc) + } + + /// Check for resource cleanups and mapping callbacks. + /// + /// Return `true` if the queue is empty, or `false` if there are more queue + /// submissions still in flight. (Note that, unless access to the [`wgpu::Queue`] is + /// coordinated somehow, this information could be out of date by the time + /// the caller receives it. `Queue`s can be shared between threads, so + /// other threads could submit new work at any time.) + /// + /// no-op on the web, device is automatically polled. + #[inline] + pub fn poll(&self, maintain: wgpu::PollType) -> Result { + self.device.poll(maintain) + } + + /// Creates an empty [`CommandEncoder`](wgpu::CommandEncoder). + #[inline] + pub fn create_command_encoder( + &self, + desc: &wgpu::CommandEncoderDescriptor, + ) -> wgpu::CommandEncoder { + self.device.create_command_encoder(desc) + } + + /// Creates an empty [`RenderBundleEncoder`](wgpu::RenderBundleEncoder). + #[inline] + pub fn create_render_bundle_encoder( + &self, + desc: &wgpu::RenderBundleEncoderDescriptor, + ) -> wgpu::RenderBundleEncoder<'_> { + self.device.create_render_bundle_encoder(desc) + } + + /// Creates a new [`BindGroup`](wgpu::BindGroup). + #[inline] + pub fn create_bind_group<'a>( + &self, + label: impl Into>, + layout: &'a BindGroupLayout, + entries: &'a [BindGroupEntry<'a>], + ) -> BindGroup { + let wgpu_bind_group = self.device.create_bind_group(&BindGroupDescriptor { + label: label.into(), + layout, + entries, + }); + BindGroup::from(wgpu_bind_group) + } + + /// Creates a [`BindGroupLayout`](wgpu::BindGroupLayout). + #[inline] + pub fn create_bind_group_layout<'a>( + &self, + label: impl Into>, + entries: &'a [BindGroupLayoutEntry], + ) -> BindGroupLayout { + BindGroupLayout::from( + self.device + .create_bind_group_layout(&BindGroupLayoutDescriptor { + label: label.into(), + entries, + }), + ) + } + + /// Creates a [`PipelineLayout`](wgpu::PipelineLayout). + #[inline] + pub fn create_pipeline_layout( + &self, + desc: &wgpu::PipelineLayoutDescriptor, + ) -> wgpu::PipelineLayout { + self.device.create_pipeline_layout(desc) + } + + /// Creates a [`RenderPipeline`]. + #[inline] + pub fn create_render_pipeline(&self, desc: &RawRenderPipelineDescriptor) -> RenderPipeline { + let wgpu_render_pipeline = self.device.create_render_pipeline(desc); + RenderPipeline::from(wgpu_render_pipeline) + } + + /// Creates a [`ComputePipeline`]. + #[inline] + pub fn create_compute_pipeline( + &self, + desc: &wgpu::ComputePipelineDescriptor, + ) -> ComputePipeline { + let wgpu_compute_pipeline = self.device.create_compute_pipeline(desc); + ComputePipeline::from(wgpu_compute_pipeline) + } + + /// Creates a [`Buffer`]. + pub fn create_buffer(&self, desc: &wgpu::BufferDescriptor) -> Buffer { + let wgpu_buffer = self.device.create_buffer(desc); + Buffer::from(wgpu_buffer) + } + + /// Creates a [`Buffer`] and initializes it with the specified data. + pub fn create_buffer_with_data(&self, desc: &wgpu::util::BufferInitDescriptor) -> Buffer { + let wgpu_buffer = self.device.create_buffer_init(desc); + Buffer::from(wgpu_buffer) + } + + /// Creates a new [`Texture`] and initializes it with the specified data. + /// + /// `desc` specifies the general format of the texture. + /// `data` is the raw data. + pub fn create_texture_with_data( + &self, + render_queue: &RenderQueue, + desc: &wgpu::TextureDescriptor, + order: wgpu::util::TextureDataOrder, + data: &[u8], + ) -> Texture { + let wgpu_texture = + self.device + .create_texture_with_data(render_queue.as_ref(), desc, order, data); + Texture::from(wgpu_texture) + } + + /// Creates a new [`Texture`]. + /// + /// `desc` specifies the general format of the texture. + pub fn create_texture(&self, desc: &wgpu::TextureDescriptor) -> Texture { + let wgpu_texture = self.device.create_texture(desc); + Texture::from(wgpu_texture) + } + + /// Creates a new [`Sampler`]. + /// + /// `desc` specifies the behavior of the sampler. + pub fn create_sampler(&self, desc: &wgpu::SamplerDescriptor) -> Sampler { + let wgpu_sampler = self.device.create_sampler(desc); + Sampler::from(wgpu_sampler) + } + + /// Initializes [`Surface`](wgpu::Surface) for presentation. + /// + /// # Panics + /// + /// - A old [`SurfaceTexture`](wgpu::SurfaceTexture) is still alive referencing an old surface. + /// - Texture format requested is unsupported on the surface. + pub fn configure_surface(&self, surface: &wgpu::Surface, config: &wgpu::SurfaceConfiguration) { + surface.configure(&self.device, config); + } + + /// Returns the wgpu [`Device`](wgpu::Device). + pub fn wgpu_device(&self) -> &wgpu::Device { + &self.device + } + + pub fn map_buffer( + &self, + buffer: &wgpu::BufferSlice, + map_mode: wgpu::MapMode, + callback: impl FnOnce(Result<(), BufferAsyncError>) + Send + 'static, + ) { + buffer.map_async(map_mode, callback); + } + + // Rounds up `row_bytes` to be a multiple of [`wgpu::COPY_BYTES_PER_ROW_ALIGNMENT`]. + pub const fn align_copy_bytes_per_row(row_bytes: usize) -> usize { + let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize; + + // If row_bytes is aligned calculate a value just under the next aligned value. + // Otherwise calculate a value greater than the next aligned value. + let over_aligned = row_bytes + align - 1; + + // Round the number *down* to the nearest aligned value. + (over_aligned / align) * align + } + + pub fn get_supported_read_only_binding_type( + &self, + buffers_per_shader_stage: u32, + ) -> BufferBindingType { + if self.limits().max_storage_buffers_per_shader_stage >= buffers_per_shader_stage { + BufferBindingType::Storage { read_only: true } + } else { + BufferBindingType::Uniform + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn align_copy_bytes_per_row() { + // Test for https://github.com/bevyengine/bevy/issues/16992 + let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize; + + assert_eq!(RenderDevice::align_copy_bytes_per_row(0), 0); + assert_eq!(RenderDevice::align_copy_bytes_per_row(1), align); + assert_eq!(RenderDevice::align_copy_bytes_per_row(align + 1), align * 2); + assert_eq!(RenderDevice::align_copy_bytes_per_row(align), align); + } +} diff --git a/third_party/bevy_render/src/renderer/wgpu_wrapper.rs b/third_party/bevy_render/src/renderer/wgpu_wrapper.rs new file mode 100644 index 0000000..272d0dd --- /dev/null +++ b/third_party/bevy_render/src/renderer/wgpu_wrapper.rs @@ -0,0 +1,50 @@ +/// A wrapper to safely make `wgpu` types Send / Sync on web with atomics enabled. +/// +/// On web with `atomics` enabled the inner value can only be accessed +/// or dropped on the `wgpu` thread or else a panic will occur. +/// On other platforms the wrapper simply contains the wrapped value. +#[derive(Debug, Clone)] +pub struct WgpuWrapper( + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] T, + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] send_wrapper::SendWrapper, +); + +// SAFETY: SendWrapper is always Send + Sync. +#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] +#[expect(unsafe_code, reason = "Blanket-impl Send requires unsafe.")] +unsafe impl Send for WgpuWrapper {} +#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] +#[expect(unsafe_code, reason = "Blanket-impl Sync requires unsafe.")] +unsafe impl Sync for WgpuWrapper {} + +impl WgpuWrapper { + /// Constructs a new instance of `WgpuWrapper` which will wrap the specified value. + pub fn new(t: T) -> Self { + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] + return Self(t); + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] + return Self(send_wrapper::SendWrapper::new(t)); + } + + /// Unwraps the value. + pub fn into_inner(self) -> T { + #[cfg(not(all(target_arch = "wasm32", target_feature = "atomics")))] + return self.0; + #[cfg(all(target_arch = "wasm32", target_feature = "atomics"))] + return self.0.take(); + } +} + +impl core::ops::Deref for WgpuWrapper { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl core::ops::DerefMut for WgpuWrapper { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/third_party/bevy_render/src/settings.rs b/third_party/bevy_render/src/settings.rs new file mode 100644 index 0000000..c3b284e --- /dev/null +++ b/third_party/bevy_render/src/settings.rs @@ -0,0 +1,226 @@ +use crate::renderer::{ + RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue, +}; +use alloc::borrow::Cow; + +pub use wgpu::{ + Backends, Dx12Compiler, Features as WgpuFeatures, Gles3MinorVersion, InstanceFlags, + Limits as WgpuLimits, MemoryHints, PowerPreference, +}; +use wgpu::{DxcShaderModel, MemoryBudgetThresholds}; + +/// Configures the priority used when automatically configuring the features/limits of `wgpu`. +#[derive(Clone)] +pub enum WgpuSettingsPriority { + /// WebGPU default features and limits + Compatibility, + /// The maximum supported features and limits of the adapter and backend + Functionality, + /// WebGPU default limits plus additional constraints in order to be compatible with WebGL2 + WebGL2, +} + +/// Provides configuration for renderer initialization. Use [`RenderDevice::features`](RenderDevice::features), +/// [`RenderDevice::limits`](RenderDevice::limits), and the [`RenderAdapterInfo`] +/// resource to get runtime information about the actual adapter, backend, features, and limits. +/// NOTE: [`Backends::DX12`](Backends::DX12), [`Backends::METAL`](Backends::METAL), and +/// [`Backends::VULKAN`](Backends::VULKAN) are enabled by default for non-web and the best choice +/// is automatically selected. Web using the `webgl` feature uses [`Backends::GL`](Backends::GL). +/// NOTE: If you want to use [`Backends::GL`](Backends::GL) in a native app on `Windows` and/or `macOS`, you must +/// use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle) and enable the `gles` feature. This is +/// because wgpu requires EGL to create a GL context without a window and only ANGLE supports that. +#[derive(Clone)] +pub struct WgpuSettings { + pub device_label: Option>, + pub backends: Option, + pub power_preference: PowerPreference, + pub priority: WgpuSettingsPriority, + /// The features to ensure are enabled regardless of what the adapter/backend supports. + /// Setting these explicitly may cause renderer initialization to fail. + pub features: WgpuFeatures, + /// The features to ensure are disabled regardless of what the adapter/backend supports + pub disabled_features: Option, + /// The imposed limits. + pub limits: WgpuLimits, + /// The constraints on limits allowed regardless of what the adapter/backend supports + pub constrained_limits: Option, + /// The shader compiler to use for the DX12 backend. + pub dx12_shader_compiler: Dx12Compiler, + /// Allows you to choose which minor version of GLES3 to use (3.0, 3.1, 3.2, or automatic) + /// This only applies when using ANGLE and the GL backend. + pub gles3_minor_version: Gles3MinorVersion, + /// These are for controlling WGPU's debug information to eg. enable validation and shader debug info in release builds. + pub instance_flags: InstanceFlags, + /// This hints to the WGPU device about the preferred memory allocation strategy. + pub memory_hints: MemoryHints, + /// The thresholds for device memory budget. + pub instance_memory_budget_thresholds: MemoryBudgetThresholds, + /// If true, will force wgpu to use a software renderer, if available. + pub force_fallback_adapter: bool, + /// The name of the adapter to use. + pub adapter_name: Option, +} + +impl Default for WgpuSettings { + fn default() -> Self { + let default_backends = if cfg!(all( + feature = "webgl", + target_arch = "wasm32", + not(feature = "webgpu") + )) { + Backends::GL + } else if cfg!(all(feature = "webgpu", target_arch = "wasm32")) { + Backends::BROWSER_WEBGPU + } else { + Backends::all() + }; + + let backends = Some(Backends::from_env().unwrap_or(default_backends)); + + let power_preference = + PowerPreference::from_env().unwrap_or(PowerPreference::HighPerformance); + + let priority = settings_priority_from_env().unwrap_or(WgpuSettingsPriority::Functionality); + + let limits = if cfg!(all( + feature = "webgl", + target_arch = "wasm32", + not(feature = "webgpu") + )) || matches!(priority, WgpuSettingsPriority::WebGL2) + { + wgpu::Limits::downlevel_webgl2_defaults() + } else { + #[expect(clippy::allow_attributes, reason = "`unused_mut` is not always linted")] + #[allow( + unused_mut, + reason = "This variable needs to be mutable if the `ci_limits` feature is enabled" + )] + let mut limits = wgpu::Limits::default(); + #[cfg(feature = "ci_limits")] + { + limits.max_storage_textures_per_shader_stage = 4; + limits.max_texture_dimension_3d = 1024; + } + limits + }; + + let dx12_shader_compiler = + Dx12Compiler::from_env().unwrap_or(if cfg!(feature = "statically-linked-dxc") { + Dx12Compiler::StaticDxc + } else { + let dxc = "dxcompiler.dll"; + + if cfg!(target_os = "windows") && std::fs::metadata(dxc).is_ok() { + Dx12Compiler::DynamicDxc { + dxc_path: String::from(dxc), + max_shader_model: DxcShaderModel::V6_7, + } + } else { + Dx12Compiler::Fxc + } + }); + + let gles3_minor_version = Gles3MinorVersion::from_env().unwrap_or_default(); + + let instance_flags = InstanceFlags::default().with_env(); + + Self { + device_label: Default::default(), + backends, + power_preference, + priority, + features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, + disabled_features: None, + limits, + constrained_limits: None, + dx12_shader_compiler, + gles3_minor_version, + instance_flags, + memory_hints: MemoryHints::default(), + instance_memory_budget_thresholds: MemoryBudgetThresholds::default(), + force_fallback_adapter: false, + adapter_name: None, + } + } +} + +#[derive(Clone)] +pub struct RenderResources( + pub RenderDevice, + pub RenderQueue, + pub RenderAdapterInfo, + pub RenderAdapter, + pub RenderInstance, + #[cfg(feature = "raw_vulkan_init")] + pub crate::renderer::raw_vulkan_init::AdditionalVulkanFeatures, +); + +/// An enum describing how the renderer will initialize resources. This is used when creating the [`RenderPlugin`](crate::RenderPlugin). +#[expect( + clippy::large_enum_variant, + reason = "See https://github.com/bevyengine/bevy/issues/19220" +)] +pub enum RenderCreation { + /// Allows renderer resource initialization to happen outside of the rendering plugin. + Manual(RenderResources), + /// Lets the rendering plugin create resources itself. + Automatic(WgpuSettings), +} + +impl RenderCreation { + /// Function to create a [`RenderCreation::Manual`] variant. + pub fn manual( + device: RenderDevice, + queue: RenderQueue, + adapter_info: RenderAdapterInfo, + adapter: RenderAdapter, + instance: RenderInstance, + #[cfg(feature = "raw_vulkan_init")] + additional_vulkan_features: crate::renderer::raw_vulkan_init::AdditionalVulkanFeatures, + ) -> Self { + RenderResources( + device, + queue, + adapter_info, + adapter, + instance, + #[cfg(feature = "raw_vulkan_init")] + additional_vulkan_features, + ) + .into() + } +} + +impl From for RenderCreation { + fn from(value: RenderResources) -> Self { + Self::Manual(value) + } +} + +impl Default for RenderCreation { + fn default() -> Self { + Self::Automatic(Default::default()) + } +} + +impl From for RenderCreation { + fn from(value: WgpuSettings) -> Self { + Self::Automatic(value) + } +} + +/// Get a features/limits priority from the environment variable `WGPU_SETTINGS_PRIO` +pub fn settings_priority_from_env() -> Option { + Some( + match std::env::var("WGPU_SETTINGS_PRIO") + .as_deref() + .map(str::to_lowercase) + .as_deref() + { + Ok("compatibility") => WgpuSettingsPriority::Compatibility, + Ok("functionality") => WgpuSettingsPriority::Functionality, + Ok("webgl2") => WgpuSettingsPriority::WebGL2, + _ => return None, + }, + ) +} diff --git a/third_party/bevy_render/src/storage.rs b/third_party/bevy_render/src/storage.rs new file mode 100644 index 0000000..0362add --- /dev/null +++ b/third_party/bevy_render/src/storage.rs @@ -0,0 +1,158 @@ +use crate::{ + render_asset::{AssetExtractionError, PrepareAssetError, RenderAsset, RenderAssetPlugin}, + render_resource::{Buffer, BufferUsages}, + renderer::RenderDevice, +}; +use bevy_app::{App, Plugin}; +use bevy_asset::{Asset, AssetApp, AssetId, RenderAssetUsages}; +use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem}; +use bevy_reflect::{prelude::ReflectDefault, Reflect}; +use bevy_utils::default; +use encase::{internal::WriteInto, ShaderType}; +use wgpu::util::BufferInitDescriptor; + +/// Adds [`ShaderStorageBuffer`] as an asset that is extracted and uploaded to the GPU. +#[derive(Default)] +pub struct StoragePlugin; + +impl Plugin for StoragePlugin { + fn build(&self, app: &mut App) { + app.add_plugins(RenderAssetPlugin::::default()) + .init_asset::() + .register_asset_reflect::(); + } +} + +/// A storage buffer that is prepared as a [`RenderAsset`] and uploaded to the GPU. +#[derive(Asset, Reflect, Debug, Clone)] +#[reflect(opaque)] +#[reflect(Default, Debug, Clone)] +pub struct ShaderStorageBuffer { + /// Optional data used to initialize the buffer. + pub data: Option>, + /// The buffer description used to create the buffer. + pub buffer_description: wgpu::BufferDescriptor<'static>, + /// The asset usage of the storage buffer. + pub asset_usage: RenderAssetUsages, +} + +impl Default for ShaderStorageBuffer { + fn default() -> Self { + Self { + data: None, + buffer_description: wgpu::BufferDescriptor { + label: None, + size: 0, + usage: BufferUsages::STORAGE, + mapped_at_creation: false, + }, + asset_usage: RenderAssetUsages::default(), + } + } +} + +impl ShaderStorageBuffer { + /// Creates a new storage buffer with the given data and asset usage. + pub fn new(data: &[u8], asset_usage: RenderAssetUsages) -> Self { + let mut storage = ShaderStorageBuffer { + data: Some(data.to_vec()), + ..default() + }; + storage.asset_usage = asset_usage; + storage + } + + /// Creates a new storage buffer with the given size and asset usage. + pub fn with_size(size: usize, asset_usage: RenderAssetUsages) -> Self { + let mut storage = ShaderStorageBuffer { + data: None, + ..default() + }; + storage.buffer_description.size = size as u64; + storage.buffer_description.mapped_at_creation = false; + storage.asset_usage = asset_usage; + storage + } + + /// Sets the data of the storage buffer to the given [`ShaderType`]. + pub fn set_data(&mut self, value: T) + where + T: ShaderType + WriteInto, + { + let size = value.size().get() as usize; + let mut wrapper = encase::StorageBuffer::>::new(Vec::with_capacity(size)); + wrapper.write(&value).unwrap(); + self.data = Some(wrapper.into_inner()); + } +} + +impl From for ShaderStorageBuffer +where + T: ShaderType + WriteInto, +{ + fn from(value: T) -> Self { + let size = value.size().get() as usize; + let mut wrapper = encase::StorageBuffer::>::new(Vec::with_capacity(size)); + wrapper.write(&value).unwrap(); + Self::new(wrapper.as_ref(), RenderAssetUsages::default()) + } +} + +/// A storage buffer that is prepared as a [`RenderAsset`] and uploaded to the GPU. +pub struct GpuShaderStorageBuffer { + pub buffer: Buffer, + pub had_data: bool, +} + +impl RenderAsset for GpuShaderStorageBuffer { + type SourceAsset = ShaderStorageBuffer; + type Param = SRes; + + fn asset_usage(source_asset: &Self::SourceAsset) -> RenderAssetUsages { + source_asset.asset_usage + } + + fn take_gpu_data( + source: &mut Self::SourceAsset, + previous_gpu_asset: Option<&Self>, + ) -> Result { + let data = source.data.take(); + + let valid_upload = data.is_some() || previous_gpu_asset.is_none_or(|prev| !prev.had_data); + + valid_upload + .then(|| Self::SourceAsset { + data, + ..source.clone() + }) + .ok_or(AssetExtractionError::AlreadyExtracted) + } + + fn prepare_asset( + source_asset: Self::SourceAsset, + _: AssetId, + render_device: &mut SystemParamItem, + _: Option<&Self>, + ) -> Result> { + match source_asset.data { + Some(data) => { + let buffer = render_device.create_buffer_with_data(&BufferInitDescriptor { + label: source_asset.buffer_description.label, + contents: &data, + usage: source_asset.buffer_description.usage, + }); + Ok(GpuShaderStorageBuffer { + buffer, + had_data: true, + }) + } + None => { + let buffer = render_device.create_buffer(&source_asset.buffer_description); + Ok(GpuShaderStorageBuffer { + buffer, + had_data: false, + }) + } + } + } +} diff --git a/third_party/bevy_render/src/sync_component.rs b/third_party/bevy_render/src/sync_component.rs new file mode 100644 index 0000000..dd7eca1 --- /dev/null +++ b/third_party/bevy_render/src/sync_component.rs @@ -0,0 +1,42 @@ +use core::marker::PhantomData; + +use bevy_app::{App, Plugin}; +use bevy_ecs::component::Component; + +use crate::sync_world::{EntityRecord, PendingSyncEntity, SyncToRenderWorld}; + +/// Plugin that registers a component for automatic sync to the render world. See [`SyncWorldPlugin`] for more information. +/// +/// This plugin is automatically added by [`ExtractComponentPlugin`], and only needs to be added for manual extraction implementations. +/// +/// # Implementation details +/// +/// It adds [`SyncToRenderWorld`] as a required component to make the [`SyncWorldPlugin`] aware of the component, and +/// handles cleanup of the component in the render world when it is removed from an entity. +/// +/// # Warning +/// When the component is removed from the main world entity, all components are removed from the entity in the render world. +/// This is done in order to handle components with custom extraction logic and derived state. +/// +/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin +/// [`SyncWorldPlugin`]: crate::sync_world::SyncWorldPlugin +pub struct SyncComponentPlugin(PhantomData); + +impl Default for SyncComponentPlugin { + fn default() -> Self { + Self(PhantomData) + } +} + +impl Plugin for SyncComponentPlugin { + fn build(&self, app: &mut App) { + app.register_required_components::(); + + app.world_mut() + .register_component_hooks::() + .on_remove(|mut world, context| { + let mut pending = world.resource_mut::(); + pending.push(EntityRecord::ComponentRemoved(context.entity)); + }); + } +} diff --git a/third_party/bevy_render/src/sync_world.rs b/third_party/bevy_render/src/sync_world.rs new file mode 100644 index 0000000..6a1a1fa --- /dev/null +++ b/third_party/bevy_render/src/sync_world.rs @@ -0,0 +1,602 @@ +use bevy_app::Plugin; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + component::Component, + entity::{ContainsEntity, Entity, EntityEquivalent, EntityHash}, + lifecycle::{Add, Remove}, + observer::On, + query::With, + reflect::ReflectComponent, + resource::Resource, + system::{Local, Query, ResMut, SystemState}, + world::{Mut, World}, +}; +use bevy_platform::collections::{HashMap, HashSet}; +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; + +/// A plugin that synchronizes entities with [`SyncToRenderWorld`] between the main world and the render world. +/// +/// All entities with the [`SyncToRenderWorld`] component are kept in sync. It +/// is automatically added as a required component by [`ExtractComponentPlugin`] +/// and [`SyncComponentPlugin`], so it doesn't need to be added manually when +/// spawning or as a required component when either of these plugins are used. +/// +/// # Implementation +/// +/// Bevy's renderer is architected independently from the main app. +/// It operates in its own separate ECS [`World`], so the renderer logic can run in parallel with the main world logic. +/// This is called "Pipelined Rendering", see [`PipelinedRenderingPlugin`] for more information. +/// +/// [`SyncWorldPlugin`] is the first thing that runs every frame and it maintains an entity-to-entity mapping +/// between the main world and the render world. +/// It does so by spawning and despawning entities in the render world, to match spawned and despawned entities in the main world. +/// The link between synced entities is maintained by the [`RenderEntity`] and [`MainEntity`] components. +/// +/// The [`RenderEntity`] contains the corresponding render world entity of a main world entity, while [`MainEntity`] contains +/// the corresponding main world entity of a render world entity. +/// For convenience, [`QueryData`](bevy_ecs::query::QueryData) implementations are provided for both components: +/// adding [`MainEntity`] to a query (without a `&`) will return the corresponding main world [`Entity`], +/// and adding [`RenderEntity`] will return the corresponding render world [`Entity`]. +/// If you have access to the component itself, the underlying entities can be accessed by calling `.id()`. +/// +/// Synchronization is necessary preparation for extraction ([`ExtractSchedule`](crate::ExtractSchedule)), which copies over component data from the main +/// to the render world for these entities. +/// +/// ```text +/// |--------------------------------------------------------------------| +/// | | | Main world update | +/// | sync | extract |---------------------------------------------------| +/// | | | Render world update | +/// |--------------------------------------------------------------------| +/// ``` +/// +/// An example for synchronized main entities 1v1 and 18v1 +/// +/// ```text +/// |---------------------------Main World------------------------------| +/// | Entity | Component | +/// |-------------------------------------------------------------------| +/// | ID: 1v1 | PointLight | RenderEntity(ID: 3V1) | SyncToRenderWorld | +/// | ID: 18v1 | PointLight | RenderEntity(ID: 5V1) | SyncToRenderWorld | +/// |-------------------------------------------------------------------| +/// +/// |----------Render World-----------| +/// | Entity | Component | +/// |---------------------------------| +/// | ID: 3v1 | MainEntity(ID: 1V1) | +/// | ID: 5v1 | MainEntity(ID: 18V1) | +/// |---------------------------------| +/// +/// ``` +/// +/// Note that this effectively establishes a link between the main world entity and the render world entity. +/// Not every entity needs to be synchronized, however; only entities with the [`SyncToRenderWorld`] component are synced. +/// Adding [`SyncToRenderWorld`] to a main world component will establish such a link. +/// Once a synchronized main entity is despawned, its corresponding render entity will be automatically +/// despawned in the next `sync`. +/// +/// The sync step does not copy any of component data between worlds, since its often not necessary to transfer over all +/// the components of a main world entity. +/// The render world probably cares about a `Position` component, but not a `Velocity` component. +/// The extraction happens in its own step, independently from, and after synchronization. +/// +/// Moreover, [`SyncWorldPlugin`] only synchronizes *entities*. [`RenderAsset`](crate::render_asset::RenderAsset)s like meshes and textures are handled +/// differently. +/// +/// [`PipelinedRenderingPlugin`]: crate::pipelined_rendering::PipelinedRenderingPlugin +/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin +/// [`SyncComponentPlugin`]: crate::sync_component::SyncComponentPlugin +#[derive(Default)] +pub struct SyncWorldPlugin; + +impl Plugin for SyncWorldPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.init_resource::(); + app.add_observer( + |add: On, mut pending: ResMut| { + pending.push(EntityRecord::Added(add.entity)); + }, + ); + app.add_observer( + |remove: On, + mut pending: ResMut, + query: Query<&RenderEntity>| { + if let Ok(e) = query.get(remove.entity) { + pending.push(EntityRecord::Removed(*e)); + }; + }, + ); + } +} +/// Marker component that indicates that its entity needs to be synchronized to the render world. +/// +/// This component is automatically added as a required component by [`ExtractComponentPlugin`] and [`SyncComponentPlugin`]. +/// For more information see [`SyncWorldPlugin`]. +/// +/// NOTE: This component should persist throughout the entity's entire lifecycle. +/// If this component is removed from its entity, the entity will be despawned. +/// +/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin +/// [`SyncComponentPlugin`]: crate::sync_component::SyncComponentPlugin +#[derive(Component, Copy, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default, Clone)] +#[component(storage = "SparseSet")] +pub struct SyncToRenderWorld; + +/// Component added on the main world entities that are synced to the Render World in order to keep track of the corresponding render world entity. +/// +/// Can also be used as a newtype wrapper for render world entities. +#[derive(Component, Deref, Copy, Clone, Debug, Eq, Hash, PartialEq, Reflect)] +#[component(clone_behavior = Ignore)] +#[reflect(Component, Clone)] +pub struct RenderEntity(Entity); +impl RenderEntity { + #[inline] + pub fn id(&self) -> Entity { + self.0 + } +} + +impl From for RenderEntity { + fn from(entity: Entity) -> Self { + RenderEntity(entity) + } +} + +impl ContainsEntity for RenderEntity { + fn entity(&self) -> Entity { + self.id() + } +} + +// SAFETY: RenderEntity is a newtype around Entity that derives its comparison traits. +unsafe impl EntityEquivalent for RenderEntity {} + +/// Component added on the render world entities to keep track of the corresponding main world entity. +/// +/// Can also be used as a newtype wrapper for main world entities. +#[derive(Component, Deref, Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Reflect)] +#[reflect(Component, Clone)] +pub struct MainEntity(Entity); +impl MainEntity { + #[inline] + pub fn id(&self) -> Entity { + self.0 + } +} + +impl From for MainEntity { + fn from(entity: Entity) -> Self { + MainEntity(entity) + } +} + +impl ContainsEntity for MainEntity { + fn entity(&self) -> Entity { + self.id() + } +} + +// SAFETY: RenderEntity is a newtype around Entity that derives its comparison traits. +unsafe impl EntityEquivalent for MainEntity {} + +/// A [`HashMap`] pre-configured to use [`EntityHash`] hashing with a [`MainEntity`]. +pub type MainEntityHashMap = HashMap; + +/// A [`HashSet`] pre-configured to use [`EntityHash`] hashing with a [`MainEntity`].. +pub type MainEntityHashSet = HashSet; + +/// Marker component that indicates that its entity needs to be despawned at the end of the frame. +#[derive(Component, Copy, Clone, Debug, Default, Reflect)] +#[reflect(Component, Default, Clone)] +pub struct TemporaryRenderEntity; + +/// A record enum to what entities with [`SyncToRenderWorld`] have been added or removed. +#[derive(Debug)] +pub(crate) enum EntityRecord { + /// When an entity is spawned on the main world, notify the render world so that it can spawn a corresponding + /// entity. This contains the main world entity. + Added(Entity), + /// When an entity is despawned on the main world, notify the render world so that the corresponding entity can be + /// despawned. This contains the render world entity. + Removed(RenderEntity), + /// When a component is removed from an entity, notify the render world so that the corresponding component can be + /// removed. This contains the main world entity. + ComponentRemoved(Entity), +} + +// Entity Record in MainWorld pending to Sync +#[derive(Resource, Default, Deref, DerefMut)] +pub(crate) struct PendingSyncEntity { + records: Vec, +} + +pub(crate) fn entity_sync_system(main_world: &mut World, render_world: &mut World) { + main_world.resource_scope(|world, mut pending: Mut| { + // TODO : batching record + for record in pending.drain(..) { + match record { + EntityRecord::Added(e) => { + if let Ok(mut main_entity) = world.get_entity_mut(e) { + match main_entity.entry::() { + bevy_ecs::world::ComponentEntry::Occupied(_) => { + panic!("Attempting to synchronize an entity that has already been synchronized!"); + } + bevy_ecs::world::ComponentEntry::Vacant(entry) => { + let id = render_world.spawn(MainEntity(e)).id(); + + entry.insert(RenderEntity(id)); + } + }; + } + } + EntityRecord::Removed(render_entity) => { + if let Ok(ec) = render_world.get_entity_mut(render_entity.id()) { + ec.despawn(); + }; + } + EntityRecord::ComponentRemoved(main_entity) => { + let Some(mut render_entity) = world.get_mut::(main_entity) else { + continue; + }; + if let Ok(render_world_entity) = render_world.get_entity_mut(render_entity.id()) { + // In order to handle components that extract to derived components, we clear the entity + // and let the extraction system re-add the components. + render_world_entity.despawn(); + + let id = render_world.spawn(MainEntity(main_entity)).id(); + render_entity.0 = id; + } + }, + } + } + }); +} + +pub(crate) fn despawn_temporary_render_entities( + world: &mut World, + state: &mut SystemState>>, + mut local: Local>, +) { + let query = state.get(world); + + local.extend(query.iter()); + + // Ensure next frame allocation keeps order + local.sort_unstable_by_key(|e| e.index()); + for e in local.drain(..).rev() { + world.despawn(e); + } +} + +/// This module exists to keep the complex unsafe code out of the main module. +/// +/// The implementations for both [`MainEntity`] and [`RenderEntity`] should stay in sync, +/// and are based off of the `&T` implementation in `bevy_ecs`. +mod render_entities_world_query_impls { + use super::{MainEntity, RenderEntity}; + + use bevy_ecs::{ + archetype::Archetype, + change_detection::Tick, + component::{ComponentId, Components}, + entity::Entity, + query::{ + ArchetypeQueryData, FilteredAccess, QueryData, ReadOnlyQueryData, + ReleaseStateQueryData, WorldQuery, + }, + storage::{Table, TableRow}, + world::{unsafe_world_cell::UnsafeWorldCell, World}, + }; + + /// SAFETY: defers completely to `&RenderEntity` implementation, + /// and then only modifies the output safely. + unsafe impl WorldQuery for RenderEntity { + type Fetch<'w> = <&'static RenderEntity as WorldQuery>::Fetch<'w>; + type State = <&'static RenderEntity as WorldQuery>::State; + + fn shrink_fetch<'wlong: 'wshort, 'wshort>( + fetch: Self::Fetch<'wlong>, + ) -> Self::Fetch<'wshort> { + fetch + } + + #[inline] + unsafe fn init_fetch<'w, 's>( + world: UnsafeWorldCell<'w>, + component_id: &'s ComponentId, + last_run: Tick, + this_run: Tick, + ) -> Self::Fetch<'w> { + // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`. + unsafe { + <&RenderEntity as WorldQuery>::init_fetch(world, component_id, last_run, this_run) + } + } + + const IS_DENSE: bool = <&'static RenderEntity as WorldQuery>::IS_DENSE; + + #[inline] + unsafe fn set_archetype<'w, 's>( + fetch: &mut Self::Fetch<'w>, + component_id: &'s ComponentId, + archetype: &'w Archetype, + table: &'w Table, + ) { + // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`. + unsafe { + <&RenderEntity as WorldQuery>::set_archetype(fetch, component_id, archetype, table); + } + } + + #[inline] + unsafe fn set_table<'w, 's>( + fetch: &mut Self::Fetch<'w>, + &component_id: &'s ComponentId, + table: &'w Table, + ) { + // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`. + unsafe { <&RenderEntity as WorldQuery>::set_table(fetch, &component_id, table) } + } + + fn update_component_access(&component_id: &ComponentId, access: &mut FilteredAccess) { + <&RenderEntity as WorldQuery>::update_component_access(&component_id, access); + } + + fn init_state(world: &mut World) -> ComponentId { + <&RenderEntity as WorldQuery>::init_state(world) + } + + fn get_state(components: &Components) -> Option { + <&RenderEntity as WorldQuery>::get_state(components) + } + + fn matches_component_set( + &state: &ComponentId, + set_contains_id: &impl Fn(ComponentId) -> bool, + ) -> bool { + <&RenderEntity as WorldQuery>::matches_component_set(&state, set_contains_id) + } + } + + // SAFETY: Component access of Self::ReadOnly is a subset of Self. + // Self::ReadOnly matches exactly the same archetypes/tables as Self. + unsafe impl QueryData for RenderEntity { + const IS_READ_ONLY: bool = true; + const IS_ARCHETYPAL: bool = <&MainEntity as QueryData>::IS_ARCHETYPAL; + type ReadOnly = RenderEntity; + type Item<'w, 's> = Entity; + + fn shrink<'wlong: 'wshort, 'wshort, 's>( + item: Self::Item<'wlong, 's>, + ) -> Self::Item<'wshort, 's> { + item + } + + #[inline(always)] + unsafe fn fetch<'w, 's>( + state: &'s Self::State, + fetch: &mut Self::Fetch<'w>, + entity: Entity, + table_row: TableRow, + ) -> Option> { + // SAFETY: defers to the `&T` implementation, with T set to `RenderEntity`. + let component = + unsafe { <&RenderEntity as QueryData>::fetch(state, fetch, entity, table_row) }; + component.map(RenderEntity::id) + } + + fn iter_access( + state: &Self::State, + ) -> impl Iterator> { + <&RenderEntity as QueryData>::iter_access(state) + } + } + + // SAFETY: the underlying `Entity` is copied, and no mutable access is provided. + unsafe impl ReadOnlyQueryData for RenderEntity {} + + impl ArchetypeQueryData for RenderEntity {} + + impl ReleaseStateQueryData for RenderEntity { + fn release_state<'w>(item: Self::Item<'w, '_>) -> Self::Item<'w, 'static> { + item + } + } + + /// SAFETY: defers completely to `&RenderEntity` implementation, + /// and then only modifies the output safely. + unsafe impl WorldQuery for MainEntity { + type Fetch<'w> = <&'static MainEntity as WorldQuery>::Fetch<'w>; + type State = <&'static MainEntity as WorldQuery>::State; + + fn shrink_fetch<'wlong: 'wshort, 'wshort>( + fetch: Self::Fetch<'wlong>, + ) -> Self::Fetch<'wshort> { + fetch + } + + #[inline] + unsafe fn init_fetch<'w, 's>( + world: UnsafeWorldCell<'w>, + component_id: &'s ComponentId, + last_run: Tick, + this_run: Tick, + ) -> Self::Fetch<'w> { + // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`. + unsafe { + <&MainEntity as WorldQuery>::init_fetch(world, component_id, last_run, this_run) + } + } + + const IS_DENSE: bool = <&'static MainEntity as WorldQuery>::IS_DENSE; + + #[inline] + unsafe fn set_archetype<'w, 's>( + fetch: &mut Self::Fetch<'w>, + component_id: &ComponentId, + archetype: &'w Archetype, + table: &'w Table, + ) { + // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`. + unsafe { + <&MainEntity as WorldQuery>::set_archetype(fetch, component_id, archetype, table); + } + } + + #[inline] + unsafe fn set_table<'w, 's>( + fetch: &mut Self::Fetch<'w>, + &component_id: &'s ComponentId, + table: &'w Table, + ) { + // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`. + unsafe { <&MainEntity as WorldQuery>::set_table(fetch, &component_id, table) } + } + + fn update_component_access(&component_id: &ComponentId, access: &mut FilteredAccess) { + <&MainEntity as WorldQuery>::update_component_access(&component_id, access); + } + + fn init_state(world: &mut World) -> ComponentId { + <&MainEntity as WorldQuery>::init_state(world) + } + + fn get_state(components: &Components) -> Option { + <&MainEntity as WorldQuery>::get_state(components) + } + + fn matches_component_set( + &state: &ComponentId, + set_contains_id: &impl Fn(ComponentId) -> bool, + ) -> bool { + <&MainEntity as WorldQuery>::matches_component_set(&state, set_contains_id) + } + } + + // SAFETY: Component access of Self::ReadOnly is a subset of Self. + // Self::ReadOnly matches exactly the same archetypes/tables as Self. + unsafe impl QueryData for MainEntity { + const IS_READ_ONLY: bool = true; + const IS_ARCHETYPAL: bool = <&MainEntity as QueryData>::IS_ARCHETYPAL; + type ReadOnly = MainEntity; + type Item<'w, 's> = Entity; + + fn shrink<'wlong: 'wshort, 'wshort, 's>( + item: Self::Item<'wlong, 's>, + ) -> Self::Item<'wshort, 's> { + item + } + + #[inline(always)] + unsafe fn fetch<'w, 's>( + state: &'s Self::State, + fetch: &mut Self::Fetch<'w>, + entity: Entity, + table_row: TableRow, + ) -> Option> { + // SAFETY: defers to the `&T` implementation, with T set to `MainEntity`. + let component = + unsafe { <&MainEntity as QueryData>::fetch(state, fetch, entity, table_row) }; + component.map(MainEntity::id) + } + + fn iter_access( + state: &Self::State, + ) -> impl Iterator> { + <&MainEntity as QueryData>::iter_access(state) + } + } + + // SAFETY: the underlying `Entity` is copied, and no mutable access is provided. + unsafe impl ReadOnlyQueryData for MainEntity {} + + impl ArchetypeQueryData for MainEntity {} + + impl ReleaseStateQueryData for MainEntity { + fn release_state<'w>(item: Self::Item<'w, '_>) -> Self::Item<'w, 'static> { + item + } + } +} + +#[cfg(test)] +mod tests { + use bevy_ecs::{ + component::Component, + entity::Entity, + lifecycle::{Add, Remove}, + observer::On, + query::With, + system::{Query, ResMut}, + world::World, + }; + + use super::{ + entity_sync_system, EntityRecord, MainEntity, PendingSyncEntity, RenderEntity, + SyncToRenderWorld, + }; + + #[derive(Component)] + struct RenderDataComponent; + + #[test] + fn sync_world() { + let mut main_world = World::new(); + let mut render_world = World::new(); + main_world.init_resource::(); + + main_world.add_observer( + |add: On, mut pending: ResMut| { + pending.push(EntityRecord::Added(add.entity)); + }, + ); + main_world.add_observer( + |remove: On, + mut pending: ResMut, + query: Query<&RenderEntity>| { + if let Ok(e) = query.get(remove.entity) { + pending.push(EntityRecord::Removed(*e)); + }; + }, + ); + + // spawn some empty entities for test + for _ in 0..99 { + main_world.spawn_empty(); + } + + // spawn + let main_entity = main_world + .spawn(RenderDataComponent) + // indicates that its entity needs to be synchronized to the render world + .insert(SyncToRenderWorld) + .id(); + + entity_sync_system(&mut main_world, &mut render_world); + + let mut q = render_world.query_filtered::>(); + + // Only one synchronized entity + assert!(q.iter(&render_world).count() == 1); + + let render_entity = q.single(&render_world).unwrap(); + let render_entity_component = main_world.get::(main_entity).unwrap(); + + assert!(render_entity_component.id() == render_entity); + + let main_entity_component = render_world + .get::(render_entity_component.id()) + .unwrap(); + + assert!(main_entity_component.id() == main_entity); + + // despawn + main_world.despawn(main_entity); + + entity_sync_system(&mut main_world, &mut render_world); + + // Only one synchronized entity + assert!(q.iter(&render_world).count() == 0); + } +} diff --git a/third_party/bevy_render/src/texture/fallback_image.rs b/third_party/bevy_render/src/texture/fallback_image.rs new file mode 100644 index 0000000..738c84b --- /dev/null +++ b/third_party/bevy_render/src/texture/fallback_image.rs @@ -0,0 +1,274 @@ +use crate::{ + render_resource::*, + renderer::{RenderDevice, RenderQueue}, + texture::{DefaultImageSampler, GpuImage}, +}; +use bevy_asset::RenderAssetUsages; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + prelude::{FromWorld, Res, ResMut}, + resource::Resource, + system::SystemParam, +}; +use bevy_image::{BevyDefault, Image, ImageSampler, TextureFormatPixelInfo}; +use bevy_platform::collections::HashMap; + +/// A [`RenderApp`](crate::RenderApp) resource that contains the default "fallback image", +/// which can be used in situations where an image was not explicitly defined. The most common +/// use case is [`AsBindGroup`] implementations (such as materials) that support optional textures. +/// +/// Defaults to a 1x1 fully opaque white texture, (1.0, 1.0, 1.0, 1.0) which makes multiplying +/// it with other colors a no-op. +#[derive(Resource)] +pub struct FallbackImage { + /// Fallback image for [`TextureViewDimension::D1`]. + pub d1: GpuImage, + /// Fallback image for [`TextureViewDimension::D2`]. + pub d2: GpuImage, + /// Fallback image for [`TextureViewDimension::D2Array`]. + pub d2_array: GpuImage, + /// Fallback image for [`TextureViewDimension::Cube`]. + pub cube: GpuImage, + /// Fallback image for [`TextureViewDimension::CubeArray`]. + pub cube_array: GpuImage, + /// Fallback image for [`TextureViewDimension::D3`]. + pub d3: GpuImage, +} + +impl FallbackImage { + /// Returns the appropriate fallback image for the given texture dimension. + pub fn get(&self, texture_dimension: TextureViewDimension) -> &GpuImage { + match texture_dimension { + TextureViewDimension::D1 => &self.d1, + TextureViewDimension::D2 => &self.d2, + TextureViewDimension::D2Array => &self.d2_array, + TextureViewDimension::Cube => &self.cube, + TextureViewDimension::CubeArray => &self.cube_array, + TextureViewDimension::D3 => &self.d3, + } + } +} + +/// A [`RenderApp`](crate::RenderApp) resource that contains a _zero-filled_ "fallback image", +/// which can be used in place of [`FallbackImage`], when a fully transparent or black fallback +/// is required instead of fully opaque white. +/// +/// Defaults to a 1x1 fully transparent black texture, (0.0, 0.0, 0.0, 0.0) which makes adding +/// or alpha-blending it to other colors a no-op. +#[derive(Resource, Deref)] +pub struct FallbackImageZero(GpuImage); + +/// A [`RenderApp`](crate::RenderApp) resource that contains a "cubemap fallback image", +/// which can be used in situations where an image was not explicitly defined. The most common +/// use case is [`AsBindGroup`] implementations (such as materials) that support optional textures. +#[derive(Resource, Deref)] +pub struct FallbackImageCubemap(GpuImage); + +fn fallback_image_new( + render_device: &RenderDevice, + render_queue: &RenderQueue, + default_sampler: &DefaultImageSampler, + format: TextureFormat, + dimension: TextureViewDimension, + samples: u32, + value: u8, +) -> GpuImage { + // TODO make this configurable per channel + + let extents = Extent3d { + width: 1, + height: 1, + depth_or_array_layers: match dimension { + TextureViewDimension::Cube | TextureViewDimension::CubeArray => 6, + _ => 1, + }, + }; + + // We can't create textures with data when it's a depth texture or when using multiple samples + let create_texture_with_data = !format.is_depth_stencil_format() && samples == 1; + + let image_dimension = dimension.compatible_texture_dimension(); + let mut image = if create_texture_with_data { + let data = vec![value; format.pixel_size().unwrap_or(0)]; + Image::new_fill( + extents, + image_dimension, + &data, + format, + RenderAssetUsages::RENDER_WORLD, + ) + } else { + let mut image = Image::default_uninit(); + image.texture_descriptor.dimension = TextureDimension::D2; + image.texture_descriptor.size = extents; + image.texture_descriptor.format = format; + image + }; + image.texture_descriptor.sample_count = samples; + if image_dimension == TextureDimension::D2 { + image.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT; + } + + let texture = if create_texture_with_data { + render_device.create_texture_with_data( + render_queue, + &image.texture_descriptor, + TextureDataOrder::default(), + &image.data.expect("Image has no data"), + ) + } else { + render_device.create_texture(&image.texture_descriptor) + }; + + let texture_view = texture.create_view(&TextureViewDescriptor { + dimension: Some(dimension), + array_layer_count: Some(extents.depth_or_array_layers), + ..TextureViewDescriptor::default() + }); + let sampler = match image.sampler { + ImageSampler::Default => (**default_sampler).clone(), + ImageSampler::Descriptor(ref descriptor) => { + render_device.create_sampler(&descriptor.as_wgpu()) + } + }; + GpuImage { + texture, + texture_view, + texture_format: image.texture_descriptor.format, + texture_view_format: image.texture_view_descriptor.and_then(|v| v.format), + sampler, + size: image.texture_descriptor.size, + mip_level_count: image.texture_descriptor.mip_level_count, + had_data: true, + } +} + +impl FromWorld for FallbackImage { + fn from_world(world: &mut bevy_ecs::prelude::World) -> Self { + let render_device = world.resource::(); + let render_queue = world.resource::(); + let default_sampler = world.resource::(); + Self { + d1: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::D1, + 1, + 255, + ), + d2: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::D2, + 1, + 255, + ), + d2_array: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::D2Array, + 1, + 255, + ), + cube: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::Cube, + 1, + 255, + ), + cube_array: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::CubeArray, + 1, + 255, + ), + d3: fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::D3, + 1, + 255, + ), + } + } +} + +impl FromWorld for FallbackImageZero { + fn from_world(world: &mut bevy_ecs::prelude::World) -> Self { + let render_device = world.resource::(); + let render_queue = world.resource::(); + let default_sampler = world.resource::(); + Self(fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::D2, + 1, + 0, + )) + } +} + +impl FromWorld for FallbackImageCubemap { + fn from_world(world: &mut bevy_ecs::prelude::World) -> Self { + let render_device = world.resource::(); + let render_queue = world.resource::(); + let default_sampler = world.resource::(); + Self(fallback_image_new( + render_device, + render_queue, + default_sampler, + TextureFormat::bevy_default(), + TextureViewDimension::Cube, + 1, + 255, + )) + } +} + +/// A Cache of fallback textures that uses the sample count and `TextureFormat` as a key +/// +/// # WARNING +/// Images using MSAA with sample count > 1 are not initialized with data, therefore, +/// you shouldn't sample them before writing data to them first. +#[derive(Resource, Deref, DerefMut, Default)] +pub struct FallbackImageFormatMsaaCache(HashMap<(u32, TextureFormat), GpuImage>); + +#[derive(SystemParam)] +pub struct FallbackImageMsaa<'w> { + cache: ResMut<'w, FallbackImageFormatMsaaCache>, + render_device: Res<'w, RenderDevice>, + render_queue: Res<'w, RenderQueue>, + default_sampler: Res<'w, DefaultImageSampler>, +} + +impl<'w> FallbackImageMsaa<'w> { + pub fn image_for_samplecount(&mut self, sample_count: u32, format: TextureFormat) -> &GpuImage { + self.cache.entry((sample_count, format)).or_insert_with(|| { + fallback_image_new( + &self.render_device, + &self.render_queue, + &self.default_sampler, + format, + TextureViewDimension::D2, + sample_count, + 255, + ) + }) + } +} diff --git a/third_party/bevy_render/src/texture/gpu_image.rs b/third_party/bevy_render/src/texture/gpu_image.rs new file mode 100644 index 0000000..43483fc --- /dev/null +++ b/third_party/bevy_render/src/texture/gpu_image.rs @@ -0,0 +1,153 @@ +use crate::{ + render_asset::{AssetExtractionError, PrepareAssetError, RenderAsset}, + render_resource::{DefaultImageSampler, Sampler, Texture, TextureView}, + renderer::{RenderDevice, RenderQueue}, +}; +use bevy_asset::{AssetId, RenderAssetUsages}; +use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem}; +use bevy_image::{Image, ImageSampler}; +use bevy_math::{AspectRatio, UVec2}; +use tracing::warn; +use wgpu::{Extent3d, TextureFormat, TextureViewDescriptor}; + +/// The GPU-representation of an [`Image`]. +/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's size. +#[derive(Debug, Clone)] +pub struct GpuImage { + pub texture: Texture, + pub texture_view: TextureView, + pub texture_format: TextureFormat, + pub texture_view_format: Option, + pub sampler: Sampler, + pub size: Extent3d, + pub mip_level_count: u32, + pub had_data: bool, +} + +impl RenderAsset for GpuImage { + type SourceAsset = Image; + type Param = ( + SRes, + SRes, + SRes, + ); + + #[inline] + fn asset_usage(image: &Self::SourceAsset) -> RenderAssetUsages { + image.asset_usage + } + + fn take_gpu_data( + source: &mut Self::SourceAsset, + previous_gpu_asset: Option<&Self>, + ) -> Result { + let data = source.data.take(); + + // check if this image originally had data and no longer does, that implies it + // has already been extracted + let valid_upload = data.is_some() || previous_gpu_asset.is_none_or(|prev| !prev.had_data); + + valid_upload + .then(|| Self::SourceAsset { + data, + ..source.clone() + }) + .ok_or(AssetExtractionError::AlreadyExtracted) + } + + #[inline] + fn byte_len(image: &Self::SourceAsset) -> Option { + image.data.as_ref().map(Vec::len) + } + + /// Converts the extracted image into a [`GpuImage`]. + fn prepare_asset( + image: Self::SourceAsset, + _: AssetId, + (render_device, render_queue, default_sampler): &mut SystemParamItem, + previous_asset: Option<&Self>, + ) -> Result> { + let had_data = image.data.is_some(); + let texture = if let Some(ref data) = image.data { + render_device.create_texture_with_data( + render_queue, + &image.texture_descriptor, + image.data_order, + data, + ) + } else { + let new_texture = render_device.create_texture(&image.texture_descriptor); + if image.copy_on_resize { + if let Some(previous) = previous_asset { + let mut command_encoder = + render_device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("copy_image_on_resize"), + }); + let copy_size = Extent3d { + width: image.texture_descriptor.size.width.min(previous.size.width), + height: image + .texture_descriptor + .size + .height + .min(previous.size.height), + depth_or_array_layers: image + .texture_descriptor + .size + .depth_or_array_layers + .min(previous.size.depth_or_array_layers), + }; + + command_encoder.copy_texture_to_texture( + previous.texture.as_image_copy(), + new_texture.as_image_copy(), + copy_size, + ); + render_queue.submit([command_encoder.finish()]); + } else { + warn!("No previous asset to copy from for image: {:?}", image); + } + } + new_texture + }; + + let texture_view = texture.create_view( + image + .texture_view_descriptor + .as_ref() + .unwrap_or(&TextureViewDescriptor::default()), + ); + let sampler = match image.sampler { + ImageSampler::Default => (***default_sampler).clone(), + ImageSampler::Descriptor(descriptor) => { + render_device.create_sampler(&descriptor.as_wgpu()) + } + }; + + Ok(GpuImage { + texture, + texture_view, + texture_format: image.texture_descriptor.format, + texture_view_format: image.texture_view_descriptor.and_then(|v| v.format), + sampler, + size: image.texture_descriptor.size, + mip_level_count: image.texture_descriptor.mip_level_count, + had_data, + }) + } +} + +impl GpuImage { + /// Returns the aspect ratio (width / height) of a 2D image. + #[inline] + pub fn aspect_ratio(&self) -> AspectRatio { + AspectRatio::try_from_pixels(self.size.width, self.size.height).expect( + "Failed to calculate aspect ratio: Image dimensions must be positive, non-zero values", + ) + } + + /// Returns the size of a 2D image. + #[inline] + pub fn size_2d(&self) -> UVec2 { + UVec2::new(self.size.width, self.size.height) + } +} diff --git a/third_party/bevy_render/src/texture/manual_texture_view.rs b/third_party/bevy_render/src/texture/manual_texture_view.rs new file mode 100644 index 0000000..a9f3a3a --- /dev/null +++ b/third_party/bevy_render/src/texture/manual_texture_view.rs @@ -0,0 +1,68 @@ +use bevy_camera::ManualTextureViewHandle; +use bevy_ecs::{prelude::Component, resource::Resource}; +use bevy_image::BevyDefault; +use bevy_math::UVec2; +use bevy_platform::collections::HashMap; +use bevy_render_macros::ExtractResource; +use wgpu::TextureFormat; + +use crate::render_resource::TextureView; + +/// A manually managed [`TextureView`] for use as a [`bevy_camera::RenderTarget`]. +#[derive(Debug, Clone, Component)] +pub struct ManualTextureView { + pub texture_view: TextureView, + pub size: UVec2, + pub view_format: TextureFormat, +} + +impl ManualTextureView { + pub fn with_default_format(texture_view: TextureView, size: UVec2) -> Self { + Self { + texture_view, + size, + view_format: TextureFormat::bevy_default(), + } + } +} + +/// Resource that stores manually managed [`ManualTextureView`]s for use as a [`RenderTarget`](bevy_camera::RenderTarget). +/// This type dereferences to a `HashMap`. +/// To add a new texture view, pick a new [`ManualTextureViewHandle`] and insert it into the map. +/// Then, to render to the view, set a [`Camera`](bevy_camera::Camera)s `target` to `RenderTarget::TextureView(handle)`. +/// ```ignore +/// # use bevy_ecs::prelude::*; +/// # let mut world = World::default(); +/// # world.insert_resource(ManualTextureViews::default()); +/// # let texture_view = todo!(); +/// let manual_views = world.resource_mut::(); +/// let manual_view = ManualTextureView::with_default_format(texture_view, UVec2::new(1024, 1024)); +/// +/// // Choose an unused handle value; it's likely only you are inserting manual views. +/// const MANUAL_VIEW_HANDLE: ManualTextureViewHandle = ManualTextureViewHandle::new(42); +/// manual_views.insert(MANUAL_VIEW_HANDLE, manual_view); +/// +/// // Now you can spawn a Camera that renders to the manual view: +/// # use bevy_camera::{Camera, RenderTarget}; +/// world.spawn(Camera { +/// target: RenderTarget::TextureView(MANUAL_VIEW_HANDLE), +/// ..Default::default() +/// }); +/// ``` +/// Bevy will then use the `ManualTextureViews` resource to find your texture view and render to it. +#[derive(Default, Clone, Resource, ExtractResource)] +pub struct ManualTextureViews(HashMap); + +impl core::ops::Deref for ManualTextureViews { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl core::ops::DerefMut for ManualTextureViews { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/third_party/bevy_render/src/texture/mod.rs b/third_party/bevy_render/src/texture/mod.rs new file mode 100644 index 0000000..3d451b5 --- /dev/null +++ b/third_party/bevy_render/src/texture/mod.rs @@ -0,0 +1,73 @@ +mod fallback_image; +mod gpu_image; +mod manual_texture_view; +mod texture_attachment; +mod texture_cache; + +pub use crate::render_resource::DefaultImageSampler; +use bevy_image::{CompressedImageFormatSupport, CompressedImageFormats, ImageLoader, ImagePlugin}; +pub use fallback_image::*; +pub use gpu_image::*; +pub use manual_texture_view::*; +pub use texture_attachment::*; +pub use texture_cache::*; + +use crate::{ + extract_resource::ExtractResourcePlugin, render_asset::RenderAssetPlugin, + renderer::RenderDevice, Render, RenderApp, RenderSystems, +}; +use bevy_app::{App, Plugin}; +use bevy_asset::AssetApp; +use bevy_ecs::prelude::*; +use tracing::warn; + +#[derive(Default)] +pub struct TexturePlugin; + +impl Plugin for TexturePlugin { + fn build(&self, app: &mut App) { + app.add_plugins(( + RenderAssetPlugin::::default(), + ExtractResourcePlugin::::default(), + )) + .init_resource::(); + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.init_resource::().add_systems( + Render, + update_texture_cache_system.in_set(RenderSystems::Cleanup), + ); + } + } + + fn finish(&self, app: &mut App) { + if !ImageLoader::SUPPORTED_FORMATS.is_empty() { + let supported_compressed_formats = if let Some(resource) = + app.world().get_resource::() + { + resource.0 + } else { + warn!("CompressedImageFormatSupport resource not found. It should either be initialized in finish() of \ + RenderPlugin, or manually if not using the RenderPlugin or the WGPU backend."); + CompressedImageFormats::NONE + }; + + app.register_asset_loader(ImageLoader::new(supported_compressed_formats)); + } + let default_sampler = app.get_added_plugins::()[0] + .default_sampler + .clone(); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + let default_sampler = { + let device = render_app.world().resource::(); + device.create_sampler(&default_sampler.as_wgpu()) + }; + render_app + .insert_resource(DefaultImageSampler(default_sampler)) + .init_resource::() + .init_resource::() + .init_resource::() + .init_resource::(); + } + } +} diff --git a/third_party/bevy_render/src/texture/texture_attachment.rs b/third_party/bevy_render/src/texture/texture_attachment.rs new file mode 100644 index 0000000..3e4fbb8 --- /dev/null +++ b/third_party/bevy_render/src/texture/texture_attachment.rs @@ -0,0 +1,172 @@ +use super::CachedTexture; +use crate::render_resource::{TextureFormat, TextureView}; +use alloc::sync::Arc; +use bevy_color::LinearRgba; +use core::sync::atomic::{AtomicBool, Ordering}; +use wgpu::{ + LoadOp, Operations, RenderPassColorAttachment, RenderPassDepthStencilAttachment, StoreOp, +}; + +/// A wrapper for a [`CachedTexture`] that is used as a [`RenderPassColorAttachment`]. +#[derive(Clone)] +pub struct ColorAttachment { + pub texture: CachedTexture, + pub resolve_target: Option, + pub previous_frame_texture: Option, + clear_color: Option, + is_first_call: Arc, +} + +impl ColorAttachment { + pub fn new( + texture: CachedTexture, + resolve_target: Option, + previous_frame_texture: Option, + clear_color: Option, + ) -> Self { + Self { + texture, + resolve_target, + previous_frame_texture, + clear_color, + is_first_call: Arc::new(AtomicBool::new(true)), + } + } + + /// Get this texture view as an attachment. The attachment will be cleared with a value of + /// `clear_color` if this is the first time calling this function, otherwise it will be loaded. + /// + /// The returned attachment will always have writing enabled (`store: StoreOp::Load`). + pub fn get_attachment(&self) -> RenderPassColorAttachment<'_> { + if let Some(resolve_target) = self.resolve_target.as_ref() { + let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst); + + RenderPassColorAttachment { + view: &resolve_target.default_view, + depth_slice: None, + resolve_target: Some(&self.texture.default_view), + ops: Operations { + load: match (self.clear_color, first_call) { + (Some(clear_color), true) => LoadOp::Clear(clear_color.into()), + (None, _) | (Some(_), false) => LoadOp::Load, + }, + store: StoreOp::Store, + }, + } + } else { + self.get_unsampled_attachment() + } + } + + /// Get this texture view as an attachment, without the resolve target. The attachment will be cleared with + /// a value of `clear_color` if this is the first time calling this function, otherwise it will be loaded. + /// + /// The returned attachment will always have writing enabled (`store: StoreOp::Load`). + pub fn get_unsampled_attachment(&self) -> RenderPassColorAttachment<'_> { + let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst); + + RenderPassColorAttachment { + view: &self.texture.default_view, + depth_slice: None, + resolve_target: None, + ops: Operations { + load: match (self.clear_color, first_call) { + (Some(clear_color), true) => LoadOp::Clear(clear_color.into()), + (None, _) | (Some(_), false) => LoadOp::Load, + }, + store: StoreOp::Store, + }, + } + } + + pub(crate) fn mark_as_cleared(&self) { + self.is_first_call.store(false, Ordering::SeqCst); + } +} + +/// A wrapper for a [`TextureView`] that is used as a depth-only [`RenderPassDepthStencilAttachment`]. +#[derive(Clone)] +pub struct DepthAttachment { + pub view: TextureView, + clear_value: Option, + is_first_call: Arc, +} + +impl DepthAttachment { + pub fn new(view: TextureView, clear_value: Option) -> Self { + Self { + view, + clear_value, + is_first_call: Arc::new(AtomicBool::new(clear_value.is_some())), + } + } + + /// Get this texture view as an attachment. The attachment will be cleared with a value of + /// `clear_value` if this is the first time calling this function with `store` == [`StoreOp::Store`], + /// and a clear value was provided, otherwise it will be loaded. + pub fn get_attachment(&self, store: StoreOp) -> RenderPassDepthStencilAttachment<'_> { + let first_call = self + .is_first_call + .fetch_and(store != StoreOp::Store, Ordering::SeqCst); + + RenderPassDepthStencilAttachment { + view: &self.view, + depth_ops: Some(Operations { + load: if first_call { + // If first_call is true, then a clear value will always have been provided in the constructor + LoadOp::Clear(self.clear_value.unwrap()) + } else { + LoadOp::Load + }, + store, + }), + stencil_ops: None, + } + } +} + +/// A wrapper for a [`TextureView`] that is used as a [`RenderPassColorAttachment`] for a view +/// target's final output texture. +#[derive(Clone)] +pub struct OutputColorAttachment { + pub view: TextureView, + pub view_format: TextureFormat, + is_first_call: Arc, +} + +impl OutputColorAttachment { + pub fn new(view: TextureView, view_format: TextureFormat) -> Self { + Self { + view, + view_format, + is_first_call: Arc::new(AtomicBool::new(true)), + } + } + + /// Get this texture view as an attachment. The attachment will be cleared with a value of + /// the provided `clear_color` if this is the first time calling this function, otherwise it + /// will be loaded. + pub fn get_attachment(&self, clear_color: Option) -> RenderPassColorAttachment<'_> { + let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst); + + RenderPassColorAttachment { + view: &self.view, + depth_slice: None, + resolve_target: None, + ops: Operations { + load: match (clear_color, first_call) { + (Some(clear_color), true) => LoadOp::Clear(clear_color.into()), + (None, _) | (Some(_), false) => LoadOp::Load, + }, + store: StoreOp::Store, + }, + } + } + + /// Returns `true` if this attachment has been written to by a render pass. + // we re-use is_first_call atomic to track usage, which assumes that calls to get_attachment + // are always consumed by a render pass that writes to the attachment + pub fn needs_present(&self) -> bool { + !self.is_first_call.load(Ordering::SeqCst) + } +} diff --git a/third_party/bevy_render/src/texture/texture_cache.rs b/third_party/bevy_render/src/texture/texture_cache.rs new file mode 100644 index 0000000..ca1ef9b --- /dev/null +++ b/third_party/bevy_render/src/texture/texture_cache.rs @@ -0,0 +1,108 @@ +use crate::{ + render_resource::{Texture, TextureView}, + renderer::RenderDevice, +}; +use bevy_ecs::{prelude::ResMut, resource::Resource}; +use bevy_platform::collections::{hash_map::Entry, HashMap}; +use wgpu::{TextureDescriptor, TextureViewDescriptor}; + +/// The internal representation of a [`CachedTexture`] used to track whether it was recently used +/// and is currently taken. +struct CachedTextureMeta { + texture: Texture, + default_view: TextureView, + taken: bool, + frames_since_last_use: usize, +} + +/// A cached GPU [`Texture`] with corresponding [`TextureView`]. +/// +/// This is useful for textures that are created repeatedly (each frame) in the rendering process +/// to reduce the amount of GPU memory allocations. +#[derive(Clone)] +pub struct CachedTexture { + pub texture: Texture, + pub default_view: TextureView, +} + +/// This resource caches textures that are created repeatedly in the rendering process and +/// are only required for one frame. +#[derive(Resource, Default)] +pub struct TextureCache { + textures: HashMap, Vec>, +} + +impl TextureCache { + /// Retrieves a texture that matches the `descriptor`. If no matching one is found a new + /// [`CachedTexture`] is created. + pub fn get( + &mut self, + render_device: &RenderDevice, + descriptor: TextureDescriptor<'static>, + ) -> CachedTexture { + match self.textures.entry(descriptor) { + Entry::Occupied(mut entry) => { + for texture in entry.get_mut().iter_mut() { + if !texture.taken { + texture.frames_since_last_use = 0; + texture.taken = true; + return CachedTexture { + texture: texture.texture.clone(), + default_view: texture.default_view.clone(), + }; + } + } + + let texture = render_device.create_texture(&entry.key().clone()); + let default_view = texture.create_view(&TextureViewDescriptor::default()); + entry.get_mut().push(CachedTextureMeta { + texture: texture.clone(), + default_view: default_view.clone(), + frames_since_last_use: 0, + taken: true, + }); + CachedTexture { + texture, + default_view, + } + } + Entry::Vacant(entry) => { + let texture = render_device.create_texture(entry.key()); + let default_view = texture.create_view(&TextureViewDescriptor::default()); + entry.insert(vec![CachedTextureMeta { + texture: texture.clone(), + default_view: default_view.clone(), + taken: true, + frames_since_last_use: 0, + }]); + CachedTexture { + texture, + default_view, + } + } + } + } + + /// Returns `true` if the texture cache contains no textures. + pub fn is_empty(&self) -> bool { + self.textures.is_empty() + } + + /// Updates the cache and only retains recently used textures. + pub fn update(&mut self) { + self.textures.retain(|_, textures| { + for texture in textures.iter_mut() { + texture.frames_since_last_use += 1; + texture.taken = false; + } + + textures.retain(|texture| texture.frames_since_last_use < 3); + !textures.is_empty() + }); + } +} + +/// Updates the [`TextureCache`] to only retains recently used textures. +pub fn update_texture_cache_system(mut texture_cache: ResMut) { + texture_cache.update(); +} diff --git a/third_party/bevy_render/src/view/mod.rs b/third_party/bevy_render/src/view/mod.rs new file mode 100644 index 0000000..1bc5a32 --- /dev/null +++ b/third_party/bevy_render/src/view/mod.rs @@ -0,0 +1,1172 @@ +pub mod visibility; +pub mod window; + +use bevy_camera::{ + primitives::Frustum, CameraMainTextureUsages, ClearColor, ClearColorConfig, Exposure, + MainPassResolutionOverride, NormalizedRenderTarget, +}; +use bevy_diagnostic::FrameCount; +pub use visibility::*; +pub use window::*; + +use crate::{ + camera::{ExtractedCamera, MipBias, NormalizedRenderTargetExt as _, TemporalJitter}, + experimental::occlusion_culling::OcclusionCulling, + extract_component::ExtractComponentPlugin, + render_asset::RenderAssets, + render_phase::ViewRangefinder3d, + render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView}, + renderer::{RenderDevice, RenderQueue}, + sync_world::MainEntity, + texture::{ + CachedTexture, ColorAttachment, DepthAttachment, GpuImage, ManualTextureViews, + OutputColorAttachment, TextureCache, + }, + Render, RenderApp, RenderSystems, +}; +use alloc::sync::Arc; +use bevy_app::{App, Plugin}; +use bevy_color::LinearRgba; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::prelude::*; +use bevy_image::{BevyDefault as _, ToExtents}; +use bevy_math::{mat3, vec2, vec3, Mat3, Mat4, UVec4, Vec2, Vec3, Vec4, Vec4Swizzles}; +use bevy_platform::collections::{hash_map::Entry, HashMap}; +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; +use bevy_render_macros::ExtractComponent; +use bevy_shader::load_shader_library; +use bevy_transform::components::GlobalTransform; +use core::{ + ops::Range, + sync::atomic::{AtomicUsize, Ordering}, +}; +use wgpu::{ + BufferUsages, RenderPassColorAttachment, RenderPassDepthStencilAttachment, StoreOp, + TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, +}; + +/// The matrix that converts from the RGB to the LMS color space. +/// +/// To derive this, first we convert from RGB to [CIE 1931 XYZ]: +/// +/// ```text +/// ⎡ X ⎤ ⎡ 0.490 0.310 0.200 ⎤ ⎡ R ⎤ +/// ⎢ Y ⎥ = ⎢ 0.177 0.812 0.011 ⎥ ⎢ G ⎥ +/// ⎣ Z ⎦ ⎣ 0.000 0.010 0.990 ⎦ ⎣ B ⎦ +/// ``` +/// +/// Then we convert to LMS according to the [CAM16 standard matrix]: +/// +/// ```text +/// ⎡ L ⎤ ⎡ 0.401 0.650 -0.051 ⎤ ⎡ X ⎤ +/// ⎢ M ⎥ = ⎢ -0.250 1.204 0.046 ⎥ ⎢ Y ⎥ +/// ⎣ S ⎦ ⎣ -0.002 0.049 0.953 ⎦ ⎣ Z ⎦ +/// ``` +/// +/// The resulting matrix is just the concatenation of these two matrices, to do +/// the conversion in one step. +/// +/// [CIE 1931 XYZ]: https://en.wikipedia.org/wiki/CIE_1931_color_space +/// [CAM16 standard matrix]: https://en.wikipedia.org/wiki/LMS_color_space +static RGB_TO_LMS: Mat3 = mat3( + vec3(0.311692, 0.0905138, 0.00764433), + vec3(0.652085, 0.901341, 0.0486554), + vec3(0.0362225, 0.00814478, 0.943700), +); + +/// The inverse of the [`RGB_TO_LMS`] matrix, converting from the LMS color +/// space back to RGB. +static LMS_TO_RGB: Mat3 = mat3( + vec3(4.06305, -0.40791, -0.0118812), + vec3(-2.93241, 1.40437, -0.0486532), + vec3(-0.130646, 0.00353630, 1.0605344), +); + +/// The [CIE 1931] *xy* chromaticity coordinates of the [D65 white point]. +/// +/// [CIE 1931]: https://en.wikipedia.org/wiki/CIE_1931_color_space +/// [D65 white point]: https://en.wikipedia.org/wiki/Standard_illuminant#D65_values +static D65_XY: Vec2 = vec2(0.31272, 0.32903); + +/// The [D65 white point] in [LMS color space]. +/// +/// [LMS color space]: https://en.wikipedia.org/wiki/LMS_color_space +/// [D65 white point]: https://en.wikipedia.org/wiki/Standard_illuminant#D65_values +static D65_LMS: Vec3 = vec3(0.975538, 1.01648, 1.08475); + +pub struct ViewPlugin; + +impl Plugin for ViewPlugin { + fn build(&self, app: &mut App) { + load_shader_library!(app, "view.wgsl"); + + app + // NOTE: windows.is_changed() handles cases where a window was resized + .add_plugins(( + ExtractComponentPlugin::::default(), + ExtractComponentPlugin::::default(), + ExtractComponentPlugin::::default(), + RenderVisibilityRangePlugin, + )); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems( + Render, + ( + // `TextureView`s need to be dropped before reconfiguring window surfaces. + clear_view_attachments + .in_set(RenderSystems::ManageViews) + .before(create_surfaces), + cleanup_view_targets_for_resize + .in_set(RenderSystems::ManageViews) + .before(create_surfaces), + prepare_view_attachments + .in_set(RenderSystems::ManageViews) + .before(prepare_view_targets) + .after(prepare_windows), + prepare_view_targets + .in_set(RenderSystems::ManageViews) + .after(prepare_windows) + .after(crate::render_asset::prepare_assets::) + .ambiguous_with(crate::camera::sort_cameras), // doesn't use `sorted_camera_index_for_target` + prepare_view_uniforms.in_set(RenderSystems::PrepareResources), + ), + ); + } + } + + fn finish(&self, app: &mut App) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .init_resource::() + .init_resource::(); + } + } +} + +/// Component for configuring the number of samples for [Multi-Sample Anti-Aliasing](https://en.wikipedia.org/wiki/Multisample_anti-aliasing) +/// for a [`Camera`](bevy_camera::Camera). +/// +/// Defaults to 4 samples. A higher number of samples results in smoother edges. +/// +/// Some advanced rendering features may require that MSAA is disabled. +/// +/// Note that the web currently only supports 1 or 4 samples. +#[derive( + Component, + Default, + Clone, + Copy, + ExtractComponent, + Reflect, + PartialEq, + PartialOrd, + Eq, + Hash, + Debug, +)] +#[reflect(Component, Default, PartialEq, Hash, Debug)] +pub enum Msaa { + Off = 1, + Sample2 = 2, + #[default] + Sample4 = 4, + Sample8 = 8, +} + +impl Msaa { + #[inline] + pub fn samples(&self) -> u32 { + *self as u32 + } + + pub fn from_samples(samples: u32) -> Self { + match samples { + 1 => Msaa::Off, + 2 => Msaa::Sample2, + 4 => Msaa::Sample4, + 8 => Msaa::Sample8, + _ => panic!("Unsupported MSAA sample count: {samples}"), + } + } +} + +/// If this component is added to a camera, the camera will use an intermediate "high dynamic range" render texture. +/// This allows rendering with a wider range of lighting values. However, this does *not* affect +/// whether the camera will render with hdr display output (which bevy does not support currently) +/// and only affects the intermediate render texture. +#[derive( + Component, Default, Copy, Clone, ExtractComponent, Reflect, PartialEq, Eq, Hash, Debug, +)] +#[reflect(Component, Default, PartialEq, Hash, Debug)] +pub struct Hdr; + +/// An identifier for a view that is stable across frames. +/// +/// We can't use [`Entity`] for this because render world entities aren't +/// stable, and we can't use just [`MainEntity`] because some main world views +/// extract to multiple render world views. For example, a directional light +/// extracts to one render world view per cascade, and a point light extracts to +/// one render world view per cubemap face. So we pair the main entity with an +/// *auxiliary entity* and a *subview index*, which *together* uniquely identify +/// a view in the render world in a way that's stable from frame to frame. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct RetainedViewEntity { + /// The main entity that this view corresponds to. + pub main_entity: MainEntity, + + /// Another entity associated with the view entity. + /// + /// This is currently used for shadow cascades. If there are multiple + /// cameras, each camera needs to have its own set of shadow cascades. Thus + /// the light and subview index aren't themselves enough to uniquely + /// identify a shadow cascade: we need the camera that the cascade is + /// associated with as well. This entity stores that camera. + /// + /// If not present, this will be `MainEntity(Entity::PLACEHOLDER)`. + pub auxiliary_entity: MainEntity, + + /// The index of the view corresponding to the entity. + /// + /// For example, for point lights that cast shadows, this is the index of + /// the cubemap face (0 through 5 inclusive). For directional lights, this + /// is the index of the cascade. + pub subview_index: u32, +} + +impl RetainedViewEntity { + /// Creates a new [`RetainedViewEntity`] from the given main world entity, + /// auxiliary main world entity, and subview index. + /// + /// See [`RetainedViewEntity::subview_index`] for an explanation of what + /// `auxiliary_entity` and `subview_index` are. + pub fn new( + main_entity: MainEntity, + auxiliary_entity: Option, + subview_index: u32, + ) -> Self { + Self { + main_entity, + auxiliary_entity: auxiliary_entity.unwrap_or(Entity::PLACEHOLDER.into()), + subview_index, + } + } +} + +/// Describes a camera in the render world. +/// +/// Each entity in the main world can potentially extract to multiple subviews, +/// each of which has a [`RetainedViewEntity::subview_index`]. For instance, 3D +/// cameras extract to both a 3D camera subview with index 0 and a special UI +/// subview with index 1. Likewise, point lights with shadows extract to 6 +/// subviews, one for each side of the shadow cubemap. +#[derive(Component)] +pub struct ExtractedView { + /// The entity in the main world corresponding to this render world view. + pub retained_view_entity: RetainedViewEntity, + /// Typically a column-major right-handed projection matrix, one of either: + /// + /// Perspective (infinite reverse z) + /// ```text + /// f = 1 / tan(fov_y_radians / 2) + /// + /// ⎡ f / aspect 0 0 0 ⎤ + /// ⎢ 0 f 0 0 ⎥ + /// ⎢ 0 0 0 near ⎥ + /// ⎣ 0 0 -1 0 ⎦ + /// ``` + /// + /// Orthographic + /// ```text + /// w = right - left + /// h = top - bottom + /// d = far - near + /// cw = -right - left + /// ch = -top - bottom + /// + /// ⎡ 2 / w 0 0 cw / w ⎤ + /// ⎢ 0 2 / h 0 ch / h ⎥ + /// ⎢ 0 0 1 / d far / d ⎥ + /// ⎣ 0 0 0 1 ⎦ + /// ``` + /// + /// `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic + /// + /// Glam matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]` + /// + /// Custom projections are also possible however. + pub clip_from_view: Mat4, + pub world_from_view: GlobalTransform, + // The view-projection matrix. When provided it is used instead of deriving it from + // `projection` and `transform` fields, which can be helpful in cases where numerical + // stability matters and there is a more direct way to derive the view-projection matrix. + pub clip_from_world: Option, + pub hdr: bool, + // uvec4(origin.x, origin.y, width, height) + pub viewport: UVec4, + pub color_grading: ColorGrading, + + /// Whether to switch culling mode so that materials that request backface + /// culling cull front faces, and vice versa. + /// + /// This is typically used for cameras that mirror the world that they + /// render across a plane, because doing that flips the winding of each + /// polygon. + /// + /// This setting doesn't affect materials that disable backface culling. + pub invert_culling: bool, +} + +impl ExtractedView { + /// Creates a 3D rangefinder for a view + pub fn rangefinder3d(&self) -> ViewRangefinder3d { + ViewRangefinder3d::from_world_from_view(&self.world_from_view.affine()) + } +} + +/// Configures filmic color grading parameters to adjust the image appearance. +/// +/// Color grading is applied just before tonemapping for a given +/// [`Camera`](bevy_camera::Camera) entity, with the sole exception of the +/// `post_saturation` value in [`ColorGradingGlobal`], which is applied after +/// tonemapping. +#[derive(Component, Reflect, Debug, Default, Clone)] +#[reflect(Component, Default, Debug, Clone)] +pub struct ColorGrading { + /// Filmic color grading values applied to the image as a whole (as opposed + /// to individual sections, like shadows and highlights). + pub global: ColorGradingGlobal, + + /// Color grading values that are applied to the darker parts of the image. + /// + /// The cutoff points can be customized with the + /// [`ColorGradingGlobal::midtones_range`] field. + pub shadows: ColorGradingSection, + + /// Color grading values that are applied to the parts of the image with + /// intermediate brightness. + /// + /// The cutoff points can be customized with the + /// [`ColorGradingGlobal::midtones_range`] field. + pub midtones: ColorGradingSection, + + /// Color grading values that are applied to the lighter parts of the image. + /// + /// The cutoff points can be customized with the + /// [`ColorGradingGlobal::midtones_range`] field. + pub highlights: ColorGradingSection, +} + +/// Filmic color grading values applied to the image as a whole (as opposed to +/// individual sections, like shadows and highlights). +#[derive(Clone, Debug, Reflect)] +#[reflect(Default, Clone)] +pub struct ColorGradingGlobal { + /// Exposure value (EV) offset, measured in stops. + pub exposure: f32, + + /// An adjustment made to the [CIE 1931] chromaticity *x* value. + /// + /// Positive values make the colors redder. Negative values make the colors + /// bluer. This has no effect on luminance (brightness). + /// + /// [CIE 1931]: https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space + pub temperature: f32, + + /// An adjustment made to the [CIE 1931] chromaticity *y* value. + /// + /// Positive values make the colors more magenta. Negative values make the + /// colors greener. This has no effect on luminance (brightness). + /// + /// [CIE 1931]: https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space + pub tint: f32, + + /// An adjustment to the [hue], in radians. + /// + /// Adjusting this value changes the perceived colors in the image: red to + /// yellow to green to blue, etc. It has no effect on the saturation or + /// brightness of the colors. + /// + /// [hue]: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation + pub hue: f32, + + /// Saturation adjustment applied after tonemapping. + /// Values below 1.0 desaturate, with a value of 0.0 resulting in a grayscale image + /// with luminance defined by ITU-R BT.709 + /// Values above 1.0 increase saturation. + pub post_saturation: f32, + + /// The luminance (brightness) ranges that are considered part of the + /// "midtones" of the image. + /// + /// This affects which [`ColorGradingSection`]s apply to which colors. Note + /// that the sections smoothly blend into one another, to avoid abrupt + /// transitions. + /// + /// The default value is 0.2 to 0.7. + pub midtones_range: Range, +} + +/// The [`ColorGrading`] structure, packed into the most efficient form for the +/// GPU. +#[derive(Clone, Copy, Debug, ShaderType)] +pub struct ColorGradingUniform { + pub balance: Mat3, + pub saturation: Vec3, + pub contrast: Vec3, + pub gamma: Vec3, + pub gain: Vec3, + pub lift: Vec3, + pub midtone_range: Vec2, + pub exposure: f32, + pub hue: f32, + pub post_saturation: f32, +} + +/// A section of color grading values that can be selectively applied to +/// shadows, midtones, and highlights. +#[derive(Reflect, Debug, Copy, Clone, PartialEq)] +#[reflect(Clone, PartialEq)] +pub struct ColorGradingSection { + /// Values below 1.0 desaturate, with a value of 0.0 resulting in a grayscale image + /// with luminance defined by ITU-R BT.709. + /// Values above 1.0 increase saturation. + pub saturation: f32, + + /// Adjusts the range of colors. + /// + /// A value of 1.0 applies no changes. Values below 1.0 move the colors more + /// toward a neutral gray. Values above 1.0 spread the colors out away from + /// the neutral gray. + pub contrast: f32, + + /// A nonlinear luminance adjustment, mainly affecting the high end of the + /// range. + /// + /// This is the *n* exponent in the standard [ASC CDL] formula for color + /// correction: + /// + /// ```text + /// out = (i × s + o)ⁿ + /// ``` + /// + /// [ASC CDL]: https://en.wikipedia.org/wiki/ASC_CDL#Combined_Function + pub gamma: f32, + + /// A linear luminance adjustment, mainly affecting the middle part of the + /// range. + /// + /// This is the *s* factor in the standard [ASC CDL] formula for color + /// correction: + /// + /// ```text + /// out = (i × s + o)ⁿ + /// ``` + /// + /// [ASC CDL]: https://en.wikipedia.org/wiki/ASC_CDL#Combined_Function + pub gain: f32, + + /// A fixed luminance adjustment, mainly affecting the lower part of the + /// range. + /// + /// This is the *o* term in the standard [ASC CDL] formula for color + /// correction: + /// + /// ```text + /// out = (i × s + o)ⁿ + /// ``` + /// + /// [ASC CDL]: https://en.wikipedia.org/wiki/ASC_CDL#Combined_Function + pub lift: f32, +} + +impl Default for ColorGradingGlobal { + fn default() -> Self { + Self { + exposure: 0.0, + temperature: 0.0, + tint: 0.0, + hue: 0.0, + post_saturation: 1.0, + midtones_range: 0.2..0.7, + } + } +} + +impl Default for ColorGradingSection { + fn default() -> Self { + Self { + saturation: 1.0, + contrast: 1.0, + gamma: 1.0, + gain: 1.0, + lift: 0.0, + } + } +} + +impl ColorGrading { + /// Creates a new [`ColorGrading`] instance in which shadows, midtones, and + /// highlights all have the same set of color grading values. + pub fn with_identical_sections( + global: ColorGradingGlobal, + section: ColorGradingSection, + ) -> ColorGrading { + ColorGrading { + global, + highlights: section, + midtones: section, + shadows: section, + } + } + + /// Returns an iterator that visits the shadows, midtones, and highlights + /// sections, in that order. + pub fn all_sections(&self) -> impl Iterator { + [&self.shadows, &self.midtones, &self.highlights].into_iter() + } + + /// Applies the given mutating function to the shadows, midtones, and + /// highlights sections, in that order. + /// + /// Returns an array composed of the results of such evaluation, in that + /// order. + pub fn all_sections_mut(&mut self) -> impl Iterator { + [&mut self.shadows, &mut self.midtones, &mut self.highlights].into_iter() + } +} + +#[derive(Clone, ShaderType)] +pub struct ViewUniform { + pub clip_from_world: Mat4, + pub unjittered_clip_from_world: Mat4, + pub world_from_clip: Mat4, + pub world_from_view: Mat4, + pub view_from_world: Mat4, + /// Typically a column-major right-handed projection matrix, one of either: + /// + /// Perspective (infinite reverse z) + /// ```text + /// f = 1 / tan(fov_y_radians / 2) + /// + /// ⎡ f / aspect 0 0 0 ⎤ + /// ⎢ 0 f 0 0 ⎥ + /// ⎢ 0 0 0 near ⎥ + /// ⎣ 0 0 -1 0 ⎦ + /// ``` + /// + /// Orthographic + /// ```text + /// w = right - left + /// h = top - bottom + /// d = far - near + /// cw = -right - left + /// ch = -top - bottom + /// + /// ⎡ 2 / w 0 0 cw / w ⎤ + /// ⎢ 0 2 / h 0 ch / h ⎥ + /// ⎢ 0 0 1 / d far / d ⎥ + /// ⎣ 0 0 0 1 ⎦ + /// ``` + /// + /// `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic + /// + /// Glam matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]` + /// + /// Custom projections are also possible however. + pub clip_from_view: Mat4, + pub view_from_clip: Mat4, + pub world_position: Vec3, + pub exposure: f32, + // viewport(x_origin, y_origin, width, height) + pub viewport: Vec4, + pub main_pass_viewport: Vec4, + /// 6 world-space half spaces (normal: vec3, distance: f32) ordered left, right, top, bottom, near, far. + /// The normal vectors point towards the interior of the frustum. + /// A half space contains `p` if `normal.dot(p) + distance > 0.` + pub frustum: [Vec4; 6], + pub color_grading: ColorGradingUniform, + pub mip_bias: f32, + pub frame_count: u32, +} + +#[derive(Resource)] +pub struct ViewUniforms { + pub uniforms: DynamicUniformBuffer, +} + +impl FromWorld for ViewUniforms { + fn from_world(world: &mut World) -> Self { + let mut uniforms = DynamicUniformBuffer::default(); + uniforms.set_label(Some("view_uniforms_buffer")); + + let render_device = world.resource::(); + if render_device.limits().max_storage_buffers_per_shader_stage > 0 { + uniforms.add_usages(BufferUsages::STORAGE); + } + + Self { uniforms } + } +} + +#[derive(Component)] +pub struct ViewUniformOffset { + pub offset: u32, +} + +#[derive(Component, Clone)] +pub struct ViewTarget { + main_textures: MainTargetTextures, + main_texture_format: TextureFormat, + /// 0 represents `main_textures.a`, 1 represents `main_textures.b` + /// This is shared across view targets with the same render target + main_texture: Arc, + out_texture: OutputColorAttachment, +} + +/// Contains [`OutputColorAttachment`] used for each target present on any view in the current +/// frame, after being prepared by [`prepare_view_attachments`]. Users that want to override +/// the default output color attachment for a specific target can do so by adding a +/// [`OutputColorAttachment`] to this resource before [`prepare_view_targets`] is called. +#[derive(Resource, Default, Deref, DerefMut)] +pub struct ViewTargetAttachments(HashMap); + +pub struct PostProcessWrite<'a> { + pub source: &'a TextureView, + pub source_texture: &'a Texture, + pub destination: &'a TextureView, + pub destination_texture: &'a Texture, +} + +impl From for ColorGradingUniform { + fn from(component: ColorGrading) -> Self { + // Compute the balance matrix that will be used to apply the white + // balance adjustment to an RGB color. Our general approach will be to + // convert both the color and the developer-supplied white point to the + // LMS color space, apply the conversion, and then convert back. + // + // First, we start with the CIE 1931 *xy* values of the standard D65 + // illuminant: + // + // + // We then adjust them based on the developer's requested white balance. + let white_point_xy = D65_XY + vec2(-component.global.temperature, component.global.tint); + + // Convert the white point from CIE 1931 *xy* to LMS. First, we convert to XYZ: + // + // Y Y + // Y = 1 X = ─ x Z = ─ (1 - x - y) + // y y + // + // Then we convert from XYZ to LMS color space, using the CAM16 matrix + // from : + // + // ⎡ L ⎤ ⎡ 0.401 0.650 -0.051 ⎤ ⎡ X ⎤ + // ⎢ M ⎥ = ⎢ -0.250 1.204 0.046 ⎥ ⎢ Y ⎥ + // ⎣ S ⎦ ⎣ -0.002 0.049 0.953 ⎦ ⎣ Z ⎦ + // + // The following formula is just a simplification of the above. + + let white_point_lms = vec3(0.701634, 1.15856, -0.904175) + + (vec3(-0.051461, 0.045854, 0.953127) + + vec3(0.452749, -0.296122, -0.955206) * white_point_xy.x) + / white_point_xy.y; + + // Now that we're in LMS space, perform the white point scaling. + let white_point_adjustment = Mat3::from_diagonal(D65_LMS / white_point_lms); + + // Finally, combine the RGB → LMS → corrected LMS → corrected RGB + // pipeline into a single 3×3 matrix. + let balance = LMS_TO_RGB * white_point_adjustment * RGB_TO_LMS; + + Self { + balance, + saturation: vec3( + component.shadows.saturation, + component.midtones.saturation, + component.highlights.saturation, + ), + contrast: vec3( + component.shadows.contrast, + component.midtones.contrast, + component.highlights.contrast, + ), + gamma: vec3( + component.shadows.gamma, + component.midtones.gamma, + component.highlights.gamma, + ), + gain: vec3( + component.shadows.gain, + component.midtones.gain, + component.highlights.gain, + ), + lift: vec3( + component.shadows.lift, + component.midtones.lift, + component.highlights.lift, + ), + midtone_range: vec2( + component.global.midtones_range.start, + component.global.midtones_range.end, + ), + exposure: component.global.exposure, + hue: component.global.hue, + post_saturation: component.global.post_saturation, + } + } +} + +/// Add this component to a camera to disable *indirect mode*. +/// +/// Indirect mode, automatically enabled on supported hardware, allows Bevy to +/// offload transform and cull operations to the GPU, reducing CPU overhead. +/// Doing this, however, reduces the amount of control that your app has over +/// instancing decisions. In certain circumstances, you may want to disable +/// indirect drawing so that your app can manually instance meshes as it sees +/// fit. See the `custom_shader_instancing` example. +/// +/// The vast majority of applications will not need to use this component, as it +/// generally reduces rendering performance. +/// +/// Note: This component should only be added when initially spawning a camera. Adding +/// or removing after spawn can result in unspecified behavior. +#[derive(Component, Default)] +pub struct NoIndirectDrawing; + +impl ViewTarget { + pub const TEXTURE_FORMAT_HDR: TextureFormat = TextureFormat::Rgba16Float; + + /// Retrieve this target's main texture's color attachment. + pub fn get_color_attachment(&self) -> RenderPassColorAttachment<'_> { + if self.main_texture.load(Ordering::SeqCst) == 0 { + self.main_textures.a.get_attachment() + } else { + self.main_textures.b.get_attachment() + } + } + + /// Retrieve this target's "unsampled" main texture's color attachment. + pub fn get_unsampled_color_attachment(&self) -> RenderPassColorAttachment<'_> { + if self.main_texture.load(Ordering::SeqCst) == 0 { + self.main_textures.a.get_unsampled_attachment() + } else { + self.main_textures.b.get_unsampled_attachment() + } + } + + /// The "main" unsampled texture. + pub fn main_texture(&self) -> &Texture { + if self.main_texture.load(Ordering::SeqCst) == 0 { + &self.main_textures.a.texture.texture + } else { + &self.main_textures.b.texture.texture + } + } + + /// The _other_ "main" unsampled texture. + /// In most cases you should use [`Self::main_texture`] instead and never this. + /// The textures will naturally be swapped when [`Self::post_process_write`] is called. + /// + /// A use case for this is to be able to prepare a bind group for all main textures + /// ahead of time. + pub fn main_texture_other(&self) -> &Texture { + if self.main_texture.load(Ordering::SeqCst) == 0 { + &self.main_textures.b.texture.texture + } else { + &self.main_textures.a.texture.texture + } + } + + /// The "main" unsampled texture. + pub fn main_texture_view(&self) -> &TextureView { + if self.main_texture.load(Ordering::SeqCst) == 0 { + &self.main_textures.a.texture.default_view + } else { + &self.main_textures.b.texture.default_view + } + } + + /// The _other_ "main" unsampled texture view. + /// In most cases you should use [`Self::main_texture_view`] instead and never this. + /// The textures will naturally be swapped when [`Self::post_process_write`] is called. + /// + /// A use case for this is to be able to prepare a bind group for all main textures + /// ahead of time. + pub fn main_texture_other_view(&self) -> &TextureView { + if self.main_texture.load(Ordering::SeqCst) == 0 { + &self.main_textures.b.texture.default_view + } else { + &self.main_textures.a.texture.default_view + } + } + + /// The "main" sampled texture. + pub fn sampled_main_texture(&self) -> Option<&Texture> { + self.main_textures + .a + .resolve_target + .as_ref() + .map(|sampled| &sampled.texture) + } + + /// The "main" sampled texture view. + pub fn sampled_main_texture_view(&self) -> Option<&TextureView> { + self.main_textures + .a + .resolve_target + .as_ref() + .map(|sampled| &sampled.default_view) + } + + #[inline] + pub fn main_texture_format(&self) -> TextureFormat { + self.main_texture_format + } + + /// Returns `true` if and only if the main texture is [`Self::TEXTURE_FORMAT_HDR`] + #[inline] + pub fn is_hdr(&self) -> bool { + self.main_texture_format == ViewTarget::TEXTURE_FORMAT_HDR + } + + /// The final texture this view will render to. + #[inline] + pub fn out_texture(&self) -> &TextureView { + &self.out_texture.view + } + + pub fn out_texture_color_attachment( + &self, + clear_color: Option, + ) -> RenderPassColorAttachment<'_> { + self.out_texture.get_attachment(clear_color) + } + + /// Whether the final texture this view will render to needs to be presented. + pub fn needs_present(&self) -> bool { + self.out_texture.needs_present() + } + + /// The format of the final texture this view will render to + #[inline] + pub fn out_texture_view_format(&self) -> TextureFormat { + self.out_texture.view_format + } + + /// This will start a new "post process write", which assumes that the caller + /// will write the [`PostProcessWrite`]'s `source` to the `destination`. + /// + /// `source` is the "current" main texture. This will internally flip this + /// [`ViewTarget`]'s main texture to the `destination` texture, so the caller + /// _must_ ensure `source` is copied to `destination`, with or without modifications. + /// Failing to do so will cause the current main texture information to be lost. + pub fn post_process_write(&self) -> PostProcessWrite<'_> { + let old_is_a_main_texture = self.main_texture.fetch_xor(1, Ordering::SeqCst); + // if the old main texture is a, then the post processing must write from a to b + if old_is_a_main_texture == 0 { + self.main_textures.b.mark_as_cleared(); + PostProcessWrite { + source: &self.main_textures.a.texture.default_view, + source_texture: &self.main_textures.a.texture.texture, + destination: &self.main_textures.b.texture.default_view, + destination_texture: &self.main_textures.b.texture.texture, + } + } else { + self.main_textures.a.mark_as_cleared(); + PostProcessWrite { + source: &self.main_textures.b.texture.default_view, + source_texture: &self.main_textures.b.texture.texture, + destination: &self.main_textures.a.texture.default_view, + destination_texture: &self.main_textures.a.texture.texture, + } + } + } +} + +#[derive(Component)] +pub struct ViewDepthTexture { + pub texture: Texture, + attachment: DepthAttachment, +} + +impl ViewDepthTexture { + pub fn new(texture: CachedTexture, clear_value: Option) -> Self { + Self { + texture: texture.texture, + attachment: DepthAttachment::new(texture.default_view, clear_value), + } + } + + pub fn get_attachment(&self, store: StoreOp) -> RenderPassDepthStencilAttachment<'_> { + self.attachment.get_attachment(store) + } + + pub fn view(&self) -> &TextureView { + &self.attachment.view + } +} + +pub fn prepare_view_uniforms( + mut commands: Commands, + render_device: Res, + render_queue: Res, + mut view_uniforms: ResMut, + views: Query<( + Entity, + Option<&ExtractedCamera>, + &ExtractedView, + Option<&Frustum>, + Option<&TemporalJitter>, + Option<&MipBias>, + Option<&MainPassResolutionOverride>, + )>, + frame_count: Res, +) { + let view_iter = views.iter(); + let view_count = view_iter.len(); + let Some(mut writer) = + view_uniforms + .uniforms + .get_writer(view_count, &render_device, &render_queue) + else { + return; + }; + for ( + entity, + extracted_camera, + extracted_view, + frustum, + temporal_jitter, + mip_bias, + resolution_override, + ) in &views + { + let viewport = extracted_view.viewport.as_vec4(); + let mut main_pass_viewport = viewport; + if let Some(resolution_override) = resolution_override { + main_pass_viewport.z = resolution_override.0.x as f32; + main_pass_viewport.w = resolution_override.0.y as f32; + } + + let unjittered_projection = extracted_view.clip_from_view; + let mut clip_from_view = unjittered_projection; + + if let Some(temporal_jitter) = temporal_jitter { + temporal_jitter.jitter_projection(&mut clip_from_view, main_pass_viewport.zw()); + } + + let view_from_clip = clip_from_view.inverse(); + let world_from_view = extracted_view.world_from_view.to_matrix(); + let view_from_world = world_from_view.inverse(); + + let clip_from_world = if temporal_jitter.is_some() { + clip_from_view * view_from_world + } else { + extracted_view + .clip_from_world + .unwrap_or_else(|| clip_from_view * view_from_world) + }; + + // Map Frustum type to shader array, 6> + let frustum = frustum + .map(|frustum| frustum.half_spaces.map(|h| h.normal_d())) + .unwrap_or([Vec4::ZERO; 6]); + + let view_uniforms = ViewUniformOffset { + offset: writer.write(&ViewUniform { + clip_from_world, + unjittered_clip_from_world: unjittered_projection * view_from_world, + world_from_clip: world_from_view * view_from_clip, + world_from_view, + view_from_world, + clip_from_view, + view_from_clip, + world_position: extracted_view.world_from_view.translation(), + exposure: extracted_camera + .map(|c| c.exposure) + .unwrap_or_else(|| Exposure::default().exposure()), + viewport, + main_pass_viewport, + frustum, + color_grading: extracted_view.color_grading.clone().into(), + mip_bias: mip_bias.unwrap_or(&MipBias(0.0)).0, + frame_count: frame_count.0, + }), + }; + + commands.entity(entity).insert(view_uniforms); + } +} + +#[derive(Clone)] +struct MainTargetTextures { + a: ColorAttachment, + b: ColorAttachment, + /// 0 represents `main_textures.a`, 1 represents `main_textures.b` + /// This is shared across view targets with the same render target + main_texture: Arc, +} + +/// Prepares the view target [`OutputColorAttachment`] for each view in the current frame. +pub fn prepare_view_attachments( + windows: Res, + images: Res>, + manual_texture_views: Res, + cameras: Query<&ExtractedCamera>, + mut view_target_attachments: ResMut, +) { + for camera in cameras.iter() { + let Some(target) = &camera.target else { + continue; + }; + + match view_target_attachments.entry(target.clone()) { + Entry::Occupied(_) => {} + Entry::Vacant(entry) => { + let Some(attachment) = target + .get_texture_view(&windows, &images, &manual_texture_views) + .cloned() + .zip(target.get_texture_view_format(&windows, &images, &manual_texture_views)) + .map(|(view, format)| OutputColorAttachment::new(view.clone(), format)) + else { + continue; + }; + entry.insert(attachment); + } + }; + } +} + +/// Clears the view target [`OutputColorAttachment`]s. +pub fn clear_view_attachments(mut view_target_attachments: ResMut) { + view_target_attachments.clear(); +} + +pub fn cleanup_view_targets_for_resize( + mut commands: Commands, + windows: Res, + cameras: Query<(Entity, &ExtractedCamera), With>, +) { + for (entity, camera) in &cameras { + if let Some(NormalizedRenderTarget::Window(window_ref)) = &camera.target + && let Some(window) = windows.get(&window_ref.entity()) + && (window.size_changed || window.present_mode_changed) + { + commands.entity(entity).remove::(); + } + } +} + +pub fn prepare_view_targets( + mut commands: Commands, + clear_color_global: Res, + render_device: Res, + mut texture_cache: ResMut, + cameras: Query<( + Entity, + &ExtractedCamera, + &ExtractedView, + &CameraMainTextureUsages, + &Msaa, + )>, + view_target_attachments: Res, +) { + let mut textures = >::default(); + for (entity, camera, view, texture_usage, msaa) in cameras.iter() { + let (Some(target_size), Some(out_attachment)) = ( + camera.physical_target_size, + camera + .target + .as_ref() + .and_then(|target| view_target_attachments.get(target)), + ) else { + // If we can't find an output attachment we need to remove the ViewTarget + // component to make sure the camera doesn't try rendering to an invalid + // output attachment. + commands.entity(entity).try_remove::(); + + continue; + }; + + let main_texture_format = if view.hdr { + ViewTarget::TEXTURE_FORMAT_HDR + } else { + TextureFormat::bevy_default() + }; + + let clear_color = match camera.clear_color { + ClearColorConfig::Custom(color) => Some(color), + ClearColorConfig::None => None, + _ => Some(clear_color_global.0), + }; + + let (a, b, sampled, main_texture) = textures + .entry((camera.target.clone(), texture_usage.0, view.hdr, msaa)) + .or_insert_with(|| { + let descriptor = TextureDescriptor { + label: None, + size: target_size.to_extents(), + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: main_texture_format, + usage: texture_usage.0, + view_formats: match main_texture_format { + TextureFormat::Bgra8Unorm => &[TextureFormat::Bgra8UnormSrgb], + TextureFormat::Rgba8Unorm => &[TextureFormat::Rgba8UnormSrgb], + _ => &[], + }, + }; + let a = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("main_texture_a"), + ..descriptor + }, + ); + let b = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("main_texture_b"), + ..descriptor + }, + ); + let sampled = if msaa.samples() > 1 { + let sampled = texture_cache.get( + &render_device, + TextureDescriptor { + label: Some("main_texture_sampled"), + size: target_size.to_extents(), + mip_level_count: 1, + sample_count: msaa.samples(), + dimension: TextureDimension::D2, + format: main_texture_format, + usage: TextureUsages::RENDER_ATTACHMENT, + view_formats: descriptor.view_formats, + }, + ); + Some(sampled) + } else { + None + }; + let main_texture = Arc::new(AtomicUsize::new(0)); + (a, b, sampled, main_texture) + }); + + let converted_clear_color = clear_color.map(Into::into); + + let main_textures = MainTargetTextures { + a: ColorAttachment::new(a.clone(), sampled.clone(), None, converted_clear_color), + b: ColorAttachment::new(b.clone(), sampled.clone(), None, converted_clear_color), + main_texture: main_texture.clone(), + }; + + commands.entity(entity).insert(ViewTarget { + main_texture: main_textures.main_texture.clone(), + main_textures, + main_texture_format, + out_texture: out_attachment.clone(), + }); + } +} diff --git a/third_party/bevy_render/src/view/view.wgsl b/third_party/bevy_render/src/view/view.wgsl new file mode 100644 index 0000000..23ded53 --- /dev/null +++ b/third_party/bevy_render/src/view/view.wgsl @@ -0,0 +1,272 @@ +#define_import_path bevy_render::view + +struct ColorGrading { + balance: mat3x3, + saturation: vec3, + contrast: vec3, + gamma: vec3, + gain: vec3, + lift: vec3, + midtone_range: vec2, + exposure: f32, + hue: f32, + post_saturation: f32, +} + +struct View { + clip_from_world: mat4x4, + unjittered_clip_from_world: mat4x4, + world_from_clip: mat4x4, + world_from_view: mat4x4, + view_from_world: mat4x4, + // Typically a column-major right-handed projection matrix, one of either: + // + // Perspective (infinite reverse z) + // ``` + // f = 1 / tan(fov_y_radians / 2) + // + // ⎡ f / aspect 0 0 0 ⎤ + // ⎢ 0 f 0 0 ⎥ + // ⎢ 0 0 0 near ⎥ + // ⎣ 0 0 -1 0 ⎦ + // ``` + // + // Orthographic + // ``` + // w = right - left + // h = top - bottom + // d = far - near + // cw = -right - left + // ch = -top - bottom + // + // ⎡ 2 / w 0 0 cw / w ⎤ + // ⎢ 0 2 / h 0 ch / h ⎥ + // ⎢ 0 0 1 / d far / d ⎥ + // ⎣ 0 0 0 1 ⎦ + // ``` + // + // `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic + // + // Wgsl matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]` + // + // Custom projections are also possible however. + clip_from_view: mat4x4, + view_from_clip: mat4x4, + world_position: vec3, + exposure: f32, + // viewport(x_origin, y_origin, width, height) + viewport: vec4, + main_pass_viewport: vec4, + // 6 world-space half spaces (normal: vec3, distance: f32) ordered left, right, top, bottom, near, far. + // The normal vectors point towards the interior of the frustum. + // A half space contains `p` if `normal.dot(p) + distance > 0.` + frustum: array, 6>, + color_grading: ColorGrading, + mip_bias: f32, + frame_count: u32, +}; + +/// World space: +/// +y is up + +/// View space: +/// -z is forward, +x is right, +y is up +/// Forward is from the camera position into the scene. +/// (0.0, 0.0, -1.0) is linear distance of 1.0 in front of the camera's view relative to the camera's rotation +/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation + +/// NDC (normalized device coordinate): +/// https://www.w3.org/TR/webgpu/#coordinate-systems +/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC +/// (1.0, 1.0) in NDC is located at the top-right corner of NDC +/// Z is depth where: +/// 1.0 is near clipping plane +/// Perspective projection: 0.0 is inf far away +/// Orthographic projection: 0.0 is far clipping plane + +/// Clip space: +/// This is NDC before the perspective divide, still in homogenous coordinate space. +/// Dividing a clip space point by its w component yields a point in NDC space. + +/// UV space: +/// 0.0, 0.0 is the top left +/// 1.0, 1.0 is the bottom right + + +// ----------------- +// TO WORLD -------- +// ----------------- + +/// Convert a view space position to world space +fn position_view_to_world(view_pos: vec3, world_from_view: mat4x4) -> vec3 { + let world_pos = world_from_view * vec4(view_pos, 1.0); + return world_pos.xyz; +} + +/// Convert a clip space position to world space +fn position_clip_to_world(clip_pos: vec4, world_from_clip: mat4x4) -> vec3 { + let world_pos = world_from_clip * clip_pos; + return world_pos.xyz; +} + +/// Convert a ndc space position to world space +fn position_ndc_to_world(ndc_pos: vec3, world_from_clip: mat4x4) -> vec3 { + let world_pos = world_from_clip * vec4(ndc_pos, 1.0); + return world_pos.xyz / world_pos.w; +} + +/// Convert a view space direction to world space +fn direction_view_to_world(view_dir: vec3, world_from_view: mat4x4) -> vec3 { + let world_dir = world_from_view * vec4(view_dir, 0.0); + return world_dir.xyz; +} + +/// Convert a clip space direction to world space +fn direction_clip_to_world(clip_dir: vec4, world_from_clip: mat4x4) -> vec3 { + let world_dir = world_from_clip * clip_dir; + return world_dir.xyz; +} + +// ----------------- +// TO VIEW --------- +// ----------------- + +/// Convert a world space position to view space +fn position_world_to_view(world_pos: vec3, view_from_world: mat4x4) -> vec3 { + let view_pos = view_from_world * vec4(world_pos, 1.0); + return view_pos.xyz; +} + +/// Convert a clip space position to view space +fn position_clip_to_view(clip_pos: vec4, view_from_clip: mat4x4) -> vec3 { + let view_pos = view_from_clip * clip_pos; + return view_pos.xyz; +} + +/// Convert a ndc space position to view space +fn position_ndc_to_view(ndc_pos: vec3, view_from_clip: mat4x4) -> vec3 { + let view_pos = view_from_clip * vec4(ndc_pos, 1.0); + return view_pos.xyz / view_pos.w; +} + +/// Convert a world space direction to view space +fn direction_world_to_view(world_dir: vec3, view_from_world: mat4x4) -> vec3 { + let view_dir = view_from_world * vec4(world_dir, 0.0); + return view_dir.xyz; +} + +/// Convert a clip space direction to view space +fn direction_clip_to_view(clip_dir: vec4, view_from_clip: mat4x4) -> vec3 { + let view_dir = view_from_clip * clip_dir; + return view_dir.xyz; +} + +// ----------------- +// TO CLIP --------- +// ----------------- + +/// Convert a world space position to clip space +fn position_world_to_clip(world_pos: vec3, clip_from_world: mat4x4) -> vec4 { + let clip_pos = clip_from_world * vec4(world_pos, 1.0); + return clip_pos; +} + +/// Convert a view space position to clip space +fn position_view_to_clip(view_pos: vec3, clip_from_view: mat4x4) -> vec4 { + let clip_pos = clip_from_view * vec4(view_pos, 1.0); + return clip_pos; +} + +/// Convert a world space direction to clip space +fn direction_world_to_clip(world_dir: vec3, clip_from_world: mat4x4) -> vec4 { + let clip_dir = clip_from_world * vec4(world_dir, 0.0); + return clip_dir; +} + +/// Convert a view space direction to clip space +fn direction_view_to_clip(view_dir: vec3, clip_from_view: mat4x4) -> vec4 { + let clip_dir = clip_from_view * vec4(view_dir, 0.0); + return clip_dir; +} + +// ----------------- +// TO NDC ---------- +// ----------------- + +/// Convert a world space position to ndc space +fn position_world_to_ndc(world_pos: vec3, clip_from_world: mat4x4) -> vec3 { + let ndc_pos = clip_from_world * vec4(world_pos, 1.0); + return ndc_pos.xyz / ndc_pos.w; +} + +/// Convert a view space position to ndc space +fn position_view_to_ndc(view_pos: vec3, clip_from_view: mat4x4) -> vec3 { + let ndc_pos = clip_from_view * vec4(view_pos, 1.0); + return ndc_pos.xyz / ndc_pos.w; +} + +// ----------------- +// DEPTH ----------- +// ----------------- + +/// Retrieve the perspective camera near clipping plane +fn perspective_camera_near(clip_from_view: mat4x4) -> f32 { + return clip_from_view[3][2]; +} + +/// Convert ndc depth to linear view z. +/// Note: Depth values in front of the camera will be negative as -z is forward +fn depth_ndc_to_view_z(ndc_depth: f32, clip_from_view: mat4x4, view_from_clip: mat4x4) -> f32 { +#ifdef VIEW_PROJECTION_PERSPECTIVE + return -perspective_camera_near(clip_from_view) / ndc_depth; +#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC + return -(clip_from_view[3][2] - ndc_depth) / clip_from_view[2][2]; +#else + let view_pos = view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0); + return view_pos.z / view_pos.w; +#endif +} + +/// Convert linear view z to ndc depth. +/// Note: View z input should be negative for values in front of the camera as -z is forward +fn view_z_to_depth_ndc(view_z: f32, clip_from_view: mat4x4) -> f32 { +#ifdef VIEW_PROJECTION_PERSPECTIVE + return -perspective_camera_near(clip_from_view) / view_z; +#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC + return clip_from_view[3][2] + view_z * clip_from_view[2][2]; +#else + let ndc_pos = clip_from_view * vec4(0.0, 0.0, view_z, 1.0); + return ndc_pos.z / ndc_pos.w; +#endif +} + +// ----------------- +// UV -------------- +// ----------------- + +/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] +fn ndc_to_uv(ndc: vec2) -> vec2 { + return ndc * vec2(0.5, -0.5) + vec2(0.5); +} + +/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] +fn uv_to_ndc(uv: vec2) -> vec2 { + return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); +} + +/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target +/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)] +fn frag_coord_to_uv(frag_coord: vec2, viewport: vec4) -> vec2 { + return (frag_coord - viewport.xy) / viewport.zw; +} + +/// Convert frag coord to ndc +fn frag_coord_to_ndc(frag_coord: vec4, viewport: vec4) -> vec3 { + return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy, viewport)), frag_coord.z); +} + +/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target +/// viewport size] +fn ndc_to_frag_coord(ndc: vec2, viewport: vec4) -> vec2 { + return ndc_to_uv(ndc) * viewport.zw; +} diff --git a/third_party/bevy_render/src/view/visibility/mod.rs b/third_party/bevy_render/src/view/visibility/mod.rs new file mode 100644 index 0000000..3994827 --- /dev/null +++ b/third_party/bevy_render/src/view/visibility/mod.rs @@ -0,0 +1,54 @@ +use core::any::TypeId; + +use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent}; +use bevy_reflect::{prelude::ReflectDefault, Reflect}; +use bevy_utils::TypeIdMap; + +use crate::sync_world::MainEntity; + +mod range; +use bevy_camera::visibility::*; +pub use range::*; + +/// Collection of entities visible from the current view. +/// +/// This component is extracted from [`VisibleEntities`]. +#[derive(Clone, Component, Default, Debug, Reflect)] +#[reflect(Component, Default, Debug, Clone)] +pub struct RenderVisibleEntities { + #[reflect(ignore, clone)] + pub entities: TypeIdMap>, +} + +impl RenderVisibleEntities { + pub fn get(&self) -> &[(Entity, MainEntity)] + where + QF: 'static, + { + match self.entities.get(&TypeId::of::()) { + Some(entities) => &entities[..], + None => &[], + } + } + + pub fn iter(&self) -> impl DoubleEndedIterator + where + QF: 'static, + { + self.get::().iter() + } + + pub fn len(&self) -> usize + where + QF: 'static, + { + self.get::().len() + } + + pub fn is_empty(&self) -> bool + where + QF: 'static, + { + self.get::().is_empty() + } +} diff --git a/third_party/bevy_render/src/view/visibility/range.rs b/third_party/bevy_render/src/view/visibility/range.rs new file mode 100644 index 0000000..543f10f --- /dev/null +++ b/third_party/bevy_render/src/view/visibility/range.rs @@ -0,0 +1,228 @@ +//! Specific distances from the camera in which entities are visible, also known +//! as *hierarchical levels of detail* or *HLOD*s. + +use super::VisibilityRange; +use bevy_app::{App, Plugin}; +use bevy_ecs::{ + entity::Entity, + lifecycle::RemovedComponents, + query::Changed, + resource::Resource, + schedule::IntoScheduleConfigs as _, + system::{Query, Res, ResMut}, +}; +use bevy_math::{vec4, Vec4}; +use bevy_platform::collections::HashMap; +use bevy_utils::prelude::default; +use nonmax::NonMaxU16; +use wgpu::{BufferBindingType, BufferUsages}; + +use crate::{ + render_resource::BufferVec, + renderer::{RenderDevice, RenderQueue}, + sync_world::{MainEntity, MainEntityHashMap}, + Extract, ExtractSchedule, Render, RenderApp, RenderSystems, +}; + +/// We need at least 4 storage buffer bindings available to enable the +/// visibility range buffer. +/// +/// Even though we only use one storage buffer, the first 3 available storage +/// buffers will go to various light-related buffers. We will grab the fourth +/// buffer slot. +pub const VISIBILITY_RANGES_STORAGE_BUFFER_COUNT: u32 = 4; + +/// The size of the visibility ranges buffer in elements (not bytes) when fewer +/// than 6 storage buffers are available and we're forced to use a uniform +/// buffer instead (most notably, on WebGL 2). +const VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE: usize = 64; + +/// A plugin that enables [`RenderVisibilityRanges`]s, which allow entities to be +/// hidden or shown based on distance to the camera. +pub struct RenderVisibilityRangePlugin; + +impl Plugin for RenderVisibilityRangePlugin { + fn build(&self, app: &mut App) { + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .init_resource::() + .add_systems(ExtractSchedule, extract_visibility_ranges) + .add_systems( + Render, + write_render_visibility_ranges.in_set(RenderSystems::PrepareResourcesFlush), + ); + } +} + +/// Stores information related to [`VisibilityRange`]s in the render world. +#[derive(Resource)] +pub struct RenderVisibilityRanges { + /// Information corresponding to each entity. + entities: MainEntityHashMap, + + /// Maps a [`VisibilityRange`] to its index within the `buffer`. + /// + /// This map allows us to deduplicate identical visibility ranges, which + /// saves GPU memory. + range_to_index: HashMap, + + /// The GPU buffer that stores [`VisibilityRange`]s. + /// + /// Each [`Vec4`] contains the start margin start, start margin end, end + /// margin start, and end margin end distances, in that order. + buffer: BufferVec, + + /// True if the buffer has been changed since the last frame and needs to be + /// reuploaded to the GPU. + buffer_dirty: bool, +} + +/// Per-entity information related to [`VisibilityRange`]s. +struct RenderVisibilityEntityInfo { + /// The index of the range within the GPU buffer. + buffer_index: NonMaxU16, + /// True if the range is abrupt: i.e. has no crossfade. + is_abrupt: bool, +} + +impl Default for RenderVisibilityRanges { + fn default() -> Self { + Self { + entities: default(), + range_to_index: default(), + buffer: BufferVec::new( + BufferUsages::STORAGE | BufferUsages::UNIFORM | BufferUsages::VERTEX, + ), + buffer_dirty: true, + } + } +} + +impl RenderVisibilityRanges { + /// Clears out the [`RenderVisibilityRanges`] in preparation for a new + /// frame. + fn clear(&mut self) { + self.entities.clear(); + self.range_to_index.clear(); + self.buffer.clear(); + self.buffer_dirty = true; + } + + /// Inserts a new entity into the [`RenderVisibilityRanges`]. + fn insert(&mut self, entity: MainEntity, visibility_range: &VisibilityRange) { + // Grab a slot in the GPU buffer, or take the existing one if there + // already is one. + let buffer_index = *self + .range_to_index + .entry(visibility_range.clone()) + .or_insert_with(|| { + NonMaxU16::try_from(self.buffer.push(vec4( + visibility_range.start_margin.start, + visibility_range.start_margin.end, + visibility_range.end_margin.start, + visibility_range.end_margin.end, + )) as u16) + .unwrap_or_default() + }); + + self.entities.insert( + entity, + RenderVisibilityEntityInfo { + buffer_index, + is_abrupt: visibility_range.is_abrupt(), + }, + ); + } + + /// Returns the index in the GPU buffer corresponding to the visible range + /// for the given entity. + /// + /// If the entity has no visible range, returns `None`. + #[inline] + pub fn lod_index_for_entity(&self, entity: MainEntity) -> Option { + self.entities.get(&entity).map(|info| info.buffer_index) + } + + /// Returns true if the entity has a visibility range and it isn't abrupt: + /// i.e. if it has a crossfade. + #[inline] + pub fn entity_has_crossfading_visibility_ranges(&self, entity: MainEntity) -> bool { + self.entities + .get(&entity) + .is_some_and(|info| !info.is_abrupt) + } + + /// Returns a reference to the GPU buffer that stores visibility ranges. + #[inline] + pub fn buffer(&self) -> &BufferVec { + &self.buffer + } +} + +/// Extracts all [`VisibilityRange`] components from the main world to the +/// render world and inserts them into [`RenderVisibilityRanges`]. +pub fn extract_visibility_ranges( + mut render_visibility_ranges: ResMut, + visibility_ranges_query: Extract>, + changed_ranges_query: Extract>>, + mut removed_visibility_ranges: Extract>, +) { + if changed_ranges_query.is_empty() && removed_visibility_ranges.read().next().is_none() { + return; + } + + render_visibility_ranges.clear(); + for (entity, visibility_range) in visibility_ranges_query.iter() { + render_visibility_ranges.insert(entity.into(), visibility_range); + } +} + +/// Writes the [`RenderVisibilityRanges`] table to the GPU. +pub fn write_render_visibility_ranges( + render_device: Res, + render_queue: Res, + mut render_visibility_ranges: ResMut, +) { + // If there haven't been any changes, early out. + if !render_visibility_ranges.buffer_dirty { + return; + } + + // Mess with the length of the buffer to meet API requirements if necessary. + match render_device.get_supported_read_only_binding_type(VISIBILITY_RANGES_STORAGE_BUFFER_COUNT) + { + // If we're using a uniform buffer, we must have *exactly* + // `VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE` elements. + BufferBindingType::Uniform + if render_visibility_ranges.buffer.len() > VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE => + { + render_visibility_ranges + .buffer + .truncate(VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE); + } + BufferBindingType::Uniform + if render_visibility_ranges.buffer.len() < VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE => + { + while render_visibility_ranges.buffer.len() < VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE { + render_visibility_ranges.buffer.push(default()); + } + } + + // Otherwise, if we're using a storage buffer, just ensure there's + // something in the buffer, or else it won't get allocated. + BufferBindingType::Storage { .. } if render_visibility_ranges.buffer.is_empty() => { + render_visibility_ranges.buffer.push(default()); + } + + _ => {} + } + + // Schedule the write. + render_visibility_ranges + .buffer + .write_buffer(&render_device, &render_queue); + render_visibility_ranges.buffer_dirty = false; +} diff --git a/third_party/bevy_render/src/view/window/mod.rs b/third_party/bevy_render/src/view/window/mod.rs new file mode 100644 index 0000000..17aa918 --- /dev/null +++ b/third_party/bevy_render/src/view/window/mod.rs @@ -0,0 +1,490 @@ +use crate::renderer::WgpuWrapper; +use crate::{ + render_resource::{SurfaceTexture, TextureView}, + renderer::{RenderAdapter, RenderDevice, RenderInstance}, + Extract, ExtractSchedule, Render, RenderApp, RenderSystems, +}; +use bevy_app::{App, Plugin}; +use bevy_ecs::{entity::EntityHashMap, prelude::*}; +use bevy_platform::collections::HashSet; +use bevy_utils::default; +use bevy_window::{ + CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosing, +}; +use core::{ + num::NonZero, + ops::{Deref, DerefMut}, +}; +use tracing::{debug, info, warn}; +use wgpu::{ + SurfaceConfiguration, SurfaceTargetUnsafe, TextureFormat, TextureUsages, TextureViewDescriptor, +}; + +pub mod screenshot; + +use screenshot::ScreenshotPlugin; + +pub struct WindowRenderPlugin; + +impl Plugin for WindowRenderPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(ScreenshotPlugin); + + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app + .init_resource::() + .init_resource::() + .add_systems(ExtractSchedule, extract_windows) + .add_systems( + Render, + create_surfaces + .run_if(need_surface_configuration) + .before(prepare_windows), + ) + .add_systems(Render, prepare_windows.in_set(RenderSystems::ManageViews)); + } + } +} + +pub struct ExtractedWindow { + /// An entity that contains the components in [`Window`]. + pub entity: Entity, + pub handle: RawHandleWrapper, + pub physical_width: u32, + pub physical_height: u32, + pub present_mode: PresentMode, + pub desired_maximum_frame_latency: Option>, + /// Note: this will not always be the swap chain texture view. When taking a screenshot, + /// this will point to an alternative texture instead to allow for copying the render result + /// to CPU memory. + pub swap_chain_texture_view: Option, + pub swap_chain_texture: Option, + pub swap_chain_texture_format: Option, + pub swap_chain_texture_view_format: Option, + pub size_changed: bool, + pub present_mode_changed: bool, + pub alpha_mode: CompositeAlphaMode, + /// Whether this window needs an initial buffer commit. + /// + /// On Wayland, windows must present at least once before they are shown. + /// See + pub needs_initial_present: bool, +} + +impl ExtractedWindow { + fn set_swapchain_texture(&mut self, frame: wgpu::SurfaceTexture) { + self.swap_chain_texture_view_format = Some(frame.texture.format().add_srgb_suffix()); + let texture_view_descriptor = TextureViewDescriptor { + format: self.swap_chain_texture_view_format, + ..default() + }; + self.swap_chain_texture_view = Some(TextureView::from( + frame.texture.create_view(&texture_view_descriptor), + )); + self.swap_chain_texture = Some(SurfaceTexture::from(frame)); + } + + fn has_swapchain_texture(&self) -> bool { + self.swap_chain_texture_view.is_some() && self.swap_chain_texture.is_some() + } + + pub fn present(&mut self) { + if let Some(surface_texture) = self.swap_chain_texture.take() { + // TODO(clean): winit docs recommends calling pre_present_notify before this. + // though `present()` doesn't present the frame, it schedules it to be presented + // by wgpu. + // https://docs.rs/winit/0.29.9/wasm32-unknown-unknown/winit/window/struct.Window.html#method.pre_present_notify + surface_texture.present(); + } + } +} + +#[derive(Default, Resource)] +pub struct ExtractedWindows { + pub primary: Option, + pub windows: EntityHashMap, +} + +impl Deref for ExtractedWindows { + type Target = EntityHashMap; + + fn deref(&self) -> &Self::Target { + &self.windows + } +} + +impl DerefMut for ExtractedWindows { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.windows + } +} + +fn extract_windows( + mut extracted_windows: ResMut, + mut closing: Extract>, + windows: Extract)>>, + mut removed: Extract>, + mut window_surfaces: ResMut, +) { + for (entity, window, handle, primary) in windows.iter() { + if primary.is_some() { + extracted_windows.primary = Some(entity); + } + + let (new_width, new_height) = ( + window.resolution.physical_width().max(1), + window.resolution.physical_height().max(1), + ); + + let extracted_window = extracted_windows.entry(entity).or_insert(ExtractedWindow { + entity, + handle: handle.clone(), + physical_width: new_width, + physical_height: new_height, + present_mode: window.present_mode, + desired_maximum_frame_latency: window.desired_maximum_frame_latency, + swap_chain_texture: None, + swap_chain_texture_view: None, + size_changed: false, + swap_chain_texture_format: None, + swap_chain_texture_view_format: None, + present_mode_changed: false, + alpha_mode: window.composite_alpha_mode, + needs_initial_present: true, + }); + + if extracted_window.swap_chain_texture.is_none() { + // If we called present on the previous swap-chain texture last update, + // then drop the swap chain frame here, otherwise we can keep it for the + // next update as an optimization. `prepare_windows` will only acquire a new + // swap chain texture if needed. + extracted_window.swap_chain_texture_view = None; + } + extracted_window.size_changed = new_width != extracted_window.physical_width + || new_height != extracted_window.physical_height; + extracted_window.present_mode_changed = + window.present_mode != extracted_window.present_mode; + + if extracted_window.size_changed { + debug!( + "Window size changed from {}x{} to {}x{}", + extracted_window.physical_width, + extracted_window.physical_height, + new_width, + new_height + ); + extracted_window.physical_width = new_width; + extracted_window.physical_height = new_height; + } + + if extracted_window.present_mode_changed { + debug!( + "Window Present Mode changed from {:?} to {:?}", + extracted_window.present_mode, window.present_mode + ); + extracted_window.present_mode = window.present_mode; + } + } + + for closing_window in closing.read() { + extracted_windows.remove(&closing_window.window); + window_surfaces.remove(&closing_window.window); + } + for removed_window in removed.read() { + extracted_windows.remove(&removed_window); + window_surfaces.remove(&removed_window); + } +} + +struct SurfaceData { + // TODO: what lifetime should this be? + surface: WgpuWrapper>, + configuration: SurfaceConfiguration, + texture_view_format: Option, +} + +#[derive(Resource, Default)] +pub struct WindowSurfaces { + surfaces: EntityHashMap, + /// List of windows that we have already called the initial `configure_surface` for + configured_windows: HashSet, +} + +impl WindowSurfaces { + fn remove(&mut self, window: &Entity) { + self.surfaces.remove(window); + self.configured_windows.remove(window); + } +} + +/// (re)configures window surfaces, and obtains a swapchain texture for rendering. +/// +/// NOTE: `get_current_texture` in `prepare_windows` can take a long time if the GPU workload is +/// the performance bottleneck. This can be seen in profiles as multiple prepare-set systems all +/// taking an unusually long time to complete, and all finishing at about the same time as the +/// `prepare_windows` system. Improvements in bevy are planned to avoid this happening when it +/// should not but it will still happen as it is easy for a user to create a large GPU workload +/// relative to the GPU performance and/or CPU workload. +/// This can be caused by many reasons, but several of them are: +/// - GPU workload is more than your current GPU can manage +/// - Error / performance bug in your custom shaders +/// - wgpu was unable to detect a proper GPU hardware-accelerated device given the chosen +/// [`Backends`](crate::settings::Backends), [`WgpuLimits`](crate::settings::WgpuLimits), +/// and/or [`WgpuFeatures`](crate::settings::WgpuFeatures). For example, on Windows currently +/// `DirectX 11` is not supported by wgpu 0.12 and so if your GPU/drivers do not support Vulkan, +/// it may be that a software renderer called "Microsoft Basic Render Driver" using `DirectX 12` +/// will be chosen and performance will be very poor. This is visible in a log message that is +/// output during renderer initialization. +/// Another alternative is to try to use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle) and +/// [`Backends::GL`](crate::settings::Backends::GL) with the `gles` feature enabled if your +/// GPU/drivers support `OpenGL 4.3` / `OpenGL ES 3.0` or later. +pub fn prepare_windows( + mut windows: ResMut, + mut window_surfaces: ResMut, + render_device: Res, + #[cfg(target_os = "linux")] render_instance: Res, +) { + for window in windows.windows.values_mut() { + let window_surfaces = window_surfaces.deref_mut(); + let Some(surface_data) = window_surfaces.surfaces.get(&window.entity) else { + continue; + }; + + // We didn't present the previous frame, so we can keep using our existing swapchain texture. + if window.has_swapchain_texture() && !window.size_changed && !window.present_mode_changed { + continue; + } + + // A recurring issue is hitting `wgpu::SurfaceError::Timeout` on certain Linux + // mesa driver implementations. This seems to be a quirk of some drivers. + // We'd rather keep panicking when not on Linux mesa, because in those case, + // the `Timeout` is still probably the symptom of a degraded unrecoverable + // application state. + // see https://github.com/bevyengine/bevy/pull/5957 + // and https://github.com/gfx-rs/wgpu/issues/1218 + #[cfg(target_os = "linux")] + let may_erroneously_timeout = || { + render_instance + .enumerate_adapters(wgpu::Backends::VULKAN) + .iter() + .any(|adapter| { + let name = adapter.get_info().name; + name.starts_with("Radeon") + || name.starts_with("AMD") + || name.starts_with("Intel") + }) + }; + + let surface = &surface_data.surface; + match surface.get_current_texture() { + Ok(frame) => { + window.set_swapchain_texture(frame); + } + Err(wgpu::SurfaceError::Outdated) => { + render_device.configure_surface(surface, &surface_data.configuration); + let frame = match surface.get_current_texture() { + Ok(frame) => frame, + Err(err) => { + // This is a common occurrence on X11 and Xwayland with NVIDIA drivers + // when opening and resizing the window. + warn!("Couldn't get swap chain texture after configuring. Cause: '{err}'"); + continue; + } + }; + window.set_swapchain_texture(frame); + } + #[cfg(target_os = "linux")] + Err(wgpu::SurfaceError::Timeout) => { + if may_erroneously_timeout() { + tracing::trace!( + "Couldn't get swap chain texture. This is probably a quirk \ + of your Linux GPU driver, so it can be safely ignored." + ); + } else { + warn!( + "Couldn't get swap chain texture due to a timeout; skipping this frame." + ); + } + continue; + } + Err(err) => { + panic!("Couldn't get swap chain texture, operation unrecoverable: {err}"); + } + } + window.swap_chain_texture_format = Some(surface_data.configuration.format); + } +} + +pub fn need_surface_configuration( + windows: Res, + window_surfaces: Res, +) -> bool { + for window in windows.windows.values() { + if !window_surfaces.configured_windows.contains(&window.entity) + || window.size_changed + || window.present_mode_changed + { + return true; + } + } + false +} + +// 2 is wgpu's default/what we've been using so far. +// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish +// all work for the previous frame before starting work on the next frame, which then means the gpu +// has to wait for the cpu to finish to start on the next frame. +const DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY: u32 = 2; + +/// Creates window surfaces. +pub fn create_surfaces( + // By accessing a NonSend resource, we tell the scheduler to put this system on the main thread, + // which is necessary for some OS's + #[cfg(any(target_os = "macos", target_os = "ios"))] _marker: bevy_ecs::system::NonSendMarker, + mut windows: ResMut, + mut window_surfaces: ResMut, + render_instance: Res, + render_adapter: Res, + render_device: Res, +) { + for window in windows.windows.values_mut() { + let data = window_surfaces + .surfaces + .entry(window.entity) + .or_insert_with(|| { + let surface_target = SurfaceTargetUnsafe::RawHandle { + raw_display_handle: window.handle.get_display_handle(), + raw_window_handle: window.handle.get_window_handle(), + }; + // SAFETY: The window handles in ExtractedWindows will always be valid objects to create surfaces on + let surface = unsafe { + // NOTE: On some OSes this MUST be called from the main thread. + // As of wgpu 0.15, only fallible if the given window is a HTML canvas and obtaining a WebGPU or WebGL2 context fails. + render_instance + .create_surface_unsafe(surface_target) + .expect("Failed to create wgpu surface") + }; + let caps = surface.get_capabilities(&render_adapter); + let present_mode = present_mode(window, &caps); + let formats = caps.formats; + // For future HDR output support, we'll need to request a format that supports HDR, + // but as of wgpu 0.15 that is not yet supported. + // Prefer sRGB formats for surfaces, but fall back to first available format if no sRGB formats are available. + let mut format = *formats.first().expect("No supported formats for surface"); + for available_format in formats { + // Rgba8UnormSrgb and Bgra8UnormSrgb and the only sRGB formats wgpu exposes that we can use for surfaces. + if available_format == TextureFormat::Rgba8UnormSrgb + || available_format == TextureFormat::Bgra8UnormSrgb + { + format = available_format; + break; + } + } + + let texture_view_format = if !format.is_srgb() { + Some(format.add_srgb_suffix()) + } else { + None + }; + let configuration = SurfaceConfiguration { + format, + width: window.physical_width, + height: window.physical_height, + usage: TextureUsages::RENDER_ATTACHMENT, + present_mode, + desired_maximum_frame_latency: window + .desired_maximum_frame_latency + .map(NonZero::::get) + .unwrap_or(DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY), + alpha_mode: match window.alpha_mode { + CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto, + CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque, + CompositeAlphaMode::PreMultiplied => { + wgpu::CompositeAlphaMode::PreMultiplied + } + CompositeAlphaMode::PostMultiplied => { + wgpu::CompositeAlphaMode::PostMultiplied + } + CompositeAlphaMode::Inherit => wgpu::CompositeAlphaMode::Inherit, + }, + view_formats: match texture_view_format { + Some(format) => vec![format], + None => vec![], + }, + }; + + render_device.configure_surface(&surface, &configuration); + + SurfaceData { + surface: WgpuWrapper::new(surface), + configuration, + texture_view_format, + } + }); + + if window.size_changed || window.present_mode_changed { + // normally this is dropped on present but we double check here to be safe as failure to + // drop it will cause validation errors in wgpu + drop(window.swap_chain_texture.take()); + #[cfg_attr( + target_arch = "wasm32", + expect(clippy::drop_non_drop, reason = "texture views are not drop on wasm") + )] + drop(window.swap_chain_texture_view.take()); + + data.configuration.width = window.physical_width; + data.configuration.height = window.physical_height; + let caps = data.surface.get_capabilities(&render_adapter); + data.configuration.present_mode = present_mode(window, &caps); + render_device.configure_surface(&data.surface, &data.configuration); + } + + window_surfaces.configured_windows.insert(window.entity); + } +} + +fn present_mode( + window: &mut ExtractedWindow, + caps: &wgpu::SurfaceCapabilities, +) -> wgpu::PresentMode { + let present_mode = match window.present_mode { + PresentMode::Fifo => wgpu::PresentMode::Fifo, + PresentMode::FifoRelaxed => wgpu::PresentMode::FifoRelaxed, + PresentMode::Mailbox => wgpu::PresentMode::Mailbox, + PresentMode::Immediate => wgpu::PresentMode::Immediate, + PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync, + PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync, + }; + let fallbacks = match present_mode { + wgpu::PresentMode::AutoVsync => { + &[wgpu::PresentMode::FifoRelaxed, wgpu::PresentMode::Fifo][..] + } + wgpu::PresentMode::AutoNoVsync => &[ + wgpu::PresentMode::Immediate, + wgpu::PresentMode::Mailbox, + wgpu::PresentMode::Fifo, + ][..], + wgpu::PresentMode::Mailbox => &[ + wgpu::PresentMode::Mailbox, + wgpu::PresentMode::Immediate, + wgpu::PresentMode::Fifo, + ][..], + // Always end in FIFO to make sure it's always supported + x => &[x, wgpu::PresentMode::Fifo][..], + }; + let new_present_mode = fallbacks + .iter() + .copied() + .find(|fallback| caps.present_modes.contains(fallback)) + .unwrap_or_else(|| { + unreachable!( + "Fallback system failed to choose present mode. \ + This is a bug. Mode: {:?}, Options: {:?}", + window.present_mode, &caps.present_modes + ); + }); + if new_present_mode != present_mode && fallbacks.contains(&present_mode) { + info!("PresentMode {present_mode:?} requested but not available. Falling back to {new_present_mode:?}"); + } + new_present_mode +} diff --git a/third_party/bevy_render/src/view/window/screenshot.rs b/third_party/bevy_render/src/view/window/screenshot.rs new file mode 100644 index 0000000..655b9b3 --- /dev/null +++ b/third_party/bevy_render/src/view/window/screenshot.rs @@ -0,0 +1,695 @@ +use super::ExtractedWindows; +use crate::{ + gpu_readback, + render_asset::RenderAssets, + render_resource::{ + binding_types::texture_2d, BindGroup, BindGroupEntries, BindGroupLayoutDescriptor, + BindGroupLayoutEntries, Buffer, BufferUsages, CachedRenderPipelineId, FragmentState, + PipelineCache, RenderPipelineDescriptor, SpecializedRenderPipeline, + SpecializedRenderPipelines, Texture, TextureUsages, TextureView, VertexState, + }, + renderer::RenderDevice, + texture::{GpuImage, ManualTextureViews, OutputColorAttachment}, + view::{prepare_view_attachments, prepare_view_targets, ViewTargetAttachments, WindowSurfaces}, + ExtractSchedule, MainWorld, Render, RenderApp, RenderStartup, RenderSystems, +}; +use alloc::{borrow::Cow, sync::Arc}; +use bevy_app::{First, Plugin, Update}; +use bevy_asset::{embedded_asset, load_embedded_asset, AssetServer, Handle, RenderAssetUsages}; +use bevy_camera::{ManualTextureViewHandle, NormalizedRenderTarget, RenderTarget}; +use bevy_derive::{Deref, DerefMut}; +use bevy_ecs::{ + entity::EntityHashMap, message::message_update_system, prelude::*, system::SystemState, +}; +use bevy_image::{Image, TextureFormatPixelInfo, ToExtents}; +use bevy_platform::collections::HashSet; +use bevy_reflect::Reflect; +use bevy_shader::Shader; +use bevy_tasks::AsyncComputeTaskPool; +use bevy_utils::default; +use bevy_window::{PrimaryWindow, WindowRef}; +use core::ops::Deref; +use std::{ + path::Path, + sync::{ + mpsc::{Receiver, Sender}, + Mutex, + }, +}; +use tracing::{error, info, warn}; +use wgpu::{CommandEncoder, Extent3d, TextureFormat}; + +#[derive(EntityEvent, Reflect, Deref, DerefMut, Debug)] +#[reflect(Debug)] +pub struct ScreenshotCaptured { + pub entity: Entity, + #[deref] + pub image: Image, +} + +/// A component that signals to the renderer to capture a screenshot this frame. +/// +/// This component should be spawned on a new entity with an observer that will trigger +/// with [`ScreenshotCaptured`] when the screenshot is ready. +/// +/// Screenshots are captured asynchronously and may not be available immediately after the frame +/// that the component is spawned on. The observer should be used to handle the screenshot when it +/// is ready. +/// +/// Note that the screenshot entity will be despawned after the screenshot is captured and the +/// observer is triggered. +/// +/// # Usage +/// +/// ``` +/// # use bevy_ecs::prelude::*; +/// # use bevy_render::view::screenshot::{save_to_disk, Screenshot}; +/// +/// fn take_screenshot(mut commands: Commands) { +/// commands.spawn(Screenshot::primary_window()) +/// .observe(save_to_disk("screenshot.png")); +/// } +/// ``` +#[derive(Component, Deref, DerefMut, Reflect, Debug)] +#[reflect(Component, Debug)] +pub struct Screenshot(pub RenderTarget); + +/// A marker component that indicates that a screenshot is currently being captured. +#[derive(Component, Default)] +pub struct Capturing; + +/// A marker component that indicates that a screenshot has been captured, the image is ready, and +/// the screenshot entity can be despawned. +#[derive(Component, Default)] +pub struct Captured; + +impl Screenshot { + /// Capture a screenshot of the provided window entity. + pub fn window(window: Entity) -> Self { + Self(RenderTarget::Window(WindowRef::Entity(window))) + } + + /// Capture a screenshot of the primary window, if one exists. + pub fn primary_window() -> Self { + Self(RenderTarget::Window(WindowRef::Primary)) + } + + /// Capture a screenshot of the provided render target image. + pub fn image(image: Handle) -> Self { + Self(RenderTarget::Image(image.into())) + } + + /// Capture a screenshot of the provided manual texture view. + pub fn texture_view(texture_view: ManualTextureViewHandle) -> Self { + Self(RenderTarget::TextureView(texture_view)) + } +} + +struct ScreenshotPreparedState { + pub texture: Texture, + pub buffer: Buffer, + pub bind_group: BindGroup, + pub pipeline_id: CachedRenderPipelineId, + pub size: Extent3d, +} + +#[derive(Resource, Deref, DerefMut)] +pub struct CapturedScreenshots(pub Arc>>); + +#[derive(Resource, Deref, DerefMut, Default)] +struct RenderScreenshotTargets(EntityHashMap); + +#[derive(Resource, Deref, DerefMut, Default)] +struct RenderScreenshotsPrepared(EntityHashMap); + +#[derive(Resource, Deref, DerefMut)] +struct RenderScreenshotsSender(Sender<(Entity, Image)>); + +/// Saves the captured screenshot to disk at the provided path. +pub fn save_to_disk(path: impl AsRef) -> impl FnMut(On) { + let path = path.as_ref().to_owned(); + move |screenshot_captured| { + let img = screenshot_captured.image.clone(); + match img.try_into_dynamic() { + Ok(dyn_img) => match image::ImageFormat::from_path(&path) { + Ok(format) => { + // discard the alpha channel which stores brightness values when HDR is enabled to make sure + // the screenshot looks right + let img = dyn_img.to_rgb8(); + #[cfg(not(target_arch = "wasm32"))] + match img.save_with_format(&path, format) { + Ok(_) => info!("Screenshot saved to {}", path.display()), + Err(e) => error!("Cannot save screenshot, IO error: {e}"), + } + + #[cfg(target_arch = "wasm32")] + { + let save_screenshot = || { + use image::EncodableLayout; + use wasm_bindgen::{JsCast, JsValue}; + + let mut image_buffer = std::io::Cursor::new(Vec::new()); + img.write_to(&mut image_buffer, format) + .map_err(|e| JsValue::from_str(&format!("{e}")))?; + + let parts = js_sys::Array::of1( + &js_sys::Uint8Array::new_from_slice( + image_buffer.into_inner().as_bytes(), + ) + .into(), + ); + let blob = web_sys::Blob::new_with_u8_array_sequence(&parts)?; + let url = web_sys::Url::create_object_url_with_blob(&blob)?; + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let link = document.create_element("a")?; + link.set_attribute("href", &url)?; + link.set_attribute( + "download", + path.file_name() + .and_then(|filename| filename.to_str()) + .ok_or_else(|| JsValue::from_str("Invalid filename"))?, + )?; + let html_element = link.dyn_into::()?; + html_element.click(); + web_sys::Url::revoke_object_url(&url)?; + Ok::<(), JsValue>(()) + }; + + match (save_screenshot)() { + Ok(_) => info!("Screenshot saved to {}", path.display()), + Err(e) => error!("Cannot save screenshot, error: {e:?}"), + }; + } + } + Err(e) => error!("Cannot save screenshot, requested format not recognized: {e}"), + }, + Err(e) => error!("Cannot save screenshot, screen format cannot be understood: {e}"), + } + } +} + +fn clear_screenshots(mut commands: Commands, screenshots: Query>) { + for entity in screenshots.iter() { + commands.entity(entity).despawn(); + } +} + +pub fn trigger_screenshots( + mut commands: Commands, + captured_screenshots: ResMut, +) { + let captured_screenshots = captured_screenshots.lock().unwrap(); + while let Ok((entity, image)) = captured_screenshots.try_recv() { + commands.entity(entity).insert(Captured); + commands.trigger(ScreenshotCaptured { image, entity }); + } +} + +fn extract_screenshots( + mut targets: ResMut, + mut main_world: ResMut, + mut system_state: Local< + Option< + SystemState<( + Commands, + Query>, + Query<(Entity, &Screenshot), Without>, + )>, + >, + >, + mut seen_targets: Local>, +) { + if system_state.is_none() { + *system_state = Some(SystemState::new(&mut main_world)); + } + let system_state = system_state.as_mut().unwrap(); + let (mut commands, primary_window, screenshots) = system_state.get_mut(&mut main_world); + + targets.clear(); + seen_targets.clear(); + + let primary_window = primary_window.iter().next(); + + for (entity, screenshot) in screenshots.iter() { + let render_target = screenshot.0.clone(); + let Some(render_target) = render_target.normalize(primary_window) else { + warn!( + "Unknown render target for screenshot, skipping: {:?}", + render_target + ); + continue; + }; + if seen_targets.contains(&render_target) { + warn!( + "Duplicate render target for screenshot, skipping entity {}: {:?}", + entity, render_target + ); + // If we don't despawn the entity here, it will be captured again in the next frame + commands.entity(entity).despawn(); + continue; + } + seen_targets.insert(render_target.clone()); + targets.insert(entity, render_target); + commands.entity(entity).insert(Capturing); + } + + system_state.apply(&mut main_world); +} + +fn prepare_screenshots( + targets: Res, + mut prepared: ResMut, + window_surfaces: Res, + render_device: Res, + screenshot_pipeline: Res, + pipeline_cache: Res, + mut pipelines: ResMut>, + images: Res>, + manual_texture_views: Res, + mut view_target_attachments: ResMut, +) { + prepared.clear(); + for (entity, target) in targets.iter() { + match target { + NormalizedRenderTarget::Window(window) => { + let window = window.entity(); + let Some(surface_data) = window_surfaces.surfaces.get(&window) else { + warn!("Unknown window for screenshot, skipping: {}", window); + continue; + }; + let view_format = surface_data + .texture_view_format + .unwrap_or(surface_data.configuration.format); + let size = Extent3d { + width: surface_data.configuration.width, + height: surface_data.configuration.height, + ..default() + }; + let (texture_view, state) = prepare_screenshot_state( + size, + view_format, + &render_device, + &screenshot_pipeline, + &pipeline_cache, + &mut pipelines, + ); + prepared.insert(*entity, state); + view_target_attachments.insert( + target.clone(), + OutputColorAttachment::new(texture_view.clone(), view_format), + ); + } + NormalizedRenderTarget::Image(image) => { + let Some(gpu_image) = images.get(&image.handle) else { + warn!("Unknown image for screenshot, skipping: {:?}", image); + continue; + }; + let view_format = gpu_image + .texture_view_format + .unwrap_or(gpu_image.texture_format); + let (texture_view, state) = prepare_screenshot_state( + gpu_image.size, + view_format, + &render_device, + &screenshot_pipeline, + &pipeline_cache, + &mut pipelines, + ); + prepared.insert(*entity, state); + view_target_attachments.insert( + target.clone(), + OutputColorAttachment::new(texture_view.clone(), view_format), + ); + } + NormalizedRenderTarget::TextureView(texture_view) => { + let Some(manual_texture_view) = manual_texture_views.get(texture_view) else { + warn!( + "Unknown manual texture view for screenshot, skipping: {:?}", + texture_view + ); + continue; + }; + let view_format = manual_texture_view.view_format; + let size = manual_texture_view.size.to_extents(); + let (texture_view, state) = prepare_screenshot_state( + size, + view_format, + &render_device, + &screenshot_pipeline, + &pipeline_cache, + &mut pipelines, + ); + prepared.insert(*entity, state); + view_target_attachments.insert( + target.clone(), + OutputColorAttachment::new(texture_view.clone(), view_format), + ); + } + NormalizedRenderTarget::None { .. } => { + // Nothing to screenshot! + } + } + } +} + +fn prepare_screenshot_state( + size: Extent3d, + format: TextureFormat, + render_device: &RenderDevice, + pipeline: &ScreenshotToScreenPipeline, + pipeline_cache: &PipelineCache, + pipelines: &mut SpecializedRenderPipelines, +) -> (TextureView, ScreenshotPreparedState) { + let texture = render_device.create_texture(&wgpu::TextureDescriptor { + label: Some("screenshot-capture-rendertarget"), + size, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: TextureUsages::RENDER_ATTACHMENT + | TextureUsages::COPY_SRC + | TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + let texture_view = texture.create_view(&Default::default()); + let buffer = render_device.create_buffer(&wgpu::BufferDescriptor { + label: Some("screenshot-transfer-buffer"), + size: gpu_readback::get_aligned_size(size, format.pixel_size().unwrap_or(0) as u32) as u64, + usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let bind_group = render_device.create_bind_group( + "screenshot-to-screen-bind-group", + &pipeline_cache.get_bind_group_layout(&pipeline.bind_group_layout), + &BindGroupEntries::single(&texture_view), + ); + let pipeline_id = pipelines.specialize(pipeline_cache, pipeline, format); + + ( + texture_view, + ScreenshotPreparedState { + texture, + buffer, + bind_group, + pipeline_id, + size, + }, + ) +} + +pub struct ScreenshotPlugin; + +impl Plugin for ScreenshotPlugin { + fn build(&self, app: &mut bevy_app::App) { + embedded_asset!(app, "screenshot.wgsl"); + + let (tx, rx) = std::sync::mpsc::channel(); + app.insert_resource(CapturedScreenshots(Arc::new(Mutex::new(rx)))) + .add_systems( + First, + clear_screenshots + .after(message_update_system) + .before(ApplyDeferred), + ) + .add_systems(Update, trigger_screenshots); + + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { + return; + }; + + render_app + .insert_resource(RenderScreenshotsSender(tx)) + .init_resource::() + .init_resource::() + .init_resource::>() + .add_systems(RenderStartup, init_screenshot_to_screen_pipeline) + .add_systems(ExtractSchedule, extract_screenshots.ambiguous_with_all()) + .add_systems( + Render, + prepare_screenshots + .after(prepare_view_attachments) + .before(prepare_view_targets) + .in_set(RenderSystems::ManageViews), + ); + } +} + +#[derive(Resource)] +pub struct ScreenshotToScreenPipeline { + pub bind_group_layout: BindGroupLayoutDescriptor, + pub shader: Handle, +} + +pub fn init_screenshot_to_screen_pipeline(mut commands: Commands, asset_server: Res) { + let bind_group_layout = BindGroupLayoutDescriptor::new( + "screenshot-to-screen-bgl", + &BindGroupLayoutEntries::single( + wgpu::ShaderStages::FRAGMENT, + texture_2d(wgpu::TextureSampleType::Float { filterable: false }), + ), + ); + + let shader = load_embedded_asset!(asset_server.as_ref(), "screenshot.wgsl"); + + commands.insert_resource(ScreenshotToScreenPipeline { + bind_group_layout, + shader, + }); +} + +impl SpecializedRenderPipeline for ScreenshotToScreenPipeline { + type Key = TextureFormat; + + fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor { + RenderPipelineDescriptor { + label: Some(Cow::Borrowed("screenshot-to-screen")), + layout: vec![self.bind_group_layout.clone()], + vertex: VertexState { + shader: self.shader.clone(), + ..default() + }, + primitive: wgpu::PrimitiveState { + cull_mode: Some(wgpu::Face::Back), + ..Default::default() + }, + multisample: Default::default(), + fragment: Some(FragmentState { + shader: self.shader.clone(), + targets: vec![Some(wgpu::ColorTargetState { + format: key, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + ..default() + }), + ..default() + } + } +} + +pub(crate) fn submit_screenshot_commands(world: &World, encoder: &mut CommandEncoder) { + let targets = world.resource::(); + let prepared = world.resource::(); + let pipelines = world.resource::(); + let gpu_images = world.resource::>(); + let windows = world.resource::(); + let manual_texture_views = world.resource::(); + + for (entity, render_target) in targets.iter() { + match render_target { + NormalizedRenderTarget::Window(window) => { + let window = window.entity(); + let Some(window) = windows.get(&window) else { + continue; + }; + let width = window.physical_width; + let height = window.physical_height; + let Some(texture_format) = window.swap_chain_texture_view_format else { + continue; + }; + let Some(swap_chain_texture_view) = window.swap_chain_texture_view.as_ref() else { + continue; + }; + render_screenshot( + encoder, + prepared, + pipelines, + entity, + width, + height, + texture_format, + swap_chain_texture_view, + ); + } + NormalizedRenderTarget::Image(image) => { + let Some(gpu_image) = gpu_images.get(&image.handle) else { + warn!("Unknown image for screenshot, skipping: {:?}", image); + continue; + }; + let width = gpu_image.size.width; + let height = gpu_image.size.height; + let texture_format = gpu_image.texture_format; + let texture_view = gpu_image.texture_view.deref(); + render_screenshot( + encoder, + prepared, + pipelines, + entity, + width, + height, + texture_format, + texture_view, + ); + } + NormalizedRenderTarget::TextureView(texture_view) => { + let Some(texture_view) = manual_texture_views.get(texture_view) else { + warn!( + "Unknown manual texture view for screenshot, skipping: {:?}", + texture_view + ); + continue; + }; + let width = texture_view.size.x; + let height = texture_view.size.y; + let texture_format = texture_view.view_format; + let texture_view = texture_view.texture_view.deref(); + render_screenshot( + encoder, + prepared, + pipelines, + entity, + width, + height, + texture_format, + texture_view, + ); + } + NormalizedRenderTarget::None { .. } => { + // Nothing to screenshot! + } + }; + } +} + +fn render_screenshot( + encoder: &mut CommandEncoder, + prepared: &RenderScreenshotsPrepared, + pipelines: &PipelineCache, + entity: &Entity, + width: u32, + height: u32, + texture_format: TextureFormat, + texture_view: &wgpu::TextureView, +) { + if let Some(prepared_state) = &prepared.get(entity) { + let extent = Extent3d { + width, + height, + depth_or_array_layers: 1, + }; + encoder.copy_texture_to_buffer( + prepared_state.texture.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &prepared_state.buffer, + layout: gpu_readback::layout_data(extent, texture_format), + }, + extent, + ); + + if let Some(pipeline) = pipelines.get_render_pipeline(prepared_state.pipeline_id) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("screenshot_to_screen_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: texture_view, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); + pass.set_pipeline(pipeline); + pass.set_bind_group(0, &prepared_state.bind_group, &[]); + pass.draw(0..3, 0..1); + } + } +} + +pub(crate) fn collect_screenshots(world: &mut World) { + #[cfg(feature = "trace")] + let _span = tracing::info_span!("collect_screenshots").entered(); + + let sender = world.resource::().deref().clone(); + let prepared = world.resource::(); + + for (entity, prepared) in prepared.iter() { + let entity = *entity; + let sender = sender.clone(); + let width = prepared.size.width; + let height = prepared.size.height; + let texture_format = prepared.texture.format(); + let Ok(pixel_size) = texture_format.pixel_size() else { + continue; + }; + let buffer = prepared.buffer.clone(); + + let finish = async move { + let (tx, rx) = async_channel::bounded(1); + let buffer_slice = buffer.slice(..); + // The polling for this map call is done every frame when the command queue is submitted. + buffer_slice.map_async(wgpu::MapMode::Read, move |result| { + if let Err(err) = result { + panic!("{}", err.to_string()); + } + tx.try_send(()).unwrap(); + }); + rx.recv().await.unwrap(); + let data = buffer_slice.get_mapped_range(); + // we immediately move the data to CPU memory to avoid holding the mapped view for long + let mut result = Vec::from(&*data); + drop(data); + + if result.len() != ((width * height) as usize * pixel_size) { + // Our buffer has been padded because we needed to align to a multiple of 256. + // We remove this padding here + let initial_row_bytes = width as usize * pixel_size; + let buffered_row_bytes = + gpu_readback::align_byte_size(width * pixel_size as u32) as usize; + + let mut take_offset = buffered_row_bytes; + let mut place_offset = initial_row_bytes; + for _ in 1..height { + result.copy_within(take_offset..take_offset + buffered_row_bytes, place_offset); + take_offset += buffered_row_bytes; + place_offset += initial_row_bytes; + } + result.truncate(initial_row_bytes * height as usize); + } + + if let Err(e) = sender.send(( + entity, + Image::new( + Extent3d { + width, + height, + depth_or_array_layers: 1, + }, + wgpu::TextureDimension::D2, + result, + texture_format, + RenderAssetUsages::RENDER_WORLD, + ), + )) { + error!("Failed to send screenshot: {}", e); + } + }; + + AsyncComputeTaskPool::get().spawn(finish).detach(); + } +} diff --git a/third_party/bevy_render/src/view/window/screenshot.wgsl b/third_party/bevy_render/src/view/window/screenshot.wgsl new file mode 100644 index 0000000..2743fa1 --- /dev/null +++ b/third_party/bevy_render/src/view/window/screenshot.wgsl @@ -0,0 +1,16 @@ +// This vertex shader will create a triangle that will cover the entire screen +// with minimal effort, avoiding the need for a vertex buffer etc. +@vertex +fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4 { + let x = f32((in_vertex_index & 1u) << 2u); + let y = f32((in_vertex_index & 2u) << 1u); + return vec4(x - 1.0, y - 1.0, 0.0, 1.0); +} + +@group(0) @binding(0) var t: texture_2d; + +@fragment +fn fs_main(@builtin(position) pos: vec4) -> @location(0) vec4 { + let coords = floor(pos.xy); + return textureLoad(t, vec2(coords), 0i); +}