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 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
#![doc = include_str!("../README.md")]
// This crate uses unsafe, but attempts to minimize its usage. All functions
// that utilize unsafe must explicitly enable it.
#![deny(unsafe_code)]
#![warn(missing_docs, clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::hash::{self, BuildHasher, Hash};
use std::mem::size_of;
use std::ops::{Add, AddAssign, Deref, DerefMut, Div, Neg};
use std::sync::atomic::{self, AtomicU64};
use std::sync::{Arc, Mutex, Weak};
use ahash::{AHashMap, AHasher};
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "cosmic-text")]
pub use cosmic_text;
use figures::units::UPx;
use figures::{Angle, Fraction, FromComponents, Point, Rect, Size, UPx2D};
#[cfg(feature = "image")]
pub use image;
use intentional::{Assert, Cast};
use pipeline::PushConstants;
use sealed::ShapeSource as _;
use wgpu::util::DeviceExt;
pub use {figures, wgpu};
use crate::pipeline::{Uniforms, Vertex};
use crate::sealed::{ClipRect, TextureSource as _};
use crate::text::Text;
/// Application and Windowing Support.
#[cfg(feature = "app")]
pub mod app;
mod atlas;
mod buffer;
/// An easy-to-use batching renderer.
pub mod drawing;
mod pipeline;
mod pod;
mod sealed;
/// Types for drawing paths and shapes.
pub mod shapes;
/// Types for animating textures.
pub mod sprite;
/// Types for text rendering.
#[cfg(feature = "cosmic-text")]
pub mod text;
pub mod tilemap;
pub use atlas::{CollectedTexture, TextureCollection};
use buffer::Buffer;
pub use pipeline::{PreparedGraphic, ShaderScalable};
/// A 2d graphics instance.
///
/// This type contains the GPU state for a single instance of Kludgine. To
/// render graphics correctly, it must know the size and scale of the surface
/// being rendered to. These values are provided in the constructor, but can be
/// updated using [`resize()`](Self::resize).
///
/// To draw using Kludgine, create a [`Frame`] using
/// [`next_frame()`](Self::next_frame). [`wgpu`] has lifetime requirements on
/// the [`wgpu::RenderPass`] which causes each item being rendered to be
/// attached to the lifetime of the render pass. This means that no temporary
/// variables can be used to render.
///
/// Instead, graphics must be prepared before rendering, and stored somewhere
/// during the remainder of the [`RenderingGraphics`]. To prepare graphics to be
/// rendered, call [`Frame::prepare()`] to receive a [`Graphics`] instance that
/// can be used in various Kludgine APIs such as
/// [`Shape::prepare`](shapes::Shape::prepare).
#[derive(Debug)]
pub struct Kludgine {
id: KludgineId,
default_bindings: wgpu::BindGroup,
pipeline: wgpu::RenderPipeline,
_shader: wgpu::ShaderModule,
binding_layout: wgpu::BindGroupLayout,
linear_sampler: wgpu::Sampler,
nearest_sampler: wgpu::Sampler,
uniforms: Buffer<Uniforms>,
size: Size<UPx>,
dpi_scale: Fraction,
zoom: Fraction,
effective_scale: Fraction,
#[cfg(feature = "cosmic-text")]
text: text::TextSystem,
}
impl Kludgine {
/// The features that wgpu requires in compatible devices.
pub const REQURED_FEATURES: wgpu::Features = wgpu::Features::PUSH_CONSTANTS;
/// Returns a new instance of Kludgine with the provided parameters.
#[must_use]
#[cfg_attr(not(feature = "cosmic-text"), allow(unused_variables))]
pub fn new(
device: &wgpu::Device,
queue: &wgpu::Queue,
format: wgpu::TextureFormat,
multisample: wgpu::MultisampleState,
initial_size: Size<UPx>,
scale: f32,
) -> Self {
let id = KludgineId::unique();
let scale = Fraction::from(scale);
let uniforms = Buffer::new(
&[Uniforms::new(initial_size, scale)],
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
device,
);
let binding_layout = pipeline::bind_group_layout(device, false);
let pipeline_layout = pipeline::layout(device, &binding_layout);
let empty_texture = device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let nearest_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
min_filter: wgpu::FilterMode::Nearest,
mag_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..wgpu::SamplerDescriptor::default()
});
let linear_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
min_filter: wgpu::FilterMode::Linear,
mag_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Linear,
..wgpu::SamplerDescriptor::default()
});
let default_bindings = pipeline::bind_group(
device,
&binding_layout,
&uniforms.wgpu,
&empty_texture.create_view(&wgpu::TextureViewDescriptor::default()),
&nearest_sampler,
);
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
let pipeline = pipeline::new(device, &pipeline_layout, &shader, format, multisample);
Self {
id,
#[cfg(feature = "cosmic-text")]
text: text::TextSystem::new(&ProtoGraphics {
id,
device,
queue,
binding_layout: &binding_layout,
linear_sampler: &linear_sampler,
nearest_sampler: &nearest_sampler,
uniforms: &uniforms.wgpu,
}),
default_bindings,
pipeline,
_shader: shader,
linear_sampler,
nearest_sampler,
size: initial_size,
dpi_scale: scale,
zoom: Fraction::ONE,
effective_scale: scale,
uniforms,
binding_layout,
}
}
/// Adjusts and returns the wgpu limits to support features used by
/// Kludgine.
#[must_use]
pub fn adjust_limits(mut limits: wgpu::Limits) -> wgpu::Limits {
limits.max_push_constant_size = limits
.max_push_constant_size
.max(size_of::<PushConstants>().try_into().assert_expected());
limits
}
/// Returns the unique id of this instance.
#[must_use]
pub const fn id(&self) -> KludgineId {
self.id
}
/// Updates the size and scale of this Kludgine instance.
///
/// This function updates data stored in the GPU that affects how graphics
/// are rendered. It should be called before calling `next_frame()` if the
/// size or scale of the underlying surface has changed.
pub fn resize(
&mut self,
new_size: Size<UPx>,
new_scale: impl Into<Fraction>,
new_zoom: impl Into<Fraction>,
queue: &wgpu::Queue,
) {
let new_scale = new_scale.into();
let new_zoom = new_zoom.into();
self.effective_scale = new_scale * new_zoom;
if self.size != new_size || self.dpi_scale != new_scale || self.zoom != new_zoom {
self.size = new_size;
self.dpi_scale = new_scale;
self.zoom = new_zoom;
self.uniforms
.update(0, &[Uniforms::new(self.size, self.effective_scale)], queue);
}
#[cfg(feature = "cosmic-text")]
self.text.scale_changed(self.effective_scale);
}
/// Sets the current zoom level.
///
/// Zoom and DPI scale are multiplied to create an effective scale for all
/// DPI-scaled operations.
pub fn set_zoom(&mut self, new_zoom: impl Into<Fraction>, queue: &wgpu::Queue) {
self.resize(self.size, self.dpi_scale, new_zoom, queue);
}
/// Sets the current DPI scale.
///
/// This scaling factor should come from the window server when possible.
pub fn set_dpi_scale(&mut self, new_scale: impl Into<Fraction>, queue: &wgpu::Queue) {
self.resize(self.size, new_scale, self.zoom, queue);
}
/// Begins rendering a new frame.
pub fn next_frame(&mut self) -> Frame<'_> {
#[cfg(feature = "cosmic-text")]
self.text.new_frame();
Frame {
kludgine: self,
commands: None,
}
}
/// Returns the currently configured size to render.
pub const fn size(&self) -> Size<UPx> {
self.size
}
/// Returns the effective scale to apply to graphics contexts.
pub const fn scale(&self) -> Fraction {
self.effective_scale
}
/// Returns the DPI scale of the underlying context.
pub const fn dpi_scale(&self) -> Fraction {
self.dpi_scale
}
/// Returns the current zoom applied.
pub const fn zoom(&self) -> Fraction {
self.zoom
}
}
/// The unique ID of a [`Kludgine`] instance.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
pub struct KludgineId(u64);
impl KludgineId {
fn unique() -> Self {
static ID_COUNTER: AtomicU64 = AtomicU64::new(0);
Self(ID_COUNTER.fetch_add(1, atomic::Ordering::Release))
}
}
/// A frame that can be rendered.
///
/// # Panics
///
/// After [`Frame::render()`] has been invoked, this type will panic if dropped
/// before either [`Frame::submit()`] or [`Frame::abort()`] are invoked. This
/// panic is designed to prevent accidentally forgetting to submit a frame to the GPU.q
pub struct Frame<'gfx> {
kludgine: &'gfx mut Kludgine,
commands: Option<wgpu::CommandEncoder>,
}
impl Frame<'_> {
/// Creates a [`Graphics`] context for this frame that can be used to
/// prepare graphics for rendering:
///
/// - [`Shape::prepare`](shapes::Shape::prepare)
/// - [`Texture::prepare`]
/// - [`Texture::prepare_partial`]
/// - [`CollectedTexture::prepare`]
/// - [`Drawing::new_frame`](drawing::Drawing::new_frame)
///
/// The returned graphics provides access to the various types to update
/// their representation on the GPU so that they can be rendered later.
pub fn prepare<'gfx>(
&'gfx mut self,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
) -> Graphics<'gfx> {
Graphics::new(self.kludgine, device, queue)
}
/// Creates a [`RenderingGraphics`] context for this frame which is used to
/// render previously prepared graphics:
///
/// - [`PreparedGraphic`]
/// - [`PreparedText`](text::PreparedText)
/// - [`Drawing`](drawing::Drawing)
#[must_use]
pub fn render<'gfx, 'pass>(
&'pass mut self,
pass: &wgpu::RenderPassDescriptor<'_>,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
) -> RenderingGraphics<'gfx, 'pass> {
if self.commands.is_none() {
self.commands =
Some(device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default()));
}
RenderingGraphics::new(
self.commands
.as_mut()
.assert("initialized above")
.begin_render_pass(pass),
self.kludgine,
device,
queue,
)
}
/// Creates a [`RenderingGraphics`] that renders into `texture` for this
/// frame. The returned context can be used to render previously prepared
/// graphics:
///
/// - [`PreparedGraphic`]
/// - [`PreparedText`](text::PreparedText)
/// - [`Drawing`](drawing::Drawing)
pub fn render_into<'gfx, 'pass>(
&'pass mut self,
texture: &'pass Texture,
load_op: wgpu::LoadOp<Color>,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
) -> RenderingGraphics<'gfx, 'pass> {
self.render(
&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &texture.data.view,
resolve_target: None,
ops: wgpu::Operations {
load: match load_op {
wgpu::LoadOp::Clear(color) => wgpu::LoadOp::Clear(color.into()),
wgpu::LoadOp::Load => wgpu::LoadOp::Load,
},
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
},
device,
queue,
)
}
/// Submits all of the commands for this frame to the GPU.
///
/// This function does not block for the operations to finish. The returned
/// [`wgpu::SubmissionIndex`] can be used to block until completion if
/// desired.
#[allow(clippy::must_use_candidate)]
pub fn submit(mut self, queue: &wgpu::Queue) -> Option<wgpu::SubmissionIndex> {
let commands = self.commands.take()?;
Some(queue.submit([commands.finish()]))
}
/// Aborts rendering this frame.
///
/// If [`Frame::render()`] has been invoked, this function must be used
/// instead of dropping the frame. This type implements a panic-on-drop to
/// prevent forgetting to submit the frame to the GPU, and this function
/// prevents the panic from happening.
pub fn abort(mut self) {
// Clear out the commands, preventing drop from panicking.
self.commands.take();
}
}
impl Drop for Frame<'_> {
fn drop(&mut self) {
assert!(
self.commands.is_none(),
"Frame dropped without calling finish() or abort()"
);
}
}
/// A generic graphics context.
///
/// This generic trait is used on some APIs because they are utilized both
/// publicly and internally. The only user-facing type that implements this
/// trait is [`Graphics`].
pub trait KludgineGraphics: sealed::KludgineGraphics {}
struct ProtoGraphics<'gfx> {
id: KludgineId,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
binding_layout: &'gfx wgpu::BindGroupLayout,
linear_sampler: &'gfx wgpu::Sampler,
nearest_sampler: &'gfx wgpu::Sampler,
uniforms: &'gfx wgpu::Buffer,
}
impl<'a> ProtoGraphics<'a> {
fn new(device: &'a wgpu::Device, queue: &'a wgpu::Queue, kludgine: &'a Kludgine) -> Self {
Self {
id: kludgine.id,
device,
queue,
binding_layout: &kludgine.binding_layout,
linear_sampler: &kludgine.linear_sampler,
nearest_sampler: &kludgine.nearest_sampler,
uniforms: &kludgine.uniforms.wgpu,
}
}
}
impl KludgineGraphics for ProtoGraphics<'_> {}
impl sealed::KludgineGraphics for ProtoGraphics<'_> {
fn id(&self) -> KludgineId {
self.id
}
fn device(&self) -> &wgpu::Device {
self.device
}
fn queue(&self) -> &wgpu::Queue {
self.queue
}
fn binding_layout(&self) -> &wgpu::BindGroupLayout {
self.binding_layout
}
fn uniforms(&self) -> &wgpu::Buffer {
self.uniforms
}
fn nearest_sampler(&self) -> &wgpu::Sampler {
self.nearest_sampler
}
fn linear_sampler(&self) -> &wgpu::Sampler {
self.linear_sampler
}
}
impl KludgineGraphics for Graphics<'_> {}
impl sealed::KludgineGraphics for Graphics<'_> {
fn id(&self) -> KludgineId {
self.kludgine.id
}
fn device(&self) -> &wgpu::Device {
self.device
}
fn queue(&self) -> &wgpu::Queue {
self.queue
}
fn binding_layout(&self) -> &wgpu::BindGroupLayout {
&self.kludgine.binding_layout
}
fn uniforms(&self) -> &wgpu::Buffer {
&self.kludgine.uniforms.wgpu
}
fn nearest_sampler(&self) -> &wgpu::Sampler {
&self.kludgine.nearest_sampler
}
fn linear_sampler(&self) -> &wgpu::Sampler {
&self.kludgine.linear_sampler
}
}
#[derive(Debug)]
struct ClipStack {
current: ClipRect,
previous_clips: Vec<ClipRect>,
}
impl ClipStack {
pub fn new(size: Size<UPx>) -> Self {
Self {
current: size.into(),
previous_clips: Vec::new(),
}
}
pub fn push_clip(&mut self, clip: Rect<UPx>) {
let previous_clip = self.current;
self.current = previous_clip.clip_to(clip.expand_rounded());
self.previous_clips.push(previous_clip);
}
pub fn pop_clip(&mut self) {
self.current = self.previous_clips.pop().expect("unpaired pop_clip");
}
}
/// A context used to prepare graphics to render.
///
/// This type is used in these APIs:
///
/// - [`Shape::prepare`](shapes::Shape::prepare)
/// - [`Texture::prepare`]
/// - [`Texture::prepare_partial`]
/// - [`CollectedTexture::prepare`]
/// - [`Drawing::new_frame`](drawing::Drawing::new_frame)
#[derive(Debug)]
pub struct Graphics<'gfx> {
kludgine: &'gfx mut Kludgine,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue, // Need this eventually to be able to have dynamic shape collections
clip: ClipStack,
}
impl<'gfx> Graphics<'gfx> {
/// Returns a new instance.
pub fn new(
kludgine: &'gfx mut Kludgine,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
) -> Self {
Self {
clip: ClipStack::new(kludgine.size),
kludgine,
device,
queue,
}
}
/// Returns a reference to the underlying [`wgpu::Device`].
#[must_use]
pub const fn device(&self) -> &'gfx wgpu::Device {
self.device
}
/// Returns a reference to the underlying [`wgpu::Queue`].
#[must_use]
pub const fn queue(&self) -> &'gfx wgpu::Queue {
self.queue
}
/// Returns a mutable reference to the [`cosmic_text::FontSystem`] used when
/// rendering text.
#[cfg(feature = "cosmic-text")]
pub fn font_system(&mut self) -> &mut cosmic_text::FontSystem {
self.kludgine.font_system()
}
/// Returns the current clipped size of the context.
///
/// If this context has not been clipped, the value returned will be
/// equivalent to [`Kludgine::size`].
#[must_use]
pub const fn size(&self) -> Size<UPx> {
self.clip.current.0.size
}
/// Returns the current rectangular area of the context.
///
/// If this context has not been clipped, the value returned will be
/// equivalent to [`Kludgine::size`] with an origin of `0,0`.
#[must_use]
pub const fn clip_rect(&self) -> Rect<UPx> {
self.clip.current.0
}
/// Sets the zoom level.
///
/// The zoom level and DPI scale are multiplied to create an effective scale
/// for all DPI-scaled drawing operations.
pub fn set_zoom(&mut self, new_zoom: impl Into<Fraction>) {
self.kludgine.set_zoom(new_zoom, self.queue);
}
}
impl AsRef<wgpu::Device> for Graphics<'_> {
fn as_ref(&self) -> &wgpu::Device {
self.device()
}
}
impl AsRef<wgpu::Queue> for Graphics<'_> {
fn as_ref(&self) -> &wgpu::Queue {
self.queue()
}
}
impl Deref for Graphics<'_> {
type Target = Kludgine;
fn deref(&self) -> &Self::Target {
self.kludgine
}
}
impl DerefMut for Graphics<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.kludgine
}
}
impl Clipped for Graphics<'_> {
fn push_clip(&mut self, clip: Rect<UPx>) {
self.clip.push_clip(clip);
}
fn pop_clip(&mut self) {
self.clip.pop_clip();
}
}
impl sealed::Clipped for Graphics<'_> {}
/// A graphics context used to render previously prepared graphics.
///
/// This type is used to render these types:
///
/// - [`PreparedGraphic`]
/// - [`PreparedText`](text::PreparedText)
/// - [`Drawing`](drawing::Drawing)
pub struct RenderingGraphics<'gfx, 'pass> {
pass: wgpu::RenderPass<'pass>,
kludgine: &'pass Kludgine,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
clip: ClipStack,
pipeline_is_active: bool,
}
impl<'gfx, 'pass> RenderingGraphics<'gfx, 'pass> {
fn new(
pass: wgpu::RenderPass<'pass>,
kludgine: &'pass Kludgine,
device: &'gfx wgpu::Device,
queue: &'gfx wgpu::Queue,
) -> Self {
Self {
pass,
clip: ClipStack::new(kludgine.size),
kludgine,
device,
queue,
pipeline_is_active: false,
}
}
/// Returns a reference to the underlying [`wgpu::Device`].
#[must_use]
pub const fn device(&self) -> &'gfx wgpu::Device {
self.device
}
/// Returns a reference to the underlying [`wgpu::Queue`].
#[must_use]
pub const fn queue(&self) -> &'gfx wgpu::Queue {
self.queue
}
fn active_pipeline_if_needed(&mut self) -> bool {
if self.pipeline_is_active {
false
} else {
self.pipeline_is_active = true;
self.pass.set_pipeline(&self.kludgine.pipeline);
true
}
}
/// Returns a [`ClipGuard`] that causes all drawing operations to be offset
/// and clipped to `clip` until it is dropped.
///
/// This function causes the [`RenderingGraphics`] to act as if the origin
/// of the context is `clip.origin`, and the size of the context is
/// `clip.size`. This means that rendering at 0,0 will actually render at
/// the effective clip rect's origin.
///
/// `clip` is relative to the current clip rect and cannot extend the
/// current clipping rectangle.
pub fn clipped_to(&mut self, clip: Rect<UPx>) -> ClipGuard<'_, Self> {
self.push_clip(clip);
ClipGuard { clipped: self }
}
/// Returns the current size of the graphics area being rendered to.
///
/// If the graphics has been clipped, this returns the current width of the
/// clipped area.
#[must_use]
pub const fn size(&self) -> Size<UPx> {
self.clip.current.0.size
}
/// Returns the current scaling factor of the display being rendered to.
#[must_use]
pub const fn scale(&self) -> Fraction {
self.kludgine.scale()
}
}
/// A graphics context that has been clipped.
pub trait Clipped: Sized + sealed::Clipped {
/// Pushes a new clipping state to the clipping stack.
///
/// This function causes this type to act as if the origin of the context is
/// `clip.origin`, and the size of the context is `clip.size`. This means
/// that rendering at 0,0 will actually render at the effective clip rect's
/// origin.
///
/// `clip` is relative to the current clip rect and cannot extend the
/// current clipping rectangle.
///
/// To restore the clipping rect to the state it was before this function
/// was called, use [`Clipped::pop_clip()`].
fn push_clip(&mut self, clip: Rect<UPx>);
/// Restores the clipping rect to the previous state before the last call to
/// [`Clipped::push_clip()`].
///
/// # Panics
///
/// This function will panic if it is called more times than
/// [`Clipped::push_clip()`].
fn pop_clip(&mut self);
/// Returns a [`ClipGuard`] that causes all drawing operations to be offset
/// and clipped to `clip` until it is dropped.
///
/// This function causes this type to act as if the origin of the context is
/// `clip.origin`, and the size of the context is `clip.size`. This means
/// that rendering at 0,0 will actually render at the effective clip rect's
/// origin.
///
/// `clip` is relative to the current clip rect and cannot extend the
/// current clipping rectangle.
fn clipped_to(&mut self, clip: Rect<UPx>) -> ClipGuard<'_, Self> {
self.push_clip(clip);
ClipGuard { clipped: self }
}
}
impl Clipped for RenderingGraphics<'_, '_> {
fn pop_clip(&mut self) {
self.clip.pop_clip();
if self.clip.current.size.width > 0 && self.clip.current.size.height > 0 {
self.pass.set_scissor_rect(
self.clip.current.origin.x.into(),
self.clip.current.origin.y.into(),
self.clip.current.size.width.into(),
self.clip.current.size.height.into(),
);
}
}
fn push_clip(&mut self, clip: Rect<UPx>) {
self.clip.push_clip(clip);
if self.clip.current.size.width > 0 && self.clip.current.size.height > 0 {
self.pass.set_scissor_rect(
self.clip.current.origin.x.into(),
self.clip.current.origin.y.into(),
self.clip.current.size.width.into(),
self.clip.current.size.height.into(),
);
}
}
}
impl sealed::Clipped for RenderingGraphics<'_, '_> {}
impl Drop for RenderingGraphics<'_, '_> {
fn drop(&mut self) {
// This shouldn't be necessary, but under the GL backend, Cushy only
// renders the final widget/clipped region. By setting this, it makes
// Cushy work on the GL backend.
self.pass.set_scissor_rect(
0,
0,
self.kludgine.size.width.get(),
self.kludgine.size.height.get(),
);
}
}
/// A clipped surface.
///
/// When dropped, the clipped type will have its clip rect restored to the
/// previously clipped rect. [`ClipGuard`]s can be nested.
///
/// This type implements [`Deref`]/[`DerefMut`] to provide access to the
/// underyling clipped type.
#[derive(Debug)]
pub struct ClipGuard<'clip, T>
where
T: Clipped,
{
clipped: &'clip mut T,
}
impl<T> Drop for ClipGuard<'_, T>
where
T: Clipped,
{
fn drop(&mut self) {
self.clipped.pop_clip();
}
}
impl<T> Deref for ClipGuard<'_, T>
where
T: Clipped,
{
type Target = T;
fn deref(&self) -> &Self::Target {
self.clipped
}
}
impl<T> DerefMut for ClipGuard<'_, T>
where
T: Clipped,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.clipped
}
}
/// A red, green, blue, and alpha color value stored in 32-bits.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Pod, Zeroable)]
#[repr(C)]
pub struct Color(pub u32);
pub(crate) fn f32_component_to_u8(component: f32) -> u8 {
(component.clamp(0., 1.0) * 255.).round().cast()
}
impl Color {
/// Returns a new color with the provided components.
#[must_use]
pub const fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self((red as u32) << 24 | (green as u32) << 16 | (blue as u32) << 8 | alpha as u32)
}
/// Returns a new color by converting each component from its `0.0..=1.0`
/// range into a `0..=255` range.
#[must_use]
pub fn new_f32(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::new(
f32_component_to_u8(red),
f32_component_to_u8(green),
f32_component_to_u8(blue),
f32_component_to_u8(alpha),
)
}
/// Returns the red component of this color, range 0-255.
#[must_use]
#[allow(clippy::cast_possible_truncation)] // truncation desired
pub const fn red(self) -> u8 {
(self.0 >> 24) as u8
}
/// Returns the red component of this color, range 0.0-1.0.
#[must_use]
pub fn red_f32(self) -> f32 {
f32::from(self.red()) / 255.
}
/// Returns the green component of this color, range 0-255.
#[must_use]
#[allow(clippy::cast_possible_truncation)] // truncation desired
pub const fn green(self) -> u8 {
(self.0 >> 16) as u8
}
/// Returns the green component of this color, range 0.0-1.0.
#[must_use]
pub fn green_f32(self) -> f32 {
f32::from(self.green()) / 255.
}
/// Returns the blue component of this color, range 0-255.
#[must_use]
#[allow(clippy::cast_possible_truncation)] // truncation desired
pub const fn blue(self) -> u8 {
(self.0 >> 8) as u8
}
/// Returns the blue component of this color, range 0.0-1.0.
#[must_use]
pub fn blue_f32(self) -> f32 {
f32::from(self.blue()) / 255.
}
/// Returns the alpha component of this color, range 0-255. A value of 255
/// is completely opaque.
#[must_use]
#[allow(clippy::cast_possible_truncation)] // truncation desired
pub const fn alpha(self) -> u8 {
self.0 as u8
}
/// Returns the alpha component of this color, range 0.0-1.0. A value of 1.0
/// is completely opaque.
#[must_use]
pub fn alpha_f32(self) -> f32 {
f32::from(self.alpha()) / 255.
}
/// Returns a new color replacing this colors red channel with `red`.
#[must_use]
pub const fn with_red(self, red: u8) -> Self {
Self(self.0 & 0x00FF_FFFF | ((red as u32) << 24))
}
/// Returns a new color replacing this colors green channel with `green`.
#[must_use]
pub const fn with_green(self, red: u8) -> Self {
Self(self.0 & 0xFF00_FFFF | ((red as u32) << 16))
}
/// Returns a new color replacing this colors blue channel with `blue`.
#[must_use]
pub const fn with_blue(self, blue: u8) -> Self {
Self(self.0 & 0xFFFF_00FF | ((blue as u32) << 8))
}
/// Returns a new color replacing this colors alpha channel with `alpha`.
#[must_use]
pub const fn with_alpha(self, alpha: u8) -> Self {
Self(self.0 & 0xFFFF_FF00 | alpha as u32)
}
/// Returns a new color replacing this colors red channel with `red`.
#[must_use]
pub fn with_red_f32(self, red: f32) -> Self {
self.with_red(f32_component_to_u8(red))
}
/// Returns a new color replacing this colors green channel with `green`.
#[must_use]
pub fn with_green_f32(self, green: f32) -> Self {
self.with_green(f32_component_to_u8(green))
}
/// Returns a new color replacing this colors blue channel with `blue`.
#[must_use]
pub fn with_blue_f32(self, blue: f32) -> Self {
self.with_blue(f32_component_to_u8(blue))
}
/// Returns a new color replacing this colors alpha channel with `alpha`.
#[must_use]
pub fn with_alpha_f32(self, alpha: f32) -> Self {
self.with_alpha(f32_component_to_u8(alpha))
}
}
fn srgb_to_linear(red: f32, green: f32, blue: f32, alpha: f32) -> Color {
let linear = palette::rgb::Srgba::new(red, green, blue, alpha).into_linear();
Color::new_f32(linear.red, linear.green, linear.blue, linear.alpha)
}
impl Debug for Color {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "#{:08x}", self.0)
}
}
impl From<Color> for wgpu::Color {
fn from(color: Color) -> Self {
Self {
r: f64::from(color.red_f32()),
g: f64::from(color.green_f32()),
b: f64::from(color.blue_f32()),
a: f64::from(color.alpha_f32()),
}
}
}
#[cfg(feature = "cosmic-text")]
impl From<cosmic_text::Color> for Color {
fn from(value: cosmic_text::Color) -> Self {
Self::new(value.r(), value.g(), value.b(), value.a())
}
}
#[cfg(feature = "cosmic-text")]
impl From<Color> for cosmic_text::Color {
fn from(value: Color) -> Self {
Self::rgba(value.red(), value.green(), value.blue(), value.alpha())
}
}
#[test]
fn color_debug() {
assert_eq!(format!("{:?}", Color::new(1, 2, 3, 4)), "#01020304");
}
impl Color {
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ALICEBLUE: Self = Self::new(240, 248, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ANTIQUEWHITE: Self = Self::new(250, 235, 215, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const AQUA: Self = Self::new(0, 255, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const AQUAMARINE: Self = Self::new(127, 255, 212, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const AZURE: Self = Self::new(240, 255, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BEIGE: Self = Self::new(245, 245, 220, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BISQUE: Self = Self::new(255, 228, 196, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BLACK: Self = Self::new(0, 0, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BLANCHEDALMOND: Self = Self::new(255, 235, 205, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BLUE: Self = Self::new(0, 0, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BLUEVIOLET: Self = Self::new(138, 43, 226, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BROWN: Self = Self::new(165, 42, 42, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const BURLYWOOD: Self = Self::new(222, 184, 135, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CADETBLUE: Self = Self::new(95, 158, 160, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CHARTREUSE: Self = Self::new(127, 255, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CHOCOLATE: Self = Self::new(210, 105, 30, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CLEAR_BLACK: Self = Self::new(0, 0, 0, 0);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CLEAR_WHITE: Self = Self::new(255, 255, 255, 0);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CORAL: Self = Self::new(255, 127, 80, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CORNFLOWERBLUE: Self = Self::new(100, 149, 237, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CORNSILK: Self = Self::new(255, 248, 220, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CRIMSON: Self = Self::new(220, 20, 60, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const CYAN: Self = Self::new(0, 255, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKBLUE: Self = Self::new(0, 0, 139, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKCYAN: Self = Self::new(0, 139, 139, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKGOLDENROD: Self = Self::new(184, 134, 11, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKGRAY: Self = Self::new(169, 169, 169, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKGREEN: Self = Self::new(0, 100, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKGREY: Self = Self::new(169, 169, 169, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKKHAKI: Self = Self::new(189, 183, 107, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKMAGENTA: Self = Self::new(139, 0, 139, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKOLIVEGREEN: Self = Self::new(85, 107, 47, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKORANGE: Self = Self::new(255, 140, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKORCHID: Self = Self::new(153, 50, 204, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKRED: Self = Self::new(139, 0, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKSALMON: Self = Self::new(233, 150, 122, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKSEAGREEN: Self = Self::new(143, 188, 143, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKSLATEBLUE: Self = Self::new(72, 61, 139, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKSLATEGRAY: Self = Self::new(47, 79, 79, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKSLATEGREY: Self = Self::new(47, 79, 79, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKTURQUOISE: Self = Self::new(0, 206, 209, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DARKVIOLET: Self = Self::new(148, 0, 211, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DEEPPINK: Self = Self::new(255, 20, 147, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DEEPSKYBLUE: Self = Self::new(0, 191, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DIMGRAY: Self = Self::new(105, 105, 105, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DIMGREY: Self = Self::new(105, 105, 105, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const DODGERBLUE: Self = Self::new(30, 144, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const FIREBRICK: Self = Self::new(178, 34, 34, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const FLORALWHITE: Self = Self::new(255, 250, 240, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const FORESTGREEN: Self = Self::new(34, 139, 34, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const FUCHSIA: Self = Self::new(255, 0, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GAINSBORO: Self = Self::new(220, 220, 220, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GHOSTWHITE: Self = Self::new(248, 248, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GOLD: Self = Self::new(255, 215, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GOLDENROD: Self = Self::new(218, 165, 32, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GRAY: Self = Self::new(128, 128, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GREEN: Self = Self::new(0, 128, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GREENYELLOW: Self = Self::new(173, 255, 47, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const GREY: Self = Self::new(128, 128, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const HONEYDEW: Self = Self::new(240, 255, 240, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const HOTPINK: Self = Self::new(255, 105, 180, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const INDIANRED: Self = Self::new(205, 92, 92, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const INDIGO: Self = Self::new(75, 0, 130, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const IVORY: Self = Self::new(255, 255, 240, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const KHAKI: Self = Self::new(240, 230, 140, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LAVENDER: Self = Self::new(230, 230, 250, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LAVENDERBLUSH: Self = Self::new(255, 240, 245, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LAWNGREEN: Self = Self::new(124, 252, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LEMONCHIFFON: Self = Self::new(255, 250, 205, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTBLUE: Self = Self::new(173, 216, 230, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTCORAL: Self = Self::new(240, 128, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTCYAN: Self = Self::new(224, 255, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTGOLDENRODYELLOW: Self = Self::new(250, 250, 210, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTGRAY: Self = Self::new(211, 211, 211, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTGREEN: Self = Self::new(144, 238, 144, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTGREY: Self = Self::new(211, 211, 211, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTPINK: Self = Self::new(255, 182, 193, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSALMON: Self = Self::new(255, 160, 122, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSEAGREEN: Self = Self::new(32, 178, 170, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSKYBLUE: Self = Self::new(135, 206, 250, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSLATEGRAY: Self = Self::new(119, 136, 153, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSLATEGREY: Self = Self::new(119, 136, 153, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTSTEELBLUE: Self = Self::new(176, 196, 222, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIGHTYELLOW: Self = Self::new(255, 255, 224, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIME: Self = Self::new(0, 255, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LIMEGREEN: Self = Self::new(50, 205, 50, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const LINEN: Self = Self::new(250, 240, 230, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MAGENTA: Self = Self::new(255, 0, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MAROON: Self = Self::new(128, 0, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMAQUAMARINE: Self = Self::new(102, 205, 170, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMBLUE: Self = Self::new(0, 0, 205, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMORCHID: Self = Self::new(186, 85, 211, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMPURPLE: Self = Self::new(147, 112, 219, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMSEAGREEN: Self = Self::new(60, 179, 113, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMSLATEBLUE: Self = Self::new(123, 104, 238, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMSPRINGGREEN: Self = Self::new(0, 250, 154, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMTURQUOISE: Self = Self::new(72, 209, 204, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MEDIUMVIOLETRED: Self = Self::new(199, 21, 133, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MIDNIGHTBLUE: Self = Self::new(25, 25, 112, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MINTCREAM: Self = Self::new(245, 255, 250, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MISTYROSE: Self = Self::new(255, 228, 225, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const MOCCASIN: Self = Self::new(255, 228, 181, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const NAVAJOWHITE: Self = Self::new(255, 222, 173, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const NAVY: Self = Self::new(0, 0, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const OLDLACE: Self = Self::new(253, 245, 230, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const OLIVE: Self = Self::new(128, 128, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const OLIVEDRAB: Self = Self::new(107, 142, 35, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ORANGE: Self = Self::new(255, 165, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ORANGERED: Self = Self::new(255, 69, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ORCHID: Self = Self::new(218, 112, 214, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PALEGOLDENROD: Self = Self::new(238, 232, 170, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PALEGREEN: Self = Self::new(152, 251, 152, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PALETURQUOISE: Self = Self::new(175, 238, 238, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PALEVIOLETRED: Self = Self::new(219, 112, 147, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PAPAYAWHIP: Self = Self::new(255, 239, 213, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PEACHPUFF: Self = Self::new(255, 218, 185, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PERU: Self = Self::new(205, 133, 63, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PINK: Self = Self::new(255, 192, 203, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PLUM: Self = Self::new(221, 160, 221, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const POWDERBLUE: Self = Self::new(176, 224, 230, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const PURPLE: Self = Self::new(128, 0, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const REBECCAPURPLE: Self = Self::new(102, 51, 153, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const RED: Self = Self::new(255, 0, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ROSYBROWN: Self = Self::new(188, 143, 143, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const ROYALBLUE: Self = Self::new(65, 105, 225, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SADDLEBROWN: Self = Self::new(139, 69, 19, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SALMON: Self = Self::new(250, 128, 114, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SANDYBROWN: Self = Self::new(244, 164, 96, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SEAGREEN: Self = Self::new(46, 139, 87, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SEASHELL: Self = Self::new(255, 245, 238, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SIENNA: Self = Self::new(160, 82, 45, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SILVER: Self = Self::new(192, 192, 192, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SKYBLUE: Self = Self::new(135, 206, 235, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SLATEBLUE: Self = Self::new(106, 90, 205, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SLATEGRAY: Self = Self::new(112, 128, 144, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SLATEGREY: Self = Self::new(112, 128, 144, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SNOW: Self = Self::new(255, 250, 250, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const SPRINGGREEN: Self = Self::new(0, 255, 127, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const STEELBLUE: Self = Self::new(70, 130, 180, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const TAN: Self = Self::new(210, 180, 140, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const TEAL: Self = Self::new(0, 128, 128, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const THISTLE: Self = Self::new(216, 191, 216, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const TOMATO: Self = Self::new(255, 99, 71, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const TURQUOISE: Self = Self::new(64, 224, 208, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const VIOLET: Self = Self::new(238, 130, 238, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const WHEAT: Self = Self::new(245, 222, 179, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const WHITE: Self = Self::new(255, 255, 255, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const WHITESMOKE: Self = Self::new(245, 245, 245, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const YELLOW: Self = Self::new(255, 255, 0, 255);
/// Equivalent to the [CSS color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) of the same name.
pub const YELLOWGREEN: Self = Self::new(154, 205, 50, 255);
}
/// A [`TextureSource`] that loads its data lazily.
///
/// This texture type can be shared between multiple [`wgpu::Device`]s. When a
/// clone of this texture is used, a unique copy will be loaded once per
/// [`wgpu::Device`].
#[derive(Debug)]
pub struct LazyTexture {
data: Arc<LazyTextureData>,
last_loaded: Mutex<Option<(KludgineId, SharedTexture)>>,
}
impl LazyTexture {
/// Returns a new texture that loads its data to the gpu once used.
#[must_use]
pub fn from_data(
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
data: Vec<u8>,
) -> Self {
Self {
data: Arc::new(LazyTextureData {
id: sealed::TextureId::new_unique_id(),
size,
format,
usage,
filter_mode,
loaded_by_device: Mutex::default(),
data,
}),
last_loaded: Mutex::default(),
}
}
/// Returns a texture that loads `image` into the gpu when it is used.
#[must_use]
#[cfg(feature = "image")]
pub fn from_image(image: image::DynamicImage, filter_mode: wgpu::FilterMode) -> Self {
let image = image.into_rgba8();
Self::from_data(
Size::upx(image.width(), image.height()),
wgpu::TextureFormat::Rgba8UnormSrgb,
wgpu::TextureUsages::TEXTURE_BINDING,
filter_mode,
image.into_raw(),
)
}
/// Loads this texture to `graphics`, if needed, returning a
/// [`SharedTexture`].
#[must_use]
pub fn upgrade(&self, graphics: &impl sealed::KludgineGraphics) -> SharedTexture {
let mut last_loaded = self.last_loaded.lock().assert("texture lock poisoned");
if let Some(last_loaded) = &*last_loaded {
if last_loaded.0 == graphics.id() {
return last_loaded.1.clone();
}
}
let mut loaded = self
.data
.loaded_by_device
.lock()
.assert("texture lock poisoned");
if let Some(loaded) = loaded.get(&graphics.id()).and_then(Weak::upgrade) {
return SharedTexture(loaded);
}
let wgpu = graphics.device().create_texture_with_data(
graphics.queue(),
&wgpu::TextureDescriptor {
label: None,
size: self.data.size.into(),
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: self.data.format,
usage: self.data.usage,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
&self.data.data,
);
let texture = SharedTexture::from(Texture {
id: self.data.id,
kludgine: graphics.id(),
size: self.data.size,
format: self.data.format,
data: TextureInstance::from_wgpu(wgpu, false, self.data.filter_mode, graphics),
});
loaded.insert(graphics.id(), Arc::downgrade(&texture.0));
*last_loaded = Some((graphics.id(), texture.clone()));
texture
}
/// The size of the texture.
#[must_use]
pub fn size(&self) -> Size<UPx> {
self.data.size
}
}
impl Clone for LazyTexture {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
last_loaded: Mutex::default(),
}
}
}
impl CanRenderTo for LazyTexture {
fn can_render_to(&self, _kludgine: &Kludgine) -> bool {
true
}
}
impl TextureSource for LazyTexture {}
impl sealed::TextureSource for LazyTexture {
fn id(&self) -> sealed::TextureId {
self.data.id
}
fn is_mask(&self) -> bool {
// TODO this should be a flag on the texture.
self.data.format == wgpu::TextureFormat::R8Unorm
}
fn bind_group(&self, graphics: &impl sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
self.upgrade(graphics).bind_group(graphics)
}
fn default_rect(&self) -> Rect<UPx> {
self.data.size.into()
}
}
#[derive(Debug)]
struct LazyTextureData {
id: sealed::TextureId,
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
loaded_by_device: Mutex<AHashMap<KludgineId, Weak<Texture>>>,
data: Vec<u8>,
}
/// An image stored on the GPU.
#[derive(Debug)]
pub struct Texture {
id: sealed::TextureId,
kludgine: KludgineId,
size: Size<UPx>,
format: wgpu::TextureFormat,
data: TextureInstance,
}
#[derive(Debug)]
struct TextureInstance {
wgpu: wgpu::Texture,
view: wgpu::TextureView,
bind_group: Arc<wgpu::BindGroup>,
}
enum MaybeRef<'a, T> {
Borrowed(&'a T),
Owned(T),
}
impl<T> AsRef<T> for MaybeRef<'_, T> {
fn as_ref(&self) -> &T {
match self {
MaybeRef::Borrowed(value) => value,
MaybeRef::Owned(value) => value,
}
}
}
impl TextureInstance {
fn from_wgpu(
wgpu: wgpu::Texture,
multisampled: bool,
filter_mode: wgpu::FilterMode,
graphics: &impl sealed::KludgineGraphics,
) -> Self {
let view = wgpu.create_view(&wgpu::TextureViewDescriptor::default());
let layout = if multisampled {
MaybeRef::Owned(pipeline::bind_group_layout(graphics.device(), multisampled))
} else {
MaybeRef::Borrowed(graphics.binding_layout())
};
let bind_group = Arc::new(pipeline::bind_group(
graphics.device(),
layout.as_ref(),
graphics.uniforms(),
&view,
match filter_mode {
wgpu::FilterMode::Nearest => graphics.nearest_sampler(),
wgpu::FilterMode::Linear => graphics.linear_sampler(),
},
));
TextureInstance {
wgpu,
view,
bind_group,
}
}
}
impl Texture {
fn from_wgpu(
wgpu: wgpu::Texture,
graphics: &impl KludgineGraphics,
multisampled: bool,
size: Size<UPx>,
format: wgpu::TextureFormat,
filter_mode: wgpu::FilterMode,
) -> Self {
Self {
id: sealed::TextureId::new_unique_id(),
kludgine: graphics.id(),
size,
format,
data: TextureInstance::from_wgpu(wgpu, multisampled, filter_mode, graphics),
}
}
pub(crate) fn new_generic(
graphics: &impl KludgineGraphics,
multisample_count: u32,
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
) -> Self {
let wgpu = graphics.device().create_texture(&wgpu::TextureDescriptor {
label: None,
size: size.into(),
mip_level_count: 1,
sample_count: multisample_count,
dimension: wgpu::TextureDimension::D2,
format,
usage,
view_formats: &[],
});
Self::from_wgpu(
wgpu,
graphics,
multisample_count > 1,
size,
format,
filter_mode,
)
}
/// Creates a new texture of the given size, format, and usages.
#[must_use]
pub fn new(
graphics: &Graphics<'_>,
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
) -> Self {
Self::multisampled(graphics, 1, size, format, usage, filter_mode)
}
/// Creates a new texture of the given multisample count, size, format, and usages.
#[must_use]
pub fn multisampled(
graphics: &Graphics<'_>,
multisample_count: u32,
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
) -> Self {
Self::new_generic(
graphics,
multisample_count,
size,
format,
usage,
filter_mode,
)
}
/// Returns a new texture of the given size, format, and usages. The texture
/// is initialized with `data`. `data` must match `format`.
#[must_use]
pub fn new_with_data(
graphics: &Graphics<'_>,
size: Size<UPx>,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
filter_mode: wgpu::FilterMode,
data: &[u8],
) -> Self {
let wgpu = graphics.device().create_texture_with_data(
graphics.queue(),
&wgpu::TextureDescriptor {
label: None,
size: size.into(),
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
data,
);
Self::from_wgpu(wgpu, graphics, false, size, format, filter_mode)
}
/// Creates a texture from `image`.
#[must_use]
#[cfg(feature = "image")]
pub fn from_image(
image: image::DynamicImage,
filter_mode: wgpu::FilterMode,
graphics: &Graphics<'_>,
) -> Self {
// TODO is it better to force rgba8, or is it better to avoid the
// conversion and allow multiple texture formats?
let image = image.into_rgba8();
Self::new_with_data(
graphics,
Size::upx(image.width(), image.height()),
wgpu::TextureFormat::Rgba8UnormSrgb,
wgpu::TextureUsages::TEXTURE_BINDING,
filter_mode,
image.as_raw(),
)
}
/// Prepares to render this texture with `size`. The returned graphic will
/// be oriented around `origin`.
#[must_use]
pub fn prepare_sized<Unit>(
&self,
origin: Origin<Unit>,
size: Size<Unit>,
graphics: &Graphics<'_>,
) -> PreparedGraphic<Unit>
where
Unit: figures::Unit + From<i32>,
Point<Unit>: Div<Unit, Output = Point<Unit>> + Neg<Output = Point<Unit>>,
Vertex<Unit>: bytemuck::Pod,
{
let origin = match origin {
Origin::TopLeft => Point::default(),
Origin::Center => -(Point::from_vec(size) / Unit::from(2)),
Origin::Custom(point) => point,
};
self.prepare(Rect::new(origin, size), graphics)
}
/// Prepares to render this texture at the given location.
#[must_use]
pub fn prepare<Unit>(&self, dest: Rect<Unit>, graphics: &Graphics<'_>) -> PreparedGraphic<Unit>
where
Unit: figures::Unit,
Vertex<Unit>: bytemuck::Pod,
{
self.prepare_partial(self.size().into(), dest, graphics)
}
/// Prepares the `source` area to be rendered at `dest`.
#[must_use]
pub fn prepare_partial<Unit>(
&self,
source: Rect<UPx>,
dest: Rect<Unit>,
graphics: &Graphics<'_>,
) -> PreparedGraphic<Unit>
where
Unit: figures::Unit,
Vertex<Unit>: bytemuck::Pod,
{
TextureBlit::new(source, dest, Color::WHITE).prepare(Some(self), graphics)
}
/// The size of the texture.
#[must_use]
pub const fn size(&self) -> Size<UPx> {
self.size
}
/// The format of the texture.
#[must_use]
pub const fn format(&self) -> wgpu::TextureFormat {
self.format
}
/// Copies the contents of this texture into `destination`.
pub fn copy_to_buffer(
&self,
destination: wgpu::ImageCopyBuffer<'_>,
encoder: &mut wgpu::CommandEncoder,
) {
self.copy_rect_to_buffer(self.default_rect(), destination, encoder);
}
/// Copies the contents of a portion of this texture into `destination`.
pub fn copy_rect_to_buffer(
&self,
source: Rect<UPx>,
destination: wgpu::ImageCopyBuffer<'_>,
encoder: &mut wgpu::CommandEncoder,
) {
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
texture: &self.data.wgpu,
mip_level: 0,
origin: source.origin.into(),
aspect: wgpu::TextureAspect::All,
},
destination,
source.size.into(),
);
}
/// Returns the underlying wgpu handle.
#[must_use]
pub const fn wgpu(&self) -> &wgpu::Texture {
&self.data.wgpu
}
/// Returns a view over the entire texture.
#[must_use]
pub const fn view(&self) -> &wgpu::TextureView {
&self.data.view
}
}
/// Loads a texture's bytes into the executable. This macro returns a result
/// containing a [`LazyTexture`].
///
/// This macro takes a single parameter, which is forwarded along to
/// [`include_bytes!`]. The bytes that are loaded are then parsed using
/// [`image::load_from_memory`] and loaded using [`LazyTexture::from_image`].
#[cfg(feature = "image")]
#[macro_export]
macro_rules! include_texture {
($path:expr) => {
$crate::include_texture!($path, $crate::wgpu::FilterMode::Nearest)
};
($path:expr, $filter_mode:expr) => {
$crate::image::load_from_memory(std::include_bytes!($path))
.map(|image| $crate::LazyTexture::from_image(image, $filter_mode))
};
}
/// The origin of a prepared graphic.
#[derive(Default, Clone, Copy, Eq, PartialEq, Debug)]
pub enum Origin<Unit> {
/// The graphic should be drawn so that the top-left of the graphic appears
/// at the rendered location. When rotating the graphic, it will rotate
/// around the top-left.
#[default]
TopLeft,
/// The grapihc should be drawn so that the center of the graphic appears at
/// the rendered location. When rotating the graphic, it will rotate around
/// the center.
Center,
/// The graphic should be drawn so that the provided relative location
/// appears at the rendered location. When rotating the graphic, it will
/// rotate around this point.
Custom(Point<Unit>),
}
/// A resource that can be checked for surface compatibility.
pub trait CanRenderTo {
/// Returns `true` if this resource can be rendered into a graphics context
/// for `kludgine`.
#[must_use]
fn can_render_to(&self, kludgine: &Kludgine) -> bool;
}
/// A type that is rendered using a texture.
pub trait TextureSource: CanRenderTo + sealed::TextureSource {}
impl CanRenderTo for Texture {
fn can_render_to(&self, kludgine: &Kludgine) -> bool {
self.kludgine == kludgine.id
}
}
impl TextureSource for Texture {}
impl sealed::TextureSource for Texture {
fn bind_group(&self, _graphics: &impl sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
self.data.bind_group.clone()
}
fn id(&self) -> sealed::TextureId {
self.id
}
fn is_mask(&self) -> bool {
// TODO this should be a flag on the texture.
self.format == wgpu::TextureFormat::R8Unorm
}
fn default_rect(&self) -> Rect<UPx> {
self.size().into()
}
}
/// A cloneable texture.
#[derive(Clone, Debug)]
pub struct SharedTexture(Arc<Texture>);
impl Eq for SharedTexture {}
impl PartialEq for SharedTexture {
fn eq(&self, other: &Self) -> bool {
self.0.id == other.0.id
}
}
impl From<Texture> for SharedTexture {
fn from(value: Texture) -> Self {
Self(Arc::new(value))
}
}
impl Deref for SharedTexture {
type Target = Texture;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// A texture that can be cloned cheaply.
#[derive(Clone, Debug)]
pub enum ShareableTexture {
/// A shared texture instance.
Shared(SharedTexture),
/// A lazy texture that loads its contents on first use.
Lazy(LazyTexture),
}
impl ShareableTexture {
/// Returns the [`SharedTexture`] from this instance, loading it if
/// necessary.
pub fn texture(&self, graphics: &impl KludgineGraphics) -> Cow<'_, SharedTexture> {
match self {
ShareableTexture::Shared(texture) => Cow::Borrowed(texture),
ShareableTexture::Lazy(texture) => Cow::Owned(texture.upgrade(graphics)),
}
}
/// The size of the texture.
#[must_use]
pub fn size(&self) -> Size<UPx> {
match self {
ShareableTexture::Shared(texture) => texture.size(),
ShareableTexture::Lazy(texture) => texture.size(),
}
}
}
impl Eq for ShareableTexture {}
impl PartialEq for ShareableTexture {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
}
}
impl CanRenderTo for ShareableTexture {
fn can_render_to(&self, kludgine: &Kludgine) -> bool {
match self {
ShareableTexture::Shared(texture) => texture.can_render_to(kludgine),
ShareableTexture::Lazy(texture) => texture.can_render_to(kludgine),
}
}
}
impl sealed::TextureSource for ShareableTexture {
fn id(&self) -> sealed::TextureId {
match self {
ShareableTexture::Shared(texture) => texture.id(),
ShareableTexture::Lazy(texture) => texture.id(),
}
}
fn is_mask(&self) -> bool {
match self {
ShareableTexture::Shared(texture) => texture.is_mask(),
ShareableTexture::Lazy(texture) => texture.is_mask(),
}
}
fn bind_group(&self, graphics: &impl sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
match self {
ShareableTexture::Shared(texture) => texture.bind_group(graphics),
ShareableTexture::Lazy(texture) => texture.bind_group(graphics),
}
}
fn default_rect(&self) -> Rect<UPx> {
match self {
ShareableTexture::Shared(texture) => texture.default_rect(),
ShareableTexture::Lazy(texture) => texture.default_rect(),
}
}
}
impl From<Texture> for ShareableTexture {
fn from(texture: Texture) -> Self {
Self::from(SharedTexture::from(texture))
}
}
impl From<SharedTexture> for ShareableTexture {
fn from(texture: SharedTexture) -> Self {
Self::Shared(texture)
}
}
impl From<LazyTexture> for ShareableTexture {
fn from(texture: LazyTexture) -> Self {
Self::Lazy(texture)
}
}
impl<'a, T> From<&'a T> for ShareableTexture
where
T: Clone + Into<Self>,
{
fn from(value: &'a T) -> Self {
value.clone().into()
}
}
/// A region of a [`SharedTexture`].
///
/// When this type is drawn, only a region of the source texture will be drawn.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TextureRegion {
texture: ShareableTexture,
region: Rect<UPx>,
}
impl TextureRegion {
/// Returns a reference to this texture that only renders a region of the
/// texture when drawn.
#[must_use]
pub fn new(texture: impl Into<ShareableTexture>, region: Rect<UPx>) -> Self {
Self {
texture: texture.into(),
region,
}
}
/// Returns the size of the region being drawn.
#[must_use]
pub const fn size(&self) -> Size<UPx> {
self.region.size
}
/// Prepares to render this texture at the given location.
#[must_use]
pub fn prepare<Unit>(&self, dest: Rect<Unit>, graphics: &Graphics<'_>) -> PreparedGraphic<Unit>
where
Unit: figures::Unit,
Vertex<Unit>: bytemuck::Pod,
{
self.texture
.texture(graphics)
.prepare_partial(self.region, dest, graphics)
}
}
impl CanRenderTo for TextureRegion {
fn can_render_to(&self, kludgine: &Kludgine) -> bool {
self.texture.can_render_to(kludgine)
}
}
impl TextureSource for TextureRegion {}
impl sealed::TextureSource for TextureRegion {
fn id(&self) -> sealed::TextureId {
self.texture.id()
}
fn is_mask(&self) -> bool {
self.texture.is_mask()
}
fn bind_group(&self, graphics: &impl sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
self.texture.bind_group(graphics)
}
fn default_rect(&self) -> Rect<UPx> {
self.region
}
}
impl From<SharedTexture> for TextureRegion {
fn from(texture: SharedTexture) -> Self {
Self::from(ShareableTexture::from(texture))
}
}
impl From<LazyTexture> for TextureRegion {
fn from(texture: LazyTexture) -> Self {
Self::from(ShareableTexture::from(texture))
}
}
impl From<ShareableTexture> for TextureRegion {
fn from(texture: ShareableTexture) -> Self {
Self {
region: texture.default_rect(),
texture,
}
}
}
/// A type that can be any [`TextureSource`] implementation that is provided by
/// Kludgine.
///
/// This type is useful if you are designing a type that supports drawing a
/// configurable texture, but you don't care whether it's a [`Texture`],
/// [`SharedTexture`], [`TextureRegion`], or [`CollectedTexture`].
#[derive(Debug)]
pub enum AnyTexture {
/// A [`Texture`].
Texture(Texture),
/// A [`LazyTexture`].
Lazy(LazyTexture),
/// A [`SharedTexture`].
Shared(SharedTexture),
/// A [`TextureRegion`].
Region(TextureRegion),
/// A [`CollectedTexture`].
Collected(CollectedTexture),
}
impl From<Texture> for AnyTexture {
fn from(texture: Texture) -> Self {
Self::Texture(texture)
}
}
impl From<LazyTexture> for AnyTexture {
fn from(texture: LazyTexture) -> Self {
Self::Lazy(texture)
}
}
impl From<SharedTexture> for AnyTexture {
fn from(texture: SharedTexture) -> Self {
Self::Shared(texture)
}
}
impl From<TextureRegion> for AnyTexture {
fn from(texture: TextureRegion) -> Self {
Self::Region(texture)
}
}
impl From<CollectedTexture> for AnyTexture {
fn from(texture: CollectedTexture) -> Self {
Self::Collected(texture)
}
}
impl From<ShareableTexture> for AnyTexture {
fn from(texture: ShareableTexture) -> Self {
match texture {
ShareableTexture::Shared(texture) => Self::Shared(texture),
ShareableTexture::Lazy(texture) => Self::Lazy(texture),
}
}
}
impl AnyTexture {
/// Returns the size of the texture.
pub fn size(&self) -> Size<UPx> {
self.default_rect().size
}
}
impl CanRenderTo for AnyTexture {
fn can_render_to(&self, kludgine: &Kludgine) -> bool {
match self {
AnyTexture::Texture(texture) => texture.can_render_to(kludgine),
AnyTexture::Lazy(texture) => texture.can_render_to(kludgine),
AnyTexture::Collected(texture) => texture.can_render_to(kludgine),
AnyTexture::Shared(texture) => texture.can_render_to(kludgine),
AnyTexture::Region(texture) => texture.can_render_to(kludgine),
}
}
}
impl TextureSource for AnyTexture {}
impl sealed::TextureSource for AnyTexture {
fn id(&self) -> sealed::TextureId {
match self {
AnyTexture::Texture(texture) => texture.id(),
AnyTexture::Lazy(texture) => texture.id(),
AnyTexture::Collected(texture) => texture.id(),
AnyTexture::Shared(texture) => texture.id(),
AnyTexture::Region(texture) => texture.id(),
}
}
fn is_mask(&self) -> bool {
match self {
AnyTexture::Texture(texture) => texture.is_mask(),
AnyTexture::Lazy(texture) => texture.is_mask(),
AnyTexture::Collected(texture) => texture.is_mask(),
AnyTexture::Shared(texture) => texture.is_mask(),
AnyTexture::Region(texture) => texture.is_mask(),
}
}
fn bind_group(&self, graphics: &impl sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
match self {
AnyTexture::Texture(texture) => texture.bind_group(graphics),
AnyTexture::Lazy(texture) => texture.bind_group(graphics),
AnyTexture::Collected(texture) => texture.bind_group(graphics),
AnyTexture::Shared(texture) => texture.bind_group(graphics),
AnyTexture::Region(texture) => texture.bind_group(graphics),
}
}
fn default_rect(&self) -> Rect<UPx> {
match self {
AnyTexture::Texture(texture) => texture.default_rect(),
AnyTexture::Lazy(texture) => texture.default_rect(),
AnyTexture::Collected(texture) => texture.default_rect(),
AnyTexture::Shared(texture) => texture.default_rect(),
AnyTexture::Region(texture) => texture.default_rect(),
}
}
}
#[derive(Default)]
struct DefaultHasher(AHasher);
impl BuildHasher for DefaultHasher {
type Hasher = AHasher;
fn build_hasher(&self) -> Self::Hasher {
self.0.clone()
}
}
#[derive(Default, Debug)]
struct VertexCollection<T> {
vertices: Vec<Vertex<T>>,
vertex_index_by_id: HashMap<VertexId, u32, DefaultHasher>,
}
impl<T> VertexCollection<T> {
fn get_or_insert(&mut self, vertex: Vertex<T>) -> u32
where
T: Copy,
Vertex<T>: Into<Vertex<i32>>,
{
*self
.vertex_index_by_id
.entry(VertexId(vertex.into()))
.or_insert_with(|| {
let index = self
.vertices
.len()
.try_into()
.expect("too many drawn verticies");
self.vertices.push(vertex);
index
})
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
struct VertexId(Vertex<i32>);
impl hash::Hash for VertexId {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
bytemuck::bytes_of(&self.0).hash(state);
}
}
/// A source of triangle data for a shape.
pub trait ShapeSource<Unit, const TEXTURED: bool>:
DrawableSource + sealed::ShapeSource<Unit>
{
}
impl<Unit> ShapeSource<Unit, true> for TextureBlit<Unit> where Unit: Add<Output = Unit> + Ord + Copy {}
impl<Unit> DrawableSource for TextureBlit<Unit> where Unit: Add<Output = Unit> + Ord + Copy {}
impl<Unit> sealed::ShapeSource<Unit> for TextureBlit<Unit>
where
Unit: Add<Output = Unit> + Ord + Copy,
{
fn vertices(&self) -> &[Vertex<Unit>] {
&self.verticies
}
fn indices(&self) -> &[u32] {
&[1, 0, 2, 1, 2, 3]
}
}
#[derive(Clone, Copy, Debug)]
struct TextureBlit<Unit> {
verticies: [Vertex<Unit>; 4],
}
#[cfg_attr(not(feature = "cosmic-text"), allow(dead_code))]
impl<Unit> TextureBlit<Unit> {
pub fn new(source: Rect<UPx>, dest: Rect<Unit>, color: Color) -> Self
where
Unit: Add<Output = Unit> + Ord + Copy + Default,
{
let color = srgb_to_linear(
color.red_f32(),
color.green_f32(),
color.blue_f32(),
color.alpha_f32(),
);
let (dest_top_left, dest_bottom_right) = dest.extents();
let (source_top_left, source_bottom_right) = source.extents();
Self {
verticies: [
Vertex {
location: dest_top_left,
texture: source_top_left,
color,
},
Vertex {
location: Point::new(dest_bottom_right.x, dest_top_left.y),
texture: Point::new(source_bottom_right.x, source_top_left.y),
color,
},
Vertex {
location: Point::new(dest_top_left.x, dest_bottom_right.y),
texture: Point::new(source_top_left.x, source_bottom_right.y),
color,
},
Vertex {
location: dest_bottom_right,
texture: source_bottom_right,
color,
},
],
}
}
pub const fn top_left(&self) -> &Vertex<Unit> {
&self.verticies[0]
}
// pub const fn top_right(&self) -> &Vertex<Unit> {
// &self.verticies[1]
// }
// pub const fn bottom_left(&self) -> &Vertex<Unit> {
// &self.verticies[2]
// }
pub const fn bottom_right(&self) -> &Vertex<Unit> {
&self.verticies[3]
}
pub fn translate_by(&mut self, offset: Point<Unit>)
where
Unit: AddAssign + Copy,
{
for vertex in &mut self.verticies {
vertex.location += offset;
}
}
}
/// A type that can be drawn in Kludgine.
pub trait DrawableSource {}
/// A drawable source with optional translation, rotation, and scaling.
pub struct Drawable<T, Unit> {
/// The source to draw.
pub source: T,
/// Translate the source before rendering.
pub translation: Point<Unit>,
/// Rotate the source before rendering.
pub rotation: Option<Angle>,
/// Scale the source before rendering.
pub scale: Option<f32>,
/// An opacity multiplier to apply to this drawable.
pub opacity: Option<f32>,
}
impl<'a, Unit> From<Text<'a, Unit>> for Drawable<Text<'a, Unit>, Unit>
where
Unit: Default,
{
fn from(what: Text<'a, Unit>) -> Self {
Self {
source: what,
translation: Point::default(),
rotation: None,
scale: None,
opacity: None,
}
}
}
impl<'a, T, Unit> From<&'a T> for Drawable<&'a T, Unit>
where
T: DrawableSource,
Unit: Default,
{
fn from(what: &'a T) -> Self {
Self {
source: what,
translation: Point::default(),
rotation: None,
scale: None,
opacity: None,
}
}
}
/// Translation, rotation, and scaling for drawable types.
pub trait DrawableExt<Source, Unit> {
/// Translates `self` by `point`.
fn translate_by(self, point: Point<Unit>) -> Drawable<Source, Unit>;
/// Rotates `self` by `angle`.
fn rotate_by(self, angle: Angle) -> Drawable<Source, Unit>;
/// Scales `self` by `factor`.
fn scale(self, factor: f32) -> Drawable<Source, Unit>;
/// Renders this drawable with `opacity`, ranged from 0.- to 1.0.
fn opacity(self, opacity: f32) -> Drawable<Source, Unit>;
}
impl<T, Unit> DrawableExt<T, Unit> for Drawable<T, Unit> {
fn translate_by(mut self, point: Point<Unit>) -> Drawable<T, Unit> {
self.translation = point;
self
}
fn rotate_by(mut self, angle: Angle) -> Drawable<T, Unit> {
self.rotation = Some(angle);
self
}
fn scale(mut self, factor: f32) -> Drawable<T, Unit> {
self.scale = Some(factor);
self
}
fn opacity(mut self, opacity: f32) -> Drawable<T, Unit> {
self.opacity = Some(opacity.clamp(0., 1.));
self
}
}
impl<T, Unit> DrawableExt<T, Unit> for T
where
Drawable<T, Unit>: From<T>,
Unit: Default,
{
fn translate_by(self, point: Point<Unit>) -> Drawable<T, Unit> {
Drawable::from(self).translate_by(point)
}
fn rotate_by(self, angle: Angle) -> Drawable<T, Unit> {
Drawable::from(self).rotate_by(angle)
}
fn scale(self, factor: f32) -> Drawable<T, Unit> {
Drawable::from(self).scale(factor)
}
fn opacity(self, opacity: f32) -> Drawable<T, Unit> {
Drawable::from(self).opacity(opacity)
}
}