Path: blob/21.2-virgl/src/gallium/auxiliary/cso_cache/cso_cache.h
4565 views
/**************************************************************************1*2* Copyright 2007-2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* @file29* Constant State Object (CSO) cache.30*31* The basic idea is that the states are created via the32* create_state/bind_state/delete_state semantics. The driver is expected to33* perform as much of the Gallium state translation to whatever its internal34* representation is during the create call. Gallium then has a caching35* mechanism where it stores the created states. When the pipeline needs an36* actual state change, a bind call is issued. In the bind call the driver37* gets its already translated representation.38*39* Those semantics mean that the driver doesn't do the repeated translations40* of states on every frame, but only once, when a new state is actually41* created.42*43* Even on hardware that doesn't do any kind of state cache, it makes the44* driver look a lot neater, plus it avoids all the redundant state45* translations on every frame.46*47* Currently our constant state objects are:48* - alpha test49* - blend50* - depth stencil51* - fragment shader52* - rasterizer (old setup)53* - sampler54* - vertex shader55* - vertex elements56*57* Things that are not constant state objects include:58* - blend_color59* - clip_state60* - clear_color_state61* - constant_buffer62* - feedback_state63* - framebuffer_state64* - polygon_stipple65* - scissor_state66* - texture_state67* - viewport_state68*69* @author Zack Rusin <[email protected]>70*/7172#ifndef CSO_CACHE_H73#define CSO_CACHE_H7475#include "pipe/p_context.h"76#include "pipe/p_state.h"7778/* cso_hash.h is necessary for cso_hash_iter, as MSVC requires structures79* returned by value to be fully defined */80#include "cso_hash.h"818283#ifdef __cplusplus84extern "C" {85#endif8687enum cso_cache_type {88CSO_RASTERIZER,89CSO_BLEND,90CSO_DEPTH_STENCIL_ALPHA,91CSO_SAMPLER,92CSO_VELEMENTS,93CSO_CACHE_MAX,94};9596typedef void (*cso_delete_cso_callback)(void *ctx, void *state,97enum cso_cache_type type);9899typedef void (*cso_state_callback)(void *ctx, void *obj);100101typedef void (*cso_sanitize_callback)(struct cso_hash *hash,102enum cso_cache_type type,103int max_size,104void *user_data);105106struct cso_cache {107struct cso_hash hashes[CSO_CACHE_MAX];108int max_size;109110cso_sanitize_callback sanitize_cb;111void *sanitize_data;112113cso_delete_cso_callback delete_cso;114void *delete_cso_ctx;115};116117struct cso_blend {118struct pipe_blend_state state;119void *data;120};121122struct cso_depth_stencil_alpha {123struct pipe_depth_stencil_alpha_state state;124void *data;125};126127struct cso_rasterizer {128struct pipe_rasterizer_state state;129void *data;130};131132struct cso_sampler {133struct pipe_sampler_state state;134void *data;135unsigned hash_key;136};137138struct cso_velems_state {139unsigned count;140struct pipe_vertex_element velems[PIPE_MAX_ATTRIBS];141};142143struct cso_velements {144struct cso_velems_state state;145void *data;146};147148void cso_cache_init(struct cso_cache *sc, struct pipe_context *pipe);149void cso_cache_delete(struct cso_cache *sc);150151void cso_cache_set_sanitize_callback(struct cso_cache *sc,152cso_sanitize_callback cb,153void *user_data);154void cso_cache_set_delete_cso_callback(struct cso_cache *sc,155cso_delete_cso_callback delete_cso,156void *ctx);157158struct cso_hash_iter cso_insert_state(struct cso_cache *sc,159unsigned hash_key, enum cso_cache_type type,160void *state);161struct cso_hash_iter cso_find_state(struct cso_cache *sc,162unsigned hash_key, enum cso_cache_type type);163struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,164unsigned hash_key, enum cso_cache_type type,165void *templ, unsigned size);166void cso_set_maximum_cache_size(struct cso_cache *sc, int number);167void cso_delete_state(struct pipe_context *pipe, void *state,168enum cso_cache_type type);169170static inline unsigned171cso_construct_key(void *key, int key_size)172{173unsigned hash = 0, *ikey = (unsigned *)key;174unsigned num_elements = key_size / 4;175176assert(key_size % 4 == 0);177178for (unsigned i = 0; i < num_elements; i++)179hash ^= ikey[i];180181return hash;182}183184#ifdef __cplusplus185}186#endif187188#endif189190191