Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
1c11971
texture to tensor.
lina128 Sep 22, 2022
73b2916
ini
Linchenn Sep 23, 2022
de32e12
lint
Linchenn Sep 23, 2022
7209e2a
tune tests
Linchenn Sep 24, 2022
24baec9
Merge branch 'master' into lins
Linchenn Sep 24, 2022
b7ee83a
tune style
Linchenn Sep 24, 2022
ad8d3ea
Merge branch 'lins' of https://github.com/Linchenn/tfjs into lins
Linchenn Sep 24, 2022
205b1f8
tune style
Linchenn Sep 25, 2022
daaa585
tune test for f16 env
Linchenn Sep 25, 2022
c60a220
tune style
Linchenn Sep 25, 2022
8cc128d
rename texShapeRC
Linchenn Sep 26, 2022
50172ac
expose func to core
Linchenn Sep 27, 2022
6ae774d
move to core
Linchenn Oct 2, 2022
563bdae
Merge branch 'master' into lins
Linchenn Oct 2, 2022
011acd3
Update engine.ts
Linchenn Oct 2, 2022
03c9dbc
add color map
Linchenn Oct 3, 2022
4e54da4
add color shader
Linchenn Oct 3, 2022
4ee1c48
rollback
Linchenn Oct 3, 2022
ced3aab
upd docs
Linchenn Oct 3, 2022
7081c9c
Update backend_webgl.ts
Linchenn Oct 3, 2022
3952626
Update backend_webgl.ts
Linchenn Oct 3, 2022
6c4d0e6
add tests
Linchenn Oct 3, 2022
961d520
test f16
Linchenn Oct 3, 2022
122b6ce
tune style
Linchenn Oct 3, 2022
2f66265
tune doc
Linchenn Oct 3, 2022
abb81a7
typo
Linchenn Oct 3, 2022
a5fc465
support subset of RGBA & disable bool
Linchenn Oct 4, 2022
988a27b
tune style
Linchenn Oct 4, 2022
e7db164
expose WebGLChannels
Linchenn Oct 4, 2022
81b333c
add tests for ABGR & int32 dtype
Linchenn Oct 4, 2022
4b61a86
Add test & doc for custom backend
Linchenn Oct 4, 2022
2d88e52
upd tests
Linchenn Oct 4, 2022
6f05771
Merge branch 'master' into lins
Linchenn Oct 4, 2022
715972c
Update backend_webgl_test.ts
Linchenn Oct 4, 2022
915acaa
tune tests
Linchenn Oct 4, 2022
939e8bb
Update backend_webgl_test.ts
Linchenn Oct 4, 2022
8b64c88
check texture
Linchenn Oct 4, 2022
87bf069
tune tests
Linchenn Oct 5, 2022
cad46bb
Update backend_webgl_test.ts
Linchenn Oct 5, 2022
a08123e
Merge branch 'master' into lins
Linchenn Oct 5, 2022
6d85b60
typo
Linchenn Oct 5, 2022
0cdc770
Merge branch 'lins' of https://github.com/Linchenn/tfjs into lins
Linchenn Oct 5, 2022
59d0698
Merge branch 'master' into lins
Linchenn Oct 5, 2022
c39e740
dataToGPU
Linchenn Oct 5, 2022
ba02b0f
Merge branch 'lins' of https://github.com/Linchenn/tfjs into lins
Linchenn Oct 5, 2022
dad105b
Merge branch 'master' into lins
Linchenn Oct 5, 2022
2a4c531
Merge branch 'master' into lins
Linchenn Oct 8, 2022
6187006
fix
Linchenn Oct 8, 2022
2ee87cb
fix
Linchenn Oct 9, 2022
0bc4c44
typo
Linchenn Oct 9, 2022
c3465e7
Merge branch 'master' into lins
Linchenn Oct 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion tfjs-backend-webgl/src/backend_webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import './flags_webgl';

