Skip to content

Commit 699764a

Browse files
FrameBuffer depthStencil texture destruction fix (#1538)
- [JS thread] engine is disposed from - [main thread] resetView is called - [main thread] DisableRendering -> bgfx context is destroyed - [JS thread] ExternalWithFinalizer for the `depthStencilTextureHandle` --------- Co-authored-by: Gary Hsu <[email protected]>
1 parent 1698bec commit 699764a

File tree

8 files changed

+24
-19
lines changed

8 files changed

+24
-19
lines changed

.github/jobs/test_install_win32.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
# BGFX_CONFIG_MAX_FRAME_BUFFERS is set so enough Framebuffers are available before V8 starts disposing unused ones
4343
- script: |
4444
# BGFX_CONFIG_MAX_FRAME_BUFFERS is set so enough Framebuffers are available before V8 starts disposing unused ones
45-
cmake -B build${{ variables.solutionName }} -A ${{ parameters.platform }} ${{ variables.jsEngineDefine }} -D BX_CONFIG_DEBUG=ON -D GRAPHICS_API=${{ parameters.graphics_api }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
45+
cmake -G "Visual Studio 17 2022" -B build${{ variables.solutionName }} -A ${{ parameters.platform }} ${{ variables.jsEngineDefine }} -D BX_CONFIG_DEBUG=ON -D GRAPHICS_API=${{ parameters.graphics_api }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
4646
displayName: 'Generate ${{ variables.solutionName }} solution'
4747
4848
- task: MSBuild@1

.github/jobs/uwp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
vmImage: ${{ parameters.vmImage }}
3333

3434
- script: |
35-
cmake -B build${{ variables.solutionName }} -D CMAKE_SYSTEM_NAME=WindowsStore -D CMAKE_SYSTEM_VERSION=10.0 ${{ variables.jsEngineDefine }} -A ${{ parameters.platform }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BABYLON_DEBUG_TRACE=ON
35+
cmake -G "Visual Studio 17 2022" -B build${{ variables.solutionName }} -D CMAKE_SYSTEM_NAME=WindowsStore -D CMAKE_SYSTEM_VERSION=10.0 ${{ variables.jsEngineDefine }} -A ${{ parameters.platform }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BABYLON_DEBUG_TRACE=ON
3636
displayName: 'Generate ${{ variables.solutionName }} solution'
3737
3838
- task: VSBuild@1

.github/jobs/win32.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040

4141
# BGFX_CONFIG_MAX_FRAME_BUFFERS is set so enough Framebuffers are available before V8 starts disposing unused ones
4242
- script: |
43-
cmake -B build${{ variables.solutionName }} -A ${{ parameters.platform }} ${{ variables.jsEngineDefine }} -D BX_CONFIG_DEBUG=ON -D GRAPHICS_API=${{ parameters.graphics_api }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
43+
cmake -G "Visual Studio 17 2022" -B build${{ variables.solutionName }} -A ${{ parameters.platform }} ${{ variables.jsEngineDefine }} -D BX_CONFIG_DEBUG=ON -D GRAPHICS_API=${{ parameters.graphics_api }} -D CMAKE_UNITY_BUILD=$(UNITY_BUILD) -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
4444
displayName: 'Generate ${{ variables.solutionName }} solution'
4545
4646
- task: MSBuild@1

Core/Graphics/InternalInclude/Babylon/Graphics/FrameBuffer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Babylon::Graphics
2020
class FrameBuffer final
2121
{
2222
public:
23-
FrameBuffer(DeviceContext& context, bgfx::FrameBufferHandle handle, uint16_t width, uint16_t height, bool defaultBackBuffer, bool hasDepth, bool hasStencil);
23+
FrameBuffer(DeviceContext& context, bgfx::FrameBufferHandle handle, uint16_t width, uint16_t height, bool defaultBackBuffer, bool hasDepth, bool hasStencil, int8_t depthStencilAttachmentIndex = -1);
2424
~FrameBuffer();
2525

2626
FrameBuffer(const FrameBuffer&) = delete;
@@ -69,5 +69,6 @@ namespace Babylon::Graphics
6969
Rect m_desiredScissor{};
7070

7171
bool m_disposed{};
72+
int8_t m_depthStencilAttachmentIndex{-1};
7273
};
7374
}

Core/Graphics/Source/DeviceImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ namespace Babylon::Graphics
109109

110110
void UpdateBgfxState();
111111
void UpdateBgfxResolution();
112-
void DiscardIfDirty();
113112
void RequestScreenShots();
114113
void Frame();
115114
bgfx::Encoder* GetEncoderForThread();

Core/Graphics/Source/FrameBuffer.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Babylon::Graphics
77
{
8-
FrameBuffer::FrameBuffer(DeviceContext& deviceContext, bgfx::FrameBufferHandle handle, uint16_t width, uint16_t height, bool defaultBackBuffer, bool hasDepth, bool hasStencil)
8+
FrameBuffer::FrameBuffer(DeviceContext& deviceContext, bgfx::FrameBufferHandle handle, uint16_t width, uint16_t height, bool defaultBackBuffer, bool hasDepth, bool hasStencil, int8_t depthStencilAttachmentIndex)
99
: m_deviceContext{deviceContext}
1010
, m_deviceID{deviceContext.GetDeviceId()}
1111
, m_handle{handle}
@@ -15,6 +15,7 @@ namespace Babylon::Graphics
1515
, m_hasDepth{hasDepth}
1616
, m_hasStencil{hasStencil}
1717
, m_disposed{false}
18+
, m_depthStencilAttachmentIndex{depthStencilAttachmentIndex}
1819
{
1920
}
2021

@@ -30,10 +31,19 @@ namespace Babylon::Graphics
3031
return;
3132
}
3233

33-
if (bgfx::isValid(m_handle) && m_deviceID == m_deviceContext.GetDeviceId())
34+
if (m_deviceID == m_deviceContext.GetDeviceId())
3435
{
35-
bgfx::destroy(m_handle);
36-
m_handle = BGFX_INVALID_HANDLE;
36+
if (m_depthStencilAttachmentIndex >= 0)
37+
{
38+
bgfx::destroy(bgfx::getTexture(m_handle, m_depthStencilAttachmentIndex));
39+
m_depthStencilAttachmentIndex = -1;
40+
}
41+
42+
if (bgfx::isValid(m_handle))
43+
{
44+
bgfx::destroy(m_handle);
45+
m_handle = BGFX_INVALID_HANDLE;
46+
}
3747
}
3848

3949
m_disposed = true;

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,7 @@ namespace Babylon
18501850
}
18511851

18521852
bgfx::TextureHandle depthStencilTextureHandle = BGFX_INVALID_HANDLE;
1853+
int8_t depthStencilAttachmentIndex = -1;
18531854
if (generateStencilBuffer || generateDepth)
18541855
{
18551856
if (generateStencilBuffer && !generateDepth)
@@ -1874,6 +1875,7 @@ namespace Babylon
18741875
// And not sure it makes sense to generate mipmaps from a depth buffer with exponential values.
18751876
// only allows mipmaps resolve step when mipmapping is asked and for the color texture, not the depth.
18761877
// https://github.com/bkaradzic/bgfx/blob/2c21f68998595fa388e25cb6527e82254d0e9bff/src/renderer_d3d11.cpp#L4525
1878+
depthStencilAttachmentIndex = numAttachments;
18771879
attachments[numAttachments++].init(depthStencilTextureHandle);
18781880
}
18791881

@@ -1888,15 +1890,8 @@ namespace Babylon
18881890
throw Napi::Error::New(info.Env(), "Failed to create frame buffer");
18891891
}
18901892

1891-
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer(m_deviceContext, frameBufferHandle, width, height, false, generateDepth, generateStencilBuffer);
1892-
return Napi::Pointer<Graphics::FrameBuffer>::Create(info.Env(), frameBuffer, [frameBuffer, depthStencilTextureHandle]() {
1893-
if (bgfx::isValid(depthStencilTextureHandle))
1894-
{
1895-
bgfx::destroy(depthStencilTextureHandle);
1896-
}
1897-
1898-
delete frameBuffer;
1899-
});
1893+
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer(m_deviceContext, frameBufferHandle, width, height, false, generateDepth, generateStencilBuffer, depthStencilAttachmentIndex);
1894+
return Napi::Pointer<Graphics::FrameBuffer>::Create(info.Env(), frameBuffer, Napi::NapiPointerDeleter(frameBuffer));
19001895
}
19011896

19021897
// TODO: This doesn't get called when an Engine instance is disposed.

nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- checkout: self
2222

2323
- script: |
24-
cmake -B build -A x64 -D BX_CONFIG_DEBUG=ON -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
24+
cmake -G "Visual Studio 17 2022" -B build -A x64 -D BX_CONFIG_DEBUG=ON -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
2525
displayName: 'Generate solution'
2626
2727
- script: |

0 commit comments

Comments
 (0)