diff --git a/dl/render/dl_factory.cpp b/dl/render/dl_factory.cpp index a55a0dd7..13858e75 100644 --- a/dl/render/dl_factory.cpp +++ b/dl/render/dl_factory.cpp @@ -306,6 +306,7 @@ gcSprite Factory::CreateSpriteDefault(const Color& col /*= ColorDef::WHITE*/) spr->_col[i] = col; spr->_uv[i] = spr_info->_uv[i]; } + spr->_sampler = _samplerNormal; #ifndef NDEBUG spr->_name = ":"; #endif @@ -337,6 +338,7 @@ gcSprite Factory::CreateSprite(const SpriteID& spr_id) spr->_order = Z_DEFAULT; for (int i = 0; i != 4; ++i) spr->_uv[i] = spr_info->_uv[i]; + spr->_sampler = _samplerNormal; #ifndef NDEBUG spr->_name = GetSpriteName(spr_id); #endif @@ -368,6 +370,7 @@ dl::gcSprite Factory::CreateSprite(size_t rt_id) spr->_uv[2] = { 1.0f, 1.0f }; spr->_uv[3] = { 0.0f, 1.0f }; + spr->_sampler = _samplerNormal; #ifndef NDEBUG spr->_name = fmt::format("RenderTarget: {}", rt_id); #endif @@ -2300,7 +2303,9 @@ Factory::Factory() : _bUseBinary{ false }, _bOrderPickupShow{ false }, _idTemp{ 0 }, - _orderPickup{ 1.0f } + _orderPickup{ 1.0f }, + _samplerNormal{ Sprite::Sampler::NORMAL }, + _samplerUI{ Sprite::Sampler::UI } { _imp = new FactoryImp; } diff --git a/dl/render/dl_factory.h b/dl/render/dl_factory.h index 928bf714..b1da49a0 100644 --- a/dl/render/dl_factory.h +++ b/dl/render/dl_factory.h @@ -64,6 +64,21 @@ public: _strPathTemp = path_temp; } /** + * @brief 设置精灵的默认采样模式 + */ + void SetSpriteSampler(Sprite::Sampler sampler_normal, Sprite::Sampler sampler_ui) + { + _samplerNormal = sampler_normal; + _samplerUI = sampler_ui; + } + /** + * @brief 返回精灵的默认采样模式 + */ + Sprite::Sampler GetSpriteSampler(bool is_ui) + { + return is_ui ? _samplerUI : _samplerNormal; + } + /** * @brief 设置发布模式,使用binary资源 */ void SetReleaseMode() @@ -513,6 +528,9 @@ private: std::size_t _idTemp; //临时文件使用id float _orderPickup; // ui精灵拾取order + Sprite::Sampler _samplerNormal; // 精灵默认的采样方法 + Sprite::Sampler _samplerUI; // ui精灵默认的采用方法 + //颜色tag std::unordered_map _hashTagColor; diff --git a/dl/render/dl_sprite.cpp b/dl/render/dl_sprite.cpp index f2bb0f7a..11b6ca69 100644 --- a/dl/render/dl_sprite.cpp +++ b/dl/render/dl_sprite.cpp @@ -115,6 +115,15 @@ void Sprite::SetColor(const Color& col, size_t index) } +void Sprite::SetUI(bool ui, bool change_sampler /*= true*/) +{ + _ui = ui; + if (change_sampler) + { + SetSampler(g_factory->GetSpriteSampler(_ui)); + } +} + void Sprite::SetUV(size_t index, const UV& uv) { if (index >= _uv.size()) diff --git a/dl/render/dl_sprite.h b/dl/render/dl_sprite.h index 3ed1bd40..89c06fcf 100644 --- a/dl/render/dl_sprite.h +++ b/dl/render/dl_sprite.h @@ -167,26 +167,26 @@ public: } /** * @brief 设置采样器,对于像素图来说应该设置为 SamplerType::PIXEL + * @note SetUI会默认设置为 SamplerType::UI */ void SetSampler(Sampler type) { - _sampler = (unsigned)type; + _sampler = type; } /** * @brief 设置混合模式 */ void SetBlend(Blend type) { - _blend = (unsigned)type; + _blend = type; } /** * @brief 设置后为ui精灵,会屏蔽order大于自己的ui精灵的pickup响应 + * @param[in] ui 是否为ui精灵 + * @param[in] change_sampler 是否自动修改sampler */ - void SetUI(bool ui) - { - _ui = ui; - } + void SetUI(bool ui, bool change_sampler = true); /** * @brief 设置裁剪(世界坐标系) @@ -253,8 +253,8 @@ private: _coor(nullptr), _viewport(nullptr), _scissorRect(nullptr), - _sampler(0), - _blend{ 0 }, + _sampler{ Sampler::NORMAL }, + _blend{ Blend::NORMAL }, _bright{ 1.0f }, _ui(false), _bRenderTarget{ false }, @@ -271,8 +271,8 @@ private: Coor* _coor; Position4* _viewport; Rect* _scissorRect; - unsigned _sampler; - unsigned _blend; + Sampler _sampler; + Blend _blend; float _bright; // 亮度 float _order;// 与z等价 diff --git a/dl/vk/dl_vk.cpp b/dl/vk/dl_vk.cpp index 779b5930..0b5b6622 100644 --- a/dl/vk/dl_vk.cpp +++ b/dl/vk/dl_vk.cpp @@ -1523,13 +1523,17 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i _2d._renderInfo._numSwitchPso = 0; auto fn_render_dc = [&](const std::vector& list_dc, bool is_trans) { + // 2d公共描述符 + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, + _2d._pipelineLayout, 0, 1, &_2d._descriptorSets0[_currentFrame], 0, nullptr); + for (const DrawCall& dc : list_dc) { // 切换管线 VkPipeline pipeline; - if (dc._blend == 1) + if (dc._blend == Sprite::Blend::EFFECT) pipeline = _2d._graphicsPipelineEffect; - else if (dc._blend == 2) + else if (dc._blend == Sprite::Blend::GRAY) pipeline = _2d._graphicsPipelineGray; else pipeline = is_trans ? _2d._graphicsPipelineTrans : _2d._graphicsPipeline; @@ -1571,12 +1575,17 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i uint32_t offset_tex = (uint32_t)(dc._canvas->_id * _countFrame + _currentFrame); vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, - _2d._pipelineLayout, 0, 1, &_2d._descriptorSets[offset_tex], 0, nullptr); + _2d._pipelineLayout, 1, 1, &_2d._descriptorSets1[offset_tex], 0, nullptr); // 推送常量(变换) size_t id_coor = dc._rootCoor ? dc._rootCoor->GetRootIndex() : 0; - vkCmdPushConstants(command_buffer, _2d._pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, - 0, sizeof(VK2D::CBO), &_2d._allCBO[id_coor]); + VK2D::CBO cbo; + cbo._model = _2d._allRootCoorMat[id_coor]; + cbo._samplerIndex = (uint32_t)dc._sampler; + assert(cbo._samplerIndex < NUM_2D_SAMPLER); + vkCmdPushConstants(command_buffer, _2d._pipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 0, sizeof(VK2D::CBO), &cbo); // 绑定顶点缓冲区 VkBuffer vertexBuffers[] = { dc._canvas->_vertexBuffer[_currentFrame] }; @@ -1590,7 +1599,6 @@ void VK::_record_command_buffer(VkCommandBuffer command_buffer, uint32_t index_i } }; - //------------------2d 不透明---------------- if (!_2d._vecDc.empty()) { @@ -3671,6 +3679,7 @@ void VK::_create_2d() { //------------------------------2d-------------------------------- // 0.描述符布局 + //-------------------------set 0 : ubo + sampler3 // ubo VkDescriptorSetLayoutBinding uboLayoutBinding{}; uboLayoutBinding.binding = 0; @@ -3678,42 +3687,74 @@ void VK::_create_2d() uboLayoutBinding.descriptorCount = 1; uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; uboLayoutBinding.pImmutableSamplers = nullptr; // Optional - // 贴图 - VkDescriptorSetLayoutBinding texLayoutBinding0{}; - texLayoutBinding0.binding = 1; - texLayoutBinding0.descriptorCount = 1; - texLayoutBinding0.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - texLayoutBinding0.pImmutableSamplers = nullptr; - texLayoutBinding0.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - std::array bindings = { - uboLayoutBinding, texLayoutBinding0 + // 采样器数组 + VkDescriptorSetLayoutBinding samplerLayoutBinding{}; + samplerLayoutBinding.binding = 1; + samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; + samplerLayoutBinding.descriptorCount = 3; // 三个采样器 + samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;// 片段阶段使用 + samplerLayoutBinding.pImmutableSamplers = nullptr; + + + std::array bindings0 = { + uboLayoutBinding, samplerLayoutBinding }; VkDescriptorSetLayoutCreateInfo layoutInfo{}; layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - layoutInfo.bindingCount = (uint32_t)bindings.size(); - layoutInfo.pBindings = bindings.data(); + layoutInfo.bindingCount = (uint32_t)bindings0.size(); + layoutInfo.pBindings = bindings0.data(); if (vkCreateDescriptorSetLayout(_device, &layoutInfo, nullptr, - &_2d._descriptorSetLayout) != VK_SUCCESS) + &_2d._descriptorSetLayout0) != VK_SUCCESS) { log_damage0("创建描述符集合失败!"); return; } + //-------------------------set 1 : texture + // 贴图 + VkDescriptorSetLayoutBinding texLayoutBinding{}; + texLayoutBinding.binding = 0; + texLayoutBinding.descriptorCount = 1; + texLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; + texLayoutBinding.pImmutableSamplers = nullptr; + texLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; + + std::array bindings1 = { + texLayoutBinding + }; + + VkDescriptorSetLayoutCreateInfo layoutInfo1{}; + layoutInfo1.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + layoutInfo1.bindingCount = (uint32_t)bindings1.size(); + layoutInfo1.pBindings = bindings1.data(); + + if (vkCreateDescriptorSetLayout(_device, &layoutInfo1, nullptr, + &_2d._descriptorSetLayout1) != VK_SUCCESS) + { + log_damage0("创建描述符集合1失败!"); + return; + } + // 2.管线布局 // 推送常量区 VkPushConstantRange pushConstantRange{}; - pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; pushConstantRange.offset = 0; pushConstantRange.size = sizeof(VK2D::CBO); // 管线布局 + std::array setLayouts = { + _2d._descriptorSetLayout0, // set 0 + _2d._descriptorSetLayout1 // set 1 + }; + VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pipelineLayoutInfo.setLayoutCount = 1; // Optional - pipelineLayoutInfo.pSetLayouts = &_2d._descriptorSetLayout; // Optional + pipelineLayoutInfo.setLayoutCount = (uint32_t)setLayouts.size(); + pipelineLayoutInfo.pSetLayouts = setLayouts.data(); pipelineLayoutInfo.pushConstantRangeCount = 1; pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; @@ -3810,38 +3851,96 @@ void VK::_create_2d() vkMapMemory(_device, _2d._uniformBuffersMemory[i], 0, bufferSize, 0, &_2d._uniformBuffersMapped[i]); } // 5.描述符池 - std::array poolSizes{}; + std::array poolSizes{}; poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - poolSizes[0].descriptorCount = _get_2d_max_descriptor_count(); - poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - poolSizes[1].descriptorCount = _get_2d_max_descriptor_count(); + poolSizes[0].descriptorCount = _countFrame; + poolSizes[1].type = VK_DESCRIPTOR_TYPE_SAMPLER; + poolSizes[1].descriptorCount = _countFrame * NUM_2D_SAMPLER; + poolSizes[2].type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; + poolSizes[2].descriptorCount = NUM_MAX_DRAW_CALL_2D; VkDescriptorPoolCreateInfo poolInfo{}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; poolInfo.poolSizeCount = (uint32_t)poolSizes.size(); poolInfo.pPoolSizes = poolSizes.data(); - poolInfo.maxSets = (uint32_t)(poolSizes.size() * _get_2d_max_descriptor_count()); + poolInfo.maxSets = (uint32_t)(_countFrame + NUM_MAX_DRAW_CALL_2D); if (vkCreateDescriptorPool(_device, &poolInfo, nullptr, &_2d._descriptorPool) != VK_SUCCESS) { log_err0("创建描述符池失败!"); return; } - //6.分配描述符 - size_t num = NUM_MAX_DRAW_CALL_2D * _countFrame; - - std::vector layouts(num, _2d._descriptorSetLayout); - VkDescriptorSetAllocateInfo allocInfo{}; - allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - allocInfo.descriptorPool = _2d._descriptorPool; - allocInfo.descriptorSetCount = (uint32_t)num; - allocInfo.pSetLayouts = layouts.data(); - - _2d._descriptorSets.resize(num); - if (vkAllocateDescriptorSets(_device, &allocInfo, _2d._descriptorSets.data()) != VK_SUCCESS) + // 6.分配描述符 { - log_err0("分配2d描述符集失败!"); - return; + std::vector layouts(_countFrame, _2d._descriptorSetLayout0); + VkDescriptorSetAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = _2d._descriptorPool; + allocInfo.descriptorSetCount = _countFrame; + allocInfo.pSetLayouts = layouts.data(); + + _2d._descriptorSets0.resize(allocInfo.descriptorSetCount); + if (vkAllocateDescriptorSets(_device, &allocInfo, _2d._descriptorSets0.data()) != VK_SUCCESS) + { + log_err0("分配2d描述符集失败!"); + return; + } + } + + { + std::vector layouts(NUM_MAX_DRAW_CALL_2D, _2d._descriptorSetLayout1); + VkDescriptorSetAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = _2d._descriptorPool; + allocInfo.descriptorSetCount = NUM_MAX_DRAW_CALL_2D; + allocInfo.pSetLayouts = layouts.data(); + + _2d._descriptorSets1.resize(allocInfo.descriptorSetCount); + if (vkAllocateDescriptorSets(_device, &allocInfo, _2d._descriptorSets1.data()) != VK_SUCCESS) + { + log_err0("分配2d描述符集1失败!"); + return; + } + } + // 更新描述符(全局) + for (size_t i = 0; i < _countFrame; ++i) + { + // ubo + VkDescriptorBufferInfo bufferInfo{}; + bufferInfo.buffer = _2d._uniformBuffers[i]; + bufferInfo.offset = 0; + bufferInfo.range = sizeof(VK2D::UBO); + // 采样器 + std::array samplerInfos{}; + samplerInfos[0].sampler = _2d._textureSampler; + samplerInfos[1].sampler = _2d._textureSamplerPixel; + samplerInfos[2].sampler = _2d._textureSamplerUI; + for (auto& info : samplerInfos) + { + info.imageView = VK_NULL_HANDLE; + info.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED; + } + // + std::array descriptorWrites{}; + descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[0].dstSet = _2d._descriptorSets0[i]; + descriptorWrites[0].dstBinding = 0; + descriptorWrites[0].dstArrayElement = 0; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorCount = 1; + descriptorWrites[0].pBufferInfo = &bufferInfo; + + descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[1].dstSet = _2d._descriptorSets0[i]; + descriptorWrites[1].dstBinding = 1; + descriptorWrites[1].dstArrayElement = 0; + descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; + descriptorWrites[1].descriptorCount = (uint32_t)samplerInfos.size(); + descriptorWrites[1].pImageInfo = samplerInfos.data(); + + vkUpdateDescriptorSets(_device, + (uint32_t)descriptorWrites.size(), descriptorWrites.data(), + 0, nullptr); } // @@ -3849,9 +3948,15 @@ void VK::_create_2d() { iter = nullptr; } + for (auto& iter : _2d._allRootCoorMat) + { + iter.Identity(); + } + for (auto& iter : _2d._allCBO) { iter._model.Identity(); + iter._samplerIndex = 0; } } @@ -3882,7 +3987,8 @@ void VK::_delete_2d() vkFreeMemory(_device, _2d._uniformBuffersMemory[i], nullptr); } vkDestroyDescriptorPool(_device, _2d._descriptorPool, nullptr); - vkDestroyDescriptorSetLayout(_device, _2d._descriptorSetLayout, nullptr); + vkDestroyDescriptorSetLayout(_device, _2d._descriptorSetLayout1, nullptr); + vkDestroyDescriptorSetLayout(_device, _2d._descriptorSetLayout0, nullptr); vkDestroyPipelineLayout(_device, _2d._pipelineLayout, nullptr); } @@ -5913,33 +6019,21 @@ void VK::UpdateCanvasRenderTarget(size_t rt_id, VKRenderTarget& rt) for (size_t i = 0; i < _countFrame; ++i) { size_t index = id * _countFrame + i; - // ubo - VkDescriptorBufferInfo bufferInfo{}; - bufferInfo.buffer = _2d._uniformBuffers[i]; - bufferInfo.offset = 0; - bufferInfo.range = sizeof(VK2D::UBO); + // 纹理 VkDescriptorImageInfo imageInfo{}; imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageInfo.imageView = canvas->_texture._imageView; - imageInfo.sampler = _2d._textureSampler; + imageInfo.sampler = VK_NULL_HANDLE; - std::array descriptorWrites{}; + std::array descriptorWrites{}; descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrites[0].dstSet = _2d._descriptorSets[index]; + descriptorWrites[0].dstSet = _2d._descriptorSets1[index]; descriptorWrites[0].dstBinding = 0; descriptorWrites[0].dstArrayElement = 0; - descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; descriptorWrites[0].descriptorCount = 1; - descriptorWrites[0].pBufferInfo = &bufferInfo; - - descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrites[1].dstSet = _2d._descriptorSets[index]; - descriptorWrites[1].dstBinding = 1; - descriptorWrites[1].dstArrayElement = 0; - descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptorWrites[1].descriptorCount = 1; - descriptorWrites[1].pImageInfo = &imageInfo; + descriptorWrites[0].pImageInfo = &imageInfo; vkUpdateDescriptorSets(_device, (uint32_t)descriptorWrites.size(), descriptorWrites.data(), @@ -5954,33 +6048,21 @@ void VK::UpdateCanvasDescriptor(Canvas* canvas) for (size_t i = 0; i < _countFrame; ++i) { size_t index = id * _countFrame + i; - // ubo - VkDescriptorBufferInfo bufferInfo{}; - bufferInfo.buffer = _2d._uniformBuffers[i]; - bufferInfo.offset = 0; - bufferInfo.range = sizeof(VK2D::UBO); - // 纹理 + + std::array descriptorWrites{}; + VkDescriptorImageInfo imageInfo{}; imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageInfo.imageView = canvas->_texture._imageView; - imageInfo.sampler = _2d._textureSampler; + imageInfo.sampler = VK_NULL_HANDLE; - std::array descriptorWrites{}; descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrites[0].dstSet = _2d._descriptorSets[index]; + descriptorWrites[0].dstSet = _2d._descriptorSets1[index]; descriptorWrites[0].dstBinding = 0; descriptorWrites[0].dstArrayElement = 0; - descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; descriptorWrites[0].descriptorCount = 1; - descriptorWrites[0].pBufferInfo = &bufferInfo; - - descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrites[1].dstSet = _2d._descriptorSets[index]; - descriptorWrites[1].dstBinding = 1; - descriptorWrites[1].dstArrayElement = 0; - descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptorWrites[1].descriptorCount = 1; - descriptorWrites[1].pImageInfo = &imageInfo; + descriptorWrites[0].pImageInfo = &imageInfo; vkUpdateDescriptorSets(_device, (uint32_t)descriptorWrites.size(), descriptorWrites.data(), @@ -6489,7 +6571,7 @@ void VK::Update() v._pos[1] = (float)pos[1]; v._pos[2] = spr->_order; - if (spr->_blend == 1) + if (spr->_blend == Sprite::Blend::EFFECT) {// blend模式的 alpha需要作用到颜色上才能实现透明 v._color = spr->_col[i]; v._color[0] *= v._color[3]; @@ -6602,11 +6684,11 @@ void VK::Update() Coor* coor = _2d._allRootCoor[i]; if (coor) { - VK2D::CBO& cbo = _2d._allCBO[i]; + Matrix4& mat = _2d._allRootCoorMat[i]; Matrix4 mat_t = Mat::CreateTranslation(toLonger(coor->GetPosition())); Matrix4 mat_r = Mat::CreateRotation(Position3{0, 0, 1}, coor->GetRotate()); Matrix4 mat_s = Mat::CreateScaling(toLonger(coor->GetScale(), 1.0f)); - cbo._model = mat_t * mat_s * mat_r; + mat = mat_t * mat_s * mat_r; } } diff --git a/dl/vk/dl_vk.h b/dl/vk/dl_vk.h index 6124aee9..acb3e148 100644 --- a/dl/vk/dl_vk.h +++ b/dl/vk/dl_vk.h @@ -132,6 +132,8 @@ constexpr size_t NUM_START_2D_VERTEX = 4 * 1024; constexpr size_t NUM_MAX_ROOT_COOR = 32; // 2d纹理空闲时间后自动释放(毫秒) constexpr unsigned TIME_FREE_TEXTURE_2D = 60 * 1000; +// 2d采样器数量 +constexpr size_t NUM_2D_SAMPLER = 3; // 样本着色(更高级的多重采样,基本不会使用) constexpr bool OPEN_SAMPLE_SHADING = false; @@ -348,8 +350,8 @@ public: Coor* _rootCoor; Viewport* _viewport; Rect* _scissorRect; - unsigned _sampler; - unsigned _blend; + Sprite::Sampler _sampler; + Sprite::Blend _blend; // 现修改为顶点位置 size_t _beg; @@ -394,10 +396,10 @@ public: alignas(16) Matrix4 _viewProj; }; - // 暂未使用真正的常量缓冲,而是乘上_viewProj写入到UBO struct CBO { alignas(16) Matrix4 _model; + uint32_t _samplerIndex; }; @@ -406,12 +408,14 @@ public: VkPipeline _graphicsPipelineTrans; // 图形管线(alpha混合) VkPipeline _graphicsPipelineEffect; // 图形管线(特效混合) VkPipeline _graphicsPipelineGray; // 图形管线(灰度绘制) - VkDescriptorSetLayout _descriptorSetLayout; // 描述符集合布局 + VkDescriptorSetLayout _descriptorSetLayout0; // 描述符集合布局(全局) + VkDescriptorSetLayout _descriptorSetLayout1; // 描述符集合布局 std::vector _uniformBuffers; // 统一缓冲 std::vector _uniformBuffersMemory; // 统一缓冲内存 std::vector _uniformBuffersMapped; // 统一缓冲映射 VkDescriptorPool _descriptorPool; // 描述符池 - std::vector _descriptorSets; // 所有描述符集 + std::vector _descriptorSets0; // 所有描述符集 + std::vector _descriptorSets1; // 所有描述符集 VkBuffer _indexBuffer; // 索引缓冲(注意是32位) VkDeviceMemory _indexBufferMemory; // 索引缓冲内存 VkSampler _textureSampler; // 2d纹理采样器 @@ -420,6 +424,7 @@ public: std::array _allCBO; // 常量缓冲 size_t _curPushConstant; // 当前使用 常量缓冲 下标 std::array _allRootCoor; // 所有根坐标系 + std::array _allRootCoorMat; // 所有根坐标系矩阵 Matrix4 _mat2D; // 2d世界坐标系 std::unordered_map _allCanvas; // 画布(RenderTarget + 静态 + Register精灵) RenderInfo2D _renderInfo; // 绘制信息 @@ -1251,10 +1256,6 @@ private: //2d管线 void _create_2d(); void _delete_2d(); - uint32_t _get_2d_max_descriptor_count() - { - return _countFrame* NUM_MAX_DRAW_CALL_2D; - } // gfx管线 void _create_gfx(); diff --git a/project/hs b/project/hs index 1e73b1cb..6ca47050 160000 --- a/project/hs +++ b/project/hs @@ -1 +1 @@ -Subproject commit 1e73b1cbd19b568385836c310c26ef7c7ffa8876 +Subproject commit 6ca470507226147ea9c55b7746cd3ec55a07cdba diff --git a/res/dl/shader/2d_frag.spv b/res/dl/shader/2d_frag.spv index 19c3b050..0ae05144 100644 Binary files a/res/dl/shader/2d_frag.spv and b/res/dl/shader/2d_frag.spv differ diff --git a/res/dl/shader/2d_gray_frag.spv b/res/dl/shader/2d_gray_frag.spv index 5b88355a..ba5f5abf 100644 Binary files a/res/dl/shader/2d_gray_frag.spv and b/res/dl/shader/2d_gray_frag.spv differ diff --git a/res/dl/shader/2d_vert.spv b/res/dl/shader/2d_vert.spv index 13ce129b..96fe5673 100644 Binary files a/res/dl/shader/2d_vert.spv and b/res/dl/shader/2d_vert.spv differ diff --git a/shader/2d.frag b/shader/2d.frag index 972124f0..14d0ffbc 100644 --- a/shader/2d.frag +++ b/shader/2d.frag @@ -1,6 +1,13 @@ #version 450 -layout(binding = 1) uniform sampler2D sampDiffuse; +layout (push_constant) uniform PushConstant +{ + mat4 model; + uint samplerIndex; +} constant; + +layout(set = 0, binding = 1) uniform sampler samplers[3]; +layout(set = 1, binding = 0) uniform texture2D tex; layout(location = 0) in vec4 fragColor; layout(location = 1) in vec2 fragTexCoord; @@ -9,7 +16,7 @@ layout(location = 0) out vec4 outColor; void main() { - outColor = texture(sampDiffuse, fragTexCoord) * fragColor; + outColor = texture(sampler2D(tex, samplers[constant.samplerIndex]), fragTexCoord) * fragColor; if(outColor.a == 0) discard; } diff --git a/shader/2d.vert b/shader/2d.vert index 04613b11..e6844cfe 100644 --- a/shader/2d.vert +++ b/shader/2d.vert @@ -1,6 +1,6 @@ #version 450 -layout(binding = 0) uniform UniformBufferObject +layout(set = 0, binding = 0) uniform UniformBufferObject { mat4 viewProj; } ubo; @@ -8,6 +8,7 @@ layout(binding = 0) uniform UniformBufferObject layout (push_constant) uniform PushConstant { mat4 model; + uint samplerIndex; } constant; layout(location = 0) in vec3 inPosition; diff --git a/shader/2d_gray.frag b/shader/2d_gray.frag index fea293d9..704adb70 100644 --- a/shader/2d_gray.frag +++ b/shader/2d_gray.frag @@ -1,6 +1,13 @@ #version 450 -layout(binding = 1) uniform sampler2D sampDiffuse; +layout (push_constant) uniform PushConstant +{ + mat4 model; + uint samplerIndex; +} constant; + +layout(set = 0, binding = 1) uniform sampler samplers[3]; +layout(set = 1, binding = 0) uniform texture2D tex; layout(location = 0) in vec4 fragColor; layout(location = 1) in vec2 fragTexCoord; @@ -9,9 +16,9 @@ layout(location = 0) out vec4 outColor; void main() { - outColor = texture(sampDiffuse, fragTexCoord) * fragColor; + outColor = texture(sampler2D(tex, samplers[constant.samplerIndex]), fragTexCoord) * fragColor; if(outColor.a == 0) discard; float gray = dot(outColor.rgb, vec3(0.299, 0.587, 0.114)); outColor = vec4(gray, gray, gray, outColor.a); -} +} \ No newline at end of file