Add basic drawing
This commit is contained in:
parent
6085f9da9f
commit
fea5ecfcee
4 changed files with 199 additions and 20 deletions
126
src/pipelines/swipe/swipe-pipeline.ts
Normal file
126
src/pipelines/swipe/swipe-pipeline.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import { setUpFullScreenQuad } from '../../utils/full-screen-quad';
|
||||
import shader from './swipe.wgsl';
|
||||
|
||||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export class SwipePipeline {
|
||||
private static readonly UNIFORM_COUNT = 8;
|
||||
|
||||
private readonly pipeline: GPURenderPipeline;
|
||||
private readonly uniforms: GPUBuffer;
|
||||
private readonly quadVertexBuffer: GPUBuffer;
|
||||
|
||||
private bindGroup?: GPUBindGroup;
|
||||
private previousTrailMapIn?: GPUTexture;
|
||||
|
||||
public constructor(private readonly device: GPUDevice) {
|
||||
const { buffer, vertex } = setUpFullScreenQuad(device);
|
||||
this.quadVertexBuffer = buffer;
|
||||
|
||||
this.pipeline = device.createRenderPipeline({
|
||||
layout: 'auto',
|
||||
vertex,
|
||||
fragment: {
|
||||
module: device.createShaderModule({
|
||||
code: shader,
|
||||
}),
|
||||
entryPoint: 'fragment',
|
||||
targets: [
|
||||
{
|
||||
format: 'rgba16float',
|
||||
},
|
||||
],
|
||||
},
|
||||
primitive: {
|
||||
topology: 'triangle-strip',
|
||||
},
|
||||
});
|
||||
|
||||
this.uniforms = this.device.createBuffer({
|
||||
size: SwipePipeline.UNIFORM_COUNT * Float32Array.BYTES_PER_ELEMENT,
|
||||
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
}
|
||||
|
||||
public setParameters({
|
||||
width,
|
||||
height,
|
||||
isSwipeActive,
|
||||
swipe,
|
||||
swipeRadius,
|
||||
}: {
|
||||
width: number;
|
||||
height: number;
|
||||
isSwipeActive: boolean;
|
||||
swipe: vec2;
|
||||
swipeRadius: number;
|
||||
}) {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
new Float32Array([
|
||||
width,
|
||||
height,
|
||||
swipe ? swipe[0] : 0,
|
||||
swipe ? swipe[1] : 0,
|
||||
swipeRadius,
|
||||
isSwipeActive ? 1 : 0,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
public execute(
|
||||
commandEncoder: GPUCommandEncoder,
|
||||
trailMapIn: GPUTexture,
|
||||
trailMapOut: GPUTexture
|
||||
) {
|
||||
this.ensureBindGroupExists(trailMapIn);
|
||||
|
||||
const renderPassDescriptor: GPURenderPassDescriptor = {
|
||||
colorAttachments: [
|
||||
{
|
||||
view: trailMapOut.createView(),
|
||||
clearValue: { r: 1.0, g: 1.0, b: 1.0, a: 1.0 },
|
||||
loadOp: 'clear',
|
||||
storeOp: 'store',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
||||
passEncoder.setPipeline(this.pipeline);
|
||||
passEncoder.setVertexBuffer(0, this.quadVertexBuffer);
|
||||
passEncoder.setBindGroup(0, this.bindGroup);
|
||||
passEncoder.draw(4, 1);
|
||||
passEncoder.end();
|
||||
}
|
||||
|
||||
private ensureBindGroupExists(trailMapIn: GPUTexture) {
|
||||
if (this.previousTrailMapIn !== trailMapIn) {
|
||||
this.bindGroup = this.device.createBindGroup({
|
||||
layout: this.pipeline.getBindGroupLayout(0),
|
||||
entries: [
|
||||
{
|
||||
binding: 0,
|
||||
resource: {
|
||||
buffer: this.uniforms,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 1,
|
||||
resource: this.device.createSampler({
|
||||
magFilter: 'linear',
|
||||
minFilter: 'linear',
|
||||
}),
|
||||
},
|
||||
{
|
||||
binding: 2,
|
||||
resource: trailMapIn.createView(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
this.previousTrailMapIn = trailMapIn;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/pipelines/swipe/swipe.wgsl
Normal file
24
src/pipelines/swipe/swipe.wgsl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
struct Settings {
|
||||
size : vec2<f32>,
|
||||
swipe : vec2<f32>,
|
||||
swipeRadius : f32,
|
||||
isSwipeActive : f32,
|
||||
};
|
||||
|
||||
@group(0) @binding(0) var<uniform> settings : Settings;
|
||||
@group(0) @binding(1) var Sampler: sampler;
|
||||
@group(0) @binding(2) var trailMap : texture_2d<f32>;
|
||||
|
||||
@fragment
|
||||
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
||||
var current = textureSample(trailMap, Sampler, uv);
|
||||
|
||||
if (
|
||||
settings.isSwipeActive == 1.0 &&
|
||||
length((uv - settings.swipe) * normalize(settings.size)) < settings.swipeRadius
|
||||
) {
|
||||
current = vec4<f32>(1);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue