How does a graphics driver programmatically communicate from CPU to GPU?

The video card is no more magical than any other item inside or outside the computer. How are you reading this web page, you have at least two computers communicating with each other over an interface (ethernet/internet). Memory, pcie (video, hard disk, usb, etc), usb (mouse, keyboard, etc), network, are all connected to the main cpu via interfaces that are ultimately tied to the cpu's external interface (memory bus for lack of a better term).

We like to think that many of these devices are pure logic and that seems to make sense that when you write to some address that is routed to that peripheral that address and data tells that blob of logic to perform some action. But just like reading this web page on one computer that is served up by some other computer, it doesnt have to be pure logic, you just need an interface to move data. Not only a gpu but a number of other things in your computer have processors inside, the network interfaces might, the mouse and keyboard probably do, hard disk most likely, and as we now know the battery packs on laptops have a processor with firmware as well (mac batteries being infected with a virus that re-infects the computer after a clean re-install).

No magic. The software/firmware running on these other processors in your computer are not unlike the logic, a percentage of their job is to wait for commands from the main cpu to do things. Mouse please give me the button status. Gpu please draw me a triangle.

There are no special assembly language instructions in the cpu for video. The cpu doesnt know a video card from a hole in the wall, the driver knows/determines the address space for the video card it knows how to talk to, through that address space it writes the data values required to tell the video card to do things. Draw me a triangle may involve several items of data, no doubt the three corner coordinates, perhaps a fill color or is it straight lines which is likely just part of the definition of the communication interface with that peripheral.

There is no magic here, if you wanted to ask someone to do something for you you would provide a list of things for them to do and communicate in a way they could understand (go to 123 main street, ring the doorbell, tell the person that answers you would like two tickets to tonights performance, give them this money, return with the change and the tickets), with hardware it is easier as the communication interface is well defined for that peripheral.


Like any hardware device on a PC the graphics card will respond to reads and writes to certain memory addresses, and possibly input/output ports. The PCI bus defines how these are allocated.

There are no specific CPU instructions to communicate with graphics cards, in the case of writing to memory locations it just uses the ordinary instructions to do that and in the case of port IO it just uses the generic instructions to do that. In both cases there will be some CPU setup needed to "map" the memory locations into virtual address space or to allow access to the ports.

A write to memory location 1234567 could be directed to the graphics card to indicate a command to it for example. (That's just a made up example of course) You certainly could write your own driver that did just that, however you'd have to know exactly what the card expected in order to different things, and that generally is a secret only known to the manufacturer and they implement it in their driver software. Some cards are better documented than others, and some have been partially reverse engineered.


Yeah, the memory address are mapped to each devices in the computer system. The basic thing is if you write to certain memory area that is mapped tot he device, the device gets written. for that the device has specific registers and other programmable spaces mapped to main memory. Using these registers you configure the devices like GPU for your case. And finally for huge data transfer obviously the GPU used a DMA transfer so it can work without intervention of the CPU once DMA transfer is initiated.

So bottom line is, To communicate with GPU or other hardware devices you need to study the system architecture where the GPU is installed because it is finally the system software receding in the system where GPU is installed responsible all kind of memory mapping and allocations.

Tags:

C++

C

Cpu

Assembly

Gpu