rdpe 0.1.0

Reaction Diffusion Particle Engine - GPU particle simulations made easy
Documentation
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
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
//! GPU state management and rendering for particle simulations.
//!
//! This module contains all WebGPU-related code including:
//! - Compute pipelines for particle physics simulation
//! - Render pipelines for particle visualization
//! - Spatial hashing for neighbor queries
//! - Field systems for continuous scalar/vector fields
//! - Visual effects (trails, connections, post-processing, volume rendering)

// Allow dead_code for public API methods that may not be used internally
#![allow(dead_code)]

mod adjacency;
mod camera;
mod connections;
mod field_gpu;
mod glyphs;
mod picking;
mod post_process;
mod spatial_gpu;
mod spatial_grid_viz;
pub mod sub_emitter_gpu;
mod trails;
mod volume_render;
mod wireframe;

#[cfg(feature = "egui")]
mod egui_integration;

// Re-export submodule types
pub use adjacency::{AdjacencyGpu, adjacency_wgsl};
pub use camera::Camera;
pub use connections::ConnectionState;
pub use field_gpu::{FieldSystemGpu, create_particle_field_bind_group_layout};
pub use glyphs::GlyphRenderer;
pub use picking::PickingState;
pub use post_process::PostProcessState;
pub use spatial_grid_viz::SpatialGridViz;
pub use sub_emitter_gpu::SubEmitterGpu;
pub use trails::TrailState;
pub use volume_render::{VolumeConfig, VolumeRenderState};
pub use wireframe::WireframeState;

use crate::error::GpuError;
use crate::field::FieldRegistry;
#[cfg(feature = "egui")]
use crate::selection::{PendingParticleWrite, SelectedParticle, SelectedParticleData};

#[cfg(feature = "egui")]
pub use egui_integration::EguiIntegration;

use std::sync::Arc;

use bytemuck::{Pod, Zeroable};
use glam::{Mat4, Vec3};
use wgpu::util::DeviceExt;
use winit::window::Window;

pub use spatial_gpu::SpatialGpu;
use crate::spatial::SpatialConfig;
use crate::visuals::BlendMode;

const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
const WORKGROUP_SIZE: u32 = 256;

/// Convert BlendMode to wgpu BlendState
fn blend_mode_to_state(mode: BlendMode) -> wgpu::BlendState {
    match mode {
        BlendMode::Alpha => wgpu::BlendState::ALPHA_BLENDING,
        BlendMode::Additive => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::SrcAlpha,
                dst_factor: wgpu::BlendFactor::One,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::One,
                operation: wgpu::BlendOperation::Add,
            },
        },
        BlendMode::Multiply => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::Dst,
                dst_factor: wgpu::BlendFactor::Zero,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent::OVER,
        },
        // Screen: 1 - (1-src)*(1-dst) ≈ src + dst*(1-src)
        BlendMode::Screen => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrc,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
        },
        // Overlay: approximated as enhanced multiply/screen combination
        // Uses src*dst + dst*src for a contrast-boosting effect
        BlendMode::Overlay => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::Dst,
                dst_factor: wgpu::BlendFactor::Src,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent::OVER,
        },
        // SoftLight: gentle contrast adjustment, approximated as blend toward dst
        BlendMode::SoftLight => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::Dst,
                dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent::OVER,
        },
        // Subtractive: dst - src (clamped to 0)
        BlendMode::Subtractive => wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::One,
                operation: wgpu::BlendOperation::ReverseSubtract,
            },
            alpha: wgpu::BlendComponent::OVER,
        },
    }
}

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct Uniforms {
    view_proj: [[f32; 4]; 4],
    time: f32,
    delta_time: f32,
}

/// Cached time values for field processing.
#[derive(Copy, Clone, Default)]
struct TimeCache {
    time: f32,
    delta_time: f32,
}

/// GPU state for particle simulation and rendering.
///
/// Some buffers are stored but not directly read - they must remain alive
/// because bind groups hold references to them.
#[allow(dead_code)]
pub struct GpuState {
    surface: wgpu::Surface<'static>,
    device: wgpu::Device,
    queue: wgpu::Queue,
    pub config: wgpu::SurfaceConfiguration,
    render_pipeline: wgpu::RenderPipeline,
    compute_pipeline: wgpu::ComputePipeline,
    particle_buffer: wgpu::Buffer,
    uniform_buffer: wgpu::Buffer,
    uniform_buffer_size: usize,
    uniform_bind_group: wgpu::BindGroup,
    compute_bind_group: wgpu::BindGroup,
    depth_texture: wgpu::TextureView,
    num_particles: u32,
    pub camera: Camera,
    // Optional spatial hashing
    spatial: Option<SpatialGpu>,
    // Trail rendering
    trail_state: Option<TrailState>,
    // Connection rendering
    connection_state: Option<ConnectionState>,
    // Particle communication inbox
    inbox_buffer: Option<wgpu::Buffer>,
    inbox_bind_group: Option<wgpu::BindGroup>,
    inbox_enabled: bool,
    // 3D spatial fields
    field_system: Option<FieldSystemGpu>,
    field_bind_group: Option<wgpu::BindGroup>,
    field_bind_group_layout: Option<wgpu::BindGroupLayout>,
    // Empty bind group for when fields are enabled but inbox is not
    empty_bind_group: Option<wgpu::BindGroup>,
    // Volume rendering for fields
    volume_render: Option<VolumeRenderState>,
    volume_config: Option<VolumeConfig>,
    // Background clear color
    background_color: Vec3,
    // Post-processing
    post_process: Option<PostProcessState>,
    // Custom textures for shaders
    custom_textures: Vec<wgpu::Texture>,
    custom_texture_views: Vec<wgpu::TextureView>,
    custom_samplers: Vec<wgpu::Sampler>,
    texture_bind_group: Option<wgpu::BindGroup>,
    texture_bind_group_layout: Option<wgpu::BindGroupLayout>,
    // Egui integration (when feature enabled)
    #[cfg(feature = "egui")]
    egui: Option<EguiIntegration>,
    #[cfg(feature = "egui")]
    window: Arc<Window>,
    // Sub-emitter system for spawning particles on death
    sub_emitter: Option<SubEmitterGpu>,
    // Spatial grid visualization
    spatial_grid_viz: Option<SpatialGridViz>,
    // Wireframe mesh rendering
    wireframe_state: Option<WireframeState>,
    // Adjacency buffer for graph-based operations
    adjacency: Option<AdjacencyGpu>,
    adjacency_bind_group: Option<wgpu::BindGroup>,
    adjacency_bind_group_layout: Option<wgpu::BindGroupLayout>,
    // CPU readback support
    particle_stride: usize,
    readback_staging: Option<wgpu::Buffer>,
    // GPU picking for particle selection
    picking: PickingState,
    // Pipeline rebuild support - store layouts and config
    render_pipeline_layout: wgpu::PipelineLayout,
    compute_pipeline_layout: wgpu::PipelineLayout,
    uniform_bind_group_layout: wgpu::BindGroupLayout,
    blend_mode: BlendMode,
    color_offset: Option<u32>,
    alive_offset: u32,
    scale_offset: u32,
    // Cached time values for field processing
    time_cache: TimeCache,
}

impl GpuState {
    /// Create new GPU state for particle simulation.
    ///
    /// Takes many parameters because GPU initialization genuinely requires
    /// configuring multiple subsystems (particles, spatial hashing, trails, connections).
    #[allow(clippy::too_many_arguments)]
    pub async fn new(
        window: Arc<Window>,
        particle_data: &[u8],
        num_particles: u32,
        particle_stride: usize,
        compute_shader_src: &str,
        render_shader_src: &str,
        has_neighbors: bool,
        spatial_config: SpatialConfig,
        color_offset: Option<u32>,
        alive_offset: u32,
        scale_offset: u32,
        custom_uniform_size: usize,
        blend_mode: BlendMode,
        trail_length: u32,
        particle_size: f32,
        connections_enabled: bool,
        connections_radius: f32,
        connections_color: Vec3,
        inbox_enabled: bool,
        background_color: Vec3,
        post_process_shader: Option<&str>,
        custom_uniform_fields: &str,
        texture_registry: &crate::textures::TextureRegistry,
        _texture_declarations: &str,
        field_registry: &FieldRegistry,
        volume_config: Option<&VolumeConfig>,
        sub_emitters: &[crate::sub_emitter::SubEmitter],
        spatial_grid_opacity: f32,
        particle_wgsl_struct: &str,
        wireframe_mesh: Option<&crate::visuals::WireframeMesh>,
        wireframe_thickness: f32,
        adjacency_config: Option<&crate::spatial::AdjacencyConfig>,
        #[cfg(feature = "egui")] egui_enabled: bool,
    ) -> Result<Self, GpuError> {
        let size = window.inner_size();

        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
            backends: wgpu::Backends::PRIMARY,
            ..Default::default()
        });

        let surface = instance.create_surface(window.clone())?;

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: Some(&surface),
                force_fallback_adapter: false,
            })
            .await
            .ok_or(GpuError::NoAdapter)?;

        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                    memory_hints: Default::default(),
                },
                None, // trace path
            )
            .await?;

        let surface_caps = surface.get_capabilities(&adapter);
        let surface_format = surface_caps
            .formats
            .iter()
            .find(|f| f.is_srgb())
            .copied()
            .unwrap_or(surface_caps.formats[0]);

        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format: surface_format,
            width: size.width,
            height: size.height,
            present_mode: wgpu::PresentMode::AutoNoVsync, // Uncapped FPS for benchmarking
            alpha_mode: surface_caps.alpha_modes[0],
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
        };
        surface.configure(&device, &config);

        let depth_texture = create_depth_texture(&device, &config);

        let particle_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("Particle Buffer"),
            contents: particle_data,
            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
        });

        let camera = Camera::new();
        let aspect = config.width as f32 / config.height as f32;
        let view = camera.view_matrix();
        let proj = Mat4::perspective_rh(45.0_f32.to_radians(), aspect, 0.1, 100.0);
        let view_proj = proj * view;

        let uniforms = Uniforms {
            view_proj: view_proj.to_cols_array_2d(),
            time: 0.0,
            delta_time: 0.0,
        };

        // Base uniform size + custom uniforms (aligned to 16 bytes for uniform buffer)
        // Note: We pad base_size to 16-byte alignment before adding custom uniforms
        // to ensure vec3/vec4 custom uniforms are properly aligned
        let base_size = std::mem::size_of::<Uniforms>();
        let padded_base_size = (base_size + 15) & !15;
        let total_size = ((padded_base_size + custom_uniform_size) + 15) & !15;

        // Create buffer with base uniforms + space for custom uniforms
        let mut uniform_data = bytemuck::bytes_of(&uniforms).to_vec();
        uniform_data.resize(total_size, 0);

        let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("Uniform Buffer"),
            contents: &uniform_data,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });
        let uniform_buffer_size = total_size;

        // Create spatial hashing if needed
        let spatial = if has_neighbors {
            Some(SpatialGpu::new(
                &device,
                &particle_buffer,
                num_particles,
                spatial_config,
                particle_wgsl_struct,
            ))
        } else {
            None
        };

        // Create inbox buffer for particle communication (4 atomic i32 channels per particle)
        let inbox_buffer = if inbox_enabled {
            // 4 i32 values per particle = 16 bytes per particle
            let inbox_size = (num_particles as usize) * 16;
            Some(device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Inbox Buffer"),
                size: inbox_size as u64,
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }))
        } else {
            None
        };

        // Create adjacency buffer for graph-based operations
        let adjacency = if let (Some(ref spatial), Some(adj_cfg)) = (&spatial, adjacency_config) {
            Some(AdjacencyGpu::new(
                &device,
                &particle_buffer,
                spatial,
                num_particles,
                adj_cfg.max_neighbors,
                adj_cfg.radius,
                particle_stride,
            ))
        } else {
            None
        };

        // Render bind group layout (visible to both vertex and fragment for custom shaders)
        let uniform_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Uniform Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
            });

        let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Uniform Bind Group"),
            layout: &uniform_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: uniform_buffer.as_entire_binding(),
            }],
        });

        // Compute bind group layout - different depending on whether we have neighbors
        let (compute_bind_group_layout, compute_bind_group) = if let Some(ref spatial) = spatial {
            // With neighbors: particles, uniforms, sorted_indices, cell_start, cell_end, spatial_params
            let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Compute Bind Group Layout (with neighbors)"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Storage { read_only: false },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 2,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Storage { read_only: true },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 3,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Storage { read_only: true },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 4,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Storage { read_only: true },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 5,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                ],
            });

            let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Compute Bind Group (with neighbors)"),
                layout: &layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: particle_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: uniform_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: spatial.particle_indices_a.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: spatial.cell_start.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: spatial.cell_end.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 5,
                        resource: spatial.spatial_params_buffer.as_entire_binding(),
                    },
                ],
            });

            (layout, bind_group)
        } else {
            // Without neighbors: just particles and uniforms
            let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Compute Bind Group Layout"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Storage { read_only: false },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: wgpu::BufferBindingType::Uniform,
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    },
                ],
            });

            let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Compute Bind Group"),
                layout: &layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: particle_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: uniform_buffer.as_entire_binding(),
                    },
                ],
            });

            (layout, bind_group)
        };

        // Create texture bind group layout early (needed for render pipeline layout)
        let texture_bind_group_layout = if !texture_registry.textures.is_empty() {
            let mut layout_entries = Vec::new();
            for i in 0..texture_registry.textures.len() {
                layout_entries.push(wgpu::BindGroupLayoutEntry {
                    binding: (i * 2) as u32,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                });
                layout_entries.push(wgpu::BindGroupLayoutEntry {
                    binding: (i * 2 + 1) as u32,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                });
            }
            Some(device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Texture Bind Group Layout"),
                entries: &layout_entries,
            }))
        } else {
            None
        };

        // Render pipeline
        let render_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Render Shader"),
            source: wgpu::ShaderSource::Wgsl(render_shader_src.into()),
        });

        // Build bind group layouts vec, including texture layout if present
        let mut bind_group_layouts_vec: Vec<&wgpu::BindGroupLayout> = vec![&uniform_bind_group_layout];
        if let Some(ref tex_layout) = texture_bind_group_layout {
            bind_group_layouts_vec.push(tex_layout);
        }

        let render_pipeline_layout =
            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("Render Pipeline Layout"),
                bind_group_layouts: &bind_group_layouts_vec,
                push_constant_ranges: &[],
            });

        // Build vertex attributes: position, optional color, alive, scale
        let vertex_attributes: Vec<wgpu::VertexAttribute> = if let Some(offset) = color_offset {
            vec![
                wgpu::VertexAttribute {
                    offset: 0,
                    shader_location: 0,
                    format: wgpu::VertexFormat::Float32x3, // position
                },
                wgpu::VertexAttribute {
                    offset: offset as wgpu::BufferAddress,
                    shader_location: 1,
                    format: wgpu::VertexFormat::Float32x3, // color
                },
                wgpu::VertexAttribute {
                    offset: alive_offset as wgpu::BufferAddress,
                    shader_location: 2,
                    format: wgpu::VertexFormat::Uint32, // alive
                },
                wgpu::VertexAttribute {
                    offset: scale_offset as wgpu::BufferAddress,
                    shader_location: 3,
                    format: wgpu::VertexFormat::Float32, // scale
                },
            ]
        } else {
            vec![
                wgpu::VertexAttribute {
                    offset: 0,
                    shader_location: 0,
                    format: wgpu::VertexFormat::Float32x3, // position
                },
                wgpu::VertexAttribute {
                    offset: alive_offset as wgpu::BufferAddress,
                    shader_location: 2,
                    format: wgpu::VertexFormat::Uint32, // alive
                },
                wgpu::VertexAttribute {
                    offset: scale_offset as wgpu::BufferAddress,
                    shader_location: 3,
                    format: wgpu::VertexFormat::Float32, // scale
                },
            ]
        };

        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Render Pipeline"),
            layout: Some(&render_pipeline_layout),
            vertex: wgpu::VertexState {
                module: &render_shader,
                entry_point: Some("vs_main"),
                buffers: &[wgpu::VertexBufferLayout {
                    array_stride: particle_stride as wgpu::BufferAddress,
                    step_mode: wgpu::VertexStepMode::Instance,
                    attributes: &vertex_attributes,
                }],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &render_shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: config.format,
                    blend: Some(blend_mode_to_state(blend_mode)),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: None,
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: DEPTH_FORMAT,
                // Disable depth writes for additive-like blending so particles can blend through each other
                depth_write_enabled: !matches!(blend_mode, BlendMode::Additive | BlendMode::Screen),
                depth_compare: wgpu::CompareFunction::Less,
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
            cache: None,
        });

        // Compute pipeline
        let compute_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Compute Shader"),
            source: wgpu::ShaderSource::Wgsl(compute_shader_src.into()),
        });

        // Create inbox bind group layout and bind group if enabled
        let (inbox_bind_group_layout, inbox_bind_group) = if let Some(ref inbox_buf) = inbox_buffer {
            let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Inbox Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: false },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
            });

            let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Inbox Bind Group"),
                layout: &layout,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: inbox_buf.as_entire_binding(),
                }],
            });

            (Some(layout), Some(bind_group))
        } else {
            (None, None)
        };

        // Create field system if fields are registered
        let (field_system, field_bind_group_layout, field_bind_group) = if !field_registry.is_empty() {
            let system = FieldSystemGpu::new(&device, field_registry);
            let layout = create_particle_field_bind_group_layout(&device, system.field_count);
            let bind_group = system.create_particle_bind_group(&device, &layout);
            (Some(system), Some(layout), bind_group)
        } else {
            (None, None, None)
        };

        // Create volume render state if configured and fields exist
        let volume_render = if let (Some(config), Some(ref fs)) = (&volume_config, &field_system) {
            Some(VolumeRenderState::new(&device, fs, config, surface_format))
        } else {
            None
        };

        // Create sub-emitter system early so we can use its bind group layout
        let sub_emitter = if !sub_emitters.is_empty() {
            Some(SubEmitterGpu::new(
                &device,
                &particle_buffer,
                num_particles,
                sub_emitters,
                particle_wgsl_struct,
            ))
        } else {
            None
        };

        // Create adjacency bind group for read-only access in main compute shader
        let (adjacency_bind_group_layout, adjacency_bind_group) = if let Some(ref adj) = adjacency {
            let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Adjacency Read Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
            });

            let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Adjacency Read Bind Group"),
                layout: &layout,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: adj.buffer.as_entire_binding(),
                }],
            });

            (Some(layout), Some(bind_group))
        } else {
            (None, None)
        };

        // Build compute pipeline layout with optional inbox, field, sub-emitter, and adjacency bind groups
        // Group 0: particles/uniforms/spatial
        // Group 1: inbox (if enabled)
        // Group 2: fields (if enabled)
        // Group 3: sub-emitter death buffers (if enabled)
        // Group 4: adjacency buffer (if enabled)
        let (compute_pipeline_layout, empty_bind_group) = {
            // Create empty layout/bind group for gaps
            let empty_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Empty Bind Group Layout"),
                entries: &[],
            });
            let empty_bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Empty Bind Group"),
                layout: &empty_layout,
                entries: &[],
            });

            // Build layouts vec dynamically
            let mut layouts: Vec<&wgpu::BindGroupLayout> = vec![&compute_bind_group_layout];

            // Group 1: inbox or empty
            if let Some(ref inbox_layout) = inbox_bind_group_layout {
                layouts.push(inbox_layout);
            } else if field_bind_group_layout.is_some() || sub_emitter.is_some() || adjacency_bind_group_layout.is_some() {
                // Need placeholder at group 1 if we have group 2, 3, or 4
                layouts.push(&empty_layout);
            }

            // Group 2: fields or empty
            if let Some(ref field_layout) = field_bind_group_layout {
                layouts.push(field_layout);
            } else if sub_emitter.is_some() || adjacency_bind_group_layout.is_some() {
                // Need placeholder at group 2 if we have group 3 or 4
                layouts.push(&empty_layout);
            }

            // Group 3: sub-emitter death buffers
            if let Some(ref se) = sub_emitter {
                layouts.push(&se.death_bind_group_layout);
            } else if adjacency_bind_group_layout.is_some() {
                // Need placeholder at group 3 if we have group 4
                layouts.push(&empty_layout);
            }

            // Group 4: adjacency buffer
            if let Some(ref adj_layout) = adjacency_bind_group_layout {
                layouts.push(adj_layout);
            }

            let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("Compute Pipeline Layout"),
                bind_group_layouts: &layouts,
                push_constant_ranges: &[],
            });

            // Only keep empty_bg if we need it for gaps
            let keep_empty = (inbox_bind_group_layout.is_none() && (field_bind_group_layout.is_some() || sub_emitter.is_some() || adjacency_bind_group_layout.is_some()))
                || (field_bind_group_layout.is_none() && (sub_emitter.is_some() || adjacency_bind_group_layout.is_some()))
                || (sub_emitter.is_none() && adjacency_bind_group_layout.is_some());
            (layout, if keep_empty { Some(empty_bg) } else { None })
        };

        let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("Compute Pipeline"),
            layout: Some(&compute_pipeline_layout),
            module: &compute_shader,
            entry_point: Some("main"),
            compilation_options: Default::default(),
            cache: None,
        });

        // Trail system (if trail_length > 0)
        let trail_state = if trail_length > 0 {
            Some(TrailState::new(
                &device,
                &particle_buffer,
                &uniform_buffer,
                num_particles,
                trail_length,
                particle_stride,
                color_offset,
                particle_size,
                blend_mode,
                config.format,
            ))
        } else {
            None
        };

        // Connection system (requires spatial hashing)
        let connection_state = if let (true, Some(spatial_ref)) = (connections_enabled, spatial.as_ref()) {
            Some(ConnectionState::new(
                &device,
                &particle_buffer,
                &uniform_buffer,
                spatial_ref,
                num_particles,
                connections_radius,
                connections_color,
                particle_stride,
                blend_mode,
                config.format,
            ))
        } else {
            None
        };

        // Post-processing setup
        let post_process = if let Some(shader_code) = post_process_shader {
            Some(PostProcessState::new(
                &device,
                &uniform_buffer,
                shader_code,
                custom_uniform_fields,
                config.width,
                config.height,
                config.format,
            ))
        } else {
            None
        };

        // Create custom textures
        let mut custom_textures = Vec::new();
        let mut custom_texture_views = Vec::new();
        let mut custom_samplers = Vec::new();

        for (_name, config) in &texture_registry.textures {
            // Create texture
            let texture = device.create_texture(&wgpu::TextureDescriptor {
                label: Some("Custom Texture"),
                size: wgpu::Extent3d {
                    width: config.width,
                    height: config.height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8UnormSrgb,
                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
                view_formats: &[],
            });

            // Upload texture data
            queue.write_texture(
                wgpu::TexelCopyTextureInfo {
                    texture: &texture,
                    mip_level: 0,
                    origin: wgpu::Origin3d::ZERO,
                    aspect: wgpu::TextureAspect::All,
                },
                &config.data,
                wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(4 * config.width),
                    rows_per_image: Some(config.height),
                },
                wgpu::Extent3d {
                    width: config.width,
                    height: config.height,
                    depth_or_array_layers: 1,
                },
            );

            // Create view
            let view = texture.create_view(&wgpu::TextureViewDescriptor::default());

            // Create sampler
            let filter = match config.filter {
                crate::textures::FilterMode::Linear => wgpu::FilterMode::Linear,
                crate::textures::FilterMode::Nearest => wgpu::FilterMode::Nearest,
            };
            let address_mode = match config.address_mode {
                crate::textures::AddressMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
                crate::textures::AddressMode::Repeat => wgpu::AddressMode::Repeat,
                crate::textures::AddressMode::MirrorRepeat => wgpu::AddressMode::MirrorRepeat,
            };
            let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
                address_mode_u: address_mode,
                address_mode_v: address_mode,
                address_mode_w: address_mode,
                mag_filter: filter,
                min_filter: filter,
                mipmap_filter: wgpu::FilterMode::Nearest,
                ..Default::default()
            });

            custom_textures.push(texture);
            custom_texture_views.push(view);
            custom_samplers.push(sampler);
        }

        // Create texture bind group using the layout we created earlier
        let texture_bind_group = if let Some(ref layout) = texture_bind_group_layout {
            let mut bind_group_entries = Vec::new();
            for i in 0..custom_texture_views.len() {
                bind_group_entries.push(wgpu::BindGroupEntry {
                    binding: (i * 2) as u32,
                    resource: wgpu::BindingResource::TextureView(&custom_texture_views[i]),
                });
                bind_group_entries.push(wgpu::BindGroupEntry {
                    binding: (i * 2 + 1) as u32,
                    resource: wgpu::BindingResource::Sampler(&custom_samplers[i]),
                });
            }

            Some(device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Texture Bind Group"),
                layout,
                entries: &bind_group_entries,
            }))
        } else {
            None
        };

        // Initialize egui if feature enabled
        #[cfg(feature = "egui")]
        let egui = if egui_enabled {
            Some(EguiIntegration::new(&device, config.format, &window))
        } else {
            None
        };

        // Spatial grid visualization (always created for runtime toggling)
        let spatial_grid_viz = Some(SpatialGridViz::new(
            &device,
            &uniform_buffer,
            &spatial_config,
            spatial_grid_opacity,
            surface_format,
        ));

        // Wireframe mesh rendering (if configured)
        let wireframe_state = wireframe_mesh.map(|mesh| WireframeState::new(
            &device,
            &particle_buffer,
            &uniform_buffer,
            mesh,
            wireframe_thickness,
            particle_size,
            num_particles,
            particle_stride,
            color_offset,
            alive_offset,
            scale_offset,
            blend_mode,
            surface_format,
        ));

        // GPU picking for particle selection
        let picking = PickingState::new(
            &device,
            config.width,
            config.height,
            particle_stride,
            color_offset,
            alive_offset,
            scale_offset,
        );

        Ok(Self {
            surface,
            device,
            queue,
            config,
            render_pipeline,
            compute_pipeline,
            particle_buffer,
            uniform_buffer,
            uniform_buffer_size,
            uniform_bind_group,
            compute_bind_group,
            depth_texture,
            num_particles,
            camera,
            spatial,
            trail_state,
            connection_state,
            inbox_buffer,
            inbox_bind_group,
            inbox_enabled,
            field_system,
            field_bind_group,
            field_bind_group_layout,
            empty_bind_group,
            volume_render,
            volume_config: volume_config.cloned(),
            background_color,
            post_process,
            custom_textures,
            custom_texture_views,
            custom_samplers,
            texture_bind_group,
            texture_bind_group_layout,
            #[cfg(feature = "egui")]
            egui,
            #[cfg(feature = "egui")]
            window,
            sub_emitter,
            spatial_grid_viz,
            wireframe_state,
            adjacency,
            adjacency_bind_group,
            adjacency_bind_group_layout,
            particle_stride,
            readback_staging: None,
            picking,
            // Pipeline rebuild support
            render_pipeline_layout,
            compute_pipeline_layout,
            uniform_bind_group_layout,
            blend_mode,
            color_offset,
            alive_offset,
            scale_offset,
            // Time cache for field processing
            time_cache: TimeCache::default(),
        })
    }

    pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
        if new_size.width > 0 && new_size.height > 0 {
            self.config.width = new_size.width;
            self.config.height = new_size.height;
            self.surface.configure(&self.device, &self.config);
            self.depth_texture = create_depth_texture(&self.device, &self.config);

            // Resize post-processing if enabled
            if let Some(ref mut pp) = self.post_process {
                pp.resize(
                    &self.device,
                    &self.uniform_buffer,
                    self.config.width,
                    self.config.height,
                    self.config.format,
                );
            }

            // Resize picking texture
            self.picking.resize(&self.device, new_size.width, new_size.height);
        }
    }

    /// Set the spatial grid visualization opacity.
    ///
    /// Use 0.0 to hide the grid, 1.0 for full visibility.
    pub fn set_grid_opacity(&mut self, opacity: f32) {
        if let Some(ref mut grid) = self.spatial_grid_viz {
            grid.set_opacity(&self.queue, opacity);
        }
    }

    /// Set the background clear color.
    ///
    /// This can be changed at runtime without rebuilding pipelines.
    pub fn set_background_color(&mut self, color: Vec3) {
        self.background_color = color;
    }

    /// Rebuild the render pipeline with new shader and blend mode.
    ///
    /// This preserves particle data but recompiles the render shaders.
    /// Use this when visual settings that affect the shader change
    /// (shape, palette, color mapping, blend mode).
    pub fn rebuild_render_pipeline(&mut self, render_shader_src: &str, blend_mode: BlendMode) {
        // Create new shader module
        let render_shader = self.device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Render Shader (rebuilt)"),
            source: wgpu::ShaderSource::Wgsl(render_shader_src.into()),
        });

        // Build vertex attributes
        let vertex_attributes: Vec<wgpu::VertexAttribute> = if let Some(offset) = self.color_offset {
            vec![
                wgpu::VertexAttribute {
                    offset: 0,
                    shader_location: 0,
                    format: wgpu::VertexFormat::Float32x3, // position
                },
                wgpu::VertexAttribute {
                    offset: offset as wgpu::BufferAddress,
                    shader_location: 1,
                    format: wgpu::VertexFormat::Float32x3, // color
                },
                wgpu::VertexAttribute {
                    offset: self.alive_offset as wgpu::BufferAddress,
                    shader_location: 2,
                    format: wgpu::VertexFormat::Uint32, // alive
                },
                wgpu::VertexAttribute {
                    offset: self.scale_offset as wgpu::BufferAddress,
                    shader_location: 3,
                    format: wgpu::VertexFormat::Float32, // scale
                },
            ]
        } else {
            vec![
                wgpu::VertexAttribute {
                    offset: 0,
                    shader_location: 0,
                    format: wgpu::VertexFormat::Float32x3, // position
                },
                wgpu::VertexAttribute {
                    offset: self.alive_offset as wgpu::BufferAddress,
                    shader_location: 2,
                    format: wgpu::VertexFormat::Uint32, // alive
                },
                wgpu::VertexAttribute {
                    offset: self.scale_offset as wgpu::BufferAddress,
                    shader_location: 3,
                    format: wgpu::VertexFormat::Float32, // scale
                },
            ]
        };

        // Create new render pipeline
        let new_pipeline = self.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Render Pipeline (rebuilt)"),
            layout: Some(&self.render_pipeline_layout),
            vertex: wgpu::VertexState {
                module: &render_shader,
                entry_point: Some("vs_main"),
                buffers: &[wgpu::VertexBufferLayout {
                    array_stride: self.particle_stride as wgpu::BufferAddress,
                    step_mode: wgpu::VertexStepMode::Instance,
                    attributes: &vertex_attributes,
                }],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &render_shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: self.config.format,
                    blend: Some(blend_mode_to_state(blend_mode)),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: None,
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: DEPTH_FORMAT,
                depth_write_enabled: !matches!(blend_mode, BlendMode::Additive | BlendMode::Screen),
                depth_compare: wgpu::CompareFunction::Less,
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
            cache: None,
        });

        self.render_pipeline = new_pipeline;
        self.blend_mode = blend_mode;
    }

    /// Rebuild the compute pipeline with new shader.
    ///
    /// This preserves particle data but recompiles the compute shaders.
    /// Use this when rules change that affect the compute shader.
    pub fn rebuild_compute_pipeline(&mut self, compute_shader_src: &str) {
        // Create new shader module
        let compute_shader = self.device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Compute Shader (rebuilt)"),
            source: wgpu::ShaderSource::Wgsl(compute_shader_src.into()),
        });

        // Create new compute pipeline
        let new_pipeline = self.device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("Compute Pipeline (rebuilt)"),
            layout: Some(&self.compute_pipeline_layout),
            module: &compute_shader,
            entry_point: Some("main"),
            compilation_options: Default::default(),
            cache: None,
        });

        self.compute_pipeline = new_pipeline;
    }

    /// Request particle picking at the given screen coordinates.
    ///
    /// The pick will be performed on the next render, and the result
    /// will be available via `selected_particle()` after that frame.
    pub fn request_pick(&mut self, x: u32, y: u32) {
        self.picking.request_pick(x, y);
    }

    /// Get the currently selected particle index, if any.
    pub fn selected_particle(&self) -> Option<u32> {
        self.picking.selected_particle
    }

    /// Clear the current particle selection.
    pub fn clear_selection(&mut self) {
        self.picking.clear_selection();
    }

    /// Read particle data from GPU to CPU synchronously.
    ///
    /// This is an expensive operation that stalls the GPU pipeline.
    /// Use sparingly (e.g., once per second, or on user request).
    ///
    /// Returns raw bytes that can be cast to your particle's GPU type:
    /// ```ignore
    /// let bytes = gpu_state.read_particles_sync()?;
    /// let particles: &[MyParticleGpu] = bytemuck::cast_slice(&bytes);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `GpuError::BufferMapping` if the buffer cannot be mapped for reading.
    pub fn read_particles_sync(&mut self) -> Result<Vec<u8>, GpuError> {
        let buffer_size = (self.num_particles as usize) * self.particle_stride;

        // Create or reuse staging buffer
        let staging = self.readback_staging.get_or_insert_with(|| {
            self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Readback Staging Buffer"),
                size: buffer_size as u64,
                usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
                mapped_at_creation: false,
            })
        });

        // Copy particle buffer to staging
        let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("Readback Encoder"),
        });
        encoder.copy_buffer_to_buffer(
            &self.particle_buffer,
            0,
            staging,
            0,
            buffer_size as u64,
        );
        self.queue.submit(std::iter::once(encoder.finish()));

        // Map and read
        let buffer_slice = staging.slice(..);
        let (tx, rx) = std::sync::mpsc::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            // Ignore send errors - receiver may have been dropped
            let _ = tx.send(result);
        });

        // Wait for mapping to complete
        self.device.poll(wgpu::Maintain::Wait);
        rx.recv()
            .map_err(|_| GpuError::BufferMapping("Channel receive failed".to_string()))?
            .map_err(|e| GpuError::BufferMapping(format!("Buffer mapping failed: {}", e)))?;

        // Copy data
        let data = buffer_slice.get_mapped_range();
        let result = data.to_vec();
        drop(data);
        staging.unmap();

        Ok(result)
    }

    /// Write particle data from CPU to GPU.
    ///
    /// This is used to restore particle state after a pipeline rebuild.
    /// The data should be the same format returned by `read_particles_sync()`.
    ///
    /// # Panics
    ///
    /// Panics if the data size doesn't match the particle buffer size.
    pub fn write_particles(&mut self, data: &[u8]) {
        let expected_size = (self.num_particles as usize) * self.particle_stride;
        assert_eq!(
            data.len(),
            expected_size,
            "Particle data size mismatch: expected {} bytes, got {}",
            expected_size,
            data.len()
        );
        self.queue.write_buffer(&self.particle_buffer, 0, data);
    }

    /// Get the number of particles.
    pub fn num_particles(&self) -> u32 {
        self.num_particles
    }

    /// Get the particle stride (bytes per particle).
    pub fn particle_stride(&self) -> usize {
        self.particle_stride
    }

    /// Process a winit event through egui.
    ///
    /// Returns true if egui consumed the event (don't pass to camera controls).
    #[cfg(feature = "egui")]
    pub fn on_window_event(&mut self, event: &winit::event::WindowEvent) -> bool {
        if let Some(ref mut egui) = self.egui {
            egui.on_window_event(&self.window, event)
        } else {
            false
        }
    }

    /// Check if egui is enabled.
    #[cfg(feature = "egui")]
    #[allow(dead_code)]
    pub fn egui_enabled(&self) -> bool {
        self.egui.is_some()
    }

    /// Get access to egui context for running UI.
    #[cfg(feature = "egui")]
    #[allow(dead_code)]
    pub fn egui_ctx(&self) -> Option<&egui::Context> {
        self.egui.as_ref().map(|e| &e.ctx)
    }

    fn update_uniforms(&mut self, time: f32, delta_time: f32, custom_uniform_bytes: Option<&[u8]>) {
        // Cache time values for field processing
        self.time_cache = TimeCache { time, delta_time };

        let aspect = self.config.width as f32 / self.config.height as f32;
        let view = self.camera.view_matrix();
        let proj = Mat4::perspective_rh(45.0_f32.to_radians(), aspect, 0.1, 100.0);
        let view_proj = proj * view;

        let uniforms = Uniforms {
            view_proj: view_proj.to_cols_array_2d(),
            time,
            delta_time,
        };

        // Write base uniforms
        let base_bytes = bytemuck::bytes_of(&uniforms);

        if let Some(custom_bytes) = custom_uniform_bytes {
            // Combine base and custom uniforms
            let mut combined = base_bytes.to_vec();
            // Pad to 16-byte alignment before appending custom uniforms
            // This ensures vec3/vec4 custom uniforms are properly aligned
            while !combined.len().is_multiple_of(16) {
                combined.push(0);
            }
            combined.extend_from_slice(custom_bytes);
            // Pad to buffer size
            combined.resize(self.uniform_buffer_size, 0);
            self.queue.write_buffer(&self.uniform_buffer, 0, &combined);
        } else {
            self.queue.write_buffer(&self.uniform_buffer, 0, base_bytes);
        }
    }

    /// Render without UI (original method for backwards compatibility).
    pub fn render(&mut self, time: f32, delta_time: f32, custom_uniform_bytes: Option<&[u8]>) -> Result<(), wgpu::SurfaceError> {
        #[cfg(feature = "egui")]
        {
            self.render_with_ui(time, delta_time, custom_uniform_bytes, |_| {})
        }
        #[cfg(not(feature = "egui"))]
        {
            self.render_internal(time, delta_time, custom_uniform_bytes)
        }
    }

    /// Render with egui UI callback.
    #[cfg(feature = "egui")]
    pub fn render_with_ui<F>(
        &mut self,
        time: f32,
        delta_time: f32,
        custom_uniform_bytes: Option<&[u8]>,
        ui_callback: F,
    ) -> Result<(), wgpu::SurfaceError>
    where
        F: FnOnce(&egui::Context),
    {
        self.update_uniforms(time, delta_time, custom_uniform_bytes);

        let output = self.surface.get_current_texture()?;
        let view = output
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());

        // Process egui frame before creating encoder
        let egui_output_and_write = if let Some(ref mut egui) = self.egui {
            egui.begin_frame(&self.window);
            // Store selection in egui's data for the callback to access
            let selected = self.picking.selected_particle;
            let selected_data = self.picking.selected_particle_data.clone();
            egui.ctx.data_mut(|d| {
                d.insert_temp(egui::Id::NULL, SelectedParticle(selected));
                d.insert_temp(egui::Id::NULL, SelectedParticleData(selected_data));
            });
            ui_callback(&egui.ctx);

            // Check for pending particle writes (will apply after compute pass)
            let pending_write = egui.ctx.data(|d| {
                d.get_temp::<PendingParticleWrite>(egui::Id::NULL)
            });
            if pending_write.is_some() {
                // Clear the pending write from egui data
                egui.ctx.data_mut(|d| {
                    d.insert_temp(egui::Id::NULL, PendingParticleWrite(None));
                });
            }

            Some((egui.end_frame(&self.window), pending_write))
        } else {
            None
        };

        // Extract egui output and pending write from the tuple
        let (egui_output, pending_particle_write) = match egui_output_and_write {
            Some((output, write)) => (Some(output), write),
            None => (None, None),
        };

        let screen_descriptor = egui_wgpu::ScreenDescriptor {
            size_in_pixels: [self.config.width, self.config.height],
            pixels_per_point: self.window.scale_factor() as f32,
        };

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Render Encoder"),
            });

        // Prepare egui textures and buffers (before render pass)
        if let (Some(ref mut egui), Some(ref egui_out)) = (&mut self.egui, &egui_output) {
            egui.prepare(&self.device, &self.queue, &mut encoder, egui_out, &screen_descriptor);
        }

        // Spatial hashing pass (if enabled)
        if let Some(ref spatial) = self.spatial {
            spatial.execute(&mut encoder, &self.queue);
        }

        // Adjacency buffer computation (after spatial hashing)
        if let Some(ref adjacency) = self.adjacency {
            adjacency.execute(&mut encoder);
        }

        // Clear inbox buffer before compute pass
        if let Some(ref inbox_buf) = self.inbox_buffer {
            let inbox_size = (self.num_particles as usize) * 16;
            let zeros = vec![0u8; inbox_size];
            self.queue.write_buffer(inbox_buf, 0, &zeros);
        }

        // Clear sub-emitter death buffers before compute pass
        if let Some(ref se) = self.sub_emitter {
            se.clear_buffers(&self.queue);
        }

        // Recreate field bind group each frame (buffers may have been swapped during blur)
        let field_bind_group = if let (Some(ref field_sys), Some(ref layout)) =
            (&self.field_system, &self.field_bind_group_layout)
        {
            field_sys.create_particle_bind_group(&self.device, layout)
        } else {
            None
        };

        // Compute pass
        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&self.compute_pipeline);
            compute_pass.set_bind_group(0, &self.compute_bind_group, &[]);

            // Set inbox bind group if enabled (group 1)
            if let Some(ref inbox_bg) = self.inbox_bind_group {
                compute_pass.set_bind_group(1, inbox_bg, &[]);
            } else if self.field_system.is_some() || self.sub_emitter.is_some() || self.adjacency_bind_group.is_some() {
                // Need placeholder at group 1 if we have group 2, 3, or 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(1, empty_bg, &[]);
                }
            }

            // Set field bind group if enabled (group 2)
            if let Some(ref field_bg) = field_bind_group {
                compute_pass.set_bind_group(2, field_bg, &[]);
            } else if self.sub_emitter.is_some() || self.adjacency_bind_group.is_some() {
                // Need placeholder at group 2 if we have group 3 or 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(2, empty_bg, &[]);
                }
            }

            // Set sub-emitter death buffer bind group if enabled (group 3)
            if let Some(ref se) = self.sub_emitter {
                compute_pass.set_bind_group(3, &se.death_bind_group, &[]);
            } else if self.adjacency_bind_group.is_some() {
                // Need placeholder at group 3 if we have group 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(3, empty_bg, &[]);
                }
            }

            // Set adjacency buffer bind group if enabled (group 4)
            if let Some(ref adj_bg) = self.adjacency_bind_group {
                compute_pass.set_bind_group(4, adj_bg, &[]);
            }

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Apply pending particle write AFTER compute pass (so edits aren't overwritten)
        #[cfg(feature = "egui")]
        if let Some(PendingParticleWrite(Some((idx, bytes)))) = pending_particle_write {
            let offset = (idx as usize) * self.particle_stride;
            self.queue.write_buffer(&self.particle_buffer, offset as u64, &bytes);
        }

        // Sub-emitter spawn pass (spawn children from death events)
        if let Some(ref se) = self.sub_emitter {
            se.spawn_children(&mut encoder);
        }

        // Field processing pass (merge deposits, blur/decay, clear write buffer)
        if let Some(ref mut field_sys) = self.field_system {
            field_sys.process(&self.device, &mut encoder, &self.queue, self.time_cache.time, self.time_cache.delta_time);

            // Update volume render bind group after field processing (buffers may have swapped)
            if let Some(ref mut vol) = self.volume_render {
                vol.update_bind_group(&self.device, field_sys);

                // Get camera matrices for ray reconstruction
                let aspect = self.config.width as f32 / self.config.height as f32;
                let proj = Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect, 0.1, 100.0);
                let view = self.camera.view_matrix();
                let view_proj = proj * view;
                let inv_view_proj = view_proj.inverse();
                let camera_pos = self.camera.position();

                // Get field extent and resolution for the rendered field
                let field_idx = vol.field_index;
                let field_extent = field_sys.fields[field_idx].config.world_extent;
                let field_resolution = field_sys.fields[field_idx].config.resolution;

                vol.update_params_with_field(
                    &self.queue,
                    inv_view_proj,
                    camera_pos,
                    field_extent,
                    field_resolution,
                );
            }
        }

        // Trail compute pass (after particles are updated)
        if let Some(ref trail) = self.trail_state {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Trail Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&trail.compute_pipeline);
            compute_pass.set_bind_group(0, &trail.compute_bind_group, &[]);

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Connection compute pass (find pairs within radius)
        if let Some(ref conn) = self.connection_state {
            // Reset connection count to 0
            self.queue.write_buffer(&conn.count_buffer, 0, &[0u8; 4]);

            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Connection Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&conn.compute_pipeline);
            compute_pass.set_bind_group(0, &conn.compute_bind_group, &[]);

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Picking pass (only if there's a pending pick request)
        if self.picking.has_pending_pick() {
            self.picking.render(
                &mut encoder,
                &self.particle_buffer,
                &self.uniform_bind_group,
                self.num_particles,
            );
        }

        // Render pass - render to offscreen texture if post-processing, otherwise to screen
        let render_target = if let Some(ref pp) = self.post_process {
            &pp.view
        } else {
            &view
        };
        let depth_target = if let Some(ref pp) = self.post_process {
            &pp.depth_view
        } else {
            &self.depth_texture
        };

        {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: render_target,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color {
                            r: self.background_color.x as f64,
                            g: self.background_color.y as f64,
                            b: self.background_color.z as f64,
                            a: 1.0,
                        }),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                    view: depth_target,
                    depth_ops: Some(wgpu::Operations {
                        load: wgpu::LoadOp::Clear(1.0),
                        store: wgpu::StoreOp::Store,
                    }),
                    stencil_ops: None,
                }),
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            // Draw connections first (behind everything)
            if let Some(ref conn) = self.connection_state {
                render_pass.set_pipeline(&conn.render_pipeline);
                render_pass.set_bind_group(0, &conn.render_bind_group, &[]);
                // Draw up to max_connections line quads (6 vertices each)
                render_pass.draw(0..6, 0..conn.max_connections);
            }

            // Draw spatial grid (debug visualization) if opacity > 0
            if let Some(ref grid) = self.spatial_grid_viz {
                if grid.opacity > 0.0 {
                    render_pass.set_pipeline(grid.pipeline());
                    render_pass.set_bind_group(0, grid.bind_group(), &[]);
                    // Draw all grid lines (6 vertices per line quad)
                    render_pass.draw(0..6, 0..grid.line_count());
                }
            }

            // Draw trails (behind particles)
            if let Some(ref trail) = self.trail_state {
                render_pass.set_pipeline(&trail.render_pipeline);
                render_pass.set_bind_group(0, &trail.render_bind_group, &[]);
                // Draw all trail points: num_particles * trail_length instances, 6 vertices each
                let total_trail_instances = self.num_particles * trail.trail_length;
                render_pass.draw(0..6, 0..total_trail_instances);
            }

            // Draw particles on top (or wireframe if configured)
            if let Some(ref wireframe) = self.wireframe_state {
                // Render as wireframe meshes
                render_pass.set_pipeline(wireframe.pipeline());
                render_pass.set_bind_group(0, wireframe.bind_group(), &[]);
                // 6 vertices per line quad, total_line_count instances
                render_pass.draw(0..6, 0..wireframe.total_line_count());
            } else {
                // Render as billboards
                render_pass.set_pipeline(&self.render_pipeline);
                render_pass.set_bind_group(0, &self.uniform_bind_group, &[]);
                // Bind textures if available
                if let Some(ref tex_bind_group) = self.texture_bind_group {
                    render_pass.set_bind_group(1, tex_bind_group, &[]);
                }
                render_pass.set_vertex_buffer(0, self.particle_buffer.slice(..));
                render_pass.draw(0..6, 0..self.num_particles);
            }
        }

        // Volume render pass (if enabled) - renders field as volumetric fog/glow
        if let Some(ref vol) = self.volume_render {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Volume Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: render_target,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Load, // Don't clear - blend on top
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None, // No depth for fullscreen volume
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            render_pass.set_pipeline(&vol.pipeline);
            render_pass.set_bind_group(0, &vol.bind_group, &[]);
            render_pass.draw(0..3, 0..1); // Fullscreen triangle
        }

        // Post-processing pass (if enabled)
        if let Some(ref pp) = self.post_process {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Post-Process Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            render_pass.set_pipeline(&pp.pipeline);
            render_pass.set_bind_group(0, &pp.bind_group, &[]);
            render_pass.draw(0..3, 0..1); // Fullscreen triangle
        }

        // Render egui on top of everything (separate render pass for proper blending)
        if let (Some(ref egui), Some(ref egui_out)) = (&self.egui, &egui_output) {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Egui Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Load, // Don't clear - draw over particles
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            // SAFETY: egui-wgpu requires RenderPass<'static> but the pass is used synchronously
            // within this scope and dropped before encoder.finish(). This transmute is safe
            // because the render pass doesn't escape this block.
            let render_pass: &mut wgpu::RenderPass<'static> = unsafe {
                std::mem::transmute(&mut render_pass)
            };
            egui.renderer().render(render_pass, &egui_out.paint_jobs, &screen_descriptor);
        }

        // Copy picked pixel to staging buffer before submit
        self.picking.copy_pixel(&mut encoder);

        // Copy selected particle data if we have a pending read
        if self.picking.needs_particle_data_copy() {
            self.picking.copy_particle_data(&mut encoder, &self.particle_buffer);
        }

        self.queue.submit(std::iter::once(encoder.finish()));
        output.present();

        // Read back picked pixel after submit
        self.picking.read_result(&self.device);

        // Read back particle data if pending
        self.picking.read_particle_data(&self.device);

        // Cleanup egui textures
        if let (Some(ref mut egui), Some(ref egui_out)) = (&mut self.egui, &egui_output) {
            egui.cleanup(egui_out);
        }

        Ok(())
    }

    /// Internal render without egui (used when feature disabled).
    #[cfg(not(feature = "egui"))]
    fn render_internal(&mut self, time: f32, delta_time: f32, custom_uniform_bytes: Option<&[u8]>) -> Result<(), wgpu::SurfaceError> {
        self.update_uniforms(time, delta_time, custom_uniform_bytes);

        let output = self.surface.get_current_texture()?;
        let view = output
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Render Encoder"),
            });

        // Spatial hashing pass (if enabled)
        if let Some(ref spatial) = self.spatial {
            spatial.execute(&mut encoder, &self.queue);
        }

        // Adjacency buffer computation (after spatial hashing)
        if let Some(ref adjacency) = self.adjacency {
            adjacency.execute(&mut encoder);
        }

        // Clear inbox buffer before compute pass
        if let Some(ref inbox_buf) = self.inbox_buffer {
            let inbox_size = (self.num_particles as usize) * 16;
            let zeros = vec![0u8; inbox_size];
            self.queue.write_buffer(inbox_buf, 0, &zeros);
        }

        // Clear sub-emitter death buffers before compute pass
        if let Some(ref se) = self.sub_emitter {
            se.clear_buffers(&self.queue);
        }

        // Recreate field bind group each frame (buffers may have been swapped during blur)
        let field_bind_group = if let (Some(ref field_sys), Some(ref layout)) =
            (&self.field_system, &self.field_bind_group_layout)
        {
            field_sys.create_particle_bind_group(&self.device, layout)
        } else {
            None
        };

        // Compute pass
        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&self.compute_pipeline);
            compute_pass.set_bind_group(0, &self.compute_bind_group, &[]);

            // Set inbox bind group if enabled (group 1)
            if let Some(ref inbox_bg) = self.inbox_bind_group {
                compute_pass.set_bind_group(1, inbox_bg, &[]);
            } else if self.field_system.is_some() || self.sub_emitter.is_some() || self.adjacency_bind_group.is_some() {
                // Need placeholder at group 1 if we have group 2, 3, or 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(1, empty_bg, &[]);
                }
            }

            // Set field bind group if enabled (group 2)
            if let Some(ref field_bg) = field_bind_group {
                compute_pass.set_bind_group(2, field_bg, &[]);
            } else if self.sub_emitter.is_some() || self.adjacency_bind_group.is_some() {
                // Need placeholder at group 2 if we have group 3 or 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(2, empty_bg, &[]);
                }
            }

            // Set sub-emitter death buffer bind group if enabled (group 3)
            if let Some(ref se) = self.sub_emitter {
                compute_pass.set_bind_group(3, &se.death_bind_group, &[]);
            } else if self.adjacency_bind_group.is_some() {
                // Need placeholder at group 3 if we have group 4
                if let Some(ref empty_bg) = self.empty_bind_group {
                    compute_pass.set_bind_group(3, empty_bg, &[]);
                }
            }

            // Set adjacency buffer bind group if enabled (group 4)
            if let Some(ref adj_bg) = self.adjacency_bind_group {
                compute_pass.set_bind_group(4, adj_bg, &[]);
            }

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Sub-emitter spawn pass (spawn children from death events)
        if let Some(ref se) = self.sub_emitter {
            se.spawn_children(&mut encoder);
        }

        // Field processing pass (merge deposits, blur/decay, clear write buffer)
        if let Some(ref mut field_sys) = self.field_system {
            field_sys.process(&self.device, &mut encoder, &self.queue, self.time_cache.time, self.time_cache.delta_time);

            // Update volume render bind group after field processing (buffers may have swapped)
            if let Some(ref mut vol) = self.volume_render {
                vol.update_bind_group(&self.device, field_sys);

                // Get camera matrices for ray reconstruction
                let aspect = self.config.width as f32 / self.config.height as f32;
                let proj = Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect, 0.1, 100.0);
                let view = self.camera.view_matrix();
                let view_proj = proj * view;
                let inv_view_proj = view_proj.inverse();
                let camera_pos = self.camera.position();

                // Get field extent and resolution for the rendered field
                let field_idx = vol.field_index;
                let field_extent = field_sys.fields[field_idx].config.world_extent;
                let field_resolution = field_sys.fields[field_idx].config.resolution;

                vol.update_params_with_field(
                    &self.queue,
                    inv_view_proj,
                    camera_pos,
                    field_extent,
                    field_resolution,
                );
            }
        }

        // Trail compute pass (after particles are updated)
        if let Some(ref trail) = self.trail_state {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Trail Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&trail.compute_pipeline);
            compute_pass.set_bind_group(0, &trail.compute_bind_group, &[]);

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Connection compute pass (find pairs within radius)
        if let Some(ref conn) = self.connection_state {
            // Reset connection count to 0
            self.queue.write_buffer(&conn.count_buffer, 0, &[0u8; 4]);

            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Connection Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&conn.compute_pipeline);
            compute_pass.set_bind_group(0, &conn.compute_bind_group, &[]);

            let workgroups = self.num_particles.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroups, 1, 1);
        }

        // Picking pass (only if there's a pending pick request)
        if self.picking.has_pending_pick() {
            self.picking.render(
                &mut encoder,
                &self.particle_buffer,
                &self.uniform_bind_group,
                self.num_particles,
            );
        }

        // Render pass - render to offscreen texture if post-processing, otherwise to screen
        let render_target = if let Some(ref pp) = self.post_process {
            &pp.view
        } else {
            &view
        };
        let depth_target = if let Some(ref pp) = self.post_process {
            &pp.depth_view
        } else {
            &self.depth_texture
        };

        {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: render_target,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color {
                            r: self.background_color.x as f64,
                            g: self.background_color.y as f64,
                            b: self.background_color.z as f64,
                            a: 1.0,
                        }),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                    view: depth_target,
                    depth_ops: Some(wgpu::Operations {
                        load: wgpu::LoadOp::Clear(1.0),
                        store: wgpu::StoreOp::Store,
                    }),
                    stencil_ops: None,
                }),
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            // Draw connections first (behind everything)
            if let Some(ref conn) = self.connection_state {
                render_pass.set_pipeline(&conn.render_pipeline);
                render_pass.set_bind_group(0, &conn.render_bind_group, &[]);
                // Draw up to max_connections line quads (6 vertices each)
                render_pass.draw(0..6, 0..conn.max_connections);
            }

            // Draw spatial grid (debug visualization) if opacity > 0
            if let Some(ref grid) = self.spatial_grid_viz {
                if grid.opacity > 0.0 {
                    render_pass.set_pipeline(grid.pipeline());
                    render_pass.set_bind_group(0, grid.bind_group(), &[]);
                    // Draw all grid lines (6 vertices per line quad)
                    render_pass.draw(0..6, 0..grid.line_count());
                }
            }

            // Draw trails (behind particles)
            if let Some(ref trail) = self.trail_state {
                render_pass.set_pipeline(&trail.render_pipeline);
                render_pass.set_bind_group(0, &trail.render_bind_group, &[]);
                // Draw all trail points: num_particles * trail_length instances, 6 vertices each
                let total_trail_instances = self.num_particles * trail.trail_length;
                render_pass.draw(0..6, 0..total_trail_instances);
            }

            // Draw particles on top (or wireframe if configured)
            if let Some(ref wireframe) = self.wireframe_state {
                // Render as wireframe meshes
                render_pass.set_pipeline(wireframe.pipeline());
                render_pass.set_bind_group(0, wireframe.bind_group(), &[]);
                // 6 vertices per line quad, total_line_count instances
                render_pass.draw(0..6, 0..wireframe.total_line_count());
            } else {
                // Render as billboards
                render_pass.set_pipeline(&self.render_pipeline);
                render_pass.set_bind_group(0, &self.uniform_bind_group, &[]);
                // Bind textures if available
                if let Some(ref tex_bind_group) = self.texture_bind_group {
                    render_pass.set_bind_group(1, tex_bind_group, &[]);
                }
                render_pass.set_vertex_buffer(0, self.particle_buffer.slice(..));
                render_pass.draw(0..6, 0..self.num_particles);
            }
        }

        // Volume render pass (if enabled) - renders field as volumetric fog/glow
        if let Some(ref vol) = self.volume_render {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Volume Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: render_target,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Load, // Don't clear - blend on top
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None, // No depth for fullscreen volume
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            render_pass.set_pipeline(&vol.pipeline);
            render_pass.set_bind_group(0, &vol.bind_group, &[]);
            render_pass.draw(0..3, 0..1); // Fullscreen triangle
        }

        // Post-processing pass (if enabled)
        if let Some(ref pp) = self.post_process {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Post-Process Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            render_pass.set_pipeline(&pp.pipeline);
            render_pass.set_bind_group(0, &pp.bind_group, &[]);
            render_pass.draw(0..3, 0..1); // Fullscreen triangle
        }

        // Copy picked pixel to staging buffer before submit
        self.picking.copy_pixel(&mut encoder);

        // Copy selected particle data if we have a pending read
        if self.picking.needs_particle_data_copy() {
            self.picking.copy_particle_data(&mut encoder, &self.particle_buffer);
        }

        self.queue.submit(std::iter::once(encoder.finish()));
        output.present();

        // Read back picked pixel after submit
        self.picking.read_result(&self.device);

        // Read back particle data if pending
        self.picking.read_particle_data(&self.device);

        Ok(())
    }
}

fn create_depth_texture(
    device: &wgpu::Device,
    config: &wgpu::SurfaceConfiguration,
) -> wgpu::TextureView {
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("Depth Texture"),
        size: wgpu::Extent3d {
            width: config.width,
            height: config.height,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: DEPTH_FORMAT,
        usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
        view_formats: &[],
    });
    texture.create_view(&wgpu::TextureViewDescriptor::default())
}