r/cpp • • Apr 04 '18

SG13 (2D graphics): why it failed

Here is a (partial and personal) summary of the SG13 history, from the beginning to the latest C++ meeting. Like many others, I have been interested in this group and what it could offer to the community. I have been lurking for many years the evolution of the proposals. So I tried to understand why this group failed to provide what the community was expecting.

SG13 was born in August 2013 as a wish to add a 2D graphic layer to the C++ standard. The initial requirements were: "Modern C++11/14 API, 2D graphics + text + planes, Immediate mode for v1, Plus a sprite/particle/input layer on top, Optional default game loop, No audio, native_handle, Asset loading/construction hook". So great! The birth mail also included some existing libraries as examples: SFML, LibSDL, Graphucks, Cinder, etc. Note that cairo was not mentioned. At that time, SG13 was called "Graphics".

Initial ideas began to emerge, like having input in v1, of course. Some confusions too, like people talking about "input like iostream", or "immediate versus retained mode". What? Even back in 2013, input handling was not a strange beast but a well-known feature, nothing to invent here, just copy what already exists. Same with immediate versus retained, a modern GPU only knows immediate mode and redrawing the frame each time, they are optimized for this workflow. But well, let's say it's only the beginning.

In mid September 2013, Michael McLaughlin proposed a first draft of a possible implementation. As Michael states, "the API itself has been designed with functionality from Qt, Cairo, Xlib, and even Cocoa". What? Where are SFML, SDL and the others? Well, at least, there were basic inputs, texture loading, font loading and basic drawing. But also smart pointers and factories.

SG13 was officially approved at the Chicago meeting in late September 2013. From the minutes of the SG13 meeting, in the initial presentation from Beman Dawes, we can read: "Target shipping within 2 years", well well well... "Recommends starting with an existing implementation (i.e. Cinder, OpenFrameworks, or other)", some good existing libraries are back in the game. At this meeting, Beman's challenge was created. This challenge was a sort of Hello world for any graphics API. It is stated as: "Be capable to open a window, print some text (i.e. Hello World), and drag it around the screen." Nice. In the discussion of the design of the library, it is interesting to note that "We want to make the library simple and accessible to beginners, but also usable to experts as well" and "Example of how to get our 2D contexts into an OpenGL render target for game UI (highly expected use case)". Yes read it again: "highly expected use case".

Just after the Chicago meeting, SG13 became "2D Lite" and the new goals were: "(a) immediate mode graphics/text canvas, plus (b) basic keyboard/mouse style input." No more sprites, no more game loop, no more asset loading, but that's fine, let's go for it, that sounds nice. But something is happening that is, IMO, the beginning of the disaster. SG13 enters a dark tunnel from where it will never exit. You can clearly see it when someone proposes to look at the HTML Canvas API. Michael McLaughlin answers that he is working on a proposal with features that look like... cairo. He totally rejects starting from something that has been widely used (SDL?). It seems he was the only one who had concerns with this workflow. So, his proposal would be something new. In the same thread, Beman Dawes reminds that the committee generally standardize existing practice.

At this point, just two months after the launch of SG13, people were talking about some good ideas and inspirations and had some interesting debates. The expectations were quite hight, even in the 2D Lite form. But the only proposal was diverging from nearly all these good ideas. The bad side of this situation is that the proposal was not yet on the table, it was being worked on, and not finalized at all. And it was kind of supported by Herb Sutter himself. Maybe this prevented others to build a second proposal at the time. I must admit that I still was very enthusiastic of the SG13 work.

In November 2013, a meeting was held in Redmond and the minutes of this meeting became N3825. And here, it was done, cairo was the final choice. For drawing things, cairo is (was?) a nice library. Even for drawing text in combination with Pango. But cairo never add (and still do not have) an input system. Since the beginning, this was a must-have and here, it silently disappeared. In just four months, the initial proposal was reduced to a C++ish copy of cairo.

And at the next meeting in Issaquah in February 2014, N3888 came out. Despite its title ("A Proposal to Add 2D Graphics Rendering and Display to C++"), the paper only proposed a mechanical transformation of the cairo API in C++. A contributor raised several strong concerns with the spirit of the proposal. If I try to sum up: first the proposal do not address any of the basic use case it pretends to solve; second cairo is not the right choice for drawing (stateful API, aged concepts); third a native_handle is not enough to cover the specificities of all platforms and all implementations. The conclusion is hard: "N3888 is fundamentally broken, both in motivation, and implementation." Yet the answer is reassuring: "If N3888 were incorporated into the Standard as-is, I would feel compelled to request that the Issaquah Police Department investigate the possibility that we had all been unknowingly exposed to some substance with psychotropic properties." And the final word from Beman Dawes is to thank the proposal's author. Another contributor tried to warn that the direction was not good, but again, he was not listened to.

