HatchitResource
ht_shadervariable.h
1 
15 #pragma once
16 
17 #include <ht_platform.h>
18 #include <ht_string.h>
19 #include <ht_math.h>
20 #include <ht_debug.h>
21 
22 #ifdef HT_SYS_LINUX
23 #include <cstring>
24 #endif
25 #include <string>
26 
27 namespace Hatchit {
28 
29  namespace Resource {
30 
31  class HT_API ShaderVariable
32  {
33  public:
34  virtual ~ShaderVariable() {
35  if (m_data != nullptr)
36  {
37  delete[] m_data;
38  m_data = nullptr;
39  }
40  };
41 
42  enum Type
43  {
44  INT,
45  FLOAT,
46  DOUBLE,
47  FLOAT2,
48  FLOAT3,
49  FLOAT4,
50  MAT4
51  };
52 
53  inline Type GetType() { return m_type; }
54  inline void* GetData() { return m_data; }
55 
56  static inline ShaderVariable::Type TypeFromString(const std::string& str)
57  {
58  if (str == "INT")
59  return ShaderVariable::Type::INT;
60  else if (str == "FLOAT")
61  return ShaderVariable::Type::FLOAT;
62  else if (str == "DOUBLE")
63  return ShaderVariable::Type::DOUBLE;
64  else if (str == "FLOAT2")
65  return ShaderVariable::Type::FLOAT2;
66  else if (str == "FLOAT3")
67  return ShaderVariable::Type::FLOAT3;
68  else if (str == "FLOAT4")
69  return ShaderVariable::Type::FLOAT4;
70  else if (str == "MAT4")
71  return ShaderVariable::Type::MAT4;
72  else
73  {
74  HT_DEBUG_PRINTF("WARNING: UNKNOWN SHADER VARIABLE TYPE; RETURNING INT\n");
75  return ShaderVariable::Type::INT;
76  }
77  }
78 
79  static inline size_t SizeFromType(ShaderVariable::Type type)
80  {
81  size_t size = 0;
82 
83  switch (type)
84  {
85  case ShaderVariable::Type::INT:
86  size = sizeof(uint32_t);
87  break;
88  case ShaderVariable::Type::DOUBLE:
89  size = sizeof(double);
90  break;
91  case ShaderVariable::Type::FLOAT:
92  size = sizeof(float);
93  break;
94  case ShaderVariable::Type::FLOAT2:
95  size = sizeof(float) * 2;
96  break;
97  case ShaderVariable::Type::FLOAT3:
98  size = sizeof(float) * 3;
99  break;
100  case ShaderVariable::Type::FLOAT4:
101  size = sizeof(float) * 4;
102  break;
103  case ShaderVariable::Type::MAT4:
104  size = sizeof(float) * 16;
105  break;
106  }
107 
108  return size;
109  }
110 
111  protected:
112  Type m_type;
113  BYTE* m_data = nullptr;
114  };
115 
116  template<typename T>
118  {
119  public:
120  inline ShaderVariableTemplate();
121  inline ShaderVariableTemplate(T t);
122 
123  inline void SetData(T t);
124  };
125 
126 
127 
128  template<>
129  inline void HT_API ShaderVariableTemplate<int>::SetData(int data)
130  {
131  memcpy(m_data, &data, sizeof(int));
132  }
133 
134  template <>
136  {
137  m_data = new BYTE[sizeof(int)];
138  SetData(0);
139  m_type = Type::INT;
140  }
141 
142  template <>
144  {
145  m_data = new BYTE[sizeof(int)];
146  SetData(data);
147  m_type = Type::INT;
148  }
149 
150 
151 
152  template<>
153  inline void HT_API ShaderVariableTemplate<float>::SetData(float data)
154  {
155  memcpy(m_data, &data, sizeof(float));
156  }
157 
158  template <>
160  {
161  m_data = new BYTE[sizeof(float)];
162  SetData(0.0f);
163  m_type = Type::FLOAT;
164  }
165 
166  template <>
168  {
169  m_data = new BYTE[sizeof(float)];
170  SetData(data);
171  m_type = Type::FLOAT;
172  }
173 
174 
175 
176  template<>
177  inline void HT_API ShaderVariableTemplate<double>::SetData(double data)
178  {
179  memcpy(m_data, &data, sizeof(double));
180  }
181 
182  template <>
184  {
185  m_data = new BYTE[sizeof(double)];
186  SetData(0.0);
187  m_type = Type::DOUBLE;
188  }
189 
190  template <>
192  {
193  m_data = new BYTE[sizeof(double)];
194  SetData(data);
195  m_type = Type::DOUBLE;
196  }
197 
198 
199 
200  template<>
201  inline void HT_API ShaderVariableTemplate<Math::Vector2>::SetData(Math::Vector2 data)
202  {
203  memcpy(m_data, data.m_data, sizeof(float) * 2);
204  }
205 
206  template <>
208  {
209  m_data = new BYTE[sizeof(float) * 2];
210  SetData(Math::Vector2());
211  m_type = Type::FLOAT2;
212  }
213 
214  template <>
215  inline HT_API ShaderVariableTemplate<Math::Vector2>::ShaderVariableTemplate(Math::Vector2 data)
216  {
217  m_data = new BYTE[sizeof(float) * 2];
218  SetData(data);
219  m_type = Type::FLOAT2;
220  }
221 
222 
223  template<>
224  inline void HT_API ShaderVariableTemplate<Math::Vector3>::SetData(Math::Vector3 data)
225  {
226  memcpy(m_data, data.m_data, sizeof(float) * 3);
227  }
228 
229  template <>
231  {
232  m_data = new BYTE[sizeof(float) * 3];
233  SetData(Math::Vector3());
234  m_type = Type::FLOAT3;
235  }
236 
237  template <>
238  inline HT_API ShaderVariableTemplate<Math::Vector3>::ShaderVariableTemplate(Math::Vector3 data)
239  {
240  m_data = new BYTE[sizeof(float) * 3];
241  SetData(data);
242  m_type = Type::FLOAT3;
243  }
244 
245 
246 
247  template<>
248  inline void HT_API ShaderVariableTemplate<Math::Vector4>::SetData(Math::Vector4 data)
249  {
250  memcpy(m_data, data.m_data, sizeof(float) * 4);
251  }
252 
253  template <>
255  {
256  m_data = new BYTE[sizeof(float) * 4];
257  SetData(Math::Vector4());
258  m_type = Type::FLOAT4;
259  }
260 
261  template <>
262  inline HT_API ShaderVariableTemplate<Math::Vector4>::ShaderVariableTemplate(Math::Vector4 data)
263  {
264  m_data = new BYTE[sizeof(float) * 4];
265  SetData(data);
266  m_type = Type::FLOAT4;
267  }
268 
269 
270 
271  template<>
272  inline void HT_API ShaderVariableTemplate<Math::Matrix4>::SetData(Math::Matrix4 data)
273  {
274  memcpy(this->m_data, data.m_data, sizeof(float) * 16);
275  }
276 
277  template <>
279  {
280  m_data = new BYTE[sizeof(float) * 16];
281  SetData(Math::Matrix4());
282  m_type = Type::MAT4;
283  }
284 
285  template <>
286  inline HT_API ShaderVariableTemplate<Math::Matrix4>::ShaderVariableTemplate(Math::Matrix4 data)
287  {
288  m_data = new BYTE[sizeof(float) * 16];
289  SetData(data);
290  m_type = Type::MAT4;
291  }
292 
293 
294 
295 
303 
304 
305 
307  {
308 
309  };
310  }
311 }
Hatchit Engine Copyright(c) 2015-2016 Third-Degree.
Definition: ht_assimp.h:31
Definition: ht_shadervariable.h:31
Definition: ht_shadervariable.h:117
Definition: ht_shadervariable.h:306