forked from BabylonJS/BabylonNative
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBufferPool.cpp
More file actions
128 lines (111 loc) · 4.92 KB
/
Copy pathFrameBufferPool.cpp
File metadata and controls
128 lines (111 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <array>
#include <vector>
#include <stdexcept>
#include <bimg/bimg.h>
#include <bgfx/bgfx.h>
#include "FrameBufferPool.h"
namespace Babylon::Polyfills
{
const std::vector<FrameBufferPool::PoolBuffer>& FrameBufferPool::GetPoolBuffers()
{
return mPoolBuffers;
}
// sets dimension of framebuffers, can only be set if no buffers in pool
void FrameBufferPool::SetDimensions(int width, int height)
{
assert(width > 0 && height > 0);
// TODO: support multiple framebuffer dimensions
if (mPoolBuffers.size() > 0)
{
throw std::runtime_error("Cannot set dimensions. FrameBufferPool already has buffers.");
}
this->m_width = width;
this->m_height = height;
}
// sets graphics context to be used for creating framebuffers
void FrameBufferPool::SetGraphicsContext(Graphics::DeviceContext* graphicsContext)
{
m_graphicsContext = graphicsContext;
}
void FrameBufferPool::Add(int nBuffers)
{
if (m_graphicsContext == nullptr)
{
throw std::runtime_error("Cannot add framebuffer to pool. Graphics context is not set.");
}
for (int i = 0; i < nBuffers; ++i)
{
bgfx::FrameBufferHandle TextBuffer{bgfx::kInvalidHandle};
Graphics::FrameBuffer* FrameBuffer;
// make sure render targets are filled with 0 : https://registry.khronos.org/webgl/specs/latest/1.0/#TEXIMAGE2D
bgfx::ReleaseFn releaseFn{[](void*, void* userData) {
bimg::imageFree(static_cast<bimg::ImageContainer*>(userData));
}};
bimg::ImageContainer* image = bimg::imageAlloc(&Babylon::Graphics::DeviceContext::GetDefaultAllocator(), bimg::TextureFormat::RGBA8, m_width, m_height, 1 /*depth*/, 1, false /*cubeMap*/, false /*hasMips*/);
const bgfx::Memory* mem = bgfx::makeRef(image->m_data, image->m_size, releaseFn, image);
bx::memSet(image->m_data, 0, image->m_size);
// TODO: make sampler flags configurable
// border sampling will result in transparent edge artifacts for blur, but this behaviour is consistent with browser implementation
std::array<bgfx::TextureHandle, 2> textures{
bgfx::createTexture2D(m_width, m_height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT | BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER | BGFX_SAMPLER_BORDER_COLOR(0), mem),
bgfx::createTexture2D(m_width, m_height, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER | BGFX_SAMPLER_BORDER_COLOR(0))};
// See NativeEngine::CreateFrameBuffer: bgfx validation now asserts when BGFX_RESOLVE_AUTO_GEN_MIPS is used
// with a texture whose format doesn't have BGFX_CAPS_FORMAT_TEXTURE_MIP_AUTOGEN. Gate the color attachment
// on the capability and pass BGFX_RESOLVE_NONE for the depth attachment (depth formats never support autogen).
const bgfx::Caps* caps = bgfx::getCaps();
const uint8_t colorResolve = 0 != (caps->formats[bgfx::TextureFormat::RGBA8] & BGFX_CAPS_FORMAT_TEXTURE_MIP_AUTOGEN)
? BGFX_RESOLVE_AUTO_GEN_MIPS
: BGFX_RESOLVE_NONE;
std::array<bgfx::Attachment, textures.size()> attachments{};
attachments[0].init(textures[0], bgfx::Access::Write, 0, 1, 0, colorResolve);
attachments[1].init(textures[1], bgfx::Access::Write, 0, 1, 0, BGFX_RESOLVE_NONE);
TextBuffer = bgfx::createFrameBuffer(static_cast<uint8_t>(attachments.size()), attachments.data(), true);
FrameBuffer = new Graphics::FrameBuffer(*m_graphicsContext, TextBuffer, m_width, m_height, false, false, false);
m_available++;
mPoolBuffers.push_back({FrameBuffer, true});
}
}
void FrameBufferPool::Clear()
{
for (auto& buffer : mPoolBuffers)
{
if (buffer.frameBuffer)
{
buffer.frameBuffer->Dispose();
delete buffer.frameBuffer;
}
}
m_available = 0;
mPoolBuffers.clear();
}
Graphics::FrameBuffer* FrameBufferPool::Acquire()
{
// no buffers in pool, add one
if (m_available == 0)
{
Add(1);
}
for (auto& buffer : mPoolBuffers)
{
if (buffer.isAvailable)
{
buffer.isAvailable = false;
m_available--;
return buffer.frameBuffer;
}
}
throw std::runtime_error("No available frame buffer in pool.");
}
void FrameBufferPool::Release(Graphics::FrameBuffer* frameBuffer)
{
for (auto& buffer : mPoolBuffers)
{
if (buffer.frameBuffer == frameBuffer)
{
buffer.isAvailable = true;
m_available++;
return;
}
}
}
}