Skip to content

Commit 64b5471

Browse files
committed
Fix non integer viewport dimensions. Change to rgba16float HDR texture.
1 parent a562d80 commit 64b5471

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

src/constants.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ export const IS_WGPU = IS_DENO;
4646
export const MODELS_DIR = IS_DENO ? 'static/models' : 'models';
4747

4848
export const DEPTH_FORMAT: GPUTextureFormat = 'depth24plus';
49-
export const HDR_RENDER_TEX_FORMAT: GPUTextureFormat = IS_DENO
50-
? 'rgba16float' // Cause: "Color state [0] is invalid: Format Rgba32Float is not blendable"
51-
: 'rgba32float';
49+
/**
50+
* Not 'rgba32float' cause: "Color state [0] is invalid: Format Rgba32Float is not blendable"
51+
* - https://github.com/Scthe/nanite-webgpu/issues/5
52+
*/
53+
export const HDR_RENDER_TEX_FORMAT: GPUTextureFormat = 'rgba16float';
5254

5355
/** 4 for Vec4, 3 for Vec3. ATM using Vec3 */
5456
export const CO_PER_VERTEX: number = 3;

src/index.deno.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseArgs } from 'jsr:@std/cli/parse-args';
44
import { Dimensions, replaceFileExt } from './utils/index.ts';
55
import { Renderer } from './renderer.ts';
66
import { SCENES, SceneName, isValidSceneName } from './scene/sceneFiles.ts';
7-
import { createGpuDevice } from './utils/webgpu.ts';
7+
import { createGpuDevice, ensureIntegerDimensions } from './utils/webgpu.ts';
88
import { OnObjectLoadedCb, loadScene } from './scene/load/loadScene.ts';
99
import { createErrorSystem } from './utils/errors.ts';
1010
import {
@@ -67,10 +67,11 @@ async function renderSceneToFile(
6767

6868
// create canvas
6969
console.log('Creating output canvas..');
70+
const canvasDimensions = ensureIntegerDimensions(VIEWPORT_SIZE);
7071
const { texture: windowTexture, outputBuffer } = createCapture(
7172
device,
72-
VIEWPORT_SIZE.width,
73-
VIEWPORT_SIZE.height
73+
canvasDimensions.width,
74+
canvasDimensions.height
7475
);
7576
const windowTextureView = windowTexture.createView();
7677

@@ -112,7 +113,12 @@ async function renderSceneToFile(
112113
renderer.cmdRender(cmdBuf, scene, windowTextureView);
113114

114115
// result to buffer
115-
cmdCopyTextureToBuffer(cmdBuf, windowTexture, outputBuffer, VIEWPORT_SIZE);
116+
cmdCopyTextureToBuffer(
117+
cmdBuf,
118+
windowTexture,
119+
outputBuffer,
120+
renderer.viewportSize
121+
);
116122

117123
// submit commands
118124
// profiler.endFrame(cmdBuf);
@@ -126,7 +132,7 @@ async function renderSceneToFile(
126132
});
127133

128134
// write output
129-
await writePngFromGPUBuffer(outputBuffer, VIEWPORT_SIZE, outputPath);
135+
await writePngFromGPUBuffer(outputBuffer, renderer.viewportSize, outputPath);
130136
}
131137

132138
async function exportScene(device: GPUDevice) {

src/renderer.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import { CullMeshletsPass } from './passes/cullMeshlets/cullMeshletsPass.ts';
2323
import { GpuProfiler } from './gpuProfiler.ts';
2424
import { Scene } from './scene/scene.ts';
2525
import { Frustum } from './utils/frustum.ts';
26-
import { assertIsGPUTextureView } from './utils/webgpu.ts';
26+
import {
27+
assertIsGPUTextureView,
28+
ensureIntegerDimensions,
29+
} from './utils/webgpu.ts';
2730
import { DepthPyramidPass } from './passes/depthPyramid/depthPyramidPass.ts';
2831
import { DepthPyramidDebugDrawPass } from './passes/depthPyramid/depthPyramidDebugDrawPass.ts';
2932
import { CullInstancesPass } from './passes/cullInstances/cullInstancesPass.ts';
@@ -39,7 +42,7 @@ export class Renderer {
3942
private readonly cameraFrustum: Frustum = new Frustum();
4043
private projectionMat: Mat4;
4144
private readonly _viewMatrix = mat4.identity(); // cached to prevent allocs.
42-
private readonly viewportSize: Dimensions = { width: 0, height: 0 };
45+
public readonly viewportSize: Dimensions = { width: 0, height: 0 };
4346
private frameIdx = 0;
4447

4548
// render target textures
@@ -256,6 +259,7 @@ export class Renderer {
256259
}
257260

258261
private handleViewportResize = (viewportSize: Dimensions) => {
262+
viewportSize = ensureIntegerDimensions(viewportSize);
259263
console.log(`Viewport resize`, viewportSize);
260264
CONFIG.nanite.render.hasValidDepthPyramid = false;
261265

src/sys_web/cavasResize.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Dimensions } from '../utils/index.ts';
2+
import { ensureIntegerDimensions } from '../utils/webgpu.ts';
23

34
export type ResizeHandler = (viewportSize: Dimensions) => void;
45

@@ -45,9 +46,9 @@ export function initCanvasResizeSystem(
4546
function getViewportSize(): Dimensions {
4647
// deno-lint-ignore no-explicit-any
4748
const devicePixelRatio = (window as any).devicePixelRatio || 1;
48-
return {
49+
return ensureIntegerDimensions({
4950
width: canvas.clientWidth * devicePixelRatio,
5051
height: canvas.clientHeight * devicePixelRatio,
51-
};
52+
});
5253
}
5354
}

src/utils/webgpu.ts

+8
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,11 @@ export const assertIsGPUTextureView = (maybeTexView: any) => {
272272
);
273273
}
274274
};
275+
276+
/** https://github.com/Scthe/nanite-webgpu/issues/2 */
277+
export function ensureIntegerDimensions(dims: Dimensions): Dimensions {
278+
return {
279+
width: Math.ceil(dims.width),
280+
height: Math.ceil(dims.height),
281+
};
282+
}

0 commit comments

Comments
 (0)