- use explicitly declared matrix uniforms.

The builtin matrices are no longer available in modern GLSL, preventing an upgrade of the shader.
Also perform better reporting of shader compilation errors.
This commit is contained in:
Christoph Oelckers 2019-10-06 10:19:51 +02:00
parent 8fd8d0858c
commit 71d132b470
9 changed files with 56 additions and 46 deletions

View file

@ -355,10 +355,6 @@ void animvpx_setup_glstate(int32_t animvpx_flags)
//Force fullscreen (glox1=-1 forces it to restore afterwards) //Force fullscreen (glox1=-1 forces it to restore afterwards)
GLInterface.SetViewport(0,0,xdim,ydim); glox1 = -1; GLInterface.SetViewport(0,0,xdim,ydim); glox1 = -1;
VSMatrix identity(0);
GLInterface.SetMatrix(Matrix_ModelView, &identity);
GLInterface.SetMatrix(Matrix_Projection, &identity);
GLInterface.EnableAlphaTest(false); GLInterface.EnableAlphaTest(false);
GLInterface.EnableDepthTest(false); GLInterface.EnableDepthTest(false);
GLInterface.EnableBlend(false); GLInterface.EnableBlend(false);

View file

@ -1930,7 +1930,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
texmat.loadIdentity(); texmat.loadIdentity();
texmat.translate(xpanning, ypanning, 1.0f); texmat.translate(xpanning, ypanning, 1.0f);
texmat.scale(f, f, 1.0f); texmat.scale(f, f, 1.0f);
GLInterface.SetMatrix(Matrix_Texture3, &texmat); GLInterface.SetMatrix(Matrix_Detail, &texmat);
} }
tex = r_glowmapping ? mdloadskin((md2model_t *) m, tile2model[Ptile2tile(tspr->picnum, lpal)].skinnum, GLOWPAL, surfi) : 0; tex = r_glowmapping ? mdloadskin((md2model_t *) m, tile2model[Ptile2tile(tspr->picnum, lpal)].skinnum, GLOWPAL, surfi) : 0;
@ -1942,7 +1942,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
texmat.loadIdentity(); texmat.loadIdentity();
texmat.translate(xpanning, ypanning, 1.0f); texmat.translate(xpanning, ypanning, 1.0f);
GLInterface.SetMatrix(Matrix_Texture4, &texmat); GLInterface.SetMatrix(Matrix_Glow, &texmat);
} }
indexhandle = m->vindexes; indexhandle = m->vindexes;

View file

@ -1400,7 +1400,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
if ((detailpth->hicr->scale.x != 1.0f) || (detailpth->hicr->scale.y != 1.0f)) if ((detailpth->hicr->scale.x != 1.0f) || (detailpth->hicr->scale.y != 1.0f))
texmat.scale(detailpth->hicr->scale.x, detailpth->hicr->scale.y, 1.0f); texmat.scale(detailpth->hicr->scale.x, detailpth->hicr->scale.y, 1.0f);
GLInterface.SetMatrix(Matrix_Texture3, &texmat); GLInterface.SetMatrix(Matrix_Detail, &texmat);
} }
} }
@ -1664,7 +1664,7 @@ do
if (pth->hicr) if (pth->hicr)
{ {
VSMatrix identity(0); VSMatrix identity(0);
GLInterface.SetMatrix(Matrix_Texture3, &identity); GLInterface.SetMatrix(Matrix_Detail, &identity);
} }
if (videoGetRenderMode() != REND_POLYMOST) if (videoGetRenderMode() != REND_POLYMOST)

View file

