appspell/ShaderView

Strange behavior: doesn't work in real project

gmikhail opened this issue · 1 comments

Everything works in an empty project, but does not work when trying to add ShaderView to a real project. My goal is simple - display a bitmap with shader effects and dynamically change it. But for some reason, I can't even display the bitmap.

My layout:

  <com.appspell.shaderview.ShaderView
      android:id="@+id/shaderView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

My code:

// Called when bitmap loaded
binding.shaderView.apply {
            fragmentShaderRawResId = R.raw.fragment_texture_shader
            shaderParams = ShaderParamsBuilder()
                .addTexture2D("uTexture", bitmap, GLES30.GL_TEXTURE0)
                .build()
        }

My shader:

#version 300 es
precision mediump float;

uniform sampler2D uTexture;

in vec2 textureCoord;
out vec4 fragColor;

void main() {
   fragColor = texture(uTexture, textureCoord);
}

Result - white ShaderView.

I tried:

  • Set updateContinuously = true
  • Set debugMode = true (no logs messages at all)
  • Use drawable instead bitmap
  • Create ShaderView programmatically
  • Use shaderView.shaderParams.updateValue2D()
  • Set in manifest android:hardwareAccelerated="true"

Nothing works. Maybe there are some unobvious restrictions? The best that I managed to achieve was to display drawable (png) if ShaderView is initialized immediately in onCreate.

As I found it is not possible to declare ShaderView in XML and then set bitmap in code when bitmap is loaded with a delay.

You need to immediately initialize ShaderView in onCreate or create it programmatically after bitmap is available.

A good example of initialization after loading an image:
https://qiita.com/tkhskt/items/3cf7e411d77c8b5a8812
https://github.com/tkhskt/ShaderViewSample

It also turned out that my bitmap is loaded with HARDWARE config and it needs to be converted to ARGB_8888 config to work correctly.