How to design a simple GLSL wrapper for shader use

后端 未结 2 858
时光取名叫无心
时光取名叫无心 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.

    0 讨论(0)
  • 2020-12-13 05:52

    Basic Linking:

    There is no standard way here. There are at least 2 general approaches:

    1. Monolithic - one shader covers many cases, using uniform boolean switches. These branches don't hurt performance because the condition result is constant for any fragment group (actually, for all of the fragments).

    2. Multi-object program compositing - main shader declares a set of entry points (like 'get_diffuse', 'get_specular', etc), which are implemented in separate shader objects attached. This implies individual shader for each object, but any kind of caching helps.

    Setting Variables: Uniforms

    I will just describe the approach I developed.

    Each shader program has a list of uniform dictionaries. It's used to fill the uniform source list upon program (re-)linking. When the program is activated, it goes through the uniform list, fetches values from their sources and uploads them to GL. In the result, data is not directly connected with the user shader program, and whatever manages it does not care about the program using it.

    One of these dictionaries can be, for example, a core one, containing model,view transformations, camera projection and maybe something else.

    Setting Variables: Attributes

    First of all, shader program is an attribute consumer, so it is what has to extract these attributes from a mesh (or any other data storage) and upload them to GL in a way it needs. It should also make sure that types of provided attributes match the requested types.

    When using with monolithic shader approach, there is a possible unpleasant situation when one the disabled branch ways requires a vertex attribute that is not provided. I would advice using another attribute's data to supply the missing one, because we don't care about the actual values in this case.

    P.S. You can find an actual implementation of these ideas here: http://code.google.com/p/kri/

    0 讨论(0)
提交回复
热议问题