cso_cache.c File Reference

Include dependency graph for cso_cache.c:

Go to the source code of this file.

Data Structures

struct  cso_cache

Functions

static unsigned hash_key (const void *key, unsigned key_size)
unsigned cso_construct_key (void *item, int item_size)
static struct cso_hash_cso_hash_for_type (struct cso_cache *sc, enum cso_cache_type type)
static int _cso_size_for_type (enum cso_cache_type type)
static void delete_blend_state (void *state, void *data)
static void delete_depth_stencil_state (void *state, void *data)
static void delete_sampler_state (void *state, void *data)
static void delete_rasterizer_state (void *state, void *data)
static void delete_fs_state (void *state, void *data)
static void delete_vs_state (void *state, void *data)
static void delete_cso (void *state, enum cso_cache_type type)
static void sanitize_hash (struct cso_cache *sc, struct cso_hash *hash, enum cso_cache_type type, int max_size)
static void sanitize_cb (struct cso_hash *hash, enum cso_cache_type type, int max_size, void *user_data)
struct cso_hash_iter cso_insert_state (struct cso_cache *sc, unsigned hash_key, enum cso_cache_type type, void *state)
struct cso_hash_iter cso_find_state (struct cso_cache *sc, unsigned hash_key, enum cso_cache_type type)
void * cso_hash_find_data_from_template (struct cso_hash *hash, unsigned hash_key, void *templ, int size)
 Convenience routine to iterate over the collision list while doing a memory comparison to see which entry in the list is a direct copy of our template and returns that entry.
struct cso_hash_iter cso_find_state_template (struct cso_cache *sc, unsigned hash_key, enum cso_cache_type type, void *templ)
void * cso_take_state (struct cso_cache *sc, unsigned hash_key, enum cso_cache_type type)
struct cso_cachecso_cache_create (void)
void cso_for_each_state (struct cso_cache *sc, enum cso_cache_type type, cso_state_callback func, void *user_data)
void cso_cache_delete (struct cso_cache *sc)
void cso_set_maximum_cache_size (struct cso_cache *sc, int number)
int cso_maximum_cache_size (const struct cso_cache *sc)
void cso_cache_set_sanitize_callback (struct cso_cache *sc, cso_sanitize_callback cb, void *user_data)


Function Documentation

static struct cso_hash* _cso_hash_for_type ( struct cso_cache sc,
enum cso_cache_type  type 
) [static, read]

Definition at line 87 of file cso_cache.c.

References cso_cache::blend_hash, CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, CSO_FRAGMENT_SHADER, CSO_RASTERIZER, CSO_SAMPLER, CSO_VERTEX_SHADER, cso_cache::depth_stencil_hash, cso_cache::fs_hash, cso_cache::rasterizer_hash, cso_cache::sampler_hash, and cso_cache::vs_hash.

