Haxe3+HxSL2+Flash11: drawTriangles fails after re-creating Context3D

90 views
Skip to first unread message

Motoshi Kamiya

unread,
Mar 6, 2013, 2:32:20 AM3/6/13
to haxe...@googlegroups.com
Hi everybody,

I met a small problem with Haxe3+HxSL2. (Haxe 3.0.0 r6371 and the latest git version of hxsl)

At first view, "drawTriangles" works well as in haxe-2.10+format-1.25 environment.  But once Context3D is disposed and re-created in Haxe3+HxSL2 env., "drawTriangles" always fails (throws error #3600; setProgram related?) and no triangles are displayed.  I confirm CREATE_CONTEXT3D event is dispatched after "dispose", new Context3D seems to be successfully re-created, and the background color specified by "clear" is ok.  I also confirm equivalent code by haxe-2.10+format-1.25 works fine.

What is wrong...?

Thanks, 

// test code (modified version of the hxsl sample excluding some common classes).
class Shader extends hxsl.Shader {
  static var SRC = {
    var input : {
      pos : Float3,
    };
    var color : Float3;
    function vertex( mpos : M44, mproj : M44 ) {
      out = input.pos.xyzw * mpos * mproj;
      color = input.pos;
    }
    function fragment() {
      out = color.xyzw;
    }
  };
}

class Test {
  var stage : flash.display.Stage;
  var s : flash.display.Stage3D;
  var c : flash.display3D.Context3D;
  var shader : Shader;
  var pol : Polygon;
  var t : Float;
  var camera : Camera;

  function new() {
    stage = flash.Lib.current.stage;
    s = stage.stage3Ds[0];
    s.addEventListener( flash.events.Event.CONTEXT3D_CREATE, onReady );
    stage.addEventListener( flash.events.KeyboardEvent.KEY_DOWN, onKey );
    flash.Lib.current.addEventListener( flash.events.Event.ENTER_FRAME, update );
    s.requestContext3D();
  }

  function onKey( e : flash.events.KeyboardEvent ) {
    c.dispose();
  }

  function onReady( _ ) {
    //trace( "context3D created." );
    t = 0;
    c = s.context3D;
    c.enableErrorChecking = true;
    c.configureBackBuffer( stage.stageWidth, stage.stageHeight, 0, true );

    shader = new Shader();
    camera = new Camera();

    pol = new Cube();
    pol.alloc( c );
  }

  function update(_) {
    if ( c == null ) return;

    t += 0.01;

    c.clear( 0, 0, 0, 1 );
    c.setDepthTest( true, flash.display3D.Context3DCompareMode.LESS_EQUAL );
    c.setCulling( flash.display3D.Context3DTriangleFace.BACK );
    camera.update();
    var project = camera.m.toMatrix();

    var mpos = new flash.geom.Matrix3D();
    mpos.appendRotation( t * 10, flash.geom.Vector3D.Z_AXIS );

    shader.mpos = mpos;
    shader.mproj = project;
    shader.bind( c, pol.vbuf );
    try {
      c.drawTriangles( pol.ibuf );
    } catch( e:flash.errors.Error ) {
      //trace( e.message ); // catch Error #3600 after dispose()
    }
    shader.unbind( c );
    c.present();
  }

  static function main() {
    haxe.Log.setColor( 0xFF0000 );
    var inst = new Test();
  }
}

Nicolas Cannasse

unread,
Mar 6, 2013, 3:46:22 AM3/6/13
to haxe...@googlegroups.com
Le 06/03/2013 08:32, Motoshi Kamiya a �crit :
> Hi everybody,
>
> I met a small problem with Haxe3+HxSL2. (Haxe 3.0.0 r6371 and the latest
> git version of hxsl)
>
> At first view, "drawTriangles" works well as in haxe-2.10+format-1.25
> environment. But once Context3D is disposed and re-created in
> Haxe3+HxSL2 env., "drawTriangles" always fails (throws error #3600;
> setProgram related?) and no triangles are displayed. I confirm
> CREATE_CONTEXT3D event is dispatched after "dispose", new Context3D
> seems to be successfully re-created, and the background color specified
> by "clear" is ok. I also confirm equivalent code by
> haxe-2.10+format-1.25 works fine.

Hi,

That's due to the shader caching mechanism of HxSL2.

We don't add yet a dispose() method but it will be done soon. You can
now add a method that does the following :

@:access(hxsl.ShaderGlobals)
public function dispose() {
globals.instances = new Map();
instance = null;
}

Best,
Nicolas



Motoshi Kamiya

unread,
Mar 6, 2013, 5:06:42 AM3/6/13
to haxe...@googlegroups.com
Thanks! :D
Reply all
Reply to author
Forward
0 new messages