react-native-shine 0.7.0 → 0.8.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.
- package/lib/module/components/Content.js +126 -135
- package/lib/module/components/Content.js.map +1 -1
- package/lib/module/components/Shine.js +22 -3
- package/lib/module/components/Shine.js.map +1 -1
- package/lib/module/components/ShineGroup.js +0 -4
- package/lib/module/components/ShineGroup.js.map +1 -1
- package/lib/module/enums/colorPresets.js +192 -0
- package/lib/module/enums/colorPresets.js.map +1 -1
- package/lib/module/enums/effectDefaults.js +59 -0
- package/lib/module/enums/effectDefaults.js.map +1 -0
- package/lib/module/enums/effectPresets.js +67 -10
- package/lib/module/enums/effectPresets.js.map +1 -1
- package/lib/module/shaders/bindGroupLayouts.js +50 -27
- package/lib/module/shaders/bindGroupLayouts.js.map +1 -1
- package/lib/module/shaders/bindGroupUtils.js +30 -44
- package/lib/module/shaders/bindGroupUtils.js.map +1 -1
- package/lib/module/shaders/colorConversions.js +78 -0
- package/lib/module/shaders/colorConversions.js.map +1 -0
- package/lib/module/shaders/computeShaders/precomputeColorMask.js +34 -0
- package/lib/module/shaders/computeShaders/precomputeColorMask.js.map +1 -0
- package/lib/module/shaders/fragmentShaders/baseTextureFragment.js +4 -9
- package/lib/module/shaders/fragmentShaders/baseTextureFragment.js.map +1 -1
- package/lib/module/shaders/fragmentShaders/colorMaskFragment.js +28 -41
- package/lib/module/shaders/fragmentShaders/colorMaskFragment.js.map +1 -1
- package/lib/module/shaders/fragmentShaders/glareFlareFragment.js +84 -0
- package/lib/module/shaders/fragmentShaders/glareFlareFragment.js.map +1 -0
- package/lib/module/shaders/fragmentShaders/glareFragment.js +8 -88
- package/lib/module/shaders/fragmentShaders/glareFragment.js.map +1 -1
- package/lib/module/shaders/fragmentShaders/holoFragment.js +120 -11
- package/lib/module/shaders/fragmentShaders/holoFragment.js.map +1 -1
- package/lib/module/shaders/fragmentShaders/maskFragment.js +5 -4
- package/lib/module/shaders/fragmentShaders/maskFragment.js.map +1 -1
- package/lib/module/shaders/fragmentShaders/reverseHoloFragment.js +7 -7
- package/lib/module/shaders/fragmentShaders/reverseHoloFragment.js.map +1 -1
- package/lib/module/shaders/pipelineSetups.js +5 -62
- package/lib/module/shaders/pipelineSetups.js.map +1 -1
- package/lib/module/shaders/resourceManagement/bufferManager.js.map +1 -1
- package/lib/module/shaders/resourceManagement/buffersMap.js +41 -0
- package/lib/module/shaders/resourceManagement/buffersMap.js.map +1 -0
- package/lib/module/shaders/resourceManagement/pipelineMap.js +115 -0
- package/lib/module/shaders/resourceManagement/pipelineMap.js.map +1 -0
- package/lib/module/shaders/resourceManagement/textures.js +2 -2
- package/lib/module/shaders/resourceManagement/textures.js.map +1 -1
- package/lib/module/shaders/tgpuUtils.js +41 -0
- package/lib/module/shaders/tgpuUtils.js.map +1 -1
- package/lib/module/shaders/utils.js +21 -2
- package/lib/module/shaders/utils.js.map +1 -1
- package/lib/module/shaders/vertexShaders/mainRotationEffectVertex.js +4 -4
- package/lib/module/shaders/vertexShaders/mainRotationEffectVertex.js.map +1 -1
- package/lib/module/types/typeUtils.js +29 -159
- package/lib/module/types/typeUtils.js.map +1 -1
- package/lib/module/types/types.js +8 -1
- package/lib/module/types/types.js.map +1 -1
- package/lib/module/utils/size.js +4 -4
- package/lib/module/utils/vector.js +38 -38
- package/lib/typescript/src/components/Content.d.ts +6 -6
- package/lib/typescript/src/components/Content.d.ts.map +1 -1
- package/lib/typescript/src/components/Shine.d.ts.map +1 -1
- package/lib/typescript/src/components/ShineGroup.d.ts +1 -1
- package/lib/typescript/src/components/ShineGroup.d.ts.map +1 -1
- package/lib/typescript/src/enums/colorPresets.d.ts +152 -0
- package/lib/typescript/src/enums/colorPresets.d.ts.map +1 -1
- package/lib/typescript/src/enums/effectDefaults.d.ts +39 -0
- package/lib/typescript/src/enums/effectDefaults.d.ts.map +1 -0
- package/lib/typescript/src/enums/effectPresets.d.ts +284 -5
- package/lib/typescript/src/enums/effectPresets.d.ts.map +1 -1
- package/lib/typescript/src/shaders/bindGroupLayouts.d.ts +82 -41
- package/lib/typescript/src/shaders/bindGroupLayouts.d.ts.map +1 -1
- package/lib/typescript/src/shaders/bindGroupUtils.d.ts +86 -70
- package/lib/typescript/src/shaders/bindGroupUtils.d.ts.map +1 -1
- package/lib/typescript/src/shaders/colorConversions.d.ts +4 -0
- package/lib/typescript/src/shaders/colorConversions.d.ts.map +1 -0
- package/lib/typescript/src/shaders/computeShaders/precomputeColorMask.d.ts +5 -0
- package/lib/typescript/src/shaders/computeShaders/precomputeColorMask.d.ts.map +1 -0
- package/lib/typescript/src/shaders/fragmentShaders/baseTextureFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/fragmentShaders/colorMaskFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/fragmentShaders/glareFlareFragment.d.ts +5 -0
- package/lib/typescript/src/shaders/fragmentShaders/glareFlareFragment.d.ts.map +1 -0
- package/lib/typescript/src/shaders/fragmentShaders/glareFragment.d.ts +0 -3
- package/lib/typescript/src/shaders/fragmentShaders/glareFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/fragmentShaders/holoFragment.d.ts +7 -0
- package/lib/typescript/src/shaders/fragmentShaders/holoFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/fragmentShaders/maskFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/fragmentShaders/reverseHoloFragment.d.ts.map +1 -1
- package/lib/typescript/src/shaders/pipelineSetups.d.ts +3 -10
- package/lib/typescript/src/shaders/pipelineSetups.d.ts.map +1 -1
- package/lib/typescript/src/shaders/resourceManagement/bufferManager.d.ts +5 -3
- package/lib/typescript/src/shaders/resourceManagement/bufferManager.d.ts.map +1 -1
- package/lib/typescript/src/shaders/resourceManagement/buffersMap.d.ts +10 -0
- package/lib/typescript/src/shaders/resourceManagement/buffersMap.d.ts.map +1 -0
- package/lib/typescript/src/shaders/resourceManagement/pipelineMap.d.ts +24 -0
- package/lib/typescript/src/shaders/resourceManagement/pipelineMap.d.ts.map +1 -0
- package/lib/typescript/src/shaders/resourceManagement/textures.d.ts +0 -1
- package/lib/typescript/src/shaders/resourceManagement/textures.d.ts.map +1 -1
- package/lib/typescript/src/shaders/tgpuUtils.d.ts +3 -0
- package/lib/typescript/src/shaders/tgpuUtils.d.ts.map +1 -1
- package/lib/typescript/src/shaders/utils.d.ts +1 -0
- package/lib/typescript/src/shaders/utils.d.ts.map +1 -1
- package/lib/typescript/src/types/typeUtils.d.ts +16 -51
- package/lib/typescript/src/types/typeUtils.d.ts.map +1 -1
- package/lib/typescript/src/types/types.d.ts +59 -54
- package/lib/typescript/src/types/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/Content.tsx +130 -269
- package/src/components/Shine.tsx +28 -3
- package/src/components/ShineGroup.tsx +0 -4
- package/src/enums/colorPresets.ts +59 -1
- package/src/enums/effectDefaults.ts +57 -0
- package/src/enums/effectPresets.ts +142 -9
- package/src/shaders/bindGroupLayouts.ts +64 -28
- package/src/shaders/bindGroupUtils.ts +66 -69
- package/src/shaders/colorConversions.ts +88 -0
- package/src/shaders/computeShaders/precomputeColorMask.ts +29 -0
- package/src/shaders/fragmentShaders/baseTextureFragment.ts +2 -10
- package/src/shaders/fragmentShaders/colorMaskFragment.ts +34 -55
- package/src/shaders/fragmentShaders/glareFlareFragment.ts +79 -0
- package/src/shaders/fragmentShaders/glareFragment.ts +9 -84
- package/src/shaders/fragmentShaders/holoFragment.ts +139 -11
- package/src/shaders/fragmentShaders/maskFragment.ts +3 -9
- package/src/shaders/fragmentShaders/reverseHoloFragment.ts +4 -9
- package/src/shaders/pipelineSetups.ts +16 -125
- package/src/shaders/resourceManagement/bufferManager.ts +8 -3
- package/src/shaders/resourceManagement/buffersMap.ts +80 -0
- package/src/shaders/resourceManagement/pipelineMap.ts +169 -0
- package/src/shaders/resourceManagement/textures.ts +2 -2
- package/src/shaders/tgpuUtils.ts +29 -0
- package/src/shaders/utils.ts +29 -0
- package/src/shaders/vertexShaders/mainRotationEffectVertex.ts +2 -2
- package/src/types/typeUtils.ts +53 -164
- package/src/types/types.ts +111 -60
|
@@ -2,12 +2,11 @@ import tgpu from 'typegpu';
|
|
|
2
2
|
import * as d from 'typegpu/data';
|
|
3
3
|
import * as std from 'typegpu/std';
|
|
4
4
|
import {
|
|
5
|
-
textureBindGroupLayout,
|
|
6
5
|
maskTextureBindGroupLayout,
|
|
7
|
-
rotationBindGroupLayout,
|
|
8
6
|
reverseHoloDetectionChannelFlagsBindGroupLayout,
|
|
7
|
+
sharedBindGroupLayout,
|
|
9
8
|
} from '../bindGroupLayouts';
|
|
10
|
-
import { hueShift, rgbToHSV } from '../tgpuUtils';
|
|
9
|
+
import { getPixelColorFromVector, hueShift, rgbToHSV } from '../tgpuUtils';
|
|
11
10
|
|
|
12
11
|
export const reverseHoloFragment = tgpu['~unstable'].fragmentFn({
|
|
13
12
|
in: { uv: d.vec2f },
|
|
@@ -17,7 +16,7 @@ export const reverseHoloFragment = tgpu['~unstable'].fragmentFn({
|
|
|
17
16
|
const uv = texcoord;
|
|
18
17
|
const centeredCoords = std.sub(std.mul(uv, 2.0), 1.0);
|
|
19
18
|
|
|
20
|
-
const rot =
|
|
19
|
+
const rot = sharedBindGroupLayout.$.rot;
|
|
21
20
|
const center = std.add(d.vec2f(0.0), d.vec2f(rot.x, rot.y)); // center from device orientation/touch
|
|
22
21
|
|
|
23
22
|
// glare options---------------------------------
|
|
@@ -43,11 +42,7 @@ export const reverseHoloFragment = tgpu['~unstable'].fragmentFn({
|
|
|
43
42
|
const valueFlag = detectionChannelFlags.value;
|
|
44
43
|
//------------------------------------------------
|
|
45
44
|
|
|
46
|
-
const cardColor =
|
|
47
|
-
textureBindGroupLayout.$.texture,
|
|
48
|
-
textureBindGroupLayout.$.sampler,
|
|
49
|
-
texcoord
|
|
50
|
-
);
|
|
45
|
+
const cardColor = getPixelColorFromVector(texcoord);
|
|
51
46
|
|
|
52
47
|
const holoMaskColor = std.textureSample(
|
|
53
48
|
maskTextureBindGroupLayout.$.texture,
|
|
@@ -1,45 +1,31 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
TgpuBindGroup,
|
|
3
|
+
TgpuComputePipeline,
|
|
3
4
|
TgpuRenderPipeline,
|
|
4
5
|
TgpuRoot,
|
|
5
6
|
TgpuTexture,
|
|
6
7
|
} from 'typegpu';
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
maskTextureBindGroupLayout,
|
|
10
|
-
textureBindGroupLayout,
|
|
11
|
-
} from './bindGroupLayouts';
|
|
8
|
+
import { maskTextureBindGroupLayout } from './bindGroupLayouts';
|
|
12
9
|
import mainVertex from './vertexShaders/mainVertex';
|
|
13
10
|
import maskFragment from './fragmentShaders/maskFragment';
|
|
14
|
-
import {
|
|
15
|
-
import { holoFragment } from './fragmentShaders/holoFragment';
|
|
16
|
-
import {
|
|
17
|
-
WAVE_CALLBACKS,
|
|
18
|
-
waveCallbackFn,
|
|
19
|
-
waveCallbackSlot,
|
|
20
|
-
} from '../enums/waveCallback';
|
|
11
|
+
import { blend } from '../enums/effectPresets';
|
|
21
12
|
|
|
22
|
-
export
|
|
13
|
+
export function attachBindGroups(
|
|
23
14
|
pipeline: TgpuRenderPipeline,
|
|
24
15
|
bindGroups: TgpuBindGroup[]
|
|
25
|
-
)
|
|
26
|
-
bindGroups.reduce(
|
|
27
|
-
(acc, bindGroup) => acc.with(bindGroup.layout, bindGroup),
|
|
28
|
-
pipeline
|
|
29
|
-
);
|
|
16
|
+
): TgpuRenderPipeline;
|
|
30
17
|
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
18
|
+
export function attachBindGroups(
|
|
19
|
+
pipeline: TgpuComputePipeline,
|
|
20
|
+
bindGroups: TgpuBindGroup[]
|
|
21
|
+
): TgpuComputePipeline;
|
|
22
|
+
|
|
23
|
+
export function attachBindGroups(
|
|
24
|
+
pipeline: TgpuRenderPipeline | TgpuComputePipeline,
|
|
25
|
+
bindGroups: TgpuBindGroup[]
|
|
26
|
+
) {
|
|
27
|
+
return bindGroups.reduce((acc, bindGroup) => acc.with(bindGroup), pipeline);
|
|
28
|
+
}
|
|
43
29
|
|
|
44
30
|
export const getDefaultTarget = (
|
|
45
31
|
presentationFormat: GPUTextureFormat,
|
|
@@ -51,17 +37,6 @@ export const getDefaultTarget = (
|
|
|
51
37
|
};
|
|
52
38
|
};
|
|
53
39
|
|
|
54
|
-
export const attachBindGroupsToPass = (
|
|
55
|
-
pass: any,
|
|
56
|
-
bindGroupPairs: BindGroupPair[]
|
|
57
|
-
) => {
|
|
58
|
-
for (const pair of bindGroupPairs) {
|
|
59
|
-
pass.setBindGroup(pair.layout, pair.group);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return pass;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
40
|
export const createMaskPipeline = (
|
|
66
41
|
root: TgpuRoot,
|
|
67
42
|
maskTexture: TgpuTexture | undefined,
|
|
@@ -90,87 +65,3 @@ export const createMaskPipeline = (
|
|
|
90
65
|
|
|
91
66
|
return maskPipeline;
|
|
92
67
|
};
|
|
93
|
-
|
|
94
|
-
export const createReverseHoloPipeline = (
|
|
95
|
-
root: TgpuRoot,
|
|
96
|
-
texture: TgpuTexture | undefined,
|
|
97
|
-
bindGroups: TgpuBindGroup[],
|
|
98
|
-
sampler: GPUSampler,
|
|
99
|
-
presentationFormat: GPUTextureFormat
|
|
100
|
-
): TgpuRenderPipeline | void => {
|
|
101
|
-
if (!texture) return;
|
|
102
|
-
|
|
103
|
-
const reverseHoloBindGroup = root.createBindGroup(
|
|
104
|
-
maskTextureBindGroupLayout,
|
|
105
|
-
{
|
|
106
|
-
texture: root.unwrap(texture).createView(),
|
|
107
|
-
sampler,
|
|
108
|
-
}
|
|
109
|
-
);
|
|
110
|
-
const reverseHoloBGP: TgpuBindGroup[] = [...bindGroups, reverseHoloBindGroup];
|
|
111
|
-
|
|
112
|
-
let reverseHoloPipeline = root['~unstable']
|
|
113
|
-
.withVertex(mainVertex, {})
|
|
114
|
-
.withFragment(
|
|
115
|
-
reverseHoloFragment,
|
|
116
|
-
getDefaultTarget(presentationFormat, blend)
|
|
117
|
-
)
|
|
118
|
-
.createPipeline();
|
|
119
|
-
reverseHoloPipeline = attachBindGroups(reverseHoloPipeline, reverseHoloBGP);
|
|
120
|
-
|
|
121
|
-
return reverseHoloPipeline;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
export const createRainbowHoloPipeline = (
|
|
125
|
-
root: TgpuRoot,
|
|
126
|
-
texture: TgpuTexture | undefined,
|
|
127
|
-
bindGroups: TgpuBindGroup[],
|
|
128
|
-
sampler: GPUSampler,
|
|
129
|
-
presentationFormat: GPUTextureFormat
|
|
130
|
-
): TgpuRenderPipeline | void => {
|
|
131
|
-
if (!texture) return;
|
|
132
|
-
|
|
133
|
-
const imageTextureBindGroup = root.createBindGroup(textureBindGroupLayout, {
|
|
134
|
-
texture: root.unwrap(texture).createView(),
|
|
135
|
-
sampler,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
let rainbowHoloPipeline = root['~unstable']
|
|
139
|
-
.with(waveCallbackSlot, waveCallbackFn(WAVE_CALLBACKS.default))
|
|
140
|
-
.withVertex(mainVertex, {})
|
|
141
|
-
.withFragment(holoFragment, getDefaultTarget(presentationFormat, blend))
|
|
142
|
-
.createPipeline();
|
|
143
|
-
|
|
144
|
-
rainbowHoloPipeline = attachBindGroups(rainbowHoloPipeline, [
|
|
145
|
-
...bindGroups,
|
|
146
|
-
imageTextureBindGroup,
|
|
147
|
-
]);
|
|
148
|
-
return rainbowHoloPipeline;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
export function renderPipelinesInSinglePass(
|
|
152
|
-
root: TgpuRoot,
|
|
153
|
-
pipelines: TgpuRenderPipeline[],
|
|
154
|
-
view: GPUTextureView
|
|
155
|
-
) {
|
|
156
|
-
const unstableRoot = root['~unstable'];
|
|
157
|
-
const attachment: GPURenderPassColorAttachment = {
|
|
158
|
-
view,
|
|
159
|
-
clearValue: [0, 0, 0, 0],
|
|
160
|
-
loadOp: 'clear',
|
|
161
|
-
storeOp: 'store',
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
unstableRoot.beginRenderPass(
|
|
165
|
-
{
|
|
166
|
-
colorAttachments: [attachment],
|
|
167
|
-
},
|
|
168
|
-
(pass) => {
|
|
169
|
-
for (const pipeline of pipelines) {
|
|
170
|
-
pass.setPipeline(pipeline);
|
|
171
|
-
pass.draw(6);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
);
|
|
175
|
-
unstableRoot.flush();
|
|
176
|
-
}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import { type BufferSchemas } from './../bindGroupLayouts';
|
|
1
2
|
import type { TgpuRoot, TgpuBuffer, ValidateBufferSchema } from 'typegpu';
|
|
2
3
|
import { debug } from '../../config/debugMode';
|
|
4
|
+
import type { Infer } from 'typegpu/data';
|
|
3
5
|
|
|
4
6
|
export type BufferUsageType = 'uniform' | 'storage' | 'vertex';
|
|
5
7
|
|
|
6
8
|
type BufferWithUsageFromEntry<
|
|
7
|
-
TEntry extends {
|
|
9
|
+
TEntry extends {
|
|
10
|
+
schema: ValidateBufferSchema<BufferSchemas>;
|
|
11
|
+
usage: BufferUsageType;
|
|
12
|
+
},
|
|
8
13
|
> = TEntry['usage'] extends 'uniform'
|
|
9
14
|
? TgpuBuffer<TEntry['schema']> & { usableAsUniform: true }
|
|
10
15
|
: TEntry['usage'] extends 'storage'
|
|
@@ -16,7 +21,7 @@ type BufferWithUsageFromEntry<
|
|
|
16
21
|
export class TypedBufferMap<
|
|
17
22
|
TSchemas extends Record<
|
|
18
23
|
string,
|
|
19
|
-
{ schema: ValidateBufferSchema<
|
|
24
|
+
{ schema: ValidateBufferSchema<BufferSchemas>; usage: BufferUsageType }
|
|
20
25
|
>,
|
|
21
26
|
> {
|
|
22
27
|
private buffers: {
|
|
@@ -41,7 +46,7 @@ export class TypedBufferMap<
|
|
|
41
46
|
addBuffer<K extends keyof TSchemas>(
|
|
42
47
|
root: TgpuRoot,
|
|
43
48
|
key: K,
|
|
44
|
-
initValues?: TSchemas[K]['schema']
|
|
49
|
+
initValues?: Infer<TSchemas[K]['schema']>
|
|
45
50
|
): BufferWithUsageFromEntry<TSchemas[K]> {
|
|
46
51
|
const entry = this.schemas[key];
|
|
47
52
|
if (!entry) {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
StorageFlag,
|
|
3
|
+
TgpuBuffer,
|
|
4
|
+
TgpuRoot,
|
|
5
|
+
ValidateStorageSchema,
|
|
6
|
+
ValidateUniformSchema,
|
|
7
|
+
} from 'typegpu';
|
|
8
|
+
import type { AnyWgslData, Infer } from 'typegpu/data';
|
|
9
|
+
import { debug } from '../../config/debugMode';
|
|
10
|
+
import type { DeepPartial, TgpuUniformBuffer } from '../../types/types';
|
|
11
|
+
import { deepMerge } from '../utils';
|
|
12
|
+
|
|
13
|
+
export class BuffersMap extends Map<AnyWgslData, TgpuBuffer<AnyWgslData>> {
|
|
14
|
+
constructor(private root: TgpuRoot) {
|
|
15
|
+
super();
|
|
16
|
+
}
|
|
17
|
+
syncUniformBuffer<Key extends ValidateUniformSchema<AnyWgslData>>(
|
|
18
|
+
schema: Key,
|
|
19
|
+
defaultOptions: Infer<Key>,
|
|
20
|
+
options?: DeepPartial<Infer<Key>>
|
|
21
|
+
): TgpuUniformBuffer<Key> {
|
|
22
|
+
const resolvedOptions = options
|
|
23
|
+
? deepMerge(defaultOptions, options)
|
|
24
|
+
: defaultOptions;
|
|
25
|
+
|
|
26
|
+
if (this.has(schema)) {
|
|
27
|
+
if (debug) {
|
|
28
|
+
console.warn(
|
|
29
|
+
`Buffer "${String(schema)}" already exists. Updating values.`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const buffer = this.get(schema)!;
|
|
34
|
+
buffer.write(resolvedOptions);
|
|
35
|
+
return buffer as TgpuUniformBuffer<Key>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const result = this.root.createUniform(
|
|
39
|
+
schema as ValidateUniformSchema<Key>,
|
|
40
|
+
resolvedOptions
|
|
41
|
+
);
|
|
42
|
+
const buffer = result.buffer;
|
|
43
|
+
|
|
44
|
+
this.set(schema, buffer);
|
|
45
|
+
|
|
46
|
+
return buffer;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
syncStorageBuffer<Key extends ValidateStorageSchema<AnyWgslData>>(
|
|
50
|
+
schema: Key,
|
|
51
|
+
defaultOptions: Infer<Key>,
|
|
52
|
+
options?: DeepPartial<Infer<Key>>
|
|
53
|
+
): TgpuBuffer<Key> & StorageFlag {
|
|
54
|
+
const resolvedOptions = options
|
|
55
|
+
? deepMerge(defaultOptions, options)
|
|
56
|
+
: defaultOptions;
|
|
57
|
+
|
|
58
|
+
if (this.has(schema)) {
|
|
59
|
+
if (debug) {
|
|
60
|
+
console.warn(
|
|
61
|
+
`Buffer "${String(schema)}" already exists. Updating values.`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const buffer = this.get(schema)!;
|
|
66
|
+
buffer.write(resolvedOptions);
|
|
67
|
+
return buffer as TgpuBuffer<Key> & StorageFlag;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const result = this.root.createMutable(
|
|
71
|
+
schema as ValidateStorageSchema<Key>,
|
|
72
|
+
resolvedOptions
|
|
73
|
+
);
|
|
74
|
+
const buffer = result.buffer;
|
|
75
|
+
|
|
76
|
+
this.set(schema, buffer);
|
|
77
|
+
|
|
78
|
+
return buffer;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TextureProps,
|
|
3
|
+
TgpuBindGroup,
|
|
4
|
+
TgpuComputeFn,
|
|
5
|
+
TgpuComputePipeline,
|
|
6
|
+
TgpuRenderPipeline,
|
|
7
|
+
TgpuRoot,
|
|
8
|
+
TgpuTexture,
|
|
9
|
+
} from 'typegpu';
|
|
10
|
+
import { vec3f } from 'typegpu/data';
|
|
11
|
+
import mainVertex from '../vertexShaders/mainVertex';
|
|
12
|
+
import type {
|
|
13
|
+
AnySchema,
|
|
14
|
+
BufferConfig,
|
|
15
|
+
ColorAttachment,
|
|
16
|
+
FragmentShaderReturnType,
|
|
17
|
+
FragmentType,
|
|
18
|
+
UnwrapLayout,
|
|
19
|
+
} from '../../types/types';
|
|
20
|
+
import { baseTextureFragment } from '../fragmentShaders/baseTextureFragment';
|
|
21
|
+
import {
|
|
22
|
+
sharedBindGroupLayout,
|
|
23
|
+
rotationSchema,
|
|
24
|
+
maskTextureBindGroupLayout,
|
|
25
|
+
} from '../bindGroupLayouts';
|
|
26
|
+
import { BuffersMap } from './buffersMap';
|
|
27
|
+
import { Effects } from '../../enums/effectPresets';
|
|
28
|
+
|
|
29
|
+
type PipelineMap<Key> = Map<Key, TgpuRenderPipeline<FragmentShaderReturnType>>;
|
|
30
|
+
type ComputePipelineMap<Key> = Map<Key, TgpuComputePipeline>;
|
|
31
|
+
|
|
32
|
+
const defaultAttachment = {
|
|
33
|
+
clearValue: [0, 0, 0, 0],
|
|
34
|
+
loadOp: 'load',
|
|
35
|
+
storeOp: 'store',
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
export class PipelineManager {
|
|
39
|
+
sharedBindGroup: TgpuBindGroup<UnwrapLayout<typeof sharedBindGroupLayout>>;
|
|
40
|
+
maskBindGroup: TgpuBindGroup<UnwrapLayout<typeof maskTextureBindGroupLayout>>;
|
|
41
|
+
buffersMap: BuffersMap;
|
|
42
|
+
pipelinesMap: PipelineMap<FragmentType>;
|
|
43
|
+
computePipelinesMap: ComputePipelineMap<TgpuComputeFn>;
|
|
44
|
+
constructor(
|
|
45
|
+
private root: TgpuRoot,
|
|
46
|
+
private presentationFormat: GPUTextureFormat,
|
|
47
|
+
imageTexture: TgpuTexture<TextureProps>,
|
|
48
|
+
maskTexture?: TgpuTexture<TextureProps>
|
|
49
|
+
) {
|
|
50
|
+
this.buffersMap = new BuffersMap(root);
|
|
51
|
+
this.pipelinesMap = new Map();
|
|
52
|
+
const rotationBuffer = this.buffersMap.syncUniformBuffer(
|
|
53
|
+
rotationSchema,
|
|
54
|
+
vec3f(0.0)
|
|
55
|
+
);
|
|
56
|
+
this.computePipelinesMap = new Map();
|
|
57
|
+
|
|
58
|
+
const sampler = root['~unstable'].createSampler({
|
|
59
|
+
magFilter: 'linear',
|
|
60
|
+
minFilter: 'linear',
|
|
61
|
+
mipmapFilter: 'linear',
|
|
62
|
+
});
|
|
63
|
+
this.sharedBindGroup = root.createBindGroup(sharedBindGroupLayout, {
|
|
64
|
+
texture: root.unwrap(imageTexture).createView(),
|
|
65
|
+
sampler,
|
|
66
|
+
rot: rotationBuffer,
|
|
67
|
+
});
|
|
68
|
+
//todo: make this optional and create when needed
|
|
69
|
+
this.maskBindGroup = root.createBindGroup(maskTextureBindGroupLayout, {
|
|
70
|
+
texture: root.unwrap(maskTexture || imageTexture).createView(),
|
|
71
|
+
sampler,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
addPipeline(
|
|
76
|
+
fragment: FragmentType,
|
|
77
|
+
bindGroupProp?: TgpuBindGroup[],
|
|
78
|
+
blend?: GPUBlendState
|
|
79
|
+
) {
|
|
80
|
+
if (this.pipelinesMap.has(fragment)) {
|
|
81
|
+
return this.pipelinesMap.get(fragment);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let pipeline = this.root['~unstable']
|
|
85
|
+
.withVertex(mainVertex, {})
|
|
86
|
+
.withFragment(fragment, { format: this.presentationFormat, blend })
|
|
87
|
+
.createPipeline();
|
|
88
|
+
|
|
89
|
+
let bindGroups: TgpuBindGroup<any>[] = [this.sharedBindGroup];
|
|
90
|
+
|
|
91
|
+
if (bindGroupProp) {
|
|
92
|
+
bindGroups = [...bindGroups, ...bindGroupProp];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const bindGroup of bindGroups) {
|
|
96
|
+
pipeline = pipeline.with(bindGroup);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.pipelinesMap.set(fragment, pipeline);
|
|
100
|
+
return pipeline;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
addComputePipeline(compute: TgpuComputeFn, bindGroupProp?: TgpuBindGroup[]) {
|
|
104
|
+
if (this.computePipelinesMap.has(compute)) {
|
|
105
|
+
return this.computePipelinesMap.get(compute);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let pipeline = this.root['~unstable'].withCompute(compute).createPipeline();
|
|
109
|
+
|
|
110
|
+
let bindGroups: TgpuBindGroup<any>[] = [this.sharedBindGroup];
|
|
111
|
+
|
|
112
|
+
if (bindGroupProp) {
|
|
113
|
+
bindGroups = [...bindGroups, ...bindGroupProp];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const bindGroup of bindGroups) {
|
|
117
|
+
pipeline = pipeline.with(bindGroup);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.computePipelinesMap.set(compute, pipeline);
|
|
121
|
+
return pipeline;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//TODO: fix any typing
|
|
125
|
+
addPipelineWithBuffer(name: keyof typeof Effects, options?: any) {
|
|
126
|
+
const { fragment, blend, buffers, bindGroupCreator } = Effects[name];
|
|
127
|
+
|
|
128
|
+
const genericBuffers = buffers as readonly BufferConfig<AnySchema>[];
|
|
129
|
+
|
|
130
|
+
const updatedBuffers = genericBuffers.map(({ schema, defaultOptions }) =>
|
|
131
|
+
this.buffersMap.syncUniformBuffer(schema, defaultOptions, options)
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
if (this.pipelinesMap.has(fragment)) {
|
|
135
|
+
return this.pipelinesMap.get(fragment);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const bindGroup = bindGroupCreator(
|
|
139
|
+
{ root: this.root, maskBindGroup: this.maskBindGroup },
|
|
140
|
+
updatedBuffers as any
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return this.addPipeline(fragment, bindGroup, blend);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async runComputePipeline(compute: TgpuComputeFn) {
|
|
147
|
+
const pipeline = this.computePipelinesMap.get(compute);
|
|
148
|
+
if (!pipeline) {
|
|
149
|
+
throw new Error('Compute pipeline not found');
|
|
150
|
+
}
|
|
151
|
+
pipeline.dispatchWorkgroups(1, 1, 1);
|
|
152
|
+
await this.root.device.queue.onSubmittedWorkDone();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
renderPipelines(view: GPUTextureView) {
|
|
156
|
+
this.pipelinesMap.forEach((pipeline, fragment) => {
|
|
157
|
+
const attachment: ColorAttachment = {
|
|
158
|
+
...defaultAttachment,
|
|
159
|
+
view,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
if (fragment === baseTextureFragment) {
|
|
163
|
+
attachment.loadOp = 'clear';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
pipeline.withColorAttachment(attachment).draw(6);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -37,13 +37,13 @@ export async function loadBitmap(
|
|
|
37
37
|
) {
|
|
38
38
|
const bitmap = await getBitmapFromURI(imageURI);
|
|
39
39
|
let texture = await createTexture(root, bitmap);
|
|
40
|
-
texture =
|
|
40
|
+
texture = addTextureUsage(texture, usage);
|
|
41
41
|
|
|
42
42
|
setTexture(texture);
|
|
43
43
|
await loadTexture(root, bitmap, texture);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
function addTextureUsage(
|
|
47
47
|
texture: TgpuTexture,
|
|
48
48
|
usage: ('sampled' | 'render')[]
|
|
49
49
|
) {
|
package/src/shaders/tgpuUtils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import tgpu from 'typegpu';
|
|
2
2
|
import * as std from 'typegpu/std';
|
|
3
3
|
import * as d from 'typegpu/data';
|
|
4
|
+
import { sharedBindGroupLayout } from './bindGroupLayouts';
|
|
4
5
|
|
|
5
6
|
export const hueShift = tgpu.fn(
|
|
6
7
|
[d.vec3f, d.f32],
|
|
@@ -160,3 +161,31 @@ export const linearstep = tgpu.fn(
|
|
|
160
161
|
const t = (x - num1) / (num2 - num1);
|
|
161
162
|
return std.clamp(t, 0, 1);
|
|
162
163
|
});
|
|
164
|
+
|
|
165
|
+
export const getPixelColorFromVector = tgpu.fn(
|
|
166
|
+
[d.vec2f],
|
|
167
|
+
d.vec4f
|
|
168
|
+
)((coords) => {
|
|
169
|
+
return std.textureSample(
|
|
170
|
+
sharedBindGroupLayout.$.texture,
|
|
171
|
+
sharedBindGroupLayout.$.sampler,
|
|
172
|
+
coords
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
export const getPixelColorFromNonReversedVector = tgpu.fn(
|
|
177
|
+
[d.vec2f],
|
|
178
|
+
d.vec4f
|
|
179
|
+
)((input) => {
|
|
180
|
+
const coords = d.vec2f(input.x, 1.0 - input.y);
|
|
181
|
+
return getPixelColorFromVector(coords);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
export const random = tgpu.fn(
|
|
185
|
+
[d.vec2f],
|
|
186
|
+
d.f32
|
|
187
|
+
)((st) => {
|
|
188
|
+
return std.fract(
|
|
189
|
+
std.sin(std.dot(st, d.vec2f(12.9898, 78.233))) * 437358.845701
|
|
190
|
+
);
|
|
191
|
+
});
|
package/src/shaders/utils.ts
CHANGED
|
@@ -57,3 +57,32 @@ export function subscribeToOrientationChange(
|
|
|
57
57
|
|
|
58
58
|
return () => subscription.remove();
|
|
59
59
|
}
|
|
60
|
+
|
|
61
|
+
export function deepMerge<T>(target: T, source: unknown): T {
|
|
62
|
+
if (
|
|
63
|
+
typeof target !== 'object' ||
|
|
64
|
+
target === null ||
|
|
65
|
+
typeof source !== 'object' ||
|
|
66
|
+
source === null
|
|
67
|
+
) {
|
|
68
|
+
return source as T;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const result = { ...target } as any;
|
|
72
|
+
const src = source as any;
|
|
73
|
+
|
|
74
|
+
for (const key in src) {
|
|
75
|
+
if (Object.prototype.hasOwnProperty.call(src, key)) {
|
|
76
|
+
if (
|
|
77
|
+
key in result &&
|
|
78
|
+
typeof result[key] === 'object' &&
|
|
79
|
+
result[key] !== null
|
|
80
|
+
) {
|
|
81
|
+
result[key] = deepMerge(result[key], src[key]);
|
|
82
|
+
} else {
|
|
83
|
+
result[key] = src[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import tgpu from 'typegpu';
|
|
2
2
|
import * as std from 'typegpu/std';
|
|
3
3
|
import * as d from 'typegpu/data';
|
|
4
|
-
import {
|
|
4
|
+
import { sharedBindGroupLayout } from '../bindGroupLayouts';
|
|
5
5
|
|
|
6
6
|
const mainRotationEffectVertex = tgpu['~unstable'].vertexFn({
|
|
7
7
|
in: { vertexIndex: d.builtin.vertexIndex },
|
|
8
8
|
out: { position: d.builtin.position, uv: d.vec2f },
|
|
9
9
|
})((input) => {
|
|
10
|
-
const rot =
|
|
10
|
+
const rot = sharedBindGroupLayout.$.rot;
|
|
11
11
|
|
|
12
12
|
// Maximum rotation angles in radians
|
|
13
13
|
const maxAngle = d.f32((25.0 * Math.PI) / 180.0);
|