- moved the main shader and its entire uniform maintenance into the backend.

This commit is contained in:
Christoph Oelckers 2019-10-05 12:28:08 +02:00
parent 50c2065425
commit bd4e4834e3
17 changed files with 650 additions and 592 deletions

View file

@ -772,6 +772,7 @@ set (PCH_SOURCES
glbackend/gl_hwtexture.cpp glbackend/gl_hwtexture.cpp
glbackend/gl_samplers.cpp glbackend/gl_samplers.cpp
glbackend/gl_shader.cpp
glbackend/glbackend.cpp glbackend/glbackend.cpp
mact/src/animlib.cpp mact/src/animlib.cpp

View file

@ -1489,23 +1489,8 @@ int32_t engineLoadBoardV5V6(const char *filename, char fromwhere, vec3_t *dapos,
#endif #endif
static FORCE_INLINE void renderDisableFog(void) void renderDisableFog(void);
{ void renderEnableFog(void);
#ifdef USE_OPENGL
if (videoGetRenderMode() >= REND_POLYMOST)
{
polymost_setFogEnabled(false);
}
#endif
}
static FORCE_INLINE void renderEnableFog(void)
{
#ifdef USE_OPENGL
if (videoGetRenderMode() >= REND_POLYMOST && !nofog)
polymost_setFogEnabled(true);
#endif
}
static vec2_t const zerovec = { 0, 0 }; static vec2_t const zerovec = { 0, 0 };

View file

@ -51,19 +51,6 @@ void polymost_completeMirror();
int32_t polymost_maskWallHasTranslucency(uwalltype const * const wall); int32_t polymost_maskWallHasTranslucency(uwalltype const * const wall);
int32_t polymost_spriteHasTranslucency(uspritetype const * const tspr); int32_t polymost_spriteHasTranslucency(uspritetype const * const tspr);
void polymost_resetVertexPointers(void);
void polymost_disableProgram(void);
void polymost_resetProgram(void);
void polymost_setTexturePosSize(vec4f_t const &texturePosSize);
void polymost_setHalfTexelSize(vec2f_t const &halfTexelSize);
char polymost_getClamp();
void polymost_setClamp(char clamp);
void polymost_setVisibility(float visibility);
void polymost_setFogEnabled(char fogEnabled);
void polymost_useColorOnly(char useColorOnly);
void polymost_usePaletteIndexing(char usePaletteIndexing);
void polymost_useDetailMapping(char useDetailMapping);
void polymost_useGlowMapping(char useGlowMapping);
void useShaderProgram(uint32_t shaderID); void useShaderProgram(uint32_t shaderID);
float* multiplyMatrix4f(float m0[4*4], const float m1[4*4]); float* multiplyMatrix4f(float m0[4*4], const float m1[4*4]);
@ -71,8 +58,6 @@ float* multiplyMatrix4f(float m0[4*4], const float m1[4*4]);
void polymost_glinit(void); void polymost_glinit(void);
void polymost_glreset(void); void polymost_glreset(void);
void polymost_init(void);
enum { enum {
INVALIDATE_ALL, INVALIDATE_ALL,
INVALIDATE_ART, INVALIDATE_ART,

View file

@ -60,7 +60,7 @@ static void drawlinegl(int32_t x1, int32_t y1, int32_t x2, int32_t y2, palette_t
GLInterface.EnableDepthTest(false); GLInterface.EnableDepthTest(false);
GLInterface.EnableBlend(true); // When using line antialiasing, this is needed GLInterface.EnableBlend(true); // When using line antialiasing, this is needed
polymost_useColorOnly(true); GLInterface.UseColorOnly(true);
GLInterface.SetColorub(p.r, p.g, p.b, 255); GLInterface.SetColorub(p.r, p.g, p.b, 255);
auto data = GLInterface.AllocVertices(2); auto data = GLInterface.AllocVertices(2);
@ -68,7 +68,7 @@ static void drawlinegl(int32_t x1, int32_t y1, int32_t x2, int32_t y2, palette_t
data.second[1].Set((float) x2 * (1.f/4096.f), (float) y2 * (1.f/4096.f)); data.second[1].Set((float) x2 * (1.f/4096.f), (float) y2 * (1.f/4096.f));
GLInterface.Draw(DT_LINES, data.first, 2); GLInterface.Draw(DT_LINES, data.first, 2);
polymost_useColorOnly(false); GLInterface.UseColorOnly(false);
} }
#endif #endif

View file

@ -417,7 +417,6 @@ void animvpx_setup_glstate(int32_t animvpx_flags)
VSMatrix identity(0); VSMatrix identity(0);
GLInterface.SetMatrix(Matrix_ModelView, &identity); GLInterface.SetMatrix(Matrix_ModelView, &identity);
GLInterface.SetMatrix(Matrix_Projection, &identity); GLInterface.SetMatrix(Matrix_Projection, &identity);
GLInterface.SetMatrix(Matrix_Texture0, &identity);
GLInterface.EnableAlphaTest(false); GLInterface.EnableAlphaTest(false);
GLInterface.EnableDepthTest(false); GLInterface.EnableDepthTest(false);
@ -444,8 +443,7 @@ void animvpx_setup_glstate(int32_t animvpx_flags)
void animvpx_restore_glstate(void) void animvpx_restore_glstate(void)
{ {
useShaderProgram(0); GLInterface.SetPolymostShader();
polymost_resetProgram();
delete texture; delete texture;
texture = nullptr; texture = nullptr;

View file

@ -249,8 +249,8 @@ int32_t FindDistance3D(int32_t x, int32_t y, int32_t z)
// Clear OSD background // Clear OSD background
void COMMON_doclearbackground(int numcols, int height) void COMMON_doclearbackground(int numcols, int height)
{ {
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
polymost_useColorOnly(true); GLInterface.UseColorOnly(true);
polymostSet2dView(); polymostSet2dView();
@ -277,8 +277,8 @@ void COMMON_doclearbackground(int numcols, int height)
GLInterface.SetColor(0.f, 0.f, 0.f, 1.f); GLInterface.SetColor(0.f, 0.f, 0.f, 1.f);
GLInterface.Draw(DT_TRIANGLE_STRIP, vert.first+4, 4); GLInterface.Draw(DT_TRIANGLE_STRIP, vert.first+4, 4);
polymost_useColorOnly(false); GLInterface.UseColorOnly(false);
polymost_setFogEnabled(true); GLInterface.SetFogEnabled(true);
} }
void COMMON_clearbackground(int numcols, int numrows) void COMMON_clearbackground(int numcols, int numrows)

View file

@ -8975,7 +8975,7 @@ killsprite:
{ {
GLInterface.EnableBlend(false); GLInterface.EnableBlend(false);
GLInterface.EnableAlphaTest(true); GLInterface.EnableAlphaTest(true);
polymost_setClamp(1+2); GLInterface.SetClamp(1+2);
if (spritesortcnt < numSprites) if (spritesortcnt < numSprites)
{ {
@ -9026,7 +9026,7 @@ killsprite:
} }
} }
polymost_setClamp(0); GLInterface.SetClamp(0);
int32_t numMaskWalls = maskwallcnt; int32_t numMaskWalls = maskwallcnt;
maskwallcnt = 0; maskwallcnt = 0;
for (i = 0; i < numMaskWalls; i++) for (i = 0; i < numMaskWalls; i++)
@ -9071,7 +9071,7 @@ killsprite:
#ifdef USE_OPENGL #ifdef USE_OPENGL
if (videoGetRenderMode() == REND_POLYMOST) if (videoGetRenderMode() == REND_POLYMOST)
polymost_setClamp(1+2); GLInterface.SetClamp(1+2);
#endif #endif
i = spritesortcnt; i = spritesortcnt;
@ -9160,14 +9160,14 @@ killsprite:
debugmask_add(maskwall[maskwallcnt], thewall[maskwall[maskwallcnt]]); debugmask_add(maskwall[maskwallcnt], thewall[maskwall[maskwallcnt]]);
#ifdef USE_OPENGL #ifdef USE_OPENGL
if (videoGetRenderMode() == REND_POLYMOST) if (videoGetRenderMode() == REND_POLYMOST)
polymost_setClamp(0); GLInterface.SetClamp(0);
#endif #endif
renderDrawMaskedWall(maskwallcnt); renderDrawMaskedWall(maskwallcnt);
} }
#ifdef USE_OPENGL #ifdef USE_OPENGL
if (videoGetRenderMode() == REND_POLYMOST) if (videoGetRenderMode() == REND_POLYMOST)
polymost_setClamp(1+2); GLInterface.SetClamp(1+2);
#endif #endif
while (spritesortcnt) while (spritesortcnt)
{ {
@ -9183,7 +9183,7 @@ killsprite:
if (videoGetRenderMode() == REND_POLYMOST) if (videoGetRenderMode() == REND_POLYMOST)
{ {
GLInterface.SetDepthMask(true); GLInterface.SetDepthMask(true);
polymost_setClamp(0); GLInterface.SetClamp(0);
} }
#endif #endif
@ -12982,10 +12982,6 @@ int32_t videoSetRenderMode(int32_t renderer)
if (videoGetRenderMode() >= REND_POLYMOST) if (videoGetRenderMode() >= REND_POLYMOST)
glrendmode = rendmode; glrendmode = rendmode;
if (renderer == REND_POLYMOST)
{
polymost_init();
}
#endif #endif
return 0; return 0;

View file

@ -2058,10 +2058,9 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
float const xpanning = (float)sext->xpanning * (1.f/256.f); float const xpanning = (float)sext->xpanning * (1.f/256.f);
float const ypanning = (float)sext->ypanning * (1.f/256.f); float const ypanning = (float)sext->ypanning * (1.f/256.f);
char prevClamp = polymost_getClamp(); int prevClamp = GLInterface.GetClamp();
polymost_setClamp(0); GLInterface.SetClamp(0);
polymost_usePaletteIndexing(false); GLInterface.UsePaletteIndexing(false);
polymost_setTexturePosSize({ 0.f, 0.f, 1.f, 1.f });
for (surfi=0; surfi<m->head.numsurfs; surfi++) for (surfi=0; surfi<m->head.numsurfs; surfi++)
{ {
@ -2130,7 +2129,6 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
if (!(tspr->extra&TSPR_EXTRA_MDHACK)) if (!(tspr->extra&TSPR_EXTRA_MDHACK))
{ {
#ifdef USE_GLEXT
//POGOTODO: if we add support for palette indexing on model skins, the texture for the palswap could be setup here //POGOTODO: if we add support for palette indexing on model skins, the texture for the palswap could be setup here
texunits += 4; texunits += 4;
@ -2140,7 +2138,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
{ {
mdskinmap_t *sk; mdskinmap_t *sk;
polymost_useDetailMapping(true); GLInterface.UseDetailMapping(true);
polymost_setupdetailtexture(3, tex); polymost_setupdetailtexture(3, tex);
for (sk = m->skinmap; sk; sk = sk->next) for (sk = m->skinmap; sk; sk = sk->next)
@ -2157,7 +2155,7 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
if (i) if (i)
{ {
polymost_useGlowMapping(true); GLInterface.UseGlowMapping(true);
polymost_setupglowtexture(4, tex); polymost_setupglowtexture(4, tex);
texmat.loadIdentity(); texmat.loadIdentity();
@ -2165,7 +2163,6 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
GLInterface.SetMatrix(Matrix_Texture4, &texmat); GLInterface.SetMatrix(Matrix_Texture4, &texmat);
} }
#endif
indexhandle = m->vindexes; indexhandle = m->vindexes;
//PLAG: delayed polygon-level sorted rendering //PLAG: delayed polygon-level sorted rendering
@ -2217,10 +2214,8 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
md3draw_handle_triangles(s, indexhandle, texunits, NULL); md3draw_handle_triangles(s, indexhandle, texunits, NULL);
} }
#ifdef USE_GLEXT GLInterface.UseDetailMapping(false);
polymost_useDetailMapping(false); GLInterface.UseGlowMapping(false);
polymost_useGlowMapping(false);
#endif
} }
//------------ //------------
@ -2230,11 +2225,10 @@ static int32_t polymost_md3draw(md3model_t *m, tspriteptr_t tspr)
VSMatrix identity(0); VSMatrix identity(0);
GLInterface.SetMatrix(Matrix_ModelView, &identity); GLInterface.SetMatrix(Matrix_ModelView, &identity);
GLInterface.SetMatrix(Matrix_Texture0, &identity);
polymost_setClamp(prevClamp); GLInterface.SetClamp(prevClamp);
polymost_usePaletteIndexing(true); GLInterface.UsePaletteIndexing(true);
polymost_resetVertexPointers(); GLInterface.SetPolymostShader();
globalnoeffect=0; globalnoeffect=0;
return 1; return 1;

View file

@ -58,13 +58,13 @@ void fullscreen_tint_gl(uint8_t r, uint8_t g, uint8_t b, uint8_t f)
GLInterface.EnableDepthTest(false); GLInterface.EnableDepthTest(false);
GLInterface.EnableAlphaTest(false); GLInterface.EnableAlphaTest(false);
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc); GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
GLInterface.EnableBlend(true); GLInterface.EnableBlend(true);
GLInterface.SetColorub(r, g, b, f); GLInterface.SetColorub(r, g, b, f);
polymost_useColorOnly(true); GLInterface.UseColorOnly(true);
auto data = GLInterface.AllocVertices(3); auto data = GLInterface.AllocVertices(3);
auto vt = data.second; auto vt = data.second;
@ -72,7 +72,7 @@ void fullscreen_tint_gl(uint8_t r, uint8_t g, uint8_t b, uint8_t f)
vt[1].Set(2.5f, 1.f); vt[1].Set(2.5f, 1.f);
vt[2].Set(.0f, -2.5f); vt[2].Set(.0f, -2.5f);
GLInterface.Draw(DT_TRIANGLES, data.first, 3); GLInterface.Draw(DT_TRIANGLES, data.first, 3);
polymost_useColorOnly(false); GLInterface.UseColorOnly(false);
GLInterface.SetMatrix(Matrix_Projection, &oldproj); GLInterface.SetMatrix(Matrix_Projection, &oldproj);
GLInterface.SetMatrix(Matrix_ModelView, &oldmv); GLInterface.SetMatrix(Matrix_ModelView, &oldmv);
@ -93,12 +93,12 @@ void fullscreen_tint_gl_blood(void)
GLInterface.EnableDepthTest(false); GLInterface.EnableDepthTest(false);
GLInterface.EnableAlphaTest(false); GLInterface.EnableAlphaTest(false);
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
GLInterface.SetBlendFunc(STYLEALPHA_One, STYLEALPHA_One); GLInterface.SetBlendFunc(STYLEALPHA_One, STYLEALPHA_One);
GLInterface.EnableBlend(true); GLInterface.EnableBlend(true);
polymost_useColorOnly(true); GLInterface.UseColorOnly(true);
GLInterface.SetColorub(max(tint_blood_r, 0), max(tint_blood_g, 0), max(tint_blood_b, 0), 255); GLInterface.SetColorub(max(tint_blood_r, 0), max(tint_blood_g, 0), max(tint_blood_b, 0), 255);
auto data = GLInterface.AllocVertices(3); auto data = GLInterface.AllocVertices(3);
auto vt = data.second; auto vt = data.second;
@ -117,7 +117,7 @@ void fullscreen_tint_gl_blood(void)
GLInterface.SetBlendOp(STYLEOP_Add); GLInterface.SetBlendOp(STYLEOP_Add);
GLInterface.SetColorub(0,0,0,0); GLInterface.SetColorub(0,0,0,0);
GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc); GLInterface.SetBlendFunc(STYLEALPHA_Src, STYLEALPHA_InvSrc);
polymost_useColorOnly(false); GLInterface.UseColorOnly(false);
GLInterface.SetMatrix(Matrix_Projection, &oldproj); GLInterface.SetMatrix(Matrix_Projection, &oldproj);
GLInterface.SetMatrix(Matrix_ModelView, &oldmv); GLInterface.SetMatrix(Matrix_ModelView, &oldmv);

View file

@ -129,7 +129,6 @@ int32_t r_flatsky = 1;
static float fogresult, fogresult2; static float fogresult, fogresult2;
coltypef fogcol, fogtable[MAXPALOOKUPS]; coltypef fogcol, fogtable[MAXPALOOKUPS];
static uint32_t currentShaderProgramID = 0;
static GLenum currentActiveTexture = 0; static GLenum currentActiveTexture = 0;
static uint32_t currentTextureID = 0; static uint32_t currentTextureID = 0;
@ -143,55 +142,6 @@ static FHardwareTexture *paletteTextureIDs[MAXBASEPALS];
static FHardwareTexture *palswapTextureID = nullptr; static FHardwareTexture *palswapTextureID = nullptr;
//extern char const *polymost1Frag; //extern char const *polymost1Frag;
//extern char const *polymost1Vert; //extern char const *polymost1Vert;
static GLuint polymost1CurrentShaderProgramID = 0;
static GLuint polymost1ExtendedShaderProgramID = 0;
static GLint polymost1TexSamplerLoc = -1;
static GLint polymost1PalSwapSamplerLoc = -1;
static GLint polymost1PaletteSamplerLoc = -1;
static GLint polymost1DetailSamplerLoc = -1;
static GLint polymost1GlowSamplerLoc = -1;
static GLint polymost1TexturePosSizeLoc = -1;
static vec4f_t polymost1TexturePosSize = { 0.f, 0.f, 1.f, 1.f };
static GLint polymost1HalfTexelSizeLoc = -1;
static vec2f_t polymost1HalfTexelSize = { 0.f, 0.f };
static GLint polymost1PalswapPosLoc = -1;
static vec2f_t polymost1PalswapPos = { 0.f, 0.f };
static GLint polymost1PalswapSizeLoc = -1;
static vec2f_t polymost1PalswapSize = { 0.f, 0.f };
static vec2f_t polymost1PalswapInnerSize = { 0.f, 0.f };
static GLint polymost1ClampLoc = -1;
static vec2f_t polymost1Clamp = { 0.f, 0.f };
static GLint polymost1ShadeLoc = -1;
static float polymost1Shade = 0.f;
static GLint polymost1NumShadesLoc = -1;
static float polymost1NumShades = 64.f;
static GLint polymost1VisFactorLoc = -1;
static float polymost1VisFactor = 128.f;
static GLint polymost1FogEnabledLoc = -1;
static float polymost1FogEnabled = 1.f;
static GLint polymost1UseColorOnlyLoc = -1;
static float polymost1UseColorOnly = 0.f;
static GLint polymost1UsePaletteLoc = -1;
static float polymost1UsePalette = 1.f;
static GLint polymost1UseDetailMappingLoc = -1;
static float polymost1UseDetailMapping = 0.f;
static GLint polymost1UseGlowMappingLoc = -1;
static float polymost1UseGlowMapping = 0.f;
static GLint polymost1NPOTEmulationLoc = -1;
static float polymost1NPOTEmulation = 0.f;
static GLint polymost1NPOTEmulationFactorLoc = -1;
static float polymost1NPOTEmulationFactor = 1.f;
static GLint polymost1NPOTEmulationXOffsetLoc = -1;
static float polymost1NPOTEmulationXOffset = 0.f;
static GLint polymost1BrightnessLoc = -1;
static float polymost1Brightness = 1.f;
static GLint polymost1RotMatrixLoc = -1;
static float polymost1RotMatrix[16] = { 1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f };
static GLint polymost1ShadeInterpolateLoc = -1;
static float polymost1ShadeInterpolate = 1.f;
static inline float float_trans(uint32_t maskprops, uint8_t blend) static inline float float_trans(uint32_t maskprops, uint8_t blend)
{ {
@ -356,44 +306,6 @@ static void calcmat(vec3f_t a0, const vec2f_t *offset, float f, float mat[16], i
mat[14] = (mat[14] + a0.y*mat[2]) + (a0.z*mat[6] + a0.x*mat[10]); mat[14] = (mat[14] + a0.y*mat[2]) + (a0.z*mat[6] + a0.x*mat[10]);
} }
static GLuint polymost2_compileShader(GLenum shaderType, const char* const source, int * pLength = nullptr)
{
GLuint shaderID = glCreateShader(shaderType);
if (shaderID == 0)
{
return 0;
}
glShaderSource(shaderID,
1,
&source,
pLength);
glCompileShader(shaderID);
GLint compileStatus;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileStatus);
if (!compileStatus)
{
GLint logLength;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
OSD_Printf("Compile Status: %u\n", compileStatus);
if (logLength > 0)
{
char *infoLog = (char*)Xmalloc(logLength);
glGetShaderInfoLog(shaderID, logLength, &logLength, infoLog);
OSD_Printf("Log:\n%s\n", infoLog);
free(infoLog);
}
}
return shaderID;
}
static GLuint polymost2_compileShader(GLenum shaderType, const char* const source, int length)
{
return polymost2_compileShader(shaderType, source, &length);
}
void polymost_glreset() void polymost_glreset()
{ {
for (bssize_t i=0; i<=MAXPALOOKUPS-1; i++) for (bssize_t i=0; i<=MAXPALOOKUPS-1; i++)
@ -450,286 +362,17 @@ void polymost_glreset()
#endif #endif
} }
// reset vertex pointers to polymost default
void polymost_resetVertexPointers()
{
polymost_resetProgram();
}
void polymost_disableProgram()
{
if (videoGetRenderMode() != REND_POLYMOST)
return;
polymost_outputGLDebugMessage(3, "polymost_disableProgram()");
useShaderProgram(0);
}
void polymost_resetProgram()
{
if (videoGetRenderMode() != REND_POLYMOST)
return;
polymost_outputGLDebugMessage(3, "polymost_resetProgram()");
useShaderProgram(polymost1CurrentShaderProgramID);
// ensure that palswapTexture and paletteTexture[curbasepal] is bound
GLInterface.BindTexture(1, palswapTextureID);
GLInterface.BindTexture(2, paletteTextureIDs[curbasepal]);
}
static void polymost_setCurrentShaderProgram(uint32_t programID)
{
polymost_outputGLDebugMessage(3, "polymost_setCurrentShaderProgram(programID:%u)", programID);
polymost1CurrentShaderProgramID = programID;
useShaderProgram(programID);
//update the uniform locations
polymost1TexSamplerLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "s_texture");
polymost1PalSwapSamplerLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "s_palswap");
polymost1PaletteSamplerLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "s_palette");
polymost1DetailSamplerLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "s_detail");
polymost1GlowSamplerLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "s_glow");
polymost1TexturePosSizeLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_texturePosSize");
polymost1HalfTexelSizeLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_halfTexelSize");
polymost1PalswapPosLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_palswapPos");
polymost1PalswapSizeLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_palswapSize");
polymost1ClampLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_clamp");
polymost1ShadeLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_shade");
polymost1NumShadesLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_numShades");
polymost1VisFactorLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_visFactor");
polymost1FogEnabledLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_fogEnabled");
polymost1UsePaletteLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_usePalette");
polymost1UseColorOnlyLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_useColorOnly");
polymost1UseDetailMappingLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_useDetailMapping");
polymost1UseGlowMappingLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_useGlowMapping");
polymost1NPOTEmulationLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_npotEmulation");
polymost1NPOTEmulationFactorLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_npotEmulationFactor");
polymost1NPOTEmulationXOffsetLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_npotEmulationXOffset");
polymost1BrightnessLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_brightness");
polymost1RotMatrixLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_rotMatrix");
polymost1ShadeInterpolateLoc = glGetUniformLocation(polymost1CurrentShaderProgramID, "u_shadeInterpolate");
//set the uniforms to the current values
glUniform4f(polymost1TexturePosSizeLoc, polymost1TexturePosSize.x, polymost1TexturePosSize.y, polymost1TexturePosSize.z, polymost1TexturePosSize.w);
glUniform2f(polymost1HalfTexelSizeLoc, polymost1HalfTexelSize.x, polymost1HalfTexelSize.y);
glUniform2f(polymost1PalswapPosLoc, polymost1PalswapPos.x, polymost1PalswapPos.y);
glUniform2f(polymost1PalswapSizeLoc, polymost1PalswapInnerSize.x, polymost1PalswapInnerSize.y);
glUniform2f(polymost1ClampLoc, polymost1Clamp.x, polymost1Clamp.y);
glUniform1f(polymost1ShadeLoc, polymost1Shade);
glUniform1f(polymost1NumShadesLoc, polymost1NumShades);
glUniform1f(polymost1VisFactorLoc, polymost1VisFactor);
glUniform1f(polymost1FogEnabledLoc, polymost1FogEnabled);
glUniform1f(polymost1UseColorOnlyLoc, polymost1UseColorOnly);
glUniform1f(polymost1UsePaletteLoc, polymost1UsePalette);
glUniform1f(polymost1UseDetailMappingLoc, polymost1UseDetailMapping);
glUniform1f(polymost1UseGlowMappingLoc, polymost1UseGlowMapping);
glUniform1f(polymost1NPOTEmulationLoc, polymost1NPOTEmulation);
glUniform1f(polymost1NPOTEmulationFactorLoc, polymost1NPOTEmulationFactor);
glUniform1f(polymost1NPOTEmulationXOffsetLoc, polymost1NPOTEmulationXOffset);
glUniform1f(polymost1BrightnessLoc, polymost1Brightness);
glUniformMatrix4fv(polymost1RotMatrixLoc, 1, false, polymost1RotMatrix);
glUniform1f(polymost1ShadeInterpolateLoc, polymost1ShadeInterpolate);
}
void polymost_setTexturePosSize(vec4f_t const &texturePosSize)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1TexturePosSize = texturePosSize;
glUniform4f(polymost1TexturePosSizeLoc, polymost1TexturePosSize.x, polymost1TexturePosSize.y, polymost1TexturePosSize.z, polymost1TexturePosSize.w);
}
static inline void polymost_setHalfTexelSize(vec2f_t const &halfTexelSize)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1HalfTexelSize = halfTexelSize;
glUniform2f(polymost1HalfTexelSizeLoc, polymost1HalfTexelSize.x, polymost1HalfTexelSize.y);
}
static void polymost_setPalswap(uint32_t index)
{
static uint32_t lastPalswapIndex;
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
lastPalswapIndex = index;
polymost1PalswapPos.x = index*polymost1PalswapSize.x;
polymost1PalswapPos.y = floorf(polymost1PalswapPos.x);
polymost1PalswapPos = { polymost1PalswapPos.x - polymost1PalswapPos.y + (0.5f/PALSWAP_TEXTURE_SIZE),
polymost1PalswapPos.y * polymost1PalswapSize.y + (0.5f/PALSWAP_TEXTURE_SIZE) };
glUniform2f(polymost1PalswapPosLoc, polymost1PalswapPos.x, polymost1PalswapPos.y);
}
static void polymost_setPalswapSize(uint32_t width, uint32_t height)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1PalswapSize = { width*(1.f/PALSWAP_TEXTURE_SIZE),
height*(1.f/PALSWAP_TEXTURE_SIZE) };
polymost1PalswapInnerSize = { (width-1)*(1.f/PALSWAP_TEXTURE_SIZE),
(height-1)*(1.f/PALSWAP_TEXTURE_SIZE) };
glUniform2f(polymost1PalswapSizeLoc, polymost1PalswapInnerSize.x, polymost1PalswapInnerSize.y);
}
char polymost_getClamp()
{
return polymost1Clamp.x + polymost1Clamp.y*2.0;
}
void polymost_setClamp(char clamp)
{
char clampx = clamp&1;
char clampy = clamp>>1;
if (currentShaderProgramID != polymost1CurrentShaderProgramID ||
(clampx == polymost1Clamp.x && clampy == polymost1Clamp.y))
return;
polymost1Clamp.x = clampx;
polymost1Clamp.y = clampy;
glUniform2f(polymost1ClampLoc, polymost1Clamp.x, polymost1Clamp.y);
}
static void polymost_setShade(int32_t shade)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
if (globalflags & GLOBAL_NO_GL_TILESHADES)
shade = 0;
static int32_t lastShade;
static int32_t lastNumShades;
if (shade != lastShade)
{
lastShade = shade;
polymost1Shade = shade;
glUniform1f(polymost1ShadeLoc, polymost1Shade);
}
if (numshades != lastNumShades)
{
lastNumShades = numshades;
polymost1NumShades = numshades;
glUniform1f(polymost1NumShadesLoc, polymost1NumShades);
}
}
static void polymost_setVisibility(float visibility)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
float visFactor = visibility * fviewingrange * (1.f / (64.f * 65536.f));
if (visFactor == polymost1VisFactor)
return;
polymost1VisFactor = visFactor;
glUniform1f(polymost1VisFactorLoc, polymost1VisFactor);
}
void polymost_setFogEnabled(char fogEnabled)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1FogEnabled = fogEnabled;
glUniform1f(polymost1FogEnabledLoc, polymost1FogEnabled);
}
void polymost_useColorOnly(char useColorOnly)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1UseColorOnly = useColorOnly;
glUniform1f(polymost1UseColorOnlyLoc, polymost1UseColorOnly);
}
void polymost_usePaletteIndexing(char usePaletteIndexing)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1UsePalette = usePaletteIndexing;
glUniform1f(polymost1UsePaletteLoc, polymost1UsePalette);
}
void polymost_useDetailMapping(char useDetailMapping)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID || useDetailMapping == polymost1UseDetailMapping)
return;
polymost1UseDetailMapping = useDetailMapping;
glUniform1f(polymost1UseDetailMappingLoc, polymost1UseDetailMapping);
}
void polymost_useGlowMapping(char useGlowMapping)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID || useGlowMapping == polymost1UseGlowMapping)
return;
polymost1UseGlowMapping = useGlowMapping;
glUniform1f(polymost1UseGlowMappingLoc, polymost1UseGlowMapping);
}
void polymost_npotEmulation(char npotEmulation, float factor, float xOffset)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID || npotEmulation == polymost1NPOTEmulation)
return;
polymost1NPOTEmulation = npotEmulation;
glUniform1f(polymost1NPOTEmulationLoc, polymost1NPOTEmulation);
polymost1NPOTEmulationFactor = factor;
glUniform1f(polymost1NPOTEmulationFactorLoc, polymost1NPOTEmulationFactor);
polymost1NPOTEmulationXOffset = xOffset;
glUniform1f(polymost1NPOTEmulationXOffsetLoc, polymost1NPOTEmulationXOffset);
}
void polymost_shadeInterpolate(int32_t shadeInterpolate)
{
if (currentShaderProgramID == polymost1CurrentShaderProgramID)
{
polymost1ShadeInterpolate = shadeInterpolate;
glUniform1f(polymost1ShadeInterpolateLoc, polymost1ShadeInterpolate);
}
}
void polymost_setBrightness(int brightness){
if (currentShaderProgramID == polymost1CurrentShaderProgramID)
{
polymost1Brightness = 8.f / (brightness+8.f);
glUniform1f(polymost1BrightnessLoc, polymost1Brightness);
}
}
static void polymost_bindPth(pthtyp const* const pPth, int sampler) static void polymost_bindPth(pthtyp const* const pPth, int sampler)
{ {
Bassert(pPth); Bassert(pPth);
vec4f_t texturePosSize = { 0.f, 0.f, 1.f, 1.f };
vec2f_t halfTexelSize = { 0.f, 0.f };
polymost_setTexturePosSize(texturePosSize);
polymost_setHalfTexelSize(halfTexelSize);
GLInterface.BindTexture(0, pPth->glpic, sampler); GLInterface.BindTexture(0, pPth->glpic, sampler);
} }
void useShaderProgram(uint32_t shaderID) void useShaderProgram(uint32_t shaderID)
{ {
glUseProgram(shaderID); glUseProgram(shaderID);
currentShaderProgramID = shaderID;
} }
FileReader GetBaseResource(const char* fn); FileReader GetBaseResource(const char* fn);
@ -742,56 +385,6 @@ void polymost_glinit()
currentTextureID = 0; currentTextureID = 0;
char allPacked = false; char allPacked = false;
auto fr1 = GetBaseResource("demolition/shaders/glsl/polymost.vp");
TArray<uint8_t> polymost1Vert = fr1.Read();
fr1 = GetBaseResource("demolition/shaders/glsl/polymost.fp");
TArray<uint8_t> polymost1Frag = fr1.Read();
// Zero-terminate both strings.
polymost1Vert.Push(0);
polymost1Frag.Push(0);
polymost1ExtendedShaderProgramID = glCreateProgram();
GLuint polymost1BasicVertexShaderID = polymost2_compileShader(GL_VERTEX_SHADER, (char*)polymost1Vert.Data());
GLuint polymost1ExtendedFragmentShaderID = polymost2_compileShader(GL_FRAGMENT_SHADER, (char*)polymost1Frag.Data());
glAttachShader(polymost1ExtendedShaderProgramID, polymost1BasicVertexShaderID);
glAttachShader(polymost1ExtendedShaderProgramID, polymost1ExtendedFragmentShaderID);
glLinkProgram(polymost1ExtendedShaderProgramID);
char buffer[10000];
glGetShaderInfoLog(polymost1BasicVertexShaderID, 10000, NULL, buffer);
if (*buffer)
{
initprintf("Vertex shader:\n%s\n", buffer);
}
glGetShaderInfoLog(polymost1ExtendedFragmentShaderID, 10000, NULL, buffer);
if (*buffer)
{
initprintf("Fragment shader:\n%s\n", buffer);
}
glGetProgramInfoLog(polymost1ExtendedShaderProgramID, 10000, NULL, buffer);
if (*buffer)
{
initprintf("Linking:\n%s\n", buffer);
}
GLint status = 0;
glGetProgramiv(polymost1ExtendedShaderProgramID, GL_LINK_STATUS, &status);
auto linked = (status == GL_TRUE);
if (!linked)
{
// only print message if there's an error.
wm_msgbox("Fatal error", "Unable to init shader\n");
exit(1);
}
// set defaults
polymost_setCurrentShaderProgram(polymost1ExtendedShaderProgramID);
glUniform1i(polymost1TexSamplerLoc, 0);
glUniform1i(polymost1PalSwapSamplerLoc, 1);
glUniform1i(polymost1PaletteSamplerLoc, 2);
glUniform1i(polymost1DetailSamplerLoc, 3);
glUniform1i(polymost1GlowSamplerLoc, 4);
polymost_setPalswapSize(256, numshades+1);
useShaderProgram(0);
lastbasepal = -1; lastbasepal = -1;
for (int basepalnum = 0; basepalnum < MAXBASEPALS; ++basepalnum) for (int basepalnum = 0; basepalnum < MAXBASEPALS; ++basepalnum)
@ -804,15 +397,6 @@ void polymost_glinit()
{ {
uploadpalswap(palookupnum); uploadpalswap(palookupnum);
} }
polymost_resetVertexPointers();
}
void polymost_init()
{
lastbasepal = -1;
polymost_resetVertexPointers();
} }
////////// VISIBILITY FOG ROUTINES ////////// ////////// VISIBILITY FOG ROUTINES //////////
@ -1060,7 +644,7 @@ static void resizeglcheck(void)
VSMatrix identity(0); VSMatrix identity(0);
GLInterface.SetMatrix(Matrix_ModelView, &identity); GLInterface.SetMatrix(Matrix_ModelView, &identity);
if (!nofog) polymost_setFogEnabled(true); if (!nofog) GLInterface.SetFogEnabled(true);
} }
} }
@ -1133,11 +717,6 @@ void uploadtexture(FHardwareTexture *tex, int32_t doalloc, vec2_t siz, int32_t t
void uploadbasepalette(int32_t basepalnum) void uploadbasepalette(int32_t basepalnum)
{ {
if (!polymost1ExtendedShaderProgramID)
{
//POGO: if we haven't initialized properly yet, we shouldn't be uploading base palettes
return;
}
if (!basepaltable[basepalnum]) if (!basepaltable[basepalnum])
{ {
return; return;
@ -1165,11 +744,6 @@ void uploadbasepalette(int32_t basepalnum)
void uploadpalswap(int32_t palookupnum) void uploadpalswap(int32_t palookupnum)
{ {
if (!polymost1ExtendedShaderProgramID)
{
//POGO: if we haven't initialized properly yet, we shouldn't be uploading palette swap tables
return;
}
if (!palookup[palookupnum]) if (!palookup[palookupnum])
{ {
return; return;
@ -1826,8 +1400,8 @@ static void polymost_updatePalette()
return; return;
} }
polymost_setPalswap(globalpal); GLInterface.SetPalswap(globalpal);
polymost_setShade(globalshade); GLInterface.SetShade(globalshade, numshades);
//POGO: only bind the base pal once when it's swapped //POGO: only bind the base pal once when it's swapped
if (curbasepal != lastbasepal) if (curbasepal != lastbasepal)
@ -1840,7 +1414,7 @@ static void polymost_updatePalette()
static void polymost_updaterotmat(void) static void polymost_updaterotmat(void)
{ {
if (currentShaderProgramID == polymost1CurrentShaderProgramID) if (1)
{ {
float matrix[16] = { float matrix[16] = {
1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f,
@ -1866,23 +1440,16 @@ static void polymost_updaterotmat(void)
multiplyMatrix4f(matrix, udmatrix); multiplyMatrix4f(matrix, udmatrix);
multiplyMatrix4f(matrix, tiltmatrix); multiplyMatrix4f(matrix, tiltmatrix);
#endif #endif
Bmemcpy(polymost1RotMatrix, matrix, sizeof(matrix)); GLInterface.SetMatrix(Matrix_View, matrix);
glUniformMatrix4fv(polymost1RotMatrixLoc, 1, false, polymost1RotMatrix);
} }
} }
static void polymost_identityrotmat(void) static void polymost_identityrotmat(void)
{ {
if (currentShaderProgramID == polymost1CurrentShaderProgramID) if (1)
{ {
float matrix[16] = { VSMatrix matrix(0);
1.f, 0.f, 0.f, 0.f, GLInterface.SetMatrix(Matrix_View, &matrix);
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
Bmemcpy(polymost1RotMatrix, matrix, sizeof(matrix));
glUniformMatrix4fv(polymost1RotMatrixLoc, 1, false, polymost1RotMatrix);
} }
} }
@ -2022,7 +1589,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
//POGOTODO: I could move this into bindPth //POGOTODO: I could move this into bindPth
if (!(pth->flags & PTH_INDEXED)) if (!(pth->flags & PTH_INDEXED))
polymost_usePaletteIndexing(false); GLInterface.UsePaletteIndexing(false);
// The entire logic here is just one lousy hack. // The entire logic here is just one lousy hack.
int mSampler = NoSampler; int mSampler = NoSampler;
@ -2065,7 +1632,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
(detailpth = texcache_fetch(globalpicnum, DETAILPAL, 0, method & ~DAMETH_MASKPROPS)) && (detailpth = texcache_fetch(globalpicnum, DETAILPAL, 0, method & ~DAMETH_MASKPROPS)) &&
detailpth->hicr && detailpth->hicr->palnum == DETAILPAL) detailpth->hicr && detailpth->hicr->palnum == DETAILPAL)
{ {
polymost_useDetailMapping(true); GLInterface.UseDetailMapping(true);
polymost_setupdetailtexture(3, detailpth->glpic); polymost_setupdetailtexture(3, detailpth->glpic);
texmat.loadIdentity(); texmat.loadIdentity();
@ -2089,7 +1656,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
(glowpth = texcache_fetch(globalpicnum, GLOWPAL, 0, (method & ~DAMETH_MASKPROPS) | DAMETH_MASK)) && (glowpth = texcache_fetch(globalpicnum, GLOWPAL, 0, (method & ~DAMETH_MASKPROPS) | DAMETH_MASK)) &&
glowpth->hicr && (glowpth->hicr->palnum == GLOWPAL)) glowpth->hicr && (glowpth->hicr->palnum == GLOWPAL))
{ {
polymost_useGlowMapping(true); GLInterface.UseGlowMapping(true);
polymost_setupglowtexture(4, glowpth->glpic); polymost_setupglowtexture(4, glowpth->glpic);
} }
} }
@ -2100,16 +1667,16 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32
int32_t size2; int32_t size2;
for (size2 = 1; size2 < size; size2 += size2) {} for (size2 = 1; size2 < size; size2 += size2) {}
if (size == size2) if (size == size2)
polymost_npotEmulation(false, 1.f, 0.f); GLInterface.SetNpotEmulation(false, 1.f, 0.f);
else else
{ {
float xOffset = 1.f / tilesiz[globalpicnum].x; float xOffset = 1.f / tilesiz[globalpicnum].x;
polymost_npotEmulation(true, (1.f*size2) / size, xOffset); GLInterface.SetNpotEmulation(true, (1.f*size2) / size, xOffset);
} }
} }
else else
{ {
polymost_npotEmulation(false, 1.f, 0.f); GLInterface.SetNpotEmulation(false, 1.f, 0.f);
} }
#endif #endif
@ -2333,12 +1900,10 @@ do
GLInterface.Draw(DT_TRIANGLE_FAN, data.first, npoints); GLInterface.Draw(DT_TRIANGLE_FAN, data.first, npoints);
} }
#ifdef USE_GLEXT GLInterface.UseDetailMapping(false);
GLInterface.UseGlowMapping(false);
GLInterface.SetNpotEmulation(false, 1.f, 0.f);
polymost_useDetailMapping(false);
polymost_useGlowMapping(false);
polymost_npotEmulation(false, 1.f, 0.f);
#endif
if (pth->hicr) if (pth->hicr)
{ {
VSMatrix identity(0); VSMatrix identity(0);
@ -2356,7 +1921,7 @@ do
if (!(pth->flags & PTH_INDEXED)) if (!(pth->flags & PTH_INDEXED))
{ {
// restore palette usage if we were just rendering a non-indexed color texture // restore palette usage if we were just rendering a non-indexed color texture
polymost_usePaletteIndexing(true); GLInterface.UsePaletteIndexing(true);
} }
if (fullbright_pass == 1) if (fullbright_pass == 1)
{ {
@ -2365,7 +1930,7 @@ do
globalshade = -128; globalshade = -128;
fullbright_pass = 2; fullbright_pass = 2;
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
GLInterface.SetDepthFunc(Depth_Equal); GLInterface.SetDepthFunc(Depth_Equal);
@ -2374,7 +1939,7 @@ do
GLInterface.SetDepthFunc(Depth_LessEqual); GLInterface.SetDepthFunc(Depth_LessEqual);
if (!nofog) if (!nofog)
polymost_setFogEnabled(true); GLInterface.SetFogEnabled(true);
globalshade = shade; globalshade = shade;
fullbright_pass = 0; fullbright_pass = 0;
@ -3771,7 +3336,7 @@ static void polymost_flatskyrender(vec2f_t const* const dpxy, int32_t const n, i
int const npot = (1<<(picsiz[globalpicnum]&15)) != tilesiz[globalpicnum].x; int const npot = (1<<(picsiz[globalpicnum]&15)) != tilesiz[globalpicnum].x;
int const xpanning = (r_parallaxskypanning?global_cf_xpanning:0); int const xpanning = (r_parallaxskypanning?global_cf_xpanning:0);
polymost_setClamp((npot || xpanning != 0) ? 0 : 2); GLInterface.SetClamp((npot || xpanning != 0) ? 0 : 2);
int picnumbak = globalpicnum; int picnumbak = globalpicnum;
ti = globalpicnum; ti = globalpicnum;
@ -3906,7 +3471,7 @@ static void polymost_flatskyrender(vec2f_t const* const dpxy, int32_t const n, i
globalpicnum = picnumbak; globalpicnum = picnumbak;
polymost_setClamp(0); GLInterface.SetClamp(0);
flatskyrender = 1; flatskyrender = 1;
} }
@ -4016,7 +3581,7 @@ static void polymost_drawalls(int32_t const bunch)
globvis2 = (sector[sectnum].visibility != 0) ? globvis2 = (sector[sectnum].visibility != 0) ?
mulscale4(globalcisibility2, (uint8_t)(sector[sectnum].visibility + 16)) : mulscale4(globalcisibility2, (uint8_t)(sector[sectnum].visibility + 16)) :
globalcisibility2; globalcisibility2;
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
tileUpdatePicnum(&globalpicnum, sectnum); tileUpdatePicnum(&globalpicnum, sectnum);
@ -4054,7 +3619,7 @@ static void polymost_drawalls(int32_t const bunch)
if (sec->visibility != 0) if (sec->visibility != 0)
globvis2 = mulscale4(globvis2, (uint8_t)(sec->visibility + 16)); globvis2 = mulscale4(globvis2, (uint8_t)(sec->visibility + 16));
float viscale = xdimscale*fxdimen*(.0000001f/256.f); float viscale = xdimscale*fxdimen*(.0000001f/256.f);
polymost_setVisibility(globvis2*viscale); GLInterface.SetVisibility(globvis2*viscale, fviewingrange);
//Use clamping for tiled sky textures //Use clamping for tiled sky textures
//(don't wrap around edges if the sky use multiple panels) //(don't wrap around edges if the sky use multiple panels)
@ -4073,7 +3638,7 @@ static void polymost_drawalls(int32_t const bunch)
skyclamphack = 0; skyclamphack = 0;
flatskyrender = 1; flatskyrender = 1;
globalshade += globvis2*xdimscale*fviewingrange*(1.f / (64.f * 65536.f * 256.f * 1024.f)); globalshade += globvis2*xdimscale*fviewingrange*(1.f / (64.f * 65536.f * 256.f * 1024.f));
polymost_setVisibility(0.f); GLInterface.SetVisibility(0.f, fviewingrange);
polymost_domost(x0,fy0,x1,fy1); polymost_domost(x0,fy0,x1,fy1);
flatskyrender = 0; flatskyrender = 0;
} }
@ -4353,7 +3918,7 @@ static void polymost_drawalls(int32_t const bunch)
skyclamphack = 0; skyclamphack = 0;
skyzbufferhack = 0; skyzbufferhack = 0;
if (!nofog) if (!nofog)
polymost_setFogEnabled(true); GLInterface.SetFogEnabled(true);
} }
// Ceiling // Ceiling
@ -4373,7 +3938,7 @@ static void polymost_drawalls(int32_t const bunch)
globvis2 = (sector[sectnum].visibility != 0) ? globvis2 = (sector[sectnum].visibility != 0) ?
mulscale4(globalcisibility2, (uint8_t)(sector[sectnum].visibility + 16)) : mulscale4(globalcisibility2, (uint8_t)(sector[sectnum].visibility + 16)) :
globalcisibility2; globalcisibility2;
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
tileUpdatePicnum(&globalpicnum, sectnum); tileUpdatePicnum(&globalpicnum, sectnum);
@ -4411,7 +3976,7 @@ static void polymost_drawalls(int32_t const bunch)
if (sec->visibility != 0) if (sec->visibility != 0)
globvis2 = mulscale4(globvis2, (uint8_t)(sec->visibility + 16)); globvis2 = mulscale4(globvis2, (uint8_t)(sec->visibility + 16));
float viscale = xdimscale*fxdimen*(.0000001f/256.f); float viscale = xdimscale*fxdimen*(.0000001f/256.f);
polymost_setVisibility(globvis2*viscale); GLInterface.SetVisibility(globvis2*viscale, fviewingrange);
//Use clamping for tiled sky textures //Use clamping for tiled sky textures
//(don't wrap around edges if the sky use multiple panels) //(don't wrap around edges if the sky use multiple panels)
@ -4430,7 +3995,7 @@ static void polymost_drawalls(int32_t const bunch)
skyclamphack = 0; skyclamphack = 0;
flatskyrender = 1; flatskyrender = 1;
globalshade += globvis2*xdimscale*fviewingrange*(1.f / (64.f * 65536.f * 256.f * 1024.f)); globalshade += globvis2*xdimscale*fviewingrange*(1.f / (64.f * 65536.f * 256.f * 1024.f));
polymost_setVisibility(0.f); GLInterface.SetVisibility(0.f, fviewingrange);
polymost_domost(x1,cy1,x0,cy0); polymost_domost(x1,cy1,x0,cy0);
flatskyrender = 0; flatskyrender = 0;
} }
@ -4713,7 +4278,7 @@ static void polymost_drawalls(int32_t const bunch)
skyclamphack = 0; skyclamphack = 0;
skyzbufferhack = 0; skyzbufferhack = 0;
if (!nofog) if (!nofog)
polymost_setFogEnabled(true); GLInterface.SetFogEnabled(true);
} }
#ifdef YAX_ENABLE #ifdef YAX_ENABLE
@ -4721,13 +4286,6 @@ static void polymost_drawalls(int32_t const bunch)
{ {
int32_t baselevp, checkcf, i, j; int32_t baselevp, checkcf, i, j;
int16_t bn[2]; int16_t bn[2];
# if 0
int32_t obunchchk = (1 && yax_globalbunch>=0 &&
haveymost[yax_globalbunch>>3]&pow2char[yax_globalbunch&7]);
// if (obunchchk)
const int32_t x2 = yax_globalbunch*xdimen;
# endif
baselevp = (yax_globallev == YAX_MAXDRAWS); baselevp = (yax_globallev == YAX_MAXDRAWS);
yax_getbunches(sectnum, &bn[0], &bn[1]); yax_getbunches(sectnum, &bn[0], &bn[1]);
@ -4806,7 +4364,7 @@ static void polymost_drawalls(int32_t const bunch)
if (sector[sectnum].visibility != 0) globvis = mulscale4(globvis, (uint8_t)(sector[sectnum].visibility+16)); if (sector[sectnum].visibility != 0) globvis = mulscale4(globvis, (uint8_t)(sector[sectnum].visibility+16));
globvis2 = globalvisibility2; globvis2 = globalvisibility2;
if (sector[sectnum].visibility != 0) globvis2 = mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility+16)); if (sector[sectnum].visibility != 0) globvis2 = mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility+16));
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
globalorientation = wal->cstat; globalorientation = wal->cstat;
tileUpdatePicnum(&globalpicnum, wallnum+16384); tileUpdatePicnum(&globalpicnum, wallnum+16384);
@ -4851,7 +4409,7 @@ static void polymost_drawalls(int32_t const bunch)
if (sector[sectnum].visibility != 0) globvis = mulscale4(globvis, (uint8_t)(sector[sectnum].visibility+16)); if (sector[sectnum].visibility != 0) globvis = mulscale4(globvis, (uint8_t)(sector[sectnum].visibility+16));
globvis2 = globalvisibility2; globvis2 = globalvisibility2;
if (sector[sectnum].visibility != 0) globvis2 = mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility+16)); if (sector[sectnum].visibility != 0) globvis2 = mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility+16));
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
globalorientation = nwal->cstat; globalorientation = nwal->cstat;
tileUpdatePicnum(&globalpicnum, wallnum+16384); tileUpdatePicnum(&globalpicnum, wallnum+16384);
@ -4904,7 +4462,7 @@ static void polymost_drawalls(int32_t const bunch)
globvis2 = (sector[sectnum].visibility != 0) ? globvis2 = (sector[sectnum].visibility != 0) ?
mulscale4(globalvisibility2, (uint8_t)(sector[sectnum].visibility + 16)) : mulscale4(globalvisibility2, (uint8_t)(sector[sectnum].visibility + 16)) :
globalvisibility2; globalvisibility2;
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
globalorientation = wal->cstat; globalorientation = wal->cstat;
tileUpdatePicnum(&globalpicnum, wallnum+16384); tileUpdatePicnum(&globalpicnum, wallnum+16384);
@ -5239,7 +4797,7 @@ void polymost_drawrooms()
GLInterface.EnableDepthTest(true); GLInterface.EnableDepthTest(true);
GLInterface.SetDepthFunc(Depth_Always); GLInterface.SetDepthFunc(Depth_Always);
polymost_setBrightness(r_brightnesshack); GLInterface.SetBrightness(r_brightnesshack);
gvrcorrection = viewingrange*(1.f/65536.f); gvrcorrection = viewingrange*(1.f/65536.f);
if (glprojectionhacks == 2) if (glprojectionhacks == 2)
@ -5270,7 +4828,7 @@ void polymost_drawrooms()
gvisibility = ((float)globalvisibility)*FOGSCALE; gvisibility = ((float)globalvisibility)*FOGSCALE;
polymost_shadeInterpolate(r_shadeinterpolate); GLInterface.SetShadeInterpolate(r_shadeinterpolate);
//global cos/sin height angle //global cos/sin height angle
if (r_yshearing) if (r_yshearing)
@ -5514,7 +5072,7 @@ static void polymost_drawmaskwallinternal(int32_t wallIndex)
globvis2 = globalvisibility2; globvis2 = globalvisibility2;
globvis2 = (sector[sectnum].visibility != 0) ? mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility + 16)) : globalvisibility2; globvis2 = (sector[sectnum].visibility != 0) ? mulscale4(globvis2, (uint8_t)(sector[sectnum].visibility + 16)) : globalvisibility2;
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
globalshade = (int32_t)wal->shade; globalshade = (int32_t)wal->shade;
globalpal = (int32_t)((uint8_t)wal->pal); globalpal = (int32_t)((uint8_t)wal->pal);
@ -5894,7 +5452,7 @@ void polymost_drawsprite(int32_t snum)
globvis2 = globalvisibility2; globvis2 = globalvisibility2;
if (sector[tspr->sectnum].visibility != 0) if (sector[tspr->sectnum].visibility != 0)
globvis2 = mulscale4(globvis2, (uint8_t)(sector[tspr->sectnum].visibility + 16)); globvis2 = mulscale4(globvis2, (uint8_t)(sector[tspr->sectnum].visibility + 16));
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
vec2_t off = { 0, 0 }; vec2_t off = { 0, 0 };
@ -6308,7 +5866,7 @@ void polymost_drawsprite(int32_t snum)
globvis2 = globalhisibility2; globvis2 = globalhisibility2;
if (sector[tspr->sectnum].visibility != 0) if (sector[tspr->sectnum].visibility != 0)
globvis2 = mulscale4(globvis2, (uint8_t)(sector[tspr->sectnum].visibility + 16)); globvis2 = mulscale4(globvis2, (uint8_t)(sector[tspr->sectnum].visibility + 16));
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
if ((globalorientation & 64) != 0 && (globalposz > tspr->z) == (!(globalorientation & 8))) if ((globalorientation & 64) != 0 && (globalposz > tspr->z) == (!(globalorientation & 8)))
goto _drawsprite_return; goto _drawsprite_return;
@ -6691,12 +6249,12 @@ void polymost_dorotatespritemodel(int32_t sx, int32_t sy, int32_t z, int16_t a,
spriteext[tspr.owner].alpha = daalpha * (1.0f / 255.0f); spriteext[tspr.owner].alpha = daalpha * (1.0f / 255.0f);
tspr.blend = dablend; tspr.blend = dablend;
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
if (videoGetRenderMode() == REND_POLYMOST) if (videoGetRenderMode() == REND_POLYMOST)
polymost_mddraw(&tspr); polymost_mddraw(&tspr);
if (!nofog) polymost_setFogEnabled(true); if (!nofog) GLInterface.SetFogEnabled(true);
gvrcorrection = ogvrcorrection; gvrcorrection = ogvrcorrection;
viewingrange = oldviewingrange; viewingrange = oldviewingrange;
@ -6730,8 +6288,8 @@ void polymost_dorotatesprite(int32_t sx, int32_t sy, int32_t z, int16_t a, int16
globvis = 0; globvis = 0;
globvis2 = 0; globvis2 = 0;
polymost_setClamp(1+2); GLInterface.SetClamp(1+2);
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
int32_t const ogpicnum = globalpicnum; int32_t const ogpicnum = globalpicnum;
globalpicnum = picnum; globalpicnum = picnum;
@ -6931,14 +6489,14 @@ void polymost_dorotatesprite(int32_t sx, int32_t sy, int32_t z, int16_t a, int16
xtex.v = yv2*(1.0/65536.0); xtex.v = yv2*(1.0/65536.0);
ytex.v = yv*(1.0/65536.0); ytex.v = yv*(1.0/65536.0);
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
pow2xsplit = 0; polymost_drawpoly(fpxy,n,method); pow2xsplit = 0; polymost_drawpoly(fpxy,n,method);
if (!nofog) polymost_setFogEnabled(true); if (!nofog) GLInterface.SetFogEnabled(true);
} }
GLInterface.EnableAlphaTest(false); GLInterface.EnableAlphaTest(false);
GLInterface.EnableBlend(false); GLInterface.EnableBlend(false);
polymost_setClamp(0); GLInterface.SetClamp(0);
GLInterface.SetMatrix(Matrix_Projection, &oldproj); GLInterface.SetMatrix(Matrix_Projection, &oldproj);
GLInterface.SetMatrix(Matrix_ModelView, &oldmv); GLInterface.SetMatrix(Matrix_ModelView, &oldmv);
@ -7130,7 +6688,7 @@ void polymost_fillpolygon(int32_t npoints)
polymost_outputGLDebugMessage(3, "polymost_fillpolygon(npoints:%d)", npoints); polymost_outputGLDebugMessage(3, "polymost_fillpolygon(npoints:%d)", npoints);
globvis2 = 0; globvis2 = 0;
polymost_setVisibility(globvis2); GLInterface.SetVisibility(globvis2, fviewingrange);
globalx1 = mulscale16(globalx1,xyaspect); globalx1 = mulscale16(globalx1,xyaspect);
globaly2 = mulscale16(globaly2,xyaspect); globaly2 = mulscale16(globaly2,xyaspect);
@ -7157,7 +6715,7 @@ void polymost_fillpolygon(int32_t npoints)
polymost_bindPth(pth, -1); polymost_bindPth(pth, -1);
if (!(pth->flags & PTH_INDEXED)) if (!(pth->flags & PTH_INDEXED))
polymost_usePaletteIndexing(false); GLInterface.UsePaletteIndexing(false);
} }
polymost_updatePalette(); polymost_updatePalette();
@ -7182,7 +6740,7 @@ void polymost_fillpolygon(int32_t npoints)
if (pth && !(pth->flags & PTH_INDEXED)) if (pth && !(pth->flags & PTH_INDEXED))
{ {
// restore palette usage if we were just rendering a non-indexed color texture // restore palette usage if we were just rendering a non-indexed color texture
polymost_usePaletteIndexing(true); GLInterface.UsePaletteIndexing(true);
} }
} }
@ -7241,6 +6799,17 @@ static int32_t gen_font_glyph_tex(void)
return 0; return 0;
} }
// These are used by the automap drawers
void renderDisableFog(void)
{
GLInterface.SetFogEnabled(false);
}
void renderEnableFog(void)
{
if (!nofog) GLInterface.SetFogEnabled(true);
}
int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t backcol, const char *name, char fontsize) int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t backcol, const char *name, char fontsize)
{ {
int const arbackcol = (unsigned)backcol < 256 ? backcol : 0; int const arbackcol = (unsigned)backcol < 256 ? backcol : 0;
@ -7261,9 +6830,8 @@ int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t ba
return -1; return -1;
GLInterface.BindTexture(0, polymosttext); GLInterface.BindTexture(0, polymosttext);
polymost_setTexturePosSize({0, 0, 1, 1});
polymost_usePaletteIndexing(false); GLInterface.UsePaletteIndexing(false);
polymostSet2dView(); // disables blending, texturing, and depth testing polymostSet2dView(); // disables blending, texturing, and depth testing
@ -7271,7 +6839,7 @@ int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t ba
GLInterface.SetDepthMask(false); GLInterface.SetDepthMask(false);
// XXX: Don't fogify the OSD text in Mapster32 with r_usenewshading >= 2. // XXX: Don't fogify the OSD text in Mapster32 with r_usenewshading >= 2.
polymost_setFogEnabled(false); GLInterface.SetFogEnabled(false);
// We want to have readable text in wireframe mode, too: // We want to have readable text in wireframe mode, too:
GLInterface.SetWireframe(false); GLInterface.SetWireframe(false);
lastglpolygonmode = 0; lastglpolygonmode = 0;
@ -7360,9 +6928,9 @@ int32_t polymost_printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t ba
GLInterface.SetDepthMask(true); GLInterface.SetDepthMask(true);
if (!nofog) polymost_setFogEnabled(true); if (!nofog) GLInterface.SetFogEnabled(true);
polymost_usePaletteIndexing(true); GLInterface.UsePaletteIndexing(true);
return 0; return 0;
} }

