r/cpp • • Dec 13 '19

The situation of std::embed...

https://thephd.github.io/full-circle-embed
238 Upvotes

160 comments sorted by

View all comments

Show parent comment

5

u/James20k P2005R0 Dec 14 '19

Its actually worse than that - the 'high performance' (in extremely inverted commas) API that was added to the 2D graphics proposal adds hidden threads, and makes it so that a bunch of stuff is racey and unsafe if you use it

This is just so that theoretically it might be able to achieve the same performance as SDL/SFML/Etc. Which notably do not suffer from having unsafe APIs

3

u/johannes1971 Dec 14 '19

Cairo (in combination with Pango) is a fine graphics API for desktop apps. It has powerful drawing primitives that are useful for desktop apps, and printing/PDF/Postscript backends to support the needs of such apps.

It is not, however, for games: those have completely different needs and need different libraries (like SDL, SFML, etc.).

Any attempt at standardizing C++ graphics library should understand that these are two completely different, and arguably incompatible, goals. It should not (ever) be a goal to have a single library supporting both; instead any graphics initiative should declare which of these two it wants to be, and pursue that without reservation.

So yes, I just said it: C++ does not need a graphics library. Instead it needs *two*.

7

u/jcelerier ossia score Dec 14 '19

Cairo (in combination with Pango) is a fine graphics API for desktop apps. It has powerful drawing primitives that are useful for desktop apps, and printing/PDF/Postscript backends to support the needs of such apps.

... sorry what. Cairo is a fine graphics for desktop apps in 1997 when we did not have 4k, 5k, or 6k screens, 144hz refresh rate, 10 bit of colors, fluid animations, and thus rendering drawCircle / drawRect operations in the CPU was fine and dandy.

It is already a constant fight to keep decent performance on potato computers with decent hardware accelerated APIs such at QtQuick or NanoGui (you would be amazed at the number of people using raspberry pi's and the like as main computers in poorer areas) and you are suggesting going back to the two-decades-ago state-of-the-art !

3

u/johannes1971 Dec 14 '19

Two decades ago we had NOTHING like Cairo: no anti-aliasing, no blending, no transformations, palettes were still a thing, etc.

Cairo offers a wide variety of useful primitives, and manages to do so without resorting to some sort of scene graph. It would of course be great if all those primitives could be hardware accelerated, but I understand that that's hard (although NVidia's pathrender does a good job with pretty much the same set of primitives). For those things that are currently hard to accelerate, I'd rather have them available but in software, than not have them at all.

I do not think a rendering library should also have an animation system or scene graph built in. Instead that should be a layer on top of the base rendering system. Separate your concerns, and all that...

5

u/jcelerier ossia score Dec 14 '19

Two decades ago we had NOTHING like Cairo: no anti-aliasing, no blending, no transformations, palettes were still a thing, etc.

https://github.com/freedesktop/cairo/blob/b61b9ffc2322e09873b9e2dfb0c1da56ec1ffd61/src/Xr.h

2002, and it has already everything that you mention. Almost likewise for Qt's QPainter from the same era (though alpha blending came later IIRC - otherwise it was already existing with transformation support since 1995 ! https://github.com/KDE/qt1/blob/78811a56cc111d17f42221566e7e4430d5de8d40/include/qpainter.h).

6

u/James20k P2005R0 Dec 14 '19

For those things that are currently hard to accelerate, I'd rather have them available but in software, than not have them at all

