Home > Software design >  Is there a way to make opengl use the DGPU insted of of the IGPU?
Is there a way to make opengl use the DGPU insted of of the IGPU?

Time:05-15

I am trying to learn opengl in C, this the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
    glewExperimental = true;

    if ( !glfwInit )
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return 1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL

    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
    if( window == NULL )
    {
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) 
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do
    {
        // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
        glClear( GL_COLOR_BUFFER_BIT );

        // Draw nothing, see you in tutorial 2 !

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } 
    // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 
        glfwWindowShouldClose(window) == 0 );

}

I compile the code using this command:

gcc app.c -l GL -l glfw3 -l GLEW -ldl -lpthread -lm

When I run the produced binary file, I get this output:

Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.

I would like to run the code using the DGPU (nvidia gtx 1650) instead of the IGPU. I am using Linux and the Nvidia GPU is the one in charge right now.

Thanks in advance.

CodePudding user response:

You have two options for choosing a specific GPU for OpenGL.

  1. First one is to setup a separate X server, or X screen. So that, each GPU has its own server/screen. From that on, you run your program on a GPU setup you want.

  2. If your system supports so called Optimus technology, you can create an EGL context for specific GPU. No separate screens/servers. Your EGL implementation must support few extensions to select a GPU and create context within an X server.

Both ways you need to link to proper libraries. EGL, OpenGL, GLU, OpenCL and so on. The libraries is different for Intel and NVIDIA. Something like -L path/to/specific/libs -lGL -lEGL.

  • Related