View file

@ -1132,15 +1132,14 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr)
#endif #endif
const float phack[2] = { 0, 1.f/256.f }; const float phack[2] = { 0, 1.f/256.f };
char prevClamp = polymost_getClamp(); int prevClamp = GLInterface.GetClamp();
polymost_setClamp(0); GLInterface.SetClamp(0);
if (!m->texid[globalpal]) if (!m->texid[globalpal])
m->texid[globalpal] = gloadtex(m->mytex, m->mytexx, m->mytexy, m->is8bit, globalpal); m->texid[globalpal] = gloadtex(m->mytex, m->mytexx, m->mytexy, m->is8bit, globalpal);
GLInterface.BindTexture(0, m->texid[globalpal]); GLInterface.BindTexture(0, m->texid[globalpal]);
polymost_usePaletteIndexing(false); GLInterface.UsePaletteIndexing(false);
polymost_setTexturePosSize({ 0.f, 0.f, 1.f, 1.f });
auto data = GLInterface.AllocVertices(m->qcnt * 4); auto data = GLInterface.AllocVertices(m->qcnt * 4);
auto vt = data.second; auto vt = data.second;
@ -1183,7 +1182,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr)
} }
GLInterface.Draw(DT_QUADS, qstart, qdone * 4); GLInterface.Draw(DT_QUADS, qstart, qdone * 4);
polymost_setClamp(prevClamp); GLInterface.SetClamp(prevClamp);
//------------ //------------
GLInterface.SetCull(Cull_None); GLInterface.SetCull(Cull_None);

