blob: 13f11b5ca92e87bbda502b428e47800862fe9ef9 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
kbr@chromium.org22152112011-10-26 01:18:28 +00002// Copyright (c) 2002-2011 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.com4f39fd92010-03-08 20:26:45 +000017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018//
19// This is the platform independent interface between an OGL driver
alokp@chromium.org774d7062010-07-21 18:55:45 +000020// and the shading language compiler.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021//
22
kbr@chromium.org22152112011-10-26 01:18:28 +000023static bool checkActiveUniformAndAttribMaxLengths(const ShHandle handle,
24 int expectedValue)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000025{
kbr@chromium.org22152112011-10-26 01:18:28 +000026 int activeUniformLimit = 0;
27 ShGetInfo(handle, SH_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformLimit);
28 int activeAttribLimit = 0;
29 ShGetInfo(handle, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttribLimit);
30 return (expectedValue == activeUniformLimit && expectedValue == activeAttribLimit);
31}
32
33static bool checkMappedNameMaxLength(const ShHandle handle, int expectedValue)
34{
35 int mappedNameMaxLength = 0;
36 ShGetInfo(handle, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameMaxLength);
37 return (expectedValue == mappedNameMaxLength);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000038}
39
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000040static void getVariableInfo(ShShaderInfo varType,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000041 const ShHandle handle,
42 int index,
43 int* length,
44 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000045 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +000046 char* name,
47 char* mappedName)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000048{
49 if (!handle || !size || !type || !name)
50 return;
51 ASSERT((varType == SH_ACTIVE_ATTRIBUTES) ||
52 (varType == SH_ACTIVE_UNIFORMS));
53
54 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
55 TCompiler* compiler = base->getAsCompiler();
56 if (compiler == 0)
57 return;
58
59 const TVariableInfoList& varList = varType == SH_ACTIVE_ATTRIBUTES ?
60 compiler->getAttribs() : compiler->getUniforms();
61 if (index < 0 || index >= static_cast<int>(varList.size()))
62 return;
63
64 const TVariableInfo& varInfo = varList[index];
65 if (length) *length = varInfo.name.size();
66 *size = varInfo.size;
67 *type = varInfo.type;
kbr@chromium.org22152112011-10-26 01:18:28 +000068
69 // This size must match that queried by
70 // SH_ACTIVE_UNIFORM_MAX_LENGTH and SH_ACTIVE_ATTRIBUTE_MAX_LENGTH
71 // in ShGetInfo, below.
72 int activeUniformAndAttribLength = 1 + MAX_SYMBOL_NAME_LEN;
73 ASSERT(checkActiveUniformAndAttribMaxLengths(handle, activeUniformAndAttribLength));
74 strncpy(name, varInfo.name.c_str(), activeUniformAndAttribLength);
75 if (mappedName) {
76 // This size must match that queried by
77 // SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below.
78 int maxMappedNameLength = 1 + MAX_SYMBOL_NAME_LEN;
79 ASSERT(checkMappedNameMaxLength(handle, maxMappedNameLength));
80 strncpy(mappedName, varInfo.mappedName.c_str(), maxMappedNameLength);
81 }
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000082}
83
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084//
85// Driver must call this first, once, before doing any other
alokp@chromium.org774d7062010-07-21 18:55:45 +000086// compiler operations.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087//
88int ShInitialize()
89{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000090 if (!InitProcess())
91 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000093 return 1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094}
95
96//
alokp@chromium.org94a86ad2010-08-25 20:02:11 +000097// Cleanup symbol tables
98//
99int ShFinalize()
100{
101 if (!DetachProcess())
102 return 0;
103
104 return 1;
105}
106
107//
108// Initialize built-in resources with minimum expected values.
109//
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000110void ShInitBuiltInResources(ShBuiltInResources* resources)
alokp@chromium.org94a86ad2010-08-25 20:02:11 +0000111{
112 // Constants.
113 resources->MaxVertexAttribs = 8;
114 resources->MaxVertexUniformVectors = 128;
115 resources->MaxVaryingVectors = 8;
116 resources->MaxVertexTextureImageUnits = 0;
117 resources->MaxCombinedTextureImageUnits = 8;
118 resources->MaxTextureImageUnits = 8;
119 resources->MaxFragmentUniformVectors = 16;
120 resources->MaxDrawBuffers = 1;
121
122 // Extensions.
123 resources->OES_standard_derivatives = 0;
zmo@google.com09c323a2011-08-12 18:22:25 +0000124 resources->OES_EGL_image_external = 0;
kbr@chromium.org205fef32011-11-22 20:50:02 +0000125 resources->ARB_texture_rectangle = 0;
alokp@chromium.org94a86ad2010-08-25 20:02:11 +0000126}
127
128//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000129// Driver calls these to create and destroy compiler objects.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130//
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000131ShHandle ShConstructCompiler(ShShaderType type, ShShaderSpec spec,
zmo@google.com5601ea02011-06-10 18:23:25 +0000132 ShShaderOutput output,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000133 const ShBuiltInResources* resources)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134{
135 if (!InitThread())
136 return 0;
137
zmo@google.com5601ea02011-06-10 18:23:25 +0000138 TShHandleBase* base = static_cast<TShHandleBase*>(ConstructCompiler(type, spec, output));
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000139 TCompiler* compiler = base->getAsCompiler();
140 if (compiler == 0)
141 return 0;
142
143 // Generate built-in symbol table.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000144 if (!compiler->Init(*resources)) {
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000145 ShDestruct(base);
146 return 0;
147 }
148
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149 return reinterpret_cast<void*>(base);
150}
151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152void ShDestruct(ShHandle handle)
153{
154 if (handle == 0)
155 return;
156
157 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
158
159 if (base->getAsCompiler())
160 DeleteCompiler(base->getAsCompiler());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161}
162
163//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164// Do an actual compile on the given strings. The result is left
165// in the given compile object.
166//
167// Return: The return value of ShCompile is really boolean, indicating
168// success or failure.
169//
170int ShCompile(
171 const ShHandle handle,
172 const char* const shaderStrings[],
173 const int numStrings,
alokp@chromium.org7beea402010-09-15 21:18:34 +0000174 int compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175{
176 if (!InitThread())
177 return 0;
178
179 if (handle == 0)
180 return 0;
181
182 TShHandleBase* base = reinterpret_cast<TShHandleBase*>(handle);
183 TCompiler* compiler = base->getAsCompiler();
184 if (compiler == 0)
185 return 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186
alokp@chromium.org07620a52010-09-23 17:53:56 +0000187 bool success = compiler->compile(shaderStrings, numStrings, compileOptions);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188 return success ? 1 : 0;
189}
190
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000191void ShGetInfo(const ShHandle handle, ShShaderInfo pname, int* params)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000192{
193 if (!handle || !params)
194 return;
195
196 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
197 TCompiler* compiler = base->getAsCompiler();
198 if (!compiler) return;
199
200 switch(pname)
201 {
202 case SH_INFO_LOG_LENGTH:
203 *params = compiler->getInfoSink().info.size() + 1;
204 break;
205 case SH_OBJECT_CODE_LENGTH:
206 *params = compiler->getInfoSink().obj.size() + 1;
207 break;
208 case SH_ACTIVE_UNIFORMS:
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000209 *params = compiler->getUniforms().size();
alokp@chromium.org7beea402010-09-15 21:18:34 +0000210 break;
211 case SH_ACTIVE_UNIFORM_MAX_LENGTH:
kbr@chromium.org22152112011-10-26 01:18:28 +0000212 *params = 1 + MAX_SYMBOL_NAME_LEN;
alokp@chromium.org7beea402010-09-15 21:18:34 +0000213 break;
214 case SH_ACTIVE_ATTRIBUTES:
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000215 *params = compiler->getAttribs().size();
alokp@chromium.org7beea402010-09-15 21:18:34 +0000216 break;
217 case SH_ACTIVE_ATTRIBUTE_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;
zmo@google.comfd747b82011-04-23 01:30:07 +0000220 case SH_MAPPED_NAME_MAX_LENGTH:
kbr@chromium.org22152112011-10-26 01:18:28 +0000221 // Use longer length than MAX_SHORTENED_IDENTIFIER_SIZE to
222 // handle array and struct dereferences.
223 *params = 1 + MAX_SYMBOL_NAME_LEN;
zmo@google.comfd747b82011-04-23 01:30:07 +0000224 break;
alokp@chromium.org7beea402010-09-15 21:18:34 +0000225 default: UNREACHABLE();
226 }
227}
228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000230// Return any compiler log of messages for the application.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231//
alokp@chromium.org7beea402010-09-15 21:18:34 +0000232void ShGetInfoLog(const ShHandle handle, char* infoLog)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000234 if (!handle || !infoLog)
235 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236
237 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000238 TCompiler* compiler = base->getAsCompiler();
239 if (!compiler) return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
alokp@chromium.org7beea402010-09-15 21:18:34 +0000241 TInfoSink& infoSink = compiler->getInfoSink();
242 strcpy(infoLog, infoSink.info.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243}
244
245//
alokp@chromium.org774d7062010-07-21 18:55:45 +0000246// Return any object code.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247//
alokp@chromium.org7beea402010-09-15 21:18:34 +0000248void ShGetObjectCode(const ShHandle handle, char* objCode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000250 if (!handle || !objCode)
251 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
253 TShHandleBase* base = static_cast<TShHandleBase*>(handle);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000254 TCompiler* compiler = base->getAsCompiler();
255 if (!compiler) return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
alokp@chromium.org7beea402010-09-15 21:18:34 +0000257 TInfoSink& infoSink = compiler->getInfoSink();
258 strcpy(objCode, infoSink.obj.c_str());
259}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260
alokp@chromium.org7beea402010-09-15 21:18:34 +0000261void ShGetActiveAttrib(const ShHandle handle,
262 int index,
263 int* length,
264 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000265 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +0000266 char* name,
267 char* mappedName)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000268{
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000269 getVariableInfo(SH_ACTIVE_ATTRIBUTES,
zmo@google.comfd747b82011-04-23 01:30:07 +0000270 handle, index, length, size, type, name, mappedName);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000271}
272
273void ShGetActiveUniform(const ShHandle handle,
274 int index,
275 int* length,
276 int* size,
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000277 ShDataType* type,
zmo@google.comfd747b82011-04-23 01:30:07 +0000278 char* name,
279 char* mappedName)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000280{
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000281 getVariableInfo(SH_ACTIVE_UNIFORMS,
zmo@google.comfd747b82011-04-23 01:30:07 +0000282 handle, index, length, size, type, name, mappedName);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283}