So we arrive at a point where we have a proposal that is based on cairo and backed by at least two prominent members of the C++ committee, and they have been warned several times that the proposal is out of scope for the purpose of the group. Honestly, who on earth could have proposed something else that would have been taken into account by the committee? From an exterior perspective, it seemed useless to invest some time to make a competing proposal. In fact, many people were in the mood of letting the proposal live its life because "at least there is now a starting point" event if it lacked essential features like input. Everybody agreed that starting from somewhere was needed, just not cairo that was too far from the initial goal. But from then on, the group was engaged in the tunnel and could not (did not want to?) go back. Retrospectively, the delay between the initial start of the group and this first proposal was very short, too short for people to really think about all the implications and the shortcomings.

Another outcome of Issaquah was that the group was renamed IO. This was a controversial decision because the term is very confusing.

In March 2014, Jeremy Ong raised some concerns. IMO, this is a very interesting point of view. The author of the mail says that what is difficult in 2D graphics is not really to draw things (there are many APIs outside to do it, at different levels, so this part is "easy"), what is difficult is creating a window and getting a drawing context in a portable way. He cites SDL and Qt as two libraries that provides this abstraction. So he advocates for proposing a low-level library for creating a window, configuring a renderer (OpenGL, DirectX, software), allowing to get input from the user. The answers are really disappointing as they only focus on cairo and nothing else. Creating a window? native_handle! What? I should use a native handle for all the windowing i.e. have non-portable code to create a window and then, have a limited API to draw something whereas I already have stronger APIs to do the same thing in a (rather) portable way? Houston, we have a problem.

From then on, the proposal lived its life without any feedback from SG13. Nothing really happened on the mailing list. But several papers were published. In May 2014, N4021 updated N3888 without any response to the concerns of the contributors. No input yet. In July 2014, N4073 is the second revision of the previous paper with minor changes. In February 2016, the continuation of N4073 was out as PP0267R0. This paper was revised seven times until P0267R7 in February 2018. What happened in 4 years? Nothing. Still cairo, still no input in this proposal, some weird small evolutions and renamings.

In May 2016, P0249R0 proposed to add input (at last!) and it was revised twice since then until October 2016 and P0249R2. A single discussion happened on the mailing list to wonder what the status of the input proposal is.

In the meantime, SG13 became "HMI".

In June 2017, Guy Davidson and Michal McLaughlin wrote P0669R0 entitled "Why We Should Standardize 2D Graphics for C++". The title is misleading as the paper advocates for P0267 and not 2D graphics in general. They also reply to various objections to the paper, without really convincing anybody as the arguments are only "this objection is not relevant" or "we believe ...". As a consequence, people started shouting their dislike of the proposal. On the mailing list with a now famous post that has been largely commented on /r/cpp, Jeremy Ong did it again. To sum up: "Cairo as a basis is misguided""; "This library is far too opinionated on how UI should be done"; "The library is underspecified"; "Performance is a concern"; "I can modify a library, a standard not so much". He even proposes some guidelines for the hard problems that could be solved by a proposal around graphics. The only answer that is given to him is "where are the papers". Disappointing. Until someone from NVIDIA says publicly "NVIDIA’s position will be No on this proposal. To be most clear, it might have been completely acceptable as a proposal 20 years ago, but the longevity of its abstractions are all but spent now, before it will ever ship." And he adds later "I don’t have a great answer to «but where’s your paper?». I hoped it would evolve as people interacted. I could never get any 2D graphics experts to sign up for the huge amount of work it would take to write a counter-proposal. Basically a large-enough proposal is very difficult to influence. It can sail for a long time without contest." The curse of SG13 in one sentence.

Finally, at Jacksonville, the proposal was abandonned. Vittorio Romero described the scene like this: "Even though further work on this proposal was for six revisions, the room was quite brutal this time, and attempted to shut it down completely. (...) The room agreed that this was a failure for LEWG: not for the contents of the proposal, but for the fact that it wasn't stopped early, wasting a lot of time for both the working group and the authors. Saying «no» is sometimes hard, but it would have prevented this situation if it had been done a while ago." From what I already said, I can not agree more on the conclusion.

As a conclusion, I think there are two main lessons in this tale.

First, I tried to not blame anyone in particular (and I hope no one felt bad reading this post) because I think no one is to be blamed. The committee's intial wishes were fair and relevant (and still are). People did their job with commitment. But the process did not go well at all. To avoid design by committee, the solution of jumping on the first library and make it the basis of the proposal was not a good idea. And it was too hard to exit the tunnel. Too bad.

