From 88b882ed0b432c6aff2063213e2f793a36dd25f7 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 6 Jun 2019 00:11:18 +0800 Subject: *misc --- .../asura-core/graphics/binding/_gfx_device.cpp | 152 ++++++++++++++ source/modules/asura-core/graphics/binding/_gl.cpp | 128 ------------ .../asura-core/graphics/binding/_shader.cpp | 104 ++-------- source/modules/asura-core/graphics/canvas.h | 2 +- source/modules/asura-core/graphics/gfx_device.cpp | 197 ++++++++++++++++++ source/modules/asura-core/graphics/gfx_device.h | 160 +++++++++++++++ source/modules/asura-core/graphics/gl.cpp | 219 --------------------- source/modules/asura-core/graphics/gl.h | 165 ---------------- source/modules/asura-core/graphics/gpu_buffer.cpp | 12 +- source/modules/asura-core/graphics/gpu_buffer.h | 2 +- source/modules/asura-core/graphics/image.cpp | 2 +- source/modules/asura-core/graphics/image.h | 4 + source/modules/asura-core/graphics/index_buffer.h | 3 + source/modules/asura-core/graphics/shader.cpp | 55 ++---- source/modules/asura-core/graphics/shader.h | 37 ++-- source/modules/asura-core/graphics/texture.cpp | 4 +- source/modules/asura-core/graphics/texture.h | 22 +-- source/modules/asura-core/graphics/vertex_buffer.h | 4 + source/modules/asura-core/input/input_device.hpp | 2 +- source/modules/asura-core/mesh/mesh2d_data.h | 2 +- 20 files changed, 586 insertions(+), 690 deletions(-) create mode 100644 source/modules/asura-core/graphics/binding/_gfx_device.cpp delete mode 100644 source/modules/asura-core/graphics/binding/_gl.cpp create mode 100644 source/modules/asura-core/graphics/gfx_device.cpp create mode 100644 source/modules/asura-core/graphics/gfx_device.h delete mode 100644 source/modules/asura-core/graphics/gl.cpp delete mode 100644 source/modules/asura-core/graphics/gl.h (limited to 'source/modules/asura-core') diff --git a/source/modules/asura-core/graphics/binding/_gfx_device.cpp b/source/modules/asura-core/graphics/binding/_gfx_device.cpp new file mode 100644 index 0000000..5ae475c --- /dev/null +++ b/source/modules/asura-core/graphics/binding/_gfx_device.cpp @@ -0,0 +1,152 @@ +#include "../gfx_device.h" + +using namespace std; +using namespace Luax; + +namespace AsuraEngine +{ + namespace Graphics + { + + LUAX_REGISTRY(GfxDevice) + { + LUAX_REGISTER_METHODS(state, + { "SetMatrixMode", _SetMatrixMode }, + { "GetMatrixMode", _GetMatrixMode }, + { "PushMatrix", _PushMatrix }, + { "PopMatrix", _PopMatrix }, + { "LoadIdentity", _LoadIdentity }, + { "Rotate", _Rotate }, + { "Translate", _Translate }, + { "Scale", _Scale }, + { "Ortho", _Ortho }, + { "GetMatrixDepth", _GetMatrixDepth }, + { "GetMatrixIndex", _GetMatrixIndex }, + { "UseShader", _UseShader }, + { "UnuseShader", _UnuseShader } + ); + } + + LUAX_POSTPROCESS(GfxDevice) + { + LUAX_REGISTER_ENUM(state, "EMatrixMode", + { "PROJECTION", MATRIX_MODE_PROJECTION }, + { "0", 0 }, + { "MODEL", MATRIX_MODE_MODEL }, + { "1", 1 }, + { "VIEW", MATRIX_MODE_VIEW }, + { "2", 2 } + ); + LUAX_REGISTER_ENUM(state, "EGLParams", + { "MAX_TEXTURE_UNIT", GL_PARAM_MAX_TEXTURE_UNIT }, + { "1", 1 } + ); + + } + + // gfxdevice:SetMatrixMode() + LUAX_IMPL_METHOD(GfxDevice, _SetMatrixMode) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:GetMatrixMode() + LUAX_IMPL_METHOD(GfxDevice, _GetMatrixMode) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:PushMatrix() + LUAX_IMPL_METHOD(GfxDevice, _PushMatrix) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:PopMatrix() + LUAX_IMPL_METHOD(GfxDevice, _PopMatrix) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:LoadIdentity() + LUAX_IMPL_METHOD(GfxDevice, _LoadIdentity) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:Rotate() + LUAX_IMPL_METHOD(GfxDevice, _Rotate) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:Translate() + LUAX_IMPL_METHOD(GfxDevice, _Translate) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:Scale() + LUAX_IMPL_METHOD(GfxDevice, _Scale) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:Ortho() + LUAX_IMPL_METHOD(GfxDevice, _Ortho) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:GetMatrixDepth() + LUAX_IMPL_METHOD(GfxDevice, _GetMatrixDepth) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:GetMatrixIndex() + LUAX_IMPL_METHOD(GfxDevice, _GetMatrixIndex) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:UseShader() + LUAX_IMPL_METHOD(GfxDevice, _UseShader) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + // gfxdevice:UnuseShader() + LUAX_IMPL_METHOD(GfxDevice, _UnuseShader) + { + LUAX_PREPARE(L, GfxDevice); + + return 0; + } + + } +} diff --git a/source/modules/asura-core/graphics/binding/_gl.cpp b/source/modules/asura-core/graphics/binding/_gl.cpp deleted file mode 100644 index 0c3a18f..0000000 --- a/source/modules/asura-core/graphics/binding/_gl.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include "../gl.h" - -using namespace std; -using namespace Luax; - -namespace AsuraEngine -{ - namespace Graphics - { - - LUAX_REGISTRY(OpenGL) - { - LUAX_REGISTER_METHODS(state, - { "SetMatrixMode", _SetMatrixMode }, - { "GetMatrixMode", _GetMatrixMode }, - { "PushMatrix", _PushMatrix }, - { "PopMatrix", _PopMatrix }, - { "LoadIdentity", _LoadIdentity }, - { "Rotate", _Rotate }, - { "Translate", _Translate }, - { "Scale", _Scale }, - { "Ortho", _Ortho }, - { "GetMatrixDepth", _GetMatrixDepth }, - { "GetMatrixIndex", _GetMatrixIndex } - ); - } - - LUAX_POSTPROCESS(OpenGL) - { - LUAX_REGISTER_ENUM(state, "EMatrixMode", - { "PROJECTION", MATRIX_MODE_PROJECTION }, - { "MODEL", MATRIX_MODE_MODEL }, - { "VIEW", MATRIX_MODE_VIEW } - ); - - } - - // GL.SetMatrixMode() - LUAX_IMPL_METHOD(OpenGL, _SetMatrixMode) - { - LUAX_PREPARE(L, OpenGL); - MatrixMode mode = (MatrixMode)state.CheckValue(1); - gl.SetMatrixMode(mode); - return 0; - } - - // GL.GetMatrixMode() - LUAX_IMPL_METHOD(OpenGL, _GetMatrixMode) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.PushMatrix() - LUAX_IMPL_METHOD(OpenGL, _PushMatrix) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.PopMatrix() - LUAX_IMPL_METHOD(OpenGL, _PopMatrix) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.LoadIdentity() - LUAX_IMPL_METHOD(OpenGL, _LoadIdentity) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.Rotate() - LUAX_IMPL_METHOD(OpenGL, _Rotate) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.Translate() - LUAX_IMPL_METHOD(OpenGL, _Translate) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.Scale() - LUAX_IMPL_METHOD(OpenGL, _Scale) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.Ortho() - LUAX_IMPL_METHOD(OpenGL, _Ortho) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.GetMatrixDepth() - LUAX_IMPL_METHOD(OpenGL, _GetMatrixDepth) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - // GL.GetMatrixIndex() - LUAX_IMPL_METHOD(OpenGL, _GetMatrixIndex) - { - LUAX_PREPARE(L, OpenGL); - - return 0; - } - - } -} diff --git a/source/modules/asura-core/graphics/binding/_shader.cpp b/source/modules/asura-core/graphics/binding/_shader.cpp index 34bc98b..71f4e28 100644 --- a/source/modules/asura-core/graphics/binding/_shader.cpp +++ b/source/modules/asura-core/graphics/binding/_shader.cpp @@ -11,25 +11,19 @@ namespace AsuraEngine LUAX_REGISTRY(Shader) { LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "Load", _Load }, - { "Update", _Update }, - { "HasUniform", _HasUniform }, - { "GetUniformLocation", _GetUniformLocation }, - { "SetUniformFloat", _SetUniformFloat }, - { "SetUniformTexture", _SetUniformTexture }, - { "SetUniformVector2", _SetUniformVector2 }, - { "SetUniformVector3", _SetUniformVector3 }, - { "SetUniformVector4", _SetUniformVector4 }, - { "SetUniformColor", _SetUniformColor }, - { "GetAttributeLocation", _GetAttributeLocation }, - { "SetAttribute", _SetAttribute }, - { "DisableAttribute", _DisableAttribute }, - { "SetBuiltInModelMatrix", _SetBuiltInModelMatrix }, - { "SetBuiltInViewMatrix", _SetBuiltInViewMatrix }, - { "SetBuiltInProjectionMatrix", _SetBuiltInProjectionMatrix }, - { "SetBuiltInMVPMatrix", _SetBuiltInMVPMatrix }, - { "SetBuiltInDrawColor", _SetBuiltInDrawColor } + { "New", _New }, + { "Load", _Load }, + { "Update", _Update }, + { "HasUniform", _HasUniform }, + { "GetUniformLocation", _GetUniformLocation }, + { "SetBuiltInUniforms", _SetBuiltInUniforms }, + { "SetUniformFloat", _SetUniformFloat }, + { "SetUniformTexture", _SetUniformTexture }, + { "SetUniformVector2", _SetUniformVector2 }, + { "SetUniformVector3", _SetUniformVector3 }, + { "SetUniformVector4", _SetUniformVector4 }, + { "SetUniformColor", _SetUniformColor }, + { "SetBuiltInUniforms", _SetBuiltInUniforms } ); } @@ -78,6 +72,14 @@ namespace AsuraEngine return 0; } + // shader:SetBuiltInUniforms() + LUAX_IMPL_METHOD(Shader, _SetBuiltInUniforms) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + // shader:SetUniformFloat() LUAX_IMPL_METHOD(Shader, _SetUniformFloat) { @@ -126,69 +128,5 @@ namespace AsuraEngine return 0; } - // shader:GetAttributeLocation() - LUAX_IMPL_METHOD(Shader, _GetAttributeLocation) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetAttribute() - LUAX_IMPL_METHOD(Shader, _SetAttribute) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:DisableAttribute() - LUAX_IMPL_METHOD(Shader, _DisableAttribute) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetBuiltInModelMatrix() - LUAX_IMPL_METHOD(Shader, _SetBuiltInModelMatrix) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetBuiltInViewMatrix() - LUAX_IMPL_METHOD(Shader, _SetBuiltInViewMatrix) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetBuiltInProjectionMatrix() - LUAX_IMPL_METHOD(Shader, _SetBuiltInProjectionMatrix) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetBuiltInMVPMatrix() - LUAX_IMPL_METHOD(Shader, _SetBuiltInMVPMatrix) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - - // shader:SetBuiltInColor() - LUAX_IMPL_METHOD(Shader, _SetBuiltInDrawColor) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - } } diff --git a/source/modules/asura-core/graphics/canvas.h b/source/modules/asura-core/graphics/canvas.h index 555ce5d..49096c7 100644 --- a/source/modules/asura-core/graphics/canvas.h +++ b/source/modules/asura-core/graphics/canvas.h @@ -6,7 +6,7 @@ #include #include -#include "gl.h" +#include "gfx_device.h" #include "texture.h" #include "render_target.h" #include "render_state.h" diff --git a/source/modules/asura-core/graphics/gfx_device.cpp b/source/modules/asura-core/graphics/gfx_device.cpp new file mode 100644 index 0000000..edfa784 --- /dev/null +++ b/source/modules/asura-core/graphics/gfx_device.cpp @@ -0,0 +1,197 @@ +#include + +#include "../core_config.h" + +#include "gfx_device.h" +#include "shader.h" +#include "matrix_stack.h" +#include "color.h" + +using namespace AEMath; + +namespace AsuraEngine +{ + namespace Graphics + { + +#if ASURA_DEBUG + static bool _instantiated = false; +#endif + + GfxDevice gfx; + + GfxDevice::GfxDevice() + { +#if ASURA_DEBUG + ASSERT(!_instantiated); + _instantiated = true; +#endif + } + + GfxDevice::~GfxDevice() + { + } + + static bool inited = false; + + bool GfxDevice::Init(const AEMath::Recti& view) + { + bool loaded = false; +#if ASURA_OPENGL_LOADER & ASURA_OPENGL_GLAD + if (!loaded) + loaded = gladLoadGL(); +#endif + if (!loaded) + return false; + SetViewport(view); + + inited = true; + return true; + } + + bool GfxDevice::Inited() + { + return inited; + } + + void GfxDevice::WipeError() + { + while (glGetError() != GL_NO_ERROR); + } + + bool GfxDevice::HasError() + { + return glGetError() != GL_NO_ERROR; + } + + GLenum GfxDevice::GetError() + { + return glGetError(); + } + + void GfxDevice::SetDrawColor(float r, float g, float b, float a) + { + state.drawColor.Set(r, g, b, a); + } + + Color& GfxDevice::GetDrawColor() + { + return state.drawColor; + } + + void GfxDevice::SetViewport(const Recti v) + { + state.viewport = v; + glViewport(v.x, v.y, v.w, v.h); + } + + const Recti& GfxDevice::GetViewport() + { + return state.viewport; + } + + void GfxDevice::UseShader(Shader* shader) + { + if (state.shader != shader) + { + glUseProgram(shader->GetGLProgram()); + state.shader = shader; +#if ASURA_GL_PROFILE + ++stats.shaderSwitch; +#endif + } + shader->OnUse(); + } + + void GfxDevice::UnuseShader() + { + state.shader->OnUnuse(); + state.shader = nullptr; + } + + Shader* GfxDevice::GetShader() + { + return state.shader; + } + + void GfxDevice::DrawArrays(GLenum mode, GLint first, GLsizei count) + { + glDrawArrays(mode, first, count); +#if ASURA_GL_PROFILE + ++stats.drawCall; +#endif + } + + //------------------------------------------------------------------------------// + + void GfxDevice::SetMatrixMode(MatrixMode mode) + { + state.matrixMode = mode; + } + + MatrixMode GfxDevice::GetMatrixMode() + { + return state.matrixMode; + } + + void GfxDevice::PushMatrix() + { + state.matrix[state.matrixMode].Push(); + } + + void GfxDevice::PopMatrix() + { + state.matrix[state.matrixMode].Pop(); + } + + void GfxDevice::LoadIdentity() + { + state.matrix[state.matrixMode].LoadIdentity(); + } + + void GfxDevice::Rotate(float angle) + { + state.matrix[state.matrixMode].Rotate(angle); + } + + void GfxDevice::Translate(float x, float y) + { + state.matrix[state.matrixMode].Translate(x, y); + } + + void GfxDevice::Scale(float x, float y) + { + state.matrix[state.matrixMode].Scale(x, y); + } + + void GfxDevice::Ortho(float l, float r, float b, float t, float n, float f) + { + state.matrix[state.matrixMode].Ortho(l, r, b, t, n, f); + } + + AEMath::Matrix44& GfxDevice::GetMatrix(MatrixMode mode) + { + return state.matrix[mode].GetTop(); + } + + AEMath::Matrix44 GfxDevice::GetMVPMatrix() + { + return state.matrix[MATRIX_MODE_MODEL].GetTop() + * state.matrix[MATRIX_MODE_MODEL].GetTop() + * state.matrix[MATRIX_MODE_MODEL].GetTop(); + } + + uint GfxDevice::GetMatrixDepth() + { + return state.matrix[state.matrixMode].GetCapacity(); + } + + uint GfxDevice::GetMatrixIndex() + { + return state.matrix[state.matrixMode].GetTopIndex(); + } + + //------------------------------------------------------------------------------// + + } +} \ No newline at end of file diff --git a/source/modules/asura-core/graphics/gfx_device.h b/source/modules/asura-core/graphics/gfx_device.h new file mode 100644 index 0000000..5bb8c6a --- /dev/null +++ b/source/modules/asura-core/graphics/gfx_device.h @@ -0,0 +1,160 @@ +#ifndef __ASURA_ENGINE_GFX_DEVICE_H__ +#define __ASURA_ENGINE_GFX_DEVICE_H__ + +#include + +#include + +#include +#include +#include +#include + +#include "color.h" +#include "matrix_stack.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + class Profiler; + class Shader; + + enum MatrixMode + { + MATRIX_MODE_PROJECTION = 0, + MATRIX_MODE_MODEL = 1, + MATRIX_MODE_VIEW = 2, + }; + + enum GLParams + { + GL_PARAM_MAX_TEXTURE_UNIT = 1, + }; + + /// + /// OpenGL上下文,用来做一些opengl状态的追踪。在编辑器多窗口环境下,一个窗口对应一个hwnd, + /// 一个hdc,以及数个opengl context,如果使用wglMakeCurrent(hdc, glc)指定当前线程耳朵 + /// 渲染窗口hdc和opengl上下文glc,gl中记录的就是任意一个线程的任意一个窗口的任意一个OpenGL + /// 上下文的状态,不支持多上下文渲染。 + /// + class GfxDevice : public AEScripting::Portable + { + public: + + GfxDevice(); + ~GfxDevice(); + + /// + /// 获得常量值 + /// + int GetParam(GLParams param); + + /// + /// 初始化OpenGL上下文参数,发生在创建OpenGL上下文之后。此函数会注册OpenGL函数的地址。 + /// + bool Init(const AEMath::Recti& viewport); + bool Inited(); + + void SetViewport(const AEMath::Recti viewport); + const AEMath::Recti& GetViewport(); + + void UseShader(Shader* shader); + void UnuseShader(); + Shader* GetShader(); + + // Draw call. + void DrawArrays(GLenum mode, GLint first, GLsizei count); + + /// + /// Matrix stack相关操作 + /// + void SetMatrixMode(MatrixMode mode); + MatrixMode GetMatrixMode(); + void PushMatrix(); + void PopMatrix(); + void LoadIdentity(); + void Rotate(float angle); + void Translate(float x, float y); + void Scale(float x, float y); + void Ortho(float l, float r, float b, float t, float n, float f); + AEMath::Matrix44& GetMatrix(MatrixMode mode); + AEMath::Matrix44 GetMVPMatrix(); + uint GetMatrixDepth(); + uint GetMatrixIndex(); + + void SetDrawColor(float r, float g, float b, float a); + Color& GetDrawColor(); + + /// + /// 清理错误提示 + /// + void WipeError(); + bool HasError(); + GLenum GetError(); + + /// + /// OpenGL3.0以后由用户管理矩阵变换、视口、shader等参数,这里保存一些OpenGL状态。注意 + /// 似乎全进程的,也就是说,Asura不支持多线程渲染。OpenGL上下文的创建使得一个上下 + /// 文绑定在一个HDC\窗口上,由于窗口是在特定线程创建的,所以OpenGL上下文也绑定了一个 + /// 特定的线程。同一个线程的不同HDC\窗口可以共享同一个OpenGL上下文。共享上下文是为了 + /// 共享上下文中创建的textuer\shader等handle。 + /// + struct + { + Shader* shader; ///< 当前使用的shader + AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 + MatrixStack matrix[3]; ///< model, view, projection矩阵 + MatrixMode matrixMode; ///< 当前操作的矩阵 + Color drawColor; ///< 绘制的颜色 + } state; + +#if ASURA_GL_PROFILE + struct + { + uint drawCall; ///< 统计drawcall + uint canvasSwitch; ///< 切换texture的次数 + uint shaderSwitch; ///< 切换shader的次数 + } stats; +#endif + + private: + + friend class Profiler; + + //----------------------------------------------------------------------------// + + LUAX_DECL_SINGLETON(GfxDevice); + + LUAX_DECL_ENUM(MatrixMode, 1); + LUAX_DECL_ENUM(GLParams, 1); + + LUAX_DECL_METHOD(_SetMatrixMode); + LUAX_DECL_METHOD(_GetMatrixMode); + LUAX_DECL_METHOD(_PushMatrix); + LUAX_DECL_METHOD(_PopMatrix); + LUAX_DECL_METHOD(_LoadIdentity); + LUAX_DECL_METHOD(_Rotate); + LUAX_DECL_METHOD(_Translate); + LUAX_DECL_METHOD(_Scale); + LUAX_DECL_METHOD(_Ortho); + LUAX_DECL_METHOD(_GetMatrixDepth); + LUAX_DECL_METHOD(_GetMatrixIndex); + + LUAX_DECL_METHOD(_UseShader); + LUAX_DECL_METHOD(_UnuseShader); + + //----------------------------------------------------------------------------// + + }; + + /// + /// OpenGL单例。 + /// + extern GfxDevice gfx; + + } +} + +#endif \ No newline at end of file diff --git a/source/modules/asura-core/graphics/gl.cpp b/source/modules/asura-core/graphics/gl.cpp deleted file mode 100644 index 5e6f216..0000000 --- a/source/modules/asura-core/graphics/gl.cpp +++ /dev/null @@ -1,219 +0,0 @@ -#include - -#include "../core_config.h" - -#include "gl.h" -#include "shader.h" -#include "matrix_stack.h" -#include "color.h" - -using namespace AEMath; - -namespace AsuraEngine -{ - namespace Graphics - { - -#if ASURA_DEBUG - static bool _instantiated = false; -#endif - - OpenGL gl; - - OpenGL::OpenGL() - : mUpdateMVPMatrix(true) - { -#if ASURA_DEBUG - ASSERT(!_instantiated); - _instantiated = true; -#endif - } - - OpenGL::~OpenGL() - { - } - - static bool inited = false; - - bool OpenGL::Init(const AEMath::Recti& view) - { - bool loaded = false; -#if ASURA_OPENGL_LOADER & ASURA_OPENGL_GLAD - if (!loaded) - loaded = gladLoadGL(); -#endif - if (!loaded) - return false; - SetViewport(view); - - inited = true; - return true; - } - - bool OpenGL::Inited() - { - return inited; - } - - void OpenGL::WipeError() - { - while (glGetError() != GL_NO_ERROR); - } - - bool OpenGL::HasError() - { - return glGetError() != GL_NO_ERROR; - } - - GLenum OpenGL::GetError() - { - return glGetError(); - } - - void OpenGL::SetDrawColor(float r, float g, float b, float a) - { - state.drawColor.Set(r, g, b, a); - } - - Color& OpenGL::GetDrawColor() - { - return state.drawColor; - } - - void OpenGL::SetViewport(const Recti v) - { - state.viewport = v; - glViewport(v.x, v.y, v.w, v.h); - } - - const Recti& OpenGL::GetViewport() - { - return state.viewport; - } - - void OpenGL::UseShader(Shader* shader) - { - if (state.shader != shader) - { - glUseProgram(shader->GetGLProgram()); - state.shader = shader; -#if ASURA_GL_PROFILE - ++stats.shaderSwitch; -#endif - } - shader->OnUse(); - } - - void OpenGL::UnuseShader() - { - state.shader->OnUnuse(); - state.shader = nullptr; - } - - Shader* OpenGL::GetShader() - { - return state.shader; - } - - void OpenGL::DrawArrays(GLenum mode, GLint first, GLsizei count) - { - glDrawArrays(mode, first, count); -#if ASURA_GL_PROFILE - ++stats.drawCall; -#endif - } - - //------------------------------------------------------------------------------// - - void OpenGL::SetMatrixMode(MatrixMode mode) - { - state.matrixMode = mode; - } - - MatrixMode OpenGL::GetMatrixMode() - { - return state.matrixMode; - } - - void OpenGL::PushMatrix() - { - state.matrix[state.matrixMode].Push(); - - mUpdateMVPMatrix = true; - } - - void OpenGL::PopMatrix() - { - state.matrix[state.matrixMode].Pop(); - - mUpdateMVPMatrix = true; - } - - void OpenGL::LoadIdentity() - { - state.matrix[state.matrixMode].LoadIdentity(); - - mUpdateMVPMatrix = true; - } - - void OpenGL::Rotate(float angle) - { - state.matrix[state.matrixMode].Rotate(angle); - - mUpdateMVPMatrix = true; - } - - void OpenGL::Translate(float x, float y) - { - state.matrix[state.matrixMode].Translate(x, y); - - mUpdateMVPMatrix = true; - } - - void OpenGL::Scale(float x, float y) - { - state.matrix[state.matrixMode].Scale(x, y); - - mUpdateMVPMatrix = true; - } - - void OpenGL::Ortho(float l, float r, float b, float t, float n, float f) - { - state.matrix[state.matrixMode].Ortho(l, r, b, t, n, f); - - mUpdateMVPMatrix = true; - } - - AEMath::Matrix44& OpenGL::GetMatrix(MatrixMode mode) - { - return state.matrix[mode].GetTop(); - } - - AEMath::Matrix44 OpenGL::GetMVPMatrix() - { - if (mUpdateMVPMatrix) - { - Matrix44& m = state.matrix[MATRIX_MODE_MODEL].GetTop(); - Matrix44& v = state.matrix[MATRIX_MODE_VIEW].GetTop(); - Matrix44& p = state.matrix[MATRIX_MODE_PROJECTION].GetTop(); - state.mvpMatrix = p * (v * m); - - mUpdateMVPMatrix = false; - } - return state.mvpMatrix; - } - - uint OpenGL::GetMatrixDepth() - { - return state.matrix[state.matrixMode].GetCapacity(); - } - - uint OpenGL::GetMatrixIndex() - { - return state.matrix[state.matrixMode].GetTopIndex(); - } - - //------------------------------------------------------------------------------// - - } -} \ No newline at end of file diff --git a/source/modules/asura-core/graphics/gl.h b/source/modules/asura-core/graphics/gl.h deleted file mode 100644 index 6c6ff30..0000000 --- a/source/modules/asura-core/graphics/gl.h +++ /dev/null @@ -1,165 +0,0 @@ -#ifndef __ASURA_ENGINE_OPENGL_H__ -#define __ASURA_ENGINE_OPENGL_H__ - -#include - -#include - -#include -#include -#include -#include - -#include "color.h" -#include "matrix_stack.h" - -namespace AsuraEngine -{ - namespace Graphics - { - - class Profiler; - class Shader; - - enum MatrixMode - { - MATRIX_MODE_PROJECTION = 0, - MATRIX_MODE_MODEL = 1, - MATRIX_MODE_VIEW = 2, - }; - - enum GLParams - { - GL_PARAM_MAX_TEXTURE_UNIT = 1, - }; - - /// - /// OpenGL上下文,用来做一些opengl状态的追踪。在编辑器多窗口环境下,一个窗口对应一个hwnd, - /// 一个hdc,以及数个opengl context,如果使用wglMakeCurrent(hdc, glc)指定当前线程耳朵 - /// 渲染窗口hdc和opengl上下文glc,gl中记录的就是任意一个线程的任意一个窗口的任意一个OpenGL - /// 上下文的状态,不支持多上下文渲染。 - /// - class OpenGL : public AEScripting::Portable - { - public: - - OpenGL(); - ~OpenGL(); - - /// - /// 获得常量值 - /// - int GetParam(GLParams param); - - /// - /// 初始化OpenGL上下文参数,发生在创建OpenGL上下文之后。此函数会注册OpenGL函数的地址。 - /// - bool Init(const AEMath::Recti& viewport); - bool Inited(); - - void SetViewport(const AEMath::Recti viewport); - const AEMath::Recti& GetViewport(); - - void UseShader(Shader* shader); - void UnuseShader(); - Shader* GetShader(); - - /// - /// 用来方便统计drawcall - /// - void DrawArrays(GLenum mode, GLint first, GLsizei count); - - /// - /// Matrix stack相关操作 - /// - void SetMatrixMode(MatrixMode mode); - MatrixMode GetMatrixMode(); - void PushMatrix(); - void PopMatrix(); - void LoadIdentity(); - void Rotate(float angle); - void Translate(float x, float y); - void Scale(float x, float y); - void Ortho(float l, float r, float b, float t, float n, float f); - AEMath::Matrix44& GetMatrix(MatrixMode mode); - AEMath::Matrix44 GetMVPMatrix(); - uint GetMatrixDepth(); - uint GetMatrixIndex(); - - void SetDrawColor(float r, float g, float b, float a); - Color& GetDrawColor(); - - /// - /// 清理错误提示 - /// - void WipeError(); - bool HasError(); - GLenum GetError(); - - /// - /// OpenGL3.0以后由用户管理矩阵变换、视口、shader等参数,这里保存一些OpenGL状态。注意 - /// 似乎全进程的,也就是说,Asura不支持多线程渲染。OpenGL上下文的创建使得一个上下 - /// 文绑定在一个HDC\窗口上,由于窗口是在特定线程创建的,所以OpenGL上下文也绑定了一个 - /// 特定的线程。同一个线程的不同HDC\窗口可以共享同一个OpenGL上下文。共享上下文是为了 - /// 共享上下文中创建的textuer\shader等handle。 - /// - struct - { - Shader* shader; ///< 当前使用的shader - AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 - MatrixStack matrix[3]; ///< model, view, projection矩阵 - MatrixMode matrixMode; ///< 当前操作的矩阵 - AEMath::Matrix44 mvpMatrix; ///< mvp matrix - Color drawColor; ///< 绘制的颜色 - } state; - -#if ASURA_GL_PROFILE - struct - { - uint drawCall; ///< 统计drawcall - uint canvasSwitch; ///< 切换texture的次数 - uint shaderSwitch; ///< 切换shader的次数 - } stats; -#endif - - private: - - friend class Profiler; - - //----------------------------------------------------------------------------// - - LUAX_DECL_SINGLETON(OpenGL); - - LUAX_DECL_ENUM(MatrixMode, 1); - LUAX_DECL_ENUM(GLParams, 1); - - LUAX_DECL_METHOD(_SetMatrixMode); - LUAX_DECL_METHOD(_GetMatrixMode); - LUAX_DECL_METHOD(_PushMatrix); - LUAX_DECL_METHOD(_PopMatrix); - LUAX_DECL_METHOD(_LoadIdentity); - LUAX_DECL_METHOD(_Rotate); - LUAX_DECL_METHOD(_Translate); - LUAX_DECL_METHOD(_Scale); - LUAX_DECL_METHOD(_Ortho); - LUAX_DECL_METHOD(_GetMatrixDepth); - LUAX_DECL_METHOD(_GetMatrixIndex); - - LUAX_DECL_METHOD(_UseShader); - LUAX_DECL_METHOD(_UnuseShader); - - //----------------------------------------------------------------------------// - - bool mUpdateMVPMatrix; - - }; - - /// - /// OpenGL单例。 - /// - extern OpenGL gl; - - } -} - -#endif \ No newline at end of file diff --git a/source/modules/asura-core/graphics/gpu_buffer.cpp b/source/modules/asura-core/graphics/gpu_buffer.cpp index e16a4ac..4c84b7e 100644 --- a/source/modules/asura-core/graphics/gpu_buffer.cpp +++ b/source/modules/asura-core/graphics/gpu_buffer.cpp @@ -72,16 +72,16 @@ namespace AsuraEngine if (mBuffer == 0) { // 初始化 - gl.WipeError(); + gfx.WipeError(); glGenBuffers(1, &mBuffer); if (mBuffer == 0) throw Exception("OpenGL glGenBuffers failed."); glBindBuffer(mTarget, mBuffer); glBufferData(mTarget, mSize, NULL, mUsage); // 初始化大小为size的缓冲,并根据usage存放到对应显存区块 - if (gl.HasError()) + if (gfx.HasError()) { glBindBuffer(mTarget, 0); - throw Exception("OpenGL glBufferData failed. Errorcode=%d.", gl.GetError()); + throw Exception("OpenGL glBufferData failed. Errorcode=%d.", gfx.GetError()); } #if ASURA_DEBUG mData = (byte*)malloc(size); @@ -91,10 +91,10 @@ namespace AsuraEngine else glBindBuffer(mTarget, mBuffer); glBufferSubData(mTarget, offset, size, data); - if (gl.HasError()) + if (gfx.HasError()) { glBindBuffer(mTarget, 0); - throw Exception("OpenGL glBufferSubData failed. Errorcode=%d.", gl.GetError()); + throw Exception("OpenGL glBufferSubData failed. Errorcode=%d.", gfx.GetError()); } glBindBuffer(mTarget, 0); #if ASURA_DEBUG @@ -108,7 +108,7 @@ namespace AsuraEngine { glBindBuffer(mTarget, mBuffer); } - + void GPUBuffer::UnBind() { glBindBuffer(mTarget, 0); diff --git a/source/modules/asura-core/graphics/gpu_buffer.h b/source/modules/asura-core/graphics/gpu_buffer.h index c492cbb..9013619 100644 --- a/source/modules/asura-core/graphics/gpu_buffer.h +++ b/source/modules/asura-core/graphics/gpu_buffer.h @@ -5,7 +5,7 @@ #include #include -#include "gl.h" +#include "gfx_device.h" namespace AsuraEngine { diff --git a/source/modules/asura-core/graphics/image.cpp b/source/modules/asura-core/graphics/image.cpp index ad2ae9a..d2f1d7b 100644 --- a/source/modules/asura-core/graphics/image.cpp +++ b/source/modules/asura-core/graphics/image.cpp @@ -4,7 +4,7 @@ #include "shader.h" #include "image.h" -#include "gl.h" +#include "gfx_device.h" using namespace AEIO; diff --git a/source/modules/asura-core/graphics/image.h b/source/modules/asura-core/graphics/image.h index cdc1b1a..932ca65 100644 --- a/source/modules/asura-core/graphics/image.h +++ b/source/modules/asura-core/graphics/image.h @@ -73,6 +73,10 @@ namespace AsuraEngine uint32 mWidth, mHeight; +#if ASURA_EDITOR + +#endif + }; } diff --git a/source/modules/asura-core/graphics/index_buffer.h b/source/modules/asura-core/graphics/index_buffer.h index 5fca0ea..ac77162 100644 --- a/source/modules/asura-core/graphics/index_buffer.h +++ b/source/modules/asura-core/graphics/index_buffer.h @@ -10,6 +10,9 @@ namespace AsuraEngine namespace Graphics { + /// + /// 顶点数据 + /// class IndexBuffer ASURA_FINAL : public AEScripting::Portable , public GPUBuffer diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp index 833cef0..866c4da 100644 --- a/source/modules/asura-core/graphics/shader.cpp +++ b/source/modules/asura-core/graphics/shader.cpp @@ -1,6 +1,6 @@ #include -#include "gl.h" +#include "gfx_device.h" #include "shader.h" using namespace std; @@ -123,59 +123,65 @@ namespace AsuraEngine void Shader::SetUniformFloat(uint loc, float value) { - if(gl.state.shader == this) + if(gfx.state.shader == this) glUniform1f(loc, value); } bool Shader::SetUniformTexture(uint loc, const Texture& texture) { - if (gl.state.shader != this) + if (gfx.state.shader != this) return false; - gl.WipeError(); + gfx.WipeError(); glActiveTexture(GL_TEXTURE0 + _texture_unit); - if (gl.HasError()) + if (gfx.HasError()) return false; GLint tex = texture.GetGLTexture(); glBindTexture(GL_TEXTURE_2D, tex); - if (gl.HasError()) + if (gfx.HasError()) return false; glUniform1i(loc, _texture_unit); - if (gl.HasError()) + if (gfx.HasError()) return false; ++_texture_unit; } void Shader::SetUniformVector2(uint loc, const Math::Vector2f& vec2) { - if (gl.state.shader == this) + if (gfx.state.shader == this) glUniform2f(loc, vec2.x, vec2.y); } void Shader::SetUniformVector3(uint loc, const Math::Vector3f& vec3) { - if (gl.state.shader == this) + if (gfx.state.shader == this) glUniform3f(loc, vec3.x, vec3.y, vec3.z); } void Shader::SetUniformVector4(uint loc, const Math::Vector4f& vec4) { - if (gl.state.shader == this) + if (gfx.state.shader == this) glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w); } void Shader::SetUniformMatrix44(uint loc, const Math::Matrix44& mat) { - if (gl.state.shader == this) + if (gfx.state.shader == this) glUniformMatrix4fv(loc, 1, GL_FALSE, mat.GetElements()); } void Shader::SetUniformColor(uint loc, const Color& color) { - if (gl.state.shader == this) + if (gfx.state.shader == this) glUniform4f(loc, color.r, color.g, color.b, color.a); } + //void Shader::GetUniform() + //{ + // //if(gfx.state.shader == this) + // // glGetUniformfv() + //} + uint Shader::GetGLTextureUnitCount() { GLint maxTextureUnits; @@ -244,30 +250,5 @@ namespace AsuraEngine glDisableVertexAttribArray(loc); } - void Shader::SetBuiltInModelMatrix(uint loc) - { - SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_MODEL)); - } - - void Shader::SetBuiltInViewMatrix(uint loc) - { - SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_VIEW)); - } - - void Shader::SetBuiltInProjectionMatrix(uint loc) - { - SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_PROJECTION)); - } - - void Shader::SetBuiltInMVPMatrix(uint loc) - { - SetUniformMatrix44(loc, gl.GetMVPMatrix()); - } - - void Shader::SetBuiltInDrawColor(uint loc) - { - SetUniformColor(loc, gl.GetDrawColor()); - } - } } \ No newline at end of file diff --git a/source/modules/asura-core/graphics/shader.h b/source/modules/asura-core/graphics/shader.h index 913332b..b5d866e 100644 --- a/source/modules/asura-core/graphics/shader.h +++ b/source/modules/asura-core/graphics/shader.h @@ -14,7 +14,7 @@ #include #include -#include "gl.h" +#include "gfx_device.h" #include "color.h" #include "texture.h" #include "vertex_buffer.h" @@ -45,18 +45,12 @@ namespace AsuraEngine void OnUse(); void OnUnuse(); - /// - /// 设置顶点属性,这些值在framework的renderer里面被设置,而不是Image、Mesh2D这些“数据源“ - /// 如果normalized为true,输入的数据会在给定buffer的类型下归一化,如255的颜色归一化到 - /// 0~1。 - /// + /// 顶点数据 void SetAttribute(int loc, VertexBuffer* vbo, uint offseti = 0, uint stridei = 0, bool normalized = false); int GetAttributeLocation(const std::string& attribute); void DisableAttribute(int loc); - /// - /// 设置uniform变量 - /// + /// Uniform变量 bool HasUniform(const std::string& uniform); uint GetUniformLocation(const std::string& uniform); void SetUniformFloat(uint loc, float value); @@ -67,14 +61,11 @@ namespace AsuraEngine void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44); bool SetUniformTexture(uint loc, const Texture& texture); - /// - /// 设置内置变量 - /// - void SetBuiltInModelMatrix(uint loc); - void SetBuiltInViewMatrix(uint loc); - void SetBuiltInProjectionMatrix(uint loc); - void SetBuiltInMVPMatrix(uint loc); - void SetBuiltInDrawColor(uint loc); + float GetUniformFloat(uint loc); + AEMath::Vector2f GetUniformVector2(uint loc); + AEMath::Vector3f GetUniformVector3(uint loc); + AEMath::Vector4f GetUniformVector4s(uint loc); + AEMath::Matrix44 GetUniformMatrix44(uint loc); GLuint GetGLProgram(); @@ -97,16 +88,12 @@ namespace AsuraEngine LUAX_DECL_METHOD(_SetUniformVector3); LUAX_DECL_METHOD(_SetUniformVector4); LUAX_DECL_METHOD(_SetUniformColor); - // 设置vertex attributes + /// 设置vertex attributes LUAX_DECL_METHOD(_GetAttributeLocation); LUAX_DECL_METHOD(_SetAttribute); LUAX_DECL_METHOD(_DisableAttribute); - // 设置内置uniform变量 - LUAX_DECL_METHOD(_SetBuiltInModelMatrix); - LUAX_DECL_METHOD(_SetBuiltInViewMatrix); - LUAX_DECL_METHOD(_SetBuiltInProjectionMatrix); - LUAX_DECL_METHOD(_SetBuiltInMVPMatrix); - LUAX_DECL_METHOD(_SetBuiltInDrawColor); + /// 设置内置uniform变量 + LUAX_DECL_METHOD(_SetBuiltInUniforms); //----------------------------------------------------------------------------// @@ -118,6 +105,8 @@ namespace AsuraEngine GLuint mFragShader; }; + + typedef Shader GpuProgram; } } diff --git a/source/modules/asura-core/graphics/texture.cpp b/source/modules/asura-core/graphics/texture.cpp index 522ba95..3438334 100644 --- a/source/modules/asura-core/graphics/texture.cpp +++ b/source/modules/asura-core/graphics/texture.cpp @@ -34,12 +34,12 @@ namespace AsuraEngine switch (colorformat) { case COLOR_FORMAT_RGBA8: - t.internalformat = GL_RGBA8; + t.internalformat = GL_RGBA8; // 4*sizeof(byte) ~= 4 bytes t.externalformat = GL_RGBA; t.type = GL_UNSIGNED_BYTE; break; case COLOR_FORMAT_RGBA32F: - t.internalformat = GL_RGBA32F; + t.internalformat = GL_RGBA32F; // 4*sizeof(float) = 16 bytes t.externalformat = GL_RGBA; t.type = GL_FLOAT; break; diff --git a/source/modules/asura-core/graphics/texture.h b/source/modules/asura-core/graphics/texture.h index 36a773d..799227e 100644 --- a/source/modules/asura-core/graphics/texture.h +++ b/source/modules/asura-core/graphics/texture.h @@ -7,7 +7,7 @@ #include "../core_config.h" #include "render_state.h" -#include "gl.h" +#include "gfx_device.h" namespace AsuraEngine { @@ -78,26 +78,6 @@ namespace AsuraEngine /// 如果设置U或V方向filter为 /// bool IsGenMipmap(); -/* - /// - /// 渲染整个texture到rt上,原点在左上角,向右,向下延伸 - /// - virtual void Render(const RenderTarget* rt, const RenderState& state) = 0; - - /// - /// 渲染texture的一部分到rt上,原点在左上角,向右,向下延伸。 - /// - virtual void Render(const RenderTarget* rt, const AEMath::Rectf& quad, const RenderState& state) = 0; -*/ - /// - /// 渲染整个image。 - /// - //virtual void Render() = 0; - - /// - /// 渲染部分image。 - /// - //virtual void Render(AEMath::Recti& quad) = 0; protected: diff --git a/source/modules/asura-core/graphics/vertex_buffer.h b/source/modules/asura-core/graphics/vertex_buffer.h index f00b4d2..b078c2b 100644 --- a/source/modules/asura-core/graphics/vertex_buffer.h +++ b/source/modules/asura-core/graphics/vertex_buffer.h @@ -10,6 +10,10 @@ namespace AsuraEngine namespace Graphics { + /// + /// 向framework提供了创建显存缓冲的功能,可以直接设置缓冲,绝大多数功能都可以通过此类 + /// 直接设置顶点数据。 + /// class VertexBuffer ASURA_FINAL : public AEScripting::Portable , public GPUBuffer diff --git a/source/modules/asura-core/input/input_device.hpp b/source/modules/asura-core/input/input_device.hpp index 1d08d09..4d82343 100644 --- a/source/modules/asura-core/input/input_device.hpp +++ b/source/modules/asura-core/input/input_device.hpp @@ -2,9 +2,9 @@ #define __ASURA_ENGINE_INPUT_BASE_H__ #include +#include #include "../core_config.h" -#include "../singleton.hpp" namespace AsuraEngine { diff --git a/source/modules/asura-core/mesh/mesh2d_data.h b/source/modules/asura-core/mesh/mesh2d_data.h index 8cb9723..cd7c9ee 100644 --- a/source/modules/asura-core/mesh/mesh2d_data.h +++ b/source/modules/asura-core/mesh/mesh2d_data.h @@ -66,7 +66,7 @@ namespace AsuraEngine /// /// 索引,用来构建ebo。 /// - std::vector mIndices; + std::vector mIndices; int mComponents; -- cgit v1.1-26-g67d0