diff --git a/src/gl/models/gl_models_md2.cpp b/src/gl/models/gl_models_md2.cpp index 76835c737..3d0fad8aa 100644 --- a/src/gl/models/gl_models_md2.cpp +++ b/src/gl/models/gl_models_md2.cpp @@ -303,7 +303,7 @@ void FDMDModel::RenderFrame(FTexture * skin, int frameno, int frameno2, double i FMaterial * tex = FMaterial::ValidateTexture(skin, false); - tex->Bind(CLAMP_NONE, translation, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_NONE, translation, -1, false); gl_RenderState.Apply(); GLRenderer->mModelVBO->SetupFrame(frames[frameno].vindex, frames[frameno2].vindex, inter); diff --git a/src/gl/models/gl_models_md3.cpp b/src/gl/models/gl_models_md3.cpp index d3f71af5c..4b1788306 100644 --- a/src/gl/models/gl_models_md3.cpp +++ b/src/gl/models/gl_models_md3.cpp @@ -267,7 +267,7 @@ void FMD3Model::RenderFrame(FTexture * skin, int frameno, int frameno2, double i FMaterial * tex = FMaterial::ValidateTexture(surfaceSkin, false); - tex->Bind(CLAMP_NONE, translation, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_NONE, translation, -1, false); gl_RenderState.Apply(); GLRenderer->mModelVBO->SetupFrame(surf->vindex + frameno * surf->numVertices, surf->vindex + frameno2 * surf->numVertices, inter); diff --git a/src/gl/models/gl_voxels.cpp b/src/gl/models/gl_voxels.cpp index 0ce907aa5..b1bb4de5a 100644 --- a/src/gl/models/gl_voxels.cpp +++ b/src/gl/models/gl_voxels.cpp @@ -417,7 +417,7 @@ int FVoxelModel::FindFrame(const char * name) void FVoxelModel::RenderFrame(FTexture * skin, int frame, int frame2, double inter, int translation) { FMaterial * tex = FMaterial::ValidateTexture(skin, false); - tex->Bind(CLAMP_NOFILTER, translation, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_NOFILTER, translation, -1, false); gl_RenderState.Apply(); GLRenderer->mModelVBO->SetupFrame(vindex, vindex, 0.f); diff --git a/src/gl/renderer/gl_renderer.cpp b/src/gl/renderer/gl_renderer.cpp index ea48d6486..dbf0c10c8 100644 --- a/src/gl/renderer/gl_renderer.cpp +++ b/src/gl/renderer/gl_renderer.cpp @@ -329,7 +329,7 @@ void FGLRenderer::DrawTexture(FTexture *img, DCanvas::DrawParms &parms) } } gl_SetRenderStyle(parms.style, !parms.masked, false); - gltex->Bind(CLAMP_XY_NOMIP, translation, 0, !!(parms.style.Flags & STYLEF_RedIsAlpha)); + gl_RenderState.SetMaterial(gltex, CLAMP_XY_NOMIP, translation, 0, !!(parms.style.Flags & STYLEF_RedIsAlpha)); u1 = gltex->GetUL(); v1 = gltex->GetVT(); @@ -339,7 +339,7 @@ void FGLRenderer::DrawTexture(FTexture *img, DCanvas::DrawParms &parms) } else { - gltex->Bind(CLAMP_XY_NOMIP, 0, -1, false); + gl_RenderState.SetMaterial(gltex, CLAMP_XY_NOMIP, 0, -1, false); u1 = 0.f; v1 = 1.f; u2 = 1.f; @@ -493,7 +493,7 @@ void FGLRenderer::FlatFill (int left, int top, int right, int bottom, FTexture * if (!gltexture) return; - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); // scaling is not used here. if (!local_origin) @@ -586,7 +586,7 @@ void FGLRenderer::FillSimplePoly(FTexture *texture, FVector2 *points, int npoint gl_SetColor(lightlevel, 0, cm, 1.f); - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); int i; float rot = float(rotation * M_PI / float(1u << 31)); diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 0e290adb3..236b5d551 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -5,6 +5,7 @@ #include "gl/system/gl_interface.h" #include "gl/data/gl_data.h" #include "gl/data/gl_matrix.h" +#include "gl/textures/gl_material.h" #include "c_cvars.h" #include "r_defs.h" @@ -97,10 +98,11 @@ public: void Reset(); - void SetShader(int shaderindex, float warptime) + void SetMaterial(FMaterial *mat, int clampmode, int translation, int overrideshader, bool alphatexture) { - mEffectState = shaderindex; - mShaderTimer = warptime; + mEffectState = overrideshader >= 0? overrideshader : mat->mShaderIndex; + mShaderTimer = mat->tex->gl_info.shaderspeed; + mat->Bind(clampmode, translation, alphatexture); } void Apply(); diff --git a/src/gl/scene/gl_decal.cpp b/src/gl/scene/gl_decal.cpp index 4fc7603cf..c938e9043 100644 --- a/src/gl/scene/gl_decal.cpp +++ b/src/gl/scene/gl_decal.cpp @@ -319,7 +319,7 @@ void GLWall::DrawDecal(DBaseDecal *decal) gl_SetRenderStyle(decal->RenderStyle, false, false); - tex->Bind(CLAMP_XY, decal->Translation, 0, !!(decal->RenderStyle.Flags & STYLEF_RedIsAlpha)); + gl_RenderState.SetMaterial(tex, CLAMP_XY, decal->Translation, 0, !!(decal->RenderStyle.Flags & STYLEF_RedIsAlpha)); // If srcalpha is one it looks better with a higher alpha threshold diff --git a/src/gl/scene/gl_drawinfo.cpp b/src/gl/scene/gl_drawinfo.cpp index 1e6bc1bfe..b92a2c6cf 100644 --- a/src/gl/scene/gl_drawinfo.cpp +++ b/src/gl/scene/gl_drawinfo.cpp @@ -1097,7 +1097,7 @@ void FDrawInfo::DrawFloodedPlane(wallseg * ws, float planez, sector_t * sec, boo int rel = getExtraLight(); gl_SetColor(lightlevel, rel, Colormap, 1.0f); gl_SetFog(lightlevel, rel, &Colormap, false); - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); float fviewx = FIXED2FLOAT(viewx); float fviewy = FIXED2FLOAT(viewy); diff --git a/src/gl/scene/gl_flats.cpp b/src/gl/scene/gl_flats.cpp index ec442294a..ac185aee9 100644 --- a/src/gl/scene/gl_flats.cpp +++ b/src/gl/scene/gl_flats.cpp @@ -341,7 +341,7 @@ void GLFlat::Draw(int pass, bool trans) // trans only has meaning for GLPASS_LIG case GLPASS_ALL: gl_SetColor(lightlevel, rel, Colormap,1.0f); gl_SetFog(lightlevel, rel, &Colormap, false); - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); gl_SetPlaneTextureRotation(&plane, gltexture); DrawSubsectors(pass, (pass == GLPASS_ALL || dynlightindex > -1), false); gl_RenderState.EnableTextureMatrix(false); @@ -367,7 +367,7 @@ void GLFlat::Draw(int pass, bool trans) // trans only has meaning for GLPASS_LIG } else { - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); gl_SetPlaneTextureRotation(&plane, gltexture); DrawSubsectors(pass, true, true); gl_RenderState.EnableTextureMatrix(false); diff --git a/src/gl/scene/gl_portal.cpp b/src/gl/scene/gl_portal.cpp index 5e82dbd7b..02e7747bc 100644 --- a/src/gl/scene/gl_portal.cpp +++ b/src/gl/scene/gl_portal.cpp @@ -1011,7 +1011,7 @@ void GLHorizonPortal::DrawContents() } - gltexture->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, CLAMP_NONE, 0, -1, false); gl_SetPlaneTextureRotation(sp, gltexture); gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f); diff --git a/src/gl/scene/gl_scene.cpp b/src/gl/scene/gl_scene.cpp index f1fdfb812..b095d261b 100644 --- a/src/gl/scene/gl_scene.cpp +++ b/src/gl/scene/gl_scene.cpp @@ -1119,7 +1119,7 @@ void FGLInterface::RenderTextureView (FCanvasTexture *tex, AActor *Viewpoint, in if (!usefb) { glFlush(); - gltex->Bind(0, 0, -1, false); + gl_RenderState.SetMaterial(gltex, 0, 0, -1, false); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, bounds.width, bounds.height); } else diff --git a/src/gl/scene/gl_skydome.cpp b/src/gl/scene/gl_skydome.cpp index b12ada3fc..a6ee502d5 100644 --- a/src/gl/scene/gl_skydome.cpp +++ b/src/gl/scene/gl_skydome.cpp @@ -268,7 +268,7 @@ void RenderDome(FMaterial * tex, float x_offset, float y_offset, bool mirror, in if (tex) { - tex->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_NONE, 0, -1, false); texw = tex->TextureWidth(); texh = tex->TextureHeight(); gl_RenderState.EnableModelMatrix(true); @@ -333,7 +333,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // north tex = FMaterial::ValidateTexture(sb->faces[0], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); @@ -349,7 +349,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // east tex = FMaterial::ValidateTexture(sb->faces[1], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); @@ -365,7 +365,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // south tex = FMaterial::ValidateTexture(sb->faces[2], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); @@ -381,7 +381,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // west tex = FMaterial::ValidateTexture(sb->faces[3], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); @@ -425,7 +425,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // top tex = FMaterial::ValidateTexture(sb->faces[faces], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); @@ -441,7 +441,7 @@ static void RenderBox(FTextureID texno, FMaterial * gltex, float x_offset, bool // bottom tex = FMaterial::ValidateTexture(sb->faces[faces+1], false); - tex->Bind(CLAMP_XY, 0, -1, false); + gl_RenderState.SetMaterial(tex, CLAMP_XY, 0, -1, false); gl_RenderState.Apply(); ptr = GLRenderer->mVBO->GetBuffer(); diff --git a/src/gl/scene/gl_sprite.cpp b/src/gl/scene/gl_sprite.cpp index 502264e20..5d8995a34 100644 --- a/src/gl/scene/gl_sprite.cpp +++ b/src/gl/scene/gl_sprite.cpp @@ -214,7 +214,7 @@ void GLSprite::Draw(int pass) gl_RenderState.SetFog(0, 0); } - if (gltexture) gltexture->Bind(CLAMP_XY, translation, OverrideShader, !!(RenderStyle.Flags & STYLEF_RedIsAlpha)); + if (gltexture) gl_RenderState.SetMaterial(gltexture, CLAMP_XY, translation, OverrideShader, !!(RenderStyle.Flags & STYLEF_RedIsAlpha)); else if (!modelframe) gl_RenderState.EnableTexture(false); if (!modelframe) diff --git a/src/gl/scene/gl_walls_draw.cpp b/src/gl/scene/gl_walls_draw.cpp index bf3e9dbd1..bad0aa5d1 100644 --- a/src/gl/scene/gl_walls_draw.cpp +++ b/src/gl/scene/gl_walls_draw.cpp @@ -274,7 +274,7 @@ void GLWall::RenderMirrorSurface() glDepthFunc(GL_LEQUAL); FMaterial * pat=FMaterial::ValidateTexture(GLRenderer->mirrortexture, false); - pat->Bind(CLAMP_NONE, 0, -1, false); + gl_RenderState.SetMaterial(pat, CLAMP_NONE, 0, -1, false); flags &= ~GLWF_GLOW; RenderWall(RWF_BLANK); @@ -326,7 +326,7 @@ void GLWall::RenderTranslucentWall() if (gltexture) { gl_RenderState.EnableGlow(!!(flags & GLWF_GLOW)); - gltexture->Bind(flags & 3, 0, -1, false); + gl_RenderState.SetMaterial(gltexture, flags & 3, 0, -1, false); extra = getExtraLight(); } else @@ -390,7 +390,7 @@ void GLWall::Draw(int pass) else gl_SetFog(255, 0, NULL, false); gl_RenderState.EnableGlow(!!(flags & GLWF_GLOW)); - gltexture->Bind(flags & 3, false, -1, false); + gl_RenderState.SetMaterial(gltexture, flags & 3, false, -1, false); RenderWall(RWF_TEXTURED|RWF_GLOW); gl_RenderState.EnableGlow(false); break; diff --git a/src/gl/scene/gl_weapon.cpp b/src/gl/scene/gl_weapon.cpp index 67e2f9576..d64c0a39a 100644 --- a/src/gl/scene/gl_weapon.cpp +++ b/src/gl/scene/gl_weapon.cpp @@ -96,7 +96,7 @@ void FGLRenderer::DrawPSprite (player_t * player,pspdef_t *psp,fixed_t sx, fixed FMaterial * tex = FMaterial::ValidateTexture(lump, true, false); if (!tex) return; - tex->Bind(CLAMP_XY_NOMIP, 0, OverrideShader, alphatexture); + gl_RenderState.SetMaterial(tex, CLAMP_XY_NOMIP, 0, OverrideShader, alphatexture); int vw = viewwidth; int vh = viewheight; diff --git a/src/gl/textures/gl_material.cpp b/src/gl/textures/gl_material.cpp index 7995b572b..063f948c0 100644 --- a/src/gl/textures/gl_material.cpp +++ b/src/gl/textures/gl_material.cpp @@ -616,11 +616,9 @@ static int lastclamp; static int lasttrans; static bool lastalpha; -void FMaterial::Bind(int clampmode, int translation, int overrideshader, bool alphatexture) -{ - int shaderindex = overrideshader >= 0? overrideshader : mShaderIndex; - gl_RenderState.SetShader(shaderindex, tex->gl_info.shaderspeed); +void FMaterial::Bind(int clampmode, int translation, bool alphatexture) +{ // avoid rebinding the same texture multiple times. if (this == last && lastclamp == clampmode && translation == lasttrans && lastalpha == alphatexture) return; last = this; @@ -636,7 +634,7 @@ void FMaterial::Bind(int clampmode, int translation, int overrideshader, bool al else if (tex->bWarped && clampmode <= CLAMP_XY) clampmode = CLAMP_NONE; const FHardwareTexture *gltexture = mBaseLayer->Bind(0, clampmode, translation, alphatexture, allowhires? tex:NULL); - if (gltexture != NULL && shaderindex > 0 && overrideshader == -1) + if (gltexture != NULL) { for(unsigned i=0;i