initTexture method

bool initTexture(
  1. Map<String, dynamic> textureProperties,
  2. Texture texture
)

Implementation

bool initTexture(Map<String, dynamic> textureProperties, Texture texture) {
  bool forceUpload = false;

  // state.unbindTexture(WebGL.TEXTURE_2D);

  if (textureProperties["__webglInit"] != true) {
    textureProperties["__webglInit"] = true;

    texture.addEventListener('dispose', onTextureDispose);

    // if (texture is OpenGLTexture) {
    //   final _texture = texture;
    //   textureProperties["__webglTexture"] = _texture.openGLTexture;
    // } else {
    //   textureProperties["__webglTexture"] = gl.createTexture();
    // }

    // info.memory["textures"] = info.memory["textures"]! + 1;
  }

  // create Source <-> WebGLTextures mapping if necessary

  final source = texture.source;
  Map? webglTextures = _sources.get(source);

  if (webglTextures == null) {
    webglTextures = {};
    _sources.set(source, webglTextures);
  }

  // check if there is already a WebGLTexture object for the given texture parameters

  final textureCacheKey = getTextureCacheKey(texture);

  if (textureCacheKey != textureProperties["__cacheKey"]) {
    // if not, create a new instance of WebGLTexture

    if (webglTextures[textureCacheKey] == null) {
      // create new entry

      webglTextures[textureCacheKey] = {"texture": _gl.createTexture(), "usedTimes": 0};

      info.memory["textures"] = info.memory["textures"]! + 1;

      // when a new instance of WebGLTexture was created, a texture upload is required
      // even if the image contents are identical

      forceUpload = true;
    }

    webglTextures[textureCacheKey]["usedTimes"]++;

    // every time the texture cache key changes, it's necessary to check if an instance of
    // WebGLTexture can be deleted in order to avoid a memory leak.

    final webglTexture = webglTextures[textureProperties["__cacheKey"]];

    if (webglTexture != null) {
      webglTextures[textureProperties["__cacheKey"]]["usedTimes"]--;

      if (webglTexture["usedTimes"] == 0) {
        deleteTexture(texture);
      }
    }

    // store references to cache key and WebGLTexture object

    textureProperties["__cacheKey"] = textureCacheKey;
    textureProperties["__webglTexture"] = webglTextures[textureCacheKey]["texture"];
  }

  return forceUpload;
}