|
| 1 | +// |
| 2 | +// GLSLOptimizerBridge.m |
| 3 | +// glsl_optimizer_lib |
| 4 | +// |
| 5 | +// Created by Ryan Walklin on 2/06/2015. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import <Foundation/Foundation.h> |
| 10 | + |
| 11 | +#import "GLSLOptimizerBridge.h" |
| 12 | + |
| 13 | +#import "glsl_optimizer.h" |
| 14 | + |
| 15 | +@implementation GLSLShaderVariableDescription |
| 16 | + |
| 17 | +-(UInt32)elementCount { |
| 18 | + return self.vecSize * self.matSize * (self.arraySize == -1 ? 1 : self.arraySize); |
| 19 | +} |
| 20 | + |
| 21 | +-(UInt32)elementSize { |
| 22 | + UInt32 elementSize = 0; |
| 23 | + |
| 24 | + switch (self.type) { |
| 25 | + case GLSLOptBasicTypeFloat: |
| 26 | + elementSize = sizeof(float); |
| 27 | + break; |
| 28 | + case GLSLOptBasicTypeInt: |
| 29 | + elementSize = sizeof(int); |
| 30 | + break; |
| 31 | + case GLSLOptBasicTypeBool: |
| 32 | + elementSize = sizeof(bool); |
| 33 | + break; |
| 34 | + case GLSLOptBasicTypeTex2D: |
| 35 | + case GLSLOptBasicTypeTex3D: |
| 36 | + case GLSLOptBasicTypeTexCube: |
| 37 | + break; |
| 38 | + default: |
| 39 | + break; |
| 40 | + } |
| 41 | + return elementSize; |
| 42 | +} |
| 43 | +-(UInt32)rawSize { |
| 44 | + return [self elementCount] * [self elementSize]; |
| 45 | +} |
| 46 | + |
| 47 | +@end |
| 48 | + |
| 49 | +typedef NS_ENUM(NSUInteger, GLSLShaderVariableType) { |
| 50 | + GLSLShaderVariableTypeInput = 0, |
| 51 | + GLSLShaderVariableTypeUniform, |
| 52 | + GLSLShaderVariableTypeTexture |
| 53 | +}; |
| 54 | + |
| 55 | +@interface GLSLShader () { |
| 56 | + |
| 57 | +@private |
| 58 | + glslopt_shader *_shader; |
| 59 | +} |
| 60 | + |
| 61 | +-(id)initWithShader: (glslopt_shader *)shader; |
| 62 | + |
| 63 | +-(GLSLShaderVariableDescription *)shaderVariableDescription:(GLSLShaderVariableType)type index:(UInt32)index; |
| 64 | + |
| 65 | +@end |
| 66 | + |
| 67 | +@implementation GLSLShader: NSObject |
| 68 | + |
| 69 | +-(id)initWithShader: (glslopt_shader *)shader { |
| 70 | + self = [super init]; |
| 71 | + if (self) { |
| 72 | + _shader = shader; |
| 73 | + } |
| 74 | + return self; |
| 75 | +} |
| 76 | + |
| 77 | +-(BOOL)status { |
| 78 | + return glslopt_get_status(_shader) == true; |
| 79 | +} |
| 80 | + |
| 81 | +-(NSString *)output { |
| 82 | + return [NSString stringWithUTF8String: glslopt_get_output(_shader)]; |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +-(NSString *)rawOutput { |
| 87 | + return [NSString stringWithUTF8String: glslopt_get_raw_output(_shader)]; |
| 88 | +} |
| 89 | + |
| 90 | +-(NSString *)log { |
| 91 | + return [NSString stringWithUTF8String: glslopt_get_log (_shader)]; |
| 92 | +} |
| 93 | + |
| 94 | +-(UInt32)inputCount { |
| 95 | + return UInt32(glslopt_shader_get_input_count(_shader)); |
| 96 | +} |
| 97 | + |
| 98 | +-(GLSLShaderVariableDescription *)inputDescription:(UInt32)index { |
| 99 | + NSAssert(index < self.inputCount, @"index < inputCount"); |
| 100 | + return [self shaderVariableDescription:GLSLShaderVariableTypeInput index:index]; |
| 101 | +} |
| 102 | + |
| 103 | +-(UInt32)uniformCount { |
| 104 | + return UInt32(glslopt_shader_get_uniform_count(_shader)); |
| 105 | +} |
| 106 | + |
| 107 | +-(UInt32)uniformTotalSize { |
| 108 | + return UInt32(glslopt_shader_get_uniform_total_size(_shader)); |
| 109 | +} |
| 110 | + |
| 111 | +-(GLSLShaderVariableDescription *)uniformDescription:(UInt32)index { |
| 112 | + NSAssert(index < self.uniformCount, @"index < inputCount"); |
| 113 | + return [self shaderVariableDescription:GLSLShaderVariableTypeUniform index:index]; |
| 114 | +} |
| 115 | + |
| 116 | +-(UInt32)textureCount { |
| 117 | + return UInt32(glslopt_shader_get_texture_count(_shader)); |
| 118 | +} |
| 119 | + |
| 120 | +-(GLSLShaderVariableDescription *)textureDescription:(UInt32)index { |
| 121 | + NSAssert(index < self.textureCount, @"index < inputCount"); |
| 122 | + return [self shaderVariableDescription:GLSLShaderVariableTypeTexture index:index]; |
| 123 | +} |
| 124 | + |
| 125 | +-(GLSLShaderVariableDescription *)shaderVariableDescription:(GLSLShaderVariableType)type index:(UInt32)index { |
| 126 | + |
| 127 | + const char *outName = nil; |
| 128 | + glslopt_basic_type outType; |
| 129 | + glslopt_precision outPrec; |
| 130 | + int outVecSize; |
| 131 | + int outMatSize; |
| 132 | + int outArraySize; |
| 133 | + int outLocation; |
| 134 | + |
| 135 | + switch (type) { |
| 136 | + case GLSLShaderVariableTypeInput: |
| 137 | + glslopt_shader_get_input_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation); |
| 138 | + break; |
| 139 | + case GLSLShaderVariableTypeUniform: |
| 140 | + glslopt_shader_get_uniform_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation); |
| 141 | + break; |
| 142 | + case GLSLShaderVariableTypeTexture: |
| 143 | + glslopt_shader_get_texture_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation); |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + GLSLShaderVariableDescription *varDesc = [[GLSLShaderVariableDescription alloc] init]; |
| 148 | + |
| 149 | + varDesc.name = [NSString stringWithUTF8String:outName]; |
| 150 | + varDesc.type = GLSLOptBasicType(outType); |
| 151 | + varDesc.prec = GLSLOptPrecision(outPrec); |
| 152 | + varDesc.vecSize = SInt32(outVecSize); |
| 153 | + varDesc.matSize = SInt32(outMatSize); |
| 154 | + varDesc.arraySize = SInt32(outArraySize); |
| 155 | + varDesc.location = SInt32(outLocation); |
| 156 | + |
| 157 | + return varDesc; |
| 158 | +} |
| 159 | + |
| 160 | +/* |
| 161 | + // Get *very* approximate shader stats: |
| 162 | + // Number of math, texture and flow control instructions. |
| 163 | + void glslopt_shader_get_stats (glslopt_shader* shader, int* approxMath, int* approxTex, int* approxFlow); |
| 164 | + */ |
| 165 | + |
| 166 | +- (void)dealloc |
| 167 | +{ |
| 168 | + glslopt_shader_delete(_shader); |
| 169 | +} |
| 170 | + |
| 171 | +@end |
| 172 | + |
| 173 | +@interface GLSLOptimizer () { |
| 174 | + @private |
| 175 | + glslopt_ctx *_ctx; |
| 176 | +} |
| 177 | + |
| 178 | +@end |
| 179 | + |
| 180 | +@implementation GLSLOptimizer |
| 181 | + |
| 182 | +-(id)init:(GLSLOptTarget)target { |
| 183 | + self = [super init]; |
| 184 | + if (self) { |
| 185 | + _ctx = glslopt_initialize(glslopt_target(target)); |
| 186 | + } |
| 187 | + return self; |
| 188 | +} |
| 189 | + |
| 190 | +-(void)dealloc { |
| 191 | + glslopt_cleanup(_ctx); |
| 192 | +} |
| 193 | + |
| 194 | +-(void)setMaxUnrollIterations:(UInt32)iterations { |
| 195 | + glslopt_set_max_unroll_iterations(_ctx, iterations); |
| 196 | +} |
| 197 | + |
| 198 | +-(GLSLShader *)optimize:(GLSLOptShaderType)shaderType shaderSource:(NSString *)shaderSource options:(NSUInteger)options { |
| 199 | + glslopt_shader* shader = glslopt_optimize(_ctx, glslopt_shader_type(shaderType), shaderSource.UTF8String, UInt32(options)); |
| 200 | + |
| 201 | + GLSLShader *glslShader = [[GLSLShader alloc] initWithShader: shader]; |
| 202 | + |
| 203 | + if ([glslShader status]) { |
| 204 | + return glslShader; |
| 205 | + } else { |
| 206 | + NSLog(@"%@", [glslShader log]); |
| 207 | + } |
| 208 | + return nil; |
| 209 | +} |
| 210 | + |
| 211 | +@end |
| 212 | + |
| 213 | + |
0 commit comments