Skip to content

Commit

Permalink
remote ia pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesky013 committed Aug 4, 2023
1 parent b37b73a commit ab4ce17
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 91 deletions.
17 changes: 11 additions & 6 deletions native/cocos/2d/renderer/Batcher2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,26 @@ void Batcher2d::generateBatch(RenderEntity* entity, RenderDrawInfo* drawInfo) {
return;
}
gfx::InputAssembler* ia = nullptr;

uint32_t indexOffset = 0;
uint32_t indexCount = 0;
if (drawInfo->getIsMeshBuffer()) {
// Todo MeshBuffer RenderData
ia = drawInfo->requestIA(getDevice());
indexOffset = drawInfo->getIndexOffset();
indexCount = drawInfo->getIbCount();
_meshRenderDrawInfo.emplace_back(drawInfo);
} else {
UIMeshBuffer* currMeshBuffer = drawInfo->getMeshBuffer();

currMeshBuffer->setDirty(true);

ia = currMeshBuffer->requireFreeIA(getDevice());
uint32_t indexCount = currMeshBuffer->getIndexOffset() - _indexStart;
indexCount = currMeshBuffer->getIndexOffset() - _indexStart;
if (ia == nullptr) {
return;
}

ia->setFirstIndex(_indexStart);
ia->setIndexCount(indexCount);
indexOffset = _indexStart;
_indexStart = currMeshBuffer->getIndexOffset();
}

