modify gl check and ipc moudle

This commit is contained in:
brige 2024-12-02 23:01:10 +08:00
parent 0e8879d543
commit 96a324e5b7
6 changed files with 117 additions and 40 deletions

View File

@ -16,8 +16,14 @@ void ImageBuffer::Uninitialize() {
void ImageBuffer::PushImage(ImageBuffer::IBType type, std::vector<unsigned char> 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<unsigned char>
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<uint8> data, uint32 width, uint32 height, uint32 comp) {
ScopeLock<CriticalSection> lock(&cs_);
void ImageBuffer::PushHumanImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp) {
ScopeLock<CriticalSection> 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<CriticalSection> lock(&cs_);
texture->Update(background_.width, background_.height, background_.comp, background_.data.data());
ScopeLock<CriticalSection> lock(&human_cs_);
texture->Update(human_.width, human_.height, human_.comp, human_.data.data());
}
void ImageBuffer::PushFaceImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp) {
ScopeLock<CriticalSection> 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<CriticalSection> lock(&face_cs_);
texture->Update(face_.width, face_.height, face_.comp, face_.data.data());
}
void ImageBuffer::PushFaceMaskImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp) {
ScopeLock<CriticalSection> 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<CriticalSection> lock(&face_mask_cs_);
texture->Update(faceMask_.width, faceMask_.height, faceMask_.comp, faceMask_.data.data());
}

View File

@ -9,8 +9,9 @@
class ImageBuffer : public Singleton<ImageBuffer> {
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<uint8> data, uint32 width, uint32 height, uint32 comp);
void UpdateBackground(class Texture2D* texture);
void PushHumanImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp);
void UpdateHuman(class Texture2D* texture);
void PushFaceImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp);
void UpdateFace(class Texture2D* texture);
void PushFaceMaskImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp);
void UpdateFaceMask(class Texture2D* texture);
private:
struct Image {
std::vector<uint8> 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_;
};

View File

@ -38,7 +38,13 @@ void OnParseImageData(const char* data, size_t size) {
std::vector<uint8> 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
}

View File

@ -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);

View File

@ -1,15 +1,17 @@
#pragma once
//#include <glad/glad.h>
#include "Core/Logger.h"
#define GLEW_STATIC
#include <GL/glew.h>
#include <glfw/glfw3.h>
#define __CHECK_GL_ERROR__ { \
auto gl_error_code=glGetError();\
if(gl_error_code!=GL_NO_ERROR){\
ERRORLOG("gl_error_code: {}", static_cast<int>(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__);

View File

@ -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,6 +48,7 @@ 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){
@ -63,6 +56,7 @@ void Texture2D::Update(int32 width, int32 height, int32 channels, uint8* data) n
} 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) {