Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering fixes #1981

Merged
merged 2 commits into from
Jun 14, 2024
Merged

Rendering fixes #1981

merged 2 commits into from
Jun 14, 2024

Conversation

smilediver
Copy link
Contributor

This PR contains two fixes:

Backend: remove RenderTargetFlag and refactor depth/stencil state setup

First fix removes flags from RenderTarget. This fixes the problem where depth and stencil flags in RenderTarget were desynchronizing from the Renderer state. And this also fixes the issue where a single GPU render pass unnecessarily was split into several incurring a performance hit, because backends instead of changing depth or stencil state were actually changing render targets.

RenderTarget should only represent a stateless thing being rendered into (a framebuffer or a render texture), so it should be just a collection of color/depth/stencil attachments. For some reason it also contained flags for which of those buffers should be enabled or disabled, but it's duplicating the state already tracked in Renderer. Renderer can check what attachments are available in the RenderTarget and what state is set and provide the combined and exact information for what attachments to enable to the backend.

Fix Z-test and Z-write being enabled by default for main queue

Second fix by default disables Z-write and Z-tests for 2D sprites for the main queue, and enables them for 3D models.

This fixes the issue that Z-write and Z-test was enabled by default for 2D sprites only in the main render queue. It causes issues when mixing 2D sprites and 3D models. This is possibly an oversight, because they are enabled only for the main queue, and for GroupCommand queues (e.g. render textures) they are disabled. So this makes all render queues behave the same.

@halx99
Copy link
Collaborator

halx99 commented Jun 12, 2024

This PR affect core renderer backend, we need more time to test.

@halx99 halx99 added needs-test enhancement New feature or request labels Jun 12, 2024
@smilediver
Copy link
Contributor Author

Sure, there's no rush. On my side I did test on Windows, macOS, Android and iOS.

@halx99 halx99 added this to the 2.1.4 milestone Jun 13, 2024
@halx99
Copy link
Collaborator

halx99 commented Jun 13, 2024

Do you run all tests of cpp-tests on macOS, because the macOS metal backend will throw exception if any render state incorrect

@halx99 halx99 merged commit 1a39fbf into axmolengine:dev Jun 14, 2024
15 checks passed
@halx99
Copy link
Collaborator

halx99 commented Jun 14, 2024

lgtm, merged

@smilediver
Copy link
Contributor Author

smilediver commented Jun 14, 2024

Do you run all tests of cpp-tests on macOS, because the macOS metal backend will throw exception if any render state incorrect

I didn't run all the cpp-tests, because auto test doesn't work (it never worked for me, and always crashed). But I did run some tests related to graphics, and all seemed to work. As well as tested on our game, which has some complex rendering stuff like mixing 2D/3D, using render textures, etc.

@solan-solan
Copy link
Contributor

I stumbled with issue according to this fix, specifically to changing inside RenderTargetGL.cpp
There is the following case to render shadows in my project

First frame:

  1. Render to first depth texture for first shadow map cascade (one Depth attachment, no any Color attachments)
  2. Render to second depth texture for second shadow map cascade (one Depth attachment, no any Color attachments)
  3. Render to third color/depth texture which gets all scene (uses Color/Depth attachment)

Second frame:

  1. Render to first depth texture... -> error occurs on the trying to clear first depth texture

====================
The first screen demonstrate how it worked before. The glFrameBufferTexture2D was called even if no color attachments exists by the reason of _flags was equal 1. To my understand it caused that color attachment was properly switched off if it was not introduced (as in my case for shadow textures).
1

Now it looks like this
3
glDrawBuffers is called for null array without glFrameBufferTexture2D. There is no issue if color attachment was not applied before.
But when it was applied (steps 3 -> step 4) I see the strange stacktrace. Debugger shows double step on Line 124
4
And then:
5

@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

@smilediver do you have any idea?

@rh101
Copy link
Contributor

rh101 commented Jun 15, 2024

I stumbled with issue according to this fix, specifically to changing inside RenderTargetGL.cpp There is the following case to render shadows in my project

Is there an existing test in the cpp-tests project that reproduces this? If not, would it be possible for you to add one, or make available a minimal project that reproduces the issue?

@halx99 If this issue is not easily solvable, then would it make sense to revert these changes, and have them in a different branch until they're tested and working correctly?

@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

