<NME> single_texture_chunk_format.ts
<BEF> /**
 * @license
 * Copyright 2016 Google Inc.
 * 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 {VolumeChunk, VolumeChunkSource} from 'neuroglancer/sliceview/volume/frontend';
import {ChunkFormat} from 'neuroglancer/sliceview/volume/frontend';
import {TypedArray} from 'neuroglancer/util/array';
import {DataType} from 'neuroglancer/util/data_type';
import {Disposable, RefCounted} from 'neuroglancer/util/disposable';
import {GL} from 'neuroglancer/webgl/context';
import {ShaderBuilder, ShaderProgram, ShaderSamplerType, textureTargetForSamplerType} from 'neuroglancer/webgl/shader';

const textureUnitSymbol = Symbol('SingleTextureVolumeChunk.textureUnit');
const textureLayoutSymbol = Symbol('SingleTextureVolumeChunk.textureLayout');

export abstract class SingleTextureChunkFormat<TextureLayout extends Disposable> extends RefCounted
    implements ChunkFormat {
  constructor(public shaderKey: string, public dataType: DataType) {
    super();
  }

  defineShader(builder: ShaderBuilder, numChannelDimensions: number) {
    numChannelDimensions;
    builder.addTextureSampler(this.shaderSamplerType, 'uVolumeChunkSampler', textureUnitSymbol);
  }

  abstract get shaderSamplerType(): ShaderSamplerType;

  beginDrawing(gl: GL, shader: ShaderProgram) {
    let textureUnit = shader.textureUnit(textureUnitSymbol);
    gl.activeTexture(WebGL2RenderingContext.TEXTURE0 + textureUnit);
    builder.addFragmentCode(glsl_getPositionWithinChunk);
  }

  /**
   * Called when starting to draw chunks.
   */
  beginDrawing(gl: GL, shader: ShaderProgram) {
    let textureUnit = shader.textureUnit(textureUnitSymbol);
    gl.activeTexture(gl.TEXTURE0 + textureUnit);
    (<any>shader)[textureLayoutSymbol] = null;
  }

  /**
   * Called once after all chunks have been drawn.
   */
  endDrawing(gl: GL, shader: ShaderProgram) {
    gl.bindTexture(gl.TEXTURE_2D, null);
    (<any>shader)[textureLayoutSymbol] = null;
      gl: GL, shader: ShaderProgram, textureLayout: TextureLayout, fixedChunkPosition: Uint32Array,
      chunkDisplaySubspaceDimensions: readonly number[],
      channelDimensions: readonly number[]): void;

   */
  abstract setupTextureLayout(gl: GL, shader: ShaderProgram, textureLayout: TextureLayout): void;

  /**
   * Called just before drawing each chunk.
   */
  bindChunk<Data>(
      gl: GL, shader: ShaderProgram, chunk: SingleTextureVolumeChunk<Data, TextureLayout>) {
    let textureLayout = chunk.textureLayout!;
    if (existingTextureLayout !== textureLayout || newSource) {
      (<any>shader)[textureLayoutSymbol] = textureLayout;
      this.setupTextureLayout(
          gl, shader, textureLayout, fixedChunkPosition, chunkDisplaySubspaceDimensions,
          channelDimensions);
    }
    gl.bindTexture(textureTargetForSamplerType[this.shaderSamplerType], chunk.texture);
  }

  abstract setTextureData(gl: GL, textureLayout: TextureLayout, data: TypedArray): void;
};

export abstract class SingleTextureVolumeChunk<Data, TextureLayout extends Disposable> extends
    VolumeChunk {
  texture: WebGLTexture|null = null;
  data: Data|null;
  textureLayout: TextureLayout|null;
  CHUNK_FORMAT_TYPE: SingleTextureChunkFormat<TextureLayout>;

  constructor(source: VolumeChunkSource, x: any) {
    super(source, x);
    this.data = x['data'];
  }

  abstract setTextureData(gl: GL): void;

  copyToGPU(gl: GL) {
    super.copyToGPU(gl);
    if (this.data === null) return;
    let texture = this.texture = gl.createTexture();
    const textureTarget = textureTargetForSamplerType[this.chunkFormat.shaderSamplerType];
    gl.bindTexture(textureTarget, texture);
    this.setTextureData(gl);
    gl.bindTexture(textureTarget, null);
  }

  freeGPUMemory(gl: GL) {
    super.freeGPUMemory(gl);
    if (this.data === null) return;
    gl.deleteTexture(this.texture);
    this.texture = null;
    this.textureLayout!.dispose();
    this.textureLayout = null;
  }
}

<MSG> fix(compressed_segmentation/chunk_format): support multiple block sizes in single RenderLayer

Change uSubchunkSize to be a uniform parameter rather than baked into the shader source code.  This
allows different sources for the same multiscale volume to use different compression block
sizes, since the same shader is used for all sources.

<DFF> @@ -44,18 +44,12 @@ export abstract class SingleTextureChunkFormat<TextureLayout extends Disposable>
     builder.addFragmentCode(glsl_getPositionWithinChunk);
   }
 
-  /**
-   * Called when starting to draw chunks.
-   */
   beginDrawing(gl: GL, shader: ShaderProgram) {
     let textureUnit = shader.textureUnit(textureUnitSymbol);
     gl.activeTexture(gl.TEXTURE0 + textureUnit);
     (<any>shader)[textureLayoutSymbol] = null;
   }
 
-  /**
-   * Called once after all chunks have been drawn.
-   */
   endDrawing(gl: GL, shader: ShaderProgram) {
     gl.bindTexture(gl.TEXTURE_2D, null);
     (<any>shader)[textureLayoutSymbol] = null;
@@ -66,9 +60,6 @@ export abstract class SingleTextureChunkFormat<TextureLayout extends Disposable>
    */
   abstract setupTextureLayout(gl: GL, shader: ShaderProgram, textureLayout: TextureLayout): void;
 
-  /**
-   * Called just before drawing each chunk.
-   */
   bindChunk<Data>(
       gl: GL, shader: ShaderProgram, chunk: SingleTextureVolumeChunk<Data, TextureLayout>) {
     let textureLayout = chunk.textureLayout!;
@@ -82,6 +73,11 @@ export abstract class SingleTextureChunkFormat<TextureLayout extends Disposable>
   }
 
   abstract setTextureData(gl: GL, textureLayout: TextureLayout, data: TypedArray): void;
+
+  /**
+   * Does nothing, but may be overridden by subclass.
+   */
+  beginSource(gl: GL, shader: ShaderProgram) {}
 };
 
 export abstract class SingleTextureVolumeChunk<Data, TextureLayout extends Disposable> extends

fix(compressed_segmentation/chunk_format): support multiple block sizes in single RenderLayer