Skip to content

Commit 4bbaace

Browse files
committed
replaced ETC2 with ASTC textures
1 parent 54ac338 commit 4bbaace

File tree

4 files changed

+137
-111
lines changed

4 files changed

+137
-111
lines changed

src/openfl/display3D/Context3D.hx

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import openfl.display3D.textures.CubeTexture;
1010
import openfl.display3D.textures.RectangleTexture;
1111
import openfl.display3D.textures.TextureBase;
1212
import openfl.display3D.textures.Texture;
13-
import openfl.display3D.textures.ETC2Texture;
13+
import openfl.display3D.textures.ASTCTexture;
1414
import openfl.display3D.textures.VideoTexture;
1515
import openfl.display.BitmapData;
1616
import openfl.display.Stage;
@@ -134,7 +134,7 @@ import lime.math.Vector2;
134134
@:access(openfl.display3D.textures.RectangleTexture)
135135
@:access(openfl.display3D.textures.TextureBase)
136136
@:access(openfl.display3D.textures.Texture)
137-
@:access(openfl.display3D.textures.ETC2Texture)
137+
@:access(openfl.display3D.textures.ASTCTexture)
138138
@:access(openfl.display3D.textures.VideoTexture)
139139
@:access(openfl.display3D.IndexBuffer3D)
140140
@:access(openfl.display3D.Program3D)
@@ -296,7 +296,7 @@ import lime.math.Vector2;
296296
#if (js && html5 && dom)
297297
gl = GL.context;
298298
#else
299-
gl = __context.webgl2;
299+
gl = __context.webgl;
300300
#end
301301

302302
if (__contextState == null) __contextState = new Context3DState();
@@ -622,26 +622,7 @@ import lime.math.Vector2;
622622
var scaledHeight = wantsBestResolution ? height : Std.int(height * __stage.window.scale);
623623
#end
624624
var vertexData = new Vector<Float>([
625-
scaledWidth,
626-
scaledHeight,
627-
0,
628-
1,
629-
1,
630-
0,
631-
scaledHeight,
632-
0,
633-
0,
634-
1,
635-
scaledWidth,
636-
0,
637-
0,
638-
1,
639-
0,
640-
0,
641-
0,
642-
0,
643-
0,
644-
0.0
625+
scaledWidth, scaledHeight, 0, 1, 1, 0, scaledHeight, 0, 0, 1, scaledWidth, 0, 0, 1, 0, 0, 0, 0, 0, 0.0
645626
]);
646627

647628
__stage3D.__vertexBuffer.uploadFromVector(vertexData, 0, 20);
@@ -933,9 +914,9 @@ import lime.math.Vector2;
933914
return new Texture(this, width, height, format, optimizeForRenderToTexture, streamingLevels);
934915
}
935916

936-
public function createETC2Texture(data:ByteArray):ETC2Texture
917+
public function createASTCTexture(data:ByteArray):ASTCTexture
937918
{
938-
return new ETC2Texture(this, data);
919+
return new ASTCTexture(this, data);
939920
}
940921

