@flighthq/render-wgpu 0.3.0-next.906.07cea63 → 0.3.1-next.1041.35b950c

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.
Files changed (95) hide show
  1. package/dist/contract.d.ts +7 -1
  2. package/dist/contract.d.ts.map +1 -1
  3. package/dist/contract.js +7 -1
  4. package/dist/contract.js.map +1 -1
  5. package/dist/enableWgpuTextureResolverGuards.d.ts +4 -0
  6. package/dist/enableWgpuTextureResolverGuards.d.ts.map +1 -0
  7. package/dist/enableWgpuTextureResolverGuards.js +8 -0
  8. package/dist/enableWgpuTextureResolverGuards.js.map +1 -0
  9. package/dist/explainWgpuTextureResolution.d.ts +3 -0
  10. package/dist/explainWgpuTextureResolution.d.ts.map +1 -0
  11. package/dist/explainWgpuTextureResolution.js +15 -0
  12. package/dist/explainWgpuTextureResolution.js.map +1 -0
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/wgpuBackground.d.ts +2 -0
  18. package/dist/wgpuBackground.d.ts.map +1 -1
  19. package/dist/wgpuBackground.js +30 -2
  20. package/dist/wgpuBackground.js.map +1 -1
  21. package/dist/wgpuCompressedTexture.d.ts +2 -2
  22. package/dist/wgpuCompressedTexture.d.ts.map +1 -1
  23. package/dist/wgpuCompressedTexture.js +52 -13
  24. package/dist/wgpuCompressedTexture.js.map +1 -1
  25. package/dist/wgpuDraw.d.ts +6 -4
  26. package/dist/wgpuDraw.d.ts.map +1 -1
  27. package/dist/wgpuDraw.js +150 -69
  28. package/dist/wgpuDraw.js.map +1 -1
  29. package/dist/wgpuExternalImageSource.d.ts +3 -0
  30. package/dist/wgpuExternalImageSource.d.ts.map +1 -0
  31. package/dist/wgpuExternalImageSource.js +51 -0
  32. package/dist/wgpuExternalImageSource.js.map +1 -0
  33. package/dist/wgpuExternalTexture.d.ts +4 -0
  34. package/dist/wgpuExternalTexture.d.ts.map +1 -0
  35. package/dist/wgpuExternalTexture.js +62 -0
  36. package/dist/wgpuExternalTexture.js.map +1 -0
  37. package/dist/wgpuMaterialRegistry.d.ts.map +1 -1
  38. package/dist/wgpuMaterialRegistry.js +17 -12
  39. package/dist/wgpuMaterialRegistry.js.map +1 -1
  40. package/dist/wgpuMipmap.d.ts.map +1 -1
  41. package/dist/wgpuMipmap.js +11 -9
  42. package/dist/wgpuMipmap.js.map +1 -1
  43. package/dist/wgpuRenderState.d.ts +15 -3
  44. package/dist/wgpuRenderState.d.ts.map +1 -1
  45. package/dist/wgpuRenderState.js +253 -33
  46. package/dist/wgpuRenderState.js.map +1 -1
  47. package/dist/wgpuRenderTarget.d.ts.map +1 -1
  48. package/dist/wgpuRenderTarget.js +2 -0
  49. package/dist/wgpuRenderTarget.js.map +1 -1
  50. package/dist/wgpuRenderTexture.d.ts +15 -0
  51. package/dist/wgpuRenderTexture.d.ts.map +1 -0
  52. package/dist/wgpuRenderTexture.js +130 -0
  53. package/dist/wgpuRenderTexture.js.map +1 -0
  54. package/dist/wgpuRenderTexturePool.d.ts +7 -0
  55. package/dist/wgpuRenderTexturePool.d.ts.map +1 -0
  56. package/dist/wgpuRenderTexturePool.js +86 -0
  57. package/dist/wgpuRenderTexturePool.js.map +1 -0
  58. package/dist/wgpuShader.d.ts.map +1 -1
  59. package/dist/wgpuShader.js +5 -1
  60. package/dist/wgpuShader.js.map +1 -1
  61. package/dist/wgpuSurface.d.ts +2 -2
  62. package/dist/wgpuSurface.d.ts.map +1 -1
  63. package/dist/wgpuSurface.js +61 -13
  64. package/dist/wgpuSurface.js.map +1 -1
  65. package/dist/wgpuTestHelper.d.ts +1 -0
  66. package/dist/wgpuTestHelper.d.ts.map +1 -1
  67. package/dist/wgpuTestHelper.js +125 -8
  68. package/dist/wgpuTestHelper.js.map +1 -1
  69. package/dist/wgpuTextureResolver.d.ts +9 -0
  70. package/dist/wgpuTextureResolver.d.ts.map +1 -0
  71. package/dist/wgpuTextureResolver.js +68 -0
  72. package/dist/wgpuTextureResolver.js.map +1 -0
  73. package/dist/wgpuTextureUpload.d.ts +2 -2
  74. package/dist/wgpuTextureUpload.d.ts.map +1 -1
  75. package/dist/wgpuTextureUpload.js +5 -10
  76. package/dist/wgpuTextureUpload.js.map +1 -1
  77. package/package.json +17 -7
  78. package/src/enableWgpuTextureResolverGuards.test.ts +65 -0
  79. package/src/explainWgpuTextureResolution.test.ts +36 -0
  80. package/src/wgpuBackground.test.ts +34 -1
  81. package/src/wgpuCompressedTexture.test.ts +35 -40
  82. package/src/wgpuDraw.test.ts +180 -62
  83. package/src/wgpuExternalImageSource.test.ts +89 -0
  84. package/src/wgpuExternalTexture.test.ts +35 -0
  85. package/src/wgpuMaterialRegistry.test.ts +44 -3
  86. package/src/wgpuMipmap.test.ts +10 -2
  87. package/src/wgpuRenderState.test.ts +462 -12
  88. package/src/wgpuRenderTarget.test.ts +14 -0
  89. package/src/wgpuRenderTexture.test.ts +165 -0
  90. package/src/wgpuRenderTexturePool.test.ts +146 -0
  91. package/src/wgpuShaderBinding.test.ts +6 -6
  92. package/src/wgpuSurface.test.ts +95 -3
  93. package/src/wgpuTestHelper.test.ts +64 -0
  94. package/src/wgpuTextureResolver.test.ts +212 -0
  95. package/src/wgpuTextureUpload.test.ts +28 -13
