add opengl drv

This commit is contained in:
jiegeaiai 2024-11-29 01:09:00 +08:00
parent 4ea6dadbad
commit fcf8cfc7f4
21 changed files with 257 additions and 98 deletions

View File

@ -2,51 +2,84 @@
#include <assert.h> #include <assert.h>
#include "Core/Logger.h" #include "Core/Core.h"
#include "RHI/RHI.h" #include "RHI/RHI.h"
#include "Renderer/MeshRender.h"
//
//static const glm::vec3 kPositions[3] = {
// glm::vec3{ -0.5f, -0.5f,0.0f},
// glm::vec3{ 0.5f, -0.5f,0.0f},
// glm::vec3{ 0.f, 0.5f,0.0f}
//};
//
//static const glm::vec4 kColors[3] = {
// glm::vec4{ 1.f, 0.f, 0.f ,1.f},
// glm::vec4{ 0.f, 1.f, 0.f ,1.f},
// glm::vec4{ 0.f, 0.f, 1.f ,1.f}
//};
Application* s_Application = nullptr; Mesh::Vertex vertices[3] = {
{{ -0.5f, -0.5f,0.0f}, { 1.f, 0.f, 0.f ,1.f},{ 0.0f, 0.0f }, { 0.0f, 0.0f,0.0f}},
{{ 0.5f, -0.5f,0.0f}, { 0.f, 1.f, 0.f ,1.f},{ 0.0f, 1.0f }, { 0.0f, 0.0f,0.0f}},
{{ 0.f, 0.5f,0.0f}, { 0.f, 0.f, 1.f ,1.f},{ 0.0f, 1.0f }, { 0.0f, 0.0f,0.0f}},
};
Application::Application() { Application* s_Application = nullptr;
assert(nullptr == s_Application);
s_Application = this;
Logger::Init(); Application::Application() {
assert(nullptr == s_Application);
s_Application = this;
Logger::Init();
}
Application::~Application() {
Logger::Shotdown();
assert(nullptr != s_Application);
s_Application = nullptr;
}
Application* Application::Get() {
return s_Application;
}
bool Application::Initialize() {
if (!RHI::Init()) {
return false;
} }
Application::~Application() { return true;
Logger::Shotdown(); }
assert(nullptr != s_Application);
s_Application = nullptr; int Application::RunLoop() {
const DriverSettings& setting = RHI::Get()->GetDriverSettings();
if (nullptr == setting.isWindowShouldClose) {
return -1;
} }
Application* Application::Get() { RHI::Get()->StartRender();
return s_Application;
mesh_ = new Mesh;
mesh_->SetVertices(vertices, 3, false);
meshRender_ = new MeshRender(mesh_);
while (!setting.isWindowShouldClose()) {
RHI::Get()->BeginFrame();
UpdateScene();
RHI::Get()->EndFrame();
RHI::Get()->PollEvents();
} }
RHI::Get()->StopRender();
return 0;
}
bool Application::Initialize() { void Application::Uninitialize() {
if (!RHI::Init()) { RHI::Shotdown();
return false; }
}
return true; void Application::UpdateScene() {
} meshRender_->Render();
}
int Application::RunLoop() {
const DriverSettings& setting = RHI::Get()->GetDriverSettings();
if (nullptr == setting.isWindowShouldClose) {
return -1;
}
RHI::Get()->StartRenderThread();
while (!setting.isWindowShouldClose()) {
RHI::Get()->PollEvents();
}
return 0;
}
void Application::Uninitialize() {
RHI::Shotdown();
}

View File

@ -12,6 +12,7 @@ public:
virtual bool Initialize(); virtual bool Initialize();
virtual int RunLoop(); virtual int RunLoop();
virtual void Uninitialize(); virtual void Uninitialize();
virtual void UpdateScene();
float GeltaSeconds() const { float GeltaSeconds() const {
return m_deltaSeconds; return m_deltaSeconds;
@ -21,4 +22,6 @@ protected:
double m_timeStamp{ 0.0 }; double m_timeStamp{ 0.0 };
float m_deltaSeconds{ 0.0f }; float m_deltaSeconds{ 0.0f };
class Mesh* mesh_{ nullptr };
class MeshRender* meshRender_{ nullptr };
}; };

View File

