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,10 +2,29 @@
#include <assert.h>
#include "Core/Logger.h"
#include "Core/Core.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}
//};
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* s_Application = nullptr;
Application::Application() {
@ -39,14 +58,28 @@
return -1;
}
RHI::Get()->StartRenderThread();
RHI::Get()->StartRender();
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;
}
void Application::Uninitialize() {
RHI::Shotdown();
}
void Application::UpdateScene() {
meshRender_->Render();
}

View File

@ -12,6 +12,7 @@ public:
virtual bool Initialize();
virtual int RunLoop();
virtual void Uninitialize();
virtual void UpdateScene();
float GeltaSeconds() const {
return m_deltaSeconds;
@ -21,4 +22,6 @@ protected:
double m_timeStamp{ 0.0 };
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 CPP_FILES ./*.cpp)
FILE(GLOB_RECURSE CC_FILES ./*.cc)
FILE(GLOB_RECURSE CC_FILES ./*.c)
SET(
ALL_FILES

View File

@ -13,7 +13,7 @@ Logger::~Logger() {
}
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);

View File

@ -34,6 +34,7 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI
#else
#if 0
#include <windows.h>
@ -111,7 +112,22 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
}
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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,10 +1,10 @@
#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 "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()
: RHICommond(RHICommond::Type::END_FRAME) {
//INFOLOG("actor {}", spdlog::fmt_lib::ptr(this));
}
GLEndCommand::~GLEndCommand() {
//INFOLOG("dctor {}", spdlog::fmt_lib::ptr(this));
}
void GLEndCommand::OnExecute() {
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 "Core/Mesh.h"
#include "Core/Core.h"
class GLVAOCreateCommand : public RHICommond {
@ -167,11 +167,23 @@ private:
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 {
public:
GLEndCommand();
virtual ~GLEndCommand() = default;
virtual ~GLEndCommand();
protected:
void OnExecute() override;

View File

@ -2,9 +2,7 @@
#include <cassert>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "RHI/OpenglDrv/gl.h"
#include "Core/StringUtil.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 "Core/Logger.h"
#include "Core/Core.h"
#include "Core/SpinLock.h"
#include "OpenGLDrv/GLDrvAPI.h"
@ -30,7 +30,6 @@ bool RHI::Initialize() {
void RHI::Uninitialize() {
if (rhiCommonList_) {
rhiCommonList_->Stop();
rhiCommonList_.reset();
}
@ -41,13 +40,22 @@ void RHI::Uninitialize() {
rhiAPI_->UninitEnv();
}
bool RHI::StartRenderThread() {
bool RHI::StartRender() {
if (nullptr == rhiCommonList_ || !rhiCommonList_->Start()) {
ERRORLOG("StartRenderThread start failed");
return false;
}
return true;
}
bool RHI::StopRender() {
if (nullptr == rhiCommonList_) {
ERRORLOG("StartRenderThread stop failed");
return false;
}
rhiCommonList_->Stop();
return true;
}
bool RHI::InitRenderEnv() {
@ -65,13 +73,19 @@ void RHI::UpdaeRenderSize() {
}
void RHI::UpdateScreenSize() {
auto task = UpdateRenderSizeCommand::Create();
SharedPtr<RHICommond> task = UpdateRenderSizeCommand::Create();
rhiCommonList_->Push(task);
task->Wait();
}
void RHI::BeginFrame() {
SharedPtr<RHICommond> task = rhiAPI_->CreateStartCommand();
rhiCommonList_->Push(task);
}
void RHI::EndFrame() {
auto task = rhiAPI_->CreateEndCommand();
SharedPtr<RHICommond> task = rhiAPI_->CreateEndCommand();
rhiCommonList_->Push(task);
task->Wait();
}
@ -80,18 +94,6 @@ void RHI::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) {
rhiCommonList_->Push(commond);
}

View File

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

View File

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

View File

@ -44,6 +44,7 @@ public:
SET_STENCIL_FUNC,
SET_STENCIL_OP,
SET_STENCIL_BUFFER_CLEAR_VALUE,
START_FRAME,
END_FRAME,
START_RENDER,
END_RENDER,
@ -76,19 +77,6 @@ private:
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 {
public:
virtual ~UpdateRenderSizeCommand() = default;

View File

@ -31,15 +31,21 @@ bool RHICommonList::Start() {
while (isRunning_) {
{
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();
queue_.pop();
if (!isRunning_) {
break;
}
commond->Execute();
}
}
driverSetting.renderSwapBuffers();
}
});
return true;
@ -47,7 +53,7 @@ bool RHICommonList::Start() {
void RHICommonList::Stop() {
isRunning_ = false;
queueCondVar_.notify_one();
if (thread_ && thread_->joinable()) {
thread_->join();
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 <GL/glew.h>
#include <GLFW/glfw3.h>
#include "RHI/OpenglDrv/gl.h"
#include "Window/GLFWImpl/GLFWImpl.h"
#include "Core/Logger.h"