summaryrefslogtreecommitdiffstats
path: root/o3d/core/cross/effect.cc
blob: 9c271fbcf6ec9ffdc0e12ac7acdce80cef57adc0 (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
/*
 * 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 definition of the Effect class.

#include "core/cross/precompile.h"
#include <cctype>
#include <algorithm>
#include "core/cross/effect.h"
#include "core/cross/param_array.h"
#include "core/cross/renderer.h"
#include "core/cross/types.h"
#include "core/cross/error.h"

namespace o3d {

namespace {
struct EffectToUpper {
  char operator() (char c) const  { return std::toupper(c); }
};
}

EffectParameterInfo::EffectParameterInfo(
    const String& name,
    const ObjectBase::Class* class_type,
    int num_elements,
    const String& semantic,
    const ObjectBase::Class* sas_class_type)
      : name_(name),
        class_type_(class_type),
        num_elements_(num_elements),
        semantic_(semantic),
        sas_class_type_(sas_class_type) {
  // Apparently CG uppercases the semantics so we need to do the same
  // in D3D.
  std::transform(semantic_.begin(),
                 semantic_.end(),
                 semantic_.begin(),
                 EffectToUpper());
}

O3D_DEFN_CLASS(Effect, ParamObject);
O3D_DEFN_CLASS(ParamEffect, RefParamBase);

const char* Effect::kVertexShaderEntryPointPrefix =
    "// #o3d VertexShaderEntryPoint ";
const char* Effect::kFragmentShaderEntryPointPrefix =
    "// #o3d PixelShaderEntryPoint ";
const char* Effect::kMatrixLoadOrderPrefix =
    "// #o3d MatrixLoadOrder ";

Effect::Effect(ServiceLocator* service_locator)
    : ParamObject(service_locator),
      weak_pointer_manager_(this),
      matrix_load_order_(ROW_MAJOR) {
}

void Effect::CreateUniformParameters(ParamObject* param_object) {
  CreateSpecifiedParameters(param_object, false);
}

void Effect::CreateSASParameters(ParamObject* param_object) {
  CreateSpecifiedParameters(param_object, true);
}

// Creates parameters on a ParamObject corresponding to the internal effect
// parameters.
void Effect::CreateSpecifiedParameters(ParamObject* param_object, bool sas) {
  String errors;
  EffectParameterInfoArray param_infos;
  GetParameterInfo(&param_infos);
  for (unsigned ii = 0; ii < param_infos.size(); ++ii) {
    const EffectParameterInfo& param_info = param_infos[ii];
    if ((param_info.sas_class_type() != NULL) == sas) {
      Param* param = param_object->GetUntypedParam(param_info.name());
      if (param) {
        // Param exists. Is the the correct type?
        if (!param->IsA(param_info.class_type())) {
          // Remove it
          if (!param_object->RemoveParam(param)) {
            errors += "Could not remove param '" + param->name() +
                      "' type '" + param->GetClassName() +
                      "' on '" + param_object->name() +
                      "' while trying to replace it with param of type '" +
                      param_info.class_type()->name() + "' for Effect '" +
                      name() + "'";
          } else {
            param = NULL;
          }
        }
      }
      if (!param) {
        const ObjectBase::Class* type = param_info.class_type();
        if (param_info.num_elements() == 0) {
          // Non-array type
          param = param_object->CreateParamByClass(
              param_info.name(),
              param_info.sas_class_type() ? param_info.sas_class_type() :
              param_info.class_type());
        } else {
          // Array type
          param = param_object->CreateParamByClass(
              param_info.name(),
              ParamParamArray::GetApparentClass());
        }
        if (!param) {
          errors += String(errors.empty() ? "" : "\n") +
                    String("Could not create Param '") + param_info.name() +
                    String("' type '") + String(type->name()) +
                    String(" for Effect '") + name() + "'";
        }
      }
    }
  }
  if (errors.length()) {
    O3D_ERROR(service_locator()) << errors;
  }
}

namespace {

String::size_type GetEndOfIdentifier(const String& original,
                                     String::size_type start) {
  if (start < original.size()) {
    // check that first character is alpha or '_'
    if (isalpha(original[start]) || original[start] == '_') {
      String::size_type end = original.size();
      String::size_type position = start;
      while (position < end) {
        char c = original[position];
        if (!isalnum(c) && c != '_') {
          break;
        }
        ++position;
      }
      return position;
    }
  }
  return String::npos;
}

bool GetIdentifierAfterString(const String& original,
                              const String& phrase,
                              String* word) {
  String::size_type position = original.find(phrase);
  if (position == String::npos) {
    return false;
  }

  // Find end of identifier
  String::size_type start = position + phrase.size();
  String::size_type end = GetEndOfIdentifier(original, start);
  if (end != start && end != String::npos) {
    *word = String(original, start, end - start);
    return true;
  }
  return false;
}

}  // anonymous namespace

// TODO: Replace this with the runtime shader parser.
//    For now it's very stupid. It requires the word "techinque" not appear
//    anywhere inside the file. Then it searches for
//
//    // #o3d VertexShaderEntryPoint name
//    // #o3d PixelShaderEntryPoint name
//
//    with that exact syntax. No extra whitespace.
//    If it doesn't find both it's a fails.
bool Effect::ValidateFX(const String& effect,
                        String* vertex_shader_entry_point,
                        String* fragment_shader_entry_point,
                        MatrixLoadOrder* matrix_load_order) {
  if (!GetIdentifierAfterString(effect,
                                kVertexShaderEntryPointPrefix,
                                vertex_shader_entry_point)) {
    O3D_ERROR(service_locator()) << "Failed to find \""
                                 << kVertexShaderEntryPointPrefix
                                 << "\" in Effect:" << effect;
    return false;
  }
  if (!GetIdentifierAfterString(effect,
                                kFragmentShaderEntryPointPrefix,
                                fragment_shader_entry_point)) {
    O3D_ERROR(service_locator()) << "Failed to find \""
                                 << kFragmentShaderEntryPointPrefix
                                 << "\" in Effect";
    return false;
  }
  String matrix_load_order_str;
  if (!GetIdentifierAfterString(effect,
                                kMatrixLoadOrderPrefix,
                                &matrix_load_order_str)) {
    O3D_ERROR(service_locator()) << "Failed to find \""
                                 << kMatrixLoadOrderPrefix
                                 << "\" in Effect";
    return false;
  }
  bool column_major = !strcmp(matrix_load_order_str.c_str(), "ColumnMajor");
  *matrix_load_order = column_major ? COLUMN_MAJOR : ROW_MAJOR;

  return true;
}

ObjectBase::Ref Effect::Create(ServiceLocator* service_locator) {
  Renderer* renderer = service_locator->GetService<Renderer>();
  if (NULL == renderer) {
    O3D_ERROR(service_locator) << "No Render Device Available";
    return ObjectBase::Ref();
  }
  return ObjectBase::Ref(renderer->CreateEffect());
}

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