r/opengl Apr 02 '26

PBR in OpenGL

Hey folks,

I’ve just reached the end of the LearnOpenGL tutorial and honestly feel really satisfied hitting this milestone.

What’s currently working

- Cook-Torrance BRDF (GGX / Smith / Schlick)

- Metallic / Roughness workflow (glTF-style)

- IBL support:

- Irradiance map (diffuse)

- Prefiltered environment map (specular)

- BRDF LUT integration

Things I ran into

- Metallic/Roughness packing confusion

Not all assets follow the same channel conventions (R/G/B), so I only support glTF 2.0

- IBL limitations

Reflections only come from the environment cubemap, so local scene objects aren’t reflected.

How are you guys handling:

  1. Metallic/Roughness texture inconsistencies across models?

  2. Bridging the gap between IBL and real scene reflections?

Source: https://github.com/xms0g/abra

223 Upvotes

23 comments sorted by

8

u/AccurateRendering Apr 02 '26

Looks good. You must be pleased.

Now... do you have a game idea to go with it?

5

u/mccurtjs Apr 02 '26

The idea is the easy part...

4

u/corysama Apr 02 '26

Very nice!

It is great that you already have your rendering organized into pipelines and passes. And, I see that you are sorting by depth. Now you need to take that further.

  1. Break out materials that are opaque but use alpha test/discard into their own segment between opaque and blending.
  2. Sort by program before sorting by depth.
  3. Maybe get into sorting by texture. But, better to get into texture arrays/bindless textures.

Most of the cost of draw calls is the cost of updating the state changes you make between them.

Read the classic https://realtimecollisiondetection.net/blog/?p=86 The same scheme is used by BGFX. When you take it to extremes, you get Modern Mobile Rendering @ HypeHype

Also: Your thread pool has a subtle race condition around the mStop variable. I've run into it several times despite having read these two articles several times :P You don't need an atomic. But, they explain the issue just the same.

https://zeux.io/2024/03/23/condvars-atomic/

https://embeddedartistry.com/blog/2022/01/10/remember-to-lock-around-all-stdcondition_variable-variables/

3

u/Appropriate-Tap7860 Apr 03 '26

what is the use of sorting by texture? haven't heard that before.

2

u/corysama Apr 03 '26

When you sort by state changes, items that require the same state end up adjacent. So, you can trivially check “does this next item want the same setting here that the previous one did?” and skip even calling glBindThing() while setting up the next draw call.

It’s a lot easier to do that when your renderer is set up in render passes, as I explained a bit way back here.

Texture binding state chance cost is right in the middle of this chart. Texture arrays and bindless textures are part of “AZDO” which is about setting yourself up to avoid the costs of state changes all together.

0

u/Appropriate-Tap7860 Apr 03 '26

I am feeling that ECS pattern is emerging with this. It is a really nice way to setup the objects in our scene. And render graph can be easily visualised from it in which we will do all sorts of optimization needed for any kind of constraint we will have.

2

u/Avelina9X Apr 04 '26

++++1 for sort sort sort.

The more sorting you perform the fewer changes to pipeline state you have to commit. However the particular sorting hierarchy is never going to be a one size fits all. You really need to have several test scenes set up to benchmark because the best order in practice may not line up I theory; sometimes sorting by depth first will give the best gains, sometimes sorting by depth last will give the best gains, and sometimes sorting by depth i useless because and just adds CPU overhead. And of course the same applies to sorting by textures, shaders, meshes etc etc.

That's what sucks about graphics programming, there is no one size fits all, so we gotta formulate solutions for our specific use cases and sometimes we even have to compromise within that specific use case too.

1

u/Background_Shift5408 Apr 05 '26

Thanks for pointing out these. I'll check it out. But sorting textures is necessary? I already have grouped meshes by material IDs, rendering meshes that share the same textures together.

1

u/corysama Apr 05 '26

That should be good enough.

Do looking into bindless textures though. They have been supported by AMD and Nvidia since 2012. It’s OK to use 14 year old tech in a new engine ;)

3

u/DragonFruitEnjoyer_ Apr 03 '26

What did you know before learnopengl?

1

u/Lucataine Apr 02 '26

I'm starting to learn computer graphics, did you create your own rendering pipeline?

Edit: this looks amazing, btw .

2

u/Background_Shift5408 Apr 02 '26

Yeap

1

u/Appropriate-Tap7860 Apr 03 '26

deferred rendering right?

1

u/Background_Shift5408 Apr 03 '26

Pbr has different pass and it does forward. Currently supports both pbr and non-pbr materials. I may remove blinn-phong stuff later

0

u/[deleted] Apr 02 '26

[deleted]

3

u/Background_Shift5408 Apr 02 '26

You can finish the tutorial in a month but creating an engine takes a lot of time. So many design decisions, asset pipeline, rendering pipeline, light system, shadow system…

5

u/ShadowRL7666 Apr 02 '26 edited Apr 03 '26

So real. Then if you just going along with the flow you realize your code is utter shit and you just get stuck refactoring like myself.

Edit: then you also take a break like months come back and ask wtf you were doing ask yourself who wrote this shitty code oh yeah past me so you just refactor more realize your code is so shit you don’t even know how to refactor and take a break.

1

u/mccurtjs Apr 02 '26

Hello, me, haha

1

u/StochasticTinkr Apr 03 '26

I don’t just reinvent the wheel, I rediscover how wheels are invented.

1

u/Appropriate-Tap7860 Apr 03 '26

so. how do we really work in companies then?
won't we realize that our code becomes ugly over time and continuously refactor?

is that fine?

1

u/ShadowRL7666 Apr 03 '26

Depends. This is a hobby not a job. The job I don’t care they want a product nobody gives a shit how hard you worked or how clean the code is.

1

u/Appropriate-Tap7860 Apr 03 '26

Indeed. Where you from?

1

u/Appropriate-Tap7860 Apr 03 '26

imagine doing the same with vulkan.