This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SUTokenStream.h
executable file
·489 lines (375 loc) · 14.5 KB
/
SUTokenStream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//=====================================================================
// Copyright 2010-2016 (c), Advanced Micro Devices, Inc. All rights reserved.
//
/// \author AMD Developer Tools Team
/// \file SUTokenStream.h
/// \brief Token streams handle the reading or writing of streams of
/// tokens to or from memory.
///
//=====================================================================
//=====================================================================
// $Id: //devtools/main/Common/Src/ShaderUtils/SUTokenStream.h#7 $
// Last checkin: $DateTime: 2016/04/18 06:01:26 $
// Last edited by: $Author: AMD Developer Tools Team
// Change list: $Change: 569612 $
//=====================================================================
#ifndef _SU_TOKEN_STREAM_H_
#define _SU_TOKEN_STREAM_H_
#include <cassert>
#include <cstring>
#include "SUCommon.h"
namespace ShaderUtils
{
/// SUTokenStream provides base functionality for CInputTokenSteam and SUOutputTokenStream.
/// Token streams handle the reading or writing of streams of tokens to or from memory.
/// It is an abstract base class that can not be instantiated.
template <typename T> class SUTokenStream
{
protected:
/// Constructor for a SUTokenStream.
/// \param[in] nInitialTokenCount Initial number of tokens to size the stream to. Default is zero.
SUTokenStream(const size_t nInitialTokenCount = 0);
/// Destructor.
virtual ~SUTokenStream() {;};
public:
/// Retrieve a pointer to the stream data.
/// \return Pointer to the stream data.
virtual const T* GetStream() const = 0;
/// Retrieve a pointer to the current position within the stream.
/// \return Pointer to the current position within the stream
virtual const T* GetStreamPos() const = 0;
/// Retrieve the number of tokens in the stream.
/// \return The number of tokens in the stream.
size_t GetTokenCount() const { return m_nTokenCount; }
protected:
/// The number of tokens in the stream.
size_t m_nTokenCount;
};
template <typename T> class SUInputTokenStream;
/// SUOutputTokenStream provides functionality for writing a stream of
/// tokens to memory, re-allocating memory as required.
template <typename T> class SUOutputTokenStream : public SUTokenStream<T>
{
public:
/// Constructor.
/// \param[in] nInitialTokenCount Initial number of tokens to size the stream to.
SUOutputTokenStream(const size_t nInitialTokenCount);
/// Destructor.
virtual ~SUOutputTokenStream();
virtual const T* GetStream() const { return m_ptStream; }
virtual const T* GetStreamPos() const { return m_ptStreamPos; }
/// Write a token to the stream.
/// \param[in] tToken The token to write.
/// \return True if successful, otherwise false.
bool WriteToken(const T tToken);
/// Write a token to the stream at a specific offset.
/// If the offset is beyond the tokens currently written then this write fails.
/// The write overwrites the token at the specified offset.
/// \param[in] nOffset The offset at which to write.
/// \param[in] tToken The token to write.
/// \return True if successful, otherwise false.
bool WriteTokenAt(const size_t nOffset,
const T tToken);
/// Write an array of tokens to the stream.
/// \param[in] ptTokens The tokens to write.
/// \param[in] nNumTokens The number of tokens to write.
/// \return True if successful, otherwise false.
bool WriteTokens(const T* const ptTokens,
const size_t nNumTokens);
/// Write an data to the stream.
/// The number of bytes to write must be a multiple of sizeof(T).
/// \param[in] pData The data to write.
/// \param[in] nBytes The number of bytes of data to write.
/// \return True if successful, otherwise false.
bool WriteData(const void* const pData,
const size_t nBytes);
/// Read tokens from the input stream and write them to this stream.
/// \param[in] src The input stream from which to read.
/// \param[in] nNumTokens The number of tokens to copy/
/// \return True if successful, otherwise false.
bool CopyTokens(SUInputTokenStream<T>& src,
const size_t nNumTokens);
/// Get the number of tokens written.
/// \return The number of tokens written.
size_t GetTokensWritten() const { return m_nTokensWritten; }
protected:
/// Resize the stream to hold the specified number of tokens.
/// The stream can not be resized to less than the number of tokens written.
/// \param[in] nTokenCount The number to tokens to resize to.
/// \return True is successful, otherwise false.
bool Resize(const size_t nTokenCount);
static size_t NextPowerOfTwo(size_t nValue);
T* m_ptStream; ///< Pointer to the buffer holding the stream data.
T* m_ptStreamPos; ///< Pointer to the next position at which to write a token.
size_t m_nTokensWritten; ///< The number of tokens written.
};
/// SUInputTokenStream provides functionality for reading a stream of tokens from memory.
template <typename T> class SUInputTokenStream : public SUTokenStream<T>
{
public:
/// Constructor.
/// \param[in] ptStream The input buffer from which to read tokens.
/// \param[in] nTokenCount The number of tokens in the input buffer.
SUInputTokenStream(const T* const tStream,
const size_t nTokenCount);
virtual const T* GetStream() const { return m_ptStream; }
virtual const T* GetStreamPos() const { return m_ptStreamPos; }
/// Peek at the next token in the input stream, i.e. read it without moving the stream position.
/// \param[out] tToken The next token.
/// \return True if successful, otherwise false.
bool PeekToken(T& tToken);
/// Read the next token in the input stream.
/// \param[out] tToken The next token.
/// \return True if successful, otherwise false.
bool ReadToken(T& tToken);
/// Read the specified number of tokens from the input stream.
/// \param[out] ptTokens The next tokens.
/// \param[in] nTokens The number of tokens to read.
/// \return True if successful, otherwise false.
bool ReadTokens(T* const ptTokens, const size_t nTokens);
/// Skip past the specified number of tokens in the input stream.
/// \param[in] nTokens The number of tokens to skip.
/// \return True if successful, otherwise false.
bool SkipTokens(const size_t nTokens);
/// Friend function in SUOutputTokenStream responsible for reading tokens from the input
/// stream and writing them to the output stream.
/// \param[in] src The input stream from which to read.
/// \param[in] nTokens The number of tokens to copy/
/// \return True if successful, otherwise false.
friend bool SUOutputTokenStream<T>::CopyTokens(SUInputTokenStream<T>& src,
const size_t nTokens);
/// Get the number of tokens read.
/// \return The number of tokens read.
int GetTokensRead() const { return m_nTokensRead; }
protected:
const T* m_ptStream; ///< Pointer to the buffer holding the stream data.
const T* m_ptStreamPos; ///< Pointer to the next position at which to write a token.
size_t m_nTokensRead; ///< The number of tokens read.
};
template <typename T> SUTokenStream<T>::SUTokenStream(const size_t nTokenCount)
: m_nTokenCount(nTokenCount)
{
}
template <typename T> SUOutputTokenStream<T>::SUOutputTokenStream(const size_t nInitialTokenCount)
: SUTokenStream<T>(),
m_ptStream(NULL),
m_ptStreamPos(NULL),
m_nTokensWritten(0)
{
SU_Verify(Resize(nInitialTokenCount));
}
template <typename T> SUOutputTokenStream<T>::~SUOutputTokenStream()
{
SU_SAFE_FREE(m_ptStream);
m_ptStreamPos = NULL;
}
template <typename T> bool SUOutputTokenStream<T>::Resize(const size_t nTokenCount)
{
SU_Assert(nTokenCount >= m_nTokensWritten);
if (nTokenCount < m_nTokensWritten)
{
return false;
}
T* ptNewStream = new(std::nothrow) T[nTokenCount];
SU_Assert(ptNewStream != NULL);
if (ptNewStream == NULL)
{
return false;
}
if (m_ptStream)
{
memcpy(ptNewStream, m_ptStream, m_nTokensWritten * sizeof(T));
delete[] m_ptStream;
}
m_ptStream = ptNewStream;
m_ptStreamPos = m_ptStream + m_nTokensWritten;
this->m_nTokenCount = nTokenCount;
return true;
}
template <typename T> bool SUOutputTokenStream<T>::WriteToken(const T tToken)
{
if (m_nTokensWritten >= this->GetTokenCount())
{
size_t nNewTokenCount = this->GetTokenCount() * 2;
if (nNewTokenCount == 0)
{
nNewTokenCount = 100;
}
if (!Resize(nNewTokenCount))
{
return false;
}
}
SU_Assert(m_ptStreamPos);
SU_Assert(this->m_nTokenCount);
SU_Assert(m_nTokensWritten < this->GetTokenCount());
if (m_ptStreamPos == NULL || m_nTokensWritten >= this->GetTokenCount())
{
return false;
}
*m_ptStreamPos++ = tToken;
m_nTokensWritten++;
return true;
}
template <typename T> bool SUOutputTokenStream<T>::WriteTokenAt(const size_t nOffset,
const T tToken)
{
SU_Assert(m_ptStreamPos);
SU_Assert(m_nTokensWritten);
SU_Assert(nOffset < m_nTokensWritten);
if (m_ptStreamPos == NULL || nOffset >= m_nTokensWritten)
{
return false;
}
m_ptStream[nOffset] = tToken;
return true;
}
template <typename T> size_t SUOutputTokenStream<T>::NextPowerOfTwo(size_t nValue)
{
assert(nValue < ((size_t)1 << ((sizeof(size_t) * 8) - 1)));
// special case zero
if (nValue == 0)
{
return 1;
}
nValue--;
nValue = (nValue >> 1) | nValue;
nValue = (nValue >> 2) | nValue;
nValue = (nValue >> 4) | nValue;
nValue = (nValue >> 8) | nValue;
nValue = (nValue >> 16) | nValue;
#ifdef _WIN64
nValue = (nValue >> 32) | nValue; // needed for 64 bit values
#endif
nValue++;
// if original nValue > (1<<(bits_in_size_t - 1)) then return will be 0
return nValue;
}
template <typename T> bool SUOutputTokenStream<T>::WriteTokens(const T* const ptTokens,
const size_t nNumTokens)
{
SU_Assert(nNumTokens > 0);
if (nNumTokens <= 0)
{
return false;
}
size_t nTokensNeeded = m_nTokensWritten + nNumTokens;
if (nTokensNeeded >= this->GetTokenCount())
{
// Resize to power-of-two greater than nTokensNeeded
size_t nNewTokenCount = NextPowerOfTwo(nTokensNeeded);
if (!Resize(nNewTokenCount))
{
return false;
}
}
SU_Assert(m_ptStreamPos != NULL);
SU_Assert(this->GetTokenCount() > 0);
SU_Assert(nTokensNeeded <= this->GetTokenCount());
SU_Assert(ptTokens != NULL);
if (m_ptStreamPos == NULL || ptTokens == NULL || nTokensNeeded > this->GetTokenCount())
{
return false;
}
memcpy(m_ptStreamPos, ptTokens, nNumTokens * sizeof(T));
m_ptStreamPos += nNumTokens;
m_nTokensWritten += nNumTokens;
return true;
}
template <typename T> bool SUOutputTokenStream<T>::WriteData(const void* const pData,
const size_t nNumBytes)
{
SU_Assert((nNumBytes % sizeof(T)) == 0);
if ((nNumBytes % sizeof(T)) == 0)
{
return WriteTokens((T*)pData, nNumBytes / sizeof(T));
}
return false;
}
template <typename T> bool SUOutputTokenStream<T>::CopyTokens(SUInputTokenStream<T>& src,
const size_t nNumTokens)
{
SU_Assert(m_ptStreamPos);
SU_Assert(this->GetTokenCount());
SU_Assert((m_nTokensWritten + nNumTokens) <= this->GetTokenCount());
if (m_ptStreamPos == NULL || (m_nTokensWritten + nNumTokens) > this->GetTokenCount())
{
return false;
}
SU_Assert(src.m_ptStreamPos);
SU_Assert(src.m_nTokenCount);
SU_Assert((src.m_nTokensRead + nNumTokens) <= src.m_nTokenCount);
if (src.m_ptStreamPos == NULL || (src.m_nTokensRead + nNumTokens) > src.m_nTokenCount)
{
return false;
}
memcpy(m_ptStreamPos, src.m_ptStreamPos, nNumTokens * sizeof(T));
m_ptStreamPos += nNumTokens;
m_nTokensWritten += nNumTokens;
src.m_ptStreamPos += nNumTokens;
src.m_nTokensRead += nNumTokens;
return true;
}
template <typename T> SUInputTokenStream<T>::SUInputTokenStream(const T* const ptStream,
const size_t nTokenCount)
: SUTokenStream<T>(nTokenCount),
m_ptStream(ptStream),
m_ptStreamPos(ptStream),
m_nTokensRead(0)
{
SU_Assert(this->m_nTokenCount > 0);
}
template <typename T> bool SUInputTokenStream<T>::PeekToken(T& tToken)
{
SU_Assert(m_ptStreamPos);
SU_Assert(this->m_nTokenCount);
if (m_ptStreamPos == NULL || m_nTokensRead >= this->GetTokenCount())
{
return false;
}
tToken = *m_ptStreamPos;
return true;
}
template <typename T> bool SUInputTokenStream<T>::ReadToken(T& tToken)
{
SU_Assert(m_ptStreamPos);
SU_Assert(this->GetTokenCount());
if (m_ptStreamPos == NULL || m_nTokensRead >= this->GetTokenCount())
{
return false;
}
tToken = *m_ptStreamPos++;
m_nTokensRead++;
return true;
}
template <typename T> bool SUInputTokenStream<T>::ReadTokens(T* const ptTokens,
const size_t nNumTokens)
{
SU_Assert(m_ptStreamPos);
SU_Assert(this->GetTokenCount());
SU_Assert((m_nTokensRead + nNumTokens) <= this->GetTokenCount());
SU_Assert(ptTokens);
if (m_ptStreamPos == NULL || ptTokens == NULL || (m_nTokensRead + nNumTokens) > this->GetTokenCount())
{
return false;
}
memcpy(ptTokens, m_ptStreamPos, nNumTokens * sizeof(T));
m_ptStreamPos += nNumTokens;
m_nTokensRead += nNumTokens;
return true;
}
template <typename T> bool SUInputTokenStream<T>::SkipTokens(const size_t nNumTokens)
{
SU_Assert(m_ptStreamPos);
SU_Assert(this->GetTokenCount());
SU_Assert((m_nTokensRead + this->nTokens) <= this->GetTokenCount());
if (m_ptStreamPos == NULL || (m_nTokensRead + nNumTokens) > this->GetTokenCount())
{
return false;
}
m_ptStreamPos += nNumTokens;
m_nTokensRead += nNumTokens;
return true;
}
} // namespace ShaderUtils
#endif //_SU_TOKEN_STREAM_H_