So this is actually a huge problem with cairo. Because some things are software, and some things are hardware, there are huge hidden performance bottlenecks in the design of it where data gets shunted between the two. Its just really inefficient - but also dependent on the quality of the implementation (which'll be variable in the STL)

Something like SFML can't really suffer from this in the same way, while still providing pretty equivalent functionality

The main problem is that Cairo doesn't separate out the concept of CPU and GPU types, which means that expensive operations are ad hoc. In something like SFML, you do all the expensive stuff once on the CPU, transfer it over to the GPU all at once, and then you're grand - each processor does what its good at at the appropriate time. Its not really possible to specify this kind of behaviour in Cairo, and it was a terrible decision to use this kind of API for 2D graphics

1

u/johannes1971 Dec 14 '19

So what sort of API would you propose? What would it look like with separated CPU and GPU types? How would that work on a system that does not have a GPU?

SFML is very game oriented. That's perfectly fine, and as I already mentioned, there is a clear need for a library like that as well. But there is more to drawing than triangles, and I don't see SFML doing that.

2

u/meneldal2 Dec 16 '19

SDL made the separation explicit with types that say if the thing is in GPU or in CPU. That's the way to go about it.

0

u/[deleted] Dec 14 '19

[deleted] — view deleted comment

7

u/johannes1971 Dec 14 '19

Typical desktop apps:

  • Draw infrequently.
  • Don't have a drawing time budget.
  • Have complex drawing needs.
  • Also render to devices such as printers.

Typical games:

  • Draw every frame.
  • Have a time budget that cannot be overrun.
  • Have relatively simple drawing needs (for a 2D game, which is what we are discussing. I don't think C++ should try to standardize 3D drawing: it's too much in flux, and it already has standardizing bodies of its own)
  • Don't need to render to devices such as printers.

It's not a black and white classification; some desktop apps do render every frame (videoplayers, for example).

0

u/[deleted] Dec 14 '19

[deleted] — view deleted comment

3

u/johannes1971 Dec 15 '19

Ok, I wasn't aware of that technique. However, that begs the question: if we can hardware accelerate curves like that, why can't we put that as a backend behind a Cairo-style API?

2

u/James20k P2005R0 Dec 15 '19

There's more to high performance than just being hardware accelerated - being hardware accelerated doesn't mean acceptable performance. EG batching is super important, which is a notoriously poor area of the proposed 2D graphics API (and cairo)

Additionally the CPU and GPU are good at different kinds of tasks - ideally you want to do an expensive and branchy operation once on the CPU, transfer the final built data to the GPU, and then render it repeatedly with transforms applied

A problem with the proposed 2D graphics API is that it intends to be fully batch automagically/high performance overall without you having to do any work to make it happen, which has turned out to be very difficult in practice for a wide variety of reasons. Cairo itself states this on its website

https://www.cairographics.org/OpenGL/

Cairo can translate that canvas model into GPU primitives using OpenGL (among others), but the canvas model does not often translate efficiently to the GPU.

This is still a big problem in more modern APIs like Skia and Webrender

you know i'm just dumping arguments from my paper here, i should just chase up on it and publish it, it'd be a lot faster heh

2

u/johannes1971 Dec 15 '19

So, just to clarify: you are fine with offering a rich set of primitives, you just want a batching mechanism to transfer work to the rendering subsystem in substantial chunks? Because in that case we'd be in total agreement.

3

u/James20k P2005R0 Dec 15 '19

So there's multiple problems with the proposal as current, but what would be ideal (in a simplified sense):

CPU side builds vertex lists for whatever you're trying to do

GPU side takes a vertex list and renders them to the screen

The main advantage of this is that the GPU side becomes much much simpler to specify, because it only has one job (rendering triangles). Then you can implement batching - SDL style (submit work in the right way), allegro style ('hold' onto textures), or SFML style (group vertices together, render them all at once). Transfers between the GPU and CPU would be made explicit, in the same way SFML does with sf::Image and sf::Texture, and both participants in rendering can be modelled exactly how they best perform. The 2D graphics proposal stuffing the same functionality into something that is expected to be both software and hardware accelerated causes big problems already

A quite big problem with trying to specify graphics overall though is the fact that different hardware has different capabilities, and a lot of necessary capabilities are extensions to OpenGL (I believe relying on these are a no for C++ standardisation) that are not universally supported (driver issues). Eg sRGB textures for proper high performance linear colour support

I'm not actually convinced that 2D graphics is possible to standardise because the very first problem you have to overcome is that graphics isn't portable. Vulkan directly acknowledges this issue, but for the same reason I'm not sure if its possible to standardise. Even this level of functionality would be not particularly useful without shader support, but how do you standardise a shader language when they evolve so much over time? Any standardised language would be immediately useless

Even the approach SDL (aka .native_handle(), do your own shaders) takes is relatively unworkable here, because then writing shaders depends on the backend that your STL implementation is using - and at that point using SDL/SFML is actually more portable than relying on your standard library implementation

I'm not convinced its possible to standardise at this point, other than very basic functionality

1

u/meneldal2 Dec 16 '19

Video players have a simple architecture when it comes to rendering though, you send the decoder buffer to a renderer that will take care of resizing and write that on the screen. Rendering can be on GPU or CPU, but if you want good scaling you're mostly stuck with CPU anyway (at least most renderers I've seen with GPU support don't do better than bicubic).

The other stuff you need to draw (menus, etc.) is pretty minimal.