BMW wrote:Would it be possible to port Direct3D to my OS?
I could port OpenGL, then make all the D3D functions as wrappers that call corresponding OpenGL functions.
Now that would annoy Microsoft.
Yes.. Kind of..
Porting Direct3D to OpenGL:
It is possible. It's not as simple as simply wrapping functions though. 10 years ago, most Direct3D applications used what was called the Fixed Function Pipeline - you pass in textures, vertices, indices, configure the operators and lighting - call Draw() and away you go. Wrapping these Direct3D calls with OpenGL is trivial.
However, Direct3D 8.1 (2001) introduced shaders. Shaders are small programs that are executed on the GPU. At first, they were limited to pixel shaders (executed per pixel) and vertex shaders (executed per vertex) - you could run calculations on the GPU much faster than the CPU, and customize it with your own lighting, blending, or transformation algorithms. This is know as the Programmable Pipeline.
Shaders were optional in Direct3D 8.x and 9.x - there was still the Fixed Function Pipeline to fall back up, but within 5 years of their introduction they became near universal in most graphics applications, because of the flexibility and speed they provided. Direct3D 10 (2006) dropped support for the Fixed Function Pipeline completely. Everything was done in shaders. Over the years, new shader types have been introduced (geometry shaders, multiple types of tessellation shaders, computation shaders.)
Most of these shaders are written in a C-style language (HLSL for Direct3D and GLSL for OpenGL) and are compiled down to byte code representation that's specific to the API, and uploaded to the GPU (does the GPU execute this directly or the driver JITs it? I don't know.)
The problem is that Direct3D's shader byte code is incompatible with the byte code used by OpenGL. Many Direct3D to OpenGL wrappers either don't support the Programmable Pipeline/shaders - hence they allow you to play games from the early 2000s but fail with modern titles, or they spend a lot of time working on code that translates the bytecode from one representation to another. It's a large feat.
Emulating Direct3D
It's fully possible to emulate Direct3D. There are many drop in "d3d9.dll" solutions that will software emulate Direct3D. You still have the shader problem - some emulate the shader bytecode, commercial solutions may attempt to JIT the bytecode to machine language.
If you do want to write your own software wrapper for Direct3D I recommend you emulate Direct3D 10. Why? It's very barebones. They stripped all of the legacy fixed function stuff right out.. There is no need to deal with legacy lighting, blending, animation, transformation code out. It's all done in shaders now. For you, that means you just need to write a bytecode interpreter, some trivial code to load different types of data structures, and away you go.