-
Notifications
You must be signed in to change notification settings - Fork 7
/
ShaderManager.m
182 lines (143 loc) · 3.39 KB
/
ShaderManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// ShaderManager.m
// TwoLivesLeft.com
//
// Created by Simeon Nasilowski on 29/05/11.
// Copyright 2011 Two Lives Left. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import "ShaderManager.h"
@implementation ShaderManager
static ShaderManager *sharedShaderManager;
#pragma mark - Singleton
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
sharedShaderManager = [[ShaderManager alloc] init];
}
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if (sharedShaderManager == nil)
{
sharedShaderManager = [super allocWithZone:zone];
return sharedShaderManager;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
+ (ShaderManager*) sharedManager
{
return sharedShaderManager;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax;
}
- (oneway void)release
{
}
- (id)retain
{
return sharedShaderManager;
}
- (id)autorelease
{
return sharedShaderManager;
}
#pragma mark - Initialization
- (id) init
{
self = [super init];
if(self)
{
shaderPrograms = [[NSMutableDictionary dictionary] retain];
activeProgram = 0;
}
return self;
}
#pragma mark - Memory
- (void) dealloc
{
[shaderPrograms release];
[super dealloc];
}
#pragma mark - Shader management
- (NSArray*) allShaders
{
return [shaderPrograms allValues];
}
- (Shader*) currentShader
{
return currentShader;
}
- (Shader*) useShader:(NSString*)name
{
currentShader = [self shaderForName:name];
if( activeProgram != currentShader.programHandle )
{
glUseProgram(currentShader.programHandle);
activeProgram = currentShader.programHandle;
}
return currentShader;
}
- (void) useShaderObject:(Shader*)shader
{
currentShader = shader;
if( activeProgram != currentShader.programHandle )
{
glUseProgram(currentShader.programHandle);
activeProgram = currentShader.programHandle;
}
}
- (Shader*) shaderForName:(NSString *)name
{
return [shaderPrograms objectForKey:name];
}
- (Shader*) createShader:(NSString*)name withSettings:(NSDictionary *)settings
{
Shader *shader = [[[Shader alloc] initWithShaderSettings:settings] autorelease];
Shader *existingShader = [self shaderForName:name];
if( existingShader != nil )
{
[self removeShader:name];
}
[shaderPrograms setObject:shader forKey:name];
return shader;
}
- (Shader*) createShader:(NSString*)name withFile:(NSString*)file
{
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@""]];
return [self createShader:name withSettings:dict];
}
- (void) removeAllShaders
{
[shaderPrograms removeAllObjects];
}
- (void) removeShader:(NSString*)name
{
Shader *shader = [self shaderForName:name];
if( shader )
{
if( shader.programHandle == activeProgram )
{
glUseProgram(0);
}
[shaderPrograms removeObjectForKey:name];
}
}
@end