Wgpu.psm1

foreach ($loaded in Get-Module -Name 'Wgpu' -All) {
$same = if ([System.IO.Path]::DirectorySeparatorChar -eq '\') { $loaded.ModuleBase -eq $PSScriptRoot } else { $loaded.ModuleBase -ceq $PSScriptRoot }
if ($same) { continue }
if ($loaded.ModuleType -eq 'Script' -and $null -ne (& $loaded { $ExecutionContext.SessionState.PSVariable.GetValue('PwrsStoodDownFor') })) { continue }
if ($loaded.Version -eq [version]'0.1.0') {
Microsoft.PowerShell.Utility\Write-Verbose ('Wgpu' + ' ' + '0.1.0' + ' is already loaded from ' + $loaded.ModuleBase + ', so the copy at ' + $PSScriptRoot + ' is not loaded.')
$ExecutionContext.SessionState.PSVariable.Set('PwrsStoodDownFor', $loaded.ModuleBase)
return
}
throw [System.Management.Automation.ErrorRecord]::new([System.InvalidOperationException]::new('Wgpu' + ' ' + $loaded.Version + ' is already loaded from ' + $loaded.ModuleBase + ', and a session can have only one version of a module, so ' + 'Wgpu' + ' ' + '0.1.0' + ' at ' + $PSScriptRoot + ' is not loaded.'), 'PwrsOtherVersionLoaded', [System.Management.Automation.ErrorCategory]::ResourceExists, $loaded)
}
$root = $PSScriptRoot
$tfm = if ($PSVersionTable.PSEdition -eq 'Core') { 'net10.0' } else { 'netstandard2.0' }
$pwrsLoad = [System.AppDomain]::CurrentDomain.GetData('pwrs-load')
if ($null -eq $pwrsLoad) {
$pwrsLoad = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'pwrs-load')
if ($tfm -eq 'net10.0' -and -not $IsWindows) {
$pwrsUid = $null
$pwrsProbe = [System.IO.Path]::GetTempFileName()
try { $pwrsUid = $ExecutionContext.InvokeProvider.Item.Get([string[]]@($pwrsProbe), $true, $true)[0].UnixStat.UserId }
finally { [System.IO.File]::Delete($pwrsProbe) }
$pwrsMine = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'pwrs-load-' + $pwrsUid)
$pwrsPrivate = [System.IO.UnixFileMode]'UserRead, UserWrite, UserExecute'
$pwrsStat = $null
$pwrsWhy = ''
if ($null -ne $pwrsUid) {
try {
$null = [System.IO.Directory]::CreateDirectory($pwrsMine, $pwrsPrivate)
$pwrsStat = $ExecutionContext.InvokeProvider.Item.Get([string[]]@($pwrsMine), $true, $true)[0].UnixStat
}
catch [System.IO.IOException], [System.UnauthorizedAccessException] {
# A file by that name, or a folder this user cannot read: the
# check below takes it as not this user's, and the warning
# gives the error's message.
$pwrsWhy = ' (' + $_.Exception.Message + ')'
}
}
if ($null -ne $pwrsStat -and $pwrsStat.UserId -eq $pwrsUid -and [string]$pwrsStat.ItemType -eq 'Directory') {
if (($pwrsStat.Mode -band 63) -ne 0) { [System.IO.File]::SetUnixFileMode($pwrsMine, $pwrsPrivate) }
$pwrsLoad = $pwrsMine
}
else {
$pwrsLoad = [System.IO.Directory]::CreateTempSubdirectory('pwrs-load-').FullName
Microsoft.PowerShell.Utility\Write-Warning ('pwrs: ' + $pwrsMine + ' is not a folder of this user''s own' + $pwrsWhy + ', so this session stages its copies in ' + $pwrsLoad + ', which no other session uses.')
}
}
[System.AppDomain]::CurrentDomain.SetData('pwrs-load', $pwrsLoad)
}
if (-not ('Pwrs.Bootstrap.Loader' -as [type])) {
$stage = [System.IO.Path]::Combine($pwrsLoad, [string]$PID, 'boot', '877a0aad59d8d4a3', $tfm)
$null = [System.IO.Directory]::CreateDirectory($stage)
$boot = [System.IO.Path]::Combine($stage, 'Pwrs.Bootstrap.dll')
if (-not [System.IO.File]::Exists($boot)) {
$pdb = [System.IO.Path]::Combine($root, $tfm, 'Pwrs.Bootstrap.pdb')
$bootPdb = [System.IO.Path]::Combine($stage, 'Pwrs.Bootstrap.pdb')
if ([System.IO.File]::Exists($pdb) -and -not [System.IO.File]::Exists($bootPdb)) {
$partial = $bootPdb + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial'
[System.IO.File]::Copy($pdb, $partial)
try { [System.IO.File]::Move($partial, $bootPdb) }
catch [System.IO.IOException] {
[System.IO.File]::Delete($partial)
if (-not [System.IO.File]::Exists($bootPdb)) { throw }
}
}
$partial = $boot + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial'
[System.IO.File]::Copy([System.IO.Path]::Combine($root, $tfm, 'Pwrs.Bootstrap.dll'), $partial)
try { [System.IO.File]::Move($partial, $boot) }
catch [System.IO.IOException] {
[System.IO.File]::Delete($partial)
if (-not [System.IO.File]::Exists($boot)) { throw }
}
}
$null = [System.Reflection.Assembly]::LoadFrom($boot)
}
try { $shell = [Pwrs.Bootstrap.Loader]::Load($root, 'Wgpu', $tfm) }
catch {
if ($tfm -eq 'net10.0' -and [Environment]::Version.Major -lt 8) {
throw ('Wgpu' + ' needs PowerShell 7.4 or later: its PowerShell 7 half is built against .NET 8, and this is PowerShell ' + $PSVersionTable.PSVersion + ' on .NET ' + [Environment]::Version + '.')
}
throw
}
$shell.GetType('Pwrs.Modules.Wgpu.PwrsModuleRoot', $true).GetField('Value').SetValue($null, $root)
if ($tfm -eq 'netstandard2.0') {
$staged = [System.IO.Path]::GetDirectoryName($shell.Location)
foreach ($dep in @('System.Security.AccessControl.dll', 'System.Security.Principal.Windows.dll', 'System.IO.FileSystem.AccessControl.dll', 'Microsoft.Win32.Registry.dll')) {
$to = [System.IO.Path]::Combine($staged, $dep)
if (-not [System.IO.File]::Exists($to)) {
$partial = $to + '.' + [System.Guid]::NewGuid().ToString('N') + '.partial'
[System.IO.File]::Copy([System.IO.Path]::Combine($root, $tfm, $dep), $partial)
try { [System.IO.File]::Move($partial, $to) }
catch [System.IO.IOException] {
[System.IO.File]::Delete($partial)
if (-not [System.IO.File]::Exists($to)) { throw }
}
}
}
}
foreach ($m in Get-Module -All) {
if ($m.ModuleType -eq 'Binary' -and ($m.Path -eq $shell.Location -or $m.Name -like '*Wgpu.Shell*')) {
Remove-Module -ModuleInfo $m -Force -ErrorAction SilentlyContinue
}
}
$lifecycle = $shell.GetType('Pwrs.Modules.Wgpu.PwrsModuleLifecycle', $true)
try {
Import-Module -Assembly $shell -Force
}
catch {
foreach ($e in $lifecycle::TakeImportErrors()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue }
$inner = $_.Exception
while ($null -ne $inner.InnerException -and $inner.GetType().FullName -ne 'Pwrs.PwrsException' -and -not $inner.Data.Contains('PwrsErrorId')) { $inner = $inner.InnerException }
if ($inner -is [System.Management.Automation.IContainsErrorRecord]) { throw $inner.ErrorRecord }
if ($inner.Data.Contains('PwrsErrorId')) { throw [System.Management.Automation.ErrorRecord]::new($inner, [string]$inner.Data['PwrsErrorId'], [System.Management.Automation.ErrorCategory][string]$inner.Data['PwrsErrorCategory'], $_.TargetObject) }
throw $inner
}
foreach ($e in $lifecycle::TakeImportErrors()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue }
$ExecutionContext.SessionState.Module.OnRemove = { foreach ($e in $lifecycle::Removed()) { Microsoft.PowerShell.Utility\Write-Error -ErrorRecord $e -ErrorAction Continue } }
Export-ModuleMember -Cmdlet 'Get-WgpuAdapter', 'Open-WgpuDevice', 'Get-WgpuDevice', 'Close-WgpuDevice', 'Wait-WgpuDevice', 'Set-WgpuLog', 'New-WgpuBuffer', 'Read-WgpuBuffer', 'Write-WgpuBuffer', 'Copy-WgpuBuffer', 'Clear-WgpuBuffer', 'Remove-WgpuBuffer', 'New-WgpuShader', 'Test-WgpuShader', 'Get-WgpuShaderInfo', 'ConvertTo-WgpuShader', 'New-WgpuComputePipeline', 'New-WgpuBindGroup', 'Invoke-WgpuCompute', 'Invoke-WgpuMap', 'Measure-WgpuArray', 'New-WgpuCommandEncoder', 'Submit-WgpuCommand', 'New-WgpuQuerySet', 'New-WgpuTexture', 'Read-WgpuTexture', 'Write-WgpuTexture', 'Import-WgpuImage', 'Export-WgpuImage', 'Resize-WgpuTexture', 'Convert-WgpuTexture', 'ConvertTo-WgpuNormalMap', 'ConvertTo-WgpuCubeMap', 'ConvertFrom-WgpuCubeMap', 'ConvertTo-WgpuIrradianceMap', 'ConvertTo-WgpuSpecularMap', 'New-WgpuBrdfTable', 'Join-WgpuTexture', 'Compare-WgpuTexture', 'Measure-WgpuTexture', 'New-WgpuSampler', 'Get-WgpuTextureFormat', 'New-WgpuRenderPipeline', 'Invoke-WgpuRender' -Alias 'Get-GpuAdapter', 'Open-GpuDevice', 'Get-GpuDevice', 'Close-GpuDevice', 'Wait-GpuDevice', 'Set-GpuLog', 'New-GpuBuffer', 'Read-GpuBuffer', 'Write-GpuBuffer', 'Copy-GpuBuffer', 'Clear-GpuBuffer', 'Remove-GpuBuffer', 'New-GpuShader', 'Test-GpuShader', 'Get-GpuShaderInfo', 'ConvertTo-GpuShader', 'New-GpuComputePipeline', 'New-GpuBindGroup', 'Invoke-GpuCompute', 'Invoke-GpuMap', 'Measure-GpuArray', 'New-GpuCommandEncoder', 'Submit-GpuCommand', 'New-GpuQuerySet', 'New-GpuTexture', 'Read-GpuTexture', 'Write-GpuTexture', 'Import-GpuImage', 'Export-GpuImage', 'Resize-GpuTexture', 'Convert-GpuTexture', 'ConvertTo-GpuNormalMap', 'ConvertTo-GpuCubeMap', 'ConvertFrom-GpuCubeMap', 'ConvertTo-GpuIrradianceMap', 'ConvertTo-GpuSpecularMap', 'New-GpuBrdfTable', 'Join-GpuTexture', 'Compare-GpuTexture', 'Measure-GpuTexture', 'New-GpuSampler', 'Get-GpuTextureFormat', 'New-GpuRenderPipeline', 'Invoke-GpuRender'