OpenGLRenderer/Context: Improve source readability

This commit is contained in:
Jérôme Leclercq 2021-07-03 12:59:06 +02:00
parent d88c4ecb5b
commit 9f76c84309
1 changed files with 48 additions and 34 deletions

View File

@ -64,7 +64,7 @@ namespace Nz::GL
} }
template<typename FuncType, std::size_t FuncIndex, typename Func> template<typename FuncType, std::size_t FuncIndex, typename Func>
bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = true) bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = false)
{ {
const Loader& loader = context.GetLoader(); const Loader& loader = context.GetLoader();
GLFunction originalFuncPtr = loader.LoadFunction(funcName); GLFunction originalFuncPtr = loader.LoadFunction(funcName);
@ -251,7 +251,7 @@ namespace Nz::GL
try try
{ {
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, true); #define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, true, true);
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Do nothing #define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Do nothing
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC) NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_EXT_FUNC #undef NAZARA_OPENGLRENDERER_EXT_FUNC
@ -403,9 +403,9 @@ namespace Nz::GL
void Context::SetScissorBox(GLint x, GLint y, GLsizei width, GLsizei height) const void Context::SetScissorBox(GLint x, GLint y, GLsizei width, GLsizei height) const
{ {
if (m_state.scissorBox.x != x || if (m_state.scissorBox.x != x ||
m_state.scissorBox.y != y || m_state.scissorBox.y != y ||
m_state.scissorBox.width != width || m_state.scissorBox.width != width ||
m_state.scissorBox.height != height) m_state.scissorBox.height != height)
{ {
if (!SetCurrentContext(this)) if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context"); throw std::runtime_error("failed to activate context");
@ -422,9 +422,9 @@ namespace Nz::GL
void Context::SetViewport(GLint x, GLint y, GLsizei width, GLsizei height) const void Context::SetViewport(GLint x, GLint y, GLsizei width, GLsizei height) const
{ {
if (m_state.viewport.x != x || if (m_state.viewport.x != x ||
m_state.viewport.y != y || m_state.viewport.y != y ||
m_state.viewport.width != width || m_state.viewport.width != width ||
m_state.viewport.height != height) m_state.viewport.height != height)
{ {
if (!SetCurrentContext(this)) if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context"); throw std::runtime_error("failed to activate context");
@ -443,29 +443,7 @@ namespace Nz::GL
if (!SetCurrentContext(this)) if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context"); throw std::runtime_error("failed to activate context");
if (renderStates.blending) // Depth compare and depth write
{
auto& currentBlend = m_state.renderStates.blend;
const auto& targetBlend = renderStates.blend;
if (currentBlend.modeColor != targetBlend.modeColor || currentBlend.modeAlpha != targetBlend.modeAlpha)
{
glBlendEquationSeparate(ToOpenGL(targetBlend.modeColor), ToOpenGL(targetBlend.modeAlpha));
currentBlend.modeAlpha = targetBlend.modeAlpha;
currentBlend.modeColor = targetBlend.modeColor;
}
if (currentBlend.dstAlpha != targetBlend.dstAlpha || currentBlend.dstColor != targetBlend.dstColor ||
currentBlend.srcAlpha != targetBlend.srcAlpha || currentBlend.srcColor != targetBlend.srcColor)
{
glBlendFuncSeparate(ToOpenGL(targetBlend.srcColor), ToOpenGL(targetBlend.dstColor), ToOpenGL(targetBlend.srcAlpha), ToOpenGL(targetBlend.dstAlpha));
currentBlend.dstAlpha = targetBlend.dstAlpha;
currentBlend.dstColor = targetBlend.dstColor;
currentBlend.srcAlpha = targetBlend.srcAlpha;
currentBlend.srcColor = targetBlend.srcColor;
}
}
if (renderStates.depthBuffer) if (renderStates.depthBuffer)
{ {
if (m_state.renderStates.depthCompare != renderStates.depthCompare) if (m_state.renderStates.depthCompare != renderStates.depthCompare)
@ -481,6 +459,7 @@ namespace Nz::GL
} }
} }
// Face culling side
if (renderStates.faceCulling) if (renderStates.faceCulling)
{ {
if (m_state.renderStates.cullingSide != renderStates.cullingSide) if (m_state.renderStates.cullingSide != renderStates.cullingSide)
@ -490,6 +469,7 @@ namespace Nz::GL
} }
} }
// Front face
FrontFace targetFrontFace = renderStates.frontFace; FrontFace targetFrontFace = renderStates.frontFace;
if (!isViewportFlipped) if (!isViewportFlipped)
targetFrontFace = (targetFrontFace == FrontFace::Clockwise) ? FrontFace::CounterClockwise : FrontFace::Clockwise; targetFrontFace = (targetFrontFace == FrontFace::Clockwise) ? FrontFace::CounterClockwise : FrontFace::Clockwise;
@ -500,12 +480,14 @@ namespace Nz::GL
m_state.renderStates.frontFace = targetFrontFace; m_state.renderStates.frontFace = targetFrontFace;
} }
// Face filling
if (glPolygonMode && m_state.renderStates.faceFilling != renderStates.faceFilling) if (glPolygonMode && m_state.renderStates.faceFilling != renderStates.faceFilling)
{ {
glPolygonMode(GL_FRONT_AND_BACK, ToOpenGL(renderStates.faceFilling)); glPolygonMode(GL_FRONT_AND_BACK, ToOpenGL(renderStates.faceFilling));
m_state.renderStates.faceFilling = renderStates.faceFilling; m_state.renderStates.faceFilling = renderStates.faceFilling;
} }
// Front and back stencil states
if (renderStates.stencilTest) if (renderStates.stencilTest)
{ {
auto ApplyStencilStates = [&](bool front) auto ApplyStencilStates = [&](bool front)
@ -544,18 +526,21 @@ namespace Nz::GL
ApplyStencilStates(false); ApplyStencilStates(false);
} }
// Line width
if (!NumberEquals(m_state.renderStates.lineWidth, renderStates.lineWidth, 0.001f)) if (!NumberEquals(m_state.renderStates.lineWidth, renderStates.lineWidth, 0.001f))
{ {
glLineWidth(renderStates.lineWidth); glLineWidth(renderStates.lineWidth);
m_state.renderStates.lineWidth = renderStates.lineWidth; m_state.renderStates.lineWidth = renderStates.lineWidth;
} }
// Point size (TODO)
/*if (!NumberEquals(m_state.renderStates.pointSize, renderStates.pointSize, 0.001f)) /*if (!NumberEquals(m_state.renderStates.pointSize, renderStates.pointSize, 0.001f))
{ {
glPointSize(renderStates.pointSize); glPointSize(renderStates.pointSize);
m_state.renderStates.pointSize = renderStates.pointSize; m_state.renderStates.pointSize = renderStates.pointSize;
}*/ }*/
// Blend states
if (m_state.renderStates.blending != renderStates.blending) if (m_state.renderStates.blending != renderStates.blending)
{ {
if (renderStates.blending) if (renderStates.blending)
@ -566,6 +551,30 @@ namespace Nz::GL
m_state.renderStates.blending = renderStates.blending; m_state.renderStates.blending = renderStates.blending;
} }
if (renderStates.blending)
{
auto& currentBlend = m_state.renderStates.blend;
const auto& targetBlend = renderStates.blend;
if (currentBlend.modeColor != targetBlend.modeColor || currentBlend.modeAlpha != targetBlend.modeAlpha)
{
glBlendEquationSeparate(ToOpenGL(targetBlend.modeColor), ToOpenGL(targetBlend.modeAlpha));
currentBlend.modeAlpha = targetBlend.modeAlpha;
currentBlend.modeColor = targetBlend.modeColor;
}
if (currentBlend.dstAlpha != targetBlend.dstAlpha || currentBlend.dstColor != targetBlend.dstColor ||
currentBlend.srcAlpha != targetBlend.srcAlpha || currentBlend.srcColor != targetBlend.srcColor)
{
glBlendFuncSeparate(ToOpenGL(targetBlend.srcColor), ToOpenGL(targetBlend.dstColor), ToOpenGL(targetBlend.srcAlpha), ToOpenGL(targetBlend.dstAlpha));
currentBlend.dstAlpha = targetBlend.dstAlpha;
currentBlend.dstColor = targetBlend.dstColor;
currentBlend.srcAlpha = targetBlend.srcAlpha;
currentBlend.srcColor = targetBlend.srcColor;
}
}
// Color write
if (m_state.renderStates.colorWrite != renderStates.colorWrite) if (m_state.renderStates.colorWrite != renderStates.colorWrite)
{ {
GLboolean param = (renderStates.colorWrite) ? GL_TRUE : GL_FALSE; GLboolean param = (renderStates.colorWrite) ? GL_TRUE : GL_FALSE;
@ -574,6 +583,7 @@ namespace Nz::GL
m_state.renderStates.colorWrite = renderStates.colorWrite; m_state.renderStates.colorWrite = renderStates.colorWrite;
} }
// Depth buffer
if (m_state.renderStates.depthBuffer != renderStates.depthBuffer) if (m_state.renderStates.depthBuffer != renderStates.depthBuffer)
{ {
if (renderStates.depthBuffer) if (renderStates.depthBuffer)
@ -584,6 +594,7 @@ namespace Nz::GL
m_state.renderStates.depthBuffer = renderStates.depthBuffer; m_state.renderStates.depthBuffer = renderStates.depthBuffer;
} }
// Face culling
if (m_state.renderStates.faceCulling != renderStates.faceCulling) if (m_state.renderStates.faceCulling != renderStates.faceCulling)
{ {
if (renderStates.faceCulling) if (renderStates.faceCulling)
@ -594,6 +605,7 @@ namespace Nz::GL
m_state.renderStates.faceCulling = renderStates.faceCulling; m_state.renderStates.faceCulling = renderStates.faceCulling;
} }
// Scissor test
if (m_state.renderStates.scissorTest != renderStates.scissorTest) if (m_state.renderStates.scissorTest != renderStates.scissorTest)
{ {
if (renderStates.scissorTest) if (renderStates.scissorTest)
@ -604,6 +616,7 @@ namespace Nz::GL
m_state.renderStates.scissorTest = renderStates.scissorTest; m_state.renderStates.scissorTest = renderStates.scissorTest;
} }
// Stencil test
if (m_state.renderStates.stencilTest != renderStates.stencilTest) if (m_state.renderStates.stencilTest != renderStates.stencilTest)
{ {
if (renderStates.stencilTest) if (renderStates.stencilTest)
@ -665,7 +678,7 @@ namespace Nz::GL
{ {
constexpr std::size_t functionIndex = UnderlyingCast(FunctionIndex::glPolygonMode); constexpr std::size_t functionIndex = UnderlyingCast(FunctionIndex::glPolygonMode);
return loader.Load<PFNGLPOLYGONMODENVPROC, functionIndex>(glPolygonMode, "glPolygonModeNV", false, false); return loader.Load<PFNGLPOLYGONMODENVPROC, functionIndex>(glPolygonMode, "glPolygonModeNV", false); //< from GL_NV_polygon_mode
} }
return false; return false;
@ -680,6 +693,7 @@ namespace Nz::GL
void Context::HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const void Context::HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const
{ {
// Ignore debug groups
if (id == 0) if (id == 0)
return; return;
@ -714,7 +728,7 @@ namespace Nz::GL
break; break;
default: default:
// Peut être rajouté par une extension // Extension source
ss << "Unknown"; ss << "Unknown";
break; break;
} }
@ -748,7 +762,7 @@ namespace Nz::GL
break; break;
default: default:
// Peut être rajouté par une extension // Extension type
ss << "Unknown"; ss << "Unknown";
break; break;
} }