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.

3.3 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
#include "./RenderTarget.hpp"
#include "./GfxDevice.hpp"

namespace esengine {

void RenderTarget::init(GfxDevice& device, u32 width, u32 height, bool depth, bool linearFilter, bool asDrawn) {
    width_ = width;
    height_ = height;
    has_depth_ = depth;
    linear_filter_ = linearFilter;

    FramebufferSpec spec;
    spec.width = width;
    spec.height = height;
    spec.depthStencil = depth;
    spec.linearFilter = linearFilter;

    framebuffer_ = Framebuffer::create(device, spec);
    device_ = &device;
    if (device.storesTargetsTopDown() && !asDrawn) {
        FramebufferSpec shown = spec;
        shown.depthStencil = false;
        published_ = Framebuffer::create(device, shown);
    }
}

void RenderTarget::shutdown() {
    published_.reset();
    framebuffer_.reset();
    width_ = 0;
    height_ = 0;
}

void RenderTarget::bind() {
    if (framebuffer_) {
        framebuffer_->bind();
    }
}

void RenderTarget::unbind() {
    if (framebuffer_) {
        framebuffer_->unbind();
    }
}

void RenderTarget::resize(u32 width, u32 height) {
    if (width == width_ && height == height_) return;

    width_ = width;
    height_ = height;

    if (framebuffer_) {
        framebuffer_->resize(width, height);
    }
    if (published_) {
        published_->resize(width, height);
    }
}

TextureHandle RenderTarget::getColorTexture() const {
    if (published_) return published_->getColorAttachment();
    return framebuffer_ ? framebuffer_->getColorAttachment() : TextureHandle::Invalid;
}

void RenderTarget::publish() {
    if (!device_ || !framebuffer_ || !published_) return;
    device_->copyTextureFlippedY(framebuffer_->getColorAttachment(), published_->getColorAttachment());
}

TextureHandle RenderTarget::getDepthTexture() const {
    return framebuffer_ ? framebuffer_->getDepthAttachment() : TextureHandle::Invalid;
}

FramebufferHandle RenderTarget::getFramebuffer() const {
    return framebuffer_ ? framebuffer_->handle() : FramebufferHandle::Default;
}

RenderTargetManager::Handle RenderTargetManager::create(u32 width, u32 height, bool depth, bool linearFilter, bool asDrawn) {
    Handle handle;

    if (!free_list_.empty()) {
        handle = free_list_.back();
        free_list_.pop_back();
    } else {
        handle = next_handle_++;
        targets_.resize(handle);
    }

    auto target = makeUnique<RenderTarget>();
    target->init(*device_, width, height, depth, linearFilter, asDrawn);

    usize index = handle - 1;
    if (index < targets_.size()) {
        targets_[index] = std::move(target);
    }

    return handle;
}

RenderTarget* RenderTargetManager::get(Handle handle) {
    if (handle == INVALID_HANDLE || handle > targets_.size()) {
        return nullptr;
    }
    return targets_[handle - 1].get();
}

void RenderTargetManager::release(Handle handle) {
    if (handle == INVALID_HANDLE || handle > targets_.size()) {
        return;
    }

    auto& target = targets_[handle - 1];
    if (target) {
        target->shutdown();
        target.reset();
        free_list_.push_back(handle);
    }
}

bool RenderTargetManager::isValid(Handle handle) const {
    if (handle == INVALID_HANDLE || handle > targets_.size()) {
        return false;
    }
    return targets_[handle - 1] != nullptr;
}

}  // namespace esengine