From 18fc14a060a059d24b1ae14d98b7b48af039fa39 Mon Sep 17 00:00:00 2001
From: Vincent Tavernier <vincent.tavernier@inria.fr>
Date: Thu, 8 Feb 2018 11:31:55 +0100
Subject: [PATCH] Update iTimeDelta automatically on all buffers

---
 examples/50-shadertoy/main.cpp |  2 --
 src/RenderContext.cpp          |  1 +
 src/ToyBuffer.cpp              | 12 ++++++++++++
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/examples/50-shadertoy/main.cpp b/examples/50-shadertoy/main.cpp
index 37169a0..dd5a519 100644
--- a/examples/50-shadertoy/main.cpp
+++ b/examples/50-shadertoy/main.cpp
@@ -397,8 +397,6 @@ int render(GLFWwindow* window,
 						 << "ns fps: " << (1e9 / renderTime)
 						 << " mpx/s: " << (contextConfig.width * contextConfig.height / (renderTime / 1e3))
 						 << std::endl;
-
-				state.V<shadertoy::iTimeDelta>() = renderTime / 1e9;
 			}
 
 			// Buffer swapping
diff --git a/src/RenderContext.cpp b/src/RenderContext.cpp
index 99d92bc..a637f73 100644
--- a/src/RenderContext.cpp
+++ b/src/RenderContext.cpp
@@ -101,6 +101,7 @@ void RenderContext::Initialize()
 {
 	// Initialize constant uniforms
 	state.V<iResolution>() = glm::vec3(config.width, config.height, 1.0f);
+	// Note that this will be overriden once query measurements are available
 	state.V<iTimeDelta>() = 1.0f / (float) config.targetFramerate;
 	state.V<iFrameRate>() = (float) config.targetFramerate;
 
diff --git a/src/ToyBuffer.cpp b/src/ToyBuffer.cpp
index 935d9db..eac6e9d 100644
--- a/src/ToyBuffer.cpp
+++ b/src/ToyBuffer.cpp
@@ -144,6 +144,18 @@ void ToyBuffer::Render()
 		resolutions[i][2] = 1.0f;
 	}
 
+	// Try to set iTimeDelta
+	GLint available = 0;
+	timeDeltaQuery.GetObjectiv(GL_QUERY_RESULT_AVAILABLE, &available);
+	if (available)
+	{
+		// Result available, set uniform value
+		GLuint64 timeDelta;
+		timeDeltaQuery.GetObjectui64v(GL_QUERY_RESULT, &timeDelta);
+		static_pointer_cast<ShaderInputsType::BoundInputs>(boundInputs[0])
+			->State.V<iTimeDelta>() = timeDelta / 1e9;
+	}
+
 	// Set all uniforms
 	for (auto &inputs : boundInputs)
 		inputs->Apply();
-- 
GitLab