summaryrefslogtreecommitdiffstats
path: root/o3d/core/cross/param.cc
blob: c5e7907f5c2ccc475e5979b32246fb3acb18c6ae (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
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
/*
 * 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 definitions for the Param class and the ones derived from
// it for each type of data (ParamFloat, ParamFloat2, ParamFloat3,
// ParamMatrix, ParamBoolean, ParamString, ParamInteger, ParamTexture).

#include "core/cross/precompile.h"
#include "core/cross/param.h"
#include "core/cross/param_object.h"
#include "core/cross/error.h"

namespace o3d {

O3D_DEFN_CLASS(Param, NamedObjectBase);
O3D_DEFN_CLASS(RefParamBase, Param);
O3D_DEFN_CLASS(ParamFloat, Param);
O3D_DEFN_CLASS(ParamFloat2, Param);
O3D_DEFN_CLASS(ParamFloat3, Param);
O3D_DEFN_CLASS(ParamFloat4, Param);
O3D_DEFN_CLASS(ParamInteger, Param);
O3D_DEFN_CLASS(ParamBoolean, Param);
O3D_DEFN_CLASS(ParamString, Param);
O3D_DEFN_CLASS(ParamMatrix4, Param);

// Base constructor that sets the Param type, resets input connection
// and handle.
Param::Param(ServiceLocator* service_locator, bool dynamic, bool read_only)
    : NamedObjectBase(service_locator),
      evaluation_counter_(service_locator->GetService<EvaluationCounter>()),
      input_connection_(NULL),
      not_cachable_count_(0),
      dynamic_(dynamic),
      read_only_(read_only),
      handle_(NULL),
      owner_(NULL),
      last_evaluation_count_(evaluation_counter_->evaluation_count() - 1),
      update_input_(true) {
}

Param::~Param() {
  // There can't be any output connections since they'd have a reference to us.
  DCHECK(output_connections_.empty());
  UnbindInput();
  DCHECK(input_connection_ == NULL);
}

const String& Param::name() const {
  return name_;
}

void Param::SetName(const String& name) {
  DCHECK(!name.empty());
  DCHECK(name_.empty());
  name_ = name;
}

// Updates the contents of data_ by recursively traversing the input connections
// and evaluating them.
void Param::ComputeValue() {
  if (input_connection_) {
    if (update_input_) {
      input_connection_->UpdateValue();
    }
    CopyDataFromParam(input_connection_);
  }
}

void Param::MarkAsReadOnly() {
  DCHECK(input_connection_ == NULL);
  read_only_ = true;
}

// Adds an output connection to the Param by just inserting
// it in the array.
void Param::AddOutputConnection(Param* param) {
  output_connections_.push_back(param);
}

bool Param::UnbindOutput(Param* param) {
  ParamVector::iterator pos = std::find(output_connections_.begin(),
                                        output_connections_.end(),
                                        param);
  if (pos != output_connections_.end()) {
    output_connections_.erase(pos);
    param->ResetInputConnection();
    return true;
  }
  return false;
}

void Param::ResetInputConnection() {
  // Keep a temporary reference to the input_connection
  // so it does not get deleted when we input_connection_.Reset()
  // before we get a chance to call
  // DecrementNotCachableCountOnParamChainForInput.
  Param::Ref temp(input_connection_);
  input_connection_.Reset();
  // This is called after input_connection_.Reset() for symmetry
  // with the way DecrementNotCachableCountOnParamChainForInput
  // is called.
  DecrementNotCachableCountOnParamChainForInput(temp);
  OnAfterUnbindInput(temp.Get());
}

void Param::set_owner(ParamObject* owner) {
  DCHECK((owner_ == NULL && owner != NULL) ||
         (owner_ != NULL && owner == NULL));
  owner_ = owner;
}

namespace {

// Check if a param is in the given param array
// Parameters:
//   param: param to search for.
//   params: ParamVector to search.
// Returns:
//   true if param is in params.
bool ParamInParams(Param* param, const ParamVector* params) {
  return std::find(params->begin(), params->end(), param) != params->end();
}

}  // anonymous namespace

void Param::AddInputsRecursive(const Param* original,
                               ParamVector* params) const {
  if (owner_) {
    ParamVector owner_params;
    owner_->GetInputsForParam(this, &owner_params);
    for (unsigned ii = 0; ii < owner_params.size(); ++ii) {
      Param* param = owner_params[ii];
      if (param != original && !ParamInParams(param, params)) {
        params->push_back(param);
        param->AddInputsRecursive(original, params);
      }
    }
  }
  if (input_connection_ &&
      input_connection_ != original &&
      !ParamInParams(input_connection_, params)) {
    params->push_back(input_connection_);
    input_connection_->AddInputsRecursive(original, params);
  }
}

void Param::AddOutputsRecursive(const Param* original,
                                ParamVector* params) const {
  if (owner_) {
    ParamVector owner_params;
    owner_->GetOutputsForParam(this, &owner_params);
    for (unsigned ii = 0; ii < owner_params.size(); ++ii) {
      Param* param = owner_params[ii];
      if (param != original && !ParamInParams(param, params)) {
        params->push_back(param);
        param->AddOutputsRecursive(original, params);
      }
    }
  }
  for (unsigned ii = 0; ii < output_connections_.size(); ++ii) {
    Param* param = output_connections_[ii];
    if (param != original && !ParamInParams(param, params)) {
      params->push_back(param);
      param->AddOutputsRecursive(original, params);
    }
  }
}

void Param::GetInputs(ParamVector* params) const {
  DCHECK(params);
  params->clear();
  AddInputsRecursive(this, params);
}

void Param::GetOutputs(ParamVector* params) const {
  DCHECK(params);
  params->clear();
  AddOutputsRecursive(this, params);
}

void Param::InvalidateAllOutputs() const {
  ParamVector params;
  GetOutputs(&params);
  for (unsigned ii = 0; ii < params.size(); ++ii) {
    params[ii]->Invalidate();
  }
}

void Param::InvalidateAllInputs() {
  ParamVector params;
  GetInputs(&params);
  for (unsigned ii = 0; ii < params.size(); ++ii) {
    params[ii]->Invalidate();
  }
  Invalidate();
}

void Param::InvalidateAllParameters() {
  evaluation_counter_->InvalidateAllParameters();
}

void Param::SetNotCachable() {
  DCHECK(not_cachable_count_ == 0);
  not_cachable_count_ = 1;
}

void Param::IncrementNotCachableCountOnParamChainForInput(Param* input) {
  if (input && !input->cachable()) {
    ++not_cachable_count_;
    ParamVector params;
    GetOutputs(&params);
    for (ParamVector::size_type ii = 0; ii < params.size(); ++ii) {
      ++params[ii]->not_cachable_count_;
    }
  }
}

void Param::DecrementNotCachableCountOnParamChainForInput(Param* input) {
  if (input && !input->cachable()) {
    --not_cachable_count_;
    ParamVector params;
    GetOutputs(&params);
    for (ParamVector::size_type ii = 0; ii < params.size(); ++ii) {
      --params[ii]->not_cachable_count_;
    }
  }
}

// Connects two Params using a direct connection or unbinds the input if NULL
// is specified as the source Param.
bool Param::Bind(Param *source_param) {
  if (!source_param) {
    UnbindInput();
    return true;
  }

  // When we clear a previous input connection our ref count could go to zero if
  // we don't hold this reference.
  Param::Ref temp(this);

  if (read_only_) {
    O3D_ERROR(service_locator()) << "attempt to bind source param '"
                                 << source_param->name()
                                 << "' to read only param '" << name() << "'";
    return false;
  }

  // Check to make sure the two Params are of the same type
  if (!source_param->IsA(GetClass())) {
    O3D_ERROR(service_locator())
        << "attempt to bind incompatible source param '"
        << source_param->name() << "' of type '"
        << source_param->GetClassName()
        << "' to read only param '" << name()
        << "' of type '" << GetClassName() << "'";
    return false;
  }

  // Check if we are connecting the same thing. It's either this or holding
  // a temp reference to the source becuase when we clear input_connection_
  // the ref count of source_param could go to zero and get freed.
  if (source_param == input_connection_) {
    return true;
  }

  // if we already had an input connection, disconnect it.
  if (input_connection_) {
    // UnbindOutput will clear input_connection_.
    bool result = input_connection_->UnbindOutput(this);
    DCHECK(result);
  }

  DCHECK(input_connection_ == NULL);

  // If our input is not cachable we need to increment the not_cachable_count
  // for ourselves and all the outputs further down the chain.
  IncrementNotCachableCountOnParamChainForInput(source_param);

  // Everything checks out, bind these params.
  input_connection_ = Param::Ref(source_param);
  source_param->AddOutputConnection(this);
  evaluation_counter_->InvalidateAllParameters();
  OnAfterBindInput();
  return true;
}

// Breaks any input connection.
void Param::UnbindInput() {
  Param *source_param = input_connection();
  if (source_param != NULL) {
    bool success = source_param->UnbindOutput(this);
    DLOG_ASSERT(success);
    DCHECK(input_connection_ == NULL);
  }
}

// Breaks all output connections on the given param.
void Param::UnbindOutputs() {
  // We need to keep a ref to ourselves because as inputs get cleared
  // they could release the last reference to us.
  Param::Ref temp(this);

  const ParamVector& params(output_connections());

  while (!params.empty()) {
    Param* param = params.front();
    param->UnbindInput();
  }
}

void Param::ReportReadOnlyError() {
  O3D_ERROR(service_locator()) << "attempt to set read only param '"
                               << name() << "'";
}

void Param::ReportDynamicSetError() {
  O3D_ERROR(service_locator()) << "attempt to set dynamic param '"
                               << name() << "'";
}

ObjectBase::Ref ParamFloat::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamFloat(service_locator, false, false));
}

ObjectBase::Ref ParamFloat2::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamFloat2(service_locator, false, false));
}

ObjectBase::Ref ParamFloat3::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamFloat3(service_locator, false, false));
}

ObjectBase::Ref ParamFloat4::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamFloat4(service_locator, false, false));
}

ObjectBase::Ref ParamInteger::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamInteger(service_locator, false, false));
}

ObjectBase::Ref ParamBoolean::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamBoolean(service_locator, false, false));
}

ObjectBase::Ref ParamString::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamString(service_locator, false, false));
}

ObjectBase::Ref ParamMatrix4::Create(ServiceLocator* service_locator) {
  return ObjectBase::Ref(new ParamMatrix4(service_locator, false, false));
}
}  // namespace o3d