11-05-2019, 03:39 AM
It took a little doing, but I was able to get Tilengine running reasonably well with OpenGL GPU shaders. A big hurdle I ran across was dealing with the shaders themselves, and their restrictions regarding 8-bit graphics. As it turns out, a shader pipeline in OpenGL does NOT like 8-bit graphics, or anything formatted like 8-bit graphics. This proved to be a bit of a problem. For whatever reason, it treated the pixel-array output of Tilengine similarly, even though that output is not technically 8-bit. It would only accept graphics fully formatted as RGBA32, and was very particular about this.
Thankfully, I was already using SDL2 as my general framing library, and it has lots of tools for dealing with this sort of scenario. I created two SDL_Surface objects, formatted one of them as BGRA32, and pointed Tilengine at its pixel array for the actual pixel drawing. This turned out to work just fine, and be a viable container for storing the Tilengine-generated pixel data. I took the second SDL_Surface and formatted it as RGBA32. I then used a ConvertPixels function call to transfer the data from the Tilengine-rendered surface to the RGBA32 surface. Then I used the RGBA32 surface as the pixel source for updating the OpenGL Texture. This approach worked fine, and the OpenGL GPU shaders were able to use that data without issue.
The extra step was lamentable, and I would have preferred to copy the data over from Tilengine's output directly. The extra SDL_Surface tacks on a little extra used memory, and transferring the pixels over to a different format almost certainly eats into the performance. But the kinds of features that can be gained from utilizing GPU shaders are fairly significant, so I'm sticking with it for now.
Thankfully, I was already using SDL2 as my general framing library, and it has lots of tools for dealing with this sort of scenario. I created two SDL_Surface objects, formatted one of them as BGRA32, and pointed Tilengine at its pixel array for the actual pixel drawing. This turned out to work just fine, and be a viable container for storing the Tilengine-generated pixel data. I took the second SDL_Surface and formatted it as RGBA32. I then used a ConvertPixels function call to transfer the data from the Tilengine-rendered surface to the RGBA32 surface. Then I used the RGBA32 surface as the pixel source for updating the OpenGL Texture. This approach worked fine, and the OpenGL GPU shaders were able to use that data without issue.
The extra step was lamentable, and I would have preferred to copy the data over from Tilengine's output directly. The extra SDL_Surface tacks on a little extra used memory, and transferring the pixels over to a different format almost certainly eats into the performance. But the kinds of features that can be gained from utilizing GPU shaders are fairly significant, so I'm sticking with it for now.