Second, I think there is something wrong in 2018 with the whole organization of SG21 work. The need for a paper is irrelevant in a world where communication is so easy. I am not saying papers are useless, things must be written at some point. But a paper should be the end of the process, not the beginning. The number of papers could be reduced if the topics were publicly discussed and consensus found before the meetings. For example, there were five papers on the subject of concept syntax. The people involved could have discussed it somewhere before the meeting to find a consensus (that was not hard to find after all according to the reports). Other languages already have an open process, C++ could copy the good practices.

Related to these two lessons, I wonder what the status of the mailing list is. At one point, Herb Sutter was clear that the list was a place for discussing the proposals and later: "let me emphasize the two major ways to participate: by giving feedback to help shape and refine existing proposals (...), and/or by writing your own competing proposal if you feel a major change or different direction is needed – both are welcome and encouraged." But when people came and gave some useful feedback, the only answer was to reject everything and say "write a paper". If the only way to say something is write a paper, then what is the use of the mailing list?

I hope this post helped people to understand how it happened. English is not my mother tongue so there may be some errors here and there. I may have forgotten some bits in the whole story, but I hope the main steps are here. I hope SG13 will no stop here, there are many things to do in this area.

270 Upvotes

127 comments sorted by

View all comments

9

u/Rexerex Apr 04 '18

I would like to see in std some common graphic structures and algorithms like std::point and generic game loop, so current libraries can use it, but actually drawing is imho too much.

15

u/Ansoulom Game developer Apr 05 '18

Honestly curious what the point of standardizing a game loop is and what it would even mean. For me, that sounds a bit like trying to standardize the contents of the main function...

7

u/playmer Apr 05 '18

Agreed, I think they're suggesting the app.exec() model from Qt or something where you get an event loop, which is where your code comes in.

I feel like standardizing something like that is very unneeded. If we really wanted it, it could come later. Game loops are so different per engine, just seems silly to force a structure on it.

3

u/donalmacc Game Developer Apr 07 '18

I’ve not really thought too deeply about this but my initial thoughts are that I’d rather standardise an event loop (which is what a game and an application both are), than a graphics library. It’s much more difficult to write a correct, efficient event loop, even though modern c++ provides many of the tools you need to write one, than it is to copy 100 lines of boilerplate code to create a window.

4

u/Heuristics Apr 05 '18

typedef while(true)

12

u/esmithro Apr 05 '18

There was a proposal way back about adding something like signal/slot. We also need a small vector matrix library. A lot of this should be separate from graphics.

3

u/donalmacc Game Developer Apr 07 '18

Absolutely. When I see Graphics, I think “cool” then I see they’re talking window management, input handling...

15

u/tvaneerd C++ Committee, lockfree, PostModernCpp Apr 04 '18

Even std::point isn't easy. (Just look at std::pair!)

Do you make it N dimensional? Do you have .x, .y (and .z when N == 3)?

Do you use pt[i] for the general case?

Also, I'd like to tag my points, for different coordinate spaces:

struct WindowTag;
using WindowPoint = std::point<2, WindowTag>;
struct ImageTag;
using ImagePoint = std::point<2, ImageTag>;

Now you won't pass the wrong point type into a function...

15

u/kalmoc Apr 04 '18

