refactor(editor): 重构 Editor 使用 Engine 的 Component/Scene 系统
- Editor CMakeLists.txt 链接 XCEngine 库 - 删除 editor/src/Core/GameObject.h (简化版) - SelectionManager 使用 Engine::Components::GameObject* - SceneManager 使用 Engine::Scene - HierarchyPanel 使用 Engine GameObject API - InspectorPanel 使用 Engine TransformComponent 注意: Engine RHI Shader 接口有编译错误需要修复
This commit is contained in:
@@ -25,35 +25,28 @@ public:
|
||||
bool Compile(const char* source, ShaderType type);
|
||||
|
||||
void Use() const;
|
||||
void Bind() override { Use(); }
|
||||
void Unbind() override;
|
||||
|
||||
void SetInt(const char* name, int value) override;
|
||||
void SetIntArray(const char* name, const int* values, unsigned int count);
|
||||
void SetFloat(const char* name, float value) override;
|
||||
void SetFloatArray(const char* name, const float* values, unsigned int count);
|
||||
void SetVec3(const char* name, float x, float y, float z) override;
|
||||
void SetVec3(const char* name, const float* values);
|
||||
void SetVec4(const char* name, float x, float y, float z, float w) override;
|
||||
void SetVec4(const char* name, const float* values);
|
||||
void SetMat2(const char* name, const float* value);
|
||||
void SetMat3(const char* name, const float* value);
|
||||
void SetMat4(const char* name, const float* value) override;
|
||||
void SetMat4Array(const char* name, const float* values, unsigned int count);
|
||||
|
||||
int GetUniformLocation(const char* name) const;
|
||||
unsigned int GetID() const { return m_program; }
|
||||
void* GetNativeHandle() override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_program)); }
|
||||
bool IsValid() const override { return m_program != 0; }
|
||||
|
||||
ShaderType GetType() const override { return m_type; }
|
||||
|
||||
const std::vector<UniformInfo>& GetUniformInfos() const override;
|
||||
const UniformInfo* GetUniformInfo(const char* name) const override;
|
||||
|
||||
int GetUniformLocation(const char* name) const;
|
||||
unsigned int GetID() const { return m_program; }
|
||||
|
||||
private:
|
||||
void CacheUniformInfos() const;
|
||||
|
||||
unsigned int m_program;
|
||||
ShaderType m_type = ShaderType::Vertex;
|
||||
mutable std::vector<UniformInfo> m_uniformInfos;
|
||||
mutable bool m_uniformsCached = false;
|
||||
bool CheckCompileErrors(unsigned int shader, const char* type);
|
||||
bool CheckLinkErrors(unsigned int program);
|
||||
};
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "RHITypes.h"
|
||||
#include "RHIEnums.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
@@ -17,19 +18,21 @@ public:
|
||||
virtual ShaderType GetType() const = 0;
|
||||
virtual bool IsValid() const = 0;
|
||||
|
||||
virtual void Bind() = 0;
|
||||
virtual void Unbind() = 0;
|
||||
|
||||
virtual void* GetNativeHandle() = 0;
|
||||
|
||||
virtual void SetInt(const char* name, int value) = 0;
|
||||
virtual void SetFloat(const char* name, float value) = 0;
|
||||
virtual void SetVec3(const char* name, float x, float y, float z) = 0;
|
||||
virtual void SetVec4(const char* name, float x, float y, float z, float w) = 0;
|
||||
virtual void SetMat4(const char* name, const float* value) = 0;
|
||||
|
||||
virtual void Shutdown() = 0;
|
||||
|
||||
struct UniformInfo {
|
||||
std::string name;
|
||||
uint32_t bindPoint;
|
||||
uint32_t size;
|
||||
uint32_t type;
|
||||
uint32_t arraySize;
|
||||
};
|
||||
|
||||
virtual const std::vector<UniformInfo>& GetUniformInfos() const = 0;
|
||||
virtual const UniformInfo* GetUniformInfo(const char* name) const = 0;
|
||||
};
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
@@ -8,7 +8,7 @@ namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
OpenGLShader::OpenGLShader()
|
||||
: m_program(0) {
|
||||
: m_program(0), m_uniformsCached(false) {
|
||||
}
|
||||
|
||||
OpenGLShader::~OpenGLShader() {
|
||||
@@ -87,6 +87,8 @@ bool OpenGLShader::Compile(const char* vertexSource, const char* fragmentSource)
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
m_uniformsCached = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -128,6 +130,8 @@ bool OpenGLShader::Compile(const char* vertexSource, const char* fragmentSource,
|
||||
glDeleteShader(fragment);
|
||||
glDeleteShader(geometry);
|
||||
|
||||
m_uniformsCached = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,6 +153,8 @@ bool OpenGLShader::CompileCompute(const char* computeSource) {
|
||||
|
||||
glDeleteShader(compute);
|
||||
|
||||
m_uniformsCached = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -197,6 +203,9 @@ bool OpenGLShader::Compile(const char* source, ShaderType type) {
|
||||
}
|
||||
|
||||
glDeleteShader(shader);
|
||||
|
||||
m_uniformsCached = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -218,62 +227,73 @@ void OpenGLShader::Shutdown() {
|
||||
glDeleteProgram(m_program);
|
||||
m_program = 0;
|
||||
}
|
||||
m_uniformInfos.clear();
|
||||
m_uniformsCached = false;
|
||||
}
|
||||
|
||||
void OpenGLShader::Use() const {
|
||||
glUseProgram(m_program);
|
||||
}
|
||||
|
||||
void OpenGLShader::Unbind() {
|
||||
glUseProgram(0);
|
||||
void OpenGLShader::CacheUniformInfos() const {
|
||||
if (m_uniformsCached || m_program == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_uniformInfos.clear();
|
||||
|
||||
GLint numUniforms = 0;
|
||||
glGetProgramInterfaceiv(m_program, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
|
||||
|
||||
for (GLint i = 0; i < numUniforms; ++i) {
|
||||
GLenum props[] = { GL_NAME_LENGTH, GL_TYPE, GL_OFFSET, GL_ARRAY_SIZE };
|
||||
GLint values[4] = { 0 };
|
||||
glGetProgramResourceiv(m_program, GL_UNIFORM, i, 4, props, 4, nullptr, values);
|
||||
|
||||
std::vector<char> nameBuffer(values[0]);
|
||||
glGetProgramResourceName(m_program, GL_UNIFORM, i, values[0], nullptr, nameBuffer.data());
|
||||
|
||||
UniformInfo info;
|
||||
info.name = nameBuffer.data();
|
||||
info.bindPoint = static_cast<uint32_t>(i);
|
||||
info.type = static_cast<uint32_t>(values[1]);
|
||||
info.offset = static_cast<uint32_t>(values[2]);
|
||||
info.arraySize = static_cast<uint32_t>(values[3]);
|
||||
|
||||
GLint size = 0;
|
||||
glGetActiveUniformsiv(m_program, 1, &i, GL_SIZE, &size);
|
||||
switch (values[1]) {
|
||||
case GL_FLOAT: info.size = sizeof(GLfloat) * size; break;
|
||||
case GL_FLOAT_VEC2: info.size = sizeof(GLfloat) * 2 * size; break;
|
||||
case GL_FLOAT_VEC3: info.size = sizeof(GLfloat) * 3 * size; break;
|
||||
case GL_FLOAT_VEC4: info.size = sizeof(GLfloat) * 4 * size; break;
|
||||
case GL_INT: info.size = sizeof(GLint) * size; break;
|
||||
case GL_BOOL: info.size = sizeof(GLboolean) * size; break;
|
||||
case GL_FLOAT_MAT4: info.size = sizeof(GLfloat) * 16 * size; break;
|
||||
case GL_FLOAT_MAT3: info.size = sizeof(GLfloat) * 9 * size; break;
|
||||
case GL_FLOAT_MAT2: info.size = sizeof(GLfloat) * 4 * size; break;
|
||||
default: info.size = 0; break;
|
||||
}
|
||||
|
||||
m_uniformInfos.push_back(info);
|
||||
}
|
||||
|
||||
m_uniformsCached = true;
|
||||
}
|
||||
|
||||
void OpenGLShader::SetInt(const char* name, int value) {
|
||||
glUniform1i(glGetUniformLocation(m_program, name), value);
|
||||
const std::vector<RHIShader::UniformInfo>& OpenGLShader::GetUniformInfos() const {
|
||||
CacheUniformInfos();
|
||||
return m_uniformInfos;
|
||||
}
|
||||
|
||||
void OpenGLShader::SetIntArray(const char* name, const int* values, unsigned int count) {
|
||||
glUniform1iv(glGetUniformLocation(m_program, name), count, values);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetFloat(const char* name, float value) {
|
||||
glUniform1f(glGetUniformLocation(m_program, name), value);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetFloatArray(const char* name, const float* values, unsigned int count) {
|
||||
glUniform1fv(glGetUniformLocation(m_program, name), count, values);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetVec3(const char* name, float x, float y, float z) {
|
||||
glUniform3f(glGetUniformLocation(m_program, name), x, y, z);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetVec3(const char* name, const float* values) {
|
||||
glUniform3fv(glGetUniformLocation(m_program, name), 1, values);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetVec4(const char* name, float x, float y, float z, float w) {
|
||||
glUniform4f(glGetUniformLocation(m_program, name), x, y, z, w);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetVec4(const char* name, const float* values) {
|
||||
glUniform4fv(glGetUniformLocation(m_program, name), 1, values);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetMat2(const char* name, const float* value) {
|
||||
glUniformMatrix2fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetMat3(const char* name, const float* value) {
|
||||
glUniformMatrix3fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetMat4(const char* name, const float* value) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||
}
|
||||
|
||||
void OpenGLShader::SetMat4Array(const char* name, const float* values, unsigned int count) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(m_program, name), count, GL_FALSE, values);
|
||||
const RHIShader::UniformInfo* OpenGLShader::GetUniformInfo(const char* name) const {
|
||||
CacheUniformInfos();
|
||||
for (const auto& info : m_uniformInfos) {
|
||||
if (info.name == name) {
|
||||
return &info;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int OpenGLShader::GetUniformLocation(const char* name) const {
|
||||
@@ -307,4 +327,4 @@ bool OpenGLShader::CheckLinkErrors(unsigned int program) {
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user