Generate agents
This commit is contained in:
parent
5ca7514891
commit
7e8ca4b16f
10 changed files with 368 additions and 89 deletions
|
|
@ -4,21 +4,42 @@ import { CommonState } from '../../common-state/common-state';
|
|||
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
||||
import shader from './agent-generation.wgsl';
|
||||
import agentSchema from './agent-schema.wgsl';
|
||||
import { GenerationCounts } from './generation-counts';
|
||||
|
||||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export class AgentGenerationPipeline {
|
||||
private static readonly WORKGROUP_SIZE = 64;
|
||||
private static readonly UNIFORM_COUNT = 6;
|
||||
private static readonly COUNTER_COUNT = 3;
|
||||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly pipeline: GPUComputePipeline;
|
||||
private readonly uniforms: GPUBuffer;
|
||||
private readonly bindGroup: GPUBindGroup;
|
||||
|
||||
private bindGroup?: GPUBindGroup;
|
||||
public readonly agentsBuffer: GPUBuffer;
|
||||
public readonly countersBuffer: GPUBuffer;
|
||||
public readonly countersStagingBuffer: GPUBuffer;
|
||||
|
||||
public constructor(
|
||||
private readonly device: GPUDevice,
|
||||
private readonly commonState: CommonState
|
||||
private readonly commonState: CommonState,
|
||||
private readonly agentCount: number
|
||||
) {
|
||||
if (agentCount <= 0 || agentCount != Math.floor(agentCount)) {
|
||||
throw new Error('Agent count must be a positive integer');
|
||||
}
|
||||
|
||||
this.bindGroupLayout = device.createBindGroupLayout({
|
||||
entries: [
|
||||
{
|
||||
binding: 0,
|
||||
visibility: GPUShaderStage.COMPUTE,
|
||||
buffer: {
|
||||
type: 'uniform',
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 1,
|
||||
visibility: GPUShaderStage.COMPUTE,
|
||||
|
|
@ -26,6 +47,57 @@ export class AgentGenerationPipeline {
|
|||
type: 'storage',
|
||||
},
|
||||
},
|
||||
{
|
||||
binding: 2,
|
||||
visibility: GPUShaderStage.COMPUTE,
|
||||
buffer: {
|
||||
type: 'storage',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
this.agentsBuffer = this.device.createBuffer({
|
||||
size: agentCount * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE,
|
||||
});
|
||||
|
||||
this.countersBuffer = this.device.createBuffer({
|
||||
size: AgentGenerationPipeline.COUNTER_COUNT * Int32Array.BYTES_PER_ELEMENT,
|
||||
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
|
||||
});
|
||||
|
||||
this.countersStagingBuffer = this.device.createBuffer({
|
||||
size: AgentGenerationPipeline.COUNTER_COUNT * Int32Array.BYTES_PER_ELEMENT,
|
||||
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
|
||||
this.uniforms = this.device.createBuffer({
|
||||
size: AgentGenerationPipeline.UNIFORM_COUNT * Float32Array.BYTES_PER_ELEMENT,
|
||||
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,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
|
|
@ -40,28 +112,39 @@ export class AgentGenerationPipeline {
|
|||
});
|
||||
}
|
||||
|
||||
public generateAgents(agentCount: number): GPUBuffer {
|
||||
if (agentCount <= 0 || agentCount != Math.floor(agentCount)) {
|
||||
throw new Error('Agent count must be a positive integer');
|
||||
}
|
||||
public async spawnNextGenerationCover(
|
||||
generationId: number,
|
||||
count: number
|
||||
): Promise<GenerationCounts> {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
new Float32Array([0, 0, 0, generationId, 0])
|
||||
);
|
||||
|
||||
const agentsBuffer = this.device.createBuffer({
|
||||
size: agentCount * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE,
|
||||
});
|
||||
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
||||
|
||||
this.bindGroup = this.device.createBindGroup({
|
||||
layout: this.bindGroupLayout,
|
||||
entries: [
|
||||
{
|
||||
binding: 1,
|
||||
resource: {
|
||||
buffer: agentsBuffer,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
return this.execute();
|
||||
}
|
||||
|
||||
public async spawnNextGenerationCircle(
|
||||
generationId: number,
|
||||
count: number,
|
||||
center: vec2,
|
||||
radius: number
|
||||
): Promise<GenerationCounts> {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
new Float32Array([...center, radius, generationId, 1])
|
||||
);
|
||||
|
||||
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
||||
|
||||
return this.execute();
|
||||
}
|
||||
|
||||
private async execute(): Promise<GenerationCounts> {
|
||||
const commandEncoder = this.device.createCommandEncoder();
|
||||
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
|
|
@ -69,11 +152,34 @@ export class AgentGenerationPipeline {
|
|||
this.commonState.execute(passEncoder);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.dispatchWorkgroups(
|
||||
Math.ceil(agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
passEncoder.end();
|
||||
|
||||
commandEncoder.copyBufferToBuffer(
|
||||
this.countersBuffer,
|
||||
0,
|
||||
this.countersStagingBuffer,
|
||||
0,
|
||||
AgentGenerationPipeline.COUNTER_COUNT * Int32Array.BYTES_PER_ELEMENT
|
||||
);
|
||||
|
||||
this.device.queue.submit([commandEncoder.finish()]);
|
||||
return agentsBuffer;
|
||||
|
||||
await this.countersStagingBuffer.mapAsync(GPUMapMode.READ);
|
||||
|
||||
const data = new Int32Array(this.countersStagingBuffer.getMappedRange().slice(0));
|
||||
this.countersStagingBuffer.unmap();
|
||||
return {
|
||||
currentGenerationCount: data[0],
|
||||
nextGenerationCount: data[1],
|
||||
};
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this.uniforms.destroy();
|
||||
this.countersBuffer.destroy();
|
||||
this.countersStagingBuffer.destroy();
|
||||
this.agentsBuffer.destroy();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,65 @@
|
|||
struct Settings {
|
||||
center: vec2<f32>,
|
||||
radius: f32,
|
||||
nextGenerationId: f32,
|
||||
shape: f32,
|
||||
};
|
||||
|
||||
struct Counters {
|
||||
currentGenerationAlive: atomic<i32>,
|
||||
nextGenerationAlive: atomic<i32>,
|
||||
remaining: atomic<i32>
|
||||
};
|
||||
|
||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||
@group(1) @binding(2) var<storage, read_write> counters: Counters;
|
||||
|
||||
@compute @workgroup_size(64)
|
||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let id = global_id.x;
|
||||
|
||||
if (id >= arrayLength(&agents)) {
|
||||
if id >= arrayLength(&agents) {
|
||||
return;
|
||||
}
|
||||
|
||||
let position = vec2(
|
||||
hash(id) * state.size.x,
|
||||
hash(id * id) * state.size.y,
|
||||
);
|
||||
if agents[id].timeToLive > 0 {
|
||||
if agents[id].species == settings.nextGenerationId {
|
||||
atomicAdd(&counters.nextGenerationAlive, 1);
|
||||
} else {
|
||||
atomicAdd(&counters.currentGenerationAlive, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let center = state.size / 2.0;
|
||||
if atomicSub(&counters.remaining, 1) <= 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let direction = position - center;
|
||||
let angle = atan2(direction.y, direction.x);
|
||||
var position: vec2<f32>;
|
||||
var angle: f32;
|
||||
|
||||
if settings.shape == 0.0 {
|
||||
position = vec2(
|
||||
hash(id) * state.size.x,
|
||||
hash(id * id) * state.size.y,
|
||||
);
|
||||
|
||||
let center = state.size / 2.0;
|
||||
let direction = position - center;
|
||||
angle = atan2(direction.y, direction.x);
|
||||
} else if settings.shape == 1.0 {
|
||||
angle = hash(id) * 2.0 * 3.1415;
|
||||
let direction = vec2(cos(angle), sin(angle));
|
||||
position = settings.center + direction * settings.radius * hash(id * 12 + 3);
|
||||
}
|
||||
|
||||
atomicAdd(&counters.nextGenerationAlive, 1);
|
||||
|
||||
agents[id] = Agent(
|
||||
position,
|
||||
angle,
|
||||
0,
|
||||
settings.nextGenerationId,
|
||||
1000000,
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
export interface GenerationCounts {
|
||||
currentGenerationCount: number;
|
||||
nextGenerationCount: number;
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ import shader from './agent.wgsl';
|
|||
|
||||
export class AgentPipeline {
|
||||
private static readonly WORKGROUP_SIZE = 64;
|
||||
private static readonly UNIFORM_COUNT = 5;
|
||||
private static readonly UNIFORM_COUNT = 6;
|
||||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly pipeline: GPUComputePipeline;
|
||||
|
|
@ -21,7 +21,7 @@ export class AgentPipeline {
|
|||
public constructor(
|
||||
private readonly device: GPUDevice,
|
||||
private readonly commonState: CommonState,
|
||||
private readonly agentsBuffer: GPUBuffer
|
||||
private readonly agentsBuffer: GPUBuffer // doesn't get destroyed
|
||||
) {
|
||||
this.bindGroupLayout = device.createBindGroupLayout(AgentPipeline.bindGroupLayout);
|
||||
|
||||
|
|
@ -47,7 +47,8 @@ export class AgentPipeline {
|
|||
turnSpeed,
|
||||
sensorOffsetAngle,
|
||||
sensorOffsetDistance,
|
||||
}: AgentSettings) {
|
||||
nextGenerationAggression,
|
||||
}: AgentSettings & { nextGenerationAggression: number }) {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
|
|
@ -57,6 +58,7 @@ export class AgentPipeline {
|
|||
turnSpeed,
|
||||
(sensorOffsetAngle * Math.PI) / 180,
|
||||
sensorOffsetDistance,
|
||||
nextGenerationAggression,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
|
@ -118,7 +120,6 @@ export class AgentPipeline {
|
|||
|
||||
public destroy() {
|
||||
this.uniforms.destroy();
|
||||
this.agentsBuffer.destroy();
|
||||
}
|
||||
|
||||
private static get bindGroupLayout(): GPUBindGroupLayoutDescriptor {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ struct Settings {
|
|||
turnRate: f32,
|
||||
sensorAngle: f32,
|
||||
sensorOffset: f32,
|
||||
nextGenerationAggression: f32,
|
||||
// nextGenerationParity: f32,
|
||||
};
|
||||
|
||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||
|
|
@ -14,38 +16,29 @@ struct Settings {
|
|||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let id = global_id.x;
|
||||
|
||||
if (id >= arrayLength(&agents)) {
|
||||
if (id >= arrayLength(&agents) || agents[id].timeToLive <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var agent = agents[id];
|
||||
|
||||
// if (agent.timeToLive <= 0.) {
|
||||
// agent.position = vec2(
|
||||
// random_with_seed(agent.position, f32(id) + state.time),
|
||||
// random_with_seed(agent.position, f32(id) + state.time + 12),
|
||||
// );
|
||||
// agent.angle = random_with_seed(vec2(agent.angle), f32(id) + state.time);
|
||||
// agent.species = 1;
|
||||
// agent.timeToLive = 1000;
|
||||
// agents[id] = agent;
|
||||
// return;
|
||||
// }
|
||||
|
||||
let random = hash(id + u32(state.time * 16732.0));
|
||||
let random = hash(id + u32(state.time % 107 * 1673.7));
|
||||
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
||||
|
||||
// var weight: f32;
|
||||
// if(agent.species == 0) {
|
||||
// weight = trailCurrent.r - trailCurrent.g;
|
||||
// } else {
|
||||
// weight = trailCurrent.g - trailCurrent.r;
|
||||
// }
|
||||
// if (weight < 0) {
|
||||
// agent.timeToLive = 0;
|
||||
// return;
|
||||
// }
|
||||
|
||||
var weight: f32;
|
||||
if(agent.species == 0) {
|
||||
if trailCurrent.r < trailCurrent.g {
|
||||
agent.species = 1;
|
||||
agents[id] = agent;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if trailCurrent.g < trailCurrent.r {
|
||||
agent.timeToLive = 0;
|
||||
agents[id] = agent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
||||
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
||||
|
|
@ -59,9 +52,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||
weightLeft += trailLeft.r - trailLeft.g;
|
||||
weightRight += trailRight.r - trailRight.g;
|
||||
} else {
|
||||
weightForward += trailForward.g - trailForward.r;
|
||||
weightLeft += trailLeft.g - trailLeft.r;
|
||||
weightRight += trailRight.g - trailRight.r;
|
||||
weightForward += trailForward.g + trailForward.r * settings.nextGenerationAggression;
|
||||
weightLeft += trailLeft.g + trailLeft.r * settings.nextGenerationAggression;
|
||||
weightRight += trailRight.g + trailRight.r * settings.nextGenerationAggression;
|
||||
}
|
||||
|
||||
var rotation: f32 = 0;
|
||||
|
|
|
|||
|
|
@ -86,7 +86,15 @@ export class CommonState {
|
|||
});
|
||||
}
|
||||
|
||||
public setParameters(canvasSize: vec2, deltaTime: number, time: number) {
|
||||
public setParameters({
|
||||
canvasSize,
|
||||
deltaTime,
|
||||
time,
|
||||
}: {
|
||||
canvasSize: vec2;
|
||||
deltaTime: number;
|
||||
time: number;
|
||||
}) {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
|
|
|
|||
Loading…
Reference in a new issue