ShaderAdvanced.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. class TestUniformBuffer extends hxsl.Shader {
  2. static var SRC = {
  3. @param var colorMatrix : Buffer<Vec4,4>;
  4. var pixelColor : Vec4;
  5. function fragment() {
  6. pixelColor *= mat4(colorMatrix[0],colorMatrix[1],colorMatrix[2],colorMatrix[3]);
  7. }
  8. };
  9. }
  10. class TestTextureArray extends hxsl.Shader {
  11. static var SRC = {
  12. @const var COUNT : Int = 3;
  13. @param var time : Float;
  14. @param var textures : Sampler2DArray;
  15. var calculatedUV : Vec2;
  16. var pixelColor : Vec4;
  17. function fragment() {
  18. pixelColor *= textures.get(vec3(calculatedUV, int((calculatedUV.x + calculatedUV.y) * COUNT + time) % COUNT));
  19. }
  20. };
  21. }
  22. class InstancedOffsetShader extends hxsl.Shader {
  23. static var SRC = {
  24. @:import h3d.shader.BaseMesh;
  25. @perInstance(2) @input var offset : Vec2;
  26. function vertex() {
  27. transformedPosition.xy += offset;
  28. transformedPosition.xy += float(instanceID & 1) * vec2(0.2,0.1);
  29. transformedPosition.z += float(instanceID) * 0.01;
  30. pixelColor.r = float(instanceID) / 16.;
  31. pixelColor.g = float(vertexID) / 8.;
  32. }
  33. };
  34. }
  35. class ShaderAdvanced extends hxd.App {
  36. var updates : Array<Float -> Void> = [];
  37. override function init() {
  38. engine.backgroundColor = 0xFF202020;
  39. // various formats read/write
  40. var d = engine.driver;
  41. var values : Map<hxd.PixelFormat,String> = [
  42. R8 => "ff",
  43. RG8 => "ff7f",
  44. RGB8 => "ff7f40",
  45. RGBA => "ff7f4020",
  46. R16F => "003c",
  47. RG16F => "003c0038",
  48. RGB16F => "003c00380034",
  49. RGBA16F => "003c003800340030",
  50. R32F => "0000803f",
  51. RG32F => "0000803f0000003f",
  52. RGB32F => "0000803f0000003f0000803e",
  53. RGBA32F => "0000803f0000003f0000803e0000003e",
  54. SRGB => #if hlsdl "ffba8800" #else "ffbc8900" #end,
  55. SRGB_ALPHA => #if hlsdl "ffba8820" #else "ffbc8920" #end,
  56. RGB10A2 => "ffff0710",
  57. RG11B10UF => "c0031c68",
  58. ];
  59. for( fmt in hxd.PixelFormat.createAll() ) {
  60. if( !d.isSupportedFormat(fmt) ) {
  61. trace("Skipping "+fmt);
  62. continue;
  63. }
  64. try {
  65. var t = new h3d.mat.Texture(1,1,[Target],fmt);
  66. d.setRenderTarget(t);
  67. d.clear(new h3d.Vector(1,0.5,0.25,0.125));
  68. d.setRenderTarget(null);
  69. var pix = t.capturePixels();
  70. var hex = pix.bytes.toHex();
  71. if( values.get(fmt) != hex )
  72. throw hex+" should be "+values.get(fmt);
  73. d.setRenderTarget(t);
  74. d.clear(new h3d.Vector(0,0,0,0));
  75. d.setRenderTarget(null);
  76. t.uploadPixels(pix);
  77. var pix2 = t.capturePixels();
  78. var hex2 = pix2.bytes.toHex();
  79. if( hex != hex2 )
  80. throw hex+" has been uploaded but we get "+hex2;
  81. } catch( e : Dynamic ) {
  82. trace(fmt,e);
  83. }
  84. }
  85. // uniform buffer
  86. var bmp = new h2d.Bitmap(h2d.Tile.fromColor(0xFF0000,128,128),s2d);
  87. var ubuffer = bmp.addShader(new TestUniformBuffer());
  88. ubuffer.colorMatrix = new h3d.Buffer(4,4,[UniformBuffer,Dynamic]);
  89. var hue : Float = 0.;
  90. updates.push(function(dt) {
  91. hue += dt * 6;
  92. var m = new h3d.Matrix();
  93. m.identity();
  94. m.colorHue(hue);
  95. var buf = new hxd.FloatBuffer();
  96. for( v in m.getFloats() )
  97. buf.push(v);
  98. ubuffer.colorMatrix.uploadVector(buf, 0, 4);
  99. });
  100. // texture array
  101. var bmp = new h2d.Bitmap(h2d.Tile.fromColor(0xFFFFFF,128,128), s2d);
  102. var tarr = bmp.addShader(new TestTextureArray());
  103. bmp.x = 128;
  104. updates.push(function(dt) {
  105. tarr.time += dt;
  106. });
  107. tarr.textures = new h3d.mat.TextureArray(1,1,3,[Target]);
  108. tarr.textures.clear(0xFF4040,1,0);
  109. tarr.textures.clear(0x40FF40,1,1);
  110. tarr.textures.clear(0x4040FF,1,2);
  111. // draw instanced
  112. var cube = h3d.prim.Cube.defaultUnitCube();
  113. var prim = new h3d.prim.Instanced();
  114. prim.setMesh(cube);
  115. prim.commands = new h3d.impl.InstanceBuffer();
  116. var icount = cube.triCount() * 3;
  117. prim.commands.setCommand(16, icount);
  118. new h3d.scene.fwd.DirLight(new h3d.Vector(-1,-2,-5),s3d);
  119. new h3d.scene.CameraController(s3d).loadFromCamera();
  120. var buf = new hxd.FloatBuffer();
  121. for( i in 0...16 ) {
  122. buf.push(i * 0.4);
  123. buf.push(i * 0.2);
  124. }
  125. var instanceBuffer = h3d.Buffer.ofFloats(buf,2);
  126. prim.addBuffer("offset",instanceBuffer);
  127. var m = new h3d.scene.Mesh(prim, s3d);
  128. m.material.mainPass.addShader(new InstancedOffsetShader());
  129. m.material.shadows = false;
  130. // 32 bits indices
  131. var bytes = haxe.io.Bytes.alloc(icount * 4);
  132. for( i in 0...icount )
  133. bytes.setInt32(i<<2,i);
  134. var indexes = new h3d.Indexes(icount,true);
  135. indexes.uploadBytes(bytes,0,icount);
  136. prim.indexes = indexes;
  137. }
  138. override function update(dt:Float) {
  139. for( f in updates )
  140. f(dt);
  141. }
  142. static function main() {
  143. new ShaderAdvanced();
  144. }
  145. }