dymokomi/luce-usd
Source of release 0.9.1
## luce-usd's benchmark. Prints one `name<TAB>milliseconds<TAB>count` line per
## case; run.py builds it with --native --opt 3 and makes the table.
##
## The reference rows time luce-geocore's own .prism codec on the same
## geometry, so the USD rows (added as the readers and writers land) read as
## ratios against it: the 837x837 quad grid (702,244 points, 700,569 faces)
## with a corner uv, as geocore's bench saves it.
import time
from luce_std import math
import c
from luce_std import files
import interop
import memory
from luce_geocore import geocore
from luce_geocore.geocore import Vector3, Mesh, GeometrySet
from luce_geocore.kernel import save_geometry, load_geometry, adopt_set, set_mesh, dispose_set
from luce_geocore.core.growing import Growing
from luce_usd.kernel import ImportOptions, ExportOptions, Warnings, import_layer, export_set, read_file, most_bytes, Names, LayerData, read_usda, read_usdc, write_usda, write_usdc, read_usdz, write_usdz, PackageFile
from io import path
import strings
## Where the grid crate goes (a module buffer: error messages and paths
## outlive the functions that format them).
var crate_path: u8[1024]
func row(name: str, started: u64, count: usize):
print(f"{name}\t{(f64)time.since(started) / 1000000.0}\t{count}\t")
## The n×n quad grid of geocore's verb benchmark.
func quad_grid(n: usize) -> interop.Reference[Mesh]!:
let points = try new Vector3[(n + 1) * (n + 1)] --- in memory.heap
defer free(points) in memory.heap
let sizes = try new i64[n * n] --- in memory.heap
defer free(sizes) in memory.heap
let corners = try new i64[n * n * 4] --- in memory.heap
defer free(corners) in memory.heap
for z in 0..<n + 1:
for x in 0..<n + 1: points[z * (n + 1) + x] = Vector3((f64)x - (f64)n * 0.5, 0.0, (f64)z - (f64)n * 0.5)
for z in 0..<n:
for x in 0..<n:
let a = (i64)(z * (n + 1) + x)
let face = z * n + x
sizes[face] = 4
corners[face * 4] = a
corners[face * 4 + 1] = a + (i64)n + 1
corners[face * 4 + 2] = a + (i64)n + 2
corners[face * 4 + 3] = a + 1
return try interop.Reference[Mesh].adopt(try new Mesh(points, sizes, corners) in memory.heap, geocore.mesh_type, memory.heap)
## A path in the temporary directory.
func temporary(file: Growing[u8]*, name: str) -> !:
try file.assign(path.temp().bytes)
try file.push(47)
for byte in name.bytes: try file.push(byte)
pub func main(arguments: str[]) -> i32!:
let grid = try quad_grid(837)
defer grid.release()
let mapped = try geocore.AttributeKernels.uv_project(grid, 1.0)
defer mapped.release()
let textured = try GeometrySet.of_mesh(mapped)
defer textured.release()
var file = Growing[u8]()
defer file.close()
try temporary(&file, "luce-usd-bench.prism")
var started = time.now()
let saved = try save_geometry(textured.value(), (str)file.view(), 1, 268435456)
row("Reference: save the grid with uv to .prism (bytes)", started, saved)
started = time.now()
let loaded = try adopt_set(try load_geometry((str)file.view()))
row("Reference: load that .prism (faces)", started, (usize)((try loaded.value().mesh()) else trap("mesh")).value().face_count())
loaded.release()
started = time.now()
let bytes = try read_file((str)file.view(), most_bytes)
row("Reference: read that file whole (bytes)", started, bytes.length)
free(bytes) in memory.heap
if arguments.length > 1: try usda_rows(arguments[1])
if arguments.length > 1: try usdc_rows(try strings.format(crate_path, f"{path.temp()}/luce-usd-bench.usdc"))
return 0
## usda layers: the grid mesh (837x837, points, indices and a faceVarying
## st) read from text into LayerData and written back.
func usda_rows(file_path: str) -> !:
let text = try read_file(file_path, most_bytes)
defer free(text) in memory.heap
var names = try Names()
defer names.close()
var layer = try LayerData(&names)
defer layer.close()
var started = time.now()
try read_usda(text, &layer)
row("usda: read the grid layer (bytes)", started, text.length)
var crate = Growing[u8]()
defer crate.close()
try write_usdc(&layer, &crate)
try files.write((c.str)try strings.format(crate_path, f"{path.temp()}/luce-usd-bench.usdc"), crate.view())
var out = Growing[u8]()
defer out.close()
started = time.now()
try write_usda(&layer, &out)
row("usda: write the grid layer (bytes)", started, out.count)
## usdc layers: the grid mesh read from a crate into LayerData.
func usdc_rows(file_path: str) -> !:
let data = try read_file(file_path, most_bytes)
defer free(data) in memory.heap
var names = try Names()
defer names.close()
var layer = try LayerData(&names)
defer layer.close()
var started = time.now()
try read_usdc(data, &layer)
row("usdc: read the grid layer (bytes)", started, data.length)
var warnings = Warnings()
defer warnings.close()
let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
started = time.now()
let set = try import_layer(&layer, &options, &warnings)
defer dispose_set(set)
let faces = (set_mesh(set) else trap("no mesh")).face_count()
row("import: the grid layer as a GeometrySet (faces)", started, (usize)faces)
var exported = try LayerData(&names)
defer exported.close()
let export_options = ExportOptions(split = true, precise = false)
started = time.now()
try export_set(set, &exported, "geo", &export_options, &warnings)
row("export: that GeometrySet as a layer (specs)", started, exported.spec_count())
var crate = Growing[u8]()
defer crate.close()
started = time.now()
try write_usdc(&exported, &crate)
row("export: that layer written as a crate (bytes)", started, crate.count)
var out = Growing[u8]()
defer out.close()
started = time.now()
try write_usdc(&layer, &out)
row("usdc: write the grid layer (bytes)", started, out.count)
var no_files: PackageFile[]
var package = Growing[u8]()
defer package.close()
started = time.now()
try write_usdz(&layer, "grid.usdc", no_files, &package)
row("usdz: write the grid layer as a package (bytes)", started, package.count)
var again = try LayerData(&names)
defer again.close()
started = time.now()
try read_usdz(package.view(), &again)
row("usdz: read that package (bytes)", started, package.count)