Skip to content

Commit 33400fc

Browse files
committed
Snow compatibility fixes
1 parent 39d958e commit 33400fc

12 files changed

+122
-51
lines changed

haxelib.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
"description": "GLSL tools for Haxe/Lime",
77
"version": "1.0.0",
88
"releasenote": "",
9-
"contributors": ["decept404"],
10-
"dependencies": { "lime":"" }
9+
"contributors": ["decept404"]
1110
}

shaderblox/ShaderBase.hx

+33-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package shaderblox;
2+
#if snow
3+
import snow.render.opengl.GL;
4+
#elseif lime
25
import lime.gl.GL;
36
import lime.gl.GLProgram;
47
import lime.gl.GLShader;
5-
import shaderblox.attributes.FloatAttribute;
8+
#end
9+
import shaderblox.attributes.Attribute;
610
import shaderblox.uniforms.IAppliable;
711
import shaderblox.uniforms.UTexture;
812

@@ -18,8 +22,7 @@ class ShaderBase
1822
{
1923
public var active:Bool;
2024
var uniforms:Array<IAppliable>;
21-
var uniformMap:Map<String, IAppliable>;
22-
var attributes:Array<FloatAttribute>;
25+
var attributes:Array<Attribute>;
2326
var aStride:Int;
2427
var name:String;
2528
var vert:GLShader;
@@ -30,7 +33,6 @@ class ShaderBase
3033

3134
private function new() {
3235
uniforms = [];
33-
uniformMap = new Map<String,IAppliable>();
3436
attributes = [];
3537
name = ("" + Type.getClass(this)).split(".").pop();
3638
createProperties();
@@ -39,16 +41,16 @@ class ShaderBase
3941
private function createProperties():Void { }
4042

4143

42-
public inline function getUniformByName(str:String):IAppliable {
43-
return uniformMap[str];
44-
}
45-
4644
public function create():Void { }
4745

4846
public function destroy():Void {
47+
trace("Destroying " + this);
4948
GL.deleteShader(vert);
5049
GL.deleteShader(frag);
5150
GL.deleteProgram(prog);
51+
prog = null;
52+
vert = null;
53+
frag = null;
5254
ready = false;
5355
}
5456

@@ -105,13 +107,18 @@ class ShaderBase
105107
prog = shaderProgram;
106108

107109
//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];
110113
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 +"'");
115122
}
116123
#if (debug && !display) trace("Defined uniform "+u.name+" at "+u.location); #end
117124
}
@@ -126,7 +133,11 @@ class ShaderBase
126133
}
127134

128135
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+
}
130141
if (!ready) create();
131142
GL.useProgram(prog);
132143
if (initUniforms) setUniforms();
@@ -141,26 +152,26 @@ class ShaderBase
141152
GL.useProgram(null);
142153
}
143154

