NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StateDump.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Hash.hpp"
30 #include "io/Stream.hpp"
31 
32 namespace FW
33 {
34 
35 //------------------------------------------------------------------------
36 
37 class StateDump : public Serializable
38 {
39 public:
40  StateDump (void) {}
41  StateDump (const StateDump& other) { add(other); }
42  virtual ~StateDump (void) { clear(); }
43 
44  virtual void readFromStream (InputStream& s);
45  virtual void writeToStream (OutputStream& s) const;
46 
47  void clear (void);
48  void add (const StateDump& other);
49  void set (const StateDump& other) { if (&other != this) { clear(); add(other); } }
50 
51  void pushOwner (const String& id) { m_owners.add(xlateId(id + "::")); }
52  void popOwner (void) { m_owners.removeLast(); }
53 
54  bool has (const String& id) const { return m_values.contains(xlateId(id)); }
55  const Array<U8>* get (const String& id) const;
56  bool get (void* ptr, int size, const String& id);
57  template <class T> bool get (T& value, const String& id) const;
58  template <class T> bool get (T& value, const String& id, const T& defValue) const;
59  template <class T> T get (const String& id, const T& defValue) const;
60 
61  void set (const void* ptr, int size, const String& id);
62  template <class T> void set (const T& value, const String& id);
63  void unset (const String& id) { setInternal(NULL, xlateId(id)); }
64 
65  StateDump& operator= (const StateDump& other) { set(other); return *this; }
66 
67 private:
68  String xlateId (const String& id) const { return (m_owners.getSize()) ? m_owners.getLast() + id : id; }
69  void setInternal (Array<U8>* data, const String& id); // takes ownership of data
70 
71 private:
72  Hash<String, Array<U8>* > m_values;
73  Array<String> m_owners;
74 
75  mutable MemoryInputStream m_memIn;
76  MemoryOutputStream m_memOut;
77 };
78 
79 //------------------------------------------------------------------------
80 
81 template <class T> bool StateDump::get(T& value, const String& id) const
82 {
83  const Array<U8>* data = get(id);
84  if (!data)
85  return false;
86 
87  m_memIn.reset(*data);
88  m_memIn >> value;
89  FW_ASSERT(m_memIn.getOffset() == data->getSize());
90  return true;
91 }
92 
93 //------------------------------------------------------------------------
94 
95 template <class T> bool StateDump::get(T& value, const String& id, const T& defValue) const
96 {
97  if (get(value, id))
98  return true;
99  value = defValue;
100  return false;
101 }
102 
103 //------------------------------------------------------------------------
104 
105 template <class T> T StateDump::get(const String& id, const T& defValue) const
106 {
107  T value;
108  get(value, id, defValue);
109  return value;
110 }
111 
112 //------------------------------------------------------------------------
113 
114 template <class T> void StateDump::set(const T& value, const String& id)
115 {
116  m_memOut.clear();
117  m_memOut << value;
118  Array<U8>& data = m_memOut.getData();
119  set(data.getPtr(), data.getNumBytes(), id);
120 }
121 
122 //------------------------------------------------------------------------
123 }
virtual void writeToStream(OutputStream &s) const
Definition: StateDump.cpp:50
#define NULL
Definition: Defs.hpp:39
StateDump(const StateDump &other)
Definition: StateDump.hpp:41
void clear(void)
Definition: StateDump.cpp:59
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid * data
Definition: DLLImports.inl:319
void ** ptr
Definition: DLLImports.cpp:74
StateDump(void)
Definition: StateDump.hpp:40
void add(const StateDump &other)
Definition: StateDump.cpp:68
void popOwner(void)
Definition: StateDump.hpp:52
StateDump & operator=(const StateDump &other)
Definition: StateDump.hpp:65
S getNumBytes(void) const
Definition: Array.hpp:225
void reset(void)
Definition: Stream.hpp:155
void set(const StateDump &other)
Definition: StateDump.hpp:49
Array< U8 > & getData(void)
Definition: Stream.hpp:181
#define FW_ASSERT(X)
Definition: Defs.hpp:67
virtual void readFromStream(InputStream &s)
Definition: StateDump.cpp:34
void unset(const String &id)
Definition: StateDump.hpp:63
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
int getOffset(void) const
Definition: Stream.hpp:152
void pushOwner(const String &id)
Definition: StateDump.hpp:51
bool has(const String &id) const
Definition: StateDump.hpp:54
const Array< U8 > * get(const String &id) const
Definition: StateDump.cpp:77
const T * getPtr(S idx=0) const
Definition: Array.hpp:202
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
virtual ~StateDump(void)
Definition: StateDump.hpp:42
S getSize(void) const
Definition: Array.hpp:188