Changeset 670


Ignore:
Timestamp:
Feb 20, 2011, 1:02:42 PM (9 years ago)
Author:
sam
Message:

Cache shader source CRCs to avoid costly recompilations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/shader.cpp

    r668 r670  
    3939private:
    4040    GLuint prog_id, vert_id, frag_id;
     41    uint32_t vert_crc, frag_crc;
     42
     43    /* Global shader cache */
     44    static Shader *shaders[];
     45    static int nshaders;
    4146};
     47
     48Shader *ShaderData::shaders[256];
     49int ShaderData::nshaders = 0;
    4250
    4351/*
     
    4755Shader *Shader::Create(char const *vert, char const *frag)
    4856{
    49     return new Shader(vert, frag);
     57    uint32_t new_vert_crc = Hash::Crc32(vert);
     58    uint32_t new_frag_crc = Hash::Crc32(frag);
     59
     60    for (int n = 0; n < ShaderData::nshaders; n++)
     61    {
     62        if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc
     63             && ShaderData::shaders[n]->data->frag_crc == new_frag_crc)
     64            return ShaderData::shaders[n];
     65    }
     66
     67    Shader *ret = new Shader(vert, frag);
     68    ShaderData::shaders[ShaderData::nshaders] = ret;
     69    ShaderData::nshaders++;
     70    return ret;
    5071}
    5172
    5273void Shader::Destroy(Shader *shader)
    5374{
    54     delete shader;
     75    /* XXX: do nothing! the shader should remain in cache */
     76    (void)shader;
    5577}
    5678
     
    6183    GLsizei len;
    6284
     85    data->vert_crc = Hash::Crc32(vert);
    6386    data->vert_id = glCreateShader(GL_VERTEX_SHADER);
    6487    glShaderSource(data->vert_id, 1, &vert, NULL);
     
    6992        fprintf(stderr, "ERROR: failed to compile vertex shader: %s", buf);
    7093
     94    data->frag_crc = Hash::Crc32(frag);
    7195    data->frag_id = glCreateShader(GL_FRAGMENT_SHADER);
    7296    glShaderSource(data->frag_id, 1, &frag, NULL);
Note: See TracChangeset for help on using the changeset viewer.