1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
26 // File: vogl_sync_object.cpp
27 #include "vogl_sync_object.h"
28 #include "vogl_gl_state_snapshot.h"
30 vogl_sync_state::vogl_sync_state()
31 : m_snapshot_handle(0),
37 vogl_sync_state::~vogl_sync_state()
42 // TODO: Requires GL >= 3.2 or extension GL_ARB_sync
43 bool vogl_sync_state::snapshot(const vogl_context_info &context_info, vogl_handle_remapper &remapper, GLuint64 handle, GLenum target)
47 VOGL_NOTE_UNUSED(context_info);
48 VOGL_NOTE_UNUSED(target);
49 VOGL_NOTE_UNUSED(remapper);
55 m_snapshot_handle = handle;
57 GLsync sync = vogl_handle_to_sync(handle);
59 bool any_gl_errors = false;
61 #define GET_INT(pname) \
66 GL_ENTRYPOINT(glGetSynciv)(sync, pname, sizeof(val), &len, &val); \
67 if (vogl_check_gl_error()) \
68 any_gl_errors = true; \
69 m_params.insert(pname, 0, &val, sizeof(val)); \
72 GET_INT(GL_OBJECT_TYPE);
73 GET_INT(GL_SYNC_STATUS);
74 GET_INT(GL_SYNC_CONDITION);
75 GET_INT(GL_SYNC_FLAGS);
83 vogl_error_printf("%s: GL error while enumerating sync %" PRIu64 "'s' params\n", VOGL_METHOD_NAME, (uint64_t)handle);
87 VOGL_ASSERT(m_params.get_value<GLenum>(GL_OBJECT_TYPE) == GL_SYNC_FENCE);
88 VOGL_ASSERT(m_params.get_value<GLenum>(GL_SYNC_CONDITION) == GL_SYNC_GPU_COMMANDS_COMPLETE);
89 VOGL_ASSERT(m_params.get_value<uint>(GL_SYNC_FLAGS) == 0);
96 bool vogl_sync_state::restore(const vogl_context_info &context_info, vogl_handle_remapper &remapper, GLuint64 &handle) const
100 VOGL_NOTE_UNUSED(context_info);
109 GLsync sync = GL_ENTRYPOINT(glFenceSync)(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
110 if ((vogl_check_gl_error()) || (!sync))
112 handle = vogl_sync_to_handle(sync);
114 remapper.declare_handle(VOGL_NAMESPACE_SYNCS, m_snapshot_handle, handle, GL_NONE);
115 VOGL_ASSERT(remapper.remap_handle(VOGL_NAMESPACE_SYNCS, m_snapshot_handle) == handle);
121 bool vogl_sync_state::remap_handles(vogl_handle_remapper &remapper)
125 m_snapshot_handle = remapper.remap_handle(VOGL_NAMESPACE_SYNCS, m_snapshot_handle);
130 void vogl_sync_state::clear()
134 m_snapshot_handle = 0;
139 bool vogl_sync_state::serialize(json_node &node, vogl_blob_manager &blob_manager) const
146 node.add_key_value("handle", m_snapshot_handle);
148 return m_params.serialize(node.add_object("params"), blob_manager);
151 bool vogl_sync_state::deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
157 m_snapshot_handle = node.value_as_uint32("handle");
159 const json_node *pParams_node = node.find_child_object("params");
160 if ((!pParams_node) || (!m_params.deserialize(*pParams_node, blob_manager)))
171 bool vogl_sync_state::compare_restorable_state(const vogl_gl_object_state &rhs_obj) const
175 if ((!m_is_valid) || (!rhs_obj.is_valid()))
178 if (rhs_obj.get_type() != cGLSTSync)
181 const vogl_sync_state &rhs = static_cast<const vogl_sync_state &>(rhs_obj);
186 return m_params == rhs.m_params;