main.es6 2.1 KB
Newer Older
Y
Yanzhan Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* eslint-disable */
/**
 * @file softmax主函数
 * @author yangmingming
 */
export default `
// start函数
void main(void) {
    vec4 v4 = getPixelsFromTexturePos_texture_origin(vCoord);
    vec2 onePixel = vec2(1.0 / float(width_texture_origin), 1.0 / float(height_texture_origin));
    float total = 0.0;
    float maxValue = getPixelsFromTexturePos_texture_origin(onePixel).r;
    int number = 0;
    vec4 pixels;
    // 求最大
    for (int i = 0; i < height_texture_origin; i++) {
        for (int j = 0; j < width_texture_origin; j++) {
            pixels = getPixelsFromTexturePos_texture_origin(onePixel * vec2(float(j), float(i)));
            number = i * width_texture_origin + j;
            if ((number * 4 + 1) < total_shape_origin) {
                maxValue = max(pixels.r, maxValue);
            }
            if ((number * 4 + 2) < total_shape_origin) {
                maxValue = max(pixels.g, maxValue);
            }
            if ((number * 4 + 3) < total_shape_origin) {
                maxValue = max(pixels.b, maxValue);
            }
            if ((number * 4 + 4) < total_shape_origin) {
                maxValue = max(pixels.a, maxValue);
            }
        }
    }
    // 求和
    for (int i = 0; i < height_texture_origin; i++) {
        for (int j = 0; j < width_texture_origin; j++) {
            pixels = getPixelsFromTexturePos_texture_origin(onePixel * vec2(float(j), float(i)));
            number = i * width_texture_origin + j;
            if ((number * 4 + 1) < total_shape_origin) {
                total += exp(pixels.r - maxValue);
            }
            if ((number * 4 + 2) < total_shape_origin) {
                total += exp(pixels.g - maxValue);
            }
            if ((number * 4 + 3) < total_shape_origin) {
                total += exp(pixels.b - maxValue);
            }
            if ((number * 4 + 4) < total_shape_origin) {
                total += exp(pixels.a - maxValue);
            }
        }
    }
    gl_FragColor = exp(v4 - vec4(maxValue, maxValue, maxValue, maxValue)) / vec4(total, total, total, total) ;
}
`;