Skip to content

Instantly share code, notes, and snippets.

View philipturner's full-sized avatar
🏠
Working from home

Philip Turner philipturner

🏠
Working from home
View GitHub Profile
@philipturner
philipturner / FIRE.swift
Last active November 11, 2025 23:42
GitHub Gist for the propargyl alcohol tripod test
//
// FIRE.swift
// MolecularRendererApp
//
// Created by Philip Turner on 5/31/24.
//
import HDL
import Numerics
import HDL
import MM4
import MolecularRenderer
import OpenMM
import QuaternionModule
import Foundation
import func QuartzCore.CACurrentMediaTime // profiling on macOS
// MARK: - Compile Structure
let cameraNear: Float = 0.001
let cameraFar: Float = 1000 // ignored for bInfinite = true, ends up being ~1e38
let bInverted: Bool = true
let bInfinite: Bool = true
// FidelityFX seems to want completely different depth semantics than MetalFX.
// In MetalFX, there is no way for the API to accept a camera near and far
// plane. The function 1 / (1 - depth) maps a near plane of 0 to an output of 1.
// In FidelityFX, we might want to do 0.001 / depth, whic maps a near plane
// of 0.001 units to an output of 1.
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
// Implementation of upscaling:
// - Debug motion vectors and camera orientation matrices changing between
// frames. Also establish depth textures; all the details except actual
// invocation of the upscaler.
// - Last component of this task is doing the motion vectors.
// - Choosing one of the auxiliary textures as input for upscaling is an
// incredible method to debug.
// - First do the camera projection matrix part, where atoms stay static.
// Then figure out tracking the motion delta when atoms may move over
// time (this is part of atom transactions).
#if os(Windows)
import FidelityFX
import SwiftCOM
import WinSDK
// Old reference code that just exists to make sure FidelityFX links properly.
public struct FFXUpscalerDescriptor {
public var device: Device?
/* Analog Control RGB LED Color, Teensyduino Tutorial #4
http://www.pjrc.com/teensy/tutorial4.html
This example code is in the public domain.
*/
// TODO: Save to GitHub gist
const int outPin = 12;
void setup() {
// Next steps:
// - Allow the window to be closed with "Ctrl + W" on Windows.
// - Track keyboard and mouse events, establishing a prototype of the
// 'UserInterface' utility.
// - Decide on how to hide the cursor from the user. Not a trivial decision.
// - Previously, hid when the user entered the window. Because of stability
// issues, the user must press 'Esc' once before the first mouse hide.
// - Afterward, 'Esc' is used to toggle mouse visibility.
// - The mouse is only tracked and connected to camera movements when the
// cursor is hidden.
import Dispatch
import struct Foundation.Date
func performWork(innerIterationCount: Int) -> Float {
var innerIterationSums = [Float](repeating: 0, count: innerIterationCount)
for innerIterationID in 0..<innerIterationCount {
let threadCount: Int = 10
nonisolated(unsafe)
var sharedMemory = [Float](repeating: 0, count: threadCount)
import simd
protocol LinearSystem {
associatedtype Matrix
var matrix: Matrix { get }
associatedtype Solution
func solve(matrix: Matrix) -> Solution
}