Generate agents
This commit is contained in:
parent
5ca7514891
commit
7e8ca4b16f
10 changed files with 368 additions and 89 deletions
|
|
@ -1,5 +1,9 @@
|
||||||
export interface GameLoopSettings {
|
export interface GameLoopSettings {
|
||||||
agentCount: number;
|
agentCount: number;
|
||||||
|
initialDeadRatio: number;
|
||||||
renderSpeed: number;
|
renderSpeed: number;
|
||||||
simulatedDelayMs: number;
|
simulatedDelayMs: number;
|
||||||
|
|
||||||
|
aggressionFactor: number;
|
||||||
|
nextGenerationSpawnRadius: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { AgentGenerationPipeline } from '../pipelines/agents/agent-generation/agent-generation-pipeline';
|
import { AgentGenerationPipeline } from '../pipelines/agents/agent-generation/agent-generation-pipeline';
|
||||||
|
import { GenerationCounts } from '../pipelines/agents/agent-generation/generation-counts';
|
||||||
import { AgentPipeline } from '../pipelines/agents/agent-pipeline';
|
import { AgentPipeline } from '../pipelines/agents/agent-pipeline';
|
||||||
import { BrushPipeline } from '../pipelines/brush/brush-pipeline';
|
import { BrushPipeline } from '../pipelines/brush/brush-pipeline';
|
||||||
import { CommonState } from '../pipelines/common-state/common-state';
|
import { CommonState } from '../pipelines/common-state/common-state';
|
||||||
|
|
@ -7,16 +8,17 @@ import { DiffusionPipeline } from '../pipelines/diffusion/diffusion-pipeline';
|
||||||
import { RenderPipeline } from '../pipelines/render/render-pipeline';
|
import { RenderPipeline } from '../pipelines/render/render-pipeline';
|
||||||
import { settings } from '../settings';
|
import { settings } from '../settings';
|
||||||
import { DeltaTimeCalculator } from '../utils/delta-time-calculator';
|
import { DeltaTimeCalculator } from '../utils/delta-time-calculator';
|
||||||
|
import { initializeContext } from '../utils/graphics/initialize-context';
|
||||||
import { ResizableTexture } from '../utils/graphics/resizable-texture';
|
import { ResizableTexture } from '../utils/graphics/resizable-texture';
|
||||||
import { sleep } from '../utils/sleep';
|
import { sleep } from '../utils/sleep';
|
||||||
|
import { GameRules } from './game-rules';
|
||||||
|
|
||||||
import { vec2 } from 'gl-matrix';
|
import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
export default class GameLoop {
|
export default class GameLoop {
|
||||||
private readonly deltaTimeCalculator = new DeltaTimeCalculator();
|
|
||||||
|
|
||||||
private readonly trailMapA: ResizableTexture;
|
private readonly trailMapA: ResizableTexture;
|
||||||
private readonly trailMapB: ResizableTexture;
|
private readonly trailMapB: ResizableTexture;
|
||||||
|
|
||||||
private readonly commonState: CommonState;
|
private readonly commonState: CommonState;
|
||||||
private readonly copyPipeline: CopyPipeline;
|
private readonly copyPipeline: CopyPipeline;
|
||||||
private readonly agentGenerationPipeline: AgentGenerationPipeline;
|
private readonly agentGenerationPipeline: AgentGenerationPipeline;
|
||||||
|
|
@ -25,6 +27,8 @@ export default class GameLoop {
|
||||||
private readonly brushPipeline: BrushPipeline;
|
private readonly brushPipeline: BrushPipeline;
|
||||||
private readonly diffusionPipeline: DiffusionPipeline;
|
private readonly diffusionPipeline: DiffusionPipeline;
|
||||||
|
|
||||||
|
private readonly gameRules = new GameRules(performance.now() / 1000);
|
||||||
|
|
||||||
private hasFinished = false;
|
private hasFinished = false;
|
||||||
private readonly hasFinishedPromise: Promise<void> = new Promise(
|
private readonly hasFinishedPromise: Promise<void> = new Promise(
|
||||||
(resolve) => (this.resolveHasFinished = resolve)
|
(resolve) => (this.resolveHasFinished = resolve)
|
||||||
|
|
@ -35,33 +39,34 @@ export default class GameLoop {
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly canvas: HTMLCanvasElement,
|
private readonly canvas: HTMLCanvasElement,
|
||||||
private readonly device: GPUDevice
|
private readonly device: GPUDevice,
|
||||||
|
private readonly deltaTimeCalculator: DeltaTimeCalculator
|
||||||
) {
|
) {
|
||||||
const context = this.canvas.getContext('webgpu') as any as GPUCanvasContext;
|
const context = initializeContext({ device, canvas });
|
||||||
context.configure({
|
|
||||||
device: this.device,
|
|
||||||
format: navigator.gpu.getPreferredCanvasFormat(),
|
|
||||||
alphaMode: 'premultiplied',
|
|
||||||
});
|
|
||||||
|
|
||||||
this.trailMapA = new ResizableTexture(this.device, this.canvasSize);
|
this.trailMapA = new ResizableTexture(this.device, this.canvasSize);
|
||||||
this.trailMapB = new ResizableTexture(this.device, this.canvasSize);
|
this.trailMapB = new ResizableTexture(this.device, this.canvasSize);
|
||||||
this.resize();
|
this.resize();
|
||||||
|
|
||||||
this.commonState = new CommonState(this.device);
|
this.commonState = new CommonState(this.device);
|
||||||
this.commonState.setParameters(this.canvasSize, 0, 0);
|
this.commonState.setParameters({
|
||||||
|
canvasSize: this.canvasSize,
|
||||||
|
time: 0,
|
||||||
|
deltaTime: 0,
|
||||||
|
});
|
||||||
|
|
||||||
this.copyPipeline = new CopyPipeline(this.device);
|
this.copyPipeline = new CopyPipeline(this.device);
|
||||||
|
|
||||||
this.agentGenerationPipeline = new AgentGenerationPipeline(
|
this.agentGenerationPipeline = new AgentGenerationPipeline(
|
||||||
this.device,
|
this.device,
|
||||||
this.commonState
|
this.commonState,
|
||||||
|
settings.agentCount
|
||||||
);
|
);
|
||||||
|
|
||||||
this.agentPipeline = new AgentPipeline(
|
this.agentPipeline = new AgentPipeline(
|
||||||
this.device,
|
this.device,
|
||||||
this.commonState,
|
this.commonState,
|
||||||
this.agentGenerationPipeline.generateAgents(settings.agentCount)
|
this.agentGenerationPipeline.agentsBuffer
|
||||||
);
|
);
|
||||||
this.brushPipeline = new BrushPipeline(this.device, this.commonState);
|
this.brushPipeline = new BrushPipeline(this.device, this.commonState);
|
||||||
this.diffusionPipeline = new DiffusionPipeline(this.device, this.commonState);
|
this.diffusionPipeline = new DiffusionPipeline(this.device, this.commonState);
|
||||||
|
|
@ -85,6 +90,10 @@ export default class GameLoop {
|
||||||
return this.hasFinishedPromise;
|
return this.hasFinishedPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get aliveAgentCounts(): GenerationCounts {
|
||||||
|
return this.gameRules.generationCounts;
|
||||||
|
}
|
||||||
|
|
||||||
private onSwipe(event: MouseEvent) {
|
private onSwipe(event: MouseEvent) {
|
||||||
if (!this.isSwipeActive) {
|
if (!this.isSwipeActive) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -104,23 +113,56 @@ export default class GameLoop {
|
||||||
|
|
||||||
private async render(time: DOMHighResTimeStamp) {
|
private async render(time: DOMHighResTimeStamp) {
|
||||||
if (this.hasFinished) {
|
if (this.hasFinished) {
|
||||||
|
this.resolveHasFinished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deltaTime = this.deltaTimeCalculator.calculateDeltaTimeInSeconds(time);
|
const deltaTime = this.deltaTimeCalculator.calculateDeltaTimeInSeconds(time);
|
||||||
|
|
||||||
this.commonState.setParameters(this.canvasSize, deltaTime, time);
|
time *= settings.renderSpeed;
|
||||||
|
const timeInSeconds = time / 1000;
|
||||||
|
|
||||||
[
|
[
|
||||||
|
this.commonState,
|
||||||
this.agentPipeline,
|
this.agentPipeline,
|
||||||
this.brushPipeline,
|
this.brushPipeline,
|
||||||
this.diffusionPipeline,
|
this.diffusionPipeline,
|
||||||
this.renderPipeline,
|
this.renderPipeline,
|
||||||
].forEach((pipeline) => pipeline.setParameters(settings));
|
].forEach((pipeline) =>
|
||||||
|
pipeline.setParameters({
|
||||||
const commandEncoder = this.device.createCommandEncoder();
|
time,
|
||||||
|
nextGenerationAggression: this.gameRules.nextGenerationAgression,
|
||||||
|
deltaTime,
|
||||||
|
canvasSize: this.canvasSize,
|
||||||
|
...settings,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
for (let i = 0; i < settings.renderSpeed; i++) {
|
for (let i = 0; i < settings.renderSpeed; i++) {
|
||||||
|
const commandEncoder = this.device.createCommandEncoder();
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.gameRules.generationCounts.currentGenerationCount == 0 &&
|
||||||
|
this.gameRules.generationCounts.nextGenerationCount == 0
|
||||||
|
) {
|
||||||
|
this.gameRules.updateGenerationCounts(
|
||||||
|
await this.agentGenerationPipeline.spawnNextGenerationCover(
|
||||||
|
0,
|
||||||
|
settings.agentCount * (1 - settings.initialDeadRatio)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const spawnAction = this.gameRules.getSpawnAction(timeInSeconds, this.canvasSize);
|
||||||
|
this.gameRules.updateGenerationCounts(
|
||||||
|
await this.agentGenerationPipeline.spawnNextGenerationCircle(
|
||||||
|
spawnAction.generation,
|
||||||
|
spawnAction.count,
|
||||||
|
spawnAction.position,
|
||||||
|
settings.nextGenerationSpawnRadius
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
this.copyPipeline.execute(
|
this.copyPipeline.execute(
|
||||||
commandEncoder,
|
commandEncoder,
|
||||||
this.trailMapA.getTextureView(),
|
this.trailMapA.getTextureView(),
|
||||||
|
|
@ -138,9 +180,9 @@ export default class GameLoop {
|
||||||
this.trailMapA.getTextureView()
|
this.trailMapA.getTextureView()
|
||||||
);
|
);
|
||||||
this.renderPipeline.execute(commandEncoder, this.trailMapA.getTextureView());
|
this.renderPipeline.execute(commandEncoder, this.trailMapA.getTextureView());
|
||||||
}
|
|
||||||
|
|
||||||
this.device.queue.submit([commandEncoder.finish()]);
|
this.device.queue.submit([commandEncoder.finish()]);
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.isSwipeActive) {
|
if (!this.isSwipeActive) {
|
||||||
this.brushPipeline.clearSwipes();
|
this.brushPipeline.clearSwipes();
|
||||||
|
|
@ -157,20 +199,19 @@ export default class GameLoop {
|
||||||
requestAnimationFrame(this.render.bind(this));
|
requestAnimationFrame(this.render.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
public destroy() {
|
public async destroy() {
|
||||||
this.hasFinished = true;
|
this.hasFinished = true;
|
||||||
|
await this.hasFinishedPromise;
|
||||||
|
|
||||||
this.copyPipeline?.destroy();
|
this.copyPipeline?.destroy();
|
||||||
|
this.agentGenerationPipeline?.destroy();
|
||||||
this.agentPipeline?.destroy();
|
this.agentPipeline?.destroy();
|
||||||
this.brushPipeline?.destroy();
|
this.brushPipeline?.destroy();
|
||||||
this.diffusionPipeline?.destroy();
|
this.diffusionPipeline?.destroy();
|
||||||
this.renderPipeline?.destroy();
|
this.renderPipeline?.destroy();
|
||||||
this.commonState?.destroy();
|
this.commonState?.destroy();
|
||||||
|
|
||||||
this.trailMapA?.destroy();
|
this.trailMapA?.destroy();
|
||||||
this.trailMapB?.destroy();
|
this.trailMapB?.destroy();
|
||||||
|
|
||||||
this.resolveHasFinished();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private get canvasSize(): vec2 {
|
private get canvasSize(): vec2 {
|
||||||
|
|
|
||||||
78
src/game-loop/game-rules.ts
Normal file
78
src/game-loop/game-rules.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import { GenerationCounts } from '../pipelines/agents/agent-generation/generation-counts';
|
||||||
|
import { settings } from '../settings';
|
||||||
|
import { clamp01 } from '../utils/clamp';
|
||||||
|
import { Random } from '../utils/random';
|
||||||
|
|
||||||
|
import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
|
export interface SpawnAction {
|
||||||
|
generation: number;
|
||||||
|
position: vec2;
|
||||||
|
count: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GameRules {
|
||||||
|
private static SPAWN_INTERVAL = 3;
|
||||||
|
|
||||||
|
private lastSpawnTimeInSeconds = 0;
|
||||||
|
private nextGenerationId = 0;
|
||||||
|
public generationCounts: GenerationCounts = {
|
||||||
|
currentGenerationCount: 0,
|
||||||
|
nextGenerationCount: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
public constructor(startingTimeInSeconds: number) {
|
||||||
|
this.lastSpawnTimeInSeconds = startingTimeInSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSpawnAction(timeInSeconds: number, canvasSize: vec2): SpawnAction {
|
||||||
|
if (timeInSeconds - this.lastSpawnTimeInSeconds < GameRules.SPAWN_INTERVAL) {
|
||||||
|
return {
|
||||||
|
generation: this.nextGenerationId,
|
||||||
|
position: vec2.create(),
|
||||||
|
count: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastSpawnTimeInSeconds = timeInSeconds;
|
||||||
|
|
||||||
|
return {
|
||||||
|
generation: this.nextGenerationId,
|
||||||
|
position: vec2.fromValues(
|
||||||
|
Random.randomBetween(0, canvasSize.x),
|
||||||
|
Random.randomBetween(0, canvasSize.y)
|
||||||
|
),
|
||||||
|
count:
|
||||||
|
settings.agentCount -
|
||||||
|
this.generationCounts.nextGenerationCount -
|
||||||
|
this.generationCounts.currentGenerationCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateGenerationCounts({
|
||||||
|
currentGenerationCount,
|
||||||
|
nextGenerationCount,
|
||||||
|
}: GenerationCounts): void {
|
||||||
|
if (currentGenerationCount === 0) {
|
||||||
|
this.nextGenerationId++;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.generationCounts = {
|
||||||
|
currentGenerationCount,
|
||||||
|
nextGenerationCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public get nextGenerationAgression(): number {
|
||||||
|
if (this.generationCounts.currentGenerationCount === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return clamp01(
|
||||||
|
(this.generationCounts.nextGenerationCount /
|
||||||
|
this.generationCounts.currentGenerationCount -
|
||||||
|
1) *
|
||||||
|
settings.aggressionFactor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,21 +4,42 @@ import { CommonState } from '../../common-state/common-state';
|
||||||
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
||||||
import shader from './agent-generation.wgsl';
|
import shader from './agent-generation.wgsl';
|
||||||
import agentSchema from './agent-schema.wgsl';
|
import agentSchema from './agent-schema.wgsl';
|
||||||
|
import { GenerationCounts } from './generation-counts';
|
||||||
|
|
||||||
|
import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
export class AgentGenerationPipeline {
|
export class AgentGenerationPipeline {
|
||||||
private static readonly WORKGROUP_SIZE = 64;
|
private static readonly WORKGROUP_SIZE = 64;
|
||||||
|
private static readonly UNIFORM_COUNT = 6;
|
||||||
|
private static readonly COUNTER_COUNT = 3;
|
||||||
|
|
||||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||||
private readonly pipeline: GPUComputePipeline;
|
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(
|
public constructor(
|
||||||
private readonly device: GPUDevice,
|
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({
|
this.bindGroupLayout = device.createBindGroupLayout({
|
||||||
entries: [
|
entries: [
|
||||||
|
{
|
||||||
|
binding: 0,
|
||||||
|
visibility: GPUShaderStage.COMPUTE,
|
||||||
|
buffer: {
|
||||||
|
type: 'uniform',
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
binding: 1,
|
binding: 1,
|
||||||
visibility: GPUShaderStage.COMPUTE,
|
visibility: GPUShaderStage.COMPUTE,
|
||||||
|
|
@ -26,6 +47,57 @@ export class AgentGenerationPipeline {
|
||||||
type: 'storage',
|
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 {
|
public async spawnNextGenerationCover(
|
||||||
if (agentCount <= 0 || agentCount != Math.floor(agentCount)) {
|
generationId: number,
|
||||||
throw new Error('Agent count must be a positive integer');
|
count: number
|
||||||
|
): Promise<GenerationCounts> {
|
||||||
|
this.device.queue.writeBuffer(
|
||||||
|
this.uniforms,
|
||||||
|
0,
|
||||||
|
new Float32Array([0, 0, 0, generationId, 0])
|
||||||
|
);
|
||||||
|
|
||||||
|
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
||||||
|
|
||||||
|
return this.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
const agentsBuffer = this.device.createBuffer({
|
public async spawnNextGenerationCircle(
|
||||||
size: agentCount * AGENT_SIZE_IN_BYTES,
|
generationId: number,
|
||||||
usage: GPUBufferUsage.STORAGE,
|
count: number,
|
||||||
});
|
center: vec2,
|
||||||
|
radius: number
|
||||||
|
): Promise<GenerationCounts> {
|
||||||
|
this.device.queue.writeBuffer(
|
||||||
|
this.uniforms,
|
||||||
|
0,
|
||||||
|
new Float32Array([...center, radius, generationId, 1])
|
||||||
|
);
|
||||||
|
|
||||||
this.bindGroup = this.device.createBindGroup({
|
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
||||||
layout: this.bindGroupLayout,
|
|
||||||
entries: [
|
|
||||||
{
|
|
||||||
binding: 1,
|
|
||||||
resource: {
|
|
||||||
buffer: agentsBuffer,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
|
return this.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async execute(): Promise<GenerationCounts> {
|
||||||
const commandEncoder = this.device.createCommandEncoder();
|
const commandEncoder = this.device.createCommandEncoder();
|
||||||
|
|
||||||
const passEncoder = commandEncoder.beginComputePass();
|
const passEncoder = commandEncoder.beginComputePass();
|
||||||
|
|
@ -69,11 +152,34 @@ export class AgentGenerationPipeline {
|
||||||
this.commonState.execute(passEncoder);
|
this.commonState.execute(passEncoder);
|
||||||
passEncoder.setBindGroup(1, this.bindGroup);
|
passEncoder.setBindGroup(1, this.bindGroup);
|
||||||
passEncoder.dispatchWorkgroups(
|
passEncoder.dispatchWorkgroups(
|
||||||
Math.ceil(agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||||
);
|
);
|
||||||
passEncoder.end();
|
passEncoder.end();
|
||||||
|
|
||||||
|
commandEncoder.copyBufferToBuffer(
|
||||||
|
this.countersBuffer,
|
||||||
|
0,
|
||||||
|
this.countersStagingBuffer,
|
||||||
|
0,
|
||||||
|
AgentGenerationPipeline.COUNTER_COUNT * Int32Array.BYTES_PER_ELEMENT
|
||||||
|
);
|
||||||
|
|
||||||
this.device.queue.submit([commandEncoder.finish()]);
|
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)
|
@compute @workgroup_size(64)
|
||||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
let id = global_id.x;
|
let id = global_id.x;
|
||||||
|
|
||||||
if (id >= arrayLength(&agents)) {
|
if id >= arrayLength(&agents) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let position = vec2(
|
if agents[id].timeToLive > 0 {
|
||||||
|
if agents[id].species == settings.nextGenerationId {
|
||||||
|
atomicAdd(&counters.nextGenerationAlive, 1);
|
||||||
|
} else {
|
||||||
|
atomicAdd(&counters.currentGenerationAlive, 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if atomicSub(&counters.remaining, 1) <= 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var position: vec2<f32>;
|
||||||
|
var angle: f32;
|
||||||
|
|
||||||
|
if settings.shape == 0.0 {
|
||||||
|
position = vec2(
|
||||||
hash(id) * state.size.x,
|
hash(id) * state.size.x,
|
||||||
hash(id * id) * state.size.y,
|
hash(id * id) * state.size.y,
|
||||||
);
|
);
|
||||||
|
|
||||||
let center = state.size / 2.0;
|
let center = state.size / 2.0;
|
||||||
|
|
||||||
let direction = position - center;
|
let direction = position - center;
|
||||||
let angle = atan2(direction.y, direction.x);
|
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(
|
agents[id] = Agent(
|
||||||
position,
|
position,
|
||||||
angle,
|
angle,
|
||||||
0,
|
settings.nextGenerationId,
|
||||||
1000000,
|
1000000,
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
export interface GenerationCounts {
|
||||||
|
currentGenerationCount: number;
|
||||||
|
nextGenerationCount: number;
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ import shader from './agent.wgsl';
|
||||||
|
|
||||||
export class AgentPipeline {
|
export class AgentPipeline {
|
||||||
private static readonly WORKGROUP_SIZE = 64;
|
private static readonly WORKGROUP_SIZE = 64;
|
||||||
private static readonly UNIFORM_COUNT = 5;
|
private static readonly UNIFORM_COUNT = 6;
|
||||||
|
|
||||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||||
private readonly pipeline: GPUComputePipeline;
|
private readonly pipeline: GPUComputePipeline;
|
||||||
|
|
@ -21,7 +21,7 @@ export class AgentPipeline {
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly device: GPUDevice,
|
private readonly device: GPUDevice,
|
||||||
private readonly commonState: CommonState,
|
private readonly commonState: CommonState,
|
||||||
private readonly agentsBuffer: GPUBuffer
|
private readonly agentsBuffer: GPUBuffer // doesn't get destroyed
|
||||||
) {
|
) {
|
||||||
this.bindGroupLayout = device.createBindGroupLayout(AgentPipeline.bindGroupLayout);
|
this.bindGroupLayout = device.createBindGroupLayout(AgentPipeline.bindGroupLayout);
|
||||||
|
|
||||||
|
|
@ -47,7 +47,8 @@ export class AgentPipeline {
|
||||||
turnSpeed,
|
turnSpeed,
|
||||||
sensorOffsetAngle,
|
sensorOffsetAngle,
|
||||||
sensorOffsetDistance,
|
sensorOffsetDistance,
|
||||||
}: AgentSettings) {
|
nextGenerationAggression,
|
||||||
|
}: AgentSettings & { nextGenerationAggression: number }) {
|
||||||
this.device.queue.writeBuffer(
|
this.device.queue.writeBuffer(
|
||||||
this.uniforms,
|
this.uniforms,
|
||||||
0,
|
0,
|
||||||
|
|
@ -57,6 +58,7 @@ export class AgentPipeline {
|
||||||
turnSpeed,
|
turnSpeed,
|
||||||
(sensorOffsetAngle * Math.PI) / 180,
|
(sensorOffsetAngle * Math.PI) / 180,
|
||||||
sensorOffsetDistance,
|
sensorOffsetDistance,
|
||||||
|
nextGenerationAggression,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +120,6 @@ export class AgentPipeline {
|
||||||
|
|
||||||
public destroy() {
|
public destroy() {
|
||||||
this.uniforms.destroy();
|
this.uniforms.destroy();
|
||||||
this.agentsBuffer.destroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static get bindGroupLayout(): GPUBindGroupLayoutDescriptor {
|
private static get bindGroupLayout(): GPUBindGroupLayoutDescriptor {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ struct Settings {
|
||||||
turnRate: f32,
|
turnRate: f32,
|
||||||
sensorAngle: f32,
|
sensorAngle: f32,
|
||||||
sensorOffset: f32,
|
sensorOffset: f32,
|
||||||
|
nextGenerationAggression: f32,
|
||||||
|
// nextGenerationParity: f32,
|
||||||
};
|
};
|
||||||
|
|
||||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||||
|
|
@ -14,38 +16,29 @@ struct Settings {
|
||||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
let id = global_id.x;
|
let id = global_id.x;
|
||||||
|
|
||||||
if (id >= arrayLength(&agents)) {
|
if (id >= arrayLength(&agents) || agents[id].timeToLive <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var agent = agents[id];
|
var agent = agents[id];
|
||||||
|
|
||||||
// if (agent.timeToLive <= 0.) {
|
let random = hash(id + u32(state.time % 107 * 1673.7));
|
||||||
// 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 trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
||||||
|
|
||||||
// var weight: f32;
|
var weight: f32;
|
||||||
// if(agent.species == 0) {
|
if(agent.species == 0) {
|
||||||
// weight = trailCurrent.r - trailCurrent.g;
|
if trailCurrent.r < trailCurrent.g {
|
||||||
// } else {
|
agent.species = 1;
|
||||||
// weight = trailCurrent.g - trailCurrent.r;
|
agents[id] = agent;
|
||||||
// }
|
return;
|
||||||
// if (weight < 0) {
|
}
|
||||||
// agent.timeToLive = 0;
|
} else {
|
||||||
// return;
|
if trailCurrent.g < trailCurrent.r {
|
||||||
// }
|
agent.timeToLive = 0;
|
||||||
|
agents[id] = agent;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
||||||
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
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;
|
weightLeft += trailLeft.r - trailLeft.g;
|
||||||
weightRight += trailRight.r - trailRight.g;
|
weightRight += trailRight.r - trailRight.g;
|
||||||
} else {
|
} else {
|
||||||
weightForward += trailForward.g - trailForward.r;
|
weightForward += trailForward.g + trailForward.r * settings.nextGenerationAggression;
|
||||||
weightLeft += trailLeft.g - trailLeft.r;
|
weightLeft += trailLeft.g + trailLeft.r * settings.nextGenerationAggression;
|
||||||
weightRight += trailRight.g - trailRight.r;
|
weightRight += trailRight.g + trailRight.r * settings.nextGenerationAggression;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rotation: f32 = 0;
|
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.device.queue.writeBuffer(
|
||||||
this.uniforms,
|
this.uniforms,
|
||||||
0,
|
0,
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,13 @@ export const settings: GameLoopSettings &
|
||||||
BrushSettings &
|
BrushSettings &
|
||||||
DiffusionSettings &
|
DiffusionSettings &
|
||||||
RenderSettings = {
|
RenderSettings = {
|
||||||
agentCount: 4_000_000,
|
agentCount: 4_000_000, // requires restart
|
||||||
|
initialDeadRatio: 0.2, // requires restart
|
||||||
|
|
||||||
renderSpeed: 1,
|
aggressionFactor: 0.5, // requires restart
|
||||||
|
nextGenerationSpawnRadius: 50,
|
||||||
|
|
||||||
|
renderSpeed: 5,
|
||||||
simulatedDelayMs: 0,
|
simulatedDelayMs: 0,
|
||||||
|
|
||||||
brushWidth: 20,
|
brushWidth: 20,
|
||||||
|
|
@ -39,7 +43,7 @@ export const settings: GameLoopSettings &
|
||||||
decayRateBrush: 0.995, // inverse
|
decayRateBrush: 0.995, // inverse
|
||||||
|
|
||||||
brushColor: palette.blue,
|
brushColor: palette.blue,
|
||||||
speciesColorA: palette.yellow,
|
speciesAColor: palette.yellow,
|
||||||
speciesColorB: palette.purple,
|
speciesBColor: palette.purple,
|
||||||
clarity: 3,
|
clarity: 3,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue