r/cpp • u/smallstepforman • Jul 06 '20
C++ is brilliant
I'm working on a Vulkan renderer, and needed to delay destruction of render scenes since the graphics pipeline has render buffers in flight, so destruction has to happen N frames after the user destroys a class. With C++, I can use private destructors to force users to add the scene to a managed destructor queue (aka garbage collect queue). Normally language purists will scorn a language which allows such shenanigans, however in real world production code, its features like this which allows developers to create modern software. So with a couple of lines of code, I solved my delayed destructor issue. Thank you C++ committee for creating a usable language.
28
u/throttlecntrl Jul 07 '20
‘‘tis is a double edged samauri sword
1
9
u/sign_in_or_sign_up Jul 07 '20
that's a neat use case, had not considered a private destructor before. :-)
6
u/josefx Jul 07 '20
I generally dislike private destructors because they force a heap allocation and I ran into several cases in the past were that ate into my execution time. However in this case the overhead compared to the API calls is probably minimal.
I probably would make the destructor queue part of the constructor parameters and have the destructor forward the required members when called. Of course that has its own limitations.
14
u/reflexpr-sarah- Jul 07 '20
you can use placement new to allocate objects dynamically on the stack without running destructors
2
u/josefx Jul 09 '20
I could use placement new if I had any alternative way of calling the destructor, so my complaint against private destructors is a bit of target. It is mostly that the libraries that I see using private destructors also assume that they can call
delete thison the object and do not provide an alternative cleanup function.
8
u/NilacTheGrim Jul 07 '20
It's Bjarne. His sensibilities are still present in the language and he blesses the committee's changes to the language and still provides input. Thank him first! But yes.. the committee too.
It's the only language where you have ultimate control but also can benefit from high level abstractions.
I hope it continues to exist. It's being threatened by the next crop of programmers hating on it.. but.. hopefully it will continue to stand the test of time. And I like the direction it's evolving in...
2
u/playman_gamer Aug 05 '20
I agree somewhat, but the language is gigantic and messy at this point, and individual projects will use very different subsets of C++. It has its handful of good ideas (RAII, etc), but the committees insistence on backwards compatibility has made it a very difficult language to maintain and work with.
I just wish Rust had taken the path of "just the good parts", but at this point it is ballooning in size and is almost as verbose as C++.
1
u/NilacTheGrim Aug 05 '20
I know where you are coming from and yes, there is a cost to all of this. There is also a benefit -- backwards compatibility has the added bonus of feeling "safe" and "sound". It's like having an old stable bank you know won't go bust tomorrow. C++, while a large investment, seems to be a good investment since it stands the test of time and also is the "lingua franca" of underlying systems (just about every major thing we depend on is written in C and C++).
Those things are so attractive to me -- and the language has evolved in such a good direction in the last 10 years (and continues to do so) -- that I am optimistic and enthusiastic about it again.
I realize this doesn't address your criticisms of the language which are valid .. but it just adds some optimism about the good things.. to counterbalance the argument a little bit.
4
u/seanballais Jul 07 '20
What are render buffers in Vulkan? Do they have equivalents in OpenGL? I'm still a noob in graphics programming with noob-level knowledge in OpenGL.
Also, how do you determine N?
2
u/annyeonghello Jul 07 '20 edited Jul 07 '20
The term render buffer widely means a buffer with objects that will be rendered onto screen. In Vulkan, a somewhat similar concept exist and is called a "Command Buffer" which is created from a "Command Pool" and it does not have an OpenGL equivalent. But you can make your own render buffer in OpenGL.
N frames is simply the number of frames and it is a slightly more advance concept. You'll slowly figure it out as you do more :)
1
u/seanballais Jul 07 '20
The term render buffer widely means a buffer with objects that will be rendered onto screen. In Vulkan, a somewhat similar concept exist and is called a "Command Buffer" which is created from a "Command Pool" and it does not have an OpenGL equivalent. But you can make your own render buffer in OpenGL.
Oh, cool! Why does Vulkan require a command pool?
N frames is simply the number of frames and it is a slightly more advance concept. You'll slowly figure it out as you do more :)
Is the "frame" you refer here different from the usual "frame" term we use in, say, games?
1
u/annyeonghello Jul 07 '20
It's not just Vulkan, pretty sure DX12 has something similar as well. You need a command pool to create a command buffer. Without it, you can not create a command buffer and Vulkan only draws what is in a command buffer. Again, this is a really advance subject and any further discussion will most likely confuse you.
In OP's context yes. Do note that there is no proper definition for a frame though. I've seen it used interchangeably between describing an image or an iteration of the program loop. But you'll notice that the context when using the word is somewhat similar :)
1
u/seanballais Jul 08 '20
Alright. Let's leave it at "Vulkan only draws what is in a command buffer, and I'll need to know more about graphics programming before I better understand the low-level nature of Vulkan and DX12." for now. I guess I'll put figuring the value of
Nin OP's "after N frames" at that too.I did not expect anyone would call a program loop iteration as a frame. Haha. Then again, as you said, it is similar to OP's context.
4
u/fii0 Jul 07 '20
in flight
What does that mean?
13
u/vazgriz Jul 07 '20
GPUs execute commands asynchronously from the CPU code that generates them. So while you are generating command buffer for frame
Xon the CPU, frameX - 1is simultaneously being executed on the GPU.You can have N frames “in flight” at a time, where N is typically 2 or 3. That means N frames are potentially executing in the GPU. So if the last time you use a resource is frame X, you need to keep that resource alive until frame
X + N.5
u/LugosFergus Jul 08 '20
This is the correct answer. For everyone else, ignore that other dude’s ridiculous linguistics answer. I have no idea why that’s getting upvotes.
11
u/MINIMAN10001 Jul 07 '20
graphics pipeline has render buffers in flight
"In flight" would probably be closest to "in use"
To quote https://english.stackexchange.com/a/189990
That is largely true, but the use of in-flight here is used to create the visualization of data as a plane. You can't (easily) divert planes in mid-air, so if you move/destroy their destination they are out of luck.
"Something happened during a process" implies that the process has stopped or gone awry. In-flight implies that something is in motion as normal, and needs a destination. It can't just stop where it is.
2
u/track33r Jul 07 '20
Even better is avoid allocations at all and use some sort of ring buffer or pool structure.
3
u/raphaelj Jul 07 '20
You could solve that problem in any GC language by just keeping a reference to the object in a deferred queue ...
And quite sincerely, the way you solve this problem is exactly what I don't like with C++ programmers. You could just use a shared_ptr to your objects and keep them in the deferred queue, like anyone would do in a GC language. Instead, you choose to hack the way users would expect destructors to work and make your software harder to understand.
15
u/TheSkiGeek Jul 07 '20 edited Jul 07 '20
As some of the other comments pointed out — having both sides (the game logic and the GPU thread) keep
shared_ptrs to the resources is a simpler solution. Unfortunately it’s also less performant:
if there are a large number of resources involved then there are a lot of unnecessary refcount increments and decrements going on. This can potentially really hurt your CPU cache hit rate, since it pulls all the involved cache lines in whenever the pointers are copied
if the GPU is the last thing holding a reference to a resource then it will get destroyed on the GPU thread as soon as the refcount hits zero, which might slow it down in an undesirable and unpredictable way
you can also get really fun rare bugs like “the destructor for this resource needs more stack space than the GPU thread has”
Those kinds of issues make it hard to write game engines in languages with mandatory GC.
That said, if I needed it to be fast I’d probably suggest keeping a queue of
shared_ptrs orunique_ptrs to the resources on the game side, and retiring them in a controlled way after the required number of frames have passed. And then the GPU code would only hold non-owning “dumb” pointers to the resources and never deal with allocating/deallocating anything.0
Jul 07 '20
[deleted]
5
u/Reticulatas Jul 07 '20
Unfortunately most of the issues TheSkiGeek mentioned are real issues that crop up in large scale games pretty regularly. Tight control of GPU resources (read: not stack dependent) is essential for games with a huge quantity of streaming assets like open world genres.
-25
u/chickentractor420 Jul 06 '20
Yes, c++ has all these features that make it very good and useable in 2020, However if you use it for a very long time you will start to notice that there are some flaws in the design of the language.
35
u/mcintosh_red Jul 07 '20
Is there a language that doesn't have design flaws?
42
u/dannomac Jul 07 '20
There are two types of languages: those that people complain about, and those that nobody uses.
-23
u/jasonwirth Jul 07 '20
And Rust.
71
26
u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 07 '20
So a language that nobody uses, yet people complain about?
9
5
12
u/Ptxcv Jul 07 '20
I use rust btw
7
9
3
u/mcdicedtea Jul 07 '20
nope, but are there languages which you will have less everyday interaction with said flaws? yes.
are their languages where 'working around said flaws' are not a major everyday concern, and active part of the language's latest features?....also yes
2
u/mcdicedtea Jul 07 '20
totally agree, and to further clarify... you'll spend more time wrangling with the flaws and heft of the language than time glamorizing some one of solution
127
u/MutantSheepdog Jul 07 '20
Yep, that would work.
Easier approach for others to implement would just be to have those buffers/textures loaded/stored/referenced with shared_ptrs, and that way destruction would happen after no entity is using it and rendering has finished.
Slightly less efficient, but easier to implement.