Author Topic: Shader for blitting  (Read 2516 times)

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Shader for blitting
« on: April 06, 2015, 10:35:51 am »
Hello, is there a way to set an own shader for blitting?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #1 on: April 07, 2015, 01:21:28 pm »
I think you hack it, but i can't remember how ATM. I'm on holiday, so i can't look it up now. I'll report back next week (please remind me in case that i should forget it).
BTW: What do you need this for?

Offline AeroShark333

  • long
  • ***
  • Posts: 152
    • View Profile
Re: Shader for blitting
« Reply #2 on: April 07, 2015, 07:04:32 pm »
Using a custom EGLConfigChooser/EGLConfig on GLSurfaceView?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #3 on: April 08, 2015, 12:03:08 pm »
What do you mean?

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: Shader for blitting
« Reply #4 on: April 08, 2015, 03:59:36 pm »
I hadn't something special in mind. Just thought it would be really useful to render some effects without 3d objects.

Offline AeroShark333

  • long
  • ***
  • Posts: 152
    • View Profile
Re: Shader for blitting
« Reply #5 on: April 08, 2015, 04:46:00 pm »
I thought you could manipulate colors (or pixels) with the EGLConfig... but I'm not sure

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #6 on: April 08, 2015, 06:00:33 pm »
Not as far as i know...the config is just for choosing a config...not for manipulating pixels.

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: Shader for blitting
« Reply #7 on: April 18, 2015, 06:51:42 pm »
Hey Egon, did you forget it?  ;D

Another thing: May it possible to use z-Testing for blitting?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #8 on: April 24, 2015, 06:57:27 am »
Kind of...it somehow thought that it's a real problem that needs to be solved. Setting a blitting shader can be done but you need some knowledge about the internals to do it and still feels clunky. I'll see if i can add an official way to do it instead.   What do you mean with z-testing in this context?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #9 on: April 24, 2015, 09:18:28 pm »
This might work: http://jpct.de/download/beta/jpct_ae.jar. It adds a new method setBlittingShader(<GLSLShader>) to FrameBuffer. To be honest, i haven't really tested it. I only made sure that it doesn't break anything...

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: Shader for blitting
« Reply #10 on: April 24, 2015, 11:29:31 pm »
Thank you, seems to work for me. Here a test where I just desaturate a game:


I used the shaders as following.

Vertex:
Code: [Select]
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;

uniform vec4 additionalColor;
uniform vec4 ambientColor;

uniform float alpha;

uniform bool useColors;

attribute vec4 position;
attribute vec4 color;
attribute vec2 texture0;

varying vec2 texCoord;
varying vec4 vertexColor;

void main() {

texCoord = (textureMatrix * vec4(texture0, 0, 1)).xy;

vec4 vertexPos = modelViewMatrix * position;
vertexColor = ambientColor + additionalColor;

vertexColor=vec4(min(vec3(1), vertexColor.xyz * color.xyz), alpha);

gl_Position = modelViewProjectionMatrix * position;
}

Fragment:
Code: [Select]
precision mediump float;

uniform sampler2D textureUnit0;

varying vec2 texCoord;
varying vec4 vertexColor;

void main() {
vec4 col = texture2D(textureUnit0, texCoord) * vertexColor;

    float grey = 0.3 * col.r + 0.6 * col.g + 0.1 * col.b;

col = vec4(vec3(grey), col.a);

gl_FragColor=col;
}

Which shader code is used for blitting by default? Maybe you can also provide it's source if it contains some useful optimizations.


Z-Testing in this context means that one can set a depth value for each blit and only fragments with smaller depth value should be drawn. I could need this to solve some issues with drawing the cars. I always have to draw them in the right order now to avoid wrong overlapping.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #11 on: April 25, 2015, 10:02:41 am »
The blitting doesn't use a special shader. It relies on the basic logic that jPCT-AE uses to pick one of the default shaders. In case of blitting, this will most likely be the most simple one (i.e. one texture stage, no light sources, no fogging).
About the depth test...that's not possible by design. Blits have no real depth, they sit on top of everything. From the looks of it, the whole game isn't real 3D...everything is blitted, isn't it?

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: Shader for blitting
« Reply #12 on: April 25, 2015, 03:40:46 pm »
You're right there. Performance tests revealed that blitting is much faster than using 3D objects instead.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Shader for blitting
« Reply #13 on: April 25, 2015, 11:56:23 pm »
Well, in that case just sort your blits. That's what any 2d engine would do anyway.

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: Shader for blitting
« Reply #14 on: April 26, 2015, 12:52:23 pm »
Yeah, that's often used for 2d, but it's anything else than trivial :( .
« Last Edit: April 26, 2015, 01:08:10 pm by Lobby »