Skip to content

Instantly share code, notes, and snippets.

@emoose
Last active April 8, 2024 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoose/11271bbb3b42fb3b1b0e1c83eef47c05 to your computer and use it in GitHub Desktop.
Save emoose/11271bbb3b42fb3b1b0e1c83eef47c05 to your computer and use it in GitHub Desktop.
PowerShell script/module that allows globally updating DLSS for all games
# Global DLSS update script by emoose - https://gist.github.com/emoose/11271bbb3b42fb3b1b0e1c83eef47c05
# This allows setting a single global DLSS DLL for games to use
# If the global version is newer than the one included with game, it should get loaded automatically
# (more info at https://forums.guru3d.com/threads/.439761/page-143#post-6221767)
#
# Two ways of using this script:
# - save script as UpdateDLSS.ps1 then run it through powershell like "powershell .\UpdateDLSS.ps1"
# (or "powershell .\UpdateDLSS.ps1 -DllPath C:\DLLs\nvngx_dlss.dll")
#
# - save script into powershell modules folder, so you can use it from any location
# Depending on PS version, save to either:
# - Documents\WindowsPowerShell\Modules\UpdateDLSS\UpdateDLSS.psm1
# - Documents\PowerShell\Modules\UpdateDLSS\UpdateDLSS.psm1
# Alternatively System32 should work for all PS versions:
# - C:\Windows\system32\WindowsPowerShell\v1.0\Modules\UpdateDLSS\UpdateDLSS.psm1
# With it saved into modules folder, you should be able to open a new powershell window and then run UpdateDLSS from any location
# (or run "UpdateDLSS -DllPath C:\DLLs\nvngx_dlss.dll")
#
# If ran without -DllPath param you will be prompted for the path
# When prompted you can usually just drag+drop the dll into the powershell window
# Admin access shouldn't be required for this script
function Update-IniFile {
param (
[string]$FilePath,
[string]$Section,
[string]$Key,
[string]$Value
)
$content = Get-Content $FilePath
$sectionExists = $false
$keyExists = $false
# Loop through each line in the content
for ($i = 0; $i -lt $content.Count; $i++) {
$line = $content[$i]
# Check if the line contains the section header
if ($line -match "^\[$Section\]") {
$sectionExists = $true
# Search for the key in subsequent lines within the same section
for ($j = $i + 1; $j -lt $content.Count; $j++) {
$line = $content[$j]
# Break out of loop if new section has started
if ($line.StartsWith("[") -And $line.EndsWith("]")) {
break
}
# Check if the line contains the key
if ($line -match "^$Key\s*=") {
# Update the value
$content[$j] = "$Key = $Value"
$keyExists = $true
break
}
}
# If the key doesn't exist, add it to the section
if (-not $keyExists) {
# Insert the key-value pair at the start of this section
$content[$i] = $content[$i] + "`r`n$Key = $Value"
}
break
}
}
# If the section doesn't exist, add it along with the key
if (-not $sectionExists) {
$content += "[$Section]`r`n"
$content += "$Key = $Value`r`n"
}
# Write the updated content back to the INI file
$content | Set-Content $FilePath
}
function UpdateDLSS {
param (
[Parameter(Mandatory=$true)]
[string]$DllPath
)
$nvngxDllPath = $DllPath.Trim(" ").Trim('"').Trim(" ")
$nvngxDllType = (Split-Path $nvngxDllPath -Leaf).Split('_')[1] -replace '\.dll$'
# DllImport needs full DLL path, try figuring it out:
$DllPath = [System.IO.Path]::GetFullPath($nvngxDllPath)
if (-not (Test-Path $DllPath)) {
$DllPath = Join-Path -Path $PWD -ChildPath $nvngxDllPath
if (-not (Test-Path $DllPath)) {
$DllPath = Join-Path -Path $PSScriptRoot -ChildPath $nvngxDllPath
if (-not (Test-Path $DllPath)) {
Write-Host "Input DLL path not found: $nvngx_filename"
return
}
}
}
Write-Host "Input DLL path: $DllPath"
$signature = Get-AuthenticodeSignature -FilePath $DllPath
if ($signature.Status -ne "Valid") {
Write-Host "The input DLL doesn't have a valid authenticode signature."
Write-Host "UpdateDLSS requires loading/executing the DLL to work, which may be a risk."
Write-Host "Aborting script."
return
}
# DllImport code
$dllimport = @"
[DllImport(@"$DllPath", CallingConvention = CallingConvention.Cdecl)]
public static extern int NVSDK_NGX_GetSnippetVersion();
"@
# Generate random string to use for class name above, so script can be ran multiple times in same session
$length = 10
$randomString = -join ((65..90) + (97..122) | Get-Random -Count $length | ForEach-Object { [char]$_ })
$type = Add-Type -MemberDefinition $dllimport -Name "NGX$randomString" -Namespace "NV" -PassThru
$nvngxVersion = 0
try {
$nvngxVersion = $type::NVSDK_NGX_GetSnippetVersion()
} catch {
Write-Host "Failed to execute NVSDK_NGX_GetSnippetVersion, error: $_"
return
}
$ver = [BitConverter]::GetBytes($nvngxVersion)
# Seems we don't need the result of NVSDK_NGX_GetGPUArchitecture, 160 always works fine?
#$nvngxDllArch = $type::NVSDK_NGX_GetGPUArchitecture().ToString('X')
$nvngxDllArch = "160"
$nvngxBasePath = "C:\ProgramData\NVIDIA\NGX\models"
$dllDestPath = "$nvngxBasePath\$nvngxDllType\versions\$nvngxVersion\files\$($nvngxDllArch)_E658703.bin"
$configFilePath = "$nvngxBasePath\nvngx_config.txt"
$configKey = "app_E658703"
$configValue = "$($ver[2]).$($ver[1]).$($ver[0])"
Write-Host " "
Write-Host "Detected NVNGX DLL info:"
Write-Host " Type: $nvngxDllType"
Write-Host " Version: $configValue"
Write-Host " Signature: $($signature.Status)"
Write-Host " "
Write-Host "The following operations will be performed:"
Write-Host "1. Copy DLL to $dllDestPath"
Write-Host "2. Update config file $configFilePath with the following:"
Write-Host " [$nvngxDllType]"
Write-Host " $configKey = $configValue"
Write-Host " "
if (Test-Path $dllDestPath) {
Write-Host "DLL destination already exists, script aborted."
return
}
$confirmation = Read-Host "Do you want to proceed? (y/n)"
if ($confirmation.ToLower() -ne 'y') {
Write-Host "Operation canceled by user."
return
}
# Create folders if needed
if (-not (Test-Path (Split-Path $dllDestPath))) {
New-Item -ItemType Directory -Force -Path (Split-Path $dllDestPath) | Out-Null
}
# Copy DLL
Copy-Item -Path $DllPath -Destination $dllDestPath -Force
if (-not (Test-Path $configFilePath)) {
Write-Host "Config file doesn't exist, creating $configFilePath"
New-Item -Path $configFilePath -ItemType File > $null
}
# Update config file
Update-IniFile -FilePath $configFilePath -Section $nvngxDllType -Key $configKey -Value $configValue
Write-Host "Operations completed successfully."
}
try
{
Export-ModuleMember -Function "UpdateDLSS"
} catch {
UpdateDLSS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment