license import { getGlslDifferences } from './glsl_version' ; import { GPGPUProgram , useShapeUniforms } from './gpgpu_math' ; import * as shader_util from './shader_compiler_util' ; import { PackingScheme } from './tex_util' ; export class DecodeMatrixPackedProgram implements GPGPUProgram { variableNames = [ 'A' ] ; userCode : string ; packedInputs = true ; packedOutput = true ; outputShape : [ number , number , number ] ; outPackingScheme = PackingScheme . DENSE ; enableShapeUniforms : boolean ; customUniforms = [ { name : 'texShape' , type : 'ivec2' as const } ] ; constructor ( outputShape ) { const glsl = getGlslDifferences ( ) ; this . outputShape = outputShape ; this . enableShapeUniforms = useShapeUniforms ( this . outputShape . length ) ; this . userCode = `
      ivec3 outCoordsFromFlatIndex(int index) {
        ${ this . enableShapeUniforms ? shader_util . getOutputLogicalCoordinatesFromFlatIndexByUniform ( [ 'r' , 'c' , 'd' ] , outputShape ) : shader_util . getLogicalCoordinatesFromFlatIndex ( [ 'r' , 'c' , 'd' ] , outputShape ) }
        return ivec3(r, c, d);
      }

      void main() {
        ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
        int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);

        vec4 result = vec4(0.);

        for (int i=0; i<4; i++) {
          int flatIndex = index + i;
          ivec3 rc = outCoordsFromFlatIndex(flatIndex);
          result[i] = getChannel(getA(rc.x, rc.y, rc.z), vec2(rc.y, rc.z));
        }

        ${ glsl . output } = result;
      }
    ` ; } }