From bebb8e041af1b41ccb89e93dd048666f29eb048c Mon Sep 17 00:00:00 2001 From: wada Date: Thu, 25 Jan 2024 12:21:03 +0900 Subject: [PATCH] Update to Cubism 5 SDK for Native R1 beta4 --- CHANGELOG.md | 13 +++ src/CubismFramework.cpp | 4 +- src/Effect/CubismPose.cpp | 6 +- src/Model/CubismModelUserData.cpp | 6 ++ src/Model/CubismUserModel.cpp | 25 ++++++ src/Motion/CubismExpressionMotion.cpp | 5 ++ src/Motion/CubismMotion.cpp | 6 ++ .../Cocos2d/CubismRenderer_Cocos2dx.cpp | 8 +- .../Cocos2d/CubismRenderer_Cocos2dx.hpp | 6 +- .../Cocos2d/CubismShader_Cocos2dx.cpp | 16 ++-- src/Rendering/CubismClippingManager.hpp | 4 +- src/Rendering/CubismClippingManager.tpp | 53 ++++++----- src/Rendering/CubismRenderer.cpp | 2 +- src/Rendering/CubismRenderer.hpp | 2 +- src/Rendering/D3D11/CubismRenderer_D3D11.cpp | 44 ++------- src/Rendering/D3D9/CubismRenderer_D3D9.cpp | 14 +-- src/Rendering/Metal/CubismRenderer_Metal.hpp | 6 +- src/Rendering/Metal/CubismRenderer_Metal.mm | 4 +- src/Rendering/Metal/CubismShader_Metal.mm | 8 +- .../OpenGL/CubismRenderer_OpenGLES2.cpp | 4 +- .../OpenGL/CubismRenderer_OpenGLES2.hpp | 6 +- .../OpenGL/CubismShader_OpenGLES2.cpp | 16 ++-- src/Rendering/Vulkan/CubismClass_Vulkan.cpp | 9 +- src/Rendering/Vulkan/CubismClass_Vulkan.hpp | 9 +- .../Vulkan/CubismOffscreenSurface_Vulkan.cpp | 14 ++- .../Vulkan/CubismRenderer_Vulkan.cpp | 57 ++++++------ .../Vulkan/CubismRenderer_Vulkan.hpp | 8 +- src/Utils/CubismJson.cpp | 90 +++++++++++++++---- 28 files changed, 284 insertions(+), 161 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5162b03..5712225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [5-r.1-beta.4] - 2024-01-25 + +### Fixed + +* Fix memory leak in DX11. +* Fix an issue where models with a specific number of masks could not be drawn correctly. +* Fix to check for null when reading json. +* Fix an issue that caused some graphics drivers to not render correctly in Vulkan. +* Fix errors related to semaphore waiting stages. +* Fix errors that occurs when building with x86 in vulkan. + + ## [5-r.1-beta.3] - 2023-10-12 ### Added @@ -326,6 +338,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Fix invalid expressions of `CubismCdiJson`. +[5-r.1-beta.4]: https://github.com/Live2D/CubismNativeFramework/compare/5-r.1-beta.3...5-r.1-beta.4 [5-r.1-beta.3]: https://github.com/Live2D/CubismNativeFramework/compare/5-r.1-beta.2...5-r.1-beta.3 [5-r.1-beta.2]: https://github.com/Live2D/CubismNativeFramework/compare/5-r.1-beta.1...5-r.1-beta.2 [5-r.1-beta.1]: https://github.com/Live2D/CubismNativeFramework/compare/4-r.7...5-r.1-beta.1 diff --git a/src/CubismFramework.cpp b/src/CubismFramework.cpp index db72502..b6246e0 100644 --- a/src/CubismFramework.cpp +++ b/src/CubismFramework.cpp @@ -83,9 +83,9 @@ csmBool CubismFramework::StartUp(ICubismAllocator* allocator, const Option* opti const csmUint32 major = static_cast((version & 0xFF000000) >> 24); const csmUint32 minor = static_cast((version & 0x00FF0000) >> 16); const csmUint32 patch = static_cast((version & 0x0000FFFF)); - const csmUint32 vesionNumber = version; + const csmUint32 versionNumber = version; - CubismLogInfo("Live2D Cubism Core version: %02d.%02d.%04d (%d)", major, minor, patch, vesionNumber); + CubismLogInfo("Live2D Cubism Core version: %02d.%02d.%04d (%d)", major, minor, patch, versionNumber); } CubismLogInfo("CubismFramework::StartUp() is complete."); diff --git a/src/Effect/CubismPose.cpp b/src/Effect/CubismPose.cpp index 45ec145..38a6ddf 100644 --- a/src/Effect/CubismPose.cpp +++ b/src/Effect/CubismPose.cpp @@ -70,8 +70,12 @@ CubismPose::~CubismPose() CubismPose* CubismPose::Create(const csmByte* pose3json, csmSizeInt size) { - CubismPose* ret = CSM_NEW CubismPose(); Utils::CubismJson* json = Utils::CubismJson::Create(pose3json, size); + if (!json) + { + return NULL; + } + CubismPose* ret = CSM_NEW CubismPose(); Utils::Value& root = json->GetRoot(); // フェード時間の指定 diff --git a/src/Model/CubismModelUserData.cpp b/src/Model/CubismModelUserData.cpp index 853acb6..540fcc4 100644 --- a/src/Model/CubismModelUserData.cpp +++ b/src/Model/CubismModelUserData.cpp @@ -47,6 +47,12 @@ void CubismModelUserData::ParseUserData(const csmByte* buffer, const csmSizeInt { CubismModelUserDataJson* json = CSM_NEW CubismModelUserDataJson(buffer, size); + if (!json->IsValid()) + { + CSM_DELETE(json); + return; + } + const ModelUserDataType typeOfArtMesh = CubismFramework::GetIdManager()->GetId(ArtMesh); const csmUint32 nodeCount = json->GetUserDataCount(); diff --git a/src/Model/CubismUserModel.cpp b/src/Model/CubismUserModel.cpp index a0b3296..87963b5 100644 --- a/src/Model/CubismUserModel.cpp +++ b/src/Model/CubismUserModel.cpp @@ -102,21 +102,41 @@ void CubismUserModel::LoadModel(const csmByte* buffer, csmSizeInt size, csmBool ACubismMotion* CubismUserModel::LoadExpression(const csmByte* buffer, csmSizeInt size, const csmChar* name) { + if (!buffer) + { + CubismLogError("Failed to LoadExpression()."); + return NULL; + } + return CubismExpressionMotion::Create(buffer, size); } void CubismUserModel::LoadPose(const csmByte* buffer, csmSizeInt size) { _pose = CubismPose::Create(buffer, size); + if (!_pose) + { + CubismLogError("Failed to LoadPose()."); + } } void CubismUserModel::LoadPhysics(const csmByte* buffer, csmSizeInt size) { _physics = CubismPhysics::Create(buffer, size); + if (!_physics) + { + CubismLogError("Failed to LoadPhysics()."); + } } void CubismUserModel::LoadUserData(const csmByte* buffer, csmSizeInt size) { + if (!buffer) + { + CubismLogError("Failed to LoadUserData()."); + return; + } + _modelUserData = CubismModelUserData::Create(buffer, size); } csmBool CubismUserModel::IsHit(CubismIdHandle drawableId, csmFloat32 pointX, csmFloat32 pointY) @@ -170,6 +190,11 @@ csmBool CubismUserModel::IsHit(CubismIdHandle drawableId, csmFloat32 pointX, csm ACubismMotion* CubismUserModel::LoadMotion(const csmByte* buffer, csmSizeInt size, const csmChar* name, ACubismMotion::FinishedMotionCallback onFinishedMotionHandler) { + if (!buffer) + { + CubismLogError("Failed to LoadMotion()."); + return NULL; + } return CubismMotion::Create(buffer, size, onFinishedMotionHandler); } diff --git a/src/Motion/CubismExpressionMotion.cpp b/src/Motion/CubismExpressionMotion.cpp index 41a9947..38aa758 100644 --- a/src/Motion/CubismExpressionMotion.cpp +++ b/src/Motion/CubismExpressionMotion.cpp @@ -198,6 +198,11 @@ csmFloat32 CubismExpressionMotion::GetFadeWeight() void CubismExpressionMotion::Parse(const csmByte* buffer, csmSizeInt size) { Utils::CubismJson* json = Utils::CubismJson::Create(buffer, size); + if (!json) + { + return; + } + Utils::Value& root = json->GetRoot(); SetFadeInTime(root[ExpressionKeyFadeIn].ToFloat(DefaultFadeTime)); // フェードイン diff --git a/src/Motion/CubismMotion.cpp b/src/Motion/CubismMotion.cpp index f5ab6a1..0ddd304 100644 --- a/src/Motion/CubismMotion.cpp +++ b/src/Motion/CubismMotion.cpp @@ -527,6 +527,12 @@ void CubismMotion::Parse(const csmByte* motionJson, const csmSizeInt size) CubismMotionJson* json = CSM_NEW CubismMotionJson(motionJson, size); + if (!json->IsValid()) + { + CSM_DELETE(json); + return; + } + _motionData->Duration = json->GetMotionDuration(); _motionData->Loop = json->IsMotionLoop(); _motionData->CurveCount = json->GetMotionCurveCount(); diff --git a/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.cpp b/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.cpp index c0f91eb..3ba4f2b 100644 --- a/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.cpp +++ b/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.cpp @@ -639,9 +639,9 @@ void CubismRenderer_Cocos2dx::RestoreProfile() _rendererProfile.Restore(); } -void CubismRenderer_Cocos2dx::BindTexture(csmUint32 modelTextureNo, cocos2d::Texture2D* texture) +void CubismRenderer_Cocos2dx::BindTexture(csmUint32 modelTextureIndex, cocos2d::Texture2D* texture) { - _textures[modelTextureNo] = texture; + _textures[modelTextureIndex] = texture; } const csmMap& CubismRenderer_Cocos2dx::GetBindedTextures() const @@ -712,9 +712,9 @@ const csmBool CubismRenderer_Cocos2dx::IsGeneratingMask() const return GetClippingContextBufferForMask() != NULL; } -cocos2d::Texture2D* CubismRenderer_Cocos2dx::GetBindedTexture(csmInt32 textureNo) +cocos2d::Texture2D* CubismRenderer_Cocos2dx::GetBindedTexture(csmInt32 textureIndex) { - return _textures[textureNo]; + return _textures[textureIndex]; } }}}} diff --git a/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.hpp b/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.hpp index 30c501d..4f07d8e 100644 --- a/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.hpp +++ b/src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.hpp @@ -170,11 +170,11 @@ class CubismRenderer_Cocos2dx : public CubismRenderer * @brief OpenGLテクスチャのバインド処理
* CubismRendererにテクスチャを設定し、CubismRenderer中でその画像を参照するためのIndex値を戻り値とする * - * @param[in] modelTextureNo -> セットするモデルテクスチャの番号 + * @param[in] modelTextureIndex -> セットするモデルテクスチャの番号 * @param[in] texture -> バックエンドテクスチャ * */ - void BindTexture(csmUint32 modelTextureNo, cocos2d::Texture2D* texture); + void BindTexture(csmUint32 modelTextureIndex, cocos2d::Texture2D* texture); /** * @brief OpenGLにバインドされたテクスチャのリストを取得する @@ -340,7 +340,7 @@ class CubismRenderer_Cocos2dx : public CubismRenderer * * @return バインドされたテクスチャ */ - cocos2d::Texture2D* GetBindedTexture(csmInt32 textureNo); + cocos2d::Texture2D* GetBindedTexture(csmInt32 textureIndex); csmMap _textures; ///< モデルが参照するテクスチャとレンダラでバインドしているテクスチャとのマップ diff --git a/src/Rendering/Cocos2d/CubismShader_Cocos2dx.cpp b/src/Rendering/Cocos2d/CubismShader_Cocos2dx.cpp index bec691e..9cb4141 100644 --- a/src/Rendering/Cocos2d/CubismShader_Cocos2dx.cpp +++ b/src/Rendering/Cocos2d/CubismShader_Cocos2dx.cpp @@ -784,8 +784,8 @@ void CubismShader_Cocos2dx::SetupShaderProgramForMask(CubismCommandBuffer_Cocos2 //テクスチャ設定 - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - cocos2d::Texture2D* texture = renderer->GetBindedTexture(textureNo); + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + cocos2d::Texture2D* texture = renderer->GetBindedTexture(textureIndex); programState->setTexture(shaderSet->SamplerTexture0Location, 0, texture->getBackendTexture()); // 頂点配列の設定 @@ -794,8 +794,8 @@ void CubismShader_Cocos2dx::SetupShaderProgramForMask(CubismCommandBuffer_Cocos2 programState->getVertexLayout()->setAttribute("a_texCoord", shaderSet->AttributeTexCoordLocation, cocos2d::backend::VertexFormat::FLOAT2, sizeof(csmFloat32) * 2, false); // チャンネル - const csmInt32 channelNo = renderer->GetClippingContextBufferForMask()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForMask()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); csmFloat32 colorFlag[4] = { colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A }; programState->setUniform(shaderSet->UnifromChannelFlagLocation, colorFlag, sizeof(float) * 4); @@ -901,15 +901,15 @@ void CubismShader_Cocos2dx::SetupShaderProgramForDraw(CubismCommandBuffer_Cocos2 sizeof(float) * 16); // 使用するカラーチャンネルを設定 - const csmInt32 channelNo = renderer->GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); csmFloat32 colorFlag[4] = { colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A }; programState->setUniform(shaderSet->UnifromChannelFlagLocation, colorFlag, sizeof(float) * 4); } //テクスチャ設定 - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - cocos2d::Texture2D* texture = renderer->GetBindedTexture(textureNo); + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + cocos2d::Texture2D* texture = renderer->GetBindedTexture(textureIndex); programState->setTexture(shaderSet->SamplerTexture0Location, 0, texture->getBackendTexture()); //座標変換 diff --git a/src/Rendering/CubismClippingManager.hpp b/src/Rendering/CubismClippingManager.hpp index acc07ba..76a5b5a 100644 --- a/src/Rendering/CubismClippingManager.hpp +++ b/src/Rendering/CubismClippingManager.hpp @@ -121,9 +121,9 @@ class CubismClippingManager /** * @brief カラーチャンネル(RGBA)のフラグを取得する * - * @param[in] channelNo -> カラーチャンネル(RGBA)の番号(0:R , 1:G , 2:B, 3:A) + * @param[in] channelIndex -> カラーチャンネル(RGBA)の番号(0:R , 1:G , 2:B, 3:A) */ - CubismRenderer::CubismTextureColor* GetChannelFlagAsColor(csmInt32 channelNo); + CubismRenderer::CubismTextureColor* GetChannelFlagAsColor(csmInt32 channelIndex); /** *@brief クリッピングマスクバッファのサイズを設定する diff --git a/src/Rendering/CubismClippingManager.tpp b/src/Rendering/CubismClippingManager.tpp index c6d670b..fa724a4 100644 --- a/src/Rendering/CubismClippingManager.tpp +++ b/src/Rendering/CubismClippingManager.tpp @@ -277,7 +277,7 @@ void CubismClippingManager::SetupLayoutBo for (csmUint32 index = 0; index < _clippingContextListForMask.GetSize(); index++) { T_ClippingContext* cc = _clippingContextListForMask[index]; - cc->_layoutChannelNo = 0; // どうせ毎回消すので固定で良い + cc->_layoutChannelIndex = 0; // どうせ毎回消すので固定で良い cc->_layoutBounds->X = 0.0f; cc->_layoutBounds->Y = 0.0f; cc->_layoutBounds->Width = 1.0f; @@ -292,28 +292,33 @@ void CubismClippingManager::SetupLayoutBo // ひとつのRenderTextureを極力いっぱいに使ってマスクをレイアウトする // マスクグループの数が4以下ならRGBA各チャンネルに1つずつマスクを配置し、5以上6以下ならRGBAを2,2,1,1と配置する - const csmInt32 countPerSheetDiv = usingClipCount / _renderTextureCount; // レンダーテクスチャ1枚あたり何枚割り当てるか - const csmInt32 countPerSheetMod = usingClipCount % _renderTextureCount; // この番号のレンダーテクスチャまでに一つずつ配分する + const csmInt32 countPerSheetDiv = (usingClipCount + _renderTextureCount - 1) / _renderTextureCount; // レンダーテクスチャ1枚あたり何枚割り当てるか(切り上げ) + const csmInt32 reduceLayoutTextureCount = usingClipCount % _renderTextureCount; // レイアウトの数を1枚減らすレンダーテクスチャの数(この数だけのレンダーテクスチャが対象) - // RGBAを順番に使っていく。 - const csmInt32 div = countPerSheetDiv / ColorChannelCount; //1チャンネルに配置する基本のマスク個数 - const csmInt32 mod = countPerSheetDiv % ColorChannelCount; //余り、この番号のチャンネルまでに1つずつ配分する + // RGBAを順番に使っていく + const csmInt32 divCount = countPerSheetDiv / ColorChannelCount; //1チャンネルに配置する基本のマスク個数 + const csmInt32 modCount = countPerSheetDiv % ColorChannelCount; //余り、この番号のチャンネルまでに1つずつ配分する // RGBAそれぞれのチャンネルを用意していく(0:R , 1:G , 2:B, 3:A, ) csmInt32 curClipIndex = 0; //順番に設定していく - for (csmInt32 renderTextureNo = 0; renderTextureNo < _renderTextureCount; renderTextureNo++) + for (csmInt32 renderTextureIndex = 0; renderTextureIndex < _renderTextureCount; renderTextureIndex++) { - for (csmInt32 channelNo = 0; channelNo < ColorChannelCount; channelNo++) + for (csmInt32 channelIndex = 0; channelIndex < ColorChannelCount; channelIndex++) { // このチャンネルにレイアウトする数 - csmInt32 layoutCount = div + (channelNo < mod ? 1 : 0); + // NOTE: レイアウト数 = 1チャンネルに配置する基本のマスク + 余りのマスクを置くチャンネルなら1つ追加 + csmInt32 layoutCount = divCount + (channelIndex < modCount ? 1 : 0); - // このレンダーテクスチャにまだ割り当てられていなければ追加する - const csmInt32 checkChannelNo = mod + 1 >= ColorChannelCount ? 0 : mod + 1; - if (layoutCount < layoutCountMaxValue && channelNo == checkChannelNo) + // レイアウトの数を1枚減らす場合にそれを行うチャンネルを決定 + // divが0の時は正常なインデックスの範囲内になるように調整 + const csmInt32 checkChannelIndex = modCount + (divCount < 1 ? -1 : 0); + + // 今回が対象のチャンネルかつ、レイアウトの数を1枚減らすレンダーテクスチャが存在する場合 + if (channelIndex == checkChannelIndex && reduceLayoutTextureCount > 0) { - layoutCount += renderTextureNo < countPerSheetMod ? 1 : 0; + // 現在のレンダーテクスチャが、対象のレンダーテクスチャであればレイアウトの数を1枚減らす + layoutCount -= !(renderTextureIndex < reduceLayoutTextureCount) ? 1 : 0; } // 分割方法を決定する @@ -325,12 +330,12 @@ void CubismClippingManager::SetupLayoutBo { //全てをそのまま使う T_ClippingContext* cc = _clippingContextListForMask[curClipIndex++]; - cc->_layoutChannelNo = channelNo; + cc->_layoutChannelIndex = channelIndex; cc->_layoutBounds->X = 0.0f; cc->_layoutBounds->Y = 0.0f; cc->_layoutBounds->Width = 1.0f; cc->_layoutBounds->Height = 1.0f; - cc->_bufferIndex = renderTextureNo; + cc->_bufferIndex = renderTextureIndex; } else if (layoutCount == 2) { @@ -339,13 +344,13 @@ void CubismClippingManager::SetupLayoutBo const csmInt32 xpos = i % 2; T_ClippingContext* cc = _clippingContextListForMask[curClipIndex++]; - cc->_layoutChannelNo = channelNo; + cc->_layoutChannelIndex = channelIndex; cc->_layoutBounds->X = xpos * 0.5f; cc->_layoutBounds->Y = 0.0f; cc->_layoutBounds->Width = 0.5f; cc->_layoutBounds->Height = 1.0f; - cc->_bufferIndex = renderTextureNo; + cc->_bufferIndex = renderTextureIndex; //UVを2つに分解して使う } } @@ -358,13 +363,13 @@ void CubismClippingManager::SetupLayoutBo const csmInt32 ypos = i / 2; T_ClippingContext* cc = _clippingContextListForMask[curClipIndex++]; - cc->_layoutChannelNo = channelNo; + cc->_layoutChannelIndex = channelIndex; cc->_layoutBounds->X = xpos * 0.5f; cc->_layoutBounds->Y = ypos * 0.5f; cc->_layoutBounds->Width = 0.5f; cc->_layoutBounds->Height = 0.5f; - cc->_bufferIndex = renderTextureNo; + cc->_bufferIndex = renderTextureIndex; } } else if (layoutCount <= layoutCountMaxValue) @@ -376,13 +381,13 @@ void CubismClippingManager::SetupLayoutBo const csmInt32 ypos = i / 3; T_ClippingContext* cc = _clippingContextListForMask[curClipIndex++]; - cc->_layoutChannelNo = channelNo; + cc->_layoutChannelIndex = channelIndex; cc->_layoutBounds->X = xpos / 3.0f; cc->_layoutBounds->Y = ypos / 3.0f; cc->_layoutBounds->Width = 1.0f / 3.0f; cc->_layoutBounds->Height = 1.0f / 3.0f; - cc->_bufferIndex = renderTextureNo; + cc->_bufferIndex = renderTextureIndex; } } // マスクの制限枚数を超えた場合の処理 @@ -402,7 +407,7 @@ void CubismClippingManager::SetupLayoutBo for (csmInt32 i = 0; i < layoutCount; i++) { T_ClippingContext* cc = _clippingContextListForMask[curClipIndex++]; - cc->_layoutChannelNo = 0; + cc->_layoutChannelIndex = 0; cc->_layoutBounds->X = 0.0f; cc->_layoutBounds->Y = 0.0f; cc->_layoutBounds->Width = 1.0f; @@ -495,9 +500,9 @@ csmInt32 CubismClippingManager::GetRender } template -CubismRenderer::CubismTextureColor* CubismClippingManager::GetChannelFlagAsColor(csmInt32 channelNo) +CubismRenderer::CubismTextureColor* CubismClippingManager::GetChannelFlagAsColor(csmInt32 channelIndex) { - return _channelColors[channelNo]; + return _channelColors[channelIndex]; } template diff --git a/src/Rendering/CubismRenderer.cpp b/src/Rendering/CubismRenderer.cpp index 9f3750b..bf89a77 100644 --- a/src/Rendering/CubismRenderer.cpp +++ b/src/Rendering/CubismRenderer.cpp @@ -164,7 +164,7 @@ CubismClippingContext::CubismClippingContext(const csmInt32* clippingDrawableInd // マスクの数 _clippingIdCount = clipCount; - _layoutChannelNo = 0; + _layoutChannelIndex = 0; _allClippedDrawRect = CSM_NEW csmRectF(); _layoutBounds = CSM_NEW csmRectF(); diff --git a/src/Rendering/CubismRenderer.hpp b/src/Rendering/CubismRenderer.hpp index 116a0ff..2746f3c 100644 --- a/src/Rendering/CubismRenderer.hpp +++ b/src/Rendering/CubismRenderer.hpp @@ -296,7 +296,7 @@ class CubismClippingContext csmBool _isUsing; ///< 現在の描画状態でマスクの準備が必要ならtrue const csmInt32* _clippingIdList; ///< クリッピングマスクのIDリスト csmInt32 _clippingIdCount; ///< クリッピングマスクの数 - csmInt32 _layoutChannelNo; ///< RGBAのいずれのチャンネルにこのクリップを配置するか(0:R , 1:G , 2:B , 3:A) + csmInt32 _layoutChannelIndex; ///< RGBAのいずれのチャンネルにこのクリップを配置するか(0:R , 1:G , 2:B , 3:A) csmRectF* _layoutBounds; ///< マスク用チャンネルのどの領域にマスクを入れるか(View座標-1..1, UVは0..1に直す) csmRectF* _allClippedDrawRect; ///< このクリッピングで、クリッピングされる全ての描画オブジェクトの囲み矩形(毎回更新) CubismMatrix44 _matrixForMask; ///< マスクの位置計算結果を保持する行列 diff --git a/src/Rendering/D3D11/CubismRenderer_D3D11.cpp b/src/Rendering/D3D11/CubismRenderer_D3D11.cpp index c22171f..8cb2706 100644 --- a/src/Rendering/D3D11/CubismRenderer_D3D11.cpp +++ b/src/Rendering/D3D11/CubismRenderer_D3D11.cpp @@ -192,40 +192,10 @@ CubismClippingContext_D3D11::CubismClippingContext_D3D11(CubismClippingManager(); } CubismClippingContext_D3D11::~CubismClippingContext_D3D11() { - if (_layoutBounds != NULL) - { - CSM_DELETE(_layoutBounds); - _layoutBounds = NULL; - } - - if (_allClippedDrawRect != NULL) - { - CSM_DELETE(_allClippedDrawRect); - _allClippedDrawRect = NULL; - } - - if (_clippedDrawableIndexList != NULL) - { - CSM_DELETE(_clippedDrawableIndexList); - _clippedDrawableIndexList = NULL; - } } CubismClippingManager* CubismClippingContext_D3D11::GetClippingManager() @@ -728,8 +698,8 @@ void CubismRenderer_D3D11::ExecuteDrawForMask(const CubismModel& model, const cs DirectX::XMMATRIX proj = ConvertToD3DX(GetClippingContextBufferForMask()->_matrixForMask); csmRectF* rect = GetClippingContextBufferForMask()->_layoutBounds; - const csmInt32 channelNo = GetClippingContextBufferForMask()->_layoutChannelNo; - CubismTextureColor* colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = GetClippingContextBufferForMask()->_layoutChannelIndex; + CubismTextureColor* colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); const CubismTextureColor& multiplyColor = model.GetMultiplyColor(index); const CubismTextureColor& screenColor = model.GetScreenColor(index); @@ -815,8 +785,8 @@ void CubismRenderer_D3D11::ExecuteDrawForDraw(const CubismModel& model, const cs XMStoreFloat4x4(&cb.clipMatrix, DirectX::XMMatrixTranspose(clip)); // 使用するカラーチャンネルを設定 - const csmInt32 channelNo = GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); XMStoreFloat4(&cb.channelFlag, DirectX::XMVectorSet(colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A)); } @@ -1067,10 +1037,10 @@ void CubismRenderer_D3D11::CopyToBuffer(ID3D11DeviceContext* renderContext, csmI ID3D11ShaderResourceView* CubismRenderer_D3D11::GetTextureViewWithIndex(const CubismModel& model, const csmInt32 index) { ID3D11ShaderResourceView* result = NULL; - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - if (textureNo >= 0) + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + if (textureIndex >= 0) { - result = _textures[textureNo]; + result = _textures[textureIndex]; } return result; } diff --git a/src/Rendering/D3D9/CubismRenderer_D3D9.cpp b/src/Rendering/D3D9/CubismRenderer_D3D9.cpp index fefca67..98f20ad 100644 --- a/src/Rendering/D3D9/CubismRenderer_D3D9.cpp +++ b/src/Rendering/D3D9/CubismRenderer_D3D9.cpp @@ -641,8 +641,8 @@ void CubismRenderer_D3D9::ExecuteDrawForDraw(const CubismModel& model, const csm shaderEffect->SetMatrix("clipMatrix", &clipM); // 使用するカラーチャンネルを設定 - const csmInt32 channelNo = GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); D3DXVECTOR4 channel(colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); shaderEffect->SetVector("channelFlag", &channel); } @@ -727,9 +727,9 @@ void CubismRenderer_D3D9::ExecuteDrawForMask(const CubismModel& model, const csm shaderEffect->SetVector("screenColor", &shaderScreenColor); // チャンネル - const csmInt32 channelNo = GetClippingContextBufferForMask()->_layoutChannelNo; + const csmInt32 channelIndex = GetClippingContextBufferForMask()->_layoutChannelIndex; // チャンネルをRGBAに変換 - CubismTextureColor* colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + CubismTextureColor* colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); D3DXVECTOR4 channel(colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); shaderEffect->SetVector("channelFlag", &channel); @@ -976,10 +976,10 @@ void CubismRenderer_D3D9::CopyToBuffer(csmInt32 drawAssign, const csmInt32 vcoun LPDIRECT3DTEXTURE9 CubismRenderer_D3D9::GetTextureWithIndex(const CubismModel& model, const csmInt32 index) { LPDIRECT3DTEXTURE9 result = NULL; - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - if (textureNo >= 0) + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + if (textureIndex >= 0) { - result = _textures[textureNo]; + result = _textures[textureIndex]; } return result; diff --git a/src/Rendering/Metal/CubismRenderer_Metal.hpp b/src/Rendering/Metal/CubismRenderer_Metal.hpp index 93fb802..c620793 100644 --- a/src/Rendering/Metal/CubismRenderer_Metal.hpp +++ b/src/Rendering/Metal/CubismRenderer_Metal.hpp @@ -135,11 +135,11 @@ class CubismRenderer_Metal : public CubismRenderer * @brief テクスチャのバインド処理
* CubismRendererにテクスチャを設定し、CubismRenderer中でその画像を参照するためのIndex値を戻り値とする * - * @param[in] modelTextureNo -> セットするモデルテクスチャの番号 + * @param[in] modelTextureIndex -> セットするモデルテクスチャの番号 * @param[in] texture -> バックエンドテクスチャ * */ - void BindTexture(csmUint32 modelTextureNo, id texture); + void BindTexture(csmUint32 modelTextureIndex, id texture); /** * @brief バインドされたテクスチャのリストを取得する @@ -214,7 +214,7 @@ class CubismRenderer_Metal : public CubismRenderer * @brief 描画オブジェクト(アートメッシュ)を描画する。
* ポリゴンメッシュとテクスチャ番号をセットで渡す。 * - * @param[in] textureNo -> 描画するテクスチャ番号 + * @param[in] textureIndex -> 描画するテクスチャ番号 * @param[in] renderEncoder -> MTLRenderCommandEncoder * @param[in] model -> 描画対象モデル * @param[in] index -> 描画オブジェクトのインデックス diff --git a/src/Rendering/Metal/CubismRenderer_Metal.mm b/src/Rendering/Metal/CubismRenderer_Metal.mm index e0e8931..652490e 100644 --- a/src/Rendering/Metal/CubismRenderer_Metal.mm +++ b/src/Rendering/Metal/CubismRenderer_Metal.mm @@ -591,9 +591,9 @@ { } -void CubismRenderer_Metal::BindTexture(csmUint32 modelTextureNo, id texture) +void CubismRenderer_Metal::BindTexture(csmUint32 modelTextureIndex, id texture) { - _textures[modelTextureNo] = texture; + _textures[modelTextureIndex] = texture; } const csmMap< csmInt32, id >& CubismRenderer_Metal::GetBindedTextures() const diff --git a/src/Rendering/Metal/CubismShader_Metal.mm b/src/Rendering/Metal/CubismShader_Metal.mm index 489ec16..d9e741a 100644 --- a/src/Rendering/Metal/CubismShader_Metal.mm +++ b/src/Rendering/Metal/CubismShader_Metal.mm @@ -272,8 +272,8 @@ [renderEncoder setFragmentTexture:tex atIndex:1]; // 使用するカラーチャンネルを設定 - const csmInt32 channelNo = renderer->GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); fragMaskedShaderUniforms.channelFlag = (vector_float4){ colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A }; { @@ -366,8 +366,8 @@ [renderEncoder setVertexBuffer:(drawCommandBuffer->GetUvBuffer()) offset:0 atIndex:MetalVertexInputUVs]; CubismSetupMaskedShaderUniforms maskedShaderUniforms; - const csmInt32 channelNo = renderer->GetClippingContextBufferForMask()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForMask()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); maskedShaderUniforms.channelFlag = (vector_float4){ colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A }; csmFloat32* srcArray = renderer->GetClippingContextBufferForMask()->_matrixForMask.GetArray(); diff --git a/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.cpp b/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.cpp index 6311abd..e608f64 100644 --- a/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.cpp +++ b/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.cpp @@ -704,9 +704,9 @@ void CubismRenderer_OpenGLES2::RestoreProfile() _rendererProfile.Restore(); } -void CubismRenderer_OpenGLES2::BindTexture(csmUint32 modelTextureNo, GLuint glTextureNo) +void CubismRenderer_OpenGLES2::BindTexture(csmUint32 modelTextureIndex, GLuint glTextureIndex) { - _textures[modelTextureNo] = glTextureNo; + _textures[modelTextureIndex] = glTextureIndex; } const csmMap& CubismRenderer_OpenGLES2::GetBindedTextures() const diff --git a/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.hpp b/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.hpp index f658e90..421e13c 100644 --- a/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.hpp +++ b/src/Rendering/OpenGL/CubismRenderer_OpenGLES2.hpp @@ -188,11 +188,11 @@ class CubismRenderer_OpenGLES2 : public CubismRenderer * @brief OpenGLテクスチャのバインド処理
* CubismRendererにテクスチャを設定し、CubismRenderer中でその画像を参照するためのIndex値を戻り値とする * - * @param[in] modelTextureNo -> セットするモデルテクスチャの番号 - * @param[in] glTextureNo -> OpenGLテクスチャの番号 + * @param[in] modelTextureIndex -> セットするモデルテクスチャの番号 + * @param[in] glTextureIndex -> OpenGLテクスチャの番号 * */ - void BindTexture(csmUint32 modelTextureNo, GLuint glTextureNo); + void BindTexture(csmUint32 modelTextureIndex, GLuint glTextureIndex); /** * @brief OpenGLにバインドされたテクスチャのリストを取得する diff --git a/src/Rendering/OpenGL/CubismShader_OpenGLES2.cpp b/src/Rendering/OpenGL/CubismShader_OpenGLES2.cpp index 0071ad2..8563bd1 100644 --- a/src/Rendering/OpenGL/CubismShader_OpenGLES2.cpp +++ b/src/Rendering/OpenGL/CubismShader_OpenGLES2.cpp @@ -867,14 +867,14 @@ void CubismShader_OpenGLES2::SetupShaderProgramForDraw(CubismRenderer_OpenGLES2* glUniformMatrix4fv(shaderSet->UniformClipMatrixLocation, 1, 0, renderer->GetClippingContextBufferForDraw()->_matrixForDraw.GetArray()); // 使用するカラーチャンネルを設定 - const csmInt32 channelNo = renderer->GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); glUniform4f(shaderSet->UnifromChannelFlagLocation, colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); } //テクスチャ設定 - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - const GLuint textureId = renderer->GetBindedTextureId(textureNo); + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + const GLuint textureId = renderer->GetBindedTextureId(textureIndex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureId); glUniform1i(shaderSet->SamplerTexture0Location, 0); @@ -912,8 +912,8 @@ void CubismShader_OpenGLES2::SetupShaderProgramForMask(CubismRenderer_OpenGLES2* glUseProgram(shaderSet->ShaderProgram); //テクスチャ設定 - const csmInt32 textureNo = model.GetDrawableTextureIndex(index); - const GLuint textureId = renderer->GetBindedTextureId(textureNo); + const csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + const GLuint textureId = renderer->GetBindedTextureId(textureIndex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureId); glUniform1i(shaderSet->SamplerTexture0Location, 0); @@ -929,8 +929,8 @@ void CubismShader_OpenGLES2::SetupShaderProgramForMask(CubismRenderer_OpenGLES2* glVertexAttribPointer(shaderSet->AttributeTexCoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(csmFloat32) * 2, uvArray); // チャンネル - const csmInt32 channelNo = renderer->GetClippingContextBufferForMask()->_layoutChannelNo; - CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = renderer->GetClippingContextBufferForMask()->_layoutChannelIndex; + CubismRenderer::CubismTextureColor* colorChannel = renderer->GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); glUniform4f(shaderSet->UnifromChannelFlagLocation, colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); glUniformMatrix4fv(shaderSet->UniformClipMatrixLocation, 1, GL_FALSE, renderer->GetClippingContextBufferForMask()->_matrixForMask.GetArray()); diff --git a/src/Rendering/Vulkan/CubismClass_Vulkan.cpp b/src/Rendering/Vulkan/CubismClass_Vulkan.cpp index fe7bfe2..9b470fe 100644 --- a/src/Rendering/Vulkan/CubismClass_Vulkan.cpp +++ b/src/Rendering/Vulkan/CubismClass_Vulkan.cpp @@ -203,14 +203,14 @@ void CubismImageVulkan::CreateSampler(VkDevice device, float maxAnistropy, } } -void CubismImageVulkan::SetImageLayout(VkCommandBuffer commandBuffer, VkImageLayout newLayout, csmUint32 mipLevels) +void CubismImageVulkan::SetImageLayout(VkCommandBuffer commandBuffer, VkImageLayout newLayout, csmUint32 mipLevels, VkImageAspectFlags aspectMask) { VkImageMemoryBarrier barrier{}; barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; barrier.oldLayout = currentLayout; barrier.newLayout = newLayout; barrier.image = image; - barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + barrier.subresourceRange.aspectMask = aspectMask; barrier.subresourceRange.baseMipLevel = 0; barrier.subresourceRange.levelCount = mipLevels; barrier.subresourceRange.baseArrayLayer = 0; @@ -288,6 +288,11 @@ void CubismImageVulkan::SetImageLayout(VkCommandBuffer commandBuffer, VkImageLay currentLayout = newLayout; } +void CubismImageVulkan::SetCurrentLayout(VkImageLayout newLayout) +{ + currentLayout = newLayout; +} + void CubismImageVulkan::Destroy(VkDevice device) { if (image != VK_NULL_HANDLE) diff --git a/src/Rendering/Vulkan/CubismClass_Vulkan.hpp b/src/Rendering/Vulkan/CubismClass_Vulkan.hpp index cf94624..6a90276 100644 --- a/src/Rendering/Vulkan/CubismClass_Vulkan.hpp +++ b/src/Rendering/Vulkan/CubismClass_Vulkan.hpp @@ -149,7 +149,14 @@ class CubismImageVulkan * @param[in] newLayout -> 新しいレイアウト * @param[in] mipLevels -> ミップマップのレベル */ - void SetImageLayout(VkCommandBuffer commandBuffer, VkImageLayout newLayout, csmUint32 mipLevels); + void SetImageLayout(VkCommandBuffer commandBuffer, VkImageLayout newLayout, csmUint32 mipLevels, VkImageAspectFlags aspectMask); + + /** + * @brief vkCmdEndRendering後のパイプラインバリアで変わってしまうイメージレイアウトを保存する + * + * @param[in] newLayout -> 新しいレイアウト + */ + void SetCurrentLayout(VkImageLayout newLayout); /** * @brief リソースを破棄する diff --git a/src/Rendering/Vulkan/CubismOffscreenSurface_Vulkan.cpp b/src/Rendering/Vulkan/CubismOffscreenSurface_Vulkan.cpp index ebce6d4..121f1ab 100644 --- a/src/Rendering/Vulkan/CubismOffscreenSurface_Vulkan.cpp +++ b/src/Rendering/Vulkan/CubismOffscreenSurface_Vulkan.cpp @@ -17,6 +17,7 @@ CubismOffscreenSurface_Vulkan::CubismOffscreenSurface_Vulkan() void CubismOffscreenSurface_Vulkan::BeginDraw(VkCommandBuffer commandBuffer, csmFloat32 r, csmFloat32 g, csmFloat32 b, csmFloat32 a) { + _colorImage->SetImageLayout(commandBuffer, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, VK_IMAGE_ASPECT_COLOR_BIT); VkRenderingAttachmentInfoKHR colorAttachment{}; colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR; colorAttachment.imageView = _colorImage->GetView(); @@ -24,7 +25,7 @@ void CubismOffscreenSurface_Vulkan::BeginDraw(VkCommandBuffer commandBuffer, csm colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.clearValue = {{r, g, b, a}}; - + _depthImage->SetImageLayout(commandBuffer, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1, VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT); VkRenderingAttachmentInfoKHR depthStencilAttachment{}; depthStencilAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR; depthStencilAttachment.imageView = _depthImage->GetView(); @@ -56,13 +57,22 @@ void CubismOffscreenSurface_Vulkan::EndDraw(VkCommandBuffer commandBuffer) VkImageMemoryBarrier memoryBarrier{}; memoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; memoryBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; + memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; memoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; memoryBarrier.image = _colorImage->GetImage(); memoryBarrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &memoryBarrier); + _colorImage->SetCurrentLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + + memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + memoryBarrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + memoryBarrier.image = _depthImage->GetImage(); + memoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, 0, 1, 0, 1 }; + vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &memoryBarrier); + _depthImage->SetCurrentLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); } void CubismOffscreenSurface_Vulkan::CreateOffscreenSurface( diff --git a/src/Rendering/Vulkan/CubismRenderer_Vulkan.cpp b/src/Rendering/Vulkan/CubismRenderer_Vulkan.cpp index eda6e7d..51cde41 100644 --- a/src/Rendering/Vulkan/CubismRenderer_Vulkan.cpp +++ b/src/Rendering/Vulkan/CubismRenderer_Vulkan.cpp @@ -205,7 +205,7 @@ CubismClippingContext_Vulkan::CubismClippingContext_Vulkan( // マスクの数 _clippingIdCount = clipCount; - _layoutChannelNo = 0; + _layoutChannelIndex = 0; _allClippedDrawRect = CSM_NEW csmRectF(); _layoutBounds = CSM_NEW csmRectF(); @@ -624,20 +624,22 @@ void CubismRenderer_Vulkan::SubmitCommand(VkCommandBuffer commandBuffer, VkSemap submitInfo.pCommandBuffers = &commandBuffer; if(waitUpdateFinishedSemaphore != VK_NULL_HANDLE) { + VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; submitInfo.waitSemaphoreCount = 1; submitInfo.pWaitSemaphores = &waitUpdateFinishedSemaphore; - vkQueueSubmit(s_queue, 1, &submitInfo, nullptr); + submitInfo.pWaitDstStageMask = waitStages; + vkQueueSubmit(s_queue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueWaitIdle(s_queue); } else if (signalUpdateFinishedSemaphore != VK_NULL_HANDLE) { submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = &signalUpdateFinishedSemaphore; - vkQueueSubmit(s_queue, 1, &submitInfo, nullptr); + vkQueueSubmit(s_queue, 1, &submitInfo, VK_NULL_HANDLE); } else { - vkQueueSubmit(s_queue, 1, &submitInfo, nullptr); + vkQueueSubmit(s_queue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueWaitIdle(s_queue); vkFreeCommandBuffers(s_device, s_commandPool, 1, &commandBuffer); } @@ -772,18 +774,20 @@ void CubismRenderer_Vulkan::CreateDescriptorSets() { // ディスクリプタプールの作成 const csmInt32 drawableCount = GetModel()->GetDrawableCount(); - + csmInt32 textureCount = 2; + csmInt32 drawModeCount = 2; // マスクされる描画と通常の描画 + csmInt32 descriptorSetCount = drawableCount * drawModeCount; VkDescriptorPoolSize poolSizes[2]{}; poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - poolSizes[0].descriptorCount = drawableCount; + poolSizes[0].descriptorCount = descriptorSetCount; poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - poolSizes[1].descriptorCount = drawableCount * 2; // drawableCount*マスクを用いるか*テクスチャの数 + poolSizes[1].descriptorCount = descriptorSetCount * textureCount; // drawableCount * 描画方法の数 * テクスチャの数 VkDescriptorPoolCreateInfo poolInfo{}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; poolInfo.poolSizeCount = sizeof(poolSizes) / sizeof(poolSizes[0]); poolInfo.pPoolSizes = poolSizes; - poolInfo.maxSets = drawableCount * 2; + poolInfo.maxSets = descriptorSetCount; if (vkCreateDescriptorPool(s_device, &poolInfo, nullptr, &_descriptorPool) != VK_SUCCESS) { @@ -825,24 +829,25 @@ void CubismRenderer_Vulkan::CreateDescriptorSets() for (csmInt32 drawAssign = 0; drawAssign < drawableCount; drawAssign++) { _descriptorSets[drawAssign].uniformBuffer.CreateBuffer(s_device, s_physicalDevice, sizeof(ModelUBO), - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); _descriptorSets[drawAssign].uniformBuffer.Map(s_device, VK_WHOLE_SIZE); } VkDescriptorSetAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.descriptorPool = _descriptorPool; - allocInfo.descriptorSetCount = drawableCount * 2; + allocInfo.descriptorSetCount = descriptorSetCount; csmVector layouts; - csmVector descriptorSets; - descriptorSets.Resize(drawableCount * 2); - for (csmInt32 i = 0; i < drawableCount * 2; i++) + for (csmInt32 i = 0; i < descriptorSetCount; i++) { layouts.PushBack(_descriptorSetLayout); } allocInfo.pSetLayouts = layouts.GetPtr(); + + csmVector descriptorSets; + descriptorSets.Resize(descriptorSetCount); if (vkAllocateDescriptorSets(s_device, &allocInfo, descriptorSets.GetPtr()) != VK_SUCCESS) { CubismLogError("failed to allocate descriptor sets!"); @@ -1074,8 +1079,8 @@ void CubismRenderer_Vulkan::ExecuteDrawForDraw(const CubismModel& model, const c { CubismMatrix44 mvp = GetClippingContextBufferForDraw()->_matrixForDraw; UpdateMatrix(_ubo.clipMatrix, mvp); // テクスチャ座標の変換に使用するのでy軸の向きは反転しない - const csmInt32 channelNo = GetClippingContextBufferForDraw()->_layoutChannelNo; - CubismTextureColor *colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = GetClippingContextBufferForDraw()->_layoutChannelIndex; + CubismTextureColor *colorChannel = GetClippingContextBufferForDraw()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); UpdateColor(_ubo.channelFlag, colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); } @@ -1104,10 +1109,10 @@ void CubismRenderer_Vulkan::ExecuteDrawForDraw(const CubismModel& model, const c vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets); vkCmdBindIndexBuffer(cmdBuffer, _indexBuffers[index].GetBuffer(), 0, VK_INDEX_TYPE_UINT16); - csmInt32 textureNo = model.GetDrawableTextureIndex(index); + csmInt32 textureIndex = model.GetDrawableTextureIndex(index); if (masked) { - UpdateDescriptorSet(descriptor, textureNo, true); + UpdateDescriptorSet(descriptor, textureIndex, true); vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CubismPipeline_Vulkan::GetInstance()->GetPipelineLayout(shaderIndex, blendIndex), 0, 1, @@ -1116,7 +1121,7 @@ void CubismRenderer_Vulkan::ExecuteDrawForDraw(const CubismModel& model, const c } else { - UpdateDescriptorSet(descriptor, textureNo, false); + UpdateDescriptorSet(descriptor, textureIndex, false); vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CubismPipeline_Vulkan::GetInstance()->GetPipelineLayout(shaderIndex, blendIndex), 0, 1, @@ -1129,8 +1134,8 @@ void CubismRenderer_Vulkan::ExecuteDrawForDraw(const CubismModel& model, const c void CubismRenderer_Vulkan::ExecuteDrawForMask(const CubismModel& model, const csmInt32 index, VkCommandBuffer& cmdBuffer) { - const csmInt32 channelNo = GetClippingContextBufferForMask()->_layoutChannelNo; - CubismTextureColor *colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelNo); + const csmInt32 channelIndex = GetClippingContextBufferForMask()->_layoutChannelIndex; + CubismTextureColor *colorChannel = GetClippingContextBufferForMask()->GetClippingManager()->GetChannelFlagAsColor(channelIndex); csmRectF *rect = GetClippingContextBufferForMask()->_layoutBounds; UpdateMatrix(_ubo.clipMatrix, GetClippingContextBufferForMask()->_matrixForMask); UpdateColor(_ubo.baseColor, rect->X * 2.0f - 1.0f, rect->Y * 2.0f - 1.0f, rect->GetRight() * 2.0f - 1.0f, @@ -1143,8 +1148,8 @@ void CubismRenderer_Vulkan::ExecuteDrawForMask(const CubismModel& model, const c UpdateColor(_ubo.channelFlag, colorChannel->R, colorChannel->G, colorChannel->B, colorChannel->A); Descriptor &descriptor = _descriptorSets[index]; descriptor.uniformBuffer.MemCpy(&_ubo, sizeof(ModelUBO)); - csmInt32 textureNo = model.GetDrawableTextureIndex(index); - UpdateDescriptorSet(descriptor, textureNo, false); + csmInt32 textureIndex = model.GetDrawableTextureIndex(index); + UpdateDescriptorSet(descriptor, textureIndex, false); csmUint32 shaderIndex = ShaderNames_SetupMask; csmUint32 blendIndex = Blend_Mask; @@ -1179,8 +1184,8 @@ void CubismRenderer_Vulkan::DrawMeshVulkan(const CubismModel& model, const csmIn return; } - csmInt32 textureNo = model.GetDrawableTextureIndices(index); - if (_textures[textureNo].GetSampler() == VK_NULL_HANDLE || _textures[textureNo].GetView() == VK_NULL_HANDLE) + csmInt32 textureIndex = model.GetDrawableTextureIndices(index); + if (_textures[textureIndex].GetSampler() == VK_NULL_HANDLE || _textures[textureIndex].GetView() == VK_NULL_HANDLE) { return; } diff --git a/src/Rendering/Vulkan/CubismRenderer_Vulkan.hpp b/src/Rendering/Vulkan/CubismRenderer_Vulkan.hpp index 7895e33..46ef3dc 100644 --- a/src/Rendering/Vulkan/CubismRenderer_Vulkan.hpp +++ b/src/Rendering/Vulkan/CubismRenderer_Vulkan.hpp @@ -288,14 +288,18 @@ class CubismRenderer_Vulkan : public CubismRenderer /** * @brief ディスクリプタセットとUBOを保持する構造体 + * コマンドバッファでまとめて描画する場合、ディスクリプタセットをバインド後に + * 更新してはならない。
+ * マスクされる描画対象のみ、フレームバッファをディスクリプタセットに設定する必要があるが、 + * その際バインド済みのディスクリプタセットを更新しないよう、別でマスクされる用のディスクリプタセットを用意する。 */ struct Descriptor { CubismBufferVulkan uniformBuffer; ///< ユニフォームバッファ VkDescriptorSet descriptorSet = VK_NULL_HANDLE; ///< ディスクリプタセット bool isDescriptorSetUpdated = false; ///< ディスクリプタセットが更新されたか - VkDescriptorSet descriptorSetMasked = VK_NULL_HANDLE; ///< マスク用ディスクリプタセット - bool isDescriptorSetMaskedUpdated = false; ///< マスク用ディスクリプタセットが更新されたか + VkDescriptorSet descriptorSetMasked = VK_NULL_HANDLE; ///< マスクされる描画対象用のディスクリプタセット + bool isDescriptorSetMaskedUpdated = false; ///< マスクされる描画対象用のディスクリプタセットが更新されたか }; protected: diff --git a/src/Utils/CubismJson.cpp b/src/Utils/CubismJson.cpp index 9e3761c..ec4d9ae 100644 --- a/src/Utils/CubismJson.cpp +++ b/src/Utils/CubismJson.cpp @@ -131,7 +131,17 @@ csmBool CubismJson::ParseBytes(const csmByte* buffer, csmInt32 size) csmString CubismJson::ParseString(const csmChar* string, csmInt32 length, csmInt32 begin, csmInt32* outEndPos) { - if (_error) return NULL; + if (_error) + { + return NULL; + } + + if (!string) + { + _error = "string is null"; + return NULL; + } + csmInt32 i = begin; csmChar c, c2; csmString ret; @@ -151,7 +161,10 @@ csmString CubismJson::ParseString(const csmChar* string, csmInt32 length, csmInt case '\\': {//エスケープの場合 i++; //2文字をセットで扱う - if (i - 1 > buf_start) ret.Append(static_cast(string + buf_start), (i - buf_start - 1)); //前の文字までを登録する + if (i - 1 > buf_start) + { + ret.Append(static_cast(string + buf_start), (i - buf_start - 1)); //前の文字までを登録する + } buf_start = i + 1; //エスケープ(2文字)の次の文字から if (i < length) @@ -200,7 +213,17 @@ csmString CubismJson::ParseString(const csmChar* string, csmInt32 length, csmInt Value* CubismJson::ParseObject(const csmChar* buffer, csmInt32 length, csmInt32 begin, csmInt32* outEndPos) { - if (_error) return NULL; + if (_error) + { + return NULL; + } + + if (!buffer) + { + _error = "buffer is null"; + return NULL; + } + Map* ret = CSM_NEW Map(); //key : value , @@ -273,7 +296,10 @@ Value* CubismJson::ParseObject(const csmChar* buffer, csmInt32 length, csmInt32 // 値をチェック Value* value = ParseValue(buffer, length, i, local_ret_endpos2); - if (_error) return NULL; + if (_error) + { + return NULL; + } i = local_ret_endpos2[0]; // ret.put( key , value ) ; ret->Put(key, value); @@ -305,7 +331,17 @@ Value* CubismJson::ParseObject(const csmChar* buffer, csmInt32 length, csmInt32 Value* CubismJson::ParseArray(const csmChar* buffer, csmInt32 length, csmInt32 begin, csmInt32* outEndPos) { - if (_error) return NULL; + if (_error) + { + return NULL; + } + + if (!buffer) + { + _error = "buffer is null"; + return NULL; + } + Array* ret = CSM_NEW Array(); //key : value , @@ -318,7 +354,10 @@ Value* CubismJson::ParseArray(const csmChar* buffer, csmInt32 length, csmInt32 b { // : をチェック Value* value = ParseValue(buffer, length, i, local_ret_endpos2); - if (_error) return NULL; + if (_error) + { + return NULL; + } i = local_ret_endpos2[0]; if (value) { @@ -357,7 +396,16 @@ Value* CubismJson::ParseArray(const csmChar* buffer, csmInt32 length, csmInt32 b Value* CubismJson::ParseValue(const csmChar* buffer, csmInt32 length, csmInt32 begin, csmInt32* outEndPos) { - if (_error) return NULL; + if (_error) + { + return NULL; + } + + if (!buffer) + { + _error = "buffer is null"; + return NULL; + } Value* o = NULL; csmInt32 i = begin; @@ -372,12 +420,13 @@ Value* CubismJson::ParseValue(const csmChar* buffer, csmInt32 length, csmInt32 b { case '-': case '.': case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - char* ret_ptr; - f = strtof(const_cast(buffer + i), &ret_ptr); - *outEndPos = static_cast(ret_ptr - buffer); - return CSM_NEW Float(f); - } + case '5': case '6': case '7': case '8': case '9': + { + char* ret_ptr; + f = strtof(const_cast(buffer + i), &ret_ptr); + *outEndPos = static_cast(ret_ptr - buffer); + return CSM_NEW Float(f); + } case '\"': return CSM_NEW String(ParseString(buffer, length, i + 1, outEndPos)); //\"の次の文字から case '[': @@ -434,11 +483,17 @@ Map::~Map() while (ite != _map.End()) { Value* v = (*ite).Second; - if (v && !v->IsStatic()) CSM_DELETE(v); + if (v && !v->IsStatic()) + { + CSM_DELETE(v); + } ++ite; } - if (_keys) CSM_DELETE(_keys); + if (_keys) + { + CSM_DELETE(_keys); + } } @@ -448,7 +503,10 @@ Array::~Array() for (; ite != _array.End(); ++ite) { Value* v = (*ite); - if (v && !v->IsStatic()) CSM_DELETE(v); + if (v && !v->IsStatic()) + { + CSM_DELETE(v); + } } } }}}}