mirror of
https://github.com/bherbruck/solidcraft.git
synced 2026-11-07 04:12:15 +00:00
9e6e0b8df1
* wip(render): reconcile completed material and image pipeline source with 0.20 (#5493) Source-only integration reference for staged review, not a merge-ready aggregate. Donor: 0xlillith/solidcraft@aea20b0115. Upstream:858e5995d6. Retain original CPU/material contracts and explicit GPU donor provenance in docs/render-materials-lab.md (#2067, #2191, #2780, #3306). Preserve released format migrations and current navigation/startup/shell behavior. New saved records use pending descriptors. The physical-card descriptor follows the maintainer's material-engineering-cards naming request, with a legacy-lab format-29 regression. Replace raw new panel hosts with shared containers. Repository checks, complete workspace/all-target Clippy, and the independent physical/resource authoring checks pass for this source. The fresh full local CI invocation has reached test compilation and is not yet complete. Do not transfer donor native output or prior test totals to this integration. Generated review screenshots, video, package ZIP and machine logs remain at the immutable personal evidence pin. Uncommitted geometric displacement is backed up separately, not silently included. No kernel/vendor change, direct main write, release or independent contributor-branch rewrite. Co-authored-by: Christian Svensson <blue@cmd.nu> * test(cli): pin the Compact camera-sample admission diagnostic (#5493) The bounded request still refuses 1024x1024x64 samples before host initialization and writes no output. Update only the stale expected diagnostic to the existing Compact budget wording; production limits and code are unchanged. Exact-tree CLI regression suite: 14 passed, 0 failed. The full repository gate has been invoked separately and is not yet complete. * Normalize UI container violation paths across platforms Report repository-relative paths with forward slashes so diagnostics and the existing nested-package regression assertions agree on Windows and Unix. Keep the container detection rules unchanged. Validation: official ui-containers gate and all seven focused xtask tests passed; rustfmt and diff checks passed. Only xtask/dependencies were compiled; no application build or heavy tests. (cherry picked from commit44b58d8e10) * fix(render): preserve camera contracts and correct acceptance regressions (#5493) Preserve Design view.apply semantics with a separate strict Render camera command. Retain existing translations when English-only aliases collide; add material-axis entries and camera-specific pitch labels. Compare complete validated engineering cards and retain job handles before cancellation. Focused and native acceptance is pinned to treef8a423b21f. Full CI has been invoked separately and is not claimed green. Landing follows the render integration policy in #5493. * fix(render): resolve showcase licence and upstream resource hygiene (#5493) Record SychPL's explicit MIT OR Apache-2.0 grant, retaining source/model bytes and the Ubuntu lettering notice. Remove 80 absent attribution rows, personal fork records, dead delivery attributes and three fork/resource-only workflows. Existing xtask assets now performs bounded offline hash checks and rejects orphan render attribution; no parallel draft CI or package installation. Original authoring reproduction remains explicit. Scoped xtask tests, assets, Clippy, formatting and prepared SELENE validation pass; no application or renderer-wide result is inferred. * fix(render): align saved bounds and progressive sample resolution (#5493) Address review r4364211669/r4364211677/r4364211681/r4364211691/r4364211695. Persisted light/frame domains respect the existing 1e9 document bound; related LED/rectangle/direction cases use the same bound. Workstation primary resolve shares the 4096 admission cap and film counters use checked conversions. Exact candidate: 485 CPU render tests, 21 render document tests and strict scoped Clippy pass, including positive boundary round trips and 2048/4096 end-to-end checkpoints. No released format/migration edits, relaxed test limits or full-stack acceptance claim. * wip(render): isolate optional resources and harden sample handling (#5493) Review work in progress: opt-in encoded render library with reproducible runtime catalog exports; native-only robust dependency through the workspace; checked bloom slices; per-sample point-light diagnostics; independent area-light spatial/film keys. Resource-module binaries in both configurations, generation, Cargo production feature graphs and attribution checks pass. Full engine/render regression and combined acceptance are pending: do not merge this donor as a constituent or treat scoped resource proof as runtime acceptance. * fix(gpu): preserve donor allocations through the scope-safety repair (#5722) Port the native-validated #5723 scope-cleanup correction into the experimental donor, retaining its adaptive, volume, aging, photographic and bloom allocations. Same reverse-drain helper and regression cases; the donor combination still needs its own compile/test gate and is not covered retroactively by render-base results. * claim: #1369 * Reject unknown pending concepts after format folding (#1369) * test(render): close bounded sampling and bloom review regressions (#5493) Validate the optional-resource/lighting/dependency donor at its exact tree. Required Vulkan GPU 127/0/3; CPU renderer 487/0; engine Render 72/0; document render 21/0; lean library 7/0; strict scoped Clippy, assets/layers/lists/containers and lean-engine WASM pass. Preserve original failed controller/typing attempts. This is the draft donor/disposition source, not a replacement for constituent render PRs or final main acceptance. * wip(render): preserve stable aging target validation candidate Exact staged source from prior review work on donor3b892eab5e. This is a recovery/review checkpoint, NOT accepted delivery. An actual command-path regression currently fails because the selected executor refuses ordered surface aging. No assertion, capability or admission limit is weakened. Native/UI/full combination acceptance remains pending. Saved/runtime split adds one pending descriptor and retains original file scope; no main/render branch is changed. * test(render): preserve numeric setup intent at the admitted coordinate bound Follow up jyviko review on #5494 without weakening the corrected saved-coordinate contract. The positive clicked fixture uses 1e9 instead of inadmissible 2e9; explicit out-of-range cases still refuse, and the full saved setup round-trips through JSON and the native container. Exact-source Render workspace 95/0 and strict all-target UI Clippy pass. This does not incorporate the separately failing stable-aging candidate or claim full current-render acceptance. * test(render): use the real GPU executor for saved aging Move successful native cold-reopen Check/Start coverage from the CPU-backed protocol fixture into the production GPU executor tests. Verify capture-index changes preserve the stable target and that enabling aging changes both PNG and raw EXR output. Retain explicit, allocation-free refusal coverage for executors without aging support. Production capabilities and admission checks are unchanged. * fix(render): persist stable identities for targeted aging (#5493) Separate saved RenderBodyTarget identities from captured mesh ordinals. Reject legacy ambiguous targets, resolve Check/Start after visibility changes and preserve native reopening, copies and undo. Unsupported executors still refuse; real AMD Vulkan regression completes saved aging after reordered capture. Source tree6b84c8adpasses document 23, engine Render 75, GPU effects 5 and Render UI 95 with no failures; scoped strict Clippy, WASM and repository gates pass. Fresh CLI/native reopen and clean/aged GPU exports pass with identical physical totals and inspected actual images. Whole current-render integration is separate. * fix(render): reconcile combined CPU/GPU contracts on current upstream Unify captured resources/status and CPU-vs-GPU request schemas after the real two-parent integration; remove obsolete duplicate Vulkan selector and retain combined platform-filter regression. Deduplicate only identical or superseded test definitions, retaining the stronger card metadata and reserved-executor assertions. No assertions, admission limits or feature refusals are weakened. All targets of render dependencies, GPU, engine, UI, CLI and native app pass cargo check with raytrace-cpu and bundled-render-library at treec70d00d1. Native executable tests and full current-source acceptance remain pending. This is a private integration rehearsal for subsequent constituent PRs, not an aggregate official render/main merge. * test(render): retain one complete host lifetime regression set Deduplicate byte-identical DropProbe/task-lifetime tests and retain the adapted environment/texture ownership regressions with every assertion and complete memory estimate. Keep the registered-geometry admission regression once; only struct field order differed. Replace a one-element diagnostic loop with its equivalent block, preserving all wrap/width assertions. Test-only integration repair. Formatting and diff checks pass; refreshed executable validation is required. No production behavior, limits, assertions or ignores changed. * fix(doc): recognize the proven format-29 card descriptor alias Normalize only the historical format-29 physical-material-cards spelling, and only when material-engineering-cards is registered. Admission and migration de-duplication use the same identity. Unknown concepts, wrong versions and incapable readers still refuse before mutation. Six controlled-history Rust regressions and actual CLI migration/no-clobber acceptance pass. No version, released migration or CI changes. Refs #5793, #1369, #5493. * test(render): preserve grounding refusal before explicit instance moves Update the two Render binding fixtures for upstream first-occurrence grounding. Verify grounded movement refuses without changing document/history, then explicitly unground through the public command before the intended move. Retain every target, resource, copy and persistence assertion. Refs #5493. --------- Co-authored-by: Christian Svensson <blue@cmd.nu> Co-authored-by: Victor Santos Oliveira <56417388+Vittuu@users.noreply.github.com> Co-authored-by: cfaulkingham <colin.faulkingham@gmail.com>
590 lines
27 KiB
Rust
590 lines
27 KiB
Rust
//! Native conformance gate for the authored starter pack, not an application importer.
|
|
//! Reads one bounded resource at a time and never changes a document or source asset.
|
|
#![forbid(unsafe_code)]
|
|
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo)]
|
|
|
|
use image::ImageDecoder;
|
|
use serde::{Deserialize, Serialize};
|
|
use sha2::{Digest, Sha256};
|
|
use solidcraft_geom::Vec3;
|
|
use solidcraft_render::{
|
|
lighting::{DecodedHdr, HdrDecodeTask, HdrSource, HdrTaskPoll},
|
|
surface::unit_direction,
|
|
texture_input::{
|
|
MapRole, TextureSource,
|
|
normalization::{ColorConversion, NormalizationOptions, NormalizationSource, UnprofiledColor},
|
|
},
|
|
texture_sampling::{
|
|
TextureView,
|
|
mips::{MAX_MIP_TAIL_BYTES, MipChain},
|
|
},
|
|
};
|
|
use std::{
|
|
error::Error,
|
|
f64::consts::PI,
|
|
fs::File,
|
|
io::{self, Read},
|
|
path::{Path, PathBuf},
|
|
process::ExitCode,
|
|
};
|
|
|
|
const PACK: &str = "assets/render/starter";
|
|
const LIMIT: usize = 16 * 1024 * 1024;
|
|
const NORMAL_CONVENTION: &str = "OpenGL +Y, +U right, +V image-up; renormalize after sampling";
|
|
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
|
|
|
fn require(ok: bool, message: impl Into<String>) -> Result<()> {
|
|
if ok { Ok(()) } else { Err(io::Error::new(io::ErrorKind::InvalidData, message.into()).into()) }
|
|
}
|
|
fn sha(bytes: &[u8]) -> String {
|
|
format!("{:x}", Sha256::digest(bytes))
|
|
}
|
|
|
|
/// Only repository-relative paths with portable, unambiguous components are admitted.
|
|
fn relative_path(path: &str) -> Result<PathBuf> {
|
|
require(!path.is_empty() && path.len() <= 240, "invalid resource path length")?;
|
|
require(path.bytes().all(|c| c.is_ascii_alphanumeric() || b"/_.-".contains(&c)), "nonportable resource path")?;
|
|
require(path.split('/').all(|c| !c.is_empty() && c != "." && c != ".."), "resource path traversal")?;
|
|
Ok(PathBuf::from(path))
|
|
}
|
|
fn read_bounded(root: &Path, path: &str, limit: usize) -> Result<Vec<u8>> {
|
|
let relative = relative_path(path)?;
|
|
let root = root.canonicalize()?;
|
|
let mut target = root.clone();
|
|
for component in relative.components() {
|
|
target.push(component.as_os_str());
|
|
require(!target.symlink_metadata()?.file_type().is_symlink(), "resource symlink refused")?;
|
|
}
|
|
require(target.canonicalize()?.starts_with(&root), "resource resolves outside checkout")?;
|
|
let file = File::open(target)?;
|
|
let meta = file.metadata()?;
|
|
require(meta.is_file() && meta.len() > 0 && meta.len() <= limit as u64, "resource byte budget exceeded")?;
|
|
let mut bytes = Vec::new();
|
|
bytes.try_reserve_exact(meta.len() as usize)?;
|
|
file.take(limit as u64 + 1).read_to_end(&mut bytes)?;
|
|
require(bytes.len() == meta.len() as usize && bytes.len() <= limit, "resource changed size while reading")?;
|
|
Ok(bytes)
|
|
}
|
|
fn verify_bytes(bytes: &[u8], length: usize, digest: &str) -> Result<()> {
|
|
require(digest.len() == 64 && digest.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)), "invalid SHA-256 receipt")?;
|
|
require(bytes.len() == length && sha(bytes) == digest, "resource byte/hash receipt mismatch")
|
|
}
|
|
fn pinned_source(root: &Path, path: &str, digest: &str) -> Result<()> {
|
|
let bytes = read_bounded(root, path, 4 * 1024 * 1024)?;
|
|
verify_bytes(&bytes, bytes.len(), digest)
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Surfaces {
|
|
schema: String,
|
|
generator_version: String,
|
|
license: String,
|
|
source_path: String,
|
|
source_sha256: String,
|
|
generator_sha256: String,
|
|
items: Vec<Surface>,
|
|
}
|
|
#[derive(Deserialize)]
|
|
struct Surface {
|
|
id: String,
|
|
runtime_status: String,
|
|
normal_convention: String,
|
|
tile_mm: [f64; 2],
|
|
maps: Maps,
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Maps {
|
|
base_color: Map,
|
|
normal_open_gl: Map,
|
|
roughness: Map,
|
|
metallic: Map,
|
|
height: Map,
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Map {
|
|
path: String,
|
|
sha256: String,
|
|
decoded_sha256: String,
|
|
size_bytes: usize,
|
|
width: u32,
|
|
height: u32,
|
|
channels: usize,
|
|
bit_depth: u8,
|
|
encoding: String,
|
|
minimum: u16,
|
|
maximum: u16,
|
|
consumer: String,
|
|
}
|
|
#[derive(Deserialize)]
|
|
struct Environments {
|
|
schema: String,
|
|
version: String,
|
|
license: String,
|
|
runtime_ready: bool,
|
|
recipe_source: String,
|
|
recipe_sha256: String,
|
|
generator: String,
|
|
generator_sha256: String,
|
|
items: Vec<Environment>,
|
|
}
|
|
#[derive(Deserialize)]
|
|
struct Environment {
|
|
id: String,
|
|
path: String,
|
|
sha256: String,
|
|
bytes: usize,
|
|
dimensions: [u32; 2],
|
|
runtime_ready: bool,
|
|
role: String,
|
|
color_space: String,
|
|
transfer_function: String,
|
|
header_orientation: String,
|
|
primaries_xy: [f64; 8],
|
|
exposure: f64,
|
|
color_correction: [f64; 3],
|
|
solar_disk: bool,
|
|
energy_owner: String,
|
|
statistics: Statistics,
|
|
}
|
|
#[derive(Deserialize)]
|
|
struct Statistics {
|
|
minimum_channel: f64,
|
|
maximum_channel: f64,
|
|
solid_angle_weighted_mean_rgb: [f64; 3],
|
|
hemisphere_cosine_integrals_rgb: std::collections::BTreeMap<String, [f64; 3]>,
|
|
}
|
|
#[derive(Serialize)]
|
|
struct TextureReceipt {
|
|
surface: String,
|
|
role: String,
|
|
source_sha256: String,
|
|
normalized_sha256: String,
|
|
mip_levels: usize,
|
|
mip_tail_bytes: usize,
|
|
}
|
|
#[derive(Serialize)]
|
|
struct HdrReceipt {
|
|
environment: String,
|
|
source_sha256: String,
|
|
decoded_f32_le_sha256: String,
|
|
pixels: usize,
|
|
}
|
|
#[derive(Default, Serialize)]
|
|
struct Report {
|
|
schema: &'static str,
|
|
runtime_ready: bool,
|
|
surfaces: usize,
|
|
texture_inputs: usize,
|
|
height_sources_verified_not_bound: usize,
|
|
hdr_inputs: usize,
|
|
mip_levels: usize,
|
|
encoded_resource_bytes: usize,
|
|
textures: Vec<TextureReceipt>,
|
|
environments: Vec<HdrReceipt>,
|
|
}
|
|
|
|
fn map_bytes(root: &Path, surface: &Surface, name: &str, map: &Map, bits: u8, channels: usize) -> Result<Vec<u8>> {
|
|
require(map.path == format!("{PACK}/materials/exports/{}/{name}.png", surface.id), "map path/role mismatch")?;
|
|
require((map.width, map.height, map.bit_depth, map.channels) == (512, 512, bits, channels), "map shape/precision mismatch")?;
|
|
require(map.minimum <= map.maximum && (bits == 16 || map.maximum <= 255), "invalid map range")?;
|
|
let encoding = if name == "base_color" { "srgb" } else { "linear_data" };
|
|
require(map.encoding == encoding, "map color/data role mismatch")?;
|
|
let consumer =
|
|
if name == "height" { "authoring_only_requires_explicit_height_adapter" } else { "bounded_texture_input_compatible_not_runtime_bound" };
|
|
require(map.consumer == consumer, "unsupported map availability")?;
|
|
let bytes = read_bounded(root, &map.path, LIMIT)?;
|
|
verify_bytes(&bytes, map.size_bytes, &map.sha256)?;
|
|
Ok(bytes)
|
|
}
|
|
fn srgb_reference(encoded: u8) -> f64 {
|
|
let value = f64::from(encoded) / 255.0;
|
|
if value <= 0.04045 { value / 12.92 } else { ((value + 0.055) / 1.055).powf(2.4) }
|
|
}
|
|
/// Native sampling uses image-down V; authored texture/tangent coordinates use V-up.
|
|
fn sample_authored(view: TextureView<'_>, uv: [f64; 2]) -> Result<[f64; 3]> {
|
|
Ok(view.sample_repeat_opaque([uv[0], 1.0 - uv[1]])?)
|
|
}
|
|
fn normal(rgb: [f64; 3]) -> Result<Vec3> {
|
|
Ok(unit_direction(Vec3::new(2.0 * rgb[0] - 1.0, 2.0 * rgb[1] - 1.0, 2.0 * rgb[2] - 1.0))?)
|
|
}
|
|
fn check_texture(root: &Path, surface: &Surface, name: &str, role: MapRole, map: &Map) -> Result<TextureReceipt> {
|
|
let channels = if matches!(role, MapRole::BaseColor | MapRole::NormalOpenGl) { 3 } else { 1 };
|
|
let bytes = map_bytes(root, surface, name, map, 8, channels)?;
|
|
let strict = TextureSource::new(bytes.clone(), role)?.decode(|| false)?;
|
|
require(
|
|
strict.info().role == role && (strict.info().width, strict.info().height) == (map.width, map.height),
|
|
"native texture metadata mismatch",
|
|
)?;
|
|
let mut hash = Sha256::new();
|
|
let (mut minimum, mut maximum) = (u16::MAX, 0u16);
|
|
for pixel in strict.rgba().as_chunks::<4>().0.iter() {
|
|
let [r, g, b, a] = pixel;
|
|
require(*a == 255, "opaque source unexpectedly has alpha")?;
|
|
if channels == 1 {
|
|
require(r == g && g == b, "scalar source gained color")?;
|
|
hash.update([*r]);
|
|
} else {
|
|
hash.update([*r, *g, *b]);
|
|
}
|
|
for value in [*r, *g, *b] {
|
|
minimum = minimum.min(u16::from(value));
|
|
maximum = maximum.max(u16::from(value));
|
|
}
|
|
}
|
|
require(format!("{:x}", hash.finalize()) == map.decoded_sha256, "native decoded pixels differ from authoring receipt")?;
|
|
require((minimum, maximum) == (map.minimum, map.maximum), "native channel range mismatch")?;
|
|
let options = NormalizationOptions { unprofiled_color: UnprofiledColor::DeclareSrgbPrimaries, ..Default::default() };
|
|
let normalized = NormalizationSource::new(bytes, role, options)?.decode(|| false)?;
|
|
let receipt = normalized.receipt();
|
|
require(receipt.source_sha256 == map.sha256 && receipt.output_dimensions == [map.width, map.height], "normalization identity mismatch")?;
|
|
require(receipt.clamped_rgb_channels == 0 && !receipt.orientation_applied && receipt.source_channel_bits == 8, "unexpected lossy normalization")?;
|
|
let expected_conversion = if role == MapRole::BaseColor { ColorConversion::DeclaredSrgb } else { ColorConversion::RawData };
|
|
require(receipt.conversion == expected_conversion, "unexpected color conversion")?;
|
|
require(normalized.rgba().len() == strict.rgba().len(), "normalization changed sample count")?;
|
|
let mut mean = [0.0; 3];
|
|
for (raw, actual) in strict.rgba().as_chunks::<4>().0.iter().zip(normalized.rgba().as_chunks::<4>().0.iter()) {
|
|
for (channel, (&raw, &actual)) in raw.iter().zip(actual).enumerate() {
|
|
let expected = if channel < 3 && role == MapRole::BaseColor { srgb_reference(raw) } else { f64::from(raw) / 255.0 };
|
|
require((f64::from(actual) - expected).abs() <= 6e-8, "native linear/data sample mismatch")?;
|
|
if let Some(total) = mean.get_mut(channel) {
|
|
*total += f64::from(actual);
|
|
}
|
|
}
|
|
}
|
|
let count = f64::from(map.width) * f64::from(map.height);
|
|
for value in &mut mean {
|
|
*value /= count;
|
|
}
|
|
let view = TextureView::new(normalized.rgba(), [map.width, map.height])?;
|
|
let chain = MipChain::prepare(view, MAX_MIP_TAIL_BYTES, || false)?;
|
|
require(chain.level_count() == 10 && chain.dimensions(9) == Some([1, 1]), "incomplete native mip chain")?;
|
|
let final_mean = chain.sample_repeat_lod_opaque([0.5, 0.5], 9.0)?;
|
|
for (actual, expected) in final_mean.into_iter().zip(mean) {
|
|
require((actual - expected).abs() < 2e-7, "native final mip changed channel mean")?;
|
|
}
|
|
for uv in [[0.125, 0.375], [-0.125, 1.25], [0.0, 1.0], [0.5, 0.5]] {
|
|
let native = sample_authored(view, uv)?;
|
|
for lod in [0.0, 2.5, 9.0] {
|
|
let a = chain.sample_repeat_lod_opaque([uv[0], 1.0 - uv[1]], lod)?;
|
|
let b = chain.sample_repeat_lod_opaque([uv[0] + 1.0, -uv[1]], lod)?;
|
|
for (a, b) in a.into_iter().zip(b) {
|
|
require((a - b).abs() < 1e-12, "native texture repeat mismatch")?;
|
|
}
|
|
if lod == 0.0 {
|
|
require(a == native, "level-zero authored V mapping mismatch")?;
|
|
}
|
|
if role == MapRole::NormalOpenGl {
|
|
let n = normal(a)?;
|
|
require(n.z > 0.0 && (n.len() - 1.0).abs() < 1e-12, "invalid filtered tangent normal")?;
|
|
}
|
|
}
|
|
}
|
|
Ok(TextureReceipt {
|
|
surface: surface.id.clone(),
|
|
role: name.into(),
|
|
source_sha256: map.sha256.clone(),
|
|
normalized_sha256: receipt.normalized_sha256.clone(),
|
|
mip_levels: chain.level_count(),
|
|
mip_tail_bytes: chain.tail_bytes(),
|
|
})
|
|
}
|
|
fn check_height(root: &Path, surface: &Surface) -> Result<()> {
|
|
let map = &surface.maps.height;
|
|
let bytes = map_bytes(root, surface, "height", map, 16, 1)?;
|
|
// Safety probe only: no height role exists, so never relabel a height as roughness.
|
|
match TextureSource::new(bytes.clone(), MapRole::Roughness) {
|
|
Err(error) if error.0.contains("8-bit") => {}
|
|
_ => return Err("16-bit source unexpectedly passed the strict eight-bit route".into()),
|
|
}
|
|
let mut decoder = image::codecs::png::PngDecoder::new(io::Cursor::new(bytes))?;
|
|
let mut limits = image::Limits::default();
|
|
limits.max_image_width = Some(512);
|
|
limits.max_image_height = Some(512);
|
|
limits.max_alloc = Some(LIMIT as u64);
|
|
decoder.set_limits(limits)?;
|
|
require(decoder.dimensions() == (512, 512) && decoder.color_type() == image::ColorType::L16, "height is not grayscale 16-bit")?;
|
|
let image = image::DynamicImage::from_decoder(decoder)?;
|
|
let pixels = image.as_luma16().ok_or("height precision changed")?;
|
|
let mut hash = Sha256::new();
|
|
let (mut low, mut high) = (u16::MAX, 0u16);
|
|
for sample in pixels.as_raw() {
|
|
hash.update(sample.to_le_bytes());
|
|
low = low.min(*sample);
|
|
high = high.max(*sample);
|
|
}
|
|
require(
|
|
format!("{:x}", hash.finalize()) == map.decoded_sha256 && (low, high) == (map.minimum, map.maximum),
|
|
"height native 16-bit receipt mismatch",
|
|
)
|
|
}
|
|
fn decode_hdr(source: HdrSource) -> Result<DecodedHdr> {
|
|
let mut task = HdrDecodeTask::new(source)?;
|
|
let mut previous = 0;
|
|
for _ in 0..1024 {
|
|
match task.step(16384, || false)? {
|
|
HdrTaskPoll::Complete(image) => return Ok(image),
|
|
HdrTaskPoll::Pending(progress) => {
|
|
require(progress.decoded_pixels >= previous && progress.decoded_pixels <= progress.total_pixels, "nonmonotonic HDR progress")?;
|
|
previous = progress.decoded_pixels;
|
|
}
|
|
}
|
|
}
|
|
Err("native HDR task exceeded bounded poll count".into())
|
|
}
|
|
fn check_hdr(root: &Path, entry: &Environment) -> Result<HdrReceipt> {
|
|
require(entry.path.starts_with("exports/") && entry.path.ends_with(".hdr"), "HDR export path mismatch")?;
|
|
require(entry.dimensions == [1024, 512] && !entry.runtime_ready, "unsupported HDR shape/availability")?;
|
|
require(
|
|
entry.role == "environment_radiance" && entry.color_space == "linear_srgb" && entry.transfer_function == "linear",
|
|
"HDR radiance semantics mismatch",
|
|
)?;
|
|
require(
|
|
entry.header_orientation == "-Y +X"
|
|
&& !entry.solar_disk
|
|
&& entry.energy_owner == if entry.id == "HDR-024" { "none" } else { "environment_map" },
|
|
"HDR orientation/energy ownership mismatch",
|
|
)?;
|
|
let path = format!("{PACK}/environments/{}", entry.path);
|
|
let bytes = read_bounded(root, &path, LIMIT)?;
|
|
verify_bytes(&bytes, entry.bytes, &entry.sha256)?;
|
|
let source = HdrSource::new(bytes)?;
|
|
require([source.info().width, source.info().height] == entry.dimensions, "native HDR dimensions mismatch")?;
|
|
require(
|
|
source.info().exposure == entry.exposure
|
|
&& source.info().color_correction == entry.color_correction
|
|
&& source.info().primaries == Some(entry.primaries_xy),
|
|
"native HDR color metadata mismatch",
|
|
)?;
|
|
let image = decode_hdr(source)?;
|
|
require(image.pixels().len() == 1024 * 512, "incomplete native HDR pixels")?;
|
|
let mut hash = Sha256::new();
|
|
let (mut low, mut high) = (f64::INFINITY, 0.0_f64);
|
|
let mut mean = [0.0; 3];
|
|
let mut integrals = [[0.0; 3]; 6];
|
|
let width = entry.dimensions[0] as usize;
|
|
let height = entry.dimensions[1] as usize;
|
|
let angles: Vec<_> = (0..width).map(|x| (2.0 * PI * ((x as f64 + 0.5) / width as f64 - 0.5)).sin_cos()).collect();
|
|
for (y, row) in image.pixels().chunks_exact(width).enumerate() {
|
|
let theta = PI * (y as f64 + 0.5) / height as f64;
|
|
let (st, ct) = theta.sin_cos();
|
|
let omega = 2.0 * PI / width as f64 * ((PI * y as f64 / height as f64).cos() - (PI * (y + 1) as f64 / height as f64).cos());
|
|
for (pixel, (sp, cp)) in row.iter().zip(&angles) {
|
|
let direction = [st * cp, -st * cp, st * sp, -st * sp, ct, -ct];
|
|
for (channel, value) in pixel.iter().enumerate() {
|
|
require(value.is_finite() && (0.0..=1e8).contains(value), "nonfinite/out-of-range native HDR radiance")?;
|
|
hash.update(value.to_le_bytes());
|
|
let value = f64::from(*value);
|
|
low = low.min(value);
|
|
high = high.max(value);
|
|
if let Some(total) = mean.get_mut(channel) {
|
|
*total += value * omega / (4.0 * PI);
|
|
}
|
|
for (integral, cosine) in integrals.iter_mut().zip(direction) {
|
|
if let Some(total) = integral.get_mut(channel) {
|
|
*total += value * omega * cosine.max(0.0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
require(low == entry.statistics.minimum_channel && high == entry.statistics.maximum_channel, "native HDR channel range mismatch")?;
|
|
if entry.id == "HDR-024" {
|
|
require(low == 0.0 && high == 0.0, "vacuum must contribute exactly zero radiance")?;
|
|
}
|
|
if entry.id == "HDR-005" {
|
|
require(low == 1.0 && high == 1.0, "uniform diagnostic must preserve unit radiance")?;
|
|
}
|
|
for (a, b) in mean.into_iter().zip(entry.statistics.solid_angle_weighted_mean_rgb) {
|
|
require((a - b).abs() < 6e-8, "native HDR solid-angle mean mismatch")?;
|
|
}
|
|
require(entry.statistics.hemisphere_cosine_integrals_rgb.len() == 6, "six directional HDR receipts required")?;
|
|
for (axis, actual) in ["+X", "-X", "+Y", "-Y", "+Z", "-Z"].into_iter().zip(integrals) {
|
|
let expected = entry.statistics.hemisphere_cosine_integrals_rgb.get(axis).ok_or("missing HDR direction receipt")?;
|
|
for (a, b) in actual.into_iter().zip(expected) {
|
|
require((a - b).abs() < 6e-8, format!("native HDR direction mismatch: {} {axis}", entry.id))?;
|
|
}
|
|
}
|
|
Ok(HdrReceipt {
|
|
environment: entry.id.clone(),
|
|
source_sha256: entry.sha256.clone(),
|
|
decoded_f32_le_sha256: format!("{:x}", hash.finalize()),
|
|
pixels: image.pixels().len(),
|
|
})
|
|
}
|
|
fn validate(root: &Path) -> Result<Report> {
|
|
let mut report = Report { schema: "solidcraft-native-starter-check/1", ..Default::default() };
|
|
let surfaces: Surfaces = serde_json::from_slice(&read_bounded(root, &format!("{PACK}/materials/manifest.json"), 4 * 1024 * 1024)?)?;
|
|
require(
|
|
surfaces.schema == "solidcraft-authored-surface-manifest/1"
|
|
&& surfaces.generator_version == "1.0.0"
|
|
&& surfaces.license == "MIT OR Apache-2.0",
|
|
"unknown surface manifest",
|
|
)?;
|
|
require(
|
|
surfaces.items.len() == 48 && surfaces.source_path == format!("{PACK}/materials/source/recipes.json"),
|
|
"surface inventory/source mismatch",
|
|
)?;
|
|
pinned_source(root, &surfaces.source_path, &surfaces.source_sha256)?;
|
|
pinned_source(root, &format!("{PACK}/tools/materials.py"), &surfaces.generator_sha256)?;
|
|
for (index, surface) in surfaces.items.iter().enumerate() {
|
|
require(surface.id == format!("SURF-{:03}", index + 1) && surface.runtime_status == "unbound", "surface identity/availability mismatch")?;
|
|
require(
|
|
surface.normal_convention == NORMAL_CONVENTION && surface.tile_mm.iter().all(|x| x.is_finite() && *x > 0.0 && *x <= 10000.0),
|
|
"surface basis/scale mismatch",
|
|
)?;
|
|
for (name, role, map) in [
|
|
("base_color", MapRole::BaseColor, &surface.maps.base_color),
|
|
("normal_open_gl", MapRole::NormalOpenGl, &surface.maps.normal_open_gl),
|
|
("roughness", MapRole::Roughness, &surface.maps.roughness),
|
|
("metallic", MapRole::Metallic, &surface.maps.metallic),
|
|
] {
|
|
let receipt = check_texture(root, surface, name, role, map)?;
|
|
report.texture_inputs += 1;
|
|
report.encoded_resource_bytes += map.size_bytes;
|
|
report.mip_levels += receipt.mip_levels;
|
|
report.textures.push(receipt);
|
|
}
|
|
check_height(root, surface)?;
|
|
report.encoded_resource_bytes += surface.maps.height.size_bytes;
|
|
report.height_sources_verified_not_bound += 1;
|
|
report.surfaces += 1;
|
|
if (index + 1) % 12 == 0 {
|
|
eprintln!("Native material admission: {}/48", index + 1);
|
|
}
|
|
}
|
|
let environments: Environments = serde_json::from_slice(&read_bounded(root, &format!("{PACK}/environments/manifest.json"), 4 * 1024 * 1024)?)?;
|
|
require(
|
|
environments.schema == "solidcraft.render-environment-manifest/1"
|
|
&& environments.version == "1.0.0"
|
|
&& environments.license == "MIT OR Apache-2.0"
|
|
&& !environments.runtime_ready,
|
|
"unknown HDR manifest",
|
|
)?;
|
|
require(
|
|
environments.items.len() == 24 && environments.recipe_source == "source/recipes.json" && environments.generator == "../tools/environments.py",
|
|
"HDR inventory/source mismatch",
|
|
)?;
|
|
pinned_source(root, &format!("{PACK}/environments/source/recipes.json"), &environments.recipe_sha256)?;
|
|
pinned_source(root, &format!("{PACK}/tools/environments.py"), &environments.generator_sha256)?;
|
|
for (index, entry) in environments.items.iter().enumerate() {
|
|
require(entry.id == format!("HDR-{:03}", index + 1), "HDR identity mismatch")?;
|
|
report.environments.push(check_hdr(root, entry)?);
|
|
report.hdr_inputs += 1;
|
|
report.encoded_resource_bytes += entry.bytes;
|
|
}
|
|
Ok(report)
|
|
}
|
|
fn root() -> Result<PathBuf> {
|
|
Ok(Path::new(env!("CARGO_MANIFEST_DIR")).parent().and_then(Path::parent).ok_or("missing repository root")?.to_path_buf())
|
|
}
|
|
fn run() -> Result<()> {
|
|
require(std::env::args_os().count() == 1, "usage: cargo run --locked --profile ci -p solidcraft-render --example validate_starter")?;
|
|
let report = validate(&root()?)?;
|
|
println!("{}", serde_json::to_string_pretty(&report)?);
|
|
Ok(())
|
|
}
|
|
fn main() -> ExitCode {
|
|
match run() {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(error) => {
|
|
eprintln!("Native starter validation failed: {error}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use solidcraft_render::{lighting::HdrErrorCode, texture_sampling::mips::MipError};
|
|
|
|
#[test]
|
|
fn portable_paths_reject_escapes_and_ambiguous_windows_forms() {
|
|
for path in ["", "/tmp/x", "../x", "a/../x", "a//x", "a/./x", "C:/x", "C:x", "a\\x", "a/x:stream", "a/", "x\0y"] {
|
|
assert!(relative_path(path).is_err(), "{path:?}");
|
|
}
|
|
assert!(relative_path("assets/render/starter/materials/exports/SURF-001/base_color.png").is_ok());
|
|
}
|
|
#[test]
|
|
fn receipts_reject_corruption_wrong_size_and_malformed_hashes() -> Result<()> {
|
|
let bytes = b"original";
|
|
verify_bytes(bytes, bytes.len(), &sha(bytes))?;
|
|
assert!(verify_bytes(b"modified", bytes.len(), &sha(bytes)).is_err());
|
|
assert!(verify_bytes(bytes, bytes.len() + 1, &sha(bytes)).is_err());
|
|
assert!(verify_bytes(bytes, bytes.len(), &"F".repeat(64)).is_err());
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn bounded_reads_refuse_small_budgets() -> Result<()> {
|
|
assert!(read_bounded(&root()?, &format!("{PACK}/pack.json"), 2).is_err());
|
|
assert!(read_bounded(&root()?, "../Cargo.toml", LIMIT).is_err());
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn authored_v_up_is_mapped_once_and_green_normal_sign_is_preserved() -> Result<()> {
|
|
let pixels = [0.5, 0.75, 1.0, 1.0, 0.5, 0.75, 1.0, 1.0, 0.5, 0.25, 1.0, 1.0, 0.5, 0.25, 1.0, 1.0];
|
|
let view = TextureView::new(&pixels, [2, 2])?;
|
|
let top = normal(sample_authored(view, [0.25, 0.75])?)?;
|
|
let bottom = normal(sample_authored(view, [0.25, 0.25])?)?;
|
|
assert!(top.y > 0.0 && bottom.y < 0.0 && top.z > 0.0 && bottom.z > 0.0);
|
|
assert!((top.len() - 1.0).abs() < 1e-12);
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn srgb_reference_anchors_distinguish_color_from_data() {
|
|
assert_eq!(srgb_reference(0), 0.0);
|
|
assert_eq!(srgb_reference(255), 1.0);
|
|
assert!((srgb_reference(128) - 0.21586050011389926).abs() < 1e-14);
|
|
assert!((srgb_reference(10) - (10.0 / 255.0) / 12.92).abs() < 1e-15);
|
|
}
|
|
#[test]
|
|
fn texture_and_normalization_cancellation_keep_source_reusable() -> Result<()> {
|
|
let bytes = read_bounded(&root()?, &format!("{PACK}/materials/exports/SURF-001/base_color.png"), LIMIT)?;
|
|
let strict = TextureSource::new(bytes.clone(), MapRole::BaseColor)?;
|
|
assert!(strict.decode(|| true).is_err());
|
|
assert_eq!(strict.decode(|| false)?.info().width, 512);
|
|
let options = NormalizationOptions { unprofiled_color: UnprofiledColor::DeclareSrgbPrimaries, ..Default::default() };
|
|
let source = NormalizationSource::new(bytes, MapRole::BaseColor, options)?;
|
|
assert!(source.decode(|| true).is_err());
|
|
assert_eq!(source.decode(|| false)?.receipt().output_dimensions, [512, 512]);
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn mip_cancellation_does_not_return_partial_chain() -> Result<()> {
|
|
let pixels = [0.5; 16];
|
|
let view = TextureView::new(&pixels, [2, 2])?;
|
|
assert!(matches!(MipChain::prepare(view, MAX_MIP_TAIL_BYTES, || true), Err(MipError::Cancelled)));
|
|
assert_eq!(MipChain::prepare(view, MAX_MIP_TAIL_BYTES, || false)?.level_count(), 2);
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn hdr_cancellation_consumes_only_its_task_not_the_source() -> Result<()> {
|
|
let bytes = read_bounded(&root()?, &format!("{PACK}/environments/exports/vacuum-black.hdr"), LIMIT)?;
|
|
let source = HdrSource::new(bytes)?;
|
|
let mut task = HdrDecodeTask::new(source.clone())?;
|
|
assert!(matches!(task.step(64, || true), Err(e) if e.code == HdrErrorCode::Cancelled));
|
|
assert!(matches!(task.step(64, || false), Err(e) if e.code == HdrErrorCode::Consumed));
|
|
assert!(decode_hdr(source)?.pixels().iter().all(|p| *p == [0.0; 3]));
|
|
Ok(())
|
|
}
|
|
#[test]
|
|
fn map_contract_rejects_duplicate_known_fields_and_unknown_maps() -> Result<()> {
|
|
let bytes = read_bounded(&root()?, &format!("{PACK}/materials/manifest.json"), 4 * 1024 * 1024)?;
|
|
let source: serde_json::Value = serde_json::from_slice(&bytes)?;
|
|
let maps = source.get("items").and_then(|v| v.get(0)).and_then(|v| v.get("maps")).ok_or("missing fixture maps")?;
|
|
let valid = serde_json::to_string(maps)?;
|
|
assert!(serde_json::from_str::<Maps>(&valid).is_ok());
|
|
let prefix = valid.strip_suffix('}').ok_or("incomplete fixture")?;
|
|
let base = serde_json::to_string(maps.get("base_color").ok_or("missing base color")?)?;
|
|
let duplicate = format!("{prefix},\"base_color\":{base}}}");
|
|
let error = serde_json::from_str::<Maps>(&duplicate).err().ok_or("duplicate accepted")?;
|
|
assert!(error.to_string().contains("duplicate field"));
|
|
let extra = format!("{prefix},\"emission\":null}}");
|
|
let error = serde_json::from_str::<Maps>(&extra).err().ok_or("extra role accepted")?;
|
|
assert!(error.to_string().contains("unknown field"));
|
|
Ok(())
|
|
}
|
|
}
|