| 45 | } |
| 46 | |
| 47 | getUserCode(): string { |
| 48 | let calculateResult; |
| 49 | const value = this.type === 'float32' ? 'value' : 'value / 255.0'; |
| 50 | calculateResult = ` |
| 51 | if (uniforms.numChannels == 1) { |
| 52 | rgba[0] = ${value}; |
| 53 | rgba[1] = ${value}; |
| 54 | rgba[2] = ${value}; |
| 55 | } else { |
| 56 | rgba[d] = ${value}; |
| 57 | }`; |
| 58 | |
| 59 | const userCode = ` |
| 60 | @group(0) @binding(0) var outImage : texture_storage_2d<${ |
| 61 | this.textureFormat}, write>; |
| 62 | ${main('index')} { |
| 63 | if (index < uniforms.size) { |
| 64 | var rgba = vec4<f32>(0.0, 0.0, 0.0, uniforms.alpha); |
| 65 | for (var d = 0; d < uniforms.numChannels; d = d + 1) { |
| 66 | let value = f32(inBuf[index * uniforms.numChannels + d]); |
| 67 | ${calculateResult} |
| 68 | } |
| 69 | rgba.x = rgba.x * rgba.w; |
| 70 | rgba.y = rgba.y * rgba.w; |
| 71 | rgba.z = rgba.z * rgba.w; |
| 72 | let coords = getCoordsFromIndex(index); |
| 73 | textureStore(outImage, vec2<i32>(coords.yx), rgba); |
| 74 | } |
| 75 | } |
| 76 | `; |
| 77 | return userCode; |
| 78 | } |
| 79 | } |