"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebGLImageScaler = void 0;
const image_scaler_1 = require("../../../ops/image-scaler");
class WebGLImageScaler extends image_scaler_1.ImageScaler {
    run(inferenceHandler, inputs) {
        return inferenceHandler.run(this, inputs);
    }
    createProgramInfo(handler, inputs) {
        const outputShape = inputs[0].dims.slice();
        const rank = outputShape.length;
        const getBiasMethod = this.createGetBiasMethod(this.bias.length);
        const shaderSource = `
      ${getBiasMethod}
      float process(int indices[${rank}]) {
        return _X(indices) * scale + getBias(bias, indices[1]);
      }`;
        return {
            inputLayouts: [handler.getOrCreateTextureLayout(inputs[0])],
            outputLayout: handler.createTextureLayoutFromShape(outputShape),
            samplers: ['X'],
            variables: [{ name: 'bias', type: 'float', arrayLength: this.bias.length }, { name: 'scale', type: 'float' }],
            shaderSource,
        };
    }
    createRunData(handler, programInfo, inputs) {
        const inputTDs = [handler.getOrCreateTextureData(inputs[0], programInfo.inputLayouts[0])];
        return {
            inputTextureDatas: inputTDs,
            outputTextureData: handler.createTextureDataFromLayout(programInfo.outputLayout, inputTDs[0].tensor.type),
            uniformData: { 'bias': this.bias, 'scale': this.scale }
        };
    }
    createGetBiasMethod(numChannels) {
        const codeLines = [`float getBias(float bias[${numChannels}], int channel) {`];
        for (let i = 0; i < numChannels; ++i) {
            if (i === 0) {
                codeLines.push('\t' +
                    `if (channel == ${i}) { return bias[${i}]; }`);
            }
            else if (i === numChannels - 1) {
                codeLines.push('\t' +
                    `else { return bias[${i}]; }`);
            }
            else {
                codeLines.push('\t' +
                    `else if (channel == ${i}) { return bias[${i}]; }`);
            }
        }
        codeLines.push('\t' +
            '}');
        return codeLines.join('\n');
    }
}
exports.WebGLImageScaler = WebGLImageScaler;
//# sourceMappingURL=image-scaler.js.map