I was following a tutorial on compute shades in which they were making a boids simulation. When I followed it I kept getting the error “syntax error: unexpected token ‘;’ at kernel BoidsDataCS”, after the first time I put it in, it never went away even when I removed the groupshared variable.
Here is my code, line 40 is where it says the error is
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel SteeringForcesCS
#pragma kernel BoidsDataCS
struct BoidData
{
float3 velocity;
float3 position;
};
#define THREAD_GROUP_SIZE 128;
StructuredBuffer<BoidData> _BoidsDataBuffer;
RWStructuredBuffer<BoidData> _BoidsDataBufferRW;
StructuredBuffer<float3> _BoidsSteeringForcesBuffer;
RWStructuredBuffer<float3> _BoidsSteeringForcesBufferRW;
int _BoidsCount;
float _DeltaTime;
float _SeperationRadius;
float _AlignmentRadius;
float _CohesionRadius;
float _BoidMaximumSpeed;
float _BoidMaximumSteeringForce;
float _SeperationWeight;
float _AlignmentWeight;
float _CohesionWeight;
float4 _SimulationCenter;
float4 _SimulationDimensions;
float _SimulationBoundsAvoidWeight;
groupshared BoidData boidData[THREAD_GROUP_SIZE];
float3 limit(float3 vec, float max)
{
float lengthSquared = dot(vec, vec);
if (lengthSquared > max * max && lengthSquared > 0)
{
float length = sqrt(lengthSquared);
return vec.xyz * (max / length);
}
return vec.xyz;
}
float3 CheckSimulationBounds(float3 position)
{
float3 wc = _SimulationCenter.xyz;
float3 ws = _SimulationDimensions.xyz;
float3 acc = float3(0, 0, 0);
acc.x = (position.x < wc.x - ws.x * 0.5) ? 1.0 : ((position.x > wc.x + ws.x * 0.5) ? -1.0 : 0.0);
acc.y = (position.y < wc.y - ws.y * 0.5) ? 1.0 : ((position.y > wc.y + ws.y * 0.5) ? -1.0 : 0.0);
acc.z = (position.z < wc.z - ws.z * 0.5) ? 1.0 : ((position.z > wc.z + ws.z * 0.5) ? -1.0 : 0.0);
return acc;
}
[numthreads(THREAD_GROUP_SIZE, 1, 1)]
void SteeringForcesCS(uint3 d_tid : SV_DISPATCHTHREADID, uint gi : SV_GroupIndex from 0 to 255)
{
const unsigned int P_ID = d_tid.x;
const float P_position = _BoidsDataBuffer[P_ID].position;
const float P_velocity = _BoidsDataBuffer[P_ID].velocity;
float3 force = float3(0, 0, 0);
float3 separationPositionOffset = float3(0, 0, 0);
float3 alignmentPositionOffset = float3(0, 0, 0);
float3 cohesionPositionOffset = float3(0, 0, 0);
int seperationBoidsCount = 0;
int alignmentBoidsCount = 0;
int cohesionBoidsCount = 0;
float3 separationSteering = float3(0, 0, 0);
float3 alignmentSteering = float3(0, 0, 0);
float3 cohesionSteering = float3(0, 0, 0);
[loop]
for (uint n_block_id = 0; n_block_id < (uint)_BoidsCount; n_block_id += THREAD_GROUP_SIZE)
{
boid_data[gi] = _BoidsDataBuffer[n_block_id + gi];
GroupMemoryBarrierWithGroupSync();
[unroll]
for (int N_tile_ID = 0, N_tile_ID < THREAD_GROUP_SIZE; N_tile_ID ++)
{
const float3 N_position = boid_data[N_tile_ID].position;
const float3 N_velocity = boid_data[N_tile_ID].velocity;
const float3 diff = P_position - N_position;
const float dist = sqrt(dot(diff, diff));
if (dist > 0.0 && dist <= _SeperationRadius)
{
float3 repulse = normalize(P_position - N_position);
repulse /= dist;
separationPositionOffset += repulse;
seperationBoidsCount++;
}
if (dist > 0.0 && dist <= _AlignmentRadius)
{
alignmentPositionOffset += N_velocity;
alignmentBoidsCount++;
}
if (dist > 0.0 && dist <= _CohesionRadius)
{
cohesionPositionOffset += N_position;
cohesionBoidsCount++;
}
}
GroupMemoryBarrierWithGroupSync();
}
if (seperationBoidsCount > 0)
{
separationSteering = separationPositionOffset / (float)separationBoidsCount;
separationSteering = normalize(separationSteering) * _BoidMaximumSpeed;
separationSteering = separationSteering - p_velocity;
separationSteering = limit(separationSteering, _BoidMaximumSteeringForce);
}
if (alignmentBoidsCount > 0)
{
alignmentSteering = alignmentPositionOffset / (float)alignmentBoidsCount;
alignmentSteering = normalize(alignmentSteering) * _BoidMaximumSpeed;
alignmentSteering = alignmentSteering - P_velocity;
alignmentSteering = limit(alignmentSteering, _BoidMaximumSteeringForce);
}
if (cohesionBoidsCount > 0)
{
cohesionPositionOffset = cohesionPositionOffset / (float)cohesionBoidsCount;
cohesionSteering = cohesionPositionOffset - P_position;
cohesionSteering = normalize(cohesionSteering) * _BoidMaximumSpeed;
cohesionSteering = cohesionSteering - P_velocity;
cohesionSteering = limit(cohesionSteering, _BoidMaximumSteeringForce);
}
force += alignmentSteering * _AlignmentWeight;
force += cohesionSteering * _CohesionWeight;
force += separationSteering * _SeperationWeight;
_BoidsSteeringForcesBufferRW[P_ID] = force;
}
[numthreads(THREAD_GROUP_SIZE, 1, 1)]
void BoidsDataCS(uint3 DTid : SV_DISPATCHTHREADID)
{
const unsigned int p_id = DTid.x;
BoidData boidData = _BoidsDataBufferRW[p_id];
float3 force = _BoidsSteeringForcesBuffer[p_id];
force += CheckSimulationBounds(boidData.position) * _SimulationBoundsAvoidWeight;
boidData.velocity += force * _DeltaTime;
boidData.velocity = limit(boidData.velocity, _BoidMaximumSpeed);
boidData.position += boidData.velocity * _DeltaTime;
_BoidsDataBufferRW[p_id] = boidData;
}
Any Ideas why its happening?