From 3f457498b9c39d40a16a0ec6328880854f8cf4de Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 26 Feb 2019 08:48:54 +0800 Subject: *misc --- Source/Asura.Engine/Graphics/Shader.h | 92 +++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 43 deletions(-) (limited to 'Source/Asura.Engine/Graphics/Shader.h') diff --git a/Source/Asura.Engine/Graphics/Shader.h b/Source/Asura.Engine/Graphics/Shader.h index 02d9f46..2cc482b 100644 --- a/Source/Asura.Engine/Graphics/Shader.h +++ b/Source/Asura.Engine/Graphics/Shader.h @@ -4,16 +4,18 @@ #include #include -#include "luax/luax.h" +#include "Scripting/Luax.hpp" +#include "Math/Vector2.h" +#include "Math/Vector3.h" +#include "Math/Vector4.h" +#include "Math/Matrix44.h" #include "StringMap.hpp" #include "Object.h" #include "Color.h" #include "Manager.hpp" #include "Texture.h" -#include "Math/Vector2.h" -#include "Math/Vector3.h" -#include "Math/Vector4.h" #include "GL.h" +#include "SimClass.h" namespace AsuraEngine { @@ -21,10 +23,10 @@ namespace AsuraEngine { /// - /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只提供设置uniforms和use着色器的方法。编辑器针对每个 - /// shader,会通过shader代码找到声明的uniforms变量,并暴露给framework的material设置。 + /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只提供设置uniforms和use着色器的方法。编辑 + /// 器针对每个shader,会通过shader代码找到声明的uniforms变量,并暴露给framework的material设置。 /// - class Shader : virtual public Object + class Shader final : virtual public Object, public SimClass { public: @@ -32,9 +34,10 @@ namespace AsuraEngine ~Shader(); /// - /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map + /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map。使用glAttachShader重新编译生成着色器, + /// 不会重新申请着色器程序。 /// - void Load(const std::string& vertexShader, const std::string& fragmentShader); + bool Load(const std::string& vertexShader, const std::string& fragmentShader); /// /// 将当期shader设置为活动 @@ -44,16 +47,7 @@ namespace AsuraEngine /// /// 将当期shader设置为非活动 /// - void UnUse(); - - /// - /// 一系列设置uniforms变量的方法 - /// - void SetUniformFloat(const std::string& uniform, float value); - void SetUniformTexture(const std::string& uniform, const Texture& texture); - void SetUniformVector2(const std::string& uniform, const Math::Vector2& vec2); - void SetUniformVector3(const std::string& uniform, const Math::Vector3& vec3); - void SetUniformVector4(const std::string& uniform, const Math::Vector4& vec4); + void Unuse(); /// /// 在已经知道uniform location的情况下,设置值。 @@ -63,46 +57,58 @@ namespace AsuraEngine void SetUniformVector2(uint loc, const Math::Vector2& vec2); void SetUniformVector3(uint loc, const Math::Vector3& vec3); void SetUniformVector4(uint loc, const Math::Vector4& vec4); - -/* - /// - /// 注:后来考虑这个功能放在framework里面实现,更方便,而且shader在editor里也会用到。 - /// 清空缓存的unifrom location,用于重新编译shader,更新最新的uniform location - /// - void ClearUniformLocation(); -*/ - /// - /// 获得program id - /// - GLint GetNativeHandle(); + void SetUniformColor(uint loc, const Color& color); + void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44); uint GetUniformLocation(const std::string& uniform); - private: + bool HasUniform(const std::string& uniform); + + GLuint GetGLProgramHandle(); /// - /// 设置内置变量: - /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 - /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 - /// Texture Asura_MainTexture 主纹理 + /// 获得texture unit数量,一般为16个 /// - void SetBuiltInUniforms(); - - // OpenGL着色器标识符program id - GLuint mPID; - //// Uniform的locations - //StringMap mUniformLocation; + static uint GetGLTextureUnitCount(); - //////////////////////////////////////////////////////////////////////////////////////////////////////////// + //---------------------------------------------------------------------------------------------------------- LUAX_DECL_FACTORY(SimShader); + LUAX_DECL_METHOD(l_Use); + LUAX_DECL_METHOD(l_Unuse); + LUAX_DECL_METHOD(l_Load); + LUAX_DECL_METHOD(l_HasUniform); + LUAX_DECL_METHOD(l_GetUniformLocation); LUAX_DECL_METHOD(l_SetBuiltInUniforms); LUAX_DECL_METHOD(l_SetUniformFloat); LUAX_DECL_METHOD(l_SetUniformTexture); LUAX_DECL_METHOD(l_SetUniformVector2); LUAX_DECL_METHOD(l_SetUniformVector3); LUAX_DECL_METHOD(l_SetUniformVector4); + LUAX_DECL_METHOD(l_SetUniformColor); + + //---------------------------------------------------------------------------------------------------------- + + private: + + /// + /// 当前活动的shader + /// + static Shader* mCurrentShader; + + /// + /// 设置内置变量: + /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 + /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 + /// Texture Asura_MainTexture 主纹理 + /// + void SetBuiltInUniforms(); + + /// + /// OpenGL shader program handle. + /// + GLuint mProgramHandle; }; -- cgit v1.1-26-g67d0