@holtjs/stage 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/{game-COsuqGzC.d.ts → game-CT_WCPjJ.d.ts} +95 -3
- package/dist/{game-2dS3Qsrn.js → game-DZoRjZNH.js} +1 -1
- package/dist/headless.d.ts +14 -3
- package/dist/headless.js +64 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/joystick.d.ts +1 -1
- package/dist/kernel.d.ts +1 -1
- package/dist/lighting.d.ts +1 -1
- package/dist/projection.d.ts +1 -1
- package/dist/{renderer-webgpu-CHJxmCvR.js → renderer-webgpu-B8vktrUk.js} +1710 -22
- package/dist/renderer-webgpu.d.ts +2 -2
- package/dist/renderer-webgpu.js +1 -1
- package/dist/rig2d.d.ts +1 -1
- package/dist/tilemap.d.ts +1 -1
- package/dist/trace.d.ts +1 -1
- package/dist/world.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1625,9 +1625,1480 @@ var Renderer2D = class Renderer2D extends EventEmitter {
|
|
|
1625
1625
|
this.tintCtx = null;
|
|
1626
1626
|
}
|
|
1627
1627
|
};
|
|
1628
|
+
// src/webgpu-headless.ts
|
|
1629
|
+
const dynamicImport = (specifier) => import(
|
|
1630
|
+
/* @vite-ignore */
|
|
1631
|
+
specifier
|
|
1632
|
+
);
|
|
1633
|
+
function isBunRuntime() {
|
|
1634
|
+
const g = globalThis;
|
|
1635
|
+
return Boolean(g.Bun && typeof g.Bun.version === "string");
|
|
1636
|
+
}
|
|
1637
|
+
function getProcessLike() {
|
|
1638
|
+
return globalThis.process ?? null;
|
|
1639
|
+
}
|
|
1640
|
+
async function tryLoadExternalModule(specifier, explicit) {
|
|
1641
|
+
if (explicit) return explicit;
|
|
1642
|
+
try {
|
|
1643
|
+
const mod = await dynamicImport(specifier);
|
|
1644
|
+
return mod?.default ?? mod;
|
|
1645
|
+
} catch {}
|
|
1646
|
+
const proc = getProcessLike();
|
|
1647
|
+
if (!proc || typeof proc.cwd !== "function") return null;
|
|
1648
|
+
try {
|
|
1649
|
+
const [modNs, pathNs, urlNs] = await Promise.all([
|
|
1650
|
+
dynamicImport("node:module"),
|
|
1651
|
+
dynamicImport("node:path"),
|
|
1652
|
+
dynamicImport("node:url")
|
|
1653
|
+
]);
|
|
1654
|
+
const req = modNs.createRequire(pathNs.join(proc.cwd(), "__stage_host_resolve__.cjs"));
|
|
1655
|
+
try {
|
|
1656
|
+
return req(specifier);
|
|
1657
|
+
} catch {
|
|
1658
|
+
const resolved = req.resolve(specifier);
|
|
1659
|
+
const esm = await dynamicImport(urlNs.pathToFileURL(resolved).href);
|
|
1660
|
+
return esm?.default ?? esm;
|
|
1661
|
+
}
|
|
1662
|
+
} catch {
|
|
1663
|
+
return null;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
async function resolveWgpuLibPath(explicitPath) {
|
|
1667
|
+
const proc = getProcessLike();
|
|
1668
|
+
const envPath = explicitPath ?? proc?.env?.STAGE_WGPU_LIB ?? proc?.env?.WGPU_NATIVE_LIB ?? null;
|
|
1669
|
+
if (envPath) return String(envPath);
|
|
1670
|
+
if (!proc) return null;
|
|
1671
|
+
try {
|
|
1672
|
+
const [fsNs, pathNs] = await Promise.all([dynamicImport("node:fs"), dynamicImport("node:path")]);
|
|
1673
|
+
const plat = proc.platform ?? "linux";
|
|
1674
|
+
const libNames = plat === "darwin" ? ["libwgpu_native.dylib"] : plat === "win32" ? ["wgpu_native.dll", "libwgpu_native.dll"] : ["libwgpu_native.so"];
|
|
1675
|
+
const cwd = typeof proc.cwd === "function" ? proc.cwd() : ".";
|
|
1676
|
+
const searchDirs = [
|
|
1677
|
+
cwd,
|
|
1678
|
+
pathNs.join(cwd, "lib"),
|
|
1679
|
+
pathNs.join(cwd, "native"),
|
|
1680
|
+
"/usr/local/lib",
|
|
1681
|
+
"/usr/lib",
|
|
1682
|
+
"/opt/homebrew/lib"
|
|
1683
|
+
];
|
|
1684
|
+
for (const dir of searchDirs) for (const name of libNames) {
|
|
1685
|
+
const candidate = pathNs.join(dir, name);
|
|
1686
|
+
if (fsNs.existsSync(candidate)) return candidate;
|
|
1687
|
+
}
|
|
1688
|
+
} catch {}
|
|
1689
|
+
return null;
|
|
1690
|
+
}
|
|
1691
|
+
const WGPUSType_ShaderSourceWGSL = 2;
|
|
1692
|
+
const WGPUCallbackMode_AllowSpontaneous = 3;
|
|
1693
|
+
const WGPUPowerPreference_LowPower = 1;
|
|
1694
|
+
const WGPUPowerPreference_HighPerformance = 2;
|
|
1695
|
+
const WGPUTextureDimension_2D = 2;
|
|
1696
|
+
const WGPUTextureFormat_RGBA8Unorm = 22;
|
|
1697
|
+
const WGPUTextureFormat_BGRA8Unorm = 27;
|
|
1698
|
+
const WGPUTextureViewDimension_2D = 2;
|
|
1699
|
+
const WGPUTextureAspect_All = 1;
|
|
1700
|
+
const WGPUFilterMode_Nearest = 1;
|
|
1701
|
+
const WGPUFilterMode_Linear = 2;
|
|
1702
|
+
const WGPUAddressMode_ClampToEdge = 1;
|
|
1703
|
+
const WGPUAddressMode_Repeat = 2;
|
|
1704
|
+
const WGPUBufferBindingType_Uniform = 2;
|
|
1705
|
+
const WGPUBufferBindingType_Storage = 3;
|
|
1706
|
+
const WGPUBufferBindingType_ReadOnlyStorage = 4;
|
|
1707
|
+
const WGPUSamplerBindingType_Filtering = 2;
|
|
1708
|
+
const WGPUSamplerBindingType_NonFiltering = 3;
|
|
1709
|
+
const WGPUSamplerBindingType_Comparison = 4;
|
|
1710
|
+
const WGPUTextureSampleType_Float = 2;
|
|
1711
|
+
const WGPUVertexStepMode_Vertex = 1;
|
|
1712
|
+
const WGPUVertexStepMode_Instance = 2;
|
|
1713
|
+
const WGPUVertexFormat_Float32 = 28;
|
|
1714
|
+
const WGPUVertexFormat_Float32x2 = 29;
|
|
1715
|
+
const WGPUVertexFormat_Float32x3 = 30;
|
|
1716
|
+
const WGPUVertexFormat_Float32x4 = 31;
|
|
1717
|
+
const WGPUVertexFormat_Uint32 = 32;
|
|
1718
|
+
const WGPUVertexFormat_Sint32 = 36;
|
|
1719
|
+
const WGPUPrimitiveTopology_TriangleList = 4;
|
|
1720
|
+
const WGPUIndexFormat_Uint16 = 1;
|
|
1721
|
+
const WGPUIndexFormat_Uint32 = 2;
|
|
1722
|
+
const WGPUFrontFace_CCW = 1;
|
|
1723
|
+
const WGPUCullMode_None = 1;
|
|
1724
|
+
const WGPUColorWriteMask_All = 15n;
|
|
1725
|
+
const WGPULoadOp_Load = 1;
|
|
1726
|
+
const WGPULoadOp_Clear = 2;
|
|
1727
|
+
const WGPUStoreOp_Store = 1;
|
|
1728
|
+
const WGPUMapMode_Read = 1n;
|
|
1729
|
+
const WGPU_DEPTH_SLICE_UNDEFINED = 0xffffffff;
|
|
1730
|
+
function mapBlendFactor(f) {
|
|
1731
|
+
switch (f) {
|
|
1732
|
+
case "zero": return 1;
|
|
1733
|
+
case "one": return 2;
|
|
1734
|
+
case "src": return 3;
|
|
1735
|
+
case "one-minus-src": return 4;
|
|
1736
|
+
case "src-alpha": return 5;
|
|
1737
|
+
case "one-minus-src-alpha": return 6;
|
|
1738
|
+
case "dst": return 7;
|
|
1739
|
+
case "one-minus-dst": return 8;
|
|
1740
|
+
case "dst-alpha": return 9;
|
|
1741
|
+
case "one-minus-dst-alpha": return 10;
|
|
1742
|
+
default: return 2;
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
function mapBlendOp(op) {
|
|
1746
|
+
switch (op) {
|
|
1747
|
+
case "subtract": return 2;
|
|
1748
|
+
case "reverse-subtract": return 3;
|
|
1749
|
+
case "min": return 4;
|
|
1750
|
+
case "max": return 5;
|
|
1751
|
+
default: return 1;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
function mapVertexFormat(fmt) {
|
|
1755
|
+
switch (fmt) {
|
|
1756
|
+
case "float32": return WGPUVertexFormat_Float32;
|
|
1757
|
+
case "float32x2": return WGPUVertexFormat_Float32x2;
|
|
1758
|
+
case "float32x3": return WGPUVertexFormat_Float32x3;
|
|
1759
|
+
case "float32x4": return WGPUVertexFormat_Float32x4;
|
|
1760
|
+
case "uint32": return WGPUVertexFormat_Uint32;
|
|
1761
|
+
case "sint32": return WGPUVertexFormat_Sint32;
|
|
1762
|
+
default: return WGPUVertexFormat_Float32x4;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
function mapTextureFormat(fmt) {
|
|
1766
|
+
return fmt === "bgra8unorm" ? WGPUTextureFormat_BGRA8Unorm : WGPUTextureFormat_RGBA8Unorm;
|
|
1767
|
+
}
|
|
1768
|
+
const BUN_TINYCC_BRIDGE_SRC = `
|
|
1769
|
+
#include <stdint.h>
|
|
1770
|
+
#include <stddef.h>
|
|
1771
|
+
typedef struct { const char* data; size_t length; } WGPUStringView;
|
|
1772
|
+
typedef struct { const void* next; uint32_t featureLevel; uint32_t powerPreference; uint32_t forceFallback; uint32_t backendType; void* surface; } WGPURequestAdapterOptions;
|
|
1773
|
+
typedef void (*AdapterCb)(uint32_t status, void* adapter, WGPUStringView msg, void* u1, void* u2);
|
|
1774
|
+
typedef struct { const void* next; uint32_t mode; AdapterCb callback; void* u1; void* u2; } WGPURequestAdapterCallbackInfo;
|
|
1775
|
+
typedef void (*DeviceCb)(uint32_t status, void* device, WGPUStringView msg, void* u1, void* u2);
|
|
1776
|
+
typedef struct { const void* next; uint32_t mode; DeviceCb callback; void* u1; void* u2; } WGPURequestDeviceCallbackInfo;
|
|
1777
|
+
typedef void (*MapCb)(uint32_t status, WGPUStringView msg, void* u1, void* u2);
|
|
1778
|
+
typedef struct { const void* next; uint32_t mode; MapCb callback; void* u1; void* u2; } WGPUBufferMapCallbackInfo;
|
|
1779
|
+
|
|
1780
|
+
typedef uint64_t (*ReqAdapterFn)(void*, const WGPURequestAdapterOptions*, WGPURequestAdapterCallbackInfo);
|
|
1781
|
+
typedef uint64_t (*ReqDeviceFn)(void*, const void*, WGPURequestDeviceCallbackInfo);
|
|
1782
|
+
typedef uint64_t (*MapAsyncFn)(void*, uint64_t, size_t, size_t, WGPUBufferMapCallbackInfo);
|
|
1783
|
+
typedef uint32_t (*PollFn)(void*, uint32_t, const void*);
|
|
1784
|
+
typedef const void* (*GetConstRangeFn)(void*, size_t, size_t);
|
|
1785
|
+
typedef void (*UnmapFn)(void*);
|
|
1786
|
+
|
|
1787
|
+
static void on_adapter(uint32_t s, void* a, WGPUStringView m, void* u1, void* u2) { (void)m; (void)u2; if (s == 1) *(void**)u1 = a; }
|
|
1788
|
+
static void on_device(uint32_t s, void* d, WGPUStringView m, void* u1, void* u2) { (void)m; (void)u2; if (s == 1) *(void**)u1 = d; }
|
|
1789
|
+
static void on_map(uint32_t s, WGPUStringView m, void* u1, void* u2) { (void)m; (void)u2; *(uint32_t*)u1 = s; }
|
|
1790
|
+
|
|
1791
|
+
uint64_t holt_req_adapter(uint64_t fn, uint64_t inst, uint32_t powerPref) {
|
|
1792
|
+
void* adapter = NULL;
|
|
1793
|
+
WGPURequestAdapterOptions opts = { NULL, 0, powerPref, 0, 0, NULL };
|
|
1794
|
+
WGPURequestAdapterCallbackInfo cb = { NULL, 3, on_adapter, &adapter, NULL };
|
|
1795
|
+
((ReqAdapterFn)(uintptr_t)fn)((void*)(uintptr_t)inst, &opts, cb);
|
|
1796
|
+
return (uint64_t)(uintptr_t)adapter;
|
|
1797
|
+
}
|
|
1798
|
+
uint64_t holt_req_device(uint64_t fn, uint64_t adapter) {
|
|
1799
|
+
void* dev = NULL;
|
|
1800
|
+
WGPURequestDeviceCallbackInfo cb = { NULL, 3, on_device, &dev, NULL };
|
|
1801
|
+
((ReqDeviceFn)(uintptr_t)fn)((void*)(uintptr_t)adapter, NULL, cb);
|
|
1802
|
+
return (uint64_t)(uintptr_t)dev;
|
|
1803
|
+
}
|
|
1804
|
+
uint32_t holt_read_buffer_sync(
|
|
1805
|
+
uint64_t mapFn,
|
|
1806
|
+
uint64_t pollFn,
|
|
1807
|
+
uint64_t rangeFn,
|
|
1808
|
+
uint64_t unmapFn,
|
|
1809
|
+
uint64_t dev,
|
|
1810
|
+
uint64_t buf,
|
|
1811
|
+
uint64_t dst,
|
|
1812
|
+
uint64_t size
|
|
1813
|
+
) {
|
|
1814
|
+
uint32_t status = 0;
|
|
1815
|
+
WGPUBufferMapCallbackInfo cb = { NULL, 3, on_map, &status, NULL };
|
|
1816
|
+
((MapAsyncFn)(uintptr_t)mapFn)((void*)(uintptr_t)buf, 1, 0, (size_t)size, cb);
|
|
1817
|
+
((PollFn)(uintptr_t)pollFn)((void*)(uintptr_t)dev, 1, NULL);
|
|
1818
|
+
if (status != 1) return 0;
|
|
1819
|
+
const uint8_t* src = (const uint8_t*)((GetConstRangeFn)(uintptr_t)rangeFn)((void*)(uintptr_t)buf, 0, (size_t)size);
|
|
1820
|
+
if (!src) {
|
|
1821
|
+
((UnmapFn)(uintptr_t)unmapFn)((void*)(uintptr_t)buf);
|
|
1822
|
+
return 0;
|
|
1823
|
+
}
|
|
1824
|
+
uint8_t* out = (uint8_t*)(uintptr_t)dst;
|
|
1825
|
+
size_t n = (size_t)size;
|
|
1826
|
+
size_t i = 0;
|
|
1827
|
+
for (; i + 8 <= n; i += 8) *(uint64_t*)(out + i) = *(const uint64_t*)(src + i);
|
|
1828
|
+
for (; i < n; i++) out[i] = src[i];
|
|
1829
|
+
((UnmapFn)(uintptr_t)unmapFn)((void*)(uintptr_t)buf);
|
|
1830
|
+
return 1;
|
|
1831
|
+
}
|
|
1832
|
+
`;
|
|
1833
|
+
let cachedBunHelperSymbols = null;
|
|
1834
|
+
async function getBunStructBridge(bunFfi) {
|
|
1835
|
+
if (cachedBunHelperSymbols) return cachedBunHelperSymbols;
|
|
1836
|
+
const [fsNs, osNs, pathNs] = await Promise.all([
|
|
1837
|
+
dynamicImport("node:fs"),
|
|
1838
|
+
dynamicImport("node:os"),
|
|
1839
|
+
dynamicImport("node:path")
|
|
1840
|
+
]);
|
|
1841
|
+
const proc = getProcessLike();
|
|
1842
|
+
const cPath = pathNs.join(osNs.tmpdir(), `holt-stage-wgpu-cc-${proc?.pid ?? 0}.c`);
|
|
1843
|
+
fsNs.writeFileSync(cPath, BUN_TINYCC_BRIDGE_SRC);
|
|
1844
|
+
try {
|
|
1845
|
+
cachedBunHelperSymbols = bunFfi.cc({
|
|
1846
|
+
source: cPath,
|
|
1847
|
+
symbols: {
|
|
1848
|
+
holt_req_adapter: {
|
|
1849
|
+
args: [
|
|
1850
|
+
"u64",
|
|
1851
|
+
"u64",
|
|
1852
|
+
"u32"
|
|
1853
|
+
],
|
|
1854
|
+
returns: "u64"
|
|
1855
|
+
},
|
|
1856
|
+
holt_req_device: {
|
|
1857
|
+
args: ["u64", "u64"],
|
|
1858
|
+
returns: "u64"
|
|
1859
|
+
},
|
|
1860
|
+
holt_read_buffer_sync: {
|
|
1861
|
+
args: [
|
|
1862
|
+
"u64",
|
|
1863
|
+
"u64",
|
|
1864
|
+
"u64",
|
|
1865
|
+
"u64",
|
|
1866
|
+
"u64",
|
|
1867
|
+
"u64",
|
|
1868
|
+
"u64",
|
|
1869
|
+
"u64"
|
|
1870
|
+
],
|
|
1871
|
+
returns: "u32"
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}).symbols;
|
|
1875
|
+
return cachedBunHelperSymbols;
|
|
1876
|
+
} finally {
|
|
1877
|
+
try {
|
|
1878
|
+
fsNs.rmSync(cPath, { force: true });
|
|
1879
|
+
} catch {}
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
async function openBunFfiDriver(libPath) {
|
|
1883
|
+
const bunFfi = await dynamicImport("bun:ffi");
|
|
1884
|
+
const { dlopen, ptr } = bunFfi;
|
|
1885
|
+
const s = dlopen(libPath, {
|
|
1886
|
+
wgpuCreateInstance: {
|
|
1887
|
+
args: ["u64"],
|
|
1888
|
+
returns: "u64"
|
|
1889
|
+
},
|
|
1890
|
+
wgpuInstanceRelease: {
|
|
1891
|
+
args: ["u64"],
|
|
1892
|
+
returns: "void"
|
|
1893
|
+
},
|
|
1894
|
+
wgpuInstanceRequestAdapter: {
|
|
1895
|
+
args: [
|
|
1896
|
+
"u64",
|
|
1897
|
+
"u64",
|
|
1898
|
+
"u64"
|
|
1899
|
+
],
|
|
1900
|
+
returns: "u64"
|
|
1901
|
+
},
|
|
1902
|
+
wgpuAdapterRelease: {
|
|
1903
|
+
args: ["u64"],
|
|
1904
|
+
returns: "void"
|
|
1905
|
+
},
|
|
1906
|
+
wgpuAdapterRequestDevice: {
|
|
1907
|
+
args: [
|
|
1908
|
+
"u64",
|
|
1909
|
+
"u64",
|
|
1910
|
+
"u64"
|
|
1911
|
+
],
|
|
1912
|
+
returns: "u64"
|
|
1913
|
+
},
|
|
1914
|
+
wgpuDeviceGetQueue: {
|
|
1915
|
+
args: ["u64"],
|
|
1916
|
+
returns: "u64"
|
|
1917
|
+
},
|
|
1918
|
+
wgpuDevicePoll: {
|
|
1919
|
+
args: [
|
|
1920
|
+
"u64",
|
|
1921
|
+
"u32",
|
|
1922
|
+
"u64"
|
|
1923
|
+
],
|
|
1924
|
+
returns: "u32"
|
|
1925
|
+
},
|
|
1926
|
+
wgpuDeviceDestroy: {
|
|
1927
|
+
args: ["u64"],
|
|
1928
|
+
returns: "void"
|
|
1929
|
+
},
|
|
1930
|
+
wgpuDeviceRelease: {
|
|
1931
|
+
args: ["u64"],
|
|
1932
|
+
returns: "void"
|
|
1933
|
+
},
|
|
1934
|
+
wgpuQueueRelease: {
|
|
1935
|
+
args: ["u64"],
|
|
1936
|
+
returns: "void"
|
|
1937
|
+
},
|
|
1938
|
+
wgpuDeviceCreateBuffer: {
|
|
1939
|
+
args: ["u64", "u64"],
|
|
1940
|
+
returns: "u64"
|
|
1941
|
+
},
|
|
1942
|
+
wgpuBufferMapAsync: {
|
|
1943
|
+
args: [
|
|
1944
|
+
"u64",
|
|
1945
|
+
"u64",
|
|
1946
|
+
"u64",
|
|
1947
|
+
"u64",
|
|
1948
|
+
"u64"
|
|
1949
|
+
],
|
|
1950
|
+
returns: "u64"
|
|
1951
|
+
},
|
|
1952
|
+
wgpuBufferGetConstMappedRange: {
|
|
1953
|
+
args: [
|
|
1954
|
+
"u64",
|
|
1955
|
+
"u64",
|
|
1956
|
+
"u64"
|
|
1957
|
+
],
|
|
1958
|
+
returns: "u64"
|
|
1959
|
+
},
|
|
1960
|
+
wgpuBufferUnmap: {
|
|
1961
|
+
args: ["u64"],
|
|
1962
|
+
returns: "void"
|
|
1963
|
+
},
|
|
1964
|
+
wgpuBufferDestroy: {
|
|
1965
|
+
args: ["u64"],
|
|
1966
|
+
returns: "void"
|
|
1967
|
+
},
|
|
1968
|
+
wgpuBufferRelease: {
|
|
1969
|
+
args: ["u64"],
|
|
1970
|
+
returns: "void"
|
|
1971
|
+
},
|
|
1972
|
+
wgpuDeviceCreateTexture: {
|
|
1973
|
+
args: ["u64", "u64"],
|
|
1974
|
+
returns: "u64"
|
|
1975
|
+
},
|
|
1976
|
+
wgpuTextureCreateView: {
|
|
1977
|
+
args: ["u64", "u64"],
|
|
1978
|
+
returns: "u64"
|
|
1979
|
+
},
|
|
1980
|
+
wgpuTextureDestroy: {
|
|
1981
|
+
args: ["u64"],
|
|
1982
|
+
returns: "void"
|
|
1983
|
+
},
|
|
1984
|
+
wgpuTextureRelease: {
|
|
1985
|
+
args: ["u64"],
|
|
1986
|
+
returns: "void"
|
|
1987
|
+
},
|
|
1988
|
+
wgpuTextureViewRelease: {
|
|
1989
|
+
args: ["u64"],
|
|
1990
|
+
returns: "void"
|
|
1991
|
+
},
|
|
1992
|
+
wgpuDeviceCreateSampler: {
|
|
1993
|
+
args: ["u64", "u64"],
|
|
1994
|
+
returns: "u64"
|
|
1995
|
+
},
|
|
1996
|
+
wgpuSamplerRelease: {
|
|
1997
|
+
args: ["u64"],
|
|
1998
|
+
returns: "void"
|
|
1999
|
+
},
|
|
2000
|
+
wgpuDeviceCreateShaderModule: {
|
|
2001
|
+
args: ["u64", "u64"],
|
|
2002
|
+
returns: "u64"
|
|
2003
|
+
},
|
|
2004
|
+
wgpuShaderModuleRelease: {
|
|
2005
|
+
args: ["u64"],
|
|
2006
|
+
returns: "void"
|
|
2007
|
+
},
|
|
2008
|
+
wgpuDeviceCreateBindGroupLayout: {
|
|
2009
|
+
args: ["u64", "u64"],
|
|
2010
|
+
returns: "u64"
|
|
2011
|
+
},
|
|
2012
|
+
wgpuBindGroupLayoutRelease: {
|
|
2013
|
+
args: ["u64"],
|
|
2014
|
+
returns: "void"
|
|
2015
|
+
},
|
|
2016
|
+
wgpuDeviceCreatePipelineLayout: {
|
|
2017
|
+
args: ["u64", "u64"],
|
|
2018
|
+
returns: "u64"
|
|
2019
|
+
},
|
|
2020
|
+
wgpuPipelineLayoutRelease: {
|
|
2021
|
+
args: ["u64"],
|
|
2022
|
+
returns: "void"
|
|
2023
|
+
},
|
|
2024
|
+
wgpuDeviceCreateRenderPipeline: {
|
|
2025
|
+
args: ["u64", "u64"],
|
|
2026
|
+
returns: "u64"
|
|
2027
|
+
},
|
|
2028
|
+
wgpuRenderPipelineRelease: {
|
|
2029
|
+
args: ["u64"],
|
|
2030
|
+
returns: "void"
|
|
2031
|
+
},
|
|
2032
|
+
wgpuDeviceCreateBindGroup: {
|
|
2033
|
+
args: ["u64", "u64"],
|
|
2034
|
+
returns: "u64"
|
|
2035
|
+
},
|
|
2036
|
+
wgpuBindGroupRelease: {
|
|
2037
|
+
args: ["u64"],
|
|
2038
|
+
returns: "void"
|
|
2039
|
+
},
|
|
2040
|
+
wgpuDeviceCreateCommandEncoder: {
|
|
2041
|
+
args: ["u64", "u64"],
|
|
2042
|
+
returns: "u64"
|
|
2043
|
+
},
|
|
2044
|
+
wgpuCommandEncoderBeginRenderPass: {
|
|
2045
|
+
args: ["u64", "u64"],
|
|
2046
|
+
returns: "u64"
|
|
2047
|
+
},
|
|
2048
|
+
wgpuCommandEncoderCopyTextureToBuffer: {
|
|
2049
|
+
args: [
|
|
2050
|
+
"u64",
|
|
2051
|
+
"u64",
|
|
2052
|
+
"u64",
|
|
2053
|
+
"u64"
|
|
2054
|
+
],
|
|
2055
|
+
returns: "void"
|
|
2056
|
+
},
|
|
2057
|
+
wgpuCommandEncoderFinish: {
|
|
2058
|
+
args: ["u64", "u64"],
|
|
2059
|
+
returns: "u64"
|
|
2060
|
+
},
|
|
2061
|
+
wgpuCommandEncoderRelease: {
|
|
2062
|
+
args: ["u64"],
|
|
2063
|
+
returns: "void"
|
|
2064
|
+
},
|
|
2065
|
+
wgpuCommandBufferRelease: {
|
|
2066
|
+
args: ["u64"],
|
|
2067
|
+
returns: "void"
|
|
2068
|
+
},
|
|
2069
|
+
wgpuRenderPassEncoderSetPipeline: {
|
|
2070
|
+
args: ["u64", "u64"],
|
|
2071
|
+
returns: "void"
|
|
2072
|
+
},
|
|
2073
|
+
wgpuRenderPassEncoderSetBindGroup: {
|
|
2074
|
+
args: [
|
|
2075
|
+
"u64",
|
|
2076
|
+
"u32",
|
|
2077
|
+
"u64",
|
|
2078
|
+
"u64",
|
|
2079
|
+
"u64"
|
|
2080
|
+
],
|
|
2081
|
+
returns: "void"
|
|
2082
|
+
},
|
|
2083
|
+
wgpuRenderPassEncoderSetVertexBuffer: {
|
|
2084
|
+
args: [
|
|
2085
|
+
"u64",
|
|
2086
|
+
"u32",
|
|
2087
|
+
"u64",
|
|
2088
|
+
"u64",
|
|
2089
|
+
"u64"
|
|
2090
|
+
],
|
|
2091
|
+
returns: "void"
|
|
2092
|
+
},
|
|
2093
|
+
wgpuRenderPassEncoderSetIndexBuffer: {
|
|
2094
|
+
args: [
|
|
2095
|
+
"u64",
|
|
2096
|
+
"u64",
|
|
2097
|
+
"u32",
|
|
2098
|
+
"u64",
|
|
2099
|
+
"u64"
|
|
2100
|
+
],
|
|
2101
|
+
returns: "void"
|
|
2102
|
+
},
|
|
2103
|
+
wgpuRenderPassEncoderSetScissorRect: {
|
|
2104
|
+
args: [
|
|
2105
|
+
"u64",
|
|
2106
|
+
"u32",
|
|
2107
|
+
"u32",
|
|
2108
|
+
"u32",
|
|
2109
|
+
"u32"
|
|
2110
|
+
],
|
|
2111
|
+
returns: "void"
|
|
2112
|
+
},
|
|
2113
|
+
wgpuRenderPassEncoderDrawIndexed: {
|
|
2114
|
+
args: [
|
|
2115
|
+
"u64",
|
|
2116
|
+
"u32",
|
|
2117
|
+
"u32",
|
|
2118
|
+
"u32",
|
|
2119
|
+
"i32",
|
|
2120
|
+
"u32"
|
|
2121
|
+
],
|
|
2122
|
+
returns: "void"
|
|
2123
|
+
},
|
|
2124
|
+
wgpuRenderPassEncoderEnd: {
|
|
2125
|
+
args: ["u64"],
|
|
2126
|
+
returns: "void"
|
|
2127
|
+
},
|
|
2128
|
+
wgpuRenderPassEncoderRelease: {
|
|
2129
|
+
args: ["u64"],
|
|
2130
|
+
returns: "void"
|
|
2131
|
+
},
|
|
2132
|
+
wgpuQueueWriteBuffer: {
|
|
2133
|
+
args: [
|
|
2134
|
+
"u64",
|
|
2135
|
+
"u64",
|
|
2136
|
+
"u64",
|
|
2137
|
+
"u64",
|
|
2138
|
+
"u64"
|
|
2139
|
+
],
|
|
2140
|
+
returns: "void"
|
|
2141
|
+
},
|
|
2142
|
+
wgpuQueueWriteTexture: {
|
|
2143
|
+
args: [
|
|
2144
|
+
"u64",
|
|
2145
|
+
"u64",
|
|
2146
|
+
"u64",
|
|
2147
|
+
"u64",
|
|
2148
|
+
"u64",
|
|
2149
|
+
"u64"
|
|
2150
|
+
],
|
|
2151
|
+
returns: "void"
|
|
2152
|
+
},
|
|
2153
|
+
wgpuQueueSubmit: {
|
|
2154
|
+
args: [
|
|
2155
|
+
"u64",
|
|
2156
|
+
"u64",
|
|
2157
|
+
"u64"
|
|
2158
|
+
],
|
|
2159
|
+
returns: "void"
|
|
2160
|
+
}
|
|
2161
|
+
}).symbols;
|
|
2162
|
+
const helper = await getBunStructBridge(bunFfi);
|
|
2163
|
+
const reqAdapterFnPtr = BigInt(s.wgpuInstanceRequestAdapter.ptr);
|
|
2164
|
+
const reqDeviceFnPtr = BigInt(s.wgpuAdapterRequestDevice.ptr);
|
|
2165
|
+
const mapAsyncFnPtr = BigInt(s.wgpuBufferMapAsync.ptr);
|
|
2166
|
+
const pollFnPtr = BigInt(s.wgpuDevicePoll.ptr);
|
|
2167
|
+
const rangeFnPtr = BigInt(s.wgpuBufferGetConstMappedRange.ptr);
|
|
2168
|
+
const unmapFnPtr = BigInt(s.wgpuBufferUnmap.ptr);
|
|
2169
|
+
const submitScratch = /* @__PURE__ */ new BigUint64Array(1);
|
|
2170
|
+
const submitScratchU8 = new Uint8Array(submitScratch.buffer);
|
|
2171
|
+
const nonEmptyDummy = /* @__PURE__ */ new Uint8Array(4);
|
|
2172
|
+
const addrOf = (buf) => {
|
|
2173
|
+
if (buf.byteLength === 0) return BigInt(ptr(nonEmptyDummy));
|
|
2174
|
+
return BigInt(ptr(buf));
|
|
2175
|
+
};
|
|
2176
|
+
return {
|
|
2177
|
+
backend: "wgpu-bun-ffi",
|
|
2178
|
+
addrOf,
|
|
2179
|
+
createInstance: () => BigInt(s.wgpuCreateInstance(0n)),
|
|
2180
|
+
instanceRelease: (inst) => s.wgpuInstanceRelease(inst),
|
|
2181
|
+
requestAdapter: (inst, powerPref) => BigInt(helper.holt_req_adapter(reqAdapterFnPtr, inst, powerPref)),
|
|
2182
|
+
adapterRelease: (a) => s.wgpuAdapterRelease(a),
|
|
2183
|
+
requestDevice: (a) => BigInt(helper.holt_req_device(reqDeviceFnPtr, a)),
|
|
2184
|
+
deviceGetQueue: (d) => BigInt(s.wgpuDeviceGetQueue(d)),
|
|
2185
|
+
devicePoll: (d, wait) => Number(s.wgpuDevicePoll(d, wait, 0n)),
|
|
2186
|
+
deviceDestroy: (d) => s.wgpuDeviceDestroy(d),
|
|
2187
|
+
deviceRelease: (d) => s.wgpuDeviceRelease(d),
|
|
2188
|
+
queueRelease: (q) => s.wgpuQueueRelease(q),
|
|
2189
|
+
deviceCreateBuffer: (d, p) => BigInt(s.wgpuDeviceCreateBuffer(d, p)),
|
|
2190
|
+
bufferDestroy: (b) => s.wgpuBufferDestroy(b),
|
|
2191
|
+
bufferRelease: (b) => s.wgpuBufferRelease(b),
|
|
2192
|
+
readBufferSync: (dev, buf, dst, byteLength) => Number(helper.holt_read_buffer_sync(mapAsyncFnPtr, pollFnPtr, rangeFnPtr, unmapFnPtr, dev, buf, addrOf(dst), BigInt(byteLength))) === 1,
|
|
2193
|
+
deviceCreateTexture: (d, p) => BigInt(s.wgpuDeviceCreateTexture(d, p)),
|
|
2194
|
+
textureCreateView: (t, p) => BigInt(s.wgpuTextureCreateView(t, p)),
|
|
2195
|
+
textureDestroy: (t) => s.wgpuTextureDestroy(t),
|
|
2196
|
+
textureRelease: (t) => s.wgpuTextureRelease(t),
|
|
2197
|
+
textureViewRelease: (v) => s.wgpuTextureViewRelease(v),
|
|
2198
|
+
deviceCreateSampler: (d, p) => BigInt(s.wgpuDeviceCreateSampler(d, p)),
|
|
2199
|
+
samplerRelease: (sp) => s.wgpuSamplerRelease(sp),
|
|
2200
|
+
deviceCreateShaderModule: (d, p) => BigInt(s.wgpuDeviceCreateShaderModule(d, p)),
|
|
2201
|
+
shaderModuleRelease: (m) => s.wgpuShaderModuleRelease(m),
|
|
2202
|
+
deviceCreateBindGroupLayout: (d, p) => BigInt(s.wgpuDeviceCreateBindGroupLayout(d, p)),
|
|
2203
|
+
bindGroupLayoutRelease: (bgl) => s.wgpuBindGroupLayoutRelease(bgl),
|
|
2204
|
+
deviceCreatePipelineLayout: (d, p) => BigInt(s.wgpuDeviceCreatePipelineLayout(d, p)),
|
|
2205
|
+
pipelineLayoutRelease: (pl) => s.wgpuPipelineLayoutRelease(pl),
|
|
2206
|
+
deviceCreateRenderPipeline: (d, p) => BigInt(s.wgpuDeviceCreateRenderPipeline(d, p)),
|
|
2207
|
+
renderPipelineRelease: (rp) => s.wgpuRenderPipelineRelease(rp),
|
|
2208
|
+
deviceCreateBindGroup: (d, p) => BigInt(s.wgpuDeviceCreateBindGroup(d, p)),
|
|
2209
|
+
bindGroupRelease: (bg) => s.wgpuBindGroupRelease(bg),
|
|
2210
|
+
deviceCreateCommandEncoder: (d, p) => BigInt(s.wgpuDeviceCreateCommandEncoder(d, p)),
|
|
2211
|
+
commandEncoderBeginRenderPass: (e, p) => BigInt(s.wgpuCommandEncoderBeginRenderPass(e, p)),
|
|
2212
|
+
commandEncoderCopyTextureToBuffer: (e, src, dst, sz) => s.wgpuCommandEncoderCopyTextureToBuffer(e, src, dst, sz),
|
|
2213
|
+
commandEncoderFinish: (e, p) => BigInt(s.wgpuCommandEncoderFinish(e, p)),
|
|
2214
|
+
commandEncoderRelease: (e) => s.wgpuCommandEncoderRelease(e),
|
|
2215
|
+
commandBufferRelease: (cb) => s.wgpuCommandBufferRelease(cb),
|
|
2216
|
+
renderPassSetPipeline: (p, rp) => s.wgpuRenderPassEncoderSetPipeline(p, rp),
|
|
2217
|
+
renderPassSetBindGroup: (p, idx, bg) => s.wgpuRenderPassEncoderSetBindGroup(p, idx, bg, 0n, 0n),
|
|
2218
|
+
renderPassSetVertexBuffer: (p, slot, buf, off, sz) => s.wgpuRenderPassEncoderSetVertexBuffer(p, slot, buf, off, sz),
|
|
2219
|
+
renderPassSetIndexBuffer: (p, buf, fmt, off, sz) => s.wgpuRenderPassEncoderSetIndexBuffer(p, buf, fmt, off, sz),
|
|
2220
|
+
renderPassSetScissorRect: (p, x, y, w, h) => s.wgpuRenderPassEncoderSetScissorRect(p, x, y, w, h),
|
|
2221
|
+
renderPassDrawIndexed: (p, ic, instC, fi, bv, fInst) => s.wgpuRenderPassEncoderDrawIndexed(p, ic, instC, fi, bv, fInst),
|
|
2222
|
+
renderPassEnd: (p) => s.wgpuRenderPassEncoderEnd(p),
|
|
2223
|
+
renderPassRelease: (p) => s.wgpuRenderPassEncoderRelease(p),
|
|
2224
|
+
queueWriteBuffer: (q, b, off, dp, sz) => s.wgpuQueueWriteBuffer(q, b, off, dp, sz),
|
|
2225
|
+
queueWriteTexture: (q, dst, dp, dsz, lay, sz) => s.wgpuQueueWriteTexture(q, dst, dp, dsz, lay, sz),
|
|
2226
|
+
queueSubmitOne: (q, cmdBuf) => {
|
|
2227
|
+
submitScratch[0] = cmdBuf;
|
|
2228
|
+
s.wgpuQueueSubmit(q, 1n, addrOf(submitScratchU8));
|
|
2229
|
+
}
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
const koffiTypesMemo = /* @__PURE__ */ new WeakMap();
|
|
2233
|
+
function getKoffiTypes(koffi) {
|
|
2234
|
+
const existing = koffiTypesMemo.get(koffi);
|
|
2235
|
+
if (existing) return existing;
|
|
2236
|
+
koffi.struct("HoltWgpuStringView", {
|
|
2237
|
+
data: "uint64_t",
|
|
2238
|
+
length: "size_t"
|
|
2239
|
+
});
|
|
2240
|
+
const ExactAdapterCb = koffi.proto("void HoltWgpuAdapterCb(uint32_t status, uint64_t adapter, HoltWgpuStringView msg, uint64_t u1, uint64_t u2)");
|
|
2241
|
+
const AdapterCbInfo = koffi.struct("HoltWgpuAdapterCbInfo", {
|
|
2242
|
+
nextInChain: "uint64_t",
|
|
2243
|
+
mode: "uint32_t",
|
|
2244
|
+
_pad: "uint32_t",
|
|
2245
|
+
callback: koffi.pointer(ExactAdapterCb),
|
|
2246
|
+
userdata1: "uint64_t",
|
|
2247
|
+
userdata2: "uint64_t"
|
|
2248
|
+
});
|
|
2249
|
+
const ExactDeviceCb = koffi.proto("void HoltWgpuDeviceCb(uint32_t status, uint64_t device, HoltWgpuStringView msg, uint64_t u1, uint64_t u2)");
|
|
2250
|
+
const DeviceCbInfo = koffi.struct("HoltWgpuDeviceCbInfo", {
|
|
2251
|
+
nextInChain: "uint64_t",
|
|
2252
|
+
mode: "uint32_t",
|
|
2253
|
+
_pad: "uint32_t",
|
|
2254
|
+
callback: koffi.pointer(ExactDeviceCb),
|
|
2255
|
+
userdata1: "uint64_t",
|
|
2256
|
+
userdata2: "uint64_t"
|
|
2257
|
+
});
|
|
2258
|
+
const ExactMapCb = koffi.proto("void HoltWgpuMapCb(uint32_t status, HoltWgpuStringView msg, uint64_t u1, uint64_t u2)");
|
|
2259
|
+
const MapCbInfo = koffi.struct("HoltWgpuMapCbInfo", {
|
|
2260
|
+
nextInChain: "uint64_t",
|
|
2261
|
+
mode: "uint32_t",
|
|
2262
|
+
_pad: "uint32_t",
|
|
2263
|
+
callback: koffi.pointer(ExactMapCb),
|
|
2264
|
+
userdata1: "uint64_t",
|
|
2265
|
+
userdata2: "uint64_t"
|
|
2266
|
+
});
|
|
2267
|
+
const proc = getProcessLike();
|
|
2268
|
+
let cRuntime;
|
|
2269
|
+
try {
|
|
2270
|
+
cRuntime = koffi.load(proc?.platform === "win32" ? "ucrtbase.dll" : null);
|
|
2271
|
+
} catch {
|
|
2272
|
+
cRuntime = koffi.load(proc?.platform === "win32" ? "msvcrt.dll" : "libc.so.6");
|
|
2273
|
+
}
|
|
2274
|
+
const memcpyAddr = cRuntime.func("uint64_t memcpy(void *dst, const void *src, size_t n)");
|
|
2275
|
+
const memcpyFromPtr = cRuntime.func("uint64_t memcpy(void *dst, uint64_t src, size_t n)");
|
|
2276
|
+
const nonEmptyDummy = /* @__PURE__ */ new Uint8Array(4);
|
|
2277
|
+
const addrOf = (buf) => {
|
|
2278
|
+
const target = buf.byteLength === 0 ? nonEmptyDummy : buf;
|
|
2279
|
+
return BigInt(memcpyAddr(target, target, 0));
|
|
2280
|
+
};
|
|
2281
|
+
const copyFromPtr = (dst, srcPtr, byteLen) => {
|
|
2282
|
+
if (byteLen > 0 && srcPtr !== 0n) memcpyFromPtr(dst, srcPtr, byteLen);
|
|
2283
|
+
};
|
|
2284
|
+
const entry = {
|
|
2285
|
+
AdapterCb: ExactAdapterCb,
|
|
2286
|
+
AdapterCbInfo,
|
|
2287
|
+
DeviceCb: ExactDeviceCb,
|
|
2288
|
+
DeviceCbInfo,
|
|
2289
|
+
MapCb: ExactMapCb,
|
|
2290
|
+
MapCbInfo,
|
|
2291
|
+
addrOf,
|
|
2292
|
+
copyFromPtr
|
|
2293
|
+
};
|
|
2294
|
+
koffiTypesMemo.set(koffi, entry);
|
|
2295
|
+
return entry;
|
|
2296
|
+
}
|
|
2297
|
+
async function openKoffiDriver(libPath, explicitKoffi) {
|
|
2298
|
+
const koffi = await tryLoadExternalModule("koffi", explicitKoffi);
|
|
2299
|
+
if (!koffi || typeof koffi.load !== "function") return null;
|
|
2300
|
+
const types = getKoffiTypes(koffi);
|
|
2301
|
+
const lib = koffi.load(libPath);
|
|
2302
|
+
const fCreateInstance = lib.func("uint64_t wgpuCreateInstance(uint64_t desc)");
|
|
2303
|
+
const fInstanceRelease = lib.func("void wgpuInstanceRelease(uint64_t inst)");
|
|
2304
|
+
const fReqAdapter = lib.func("uint64_t wgpuInstanceRequestAdapter(uint64_t inst, uint64_t opts, HoltWgpuAdapterCbInfo cb)");
|
|
2305
|
+
const fAdapterRelease = lib.func("void wgpuAdapterRelease(uint64_t adapter)");
|
|
2306
|
+
const fReqDevice = lib.func("uint64_t wgpuAdapterRequestDevice(uint64_t adapter, uint64_t desc, HoltWgpuDeviceCbInfo cb)");
|
|
2307
|
+
const fDeviceGetQueue = lib.func("uint64_t wgpuDeviceGetQueue(uint64_t dev)");
|
|
2308
|
+
const fDevicePoll = lib.func("uint32_t wgpuDevicePoll(uint64_t dev, uint32_t wait, uint64_t idx)");
|
|
2309
|
+
const fDeviceDestroy = lib.func("void wgpuDeviceDestroy(uint64_t dev)");
|
|
2310
|
+
const fDeviceRelease = lib.func("void wgpuDeviceRelease(uint64_t dev)");
|
|
2311
|
+
const fQueueRelease = lib.func("void wgpuQueueRelease(uint64_t q)");
|
|
2312
|
+
const fCreateBuffer = lib.func("uint64_t wgpuDeviceCreateBuffer(uint64_t dev, uint64_t desc)");
|
|
2313
|
+
const fBufferMapAsync = lib.func("uint64_t wgpuBufferMapAsync(uint64_t buf, uint64_t mode, size_t off, size_t sz, HoltWgpuMapCbInfo cb)");
|
|
2314
|
+
const fBufferGetConstMappedRange = lib.func("uint64_t wgpuBufferGetConstMappedRange(uint64_t buf, size_t off, size_t sz)");
|
|
2315
|
+
const fBufferUnmap = lib.func("void wgpuBufferUnmap(uint64_t buf)");
|
|
2316
|
+
const fBufferDestroy = lib.func("void wgpuBufferDestroy(uint64_t buf)");
|
|
2317
|
+
const fBufferRelease = lib.func("void wgpuBufferRelease(uint64_t buf)");
|
|
2318
|
+
const fCreateTexture = lib.func("uint64_t wgpuDeviceCreateTexture(uint64_t dev, uint64_t desc)");
|
|
2319
|
+
const fTextureCreateView = lib.func("uint64_t wgpuTextureCreateView(uint64_t tex, uint64_t desc)");
|
|
2320
|
+
const fTextureDestroy = lib.func("void wgpuTextureDestroy(uint64_t tex)");
|
|
2321
|
+
const fTextureRelease = lib.func("void wgpuTextureRelease(uint64_t tex)");
|
|
2322
|
+
const fTextureViewRelease = lib.func("void wgpuTextureViewRelease(uint64_t view)");
|
|
2323
|
+
const fCreateSampler = lib.func("uint64_t wgpuDeviceCreateSampler(uint64_t dev, uint64_t desc)");
|
|
2324
|
+
const fSamplerRelease = lib.func("void wgpuSamplerRelease(uint64_t samp)");
|
|
2325
|
+
const fCreateShaderModule = lib.func("uint64_t wgpuDeviceCreateShaderModule(uint64_t dev, uint64_t desc)");
|
|
2326
|
+
const fShaderModuleRelease = lib.func("void wgpuShaderModuleRelease(uint64_t mod)");
|
|
2327
|
+
const fCreateBgl = lib.func("uint64_t wgpuDeviceCreateBindGroupLayout(uint64_t dev, uint64_t desc)");
|
|
2328
|
+
const fBglRelease = lib.func("void wgpuBindGroupLayoutRelease(uint64_t bgl)");
|
|
2329
|
+
const fCreatePl = lib.func("uint64_t wgpuDeviceCreatePipelineLayout(uint64_t dev, uint64_t desc)");
|
|
2330
|
+
const fPlRelease = lib.func("void wgpuPipelineLayoutRelease(uint64_t pl)");
|
|
2331
|
+
const fCreateRp = lib.func("uint64_t wgpuDeviceCreateRenderPipeline(uint64_t dev, uint64_t desc)");
|
|
2332
|
+
const fRpRelease = lib.func("void wgpuRenderPipelineRelease(uint64_t rp)");
|
|
2333
|
+
const fCreateBg = lib.func("uint64_t wgpuDeviceCreateBindGroup(uint64_t dev, uint64_t desc)");
|
|
2334
|
+
const fBgRelease = lib.func("void wgpuBindGroupRelease(uint64_t bg)");
|
|
2335
|
+
const fCreateCe = lib.func("uint64_t wgpuDeviceCreateCommandEncoder(uint64_t dev, uint64_t desc)");
|
|
2336
|
+
const fBeginRp = lib.func("uint64_t wgpuCommandEncoderBeginRenderPass(uint64_t enc, uint64_t desc)");
|
|
2337
|
+
const fCopyTexToBuf = lib.func("void wgpuCommandEncoderCopyTextureToBuffer(uint64_t enc, uint64_t src, uint64_t dst, uint64_t sz)");
|
|
2338
|
+
const fCeFinish = lib.func("uint64_t wgpuCommandEncoderFinish(uint64_t enc, uint64_t desc)");
|
|
2339
|
+
const fCeRelease = lib.func("void wgpuCommandEncoderRelease(uint64_t enc)");
|
|
2340
|
+
const fCbRelease = lib.func("void wgpuCommandBufferRelease(uint64_t cb)");
|
|
2341
|
+
const fRpSetPipeline = lib.func("void wgpuRenderPassEncoderSetPipeline(uint64_t pass, uint64_t rp)");
|
|
2342
|
+
const fRpSetBg = lib.func("void wgpuRenderPassEncoderSetBindGroup(uint64_t pass, uint32_t idx, uint64_t bg, size_t dynCnt, uint64_t dynOff)");
|
|
2343
|
+
const fRpSetVbo = lib.func("void wgpuRenderPassEncoderSetVertexBuffer(uint64_t pass, uint32_t slot, uint64_t buf, uint64_t off, uint64_t sz)");
|
|
2344
|
+
const fRpSetIbo = lib.func("void wgpuRenderPassEncoderSetIndexBuffer(uint64_t pass, uint64_t buf, uint32_t fmt, uint64_t off, uint64_t sz)");
|
|
2345
|
+
const fRpSetScissor = lib.func("void wgpuRenderPassEncoderSetScissorRect(uint64_t pass, uint32_t x, uint32_t y, uint32_t w, uint32_t h)");
|
|
2346
|
+
const fRpDrawIndexed = lib.func("void wgpuRenderPassEncoderDrawIndexed(uint64_t pass, uint32_t ic, uint32_t instC, uint32_t fi, int32_t bv, uint32_t fInst)");
|
|
2347
|
+
const fRpEnd = lib.func("void wgpuRenderPassEncoderEnd(uint64_t pass)");
|
|
2348
|
+
const fRpEncoderRelease = lib.func("void wgpuRenderPassEncoderRelease(uint64_t pass)");
|
|
2349
|
+
const fQueueWriteBuffer = lib.func("void wgpuQueueWriteBuffer(uint64_t q, uint64_t buf, uint64_t off, uint64_t data, size_t sz)");
|
|
2350
|
+
const fQueueWriteTexture = lib.func("void wgpuQueueWriteTexture(uint64_t q, uint64_t dst, uint64_t data, size_t dsz, uint64_t lay, uint64_t sz)");
|
|
2351
|
+
const fQueueSubmit = lib.func("void wgpuQueueSubmit(uint64_t q, size_t cnt, uint64_t cmds)");
|
|
2352
|
+
const submitScratch = /* @__PURE__ */ new BigUint64Array(1);
|
|
2353
|
+
const submitScratchU8 = new Uint8Array(submitScratch.buffer);
|
|
2354
|
+
const addrOf = types.addrOf;
|
|
2355
|
+
return {
|
|
2356
|
+
backend: "wgpu-node-koffi",
|
|
2357
|
+
addrOf,
|
|
2358
|
+
createInstance: () => BigInt(fCreateInstance(0n)),
|
|
2359
|
+
instanceRelease: (inst) => fInstanceRelease(inst),
|
|
2360
|
+
requestAdapter: (inst, powerPref) => {
|
|
2361
|
+
const optsBuf = /* @__PURE__ */ new Uint8Array(32);
|
|
2362
|
+
new DataView(optsBuf.buffer).setUint32(12, powerPref, true);
|
|
2363
|
+
let got = 0n;
|
|
2364
|
+
const cbPtr = koffi.register((status, adapter) => {
|
|
2365
|
+
if (status === 1) got = BigInt(adapter);
|
|
2366
|
+
}, koffi.pointer(types.AdapterCb));
|
|
2367
|
+
try {
|
|
2368
|
+
fReqAdapter(inst, addrOf(optsBuf), {
|
|
2369
|
+
nextInChain: 0n,
|
|
2370
|
+
mode: WGPUCallbackMode_AllowSpontaneous,
|
|
2371
|
+
_pad: 0,
|
|
2372
|
+
callback: cbPtr,
|
|
2373
|
+
userdata1: 0n,
|
|
2374
|
+
userdata2: 0n
|
|
2375
|
+
});
|
|
2376
|
+
} finally {
|
|
2377
|
+
koffi.unregister(cbPtr);
|
|
2378
|
+
}
|
|
2379
|
+
return got;
|
|
2380
|
+
},
|
|
2381
|
+
adapterRelease: (a) => fAdapterRelease(a),
|
|
2382
|
+
requestDevice: (a) => {
|
|
2383
|
+
let got = 0n;
|
|
2384
|
+
const cbPtr = koffi.register((status, device) => {
|
|
2385
|
+
if (status === 1) got = BigInt(device);
|
|
2386
|
+
}, koffi.pointer(types.DeviceCb));
|
|
2387
|
+
try {
|
|
2388
|
+
fReqDevice(a, 0n, {
|
|
2389
|
+
nextInChain: 0n,
|
|
2390
|
+
mode: WGPUCallbackMode_AllowSpontaneous,
|
|
2391
|
+
_pad: 0,
|
|
2392
|
+
callback: cbPtr,
|
|
2393
|
+
userdata1: 0n,
|
|
2394
|
+
userdata2: 0n
|
|
2395
|
+
});
|
|
2396
|
+
} finally {
|
|
2397
|
+
koffi.unregister(cbPtr);
|
|
2398
|
+
}
|
|
2399
|
+
return got;
|
|
2400
|
+
},
|
|
2401
|
+
deviceGetQueue: (d) => BigInt(fDeviceGetQueue(d)),
|
|
2402
|
+
devicePoll: (d, wait) => Number(fDevicePoll(d, wait, 0n)),
|
|
2403
|
+
deviceDestroy: (d) => fDeviceDestroy(d),
|
|
2404
|
+
deviceRelease: (d) => fDeviceRelease(d),
|
|
2405
|
+
queueRelease: (q) => fQueueRelease(q),
|
|
2406
|
+
deviceCreateBuffer: (d, p) => BigInt(fCreateBuffer(d, p)),
|
|
2407
|
+
bufferDestroy: (b) => fBufferDestroy(b),
|
|
2408
|
+
bufferRelease: (b) => fBufferRelease(b),
|
|
2409
|
+
readBufferSync: (dev, buf, dst, byteLength) => {
|
|
2410
|
+
let mapStatus = 0;
|
|
2411
|
+
const cbPtr = koffi.register((status) => {
|
|
2412
|
+
mapStatus = status;
|
|
2413
|
+
}, koffi.pointer(types.MapCb));
|
|
2414
|
+
try {
|
|
2415
|
+
fBufferMapAsync(buf, WGPUMapMode_Read, 0, byteLength, {
|
|
2416
|
+
nextInChain: 0n,
|
|
2417
|
+
mode: WGPUCallbackMode_AllowSpontaneous,
|
|
2418
|
+
_pad: 0,
|
|
2419
|
+
callback: cbPtr,
|
|
2420
|
+
userdata1: 0n,
|
|
2421
|
+
userdata2: 0n
|
|
2422
|
+
});
|
|
2423
|
+
fDevicePoll(dev, 1, 0n);
|
|
2424
|
+
} finally {
|
|
2425
|
+
koffi.unregister(cbPtr);
|
|
2426
|
+
}
|
|
2427
|
+
if (mapStatus !== 1) return false;
|
|
2428
|
+
try {
|
|
2429
|
+
const srcPtr = BigInt(fBufferGetConstMappedRange(buf, 0, byteLength));
|
|
2430
|
+
if (!srcPtr) return false;
|
|
2431
|
+
types.copyFromPtr(dst, srcPtr, byteLength);
|
|
2432
|
+
return true;
|
|
2433
|
+
} finally {
|
|
2434
|
+
fBufferUnmap(buf);
|
|
2435
|
+
}
|
|
2436
|
+
},
|
|
2437
|
+
deviceCreateTexture: (d, p) => BigInt(fCreateTexture(d, p)),
|
|
2438
|
+
textureCreateView: (t, p) => BigInt(fTextureCreateView(t, p)),
|
|
2439
|
+
textureDestroy: (t) => fTextureDestroy(t),
|
|
2440
|
+
textureRelease: (t) => fTextureRelease(t),
|
|
2441
|
+
textureViewRelease: (v) => fTextureViewRelease(v),
|
|
2442
|
+
deviceCreateSampler: (d, p) => BigInt(fCreateSampler(d, p)),
|
|
2443
|
+
samplerRelease: (sp) => fSamplerRelease(sp),
|
|
2444
|
+
deviceCreateShaderModule: (d, p) => BigInt(fCreateShaderModule(d, p)),
|
|
2445
|
+
shaderModuleRelease: (m) => fShaderModuleRelease(m),
|
|
2446
|
+
deviceCreateBindGroupLayout: (d, p) => BigInt(fCreateBgl(d, p)),
|
|
2447
|
+
bindGroupLayoutRelease: (bgl) => fBglRelease(bgl),
|
|
2448
|
+
deviceCreatePipelineLayout: (d, p) => BigInt(fCreatePl(d, p)),
|
|
2449
|
+
pipelineLayoutRelease: (pl) => fPlRelease(pl),
|
|
2450
|
+
deviceCreateRenderPipeline: (d, p) => BigInt(fCreateRp(d, p)),
|
|
2451
|
+
renderPipelineRelease: (rp) => fRpRelease(rp),
|
|
2452
|
+
deviceCreateBindGroup: (d, p) => BigInt(fCreateBg(d, p)),
|
|
2453
|
+
bindGroupRelease: (bg) => fBgRelease(bg),
|
|
2454
|
+
deviceCreateCommandEncoder: (d, p) => BigInt(fCreateCe(d, p)),
|
|
2455
|
+
commandEncoderBeginRenderPass: (e, p) => BigInt(fBeginRp(e, p)),
|
|
2456
|
+
commandEncoderCopyTextureToBuffer: (e, src, dst, sz) => fCopyTexToBuf(e, src, dst, sz),
|
|
2457
|
+
commandEncoderFinish: (e, p) => BigInt(fCeFinish(e, p)),
|
|
2458
|
+
commandEncoderRelease: (e) => fCeRelease(e),
|
|
2459
|
+
commandBufferRelease: (cb) => fCbRelease(cb),
|
|
2460
|
+
renderPassSetPipeline: (p, rp) => fRpSetPipeline(p, rp),
|
|
2461
|
+
renderPassSetBindGroup: (p, idx, bg) => fRpSetBg(p, idx, bg, 0n, 0n),
|
|
2462
|
+
renderPassSetVertexBuffer: (p, slot, buf, off, sz) => fRpSetVbo(p, slot, buf, off, sz),
|
|
2463
|
+
renderPassSetIndexBuffer: (p, buf, fmt, off, sz) => fRpSetIbo(p, buf, fmt, off, sz),
|
|
2464
|
+
renderPassSetScissorRect: (p, x, y, w, h) => fRpSetScissor(p, x, y, w, h),
|
|
2465
|
+
renderPassDrawIndexed: (p, ic, instC, fi, bv, fInst) => fRpDrawIndexed(p, ic, instC, fi, bv, fInst),
|
|
2466
|
+
renderPassEnd: (p) => fRpEnd(p),
|
|
2467
|
+
renderPassRelease: (p) => fRpEncoderRelease(p),
|
|
2468
|
+
queueWriteBuffer: (q, b, off, dp, sz) => fQueueWriteBuffer(q, b, off, dp, sz),
|
|
2469
|
+
queueWriteTexture: (q, dst, dp, dsz, lay, sz) => fQueueWriteTexture(q, dst, dp, dsz, lay, sz),
|
|
2470
|
+
queueSubmitOne: (q, cmdBuf) => {
|
|
2471
|
+
submitScratch[0] = cmdBuf;
|
|
2472
|
+
fQueueSubmit(q, 1n, addrOf(submitScratchU8));
|
|
2473
|
+
}
|
|
2474
|
+
};
|
|
2475
|
+
}
|
|
2476
|
+
function asUint8View(data, dataOffset, size) {
|
|
2477
|
+
if (ArrayBuffer.isView(data)) {
|
|
2478
|
+
const base = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
2479
|
+
const bpe = data.BYTES_PER_ELEMENT ?? 1;
|
|
2480
|
+
const byteOff = (dataOffset ?? 0) * bpe;
|
|
2481
|
+
const byteLen = size !== void 0 ? size * bpe : base.byteLength - byteOff;
|
|
2482
|
+
return base.subarray(byteOff, byteOff + byteLen);
|
|
2483
|
+
}
|
|
2484
|
+
const full = new Uint8Array(data);
|
|
2485
|
+
const byteOff = dataOffset ?? 0;
|
|
2486
|
+
const byteLen = size ?? full.byteLength - byteOff;
|
|
2487
|
+
return full.subarray(byteOff, byteOff + byteLen);
|
|
2488
|
+
}
|
|
2489
|
+
function createWgpuDeviceWrapper(drv, instance, adapter, devPtr, queuePtr) {
|
|
2490
|
+
const utf8 = new TextEncoder();
|
|
2491
|
+
let destroyed = false;
|
|
2492
|
+
const trackedResources = [];
|
|
2493
|
+
let padScratch = /* @__PURE__ */ new Uint8Array(256);
|
|
2494
|
+
const destroyAll = () => {
|
|
2495
|
+
if (destroyed) return;
|
|
2496
|
+
destroyed = true;
|
|
2497
|
+
for (let i = trackedResources.length - 1; i >= 0; i--) try {
|
|
2498
|
+
trackedResources[i]();
|
|
2499
|
+
} catch {}
|
|
2500
|
+
trackedResources.length = 0;
|
|
2501
|
+
try {
|
|
2502
|
+
drv.queueRelease(queuePtr);
|
|
2503
|
+
} catch {}
|
|
2504
|
+
try {
|
|
2505
|
+
drv.deviceDestroy(devPtr);
|
|
2506
|
+
} catch {}
|
|
2507
|
+
try {
|
|
2508
|
+
drv.deviceRelease(devPtr);
|
|
2509
|
+
} catch {}
|
|
2510
|
+
try {
|
|
2511
|
+
drv.adapterRelease(adapter);
|
|
2512
|
+
} catch {}
|
|
2513
|
+
try {
|
|
2514
|
+
drv.instanceRelease(instance);
|
|
2515
|
+
} catch {}
|
|
2516
|
+
};
|
|
2517
|
+
return {
|
|
2518
|
+
device: {
|
|
2519
|
+
queue: {
|
|
2520
|
+
writeBuffer(buffer, bufferOffset, data, dataOffset, size) {
|
|
2521
|
+
const view = asUint8View(data, dataOffset, size);
|
|
2522
|
+
if (view.byteLength === 0) return;
|
|
2523
|
+
if ((view.byteLength & 3) === 0) {
|
|
2524
|
+
drv.queueWriteBuffer(queuePtr, buffer._ptr, BigInt(bufferOffset), drv.addrOf(view), BigInt(view.byteLength));
|
|
2525
|
+
return;
|
|
2526
|
+
}
|
|
2527
|
+
const aligned = view.byteLength + 3 & -4;
|
|
2528
|
+
if (padScratch.byteLength < aligned) padScratch = new Uint8Array(aligned);
|
|
2529
|
+
padScratch.fill(0, view.byteLength, aligned);
|
|
2530
|
+
padScratch.set(view, 0);
|
|
2531
|
+
drv.queueWriteBuffer(queuePtr, buffer._ptr, BigInt(bufferOffset), drv.addrOf(padScratch), BigInt(aligned));
|
|
2532
|
+
},
|
|
2533
|
+
writeTexture(destination, data, dataLayout, size) {
|
|
2534
|
+
const view = asUint8View(data);
|
|
2535
|
+
const w = Array.isArray(size) ? size[0] ?? 1 : size.width ?? 1;
|
|
2536
|
+
const h = Array.isArray(size) ? size[1] ?? 1 : size.height ?? 1;
|
|
2537
|
+
const d = Array.isArray(size) ? size[2] ?? 1 : size.depthOrArrayLayers ?? 1;
|
|
2538
|
+
const dstBuf = /* @__PURE__ */ new Uint8Array(32);
|
|
2539
|
+
const dstDv = new DataView(dstBuf.buffer);
|
|
2540
|
+
dstDv.setBigUint64(0, destination.texture._ptr, true);
|
|
2541
|
+
dstDv.setUint32(8, destination.mipLevel ?? 0, true);
|
|
2542
|
+
dstDv.setUint32(12, destination.origin?.[0] ?? 0, true);
|
|
2543
|
+
dstDv.setUint32(16, destination.origin?.[1] ?? 0, true);
|
|
2544
|
+
dstDv.setUint32(20, destination.origin?.[2] ?? 0, true);
|
|
2545
|
+
dstDv.setUint32(24, WGPUTextureAspect_All, true);
|
|
2546
|
+
const layBuf = /* @__PURE__ */ new Uint8Array(16);
|
|
2547
|
+
const layDv = new DataView(layBuf.buffer);
|
|
2548
|
+
layDv.setBigUint64(0, BigInt(dataLayout.offset ?? 0), true);
|
|
2549
|
+
layDv.setUint32(8, dataLayout.bytesPerRow, true);
|
|
2550
|
+
layDv.setUint32(12, dataLayout.rowsPerImage ?? h, true);
|
|
2551
|
+
const extBuf = /* @__PURE__ */ new Uint8Array(12);
|
|
2552
|
+
const extDv = new DataView(extBuf.buffer);
|
|
2553
|
+
extDv.setUint32(0, w, true);
|
|
2554
|
+
extDv.setUint32(4, h, true);
|
|
2555
|
+
extDv.setUint32(8, d, true);
|
|
2556
|
+
drv.queueWriteTexture(queuePtr, drv.addrOf(dstBuf), drv.addrOf(view), BigInt(view.byteLength), drv.addrOf(layBuf), drv.addrOf(extBuf));
|
|
2557
|
+
},
|
|
2558
|
+
submit(commandBuffers) {
|
|
2559
|
+
for (let i = 0; i < commandBuffers.length; i++) {
|
|
2560
|
+
const cb = commandBuffers[i];
|
|
2561
|
+
if (!cb?._ptr) continue;
|
|
2562
|
+
drv.queueSubmitOne(queuePtr, cb._ptr);
|
|
2563
|
+
drv.commandBufferRelease(cb._ptr);
|
|
2564
|
+
cb._ptr = 0n;
|
|
2565
|
+
}
|
|
2566
|
+
drv.devicePoll(devPtr, 0);
|
|
2567
|
+
}
|
|
2568
|
+
},
|
|
2569
|
+
createShaderModule(descriptor) {
|
|
2570
|
+
const codeBytes = utf8.encode(descriptor.code);
|
|
2571
|
+
const wgslBuf = /* @__PURE__ */ new Uint8Array(32);
|
|
2572
|
+
const wgslDv = new DataView(wgslBuf.buffer);
|
|
2573
|
+
wgslDv.setUint32(8, WGPUSType_ShaderSourceWGSL, true);
|
|
2574
|
+
wgslDv.setBigUint64(16, drv.addrOf(codeBytes), true);
|
|
2575
|
+
wgslDv.setBigUint64(24, BigInt(codeBytes.byteLength), true);
|
|
2576
|
+
const modBuf = /* @__PURE__ */ new Uint8Array(24);
|
|
2577
|
+
new DataView(modBuf.buffer).setBigUint64(0, drv.addrOf(wgslBuf), true);
|
|
2578
|
+
const handle = drv.deviceCreateShaderModule(devPtr, drv.addrOf(modBuf));
|
|
2579
|
+
if (!handle) throw new Error("wgpu-native: failed to compile WGSL shader module");
|
|
2580
|
+
let released = false;
|
|
2581
|
+
const release = () => {
|
|
2582
|
+
if (!released) {
|
|
2583
|
+
released = true;
|
|
2584
|
+
drv.shaderModuleRelease(handle);
|
|
2585
|
+
}
|
|
2586
|
+
};
|
|
2587
|
+
trackedResources.push(release);
|
|
2588
|
+
return {
|
|
2589
|
+
_kind: "shader",
|
|
2590
|
+
_ptr: handle,
|
|
2591
|
+
destroy: release
|
|
2592
|
+
};
|
|
2593
|
+
},
|
|
2594
|
+
createBuffer(descriptor) {
|
|
2595
|
+
const byteSize = Math.max(4, Math.ceil(descriptor.size)) + 3 & -4;
|
|
2596
|
+
const descBuf = /* @__PURE__ */ new Uint8Array(48);
|
|
2597
|
+
const dv = new DataView(descBuf.buffer);
|
|
2598
|
+
dv.setBigUint64(24, BigInt(descriptor.usage), true);
|
|
2599
|
+
dv.setBigUint64(32, BigInt(byteSize), true);
|
|
2600
|
+
dv.setUint32(40, descriptor.mappedAtCreation ? 1 : 0, true);
|
|
2601
|
+
const handle = drv.deviceCreateBuffer(devPtr, drv.addrOf(descBuf));
|
|
2602
|
+
if (!handle) throw new Error("wgpu-native: failed to allocate GPUBuffer");
|
|
2603
|
+
let released = false;
|
|
2604
|
+
const release = () => {
|
|
2605
|
+
if (!released) {
|
|
2606
|
+
released = true;
|
|
2607
|
+
drv.bufferDestroy(handle);
|
|
2608
|
+
drv.bufferRelease(handle);
|
|
2609
|
+
}
|
|
2610
|
+
};
|
|
2611
|
+
trackedResources.push(release);
|
|
2612
|
+
return {
|
|
2613
|
+
_kind: "buffer",
|
|
2614
|
+
_ptr: handle,
|
|
2615
|
+
_byteSize: byteSize,
|
|
2616
|
+
destroy: release
|
|
2617
|
+
};
|
|
2618
|
+
},
|
|
2619
|
+
createTexture(descriptor) {
|
|
2620
|
+
const sz = descriptor.size;
|
|
2621
|
+
const w = Array.isArray(sz) ? sz[0] ?? 1 : sz?.width ?? 1;
|
|
2622
|
+
const h = Array.isArray(sz) ? sz[1] ?? 1 : sz?.height ?? 1;
|
|
2623
|
+
const d = Array.isArray(sz) ? sz[2] ?? 1 : sz?.depthOrArrayLayers ?? 1;
|
|
2624
|
+
const descBuf = /* @__PURE__ */ new Uint8Array(80);
|
|
2625
|
+
const dv = new DataView(descBuf.buffer);
|
|
2626
|
+
dv.setBigUint64(24, BigInt(descriptor.usage), true);
|
|
2627
|
+
dv.setUint32(32, WGPUTextureDimension_2D, true);
|
|
2628
|
+
dv.setUint32(36, w, true);
|
|
2629
|
+
dv.setUint32(40, h, true);
|
|
2630
|
+
dv.setUint32(44, d, true);
|
|
2631
|
+
dv.setUint32(48, mapTextureFormat(descriptor.format), true);
|
|
2632
|
+
dv.setUint32(52, descriptor.mipLevelCount ?? 1, true);
|
|
2633
|
+
dv.setUint32(56, descriptor.sampleCount ?? 1, true);
|
|
2634
|
+
const handle = drv.deviceCreateTexture(devPtr, drv.addrOf(descBuf));
|
|
2635
|
+
if (!handle) throw new Error("wgpu-native: failed to allocate GPUTexture");
|
|
2636
|
+
const views = [];
|
|
2637
|
+
let released = false;
|
|
2638
|
+
const release = () => {
|
|
2639
|
+
if (!released) {
|
|
2640
|
+
released = true;
|
|
2641
|
+
for (let i = 0; i < views.length; i++) drv.textureViewRelease(views[i]);
|
|
2642
|
+
views.length = 0;
|
|
2643
|
+
drv.textureDestroy(handle);
|
|
2644
|
+
drv.textureRelease(handle);
|
|
2645
|
+
}
|
|
2646
|
+
};
|
|
2647
|
+
trackedResources.push(release);
|
|
2648
|
+
return {
|
|
2649
|
+
_kind: "texture",
|
|
2650
|
+
_ptr: handle,
|
|
2651
|
+
width: w,
|
|
2652
|
+
height: h,
|
|
2653
|
+
createView: () => {
|
|
2654
|
+
const v = drv.textureCreateView(handle, 0n);
|
|
2655
|
+
if (!v) throw new Error("wgpu-native: failed to create GPUTextureView");
|
|
2656
|
+
views.push(v);
|
|
2657
|
+
return {
|
|
2658
|
+
_kind: "view",
|
|
2659
|
+
_ptr: v
|
|
2660
|
+
};
|
|
2661
|
+
},
|
|
2662
|
+
destroy: release
|
|
2663
|
+
};
|
|
2664
|
+
},
|
|
2665
|
+
createSampler(descriptor) {
|
|
2666
|
+
const descBuf = /* @__PURE__ */ new Uint8Array(64);
|
|
2667
|
+
const dv = new DataView(descBuf.buffer);
|
|
2668
|
+
const addrU = descriptor?.addressModeU === "repeat" ? WGPUAddressMode_Repeat : WGPUAddressMode_ClampToEdge;
|
|
2669
|
+
const addrV = descriptor?.addressModeV === "repeat" ? WGPUAddressMode_Repeat : WGPUAddressMode_ClampToEdge;
|
|
2670
|
+
const mag = descriptor?.magFilter === "linear" ? WGPUFilterMode_Linear : WGPUFilterMode_Nearest;
|
|
2671
|
+
const min = descriptor?.minFilter === "linear" ? WGPUFilterMode_Linear : WGPUFilterMode_Nearest;
|
|
2672
|
+
const mip = descriptor?.mipmapFilter === "linear" ? WGPUFilterMode_Linear : WGPUFilterMode_Nearest;
|
|
2673
|
+
dv.setUint32(24, addrU, true);
|
|
2674
|
+
dv.setUint32(28, addrV, true);
|
|
2675
|
+
dv.setUint32(32, WGPUAddressMode_ClampToEdge, true);
|
|
2676
|
+
dv.setUint32(36, mag, true);
|
|
2677
|
+
dv.setUint32(40, min, true);
|
|
2678
|
+
dv.setUint32(44, mip, true);
|
|
2679
|
+
dv.setFloat32(48, 0, true);
|
|
2680
|
+
dv.setFloat32(52, 32, true);
|
|
2681
|
+
dv.setUint16(60, 1, true);
|
|
2682
|
+
const handle = drv.deviceCreateSampler(devPtr, drv.addrOf(descBuf));
|
|
2683
|
+
if (!handle) throw new Error("wgpu-native: failed to create GPUSampler");
|
|
2684
|
+
let released = false;
|
|
2685
|
+
const release = () => {
|
|
2686
|
+
if (!released) {
|
|
2687
|
+
released = true;
|
|
2688
|
+
drv.samplerRelease(handle);
|
|
2689
|
+
}
|
|
2690
|
+
};
|
|
2691
|
+
trackedResources.push(release);
|
|
2692
|
+
return {
|
|
2693
|
+
_kind: "sampler",
|
|
2694
|
+
_ptr: handle,
|
|
2695
|
+
destroy: release
|
|
2696
|
+
};
|
|
2697
|
+
},
|
|
2698
|
+
createBindGroupLayout(descriptor) {
|
|
2699
|
+
const entries = descriptor.entries ?? [];
|
|
2700
|
+
const entriesBuf = new Uint8Array(Math.max(1, entries.length) * 120);
|
|
2701
|
+
const edv = new DataView(entriesBuf.buffer);
|
|
2702
|
+
for (let i = 0; i < entries.length; i++) {
|
|
2703
|
+
const e = entries[i];
|
|
2704
|
+
const base = i * 120;
|
|
2705
|
+
edv.setUint32(base + 8, e.binding, true);
|
|
2706
|
+
edv.setBigUint64(base + 16, BigInt(e.visibility), true);
|
|
2707
|
+
if (e.buffer) {
|
|
2708
|
+
const bType = e.buffer.type === "storage" ? WGPUBufferBindingType_Storage : e.buffer.type === "read-only-storage" ? WGPUBufferBindingType_ReadOnlyStorage : WGPUBufferBindingType_Uniform;
|
|
2709
|
+
edv.setUint32(base + 40, bType, true);
|
|
2710
|
+
}
|
|
2711
|
+
if (e.sampler) {
|
|
2712
|
+
const sType = e.sampler.type === "non-filtering" ? WGPUSamplerBindingType_NonFiltering : e.sampler.type === "comparison" ? WGPUSamplerBindingType_Comparison : WGPUSamplerBindingType_Filtering;
|
|
2713
|
+
edv.setUint32(base + 64, sType, true);
|
|
2714
|
+
}
|
|
2715
|
+
if (e.texture) {
|
|
2716
|
+
edv.setUint32(base + 80, WGPUTextureSampleType_Float, true);
|
|
2717
|
+
edv.setUint32(base + 84, WGPUTextureViewDimension_2D, true);
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
const bglBuf = /* @__PURE__ */ new Uint8Array(40);
|
|
2721
|
+
const bdv = new DataView(bglBuf.buffer);
|
|
2722
|
+
bdv.setBigUint64(24, BigInt(entries.length), true);
|
|
2723
|
+
bdv.setBigUint64(32, entries.length > 0 ? drv.addrOf(entriesBuf) : 0n, true);
|
|
2724
|
+
const handle = drv.deviceCreateBindGroupLayout(devPtr, drv.addrOf(bglBuf));
|
|
2725
|
+
if (!handle) throw new Error("wgpu-native: failed to create GPUBindGroupLayout");
|
|
2726
|
+
let released = false;
|
|
2727
|
+
const release = () => {
|
|
2728
|
+
if (!released) {
|
|
2729
|
+
released = true;
|
|
2730
|
+
drv.bindGroupLayoutRelease(handle);
|
|
2731
|
+
}
|
|
2732
|
+
};
|
|
2733
|
+
trackedResources.push(release);
|
|
2734
|
+
return {
|
|
2735
|
+
_kind: "bgl",
|
|
2736
|
+
_ptr: handle,
|
|
2737
|
+
destroy: release
|
|
2738
|
+
};
|
|
2739
|
+
},
|
|
2740
|
+
createPipelineLayout(descriptor) {
|
|
2741
|
+
const bgls = descriptor.bindGroupLayouts ?? [];
|
|
2742
|
+
const arr = new BigUint64Array(Math.max(1, bgls.length));
|
|
2743
|
+
for (let i = 0; i < bgls.length; i++) arr[i] = bgls[i]._ptr;
|
|
2744
|
+
const arrU8 = new Uint8Array(arr.buffer);
|
|
2745
|
+
const plBuf = /* @__PURE__ */ new Uint8Array(48);
|
|
2746
|
+
const dv = new DataView(plBuf.buffer);
|
|
2747
|
+
dv.setBigUint64(24, BigInt(bgls.length), true);
|
|
2748
|
+
dv.setBigUint64(32, bgls.length > 0 ? drv.addrOf(arrU8) : 0n, true);
|
|
2749
|
+
const handle = drv.deviceCreatePipelineLayout(devPtr, drv.addrOf(plBuf));
|
|
2750
|
+
if (!handle) throw new Error("wgpu-native: failed to create GPUPipelineLayout");
|
|
2751
|
+
let released = false;
|
|
2752
|
+
const release = () => {
|
|
2753
|
+
if (!released) {
|
|
2754
|
+
released = true;
|
|
2755
|
+
drv.pipelineLayoutRelease(handle);
|
|
2756
|
+
}
|
|
2757
|
+
};
|
|
2758
|
+
trackedResources.push(release);
|
|
2759
|
+
return {
|
|
2760
|
+
_kind: "pl",
|
|
2761
|
+
_ptr: handle,
|
|
2762
|
+
destroy: release
|
|
2763
|
+
};
|
|
2764
|
+
},
|
|
2765
|
+
createRenderPipeline(descriptor) {
|
|
2766
|
+
const keepAlive = [];
|
|
2767
|
+
const vsEntry = utf8.encode(descriptor.vertex.entryPoint ?? "main");
|
|
2768
|
+
const fsEntry = utf8.encode(descriptor.fragment.entryPoint ?? "main");
|
|
2769
|
+
keepAlive.push(vsEntry, fsEntry);
|
|
2770
|
+
const vboSpecs = descriptor.vertex.buffers ?? [];
|
|
2771
|
+
const vboLayoutsBuf = new Uint8Array(Math.max(1, vboSpecs.length) * 40);
|
|
2772
|
+
const vboDv = new DataView(vboLayoutsBuf.buffer);
|
|
2773
|
+
keepAlive.push(vboLayoutsBuf);
|
|
2774
|
+
for (let i = 0; i < vboSpecs.length; i++) {
|
|
2775
|
+
const vb = vboSpecs[i];
|
|
2776
|
+
const attrs = vb.attributes ?? [];
|
|
2777
|
+
const attrBuf = new Uint8Array(Math.max(1, attrs.length) * 32);
|
|
2778
|
+
const attrDv = new DataView(attrBuf.buffer);
|
|
2779
|
+
keepAlive.push(attrBuf);
|
|
2780
|
+
for (let a = 0; a < attrs.length; a++) {
|
|
2781
|
+
const at = attrs[a];
|
|
2782
|
+
const ab = a * 32;
|
|
2783
|
+
attrDv.setUint32(ab + 8, mapVertexFormat(at.format), true);
|
|
2784
|
+
attrDv.setBigUint64(ab + 16, BigInt(at.offset), true);
|
|
2785
|
+
attrDv.setUint32(ab + 24, at.shaderLocation, true);
|
|
2786
|
+
}
|
|
2787
|
+
const lb = i * 40;
|
|
2788
|
+
vboDv.setUint32(lb + 8, vb.stepMode === "instance" ? WGPUVertexStepMode_Instance : WGPUVertexStepMode_Vertex, true);
|
|
2789
|
+
vboDv.setBigUint64(lb + 16, BigInt(vb.arrayStride), true);
|
|
2790
|
+
vboDv.setBigUint64(lb + 24, BigInt(attrs.length), true);
|
|
2791
|
+
vboDv.setBigUint64(lb + 32, attrs.length > 0 ? drv.addrOf(attrBuf) : 0n, true);
|
|
2792
|
+
}
|
|
2793
|
+
const targets = descriptor.fragment.targets ?? [];
|
|
2794
|
+
const targetsBuf = new Uint8Array(Math.max(1, targets.length) * 32);
|
|
2795
|
+
const tDv = new DataView(targetsBuf.buffer);
|
|
2796
|
+
keepAlive.push(targetsBuf);
|
|
2797
|
+
for (let i = 0; i < targets.length; i++) {
|
|
2798
|
+
const t = targets[i];
|
|
2799
|
+
const tb = i * 32;
|
|
2800
|
+
let blendPtr = 0n;
|
|
2801
|
+
if (t.blend) {
|
|
2802
|
+
const blendBuf = /* @__PURE__ */ new Uint8Array(24);
|
|
2803
|
+
const bDv = new DataView(blendBuf.buffer);
|
|
2804
|
+
keepAlive.push(blendBuf);
|
|
2805
|
+
bDv.setUint32(0, mapBlendOp(t.blend.color?.operation), true);
|
|
2806
|
+
bDv.setUint32(4, mapBlendFactor(t.blend.color?.srcFactor), true);
|
|
2807
|
+
bDv.setUint32(8, mapBlendFactor(t.blend.color?.dstFactor), true);
|
|
2808
|
+
bDv.setUint32(12, mapBlendOp(t.blend.alpha?.operation), true);
|
|
2809
|
+
bDv.setUint32(16, mapBlendFactor(t.blend.alpha?.srcFactor), true);
|
|
2810
|
+
bDv.setUint32(20, mapBlendFactor(t.blend.alpha?.dstFactor), true);
|
|
2811
|
+
blendPtr = drv.addrOf(blendBuf);
|
|
2812
|
+
}
|
|
2813
|
+
tDv.setUint32(tb + 8, mapTextureFormat(t.format), true);
|
|
2814
|
+
tDv.setBigUint64(tb + 16, blendPtr, true);
|
|
2815
|
+
tDv.setBigUint64(tb + 24, WGPUColorWriteMask_All, true);
|
|
2816
|
+
}
|
|
2817
|
+
const fragBuf = /* @__PURE__ */ new Uint8Array(64);
|
|
2818
|
+
const fDv = new DataView(fragBuf.buffer);
|
|
2819
|
+
keepAlive.push(fragBuf);
|
|
2820
|
+
fDv.setBigUint64(8, descriptor.fragment.module._ptr, true);
|
|
2821
|
+
fDv.setBigUint64(16, drv.addrOf(fsEntry), true);
|
|
2822
|
+
fDv.setBigUint64(24, BigInt(fsEntry.byteLength), true);
|
|
2823
|
+
fDv.setBigUint64(48, BigInt(targets.length), true);
|
|
2824
|
+
fDv.setBigUint64(56, targets.length > 0 ? drv.addrOf(targetsBuf) : 0n, true);
|
|
2825
|
+
const rpBuf = /* @__PURE__ */ new Uint8Array(168);
|
|
2826
|
+
const rDv = new DataView(rpBuf.buffer);
|
|
2827
|
+
rDv.setBigUint64(24, descriptor.layout?._ptr ?? 0n, true);
|
|
2828
|
+
rDv.setBigUint64(40, descriptor.vertex.module._ptr, true);
|
|
2829
|
+
rDv.setBigUint64(48, drv.addrOf(vsEntry), true);
|
|
2830
|
+
rDv.setBigUint64(56, BigInt(vsEntry.byteLength), true);
|
|
2831
|
+
rDv.setBigUint64(80, BigInt(vboSpecs.length), true);
|
|
2832
|
+
rDv.setBigUint64(88, vboSpecs.length > 0 ? drv.addrOf(vboLayoutsBuf) : 0n, true);
|
|
2833
|
+
rDv.setUint32(104, WGPUPrimitiveTopology_TriangleList, true);
|
|
2834
|
+
rDv.setUint32(112, WGPUFrontFace_CCW, true);
|
|
2835
|
+
rDv.setUint32(116, WGPUCullMode_None, true);
|
|
2836
|
+
rDv.setUint32(144, 1, true);
|
|
2837
|
+
rDv.setUint32(148, 0xffffffff, true);
|
|
2838
|
+
rDv.setBigUint64(160, drv.addrOf(fragBuf), true);
|
|
2839
|
+
const handle = drv.deviceCreateRenderPipeline(devPtr, drv.addrOf(rpBuf));
|
|
2840
|
+
keepAlive.length;
|
|
2841
|
+
if (!handle) throw new Error("wgpu-native: failed to create GPURenderPipeline");
|
|
2842
|
+
let released = false;
|
|
2843
|
+
const release = () => {
|
|
2844
|
+
if (!released) {
|
|
2845
|
+
released = true;
|
|
2846
|
+
drv.renderPipelineRelease(handle);
|
|
2847
|
+
}
|
|
2848
|
+
};
|
|
2849
|
+
trackedResources.push(release);
|
|
2850
|
+
return {
|
|
2851
|
+
_kind: "pipeline",
|
|
2852
|
+
_ptr: handle,
|
|
2853
|
+
destroy: release
|
|
2854
|
+
};
|
|
2855
|
+
},
|
|
2856
|
+
createBindGroup(descriptor) {
|
|
2857
|
+
const entries = descriptor.entries ?? [];
|
|
2858
|
+
const entriesBuf = new Uint8Array(Math.max(1, entries.length) * 56);
|
|
2859
|
+
const edv = new DataView(entriesBuf.buffer);
|
|
2860
|
+
for (let i = 0; i < entries.length; i++) {
|
|
2861
|
+
const e = entries[i];
|
|
2862
|
+
const base = i * 56;
|
|
2863
|
+
edv.setUint32(base + 8, e.binding, true);
|
|
2864
|
+
const res = e.resource;
|
|
2865
|
+
if (res?.buffer) {
|
|
2866
|
+
edv.setBigUint64(base + 16, res.buffer._ptr, true);
|
|
2867
|
+
edv.setBigUint64(base + 24, BigInt(res.offset ?? 0), true);
|
|
2868
|
+
edv.setBigUint64(base + 32, BigInt(res.size ?? res.buffer._byteSize), true);
|
|
2869
|
+
} else if (res?._kind === "sampler") edv.setBigUint64(base + 40, res._ptr, true);
|
|
2870
|
+
else if (res?._kind === "view") edv.setBigUint64(base + 48, res._ptr, true);
|
|
2871
|
+
}
|
|
2872
|
+
const bgBuf = /* @__PURE__ */ new Uint8Array(48);
|
|
2873
|
+
const dv = new DataView(bgBuf.buffer);
|
|
2874
|
+
dv.setBigUint64(24, descriptor.layout._ptr, true);
|
|
2875
|
+
dv.setBigUint64(32, BigInt(entries.length), true);
|
|
2876
|
+
dv.setBigUint64(40, entries.length > 0 ? drv.addrOf(entriesBuf) : 0n, true);
|
|
2877
|
+
const handle = drv.deviceCreateBindGroup(devPtr, drv.addrOf(bgBuf));
|
|
2878
|
+
if (!handle) throw new Error("wgpu-native: failed to create GPUBindGroup");
|
|
2879
|
+
let released = false;
|
|
2880
|
+
const release = () => {
|
|
2881
|
+
if (!released) {
|
|
2882
|
+
released = true;
|
|
2883
|
+
drv.bindGroupRelease(handle);
|
|
2884
|
+
}
|
|
2885
|
+
};
|
|
2886
|
+
trackedResources.push(release);
|
|
2887
|
+
return {
|
|
2888
|
+
_kind: "bindgroup",
|
|
2889
|
+
_ptr: handle,
|
|
2890
|
+
destroy: release
|
|
2891
|
+
};
|
|
2892
|
+
},
|
|
2893
|
+
createCommandEncoder() {
|
|
2894
|
+
let encPtr = drv.deviceCreateCommandEncoder(devPtr, 0n);
|
|
2895
|
+
if (!encPtr) throw new Error("wgpu-native: failed to create GPUCommandEncoder");
|
|
2896
|
+
return {
|
|
2897
|
+
beginRenderPass(desc) {
|
|
2898
|
+
const ca = desc.colorAttachments[0];
|
|
2899
|
+
const attBuf = /* @__PURE__ */ new Uint8Array(72);
|
|
2900
|
+
const adv = new DataView(attBuf.buffer);
|
|
2901
|
+
adv.setBigUint64(8, ca.view._ptr, true);
|
|
2902
|
+
adv.setUint32(16, WGPU_DEPTH_SLICE_UNDEFINED, true);
|
|
2903
|
+
adv.setUint32(32, ca.loadOp === "load" ? WGPULoadOp_Load : WGPULoadOp_Clear, true);
|
|
2904
|
+
adv.setUint32(36, WGPUStoreOp_Store, true);
|
|
2905
|
+
adv.setFloat64(40, ca.clearValue?.r ?? 0, true);
|
|
2906
|
+
adv.setFloat64(48, ca.clearValue?.g ?? 0, true);
|
|
2907
|
+
adv.setFloat64(56, ca.clearValue?.b ?? 0, true);
|
|
2908
|
+
adv.setFloat64(64, ca.clearValue?.a ?? 1, true);
|
|
2909
|
+
const rpDescBuf = /* @__PURE__ */ new Uint8Array(64);
|
|
2910
|
+
const rdv = new DataView(rpDescBuf.buffer);
|
|
2911
|
+
rdv.setBigUint64(24, 1n, true);
|
|
2912
|
+
rdv.setBigUint64(32, drv.addrOf(attBuf), true);
|
|
2913
|
+
let passPtr = drv.commandEncoderBeginRenderPass(encPtr, drv.addrOf(rpDescBuf));
|
|
2914
|
+
if (!passPtr) throw new Error("wgpu-native: failed to begin render pass");
|
|
2915
|
+
return {
|
|
2916
|
+
setPipeline(pipeline) {
|
|
2917
|
+
drv.renderPassSetPipeline(passPtr, pipeline._ptr);
|
|
2918
|
+
},
|
|
2919
|
+
setBindGroup(index, bg) {
|
|
2920
|
+
drv.renderPassSetBindGroup(passPtr, index, bg._ptr);
|
|
2921
|
+
},
|
|
2922
|
+
setVertexBuffer(slot, buf, offset = 0, size) {
|
|
2923
|
+
drv.renderPassSetVertexBuffer(passPtr, slot, buf._ptr, BigInt(offset), BigInt(size ?? buf._byteSize - offset));
|
|
2924
|
+
},
|
|
2925
|
+
setIndexBuffer(buf, format, offset = 0, size) {
|
|
2926
|
+
drv.renderPassSetIndexBuffer(passPtr, buf._ptr, format === "uint32" ? WGPUIndexFormat_Uint32 : WGPUIndexFormat_Uint16, BigInt(offset), BigInt(size ?? buf._byteSize - offset));
|
|
2927
|
+
},
|
|
2928
|
+
setScissorRect(x, y, w, h) {
|
|
2929
|
+
drv.renderPassSetScissorRect(passPtr, x >>> 0, y >>> 0, w >>> 0, h >>> 0);
|
|
2930
|
+
},
|
|
2931
|
+
drawIndexed(indexCount, instanceCount = 1, firstIndex = 0, baseVertex = 0, firstInstance = 0) {
|
|
2932
|
+
drv.renderPassDrawIndexed(passPtr, indexCount, instanceCount, firstIndex, baseVertex, firstInstance);
|
|
2933
|
+
},
|
|
2934
|
+
end() {
|
|
2935
|
+
if (!passPtr) return;
|
|
2936
|
+
drv.renderPassEnd(passPtr);
|
|
2937
|
+
drv.renderPassRelease(passPtr);
|
|
2938
|
+
passPtr = 0n;
|
|
2939
|
+
}
|
|
2940
|
+
};
|
|
2941
|
+
},
|
|
2942
|
+
copyTextureToBuffer(src, dst, copySize) {
|
|
2943
|
+
const w = Array.isArray(copySize) ? copySize[0] ?? 1 : copySize.width ?? 1;
|
|
2944
|
+
const h = Array.isArray(copySize) ? copySize[1] ?? 1 : copySize.height ?? 1;
|
|
2945
|
+
const d = Array.isArray(copySize) ? copySize[2] ?? 1 : copySize.depthOrArrayLayers ?? 1;
|
|
2946
|
+
const srcBuf = /* @__PURE__ */ new Uint8Array(32);
|
|
2947
|
+
const sdv = new DataView(srcBuf.buffer);
|
|
2948
|
+
sdv.setBigUint64(0, src.texture._ptr, true);
|
|
2949
|
+
sdv.setUint32(8, src.mipLevel ?? 0, true);
|
|
2950
|
+
sdv.setUint32(12, src.origin?.[0] ?? 0, true);
|
|
2951
|
+
sdv.setUint32(16, src.origin?.[1] ?? 0, true);
|
|
2952
|
+
sdv.setUint32(20, src.origin?.[2] ?? 0, true);
|
|
2953
|
+
sdv.setUint32(24, WGPUTextureAspect_All, true);
|
|
2954
|
+
const dstBuf = /* @__PURE__ */ new Uint8Array(24);
|
|
2955
|
+
const ddv = new DataView(dstBuf.buffer);
|
|
2956
|
+
ddv.setBigUint64(0, BigInt(dst.offset ?? 0), true);
|
|
2957
|
+
ddv.setUint32(8, dst.bytesPerRow, true);
|
|
2958
|
+
ddv.setUint32(12, dst.rowsPerImage ?? h, true);
|
|
2959
|
+
ddv.setBigUint64(16, dst.buffer._ptr, true);
|
|
2960
|
+
const extBuf = /* @__PURE__ */ new Uint8Array(12);
|
|
2961
|
+
const edv = new DataView(extBuf.buffer);
|
|
2962
|
+
edv.setUint32(0, w, true);
|
|
2963
|
+
edv.setUint32(4, h, true);
|
|
2964
|
+
edv.setUint32(8, d, true);
|
|
2965
|
+
drv.commandEncoderCopyTextureToBuffer(encPtr, drv.addrOf(srcBuf), drv.addrOf(dstBuf), drv.addrOf(extBuf));
|
|
2966
|
+
},
|
|
2967
|
+
finish() {
|
|
2968
|
+
if (!encPtr) throw new Error("wgpu-native: command encoder already finished");
|
|
2969
|
+
const cmdBuf = drv.commandEncoderFinish(encPtr, 0n);
|
|
2970
|
+
drv.commandEncoderRelease(encPtr);
|
|
2971
|
+
encPtr = 0n;
|
|
2972
|
+
return {
|
|
2973
|
+
_kind: "cmdbuf",
|
|
2974
|
+
_ptr: cmdBuf
|
|
2975
|
+
};
|
|
2976
|
+
}
|
|
2977
|
+
};
|
|
2978
|
+
},
|
|
2979
|
+
_readBufferSync(buffer, dst, byteLength) {
|
|
2980
|
+
return drv.readBufferSync(devPtr, buffer._ptr, dst, byteLength);
|
|
2981
|
+
},
|
|
2982
|
+
destroy: destroyAll
|
|
2983
|
+
},
|
|
2984
|
+
destroy: destroyAll
|
|
2985
|
+
};
|
|
2986
|
+
}
|
|
2987
|
+
/**
|
|
2988
|
+
* Open a synchronous native Headless WebGPU `GPUDevice` via `wgpu-native` (`libwgpu_native.so`/`.dylib`/`.dll`)
|
|
2989
|
+
* using Bun's built-in `bun:ffi` or Node.js's prebuilt `koffi` C FFI.
|
|
2990
|
+
* Returns `null` cleanly if `libwgpu_native` or the FFI driver is unavailable.
|
|
2991
|
+
*/
|
|
2992
|
+
async function openWgpuNativeFfi(options = {}) {
|
|
2993
|
+
const libPath = await resolveWgpuLibPath(options.libPath);
|
|
2994
|
+
if (!libPath) return null;
|
|
2995
|
+
const wantRuntime = options.runtime ?? "auto";
|
|
2996
|
+
const useBun = wantRuntime === "bun" || wantRuntime === "auto" && isBunRuntime();
|
|
2997
|
+
let drv = null;
|
|
2998
|
+
try {
|
|
2999
|
+
drv = useBun ? await openBunFfiDriver(libPath) : await openKoffiDriver(libPath, options.koffiModule);
|
|
3000
|
+
} catch {
|
|
3001
|
+
return null;
|
|
3002
|
+
}
|
|
3003
|
+
if (!drv) return null;
|
|
3004
|
+
const instance = drv.createInstance();
|
|
3005
|
+
if (!instance) return null;
|
|
3006
|
+
const powerPref = options.powerPreference === "low-power" ? WGPUPowerPreference_LowPower : WGPUPowerPreference_HighPerformance;
|
|
3007
|
+
const adapter = drv.requestAdapter(instance, powerPref);
|
|
3008
|
+
if (!adapter) {
|
|
3009
|
+
drv.instanceRelease(instance);
|
|
3010
|
+
return null;
|
|
3011
|
+
}
|
|
3012
|
+
const devPtr = drv.requestDevice(adapter);
|
|
3013
|
+
if (!devPtr) {
|
|
3014
|
+
drv.adapterRelease(adapter);
|
|
3015
|
+
drv.instanceRelease(instance);
|
|
3016
|
+
return null;
|
|
3017
|
+
}
|
|
3018
|
+
const queuePtr = drv.deviceGetQueue(devPtr);
|
|
3019
|
+
const { device, destroy } = createWgpuDeviceWrapper(drv, instance, adapter, devPtr, queuePtr);
|
|
3020
|
+
return {
|
|
3021
|
+
backend: drv.backend,
|
|
3022
|
+
device,
|
|
3023
|
+
format: "rgba8unorm",
|
|
3024
|
+
supportsSyncReadback: true,
|
|
3025
|
+
destroy
|
|
3026
|
+
};
|
|
3027
|
+
}
|
|
3028
|
+
/**
|
|
3029
|
+
* Open a native Headless WebGPU `GPUDevice` via Google Dawn's prebuilt N-API `'webgpu'` package
|
|
3030
|
+
* (works identically in both Node.js and Bun!).
|
|
3031
|
+
* Returns `null` cleanly if `'webgpu'` is not installed or no Vulkan/Metal/D3D12 adapter is available.
|
|
3032
|
+
*/
|
|
3033
|
+
async function openDawnWebGpu(options = {}) {
|
|
3034
|
+
const dawn = await tryLoadExternalModule("webgpu", options.dawnModule);
|
|
3035
|
+
if (!dawn || typeof dawn.create !== "function") return null;
|
|
3036
|
+
try {
|
|
3037
|
+
const gpu = dawn.create([...options.dawnFlags ?? []]);
|
|
3038
|
+
const adapter = await gpu.requestAdapter({ powerPreference: options.powerPreference ?? "high-performance" });
|
|
3039
|
+
if (!adapter) return null;
|
|
3040
|
+
const device = await adapter.requestDevice();
|
|
3041
|
+
device.__holtDawnOwner = [gpu, adapter];
|
|
3042
|
+
return {
|
|
3043
|
+
backend: "dawn",
|
|
3044
|
+
device,
|
|
3045
|
+
format: "rgba8unorm",
|
|
3046
|
+
supportsSyncReadback: false,
|
|
3047
|
+
destroy: () => {
|
|
3048
|
+
try {
|
|
3049
|
+
device.destroy?.();
|
|
3050
|
+
} catch {}
|
|
3051
|
+
device.__holtDawnOwner = null;
|
|
3052
|
+
}
|
|
3053
|
+
};
|
|
3054
|
+
} catch {
|
|
3055
|
+
return null;
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
/**
|
|
3059
|
+
* Acquire an isomorphic Headless WebGPU device across Bun (`bun:ffi` + `wgpu-native` or Dawn `webgpu`),
|
|
3060
|
+
* Node.js (`koffi` + `wgpu-native` or Dawn `webgpu`), and Deno / Browser (`navigator.gpu`).
|
|
3061
|
+
* Never throws when native drivers are absent — returns `null` so callers can cleanly fall back to Canvas2D.
|
|
3062
|
+
*/
|
|
3063
|
+
async function requestHeadlessWebGpu(options = {}) {
|
|
3064
|
+
const backend = options.backend ?? "auto";
|
|
3065
|
+
if (backend === "ffi" || backend === "auto") {
|
|
3066
|
+
const ffiHandle = await openWgpuNativeFfi(options);
|
|
3067
|
+
if (ffiHandle) return ffiHandle;
|
|
3068
|
+
if (backend === "ffi") return null;
|
|
3069
|
+
}
|
|
3070
|
+
if (backend === "dawn" || backend === "auto") {
|
|
3071
|
+
const dawnHandle = await openDawnWebGpu(options);
|
|
3072
|
+
if (dawnHandle) return dawnHandle;
|
|
3073
|
+
if (backend === "dawn") return null;
|
|
3074
|
+
}
|
|
3075
|
+
if (backend === "navigator" || backend === "auto") {
|
|
3076
|
+
const nav = typeof navigator !== "undefined" ? navigator : null;
|
|
3077
|
+
if (nav?.gpu && typeof nav.gpu.requestAdapter === "function") try {
|
|
3078
|
+
const adapter = await nav.gpu.requestAdapter({ powerPreference: options.powerPreference ?? "high-performance" });
|
|
3079
|
+
if (!adapter) return null;
|
|
3080
|
+
const device = await adapter.requestDevice();
|
|
3081
|
+
return {
|
|
3082
|
+
backend: "navigator",
|
|
3083
|
+
device,
|
|
3084
|
+
format: "rgba8unorm",
|
|
3085
|
+
supportsSyncReadback: false,
|
|
3086
|
+
destroy: () => {
|
|
3087
|
+
try {
|
|
3088
|
+
device.destroy?.();
|
|
3089
|
+
} catch {}
|
|
3090
|
+
}
|
|
3091
|
+
};
|
|
3092
|
+
} catch {
|
|
3093
|
+
return null;
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
return null;
|
|
3097
|
+
}
|
|
1628
3098
|
// src/renderer-webgpu.ts
|
|
1629
3099
|
const GPUShaderStage_VERTEX = 1;
|
|
1630
3100
|
const GPUShaderStage_FRAGMENT = 2;
|
|
3101
|
+
const GPUMapMode_READ = 1;
|
|
1631
3102
|
/**
|
|
1632
3103
|
* Asynchronously acquire a browser `GPUDevice` + configured `GPUCanvasContext` ready to pass directly into
|
|
1633
3104
|
* `new Game({ type: WEBGPU, gpuDevice: gpu.device, gpuContext: gpu.context, gpuFormat: gpu.format })`.
|
|
@@ -1657,8 +3128,8 @@ async function requestWebGpuDevice(canvas, options = {}) {
|
|
|
1657
3128
|
return null;
|
|
1658
3129
|
}
|
|
1659
3130
|
}
|
|
1660
|
-
const MAX_VERTS =
|
|
1661
|
-
const MAX_INDICES =
|
|
3131
|
+
const MAX_VERTS = 65536;
|
|
3132
|
+
const MAX_INDICES = 98304;
|
|
1662
3133
|
const VERT_FLOATS = 6;
|
|
1663
3134
|
const VERT_STRIDE = 24;
|
|
1664
3135
|
const MAX_SDF_PANELS = 16384;
|
|
@@ -1709,10 +3180,10 @@ fn vsMain(in: VsIn) -> VsOut {
|
|
|
1709
3180
|
|
|
1710
3181
|
@fragment
|
|
1711
3182
|
fn fsMain(in: VsOut) -> @location(0) vec4<f32> {
|
|
3183
|
+
let tex = textureSample(uTex, uSampler, in.uv);
|
|
1712
3184
|
if (in.mode > 1.5) {
|
|
1713
3185
|
return vec4<f32>(in.color.rgb * in.color.a, in.color.a);
|
|
1714
3186
|
}
|
|
1715
|
-
let tex = textureSample(uTex, uSampler, in.uv);
|
|
1716
3187
|
if (in.mode > 0.5) {
|
|
1717
3188
|
let a = tex.a * in.color.a;
|
|
1718
3189
|
return vec4<f32>(in.color.rgb * a, a);
|
|
@@ -1916,6 +3387,16 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
1916
3387
|
format;
|
|
1917
3388
|
width;
|
|
1918
3389
|
height;
|
|
3390
|
+
offscreenTex = null;
|
|
3391
|
+
offscreenView = null;
|
|
3392
|
+
offscreenW = 0;
|
|
3393
|
+
offscreenH = 0;
|
|
3394
|
+
readbackBuf = null;
|
|
3395
|
+
readbackCap = 0;
|
|
3396
|
+
readbackBytesPerRow = 0;
|
|
3397
|
+
uploadStaging = /* @__PURE__ */ new Uint8Array(0);
|
|
3398
|
+
uploadScratchCanvas = null;
|
|
3399
|
+
uploadScratchCtx = null;
|
|
1919
3400
|
globalsBuf;
|
|
1920
3401
|
vbo;
|
|
1921
3402
|
ibo;
|
|
@@ -1971,12 +3452,11 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
1971
3452
|
this.width = Math.max(1, Math.round(width));
|
|
1972
3453
|
this.height = Math.max(1, Math.round(height));
|
|
1973
3454
|
this.device = opts.device;
|
|
1974
|
-
const
|
|
3455
|
+
const ctx = opts.context !== void 0 ? opts.context : canvas?.getContext?.("webgpu") ?? null;
|
|
3456
|
+
const format = opts.format ?? (ctx ? "bgra8unorm" : "rgba8unorm");
|
|
1975
3457
|
this.format = format;
|
|
1976
|
-
const ctx = opts.context ?? canvas.getContext("webgpu");
|
|
1977
|
-
if (!ctx) throw new Error("Stage: WebGPU canvas context unavailable");
|
|
1978
3458
|
this.gpuContext = ctx;
|
|
1979
|
-
ctx.configure({
|
|
3459
|
+
if (ctx) ctx.configure({
|
|
1980
3460
|
device: this.device,
|
|
1981
3461
|
format,
|
|
1982
3462
|
alphaMode: opts.transparent ? "premultiplied" : "opaque"
|
|
@@ -2169,6 +3649,20 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2169
3649
|
format: "rgba8unorm",
|
|
2170
3650
|
usage: 6
|
|
2171
3651
|
});
|
|
3652
|
+
if (typeof dev.queue.writeTexture === "function") dev.queue.writeTexture({ texture: dummyTex }, new Uint8Array([
|
|
3653
|
+
255,
|
|
3654
|
+
255,
|
|
3655
|
+
255,
|
|
3656
|
+
255
|
|
3657
|
+
]), {
|
|
3658
|
+
offset: 0,
|
|
3659
|
+
bytesPerRow: 4,
|
|
3660
|
+
rowsPerImage: 1
|
|
3661
|
+
}, [
|
|
3662
|
+
1,
|
|
3663
|
+
1,
|
|
3664
|
+
1
|
|
3665
|
+
]);
|
|
2172
3666
|
const dummyView = dummyTex.createView();
|
|
2173
3667
|
this.dummyEntry = {
|
|
2174
3668
|
tex: dummyTex,
|
|
@@ -2206,11 +3700,113 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2206
3700
|
const h = Math.max(1, Math.round(height));
|
|
2207
3701
|
this.width = w;
|
|
2208
3702
|
this.height = h;
|
|
2209
|
-
this.canvas
|
|
2210
|
-
|
|
3703
|
+
if (this.canvas) {
|
|
3704
|
+
this.canvas.width = w;
|
|
3705
|
+
this.canvas.height = h;
|
|
3706
|
+
}
|
|
2211
3707
|
}
|
|
2212
|
-
|
|
2213
|
-
if (
|
|
3708
|
+
ensureOffscreenTarget() {
|
|
3709
|
+
if (this.offscreenTex && this.offscreenW === this.width && this.offscreenH === this.height) return this.offscreenView;
|
|
3710
|
+
try {
|
|
3711
|
+
this.offscreenTex?.destroy?.();
|
|
3712
|
+
} catch {}
|
|
3713
|
+
this.offscreenW = this.width;
|
|
3714
|
+
this.offscreenH = this.height;
|
|
3715
|
+
this.offscreenTex = this.device.createTexture({
|
|
3716
|
+
size: [
|
|
3717
|
+
this.width,
|
|
3718
|
+
this.height,
|
|
3719
|
+
1
|
|
3720
|
+
],
|
|
3721
|
+
format: this.format,
|
|
3722
|
+
usage: 21
|
|
3723
|
+
});
|
|
3724
|
+
this.offscreenView = this.offscreenTex.createView();
|
|
3725
|
+
return this.offscreenView;
|
|
3726
|
+
}
|
|
3727
|
+
extractAndUploadTexture(tex, image, w, h, premultiply = true) {
|
|
3728
|
+
if (this.gpuContext !== null && typeof this.device.queue.copyExternalImageToTexture === "function") try {
|
|
3729
|
+
this.device.queue.copyExternalImageToTexture({ source: image }, {
|
|
3730
|
+
texture: tex,
|
|
3731
|
+
premultipliedAlpha: premultiply
|
|
3732
|
+
}, [w, h]);
|
|
3733
|
+
return;
|
|
3734
|
+
} catch {}
|
|
3735
|
+
if (typeof this.device.queue.writeTexture !== "function") {
|
|
3736
|
+
if (typeof this.device.queue.copyExternalImageToTexture === "function") this.device.queue.copyExternalImageToTexture({ source: image }, {
|
|
3737
|
+
texture: tex,
|
|
3738
|
+
premultipliedAlpha: premultiply
|
|
3739
|
+
}, [w, h]);
|
|
3740
|
+
return;
|
|
3741
|
+
}
|
|
3742
|
+
let rawPixels = null;
|
|
3743
|
+
if (image.data && (image.data instanceof Uint8Array || image.data instanceof Uint8ClampedArray)) rawPixels = image.data;
|
|
3744
|
+
else if (typeof image.getContext === "function") {
|
|
3745
|
+
const c2d = image.getContext("2d");
|
|
3746
|
+
if (c2d && typeof c2d.getImageData === "function") rawPixels = c2d.getImageData(0, 0, w, h).data;
|
|
3747
|
+
}
|
|
3748
|
+
if (!rawPixels) {
|
|
3749
|
+
const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : null);
|
|
3750
|
+
if (doc) {
|
|
3751
|
+
if (!this.uploadScratchCanvas) {
|
|
3752
|
+
this.uploadScratchCanvas = doc.createElement("canvas");
|
|
3753
|
+
this.uploadScratchCtx = this.uploadScratchCanvas.getContext("2d");
|
|
3754
|
+
}
|
|
3755
|
+
if (this.uploadScratchCanvas && this.uploadScratchCtx) {
|
|
3756
|
+
if (this.uploadScratchCanvas.width < w || this.uploadScratchCanvas.height < h) {
|
|
3757
|
+
this.uploadScratchCanvas.width = Math.max(this.uploadScratchCanvas.width, w);
|
|
3758
|
+
this.uploadScratchCanvas.height = Math.max(this.uploadScratchCanvas.height, h);
|
|
3759
|
+
}
|
|
3760
|
+
this.uploadScratchCtx.clearRect(0, 0, w, h);
|
|
3761
|
+
this.uploadScratchCtx.drawImage(image, 0, 0, w, h);
|
|
3762
|
+
rawPixels = this.uploadScratchCtx.getImageData(0, 0, w, h).data;
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
if (!rawPixels) return;
|
|
3767
|
+
const rowBytes = w * 4;
|
|
3768
|
+
const alignedRow = rowBytes + 255 & -256;
|
|
3769
|
+
const totalBytes = alignedRow * h;
|
|
3770
|
+
if (this.uploadStaging.byteLength < totalBytes) this.uploadStaging = new Uint8Array(totalBytes);
|
|
3771
|
+
const dst = this.uploadStaging;
|
|
3772
|
+
for (let y = 0; y < h; y++) {
|
|
3773
|
+
const srcRowOff = y * rowBytes;
|
|
3774
|
+
const dstRowOff = y * alignedRow;
|
|
3775
|
+
if (premultiply) for (let x = 0; x < rowBytes; x += 4) {
|
|
3776
|
+
const s = srcRowOff + x;
|
|
3777
|
+
const d = dstRowOff + x;
|
|
3778
|
+
const a = rawPixels[s + 3];
|
|
3779
|
+
if (a === 255) {
|
|
3780
|
+
dst[d] = rawPixels[s];
|
|
3781
|
+
dst[d + 1] = rawPixels[s + 1];
|
|
3782
|
+
dst[d + 2] = rawPixels[s + 2];
|
|
3783
|
+
dst[d + 3] = 255;
|
|
3784
|
+
} else if (a === 0) {
|
|
3785
|
+
dst[d] = 0;
|
|
3786
|
+
dst[d + 1] = 0;
|
|
3787
|
+
dst[d + 2] = 0;
|
|
3788
|
+
dst[d + 3] = 0;
|
|
3789
|
+
} else {
|
|
3790
|
+
dst[d] = (rawPixels[s] * a + 127) / 255 | 0;
|
|
3791
|
+
dst[d + 1] = (rawPixels[s + 1] * a + 127) / 255 | 0;
|
|
3792
|
+
dst[d + 2] = (rawPixels[s + 2] * a + 127) / 255 | 0;
|
|
3793
|
+
dst[d + 3] = a;
|
|
3794
|
+
}
|
|
3795
|
+
}
|
|
3796
|
+
else dst.set(rawPixels.subarray(srcRowOff, srcRowOff + rowBytes), dstRowOff);
|
|
3797
|
+
}
|
|
3798
|
+
this.device.queue.writeTexture({ texture: tex }, dst.subarray(0, totalBytes), {
|
|
3799
|
+
offset: 0,
|
|
3800
|
+
bytesPerRow: alignedRow,
|
|
3801
|
+
rowsPerImage: h
|
|
3802
|
+
}, [
|
|
3803
|
+
w,
|
|
3804
|
+
h,
|
|
3805
|
+
1
|
|
3806
|
+
]);
|
|
3807
|
+
}
|
|
3808
|
+
ensureGpuTexture(keyObj, image, w, h, nearest, dirty, premultiply = true, isRenderTarget = false) {
|
|
3809
|
+
if (w <= 0 || h <= 0 || !image && !isRenderTarget) return null;
|
|
2214
3810
|
let entry = this.texCache.get(keyObj);
|
|
2215
3811
|
if (!entry || entry.width !== w || entry.height !== h) {
|
|
2216
3812
|
try {
|
|
@@ -2223,7 +3819,7 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2223
3819
|
1
|
|
2224
3820
|
],
|
|
2225
3821
|
format: "rgba8unorm",
|
|
2226
|
-
usage:
|
|
3822
|
+
usage: 23
|
|
2227
3823
|
});
|
|
2228
3824
|
const view = tex.createView();
|
|
2229
3825
|
entry = {
|
|
@@ -2235,16 +3831,13 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2235
3831
|
bindGroupNearest: this.makeBatchBindGroup(view, this.samplerNearest)
|
|
2236
3832
|
};
|
|
2237
3833
|
this.texCache.set(keyObj, entry);
|
|
2238
|
-
dirty =
|
|
3834
|
+
dirty = !isRenderTarget;
|
|
2239
3835
|
}
|
|
2240
|
-
if (dirty && entry) this.
|
|
2241
|
-
texture: entry.tex,
|
|
2242
|
-
premultipliedAlpha: true
|
|
2243
|
-
}, [w, h]);
|
|
3836
|
+
if (dirty && entry && !isRenderTarget) this.extractAndUploadTexture(entry.tex, image, w, h, premultiply);
|
|
2244
3837
|
return nearest ? entry.bindGroupNearest : entry.bindGroupLinear;
|
|
2245
3838
|
}
|
|
2246
3839
|
ensureSourceBindGroup(src) {
|
|
2247
|
-
const bg = this.ensureGpuTexture(src, src.image, src.width, src.height, src.filterMode === FilterMode.NEAREST, src.dirty);
|
|
3840
|
+
const bg = this.ensureGpuTexture(src, src.image, src.width, src.height, src.filterMode === FilterMode.NEAREST, src.dirty, src.premultiplyAlpha, src.isRenderTarget);
|
|
2248
3841
|
src.dirty = false;
|
|
2249
3842
|
return bg;
|
|
2250
3843
|
}
|
|
@@ -2606,7 +4199,7 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2606
4199
|
case "Sprite": {
|
|
2607
4200
|
const img = obj;
|
|
2608
4201
|
const fr = img.frame;
|
|
2609
|
-
if (!fr || fr.width <= 0 || fr.height <= 0) break;
|
|
4202
|
+
if (!fr || fr.empty || fr.width <= 0 || fr.height <= 0) break;
|
|
2610
4203
|
const bg = this.ensureSourceBindGroup(fr.source);
|
|
2611
4204
|
if (!bg) break;
|
|
2612
4205
|
const ox = img.displayOriginX, oy = img.displayOriginY;
|
|
@@ -2690,6 +4283,7 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2690
4283
|
const a = effAlpha * (cols.alpha ? cols.alpha[s] : 1);
|
|
2691
4284
|
if (a <= 0) continue;
|
|
2692
4285
|
const fr = cols.frameIndex ? batch.texture.get(cols.frameIndex[s]) : batch.defaultFrame;
|
|
4286
|
+
if (!fr || fr.empty || fr.width <= 0 || fr.height <= 0) continue;
|
|
2693
4287
|
const sx = cols.scaleX ? cols.scaleX[s] : 1;
|
|
2694
4288
|
const sy = cols.scaleY ? cols.scaleY[s] : sx;
|
|
2695
4289
|
const rot = cols.rotation ? cols.rotation[s] : 0;
|
|
@@ -2811,7 +4405,8 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2811
4405
|
]));
|
|
2812
4406
|
if (this.vertCount > 0) {
|
|
2813
4407
|
this.device.queue.writeBuffer(this.vbo, 0, this.f32.subarray(0, this.vertCount * VERT_FLOATS));
|
|
2814
|
-
|
|
4408
|
+
const idxWriteCount = this.indexCount + 1 & -2;
|
|
4409
|
+
this.device.queue.writeBuffer(this.ibo, 0, this.indices.subarray(0, idxWriteCount));
|
|
2815
4410
|
}
|
|
2816
4411
|
if (this.sdfCount > 0) this.device.queue.writeBuffer(this.sdfInstVbo, 0, this.sdfF32.subarray(0, this.sdfCount * SDF_FLOATS));
|
|
2817
4412
|
const bg = camera.backgroundColor;
|
|
@@ -2826,7 +4421,7 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2826
4421
|
b: bg.b * bg.a,
|
|
2827
4422
|
a: bg.a
|
|
2828
4423
|
};
|
|
2829
|
-
const view = this.gpuContext.getCurrentTexture().createView();
|
|
4424
|
+
const view = this.gpuContext ? this.gpuContext.getCurrentTexture().createView() : this.ensureOffscreenTarget();
|
|
2830
4425
|
const encoder = this.device.createCommandEncoder();
|
|
2831
4426
|
const pass = encoder.beginRenderPass({ colorAttachments: [{
|
|
2832
4427
|
view,
|
|
@@ -2866,6 +4461,97 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2866
4461
|
this.lastStats.sdfPanels = this.sdfCount;
|
|
2867
4462
|
this.lastStats.sdfDraws = sdfDraws;
|
|
2868
4463
|
}
|
|
4464
|
+
encodeCopyToStaging() {
|
|
4465
|
+
if (!this.offscreenTex) this.ensureOffscreenTarget();
|
|
4466
|
+
const w = this.width;
|
|
4467
|
+
const h = this.height;
|
|
4468
|
+
const bytesPerRow = w * 4 + 255 & -256;
|
|
4469
|
+
const byteLen = bytesPerRow * h;
|
|
4470
|
+
if (!this.readbackBuf || this.readbackCap < byteLen) {
|
|
4471
|
+
try {
|
|
4472
|
+
this.readbackBuf?.destroy?.();
|
|
4473
|
+
} catch {}
|
|
4474
|
+
this.readbackBuf = this.device.createBuffer({
|
|
4475
|
+
size: byteLen,
|
|
4476
|
+
usage: 9
|
|
4477
|
+
});
|
|
4478
|
+
this.readbackCap = byteLen;
|
|
4479
|
+
}
|
|
4480
|
+
this.readbackBytesPerRow = bytesPerRow;
|
|
4481
|
+
const enc = this.device.createCommandEncoder();
|
|
4482
|
+
enc.copyTextureToBuffer({ texture: this.offscreenTex }, {
|
|
4483
|
+
buffer: this.readbackBuf,
|
|
4484
|
+
offset: 0,
|
|
4485
|
+
bytesPerRow,
|
|
4486
|
+
rowsPerImage: h
|
|
4487
|
+
}, [
|
|
4488
|
+
w,
|
|
4489
|
+
h,
|
|
4490
|
+
1
|
|
4491
|
+
]);
|
|
4492
|
+
this.device.queue.submit([enc.finish()]);
|
|
4493
|
+
return {
|
|
4494
|
+
buf: this.readbackBuf,
|
|
4495
|
+
w,
|
|
4496
|
+
h,
|
|
4497
|
+
bytesPerRow,
|
|
4498
|
+
byteLen
|
|
4499
|
+
};
|
|
4500
|
+
}
|
|
4501
|
+
unpackStagingBytes(mapped, w, h, bytesPerRow, out) {
|
|
4502
|
+
const rowBytes = w * 4;
|
|
4503
|
+
const totalOut = rowBytes * h;
|
|
4504
|
+
const dst = out && out.byteLength >= totalOut ? out : new Uint8Array(totalOut);
|
|
4505
|
+
if (bytesPerRow === rowBytes && this.format !== "bgra8unorm") {
|
|
4506
|
+
dst.set(mapped.subarray(0, totalOut));
|
|
4507
|
+
return dst;
|
|
4508
|
+
}
|
|
4509
|
+
const isBgra = this.format === "bgra8unorm";
|
|
4510
|
+
for (let y = 0; y < h; y++) {
|
|
4511
|
+
const sOff = y * bytesPerRow;
|
|
4512
|
+
const dOff = y * rowBytes;
|
|
4513
|
+
if (!isBgra) dst.set(mapped.subarray(sOff, sOff + rowBytes), dOff);
|
|
4514
|
+
else for (let x = 0; x < rowBytes; x += 4) {
|
|
4515
|
+
dst[dOff + x] = mapped[sOff + x + 2];
|
|
4516
|
+
dst[dOff + x + 1] = mapped[sOff + x + 1];
|
|
4517
|
+
dst[dOff + x + 2] = mapped[sOff + x];
|
|
4518
|
+
dst[dOff + x + 3] = mapped[sOff + x + 3];
|
|
4519
|
+
}
|
|
4520
|
+
}
|
|
4521
|
+
return dst;
|
|
4522
|
+
}
|
|
4523
|
+
/**
|
|
4524
|
+
* Synchronously read rendered W3C `RGBA8888` pixels when backed by a synchronous native WebGPU device
|
|
4525
|
+
* (`wgpu-native` C FFI via `bun:ffi` or Node `koffi`).
|
|
4526
|
+
*/
|
|
4527
|
+
readPixelsSync(out) {
|
|
4528
|
+
if (typeof this.device._readBufferSync !== "function") throw new Error("RendererWebGpu.readPixelsSync: current GPUDevice only supports async mapAsync(); use await stage.toRgbaAsync() or stage.recordFrames() / stage.encodeVideo()");
|
|
4529
|
+
const { buf, w, h, bytesPerRow, byteLen } = this.encodeCopyToStaging();
|
|
4530
|
+
if (bytesPerRow === w * 4 && this.format !== "bgra8unorm") {
|
|
4531
|
+
const totalOut = w * h * 4;
|
|
4532
|
+
const dst = out && out.byteLength >= totalOut ? out : new Uint8Array(totalOut);
|
|
4533
|
+
this.device._readBufferSync(buf, dst, totalOut);
|
|
4534
|
+
return dst;
|
|
4535
|
+
}
|
|
4536
|
+
const raw = new Uint8Array(byteLen);
|
|
4537
|
+
this.device._readBufferSync(buf, raw, byteLen);
|
|
4538
|
+
return this.unpackStagingBytes(raw, w, h, bytesPerRow, out);
|
|
4539
|
+
}
|
|
4540
|
+
/**
|
|
4541
|
+
* Asynchronously read rendered W3C `RGBA8888` pixels across ALL WebGPU backends
|
|
4542
|
+
* (`wgpu-native` FFI, Google Dawn `webgpu`, Deno `navigator.gpu`, and Browser WebGPU).
|
|
4543
|
+
*/
|
|
4544
|
+
async readPixelsAsync(out) {
|
|
4545
|
+
if (typeof this.device._readBufferSync === "function") return this.readPixelsSync(out);
|
|
4546
|
+
const { buf, w, h, bytesPerRow, byteLen } = this.encodeCopyToStaging();
|
|
4547
|
+
await buf.mapAsync(GPUMapMode_READ, 0, byteLen);
|
|
4548
|
+
try {
|
|
4549
|
+
const mapped = new Uint8Array(buf.getMappedRange(0, byteLen));
|
|
4550
|
+
return this.unpackStagingBytes(mapped, w, h, bytesPerRow, out);
|
|
4551
|
+
} finally {
|
|
4552
|
+
buf.unmap();
|
|
4553
|
+
}
|
|
4554
|
+
}
|
|
2869
4555
|
/** Create an offscreen render target backed by a registered Stage `Texture` (`key`) for WebGPU passes (e.g. `Lighting2DSystem`). */
|
|
2870
4556
|
createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
|
|
2871
4557
|
const w = Math.max(1, Math.round(width));
|
|
@@ -2921,7 +4607,9 @@ var RendererWebGpu = class extends EventEmitter {
|
|
|
2921
4607
|
this.sdfInstVbo?.destroy?.();
|
|
2922
4608
|
this.sdfQuadIbo?.destroy?.();
|
|
2923
4609
|
this.dummyEntry?.tex?.destroy?.();
|
|
4610
|
+
this.offscreenTex?.destroy?.();
|
|
4611
|
+
this.readbackBuf?.destroy?.();
|
|
2924
4612
|
} catch {}
|
|
2925
4613
|
}
|
|
2926
4614
|
};
|
|
2927
|
-
export {
|
|
4615
|
+
export { requestHeadlessWebGpu as a, openWgpuNativeFfi as i, requestWebGpuDevice as n, Renderer2D as o, openDawnWebGpu as r, Camera as s, RendererWebGpu as t };
|