diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index a918baa5..2ba67f43 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5656,6 +5656,7 @@ GLFWAPI void glfwSwapBuffers(GLFWwindow* window); * @sa @ref buffer_swap * @sa @ref glfwSwapBuffers * @sa @ref glfwSwapInterval + * @sa @ref glfwGetBufferAge * * @since Added in version 3.4. * @@ -5709,6 +5710,36 @@ GLFWAPI void glfwSwapBuffersWithDamage(GLFWwindow* window, GLFWrect* rects, int */ GLFWAPI void glfwSwapInterval(int interval); +/*! @brief Returns the buffer age of the window’s current buffer. + * + * This function returns the age of the current buffer, in frames. It may be + * used to redraw only the parts of the buffer that have changed since this + * buffer was last used, thus avoiding a clear and draw of the entire buffer. + * + * A context must be current on the calling thread. Calling this function + * without a current context will cause a @ref GLFW_NO_CURRENT_CONTEXT error. + * + * This function does not apply to Vulkan. If you are rendering with Vulkan, + * see the present mode of your swapchain instead. + * + * @param[in] window The window whose buffers to swap. + * @return The age of the back buffer if the extension is available, or 0 + * otherwise. + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED, @ref + * GLFW_NO_CURRENT_CONTEXT and @ref GLFW_PLATFORM_ERROR. + * + * @thread_safety This function may be called from any thread. + * + * @sa @ref buffer_swap + * @sa @ref glfwSwapBuffersWithDamage + * + * @since Added in version 3.4. + * + * @ingroup context + */ +GLFWAPI int glfwGetBufferAge(GLFWwindow* window); + /*! @brief Returns whether the specified extension is available. * * This function returns whether the specified diff --git a/src/context.c b/src/context.c index 8f5b868a..90c1c094 100644 --- a/src/context.c +++ b/src/context.c @@ -694,6 +694,26 @@ GLFWAPI void glfwSwapInterval(int interval) window->context.swapInterval(interval); } +GLFWAPI int glfwGetBufferAge(GLFWwindow* handle) +{ + _GLFWwindow* window = (_GLFWwindow*) handle; + assert(window != NULL); + + _GLFW_REQUIRE_INIT_OR_RETURN(0); + + if (window->context.client == GLFW_NO_API) + { + _glfwInputError(GLFW_NO_WINDOW_CONTEXT, + "Cannot get buffer age of a window that has no OpenGL or OpenGL ES context"); + return 0; + } + + if (window->context.getBufferAge) + return window->context.getBufferAge(window); + + return 0; +} + GLFWAPI int glfwExtensionSupported(const char* extension) { _GLFWwindow* window; diff --git a/src/internal.h b/src/internal.h index ab0d1ebd..8ea273c4 100644 --- a/src/internal.h +++ b/src/internal.h @@ -80,6 +80,7 @@ typedef void (* _GLFWmakecontextcurrentfun)(_GLFWwindow*); typedef void (* _GLFWswapbuffersfun)(_GLFWwindow*); typedef void (* _GLFWswapbufferswithdamagefun)(_GLFWwindow*,GLFWrect*,int); typedef void (* _GLFWswapintervalfun)(int); +typedef int (* _GLFWgetbufferagefun)(_GLFWwindow*); typedef int (* _GLFWextensionsupportedfun)(const char*); typedef GLFWglproc (* _GLFWgetprocaddressfun)(const char*); typedef void (* _GLFWdestroycontextfun)(_GLFWwindow*); @@ -353,6 +354,7 @@ struct _GLFWcontext _GLFWswapbuffersfun swapBuffers; _GLFWswapbufferswithdamagefun swapBuffersWithDamage; _GLFWswapintervalfun swapInterval; + _GLFWgetbufferagefun getBufferAge; _GLFWextensionsupportedfun extensionSupported; _GLFWgetprocaddressfun getProcAddress; _GLFWdestroycontextfun destroy;