Skip to content

Commit b91a4c5

Browse files
committed
No MSAA on main canvas, simpler blit shader
1 parent 8b74c3f commit b91a4c5

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/webgpu/p5.RendererWebGPU.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { colorVertexShader, colorFragmentShader } from './shaders/color';
66
import { lineVertexShader, lineFragmentShader} from './shaders/line';
77
import { materialVertexShader, materialFragmentShader } from './shaders/material';
88
import { fontVertexShader, fontFragmentShader } from './shaders/font';
9+
import { blitVertexShader, blitFragmentShader } from './shaders/blit';
910
import { wgslBackend } from './strands_wgslBackend';
1011
import noiseWGSL from './shaders/functions/noise3DWGSL';
1112
import { baseFilterVertexShader, baseFilterFragmentShader } from './shaders/filters/base';
@@ -381,7 +382,7 @@ function rendererWebGPU(p5, fn) {
381382

382383
const requestedSampleCount = activeFramebuffer ?
383384
(activeFramebuffer.antialias ? activeFramebuffer.antialiasSamples : 1) :
384-
(this.antialias || 1);
385+
1; // No MSAA needed when blitting already-antialiased textures to canvas
385386
const sampleCount = this._getValidSampleCount(requestedSampleCount);
386387

387388
const depthFormat = activeFramebuffer && activeFramebuffer.useDepth ?
@@ -1012,7 +1013,7 @@ function rendererWebGPU(p5, fn) {
10121013
this.states.setValue('enableLighting', false);
10131014
this.states.setValue('activeImageLight', null);
10141015
this._pInst.setCamera(this.finalCamera);
1015-
this._pInst.resetShader();
1016+
this._pInst.shader(this._getBlitShader());
10161017
this._pInst.resetMatrix();
10171018
this._pInst.imageMode(this._pInst.CENTER);
10181019
this._pInst.image(this.mainFramebuffer, 0, 0);
@@ -1619,6 +1620,17 @@ function rendererWebGPU(p5, fn) {
16191620
return this._defaultFontShader;
16201621
}
16211622

1623+
_getBlitShader() {
1624+
if (!this._defaultBlitShader) {
1625+
this._defaultBlitShader = new Shader(
1626+
this,
1627+
blitVertexShader,
1628+
blitFragmentShader
1629+
);
1630+
}
1631+
return this._defaultBlitShader;
1632+
}
1633+
16221634
//////////////////////////////////////////////
16231635
// Setting
16241636
//////////////////////////////////////////////

src/webgpu/shaders/blit.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const uniforms = `
2+
struct Uniforms {
3+
uModelViewMatrix: mat4x4<f32>,
4+
uProjectionMatrix: mat4x4<f32>,
5+
};
6+
`;
7+
8+
export const blitVertexShader = `
9+
struct VertexInput {
10+
@location(0) aPosition: vec3<f32>,
11+
@location(1) aNormal: vec3<f32>,
12+
@location(2) aTexCoord: vec2<f32>,
13+
@location(3) aVertexColor: vec4<f32>,
14+
};
15+
16+
struct VertexOutput {
17+
@builtin(position) Position: vec4<f32>,
18+
@location(0) vTexCoord: vec2<f32>,
19+
};
20+
21+
${uniforms}
22+
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
23+
24+
@vertex
25+
fn main(input: VertexInput) -> VertexOutput {
26+
var output: VertexOutput;
27+
output.vTexCoord = input.aTexCoord;
28+
let positionVec4 = vec4<f32>(input.aPosition, 1.0);
29+
output.Position = uniforms.uProjectionMatrix * uniforms.uModelViewMatrix * positionVec4;
30+
return output;
31+
}
32+
`;
33+
34+
export const blitFragmentShader = `
35+
struct FragmentInput {
36+
@location(0) vTexCoord: vec2<f32>,
37+
};
38+
39+
${uniforms}
40+
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
41+
@group(0) @binding(1) var uSampler: texture_2d<f32>;
42+
@group(0) @binding(2) var uSampler_sampler: sampler;
43+
44+
@fragment
45+
fn main(input: FragmentInput) -> @location(0) vec4<f32> {
46+
return textureSample(uSampler, uSampler_sampler, input.vTexCoord);
47+
}
48+
`;

0 commit comments

Comments
 (0)