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/.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-04-24 21:29:19 +00:00
|
|
|
#include <locale.h>
|
|
|
|
|
2013-10-28 14:22:33 +00:00
|
|
|
#include "gfx_es2/gpu_features.h"
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
2013-07-29 03:43:25 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2012-12-20 13:10:42 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-07-27 22:18:41 +00:00
|
|
|
#include "base/stringutil.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
|
|
|
#include "GPU/GPUState.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "GPU/GLES/VertexShaderGenerator.h"
|
2014-12-14 18:22:49 +00:00
|
|
|
#include "GPU/GLES/ShaderManager.h"
|
2014-09-10 08:44:22 +00:00
|
|
|
#include "GPU/Common/VertexDecoderCommon.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// SDL 1.2 on Apple does not have support for OpenGL 3 and hence needs
|
|
|
|
// special treatment in the shader generator.
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define FORCE_OPENGL_2_0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef WRITE
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
#define WRITE p+=sprintf
|
|
|
|
|
2013-04-15 20:57:54 +00:00
|
|
|
bool CanUseHardwareTransform(int prim) {
|
2012-12-21 10:08:54 +00:00
|
|
|
if (!g_Config.bHardwareTransform)
|
|
|
|
return false;
|
|
|
|
return !gstate.isModeThrough() && prim != GE_PRIM_RECTANGLES;
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-12-14 19:16:46 +00:00
|
|
|
void ComputeVertexShaderID(ShaderID *id, u32 vertType, bool useHWTransform) {
|
2014-01-18 22:56:04 +00:00
|
|
|
bool doTexture = gstate.isTextureMapEnabled() && !gstate.isModeClear();
|
2013-08-19 07:43:54 +00:00
|
|
|
bool doTextureProjection = gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_MATRIX;
|
2013-09-14 06:24:25 +00:00
|
|
|
bool doShadeMapping = gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP;
|
2014-12-02 00:13:26 +00:00
|
|
|
bool doFlatShading = gstate.getShadeMode() == GE_SHADE_FLAT && !gstate.isModeClear();
|
2012-12-05 04:13:36 +00:00
|
|
|
|
2013-04-20 18:20:03 +00:00
|
|
|
bool hasColor = (vertType & GE_VTYPE_COL_MASK) != 0;
|
|
|
|
bool hasNormal = (vertType & GE_VTYPE_NRM_MASK) != 0;
|
2014-01-18 22:56:04 +00:00
|
|
|
bool hasTexcoord = (vertType & GE_VTYPE_TC_MASK) != 0;
|
2013-01-20 23:34:37 +00:00
|
|
|
bool enableFog = gstate.isFogEnabled() && !gstate.isModeThrough() && !gstate.isModeClear();
|
2013-07-22 00:55:26 +00:00
|
|
|
bool lmode = gstate.isUsingSecondaryColor() && gstate.isLightingEnabled();
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2014-03-29 17:35:09 +00:00
|
|
|
int id0 = 0;
|
2014-09-21 06:55:48 +00:00
|
|
|
int id1 = 0;
|
2014-03-29 17:35:09 +00:00
|
|
|
|
|
|
|
id0 = lmode & 1;
|
|
|
|
id0 |= (gstate.isModeThrough() & 1) << 1;
|
|
|
|
id0 |= (enableFog & 1) << 2;
|
|
|
|
id0 |= (hasColor & 1) << 3;
|
2013-06-06 08:05:31 +00:00
|
|
|
if (doTexture) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= 1 << 4;
|
|
|
|
id0 |= (gstate_c.flipTexture & 1) << 5;
|
|
|
|
id0 |= (doTextureProjection & 1) << 6;
|
2013-04-17 19:41:47 +00:00
|
|
|
}
|
2013-02-14 23:30:02 +00:00
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
if (useHWTransform) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= 1 << 8;
|
|
|
|
id0 |= (hasNormal & 1) << 9;
|
2012-12-20 13:10:42 +00:00
|
|
|
|
|
|
|
// UV generation mode
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= gstate.getUVGenMode() << 16;
|
2012-12-20 13:10:42 +00:00
|
|
|
|
|
|
|
// The next bits are used differently depending on UVgen mode
|
2013-09-14 06:24:25 +00:00
|
|
|
if (doTextureProjection) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= gstate.getUVProjMode() << 18;
|
2013-09-14 06:24:25 +00:00
|
|
|
} else if (doShadeMapping) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= gstate.getUVLS0() << 18;
|
|
|
|
id0 |= gstate.getUVLS1() << 20;
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bones
|
2013-09-21 21:37:14 +00:00
|
|
|
if (vertTypeIsSkinningEnabled(vertType))
|
2014-03-29 17:35:09 +00:00
|
|
|
id0 |= (TranslateNumBones(vertTypeGetNumBoneWeights(vertType)) - 1) << 22;
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2013-01-22 20:57:47 +00:00
|
|
|
// Okay, d[1] coming up. ==============
|
|
|
|
|
2013-09-14 06:24:25 +00:00
|
|
|
if (gstate.isLightingEnabled() || doShadeMapping) {
|
2013-01-22 20:57:47 +00:00
|
|
|
// Light bits
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id1 |= gstate.getLightComputation(i) << (i * 4);
|
|
|
|
id1 |= gstate.getLightType(i) << (i * 4 + 2);
|
2013-01-22 20:57:47 +00:00
|
|
|
}
|
2014-03-29 17:35:09 +00:00
|
|
|
id1 |= (gstate.materialupdate & 7) << 16;
|
2013-01-22 20:57:47 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id1 |= (gstate.isLightChanEnabled(i) & 1) << (20 + i);
|
2013-01-22 20:57:47 +00:00
|
|
|
}
|
2014-03-29 17:35:09 +00:00
|
|
|
// doShadeMapping is stored as UVGenMode, so this is enough for isLightingEnabled.
|
|
|
|
id1 |= 1 << 24;
|
2012-12-21 20:49:09 +00:00
|
|
|
}
|
2014-03-29 17:35:09 +00:00
|
|
|
// 2 bits.
|
|
|
|
id1 |= (vertTypeGetWeightMask(vertType) >> GE_VTYPE_WEIGHT_SHIFT) << 25;
|
|
|
|
id1 |= (gstate.areNormalsReversed() & 1) << 27;
|
2013-12-08 10:52:20 +00:00
|
|
|
if (doTextureProjection && gstate.getUVProjMode() == GE_PROJMAP_UV) {
|
2014-03-29 17:35:09 +00:00
|
|
|
id1 |= ((vertType & GE_VTYPE_TC_MASK) >> GE_VTYPE_TC_SHIFT) << 28; // two bits
|
2014-01-18 22:56:04 +00:00
|
|
|
} else {
|
2014-03-29 17:35:09 +00:00
|
|
|
id1 |= (hasTexcoord & 1) << 28;
|
2013-12-08 10:52:20 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2014-11-28 09:59:14 +00:00
|
|
|
id1 |= (doFlatShading & 1) << 29;
|
2014-03-29 17:35:09 +00:00
|
|
|
|
|
|
|
id->d[0] = id0;
|
|
|
|
id->d[1] = id1;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-27 18:09:22 +00:00
|
|
|
static const char * const boneWeightAttrDecl[9] = {
|
|
|
|
"#ERROR#",
|
2013-10-08 15:26:33 +00:00
|
|
|
"attribute mediump float w1;\n",
|
|
|
|
"attribute mediump vec2 w1;\n",
|
|
|
|
"attribute mediump vec3 w1;\n",
|
|
|
|
"attribute mediump vec4 w1;\n",
|
|
|
|
"attribute mediump vec4 w1;\nattribute mediump float w2;\n",
|
|
|
|
"attribute mediump vec4 w1;\nattribute mediump vec2 w2;\n",
|
|
|
|
"attribute mediump vec4 w1;\nattribute mediump vec3 w2;\n",
|
|
|
|
"attribute mediump vec4 w1, w2;\n",
|
2012-12-20 13:10:42 +00:00
|
|
|
};
|
|
|
|
|
2013-12-18 10:22:31 +00:00
|
|
|
static const char * const boneWeightInDecl[9] = {
|
|
|
|
"#ERROR#",
|
|
|
|
"in mediump float w1;\n",
|
|
|
|
"in mediump vec2 w1;\n",
|
|
|
|
"in mediump vec3 w1;\n",
|
|
|
|
"in mediump vec4 w1;\n",
|
|
|
|
"in mediump vec4 w1;\nin mediump float w2;\n",
|
|
|
|
"in mediump vec4 w1;\nin mediump vec2 w2;\n",
|
|
|
|
"in mediump vec4 w1;\nin mediump vec3 w2;\n",
|
|
|
|
"in mediump vec4 w1, w2;\n",
|
|
|
|
};
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
enum DoLightComputation {
|
|
|
|
LIGHT_OFF,
|
2013-04-02 10:23:39 +00:00
|
|
|
LIGHT_SHADE,
|
2012-12-20 13:10:42 +00:00
|
|
|
LIGHT_FULL,
|
|
|
|
};
|
|
|
|
|
2013-09-21 21:37:14 +00:00
|
|
|
void GenerateVertexShader(int prim, u32 vertType, char *buffer, bool useHWTransform) {
|
2012-11-01 15:19:01 +00:00
|
|
|
char *p = buffer;
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-05-23 10:48:44 +00:00
|
|
|
// #define USE_FOR_LOOP
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-12-07 11:53:52 +00:00
|
|
|
// In GLSL ES 3.0, you use "out" variables instead.
|
|
|
|
bool glslES30 = false;
|
|
|
|
const char *varying = "varying";
|
2013-12-15 11:49:13 +00:00
|
|
|
const char *attribute = "attribute";
|
2013-12-18 10:22:31 +00:00
|
|
|
const char * const * boneWeightDecl = boneWeightAttrDecl;
|
2013-12-07 11:53:52 +00:00
|
|
|
bool highpFog = false;
|
2014-12-14 19:59:21 +00:00
|
|
|
bool highpTexcoord = false;
|
2013-12-07 11:53:52 +00:00
|
|
|
|
2012-11-26 03:25:14 +00:00
|
|
|
#if defined(USING_GLES2)
|
2013-12-07 11:53:52 +00:00
|
|
|
// Let's wait until we have a real use for this.
|
|
|
|
// ES doesn't support dual source alpha :(
|
2013-12-17 22:38:58 +00:00
|
|
|
if (gl_extensions.GLES3) {
|
|
|
|
WRITE(p, "#version 300 es\n");
|
2013-12-07 11:53:52 +00:00
|
|
|
glslES30 = true;
|
|
|
|
} else {
|
|
|
|
WRITE(p, "#version 100\n"); // GLSL ES 1.0
|
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "precision highp float;\n");
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-12-07 11:53:52 +00:00
|
|
|
// PowerVR needs highp to do the fog in MHU correctly.
|
|
|
|
// Others don't, and some can't handle highp in the fragment shader.
|
2014-12-15 00:15:09 +00:00
|
|
|
highpFog = (gl_extensions.bugs & BUG_PVR_SHADER_PRECISION_BAD) ? true : false;
|
|
|
|
highpTexcoord = highpFog;
|
2014-12-14 19:59:21 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
#elif !defined(FORCE_OPENGL_2_0)
|
2013-12-07 11:53:52 +00:00
|
|
|
if (gl_extensions.VersionGEThan(3, 3, 0)) {
|
|
|
|
glslES30 = true;
|
|
|
|
WRITE(p, "#version 330\n");
|
2014-06-10 20:41:53 +00:00
|
|
|
WRITE(p, "#define lowp\n");
|
|
|
|
WRITE(p, "#define mediump\n");
|
|
|
|
WRITE(p, "#define highp\n");
|
2013-12-29 11:27:19 +00:00
|
|
|
} else if (gl_extensions.VersionGEThan(3, 0, 0)) {
|
|
|
|
WRITE(p, "#version 130\n");
|
|
|
|
// Remove lowp/mediump in non-mobile non-glsl 3 implementations
|
|
|
|
WRITE(p, "#define lowp\n");
|
|
|
|
WRITE(p, "#define mediump\n");
|
|
|
|
WRITE(p, "#define highp\n");
|
2013-12-07 11:53:52 +00:00
|
|
|
} else {
|
|
|
|
WRITE(p, "#version 110\n");
|
|
|
|
// Remove lowp/mediump in non-mobile non-glsl 3 implementations
|
|
|
|
WRITE(p, "#define lowp\n");
|
|
|
|
WRITE(p, "#define mediump\n");
|
|
|
|
WRITE(p, "#define highp\n");
|
|
|
|
}
|
2013-03-03 22:57:29 +00:00
|
|
|
#else
|
|
|
|
// Need to remove lowp/mediump for Mac
|
|
|
|
WRITE(p, "#define lowp\n");
|
|
|
|
WRITE(p, "#define mediump\n");
|
2013-10-22 17:54:43 +00:00
|
|
|
WRITE(p, "#define highp\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-07 11:53:52 +00:00
|
|
|
if (glslES30) {
|
2013-12-15 11:49:13 +00:00
|
|
|
attribute = "in";
|
2013-12-07 11:53:52 +00:00
|
|
|
varying = "out";
|
2013-12-18 10:22:31 +00:00
|
|
|
boneWeightDecl = boneWeightInDecl;
|
2013-12-07 11:53:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 01:03:17 +00:00
|
|
|
bool lmode = gstate.isUsingSecondaryColor() && gstate.isLightingEnabled() && !gstate.isModeThrough();
|
2014-01-18 22:56:04 +00:00
|
|
|
bool doTexture = gstate.isTextureMapEnabled() && !gstate.isModeClear();
|
2013-09-14 06:24:25 +00:00
|
|
|
bool doTextureProjection = gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_MATRIX;
|
|
|
|
bool doShadeMapping = gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP;
|
2014-12-02 00:13:26 +00:00
|
|
|
bool doFlatShading = gstate.getShadeMode() == GE_SHADE_FLAT && !gstate.isModeClear();
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
bool hasColor = (vertType & GE_VTYPE_COL_MASK) != 0 || !useHWTransform;
|
|
|
|
bool hasNormal = (vertType & GE_VTYPE_NRM_MASK) != 0 && useHWTransform;
|
2014-01-18 23:44:04 +00:00
|
|
|
bool hasTexcoord = (vertType & GE_VTYPE_TC_MASK) != 0 || !useHWTransform;
|
2013-01-14 19:43:18 +00:00
|
|
|
bool enableFog = gstate.isFogEnabled() && !gstate.isModeThrough() && !gstate.isModeClear();
|
2013-04-20 18:20:03 +00:00
|
|
|
bool throughmode = (vertType & GE_VTYPE_THROUGH_MASK) != 0;
|
2013-11-19 22:37:09 +00:00
|
|
|
bool flipV = gstate_c.flipTexture; // This also means that we are texturing from a render target
|
2013-11-02 10:05:31 +00:00
|
|
|
bool flipNormal = gstate.areNormalsReversed();
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2014-12-01 10:16:20 +00:00
|
|
|
const char *shading = "";
|
|
|
|
if (glslES30)
|
2014-12-02 00:13:26 +00:00
|
|
|
shading = doFlatShading ? "flat" : "";
|
2014-11-28 09:59:14 +00:00
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
DoLightComputation doLight[4] = {LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF};
|
2013-06-06 08:05:31 +00:00
|
|
|
if (useHWTransform) {
|
2013-09-14 06:24:25 +00:00
|
|
|
int shadeLight0 = doShadeMapping ? gstate.getUVLS0() : -1;
|
|
|
|
int shadeLight1 = doShadeMapping ? gstate.getUVLS1() : -1;
|
2012-12-20 13:10:42 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (i == shadeLight0 || i == shadeLight1)
|
2013-04-02 10:23:39 +00:00
|
|
|
doLight[i] = LIGHT_SHADE;
|
2013-07-22 01:13:55 +00:00
|
|
|
if (gstate.isLightingEnabled() && gstate.isLightChanEnabled(i))
|
2012-12-20 13:10:42 +00:00
|
|
|
doLight[i] = LIGHT_FULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:37:14 +00:00
|
|
|
if (vertTypeIsSkinningEnabled(vertType)) {
|
2013-12-18 10:22:31 +00:00
|
|
|
WRITE(p, "%s", boneWeightDecl[TranslateNumBones(vertTypeGetNumBoneWeights(vertType))]);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
if (useHWTransform)
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s vec3 position;\n", attribute);
|
2013-01-14 19:43:18 +00:00
|
|
|
else
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s vec4 position;\n", attribute); // need to pass the fog coord in w
|
2013-01-14 19:43:18 +00:00
|
|
|
|
2013-06-24 17:11:04 +00:00
|
|
|
if (useHWTransform && hasNormal)
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s mediump vec3 normal;\n", attribute);
|
2013-06-24 17:11:04 +00:00
|
|
|
|
2014-01-18 22:56:04 +00:00
|
|
|
if (doTexture && hasTexcoord) {
|
2015-03-08 20:12:13 +00:00
|
|
|
if (!useHWTransform && doTextureProjection && !throughmode)
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s vec3 texcoord;\n", attribute);
|
2013-04-17 19:41:47 +00:00
|
|
|
else
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s vec2 texcoord;\n", attribute);
|
2013-04-17 19:41:47 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
if (hasColor) {
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s lowp vec4 color0;\n", attribute);
|
2015-03-09 01:03:17 +00:00
|
|
|
if (lmode && !useHWTransform) // only software transform supplies color1 as vertex data
|
2013-12-15 11:49:13 +00:00
|
|
|
WRITE(p, "%s lowp vec3 color1;\n", attribute);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 12:45:22 +00:00
|
|
|
if (gstate.isModeThrough()) {
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "uniform mat4 u_proj_through;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
} else {
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "uniform mat4 u_proj;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
// Add all the uniforms we'll need to transform properly.
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-11-30 14:54:20 +00:00
|
|
|
bool prescale = g_Config.bPrescaleUV && !throughmode && (gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_COORDS || gstate.getUVGenMode() == GE_TEXMAP_UNKNOWN);
|
2013-10-22 17:54:43 +00:00
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
if (useHWTransform) {
|
2012-12-20 13:10:42 +00:00
|
|
|
// When transforming by hardware, we need a great deal more uniforms...
|
2012-12-21 10:08:54 +00:00
|
|
|
WRITE(p, "uniform mat4 u_world;\n");
|
|
|
|
WRITE(p, "uniform mat4 u_view;\n");
|
2013-09-14 06:24:25 +00:00
|
|
|
if (doTextureProjection)
|
2013-05-18 17:50:07 +00:00
|
|
|
WRITE(p, "uniform mediump mat4 u_texmtx;\n");
|
2013-09-21 21:37:14 +00:00
|
|
|
if (vertTypeIsSkinningEnabled(vertType)) {
|
|
|
|
int numBones = TranslateNumBones(vertTypeGetNumBoneWeights(vertType));
|
2013-05-26 12:03:02 +00:00
|
|
|
#ifdef USE_BONE_ARRAY
|
2013-05-18 18:44:25 +00:00
|
|
|
WRITE(p, "uniform mediump mat4 u_bone[%i];\n", numBones);
|
2013-05-26 12:03:02 +00:00
|
|
|
#else
|
|
|
|
for (int i = 0; i < numBones; i++) {
|
2013-05-28 15:53:43 +00:00
|
|
|
WRITE(p, "uniform mat4 u_bone%i;\n", i);
|
2013-05-26 12:03:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-11-19 22:37:09 +00:00
|
|
|
if (doTexture && (flipV || !prescale || gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP || gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_MATRIX)) {
|
2013-06-24 17:11:04 +00:00
|
|
|
WRITE(p, "uniform vec4 u_uvscaleoffset;\n");
|
2013-07-09 23:22:15 +00:00
|
|
|
}
|
2012-12-20 17:31:21 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (doLight[i] != LIGHT_OFF) {
|
2013-04-02 10:23:39 +00:00
|
|
|
// This is needed for shade mapping
|
2012-12-20 17:31:21 +00:00
|
|
|
WRITE(p, "uniform vec3 u_lightpos%i;\n", i);
|
|
|
|
}
|
|
|
|
if (doLight[i] == LIGHT_FULL) {
|
2013-07-22 01:13:55 +00:00
|
|
|
GELightType type = gstate.getLightType(i);
|
2013-04-02 10:23:39 +00:00
|
|
|
|
2013-05-19 14:07:48 +00:00
|
|
|
if (type != GE_LIGHTTYPE_DIRECTIONAL)
|
|
|
|
WRITE(p, "uniform mediump vec3 u_lightatt%i;\n", i);
|
|
|
|
|
2013-08-20 11:43:26 +00:00
|
|
|
if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) {
|
2013-09-01 10:11:02 +00:00
|
|
|
WRITE(p, "uniform mediump vec3 u_lightdir%i;\n", i);
|
2013-05-19 14:07:48 +00:00
|
|
|
WRITE(p, "uniform mediump float u_lightangle%i;\n", i);
|
|
|
|
WRITE(p, "uniform mediump float u_lightspotCoef%i;\n", i);
|
|
|
|
}
|
2013-03-03 22:57:29 +00:00
|
|
|
WRITE(p, "uniform lowp vec3 u_lightambient%i;\n", i);
|
|
|
|
WRITE(p, "uniform lowp vec3 u_lightdiffuse%i;\n", i);
|
2013-05-19 14:07:48 +00:00
|
|
|
|
2013-07-22 01:13:55 +00:00
|
|
|
if (gstate.isUsingSpecularLight(i))
|
2013-05-19 14:07:48 +00:00
|
|
|
WRITE(p, "uniform lowp vec3 u_lightspecular%i;\n", i);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 14:07:48 +00:00
|
|
|
if (gstate.isLightingEnabled()) {
|
|
|
|
WRITE(p, "uniform lowp vec4 u_ambient;\n");
|
2013-12-29 20:57:30 +00:00
|
|
|
if ((gstate.materialupdate & 2) == 0 || !hasColor)
|
2013-05-19 14:07:48 +00:00
|
|
|
WRITE(p, "uniform lowp vec3 u_matdiffuse;\n");
|
|
|
|
// if ((gstate.materialupdate & 4) == 0)
|
|
|
|
WRITE(p, "uniform lowp vec4 u_matspecular;\n"); // Specular coef is contained in alpha
|
|
|
|
WRITE(p, "uniform lowp vec3 u_matemissive;\n");
|
|
|
|
}
|
2013-06-23 22:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (useHWTransform || !hasColor)
|
|
|
|
WRITE(p, "uniform lowp vec4 u_matambientalpha;\n"); // matambient + matalpha
|
|
|
|
|
|
|
|
if (enableFog) {
|
2013-10-22 17:54:43 +00:00
|
|
|
WRITE(p, "uniform highp vec2 u_fogcoef;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2014-11-28 09:59:14 +00:00
|
|
|
WRITE(p, "%s %s lowp vec4 v_color0;\n", shading, varying);
|
2015-03-09 01:03:17 +00:00
|
|
|
if (lmode) {
|
2014-11-28 09:59:14 +00:00
|
|
|
WRITE(p, "%s %s lowp vec3 v_color1;\n", shading, varying);
|
2013-12-07 11:53:52 +00:00
|
|
|
}
|
2013-04-17 19:41:47 +00:00
|
|
|
if (doTexture) {
|
|
|
|
if (doTextureProjection)
|
2014-12-14 19:59:21 +00:00
|
|
|
WRITE(p, "%s %s vec3 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
|
2013-04-17 19:41:47 +00:00
|
|
|
else
|
2014-12-14 19:59:21 +00:00
|
|
|
WRITE(p, "%s %s vec2 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
|
2013-04-17 19:41:47 +00:00
|
|
|
}
|
2013-10-28 14:22:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (enableFog) {
|
|
|
|
// See the fragment shader generator
|
2013-12-07 11:53:52 +00:00
|
|
|
if (highpFog) {
|
|
|
|
WRITE(p, "%s highp float v_fogdepth;\n", varying);
|
2013-10-28 14:22:33 +00:00
|
|
|
} else {
|
2013-12-07 11:53:52 +00:00
|
|
|
WRITE(p, "%s mediump float v_fogdepth;\n", varying);
|
2013-10-28 14:22:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 19:43:18 +00:00
|
|
|
|
2015-08-24 23:26:29 +00:00
|
|
|
// Even with this we don't match, because the viewport is different I think.
|
|
|
|
// I think we need to apply viewportZ, round/floor, and then undo viewportZ.
|
|
|
|
WRITE(p, "\nvec4 depthRoundZ(vec4 v) {\n");
|
|
|
|
WRITE(p, " return vec4(v.x, v.y, (1.0/65536.0) * floor(v.z * 65536.0), v.w);\n");
|
|
|
|
WRITE(p, "}\n\n");
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "void main() {\n");
|
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
if (!useHWTransform) {
|
2012-12-20 13:10:42 +00:00
|
|
|
// Simple pass-through of vertex data to fragment shader
|
2014-01-18 22:56:04 +00:00
|
|
|
if (doTexture) {
|
2015-03-08 20:12:13 +00:00
|
|
|
if (throughmode && doTextureProjection) {
|
|
|
|
WRITE(p, " v_texcoord = vec3(texcoord, 1.0);\n");
|
|
|
|
} else {
|
|
|
|
WRITE(p, " v_texcoord = texcoord;\n");
|
|
|
|
}
|
2014-01-18 22:56:04 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
if (hasColor) {
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " v_color0 = color0;\n");
|
2015-03-09 01:03:17 +00:00
|
|
|
if (lmode)
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " v_color1 = color1;\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
} else {
|
|
|
|
WRITE(p, " v_color0 = u_matambientalpha;\n");
|
2015-03-09 01:03:17 +00:00
|
|
|
if (lmode)
|
2013-03-27 10:21:02 +00:00
|
|
|
WRITE(p, " v_color1 = vec3(0.0);\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-01-14 19:43:18 +00:00
|
|
|
if (enableFog) {
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " v_fogdepth = position.w;\n");
|
2013-01-14 19:43:18 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
if (gstate.isModeThrough()) {
|
2015-08-24 23:26:29 +00:00
|
|
|
WRITE(p, " gl_Position = depthRoundZ(u_proj_through * vec4(position.xyz, 1.0));\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
} else {
|
2015-08-24 23:26:29 +00:00
|
|
|
WRITE(p, " gl_Position = depthRoundZ(u_proj * vec4(position.xyz, 1.0));\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Step 1: World Transform / Skinning
|
2013-09-21 21:37:14 +00:00
|
|
|
if (!vertTypeIsSkinningEnabled(vertType)) {
|
2012-12-20 13:10:42 +00:00
|
|
|
// No skinning, just standard T&L.
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " vec3 worldpos = (u_world * vec4(position.xyz, 1.0)).xyz;\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
if (hasNormal)
|
2013-11-02 10:05:31 +00:00
|
|
|
WRITE(p, " mediump vec3 worldnormal = normalize((u_world * vec4(%snormal, 0.0)).xyz);\n", flipNormal ? "-" : "");
|
2013-05-04 20:23:50 +00:00
|
|
|
else
|
2013-10-01 16:33:32 +00:00
|
|
|
WRITE(p, " mediump vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
} else {
|
2013-09-21 21:37:14 +00:00
|
|
|
int numWeights = TranslateNumBones(vertTypeGetNumBoneWeights(vertType));
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-07-27 20:14:34 +00:00
|
|
|
static const char *rescale[4] = {"", " * 1.9921875", " * 1.999969482421875", ""}; // 2*127.5f/128.f, 2*32767.5f/32768.f, 1.0f};
|
2013-09-21 21:44:11 +00:00
|
|
|
const char *factor = rescale[vertTypeGetWeightMask(vertType) >> GE_VTYPE_WEIGHT_SHIFT];
|
2013-05-22 13:44:15 +00:00
|
|
|
|
|
|
|
static const char * const boneWeightAttr[8] = {
|
2013-10-08 15:26:33 +00:00
|
|
|
"w1.x", "w1.y", "w1.z", "w1.w",
|
|
|
|
"w2.x", "w2.y", "w2.z", "w2.w",
|
2013-05-22 13:44:15 +00:00
|
|
|
};
|
2013-05-30 12:55:48 +00:00
|
|
|
|
2013-05-26 12:03:02 +00:00
|
|
|
#if defined(USE_FOR_LOOP) && defined(USE_BONE_ARRAY)
|
2013-05-22 13:44:15 +00:00
|
|
|
|
2013-05-18 18:44:25 +00:00
|
|
|
// To loop through the weights, we unfortunately need to put them in a float array.
|
|
|
|
// GLSL ES sucks - no way to directly initialize an array!
|
|
|
|
switch (numWeights) {
|
2013-10-08 15:26:33 +00:00
|
|
|
case 1: WRITE(p, " float w[1]; w[0] = w1;\n"); break;
|
|
|
|
case 2: WRITE(p, " float w[2]; w[0] = w1.x; w[1] = w1.y;\n"); break;
|
|
|
|
case 3: WRITE(p, " float w[3]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z;\n"); break;
|
|
|
|
case 4: WRITE(p, " float w[4]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z; w[3] = w1.w;\n"); break;
|
|
|
|
case 5: WRITE(p, " float w[5]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z; w[3] = w1.w; w[4] = w2;\n"); break;
|
|
|
|
case 6: WRITE(p, " float w[6]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z; w[3] = w1.w; w[4] = w2.x; w[5] = w2.y;\n"); break;
|
|
|
|
case 7: WRITE(p, " float w[7]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z; w[3] = w1.w; w[4] = w2.x; w[5] = w2.y; w[6] = w2.z;\n"); break;
|
|
|
|
case 8: WRITE(p, " float w[8]; w[0] = w1.x; w[1] = w1.y; w[2] = w1.z; w[3] = w1.w; w[4] = w2.x; w[5] = w2.y; w[6] = w2.z; w[7] = w2.w;\n"); break;
|
2013-05-18 18:44:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 22:15:15 +00:00
|
|
|
WRITE(p, " mat4 skinMatrix = w[0] * u_bone[0];\n");
|
|
|
|
if (numWeights > 1) {
|
|
|
|
WRITE(p, " for (int i = 1; i < %i; i++) {\n", numWeights);
|
|
|
|
WRITE(p, " skinMatrix += w[i] * u_bone[i];\n");
|
|
|
|
WRITE(p, " }\n");
|
|
|
|
}
|
2013-05-18 18:44:25 +00:00
|
|
|
|
|
|
|
#else
|
2013-05-26 12:03:02 +00:00
|
|
|
|
|
|
|
#ifdef USE_BONE_ARRAY
|
2013-05-19 22:15:15 +00:00
|
|
|
if (numWeights == 1)
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " mat4 skinMatrix = w1 * u_bone[0]");
|
2013-05-19 22:15:15 +00:00
|
|
|
else
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " mat4 skinMatrix = w1.x * u_bone[0]");
|
2013-05-19 22:15:15 +00:00
|
|
|
for (int i = 1; i < numWeights; i++) {
|
2012-12-20 17:31:21 +00:00
|
|
|
const char *weightAttr = boneWeightAttr[i];
|
|
|
|
// workaround for "cant do .x of scalar" issue
|
2013-10-08 15:26:33 +00:00
|
|
|
if (numWeights == 1 && i == 0) weightAttr = "w1";
|
|
|
|
if (numWeights == 5 && i == 4) weightAttr = "w2";
|
2013-05-22 13:44:15 +00:00
|
|
|
WRITE(p, " + %s * u_bone[%i]", weightAttr, i);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-05-26 12:03:02 +00:00
|
|
|
#else
|
2013-06-06 08:05:31 +00:00
|
|
|
// Uncomment this to screw up bone shaders to check the vertex shader software fallback
|
|
|
|
// WRITE(p, "THIS SHOULD ERROR! #error");
|
2013-05-26 12:03:02 +00:00
|
|
|
if (numWeights == 1)
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " mat4 skinMatrix = w1 * u_bone0");
|
2013-05-26 12:03:02 +00:00
|
|
|
else
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " mat4 skinMatrix = w1.x * u_bone0");
|
2013-05-26 12:03:02 +00:00
|
|
|
for (int i = 1; i < numWeights; i++) {
|
|
|
|
const char *weightAttr = boneWeightAttr[i];
|
|
|
|
// workaround for "cant do .x of scalar" issue
|
2013-10-08 15:26:33 +00:00
|
|
|
if (numWeights == 1 && i == 0) weightAttr = "w1";
|
|
|
|
if (numWeights == 5 && i == 4) weightAttr = "w2";
|
2013-05-26 12:03:02 +00:00
|
|
|
WRITE(p, " + %s * u_bone%i", weightAttr, i);
|
|
|
|
}
|
2013-05-22 17:49:58 +00:00
|
|
|
#endif
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-05-26 12:03:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
WRITE(p, ";\n");
|
|
|
|
|
2013-05-22 17:49:58 +00:00
|
|
|
// Trying to simplify this results in bugs in LBP...
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " vec3 skinnedpos = (skinMatrix * vec4(position, 1.0)).xyz %s;\n", factor);
|
2013-05-22 17:49:58 +00:00
|
|
|
WRITE(p, " vec3 worldpos = (u_world * vec4(skinnedpos, 1.0)).xyz;\n");
|
2013-05-19 22:15:15 +00:00
|
|
|
|
2013-05-22 17:49:58 +00:00
|
|
|
if (hasNormal) {
|
2013-11-02 10:05:31 +00:00
|
|
|
WRITE(p, " mediump vec3 skinnednormal = (skinMatrix * vec4(%snormal, 0.0)).xyz %s;\n", flipNormal ? "-" : "", factor);
|
2013-05-22 17:49:58 +00:00
|
|
|
} else {
|
2015-01-12 19:37:33 +00:00
|
|
|
WRITE(p, " mediump vec3 skinnednormal = (skinMatrix * vec4(0.0, 0.0, %s1.0, 0.0)).xyz %s;\n", flipNormal ? "-" : "", factor);
|
2013-05-22 17:49:58 +00:00
|
|
|
}
|
2015-01-12 19:37:33 +00:00
|
|
|
WRITE(p, " mediump vec3 worldnormal = normalize((u_world * vec4(skinnednormal, 0.0)).xyz);\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 17:11:04 +00:00
|
|
|
WRITE(p, " vec4 viewPos = u_view * vec4(worldpos, 1.0);\n");
|
|
|
|
|
|
|
|
// Final view and projection transforms.
|
2015-08-24 23:26:29 +00:00
|
|
|
WRITE(p, " gl_Position = depthRoundZ(u_proj * viewPos);\n");
|
2013-05-22 13:44:15 +00:00
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// TODO: Declare variables for dots for shade mapping if needed.
|
|
|
|
|
2013-12-29 20:57:30 +00:00
|
|
|
const char *ambientStr = (gstate.materialupdate & 1) && hasColor ? "color0" : "u_matambientalpha";
|
|
|
|
const char *diffuseStr = (gstate.materialupdate & 2) && hasColor ? "color0.rgb" : "u_matdiffuse";
|
|
|
|
const char *specularStr = (gstate.materialupdate & 4) && hasColor ? "color0.rgb" : "u_matspecular.rgb";
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2013-05-19 12:36:07 +00:00
|
|
|
bool diffuseIsZero = true;
|
|
|
|
bool specularIsZero = true;
|
|
|
|
bool distanceNeeded = false;
|
2013-05-19 12:23:17 +00:00
|
|
|
|
2013-05-19 12:45:56 +00:00
|
|
|
if (gstate.isLightingEnabled()) {
|
|
|
|
WRITE(p, " lowp vec4 lightSum0 = u_ambient * %s + vec4(u_matemissive, 0.0);\n", ambientStr);
|
2013-05-19 11:52:47 +00:00
|
|
|
|
2013-05-19 12:45:56 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (doLight[i] != LIGHT_FULL)
|
|
|
|
continue;
|
|
|
|
diffuseIsZero = false;
|
2013-07-22 01:13:55 +00:00
|
|
|
if (gstate.isUsingSpecularLight(i))
|
2013-05-19 12:45:56 +00:00
|
|
|
specularIsZero = false;
|
2013-07-22 01:13:55 +00:00
|
|
|
GELightType type = gstate.getLightType(i);
|
2013-05-19 12:45:56 +00:00
|
|
|
if (type != GE_LIGHTTYPE_DIRECTIONAL)
|
|
|
|
distanceNeeded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!specularIsZero) {
|
|
|
|
WRITE(p, " lowp vec3 lightSum1 = vec3(0.0);\n");
|
|
|
|
}
|
|
|
|
if (!diffuseIsZero) {
|
|
|
|
WRITE(p, " vec3 toLight;\n");
|
|
|
|
WRITE(p, " lowp vec3 diffuse;\n");
|
|
|
|
}
|
|
|
|
if (distanceNeeded) {
|
|
|
|
WRITE(p, " float distance;\n");
|
|
|
|
WRITE(p, " lowp float lightScale;\n");
|
|
|
|
}
|
2013-05-19 12:36:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Calculate lights if needed. If shade mapping is enabled, lights may need to be
|
2012-12-26 19:47:09 +00:00
|
|
|
// at least partially calculated.
|
2012-12-20 13:10:42 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2013-04-02 10:23:39 +00:00
|
|
|
if (doLight[i] != LIGHT_FULL)
|
2012-12-20 13:10:42 +00:00
|
|
|
continue;
|
2012-12-26 19:47:09 +00:00
|
|
|
|
2013-07-22 01:13:55 +00:00
|
|
|
GELightType type = gstate.getLightType(i);
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2013-06-20 21:30:21 +00:00
|
|
|
if (type == GE_LIGHTTYPE_DIRECTIONAL) {
|
|
|
|
// We prenormalize light positions for directional lights.
|
2013-06-24 17:11:04 +00:00
|
|
|
WRITE(p, " toLight = u_lightpos%i;\n", i);
|
2013-06-20 21:30:21 +00:00
|
|
|
} else {
|
2013-05-19 12:45:56 +00:00
|
|
|
WRITE(p, " toLight = u_lightpos%i - worldpos;\n", i);
|
2013-05-22 13:44:15 +00:00
|
|
|
WRITE(p, " distance = length(toLight);\n");
|
|
|
|
WRITE(p, " toLight /= distance;\n");
|
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2013-07-22 01:13:55 +00:00
|
|
|
bool doSpecular = gstate.isUsingSpecularLight(i);
|
|
|
|
bool poweredDiffuse = gstate.isUsingPoweredDiffuseLight(i);
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2014-09-24 06:51:58 +00:00
|
|
|
WRITE(p, " mediump float dot%i = max(dot(toLight, worldnormal), 0.0);\n", i);
|
2012-12-20 17:31:21 +00:00
|
|
|
if (poweredDiffuse) {
|
2014-09-24 06:51:58 +00:00
|
|
|
// pow(0.0, 0.0) may be undefined, but the PSP seems to treat it as 1.0.
|
2013-09-01 10:11:02 +00:00
|
|
|
// Seen in Tales of the World: Radiant Mythology (#2424.)
|
2014-09-24 06:51:58 +00:00
|
|
|
WRITE(p, " if (dot%i == 0.0 && u_matspecular.a == 0.0) {\n", i);
|
2013-09-01 10:11:02 +00:00
|
|
|
WRITE(p, " dot%i = 1.0;\n", i);
|
2014-09-24 06:51:58 +00:00
|
|
|
WRITE(p, " } else {\n");
|
|
|
|
WRITE(p, " dot%i = pow(dot%i, u_matspecular.a);\n", i, i);
|
|
|
|
WRITE(p, " }\n");
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2012-12-26 19:47:09 +00:00
|
|
|
|
2013-05-22 13:44:15 +00:00
|
|
|
const char *timesLightScale = " * lightScale";
|
2013-05-19 11:51:46 +00:00
|
|
|
|
2013-04-09 16:25:22 +00:00
|
|
|
// Attenuation
|
|
|
|
switch (type) {
|
|
|
|
case GE_LIGHTTYPE_DIRECTIONAL:
|
2013-05-22 13:44:15 +00:00
|
|
|
timesLightScale = "";
|
2013-04-09 16:25:22 +00:00
|
|
|
break;
|
|
|
|
case GE_LIGHTTYPE_POINT:
|
2013-05-19 12:36:07 +00:00
|
|
|
WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, vec3(1.0, distance, distance*distance)), 0.0, 1.0);\n", i);
|
2013-04-09 16:25:22 +00:00
|
|
|
break;
|
|
|
|
case GE_LIGHTTYPE_SPOT:
|
2013-08-20 03:43:18 +00:00
|
|
|
case GE_LIGHTTYPE_UNKNOWN:
|
2013-05-30 13:29:47 +00:00
|
|
|
WRITE(p, " lowp float angle%i = dot(normalize(u_lightdir%i), toLight);\n", i, i);
|
2013-04-09 16:25:22 +00:00
|
|
|
WRITE(p, " if (angle%i >= u_lightangle%i) {\n", i, i);
|
2013-05-19 12:36:07 +00:00
|
|
|
WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, vec3(1.0, distance, distance*distance)), 0.0, 1.0) * pow(angle%i, u_lightspotCoef%i);\n", i, i, i);
|
|
|
|
WRITE(p, " } else {\n");
|
|
|
|
WRITE(p, " lightScale = 0.0;\n");
|
2013-05-30 13:29:47 +00:00
|
|
|
WRITE(p, " }\n");
|
2013-04-09 16:25:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// ILLEGAL
|
|
|
|
break;
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2013-04-09 16:25:22 +00:00
|
|
|
|
2014-09-24 06:51:58 +00:00
|
|
|
WRITE(p, " diffuse = (u_lightdiffuse%i * %s) * dot%i;\n", i, diffuseStr, i);
|
2012-12-20 17:31:21 +00:00
|
|
|
if (doSpecular) {
|
2013-07-27 16:16:32 +00:00
|
|
|
WRITE(p, " dot%i = dot(normalize(toLight + vec3(0.0, 0.0, 1.0)), worldnormal);\n", i);
|
2012-12-20 17:31:21 +00:00
|
|
|
WRITE(p, " if (dot%i > 0.0)\n", i);
|
2013-05-19 12:45:56 +00:00
|
|
|
WRITE(p, " lightSum1 += u_lightspecular%i * %s * (pow(dot%i, u_matspecular.a) %s);\n", i, specularStr, i, timesLightScale);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2013-06-05 05:54:19 +00:00
|
|
|
WRITE(p, " lightSum0.rgb += (u_lightambient%i * %s.rgb + diffuse)%s;\n", i, ambientStr, timesLightScale);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2013-03-27 10:21:02 +00:00
|
|
|
if (gstate.isLightingEnabled()) {
|
2012-12-20 13:10:42 +00:00
|
|
|
// Sum up ambient, emissive here.
|
|
|
|
if (lmode) {
|
2013-05-19 12:23:17 +00:00
|
|
|
WRITE(p, " v_color0 = clamp(lightSum0, 0.0, 1.0);\n");
|
|
|
|
// v_color1 only exists when lmode = 1.
|
2015-03-09 01:03:17 +00:00
|
|
|
if (specularIsZero) {
|
2013-05-19 12:23:17 +00:00
|
|
|
WRITE(p, " v_color1 = vec3(0.0);\n");
|
2015-03-09 01:03:17 +00:00
|
|
|
} else {
|
2013-05-19 12:23:17 +00:00
|
|
|
WRITE(p, " v_color1 = clamp(lightSum1, 0.0, 1.0);\n");
|
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
} else {
|
2013-05-19 12:36:07 +00:00
|
|
|
if (specularIsZero) {
|
2013-05-19 12:23:17 +00:00
|
|
|
WRITE(p, " v_color0 = clamp(lightSum0, 0.0, 1.0);\n");
|
|
|
|
} else {
|
|
|
|
WRITE(p, " v_color0 = clamp(clamp(lightSum0, 0.0, 1.0) + vec4(lightSum1, 0.0), 0.0, 1.0);\n");
|
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Lighting doesn't affect color.
|
|
|
|
if (hasColor) {
|
2013-10-08 15:26:33 +00:00
|
|
|
WRITE(p, " v_color0 = color0;\n");
|
2012-12-26 19:47:09 +00:00
|
|
|
} else {
|
2013-01-12 00:24:05 +00:00
|
|
|
WRITE(p, " v_color0 = u_matambientalpha;\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2015-03-09 01:03:17 +00:00
|
|
|
if (lmode)
|
2013-03-27 10:21:02 +00:00
|
|
|
WRITE(p, " v_color1 = vec3(0.0);\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: UV generation
|
|
|
|
if (doTexture) {
|
|
|
|
switch (gstate.getUVGenMode()) {
|
2013-08-19 07:43:54 +00:00
|
|
|
case GE_TEXMAP_TEXTURE_COORDS: // Scale-offset. Easy.
|
|
|
|
case GE_TEXMAP_UNKNOWN: // Not sure what this is, but Riviera uses it. Treating as coords works.
|
2013-11-19 22:37:09 +00:00
|
|
|
if (prescale && !flipV) {
|
2014-01-18 22:56:04 +00:00
|
|
|
if (hasTexcoord) {
|
|
|
|
WRITE(p, " v_texcoord = texcoord;\n");
|
|
|
|
} else {
|
|
|
|
WRITE(p, " v_texcoord = vec2(0.0);\n");
|
|
|
|
}
|
2013-07-27 21:21:48 +00:00
|
|
|
} else {
|
2014-01-18 22:56:04 +00:00
|
|
|
if (hasTexcoord) {
|
|
|
|
WRITE(p, " v_texcoord = texcoord * u_uvscaleoffset.xy + u_uvscaleoffset.zw;\n");
|
|
|
|
} else {
|
|
|
|
WRITE(p, " v_texcoord = u_uvscaleoffset.zw;\n");
|
|
|
|
}
|
2013-07-27 21:21:48 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
break;
|
|
|
|
|
2013-08-19 07:43:54 +00:00
|
|
|
case GE_TEXMAP_TEXTURE_MATRIX: // Projection mapping.
|
2013-05-19 22:15:15 +00:00
|
|
|
{
|
2013-07-27 22:18:41 +00:00
|
|
|
std::string temp_tc;
|
2013-05-19 22:15:15 +00:00
|
|
|
switch (gstate.getUVProjMode()) {
|
2013-08-20 03:56:43 +00:00
|
|
|
case GE_PROJMAP_POSITION: // Use model space XYZ as source
|
2013-10-08 15:26:33 +00:00
|
|
|
temp_tc = "vec4(position.xyz, 1.0)";
|
2013-05-19 22:15:15 +00:00
|
|
|
break;
|
2013-08-20 03:56:43 +00:00
|
|
|
case GE_PROJMAP_UV: // Use unscaled UV as source
|
2013-07-27 22:18:41 +00:00
|
|
|
{
|
2013-12-08 10:52:20 +00:00
|
|
|
// prescale is false here.
|
2014-01-18 22:56:04 +00:00
|
|
|
if (hasTexcoord) {
|
|
|
|
static const char *rescaleuv[4] = {"", " * 1.9921875", " * 1.999969482421875", ""}; // 2*127.5f/128.f, 2*32767.5f/32768.f, 1.0f};
|
|
|
|
const char *factor = rescaleuv[(vertType & GE_VTYPE_TC_MASK) >> GE_VTYPE_TC_SHIFT];
|
|
|
|
temp_tc = StringFromFormat("vec4(texcoord.xy %s, 0.0, 1.0)", factor);
|
|
|
|
} else {
|
|
|
|
temp_tc = "vec4(0.0, 0.0, 0.0, 1.0)";
|
|
|
|
}
|
2013-07-27 22:18:41 +00:00
|
|
|
}
|
2013-05-19 22:15:15 +00:00
|
|
|
break;
|
2013-08-20 03:56:43 +00:00
|
|
|
case GE_PROJMAP_NORMALIZED_NORMAL: // Use normalized transformed normal as source
|
2013-05-19 22:15:15 +00:00
|
|
|
if (hasNormal)
|
2013-11-02 10:05:31 +00:00
|
|
|
temp_tc = flipNormal ? "vec4(normalize(-normal), 1.0)" : "vec4(normalize(normal), 1.0)";
|
2013-05-19 22:15:15 +00:00
|
|
|
else
|
|
|
|
temp_tc = "vec4(0.0, 0.0, 1.0, 1.0)";
|
|
|
|
break;
|
2013-08-20 03:56:43 +00:00
|
|
|
case GE_PROJMAP_NORMAL: // Use non-normalized transformed normal as source
|
2013-05-19 22:15:15 +00:00
|
|
|
if (hasNormal)
|
2013-11-02 10:05:31 +00:00
|
|
|
temp_tc = flipNormal ? "vec4(-normal, 1.0)" : "vec4(normal, 1.0)";
|
2013-05-19 22:15:15 +00:00
|
|
|
else
|
|
|
|
temp_tc = "vec4(0.0, 0.0, 1.0, 1.0)";
|
|
|
|
break;
|
|
|
|
}
|
2013-10-01 00:32:33 +00:00
|
|
|
// Transform by texture matrix. XYZ as we are doing projection mapping.
|
2013-07-27 22:18:41 +00:00
|
|
|
WRITE(p, " v_texcoord = (u_texmtx * %s).xyz * vec3(u_uvscaleoffset.xy, 1.0);\n", temp_tc.c_str());
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-08-19 07:43:54 +00:00
|
|
|
case GE_TEXMAP_ENVIRONMENT_MAP: // Shade mapping - use dots from light sources.
|
2014-03-06 14:07:08 +00:00
|
|
|
WRITE(p, " v_texcoord = u_uvscaleoffset.xy * vec2(1.0 + dot(normalize(u_lightpos%i), worldnormal), 1.0 + dot(normalize(u_lightpos%i), worldnormal)) * 0.5;\n", gstate.getUVLS0(), gstate.getUVLS1());
|
2012-12-20 13:10:42 +00:00
|
|
|
break;
|
|
|
|
|
2013-07-29 08:53:06 +00:00
|
|
|
default:
|
2012-12-20 13:10:42 +00:00
|
|
|
// ILLEGAL
|
|
|
|
break;
|
|
|
|
}
|
2013-03-27 10:21:02 +00:00
|
|
|
|
2014-06-08 02:32:19 +00:00
|
|
|
// Will flip in the fragment for GE_TEXMAP_TEXTURE_MATRIX.
|
|
|
|
if (flipV && gstate.getUVGenMode() != GE_TEXMAP_TEXTURE_MATRIX)
|
2013-11-02 10:05:31 +00:00
|
|
|
WRITE(p, " v_texcoord.y = 1.0 - v_texcoord.y;\n");
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-06-24 17:11:04 +00:00
|
|
|
|
|
|
|
// Compute fogdepth
|
|
|
|
if (enableFog)
|
|
|
|
WRITE(p, " v_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n");
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
WRITE(p, "}\n");
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|