MCPcopy
hub / github.com/tensorflow/tfjs / registerWebGLBackend

Function registerWebGLBackend

tfjs-react-native/src/platform_react_native.ts:192–276  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

190}
191
192function registerWebGLBackend() {
193 try {
194 const PRIORITY = 5;
195 tf.registerBackend(
196 "rn-webgl",
197 async () => {
198 const glContext = await GLView.createContextAsync();
199
200 // ExpoGl getBufferSubData is not implemented yet (throws an exception).
201 tf.env().set("WEBGL_BUFFER_SUPPORTED", false);
202
203 //
204 // Mock extension support for EXT_color_buffer_float and
205 // EXT_color_buffer_half_float on the expo-gl context object.
206 // In react native we do not have to get a handle to the extension
207 // in order to use the functionality of that extension on the device.
208 //
209 // This code block makes iOS and Android devices pass the extension checks
210 // used in core. After those are done core will actually test whether
211 // we can render/download float or half float textures.
212 //
213 // We can remove this block once we upstream checking for these
214 // extensions in expo.
215 //
216 // TODO look into adding support for checking these extensions in expo-gl
217 //
218 //@ts-ignore
219 const getExt = glContext.getExtension.bind(glContext);
220 const shimGetExt = (name: string) => {
221 if (name === "EXT_color_buffer_float") {
222 if (RNPlatform.OS === "ios") {
223 // iOS does not support EXT_color_buffer_float
224 return null;
225 } else {
226 return {};
227 }
228 }
229
230 if (name === "EXT_color_buffer_half_float") {
231 return {};
232 }
233 return getExt(name);
234 };
235
236 //
237 // Manually make 'read' synchronous. glContext has a defined gl.fenceSync
238 // function that throws a "Not implemented yet" exception so core
239 // cannot properly detect that it is not supported. We mock
240 // implementations of gl.fenceSync and gl.clientWaitSync
241 // TODO remove once fenceSync and clientWaitSync is implemented upstream.
242 //
243 const shimFenceSync = () => {
244 return {};
245 };
246 const shimClientWaitSync = () => glContext.CONDITION_SATISFIED;
247
248 // @ts-ignore
249 glContext.getExtension = shimGetExt.bind(glContext);

Callers 1

Calls 4

setWebGLContextFunction · 0.90
registerBackendMethod · 0.80
assignMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…