2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
8 // Implement the top-level of interface to the compiler,
9 // as defined in ShaderLang.h
12 #include "GLSLANG/ShaderLang.h"
14 #include "compiler/InitializeDll.h"
15 #include "compiler/ShHandle.h"
18 // This is the platform independent interface between an OGL driver
19 // and the shading language compiler.
22 static int getVariableMaxLength(const TVariableInfoList& varList)
24 TString::size_type maxLen = 0;
25 for (TVariableInfoList::const_iterator i = varList.begin();
26 i != varList.end(); ++i)
28 maxLen = std::max(maxLen, i->name.size());
30 // Add 1 to include null-termination character.
31 return static_cast<int>(maxLen) + 1;
34 static void getVariableInfo(ShShaderInfo varType,
35 const ShHandle handle,
43 if (!handle || !size || !type || !name)
45 ASSERT((varType == SH_ACTIVE_ATTRIBUTES) ||
46 (varType == SH_ACTIVE_UNIFORMS));
48 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
49 TCompiler* compiler = base->getAsCompiler();
53 const TVariableInfoList& varList = varType == SH_ACTIVE_ATTRIBUTES ?
54 compiler->getAttribs() : compiler->getUniforms();
55 if (index < 0 || index >= static_cast<int>(varList.size()))
58 const TVariableInfo& varInfo = varList[index];
59 if (length) *length = varInfo.name.size();
62 strcpy(name, varInfo.name.c_str());
64 strcpy(mappedName, varInfo.mappedName.c_str());
68 // Driver must call this first, once, before doing any other
69 // compiler operations.
80 // Cleanup symbol tables
91 // Initialize built-in resources with minimum expected values.
93 void ShInitBuiltInResources(ShBuiltInResources* resources)
96 resources->MaxVertexAttribs = 8;
97 resources->MaxVertexUniformVectors = 128;
98 resources->MaxVaryingVectors = 8;
99 resources->MaxVertexTextureImageUnits = 0;
100 resources->MaxCombinedTextureImageUnits = 8;
101 resources->MaxTextureImageUnits = 8;
102 resources->MaxFragmentUniformVectors = 16;
103 resources->MaxDrawBuffers = 1;
106 resources->OES_standard_derivatives = 0;
110 // Driver calls these to create and destroy compiler objects.
112 ShHandle ShConstructCompiler(ShShaderType type, ShShaderSpec spec,
113 ShShaderOutput output,
114 const ShBuiltInResources* resources)
119 TShHandleBase* base = static_cast<TShHandleBase*>(ConstructCompiler(type, spec, output));
120 TCompiler* compiler = base->getAsCompiler();
124 // Generate built-in symbol table.
125 if (!compiler->Init(*resources)) {
130 return reinterpret_cast<void*>(base);
133 void ShDestruct(ShHandle handle)
138 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
140 if (base->getAsCompiler())
141 DeleteCompiler(base->getAsCompiler());
145 // Do an actual compile on the given strings. The result is left
146 // in the given compile object.
148 // Return: The return value of ShCompile is really boolean, indicating
149 // success or failure.
152 const ShHandle handle,
153 const char* const shaderStrings[],
154 const int numStrings,
163 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
164 TCompiler* compiler = base->getAsCompiler();
168 bool success = compiler->compile(shaderStrings, numStrings, compileOptions);
169 return success ? 1 : 0;
172 void ShGetInfo(const ShHandle handle, ShShaderInfo pname, int* params)
174 if (!handle || !params)
177 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
178 TCompiler* compiler = base->getAsCompiler();
179 if (!compiler) return;
183 case SH_INFO_LOG_LENGTH:
184 *params = compiler->getInfoSink().info.size() + 1;
186 case SH_OBJECT_CODE_LENGTH:
187 *params = compiler->getInfoSink().obj.size() + 1;
189 case SH_ACTIVE_UNIFORMS:
190 *params = compiler->getUniforms().size();
192 case SH_ACTIVE_UNIFORM_MAX_LENGTH:
193 *params = getVariableMaxLength(compiler->getUniforms());
195 case SH_ACTIVE_ATTRIBUTES:
196 *params = compiler->getAttribs().size();
198 case SH_ACTIVE_ATTRIBUTE_MAX_LENGTH:
199 *params = getVariableMaxLength(compiler->getAttribs());
201 case SH_MAPPED_NAME_MAX_LENGTH:
202 *params = compiler->getMappedNameMaxLength();
204 default: UNREACHABLE();
209 // Return any compiler log of messages for the application.
211 void ShGetInfoLog(const ShHandle handle, char* infoLog)
213 if (!handle || !infoLog)
216 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
217 TCompiler* compiler = base->getAsCompiler();
218 if (!compiler) return;
220 TInfoSink& infoSink = compiler->getInfoSink();
221 strcpy(infoLog, infoSink.info.c_str());
225 // Return any object code.
227 void ShGetObjectCode(const ShHandle handle, char* objCode)
229 if (!handle || !objCode)
232 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
233 TCompiler* compiler = base->getAsCompiler();
234 if (!compiler) return;
236 TInfoSink& infoSink = compiler->getInfoSink();
237 strcpy(objCode, infoSink.obj.c_str());
240 void ShGetActiveAttrib(const ShHandle handle,
248 getVariableInfo(SH_ACTIVE_ATTRIBUTES,
249 handle, index, length, size, type, name, mappedName);
252 void ShGetActiveUniform(const ShHandle handle,
260 getVariableInfo(SH_ACTIVE_UNIFORMS,
261 handle, index, length, size, type, name, mappedName);