|
3 | 3 | * @author yueshuangyan |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { GLHelper } from '../../webgl/WebGLUtils'; |
7 | | - |
8 | | -function getValueFromTensorPosNoLimit(textureName: string, { channel, height_shape, width_texture, height_texture }) { |
9 | | - return ` |
10 | | - // 根据tensor坐标获取这个tensor位置的值 |
11 | | - float getValueFromTensorPos_${textureName}(int r, int g, int b, int a) { |
12 | | - vec4 pixels = TEXTURE2D(texture_${textureName}, |
13 | | - vec2( |
14 | | - (float(a * ${channel} + g) + 0.5) / float(${width_texture}), |
15 | | - (float(r * ${height_shape} + b) + 0.5) / float(${height_texture}) |
16 | | - ) |
17 | | - ); |
18 | | - // 只用了r通道 |
19 | | - return pixels.r; |
20 | | - }`; |
21 | | -} |
22 | | - |
23 | | -function getValueFromTensorPosPackingNoLimit( |
24 | | - textureName: string, |
25 | | - { channel, height_shape, width_texture, height_texture } |
26 | | -) { |
27 | | - return ` |
28 | | - // 根据tensor坐标获取这个tensor位置的值 |
29 | | - vec4 getValueFromTensorPosPacking_${textureName}(int r, int g, int b, int a) { |
30 | | - vec4 pixels = TEXTURE2D(texture_${textureName}, |
31 | | - vec2( |
32 | | - (float(a * ${channel} + g) + 0.5) / float(${width_texture}), |
33 | | - (float(r * ${height_shape} + b) + 0.5) / float(${height_texture}) |
34 | | - ) |
35 | | - ); |
36 | | - return pixels; |
37 | | - }`; |
38 | | -} |
39 | | - |
40 | | -function genOffsetYIfConditions(cut, height) { |
41 | | - const curIndexArr = Array.from({ length: cut }, (_, i) => i).reverse(); |
42 | | - |
43 | | - return curIndexArr.reduce((acc, cur, idx) => { |
44 | | - const curIf = cur > 0 |
45 | | - ? idx === 0 |
46 | | - ? ` |
47 | | - if (float(a) / float(pieceW) >= float(${cur})) { |
48 | | - offsetY = int(${cur}) * ${height}; |
49 | | - } |
50 | | - ` |
51 | | - : ` |
52 | | - else if (float(a) / float(pieceW) >= float(${cur})) { |
53 | | - offsetY = int(${cur}) * ${height}; |
54 | | - } |
55 | | - ` |
56 | | - : ''; |
57 | | - return acc + curIf; |
58 | | - }, ''); |
59 | | -} |
60 | | - |
61 | | -function getValueFromTensorPosLimit( |
| 6 | +export function getValueFromTensorPos( |
62 | 7 | textureName: string, |
63 | 8 | { width_shape, height_shape, channel, width_texture, height_texture } |
64 | 9 | ) { |
65 | | - const limitCut = GLHelper.getWebglTextureLimitCut(); |
| 10 | + const chw = width_shape * height_shape * channel; |
| 11 | + const hw = width_shape * height_shape; |
66 | 12 | return ` |
67 | | - // 超限布局根据tensor坐标获取这个tensor位置的值 |
68 | | - float getValueFromTensorPos_${textureName}(int r, int g, int b, int a) { |
69 | | - int limitCut = ${limitCut}; |
70 | | - int pieceW = calCeil(${width_shape}, ${limitCut}); |
71 | | - int x = calMod(a, pieceW); |
72 | | - int offsetY = 0; |
73 | | -
|
74 | | - ${genOffsetYIfConditions(limitCut, height_shape)} |
75 | | -
|
| 13 | + // 根据tensor坐标获取这个tensor位置的值 |
| 14 | + float getValueFromTensorPos_${textureName}(int n, int c, int h, int w) { |
| 15 | + int index = n * ${chw} + c * ${hw} + h * ${width_shape} + w; |
| 16 | + int pos_w = int(mod(float(index), float(${width_texture}))); |
| 17 | + int pos_h = index / int(${width_texture}); |
76 | 18 | vec4 pixels = TEXTURE2D(texture_${textureName}, |
77 | 19 | vec2( |
78 | | - (float(x * ${channel} + g) + 0.5) / float(${width_texture}), |
79 | | - (float(r * limitCut * ${height_shape} + b + offsetY) + 0.5) / float(${height_texture}) |
| 20 | + (float(pos_w) + 0.5) / float(${width_texture}), |
| 21 | + (float(pos_h) + 0.5) / float(${height_texture}) |
80 | 22 | ) |
81 | 23 | ); |
| 24 | + // 只用了r通道 |
82 | 25 | return pixels.r; |
83 | 26 | }`; |
84 | 27 | } |
85 | 28 |
|
86 | | -function getValueFromTensorPosPackingLimit( |
| 29 | +export function getValueFromTensorPosPacking( |
87 | 30 | textureName: string, |
88 | | - { width_shape, height_shape, channel, width_texture, height_texture } |
| 31 | + { channel, height_shape, width_texture, height_texture, width_shape } |
89 | 32 | ) { |
| 33 | + const chw = width_shape * height_shape * channel; |
| 34 | + const hw = width_shape * height_shape; |
90 | 35 | return ` |
91 | | - // 超限布局根据tensor坐标获取这个tensor位置的值 |
92 | | - vec4 getValueFromTensorPosPacking_${textureName}(int r, int g, int b, int a) { |
93 | | - float pieceW = ceil(float(${width_shape}) / 4.0); |
94 | | - int x = calMod(a, int(pieceW)); |
95 | | - int offsetY = 0; |
96 | | -
|
97 | | - if ((float(a) / pieceW) >= 3.0) { |
98 | | - offsetY = 3 * ${height_shape}; |
99 | | - } |
100 | | - else if (float(a) / pieceW >= 2.0) { |
101 | | - offsetY = 2 * ${height_shape}; |
102 | | - } |
103 | | - else if (float(a) >= pieceW) { |
104 | | - offsetY = ${height_shape}; |
105 | | - } |
| 36 | + // 根据tensor坐标获取这个tensor位置的值 |
| 37 | + vec4 getValueFromTensorPosPacking_${textureName}(int n, int c, int h, int w) { |
| 38 | + int index = n * ${chw} + c * ${hw} + h * ${width_shape} + w; |
| 39 | + int pos_w = int(mod(float(index), float(${width_texture}))); |
| 40 | + int pos_h = index / int(${width_texture}); |
106 | 41 | vec4 pixels = TEXTURE2D(texture_${textureName}, |
107 | 42 | vec2( |
108 | | - (float(x * ${channel} + g) + 0.5) / float(${width_texture}), |
109 | | - (float(r * 4 * ${height_shape} + b + offsetY) + 0.5) / float(${height_texture}) |
| 43 | + (float(pos_w) + 0.5) / float(${width_texture}), |
| 44 | + (float(pos_h) + 0.5) / float(${height_texture}) |
110 | 45 | ) |
111 | 46 | ); |
| 47 | + // 只用了r通道 |
112 | 48 | return pixels; |
113 | 49 | }`; |
114 | 50 | } |
115 | 51 |
|
116 | | -export function getValueFromTensorPos(textureName: string, textureParams) { |
117 | | - return textureParams.limit |
118 | | - ? getValueFromTensorPosLimit(textureName, textureParams) |
119 | | - : getValueFromTensorPosNoLimit(textureName, textureParams); |
120 | | -} |
121 | | - |
122 | | -export function getValueFromTensorPosPacking(textureName: string, textureParams) { |
123 | | - return textureParams.limit |
124 | | - ? getValueFromTensorPosPackingLimit(textureName, textureParams) |
125 | | - : getValueFromTensorPosPackingNoLimit(textureName, textureParams); |
126 | | - |
127 | | -} |
128 | | - |
129 | | -export function getValueFromTensorPosPacked( |
130 | | - textureName: string, |
131 | | - { offset_y, height_shape, width_texture, height_texture } |
132 | | -) { |
133 | | - return ` |
134 | | - // 超限布局根据tensor坐标获取这个tensor位置的值 |
135 | | - float getValueFromTensorPosPacked_${textureName}(int r, int g, int b, int a) { |
136 | | - int y = b / 2; |
137 | | - int yOffset = calMod(b, 2); |
138 | | - int x = a / 2; |
139 | | - int xOffset = calMod(a, 2); |
140 | | - int height = ${height_shape} + ${offset_y}; |
141 | | - vec4 pixels = TEXTURE2D( |
142 | | - texture_${textureName}, |
143 | | - vec2((float(x) + 0.5) / float(${width_texture}), |
144 | | - (float(g * height / 2 + y) + 0.5) / float(${height_texture})) |
145 | | - ); |
146 | | - int index = 0; |
147 | | - if (xOffset == 0 && yOffset == 0) { |
148 | | - return pixels[0]; |
149 | | - } |
150 | | - else if (xOffset == 1 && yOffset == 0) { |
151 | | - return pixels[1]; |
152 | | - } |
153 | | - else if (xOffset == 0 && yOffset == 1) { |
154 | | - return pixels[2]; |
155 | | - } |
156 | | - return pixels[3]; |
157 | | - }`; |
158 | | -} |
159 | 52 |
|
160 | 53 | export function getTensorPosFromArrayIndex( |
161 | 54 | textureName: string, |
|
0 commit comments