blob: a4a8f6c9754041ad471ddb845c11a6e9677925e0 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com043da132012-12-20 21:12:22 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
alokp@chromium.org774d7062010-07-21 18:55:45 +00008// Implement the top-level of interface to the compiler,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009// as defined in ShaderLang.h
10//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000012#include "GLSLANG/ShaderLang.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014#include "compiler/InitializeDll.h"
kbr@chromium.org22152112011-10-26 01:18:28 +000015#include "compiler/preprocessor/length_limits.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000016#include "compiler/ShHandle.h"
daniel@transgaming.com043da132012-12-20 21:12:22 +000017#include "compiler/TranslatorHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019//
20// This is the platform independent interface between an OGL driver
alokp@chromium.org774d7062010-07-21 18:55:45 +000021// and the shading language compiler.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022//
23
kbr@chromium.org22152112011-10-26 01:18:28 +000024static bool checkActiveUniformAndAttribMaxLengths(const ShHandle handle,
25 int expectedValue)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000026{
kbr@chromium.org22152112011-10-26 01:18:28 +000027 int activeUniformLimit = 0;
28 ShGetInfo(handle, SH_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformLimit);
29 int activeAttribLimit = 0;
30 ShGetInfo(handle, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttribLimit);
31 return (expectedValue == activeUniformLimit && expectedValue == activeAttribLimit);
32}
33
34static bool checkMappedNameMaxLength(const ShHandle handle, int expectedValue)
35{
36 int mappedNameMaxLength = 0;
37 ShGetInfo(handle, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameMaxLength);
38 return (expectedValue == mappedNameMaxLength);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000039}
40
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000041static void getVariableInfo(ShShaderInfo varType,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000042 const ShHandle handle,
43 int index,
44 int* length,
45 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000046 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +000047 char* name,
48 char* mappedName)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000049{
50 if (!handle || !size || !type || !name)
51 return;
52 ASSERT((varType == SH_ACTIVE_ATTRIBUTES) ||
53 (varType == SH_ACTIVE_UNIFORMS));
54
55 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
56 TCompiler* compiler = base->getAsCompiler();
57 if (compiler == 0)
58 return;
59
60 const TVariableInfoList& varList = varType == SH_ACTIVE_ATTRIBUTES ?
61 compiler->getAttribs() : compiler->getUniforms();
62 if (index < 0 || index >= static_cast<int>(varList.size()))
63 return;
64
65 const TVariableInfo& varInfo = varList[index];
66 if (length) *length = varInfo.name.size();
67 *size = varInfo.size;
68 *type = varInfo.type;
kbr@chromium.org22152112011-10-26 01:18:28 +000069
70 // This size must match that queried by
71 // SH_ACTIVE_UNIFORM_MAX_LENGTH and SH_ACTIVE_ATTRIBUTE_MAX_LENGTH
72 // in ShGetInfo, below.
73 int activeUniformAndAttribLength = 1 + MAX_SYMBOL_NAME_LEN;
74 ASSERT(checkActiveUniformAndAttribMaxLengths(handle, activeUniformAndAttribLength));
75 strncpy(name, varInfo.name.c_str(), activeUniformAndAttribLength);
jacob.benoit.1@gmail.comb45306b2012-05-07 02:12:34 +000076 name[activeUniformAndAttribLength - 1] = 0;
kbr@chromium.org22152112011-10-26 01:18:28 +000077 if (mappedName) {
78 // This size must match that queried by
79 // SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below.
80 int maxMappedNameLength = 1 + MAX_SYMBOL_NAME_LEN;
81 ASSERT(checkMappedNameMaxLength(handle, maxMappedNameLength));
82 strncpy(mappedName, varInfo.mappedName.c_str(), maxMappedNameLength);
jacob.benoit.1@gmail.comb45306b2012-05-07 02:12:34 +000083 mappedName[maxMappedNameLength - 1] = 0;
kbr@chromium.org22152112011-10-26 01:18:28 +000084 }
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000085}
86
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087//
88// Driver must call this first, once, before doing any other
alokp@chromium.org774d7062010-07-21 18:55:45 +000089// compiler operations.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090//
91int ShInitialize()
92{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000093 if (!InitProcess())
94 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000096 return 1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097}
98
99//
alokp@chromium.org94a86ad2010-08-25 20:02:11 +0000100// Cleanup symbol tables
101//
102int ShFinalize()
103{
104 if (!DetachProcess())
105 return 0;
106
107 return 1;
108}
109
110//
111// Initialize built-in resources with minimum expected values.
112//
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000113void ShInitBuiltInResources(ShBuiltInResources* resources)
alokp@chromium.org94a86ad2010-08-25 20:02:11 +0000114{
115 // Constants.
116 resources->MaxVertexAttribs = 8;
117 resources->MaxVertexUniformVectors = 128;
118 resources->MaxVaryingVectors = 8;
119 resources->MaxVertexTextureImageUnits = 0;
120 resources->MaxCombinedTextureImageUnits = 8;
121 resources->MaxTextureImageUnits = 8;
122 resources->MaxFragmentUniformVectors = 16;
123 resources->MaxDrawBuffers = 1;
124
125 // Extensions.
126 resources->OES_standard_derivatives = 0;
zmo@google.com09c323a2011-08-12 18:22:25 +0000127 resources->OES_EGL_image_external = 0;
kbr@chromium.org205fef32011-11-22 20:50:02 +0000128 resources->ARB_texture_rectangle = 0;
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000129
130 // Disable name hashing by default.
131 resources->HashFunction = NULL;
alokp@chromium.org94a86ad2010-08-25 20:02:11 +0000132}
133
134//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000135// Driver calls these to create and destroy compiler objects.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136//
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000137ShHandle ShConstructCompiler(ShShaderType type, ShShaderSpec spec,
zmo@google.com5601ea02011-06-10 18:23:25 +0000138 ShShaderOutput output,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000139 const ShBuiltInResources* resources)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140{
141 if (!InitThread())
142 return 0;
143
zmo@google.com5601ea02011-06-10 18:23:25 +0000144 TShHandleBase* base = static_cast<TShHandleBase*>(ConstructCompiler(type, spec, output));
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000145 TCompiler* compiler = base->getAsCompiler();
146 if (compiler == 0)
147 return 0;
148
149 // Generate built-in symbol table.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000150 if (!compiler->Init(*resources)) {
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000151 ShDestruct(base);
152 return 0;
153 }
154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155 return reinterpret_cast<void*>(base);
156}
157
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158void ShDestruct(ShHandle handle)
159{
160 if (handle == 0)
161 return;
162
163 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
164
165 if (base->getAsCompiler())
166 DeleteCompiler(base->getAsCompiler());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170// Do an actual compile on the given strings. The result is left
171// in the given compile object.
172//
173// Return: The return value of ShCompile is really boolean, indicating
174// success or failure.
175//
176int ShCompile(
177 const ShHandle handle,
178 const char* const shaderStrings[],
179 const int numStrings,
alokp@chromium.org7beea402010-09-15 21:18:34 +0000180 int compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
182 if (!InitThread())
183 return 0;
184
185 if (handle == 0)
186 return 0;
187
188 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
189 TCompiler* compiler = base->getAsCompiler();
190 if (compiler == 0)
191 return 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192
alokp@chromium.org07620a52010-09-23 17:53:56 +0000193 bool success = compiler->compile(shaderStrings, numStrings, compileOptions);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194 return success ? 1 : 0;
195}
196
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000197void ShGetInfo(const ShHandle handle, ShShaderInfo pname, int* params)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000198{
199 if (!handle || !params)
200 return;
201
202 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
203 TCompiler* compiler = base->getAsCompiler();
204 if (!compiler) return;
205
206 switch(pname)
207 {
208 case SH_INFO_LOG_LENGTH:
209 *params = compiler->getInfoSink().info.size() + 1;
210 break;
211 case SH_OBJECT_CODE_LENGTH:
212 *params = compiler->getInfoSink().obj.size() + 1;
213 break;
214 case SH_ACTIVE_UNIFORMS:
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000215 *params = compiler->getUniforms().size();
alokp@chromium.org7beea402010-09-15 21:18:34 +0000216 break;
217 case SH_ACTIVE_UNIFORM_MAX_LENGTH:
kbr@chromium.org22152112011-10-26 01:18:28 +0000218 *params = 1 + MAX_SYMBOL_NAME_LEN;
alokp@chromium.org7beea402010-09-15 21:18:34 +0000219 break;
220 case SH_ACTIVE_ATTRIBUTES:
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000221 *params = compiler->getAttribs().size();
alokp@chromium.org7beea402010-09-15 21:18:34 +0000222 break;
223 case SH_ACTIVE_ATTRIBUTE_MAX_LENGTH:
kbr@chromium.org22152112011-10-26 01:18:28 +0000224 *params = 1 + MAX_SYMBOL_NAME_LEN;
alokp@chromium.org7beea402010-09-15 21:18:34 +0000225 break;
zmo@google.comfd747b82011-04-23 01:30:07 +0000226 case SH_MAPPED_NAME_MAX_LENGTH:
kbr@chromium.org22152112011-10-26 01:18:28 +0000227 // Use longer length than MAX_SHORTENED_IDENTIFIER_SIZE to
228 // handle array and struct dereferences.
229 *params = 1 + MAX_SYMBOL_NAME_LEN;
zmo@google.comfd747b82011-04-23 01:30:07 +0000230 break;
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000231 case SH_NAME_MAX_LENGTH:
232 *params = 1 + MAX_SYMBOL_NAME_LEN;
233 break;
234 case SH_HASHED_NAME_MAX_LENGTH:
235 if (compiler->getHashFunction() == NULL) {
236 *params = 0;
237 } else {
238 // 64 bits hashing output requires 16 bytes for hex
239 // representation.
240 const char HashedNamePrefix[] = HASHED_NAME_PREFIX;
241 *params = 16 + sizeof(HashedNamePrefix);
242 }
243 break;
244 case SH_HASHED_NAMES_COUNT:
245 *params = compiler->getNameMap().size();
246 break;
alokp@chromium.org7beea402010-09-15 21:18:34 +0000247 default: UNREACHABLE();
248 }
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000252// Return any compiler log of messages for the application.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253//
alokp@chromium.org7beea402010-09-15 21:18:34 +0000254void ShGetInfoLog(const ShHandle handle, char* infoLog)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000256 if (!handle || !infoLog)
257 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
259 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000260 TCompiler* compiler = base->getAsCompiler();
261 if (!compiler) return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
alokp@chromium.org7beea402010-09-15 21:18:34 +0000263 TInfoSink& infoSink = compiler->getInfoSink();
264 strcpy(infoLog, infoSink.info.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265}
266
267//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000268// Return any object code.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269//
alokp@chromium.org7beea402010-09-15 21:18:34 +0000270void ShGetObjectCode(const ShHandle handle, char* objCode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000272 if (!handle || !objCode)
273 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274
275 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000276 TCompiler* compiler = base->getAsCompiler();
277 if (!compiler) return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
alokp@chromium.org7beea402010-09-15 21:18:34 +0000279 TInfoSink& infoSink = compiler->getInfoSink();
280 strcpy(objCode, infoSink.obj.c_str());
281}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282
alokp@chromium.org7beea402010-09-15 21:18:34 +0000283void ShGetActiveAttrib(const ShHandle handle,
284 int index,
285 int* length,
286 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000287 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +0000288 char* name,
289 char* mappedName)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000290{
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000291 getVariableInfo(SH_ACTIVE_ATTRIBUTES,
zmo@google.comfd747b82011-04-23 01:30:07 +0000292 handle, index, length, size, type, name, mappedName);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000293}
294
295void ShGetActiveUniform(const ShHandle handle,
296 int index,
297 int* length,
298 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000299 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +0000300 char* name,
301 char* mappedName)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000302{
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000303 getVariableInfo(SH_ACTIVE_UNIFORMS,
zmo@google.comfd747b82011-04-23 01:30:07 +0000304 handle, index, length, size, type, name, mappedName);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305}
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000306
307void ShGetNameHashingEntry(const ShHandle handle,
308 int index,
309 char* name,
310 char* hashedName)
311{
312 if (!handle || !name || !hashedName || index < 0)
313 return;
314
315 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
316 TCompiler* compiler = base->getAsCompiler();
317 if (!compiler) return;
318
319 const NameMap& nameMap = compiler->getNameMap();
320 if (index >= static_cast<int>(nameMap.size()))
321 return;
322
323 NameMap::const_iterator it = nameMap.begin();
324 for (int i = 0; i < index; ++i)
325 ++it;
326
327 size_t len = it->first.length() + 1;
328 int max_len = 0;
329 ShGetInfo(handle, SH_NAME_MAX_LENGTH, &max_len);
330 if (static_cast<int>(len) > max_len) {
331 ASSERT(false);
332 len = max_len;
333 }
334 strncpy(name, it->first.c_str(), len);
335 // To be on the safe side in case the source is longer than expected.
336 name[len] = '\0';
337
338 len = it->second.length() + 1;
339 max_len = 0;
340 ShGetInfo(handle, SH_HASHED_NAME_MAX_LENGTH, &max_len);
341 if (static_cast<int>(len) > max_len) {
342 ASSERT(false);
343 len = max_len;
344 }
345 strncpy(hashedName, it->second.c_str(), len);
346 // To be on the safe side in case the source is longer than expected.
347 hashedName[len] = '\0';
348}
daniel@transgaming.com043da132012-12-20 21:12:22 +0000349
350void ShGetInfoPointer(const ShHandle handle, ShShaderInfo pname, void** params)
351{
352 if (!handle || !params)
353 return;
354
355 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
356 TranslatorHLSL* translator = base->getAsTranslatorHLSL();
357 if (!translator) return;
358
359 switch(pname)
360 {
361 case SH_ACTIVE_UNIFORMS_ARRAY:
362 *params = (void*)&translator->getUniforms();
363 break;
364 default: UNREACHABLE();
365 }
366}