-
Notifications
You must be signed in to change notification settings - Fork 102
perf(gl): actual CPU state tracking wrappers #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cobaltgit
wants to merge
10
commits into
ButterscotchRunner:main
Choose a base branch
from
cobaltgit:gl-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5369268
perf(gl): actual CPU state tracking wrappers
cobaltgit 7c21f4b
gl state
cobaltgit 2144b51
init arrays
cobaltgit 81b09b7
enable texture2d per slot tracking
cobaltgit f4a2f3a
okay
cobaltgit c96c5de
bounds-check
cobaltgit 8950cb4
whiny ass compiler
cobaltgit 0106c95
inline them all
cobaltgit 9f9e4c2
cache the rest
cobaltgit f6b0dc6
make it a struct
cobaltgit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| #ifndef _BS_GL_STATE_H | ||
| #define _BS_GL_STATE_H | ||
|
|
||
| #define GL_MAX_TEXTURE_2D 32 | ||
|
|
||
| typedef struct { | ||
| GLenum activeTextureSlot; | ||
| GLenum blendSrcRGB; | ||
| GLenum blendDstRGB; | ||
| GLenum blendSrcAlpha; | ||
| GLenum blendDstAlpha; | ||
| GLenum blendEqRGB; | ||
| GLenum blendEqAlpha; | ||
| GLclampf clearColor[4]; | ||
| GLint viewport[4]; | ||
| GLint scissor[4]; | ||
| GLuint currentReadFb; | ||
| GLuint currentDrawFb; | ||
| GLuint currentProgram; | ||
| GLuint currentVAO; | ||
| GLuint boundTextures2D[GL_MAX_TEXTURE_2D]; | ||
| GLboolean blend; | ||
| GLboolean depthTest; | ||
| GLboolean scissorTest; | ||
| GLboolean texture2D[GL_MAX_TEXTURE_2D]; | ||
| } GLStateCache; | ||
|
|
||
| static GLStateCache glState; | ||
| static GLboolean glStateInitialized = GL_FALSE; | ||
|
|
||
| static inline void GLState_init(void) { | ||
| glState.activeTextureSlot = GL_TEXTURE0; | ||
| glState.blendSrcRGB = GL_ONE; | ||
| glState.blendDstRGB = GL_ZERO; | ||
| glState.blendSrcAlpha = GL_ONE; | ||
| glState.blendDstAlpha = GL_ZERO; | ||
| glState.blendEqRGB = GL_FUNC_ADD; | ||
| glState.blendEqAlpha = GL_FUNC_ADD; | ||
|
|
||
| glState.clearColor[0] = -1.0f; glState.clearColor[1] = -1.0f; | ||
| glState.clearColor[2] = -1.0f; glState.clearColor[3] = -1.0f; | ||
|
|
||
| glState.viewport[0] = -1; glState.viewport[1] = -1; | ||
| glState.viewport[2] = -1; glState.viewport[3] = -1; | ||
|
|
||
| glState.scissor[0] = -1; glState.scissor[1] = -1; | ||
| glState.scissor[2] = -1; glState.scissor[3] = -1; | ||
|
|
||
| glState.currentReadFb = 0; | ||
| glState.currentDrawFb = 0; | ||
| glState.currentProgram = 0; | ||
| glState.currentVAO = 0xFFFFFFFF; | ||
|
|
||
| glState.blend = GL_FALSE; | ||
| glState.depthTest = GL_FALSE; | ||
| glState.scissorTest = GL_FALSE; | ||
|
|
||
| for (int i = 0; i < GL_MAX_TEXTURE_2D; i++) { | ||
| glState.boundTextures2D[i] = 0; | ||
| glState.texture2D[i] = GL_FALSE; | ||
| } | ||
|
|
||
| glStateInitialized = GL_TRUE; | ||
| } | ||
|
|
||
| static inline void cached_glViewport(int x, int y, int width, int height) { | ||
| if (glState.viewport[0] == x && glState.viewport[1] == y && glState.viewport[2] == width && glState.viewport[3] == height) return; | ||
| glViewport(x, y, width, height); | ||
| glState.viewport[0] = x; | ||
| glState.viewport[1] = y; | ||
| glState.viewport[2] = width; | ||
| glState.viewport[3] = height; | ||
| } | ||
| #undef glViewport | ||
| #define glViewport cached_glViewport | ||
|
|
||
| static inline void cached_glScissor(int x, int y, int width, int height) { | ||
| if (glState.scissor[0] == x && glState.scissor[1] == y && glState.scissor[2] == width && glState.scissor[3] == height) return; | ||
| glScissor(x, y, width, height); | ||
| glState.scissor[0] = x; | ||
| glState.scissor[1] = y; | ||
| glState.scissor[2] = width; | ||
| glState.scissor[3] = height; | ||
| } | ||
| #undef glScissor | ||
| #define glScissor cached_glScissor | ||
|
|
||
| static inline void cached_glBindFramebuffer(GLenum type, GLuint framebuffer) { | ||
| switch (type) { | ||
| case GL_READ_FRAMEBUFFER: | ||
| if (glState.currentReadFb == framebuffer) return; | ||
| glState.currentReadFb = framebuffer; | ||
| break; | ||
| case GL_DRAW_FRAMEBUFFER: | ||
| if (glState.currentDrawFb == framebuffer) return; | ||
| glState.currentDrawFb = framebuffer; | ||
| break; | ||
| case GL_FRAMEBUFFER: | ||
| if (glState.currentReadFb == framebuffer && glState.currentDrawFb == framebuffer) return; | ||
| glState.currentReadFb = framebuffer; | ||
| glState.currentDrawFb = framebuffer; | ||
| break; | ||
| default: | ||
| return; | ||
| } | ||
| glBindFramebuffer(type, framebuffer); | ||
| } | ||
| #undef glBindFramebuffer | ||
| #define glBindFramebuffer cached_glBindFramebuffer | ||
|
|
||
| static inline void cached_glGetIntegerv(GLenum pname, GLint* params) { | ||
| if (!params) return; | ||
|
|
||
| switch (pname) { | ||
| case GL_VIEWPORT: | ||
| params[0] = glState.viewport[0]; | ||
| params[1] = glState.viewport[1]; | ||
| params[2] = glState.viewport[2]; | ||
| params[3] = glState.viewport[3]; | ||
| return; | ||
| case GL_FRAMEBUFFER_BINDING: | ||
| params[0] = (GLint)glState.currentDrawFb; | ||
| return; | ||
| case GL_READ_FRAMEBUFFER_BINDING: | ||
| params[0] = (GLint)glState.currentReadFb; | ||
| return; | ||
| default: | ||
| glGetIntegerv(pname, params); | ||
| break; | ||
| } | ||
| } | ||
| #undef glGetIntegerv | ||
| #define glGetIntegerv cached_glGetIntegerv | ||
|
|
||
| static inline void cached_glActiveTexture(GLenum texture) { | ||
| if (glState.activeTextureSlot == texture) return; | ||
| glActiveTexture(texture); | ||
| glState.activeTextureSlot = texture; | ||
| } | ||
| #undef glActiveTexture | ||
| #define glActiveTexture cached_glActiveTexture | ||
|
|
||
| static inline void cached_glBindTexture(GLenum target, GLuint texture) { | ||
| if (target == GL_TEXTURE_2D) { | ||
| int slotIndex = glState.activeTextureSlot - GL_TEXTURE0; | ||
| if (slotIndex >= 0 && slotIndex < GL_MAX_TEXTURE_2D) { | ||
| if (glState.boundTextures2D[slotIndex] == texture) return; | ||
| glState.boundTextures2D[slotIndex] = texture; | ||
| } | ||
| } | ||
| glBindTexture(target, texture); | ||
| } | ||
| #undef glBindTexture | ||
| #define glBindTexture cached_glBindTexture | ||
|
|
||
| static inline void cached_glUseProgram(GLuint program) { | ||
| if (glState.currentProgram == program) return; | ||
| glUseProgram(program); | ||
| glState.currentProgram = program; | ||
| } | ||
| #undef glUseProgram | ||
| #define glUseProgram cached_glUseProgram | ||
|
|
||
| static inline void cached_glBindVertexArray(GLuint vao) { | ||
| if (glState.currentVAO == vao) return; | ||
| glBindVertexArray(vao); | ||
| glState.currentVAO = vao; | ||
| } | ||
| #undef glBindVertexArray | ||
| #define glBindVertexArray cached_glBindVertexArray | ||
|
|
||
| static inline void cached_glDeleteVertexArrays(GLsizei n, const GLuint* arrays) { | ||
| for (GLsizei i = 0; i < n; i++) { | ||
| if (glState.currentVAO == arrays[i]) glState.currentVAO = 0; | ||
| } | ||
| glDeleteVertexArrays(n, arrays); | ||
| } | ||
| #undef glDeleteVertexArrays | ||
| #define glDeleteVertexArrays cached_glDeleteVertexArrays | ||
|
|
||
| static inline void cached_glBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) { | ||
| if (glState.blendSrcRGB == srcRGB && glState.blendDstRGB == dstRGB && | ||
| glState.blendSrcAlpha == srcAlpha && glState.blendDstAlpha == dstAlpha) return; | ||
|
|
||
| glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); | ||
|
|
||
| glState.blendSrcRGB = srcRGB; | ||
| glState.blendDstRGB = dstRGB; | ||
| glState.blendSrcAlpha = srcAlpha; | ||
| glState.blendDstAlpha = dstAlpha; | ||
| } | ||
| #undef glBlendFuncSeparate | ||
| #define glBlendFuncSeparate cached_glBlendFuncSeparate | ||
|
|
||
| static inline void cached_glBlendFunc(GLenum sfactor, GLenum dfactor) { | ||
| cached_glBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor); | ||
| } | ||
| #undef glBlendFunc | ||
| #define glBlendFunc cached_glBlendFunc | ||
|
|
||
| static inline void cached_glBlendEquationSeparate(GLenum eqRGB, GLenum eqAlpha) { | ||
| if (glState.blendEqRGB == eqRGB && glState.blendEqAlpha == eqAlpha) return; | ||
| glBlendEquationSeparate(eqRGB, eqAlpha); | ||
| glState.blendEqRGB = eqRGB; | ||
| glState.blendEqAlpha = eqAlpha; | ||
| } | ||
| #undef glBlendEquationSeparate | ||
| #define glBlendEquationSeparate cached_glBlendEquationSeparate | ||
|
|
||
| static inline void cached_glBlendEquation(GLenum mode) { | ||
| cached_glBlendEquationSeparate(mode, mode); | ||
| } | ||
| #undef glBlendEquation | ||
| #define glBlendEquation cached_glBlendEquation | ||
|
|
||
| static inline void cached_glClearColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a) { | ||
| if (glState.clearColor[0] == r && glState.clearColor[1] == g && glState.clearColor[2] == b && glState.clearColor[3] == a) return; | ||
| glClearColor(r, g, b, a); | ||
| glState.clearColor[0] = r; | ||
| glState.clearColor[1] = g; | ||
| glState.clearColor[2] = b; | ||
| glState.clearColor[3] = a; | ||
| } | ||
| #undef glClearColor | ||
| #define glClearColor cached_glClearColor | ||
|
|
||
| static inline void cached_glEnable(GLenum cap) { | ||
| switch (cap) { | ||
| case GL_BLEND: | ||
| if (glState.blend) return; | ||
| glState.blend = GL_TRUE; | ||
| break; | ||
| case GL_DEPTH_TEST: | ||
| if (glState.depthTest) return; | ||
| glState.depthTest = GL_TRUE; | ||
| break; | ||
| case GL_SCISSOR_TEST: | ||
| if (glState.scissorTest) return; | ||
| glState.scissorTest = GL_TRUE; | ||
| break; | ||
| case GL_TEXTURE_2D: { | ||
| int slotIndex = (int)glState.activeTextureSlot - GL_TEXTURE0; | ||
| if (slotIndex >= 0 && slotIndex < GL_MAX_TEXTURE_2D) { | ||
| if (glState.texture2D[slotIndex]) return; | ||
| glState.texture2D[slotIndex] = GL_TRUE; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| glEnable(cap); | ||
| } | ||
| #undef glEnable | ||
| #define glEnable cached_glEnable | ||
|
|
||
| static inline void cached_glDisable(GLenum cap) { | ||
| switch (cap) { | ||
| case GL_BLEND: | ||
| if (!glState.blend) return; | ||
| glState.blend = GL_FALSE; | ||
| break; | ||
| case GL_DEPTH_TEST: | ||
| if (!glState.depthTest) return; | ||
| glState.depthTest = GL_FALSE; | ||
| break; | ||
| case GL_SCISSOR_TEST: | ||
| if (!glState.scissorTest) return; | ||
| glState.scissorTest = GL_FALSE; | ||
| break; | ||
| case GL_TEXTURE_2D: { | ||
| int slotIndex = (int)glState.activeTextureSlot - GL_TEXTURE0; | ||
| if (slotIndex >= 0 && slotIndex < GL_MAX_TEXTURE_2D) { | ||
| if (!glState.texture2D[slotIndex]) return; | ||
| glState.texture2D[slotIndex] = GL_FALSE; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| glDisable(cap); | ||
| } | ||
| #undef glDisable | ||
| #define glDisable cached_glDisable | ||
|
|
||
| static inline GLboolean cached_glIsEnabled(GLenum cap) { | ||
| switch (cap) { | ||
| case GL_BLEND: return glState.blend; | ||
| case GL_DEPTH_TEST: return glState.depthTest; | ||
| case GL_SCISSOR_TEST: return glState.scissorTest; | ||
| case GL_TEXTURE_2D: { | ||
| int slotIndex = (int)glState.activeTextureSlot - GL_TEXTURE0; | ||
| if (slotIndex >= 0 && slotIndex < GL_MAX_TEXTURE_2D) { | ||
| return glState.texture2D[slotIndex]; | ||
| } | ||
| return glIsEnabled(cap); | ||
| } | ||
| default: | ||
| return glIsEnabled(cap); | ||
| } | ||
| } | ||
| #undef glIsEnabled | ||
| #define glIsEnabled cached_glIsEnabled | ||
|
|
||
| #endif // _BS_GL_STATE_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be incompatible with the gl wrappers stuff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I included it after gl_wrappers so it should wrap the framebuffer compatibility wrapper right?