2

I'm trying to use custom shaders for a libgdx project to run on Android. I have not figured out how to USB debug, so I can't seem to get any logs when running on a real Android device.

The problem is that implementing the shader I wrote results in a black screen on my Samsung Galaxy Nexus. It produces the correct results in the desktop project AND the Android project run on an Android emulator in Eclipse. Not implementing the shader makes it also render right on my Android (albeit without the shader effect).

So I'm presenting the issue hoping someone else can spot any problem(s). Best regards.

The shader setup (in Java):

ShaderProgram.pedantic = false;
shader = new ShaderProgram(
    Gdx.files.internal("shaders/vignette.vsh"),
    Gdx.files.internal("shaders/vignette.fsh"));

if (! shader.isCompiled()) {
    Gdx.app.log("Shader", shader.getLog());
}

renderer.getSpriteBatch().setShader(shader);

glow.batch.setShader(shader);

shader.begin();
shader.setUniformf("u_near", .2f);
shader.setUniformf("u_dim", .85f);
shader.end();

The vertex shader:

precision mediump float;

attribute vec4 a_color;
attribute vec3 a_position;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoord0;

void main() {
    v_color = a_color;
    v_texCoord0 = a_texCoord0;
    gl_Position = u_projTrans * vec4(a_position, 1.0);
}

Fragment shader:

precision mediump float;

varying vec4 v_color;
varying vec2 v_texCoord0;

uniform vec2 u_lightPos;
uniform float u_radius;
uniform float u_near;
uniform float u_dim;
uniform sampler2D u_sampler2D;

void main() {
    vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;

    vec2 relativePosition = (u_lightPos - gl_FragCoord.xy) / u_radius;
    float len = length(relativePosition);
    float vignette = smoothstep(.5, u_near, len);
    color.rgb = mix(color.rgb, color.rgb * vec3(vignette, vignette, color.b / 3), u_dim);

    gl_FragColor = color;
}

The correct result is a "vignette" or spotlight effect.

Target Android version: 4.4.2

MainActivity contains

    cfg.useGL20 = true;

AndroidManifest contains

    <uses-feature android:glEsVersion="0x00020000" android:required="true" />

Edit: I have set up the projects correctly so that they share assets located in an assets folder in the Android project, including the shader files.

Fisk42
  • 31
  • 1
  • 7
  • 2
    I solved my problem with this great shader validator http://shdr.bkcore.com/. I will answer my question in 8 hours. It is solved. Apologies for a redundant question. – Fisk42 Jan 14 '14 at 18:05

1 Answers1

1

I solved my problem with this http://shdr.bkcore.com/. I also learned that OpenGL ES shading language is different from GLSL in some regards.

My problem was in my fragment shader. The working shader is thus:

#ifdef GL_ES
precision highp float;
#endif

varying vec4 v_color;
varying vec2 v_texCoord0;

uniform vec2 u_lightPos;
uniform float u_radius;
uniform float u_near;
uniform float u_dim;
uniform sampler2D u_sampler2D;

void main() {
  vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;

    vec2 relativePosition = (u_lightPos - gl_FragCoord.xy) / u_radius;
    float len = length(relativePosition);
    float vignette = smoothstep(.5, u_near, len);
  color.rgb = mix(color.rgb, color.rgb * vec3(vignette, vignette, color.b / 3.), u_dim);
    // "color.b / 3." instead of "color.b / 3"

    gl_FragColor = color;
}

Very basic mistakes I think. Defining LOWP (what ever that means? - the topmost part) is specific to OpenGL ES. I had also written "3" instead of "3." where I wanted to express a float value. (No implicit typecasting here, so it would be taken for an integer, which you can't divide a float by.) It's my first time with GLSL.

Fisk42
  • 31
  • 1
  • 7
  • 1
    See http://stackoverflow.com/questions/13780609/what-does-precision-mediump-float-mean for details on precision (`highp`, `lowp`, `mediump`, etc). More than just GLSL vs GLSL ES, each GPU might make different choices in implementing the spec. Adding a `#version 100` (see https://github.com/mattdesl/lwjgl-basics/wiki/GLSL-Versions) will get a bit more consistency from GPUs – P.T. Jan 16 '14 at 08:38