|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {getChannels} from '../packing_util'; |
| 19 | +import {GPGPUProgram} from './gpgpu_math'; |
| 20 | +import {getCoordsDataType} from './shader_compiler'; |
| 21 | + |
| 22 | +export class ReversePackedProgram implements GPGPUProgram { |
| 23 | + variableNames = ['x']; |
| 24 | + outputShape: number[]; |
| 25 | + userCode: string; |
| 26 | + usesPackedTextures = true; |
| 27 | + |
| 28 | + constructor(xShape: number[], axis: number[]) { |
| 29 | + const rank = xShape.length; |
| 30 | + if (rank > 4) { |
| 31 | + throw new Error( |
| 32 | + `WebGL backend: Reverse of rank-${rank} tensor is not yet supported`); |
| 33 | + } |
| 34 | + this.outputShape = xShape; |
| 35 | + const channels = getChannels('rc', rank); |
| 36 | + const nextColumn = |
| 37 | + `${channels[rank - 1]} + 1 < ${this.outputShape[rank - 1]}`; |
| 38 | + const nextRow = `${channels[rank - 2]} + 1 < ${this.outputShape[rank - 2]}`; |
| 39 | + const type = getCoordsDataType(rank); |
| 40 | + if (rank === 1) { |
| 41 | + this.userCode = ` |
| 42 | + void main(){ |
| 43 | + int rc = getOutputCoords(); |
| 44 | + vec4 result = vec4(0.); |
| 45 | + result.r = getChannel(getX(${xShape[0]} - rc - 1), rc); |
| 46 | + if(${nextColumn}){ |
| 47 | + result.g = getChannel(getX(${xShape[0]} - (rc + 1) - 1), rc + 1); |
| 48 | + } |
| 49 | + setOutput(result); |
| 50 | + } |
| 51 | + `; |
| 52 | + } else { |
| 53 | + this.userCode = ` |
| 54 | + void main() { |
| 55 | + ${type} rc = getOutputCoords(); |
| 56 | + vec4 result = vec4(0.); |
| 57 | + result.r = ${getR(channels.slice())}; |
| 58 | + if(${nextColumn}){ |
| 59 | + result.g = ${getG(channels.slice())}; |
| 60 | + } |
| 61 | + if(${nextRow}) { |
| 62 | + result.b = ${getB(channels.slice())}; |
| 63 | + if(${nextColumn}) { |
| 64 | + result.a = ${getA(channels.slice())}; |
| 65 | + } |
| 66 | + } |
| 67 | + setOutput(result); |
| 68 | + } |
| 69 | + `; |
| 70 | + } |
| 71 | + |
| 72 | + function getR(channels: string[]): string { |
| 73 | + return getChannel(channels); |
| 74 | + } |
| 75 | + |
| 76 | + function getG(channels: string[]): string { |
| 77 | + channels[rank - 1] = '(' + channels[rank - 1] + ` + 1)`; |
| 78 | + return getChannel(channels); |
| 79 | + } |
| 80 | + |
| 81 | + function getB(channels: string[]): string { |
| 82 | + channels[rank - 2] = '(' + channels[rank - 2] + ` + 1)`; |
| 83 | + return getChannel(channels); |
| 84 | + } |
| 85 | + |
| 86 | + function getA(channels: string[]): string { |
| 87 | + channels[rank - 1] = '(' + channels[rank - 1] + ` + 1)`; |
| 88 | + channels[rank - 2] = '(' + channels[rank - 2] + ` + 1)`; |
| 89 | + return getChannel(channels); |
| 90 | + } |
| 91 | + |
| 92 | + function getChannel(channels: string[]): string { |
| 93 | + const inCoordsArray = xShape.map((_, i) => getInCoord(i, channels)); |
| 94 | + const inCoords = inCoordsArray.join(','); |
| 95 | + const innerDims = inCoordsArray.slice(-2).join(','); |
| 96 | + return `getChannel(getX(${inCoords}), vec2(${innerDims}))`; |
| 97 | + } |
| 98 | + |
| 99 | + function getInCoord(i: number, channels1: string[]): string { |
| 100 | + if (axis.indexOf(i) !== -1 && xShape[i] !== 1) { |
| 101 | + return `${xShape[i]} - ${channels1[i]} - 1`; |
| 102 | + } else { |
| 103 | + return `${channels1[i]}`; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments