Skip to content

Instantly share code, notes, and snippets.

@philipturner
Last active October 11, 2025 16:58
Show Gist options
  • Select an option

  • Save philipturner/44e71d77abebb554bb8e58c1cbae8eff to your computer and use it in GitHub Desktop.

Select an option

Save philipturner/44e71d77abebb554bb8e58c1cbae8eff to your computer and use it in GitHub Desktop.
import HDL
import MolecularRenderer
@MainActor
func createApplication() -> Application {
// Set up the device.
var deviceDesc = DeviceDescriptor()
deviceDesc.deviceID = Device.fastestDeviceID
let device = Device(descriptor: deviceDesc)
// Set up the display.
var displayDesc = DisplayDescriptor()
displayDesc.device = device
displayDesc.frameBufferSize = SIMD2<Int>(1080, 1080)
displayDesc.monitorID = device.fastestMonitorID
let display = Display(descriptor: displayDesc)
// Set up the application.
var applicationDesc = ApplicationDescriptor()
applicationDesc.device = device
applicationDesc.display = display
applicationDesc.upscaleFactor = 1
applicationDesc.addressSpaceSize = 2_000_000
applicationDesc.voxelAllocationSize = 200_000_000
applicationDesc.worldDimension = 32
let application = Application(descriptor: applicationDesc)
return application
}
let application = createApplication()
// 8631 atoms
let lattice = Lattice<Cubic> { h, k, l in
Bounds { 10 * (h + k + l) }
Material { .checkerboard(.silicon, .carbon) }
}
for atomID in lattice.atoms.indices {
let atom = lattice.atoms[atomID]
application.atoms[atomID] = atom
}
application.updateBVH(inFlightFrameID: 0)
application.forgetIdleState(inFlightFrameID: 0)
application.runDiagnostic()
extension Application {
public func runDiagnostic() {
device.commandQueue.withCommandList { commandList in
bvhBuilder.debugDiagnostic(
commandList: commandList,
dataBuffer: bvhBuilder.voxelResources.atomicCounters)
bvhBuilder.counters.diagnosticBuffer.download(
commandList: commandList,
inFlightFrameID: 0)
}
device.commandQueue.flush()
var output = [SIMD8<UInt32>](repeating: .zero, count: 4096)
bvhBuilder.counters.diagnosticBuffer.read(
data: &output,
inFlightFrameID: 0)
var xorHash: SIMD4<UInt32> = .zero
var rotateHash: SIMD4<UInt32> = .zero
var addressRotateHash: UInt32 = .zero
var referenceSum: UInt32 = .zero
var voxelSum: UInt32 = .zero
for z in 0..<16 {
for y in 0..<16 {
for x in 0..<16 {
let address = z * 16 * 16 + y * 16 + x
let counters = output[address]
guard counters.wrappedSum() > 0 else {
continue
}
let storage = SIMD8<UInt16>(truncatingIfNeeded: counters)
let storageCasted = unsafeBitCast(storage, to: SIMD4<UInt32>.self)
xorHash ^= storageCasted
xorHash = (xorHash &<< 3) | (xorHash &>> (32 - 3))
rotateHash &*= storageCasted
rotateHash &+= 1
rotateHash = (rotateHash &<< 9) | (rotateHash &>> (32 - 9))
addressRotateHash &*= UInt32(address)
addressRotateHash &+= 1
addressRotateHash =
(addressRotateHash &<< 9) | (addressRotateHash &>> (32 - 9))
referenceSum += counters.wrappedSum()
voxelSum += 1
}
}
}
// Inspect the checksum.
print(xorHash)
print(rotateHash)
print(addressRotateHash)
print(referenceSum)
print(voxelSum)
let expectedXorHash = SIMD4<UInt32>(
66447249, 3273538316, 1173786066, 933301158)
let expectedRotateHash = SIMD4<UInt32>(
2682827453, 1944504064, 3826119466, 1921755015)
let expectedAddressRotateHash: UInt32 = 396663315
let expectedReferenceSum: UInt32 = 13897
let expectedVoxelSum: UInt32 = 64
guard all(xorHash .== expectedXorHash),
all(rotateHash .== expectedRotateHash),
addressRotateHash == expectedAddressRotateHash,
referenceSum == expectedReferenceSum,
voxelSum == expectedVoxelSum else {
fatalError("Did not get expected results.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment