How to design a simple GLSL wrapper for shader use

后端 未结 2 863
时光取名叫无心
时光取名叫无心 2020-12-13 05:12

UPDATE: Because I needed something right away, I\'ve created a simple shader wrapper that does the sort of thing I need. You can find it here: ShaderManager on GitHu

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 05:49

    I see this is tagged with iOS, so if you're partial to Objective-C, I'd take a good look at Jeff LaMarche's GLProgram wrapper class, which he describes here and has source available here. I've used it within my own applications to simplify some of the shader program setup, and to make the code a little cleaner.

    For example, you can set up a shader and its attributes and uniforms using code like the following:

    sphereDepthProgram = [[GLProgram alloc] initWithVertexShaderFilename:@"SphereDepth" fragmentShaderFilename:@"SphereDepth"];
    [sphereDepthProgram addAttribute:@"position"];
    [sphereDepthProgram addAttribute:@"inputImpostorSpaceCoordinate"];
    if (![sphereDepthProgram link])
    {
        NSLog(@"Depth shader link failed");
        NSString *progLog = [sphereDepthProgram programLog];
        NSLog(@"Program Log: %@", progLog); 
        NSString *fragLog = [sphereDepthProgram fragmentShaderLog];
        NSLog(@"Frag Log: %@", fragLog);
        NSString *vertLog = [sphereDepthProgram vertexShaderLog];
        NSLog(@"Vert Log: %@", vertLog);
        [sphereDepthProgram release];
        sphereDepthProgram = nil;
    }
    
    sphereDepthPositionAttribute = [sphereDepthProgram attributeIndex:@"position"];
    sphereDepthImpostorSpaceAttribute = [sphereDepthProgram attributeIndex:@"inputImpostorSpaceCoordinate"];
    sphereDepthModelViewMatrix = [sphereDepthProgram uniformIndex:@"modelViewProjMatrix"];
    sphereDepthRadius = [sphereDepthProgram uniformIndex:@"sphereRadius"];
    

    When you need to use the shader program, you then do something like the following:

    [sphereDepthProgram use];
    

    This doesn't address the issues of branching vs. individual shaders that you bring up above, but Jeff's implementation does provide a nice encapsulation of some of the OpenGL ES boilerplate shader setup code.

提交回复
热议问题