I stumbled with issue according to this fix, specifically to changing inside RenderTargetGL.cpp There is the following case to render shadows in my project

Is there an existing test in the cpp-tests project that reproduces this? If not, would it be possible for you to add one, or make available a minimal project that reproduces the issue?

@halx99 If this issue is not easily solvable, then would it make sense to revert these changes, and have them in a different branch until they're tested and working correctly?

agree

halx99 added a commit that referenced this pull request Jun 15, 2024
@halx99 halx99 mentioned this pull request Jun 15, 2024
@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

reverting

halx99 added a commit that referenced this pull request Jun 15, 2024
@solan-solan
Copy link
Contributor

I updated https://github.com/solan-solan/HeightMap/commits/smooth_lod_passing to correspond it for [a13c6bf] and [0ea9efd] branches, check it please.

By the way RenderTarget::_flags work inconsistence now.
They set here at first when RenderTarget is created
Screenshot_10

Then, if I properly understand, they should be changed for each separate CommandBufferGL like here for depth state:
image

But _currentRT->addFlag/removeFlag works with error for me:
6
7

_flags gets 1 everytime after _currentRT->addFlag(TargetBufferFlags::DEPTH); and 33 after _currentRT->removeFlag(TargetBufferFlags::DEPTH) it should be related with this macros
8
(Can you confirm it?)

In other words RenderTarget::_flags == 1 always for RenderTargetGL::update(). (By side effect?)

Besides its never reset COLOR like it supposed that one color attachment would be anyway.
Really this render changes looks good if you can confirm inconsistence of RenderTarget::_flags.

I use this workaround now:

void RenderTargetGL::update() const {
    if (!_dirty) return;
    if(!_defaultRenderTarget) {
        { // color attachments
            GLenum bufs[MAX_COLOR_ATTCHMENT] = {GL_NONE};
            for (size_t i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
            {
                if (_color[i] || !i)

@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

I updated https://github.com/solan-solan/HeightMap/commits/smooth_lod_passing to correspond it for [a13c6bf] and [0ea9efd] branches, check it please.

By the way RenderTarget::_flags work inconsistence now. They set here at first when RenderTarget is created Screenshot_10

Then, if I properly understand, they should be changed for each separate CommandBufferGL like here for depth state: image

But _currentRT->addFlag/removeFlag works with error for me: 6 7

_flags gets 1 everytime after _currentRT->addFlag(TargetBufferFlags::DEPTH); and 33 after _currentRT->removeFlag(TargetBufferFlags::DEPTH) it should be related with this macros 8 (Can you confirm it?)

In other words RenderTarget::_flags == 1 always for RenderTargetGL::update(). (By side effect?)

Besides its never reset COLOR like it supposed that one color attachment would be anyway. Really this render changes looks good if you can confirm inconsistence of RenderTarget::_flags.

I use this workaround now:

void RenderTargetGL::update() const {
    if (!_dirty) return;
    if(!_defaultRenderTarget) {
        { // color attachments
            GLenum bufs[MAX_COLOR_ATTCHMENT] = {GL_NONE};
            for (size_t i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
            {
                if (_color[i] || !i)

checked, it's correct, due to the TargetBufferFlags is uint8_t, the debugger display char, it's num value is 49

@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

image

@halx99
Copy link
Collaborator

halx99 commented Jun 15, 2024

I reset axmol to 42bf253 and build your https://github.com/solan-solan/HeightMap/commits/smooth_lod_passing, and the GL errors occured:

image

{ // color attachments
GLenum bufs[MAX_COLOR_ATTCHMENT] = {GL_NONE};
for (size_t i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
{
if (bitmask::any(_flags, getMRTColorFlag(i)))
if (_color[i])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

due to _flags was removed, need update color attachment always

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solan-solan remove the condition check _color[i] can solve above GL errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solan-solan remove the condition check _color[i] can solve above GL errors

@halx99 I believe that it helps

@smilediver
Copy link
Contributor Author

I'll have a look at this issue next week.

@halx99
Copy link
Collaborator

halx99 commented Jun 16, 2024

I'v rebase and drop the merge commit, and a backup branch was created: https://github.com/axmolengine/axmol/tree/refactor-render-target , you can fork it and continue working on it, then create a new PR

@smilediver smilediver mentioned this pull request Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants