-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE * [webgpu] Implement draw API * Support bgra8-unorm * Mark texture externael and rename to_pixels to draw * Nit --------- Co-authored-by: Linchenn <40653845+Linchenn@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
374 additions
and
86 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
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,79 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {DataType} from '@tensorflow/tfjs-core'; | ||
|
||
import {getMainHeaderString as main, PixelsOpType, WebGPUProgram} from './webgpu_program'; | ||
import {computeDispatch, flatDispatchLayout} from './webgpu_util'; | ||
|
||
export class DrawProgram implements WebGPUProgram { | ||
variableNames = ['Image']; | ||
uniforms = 'alpha: f32,'; | ||
outputShape: number[]; | ||
shaderKey: string; | ||
dispatchLayout: {x: number[]}; | ||
dispatch: [number, number, number]; | ||
workgroupSize: [number, number, number] = [64, 1, 1]; | ||
type: DataType; | ||
textureFormat: GPUTextureFormat; | ||
pixelsOpType = PixelsOpType.DRAW; | ||
size = true; | ||
|
||
constructor( | ||
outShape: number[], type: DataType, textureFormat: GPUTextureFormat) { | ||
this.outputShape = outShape; | ||
this.dispatchLayout = flatDispatchLayout(this.outputShape); | ||
this.dispatch = computeDispatch( | ||
this.dispatchLayout, this.outputShape, this.workgroupSize); | ||
this.type = type; | ||
this.textureFormat = textureFormat; | ||
this.shaderKey = `draw_${type}_${textureFormat}`; | ||
} | ||
|
||
getUserCode(): string { | ||
let calculateResult; | ||
const value = this.type === 'float32' ? 'value' : 'value / 255.0'; | ||
calculateResult = ` | ||
if (uniforms.numChannels == 1) { | ||
rgba[0] = ${value}; | ||
rgba[1] = ${value}; | ||
rgba[2] = ${value}; | ||
} else { | ||
rgba[d] = ${value}; | ||
}`; | ||
|
||
const userCode = ` | ||
@group(0) @binding(0) var outImage : texture_storage_2d<${ | ||
this.textureFormat}, write>; | ||
${main('index')} { | ||
if (index < uniforms.size) { | ||
var rgba = vec4<f32>(0.0, 0.0, 0.0, uniforms.alpha); | ||
for (var d = 0; d < uniforms.numChannels; d = d + 1) { | ||
let value = f32(inBuf[index * uniforms.numChannels + d]); | ||
${calculateResult} | ||
} | ||
rgba.x = rgba.x * rgba.w; | ||
rgba.y = rgba.y * rgba.w; | ||
rgba.z = rgba.z * rgba.w; | ||
let coords = getCoordsFromIndex(index); | ||
textureStore(outImage, vec2<i32>(coords.yx), rgba); | ||
} | ||
} | ||
`; | ||
return userCode; | ||
} | ||
} |
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,85 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use backend file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {KernelConfig, KernelFunc, TensorInfo} from '@tensorflow/tfjs-core'; | ||
import {Draw, DrawAttrs, DrawInputs,} from '@tensorflow/tfjs-core'; | ||
|
||
import {WebGPUBackend} from '../backend_webgpu'; | ||
import {DrawProgram} from '../draw_webgpu'; | ||
|
||
export function draw( | ||
args: {inputs: DrawInputs, backend: WebGPUBackend, attrs: DrawAttrs}): | ||
TensorInfo { | ||
const {inputs, backend, attrs} = args; | ||
const {image} = inputs; | ||
const {canvas, options} = attrs; | ||
const [height, width] = image.shape.slice(0, 2); | ||
const {imageOptions} = options || {}; | ||
const alpha = imageOptions ?.alpha || 1; | ||
|
||
// 'rgba8unorm' should work on macOS according to | ||
// https://bugs.chromium.org/p/chromium/issues/detail?id=1298618. But | ||
// failed on macOS/M2. So use 'bgra8unorm' first when available. | ||
const format = backend.device.features.has('bgra8unorm-storage') ? | ||
'bgra8unorm' : | ||
'rgba8unorm'; | ||
const outShape = [height, width]; | ||
const program = new DrawProgram(outShape, image.dtype, format); | ||
canvas.width = width; | ||
canvas.height = height; | ||
const backendName = 'webgpu'; | ||
let gpuContext = canvas.getContext(backendName); | ||
let canvasWebGPU; | ||
if (!gpuContext) { | ||
canvasWebGPU = new OffscreenCanvas(width, height); | ||
gpuContext = canvasWebGPU.getContext(backendName); | ||
} | ||
const numChannels = image.shape.length === 3 ? image.shape[2] : 1; | ||
gpuContext.configure({ | ||
device: backend.device, | ||
format, | ||
usage: GPUTextureUsage.STORAGE_BINDING, | ||
alphaMode: 'premultiplied' | ||
}); | ||
|
||
const outputDtype = 'int32'; | ||
const output = backend.makeTensorInfo(outShape, outputDtype); | ||
const info = backend.tensorMap.get(output.dataId); | ||
info.resource = gpuContext.getCurrentTexture(); | ||
info.external = true; | ||
|
||
const uniformData = | ||
[{type: 'uint32', data: [numChannels]}, {type: 'float32', data: [alpha]}]; | ||
backend.runWebGPUProgram(program, [image], outputDtype, uniformData, output); | ||
|
||
if (canvasWebGPU) { | ||
const canvas2dContext = canvas.getContext('2d'); | ||
if (!canvas2dContext) { | ||
throw new Error( | ||
`Please make sure this canvas has only been used for 2d or webgpu context!`); | ||
} | ||
canvas2dContext.drawImage(canvasWebGPU, 0, 0); | ||
} | ||
backend.disposeData(output.dataId); | ||
return image; | ||
} | ||
|
||
export const drawConfig: KernelConfig = { | ||
kernelName: Draw, | ||
backendName: 'webgpu', | ||
kernelFunc: draw as unknown as KernelFunc | ||
}; |
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
Oops, something went wrong.