2012-10-26 01:23:06 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
Copyright (C) 2006-2009 Robert Beckebans <trebor_7@users.sourceforge.net>
|
|
|
|
|
|
|
|
This file is part of XreaL source code.
|
|
|
|
|
|
|
|
XreaL source code is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
XreaL source code 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 for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with XreaL source code; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
// tr_glsl.c
|
|
|
|
#include "tr_local.h"
|
|
|
|
|
2016-01-19 07:30:36 +00:00
|
|
|
#include "tr_dsa.h"
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-12-04 01:56:52 +00:00
|
|
|
extern const char *fallbackShader_bokeh_vp;
|
|
|
|
extern const char *fallbackShader_bokeh_fp;
|
|
|
|
extern const char *fallbackShader_calclevels4x_vp;
|
|
|
|
extern const char *fallbackShader_calclevels4x_fp;
|
|
|
|
extern const char *fallbackShader_depthblur_vp;
|
|
|
|
extern const char *fallbackShader_depthblur_fp;
|
|
|
|
extern const char *fallbackShader_dlight_vp;
|
|
|
|
extern const char *fallbackShader_dlight_fp;
|
|
|
|
extern const char *fallbackShader_down4x_vp;
|
|
|
|
extern const char *fallbackShader_down4x_fp;
|
|
|
|
extern const char *fallbackShader_fogpass_vp;
|
|
|
|
extern const char *fallbackShader_fogpass_fp;
|
|
|
|
extern const char *fallbackShader_generic_vp;
|
|
|
|
extern const char *fallbackShader_generic_fp;
|
|
|
|
extern const char *fallbackShader_lightall_vp;
|
|
|
|
extern const char *fallbackShader_lightall_fp;
|
|
|
|
extern const char *fallbackShader_pshadow_vp;
|
|
|
|
extern const char *fallbackShader_pshadow_fp;
|
|
|
|
extern const char *fallbackShader_shadowfill_vp;
|
|
|
|
extern const char *fallbackShader_shadowfill_fp;
|
|
|
|
extern const char *fallbackShader_shadowmask_vp;
|
|
|
|
extern const char *fallbackShader_shadowmask_fp;
|
|
|
|
extern const char *fallbackShader_ssao_vp;
|
|
|
|
extern const char *fallbackShader_ssao_fp;
|
|
|
|
extern const char *fallbackShader_texturecolor_vp;
|
|
|
|
extern const char *fallbackShader_texturecolor_fp;
|
|
|
|
extern const char *fallbackShader_tonemap_vp;
|
|
|
|
extern const char *fallbackShader_tonemap_fp;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
typedef struct uniformInfo_s
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int type;
|
|
|
|
}
|
|
|
|
uniformInfo_t;
|
|
|
|
|
|
|
|
// These must be in the same order as in uniform_t in tr_local.h.
|
|
|
|
static uniformInfo_t uniformsInfo[] =
|
|
|
|
{
|
|
|
|
{ "u_DiffuseMap", GLSL_INT },
|
|
|
|
{ "u_LightMap", GLSL_INT },
|
|
|
|
{ "u_NormalMap", GLSL_INT },
|
|
|
|
{ "u_DeluxeMap", GLSL_INT },
|
|
|
|
{ "u_SpecularMap", GLSL_INT },
|
|
|
|
|
|
|
|
{ "u_TextureMap", GLSL_INT },
|
|
|
|
{ "u_LevelsMap", GLSL_INT },
|
2013-09-16 07:54:26 +00:00
|
|
|
{ "u_CubeMap", GLSL_INT },
|
2013-03-21 05:53:30 +00:00
|
|
|
|
|
|
|
{ "u_ScreenImageMap", GLSL_INT },
|
|
|
|
{ "u_ScreenDepthMap", GLSL_INT },
|
|
|
|
|
|
|
|
{ "u_ShadowMap", GLSL_INT },
|
|
|
|
{ "u_ShadowMap2", GLSL_INT },
|
|
|
|
{ "u_ShadowMap3", GLSL_INT },
|
2014-08-01 04:01:57 +00:00
|
|
|
{ "u_ShadowMap4", GLSL_INT },
|
2013-03-21 05:53:30 +00:00
|
|
|
|
|
|
|
{ "u_ShadowMvp", GLSL_MAT16 },
|
|
|
|
{ "u_ShadowMvp2", GLSL_MAT16 },
|
|
|
|
{ "u_ShadowMvp3", GLSL_MAT16 },
|
2014-08-01 04:01:57 +00:00
|
|
|
{ "u_ShadowMvp4", GLSL_MAT16 },
|
2013-03-21 05:53:30 +00:00
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
{ "u_EnableTextures", GLSL_VEC4 },
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
{ "u_DiffuseTexMatrix", GLSL_VEC4 },
|
|
|
|
{ "u_DiffuseTexOffTurb", GLSL_VEC4 },
|
|
|
|
|
|
|
|
{ "u_TCGen0", GLSL_INT },
|
|
|
|
{ "u_TCGen0Vector0", GLSL_VEC3 },
|
|
|
|
{ "u_TCGen0Vector1", GLSL_VEC3 },
|
|
|
|
|
|
|
|
{ "u_DeformGen", GLSL_INT },
|
|
|
|
{ "u_DeformParams", GLSL_FLOAT5 },
|
|
|
|
|
|
|
|
{ "u_ColorGen", GLSL_INT },
|
|
|
|
{ "u_AlphaGen", GLSL_INT },
|
|
|
|
{ "u_Color", GLSL_VEC4 },
|
|
|
|
{ "u_BaseColor", GLSL_VEC4 },
|
|
|
|
{ "u_VertColor", GLSL_VEC4 },
|
|
|
|
|
|
|
|
{ "u_DlightInfo", GLSL_VEC4 },
|
|
|
|
{ "u_LightForward", GLSL_VEC3 },
|
|
|
|
{ "u_LightUp", GLSL_VEC3 },
|
|
|
|
{ "u_LightRight", GLSL_VEC3 },
|
|
|
|
{ "u_LightOrigin", GLSL_VEC4 },
|
2013-10-10 10:41:31 +00:00
|
|
|
{ "u_ModelLightDir", GLSL_VEC3 },
|
2013-03-21 05:53:30 +00:00
|
|
|
{ "u_LightRadius", GLSL_FLOAT },
|
|
|
|
{ "u_AmbientLight", GLSL_VEC3 },
|
|
|
|
{ "u_DirectedLight", GLSL_VEC3 },
|
|
|
|
|
|
|
|
{ "u_PortalRange", GLSL_FLOAT },
|
|
|
|
|
|
|
|
{ "u_FogDistance", GLSL_VEC4 },
|
|
|
|
{ "u_FogDepth", GLSL_VEC4 },
|
|
|
|
{ "u_FogEyeT", GLSL_FLOAT },
|
|
|
|
{ "u_FogColorMask", GLSL_VEC4 },
|
|
|
|
|
|
|
|
{ "u_ModelMatrix", GLSL_MAT16 },
|
|
|
|
{ "u_ModelViewProjectionMatrix", GLSL_MAT16 },
|
|
|
|
|
2014-03-04 05:02:39 +00:00
|
|
|
{ "u_Time", GLSL_FLOAT },
|
|
|
|
{ "u_VertexLerp" , GLSL_FLOAT },
|
|
|
|
{ "u_NormalScale", GLSL_VEC4 },
|
|
|
|
{ "u_SpecularScale", GLSL_VEC4 },
|
2013-03-21 05:53:30 +00:00
|
|
|
|
2013-10-10 10:41:31 +00:00
|
|
|
{ "u_ViewInfo", GLSL_VEC4 },
|
|
|
|
{ "u_ViewOrigin", GLSL_VEC3 },
|
|
|
|
{ "u_LocalViewOrigin", GLSL_VEC3 },
|
|
|
|
{ "u_ViewForward", GLSL_VEC3 },
|
|
|
|
{ "u_ViewLeft", GLSL_VEC3 },
|
|
|
|
{ "u_ViewUp", GLSL_VEC3 },
|
2013-03-21 05:53:30 +00:00
|
|
|
|
|
|
|
{ "u_InvTexRes", GLSL_VEC2 },
|
|
|
|
{ "u_AutoExposureMinMax", GLSL_VEC2 },
|
2013-04-02 07:17:24 +00:00
|
|
|
{ "u_ToneMinAvgMaxLinear", GLSL_VEC3 },
|
|
|
|
|
|
|
|
{ "u_PrimaryLightOrigin", GLSL_VEC4 },
|
|
|
|
{ "u_PrimaryLightColor", GLSL_VEC3 },
|
|
|
|
{ "u_PrimaryLightAmbient", GLSL_VEC3 },
|
2014-03-16 23:29:38 +00:00
|
|
|
{ "u_PrimaryLightRadius", GLSL_FLOAT },
|
|
|
|
|
|
|
|
{ "u_CubeMapInfo", GLSL_VEC4 },
|
2017-07-13 19:03:10 +00:00
|
|
|
|
|
|
|
{ "u_AlphaTest", GLSL_INT },
|
2013-03-21 05:53:30 +00:00
|
|
|
};
|
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GLSL_PRINTLOG_PROGRAM_INFO,
|
|
|
|
GLSL_PRINTLOG_SHADER_INFO,
|
|
|
|
GLSL_PRINTLOG_SHADER_SOURCE
|
|
|
|
}
|
|
|
|
glslPrintLog_t;
|
2013-03-21 05:53:30 +00:00
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
static void GLSL_PrintLog(GLuint programOrShader, glslPrintLog_t type, qboolean developerOnly)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
char *msg;
|
|
|
|
static char msgPart[1024];
|
|
|
|
int maxLength = 0;
|
|
|
|
int i;
|
|
|
|
int printLevel = developerOnly ? PRINT_DEVELOPER : PRINT_ALL;
|
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GLSL_PRINTLOG_PROGRAM_INFO:
|
|
|
|
ri.Printf(printLevel, "Program info log:\n");
|
|
|
|
qglGetProgramiv(programOrShader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLSL_PRINTLOG_SHADER_INFO:
|
|
|
|
ri.Printf(printLevel, "Shader info log:\n");
|
|
|
|
qglGetShaderiv(programOrShader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLSL_PRINTLOG_SHADER_SOURCE:
|
|
|
|
ri.Printf(printLevel, "Shader source:\n");
|
|
|
|
qglGetShaderiv(programOrShader, GL_SHADER_SOURCE_LENGTH, &maxLength);
|
|
|
|
break;
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (maxLength <= 0)
|
|
|
|
{
|
2016-08-05 03:16:22 +00:00
|
|
|
ri.Printf(printLevel, "None.\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxLength < 1023)
|
2016-08-05 03:16:22 +00:00
|
|
|
msg = msgPart;
|
|
|
|
else
|
|
|
|
msg = ri.Malloc(maxLength);
|
|
|
|
|
|
|
|
switch (type)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-08-05 03:16:22 +00:00
|
|
|
case GLSL_PRINTLOG_PROGRAM_INFO:
|
|
|
|
qglGetProgramInfoLog(programOrShader, maxLength, &maxLength, msg);
|
|
|
|
break;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
case GLSL_PRINTLOG_SHADER_INFO:
|
|
|
|
qglGetShaderInfoLog(programOrShader, maxLength, &maxLength, msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLSL_PRINTLOG_SHADER_SOURCE:
|
|
|
|
qglGetShaderSource(programOrShader, maxLength, &maxLength, msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxLength < 1023)
|
|
|
|
{
|
2012-10-26 01:23:06 +00:00
|
|
|
msgPart[maxLength + 1] = '\0';
|
|
|
|
|
|
|
|
ri.Printf(printLevel, "%s\n", msgPart);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-05 03:16:22 +00:00
|
|
|
for(i = 0; i < maxLength; i += 1023)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
Q_strncpyz(msgPart, msg + i, sizeof(msgPart));
|
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
ri.Printf(printLevel, "%s", msgPart);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
ri.Printf(printLevel, "\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-08-05 03:16:22 +00:00
|
|
|
ri.Free(msg);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
static void GLSL_GetShaderHeader( GLenum shaderType, const GLchar *extra, char *dest, int size )
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
float fbufWidthScale, fbufHeightScale;
|
|
|
|
|
|
|
|
dest[0] = '\0';
|
|
|
|
|
|
|
|
// HACK: abuse the GLSL preprocessor to turn GLSL 1.20 shaders into 1.30 ones
|
|
|
|
if(glRefConfig.glslMajorVersion > 1 || (glRefConfig.glslMajorVersion == 1 && glRefConfig.glslMinorVersion >= 30))
|
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
if (glRefConfig.glslMajorVersion > 1 || (glRefConfig.glslMajorVersion == 1 && glRefConfig.glslMinorVersion >= 50))
|
|
|
|
Q_strcat(dest, size, "#version 150\n");
|
|
|
|
else
|
|
|
|
Q_strcat(dest, size, "#version 130\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if(shaderType == GL_VERTEX_SHADER)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
Q_strcat(dest, size, "#define attribute in\n");
|
|
|
|
Q_strcat(dest, size, "#define varying out\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Q_strcat(dest, size, "#define varying in\n");
|
|
|
|
|
|
|
|
Q_strcat(dest, size, "out vec4 out_Color;\n");
|
|
|
|
Q_strcat(dest, size, "#define gl_FragColor out_Color\n");
|
2017-07-13 19:03:10 +00:00
|
|
|
Q_strcat(dest, size, "#define texture2D texture\n");
|
|
|
|
Q_strcat(dest, size, "#define textureCubeLod textureLod\n");
|
|
|
|
Q_strcat(dest, size, "#define shadow2D texture\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Q_strcat(dest, size, "#version 120\n");
|
2017-07-13 19:03:10 +00:00
|
|
|
Q_strcat(dest, size, "#define shadow2D(a,b) shadow2D(a,b).r \n");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: add some macros to avoid extra uniforms and save speed and code maintenance
|
|
|
|
//Q_strcat(dest, size,
|
|
|
|
// va("#ifndef r_SpecularExponent\n#define r_SpecularExponent %f\n#endif\n", r_specularExponent->value));
|
|
|
|
//Q_strcat(dest, size,
|
|
|
|
// va("#ifndef r_SpecularScale\n#define r_SpecularScale %f\n#endif\n", r_specularScale->value));
|
|
|
|
//Q_strcat(dest, size,
|
|
|
|
// va("#ifndef r_NormalScale\n#define r_NormalScale %f\n#endif\n", r_normalScale->value));
|
|
|
|
|
|
|
|
|
2014-01-12 01:42:02 +00:00
|
|
|
Q_strcat(dest, size, "#ifndef M_PI\n#define M_PI 3.14159265358979323846\n#endif\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
//Q_strcat(dest, size, va("#ifndef MAX_SHADOWMAPS\n#define MAX_SHADOWMAPS %i\n#endif\n", MAX_SHADOWMAPS));
|
|
|
|
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef deformGen_t\n"
|
|
|
|
"#define deformGen_t\n"
|
|
|
|
"#define DGEN_WAVE_SIN %i\n"
|
|
|
|
"#define DGEN_WAVE_SQUARE %i\n"
|
|
|
|
"#define DGEN_WAVE_TRIANGLE %i\n"
|
|
|
|
"#define DGEN_WAVE_SAWTOOTH %i\n"
|
|
|
|
"#define DGEN_WAVE_INVERSE_SAWTOOTH %i\n"
|
|
|
|
"#define DGEN_BULGE %i\n"
|
|
|
|
"#define DGEN_MOVE %i\n"
|
|
|
|
"#endif\n",
|
|
|
|
DGEN_WAVE_SIN,
|
|
|
|
DGEN_WAVE_SQUARE,
|
|
|
|
DGEN_WAVE_TRIANGLE,
|
|
|
|
DGEN_WAVE_SAWTOOTH,
|
|
|
|
DGEN_WAVE_INVERSE_SAWTOOTH,
|
|
|
|
DGEN_BULGE,
|
|
|
|
DGEN_MOVE));
|
|
|
|
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef tcGen_t\n"
|
|
|
|
"#define tcGen_t\n"
|
|
|
|
"#define TCGEN_LIGHTMAP %i\n"
|
|
|
|
"#define TCGEN_TEXTURE %i\n"
|
|
|
|
"#define TCGEN_ENVIRONMENT_MAPPED %i\n"
|
|
|
|
"#define TCGEN_FOG %i\n"
|
|
|
|
"#define TCGEN_VECTOR %i\n"
|
|
|
|
"#endif\n",
|
|
|
|
TCGEN_LIGHTMAP,
|
|
|
|
TCGEN_TEXTURE,
|
|
|
|
TCGEN_ENVIRONMENT_MAPPED,
|
|
|
|
TCGEN_FOG,
|
|
|
|
TCGEN_VECTOR));
|
|
|
|
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef colorGen_t\n"
|
|
|
|
"#define colorGen_t\n"
|
|
|
|
"#define CGEN_LIGHTING_DIFFUSE %i\n"
|
|
|
|
"#endif\n",
|
|
|
|
CGEN_LIGHTING_DIFFUSE));
|
|
|
|
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef alphaGen_t\n"
|
|
|
|
"#define alphaGen_t\n"
|
|
|
|
"#define AGEN_LIGHTING_SPECULAR %i\n"
|
|
|
|
"#define AGEN_PORTAL %i\n"
|
|
|
|
"#endif\n",
|
|
|
|
AGEN_LIGHTING_SPECULAR,
|
2013-09-24 10:29:49 +00:00
|
|
|
AGEN_PORTAL));
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef texenv_t\n"
|
|
|
|
"#define texenv_t\n"
|
|
|
|
"#define TEXENV_MODULATE %i\n"
|
|
|
|
"#define TEXENV_ADD %i\n"
|
|
|
|
"#define TEXENV_REPLACE %i\n"
|
|
|
|
"#endif\n",
|
|
|
|
GL_MODULATE,
|
|
|
|
GL_ADD,
|
|
|
|
GL_REPLACE));
|
|
|
|
|
|
|
|
fbufWidthScale = 1.0f / ((float)glConfig.vidWidth);
|
|
|
|
fbufHeightScale = 1.0f / ((float)glConfig.vidHeight);
|
|
|
|
Q_strcat(dest, size,
|
|
|
|
va("#ifndef r_FBufScale\n#define r_FBufScale vec2(%f, %f)\n#endif\n", fbufWidthScale, fbufHeightScale));
|
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
if (r_pbr->integer)
|
|
|
|
Q_strcat(dest, size, "#define USE_PBR\n");
|
2014-04-16 12:26:03 +00:00
|
|
|
|
2016-06-08 10:36:15 +00:00
|
|
|
if (r_cubeMapping->integer)
|
|
|
|
{
|
|
|
|
int cubeMipSize = r_cubemapSize->integer;
|
|
|
|
int numRoughnessMips = 0;
|
|
|
|
|
|
|
|
while (cubeMipSize)
|
|
|
|
{
|
|
|
|
cubeMipSize >>= 1;
|
|
|
|
numRoughnessMips++;
|
|
|
|
}
|
|
|
|
numRoughnessMips = MAX(1, numRoughnessMips - 2);
|
|
|
|
Q_strcat(dest, size, va("#define ROUGHNESS_MIPS float(%d)\n", numRoughnessMips));
|
|
|
|
}
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
if (extra)
|
|
|
|
{
|
|
|
|
Q_strcat(dest, size, extra);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK we added a lot of stuff but if we do something bad in the GLSL shaders then we want the proper line
|
|
|
|
// so we have to reset the line counting
|
|
|
|
Q_strcat(dest, size, "#line 0\n");
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
static int GLSL_CompileGPUShader(GLuint program, GLuint *prevShader, const GLchar *buffer, int size, GLenum shaderType)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
GLint compiled;
|
2016-07-25 04:29:13 +00:00
|
|
|
GLuint shader;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
shader = qglCreateShader(shaderType);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
qglShaderSource(shader, 1, (const GLchar **)&buffer, &size);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
// compile shader
|
2016-07-25 04:29:13 +00:00
|
|
|
qglCompileShader(shader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
// check if shader compiled
|
2016-07-25 04:29:13 +00:00
|
|
|
qglGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
|
2012-10-26 01:23:06 +00:00
|
|
|
if(!compiled)
|
|
|
|
{
|
2016-08-05 03:16:22 +00:00
|
|
|
GLSL_PrintLog(shader, GLSL_PRINTLOG_SHADER_SOURCE, qfalse);
|
|
|
|
GLSL_PrintLog(shader, GLSL_PRINTLOG_SHADER_INFO, qfalse);
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Error(ERR_DROP, "Couldn't compile shader");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*prevShader)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
qglDetachShader(program, *prevShader);
|
|
|
|
qglDeleteShader(*prevShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// attach shader to program
|
2016-07-25 04:29:13 +00:00
|
|
|
qglAttachShader(program, shader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
*prevShader = shader;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GLSL_LoadGPUShaderText(const char *name, const char *fallback,
|
2012-12-04 01:56:52 +00:00
|
|
|
GLenum shaderType, char *dest, int destSize)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
char filename[MAX_QPATH];
|
2016-07-25 04:29:13 +00:00
|
|
|
GLchar *buffer = NULL;
|
|
|
|
const GLchar *shaderText = NULL;
|
2012-10-26 01:23:06 +00:00
|
|
|
int size;
|
|
|
|
int result;
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if(shaderType == GL_VERTEX_SHADER)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
Com_sprintf(filename, sizeof(filename), "glsl/%s_vp.glsl", name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Com_sprintf(filename, sizeof(filename), "glsl/%s_fp.glsl", name);
|
|
|
|
}
|
|
|
|
|
2014-07-26 04:34:29 +00:00
|
|
|
if ( r_externalGLSL->integer ) {
|
|
|
|
size = ri.FS_ReadFile(filename, (void **)&buffer);
|
|
|
|
} else {
|
|
|
|
size = 0;
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
if(!buffer)
|
|
|
|
{
|
|
|
|
if (fallback)
|
|
|
|
{
|
2014-07-26 04:34:29 +00:00
|
|
|
ri.Printf(PRINT_DEVELOPER, "...loading built-in '%s'\n", filename);
|
2012-10-26 01:23:06 +00:00
|
|
|
shaderText = fallback;
|
|
|
|
size = strlen(shaderText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-26 04:34:29 +00:00
|
|
|
ri.Printf(PRINT_DEVELOPER, "couldn't load '%s'\n", filename);
|
2012-10-26 01:23:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-26 04:34:29 +00:00
|
|
|
ri.Printf(PRINT_DEVELOPER, "...loading '%s'\n", filename);
|
2012-10-26 01:23:06 +00:00
|
|
|
shaderText = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > destSize)
|
|
|
|
{
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Q_strncpyz(dest, shaderText, size + 1);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
ri.FS_FreeFile(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
static void GLSL_LinkProgram(GLuint program)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
GLint linked;
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
qglLinkProgram(program);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
qglGetProgramiv(program, GL_LINK_STATUS, &linked);
|
2012-10-26 01:23:06 +00:00
|
|
|
if(!linked)
|
|
|
|
{
|
2016-08-05 03:16:22 +00:00
|
|
|
GLSL_PrintLog(program, GLSL_PRINTLOG_PROGRAM_INFO, qfalse);
|
2013-05-30 20:32:44 +00:00
|
|
|
ri.Error(ERR_DROP, "shaders failed to link");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
static void GLSL_ShowProgramUniforms(GLuint program)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
int i, count, size;
|
|
|
|
GLenum type;
|
|
|
|
char uniformName[1000];
|
|
|
|
|
|
|
|
// query the number of active uniforms
|
2016-07-25 04:29:13 +00:00
|
|
|
qglGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
// Loop over each of the active uniforms, and set their value
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
qglGetActiveUniform(program, i, sizeof(uniformName), NULL, &size, &type, uniformName);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
ri.Printf(PRINT_DEVELOPER, "active uniform: '%s'\n", uniformName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
static int GLSL_InitGPUShader2(shaderProgram_t * program, const char *name, int attribs, const char *vpCode, const char *fpCode)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf(PRINT_DEVELOPER, "------- GPU shader -------\n");
|
|
|
|
|
|
|
|
if(strlen(name) >= MAX_QPATH)
|
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_DROP, "GLSL_InitGPUShader2: \"%s\" is too long", name);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_strncpyz(program->name, name, sizeof(program->name));
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
program->program = qglCreateProgram();
|
2012-10-26 01:23:06 +00:00
|
|
|
program->attribs = attribs;
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if (!(GLSL_CompileGPUShader(program->program, &program->vertexShader, vpCode, strlen(vpCode), GL_VERTEX_SHADER)))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
ri.Printf(PRINT_ALL, "GLSL_InitGPUShader2: Unable to load \"%s\" as GL_VERTEX_SHADER\n", name);
|
|
|
|
qglDeleteProgram(program->program);
|
2012-10-26 01:23:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fpCode)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
if(!(GLSL_CompileGPUShader(program->program, &program->fragmentShader, fpCode, strlen(fpCode), GL_FRAGMENT_SHADER)))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
ri.Printf(PRINT_ALL, "GLSL_InitGPUShader2: Unable to load \"%s\" as GL_FRAGMENT_SHADER\n", name);
|
|
|
|
qglDeleteProgram(program->program);
|
2012-10-26 01:23:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(attribs & ATTR_POSITION)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_POSITION, "attr_Position");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_TEXCOORD)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_TEXCOORD, "attr_TexCoord0");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_LIGHTCOORD)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_LIGHTCOORD, "attr_TexCoord1");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
// if(attribs & ATTR_TEXCOORD2)
|
2016-07-25 04:29:13 +00:00
|
|
|
// qglBindAttribLocation(program->program, ATTR_INDEX_TEXCOORD2, "attr_TexCoord2");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
// if(attribs & ATTR_TEXCOORD3)
|
2016-07-25 04:29:13 +00:00
|
|
|
// qglBindAttribLocation(program->program, ATTR_INDEX_TEXCOORD3, "attr_TexCoord3");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_TANGENT)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_TANGENT, "attr_Tangent");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_NORMAL)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_NORMAL, "attr_Normal");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_COLOR)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_COLOR, "attr_Color");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_PAINTCOLOR)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_PAINTCOLOR, "attr_PaintColor");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_LIGHTDIRECTION)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_LIGHTDIRECTION, "attr_LightDirection");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_POSITION2)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_POSITION2, "attr_Position2");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_NORMAL2)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_NORMAL2, "attr_Normal2");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(attribs & ATTR_TANGENT2)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglBindAttribLocation(program->program, ATTR_INDEX_TANGENT2, "attr_Tangent2");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_LinkProgram(program->program);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GLSL_InitGPUShader(shaderProgram_t * program, const char *name,
|
2016-07-25 04:29:13 +00:00
|
|
|
int attribs, qboolean fragmentShader, const GLchar *extra, qboolean addHeader,
|
2013-03-21 05:53:30 +00:00
|
|
|
const char *fallback_vp, const char *fallback_fp)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
char vpCode[32000];
|
|
|
|
char fpCode[32000];
|
|
|
|
char *postHeader;
|
|
|
|
int size;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
size = sizeof(vpCode);
|
|
|
|
if (addHeader)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
GLSL_GetShaderHeader(GL_VERTEX_SHADER, extra, vpCode, size);
|
2012-10-26 01:23:06 +00:00
|
|
|
postHeader = &vpCode[strlen(vpCode)];
|
|
|
|
size -= strlen(vpCode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
postHeader = &vpCode[0];
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if (!GLSL_LoadGPUShaderText(name, fallback_vp, GL_VERTEX_SHADER, postHeader, size))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fragmentShader)
|
|
|
|
{
|
|
|
|
size = sizeof(fpCode);
|
|
|
|
if (addHeader)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
GLSL_GetShaderHeader(GL_FRAGMENT_SHADER, extra, fpCode, size);
|
2012-10-26 01:23:06 +00:00
|
|
|
postHeader = &fpCode[strlen(fpCode)];
|
|
|
|
size -= strlen(fpCode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
postHeader = &fpCode[0];
|
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if (!GLSL_LoadGPUShaderText(name, fallback_fp, GL_FRAGMENT_SHADER, postHeader, size))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
result = GLSL_InitGPUShader2(program, name, attribs, vpCode, fragmentShader ? fpCode : NULL);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
void GLSL_InitUniforms(shaderProgram_t *program)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-03-21 05:53:30 +00:00
|
|
|
int i, size;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLint *uniforms = program->uniforms;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < UNIFORM_COUNT; i++)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
uniforms[i] = qglGetUniformLocation(program->program, uniformsInfo[i].name);
|
2013-03-21 05:53:30 +00:00
|
|
|
|
|
|
|
if (uniforms[i] == -1)
|
|
|
|
continue;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
program->uniformBufferOffsets[i] = size;
|
|
|
|
|
|
|
|
switch(uniformsInfo[i].type)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-03-21 05:53:30 +00:00
|
|
|
case GLSL_INT:
|
|
|
|
size += sizeof(GLint);
|
|
|
|
break;
|
|
|
|
case GLSL_FLOAT:
|
|
|
|
size += sizeof(GLfloat);
|
|
|
|
break;
|
|
|
|
case GLSL_FLOAT5:
|
|
|
|
size += sizeof(vec_t) * 5;
|
|
|
|
break;
|
|
|
|
case GLSL_VEC2:
|
|
|
|
size += sizeof(vec_t) * 2;
|
|
|
|
break;
|
|
|
|
case GLSL_VEC3:
|
|
|
|
size += sizeof(vec_t) * 3;
|
|
|
|
break;
|
|
|
|
case GLSL_VEC4:
|
|
|
|
size += sizeof(vec_t) * 4;
|
|
|
|
break;
|
|
|
|
case GLSL_MAT16:
|
|
|
|
size += sizeof(vec_t) * 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-21 05:53:30 +00:00
|
|
|
|
|
|
|
program->uniformBuffer = ri.Malloc(size);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_FinishGPUShader(shaderProgram_t *program)
|
|
|
|
{
|
|
|
|
GLSL_ShowProgramUniforms(program->program);
|
|
|
|
GL_CheckErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformInt(shaderProgram_t *program, int uniformNum, GLint value)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
GLint *compare = (GLint *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_INT)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformInt: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == *compare)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*compare = value;
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform1iEXT(program->program, uniforms[uniformNum], value);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformFloat(shaderProgram_t *program, int uniformNum, GLfloat value)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
GLfloat *compare = (GLfloat *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_FLOAT)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformFloat: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == *compare)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*compare = value;
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform1fEXT(program->program, uniforms[uniformNum], value);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformVec2(shaderProgram_t *program, int uniformNum, const vec2_t v)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
vec_t *compare = (float *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_VEC2)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformVec2: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v[0] == compare[0] && v[1] == compare[1])
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
compare[0] = v[0];
|
|
|
|
compare[1] = v[1];
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform2fEXT(program->program, uniforms[uniformNum], v[0], v[1]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformVec3(shaderProgram_t *program, int uniformNum, const vec3_t v)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
vec_t *compare = (float *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_VEC3)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformVec3: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VectorCompare(v, compare))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorCopy(v, compare);
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform3fEXT(program->program, uniforms[uniformNum], v[0], v[1], v[2]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformVec4(shaderProgram_t *program, int uniformNum, const vec4_t v)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
vec_t *compare = (float *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_VEC4)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformVec4: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VectorCompare4(v, compare))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorCopy4(v, compare);
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform4fEXT(program->program, uniforms[uniformNum], v[0], v[1], v[2], v[3]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_SetUniformFloat5(shaderProgram_t *program, int uniformNum, const vec5_t v)
|
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
vec_t *compare = (float *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_FLOAT5)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformFloat5: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VectorCompare5(v, compare))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorCopy5(v, compare);
|
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniform1fvEXT(program->program, uniforms[uniformNum], 5, v);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
void GLSL_SetUniformMat4(shaderProgram_t *program, int uniformNum, const mat4_t matrix)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
GLint *uniforms = program->uniforms;
|
|
|
|
vec_t *compare = (float *)(program->uniformBuffer + program->uniformBufferOffsets[uniformNum]);
|
|
|
|
|
|
|
|
if (uniforms[uniformNum] == -1)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (uniformsInfo[uniformNum].type != GLSL_MAT16)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-11-19 11:23:50 +00:00
|
|
|
ri.Printf( PRINT_WARNING, "GLSL_SetUniformMat4: wrong type for uniform %i in program %s\n", uniformNum, program->name);
|
2012-10-26 01:23:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
if (Mat4Compare(matrix, compare))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
Mat4Copy(matrix, compare);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglProgramUniformMatrix4fvEXT(program->program, uniforms[uniformNum], 1, GL_FALSE, matrix);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_DeleteGPUShader(shaderProgram_t *program)
|
|
|
|
{
|
|
|
|
if(program->program)
|
|
|
|
{
|
|
|
|
if (program->vertexShader)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
qglDetachShader(program->program, program->vertexShader);
|
|
|
|
qglDeleteShader(program->vertexShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (program->fragmentShader)
|
|
|
|
{
|
2016-07-25 04:29:13 +00:00
|
|
|
qglDetachShader(program->program, program->fragmentShader);
|
|
|
|
qglDeleteShader(program->fragmentShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
qglDeleteProgram(program->program);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (program->uniformBuffer)
|
|
|
|
{
|
|
|
|
ri.Free(program->uniformBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Com_Memset(program, 0, sizeof(*program));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_InitGPUShaders(void)
|
|
|
|
{
|
|
|
|
int startTime, endTime;
|
|
|
|
int i;
|
|
|
|
char extradefines[1024];
|
|
|
|
int attribs;
|
|
|
|
int numGenShaders = 0, numLightShaders = 0, numEtcShaders = 0;
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, "------- GLSL_InitGPUShaders -------\n");
|
|
|
|
|
2013-01-24 22:53:08 +00:00
|
|
|
R_IssuePendingRenderCommands();
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
startTime = ri.Milliseconds();
|
|
|
|
|
|
|
|
for (i = 0; i < GENERICDEF_COUNT; i++)
|
|
|
|
{
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_NORMAL | ATTR_COLOR;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (i & GENERICDEF_USE_DEFORM_VERTEXES)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_DEFORM_VERTEXES\n");
|
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (i & GENERICDEF_USE_TCGEN_AND_TCMOD)
|
|
|
|
{
|
2012-10-26 01:23:06 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define USE_TCGEN\n");
|
2012-11-14 10:56:31 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define USE_TCMOD\n");
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (i & GENERICDEF_USE_VERTEX_ANIMATION)
|
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_VERTEX_ANIMATION\n");
|
|
|
|
attribs |= ATTR_POSITION2 | ATTR_NORMAL2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i & GENERICDEF_USE_FOG)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_FOG\n");
|
|
|
|
|
|
|
|
if (i & GENERICDEF_USE_RGBAGEN)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_RGBAGEN\n");
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.genericShader[i], "generic", attribs, qtrue, extradefines, qtrue, fallbackShader_generic_vp, fallbackShader_generic_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load generic shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.genericShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.genericShader[i], UNIFORM_DIFFUSEMAP, TB_DIFFUSEMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.genericShader[i], UNIFORM_LIGHTMAP, TB_LIGHTMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.genericShader[i]);
|
|
|
|
|
|
|
|
numGenShaders++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.textureColorShader, "texturecolor", attribs, qtrue, extradefines, qtrue, fallbackShader_texturecolor_vp, fallbackShader_texturecolor_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load texturecolor shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.textureColorShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.textureColorShader, UNIFORM_TEXTUREMAP, TB_DIFFUSEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.textureColorShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
for (i = 0; i < FOGDEF_COUNT; i++)
|
|
|
|
{
|
|
|
|
attribs = ATTR_POSITION | ATTR_POSITION2 | ATTR_NORMAL | ATTR_NORMAL2 | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (i & FOGDEF_USE_DEFORM_VERTEXES)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_DEFORM_VERTEXES\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (i & FOGDEF_USE_VERTEX_ANIMATION)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_VERTEX_ANIMATION\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.fogShader[i], "fogpass", attribs, qtrue, extradefines, qtrue, fallbackShader_fogpass_vp, fallbackShader_fogpass_fp))
|
2012-11-14 10:56:31 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load fogpass shader!");
|
2012-11-14 10:56:31 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.fogShader[i]);
|
2012-11-14 10:56:31 +00:00
|
|
|
GLSL_FinishGPUShader(&tr.fogShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
numEtcShaders++;
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
for (i = 0; i < DLIGHTDEF_COUNT; i++)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2012-11-14 10:56:31 +00:00
|
|
|
attribs = ATTR_POSITION | ATTR_NORMAL | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (i & DLIGHTDEF_USE_DEFORM_VERTEXES)
|
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_DEFORM_VERTEXES\n");
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.dlightShader[i], "dlight", attribs, qtrue, extradefines, qtrue, fallbackShader_dlight_vp, fallbackShader_dlight_fp))
|
2012-11-14 10:56:31 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load dlight shader!");
|
2012-11-14 10:56:31 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.dlightShader[i]);
|
2012-11-14 10:56:31 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.dlightShader[i], UNIFORM_DIFFUSEMAP, TB_DIFFUSEMAP);
|
2012-11-14 10:56:31 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.dlightShader[i]);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < LIGHTDEF_COUNT; i++)
|
|
|
|
{
|
2013-11-04 03:34:22 +00:00
|
|
|
int lightType = i & LIGHTDEF_LIGHTTYPE_MASK;
|
|
|
|
qboolean fastLight = !(r_normalMapping->integer || r_specularMapping->integer);
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
// skip impossible combos
|
|
|
|
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !r_parallaxMapping->integer)
|
|
|
|
continue;
|
|
|
|
|
2017-01-27 03:58:28 +00:00
|
|
|
if ((i & LIGHTDEF_USE_SHADOWMAP) && (!lightType || !r_sunlightMode->integer))
|
2012-10-26 01:23:06 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD | ATTR_COLOR | ATTR_NORMAL;
|
|
|
|
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (r_dlightMode->integer >= 2)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SHADOWMAP\n");
|
|
|
|
|
2015-12-15 11:23:55 +00:00
|
|
|
if (glRefConfig.swizzleNormalmap)
|
2012-10-26 01:23:06 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define SWIZZLE_NORMALMAP\n");
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
if (lightType)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_LIGHT\n");
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
if (fastLight)
|
2012-10-26 01:23:06 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define USE_FAST_LIGHT\n");
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
switch (lightType)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
case LIGHTDEF_USE_LIGHTMAP:
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_LIGHTMAP\n");
|
2013-11-04 03:34:22 +00:00
|
|
|
if (r_deluxeMapping->integer && !fastLight)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_DELUXEMAP\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
attribs |= ATTR_LIGHTCOORD | ATTR_LIGHTDIRECTION;
|
|
|
|
break;
|
|
|
|
case LIGHTDEF_USE_LIGHT_VECTOR:
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_LIGHT_VECTOR\n");
|
|
|
|
break;
|
|
|
|
case LIGHTDEF_USE_LIGHT_VERTEX:
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_LIGHT_VERTEX\n");
|
|
|
|
attribs |= ATTR_LIGHTDIRECTION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
if (r_normalMapping->integer)
|
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_NORMALMAP\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-11-13 03:21:51 +00:00
|
|
|
attribs |= ATTR_TANGENT;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer)
|
2014-11-11 06:11:36 +00:00
|
|
|
{
|
2013-11-04 03:34:22 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n");
|
2014-11-11 06:11:36 +00:00
|
|
|
if (r_parallaxMapping->integer > 1)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_RELIEFMAP\n");
|
|
|
|
}
|
2013-11-04 03:34:22 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
if (r_specularMapping->integer)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n");
|
|
|
|
|
|
|
|
if (r_cubeMapping->integer)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n");
|
2016-03-07 11:30:16 +00:00
|
|
|
|
|
|
|
switch (r_glossType->integer)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
Q_strcat(extradefines, 1024, "#define GLOSS_IS_GLOSS\n");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Q_strcat(extradefines, 1024, "#define GLOSS_IS_SMOOTHNESS\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
Q_strcat(extradefines, 1024, "#define GLOSS_IS_ROUGHNESS\n");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
Q_strcat(extradefines, 1024, "#define GLOSS_IS_SHININESS\n");
|
|
|
|
break;
|
|
|
|
}
|
2013-11-04 03:34:22 +00:00
|
|
|
}
|
2013-09-16 07:54:26 +00:00
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
if (i & LIGHTDEF_USE_SHADOWMAP)
|
2013-04-02 07:17:24 +00:00
|
|
|
{
|
2012-10-26 01:23:06 +00:00
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SHADOWMAP\n");
|
|
|
|
|
2013-04-02 07:17:24 +00:00
|
|
|
if (r_sunlightMode->integer == 1)
|
|
|
|
Q_strcat(extradefines, 1024, "#define SHADOWMAP_MODULATE\n");
|
|
|
|
else if (r_sunlightMode->integer == 2)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_PRIMARY_LIGHT\n");
|
|
|
|
}
|
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (i & LIGHTDEF_USE_TCGEN_AND_TCMOD)
|
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_TCGEN\n");
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_TCMOD\n");
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (i & LIGHTDEF_ENTITY)
|
|
|
|
{
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_VERTEX_ANIMATION\n#define USE_MODELMATRIX\n");
|
|
|
|
attribs |= ATTR_POSITION2 | ATTR_NORMAL2;
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
if (r_normalMapping->integer)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-11-13 03:21:51 +00:00
|
|
|
attribs |= ATTR_TANGENT2;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.lightallShader[i], "lightall", attribs, qtrue, extradefines, qtrue, fallbackShader_lightall_vp, fallbackShader_lightall_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load lightall shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.lightallShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_DIFFUSEMAP, TB_DIFFUSEMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_LIGHTMAP, TB_LIGHTMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_NORMALMAP, TB_NORMALMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_DELUXEMAP, TB_DELUXEMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_SPECULARMAP, TB_SPECULARMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_SHADOWMAP, TB_SHADOWMAP);
|
2013-09-16 07:54:26 +00:00
|
|
|
GLSL_SetUniformInt(&tr.lightallShader[i], UNIFORM_CUBEMAP, TB_CUBEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.lightallShader[i]);
|
|
|
|
|
|
|
|
numLightShaders++;
|
|
|
|
}
|
2014-01-12 01:42:02 +00:00
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
attribs = ATTR_POSITION | ATTR_POSITION2 | ATTR_NORMAL | ATTR_NORMAL2 | ATTR_TEXCOORD;
|
|
|
|
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.shadowmapShader, "shadowfill", attribs, qtrue, extradefines, qtrue, fallbackShader_shadowfill_vp, fallbackShader_shadowfill_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load shadowfill shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.shadowmapShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
GLSL_FinishGPUShader(&tr.shadowmapShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_NORMAL;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_PCF\n#define USE_DISCARD\n");
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.pshadowShader, "pshadow", attribs, qtrue, extradefines, qtrue, fallbackShader_pshadow_vp, fallbackShader_pshadow_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load pshadow shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.pshadowShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.pshadowShader, UNIFORM_SHADOWMAP, TB_DIFFUSEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.pshadowShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.down4xShader, "down4x", attribs, qtrue, extradefines, qtrue, fallbackShader_down4x_vp, fallbackShader_down4x_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load down4x shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.down4xShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.down4xShader, UNIFORM_TEXTUREMAP, TB_DIFFUSEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.down4xShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.bokehShader, "bokeh", attribs, qtrue, extradefines, qtrue, fallbackShader_bokeh_vp, fallbackShader_bokeh_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load bokeh shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.bokehShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.bokehShader, UNIFORM_TEXTUREMAP, TB_DIFFUSEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.bokehShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.tonemapShader, "tonemap", attribs, qtrue, extradefines, qtrue, fallbackShader_tonemap_vp, fallbackShader_tonemap_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load tonemap shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.tonemapShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.tonemapShader, UNIFORM_TEXTUREMAP, TB_COLORMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.tonemapShader, UNIFORM_LEVELSMAP, TB_LEVELSMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.tonemapShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
Q_strcat(extradefines, 1024, "#define FIRST_PASS\n");
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.calclevels4xShader[i], "calclevels4x", attribs, qtrue, extradefines, qtrue, fallbackShader_calclevels4x_vp, fallbackShader_calclevels4x_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load calclevels4x shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.calclevels4xShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.calclevels4xShader[i], UNIFORM_TEXTUREMAP, TB_DIFFUSEMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.calclevels4xShader[i]);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (r_shadowFilter->integer >= 1)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SHADOW_FILTER\n");
|
|
|
|
|
|
|
|
if (r_shadowFilter->integer >= 2)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SHADOW_FILTER2\n");
|
|
|
|
|
2014-08-01 04:01:57 +00:00
|
|
|
if (r_shadowCascadeZFar->integer != 0)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_SHADOW_CASCADE\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2014-01-12 01:42:02 +00:00
|
|
|
Q_strcat(extradefines, 1024, va("#define r_shadowMapSize %f\n", r_shadowMapSize->value));
|
2012-10-26 01:23:06 +00:00
|
|
|
Q_strcat(extradefines, 1024, va("#define r_shadowCascadeZFar %f\n", r_shadowCascadeZFar->value));
|
|
|
|
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.shadowmaskShader, "shadowmask", attribs, qtrue, extradefines, qtrue, fallbackShader_shadowmask_vp, fallbackShader_shadowmask_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load shadowmask shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.shadowmaskShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.shadowmaskShader, UNIFORM_SCREENDEPTHMAP, TB_COLORMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.shadowmaskShader, UNIFORM_SHADOWMAP, TB_SHADOWMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.shadowmaskShader, UNIFORM_SHADOWMAP2, TB_SHADOWMAP2);
|
|
|
|
GLSL_SetUniformInt(&tr.shadowmaskShader, UNIFORM_SHADOWMAP3, TB_SHADOWMAP3);
|
2014-08-01 04:01:57 +00:00
|
|
|
GLSL_SetUniformInt(&tr.shadowmaskShader, UNIFORM_SHADOWMAP4, TB_SHADOWMAP4);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.shadowmaskShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.ssaoShader, "ssao", attribs, qtrue, extradefines, qtrue, fallbackShader_ssao_vp, fallbackShader_ssao_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load ssao shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.ssaoShader);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.ssaoShader, UNIFORM_SCREENDEPTHMAP, TB_COLORMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.ssaoShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
|
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (i & 1)
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_VERTICAL_BLUR\n");
|
|
|
|
else
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_HORIZONTAL_BLUR\n");
|
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
if (!(i & 2))
|
|
|
|
Q_strcat(extradefines, 1024, "#define USE_DEPTH\n");
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
if (!GLSL_InitGPUShader(&tr.depthBlurShader[i], "depthBlur", attribs, qtrue, extradefines, qtrue, fallbackShader_depthblur_vp, fallbackShader_depthblur_fp))
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-11 22:37:00 +00:00
|
|
|
ri.Error(ERR_FATAL, "Could not load depthBlur shader!");
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_InitUniforms(&tr.depthBlurShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformInt(&tr.depthBlurShader[i], UNIFORM_SCREENIMAGEMAP, TB_COLORMAP);
|
|
|
|
GLSL_SetUniformInt(&tr.depthBlurShader[i], UNIFORM_SCREENDEPTHMAP, TB_LIGHTMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.depthBlurShader[i]);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
}
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if 0
|
|
|
|
attribs = ATTR_POSITION | ATTR_TEXCOORD;
|
|
|
|
extradefines[0] = '\0';
|
|
|
|
|
|
|
|
if (!GLSL_InitGPUShader(&tr.testcubeShader, "testcube", attribs, qtrue, extradefines, qtrue, NULL, NULL))
|
|
|
|
{
|
|
|
|
ri.Error(ERR_FATAL, "Could not load testcube shader!");
|
|
|
|
}
|
|
|
|
|
|
|
|
GLSL_InitUniforms(&tr.testcubeShader);
|
|
|
|
|
|
|
|
GLSL_SetUniformInt(&tr.testcubeShader, UNIFORM_TEXTUREMAP, TB_COLORMAP);
|
|
|
|
|
|
|
|
GLSL_FinishGPUShader(&tr.testcubeShader);
|
|
|
|
|
|
|
|
numEtcShaders++;
|
|
|
|
#endif
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
endTime = ri.Milliseconds();
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, "loaded %i GLSL shaders (%i gen %i light %i etc) in %5.2f seconds\n",
|
|
|
|
numGenShaders + numLightShaders + numEtcShaders, numGenShaders, numLightShaders,
|
|
|
|
numEtcShaders, (endTime - startTime) / 1000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLSL_ShutdownGPUShaders(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, "------- GLSL_ShutdownGPUShaders -------\n");
|
|
|
|
|
2014-10-14 04:57:06 +00:00
|
|
|
for (i = 0; i < ATTR_INDEX_COUNT; i++)
|
2016-07-25 04:29:13 +00:00
|
|
|
qglDisableVertexAttribArray(i);
|
2014-10-14 04:57:06 +00:00
|
|
|
|
2016-01-19 07:30:36 +00:00
|
|
|
GL_BindNullProgram();
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < GENERICDEF_COUNT; i++)
|
|
|
|
GLSL_DeleteGPUShader(&tr.genericShader[i]);
|
|
|
|
|
|
|
|
GLSL_DeleteGPUShader(&tr.textureColorShader);
|
2012-11-14 10:56:31 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < FOGDEF_COUNT; i++)
|
|
|
|
GLSL_DeleteGPUShader(&tr.fogShader[i]);
|
|
|
|
|
|
|
|
for ( i = 0; i < DLIGHTDEF_COUNT; i++)
|
|
|
|
GLSL_DeleteGPUShader(&tr.dlightShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < LIGHTDEF_COUNT; i++)
|
|
|
|
GLSL_DeleteGPUShader(&tr.lightallShader[i]);
|
|
|
|
|
|
|
|
GLSL_DeleteGPUShader(&tr.shadowmapShader);
|
|
|
|
GLSL_DeleteGPUShader(&tr.pshadowShader);
|
|
|
|
GLSL_DeleteGPUShader(&tr.down4xShader);
|
2013-04-11 04:21:40 +00:00
|
|
|
GLSL_DeleteGPUShader(&tr.bokehShader);
|
|
|
|
GLSL_DeleteGPUShader(&tr.tonemapShader);
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
for ( i = 0; i < 2; i++)
|
|
|
|
GLSL_DeleteGPUShader(&tr.calclevels4xShader[i]);
|
|
|
|
|
2013-04-11 04:21:40 +00:00
|
|
|
GLSL_DeleteGPUShader(&tr.shadowmaskShader);
|
|
|
|
GLSL_DeleteGPUShader(&tr.ssaoShader);
|
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
for ( i = 0; i < 4; i++)
|
2013-04-11 04:21:40 +00:00
|
|
|
GLSL_DeleteGPUShader(&tr.depthBlurShader[i]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GLSL_BindProgram(shaderProgram_t * program)
|
|
|
|
{
|
2016-01-19 07:30:36 +00:00
|
|
|
GLuint programObject = program ? program->program : 0;
|
|
|
|
char *name = program ? program->name : "NULL";
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(r_logFile->integer)
|
|
|
|
{
|
|
|
|
// don't just call LogComment, or we will get a call to va() every frame!
|
2016-01-19 07:30:36 +00:00
|
|
|
GLimp_LogComment(va("--- GLSL_BindProgram( %s ) ---\n", name));
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 04:29:13 +00:00
|
|
|
if (GL_UseProgram(programObject))
|
2012-10-26 01:23:06 +00:00
|
|
|
backEnd.pc.c_glslShaderBinds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
shaderProgram_t *GLSL_GetGenericShaderProgram(int stage)
|
|
|
|
{
|
|
|
|
shaderStage_t *pStage = tess.xstages[stage];
|
|
|
|
int shaderAttribs = 0;
|
|
|
|
|
|
|
|
if (tess.fogNum && pStage->adjustColorsForFog)
|
|
|
|
{
|
|
|
|
shaderAttribs |= GENERICDEF_USE_FOG;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pStage->rgbGen)
|
|
|
|
{
|
|
|
|
case CGEN_LIGHTING_DIFFUSE:
|
|
|
|
shaderAttribs |= GENERICDEF_USE_RGBAGEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pStage->alphaGen)
|
|
|
|
{
|
|
|
|
case AGEN_LIGHTING_SPECULAR:
|
|
|
|
case AGEN_PORTAL:
|
|
|
|
shaderAttribs |= GENERICDEF_USE_RGBAGEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pStage->bundle[0].tcGen != TCGEN_TEXTURE)
|
|
|
|
{
|
2012-11-14 10:56:31 +00:00
|
|
|
shaderAttribs |= GENERICDEF_USE_TCGEN_AND_TCMOD;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tess.shader->numDeforms && !ShaderRequiresCPUDeforms(tess.shader))
|
|
|
|
{
|
|
|
|
shaderAttribs |= GENERICDEF_USE_DEFORM_VERTEXES;
|
|
|
|
}
|
|
|
|
|
2013-10-14 08:55:54 +00:00
|
|
|
if (glState.vertexAnimation)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
shaderAttribs |= GENERICDEF_USE_VERTEX_ANIMATION;
|
|
|
|
}
|
|
|
|
|
2012-11-14 10:56:31 +00:00
|
|
|
if (pStage->bundle[0].numTexMods)
|
|
|
|
{
|
|
|
|
shaderAttribs |= GENERICDEF_USE_TCGEN_AND_TCMOD;
|
|
|
|
}
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
return &tr.genericShader[shaderAttribs];
|
|
|
|
}
|