diff --git a/Shaders/painter/layer_copy_bgra.frag.glsl b/Shaders/painter/layer_copy_bgra.frag.glsl new file mode 100644 index 00000000..0dcb5dca --- /dev/null +++ b/Shaders/painter/layer_copy_bgra.frag.glsl @@ -0,0 +1,8 @@ +#version 330 +uniform sampler2D tex; +in vec2 texCoord; +in vec4 color; +out vec4 FragColor; +void main() { + FragColor = textureLod(tex, texCoord, 0).bgra * color; +} diff --git a/Sources/arm/Layers.hx b/Sources/arm/Layers.hx index 11c663f9..136ae91a 100644 --- a/Sources/arm/Layers.hx +++ b/Sources/arm/Layers.hx @@ -28,6 +28,7 @@ class Layers { public static var pipeMergeA: PipelineState = null; public static var pipeCopy: PipelineState; public static var pipeCopy8: PipelineState; + public static var pipeCopyBGRA: PipelineState; public static var pipeMask: PipelineState; public static var tex0: TextureUnit; public static var tex1: TextureUnit; @@ -144,6 +145,16 @@ class Layers { pipeCopy.inputLayout = [vs]; pipeCopy.compile(); + pipeCopyBGRA = new PipelineState(); + pipeCopyBGRA.vertexShader = Reflect.field(kha.Shaders, "layer_view_vert"); + pipeCopyBGRA.fragmentShader = Reflect.field(kha.Shaders, "layer_copy_bgra_frag"); + var vs = new VertexStructure(); + vs.add("pos", VertexData.Float3); + vs.add("tex", VertexData.Float2); + vs.add("col", VertexData.Float4); + pipeCopyBGRA.inputLayout = [vs]; + pipeCopyBGRA.compile(); + #if kha_metal pipeCopy8 = new PipelineState(); pipeCopy8.vertexShader = Reflect.field(kha.Shaders, "layer_view_vert"); diff --git a/Sources/arm/ProjectFormat.hx b/Sources/arm/ProjectFormat.hx index 3e207f4a..83fcebda 100644 --- a/Sources/arm/ProjectFormat.hx +++ b/Sources/arm/ProjectFormat.hx @@ -13,6 +13,7 @@ typedef TProjectFormat = { @:optional public var layer_datas: Array; @:optional public var mesh_datas: Array; @:optional public var mesh_assets: Array; + @:optional public var is_bgra: Null; // Swapped red and blue channels for layer textures } typedef TLayerData = { diff --git a/Sources/arm/io/ExportArm.hx b/Sources/arm/io/ExportArm.hx index 972a335e..94919ae3 100644 --- a/Sources/arm/io/ExportArm.hx +++ b/Sources/arm/io/ExportArm.hx @@ -80,7 +80,12 @@ class ExportArm { mesh_datas: md, layer_datas: ld, assets: texture_files, - mesh_assets: mesh_files + mesh_assets: mesh_files, + #if kha_metal + is_bgra: true + #else + is_bgra: false + #end }; var bytes = ArmPack.encode(Project.raw); diff --git a/Sources/arm/io/ImportArm.hx b/Sources/arm/io/ImportArm.hx index abd3f5e8..fc964bd5 100644 --- a/Sources/arm/io/ImportArm.hx +++ b/Sources/arm/io/ImportArm.hx @@ -211,21 +211,21 @@ class ImportArm { // TODO: create render target from bytes var texpaint = Image.fromBytes(Lz4.decode(ld.texpaint, ld.res * ld.res * 4 * bytesPerPixel), ld.res, ld.res, format); l.texpaint.g2.begin(false); - l.texpaint.g2.pipeline = Layers.pipeCopy; + l.texpaint.g2.pipeline = project.is_bgra ? Layers.pipeCopyBGRA : Layers.pipeCopy; l.texpaint.g2.drawImage(texpaint, 0, 0); l.texpaint.g2.pipeline = null; l.texpaint.g2.end(); var texpaint_nor = Image.fromBytes(Lz4.decode(ld.texpaint_nor, ld.res * ld.res * 4 * bytesPerPixel), ld.res, ld.res, format); l.texpaint_nor.g2.begin(false); - l.texpaint_nor.g2.pipeline = Layers.pipeCopy; + l.texpaint_nor.g2.pipeline = project.is_bgra ? Layers.pipeCopyBGRA : Layers.pipeCopy; l.texpaint_nor.g2.drawImage(texpaint_nor, 0, 0); l.texpaint_nor.g2.pipeline = null; l.texpaint_nor.g2.end(); var texpaint_pack = Image.fromBytes(Lz4.decode(ld.texpaint_pack, ld.res * ld.res * 4 * bytesPerPixel), ld.res, ld.res, format); l.texpaint_pack.g2.begin(false); - l.texpaint_pack.g2.pipeline = Layers.pipeCopy; + l.texpaint_pack.g2.pipeline = project.is_bgra ? Layers.pipeCopyBGRA : Layers.pipeCopy; l.texpaint_pack.g2.drawImage(texpaint_pack, 0, 0); l.texpaint_pack.g2.pipeline = null; l.texpaint_pack.g2.end();