summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/shader_translator.cc
blob: 855cecaf109a3b4357bb9557803b8ea7bbfc2bb4 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/shader_translator.h"

#include <string.h>
#include <algorithm>

#include "base/at_exit.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"

namespace {

using gpu::gles2::ShaderTranslator;

bool g_translator_initialized = false;

void FinalizeShaderTranslator(void* /* dummy */) {
  TRACE_EVENT0("gpu", "ShFinalize");
  ShFinalize();
  g_translator_initialized = false;
}

bool InitializeShaderTranslator() {
  if (!g_translator_initialized) {
    TRACE_EVENT0("gpu", "ShInitialize");
    CHECK(ShInitialize());
    base::AtExitManager::RegisterCallback(&FinalizeShaderTranslator, NULL);
    g_translator_initialized = true;
  }
  return g_translator_initialized;
}

#if !defined(ANGLE_SH_VERSION) || ANGLE_SH_VERSION < 108
typedef int ANGLEGetInfoType;
#else
typedef size_t ANGLEGetInfoType;
#endif

void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type,
                     ShaderTranslator::VariableMap* var_map) {
  ANGLEGetInfoType name_len = 0, mapped_name_len = 0;
  switch (var_type) {
    case SH_ACTIVE_ATTRIBUTES:
      ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &name_len);
      break;
    case SH_ACTIVE_UNIFORMS:
      ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &name_len);
      break;
    case SH_VARYINGS:
      ShGetInfo(compiler, SH_VARYING_MAX_LENGTH, &name_len);
      break;
    default: NOTREACHED();
  }
  ShGetInfo(compiler, SH_MAPPED_NAME_MAX_LENGTH, &mapped_name_len);
  if (name_len <= 1 || mapped_name_len <= 1) return;
  scoped_ptr<char[]> name(new char[name_len]);
  scoped_ptr<char[]> mapped_name(new char[mapped_name_len]);

  ANGLEGetInfoType num_vars = 0;
  ShGetInfo(compiler, var_type, &num_vars);
  for (ANGLEGetInfoType i = 0; i < num_vars; ++i) {
    ANGLEGetInfoType len = 0;
    int size = 0;
    ShDataType type = SH_NONE;
    ShPrecisionType precision = SH_PRECISION_UNDEFINED;
    int static_use = 0;

    ShGetVariableInfo(compiler, var_type, i,
                      &len, &size, &type, &precision, &static_use,
                      name.get(), mapped_name.get());

    // In theory we should CHECK(len <= name_len - 1) here, but ANGLE needs
    // to handle long struct field name mapping before we can do this.
    // Also, we should modify the ANGLE interface to also return a length
    // for mapped_name.
    std::string name_string(name.get(), std::min(len, name_len - 1));
    mapped_name.get()[mapped_name_len - 1] = '\0';

    ShaderTranslator::VariableInfo info(
        type, size, precision, static_use, name_string);
    (*var_map)[mapped_name.get()] = info;
  }
}

void GetNameHashingInfo(
    ShHandle compiler, ShaderTranslator::NameMap* name_map) {
  ANGLEGetInfoType hashed_names_count = 0;
  ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count);
  if (hashed_names_count == 0)
    return;

  ANGLEGetInfoType name_max_len = 0, hashed_name_max_len = 0;
  ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len);
  ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len);

  scoped_ptr<char[]> name(new char[name_max_len]);
  scoped_ptr<char[]> hashed_name(new char[hashed_name_max_len]);

  for (ANGLEGetInfoType i = 0; i < hashed_names_count; ++i) {
    ShGetNameHashingEntry(compiler, i, name.get(), hashed_name.get());
    (*name_map)[hashed_name.get()] = name.get();
  }
}

}  // namespace

namespace gpu {
namespace gles2 {

ShaderTranslator::DestructionObserver::DestructionObserver() {
}

ShaderTranslator::DestructionObserver::~DestructionObserver() {
}

ShaderTranslator::ShaderTranslator()
    : compiler_(NULL),
      implementation_is_glsl_es_(false),
      driver_bug_workarounds_(static_cast<ShCompileOptions>(0)) {
}

bool ShaderTranslator::Init(
    ShShaderType shader_type,
    ShShaderSpec shader_spec,
    const ShBuiltInResources* resources,
    ShaderTranslatorInterface::GlslImplementationType glsl_implementation_type,
    ShCompileOptions driver_bug_workarounds) {
  // Make sure Init is called only once.
  DCHECK(compiler_ == NULL);
  DCHECK(shader_type == SH_FRAGMENT_SHADER || shader_type == SH_VERTEX_SHADER);
  DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC);
  DCHECK(resources != NULL);

  if (!InitializeShaderTranslator())
    return false;

  ShShaderOutput shader_output =
      (glsl_implementation_type == kGlslES ? SH_ESSL_OUTPUT : SH_GLSL_OUTPUT);

