-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug when using the same Buffer for input and output. Added a te…
…st/example.
- Loading branch information
Showing
9 changed files
with
172 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Greyscale</title> | ||
<style> | ||
body {font-family:sans-serif;} | ||
div {float:left; margin:10px;} | ||
canvas, button, span {display:block;} | ||
button, span {width:400px; padding:4px 0; margin-top:20px;} | ||
</style> | ||
<script src="../dist/blink.js"></script> | ||
</head> | ||
<body> | ||
<script> | ||
const kernelSource = ` | ||
void main() { | ||
highp uvec4 color = texture(in_color, bl_UV); | ||
highp uint grey = (color.r + color.g + color.b) / 3u; | ||
out_color = uvec4(grey, grey, grey, color.a); | ||
}` | ||
|
||
|
||
makeTest('Buffer', function (ctx) { | ||
// Get the canvas data. | ||
let imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height) | ||
|
||
// Create buffer. | ||
let buffer = new blink.Buffer({ | ||
data: imageData.data, | ||
vector: 4 | ||
}) | ||
|
||
let kernel = new blink.Kernel({ | ||
input: { in_color: buffer }, | ||
output: { out_color: buffer } | ||
}, kernelSource) | ||
|
||
kernel.exec() | ||
|
||
ctx.putImageData(imageData, 0, 0) | ||
|
||
// Clean up. | ||
kernel.delete() | ||
}) | ||
|
||
makeTest('DeviceBuffer', function (ctx) { | ||
let imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height) | ||
|
||
let buffer = new blink.DeviceBuffer({ | ||
data: imageData.data, | ||
vector: 4 | ||
}) | ||
|
||
let kernel = new blink.Kernel({ | ||
input: { in_color: buffer }, | ||
output: { out_color: buffer } | ||
}, kernelSource) | ||
|
||
kernel.exec() | ||
|
||
// Download to the original ImageData object. | ||
buffer.toHost(imageData.data) | ||
|
||
ctx.putImageData(imageData, 0, 0) | ||
|
||
// Clean up. | ||
kernel.delete() | ||
buffer.delete() | ||
}) | ||
|
||
|
||
function makeTest(title, onClick) { | ||
let div = document.createElement('div') | ||
|
||
let h1 = document.createElement('h1') | ||
h1.textContent = title | ||
div.appendChild(h1) | ||
|
||
let canvas = document.createElement('canvas') | ||
canvas.width = canvas.height = 400 | ||
div.appendChild(canvas) | ||
|
||
let rect = [0, 0, canvas.width, canvas.height] | ||
|
||
// Draw on the canvas. | ||
const ctx = canvas.getContext('2d') | ||
let gradient = ctx.createLinearGradient(0, 0, 0, canvas.height) | ||
gradient.addColorStop(0, 'rgb(255, 0, 0)') | ||
gradient.addColorStop(0.2, 'rgb(255, 255, 0)') | ||
gradient.addColorStop(0.4, 'rgb(0, 255, 0)') | ||
gradient.addColorStop(0.6, 'rgb(0, 255, 255)') | ||
gradient.addColorStop(0.8, 'rgb(0, 0, 255)') | ||
gradient.addColorStop(1, 'rgb(255, 0, 255)') | ||
ctx.fillStyle = gradient | ||
ctx.fillRect(...rect) | ||
|
||
const text = `blink.js ${blink.VERSION}` | ||
ctx.fillStyle = 'rgb(0, 255, 0)' | ||
ctx.font = '50px sans-serif' | ||
ctx.textAlign = 'center' | ||
ctx.textBaseline = 'top' | ||
ctx.lineWidth = 4 | ||
ctx.strokeStyle = 'white' | ||
ctx.strokeText(text, canvas.width / 2, (canvas.height - 50) / 2) | ||
ctx.fillText(text, canvas.width / 2, (canvas.height - 50) / 2) | ||
|
||
// Button. | ||
let button = document.createElement('button') | ||
button.textContent = 'Greyscale' | ||
|
||
button.addEventListener('click', function () { | ||
let then = performance.now() | ||
onClick(ctx) | ||
button.remove() | ||
span.textContent = `${performance.now() - then} ms` | ||
}) | ||
|
||
div.appendChild(button) | ||
|
||
let span = document.createElement('span') | ||
div.appendChild(span) | ||
|
||
document.body.appendChild(div) | ||
} | ||
</script> | ||
</body> | ||
</html> |