Expand All @@ -364,6 +367,8 @@ void Batcher2d::generateBatch(RenderEntity* entity, RenderDrawInfo* drawInfo) {
auto* curdrawBatch = _drawBatchPool.alloc();
curdrawBatch->setVisFlags(_currLayer);
curdrawBatch->setInputAssembler(ia);
curdrawBatch->setFirstIndex(indexOffset);
curdrawBatch->setIndexCount(indexCount);
curdrawBatch->fillPass(_currMaterial, depthStencil, dssHash);
const auto& pass = curdrawBatch->getPasses().at(0);

Expand Down Expand Up @@ -392,8 +397,6 @@ void Batcher2d::generateBatchForMiddleware(RenderEntity* entity, RenderDrawInfo*

meshBuffer->setDirty(true);
gfx::InputAssembler* ia = meshBuffer->requireFreeIA(getDevice());
ia->setFirstIndex(drawInfo->getIndexOffset());
ia->setIndexCount(drawInfo->getIbCount());

// stencilstage
auto stencilStage = _stencilManager->getStencilStage();
Expand All @@ -403,6 +406,8 @@ void Batcher2d::generateBatchForMiddleware(RenderEntity* entity, RenderDrawInfo*
auto* curdrawBatch = _drawBatchPool.alloc();
curdrawBatch->setVisFlags(_currLayer);
curdrawBatch->setInputAssembler(ia);
curdrawBatch->setFirstIndex(drawInfo->getIndexOffset());
curdrawBatch->setIndexCount(drawInfo->getIbCount());
curdrawBatch->fillPass(material, depthStencil, dssHash);
const auto& pass = curdrawBatch->getPasses().at(0);
if (entity->getUseLocal()) {
Expand Down
55 changes: 13 additions & 42 deletions native/cocos/2d/renderer/RenderDrawInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,28 @@ void RenderDrawInfo::changeMeshBuffer() {

gfx::InputAssembler* RenderDrawInfo::requestIA(gfx::Device* device) {
CC_ASSERT(_drawInfoAttrs._isMeshBuffer && _drawInfoAttrs._drawInfoType == RenderDrawInfoType::COMP);
if (!_iaPool) {
_iaPool = ccnew ccstd::vector<gfx::InputAssembler*>;
}
if (_nextFreeIAHandle >= _iaPool->size()) {
initIAInfo(device);
}
auto* ia = (*_iaPool)[_nextFreeIAHandle++];
ia->setFirstIndex(getIndexOffset());
ia->setIndexCount(getIbCount());
return ia;
return initIAInfo(device);
}

void RenderDrawInfo::uploadBuffers() {
CC_ASSERT(_drawInfoAttrs._isMeshBuffer && _drawInfoAttrs._drawInfoType == RenderDrawInfoType::COMP);
if (_drawInfoAttrs._vbCount == 0 || _drawInfoAttrs._ibCount == 0) return;
uint32_t size = _drawInfoAttrs._vbCount * 9 * sizeof(float); // magic Number
gfx::Buffer* vBuffer = _iaInfo->vertexBuffers[0];
gfx::Buffer* vBuffer = _ia->getVertexBuffers()[0];
vBuffer->resize(size);
vBuffer->update(_vDataBuffer);
gfx::Buffer* iBuffer = _iaInfo->indexBuffer;
gfx::Buffer* iBuffer = _ia->getIndexBuffer();
uint32_t iSize = _drawInfoAttrs._ibCount * 2;
iBuffer->resize(iSize);
iBuffer->update(_iDataBuffer);
}

void RenderDrawInfo::resetMeshIA() {
void RenderDrawInfo::resetMeshIA() { // NOLINT(readability-make-member-function-const)
CC_ASSERT(_drawInfoAttrs._isMeshBuffer && _drawInfoAttrs._drawInfoType == RenderDrawInfoType::COMP);
_nextFreeIAHandle = 0;
}

void RenderDrawInfo::destroy() {
_nextFreeIAHandle = 0;
if (_iaInfo) {
CC_SAFE_DELETE(_iaInfo->indexBuffer);
if (!_iaInfo->vertexBuffers.empty()) {
// only one vb
CC_SAFE_DELETE(_iaInfo->vertexBuffers[0]);
_iaInfo->vertexBuffers.clear();
}
CC_SAFE_DELETE(_iaInfo);
}

if (_iaPool) {
for (auto* ia : *_iaPool) {
CC_SAFE_DELETE(ia);
}
_iaPool->clear();
CC_SAFE_DELETE(_iaPool);
}

_ia = nullptr;
if (_localDSBF) {
CC_SAFE_DELETE(_localDSBF->ds);
CC_SAFE_DELETE(_localDSBF->uboBuf);
Expand All @@ -109,8 +81,8 @@ void RenderDrawInfo::destroy() {
}

gfx::InputAssembler* RenderDrawInfo::initIAInfo(gfx::Device* device) {
if (_iaPool->empty()) {
_iaInfo = ccnew gfx::InputAssemblerInfo();
if (!_ia) {
gfx::InputAssemblerInfo iaInfo = {};
uint32_t vbStride = 9 * sizeof(float); // magic Number
uint32_t ibStride = sizeof(uint16_t);
auto* vertexBuffer = device->createBuffer({
Expand All @@ -126,14 +98,13 @@ gfx::InputAssembler* RenderDrawInfo::initIAInfo(gfx::Device* device) {
ibStride,
});

_iaInfo->attributes = *(Root::getInstance()->getBatcher2D()->getDefaultAttribute());
_iaInfo->vertexBuffers.emplace_back(vertexBuffer);
_iaInfo->indexBuffer = indexBuffer;
}
auto* ia = device->createInputAssembler(*_iaInfo);
_iaPool->emplace_back(ia);
iaInfo.attributes = *(Root::getInstance()->getBatcher2D()->getDefaultAttribute());
iaInfo.vertexBuffers.emplace_back(vertexBuffer);
iaInfo.indexBuffer = indexBuffer;

return ia;
_ia = device->createInputAssembler(iaInfo);
}
return _ia;
}

void RenderDrawInfo::updateLocalDescriptorSet(Node* transform, const gfx::DescriptorSetLayout* dsLayout) {
Expand Down
8 changes: 4 additions & 4 deletions native/cocos/2d/renderer/RenderDrawInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#pragma once
#include "2d/renderer/UIMeshBuffer.h"
#include "base/Ptr.h"
#include "base/Macros.h"
#include "base/TypeDef.h"
#include "bindings/utils/BindingUtils.h"
Expand Down Expand Up @@ -266,8 +267,6 @@ class RenderDrawInfo final {
ccstd::hash_t _dataHash{0};
} _drawInfoAttrs{};

uint16_t _nextFreeIAHandle{0};

bindings::NativeMemorySharedToScriptActor _attrSharedBufferActor;
// weak reference
Material* _material{nullptr};
Expand All @@ -291,8 +290,9 @@ class RenderDrawInfo final {
scene::Model* _model;
uint8_t* _sharedBuffer;
};
gfx::InputAssemblerInfo* _iaInfo{nullptr};
ccstd::vector<gfx::InputAssembler*>* _iaPool{nullptr};
LocalDSBF* _localDSBF{nullptr};

// ia
IntrusivePtr<gfx::InputAssembler> _ia;
};
} // namespace cc
30 changes: 8 additions & 22 deletions native/cocos/2d/renderer/UIMeshBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,10 @@ void UIMeshBuffer::initialize(ccstd::vector<gfx::Attribute>&& attrs, bool needCr

void UIMeshBuffer::reset() {
setIndexOffset(0);
_nextFreeIAHandle = 0;
_dirty = false;
}

void UIMeshBuffer::resetIA() {
for (auto* ia : _iaPool) {
ia->setFirstIndex(0);
ia->setIndexCount(0);
}
}

void UIMeshBuffer::destroy() {
Expand All @@ -85,11 +80,7 @@ void UIMeshBuffer::destroy() {
_vData = nullptr;
_iData = nullptr;
// Destroy InputAssemblers
for (auto* ia : _iaPool) {
ia->destroy();
delete ia;
}
_iaPool.clear();
_ia = nullptr;
if (_needDeleteLayout) {
CC_SAFE_DELETE(_meshBufferLayout);
}
Expand All @@ -100,32 +91,28 @@ void UIMeshBuffer::setDirty() {
}

gfx::InputAssembler* UIMeshBuffer::requireFreeIA(gfx::Device* device) {
if (_nextFreeIAHandle >= _iaPool.size()) {
createNewIA(device);
}
return _iaPool[_nextFreeIAHandle++];
return createNewIA(device);
}

void UIMeshBuffer::uploadBuffers() {
uint32_t byteOffset = getByteOffset();
bool dirty = getDirty();
if (_meshBufferLayout == nullptr || byteOffset == 0 || !dirty || _iaPool.empty()) {
if (_meshBufferLayout == nullptr || byteOffset == 0 || !dirty || !_ia) {
return;
}

uint32_t indexCount = getIndexOffset();
uint32_t byteCount = getByteOffset();

gfx::InputAssembler* ia = _iaPool[0];
gfx::BufferList vBuffers = ia->getVertexBuffers();
gfx::BufferList vBuffers = _ia->getVertexBuffers();
if (!vBuffers.empty()) {
gfx::Buffer* vBuffer = vBuffers[0];
if (byteCount > vBuffer->getSize()) {
vBuffer->resize(byteCount);
}
vBuffer->update(_vData);
}
gfx::Buffer* iBuffer = ia->getIndexBuffer();
gfx::Buffer* iBuffer = _ia->getIndexBuffer();
if (indexCount * 2 > iBuffer->getSize()) {
iBuffer->resize(indexCount * 2);
}
Expand All @@ -139,7 +126,7 @@ void UIMeshBuffer::recycleIA(gfx::InputAssembler* ia) {
}

gfx::InputAssembler* UIMeshBuffer::createNewIA(gfx::Device* device) {
if (_iaPool.empty()) {
if (!_ia) {
uint32_t vbStride = _vertexFormatBytes;
uint32_t ibStride = sizeof(uint16_t);

Expand All @@ -159,11 +146,10 @@ gfx::InputAssembler* UIMeshBuffer::createNewIA(gfx::Device* device) {
_iaInfo.attributes = _attributes;
_iaInfo.vertexBuffers.emplace_back(vertexBuffer);
_iaInfo.indexBuffer = indexBuffer;
_ia = device->createInputAssembler(_iaInfo);
}
auto* ia = device->createInputAssembler(_iaInfo);
_iaPool.emplace_back(ia);

return ia;
return _ia;
}

void UIMeshBuffer::syncSharedBufferToNative(uint32_t* buffer) {
Expand Down
5 changes: 3 additions & 2 deletions native/cocos/2d/renderer/UIMeshBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
****************************************************************************/

#pragma once
#include "base/Ptr.h"
#include "base/Macros.h"
#include "base/TypeDef.h"
#include "renderer/gfx-base/GFXInputAssembler.h"
#include "renderer/gfx-base/GFXDef-common.h"

namespace cc {
Expand Down Expand Up @@ -84,10 +86,9 @@ class UIMeshBuffer final {
uint32_t _vertexFormatBytes{0};
uint32_t _initVDataCount{0};
uint32_t _initIDataCount{0};
uint32_t _nextFreeIAHandle{0};

ccstd::vector<gfx::Attribute> _attributes;
ccstd::vector<gfx::InputAssembler*> _iaPool{};
IntrusivePtr<gfx::InputAssembler> _ia;
gfx::InputAssemblerInfo _iaInfo;

bool _dirty{false};
Expand Down
17 changes: 9 additions & 8 deletions native/cocos/renderer/pipeline/InstancedBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
}

for (auto &instance : _instances) {
if (instance.ia->getIndexBuffer() != sourceIA->getIndexBuffer() || instance.count >= MAX_CAPACITY) {
if (instance.ia->getIndexBuffer() != sourceIA->getIndexBuffer() || instance.drawInfo.instanceCount >= MAX_CAPACITY) {
continue;
}

Expand All @@ -99,7 +99,7 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
if (instance.stride != stride) {
continue;
}
if (instance.count >= instance.capacity) { // resize buffers
if (instance.drawInfo.instanceCount >= instance.capacity) { // resize buffers
instance.capacity <<= 1;
const auto newSize = instance.stride * instance.capacity;
instance.data = static_cast<uint8_t *>(CC_REALLOC(instance.data, newSize));
Expand All @@ -111,7 +111,7 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
if (instance.descriptorSet != descriptorSet) {
instance.descriptorSet = descriptorSet;
}
memcpy(instance.data + instance.stride * instance.count++, attrs.buffer.buffer()->getData(), stride);
memcpy(instance.data + instance.stride * instance.drawInfo.instanceCount++, attrs.buffer.buffer()->getData(), stride);
_hasPendingModels = true;
return;
}
Expand Down Expand Up @@ -144,24 +144,25 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
vertexBuffers.emplace_back(vb);
const gfx::InputAssemblerInfo iaInfo = {attributes, vertexBuffers, indexBuffer};
auto *ia = _device->createInputAssembler(iaInfo);
InstancedItem item = {1, INITIAL_CAPACITY, vb, data, ia, stride, shader, descriptorSet,
lightingMap, reflectionProbeCubemap, reflectionProbePlanarMap, reflectionProbeType, reflectionProbeBlendCubemap};
InstancedItem item = {INITIAL_CAPACITY, vb, data, ia, stride, shader, descriptorSet,
lightingMap, reflectionProbeCubemap, reflectionProbePlanarMap, reflectionProbeType, reflectionProbeBlendCubemap,
ia->getDrawInfo()};
item.drawInfo.instanceCount = 1;
_instances.emplace_back(item);
_hasPendingModels = true;
}

void InstancedBuffer::uploadBuffers(gfx::CommandBuffer *cmdBuff) const {
for (const auto &instance : _instances) {
if (!instance.count) continue;
if (!instance.drawInfo.instanceCount) continue;

cmdBuff->updateBuffer(instance.vb, instance.data, instance.vb->getSize());
instance.ia->setInstanceCount(instance.count);
}
}

void InstancedBuffer::clear() {
for (auto &instance : _instances) {
instance.count = 0;
instance.drawInfo.instanceCount = 0;
}
_hasPendingModels = false;
}
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/pipeline/InstancedBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace pipeline {
struct PSOInfo;

struct CC_DLL InstancedItem {
uint32_t count = 0;
uint32_t capacity = 0;
gfx::Buffer *vb = nullptr;
uint8_t *data = nullptr;
Expand All @@ -51,6 +50,7 @@ struct CC_DLL InstancedItem {
gfx::Texture *reflectionProbePlanarMap = nullptr;
uint32_t reflectionProbeType = 0;
gfx::Texture *reflectionProbeBlendCubemap = nullptr;
gfx::DrawInfo drawInfo;
};
using InstancedItemList = ccstd::vector<InstancedItem>;
using DynamicOffsetList = ccstd::vector<uint32_t>;
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/pipeline/RenderInstancedQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void RenderInstancedQueue::recordCommandBuffer(gfx::Device * /*device*/, gfx::Re
cmdBuffer->bindDescriptorSet(materialSet, pass->getDescriptorSet());
gfx::PipelineState *lastPSO = nullptr;
for (const auto &instance : instances) {
if (!instance.count) {
if (!instance.drawInfo.instanceCount) {
continue;
}
auto *pso = PipelineStateManager::getOrCreatePipelineState(pass, instance.shader, instance.ia, renderPass);
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/pipeline/UIPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void UIPhase::render(scene::Camera *camera, gfx::RenderPass *renderPass) {
cmdBuff->bindDescriptorSet(materialSet, pass->getDescriptorSet());
cmdBuff->bindInputAssembler(inputAssembler);
cmdBuff->bindDescriptorSet(localSet, ds);
cmdBuff->draw(inputAssembler);
cmdBuff->draw(batch->getDrawInfo());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/pipeline/custom/NativeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ void submitUICommands(
cmdBuff->bindInputAssembler(inputAssembler);
cmdBuff->bindDescriptorSet(
static_cast<uint32_t>(pipeline::SetIndex::LOCAL), ds);
cmdBuff->draw(inputAssembler);
cmdBuff->draw(batch->getDrawInfo());
}
}
}
Expand Down
Loading

0 comments on commit ab4ce17

Please sign in to comment.