Skip to content

Commit

Permalink
Adjust rendering texture
Browse files Browse the repository at this point in the history
Use f32 instead of a 8unorm.
This aligns with oidn requirements
  • Loading branch information
StuckiSimon committed Aug 20, 2024
1 parent 059101f commit 3298433
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
44 changes: 32 additions & 12 deletions strahl-lib/src/path-tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type PathTracerOptions = {
}) => void;
signal?: AbortSignal;
enableTimestampQuery?: boolean;
enableFloatTextureFiltering?: boolean;
enableDenoise?: boolean;
};

Expand Down Expand Up @@ -129,6 +130,7 @@ async function runPathTracer(
clearColor = [1.0, 1.0, 1.0],
maxRayDepth = 5,
enableTimestampQuery = true,
enableFloatTextureFiltering = true,
finishedSampling,
signal = new AbortController().signal,
enableDenoise = false,
Expand Down Expand Up @@ -181,11 +183,23 @@ async function runPathTracer(
}

const supportsTimestampQuery = adapter.features.has("timestamp-query");
const supportsFilterableFloatTexture =
adapter.features.has("float32-filterable");

let useTimestampQuery = enableTimestampQuery && supportsTimestampQuery;
const useTimestampQuery = enableTimestampQuery && supportsTimestampQuery;
const useFloatTextureFiltering =
enableFloatTextureFiltering && supportsFilterableFloatTexture;

let featureList: GPUFeatureName[] = [];
if (useTimestampQuery) {
featureList.push("timestamp-query");
}
if (useFloatTextureFiltering) {
featureList.push("float32-filterable");
}

const device = await adapter.requestDevice({
requiredFeatures: useTimestampQuery ? ["timestamp-query"] : [],
requiredFeatures: featureList,
});

abortEventHub.setDestructionNotifier("device", () => {
Expand Down Expand Up @@ -244,11 +258,11 @@ async function runPathTracer(
maxBvhStackDepth: maxBvhDepth,
});

const textureData = new Uint8Array(width * height * 4);
const textureData = new Float32Array(width * height * 4);

const texture = device.createTexture({
size: [width, height],
format: "rgba8unorm",
format: "rgba32float",
usage:
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.COPY_DST |
Expand All @@ -259,13 +273,13 @@ async function runPathTracer(
device.queue.writeTexture(
{ texture },
textureData,
{ bytesPerRow: width * 4 },
{ bytesPerRow: width * 4 * Float32Array.BYTES_PER_ELEMENT },
{ width: width, height: height },
);

const textureB = device.createTexture({
size: [width, height],
format: "rgba8unorm",
format: "rgba32float",
usage:
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.COPY_DST |
Expand All @@ -279,15 +293,17 @@ async function runPathTracer(
},
textureData,
{
bytesPerRow: width * 4,
bytesPerRow: width * 4 * Float32Array.BYTES_PER_ELEMENT,
},
{
width: width,
height: height,
},
);

const sampler = device.createSampler();
const sampler = device.createSampler({
magFilter: useFloatTextureFiltering ? "linear" : "nearest",
});

const renderShaderModule = device.createShaderModule({
label: "Render Shader",
Expand All @@ -300,12 +316,16 @@ async function runPathTracer(
{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
sampler: { type: "filtering" },
sampler: {
type: useFloatTextureFiltering ? "filtering" : "non-filtering",
},
},
{
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
texture: { sampleType: "float" },
texture: {
sampleType: useFloatTextureFiltering ? "float" : "unfilterable-float",
},
},
],
});
Expand Down Expand Up @@ -563,12 +583,12 @@ async function runPathTracer(
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
storageTexture: { format: "rgba8unorm" /*, access: "write-only"*/ },
storageTexture: { format: "rgba32float" },
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
storageTexture: { format: "rgba8unorm", access: "read-only" },
storageTexture: { format: "rgba32float", access: "read-only" },
},
{
binding: 2,
Expand Down
4 changes: 2 additions & 2 deletions strahl-lib/src/tracer-shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ struct IndicesPackage {

@group(0) @binding(7) var<storage, read> materials: array<Material>;

@group(1) @binding(0) var texture: texture_storage_2d<rgba8unorm, write>;
@group(1) @binding(0) var texture: texture_storage_2d<rgba32float, write>;

@group(1) @binding(1) var readTexture: texture_storage_2d<rgba8unorm, read>;
@group(1) @binding(1) var readTexture: texture_storage_2d<rgba32float, read>;

@group(1) @binding(2) var<uniform> uniformData: UniformData;

Expand Down

0 comments on commit 3298433

Please sign in to comment.