@ -7,6 +7,7 @@ SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")
FILE(GLOB_RECURSE HEADER_FILES ./*.h) FILE(GLOB_RECURSE HEADER_FILES ./*.h)
FILE(GLOB_RECURSE CPP_FILES ./*.cpp) FILE(GLOB_RECURSE CPP_FILES ./*.cpp)
FILE(GLOB_RECURSE CC_FILES ./*.cc) FILE(GLOB_RECURSE CC_FILES ./*.cc)
FILE(GLOB_RECURSE CC_FILES ./*.c)
SET( SET(
ALL_FILES ALL_FILES

View File

@ -13,7 +13,7 @@ Logger::~Logger() {
} }
bool Logger::Initialize() { bool Logger::Initialize() {
logPtr = spdlog::daily_logger_format_mt("daily_logger", "./ProjectorPlayer.log", 23, 59); logPtr = spdlog::daily_logger_format_mt("daily_logger", "./human_render.log", 23, 59);
logPtr->set_level(spdlog::level::trace); logPtr->set_level(spdlog::level::trace);

View File

@ -34,6 +34,7 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI
#else #else
#if 0
#include <windows.h> #include <windows.h>
@ -111,7 +112,22 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
} }
return 0; return 0;
} }
#else
//#include <glad/glad.h>
#include <GLFW/glfw3.h>
static int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, wchar_t* lpCmdLine, int nCmdShow)
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
return 0;
}
#endif
#endif #endif

View File

@ -7,6 +7,7 @@
using DriverSettingsBoolNoParamFunction = std::function<bool()>; using DriverSettingsBoolNoParamFunction = std::function<bool()>;
using DriverSettingsVoidNoParamFunction = std::function<void()>; using DriverSettingsVoidNoParamFunction = std::function<void()>;
using DriverSettingsGetBufferSizeFunction = std::function<void(int32* width, int32* height)>;
struct DriverSettings { struct DriverSettings {
bool debugProfile{ false }; bool debugProfile{ false };
bool forwardCompatibility{ false }; bool forwardCompatibility{ false };
@ -16,4 +17,5 @@ struct DriverSettings {
DriverSettingsBoolNoParamFunction isWindowShouldClose{ nullptr }; DriverSettingsBoolNoParamFunction isWindowShouldClose{ nullptr };
DriverSettingsVoidNoParamFunction renderMakeCurrentContext{ nullptr }; DriverSettingsVoidNoParamFunction renderMakeCurrentContext{ nullptr };
DriverSettingsVoidNoParamFunction renderSwapBuffers{ nullptr }; DriverSettingsVoidNoParamFunction renderSwapBuffers{ nullptr };
DriverSettingsGetBufferSizeFunction getBufferSize{ nullptr };
}; };

View File

@ -56,5 +56,6 @@ public:
virtual RHICommond* DeleteProgramCommand( virtual RHICommond* DeleteProgramCommand(
unsigned int programHandle unsigned int programHandle
) = 0; ) = 0;
virtual RHICommond* CreateStartCommand() = 0;
virtual RHICommond* CreateEndCommand() = 0; virtual RHICommond* CreateEndCommand() = 0;
}; };

View File

@ -1,8 +1,6 @@
#include "RHI/OpenglDrv/GLDrvAPI.h" #include "RHI/OpenglDrv/GLDrvAPI.h"
#define GLEW_STATIC #include "RHI/OpenglDrv/gl.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Core/Logger.h" #include "Core/Logger.h"
@ -29,22 +27,26 @@ bool GLDrvAPI::InitEnv(const DriverSettings& setting) {
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
} }
glewExperimental = GL_TRUE;
return true; return true;
} }
bool GLDrvAPI::InitRenderEnv(const DriverSettings& setting) { bool GLDrvAPI::InitRenderEnv(const DriverSettings& setting) {
// Initialize GLEW to setup the OpenGL Function pointers // Initialize GLEW to setup the OpenGL Function pointers
//glfwMakeContextCurrent(reinterpret_cast<GLFWwindow*>(setting.windowHandle)); //glfwMakeContextCurrent(reinterpret_cast<GLFWwindow*>(setting.windowHandle));
/*GLenum err = glewInit(); glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) { if (GLEW_OK != err) {
const GLubyte* errorString = glewGetErrorString(err); const GLubyte* errorString = glewGetErrorString(err);
ERRORLOG("glewInit={1}, err={2}", err, reinterpret_cast<const char*>(errorString)); ERRORLOG("glewInit={1}, err={2}", err, reinterpret_cast<const char*>(errorString));
return false; return false;
}*/ }
//glfwSwapInterval(1); //glfwSwapInterval(1);
//gladLoadGL(glfwGetProcAddress); //if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
// ERRORLOG("Failed to initialize GLAD");
// return false;
//}
glfwSwapInterval(1); glfwSwapInterval(1);
return true; return true;
} }
@ -118,6 +120,12 @@ RHICommond* GLDrvAPI::DeleteProgramCommand(uint32 programHandle) {
return endCommand; return endCommand;
} }
RHICommond* GLDrvAPI::CreateStartCommand() {
auto endCommand = new GLStartCommand();
return endCommand;
}
RHICommond* GLDrvAPI::CreateEndCommand() { RHICommond* GLDrvAPI::CreateEndCommand() {
auto endCommand = new GLEndCommand(); auto endCommand = new GLEndCommand();
return endCommand; return endCommand;

View File

@ -55,5 +55,6 @@ public:
RHICommond* DeleteProgramCommand( RHICommond* DeleteProgramCommand(
uint32 programHandle uint32 programHandle
) override; ) override;
RHICommond* CreateStartCommand() override;
RHICommond* CreateEndCommand() override; RHICommond* CreateEndCommand() override;
}; };

