Author Topic: How to set a blit shader?  (Read 6213 times)

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
How to set a blit shader?
« on: March 09, 2016, 02:08:06 am »
How and where should a shader be set for blitting?

I'm using a shader with fb.setBlittingShader but it get's ignored?

I'm trying to do this on an android device.

I'm trying to do a filtering effect kind of like this but it seems to just be ignored.

http://www.jpct.net/forum2/index.php?topic=4355.0

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #1 on: March 09, 2016, 08:49:22 am »
It should work. At least I see no reason, why it shouldn't. Just make sure not to do this:

Code: [Select]
fb.setBlittingShader(...);
fb.blit(...);
fb.setBlittingShader(null);

That doesn't work, because blits are buffered but the shader isn't. If you need to reset the shader in the process, make sure to execute the blits before. You can force this by blitting a dummy blit from some other texture. Like:

Code: [Select]
fb.setBlittingShader(...);
fb.blit(...);
fb.blit(...); // <- Dummy-blit (1x1 transparent pixel with some other texture or something)
fb.setBlittingShader(null);
« Last Edit: March 10, 2016, 07:30:58 pm by EgonOlsen »

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #2 on: March 09, 2016, 11:16:25 pm »
Yeah i was doing exactly what you said i shouldn't do  :P.   Got it working now though  8)

Game boy style pixelated shader.

Partially based on Lobby s here http://www.jpct.net/forum2/index.php?topic=4355.0
and this pixelation shader http://coding-experiments.blogspot.ie/2010/06/pixelation.html



Vertex Shader
Code: [Select]
precision lowp float;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;
attribute vec4 position;
attribute vec2 texture0;
varying  lowp vec2 v_texCoord;
void main() {
v_texCoord  = (textureMatrix * vec4(texture0, 0, 1)).xy;
gl_Position = modelViewProjectionMatrix * position;
}




FragmentShader
Code: [Select]

precision lowp float;

uniform sampler2D textureUnit0;
varying  lowp vec2 v_texCoord;
//pallete
lowp vec3 lighthest  = vec3(0.61,0.74,0.06);
lowp vec3 light      = vec3(0.55,0.67,0.06);
lowp vec3 dark       = vec3(0.19,0.38,0.19);
lowp vec3 darkest    = vec3(0.06,0.22,0.06);

void main() {
    float dx = 0.005859375;//3.0*(1./512.);
    float dy = 0.00390625; //2.*(1./512.);
    vec2 coord = vec2(dx*floor(v_texCoord.x/dx),
                      dy*floor(v_texCoord.y/dy));
    vec4 col = texture2D(textureUnit0, coord);

    float ave = (col.r + col.g +col.b)/3.0;
    vec3 fin;

    if(ave < 0.25){fin =darkest;}
    if(ave >= 0.25){fin =dark;}
    if(ave >= 0.50){fin =light;}
    if(ave >= 0.75){fin =lighthest;}

gl_FragColor= vec4(fin,1.0);
}

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #3 on: March 11, 2016, 01:14:00 pm »
Maybe have to update it though, shader works fine on an elephone p6000 but not on a HTC one.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #4 on: March 11, 2016, 01:19:43 pm »
What does it do wrong on the HTC?

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #5 on: March 11, 2016, 02:41:06 pm »
screen just goes almost completely green and flickers. It doesn't crash. The HTC uses an Adreno GPU , whereas the ellephone p6000 uses a Mali one, that could be the issue.


Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #6 on: March 11, 2016, 02:43:31 pm »
I'll try later again when  i'm at home.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #7 on: March 11, 2016, 02:59:51 pm »
Adreno usually isn't a troublemaker in that regard...strange. My first try would be to up all low precision settings to high or at least medium to see if that helps.

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #8 on: March 16, 2016, 01:14:45 am »
No that doesn't seem to be it. It does cause a weird visual echoing effect though i've noticed of things that should be drawn above it.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #9 on: March 16, 2016, 09:31:04 am »
Sounds like a depth buffer issue of some kind...

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #10 on: April 20, 2016, 04:02:39 pm »
OK tried the app now

On a samsung s6 It works fine .
IT works fine on the elephone p6000

Both of these use Mali chips

On HTC the shader gets messed up , it doesn't crash.
What i have noticed is it causes a repeating effect on some ui elements that should be drawn after the shader had even been used.

This is an Adreno chip, probably 330

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #11 on: April 20, 2016, 08:25:21 pm »
The shader itself looks fine to me. But you never know. Some shader compilers compile some shaders to crap even if there are actually correct.
It's hard to tell...do you have a test case to reproduce it? I've several Adreno based devices as well as PowerVR, Nvidia and Intel based ones to test it on.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #12 on: April 20, 2016, 10:28:10 pm »
Try this jar to see if it changes something: http://jpct.de/download/beta/jpct_ae.jar

There was small problem with setting the blitting shader correctly in some cases, but I'm not sure if it applies here. Anyway, it's worth a try.

Offline lawless_c

  • int
  • **
  • Posts: 96
    • View Profile
Re: How to set a blit shader?
« Reply #13 on: April 22, 2016, 01:24:36 am »
Tried that out , the shader still comes out messy on the adreno device.

However it also seems to get loaded before i chose to use it with this version. so things have a little green tint. which disappears when activate and then deactivate the blitter shader.


I'll work on a simple program to demo it. The shader does run, it just comes out messed up on that one device.
« Last Edit: April 22, 2016, 01:36:36 am by lawless_c »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to set a blit shader?
« Reply #14 on: April 22, 2016, 08:33:00 am »
However it also seems to get loaded before i chose to use it with this version. so things have a little green tint. which disappears when activate and then deactivate the blitter shader.
No, it actually can't. The only place that activates the blitting shader is the blitting...something is fishy here...