import * as tf from '@tensorflow/tfjs-core';
import {backend_util, BackendValues, buffer, DataId, DataStorage, DataToGPUWebGLOption, DataType, engine, env, GPUData, kernel_impls, KernelBackend, MemoryInfo, nextFrame, NumericDataType, Rank, RecursiveArray, scalar, ShapeMap, Tensor, Tensor2D, TensorBuffer, TensorInfo, tidy, TimingInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {backend_util, BackendValues, buffer, DataId, DataStorage, DataToGPUWebGLOption, DataType, engine, env, GPUData, kernel_impls, KernelBackend, MemoryInfo, nextFrame, NumericDataType, Rank, RecursiveArray, scalar, ShapeMap, Tensor, Tensor2D, TensorBuffer, TensorInfo, tidy, TimingInfo, TypedArray, util, WebGLData} from '@tensorflow/tfjs-core';
import {getWebGLContext} from './canvas_util';
import {DecodeMatrixProgram} from './decode_matrix_gpu';
import {DecodeMatrixPackedProgram} from './decode_matrix_packed_gpu';
Expand Down Expand Up @@ -176,6 +176,38 @@ export class MathBackendWebGL extends KernelBackend {
return this.texData.numDataIds() - this.pendingDeletes;
}

// Writes a new entry to the data store with a WebGL texture, and registers it
// to the texture manager.
writeTexture(
texture: WebGLTexture, shape: number[], dtype: DataType,
texHeight: number, texWidth: number, channels: string): DataId {
// Temporarily create an tensor info to make the texture compatible with
// the runWebGLProgram's input.
const input = this.makeTensorInfo(shape, dtype);
const inData = this.texData.get(input.dataId);
// Even though the input texture could be unpacked or dense packed, it is
// always considered as unpacked for EncodeMatrixProgram.
inData.isPacked = false;

// Bind texture to the input tensor.
inData.texture = {texture, texShape: [texHeight, texWidth]};
inData.texShape = [texHeight, texWidth];

const shapeAs3D = webgl_util.getShapeAs3D(shape);
const program =
new EncodeMatrixProgram(shapeAs3D, false /* isByteArray */, channels);
const output =
this.runWebGLProgram(program, [input], dtype, [[texHeight, texWidth]]);
output.shape = shape;

// Unbind the texture from the input tensor to avoid the texture being
// released.
inData.texture = null;
this.disposeIntermediateTensorInfo(input);

return output.dataId;
}

write(values: BackendValues, shape: number[], dtype: DataType): DataId {
if (env().getBool('WEBGL_CHECK_NUMERICAL_PROBLEMS') ||
env().getBool('DEBUG')) {
Expand Down Expand Up @@ -1267,6 +1299,31 @@ export class MathBackendWebGL extends KernelBackend {
binary.outTexShapeLocation = outTexShapeLocation;
}
}

/**
* Create a TF.js tensor out of an existing WebGL texture. A new texture will
* be created.
*/
createTensorFromTexture(values: WebGLData, shape: number[], dtype: DataType):
Tensor {
const {texture, height, width, channels} = values;
const backend = engine().backend as MathBackendWebGL;

// Have to throw an error, otherwise WebGL just warns and returns wrong
// values.
if (!backend.gpgpu.gl.isTexture(texture)) {
throw new Error(
`The texture is invalid. Also, please make sure the texture and ` +
`the TFJS WebGL backend are using the same canvas. If you want to ` +
`use your own custom canvas, you have to create and use the custom ` +
`TFJS WebGL backend created from the canvas through ` +
`'new tf.MathBackendWebGL(customCanvas)'.`);
}

const dataId =
backend.writeTexture(texture, shape, dtype, height, width, channels);
return engine().makeTensorFromDataId(dataId, shape, dtype, backend);
}
}

function float32ToTypedArray<D extends NumericDataType>(
Expand Down
Loading