Ejemplo n.º 1
0
    // internal initialization. performed when run is invoked
    void Raspi_App::init()
    {
        gettimeofday(&m_startTime, NULL);

        esInitContext(m_context.get());
        esCreateWindow(m_context.get(), name().c_str(),
                       ES_WINDOW_RGB | ES_WINDOW_ALPHA | ES_WINDOW_DEPTH /*| ES_WINDOW_MULTISAMPLE*/);

        // set graphical log stream
        Logger::get()->add_outstream(&m_outstream_gl);

        // version
        LOG_INFO<<"OpenGL: " << glGetString(GL_VERSION);
        LOG_INFO<<"GLSL: " << glGetString(GL_SHADING_LANGUAGE_VERSION);

        set_window_size(gl::vec2(m_context->width, m_context->height));
        LOG_INFO<<"Context: " << gl::window_dimension().x << " x " << gl::window_dimension().y;

        // file search paths
        if(!args().empty()){ fs::add_search_path(fs::get_directory_part(args().front())); }
        // fs::add_search_path("./");
        fs::add_search_path("./res");
        fs::add_search_path("/usr/local/share/fonts");

        get_input_file_descriptors(&m_mouse_fd, &m_keyboard_fd, &m_touch_fd);

        m_timer_device_scan = Timer(background_queue().io_service(), [this]()
        {
            int mouse_fd = m_mouse_fd, keyboard_fd = m_keyboard_fd;
            int *mp = nullptr, *kp = nullptr;
            bool has_changed = false;

            // check for keyboard and mouse being added/removed
            auto mouse_handler = find_mouse_handler();
            auto kb_handler = find_keyboard_handler();

            if((!m_keyboard_fd && !kb_handler.empty()) ||
               (m_keyboard_fd && kb_handler.empty()))
            {
                keyboard_fd = 0;
                kp = &keyboard_fd;
                has_changed = true;
            }
            if((!m_mouse_fd && !mouse_handler.empty()) ||
               (m_mouse_fd && mouse_handler.empty()))
            {
                mouse_fd = 0;
                mp = &mouse_fd;
                has_changed = true;
            }
            if(!has_changed){ return; }

            get_input_file_descriptors(mp, kp, nullptr);
            main_queue().submit([this, mouse_fd, keyboard_fd]
            {
                if(mouse_fd && !m_mouse_fd)
                {
                    LOG_TRACE << "mouse connected";
                    m_mouse_fd = mouse_fd;
                }
                else if(!mouse_fd && m_mouse_fd)
                {
                     LOG_TRACE << "mouse disconnected";
                     close(m_mouse_fd); m_mouse_fd = 0;
                }
                if(keyboard_fd && !m_keyboard_fd)
                {
                    LOG_TRACE << "keyboard connected";
                    m_keyboard_fd = keyboard_fd;
                }
                else if(!keyboard_fd && m_keyboard_fd)
                {
                    LOG_TRACE << "keyboard disconnected";
                    close(m_keyboard_fd); m_keyboard_fd = 0;
                }
            });
        });
        m_timer_device_scan.set_periodic();
        m_timer_device_scan.expires_from_now(5.0);

        // make sure touchscreen backlight stays on
        // TODO: use timer here
        // set_lcd_backlight(true);

        // center cursor
        current_mouse_pos = gl::window_dimension() / 2.f;

        // user setup hook
        setup();
    }
