onnxruntime

Форк
0
/
glsl-encoding-lib.ts 
99 строк · 3.4 Кб
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
import { GlslContext, GlslLib, GlslLibRoutine } from './glsl-definitions';
5

6
/**
7
 * This GLSL library handles routines converting
8
 * float32 to/from Unsigned byte or float 16
9
 */
10
export class EncodingGlslLib extends GlslLib {
11
  constructor(context: GlslContext) {
12
    super(context);
13
  }
14
  getFunctions(): { [name: string]: GlslLibRoutine } {
15
    return { ...this.encodeFloat32(), ...this.decodeFloat32() };
16
  }
17
  getCustomTypes(): { [name: string]: string } {
18
    return {};
19
  }
20
  protected encodeFloat32(): { [name: string]: GlslLibRoutine } {
21
    return {
22
      encode: new GlslLibRoutine(`highp vec4 encode(highp float f) {
23
        return vec4(f, 0.0, 0.0, 0.0);
24
      }
25
        `),
26
    };
27
  }
28
  protected decodeFloat32(): { [name: string]: GlslLibRoutine } {
29
    return {
30
      decode: new GlslLibRoutine(`highp float decode(highp vec4 rgba) {
31
        return rgba.r;
32
      }
33
        `),
34
    };
35
  }
36
  /**
37
   * returns the routine to encode encode a 32bit float to a vec4 (of unsigned bytes)
38
   * @credit: https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
39
   */
40
  protected encodeUint8(): { [name: string]: GlslLibRoutine } {
41
    const endianness = EncodingGlslLib.isLittleEndian() ? 'rgba.rgba=rgba.abgr;' : '';
42
    return {
43
      encode: new GlslLibRoutine(`
44
      highp vec4 encode(highp float f) {
45
        highp float F = abs(f);
46
        highp float Sign = step(0.0,-f);
47
        highp float Exponent = floor(log2(F));
48
        highp float Mantissa = (exp2(- Exponent) * F);
49
        Exponent = floor(log2(F) + 127.0) + floor(log2(Mantissa));
50
        highp vec4 rgba;
51
        rgba[0] = 128.0 * Sign  + floor(Exponent*exp2(-1.0));
52
        rgba[1] = 128.0 * mod(Exponent,2.0) + mod(floor(Mantissa*128.0),128.0);
53
        rgba[2] = floor(mod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0)));
54
        rgba[3] = floor(exp2(23.0)*mod(Mantissa,exp2(-15.0)));
55
        ${endianness}
56
        rgba = rgba / 255.0; // values need to be normalized to [0,1]
57
        return rgba;
58
    }
59
        `),
60
    };
61
  }
62
  /**
63
   * returns the routine to encode a vec4 of unsigned bytes to float32
64
   * @credit: https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
65
   */
66
  protected decodeUint8(): { [name: string]: GlslLibRoutine } {
67
    const endianness = EncodingGlslLib.isLittleEndian() ? 'rgba.rgba=rgba.abgr;' : '';
68
    return {
69
      decode: new GlslLibRoutine(`
70
        highp float decode(highp vec4 rgba) {
71
          rgba = rgba * 255.0; // values need to be de-normalized from [0,1] to [0,255]
72
          ${endianness}
73
          highp float Sign = 1.0 - step(128.0,rgba[0])*2.0;
74
          highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;
75
          highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);
76
          highp float Result =  Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 ));
77
          return Result;
78
      }
79
        `),
80
    };
81
  }
82
  /**
83
   * Determines if the machine is little endian or not
84
   * @credit: https://gist.github.com/TooTallNate/4750953
85
   */
86
  static isLittleEndian(): boolean {
87
    const b = new ArrayBuffer(4);
88
    const a = new Uint32Array(b);
89
    const c = new Uint8Array(b);
90
    a[0] = 0xdeadbeef;
91
    if (c[0] === 0xef) {
92
      return true;
93
    }
94
    if (c[0] === 0xde) {
95
      return false;
96
    }
97
    throw new Error('unknown endianness');
98
  }
99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.