Files
cocos4/native/cocos/renderer/gfx-validator/BufferValidator.cpp
T
Yun Hsiao Wu da2f03ff73 gfx: swapchain refactor (#3751)
* gfx: swapchain

* acquire/release/surface on swapchain

* fix swapchain texture creation

* acquire/present back on device

* formats are swapchain-specific

* swapchain: vk works

* enable device agent

* format fix

* update gfxdef

* fix enum decl

* pipeline adapt

* minor

* vk: compress attr locations directly in SPIR-V

* gles3 adjust

* InputAssembler owns an DrawInfo

* gles2 adapt

* texture keep track of its hash

* device cached states

* format

* glslang finalize

* buffer view adapt

* fg: subpass support

* minor

* 3.4 adapt

* profiler adapt

* buffer update frequency detection

* minor

* base commit

* swapchain works on metal

* spirv

* mtltexture issue

* remote test code

* depthtexture

* revert ifdef USE_METAL

* ios swapchain header

* fix some triple buffer issues

* resize accept surface transform

Co-authored-by: Zeqiang Li <Zeqiang-li@outlook.com>
2021-08-31 19:40:32 +08:00

151 lines
5.3 KiB
C++

/****************************************************************************
Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "base/CoreStd.h"
#include "base/Log.h"
#include "base/Macros.h"
#include "base/threading/MessageQueue.h"
#include "bindings/jswrapper/SeApi.h"
#include "BufferValidator.h"
#include "DeviceValidator.h"
#include "ValidationUtils.h"
#include "gfx-base/GFXDef-common.h"
namespace cc {
namespace gfx {
BufferValidator::BufferValidator(Buffer *actor)
: Agent<Buffer>(actor) {
_typedID = actor->getTypedID();
}
BufferValidator::~BufferValidator() {
DeviceResourceTracker<Buffer>::erase(this);
CC_SAFE_DELETE(_actor);
uint lifeTime = DeviceValidator::getInstance()->currentFrame() - _creationFrame;
// skip those that have never been updated
if (!_isBufferView && hasFlag(_memUsage, MemoryUsageBit::HOST) && _totalUpdateTimes && _totalUpdateTimes < lifeTime / 3) {
CC_LOG_WARNING("Triple buffer enabled for infrequently-updated buffer, consider using MemoryUsageBit::DEVICE instead");
CC_LOG_DEBUG("Init Stacktrace: %s", _initStack.c_str());
}
}
void BufferValidator::doInit(const BufferInfo &info) {
CCASSERT(info.usage != BufferUsageBit::NONE, "invalid buffer param");
CCASSERT(info.memUsage != MemoryUsageBit::NONE, "invalid buffer param");
// CCASSERT(info.size, "zero-sized buffer?"); // be more lenient on this for now
_initStack = se::ScriptEngine::getInstance()->getCurrentStackTrace();
_creationFrame = DeviceValidator::getInstance()->currentFrame();
_totalUpdateTimes = 0U;
if (hasFlag(info.usage, BufferUsageBit::VERTEX) && !info.stride) {
CCASSERT(false, "invalid stride for vertex buffer");
}
/////////// execute ///////////
_actor->initialize(info);
}
void BufferValidator::doInit(const BufferViewInfo &info) {
CCASSERT(info.buffer, "invalid source buffer");
CCASSERT(info.offset + info.range <= info.buffer->getSize(), "invalid range");
/////////// execute ///////////
BufferViewInfo actorInfo = info;
actorInfo.buffer = static_cast<BufferValidator *>(info.buffer)->getActor();
_actor->initialize(actorInfo);
}
void BufferValidator::doResize(uint size, uint /*count*/) {
CCASSERT(!_isBufferView, "cannot resize through buffer views");
CCASSERT(size, "invalid size");
/////////// execute ///////////
_actor->resize(size);
}
void BufferValidator::doDestroy() {
_actor->destroy();
}
void BufferValidator::update(const void *buffer, uint size) {
CCASSERT(!_isBufferView, "cannot update through buffer views");
CCASSERT(size && size <= _size, "invalid size");
CCASSERT(buffer, "invalid buffer data");
if (hasFlag(_usage, BufferUsageBit::INDIRECT)) {
const auto * drawInfo = static_cast<const DrawInfo *>(buffer);
const size_t drawInfoCount = size / sizeof(DrawInfo);
const bool isIndexed = drawInfoCount > 0 && drawInfo->indexCount > 0;
for (size_t i = 1U; i < drawInfoCount; ++i) {
if ((++drawInfo)->indexCount > 0 != isIndexed) {
CCASSERT(false, "inconsistent indirect draw infos on using index buffer");
}
}
}
sanityCheck(buffer, size);
++_totalUpdateTimes; // only count direct updates
/////////// execute ///////////
_actor->update(buffer, size);
}
void BufferValidator::sanityCheck(const void *buffer, uint size) {
uint cur = DeviceValidator::getInstance()->currentFrame();
if (cur == _lastUpdateFrame) {
// FIXME: minggo: as current implementation need to update some buffers more than once, so disable it.
// Should enable it when it is fixed.
// CC_LOG_WARNING(utils::getStacktraceJS().c_str());
// CC_LOG_WARNING("performance warning: buffer updated more than once per frame");
}
if (DeviceValidator::getInstance()->isRecording()) {
_buffer.resize(_size);
memcpy(_buffer.data(), buffer, size);
}
_lastUpdateFrame = cur;
if (DeviceValidator::getInstance()->isRecording()) {
_buffer.resize(_size);
memcpy(_buffer.data(), buffer, size);
}
}
} // namespace gfx
} // namespace cc