1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#ifndef _ASURA_ENGINE_SHADER_H_
#define _ASURA_ENGINE_SHADER_H_
#include <map>
#include <string>
#include <vector>
#include <asura-base/Exception.h>
#include <asura-base/Scripting/Scripting.h>
#include <asura-base/FileSystem/Renewable.h>
#include <asura-base/Math/Vector2.hpp>
#include <asura-base/Math/vector3.hpp>
#include <asura-base/Math/vector4.h>
#include <asura-base/Math/matrix44.h>
#include <asura-base/Utilities/Stringmap.hpp>
#include <asura-base/Manager.hpp>
#include "GfxDevice.h"
#include "Color.h"
#include "Texture.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
namespace_begin(AsuraEngine)
namespace_begin(Graphics)
enum ShaderChannel
{
SHADER_CHANNEL_NONE = -1,
SHADER_CHANNEL_VERTEX = 0, // Vertex (vector3)
SHADER_CHANNEL_NORMAL, // Normal (vector3)
SHADER_CHANNEL_COLOR, // Vertex color
SHADER_CHANNEL_TEXCOORD0, // UV set 0 (vector2)
SHADER_CHANNEL_TEXCOORD1, // UV set 1 (vector2)
SHADER_CHANNEL_TANGENT, // Tangent (vector4)
SHADER_CHANNEL_COUNT, // Keep this last!
};
///
/// һshaderһڲʼ乲ijShaderuniformsͶݣֻṩ uniformsuseɫķ
/// ༭ÿshaderͨshaderҵuniforms¶frameworkmaterialá
///
class Shader ASURA_FINAL
: public Scripting::Portable<Shader>
, public AEFileSystem::Renewable
{
public:
Shader();
~Shader();
static void SetActive(Shader* shader);
static Shader* GetActive();
bool Load(const std::string& vert, const std::string& frag) ASURA_THROW(Exception);
// ʹSetActiveлshaderʱ
void OnEnable();
void OnDisable();
// Draw call֮
void OnUsed();
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);
bool HasUniform(const std::string& uniform);
uint GetUniformLocation(const std::string& uniform);
void SetUniformFloat(uint loc, float value);
void SetUniformVector2(uint loc, const Math::Vector2f& vec2);
void SetUniformVector3(uint loc, const Math::Vector3f& vec3);
void SetUniformVector4(uint loc, const Math::Vector4f& vec4);
void SetUniformColor(uint loc, const Color& color);
void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44);
bool SetUniformTexture(uint loc, const Texture& texture);
float GetUniformFloat(uint loc);
Vector2f GetUniformVector2(uint loc);
Vector3f GetUniformVector3(uint loc);
Vector4f GetUniformVector4s(uint loc);
Matrix44 GetUniformMatrix44(uint loc);
GLuint GetGLProgram();
static uint GetGLTextureUnitCount();
private:
bool CompileVertexShader(const std::string& vert, std::string& outError);
bool CompileFragementShader(const std::string& frag, std::string& outError);
std::string GetProgramWarnings();
std::string GetShaderWarnings(GLuint shader);
GLuint m_Program;
GLuint m_VertShader;
GLuint m_FragShader;
luaxport:
LUAX_DECL_FACTORY(Shader);
LUAX_DECL_METHOD(_New);
LUAX_DECL_METHOD(_Load);
LUAX_DECL_METHOD(_Update);
LUAX_DECL_METHOD(_HasUniform);
LUAX_DECL_METHOD(_GetUniformLocation);
LUAX_DECL_METHOD(_SetUniformFloat);
LUAX_DECL_METHOD(_SetUniformTexture);
LUAX_DECL_METHOD(_SetUniformVector2);
LUAX_DECL_METHOD(_SetUniformVector3);
LUAX_DECL_METHOD(_SetUniformVector4);
LUAX_DECL_METHOD(_SetUniformColor);
LUAX_DECL_METHOD(_GetAttributeLocation);
LUAX_DECL_METHOD(_SetAttribute);
LUAX_DECL_METHOD(_DisableAttribute);
LUAX_DECL_METHOD(_SetBuiltInUniforms);
};
// GPU program
typedef Shader GpuProgram;
namespace_end
namespace_end
#endif
|