1 #pragma once
2 #ifndef GLSL_OPTIMIZER_H
3 #define GLSL_OPTIMIZER_H
4 
5 /*
6  Main GLSL optimizer interface.
7  See ../../README.md for more instructions.
8 
9  General usage:
10 
11  ctx = glslopt_initialize();
12  for (lots of shaders) {
13    shader = glslopt_optimize (ctx, shaderType, shaderSource, options);
14    if (glslopt_get_status (shader)) {
15      newSource = glslopt_get_output (shader);
16    } else {
17      errorLog = glslopt_get_log (shader);
18    }
19    glslopt_shader_delete (shader);
20  }
21  glslopt_cleanup (ctx);
22 */
23 
24 extern "C" {
25 
26 struct glslopt_shader;
27 struct glslopt_ctx;
28 
29 enum glslopt_shader_type {
30 	kGlslOptShaderVertex = 0,
31 	kGlslOptShaderFragment,
32 };
33 
34 // Options flags for glsl_optimize
35 enum glslopt_options {
36 	kGlslOptionSkipPreprocessor = (1<<0), // Skip preprocessing shader source. Saves some time if you know you don't need it.
37 	kGlslOptionNotFullShader = (1<<1), // Passed shader is not the full shader source. This makes some optimizations weaker.
38 };
39 
40 // Optimizer target language
41 enum glslopt_target {
42 	kGlslTargetOpenGL = 0,
43 	kGlslTargetOpenGLES20 = 1,
44 	kGlslTargetOpenGLES30 = 2,
45 	kGlslTargetMetal = 3,
46 };
47 
48 // Type info
49 enum glslopt_basic_type {
50 	kGlslTypeFloat = 0,
51 	kGlslTypeInt,
52 	kGlslTypeBool,
53 	kGlslTypeTex2D,
54 	kGlslTypeTex3D,
55 	kGlslTypeTexCube,
56 	kGlslTypeTex2DShadow,
57 	kGlslTypeTex2DArray,
58 	kGlslTypeOther,
59 	kGlslTypeCount
60 };
61 enum glslopt_precision {
62 	kGlslPrecHigh = 0,
63 	kGlslPrecMedium,
64 	kGlslPrecLow,
65 	kGlslPrecCount
66 };
67 
68 glslopt_ctx* glslopt_initialize (glslopt_target target);
69 void glslopt_cleanup (glslopt_ctx* ctx);
70 
71 void glslopt_set_max_unroll_iterations (glslopt_ctx* ctx, unsigned iterations);
72 
73 glslopt_shader* glslopt_optimize (glslopt_ctx* ctx, glslopt_shader_type type, const char* shaderSource, unsigned options);
74 bool glslopt_get_status (glslopt_shader* shader);
75 const char* glslopt_get_output (glslopt_shader* shader);
76 const char* glslopt_get_raw_output (glslopt_shader* shader);
77 const char* glslopt_get_log (glslopt_shader* shader);
78 void glslopt_shader_delete (glslopt_shader* shader);
79 
80 int glslopt_shader_get_input_count (glslopt_shader* shader);
81 void glslopt_shader_get_input_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation);
82 int glslopt_shader_get_uniform_count (glslopt_shader* shader);
83 int glslopt_shader_get_uniform_total_size (glslopt_shader* shader);
84 void glslopt_shader_get_uniform_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation);
85 int glslopt_shader_get_texture_count (glslopt_shader* shader);
86 void glslopt_shader_get_texture_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation);
87 
88 // Get *very* approximate shader stats:
89 // Number of math, texture and flow control instructions.
90 void glslopt_shader_get_stats (glslopt_shader* shader, int* approxMath, int* approxTex, int* approxFlow);
91 
92 } // extern "C"
93 
94 #endif /* GLSL_OPTIMIZER_H */
95