forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeShaderParticleSystem.ts
More file actions
184 lines (150 loc) · 7.98 KB
/
Copy pathcomputeShaderParticleSystem.ts
File metadata and controls
184 lines (150 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { ThinEngine } from "../Engines/thinEngine";
import { StorageBuffer } from "../Buffers/storageBuffer";
import { ComputeShader } from "../Compute/computeShader";
import { UniformBuffer } from "../Materials/uniformBuffer";
import { IGPUParticleSystemPlatform } from "./IGPUParticleSystemPlatform";
import { Buffer } from "../Buffers/buffer";
import { GPUParticleSystem } from "./gpuParticleSystem";
import { VertexBuffer } from "../Buffers/buffer";
import { DataArray } from "../types";
import { DataBuffer } from "../Buffers/dataBuffer";
import { Constants } from "../Engines/constants";
import { UniformBufferEffectCommonAccessor } from "../Materials/uniformBufferEffectCommonAccessor";
import { ComputeBindingMapping } from "../Engines/Extensions/engine.computeShader";
import { Effect } from "../Materials/effect";
import { RegisterClass } from "../Misc/typeStore";
import "../ShadersWGSL/gpuUpdateParticles.compute";
/** @hidden */
export class ComputeShaderParticleSystem implements IGPUParticleSystemPlatform {
private _parent: GPUParticleSystem;
private _engine: ThinEngine;
private _updateComputeShader: ComputeShader;
private _simParamsComputeShader: UniformBuffer;
private _bufferComputeShader: StorageBuffer[] = [];
private _renderVertexBuffers: Array<{ [key: string]: VertexBuffer }> = [];
public readonly alignDataInBuffer = true;
constructor(parent: GPUParticleSystem, engine: ThinEngine) {
this._parent = parent;
this._engine = engine;
}
public isUpdateBufferCreated(): boolean {
return !!this._updateComputeShader;
}
public isUpdateBufferReady(): boolean {
return this._updateComputeShader?.isReady() ?? false;
}
public createUpdateBuffer(defines: string): UniformBufferEffectCommonAccessor {
const bindingsMapping: ComputeBindingMapping = {
"params": { group: 0, binding: 0 },
"particlesIn": { group: 0, binding: 1 },
"particlesOut": { group: 0, binding: 2 },
"randomTexture": { group: 0, binding: 3 },
"randomTexture2": { group: 0, binding: 4 },
};
if (this._parent._sizeGradientsTexture) {
bindingsMapping["sizeGradientTexture"] = { group: 1, binding: 1 };
}
if (this._parent._angularSpeedGradientsTexture) {
bindingsMapping["angularSpeedGradientTexture"] = { group: 1, binding: 3 };
}
if (this._parent._velocityGradientsTexture) {
bindingsMapping["velocityGradientTexture"] = { group: 1, binding: 5 };
}
if (this._parent._limitVelocityGradientsTexture) {
bindingsMapping["limitVelocityGradientTexture"] = { group: 1, binding: 7 };
}
if (this._parent._dragGradientsTexture) {
bindingsMapping["dragGradientTexture"] = { group: 1, binding: 9 };
}
if (this._parent.noiseTexture) {
bindingsMapping["noiseTexture"] = { group: 1, binding: 11 };
}
this._updateComputeShader = new ComputeShader("updateParticles", this._engine, "gpuUpdateParticles", { bindingsMapping, defines: defines.split("\n") });
this._simParamsComputeShader?.dispose();
this._simParamsComputeShader = new UniformBuffer(this._engine);
this._simParamsComputeShader.addUniform("currentCount", 1);
this._simParamsComputeShader.addUniform("timeDelta", 1);
this._simParamsComputeShader.addUniform("stopFactor", 1);
this._simParamsComputeShader.addUniform("randomTextureSize", 1);
this._simParamsComputeShader.addUniform("lifeTime", 2);
this._simParamsComputeShader.addUniform("emitPower", 2);
if (!this._parent._colorGradientsTexture) {
this._simParamsComputeShader.addUniform("color1", 4);
this._simParamsComputeShader.addUniform("color2", 4);
}
this._simParamsComputeShader.addUniform("sizeRange", 2);
this._simParamsComputeShader.addUniform("scaleRange", 4);
this._simParamsComputeShader.addUniform("angleRange", 4);
this._simParamsComputeShader.addUniform("gravity", 3);
if (this._parent._limitVelocityGradientsTexture) {
this._simParamsComputeShader.addUniform("limitVelocityDamping", 1);
}
if (this._parent.isAnimationSheetEnabled) {
this._simParamsComputeShader.addUniform("cellInfos", 4);
}
if (this._parent.noiseTexture) {
this._simParamsComputeShader.addUniform("noiseStrength", 3);
}
if (!this._parent.isLocal) {
this._simParamsComputeShader.addUniform("emitterWM", 16);
}
if (this._parent.particleEmitterType) {
this._parent.particleEmitterType.buildUniformLayout(this._simParamsComputeShader);
}
this._updateComputeShader.setUniformBuffer("params", this._simParamsComputeShader);
return new UniformBufferEffectCommonAccessor(this._simParamsComputeShader);
}
public createVertexBuffers(updateBuffer: Buffer, renderVertexBuffers: { [key: string]: VertexBuffer }): void {
this._renderVertexBuffers.push(renderVertexBuffers);
}
public createParticleBuffer(data: number[]): DataArray | DataBuffer {
const buffer = new StorageBuffer(this._engine, data.length * 4, Constants.BUFFER_CREATIONFLAG_READWRITE | Constants.BUFFER_CREATIONFLAG_VERTEX);
buffer.update(data);
this._bufferComputeShader.push(buffer);
return buffer.getBuffer();
}
public bindDrawBuffers(index: number, effect: Effect): void {
this._engine.bindBuffers(this._renderVertexBuffers[index], null, effect);
}
public preUpdateParticleBuffer(): void {
}
public updateParticleBuffer(index: number, targetBuffer: Buffer, currentActiveCount: number): void {
this._simParamsComputeShader.update();
this._updateComputeShader.setTexture("randomTexture", this._parent._randomTexture, false);
this._updateComputeShader.setTexture("randomTexture2", this._parent._randomTexture2, false);
if (this._parent._sizeGradientsTexture) {
this._updateComputeShader.setTexture("sizeGradientTexture", this._parent._sizeGradientsTexture);
}
if (this._parent._angularSpeedGradientsTexture) {
this._updateComputeShader.setTexture("angularSpeedGradientTexture", this._parent._angularSpeedGradientsTexture);
}
if (this._parent._velocityGradientsTexture) {
this._updateComputeShader.setTexture("velocityGradientTexture", this._parent._velocityGradientsTexture);
}
if (this._parent._limitVelocityGradientsTexture) {
this._updateComputeShader.setTexture("limitVelocityGradientTexture", this._parent._limitVelocityGradientsTexture);
}
if (this._parent._dragGradientsTexture) {
this._updateComputeShader.setTexture("dragGradientTexture", this._parent._dragGradientsTexture);
}
if (this._parent.noiseTexture) {
this._updateComputeShader.setTexture("noiseTexture", this._parent.noiseTexture);
}
this._updateComputeShader.setStorageBuffer("particlesIn", this._bufferComputeShader[index]);
this._updateComputeShader.setStorageBuffer("particlesOut", this._bufferComputeShader[index ^ 1]);
this._updateComputeShader.dispatch(Math.ceil(currentActiveCount / 64));
}
public releaseBuffers(): void {
for (let i = 0; i < this._bufferComputeShader.length; ++i) {
this._bufferComputeShader[i].dispose();
}
this._bufferComputeShader = [];
this._simParamsComputeShader?.dispose();
(<any>this._simParamsComputeShader) = null;
(<any>this._updateComputeShader) = null;
}
public releaseVertexBuffers(): void {
this._renderVertexBuffers = [];
}
}
RegisterClass("BABYLON.ComputeShaderParticleSystem", ComputeShaderParticleSystem);