@ -5,6 +5,8 @@
class FBitmap; class FBitmap;
class FTexture; class FTexture;
#include "tarray.h"
class FHardwareTexture //: public IHardwareTexture class FHardwareTexture //: public IHardwareTexture
{ {
public: public:
@ -34,13 +36,16 @@ public:
friend class FGameTexture; friend class FGameTexture;
}; };
// This class identifies a single source image to the game.
// Since hightile palette variations are identified by file name, they will create separate game textures.
class FGameTexture class FGameTexture
{ {
int Width, Height; int Width, Height;
bool isHightile; bool isHightile;
// Source image for this texture.
FTexture* sourceData = nullptr; FTexture* sourceData = nullptr;
// either indexed or the sole image for hightiles. // indexed or the sole image for hightiles.
FHardwareTexture* hwBase = nullptr; FHardwareTexture* hwBase = nullptr;
// If the number was large a TMap would be better - // If the number was large a TMap would be better -
// but in most cases the maximum number of palettes for a single tile is less than 10 where a linear search is much faster than a TMap. // but in most cases the maximum number of palettes for a single tile is less than 10 where a linear search is much faster than a TMap.

View file

@ -99,8 +99,8 @@ bool FShader::Load(const char * name, const char * vert_prog, const char * frag_
if (linked == 0) if (linked == 0)
{ {
// only print message if there's an error. // only print message if there's an error.
initprintf("Init Shader '%s':\n%s\n", name, error.GetChars()); FStringf err("Init Shader '%s':\n%s\n", name, error.GetChars());
return false; throw std::runtime_error(err); // Failing to compile a shader is fatal.
} }
return true; return true;
} }
@ -158,6 +158,11 @@ bool PolymostShader::Load(const char * name, const char * vert_prog, const char
NPOTEmulationXOffset.Init(hShader, "u_npotEmulationXOffset"); NPOTEmulationXOffset.Init(hShader, "u_npotEmulationXOffset");
Brightness.Init(hShader, "u_brightness"); Brightness.Init(hShader, "u_brightness");
RotMatrix.Init(hShader, "u_rotMatrix"); RotMatrix.Init(hShader, "u_rotMatrix");
ModelMatrix.Init(hShader, "u_modelMatrix");
ProjectionMatrix.Init(hShader, "u_projectionMatrix");
DetailMatrix.Init(hShader, "u_detailMatrix");
GlowMatrix.Init(hShader, "u_glowMatrix");
ShadeInterpolate.Init(hShader, "u_shadeInterpolate"); ShadeInterpolate.Init(hShader, "u_shadeInterpolate");
glUseProgram(hShader); glUseProgram(hShader);

View file

@ -51,6 +51,10 @@ public:
FBufferedUniform1f NPOTEmulationXOffset; FBufferedUniform1f NPOTEmulationXOffset;
FBufferedUniform1f Brightness; FBufferedUniform1f Brightness;
FUniformMatrix4f RotMatrix; FUniformMatrix4f RotMatrix;
FUniformMatrix4f ModelMatrix;
FUniformMatrix4f ProjectionMatrix;
FUniformMatrix4f DetailMatrix;
FUniformMatrix4f GlowMatrix;
FBufferedUniform1f ShadeInterpolate; FBufferedUniform1f ShadeInterpolate;
public: public:

View file

@ -62,9 +62,18 @@ void GLInstance::Init()
glinfo.dumped = 1; glinfo.dumped = 1;
} }
new(&renderState) PolymostRenderState; // reset to defaults. new(&renderState) PolymostRenderState; // reset to defaults.
try
{
LoadSurfaceShader(); LoadSurfaceShader();
LoadVPXShader(); LoadVPXShader();
LoadPolymostShader(); LoadPolymostShader();
}
catch (const std::runtime_error& err)
{
// This is far from an optimal solution but at this point the only way to get the error out.
wm_msgbox("Shader compilation failed", err.what());
exit(1);
}
} }
@ -78,11 +87,7 @@ void GLInstance::LoadPolymostShader()
Vert.Push(0); Vert.Push(0);
Frag.Push(0); Frag.Push(0);
polymostShader = new PolymostShader(); polymostShader = new PolymostShader();
if (!polymostShader->Load("PolymostShader", (const char*)Vert.Data(), (const char*)Frag.Data())) polymostShader->Load("PolymostShader", (const char*)Vert.Data(), (const char*)Frag.Data());
{
wm_msgbox("Fatal Error", "Shader compilation failed");
exit(1);
}
SetPolymostShader(); SetPolymostShader();
} }
@ -96,11 +101,7 @@ void GLInstance::LoadVPXShader()
Vert.Push(0); Vert.Push(0);
Frag.Push(0); Frag.Push(0);
vpxShader = new FShader(); vpxShader = new FShader();
if (!vpxShader->Load("VPXShader", (const char*)Vert.Data(), (const char*)Frag.Data())) vpxShader->Load("VPXShader", (const char*)Vert.Data(), (const char*)Frag.Data());
{
wm_msgbox("Fatal Error", "Shader compilation failed");
exit(1);
}
} }
void GLInstance::LoadSurfaceShader() void GLInstance::LoadSurfaceShader()
@ -113,11 +114,7 @@ void GLInstance::LoadSurfaceShader()
Vert.Push(0); Vert.Push(0);
Frag.Push(0); Frag.Push(0);
surfaceShader = new SurfaceShader(); surfaceShader = new SurfaceShader();
if (!surfaceShader->Load("SurfaceShader", (const char*)Vert.Data(), (const char*)Frag.Data())) surfaceShader->Load("SurfaceShader", (const char*)Vert.Data(), (const char*)Frag.Data());
{
wm_msgbox("Fatal Error", "Shader compilation failed");
exit(1);
}
} }
@ -253,26 +250,25 @@ void GLInstance::SetMatrix(int num, const VSMatrix *mat)
{ {
default: default:
return; return;
case Matrix_View: case Matrix_View:
polymostShader->RotMatrix.Set(mat->get()); polymostShader->RotMatrix.Set(mat->get());
break; break;
case Matrix_Projection: case Matrix_Projection:
glMatrixMode(GL_PROJECTION); polymostShader->ProjectionMatrix.Set(mat->get());
glLoadMatrixf(mat->get());
break; break;
case Matrix_ModelView: case Matrix_ModelView:
glMatrixMode(GL_MODELVIEW); polymostShader->ModelMatrix.Set(mat->get());
glLoadMatrixf(mat->get());
break; break;
case Matrix_Texture3: case Matrix_Detail:
case Matrix_Texture4: polymostShader->DetailMatrix.Set(mat->get());
glActiveTexture(GL_TEXTURE3 + num - Matrix_Texture3); break;
glMatrixMode(GL_TEXTURE);
glLoadMatrixf(mat->get()); case Matrix_Glow:
glActiveTexture(GL_TEXTURE0); polymostShader->GlowMatrix.Set(mat->get());
break; break;
} }
} }

