@luma.gl/core 9.0.0-alpha.9 → 9.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -1
- package/README.md +4 -4
- package/dist/adapter/attribute-utils/get-attribute-from-layouts.d.ts +52 -0
- package/dist/adapter/attribute-utils/get-attribute-from-layouts.d.ts.map +1 -0
- package/dist/adapter/attribute-utils/get-attribute-from-layouts.js +130 -0
- package/dist/adapter/attribute-utils/get-attribute-from-layouts.js.map +1 -0
- package/dist/adapter/canvas-context.d.ts +109 -0
- package/dist/adapter/canvas-context.d.ts.map +1 -0
- package/dist/adapter/canvas-context.js +254 -0
- package/dist/adapter/canvas-context.js.map +1 -0
- package/dist/adapter/device.d.ts +219 -0
- package/dist/adapter/device.d.ts.map +1 -0
- package/dist/adapter/device.js +99 -0
- package/dist/adapter/device.js.map +1 -0
- package/dist/adapter/resources/buffer.d.ts +56 -0
- package/dist/adapter/resources/buffer.d.ts.map +1 -0
- package/dist/adapter/resources/buffer.js +62 -0
- package/dist/adapter/resources/buffer.js.map +1 -0
- package/dist/adapter/resources/command-buffer.d.ts +12 -0
- package/dist/adapter/resources/command-buffer.d.ts.map +1 -0
- package/dist/adapter/resources/command-buffer.js +15 -0
- package/dist/adapter/resources/command-buffer.js.map +1 -0
- package/dist/adapter/resources/command-encoder.d.ts +113 -0
- package/dist/adapter/resources/command-encoder.d.ts.map +1 -0
- package/dist/adapter/resources/command-encoder.js +19 -0
- package/dist/adapter/resources/command-encoder.js.map +1 -0
- package/dist/adapter/resources/compute-pass.d.ts +31 -0
- package/dist/adapter/resources/compute-pass.d.ts.map +1 -0
- package/dist/adapter/resources/compute-pass.js +15 -0
- package/dist/adapter/resources/compute-pass.js.map +1 -0
- package/dist/adapter/resources/compute-pipeline.d.ts +24 -0
- package/dist/adapter/resources/compute-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/compute-pipeline.js +20 -0
- package/dist/adapter/resources/compute-pipeline.js.map +1 -0
- package/dist/adapter/resources/external-texture.d.ts +12 -0
- package/dist/adapter/resources/external-texture.d.ts.map +1 -0
- package/dist/adapter/resources/external-texture.js +17 -0
- package/dist/adapter/resources/external-texture.js.map +1 -0
- package/dist/adapter/resources/framebuffer.d.ts +50 -0
- package/dist/adapter/resources/framebuffer.d.ts.map +1 -0
- package/dist/adapter/resources/framebuffer.js +102 -0
- package/dist/adapter/resources/framebuffer.js.map +1 -0
- package/dist/adapter/resources/render-pass.d.ts +51 -0
- package/dist/adapter/resources/render-pass.d.ts.map +1 -0
- package/dist/adapter/resources/render-pass.js +23 -0
- package/dist/adapter/resources/render-pass.js.map +1 -0
- package/dist/adapter/resources/render-pipeline.d.ts +89 -0
- package/dist/adapter/resources/render-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/render-pipeline.js +36 -0
- package/dist/adapter/resources/render-pipeline.js.map +1 -0
- package/dist/adapter/resources/resource.d.ts +73 -0
- package/dist/adapter/resources/resource.d.ts.map +1 -0
- package/dist/adapter/resources/resource.js +101 -0
- package/dist/adapter/resources/resource.js.map +1 -0
- package/dist/adapter/resources/sampler.d.ts +42 -0
- package/dist/adapter/resources/sampler.d.ts.map +1 -0
- package/dist/adapter/resources/sampler.js +26 -0
- package/dist/adapter/resources/sampler.js.map +1 -0
- package/dist/adapter/resources/shader.d.ts +45 -0
- package/dist/adapter/resources/shader.d.ts.map +1 -0
- package/dist/adapter/resources/shader.js +90 -0
- package/dist/adapter/resources/shader.js.map +1 -0
- package/dist/adapter/resources/texture.d.ts +90 -0
- package/dist/adapter/resources/texture.d.ts.map +1 -0
- package/dist/adapter/resources/texture.js +45 -0
- package/dist/adapter/resources/texture.js.map +1 -0
- package/dist/adapter/resources/transform-feedback.d.ts +30 -0
- package/dist/adapter/resources/transform-feedback.d.ts.map +1 -0
- package/dist/adapter/resources/transform-feedback.js +17 -0
- package/dist/adapter/resources/transform-feedback.js.map +1 -0
- package/dist/adapter/resources/vertex-array.d.ts +40 -0
- package/dist/adapter/resources/vertex-array.d.ts.map +1 -0
- package/dist/adapter/resources/vertex-array.js +24 -0
- package/dist/adapter/resources/vertex-array.js.map +1 -0
- package/dist/adapter/type-utils/decode-attribute-type.d.ts +20 -0
- package/dist/adapter/type-utils/decode-attribute-type.d.ts.map +1 -0
- package/dist/adapter/type-utils/decode-attribute-type.js +60 -0
- package/dist/adapter/type-utils/decode-attribute-type.js.map +1 -0
- package/dist/adapter/type-utils/decode-data-type.d.ts +16 -0
- package/dist/adapter/type-utils/decode-data-type.d.ts.map +1 -0
- package/dist/adapter/type-utils/decode-data-type.js +43 -0
- package/dist/adapter/type-utils/decode-data-type.js.map +1 -0
- package/dist/adapter/type-utils/decode-shader-types.d.ts +9 -0
- package/dist/adapter/type-utils/decode-shader-types.d.ts.map +1 -0
- package/dist/adapter/type-utils/decode-shader-types.js +103 -0
- package/dist/adapter/type-utils/decode-shader-types.js.map +1 -0
- package/dist/adapter/type-utils/decode-texture-format.d.ts +19 -0
- package/dist/adapter/type-utils/decode-texture-format.d.ts.map +1 -0
- package/dist/adapter/type-utils/decode-texture-format.js +101 -0
- package/dist/adapter/type-utils/decode-texture-format.js.map +1 -0
- package/dist/adapter/type-utils/decode-vertex-format.d.ts +22 -0
- package/dist/adapter/type-utils/decode-vertex-format.d.ts.map +1 -0
- package/dist/adapter/type-utils/decode-vertex-format.js +25 -0
- package/dist/adapter/type-utils/decode-vertex-format.js.map +1 -0
- package/dist/adapter/type-utils/vertex-format-from-attribute.d.ts +10 -0
- package/dist/adapter/type-utils/vertex-format-from-attribute.d.ts.map +1 -0
- package/dist/adapter/type-utils/vertex-format-from-attribute.js +76 -0
- package/dist/adapter/type-utils/vertex-format-from-attribute.js.map +1 -0
- package/dist/adapter/type-utils/wgsl-utils.d.ts +4 -0
- package/dist/adapter/type-utils/wgsl-utils.d.ts.map +1 -0
- package/dist/adapter/type-utils/wgsl-utils.js +15 -0
- package/dist/adapter/type-utils/wgsl-utils.js.map +1 -0
- package/dist/adapter/types/accessor.d.ts +23 -0
- package/dist/adapter/types/accessor.d.ts.map +1 -0
- package/dist/adapter/types/accessor.js +2 -0
- package/dist/adapter/types/accessor.js.map +1 -0
- package/dist/adapter/types/buffer-layout.d.ts +59 -0
- package/dist/adapter/types/buffer-layout.d.ts.map +1 -0
- package/dist/adapter/types/buffer-layout.js +2 -0
- package/dist/adapter/types/buffer-layout.js.map +1 -0
- package/dist/adapter/types/parameters.d.ts +97 -0
- package/dist/adapter/types/parameters.d.ts.map +1 -0
- package/dist/adapter/types/parameters.js +28 -0
- package/dist/adapter/types/parameters.js.map +1 -0
- package/dist/adapter/types/shader-layout.d.ts +145 -0
- package/dist/adapter/types/shader-layout.d.ts.map +1 -0
- package/dist/adapter/types/shader-layout.js +2 -0
- package/dist/adapter/types/shader-layout.js.map +1 -0
- package/dist/adapter/types/shader-types.d.ts +21 -0
- package/dist/adapter/types/shader-types.d.ts.map +1 -0
- package/dist/adapter/types/shader-types.js +2 -0
- package/dist/adapter/types/shader-types.js.map +1 -0
- package/dist/adapter/types/texture-formats.d.ts +12 -0
- package/dist/adapter/types/texture-formats.d.ts.map +1 -0
- package/dist/adapter/types/texture-formats.js +2 -0
- package/dist/adapter/types/texture-formats.js.map +1 -0
- package/dist/adapter/types/types.d.ts +90 -0
- package/dist/adapter/types/types.d.ts.map +1 -0
- package/dist/adapter/types/types.js +2 -0
- package/dist/adapter/types/types.js.map +1 -0
- package/dist/adapter/types/vertex-formats.d.ts +14 -0
- package/dist/adapter/types/vertex-formats.d.ts.map +1 -0
- package/dist/adapter/types/vertex-formats.js +2 -0
- package/dist/adapter/types/vertex-formats.js.map +1 -0
- package/dist/dist.dev.js +2976 -0
- package/dist/index.cjs +2445 -0
- package/dist/index.d.ts +81 -128
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -18
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +5 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +24 -0
- package/dist/init.js.map +1 -0
- package/dist/lib/compiler-log/compiler-message.d.ts +8 -0
- package/dist/lib/compiler-log/compiler-message.d.ts.map +1 -0
- package/dist/lib/compiler-log/compiler-message.js +2 -0
- package/dist/lib/compiler-log/compiler-message.js.map +1 -0
- package/dist/lib/compiler-log/format-compiler-log.d.ts +8 -0
- package/dist/lib/compiler-log/format-compiler-log.d.ts.map +1 -0
- package/dist/lib/compiler-log/format-compiler-log.js +64 -0
- package/dist/lib/compiler-log/format-compiler-log.js.map +1 -0
- package/dist/lib/compiler-log/get-shader-info.d.ts +9 -0
- package/dist/lib/compiler-log/get-shader-info.d.ts.map +1 -0
- package/dist/lib/compiler-log/get-shader-info.js +25 -0
- package/dist/lib/compiler-log/get-shader-info.js.map +1 -0
- package/dist/lib/luma.d.ts +22 -0
- package/dist/lib/luma.d.ts.map +1 -0
- package/dist/lib/luma.js +63 -0
- package/dist/lib/luma.js.map +1 -0
- package/dist/lib/uniforms/uniform-block.d.ts +29 -0
- package/dist/lib/uniforms/uniform-block.d.ts.map +1 -0
- package/dist/lib/uniforms/uniform-block.js +48 -0
- package/dist/lib/uniforms/uniform-block.js.map +1 -0
- package/dist/lib/uniforms/uniform-buffer-layout.d.ts +27 -0
- package/dist/lib/uniforms/uniform-buffer-layout.d.ts.map +1 -0
- package/dist/lib/uniforms/uniform-buffer-layout.js +76 -0
- package/dist/lib/uniforms/uniform-buffer-layout.js.map +1 -0
- package/dist/lib/uniforms/uniform-store.d.ts +62 -0
- package/dist/lib/uniforms/uniform-store.d.ts.map +1 -0
- package/dist/lib/uniforms/uniform-store.js +89 -0
- package/dist/lib/uniforms/uniform-store.js.map +1 -0
- package/dist/lib/uniforms/uniform.d.ts +10 -0
- package/dist/lib/uniforms/uniform.d.ts.map +1 -0
- package/dist/lib/uniforms/uniform.js +20 -0
- package/dist/lib/uniforms/uniform.js.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/array-equal.d.ts +5 -0
- package/dist/utils/array-equal.d.ts.map +1 -0
- package/dist/utils/array-equal.js +28 -0
- package/dist/utils/array-equal.js.map +1 -0
- package/dist/utils/array-utils-flat.d.ts +10 -0
- package/dist/utils/array-utils-flat.d.ts.map +1 -0
- package/dist/utils/array-utils-flat.js +36 -0
- package/dist/utils/array-utils-flat.js.map +1 -0
- package/dist/utils/assert.d.ts +2 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/assert.js +6 -0
- package/dist/utils/assert.js.map +1 -0
- package/dist/utils/cast.d.ts +3 -0
- package/dist/utils/cast.d.ts.map +1 -0
- package/dist/utils/cast.js +4 -0
- package/dist/utils/cast.js.map +1 -0
- package/dist/utils/check-props.d.ts +7 -0
- package/dist/utils/check-props.d.ts.map +1 -0
- package/dist/utils/check-props.js +32 -0
- package/dist/utils/check-props.js.map +1 -0
- package/dist/utils/deep-equal.d.ts +9 -0
- package/dist/utils/deep-equal.d.ts.map +1 -0
- package/dist/utils/deep-equal.js +40 -0
- package/dist/utils/deep-equal.js.map +1 -0
- package/dist/utils/format-value.d.ts +7 -0
- package/dist/utils/format-value.d.ts.map +1 -0
- package/dist/utils/format-value.js +42 -0
- package/dist/utils/format-value.js.map +1 -0
- package/dist/utils/is-array.d.ts +16 -0
- package/dist/utils/is-array.d.ts.map +1 -0
- package/dist/utils/is-array.js +10 -0
- package/dist/utils/is-array.js.map +1 -0
- package/dist/utils/load-file.d.ts +35 -0
- package/dist/utils/load-file.d.ts.map +1 -0
- package/dist/utils/load-file.js +48 -0
- package/dist/utils/load-file.js.map +1 -0
- package/dist/utils/log.d.ts +4 -0
- package/dist/utils/log.d.ts.map +1 -0
- package/dist/utils/log.js +5 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/utils/random.d.ts +5 -0
- package/dist/utils/random.d.ts.map +1 -0
- package/dist/utils/random.js +14 -0
- package/dist/utils/random.js.map +1 -0
- package/dist/utils/request-animation-frame.d.ts +3 -0
- package/dist/utils/request-animation-frame.d.ts.map +1 -0
- package/dist/utils/request-animation-frame.js +7 -0
- package/dist/utils/request-animation-frame.js.map +1 -0
- package/dist/utils/stats-manager.d.ts +12 -0
- package/dist/utils/stats-manager.d.ts.map +1 -0
- package/dist/utils/stats-manager.js +19 -0
- package/dist/utils/stats-manager.js.map +1 -0
- package/dist/utils/stub-methods.d.ts +2 -0
- package/dist/utils/stub-methods.d.ts.map +1 -0
- package/dist/utils/stub-methods.js +16 -0
- package/dist/utils/stub-methods.js.map +1 -0
- package/dist/utils/utils.d.ts +15 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +19 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist.min.js +33 -0
- package/package.json +17 -12
- package/src/adapter/attribute-utils/get-attribute-from-layouts.ts +259 -0
- package/src/adapter/canvas-context.ts +433 -0
- package/src/adapter/device.ts +435 -0
- package/src/adapter/resources/buffer.ts +106 -0
- package/src/adapter/resources/command-buffer.ts +38 -0
- package/src/adapter/resources/command-encoder.ts +172 -0
- package/src/adapter/resources/compute-pass.ts +52 -0
- package/src/adapter/resources/compute-pipeline.ts +39 -0
- package/src/adapter/resources/external-texture.ts +20 -0
- package/src/adapter/resources/framebuffer.ts +230 -0
- package/src/adapter/resources/render-pass.ts +117 -0
- package/src/adapter/resources/render-pipeline.ts +133 -0
- package/src/adapter/resources/resource.ts +176 -0
- package/src/adapter/resources/sampler.ts +69 -0
- package/src/adapter/resources/shader.ts +143 -0
- package/src/adapter/resources/texture.ts +140 -0
- package/src/adapter/resources/transform-feedback.ts +45 -0
- package/src/adapter/resources/vertex-array.ts +65 -0
- package/src/adapter/type-utils/decode-attribute-type.ts +82 -0
- package/src/adapter/type-utils/decode-data-type.ts +62 -0
- package/src/adapter/type-utils/decode-shader-types.ts +48 -0
- package/src/adapter/type-utils/decode-texture-format.ts +190 -0
- package/src/adapter/type-utils/decode-vertex-format.ts +49 -0
- package/src/adapter/type-utils/vertex-format-from-attribute.ts +103 -0
- package/src/adapter/type-utils/wgsl-utils.ts +18 -0
- package/src/adapter/types/accessor.ts +34 -0
- package/src/adapter/types/buffer-layout.ts +62 -0
- package/src/adapter/types/parameters.ts +250 -0
- package/src/adapter/types/shader-layout.ts +193 -0
- package/src/adapter/types/shader-types.ts +77 -0
- package/src/adapter/types/texture-formats.ts +168 -0
- package/src/adapter/types/types.ts +109 -0
- package/src/adapter/types/vertex-formats.ts +91 -0
- package/src/index.ts +160 -92
- package/src/init.ts +47 -0
- package/src/lib/compiler-log/compiler-message.ts +10 -0
- package/src/lib/compiler-log/format-compiler-log.ts +113 -0
- package/src/lib/compiler-log/get-shader-info.ts +41 -0
- package/src/lib/luma.ts +84 -0
- package/src/lib/uniforms/uniform-block.ts +80 -0
- package/src/lib/uniforms/uniform-buffer-layout.ts +109 -0
- package/src/lib/uniforms/uniform-store.ts +173 -0
- package/src/lib/uniforms/uniform.ts +25 -0
- package/src/types.ts +33 -0
- package/src/utils/array-equal.ts +33 -0
- package/src/utils/array-utils-flat.ts +43 -0
- package/src/utils/assert.ts +7 -0
- package/src/utils/cast.ts +4 -0
- package/src/utils/check-props.ts +74 -0
- package/src/utils/deep-equal.ts +47 -0
- package/src/utils/format-value.ts +40 -0
- package/src/utils/is-array.ts +24 -0
- package/src/utils/load-file.ts +89 -0
- package/src/utils/log.ts +4 -0
- package/src/utils/random.ts +17 -0
- package/src/utils/request-animation-frame.ts +15 -0
- package/src/utils/stats-manager.ts +23 -0
- package/src/utils/stub-methods.ts +20 -0
- package/src/utils/utils.ts +36 -0
- package/dist/bundle.d.ts +0 -2
- package/dist/bundle.d.ts.map +0 -1
- package/dist/bundle.js +0 -5
- package/dist/bundle.js.map +0 -1
- package/src/bundle.ts +0 -4
package/LICENSE
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
luma.gl is provided under the MIT license
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 vis.gl contributors
|
|
2
4
|
|
|
3
5
|
This software includes parts initially developed by Uber and open sourced under MIT license.
|
|
4
6
|
Copyright (c) 2015 Uber Technologies, Inc.
|
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
# luma.gl
|
|
1
|
+
# @luma.gl/core
|
|
2
2
|
|
|
3
3
|
<h1 align="center">luma.gl | <a href="https://luma.gl">Docs</a></h1>
|
|
4
4
|
|
|
5
|
-
<h5 align="center">luma.gl: WebGL2 Components for
|
|
5
|
+
<h5 align="center">luma.gl: WebGPU and WebGL2 Components for GPU Visualization and Compute</h5>
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
|
-
luma.gl is a set of WebGL2 components for high-performance GPU-based rendering and computation in the browser.
|
|
9
|
+
luma.gl is a set of WebGPU and WebGL2 components for high-performance GPU-based rendering and computation in the browser.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
luma.gl is the GPU engine that powers applications and frameworks like
|
|
12
12
|
|
|
13
13
|
- [kepler.gl](https://github.com/keplergl/kepler.gl) WebGL based visualization overlays
|
|
14
14
|
- [deck.gl](https://github.com/visgl/deck.gl) WebGL based visualization overlays
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ShaderLayout } from '../types/shader-layout';
|
|
2
|
+
import type { BufferLayout } from '../types/buffer-layout';
|
|
3
|
+
import type { ShaderDataType, ShaderAttributeType } from '../types/shader-types';
|
|
4
|
+
import type { VertexFormat, VertexType } from '../types/vertex-formats';
|
|
5
|
+
/** Resolved info for a buffer / attribute combination to help backend configure it correctly */
|
|
6
|
+
export type AttributeInfo = {
|
|
7
|
+
/** Attribute name */
|
|
8
|
+
attributeName: string;
|
|
9
|
+
/** Location in shader */
|
|
10
|
+
location: number;
|
|
11
|
+
/** Type / precision used in shader (buffer values may be converted) */
|
|
12
|
+
shaderType: ShaderAttributeType;
|
|
13
|
+
/** Calculations are done in this type in the shader's attribute declaration */
|
|
14
|
+
shaderDataType: ShaderDataType;
|
|
15
|
+
/** Components refer to the number of components in the shader's attribute declaration */
|
|
16
|
+
shaderComponents: 1 | 2 | 3 | 4;
|
|
17
|
+
/** It is the shader attribute declaration that determines whether GPU will process as integer or float */
|
|
18
|
+
integer: boolean;
|
|
19
|
+
/** BufferName */
|
|
20
|
+
bufferName: string;
|
|
21
|
+
/** Format of buffer data */
|
|
22
|
+
vertexFormat: VertexFormat;
|
|
23
|
+
/** Memory data type refers to the data type in the buffer */
|
|
24
|
+
bufferDataType: VertexType;
|
|
25
|
+
/** Components refer to the number of components in the buffer's vertex format */
|
|
26
|
+
bufferComponents: 1 | 2 | 3 | 4;
|
|
27
|
+
/** Normalization is encoded in the buffer layout's vertex format... */
|
|
28
|
+
normalized: boolean;
|
|
29
|
+
/** If not specified, the step mode is inferred from the attribute name in the shader (contains string instance) */
|
|
30
|
+
stepMode: 'vertex' | 'instance';
|
|
31
|
+
/** The byteOffset is encoded in or calculated from the buffer layout */
|
|
32
|
+
byteOffset: number;
|
|
33
|
+
/** The byteStride is encoded in or calculated from the buffer layout */
|
|
34
|
+
byteStride: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Map from "attribute names" to "resolved attribute infos"
|
|
38
|
+
* containing information about both buffer layouts and shader attribute declarations
|
|
39
|
+
*/
|
|
40
|
+
export declare function getAttributeInfosFromLayouts(shaderLayout: ShaderLayout, bufferLayout: BufferLayout[]): Record<string, AttributeInfo>;
|
|
41
|
+
/**
|
|
42
|
+
* Array indexed by "location" holding "resolved attribute infos"
|
|
43
|
+
*/
|
|
44
|
+
export declare function getAttributeInfosByLocation(shaderLayout: ShaderLayout, bufferLayout: BufferLayout[], maxVertexAttributes?: number): AttributeInfo[];
|
|
45
|
+
/**
|
|
46
|
+
* Merges an provided shader layout into a base shader layout
|
|
47
|
+
* In WebGL, this allows the auto generated shader layout to be overridden by the application
|
|
48
|
+
* Typically to change the format of the vertex attributes (from float32x4 to uint8x4 etc).
|
|
49
|
+
* @todo Drop this? Aren't all use cases covered by mergeBufferLayout()?
|
|
50
|
+
*/
|
|
51
|
+
export declare function mergeShaderLayout(baseLayout: ShaderLayout, overrideLayout: ShaderLayout): ShaderLayout;
|
|
52
|
+
//# sourceMappingURL=get-attribute-from-layouts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-attribute-from-layouts.d.ts","sourceRoot":"","sources":["../../../src/adapter/attribute-utils/get-attribute-from-layouts.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAuB,MAAM,wBAAwB,CAAC;AAC/E,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAC,cAAc,EAAE,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAE/E,OAAO,KAAK,EAAC,YAAY,EAAE,UAAU,EAAC,MAAM,yBAAyB,CAAC;AAGtE,gGAAgG;AAChG,MAAM,MAAM,aAAa,GAAG;IAC1B,qBAAqB;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,yBAAyB;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,UAAU,EAAE,mBAAmB,CAAC;IAChC,+EAA+E;IAC/E,cAAc,EAAE,cAAc,CAAC;IAC/B,yFAAyF;IACzF,gBAAgB,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChC,0GAA0G;IAC1G,OAAO,EAAE,OAAO,CAAC;IAEjB,iBAAiB;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,4BAA4B;IAC5B,YAAY,EAAE,YAAY,CAAC;IAC3B,6DAA6D;IAC7D,cAAc,EAAE,UAAU,CAAC;IAC3B,iFAAiF;IACjF,gBAAgB,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChC,uEAAuE;IACvE,UAAU,EAAE,OAAO,CAAC;IAEpB,mHAAmH;IACnH,QAAQ,EAAE,QAAQ,GAAG,UAAU,CAAC;IAEhC,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;IACnB,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAWF;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,YAAY,EAAE,YAAY,EAC1B,YAAY,EAAE,YAAY,EAAE,GAC3B,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAU/B;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,YAAY,EAAE,YAAY,EAC1B,YAAY,EAAE,YAAY,EAAE,EAC5B,mBAAmB,GAAE,MAAW,GAC/B,aAAa,EAAE,CAOjB;AAgJD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,YAAY,EACxB,cAAc,EAAE,YAAY,GAC3B,YAAY,CAiBd"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { log } from "../../utils/log.js";
|
|
2
|
+
import { decodeShaderAttributeType } from "../type-utils/decode-attribute-type.js";
|
|
3
|
+
import { decodeVertexFormat } from "../type-utils/decode-vertex-format.js";
|
|
4
|
+
export function getAttributeInfosFromLayouts(shaderLayout, bufferLayout) {
|
|
5
|
+
const attributeInfos = {};
|
|
6
|
+
for (const attribute of shaderLayout.attributes) {
|
|
7
|
+
attributeInfos[attribute.name] = getAttributeInfoFromLayouts(shaderLayout, bufferLayout, attribute.name);
|
|
8
|
+
}
|
|
9
|
+
return attributeInfos;
|
|
10
|
+
}
|
|
11
|
+
export function getAttributeInfosByLocation(shaderLayout, bufferLayout) {
|
|
12
|
+
let maxVertexAttributes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 16;
|
|
13
|
+
const attributeInfos = getAttributeInfosFromLayouts(shaderLayout, bufferLayout);
|
|
14
|
+
const locationInfos = new Array(maxVertexAttributes).fill(null);
|
|
15
|
+
for (const attributeInfo of Object.values(attributeInfos)) {
|
|
16
|
+
locationInfos[attributeInfo.location] = attributeInfo;
|
|
17
|
+
}
|
|
18
|
+
return locationInfos;
|
|
19
|
+
}
|
|
20
|
+
function getAttributeInfoFromLayouts(shaderLayout, bufferLayout, name) {
|
|
21
|
+
const shaderDeclaration = getAttributeFromShaderLayout(shaderLayout, name);
|
|
22
|
+
const bufferMapping = getAttributeFromBufferLayout(bufferLayout, name);
|
|
23
|
+
if (!shaderDeclaration) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const attributeTypeInfo = decodeShaderAttributeType(shaderDeclaration.type);
|
|
27
|
+
const vertexFormat = (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.vertexFormat) || attributeTypeInfo.defaultVertexFormat;
|
|
28
|
+
const vertexFormatInfo = decodeVertexFormat(vertexFormat);
|
|
29
|
+
return {
|
|
30
|
+
attributeName: (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.attributeName) || shaderDeclaration.name,
|
|
31
|
+
bufferName: (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.bufferName) || shaderDeclaration.name,
|
|
32
|
+
location: shaderDeclaration.location,
|
|
33
|
+
shaderType: shaderDeclaration.type,
|
|
34
|
+
shaderDataType: attributeTypeInfo.dataType,
|
|
35
|
+
shaderComponents: attributeTypeInfo.components,
|
|
36
|
+
vertexFormat,
|
|
37
|
+
bufferDataType: vertexFormatInfo.type,
|
|
38
|
+
bufferComponents: vertexFormatInfo.components,
|
|
39
|
+
normalized: vertexFormatInfo.normalized,
|
|
40
|
+
integer: attributeTypeInfo.integer,
|
|
41
|
+
stepMode: (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.stepMode) || shaderDeclaration.stepMode,
|
|
42
|
+
byteOffset: (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.byteOffset) || 0,
|
|
43
|
+
byteStride: (bufferMapping === null || bufferMapping === void 0 ? void 0 : bufferMapping.byteStride) || 0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function getAttributeFromShaderLayout(shaderLayout, name) {
|
|
47
|
+
const attribute = shaderLayout.attributes.find(attr => attr.name === name);
|
|
48
|
+
if (!attribute) {
|
|
49
|
+
log.warn(`shader layout attribute "${name}" not present in shader`);
|
|
50
|
+
}
|
|
51
|
+
return attribute || null;
|
|
52
|
+
}
|
|
53
|
+
function getAttributeFromBufferLayout(bufferLayouts, name) {
|
|
54
|
+
checkBufferLayouts(bufferLayouts);
|
|
55
|
+
let bufferLayoutInfo = getAttributeFromShortHand(bufferLayouts, name);
|
|
56
|
+
if (bufferLayoutInfo) {
|
|
57
|
+
return bufferLayoutInfo;
|
|
58
|
+
}
|
|
59
|
+
bufferLayoutInfo = getAttributeFromAttributesList(bufferLayouts, name);
|
|
60
|
+
if (bufferLayoutInfo) {
|
|
61
|
+
return bufferLayoutInfo;
|
|
62
|
+
}
|
|
63
|
+
log.warn(`layout for attribute "${name}" not present in buffer layout`);
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
function checkBufferLayouts(bufferLayouts) {
|
|
67
|
+
for (const bufferLayout of bufferLayouts) {
|
|
68
|
+
if (bufferLayout.attributes && bufferLayout.format || !bufferLayout.attributes && !bufferLayout.format) {
|
|
69
|
+
log.warn(`BufferLayout ${name} must have either 'attributes' or 'format' field`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function getAttributeFromShortHand(bufferLayouts, name) {
|
|
74
|
+
for (const bufferLayout of bufferLayouts) {
|
|
75
|
+
if (bufferLayout.format && bufferLayout.name === name) {
|
|
76
|
+
return {
|
|
77
|
+
attributeName: bufferLayout.name,
|
|
78
|
+
bufferName: name,
|
|
79
|
+
stepMode: bufferLayout.stepMode,
|
|
80
|
+
vertexFormat: bufferLayout.format,
|
|
81
|
+
byteOffset: 0,
|
|
82
|
+
byteStride: bufferLayout.byteStride || 0
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
function getAttributeFromAttributesList(bufferLayouts, name) {
|
|
89
|
+
for (const bufferLayout of bufferLayouts) {
|
|
90
|
+
var _bufferLayout$attribu;
|
|
91
|
+
let byteStride = bufferLayout.byteStride;
|
|
92
|
+
if (typeof bufferLayout.byteStride !== 'number') {
|
|
93
|
+
for (const attributeMapping of bufferLayout.attributes || []) {
|
|
94
|
+
const info = decodeVertexFormat(attributeMapping.format);
|
|
95
|
+
byteStride += info.byteLength;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const attributeMapping = (_bufferLayout$attribu = bufferLayout.attributes) === null || _bufferLayout$attribu === void 0 ? void 0 : _bufferLayout$attribu.find(mapping => mapping.attribute === name);
|
|
99
|
+
if (attributeMapping) {
|
|
100
|
+
return {
|
|
101
|
+
attributeName: attributeMapping.attribute,
|
|
102
|
+
bufferName: bufferLayout.name,
|
|
103
|
+
stepMode: bufferLayout.stepMode,
|
|
104
|
+
vertexFormat: attributeMapping.format,
|
|
105
|
+
byteOffset: attributeMapping.byteOffset,
|
|
106
|
+
byteStride
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
export function mergeShaderLayout(baseLayout, overrideLayout) {
|
|
113
|
+
const mergedLayout = {
|
|
114
|
+
...baseLayout,
|
|
115
|
+
attributes: baseLayout.attributes.map(attribute => ({
|
|
116
|
+
...attribute
|
|
117
|
+
}))
|
|
118
|
+
};
|
|
119
|
+
for (const attribute of (overrideLayout === null || overrideLayout === void 0 ? void 0 : overrideLayout.attributes) || []) {
|
|
120
|
+
const baseAttribute = mergedLayout.attributes.find(attr => attr.name === attribute.name);
|
|
121
|
+
if (!baseAttribute) {
|
|
122
|
+
log.warn(`shader layout attribute ${attribute.name} not present in shader`);
|
|
123
|
+
} else {
|
|
124
|
+
baseAttribute.type = attribute.type || baseAttribute.type;
|
|
125
|
+
baseAttribute.stepMode = attribute.stepMode || baseAttribute.stepMode;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return mergedLayout;
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=get-attribute-from-layouts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-attribute-from-layouts.js","names":["log","decodeShaderAttributeType","decodeVertexFormat","getAttributeInfosFromLayouts","shaderLayout","bufferLayout","attributeInfos","attribute","attributes","name","getAttributeInfoFromLayouts","getAttributeInfosByLocation","maxVertexAttributes","arguments","length","undefined","locationInfos","Array","fill","attributeInfo","Object","values","location","shaderDeclaration","getAttributeFromShaderLayout","bufferMapping","getAttributeFromBufferLayout","attributeTypeInfo","type","vertexFormat","defaultVertexFormat","vertexFormatInfo","attributeName","bufferName","shaderType","shaderDataType","dataType","shaderComponents","components","bufferDataType","bufferComponents","normalized","integer","stepMode","byteOffset","byteStride","find","attr","warn","bufferLayouts","checkBufferLayouts","bufferLayoutInfo","getAttributeFromShortHand","getAttributeFromAttributesList","format","_bufferLayout$attribu","attributeMapping","info","byteLength","mapping","mergeShaderLayout","baseLayout","overrideLayout","mergedLayout","map","baseAttribute"],"sources":["../../../src/adapter/attribute-utils/get-attribute-from-layouts.ts"],"sourcesContent":["// luma.gl, MIT license\n// Copyright (c) vis.gl contributors\n\nimport {log} from '../../utils/log';\nimport type {ShaderLayout, AttributeDeclaration} from '../types/shader-layout';\nimport type {BufferLayout} from '../types/buffer-layout';\nimport type {ShaderDataType, ShaderAttributeType} from '../types/shader-types';\nimport {decodeShaderAttributeType} from '../type-utils/decode-attribute-type';\nimport type {VertexFormat, VertexType} from '../types/vertex-formats';\nimport {decodeVertexFormat} from '../type-utils/decode-vertex-format';\n\n/** Resolved info for a buffer / attribute combination to help backend configure it correctly */\nexport type AttributeInfo = {\n /** Attribute name */\n attributeName: string;\n /** Location in shader */\n location: number;\n /** Type / precision used in shader (buffer values may be converted) */\n shaderType: ShaderAttributeType;\n /** Calculations are done in this type in the shader's attribute declaration */\n shaderDataType: ShaderDataType;\n /** Components refer to the number of components in the shader's attribute declaration */\n shaderComponents: 1 | 2 | 3 | 4;\n /** It is the shader attribute declaration that determines whether GPU will process as integer or float */\n integer: boolean;\n\n /** BufferName */\n bufferName: string;\n /** Format of buffer data */\n vertexFormat: VertexFormat;\n /** Memory data type refers to the data type in the buffer */\n bufferDataType: VertexType;\n /** Components refer to the number of components in the buffer's vertex format */\n bufferComponents: 1 | 2 | 3 | 4;\n /** Normalization is encoded in the buffer layout's vertex format... */\n normalized: boolean;\n\n /** If not specified, the step mode is inferred from the attribute name in the shader (contains string instance) */\n stepMode: 'vertex' | 'instance';\n\n /** The byteOffset is encoded in or calculated from the buffer layout */\n byteOffset: number;\n /** The byteStride is encoded in or calculated from the buffer layout */\n byteStride: number;\n};\n\ntype BufferAttributeInfo = {\n attributeName: string;\n bufferName: string;\n stepMode?: 'vertex' | 'instance';\n vertexFormat: VertexFormat;\n byteOffset: number;\n byteStride: number;\n};\n\n/** \n * Map from \"attribute names\" to \"resolved attribute infos\" \n * containing information about both buffer layouts and shader attribute declarations\n */\nexport function getAttributeInfosFromLayouts(\n shaderLayout: ShaderLayout,\n bufferLayout: BufferLayout[]\n): Record<string, AttributeInfo> {\n const attributeInfos: Record<string, AttributeInfo> = {};\n for (const attribute of shaderLayout.attributes) {\n attributeInfos[attribute.name] = getAttributeInfoFromLayouts(\n shaderLayout,\n bufferLayout,\n attribute.name\n );\n }\n return attributeInfos;\n}\n\n/** \n * Array indexed by \"location\" holding \"resolved attribute infos\" \n */\nexport function getAttributeInfosByLocation(\n shaderLayout: ShaderLayout,\n bufferLayout: BufferLayout[],\n maxVertexAttributes: number = 16\n): AttributeInfo[] {\n const attributeInfos = getAttributeInfosFromLayouts(shaderLayout, bufferLayout);\n const locationInfos: AttributeInfo[] = new Array(maxVertexAttributes).fill(null);\n for (const attributeInfo of Object.values(attributeInfos)) {\n locationInfos[attributeInfo.location] = attributeInfo;\n }\n return locationInfos;\n}\n\n/**\n * Get the combined information from a shader layout and a buffer layout for a specific attribute\n */\nfunction getAttributeInfoFromLayouts(\n shaderLayout: ShaderLayout,\n bufferLayout: BufferLayout[],\n name: string\n): AttributeInfo | null {\n const shaderDeclaration = getAttributeFromShaderLayout(shaderLayout, name);\n const bufferMapping: BufferAttributeInfo = getAttributeFromBufferLayout(bufferLayout, name);\n\n // TODO should no longer happen\n if (!shaderDeclaration) {\n // || !bufferMapping\n return null;\n }\n\n const attributeTypeInfo = decodeShaderAttributeType(shaderDeclaration.type);\n const vertexFormat = bufferMapping?.vertexFormat || attributeTypeInfo.defaultVertexFormat;\n const vertexFormatInfo = decodeVertexFormat(vertexFormat);\n\n return {\n attributeName: bufferMapping?.attributeName || shaderDeclaration.name,\n bufferName: bufferMapping?.bufferName || shaderDeclaration.name,\n location: shaderDeclaration.location,\n shaderType: shaderDeclaration.type,\n shaderDataType: attributeTypeInfo.dataType,\n shaderComponents: attributeTypeInfo.components,\n vertexFormat,\n bufferDataType: vertexFormatInfo.type,\n bufferComponents: vertexFormatInfo.components,\n // normalized is a property of the buffer's vertex format\n normalized: vertexFormatInfo.normalized,\n // integer is a property of the shader declaration\n integer: attributeTypeInfo.integer,\n stepMode: bufferMapping?.stepMode || shaderDeclaration.stepMode,\n byteOffset: bufferMapping?.byteOffset || 0,\n byteStride: bufferMapping?.byteStride || 0\n };\n}\n\nfunction getAttributeFromShaderLayout(\n shaderLayout: ShaderLayout,\n name: string\n): AttributeDeclaration | null {\n const attribute = shaderLayout.attributes.find(attr => attr.name === name);\n if (!attribute) {\n log.warn(`shader layout attribute \"${name}\" not present in shader`);\n }\n return attribute || null;\n}\n\nfunction getAttributeFromBufferLayout(\n bufferLayouts: BufferLayout[],\n name: string\n): BufferAttributeInfo | null {\n // Check that bufferLayouts are valid (each either has format or attribute)\n checkBufferLayouts(bufferLayouts);\n\n let bufferLayoutInfo = getAttributeFromShortHand(bufferLayouts, name);\n if (bufferLayoutInfo) {\n return bufferLayoutInfo;\n }\n\n bufferLayoutInfo = getAttributeFromAttributesList(bufferLayouts, name);\n if (bufferLayoutInfo) {\n return bufferLayoutInfo;\n }\n\n // Didn't find...\n log.warn(`layout for attribute \"${name}\" not present in buffer layout`);\n return null;\n}\n\n/** Check that bufferLayouts are valid (each either has format or attribute) */\nfunction checkBufferLayouts(bufferLayouts: BufferLayout[]) {\n for (const bufferLayout of bufferLayouts) {\n if (\n (bufferLayout.attributes && bufferLayout.format) ||\n (!bufferLayout.attributes && !bufferLayout.format)\n ) {\n log.warn(`BufferLayout ${name} must have either 'attributes' or 'format' field`);\n }\n }\n}\n\n/** Get attribute from format shorthand if specified */\nfunction getAttributeFromShortHand(\n bufferLayouts: BufferLayout[],\n name: string\n): BufferAttributeInfo | null {\n for (const bufferLayout of bufferLayouts) {\n if (bufferLayout.format && bufferLayout.name === name) {\n return {\n attributeName: bufferLayout.name,\n bufferName: name,\n stepMode: bufferLayout.stepMode,\n vertexFormat: bufferLayout.format,\n // If offset is needed, use `attributes` field.\n byteOffset: 0,\n byteStride: bufferLayout.byteStride || 0\n };\n }\n }\n return null;\n}\n\n/**\n * Search attribute mappings (e.g. interleaved attributes) for buffer mapping.\n * Not the name of the buffer might be the same as one of the interleaved attributes.\n */\nfunction getAttributeFromAttributesList(\n bufferLayouts: BufferLayout[],\n name: string\n): BufferAttributeInfo | null {\n for (const bufferLayout of bufferLayouts) {\n let byteStride: number | undefined = bufferLayout.byteStride;\n\n // Calculate a default byte stride if not provided\n if (typeof bufferLayout.byteStride !== 'number') {\n for (const attributeMapping of bufferLayout.attributes || []) {\n const info = decodeVertexFormat(attributeMapping.format);\n byteStride += info.byteLength;\n }\n }\n\n const attributeMapping = bufferLayout.attributes?.find(mapping => mapping.attribute === name);\n if (attributeMapping) {\n return {\n attributeName: attributeMapping.attribute,\n bufferName: bufferLayout.name,\n stepMode: bufferLayout.stepMode,\n vertexFormat: attributeMapping.format,\n byteOffset: attributeMapping.byteOffset,\n byteStride\n };\n }\n }\n\n return null;\n}\n\n/**\n * Merges an provided shader layout into a base shader layout\n * In WebGL, this allows the auto generated shader layout to be overridden by the application\n * Typically to change the format of the vertex attributes (from float32x4 to uint8x4 etc).\n * @todo Drop this? Aren't all use cases covered by mergeBufferLayout()?\n */\nexport function mergeShaderLayout(\n baseLayout: ShaderLayout,\n overrideLayout: ShaderLayout\n): ShaderLayout {\n // Deep clone the base layout\n const mergedLayout: ShaderLayout = {\n ...baseLayout,\n attributes: baseLayout.attributes.map(attribute => ({...attribute}))\n };\n // Merge the attributes\n for (const attribute of overrideLayout?.attributes || []) {\n const baseAttribute = mergedLayout.attributes.find(attr => attr.name === attribute.name);\n if (!baseAttribute) {\n log.warn(`shader layout attribute ${attribute.name} not present in shader`);\n } else {\n baseAttribute.type = attribute.type || baseAttribute.type;\n baseAttribute.stepMode = attribute.stepMode || baseAttribute.stepMode;\n }\n }\n return mergedLayout;\n}\n"],"mappings":"SAGQA,GAAG;AAAA,SAIHC,yBAAyB;AAAA,SAEzBC,kBAAkB;AAkD1B,OAAO,SAASC,4BAA4BA,CAC1CC,YAA0B,EAC1BC,YAA4B,EACG;EAC/B,MAAMC,cAA6C,GAAG,CAAC,CAAC;EACxD,KAAK,MAAMC,SAAS,IAAIH,YAAY,CAACI,UAAU,EAAE;IAC/CF,cAAc,CAACC,SAAS,CAACE,IAAI,CAAC,GAAGC,2BAA2B,CAC1DN,YAAY,EACZC,YAAY,EACZE,SAAS,CAACE,IACZ,CAAC;EACH;EACA,OAAOH,cAAc;AACvB;AAKA,OAAO,SAASK,2BAA2BA,CACzCP,YAA0B,EAC1BC,YAA4B,EAEX;EAAA,IADjBO,mBAA2B,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,EAAE;EAEhC,MAAMP,cAAc,GAAGH,4BAA4B,CAACC,YAAY,EAAEC,YAAY,CAAC;EAC/E,MAAMW,aAA8B,GAAG,IAAIC,KAAK,CAACL,mBAAmB,CAAC,CAACM,IAAI,CAAC,IAAI,CAAC;EAChF,KAAK,MAAMC,aAAa,IAAIC,MAAM,CAACC,MAAM,CAACf,cAAc,CAAC,EAAE;IACzDU,aAAa,CAACG,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AEtC,YAAY,CAACsC,QAAQ;QAC/Bd,YAAY,EAAE2B,gBAAgB,CAACF,MAAM;QACrCV,UAAU,EAAEY,gBAAgB,CAACZ,UAAU;QACvCC;MACF,CAAC;IACH;EACF;EAEA,OAAO,IAAI;AACb;AAQA,OAAO,SAASe,iBAAiBA,CAC/BC,UAAwB,EACxBC,cAA4B,EACd;EAEd,MAAMC,YAA0B,GAAG;IACjC,GAAGF,UAAU;IACbrD,UAAU,EAAEqD,UAAU,CAACrD,UAAU,CAACwD,GAAG,CAACzD,SAAS,KAAK;MAAC,GAAGA;IAAS,CAAC,CAAC;EACrE,CAAC;EAED,KAAK,MAAMA,SAAS,IAAI,CAAAuD,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAEtD,UAAU,KAAI,EAAE,EAAE;IACxD,MAAMyD,aAAa,GAAGF,YAAY,CAACvD,UAAU,CAACsC,IAAI,CAACC,IAAI,IAAIA,IAAI,CAACtC,IAAI,KAAKF,SAAS,CAACE,IAAI,CAAC;IACxF,IAAI,CAACwD,aAAa,EAAE;MAClBjE,GAAG,CAACgD,IAAI,CAAE,2BAA0BzC,SAAS,CAACE,IAAK,wBAAuB,CAAC;IAC7E,CAAC,MAAM;MACLwD,aAAa,CAACrC,IAAI,GAAGrB,SAAS,CAACqB,IAAI,IAAIqC,aAAa,CAACrC,IAAI;MACzDqC,aAAa,CAACtB,QAAQ,GAAGpC,SAAS,CAACoC,QAAQ,IAAIsB,aAAa,CAACtB,QAAQ;IACvE;EACF;EACA,OAAOoB,YAAY;AACrB"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Device } from './device';
|
|
2
|
+
import type { Framebuffer } from './resources/framebuffer';
|
|
3
|
+
/** Properties for a CanvasContext */
|
|
4
|
+
export type CanvasContextProps = {
|
|
5
|
+
/** If canvas not supplied, will be created and added to the DOM. If string, will be looked up in the DOM */
|
|
6
|
+
canvas?: HTMLCanvasElement | OffscreenCanvas | string | null;
|
|
7
|
+
/** If new canvas is created, it will be created in the specified container, otherwise appended to body */
|
|
8
|
+
container?: HTMLElement | string | null;
|
|
9
|
+
/** Width in pixels of the canvas */
|
|
10
|
+
width?: number;
|
|
11
|
+
/** Height in pixels of the canvas */
|
|
12
|
+
height?: number;
|
|
13
|
+
/** Whether to apply a device pixels scale factor (`true` uses browser DPI) */
|
|
14
|
+
useDevicePixels?: boolean | number;
|
|
15
|
+
/** Whether to track resizes (if not ) */
|
|
16
|
+
autoResize?: boolean;
|
|
17
|
+
/** Visibility (only used if new canvas is created). */
|
|
18
|
+
visible?: boolean;
|
|
19
|
+
/** WebGPU only https://www.w3.org/TR/webgpu/#canvas-configuration */
|
|
20
|
+
colorSpace?: 'srgb';
|
|
21
|
+
/** WebGPU only https://www.w3.org/TR/webgpu/#canvas-configuration */
|
|
22
|
+
alphaMode?: 'opaque' | 'premultiplied';
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Manages a canvas. Supports both HTML or offscreen canvas
|
|
26
|
+
* - Creates a new canvas or looks up a canvas from the DOM
|
|
27
|
+
* - Provides check for DOM loaded
|
|
28
|
+
* @todo commit(): https://github.com/w3ctag/design-reviews/issues/288
|
|
29
|
+
* @todo transferControlToOffscreen: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/transferControlToOffscreen
|
|
30
|
+
*/
|
|
31
|
+
export declare abstract class CanvasContext {
|
|
32
|
+
abstract readonly device: Device;
|
|
33
|
+
readonly id: string;
|
|
34
|
+
readonly props: Required<CanvasContextProps>;
|
|
35
|
+
readonly canvas: HTMLCanvasElement | OffscreenCanvas;
|
|
36
|
+
readonly htmlCanvas?: HTMLCanvasElement;
|
|
37
|
+
readonly offscreenCanvas?: OffscreenCanvas;
|
|
38
|
+
readonly type: 'html-canvas' | 'offscreen-canvas' | 'node';
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
readonly resizeObserver: ResizeObserver | undefined;
|
|
42
|
+
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
43
|
+
readonly _canvasSizeInfo: {
|
|
44
|
+
clientWidth: number;
|
|
45
|
+
clientHeight: number;
|
|
46
|
+
devicePixelRatio: number;
|
|
47
|
+
};
|
|
48
|
+
/** Check if the DOM is loaded */
|
|
49
|
+
static get isPageLoaded(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get a 'lazy' promise that resolves when the DOM is loaded.
|
|
52
|
+
* @note Since there may be limitations on number of `load` event listeners,
|
|
53
|
+
* it is recommended avoid calling this function until actually needed.
|
|
54
|
+
* I.e. don't call it until you know that you will be looking up a string in the DOM.
|
|
55
|
+
*/
|
|
56
|
+
static pageLoaded: Promise<void>;
|
|
57
|
+
constructor(props?: CanvasContextProps);
|
|
58
|
+
/** Returns a framebuffer with properly resized current 'swap chain' textures */
|
|
59
|
+
abstract getCurrentFramebuffer(): Framebuffer;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the current DPR, if props.useDevicePixels is true
|
|
62
|
+
* Device refers to physical
|
|
63
|
+
*/
|
|
64
|
+
getDevicePixelRatio(useDevicePixels?: boolean | number): number;
|
|
65
|
+
/**
|
|
66
|
+
* Returns the size of drawing buffer in device pixels.
|
|
67
|
+
* @note This can be different from the 'CSS' size of a canvas, and also from the
|
|
68
|
+
* canvas' internal drawing buffer size (.width, .height).
|
|
69
|
+
* This is the size required to cover the canvas, adjusted for DPR
|
|
70
|
+
*/
|
|
71
|
+
getPixelSize(): [number, number];
|
|
72
|
+
getAspect(): number;
|
|
73
|
+
/**
|
|
74
|
+
* Returns multiplier need to convert CSS size to Device size
|
|
75
|
+
*/
|
|
76
|
+
cssToDeviceRatio(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Maps CSS pixel position to device pixel position
|
|
79
|
+
*/
|
|
80
|
+
cssToDevicePixels(cssPixel: number[], yInvert?: boolean): {
|
|
81
|
+
x: number;
|
|
82
|
+
y: number;
|
|
83
|
+
width: number;
|
|
84
|
+
height: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Use devicePixelRatio to set canvas width and height
|
|
88
|
+
* @note this is a raw port of luma.gl v8 code. Might be worth a review
|
|
89
|
+
*/
|
|
90
|
+
setDevicePixelRatio(devicePixelRatio: number, options?: {
|
|
91
|
+
width?: number;
|
|
92
|
+
height?: number;
|
|
93
|
+
}): void;
|
|
94
|
+
/** @todo Major hack done to port the CSS methods above, base canvas context should not depend on WebGL */
|
|
95
|
+
getDrawingBufferSize(): [number, number];
|
|
96
|
+
abstract resize(options?: {
|
|
97
|
+
width?: number;
|
|
98
|
+
height?: number;
|
|
99
|
+
useDevicePixels?: boolean | number;
|
|
100
|
+
}): void;
|
|
101
|
+
/** Perform platform specific updates (WebGPU vs WebGL) */
|
|
102
|
+
protected abstract update(): void;
|
|
103
|
+
/**
|
|
104
|
+
* Allows subclass constructor to override the canvas id for auto created canvases.
|
|
105
|
+
* This can really help when debugging DOM in apps that create multiple devices
|
|
106
|
+
*/
|
|
107
|
+
protected _setAutoCreatedCanvasId(id: string): void;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=canvas-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canvas-context.d.ts","sourceRoot":"","sources":["../../src/adapter/canvas-context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AACrC,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,yBAAyB,CAAC;AAMzD,qCAAqC;AACrC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,4GAA4G;IAC5G,MAAM,CAAC,EAAE,iBAAiB,GAAG,eAAe,GAAG,MAAM,GAAG,IAAI,CAAC;IAC7D,0GAA0G;IAC1G,SAAS,CAAC,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAAC;IACxC,oCAAoC;IACpC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qCAAqC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9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AAE,MAAM,CAAC;IAkBhC,SAAS,IAAI,MAAM;IAKnB;;OAEG;IACH,gBAAgB,IAAI,MAAM;IAY1B;;OAEG;IACH,iBAAiB,CACf,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,GAAE,OAAc,GACtB;QACD,CAAC,EAAE,MAAM,CAAC;QACV,CAAC,EAAE,MAAM,CAAC;QACV,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;KAChB;IAMD;;;OAGG;IACH,mBAAmB,CACjB,gBAAgB,EAAE,MAAM,EACxB,OAAO,GAAE;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAM,GAC9C,IAAI;IAwDP,0GAA0G;IAC1G,oBAAoB,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IAUxC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;QACxB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,eAAe,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;KACpC,GAAG,IAAI;IAER,0DAA0D;IAC1D,SAAS,CAAC,QAAQ,CAAC,MAAM,IAAI,IAAI;IAEjC;;;OAGG;IACH,SAAS,CAAC,uBAAuB,CAAC,EAAE,EAAE,MAAM;CAK7C"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { isBrowser } from '@probe.gl/env';
|
|
2
|
+
import { log } from "../utils/log.js";
|
|
3
|
+
const isPage = isBrowser() && typeof document !== 'undefined';
|
|
4
|
+
const isPageLoaded = () => isPage && document.readyState === 'complete';
|
|
5
|
+
const DEFAULT_CANVAS_CONTEXT_PROPS = {
|
|
6
|
+
canvas: null,
|
|
7
|
+
width: 800,
|
|
8
|
+
height: 600,
|
|
9
|
+
useDevicePixels: true,
|
|
10
|
+
autoResize: true,
|
|
11
|
+
container: null,
|
|
12
|
+
visible: true,
|
|
13
|
+
colorSpace: 'srgb',
|
|
14
|
+
alphaMode: 'opaque'
|
|
15
|
+
};
|
|
16
|
+
export class CanvasContext {
|
|
17
|
+
static get isPageLoaded() {
|
|
18
|
+
return isPageLoaded();
|
|
19
|
+
}
|
|
20
|
+
constructor(props) {
|
|
21
|
+
this.device = void 0;
|
|
22
|
+
this.id = void 0;
|
|
23
|
+
this.props = void 0;
|
|
24
|
+
this.canvas = void 0;
|
|
25
|
+
this.htmlCanvas = void 0;
|
|
26
|
+
this.offscreenCanvas = void 0;
|
|
27
|
+
this.type = void 0;
|
|
28
|
+
this.width = 1;
|
|
29
|
+
this.height = 1;
|
|
30
|
+
this.resizeObserver = void 0;
|
|
31
|
+
this._canvasSizeInfo = {
|
|
32
|
+
clientWidth: 0,
|
|
33
|
+
clientHeight: 0,
|
|
34
|
+
devicePixelRatio: 1
|
|
35
|
+
};
|
|
36
|
+
this.props = {
|
|
37
|
+
...DEFAULT_CANVAS_CONTEXT_PROPS,
|
|
38
|
+
...props
|
|
39
|
+
};
|
|
40
|
+
props = this.props;
|
|
41
|
+
if (!isBrowser()) {
|
|
42
|
+
this.id = 'node-canvas-context';
|
|
43
|
+
this.type = 'node';
|
|
44
|
+
this.width = this.props.width;
|
|
45
|
+
this.height = this.props.height;
|
|
46
|
+
this.canvas = null;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!props.canvas) {
|
|
50
|
+
var _props, _props2;
|
|
51
|
+
const canvas = createCanvas(props);
|
|
52
|
+
const container = getContainer(((_props = props) === null || _props === void 0 ? void 0 : _props.container) || null);
|
|
53
|
+
container.insertBefore(canvas, container.firstChild);
|
|
54
|
+
this.canvas = canvas;
|
|
55
|
+
if (!((_props2 = props) !== null && _props2 !== void 0 && _props2.visible)) {
|
|
56
|
+
this.canvas.style.visibility = 'hidden';
|
|
57
|
+
}
|
|
58
|
+
} else if (typeof props.canvas === 'string') {
|
|
59
|
+
this.canvas = getCanvasFromDOM(props.canvas);
|
|
60
|
+
} else {
|
|
61
|
+
this.canvas = props.canvas;
|
|
62
|
+
}
|
|
63
|
+
if (this.canvas instanceof HTMLCanvasElement) {
|
|
64
|
+
this.id = this.canvas.id;
|
|
65
|
+
this.type = 'html-canvas';
|
|
66
|
+
this.htmlCanvas = this.canvas;
|
|
67
|
+
} else {
|
|
68
|
+
this.id = 'offscreen-canvas';
|
|
69
|
+
this.type = 'offscreen-canvas';
|
|
70
|
+
this.offscreenCanvas = this.canvas;
|
|
71
|
+
}
|
|
72
|
+
if (this.canvas instanceof HTMLCanvasElement && props.autoResize) {
|
|
73
|
+
this.resizeObserver = new ResizeObserver(entries => {
|
|
74
|
+
for (const entry of entries) {
|
|
75
|
+
if (entry.target === this.canvas) {
|
|
76
|
+
this.update();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
this.resizeObserver.observe(this.canvas);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
getDevicePixelRatio(useDevicePixels) {
|
|
84
|
+
if (typeof OffscreenCanvas !== 'undefined' && this.canvas instanceof OffscreenCanvas) {
|
|
85
|
+
return 1;
|
|
86
|
+
}
|
|
87
|
+
useDevicePixels = useDevicePixels === undefined ? this.props.useDevicePixels : useDevicePixels;
|
|
88
|
+
if (!useDevicePixels || useDevicePixels <= 0) {
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
if (useDevicePixels === true) {
|
|
92
|
+
const dpr = typeof window !== 'undefined' && window.devicePixelRatio;
|
|
93
|
+
return dpr || 1;
|
|
94
|
+
}
|
|
95
|
+
return useDevicePixels;
|
|
96
|
+
}
|
|
97
|
+
getPixelSize() {
|
|
98
|
+
switch (this.type) {
|
|
99
|
+
case 'node':
|
|
100
|
+
return [this.width, this.height];
|
|
101
|
+
case 'offscreen-canvas':
|
|
102
|
+
return [this.canvas.width, this.canvas.height];
|
|
103
|
+
case 'html-canvas':
|
|
104
|
+
const dpr = this.getDevicePixelRatio();
|
|
105
|
+
const canvas = this.canvas;
|
|
106
|
+
return canvas.parentElement ? [canvas.clientWidth * dpr, canvas.clientHeight * dpr] : [this.canvas.width, this.canvas.height];
|
|
107
|
+
default:
|
|
108
|
+
throw new Error(this.type);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
getAspect() {
|
|
112
|
+
const [width, height] = this.getPixelSize();
|
|
113
|
+
return width / height;
|
|
114
|
+
}
|
|
115
|
+
cssToDeviceRatio() {
|
|
116
|
+
try {
|
|
117
|
+
const [drawingBufferWidth] = this.getDrawingBufferSize();
|
|
118
|
+
const {
|
|
119
|
+
clientWidth
|
|
120
|
+
} = this._canvasSizeInfo;
|
|
121
|
+
return clientWidth ? drawingBufferWidth / clientWidth : 1;
|
|
122
|
+
} catch {
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
cssToDevicePixels(cssPixel) {
|
|
127
|
+
let yInvert = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
128
|
+
const ratio = this.cssToDeviceRatio();
|
|
129
|
+
const [width, height] = this.getDrawingBufferSize();
|
|
130
|
+
return scalePixels(cssPixel, ratio, width, height, yInvert);
|
|
131
|
+
}
|
|
132
|
+
setDevicePixelRatio(devicePixelRatio) {
|
|
133
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
134
|
+
if (!this.htmlCanvas) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
let clientWidth = 'width' in options ? options.width : this.htmlCanvas.clientWidth;
|
|
138
|
+
let clientHeight = 'height' in options ? options.height : this.htmlCanvas.clientHeight;
|
|
139
|
+
if (!clientWidth || !clientHeight) {
|
|
140
|
+
log.log(1, 'Canvas clientWidth/clientHeight is 0')();
|
|
141
|
+
devicePixelRatio = 1;
|
|
142
|
+
clientWidth = this.htmlCanvas.width || 1;
|
|
143
|
+
clientHeight = this.htmlCanvas.height || 1;
|
|
144
|
+
}
|
|
145
|
+
const cachedSize = this._canvasSizeInfo;
|
|
146
|
+
if (cachedSize.clientWidth !== clientWidth || cachedSize.clientHeight !== clientHeight || cachedSize.devicePixelRatio !== devicePixelRatio) {
|
|
147
|
+
let clampedPixelRatio = devicePixelRatio;
|
|
148
|
+
const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);
|
|
149
|
+
const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);
|
|
150
|
+
this.htmlCanvas.width = canvasWidth;
|
|
151
|
+
this.htmlCanvas.height = canvasHeight;
|
|
152
|
+
const [drawingBufferWidth, drawingBufferHeight] = this.getDrawingBufferSize();
|
|
153
|
+
if (drawingBufferWidth !== canvasWidth || drawingBufferHeight !== canvasHeight) {
|
|
154
|
+
clampedPixelRatio = Math.min(drawingBufferWidth / clientWidth, drawingBufferHeight / clientHeight);
|
|
155
|
+
this.htmlCanvas.width = Math.floor(clientWidth * clampedPixelRatio);
|
|
156
|
+
this.htmlCanvas.height = Math.floor(clientHeight * clampedPixelRatio);
|
|
157
|
+
log.warn('Device pixel ratio clamped')();
|
|
158
|
+
}
|
|
159
|
+
this._canvasSizeInfo.clientWidth = clientWidth;
|
|
160
|
+
this._canvasSizeInfo.clientHeight = clientHeight;
|
|
161
|
+
this._canvasSizeInfo.devicePixelRatio = devicePixelRatio;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
getDrawingBufferSize() {
|
|
165
|
+
const gl = this.device.gl;
|
|
166
|
+
if (!gl) {
|
|
167
|
+
throw new Error('canvas size');
|
|
168
|
+
}
|
|
169
|
+
return [gl.drawingBufferWidth, gl.drawingBufferHeight];
|
|
170
|
+
}
|
|
171
|
+
_setAutoCreatedCanvasId(id) {
|
|
172
|
+
var _this$htmlCanvas;
|
|
173
|
+
if (((_this$htmlCanvas = this.htmlCanvas) === null || _this$htmlCanvas === void 0 ? void 0 : _this$htmlCanvas.id) === 'lumagl-auto-created-canvas') {
|
|
174
|
+
this.htmlCanvas.id = id;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
CanvasContext.pageLoaded = getPageLoadPromise();
|
|
179
|
+
function getPageLoadPromise() {
|
|
180
|
+
if (isPageLoaded() || typeof window === 'undefined') {
|
|
181
|
+
return Promise.resolve();
|
|
182
|
+
}
|
|
183
|
+
return new Promise(resolve => {
|
|
184
|
+
window.addEventListener('load', () => resolve());
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function getContainer(container) {
|
|
188
|
+
if (typeof container === 'string') {
|
|
189
|
+
const element = document.getElementById(container);
|
|
190
|
+
if (!element && !isPageLoaded()) {
|
|
191
|
+
throw new Error(`Accessing '${container}' before page was loaded`);
|
|
192
|
+
}
|
|
193
|
+
if (!element) {
|
|
194
|
+
throw new Error(`${container} is not an HTML element`);
|
|
195
|
+
}
|
|
196
|
+
return element;
|
|
197
|
+
} else if (container) {
|
|
198
|
+
return container;
|
|
199
|
+
}
|
|
200
|
+
return document.body;
|
|
201
|
+
}
|
|
202
|
+
function getCanvasFromDOM(canvasId) {
|
|
203
|
+
const canvas = document.getElementById(canvasId);
|
|
204
|
+
if (!canvas && !isPageLoaded()) {
|
|
205
|
+
throw new Error(`Accessing '${canvasId}' before page was loaded`);
|
|
206
|
+
}
|
|
207
|
+
if (!(canvas instanceof HTMLCanvasElement)) {
|
|
208
|
+
throw new Error('Object is not a canvas element');
|
|
209
|
+
}
|
|
210
|
+
return canvas;
|
|
211
|
+
}
|
|
212
|
+
function createCanvas(props) {
|
|
213
|
+
const {
|
|
214
|
+
width,
|
|
215
|
+
height
|
|
216
|
+
} = props;
|
|
217
|
+
const targetCanvas = document.createElement('canvas');
|
|
218
|
+
targetCanvas.id = 'lumagl-auto-created-canvas';
|
|
219
|
+
targetCanvas.width = width || 1;
|
|
220
|
+
targetCanvas.height = height || 1;
|
|
221
|
+
targetCanvas.style.width = Number.isFinite(width) ? `${width}px` : '100%';
|
|
222
|
+
targetCanvas.style.height = Number.isFinite(height) ? `${height}px` : '100%';
|
|
223
|
+
return targetCanvas;
|
|
224
|
+
}
|
|
225
|
+
function scalePixels(pixel, ratio, width, height, yInvert) {
|
|
226
|
+
const point = pixel;
|
|
227
|
+
const x = scaleX(point[0], ratio, width);
|
|
228
|
+
let y = scaleY(point[1], ratio, height, yInvert);
|
|
229
|
+
let t = scaleX(point[0] + 1, ratio, width);
|
|
230
|
+
const xHigh = t === width - 1 ? t : t - 1;
|
|
231
|
+
t = scaleY(point[1] + 1, ratio, height, yInvert);
|
|
232
|
+
let yHigh;
|
|
233
|
+
if (yInvert) {
|
|
234
|
+
t = t === 0 ? t : t + 1;
|
|
235
|
+
yHigh = y;
|
|
236
|
+
y = t;
|
|
237
|
+
} else {
|
|
238
|
+
yHigh = t === height - 1 ? t : t - 1;
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
x,
|
|
242
|
+
y,
|
|
243
|
+
width: Math.max(xHigh - x + 1, 1),
|
|
244
|
+
height: Math.max(yHigh - y + 1, 1)
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function scaleX(x, ratio, width) {
|
|
248
|
+
const r = Math.min(Math.round(x * ratio), width - 1);
|
|
249
|
+
return r;
|
|
250
|
+
}
|
|
251
|
+
function scaleY(y, ratio, height, yInvert) {
|
|
252
|
+
return yInvert ? Math.max(0, height - 1 - Math.round(y * ratio)) : Math.min(Math.round(y * ratio), height - 1);
|
|
253
|
+
}
|
|
254
|
+
//# sourceMappingURL=canvas-context.js.map
|