2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2012-11-26 03:25:14 +00:00
|
|
|
#if defined(USING_GLES2)
|
2012-11-01 15:19:01 +00:00
|
|
|
#define GLSL_ES_1_0
|
|
|
|
#else
|
|
|
|
#define GLSL_1_3
|
2012-12-02 04:48:00 +00:00
|
|
|
|
|
|
|
// SDL 1.2 on Apple does not have support for OpenGL 3 and hence needs
|
|
|
|
// special treatment in the shader generator.
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define FORCE_OPENGL_2_0
|
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "FragmentShaderGenerator.h"
|
|
|
|
#include "../ge_constants.h"
|
|
|
|
#include "../GPUState.h"
|
2012-11-03 02:33:24 +00:00
|
|
|
#include <cstdio>
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#define WRITE p+=sprintf
|
|
|
|
|
2013-01-22 21:27:03 +00:00
|
|
|
// #define DEBUG_SHADER
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// GL_NV_shader_framebuffer_fetch looks interesting....
|
|
|
|
|
2013-03-11 17:43:36 +00:00
|
|
|
static bool IsAlphaTestTriviallyTrue() {
|
|
|
|
int alphaTestFunc = gstate.alphatest & 7;
|
|
|
|
int alphaTestRef = (gstate.alphatest >> 8) & 0xFF;
|
|
|
|
|
|
|
|
switch (alphaTestFunc) {
|
|
|
|
case GE_COMP_ALWAYS:
|
|
|
|
return true;
|
|
|
|
case GE_COMP_GEQUAL:
|
|
|
|
if (alphaTestRef == 0)
|
|
|
|
return true;
|
2013-03-11 19:26:04 +00:00
|
|
|
|
|
|
|
// This breaks the trees in MotoGP, for example.
|
|
|
|
// case GE_COMP_GREATER:
|
|
|
|
//if (alphaTestRef == 0 && (gstate.alphaBlendEnable & 1) && gstate.getBlendFuncA() == GE_SRCBLEND_SRCALPHA && gstate.getBlendFuncB() == GE_SRCBLEND_INVSRCALPHA)
|
|
|
|
// return true;
|
|
|
|
|
2013-03-11 17:43:36 +00:00
|
|
|
case GE_COMP_LEQUAL:
|
|
|
|
if (alphaTestRef == 255)
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Here we must take all the bits of the gstate that determine what the fragment shader will
|
|
|
|
// look like, and concatenate them together into an ID.
|
2013-03-11 17:43:36 +00:00
|
|
|
void ComputeFragmentShaderID(FragmentShaderID *id) {
|
2012-11-01 15:19:01 +00:00
|
|
|
memset(&id->d[0], 0, sizeof(id->d));
|
2013-01-22 20:57:47 +00:00
|
|
|
bool enableFog = gstate.isFogEnabled() && !gstate.isModeThrough() && !gstate.isModeClear();
|
2013-03-27 10:21:02 +00:00
|
|
|
bool enableAlphaTest = gstate.isAlphaTestEnabled() && !IsAlphaTestTriviallyTrue();
|
|
|
|
bool enableColorTest = gstate.isColorTestEnabled();
|
|
|
|
int lmode = (gstate.lmode & 1) && gstate.isLightingEnabled();
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
if (gstate.clearmode & 1) {
|
|
|
|
// We only need one clear shader, so let's ignore the rest of the bits.
|
|
|
|
id->d[0] = 1;
|
|
|
|
} else {
|
|
|
|
// id->d[0] |= (gstate.clearmode & 1);
|
2013-03-27 10:21:02 +00:00
|
|
|
if (gstate.isTextureMapEnabled()) {
|
2013-01-22 20:57:47 +00:00
|
|
|
id->d[0] |= 1 << 1;
|
|
|
|
id->d[0] |= (gstate.texfunc & 0x7) << 2;
|
|
|
|
id->d[0] |= ((gstate.texfunc & 0x100) >> 8) << 5; // rgb or rgba
|
|
|
|
id->d[0] |= ((gstate.texfunc & 0x10000) >> 16) << 6; // color double
|
|
|
|
}
|
2013-01-25 13:15:56 +00:00
|
|
|
id->d[0] |= (lmode & 1) << 7;
|
2013-03-27 10:21:02 +00:00
|
|
|
id->d[0] |= gstate.isAlphaTestEnabled() << 8;
|
2013-03-11 17:43:36 +00:00
|
|
|
if (enableAlphaTest)
|
2013-01-22 20:57:47 +00:00
|
|
|
id->d[0] |= (gstate.alphatest & 0x7) << 9; // alpha test func
|
2013-03-27 10:21:02 +00:00
|
|
|
id->d[0] |= gstate.isColorTestEnabled() << 12;
|
2013-03-11 17:43:36 +00:00
|
|
|
if (enableColorTest)
|
|
|
|
id->d[0] |= (gstate.colortest & 0x3) << 13; // color test func
|
2013-01-22 20:57:47 +00:00
|
|
|
id->d[0] |= (enableFog & 1) << 15;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Missing: Alpha test, color test, Z depth range, fog
|
|
|
|
// Also, logic ops etc, of course. Urgh.
|
2012-11-28 12:45:22 +00:00
|
|
|
// We could do all this with booleans, but I don't trust the shader compilers on
|
|
|
|
// Android devices to be anything but stupid.
|
2013-03-11 17:43:36 +00:00
|
|
|
void GenerateFragmentShader(char *buffer) {
|
2012-11-01 15:19:01 +00:00
|
|
|
char *p = buffer;
|
2013-01-22 21:27:03 +00:00
|
|
|
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
#if defined(GLSL_ES_1_0)
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, "precision lowp float;\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
#elif !defined(FORCE_OPENGL_2_0)
|
2013-01-07 09:07:00 +00:00
|
|
|
WRITE(p, "#version 110\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-03-27 10:21:02 +00:00
|
|
|
int lmode = (gstate.lmode & 1) && gstate.isLightingEnabled();
|
|
|
|
int doTexture = gstate.isTextureMapEnabled() && !gstate.isModeClear();
|
2013-03-25 14:57:40 +00:00
|
|
|
|
2013-01-14 19:43:18 +00:00
|
|
|
bool enableFog = gstate.isFogEnabled() && !gstate.isModeThrough() && !gstate.isModeClear();
|
2013-03-27 10:21:02 +00:00
|
|
|
bool enableAlphaTest = gstate.isAlphaTestEnabled() && !gstate.isModeClear() && !IsAlphaTestTriviallyTrue();
|
|
|
|
bool enableColorTest = gstate.isColorTestEnabled() && !gstate.isModeClear();
|
2013-03-03 12:00:21 +00:00
|
|
|
bool enableColorDoubling = (gstate.texfunc & 0x10000) != 0;
|
2013-02-05 00:37:00 +00:00
|
|
|
|
2012-12-05 03:45:28 +00:00
|
|
|
|
|
|
|
if (doTexture)
|
2012-11-01 15:19:01 +00:00
|
|
|
WRITE(p, "uniform sampler2D tex;\n");
|
2013-03-25 14:57:40 +00:00
|
|
|
|
2013-02-05 00:37:00 +00:00
|
|
|
if (enableAlphaTest || enableColorTest) {
|
2012-12-19 17:35:37 +00:00
|
|
|
WRITE(p, "uniform vec4 u_alphacolorref;\n");
|
2013-02-24 21:42:23 +00:00
|
|
|
WRITE(p, "uniform vec4 u_colormask;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2013-03-27 10:21:02 +00:00
|
|
|
if (gstate.isTextureMapEnabled())
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, "uniform vec3 u_texenv;\n");
|
2013-03-25 14:57:40 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
WRITE(p, "varying vec4 v_color0;\n");
|
2012-11-09 16:51:45 +00:00
|
|
|
if (lmode)
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "varying vec3 v_color1;\n");
|
2013-01-14 19:43:18 +00:00
|
|
|
if (enableFog) {
|
|
|
|
WRITE(p, "uniform vec3 u_fogcolor;\n");
|
2013-02-05 00:37:00 +00:00
|
|
|
#if defined(GLSL_ES_1_0)
|
|
|
|
WRITE(p, "varying mediump float v_fogdepth;\n");
|
|
|
|
#else
|
2013-01-14 19:43:18 +00:00
|
|
|
WRITE(p, "varying float v_fogdepth;\n");
|
2013-02-05 00:37:00 +00:00
|
|
|
#endif
|
2013-01-14 19:43:18 +00:00
|
|
|
}
|
2012-12-05 03:45:28 +00:00
|
|
|
if (doTexture)
|
|
|
|
WRITE(p, "varying vec2 v_texcoord;\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-23 11:43:31 +00:00
|
|
|
WRITE(p, "void main() {\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
if (gstate.clearmode & 1)
|
|
|
|
{
|
2012-12-05 03:45:28 +00:00
|
|
|
// Clear mode does not allow any fancy shading.
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " gl_FragColor = v_color0;\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *secondary = "";
|
2012-11-23 00:26:12 +00:00
|
|
|
// Secondary color for specular on top of texture
|
2012-11-09 16:51:45 +00:00
|
|
|
if (lmode) {
|
2013-01-22 20:57:47 +00:00
|
|
|
WRITE(p, " vec4 s = vec4(v_color1, 0.0);\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
secondary = " + s";
|
2012-11-09 16:51:45 +00:00
|
|
|
} else {
|
|
|
|
secondary = "";
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gstate.textureMapEnable & 1) {
|
2013-01-14 19:43:18 +00:00
|
|
|
WRITE(p, " vec4 t = texture2D(tex, v_texcoord);\n");
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 p = v_color0;\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-01-22 20:57:47 +00:00
|
|
|
if (gstate.texfunc & 0x100) { // texfmt == RGBA
|
|
|
|
switch (gstate.texfunc & 0x7) {
|
|
|
|
case GE_TEXFUNC_MODULATE:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = t * p%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_DECAL:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(1.0 - t.a * p.rgb + t.a * u_texenv.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_BLEND:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4((1.0 - t.rgb) * p.rgb + t.rgb * u_texenv.rgb, p.a * t.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_REPLACE:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = t%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_ADD:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(t.rgb + p.rgb, p.a * t.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
default:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = p;\n"); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
}
|
|
|
|
} else { // texfmt == RGB
|
|
|
|
switch (gstate.texfunc & 0x7) {
|
|
|
|
case GE_TEXFUNC_MODULATE:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(t.rgb * p.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_DECAL:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(t.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_BLEND:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(1.0 - t.rgb) * p.rgb + t.rgb * u_texenv.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_REPLACE:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(t.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
case GE_TEXFUNC_ADD:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = vec4(t.rgb + p.rgb, p.a)%s;\n", secondary); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
default:
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = p;\n"); break;
|
2013-01-22 20:57:47 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-01-22 20:57:47 +00:00
|
|
|
} else {
|
|
|
|
// No texture mapping
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " vec4 v = v_color0 %s;\n", secondary);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-03-03 07:05:50 +00:00
|
|
|
|
|
|
|
if (enableColorDoubling) {
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " v = v * 2.0;\n");
|
2012-11-23 00:26:12 +00:00
|
|
|
}
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2013-02-05 00:37:00 +00:00
|
|
|
if (enableAlphaTest) {
|
2012-11-01 15:19:01 +00:00
|
|
|
int alphaTestFunc = gstate.alphatest & 7;
|
2013-03-10 15:00:15 +00:00
|
|
|
const char *alphaTestFuncs[] = { "#", "#", " != ", " == ", " >= ", " > ", " <= ", " < " }; // never/always don't make sense
|
2012-11-23 11:43:31 +00:00
|
|
|
if (alphaTestFuncs[alphaTestFunc][0] != '#')
|
2013-03-10 15:00:15 +00:00
|
|
|
WRITE(p, " if (v.a %s u_alphacolorref.a) discard;\n", alphaTestFuncs[alphaTestFunc]);
|
2012-11-23 11:43:31 +00:00
|
|
|
}
|
2013-03-25 14:36:56 +00:00
|
|
|
|
2013-02-12 23:50:08 +00:00
|
|
|
if (enableColorTest) {
|
2012-12-19 17:35:37 +00:00
|
|
|
// TODO: There are some colortestmasks we could handle.
|
|
|
|
int colorTestFunc = gstate.colortest & 3;
|
2013-03-25 14:57:40 +00:00
|
|
|
const char *colorTestFuncs[] = { "#", "#", " != ", " == " }; // never/always don't make sense
|
2012-12-19 17:35:37 +00:00
|
|
|
int colorTestMask = gstate.colormask;
|
|
|
|
if (colorTestFuncs[colorTestFunc][0] != '#')
|
2013-03-25 14:36:56 +00:00
|
|
|
WRITE(p, "if (v.rgb %s u_alphacolorref.rgb) discard;\n", colorTestFuncs[colorTestFunc]);
|
|
|
|
}
|
2013-03-03 07:05:50 +00:00
|
|
|
|
2013-01-14 19:43:18 +00:00
|
|
|
if (enableFog) {
|
|
|
|
WRITE(p, " float fogCoef = clamp(v_fogdepth, 0.0, 1.0);\n");
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " gl_FragColor = mix(vec4(u_fogcolor, v.a), v, fogCoef);\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
// WRITE(p, " v.x = v_depth;\n");
|
2013-02-05 00:37:00 +00:00
|
|
|
} else {
|
|
|
|
WRITE(p, " gl_FragColor = v;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-23 11:43:31 +00:00
|
|
|
|
2013-01-22 21:27:03 +00:00
|
|
|
#ifdef DEBUG_SHADER
|
|
|
|
if (doTexture) {
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " gl_FragColor = texture2D(tex, v_texcoord);\n");
|
2013-01-22 21:27:03 +00:00
|
|
|
} else {
|
2013-02-05 00:37:00 +00:00
|
|
|
WRITE(p, " gl_FragColor = vec4(1,0,1,1);\n");
|
2013-01-22 21:27:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
WRITE(p, "}\n");
|
|
|
|
}
|
|
|
|
|