Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Adding ContextSettings to RenderTexture  (Read 5852 times)

0 Members and 1 Guest are viewing this topic.

SGauvin

  • Newbie
  • *
  • Posts: 4
    • View Profile
Adding ContextSettings to RenderTexture
« on: June 22, 2017, 01:28:09 am »
I am using a sf::RenderTexture in my game. I draw everything on it before rendering the RenderTexture's sprite on a window.
I am doing this because simply rendering on the window created unwanted black lines, discussed here: https://en.sfml-dev.org/forums/index.php?topic=21694
This means that I can no longer have antialiasing enabled when drawing sprites to a RenderTexture.
The only topics I found talking about this are pretty old, and wanted to know if this is going to be a feature in future SFML versions.
I know I can use a shader as a workaround, but I think having it implemented inside SFML would be pretty useful  :)

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Adding ContextSettings to RenderTexture
« Reply #1 on: June 22, 2017, 10:12:14 pm »
It isn't as simple as adding a ContextSettings to RenderTexture. The context created by the render texture is only needed to safely move the internal FBO object between multiple threads, which would not be possible re-using another context. ContextSettings only controls the settings that are used when creating the rendering surface of the context itself, not the FBO or its attachments. In the case of sf::Context, and therefore sf::RenderTexture, the internal framebuffer that belongs to the context is created with 0 width and 0 height, meaning you can't render to it at all. This is OK because sf::Context is not and should never be used as a target for rendering to in SFML. Any glDraw* commands issued when its default framebuffer is active will unnecessarily occupy the rendering pipeline since the output is essentially discarded anyway.

In order to get "real" RenderTexture multisampling, we would have to support creating texture images with multisampling support via glTexImage2DMultisample. This relies on hardware support that has realistically only been around since OpenGL 3.x class hardware.

Considering that there won't be many people making use of such a feature and the possibly troubling hardware support, it doesn't feel like it is worth it for SFML to start supporting.

Considering that what you are doing now is primarily a workaround anyway, you should probably try to solve the problem in a cleaner way. Rendering to a RenderTarget only to render it to the window again, both times at the full resolution of the window, wastes a considerable amount of GPU resources both in terms of processing time and memory usage. There have been enough people who have had similar problems with misaligned textures in the past, and as far as I can tell they always managed to find a solution.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

SGauvin

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding ContextSettings to RenderTexture
« Reply #2 on: June 23, 2017, 06:53:47 am »
I understand it isn't that simple, but if for example I want to draw a minimap on the corner of the screen, the only way I see how to do it is using a RenderTexture. So here I wouldn't be able to have antialiasing on my minimap. How should I do this?

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Adding ContextSettings to RenderTexture
« Reply #3 on: June 23, 2017, 02:04:35 pm »
I think you are overestimating what multisampling is meant to achieve. As the term anti-aliasing kind of gives away, it is a technique that is meant to counter aliasing. If you don't suffer from this "problem" then there is no need to employ any form of AA, be it MSAA or SSAA. As long as you are rendering rectilinear polygons that are not rotated by non-right angles and assuming that their texture contents don't exhibit aliasing, then this problem simply cannot occur. I assume that your minimap, as is the case for most minimaps, will also be a rectangular region somewhere on the screen, meaning it will not benefit from AA at all.

Following from what I said, AA is only required when rendered polygons don't line up with the target pixel grid, thus necessitating sampling multiple pixel locations in order to determine the final framebuffer value. In order to get into a scenario where AA in RenderTargets might be necessary, one would have to render multiple rotated polygons into a RenderTexture, perform some post-processing and then finally render the output to the window framebuffer. It is important that the drawn primitives themselves contain unaligned edges. If you were just drawing sprites that rely on an alpha-blended texture, AA wouldn't help you much either. In this case, it would be smarter to pre-process the image data offline to smooth the edges. SFML also lets you get texture "smoothing" by choosing whether to employ linear magnification and minification filters to the texture data, but this is a completely different problem from aliasing.

RenderTextures should really only be used when any form of intermediate compositing or post-processing is necessary. I've seen enough cases where people have misused them where a simple viewport/clipping would have sufficed. The problem is that SFML doesn't adequately document the cost of these graphics resources, so beginners are inclined to believe everything is just as "cheap" as everything else. RenderTextures are quite a heavy resource because it among other things requires creating a new context. Unless you absolutely need to use them and there is no other alternative, I would always opt to use the cheaper solution.

Take a look at the sf::View tutorial, it even explicitly explains (with pictures) how one would use them to create a minimap, all without a single RenderTexture.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

I_HATE_FIDGET_SPINNERS

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding ContextSettings to RenderTexture
« Reply #4 on: June 25, 2017, 02:29:05 am »
In order to get "real" RenderTexture multisampling, we would have to support creating texture images with multisampling support via glTexImage2DMultisample. This relies on hardware support that has realistically only been around since OpenGL 3.x class hardware.

Considering that there won't be many people making use of such a feature and the possibly troubling hardware support, it doesn't feel like it is worth it for SFML to start supporting.

How many people are running SFML on pre-OpenGL 3.x hardware? I can't imagine such ancient hardware being able to handle such a slow work-around.

 

anything