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_gl_state_snapshot.h
27 #ifndef VOGL_GL_STATE_SNAPSHOT_H
28 #define VOGL_GL_STATE_SNAPSHOT_H
30 #include "vogl_core.h"
31 #include "vogl_sparse_vector.h"
34 #include "vogl_common.h"
35 #include "vogl_general_context_state.h"
36 #include "vogl_texture_state.h"
37 #include "vogl_buffer_state.h"
38 #include "vogl_fbo_state.h"
39 #include "vogl_sampler_state.h"
40 #include "vogl_shader_state.h"
41 #include "vogl_program_state.h"
42 #include "vogl_renderbuffer_state.h"
43 #include "vogl_query_state.h"
44 #include "vogl_vao_state.h"
45 #include "vogl_texenv_state.h"
46 #include "vogl_light_state.h"
47 #include "vogl_material_state.h"
48 #include "vogl_display_list_state.h"
49 #include "vogl_matrix_state.h"
50 #include "vogl_current_vertex_attrib_state.h"
51 #include "vogl_arb_program_state.h"
52 #include "vogl_handle_tracker.h"
53 #include "vogl_default_framebuffer_state.h"
55 //----------------------------------------------------------------------------------------------------------------------
57 //----------------------------------------------------------------------------------------------------------------------
58 typedef vogl::hash_map<GLuint, GLenum> vogl_handle_hash_map;
59 typedef vogl::hash_map<GLuint> vogl_handle_hash_set;
60 typedef vogl::hash_map<uint64_t, empty_type> vogl_sync_hash_set;
62 //----------------------------------------------------------------------------------------------------------------------
63 // vogl_handle_to_sync
64 //----------------------------------------------------------------------------------------------------------------------
65 inline GLsync vogl_handle_to_sync(uint64_t handle)
68 memcpy(&sync, &handle, math::minimum<uint>(sizeof(GLsync), sizeof(uint64_t)));
72 //----------------------------------------------------------------------------------------------------------------------
73 // vogl_sync_to_handle
74 //----------------------------------------------------------------------------------------------------------------------
75 inline uint64_t vogl_sync_to_handle(GLsync sync)
78 memcpy(&handle, &sync, math::minimum<uint>(sizeof(GLsync), sizeof(uint64_t)));
82 //----------------------------------------------------------------------------------------------------------------------
83 // struct vogl_mapped_buffer_desc
84 //----------------------------------------------------------------------------------------------------------------------
85 struct vogl_mapped_buffer_desc
87 // Handles and ptrs here are in the replay/GL domain
90 vogl_trace_ptr_value m_offset;
91 vogl_trace_ptr_value m_length;
96 vogl_mapped_buffer_desc()
103 memset(this, 0, sizeof(*this));
107 typedef vogl::vector<vogl_mapped_buffer_desc> vogl_mapped_buffer_desc_vec;
109 //----------------------------------------------------------------------------------------------------------------------
110 // class vogl_capture_context_params
111 // TODO: Rename this to vogl_context_state_shadow?
112 //----------------------------------------------------------------------------------------------------------------------
113 class vogl_capture_context_params
116 vogl_capture_context_params()
117 : m_rbos(VOGL_NAMESPACE_RENDER_BUFFERS),
118 m_textures(VOGL_NAMESPACE_TEXTURES),
119 m_objs(VOGL_NAMESPACE_PROGRAMS),
120 m_filter_program_handles(false)
129 m_query_targets.clear();
130 m_buffer_targets.clear();
134 m_framebuffers.clear();
135 m_display_lists.clear();
136 m_arb_program_targets.clear();
142 m_linked_programs.clear();
144 m_program_handles_filter.clear();
145 m_filter_program_handles = false;
148 // During tracing: All handles live in the tracing GL namespace (there is no replay namespace).
149 // During replay: Any handles here live in the actual GL namespace (i.e. the "replay" or native GL namespace, NOT the trace namespace).
150 vogl_handle_hash_map m_query_targets;
151 vogl_handle_hash_map m_buffer_targets;
152 vogl_handle_hash_set m_samplers;
153 vogl_handle_hash_set m_vaos;
154 vogl_sync_hash_set m_syncs;
155 vogl_handle_hash_set m_framebuffers;
156 vogl_handle_hash_map m_arb_program_targets;
158 // During replay: trace domain
159 vogl_display_list_state m_display_lists;
161 // During replay: replay domain
162 vogl_linked_program_state m_linked_programs;
163 vogl_mapped_buffer_desc_vec m_mapped_buffers;
165 // During replay: These objects map from trace (non-inv) to replay (inv).
166 // TODO: Transition ALL the above hash sets/maps to instances of vogl_handle_tracker.
167 vogl_handle_tracker m_rbos;
168 vogl_handle_tracker m_textures;
169 vogl_handle_tracker m_objs;
171 vogl_handle_hash_set m_program_handles_filter;
172 bool m_filter_program_handles;
175 //----------------------------------------------------------------------------------------------------------------------
176 // class vogl_state_snapshot
177 //----------------------------------------------------------------------------------------------------------------------
178 class vogl_context_snapshot
180 VOGL_NO_COPY_OR_ASSIGNMENT_OP(vogl_context_snapshot);
183 vogl_context_snapshot();
184 ~vogl_context_snapshot();
188 bool capture(const vogl_context_desc &desc, const vogl_context_info &info, const vogl_capture_context_params &capture_params, vogl_handle_remapper &remapper);
190 bool is_valid() const
195 const vogl_context_desc &get_context_desc() const
197 return m_context_desc;
199 const vogl_context_info &get_context_info() const
201 return m_context_info;
204 bool remap_handles(vogl_handle_remapper &remapper);
206 const vogl_general_context_state &get_general_state() const
208 return m_general_state;
210 vogl_general_context_state &get_general_state()
212 return m_general_state;
215 const vogl_texenv_state &get_texenv_state() const
217 return m_texenv_state;
219 vogl_texenv_state &get_texenv_state()
221 return m_texenv_state;
224 const vogl_light_state &get_light_state() const
226 return m_light_state;
228 vogl_light_state &get_light_state()
230 return m_light_state;
233 const vogl_material_state &get_material_state() const
235 return m_material_state;
237 vogl_material_state &get_material_state()
239 return m_material_state;
242 const vogl_display_list_state &get_display_list_state() const
244 return m_display_list_state;
246 vogl_display_list_state &get_display_list_state()
248 return m_display_list_state;
251 const vogl_matrix_state &get_matrix_state() const
253 return m_matrix_state;
255 vogl_matrix_state &get_matrix_state()
257 return m_matrix_state;
260 const vogl_polygon_stipple_state &get_polygon_stipple_state() const
262 return m_polygon_stipple_state;
264 vogl_polygon_stipple_state &get_polygon_stipple_state()
266 return m_polygon_stipple_state;
269 const vogl_gl_object_state_ptr_vec &get_objects() const
271 return m_object_ptrs;
273 vogl_gl_object_state_ptr_vec &get_objects()
275 return m_object_ptrs;
278 const vogl_current_vertex_attrib_state &get_current_vertex_attrib_state() const
280 return m_current_vertex_attrib_state;
282 vogl_current_vertex_attrib_state &get_current_vertex_attrib_state()
284 return m_current_vertex_attrib_state;
287 const vogl_arb_program_environment_state &get_arb_program_environment_state() const
289 return m_arb_program_environment_state;
291 vogl_arb_program_environment_state &get_arb_program_environment_state()
293 return m_arb_program_environment_state;
296 void get_all_objects_of_category(vogl_gl_object_state_type state_type, vogl_gl_object_state_ptr_vec &obj_ptr_vec) const;
298 bool serialize(json_node &node, vogl_blob_manager &blob_manager, const vogl_ctypes *pCtypes) const;
299 bool deserialize(const json_node &node, const vogl_blob_manager &blob_manager, const vogl_ctypes *pCtypes);
302 vogl_context_desc m_context_desc;
303 vogl_context_info m_context_info;
305 vogl_general_context_state m_general_state;
306 vogl_texenv_state m_texenv_state;
307 vogl_light_state m_light_state;
308 vogl_material_state m_material_state;
309 vogl_display_list_state m_display_list_state;
310 vogl_matrix_state m_matrix_state;
311 vogl_polygon_stipple_state m_polygon_stipple_state;
312 vogl_current_vertex_attrib_state m_current_vertex_attrib_state;
313 vogl_arb_program_environment_state m_arb_program_environment_state;
315 vogl_gl_object_state_ptr_vec m_object_ptrs;
317 bool capture_objects(vogl_gl_object_state_type state_type, const vogl_capture_context_params &capture_params, vogl_handle_remapper &remapper);
321 void destroy_objects();
324 typedef vogl::vector<vogl_context_snapshot *> vogl_context_snapshot_ptr_vec;
326 //----------------------------------------------------------------------------------------------------------------------
327 // struct vogl_client_side_array_desc
328 //----------------------------------------------------------------------------------------------------------------------
329 struct vogl_client_side_array_desc
331 vogl_client_side_array_desc()
334 vogl_client_side_array_desc(vogl_trace_ptr_value ptr, uint size)
335 : m_ptr(ptr), m_size(size)
339 void init(vogl_trace_ptr_value ptr, uint size)
345 vogl_trace_ptr_value m_ptr;
348 bool serialize(json_node &node, vogl_blob_manager &blob_manager) const
352 VOGL_NOTE_UNUSED(blob_manager);
354 node.add_key_value("ptr", m_ptr);
355 node.add_key_value("size", m_size);
359 bool deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
363 VOGL_NOTE_UNUSED(blob_manager);
365 m_ptr = node.value_as_uint64("ptr");
366 m_size = node.value_as_uint32("size");
371 typedef vogl::vector<vogl_client_side_array_desc> vogl_client_side_array_desc_vec;
373 //----------------------------------------------------------------------------------------------------------------------
374 // class vogl_gl_state_snapshot
375 //----------------------------------------------------------------------------------------------------------------------
376 class vogl_gl_state_snapshot
378 VOGL_NO_COPY_OR_ASSIGNMENT_OP(vogl_gl_state_snapshot);
381 vogl_gl_state_snapshot();
382 ~vogl_gl_state_snapshot();
386 // frame_index indicates the beginning of frame X, at a swap boundary
387 bool begin_capture(uint window_width, uint window_height, vogl_trace_ptr_value cur_context, uint frame_index, int64_t gl_call_counter, bool at_frame_boundary);
389 void add_client_side_array_ptrs(const vogl_client_side_array_desc_vec &client_side_vertex_attrib_ptrs, const vogl_client_side_array_desc_vec &client_side_array_ptrs, const vogl_client_side_array_desc_vec &client_side_texcoord_ptrs);
391 bool capture_context(
392 const vogl_context_desc &desc, const vogl_context_info &info, vogl_handle_remapper &remapper,
393 const vogl_capture_context_params &capture_params);
397 bool is_valid() const
402 bool serialize(json_node &node, vogl_blob_manager &blob_manager, const vogl_ctypes *pCtypes) const;
403 bool deserialize(const json_node &node, const vogl_blob_manager &blob_manager, const vogl_ctypes *pCtypes);
405 md5_hash get_uuid() const
409 uint get_window_width() const
411 return m_window_width;
413 uint get_window_height() const
415 return m_window_height;
417 vogl_trace_ptr_value get_cur_trace_context() const
419 return m_cur_trace_context;
422 void set_frame_index(uint frame_index)
424 m_frame_index = frame_index;
426 uint get_frame_index() const
428 return m_frame_index;
430 int64_t get_gl_call_counter() const
432 return m_gl_call_counter;
434 bool get_at_frame_boundary() const
436 return m_at_frame_boundary;
439 bool get_is_restorable() const
441 return m_is_restorable;
443 void set_is_restorable(bool is_restorable)
445 m_is_restorable = is_restorable;
448 const vogl_client_side_array_desc_vec &get_client_side_vertex_attrib_ptrs() const
450 return m_client_side_vertex_attrib_ptrs;
452 const vogl_client_side_array_desc_vec &get_client_side_array_ptrs() const
454 return m_client_side_array_ptrs;
456 const vogl_client_side_array_desc_vec &get_client_side_texcoord_ptrs() const
458 return m_client_side_texcoord_ptrs;
461 const vogl_context_snapshot_ptr_vec &get_contexts() const
463 return m_context_ptrs;
465 vogl_context_snapshot_ptr_vec &get_contexts()
467 return m_context_ptrs;
470 const vogl_default_framebuffer_state &get_default_framebuffer() const
472 return m_default_framebuffer;
474 vogl_default_framebuffer_state &get_default_framebuffer()
476 return m_default_framebuffer;
479 vogl_context_snapshot* get_context(vogl_trace_ptr_value contextHandle) const
481 for (vogl_context_snapshot_ptr_vec::const_iterator iter = m_context_ptrs.begin(); iter != m_context_ptrs.end(); iter++)
483 if ((*iter)->get_context_desc().get_trace_context() == contextHandle)
494 uint m_window_height;
495 vogl_trace_ptr_value m_cur_trace_context;
497 int64_t m_gl_call_counter;
498 bool m_at_frame_boundary;
499 bool m_is_restorable;
501 vogl_client_side_array_desc_vec m_client_side_vertex_attrib_ptrs;
502 vogl_client_side_array_desc_vec m_client_side_array_ptrs;
503 vogl_client_side_array_desc_vec m_client_side_texcoord_ptrs;
505 vogl_context_snapshot_ptr_vec m_context_ptrs;
507 vogl_default_framebuffer_state m_default_framebuffer;
509 bool m_captured_default_framebuffer;
512 void destroy_contexts()
514 for (uint i = 0; i < m_context_ptrs.size(); i++)
515 vogl_delete(m_context_ptrs[i]);
516 m_context_ptrs.clear();
522 VOGL_DEFINE_BITWISE_MOVABLE(vogl_context_snapshot);
523 VOGL_DEFINE_BITWISE_MOVABLE(vogl_gl_state_snapshot);
526 #endif // VOGL_GL_STATE_SNAPSHOT_H