"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebGLBatchNormalization = void 0;
const batch_normalization_1 = require("../../../ops/batch-normalization");
const glsl_source_1 = require("../glsl-source");
class WebGLBatchNormalization extends batch_normalization_1.BatchNormalization {
    run(inferenceHandler, inputs) {
        return inferenceHandler.run(this, inputs);
    }
    createProgramInfo(handler, inputs) {
        const inputLayouts = inputs.map(t => handler.getOrCreateTextureLayout(t));
        const outputShape = inputs[0].dims.slice();
        const rank = outputShape.length;
        const scale = inputLayouts[1];
        const glsl = glsl_source_1.getGlsl(handler.session.backend.glContext.version);
        const shaderSource = `
      float process(int[${rank}] indices) {
        vec2 position = offsetToCoords(indices[1], ${scale.width}, ${scale.height});
        float scale = getColorAsFloat(${glsl.texture2D}(Scale, position));
        float mean = getColorAsFloat(${glsl.texture2D}(Mean, position));
        float variance = getColorAsFloat(${glsl.texture2D}(Variance, position));
        float b = getColorAsFloat(${glsl.texture2D}(B, position));

        return scale * ( (_A(indices) - mean) / sqrt(variance + float(${this.epsilon})) ) + b;
      }`;
        return {
            inputLayouts,
            outputLayout: handler.createTextureLayoutFromShape(outputShape),
            samplers: ['A', 'Scale', 'B', 'Mean', 'Variance'],
            shaderSource
        };
    }
    createRunData(handler, programInfo, inputs) {
        const inputTDs = [handler.getOrCreateTextureData(inputs[0], programInfo.inputLayouts[0])];
        inputs.slice(1).forEach(t => inputTDs.push(handler.getOrCreateTextureData(t)));
        const outputTD = handler.createTextureDataFromLayout(programInfo.outputLayout, inputTDs[0].tensor.type);
        return { inputTextureDatas: inputTDs, outputTextureData: outputTD, uniformData: {} };
    }
}
exports.WebGLBatchNormalization = WebGLBatchNormalization;
//# sourceMappingURL=batch-normalization.js.map