Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.0 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    LightStore.cpp
 * @brief   GPU-touching parts of LightStore (the per-frame lighting UBO lifecycle + binding).
 */
#include "./LightStore.hpp"

#include "../rhi/GfxDevice.hpp"
#include "../rhi/GfxEnums.hpp"

namespace esengine {

void LightStore::uploadAndBind() {
    if (!device_) return;

    if (ubo_ == BufferHandle::Invalid) {
        ubo_ = device_->createBuffer(
            {GfxBufferUsage::Uniform, static_cast<u32>(sizeof(LightConstants)), /*dynamic=*/true},
            GfxContent::retained(), &data_);
        dirty_ = false;
    } else if (dirty_) {
        device_->updateBuffer(ubo_, 0, &data_, sizeof(LightConstants));
        dirty_ = false;
    }
    device_->setUniformBuffer(LIGHT_CONSTANTS_BINDING, ubo_);
}

void LightStore::free() {
    if (ubo_ != BufferHandle::Invalid && device_) {
        device_->deleteBuffer(ubo_);
        ubo_ = BufferHandle::Invalid;
    }
    dirty_ = true;
}

}  // namespace esengine