Add cancer
This commit is contained in:
parent
b547dc3339
commit
3414f38c3a
14 changed files with 246 additions and 149 deletions
21
src/pipelines/agents/agent-generation/agent-counting.wgsl
Normal file
21
src/pipelines/agents/agent-generation/agent-counting.wgsl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
struct Counters {
|
||||
evenGenerationAlive: atomic<i32>,
|
||||
oddGenerationAlive: atomic<i32>,
|
||||
};
|
||||
|
||||
@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) {
|
||||
return;
|
||||
}
|
||||
|
||||
if agents[id].species % 2 == 0 {
|
||||
atomicAdd(&counters.evenGenerationAlive, 1);
|
||||
} else {
|
||||
atomicAdd(&counters.oddGenerationAlive, 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
@compute @workgroup_size(64)
|
||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let id = global_id.x;
|
||||
|
||||
if id >= arrayLength(&agents) {
|
||||
return;
|
||||
}
|
||||
|
||||
let position = vec2(
|
||||
hash(id) * state.size.x,
|
||||
hash(id * id) * state.size.y,
|
||||
);
|
||||
let center = state.size / 2.0;
|
||||
let direction = position - center;
|
||||
|
||||
agents[id] = Agent(
|
||||
position,
|
||||
atan2(direction.y, direction.x),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
|
@ -2,7 +2,9 @@ import random from '../../../utils/graphics/random.wgsl';
|
|||
import { smartCompile } from '../../../utils/graphics/smart-compile';
|
||||
import { CommonState } from '../../common-state/common-state';
|
||||
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
||||
import shader from './agent-generation.wgsl';
|
||||
import countingShader from './agent-counting.wgsl';
|
||||
import firstGenerationShader from './agent-first-generation.wgsl';
|
||||
import agentGenerationShader from './agent-generation.wgsl';
|
||||
import agentSchema from './agent-schema.wgsl';
|
||||
import { GenerationCounts } from './generation-counts';
|
||||
|
||||
|
|
@ -10,14 +12,17 @@ import { vec2 } from 'gl-matrix';
|
|||
|
||||
export class AgentGenerationPipeline {
|
||||
private static readonly WORKGROUP_SIZE = 64;
|
||||
private static readonly UNIFORM_COUNT = 6;
|
||||
private static readonly UNIFORM_COUNT = 4;
|
||||
private static readonly COUNTER_COUNT = 3;
|
||||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly pipeline: GPUComputePipeline;
|
||||
private readonly uniforms: GPUBuffer;
|
||||
private readonly bindGroup: GPUBindGroup;
|
||||
|
||||
private readonly firstGenerationPipeline: GPUComputePipeline;
|
||||
private readonly nextGenerationPipeline: GPUComputePipeline;
|
||||
private readonly countingPipeline: GPUComputePipeline;
|
||||
|
||||
public readonly agentsBuffer: GPUBuffer;
|
||||
public readonly countersBuffer: GPUBuffer;
|
||||
public readonly countersStagingBuffer: GPUBuffer;
|
||||
|
|
@ -101,54 +106,98 @@ export class AgentGenerationPipeline {
|
|||
],
|
||||
});
|
||||
|
||||
this.pipeline = device.createComputePipeline({
|
||||
this.firstGenerationPipeline = device.createComputePipeline({
|
||||
layout: device.createPipelineLayout({
|
||||
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
|
||||
}),
|
||||
compute: {
|
||||
module: smartCompile(device, CommonState.shaderCode, random, agentSchema, shader),
|
||||
module: smartCompile(
|
||||
device,
|
||||
CommonState.shaderCode,
|
||||
random,
|
||||
agentSchema,
|
||||
firstGenerationShader
|
||||
),
|
||||
entryPoint: 'main',
|
||||
},
|
||||
});
|
||||
|
||||
this.nextGenerationPipeline = device.createComputePipeline({
|
||||
layout: device.createPipelineLayout({
|
||||
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
|
||||
}),
|
||||
compute: {
|
||||
module: smartCompile(
|
||||
device,
|
||||
CommonState.shaderCode,
|
||||
random,
|
||||
agentSchema,
|
||||
agentGenerationShader
|
||||
),
|
||||
entryPoint: 'main',
|
||||
},
|
||||
});
|
||||
|
||||
this.countingPipeline = device.createComputePipeline({
|
||||
layout: device.createPipelineLayout({
|
||||
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
|
||||
}),
|
||||
compute: {
|
||||
module: smartCompile(
|
||||
device,
|
||||
CommonState.shaderCode,
|
||||
random,
|
||||
agentSchema,
|
||||
countingShader
|
||||
),
|
||||
entryPoint: 'main',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public async spawnNextGenerationCover(
|
||||
generationId: number,
|
||||
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();
|
||||
}
|
||||
|
||||
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> {
|
||||
public spawnFirstGeneration(): void {
|
||||
const commandEncoder = this.device.createCommandEncoder();
|
||||
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
passEncoder.setPipeline(this.pipeline);
|
||||
this.commonState.execute(passEncoder);
|
||||
passEncoder.setPipeline(this.firstGenerationPipeline);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.dispatchWorkgroups(
|
||||
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
passEncoder.end();
|
||||
|
||||
this.device.queue.submit([commandEncoder.finish()]);
|
||||
}
|
||||
|
||||
public spawnNextGeneration(center: vec2, radius: number, generationId: number): void {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
new Float32Array([...center, radius, generationId])
|
||||
);
|
||||
|
||||
const commandEncoder = this.device.createCommandEncoder();
|
||||
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
this.commonState.execute(passEncoder);
|
||||
passEncoder.setPipeline(this.nextGenerationPipeline);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.dispatchWorkgroups(
|
||||
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||
);
|
||||
passEncoder.end();
|
||||
|
||||
this.device.queue.submit([commandEncoder.finish()]);
|
||||
}
|
||||
|
||||
public async countAgents(): Promise<GenerationCounts> {
|
||||
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0]));
|
||||
|
||||
const commandEncoder = this.device.createCommandEncoder();
|
||||
|
||||
const passEncoder = commandEncoder.beginComputePass();
|
||||
passEncoder.setPipeline(this.countingPipeline);
|
||||
this.commonState.execute(passEncoder);
|
||||
passEncoder.setBindGroup(1, this.bindGroup);
|
||||
passEncoder.dispatchWorkgroups(
|
||||
|
|
@ -171,8 +220,8 @@ export class AgentGenerationPipeline {
|
|||
const data = new Int32Array(this.countersStagingBuffer.getMappedRange().slice(0));
|
||||
this.countersStagingBuffer.unmap();
|
||||
return {
|
||||
currentGenerationCount: data[0],
|
||||
nextGenerationCount: data[1],
|
||||
evenGenerationCount: data[0],
|
||||
oddGenerationCount: data[1],
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,17 +2,9 @@ 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>) {
|
||||
|
|
@ -22,44 +14,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||
return;
|
||||
}
|
||||
|
||||
if agents[id].timeToLive > 0 {
|
||||
if agents[id].species == settings.nextGenerationId {
|
||||
atomicAdd(&counters.nextGenerationAlive, 1);
|
||||
} else {
|
||||
atomicAdd(&counters.currentGenerationAlive, 1);
|
||||
}
|
||||
return;
|
||||
if length(settings.center - agents[id].position) < settings.radius {
|
||||
agents[id].species = settings.nextGenerationId;
|
||||
}
|
||||
|
||||
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 * 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,
|
||||
settings.nextGenerationId,
|
||||
1000000,
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ struct Agent {
|
|||
position: vec2<f32>,
|
||||
angle: f32,
|
||||
species: f32,
|
||||
timeToLive: f32,
|
||||
}
|
||||
|
||||
@group(1) @binding(1) var<storage, read_write> agents: array<Agent>;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ export interface Agent {
|
|||
position: vec2;
|
||||
angle: number;
|
||||
species: number;
|
||||
timeToLive: number;
|
||||
}
|
||||
|
||||
export const AGENT_SIZE_IN_BYTES = 5 * Float32Array.BYTES_PER_ELEMENT;
|
||||
export const AGENT_SIZE_IN_BYTES = 4 * Float32Array.BYTES_PER_ELEMENT;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export interface GenerationCounts {
|
||||
currentGenerationCount: number;
|
||||
nextGenerationCount: number;
|
||||
evenGenerationCount: number;
|
||||
oddGenerationCount: number;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import shader from './agent.wgsl';
|
|||
|
||||
export class AgentPipeline {
|
||||
private static readonly WORKGROUP_SIZE = 64;
|
||||
private static readonly UNIFORM_COUNT = 6;
|
||||
private static readonly UNIFORM_COUNT = 8;
|
||||
|
||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||
private readonly pipeline: GPUComputePipeline;
|
||||
|
|
@ -47,8 +47,14 @@ export class AgentPipeline {
|
|||
turnSpeed,
|
||||
sensorOffsetAngle,
|
||||
sensorOffsetDistance,
|
||||
nextGenerationAggression,
|
||||
}: AgentSettings & { nextGenerationAggression: number }) {
|
||||
evenGenerationAggression,
|
||||
oddGenerationAggression,
|
||||
nextGenerationId,
|
||||
}: AgentSettings & {
|
||||
evenGenerationAggression: number;
|
||||
oddGenerationAggression: number;
|
||||
nextGenerationId: number;
|
||||
}) {
|
||||
this.device.queue.writeBuffer(
|
||||
this.uniforms,
|
||||
0,
|
||||
|
|
@ -58,7 +64,9 @@ export class AgentPipeline {
|
|||
turnSpeed,
|
||||
(sensorOffsetAngle * Math.PI) / 180,
|
||||
sensorOffsetDistance,
|
||||
nextGenerationAggression,
|
||||
evenGenerationAggression,
|
||||
oddGenerationAggression,
|
||||
nextGenerationId,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,9 @@ struct Settings {
|
|||
turnRate: f32,
|
||||
sensorAngle: f32,
|
||||
sensorOffset: f32,
|
||||
nextGenerationAggression: f32,
|
||||
// nextGenerationParity: f32,
|
||||
evenGenerationAggression: f32,
|
||||
oddGenerationAggression: f32,
|
||||
nextGenerationId: f32
|
||||
};
|
||||
|
||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||
|
|
@ -16,7 +17,7 @@ struct Settings {
|
|||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
let id = global_id.x;
|
||||
|
||||
if (id >= arrayLength(&agents) || agents[id].timeToLive <= 0) {
|
||||
if (id >= arrayLength(&agents)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -26,19 +27,11 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// even generation id -> red channel
|
||||
// odd generation id -> green channel
|
||||
|
||||
let isFromEvenGeneration = agent.species % 2 == 0;
|
||||
|
||||
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
||||
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
||||
|
|
@ -47,14 +40,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||
var weightForward: f32 = trailForward.a * settings.brushTrailWeight;
|
||||
var weightLeft: f32 = trailLeft.a * settings.brushTrailWeight;
|
||||
var weightRight: f32 = trailRight.a * settings.brushTrailWeight;
|
||||
if (agent.species == 0) {
|
||||
weightForward += trailForward.r - trailForward.g;
|
||||
weightLeft += trailLeft.r - trailLeft.g;
|
||||
weightRight += trailRight.r - trailRight.g;
|
||||
if (isFromEvenGeneration) {
|
||||
weightForward += trailForward.r + settings.evenGenerationAggression * trailForward.g;
|
||||
weightLeft += trailLeft.r + settings.evenGenerationAggression * trailLeft.g;
|
||||
weightRight += trailRight.r + settings.evenGenerationAggression * trailRight.g;
|
||||
} else {
|
||||
weightForward += trailForward.g + trailForward.r * settings.nextGenerationAggression;
|
||||
weightLeft += trailLeft.g + trailLeft.r * settings.nextGenerationAggression;
|
||||
weightRight += trailRight.g + trailRight.r * settings.nextGenerationAggression;
|
||||
weightForward += trailForward.g + settings.oddGenerationAggression * trailForward.r;
|
||||
weightLeft += trailLeft.g + settings.oddGenerationAggression * trailLeft.r;
|
||||
weightRight += trailRight.g + settings.oddGenerationAggression * trailRight.r;
|
||||
}
|
||||
|
||||
var rotation: f32 = 0;
|
||||
|
|
@ -77,16 +70,35 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||
}
|
||||
|
||||
var trail = vec4<f32>(0, 0.1, 0, 0);
|
||||
if (agent.species == 0) {
|
||||
if (isFromEvenGeneration) {
|
||||
trail = vec4(0.1, 0, 0, 0);
|
||||
}
|
||||
|
||||
let current = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
|
||||
textureStore(trailMapOut, vec2<i32>(nextPosition), vec4(trail.rgb + current.rgb, current.a));
|
||||
let current = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
|
||||
let next = vec4(trail.rgb + current.rgb, current.a);
|
||||
textureStore(trailMapOut, vec2<i32>(nextPosition), next);
|
||||
|
||||
|
||||
if(isFromEvenGeneration) {
|
||||
if next.r < next.g {
|
||||
if agent.species == settings.nextGenerationId {
|
||||
// agent.species -= 1;
|
||||
} else {
|
||||
agent.species += 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if next.g < next.r {
|
||||
if agent.species == settings.nextGenerationId {
|
||||
// agent.species -= 1;
|
||||
} else {
|
||||
agent.species += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
agent.position = nextPosition;
|
||||
agent.angle = nextAngle;
|
||||
agent.timeToLive -= state.deltaTime;
|
||||
agents[id] = agent;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue