-
Notifications
You must be signed in to change notification settings - Fork 124
/
GLSLShader.hx
139 lines (112 loc) · 4.22 KB
/
GLSLShader.hx
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
131
132
133
134
135
136
137
138
139
package nme.display3D.shaders.glsl;
import nme.utils.ByteArray;
import nme.display3D.Program3D;
import nme.Vector;
import nme.display3D.textures.Texture;
import nme.display3D.Context3DVertexBufferFormat;
import nme.display3D.VertexBuffer3D;
import nme.display3D.Context3D;
import nme.geom.Matrix3D;
import nme.display3D.Context3DProgramType;
import nme.display3D.shaders.ShaderUtils;
import haxe.Json;
typedef AgalInfoData = {
types : Dynamic,
consts : Dynamic,
storage : Dynamic,
varnames : Dynamic,
info : String,
agalasm : String
}
typedef Constant = Array<Float>;
class AgalInfo{
public var types : Hash<String>;
public var consts : Hash<Constant>;
public var storage : Hash<String>;
public var varnames : Hash<String>;
public var info : String;
public var agalasm : String;
public function new(agalInfoData : AgalInfoData) {
types = populate(agalInfoData.types);
consts = populate(agalInfoData.consts);
storage = populate(agalInfoData.storage);
varnames = populate(agalInfoData.varnames);
info = agalInfoData.info;
agalasm = agalInfoData.agalasm;
}
private function populate<Type>(data) : Hash<Type>{
var hash = new Hash();
for (key in Reflect.fields(data)) {
hash.set(key, Reflect.field(data, key));
}
return hash;
}
}
class GLSLShader {
//#if flash
private var agalInfo : AgalInfo;
//#end
public var type : Context3DProgramType;
public var nativeShader : nme.display3D.shaders.Shader;
public function new(type : Context3DProgramType, glslSource : String,
#if (cpp || neko || js)
?
#elseif glsl2agal
?
#end
agalInfoJson : String) {
this.type = type;
#if (cpp || neko || js)
nativeShader = ShaderUtils.createShader(type,glslSource);
#elseif flash
#if glsl2agal
if(agalInfoJson == null){ // compute agal from glsl only if no agalInfo are provided
var glsl2agal = new nme.display3D.shaders.GlslToAgal(glslSource, cast(type));
agalInfoJson = glsl2agal.compile();
}
#end
var agalInfoData : AgalInfoData = Json.parse(agalInfoJson);
agalInfo = new AgalInfo(agalInfoData);
var agalSource = agalInfo.agalasm;
nativeShader = ShaderUtils.createShader(type,agalSource);
#end
}
public function setUniformFromMatrix(context3D : Context3D, name : String , matrix : Matrix3D,transposedMatrix : Bool = false) : Void{
#if flash
var registerIndex = getRegisterIndexForUniform(name);
context3D.setProgramConstantsFromMatrix(type, registerIndex, matrix, transposedMatrix);
#elseif (cpp || neko || js)
context3D.setGLSLProgramConstantsFromMatrix(name, matrix, transposedMatrix);
#end
}
// expect 4 values
public function setUniformFromByteArray(context3D, name : String, data:ByteArray, byteArrayOffset:Int) : Void{
#if flash
var registerIndex = getRegisterIndexForUniform(name);
context3D.setProgramConstantsFromByteArray(type, registerIndex, 1, data, byteArrayOffset);
#elseif (cpp || neko || js)
context3D.setGLSLProgramConstantsFromByteArray(name, data, byteArrayOffset);
#end
}
// TODO do not use vector but use 4 float arguments ?
// for now it only use the first 4 float in the vector
public function setUniformFromVector(context3D : Context3D, name : String , vector : Vector<Float>) : Void{
#if flash
var registerIndex = getRegisterIndexForUniform(name);
context3D.setProgramConstantsFromVector(type, registerIndex, vector, 1);
#elseif (cpp || neko || js)
context3D.setGLSLProgramConstantsFromVector4(name, vector);
#end
}
private function getRegisterIndexForUniform(name : String) : Int{
//SUBCLASS NEED TO IMPLEMENT
return -1;
}
public function setup(context3D : Context3D) : Void{
#if flash
for (constantName in agalInfo.consts.keys()){
setUniformFromVector(context3D, constantName, Vector.ofArray(agalInfo.consts.get(constantName)));
}
#end
}
}