For mapping I allocate a temporary buffer and on unmap, I call glTexImage2D to replace the storage.
#Opengl vs directx 11 update
For a simple update (where I use UpdateSubresource in Direct3D), I simply replace all image data using glTexSubImage2D. Uploading and downloading data is the next part. This allocates and fixes the texture storage, and only allows you to upload new data. These APIs allow you to resize a texture later on, which is something Direct3D doesn’t, and the same behaviour can be obtained in OpenGL using the glTexStorage2D function. Using glTexImage2D is however not the best way to define texture storage. The only problem here is to handle the internal format, format and type for OpenGL – I store them along with the texture, as they are all needed at some point. Currently I use glTexImage2D and glCompressedTexImage2D for each mip-map individually.
![opengl vs directx 11 opengl vs directx 11](https://i.ytimg.com/vi/YBIz7HVe1Xk/hqdefault.jpg)
With glMapBufferRange, you can mimic the Direct3D behaviour perfectly and with the same performance. Works basically the same in OpenGL as in Direct3D, just make sure to use glMapBufferRange and not glMapBuffer, which gives you better control over how the data is mapped, and makes it easy to guarantee that no synchronization happens. Create a new buffer using glGenBuffers, bind it to either vertex storage ( GL_ARRAY_BUFFER) or to index storage ( GL_ELEMENT_ARRAY_BUFFER) and populate it using glBufferData. I wrap every object type into a class, just like in Direct3D. In OpenGL, everything is an unsigned integer. That is, in OpenGL, a context is just use to bundle the state changing functions, while in Direct3D, it wraps the immediate device context. As using multiple device contexts is not beneficial for performance, my devices only expose the “immediate” context.
![opengl vs directx 11 opengl vs directx 11](http://media.redgamingtech.com/rgt-website/2018/08/deus-ex-1440p-1024x575.jpg)
The device handles all resource creation, and the device context handles all state changes. I also follow the Direct3D split of a device and a device context. For extensions, I use glLoadGen which is by far the easiest and safest way to load OpenGL extensions I have found. In OpenGL, I go through the usual hoops: That is, I create an invisible window, query the extension functions on that, and then finally go on to create an OpenGL context that suits me. If you find a mistake, please drop me a line so I can fix it! Device creation & rendering contexts The hardest part for me during porting is to find out which OpenGL API corresponds to a specific Direct3D API call, and here is a write-down of what I found out & implemented in my rendering engine.
![opengl vs directx 11 opengl vs directx 11](https://i.ytimg.com/vi/JkaBM4c0JBg/maxresdefault.jpg)
Welcome to my Direct3D to OpenGL mapping cheat-sheet, which will hopefully help you to get started with adding support for OpenGL to your renderer.