r/GraphicsProgramming • • May 01 '24

At My Wits End

I've been looking around hard for a CPU based renderer and pretty much anything I find is either deprecated (which makes sense in today's context), for Linux (I'm on Windows btw, but a general solution would obviously be great), or secretly uses the GPU.

The reason I can't use the GPU is because my code doesn't think in terms of triangles/vectors as a GPU does, but rather RGBA values (as bytes). This is an issue as the conversion from a big chunk of bytes (which would be 6.5mb per frame) to the screen goes through the step of converting everything into triangles, which is obviously slow in my case.

For those who are going to say I should change my code to fit the GPU mindset more, you're not 100% wrong, and in future I do plan on working with the GPU to do some cool stuff. However, imagine the scenario that you don't have a GPU, or that you're working on an older computer/embedded hardware, well then you have literally no choice.

I don't mind if I get pointed towards a library or the bare bones API on how to do it, as long as I get something (in the meantime I will keep looking for myself). My code is currently in Golang, however I don't mind switching languages to C (or whatever else that is compiled).

Thanks in advance (:

2 Upvotes

34 comments sorted by

12

u/fllr May 01 '24

I don’t have cpu based renderer, but just coming here to say that you could render two triangles to cover the entire screen, and you could be dealing with each pixel individually in that case. 6.5mb per frame doesn’t sound like a lot either.

2

u/Jeeves7737 May 01 '24

I would have to look into that more but yeah that could work, probably the most efficient translation (I think)? It would just be nice not to have that translation step lol.
And yeah 6.5mb should be fine, even more reason for this to be possible and I was honestly surprised at how ill-performant the result was.

9

u/fllr May 01 '24

It shouldn’t be that bad. People write cpu based renderers all the time for the exact use case you mention, but it’s for really, reaaaaaaally low end computers. Most computers nowadays have even the most basic gpus included. The two triangle solution, by the way, is how most people write post processing effects, so that should help you get started. And, yeah. I don’t blame you at all for wanting to try something different, and learning on your own. :) but i wanted to make sure you knew of this alternative for when you got to that bottleneck :D good luck!

1

u/Jeeves7737 May 01 '24

Exactly, and back when they didn't even have GPUs commercially this was normal, so with today's hardware it should be more than possible. I'm very happy with today's GPUs, even integrated graphics is insanely fast which is really nice (: Exactly, it's all a learning exercise for myself to get better, and thank you for giving me his alternative (:

2

u/fllr May 01 '24

They were, but back then lighting was less complicated, screen sizes were smaller, and everything were hacks upon hacks.

One quick tip is that most people still write their first raytracer on the cpu, so maybe look at that for inspiration on how to do the cpu sided thing?

And for sure. I do learning projects all the time. I understand how people see something they don’t understand and get all toxic about it. So i try to support anyone with their weird projects nowadays. Lol. I’ve done my share of them, and always come out better for it!

2

u/Solrax May 01 '24

This is a really good point, and reminded me of this: https://raytracing.github.io/books/RayTracingInOneWeekend.html

You might find it interesting, though he dumps his images to a file, you will certainly be able to adapt it to a display.

2

u/Jeeves7737 May 01 '24

That's really cool, the images look really nice I'll definitely have to read through the maths for that one to see what I can get (:

1

u/Jeeves7737 May 01 '24

I will have to look at other people's raycasters more you're right.

Yeah it's been great learning raycasting, I did a whole blog series about my raycaster showing the code and all, but at some point in the future I want to be able to write up a tutorial on it so that people can learn how to make their own really easily and understand every step.

2

u/IceSentry May 01 '24

These days people use a single fullscreen triangle actually. It's more efficient than 2 triangles.

0

u/fllr May 01 '24

Huh? How?

4

u/IceSentry May 01 '24

0

u/[deleted] May 01 '24

[deleted]

2

u/Henrarzz May 01 '24

Full screen triangles are pretty common in my industry (games)

11

u/kraytex May 01 '24

You don't need triangles. They're not the only way to represent geometry. They're fast, which is why they're the default solution.

I would tell you to look up "Software Rasterization" which is rasterizing on the CPU - but this is yet again a triangle approach. It's usually taught to undergrads taking a Computer Graphics course.

Another way to represent geometry is with Signed Distance Fields (SDF). Tons of examples on ShaderToy: https://www.shadertoy.com/results?query=sdf&sort=popular No triangles here. A general approach is to use Sphere Tracing, where every pixel figures out if they hit any of the geometry based on their signed distanced from each geometry and computes their own color from there. This only really works in real time on a GPU because the GPU is highly parallel and every single pixel can computed at the same time.

If your data is RGBA bytes, then it sounds like you already have an image?

If you don't have a GPU, then presumably don't have a display? Today these things go hand-in-hand. Even the Raspberry Pi has a GPU on board. Unless you're doing something on a dot matrix.

Regardless of how your data is, I don't see any argument on how it can't be parallelized on the GPU in a compute shader, unless your GPU doesn't support compute shaders.

1

u/Jeeves7737 May 01 '24

Thanks I'll have a look at that link and software rasterization.
Yeah exactly I have an image as RGBA bytes, but I don't know how to push that to the physical screen as is, the only ways I have right now have the step in the middle that tried to optimize it by converting to triangles. It sounds so simple and yet I can't figure out what to use.

I think the Sphere Tracing sounds cool, and I'll keep that in mind for future projects for when I want to optimize further.

In essence I just want to write RGBA bytes to screen lol, but I think I complicated that in my post.

4

u/wrosecrans May 01 '24

Yeah exactly I have an image as RGBA bytes, but I don't know how to push that to the physical screen as is,

Basically any normal 2D GUI API will let you display an image in memory. Something like Qt is massive overkill for that. But this would be working minimal code for displaying an image on screen, quickly copied and pasted from Stack Overflow.

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QImage myImage;
  myImage.load("test.png");
  QLabel myLabel;
  myLabel.setPixmap(QPixmap::fromImage(myImage));
  myLabel.show();
  return a.exec();

}

That loads an image off disk, but it doesn't really matter where the bytes come from. You can construct a QImage from a buffer in memory or various other sources. No messing around with GPU shaders or using triangles to draw your image as a texture. The toolkit handles anything like that under the hood if it needs to.

Or if you are doing something low level like Vulkan without a fancy GUI toolkit, you just memcopy your image into the memory of a VkImage, and then vkCmdBlitImage onto the swapchain image that gets displayed, and you can ignore all the 3D stuff.

If you want to write raw Win32 on Windows, you can do it pretty easily, but Win32 is ugly and verbose for a Hello World if you aren't using a higher level toolkit like Qt.

If you want to use SDL, you would use SDL_BlitSurface to copy from your image in memory to the window. https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlblitsurface.html

There's a zillion ways to display an image on the screen. It just sounds like you've gone a bit astray reading about GPU 3d rendering stuff and sidetracked yourself.

1

u/Jeeves7737 May 01 '24

Yeah I would expect what I'm trying to do to be simple, but I'll have to try to keep track of all those, especially looking more into SDL etc.

I think in the end if I'm not happy with the solution that I have to use then I'll also write my own library on top of it so that other people who are trying to learn what I was trying to learn don't have the same issues, but we'll see if I get that far lol.

2

u/corysama May 01 '24

Here's how to do it in SDL. Under the hood it does the fastest technique possible for each platform. And, it should run on tons of platforms.

https://gist.github.com/CoryBloyd/6725bb78323bb1157ff8d4175d42d789

4

u/Solrax May 01 '24

Maybe we could help more if we knew what you are trying to do. Is this real-time? Is it 2D or 3D? Are you composing images or doing image processing or generating the images (photo app vs. data graphing for example).

2

u/Jeeves7737 May 01 '24

So in my specific code it's a real time "3D" game (raycasted so it's primitive and I'm using it for learning). This practically means that I now have a 2D image from what I calculated and just need to be able to push that to the screen as efficiently as possible. (Also if you don't know what a raycaster is it's the graphics of Wolfenstein3D).

2

u/Solrax May 01 '24 edited May 01 '24

You might be best off with some old classic texts, like literally from the Wolfenstein 3D era. I still have this book on my shelf: https://archive.org/details/gpbb20/gpbb0/

Also, I believe the old Doom and/or Quake code has been open sourced now, and they didn't originally run on any GPU.

That book has some general techniques, though stuff like VGA programming is well and truly obsolete. But you just need to blast pixels as fast as possible, and really if you are creating the bitmap yourself it's just fast copying to whatever display or window you are using. What are you using for your windowing?

Edit: or just use a library like this to do the actual display, and you can concentrate on generating the images. It says it used GPU for Direct2D, but it seems to me you can ignore that and focus on your own renderer. https://github.com/microsoft/Win2D

2

u/Jeeves7737 May 01 '24

I bookmarked that book to read, I definitely should read the old (and new) books on it, it would be really helpful and almost stupid of me not to learn from the people who've done it decades ago lol.

Yes I looked through the DOOM source code a bit and that's only for Linux using X11, which I was too stupid to use on my Windows machine especially since I'm not well versed in C.

I should look at the Quake source code though, I guess I just never really put 2 and 2 together to think that they released it (':

So the library I'm using for graphics is called ebiten (https://github.com/hajimehoshi/ebiten), I have no idea what it uses for underlying window control, as I've only gone through sections of the source code.

4

u/waramped May 01 '24

You can simply upload that image as a texture and draw it as a full screen quad. Any 2D "engine" will do that for you. Look at SDL or BGFX

4

u/Revolutionalredstone May 01 '24

Writing a 3D rasterizer is pretty trivial but it sounds like you just want a pointer to the pixels of a window (which is even more simple)

Just call SDL_GetWindowSurface(m_pWindow)->pixels and write away at full speed :D

I've been writting software renderers like this for decades: https://www.youtube.com/watch?v=UAncBhm8TvA

Enjoy

1

u/Jeeves7737 May 01 '24

Nice (: Looks like C/C++, so I'll give that a try.

2

u/Solrax May 01 '24

I'm also going to just throw out one of my favorite websites, https://webgl2fundamentals.org/ The author did work on the WebGL support in Chromium and seems to have answered most of the webgl questions on StackOverflow :)

When you do want to start messing around with GPU stuff, this can be a really fun way of learning it, if you know Javascript already. It is so fast and simple to mess with shaders in the browser (see also shadertoy.com). Don't be intimidated the examples you see there. You can do a simple shader with 2 triangles dividing your window, and map one texture to both of them, and that texture can be your rendering or anything else you want.

2

u/Jeeves7737 May 01 '24

Yes going into WebGL is something I should definitely back-pocket for the next thing to learn. That and maybe doing more stuff with WASM, which I've done a bit of work with but nothing really insane and nothing I remember, so at some point going back into the browser would be a great path to go down. Just have to write all the suggestions I'm getting down (':

2

u/thisismyredaccount May 01 '24

I often want to just open a window and draw a raw argb framebuffer to the screen, so I have made a "library" (just a single .c and .h file) that does this for me. All I have to do is create a framebuffer and run a function each loop that presents whatever is in that framebuffer on the screen.

I am using it in this repository for a [software terrain renderer](https://github.com/thisismyusernameongithub/Sandbox)

The files you would be interested in is window.c and .h

I could make a barebones example for you later if you are interested.

3

u/Jeeves7737 May 03 '24

I'll have to play around with this at some point, I had a quick look but I'll have a better look later and try to get some things on screen (:

2

u/msqrt May 03 '24

You don't have to convert anything to render on the GPU! You can take a pointer to your bytes and instruct your GPU API to copy those to a buffer representing the screen in one go. It's also not the case that GPUs force you to think in triangles and vectors. This used to be the case before the 2010s, but for quite a while you have had the option to just launch a bunch of threads, read/write in any pattern, and do any bit twiddling you need to get done. Of course it's still very different to CPU programming, but a modern GPU is not just a big rasterize+texturize device.

You're right that there are platforms where you can't use the GPU. But the converse also holds: if you're on a modern desktop, there is quite literally no way to put pixels on the screen without streaming them through the GPU. After all, that's where the screen is plugged in.

2

u/Jeeves7737 May 03 '24

I'm going to be honest with everyone here and say that I've fixed my issue, so thanks to everybody (: I've learned quite a bit especially things like this comment.

So I hinted in my post about how this was causing a performance issue for me, and it's fixed, my code can run crudely fast now. I took some caveman function counting to find out how many times per frame I was calling particularly expensive functions (such as square root). Turns out I was calling the fast inverse square root function 2.5 million times every single frame (classic blunder). I was also calling square root, sin, cos, and atan 800,000 times each, and tan 1.6 million times. So yeah..., I pulled out what I could from loops, calculated as much as I could before runtime, and well I went from 8fps to practically unbound (100's).

I'm a tiny bit annoyed because I feel like I wasted people's time, but then again, I don't think I'm wrong about my question about skipping the triangle step (thanks for pointing out that GPU's don't force triangles, you're right, it was just my graphics library I think lol). I do think that it's causing a bit of lag (also the fact that it holds all images in GPU memory, which is bad in my case).

I will try to get through all the resources people sent me, I've started a few of them, and I'll also still be looking for low-level libraries that I could use in future.

2

u/No-General2310 May 02 '24

Pikuma has a 3d tutorial in sdl that you might want to check out.

5

u/[deleted] May 01 '24 edited Jun 11 '25

[removed] — view removed comment

1

u/Jeeves7737 May 01 '24

Sadly I will admit this one lol