941922
/**
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package openfl.display3D.textures;
2+
3+
#if !flash
4+
import haxe.io.Bytes;
5+
import openfl.utils._internal.UInt8Array;
6+
import openfl.display.BlendMode;
7+
import openfl.utils.ByteArray;
8+
import openfl.Lib;
9+
10+
/**
11+
The ASTCTexture class represents a 2-dimensional compressed ASTC texture uploaded to a rendering context.
12+
13+
Defines a 2D texture for use during rendering.
14+
15+
ASTCTexture cannot be instantiated directly. Create instances by using Context3D
16+
`createASTCTexture()` method.
17+
**/
18+
#if !openfl_debug
19+
@:fileXml('tags="haxe,release"')
20+
@:noDebug
21+
#end
22+
@:access(openfl.display3D.Context3D)
23+
@:final class ASTCTexture extends TextureBase
24+
{
25+
@:noCompletion private static var __lowMemoryMode:Bool = false;
26+
@:noCompletion private static var __warned:Bool = false;
27+
public static inline final IMAGE_DATA_OFFSET = 16;
28+
29+
public var supported:Bool = true;
30+
public var imageSize(default, null):Int = 0;
31+
public var depth(default, null):Int = 0;
32+
33+
@:noCompletion private function new(context:Context3D, data:ByteArray)
34+
{
35+
super(context);
36+
var gl = __context.gl;
37+
var astcExtension = gl.getExtension("KHR_texture_compression_astc_ldr");
38+
39+
if (astcExtension == null)
40+
{
41+
if (!__warned)
42+
{
43+
Lib.current.stage.window.alert("ASTC compression is not available on this device.", "Rendering Error!");
44+
__warned = true;
45+
}
46+
47+
supported = false;
48+
}
49+
50+
if (supported)
51+
{
52+
var format = astcExtension.COMPRESSED_RGBA_ASTC_4x4_KHR;
53+
__format = format;
54+
__internalFormat = format;
55+
__optimizeForRenderToTexture = false;
56+
__streamingLevels = 0;
57+
58+
__getImageSize(data);
59+
__getImageDimensions(data);
60+
61+
__uploadASTCTextureFromByteArray(data);
62+
}
63+
}
64+
65+
@:noCompletion public function __uploadASTCTextureFromByteArray(data:ByteArray):Void
66+
{
67+
var context = __context;
68+
var gl = context.gl;
69+
70+
__textureTarget = gl.TEXTURE_2D;
71+
__context.__bindGLTexture2D(__textureID);
72+
73+
var bytes:Bytes = cast data;
74+
var textureBytes = new UInt8Array(#if js @:privateAccess bytes.b.buffer #else bytes #end, IMAGE_DATA_OFFSET, imageSize);
75+
gl.compressedTexImage2D(__textureTarget, 0, __internalFormat, __width, __height, 0, textureBytes);
76+
gl.texParameteri(__textureTarget, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
77+
gl.texParameteri(__textureTarget, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
78+
79+
__context.__bindGLTexture2D(null);
80+
}
81+
82+
@:noCompletion private function __getImageDimensions(bytes:ByteArray):Void
83+
{
84+
bytes.position = 7;
85+
86+
__width = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
87+
__height = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
88+
depth = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
89+
}
90+
91+
@:noCompletion private function __getImageSize(bytes:ByteArray):Void
92+
{
93+
bytes.position = 4;
94+
95+
var blockDimX = bytes.readUnsignedByte();
96+
var blockDimY = bytes.readUnsignedByte();
97+
var blockDimZ = bytes.readUnsignedByte();
98+
99+
bytes.position = 7;
100+
101+
var dimX = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
102+
var dimY = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
103+
var dimZ = bytes.readUnsignedByte() | (bytes.readUnsignedByte() << 8) | (bytes.readUnsignedByte() << 16);
104+
105+
var blocksX = Math.ceil(dimX / blockDimX);
106+
var blocksY = Math.ceil(dimY / blockDimY);
107+
var blocksZ = Math.ceil(dimZ / blockDimZ);
108+
109+
var totalBlocks = blocksX * blocksY * blocksZ;
110+
imageSize = totalBlocks * 16;
111+
}
112+
}
113+
#end

src/openfl/display3D/textures/ETC2Texture.hx

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/openfl/utils/Assets.hx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ class Assets
8686
@usage var bitmap = new Bitmap (Assets.getBitmapData ("image.png"));
8787
@param id The ID or asset path for the bitmap
8888
@param useCache (Optional) Whether to allow use of the asset cache (Default: true)
89+
@param allowASTC (Optional) Wether to allow ASTC compressed textures to be used to get this bitmap (Default: true)
8990
@return A new BitmapData object
9091
**/
91-
public static function getBitmapData(id:String, useCache:Bool = true):BitmapData
92+
public static function getBitmapData(id:String, useCache:Bool = true, allowASTC:Bool = true):BitmapData
9293
{
9394
#if (lime && tools && !display)
9495
if (useCache && cache.enabled && cache.hasBitmapData(id))
@@ -101,18 +102,26 @@ class Assets
101102
}
102103
}
103104

104-
final etcTexture:String = haxe.io.Path.withoutExtension(id) + ".ktx";
105-
if (Assets.exists(etcTexture, AssetType.BINARY))
105+
final astcTexture:String = haxe.io.Path.withoutExtension(id) + ".astc";
106+
if (Assets.exists(astcTexture, AssetType.BINARY) && allowASTC)
106107
{
107-
var texture = openfl.Lib.current.stage.context3D.createETC2Texture(Assets.getBytes(etcTexture));
108-
var bitmapData = BitmapData.fromTexture(texture);
108+
var texture = openfl.Lib.current.stage.context3D.createASTCTexture(Assets.getBytes(astcTexture));
109109

110-
if (useCache && cache.enabled)
110+
if (texture.supported)
111111
{
112-
cache.setBitmapData(id, bitmapData);
113-
}
112+
var bitmapData = BitmapData.fromTexture(texture);
114113

115-
return bitmapData;
114+
if (useCache && cache.enabled)
115+
{
116+
cache.setBitmapData(id, bitmapData);
117+
}
118+
119+
return bitmapData;
120+
}
121+
else
122+
{
123+
texture.dispose();
124+
}
116125
}
117126

118127
var image = LimeAssets.getImage(id, false);

0 commit comments

Comments
 (0)