gfx代码改进、rt纹理泄露修复

This commit is contained in:
2026-08-24 18:33:17 +08:00
parent 20700226bd
commit fe4cdfebc0
5 changed files with 91 additions and 72 deletions
+3
View File
@@ -132,6 +132,9 @@ inline Color colorInverse(const Color& col)
return ret;
}
/**
* @brief 不透明(a通道为1)
*/
inline bool isColorOpaque(const Color& col)
{
return col[3] == 1.0f;
+11 -1
View File
@@ -162,11 +162,21 @@ void Panel::Update()
return;
}
_bUpdate = true;
// 先优先级组件(drag)
for (Components* cpt : _allEle)
{
_update_cpt_drag(cpt);
}
// 位置刷新
if (_bNeedRefreshPos)
{
_bNeedRefreshPos = false;
_refresh_pos(GetParent()->GetSize());
}
// 后优先级组件
for (Components* cpt : _allEle)
{
if (Cpt::ElementData* comp = cpt->Get<Cpt::ElementData>())
@@ -179,7 +189,6 @@ void Panel::Update()
if (Cpt::Update* comp = cpt->Get<Cpt::Update>())
comp->Call();
_update_cpt_drag(cpt);
_update_cpt_radio(cpt);
}
_update_child();
@@ -539,6 +548,7 @@ void Panel::_update_cpt_drag(Components* cpt)
if (drag->_bControlPanel)
{
// drag->Update 已更新
_posPixel += pos_dt;
//log_info("拖拽:{} dt {}", to_string(_posPixel), to_string(pos_dt));
}
+44 -46
View File
@@ -1387,7 +1387,6 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i
}
// 通用视口
VkViewport viewport{};
viewport.x = 0.0f;
@@ -1402,6 +1401,36 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i
scissor.offset = { 0, 0 };
scissor.extent = _extentSwapChain;
// gfx通用绘制函数
auto fn_render_gfx = [&](bool is_2d, bool trans)
{
size_t offset = 0;
if (trans)
offset = 6;
if (is_2d)
offset += 3;
size_t index_uniform = _currentFrame + (is_2d ? _countFrame : 0);
for (size_t i = 0; i < 3; ++i)
{
size_t index = i + offset;
size_t index_pipeline = i + (trans ? 3 : 0);
// 绑定管线
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._vecGraphicsPipeline[index_pipeline]);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._pipelineLayout, 0, 1, &_gfx._descriptorSets[index_uniform], 0, nullptr);
VertexBufferGfx& vb = _gfx._arrVB[index];
VkBuffer vertexBuffers[] = { vb[_currentFrame]._vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertexBuffers, offsets);
// 绘制
vkCmdDraw(command_buffer, (uint32_t)vb[_currentFrame]._index, 1, 0, 0);
}
};
{
// 渲染流程3d Begin
VKRenderTarget& rt = _allRenderTarget[0];
@@ -1455,6 +1484,14 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i
RenderModel(command_buffer, model, -1);
}
}
// todo render pass 修正
////--------------------------------3d gfx------------------------------------
//// 设置视口
//vkCmdSetViewport(command_buffer, 0, 1, &viewport);
//// 设置裁剪
//vkCmdSetScissor(command_buffer, 0, 1, &scissor);
//fn_render_gfx(false, false);
//fn_render_gfx(false, true);
}
vkCmdEndRenderPass(command_buffer);
}
@@ -1561,31 +1598,12 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i
vkCmdBindIndexBuffer(command_buffer, _2d._indexBuffer, 0, VK_INDEX_TYPE_UINT32);
fn_render_dc(_2d._vecDc, false);
}
//--------------------gfx 不透明----------------
//--------------------2d gfx 不透明----------------
// 设置视口
vkCmdSetViewport(command_buffer, 0, 1, &viewport);
// 设置裁剪
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
// 绑定顶点缓冲区
for (size_t i = 0; i < NUM_GFX_HALF; ++i)
{
size_t index = _currentFrame;
if (i >= NUM_GFX_QUAD)
index += _countFrame;
// 绑定管线
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._vecGraphicsPipeline[i % NUM_GFX_HALF]);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._pipelineLayout, 0, 1, &_gfx._descriptorSets[index], 0, nullptr);
VertexBufferGfx& vb = _gfx._arrVB[i];
VkBuffer vertexBuffers[] = { vb[_currentFrame]._vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertexBuffers, offsets);
// 绘制
vkCmdDraw(command_buffer, (uint32_t)vb[_currentFrame]._index, 1, 0, 0);
}
fn_render_gfx(true, false);
//--------------------2d 透明----------------------
if (!_2d._vecDcTrans.empty())
{
@@ -1597,32 +1615,12 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i
fn_render_dc(_2d._vecDcTrans, true);
}
//--------------------gfx 透明----------------
//--------------------2d gfx 透明----------------
// 设置视口
vkCmdSetViewport(command_buffer, 0, 1, &viewport);
// 设置裁剪
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
//绑定顶点缓冲区
for (size_t i = NUM_GFX_HALF; i < NUM_GFX; ++i)
{
size_t index = _currentFrame;
if (i >= NUM_GFX_HALF + NUM_GFX_QUAD)
index += _countFrame;
//绑定管线
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._vecGraphicsPipeline[NUM_GFX_QUAD + (i - NUM_GFX_HALF) % 3]);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_gfx._pipelineLayout, 0, 1, &_gfx._descriptorSets[index], 0, nullptr);
VertexBufferGfx& vb = _gfx._arrVB[i];
VkBuffer vertexBuffers[] = { vb[_currentFrame]._vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertexBuffers, offsets);
//绘制
vkCmdDraw(command_buffer, (uint32_t)vb[_currentFrame]._index, 1, 0, 0);
}
fn_render_gfx(true, true);
//结束
vkCmdEndRenderPass(command_buffer);
}
@@ -3976,7 +3974,6 @@ void VK::_create_gfx()
//填充描述符
for (size_t i = 0; i < num; ++i)
{
size_t index = 0 * num + i;
//ubo
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = _gfx._uniformBuffers[i];
@@ -3985,7 +3982,7 @@ void VK::_create_gfx()
std::array<VkWriteDescriptorSet, 1> descriptorWrites{};
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = _gfx._descriptorSets[index];
descriptorWrites[0].dstSet = _gfx._descriptorSets[i];
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
@@ -5894,6 +5891,7 @@ void VK::UpdateCanvasRenderTarget(size_t rt_id, VKRenderTarget& rt)
{
Canvas* canvas = GetCanvas(rt_id);
assert(canvas);
canvas->_texture.Delete(_device);
canvas->_texture = rt._texColorOut;
//填充描述符
+32 -24
View File
@@ -154,12 +154,25 @@ constexpr bool ENABLE_VALIDATION_LAYERS = false;
// 最大演员实例数量
constexpr size_t NUM_MAX_ACTOR = 10240;
// gfx顶点数
constexpr std::size_t NUM_GFX_VERTEX = 1024;
// gfx数量(点、线、三角形、2d点、2d线、2d三角形)透明再x2
constexpr std::size_t NUM_GFX = 12;
constexpr std::size_t NUM_GFX_HALF = NUM_GFX / 2;
constexpr std::size_t NUM_GFX_QUAD = NUM_GFX / 4;
// gfx顶点数(默认,会自动扩容)
constexpr size_t NUM_GFX_VERTEX = 1024;
// gfx定义(请勿修改)
constexpr size_t INDEX_GFX_3D_NORMAL_DOT = 0;
constexpr size_t INDEX_GFX_3D_NORMAL_LINE = 1;
constexpr size_t INDEX_GFX_3D_NORMAL_TRIANGLE = 2;
constexpr size_t INDEX_GFX_2D_NORMAL_DOT = 3;
constexpr size_t INDEX_GFX_2D_NORMAL_LINE = 4;
constexpr size_t INDEX_GFX_2D_NORMAL_TRIANGLE = 5;
constexpr size_t INDEX_GFX_3D_TRANS_DOT = 6;
constexpr size_t INDEX_GFX_3D_TRANS_LINE = 7;
constexpr size_t INDEX_GFX_3D_TRANS_TRIANGLE = 8;
constexpr size_t INDEX_GFX_2D_TRANS_DOT = 9;
constexpr size_t INDEX_GFX_2D_TRANS_LINE = 10;
constexpr size_t INDEX_GFX_2D_TRANS_TRIANGLE = 11;
constexpr size_t NUM_GFX = 12;
constexpr size_t NUM_GFX_HALF = 6;
constexpr size_t NUM_GFX_QUAD = 3;
// 持久映射2d ubo(效率似乎差不多)
constexpr bool USE_MAPPED_2D_UBO = false;
@@ -483,6 +496,7 @@ public:
}
};
struct VKGfx
{
struct UBO
@@ -491,7 +505,9 @@ public:
};
VkPipelineLayout _pipelineLayout; //管线布局
// 3d和2d共用管线,但透明度需要区分
// 点、线、三角形(半透明x2)
std::array<VkPipeline, NUM_GFX_HALF> _vecGraphicsPipeline;// 图形管线
// uniform为 帧数 * 2 (3d,2d 各一个)
VkDescriptorSetLayout _descriptorSetLayout; //描述符集合布局
std::vector<VkBuffer> _uniformBuffers; //统一缓冲
std::vector<VkDeviceMemory> _uniformBuffersMemory; //统一缓冲内存
@@ -515,7 +531,6 @@ public:
vkDestroyFramebuffer(device, _frameBuffer, nullptr);
_texColor.Delete(device);
_texDepth.Delete(device);
//_texColorOut.Delete(device);
}
};
@@ -950,17 +965,6 @@ public:
~VK();
private:
//struct Pipeline
//{
// VkPipelineLayout _layout; // 管线布局
// VkPipeline _pipe; // 图形管线
// void Delete(VkDevice device)
// {
// vkDestroyPipeline(device, _pipe, nullptr);
// vkDestroyPipelineLayout(device, _layout, nullptr);
// }
//};
VkInstance _instance; // vk实例
std::vector<VkExtensionProperties> _allExtension; // 所有扩展,返回获得
@@ -1304,6 +1308,10 @@ public:
}_param;
uint32_t GetCurrentFrame()
{
return _currentFrame;
}
void GfxLock()
{
@@ -1328,7 +1336,7 @@ public:
void GfxPushDot(const VertexGfx& v)
{
bool opaque = isColorOpaque(v._color);
VertexBufferGfx& vb = _gfx._arrVB[0 + !opaque * NUM_GFX_HALF];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_3D_NORMAL_DOT : INDEX_GFX_3D_TRANS_DOT];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v);
@@ -1336,7 +1344,7 @@ public:
void GfxPushLine(const VertexGfx& v0, const VertexGfx& v1)
{
bool opaque = isColorOpaque(v0._color) && isColorOpaque(v1._color);
VertexBufferGfx& vb = _gfx._arrVB[1 + !opaque * NUM_GFX_HALF];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_3D_NORMAL_LINE : INDEX_GFX_3D_TRANS_LINE];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v0);
@@ -1345,7 +1353,7 @@ public:
void GfxPushTriangle(const VertexGfx& v0, const VertexGfx& v1, const VertexGfx& v2)
{
bool opaque = isColorOpaque(v0._color) && isColorOpaque(v1._color) && isColorOpaque(v2._color);
VertexBufferGfx& vb = _gfx._arrVB[2 + !opaque * NUM_GFX_HALF];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_3D_NORMAL_TRIANGLE : INDEX_GFX_3D_TRANS_TRIANGLE];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v0);
@@ -1355,7 +1363,7 @@ public:
void GfxPushDot2D(const VertexGfx& v)
{
bool opaque = isColorOpaque(v._color);
VertexBufferGfx& vb = _gfx._arrVB[0 + !opaque * NUM_GFX_HALF + NUM_GFX_QUAD];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_2D_NORMAL_DOT : INDEX_GFX_2D_TRANS_DOT];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v);
@@ -1363,7 +1371,7 @@ public:
void GfxPushLine2D(const VertexGfx& v0, const VertexGfx& v1)
{
bool opaque = isColorOpaque(v0._color) && isColorOpaque(v1._color);
VertexBufferGfx& vb = _gfx._arrVB[1 + !opaque * NUM_GFX_HALF + NUM_GFX_QUAD];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_2D_NORMAL_LINE : INDEX_GFX_2D_TRANS_LINE];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v0);
@@ -1372,7 +1380,7 @@ public:
void GfxPushTriangle2D(const VertexGfx& v0, const VertexGfx& v1, const VertexGfx& v2)
{
bool opaque = isColorOpaque(v0._color) && isColorOpaque(v1._color) && isColorOpaque(v2._color);
VertexBufferGfx& vb = _gfx._arrVB[2 + !opaque * NUM_GFX_HALF + NUM_GFX_QUAD];
VertexBufferGfx& vb = _gfx._arrVB[opaque ? INDEX_GFX_2D_NORMAL_TRIANGLE : INDEX_GFX_2D_TRANS_TRIANGLE];
if (!_check_gfx_vertex(vb))
return;
vb[_currentFrame].PushDot(v0);