1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/************************************************************************** 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved. 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish, 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions: 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software. 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/ 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* Authors: Zack Rusin <zack@tungstengraphics.com> 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_debug.h" 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_memory.h" 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "cso_cache.h" 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "cso_hash.h" 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct cso_cache { 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hashes[CSO_CACHE_MAX]; 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int max_size; 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_sanitize_callback sanitize_cb; 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *sanitize_data; 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if 1 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic unsigned hash_key(const void *key, unsigned key_size) 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned *ikey = (unsigned *)key; 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash = 0, i; 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assert(key_size % 4 == 0); 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* I'm sure this can be improved on: 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (i = 0; i < key_size/4; i++) 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org hash ^= ikey[i]; 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return hash; 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic unsigned hash_key(const unsigned char *p, int n) 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned h = 0; 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned g; 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (n--) { 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org h = (h << 4) + *p++; 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ((g = (h & 0xf0000000)) != 0) 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org h ^= g >> 23; 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org h &= ~g; 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return h; 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgunsigned cso_construct_key(void *item, int item_size) 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return hash_key((item), item_size); 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic INLINE struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type) 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash; 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org hash = sc->hashes[type]; 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return hash; 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void delete_blend_state(void *state, void *data) 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_blend *cso = (struct cso_blend *)state; 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cso->delete_state) 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso->delete_state(cso->context, cso->data); 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void delete_depth_stencil_state(void *state, void *data) 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state; 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cso->delete_state) 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso->delete_state(cso->context, cso->data); 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void delete_sampler_state(void *state, void *data) 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_sampler *cso = (struct cso_sampler *)state; 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cso->delete_state) 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso->delete_state(cso->context, cso->data); 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void delete_rasterizer_state(void *state, void *data) 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_rasterizer *cso = (struct cso_rasterizer *)state; 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cso->delete_state) 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso->delete_state(cso->context, cso->data); 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void delete_velements(void *state, void *data) 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_velements *cso = (struct cso_velements *)state; 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cso->delete_state) 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso->delete_state(cso->context, cso->data); 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic INLINE void delete_cso(void *state, enum cso_cache_type type) 131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (type) { 133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case CSO_BLEND: 134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_blend_state(state, 0); 135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case CSO_SAMPLER: 137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_sampler_state(state, 0); 138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case CSO_DEPTH_STENCIL_ALPHA: 140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_depth_stencil_state(state, 0); 141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case CSO_RASTERIZER: 143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_rasterizer_state(state, 0); 144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case CSO_VELEMENTS: 146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_velements(state, 0); 147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: 149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assert(0); 150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(state); 151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic INLINE void sanitize_hash(struct cso_cache *sc, 156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash, 157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org enum cso_cache_type type, 158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int max_size) 159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (sc->sanitize_cb) 161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->sanitize_cb(hash, type, max_size, sc->sanitize_data); 162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic INLINE void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type, 166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int max_size, void *user_data) 167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* if we're approach the maximum size, remove fourth of the entries 169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * otherwise every subsequent call will go through the same */ 170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int hash_size = cso_hash_size(hash); 171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int max_entries = (max_size > hash_size) ? max_size : hash_size; 172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int to_remove = (max_size < max_entries) * max_entries/4; 173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (hash_size > max_size) 174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org to_remove += hash_size - max_size; 175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (to_remove) { 176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /*remove elements until we're good */ 177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /*fixme: currently we pick the nodes to remove at random*/ 178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash_iter iter = cso_hash_first_node(hash); 179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *cso = cso_hash_take(hash, cso_hash_iter_key(iter)); 180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete_cso(cso, type); 181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org --to_remove; 182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct cso_hash_iter 186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgcso_insert_state(struct cso_cache *sc, 187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash_key, enum cso_cache_type type, 188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *state) 189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash = _cso_hash_for_type(sc, type); 191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sanitize_hash(sc, hash, type, sc->max_size); 192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return cso_hash_insert(hash, hash_key, state); 194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct cso_hash_iter 197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgcso_find_state(struct cso_cache *sc, 198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash_key, enum cso_cache_type type) 199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash = _cso_hash_for_type(sc, type); 201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return cso_hash_find(hash, hash_key); 203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid *cso_hash_find_data_from_template( struct cso_hash *hash, 207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash_key, 208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *templ, 209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int size ) 210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash_iter iter = cso_hash_find(hash, hash_key); 212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (!cso_hash_iter_is_null(iter)) { 213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *iter_data = cso_hash_iter_data(iter); 214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!memcmp(iter_data, templ, size)) { 215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* We found a match 216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return iter_data; 218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org iter = cso_hash_iter_next(iter); 220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return NULL; 222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct cso_hash_iter cso_find_state_template(struct cso_cache *sc, 226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash_key, enum cso_cache_type type, 227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *templ, unsigned size) 228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash_iter iter = cso_find_state(sc, hash_key, type); 230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (!cso_hash_iter_is_null(iter)) { 231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *iter_data = cso_hash_iter_data(iter); 232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!memcmp(iter_data, templ, size)) 233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return iter; 234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org iter = cso_hash_iter_next(iter); 235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return iter; 237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid * cso_take_state(struct cso_cache *sc, 240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned hash_key, enum cso_cache_type type) 241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash = _cso_hash_for_type(sc, type); 243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return cso_hash_take(hash, hash_key); 244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct cso_cache *cso_cache_create(void) 247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_cache *sc = MALLOC_STRUCT(cso_cache); 249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int i; 250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (sc == NULL) 251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return NULL; 252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->max_size = 4096; 254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (i = 0; i < CSO_CACHE_MAX; i++) 255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->hashes[i] = cso_hash_create(); 256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->sanitize_cb = sanitize_cb; 258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->sanitize_data = 0; 259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return sc; 261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type, 264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_state_callback func, void *user_data) 265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash *hash = _cso_hash_for_type(sc, type); 267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct cso_hash_iter iter; 268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org iter = cso_hash_first_node(hash); 270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (!cso_hash_iter_is_null(iter)) { 271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *state = cso_hash_iter_data(iter); 272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org iter = cso_hash_iter_next(iter); 273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (state) { 274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org func(state, user_data); 275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid cso_cache_delete(struct cso_cache *sc) 280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int i; 282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assert(sc); 283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!sc) 285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* delete driver data */ 288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0); 289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0); 290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0); 291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0); 292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0); 293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (i = 0; i < CSO_CACHE_MAX; i++) 295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_hash_delete(sc->hashes[i]); 296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FREE(sc); 298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid cso_set_maximum_cache_size(struct cso_cache *sc, int number) 301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int i; 303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->max_size = number; 305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (i = 0; i < CSO_CACHE_MAX; i++) 307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sanitize_hash(sc, sc->hashes[i], i, sc->max_size); 308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint cso_maximum_cache_size(const struct cso_cache *sc) 311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return sc->max_size; 313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid cso_cache_set_sanitize_callback(struct cso_cache *sc, 316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cso_sanitize_callback cb, 317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *user_data) 318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->sanitize_cb = cb; 320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sc->sanitize_data = user_data; 321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 323