How interfacing with graphics card work with C or C++?

前端 未结 2 1958
遇见更好的自我
遇见更好的自我 2020-12-12 17:38

Libraries such as OpenGL access the graphics card and can produce graphics programs, how does these libraries access the graphics card since they are implemented using C. Ac

2条回答
  •  春和景丽
    2020-12-12 18:07

    If I am not mistaken, you question boils down to how does one use C to access hardware. The answer, in a very general and hand wavy fashion, is that peripheral hardware is often mapped into the address space and accessed as if it is normal memory with the caveat that it can change unexpectedly.

    C does a great job of accessing memory so it is totally suitable for reading and writing directly with hardware. The volatile keyword is there to explicitly stop the compiler from taking short cuts and force querying the addresses in question. This is because memory mapped IO addresses can change unexpectedly and do not behave like normal RAM.

    On a simple system this low level memory access to set addresses will be abstracted into a more easily usable library. On modern operating systems, the kernel must mediate access to shared resources like a graphics card. This means that the kernel will implement the memory mapped IO but also put in place a series of system calls (often using Swiss Army Knife constructs like ioctl) so that a user process can gain access to the peripheral. This in turn will be wrapped in a nice user-friendly library like OpenGL.

提交回复
热议问题