// //--------------------------------------------------------------------------- // // Copyright(C) 2009-2016 Christoph Oelckers // All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see http://www.gnu.org/licenses/ // //-------------------------------------------------------------------------- // /* ** gl_renderstate.cpp ** Render state maintenance ** */ #include "templates.h" #include "doomstat.h" #include "r_data/colormaps.h" #include "gl_load/gl_system.h" #include "gl_load/gl_interface.h" #include "hwrenderer/utility/hw_cvars.h" #include "hwrenderer/data/flatvertices.h" #include "hwrenderer/scene/hw_skydome.h" #include "gl/shaders/gl_shader.h" #include "gl/renderer/gl_renderer.h" #include "gl/dynlights//gl_lightbuffer.h" #include "gl/renderer/gl_renderbuffers.h" #include "gl/textures/gl_hwtexture.h" #include "gl/system/gl_buffers.h" FGLRenderState gl_RenderState; static VSMatrix identityMatrix(1); TArray gl_MatrixStack; static void matrixToGL(const VSMatrix &mat, int loc) { glUniformMatrix4fv(loc, 1, false, (float*)&mat); } //========================================================================== // // This only gets called once upon setup. // With OpenGL the state is persistent and cannot be cleared, once set up. // //========================================================================== void FGLRenderState::Reset() { FRenderState::Reset(); mVertexBuffer = mCurrentVertexBuffer = nullptr; mGlossiness = 0.0f; mSpecularLevel = 0.0f; mShaderTimer = 0.0f; stRenderStyle = DefaultRenderStyle(); stSrcBlend = stDstBlend = -1; stBlendEquation = -1; stAlphaTest = 0; mLastDepthClamp = true; mEffectState = 0; activeShader = nullptr; mPassType = NORMAL_PASS; mCurrentVertexBuffer = nullptr; mCurrentVertexOffsets[0] = mVertexOffsets[0] = 0; mCurrentIndexBuffer = nullptr; } //========================================================================== // // Apply shader settings // //========================================================================== bool FGLRenderState::ApplyShader() { static uint64_t firstFrame = 0; // if firstFrame is not yet initialized, initialize it to current time // if we're going to overflow a float (after ~4.6 hours, or 24 bits), re-init to regain precision if ((firstFrame == 0) || (screen->FrameTime - firstFrame >= 1<<24) || level.ShaderStartTime >= firstFrame) firstFrame = screen->FrameTime; static const float nulvec[] = { 0.f, 0.f, 0.f, 0.f }; if (mSpecialEffect > EFF_NONE) { activeShader = GLRenderer->mShaderManager->BindEffect(mSpecialEffect, mPassType); } else { activeShader = GLRenderer->mShaderManager->Get(mTextureEnabled ? mEffectState : SHADER_NoTexture, mAlphaThreshold >= 0.f, mPassType); activeShader->Bind(); } int fogset = 0; if (mFogEnabled) { if (mFogEnabled == 2) { fogset = -3; // 2D rendering with 'foggy' overlay. } else if ((mFogColor & 0xffffff) == 0) { fogset = gl_fogmode; } else { fogset = -gl_fogmode; } } glVertexAttrib4fv(VATTR_COLOR, mColor.vec); glVertexAttrib4fv(VATTR_NORMAL, mNormal.vec); activeShader->muDesaturation.Set(mDesaturation / 255.f); activeShader->muFogEnabled.Set(fogset); activeShader->muTextureMode.Set(mTextureMode == TM_NORMAL && mTempTM == TM_OPAQUE ? TM_OPAQUE : mTextureMode); activeShader->muLightParms.Set(mLightParms); activeShader->muFogColor.Set(mFogColor); activeShader->muObjectColor.Set(mObjectColor); activeShader->muObjectColor2.Set(mObjectColor2); activeShader->muDynLightColor.Set(mDynColor.vec); activeShader->muInterpolationFactor.Set(mInterpolationFactor); activeShader->muTimer.Set((double)(screen->FrameTime - firstFrame) * (double)mShaderTimer / 1000.); activeShader->muAlphaThreshold.Set(mAlphaThreshold); activeShader->muLightIndex.Set(-1); activeShader->muClipSplit.Set(mClipSplit); activeShader->muSpecularMaterial.Set(mGlossiness, mSpecularLevel); if (mGlowEnabled) { activeShader->muGlowTopColor.Set(mGlowTop.vec); activeShader->muGlowBottomColor.Set(mGlowBottom.vec); activeShader->currentglowstate = 1; } else if (activeShader->currentglowstate) { // if glowing is on, disable it. activeShader->muGlowTopColor.Set(nulvec); activeShader->muGlowBottomColor.Set(nulvec); activeShader->currentglowstate = 0; } if (mGlowEnabled || mObjectColor2.a != 0) { activeShader->muGlowTopPlane.Set(mGlowTopPlane.vec); activeShader->muGlowBottomPlane.Set(mGlowBottomPlane.vec); } if (mSplitEnabled) { activeShader->muSplitTopPlane.Set(mSplitTopPlane.vec); activeShader->muSplitBottomPlane.Set(mSplitBottomPlane.vec); activeShader->currentsplitstate = 1; } else if (activeShader->currentsplitstate) { activeShader->muSplitTopPlane.Set(nulvec); activeShader->muSplitBottomPlane.Set(nulvec); activeShader->currentsplitstate = 0; } if (mTextureMatrixEnabled) { matrixToGL(mTextureMatrix, activeShader->texturematrix_index); activeShader->currentTextureMatrixState = true; } else if (activeShader->currentTextureMatrixState) { activeShader->currentTextureMatrixState = false; matrixToGL(identityMatrix, activeShader->texturematrix_index); } if (mModelMatrixEnabled) { matrixToGL(mModelMatrix, activeShader->modelmatrix_index); VSMatrix norm; norm.computeNormalMatrix(mModelMatrix); matrixToGL(norm, activeShader->normalmodelmatrix_index); activeShader->currentModelMatrixState = true; } else if (activeShader->currentModelMatrixState) { activeShader->currentModelMatrixState = false; matrixToGL(identityMatrix, activeShader->modelmatrix_index); matrixToGL(identityMatrix, activeShader->normalmodelmatrix_index); } auto index = GLRenderer->mLights->BindUBO(mLightIndex); activeShader->muLightIndex.Set(index); return true; } //========================================================================== // // Apply State // //========================================================================== void FGLRenderState::ApplyState() { if (mRenderStyle != stRenderStyle) { ApplyBlendMode(); stRenderStyle = mRenderStyle; } if (mSplitEnabled != stSplitEnabled) { if (mSplitEnabled) { glEnable(GL_CLIP_DISTANCE3); glEnable(GL_CLIP_DISTANCE4); } else { glDisable(GL_CLIP_DISTANCE3); glDisable(GL_CLIP_DISTANCE4); } stSplitEnabled = mSplitEnabled; } if (mMaterial.mChanged) { ApplyMaterial(mMaterial.mMaterial, mMaterial.mClampMode, mMaterial.mTranslation, mMaterial.mOverrideShader); mMaterial.mChanged = false; } if (mBias.mChanged) { if (mBias.mFactor == 0 && mBias.mUnits == 0) { glDisable(GL_POLYGON_OFFSET_FILL); } else { glEnable(GL_POLYGON_OFFSET_FILL); } glPolygonOffset(mBias.mFactor, mBias.mUnits); mBias.mChanged = false; } } void FGLRenderState::ApplyBuffers() { if (mVertexBuffer != mCurrentVertexBuffer || mVertexOffsets[0] != mCurrentVertexOffsets[0] || mVertexOffsets[1] != mCurrentVertexOffsets[1]) { assert(mVertexBuffer != nullptr); static_cast(mVertexBuffer)->Bind(mVertexOffsets); mCurrentVertexBuffer = mVertexBuffer; mCurrentVertexOffsets[0] = mVertexOffsets[0]; mCurrentVertexOffsets[1] = mVertexOffsets[1]; } if (mIndexBuffer != mCurrentIndexBuffer) { if (mIndexBuffer) static_cast(mIndexBuffer)->Bind(); mCurrentIndexBuffer = mIndexBuffer; } } void FGLRenderState::Apply() { ApplyState(); ApplyBuffers(); ApplyShader(); } //=========================================================================== // // Binds a texture to the renderer // //=========================================================================== void FGLRenderState::ApplyMaterial(FMaterial *mat, int clampmode, int translation, int overrideshader) { if (mat->tex->bHasCanvas) { mTempTM = TM_OPAQUE; } else { mTempTM = TM_NORMAL; } mEffectState = overrideshader >= 0 ? overrideshader : mat->GetShaderIndex(); mShaderTimer = mat->tex->shaderspeed; SetSpecular(mat->tex->Glossiness, mat->tex->SpecularLevel); auto tex = mat->tex; if (tex->UseType == ETextureType::SWCanvas) clampmode = CLAMP_NOFILTER; if (tex->bHasCanvas) clampmode = CLAMP_CAMTEX; else if ((tex->bWarped || tex->shaderindex >= FIRST_USER_SHADER) && clampmode <= CLAMP_XY) clampmode = CLAMP_NONE; // avoid rebinding the same texture multiple times. if (mat == lastMaterial && lastClamp == clampmode && translation == lastTranslation) return; lastMaterial = mat; lastClamp = clampmode; lastTranslation = translation; int usebright = false; int maxbound = 0; // Textures that are already scaled in the texture lump will not get replaced by hires textures. int flags = mat->isExpanded() ? CTF_Expand : (gl_texture_usehires && tex->Scale.X == 1 && tex->Scale.Y == 1 && clampmode <= CLAMP_XY) ? CTF_CheckHires : 0; int numLayers = mat->GetLayers(); auto base = static_cast(mat->GetLayer(0)); if (base->BindOrCreate(tex, 0, clampmode, translation, flags)) { for (int i = 1; i(mat->GetLayer(i, &layer)); systex->BindOrCreate(layer, i, clampmode, 0, mat->isExpanded() ? CTF_Expand : 0); maxbound = i; } } // unbind everything from the last texture that's still active for (int i = maxbound + 1; i <= maxBoundMaterial; i++) { FHardwareTexture::Unbind(i); maxBoundMaterial = maxbound; } } //========================================================================== // // Apply blend mode from RenderStyle // //========================================================================== void FGLRenderState::ApplyBlendMode() { static int blendstyles[] = { GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, }; static int renderops[] = { 0, GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int srcblend = blendstyles[mRenderStyle.SrcAlpha%STYLEALPHA_MAX]; int dstblend = blendstyles[mRenderStyle.DestAlpha%STYLEALPHA_MAX]; int blendequation = renderops[mRenderStyle.BlendOp & 15]; if (blendequation == -1) // This was a fuzz style. { srcblend = GL_DST_COLOR; dstblend = GL_ONE_MINUS_SRC_ALPHA; blendequation = GL_FUNC_ADD; } // Checks must be disabled until all draw code has been converted. //if (srcblend != stSrcBlend || dstblend != stDstBlend) { stSrcBlend = srcblend; stDstBlend = dstblend; glBlendFunc(srcblend, dstblend); } //if (blendequation != stBlendEquation) { stBlendEquation = blendequation; glBlendEquation(blendequation); } }