View file

@ -0,0 +1,26 @@
#pragma once
class PolymostShader;
struct PolymostRenderState
{
int PalSwapIndex;
//float PalswapPos[2];
//float PalswapSize[2];
float Clamp[2];
float Shade;
float NumShades = 64.f;
float VisFactor = 128.f;
float FogEnabled = 1.f;
float UseColorOnly;
float UsePalette = 1.f;
float UseDetailMapping;
float UseGlowMapping;
float NPOTEmulation;
float NPOTEmulationFactor = 1.f;
float NPOTEmulationXOffset;
float Brightness = 1.f;
float ShadeInterpolate = 1.f;
void Apply(PolymostShader *shader);
};

View file

@ -0,0 +1,179 @@
/*
** gl_shader.cpp
**
** GLSL shader handling
**
**---------------------------------------------------------------------------
** Copyright 2004-2019 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
** 4. When not used as part of GZDoom or a GZDoom derivative, this code will be
** covered by the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation; either version 2.1 of the License, or (at
** your option) any later version.
** 5. Full disclosure of the entire project's source code, except for third
** party libraries is mandatory. (NOTE: This clause is non-negotiable!)
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include <memory>
#include "glbackend.h"
#include "glad/glad.h"
#include "gl_shader.h"
#include "zstring.h"
#include "baselayer.h"
//==========================================================================
//
//
//
//==========================================================================
bool FShader::Load(const char * name, const char * vert_prog, const char * frag_prog)
{
FString error;
static char buffer[10000];
hVertProg = glCreateShader(GL_VERTEX_SHADER);
hFragProg = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(hVertProg, 1, &vert_prog, nullptr);
glShaderSource(hFragProg, 1, &frag_prog, nullptr);
glCompileShader(hVertProg);
glCompileShader(hFragProg);
hShader = glCreateProgram();
glAttachShader(hShader, hVertProg);
glAttachShader(hShader, hFragProg);
//glBindAttribLocation(hShader, VATTR_VERTEX, "aPosition");
//glBindAttribLocation(hShader, VATTR_TEXCOORD, "aTexCoord");
//glBindAttribLocation(hShader, VATTR_COLOR, "aColor");
//glBindAttribLocation(hShader, VATTR_VERTEX2, "aVertex2");
glLinkProgram(hShader);
glGetShaderInfoLog(hVertProg, 10000, NULL, buffer);
if (*buffer)
{
error << "Vertex shader:\n" << buffer << "\n";
}
glGetShaderInfoLog(hFragProg, 10000, NULL, buffer);
if (*buffer)
{
error << "Fragment shader:\n" << buffer << "\n";
}
glGetProgramInfoLog(hShader, 10000, NULL, buffer);
if (*buffer)
{
error << "Linking:\n" << buffer << "\n";
}
int linked;
glGetProgramiv(hShader, GL_LINK_STATUS, &linked);
if (linked == 0)
{
// only print message if there's an error.
initprintf("Init Shader '%s':\n%s\n", name, error.GetChars());
return false;
}
return true;
}
//==========================================================================
//
//
//
//==========================================================================
FShader::~FShader()
{
glDeleteProgram(hShader);
glDeleteShader(hVertProg);
glDeleteShader(hFragProg);
}
//==========================================================================
//
//
//
//==========================================================================
bool FShader::Bind()
{
glUseProgram(hShader);
return true;
}
//==========================================================================
//
//
//
//==========================================================================
bool PolymostShader::Load(const char * name, const char * vert_prog, const char * frag_prog)
{
if (!FShader::Load(name, vert_prog, frag_prog)) return false;
PalswapPos.Init(hShader, "u_palswapPos");
PalswapSize.Init(hShader, "u_palswapSize");
Clamp.Init(hShader, "u_clamp");
Shade.Init(hShader, "u_shade");
NumShades.Init(hShader, "u_numShades");
VisFactor.Init(hShader, "u_visFactor");
FogEnabled.Init(hShader, "u_fogEnabled");
UseColorOnly.Init(hShader, "u_useColorOnly");
UsePalette.Init(hShader, "u_usePalette");
UseDetailMapping.Init(hShader, "u_useDetailMapping");
UseGlowMapping.Init(hShader, "u_useGlowMapping");
NPOTEmulation.Init(hShader, "u_npotEmulation");
NPOTEmulationFactor.Init(hShader, "u_npotEmulationFactor");
NPOTEmulationXOffset.Init(hShader, "u_npotEmulationXOffset");
Brightness.Init(hShader, "u_brightness");
RotMatrix.Init(hShader, "u_rotMatrix");
ShadeInterpolate.Init(hShader, "u_shadeInterpolate");
glUseProgram(hShader);
int SamplerLoc;
SamplerLoc = glGetUniformLocation(hShader, "s_texture");
glUniform1i(SamplerLoc, 0);
SamplerLoc = glGetUniformLocation(hShader, "s_palswap");
glUniform1i(SamplerLoc, 1);
SamplerLoc = glGetUniformLocation(hShader, "s_palette");
glUniform1i(SamplerLoc, 2);
SamplerLoc = glGetUniformLocation(hShader, "s_detail");
glUniform1i(SamplerLoc, 3);
SamplerLoc = glGetUniformLocation(hShader, "s_glow");
glUniform1i(SamplerLoc, 4);
glUseProgram(0);
return true;
}

View file

@ -0,0 +1,60 @@
#pragma once;
#include "gl_uniform.h"
class FShader
{
friend class FShaderManager;
friend class FRenderState;
unsigned int hVertProg = 0;
unsigned int hFragProg = 0;
protected:
unsigned int hShader = 0;
#if 0
FName mName;
int projectionmatrix_index;
int viewmatrix_index;
int modelmatrix_index;
int texturematrix_index;
bool currentTextureMatrixState = false;
bool currentModelMatrixState = false;
#endif
public:
FShader() = default;
virtual ~FShader();
virtual bool Load(const char * name, const char * vert_prog_lump, const char * fragprog); //, const char * fragprog2, const char *defines);
bool Bind();
unsigned int GetHandle() const { return hShader; }
};
class PolymostShader : public FShader
{
public:
FBufferedUniform2f PalswapPos;
FBufferedUniform2f PalswapSize;
FBufferedUniform2f Clamp;
FBufferedUniform1f Shade;
FBufferedUniform1f NumShades;
FBufferedUniform1f VisFactor;
FBufferedUniform1f FogEnabled;
FBufferedUniform1f UseColorOnly;
FBufferedUniform1f UsePalette;
FBufferedUniform1f UseDetailMapping;
FBufferedUniform1f UseGlowMapping;
FBufferedUniform1f NPOTEmulation;
FBufferedUniform1f NPOTEmulationFactor;
FBufferedUniform1f NPOTEmulationXOffset;
FBufferedUniform1f Brightness;
FUniformMatrix4f RotMatrix;
FBufferedUniform1f ShadeInterpolate;
public:
PolymostShader() = default;
virtual bool Load(const char * name, const char * vert_prog_lump, const char * fragprog); //, const char * fragprog2, const char *defines);
};

View file

@ -0,0 +1,94 @@
#pragma once
class FBufferedUniform1f
{
float mBuffer;
int mIndex;
public:
void Init(GLuint hShader, const GLchar *name)
{
mIndex = glGetUniformLocation(hShader, name);
mBuffer = 0;
}
void Set(float newvalue)
{
if (newvalue != mBuffer)
{
mBuffer = newvalue;
glUniform1f(mIndex, newvalue);
}
}
};
class FBufferedUniform2f
{
float mBuffer[2];
int mIndex;
public:
void Init(GLuint hShader, const GLchar *name)
{
mIndex = glGetUniformLocation(hShader, name);
memset(mBuffer, 0, sizeof(mBuffer));
}
void Set(const float *newvalue)
{
if (memcmp(newvalue, mBuffer, sizeof(mBuffer)))
{
memcpy(mBuffer, newvalue, sizeof(mBuffer));
glUniform2fv(mIndex, 1, newvalue);
}
}
void Set(float f1, float f2)
{
if (mBuffer[0] != f1 || mBuffer[1] != f2)
{
mBuffer[0] = f1;
mBuffer[1] = f2;
glUniform2fv(mIndex, 1, mBuffer);
}
}
};
class FBufferedUniform4f
{
float mBuffer[4];
int mIndex;
public:
void Init(GLuint hShader, const GLchar *name)
{
mIndex = glGetUniformLocation(hShader, name);
memset(mBuffer, 0, sizeof(mBuffer));
}
void Set(const float *newvalue)
{
if (memcmp(newvalue, mBuffer, sizeof(mBuffer)))
{
memcpy(mBuffer, newvalue, sizeof(mBuffer));
glUniform4fv(mIndex, 1, newvalue);
}
}
};
class FUniformMatrix4f
{
int mIndex;
public:
void Init(GLuint hShader, const GLchar *name)
{
mIndex = glGetUniformLocation(hShader, name);
}
void Set(const float *newvalue)
{
glUniformMatrix4fv(mIndex, 1, false, newvalue);
}
};

View file

@ -2,6 +2,7 @@
#include "glbackend.h" #include "glbackend.h"
#include "glad/glad.h" #include "glad/glad.h"
#include "gl_samplers.h" #include "gl_samplers.h"
#include "gl_shader.h"
#include "baselayer.h" #include "baselayer.h"
#include "resourcefile.h" #include "resourcefile.h"
@ -60,9 +61,29 @@ void GLInstance::Init()
osdcmd_glinfo(NULL); osdcmd_glinfo(NULL);
glinfo.dumped = 1; glinfo.dumped = 1;
} }
new(&renderState) PolymostRenderState; // reset to defaults.
LoadPolymostShader();
} }
void GLInstance::LoadPolymostShader()
{
auto fr1 = GetBaseResource("demolition/shaders/glsl/polymost.vp");
TArray<uint8_t> polymost1Vert = fr1.Read();
fr1 = GetBaseResource("demolition/shaders/glsl/polymost.fp");
TArray<uint8_t> polymost1Frag = fr1.Read();
// Zero-terminate both strings.
polymost1Vert.Push(0);
polymost1Frag.Push(0);
polymostShader = new PolymostShader();
if (!polymostShader->Load("PolymostShader", (const char*)polymost1Vert.Data(), (const char*)polymost1Frag.Data()))
{
exit(1);
}
SetPolymostShader();
}
void GLInstance::InitGLState(int fogmode, int multisample) void GLInstance::InitGLState(int fogmode, int multisample)
{ {
glShadeModel(GL_SMOOTH); // GL_FLAT glShadeModel(GL_SMOOTH); // GL_FLAT
@ -89,6 +110,10 @@ void GLInstance::Deinit()
{ {
if (mSamplers) delete mSamplers; if (mSamplers) delete mSamplers;
mSamplers = nullptr; mSamplers = nullptr;
if (polymostShader) delete polymostShader;
polymostShader = nullptr;
activeShader = nullptr;
} }
std::pair<size_t, BaseVertex *> GLInstance::AllocVertices(size_t num) std::pair<size_t, BaseVertex *> GLInstance::AllocVertices(size_t num)
@ -108,6 +133,7 @@ static GLint primtypes[] =
void GLInstance::Draw(EDrawType type, size_t start, size_t count) void GLInstance::Draw(EDrawType type, size_t start, size_t count)
{ {
if (activeShader == polymostShader) renderState.Apply(polymostShader);
glBegin(primtypes[type]); glBegin(primtypes[type]);
auto p = &Buffer[start]; auto p = &Buffer[start];
for (size_t i = 0; i < count; i++, p++) for (size_t i = 0; i < count; i++, p++)
@ -188,22 +214,30 @@ void GLInstance::SetMatrix(int num, const VSMatrix *mat)
matrices[num] = *mat; matrices[num] = *mat;
switch(num) switch(num)
{ {
default:
return;
case Matrix_View:
polymostShader->RotMatrix.Set(mat->get());
break;
case Matrix_Projection: case Matrix_Projection:
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadMatrixf(mat->get());
break; break;
case Matrix_ModelView: case Matrix_ModelView:
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(mat->get());
break; break;
default: case Matrix_Texture3:
glActiveTexture(GL_TEXTURE0 + num - Matrix_Texture0); case Matrix_Texture4:
glActiveTexture(GL_TEXTURE3 + num - Matrix_Texture3);
glMatrixMode(GL_TEXTURE); glMatrixMode(GL_TEXTURE);
glLoadMatrixf(mat->get());
glActiveTexture(GL_TEXTURE0);
break; break;
} }
glLoadMatrixf(mat->get());
glMatrixMode(GL_MODELVIEW);
if (num > Matrix_Texture0) glActiveTexture(GL_TEXTURE0);
} }
void GLInstance::EnableStencilWrite(int value) void GLInstance::EnableStencilWrite(int value)
@ -326,3 +360,68 @@ void GLInstance::ReadPixels(int xdim, int ydim, uint8_t* buffer)
{ {
glReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, buffer); glReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, buffer);
} }
void GLInstance::SetPolymostShader()
{
if (activeShader != polymostShader)
{
polymostShader->Bind();
activeShader = polymostShader;
}
//GLInterface.BindTexture(1, palswapTextureID);
//GLInterface.BindTexture(2, paletteTextureIDs[curbasepal]);
}
void PolymostRenderState::Apply(PolymostShader* shader)
{
shader->Clamp.Set(Clamp);
shader->Shade.Set(Shade);
shader->NumShades.Set(NumShades);
shader->VisFactor.Set(VisFactor);
shader->FogEnabled.Set(FogEnabled);
shader->UseColorOnly.Set(UseColorOnly);
shader->UsePalette.Set(UsePalette);
shader->UseDetailMapping.Set(UseDetailMapping);
shader->UseGlowMapping.Set(UseGlowMapping);
shader->NPOTEmulation.Set(NPOTEmulation);
shader->NPOTEmulationFactor.Set(NPOTEmulationFactor);
shader->NPOTEmulationXOffset.Set(NPOTEmulationXOffset);
shader->ShadeInterpolate.Set(ShadeInterpolate);
shader->Brightness.Set(Brightness);
}
#if 0
static void polymost_setPalswap(uint32_t index)
{
static uint32_t lastPalswapIndex;
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
lastPalswapIndex = index;
polymost1PalswapPos.x = index * polymost1PalswapSize.x;
polymost1PalswapPos.y = floorf(polymost1PalswapPos.x);
polymost1PalswapPos = { polymost1PalswapPos.x - polymost1PalswapPos.y + (0.5f / PALSWAP_TEXTURE_SIZE),
polymost1PalswapPos.y * polymost1PalswapSize.y + (0.5f / PALSWAP_TEXTURE_SIZE) };
glUniform2f(polymost1PalswapPosLoc, polymost1PalswapPos.x, polymost1PalswapPos.y);
}
static void polymost_setPalswapSize(uint32_t width, uint32_t height)
{
if (currentShaderProgramID != polymost1CurrentShaderProgramID)
return;
polymost1PalswapSize = { width * (1.f / PALSWAP_TEXTURE_SIZE),
height * (1.f / PALSWAP_TEXTURE_SIZE) };
polymost1PalswapInnerSize = { (width - 1) * (1.f / PALSWAP_TEXTURE_SIZE),
(height - 1) * (1.f / PALSWAP_TEXTURE_SIZE) };
glUniform2f(polymost1PalswapSizeLoc, polymost1PalswapInnerSize.x, polymost1PalswapInnerSize.y);
}
#endif

View file

@ -5,9 +5,12 @@
#include <map> #include <map>
#include "gl_samplers.h" #include "gl_samplers.h"
#include "gl_hwtexture.h" #include "gl_hwtexture.h"
#include "gl_renderstate.h"
#include "matrix.h" #include "matrix.h"
class FSamplerManager; class FSamplerManager;
class FShader;
class PolymostShader;
struct glinfo_t { struct glinfo_t {
const char* vendor; const char* vendor;
@ -59,16 +62,12 @@ enum EDrawType
enum EMatrixType enum EMatrixType
{ {
Matrix_View,
Matrix_Projection, Matrix_Projection,
Matrix_ModelView, Matrix_ModelView,
Matrix_Texture0,
Matrix_Texture1,
Matrix_Texture2,
Matrix_Texture3, Matrix_Texture3,
Matrix_Texture4, Matrix_Texture4,
Matrix_Texture5, // These are the only ones being used.
Matrix_Texture6,
Matrix_Texture7,
NUMMATRICES NUMMATRICES
}; };
@ -128,6 +127,9 @@ class GLInstance
int maxTextureSize; int maxTextureSize;
VSMatrix matrices[NUMMATRICES]; VSMatrix matrices[NUMMATRICES];
PolymostRenderState renderState;
FShader* activeShader;
PolymostShader* polymostShader;
public: public:
@ -136,6 +138,7 @@ public:
void Init(); void Init();
void InitGLState(int fogmode, int multisample); void InitGLState(int fogmode, int multisample);
void LoadPolymostShader();
void Deinit(); void Deinit();
@ -188,8 +191,79 @@ public:
void SetViewport(int x, int y, int w, int h); void SetViewport(int x, int y, int w, int h);
void SetAlphaThreshold(float al); void SetAlphaThreshold(float al);
void SetWireframe(bool on); void SetWireframe(bool on);
void SetPolymostShader();
void ReadPixels(int w, int h, uint8_t* buffer); void ReadPixels(int w, int h, uint8_t* buffer);
void SetPalswap(uint32_t index)
{
renderState.PalSwapIndex = index;
}
int GetClamp()
{
return int(renderState.Clamp[0] + 2*renderState.Clamp[1]);
}
void SetClamp(int clamp)
{
renderState.Clamp[0] = clamp & 1;
renderState.Clamp[1] = !!(clamp & 2);
}
void SetShade(int32_t shade, int numshades)
{
renderState.Shade = shade;
renderState.NumShades = numshades;
}
void SetVisibility(float visibility, float fviewingrange)
{
renderState.VisFactor = visibility * fviewingrange * (1.f / (64.f * 65536.f));
}
void SetFogEnabled(bool fogEnabled)
{
renderState.FogEnabled = fogEnabled;
}
void UseColorOnly(bool useColorOnly)
{
renderState.UseColorOnly = useColorOnly;
}
void UsePaletteIndexing(bool usePaletteIndexing)
{
renderState.UsePalette = usePaletteIndexing;
}
void UseDetailMapping(bool useDetailMapping)
{
renderState.UseDetailMapping = useDetailMapping;
}
void UseGlowMapping(bool useGlowMapping)
{
renderState.UseGlowMapping = useGlowMapping;
}
void SetNpotEmulation(bool npotEmulation, float factor, float xOffset)
{
renderState.NPOTEmulation = npotEmulation;
renderState.NPOTEmulationFactor = factor;
renderState.NPOTEmulationXOffset = xOffset;
}
void SetShadeInterpolate(int32_t shadeInterpolate)
{
renderState.ShadeInterpolate = shadeInterpolate;
}
void SetBrightness(int brightness)
{
renderState.Brightness = 8.f / (brightness + 8.f);
}
}; };
extern GLInstance GLInterface; extern GLInstance GLInterface;