From 89ebd9e7905bb9e79f523c823440f0f7de490ab5 Mon Sep 17 00:00:00 2001 From: PurSnake Date: Thu, 18 Sep 2025 23:29:09 +0300 Subject: [PATCH 1/4] Update Assets.hx --- src/openfl/utils/Assets.hx | 55 +++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/openfl/utils/Assets.hx b/src/openfl/utils/Assets.hx index e40857e629..8367f86675 100644 --- a/src/openfl/utils/Assets.hx +++ b/src/openfl/utils/Assets.hx @@ -49,6 +49,8 @@ import lime.media.vorbis.VorbisFile; @:access(openfl.utils.AssetLibrary) class Assets { + public static var allowGPU:Bool = #if desktop true #else false #end; + public static var cache:IAssetCache = new AssetCache(); @:noCompletion private static var dispatcher:EventDispatcher #if !macro = new EventDispatcher() #end; @@ -114,18 +116,21 @@ class Assets @param id The ID or asset path for the bitmap @param useCache (Optional) Whether to allow use of the asset cache (Default: true) @param allowCompressedTextures (Optional) Wether to allow compressed textures to be used to get this bitmap (Default: true) + @param pushToGPU Whenever the image should be immediately pushed to GPU. + We wont load graphic to GPU, if it Compressed. @return A new BitmapData object @see [Working with bitmap assets](https://books.openfl.org/openfl-developers-guide/working-with-bitmaps/working-with-bitmap-assets.html) **/ - public static function getBitmapData(id:String, useCache:Bool = true, allowCompressedTextures:Bool = true):BitmapData + @:access(openfl.display.BitmapData) + public static function getBitmapData(id:String, useCache:Bool = true, allowCompressedTextures:Bool = true, pushToGPU:Bool = true):BitmapData { #if (lime && tools && !display) if (useCache && cache.enabled && cache.hasBitmapData(id)) { var bitmapData = cache.getBitmapData(id); - if (isValidBitmapData(bitmapData)) + if (isValidBitmapData(bitmapData) && bitmapData.readable) { return bitmapData; } @@ -162,7 +167,23 @@ class Assets #if flash var bitmapData = image.src; #else - var bitmapData = BitmapData.fromImage(image); + var bitmapData:BitmapData = BitmapData.fromImage(image); + #if (!macro && desktop) + if (pushToGPU && Assets.allowGPU) + { + bitmapData.lock(); + if (bitmapData.__texture == null) + { + bitmapData.image.premultiplied = true; + bitmapData.getTexture(flixel.FlxG.stage.context3D); + } + bitmapData.__surface ??= lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); + + bitmapData.readable = true; + bitmapData.image.data = null; + bitmapData.unlock(); + } + #end #end if (useCache && cache.enabled) @@ -507,8 +528,7 @@ class Assets return false; } #else - return (bitmapData != null - && #if !lime_hybrid (bitmapData.image != null || bitmapData.__texture != null) #else bitmapData.__handle != null #end); + return (bitmapData != null && #if !lime_hybrid bitmapData.image != null #else bitmapData.__handle != null #end); #end #else return true; @@ -548,11 +568,13 @@ class Assets @param id The ID or asset path for the asset @param useCache (Optional) Whether to allow use of the asset cache (Default: true) + @param pushToGPU Whenever the image should be immediately pushed to GPU. @return Returns a Future @see [Working with bitmap assets](https://books.openfl.org/openfl-developers-guide/working-with-bitmaps/working-with-bitmap-assets.html) **/ - public static function loadBitmapData(id:String, useCache:Null = true):Future + @:access(openfl.display.BitmapData) + public static function loadBitmapData(id:String, useCache:Null = true, pushToGPU:Bool = true):Future { if (useCache == null) useCache = true; @@ -563,7 +585,7 @@ class Assets { var bitmapData = cache.getBitmapData(id); - if (isValidBitmapData(bitmapData)) + if (isValidBitmapData(bitmapData) && (pushToGPU || bitmapData.readable)) { promise.complete(bitmapData); return promise.future; @@ -578,6 +600,22 @@ class Assets var bitmapData = image.src; #else var bitmapData = BitmapData.fromImage(image); + #if (!macro && desktop) + if (pushToGPU && Assets.allowGPU) + { + bitmapData.lock(); + if (bitmapData.__texture == null) + { + bitmapData.image.premultiplied = true; + bitmapData.getTexture(flixel.FlxG.stage.context3D); + } + if (bitmapData.__surface == null) bitmapData.__surface = lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); + + bitmapData.readable = true; + bitmapData.image.data = null; + bitmapData.unlock(); + } + #end #end if (useCache && cache.enabled) @@ -885,8 +923,7 @@ class Assets public static function loadText(id:String):Future { #if lime - var future = LimeAssets.loadText(id); - return future; + return LimeAssets.loadText(id); #else return Future.withValue(getText(id)); #end From 0a68007c3e8cc09c9f960dbe11ff2b086843664b Mon Sep 17 00:00:00 2001 From: Herman Khomich <56314743+PurSnake@users.noreply.github.com> Date: Fri, 19 Sep 2025 10:33:47 +0300 Subject: [PATCH 2/4] Oopps,OpenFl,not Flixel! --- src/openfl/utils/Assets.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openfl/utils/Assets.hx b/src/openfl/utils/Assets.hx index 8367f86675..82b73dfb9a 100644 --- a/src/openfl/utils/Assets.hx +++ b/src/openfl/utils/Assets.hx @@ -175,7 +175,7 @@ class Assets if (bitmapData.__texture == null) { bitmapData.image.premultiplied = true; - bitmapData.getTexture(flixel.FlxG.stage.context3D); + bitmapData.getTexture(openfl.Lib.current.stage.context3D); } bitmapData.__surface ??= lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); @@ -607,7 +607,7 @@ class Assets if (bitmapData.__texture == null) { bitmapData.image.premultiplied = true; - bitmapData.getTexture(flixel.FlxG.stage.context3D); + bitmapData.getTexture(openfl.Lib.current.stage.context3D); } if (bitmapData.__surface == null) bitmapData.__surface = lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); From 66fdd848ebf6412a59eda9cfe833392b7c5bc7d9 Mon Sep 17 00:00:00 2001 From: PurSnake Date: Fri, 19 Sep 2025 14:37:47 +0300 Subject: [PATCH 3/4] revert this line --- src/openfl/utils/Assets.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openfl/utils/Assets.hx b/src/openfl/utils/Assets.hx index 82b73dfb9a..06ec6cbfe0 100644 --- a/src/openfl/utils/Assets.hx +++ b/src/openfl/utils/Assets.hx @@ -528,7 +528,7 @@ class Assets return false; } #else - return (bitmapData != null && #if !lime_hybrid bitmapData.image != null #else bitmapData.__handle != null #end); + return (bitmapData != null && #if !lime_hybrid (bitmapData.image != null || bitmapData.__texture != null) #else bitmapData.__handle != null #end); #end #else return true; From 9302aeb05a59c9b6b095fe59a128582a97825de5 Mon Sep 17 00:00:00 2001 From: PurSnake Date: Fri, 19 Sep 2025 22:29:51 +0300 Subject: [PATCH 4/4] Polish everything Co-Authored-By: Mihai Alexandru <77043862+majigsaw77@users.noreply.github.com> --- src/openfl/utils/Assets.hx | 65 +++++++++++++++----------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/src/openfl/utils/Assets.hx b/src/openfl/utils/Assets.hx index 06ec6cbfe0..5ea640911c 100644 --- a/src/openfl/utils/Assets.hx +++ b/src/openfl/utils/Assets.hx @@ -49,7 +49,9 @@ import lime.media.vorbis.VorbisFile; @:access(openfl.utils.AssetLibrary) class Assets { - public static var allowGPU:Bool = #if desktop true #else false #end; + public static var allowCompressedTextures:Bool = true; + + public static var allowUncompressedTextures:Bool = #if desktop true #else false #end; public static var cache:IAssetCache = new AssetCache(); @@ -84,7 +86,7 @@ class Assets { #if lime #if !flash - if (allowCompressedTextures) + if (allowCompressedTextures && Assets.allowCompressedTextures) { if (id != null && haxe.io.Path.extension(id) == "png") { @@ -116,28 +118,27 @@ class Assets @param id The ID or asset path for the bitmap @param useCache (Optional) Whether to allow use of the asset cache (Default: true) @param allowCompressedTextures (Optional) Wether to allow compressed textures to be used to get this bitmap (Default: true) - @param pushToGPU Whenever the image should be immediately pushed to GPU. + @param allowUncompressedTextures (Optional) Wether to allow uncompressed textures to be used (Default: true) We wont load graphic to GPU, if it Compressed. @return A new BitmapData object @see [Working with bitmap assets](https://books.openfl.org/openfl-developers-guide/working-with-bitmaps/working-with-bitmap-assets.html) **/ - @:access(openfl.display.BitmapData) - public static function getBitmapData(id:String, useCache:Bool = true, allowCompressedTextures:Bool = true, pushToGPU:Bool = true):BitmapData + public static function getBitmapData(id:String, useCache:Bool = true, allowCompressedTextures:Bool = true, allowUncompressedTextures:Bool = true):BitmapData { #if (lime && tools && !display) if (useCache && cache.enabled && cache.hasBitmapData(id)) { var bitmapData = cache.getBitmapData(id); - if (isValidBitmapData(bitmapData) && bitmapData.readable) + if (isValidBitmapData(bitmapData)) { return bitmapData; } } #if !flash - if (allowCompressedTextures || haxe.io.Path.extension(id) == "astc") + if ((allowCompressedTextures && Assets.allowCompressedTextures) || haxe.io.Path.extension(id) == "astc") { final astcTexture:String = haxe.io.Path.withExtension(id, "astc"); @@ -167,24 +168,17 @@ class Assets #if flash var bitmapData = image.src; #else - var bitmapData:BitmapData = BitmapData.fromImage(image); - #if (!macro && desktop) - if (pushToGPU && Assets.allowGPU) + var bitmapData = BitmapData.fromImage(image); + + if (allowUncompressedTextures && Assets.allowUncompressedTextures) { - bitmapData.lock(); - if (bitmapData.__texture == null) - { - bitmapData.image.premultiplied = true; - bitmapData.getTexture(openfl.Lib.current.stage.context3D); - } - bitmapData.__surface ??= lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); + var uncompressedTexture = openfl.Lib.current.stage.context3D.createTexture(bitmapData.width, bitmapData.height, BGRA, false); + + uncompressedTexture.uploadFromBitmapData(bitmapData); - bitmapData.readable = true; - bitmapData.image.data = null; - bitmapData.unlock(); + bitmapData = BitmapData.fromTexture(uncompressedTexture, false); } #end - #end if (useCache && cache.enabled) { @@ -528,7 +522,8 @@ class Assets return false; } #else - return (bitmapData != null && #if !lime_hybrid (bitmapData.image != null || bitmapData.__texture != null) #else bitmapData.__handle != null #end); + return (bitmapData != null + && #if !lime_hybrid (bitmapData.image != null || bitmapData.__texture != null) #else bitmapData.__handle != null #end); #end #else return true; @@ -568,13 +563,12 @@ class Assets @param id The ID or asset path for the asset @param useCache (Optional) Whether to allow use of the asset cache (Default: true) - @param pushToGPU Whenever the image should be immediately pushed to GPU. + @param allowUncompressedTextures (Optional) Wether to allow uncompressed textures to be used (Default: true) @return Returns a Future @see [Working with bitmap assets](https://books.openfl.org/openfl-developers-guide/working-with-bitmaps/working-with-bitmap-assets.html) **/ - @:access(openfl.display.BitmapData) - public static function loadBitmapData(id:String, useCache:Null = true, pushToGPU:Bool = true):Future + public static function loadBitmapData(id:String, useCache:Null = true, allowUncompressedTextures:Bool = true):Future { if (useCache == null) useCache = true; @@ -585,7 +579,7 @@ class Assets { var bitmapData = cache.getBitmapData(id); - if (isValidBitmapData(bitmapData) && (pushToGPU || bitmapData.readable)) + if (isValidBitmapData(bitmapData)) { promise.complete(bitmapData); return promise.future; @@ -600,23 +594,16 @@ class Assets var bitmapData = image.src; #else var bitmapData = BitmapData.fromImage(image); - #if (!macro && desktop) - if (pushToGPU && Assets.allowGPU) + + if (allowUncompressedTextures && Assets.allowUncompressedTextures) { - bitmapData.lock(); - if (bitmapData.__texture == null) - { - bitmapData.image.premultiplied = true; - bitmapData.getTexture(openfl.Lib.current.stage.context3D); - } - if (bitmapData.__surface == null) bitmapData.__surface = lime.graphics.cairo.CairoImageSurface.fromImage(bitmapData.image); + var uncompressedTexture = openfl.Lib.current.stage.context3D.createTexture(bitmapData.width, bitmapData.height, BGRA, false); - bitmapData.readable = true; - bitmapData.image.data = null; - bitmapData.unlock(); + uncompressedTexture.uploadFromBitmapData(bitmapData); + + bitmapData = BitmapData.fromTexture(uncompressedTexture, false); } #end - #end if (useCache && cache.enabled) {