144-
public inline function setUniforms()
155+
public function setUniforms()
145156
{
146157
for (u in uniforms) {
147-
if (u.location == -1) continue;
148158
u.apply();
149159
}
150160
}
151161
public function setAttributes()
152162
{
153163
var offset:Int = 0;
154164
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) {
157168
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);
159170
}
160-
offset += attributes[i].byteSize;
171+
offset += att.byteSize;
161172
}
162173
}
163-
inline function disableAttributes()
174+
function disableAttributes()
164175
{
165176
for (i in 0...attributes.length) {
166177
var idx = attributes[i].location;

shaderblox/attributes/Attribute.hx

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package shaderblox.attributes;
77
class Attribute
88
{
99
public var byteSize:Int;
10+
public var itemCount:Int;
1011
public var location:Int;
1112
public var name:String;
13+
public var type:Int;
1214

1315
}
+8-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package shaderblox.attributes;
2+
#if snow
3+
import snow.render.opengl.GL;
4+
#elseif lime
5+
import lime.gl.GL;
6+
#end
27

38
/**
49
* 4-byte float vertex attribute occupying a variable number of floats
510
* @author Andreas Rønning
611
*/
712
class FloatAttribute extends Attribute
813
{
9-
public var numFloats:Int;
1014
public function new(name:String, location:Int, nFloats:Int = 1)
1115
{
1216
this.name = name;
1317
this.location = location;
1418
byteSize = nFloats * 4;
15-
numFloats = nFloats;
19+
itemCount = nFloats;
20+
type = GL.FLOAT;
1621
}
1722
public function toString():String
1823
{
19-
return "[FloatAttribute numFloats=" + numFloats + " byteSize=" + byteSize + " location=" + location + " name=" + name + "]";
24+
return "[FloatAttribute itemCount=" + itemCount + " byteSize=" + byteSize + " location=" + location + " name=" + name + "]";
2025
}
2126

2227
}

shaderblox/macro/ShaderBuilder.hx

+25-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Lambda;
1212
* @author Andreas Rønning
1313
*/
1414
private typedef FieldDef = {index:Int, typeName:String, fieldName:String };
15-
private typedef AttribDef = {index:Int, typeName:String, fieldName:String, numFloats:Int };
15+
private typedef AttribDef = {index:Int, typeName:String, fieldName:String, itemCount:Int };
1616
class ShaderBuilder
1717
{
1818
#if macro
@@ -22,6 +22,8 @@ class ShaderBuilder
2222
static var vertSource:String;
2323
static var fragSource:String;
2424
static var asTemplate:Bool;
25+
static inline var GL_FLOAT:Int = 0x1406;
26+
static inline var GL_INT:Int = 0x1404;
2527

2628
static function getSources(type:ClassType):Array<String> {
2729
var meta = type.meta.get();
@@ -139,7 +141,7 @@ class ShaderBuilder
139141
return false;
140142
}
141143

142-
static function buildAttribute(position, fields, source:String) {
144+
static function buildAttribute(position, fields, source:String):Void {
143145
source = StringTools.trim(source);
144146
var args = source.split(" ").slice(1);
145147
var name = StringTools.trim(args[1].split(";").join(""));
@@ -151,20 +153,31 @@ class ShaderBuilder
151153
if (existing.fieldName == name) return;
152154
}
153155
var pack = ["shaderblox", "attributes"];
154-
var type = { pack : pack, name : "FloatAttribute", params : [], sub : null };
155-
var numFloats:Int = 0;
156+
var itemCount:Int = 0;
157+
var itemType:Int = -1;
156158
switch(args[0]) {
157159
case "float":
158-
numFloats = 1;
160+
itemCount = 1;
161+
itemType = GL_FLOAT;
159162
case "vec2":
160-
numFloats = 2;
163+
itemCount = 2;
164+
itemType = GL_FLOAT;
161165
case "vec3":
162-
numFloats = 3;
166+
itemCount = 3;
167+
itemType = GL_FLOAT;
163168
case "vec4":
164-
numFloats = 4;
169+
itemCount = 4;
170+
itemType = GL_FLOAT;
165171
default:
166172
throw "Unknown attribute type: " + args[0];
167173
}
174+
var attribClassName:String = switch(itemType) {
175+
case GL_FLOAT:
176+
"FloatAttribute";
177+
default:
178+
throw "Unknown attribute type: " + itemType;
179+
}
180+
var type = { pack : pack, name : attribClassName, params : [], sub : null };
168181
var fld = {
169182
name : name,
170183
doc : null,
@@ -174,7 +187,7 @@ class ShaderBuilder
174187
pos : position
175188
};
176189
fields.push(fld);
177-
var f = { index:attributeFields.length, fieldName:name, typeName:pack.join(".") + ".FloatAttribute", numFloats:numFloats };
190+
var f = { index:attributeFields.length, fieldName:name, typeName:pack.join(".") + "." + attribClassName, itemCount:itemCount };
178191
attributeFields.push(f);
179192
}
180193
static function buildUniform(position, fields, source:String) {
@@ -303,11 +316,11 @@ class ShaderBuilder
303316
var stride:Int = 0;
304317
for (att in attributeFields) {
305318
var name:String = att.fieldName;
306-
var numFloats:Int = att.numFloats;
307-
stride += numFloats * 4;
319+
var numItems:Int = att.itemCount;
320+
stride += numItems * 4;
308321
exprs.push(
309322
macro {
310-
var instance = Type.createInstance( Type.resolveClass( $v { att.typeName } ), [$v { att.fieldName }, $v { att.index }, $v { numFloats } ]);
323+
var instance = Type.createInstance( Type.resolveClass( $v { att.typeName } ), [$v { att.fieldName }, $v { att.index }, $v { numItems } ]);
311324
Reflect.setField(this, $v { name }, instance);
312325
attributes.push(instance);
313326
}

shaderblox/uniforms/UFloat.hx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package shaderblox.uniforms;
2+
#if snow
3+
import snow.render.opengl.GL;
4+
#elseif lime
25
import lime.gl.GL;
6+
#end
37

48
/**
59
* Float uniform
@@ -10,6 +14,6 @@ class UFloat extends UniformBase<Float> implements IAppliable {
1014
super(name, index, f);
1115
}
1216
public inline function apply():Void {
13-
GL.uniform1f(location, data);
17+
if(location!=-1) GL.uniform1f(location, data);
1418
}
1519
}

shaderblox/uniforms/UMatrix.hx

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package shaderblox.uniforms;
2+
#if snow
3+
import snow.utils.Float32Array;
4+
import falconer.utils.Matrix3D;
5+
import snow.render.opengl.GL;
6+
#elseif lime
27
import lime.gl.GL;
38
import lime.utils.Matrix3D;
9+
#else
10+
throw "Requires lime or snow";
11+
#end
412

513
/**
614
* Matrix3D uniform (not transposed)
@@ -12,6 +20,12 @@ class UMatrix extends UniformBase<Matrix3D> implements IAppliable {
1220
super(name, index, m);
1321
}
1422
public inline function apply():Void {
15-
GL.uniformMatrix3D(location, false, data);
23+
#if lime
24+
if (location != -1) GL.uniformMatrix3D(location, false, data);
25+
#elseif snow
26+
if (location != -1) {
27+
GL.uniformMatrix4fv(location, false, new Float32Array(data.rawData));
28+
}
29+
#end
1630
}
1731
}

shaderblox/uniforms/UMatrixTransposed.hx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package shaderblox.uniforms;
2-
import lime.gl.GL;
2+
#if lime
33
import lime.utils.Matrix3D;
4+
import lime.gl.GL;
5+
#elseif snow
6+
import falconer.utils.Matrix3D;
7+
import snow.render.opengl.GL;
8+
#end
49

510
/**
611
* Transposed Matrix3D uniform
@@ -12,6 +17,6 @@ class UMatrixTransposed extends UniformBase<Matrix3D> implements IAppliable {
1217
super(index, m);
1318
}
1419
public inline function apply():Void {
15-
GL.uniformMatrix3D(location, true, data);
20+
if(location!=-1) GL.uniformMatrix3D(location, true, data);
1621
}
1722
}

shaderblox/uniforms/UTexture.hx

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package shaderblox.uniforms;
2+
#if snow
3+
import snow.render.opengl.GL;
4+
#elseif lime
25
import lime.gl.GL;
36
import lime.gl.GLTexture;
7+
#end
48

59
/**
610
* GLTexture uniform
711
* @author Andreas Rønning
812
*/
913
class UTexture extends UniformBase<GLTexture> implements IAppliable {
1014
public var samplerIndex:Int;
15+
static var lastActiveTexture:Int = -1;
1116
public function new(name:String, index:Int) {
1217
super(name, index, null);
1318
}
1419
public inline function apply():Void {
15-
if (data == null) return;
20+
if (data == null || location==-1 ) return;
1621
GL.uniform1i(location, samplerIndex);
1722
var idx = GL.TEXTURE0 + samplerIndex;
18-
//if (RenderState.ACTIVE_TEXTURE_UNIT != idx) {
19-
GL.activeTexture(idx);
23+
if (lastActiveTexture != idx) {
24+
GL.activeTexture(lastActiveTexture = idx);
2025
GL.enable(GL.TEXTURE_2D);
21-
//}
22-
//RenderState.ACTIVE_TEXTURE_UNIT = idx;
26+
}
2327
GL.bindTexture(GL.TEXTURE_2D, data);
2428
}
2529
}

shaderblox/uniforms/UVec2.hx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package shaderblox.uniforms;
2+
#if snow
3+
import snow.render.opengl.GL;
4+
#elseif lime
25
import lime.gl.GL;
6+
#end
37

48
/**
59
* Vector2 float uniform
@@ -22,6 +26,6 @@ class UVec2 extends UniformBase<Pt> implements IAppliable {
2226
super(name, index, p);
2327
}
2428
public inline function apply():Void {
25-
GL.uniform2f(location, data.x, data.y);
29+
if(location!=-1) GL.uniform2f(location, data.x, data.y);
2630
}
2731
}

0 commit comments

Comments
 (0)