View File

@ -1,10 +1,10 @@
#include "RHI/OpenglDrv/GLRHICommond.h" #include "RHI/OpenglDrv/GLRHICommond.h"
#define GLEW_STATIC
#include <GL/glew.h>
#include <glfw/glfw3.h>
#include "Core/Logger.h" #include <glm/glm.hpp>
#include <glm/ext.hpp>
#include "RHI/OpenglDrv/gl.h"
#include "RHI/RHI.h" #include "RHI/RHI.h"
#include "GLHandleMapper.h" #include "GLHandleMapper.h"
@ -241,12 +241,42 @@ void GLProgramDeleteCommand::OnExecute() {
} }
} }
GLStartCommand::GLStartCommand()
: RHICommond(RHICommond::Type::START_FRAME) {
const DriverSettings& setting = RHI::Get()->GetDriverSettings();
if (nullptr == setting.getBufferSize) {
return;
}
setting.getBufferSize(&width_, &height_);
}
void GLStartCommand::OnExecute() {
float ratio;
glm::mat4 model, view, projection, mvp;
height_ = height_ == 0 ? 1 : height_;
ratio = width_ / (float)height_;
glViewport(0, 0, width_, height_);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(49.f / 255, 77.f / 255, 121.f / 255, 1.f);
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);
}
GLEndCommand::GLEndCommand() GLEndCommand::GLEndCommand()
: RHICommond(RHICommond::Type::END_FRAME) { : RHICommond(RHICommond::Type::END_FRAME) {
//INFOLOG("actor {}", spdlog::fmt_lib::ptr(this));
} }
GLEndCommand::~GLEndCommand() {
//INFOLOG("dctor {}", spdlog::fmt_lib::ptr(this));
}
void GLEndCommand::OnExecute() { void GLEndCommand::OnExecute() {
const DriverSettings& setting = RHI::Get()->GetDriverSettings(); const DriverSettings& setting = RHI::Get()->GetDriverSettings();
//glfwSwapBuffers(reinterpret_cast<GLFWwindow*>(setting.windowHandle)); if (nullptr == setting.renderSwapBuffers) {
return;
}
setting.renderSwapBuffers();
} }

View File

