From 92f6bc99b2e9ed57027ae0033d721408009314d8 Mon Sep 17 00:00:00 2001 From: Simon Stucki Date: Sun, 18 Aug 2024 11:43:17 +0200 Subject: [PATCH] Extend material validation --- strahl-lib/src/path-tracer.ts | 8 +++----- strahl-lib/src/prepare-geometry.ts | 29 ++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/strahl-lib/src/path-tracer.ts b/strahl-lib/src/path-tracer.ts index dfae3fb..8175d4c 100644 --- a/strahl-lib/src/path-tracer.ts +++ b/strahl-lib/src/path-tracer.ts @@ -381,11 +381,10 @@ async function runPathTracer( // Prepare Object Definitions const OBJECT_DEFINITION_SIZE_PER_ENTRY = Uint32Array.BYTES_PER_ELEMENT * 3; - const groups = modelGroups; const objectDefinitionsBuffer = device.createBuffer({ label: "Object definitions buffer", - size: OBJECT_DEFINITION_SIZE_PER_ENTRY * groups.length, + size: OBJECT_DEFINITION_SIZE_PER_ENTRY * modelGroups.length, usage: GPUBufferUsage.STORAGE, mappedAtCreation: true, }); @@ -394,8 +393,7 @@ async function runPathTracer( const objectDefinitionsData = new Uint32Array(objectDefinitionsMapped); objectDefinitionsData.set( - // todo: reconsider type assertion - groups.map((g) => [g.start, g.count, g.materialIndex!]).flat(1), + modelGroups.map((g) => [g.start, g.count, g.materialIndex]).flat(1), ); objectDefinitionsBuffer.unmap(); @@ -659,7 +657,7 @@ async function runPathTracer( clearColor: clearColor === false ? [0, 0, 0] : clearColor, enableClearColor: clearColor === false ? 0 : 1, maxRayDepth, - objectDefinitionLength: groups.length, + objectDefinitionLength: modelGroups.length, }); // todo: consider buffer writing device.queue.writeBuffer(uniformBuffer, 0, uniformData.arrayBuffer); diff --git a/strahl-lib/src/prepare-geometry.ts b/strahl-lib/src/prepare-geometry.ts index 41bf1c6..973463d 100644 --- a/strahl-lib/src/prepare-geometry.ts +++ b/strahl-lib/src/prepare-geometry.ts @@ -1,9 +1,27 @@ import { getBVHExtremes, MeshBVH } from "three-mesh-bvh"; import { logGroup } from "./benchmark/cpu-performance-logger"; import { consolidateMesh } from "./consolidate-mesh"; -import { Group } from "three"; +import { GeometryGroup, Group } from "three"; import { assertMeshBVHInternalStructure, bvhToTextures } from "./bvh-util"; -import { InternalError } from "./core/exceptions"; +import { InternalError, InvalidMaterialGroupError } from "./core/exceptions"; + +export type MaterializedGeometryGroup = { + start: number; + count: number; + materialIndex: number; +}; + +function assertMaterializedGeometryGroup( + geometryGroup: GeometryGroup, +): asserts geometryGroup is MaterializedGeometryGroup { + if ( + typeof geometryGroup.start !== "number" || + typeof geometryGroup.count !== "number" || + typeof geometryGroup.materialIndex !== "number" + ) { + throw new InvalidMaterialGroupError(geometryGroup); + } +} export function prepareGeometry(model: { scene: Group }) { const reducedModel = consolidateMesh([model.scene]); @@ -34,6 +52,11 @@ export function prepareGeometry(model: { scene: Group }) { const normals = boundsTree.geometry.attributes.normal.array; + const modelGroups = reducedModel.geometry.groups.map((geometryGroup) => { + assertMaterializedGeometryGroup(geometryGroup); + return geometryGroup; + }); + return { indirectBuffer: boundsTree._indirectBuffer, boundsArray, @@ -41,7 +64,7 @@ export function prepareGeometry(model: { scene: Group }) { positions, normals, meshIndices, - modelGroups: reducedModel.geometry.groups, + modelGroups, modelMaterials: reducedModel.materials, maxBvhDepth, bvhBuildTime,