From a8ee9d473e60cbefdc009c3fcea543d7917182ce Mon Sep 17 00:00:00 2001 From: Powei Feng Date: Fri, 19 Dec 2025 14:15:22 -0800 Subject: [PATCH] android: fix texture rendertarget sample for vk (#9528) - add a flag to run the sample using regular, non-external texture as render target. - Add a non-external version of the material - vk: fix y-flipping issue with uvToRenderTargetUV() --- .../filament/texturetarget/MainActivity.kt | 56 +++++++++++++------ .../src/main/materials/textured.mat | 4 +- .../src/main/materials/texturedExternal.mat | 20 +++++++ 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 android/samples/sample-texture-target/src/main/materials/texturedExternal.mat diff --git a/android/samples/sample-texture-target/src/main/java/com/google/android/filament/texturetarget/MainActivity.kt b/android/samples/sample-texture-target/src/main/java/com/google/android/filament/texturetarget/MainActivity.kt index 3065038829..6cee660fa4 100644 --- a/android/samples/sample-texture-target/src/main/java/com/google/android/filament/texturetarget/MainActivity.kt +++ b/android/samples/sample-texture-target/src/main/java/com/google/android/filament/texturetarget/MainActivity.kt @@ -76,11 +76,16 @@ class MainActivity : Activity() { private var texture: Texture? = null private var renderTarget: RenderTarget? = null + private var useExternalTexture = true + private lateinit var offscreenView: View private lateinit var offscreenCamera: Camera override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + // To use set this flag with adb, run + // adb shell am start -n com.google.android.filament.texturetarget/.MainActivity --ez useExternalTexture false + useExternalTexture = intent.getBooleanExtra("useExternalTexture", true) surfaceView = SurfaceView(this) setContentView(surfaceView) choreographer = Choreographer.getInstance() @@ -173,8 +178,15 @@ class MainActivity : Activity() { readUncompressedAsset("materials/baked_color.filamat").let { triangleMaterial = Material.Builder().payload(it, it.remaining()).build(engine) } - readUncompressedAsset("materials/textured.filamat").let { - texturedMaterial = Material.Builder().payload(it, it.remaining()).build(engine) + + if (useExternalTexture) { + readUncompressedAsset("materials/texturedExternal.filamat").let { + texturedMaterial = Material.Builder().payload(it, it.remaining()).build(engine) + } + } else { + readUncompressedAsset("materials/textured.filamat").let { + texturedMaterial = Material.Builder().payload(it, it.remaining()).build(engine) + } } } @@ -385,33 +397,41 @@ class MainActivity : Activity() { texture?.let { engine.destroyTexture(it) } hardwareBuffer?.close() - // Create a new render target. - hardwareBuffer = HardwareBuffer.create(width, height, - HardwareBuffer.RGBA_8888, 1, - HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT) + if (useExternalTexture) { + // Create a new render target. + hardwareBuffer = HardwareBuffer.create(width, height, + HardwareBuffer.RGBA_8888, 1, + HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT) - texture = Texture.Builder() - .width(width) - .height(height) - .usage(Texture.Usage.COLOR_ATTACHMENT or Texture.Usage.SAMPLEABLE) - .sampler(Texture.Sampler.SAMPLER_EXTERNAL) - .format(Texture.InternalFormat.RGBA8) - .external() - .build(engine) + texture = Texture.Builder() + .width(width) + .height(height) + .usage(Texture.Usage.COLOR_ATTACHMENT or Texture.Usage.SAMPLEABLE) + .sampler(Texture.Sampler.SAMPLER_EXTERNAL) + .format(Texture.InternalFormat.RGBA8) + .external() + .build(engine) - texture!!.setExternalImage(engine, hardwareBuffer!!) + texture!!.setExternalImage(engine, hardwareBuffer!!) + } else { + texture = Texture.Builder() + .width(width) + .height(height) + .levels(1) + .usage(Texture.Usage.COLOR_ATTACHMENT or Texture.Usage.SAMPLEABLE) + .format(Texture.InternalFormat.RGBA8) + .build(engine) + } renderTarget = RenderTarget.Builder() .texture(RenderTarget.AttachmentPoint.COLOR, texture!!) .build(engine) offscreenView.renderTarget = renderTarget - // Set the texture on the quad material. texturedMaterial.defaultInstance.setParameter("texture", texture!!, TextureSampler(TextureSampler.MinFilter.LINEAR, TextureSampler.MagFilter.LINEAR, - TextureSampler.WrapMode.CLAMP_TO_EDGE)) - + TextureSampler.WrapMode.CLAMP_TO_EDGE)) FilamentHelper.synchronizePendingFrames(engine) } } diff --git a/android/samples/sample-texture-target/src/main/materials/textured.mat b/android/samples/sample-texture-target/src/main/materials/textured.mat index 5207b78852..2003c0a8f7 100644 --- a/android/samples/sample-texture-target/src/main/materials/textured.mat +++ b/android/samples/sample-texture-target/src/main/materials/textured.mat @@ -3,7 +3,7 @@ material { shadingModel : unlit, parameters : [ { - type : samplerExternal, + type : sampler2d, name : texture } ], @@ -15,6 +15,6 @@ material { fragment { void material(inout MaterialInputs material) { prepareMaterial(material); - material.baseColor = texture(materialParams_texture, getUV0()); + material.baseColor = texture(materialParams_texture, uvToRenderTargetUV(getUV0())); } } diff --git a/android/samples/sample-texture-target/src/main/materials/texturedExternal.mat b/android/samples/sample-texture-target/src/main/materials/texturedExternal.mat new file mode 100644 index 0000000000..1b55900aff --- /dev/null +++ b/android/samples/sample-texture-target/src/main/materials/texturedExternal.mat @@ -0,0 +1,20 @@ +material { + name : textured, + shadingModel : unlit, + parameters : [ + { + type : samplerExternal, + name : texture + } + ], + requires: [ + uv0 + ] +} + +fragment { + void material(inout MaterialInputs material) { + prepareMaterial(material); + material.baseColor = texture(materialParams_texture, uvToRenderTargetUV(getUV0())); + } +}