Skip to content

Commit

Permalink
Update to Cubism 5 SDK for Native R1 beta4
Browse files Browse the repository at this point in the history
  • Loading branch information
wada-at-live2d-com committed Jan 25, 2024
1 parent 19d1875 commit bebb8e0
Show file tree
Hide file tree
Showing 28 changed files with 284 additions and 161 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/CubismFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ csmBool CubismFramework::StartUp(ICubismAllocator* allocator, const Option* opti
const csmUint32 major = static_cast<csmUint32>((version & 0xFF000000) >> 24);
const csmUint32 minor = static_cast<csmUint32>((version & 0x00FF0000) >> 16);
const csmUint32 patch = static_cast<csmUint32>((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.");
Expand Down
6 changes: 5 additions & 1 deletion src/Effect/CubismPose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// フェード時間の指定
Expand Down
6 changes: 6 additions & 0 deletions src/Model/CubismModelUserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/Model/CubismUserModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Motion/CubismExpressionMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)); // フェードイン
Expand Down
6 changes: 6 additions & 0 deletions src/Motion/CubismMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<csmInt32, cocos2d::Texture2D*>& CubismRenderer_Cocos2dx::GetBindedTextures() const
Expand Down Expand Up @@ -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];
}

}}}}
Expand Down
6 changes: 3 additions & 3 deletions src/Rendering/Cocos2d/CubismRenderer_Cocos2dx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ class CubismRenderer_Cocos2dx : public CubismRenderer
* @brief OpenGLテクスチャのバインド処理<br>
* 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にバインドされたテクスチャのリストを取得する
Expand Down Expand Up @@ -340,7 +340,7 @@ class CubismRenderer_Cocos2dx : public CubismRenderer
*
* @return バインドされたテクスチャ
*/
cocos2d::Texture2D* GetBindedTexture(csmInt32 textureNo);
cocos2d::Texture2D* GetBindedTexture(csmInt32 textureIndex);


csmMap<csmInt32, cocos2d::Texture2D*> _textures; ///< モデルが参照するテクスチャとレンダラでバインドしているテクスチャとのマップ
Expand Down
16 changes: 8 additions & 8 deletions src/Rendering/Cocos2d/CubismShader_Cocos2dx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

// 頂点配列の設定
Expand All @@ -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);

Expand Down Expand Up @@ -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());

//座標変換
Expand Down
4 changes: 2 additions & 2 deletions src/Rendering/CubismClippingManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 クリッピングマスクバッファのサイズを設定する
Expand Down
53 changes: 29 additions & 24 deletions src/Rendering/CubismClippingManager.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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;
Expand All @@ -292,28 +292,33 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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;
}

// 分割方法を決定する
Expand All @@ -325,12 +330,12 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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)
{
Expand All @@ -339,13 +344,13 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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つに分解して使う
}
}
Expand All @@ -358,13 +363,13 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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)
Expand All @@ -376,13 +381,13 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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;
}
}
// マスクの制限枚数を超えた場合の処理
Expand All @@ -402,7 +407,7 @@ void CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::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;
Expand Down Expand Up @@ -495,9 +500,9 @@ csmInt32 CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::GetRender
}

template <class T_ClippingContext, class T_OffscreenSurface>
CubismRenderer::CubismTextureColor* CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::GetChannelFlagAsColor(csmInt32 channelNo)
CubismRenderer::CubismTextureColor* CubismClippingManager<T_ClippingContext, T_OffscreenSurface>::GetChannelFlagAsColor(csmInt32 channelIndex)
{
return _channelColors[channelNo];
return _channelColors[channelIndex];
}

template <class T_ClippingContext, class T_OffscreenSurface>
Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/CubismRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ CubismClippingContext::CubismClippingContext(const csmInt32* clippingDrawableInd
// マスクの数
_clippingIdCount = clipCount;

_layoutChannelNo = 0;
_layoutChannelIndex = 0;

_allClippedDrawRect = CSM_NEW csmRectF();
_layoutBounds = CSM_NEW csmRectF();
Expand Down
Loading

0 comments on commit bebb8e0

Please sign in to comment.