• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antvis / L7 / 9174082321

21 May 2024 11:41AM UTC coverage: 45.293%. First build
9174082321

Pull #2490

github

web-flow
Merge f68dd56aa into 8b72df14d
Pull Request #2490: fix: raster with luminance in webgl1

2345 of 7502 branches covered (31.26%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

9177 of 17937 relevant lines covered (51.16%)

106.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.36
/packages/renderer/src/device/DeviceTexture2D.ts
1
import type { Device, Sampler, Texture } from '@antv/g-device-api';
2
import {
40✔
3
  TextureUsage as DeviceTextureUsage,
4
  FilterMode,
5
  Format,
6
  MipmapFilterMode,
7
} from '@antv/g-device-api';
8
import type { ITexture2D, ITexture2DInitializationOptions } from '@antv/l7-core';
9
import { TextureUsage, gl } from '@antv/l7-core';
40✔
10
import { wrapModeMap } from './constants';
40✔
11
import { extend3ChannelsTo4 } from './utils/typedarray';
40✔
12

13
export function isTexture2D(t: any): t is ITexture2D {
40✔
14
  return !!(t && t['texture']);
×
15
}
16

17
export default class DeviceTexture2D implements ITexture2D {
40✔
18
  private texture: Texture;
19
  private sampler: Sampler;
20
  private width: number;
21
  private height: number;
22
  private isDestroy: boolean = false;
×
23

24
  constructor(
25
    private device: Device,
×
26
    private options: ITexture2DInitializationOptions,
×
27
  ) {
28
    const {
29
      wrapS = gl.CLAMP_TO_EDGE,
×
30
      wrapT = gl.CLAMP_TO_EDGE,
×
31
      aniso,
32
      mag = gl.NEAREST,
×
33
      min = gl.NEAREST,
×
34
    } = options;
×
35

36
    this.createTexture(options);
×
37

38
    this.sampler = device.createSampler({
×
39
      addressModeU: wrapModeMap[wrapS],
40
      addressModeV: wrapModeMap[wrapT],
41
      minFilter: min === gl.NEAREST ? FilterMode.POINT : FilterMode.BILINEAR,
×
42
      magFilter: mag === gl.NEAREST ? FilterMode.POINT : FilterMode.BILINEAR,
×
43
      mipmapFilter: MipmapFilterMode.NO_MIP,
44
      // lodMinClamp: 0,
45
      // lodMaxClamp: 0,
46
      maxAnisotropy: aniso,
47
    });
48
  }
49

50
  private createTexture(options: ITexture2DInitializationOptions) {
51
    const {
52
      type = gl.UNSIGNED_BYTE,
×
53
      width,
54
      height,
55
      flipY = false,
×
56
      format = gl.RGBA,
×
57
      alignment = 1,
×
58
      usage = TextureUsage.SAMPLED,
×
59
      // premultiplyAlpha = false,
60
      unorm = false,
×
61
      // colorSpace = gl.BROWSER_DEFAULT_WEBGL,
62
      // x = 0,
63
      // y = 0,
64
      // copy = false,
65
      label,
66
    } = options;
×
67
    let { data } = options;
×
68

69
    this.width = width;
×
70
    this.height = height;
×
71

72
    let pixelFormat: Format = Format.U8_RGBA_RT;
×
73
    if (type === gl.UNSIGNED_BYTE && format === gl.RGBA) {
×
74
      pixelFormat = unorm ? Format.U8_RGBA_NORM : Format.U8_RGBA_RT;
×
75
    } else if (type === gl.UNSIGNED_BYTE && format === gl.LUMINANCE) {
×
76
      pixelFormat = Format.U8_LUMINANCE;
×
NEW
77
    } else if (type === gl.FLOAT && format === gl.LUMINANCE) {
×
NEW
78
      pixelFormat = Format.F32_LUMINANCE;
×
79
    } else if (type === gl.FLOAT && format === gl.RGB) {
×
80
      // @see https://github.com/antvis/L7/pull/2262
81
      if (this.device.queryVendorInfo().platformString === 'WebGPU') {
×
82
        if (data) {
×
83
          // @ts-ignore
84
          data = extend3ChannelsTo4(data as unknown as Float32Array, 0);
×
85
        }
86
        pixelFormat = Format.F32_RGBA;
×
87
      } else {
88
        pixelFormat = Format.F32_RGB;
×
89
      }
90
    } else if (type === gl.FLOAT && format === gl.RGBA) {
×
91
      pixelFormat = Format.F32_RGBA;
×
92
    } else if (type === gl.FLOAT && format === gl.RED) {
×
93
      pixelFormat = Format.F32_R;
×
94
    } else {
95
      throw new Error(`create texture error, type: ${type}, format: ${format}`);
×
96
    }
97

98
    this.texture = this.device.createTexture({
×
99
      format: pixelFormat!,
100
      width,
101
      height,
102
      usage:
103
        usage === TextureUsage.SAMPLED
×
104
          ? DeviceTextureUsage.SAMPLED
105
          : DeviceTextureUsage.RENDER_TARGET,
106
      pixelStore: {
107
        unpackFlipY: flipY,
108
        packAlignment: alignment,
109
      },
110
      // mipLevelCount: usage === TextureUsage.RENDER_TARGET ? 1 : mipmap ? 1 : 0,
111
      mipLevelCount: 1,
112
    });
113
    if (label) {
×
114
      this.device.setResourceName(this.texture, label);
×
115
    }
116

117
    if (data) {
×
118
      // @ts-ignore
119
      this.texture.setImageData([data]);
×
120
    }
121
  }
122

123
  get() {
124
    return this.texture;
×
125
  }
126

127
  update(props: any) {
128
    const { data } = props;
×
129
    this.texture.setImageData([data]);
×
130
  }
131

132
  bind() {
133
    // this.texture._texture.bind();
134
  }
135

136
  resize({ width, height }: { width: number; height: number }): void {
137
    if (this.width !== width || this.height !== height) {
×
138
      this.destroy();
×
139
    }
140

141
    this.options.width = width;
×
142
    this.options.height = height;
×
143

144
    this.createTexture(this.options);
×
145

146
    this.isDestroy = false;
×
147
  }
148

149
  getSize(): [number, number] {
150
    return [this.width, this.height];
×
151
  }
152

153
  destroy() {
154
    // @ts-ignore
155
    if (!this.isDestroy && !this.texture.destroyed) {
×
156
      this.texture?.destroy();
×
157
    }
158
    this.isDestroy = true;
×
159
  }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc