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
|
// Copyright (c) 2011 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 "base/at_exit.h"
#include "base/logging.h"
namespace {
void FinalizeShaderTranslator(void* /* dummy */) {
ShFinalize();
}
bool InitializeShaderTranslator() {
static bool initialized = false;
if (!initialized && ShInitialize()) {
base::AtExitManager::RegisterCallback(&FinalizeShaderTranslator, NULL);
initialized = true;
}
return initialized;
}
using gpu::gles2::ShaderTranslator;
void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type,
ShaderTranslator::VariableMap* var_map) {
int 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;
default: NOTREACHED();
}
ShGetInfo(compiler, SH_MAPPED_NAME_MAX_LENGTH, &mapped_name_len);
if (name_len <= 1 || mapped_name_len <= 1) return;
scoped_array<char> name(new char[name_len]);
scoped_array<char> mapped_name(new char[mapped_name_len]);
int num_vars = 0;
ShGetInfo(compiler, var_type, &num_vars);
for (int i = 0; i < num_vars; ++i) {
int size = 0;
ShDataType type = SH_NONE;
switch (var_type) {
case SH_ACTIVE_ATTRIBUTES:
ShGetActiveAttrib(
compiler, i, NULL, &size, &type, name.get(), mapped_name.get());
break;
case SH_ACTIVE_UNIFORMS:
ShGetActiveUniform(
compiler, i, NULL, &size, &type, name.get(), mapped_name.get());
break;
default: NOTREACHED();
}
ShaderTranslator::VariableInfo info(type, size, name.get());
(*var_map)[mapped_name.get()] = info;
}
}
} // namespace
namespace gpu {
namespace gles2 {
ShaderTranslator::ShaderTranslator()
: compiler_(NULL),
implementation_is_glsl_es_(false) {
}
ShaderTranslator::~ShaderTranslator() {
if (compiler_ != NULL)
ShDestruct(compiler_);
}
bool ShaderTranslator::Init(ShShaderType shader_type,
ShShaderSpec shader_spec,
const ShBuiltInResources* resources,
bool implementation_is_glsl_es) {
// 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;
compiler_ = ShConstructCompiler(shader_type, shader_spec, resources);
implementation_is_glsl_es_ = implementation_is_glsl_es;
return compiler_ != NULL;
}
bool ShaderTranslator::Translate(const char* shader) {
// Make sure this instance is initialized.
DCHECK(compiler_ != NULL);
DCHECK(shader != NULL);
ClearResults();
bool success = false;
int compile_options =
SH_OBJECT_CODE | SH_ATTRIBUTES_UNIFORMS | SH_MAP_LONG_VARIABLE_NAMES;
if (ShCompile(compiler_, &shader, 1, compile_options)) {
success = true;
if (!implementation_is_glsl_es_) {
// Get translated shader.
int 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());
}
} else {
// Pass down the original shader's source rather than the
// compiler's output. TODO(kbr): once the shader compiler has a
// GLSL ES backend, use its output.
int shader_code_len = 1 + strlen(shader);
if (shader_code_len > 1) {
translated_shader_.reset(new char[shader_code_len]);
strncpy(translated_shader_.get(), shader, shader_code_len);
}
}
// Get info for attribs and uniforms.
GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_);
GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_);
}
// Get info log.
int 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;
}
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_;
}
void ShaderTranslator::ClearResults() {
translated_shader_.reset();
info_log_.reset();
attrib_map_.clear();
uniform_map_.clear();
}
} // namespace gles2
} // namespace gpu
|