glre 0.22.0 → 0.24.0

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 (50) hide show
  1. package/README.md +19 -28
  2. package/dist/index.d.ts +49 -222
  3. package/dist/index.js +43 -37
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +43 -37
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/native.d.ts +49 -5
  8. package/dist/native.js +43 -37
  9. package/dist/native.js.map +1 -1
  10. package/dist/native.mjs +43 -37
  11. package/dist/native.mjs.map +1 -1
  12. package/dist/react.d.ts +1 -2
  13. package/dist/react.js +43 -37
  14. package/dist/react.js.map +1 -1
  15. package/dist/react.mjs +43 -37
  16. package/dist/react.mjs.map +1 -1
  17. package/dist/solid.d.ts +1 -2
  18. package/dist/solid.js +43 -37
  19. package/dist/solid.js.map +1 -1
  20. package/dist/solid.mjs +43 -37
  21. package/dist/solid.mjs.map +1 -1
  22. package/package.json +5 -1
  23. package/src/code/glsl.ts +7 -45
  24. package/src/code/wgsl.ts +10 -46
  25. package/src/index.ts +34 -35
  26. package/src/native.ts +9 -4
  27. package/src/node/cache.ts +3 -10
  28. package/src/node/const.ts +2 -19
  29. package/src/node/conv.ts +6 -17
  30. package/src/node/index.ts +5 -8
  31. package/src/node/node.ts +8 -22
  32. package/src/node/uniform.ts +6 -13
  33. package/src/react.ts +0 -1
  34. package/src/solid.ts +0 -1
  35. package/src/types.ts +21 -21
  36. package/src/{utils.ts → utils/helpers.ts} +0 -9
  37. package/src/utils/pipeline.ts +124 -0
  38. package/src/utils/program.ts +94 -0
  39. package/src/webgl.ts +77 -0
  40. package/src/webgpu.ts +102 -0
  41. package/src/webgl/buffer.ts +0 -78
  42. package/src/webgl/index.ts +0 -79
  43. package/src/webgl/program.ts +0 -61
  44. package/src/webgl/shader.ts +0 -60
  45. package/src/webgl/texture.ts +0 -93
  46. package/src/webgpu/buffer.ts +0 -96
  47. package/src/webgpu/device.ts +0 -91
  48. package/src/webgpu/index.ts +0 -40
  49. package/src/webgpu/pipeline.ts +0 -94
  50. package/src/webgpu/texture.ts +0 -139
