Skip to content

Instantly share code, notes, and snippets.

@tomaka
Created May 30, 2016 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaka/86635d5e4cfdca67d0fee7423e70d859 to your computer and use it in GitHub Desktop.
Save tomaka/86635d5e4cfdca67d0fee7423e70d859 to your computer and use it in GitHub Desktop.
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
struct Neighbour {
int id;
float dist;
};
struct TileDynamic {
float tax_money_in_delivery;
float population;
int dropout_direction;
float dist_nearest_primary_dropout;
float dist_nearest_secondary_dropout;
vec4 resources_stock[8];
vec4 resources_flux[8];
};
struct TileRead {
vec2 position;
Neighbour neighbours[8];
int controller_id;
float local_science_production;
int agriculture_request;
uint climate;
float terrain_penalty;
float infrastructure_level;
int num_primary_dropout;
int num_secondary_dropout;
vec4 resources_availability[8];
};
struct Nation {
vec3 rgb_color;
float money_per_population_per_tick;
};
struct NationOut {
float taxes_money;
};
layout(set = 0, binding = 0) readonly buffer TilesRead {
TileRead tiles[];
};
layout(set = 0, binding = 1) readonly buffer TilesIn {
TileDynamic tiles_in[];
};
layout(set = 0, binding = 2) buffer TilesOut {
TileDynamic tiles_out[];
};
layout(set = 0, binding = 3) readonly buffer NationsRead {
Nation nations[];
};
layout(set = 0, binding = 4) buffer NationsOut {
NationOut nations_out[];
};
void calculate_resource(uint tile_id) {
vec4 self_demand_weight[1];
for (int res_id = 0; res_id < 1; ++res_id) {
self_demand_weight[res_id] = 1.0 - exp(1.0 - tiles_in[tile_id].population / tiles_in[tile_id].resources_stock[res_id]);
self_demand_weight[res_id] *= 1.0 + 0.02 * log(2000.0 + tiles_in[tile_id].population);
tiles_out[tile_id].resources_stock[res_id] = tiles_in[tile_id].resources_stock[res_id];
tiles_out[tile_id].resources_stock[res_id] += tiles_in[tile_id].population * 0.005;
}
for (int i = 0; i < 1; ++i) {
Neighbour neighbour = tiles[tile_id].neighbours[i];
if (neighbour.id == -1) return;
for (int res_id = 0; i < 1; ++res_id) {
vec4 neighbour_stock = tiles_in[neighbour.id].resources_stock[res_id];
float neighbour_pop = tiles_in[neighbour.id].population;
vec4 neighbour_demand_weight = 1.0 - exp(1.0 - neighbour_pop / neighbour_stock);
neighbour_demand_weight *= 1.0 + 0.02 * log(2000.0 + neighbour_pop);
vec4 transfer_from_neighbour = self_demand_weight[res_id] - neighbour_demand_weight;
vec4 transfer_quantity = mix(tiles_in[tile_id].resources_stock[res_id], neighbour_stock, step(0.0, transfer_from_neighbour));
transfer_quantity *= transfer_from_neighbour;
transfer_quantity /= 8.0;
tiles_out[tile_id].resources_stock[res_id] += transfer_quantity;
}
}
}
void main() {
uint tile_id = gl_NumWorkGroups.x * gl_WorkGroupID.y + gl_WorkGroupID.x;
calculate_resource(tile_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment