1 /*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ShaderParser.h"
18
19 #include "GLcommon/GLutils.h"
20
21 #include <memory>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <string>
25 #include <vector>
26
ShaderParser(GLenum type,bool coreProfile)27 ShaderParser::ShaderParser(GLenum type, bool coreProfile) : ObjectData(SHADER_DATA), m_type(type), m_coreProfile(coreProfile) {}
28
getGenNameInfo() const29 GenNameInfo ShaderParser::getGenNameInfo() const {
30 switch (m_type) {
31 case GL_VERTEX_SHADER:
32 return GenNameInfo(ShaderProgramType::VERTEX_SHADER);
33 case GL_FRAGMENT_SHADER:
34 return GenNameInfo(ShaderProgramType::FRAGMENT_SHADER);
35 case GL_COMPUTE_SHADER:
36 return GenNameInfo(ShaderProgramType::COMPUTE_SHADER);
37 default:
38 assert(0);
39 return GenNameInfo(NamedObjectType::SHADER_OR_PROGRAM);
40 }
41 }
42
ShaderParser(android::base::Stream * stream)43 ShaderParser::ShaderParser(android::base::Stream* stream) : ObjectData(stream) {
44 m_originalSrc = stream->getString();
45 m_src = stream->getString();
46 m_parsedSrc = stream->getString();
47 m_parsedLines = (GLchar*)m_parsedSrc.c_str();
48 m_compiledSrc = stream->getString();
49 m_infoLog = stream->getString();
50 size_t programSize = stream->getBe32();
51 for (size_t program = 0; program < programSize; program++) {
52 m_programs.insert(stream->getBe32());
53 }
54 m_type = stream->getBe32();
55 m_compileStatus = stream->getByte();
56 m_deleteStatus = stream->getByte();
57 m_valid = stream->getByte();
58 m_coreProfile = stream->getByte();
59 }
60
onSave(android::base::Stream * stream,unsigned int globalName) const61 void ShaderParser::onSave(android::base::Stream* stream, unsigned int globalName) const {
62 // The first byte is used to distinguish between program and shader object.
63 // It will be loaded outside of this class
64 stream->putByte(LOAD_SHADER);
65 ObjectData::onSave(stream, globalName);
66 stream->putString(m_originalSrc);
67 stream->putString(m_src);
68 stream->putString(m_parsedSrc);
69 // do not snapshot m_parsedLines
70 stream->putString(m_compiledSrc);
71 stream->putString(m_infoLog);
72 stream->putBe32(m_programs.size());
73 for (const auto& program : m_programs) {
74 stream->putBe32(program);
75 }
76 stream->putBe32(m_type);
77 stream->putByte(m_compileStatus);
78 stream->putByte(m_deleteStatus);
79 stream->putByte(m_valid);
80 stream->putByte(m_coreProfile);
81 }
82
restore(ObjectLocalName localName,const getGlobalName_t & getGlobalName)83 void ShaderParser::restore(ObjectLocalName localName,
84 const getGlobalName_t& getGlobalName) {
85 ObjectData::restore(localName, getGlobalName);
86 if (m_parsedSrc.empty()) return;
87 int globalName = getGlobalName(NamedObjectType::SHADER_OR_PROGRAM,
88 localName);
89 if (isGles2Gles()) {
90 const char* src = getOriginalSrc().c_str();
91 GLEScontext::dispatcher().glShaderSource(globalName, 1, &src, NULL);
92 } else {
93 GLEScontext::dispatcher().glShaderSource(globalName, 1, parsedLines(), NULL);
94 }
95 if (m_compileStatus)
96 GLEScontext::dispatcher().glCompileShader(globalName);
97 }
98
99 static const char kAlwaysInvalidShader[] =
100 "When shader translation fails, passing an empty string to underlying GL "
101 "may result in GL_COMPILE_STATUS == GL_TRUE and letting us link an "
102 "invalid shader program, due to tripping the underlying GL's separable "
103 "shader program capability."
104 "This shader is meant to make the underlying GL agree that the shader is"
105 "indeed invalid when translation fails.";
106
convertESSLToGLSL()107 void ShaderParser::convertESSLToGLSL() {
108 if (isGles2Gles()) {
109 m_parsedSrc = m_originalSrc;
110 return;
111 }
112
113 std::string translationLog;
114 std::string parsedSource;
115
116 #if USE_ANGLE_SHADER_PARSER
117 m_valid =
118 ANGLEShaderParser::translate(
119 m_coreProfile,
120 m_originalSrc.c_str(),
121 m_type,
122 &translationLog,
123 &parsedSource,
124 &m_shaderLinkInfo);
125 #else
126 m_valid = false;
127 #endif
128
129 if (!m_valid) {
130 m_infoLog = static_cast<const GLchar*>(translationLog.c_str());
131 m_parsedSrc = kAlwaysInvalidShader;
132 } else {
133 m_parsedSrc = parsedSource;
134 }
135 }
136
setSrc(GLsizei count,const GLchar * const * strings,const GLint * length)137 void ShaderParser::setSrc(GLsizei count, const GLchar* const* strings, const GLint* length){
138 m_src.clear();
139 for(int i = 0;i<count;i++){
140 const size_t strLen =
141 (length && length[i] >= 0) ? length[i] : strlen(strings[i]);
142 m_src.append(strings[i], strings[i] + strLen);
143 }
144 // Store original source as some 'parsing' functions actually modify m_src.
145 // Note: assign() call is a workaround for the reference-counting
146 // std::string in GCC's STL - we need a deep copy here.
147 m_originalSrc.assign(m_src.c_str(), m_src.size());
148
149 convertESSLToGLSL();
150 }
151
parsedLines()152 const GLchar** ShaderParser::parsedLines() {
153 m_parsedLines = (GLchar*)m_parsedSrc.c_str();
154 return const_cast<const GLchar**> (&m_parsedLines);
155 }
156
clear()157 void ShaderParser::clear() {
158 m_parsedLines = nullptr;
159 std::string().swap(m_parsedSrc);
160 std::string().swap(m_src);
161 }
162
getOriginalSrc() const163 const std::string& ShaderParser::getOriginalSrc() const {
164 return m_originalSrc;
165 }
166
getType()167 GLenum ShaderParser::getType() {
168 return m_type;
169 }
170
setInfoLog(GLchar * infoLog)171 void ShaderParser::setInfoLog(GLchar* infoLog) {
172 assert(infoLog);
173 std::unique_ptr<GLchar[]> infoLogDeleter(infoLog);
174 m_infoLog.assign(infoLog);
175 }
176
validShader() const177 bool ShaderParser::validShader() const {
178 return m_valid;
179 }
180
181 static const GLchar glsles_invalid[] =
182 { "ERROR: Valid GLSL but not GLSL ES" };
183
setInvalidInfoLog()184 void ShaderParser::setInvalidInfoLog() {
185 m_infoLog = glsles_invalid;
186 }
187
getInfoLog() const188 const GLchar* ShaderParser::getInfoLog() const {
189 return m_infoLog.c_str();
190 }
191
setCompileStatus(bool val)192 void ShaderParser::setCompileStatus(bool val) {
193 m_compileStatus = val;
194 m_compiledSrc = m_parsedSrc.c_str();
195 }
196
attachProgram(GLuint program)197 void ShaderParser::attachProgram(GLuint program) {
198 android::base::AutoLock lock(m_programsLock);
199 m_programs.insert(program);
200 }
201
detachProgram(GLuint program)202 void ShaderParser::detachProgram(GLuint program) {
203 android::base::AutoLock lock(m_programsLock);
204 m_programs.erase(program);
205 }
206
hasAttachedPrograms() const207 bool ShaderParser::hasAttachedPrograms() const {
208 android::base::AutoLock lock(m_programsLock);
209 return m_programs.size() > 0;
210 }
211