"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.reshape = exports.WebGLReshape = void 0;
const reshape_1 = require("../../../ops/reshape");
const util_1 = require("../../../util");
const utils_1 = require("../utils");
const reshape_packed_1 = require("./reshape-packed");
class WebGLReshape extends reshape_1.Reshape {
    constructor() {
        super();
        this.packedImpl = new reshape_packed_1.WebGLReshapePacked();
    }
    run(inferenceHandler, inputs) {
        if (inferenceHandler.session.pack) {
            return inferenceHandler.run(this.packedImpl, inputs);
        }
        else {
            const reshapedDims = util_1.ShapeUtil.calculateReshapedDims(inputs[0].dims, inputs[1].integerData);
            const reshapedTensor = reshape(inferenceHandler, inputs[0], reshapedDims);
            return [reshapedTensor];
        }
    }
}
exports.WebGLReshape = WebGLReshape;
function reshape(inferenceHandler, input, reshapedDims) {
    const inputTD = inferenceHandler.getOrCreateTextureData(input);
    let packedShape = reshapedDims;
    if (inputTD.channels === 4) {
        packedShape = utils_1.getPackedShape(reshapedDims);
    }
    const newTextureLayout = {
        channels: inputTD.channels,
        height: inputTD.height,
        width: inputTD.width,
        // handle reshaping into scalar Tensors
        shape: packedShape.length !== 0 ? packedShape : [1],
        strides: util_1.ShapeUtil.computeStrides(packedShape),
        unpackedShape: reshapedDims,
    };
    const newTextureData = inferenceHandler.createSharedTextureData(newTextureLayout, input.type, inputTD.texture);
    return newTextureData.tensor;
}
exports.reshape = reshape;
//# sourceMappingURL=reshape.js.map