1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
//! Core ECS storage and orchestration layer.
//!
//! This module defines [`ECSData`], the central data structure of the ECS runtime.
//! It owns all archetypes and their component storage, manages entity placement and
//! migration, and drives both structural mutations and parallel query execution.
//!
//! # Architecture
//!
//! The ECS follows an **archetype-based** storage model: entities are grouped into
//! [`Archetype`]s according to their exact component signature. Each archetype stores
//! its components in contiguous, chunk-aligned columns, enabling cache-friendly
//! parallel iteration.
//!
//! # Responsibilities
//!
//! - **Archetype management** - archetypes are created lazily and looked up by
//! component signature via `signature_map`. Each archetype is created with an
//! explicit `&ComponentRegistry` rather than calling global registry functions,
//! which enables multi-world support.
//! - **Entity placement** - entity locations `(archetype, chunk, row)` are tracked
//! through [`EntityShards`] and kept consistent with archetype storage.
//! - **Structural mutations** - adding or removing a component migrates the entity's
//! row to a new archetype whose signature reflects the change.
//! - **Deferred commands** - [`ECSData::apply_deferred_commands`] flushes a batch of
//! [`Command`]s (spawn, despawn, add, remove) as a single synchronisation point.
//! - **Parallel iteration** - [`ECSData::for_each_abstraction_unchecked`] and
//! [`ECSData::reduce_abstraction_unchecked`] execute chunk-oriented queries across
//! matching archetypes using Rayon, with per-column RwLock guards enforcing
//! read/write separation. Column locks are acquired in ascending [`ComponentID`]
//! order to prevent deadlocks consistent with the lock-ordering contract documented
//! in `archetype/mod.rs`.
//!
//! # GPU support (`feature = "gpu"`)
//!
//! When the `gpu` feature is enabled, `ECSData` additionally maintains:
//! - A [`DirtyChunks`] tracker that records which component chunks were written
//! during a query, enabling selective CPU->GPU uploads.
//! - A [`GPUResourceRegistry`] for world-owned GPU buffers and bind-group resources.
//!
//! # Safety contract
//!
//! The unchecked iteration methods expose raw byte slices to user callbacks.
//! Callers must guarantee:
//! - No structural mutations occur during iteration.
//! - Component borrow rules (read/write exclusivity) are enforced at the call site.
//! - Callbacks do not allow references to escape the closure.
use std::any::TypeId;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use crate::engine::archetype::Archetype;
use crate::engine::commands::{
Command, CommandEvents, DespawnEvent, SpawnBatch, SpawnEvent, TemplateLifecycleBatch,
};
use crate::engine::component::{ComponentRegistry, Signature};
use crate::engine::entity::{Entity, EntityLocation, EntityShards};
use crate::engine::error::{
AccessKind, AttributeError, ECSError, ECSResult, ExecutionError, InternalViolation, MoveError,
RegistryError, SpawnError, StaleEntityError,
};
use crate::engine::query::BuiltQuery;
use crate::engine::types::{
AgentTemplateId, ArchetypeID, ChunkID, ComponentID, RowID, ShardID, CHUNK_CAP, SIGNATURE_SIZE,
};
#[cfg(feature = "gpu")]
use crate::engine::dirty::DirtyChunks;
#[cfg(feature = "gpu")]
use crate::engine::types::GPUResourceID;
#[cfg(feature = "gpu")]
use crate::gpu::{GPUResource, GPUResourceRegistry, GpuWorldState};
/// Core ECS storage and orchestration structure.
pub struct ECSData {
archetypes: Vec<Archetype>,
signature_map: HashMap<[u64; SIGNATURE_SIZE], ArchetypeID>,
archetype_generation: u64,
query_match_cache: RwLock<HashMap<QueryMatchKey, QueryMatchEntry>>,
shards: EntityShards,
next_spawn_shard: ShardID,
registry: Arc<RwLock<ComponentRegistry>>,
#[cfg(feature = "gpu")]
gpu_dirty_chunks: DirtyChunks,
#[cfg(feature = "gpu")]
gpu_resources: GPUResourceRegistry,
#[cfg(feature = "gpu")]
gpu_world_state: GpuWorldState,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
struct QueryMatchKey {
read: [u64; SIGNATURE_SIZE],
write: [u64; SIGNATURE_SIZE],
without: [u64; SIGNATURE_SIZE],
}
impl QueryMatchKey {
fn from_signature(signature: &crate::engine::query::QuerySignature) -> Self {
Self {
read: signature.read.components,
write: signature.write.components,
without: signature.without.components,
}
}
}
#[derive(Clone, Debug)]
struct QueryMatchEntry {
generation: u64,
/// Shared so cache hits hand out a refcount bump instead of cloning a
/// `Vec` on every query execution.
archetype_ids: Arc<[ArchetypeID]>,
}
/// Result of a deferred command drain that stopped before applying the whole batch.
pub(crate) struct CommandDrainFailure {
/// Error returned by the failed command.
pub(crate) error: ECSError,
/// Commands that were never attempted and must remain queued.
pub(crate) unapplied: Vec<Command>,
/// Lifecycle events produced by commands that completed before the failure.
pub(crate) events: CommandEvents,
}
impl ECSData {
/// Creates a new `ECSData` instance with the specified number of entity shards
/// and component registry.
///
/// # Arguments
/// * `shards` - Configuration for entity sharding (number of shards)
/// * `registry` - Shared component registry for type resolution
pub fn new(shards: EntityShards, registry: Arc<RwLock<ComponentRegistry>>) -> Self {
Self {
archetypes: Vec::new(),
signature_map: HashMap::new(),
archetype_generation: 0,
query_match_cache: RwLock::new(HashMap::new()),
shards,
next_spawn_shard: 0,
registry,
#[cfg(feature = "gpu")]
gpu_dirty_chunks: DirtyChunks::new(),
#[cfg(feature = "gpu")]
gpu_resources: GPUResourceRegistry::new(),
#[cfg(feature = "gpu")]
gpu_world_state: GpuWorldState::new(),
}
}
/// Returns a reference to the component registry.
///
/// The registry is shared via `Arc<RwLock<ComponentRegistry>>`, allowing
/// concurrent reads and exclusive writes for registration of new components.
#[inline]
pub fn registry(&self) -> &Arc<RwLock<ComponentRegistry>> {
&self.registry
}
#[inline]
fn pick_spawn_shard(&mut self) -> ShardID {
let shard = self.next_spawn_shard;
self.next_spawn_shard = (self.next_spawn_shard + 1) % (self.shards.shard_count() as u16);
shard
}
#[cfg(feature = "gpu")]
#[inline]
/// Returns a reference to the dirty chunks tracker.
///
/// The dirty chunks tracker records which component chunks were modified
/// during query execution, enabling selective CPU->GPU uploads. This is
/// used by the GPU synchronisation layer to minimise data transfer
/// between host and device.
pub fn gpu_dirty_chunks(&self) -> &DirtyChunks {
&self.gpu_dirty_chunks
}
#[cfg(feature = "gpu")]
#[inline]
pub(crate) fn gpu_world_state(&self) -> &GpuWorldState {
&self.gpu_world_state
}
#[cfg(feature = "gpu")]
#[inline]
pub(crate) fn gpu_world_state_mut(&mut self) -> &mut GpuWorldState {
&mut self.gpu_world_state
}
#[cfg(feature = "gpu")]
#[inline]
pub(crate) fn gpu_execution_parts(
&mut self,
) -> (
&[Archetype],
&DirtyChunks,
&mut GpuWorldState,
&GPUResourceRegistry,
) {
(
&self.archetypes,
&self.gpu_dirty_chunks,
&mut self.gpu_world_state,
&self.gpu_resources,
)
}
#[cfg(feature = "gpu")]
#[inline]
pub(crate) fn gpu_download_parts(&mut self) -> (&mut [Archetype], &mut GpuWorldState) {
(&mut self.archetypes, &mut self.gpu_world_state)
}
fn get_or_create_archetype(&mut self, signature: &Signature) -> ECSResult<ArchetypeID> {
let key = signature.components;
if let Some(&id) = self.signature_map.get(&key) {
return Ok(id);
}
let id = self.archetypes.len() as ArchetypeID;
self.signature_map.insert(key, id);
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
let arch = Archetype::new(id, *signature, ®istry)?;
self.archetypes.push(arch);
drop(registry);
self.invalidate_query_match_cache();
Ok(id)
}
fn invalidate_query_match_cache(&mut self) {
self.archetype_generation = self.archetype_generation.wrapping_add(1);
if let Ok(cache) = self.query_match_cache.get_mut() {
cache.clear();
}
}
fn matching_archetype_ids(
&self,
query: &crate::engine::query::QuerySignature,
) -> Result<Arc<[ArchetypeID]>, ExecutionError> {
let key = QueryMatchKey::from_signature(query);
let generation = self.archetype_generation;
{
let cache =
self.query_match_cache
.read()
.map_err(|_| ExecutionError::LockPoisoned {
what: "query match cache",
})?;
if let Some(entry) = cache.get(&key) {
if entry.generation == generation {
return Ok(Arc::clone(&entry.archetype_ids));
}
}
}
let mut archetype_ids = Vec::new();
for archetype in &self.archetypes {
if query.requires_all(archetype.signature()) {
archetype_ids.push(archetype.archetype_id());
}
}
let archetype_ids: Arc<[ArchetypeID]> = archetype_ids.into();
let mut cache =
self.query_match_cache
.write()
.map_err(|_| ExecutionError::LockPoisoned {
what: "query match cache",
})?;
cache.insert(
key,
QueryMatchEntry {
generation,
archetype_ids: Arc::clone(&archetype_ids),
},
);
Ok(archetype_ids)
}
#[inline]
fn get_archetype_pair_mut(
archetypes: &mut [Archetype],
a: ArchetypeID,
b: ArchetypeID,
) -> ECSResult<(&mut Archetype, &mut Archetype)> {
if a == b {
return Err(InternalViolation::ArchetypePairSameId.into());
}
let (low, high) = if a < b { (a, b) } else { (b, a) };
let (head, tail) = archetypes.split_at_mut(high as usize);
let left = &mut head[low as usize];
let right = &mut tail[0];
Ok(if a < b { (left, right) } else { (right, left) })
}
/// Adds a component to an existing entity.
///
/// If the component type already exists on the entity, this operation will fail.
/// The entity is moved to a new archetype that includes the added component.
///
/// # Arguments
/// * `entity` - The target entity
/// * `added_component_id` - The `ComponentID` of the component to add
/// * `added_value` - The boxed initial value for the component
///
/// # Errors
/// Returns an error if:
/// * The entity is stale (already despawned)
/// * The component already exists on the entity
/// * The registry lock is poisoned
/// * Archetype creation or component storage allocation fails
pub fn add_component(
&mut self,
entity: Entity,
added_component_id: ComponentID,
added_value: Box<dyn std::any::Any>,
) -> ECSResult<()> {
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
registry.require_component_id(added_component_id)?;
}
let Some(location) = self.shards.get_location(entity)? else {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
};
let source_id = location.archetype;
if self.archetypes[source_id as usize]
.signature()
.try_has(added_component_id)?
{
return Err(MoveError::ComponentAlreadyPresent {
component_id: added_component_id,
}
.into());
}
let mut new_signature = *self.archetypes[source_id as usize].signature();
new_signature.try_set(added_component_id)?;
let destination_id = self.get_or_create_archetype(&new_signature)?;
let source_sig = *self.archetypes[source_id as usize].signature();
let shards = &self.shards;
let (source, destination) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
let factory = || registry.make_empty_component(added_component_id);
destination
.ensure_component(added_component_id, factory)
.map_err(ECSError::from)?;
Self::ensure_shared_components(&source_sig, destination, added_component_id, ®istry)?;
drop(registry);
source.move_row_to_archetype(
destination,
shards,
entity,
(location.chunk, location.row),
vec![(added_component_id, added_value)],
)?;
Ok(())
}
/// Removes a component from an existing entity.
///
/// The entity is moved to a new archetype that excludes the removed component.
/// If the entity has no components remaining after removal, it is despawned.
///
/// # Arguments
/// * `entity` - The target entity
/// * `removed_component_id` - The `ComponentID` of the component to remove
///
/// # Returns
/// Returns `Ok(())` if:
/// * The component was successfully removed
/// * The entity didn't have the component (no-op)
/// * The entity was despawned due to having no components left
///
/// # Errors
/// Returns an error if:
/// * The entity is stale (already despawned)
/// * The registry lock is poisoned
/// * Archetype creation or entity movement fails
pub fn remove_component(
&mut self,
entity: Entity,
removed_component_id: ComponentID,
) -> ECSResult<()> {
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
registry.require_component_id(removed_component_id)?;
}
let Some(location) = self.shards.get_location(entity)? else {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
};
let source_id = location.archetype;
if !self.archetypes[source_id as usize]
.signature()
.try_has(removed_component_id)?
{
return Ok(());
}
let mut new_signature = *self.archetypes[source_id as usize].signature();
new_signature.try_clear(removed_component_id)?;
if new_signature.components.iter().all(|&bits| bits == 0) {
self.archetypes[source_id as usize].despawn_on(&self.shards, entity)?;
return Ok(());
}
let destination_id = self.get_or_create_archetype(&new_signature)?;
let source_sig = *self.archetypes[source_id as usize].signature();
let shards = &self.shards;
let (source_arch, dest_arch) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
Self::ensure_shared_components(&source_sig, dest_arch, removed_component_id, ®istry)?;
drop(registry);
source_arch.move_row_to_archetype(
dest_arch,
shards,
entity,
(location.chunk, location.row),
Vec::new(),
)?;
Ok(())
}
/// Writes a new component value directly into the entity's current archetype
/// row, **without** any archetype transition.
///
/// The old value stored at the target slot is dropped correctly using the
/// stored component's real drop glue, so this path is sound for any
/// registered component type regardless of whether it implements `Copy`.
///
/// ## Errors
///
/// - [`ExecutionError::MissingComponent`] - the entity's archetype does not
/// contain `component_id`.
/// - [`ExecutionError::InternalExecutionError`] - the dynamic `TypeId` of
/// `value` does not match the column's registered element type, or the
/// entity's row is out of range.
/// - [`SpawnError::StaleEntity`] - the entity no longer exists.
/// - [`ExecutionError::LockPoisoned`] - the component column lock is poisoned.
///
/// ## Safety / exclusivity
///
/// Must only be called while holding the exclusive phase lock
/// (`PhaseWrite`), which is the invariant of `apply_deferred_commands`.
pub(crate) fn apply_set_command(
&mut self,
entity: Entity,
component_id: ComponentID,
value: Box<dyn std::any::Any + Send>,
) -> ECSResult<()> {
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
registry.require_component_id(component_id)?;
}
// 1. Resolve entity location.
let location = self
.shards
.get_location(entity)?
.ok_or(ECSError::from(SpawnError::StaleEntity(StaleEntityError)))?;
// 2. Verify archetype contains the component.
let archetype = self
.archetypes
.get(location.archetype as usize)
.ok_or(ECSError::from(ExecutionError::InternalExecutionError))?;
if !archetype.has(component_id) {
return Err(ECSError::from(ExecutionError::MissingComponent {
component_id,
}));
}
// 3. Acquire an exclusive write lock on the column.
let col_lock = archetype
.component_locked(component_id)
.ok_or(ECSError::from(ExecutionError::MissingComponent {
component_id,
}))?;
let mut col_guard = col_lock.write().map_err(|_| {
ECSError::from(ExecutionError::LockPoisoned {
what: "component column (set)",
})
})?;
// 4. Delegate the actual replace to the storage layer, which:
// - type-checks the incoming value against the column,
// - drops the old value in place using its real drop glue,
// - consumes the incoming box cleanly (no leak, no double-free).
//
// `Box<dyn Any + Send>` coerces freely to `Box<dyn Any>`; the `Send`
// bound is not needed at the apply site because we hold the exclusive
// phase lock.
let value: Box<dyn std::any::Any> = value;
match col_guard.replace_slot_dyn(location.chunk, location.row, value) {
Ok(()) => Ok(()),
Err(AttributeError::TypeMismatch(_)) => {
Err(ECSError::from(ExecutionError::InternalExecutionError))
}
Err(AttributeError::Position(_)) => {
Err(ECSError::from(ExecutionError::InternalExecutionError))
}
Err(other) => Err(ECSError::from(other)),
}
}
/// Applies all queued deferred commands.
/// `Spawn`, `Despawn`, `Add`, and `Remove` variants.
pub fn apply_deferred_commands(&mut self, commands: Vec<Command>) -> ECSResult<CommandEvents> {
self.apply_deferred_commands_partial(commands)
.map_err(|failure| failure.error)
}
/// Applies deferred commands until completion or the first error.
///
/// On error, commands that were never attempted are returned so the manager
/// can preserve their relative order in the deferred queue.
#[allow(clippy::result_large_err)]
pub(crate) fn apply_deferred_commands_partial(
&mut self,
commands: Vec<Command>,
) -> Result<CommandEvents, CommandDrainFailure> {
let mut events = CommandEvents::default();
#[cfg(feature = "gpu")]
let mut touched: std::collections::BTreeSet<ArchetypeID> =
std::collections::BTreeSet::new();
let mut iter = commands.into_iter();
while let Some(command) = iter.next() {
let result = match command {
Command::Spawn { bundle } => {
let signature = bundle.signature();
let archetype_id = match self.get_or_create_archetype(&signature) {
Ok(id) => id,
Err(error) => {
#[cfg(feature = "gpu")]
self.notify_touched_archetypes(&touched);
return Err(CommandDrainFailure {
error,
unapplied: iter.collect(),
events,
});
}
};
#[cfg(feature = "gpu")]
touched.insert(archetype_id);
let shard_id = self.pick_spawn_shard();
let archetype = &mut self.archetypes[archetype_id as usize];
archetype
.spawn_on(&self.shards, shard_id, bundle)
.map(|entity| {
events.spawned.push(SpawnEvent::untagged(entity));
})
}
Command::SpawnTagged { bundle, tag } => {
let signature = bundle.signature();
let archetype_id = match self.get_or_create_archetype(&signature) {
Ok(id) => id,
Err(error) => {
#[cfg(feature = "gpu")]
self.notify_touched_archetypes(&touched);
return Err(CommandDrainFailure {
error,
unapplied: iter.collect(),
events,
});
}
};
#[cfg(feature = "gpu")]
touched.insert(archetype_id);
let shard_id = self.pick_spawn_shard();
let archetype = &mut self.archetypes[archetype_id as usize];
archetype
.spawn_on(&self.shards, shard_id, bundle)
.map(|entity| {
events.spawned.push(SpawnEvent::tagged(entity, tag));
})
}
Command::SpawnBatchTagged { batch, template_id } => {
match self.apply_spawn_batch(batch, template_id, &mut events) {
Ok(_archetype_id) => {
#[cfg(feature = "gpu")]
touched.insert(_archetype_id);
Ok(())
}
Err(error) => Err(error),
}
}
Command::Despawn { entity } => {
let loc = self
.shards
.get_location(entity)
.map_err(ECSError::from)
.and_then(|loc| {
loc.ok_or(ECSError::from(SpawnError::StaleEntity(StaleEntityError)))
});
match loc {
Ok(loc) => {
#[cfg(feature = "gpu")]
touched.insert(loc.archetype);
let archetype = &mut self.archetypes[loc.archetype as usize];
archetype.despawn_on(&self.shards, entity).map(|()| {
events.despawned.push(DespawnEvent::untagged(entity));
})
}
Err(error) => Err(error),
}
}
Command::DespawnTagged { entity, tag } => {
let loc = self
.shards
.get_location(entity)
.map_err(ECSError::from)
.and_then(|loc| {
loc.ok_or(ECSError::from(SpawnError::StaleEntity(StaleEntityError)))
});
match loc {
Ok(loc) => {
#[cfg(feature = "gpu")]
touched.insert(loc.archetype);
let archetype = &mut self.archetypes[loc.archetype as usize];
archetype.despawn_on(&self.shards, entity).map(|()| {
events.despawned.push(DespawnEvent::tagged(entity, tag));
})
}
Err(error) => Err(error),
}
}
Command::DespawnBatchTagged {
entities,
template_id,
} => match self.apply_despawn_batch(entities, template_id, &mut events) {
Ok(_archetype_ids) => {
#[cfg(feature = "gpu")]
touched.extend(_archetype_ids);
Ok(())
}
Err(error) => Err(error),
},
Command::Add {
entity,
component_id,
value,
} => {
#[cfg(feature = "gpu")]
self.touch_entity_archetype(entity, &mut touched);
let result = self.add_component(entity, component_id, value);
#[cfg(feature = "gpu")]
if result.is_ok() {
self.touch_entity_archetype(entity, &mut touched);
}
result
}
Command::Remove {
entity,
component_id,
} => {
#[cfg(feature = "gpu")]
self.touch_entity_archetype(entity, &mut touched);
let result = self.remove_component(entity, component_id);
#[cfg(feature = "gpu")]
if result.is_ok() {
self.touch_entity_archetype(entity, &mut touched);
}
result
}
Command::AddComponentBatch {
entities,
component_id,
values,
len,
} => match self.apply_add_component_batch(entities, component_id, values, len) {
Ok(_touched) => {
#[cfg(feature = "gpu")]
touched.extend(_touched);
Ok(())
}
Err(error) => Err(error),
},
Command::RemoveComponentBatch {
entities,
component_id,
} => match self.apply_remove_component_batch(entities, component_id) {
Ok(_touched) => {
#[cfg(feature = "gpu")]
touched.extend(_touched);
Ok(())
}
Err(error) => Err(error),
},
Command::Set {
entity,
component_id,
value,
} => {
#[cfg(feature = "gpu")]
self.touch_entity_archetype(entity, &mut touched);
self.apply_set_command(entity, component_id, value)
}
};
if let Err(error) = result {
#[cfg(feature = "gpu")]
self.notify_touched_archetypes(&touched);
return Err(CommandDrainFailure {
error,
unapplied: iter.collect(),
events,
});
}
}
#[cfg(feature = "gpu")]
self.notify_touched_archetypes(&touched);
Ok(events)
}
/// Applies one columnar spawn batch: bulk column appends, bulk entity
/// allocation, and a single metadata commit. Returns the archetype the
/// batch spawned into.
///
/// ## Failure semantics
/// The batch is atomic: on any error, appended column data is truncated
/// back to the pre-batch length and any allocated entity handles are
/// despawned, leaving the world as if the command had never run.
fn apply_spawn_batch(
&mut self,
batch: SpawnBatch,
template_id: AgentTemplateId,
events: &mut CommandEvents,
) -> ECSResult<ArchetypeID> {
let count = batch.count;
let archetype_id = self.get_or_create_archetype(&batch.signature)?;
if count == 0 {
events.spawned_batches.push(TemplateLifecycleBatch {
template_id,
entities: Vec::new(),
});
return Ok(archetype_id);
}
let start = self.archetypes[archetype_id as usize].length()?;
self.archetypes[archetype_id as usize].reserve_additional_rows(count)?;
let column_ids: Vec<ComponentID> = batch
.columns
.iter()
.map(|column| column.component_id)
.collect();
self.archetypes[archetype_id as usize].append_batch_columns(start, count, batch.columns)?;
// Every location below is addressable: `append_batch_columns`
// validated `start + count - 1` against ChunkID/RowID limits.
let entities = match self.shards.spawn_batch(count, |k| {
let index = start + k;
EntityLocation {
archetype: archetype_id,
chunk: (index / CHUNK_CAP) as ChunkID,
row: (index % CHUNK_CAP) as RowID,
}
}) {
Ok(entities) => entities,
Err(error) => {
self.archetypes[archetype_id as usize].truncate_columns_to(&column_ids, start);
return Err(error.into());
}
};
if let Err(error) =
self.archetypes[archetype_id as usize].commit_batch_rows(start, &entities)
{
self.archetypes[archetype_id as usize].truncate_columns_to(&column_ids, start);
for entity in entities {
let _ = self.shards.despawn(entity);
}
return Err(error);
}
events.spawned.reserve(entities.len());
for &entity in &entities {
events
.spawned
.push(SpawnEvent::template_tagged(entity, template_id));
}
events.spawned_batches.push(TemplateLifecycleBatch {
template_id,
entities,
});
Ok(archetype_id)
}
/// Applies one batched despawn. Returns the archetypes it touched.
///
/// ## Failure semantics
/// Every handle is preflighted: stale or duplicate entities fail the
/// whole command *before* any despawn happens (the per-entity `Despawn`
/// command retains its fail-midway semantics).
fn apply_despawn_batch(
&mut self,
entities: Vec<Entity>,
template_id: AgentTemplateId,
events: &mut CommandEvents,
) -> ECSResult<Vec<ArchetypeID>> {
let mut resolved: Vec<(Entity, EntityLocation)> = Vec::with_capacity(entities.len());
for &entity in &entities {
let Some(location) = self.shards.get_location(entity)? else {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
};
resolved.push((entity, location));
}
let mut by_archetype: HashMap<ArchetypeID, Vec<(Entity, ChunkID, RowID)>> = HashMap::new();
for (entity, location) in resolved {
by_archetype.entry(location.archetype).or_default().push((
entity,
location.chunk,
location.row,
));
}
let mut archetype_ids: Vec<ArchetypeID> = by_archetype.keys().copied().collect();
archetype_ids.sort_unstable();
for &archetype_id in &archetype_ids {
let Some(mut targets) = by_archetype.remove(&archetype_id) else {
continue;
};
// Descending linear index: each swap-remove's moved row (the
// current last) can never itself be a pending target, so the
// locations resolved above stay valid throughout the batch.
targets.sort_by_key(|&(_, chunk, row)| {
std::cmp::Reverse(chunk as usize * CHUNK_CAP + row as usize)
});
for pair in targets.windows(2) {
if (pair[0].1, pair[0].2) == (pair[1].1, pair[1].2) {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
}
}
self.archetypes[archetype_id as usize].despawn_rows_batch(&self.shards, &targets)?;
}
events.despawned.reserve(entities.len());
for &entity in &entities {
events
.despawned
.push(DespawnEvent::template_tagged(entity, template_id));
}
events.despawned_batches.push(TemplateLifecycleBatch {
template_id,
entities,
});
Ok(archetype_ids)
}
/// Applies one columnar add-component batch. Returns the touched
/// archetypes (source, destination) for GPU dirty tracking.
///
/// ## Failure semantics
/// Fully atomic: stale/duplicate handles, archetype mismatches,
/// already-present components, and length mismatches are all rejected in
/// preflight; storage failures roll back via the copy-then-commit
/// transaction in [`Archetype::migrate_rows_batch`].
fn apply_add_component_batch(
&mut self,
entities: Vec<Entity>,
component_id: ComponentID,
values: Box<dyn std::any::Any + Send>,
len: usize,
) -> ECSResult<Vec<ArchetypeID>> {
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
registry.require_component_id(component_id)?;
}
if len != entities.len() {
return Err(SpawnError::BatchColumnMismatch {
component_id,
expected: entities.len(),
actual: len,
}
.into());
}
if entities.is_empty() {
return Ok(Vec::new());
}
// Preflight: one shared source archetype, all live, none already
// carrying the component.
let mut resolved: Vec<(usize, Entity, EntityLocation)> = Vec::with_capacity(entities.len());
let mut source_id: Option<ArchetypeID> = None;
for (input_index, &entity) in entities.iter().enumerate() {
let Some(location) = self.shards.get_location(entity)? else {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
};
match source_id {
None => source_id = Some(location.archetype),
Some(expected) if expected != location.archetype => {
return Err(MoveError::BatchSpansArchetypes {
expected,
got: location.archetype,
}
.into());
}
Some(_) => {}
}
resolved.push((input_index, entity, location));
}
let source_id = source_id.expect("non-empty batch has a source archetype");
if self.archetypes[source_id as usize]
.signature()
.try_has(component_id)?
{
return Err(MoveError::ComponentAlreadyPresent { component_id }.into());
}
// Destination archetype: source signature plus the new component.
let mut new_signature = *self.archetypes[source_id as usize].signature();
new_signature.try_set(component_id)?;
let destination_id = self.get_or_create_archetype(&new_signature)?;
let source_signature = *self.archetypes[source_id as usize].signature();
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
let (_, destination) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
let factory = || registry.make_empty_component(component_id);
destination
.ensure_component(component_id, factory)
.map_err(ECSError::from)?;
Self::ensure_shared_components(
&source_signature,
destination,
component_id,
®istry,
)?;
}
// Descending linear row order; duplicates become adjacent equals.
resolved.sort_by_key(|&(_, _, location)| {
std::cmp::Reverse(location.chunk as usize * CHUNK_CAP + location.row as usize)
});
for pair in resolved.windows(2) {
if (pair[0].2.chunk, pair[0].2.row) == (pair[1].2.chunk, pair[1].2.row) {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
}
}
let order: Vec<usize> = resolved
.iter()
.map(|&(input_index, _, _)| input_index)
.collect();
let targets: Vec<(Entity, ChunkID, RowID)> = resolved
.iter()
.map(|&(_, entity, location)| (entity, location.chunk, location.row))
.collect();
let shards = &self.shards;
let (source, destination) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
source.migrate_rows_batch(
destination,
shards,
&targets,
Some((component_id, values, order)),
None,
)?;
Ok(vec![source_id, destination_id])
}
/// Applies one columnar remove-component batch. Entities may span
/// archetypes (each group migrates atomically, groups in ascending
/// archetype order); entities without the component are skipped to match
/// the per-entity `Remove` no-op. Returns the touched archetypes.
fn apply_remove_component_batch(
&mut self,
entities: Vec<Entity>,
component_id: ComponentID,
) -> ECSResult<Vec<ArchetypeID>> {
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
registry.require_component_id(component_id)?;
}
if entities.is_empty() {
return Ok(Vec::new());
}
// Preflight: resolve, drop non-carriers, group by archetype.
let mut by_archetype: HashMap<ArchetypeID, Vec<(Entity, ChunkID, RowID)>> = HashMap::new();
for &entity in &entities {
let Some(location) = self.shards.get_location(entity)? else {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
};
if !self.archetypes[location.archetype as usize]
.signature()
.try_has(component_id)?
{
continue;
}
by_archetype.entry(location.archetype).or_default().push((
entity,
location.chunk,
location.row,
));
}
let mut archetype_ids: Vec<ArchetypeID> = by_archetype.keys().copied().collect();
archetype_ids.sort_unstable();
let mut touched: Vec<ArchetypeID> = Vec::new();
for &source_id in &archetype_ids {
let Some(mut targets) = by_archetype.remove(&source_id) else {
continue;
};
targets.sort_by_key(|&(_, chunk, row)| {
std::cmp::Reverse(chunk as usize * CHUNK_CAP + row as usize)
});
for pair in targets.windows(2) {
if (pair[0].1, pair[0].2) == (pair[1].1, pair[1].2) {
return Err(SpawnError::StaleEntity(StaleEntityError).into());
}
}
let mut new_signature = *self.archetypes[source_id as usize].signature();
new_signature.try_clear(component_id)?;
if new_signature.components.iter().all(|&bits| bits == 0) {
return Err(MoveError::BatchWouldEmptyEntity { component_id }.into());
}
let destination_id = self.get_or_create_archetype(&new_signature)?;
let source_signature = *self.archetypes[source_id as usize].signature();
{
let registry = self
.registry
.read()
.map_err(|_| ECSError::from(RegistryError::PoisonedLock))?;
let (_, destination) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
Self::ensure_shared_components(
&source_signature,
destination,
component_id,
®istry,
)?;
}
let shards = &self.shards;
let (source, destination) =
Self::get_archetype_pair_mut(&mut self.archetypes, source_id, destination_id)?;
source.migrate_rows_batch(destination, shards, &targets, None, Some(component_id))?;
touched.push(source_id);
touched.push(destination_id);
}
Ok(touched)
}
/// Records the archetype currently holding `entity` in the GPU dirty set.
#[cfg(feature = "gpu")]
fn touch_entity_archetype(
&self,
entity: Entity,
touched: &mut std::collections::BTreeSet<ArchetypeID>,
) {
if let Ok(Some(location)) = self.shards.get_location(entity) {
touched.insert(location.archetype);
}
}
/// Marks every chunk of each touched archetype dirty for GPU re-upload.
///
/// Replaces the previous blanket invalidation of *all* archetypes after
/// any structural change: only archetypes a command actually touched are
/// re-uploaded at the next GPU sync.
#[cfg(feature = "gpu")]
fn notify_touched_archetypes(&self, touched: &std::collections::BTreeSet<ArchetypeID>) {
for &archetype_id in touched {
self.gpu_dirty_chunks.notify_archetype_changed(archetype_id);
}
}
fn ensure_shared_components(
source_sig: &Signature,
destination: &mut Archetype,
excluded: ComponentID,
registry: &ComponentRegistry,
) -> ECSResult<()> {
for cid in source_sig.iterate_over_components() {
if cid == excluded {
continue;
}
let factory = || registry.make_empty_component(cid);
destination
.ensure_component(cid, factory)
.map_err(ECSError::from)?;
}
Ok(())
}
/// Reads a component value from a specific entity, cloning it.
///
/// This method provides random-access read of a single entity's component.
/// For bulk operations, prefer using queries with `for_each` or `reduce`.
///
/// # Type Parameters
/// * `T` - The expected component type. Must match the type registered for
/// `component_id`.
///
/// # Arguments
/// * `entity` - The target entity
/// * `component_id` - The `ComponentID` of the component to read
///
/// # Returns
/// Returns a cloned copy of the component value.
///
/// # Errors
/// Returns an error if:
/// * The entity is stale (already despawned)
/// * The archetype or component column is missing
/// * The component type `T` doesn't match the actual stored type
/// * The component is currently borrowed for writing
/// * The column lock is poisoned
pub fn read_component<T: 'static + Clone>(
&self,
entity: Entity,
component_id: ComponentID,
) -> ECSResult<T> {
let loc = self
.shards
.get_location(entity)?
.ok_or(ECSError::from(SpawnError::StaleEntity(StaleEntityError)))?;
let arch = self
.archetypes
.get(loc.archetype as usize)
.ok_or(ECSError::from(ExecutionError::InternalExecutionError))?;
let col_lock = arch.component_locked(component_id).ok_or(ECSError::from(
ExecutionError::MissingComponent { component_id },
))?;
let col = col_lock.try_read().map_err(|e| match e {
std::sync::TryLockError::WouldBlock => ECSError::from(ExecutionError::BorrowConflict {
component_id,
held: AccessKind::Write,
requested: AccessKind::Read,
}),
std::sync::TryLockError::Poisoned(_) => ECSError::from(ExecutionError::LockPoisoned {
what: "component column",
}),
})?;
if col.element_type_id() != TypeId::of::<T>() {
return Err(ECSError::from(ExecutionError::InternalExecutionError));
}
let chunk_len = arch.chunk_valid_length(loc.chunk as usize)?;
let (ptr, bytes) = col
.chunk_bytes(loc.chunk, chunk_len)
.ok_or(ECSError::from(ExecutionError::InternalExecutionError))?;
let slice: &[T] = unsafe { crate::engine::storage::cast_slice::<T>(ptr, bytes) };
let row = loc.row as usize;
if row >= slice.len() {
return Err(ECSError::from(ExecutionError::InternalExecutionError));
}
Ok(slice[row].clone())
}
pub(crate) fn for_each_abstraction_unchecked(
&self,
query: BuiltQuery,
f: impl Fn(&[&[u8]], &mut [&mut [u8]]) + Send + Sync,
) -> Result<(), ExecutionError> {
let matches = self.matching_archetype_ids(query.signature())?;
#[cfg(feature = "gpu")]
{
super::query_executor::for_each_unchecked(
&self.archetypes,
&matches,
query,
&self.gpu_dirty_chunks,
crate::engine::activation::current_activation_context(),
f,
)
}
#[cfg(not(feature = "gpu"))]
{
super::query_executor::for_each_unchecked(
&self.archetypes,
&matches,
query,
crate::engine::activation::current_activation_context(),
f,
)
}
}
pub(crate) fn for_each_entity_abstraction_unchecked(
&self,
query: BuiltQuery,
f: impl Fn(&[Entity], &[&[u8]], &mut [&mut [u8]]) + Send + Sync,
) -> Result<(), ExecutionError> {
let matches = self.matching_archetype_ids(query.signature())?;
#[cfg(feature = "gpu")]
{
super::query_executor::for_each_entity_unchecked(
&self.archetypes,
&matches,
query,
&self.gpu_dirty_chunks,
crate::engine::activation::current_activation_context(),
f,
)
}
#[cfg(not(feature = "gpu"))]
{
super::query_executor::for_each_entity_unchecked(
&self.archetypes,
&matches,
query,
crate::engine::activation::current_activation_context(),
f,
)
}
}
/// Parallel chunk iteration whose closure may return an error.
///
/// Identical contract to [`for_each_abstraction_unchecked`] for borrow,
/// phase, and aliasing safety, but the user closure returns
/// [`ECSResult<()>`]. If any chunk's closure returns `Err`, every other
/// chunk in the same archetype short-circuits and the lowest-chunk-index
/// error is returned. The selected error is therefore deterministic in
/// the chunk identifier rather than wall-clock first-to-error, so
/// repeated runs over identical data produce identical error reports.
pub(crate) fn for_each_abstraction_fallible_unchecked(
&self,
query: BuiltQuery,
f: impl Fn(&[&[u8]], &mut [&mut [u8]]) -> crate::engine::error::ECSResult<()> + Send + Sync,
) -> crate::engine::error::ECSResult<()> {
let matches = self
.matching_archetype_ids(query.signature())
.map_err(ECSError::from)?;
#[cfg(feature = "gpu")]
{
super::query_executor::for_each_fallible_unchecked(
&self.archetypes,
&matches,
query,
&self.gpu_dirty_chunks,
crate::engine::activation::current_activation_context(),
f,
)
}
#[cfg(not(feature = "gpu"))]
{
super::query_executor::for_each_fallible_unchecked(
&self.archetypes,
&matches,
query,
crate::engine::activation::current_activation_context(),
f,
)
}
}
pub(crate) fn for_each_entity_abstraction_fallible_unchecked(
&self,
query: BuiltQuery,
f: impl Fn(&[Entity], &[&[u8]], &mut [&mut [u8]]) -> crate::engine::error::ECSResult<()>
+ Send
+ Sync,
) -> crate::engine::error::ECSResult<()> {
let matches = self
.matching_archetype_ids(query.signature())
.map_err(ECSError::from)?;
#[cfg(feature = "gpu")]
{
super::query_executor::for_each_entity_fallible_unchecked(
&self.archetypes,
&matches,
query,
&self.gpu_dirty_chunks,
crate::engine::activation::current_activation_context(),
f,
)
}
#[cfg(not(feature = "gpu"))]
{
super::query_executor::for_each_entity_fallible_unchecked(
&self.archetypes,
&matches,
query,
crate::engine::activation::current_activation_context(),
f,
)
}
}
pub(crate) fn reduce_abstraction_unchecked<R>(
&self,
query: BuiltQuery,
init: impl Fn() -> R + Send + Sync,
fold_chunk: impl Fn(&mut R, &[&[u8]], usize) + Send + Sync,
combine: impl Fn(&mut R, R) + Send + Sync,
) -> Result<R, ExecutionError>
where
R: Send + 'static,
{
let matches = self.matching_archetype_ids(query.signature())?;
super::query_executor::reduce_unchecked(
&self.archetypes,
&matches,
query,
init,
fold_chunk,
combine,
)
}
#[cfg(feature = "gpu")]
/// Registers a GPU resource with the ECS world.
///
/// GPU resources are world-owned buffers, textures, or bind groups that
/// persist across frames. Once registered, the resource is managed by the
/// ECS and can be accessed from GPU systems.
///
/// # Type Parameters
/// * `R` - The GPU resource type, which must implement [`GPUResource`].
///
/// # Arguments
/// * `r` - The resource instance to register.
///
/// # Returns
/// A [`GPUResourceID`] that can be used to reference this resource in
/// GPU systems and queries.
///
/// # Example
/// ```text
/// let buffer = world.create_buffer(&device, &descriptor);
/// let resource_id = ecs_data.register_gpu_resource(buffer);
/// ```
pub fn register_gpu_resource<R: GPUResource + 'static>(
&mut self,
r: R,
) -> ECSResult<GPUResourceID> {
self.gpu_resources.register(r)
}
#[cfg(feature = "gpu")]
#[inline]
/// Returns an immutable reference to the GPU resource registry.
///
/// The registry holds all world-owned GPU resources (buffers, textures,
/// bind groups) and provides lookup by [`GPUResourceID`]. This is used
/// by GPU systems to access resources during execution.
pub fn gpu_resources(&self) -> &GPUResourceRegistry {
&self.gpu_resources
}
#[cfg(feature = "gpu")]
#[inline]
/// Returns a mutable reference to the GPU resource registry.
///
/// Allows adding, removing, or modifying GPU resources owned by the world.
/// Used during resource creation, clean-up, or when updating resource contents.
///
/// # Safety
/// Callers must ensure that no GPU systems are currently executing when
/// mutating the registry, as this may invalidate active resource bindings.
pub fn gpu_resources_mut(&mut self) -> &mut GPUResourceRegistry {
&mut self.gpu_resources
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::component::Bundle;
use crate::engine::entity::EntityShards;
use std::sync::{Arc, RwLock};
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct Marker(u32);
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct Extra(u32);
fn test_manager() -> (crate::engine::manager::ECSManager, ComponentID, ComponentID) {
let registry = Arc::new(RwLock::new(ComponentRegistry::new()));
let (marker_id, extra_id) = {
let mut registry = registry.write().unwrap();
let marker_id = registry.register::<Marker>().unwrap();
let extra_id = registry.register::<Extra>().unwrap();
registry.freeze();
(marker_id, extra_id)
};
(
crate::engine::manager::ECSManager::with_registry(
EntityShards::new(1).unwrap(),
registry,
),
marker_id,
extra_id,
)
}
fn marker_bundle(marker_id: ComponentID, value: u32) -> Bundle {
let mut bundle = Bundle::new();
bundle.insert(marker_id, Marker(value));
bundle
}
fn spawn_marker(
ecs: &crate::engine::manager::ECSManager,
marker_id: ComponentID,
value: u32,
) -> Entity {
let world = ecs.world_ref();
world
.defer(Command::Spawn {
bundle: marker_bundle(marker_id, value),
})
.unwrap();
ecs.apply_deferred_commands().unwrap().spawned[0].entity
}
fn assert_all_columns_empty(data: &ECSData) {
for archetype in &data.archetypes {
assert_eq!(archetype.length().unwrap(), 0);
for (_, len) in archetype.component_lengths_for_test() {
assert_eq!(len, 0);
}
}
}
fn marker_signature(marker_id: ComponentID) -> Signature {
let mut signature = Signature::default();
signature.set(marker_id);
signature
}
#[test]
fn spawn_batch_column_length_mismatch_rolls_back_cleanly() {
use crate::engine::commands::{BatchColumn, SpawnBatch};
let (ecs, marker_id, _extra_id) = test_manager();
let world = ecs.world_ref();
// Declared len disagrees with the batch count.
world
.defer(Command::SpawnBatchTagged {
batch: SpawnBatch {
count: 4,
signature: marker_signature(marker_id),
columns: vec![BatchColumn {
component_id: marker_id,
values: Box::new(vec![Marker(1), Marker(2), Marker(3)]),
len: 3,
}],
},
template_id: crate::AgentTemplateId(7),
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Spawn(
crate::engine::error::SpawnError::BatchColumnMismatch { .. }
))
));
// Declared len matches the count but the payload is short: the append
// itself must detect the shortfall and truncate back.
world
.defer(Command::SpawnBatchTagged {
batch: SpawnBatch {
count: 4,
signature: marker_signature(marker_id),
columns: vec![BatchColumn {
component_id: marker_id,
values: Box::new(vec![Marker(1), Marker(2)]),
len: 4,
}],
},
template_id: crate::AgentTemplateId(7),
})
.unwrap();
assert!(ecs.apply_deferred_commands().is_err());
world
.with_exclusive(|data| {
assert_all_columns_empty(data);
Ok(())
})
.unwrap();
}
#[test]
fn spawn_batch_rows_are_visible_and_row_aligned() {
use crate::engine::commands::{BatchColumn, SpawnBatch};
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
let n = 40_000u32; // spans three chunks
let mut signature = Signature::default();
signature.set(marker_id);
signature.set(extra_id);
let markers: Vec<Marker> = (0..n).map(Marker).collect();
let extras: Vec<Extra> = (0..n).map(|i| Extra(i * 2)).collect();
world
.defer(Command::SpawnBatchTagged {
batch: SpawnBatch {
count: n as usize,
signature,
columns: vec![
BatchColumn {
component_id: marker_id,
values: Box::new(markers),
len: n as usize,
},
BatchColumn {
component_id: extra_id,
values: Box::new(extras),
len: n as usize,
},
],
},
template_id: crate::AgentTemplateId(1),
})
.unwrap();
let events = ecs.apply_deferred_commands().unwrap();
assert_eq!(events.spawned.len(), n as usize);
assert_eq!(events.spawned_batches[0].entities.len(), n as usize);
// Row alignment: both columns of every row agree, and all rows exist.
let query = world
.query()
.unwrap()
.read::<Marker>()
.unwrap()
.read::<Extra>()
.unwrap()
.build()
.unwrap();
let count = std::sync::atomic::AtomicUsize::new(0);
let misaligned = std::sync::atomic::AtomicUsize::new(0);
world
.for_each_r2(query, |marker: &Marker, extra: &Extra| {
count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if extra.0 != marker.0 * 2 {
misaligned.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
})
.unwrap();
assert_eq!(count.load(std::sync::atomic::Ordering::Relaxed), n as usize);
assert_eq!(misaligned.load(std::sync::atomic::Ordering::Relaxed), 0);
// Entity handles resolve to the values they were spawned with.
for (k, &entity) in events.spawned_batches[0]
.entities
.iter()
.enumerate()
.step_by(7919)
{
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
assert_eq!(marker.0 as usize, k);
}
}
#[test]
fn despawn_batch_removes_interleaved_targets_atomically() {
let (ecs, marker_id, _extra_id) = test_manager();
let world = ecs.world_ref();
let mut entities = Vec::new();
for i in 0..100 {
entities.push(spawn_marker(&ecs, marker_id, i));
}
// Every third entity, unordered rows interleaved with survivors.
let targets: Vec<_> = entities.iter().copied().step_by(3).collect();
let survivors: Vec<(crate::engine::entity::Entity, u32)> = entities
.iter()
.copied()
.enumerate()
.filter(|(i, _)| i % 3 != 0)
.map(|(i, e)| (e, i as u32))
.collect();
world
.defer(Command::DespawnBatchTagged {
entities: targets.clone(),
template_id: crate::AgentTemplateId(2),
})
.unwrap();
let events = ecs.apply_deferred_commands().unwrap();
assert_eq!(events.despawned.len(), targets.len());
assert_eq!(events.despawned_batches[0].entities, targets);
// Survivors keep their values through the swap-remove churn.
for &(entity, expected) in &survivors {
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
assert_eq!(marker.0, expected);
}
assert_eq!(count_markers_in(&ecs), survivors.len());
// A batch containing a duplicate handle fails before despawning
// anything (atomic preflight).
let dup_target = survivors[0].0;
world
.defer(Command::DespawnBatchTagged {
entities: vec![dup_target, dup_target],
template_id: crate::AgentTemplateId(2),
})
.unwrap();
assert!(ecs.apply_deferred_commands().is_err());
assert_eq!(count_markers_in(&ecs), survivors.len());
let marker: Marker = world.read_entity_component(dup_target, marker_id).unwrap();
assert_eq!(marker.0, survivors[0].1);
}
fn count_markers_in(ecs: &crate::engine::manager::ECSManager) -> usize {
let world = ecs.world_ref();
let query = world
.query()
.unwrap()
.read::<Marker>()
.unwrap()
.build()
.unwrap();
let count = std::sync::atomic::AtomicUsize::new(0);
world
.for_each_r1(query, |_: &Marker| {
count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
})
.unwrap();
count.load(std::sync::atomic::Ordering::Relaxed)
}
#[test]
fn add_component_batch_maps_values_to_entities() {
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
let n = 10_000u32;
let mut entities = Vec::with_capacity(n as usize);
for i in 0..n {
entities.push(spawn_marker(&ecs, marker_id, i));
}
// Values in input order; processing order is per-archetype descending,
// so this exercises the permutation path end to end.
let values: Vec<Extra> = (0..n).map(|i| Extra(i * 3)).collect();
world
.defer(Command::AddComponentBatch {
entities: entities.clone(),
component_id: extra_id,
values: Box::new(values),
len: n as usize,
})
.unwrap();
ecs.apply_deferred_commands().unwrap();
for (k, &entity) in entities.iter().enumerate().step_by(997) {
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
let extra: Extra = world.read_entity_component(entity, extra_id).unwrap();
assert_eq!(marker.0 as usize, k, "shared column must follow its entity");
assert_eq!(extra.0 as usize, k * 3, "added column must map input order");
}
world
.with_exclusive(|data| {
let location = data.shards.get_location(entities[0])?.unwrap();
assert_eq!(
data.archetypes[location.archetype as usize]
.length()
.unwrap(),
n as usize
);
Ok(())
})
.unwrap();
}
#[test]
fn add_component_batch_preflight_rejections() {
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
let a = spawn_marker(&ecs, marker_id, 1);
let b = spawn_marker(&ecs, marker_id, 2);
// Duplicate handle.
world
.defer(Command::AddComponentBatch {
entities: vec![a, a],
component_id: extra_id,
values: Box::new(vec![Extra(1), Extra(2)]),
len: 2,
})
.unwrap();
assert!(ecs.apply_deferred_commands().is_err());
// Length mismatch.
world
.defer(Command::AddComponentBatch {
entities: vec![a, b],
component_id: extra_id,
values: Box::new(vec![Extra(1)]),
len: 1,
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Spawn(
crate::engine::error::SpawnError::BatchColumnMismatch { .. }
))
));
// Cross-archetype batch: give `b` the Extra component first.
world
.defer(Command::Add {
entity: b,
component_id: extra_id,
value: Box::new(Extra(9)),
})
.unwrap();
ecs.apply_deferred_commands().unwrap();
world
.defer(Command::AddComponentBatch {
entities: vec![a, b],
component_id: extra_id,
values: Box::new(vec![Extra(1), Extra(2)]),
len: 2,
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Move(MoveError::BatchSpansArchetypes { .. }))
));
// Already-present component (single archetype).
world
.defer(Command::AddComponentBatch {
entities: vec![b],
component_id: extra_id,
values: Box::new(vec![Extra(1)]),
len: 1,
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Move(MoveError::ComponentAlreadyPresent { .. }))
));
// Untouched survivor.
let marker: Marker = world.read_entity_component(a, marker_id).unwrap();
assert_eq!(marker.0, 1);
}
#[test]
fn add_component_batch_type_mismatch_is_a_no_op() {
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
let mut entities = Vec::new();
for i in 0..100 {
entities.push(spawn_marker(&ecs, marker_id, i));
}
// Wrong payload type: Vec<Marker> where Vec<Extra> is required.
world
.defer(Command::AddComponentBatch {
entities: entities.clone(),
component_id: extra_id,
values: Box::new((0..100u32).map(Marker).collect::<Vec<_>>()),
len: 100,
})
.unwrap();
assert!(ecs.apply_deferred_commands().is_err());
// World unchanged: values intact, no Extra column rows anywhere.
for (k, &entity) in entities.iter().enumerate() {
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
assert_eq!(marker.0 as usize, k);
assert!(world
.read_entity_component::<Extra>(entity, extra_id)
.is_err());
}
assert_eq!(count_markers_in(&ecs), 100);
}
#[test]
fn remove_component_batch_migrates_and_skips_non_carriers() {
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
// 60 carriers (Marker + Extra), 40 plain markers.
let mut carriers = Vec::new();
for i in 0..60u32 {
let entity = spawn_marker(&ecs, marker_id, i);
world
.defer(Command::Add {
entity,
component_id: extra_id,
value: Box::new(Extra(i + 1000)),
})
.unwrap();
carriers.push(entity);
}
ecs.apply_deferred_commands().unwrap();
let mut plain = Vec::new();
for i in 60..100u32 {
plain.push(spawn_marker(&ecs, marker_id, i));
}
// Remove Extra from everyone; non-carriers are skipped (no-op parity).
let mut all = carriers.clone();
all.extend_from_slice(&plain);
world
.defer(Command::RemoveComponentBatch {
entities: all,
component_id: extra_id,
})
.unwrap();
ecs.apply_deferred_commands().unwrap();
for (k, &entity) in carriers.iter().enumerate() {
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
assert_eq!(marker.0 as usize, k, "shared column must survive removal");
assert!(world
.read_entity_component::<Extra>(entity, extra_id)
.is_err());
}
for (k, &entity) in plain.iter().enumerate() {
let marker: Marker = world.read_entity_component(entity, marker_id).unwrap();
assert_eq!(marker.0 as usize, k + 60);
}
assert_eq!(count_markers_in(&ecs), 100);
// Removal that would empty the signature is rejected.
world
.defer(Command::RemoveComponentBatch {
entities: vec![plain[0]],
component_id: marker_id,
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Move(MoveError::BatchWouldEmptyEntity { .. }))
));
assert_eq!(count_markers_in(&ecs), 100);
}
#[test]
fn wrong_type_spawn_rolls_back_all_written_columns() {
let (ecs, marker_id, extra_id) = test_manager();
let world = ecs.world_ref();
let mut bundle = Bundle::new();
bundle.insert(marker_id, Marker(1));
bundle.insert(extra_id, Marker(2));
world.defer(Command::Spawn { bundle }).unwrap();
assert!(ecs.apply_deferred_commands().is_err());
world
.with_exclusive(|data| {
assert_all_columns_empty(data);
Ok(())
})
.unwrap();
}
#[test]
fn failed_add_component_leaves_source_destination_and_location_unchanged() {
let (ecs, marker_id, extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 7);
let world = ecs.world_ref();
let before = world
.with_exclusive(|data| {
let loc = data.shards.get_location(entity)?.unwrap();
let source_len = data.archetypes[loc.archetype as usize].length()?;
let marker_len = data.archetypes[loc.archetype as usize]
.component_locked(marker_id)
.unwrap()
.read()
.unwrap()
.length();
Ok((loc, source_len, marker_len))
})
.unwrap();
world
.defer(Command::Add {
entity,
component_id: extra_id,
value: Box::new(Marker(99)),
})
.unwrap();
assert!(ecs.apply_deferred_commands().is_err());
world
.with_exclusive(|data| {
let loc = data.shards.get_location(entity)?.unwrap();
assert_eq!(loc.archetype, before.0.archetype);
assert_eq!(loc.chunk, before.0.chunk);
assert_eq!(loc.row, before.0.row);
assert_eq!(
data.archetypes[loc.archetype as usize].length().unwrap(),
before.1
);
assert_eq!(
data.archetypes[loc.archetype as usize]
.component_locked(marker_id)
.unwrap()
.read()
.unwrap()
.length(),
before.2
);
for archetype in &data.archetypes {
if archetype.archetype_id() != loc.archetype {
assert_eq!(archetype.length().unwrap(), 0);
for (_, len) in archetype.component_lengths_for_test() {
assert_eq!(len, 0);
}
}
}
Ok(())
})
.unwrap();
}
#[test]
fn duplicate_add_component_returns_error_without_migration() {
let (ecs, marker_id, _extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 7);
let world = ecs.world_ref();
world
.defer(Command::Add {
entity,
component_id: marker_id,
value: Box::new(Marker(8)),
})
.unwrap();
assert!(matches!(
ecs.apply_deferred_commands(),
Err(ECSError::Move(MoveError::ComponentAlreadyPresent { .. }))
));
}
#[test]
fn disjoint_archetype_move_replaces_source_only_with_destination_only_components() {
let (ecs, marker_id, extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 7);
let world = ecs.world_ref();
let (source_id, destination_id) = world
.with_exclusive(|data| {
let location = data.shards.get_location(entity)?.unwrap();
let source_id = location.archetype;
let mut destination_signature = Signature::default();
destination_signature.set(extra_id);
let destination_id = data.get_or_create_archetype(&destination_signature)?;
let shards = &data.shards;
let (source, destination) = ECSData::get_archetype_pair_mut(
&mut data.archetypes,
source_id,
destination_id,
)?;
source.move_row_to_archetype(
destination,
shards,
entity,
(location.chunk, location.row),
vec![(extra_id, Box::new(Extra(9)) as Box<dyn std::any::Any>)],
)?;
Ok((source_id, destination_id))
})
.unwrap();
world
.with_exclusive(|data| {
let location = data.shards.get_location(entity)?.unwrap();
assert_eq!(location.archetype, destination_id);
assert_eq!(data.archetypes[source_id as usize].length().unwrap(), 0);
assert_eq!(
data.archetypes[source_id as usize]
.component_locked(marker_id)
.unwrap()
.read()
.unwrap()
.length(),
0
);
assert_eq!(
data.archetypes[destination_id as usize].length().unwrap(),
1
);
assert_eq!(
data.archetypes[destination_id as usize]
.component_locked(extra_id)
.unwrap()
.read()
.unwrap()
.length(),
1
);
Ok(())
})
.unwrap();
}
#[cfg(feature = "gpu")]
fn clear_dirty_for_entity(
data: &mut ECSData,
entity: Entity,
component_id: ComponentID,
) -> (ArchetypeID, usize) {
let loc = data.shards.get_location(entity).unwrap().unwrap();
let chunk_count = data.archetypes[loc.archetype as usize]
.chunk_count()
.unwrap();
let _ = data
.gpu_dirty_chunks
.take_dirty_chunks(loc.archetype, component_id, chunk_count);
(loc.archetype, chunk_count)
}
#[cfg(feature = "gpu")]
#[test]
fn cpu_write_query_marks_gpu_dirty_chunks() {
let (ecs, marker_id, _extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 1);
let world = ecs.world_ref();
let (archetype_id, chunk_count) = world
.with_exclusive(|data| Ok(clear_dirty_for_entity(data, entity, marker_id)))
.unwrap();
let query = world
.query()
.unwrap()
.write::<Marker>()
.unwrap()
.build()
.unwrap();
world
.for_each_w1(query, |marker: &mut Marker| marker.0 += 1)
.unwrap();
world
.with_exclusive(|data| {
let dirty =
data.gpu_dirty_chunks
.take_dirty_chunks(archetype_id, marker_id, chunk_count);
assert_eq!(dirty, vec![0]);
Ok(())
})
.unwrap();
}
#[cfg(feature = "gpu")]
#[test]
fn cpu_read_query_does_not_mark_gpu_dirty_chunks() {
let (ecs, marker_id, _extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 1);
let world = ecs.world_ref();
let (archetype_id, chunk_count) = world
.with_exclusive(|data| Ok(clear_dirty_for_entity(data, entity, marker_id)))
.unwrap();
let query = world
.query()
.unwrap()
.read::<Marker>()
.unwrap()
.build()
.unwrap();
world.for_each_r1(query, |_marker: &Marker| {}).unwrap();
world
.with_exclusive(|data| {
let dirty =
data.gpu_dirty_chunks
.take_dirty_chunks(archetype_id, marker_id, chunk_count);
assert!(dirty.is_empty());
Ok(())
})
.unwrap();
}
#[cfg(feature = "gpu")]
#[test]
fn fallible_cpu_write_query_marks_gpu_dirty_before_error() {
let (ecs, marker_id, _extra_id) = test_manager();
let entity = spawn_marker(&ecs, marker_id, 1);
let world = ecs.world_ref();
let (archetype_id, chunk_count) = world
.with_exclusive(|data| Ok(clear_dirty_for_entity(data, entity, marker_id)))
.unwrap();
let query = world
.query()
.unwrap()
.write::<Marker>()
.unwrap()
.build()
.unwrap();
let result = world.for_each_w1_fallible(query, |marker: &mut Marker| {
marker.0 += 1;
Err(ECSError::from(ExecutionError::InternalExecutionError))
});
assert!(result.is_err());
world
.with_exclusive(|data| {
let dirty =
data.gpu_dirty_chunks
.take_dirty_chunks(archetype_id, marker_id, chunk_count);
assert_eq!(dirty, vec![0]);
Ok(())
})
.unwrap();
}
}