save code

This commit is contained in:
xsl
2025-12-18 22:05:10 +08:00
parent 9ee028248d
commit e38da96e37
40 changed files with 853 additions and 123 deletions
+112 -74
View File
@@ -17,6 +17,9 @@ FaceApp::FaceApp(/* args */)
FaceApp::~FaceApp()
{
if (worker_.joinable()) {
worker_.join();
}
}
void ReceiveFacePoint(float* pos, int pointCount, int width, int height)
@@ -106,7 +109,8 @@ void TextureLoadProcessWithVulkan(uint8_t* data, int width, int height, int rowS
oldestFrame.rowStride,
oldestFrame.dataSize,
tex_bg,
false
false,
FaceApp::Get()->commandPool
);
frameQueue.pop();
@@ -660,7 +664,7 @@ void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime)
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_sets[_curTexIndex], 0, NULL);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_sets[_curMotionIndex], 0, NULL);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline);
vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue);
@@ -718,22 +722,22 @@ void FaceApp::initVulkan()
m_texs_ex.resize(kMaxTexture);
}
std::vector<unsigned char> data = readFileUnsignedChar("demo0.png", false);
std::vector<unsigned char> data = readFileUnsignedChar("dummy.png", false);
std::vector<unsigned char> image;
unsigned w, h;
unsigned error = lodepng::decode(image, w, h, data, LCT_RGBA, 8);
for (int i = 0; i < kMaxTexture; ++i)
{
loadTexture(image, image.size(), w, h, m_texs[i], true);
loadTexture(image, image.size(), w, h, m_texs_next[i], true);
loadTexture(image, image.size(), w, h, m_texs[i], true, commandPool);
loadTexture(image, image.size(), w, h, m_texs_next[i], true, commandPool);
if (kThick)
{
loadTexture(image, image.size(), w, h, m_texs_ex[i], true);
loadTexture(image, image.size(), w, h, m_texs_ex[i], true, commandPool);
}
}
loadTexture("out.png", tex_bg, true);
loadTexture("out.png", tex_bg, true, commandPool);
createVertexBuffer();
createUniformBuffer();
setup_descriptor_pool();
@@ -1271,6 +1275,8 @@ void FaceApp::cleanup()
// vmaDestroyAllocator(allocator);
// allocator = VK_NULL_HANDLE;
//}
}
@@ -1287,57 +1293,74 @@ void FaceApp::drawFrame(long long frameTime)
return;
}
if (_motionState == loading_next_motion)
{
int i = _nextMotionIndex;
//for (int i = 0; i < _curMotion.png_names.size(); ++i)
{
#ifdef _WIN32
string path = "pic";
string path_name = path + "/" + _nextMotion.type + "/" + _nextMotion.png_names[i];// +std::to_string(i) + ".png";
loadTextureExample(UTF8ToWideString(path_name), m_texs_next[i], true);
#else
//string path = _curMotion.type + "/" + _curMotion.technique + "/" + _curMotion.step + "/" + _curMotion.show_type;
string path = "pic";
string path_name = path + "/" + _nextMotion.type + "/" + _nextMotion.png_names[i];// +std::to_string(i) + ".png";
loadTexture(path_name, m_texs_next[i], true);
#endif // _WIN32
_nextMotionIndex++;
if (_nextMotionIndex >= _nextMotion.png_names.size())
{
_motionState = load_next_motion_finished;
_callback(_nextMotion.type);
}
}
}
// if (_motionState == loading_next_motion)
// {
// int i = _nextMotionIndex;
// //for (int i = 0; i < _curMotion.png_names.size(); ++i)
// {
//
//#ifdef _WIN32
// string path = "pic";
// string path_name = path + "/" + _nextMotion.name + "/" + _nextMotion.png_names[i];// +std::to_string(i) + ".png";
// loadTextureExample(UTF8ToWideString(path_name), m_texs_next[i], true);
//#else
// //string path = _curMotion.type + "/" + _curMotion.technique + "/" + _curMotion.step + "/" + _curMotion.show_type;
// string path = "pic";
// string path_name = path + "/" + _nextMotion.type + "/" + _nextMotion.png_names[i];// +std::to_string(i) + ".png";
// loadTexture(path_name, m_texs_next[i], true);
//#endif // _WIN32
//
// _nextMotionIndex++;
// if (_nextMotionIndex >= _nextMotion.png_names.size())
// {
// _motionState = load_next_motion_finished;
// _callback_loadfinish(_nextMotion.name);
// }
// }
// }
std::unique_lock lock(changeMotionMtx);
static long long game_time = 0;
game_time += frameTime;
long long actionTime = (1000 / _initArg.action_fps);
if (game_time > actionTime)
{
_curTexIndex++;
if (_curTexIndex >= _curMotion_png_size)
{
if(_animationFinishedCallback != nullptr)
{
_animationFinishedCallback();
}
if (_animationLoop)
if (_curMotions.size() > 0)
{
static long long game_time = 0;
game_time += frameTime;
long long actionTime = (1000 / _initArg.action_fps);
if (game_time > actionTime)
{
if (_curFrameIndex < _curMotions[_curMotionIndex].frames.size() - 1)
{
_curTexIndex = 0;
_curFrameIndex += 1;
}
else
{
_curTexIndex = _curMotion_png_size - 1;
if (_curMotionIndex < _curMotions.size() - 1)
{
_curMotionIndex++;
_curFrameIndex = 0;
}
else
{
if (_animationFinishedCallback != nullptr)
{
_animationFinishedCallback();
}
if (_animationLoop)
{
_curMotionIndex = 0;
_curFrameIndex = 0;
}
else
{
_animationFinishedCallback = nullptr;
}
}
}
game_time = game_time - actionTime;
}
game_time = game_time - actionTime;
}
Application::drawFrame(frameTime);
}
@@ -1347,36 +1370,52 @@ void FaceApp::drawFrame(long long frameTime)
// changeMotion(motion);
//}
string FaceApp::preReadyMotion(const std::string& json, Callback callback)
string FaceApp::preLoadMotionList(const vector<string>& motions, Callback callback)
{
if (_motionState == loading_next_motion)
if (_isLoadMotion)
{
return "busy on loading:" + _nextMotion.type;
return "failue load not finished";
}
else if (_motionState == load_next_motion_finished)
_isLoadMotion = true;
_callback_loadfinish = callback;
_preLoadMotions.clear();
for (auto s : motions)
{
return "waitting change next action:" + _nextMotion.type;
json j = json::parse(s);
Motion motion = j.get<Motion>();
_preLoadMotions.push_back(motion);
}
else if (_motionState == ready)
{
Motion motion = json::parse(json);
_nextMotion = motion;
_motionState = loading_next_motion;
_nextMotionIndex = 0;
_callback = callback;
return "success";
}
return "failure";
worker_ = std::thread(&FaceApp::loadMotionThread, this);
return "ok";
}
void FaceApp::changeMotion(const string motion_type, AnimationFinishedCallback callback, bool loop)
void FaceApp::loadMotionThread()
{
if (_curMotion_type == motion_type)
for (int i = 0; i < _preLoadMotions.size(); ++i)
{
return;
}
if (!_running)
{
break;
}
if (_motionState != load_next_motion_finished)
#ifdef _WIN32
string path = "pic";
string path_name = path + "/" + _preLoadMotions[i].name + "/" + _preLoadMotions[i].name + "ex.png";
loadTextureExample(UTF8ToWideString(path_name), m_texs_next[i], true, commandPool_ex);
#else
string path = "pic";
string path_name = path + "/" + _preLoadMotions[i].name + "/" + _preLoadMotions[i].name + "ex.png";
loadTexture(path_name, m_texs_next[i], true, commandPool_ex);
#endif // _WIN32
}
_isLoadMotion = false;
_callback_loadfinish();
}
void FaceApp::changeMotionList(AnimationFinishedCallback callback, bool loop)
{
if (_isLoadMotion)
{
return;
}
@@ -1384,24 +1423,23 @@ void FaceApp::changeMotion(const string motion_type, AnimationFinishedCallback c
std::unique_lock<std::mutex> lock_changeMotion(changeMotionMtx);
for (int i = 0; i < _nextMotion.png_names.size(); ++i)
for (int i = 0; i < _preLoadMotions.size(); ++i)
{
destroyTexture(device, m_texs[i]);
}
for (int i = 0; i < _nextMotion.png_names.size(); ++i)
for (int i = 0; i < _preLoadMotions.size(); ++i)
{
m_texs[i] = m_texs_next[i];
m_texs_next[i].reset();
}
update_descriptor_set(m_texs);
_curMotion_type = motion_type;
_curMotion_png_size = _nextMotion.png_names.size();
_motionState = ready;
_curMotions = _preLoadMotions;
_animationFinishedCallback = callback;
_animationLoop = loop;
_curTexIndex = 0;
_curFrameIndex = 0;
_curMotionIndex = 0;
// for (int i = 0; i < _curMotion.png_names.size(); ++i)