diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index f1311dc..d557a8a 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -2,51 +2,84 @@ #include -#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} +//}; - 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() { - assert(nullptr == s_Application); - s_Application = this; +Application* s_Application = nullptr; - 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() { - Logger::Shotdown(); - assert(nullptr != s_Application); - s_Application = nullptr; + return true; +} + +int Application::RunLoop() { + const DriverSettings& setting = RHI::Get()->GetDriverSettings(); + if (nullptr == setting.isWindowShouldClose) { + return -1; } - Application* Application::Get() { - return s_Application; + 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; +} - bool Application::Initialize() { - if (!RHI::Init()) { - return false; - } +void Application::Uninitialize() { + RHI::Shotdown(); +} - return true; - } - - 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(); - } \ No newline at end of file +void Application::UpdateScene() { + meshRender_->Render(); +} diff --git a/src/Application/Application.h b/src/Application/Application.h index 38d67a7..9043061 100644 --- a/src/Application/Application.h +++ b/src/Application/Application.h @@ -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 }; }; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d31784a..df77a82 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/Core/Logger.cpp b/src/Core/Logger.cpp index 0f6c321..0d96932 100644 --- a/src/Core/Logger.cpp +++ b/src/Core/Logger.cpp @@ -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); diff --git a/src/Platforms/Windows/Main.cpp b/src/Platforms/Windows/Main.cpp index e5dc619..61b8d75 100644 --- a/src/Platforms/Windows/Main.cpp +++ b/src/Platforms/Windows/Main.cpp @@ -34,6 +34,7 @@ static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevI #else +#if 0 #include @@ -111,7 +112,22 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) { } return 0; } +#else +//#include +#include + +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 \ No newline at end of file diff --git a/src/RHI/DriverSettings.h b/src/RHI/DriverSettings.h index cf10115..00a1101 100644 --- a/src/RHI/DriverSettings.h +++ b/src/RHI/DriverSettings.h @@ -7,6 +7,7 @@ using DriverSettingsBoolNoParamFunction = std::function; using DriverSettingsVoidNoParamFunction = std::function; +using DriverSettingsGetBufferSizeFunction = std::function; 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 }; }; diff --git a/src/RHI/IRHIAPI.h b/src/RHI/IRHIAPI.h index 4d73dff..0bbd3c2 100644 --- a/src/RHI/IRHIAPI.h +++ b/src/RHI/IRHIAPI.h @@ -56,5 +56,6 @@ public: virtual RHICommond* DeleteProgramCommand( unsigned int programHandle ) = 0; + virtual RHICommond* CreateStartCommand() = 0; virtual RHICommond* CreateEndCommand() = 0; }; diff --git a/src/RHI/OpenglDrv/GLDrvAPI.cpp b/src/RHI/OpenglDrv/GLDrvAPI.cpp index 18318c0..c184d7d 100644 --- a/src/RHI/OpenglDrv/GLDrvAPI.cpp +++ b/src/RHI/OpenglDrv/GLDrvAPI.cpp @@ -1,8 +1,6 @@ #include "RHI/OpenglDrv/GLDrvAPI.h" -#define GLEW_STATIC -#include -#include +#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(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(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; diff --git a/src/RHI/OpenglDrv/GLDrvAPI.h b/src/RHI/OpenglDrv/GLDrvAPI.h index f04b184..088dcef 100644 --- a/src/RHI/OpenglDrv/GLDrvAPI.h +++ b/src/RHI/OpenglDrv/GLDrvAPI.h @@ -55,5 +55,6 @@ public: RHICommond* DeleteProgramCommand( uint32 programHandle ) override; + RHICommond* CreateStartCommand() override; RHICommond* CreateEndCommand() override; }; \ No newline at end of file diff --git a/src/RHI/OpenglDrv/GLRHICommond.cpp b/src/RHI/OpenglDrv/GLRHICommond.cpp index c2b3a98..470f9ff 100644 --- a/src/RHI/OpenglDrv/GLRHICommond.cpp +++ b/src/RHI/OpenglDrv/GLRHICommond.cpp @@ -1,10 +1,10 @@ #include "RHI/OpenglDrv/GLRHICommond.h" -#define GLEW_STATIC -#include -#include -#include "Core/Logger.h" +#include +#include + +#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(setting.windowHandle)); + if (nullptr == setting.renderSwapBuffers) { + return; + } + setting.renderSwapBuffers(); } diff --git a/src/RHI/OpenglDrv/GLRHICommond.h b/src/RHI/OpenglDrv/GLRHICommond.h index 89a8075..c53deaf 100644 --- a/src/RHI/OpenglDrv/GLRHICommond.h +++ b/src/RHI/OpenglDrv/GLRHICommond.h @@ -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; diff --git a/src/RHI/OpenglDrv/GLShader.cpp b/src/RHI/OpenglDrv/GLShader.cpp index 965f89a..a0394da 100644 --- a/src/RHI/OpenglDrv/GLShader.cpp +++ b/src/RHI/OpenglDrv/GLShader.cpp @@ -2,9 +2,7 @@ #include -#define GLEW_STATIC -#include -#include +#include "RHI/OpenglDrv/gl.h" #include "Core/StringUtil.h" #include "RHI/OpenglDrv/GLHandleMapper.h" diff --git a/src/RHI/OpenglDrv/gl.h b/src/RHI/OpenglDrv/gl.h new file mode 100644 index 0000000..f71cecd --- /dev/null +++ b/src/RHI/OpenglDrv/gl.h @@ -0,0 +1,7 @@ +#pragma once + +//#include + +#define GLEW_STATIC +#include +#include diff --git a/src/RHI/RHI.cpp b/src/RHI/RHI.cpp index d7e5d82..6f929eb 100644 --- a/src/RHI/RHI.cpp +++ b/src/RHI/RHI.cpp @@ -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 task = UpdateRenderSizeCommand::Create(); rhiCommonList_->Push(task); task->Wait(); } + +void RHI::BeginFrame() { + SharedPtr task = rhiAPI_->CreateStartCommand(); + rhiCommonList_->Push(task); +} + void RHI::EndFrame() { - auto task = rhiAPI_->CreateEndCommand(); + SharedPtr 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); } diff --git a/src/RHI/RHI.h b/src/RHI/RHI.h index 30dfe14..bb4eb24 100644 --- a/src/RHI/RHI.h +++ b/src/RHI/RHI.h @@ -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); diff --git a/src/RHI/RHICommond.cpp b/src/RHI/RHICommond.cpp index 35191a1..d7fd776 100644 --- a/src/RHI/RHICommond.cpp +++ b/src/RHI/RHICommond.cpp @@ -2,9 +2,6 @@ #include "RHI/RHI.h" -void StartRenderCommand::OnExecute() { - RHI::Get()->InitRenderEnv(); -} void UpdateRenderSizeCommand::OnExecute() { RHI::Get()->UpdaeRenderSize(); diff --git a/src/RHI/RHICommond.h b/src/RHI/RHICommond.h index 836d805..62b7c11 100644 --- a/src/RHI/RHICommond.h +++ b/src/RHI/RHICommond.h @@ -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; diff --git a/src/RHI/RHICommondList.cpp b/src/RHI/RHICommondList.cpp index 167dcc3..b3f347f 100644 --- a/src/RHI/RHICommondList.cpp +++ b/src/RHI/RHICommondList.cpp @@ -31,15 +31,21 @@ bool RHICommonList::Start() { while (isRunning_) { { std::unique_lock 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 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(); diff --git a/src/Renderer/MeshRender.cpp b/src/Renderer/MeshRender.cpp new file mode 100644 index 0000000..f5e9a3e --- /dev/null +++ b/src/Renderer/MeshRender.cpp @@ -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); + } + + +} \ No newline at end of file diff --git a/src/Renderer/MeshRender.h b/src/Renderer/MeshRender.h new file mode 100644 index 0000000..a190cae --- /dev/null +++ b/src/Renderer/MeshRender.h @@ -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 }; +}; diff --git a/src/Window/GLFWImpl/GLFWImpl.cpp b/src/Window/GLFWImpl/GLFWImpl.cpp index a23b838..112d249 100644 --- a/src/Window/GLFWImpl/GLFWImpl.cpp +++ b/src/Window/GLFWImpl/GLFWImpl.cpp @@ -1,6 +1,4 @@ -#define GLEW_STATIC -#include -#include +#include "RHI/OpenglDrv/gl.h" #include "Window/GLFWImpl/GLFWImpl.h" #include "Core/Logger.h"