Shader for blitting

Started by Lobby, April 06, 2015, 10:35:51 AM

Previous topic - Next topic

Lobby

Hello, is there a way to set an own shader for blitting?

EgonOlsen

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?

AeroShark333

Using a custom EGLConfigChooser/EGLConfig on GLSurfaceView?

EgonOlsen


Lobby

I hadn't something special in mind. Just thought it would be really useful to render some effects without 3d objects.

AeroShark333

I thought you could manipulate colors (or pixels) with the EGLConfig... but I'm not sure

EgonOlsen

Not as far as i know...the config is just for choosing a config...not for manipulating pixels.

Lobby

Hey Egon, did you forget it?  ;D

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

EgonOlsen

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?

EgonOlsen

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...

Lobby

Thank you, seems to work for me. Here a test where I just desaturate a game:


I used the shaders as following.

Vertex:
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:
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.

EgonOlsen

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?

Lobby

You're right there. Performance tests revealed that blitting is much faster than using 3D objects instead.

EgonOlsen

Well, in that case just sort your blits. That's what any 2d engine would do anyway.

Lobby

#14
Yeah, that's often used for 2d, but it's anything else than trivial :( .