ShaderLang.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 GLSLANG_SHADERLANG_H_
  7. #define GLSLANG_SHADERLANG_H_
  8. #if defined(COMPONENT_BUILD) && !defined(ANGLE_TRANSLATOR_STATIC)
  9. #if defined(_WIN32) || defined(_WIN64)
  10. #if defined(ANGLE_TRANSLATOR_IMPLEMENTATION)
  11. #define COMPILER_EXPORT __declspec(dllexport)
  12. #else
  13. #define COMPILER_EXPORT __declspec(dllimport)
  14. #endif // defined(ANGLE_TRANSLATOR_IMPLEMENTATION)
  15. #else // defined(_WIN32) || defined(_WIN64)
  16. #define COMPILER_EXPORT __attribute__((visibility("default")))
  17. #endif
  18. #else // defined(COMPONENT_BUILD) && !defined(ANGLE_TRANSLATOR_STATIC)
  19. #define COMPILER_EXPORT
  20. #endif
  21. #include <stddef.h>
  22. #include "KHR/khrplatform.h"
  23. #include <map>
  24. #include <string>
  25. #include <vector>
  26. //
  27. // This is the platform independent interface between an OGL driver
  28. // and the shading language compiler.
  29. //
  30. namespace sh
  31. {
  32. // GLenum alias
  33. typedef unsigned int GLenum;
  34. }
  35. // Must be included after GLenum proxy typedef
  36. // Note: make sure to increment ANGLE_SH_VERSION when changing ShaderVars.h
  37. #include "ShaderVars.h"
  38. // Version number for shader translation API.
  39. // It is incremented every time the API changes.
  40. #define ANGLE_SH_VERSION 138
  41. typedef enum {
  42. SH_GLES2_SPEC = 0x8B40,
  43. SH_WEBGL_SPEC = 0x8B41,
  44. SH_GLES3_SPEC = 0x8B86,
  45. SH_WEBGL2_SPEC = 0x8B87,
  46. // The CSS Shaders spec is a subset of the WebGL spec.
  47. //
  48. // In both CSS vertex and fragment shaders, ANGLE:
  49. // (1) Reserves the "css_" prefix.
  50. // (2) Renames the main function to css_main.
  51. // (3) Disables the gl_MaxDrawBuffers built-in.
  52. //
  53. // In CSS fragment shaders, ANGLE:
  54. // (1) Disables the gl_FragColor built-in.
  55. // (2) Disables the gl_FragData built-in.
  56. // (3) Enables the css_MixColor built-in.
  57. // (4) Enables the css_ColorMatrix built-in.
  58. //
  59. // After passing a CSS shader through ANGLE, the browser is expected to append
  60. // a new main function to it.
  61. // This new main function will call the css_main function.
  62. // It may also perform additional operations like varying assignment, texture
  63. // access, and gl_FragColor assignment in order to implement the CSS Shaders
  64. // blend modes.
  65. //
  66. SH_CSS_SHADERS_SPEC = 0x8B42
  67. } ShShaderSpec;
  68. typedef enum {
  69. SH_ESSL_OUTPUT = 0x8B45,
  70. // SH_GLSL_OUTPUT is deprecated. This is to not break the build.
  71. SH_GLSL_OUTPUT = 0x8B46,
  72. SH_GLSL_COMPATIBILITY_OUTPUT = 0x8B46,
  73. // SH_GLSL_CORE_OUTPUT is deprecated.
  74. SH_GLSL_CORE_OUTPUT = 0x8B47,
  75. //Note: GL introduced core profiles in 1.5. However, for compatiblity with Chromium, we treat SH_GLSL_CORE_OUTPUT as GLSL_130_OUTPUT.
  76. //TODO: Remove SH_GLSL_CORE_OUTPUT
  77. SH_GLSL_130_OUTPUT = 0x8B47,
  78. SH_GLSL_140_OUTPUT = 0x8B80,
  79. SH_GLSL_150_CORE_OUTPUT = 0x8B81,
  80. SH_GLSL_330_CORE_OUTPUT = 0x8B82,
  81. SH_GLSL_400_CORE_OUTPUT = 0x8B83,
  82. SH_GLSL_410_CORE_OUTPUT = 0x8B84,
  83. SH_GLSL_420_CORE_OUTPUT = 0x8B85,
  84. SH_GLSL_430_CORE_OUTPUT = 0x8B86,
  85. SH_GLSL_440_CORE_OUTPUT = 0x8B87,
  86. SH_GLSL_450_CORE_OUTPUT = 0x8B88,
  87. // HLSL output only supported in some configurations.
  88. SH_HLSL_OUTPUT = 0x8B48,
  89. SH_HLSL9_OUTPUT = 0x8B48,
  90. SH_HLSL11_OUTPUT = 0x8B49
  91. } ShShaderOutput;
  92. // Compile options.
  93. typedef enum {
  94. SH_VALIDATE = 0,
  95. SH_VALIDATE_LOOP_INDEXING = 0x0001,
  96. SH_INTERMEDIATE_TREE = 0x0002,
  97. SH_OBJECT_CODE = 0x0004,
  98. SH_VARIABLES = 0x0008,
  99. SH_LINE_DIRECTIVES = 0x0010,
  100. SH_SOURCE_PATH = 0x0020,
  101. SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX = 0x0040,
  102. // If a sampler array index happens to be a loop index,
  103. // 1) if its type is integer, unroll the loop.
  104. // 2) if its type is float, fail the shader compile.
  105. // This is to work around a mac driver bug.
  106. SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX = 0x0080,
  107. // This is needed only as a workaround for certain OpenGL driver bugs.
  108. SH_EMULATE_BUILT_IN_FUNCTIONS = 0x0100,
  109. // This is an experimental flag to enforce restrictions that aim to prevent
  110. // timing attacks.
  111. // It generates compilation errors for shaders that could expose sensitive
  112. // texture information via the timing channel.
  113. // To use this flag, you must compile the shader under the WebGL spec
  114. // (using the SH_WEBGL_SPEC flag).
  115. SH_TIMING_RESTRICTIONS = 0x0200,
  116. // This flag prints the dependency graph that is used to enforce timing
  117. // restrictions on fragment shaders.
  118. // This flag only has an effect if all of the following are true:
  119. // - The shader spec is SH_WEBGL_SPEC.
  120. // - The compile options contain the SH_TIMING_RESTRICTIONS flag.
  121. // - The shader type is GL_FRAGMENT_SHADER.
  122. SH_DEPENDENCY_GRAPH = 0x0400,
  123. // Enforce the GLSL 1.017 Appendix A section 7 packing restrictions.
  124. // This flag only enforces (and can only enforce) the packing
  125. // restrictions for uniform variables in both vertex and fragment
  126. // shaders. ShCheckVariablesWithinPackingLimits() lets embedders
  127. // enforce the packing restrictions for varying variables during
  128. // program link time.
  129. SH_ENFORCE_PACKING_RESTRICTIONS = 0x0800,
  130. // This flag ensures all indirect (expression-based) array indexing
  131. // is clamped to the bounds of the array. This ensures, for example,
  132. // that you cannot read off the end of a uniform, whether an array
  133. // vec234, or mat234 type. The ShArrayIndexClampingStrategy enum,
  134. // specified in the ShBuiltInResources when constructing the
  135. // compiler, selects the strategy for the clamping implementation.
  136. SH_CLAMP_INDIRECT_ARRAY_BOUNDS = 0x1000,
  137. // This flag limits the complexity of an expression.
  138. SH_LIMIT_EXPRESSION_COMPLEXITY = 0x2000,
  139. // This flag limits the depth of the call stack.
  140. SH_LIMIT_CALL_STACK_DEPTH = 0x4000,
  141. // This flag initializes gl_Position to vec4(0,0,0,0) at the
  142. // beginning of the vertex shader's main(), and has no effect in the
  143. // fragment shader. It is intended as a workaround for drivers which
  144. // incorrectly fail to link programs if gl_Position is not written.
  145. SH_INIT_GL_POSITION = 0x8000,
  146. // This flag replaces
  147. // "a && b" with "a ? b : false",
  148. // "a || b" with "a ? true : b".
  149. // This is to work around a MacOSX driver bug that |b| is executed
  150. // independent of |a|'s value.
  151. SH_UNFOLD_SHORT_CIRCUIT = 0x10000,
  152. // This flag initializes varyings without static use in vertex shader
  153. // at the beginning of main(), and has no effects in the fragment shader.
  154. // It is intended as a workaround for drivers which incorrectly optimize
  155. // out such varyings and cause a link failure.
  156. SH_INIT_VARYINGS_WITHOUT_STATIC_USE = 0x20000,
  157. // This flag scalarizes vec/ivec/bvec/mat constructor args.
  158. // It is intended as a workaround for Linux/Mac driver bugs.
  159. SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS = 0x40000,
  160. // This flag overwrites a struct name with a unique prefix.
  161. // It is intended as a workaround for drivers that do not handle
  162. // struct scopes correctly, including all Mac drivers and Linux AMD.
  163. SH_REGENERATE_STRUCT_NAMES = 0x80000,
  164. // This flag makes the compiler not prune unused function early in the
  165. // compilation process. Pruning coupled with SH_LIMIT_CALL_STACK_DEPTH
  166. // helps avoid bad shaders causing stack overflows.
  167. SH_DONT_PRUNE_UNUSED_FUNCTIONS = 0x100000,
  168. // This flag works around a bug in NVIDIA 331 series drivers related
  169. // to pow(x, y) where y is a constant vector.
  170. SH_REMOVE_POW_WITH_CONSTANT_EXPONENT = 0x200000,
  171. } ShCompileOptions;
  172. // Defines alternate strategies for implementing array index clamping.
  173. typedef enum {
  174. // Use the clamp intrinsic for array index clamping.
  175. SH_CLAMP_WITH_CLAMP_INTRINSIC = 1,
  176. // Use a user-defined function for array index clamping.
  177. SH_CLAMP_WITH_USER_DEFINED_INT_CLAMP_FUNCTION
  178. } ShArrayIndexClampingStrategy;
  179. //
  180. // Driver must call this first, once, before doing any other
  181. // compiler operations.
  182. // If the function succeeds, the return value is true, else false.
  183. //
  184. COMPILER_EXPORT bool ShInitialize();
  185. //
  186. // Driver should call this at shutdown.
  187. // If the function succeeds, the return value is true, else false.
  188. //
  189. COMPILER_EXPORT bool ShFinalize();
  190. // The 64 bits hash function. The first parameter is the input string; the
  191. // second parameter is the string length.
  192. typedef khronos_uint64_t (*ShHashFunction64)(const char*, size_t);
  193. //
  194. // Implementation dependent built-in resources (constants and extensions).
  195. // The names for these resources has been obtained by stripping gl_/GL_.
  196. //
  197. typedef struct
  198. {
  199. // Constants.
  200. int MaxVertexAttribs;
  201. int MaxVertexUniformVectors;
  202. int MaxVaryingVectors;
  203. int MaxVertexTextureImageUnits;
  204. int MaxCombinedTextureImageUnits;
  205. int MaxTextureImageUnits;
  206. int MaxFragmentUniformVectors;
  207. int MaxDrawBuffers;
  208. // Extensions.
  209. // Set to 1 to enable the extension, else 0.
  210. int OES_standard_derivatives;
  211. int OES_EGL_image_external;
  212. int ARB_texture_rectangle;
  213. int EXT_draw_buffers;
  214. int EXT_frag_depth;
  215. int EXT_shader_texture_lod;
  216. int WEBGL_debug_shader_precision;
  217. int EXT_shader_framebuffer_fetch;
  218. int NV_shader_framebuffer_fetch;
  219. int ARM_shader_framebuffer_fetch;
  220. // Set to 1 to enable replacing GL_EXT_draw_buffers #extension directives
  221. // with GL_NV_draw_buffers in ESSL output. This flag can be used to emulate
  222. // EXT_draw_buffers by using it in combination with GLES3.0 glDrawBuffers
  223. // function. This applies to Tegra K1 devices.
  224. int NV_draw_buffers;
  225. // Set to 1 if highp precision is supported in the fragment language.
  226. // Default is 0.
  227. int FragmentPrecisionHigh;
  228. // GLSL ES 3.0 constants.
  229. int MaxVertexOutputVectors;
  230. int MaxFragmentInputVectors;
  231. int MinProgramTexelOffset;
  232. int MaxProgramTexelOffset;
  233. // Name Hashing.
  234. // Set a 64 bit hash function to enable user-defined name hashing.
  235. // Default is NULL.
  236. ShHashFunction64 HashFunction;
  237. // Selects a strategy to use when implementing array index clamping.
  238. // Default is SH_CLAMP_WITH_CLAMP_INTRINSIC.
  239. ShArrayIndexClampingStrategy ArrayIndexClampingStrategy;
  240. // The maximum complexity an expression can be.
  241. int MaxExpressionComplexity;
  242. // The maximum depth a call stack can be.
  243. int MaxCallStackDepth;
  244. } ShBuiltInResources;
  245. //
  246. // Initialize built-in resources with minimum expected values.
  247. // Parameters:
  248. // resources: The object to initialize. Will be comparable with memcmp.
  249. //
  250. COMPILER_EXPORT void ShInitBuiltInResources(ShBuiltInResources *resources);
  251. //
  252. // ShHandle held by but opaque to the driver. It is allocated,
  253. // managed, and de-allocated by the compiler. Its contents
  254. // are defined by and used by the compiler.
  255. //
  256. // If handle creation fails, 0 will be returned.
  257. //
  258. typedef void *ShHandle;
  259. //
  260. // Returns the a concatenated list of the items in ShBuiltInResources as a
  261. // null-terminated string.
  262. // This function must be updated whenever ShBuiltInResources is changed.
  263. // Parameters:
  264. // handle: Specifies the handle of the compiler to be used.
  265. COMPILER_EXPORT const std::string &ShGetBuiltInResourcesString(const ShHandle handle);
  266. //
  267. // Driver calls these to create and destroy compiler objects.
  268. //
  269. // Returns the handle of constructed compiler, null if the requested compiler is
  270. // not supported.
  271. // Parameters:
  272. // type: Specifies the type of shader - GL_FRAGMENT_SHADER or GL_VERTEX_SHADER.
  273. // spec: Specifies the language spec the compiler must conform to -
  274. // SH_GLES2_SPEC or SH_WEBGL_SPEC.
  275. // output: Specifies the output code type - SH_ESSL_OUTPUT, SH_GLSL_OUTPUT,
  276. // SH_HLSL9_OUTPUT or SH_HLSL11_OUTPUT. Note: HLSL output is only
  277. // supported in some configurations.
  278. // resources: Specifies the built-in resources.
  279. COMPILER_EXPORT ShHandle ShConstructCompiler(
  280. sh::GLenum type,
  281. ShShaderSpec spec,
  282. ShShaderOutput output,
  283. const ShBuiltInResources *resources);
  284. COMPILER_EXPORT void ShDestruct(ShHandle handle);
  285. //
  286. // Compiles the given shader source.
  287. // If the function succeeds, the return value is true, else false.
  288. // Parameters:
  289. // handle: Specifies the handle of compiler to be used.
  290. // shaderStrings: Specifies an array of pointers to null-terminated strings
  291. // containing the shader source code.
  292. // numStrings: Specifies the number of elements in shaderStrings array.
  293. // compileOptions: A mask containing the following parameters:
  294. // SH_VALIDATE: Validates shader to ensure that it conforms to the spec
  295. // specified during compiler construction.
  296. // SH_VALIDATE_LOOP_INDEXING: Validates loop and indexing in the shader to
  297. // ensure that they do not exceed the minimum
  298. // functionality mandated in GLSL 1.0 spec,
  299. // Appendix A, Section 4 and 5.
  300. // There is no need to specify this parameter when
  301. // compiling for WebGL - it is implied.
  302. // SH_INTERMEDIATE_TREE: Writes intermediate tree to info log.
  303. // Can be queried by calling ShGetInfoLog().
  304. // SH_OBJECT_CODE: Translates intermediate tree to glsl or hlsl shader.
  305. // Can be queried by calling ShGetObjectCode().
  306. // SH_VARIABLES: Extracts attributes, uniforms, and varyings.
  307. // Can be queried by calling ShGetVariableInfo().
  308. //
  309. COMPILER_EXPORT bool ShCompile(
  310. const ShHandle handle,
  311. const char * const shaderStrings[],
  312. size_t numStrings,
  313. int compileOptions);
  314. // Clears the results from the previous compilation.
  315. COMPILER_EXPORT void ShClearResults(const ShHandle handle);
  316. // Return the version of the shader language.
  317. COMPILER_EXPORT int ShGetShaderVersion(const ShHandle handle);
  318. // Return the currently set language output type.
  319. COMPILER_EXPORT ShShaderOutput ShGetShaderOutputType(
  320. const ShHandle handle);
  321. // Returns null-terminated information log for a compiled shader.
  322. // Parameters:
  323. // handle: Specifies the compiler
  324. COMPILER_EXPORT const std::string &ShGetInfoLog(const ShHandle handle);
  325. // Returns null-terminated object code for a compiled shader.
  326. // Parameters:
  327. // handle: Specifies the compiler
  328. COMPILER_EXPORT const std::string &ShGetObjectCode(const ShHandle handle);
  329. // Returns a (original_name, hash) map containing all the user defined
  330. // names in the shader, including variable names, function names, struct
  331. // names, and struct field names.
  332. // Parameters:
  333. // handle: Specifies the compiler
  334. COMPILER_EXPORT const std::map<std::string, std::string> *ShGetNameHashingMap(
  335. const ShHandle handle);
  336. // Shader variable inspection.
  337. // Returns a pointer to a list of variables of the designated type.
  338. // (See ShaderVars.h for type definitions, included above)
  339. // Returns NULL on failure.
  340. // Parameters:
  341. // handle: Specifies the compiler
  342. COMPILER_EXPORT const std::vector<sh::Uniform> *ShGetUniforms(const ShHandle handle);
  343. COMPILER_EXPORT const std::vector<sh::Varying> *ShGetVaryings(const ShHandle handle);
  344. COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetAttributes(const ShHandle handle);
  345. COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetOutputVariables(const ShHandle handle);
  346. COMPILER_EXPORT const std::vector<sh::InterfaceBlock> *ShGetInterfaceBlocks(const ShHandle handle);
  347. typedef struct
  348. {
  349. sh::GLenum type;
  350. int size;
  351. } ShVariableInfo;
  352. // Returns true if the passed in variables pack in maxVectors following
  353. // the packing rules from the GLSL 1.017 spec, Appendix A, section 7.
  354. // Returns false otherwise. Also look at the SH_ENFORCE_PACKING_RESTRICTIONS
  355. // flag above.
  356. // Parameters:
  357. // maxVectors: the available rows of registers.
  358. // varInfoArray: an array of variable info (types and sizes).
  359. // varInfoArraySize: the size of the variable array.
  360. COMPILER_EXPORT bool ShCheckVariablesWithinPackingLimits(
  361. int maxVectors,
  362. ShVariableInfo *varInfoArray,
  363. size_t varInfoArraySize);
  364. // Gives the compiler-assigned register for an interface block.
  365. // The method writes the value to the output variable "indexOut".
  366. // Returns true if it found a valid interface block, false otherwise.
  367. // Parameters:
  368. // handle: Specifies the compiler
  369. // interfaceBlockName: Specifies the interface block
  370. // indexOut: output variable that stores the assigned register
  371. COMPILER_EXPORT bool ShGetInterfaceBlockRegister(const ShHandle handle,
  372. const std::string &interfaceBlockName,
  373. unsigned int *indexOut);
  374. // Gives the compiler-assigned register for uniforms in the default
  375. // interface block.
  376. // The method writes the value to the output variable "indexOut".
  377. // Returns true if it found a valid default uniform, false otherwise.
  378. // Parameters:
  379. // handle: Specifies the compiler
  380. // interfaceBlockName: Specifies the uniform
  381. // indexOut: output variable that stores the assigned register
  382. COMPILER_EXPORT bool ShGetUniformRegister(const ShHandle handle,
  383. const std::string &uniformName,
  384. unsigned int *indexOut);
  385. #endif // GLSLANG_SHADERLANG_H_