]> git.cworth.org Git - vogl/blob - src/voglcommon/vogl_sync_object.cpp
- Initial support for KHR_debug API's in tracer/replayer
[vogl] / src / voglcommon / vogl_sync_object.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 // File: vogl_sync_object.cpp
27 #include "vogl_sync_object.h"
28 #include "vogl_gl_state_snapshot.h"
29
30 vogl_sync_state::vogl_sync_state()
31     : m_snapshot_handle(0),
32       m_is_valid(false)
33 {
34     VOGL_FUNC_TRACER
35 }
36
37 vogl_sync_state::~vogl_sync_state()
38 {
39     VOGL_FUNC_TRACER
40 }
41
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)
44 {
45     VOGL_FUNC_TRACER
46
47     VOGL_NOTE_UNUSED(context_info);
48     VOGL_NOTE_UNUSED(target);
49     VOGL_NOTE_UNUSED(remapper);
50
51     VOGL_CHECK_GL_ERROR;
52
53     clear();
54
55     m_snapshot_handle = handle;
56
57     GLsync sync = vogl_handle_to_sync(handle);
58
59     bool any_gl_errors = false;
60
61 #define GET_INT(pname)                                                    \
62     do                                                                    \
63     {                                                                     \
64         GLint val = 0;                                                    \
65         GLsizei len = 0;                                                  \
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));                     \
70     } while (0)
71
72     GET_INT(GL_OBJECT_TYPE);
73     GET_INT(GL_SYNC_STATUS);
74     GET_INT(GL_SYNC_CONDITION);
75     GET_INT(GL_SYNC_FLAGS);
76
77 #undef GET_INT
78
79     if (any_gl_errors)
80     {
81         clear();
82
83         vogl_error_printf("%s: GL error while enumerating sync %" PRIu64 "'s' params\n", VOGL_METHOD_NAME, (uint64_t)handle);
84         return false;
85     }
86
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);
90
91     m_is_valid = true;
92
93     return true;
94 }
95
96 bool vogl_sync_state::restore(const vogl_context_info &context_info, vogl_handle_remapper &remapper, GLuint64 &handle) const
97 {
98     VOGL_FUNC_TRACER
99
100     VOGL_NOTE_UNUSED(context_info);
101
102     if (!m_is_valid)
103         return false;
104
105     VOGL_CHECK_GL_ERROR;
106
107     if (!handle)
108     {
109         GLsync sync = GL_ENTRYPOINT(glFenceSync)(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
110         if ((vogl_check_gl_error()) || (!sync))
111             return false;
112         handle = vogl_sync_to_handle(sync);
113
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);
116     }
117
118     return true;
119 }
120
121 bool vogl_sync_state::remap_handles(vogl_handle_remapper &remapper)
122 {
123     VOGL_FUNC_TRACER
124
125     m_snapshot_handle = remapper.remap_handle(VOGL_NAMESPACE_SYNCS, m_snapshot_handle);
126
127     return true;
128 }
129
130 void vogl_sync_state::clear()
131 {
132     VOGL_FUNC_TRACER
133
134     m_snapshot_handle = 0;
135     m_params.clear();
136     m_is_valid = false;
137 }
138
139 bool vogl_sync_state::serialize(json_node &node, vogl_blob_manager &blob_manager) const
140 {
141     VOGL_FUNC_TRACER
142
143     if (!m_is_valid)
144         return false;
145
146     node.add_key_value("handle", m_snapshot_handle);
147
148     return m_params.serialize(node.add_object("params"), blob_manager);
149 }
150
151 bool vogl_sync_state::deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
152 {
153     VOGL_FUNC_TRACER
154
155     clear();
156
157     m_snapshot_handle = node.value_as_uint32("handle");
158
159     const json_node *pParams_node = node.find_child_object("params");
160     if ((!pParams_node) || (!m_params.deserialize(*pParams_node, blob_manager)))
161     {
162         clear();
163         return false;
164     }
165
166     m_is_valid = true;
167
168     return true;
169 }
170
171 bool vogl_sync_state::compare_restorable_state(const vogl_gl_object_state &rhs_obj) const
172 {
173     VOGL_FUNC_TRACER
174
175     if ((!m_is_valid) || (!rhs_obj.is_valid()))
176         return false;
177
178     if (rhs_obj.get_type() != cGLSTSync)
179         return false;
180
181     const vogl_sync_state &rhs = static_cast<const vogl_sync_state &>(rhs_obj);
182
183     if (this == &rhs)
184         return true;
185
186     return m_params == rhs.m_params;
187 }