diff options
Diffstat (limited to 'intern/cycles/hydra/display_driver.cpp')
-rw-r--r-- | intern/cycles/hydra/display_driver.cpp | 85 |
1 files changed, 52 insertions, 33 deletions
diff --git a/intern/cycles/hydra/display_driver.cpp b/intern/cycles/hydra/display_driver.cpp index a809ace63e2..0c0b577c358 100644 --- a/intern/cycles/hydra/display_driver.cpp +++ b/intern/cycles/hydra/display_driver.cpp @@ -23,10 +23,18 @@ HdCyclesDisplayDriver::HdCyclesDisplayDriver(HdCyclesSession *renderParam, Hgi * HdCyclesDisplayDriver::~HdCyclesDisplayDriver() { - deinit(); + if (texture_) { + _hgi->DestroyTexture(&texture_); + } + + if (gl_pbo_id_) { + glDeleteBuffers(1, &gl_pbo_id_); + } + + gl_context_dispose(); } -void HdCyclesDisplayDriver::init() +void HdCyclesDisplayDriver::gl_context_create() { #ifdef _WIN32 if (!gl_context_) { @@ -64,16 +72,42 @@ void HdCyclesDisplayDriver::init() } } -void HdCyclesDisplayDriver::deinit() +bool HdCyclesDisplayDriver::gl_context_enable() { - if (texture_) { - _hgi->DestroyTexture(&texture_); +#ifdef _WIN32 + if (!hdc_ || !gl_context_) { + return false; } - if (gl_pbo_id_) { - glDeleteBuffers(1, &gl_pbo_id_); + mutex_.lock(); + + // Do not change context if this is called in the main thread + if (wglGetCurrentContext() == nullptr) { + if (!TF_VERIFY(wglMakeCurrent((HDC)hdc_, (HGLRC)gl_context_))) { + mutex_.unlock(); + return false; + } + } + + return true; +#else + return false; +#endif +} + +void HdCyclesDisplayDriver::gl_context_disable() +{ +#ifdef _WIN32 + if (wglGetCurrentContext() == gl_context_) { + TF_VERIFY(wglMakeCurrent(nullptr, nullptr)); } + mutex_.unlock(); +#endif +} + +void HdCyclesDisplayDriver::gl_context_dispose() +{ #ifdef _WIN32 if (gl_context_) { TF_VERIFY(wglDeleteContext((HGLRC)gl_context_)); @@ -90,13 +124,9 @@ bool HdCyclesDisplayDriver::update_begin(const Params ¶ms, int texture_width, int texture_height) { -#ifdef _WIN32 - if (!hdc_ || !gl_context_) { + if (!gl_context_enable()) { return false; } -#endif - - graphics_interop_activate(); if (gl_render_sync_) { glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED); @@ -121,15 +151,14 @@ bool HdCyclesDisplayDriver::update_begin(const Params ¶ms, void HdCyclesDisplayDriver::update_end() { gl_upload_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); - glFlush(); - graphics_interop_deactivate(); + gl_context_disable(); } void HdCyclesDisplayDriver::flush() { - graphics_interop_activate(); + gl_context_enable(); if (gl_upload_sync_) { glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED); @@ -139,7 +168,7 @@ void HdCyclesDisplayDriver::flush() glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED); } - graphics_interop_deactivate(); + gl_context_disable(); } half4 *HdCyclesDisplayDriver::map_texture_buffer() @@ -179,25 +208,12 @@ DisplayDriver::GraphicsInterop HdCyclesDisplayDriver::graphics_interop_get() void HdCyclesDisplayDriver::graphics_interop_activate() { - mutex_.lock(); - -#ifdef _WIN32 - // Do not change context if this is called in the main thread - if (wglGetCurrentContext() == nullptr) { - TF_VERIFY(wglMakeCurrent((HDC)hdc_, (HGLRC)gl_context_)); - } -#endif + gl_context_enable(); } void HdCyclesDisplayDriver::graphics_interop_deactivate() { -#ifdef _WIN32 - if (wglGetCurrentContext() == gl_context_) { - TF_VERIFY(wglMakeCurrent(nullptr, nullptr)); - } -#endif - - mutex_.unlock(); + gl_context_disable(); } void HdCyclesDisplayDriver::clear() @@ -214,7 +230,11 @@ void HdCyclesDisplayDriver::draw(const Params ¶ms) return; } - init(); + if (!renderBuffer->IsResourceUsed()) { + return; + } + + gl_context_create(); // Cycles 'DisplayDriver' only supports 'half4' format TF_VERIFY(renderBuffer->GetFormat() == HdFormatFloat16Vec4); @@ -255,7 +275,6 @@ void HdCyclesDisplayDriver::draw(const Params ¶ms) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); gl_render_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); - glFlush(); need_update_ = false; |