About OpenGL drawing context

https://www.cnblogs.com/Liuwq/p/5444641.html

What is a rendering context (Rendering Context)

      If you are a beginner to learn OpenGL and open the Red Book, you will be told that OpenGL is a state machine. OpenGL adopts the client-server model. We can think that each hardware GPU is a server, each drawing context corresponds to a client of the application, and a client maintains a set of state machines. If two windows correspond to two different drawing contexts, the two windows are mutually exclusive. State independent. Applying for a drawing context means applying for system resources. Each drawing context still requires a lot of resources. I remember that I have tried to continuously load the WebGL page on the TAB page of chrome. Since WebGL uses OpenGL ES, the drawing context is also required. When When loading more than 30 pages, Chrome completely crashes.

     All OpenGL calls need to specify the context in which they are called. In different contexts, the same resource ID may correspond to different types of resources in their respective contexts.

     Different operating systems have their own APIs for creating and setting the current drawing context.

     How to create a drawing context

     We generally use GLUT for demo programs, so many people may not have created a drawing context by themselves. In addition to creating a window, the GLUT function GlutCreateWindow also creates a drawing context, and sets the created drawing context as the current drawing context. For the Windows platform, first create a device context (Device Context, DC), with DC as input, you can create a drawing context. After creating the drawing context, call MakeCurrent to set the created context as the current drawing context.

     After the drawing context is created and set as the current context, the latest features of OpenGL cannot be used, and API calls after OpenGL 1.1 will still crash. Generally, we use the Glew library and call glewInit() to get the function entry address of the new features released by OpenGL with the graphics card driver.

    Drawing contexts and threads

   Two threads MakeCurrent to the same drawing context at the same time will cause the program to crash. The general practice of large programs is to apply for a thread dedicated to drawing. When creating a thread, apply for a drawing context for the drawing thread, which is always used as the current context. All drawing-related operations are done on the drawing thread.

   resource sharing between contexts

   One context per window, the advantage is that the state machines can be guaranteed not to affect each other. But multiple windows need to use the same texture, how to avoid repeated resource application? The answer is that graphics resources between contexts can be shared. First create context A, then use A as input to create context B, then B can access the texture resources created under A context. Resources such as textures, shaders, and Buffers can be shared, but container objects such as Frame Buffer Object (FBO) and Vertex Array Object (VAO) cannot be shared, but shared textures and VBOs can be bound to container objects in their respective contexts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324592959&siteId=291194637