Permalink
Browse files

Added SharedStage3DContext.hx util for sharing stage3D contexts... in…

…vestigating issues with shared context and clipRect in feathers/starling.

Components like the PickerList and Toggle buttons that use the clipRect (stage3D scissorRectangle) for their masking, don't get any masking applied. scissorRect not being set on the context before rendering..?
  • Loading branch information...
1 parent 41cfef9 commit cc499103f1026db6f3876a5a20e72a02a34ceb80 @mikedotalmond committed Dec 20, 2012
View
Binary file not shown.
View
@@ -30,7 +30,7 @@
</build>
<!-- haxelib libraries -->
<haxelib>
- <!-- example: <library name="..." /> -->
+ <library name="hxs" />
</haxelib>
<!-- Class files to compile (other referenced classes will automatically be included) -->
<compileTargets>
Binary file not shown.
Binary file not shown.
View
@@ -2,6 +2,7 @@ package ;
import feathers.controls.Button;
import feathers.system.DeviceCapabilities;
+import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
@@ -23,19 +24,19 @@ import starling.core.Starling;
@:final class Main {
- public static var viewport(default, null):Rectangle;
-
private var _starling:Starling;
+ private var sharedContext:SharedStage3DContext;
public function new() {
- if (Capabilities.cpuArchitecture == "ARM") { // mobile - fullscreen
- viewport = new Rectangle(0, 0, Lib.current.stage.fullScreenWidth, Lib.current.stage.fullScreenHeight);
- } else {
+ if (Capabilities.cpuArchitecture != "ARM") { // not mobile
DeviceCapabilities.dpi = 132; // fake a higher dpi on desktop
- viewport = new Rectangle(0, 0, Lib.current.stage.stageWidth, Lib.current.stage.stageHeight);
}
+ sharedContext = new SharedStage3DContext(Lib.current.stage);
+ sharedContext.ready.addOnce(startup);
+ sharedContext.requestDraw.add(starlingRender);
+
/*
* The feathers Haxe extern library can be found in lib/feathers
* (it might end up on haxelib later)
@@ -64,10 +65,25 @@ import starling.core.Starling;
* I also ported the MetalWorksMobileTheme as part of this example - lib/MetalWorksMobileTheme
* */
- _starling = new Starling(feathers.examples.componentsExplorer.Main, Lib.current.stage, viewport);
+ }
+ private function startup() {
+ _starling = new Starling(feathers.examples.componentsExplorer.Main, sharedContext.stage, sharedContext.fullViewport, sharedContext.stage3D);
_starling.start();
+
+ Lib.current.stage.addEventListener(Event.ENTER_FRAME, enterFrame);
+ }
+
+ private function enterFrame(e:Event):Void {
+ sharedContext.update(0,0);
}
+
+ private function starlingRender(dt,t):Void {
+ _starling.nextFrame();
+ }
+
+
+
static function main() {
var stage = Lib.current.stage;
@@ -0,0 +1,172 @@
+package;
+
+import flash.display.Stage;
+import flash.display.Stage3D;
+
+import flash.display3D.Context3D;
+import flash.display3D.Context3DTriangleFace;
+import flash.display3D.Context3DCompareMode;
+import flash.display3D.Context3DBlendFactor;
+import flash.display3D.Context3DRenderMode;
+
+import flash.errors.Error;
+import flash.events.ErrorEvent;
+import flash.events.Event;
+import flash.events.FullScreenEvent;
+import flash.geom.Rectangle;
+import flash.system.Capabilities;
+
+import hxs.Signal;
+import hxs.Signal1;
+import hxs.Signal2;
+
+/**
+ * ...
+ * @author Mike Almond - https://github.com/mikedotalmond
+ */
+
+
+@:final class SharedStage3DContext {
+
+ #if debug
+ public static inline var Debug = true;
+ public static inline var AA = 0;
+ #else
+ public static inline var Debug = false;
+ #if air
+ public static inline var AA = 0;
+ #else
+ public static inline var AA = 8;
+ #end
+ #end
+ public static var CPUArchitecture :String = Capabilities.cpuArchitecture;
+ public static var isMobile :Bool = CPUArchitecture == "ARM";
+
+ public var stage(default,null):Stage;
+ public var stage3D(default,null):Stage3D;
+ public var context3D(default,null):Context3D;
+ public var contextReady(default, null):Bool;
+ public var contextError(default, null):Signal1<ErrorEvent>;
+
+ public var contextLost(default, null):Signal;
+ public var ready(default,null):Signal;
+ public var resize(default, null):Signal2<Int,Int>;
+ public var requestDraw(default,null):Signal2<Float,Float>;
+
+ public var stageWidth(default, null):Int;
+ public var stageHeight(default, null):Int;
+
+ public var halfStageWidth(default, null):Int;
+ public var halfStageHeight(default, null):Int;
+
+ public var fullViewport(default, null):Rectangle;
+
+
+ public function new(stage:Stage, stage3D:Stage3D = null, renderMode:String="auto", profile:String="baseline") {
+
+ contextReady = false;
+ ready = new Signal();
+ contextLost = new Signal();
+ contextError = new Signal1<ErrorEvent>();
+ resize = new Signal2<Int,Int>();
+ requestDraw = new Signal2<Float,Float>();
+ fullViewport = new Rectangle();
+
+ this.stage = stage;
+ this.stage3D = (stage3D != null) ? stage3D : (stage.stage3Ds.length > 0 ? stage.stage3Ds[0] : null);
+
+ if (this.stage3D == null) {
+ throw "No stage3Ds available.";
+ } else {
+ this.stage.addEventListener(Event.FULLSCREEN, onFullScreen, false, 1000);
+ this.stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextReady, false, 1000);
+ this.stage3D.addEventListener(ErrorEvent.ERROR, onContextError, false, 1000);
+ this.stage3D.requestContext3D(renderMode, profile);
+ }
+ }
+
+ private function onContextError(e:ErrorEvent):Void {
+ contextReady = false;
+ contextError.dispatch(e);
+ }
+
+ private function onContextReady( e:Event ) {
+
+ if (contextReady) { // the previsously initialised context was lost and is now available again... handle it.
+
+ if (context3D.driverInfo.toLowerCase() == "disposed") {
+ trace("Context3D was disposed");
+ } else {
+ trace("Context3D was lost for reasons unknown...");
+ trace("driverInfo: " + context3D.driverInfo);
+ }
+
+ context3D = null;
+
+ // observe the contextLost signal to trigger disosal of your buffers before a new context is ready
+ contextLost.dispatch();
+ }
+
+ context3D = stage3D.context3D;
+
+ stage.addEventListener(Event.RESIZE, onResize);
+ stage.addEventListener(FullScreenEvent.FULL_SCREEN, onResize);
+
+ onResize(null);
+
+ contextReady = true;
+
+ ready.dispatch();
+ }
+
+
+ private function onResize(e:Event):Void {
+
+ if (isMobile) {
+ stageWidth = stage.fullScreenWidth;
+ stageHeight = stage.fullScreenHeight;
+ } else {
+ stageWidth = stage.stageWidth;
+ stageHeight = stage.stageHeight;
+ }
+
+ fullViewport.width = stageWidth;
+ fullViewport.height = stageHeight;
+ halfStageWidth = Std.int(stageWidth * .5);
+ halfStageHeight = Std.int(stageHeight * .5);
+
+ // context3D.setScissorRectangle(new Rectangle(0,0,320,640)); .... Starling interference?
+
+ // set the backbuffer size
+ context3D.enableErrorChecking = Debug;
+ context3D.configureBackBuffer(stageWidth, stageHeight, AA, false);
+
+ // allow alpha blending
+ #if !air
+ context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
+ #end
+
+ context3D.setDepthTest(false, Context3DCompareMode.LESS_EQUAL);
+
+ // don't draw back-faces
+ context3D.setCulling(Context3DTriangleFace.BACK);
+
+ if (e != null) resize.dispatch(stageWidth, stageHeight);
+ }
+
+ private function onFullScreen(e:Event):Void {
+ onResize(null);
+ resize.dispatch(stageWidth, stageHeight);
+ }
+
+ public function update(delta:Float, time:Float) {
+ if (null == stage3D) return;
+
+ context3D.clear();
+
+ requestDraw.dispatch(delta, time);
+
+ context3D.present();
+ }
+
+}

0 comments on commit cc49910

Please sign in to comment.