/* Init CAL, check device capabilities, and prepare new kernel from workunit parameters */
CALresult separationCALInit(MWCALInfo* ci, const CLRequest* clr)
{
    CALresult err;

    err = calInit();
    if (err != CAL_RESULT_OK)
    {
        cal_warn("Failed to init CAL", err);
        return err;
    }

    err = calExtSupported(0x8009);
    if (err != CAL_RESULT_OK)
    {
        cal_warn("calCtxWaitForEvents not supported\n", err);
        return err;
    }

    err = calExtGetProc((CALextproc*) &mw_calCtxWaitForEvents, 0x8009, "calCtxWaitForEvents");
    if (err != CAL_RESULT_OK)
    {
        cal_warn("Error getting calCtxWaitForEvents", err);
        return err;
    }

    err = mwGetCALInfo(ci, clr->devNum);
    if (err != CAL_RESULT_OK)
    {
        cal_warn("Failed to get CAL info", err);
        calShutdown();
        return err;
    }

    printCALInfo(ci);

    if (!checkDeviceCapabilities(&ci->devAttribs))
    {
        warn("Device failed capability check\n");
        mwCALShutdown(ci);
        return CAL_RESULT_ERROR;
    }

    return CAL_RESULT_OK;
}
Beispiel #2
0
int Application::run(Application* self)
{
	m_self = self;

	if (!framework::Utils::exists("data/gui"))
	{
		Logger::toLog("Error: could not find gui directory. Probably working directory has not been set correctly (especially if you are running from IDE).\n");
		return EXIT_FAILURE;
	}

	init();
	m_isRunning = true;

	glfwSetErrorCallback(&Application::errorCallback);

	if (!glfwInit())
	{
		Logger::toLog("Error: glfwInit failed");
		return EXIT_FAILURE;
	}

	glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

	glfwWindowHint(GLFW_RED_BITS, 8);
	glfwWindowHint(GLFW_GREEN_BITS, 8);
	glfwWindowHint(GLFW_BLUE_BITS, 8);
	glfwWindowHint(GLFW_ALPHA_BITS, 0);
	glfwWindowHint(GLFW_DEPTH_BITS, 32);
	glfwWindowHint(GLFW_STENCIL_BITS, 0);

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, m_info.majorVersion);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, m_info.minorVersion);
	#ifdef _DEBUG
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
	#endif
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_SAMPLES, m_info.samples);
    glfwWindowHint(GLFW_STEREO, m_info.flags.stereo ? GL_TRUE : GL_FALSE);

	// create window
	m_window = glfwCreateWindow(m_info.windowWidth, m_info.windowHeight, 
								m_info.title.c_str(), 
								m_info.flags.fullscreen ? glfwGetPrimaryMonitor() : NULL, 
								NULL);
	if (!m_window)
    {
        glfwTerminate();
        return EXIT_FAILURE;
    }

	glfwSetWindowSizeCallback(m_window, &Application::_setWindowSize);
    glfwSetKeyCallback(m_window, &Application::_onKey);
	glfwSetCharCallback(m_window, &Application::_onChar);
    glfwSetMouseButtonCallback(m_window, &Application::_onMouse);
    glfwSetCursorPosCallback(m_window, &Application::_onCursor);
    glfwSetScrollCallback(m_window, &Application::_onScroll);
	glfwSetInputMode(m_window, GLFW_CURSOR, m_info.flags.cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN);

	// center position
	GLFWmonitor* primary = glfwGetPrimaryMonitor();
	const GLFWvidmode* mode = glfwGetVideoMode(primary);
	int posx = (mode->width - m_info.windowWidth) >> 1;
	int posy = (mode->height - m_info.windowHeight) >> 1;
	glfwSetWindowPos(m_window, posx, posy);

	// set vsync
	glfwMakeContextCurrent(m_window);
	glfwSwapInterval((int)m_info.flags.vsync);

	// init GL3w
	gl3wInit();

	std::vector<int> multisamplingLevels;
	if (!checkOpenGLVersion() || !checkDeviceCapabilities(multisamplingLevels))
	{
		glfwDestroyWindow(m_window);
		glfwTerminate();
		return EXIT_FAILURE;
	}

	#ifdef _DEBUG
	Logger::toLogWithFormat("Video adapter: %s - %s, OpenGL: %s\n", (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER), (const char*)glGetString(GL_VERSION));
	#endif

    if (m_info.flags.debug)
    {
        if (gl3wIsSupported(4, 3))
        {
            glDebugMessageCallback(debugCallback, this);
            glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
        }
        else if (IsExtensionSupported("GL_ARB_debug_output"))
        {
            glDebugMessageCallbackARB(debugCallback, this);
            glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
        }
    }

	initGui();

	if (!StandardGpuPrograms::init())
	{
		glfwDestroyWindow(m_window);
		glfwTerminate();
		return EXIT_FAILURE;
	}
	initAxes();
	startup(m_rootWindow);

	do
    {
		glfwMakeContextCurrent(m_window);
		Texture::beginFrame();
		if (fabs(m_lastTime) < 1e-7)
		{
			render(0);
			Texture::endFrame();
			renderGui(0);

			m_lastTime = glfwGetTime();
		}
		else
		{
			double curTime = glfwGetTime();
			double delta = curTime - m_lastTime;
				
			// fps counter
			measureFps(delta);

			// rendering
			render(delta);
			Texture::endFrame();
			renderGui(delta);

			m_lastTime = curTime;
		}

        glfwSwapBuffers(m_window);

		glfwPollEvents();
			
		if (glfwWindowShouldClose(m_window))
		{
            m_isRunning = GL_FALSE;
		}
		m_isRunning &= (glfwGetKey(m_window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
    } 
	while(m_isRunning);

	shutdown();
	destroyAllDestroyable();
	destroyGui();

	glfwDestroyWindow(m_window);
	glfwTerminate();
	return EXIT_SUCCESS;
}