Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
phoenix; add per geometry blend state #414
Browse files Browse the repository at this point in the history
- doesn't influence the batching order to minimize state changes (i.e
blend state is order of creation)
  • Loading branch information
ruby0x1 committed Mar 17, 2017
1 parent 820525a commit 6d18aa2
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 42 deletions.
38 changes: 37 additions & 1 deletion luxe/Visual.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ class Visual extends Entity {
@:isVar public var visible (default,set) : Bool = true;
/** the geometry depth value (see guides)*/
@:isVar public var depth (default,set) : Float = 0.0;
/** If note null, the geometry will be clipped to this rectangle region (in world space). */
/** If not null, the geometry will be clipped to this rectangle region (in window viewport space). */
@:isVar public var clip_rect (default,set) : Rectangle;
/** convenience: controls the rotation around the z axis, in radians. */
@:isVar public var radians (get,set) : Float = 0.0;
/** convenience: controls the rotation around the z axis, in degrees. */
public var rotation_z (get,set) : Float;
/** Set the per geometry source blending, for the alpha component */
public var blend_src_alpha (get, set) : BlendMode;
/** Set the per geometry source blending, for the rgb components */
public var blend_src_rgb (get, set) : BlendMode;
/** Set the per geometry destination blending, for the alpha component */
public var blend_dest_alpha (get, set) : BlendMode;
/** Set the per geometry destination blending, for the rgb components */
public var blend_dest_rgb (get, set) : BlendMode;

//private
var _rotation_euler : Vector;
Expand Down Expand Up @@ -354,6 +362,34 @@ class Visual extends Entity {

} //set_size

//Blending

inline function get_blend_src_alpha() : BlendMode return geometry.blend_src_alpha;
inline function get_blend_src_rgb() : BlendMode return geometry.blend_src_rgb;
inline function get_blend_dest_alpha() : BlendMode return geometry.blend_dest_alpha;
inline function get_blend_dest_rgb() : BlendMode return geometry.blend_dest_rgb;

inline function set_blend_src_alpha(val:BlendMode) {
geometry.blend_src_alpha = val;
return val;
}

inline function set_blend_src_rgb(val:BlendMode) {
geometry.blend_src_rgb = val;
return val;
}

inline function set_blend_dest_alpha(val:BlendMode) {
geometry.blend_dest_alpha = val;
return val;
}

inline function set_blend_dest_rgb(val:BlendMode) {
geometry.blend_dest_rgb = val;
return val;
}


//Rotation

function get_rotation_z() : Float {
Expand Down
42 changes: 35 additions & 7 deletions phoenix/BatchState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class BatchState {
public var is_clipping : Bool;
public var clip_rect : Rectangle;
public var last_clip_rect : Rectangle;
public var last_blend_src_alpha : Int;
public var last_blend_src_rgb : Int;
public var last_blend_dest_alpha : Int;
public var last_blend_dest_rgb : Int;

public var log : Bool = false;

Expand Down Expand Up @@ -82,6 +86,23 @@ class BatchState {
last_shader_id = _shader.program;
}

var blend_dirty = last_blend_src_rgb != geom_state.blend_src_rgb;
blend_dirty = blend_dirty || (last_blend_src_alpha != geom_state.blend_src_alpha);
blend_dirty = blend_dirty || (last_blend_dest_rgb != geom_state.blend_dest_rgb);
blend_dirty = blend_dirty || (last_blend_dest_alpha != geom_state.blend_dest_alpha);

if(blend_dirty && !geom_state.ignore_blend) {
last_blend_src_rgb = geom_state.blend_src_rgb;
last_blend_src_alpha = geom_state.blend_src_alpha;
last_blend_dest_rgb = geom_state.blend_dest_rgb;
last_blend_dest_alpha = geom_state.blend_dest_alpha;
GL.blendFuncSeparate(
last_blend_src_rgb,
last_blend_dest_rgb,
last_blend_src_alpha,
last_blend_dest_alpha
);
}

} //state.dirty

Expand Down Expand Up @@ -118,26 +139,32 @@ class BatchState {

// finally, mark the state as clean.
geom_state.clean();
}

} //activate

public function deactivate(batcher:Batcher) {

//undo any textures we bound last
if(last_texture_id != null) {
//undo any textures we bound last
Luxe.renderer.state.bindTexture2D(null);
}

//for now we just disable any shader because other
//batchers are not aware of us yet.
Luxe.renderer.state.useProgram(null);

// remove clipping
if( is_clipping ) GL.disable( GL.SCISSOR_TEST );
}
//remove clipping
if(is_clipping) GL.disable(GL.SCISSOR_TEST);

//default blend mode
GL.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
GL.blendEquation(GL.FUNC_ADD);

} //deactivate

public function update( geom:Geometry ) : Bool {

geom_state.clone_onto( last_geom_state );
geom_state.clone_onto(last_geom_state);
geom_state.update(geom.state);

if(geom_state.clip){
Expand All @@ -146,7 +173,8 @@ class BatchState {
}

return geom_state.dirty || (last_clip_rect != clip_rect);
}

} //update


