• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2002-2013 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.
5 //
6 #ifndef _COMPILER_INTERFACE_INCLUDED_
7 #define _COMPILER_INTERFACE_INCLUDED_
8 
9 #if defined(COMPONENT_BUILD) && !defined(ANGLE_TRANSLATOR_STATIC)
10 #if defined(_WIN32) || defined(_WIN64)
11 
12 #if defined(ANGLE_TRANSLATOR_IMPLEMENTATION)
13 #define COMPILER_EXPORT __declspec(dllexport)
14 #else
15 #define COMPILER_EXPORT __declspec(dllimport)
16 #endif  // defined(ANGLE_TRANSLATOR_IMPLEMENTATION)
17 
18 #else  // defined(_WIN32) || defined(_WIN64)
19 #define COMPILER_EXPORT __attribute__((visibility("default")))
20 #endif
21 
22 #else  // defined(COMPONENT_BUILD) && !defined(ANGLE_TRANSLATOR_STATIC)
23 #define COMPILER_EXPORT
24 #endif
25 
26 #include "KHR/khrplatform.h"
27 #include <stddef.h>
28 
29 //
30 // This is the platform independent interface between an OGL driver
31 // and the shading language compiler.
32 //
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 // Version number for shader translation API.
39 // It is incremented everytime the API changes.
40 #define ANGLE_SH_VERSION 112
41 
42 //
43 // The names of the following enums have been derived by replacing GL prefix
44 // with SH. For example, SH_INFO_LOG_LENGTH is equivalent to GL_INFO_LOG_LENGTH.
45 // The enum values are also equal to the values of their GL counterpart. This
46 // is done to make it easier for applications to use the shader library.
47 //
48 typedef enum {
49   SH_FRAGMENT_SHADER = 0x8B30,
50   SH_VERTEX_SHADER   = 0x8B31
51 } ShShaderType;
52 
53 typedef enum {
54   SH_GLES2_SPEC = 0x8B40,
55   SH_WEBGL_SPEC = 0x8B41,
56 
57   // The CSS Shaders spec is a subset of the WebGL spec.
58   //
59   // In both CSS vertex and fragment shaders, ANGLE:
60   // (1) Reserves the "css_" prefix.
61   // (2) Renames the main function to css_main.
62   // (3) Disables the gl_MaxDrawBuffers built-in.
63   //
64   // In CSS fragment shaders, ANGLE:
65   // (1) Disables the gl_FragColor built-in.
66   // (2) Disables the gl_FragData built-in.
67   // (3) Enables the css_MixColor built-in.
68   // (4) Enables the css_ColorMatrix built-in.
69   //
70   // After passing a CSS shader through ANGLE, the browser is expected to append
71   // a new main function to it.
72   // This new main function will call the css_main function.
73   // It may also perform additional operations like varying assignment, texture
74   // access, and gl_FragColor assignment in order to implement the CSS Shaders
75   // blend modes.
76   //
77   SH_CSS_SHADERS_SPEC = 0x8B42
78 } ShShaderSpec;
79 
80 typedef enum {
81   SH_ESSL_OUTPUT   = 0x8B45,
82   SH_GLSL_OUTPUT   = 0x8B46,
83   SH_HLSL_OUTPUT   = 0x8B47,
84   SH_HLSL9_OUTPUT  = 0x8B47,
85   SH_HLSL11_OUTPUT = 0x8B48
86 } ShShaderOutput;
87 
88 typedef enum {
89   SH_NONE           = 0,
90   SH_INT            = 0x1404,
91   SH_FLOAT          = 0x1406,
92   SH_FLOAT_VEC2     = 0x8B50,
93   SH_FLOAT_VEC3     = 0x8B51,
94   SH_FLOAT_VEC4     = 0x8B52,
95   SH_INT_VEC2       = 0x8B53,
96   SH_INT_VEC3       = 0x8B54,
97   SH_INT_VEC4       = 0x8B55,
98   SH_BOOL           = 0x8B56,
99   SH_BOOL_VEC2      = 0x8B57,
100   SH_BOOL_VEC3      = 0x8B58,
101   SH_BOOL_VEC4      = 0x8B59,
102   SH_FLOAT_MAT2     = 0x8B5A,
103   SH_FLOAT_MAT3     = 0x8B5B,
104   SH_FLOAT_MAT4     = 0x8B5C,
105   SH_SAMPLER_2D     = 0x8B5E,
106   SH_SAMPLER_CUBE   = 0x8B60,
107   SH_SAMPLER_2D_RECT_ARB = 0x8B63,
108   SH_SAMPLER_EXTERNAL_OES = 0x8D66
109 } ShDataType;
110 
111 typedef enum {
112   SH_PRECISION_HIGHP     = 0x5001,
113   SH_PRECISION_MEDIUMP   = 0x5002,
114   SH_PRECISION_LOWP      = 0x5003,
115   SH_PRECISION_UNDEFINED = 0
116 } ShPrecisionType;
117 
118 typedef enum {
119   SH_INFO_LOG_LENGTH             =  0x8B84,
120   SH_OBJECT_CODE_LENGTH          =  0x8B88,  // GL_SHADER_SOURCE_LENGTH
121   SH_ACTIVE_UNIFORMS             =  0x8B86,
122   SH_ACTIVE_UNIFORM_MAX_LENGTH   =  0x8B87,
123   SH_ACTIVE_ATTRIBUTES           =  0x8B89,
124   SH_ACTIVE_ATTRIBUTE_MAX_LENGTH =  0x8B8A,
125   SH_VARYINGS                    =  0x8BBB,
126   SH_VARYING_MAX_LENGTH          =  0x8BBC,
127   SH_MAPPED_NAME_MAX_LENGTH      =  0x6000,
128   SH_NAME_MAX_LENGTH             =  0x6001,
129   SH_HASHED_NAME_MAX_LENGTH      =  0x6002,
130   SH_HASHED_NAMES_COUNT          =  0x6003,
131   SH_ACTIVE_UNIFORMS_ARRAY       =  0x6004
132 } ShShaderInfo;
133 
134 // Compile options.
135 typedef enum {
136   SH_VALIDATE                = 0,
137   SH_VALIDATE_LOOP_INDEXING  = 0x0001,
138   SH_INTERMEDIATE_TREE       = 0x0002,
139   SH_OBJECT_CODE             = 0x0004,
140   SH_VARIABLES               = 0x0008,
141   SH_LINE_DIRECTIVES         = 0x0010,
142   SH_SOURCE_PATH             = 0x0020,
143   SH_MAP_LONG_VARIABLE_NAMES = 0x0040,
144   SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX = 0x0080,
145 
146   // This is needed only as a workaround for certain OpenGL driver bugs.
147   SH_EMULATE_BUILT_IN_FUNCTIONS = 0x0100,
148 
149   // This is an experimental flag to enforce restrictions that aim to prevent
150   // timing attacks.
151   // It generates compilation errors for shaders that could expose sensitive
152   // texture information via the timing channel.
153   // To use this flag, you must compile the shader under the WebGL spec
154   // (using the SH_WEBGL_SPEC flag).
155   SH_TIMING_RESTRICTIONS = 0x0200,
156 
157   // This flag prints the dependency graph that is used to enforce timing
158   // restrictions on fragment shaders.
159   // This flag only has an effect if all of the following are true:
160   // - The shader spec is SH_WEBGL_SPEC.
161   // - The compile options contain the SH_TIMING_RESTRICTIONS flag.
162   // - The shader type is SH_FRAGMENT_SHADER.
163   SH_DEPENDENCY_GRAPH = 0x0400,
164 
165   // Enforce the GLSL 1.017 Appendix A section 7 packing restrictions.
166   // This flag only enforces (and can only enforce) the packing
167   // restrictions for uniform variables in both vertex and fragment
168   // shaders. ShCheckVariablesWithinPackingLimits() lets embedders
169   // enforce the packing restrictions for varying variables during
170   // program link time.
171   SH_ENFORCE_PACKING_RESTRICTIONS = 0x0800,
172 
173   // This flag ensures all indirect (expression-based) array indexing
174   // is clamped to the bounds of the array. This ensures, for example,
175   // that you cannot read off the end of a uniform, whether an array
176   // vec234, or mat234 type. The ShArrayIndexClampingStrategy enum,
177   // specified in the ShBuiltInResources when constructing the
178   // compiler, selects the strategy for the clamping implementation.
179   SH_CLAMP_INDIRECT_ARRAY_BOUNDS = 0x1000,
180 
181   // This flag limits the complexity of an expression.
182   SH_LIMIT_EXPRESSION_COMPLEXITY = 0x2000,
183 
184   // This flag limits the depth of the call stack.
185   SH_LIMIT_CALL_STACK_DEPTH = 0x4000,
186 
187   // This flag initializes gl_Position to vec4(0.0, 0.0, 0.0, 1.0) at
188   // the beginning of the vertex shader, and has no effect in the
189   // fragment shader. It is intended as a workaround for drivers which
190   // incorrectly fail to link programs if gl_Position is not written.
191   SH_INIT_GL_POSITION = 0x8000,
192 
193   // This flag replaces
194   //   "a && b" with "a ? b : false",
195   //   "a || b" with "a ? true : b".
196   // This is to work around a MacOSX driver bug that |b| is executed
197   // independent of |a|'s value.
198   SH_UNFOLD_SHORT_CIRCUIT = 0x10000,
199 } ShCompileOptions;
200 
201 // Defines alternate strategies for implementing array index clamping.
202 typedef enum {
203   // Use the clamp intrinsic for array index clamping.
204   SH_CLAMP_WITH_CLAMP_INTRINSIC = 1,
205 
206   // Use a user-defined function for array index clamping.
207   SH_CLAMP_WITH_USER_DEFINED_INT_CLAMP_FUNCTION
208 } ShArrayIndexClampingStrategy;
209 
210 //
211 // Driver must call this first, once, before doing any other
212 // compiler operations.
213 // If the function succeeds, the return value is nonzero, else zero.
214 //
215 COMPILER_EXPORT int ShInitialize();
216 //
217 // Driver should call this at shutdown.
218 // If the function succeeds, the return value is nonzero, else zero.
219 //
220 COMPILER_EXPORT int ShFinalize();
221 
222 // The 64 bits hash function. The first parameter is the input string; the
223 // second parameter is the string length.
224 typedef khronos_uint64_t (*ShHashFunction64)(const char*, size_t);
225 
226 //
227 // Implementation dependent built-in resources (constants and extensions).
228 // The names for these resources has been obtained by stripping gl_/GL_.
229 //
230 typedef struct
231 {
232     // Constants.
233     int MaxVertexAttribs;
234     int MaxVertexUniformVectors;
235     int MaxVaryingVectors;
236     int MaxVertexTextureImageUnits;
237     int MaxCombinedTextureImageUnits;
238     int MaxTextureImageUnits;
239     int MaxFragmentUniformVectors;
240     int MaxDrawBuffers;
241 
242     // Extensions.
243     // Set to 1 to enable the extension, else 0.
244     int OES_standard_derivatives;
245     int OES_EGL_image_external;
246     int ARB_texture_rectangle;
247     int EXT_draw_buffers;
248     int EXT_frag_depth;
249 
250     // Set to 1 if highp precision is supported in the fragment language.
251     // Default is 0.
252     int FragmentPrecisionHigh;
253 
254     // Name Hashing.
255     // Set a 64 bit hash function to enable user-defined name hashing.
256     // Default is NULL.
257     ShHashFunction64 HashFunction;
258 
259     // Selects a strategy to use when implementing array index clamping.
260     // Default is SH_CLAMP_WITH_CLAMP_INTRINSIC.
261     ShArrayIndexClampingStrategy ArrayIndexClampingStrategy;
262 
263     // The maximum complexity an expression can be.
264     int MaxExpressionComplexity;
265 
266     // The maximum depth a call stack can be.
267     int MaxCallStackDepth;
268 } ShBuiltInResources;
269 
270 //
271 // Initialize built-in resources with minimum expected values.
272 //
273 COMPILER_EXPORT void ShInitBuiltInResources(ShBuiltInResources* resources);
274 
275 //
276 // ShHandle held by but opaque to the driver.  It is allocated,
277 // managed, and de-allocated by the compiler. It's contents
278 // are defined by and used by the compiler.
279 //
280 // If handle creation fails, 0 will be returned.
281 //
282 typedef void* ShHandle;
283 
284 //
285 // Driver calls these to create and destroy compiler objects.
286 //
287 // Returns the handle of constructed compiler, null if the requested compiler is
288 // not supported.
289 // Parameters:
290 // type: Specifies the type of shader - SH_FRAGMENT_SHADER or SH_VERTEX_SHADER.
291 // spec: Specifies the language spec the compiler must conform to -
292 //       SH_GLES2_SPEC or SH_WEBGL_SPEC.
293 // output: Specifies the output code type - SH_ESSL_OUTPUT, SH_GLSL_OUTPUT,
294 //         SH_HLSL9_OUTPUT or SH_HLSL11_OUTPUT.
295 // resources: Specifies the built-in resources.
296 COMPILER_EXPORT ShHandle ShConstructCompiler(
297     ShShaderType type,
298     ShShaderSpec spec,
299     ShShaderOutput output,
300     const ShBuiltInResources* resources);
301 COMPILER_EXPORT void ShDestruct(ShHandle handle);
302 
303 //
304 // Compiles the given shader source.
305 // If the function succeeds, the return value is nonzero, else zero.
306 // Parameters:
307 // handle: Specifies the handle of compiler to be used.
308 // shaderStrings: Specifies an array of pointers to null-terminated strings
309 //                containing the shader source code.
310 // numStrings: Specifies the number of elements in shaderStrings array.
311 // compileOptions: A mask containing the following parameters:
312 // SH_VALIDATE: Validates shader to ensure that it conforms to the spec
313 //              specified during compiler construction.
314 // SH_VALIDATE_LOOP_INDEXING: Validates loop and indexing in the shader to
315 //                            ensure that they do not exceed the minimum
316 //                            functionality mandated in GLSL 1.0 spec,
317 //                            Appendix A, Section 4 and 5.
318 //                            There is no need to specify this parameter when
319 //                            compiling for WebGL - it is implied.
320 // SH_INTERMEDIATE_TREE: Writes intermediate tree to info log.
321 //                       Can be queried by calling ShGetInfoLog().
322 // SH_OBJECT_CODE: Translates intermediate tree to glsl or hlsl shader.
323 //                 Can be queried by calling ShGetObjectCode().
324 // SH_VARIABLES: Extracts attributes, uniforms, and varyings.
325 //               Can be queried by calling ShGetVariableInfo().
326 //
327 COMPILER_EXPORT int ShCompile(
328     const ShHandle handle,
329     const char* const shaderStrings[],
330     size_t numStrings,
331     int compileOptions
332     );
333 
334 // Returns a parameter from a compiled shader.
335 // Parameters:
336 // handle: Specifies the compiler
337 // pname: Specifies the parameter to query.
338 // The following parameters are defined:
339 // SH_INFO_LOG_LENGTH: the number of characters in the information log
340 //                     including the null termination character.
341 // SH_OBJECT_CODE_LENGTH: the number of characters in the object code
342 //                        including the null termination character.
343 // SH_ACTIVE_ATTRIBUTES: the number of active attribute variables.
344 // SH_ACTIVE_ATTRIBUTE_MAX_LENGTH: the length of the longest active attribute
345 //                                 variable name including the null
346 //                                 termination character.
347 // SH_ACTIVE_UNIFORMS: the number of active uniform variables.
348 // SH_ACTIVE_UNIFORM_MAX_LENGTH: the length of the longest active uniform
349 //                               variable name including the null
350 //                               termination character.
351 // SH_VARYINGS: the number of varying variables.
352 // SH_VARYING_MAX_LENGTH: the length of the longest varying variable name
353 //                        including the null termination character.
354 // SH_MAPPED_NAME_MAX_LENGTH: the length of the mapped variable name including
355 //                            the null termination character.
356 // SH_NAME_MAX_LENGTH: the max length of a user-defined name including the
357 //                     null termination character.
358 // SH_HASHED_NAME_MAX_LENGTH: the max length of a hashed name including the
359 //                            null termination character.
360 // SH_HASHED_NAMES_COUNT: the number of hashed names from the latest compile.
361 //
362 // params: Requested parameter
363 COMPILER_EXPORT void ShGetInfo(const ShHandle handle,
364                                ShShaderInfo pname,
365                                size_t* params);
366 
367 // Returns nul-terminated information log for a compiled shader.
368 // Parameters:
369 // handle: Specifies the compiler
370 // infoLog: Specifies an array of characters that is used to return
371 //          the information log. It is assumed that infoLog has enough memory
372 //          to accomodate the information log. The size of the buffer required
373 //          to store the returned information log can be obtained by calling
374 //          ShGetInfo with SH_INFO_LOG_LENGTH.
375 COMPILER_EXPORT void ShGetInfoLog(const ShHandle handle, char* infoLog);
376 
377 // Returns null-terminated object code for a compiled shader.
378 // Parameters:
379 // handle: Specifies the compiler
380 // infoLog: Specifies an array of characters that is used to return
381 //          the object code. It is assumed that infoLog has enough memory to
382 //          accomodate the object code. The size of the buffer required to
383 //          store the returned object code can be obtained by calling
384 //          ShGetInfo with SH_OBJECT_CODE_LENGTH.
385 COMPILER_EXPORT void ShGetObjectCode(const ShHandle handle, char* objCode);
386 
387 // Returns information about a shader variable.
388 // Parameters:
389 // handle: Specifies the compiler
390 // variableType: Specifies the variable type; options include
391 //               SH_ACTIVE_ATTRIBUTES, SH_ACTIVE_UNIFORMS, SH_VARYINGS.
392 // index: Specifies the index of the variable to be queried.
393 // length: Returns the number of characters actually written in the string
394 //         indicated by name (excluding the null terminator) if a value other
395 //         than NULL is passed.
396 // size: Returns the size of the variable.
397 // type: Returns the data type of the variable.
398 // precision: Returns the precision of the variable.
399 // staticUse: Returns 1 if the variable is accessed in a statement after
400 //            pre-processing, whether or not run-time flow of control will
401 //            cause that statement to be executed.
402 //            Returns 0 otherwise.
403 // name: Returns a null terminated string containing the name of the
404 //       variable. It is assumed that name has enough memory to accormodate
405 //       the variable name. The size of the buffer required to store the
406 //       variable name can be obtained by calling ShGetInfo with
407 //       SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, SH_ACTIVE_UNIFORM_MAX_LENGTH,
408 //       SH_VARYING_MAX_LENGTH.
409 // mappedName: Returns a null terminated string containing the mapped name of
410 //             the variable, It is assumed that mappedName has enough memory
411 //             (SH_MAPPED_NAME_MAX_LENGTH), or NULL if don't care about the
412 //             mapped name. If the name is not mapped, then name and mappedName
413 //             are the same.
414 COMPILER_EXPORT void ShGetVariableInfo(const ShHandle handle,
415                                        ShShaderInfo variableType,
416                                        int index,
417                                        size_t* length,
418                                        int* size,
419                                        ShDataType* type,
420                                        ShPrecisionType* precision,
421                                        int* staticUse,
422                                        char* name,
423                                        char* mappedName);
424 
425 // Returns information about a name hashing entry from the latest compile.
426 // Parameters:
427 // handle: Specifies the compiler
428 // index: Specifies the index of the name hashing entry to be queried.
429 // name: Returns a null terminated string containing the user defined name.
430 //       It is assumed that name has enough memory to accomodate the name.
431 //       The size of the buffer required to store the user defined name can
432 //       be obtained by calling ShGetInfo with SH_NAME_MAX_LENGTH.
433 // hashedName: Returns a null terminated string containing the hashed name of
434 //             the uniform variable, It is assumed that hashedName has enough
435 //             memory to accomodate the name. The size of the buffer required
436 //             to store the name can be obtained by calling ShGetInfo with
437 //             SH_HASHED_NAME_MAX_LENGTH.
438 COMPILER_EXPORT void ShGetNameHashingEntry(const ShHandle handle,
439                                            int index,
440                                            char* name,
441                                            char* hashedName);
442 
443 // Returns a parameter from a compiled shader.
444 // Parameters:
445 // handle: Specifies the compiler
446 // pname: Specifies the parameter to query.
447 // The following parameters are defined:
448 // SH_ACTIVE_UNIFORMS_ARRAY: an STL vector of active uniforms. Valid only for
449 //                           HLSL output.
450 // params: Requested parameter
451 COMPILER_EXPORT void ShGetInfoPointer(const ShHandle handle,
452                                       ShShaderInfo pname,
453                                       void** params);
454 
455 typedef struct
456 {
457     ShDataType type;
458     int size;
459 } ShVariableInfo;
460 
461 // Returns 1 if the passed in variables pack in maxVectors following
462 // the packing rules from the GLSL 1.017 spec, Appendix A, section 7.
463 // Returns 0 otherwise. Also look at the SH_ENFORCE_PACKING_RESTRICTIONS
464 // flag above.
465 // Parameters:
466 // maxVectors: the available rows of registers.
467 // varInfoArray: an array of variable info (types and sizes).
468 // varInfoArraySize: the size of the variable array.
469 COMPILER_EXPORT int ShCheckVariablesWithinPackingLimits(
470     int maxVectors,
471     ShVariableInfo* varInfoArray,
472     size_t varInfoArraySize);
473 
474 #ifdef __cplusplus
475 }
476 #endif
477 
478 #endif // _COMPILER_INTERFACE_INCLUDED_
479