Improve drawing

This commit is contained in:
Andras Schmelczer 2023-04-27 20:55:48 +01:00
commit de7fcc15d0
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 221 additions and 56 deletions

View file

@ -0,0 +1,143 @@
import shader from './brush.wgsl';
import { vec2 } from 'gl-matrix';
export class BrushPipeline {
private static readonly UNIFORM_COUNT = 2;
private static readonly MAX_LINE_COUNT = 100;
private static readonly VERTICES_PER_LINE_SEGMENT = 6;
private readonly pipeline: GPURenderPipeline;
private readonly uniforms: GPUBuffer;
private readonly vertexBuffer: GPUBuffer;
private readonly linePoints: Array<vec2> = [];
private bindGroup: GPUBindGroup;
public constructor(private readonly device: GPUDevice) {
this.vertexBuffer = device.createBuffer({
size:
BrushPipeline.MAX_LINE_COUNT *
BrushPipeline.VERTICES_PER_LINE_SEGMENT *
2 *
Float32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
});
this.pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
module: device.createShaderModule({
code: shader,
}),
entryPoint: 'vertex',
buffers: [
{
arrayStride: Float32Array.BYTES_PER_ELEMENT * 2,
attributes: [
{
shaderLocation: 0,
format: 'float32x2',
offset: 0,
},
],
},
],
},
fragment: {
module: device.createShaderModule({
code: shader,
}),
entryPoint: 'fragment',
targets: [
{
format: 'rgba16float',
},
],
},
primitive: {
topology: 'triangle-list',
},
});
this.uniforms = this.device.createBuffer({
size: BrushPipeline.UNIFORM_COUNT * Float32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
this.bindGroup = this.device.createBindGroup({
layout: this.pipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: {
buffer: this.uniforms,
},
},
],
});
}
public addSwipe(position: vec2) {
this.linePoints.push(position);
}
public clearSwipes() {
this.linePoints.length = 0;
}
public setParameters({ width, height }: { width: number; height: number }) {
this.device.queue.writeBuffer(this.uniforms, 0, new Float32Array([width, height]));
this.device.queue.writeBuffer(
this.vertexBuffer,
0,
new Float32Array(
new Array(this.lineCount).fill(0).flatMap((_, i) => {
const from = this.linePoints[i];
const to = this.linePoints[i + 1];
const [a, b, c, d] = this.lineToRectangle(from, to, 0.01);
return [...a, ...b, ...c, ...b, ...c, ...d];
})
)
);
}
private get lineCount() {
return Math.max(0, this.linePoints.length - 1);
}
private lineToRectangle(from: vec2, to: vec2, width: number): [vec2, vec2, vec2, vec2] {
const dir = vec2.sub(vec2.create(), to, from);
const perp = vec2.fromValues(dir[1], -dir[0]);
vec2.normalize(perp, perp);
vec2.scale(perp, perp, width / 2);
return [
vec2.add(vec2.create(), from, perp),
vec2.sub(vec2.create(), from, perp),
vec2.add(vec2.create(), to, perp),
vec2.sub(vec2.create(), to, perp),
];
}
public execute(commandEncoder: GPUCommandEncoder, trailMapOut: GPUTexture) {
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: trailMapOut.createView(),
clearValue: { r: 1.0, g: 1.0, b: 1.0, a: 1.0 },
loadOp: 'load',
storeOp: 'store',
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(this.pipeline);
passEncoder.setBindGroup(0, this.bindGroup);
passEncoder.setVertexBuffer(0, this.vertexBuffer);
passEncoder.draw(this.lineCount * BrushPipeline.VERTICES_PER_LINE_SEGMENT, 1);
passEncoder.end();
this.linePoints.splice(0, this.linePoints.length - 1); // clear the array
}
}

View file

@ -0,0 +1,23 @@
struct VertexOutput {
@builtin(position) position : vec4<f32>,
@location(0) uv : vec2<f32>
}
@vertex
fn vertex(
@location(0) uv : vec2<f32>
) -> VertexOutput {
let position = uv * 2.0 - 1.0;
return VertexOutput(vec4(position, 0.0, 1.0), uv);
}
struct Settings {
size : vec2<f32>
};
@group(0) @binding(0) var<uniform> settings : Settings;
@fragment
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
return vec4(1, settings.size.x * 0, 0, 1);
}

View file

@ -1,14 +1,11 @@
struct Settings {
size : vec2<f32>,
swipePrevious : vec2<f32>,
swipeCurrent : vec2<f32>,
diffusionRate : f32,
decayRate : f32,
deltaTime : f32,
time : f32,
swipeRadius : f32,
swipeBlur : f32,
isSwipeActive : f32
};
@group(0) @binding(0) var<uniform> settings : Settings;
@ -26,18 +23,6 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
+ textureSample(trailMap, Sampler, uv + vec2<f32>(1, 0) / settings.size)
);
if (settings.isSwipeActive == 1.0) {
let pa = (uv - settings.swipePrevious) * normalize(settings.size);
let direction = (settings.swipeCurrent - settings.swipePrevious) * normalize(settings.size);
let q = clamp(dot(pa, direction) / dot(direction, direction), 0, 1);
let distance = length(pa - direction * q) - settings.swipeRadius;
if(distance < 0) {
let opacity = -distance / settings.swipeBlur;
return clamp(vec4(1), current, vec4(1));
}
}
return mix(
current,
neighbours / 4.0,

View file

@ -1,19 +1,13 @@
import { setUpFullScreenQuad } from '../../utils/full-screen-quad';
import shader from './diffuse.wgsl';
import { vec2 } from 'gl-matrix';
export class DiffusionPipeline {
private static readonly UNIFORM_COUNT = 14;
private static readonly UNIFORM_COUNT = 16;
private readonly pipeline: GPURenderPipeline;
private readonly uniforms: GPUBuffer;
private readonly quadVertexBuffer: GPUBuffer;
private swipes: Array<vec2> = [
vec2.fromValues(Number.NaN, Number.NaN),
vec2.fromValues(Number.NaN, Number.NaN),
];
private bindGroup?: GPUBindGroup;
private previousTrailMapIn?: GPUTexture;
@ -53,40 +47,30 @@ export class DiffusionPipeline {
decayRate,
deltaTime,
time,
swipe,
swipeRadius,
swipeBlur,
isSwipeActive,
}: {
width: number;
height: number;
swipe: vec2;
diffusionRate: number;
decayRate: number;
deltaTime: number;
time: number;
swipeRadius: number;
swipeBlur: number;
isSwipeActive: boolean;
}) {
if (swipe) {
this.swipes = [...this.swipes.slice(-1), swipe];
}
this.device.queue.writeBuffer(
this.uniforms,
0,
new Float32Array([
width,
height,
...this.swipes.flatMap((s) => [s[0], s[1]]),
diffusionRate,
decayRate,
deltaTime,
time,
swipeRadius,
swipeBlur,
isSwipeActive ? 1.0 : 0.0,
])
);
}