aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-12-16 13:58:20 +0100
committerAdrian Kummerlaender2018-12-16 13:58:20 +0100
commit2eb7f9925315989735b4fb746cbd7bda6c9bd5bb (patch)
tree67e94d6898eac507a8a7c2c6cccf7f875f2979dc
parent44f5ac32a68a617f93704d44c4339f7db13b323e (diff)
downloadcompustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar.gz
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar.bz2
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar.lz
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar.xz
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.tar.zst
compustream-2eb7f9925315989735b4fb746cbd7bda6c9bd5bb.zip
Parametrize lattice resolution
-rw-r--r--src/buffer/vertex/fluid_cell_buffer.cc4
-rw-r--r--src/buffer/vertex/fluid_cell_buffer.h2
-rw-r--r--src/buffer/vertex/lattice_cell_buffer.cc12
-rw-r--r--src/buffer/vertex/lattice_cell_buffer.h2
-rw-r--r--src/main.cc19
-rw-r--r--src/shader/code/collide.glsl39
-rw-r--r--src/shader/code/stream.glsl22
-rw-r--r--src/shader/code/vertex.glsl2
-rw-r--r--src/shader/wrap/compute_shader.cc12
-rw-r--r--src/shader/wrap/compute_shader.h3
10 files changed, 76 insertions, 41 deletions
diff --git a/src/buffer/vertex/fluid_cell_buffer.cc b/src/buffer/vertex/fluid_cell_buffer.cc
index 5056569..c602e0c 100644
--- a/src/buffer/vertex/fluid_cell_buffer.cc
+++ b/src/buffer/vertex/fluid_cell_buffer.cc
@@ -1,7 +1,7 @@
#include "fluid_cell_buffer.h"
-FluidCellBuffer::FluidCellBuffer():
- _data(3*128*128, GLfloat{}) {
+FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY):
+ _data(3*nX*nY, GLfloat{}) {
glGenVertexArrays(1, &_array);
glGenBuffers(1, &_buffer);
diff --git a/src/buffer/vertex/fluid_cell_buffer.h b/src/buffer/vertex/fluid_cell_buffer.h
index b2b880a..f71c2cb 100644
--- a/src/buffer/vertex/fluid_cell_buffer.h
+++ b/src/buffer/vertex/fluid_cell_buffer.h
@@ -12,7 +12,7 @@ private:
GLuint _buffer;
public:
- FluidCellBuffer();
+ FluidCellBuffer(GLuint nX, GLuint nY);
~FluidCellBuffer();
GLuint getBuffer() const;
diff --git a/src/buffer/vertex/lattice_cell_buffer.cc b/src/buffer/vertex/lattice_cell_buffer.cc
index 7fb51d8..c2b877b 100644
--- a/src/buffer/vertex/lattice_cell_buffer.cc
+++ b/src/buffer/vertex/lattice_cell_buffer.cc
@@ -2,16 +2,18 @@
#include <fstream>
-LatticeCellBuffer::LatticeCellBuffer():
- _data(9*128*128, GLfloat{1./9.}) {
+LatticeCellBuffer::LatticeCellBuffer(GLuint nX, GLuint nY):
+ _data(9*nX*nY, GLfloat{1./9.}) {
glGenVertexArrays(1, &_array);
glGenBuffers(1, &_buffer);
- for (int x = 50; x < 128-50; x++) {
- for (int y = 50; y < 128-50; y++) {
+ const int inset = 0.4*nX;
+
+ for (int x = inset; x < nX-inset; x++) {
+ for (int y = inset; y < nY-inset; y++) {
for ( int i = -1; i <= 1; ++i ) {
for ( int j = -1; j <= 1; ++j ) {
- _data[9*128*y + 9*x + (i+1)*3 + j+1] = 1./128.;
+ _data[9*nX*y + 9*x + (i+1)*3 + j+1] = 1./64.;
}
}
}
diff --git a/src/buffer/vertex/lattice_cell_buffer.h b/src/buffer/vertex/lattice_cell_buffer.h
index c28319e..f6638c8 100644
--- a/src/buffer/vertex/lattice_cell_buffer.h
+++ b/src/buffer/vertex/lattice_cell_buffer.h
@@ -12,7 +12,7 @@ private:
GLuint _buffer;
public:
- LatticeCellBuffer();
+ LatticeCellBuffer(GLuint nX, GLuint nY);
~LatticeCellBuffer();
GLuint getBuffer() const;
diff --git a/src/main.cc b/src/main.cc
index 914d505..1feb649 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -22,6 +22,9 @@
#include "timer.h"
+constexpr GLuint nX = 128;
+constexpr GLuint nY = 128;
+
float getWorldHeight(int window_width, int window_height, float world_width) {
return world_width / window_width * window_height;
}
@@ -53,7 +56,7 @@ int renderWindow() {
int window_width = window.getWidth();
int window_height = window.getHeight();
- float world_width = 256.0;
+ float world_width = 2*nX;
float world_height = getWorldHeight(window_width, window_height, world_width);
glm::mat4 MVP = getMVP(world_width, world_height);
@@ -71,12 +74,12 @@ int renderWindow() {
scene_shader = std::make_unique<GraphicShader>(
VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
- lattice_buffer_a = std::make_unique<LatticeCellBuffer>();
- lattice_buffer_b = std::make_unique<LatticeCellBuffer>();
- fluid_buffer = std::make_unique<FluidCellBuffer>();
+ lattice_buffer_a = std::make_unique<LatticeCellBuffer>(nX, nY);
+ lattice_buffer_b = std::make_unique<LatticeCellBuffer>(nX, nY);
+ fluid_buffer = std::make_unique< FluidCellBuffer>(nX, nY);
collide_shader = std::make_unique<ComputeShader>(COLLIDE_SHADER_CODE);
- stream_shader = std::make_unique<ComputeShader>(STREAM_SHADER_CODE);
+ stream_shader = std::make_unique<ComputeShader>(STREAM_SHADER_CODE);
});
if ( !collide_shader->isGood() || !stream_shader->isGood() ) {
@@ -107,7 +110,7 @@ int renderWindow() {
}
if ( update_lattice ) {
- if ( timer::millisecondsSince(last_frame) >= 1000/25 ) {
+ if ( timer::millisecondsSince(last_frame) >= 1000/50 ) {
if ( tick ) {
collide_shader->workOn(lattice_buffer_a->getBuffer(), lattice_buffer_b->getBuffer(), fluid_buffer->getBuffer());
stream_shader->workOn(lattice_buffer_a->getBuffer(), lattice_buffer_b->getBuffer(), fluid_buffer->getBuffer());
@@ -120,11 +123,11 @@ int renderWindow() {
{
auto guard = collide_shader->use();
- collide_shader->dispatch();
+ collide_shader->dispatch(nX, nY);
}
{
auto guard = stream_shader->use();
- stream_shader->dispatch();
+ stream_shader->dispatch(nX, nY);
}
last_frame = timer::now();
diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl
index 67e762b..90af535 100644
--- a/src/shader/code/collide.glsl
+++ b/src/shader/code/collide.glsl
@@ -2,34 +2,43 @@ static const std::string COLLIDE_SHADER_CODE = R"(
#version 430
layout (local_size_x = 1, local_size_y = 1) in;
+
layout (std430, binding=1) buffer bufferCollide{ float collideCells[]; };
layout (std430, binding=2) buffer bufferStream{ float streamCells[]; };
layout (std430, binding=3) buffer bufferFluid{ float fluidCells[]; };
+uniform uint nX;
+uniform uint nY;
+
+const uint q = 9;
+const float omega = 0.6;
+
+const float velocityDisplayScalar = 500.;
+
float get(uint x, uint y, int i, int j) {
- return collideCells[9*128*y + 9*x + (i+1)*3 + j+1];
+ return collideCells[q*nX*y + q*x + (i+1)*3 + j+1];
}
void set(uint x, uint y, int i, int j, float v) {
- collideCells[9*128*y + 9*x + (i+1)*3 + j+1] = v;
+ collideCells[q*nX*y + q*x + (i+1)*3 + j+1] = v;
}
void setFluid(uint x, uint y, vec2 v, float d) {
- fluidCells[3*128*y + 3*x + 0] = float(x)-64. + 10.*v.x;
- fluidCells[3*128*y + 3*x + 1] = float(y)-64. + 10.*v.y;
- fluidCells[3*128*y + 3*x + 2] = d;
+ fluidCells[3*nX*y + 3*x + 0] = float(x)-nX/2 + velocityDisplayScalar*v.x;
+ fluidCells[3*nX*y + 3*x + 1] = float(y)-nY/2 + velocityDisplayScalar*v.y;
+ fluidCells[3*nX*y + 3*x + 2] = d;
}
float density(uint x, uint y) {
- return collideCells[9*128*y + 9*x + 0]
- + collideCells[9*128*y + 9*x + 1]
- + collideCells[9*128*y + 9*x + 2]
- + collideCells[9*128*y + 9*x + 3]
- + collideCells[9*128*y + 9*x + 4]
- + collideCells[9*128*y + 9*x + 5]
- + collideCells[9*128*y + 9*x + 6]
- + collideCells[9*128*y + 9*x + 7]
- + collideCells[9*128*y + 9*x + 8];
+ return collideCells[q*nX*y + q*x + 0]
+ + collideCells[q*nX*y + q*x + 1]
+ + collideCells[q*nX*y + q*x + 2]
+ + collideCells[q*nX*y + q*x + 3]
+ + collideCells[q*nX*y + q*x + 4]
+ + collideCells[q*nX*y + q*x + 5]
+ + collideCells[q*nX*y + q*x + 6]
+ + collideCells[q*nX*y + q*x + 7]
+ + collideCells[q*nX*y + q*x + 8];
}
vec2 velocity(uint x, uint y, float d) {
@@ -77,8 +86,6 @@ void main() {
const uint x = gl_GlobalInvocationID.x;
const uint y = gl_GlobalInvocationID.y;
- const float omega = 0.6;
-
const float d = density(x,y);
const vec2 v = velocity(x,y,d);
diff --git a/src/shader/code/stream.glsl b/src/shader/code/stream.glsl
index 7dd5ba8..147b49b 100644
--- a/src/shader/code/stream.glsl
+++ b/src/shader/code/stream.glsl
@@ -2,29 +2,43 @@ static const std::string STREAM_SHADER_CODE = R"(
#version 430
layout (local_size_x = 1, local_size_y = 1) in;
+
layout (std430, binding=1) buffer bufferCollide{ float collideCells[]; };
layout (std430, binding=2) buffer bufferStream{ float streamCells[]; };
+uniform uint nX;
+uniform uint nY;
+
+const uint q = 9;
+
float get(uint x, uint y, int i, int j) {
- return collideCells[9*128*y + 9*x + (i+1)*3 + j+1];
+ return collideCells[q*nX*y + q*x + (i+1)*3 + j+1];
}
void set(uint x, uint y, int i, int j, float v) {
- streamCells[9*128*y + 9*x + (i+1)*3 + j+1] = v;
+ streamCells[q*nX*y + q*x + (i+1)*3 + j+1] = v;
}
void main() {
const uint x = gl_GlobalInvocationID.x;
const uint y = gl_GlobalInvocationID.y;
- if ( x != 0 && x != 128-1 && y != 0 && y != 128-1 ) {
+ if ( x != 0 && x != nX-1 && y != 0 && y != nY-1 ) {
for ( int i = -1; i <= 1; ++i ) {
for ( int j = -1; j <= 1; ++j ) {
set(x+i,y+j,i,j, get(x,y,i,j));
}
}
} else {
-
+ for ( int i = -1; i <= 1; ++i ) {
+ for ( int j = -1; j <= 1; ++j ) {
+ if ( (x > 0 || i >= 0) && x+i <= nX-1 && (y > 0 || j >= 0) && y+j <= nY-1 ) {
+ set(x+i,y+j,i,j, get(x,y,i,j));
+ } else {
+ set(x,y,i*(-1),j*(-1), get(x,y,i,j));
+ }
+ }
+ }
}
}
)";
diff --git a/src/shader/code/vertex.glsl b/src/shader/code/vertex.glsl
index 4c307d8..0eccad4 100644
--- a/src/shader/code/vertex.glsl
+++ b/src/shader/code/vertex.glsl
@@ -3,6 +3,6 @@ uniform mat4 MVP;
void main() {
gl_Position = MVP * vec4(gl_Vertex.xy, 0.0, 1.0);
- gl_FrontColor = vec4(1., 0., 0., 0.);
+ gl_FrontColor = gl_Vertex.z * vec4(1., 0., 0., 0.);
}
)";
diff --git a/src/shader/wrap/compute_shader.cc b/src/shader/wrap/compute_shader.cc
index c90c370..ab5d4c4 100644
--- a/src/shader/wrap/compute_shader.cc
+++ b/src/shader/wrap/compute_shader.cc
@@ -40,12 +40,20 @@ GLuint ComputeShader::setUniform(const std::string& name, float x, float y) cons
return id;
}
+GLuint ComputeShader::setUniform(const std::string& name, unsigned int value) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform1ui(id, value);
+ return id;
+}
+
void ComputeShader::workOn(GLuint bufferA, GLuint bufferB, GLuint bufferC) const {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bufferA);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bufferB);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, bufferC);
}
-void ComputeShader::dispatch() const {
- glDispatchCompute(128, 128, 1);
+void ComputeShader::dispatch(GLuint nX, GLuint nY) const {
+ setUniform("nX", nX);
+ setUniform("nY", nY);
+ glDispatchCompute(nX, nY, 1);
}
diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h
index 74c8270..816b925 100644
--- a/src/shader/wrap/compute_shader.h
+++ b/src/shader/wrap/compute_shader.h
@@ -26,7 +26,8 @@ public:
bool isGood() const;
GLuint setUniform(const std::string& name, float x, float y) const;
+ GLuint setUniform(const std::string& name, unsigned int value) const;
void workOn(GLuint bufferA, GLuint bufferB, GLuint bufferC) const;
- void dispatch() const;
+ void dispatch(GLuint nX, GLuint nY) const;
};