Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simsaens committed May 29, 2011
0 parents commit 4e399e8
Show file tree
Hide file tree
Showing 21 changed files with 1,829 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.DS_Store
*~
build
Build
Products
xcuserdata/
*.pbxuser
*.perspectivev3
*.mode1v3
*.xcworkspace
config
.svn
*.pyc
.DS_STORE
*~
build/*
*.pbxuser
*.perspectivev3
.svn
37 changes: 37 additions & 0 deletions Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Shader.h
// 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 <Foundation/Foundation.h>

#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>

@interface Shader : NSObject
{
GLuint programHandle;

NSMutableDictionary *attributeHandles;
NSMutableDictionary *uniformHandles;
}

@property (nonatomic,readonly) GLuint programHandle;

- (id) initWithShaderSettings:(NSDictionary*)settings;

- (int) uniformLocation:(NSString*)name;
- (GLuint) attributeHandle:(NSString*)name;

@end
207 changes: 207 additions & 0 deletions Shader.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//
// Shader.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 "Shader.h"

@implementation Shader

@synthesize programHandle;

#pragma mark - Shader compiling and linking

- (BOOL)compileShader:(GLuint *)shader file:(NSString *)file
{
GLint status;
const GLchar *source;

GLenum type = GL_VERTEX_SHADER;

NSString *extension = [file pathExtension];
if( [extension isEqualToString:@"vsh"] )
{
type = GL_VERTEX_SHADER;
}
else if( [extension isEqualToString:@"fsh"] )
{
type = GL_FRAGMENT_SHADER;
}
else
{
NSLog(@"Unknown shader file extension");
return FALSE;
}

source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (!source)
{
NSLog(@"Failed to load vertex shader");
return FALSE;
}

*shader = glCreateShader(type);
glShaderSource(*shader, 1, &source, NULL);
glCompileShader(*shader);

#if defined(DEBUG)
GLint logLength;
glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(*shader, logLength, &logLength, log);
NSLog(@"Shader compile log:\n%s", log);
free(log);
}
#endif

glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
if (status == 0)
{
glDeleteShader(*shader);
return FALSE;
}

return TRUE;
}

- (BOOL)linkProgram:(GLuint)prog
{
GLint status;

glLinkProgram(prog);

#if defined(DEBUG)
GLint logLength;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log);
free(log);
}
#endif

glGetProgramiv(prog, GL_LINK_STATUS, &status);
if (status == 0)
return FALSE;

return TRUE;
}

#pragma mark - Initialization

- (id) initWithShaderSettings:(NSDictionary*)settings
{
self = [super init];
if( self )
{
NSArray *shaderFiles = [settings objectForKey:@"Files"];
NSArray *attributes = [settings objectForKey:@"Attributes"];
NSArray *uniforms = [settings objectForKey:@"Uniforms"];

attributeHandles = [[NSMutableDictionary dictionary] retain];
uniformHandles = [[NSMutableDictionary dictionary] retain];

programHandle = glCreateProgram();

NSMutableArray *shaderList = [NSMutableArray array];

for( NSString *file in shaderFiles )
{
GLuint shader;

if( ![self compileShader:&shader file:[[NSBundle mainBundle] pathForResource:file ofType:@""]] )
{
NSLog(@"Failed to compile shader: %@", file);
}

[shaderList addObject:[NSNumber numberWithInt:shader]];
}

for( NSNumber *shader in shaderList )
{
//Attach each shader to the program
glAttachShader( programHandle, [shader unsignedIntValue] );
}

GLuint attribLoc = 1;
for( NSString *attribute in attributes )
{
glBindAttribLocation(programHandle, attribLoc, [attribute UTF8String]);

[attributeHandles setObject:[NSNumber numberWithInt:attribLoc] forKey:attribute];

attribLoc += 1;
}

if( ![self linkProgram:programHandle] )
{
NSLog(@"Failed to link program: %d", programHandle);

for( NSNumber *shader in shaderList )
{
if( [shader unsignedIntValue] )
{
glDeleteShader([shader unsignedIntValue]);
}
}

if (programHandle)
{
glDeleteProgram(programHandle);
programHandle = 0;
}
}

if( programHandle )
{
for( NSString *uniform in uniforms )
{
int uniformHandle = glGetUniformLocation(programHandle, [uniform UTF8String]);

[uniformHandles setObject:[NSNumber numberWithInt:uniformHandle] forKey:uniform];
}
}
}
return self;
}

- (void) dealloc
{
if( programHandle )
{
glDeleteProgram(programHandle);
programHandle = 0;
}

[attributeHandles release];
[uniformHandles release];

[super dealloc];
}

#pragma mark - Access to uniforms and attributes

- (int) uniformLocation:(NSString*)name
{
return [[uniformHandles objectForKey:name] intValue];
}

- (GLuint) attributeHandle:(NSString*)name
{
return [[attributeHandles objectForKey:name] unsignedIntValue];
}

@end
43 changes: 43 additions & 0 deletions ShaderManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// ShaderManager.h
// 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 <Foundation/Foundation.h>

#import "Shader.h"

#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>

@interface ShaderManager : NSObject
{
NSMutableDictionary *shaderPrograms;

GLuint activeProgram;
Shader *currentShader;
}

+ (ShaderManager*) sharedManager;

- (Shader*) currentShader;
- (Shader*) useShader:(NSString*)name;
- (Shader*) shaderForName:(NSString*)name;

- (Shader*) createShader:(NSString*)name withSettings:(NSDictionary*)settings;
- (Shader*) createShader:(NSString*)name withSettingsFile:(NSString*)file;

- (void) removeAllShaders;

@end
Loading

0 comments on commit 4e399e8

Please sign in to comment.