//noisy debug stuff
Expand Down
90 changes: 90 additions & 0 deletions phoenix/geometry/Geometry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,27 @@ class Geometry {
@:isVar public var shader (get, set) : Shader;
@:isVar public var depth (get, set) : Float;
@:isVar public var clip_rect (get, set) : Rectangle;
@:isVar public var blend_src_alpha(get, set) : Int = BlendMode.src_alpha;
@:isVar public var blend_src_rgb(get, set) : Int = BlendMode.src_alpha;
@:isVar public var blend_dest_alpha(get, set) : Int = BlendMode.one_minus_src_alpha;
@:isVar public var blend_dest_rgb(get, set) : Int = BlendMode.one_minus_src_alpha;

var shadow_primitive_type : PrimitiveType;
var shadow_texture : Texture;
var shadow_shader : Shader;
var shadow_depth : Float = 0.0;
var shadow_clip : Bool = false;
var shadow_blend_src_alpha : Int = BlendMode.src_alpha;
var shadow_blend_src_rgb : Int = BlendMode.src_alpha;
var shadow_blend_dest_alpha : Int = BlendMode.one_minus_src_alpha;
var shadow_blend_dest_rgb : Int = BlendMode.one_minus_src_alpha;

var dirty_primitive_type : Bool = false;
var dirty_texture : Bool = false;
var dirty_shader : Bool = false;
var dirty_depth : Bool = false;
var dirty_clip : Bool = false;
var dirty_blend : Bool = false;

//Geometry properties
@:isVar public var visible (default, set) : Bool = true;
Expand Down Expand Up @@ -170,6 +179,10 @@ class Geometry {
key.shader = state.shader;
key.depth = state.depth;
key.clip = state.clip;
key.blend_src_alpha = state.blend_src_alpha;
key.blend_src_rgb = state.blend_src_rgb;
key.blend_dest_alpha = state.blend_dest_alpha;
key.blend_dest_rgb = state.blend_dest_rgb;

transform.id = uuid;
transform.name = id;
Expand Down Expand Up @@ -211,6 +224,10 @@ class Geometry {
key.shader = state.shader;
key.depth = state.depth;
key.clip = state.clip;
key.blend_src_alpha = state.blend_src_alpha;
key.blend_src_rgb = state.blend_src_rgb;
key.blend_dest_alpha = state.blend_dest_alpha;
key.blend_dest_rgb = state.blend_dest_rgb;

} //refresh_key

Expand Down Expand Up @@ -636,6 +653,14 @@ class Geometry {
state.clip = shadow_clip;
} //dirty_clip

if(dirty_blend) {
dirty_blend = false;
state.blend_src_alpha = shadow_blend_src_alpha;
state.blend_src_rgb = shadow_blend_src_rgb;
state.blend_dest_alpha = shadow_blend_dest_alpha;
state.blend_dest_rgb = shadow_blend_dest_rgb;
} //dirty_clip

//make sure the key is updated
refresh_key();

Expand Down Expand Up @@ -748,6 +773,67 @@ class Geometry {

} //set_depth

//Blends

inline function get_blend_src_alpha() : Int {
return state.blend_src_alpha;
}

inline function get_blend_src_rgb() : Int {
return state.blend_src_rgb;
}

inline function get_blend_dest_alpha() : Int {
return state.blend_dest_alpha;
}

inline function get_blend_dest_rgb() : Int {
return state.blend_dest_rgb;
}

function set_blend_src_alpha(val:Int) : Int {
state.ignore_blend = false;
if(state.blend_src_alpha != val) {
shadow_blend_src_alpha = val;
dirty_blend = true;
refresh();
}
return blend_src_alpha = val;
}

function set_blend_src_rgb(val:Int) : Int {
state.ignore_blend = false;
if(state.blend_src_rgb != val) {
shadow_blend_src_rgb = val;
dirty_blend = true;
refresh();
}
return blend_src_rgb = val;
}

function set_blend_dest_alpha(val:Int) : Int {
state.ignore_blend = false;
if(state.blend_dest_alpha != val) {
shadow_blend_dest_alpha = val;
dirty_blend = true;
refresh();
}
return blend_dest_alpha = val;
}

function set_blend_dest_rgb(val:Int) : Int {
state.ignore_blend = false;
if(state.blend_dest_rgb != val) {
shadow_blend_dest_rgb = val;
dirty_blend = true;
refresh();
}
return blend_dest_rgb = val;
}




//Clip

inline function get_clip() : Bool {
Expand Down Expand Up @@ -808,5 +894,9 @@ class GeometryKey {
public var shader : Shader;
public var depth : Float = 0;
public var clip : Bool = false;
public var blend_src_alpha : Int = 0;
public var blend_src_rgb : Int = 0;
public var blend_dest_alpha : Int = 0;
public var blend_dest_rgb : Int = 0;

} //GeometryKey
Loading

0 comments on commit 6d18aa2

Please sign in to comment.