  {
    TRACE_EVENT0("gpu", "ShConstructCompiler");
    compiler_ = ShConstructCompiler(
        shader_type, shader_spec, shader_output, resources);
  }
  compiler_options_ = *resources;
  implementation_is_glsl_es_ = (glsl_implementation_type == kGlslES);
  driver_bug_workarounds_ = driver_bug_workarounds;
  return compiler_ != NULL;
}

int ShaderTranslator::GetCompileOptions() const {
  int compile_options =
      SH_OBJECT_CODE | SH_VARIABLES |
      SH_MAP_LONG_VARIABLE_NAMES | SH_ENFORCE_PACKING_RESTRICTIONS |
      SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH;

  compile_options |= SH_CLAMP_INDIRECT_ARRAY_BOUNDS;

  compile_options |= driver_bug_workarounds_;

  return compile_options;
}

bool ShaderTranslator::Translate(const char* shader) {
  // Make sure this instance is initialized.
  DCHECK(compiler_ != NULL);
  DCHECK(shader != NULL);
  ClearResults();

  bool success = false;
  {
    TRACE_EVENT0("gpu", "ShCompile");
    success = !!ShCompile(compiler_, &shader, 1, GetCompileOptions());
  }
  if (success) {
    // Get translated shader.
    ANGLEGetInfoType obj_code_len = 0;
    ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len);
    if (obj_code_len > 1) {
      translated_shader_.reset(new char[obj_code_len]);
      ShGetObjectCode(compiler_, translated_shader_.get());
    }
    // Get info for attribs and uniforms.
    GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_);
    GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_);
    GetVariableInfo(compiler_, SH_VARYINGS, &varying_map_);
    // Get info for name hashing.
    GetNameHashingInfo(compiler_, &name_map_);
  }

  // Get info log.
  ANGLEGetInfoType info_log_len = 0;
  ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len);
  if (info_log_len > 1) {
    info_log_.reset(new char[info_log_len]);
    ShGetInfoLog(compiler_, info_log_.get());
  } else {
    info_log_.reset();
  }

  return success;
}

std::string ShaderTranslator::GetStringForOptionsThatWouldEffectCompilation()
    const {
  const size_t kNumIntFields = 16;
  const size_t kNumEnumFields = 1;
  const size_t kNumFunctionPointerFields = 1;
  struct MustMatchShBuiltInResource {
    typedef khronos_uint64_t (*FunctionPointer)(const char*, size_t);
    enum Enum {
      kFirst,
    };
    int int_fields[kNumIntFields];
    FunctionPointer pointer_fields[kNumFunctionPointerFields];
    Enum enum_fields[kNumEnumFields];
  };
  // If this assert fails most likely that means something below needs updating.
  COMPILE_ASSERT(
      sizeof(ShBuiltInResources) == sizeof(MustMatchShBuiltInResource),
      Fields_Have_Changed_In_ShBuiltInResource_So_Update_Below);

  return std::string(
      ":CompileOptions:" +
      base::IntToString(GetCompileOptions()) +
      ":MaxVertexAttribs:" +
      base::IntToString(compiler_options_.MaxVertexAttribs) +
      ":MaxVertexUniformVectors:" +
      base::IntToString(compiler_options_.MaxVertexUniformVectors) +
      ":MaxVaryingVectors:" +
      base::IntToString(compiler_options_.MaxVaryingVectors) +
      ":MaxVertexTextureImageUnits:" +
      base::IntToString(compiler_options_.MaxVertexTextureImageUnits) +
      ":MaxCombinedTextureImageUnits:" +
      base::IntToString(compiler_options_.MaxCombinedTextureImageUnits) +
      ":MaxTextureImageUnits:" +
      base::IntToString(compiler_options_.MaxTextureImageUnits) +
      ":MaxFragmentUniformVectors:" +
      base::IntToString(compiler_options_.MaxFragmentUniformVectors) +
      ":MaxDrawBuffers:" +
      base::IntToString(compiler_options_.MaxDrawBuffers) +
      ":OES_standard_derivatives:" +
      base::IntToString(compiler_options_.OES_standard_derivatives) +
      ":OES_EGL_image_external:" +
      base::IntToString(compiler_options_.OES_EGL_image_external) +
      ":ARB_texture_rectangle:" +
      base::IntToString(compiler_options_.ARB_texture_rectangle) +
      ":EXT_draw_buffers:" +
      base::IntToString(compiler_options_.EXT_draw_buffers) +
      ":FragmentPrecisionHigh:" +
      base::IntToString(compiler_options_.FragmentPrecisionHigh) +
      ":MaxExpressionComplexity:" +
      base::IntToString(compiler_options_.MaxExpressionComplexity) +
      ":MaxCallStackDepth:" +
      base::IntToString(compiler_options_.MaxCallStackDepth) +
      ":EXT_frag_depth:" +
      base::IntToString(compiler_options_.EXT_frag_depth));
}

const char* ShaderTranslator::translated_shader() const {
  return translated_shader_.get();
}

const char* ShaderTranslator::info_log() const {
  return info_log_.get();
}

const ShaderTranslatorInterface::VariableMap&
ShaderTranslator::attrib_map() const {
  return attrib_map_;
}

const ShaderTranslatorInterface::VariableMap&
ShaderTranslator::uniform_map() const {
  return uniform_map_;
}

const ShaderTranslatorInterface::VariableMap&
ShaderTranslator::varying_map() const {
  return varying_map_;
}

const ShaderTranslatorInterface::NameMap&
ShaderTranslator::name_map() const {
  return name_map_;
}

void ShaderTranslator::AddDestructionObserver(
    DestructionObserver* observer) {
  destruction_observers_.AddObserver(observer);
}

void ShaderTranslator::RemoveDestructionObserver(
    DestructionObserver* observer) {
  destruction_observers_.RemoveObserver(observer);
}

ShaderTranslator::~ShaderTranslator() {
  FOR_EACH_OBSERVER(DestructionObserver,
                    destruction_observers_,
                    OnDestruct(this));

  if (compiler_ != NULL)
    ShDestruct(compiler_);
}

void ShaderTranslator::ClearResults() {
  translated_shader_.reset();
  info_log_.reset();
  attrib_map_.clear();
  uniform_map_.clear();
  varying_map_.clear();
  name_map_.clear();
}

}  // namespace gles2
}  // namespace gpu