@ -5,7 +5,7 @@
#include "RHI/RHICommond.h" #include "RHI/RHICommond.h"
#include "Core/Mesh.h" #include "Core/Core.h"
class GLVAOCreateCommand : public RHICommond { class GLVAOCreateCommand : public RHICommond {
@ -167,11 +167,23 @@ private:
uint32 programHandle_{ 0 }; uint32 programHandle_{ 0 };
}; };
class GLStartCommand : public RHICommond {
public:
GLStartCommand();
virtual ~GLStartCommand() = default;
protected:
void OnExecute() override;
private:
int32 width_{ 1 };
int32 height_{ 1 };
};
class GLEndCommand : public RHICommond { class GLEndCommand : public RHICommond {
public: public:
GLEndCommand(); GLEndCommand();
virtual ~GLEndCommand() = default; virtual ~GLEndCommand();
protected: protected:
void OnExecute() override; void OnExecute() override;

View File

@ -2,9 +2,7 @@
#include <cassert> #include <cassert>
#define GLEW_STATIC #include "RHI/OpenglDrv/gl.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Core/StringUtil.h" #include "Core/StringUtil.h"
#include "RHI/OpenglDrv/GLHandleMapper.h" #include "RHI/OpenglDrv/GLHandleMapper.h"

7
src/RHI/OpenglDrv/gl.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
//#include <glad/glad.h>
#define GLEW_STATIC
#include <GL/glew.h>
#include <glfw/glfw3.h>

View File

@ -1,6 +1,6 @@
#include "RHI/RHI.h" #include "RHI/RHI.h"
#include "Core/Logger.h" #include "Core/Core.h"
#include "Core/SpinLock.h" #include "Core/SpinLock.h"
#include "OpenGLDrv/GLDrvAPI.h" #include "OpenGLDrv/GLDrvAPI.h"
@ -30,7 +30,6 @@ bool RHI::Initialize() {
void RHI::Uninitialize() { void RHI::Uninitialize() {
if (rhiCommonList_) { if (rhiCommonList_) {
rhiCommonList_->Stop();
rhiCommonList_.reset(); rhiCommonList_.reset();
} }
@ -41,13 +40,22 @@ void RHI::Uninitialize() {
rhiAPI_->UninitEnv(); rhiAPI_->UninitEnv();
} }
bool RHI::StartRenderThread() { bool RHI::StartRender() {
if (nullptr == rhiCommonList_ || !rhiCommonList_->Start()) { if (nullptr == rhiCommonList_ || !rhiCommonList_->Start()) {
ERRORLOG("StartRenderThread start failed"); ERRORLOG("StartRenderThread start failed");
return false; return false;
} }
return true; return true;
}
bool RHI::StopRender() {
if (nullptr == rhiCommonList_) {
ERRORLOG("StartRenderThread stop failed");
return false;
}
rhiCommonList_->Stop();
return true;
} }
bool RHI::InitRenderEnv() { bool RHI::InitRenderEnv() {
@ -65,13 +73,19 @@ void RHI::UpdaeRenderSize() {
} }
void RHI::UpdateScreenSize() { void RHI::UpdateScreenSize() {
auto task = UpdateRenderSizeCommand::Create(); SharedPtr<RHICommond> task = UpdateRenderSizeCommand::Create();
rhiCommonList_->Push(task); rhiCommonList_->Push(task);
task->Wait(); task->Wait();
} }
void RHI::BeginFrame() {
SharedPtr<RHICommond> task = rhiAPI_->CreateStartCommand();
rhiCommonList_->Push(task);
}
void RHI::EndFrame() { void RHI::EndFrame() {
auto task = rhiAPI_->CreateEndCommand(); SharedPtr<RHICommond> task = rhiAPI_->CreateEndCommand();
rhiCommonList_->Push(task); rhiCommonList_->Push(task);
task->Wait(); task->Wait();
} }
@ -80,18 +94,6 @@ void RHI::PollEvents() {
rhiAPI_->PollEvents(); rhiAPI_->PollEvents();
} }
void RHI::StartRender() {
auto task = StartRenderCommand::Create();
rhiCommonList_->Push(task);
task->Wait();
}
void RHI::StopRender() {
auto task = EndRenderCommand::Create();
rhiCommonList_->Push(task);
task->Wait();
}
void RHI::Push(class RHICommond* commond) { void RHI::Push(class RHICommond* commond) {
rhiCommonList_->Push(commond); rhiCommonList_->Push(commond);
} }

View File

@ -15,15 +15,15 @@ public:
bool Initialize() override; bool Initialize() override;
void Uninitialize() override; void Uninitialize() override;
bool StartRenderThread(); bool StartRender();
bool StopRender();
bool InitRenderEnv(); bool InitRenderEnv();
void UpdaeRenderSize(); void UpdaeRenderSize();
void UpdateScreenSize(); void UpdateScreenSize();
void BeginFrame();
void EndFrame(); void EndFrame();
void PollEvents(); void PollEvents();
void StartRender();
void StopRender();
void Push(class RHICommond* commond); void Push(class RHICommond* commond);

View File

@ -2,9 +2,6 @@
#include "RHI/RHI.h" #include "RHI/RHI.h"
void StartRenderCommand::OnExecute() {
RHI::Get()->InitRenderEnv();
}
void UpdateRenderSizeCommand::OnExecute() { void UpdateRenderSizeCommand::OnExecute() {
RHI::Get()->UpdaeRenderSize(); RHI::Get()->UpdaeRenderSize();

View File

@ -44,6 +44,7 @@ public:
SET_STENCIL_FUNC, SET_STENCIL_FUNC,
SET_STENCIL_OP, SET_STENCIL_OP,
SET_STENCIL_BUFFER_CLEAR_VALUE, SET_STENCIL_BUFFER_CLEAR_VALUE,
START_FRAME,
END_FRAME, END_FRAME,
START_RENDER, START_RENDER,
END_RENDER, END_RENDER,
@ -76,19 +77,6 @@ private:
IMPLEMENT_OBJECT_REFCOUN(RHICommond) IMPLEMENT_OBJECT_REFCOUN(RHICommond)
}; };
class StartRenderCommand : public RHICommond {
public:
virtual ~StartRenderCommand() = default;
static RHICommond* Create() {
RHICommond* Self(new StartRenderCommand);
return Self;
}
protected:
StartRenderCommand() : RHICommond(RHICommond::Type::END_RENDER) {}
void OnExecute() override;
};
class UpdateRenderSizeCommand : public RHICommond { class UpdateRenderSizeCommand : public RHICommond {
public: public:
virtual ~UpdateRenderSizeCommand() = default; virtual ~UpdateRenderSizeCommand() = default;

View File

@ -31,15 +31,21 @@ bool RHICommonList::Start() {
while (isRunning_) { while (isRunning_) {
{ {
std::unique_lock<std::mutex> lock(queueMutex_); std::unique_lock<std::mutex> lock(queueMutex_);
queueCondVar_.wait(lock, [this]() { return !queue_.empty() || !isRunning_; }); queueCondVar_.wait(lock, [this, driverSetting]() { return !queue_.empty() || !isRunning_; });
if (!isRunning_) {
break;
}
while (!queue_.empty() && isRunning_) { while (!queue_.empty()) {
SharedPtr<RHICommond> commond = queue_.front(); SharedPtr<RHICommond> commond = queue_.front();
queue_.pop(); queue_.pop();
if (!isRunning_) {
break;
}
commond->Execute(); commond->Execute();
} }
} }
driverSetting.renderSwapBuffers();
} }
}); });
return true; return true;
@ -47,7 +53,7 @@ bool RHICommonList::Start() {
void RHICommonList::Stop() { void RHICommonList::Stop() {
isRunning_ = false; isRunning_ = false;
queueCondVar_.notify_one();
if (thread_ && thread_->joinable()) { if (thread_ && thread_->joinable()) {
thread_->join(); thread_->join();
thread_.reset(); thread_.reset();

View File

@ -0,0 +1,36 @@
#include "Renderer/MeshRender.h"
#include "RHI/OpenglDrv/GLHandleMapper.h"
MeshRender::MeshRender(Mesh* mesh)
: mesh_(mesh) {
RHI* rhi = RHI::Get();
rhiApi_ = rhi->GetRHIAPI();
}
MeshRender::~MeshRender() {
if (0 == vaoHandle_) {
return;
}
auto task = rhiApi_->DeleteVAOCommand(vaoHandle_, vboHandle_, eboHandle_);
RHI::Get()->Push(task);
}
void MeshRender::Render() {
if (nullptr == mesh_) {
return;
}
if (0 == vaoHandle_) {
vaoHandle_ = GLHandleMapper::GeneratorVAOHandle();
vboHandle_ = GLHandleMapper::GeneratorVBOHandle();
eboHandle_ = GLHandleMapper::GeneratorEBOHandle();
auto task = rhiApi_->CreateVAOCommand(mesh_, vaoHandle_, vboHandle_, eboHandle_);
RHI::Get()->Push(task);
} else {
auto task = rhiApi_->DrawElementsCommand(vaoHandle_, eboHandle_, mesh_->GetIndexCount());
RHI::Get()->Push(task);
}
}

20
src/Renderer/MeshRender.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "Core/Core.h"
#include "RHI/RHI.h"
class MeshRender {
public:
MeshRender(Mesh* mesh);
~MeshRender();
void Render();
private:
Mesh* mesh_{ nullptr };
IRHIAPI* rhiApi_{ nullptr };
unsigned int vaoHandle_{ 0 };
unsigned int vboHandle_{ 0 };
unsigned int eboHandle_{ 0 };
};

View File

@ -1,6 +1,4 @@
#define GLEW_STATIC #include "RHI/OpenglDrv/gl.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Window/GLFWImpl/GLFWImpl.h" #include "Window/GLFWImpl/GLFWImpl.h"
#include "Core/Logger.h" #include "Core/Logger.h"