Skip to content

Commit

Permalink
Now properly emptying BGFX handles after bgfx::destroy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 29, 2023
1 parent 4dd3e1d commit 690348f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
11 changes: 9 additions & 2 deletions source/client/graphics/ChunkRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ ChunkRenderer::ChunkRenderer(const TextureAtlas &textureAtlas) : m_textureAtlas(
}

ChunkRenderer::~ChunkRenderer() {
bgfx::destroy(m_fogColor);
bgfx::destroy(m_renderDistance);
if (bgfx::isValid(m_fogColor)) {
bgfx::destroy(m_fogColor);
m_fogColor.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_renderDistance)) {
bgfx::destroy(m_renderDistance);
m_renderDistance.idx = bgfx::kInvalidHandle;
}
}

inline static bool bbIntersects(const glm::vec3 &a0, const glm::vec3 &a1, const glm::vec3 &b0, const glm::vec3 &b1) {
Expand Down
20 changes: 15 additions & 5 deletions source/client/graphics/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,30 @@ void Framebuffer::free() {
bgfx::setViewFrameBuffer(0, BGFX_INVALID_HANDLE);
bgfx::setViewFrameBuffer(1, BGFX_INVALID_HANDLE);

if (bgfx::isValid(m_handle))
if (bgfx::isValid(m_handle)) {
bgfx::destroy(m_handle);
m_handle.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_depthFogColorUniform))
if (bgfx::isValid(m_depthFogColorUniform)) {
bgfx::destroy(m_depthFogColorUniform);
m_depthFogColorUniform.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_effectTypeUniform))
if (bgfx::isValid(m_effectTypeUniform)) {
bgfx::destroy(m_effectTypeUniform);
m_effectTypeUniform.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_depthTextureSampler))
if (bgfx::isValid(m_depthTextureSampler)) {
bgfx::destroy(m_depthTextureSampler);
m_depthTextureSampler.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_colorTextureSampler))
if (bgfx::isValid(m_colorTextureSampler)) {
bgfx::destroy(m_colorTextureSampler);
m_colorTextureSampler.idx = bgfx::kInvalidHandle;
}
}

void Framebuffer::loadShader(const std::string &name) {
Expand Down
5 changes: 4 additions & 1 deletion source/client/graphics/RenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void RenderTarget::free() {
m_cubeIndexBuffer.free();
m_defaultIndexBuffer.free();

bgfx::destroy(m_samplerUniform);
if (bgfx::isValid(m_samplerUniform)) {
bgfx::destroy(m_samplerUniform);
m_samplerUniform.idx = bgfx::kInvalidHandle;
}
}

void RenderTarget::draw(const Drawable &drawable, const RenderStates &states) {
Expand Down
4 changes: 3 additions & 1 deletion source/client/graphics/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Shader::Shader(const std::string &name) {
}

Shader::~Shader() {
if (bgfx::isValid(m_program))
if (bgfx::isValid(m_program)) {
bgfx::destroy(m_program);
m_program.idx = bgfx::kInvalidHandle;
}
}

void Shader::loadFromFile(const std::string &name) {
Expand Down
5 changes: 4 additions & 1 deletion source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ Skybox::Skybox(Camera &camera, ClientWorld &world) : m_camera(camera), m_world(w
}

Skybox::~Skybox() {
bgfx::destroy(m_starColor);
if (bgfx::isValid(m_starColor)) {
bgfx::destroy(m_starColor);
m_starColor.idx = bgfx::kInvalidHandle;
}
}

void Skybox::loadSky(const Sky &sky) {
Expand Down
4 changes: 3 additions & 1 deletion source/client/graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ void Texture::enable(u8 unit, bgfx::UniformHandle handle) const {
}

void Texture::free() {
if (bgfx::isValid(m_handle))
if (bgfx::isValid(m_handle)) {
bgfx::destroy(m_handle);
m_handle.idx = bgfx::kInvalidHandle;
}
}
11 changes: 9 additions & 2 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ GameState::GameState()
}

GameState::~GameState() {
bgfx::destroy(m_sunlightIntensity);
bgfx::destroy(m_skyColor);
if (bgfx::isValid(m_sunlightIntensity)) {
bgfx::destroy(m_sunlightIntensity);
m_sunlightIntensity.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_skyColor)) {
bgfx::destroy(m_skyColor);
m_skyColor.idx = bgfx::kInvalidHandle;
}
}

void GameState::init() {
Expand Down

0 comments on commit 690348f

Please sign in to comment.