-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Ported the stroke shader to WebGPU renderer #7915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
77a9733
added stroke shader, currently not working
lukeplowden 5db2d33
changed drawbuffers to draw stroke and fill buffers depending on curr…
lukeplowden a116e6f
fixed uViewport uniform (uniform problem fixed in upstream)
lukeplowden ddfeb05
remove console log
lukeplowden 14f1857
remove unused variable
lukeplowden 2c19cdc
remove hardcoded viewport (uniform issue fixed upstream)
lukeplowden e7696f0
fix stroke shader bugs from porting process)
lukeplowden ed88f91
stroke test
lukeplowden 186f423
Merge remote-tracking branch 'upstream/webgpu' into webgpu
lukeplowden 1e9b308
Merge remote-tracking branch 'upstream/webgpu' into webgpu
lukeplowden 4dc493d
add stroke to preview
lukeplowden d88fba9
change stroke shader constants/ preprocessor to be compatible with we…
lukeplowden c48977a
rename fillHooks to populateHooks (ambiguous with fill/stroke)
lukeplowden f0875d7
add strokes back to WebGL mode
lukeplowden e8bedfc
add strokes back to WebGL mode
lukeplowden 0b50257
missing }
lukeplowden 0af8df9
typo in string
lukeplowden f120ad9
typo in string
lukeplowden 1474c97
Merge branch 'webgpu' of github.com:lukeplowden/p5.js into webgpu
lukeplowden 468b638
multiply alpha after hook
lukeplowden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| import { Renderer3D } from '../core/p5.Renderer3D'; | ||
| import { Renderer3D, getStrokeDefs } from '../core/p5.Renderer3D'; | ||
| import { Shader } from '../webgl/p5.Shader'; | ||
| import * as constants from '../core/constants'; | ||
|
|
||
|
|
||
| import { colorVertexShader, colorFragmentShader } from './shaders/color'; | ||
| import { lineVertexShader, lineFragmentShader} from './shaders/line'; | ||
| import { materialVertexShader, materialFragmentShader } from './shaders/material'; | ||
|
|
||
| const { lineDefs } = getStrokeDefs((n, v, t) => `const ${n}: ${t} = ${v};\n`); | ||
|
|
||
| class RendererWebGPU extends Renderer3D { | ||
| constructor(pInst, w, h, isMainCanvas, elt) { | ||
| super(pInst, w, h, isMainCanvas, elt) | ||
|
|
@@ -457,7 +462,6 @@ class RendererWebGPU extends Renderer3D { | |
| for (const attrName in shader.attributes) { | ||
| const attr = shader.attributes[attrName]; | ||
| if (!attr || attr.location === -1) continue; | ||
|
|
||
| // Get the vertex buffer info associated with this attribute | ||
| const renderBuffer = | ||
| this.buffers[shader.shaderType].find(buf => buf.attr === attrName) || | ||
|
|
@@ -481,7 +485,6 @@ class RendererWebGPU extends Renderer3D { | |
| ], | ||
| }); | ||
| } | ||
|
|
||
| return layouts; | ||
| } | ||
|
|
||
|
|
@@ -516,7 +519,9 @@ class RendererWebGPU extends Renderer3D { | |
|
|
||
| _useShader(shader, options) {} | ||
|
|
||
| _updateViewport() {} | ||
| _updateViewport() { | ||
| this._viewport = [0, 0, this.width, this.height]; | ||
| } | ||
|
|
||
| zClipRange() { | ||
| return [0, 1]; | ||
|
|
@@ -557,8 +562,9 @@ class RendererWebGPU extends Renderer3D { | |
| if (!buffers) return; | ||
|
|
||
| const commandEncoder = this.device.createCommandEncoder(); | ||
| const currentTexture = this.drawingContext.getCurrentTexture(); | ||
| const colorAttachment = { | ||
| view: this.drawingContext.getCurrentTexture().createView(), | ||
| view: currentTexture.createView(), | ||
| loadOp: "load", | ||
| storeOp: "store", | ||
| }; | ||
|
|
@@ -581,34 +587,33 @@ class RendererWebGPU extends Renderer3D { | |
| }; | ||
|
|
||
| const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); | ||
| passEncoder.setPipeline(this._curShader.getPipeline(this._shaderOptions({ mode }))); | ||
|
|
||
| const currentShader = this._curShader; | ||
| passEncoder.setPipeline(currentShader.getPipeline(this._shaderOptions({ mode }))); | ||
| // Bind vertex buffers | ||
| for (const buffer of this._getVertexBuffers(this._curShader)) { | ||
| for (const buffer of this._getVertexBuffers(currentShader)) { | ||
| passEncoder.setVertexBuffer( | ||
| this._curShader.attributes[buffer.attr].location, | ||
| currentShader.attributes[buffer.attr].location, | ||
| buffers[buffer.dst], | ||
| 0 | ||
| ); | ||
| } | ||
|
|
||
| // Bind uniforms | ||
| this._packUniforms(this._curShader); | ||
| this.device.queue.writeBuffer( | ||
| this._curShader._uniformBuffer, | ||
| currentShader._uniformBuffer, | ||
| 0, | ||
| this._curShader._uniformData.buffer, | ||
| this._curShader._uniformData.byteOffset, | ||
| this._curShader._uniformData.byteLength | ||
| currentShader._uniformData.buffer, | ||
| currentShader._uniformData.byteOffset, | ||
| currentShader._uniformData.byteLength | ||
| ); | ||
|
|
||
| // Bind sampler/texture uniforms | ||
| for (const [group, entries] of this._curShader._groupEntries) { | ||
| for (const [group, entries] of currentShader._groupEntries) { | ||
| const bgEntries = entries.map(entry => { | ||
| if (group === 0 && entry.binding === 0) { | ||
| return { | ||
| binding: 0, | ||
| resource: { buffer: this._curShader._uniformBuffer }, | ||
| resource: { buffer: currentShader._uniformBuffer }, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -620,22 +625,27 @@ class RendererWebGPU extends Renderer3D { | |
| }; | ||
| }); | ||
|
|
||
| const layout = this._curShader._bindGroupLayouts[group]; | ||
| const layout = currentShader._bindGroupLayouts[group]; | ||
| const bindGroup = this.device.createBindGroup({ | ||
| layout, | ||
| entries: bgEntries, | ||
| }); | ||
|
|
||
| passEncoder.setBindGroup(group, bindGroup); | ||
| } | ||
|
|
||
| // Bind index buffer and issue draw | ||
| if (buffers.indexBuffer) { | ||
| const indexFormat = buffers.indexFormat || "uint16"; | ||
| passEncoder.setIndexBuffer(buffers.indexBuffer, indexFormat); | ||
| passEncoder.drawIndexed(geometry.faces.length * 3, count, 0, 0, 0); | ||
| } else { | ||
| passEncoder.draw(geometry.vertices.length, count, 0, 0); | ||
| if (currentShader.shaderType === "fill") { | ||
| // Bind index buffer and issue draw | ||
| if (buffers.indexBuffer) { | ||
| const indexFormat = buffers.indexFormat || "uint16"; | ||
| passEncoder.setIndexBuffer(buffers.indexBuffer, indexFormat); | ||
| passEncoder.drawIndexed(geometry.faces.length * 3, count, 0, 0, 0); | ||
| } else { | ||
| passEncoder.draw(geometry.vertices.length, count, 0, 0); | ||
| } | ||
| } | ||
|
|
||
| if (buffers.lineVerticesBuffer && currentShader.shaderType === "stroke") { | ||
| passEncoder.draw(geometry.lineVertices.length / 3, count, 0, 0); | ||
| } | ||
|
|
||
| passEncoder.end(); | ||
|
|
@@ -665,7 +675,7 @@ class RendererWebGPU extends Renderer3D { | |
| new RegExp(`struct\\s+${structName}\\s*\\{([^\\}]+)\\}`) | ||
| ); | ||
| if (!structMatch) { | ||
| throw new Error(`Can't find a struct definition for ${structName}`); | ||
| throw new Error(`Can't find a struct defnition for ${structName}`); | ||
|
||
| } | ||
|
|
||
| const structBody = structMatch[1]; | ||
|
|
@@ -788,7 +798,6 @@ class RendererWebGPU extends Renderer3D { | |
| } | ||
| const structType = uniformVarMatch[2]; | ||
| const uniforms = this._parseStruct(shader.vertSrc(), structType); | ||
|
|
||
| // Extract samplers from group bindings | ||
| const samplers = []; | ||
| const samplerRegex = /@group\((\d+)\)\s*@binding\((\d+)\)\s*var\s+(\w+)\s*:\s*(\w+);/g; | ||
|
|
@@ -941,6 +950,33 @@ class RendererWebGPU extends Renderer3D { | |
| return this._defaultColorShader; | ||
| } | ||
|
|
||
| _getLineShader() { | ||
| if (!this._defaultLineShader) { | ||
| this._defaultLineShader = new Shader( | ||
| this, | ||
| lineDefs + lineVertexShader, | ||
| lineDefs + lineFragmentShader, | ||
| { | ||
| vertex: { | ||
| "void beforeVertex": "() {}", | ||
| "StrokeVertex getObjectInputs": "(inputs: StrokeVertex) { return inputs; }", | ||
| "StrokeVertex getWorldInputs": "(inputs: StrokeVertex) { return inputs; }", | ||
| "StrokeVertex getCameraInputs": "(inputs: StrokeVertex) { return inputs; }", | ||
| "void afterVertex": "() {}", | ||
| }, | ||
| fragment: { | ||
| "void beforeFragment": "() {}", | ||
| "Inputs getPixelInputs": "(inputs: Inputs) { return inputs; }", | ||
| "vec4<f32> getFinalColor": "(color: vec4<f32>) { return color; }", | ||
| "bool shouldDiscard": "(outside: bool) { return outside; };", | ||
| "void afterFragment": "() {}", | ||
| }, | ||
| } | ||
| ); | ||
| } | ||
| return this._defaultLineShader; | ||
| } | ||
|
|
||
| ////////////////////////////////////////////// | ||
| // Setting | ||
| ////////////////////////////////////////////// | ||
|
|
@@ -956,7 +992,7 @@ class RendererWebGPU extends Renderer3D { | |
| ////////////////////////////////////////////// | ||
| // Shader hooks | ||
| ////////////////////////////////////////////// | ||
| fillHooks(shader, src, shaderType) { | ||
| populateHooks(shader, src, shaderType) { | ||
| if (!src.includes('fn main')) return src; | ||
|
|
||
| // Apply some p5-specific preprocessing. WGSL doesn't have preprocessor | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be missing a } here