Skip to content

Commit 301ab90

Browse files
Fixed issue when setting ViewPort multiple times per frame. (#1183)
- Changed viewport set from been instant to be done though the command queue. This fixes the behavior when dealing with multiple cameras. - Added validation tests for this. - Updated Babylon.js version.
1 parent c207e42 commit 301ab90

File tree

8 files changed

+112
-91
lines changed

8 files changed

+112
-91
lines changed
8.93 KB
Loading

Apps/ValidationTests/Scripts/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@
194194
"renderCount": 20,
195195
"referenceImage": "bwpp.png"
196196
},
197+
{
198+
"title": "Multi camera rendering",
199+
"playgroundId": "#1LK70I#22",
200+
"referenceImage": "multiCameraRendering.png"
201+
},
197202
{
198203
"title": "setParent",
199204
"playgroundId": "#JD49CT#2",

Apps/package-lock.json

Lines changed: 54 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"getNightly": "node scripts/getNightly.js"
77
},
88
"dependencies": {
9-
"babylonjs": "^5.35.1",
10-
"babylonjs-gui": "^5.35.1",
11-
"babylonjs-loaders": "^5.35.1",
12-
"babylonjs-materials": "^5.35.1",
9+
"babylonjs": "^5.42.2",
10+
"babylonjs-gui": "^5.42.2",
11+
"babylonjs-loaders": "^5.42.2",
12+
"babylonjs-materials": "^5.42.2",
1313
"chai": "^4.3.4",
1414
"jsc-android": "^241213.1.0",
1515
"mocha": "^9.2.2",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ namespace Babylon::Graphics
5757

5858
bgfx::ViewId m_viewId{};
5959
ViewPort m_viewPort{};
60+
uint16_t m_flags{BGFX_CLEAR_NONE};
61+
uint32_t m_rgba{0x000000ff};
62+
float m_depth{1.0f};
63+
uint8_t m_stencil{0};
64+
6065
bool m_hasViewIdBeenUsed{false};
6166

6267
bool m_disposed{false};

Core/Graphics/Source/FrameBuffer.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace Babylon::Graphics
77
{
88
namespace
99
{
10-
void setDefaultClearMode(bgfx::ViewId viewId, bgfx::FrameBufferHandle handle)
10+
void SetDefaultClearMode(bgfx::ViewId viewId, bgfx::FrameBufferHandle handle, uint16_t flags, uint32_t rgba, float depth, uint8_t stencil)
1111
{
1212
bgfx::setViewMode(viewId, bgfx::ViewMode::Sequential);
13-
bgfx::setViewClear(viewId, BGFX_CLEAR_NONE);
13+
bgfx::setViewClear(viewId, flags, rgba, depth, stencil);
1414
bgfx::setViewFrameBuffer(viewId, handle);
1515
}
1616

@@ -63,7 +63,15 @@ namespace Babylon::Graphics
6363
void FrameBuffer::Bind(bgfx::Encoder& encoder)
6464
{
6565
m_viewId = m_context.AcquireNewViewId(encoder);
66-
setDefaultClearMode(m_viewId, m_handle);
66+
67+
//Reset view state for next frame.
68+
m_viewPort = {0, 0, 1, 1};
69+
m_flags = BGFX_CLEAR_NONE;
70+
m_rgba = 0x000000ff;
71+
m_depth = 1.0f;
72+
m_stencil = 0;
73+
74+
SetDefaultClearMode(m_viewId, m_handle, BGFX_CLEAR_NONE, 0x000000ff, 1.0f, 0);
6775
setViewPort(m_viewId, m_viewPort, Width(), Height());
6876
m_hasViewIdBeenUsed = false;
6977
}
@@ -74,21 +82,24 @@ namespace Babylon::Graphics
7482

7583
void FrameBuffer::Clear(bgfx::Encoder& encoder, uint16_t flags, uint32_t rgba, float depth, uint8_t stencil)
7684
{
85+
m_flags = flags;
86+
m_rgba = rgba;
87+
m_depth = depth;
88+
m_stencil = stencil;
89+
7790
if (m_hasViewIdBeenUsed)
7891
{
7992
m_viewId = m_context.AcquireNewViewId(encoder);
80-
setDefaultClearMode(m_viewId, m_handle);
93+
SetDefaultClearMode(m_viewId, m_handle, m_flags, m_rgba, m_depth, m_stencil);
8194
setViewPort(m_viewId, m_viewPort, Width(), Height());
82-
m_hasViewIdBeenUsed = false;
8395
}
84-
85-
bgfx::setViewClear(m_viewId, flags, rgba, depth, stencil);
86-
87-
if (!m_hasViewIdBeenUsed)
96+
else
8897
{
89-
encoder.touch(m_viewId);
90-
m_hasViewIdBeenUsed = true;
98+
bgfx::setViewClear(m_viewId, m_flags, m_rgba, m_depth, m_stencil);
9199
}
100+
101+
m_hasViewIdBeenUsed = true;
102+
encoder.touch(m_viewId);
92103
}
93104

94105
void FrameBuffer::SetViewPort(bgfx::Encoder& encoder, float x, float y, float width, float height)
@@ -98,15 +109,16 @@ namespace Babylon::Graphics
98109
return;
99110
}
100111

112+
m_viewPort = {x, y, width, height};
113+
101114
if (m_hasViewIdBeenUsed)
102115
{
103116
m_viewId = m_context.AcquireNewViewId(encoder);
104-
setDefaultClearMode(m_viewId, m_handle);
105-
m_hasViewIdBeenUsed = false;
117+
SetDefaultClearMode(m_viewId, m_handle, m_flags, m_rgba, m_depth, m_stencil);
106118
}
107119

108-
m_viewPort = {x, y, width, height};
109120
setViewPort(m_viewId, m_viewPort, Width(), Height());
121+
m_hasViewIdBeenUsed = true;
110122
}
111123

112124
void FrameBuffer::Submit(bgfx::Encoder& encoder, bgfx::ProgramHandle programHandle, uint8_t flags)
@@ -120,7 +132,7 @@ namespace Babylon::Graphics
120132
// 1 blit per view, create a new viewId for each blit
121133
// TODO: Really? Why? Is this from the examples or something?
122134
m_viewId = m_context.AcquireNewViewId(encoder);
123-
setDefaultClearMode(m_viewId, m_handle);
135+
SetDefaultClearMode(m_viewId, m_handle, m_flags, m_rgba, m_depth, m_stencil);
124136
setViewPort(m_viewId, m_viewPort, Width(), Height());
125137

126138
encoder.blit(m_viewId, dst, dstX, dstY, src, srcX, srcY, width, height);

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ namespace Babylon
303303
JS_CLASS_NAME,
304304
{
305305
// This must match the version in nativeEngine.ts
306-
StaticValue("PROTOCOL_VERSION", Napi::Number::From(env, 7)),
306+
StaticValue("PROTOCOL_VERSION", Napi::Number::From(env, 8)),
307307

308308
StaticValue("CAPS_LIMITS_MAX_TEXTURE_SIZE", Napi::Number::From(env, limits.maxTextureSize)),
309309
StaticValue("CAPS_LIMITS_MAX_TEXTURE_LAYERS", Napi::Number::From(env, limits.maxTextureLayers)),
@@ -440,6 +440,7 @@ namespace Babylon
440440
StaticValue("COMMAND_DRAW", Napi::FunctionPointer::Create(env, &NativeEngine::Draw)),
441441
StaticValue("COMMAND_CLEAR", Napi::FunctionPointer::Create(env, &NativeEngine::Clear)),
442442
StaticValue("COMMAND_SETSTENCIL", Napi::FunctionPointer::Create(env, &NativeEngine::SetStencil)),
443+
StaticValue("COMMAND_SETVIEWPORT", Napi::FunctionPointer::Create(env, &NativeEngine::SetViewPort)),
443444

444445
InstanceMethod("dispose", &NativeEngine::Dispose),
445446

@@ -482,8 +483,6 @@ namespace Babylon
482483
InstanceMethod("getHardwareScalingLevel", &NativeEngine::GetHardwareScalingLevel),
483484
InstanceMethod("setHardwareScalingLevel", &NativeEngine::SetHardwareScalingLevel),
484485

485-
InstanceMethod("setViewPort", &NativeEngine::SetViewPort),
486-
487486
InstanceMethod("setCommandDataStream", &NativeEngine::SetCommandDataStream),
488487
InstanceMethod("submitCommands", &NativeEngine::SubmitCommands),
489488

@@ -1622,20 +1621,7 @@ namespace Babylon
16221621
Napi::Value NativeEngine::GetRenderHeight(const Napi::CallbackInfo& info)
16231622
{
16241623
return Napi::Value::From(info.Env(), m_graphicsContext.GetHeight());
1625-
}
1626-
1627-
void NativeEngine::SetViewPort(const Napi::CallbackInfo& info)
1628-
{
1629-
bgfx::Encoder* encoder{GetUpdateToken().GetEncoder()};
1630-
1631-
const auto x = info[0].As<Napi::Number>().FloatValue();
1632-
const auto y = info[1].As<Napi::Number>().FloatValue();
1633-
const auto width = info[2].As<Napi::Number>().FloatValue();
1634-
const auto height = info[3].As<Napi::Number>().FloatValue();
1635-
const float yOrigin = bgfx::getCaps()->originBottomLeft ? y : (1.f - y - height);
1636-
1637-
GetBoundFrameBuffer(*encoder).SetViewPort(*encoder, x, yOrigin, width, height);
1638-
}
1624+
}
16391625

16401626
Napi::Value NativeEngine::GetHardwareScalingLevel(const Napi::CallbackInfo& info)
16411627
{
@@ -1796,6 +1782,19 @@ namespace Babylon
17961782
m_stencilState |= BGFX_STENCIL_FUNC_REF(ref);
17971783
}
17981784

1785+
void NativeEngine::SetViewPort(NativeDataStream::Reader& data)
1786+
{
1787+
bgfx::Encoder* encoder{GetUpdateToken().GetEncoder()};
1788+
1789+
const float x{data.ReadFloat32()};
1790+
const float y{data.ReadFloat32()};
1791+
const float width{data.ReadFloat32()};
1792+
const float height{data.ReadFloat32()};
1793+
const float yOrigin = bgfx::getCaps()->originBottomLeft ? y : (1.f - y - height);
1794+
1795+
GetBoundFrameBuffer(*encoder).SetViewPort(*encoder, x, yOrigin, width, height);
1796+
}
1797+
17991798
void NativeEngine::SetCommandDataStream(const Napi::CallbackInfo& info)
18001799
{
18011800
// TODO: This should be moved to the constructor once multi-update is available.

Plugins/NativeEngine/Source/NativeEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ namespace Babylon
174174
void Clear(NativeDataStream::Reader& data);
175175
Napi::Value GetRenderWidth(const Napi::CallbackInfo& info);
176176
Napi::Value GetRenderHeight(const Napi::CallbackInfo& info);
177-
void SetViewPort(const Napi::CallbackInfo& info);
178177
Napi::Value GetHardwareScalingLevel(const Napi::CallbackInfo& info);
179178
void SetHardwareScalingLevel(const Napi::CallbackInfo& info);
180179
Napi::Value CreateImageBitmap(const Napi::CallbackInfo& info);
181180
Napi::Value ResizeImageBitmap(const Napi::CallbackInfo& info);
182181
void GetFrameBufferData(const Napi::CallbackInfo& info);
183182
void SetStencil(NativeDataStream::Reader& data);
183+
void SetViewPort(NativeDataStream::Reader& data);
184184
void SetCommandDataStream(const Napi::CallbackInfo& info);
185185
void SubmitCommands(const Napi::CallbackInfo& info);
186186
void DrawInternal(bgfx::Encoder* encoder, uint32_t fillMode);

0 commit comments

Comments
 (0)