View file

@ -67,8 +67,8 @@ enum EMatrixType
Matrix_View, Matrix_View,
Matrix_Projection, Matrix_Projection,
Matrix_ModelView, Matrix_ModelView,
Matrix_Texture3, Matrix_Detail,
Matrix_Texture4, Matrix_Glow,
// These are the only ones being used. // These are the only ones being used.
NUMMATRICES NUMMATRICES
}; };

View file

@ -5,6 +5,10 @@ varying float v_distance;
uniform float u_usePalette; uniform float u_usePalette;
uniform mat4 u_rotMatrix; uniform mat4 u_rotMatrix;
uniform mat4 u_modelMatrix;
uniform mat4 u_projectionMatrix;
uniform mat4 u_detailMatrix;
uniform mat4 u_glowMatrix;
const float c_zero = 0.0; const float c_zero = 0.0;
const float c_one = 1.0; const float c_one = 1.0;
@ -12,16 +16,16 @@ const float c_one = 1.0;
void main() void main()
{ {
vec4 vertex = u_rotMatrix * gl_Vertex; vec4 vertex = u_rotMatrix * gl_Vertex;
vec4 eyeCoordPosition = gl_ModelViewMatrix * vertex; vec4 eyeCoordPosition = u_modelMatrix * vertex;
gl_Position = gl_ModelViewProjectionMatrix * vertex; gl_Position = u_projectionMatrix * eyeCoordPosition;
eyeCoordPosition.xyz /= eyeCoordPosition.w; eyeCoordPosition.xyz /= eyeCoordPosition.w;
gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[0] = gl_MultiTexCoord0;
//gl_TexCoord[0] = mix(gl_TexCoord[0].xyzw, gl_TexCoord[0].yxzw, u_usePalette); WTF is this??? //gl_TexCoord[0] = mix(gl_TexCoord[0].xyzw, gl_TexCoord[0].yxzw, u_usePalette); WTF is this???
gl_TexCoord[3] = gl_TextureMatrix[3] * gl_MultiTexCoord0; gl_TexCoord[3] = u_detailMatrix * gl_MultiTexCoord0;
gl_TexCoord[4] = gl_TextureMatrix[4] * gl_MultiTexCoord0; gl_TexCoord[4] = u_glowMatrix * gl_MultiTexCoord0;
gl_FogFragCoord = abs(eyeCoordPosition.z); gl_FogFragCoord = abs(eyeCoordPosition.z);