| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "BsGLEventQuery.h" |
| 4 | #include "BsGLCommandBuffer.h" |
| 5 | #include "Profiling/BsRenderStats.h" |
| 6 | |
| 7 | namespace bs { namespace ct |
| 8 | { |
| 9 | GLEventQuery::GLEventQuery(UINT32 deviceIdx) |
| 10 | { |
| 11 | assert(deviceIdx == 0 && "Multiple GPUs not supported natively on OpenGL."); |
| 12 | |
| 13 | glGenQueries(1, &mQueryObj); |
| 14 | BS_CHECK_GL_ERROR(); |
| 15 | |
| 16 | BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Query); |
| 17 | } |
| 18 | |
| 19 | GLEventQuery::~GLEventQuery() |
| 20 | { |
| 21 | glDeleteQueries(1, &mQueryObj); |
| 22 | BS_CHECK_GL_ERROR(); |
| 23 | |
| 24 | BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Query); |
| 25 | } |
| 26 | |
| 27 | void GLEventQuery::begin(const SPtr<CommandBuffer>& cb) |
| 28 | { |
| 29 | auto execute = [&]() |
| 30 | { |
| 31 | glQueryCounter(mQueryObj, GL_TIMESTAMP); |
| 32 | BS_CHECK_GL_ERROR(); |
| 33 | |
| 34 | setActive(true); |
| 35 | }; |
| 36 | |
| 37 | if (cb == nullptr) |
| 38 | execute(); |
| 39 | else |
| 40 | { |
| 41 | SPtr<GLCommandBuffer> glCB = std::static_pointer_cast<GLCommandBuffer>(cb); |
| 42 | glCB->queueCommand(execute); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool GLEventQuery::isReady() const |
| 47 | { |
| 48 | GLint done = 0; |
| 49 | glGetQueryObjectiv(mQueryObj, GL_QUERY_RESULT_AVAILABLE, &done); |
| 50 | BS_CHECK_GL_ERROR(); |
| 51 | |
| 52 | return done == GL_TRUE; |
| 53 | } |
| 54 | }} |