Can I render offscreen with EGL in Optix?

Hello,

I am running the examples of optix7course. In example 3, is it possible to change the GLFWindow to an offscreen EGL window? I don’t need real time rendering just images.

Thank you,

Rafael Scatena

 struct SampleWindow : public GLFWindow
  {
    SampleWindow(const std::string &title)
      : GLFWindow(title)
    {}
  
  virtual void render() override
  {
    sample.render();
  }
  
  virtual void draw() override
  {
    sample.downloadPixels(pixels.data());
    if (fbTexture == 0)
      glGenTextures(1, &fbTexture);
    
    glBindTexture(GL_TEXTURE_2D, fbTexture);
    GLenum texFormat = GL_RGBA;
    GLenum texelType = GL_UNSIGNED_BYTE;
    glTexImage2D(GL_TEXTURE_2D, 0, texFormat, fbSize.x, fbSize.y, 0, GL_RGBA,
                 texelType, pixels.data());

    glDisable(GL_LIGHTING);
    glColor3f(1, 1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, fbTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glDisable(GL_DEPTH_TEST);

    glViewport(0, 0, fbSize.x, fbSize.y);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.f, (float)fbSize.x, 0.f, (float)fbSize.y, -1.f, 1.f);

    glBegin(GL_QUADS);
    {
      glTexCoord2f(0.f, 0.f);
      glVertex3f(0.f, 0.f, 0.f);
    
      glTexCoord2f(0.f, 1.f);
      glVertex3f(0.f, (float)fbSize.y, 0.f);
    
      glTexCoord2f(1.f, 1.f);
      glVertex3f((float)fbSize.x, (float)fbSize.y, 0.f);
    
      glTexCoord2f(1.f, 0.f);
      glVertex3f((float)fbSize.x, 0.f, 0.f);
    }
    glEnd();
  }
  
  virtual void resize(const vec2i &newSize) 
  {
    fbSize = newSize;
    sample.resize(newSize);
    pixels.resize(newSize.x*newSize.y);
  }

  vec2i                 fbSize;
  GLuint                fbTexture {0};
  SampleRenderer        sample;
  std::vector<uint32_t> pixels;
};

OptiX doesn’t know anything about windowing systems or graphics APIs.
Everything related to input and output of data is managed by CUDA host calls inside your application.

If you look into the source code of that example and follow the sample.downloadPixels(pixels.data()); you’ll see that this is calling a function which is simply copying the data from a native CUDA buffer (the device pointer d_ptr) to the given host memory pointer at pixels.data().

What you do with that data afterwards is completely your choice.

In the given example code, that is uploaded to an OpenGL texture and rendered to the screen with a textured quad. (That code is the slowest but simplest way you can do this and it will work with any OpenGL implementation.)

If you don’t need or cannot use the GLFW window managing around these OpenGL calls but want to use EGL and OpenGL ES, you would need to either replace all that with your own EGL-based application framework or port all the OptiX code over to your application framework. In the end you would do a similar texture upload and display to your back buffer.

Maybe look at the OptiX SDK examples first. There is the optixConsole application which doesn’t even use any windowing system but prints the resulting image as ASCII characters. Many examples also simply write an image to disk when using the respective command line options.