not sure
This commit is contained in:
parent
560398fefb
commit
80ed37298b
73 changed files with 3619 additions and 1295 deletions
|
|
@ -15,7 +15,6 @@ struct Counters {
|
|||
|
||||
var<workgroup> workgroupAliveCount: atomic<u32>;
|
||||
var<workgroup> workgroupCompactedOffset: u32;
|
||||
var<workgroup> workgroupCopyCount: u32;
|
||||
|
||||
@compute @workgroup_size(64)
|
||||
fn main(
|
||||
|
|
@ -31,9 +30,12 @@ fn main(
|
|||
workgroupBarrier();
|
||||
|
||||
var localCompactedIndex = 0u;
|
||||
var agent = Agent(vec2<f32>(0.0, 0.0), 0.0, -1.0, vec2<f32>(-1.0, -1.0), 0.0, 0.0);
|
||||
var isAlive = false;
|
||||
if id < settings.agentCount {
|
||||
let agent = agents[id];
|
||||
if agent.colorIndex >= 0.0 {
|
||||
agent = agents[id];
|
||||
isAlive = agent.colorIndex >= 0.0;
|
||||
if isAlive {
|
||||
localCompactedIndex = atomicAdd(&workgroupAliveCount, 1u);
|
||||
}
|
||||
}
|
||||
|
|
@ -51,30 +53,7 @@ fn main(
|
|||
|
||||
workgroupBarrier();
|
||||
|
||||
if id < settings.agentCount {
|
||||
let agent = agents[id];
|
||||
if agent.colorIndex >= 0.0 {
|
||||
compactedAgents[workgroupCompactedOffset + localCompactedIndex] = agent;
|
||||
}
|
||||
if isAlive {
|
||||
compactedAgents[workgroupCompactedOffset + localCompactedIndex] = agent;
|
||||
}
|
||||
}
|
||||
|
||||
@compute @workgroup_size(64)
|
||||
fn copyCompactedAgents(
|
||||
@builtin(global_invocation_id) global_id: vec3<u32>,
|
||||
@builtin(local_invocation_id) local_id: vec3<u32>
|
||||
) {
|
||||
let id = get_id(global_id);
|
||||
|
||||
if local_id.x == 0u {
|
||||
workgroupCopyCount = atomicLoad(&counters.aliveAgentCount);
|
||||
}
|
||||
|
||||
workgroupBarrier();
|
||||
|
||||
if id >= workgroupCopyCount {
|
||||
return;
|
||||
}
|
||||
|
||||
agents[id] = compactedAgents[id];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -177,17 +177,14 @@ const createPipeline = (compactedCount: number) => {
|
|||
};
|
||||
|
||||
describe('AgentGenerationPipeline compaction', () => {
|
||||
it('copies compacted agents back with compute instead of a full agent buffer copy', async () => {
|
||||
it('swaps compacted agents into the active buffer without a copy-back dispatch', async () => {
|
||||
const agentCount = 10;
|
||||
const { device, pipeline } = createPipeline(3);
|
||||
|
||||
await expect(pipeline.compactAgents(agentCount)).resolves.toBe(3);
|
||||
|
||||
expect(device.createdComputeEntryPoints).toContain('copyCompactedAgents');
|
||||
expect(device.dispatchCalls.map((call) => call.entryPoint)).toEqual([
|
||||
'main',
|
||||
'copyCompactedAgents',
|
||||
]);
|
||||
expect(device.createdComputeEntryPoints).not.toContain('copyCompactedAgents');
|
||||
expect(device.dispatchCalls.map((call) => call.entryPoint)).toEqual(['main']);
|
||||
expect(device.copyCalls.map((call) => call.size)).toEqual([
|
||||
Uint32Array.BYTES_PER_ELEMENT,
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -14,14 +14,16 @@ export class AgentGenerationPipeline {
|
|||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly uniforms: GPUBuffer;
|
||||
private readonly bindGroup: GPUBindGroup;
|
||||
private readonly bindGroupsByActiveBuffer = new WeakMap<
|
||||
GPUBuffer,
|
||||
WeakMap<GPUBuffer, GPUBindGroup>
|
||||
>();
|
||||
|
||||
private readonly resizePipeline: GPUComputePipeline;
|
||||
private readonly compactionPipeline: GPUComputePipeline;
|
||||
private readonly compactedAgentsCopyPipeline: GPUComputePipeline;
|
||||
|
||||
public readonly agentsBuffer: GPUBuffer;
|
||||
private readonly compactedAgentsBuffer: GPUBuffer;
|
||||
private activeAgentsBuffer: GPUBuffer;
|
||||
private inactiveAgentsBuffer: GPUBuffer;
|
||||
private readonly countersBuffer: GPUBuffer;
|
||||
private readonly countersStagingBuffer: GPUBuffer;
|
||||
private readonly counterClearValues = new Uint32Array(
|
||||
|
|
@ -69,15 +71,8 @@ export class AgentGenerationPipeline {
|
|||
],
|
||||
});
|
||||
|
||||
this.agentsBuffer = this.device.createBuffer({
|
||||
size: this.maxAgentCount * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
|
||||
this.compactedAgentsBuffer = this.device.createBuffer({
|
||||
size: this.maxAgentCount * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE,
|
||||
});
|
||||
this.activeAgentsBuffer = this.createAgentsBuffer();
|
||||
this.inactiveAgentsBuffer = this.createAgentsBuffer();
|
||||
|
||||
this.countersBuffer = this.device.createBuffer({
|
||||
size: AgentGenerationPipeline.COUNTER_COUNT * Uint32Array.BYTES_PER_ELEMENT,
|
||||
|
|
@ -94,36 +89,6 @@ export class AgentGenerationPipeline {
|
|||
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
|
||||
this.bindGroup = this.device.createBindGroup({
|
||||
layout: this.bindGroupLayout,
|
||||
entries: [
|
||||
{
|
||||
binding: 0,
|
||||
resource: {
|
||||
buffer: this.uniforms,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 1,
|
||||
resource: {
|
||||
buffer: this.agentsBuffer,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 2,
|
||||
resource: {
|
||||
buffer: this.countersBuffer,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 3,
|
||||
resource: {
|
||||
buffer: this.compactedAgentsBuffer,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
this.resizePipeline = device.createComputePipeline({
|
||||
layout: device.createPipelineLayout({
|
||||
bindGroupLayouts: [emptyBindGroupLayout, this.bindGroupLayout],
|
||||
|
|
@ -145,15 +110,16 @@ export class AgentGenerationPipeline {
|
|||
entryPoint: 'main',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
this.compactedAgentsCopyPipeline = device.createComputePipeline({
|
||||
layout: device.createPipelineLayout({
|
||||
bindGroupLayouts: [emptyBindGroupLayout, this.bindGroupLayout],
|
||||
}),
|
||||
compute: {
|
||||
module: compactionModule,
|
||||
entryPoint: 'copyCompactedAgents',
|
||||
},
|
||||
public get agentsBuffer(): GPUBuffer {
|
||||
return this.activeAgentsBuffer;
|
||||
}
|
||||
|
||||
private createAgentsBuffer(): GPUBuffer {
|
||||
return this.device.createBuffer({
|
||||
size: this.maxAgentCount * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -163,6 +129,10 @@ export class AgentGenerationPipeline {
|
|||
? this.maxAgentCountUpperLimit
|
||||
: Number.POSITIVE_INFINITY,
|
||||
Math.floor(this.device.limits.maxBufferSize / AGENT_SIZE_IN_BYTES) - 1,
|
||||
Math.floor(
|
||||
((this.device.limits as GPUSupportedLimits).maxStorageBufferBindingSize ??
|
||||
this.device.limits.maxBufferSize) / AGENT_SIZE_IN_BYTES
|
||||
) - 1,
|
||||
this.device.limits.maxComputeWorkgroupsPerDimension *
|
||||
AgentGenerationPipeline.WORKGROUP_SIZE
|
||||
);
|
||||
|
|
@ -170,7 +140,7 @@ export class AgentGenerationPipeline {
|
|||
|
||||
public writeAgents(agentOffset: number, data: Float32Array): void {
|
||||
this.device.queue.writeBuffer(
|
||||
this.agentsBuffer,
|
||||
this.activeAgentsBuffer,
|
||||
agentOffset * AGENT_SIZE_IN_BYTES,
|
||||
data
|
||||
);
|
||||
|
|
@ -190,7 +160,7 @@ export class AgentGenerationPipeline {
|
|||
const commandEncoder = this.device.createCommandEncoder();
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
passEncoder.setPipeline(this.resizePipeline);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.setBindGroup(1, this.getBindGroup());
|
||||
passEncoder.dispatchWorkgroups(
|
||||
getWorkgroupCount(agentCount, AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
|
|
@ -204,8 +174,6 @@ export class AgentGenerationPipeline {
|
|||
return 0;
|
||||
}
|
||||
|
||||
this.counterClearValues.fill(0);
|
||||
this.agentCountUniformValues.fill(0);
|
||||
this.agentCountUniformValues[0] = agentCount;
|
||||
this.device.queue.writeBuffer(this.countersBuffer, 0, this.counterClearValues);
|
||||
this.device.queue.writeBuffer(this.uniforms, 0, this.agentCountUniformValues);
|
||||
|
|
@ -213,20 +181,12 @@ export class AgentGenerationPipeline {
|
|||
const commandEncoder = this.device.createCommandEncoder();
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
passEncoder.setPipeline(this.compactionPipeline);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.setBindGroup(1, this.getBindGroup());
|
||||
passEncoder.dispatchWorkgroups(
|
||||
getWorkgroupCount(agentCount, AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
passEncoder.end();
|
||||
|
||||
const copyPassEncoder = commandEncoder.beginComputePass();
|
||||
copyPassEncoder.setPipeline(this.compactedAgentsCopyPipeline);
|
||||
copyPassEncoder.setBindGroup(1, this.bindGroup);
|
||||
copyPassEncoder.dispatchWorkgroups(
|
||||
getWorkgroupCount(agentCount, AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
copyPassEncoder.end();
|
||||
|
||||
commandEncoder.copyBufferToBuffer(
|
||||
this.countersBuffer,
|
||||
0,
|
||||
|
|
@ -239,18 +199,74 @@ export class AgentGenerationPipeline {
|
|||
|
||||
await this.countersStagingBuffer.mapAsync(GPUMapMode.READ);
|
||||
const compactedCount = new Uint32Array(
|
||||
this.countersStagingBuffer.getMappedRange().slice(0, Uint32Array.BYTES_PER_ELEMENT)
|
||||
this.countersStagingBuffer.getMappedRange(),
|
||||
0,
|
||||
1
|
||||
)[0];
|
||||
this.countersStagingBuffer.unmap();
|
||||
this.swapAgentBuffers();
|
||||
|
||||
return compactedCount;
|
||||
}
|
||||
|
||||
private getBindGroup(): GPUBindGroup {
|
||||
let inactiveCache = this.bindGroupsByActiveBuffer.get(this.activeAgentsBuffer);
|
||||
if (!inactiveCache) {
|
||||
inactiveCache = new WeakMap<GPUBuffer, GPUBindGroup>();
|
||||
this.bindGroupsByActiveBuffer.set(this.activeAgentsBuffer, inactiveCache);
|
||||
}
|
||||
|
||||
const cached = inactiveCache.get(this.inactiveAgentsBuffer);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const bindGroup = this.device.createBindGroup({
|
||||
layout: this.bindGroupLayout,
|
||||
entries: [
|
||||
{
|
||||
binding: 0,
|
||||
resource: {
|
||||
buffer: this.uniforms,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 1,
|
||||
resource: {
|
||||
buffer: this.activeAgentsBuffer,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 2,
|
||||
resource: {
|
||||
buffer: this.countersBuffer,
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 3,
|
||||
resource: {
|
||||
buffer: this.inactiveAgentsBuffer,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
inactiveCache.set(this.inactiveAgentsBuffer, bindGroup);
|
||||
return bindGroup;
|
||||
}
|
||||
|
||||
private swapAgentBuffers(): void {
|
||||
[this.activeAgentsBuffer, this.inactiveAgentsBuffer] = [
|
||||
this.inactiveAgentsBuffer,
|
||||
this.activeAgentsBuffer,
|
||||
];
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this.uniforms.destroy();
|
||||
this.countersBuffer.destroy();
|
||||
this.countersStagingBuffer.destroy();
|
||||
this.compactedAgentsBuffer.destroy();
|
||||
this.agentsBuffer.destroy();
|
||||
this.inactiveAgentsBuffer.destroy();
|
||||
this.activeAgentsBuffer.destroy();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,6 @@ fn main(
|
|||
return;
|
||||
}
|
||||
|
||||
var agent = agents[id];
|
||||
agent.position *= resizeSettings.scale;
|
||||
|
||||
if agent.targetPosition.x >= 0.0 && agent.targetPosition.y >= 0.0 {
|
||||
agent.targetPosition *= resizeSettings.scale;
|
||||
}
|
||||
|
||||
agents[id] = agent;
|
||||
agents[id].position = agents[id].position * resizeSettings.scale;
|
||||
agents[id].targetPosition = agents[id].targetPosition * resizeSettings.scale;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,13 +67,12 @@ describe('Agent TS/WGSL contract', () => {
|
|||
expect(compactionShader).toContain('if id < settings.agentCount');
|
||||
});
|
||||
|
||||
it('keeps compaction copy-back bounded by the compacted count', () => {
|
||||
expect(compactionShader).toContain('fn copyCompactedAgents');
|
||||
it('keeps compaction as a ping-pong write without copy-back shader work', () => {
|
||||
expect(compactionShader).not.toContain('fn copyCompactedAgents');
|
||||
expect(compactionShader).not.toContain('agents[id] = compactedAgents[id];');
|
||||
expect(compactionShader).toContain(
|
||||
'workgroupCopyCount = atomicLoad(&counters.aliveAgentCount);'
|
||||
'compactedAgents[workgroupCompactedOffset + localCompactedIndex] = agent;'
|
||||
);
|
||||
expect(compactionShader).toContain('if id >= workgroupCopyCount');
|
||||
expect(compactionShader).toContain('agents[id] = compactedAgents[id];');
|
||||
});
|
||||
|
||||
it('uses workgroup-local counting before allocating global compacted ranges', () => {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import shader from './agent.wgsl?raw';
|
|||
|
||||
export class AgentPipeline {
|
||||
private static readonly WORKGROUP_SIZE = 64;
|
||||
private static readonly UNIFORM_COUNT = 17;
|
||||
private static readonly UNIFORM_COUNT = 33;
|
||||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly pipeline: GPUComputePipeline;
|
||||
|
|
@ -20,9 +20,12 @@ export class AgentPipeline {
|
|||
private readonly uniformCache = createCachedFloat32BufferWrite(
|
||||
AgentPipeline.UNIFORM_COUNT
|
||||
);
|
||||
private readonly bindGroupsByTexture = new WeakMap<
|
||||
GPUTextureView,
|
||||
WeakMap<GPUTextureView, WeakMap<GPUTextureView, GPUBindGroup>>
|
||||
private readonly bindGroupsByAgentsBuffer = new WeakMap<
|
||||
GPUBuffer,
|
||||
WeakMap<
|
||||
GPUTextureView,
|
||||
WeakMap<GPUTextureView, WeakMap<GPUTextureView, GPUBindGroup>>
|
||||
>
|
||||
>();
|
||||
|
||||
private agentCount = 0;
|
||||
|
|
@ -30,7 +33,7 @@ export class AgentPipeline {
|
|||
public constructor(
|
||||
private readonly device: GPUDevice,
|
||||
private readonly commonState: CommonState,
|
||||
private readonly agentsBuffer: GPUBuffer // doesn't get destroyed
|
||||
private readonly getAgentsBuffer: () => GPUBuffer // doesn't get destroyed
|
||||
) {
|
||||
this.bindGroupLayout = device.createBindGroupLayout(AgentPipeline.bindGroupLayout);
|
||||
|
||||
|
|
@ -67,6 +70,21 @@ export class AgentPipeline {
|
|||
color3ToColor1,
|
||||
color3ToColor2,
|
||||
color3ToColor3,
|
||||
sourceAttractionWeight,
|
||||
sourceSlowMoveRate,
|
||||
sourceTrailWeightMultiplier,
|
||||
forwardRotationScale,
|
||||
introNearDistanceInner,
|
||||
introNearDistanceMin,
|
||||
introNearSensorOffsetMultiplier,
|
||||
introTargetAngleBlend,
|
||||
introProgressCutoff,
|
||||
introTurnRateMultiplier,
|
||||
introRandomTurnMultiplier,
|
||||
introFarMoveMultiplier,
|
||||
introNearMoveMultiplier,
|
||||
introStepStopDistance,
|
||||
randomTimeScale,
|
||||
agentCount,
|
||||
introProgress,
|
||||
}: AgentSettings & {
|
||||
|
|
@ -77,21 +95,38 @@ export class AgentPipeline {
|
|||
this.agentCount = agentCount;
|
||||
this.uniformValues[0] = moveSpeed * deltaTime;
|
||||
this.uniformValues[1] = turnSpeed * deltaTime;
|
||||
this.uniformValues[2] = (sensorOffsetAngle * Math.PI) / 180;
|
||||
this.uniformValues[3] = sensorOffsetDistance;
|
||||
this.uniformValues[4] = turnWhenLost;
|
||||
this.uniformValues[5] = individualTrailWeight;
|
||||
this.uniformValues[6] = agentCount;
|
||||
this.uniformValues[7] = introProgress ?? 1;
|
||||
this.uniformValues[8] = color1ToColor1;
|
||||
this.uniformValues[9] = color1ToColor2;
|
||||
this.uniformValues[10] = color1ToColor3;
|
||||
this.uniformValues[11] = color2ToColor1;
|
||||
this.uniformValues[12] = color2ToColor2;
|
||||
this.uniformValues[13] = color2ToColor3;
|
||||
this.uniformValues[14] = color3ToColor1;
|
||||
this.uniformValues[15] = color3ToColor2;
|
||||
this.uniformValues[16] = color3ToColor3;
|
||||
const sensorAngle = (sensorOffsetAngle * Math.PI) / 180;
|
||||
this.uniformValues[2] = Math.sin(sensorAngle);
|
||||
this.uniformValues[3] = Math.cos(sensorAngle);
|
||||
this.uniformValues[4] = sensorOffsetDistance;
|
||||
this.uniformValues[5] = turnWhenLost;
|
||||
this.uniformValues[6] = individualTrailWeight;
|
||||
this.uniformValues[7] = agentCount;
|
||||
this.uniformValues[8] = introProgress ?? 1;
|
||||
this.uniformValues[9] = color1ToColor1;
|
||||
this.uniformValues[10] = color1ToColor2;
|
||||
this.uniformValues[11] = color1ToColor3;
|
||||
this.uniformValues[12] = color2ToColor1;
|
||||
this.uniformValues[13] = color2ToColor2;
|
||||
this.uniformValues[14] = color2ToColor3;
|
||||
this.uniformValues[15] = color3ToColor1;
|
||||
this.uniformValues[16] = color3ToColor2;
|
||||
this.uniformValues[17] = color3ToColor3;
|
||||
this.uniformValues[18] = sourceAttractionWeight;
|
||||
this.uniformValues[19] = sourceSlowMoveRate;
|
||||
this.uniformValues[20] = sourceTrailWeightMultiplier;
|
||||
this.uniformValues[21] = forwardRotationScale;
|
||||
this.uniformValues[22] = introNearDistanceInner;
|
||||
this.uniformValues[23] = introNearDistanceMin;
|
||||
this.uniformValues[24] = introNearSensorOffsetMultiplier;
|
||||
this.uniformValues[25] = introTargetAngleBlend;
|
||||
this.uniformValues[26] = introProgressCutoff;
|
||||
this.uniformValues[27] = introTurnRateMultiplier;
|
||||
this.uniformValues[28] = introRandomTurnMultiplier;
|
||||
this.uniformValues[29] = introFarMoveMultiplier;
|
||||
this.uniformValues[30] = introNearMoveMultiplier;
|
||||
this.uniformValues[31] = introStepStopDistance;
|
||||
this.uniformValues[32] = randomTimeScale;
|
||||
writeFloat32BufferIfChanged(
|
||||
this.device,
|
||||
this.uniforms,
|
||||
|
|
@ -127,10 +162,20 @@ export class AgentPipeline {
|
|||
trailMapOut: GPUTextureView,
|
||||
sourceMap: GPUTextureView
|
||||
): GPUBindGroup {
|
||||
let outputCache = this.bindGroupsByTexture.get(trailMapIn);
|
||||
const agentsBuffer = this.getAgentsBuffer();
|
||||
let textureCache = this.bindGroupsByAgentsBuffer.get(agentsBuffer);
|
||||
if (!textureCache) {
|
||||
textureCache = new WeakMap<
|
||||
GPUTextureView,
|
||||
WeakMap<GPUTextureView, WeakMap<GPUTextureView, GPUBindGroup>>
|
||||
>();
|
||||
this.bindGroupsByAgentsBuffer.set(agentsBuffer, textureCache);
|
||||
}
|
||||
|
||||
let outputCache = textureCache.get(trailMapIn);
|
||||
if (!outputCache) {
|
||||
outputCache = new WeakMap<GPUTextureView, WeakMap<GPUTextureView, GPUBindGroup>>();
|
||||
this.bindGroupsByTexture.set(trailMapIn, outputCache);
|
||||
textureCache.set(trailMapIn, outputCache);
|
||||
}
|
||||
|
||||
let sourceCache = outputCache.get(trailMapOut);
|
||||
|
|
@ -156,7 +201,7 @@ export class AgentPipeline {
|
|||
{
|
||||
binding: 1,
|
||||
resource: {
|
||||
buffer: this.agentsBuffer,
|
||||
buffer: agentsBuffer,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,4 +14,19 @@ export interface AgentSettings {
|
|||
sensorOffsetDistance: number;
|
||||
turnWhenLost: number;
|
||||
individualTrailWeight: number;
|
||||
sourceAttractionWeight: number;
|
||||
sourceSlowMoveRate: number;
|
||||
sourceTrailWeightMultiplier: number;
|
||||
forwardRotationScale: number;
|
||||
introNearDistanceMin: number;
|
||||
introNearSensorOffsetMultiplier: number;
|
||||
introTargetAngleBlend: number;
|
||||
introProgressCutoff: number;
|
||||
introNearDistanceInner: number;
|
||||
introTurnRateMultiplier: number;
|
||||
introRandomTurnMultiplier: number;
|
||||
introFarMoveMultiplier: number;
|
||||
introNearMoveMultiplier: number;
|
||||
introStepStopDistance: number;
|
||||
randomTimeScale: number;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
struct Settings {
|
||||
moveRate: f32,
|
||||
turnRate: f32,
|
||||
sensorAngle: f32,
|
||||
sensorAngleSin: f32,
|
||||
sensorAngleCos: f32,
|
||||
sensorOffset: f32,
|
||||
turnWhenLost: f32,
|
||||
individualTrailWeight: f32,
|
||||
|
|
@ -16,6 +17,21 @@ struct Settings {
|
|||
color3ToColor1: f32,
|
||||
color3ToColor2: f32,
|
||||
color3ToColor3: f32,
|
||||
sourceAttractionWeight: f32,
|
||||
sourceSlowMoveRate: f32,
|
||||
sourceTrailWeightMultiplier: f32,
|
||||
forwardRotationScale: f32,
|
||||
introNearDistanceInner: f32,
|
||||
introNearDistanceMin: f32,
|
||||
introNearSensorOffsetMultiplier: f32,
|
||||
introTargetAngleBlend: f32,
|
||||
introProgressCutoff: f32,
|
||||
introTurnRateMultiplier: f32,
|
||||
introRandomTurnMultiplier: f32,
|
||||
introFarMoveMultiplier: f32,
|
||||
introNearMoveMultiplier: f32,
|
||||
introStepStopDistance: f32,
|
||||
randomTimeScale: f32,
|
||||
};
|
||||
|
||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||
|
|
@ -33,25 +49,38 @@ fn main(
|
|||
return;
|
||||
}
|
||||
|
||||
var agent = agents[id];
|
||||
if agent.colorIndex < 0.0 || agent.colorIndex >= 2.5 {
|
||||
let colorIndex = agents[id].colorIndex;
|
||||
if colorIndex < 0.0 || colorIndex >= 2.5 {
|
||||
return;
|
||||
}
|
||||
|
||||
let hasIntroTarget =
|
||||
settings.introProgress < 0.999 &&
|
||||
agent.targetPosition.x >= 0.0 &&
|
||||
agent.targetPosition.y >= 0.0;
|
||||
|
||||
if hasIntroTarget && settings.introProgress < agent.introDelay {
|
||||
return;
|
||||
var position = agents[id].position;
|
||||
var angle = agents[id].angle;
|
||||
var targetPosition = vec2<f32>(-1.0, -1.0);
|
||||
var hasIntroTarget = false;
|
||||
if settings.introProgress < settings.introProgressCutoff {
|
||||
targetPosition = agents[id].targetPosition;
|
||||
hasIntroTarget = targetPosition.x >= 0.0 && targetPosition.y >= 0.0;
|
||||
if hasIntroTarget && settings.introProgress < agents[id].introDelay {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let random = random_vec4(id, state.time);
|
||||
let randomSeed = random_seed(id, state.time);
|
||||
let randomTurn = random_float(randomSeed);
|
||||
let direction = vec2(cos(angle), sin(angle));
|
||||
|
||||
let forwardSensor = sensor_position(agent.position, agent.angle, settings.sensorOffset, 0);
|
||||
let leftSensor = sensor_position(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
||||
let rightSensor = sensor_position(agent.position, agent.angle, settings.sensorOffset, -settings.sensorAngle);
|
||||
let forwardSensor = sensor_position(position, direction, settings.sensorOffset);
|
||||
let leftSensor = sensor_position(
|
||||
position,
|
||||
rotate_direction(direction, settings.sensorAngleSin, settings.sensorAngleCos),
|
||||
settings.sensorOffset
|
||||
);
|
||||
let rightSensor = sensor_position(
|
||||
position,
|
||||
rotate_direction(direction, -settings.sensorAngleSin, settings.sensorAngleCos),
|
||||
settings.sensorOffset
|
||||
);
|
||||
|
||||
let trailForward = textureLoad(trailMapIn, forwardSensor, 0);
|
||||
let trailLeft = textureLoad(trailMapIn, leftSensor, 0);
|
||||
|
|
@ -60,8 +89,8 @@ fn main(
|
|||
let sourceLeftSample = textureLoad(sourceMap, leftSensor, 0);
|
||||
let sourceRightSample = textureLoad(sourceMap, rightSensor, 0);
|
||||
|
||||
let channelMask = get_channel_mask(agent.colorIndex);
|
||||
let reactionMask = get_reaction_mask(agent.colorIndex);
|
||||
let channelMask = get_channel_mask(colorIndex);
|
||||
let reactionMask = get_reaction_mask(colorIndex);
|
||||
|
||||
let trailForwardWeight = dot(trailForward.rgb, reactionMask);
|
||||
let trailLeftWeight = dot(trailLeft.rgb, reactionMask);
|
||||
|
|
@ -71,76 +100,105 @@ fn main(
|
|||
let sourceLeftWeight = dot(sourceLeftSample.rgb, reactionMask);
|
||||
let sourceRightWeight = dot(sourceRightSample.rgb, reactionMask);
|
||||
|
||||
let weightForward = trailForwardWeight + sourceForwardWeight * 24.0;
|
||||
let weightLeft = trailLeftWeight + sourceLeftWeight * 24.0;
|
||||
let weightRight = trailRightWeight + sourceRightWeight * 24.0;
|
||||
let weightForward =
|
||||
trailForwardWeight + sourceForwardWeight * settings.sourceAttractionWeight;
|
||||
let weightLeft = trailLeftWeight + sourceLeftWeight * settings.sourceAttractionWeight;
|
||||
let weightRight =
|
||||
trailRightWeight + sourceRightWeight * settings.sourceAttractionWeight;
|
||||
|
||||
var rotation = (random.r - 0.5) * settings.turnWhenLost;
|
||||
var rotation = (randomTurn - 0.5) * settings.turnWhenLost;
|
||||
if weightForward >= weightLeft && weightForward >= weightRight {
|
||||
rotation = rotation * 0.25;
|
||||
rotation = rotation * settings.forwardRotationScale;
|
||||
} else {
|
||||
rotation += sign(weightLeft - weightRight) * settings.turnRate;
|
||||
}
|
||||
|
||||
let sourceAtAgent = textureLoad(sourceMap, vec2<i32>(agent.position), 0);
|
||||
let sourceAtAgent = textureLoad(sourceMap, vec2<i32>(position), 0);
|
||||
let positiveReactionMask = max(reactionMask, vec3<f32>(0.0));
|
||||
let sourceAtAgentStrength = clamp(dot(sourceAtAgent.rgb, positiveReactionMask), 0.0, 1.0);
|
||||
var moveRate = settings.moveRate * mix(1.0, 0.08, sourceAtAgentStrength);
|
||||
var moveRate = settings.moveRate * mix(1.0, settings.sourceSlowMoveRate, sourceAtAgentStrength);
|
||||
var introTargetOffset = vec2<f32>(0.0, 0.0);
|
||||
var introTargetDistance = 0.0;
|
||||
|
||||
if hasIntroTarget {
|
||||
introTargetOffset = agent.targetPosition - agent.position;
|
||||
introTargetOffset = targetPosition - position;
|
||||
introTargetDistance = length(introTargetOffset);
|
||||
let targetAngle = atan2(introTargetOffset.y, introTargetOffset.x);
|
||||
let nearTitle = 1.0 - smoothstep(4.0, max(28.0, settings.sensorOffset * 0.75), introTargetDistance);
|
||||
let desiredAngle = mix(targetAngle, agent.targetAngle, nearTitle * 0.2);
|
||||
let introTurn = angle_delta(agent.angle, desiredAngle);
|
||||
let nearTitle = 1.0 - smoothstep(
|
||||
settings.introNearDistanceInner,
|
||||
max(
|
||||
settings.introNearDistanceMin,
|
||||
settings.sensorOffset * settings.introNearSensorOffsetMultiplier
|
||||
),
|
||||
introTargetDistance
|
||||
);
|
||||
let desiredAngle = mix(
|
||||
targetAngle,
|
||||
agents[id].targetAngle,
|
||||
nearTitle * settings.introTargetAngleBlend
|
||||
);
|
||||
let introTurn = angle_delta(angle, desiredAngle);
|
||||
|
||||
rotation = clamp(introTurn, -settings.turnRate * 3.4, settings.turnRate * 3.4)
|
||||
+ (random.g - 0.5) * settings.turnWhenLost * 0.18;
|
||||
moveRate = min(settings.moveRate * mix(2.65, 0.01, nearTitle), introTargetDistance);
|
||||
rotation = clamp(
|
||||
introTurn,
|
||||
-settings.turnRate * settings.introTurnRateMultiplier,
|
||||
settings.turnRate * settings.introTurnRateMultiplier
|
||||
)
|
||||
+ (random_float(randomSeed + 1013904223u) - 0.5) *
|
||||
settings.turnWhenLost *
|
||||
settings.introRandomTurnMultiplier;
|
||||
moveRate = min(
|
||||
settings.moveRate *
|
||||
mix(settings.introFarMoveMultiplier, settings.introNearMoveMultiplier, nearTitle),
|
||||
introTargetDistance
|
||||
);
|
||||
}
|
||||
|
||||
var step = vec2(cos(agent.angle), sin(agent.angle)) * moveRate;
|
||||
var step = direction * moveRate;
|
||||
if hasIntroTarget {
|
||||
step = vec2<f32>(0.0, 0.0);
|
||||
if introTargetDistance > 0.5 {
|
||||
if introTargetDistance > settings.introStepStopDistance {
|
||||
step = introTargetOffset / introTargetDistance * moveRate;
|
||||
}
|
||||
}
|
||||
|
||||
let maxPosition = state.size - vec2<f32>(1.0, 1.0);
|
||||
let nextPosition = clamp(agent.position + step, vec2<f32>(0, 0), maxPosition);
|
||||
let nextPosition = clamp(position + step, vec2<f32>(0, 0), maxPosition);
|
||||
if nextPosition.x == 0 || nextPosition.x == maxPosition.x || nextPosition.y == 0 || nextPosition.y == maxPosition.y {
|
||||
rotation = 3.14159265359 + random.a - 0.5;
|
||||
rotation = 3.14159265359 + random_float(randomSeed + 22695477u) - 0.5;
|
||||
}
|
||||
|
||||
let sourceBelow = textureLoad(sourceMap, vec2<i32>(nextPosition), 0);
|
||||
let sourceBelowStrength = clamp(dot(sourceBelow.rgb, positiveReactionMask), 0.0, 1.0);
|
||||
let trailWeight = settings.individualTrailWeight * (1.0 + sourceBelowStrength * 16.0);
|
||||
let trailWeight =
|
||||
settings.individualTrailWeight *
|
||||
(1.0 + sourceBelowStrength * settings.sourceTrailWeightMultiplier);
|
||||
var trailBelow = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
|
||||
trailBelow = vec4<f32>(
|
||||
trailBelow.rgb + channelMask * trailWeight,
|
||||
max(trailBelow.a, 0.0)
|
||||
);
|
||||
|
||||
agent.angle += rotation;
|
||||
agent.position = nextPosition;
|
||||
|
||||
textureStore(trailMapOut, vec2<i32>(nextPosition), trailBelow);
|
||||
agents[id] = agent;
|
||||
agents[id].angle = angle + rotation;
|
||||
agents[id].position = nextPosition;
|
||||
}
|
||||
|
||||
fn sensor_position(agentPosition: vec2<f32>, agentAngle: f32, sensorOffset: f32, sensorOffsetAngle: f32) -> vec2<i32> {
|
||||
let sensorAngle = agentAngle + sensorOffsetAngle;
|
||||
fn sensor_position(agentPosition: vec2<f32>, direction: vec2<f32>, sensorOffset: f32) -> vec2<i32> {
|
||||
return vec2<i32>(clamp(
|
||||
agentPosition + vec2(cos(sensorAngle), sin(sensorAngle)) * sensorOffset,
|
||||
agentPosition + direction * sensorOffset,
|
||||
vec2<f32>(0, 0),
|
||||
state.size - vec2<f32>(1, 1)
|
||||
));
|
||||
}
|
||||
|
||||
fn rotate_direction(direction: vec2<f32>, angleSin: f32, angleCos: f32) -> vec2<f32> {
|
||||
return vec2<f32>(
|
||||
direction.x * angleCos - direction.y * angleSin,
|
||||
direction.x * angleSin + direction.y * angleCos
|
||||
);
|
||||
}
|
||||
|
||||
fn get_channel_mask(colorIndex: f32) -> vec3<f32> {
|
||||
if colorIndex < 0.5 {
|
||||
return vec3<f32>(1, 0, 0);
|
||||
|
|
@ -183,15 +241,9 @@ fn angle_delta(sourceAngle: f32, targetAngle: f32) -> f32 {
|
|||
return atan2(sin(targetAngle - sourceAngle), cos(targetAngle - sourceAngle));
|
||||
}
|
||||
|
||||
fn random_vec4(id: u32, time: f32) -> vec4<f32> {
|
||||
let timeSeed = u32(time * 0.34816);
|
||||
let seed = id * 747796405u + timeSeed * 2891336453u;
|
||||
return vec4<f32>(
|
||||
random_float(seed),
|
||||
random_float(seed + 1013904223u),
|
||||
random_float(seed + 1664525u),
|
||||
random_float(seed + 22695477u)
|
||||
);
|
||||
fn random_seed(id: u32, time: f32) -> u32 {
|
||||
let timeSeed = u32(time * settings.randomTimeScale);
|
||||
return id * 747796405u + timeSeed * 2891336453u;
|
||||
}
|
||||
|
||||
fn random_float(seed: u32) -> f32 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue