int main (int argc, char** argv) { /*---------------------- Initialize OpenGL and OpenCL ----------------------*/ if (init_gl(argc,argv,&glinfo, window_size, "RT") != 0){ std::cerr << "Failed to initialize GL" << std::endl; exit(1); } else { std::cout << "Initialized GL succesfully" << std::endl; } if (init_cl(glinfo,&clinfo) != CL_SUCCESS){ std::cerr << "Failed to initialize CL" << std::endl; exit(1); } else { std::cout << "Initialized CL succesfully" << std::endl; } print_cl_info(clinfo); /* Initialize device interface */ if (device.initialize(clinfo)) { std::cerr << "Failed to initialize device interface" << std::endl; exit(1); } /* Initialize generic gpu library */ DeviceFunctionLibrary::initialize(clinfo); /*---------------------- Create shared GL-CL texture ----------------------*/ gl_tex = create_tex_gl(window_size[0],window_size[1]); tex_id = device.new_memory(); DeviceMemory& tex_mem = device.memory(tex_id); if (tex_mem.initialize_from_gl_texture(gl_tex)) { std::cerr << "Failed to create memory object from gl texture" << std::endl; exit(1); } /*---------------------- Set up scene ---------------------------*/ frames = 15; const std::string pack = "pack1OBJ"; // const std::string pack = "pack2OBJ"; double wave_attenuation = 1.f; scenes.resize(frames); for (uint32_t i = 0; i < frames ; ++i) { scenes[i].initialize(); std::stringstream grid_path,visual_path; grid_path << "models/obj/" << pack << "/gridFluid" << i+1 << ".obj"; mesh_id grid_mid = scenes[i].load_obj_file_as_aggregate(grid_path.str()); object_id grid_oid = scenes[i].add_object(grid_mid); Object& grid = scenes[i].object(grid_oid); grid.mat.diffuse = White; grid.mat.reflectiveness = 0.95f; grid.mat.refractive_index = 1.5f; grid.geom.setScale(makeVector(1.f,wave_attenuation,1.f)); /* ---- Solids ------ */ // visual_path << "models/obj/" << pack << "/visual" << visual_frame << ".obj"; // mesh_id visual_mid = scenes[i].load_obj_file_as_aggregate(visual_path.str()); // object_id visual_oid = scenes[i].geometry.add_object(visual_mid); // Object& visual = scenes[i].geometry.object(visual_oid); // visual.mat.diffuse = Red; // for (uint32_t j = 0; j < bridge_parts ; ++j) { // std::stringstream bridge_path; // bridge_path << "models/obj/" << pack << "/bridge" << j << i+1 << ".obj"; // mesh_id bridge_mid = scenes[i].load_obj_file_as_aggregate(bridge_path.str()); // object_id bridge_oid = scenes[i].geometry.add_object(bridge_mid); // Object& bridge = scenes[i].geometry.object(bridge_oid); // bridge.mat.diffuse = Green; // } // mesh_id teapot_mesh_id = scenes[i].load_obj_file_as_aggregate("models/obj/teapot2.obj"); // mesh_id teapot_mesh_id = scenes[i].load_obj_file_as_aggregate("models/obj/teapot-low_res.obj"); // object_id teapot_obj_id = scenes[i].geometry.add_object(teapot_mesh_id); // Object& teapot_obj = scenes[i].geometry.object(teapot_obj_id); // teapot_obj.geom.setPos(makeVector(-1.f,0.f,0.f)); // teapot_obj.geom.setScale(makeVector(3.f,3.f,3.f)); // teapot_obj.mat.diffuse = Green; // teapot_obj.mat.shininess = 1.f; // teapot_obj.mat.reflectiveness = 0.3f; /* ------------------*/ // scenes[i].set_accelerator_type(KDTREE_ACCELERATOR); scenes[i].set_accelerator_type(BVH_ACCELERATOR); scenes[i].create_aggregate_mesh(); scenes[i].create_aggregate_accelerator(); Mesh& scene_mesh = scenes[i].get_aggregate_mesh(); if (scenes[i].transfer_aggregate_mesh_to_device() || scenes[i].transfer_aggregate_accelerator_to_device()) std::cerr << "Failed to transfer scene info to device"<< std::endl; /*---------------------- Print scene data ----------------------*/ std::cerr << "Scene " << i << " stats: " << std::endl; std::cerr << "\tTriangle count: " << scene_mesh.triangleCount() << std::endl; std::cerr << "\tVertex count: " << scene_mesh.vertexCount() << std::endl; std::cerr << std::endl; } /*---------------------- Set initial Camera paramaters -----------------------*/ camera.set(makeVector(0,3,-30), makeVector(0,0,1), makeVector(0,1,0), M_PI/4., window_size[0] / (float)window_size[1]); /*---------------------------- Set tile size ------------------------------*/ best_tile_size = clinfo.max_compute_units * clinfo.max_work_item_sizes[0]; best_tile_size *= 64; best_tile_size = std::min(pixel_count, best_tile_size); /*---------------------- Initialize ray bundles -----------------------------*/ int32_t ray_bundle_size = best_tile_size * 4; if (ray_bundle_1.initialize(ray_bundle_size)) { std::cerr << "Error initializing ray bundle 1" << std::endl; std::cerr.flush(); exit(1); } if (ray_bundle_2.initialize(ray_bundle_size)) { std::cerr << "Error initializing ray bundle 2" << std::endl; std::cerr.flush(); exit(1); } std::cout << "Initialized ray bundles succesfully" << std::endl; /*---------------------- Initialize hit bundle -----------------------------*/ int32_t hit_bundle_size = ray_bundle_size; if (hit_bundle.initialize(hit_bundle_size)) { std::cerr << "Error initializing hit bundle" << std::endl; std::cerr.flush(); exit(1); } std::cout << "Initialized hit bundle succesfully" << std::endl; /*----------------------- Initialize cubemap ---------------------------*/ if (cubemap.initialize("textures/cubemap/Path/posx.jpg", "textures/cubemap/Path/negx.jpg", "textures/cubemap/Path/posy.jpg", "textures/cubemap/Path/negy.jpg", "textures/cubemap/Path/posz.jpg", "textures/cubemap/Path/negz.jpg")) { std::cerr << "Failed to initialize cubemap." << std::endl; exit(1); } std::cerr << "Initialized cubemap succesfully." << std::endl; /*------------------------ Initialize FrameBuffer ---------------------------*/ if (framebuffer.initialize(window_size)) { std::cerr << "Error initializing framebuffer." << std::endl; exit(1); } std::cout << "Initialized framebuffer succesfully." << std::endl; /* ------------------ Initialize ray tracer kernel ----------------------*/ if (tracer.initialize()){ std::cerr << "Failed to initialize tracer." << std::endl; return 0; } std::cerr << "Initialized tracer succesfully." << std::endl; /* ------------------ Initialize Primary Ray Generator ----------------------*/ if (prim_ray_gen.initialize()) { std::cerr << "Error initializing primary ray generator." << std::endl; exit(1); } std::cout << "Initialized primary ray generator succesfully." << std::endl; /* ------------------ Initialize Secondary Ray Generator ----------------------*/ if (sec_ray_gen.initialize()) { std::cerr << "Error initializing secondary ray generator." << std::endl; exit(1); } sec_ray_gen.set_max_rays(ray_bundle_1.count()); std::cout << "Initialized secondary ray generator succesfully." << std::endl; /*------------------------ Initialize RayShader ---------------------------*/ if (ray_shader.initialize()) { std::cerr << "Error initializing ray shader." << std::endl; exit(1); } std::cout << "Initialized ray shader succesfully." << std::endl; /*----------------------- Enable timing in all clases -------------------*/ framebuffer.timing(true); prim_ray_gen.timing(true); sec_ray_gen.timing(true); tracer.timing(true); ray_shader.timing(true); /*------------------------- Count mem usage -----------------------------------*/ int32_t total_cl_mem = 0; total_cl_mem += pixel_count * 4; /* 4bpp texture */ // for (uint32_t i = 0; i < frames; ++i) // total_cl_mem += scene_info[i].size(); total_cl_mem += ray_bundle_1.mem().size() + ray_bundle_2.mem().size(); total_cl_mem += hit_bundle.mem().size(); total_cl_mem += cubemap.positive_x_mem().size() * 6; total_cl_mem += framebuffer.image_mem().size(); std::cout << "\nMemory stats: " << std::endl; std::cout << "\tTotal opencl mem usage: " << total_cl_mem/1e6 << " MB." << std::endl; // for (uint32_t i = 0; i < frames; ++i) // std::cout << "\tScene " << i << " mem usage: " << scene_info[i].size()/1e6 << " MB." << std::endl; std::cout << "\tFramebuffer+Tex mem usage: " << (framebuffer.image_mem().size() + pixel_count * 4)/1e6 << " MB."<< std::endl; std::cout << "\tCubemap mem usage: " << (cubemap.positive_x_mem().size()*6)/1e6 << " MB."<< std::endl; std::cout << "\tRay mem usage: " << (ray_bundle_1.mem().size()*2)/1e6 << " MB."<< std::endl; std::cout << "\tRay hit info mem usage: " << hit_bundle.mem().size()/1e6 << " MB."<< std::endl; /* !! ---------------------- Test area ---------------- */ std::cerr << std::endl; std::cerr << "Misc info: " << std::endl; std::cerr << "Tile size: " << best_tile_size << std::endl; std::cerr << "Tiles: " << pixel_count / (float)best_tile_size << std::endl; std::cerr << "color_cl size: " << sizeof(color_cl) << std::endl; std::cerr << "directional_light_cl size: " << sizeof(directional_light_cl) << std::endl; std::cerr << "ray_cl size: " << sizeof(ray_cl) << std::endl; std::cerr << "sample_cl size: " << sizeof(sample_cl) << std::endl; std::cerr << "sample_trace_info_cl size: " << sizeof(sample_trace_info_cl) << std::endl; /*------------------------ Set GLUT and misc functions -----------------------*/ rt_time.snap_time(); seq_time.snap_time(); glutKeyboardFunc(gl_key); glutMotionFunc(gl_mouse); glutDisplayFunc(gl_loop); glutIdleFunc(gl_loop); glutMainLoop(); clinfo.release_resources(); return 0; }
int32_t RayShader::shade(RayBundle& rays, HitBundle& hb, Scene& scene, Cubemap& cm, FrameBuffer& fb, size_t size, bool primary) { if (m_timing) m_timer.snap_time(); DeviceInterface& device = *DeviceInterface::instance(); function_id shade_id = primary? p_shade_id : s_shade_id; DeviceFunction& shade_function = device.function(shade_id); if (shade_function.set_arg(0, fb.image_mem())) return -1; if (shade_function.set_arg(1,hb.mem())) return -1; if (shade_function.set_arg(2,rays.mem())) return -1; if (shade_function.set_arg(3,scene.material_list_mem())) return -1; if (shade_function.set_arg(4,scene.material_map_mem())) return -1; if (shade_function.set_arg(5,cm.positive_x_mem())) return -1; if (shade_function.set_arg(6,cm.negative_x_mem())) return -1; if (shade_function.set_arg(7,cm.positive_y_mem())) return -1; if (shade_function.set_arg(8,cm.negative_y_mem())) return -1; if (shade_function.set_arg(9,cm.positive_z_mem())) return -1; if (shade_function.set_arg(10,cm.negative_z_mem())) return -1; /*Texture_0 to Texture_25*/ if (shade_function.set_arg(11,scene.texture_atlas.texture_mem(0))) return -1; if (shade_function.set_arg(12,scene.texture_atlas.texture_mem(1))) return -1; if (shade_function.set_arg(13,scene.texture_atlas.texture_mem(2))) return -1; if (shade_function.set_arg(14,scene.texture_atlas.texture_mem(3))) return -1; if (shade_function.set_arg(15,scene.texture_atlas.texture_mem(4))) return -1; if (shade_function.set_arg(16,scene.texture_atlas.texture_mem(5))) return -1; if (shade_function.set_arg(17,scene.texture_atlas.texture_mem(6))) return -1; if (shade_function.set_arg(18,scene.texture_atlas.texture_mem(7))) return -1; if (shade_function.set_arg(19,scene.texture_atlas.texture_mem(8))) return -1; if (shade_function.set_arg(20,scene.texture_atlas.texture_mem(9))) return -1; if (shade_function.set_arg(21,scene.texture_atlas.texture_mem(10))) return -1; if (shade_function.set_arg(22,scene.texture_atlas.texture_mem(11))) return -1; if (shade_function.set_arg(23,scene.texture_atlas.texture_mem(12))) return -1; if (shade_function.set_arg(24,scene.texture_atlas.texture_mem(13))) return -1; if (shade_function.set_arg(25,scene.texture_atlas.texture_mem(14))) return -1; if (shade_function.set_arg(26,scene.texture_atlas.texture_mem(15))) return -1; if (shade_function.set_arg(27,scene.texture_atlas.texture_mem(16))) return -1; if (shade_function.set_arg(28,scene.texture_atlas.texture_mem(17))) return -1; if (shade_function.set_arg(29,scene.texture_atlas.texture_mem(18))) return -1; if (shade_function.set_arg(30,scene.texture_atlas.texture_mem(19))) return -1; if (shade_function.set_arg(31,scene.texture_atlas.texture_mem(20))) return -1; if (shade_function.set_arg(32,scene.texture_atlas.texture_mem(21))) return -1; if (shade_function.set_arg(33,scene.texture_atlas.texture_mem(22))) return -1; if (shade_function.set_arg(34,scene.texture_atlas.texture_mem(23))) return -1; if (shade_function.set_arg(35,scene.texture_atlas.texture_mem(24))) return -1; if (shade_function.set_arg(36,scene.texture_atlas.texture_mem(25))) return -1; cl_uint sample_count = size; if (shade_function.set_arg(37,sizeof(cl_uint), &sample_count)) return -1; cl_int use_cubemap = cm.enabled; if (shade_function.set_arg(38,sizeof(cl_int), &use_cubemap)) return -1; if (shade_function.set_arg(39,scene.lights_mem())) return -1; size_t group_size = shade_function.max_group_size(); if (shade_function.enqueue_single_dim(size,group_size)) return -1; device.enqueue_barrier(); if (m_timing) { device.finish_commands(); m_time_ms = m_timer.msec_since_snap(); } return 0; }