Skip to content

Commit 49f4cae

Browse files
Create iOS framework target and Objective-C public API wrapper to enable import as a LLVM module
1 parent bdc6f4a commit 49f4cae

File tree

7 files changed

+939
-1
lines changed

7 files changed

+939
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// GLSLOptimiser.h
3+
// GLSLOptimiser
4+
//
5+
// Created by Ryan Walklin on 2/06/2015.
6+
//
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
//! Project version number for GLSLOptimizer.
12+
FOUNDATION_EXPORT double GLSLOptimizerVersionNumber;
13+
14+
//! Project version string for GLSLOptimizer.
15+
FOUNDATION_EXPORT const unsigned char GLSLOptimizerVersionString[];
16+
17+
// In this header, you should import all the public headers of your framework using statements like #import <GLSLOptimiser/PublicHeader.h>
18+
19+
#import "GLSLOptimizerBridge.h"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// GLSLOptimizerBridge.h
3+
// glsl_optimizer_lib
4+
//
5+
// Created by Ryan Walklin on 2/06/2015.
6+
//
7+
//
8+
9+
@class GLSLShader;
10+
11+
#import <Foundation/Foundation.h>
12+
13+
typedef NS_ENUM(NSUInteger, GLSLOptShaderType) {
14+
GLSLOptShaderTypeVertex = 0, // kGlslOptShaderVertex
15+
GLSLOptShaderTypeFragment // kGlslOptShaderFragment
16+
}; // glslopt_shader_type
17+
18+
// Options flags for glsl_optimize
19+
typedef NS_ENUM(NSUInteger, GLSLOptOptions) {
20+
GLSLOptOptionsSkipProcessor = 0, //kGlslOptionSkipPreprocessor = (1<<0), // Skip preprocessing shader source. Saves some time if you know you don't need it.
21+
GLSLOptOptionsNotFullShader // kGlslOptionNotFullShader = (1<<1), // Passed shader is not the full shader source. This makes some optimizations weaker.
22+
}; // glslopt_options
23+
24+
// Optimizer target language
25+
typedef NS_ENUM(NSUInteger, GLSLOptTarget) {
26+
GLSLOptTargetOpenGL = 0, // kGlslTargetOpenGL = 0,
27+
GLSLOptTargetOpenGLES20 = 1, // kGlslTargetOpenGLES20 = 1,
28+
GLSLOptTargetOpenGLES30 = 2,// kGlslTargetOpenGLES30 = 2,
29+
GLSLOptTargetMetal = 3// kGlslTargetMetal = 3,
30+
}; // glslopt_target
31+
32+
// Type info
33+
typedef NS_ENUM(NSUInteger, GLSLOptBasicType) {
34+
GLSLOptBasicTypeFloat = 0, // kGlslTypeFloat = 0,
35+
GLSLOptBasicTypeInt, // kGlslTypeInt,
36+
GLSLOptBasicTypeBool, // kGlslTypeBool,
37+
GLSLOptBasicTypeTex2D, // kGlslTypeTex2D,
38+
GLSLOptBasicTypeTex3D, // kGlslTypeTex3D,
39+
GLSLOptBasicTypeTexCube, // kGlslTypeTexCube,
40+
GLSLOptBasicTypeOther, // kGlslTypeOther,
41+
GLSLOptBasicTypeCount // kGlslTypeCount
42+
}; // glslopt_basic_type
43+
44+
typedef NS_ENUM(NSUInteger, GLSLOptPrecision) {
45+
GLSLOptPrecisionHigh = 0, // kGlslPrecHigh = 0,
46+
GLSLOptPrecisionMedium, // kGlslPrecMedium,
47+
GLSLOptPrecisionLow, // kGlslPrecLow,
48+
GLSLOptPrecisionCount // kGlslPrecCount
49+
}; // glslopt_precision
50+
51+
@interface GLSLShaderVariableDescription : NSObject
52+
53+
@property UInt32 index;
54+
@property NSString *name;
55+
@property GLSLOptBasicType type;
56+
@property GLSLOptPrecision prec;
57+
@property UInt32 vecSize;
58+
@property UInt32 matSize;
59+
@property SInt32 arraySize;
60+
@property UInt32 location;
61+
62+
@end
63+
64+
@interface GLSLShader: NSObject
65+
66+
-(BOOL)status;
67+
68+
-(NSString *)output;
69+
-(NSString *)rawOutput;
70+
-(NSString *)log;
71+
-(UInt32)inputCount;
72+
-(GLSLShaderVariableDescription *)inputDescription:(UInt32)index;
73+
74+
@end
75+
76+
@interface GLSLOptimizer: NSObject
77+
78+
-(id)init:(GLSLOptTarget)target;
79+
80+
-(void)setMaxUnrollIterations:(UInt32)iterations;
81+
82+
-(GLSLShader *)optimize:(GLSLOptShaderType)shaderType shaderSource:(NSString *)shaderSource options:(NSUInteger)options;
83+
84+
@end
85+
86+
87+
88+
/*
89+
90+
int glslopt_shader_get_uniform_count (glslopt_shader* shader);
91+
int glslopt_shader_get_uniform_total_size (glslopt_shader* shader);
92+
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);
93+
int glslopt_shader_get_texture_count (glslopt_shader* shader);
94+
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);
95+
96+
// Get *very* approximate shader stats:
97+
// Number of math, texture and flow control instructions.
98+
void glslopt_shader_get_stats (glslopt_shader* shader, int* approxMath, int* approxTex, int* approxFlow);
99+
100+
*/
101+
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
@end
18+
19+
typedef NS_ENUM(NSUInteger, GLSLShaderVariableType) {
20+
GLSLShaderVariableTypeInput = 0,
21+
GLSLShaderVariableTypeUniform,
22+
GLSLShaderVariableTypeTexture
23+
};
24+
25+
@interface GLSLShader () {
26+
27+
@private
28+
glslopt_shader *_shader;
29+
}
30+
31+
-(id)initWithShader: (glslopt_shader *)shader;
32+
33+
-(GLSLShaderVariableDescription *)shaderVariableDescription:(GLSLShaderVariableType)type index:(UInt32)index;
34+
35+
@end
36+
37+
@implementation GLSLShader: NSObject
38+
39+
-(id)initWithShader: (glslopt_shader *)shader {
40+
self = [super init];
41+
if (self) {
42+
_shader = shader;
43+
}
44+
return self;
45+
}
46+
47+
-(BOOL)status {
48+
return glslopt_get_status(_shader) == true;
49+
}
50+
51+
-(NSString *)output {
52+
return [NSString stringWithUTF8String: glslopt_get_output(_shader)];
53+
54+
}
55+
56+
-(NSString *)rawOutput {
57+
return [NSString stringWithUTF8String: glslopt_get_raw_output(_shader)];
58+
}
59+
60+
-(NSString *)log {
61+
return [NSString stringWithUTF8String: glslopt_get_log (_shader)];
62+
}
63+
64+
-(UInt32)inputCount {
65+
return UInt32(glslopt_shader_get_input_count(_shader));
66+
}
67+
68+
-(GLSLShaderVariableDescription *)inputDescription:(UInt32)index {
69+
NSAssert(index < self.inputCount, @"index < inputCount");
70+
return [self shaderVariableDescription:GLSLShaderVariableTypeInput index:index];
71+
}
72+
73+
-(UInt32)uniformCount {
74+
return UInt32(glslopt_shader_get_uniform_count(_shader));
75+
}
76+
77+
-(UInt32)uniformTotalSize {
78+
return UInt32(glslopt_shader_get_uniform_total_size(_shader));
79+
}
80+
81+
-(GLSLShaderVariableDescription *)uniformDescription:(UInt32)index {
82+
NSAssert(index < self.uniformCount, @"index < inputCount");
83+
return [self shaderVariableDescription:GLSLShaderVariableTypeUniform index:index];
84+
}
85+
86+
-(UInt32)textureCount {
87+
return UInt32(glslopt_shader_get_texture_count(_shader));
88+
}
89+
90+
-(GLSLShaderVariableDescription *)textureDescription:(UInt32)index {
91+
NSAssert(index < self.textureCount, @"index < inputCount");
92+
return [self shaderVariableDescription:GLSLShaderVariableTypeTexture index:index];
93+
}
94+
95+
-(GLSLShaderVariableDescription *)shaderVariableDescription:(GLSLShaderVariableType)type index:(UInt32)index {
96+
97+
const char *outName = nil;
98+
glslopt_basic_type outType;
99+
glslopt_precision outPrec;
100+
int outVecSize;
101+
int outMatSize;
102+
int outArraySize;
103+
int outLocation;
104+
105+
switch (type) {
106+
case GLSLShaderVariableTypeInput:
107+
glslopt_shader_get_input_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation);
108+
break;
109+
case GLSLShaderVariableTypeUniform:
110+
glslopt_shader_get_uniform_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation);
111+
break;
112+
case GLSLShaderVariableTypeTexture:
113+
glslopt_shader_get_texture_desc(_shader, index, &outName, &outType, &outPrec, &outVecSize, &outMatSize, &outArraySize, &outLocation);
114+
break;
115+
}
116+
117+
GLSLShaderVariableDescription *varDesc = [[GLSLShaderVariableDescription alloc] init];
118+
119+
varDesc.name = [NSString stringWithUTF8String:outName];
120+
varDesc.type = GLSLOptBasicType(outType);
121+
varDesc.prec = GLSLOptPrecision(outPrec);
122+
varDesc.vecSize = SInt32(outVecSize);
123+
varDesc.matSize = SInt32(outMatSize);
124+
varDesc.arraySize = SInt32(outArraySize);
125+
varDesc.location = SInt32(outLocation);
126+
127+
return varDesc;
128+
}
129+
130+
/*
131+
// Get *very* approximate shader stats:
132+
// Number of math, texture and flow control instructions.
133+
void glslopt_shader_get_stats (glslopt_shader* shader, int* approxMath, int* approxTex, int* approxFlow);
134+
*/
135+
136+
- (void)dealloc
137+
{
138+
glslopt_shader_delete(_shader);
139+
}
140+
141+
@end
142+
143+
@interface GLSLOptimizer () {
144+
@private
145+
glslopt_ctx *_ctx;
146+
}
147+
148+
@end
149+
150+
@implementation GLSLOptimizer
151+
152+
-(id)init:(GLSLOptTarget)target {
153+
self = [super init];
154+
if (self) {
155+
_ctx = glslopt_initialize(glslopt_target(target));
156+
}
157+
return self;
158+
}
159+
160+
-(void)dealloc {
161+
glslopt_cleanup(_ctx);
162+
}
163+
164+
-(void)setMaxUnrollIterations:(UInt32)iterations {
165+
glslopt_set_max_unroll_iterations(_ctx, iterations);
166+
}
167+
168+
-(GLSLShader *)optimize:(GLSLOptShaderType)shaderType shaderSource:(NSString *)shaderSource options:(NSUInteger)options {
169+
glslopt_shader* shader = glslopt_optimize(_ctx, glslopt_shader_type(shaderType), shaderSource.UTF8String, UInt32(options));
170+
171+
GLSLShader *glslShader = [[GLSLShader alloc] initWithShader: shader];
172+
173+
if ([glslShader status]) {
174+
return glslShader;
175+
} else {
176+
NSLog(@"%@", [glslShader log]);
177+
}
178+
return nil;
179+
}
180+
181+
@end
182+
183+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>testtoast.$(PRODUCT_NAME:rfc1034identifier)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>$(CURRENT_PROJECT_VERSION)</string>
23+
<key>NSPrincipalClass</key>
24+
<string></string>
25+
</dict>
26+
</plist>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// GLSLOptimiserTests.m
3+
// GLSLOptimiserTests
4+
//
5+
// Created by Ryan Walklin on 2/06/2015.
6+
//
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <XCTest/XCTest.h>
11+
12+
@interface GLSLOptimiserTests : XCTestCase
13+
14+
@end
15+
16+
@implementation GLSLOptimiserTests
17+
18+
- (void)setUp {
19+
[super setUp];
20+
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
}
22+
23+
- (void)tearDown {
24+
// Put teardown code here. This method is called after the invocation of each test method in the class.
25+
[super tearDown];
26+
}
27+
28+
- (void)testExample {
29+
// This is an example of a functional test case.
30+
XCTAssert(YES, @"Pass");
31+
}
32+
33+
- (void)testPerformanceExample {
34+
// This is an example of a performance test case.
35+
[self measureBlock:^{
36+
// Put the code you want to measure the time of here.
37+
}];
38+
}
39+
40+
@end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>testtoast.$(PRODUCT_NAME:rfc1034identifier)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
</dict>
24+
</plist>

0 commit comments

Comments
 (0)