Skip to content

Instantly share code, notes, and snippets.

@philipturner
Created September 30, 2025 11:58
Show Gist options
  • Select an option

  • Save philipturner/6bb842cf12c7a21326ef67e9e785511f to your computer and use it in GitHub Desktop.

Select an option

Save philipturner/6bb842cf12c7a21326ef67e9e785511f to your computer and use it in GitHub Desktop.
do {
// Allocate the UpscaleGetGPUMemoryUsageV2, causing a memory leak.
let upscaleGetGPUMemoryUsageV2 = UnsafeMutablePointer<ffxQueryDescUpscaleGetGPUMemoryUsageV2>.allocate(capacity: 1)
upscaleGetGPUMemoryUsageV2.pointee.header.type = UInt64(FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE_V2)
upscaleGetGPUMemoryUsageV2.pointee.header.pNext = nil
// Bind the device, causing a memory leak.
do {
let iid = SwiftCOM.ID3D12Device.IID
let d3d12Device = application.device.d3d12Device
let interface = try! d3d12Device.QueryInterface(iid: iid)
guard let interface else {
fatalError("Could not get interface.")
}
upscaleGetGPUMemoryUsageV2.pointee.device = interface
}
// Bind the texture dimensions.
func createFFXDimensions(
_ input: SIMD2<Int>
) -> FfxApiDimensions2D {
var output = FfxApiDimensions2D()
output.width = UInt32(input[0])
output.height = UInt32(input[1])
return output
}
do {
let maxRenderSize = createFFXDimensions(
application.display.frameBufferSize / 3)
let maxUpscaleSize = createFFXDimensions(
application.display.frameBufferSize)
upscaleGetGPUMemoryUsageV2.pointee.maxRenderSize = maxRenderSize
upscaleGetGPUMemoryUsageV2.pointee.maxUpscaleSize = maxUpscaleSize
}
upscaleGetGPUMemoryUsageV2.pointee.flags = UInt32(
FFX_UPSCALE_ENABLE_DEPTH_INVERTED.rawValue)
// Allocate the EffectMemoryUsage, causing a memory leak.
var effectMemoryUsage = UnsafeMutablePointer<FfxApiEffectMemoryUsage>.allocate(capacity: 1)
upscaleGetGPUMemoryUsageV2.pointee.gpuMemoryUsageUpscaler = effectMemoryUsage
// Obtain a pointer to the header.
upscaleGetGPUMemoryUsageV2.withMemoryRebound(
to: ffxApiHeader.self, capacity: 1
) { pointer in
let error = ffxQuery(nil, pointer)
guard error == 0 else {
fatalError("Received error code \(error).")
}
}
// 1440x1080, 2x upscaling
// totalUsageInBytes 48_758_784
// aliasableUsageInBytes 5_636_096
//
// 1440x1080, 3x upscaling
// totalUsageInBytes 36_896_768
// aliasableUsageInBytes 3_407_872
print("Default Upscaler Query GPUMemoryUsageV2 totalUsageInBytes", effectMemoryUsage.pointee.totalUsageInBytes)
print("Default Upscaler Query GPUMemoryUsageV2 aliasableUsageInBytes", effectMemoryUsage.pointee.aliasableUsageInBytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment