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
//! Vertex buffer with dynamic layout. See [`VertexBuffer`] docs for more info and usage examples.
use crate::{
core::{
algebra::{Vector2, Vector3, Vector4},
arrayvec::ArrayVec,
byteorder::{ByteOrder, LittleEndian},
futures::io::Error,
math::TriangleDefinition,
visitor::{prelude::*, PodVecView},
},
core::{array_as_u8_slice, value_as_u8_slice},
};
use fxhash::FxHasher;
use std::{
alloc::Layout,
fmt::{Display, Formatter},
hash::{Hash, Hasher},
marker::PhantomData,
mem::MaybeUninit,
ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
vec::Drain,
};
/// A common trait for all vertex types. **IMPORTANT:** Implementors **must** use `#[repr(C)]` attribute, otherwise the compiler
/// is free to reorder fields and you might get weird results, because definition order will be different from memory order! See
/// examples in [`VertexBuffer`] docs.
pub trait VertexTrait: Copy + 'static {
/// Returns memory layout of the vertex. It basically tells a GPU how to interpret every byte range
/// of your vertex type; which kind of information it holds.
fn layout() -> &'static [VertexAttributeDescriptor];
}
/// Data type for a vertex attribute component.
#[derive(Copy, Clone, PartialOrd, PartialEq, Eq, Ord, Hash, Visit, Debug)]
#[repr(u8)]
pub enum VertexAttributeDataType {
/// 32-bit floating-point.
F32,
/// 32-bit unsigned integer.
U32,
/// 16-bit unsigned integer.
U16,
/// 8-bit unsigned integer.
U8,
}
impl Default for VertexAttributeDataType {
fn default() -> Self {
Self::F32
}
}
impl VertexAttributeDataType {
/// Returns size of data in bytes.
pub fn size(self) -> u8 {
match self {
VertexAttributeDataType::F32 | VertexAttributeDataType::U32 => 4,
VertexAttributeDataType::U16 => 2,
VertexAttributeDataType::U8 => 1,
}
}
}
/// An usage for vertex attribute. It is a fixed set, but there are plenty
/// room for any custom data - it may be fit into `TexCoordN` attributes.
#[derive(Copy, Clone, PartialOrd, PartialEq, Eq, Ord, Hash, Visit, Debug)]
#[repr(u32)]
pub enum VertexAttributeUsage {
/// Vertex position. Usually `Vector2<f32>` or `Vector3<f32>`.
Position = 0,
/// Vertex normal. Usually `Vector3<f32>`, more rare `Vector3<u16>` (F16).
Normal = 1,
/// Vertex tangent. Usually `Vector3<f32>`.
Tangent = 2,
/// First texture coordinates. Usually `Vector2<f32>`.
/// It may be used for everything else, not only for texture coordinates.
TexCoord0 = 3,
/// Second texture coordinates.
TexCoord1 = 4,
/// Third texture coordinates.
TexCoord2 = 5,
/// Fourth texture coordinates.
TexCoord3 = 6,
/// Fifth texture coordinates.
TexCoord4 = 7,
/// Sixth texture coordinates.
TexCoord5 = 8,
/// Seventh texture coordinates.
TexCoord6 = 9,
/// Eighth texture coordinates.
TexCoord7 = 10,
/// Bone weights. Usually `Vector4<f32>`.
BoneWeight = 11,
/// Bone indices. Usually `Vector4<u8>`.
BoneIndices = 12,
/// Color. Usually `Vector4<u8>`.
Color = 13,
/// First custom attribute with arbitrary, context-dependent meaning.
Custom0 = 14,
/// Second custom attribute with arbitrary, context-dependent meaning.
Custom1 = 15,
/// Third custom attribute with arbitrary, context-dependent meaning.
Custom2 = 16,
/// Fourth custom attribute with arbitrary, context-dependent meaning.
Custom3 = 17,
/// Fifth custom attribute with arbitrary, context-dependent meaning.
Custom4 = 18,
/// Sixth custom attribute with arbitrary, context-dependent meaning.
Custom5 = 19,
/// Seventh custom attribute with arbitrary, context-dependent meaning.
Custom6 = 20,
/// Eigth custom attribute with arbitrary, context-dependent meaning.
Custom7 = 21,
/// Maximum amount of attribute kinds.
Count,
}
impl Default for VertexAttributeUsage {
fn default() -> Self {
Self::Position
}
}
/// Input vertex attribute descriptor used to construct layouts and feed vertex buffer.
#[derive(Debug, Hash)]
pub struct VertexAttributeDescriptor {
/// Claimed usage of the attribute. It could be Position, Normal, etc.
pub usage: VertexAttributeUsage,
/// Data type of every component of the attribute. It could be F32, U32, U16, etc.
pub data_type: VertexAttributeDataType,
/// Size of attribute expressed in components. For example, for `Position` it could
/// be 3 - which means there are 3 components in attribute of `data_type`.
pub size: u8,
/// Sets a "fetch rate" for vertex shader at which it will read vertex attribute:
/// 0 - per vertex (default)
/// 1 - per instance
/// 2 - per 2 instances and so on.
pub divisor: u8,
/// Defines location of the attribute in a shader (`layout(location = x) attrib;`)
pub shader_location: u8,
/// Whether the attribute values should be normalized into `0.0..1.0` range or not.
/// If this field is set to `false`, then the numbers will appear "as-is" when fetching
/// them in a shader. On the other hand, if it is `true`, then any numeric value will be
/// normalized by applying `normalized = num / T::max()` equation. This way all numbers will
/// always stay in `0.0..1.0` range.
///
/// For example, normalization could be useful for RGB colors that expressed as three bytes (u8).
/// In this case normalization will turn the color into `0.0..1.0` range.
pub normalized: bool,
}
/// Vertex attribute is a simple "bridge" between raw data and its interpretation. In
/// other words it defines how to treat raw data in vertex shader.
#[derive(Visit, Copy, Clone, Default, Debug, Hash)]
pub struct VertexAttribute {
/// Claimed usage of the attribute. It could be Position, Normal, etc.
pub usage: VertexAttributeUsage,
/// Data type of every component of the attribute. It could be F32, U32, U16, etc.
pub data_type: VertexAttributeDataType,
/// Size of attribute expressed in components. For example, for `Position` it could
/// be 3 - which means there are 3 components in attribute of `data_type`.
pub size: u8,
/// Sets a "fetch rate" for vertex shader at which it will read vertex attribute:
/// 0 - per vertex (default)
/// 1 - per instance
/// 2 - per 2 instances and so on.
pub divisor: u8,
/// Offset in bytes from beginning of the vertex.
pub offset: u8,
/// Defines location of the attribute in a shader (`layout(location = x) attrib;`)
pub shader_location: u8,
/// Whether the attribute values should be normalized into `0.0..1.0` range or not.
/// If this field is set to `false`, then the numbers will appear "as-is" when fetching
/// them in a shader. On the other hand, if it is `true`, then any numeric value will be
/// normalized by applying `normalized = num / T::max()` equation. This way all numbers will
/// always stay in `0.0..1.0` range.
///
/// For example, normalization could be useful for RGB colors that expressed as three bytes (u8).
/// In this case normalization will turn the color into `0.0..1.0` range.
#[visit(optional)]
pub normalized: bool,
}
/// Bytes storage of a vertex buffer.
#[derive(Clone, Debug)]
pub struct BytesStorage {
bytes: Vec<u8>,
layout: Layout,
}
impl Visit for BytesStorage {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut bytes_adapter = PodVecView::from_pod_vec(&mut self.bytes);
if bytes_adapter.visit(name, visitor).is_err() {
let mut bytes = Vec::<u8>::new();
bytes.visit(name, visitor)?;
self.bytes = bytes;
}
if visitor.is_reading() {
self.layout = Layout::array::<u8>(self.bytes.capacity()).unwrap();
}
Ok(())
}
}
impl Default for BytesStorage {
fn default() -> Self {
Self {
bytes: Default::default(),
layout: Layout::array::<u8>(0).unwrap(),
}
}
}
impl BytesStorage {
/// Creates new empty bytes storage with the given capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
bytes: Vec::with_capacity(capacity),
layout: Layout::array::<u8>(capacity).unwrap(),
}
}
/// Creates new bytes storage from the given data buffer.
pub fn new<T>(data: Vec<T>) -> Self {
// Prevent destructor to be called on `data`, this is needed because we're taking its
// data storage and treat it as a simple bytes block.
let mut data = std::mem::ManuallyDrop::new(data);
let bytes_length = data.len() * std::mem::size_of::<T>();
let bytes_capacity = data.capacity() * std::mem::size_of::<T>();
Self {
bytes: unsafe {
Vec::<u8>::from_raw_parts(
data.as_mut_ptr() as *mut u8,
bytes_length,
bytes_capacity,
)
},
// Preserve initial memory layout, to ensure that the memory block will be deallocated
// with initial memory layout.
layout: Layout::array::<T>(data.capacity()).unwrap(),
}
}
fn extend_from_slice(&mut self, slice: &[u8]) {
if self.layout.align() != 1 {
// Realloc backing storage manually if the alignment is anything else than 1.
let new_storage = Vec::with_capacity(self.bytes.len());
let old_storage = std::mem::replace(&mut self.bytes, new_storage);
self.bytes.extend_from_slice(old_storage.as_slice());
self.layout = Layout::array::<u8>(self.bytes.capacity()).unwrap();
}
self.bytes.extend_from_slice(slice);
self.layout = Layout::array::<u8>(self.bytes.capacity()).unwrap();
}
fn drain<R>(&mut self, range: R) -> Drain<'_, u8>
where
R: RangeBounds<usize>,
{
self.bytes.drain(range)
}
fn as_mut_ptr(&mut self) -> *mut u8 {
self.bytes.as_mut_ptr()
}
fn as_slice_mut(&mut self) -> &mut [u8] {
self.bytes.as_mut_slice()
}
fn clear(&mut self) {
self.bytes.clear()
}
}
impl Drop for BytesStorage {
fn drop(&mut self) {
let mut bytes = std::mem::ManuallyDrop::new(std::mem::take(&mut self.bytes));
// Dealloc manually with initial memory layout.
if bytes.capacity() != 0 {
unsafe { std::alloc::dealloc(bytes.as_mut_ptr(), self.layout) }
}
}
}
impl Deref for BytesStorage {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.bytes
}
}
/// Vertex buffer with dynamic layout. It is used to store multiple vertices of a single type, that implements [`VertexTrait`].
/// Different vertex types used to for efficient memory usage. For example, you could have a simple vertex with only position
/// expressed as Vector3 and it will be enough for simple cases, when only position is required. However, if you want to draw
/// a mesh with skeletal animation, that also supports texturing, lighting, you need to provide a lot more data (bone indices,
/// bone weights, normals, tangents, texture coordinates).
///
/// ## Examples
///
/// ```rust
/// # use fyrox_impl::{
/// # core::algebra::Vector3,
/// # scene::mesh::buffer::{
/// # VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage, VertexBuffer,
/// # VertexTrait,
/// # },
/// # };
/// #
/// #[derive(Copy, Clone)]
/// #[repr(C)]
/// struct MyVertex {
/// position: Vector3<f32>,
/// }
///
/// impl VertexTrait for MyVertex {
/// fn layout() -> &'static [VertexAttributeDescriptor] {
/// &[VertexAttributeDescriptor {
/// usage: VertexAttributeUsage::Position,
/// data_type: VertexAttributeDataType::F32,
/// size: 3,
/// divisor: 0,
/// shader_location: 0,
/// normalized: false
/// }]
/// }
/// }
///
/// fn create_triangle_vertex_buffer() -> VertexBuffer {
/// VertexBuffer::new(
/// 3,
/// vec![
/// MyVertex {
/// position: Vector3::new(0.0, 0.0, 0.0),
/// },
/// MyVertex {
/// position: Vector3::new(0.0, 1.0, 0.0),
/// },
/// MyVertex {
/// position: Vector3::new(1.0, 1.0, 0.0),
/// },
/// ],
/// )
/// .unwrap()
/// }
/// ```
///
/// This example creates a simple vertex buffer that contains a single triangle with custom vertex format. The most important
/// part here is [`VertexTrait::layout`] implementation - it describes each "attribute" of your vertex, if your layout does not
/// match the actual content of the vertex (in terms of size in bytes), then vertex buffer cannot be created and [`VertexBuffer::new`]
/// will return [`None`].
///
/// The second, but not least important is `#[repr(C)]` attribute - it is mandatory for every vertex type, it forbids fields
/// reordering of you vertex structure and guarantees that they will have the same layout in memory as their declaration order.
///
/// ## Limitations
///
/// Vertex size cannot be more than 256 bytes, this limitation shouldn't be a problem because almost every GPU supports up to
/// 16 vertex attributes with 16 bytes of size each, which gives exactly 256 bytes.
#[derive(Clone, Visit, Default, Debug)]
pub struct VertexBuffer {
dense_layout: Vec<VertexAttribute>,
sparse_layout: [Option<VertexAttribute>; VertexAttributeUsage::Count as usize],
vertex_size: u8,
vertex_count: u32,
data: BytesStorage,
#[visit(optional)]
layout_hash: u64,
#[visit(optional)]
modifications_counter: u64,
}
fn calculate_layout_hash(layout: &[VertexAttribute]) -> u64 {
let mut hasher = FxHasher::default();
layout.hash(&mut hasher);
hasher.finish()
}
fn calculate_data_hash(data: &[u8]) -> u64 {
let mut hasher = FxHasher::default();
data.hash(&mut hasher);
hasher.finish()
}
/// See VertexBuffer::modify for more info.
pub struct VertexBufferRefMut<'a> {
vertex_buffer: &'a mut VertexBuffer,
}
impl<'a> Drop for VertexBufferRefMut<'a> {
fn drop(&mut self) {
self.vertex_buffer.modifications_counter += 1;
}
}
impl<'a> Deref for VertexBufferRefMut<'a> {
type Target = VertexBuffer;
fn deref(&self) -> &Self::Target {
self.vertex_buffer
}
}
impl<'a> DerefMut for VertexBufferRefMut<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.vertex_buffer
}
}
impl<'a> VertexBufferRefMut<'a> {
/// Tries to append a vertex to the buffer.
///
/// # Safety and validation
///
/// This method accepts any type that has appropriate size, the size must be equal
/// with the size defined by layout. The Copy trait bound is required to ensure that
/// the type does not have any custom destructors.
pub fn push_vertex<T>(&mut self, vertex: &T) -> Result<(), ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
self.vertex_buffer
.data
.extend_from_slice(value_as_u8_slice(vertex));
self.vertex_buffer.vertex_count += 1;
Ok(())
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Tries to append a slice of vertices to the buffer.
///
/// # Safety and validation
///
/// This method accepts any type that has appropriate size, the size must be equal
/// with the size defined by layout. The Copy trait bound is required to ensure that
/// the type does not have any custom destructors.
pub fn push_vertices<T>(&mut self, vertices: &[T]) -> Result<(), ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
self.vertex_buffer
.data
.extend_from_slice(array_as_u8_slice(vertices));
self.vertex_buffer.vertex_count += vertices.len() as u32;
Ok(())
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Tries to append a raw vertex data to the vertex buffer. This method will fail if the `data`
/// size does not match the vertex size of the buffer.
pub fn push_vertex_raw(&mut self, data: &[u8]) -> Result<(), ValidationError> {
if data.len() == self.vertex_buffer.vertex_size as usize {
self.vertex_buffer.data.extend_from_slice(data);
self.vertex_buffer.vertex_count += 1;
Ok(())
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: data.len() as u8,
})
}
}
/// Tries to append the vertices that the given iterator produces.
///
/// # Safety and validation
///
/// This method accepts any type that has appropriate size, the size must be equal
/// with the size defined by layout. The Copy trait bound is required to ensure that
/// the type does not have any custom destructors.
pub fn push_vertices_iter<T>(
&mut self,
vertices: impl Iterator<Item = T>,
) -> Result<(), ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
for vertex in vertices {
self.vertex_buffer
.data
.extend_from_slice(value_as_u8_slice(&vertex));
self.vertex_buffer.vertex_count += 1;
}
Ok(())
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Tries to append a slice of vertices to the buffer. Each vertex will be transformed using
/// `transformer` callback.
///
/// # Safety and validation
///
/// This method accepts any type that has appropriate size, the size must be equal
/// with the size defined by layout. The Copy trait bound is required to ensure that
/// the type does not have any custom destructors.
pub fn push_vertices_transform<T, F>(
&mut self,
vertices: &[T],
mut transformer: F,
) -> Result<(), ValidationError>
where
T: VertexTrait,
F: FnMut(&T) -> T,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
for vertex in vertices {
let transformed = transformer(vertex);
self.vertex_buffer
.data
.extend_from_slice(value_as_u8_slice(&transformed));
}
self.vertex_buffer.vertex_count += vertices.len() as u32;
Ok(())
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Removes last vertex from the buffer.
pub fn remove_last_vertex(&mut self) {
let range = (self.vertex_buffer.data.len() - self.vertex_buffer.vertex_size as usize)..;
self.vertex_buffer.data.drain(range);
self.vertex_buffer.vertex_count -= 1;
}
/// Copies data of last vertex from the buffer to an instance of variable of a type.
///
/// # Safety and validation
///
/// This method accepts any type that has appropriate size, the size must be equal
/// with the size defined by layout. The Copy trait bound is required to ensure that
/// the type does not have any custom destructors.
pub fn pop_vertex<T>(&mut self) -> Result<T, ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize
&& self.vertex_buffer.data.len() >= self.vertex_buffer.vertex_size as usize
{
unsafe {
let mut v = MaybeUninit::<T>::uninit();
std::ptr::copy_nonoverlapping(
self.vertex_buffer.data.as_ptr().add(
self.vertex_buffer.data.len() - self.vertex_buffer.vertex_size as usize,
),
v.as_mut_ptr() as *mut u8,
self.vertex_buffer.vertex_size as usize,
);
let range =
(self.vertex_buffer.data.len() - self.vertex_buffer.vertex_size as usize)..;
self.vertex_buffer.data.drain(range);
self.vertex_buffer.vertex_count -= 1;
Ok(v.assume_init())
}
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Tries to cast internal data buffer to a slice of given type. It may fail if
/// size of type is not equal with claimed size (which is set by the layout).
pub fn cast_data_mut<T>(&mut self) -> Result<&mut [T], ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
Ok(unsafe {
std::slice::from_raw_parts_mut(
self.vertex_buffer.data.as_mut_ptr() as *const T as *mut T,
self.vertex_buffer.data.len() / std::mem::size_of::<T>(),
)
})
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_buffer.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Creates iterator that emits read/write accessors for vertices.
pub fn iter_mut(&mut self) -> impl Iterator<Item = VertexViewMut<'_>> + '_ {
unsafe {
VertexViewMutIterator {
ptr: self.vertex_buffer.data.as_mut_ptr(),
end: self.data.as_mut_ptr().add(
self.vertex_buffer.vertex_size as usize
* self.vertex_buffer.vertex_count as usize,
),
vertex_size: self.vertex_buffer.vertex_size,
sparse_layout: &self.vertex_buffer.sparse_layout,
marker: PhantomData,
}
}
}
/// Returns a read/write accessor of n-th vertex.
pub fn get_mut(&mut self, n: usize) -> Option<VertexViewMut<'_>> {
let offset = n * self.vertex_buffer.vertex_size as usize;
if offset < self.vertex_buffer.data.len() {
Some(VertexViewMut {
vertex_data: &mut self.vertex_buffer.data.as_slice_mut()
[offset..(offset + self.vertex_buffer.vertex_size as usize)],
sparse_layout: &self.vertex_buffer.sparse_layout,
})
} else {
None
}
}
/// Duplicates n-th vertex and puts it at the back of the buffer.
pub fn duplicate(&mut self, n: usize) {
// Vertex cannot be larger than 256 bytes, so having temporary array of
// such size is ok.
let mut temp = ArrayVec::<u8, 256>::new();
temp.try_extend_from_slice(
&self.vertex_buffer.data[(n * self.vertex_buffer.vertex_size as usize)
..((n + 1) * self.vertex_buffer.vertex_size as usize)],
)
.unwrap();
self.vertex_buffer.data.extend_from_slice(temp.as_slice());
self.vertex_buffer.vertex_count += 1;
}
/// Adds new attribute at the end of layout, reorganizes internal data storage to be
/// able to contain new attribute. Default value of the new attribute in the buffer
/// becomes `fill_value`. Graphically this could be represented like so:
///
/// Add secondary texture coordinates:
/// Before: P1_N1_TC1_P2_N2_TC2...
/// After: P1_N1_TC1_TC2(fill_value)_P2_N2_TC2_TC2(fill_value)...
pub fn add_attribute<T>(
&mut self,
descriptor: VertexAttributeDescriptor,
fill_value: T,
) -> Result<(), ValidationError>
where
T: Copy,
{
if self.vertex_buffer.sparse_layout[descriptor.usage as usize].is_some() {
Err(ValidationError::DuplicatedAttributeDescriptor)
} else {
let vertex_attribute = VertexAttribute {
usage: descriptor.usage,
data_type: descriptor.data_type,
size: descriptor.size,
divisor: descriptor.divisor,
offset: self.vertex_buffer.vertex_size,
shader_location: descriptor.shader_location,
normalized: descriptor.normalized,
};
self.vertex_buffer.sparse_layout[descriptor.usage as usize] = Some(vertex_attribute);
self.vertex_buffer.dense_layout.push(vertex_attribute);
self.layout_hash = calculate_layout_hash(&self.vertex_buffer.dense_layout);
let mut new_data = Vec::new();
for chunk in self
.vertex_buffer
.data
.chunks_exact(self.vertex_buffer.vertex_size as usize)
{
let mut temp = ArrayVec::<u8, 256>::new();
temp.try_extend_from_slice(chunk).unwrap();
temp.try_extend_from_slice(value_as_u8_slice(&fill_value))
.unwrap();
new_data.extend_from_slice(&temp);
}
self.vertex_buffer.data = BytesStorage::new(new_data);
self.vertex_buffer.vertex_size += std::mem::size_of::<T>() as u8;
Ok(())
}
}
/// Clears the buffer making it empty.
pub fn clear(&mut self) {
self.data.clear();
self.vertex_count = 0;
}
}
/// An error that may occur during input data and layout validation.
#[derive(Debug)]
pub enum ValidationError {
/// Attribute size must be either 1, 2, 3 or 4.
InvalidAttributeSize(usize),
/// Data size is not correct.
InvalidDataSize {
/// Expected data size in bytes.
expected: usize,
/// Actual data size in bytes.
actual: usize,
},
/// Trying to add vertex of incorrect size.
InvalidVertexSize {
/// Expected vertex size.
expected: u8,
/// Actual vertex size.
actual: u8,
},
/// A duplicate of a descriptor was found.
DuplicatedAttributeDescriptor,
/// Duplicate shader locations were found.
ConflictingShaderLocations(usize),
}
impl Display for ValidationError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ValidationError::InvalidAttributeSize(v) => {
write!(f, "Invalid attribute size {v}. Must be either 1, 2, 3 or 4")
}
ValidationError::InvalidDataSize { expected, actual } => {
write!(f, "Invalid data size. Expected {expected}, got {actual}.")
}
ValidationError::InvalidVertexSize { expected, actual } => {
write!(f, "Invalid vertex size. Expected {expected}, got {actual}.",)
}
ValidationError::DuplicatedAttributeDescriptor => {
write!(f, "A duplicate of a descriptor was found.")
}
ValidationError::ConflictingShaderLocations(v) => {
write!(f, "Duplicate shader locations were found {v}.")
}
}
}
}
impl VertexBuffer {
/// Creates new vertex buffer from provided data and with the given layout of the vertex type `T`.
pub fn new<T>(vertex_count: usize, data: Vec<T>) -> Result<Self, ValidationError>
where
T: VertexTrait,
{
Self::new_with_layout(T::layout(), vertex_count, BytesStorage::new(data))
}
/// Creates new vertex buffer from the given layout, vertex count and bytes storage.
pub fn new_with_layout(
layout: &[VertexAttributeDescriptor],
vertex_count: usize,
bytes: BytesStorage,
) -> Result<Self, ValidationError> {
// Validate for duplicates and invalid layout.
for descriptor in layout {
for other_descriptor in layout {
if !std::ptr::eq(descriptor, other_descriptor) {
if descriptor.usage == other_descriptor.usage {
return Err(ValidationError::DuplicatedAttributeDescriptor);
} else if descriptor.shader_location == other_descriptor.shader_location {
return Err(ValidationError::ConflictingShaderLocations(
descriptor.shader_location as usize,
));
}
}
}
}
let mut dense_layout = Vec::new();
// Validate everything as much as possible and calculate vertex size.
let mut sparse_layout = [None; VertexAttributeUsage::Count as usize];
let mut vertex_size_bytes = 0u8;
for attribute in layout.iter() {
if attribute.size < 1 || attribute.size > 4 {
return Err(ValidationError::InvalidAttributeSize(
attribute.size as usize,
));
}
let vertex_attribute = VertexAttribute {
usage: attribute.usage,
data_type: attribute.data_type,
size: attribute.size,
divisor: attribute.divisor,
offset: vertex_size_bytes,
shader_location: attribute.shader_location,
normalized: attribute.normalized,
};
dense_layout.push(vertex_attribute);
// Map dense to sparse layout to increase performance.
sparse_layout[attribute.usage as usize] = Some(vertex_attribute);
vertex_size_bytes += attribute.size * attribute.data_type.size();
}
let expected_data_size = vertex_count * vertex_size_bytes as usize;
if expected_data_size != bytes.len() {
return Err(ValidationError::InvalidDataSize {
expected: expected_data_size,
actual: bytes.len(),
});
}
Ok(Self {
vertex_size: vertex_size_bytes,
vertex_count: vertex_count as u32,
modifications_counter: 0,
data: bytes,
layout_hash: calculate_layout_hash(&dense_layout),
sparse_layout,
dense_layout,
})
}
/// Creates a new empty vertex buffer with the same layout and vertex size, but with an empty
/// inner buffer of the specified capacity.
pub fn clone_empty(&self, capacity: usize) -> Self {
Self {
dense_layout: self.dense_layout.clone(),
sparse_layout: self.sparse_layout,
vertex_size: self.vertex_size,
vertex_count: 0,
data: BytesStorage::with_capacity(capacity),
layout_hash: self.layout_hash,
modifications_counter: 0,
}
}
/// Returns a reference to underlying data buffer slice.
pub fn raw_data(&self) -> &[u8] {
&self.data
}
/// Returns true if buffer does not contain any vertex, false - otherwise.
pub fn is_empty(&self) -> bool {
self.vertex_count == 0
}
/// Returns the total amount of times the buffer was modified.
pub fn modifications_count(&self) -> u64 {
self.modifications_counter
}
/// Calculates inner data hash.
pub fn content_hash(&self) -> u64 {
calculate_data_hash(&self.data.bytes)
}
/// Returns hash of vertex buffer layout. Cached value is guaranteed to be in actual state.
/// The hash could be used to check if the layout has changed.
pub fn layout_hash(&self) -> u64 {
self.layout_hash
}
/// Provides mutable access to content of the buffer.
///
/// # Performance
///
/// This method returns special structure which has custom destructor that
/// calculates hash of the data once modification is over. You **must** hold
/// this structure as long as possible while modifying contents of the buffer.
/// Do **not** even try to do this:
///
/// ```no_run
/// use fyrox_impl::{
/// scene::mesh::buffer::{VertexBuffer, VertexWriteTrait, VertexAttributeUsage},
/// core::algebra::Vector3
/// };
/// fn do_something(buffer: &mut VertexBuffer) {
/// for i in 0..buffer.vertex_count() {
/// buffer
/// .modify() // Doing this in a loop will cause HUGE performance issues!
/// .get_mut(i as usize)
/// .unwrap()
/// .write_3_f32(VertexAttributeUsage::Position, Vector3::<f32>::default())
/// .unwrap();
/// }
/// }
/// ```
///
/// Instead do this:
///
/// ```no_run
/// use fyrox_impl::{
/// scene::mesh::buffer::{VertexBuffer, VertexWriteTrait, VertexAttributeUsage},
/// core::algebra::Vector3
/// };
/// fn do_something(buffer: &mut VertexBuffer) {
/// let mut buffer_modifier = buffer.modify();
/// for mut vertex in buffer_modifier.iter_mut() {
/// vertex
/// .write_3_f32(VertexAttributeUsage::Position, Vector3::<f32>::default())
/// .unwrap();
/// }
/// }
/// ```
///
/// Why do we even need such complications? It is used for lazy hash calculation which is
/// used for automatic upload of contents to GPU in case if content has changed.
pub fn modify(&mut self) -> VertexBufferRefMut<'_> {
VertexBufferRefMut {
vertex_buffer: self,
}
}
/// Checks if an attribute of `usage` exists.
pub fn has_attribute(&self, usage: VertexAttributeUsage) -> bool {
self.sparse_layout[usage as usize].is_some()
}
/// Returns vertex buffer layout.
pub fn layout(&self) -> &[VertexAttribute] {
&self.dense_layout
}
/// Returns vertex buffer layout.
pub fn layout_descriptor(&self) -> impl Iterator<Item = VertexAttributeDescriptor> + '_ {
self.dense_layout
.iter()
.map(|attrib| VertexAttributeDescriptor {
usage: attrib.usage,
data_type: attrib.data_type,
size: attrib.size,
divisor: attrib.divisor,
shader_location: attrib.shader_location,
normalized: attrib.normalized,
})
}
/// Tries to cast internal data buffer to a slice of given type. It may fail if
/// size of type is not equal with claimed size (which is set by the layout).
pub fn cast_data_ref<T>(&self) -> Result<&[T], ValidationError>
where
T: VertexTrait,
{
if std::mem::size_of::<T>() == self.vertex_size as usize {
Ok(unsafe {
std::slice::from_raw_parts(
self.data.as_ptr() as *const T,
self.data.len() / std::mem::size_of::<T>(),
)
})
} else {
Err(ValidationError::InvalidVertexSize {
expected: self.vertex_size,
actual: std::mem::size_of::<T>() as u8,
})
}
}
/// Creates iterator that emits read accessors for vertices.
pub fn iter(&self) -> impl Iterator<Item = VertexViewRef<'_>> + '_ {
VertexViewRefIterator {
data: &self.data,
offset: 0,
end: self.vertex_size as usize * self.vertex_count as usize,
vertex_size: self.vertex_size,
sparse_layout: &self.sparse_layout,
}
}
/// Returns a read accessor of n-th vertex.
pub fn get(&self, n: usize) -> Option<VertexViewRef<'_>> {
let offset = n * self.vertex_size as usize;
if offset < self.data.len() {
Some(VertexViewRef {
vertex_data: &self.data[offset..(offset + self.vertex_size as usize)],
sparse_layout: &self.sparse_layout,
})
} else {
None
}
}
/// Returns exact amount of vertices in the buffer.
pub fn vertex_count(&self) -> u32 {
self.vertex_count
}
/// Return vertex size of the buffer.
pub fn vertex_size(&self) -> u8 {
self.vertex_size
}
/// Finds free location for an attribute in the layout.
pub fn find_free_shader_location(&self) -> u8 {
let mut location = None;
for attribute in self.dense_layout.chunks_exact(2) {
let left = &attribute[0];
let right = &attribute[1];
if (left.shader_location as i32 - right.shader_location as i32).abs() > 1 {
// We have a gap, use some value from it.
let origin = left.shader_location.min(right.shader_location);
location = Some(origin + 1);
break;
}
}
location.unwrap_or_else(|| {
self.dense_layout
.last()
.map(|a| a.shader_location)
.unwrap_or(0)
+ 1
})
}
/// Tries to find an attribute with the given `usage` and if it exists, returns its "view", that
/// allows you to fetch data like in ordinary array.
#[inline]
pub fn attribute_view<T>(&self, usage: VertexAttributeUsage) -> Option<AttributeViewRef<'_, T>>
where
T: Copy,
{
self.dense_layout
.iter()
.find(|attribute| {
attribute.usage == usage
&& attribute.size * attribute.data_type.size() == std::mem::size_of::<T>() as u8
})
.map(|attribute| AttributeViewRef {
ptr: unsafe { self.data.bytes.as_ptr().add(attribute.offset as usize) },
stride: self.vertex_size as usize,
count: self.vertex_count as usize,
phantom: Default::default(),
})
}
/// Tries to find an attribute with the given `usage` and if it exists, returns its "view", that
/// allows you to fetch data like in ordinary array.
#[inline]
pub fn attribute_view_mut<T: Copy>(
&mut self,
usage: VertexAttributeUsage,
) -> Option<AttributeViewRef<'_, T>> {
if let Some(attribute) = self.dense_layout.iter().find(|attribute| {
attribute.usage == usage
&& attribute.size * attribute.data_type.size() == std::mem::size_of::<T>() as u8
}) {
Some(AttributeViewRef {
ptr: unsafe { self.data.bytes.as_mut_ptr().add(attribute.offset as usize) },
stride: self.vertex_size as usize,
count: self.vertex_count as usize,
phantom: Default::default(),
})
} else {
None
}
}
}
struct VertexViewRefIterator<'a> {
data: &'a [u8],
sparse_layout: &'a [Option<VertexAttribute>],
offset: usize,
end: usize,
vertex_size: u8,
}
impl<'a> Iterator for VertexViewRefIterator<'a> {
type Item = VertexViewRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.end {
None
} else {
let view = VertexViewRef {
vertex_data: &self.data[self.offset..(self.offset + self.vertex_size as usize)],
sparse_layout: self.sparse_layout,
};
self.offset += self.vertex_size as usize;
Some(view)
}
}
}
struct VertexViewMutIterator<'a> {
ptr: *mut u8,
sparse_layout: &'a [Option<VertexAttribute>],
end: *mut u8,
vertex_size: u8,
marker: PhantomData<&'a mut u8>,
}
impl<'a> Iterator for VertexViewMutIterator<'a> {
type Item = VertexViewMut<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.ptr >= self.end {
None
} else {
unsafe {
let data = std::slice::from_raw_parts_mut(self.ptr, self.vertex_size as usize);
let view = VertexViewMut {
vertex_data: data,
sparse_layout: self.sparse_layout,
};
self.ptr = self.ptr.add(self.vertex_size as usize);
Some(view)
}
}
}
}
/// Read accessor for a vertex with some layout.
#[derive(Debug)]
pub struct VertexViewRef<'a> {
vertex_data: &'a [u8],
sparse_layout: &'a [Option<VertexAttribute>],
}
impl<'a> PartialEq for VertexViewRef<'a> {
fn eq(&self, other: &Self) -> bool {
self.vertex_data == other.vertex_data
}
}
/// Read/write accessor for a vertex with some layout.
#[derive(Debug)]
pub struct VertexViewMut<'a> {
vertex_data: &'a mut [u8],
sparse_layout: &'a [Option<VertexAttribute>],
}
impl<'a> PartialEq for VertexViewMut<'a> {
fn eq(&self, other: &Self) -> bool {
self.vertex_data == other.vertex_data
}
}
/// An error that may occur during fetching using vertex read/write accessor.
#[derive(Debug)]
pub enum VertexFetchError {
/// Trying to read/write non-existent attribute.
NoSuchAttribute(VertexAttributeUsage),
/// Size mistmatch.
SizeMismatch {
/// Expected size in bytes.
expected: u8,
/// Actual size in bytes.
actual: u8,
},
/// IO error.
Io(std::io::Error),
}
impl std::error::Error for VertexFetchError {}
impl Display for VertexFetchError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
VertexFetchError::NoSuchAttribute(v) => {
write!(f, "No attribute with such usage: {v:?}")
}
VertexFetchError::Io(v) => {
write!(f, "An i/o error has occurred {v:?}")
}
VertexFetchError::SizeMismatch { expected, actual } => {
write!(f, "Size mismatch. Expected {expected}, got {actual}")
}
}
}
}
impl From<std::io::Error> for VertexFetchError {
fn from(e: Error) -> Self {
Self::Io(e)
}
}
/// A trait for read-only vertex data accessor.
pub trait VertexReadTrait {
#[doc(hidden)]
fn data_layout_ref(&self) -> (&[u8], &[Option<VertexAttribute>]);
/// Clones the vertex and applies the given transformer closure to it and returns a stack-allocated
/// data buffer representing the transformed vertex.
#[inline(always)]
fn transform<F>(&self, func: &mut F) -> ArrayVec<u8, 256>
where
F: FnMut(VertexViewMut),
{
let (data, layout) = self.data_layout_ref();
let mut transformed = ArrayVec::new();
transformed
.try_extend_from_slice(data)
.expect("Vertex size cannot be larger than 256 bytes!");
func(VertexViewMut {
vertex_data: &mut transformed,
sparse_layout: layout,
});
transformed
}
/// Tries to read an attribute with given usage as a pair of two f32.
#[inline(always)]
fn read_2_f32(&self, usage: VertexAttributeUsage) -> Result<Vector2<f32>, VertexFetchError> {
let (data, layout) = self.data_layout_ref();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
let x = LittleEndian::read_f32(&data[(attribute.offset as usize)..]);
let y = LittleEndian::read_f32(&data[(attribute.offset as usize + 4)..]);
Ok(Vector2::new(x, y))
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
/// Tries to read an attribute with given usage as a pair of three f32.
#[inline(always)]
fn read_3_f32(&self, usage: VertexAttributeUsage) -> Result<Vector3<f32>, VertexFetchError> {
let (data, layout) = self.data_layout_ref();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
let x = LittleEndian::read_f32(&data[(attribute.offset as usize)..]);
let y = LittleEndian::read_f32(&data[(attribute.offset as usize + 4)..]);
let z = LittleEndian::read_f32(&data[(attribute.offset as usize + 8)..]);
Ok(Vector3::new(x, y, z))
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
/// Tries to read an attribute with given usage as a pair of four f32.
#[inline(always)]
fn read_4_f32(&self, usage: VertexAttributeUsage) -> Result<Vector4<f32>, VertexFetchError> {
let (data, layout) = self.data_layout_ref();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
let x = LittleEndian::read_f32(&data[(attribute.offset as usize)..]);
let y = LittleEndian::read_f32(&data[(attribute.offset as usize + 4)..]);
let z = LittleEndian::read_f32(&data[(attribute.offset as usize + 8)..]);
let w = LittleEndian::read_f32(&data[(attribute.offset as usize + 12)..]);
Ok(Vector4::new(x, y, z, w))
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
/// Tries to read an attribute with given usage as a pair of four u8.
#[inline(always)]
fn read_4_u8(&self, usage: VertexAttributeUsage) -> Result<Vector4<u8>, VertexFetchError> {
let (data, layout) = self.data_layout_ref();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
let offset = attribute.offset as usize;
let x = data[offset];
let y = data[offset + 1];
let z = data[offset + 2];
let w = data[offset + 3];
Ok(Vector4::new(x, y, z, w))
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
}
impl<'a> VertexReadTrait for VertexViewRef<'a> {
fn data_layout_ref(&self) -> (&[u8], &[Option<VertexAttribute>]) {
(self.vertex_data, self.sparse_layout)
}
}
/// A trait for read/write vertex data accessor.
pub trait VertexWriteTrait: VertexReadTrait {
#[doc(hidden)]
fn data_layout_mut(&mut self) -> (&mut [u8], &[Option<VertexAttribute>]);
/// Tries to find an attribute of the given type and returns a mutable reference of the specified
/// type. Type casting will fail if the size of the destination type `T` does not match the
/// actual attribute size.
#[inline(always)]
fn cast_attribute<T: Copy>(
&mut self,
usage: VertexAttributeUsage,
) -> Result<&mut T, VertexFetchError> {
let (data, layout) = self.data_layout_mut();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
let expected_size = (attribute.size * attribute.data_type.size()) as usize;
let actual_size = std::mem::size_of::<T>();
if expected_size == std::mem::size_of::<T>() {
Ok(unsafe { &mut *(data.as_mut_ptr().add(attribute.offset as usize) as *mut T) })
} else {
Err(VertexFetchError::SizeMismatch {
expected: expected_size as u8,
actual: actual_size as u8,
})
}
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
/// Tries to write an attribute with given usage as a pair of two f32.
fn write_2_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector2<f32>,
) -> Result<(), VertexFetchError>;
/// Tries to write an attribute with given usage as a pair of three f32.
fn write_3_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector3<f32>,
) -> Result<(), VertexFetchError>;
/// Tries to write an attribute with given usage as a pair of four f32.
fn write_4_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector4<f32>,
) -> Result<(), VertexFetchError>;
/// Tries to write an attribute with given usage as a pair of four u8.
fn write_4_u8(
&mut self,
usage: VertexAttributeUsage,
value: Vector4<u8>,
) -> Result<(), VertexFetchError>;
}
impl<'a> VertexReadTrait for VertexViewMut<'a> {
fn data_layout_ref(&self) -> (&[u8], &[Option<VertexAttribute>]) {
(self.vertex_data, self.sparse_layout)
}
}
impl<'a> VertexWriteTrait for VertexViewMut<'a> {
#[inline(always)]
fn data_layout_mut(&mut self) -> (&mut [u8], &[Option<VertexAttribute>]) {
(self.vertex_data, self.sparse_layout)
}
#[inline(always)]
fn write_2_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector2<f32>,
) -> Result<(), VertexFetchError> {
let (data, layout) = self.data_layout_mut();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
LittleEndian::write_f32(&mut data[(attribute.offset as usize)..], value.x);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 4)..], value.y);
Ok(())
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
#[inline(always)]
fn write_3_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector3<f32>,
) -> Result<(), VertexFetchError> {
let (data, layout) = self.data_layout_mut();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
LittleEndian::write_f32(&mut data[(attribute.offset as usize)..], value.x);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 4)..], value.y);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 8)..], value.z);
Ok(())
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
#[inline(always)]
fn write_4_f32(
&mut self,
usage: VertexAttributeUsage,
value: Vector4<f32>,
) -> Result<(), VertexFetchError> {
let (data, layout) = self.data_layout_mut();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
LittleEndian::write_f32(&mut data[(attribute.offset as usize)..], value.x);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 4)..], value.y);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 8)..], value.z);
LittleEndian::write_f32(&mut data[(attribute.offset as usize + 12)..], value.w);
Ok(())
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
#[inline(always)]
fn write_4_u8(
&mut self,
usage: VertexAttributeUsage,
value: Vector4<u8>,
) -> Result<(), VertexFetchError> {
let (data, layout) = self.data_layout_mut();
if let Some(attribute) = layout.get(usage as usize).unwrap() {
data[attribute.offset as usize] = value.x;
data[(attribute.offset + 1) as usize] = value.y;
data[(attribute.offset + 2) as usize] = value.z;
data[(attribute.offset + 3) as usize] = value.w;
Ok(())
} else {
Err(VertexFetchError::NoSuchAttribute(usage))
}
}
}
/// A buffer for data that defines connections between vertices.
#[derive(Visit, Default, Clone, Debug)]
pub struct TriangleBuffer {
triangles: Vec<TriangleDefinition>,
modifications_counter: u64,
}
fn calculate_triangle_buffer_hash(triangles: &[TriangleDefinition]) -> u64 {
let mut hasher = FxHasher::default();
triangles.hash(&mut hasher);
hasher.finish()
}
impl TriangleBuffer {
/// Creates new triangle buffer with given set of triangles.
pub fn new(triangles: Vec<TriangleDefinition>) -> Self {
Self {
triangles,
modifications_counter: 0,
}
}
/// Creates new ref iterator.
pub fn iter(&self) -> impl Iterator<Item = &TriangleDefinition> {
self.triangles.iter()
}
/// Returns a ref to inner data with triangles.
pub fn triangles_ref(&self) -> &[TriangleDefinition] {
&self.triangles
}
/// Sets a new set of triangles.
pub fn set_triangles(&mut self, triangles: Vec<TriangleDefinition>) {
self.triangles = triangles;
self.modifications_counter += 1;
}
/// Returns amount of triangles in the buffer.
pub fn len(&self) -> usize {
self.triangles.len()
}
/// Returns true if the buffer is empty, false - otherwise.
pub fn is_empty(&self) -> bool {
self.triangles.is_empty()
}
/// Returns the total amount of times the buffer was modified.
pub fn modifications_count(&self) -> u64 {
self.modifications_counter
}
/// Calculates inner data hash.
pub fn content_hash(&self) -> u64 {
calculate_triangle_buffer_hash(&self.triangles)
}
/// See VertexBuffer::modify for more info.
pub fn modify(&mut self) -> TriangleBufferRefMut<'_> {
TriangleBufferRefMut {
triangle_buffer: self,
}
}
}
impl Index<usize> for TriangleBuffer {
type Output = TriangleDefinition;
fn index(&self, index: usize) -> &Self::Output {
&self.triangles[index]
}
}
/// See VertexBuffer::modify for more info.
pub struct TriangleBufferRefMut<'a> {
triangle_buffer: &'a mut TriangleBuffer,
}
impl<'a> Deref for TriangleBufferRefMut<'a> {
type Target = TriangleBuffer;
fn deref(&self) -> &Self::Target {
self.triangle_buffer
}
}
impl<'a> DerefMut for TriangleBufferRefMut<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.triangle_buffer
}
}
impl<'a> Drop for TriangleBufferRefMut<'a> {
fn drop(&mut self) {
self.triangle_buffer.modifications_counter += 1;
}
}
impl<'a> TriangleBufferRefMut<'a> {
/// Returns mutable iterator.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut TriangleDefinition> {
self.triangles.iter_mut()
}
/// Adds new triangle in the buffer.
pub fn push(&mut self, triangle: TriangleDefinition) {
self.triangles.push(triangle)
}
/// Adds triangles from the given iterator to the current buffer. Offsets each triangle by the
/// given `offset` value.
pub fn push_triangles_iter_with_offset(
&mut self,
offset: u32,
triangles: impl Iterator<Item = TriangleDefinition>,
) {
self.triangles.extend(triangles.map(|t| t.add(offset)))
}
/// Adds triangles from the given slice to the current buffer.
pub fn push_triangles(&mut self, triangles: &[TriangleDefinition]) {
self.triangles.extend_from_slice(triangles)
}
/// Adds triangles from the given iterator to the current buffer.
pub fn push_triangles_iter(&mut self, triangles: impl Iterator<Item = TriangleDefinition>) {
self.triangles.extend(triangles)
}
/// Adds triangles from the given slice to the current buffer. Offsets each triangle by the
/// given `offset` value.
pub fn push_triangles_with_offset(&mut self, offset: u32, triangles: &[TriangleDefinition]) {
self.triangles
.extend(triangles.iter().map(|t| t.add(offset)))
}
/// Clears the buffer.
pub fn clear(&mut self) {
self.triangles.clear();
}
}
impl<'a> Index<usize> for TriangleBufferRefMut<'a> {
type Output = TriangleDefinition;
fn index(&self, index: usize) -> &Self::Output {
&self.triangle_buffer.triangles[index]
}
}
impl<'a> IndexMut<usize> for TriangleBufferRefMut<'a> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.triangle_buffer.triangles[index]
}
}
/// A typed attribute view for a specific vertex attribute in a vertex buffer.
pub struct AttributeViewRef<'a, T> {
ptr: *const u8,
stride: usize,
count: usize,
phantom: PhantomData<&'a T>,
}
impl<'a, T> AttributeViewRef<'a, T> {
/// Tries to fetch attribute data at the given index.
pub fn get(&'a self, i: usize) -> Option<&'a T> {
if i < self.count {
Some(unsafe { &*((self.ptr.add(i * self.stride)) as *const T) })
} else {
None
}
}
}
/// A typed attribute view for a specific vertex attribute in a vertex buffer.
pub struct AttributeViewRefMut<'a, T> {
ptr: *mut u8,
stride: usize,
count: usize,
phantom: PhantomData<&'a T>,
}
impl<'a, T> AttributeViewRefMut<'a, T> {
/// Tries to fetch attribute data at the given index.
pub fn get(&'a self, i: usize) -> Option<&'a mut T> {
if i < self.count {
Some(unsafe { &mut *((self.ptr.add(i * self.stride)) as *mut T) })
} else {
None
}
}
}
#[cfg(test)]
mod test {
use crate::scene::mesh::buffer::VertexTrait;
use crate::{
core::algebra::{Vector2, Vector3, Vector4},
scene::mesh::buffer::{
VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage, VertexBuffer,
VertexReadTrait,
},
};
#[derive(Clone, Copy, PartialEq, Debug)]
#[repr(C)]
struct Vertex {
position: Vector3<f32>,
tex_coord: Vector2<f32>,
second_tex_coord: Vector2<f32>,
normal: Vector3<f32>,
tangent: Vector4<f32>,
bone_weights: Vector4<f32>,
bone_indices: Vector4<u8>,
}
impl VertexTrait for Vertex {
fn layout() -> &'static [VertexAttributeDescriptor] {
static LAYOUT: [VertexAttributeDescriptor; 7] = [
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Position,
data_type: VertexAttributeDataType::F32,
size: 3,
divisor: 0,
shader_location: 0,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::TexCoord0,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 1,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::TexCoord1,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 2,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Normal,
data_type: VertexAttributeDataType::F32,
size: 3,
divisor: 0,
shader_location: 3,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::Tangent,
data_type: VertexAttributeDataType::F32,
size: 4,
divisor: 0,
shader_location: 4,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::BoneWeight,
data_type: VertexAttributeDataType::F32,
size: 4,
divisor: 0,
shader_location: 5,
normalized: false,
},
VertexAttributeDescriptor {
usage: VertexAttributeUsage::BoneIndices,
data_type: VertexAttributeDataType::U8,
size: 4,
divisor: 0,
shader_location: 6,
normalized: false,
},
];
&LAYOUT
}
}
const VERTICES: [Vertex; 3] = [
Vertex {
position: Vector3::new(1.0, 2.0, 3.0),
tex_coord: Vector2::new(0.0, 1.0),
second_tex_coord: Vector2::new(1.0, 0.0),
normal: Vector3::new(0.0, 1.0, 0.0),
tangent: Vector4::new(1.0, 0.0, 0.0, 1.0),
bone_weights: Vector4::new(0.25, 0.25, 0.25, 0.25),
bone_indices: Vector4::new(1, 2, 3, 4),
},
Vertex {
position: Vector3::new(3.0, 2.0, 1.0),
tex_coord: Vector2::new(1.0, 0.0),
second_tex_coord: Vector2::new(1.0, 0.0),
normal: Vector3::new(0.0, 1.0, 0.0),
tangent: Vector4::new(1.0, 0.0, 0.0, 1.0),
bone_weights: Vector4::new(0.25, 0.25, 0.25, 0.25),
bone_indices: Vector4::new(1, 2, 3, 4),
},
Vertex {
position: Vector3::new(1.0, 1.0, 1.0),
tex_coord: Vector2::new(1.0, 1.0),
second_tex_coord: Vector2::new(1.0, 0.0),
normal: Vector3::new(0.0, 1.0, 0.0),
tangent: Vector4::new(1.0, 0.0, 0.0, 1.0),
bone_weights: Vector4::new(0.25, 0.25, 0.25, 0.25),
bone_indices: Vector4::new(1, 2, 3, 4),
},
];
fn test_view_original_equal<T: VertexReadTrait>(view: T, original: &Vertex) {
assert_eq!(
view.read_3_f32(VertexAttributeUsage::Position).unwrap(),
original.position
);
assert_eq!(
view.read_2_f32(VertexAttributeUsage::TexCoord0).unwrap(),
original.tex_coord
);
assert_eq!(
view.read_2_f32(VertexAttributeUsage::TexCoord1).unwrap(),
original.second_tex_coord
);
assert_eq!(
view.read_3_f32(VertexAttributeUsage::Normal).unwrap(),
original.normal
);
assert_eq!(
view.read_4_f32(VertexAttributeUsage::Tangent).unwrap(),
original.tangent
);
assert_eq!(
view.read_4_f32(VertexAttributeUsage::BoneWeight).unwrap(),
original.bone_weights
);
assert_eq!(
view.read_4_u8(VertexAttributeUsage::BoneIndices).unwrap(),
original.bone_indices
);
}
fn create_test_buffer() -> VertexBuffer {
VertexBuffer::new(VERTICES.len(), VERTICES.to_vec()).unwrap()
}
#[test]
fn test_empty() {
VertexBuffer::new::<Vertex>(0, vec![]).unwrap();
}
#[test]
fn test_iter() {
let buffer = create_test_buffer();
for (view, original) in buffer.iter().zip(VERTICES.iter()) {
test_view_original_equal(view, original);
}
}
#[test]
fn test_iter_mut() {
let mut buffer = create_test_buffer();
for (view, original) in buffer.modify().iter_mut().zip(VERTICES.iter()) {
test_view_original_equal(view, original);
}
}
#[test]
fn test_vertex_duplication() {
let mut buffer = create_test_buffer();
buffer.modify().duplicate(0);
assert_eq!(buffer.vertex_count(), 4);
assert_eq!(buffer.get(0).unwrap(), buffer.get(3).unwrap())
}
#[test]
fn test_pop_vertex() {
let mut buffer = create_test_buffer();
let vertex = buffer.modify().pop_vertex::<Vertex>().unwrap();
assert_eq!(buffer.vertex_count(), 2);
assert_eq!(vertex, VERTICES[2]);
}
#[test]
fn test_remove_last_vertex() {
let mut buffer = create_test_buffer();
buffer.modify().remove_last_vertex();
assert_eq!(buffer.vertex_count(), 2);
}
#[test]
fn test_attribute_view() {
let buffer = create_test_buffer();
let position_view = buffer
.attribute_view::<Vector3<f32>>(VertexAttributeUsage::Position)
.unwrap();
assert_eq!(position_view.get(0), Some(&Vector3::new(1.0, 2.0, 3.0)));
assert_eq!(position_view.get(1), Some(&Vector3::new(3.0, 2.0, 1.0)));
assert_eq!(position_view.get(2), Some(&Vector3::new(1.0, 1.0, 1.0)));
let uv_view = buffer
.attribute_view::<Vector2<f32>>(VertexAttributeUsage::TexCoord0)
.unwrap();
assert_eq!(uv_view.get(0), Some(&Vector2::new(0.0, 1.0)));
assert_eq!(uv_view.get(1), Some(&Vector2::new(1.0, 0.0)));
assert_eq!(uv_view.get(2), Some(&Vector2::new(1.0, 1.0)));
}
#[test]
fn test_add_attribute() {
let mut buffer = create_test_buffer();
let fill = Vector2::new(0.25, 0.75);
let test_index = 1;
buffer
.modify()
.add_attribute(
VertexAttributeDescriptor {
usage: VertexAttributeUsage::TexCoord2,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 7,
normalized: false,
},
fill,
)
.unwrap();
#[derive(Clone, Copy, PartialEq, Debug)]
#[repr(C)]
struct ExtendedVertex {
position: Vector3<f32>,
tex_coord: Vector2<f32>,
second_tex_coord: Vector2<f32>,
normal: Vector3<f32>,
tangent: Vector4<f32>,
bone_weights: Vector4<f32>,
bone_indices: Vector4<u8>,
third_tex_coord: Vector2<f32>, // NEW
}
let new_1 = ExtendedVertex {
position: VERTICES[test_index].position,
tex_coord: VERTICES[test_index].tex_coord,
second_tex_coord: VERTICES[test_index].second_tex_coord,
normal: VERTICES[test_index].normal,
tangent: VERTICES[test_index].tangent,
bone_weights: VERTICES[test_index].bone_weights,
bone_indices: VERTICES[test_index].bone_indices,
third_tex_coord: fill,
};
assert_eq!(
buffer.vertex_size,
std::mem::size_of::<ExtendedVertex>() as u8
);
let view = buffer.get(test_index).unwrap();
assert_eq!(
view.read_3_f32(VertexAttributeUsage::Position).unwrap(),
new_1.position
);
assert_eq!(
view.read_2_f32(VertexAttributeUsage::TexCoord0).unwrap(),
new_1.tex_coord
);
assert_eq!(
view.read_2_f32(VertexAttributeUsage::TexCoord1).unwrap(),
new_1.second_tex_coord
);
assert_eq!(
view.read_2_f32(VertexAttributeUsage::TexCoord2).unwrap(),
new_1.third_tex_coord
);
assert_eq!(
view.read_3_f32(VertexAttributeUsage::Normal).unwrap(),
new_1.normal
);
assert_eq!(
view.read_4_f32(VertexAttributeUsage::Tangent).unwrap(),
new_1.tangent
);
assert_eq!(
view.read_4_f32(VertexAttributeUsage::BoneWeight).unwrap(),
new_1.bone_weights
);
assert_eq!(
view.read_4_u8(VertexAttributeUsage::BoneIndices).unwrap(),
new_1.bone_indices
);
}
}