1
1
package shaderblox ;
2
+ #if snow
3
+ import snow .render .opengl .GL ;
4
+ #elseif lime
2
5
import lime .gl .GL ;
3
6
import lime .gl .GLProgram ;
4
7
import lime .gl .GLShader ;
5
- import shaderblox .attributes .FloatAttribute ;
8
+ #end
9
+ import shaderblox .attributes .Attribute ;
6
10
import shaderblox .uniforms .IAppliable ;
7
11
import shaderblox .uniforms .UTexture ;
8
12
@@ -18,8 +22,7 @@ class ShaderBase
18
22
{
19
23
public var active : Bool ;
20
24
var uniforms : Array <IAppliable >;
21
- var uniformMap : Map <String , IAppliable >;
22
- var attributes : Array <FloatAttribute >;
25
+ var attributes : Array <Attribute >;
23
26
var aStride : Int ;
24
27
var name : String ;
25
28
var vert : GLShader ;
@@ -30,7 +33,6 @@ class ShaderBase
30
33
31
34
private function new () {
32
35
uniforms = [];
33
- uniformMap = new Map <String ,IAppliable >();
34
36
attributes = [];
35
37
name = (" " + Type .getClass (this )).split (" ." ).pop ();
36
38
createProperties ();
@@ -39,16 +41,16 @@ class ShaderBase
39
41
private function createProperties (): Void { }
40
42
41
43
42
- public inline function getUniformByName (str : String ): IAppliable {
43
- return uniformMap [str ];
44
- }
45
-
46
44
public function create (): Void { }
47
45
48
46
public function destroy (): Void {
47
+ trace (" Destroying " + this );
49
48
GL .deleteShader (vert );
50
49
GL .deleteShader (frag );
51
50
GL .deleteProgram (prog );
51
+ prog = null ;
52
+ vert = null ;
53
+ frag = null ;
52
54
ready = false ;
53
55
}
54
56
@@ -105,13 +107,18 @@ class ShaderBase
105
107
prog = shaderProgram ;
106
108
107
109
// Validate uniform locations
108
- for (u in uniforms ) {
109
- uniformMap [u .name ] = u ;
110
+ var count = uniforms .length ;
111
+ while (count -- > 0 ) {
112
+ var u = uniforms [count ];
110
113
var loc = uniformLocations .get (u .name );
111
- u .location = loc == null ? - 1 : loc ;
112
- if (u .location == - 1 ) trace (" WARNING(" + name + " ): unused uniform '" + u .name + " '" );
113
- if (Std .is (u , UTexture ) && u .location != - 1 ) {
114
- cast (u , UTexture ).samplerIndex = numTextures ++ ;
114
+ if (loc != null ) {
115
+ u .location = loc ;
116
+ if (Std .is (u , UTexture )) {
117
+ cast (u , UTexture ).samplerIndex = numTextures ++ ;
118
+ }
119
+ }else {
120
+ uniforms .remove (u );
121
+ trace (" WARNING(" + name + " ): unused uniform '" + u .name + " '" );
115
122
}
116
123
#if (debug && !display) trace (" Defined uniform " + u .name + " at " + u .location ); #end
117
124
}
@@ -126,7 +133,11 @@ class ShaderBase
126
133
}
127
134
128
135
public function activate (initUniforms : Bool = true , initAttribs : Bool = false ): Void {
129
- if (active ) return ;
136
+ if (active ) {
137
+ if (initUniforms ) setUniforms ();
138
+ if (initAttribs ) setAttributes ();
139
+ return ;
140
+ }
130
141
if (! ready ) create ();
131
142
GL .useProgram (prog );
132
143
if (initUniforms ) setUniforms ();
@@ -141,26 +152,26 @@ class ShaderBase
141
152
GL .useProgram (null );
142
153
}
143
154
144
- public inline function setUniforms ()
155
+ public function setUniforms ()
145
156
{
146
157
for (u in uniforms ) {
147
- if (u .location == - 1 ) continue ;
148
158
u .apply ();
149
159
}
150
160
}
151
161
public function setAttributes ()
152
162
{
153
163
var offset : Int = 0 ;
154
164
for (i in 0 ... attributes .length ) {
155
- var location = attributes [i ].location ;
156
- if (location != - 1 ){
165
+ var att = attributes [i ];
166
+ var location = att .location ;
167
+ if (location != - 1 ) {
157
168
GL .enableVertexAttribArray (location );
158
- GL .vertexAttribPointer (location , attributes [ i ]. numFloats , GL . FLOAT , false , aStride , offset );
169
+ GL .vertexAttribPointer (location , att . itemCount , att . type , false , aStride , offset );
159
170
}
160
- offset + = attributes [ i ] .byteSize ;
171
+ offset + = att .byteSize ;
161
172
}
162
173
}
163
- inline function disableAttributes ()
174
+ function disableAttributes ()
164
175
{
165
176
for (i in 0 ... attributes .length ) {
166
177
var idx = attributes [i ].location ;
0 commit comments