stackgl / shader-school

:mortar_board: A workshopper for GLSL shaders and graphics programming
Other
4.28k stars 254 forks source link

Bug in gpgpu-1 (game of life)? [spoilers inside!!] #131

Closed dhcoder closed 9 years ago

dhcoder commented 9 years ago

It doesn't seem like the official solution checks for the out of bounds condition. That is, what does it mean for neighbor (-1, 0) to be sampled?

I have the following code in my shader:

for (int dx = -1; dx <= 1; ++dx) {
  for (int dy = -1; dy <= 1; ++dy) {
    if (dx != 0 || dy != 0) {
      vec2 nCoord = coord + vec2(dx, dy);
      bvec2 gte = greaterThanEqual(nCoord, zero);
      bvec2 lt = lessThan(nCoord, stateSize);
      bvec2 bounded = bvec2(gte.x && lt.x, gte.y && lt.y);

      if (bounded.x && bounded.y) {
        n += state(nCoord);
      }
    }
  }
}

but when I take out the "bounded" logic, and just do

for (int dx = -1; dx <= 1; ++dx) {
  for (int dy = -1; dy <= 1; ++dy) {
    if (dx != 0 || dy != 0) {
        vec2 nCoord = coord + vec2(dx, dy);
        n += state(nCoord);
    }
  }
}

then I match the official solution.

dhcoder commented 9 years ago

Also: I can't seem to mark the "game of life" question as [completed] but I don't really care too much about that. Just FYI!

dhcoder commented 9 years ago

Ack, nevermind. I just saw the instruction in the problem description:

The world in the game of life is a 2D grid of cells, which we shall assume wraps around at the boundary.

Sorry about that!