summaryrefslogtreecommitdiffstats
path: root/o3d/core/cross/vertex_source_test.cc
blob: ad0cdebf94692f44e80186cf3de538c11ef5324f (plain)
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
/*
 * 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 implements unit tests for class VertexSource.

#include "tests/common/win/testing_common.h"
#include "core/cross/vertex_source.h"
#include "core/cross/error_status.h"
#include "core/cross/evaluation_counter.h"
#include "core/cross/fake_vertex_source.h"
#include "core/cross/pointer_utils.h"
#include "core/cross/buffer.h"
#include "core/cross/object_manager.h"
#include "core/cross/pack.h"
#include "core/cross/service_dependency.h"

namespace o3d {

namespace {

static const float kEpsilon = 0.0000001f;

bool CompareVertices(FakeVertexSource* source,
                     Stream::Semantic semantic,
                     int semantic_index,
                     const float* values,
                     float multiplier) {
  const Stream* stream = source->GetVertexStream(semantic, semantic_index);
  if (!stream) {
    return false;
  }

  const Field& field = stream->field();
  Buffer* buffer = field.buffer();
  if (!buffer) {
    return false;
  }

  BufferLockHelper helper(buffer);
  void* data = helper.GetData(Buffer::READ_ONLY);
  if (!data) {
    return false;
  }

  unsigned num_vertices = stream->GetMaxVertices();
  unsigned stride = buffer->stride();

  const float* source_values = PointerFromVoidPointer<const float*>(
      data, field.offset());
  while (num_vertices) {
    for (unsigned jj = 0; jj < 3; ++jj) {
      float difference = fabsf(source_values[jj] - values[jj] * multiplier);
      if (difference > kEpsilon) {
        return false;
      }
    }
    values += 3;
    source_values = AddPointerOffset(source_values, stride);
    --num_vertices;
  }
  return true;
}

// TODO: Remove the need for this.
void InvalidateAllParameters(Pack* pack) {
  EvaluationCounter* evaluation_counter(
      pack->service_locator()->GetService<EvaluationCounter>());
  evaluation_counter->InvalidateAllParameters();
}

}  // anonymous namespace

class VertexSourceTest : public testing::Test {
 protected:

  VertexSourceTest()
      : object_manager_(g_service_locator) {}

  virtual void SetUp();
  virtual void TearDown();

  Pack* pack() { return pack_; }

 private:
  ServiceDependency<ObjectManager> object_manager_;
  Pack *pack_;
  ErrorStatus* error_status_;
};

void VertexSourceTest::SetUp() {
  error_status_ = new ErrorStatus(g_service_locator);
  pack_ = object_manager_->CreatePack();
}

void VertexSourceTest::TearDown() {
  pack_->Destroy();
  delete error_status_;
}

TEST_F(VertexSourceTest, BindStream) {
  static float some_vertices[][3] = {
    { 1.0f, 2.0f, 3.0f, },
    { 7.0f, 8.0f, 9.0f, },
    { 4.0f, 5.0f, 6.0f, },
  };
  const unsigned kNumVertices = arraysize(some_vertices);
  const unsigned kNumComponents = arraysize(some_vertices[0]);

  scoped_ptr<FakeVertexSource> destination(new FakeVertexSource(
      pack()->service_locator()));
  ASSERT_TRUE(destination != NULL);

  // Create 2 Buffers.
  SourceBuffer* destination_buffer_1 = pack()->Create<SourceBuffer>();
  SourceBuffer* destination_buffer_2 = pack()->Create<SourceBuffer>();
  ASSERT_TRUE(destination_buffer_1 != NULL);
  ASSERT_TRUE(destination_buffer_2 != NULL);

  // Create a field for each buffer.
  Field* destination_field_1 = destination_buffer_1->CreateField(
      FloatField::GetApparentClass(), kNumComponents);
  Field* destination_field_2 = destination_buffer_2->CreateField(
      FloatField::GetApparentClass(), kNumComponents);
  ASSERT_TRUE(destination_field_1 != NULL);
  ASSERT_TRUE(destination_field_2 != NULL);
  ASSERT_TRUE(destination_buffer_1->AllocateElements(kNumVertices));
  ASSERT_TRUE(destination_buffer_2->AllocateElements(kNumVertices));

  // Set up streams on primitive.
  EXPECT_TRUE(destination->SetVertexStream(Stream::POSITION,
                                           0,
                                           destination_field_1,
                                           0));
  EXPECT_TRUE(destination->SetVertexStream(Stream::POSITION,
                                           1,
                                           destination_field_2,
                                           0));

  // Create 2 source buffers.
  SourceBuffer* source_buffer_1 = pack()->Create<SourceBuffer>();
  SourceBuffer* source_buffer_2 = pack()->Create<SourceBuffer>();
  ASSERT_TRUE(source_buffer_1 != NULL);
  ASSERT_TRUE(source_buffer_2 != NULL);

  // Create a field for each buffer.
  Field* source_field_1 = source_buffer_1->CreateField(
      FloatField::GetApparentClass(), kNumComponents);
  Field* source_field_2 = source_buffer_2->CreateField(
      FloatField::GetApparentClass(), kNumComponents);
  ASSERT_TRUE(source_field_1 != NULL);
  ASSERT_TRUE(source_field_2 != NULL);
  ASSERT_TRUE(source_buffer_1->AllocateElements(kNumVertices));
  ASSERT_TRUE(source_buffer_2->AllocateElements(kNumVertices));


  // Put some vertices in the source streams.
  ASSERT_TRUE(source_buffer_1->AllocateElements(kNumVertices));
  source_field_1->SetFromFloats(&some_vertices[0][0], kNumComponents, 0,
                                kNumVertices);
  ASSERT_TRUE(source_buffer_2->AllocateElements(kNumVertices));
  source_field_2->SetFromFloats(&some_vertices[0][0], kNumComponents, 0,
                                kNumVertices);

  scoped_ptr<FakeVertexSource>source(new FakeVertexSource(
      pack()->service_locator()));
  ASSERT_TRUE(source != NULL);

  // Set up streams on source
  EXPECT_TRUE(source->SetVertexStream(Stream::POSITION,
                                      0,
                                      source_field_1,
                                      0));
  EXPECT_TRUE(source->SetVertexStream(Stream::POSITION,
                                      1,
                                      source_field_2,
                                      0));


  // Bind the vertices to both dest streams.
  EXPECT_TRUE(destination->BindStream(source.get(), Stream::POSITION, 0));
  EXPECT_TRUE(destination->BindStream(source.get(), Stream::POSITION, 1));
  // Non-existant streams should fail.
  EXPECT_FALSE(destination->BindStream(source.get(), Stream::POSITION, 2));
  EXPECT_FALSE(destination->BindStream(source.get(), Stream::COLOR, 10));

  // Cause the vertices to get updated.
  destination->UpdateStreams();

  // Test that vertices get updated.
  EXPECT_TRUE(CompareVertices(destination.get(),
                              Stream::POSITION,
                              0,
                              &some_vertices[0][0],
                              2.0f));
  EXPECT_TRUE(CompareVertices(destination.get(),
                              Stream::POSITION,
                              1,
                              &some_vertices[0][0],
                              3.0f));

  // Check that UpdateOutputs only got called once.
  EXPECT_EQ(destination->update_outputs_call_count(), 1U);
  EXPECT_EQ(source->update_outputs_call_count(), 1U);

  // Test that if we chain another VertexSource UpdateOutputs only gets called
  // once.
  SourceBuffer* source_buffer_1b = pack()->Create<SourceBuffer>();
  ASSERT_TRUE(source_buffer_1b != NULL);
  Field* source_field_1b = source_buffer_1b->CreateField(
      FloatField::GetApparentClass(), kNumComponents);
  ASSERT_TRUE(source_field_1b != NULL);

  // Put some vertices in the source streams.
  ASSERT_TRUE(source_buffer_1b->AllocateElements(kNumVertices));
  source_field_1b->SetFromFloats(&some_vertices[0][0], kNumComponents, 0,
                                 kNumVertices);

  scoped_ptr<FakeVertexSource>source_b(new FakeVertexSource(
      pack()->service_locator()));
  ASSERT_TRUE(source_b != NULL);

  // Set up streams on source
  EXPECT_TRUE(source_b->SetVertexStream(Stream::POSITION,
                                        0,
                                        source_field_1b,
                                        0));

  // Bind the vertices to streams.
  EXPECT_TRUE(source->BindStream(source_b.get(), Stream::POSITION, 0));

  // Cause the vertices to get updated.
  InvalidateAllParameters(pack());
  destination->UpdateStreams();

  // Test that vertices get updated.
  EXPECT_TRUE(CompareVertices(destination.get(),
                              Stream::POSITION,
                              0,
                              &some_vertices[0][0],
                              4.0f));
  // Test that these vertices stayed the same.
  EXPECT_TRUE(CompareVertices(destination.get(),
                              Stream::POSITION,
                              1,
                              &some_vertices[0][0],
                              3.0f));

  // Check that UpdateOutputs only got called once.
  EXPECT_EQ(destination->update_outputs_call_count(), 2U);
  EXPECT_EQ(source->update_outputs_call_count(), 2U);
  EXPECT_EQ(source_b->update_outputs_call_count(), 1U);

  // Test that if we ask again UpdateOutputs does not get called.
  destination->UpdateStreams();
  EXPECT_EQ(destination->update_outputs_call_count(), 2U);
  EXPECT_EQ(source->update_outputs_call_count(), 2U);
  EXPECT_EQ(source_b->update_outputs_call_count(), 1U);

  // Test that we can unbind.
  EXPECT_TRUE(source->UnbindStream(Stream::POSITION, 0));
  // Test that unbinding fails if we try a stream that does not exist.
  EXPECT_FALSE(source->UnbindStream(Stream::POSITION, 0));
}

}  // namespace o3d