Skip to content

Commit

Permalink
Extend material validation
Browse files Browse the repository at this point in the history
  • Loading branch information
StuckiSimon committed Aug 18, 2024
1 parent 0d158cf commit 92f6bc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
8 changes: 3 additions & 5 deletions strahl-lib/src/path-tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
29 changes: 26 additions & 3 deletions strahl-lib/src/prepare-geometry.ts
Original file line number Diff line number Diff line change
@@ -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]);
Expand Down Expand Up @@ -34,14 +52,19 @@ 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,
contentsArray,
positions,
normals,
meshIndices,
modelGroups: reducedModel.geometry.groups,
modelGroups,
modelMaterials: reducedModel.materials,
maxBvhDepth,
bvhBuildTime,
Expand Down

0 comments on commit 92f6bc9

Please sign in to comment.