|
@ -6,28 +6,33 @@ |
|
|
|
|
|
|
|
|
using namespace std; |
|
|
using namespace std; |
|
|
|
|
|
|
|
|
static std::vector<Vertex> generate_random_points(size_t N){ |
|
|
// implementation follow later
|
|
|
std::vector<Vertex> ret(N); |
|
|
static moggle::shader_program create_shader_from_files(std::string vertex, std::string fragment); |
|
|
|
|
|
template <typename T> |
|
|
const float d = 0.1; |
|
|
static moggle::vbo<T> create_vbo(std::vector<T> const & data); |
|
|
for(auto& x : ret){ |
|
|
static moggle::vao create_vao(moggle::vbo<GLPoint> const & vbo); |
|
|
x[0] = d * rand() / float(RAND_MAX); |
|
|
static moggle::vao create_vao(moggle::vbo<Vertex> const & vbo); |
|
|
x[1] = d * rand() / float(RAND_MAX); |
|
|
static GLuint create_gl_texture(GLsizei width, GLsizei height); |
|
|
x[2] = d * rand() / float(RAND_MAX); |
|
|
static std::vector<GLPoint> generate_random_points(size_t N); |
|
|
} |
|
|
cl::Context create_shared_cl_context(GLContext & gl_context); |
|
|
|
|
|
|
|
|
return ret; |
|
|
std::vector<Vertex> quad = { |
|
|
} |
|
|
{{-1, -1}, {0, 0}}, |
|
|
|
|
|
{{ 1, -1}, {1, 0}}, |
|
|
enum { |
|
|
{{-1, 1}, {0, 1}}, |
|
|
POS_ATTR, |
|
|
{{ 1, 1}, {1, 1}}, |
|
|
TEX_ATTR |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const GLsizei FBO_SIZE = 1024; |
|
|
|
|
|
|
|
|
App::App(GLContext& gl_context) |
|
|
App::App(GLContext& gl_context) |
|
|
: gl_vbo(create_vbo(generate_random_points(N))) |
|
|
: gl_vbo(create_vbo(generate_random_points(N))) |
|
|
, gl_vao(create_vao(gl_vbo)) |
|
|
, gl_vao(create_vao(gl_vbo)) |
|
|
, gl_program(create_shader_from_files("Fractal.vsh", "Fractal.fsh")) |
|
|
, gl_quad_vbo(create_vbo(quad)) |
|
|
|
|
|
, gl_quad_vao(create_vao(gl_quad_vbo)) |
|
|
|
|
|
, gl_points_program(create_shader_from_files("Points.vsh", "Points.fsh")) |
|
|
|
|
|
, gl_texture_program(create_shader_from_files("Texture.vsh", "Texture.fsh")) |
|
|
|
|
|
, gl_fbo(moggle::fbo::create_default(FBO_SIZE, FBO_SIZE)) |
|
|
, cl_context(create_shared_cl_context(gl_context)) |
|
|
, cl_context(create_shared_cl_context(gl_context)) |
|
|
, cl_device(cl_context.getInfo<CL_CONTEXT_DEVICES>().front()) |
|
|
, cl_device(cl_context.getInfo<CL_CONTEXT_DEVICES>().front()) |
|
|
, cl_queue(cl_context, cl_device) |
|
|
, cl_queue(cl_context, cl_device) |
|
@ -40,35 +45,68 @@ App::App(GLContext& gl_context) |
|
|
moggle::gl::enable(GL_BLEND); |
|
|
moggle::gl::enable(GL_BLEND); |
|
|
moggle::gl::blend_function(GL_ONE, GL_ONE); |
|
|
moggle::gl::blend_function(GL_ONE, GL_ONE); |
|
|
moggle::gl::blend_equation(GL_FUNC_ADD); |
|
|
moggle::gl::blend_equation(GL_FUNC_ADD); |
|
|
|
|
|
|
|
|
|
|
|
moggle::gl::active_texture(GL_TEXTURE0); |
|
|
|
|
|
moggle::gl::texture_parameter_i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
|
|
moggle::gl::texture_parameter_i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void App::draw(){ |
|
|
void App::draw(std::function<void(void)> bind, std::function<void(void)> flush){ |
|
|
auto dt = 1/60.0; |
|
|
auto dt = 1/60.0; |
|
|
time += dt; |
|
|
time += dt; |
|
|
if(wait > 0) wait -= dt; |
|
|
|
|
|
|
|
|
|
|
|
if(wait <= 0){ |
|
|
// OpenCL update
|
|
|
k_update(cl_queue, N, p_s, p_r, p_b, p_dt, cl_buffer); |
|
|
k_update(cl_queue, N, p_s, p_r, p_b, p_dt, cl_buffer); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cl::checky(cl_queue.finish()); |
|
|
cl::checky(cl_queue.finish()); |
|
|
|
|
|
|
|
|
moggle::gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
// OpenGL to fbo
|
|
|
|
|
|
gl_fbo.bind(); |
|
|
|
|
|
moggle::gl::viewport(0, 0, FBO_SIZE, FBO_SIZE); |
|
|
|
|
|
|
|
|
gl_program.use(); |
|
|
gl_points_program.use(); |
|
|
gl_vao.bind(); |
|
|
gl_vao.bind(); |
|
|
|
|
|
|
|
|
moggle::gl::draw_arrays(GL_POINTS, 0, N); |
|
|
moggle::gl::draw_arrays(GL_POINTS, 0, N); |
|
|
|
|
|
|
|
|
|
|
|
// Draw fbo
|
|
|
|
|
|
bind(); |
|
|
|
|
|
moggle::gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
|
|
|
|
|
|
|
|
gl_texture_program.use(); |
|
|
|
|
|
|
|
|
|
|
|
gl_fbo.get_texture().bind(GL_TEXTURE_2D); |
|
|
|
|
|
gl_texture_program.uniform<GLint>("tex").set(0); |
|
|
|
|
|
gl_texture_program.uniform<GLfloat>("maxf").set(maxf); |
|
|
|
|
|
|
|
|
|
|
|
gl_quad_vao.bind(); |
|
|
|
|
|
moggle::gl::draw_arrays(GL_TRIANGLE_STRIP, 0, 4); |
|
|
|
|
|
|
|
|
|
|
|
flush(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void App::resize(size_t w, size_t h){ |
|
|
void App::resize(size_t w, size_t h){ |
|
|
wait = 5; |
|
|
|
|
|
|
|
|
|
|
|
W = w; |
|
|
W = w; |
|
|
H = h; |
|
|
H = h; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
moggle::shader_program App::create_shader_from_files(string vertex, string fragment){ |
|
|
cl::Program App::create_cl_program(string file){ |
|
|
|
|
|
cl_int err = 0; |
|
|
|
|
|
|
|
|
|
|
|
// build the program
|
|
|
|
|
|
cl::Program cl_program(cl_context, slurp(file), true, &err); |
|
|
|
|
|
cout << cl_program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(cl_device) << std::endl; |
|
|
|
|
|
|
|
|
|
|
|
cl::checky(err); |
|
|
|
|
|
cout << cl_program << endl; |
|
|
|
|
|
|
|
|
|
|
|
return cl_program; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum { |
|
|
|
|
|
POS_ATTR, |
|
|
|
|
|
TEX_ATTR |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
moggle::shader_program create_shader_from_files(string vertex, string fragment){ |
|
|
moggle::shader_program program; |
|
|
moggle::shader_program program; |
|
|
|
|
|
|
|
|
auto vs = moggle::shader::from_file(moggle::shader_type::vertex, vertex); |
|
|
auto vs = moggle::shader::from_file(moggle::shader_type::vertex, vertex); |
|
@ -78,20 +116,22 @@ moggle::shader_program App::create_shader_from_files(string vertex, string fragm |
|
|
program.attach(fs); |
|
|
program.attach(fs); |
|
|
|
|
|
|
|
|
program.bind_attribute(POS_ATTR, "position"); |
|
|
program.bind_attribute(POS_ATTR, "position"); |
|
|
|
|
|
program.bind_attribute(TEX_ATTR, "tex_coord"); |
|
|
|
|
|
|
|
|
program.link(); |
|
|
program.link(); |
|
|
|
|
|
|
|
|
return program; |
|
|
return program; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
moggle::vbo<Vertex> App::create_vbo(const std::vector<Vertex>& data){ |
|
|
template <typename T> |
|
|
moggle::vbo<Vertex> vbo; |
|
|
moggle::vbo<T> create_vbo(const std::vector<T>& data){ |
|
|
|
|
|
moggle::vbo<T> vbo; |
|
|
vbo.bind(GL_ARRAY_BUFFER); |
|
|
vbo.bind(GL_ARRAY_BUFFER); |
|
|
vbo.data(data); |
|
|
vbo.data(data); |
|
|
return vbo; |
|
|
return vbo; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
moggle::vao App::create_vao(const moggle::vbo<Vertex>& vbo){ |
|
|
moggle::vao create_vao(const moggle::vbo<GLPoint>& vbo){ |
|
|
moggle::vao vao; |
|
|
moggle::vao vao; |
|
|
vao.bind(); |
|
|
vao.bind(); |
|
|
|
|
|
|
|
@ -102,18 +142,41 @@ moggle::vao App::create_vao(const moggle::vbo<Vertex>& vbo){ |
|
|
return vao; |
|
|
return vao; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
GLuint App::create_gl_texture(GLsizei width, GLsizei height){ |
|
|
moggle::vao create_vao(const moggle::vbo<Vertex>& vbo){ |
|
|
|
|
|
moggle::vao vao; |
|
|
|
|
|
vao.bind(); |
|
|
|
|
|
|
|
|
|
|
|
moggle::gl::enable_vertex_attribute_array(POS_ATTR); |
|
|
|
|
|
moggle::gl::enable_vertex_attribute_array(TEX_ATTR); |
|
|
|
|
|
|
|
|
|
|
|
vao.attribute(POS_ATTR, vbo, &Vertex::pos); |
|
|
|
|
|
vao.attribute(TEX_ATTR, vbo, &Vertex::tex); |
|
|
|
|
|
|
|
|
|
|
|
return vao; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GLuint create_gl_texture(GLsizei width, GLsizei height){ |
|
|
GLuint tex = 0; |
|
|
GLuint tex = 0; |
|
|
moggle::gl::active_texture(GL_TEXTURE0); |
|
|
|
|
|
moggle::gl::generate_textures(1, &tex); |
|
|
moggle::gl::generate_textures(1, &tex); |
|
|
moggle::gl::bind_texture(GL_TEXTURE_2D, tex); |
|
|
moggle::gl::bind_texture(GL_TEXTURE_2D, tex); |
|
|
moggle::gl::texture_image_2d(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
|
|
moggle::gl::texture_image_2d(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
|
|
moggle::gl::texture_parameter_i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
|
|
|
moggle::gl::texture_parameter_i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
|
|
|
return tex; |
|
|
return tex; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cl::Context App::create_shared_cl_context(GLContext& gl_context){ |
|
|
static std::vector<GLPoint> generate_random_points(size_t N){ |
|
|
|
|
|
std::vector<GLPoint> ret(N); |
|
|
|
|
|
|
|
|
|
|
|
const auto d = 2.0; |
|
|
|
|
|
for(auto& x : ret){ |
|
|
|
|
|
x[0] = d * rand() / float(RAND_MAX) - 0.5*d; |
|
|
|
|
|
x[1] = d * rand() / float(RAND_MAX) - 0.5*d; |
|
|
|
|
|
x[2] = d * rand() / float(RAND_MAX) - 0.5*d; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cl::Context create_shared_cl_context(GLContext& gl_context){ |
|
|
CGLShareGroupObj sharegroup = CGLGetShareGroup(gl_context.ctx); |
|
|
CGLShareGroupObj sharegroup = CGLGetShareGroup(gl_context.ctx); |
|
|
|
|
|
|
|
|
cl_context_properties properties[] = { |
|
|
cl_context_properties properties[] = { |
|
@ -127,16 +190,3 @@ cl::Context App::create_shared_cl_context(GLContext& gl_context){ |
|
|
|
|
|
|
|
|
return cl_context; |
|
|
return cl_context; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cl::Program App::create_cl_program(string file){ |
|
|
|
|
|
cl_int err = 0; |
|
|
|
|
|
|
|
|
|
|
|
// build the program
|
|
|
|
|
|
cl::Program cl_program(cl_context, slurp(file), true, &err); |
|
|
|
|
|
cout << cl_program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(cl_device) << std::endl; |
|
|
|
|
|
|
|
|
|
|
|
cl::checky(err); |
|
|
|
|
|
cout << cl_program << endl; |
|
|
|
|
|
|
|
|
|
|
|
return cl_program; |
|
|
|
|
|
} |
|
|
|
|
|