Ejemplo n.º 2
0
int
main()
try
{
	sge::opencl::system opencl_system;

	fcppt::io::cout()
		<< FCPPT_TEXT("Querying the number of available platforms...\n");

	sge::opencl::platform::object_sequence &platforms(
		opencl_system.platforms());

	if(platforms.empty())
	{
		fcppt::io::cerr()
			<< FCPPT_TEXT("Couldn't find any OpenCL platforms on your system.\n");
		return EXIT_FAILURE;
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Number of OpenCL platforms: ") << platforms.size() << FCPPT_TEXT("\n")
		<< FCPPT_TEXT("Platform listing begin:\n")
		<< FCPPT_TEXT("-----------------------\n");

	sge::opencl::platform::object_sequence::size_type platform_index = 0;
	for(
		sge::opencl::platform::object const &current_platform
		:
		platforms
	)
	{
		fcppt::io::cout()
			<< FCPPT_TEXT("\tPlatform ")
			<< platform_index++
			<< FCPPT_TEXT(":\n")
			<< FCPPT_TEXT("\tName: ")
			<< fcppt::from_std_string(
				current_platform.name()
			)
			<< FCPPT_TEXT("\n")
			<< FCPPT_TEXT("\tVendor: ")
			<< fcppt::from_std_string(
				current_platform.vendor()
			)
			<< FCPPT_TEXT("\n")
			<< FCPPT_TEXT("Profile type: ")
			<<
				(current_platform.profile() == sge::opencl::platform::profile_type::full
				?
					fcppt::string(FCPPT_TEXT("full"))
				:
					fcppt::string(FCPPT_TEXT("embedded")))
			<< FCPPT_TEXT("\n")
			<< FCPPT_TEXT("\tVersion: ")
			<< current_platform.version().major_part()
			<< FCPPT_TEXT(".")
			<< current_platform.version().minor_part()
			<< FCPPT_TEXT("\n");

		if(!current_platform.version().platform_specific().empty())
			fcppt::io::cout()
				<< FCPPT_TEXT("\tPlatform specific version info: ")
				<< fcppt::from_std_string(current_platform.version().platform_specific())
				<< FCPPT_TEXT("\n");

		fcppt::io::cout()
			<< FCPPT_TEXT("\tExtension list begin:\n")
			<< FCPPT_TEXT("\t*********************\n")
			<< FCPPT_TEXT("\t\t")
			<<
				fcppt::from_std_string(
					boost::algorithm::join(
						current_platform.extensions(),
						std::string("\n\t\t")))
			<< FCPPT_TEXT("\r\t*********************\n")
			<< FCPPT_TEXT("-----------------------\n");
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Platform listing end\n");

	sge::opencl::platform::object_sequence::size_type chosen_platform_index;
	if(platforms.size() == 1)
	{
		chosen_platform_index = 0;
	}
	else
	{
		fcppt::io::cout()
			<< FCPPT_TEXT("Your choice: ");
		do
			chosen_platform_index =
				query_value_from_user<sge::opencl::platform::object_sequence::size_type>(
					fcppt::io::cin());
		while(chosen_platform_index >= platforms.size());
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("List devices with properties? [y/n] ");

	fcppt::char_type list_devices;
	do
		list_devices =
			query_value_from_user<fcppt::char_type>(
				fcppt::io::cin());
	while(list_devices != FCPPT_TEXT('y') && list_devices != FCPPT_TEXT('n'));

	sge::opencl::platform::object &chosen_platform =
		platforms[chosen_platform_index];

	if(list_devices == FCPPT_TEXT('y'))
	{
		fcppt::io::cout()
			<< FCPPT_TEXT("Number of devices on this platform: ")
			<< chosen_platform.devices().size()
			<< FCPPT_TEXT('\n')
			<< FCPPT_TEXT("Device listing begin:\n")
			<< FCPPT_TEXT("-----------------------\n");

		for(
			sge::opencl::device::object const &current_device
			:
			chosen_platform.devices()
		)
		{
			current_device.output_info(
				std::cout);
			fcppt::io::cout()
				<< FCPPT_TEXT("-----------------------\n");
		}

		fcppt::io::cout()
			<< FCPPT_TEXT("-----------------------\n")
			<< FCPPT_TEXT("Device listing end\n");
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Creating sge::systems object...\n");

	sge::window::dim const window_dim{
		1024u,
		768u
	};

	sge::systems::instance<
		brigand::list<
			sge::systems::with_window,
			sge::systems::with_renderer<
				sge::systems::renderer_caps::core
			>
		>
	> const sys(
		sge::systems::make_list
		(
			// FIXME: Move this higher?
			sge::systems::config()
			.log_settings(
				sge::systems::log_settings{
					sge::log::option_container{
						sge::log::option{
							sge::opencl::log_location(),
							fcppt::log::level::verbose
						}
					}
				}
			)
		)
		(
			sge::systems::window(
				sge::systems::window_source(
					sge::systems::original_window(
						sge::window::title(
							FCPPT_TEXT("Simple OpenCL example")
						)
					)
				)
			)
			.dont_show()
		)
		(
			sge::systems::renderer(
				sge::renderer::pixel_format::object(
					sge::renderer::pixel_format::color::depth32,
					sge::renderer::pixel_format::depth_stencil::off,
					sge::renderer::pixel_format::optional_multi_samples(),
					sge::renderer::pixel_format::srgb::no
				),
				sge::renderer::display_mode::parameters(
					sge::renderer::display_mode::vsync::on,
					sge::renderer::display_mode::optional_object()
				),
				sge::viewport::optional_resize_callback{
					sge::viewport::center_on_resize(
						window_dim
					)
				}
			)
		)
	);

	fcppt::io::cout()
		<< FCPPT_TEXT("Done. Creating a context with all devices on this platform...\n");

	sge::opencl::device::object_ref_sequence const device_refs(
		fcppt::algorithm::map<
			sge::opencl::device::object_ref_sequence
		>(
			chosen_platform.devices(),
			[](
				sge::opencl::device::object &_device
			)
			{
				return
					fcppt::make_ref(
						_device
					);
			}
		)
	);

	sge::opencl::context::object main_context(
		sge::opencl::context::parameters(
			chosen_platform,
			device_refs
		)
		.share_with(
			sys.renderer_device_core())
		.error_callback(
			sge::opencl::context::error_callback{
				&opencl_error_callback
			}
		)
	);

	fcppt::io::cout()
		<< FCPPT_TEXT("Context created, listing available planar image formats (read/write)\n");

	sge::opencl::memory_object::image::format_sequence const planar_image_formats =
		main_context.supported_planar_image_formats(
			CL_MEM_READ_WRITE);

	for(
		auto const format
		:
		planar_image_formats
	)
	{
		sge::opencl::memory_object::image::format_output(
			std::cout,
			format
		);

		std::cout << '\n';
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Listing available volume image formats (read/write)...\n");

	sge::opencl::memory_object::image::format_sequence const volume_image_formats =
		main_context.supported_volume_image_formats(
			CL_MEM_READ_WRITE);

	for(
		auto const format
		:
		volume_image_formats
	)
	{
		sge::opencl::memory_object::image::format_output(
			std::cout,
			format
		);

		std::cout << '\n';
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Done, now creating a program...");

	sge::opencl::program::object main_program(
		sys.log_context(),
		main_context,
		sge::opencl::program::source_string_sequence{
			std::string(
				"__kernel void hello_kernel("
				"float const multiplier,"
				"__global float *input)"
				"{"
				"int gid = get_global_id(0);"
				"int lid = get_local_id(0);"
				"input[gid] = lid * multiplier;"
				"}"
			)
		},
		sge::opencl::program::optional_build_parameters());

	fcppt::io::cout()
		<< FCPPT_TEXT("Program created, building the program...\n");

	volatile bool build_finished =
		false;

	main_program.build(
		sge::opencl::program::build_parameters()
		.notification_callback(
			sge::opencl::program::notification_callback{
				std::bind(
					&program_build_finished,
					std::ref(
						build_finished
					)
				)
			}
		)
	);

	std::cout << "Waiting for build completion\n";
	while(!build_finished)
		std::cout << "Build not finished yet\n";

	fcppt::io::cout()
		<< FCPPT_TEXT("Program built, now creating a kernel...\n");

	sge::opencl::kernel::object main_kernel(
		main_program,
		sge::opencl::kernel::name(
			"hello_kernel"));

	fcppt::io::cout()
		<< FCPPT_TEXT("Kernel created, now creating a vertex buffer...\n");

	sge::renderer::vertex::declaration_unique_ptr const vertex_declaration(
		sys.renderer_device_core().create_vertex_declaration(
			sge::renderer::vertex::declaration_parameters(
				sge::renderer::vf::dynamic::make_format<vf::format>())));

	sge::renderer::vertex::buffer_unique_ptr const vb(
		sys.renderer_device_core().create_vertex_buffer(
			sge::renderer::vertex::buffer_parameters(
				*vertex_declaration,
				sge::renderer::vf::dynamic::make_part_index<
					vf::format,
					vf::part
				>(),
				sge::renderer::vertex::count(
					6u),
				sge::renderer::resource_flags_field{
					sge::renderer::resource_flags::readable})));

	fcppt::io::cout()
		<< FCPPT_TEXT("Done, now creating OpenCL buffer from it\n");

	sge::opencl::memory_object::buffer cl_vb(
		main_context,
		*vb,
		sge::opencl::memory_object::renderer_buffer_lock_mode::write_only);

	main_kernel.argument(
		sge::opencl::kernel::argument_index(
			1u),
		cl_vb);

	main_kernel.argument(
		sge::opencl::kernel::argument_index(
			0u),
		sge::opencl::kernel::numeric_type(
			static_cast<cl_float>(
				2.0
			)
		)
	);

	fcppt::io::cout()
		<< FCPPT_TEXT("Done, now creating a command queue\n");

	sge::opencl::command_queue::object main_queue(
		device_refs[0].get(),
		main_context,
		sge::opencl::command_queue::execution_mode::out_of_order,
		sge::opencl::command_queue::profiling_mode::disabled);

	fcppt::io::cout()
		<< FCPPT_TEXT("Done, now enqueueing kernel and running it\n");

	{
		sge::opencl::memory_object::scoped_objects scoped_vb(
			main_queue,
			sge::opencl::memory_object::base_ref_sequence{
				fcppt::reference_to_base<
					sge::opencl::memory_object::base
				>(
					fcppt::make_ref(
						cl_vb
					)
				)
			}
		);

		sge::opencl::command_queue::enqueue_kernel(
			main_queue,
			main_kernel,
			sge::opencl::command_queue::global_dim1(
				sge::opencl::dim1(
					vb->linear_size()
					* 2u
				)
			),
			sge::opencl::command_queue::local_dim1(
				sge::opencl::dim1(
					2u
				)
			),
			sge::opencl::event::sequence()
		);
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Now locking the vb for reading and printing the values\n");

	{
		sge::renderer::vertex::scoped_lock const scoped_vb(
			*vb,
			sge::renderer::lock_mode::readwrite);

		typedef
		sge::renderer::vf::view<vf::part>
		vertex_view;

		vertex_view const vertices(
			scoped_vb.value());

		for(
			auto const vertex
			:
			vertices
		)
		{
			fcppt::io::cout()
				<< vertex.get<vf::scalar_quantity>()
				<< FCPPT_TEXT('\n');
		}
	}

	fcppt::io::cout()
		<< FCPPT_TEXT("Done\n");
}
catch(
	fcppt::exception const &_error
)
{
	fcppt::io::cerr()
		<< _error.string()
		<< FCPPT_TEXT('\n');

	return EXIT_FAILURE;
}
catch(
	std::exception const &_error
)
{
	std::cerr
		<< _error.what()
		<< '\n';

	return EXIT_FAILURE;
}