Skip to content

Commit

Permalink
Deploying to gh-pages from @ 8d143c2 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Jul 23, 2024
1 parent e1593f7 commit e8cd6a2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ var occlusionQuery = {
};

var particles = {
name: 'Particles',
description: 'This example demonstrates rendering of particles simulated with compute shaders.',
name: 'Particles (HDR)',
description: 'This example demonstrates rendering of particles (using HDR capabilities when possible) simulated with compute shaders.',
filename: "sample/particles",
sources: [
{ path: 'main.ts' },
Expand Down Expand Up @@ -643,7 +643,7 @@ const pageCategories = [
normalMap,
shadowMapping,
deferredRendering,
particles,
'particles (HDR)': particles,
points,
imageBlur,
cornell,
Expand Down
2 changes: 1 addition & 1 deletion sample/particles/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>webgpu-samples: particles</title>
<title>webgpu-samples: particles (HDR)</title>
<style>
:root {
color-scheme: light dark;
Expand Down
37 changes: 31 additions & 6 deletions sample/particles/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/particles/main.js.map

Large diffs are not rendered by default.

37 changes: 30 additions & 7 deletions sample/particles/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ const context = canvas.getContext('webgpu') as GPUCanvasContext;
const devicePixelRatio = window.devicePixelRatio;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();

context.configure({
device,
format: presentationFormat,
alphaMode: 'premultiplied',
});
const presentationFormat = 'rgba16float';

function configureContext() {
context.configure({
device,
format: presentationFormat,
toneMapping: { mode: simulationParams.toneMappingMode },
alphaMode: 'premultiplied',
});
}

const particlesBuffer = device.createBuffer({
size: numParticles * particleInstanceByteSize,
Expand Down Expand Up @@ -317,10 +320,13 @@ let numMipLevels = 1;
const simulationParams = {
simulate: true,
deltaTime: 0.04,
toneMappingMode: 'standard' as GPUCanvasToneMappingMode,
brightnessFactor: 1.0,
};

const simulationUBOBufferSize =
1 * 4 + // deltaTime
1 * 4 + // brightnessFactor
3 * 4 + // padding
4 * 4 + // seed
0;
Expand All @@ -330,8 +336,23 @@ const simulationUBOBuffer = device.createBuffer({
});

const gui = new GUI();
gui.width = 325;
gui.add(simulationParams, 'simulate');
gui.add(simulationParams, 'deltaTime');
const hdrFolder = gui.addFolder('');
hdrFolder
.add(simulationParams, 'toneMappingMode', ['standard', 'extended'])
.onChange(configureContext);
hdrFolder.add(simulationParams, 'brightnessFactor', 0, 4, 0.1);
hdrFolder.open();
const hdrMediaQuery = window.matchMedia('(dynamic-range: high)');
function updateHdrFolderName() {
hdrFolder.name = `HDR settings ${
hdrMediaQuery.matches ? '' : '⚠️ Your display is not compatible'
}`;
}
updateHdrFolderName();
hdrMediaQuery.onchange = updateHdrFolderName;

const computePipeline = device.createComputePipeline({
layout: 'auto',
Expand Down Expand Up @@ -377,6 +398,7 @@ function frame() {
0,
new Float32Array([
simulationParams.simulate ? simulationParams.deltaTime : 0.0,
simulationParams.brightnessFactor,
0.0,
0.0,
0.0, // padding
Expand Down Expand Up @@ -438,4 +460,5 @@ function frame() {

requestAnimationFrame(frame);
}
configureContext();
requestAnimationFrame(frame);
4 changes: 2 additions & 2 deletions sample/particles/meta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
name: 'Particles',
name: 'Particles (HDR)',
description:
'This example demonstrates rendering of particles simulated with compute shaders.',
'This example demonstrates rendering of particles (using HDR capabilities when possible) simulated with compute shaders.',
filename: __DIRNAME__,
sources: [
{ path: 'main.ts' },
Expand Down
4 changes: 4 additions & 0 deletions sample/particles/particle.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn fs_main(in : VertexOutput) -> @location(0) vec4f {
////////////////////////////////////////////////////////////////////////////////
struct SimulationParams {
deltaTime : f32,
brightnessFactor : f32,
seed : vec4f,
}

Expand Down Expand Up @@ -125,6 +126,9 @@ fn simulate(@builtin(global_invocation_id) global_invocation_id : vec3u) {
let uv = vec2f(coord) / vec2f(textureDimensions(texture));
particle.position = vec3f((uv - 0.5) * 3.0 * vec2f(1.0, -1.0), 0.0);
particle.color = textureLoad(texture, coord, 0);
particle.color.r *= sim_params.brightnessFactor;
particle.color.g *= sim_params.brightnessFactor;
particle.color.b *= sim_params.brightnessFactor;
particle.velocity.x = (rand() - 0.5) * 0.1;
particle.velocity.y = (rand() - 0.5) * 0.1;
particle.velocity.z = rand() * 0.3;
Expand Down

0 comments on commit e8cd6a2

Please sign in to comment.