don't call endFrame() unless beginFrame() was called
only use condition_variable on non-emscripten builds
This commit is contained in:
@@ -32,7 +32,7 @@ static void Log(const char *fmt, ...) {
|
||||
#endif
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
#endif // ifdef __EMSCRIPTEN__
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
|
||||
|
||||
@@ -75,10 +75,8 @@ public:
|
||||
#endif
|
||||
|
||||
bool mRendered = false;
|
||||
|
||||
private:
|
||||
|
||||
bool mRender = false;
|
||||
private:
|
||||
std::mutex _taskMutex;
|
||||
std::condition_variable _cv;
|
||||
std::deque<std::function<void()>> _tasks;
|
||||
@@ -106,8 +104,9 @@ auto RenderThread::add_task(std::packaged_task<Rt()>& pt) -> std::future<Rt> {
|
||||
_tasks.push_back([pt = std::make_shared<std::packaged_task<Rt()>>(
|
||||
std::move(pt))]
|
||||
{ (*pt)(); });
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
_cv.notify_one();
|
||||
#endif
|
||||
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -65,16 +65,19 @@ namespace thermion
|
||||
bool RenderTicker::render(uint64_t frameTimeInNanos)
|
||||
{
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
bool rendered = false;
|
||||
|
||||
std::lock_guard lock(mMutex);
|
||||
|
||||
for (auto animationManager : mAnimationManagers) {
|
||||
animationManager->update(frameTimeInNanos);
|
||||
TRACE("Updated AnimationManager");
|
||||
}
|
||||
|
||||
auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f;
|
||||
TRACE("Updated animations in %.3f ms", durationNs);
|
||||
|
||||
int numRendered = 0;
|
||||
|
||||
#ifdef ENABLE_TRACING
|
||||
int numRendered = 0;
|
||||
TRACE("%d swapchains", mRenderable.size());
|
||||
#endif
|
||||
|
||||
@@ -87,22 +90,21 @@ namespace thermion
|
||||
bool beginFrame = mRenderer->beginFrame(swapChain, frameTimeInNanos);
|
||||
if (beginFrame)
|
||||
{
|
||||
rendered = true;
|
||||
auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f;
|
||||
numRendered++;
|
||||
durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f;
|
||||
TRACE("Beginning frame (%.3f ms since last endFrame())", durationNs);
|
||||
for (auto view : views)
|
||||
{
|
||||
mRenderer->render(view);
|
||||
}
|
||||
mLastRender = std::chrono::high_resolution_clock::now();
|
||||
mLastRender = std::chrono::high_resolution_clock::now();
|
||||
mRenderer->endFrame();
|
||||
} else {
|
||||
auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f;
|
||||
durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f;
|
||||
TRACE("Skipping frame (%.3f ms since last endFrame())", durationNs);
|
||||
}
|
||||
mRenderer->endFrame();
|
||||
|
||||
|
||||
#ifdef ENABLE_TRACING
|
||||
numRendered++;
|
||||
} else {
|
||||
TRACE("No views for swapchain");
|
||||
}
|
||||
@@ -115,11 +117,11 @@ namespace thermion
|
||||
mEngine->execute();
|
||||
#endif
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
|
||||
durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
|
||||
float durationMs = durationNs / 1e6f;
|
||||
|
||||
TRACE("Total render() time: %.3f ms", durationMs);
|
||||
return rendered;
|
||||
return numRendered > 0;
|
||||
}
|
||||
|
||||
void RenderTicker::addAnimationManager(AnimationManager* animationManager) {
|
||||
|
||||
@@ -27,20 +27,23 @@ static void mainLoop(void* arg) {
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto timeSinceLastLoopStart = std::chrono::duration_cast<std::chrono::milliseconds>(startTime - loopStart).count();
|
||||
if(timeSinceLastLoopStart > 20) {
|
||||
Log("%dms elapsed since last loop", timeSinceLastLoopStart);
|
||||
}
|
||||
// if(timeSinceLastLoopStart > 20) {
|
||||
// Log("%dms elapsed since last loop", timeSinceLastLoopStart);
|
||||
// }
|
||||
|
||||
loopStart = startTime;
|
||||
rt->mRendered = false;
|
||||
long long elapsed = 0;
|
||||
int numIters = 0;
|
||||
while (!rt->_stop && elapsed < 10) {
|
||||
while (!rt->_stop && elapsed < 12) {
|
||||
rt->iter();
|
||||
numIters++;
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
}
|
||||
// Log("Spent %lldms processing, %d iters", elapsed, numIters);
|
||||
// if(!rt->mRendered) {
|
||||
// Log("Spent %lldms processing, %d iters, rendered %s, render requested %s, context lost %s", elapsed, numIters, rt->mRendered ? "true" : "false", rt->mRender ? "true" : "false", emscripten_is_webgl_context_lost(Thermion_getGLContext()) ? "yes" : "no");
|
||||
// }
|
||||
if(rt->_stop) {
|
||||
Log("RenderThread stopped")
|
||||
emscripten_set_main_loop_arg(nullptr, nullptr, 0, true);
|
||||
@@ -109,7 +112,9 @@ void RenderThread::requestFrame()
|
||||
TRACE("Warning - frame requested before previous frame has completed rendering");
|
||||
}
|
||||
mRender = true;
|
||||
#ifndef __EMSCRIPTEN__
|
||||
_cv.notify_one();
|
||||
#endif
|
||||
}
|
||||
|
||||
void RenderThread::iter()
|
||||
|
||||
@@ -8,6 +8,7 @@ extern "C" {
|
||||
|
||||
void Thermion_resizeCanvas(int width, int height);
|
||||
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE Thermion_createGLContext();
|
||||
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE Thermion_getGLContext();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -24,7 +24,13 @@ extern "C"
|
||||
EMSCRIPTEN_KEEPALIVE void Thermion_resizeCanvas(int width, int height) {
|
||||
emscripten_set_canvas_element_size("#thermion_canvas", width, height);
|
||||
}
|
||||
|
||||
|
||||
static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE _context;
|
||||
|
||||
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE EMSCRIPTEN_KEEPALIVE Thermion_getGLContext() {
|
||||
return _context;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE EMSCRIPTEN_KEEPALIVE Thermion_createGLContext() {
|
||||
|
||||
std::cout << "Creating WebGL context." << std::endl;
|
||||
@@ -43,16 +49,16 @@ extern "C"
|
||||
attr.renderViaOffscreenBackBuffer = EM_FALSE;
|
||||
attr.majorVersion = 2;
|
||||
|
||||
auto context = emscripten_webgl_create_context("#thermion_canvas", &attr);
|
||||
_context = emscripten_webgl_create_context("#thermion_canvas", &attr);
|
||||
|
||||
if(!context) {
|
||||
if(!_context) {
|
||||
std::cout << "Failed to create WebGL context" << std::endl;
|
||||
return context;
|
||||
return _context;
|
||||
}
|
||||
|
||||
std::cout << "Created WebGL context " << attr.majorVersion << "." << attr.minorVersion << std::endl;
|
||||
|
||||
auto success = emscripten_webgl_make_context_current((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context);
|
||||
auto success = emscripten_webgl_make_context_current(_context);
|
||||
if(success != EMSCRIPTEN_RESULT_SUCCESS) {
|
||||
std::cout << "Failed to make WebGL context current"<< std::endl;
|
||||
} else {
|
||||
@@ -65,7 +71,7 @@ extern "C"
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
std::cout << "Returning context" << std::endl;
|
||||
return context;
|
||||
return _context;
|
||||
}
|
||||
|
||||
emscripten::val emscripten_make_uint8_buffer(int ptr, int length) {
|
||||
|
||||
Reference in New Issue
Block a user