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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This file contains the definitions for the Buffer, VertexBuffer and
// IndexBuffer.
#include "core/cross/precompile.h"
#include <vector>
#include "core/cross/field.h"
#include "core/cross/error.h"
#include "core/cross/buffer.h"
#include "core/cross/pointer_utils.h"
#include "core/cross/types.h"
#include "core/cross/renderer.h"
#include "import/cross/memory_stream.h"
namespace o3d {
O3D_DEFN_CLASS(Field, NamedObject);
O3D_DEFN_CLASS(FloatField, Field);
O3D_DEFN_CLASS(UInt32Field, Field);
O3D_DEFN_CLASS(UByteNField, Field);
namespace {
// Sets a field from a specific type of source converting through a
// convertFunction.
template <typename SourceType,
typename DestinationType,
DestinationType convert_function(SourceType value)>
void SetFrom(const SourceType* source,
unsigned source_stride,
Field* field,
unsigned destination_start_index,
unsigned num_elements) {
if (!field->RangeValid(destination_start_index, num_elements)) {
return;
}
Buffer* buffer = field->buffer();
o3d::BufferLockHelper helper(buffer);
void* buffer_data = helper.GetData(o3d::Buffer::WRITE_ONLY);
if (!buffer_data) {
O3D_ERROR(field->service_locator())
<< "could not lock buffer for field '" << field->name() << "'";
return;
}
unsigned destination_stride = buffer->stride();
unsigned num_components = field->num_components();
DestinationType* destination =
PointerFromVoidPointer<DestinationType*>(
buffer_data,
destination_start_index * destination_stride + field->offset());
for (; num_elements; --num_elements) {
for (unsigned cc = 0; cc < num_components; ++cc) {
destination[cc] = convert_function(source[cc]);
}
source += source_stride;
destination = AddPointerOffset(destination, destination_stride);
}
}
template <typename SourceType,
typename DestinationType,
DestinationType convert_function(SourceType value)>
void SetFromWithSwizzle(const SourceType* source,
unsigned source_stride,
Field* field,
unsigned destination_start_index,
unsigned num_elements,
const int* swizzle_table) {
if (!field->RangeValid(destination_start_index, num_elements)) {
return;
}
Buffer* buffer = field->buffer();
o3d::BufferLockHelper helper(buffer);
void* buffer_data = helper.GetData(o3d::Buffer::WRITE_ONLY);
if (!buffer_data) {
O3D_ERROR(field->service_locator())
<< "could not lock buffer for field '" << field->name() << "'";
return;
}
unsigned destination_stride = buffer->stride();
unsigned num_components = field->num_components();
DestinationType* destination =
PointerFromVoidPointer<DestinationType*>(
buffer_data,
destination_start_index * destination_stride + field->offset());
for (; num_elements; --num_elements) {
for (unsigned cc = 0; cc < num_components; ++cc) {
destination[swizzle_table[cc]] = convert_function(source[cc]);
}
source += source_stride;
destination = AddPointerOffset(destination, destination_stride);
}
}
// Gets a field copying into a specific type of destination converting through a
// convertFunction.
template <typename SourceType,
typename DestinationType,
DestinationType convert_function(SourceType value)>
void GetAs(const Field* field_c,
unsigned source_start_index,
DestinationType* destination,
unsigned destination_stride,
unsigned num_elements) {
Field* field = const_cast<Field*>(field_c);
if (!field->RangeValid(source_start_index, num_elements)) {
return;
}
Buffer* buffer = field->buffer();
o3d::BufferLockHelper helper(buffer);
void* buffer_data = helper.GetData(o3d::Buffer::READ_ONLY);
if (!buffer_data) {
O3D_ERROR(field->service_locator())
<< "could not lock buffer for field '" << field->name() << "'";
return;
}
unsigned source_stride = buffer->stride();
unsigned num_components = field->num_components();
SourceType* source = PointerFromVoidPointer<SourceType*>(
buffer_data,
source_start_index * source_stride + field->offset());
for (; num_elements; --num_elements) {
for (unsigned cc = 0; cc < num_components; ++cc) {
destination[cc] = convert_function(source[cc]);
}
source = AddPointerOffset(source, source_stride);
destination += destination_stride;
}
}
// Gets a field copying into a specific type of destination converting through a
// convertFunction.
template <typename SourceType,
typename DestinationType,
DestinationType convert_function(SourceType value)>
void GetAsWithSwizzle(const Field* field_c,
unsigned source_start_index,
DestinationType* destination,
unsigned destination_stride,
unsigned num_elements,
const int* swizzle_table) {
Field* field = const_cast<Field*>(field_c);
if (!field->RangeValid(source_start_index, num_elements)) {
return;
}
Buffer* buffer = field->buffer();
o3d::BufferLockHelper helper(buffer);
void* buffer_data = helper.GetData(o3d::Buffer::READ_ONLY);
if (!buffer_data) {
O3D_ERROR(field->service_locator())
<< "could not lock buffer for field '" << field->name() << "'";
return;
}
unsigned source_stride = buffer->stride();
unsigned num_components = field->num_components();
SourceType* source = PointerFromVoidPointer<SourceType*>(
buffer_data,
source_start_index * source_stride + field->offset());
for (; num_elements; --num_elements) {
for (unsigned cc = 0; cc < num_components; ++cc) {
destination[cc] = convert_function(source[swizzle_table[cc]]);
}
source = AddPointerOffset(source, source_stride);
destination += destination_stride;
}
}
inline float ConvertFloatToFloat(float value) {
return value;
}
// Note that |value| is an int here since we want to avoid loading
// an incorrectly swapped value into a float register
inline float ConvertLittleEndianFloatToFloat(uint32 value) {
union UInt32FloatUnion {
uint32 i;
float f;
};
UInt32FloatUnion u;
u.i = MemoryReadStream::GetLittleEndianUInt32(
reinterpret_cast<uint32*>(&value));
return u.f;
}
inline uint32 ConvertLittleEndianUInt32ToUInt32(uint32 value) {
return MemoryReadStream::GetLittleEndianUInt32(
reinterpret_cast<uint32*>(&value));
}
inline float ConvertUInt32ToFloat(uint32 value) {
return static_cast<float>(value);
}
inline float ConvertUByteNToFloat(uint8 value) {
return static_cast<float>(value) / 255.0f;
}
inline uint32 ConvertFloatToUInt32(float value) {
return static_cast<uint32>(std::max(0.0f, value));
}
inline uint32 ConvertUInt32ToUInt32(uint32 value) {
return value;
}
inline uint32 ConvertUByteNToUInt32(uint8 value) {
return static_cast<uint32>(value > 0);
}
inline uint8 ConvertFloatToUByteN(float value) {
return static_cast<uint8>(floorf(
std::min(1.0f, std::max(0.0f, value)) * 255.0f + 0.5f));
}
inline uint8 ConvertUInt32ToUByteN(uint32 value) {
return static_cast<uint8>(std::min<unsigned>(255, value));
}
inline uint8 ConvertUByteNToUByteN(uint8 value) {
return value;
}
} // anonymous namespace.
Field::Field(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset)
: NamedObject(service_locator),
buffer_(buffer),
num_components_(num_components),
offset_(offset) {
DCHECK(num_components > 0);
}
bool Field::RangeValid(unsigned int start_index, unsigned int num_elements) {
if (!buffer_) {
O3D_ERROR(service_locator())
<< "The buffer for field '" << name() << "' no longer exists";
return false;
}
if (start_index + num_elements > buffer_->num_elements() ||
start_index + num_elements < start_index) {
O3D_ERROR(service_locator())
<< "Range is not valid for Buffer '"<< buffer_->name()
<< "' on Field '" << name() << "'";
return false;
}
return true;
}
void Field::Copy(const Field& source) {
if (!source.IsA(GetClass())) {
O3D_ERROR(service_locator())
<< "source field of type " << source.GetClassName()
<< " is not compatible with field of type " << GetClassName();
return;
}
if (!source.buffer()) {
O3D_ERROR(service_locator()) << "source buffer is null";
return;
}
if (!buffer()) {
O3D_ERROR(service_locator()) << "destination buffer is null";
return;
}
ConcreteCopy(source);
}
// FloatField -------------------
FloatField::FloatField(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset)
: Field(service_locator, buffer, num_components, offset) {
}
size_t FloatField::GetFieldComponentSize() const {
return sizeof(float); // NOLINT
}
void FloatField::SetFromFloats(const float* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const float, float, ConvertFloatToFloat>(
source, source_stride, this, destination_start_index, num_elements);
}
void FloatField::SetFromUInt32s(const uint32* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const uint32, float, ConvertUInt32ToFloat>(
source, source_stride, this, destination_start_index, num_elements);
}
void FloatField::SetFromUByteNs(const uint8* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const uint8, float, ConvertUByteNToFloat>(
source, source_stride, this, destination_start_index, num_elements);
}
bool FloatField::SetFromMemoryStream(MemoryReadStream* stream) {
if (!buffer()) {
O3D_ERROR(service_locator())
<< "The buffer for field '" << name() << "' no longer exists";
return false;
}
size_t num_elements = buffer()->num_elements();
// sanity check that the stream has enough data
if (stream->GetRemainingByteCount() < num_elements * size()) {
return false;
}
// Note that we're interpreting the source as uint32 since that's
// what ConvertLittleEndianFloatToFloat wants (byte swapping for
// float32 and uint32 are the same). Interpreting floating point
// values before they're byte-swapped can cause problems...
const uint32 *source = stream->GetDirectMemoryPointerAs<const uint32>();
stream->Skip(num_elements * size());
SetFrom<const uint32, float, ConvertLittleEndianFloatToFloat>(
source, num_components(), this, 0, num_elements);
return true;
}
void FloatField::GetAsFloats(unsigned source_start_index,
float* destination,
unsigned destination_stride,
unsigned num_elements) const {
GetAs<const float, float, ConvertFloatToFloat>(
this,
source_start_index,
destination,
destination_stride,
num_elements);
}
void FloatField::ConcreteCopy(const Field& source) {
DCHECK(source.IsA(GetClass()));
DCHECK(source.buffer());
unsigned num_components = source.num_components();
unsigned num_elements = source.buffer()->num_elements();
scoped_array<float> temp(new float[num_components * num_elements]);
source.GetAsFloats(0, temp.get(), num_components, num_elements);
SetFromFloats(temp.get(), num_components, 0, num_elements);
}
Field::Ref FloatField::Create(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset) {
return Field::Ref(
new FloatField(service_locator, buffer, num_components, offset));
}
// UInt32Field -------------------
UInt32Field::UInt32Field(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset)
: Field(service_locator, buffer, num_components, offset) {
}
size_t UInt32Field::GetFieldComponentSize() const {
return sizeof(uint32); // NOLINT
}
void UInt32Field::SetFromFloats(const float* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const float, uint32, ConvertFloatToUInt32>(
source, source_stride, this, destination_start_index, num_elements);
}
void UInt32Field::SetFromUInt32s(const uint32* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const uint32, uint32, ConvertUInt32ToUInt32>(
source, source_stride, this, destination_start_index, num_elements);
}
void UInt32Field::SetFromUByteNs(const uint8* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFrom<const uint8, uint32, ConvertUByteNToUInt32>(
source, source_stride, this, destination_start_index, num_elements);
}
bool UInt32Field::SetFromMemoryStream(MemoryReadStream* stream) {
if (!buffer()) {
O3D_ERROR(service_locator())
<< "The buffer for field '" << name() << "' no longer exists";
return false;
}
size_t num_elements = buffer()->num_elements();
// sanity check that the stream has enough data
if (stream->GetRemainingByteCount() < num_elements * size()) {
return false;
}
const uint32 *source = stream->GetDirectMemoryPointerAs<const uint32>();
stream->Skip(num_elements * size());
SetFrom<const uint32, uint32, ConvertLittleEndianUInt32ToUInt32>(
source, num_components(), this, 0, num_elements);
return true;
}
void UInt32Field::GetAsFloats(unsigned source_start_index,
float* destination,
unsigned destination_stride,
unsigned num_elements) const {
GetAs<const uint32, float, ConvertUInt32ToFloat>(
this,
source_start_index,
destination,
destination_stride,
num_elements);
}
void UInt32Field::GetAsUInt32s(unsigned source_start_index,
uint32* destination,
unsigned destination_stride,
unsigned num_elements) const {
GetAs<const uint32, uint32, ConvertUInt32ToUInt32>(
this,
source_start_index,
destination,
destination_stride,
num_elements);
}
void UInt32Field::ConcreteCopy(const Field& source) {
DCHECK(source.IsA(GetClass()));
DCHECK(source.buffer());
unsigned num_components = source.num_components();
unsigned num_elements = source.buffer()->num_elements();
scoped_array<uint32> temp(new uint32[num_components * num_elements]);
down_cast<const UInt32Field*>(&source)->GetAsUInt32s(
0, temp.get(), num_components, num_elements);
SetFromUInt32s(temp.get(), num_components, 0, num_elements);
}
Field::Ref UInt32Field::Create(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset) {
return Field::Ref(
new UInt32Field(service_locator, buffer, num_components, offset));
}
// UByteNField -------------------
UByteNField::UByteNField(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset)
: Field(service_locator, buffer, num_components, offset) {
Renderer* renderer = service_locator->GetService<Renderer>();
DCHECK(renderer);
DCHECK(num_components % 4 == 0);
swizzle_table_ = renderer->GetRGBAUByteNSwizzleTable();
}
size_t UByteNField::GetFieldComponentSize() const {
return sizeof(uint8); // NOLINT
}
void UByteNField::SetFromFloats(const float* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFromWithSwizzle<const float, uint8, ConvertFloatToUByteN>(
source, source_stride, this, destination_start_index, num_elements,
swizzle_table_);
}
void UByteNField::SetFromUInt32s(const uint32* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFromWithSwizzle<const uint32, uint8, ConvertUInt32ToUByteN>(
source, source_stride, this, destination_start_index, num_elements,
swizzle_table_);
}
void UByteNField::SetFromUByteNs(const uint8* source,
unsigned source_stride,
unsigned destination_start_index,
unsigned num_elements) {
SetFromWithSwizzle<const uint8, uint8, ConvertUByteNToUByteN>(
source, source_stride, this, destination_start_index, num_elements,
swizzle_table_);
}
bool UByteNField::SetFromMemoryStream(MemoryReadStream* stream) {
if (!buffer()) {
O3D_ERROR(service_locator())
<< "The buffer for field '" << name() << "' no longer exists";
return false;
}
size_t num_elements = buffer()->num_elements();
// sanity check that the stream has enough data
if (stream->GetRemainingByteCount() < num_elements * size()) {
return false;
}
const uint8 *source = stream->GetDirectMemoryPointerAs<const uint8>();
stream->Skip(num_elements * size());
SetFromWithSwizzle<const uint8, uint8, ConvertUByteNToUByteN>(
source, num_components(), this, 0, num_elements, swizzle_table_);
return true;
}
void UByteNField::GetAsFloats(unsigned source_start_index,
float* destination,
unsigned destination_stride,
unsigned num_elements) const {
GetAsWithSwizzle<const uint8, float, ConvertUByteNToFloat>(
this,
source_start_index,
destination,
destination_stride,
num_elements,
swizzle_table_);
}
void UByteNField::GetAsUByteNs(unsigned source_start_index,
uint8* destination,
unsigned destination_stride,
unsigned num_elements) const {
GetAsWithSwizzle<const uint8, uint8, ConvertUByteNToUByteN>(
this,
source_start_index,
destination,
destination_stride,
num_elements,
swizzle_table_);
}
void UByteNField::ConcreteCopy(const Field& source) {
DCHECK(source.IsA(GetClass()));
DCHECK(source.buffer());
unsigned num_components = source.num_components();
unsigned num_elements = source.buffer()->num_elements();
scoped_array<uint8> temp(new uint8[num_components * num_elements]);
down_cast<const UByteNField*>(&source)->GetAsUByteNs(
0, temp.get(), num_components, num_elements);
SetFromUByteNs(temp.get(), num_components, 0, num_elements);
}
Field::Ref UByteNField::Create(ServiceLocator* service_locator,
Buffer* buffer,
unsigned num_components,
unsigned offset) {
return Field::Ref(
new UByteNField(service_locator, buffer, num_components, offset));
}
} // namespace o3d
|