@@ -0,0 +1,212 @@
1
+ import { createEntity } from '@flighthq/entity/contract';
2
+ import { getRegistryTableKeys } from '@flighthq/registry/contract';
3
+ import type { Image, RenderTexture, Texture, TextureSource } from '@flighthq/types/contract';
4
+ import {
5
+ BitmapTextureSourceKind,
6
+ CompressedImageTextureSourceKind,
7
+ ImageTextureSourceKind,
8
+ RenderTargetTextureSourceKind,
9
+ } from '@flighthq/types/contract';
10
+
11
+ import { renderWgpuBackground, submitWgpuRenderPass } from './wgpuBackground';
12
+ import { getWgpuRenderStateRuntime } from './wgpuRenderState';
13
+ import { renderIntoWgpuRenderTexture } from './wgpuRenderTexture';
14
+ import { createWgpuRenderStateForTest, installWgpuMock } from './wgpuTestHelper';
15
+ import {
16
+ registerWgpuImageTextureResolver,
17
+ registerWgpuBitmapTextureResolver,
18
+ registerWgpuCompressedImageTextureResolver,
19
+ registerWgpuRenderTextureResolver,
20
+ registerStandardWgpuTextureResolvers,
21
+ registerWgpuTextureResolver,
22
+ resolveWgpuTexture,
23
+ } from './wgpuTextureResolver';
24
+
25
+ beforeAll(() => {
26
+ installWgpuMock();
27
+ });
28
+
29
+ function imageResource(source: CanvasImageSource = document.createElement('canvas')): Image {
30
+ return {
31
+ height: 4,
32
+ alphaType: 'straight',
33
+ gamut: 'srgb',
34
+ kind: ImageTextureSourceKind,
35
+ source,
36
+ version: 0,
37
+ width: 4,
38
+ } as unknown as Image;
39
+ }
40
+
41
+ function textureSource(kind: string): TextureSource {
42
+ return {
43
+ height: 4,
44
+ kind,
45
+ version: 0,
46
+ width: 4,
47
+ } as unknown as TextureSource;
48
+ }
49
+
50
+ function textureWithImage(image: TextureSource | null): Texture {
51
+ return {
52
+ colorSpace: 'srgb',
53
+ flipX: false,
54
+ flipY: false,
55
+ sampler: {
56
+ anisotropy: 1,
57
+ magFilter: 'linear',
58
+ minFilter: 'linear',
59
+ mipmaps: false,
60
+ wrapU: 'clamp-to-edge',
61
+ wrapV: 'clamp-to-edge',
62
+ },
63
+ dimension: '2d',
64
+ source: image,
65
+ uvOffset: { x: 0, y: 0 },
66
+ uvRotation: 0,
67
+ uvScale: { x: 1, y: 1 },
68
+ version: 0,
69
+ } as unknown as Texture;
70
+ }
71
+
72
+ function renderTexture(): RenderTexture {
73
+ const texture = textureWithImage(null);
74
+ texture.colorSpace = 'linear';
75
+ if (texture.dimension !== '2d') throw new Error('test texture must be 2d');
76
+ texture.source = createEntity({
77
+ alphaType: 'straight' as const,
78
+ gamut: 'srgb' as const,
79
+ height: 8,
80
+ kind: RenderTargetTextureSourceKind,
81
+ version: 0,
82
+ width: 8,
83
+ });
84
+ return texture as RenderTexture;
85
+ }
86
+
87
+ function registeredTextureSourceKinds(state: Parameters<typeof getWgpuRenderStateRuntime>[0]): string[] {
88
+ const kinds: string[] = [];
89
+ getRegistryTableKeys(kinds, getWgpuRenderStateRuntime(state).registries.textureResolvers);
90
+ return kinds;
91
+ }
92
+
93
+ describe('registerStandardWgpuTextureResolvers', () => {
94
+ it('registers bitmap, image, and render texture sources without compressed images', async () => {
95
+ const state = await createWgpuRenderStateForTest();
96
+ registerStandardWgpuTextureResolvers(state);
97
+ expect(registeredTextureSourceKinds(state)).toEqual([
98
+ BitmapTextureSourceKind,
99
+ ImageTextureSourceKind,
100
+ RenderTargetTextureSourceKind,
101
+ ]);
102
+ });
103
+ });
104
+
105
+ describe('registerWgpuBitmapTextureResolver', () => {
106
+ it('registers only the Bitmap source key', async () => {
107
+ const state = await createWgpuRenderStateForTest();
108
+ registerWgpuBitmapTextureResolver(state);
109
+ expect(registeredTextureSourceKinds(state)).toEqual([BitmapTextureSourceKind]);
110
+ });
111
+ });
112
+
113
+ describe('registerWgpuCompressedImageTextureResolver', () => {
114
+ it('registers only the CompressedImage source key', async () => {
115
+ const state = await createWgpuRenderStateForTest();
116
+ registerWgpuCompressedImageTextureResolver(state);
117
+ expect(registeredTextureSourceKinds(state)).toEqual([CompressedImageTextureSourceKind]);
118
+ });
119
+ });
120
+
121
+ describe('registerWgpuImageTextureResolver', () => {
122
+ it('resolves and caches a declared still-image source', async () => {
123
+ const state = await createWgpuRenderStateForTest();
124
+ const texture = textureWithImage(imageResource());
125
+ registerWgpuImageTextureResolver(state);
126
+
127
+ const first = resolveWgpuTexture(state, texture);
128
+ expect(first).not.toBeNull();
129
+ expect(resolveWgpuTexture(state, texture)).toBe(first);
130
+ });
131
+
132
+ // The sample format comes from the PAIR (what the content is, what the destination composites in), not
133
+ // from an override. One sRGB texture therefore realizes two ways: decoded for a linear-working path
134
+ // (3D), byte-through for an encoded-working one (2D), cached apart so both can be live at once.
135
+ it('derives the sample format from the texture and working color spaces', async () => {
136
+ const state = await createWgpuRenderStateForTest();
137
+ const createTexture = vi.spyOn(state.device, 'createTexture');
138
+ const texture = textureWithImage(imageResource());
139
+ registerWgpuImageTextureResolver(state);
140
+
141
+ const decoded = resolveWgpuTexture(state, texture, false, 'linear');
142
+ expect(createTexture).toHaveBeenLastCalledWith(expect.objectContaining({ format: 'rgba8unorm-srgb' }));
143
+
144
+ const byteThrough = resolveWgpuTexture(state, texture, false, 'srgb');
145
+ expect(byteThrough).not.toBe(decoded);
146
+ expect(createTexture).toHaveBeenLastCalledWith(expect.objectContaining({ format: 'rgba8unorm' }));
147
+
148
+ // Linear content never decodes, whichever space the destination works in.
149
+ const linearTexture = textureWithImage(imageResource());
150
+ (linearTexture as { colorSpace: string }).colorSpace = 'linear';
151
+ resolveWgpuTexture(state, linearTexture, false, 'linear');
152
+ expect(createTexture).toHaveBeenLastCalledWith(expect.objectContaining({ format: 'rgba8unorm' }));
153
+
154
+ expect(resolveWgpuTexture(state, texture, false, 'linear')).toBe(decoded);
155
+ });
156
+
157
+ it('resolves a host video through the image source kind', async () => {
158
+ const state = await createWgpuRenderStateForTest();
159
+ const source = document.createElement('video');
160
+ Object.defineProperties(source, {
161
+ readyState: { value: 4 },
162
+ videoHeight: { value: 8 },
163
+ videoWidth: { value: 8 },
164
+ });
165
+ const image = imageResource(source);
166
+ const texture = textureWithImage(image);
167
+ registerWgpuImageTextureResolver(state);
168
+ expect(resolveWgpuTexture(state, texture)).not.toBeNull();
169
+ });
170
+ });
171
+
172
+ describe('registerWgpuRenderTextureResolver', () => {
173
+ it('returns a render target only after it has been rendered', async () => {
174
+ const state = await createWgpuRenderStateForTest();
175
+ const texture = renderTexture();
176
+ registerWgpuRenderTextureResolver(state);
177
+ expect(resolveWgpuTexture(state, texture)).toBeNull();
178
+
179
+ renderWgpuBackground(state);
180
+ renderIntoWgpuRenderTexture(state, texture, () => {});
181
+ expect(resolveWgpuTexture(state, texture)).not.toBeNull();
182
+ submitWgpuRenderPass(state);
183
+ });
184
+ });
185
+
186
+ describe('registerWgpuTextureResolver', () => {
187
+ it('is state-scoped, last-write-wins by string kind, and removable', async () => {
188
+ const a = await createWgpuRenderStateForTest();
189
+ const b = await createWgpuRenderStateForTest();
190
+ const image = textureSource('acme.generated');
191
+ const texture = textureWithImage(image);
192
+ const first = vi.fn(() => ({ first: true }) as never);
193
+ const second = vi.fn(() => ({ second: true }) as never);
194
+
195
+ registerWgpuTextureResolver(a, image.kind, first);
196
+ expect(resolveWgpuTexture(a, texture)).toEqual({ first: true });
197
+ expect(resolveWgpuTexture(b, texture)).toBeNull();
198
+
199
+ registerWgpuTextureResolver(a, image.kind, second);
200
+ expect(resolveWgpuTexture(a, texture)).toEqual({ second: true });
201
+
202
+ registerWgpuTextureResolver(a, image.kind, null);
203
+ expect(resolveWgpuTexture(a, texture)).toBeNull();
204
+ });
205
+ });
206
+
207
+ describe('resolveWgpuTexture', () => {
208
+ it('returns null without an exact registered source kind', async () => {
209
+ const state = await createWgpuRenderStateForTest();
210
+ expect(resolveWgpuTexture(state, textureWithImage(null))).toBeNull();
211
+ });
212
+ });
@@ -1,4 +1,4 @@
1
- import type { ImageResource } from '@flighthq/types/contract';
1
+ import type { Image } from '@flighthq/types/contract';
2
2
 
