brw_wm_sampler_state.c revision 34474fa4119378ef9fbb9fb557cc19c0a1ca1f7e
19f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* 29f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Copyright (C) Intel Corp. 2006. All Rights Reserved. 39f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to 49f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt develop this 3D driver. 59f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 69f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Permission is hereby granted, free of charge, to any person obtaining 79f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt a copy of this software and associated documentation files (the 89f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt "Software"), to deal in the Software without restriction, including 99f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt without limitation the rights to use, copy, modify, merge, publish, 109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt distribute, sublicense, and/or sell copies of the Software, and to 119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt permit persons to whom the Software is furnished to do so, subject to 129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt the following conditions: 139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt The above copyright notice and this permission notice (including the 159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt next paragraph) shall be included in all copies or substantial 169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt portions of the Software. 179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt **********************************************************************/ 279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* 289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * Authors: 299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * Keith Whitwell <keith@tungstengraphics.com> 309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_context.h" 349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_state.h" 359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_defines.h" 369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 37ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/macros.h" 389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* Samplers aren't strictly wm state from the hardware's perspective, 429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * but that is the only situation in which we use them in this driver. 439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* The brw (and related graphics cores) do not support GL_CLAMP. The 489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * Intel drivers for "other operating systems" implement GL_CLAMP as 499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * GL_CLAMP_TO_EDGE, so the same is done here. 509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic GLuint translate_wrap_mode( GLenum wrap ) 529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{ 539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt switch( wrap ) { 549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_REPEAT: 559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt return BRW_TEXCOORDMODE_WRAP; 569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_CLAMP: 57ab999608582534bb5187a786b2ea437167f2d8a4Xiang, Haihao return BRW_TEXCOORDMODE_CLAMP; 589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_CLAMP_TO_EDGE: 599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt return BRW_TEXCOORDMODE_CLAMP; /* conform likes it this way */ 609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_CLAMP_TO_BORDER: 619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt return BRW_TEXCOORDMODE_CLAMP_BORDER; 629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_MIRRORED_REPEAT: 639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt return BRW_TEXCOORDMODE_MIRROR; 649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt default: 659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt return BRW_TEXCOORDMODE_WRAP; 669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt} 689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 6934474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholtstatic drm_intel_bo *upload_default_color( struct brw_context *brw, 7038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt const GLfloat *color ) 719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{ 729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt struct brw_sampler_default_color sdc; 739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt COPY_4V(sdc.color, color); 759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 768395da2e8af40367714c70afe299568272f36cc8Eric Anholt return brw_cache_data(&brw->cache, BRW_SAMPLER_DEFAULT_COLOR, 778395da2e8af40367714c70afe299568272f36cc8Eric Anholt &sdc, sizeof(sdc), NULL, 0); 789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt} 799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 813e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstruct wm_sampler_key { 823e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt int sampler_count; 833e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 843e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct wm_sampler_entry { 856f915b10d5963466567ea5445631192fd9c4e802Brian Paul GLenum tex_target; 863e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt GLenum wrap_r, wrap_s, wrap_t; 873e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt float maxlod, minlod; 883e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt float lod_bias; 893e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt float max_aniso; 903e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt GLenum minfilter, magfilter; 917086df58688dc375ffd4c0fb9a9884eae05a6e46Xiang, Haihao GLenum comparemode, comparefunc; 92e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick 93e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick /** If target is cubemap, take context setting. 94e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick */ 95e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick GLboolean seamless_cube_map; 963e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt } sampler[BRW_MAX_TEX_UNIT]; 973e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt}; 983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 993e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/** 1003e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * Sets the sampler state for a single unit based off of the sampler key 1013e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * entry. 1029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 1033e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void brw_update_sampler_state(struct wm_sampler_entry *key, 10434474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt drm_intel_bo *sdc_bo, 1053e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct brw_sampler_state *sampler) 1063e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{ 10726f8fad1456fdc2b352cea9d3b4c32cb5f6ae947Kenneth Graunke memset(sampler, 0, sizeof(*sampler)); 1089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 1093e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt switch (key->minfilter) { 1109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_NEAREST: 1119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST; 1129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_NONE; 1139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_LINEAR: 1159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR; 1169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_NONE; 1179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_NEAREST_MIPMAP_NEAREST: 1199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST; 1209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST; 1219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_LINEAR_MIPMAP_NEAREST: 1239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR; 1249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST; 1259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_NEAREST_MIPMAP_LINEAR: 1279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST; 1289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR; 1299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_LINEAR_MIPMAP_LINEAR: 1319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR; 1329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR; 1339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt default: 1359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 1379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 1389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* Set Anisotropy: 1399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 1403e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (key->max_aniso > 1.0) { 1419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC; 1429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC; 1439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 1443e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (key->max_aniso > 2.0) { 14550853be894aa3edd1e9271f7d625f319209e340fRoland Scheidegger sampler->ss3.max_aniso = MIN2((key->max_aniso - 2) / 2, 1469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt BRW_ANISORATIO_16); 1479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 1489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 1499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt else { 1503e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt switch (key->magfilter) { 1519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_NEAREST: 1529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST; 1539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt case GL_LINEAR: 1559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR; 1569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt default: 1589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt break; 1599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 1609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 1619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 162e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.r_wrap_mode = translate_wrap_mode(key->wrap_r); 163e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.s_wrap_mode = translate_wrap_mode(key->wrap_s); 164e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.t_wrap_mode = translate_wrap_mode(key->wrap_t); 165e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick 166e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick /* Cube-maps on 965 and later must use the same wrap mode for all 3 167e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick * coordinate dimensions. Futher, only CUBE and CLAMP are valid. 168e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick */ 169e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick if (key->tex_target == GL_TEXTURE_CUBE_MAP) { 170e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick if (key->seamless_cube_map && 171e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick (key->minfilter != GL_NEAREST || key->magfilter != GL_NEAREST)) { 172e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE; 173e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE; 174e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE; 175e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick } else { 176e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP; 177e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP; 178e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP; 179e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick } 180e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick } else if (key->tex_target == GL_TEXTURE_1D) { 181ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison /* There's a bug in 1D texture sampling - it actually pays 182ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison * attention to the wrap_t value, though it should not. 183ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison * Override the wrap_t value here to GL_REPEAT to keep 184ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison * any nonexistent border pixels from floating in. 185ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison */ 186ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP; 187ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison } 188e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick 1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 1909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* Set shadow function: 1919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 1923e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (key->comparemode == GL_COMPARE_R_TO_TEXTURE_ARB) { 1939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* Shadowing is "enabled" by emitting a particular sampler 1949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * message (sample_c). So need to recompile WM program when 1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * shadow comparison is enabled on each/any texture unit. 1969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 1973e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt sampler->ss0.shadow_function = 1987086df58688dc375ffd4c0fb9a9884eae05a6e46Xiang, Haihao intel_translate_shadow_compare_func(key->comparefunc); 1999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 2009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* Set LOD bias: 2029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 2033e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt sampler->ss0.lod_bias = S_FIXED(CLAMP(key->lod_bias, -16, 15), 6); 2049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.lod_preclamp = 1; /* OpenGL mode */ 2069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */ 2079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /* Set BaseMipLevel, MaxLOD, MinLOD: 2099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * 2109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * XXX: I don't think that using firstLevel, lastLevel works, 2119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * because we always setup the surface state as if firstLevel == 2129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * level zero. Probably have to subtract firstLevel from each of 2139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * these: 2149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */ 2159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt sampler->ss0.base_level = U_FIXED(0, 1); 2169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 217374447244165b3b79892cb6840a76ea4c2f21f1eEric Anholt sampler->ss1.max_lod = U_FIXED(CLAMP(key->maxlod, 0, 13), 6); 218374447244165b3b79892cb6840a76ea4c2f21f1eEric Anholt sampler->ss1.min_lod = U_FIXED(CLAMP(key->minlod, 0, 13), 6); 2199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 22038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt sampler->ss2.default_color_pointer = sdc_bo->offset >> 5; /* reloc */ 2219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt} 2229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2238c093a1fb0843e152d2a515c5127ccd5999b0d94Brian Paul 2243e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/** Sets up the cache key for sampler state for all texture units */ 2253e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void 2263e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtbrw_wm_sampler_populate_key(struct brw_context *brw, 2273e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct wm_sampler_key *key) 2289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{ 229052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt GLcontext *ctx = &brw->intel.ctx; 2303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt int unit; 231da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock char *last_entry_end = ((char*)&key->sampler_count) + 232da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock sizeof(key->sampler_count); 2333e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 2348c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt key->sampler_count = 0; 2359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) { 237052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt if (ctx->Texture.Unit[unit]._ReallyEnabled) { 2383e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct wm_sampler_entry *entry = &key->sampler[unit]; 239052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 2409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt struct gl_texture_object *texObj = texUnit->_Current; 2417216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt struct intel_texture_object *intelObj = intel_texture_object(texObj); 2427216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt struct gl_texture_image *firstImage = 2437216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt texObj->Image[0][intelObj->firstLevel]; 2446f915b10d5963466567ea5445631192fd9c4e802Brian Paul 245da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock memset(last_entry_end, 0, 246da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock (char*)entry - last_entry_end + sizeof(*entry)); 247da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock last_entry_end = ((char*)entry) + sizeof(*entry); 2488c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt 2496f915b10d5963466567ea5445631192fd9c4e802Brian Paul entry->tex_target = texObj->Target; 250e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick 251e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick entry->seamless_cube_map = (texObj->Target == GL_TEXTURE_CUBE_MAP) 252e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick ? ctx->Texture.CubeMapSeamless : GL_FALSE; 2539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2543e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->wrap_r = texObj->WrapR; 2553e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->wrap_s = texObj->WrapS; 2563e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->wrap_t = texObj->WrapT; 2579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 2583e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->maxlod = texObj->MaxLod; 2593e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->minlod = texObj->MinLod; 2603e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->lod_bias = texUnit->LodBias + texObj->LodBias; 2613e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->max_aniso = texObj->MaxAnisotropy; 2623e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->minfilter = texObj->MinFilter; 2633e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->magfilter = texObj->MagFilter; 2643e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt entry->comparemode = texObj->CompareMode; 2651b3e3e6b841535b78ff4fa1d3861e9d8cdeff972Brian Paul entry->comparefunc = texObj->CompareFunc; 2669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 26734474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt drm_intel_bo_unreference(brw->wm.sdc_bo[unit]); 2687216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) { 2697216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt float bordercolor[4] = { 270f6c2d1f94ffa8f87b22ed1ff5c465d0e9aa1489eBrian Paul texObj->BorderColor.f[0], 271f6c2d1f94ffa8f87b22ed1ff5c465d0e9aa1489eBrian Paul texObj->BorderColor.f[0], 272f6c2d1f94ffa8f87b22ed1ff5c465d0e9aa1489eBrian Paul texObj->BorderColor.f[0], 273f6c2d1f94ffa8f87b22ed1ff5c465d0e9aa1489eBrian Paul texObj->BorderColor.f[0] 2747216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt }; 2757216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt /* GL specs that border color for depth textures is taken from the 2767216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt * R channel, while the hardware uses A. Spam R into all the 2777216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt * channels for safety. 2787216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt */ 2797216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt brw->wm.sdc_bo[unit] = upload_default_color(brw, bordercolor); 2807216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt } else { 2817216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt brw->wm.sdc_bo[unit] = upload_default_color(brw, 282f6c2d1f94ffa8f87b22ed1ff5c465d0e9aa1489eBrian Paul texObj->BorderColor.f); 2837216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt } 2843e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt key->sampler_count = unit + 1; 2859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 2869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 287da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock struct wm_sampler_entry *entry = &key->sampler[key->sampler_count]; 288da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock memset(last_entry_end, 0, (char*)entry - last_entry_end); 2893e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt} 2903e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 2913e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/* All samplers must be uploaded in a single contiguous array, which 2923e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * complicates various things. However, this is still too confusing - 2933e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * FIXME: simplify all the different new texture state flags. 2943e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */ 295f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void upload_wm_samplers( struct brw_context *brw ) 2963e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{ 297052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt GLcontext *ctx = &brw->intel.ctx; 2983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct wm_sampler_key key; 2998c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt int i, sampler_key_size; 3003e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3013e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw_wm_sampler_populate_key(brw, &key); 3023e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3033e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (brw->wm.sampler_count != key.sampler_count) { 3043e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sampler_count = key.sampler_count; 3059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt brw->state.dirty.cache |= CACHE_NEW_SAMPLER; 3069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt } 3079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 30834474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt drm_intel_bo_unreference(brw->wm.sampler_bo); 3093e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sampler_bo = NULL; 3103e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (brw->wm.sampler_count == 0) 311f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie return; 3123e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3138c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt /* Only include the populated portion of the key in the search. */ 3148c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt sampler_key_size = offsetof(struct wm_sampler_key, 3158c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt sampler[key.sampler_count]); 3163e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sampler_bo = brw_search_cache(&brw->cache, BRW_SAMPLER, 3178c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt &key, sampler_key_size, 3183e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sdc_bo, key.sampler_count, 3193e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt NULL); 3203e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3213e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt /* If we didnt find it in the cache, compute the state and put it in the 3223e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * cache. 3233e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */ 3243e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (brw->wm.sampler_bo == NULL) { 3253e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT]; 3263e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3273e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt memset(sampler, 0, sizeof(sampler)); 3283e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt for (i = 0; i < key.sampler_count; i++) { 3293e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt if (brw->wm.sdc_bo[i] == NULL) 3303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt continue; 3313e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3323e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw_update_sampler_state(&key.sampler[i], brw->wm.sdc_bo[i], 3333e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt &sampler[i]); 3343e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt } 3353e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt 3363e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sampler_bo = brw_upload_cache(&brw->cache, BRW_SAMPLER, 3378c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt &key, sampler_key_size, 3383e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt brw->wm.sdc_bo, key.sampler_count, 33962a96f74c9a1fd07301d349e4181a7212fc7d45cEric Anholt &sampler, sizeof(sampler)); 3409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 3418abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt /* Emit SDC relocations */ 3428abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt for (i = 0; i < BRW_MAX_TEX_UNIT; i++) { 343052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt if (!ctx->Texture.Unit[i]._ReallyEnabled) 3448abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt continue; 34538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt 3463628185f566e178a12b493fb89abf52b4b281f99Eric Anholt dri_bo_emit_reloc(brw->wm.sampler_bo, 34717fdd1ab3b48da8dff742e626c3c59bc89cafeedEric Anholt I915_GEM_DOMAIN_SAMPLER, 0, 3483628185f566e178a12b493fb89abf52b4b281f99Eric Anholt 0, 3493628185f566e178a12b493fb89abf52b4b281f99Eric Anholt i * sizeof(struct brw_sampler_state) + 3503628185f566e178a12b493fb89abf52b4b281f99Eric Anholt offsetof(struct brw_sampler_state, ss2), 3513628185f566e178a12b493fb89abf52b4b281f99Eric Anholt brw->wm.sdc_bo[i]); 3528abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt } 35338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt } 35438bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt} 3559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 3569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_samplers = { 3579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt .dirty = { 3589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt .mesa = _NEW_TEXTURE, 3599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt .brw = 0, 3609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt .cache = 0 3619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt }, 362008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie .prepare = upload_wm_samplers, 3639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}; 3649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 3659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt 366