00089 {
00090    struct cso_hash *hash = 0;
00091 
00092    switch(type) {
00093    case CSO_BLEND:
00094       hash = sc->blend_hash;
00095       break;
00096    case CSO_SAMPLER:
00097       hash = sc->sampler_hash;
00098       break;
00099    case CSO_DEPTH_STENCIL_ALPHA:
00100       hash = sc->depth_stencil_hash;
00101       break;
00102    case CSO_RASTERIZER:
00103       hash = sc->rasterizer_hash;
00104       break;
00105    case CSO_FRAGMENT_SHADER:
00106       hash = sc->fs_hash;
00107       break;
00108    case CSO_VERTEX_SHADER:
00109       hash = sc->vs_hash;
00110       break;
00111    }
00112 
00113    return hash;

static int _cso_size_for_type ( enum cso_cache_type  type  )  [static]

Definition at line 115 of file cso_cache.c.

References CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, CSO_FRAGMENT_SHADER, CSO_RASTERIZER, CSO_SAMPLER, and CSO_VERTEX_SHADER.

00117 {
00118    switch(type) {
00119    case CSO_BLEND:
00120       return sizeof(struct pipe_blend_state);
00121    case CSO_SAMPLER:
00122       return sizeof(struct pipe_sampler_state);
00123    case CSO_DEPTH_STENCIL_ALPHA:
00124       return sizeof(struct pipe_depth_stencil_alpha_state);
00125    case CSO_RASTERIZER:
00126       return sizeof(struct pipe_rasterizer_state);
00127    case CSO_FRAGMENT_SHADER:
00128       return sizeof(struct pipe_shader_state);
00129    case CSO_VERTEX_SHADER:
00130       return sizeof(struct pipe_shader_state);
00131    }
00132    return 0;

struct cso_cache* cso_cache_create ( void   )  [read]

Definition at line 304 of file cso_cache.c.

References cso_cache::blend_hash, cso_hash_create(), cso_cache::depth_stencil_hash, cso_cache::fs_hash, MALLOC_STRUCT, cso_cache::max_size, cso_cache::rasterizer_hash, cso_cache::sampler_hash, sanitize_cb(), cso_cache::sanitize_cb, cso_cache::sanitize_data, and cso_cache::vs_hash.

00306 {
00307    struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
00308    if (sc == NULL)
00309       return NULL;
00310 
00311    sc->max_size           = 4096;
00312    sc->blend_hash         = cso_hash_create();
00313    sc->sampler_hash       = cso_hash_create();
00314    sc->depth_stencil_hash = cso_hash_create();
00315    sc->rasterizer_hash    = cso_hash_create();
00316    sc->fs_hash            = cso_hash_create();
00317    sc->vs_hash            = cso_hash_create();
00318    sc->sanitize_cb        = sanitize_cb;
00319    sc->sanitize_data      = 0;
00320 
00321    return sc;

void cso_cache_delete ( struct cso_cache sc  ) 

Definition at line 360 of file cso_cache.c.

References assert, cso_cache::blend_hash, CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, cso_for_each_state(), CSO_FRAGMENT_SHADER, cso_hash_delete(), CSO_RASTERIZER, CSO_SAMPLER, CSO_VERTEX_SHADER, delete_blend_state(), delete_depth_stencil_state(), delete_fs_state(), delete_rasterizer_state(), delete_sampler_state(), delete_vs_state(), cso_cache::depth_stencil_hash, FREE, cso_cache::fs_hash, cso_cache::rasterizer_hash, cso_cache::sampler_hash, and cso_cache::vs_hash.

void cso_cache_set_sanitize_callback ( struct cso_cache sc,
cso_sanitize_callback  cb,
void *  user_data 
)

Definition at line 398 of file cso_cache.c.

References cso_cache::sanitize_cb, and cso_cache::sanitize_data.

00402 {
00403    sc->sanitize_cb   = cb;
00404    sc->sanitize_data = user_data;

unsigned cso_construct_key ( void *  item,
int  item_size 
)

Definition at line 82 of file cso_cache.c.

References hash_key().

00084 {
00085    return hash_key((item), item_size);

struct cso_hash_iter cso_find_state ( struct cso_cache sc,
unsigned  hash_key,
enum cso_cache_type  type 
) [read]

Definition at line 254 of file cso_cache.c.

References _cso_hash_for_type(), and cso_hash_find().

00257 {
00258    struct cso_hash *hash = _cso_hash_for_type(sc, type);
00259 
00260    return cso_hash_find(hash, hash_key);

struct cso_hash_iter cso_find_state_template ( struct cso_cache sc,
unsigned  hash_key,
enum cso_cache_type  type,
void *  templ 
) [read]

Definition at line 282 of file cso_cache.c.

References _cso_size_for_type(), cso_find_state(), cso_hash_iter_data(), cso_hash_iter_is_null(), and cso_hash_iter_next().

00286 {
00287    struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
00288    int size = _cso_size_for_type(type);
00289    while (!cso_hash_iter_is_null(iter)) {
00290       void *iter_data = cso_hash_iter_data(iter);
00291       if (!memcmp(iter_data, templ, size))
00292          return iter;
00293       iter = cso_hash_iter_next(iter);
00294    }
00295    return iter;

void cso_for_each_state ( struct cso_cache sc,
enum cso_cache_type  type,
cso_state_callback  func,
void *  user_data 
)

Definition at line 323 of file cso_cache.c.

References cso_cache::blend_hash, CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, CSO_FRAGMENT_SHADER, cso_hash_first_node(), cso_hash_iter_data(), cso_hash_iter_is_null(), cso_hash_iter_next(), CSO_RASTERIZER, CSO_SAMPLER, CSO_VERTEX_SHADER, cso_cache::depth_stencil_hash, cso_cache::fs_hash, cso_cache::rasterizer_hash, cso_cache::sampler_hash, and cso_cache::vs_hash.

00326 {
00327    struct cso_hash *hash = 0;
00328    struct cso_hash_iter iter;
00329 
00330    switch (type) {
00331    case CSO_BLEND:
00332       hash = sc->blend_hash;
00333       break;
00334    case CSO_SAMPLER:
00335       hash = sc->sampler_hash;
00336       break;
00337    case CSO_DEPTH_STENCIL_ALPHA:
00338       hash = sc->depth_stencil_hash;
00339       break;
00340    case CSO_RASTERIZER:
00341       hash = sc->rasterizer_hash;
00342       break;
00343    case CSO_FRAGMENT_SHADER:
00344       hash = sc->fs_hash;
00345       break;
00346    case CSO_VERTEX_SHADER:
00347       hash = sc->vs_hash;
00348       break;
00349    }
00350 
00351    iter = cso_hash_first_node(hash);
00352    while (!cso_hash_iter_is_null(iter)) {
00353       void *state = cso_hash_iter_data(iter);
00354       iter = cso_hash_iter_next(iter);
00355       if (state) {
00356          func(state, user_data);
00357       }
00358    }

void* cso_hash_find_data_from_template ( struct cso_hash hash,
unsigned  hash_key,
void *  templ,
int  size 
)

Convenience routine to iterate over the collision list while doing a memory comparison to see which entry in the list is a direct copy of our template and returns that entry.

Definition at line 263 of file cso_cache.c.

References cso_hash_find(), cso_hash_iter_data(), cso_hash_iter_is_null(), and cso_hash_iter_next().

00268 {
00269    struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
00270    while (!cso_hash_iter_is_null(iter)) {
00271       void *iter_data = cso_hash_iter_data(iter);
00272       if (!memcmp(iter_data, templ, size)) {
00273          /* We found a match
00274           */
00275          return iter_data;
00276       }
00277       iter = cso_hash_iter_next(iter);
00278    }
00279    return NULL;

struct cso_hash_iter cso_insert_state ( struct cso_cache sc,
unsigned  hash_key,
enum cso_cache_type  type,
void *  state 
) [read]

Definition at line 243 of file cso_cache.c.

References _cso_hash_for_type(), cso_hash_insert(), and sanitize_hash().

00247 {
00248    struct cso_hash *hash = _cso_hash_for_type(sc, type);
00249    sanitize_hash(sc, hash, type, sc->max_size);
00250 
00251    return cso_hash_insert(hash, hash_key, state);

int cso_maximum_cache_size ( const struct cso_cache sc  ) 

Definition at line 393 of file cso_cache.c.

References cso_cache::max_size.

00395 {
00396    return sc->max_size;

void cso_set_maximum_cache_size ( struct cso_cache sc,
int  number 
)

Definition at line 380 of file cso_cache.c.

References cso_cache::blend_hash, CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, CSO_FRAGMENT_SHADER, CSO_RASTERIZER, CSO_SAMPLER, CSO_VERTEX_SHADER, cso_cache::depth_stencil_hash, cso_cache::fs_hash, cso_cache::max_size, cso_cache::rasterizer_hash, cso_cache::sampler_hash, sanitize_hash(), and cso_cache::vs_hash.

void* cso_take_state ( struct cso_cache sc,
unsigned  hash_key,
enum cso_cache_type  type 
)

Definition at line 297 of file cso_cache.c.

References _cso_hash_for_type(), and cso_hash_take().

00300 {
00301    struct cso_hash *hash = _cso_hash_for_type(sc, type);
00302    return cso_hash_take(hash, hash_key);

static void delete_blend_state ( void *  state,
void *  data 
) [static]

Definition at line 135 of file cso_cache.c.

References cso_blend::context, cso_blend::data, cso_blend::delete_state, and FREE.

00137 {
00138    struct cso_blend *cso = (struct cso_blend *)state;
00139    if (cso->delete_state)
00140       cso->delete_state(cso->context, cso->data);
00141    FREE(state);

static void delete_cso ( void *  state,
enum cso_cache_type  type 
) [static]

Definition at line 184 of file cso_cache.c.

References assert, CSO_BLEND, CSO_DEPTH_STENCIL_ALPHA, CSO_FRAGMENT_SHADER, CSO_RASTERIZER, CSO_SAMPLER, CSO_VERTEX_SHADER, delete_blend_state(), delete_depth_stencil_state(), delete_fs_state(), delete_rasterizer_state(), delete_sampler_state(), delete_vs_state(), and FREE.

00186 {
00187    switch (type) {
00188    case CSO_BLEND:
00189       delete_blend_state(state, 0);
00190       break;
00191    case CSO_SAMPLER:
00192       delete_sampler_state(state, 0);
00193       break;
00194    case CSO_DEPTH_STENCIL_ALPHA:
00195       delete_depth_stencil_state(state, 0);
00196       break;
00197    case CSO_RASTERIZER:
00198       delete_rasterizer_state(state, 0);
00199       break;
00200    case CSO_FRAGMENT_SHADER:
00201       delete_fs_state(state, 0);
00202       break;
00203    case CSO_VERTEX_SHADER:
00204       delete_vs_state(state, 0);
00205       break;
00206    default:
00207       assert(0);
00208       FREE(state);
00209    }

static void delete_depth_stencil_state ( void *  state,
void *  data 
) [static]

Definition at line 143 of file cso_cache.c.

References cso_depth_stencil_alpha::context, cso_depth_stencil_alpha::data, cso_depth_stencil_alpha::delete_state, and FREE.

00145 {
00146    struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
00147    if (cso->delete_state)
00148       cso->delete_state(cso->context, cso->data);
00149    FREE(state);

static void delete_fs_state ( void *  state,
void *  data 
) [static]

Definition at line 167 of file cso_cache.c.

References cso_fragment_shader::context, cso_fragment_shader::data, cso_fragment_shader::delete_state, and FREE.

00169 {
00170    struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
00171    if (cso->delete_state)
00172       cso->delete_state(cso->context, cso->data);
00173    FREE(state);

static void delete_rasterizer_state ( void *  state,
void *  data 
) [static]

Definition at line 159 of file cso_cache.c.

References cso_rasterizer::context, cso_rasterizer::data, cso_rasterizer::delete_state, and FREE.

00161 {
00162    struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
00163    if (cso->delete_state)
00164       cso->delete_state(cso->context, cso->data);
00165    FREE(state);

static void delete_sampler_state ( void *  state,
void *  data 
) [static]

Definition at line 151 of file cso_cache.c.

References cso_sampler::context, cso_sampler::data, cso_sampler::delete_state, and FREE.

00153 {
00154    struct cso_sampler *cso = (struct cso_sampler *)state;
00155    if (cso->delete_state)
00156       cso->delete_state(cso->context, cso->data);
00157    FREE(state);

static void delete_vs_state ( void *  state,
void *  data 
) [static]

Definition at line 175 of file cso_cache.c.

References cso_vertex_shader::context, cso_vertex_shader::data, cso_vertex_shader::delete_state, and FREE.

00177 {
00178    struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
00179    if (cso->delete_state)
00180       cso->delete_state(cso->context, cso->data);
00181    FREE(state);

static unsigned hash_key ( const void *  key,
unsigned  key_size 
) [static]

Definition at line 53 of file cso_cache.c.

References assert.

00054 {
00055    unsigned *ikey = (unsigned *)key;
00056    unsigned hash = 0, i;
00057 
00058    assert(key_size % 4 == 0);
00059 
00060    /* I'm sure this can be improved on:
00061     */
00062    for (i = 0; i < key_size/4; i++)
00063       hash ^= ikey[i];
00064 
00065    return hash;
00066 }

static void sanitize_cb ( struct cso_hash hash,
enum cso_cache_type  type,
int  max_size,
void *  user_data 
) [static]

Definition at line 222 of file cso_cache.c.

References cso_hash_first_node(), cso_hash_iter_key(), cso_hash_size(), cso_hash_take(), and delete_cso().

00225 {
00226    /* if we're approach the maximum size, remove fourth of the entries
00227     * otherwise every subsequent call will go through the same */
00228    int hash_size = cso_hash_size(hash);
00229    int max_entries = (max_size > hash_size) ? max_size : hash_size;
00230    int to_remove =  (max_size < max_entries) * max_entries/4;
00231    if (hash_size > max_size)
00232       to_remove += hash_size - max_size;
00233    while (to_remove) {
00234       /*remove elements until we're good */
00235       /*fixme: currently we pick the nodes to remove at random*/
00236       struct cso_hash_iter iter = cso_hash_first_node(hash);
00237       void  *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
00238       delete_cso(cso, type);
00239       --to_remove;
00240    }

static void sanitize_hash ( struct cso_cache sc,
struct cso_hash hash,
enum cso_cache_type  type,
int  max_size 
) [static]

Definition at line 212 of file cso_cache.c.

References cso_cache::sanitize_cb, and cso_cache::sanitize_data.

00217 {
00218    if (sc->sanitize_cb)
00219       sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);


Generated on Tue Sep 29 06:25:18 2009 for Gallium3D by  doxygen 1.5.4