@@ -1,93 +0,0 @@
1
- // WebGLテクスチャ作成と管理
2
- export const createTexture = (
3
- c: WebGLRenderingContext,
4
- img: HTMLImageElement
5
- ) => {
6
- const texture = c.createTexture()
7
- if (!texture) throw new Error('Failed to create texture')
8
- c.bindTexture(c.TEXTURE_2D, texture)
9
- c.texImage2D(c.TEXTURE_2D, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, img)
10
- c.generateMipmap(c.TEXTURE_2D)
11
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MIN_FILTER, c.LINEAR)
12
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MAG_FILTER, c.LINEAR)
13
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_S, c.CLAMP_TO_EDGE)
14
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_T, c.CLAMP_TO_EDGE)
15
- c.bindTexture(c.TEXTURE_2D, null)
16
- return texture
17
- }
18
-
19
- // テクスチャユニットをアクティブ化
20
- export const activeTexture = (
21
- c: WebGLRenderingContext,
22
- location: WebGLUniformLocation | null,
23
- unit: number,
24
- texture: WebGLTexture
25
- ) => {
26
- if (!location) return
27
- c.uniform1i(location, unit)
28
- c.activeTexture(c.TEXTURE0 + unit)
29
- c.bindTexture(c.TEXTURE_2D, texture)
30
- }
31
-
32
- // テクスチャの削除
33
- export const deleteTexture = (
34
- c: WebGLRenderingContext,
35
- texture: WebGLTexture
36
- ) => {
37
- c.deleteTexture(texture)
38
- }
39
-
40
- // 空のテクスチャを作成
41
- export const createEmptyTexture = (
42
- c: WebGLRenderingContext,
43
- w = 1280,
44
- h = 800,
45
- format = c.RGBA,
46
- type = c.UNSIGNED_BYTE
47
- ) => {
48
- const texture = c.createTexture()
49
- if (!texture) throw new Error('Failed to create empty texture')
50
- c.bindTexture(c.TEXTURE_2D, texture)
51
- // prettier-ignore
52
- c.texImage2D(c.TEXTURE_2D, 0, format, w, h, 0, format, type, null)
53
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MIN_FILTER, c.LINEAR)
54
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MAG_FILTER, c.LINEAR)
55
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_S, c.CLAMP_TO_EDGE)
56
- c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_T, c.CLAMP_TO_EDGE)
57
- c.bindTexture(c.TEXTURE_2D, null)
58
- return texture
59
- }
60
-
61
- // キューブマップテクスチャを作成
62
- export const createCubeTexture = (
63
- c: WebGLRenderingContext,
64
- imgs: HTMLImageElement[]
65
- ) => {
66
- if (imgs.length !== 6)
67
- throw new Error('Cube texture requires exactly 6 imgs')
68
- const texture = c.createTexture()
69
- if (!texture) throw new Error('Failed to create cube texture')
70
- c.bindTexture(c.TEXTURE_CUBE_MAP, texture)
71
- const faces = [
72
- c.TEXTURE_CUBE_MAP_POSITIVE_X,
73
- c.TEXTURE_CUBE_MAP_NEGATIVE_X,
74
- c.TEXTURE_CUBE_MAP_POSITIVE_Y,
75
- c.TEXTURE_CUBE_MAP_NEGATIVE_Y,
76
- c.TEXTURE_CUBE_MAP_POSITIVE_Z,
77
- c.TEXTURE_CUBE_MAP_NEGATIVE_Z,
78
- ]
79
- // prettier-ignore
80
- faces.forEach((face, index) => {
81
- c.texImage2D( face, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, imgs[index])
82
- })
83
- c.generateMipmap(c.TEXTURE_CUBE_MAP)
84
- // prettier-ignore
85
- c.texParameteri(c.TEXTURE_CUBE_MAP, c.TEXTURE_MIN_FILTER, c.LINEAR_MIPMAP_LINEAR)
86
- c.texParameteri(c.TEXTURE_CUBE_MAP, c.TEXTURE_MAG_FILTER, c.LINEAR)
87
- c.texParameteri(c.TEXTURE_CUBE_MAP, c.TEXTURE_WRAP_S, c.CLAMP_TO_EDGE)
88
- c.texParameteri(c.TEXTURE_CUBE_MAP, c.TEXTURE_WRAP_T, c.CLAMP_TO_EDGE)
89
-
90
- c.bindTexture(c.TEXTURE_CUBE_MAP, null)
91
-
92
- return texture
93
- }
@@ -1,96 +0,0 @@
1
- import { is } from '../utils'
2
-
3
- // WebGPUバッファー管理
4
- export const createBuffer = (
5
- device: any,
6
- data: Float32Array | Uint32Array | number[],
7
- usage: number
8
- ) => {
9
- const array = is.arr(data) ? new Float32Array(data) : data
10
- const buffer = device.createBuffer({
11
- size: array.byteLength,
12
- usage,
13
- mappedAtCreation: true,
14
- })
15
- if (array instanceof Float32Array)
16
- new Float32Array(buffer.getMappedRange()).set(array)
17
- else if (array instanceof Uint32Array)
18
- new Uint32Array(buffer.getMappedRange()).set(array)
19
- buffer.unmap()
20
- return buffer
21
- }
22
-
23
- // 頂点バッファー作成
24
- export const createVertexBuffer = (device: any, data: number[]) => {
25
- return createBuffer(device, data, 0x20) // GPUBufferUsage.VERTEX
26
- }
27
-
28
- // インデックスバッファー作成
29
- export const createIndexBuffer = (device: any, data: number[]) => {
30
- return createBuffer(device, new Uint32Array(data), 0x40) // GPUBufferUsage.INDEX
31
- }
32
-
33
- // ユニフォームバッファー作成
34
- export const createUniformBuffer = (device: any, size: number) => {
35
- return device.createBuffer({
36
- size,
37
- usage: 0x40 | 0x4, // GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
38
- mappedAtCreation: false,
39
- })
40
- }
41
-
42
- // ストレージバッファー作成
43
- export const createStorageBuffer = (device: any, size: number) => {
44
- return device.createBuffer({
45
- size,
46
- usage: 0x80 | 0x4, // GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
47
- mappedAtCreation: false,
48
- })
49
- }
50
-
51
- // バッファーデータ更新
52
- export const updateBuffer = (
53
- device: any,
54
- buffer: any,
55
- data: Float32Array | Uint32Array | number[],
56
- offset = 0
57
- ) => {
58
- const array = is.arr(data) ? new Float32Array(data) : data
59
- device.queue.writeBuffer(buffer, offset, array)
60
- }
61
-
62
- // バッファーの削除
63
- export const destroyBuffer = (buffer: any) => buffer.destroy()
64
-
65
- // バッファーサイズの計算
66
- export const calculateBufferSize = (data: number[]) => data.length * 4 // 4 bytes per float
67
-
68
- // アライメント調整
69
- export const alignBufferSize = (size: number, alignment = 256) => {
70
- return Math.ceil(size / alignment) * alignment
71
- }
72
-
73
- // 複数のバッファーを作成
74
- export const createBuffers = (
75
- device: any,
76
- bufferConfigs: Array<{
77
- data: number[]
78
- usage: number
79
- }>
80
- ): any[] => {
81
- return bufferConfigs.map((config) => {
82
- return createBuffer(device, config.data, config.usage)
83
- })
84
- }
85
-
86
- // バッファー使用量の定数
87
- export const BufferUsage = {
88
- VERTEX: 0x20,
89
- INDEX: 0x40,
90
- UNIFORM: 0x40,
91
- STORAGE: 0x80,
92
- COPY_SRC: 0x4,
93
- COPY_DST: 0x8,
94
- MAP_READ: 0x1,
95
- MAP_WRITE: 0x2,
96
- } as const
@@ -1,91 +0,0 @@
1
- // WebGPUデバイス管理
2
- export const requestWebGPUDevice = async (): Promise<{
3
- adapter: any // GPUAdapter @TODO FIX
4
- device: any // GPUDevice @TODO FIX
5
- }> => {
6
- // @ts-ignore @TODO FIX
7
- if (!navigator.gpu)
8
- throw new Error('WebGPU is not supported in this browser')
9
- // @ts-ignore
10
- const adapter = await navigator.gpu.requestAdapter()
11
- if (!adapter) throw new Error('Failed to get WebGPU adapter')
12
- const device = await adapter.requestDevice()
13
- if (!device) throw new Error('Failed to get WebGPU device')
14
- return { adapter, device }
15
- }
16
-
17
- // WebGPUデバイスの機能チェック
18
- export const checkWebGPUSupport = (): boolean => {
19
- return 'gpu' in navigator
20
- }
21
-
22
- // デバイスの制限情報を取得
23
- export const getDeviceLimits = (device: any) => {
24
- return {
25
- maxTextureDimension1D: device.limits.maxTextureDimension1D,
26
- maxTextureDimension2D: device.limits.maxTextureDimension2D,
27
- maxTextureDimension3D: device.limits.maxTextureDimension3D,
28
- maxTextureArrayLayers: device.limits.maxTextureArrayLayers,
29
- maxBindGroups: device.limits.maxBindGroups,
30
- maxDynamicUniformBuffersPerPipelineLayout:
31
- device.limits.maxDynamicUniformBuffersPerPipelineLayout,
32
- maxDynamicStorageBuffersPerPipelineLayout:
33
- device.limits.maxDynamicStorageBuffersPerPipelineLayout,
34
- maxSampledTexturesPerShaderStage:
35
- device.limits.maxSampledTexturesPerShaderStage,
36
- maxSamplersPerShaderStage:
37
- device.limits.maxSamplersPerShaderStage,
38
- maxStorageBuffersPerShaderStage:
39
- device.limits.maxStorageBuffersPerShaderStage,
40
- maxStorageTexturesPerShaderStage:
41
- device.limits.maxStorageTexturesPerShaderStage,
42
- maxUniformBuffersPerShaderStage:
43
- device.limits.maxUniformBuffersPerShaderStage,
44
- maxUniformBufferBindingSize:
45
- device.limits.maxUniformBufferBindingSize,
46
- maxStorageBufferBindingSize:
47
- device.limits.maxStorageBufferBindingSize,
48
- maxVertexBuffers: device.limits.maxVertexBuffers,
49
- maxVertexAttributes: device.limits.maxVertexAttributes,
50
- maxVertexBufferArrayStride:
51
- device.limits.maxVertexBufferArrayStride,
52
- maxComputeWorkgroupStorageSize:
53
- device.limits.maxComputeWorkgroupStorageSize,
54
- maxComputeInvocationsPerWorkgroup:
55
- device.limits.maxComputeInvocationsPerWorkgroup,
56
- maxComputeWorkgroupSizeX:
57
- device.limits.maxComputeWorkgroupSizeX,
58
- maxComputeWorkgroupSizeY:
59
- device.limits.maxComputeWorkgroupSizeY,
60
- maxComputeWorkgroupSizeZ:
61
- device.limits.maxComputeWorkgroupSizeZ,
62
- maxComputeWorkgroupsPerDimension:
63
- device.limits.maxComputeWorkgroupsPerDimension,
64
- }
65
- }
66
-
67
- // デバイスエラーハンドリング
68
- export const setupDeviceErrorHandling = (device: any) => {
69
- device.addEventListener('uncapturederror', (event: any) => {
70
- console.error('WebGPU uncaptured error:', event.error)
71
- })
72
- }
73
-
74
- // キャンバスコンテキストの設定
75
- export const configureCanvasContext = (
76
- canvas: HTMLCanvasElement,
77
- device: any,
78
- format: any = 'bgra8unorm'
79
- ) => {
80
- const context = canvas.getContext('webgpu')
81
- if (!context) throw new Error('Failed to get WebGPU canvas context')
82
-
83
- // @ts-ignore
84
- context.configure({
85
- device,
86
- format,
87
- alphaMode: 'premultiplied',
88
- })
89
-
90
- return context
91
- }
@@ -1,40 +0,0 @@
1
- import { requestWebGPUDevice } from './device'
2
- import { createCommandEncoder, createRenderPass, createRenderPipeline } from './pipeline'
3
- import { wgsl } from '../code/wgsl'
4
- import { is } from '../utils'
5
- import type { X } from '../node'
6
- import type { GL } from '../types'
7
- export * from './buffer'
8
- export * from './device'
9
- export * from './pipeline'
10
- export * from './texture'
11
-
12
- export const webgpu = (gl: GL) => {
13
- const init = async () => {
14
- const { device } = await requestWebGPUDevice()
15
- gl.gl = { device }
16
- let vs = gl.vs || gl.vert || gl.vertex
17
- let fs = gl.fs || gl.frag || gl.fragment
18
- if (is.obj(vs)) vs = wgsl(fs as X)
19
- if (is.obj(fs)) fs = wgsl(fs as X)
20
- gl.gl.pipeline = createRenderPipeline(device, vs, fs)
21
- }
22
-
23
- const loop = () => {
24
- const { device, context, pipeline } = gl.gl
25
- const encoder = createCommandEncoder(device)
26
- const pass = createRenderPass(encoder, {
27
- view: context.getCurrentTexture().createView(),
28
- clearValue: { r: 0, g: 0, b: 0, a: 1 },
29
- loadOp: 'clear',
30
- storeOp: 'store',
31
- })
32
- pass.setPipeline(pipeline)
33
- pass.draw(gl.count)
34
- pass.end()
35
- device.queue.submit([encoder.finish()])
36
- }
37
-
38
- // @TODO add uniform and attribute
39
- return gl({ init, loop })
40
- }
@@ -1,94 +0,0 @@
1
- // デフォルトの頂点シェーダー
2
- const defaultVertexShader = `
3
- @vertex
4
- fn main(@builtin(vertex_index) vertexIndex: u32) -> @builtin(position) vec4<f32> {
5
- var pos = array<vec2<f32>, 6>(
6
- vec2<f32>(-1.0, -1.0),
7
- vec2<f32>( 1.0, -1.0),
8
- vec2<f32>(-1.0, 1.0),
9
- vec2<f32>(-1.0, 1.0),
10
- vec2<f32>( 1.0, -1.0),
11
- vec2<f32>( 1.0, 1.0)
12
- );
13
- return vec4<f32>(pos[vertexIndex], 0.0, 1.0);
14
- }
15
- `
16
-
17
- // デフォルトのフラグメントシェーダー
18
- const defaultFragmentShader = `
19
- @fragment
20
- fn main() -> @location(0) vec4<f32> {
21
- return vec4<f32>(1.0, 0.0, 0.0, 1.0);
22
- }
23
- `
24
-
25
- // WebGPUパイプライン管理
26
- export const createRenderPipeline = (
27
- device: any,
28
- vertexShader = defaultVertexShader,
29
- fragmentShader = defaultFragmentShader,
30
- format = 'bgra8unorm'
31
- ) => {
32
- const v = device.createShaderModule({ code: vertexShader })
33
- const f = device.createShaderModule({ code: fragmentShader })
34
- return device.createRenderPipeline({
35
- layout: 'auto',
36
- vertex: {
37
- module: v,
38
- entryPoint: 'main',
39
- },
40
- fragment: {
41
- module: f,
42
- entryPoint: 'main',
43
- targets: [
44
- {
45
- format,
46
- },
47
- ],
48
- },
49
- primitive: {
50
- topology: 'triangle-list',
51
- },
52
- })
53
- }
54
-
55
- // コンピュートパイプライン作成
56
- export const createComputePipeline = (device: any, computeShader: string) => {
57
- const computeModule = device.createShaderModule({ code: computeShader })
58
- return device.createComputePipeline({
59
- layout: 'auto',
60
- compute: { module: computeModule, entryPoint: 'main' },
61
- })
62
- }
63
-
64
- // シェーダーモジュール作成
65
- export const createShaderModule = (device: any, code: string) => {
66
- return device.createShaderModule({ code })
67
- }
68
-
69
- // バインドグループレイアウト作成
70
- export const createBindGroupLayout = (device: any, entries: any[]) => {
71
- return device.createBindGroupLayout({ entries })
72
- }
73
-
74
- // バインドグループ作成
75
- export const createBindGroup = (device: any, layout: any, entries: any[]) => {
76
- return device.createBindGroup({ layout, entries })
77
- }
78
-
79
- // レンダーパス作成
80
- export const createRenderPass = (
81
- encoder: any,
82
- colorAttachment: any,
83
- depthStencilAttachment?: any
84
- ) => {
85
- const descriptor: any = { colorAttachments: [colorAttachment] }
86
- if (depthStencilAttachment)
87
- descriptor.depthStencilAttachment = depthStencilAttachment
88
- return encoder.beginRenderPass(descriptor)
89
- }
90
-
91
- // コマンドエンコーダー作成
92
- export const createCommandEncoder = (device: any) => {
93
- return device.createCommandEncoder()
94
- }
@@ -1,139 +0,0 @@
1
- // WebGPUテクスチャ管理
2
- const createTexture = (
3
- device: any,
4
- w = 1280,
5
- h = 800,
6
- format = 'rgba8unorm',
7
- usage = 0x14 // GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
8
- ) => {
9
- return device.createTexture({
10
- size: { width: w, height: h, depthOrArrayLayers: 1 },
11
- format,
12
- usage,
13
- })
14
- }
15
-
16
- // 画像からテクスチャ作成
17
- export const createTextureFromImage = (
18
- device: any,
19
- image: HTMLImageElement | ImageBitmap,
20
- format = 'rgba8unorm'
21
- ) => {
22
- const texture = createTexture(
23
- device,
24
- image.width,
25
- image.height,
26
- format,
27
- 0x14 | 0x4 // TEXTURE_BINDING | COPY_DST
28
- )
29
-
30
- device.queue.copyExternalImageToTexture(
31
- { source: image },
32
- { texture },
33
- { width: image.width, height: image.height }
34
- )
35
-
36
- return texture
37
- }
38
-
39
- // キューブマップテクスチャ作成
40
- const createCubeTexture = (
41
- device: any,
42
- size: number,
43
- format = 'rgba8unorm'
44
- ) => {
45
- return device.createTexture({
46
- size: { width: size, height: size, depthOrArrayLayers: 6 },
47
- format,
48
- usage: 0x14, // GPUTextureUsage.TEXTURE_BINDING
49
- dimension: '2d',
50
- })
51
- }
52
-
53
- // 深度テクスチャ作成
54
- export const createDepthTexture = (
55
- device: any,
56
- w = 1280,
57
- h = 800,
58
- format = 'depth24plus'
59
- ) => {
60
- return device.createTexture({
61
- size: { width: w, height: h, depthOrArrayLayers: 1 },
62
- format,
63
- usage: 0x10, // GPUTextureUsage.RENDER_ATTACHMENT
64
- })
65
- }
66
-
67
- // サンプラー作成
68
- export const createSampler = (
69
- device: any,
70
- options: {
71
- magFilter?: string
72
- minFilter?: string
73
- addressModeU?: string
74
- addressModeV?: string
75
- addressModeW?: string
76
- } = {}
77
- ) => {
78
- return device.createSampler({
79
- magFilter: options.magFilter || 'linear',
80
- minFilter: options.minFilter || 'linear',
81
- addressModeU: options.addressModeU || 'repeat',
82
- addressModeV: options.addressModeV || 'repeat',
83
- addressModeW: options.addressModeW || 'repeat',
84
- })
85
- }
86
-
87
- // テクスチャビュー作成
88
- export const createTextureView = (
89
- texture: any,
90
- options: {
91
- format?: string
92
- dimension?: string
93
- aspect?: string
94
- baseMipLevel?: number
95
- mipLevelCount?: number
96
- baseArrayLayer?: number
97
- arrayLayerCount?: number
98
- } = {}
99
- ) => {
100
- return texture.createView(options)
101
- }
102
-
103
- // テクスチャデータ更新
104
- export const updateTexture = (
105
- device: any,
106
- texture: any,
107
- data: Uint8Array | Uint8ClampedArray,
108
- w = 1280,
109
- h = 800
110
- ) => {
111
- device.queue.writeTexture(
112
- { texture },
113
- data,
114
- { bytesPerRow: w * 4 },
115
- { width: w, height: h }
116
- )
117
- }
118
-
119
- // テクスチャの削除
120
- export const destroyTexture = (texture: any) => texture.destroy()
121
-
122
- // テクスチャ使用量の定数
123
- export const TextureUsage = {
124
- COPY_SRC: 0x1,
125
- COPY_DST: 0x2,
126
- TEXTURE_BINDING: 0x4,
127
- STORAGE_BINDING: 0x8,
128
- RENDER_ATTACHMENT: 0x10,
129
- } as const
130
-
131
- // テクスチャフォーマットの定数
132
- export const TextureFormat = {
133
- RGBA8_UNORM: 'rgba8unorm',
134
- RGBA8_SRGB: 'rgba8unorm-srgb',
135
- BGRA8_UNORM: 'bgra8unorm',
136
- BGRA8_SRGB: 'bgra8unorm-srgb',
137
- DEPTH24_PLUS: 'depth24plus',
138
- DEPTH32_FLOAT: 'depth32float',
139
- } as const