Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Last active July 18, 2024 21:55
Show Gist options
  • Save sortofsleepy/41cab3f324184bddb3ebde932487394e to your computer and use it in GitHub Desktop.
Save sortofsleepy/41cab3f324184bddb3ebde932487394e to your computer and use it in GitHub Desktop.
Grid position helper functions
ivec2 get_coord(int index, int size){
return ivec2(index % size, index / size);
}
int get_index(int x, int y, int size){
return x + size * y;
}
ivec4 get_neighbor_indices(int index, int size){
// get coord of current index
ivec2 coord = get_coord(index, size);
// get coordinates of neighbors
ivec2 left = ivec2(coord.x - 1, coord.y);
int left_index = get_index(left.x, left.y, size);
ivec2 right = ivec2(coord.x + 1, coord.y);
int right_index = get_index(right.x, right.y, size);
ivec2 top = ivec2(coord.x, coord.y - 1);
int top_index = get_index(top.x, top.y, size);
ivec2 bottom = ivec2(coord.x, coord.y + 1);
int bottom_index = get_index(bottom.x, bottom.y, size);
return ivec4(top_index, left_index, bottom_index, right_index);
}
@sortofsleepy
Copy link
Author

sortofsleepy commented Jul 18, 2024

An idea of how to possibly sample the neighbors of a storage buffer value. "size" refers to the "width" of the buffer, as such it's probably best to make buffers an even number to make figuring things out a bit simpler.

Have not tested fully.

Based on this
https://softwareengineering.stackexchange.com/questions/212808/treating-a-1d-data-structure-as-2d-grid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment