Line data Source code
1 : import { tgpu } from 'typegpu'
2 : import * as d from 'typegpu/data'
3 : import { BrushUniforms } from './gpuSchema'
4 :
5 29 : const VertexOutput = d.struct({
6 : position: d.builtin.position,
7 : localUV: d.location(0, d.vec2f),
8 : color: d.location(1, d.vec3f),
9 : opacity: d.location(2, d.f32),
10 : hardness: d.location(3, d.f32)
11 : })
12 :
13 : const brushVertexTemplate = `
14 : @group(0) @binding(0) var<uniform> globals: BrushUniforms;
15 :
16 : @vertex
17 : fn vs(
18 : @location(0) quadPos: vec2<f32>,
19 : @location(1) pos: vec2<f32>,
20 : @location(2) size: f32,
21 : @location(3) pressure: f32
22 : ) -> VertexOutput {
23 : // Convert diameter to radius
24 : let radius = size * pressure;
25 : let pixelPos = pos + (quadPos * radius);
26 :
27 : // Convert pixel coordinates to Normalized Device Coordinates (NDC)
28 : let ndcX = (pixelPos.x / globals.screenSize.x) * 2.0 - 1.0;
29 : let ndcY = 1.0 - ((pixelPos.y / globals.screenSize.y) * 2.0); // Flip Y axis for WebGPU coordinate system
30 :
31 : return VertexOutput(
32 : vec4<f32>(ndcX, ndcY, 0.0, 1.0),
33 : quadPos,
34 : globals.brushColor,
35 : pressure * globals.brushOpacity,
36 : globals.hardness
37 : );
38 : }
39 : `
40 :
41 29 : export const brushVertex = tgpu.resolve({
42 : template: brushVertexTemplate,
43 : externals: {
44 : BrushUniforms,
45 : VertexOutput
46 : }
47 : })
48 :
49 : const brushFragmentTemplate = `
50 : @group(0) @binding(0) var<uniform> globals: BrushUniforms;
51 :
52 : @fragment
53 : fn fs(v: VertexOutput) -> @location(0) vec4<f32> {
54 : var dist: f32;
55 : if (globals.brushShape == 1u) {
56 : // Calculate Chebyshev distance for square shape
57 : dist = max(abs(v.localUV.x), abs(v.localUV.y));
58 : } else {
59 : // Calculate Euclidean distance for circle shape
60 : dist = length(v.localUV);
61 : }
62 :
63 : if (dist > 1.0) { discard; }
64 :
65 : // Calculate alpha with hardness and anti-aliasing
66 : let edgeWidth = fwidth(dist);
67 : let startFade = min(v.hardness, 1.0 - edgeWidth * 2.0);
68 : let linearAlpha = 1.0 - smoothstep(startFade, 1.0, dist);
69 : // Apply quadratic falloff for smoother edges
70 : let alphaShape = pow(linearAlpha, 2.0);
71 :
72 : // Return premultiplied alpha color
73 : let alpha = alphaShape * v.opacity;
74 : return vec4<f32>(v.color * alpha, alpha);
75 : }
76 : `
77 :
78 29 : export const brushFragment = tgpu.resolve({
79 : template: brushFragmentTemplate,
80 : externals: {
81 : VertexOutput,
82 : BrushUniforms
83 : }
84 : })
85 :
86 : const blitShaderTemplate = `
87 : @vertex fn vs(@builtin(vertex_index) vIdx: u32) -> @builtin(position) vec4<f32> {
88 : var pos = array<vec2<f32>, 3>(
89 : vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0)
90 : );
91 : return vec4<f32>(pos[vIdx], 0.0, 1.0);
92 : }
93 :
94 : @group(0) @binding(0) var myTexture: texture_2d<f32>;
95 :
96 : @fragment fn fs(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
97 : let c = textureLoad(myTexture, vec2<i32>(pos.xy), 0);
98 : // Treat texture as premultiplied to prevent double-darkening on overlaps
99 : return c;
100 : }
101 : `
102 :
103 29 : export const blitShader = tgpu.resolve({
104 : template: blitShaderTemplate,
105 : externals: {}
106 : })
107 :
108 : const compositeShaderTemplate = `
109 : @vertex fn vs(@builtin(vertex_index) vIdx: u32) -> @builtin(position) vec4<f32> {
110 : var pos = array<vec2<f32>, 3>(
111 : vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0)
112 : );
113 : return vec4<f32>(pos[vIdx], 0.0, 1.0);
114 : }
115 :
116 : @group(0) @binding(0) var myTexture: texture_2d<f32>;
117 : @group(1) @binding(0) var<uniform> globals: BrushUniforms;
118 :
119 : @fragment fn fs(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
120 : let sampled = textureLoad(myTexture, vec2<i32>(pos.xy), 0);
121 : // Apply global brush opacity to accumulated coverage
122 : return sampled * globals.brushOpacity;
123 : }
124 : `
125 :
126 29 : export const compositeShader = tgpu.resolve({
127 : template: compositeShaderTemplate,
128 : externals: {
129 : BrushUniforms
130 : }
131 : })
132 :
133 : const readbackShaderTemplate = `
134 : @group(0) @binding(0) var inputTex: texture_2d<f32>;
135 : @group(0) @binding(1) var<storage, read_write> outputBuf: array<u32>;
136 :
137 : @compute @workgroup_size(8, 8)
138 : fn main(@builtin(global_invocation_id) id: vec3<u32>) {
139 : let dims = textureDimensions(inputTex);
140 : if (id.x >= dims.x || id.y >= dims.y) { return; }
141 :
142 : let color = textureLoad(inputTex, vec2<i32>(id.xy), 0);
143 :
144 : var r = color.r;
145 : var g = color.g;
146 : var b = color.b;
147 : let a = color.a;
148 :
149 : if (a > 0.0) {
150 : r = r / a;
151 : g = g / a;
152 : b = b / a;
153 : }
154 :
155 : let ir = u32(clamp(r * 255.0 + 0.5, 0.0, 255.0));
156 : let ig = u32(clamp(g * 255.0 + 0.5, 0.0, 255.0));
157 : let ib = u32(clamp(b * 255.0 + 0.5, 0.0, 255.0));
158 : let ia = u32(clamp(a * 255.0 + 0.5, 0.0, 255.0));
159 :
160 : // Pack RGBA channels into a single u32 (Little Endian)
161 : let packed = ir | (ig << 8u) | (ib << 16u) | (ia << 24u);
162 :
163 : let index = id.y * dims.x + id.x;
164 : outputBuf[index] = packed;
165 : }
166 : `
167 :
168 29 : export const readbackShader = tgpu.resolve({
169 : template: readbackShaderTemplate,
170 : externals: {}
171 : })
|