Coco2d 2.1和Xcode 7 iOS 9崩溃了ccShader

Ben*_*lon 19 xcode cocos2d-iphone

我测试了Xcode 7,但我的cocos2d 2.1游戏在模拟器或设备上崩溃:

ccShader_PositionColorLenghtTexture_frag.h

2015-06-15 22:36:13.319 NanoWar[18789:456971] cocos2d: ERROR: 0:12: '' : syntax error: #extension must always be before any non-preprocessor tokens
Run Code Online (Sandbox Code Playgroud)

cocos2d: ERROR: 0:26: Invalid call of undeclared identifier 'fwidth'
Run Code Online (Sandbox Code Playgroud)

这堂课让游戏崩溃了

#extension GL_OES_standard_derivatives : enable                                                                             

#ifdef GL_ES                                                                                                                
varying mediump vec4 v_color;                                                                                               
varying mediump vec2 v_texcoord;                                                                                            
#else                                                                                                                       
varying vec4 v_color;                                                                                                       
varying vec2 v_texcoord;                                                                                                    
#endif                                                                                                                      

void main()                                                                                                                 
{                                                                                                                           
#ifdef GL_OES_standard_derivatives                                                                                          
#if defined GL_OES_standard_derivatives                                                                                     
    gl_FragColor = v_color*smoothstep(0.0, length(fwidth(v_texcoord)), 1.0 - length(v_texcoord));                           
#else                                                                                                                       
    gl_FragColor = v_color*step(0.0, 1.0 - length(v_texcoord));                                                             
#endif                                                                                                                      
#endif                                                                                                                      
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*lis 47

我在forum.cocos2d-objc.org上"rae" ,我也可以在这里提出我的答案,因为所有的道路都会导致StackOverflow.:-)

如果您使用较旧版本的Cocos 2D,这可能会有所帮助.我编辑了CCGLProgram.m来改变 - (BOOL)compileShader:方法的开头:

#define EXTENSION_STRING "#extension GL_OES_standard_derivatives : enable"
static NSString * g_extensionStr = @EXTENSION_STRING;

- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type byteArray:(const GLchar *)source
{
    GLint status;

    if (!source)
        return NO;

    // BEGIN workaround for Xcode 7 bug
    BOOL hasExtension = NO;
    NSString *sourceStr = [NSString stringWithUTF8String:source];
    if([sourceStr rangeOfString:g_extensionStr].location != NSNotFound) {
        hasExtension = YES;
        NSArray *strs = [sourceStr componentsSeparatedByString:g_extensionStr];
        assert(strs.count == 2);
        sourceStr = [strs componentsJoinedByString:@"\n"];
        source = (GLchar *)[sourceStr UTF8String];
    }

    const GLchar *sources[] = {
        (hasExtension ? EXTENSION_STRING "\n" : ""),
    #ifdef __CC_PLATFORM_IOS
        (type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"),
    #endif
        "uniform mat4 CC_PMatrix;\n"
        "uniform mat4 CC_MVMatrix;\n"
        "uniform mat4 CC_MVPMatrix;\n"
        "uniform vec4 CC_Time;\n"
        "uniform vec4 CC_SinTime;\n"
        "uniform vec4 CC_CosTime;\n"
        "uniform vec4 CC_Random01;\n"
        "//CC INCLUDES END\n\n",
        source,
    };
    // END workaround for Xcode 7 bug
Run Code Online (Sandbox Code Playgroud)

这是一个黑客,但它适用于Xcode 7 beta 2.希望这对谷歌搜索有用.

里德

  • 如果你也支持iOS 8.0之前的iOS版本而不仅仅确保用[sourceStr rangeOfString:g_extensionStr]替换[sourceStr containsString:g_extensionStr] .location!= NSNotFound,因为 - (BOOL)containsString:(NSString*)str NS_AVAILABLE( 10_10,8_0); (4认同)

bod*_*tta 21

接受的答案对我不起作用,但确实如此.

转到名为的文件 CCGLProgram.m

替换名为的整个方法 - (BOOL)compileShader:(CLuint *)shader type(GLenum)typebyteArray:(const GLchar *)source

用这种方法

#define _IPHONE9_0 [[[UIDevice currentDevice] systemVersion] floatValue] == 9.000

#define EXTENSION_STRING "#extension GL_OES_standard_derivatives : enable"
static NSString * g_extensionStr = @EXTENSION_STRING;


- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type byteArray:(const GLchar *)source
{
GLint status;
if (!source)
    return NO;


#ifdef _IPHONE9_0

NSLog(@"USING IOS9 shaders");
// BEGIN workaround for Xcode 7 ios9----
BOOL hasExtension = NO;
NSString *sourceStr = [NSString stringWithUTF8String:source];
if([sourceStr containsString:g_extensionStr]) {
    hasExtension = YES;
    NSArray *strs = [sourceStr componentsSeparatedByString:g_extensionStr];
    assert(strs.count == 2);
    sourceStr = [strs componentsJoinedByString:@"\n"];
    source = (GLchar *)[sourceStr UTF8String];
}

const GLchar *sources[] = {
    (hasExtension ? EXTENSION_STRING "\n" : ""),
#ifdef __CC_PLATFORM_IOS
    (type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"),
 #endif
    "uniform mat4 CC_PMatrix;\n"
    "uniform mat4 CC_MVMatrix;\n"
    "uniform mat4 CC_MVPMatrix;\n"
    "uniform vec4 CC_Time;\n"
    "uniform vec4 CC_SinTime;\n"
    "uniform vec4 CC_CosTime;\n"
    "uniform vec4 CC_Random01;\n"
    "//CC INCLUDES END\n\n",
    source,
};
#else

NSLog(@"USING IOS8 shaders");

const GLchar *sources[] = {
#ifdef __CC_PLATFORM_IOS
    (type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"),
#endif
    "uniform mat4 CC_PMatrix;\n"
    "uniform mat4 CC_MVMatrix;\n"
    "uniform mat4 CC_MVPMatrix;\n"
    "uniform vec4 CC_Time;\n"
    "uniform vec4 CC_SinTime;\n"
    "uniform vec4 CC_CosTime;\n"
    "uniform vec4 CC_Random01;\n"
    "//CC INCLUDES END\n\n",
    source,
};

#endif


*shader = glCreateShader(type);
glShaderSource(*shader, sizeof(sources)/sizeof(*sources), sources, NULL);
glCompileShader(*shader);

glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);

if( ! status ) {
    GLsizei length;
    glGetShaderiv(*shader, GL_SHADER_SOURCE_LENGTH, &length);
    GLchar src[length];

    glGetShaderSource(*shader, length, NULL, src);
    CCLOG(@"cocos2d: ERROR: Failed to compile shader:\n%s", src);

    if( type == GL_VERTEX_SHADER )
        CCLOG(@"cocos2d: %@", [self vertexShaderLog] );
    else
        CCLOG(@"cocos2d: %@", [self fragmentShaderLog] );

    abort();
}
return ( status == GL_TRUE );
}
Run Code Online (Sandbox Code Playgroud)


Aus*_*den 7

使用上面接受的答案,但对于较新版本的cocos2d:

在CCShader.m中替换行

static GLint
CompileShaderSources(GLenum type, NSArray *sources)
{
Run Code Online (Sandbox Code Playgroud)

#define EXTENSION_STRING "#extension GL_OES_standard_derivatives : enable"
static NSString * g_extensionStr = @EXTENSION_STRING;

static NSArray * PrependExtensionIfNeeded(NSArray *sources)
{
    NSMutableArray *mutableSources = [NSMutableArray array];

    BOOL hasExtension = NO;

    for (NSString *source in sources)
    {
        NSString *newSource = [source copy];
        if([source rangeOfString:g_extensionStr].location != NSNotFound)
        {
            hasExtension = YES;
            NSArray *strs = [source componentsSeparatedByString:g_extensionStr];
            newSource = [strs componentsJoinedByString:@"\n"];
        }

        [mutableSources addObject:newSource];
    }

    if (hasExtension)
    {
        NSMutableString *firstSource = [NSMutableString stringWithString:[mutableSources firstObject]];
        [firstSource insertString:[NSString stringWithFormat:@"%@\n", g_extensionStr] atIndex:0];
        [mutableSources replaceObjectAtIndex:0 withObject:firstSource];
    }

    return mutableSources;
}

static GLint
CompileShaderSources(GLenum type, NSArray *sources)
{
    sources = PrependExtensionIfNeeded(sources);
Run Code Online (Sandbox Code Playgroud)