3
3
  import { uploadWgpuTextureData, uploadWgpuTextureElement, uploadWgpuTextureImageResource } from './wgpuTextureUpload';
4
4
 
@@ -28,7 +28,9 @@ describe('uploadWgpuTextureElement', () => {
28
28
  it('drives copyExternalImageToTexture with the external source', () => {
29
29
  const device = makeDevice();
30
30
  const texture = {} as GPUTexture;
31
- const source = {} as GPUCopyExternalImageSource;
31
+ const source = document.createElement('canvas');
32
+ source.width = 4;
33
+ source.height = 4;
32
34
  uploadWgpuTextureElement(device, texture, [0, 0, 0], 4, 4, source);
33
35
  expect(device.queue.copyExternalImageToTexture).toHaveBeenCalledWith(
34
36
  { source },
@@ -36,25 +38,38 @@ describe('uploadWgpuTextureElement', () => {
36
38
  [4, 4, 1],
37
39
  );
38
40
  });
41
+
42
+ it('skips a copy with a zero-sized extent', () => {
43
+ const device = makeDevice();
44
+ const source = document.createElement('canvas');
45
+ source.width = 4;
46
+ source.height = 4;
47
+ uploadWgpuTextureElement(device, {} as GPUTexture, [0, 0, 0], 0, 4, source);
48
+ expect(device.queue.copyExternalImageToTexture).not.toHaveBeenCalled();
49
+ });
50
+
51
+ it('skips a copy when the browser cannot snapshot the source', () => {
52
+ const device = makeDevice();
53
+ const canvas = document.createElement('canvas');
54
+ canvas.width = 4;
55
+ canvas.height = 4;
56
+ vi.mocked(device.queue.copyExternalImageToTexture).mockImplementation(() => {
57
+ throw new TypeError('Failed to copy content from external image.');
58
+ });
59
+ expect(() => uploadWgpuTextureElement(device, {} as GPUTexture, [0, 0, 0], 4, 4, canvas)).not.toThrow();
60
+ });
39
61
  });
40
62
 
41
63
  describe('uploadWgpuTextureImageResource', () => {
42
64
  it('takes the element path when the resource carries a source', () => {
43
65
  const device = makeDevice();
44
66
  const texture = {} as GPUTexture;
45
- const image = { source: {} as CanvasImageSource, data: null, width: 4, height: 4 } as ImageResource;
67
+ const source = document.createElement('canvas');
68
+ source.width = 4;
69
+ source.height = 4;
70
+ const image = { source, width: 4, height: 4 } as unknown as Image;
46
71
  uploadWgpuTextureImageResource(device, texture, [0, 0, 0], image);
47
72
  expect(device.queue.copyExternalImageToTexture).toHaveBeenCalledTimes(1);
48
73
  expect(device.queue.writeTexture).not.toHaveBeenCalled();
49
74
  });
50
-
51
- it('takes the raw-pixel path when the resource is data-only', () => {
52
- const device = makeDevice();
53
- const texture = {} as GPUTexture;
54
- const data = new Uint8ClampedArray(4 * 4 * 4);
55
- const image = { source: null, data, width: 4, height: 4 } as ImageResource;
56
- uploadWgpuTextureImageResource(device, texture, [0, 0, 0], image);
57
- expect(device.queue.writeTexture).toHaveBeenCalledTimes(1);
58
- expect(device.queue.copyExternalImageToTexture).not.toHaveBeenCalled();
59
- });
60
75
  });