diff --git a/src/ImageBuffer.cpp b/src/ImageBuffer.cpp index 4a2a5d1..2c7f52d 100644 --- a/src/ImageBuffer.cpp +++ b/src/ImageBuffer.cpp @@ -16,9 +16,15 @@ void ImageBuffer::Uninitialize() { void ImageBuffer::PushImage(ImageBuffer::IBType type, std::vector data, uint32 width, uint32 height, uint32 comp) { switch (type) { - case ImageBuffer::IBType::Background: - PushBackgroundImage(std::move(data), width, height, comp); + case ImageBuffer::IBType::Human: + PushHumanImage(std::move(data), width, height, comp); break; + case ImageBuffer::IBType::Face: + PushFaceImage(std::move(data), width, height, comp); + break; + case ImageBuffer::IBType::FaceMask: + PushFaceMaskImage(std::move(data), width, height, comp); + break; default: break; } @@ -26,28 +32,71 @@ void ImageBuffer::PushImage(ImageBuffer::IBType type, std::vector void ImageBuffer::Update(IBType type, Texture2D* texture) { switch (type) { - case ImageBuffer::IBType::Background: - UpdateBackground(texture); + case ImageBuffer::IBType::Human: + UpdateHuman(texture); break; + + case IBType::Face: + UpdateFace(texture); + + case IBType::FaceMask: + UpdateFaceMask(texture); + default: break; } } -void ImageBuffer::PushBackgroundImage(std::vector data, uint32 width, uint32 height, uint32 comp) { - ScopeLock lock(&cs_); +void ImageBuffer::PushHumanImage(std::vector data, uint32 width, uint32 height, uint32 comp) { + ScopeLock lock(&human_cs_); - background_.data = std::move(data); - background_.width = width; - background_.height = height; - background_.comp = comp; + human_.data = std::move(data); + human_.width = width; + human_.height = height; + human_.comp = comp; } -void ImageBuffer::UpdateBackground(Texture2D* texture) { - if (background_.data.empty()) { +void ImageBuffer::UpdateHuman(Texture2D* texture) { + if (human_.data.empty()) { return; } - ScopeLock lock(&cs_); - texture->Update(background_.width, background_.height, background_.comp, background_.data.data()); + ScopeLock lock(&human_cs_); + texture->Update(human_.width, human_.height, human_.comp, human_.data.data()); +} + +void ImageBuffer::PushFaceImage(std::vector data, uint32 width, uint32 height, uint32 comp) { + ScopeLock lock(&face_cs_); + + face_.data = std::move(data); + face_.width = width; + face_.height = height; + face_.comp = comp; +} + +void ImageBuffer::UpdateFace(class Texture2D* texture) { + if (face_.data.empty()) { + return; + } + + ScopeLock lock(&face_cs_); + texture->Update(face_.width, face_.height, face_.comp, face_.data.data()); +} + +void ImageBuffer::PushFaceMaskImage(std::vector data, uint32 width, uint32 height, uint32 comp) { + ScopeLock lock(&face_mask_cs_); + + faceMask_.data = std::move(data); + faceMask_.width = width; + faceMask_.height = height; + faceMask_.comp = comp; +} + +void ImageBuffer::UpdateFaceMask(class Texture2D* texture) { + if (faceMask_.data.empty()) { + return; + } + + ScopeLock lock(&face_mask_cs_); + texture->Update(faceMask_.width, faceMask_.height, faceMask_.comp, faceMask_.data.data()); } diff --git a/src/ImageBuffer.h b/src/ImageBuffer.h index 6ea35e8..48a2716 100644 --- a/src/ImageBuffer.h +++ b/src/ImageBuffer.h @@ -9,8 +9,9 @@ class ImageBuffer : public Singleton { public: enum class IBType { - Background, - + Human, + Face, + FaceMask, }; public: explicit ImageBuffer() noexcept = default; @@ -26,8 +27,14 @@ public: void Update(IBType type, class Texture2D* texture); private: - void PushBackgroundImage(std::vector data, uint32 width, uint32 height, uint32 comp); - void UpdateBackground(class Texture2D* texture); + void PushHumanImage(std::vector data, uint32 width, uint32 height, uint32 comp); + void UpdateHuman(class Texture2D* texture); + + void PushFaceImage(std::vector data, uint32 width, uint32 height, uint32 comp); + void UpdateFace(class Texture2D* texture); + void PushFaceMaskImage(std::vector data, uint32 width, uint32 height, uint32 comp); + void UpdateFaceMask(class Texture2D* texture); + private: struct Image { std::vector data; @@ -35,8 +42,14 @@ private: uint32 height; uint32 comp; }; - CriticalSection cs_; - Image background_; + CriticalSection human_cs_; + Image human_; + + CriticalSection face_cs_; + Image face_; + + CriticalSection face_mask_cs_; + Image faceMask_; }; diff --git a/src/Ipc/IpcMoudle.cpp b/src/Ipc/IpcMoudle.cpp index b1ed62d..be98360 100644 --- a/src/Ipc/IpcMoudle.cpp +++ b/src/Ipc/IpcMoudle.cpp @@ -38,7 +38,13 @@ void OnParseImageData(const char* data, size_t size) { std::vector img_data(img_size); memcpy(img_data.data(), img_bytes, img_size); - ImageBuffer::Get()->PushImage(ImageBuffer::IBType::Background, std::move(img_data), width, height, bit_depth); + if (0x01 == identifier) { + ImageBuffer::Get()->PushImage(ImageBuffer::IBType::Human, std::move(img_data), width, height, bit_depth); + } else if (0x02 == identifier) { + ImageBuffer::Get()->PushImage(ImageBuffer::IBType::Face, std::move(img_data), width, height, bit_depth); + } else if (0x03 == identifier) { + ImageBuffer::Get()->PushImage(ImageBuffer::IBType::FaceMask, std::move(img_data), width, height, bit_depth); + } // Further processing of img_bytes can be done here } diff --git a/src/Main.cpp b/src/Main.cpp index 55f691c..a975864 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -128,13 +128,22 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI } stbi_image_free(data); + int32 imageWidth = 1080; + int32 imageHeight = 1920; Texture2D huaman; - huaman.Create(1920, 1080, 4, nullptr); + huaman.Create(imageWidth, imageHeight, 4, nullptr); + + Texture2D huamanFace; + huamanFace.Create(imageWidth, imageHeight, 3, nullptr); + Texture2D huamanFaceMask; + huamanFace.Create(imageWidth, imageHeight, 3, nullptr); Shader shader("./data/shader/texture.vs", "./data/shader/texture.fs"); shader.use(); shader.setInt("background", 0); shader.setInt("human", 1); + shader.setInt("face", 2); + shader.setInt("face_mask", 3); while (!glfwWindowShouldClose(window)) { @@ -152,12 +161,16 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI view = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); projection = glm::perspective(glm::radians(60.f), ratio, 1.f, 1000.f); - ImageBuffer::Get()->Update(ImageBuffer::IBType::Background, &huaman); + ImageBuffer::Get()->Update(ImageBuffer::IBType::Human, &huaman); + ImageBuffer::Get()->Update(ImageBuffer::IBType::Face, &huamanFace); + ImageBuffer::Get()->Update(ImageBuffer::IBType::FaceMask, &huamanFaceMask); glBindVertexArray(VAO); shader.use(); background.Active(0); huaman.Active(1); + huamanFace.Active(2); + huamanFaceMask.Active(3); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/RHI/OpenglDrv/gl.h b/src/RHI/OpenglDrv/gl.h index e2059cc..05feb4e 100644 --- a/src/RHI/OpenglDrv/gl.h +++ b/src/RHI/OpenglDrv/gl.h @@ -1,15 +1,17 @@ #pragma once -//#include #include "Core/Logger.h" #define GLEW_STATIC #include #include -#define __CHECK_GL_ERROR__ { \ - auto gl_error_code=glGetError();\ - if(gl_error_code!=GL_NO_ERROR){\ - ERRORLOG("gl_error_code: {}", static_cast(gl_error_code));\ - }\ +inline void CheckGLError(const char* file, int line) { + GLenum err = glGetError(); + if (err != GL_NO_ERROR) { + ERRORLOG("GL Error: {} - {}:{}", err, file, line); + assert(false); } +} + +#define __CHECK_GL_ERROR__ CheckGLError(__FILE__, __LINE__); diff --git a/src/Texture2D.cpp b/src/Texture2D.cpp index 5fcf167..88a2ca3 100644 --- a/src/Texture2D.cpp +++ b/src/Texture2D.cpp @@ -31,22 +31,14 @@ bool Texture2D::Create(int32 width, int32 height, int32 nrChannels, uint8* data) } else if (nrChannels == 4) { format = GL_RGBA; } - bool alloced = false; - if (nullptr == data) { - const uint32 size = width * height * nrChannels; - data = new uint8[size]; - memset(data, 0, size); - alloced = true; - } glBindTexture(GL_TEXTURE_2D, texture_); glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); __CHECK_GL_ERROR__ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); __CHECK_GL_ERROR__ - if (alloced) { - delete[] data; - } + + glBindTexture(GL_TEXTURE_2D, 0); //glGenerateMipmap(GL_TEXTURE_2D); return true; @@ -56,13 +48,15 @@ void Texture2D::Update(int32 width, int32 height, int32 channels, uint8* data) n if (0 == texture_ || nullptr == data) { return; } + glBindTexture(GL_TEXTURE_2D, texture_); __CHECK_GL_ERROR__ glPixelStorei(GL_UNPACK_ALIGNMENT, 1); __CHECK_GL_ERROR__ if (3 == channels){ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); __CHECK_GL_ERROR__ } else if (4 == channels) { glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); __CHECK_GL_ERROR__ - } + } + glBindTexture(GL_TEXTURE_2D, 0); __CHECK_GL_ERROR__ } void Texture2D::Active(int32 txture) {