From ab3468ea027384b6713990f7f6dd46a3ccc10134 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sun, 6 Sep 2015 13:45:17 +0200 Subject: [PATCH] Move native/gfx_es2/gl_state.cpp/h into GPU/GLES/GLStateCache.cpp/h --- CMakeLists.txt | 4 +- GPU/Common/DepalettizeShaderCommon.cpp | 2 +- GPU/GLES/DepalettizeShader.cpp | 1 + GPU/GLES/DepalettizeShader.h | 2 +- GPU/GLES/FBO.cpp | 2 +- GPU/GLES/FragmentTestCache.h | 5 +- GPU/GLES/Framebuffer.cpp | 2 +- GPU/GLES/GLES_GPU.cpp | 2 +- GPU/GLES/GLStateCache.cpp | 52 ++++++ GPU/GLES/GLStateCache.h | 240 +++++++++++++++++++++++++ GPU/GLES/ShaderManager.cpp | 2 +- GPU/GLES/StateMapping.cpp | 2 +- GPU/GLES/StencilBuffer.cpp | 2 +- GPU/GLES/TextureCache.cpp | 7 +- GPU/GLES/TransformPipeline.cpp | 2 +- GPU/GPU.vcxproj | 2 + GPU/GPU.vcxproj.filters | 6 + GPU/Software/SoftGpu.cpp | 1 - Qt/Debugger/debugger_memorytex.cpp | 2 +- native | 2 +- 20 files changed, 320 insertions(+), 20 deletions(-) create mode 100644 GPU/GLES/GLStateCache.cpp create mode 100644 GPU/GLES/GLStateCache.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 08c83a569e..19b16ba0df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -923,8 +923,6 @@ add_library(native STATIC native/gfx_es2/draw_buffer.h native/gfx_es2/draw_text.cpp native/gfx_es2/draw_text.h - native/gfx_es2/gl_state.cpp - native/gfx_es2/gl_state.h native/gfx_es2/gpu_features.cpp native/gfx_es2/gpu_features.h native/gfx_es2/glsl_program.cpp @@ -1487,6 +1485,8 @@ add_library(GPU OBJECT GPU/GLES/FBO.h GPU/GLES/GLES_GPU.cpp GPU/GLES/GLES_GPU.h + GPU/GLES/GLStateCache.cpp + GPU/GLES/GLStateCache.h GPU/GLES/FragmentShaderGenerator.cpp GPU/GLES/FragmentShaderGenerator.h GPU/GLES/FragmentTestCache.cpp diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index a6e52bdb4a..721fdf18a3 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -17,10 +17,10 @@ #include -#include "gfx_es2/gl_state.h" #include "Common/Log.h" #include "Core/Reporting.h" #include "GPU/GPUState.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/Common/DepalettizeShaderCommon.h" diff --git a/GPU/GLES/DepalettizeShader.cpp b/GPU/GLES/DepalettizeShader.cpp index 1a2859aa2c..d82191c83e 100644 --- a/GPU/GLES/DepalettizeShader.cpp +++ b/GPU/GLES/DepalettizeShader.cpp @@ -22,6 +22,7 @@ #include "Core/Reporting.h" #include "DepalettizeShader.h" #include "GPU/GLES/TextureCache.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/Common/DepalettizeShaderCommon.h" static const int DEPAL_TEXTURE_OLD_AGE = 120; diff --git a/GPU/GLES/DepalettizeShader.h b/GPU/GLES/DepalettizeShader.h index 82479b8cff..1916d43df4 100644 --- a/GPU/GLES/DepalettizeShader.h +++ b/GPU/GLES/DepalettizeShader.h @@ -18,7 +18,7 @@ #include #include "Common/CommonTypes.h" -#include "gfx_es2/gl_state.h" +#include "gfx/gl_common.h" #include "GPU/ge_constants.h" class DepalShader { diff --git a/GPU/GLES/FBO.cpp b/GPU/GLES/FBO.cpp index 5465723133..889ed00767 100644 --- a/GPU/GLES/FBO.cpp +++ b/GPU/GLES/FBO.cpp @@ -19,8 +19,8 @@ #include "base/logging.h" #include "gfx/gl_common.h" -#include "gfx_es2/gl_state.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/FBO.h" #ifdef IOS diff --git a/GPU/GLES/FragmentTestCache.h b/GPU/GLES/FragmentTestCache.h index 1600161b24..31df1233d9 100644 --- a/GPU/GLES/FragmentTestCache.h +++ b/GPU/GLES/FragmentTestCache.h @@ -19,10 +19,11 @@ #include #include "Common/CommonTypes.h" -#include "gfx_es2/gl_state.h" -#include "GPU/ge_constants.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/TextureCache.h" +#include "GPU/ge_constants.h" + struct FragmentTestID { union { struct { diff --git a/GPU/GLES/Framebuffer.cpp b/GPU/GLES/Framebuffer.cpp index 4f2b4fe840..ac1eaf7592 100644 --- a/GPU/GLES/Framebuffer.cpp +++ b/GPU/GLES/Framebuffer.cpp @@ -21,7 +21,6 @@ #include "profiler/profiler.h" #include "gfx_es2/glsl_program.h" -#include "gfx_es2/gl_state.h" #include "base/timeutil.h" #include "math/lin/matrix4x4.h" @@ -40,6 +39,7 @@ #include "GPU/Common/TextureDecoder.h" #include "GPU/Common/FramebufferCommon.h" #include "GPU/Debugger/Stepping.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/FBO.h" #include "GPU/GLES/Framebuffer.h" #include "GPU/GLES/TextureCache.h" diff --git a/GPU/GLES/GLES_GPU.cpp b/GPU/GLES/GLES_GPU.cpp index c29eb499f8..ce7337026d 100644 --- a/GPU/GLES/GLES_GPU.cpp +++ b/GPU/GLES/GLES_GPU.cpp @@ -16,7 +16,6 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "base/logging.h" -#include "gfx_es2/gl_state.h" #include "profiler/profiler.h" #include "Common/ChunkFile.h" @@ -34,6 +33,7 @@ #include "GPU/GeDisasm.h" #include "GPU/Common/FramebufferCommon.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/ShaderManager.h" #include "GPU/GLES/GLES_GPU.h" #include "GPU/GLES/Framebuffer.h" diff --git a/GPU/GLES/GLStateCache.cpp b/GPU/GLES/GLStateCache.cpp new file mode 100644 index 0000000000..d4b1cd4854 --- /dev/null +++ b/GPU/GLES/GLStateCache.cpp @@ -0,0 +1,52 @@ +#include + +#include "base/logging.h" + +#include "GPU/GLES/GLStateCache.h" + +OpenGLState glstate; + +int OpenGLState::state_count = 0; + +void OpenGLState::Restore() { + int count = 0; + + blend.restore(); count++; + blendEquationSeparate.restore(); count++; + blendFuncSeparate.restore(); count++; + blendColor.restore(); count++; + + scissorTest.restore(); count++; + scissorRect.restore(); count++; + + cullFace.restore(); count++; + cullFaceMode.restore(); count++; + frontFace.restore(); count++; + + depthTest.restore(); count++; + depthRange.restore(); count++; + depthFunc.restore(); count++; + depthWrite.restore(); count++; + + colorMask.restore(); count++; + viewport.restore(); count++; + + stencilTest.restore(); count++; + stencilOp.restore(); count++; + stencilFunc.restore(); count++; + stencilMask.restore(); count++; + + dither.restore(); count++; + +#if !defined(USING_GLES2) + colorLogicOp.restore(); count++; + logicOp.restore(); count++; +#endif + + arrayBuffer.restore(); count++; + elementArrayBuffer.restore(); count++; + + if (count != state_count) { + FLOG("OpenGLState::Restore is missing some states"); + } +} diff --git a/GPU/GLES/GLStateCache.h b/GPU/GLES/GLStateCache.h new file mode 100644 index 0000000000..c1d6f2669d --- /dev/null +++ b/GPU/GLES/GLStateCache.h @@ -0,0 +1,240 @@ +#pragma once + +#include +#include +#include + +#include "gfx/gl_common.h" +#include "gfx_es2/gpu_features.h" + +// OpenGL state cache. +// Probably only really worth it in rendering cores on weak mobile hardware. +class OpenGLState { +private: + template + class BoolState { + private: + bool _value; + public: + BoolState() : _value(init) { + OpenGLState::state_count++; + } + + inline void set(bool value) { + if (value && value != _value) { + _value = value; + glEnable(cap); + } + if (!value && value != _value) { + _value = value; + glDisable(cap); + } + } + inline void enable() { + set(true); + } + inline void disable() { + set(false); + } + operator bool() const { + return isset(); + } + inline bool isset() { + return _value; + } + void restore() { + if (_value) + glEnable(cap); + else + glDisable(cap); + } + }; + +#define STATE1(func, p1type, p1def) \ + class SavedState1_##func { \ + p1type p1; \ + public: \ + SavedState1_##func() : p1(p1def) { \ + OpenGLState::state_count++; \ + } \ + void set(p1type newp1) { \ + if (newp1 != p1) { \ + p1 = newp1; \ + func(p1); \ + } \ + } \ + void restore() { \ + func(p1); \ + } \ + } + +#define STATE2(func, p1type, p2type, p1def, p2def) \ + class SavedState2_##func { \ + p1type p1; \ + p2type p2; \ + public: \ + SavedState2_##func() : p1(p1def), p2(p2def) { \ + OpenGLState::state_count++; \ + } \ + inline void set(p1type newp1, p2type newp2) { \ + if (newp1 != p1 || newp2 != p2) { \ + p1 = newp1; \ + p2 = newp2; \ + func(p1, p2); \ + } \ + } \ + inline void restore() { \ + func(p1, p2); \ + } \ + } + +#define STATE3(func, p1type, p2type, p3type, p1def, p2def, p3def) \ + class SavedState3_##func { \ + p1type p1; \ + p2type p2; \ + p3type p3; \ + public: \ + SavedState3_##func() : p1(p1def), p2(p2def), p3(p3def) { \ + OpenGLState::state_count++; \ + } \ + inline void set(p1type newp1, p2type newp2, p3type newp3) { \ + if (newp1 != p1 || newp2 != p2 || newp3 != p3) { \ + p1 = newp1; \ + p2 = newp2; \ + p3 = newp3; \ + func(p1, p2, p3); \ + } \ + } \ + inline void restore() { \ + func(p1, p2, p3); \ + } \ + } + + #define STATE4(func, p1type, p2type, p3type, p4type, p1def, p2def, p3def, p4def) \ + class SavedState4_##func { \ + p1type p1; \ + p2type p2; \ + p3type p3; \ + p4type p4; \ + public: \ + SavedState4_##func() : p1(p1def), p2(p2def), p3(p3def), p4(p4def) { \ + OpenGLState::state_count++; \ + } \ + inline void set(p1type newp1, p2type newp2, p3type newp3, p4type newp4) { \ + if (newp1 != p1 || newp2 != p2 || newp3 != p3 || newp4 != p4) { \ + p1 = newp1; \ + p2 = newp2; \ + p3 = newp3; \ + p4 = newp4; \ + func(p1, p2, p3, p4); \ + } \ + } \ + inline void restore() { \ + func(p1, p2, p3, p4); \ + } \ + } + +#define STATEFLOAT4(func, def) \ + class SavedState4_##func { \ + float p[4]; \ + public: \ + SavedState4_##func() { \ + for (int i = 0; i < 4; i++) {p[i] = def;} \ + OpenGLState::state_count++; \ + } \ + inline void set(const float v[4]) { \ + if (memcmp(p,v,sizeof(float)*4)) { \ + memcpy(p,v,sizeof(float)*4); \ + func(p[0], p[1], p[2], p[3]); \ + } \ + } \ + inline void restore() { \ + func(p[0], p[1], p[2], p[3]); \ + } \ + } + +#define STATEBIND(func, target) \ + class SavedBind_##func_##target { \ + GLuint val_; \ + public: \ + SavedBind_##func_##target() { \ + val_ = 0; \ + OpenGLState::state_count++; \ + } \ + inline void bind(GLuint val) { \ + if (val_ != val) { \ + func(target, val); \ + val_ = val; \ + } \ + } \ + inline void unbind() { \ + bind(0); \ + } \ + inline void restore() { \ + func(target, val_); \ + } \ + } + +public: + static int state_count; + OpenGLState() {} + void Restore(); + + // When adding a state here, don't forget to add it to OpenGLState::Restore() too + + // Blending + BoolState blend; + STATE4(glBlendFuncSeparate, GLenum, GLenum, GLenum, GLenum, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blendFuncSeparate; + + // On OpenGL ES, using minmax blend requires glBlendEquationEXT (in theory at least but I don't think it's true in practice) + STATE2(glBlendEquationSeparate, GLenum, GLenum, GL_FUNC_ADD, GL_FUNC_ADD) blendEquationSeparate; + STATEFLOAT4(glBlendColor, 1.0f) blendColor; + + // Logic Ops. Not available on OpenGL ES at all. +#if !defined(USING_GLES2) + BoolState colorLogicOp; + STATE1(glLogicOp, GLenum, GL_COPY) logicOp; +#endif + + // Dither + BoolState dither; + + // Cull Face + BoolState cullFace; + STATE1(glCullFace, GLenum, GL_FRONT) cullFaceMode; + STATE1(glFrontFace, GLenum, GL_CCW) frontFace; + + // Depth Test + BoolState depthTest; +#if defined(USING_GLES2) + STATE2(glDepthRangef, float, float, 0.f, 1.f) depthRange; +#else + STATE2(glDepthRange, double, double, 0.0, 1.0) depthRange; +#endif + STATE1(glDepthFunc, GLenum, GL_LESS) depthFunc; + STATE1(glDepthMask, GLboolean, GL_TRUE) depthWrite; + + // Color Mask + STATE4(glColorMask, bool, bool, bool, bool, true, true, true, true) colorMask; + + // Viewport + STATE4(glViewport, GLint, GLint, GLsizei, GLsizei, 0, 0, 128, 128) viewport; + + // Scissor Test + BoolState scissorTest; + STATE4(glScissor, GLint, GLint, GLsizei, GLsizei, 0, 0, 128, 128) scissorRect; + + // Stencil Test + BoolState stencilTest; + STATE3(glStencilOp, GLenum, GLenum, GLenum, GL_KEEP, GL_KEEP, GL_KEEP) stencilOp; + STATE3(glStencilFunc, GLenum, GLint, GLuint, GL_ALWAYS, 0, 0xFF) stencilFunc; + STATE1(glStencilMask, GLuint, 0xFF) stencilMask; + + STATEBIND(glBindBuffer, GL_ARRAY_BUFFER) arrayBuffer; + STATEBIND(glBindBuffer, GL_ELEMENT_ARRAY_BUFFER) elementArrayBuffer; +}; + +#undef STATE1 +#undef STATE2 + +extern OpenGLState glstate; diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index dc86026fec..c305d5a079 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -27,7 +27,6 @@ #include "base/logging.h" #include "math/math_util.h" -#include "gfx_es2/gl_state.h" #include "math/lin/matrix4x4.h" #include "profiler/profiler.h" @@ -36,6 +35,7 @@ #include "GPU/Math3D.h" #include "GPU/GPUState.h" #include "GPU/ge_constants.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/ShaderManager.h" #include "GPU/GLES/TransformPipeline.h" #include "UI/OnScreenDisplay.h" diff --git a/GPU/GLES/StateMapping.cpp b/GPU/GLES/StateMapping.cpp index e1cba640be..1d728c7a1c 100644 --- a/GPU/GLES/StateMapping.cpp +++ b/GPU/GLES/StateMapping.cpp @@ -21,7 +21,6 @@ #include "StateMapping.h" -#include "gfx_es2/gl_state.h" #include "profiler/profiler.h" #include "GPU/Math3D.h" @@ -31,6 +30,7 @@ #include "Core/Config.h" #include "Core/Reporting.h" #include "GPU/GLES/GLES_GPU.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/ShaderManager.h" #include "GPU/GLES/TextureCache.h" #include "GPU/GLES/Framebuffer.h" diff --git a/GPU/GLES/StencilBuffer.cpp b/GPU/GLES/StencilBuffer.cpp index 6cfd6ad52e..fe6b7cb70e 100644 --- a/GPU/GLES/StencilBuffer.cpp +++ b/GPU/GLES/StencilBuffer.cpp @@ -16,8 +16,8 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "gfx_es2/glsl_program.h" -#include "gfx_es2/gl_state.h" #include "Core/Reporting.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/Framebuffer.h" #include "GPU/GLES/ShaderManager.h" #include "GPU/GLES/TextureCache.h" diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index aba39f7ad1..d796230bc4 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -18,6 +18,8 @@ #include #include +#include "ext/xxhash.h" +#include "math/math_util.h" #include "profiler/profiler.h" #include "Common/ColorConv.h" @@ -26,6 +28,7 @@ #include "Core/Reporting.h" #include "GPU/ge_constants.h" #include "GPU/GPUState.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/TextureCache.h" #include "GPU/GLES/Framebuffer.h" #include "GPU/GLES/FragmentShaderGenerator.h" @@ -35,10 +38,6 @@ #include "Core/Config.h" #include "Core/Host.h" -#include "ext/xxhash.h" -#include "math/math_util.h" -#include "native/gfx_es2/gl_state.h" - #ifdef _M_SSE #include #endif diff --git a/GPU/GLES/TransformPipeline.cpp b/GPU/GLES/TransformPipeline.cpp index f47599c4ee..bf313174e5 100644 --- a/GPU/GLES/TransformPipeline.cpp +++ b/GPU/GLES/TransformPipeline.cpp @@ -72,7 +72,6 @@ #include "Core/Config.h" #include "Core/CoreTiming.h" -#include "gfx_es2/gl_state.h" #include "profiler/profiler.h" #include "GPU/Math3D.h" @@ -83,6 +82,7 @@ #include "GPU/Common/SplineCommon.h" #include "GPU/Common/VertexDecoderCommon.h" #include "GPU/Common/SoftwareTransformCommon.h" +#include "GPU/GLES/GLStateCache.h" #include "GPU/GLES/FragmentTestCache.h" #include "GPU/GLES/StateMapping.h" #include "GPU/GLES/TextureCache.h" diff --git a/GPU/GPU.vcxproj b/GPU/GPU.vcxproj index cd8ef72cf3..151c945a0d 100644 --- a/GPU/GPU.vcxproj +++ b/GPU/GPU.vcxproj @@ -222,6 +222,7 @@ + @@ -297,6 +298,7 @@ + diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index 493bf25451..c1f9d54f59 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -189,6 +189,9 @@ DirectX9\helper + + GLES + @@ -365,5 +368,8 @@ DirectX9\helper + + GLES + \ No newline at end of file diff --git a/GPU/Software/SoftGpu.cpp b/GPU/Software/SoftGpu.cpp index 79dd804036..c60bd16018 100644 --- a/GPU/Software/SoftGpu.cpp +++ b/GPU/Software/SoftGpu.cpp @@ -29,7 +29,6 @@ #include "Core/MIPS/MIPS.h" #include "Core/Reporting.h" #include "gfx/gl_common.h" -// #include "gfx_es2/gl_state.h" #include "profiler/profiler.h" #include "GPU/Software/SoftGpu.h" diff --git a/Qt/Debugger/debugger_memorytex.cpp b/Qt/Debugger/debugger_memorytex.cpp index fef57135ed..c95ac9bd27 100644 --- a/Qt/Debugger/debugger_memorytex.cpp +++ b/Qt/Debugger/debugger_memorytex.cpp @@ -1,5 +1,5 @@ #include "debugger_memorytex.h" -#include "gfx_es2/gl_state.h" +#include "GPU/GLES/GLStateCache.h" #include "gfx/gl_common.h" #include "gfx/gl_lost_manager.h" #include "ui_debugger_memorytex.h" diff --git a/native b/native index 65e3d31e24..3f02f9fc77 160000 --- a/native +++ b/native @@ -1 +1 @@ -Subproject commit 65e3d31e249ca811809121529f1971e408c154d3 +Subproject commit 3f02f9fc778b5b4c45db92a96f0a4087f947d5a2