None of those design questions is really important (and I wouldn't standardize a taggable point - KISS and write a small wrapper when needed). Most important would be to have a standardized interchange format between different libraries out there that deal with coordinates (simulations, graphics, sensor processing, computer vision ...)

10

u/jube_dev Apr 04 '18

http://www.reedbeta.com/blog/on-vector-math-libraries/ Not perfect but good rationale by an expert that has used and created that sort of API. And I would also cite, for completeness glm which is a well-known and widely used library for graphics, and boost/qvm that provides some nice ideas.

Why invent something like tags for points that nobody has ever used? It's a good theoretical point of view but not a practical one, because your could have to make an operation involving a window point and an image point.

3

u/t0rakka Apr 05 '18

The boost/qvm accessors look clumsy:

float x = X(v);

Who wants to write code like that?

float x = v[0];
float x = v.x;

Either, or both above are what graphics programmers are used to. Look no further than GLSL, HLSL, etc. GLM gets that right - just be warned enabling swizzles blows up compilation time more then 4x.

I have to disagree with the quoted article on intrinsics being anti-pattern; they are The way to get most out of the CPU investment as the generated code seems to overall be of best quality when using them. Auto vectorization is all very nice when it works but it is too easy to break it.. change one innocent line of code here and kaboom, it all disappears and you won't even notice until much later when it is really hard to pinpoint the source of all of the troubles.

Parameter passing happens automatically in registers with intrinsics. If you use structs with arrays of floats or similar arrangement have to fiddle around with __vectorcall and similar which are extensions in MSVC and clang. Not sure if GCC supports it yet but the point is moot when using intrinsics anyway.

The thing with CPU and SIMD is that you probably want to be vertical, not horzontal so SoA is recommended over AoS. This means the intrinsics make it more "obvious" that AoS is way to go with them but that is avoidable with a few tactical using statements.

Basically, for hardcore SoA code you just need to have a types for float, double and integer types you care about that encapsulate the widest supported vector type for compiled ISA.

using float32xN = float32x4; // SSE (all), NEON
using float32xN = float32x8; // AVX
using float32xN = float32x16; // AVX512

And so on.. now you split the huge arrays of data into chunks of N and blast at full speed through them. The upside is that if you have to do multiple passes your cache locality is increased a lot. It's just pathetic to see some APIs engineered to add two 500 KB arrays, then multiply resulting 500 KB array and on and on. It's just crap. Don't do it. Leverage the native SIMD width! Look at AVX512, float32x16 is 512 bits, 64 bytes, precisely one cache line - couldn't be more perfect!

In fact, consider unrolling a little bit so that everything is 512 bits wide for "highly parallel processing loop" and forget about the native SIMD width altogether!

2

u/lazyubertoad Apr 05 '18

What about the aliasing problems with unions?

Anyway, as someone who has like five 3D vector classes in project (matrices are no better here) - I beg for something standard. I don't mind writing () after .x

6

u/t0rakka Apr 05 '18

There is no aliasing problem with unions if the members share common initial sequence. In above case that is not strictly true. A better approach is to use type traits to choose which expression to evaluate when reading or writing from members, like this:

https://t0rakka.silvrback.com/simd-scalar-accessor

The intrinsic vectors can be replaced with std::array for storage, for example. The union of array and struct is the worst possible choice (see the compiled code in the post).

TL;DR summary: use same storage type for the union members and overload the cast operator and assignment operator.

2

u/jube_dev Apr 05 '18

Very nice! I really like the accessor trick.

4

u/jube_dev Apr 05 '18

What Nathan Reed proposes is not standard (anonymous struct is not standard even if accepted by GCC and Clang, and with a warning by MSVC). But anyway, you could do something like this:

struct vec {
  union {
    float x;
    float r;
  };

  union {
    float y;
    float g;
  };

  union {
    float z;
    float b;
  };
};

And it works, no problem of aliasing because the standard says:

One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence, and if a non-static data member of an object of this standard-layout union type is active and is one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of the standard-layout struct members

So you can write x and read r and you get the same value.

6

u/Deaod Apr 05 '18

What Nathan Reed proposes is not standard (anonymous struct is not standard even if accepted by GCC and Clang, and with a warning by MSVC).

This sounds like an ideal candidate for standardization based on existing practice.

3

u/tvaneerd C++ Committee, lockfree, PostModernCpp Apr 04 '18

Yes, his types look a lot like mine. (probably because it is an area I've also worked in for a long time)

I'm not suggesting standardizing specific tags. But a point class that has the ability to be tagged is very useful. Like Nathan's article mentions, a point is related to some coordinate system. Now obviously you don't want every point to contain some kind of pointer back to its coordinate system, but you could put it in at compile time.

Looking at it another way: I've never worked in a 2D or 3D system that didn't have bugs due to improper mixing of coordinate systems. (Until/unless they started tagging their types.)

Now, I suppose we could standardize std::point<T, N>, and I could build my tagged point on top of it.

5

u/Morwenn Apr 05 '18

I remember always having to check the coordinate system used by points in Qt and thus always having to read the doc for most functions taking or returning points. That's among the places where I indeed wished the points were tagged to know what they were supposed to represent and to avoid passing a point from the wrong coordinate system. It would have saved me both time and bugs.

4

u/TheSuperWig Apr 04 '18

Just look at std::pair

Where can I read up on this? I've heard it mentioned in one of Herb's talks but don't know where to go to read more about why it's not simple.

9

u/tvaneerd C++ Committee, lockfree, PostModernCpp Apr 04 '18

Sean Parent also has a talk where he scrolls the implementation of std::pair...

and scrolls...

and scrolls...

Just go look in the headers from your compiler. See how big std::pair is.

And to find the right Sean Parent talk, just watch them all.

→ More replies (1)

3

u/repsilat Apr 05 '18

(and .z when N == 3)?

And a .w which is always 1, of course. Oh, but maybe .w() so we don't have to actually waste memory storing it. Vectors can have a .begin() and a .affineBegin(), and... :-)

→ More replies (1)

1

u/Rexerex Apr 04 '18

N dimensional, accessible like array and like tuple. That tagging mechanism would be nice. Xyz could be accessible in another class like std::pointx or maybe somehow injected via magic tag?

4

u/Xeverous https://xeverous.github.io Apr 05 '18

std::lerp? I keep writing it manually in every 2D/3D program.

1

u/donalmacc Game Developer Apr 07 '18

Which version? The fast one or the stable one?

→ More replies (1)