1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci /* 29bf215546Sopenharmony_ci * Authors: 30bf215546Sopenharmony_ci * Keith Whitwell <keithw@vmware.com> 31bf215546Sopenharmony_ci * Brian Paul 32bf215546Sopenharmony_ci */ 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include "main/macros.h" 36bf215546Sopenharmony_ci#include "main/glformats.h" 37bf215546Sopenharmony_ci#include "main/samplerobj.h" 38bf215546Sopenharmony_ci#include "main/teximage.h" 39bf215546Sopenharmony_ci#include "main/texobj.h" 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci#include "st_context.h" 42bf215546Sopenharmony_ci#include "st_cb_texture.h" 43bf215546Sopenharmony_ci#include "st_format.h" 44bf215546Sopenharmony_ci#include "st_atom.h" 45bf215546Sopenharmony_ci#include "st_sampler_view.h" 46bf215546Sopenharmony_ci#include "st_texture.h" 47bf215546Sopenharmony_ci#include "pipe/p_context.h" 48bf215546Sopenharmony_ci#include "pipe/p_defines.h" 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci#include "cso_cache/cso_context.h" 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci#include "util/format/u_format.h" 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci/** 56bf215546Sopenharmony_ci * Convert a gl_sampler_object to a pipe_sampler_state object. 57bf215546Sopenharmony_ci */ 58bf215546Sopenharmony_civoid 59bf215546Sopenharmony_cist_convert_sampler(const struct st_context *st, 60bf215546Sopenharmony_ci const struct gl_texture_object *texobj, 61bf215546Sopenharmony_ci const struct gl_sampler_object *msamp, 62bf215546Sopenharmony_ci float tex_unit_lod_bias, 63bf215546Sopenharmony_ci struct pipe_sampler_state *sampler, 64bf215546Sopenharmony_ci bool seamless_cube_map) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci memcpy(sampler, &msamp->Attrib.state, sizeof(*sampler)); 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci sampler->seamless_cube_map |= seamless_cube_map; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci if (texobj->_IsIntegerFormat && st->ctx->Const.ForceIntegerTexNearest) { 71bf215546Sopenharmony_ci sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST; 72bf215546Sopenharmony_ci sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST; 73bf215546Sopenharmony_ci } 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB || st->lower_rect_tex) 76bf215546Sopenharmony_ci sampler->normalized_coords = 1; 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci sampler->lod_bias += tex_unit_lod_bias; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci /* Check that only wrap modes using the border color have the first bit 81bf215546Sopenharmony_ci * set. 82bf215546Sopenharmony_ci */ 83bf215546Sopenharmony_ci STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP & 0x1); 84bf215546Sopenharmony_ci STATIC_ASSERT(PIPE_TEX_WRAP_CLAMP_TO_BORDER & 0x1); 85bf215546Sopenharmony_ci STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP & 0x1); 86bf215546Sopenharmony_ci STATIC_ASSERT(PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER & 0x1); 87bf215546Sopenharmony_ci STATIC_ASSERT(((PIPE_TEX_WRAP_REPEAT | 88bf215546Sopenharmony_ci PIPE_TEX_WRAP_CLAMP_TO_EDGE | 89bf215546Sopenharmony_ci PIPE_TEX_WRAP_MIRROR_REPEAT | 90bf215546Sopenharmony_ci PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE) & 0x1) == 0); 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci /* For non-black borders... */ 93bf215546Sopenharmony_ci if (msamp->Attrib.IsBorderColorNonZero && 94bf215546Sopenharmony_ci /* This is true if wrap modes are using the border color: */ 95bf215546Sopenharmony_ci (sampler->wrap_s | sampler->wrap_t | sampler->wrap_r) & 0x1) { 96bf215546Sopenharmony_ci GLenum texBaseFormat = _mesa_base_tex_image(texobj)->_BaseFormat; 97bf215546Sopenharmony_ci const GLboolean is_integer = 98bf215546Sopenharmony_ci texobj->_IsIntegerFormat || texobj->StencilSampling || 99bf215546Sopenharmony_ci texBaseFormat == GL_STENCIL_INDEX; 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci if (texobj->StencilSampling) 102bf215546Sopenharmony_ci texBaseFormat = GL_STENCIL_INDEX; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci if (st->apply_texture_swizzle_to_border_color) { 105bf215546Sopenharmony_ci const struct gl_texture_object *stobj = st_texture_object_const(texobj); 106bf215546Sopenharmony_ci /* XXX: clean that up to not use the sampler view at all */ 107bf215546Sopenharmony_ci const struct st_sampler_view *sv = st_texture_get_current_sampler_view(st, stobj); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci if (sv) { 110bf215546Sopenharmony_ci struct pipe_sampler_view *view = sv->view; 111bf215546Sopenharmony_ci union pipe_color_union tmp = sampler->border_color; 112bf215546Sopenharmony_ci const unsigned char swz[4] = 113bf215546Sopenharmony_ci { 114bf215546Sopenharmony_ci view->swizzle_r, 115bf215546Sopenharmony_ci view->swizzle_g, 116bf215546Sopenharmony_ci view->swizzle_b, 117bf215546Sopenharmony_ci view->swizzle_a, 118bf215546Sopenharmony_ci }; 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci st_translate_color(&tmp, texBaseFormat, is_integer); 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci util_format_apply_color_swizzle(&sampler->border_color, 123bf215546Sopenharmony_ci &tmp, swz, is_integer); 124bf215546Sopenharmony_ci } else { 125bf215546Sopenharmony_ci st_translate_color(&sampler->border_color, 126bf215546Sopenharmony_ci texBaseFormat, is_integer); 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci } else { 129bf215546Sopenharmony_ci st_translate_color(&sampler->border_color, 130bf215546Sopenharmony_ci texBaseFormat, is_integer); 131bf215546Sopenharmony_ci if (st->use_format_with_border_color) 132bf215546Sopenharmony_ci sampler->border_color_format = st_get_sampler_view_format(st, texobj, 133bf215546Sopenharmony_ci msamp->Attrib.sRGBDecode == GL_SKIP_DECODE_EXT); 134bf215546Sopenharmony_ci } 135bf215546Sopenharmony_ci sampler->border_color_is_integer = is_integer; 136bf215546Sopenharmony_ci } 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci /* If sampling a depth texture and using shadow comparison */ 139bf215546Sopenharmony_ci if (msamp->Attrib.CompareMode == GL_COMPARE_R_TO_TEXTURE) { 140bf215546Sopenharmony_ci GLenum texBaseFormat = _mesa_base_tex_image(texobj)->_BaseFormat; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci if (texBaseFormat == GL_DEPTH_COMPONENT || 143bf215546Sopenharmony_ci (texBaseFormat == GL_DEPTH_STENCIL && !texobj->StencilSampling)) 144bf215546Sopenharmony_ci sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE; 145bf215546Sopenharmony_ci } 146bf215546Sopenharmony_ci} 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci/** 149bf215546Sopenharmony_ci * Get a pipe_sampler_state object from a texture unit. 150bf215546Sopenharmony_ci */ 151bf215546Sopenharmony_civoid 152bf215546Sopenharmony_cist_convert_sampler_from_unit(const struct st_context *st, 153bf215546Sopenharmony_ci struct pipe_sampler_state *sampler, 154bf215546Sopenharmony_ci GLuint texUnit) 155bf215546Sopenharmony_ci{ 156bf215546Sopenharmony_ci const struct gl_texture_object *texobj; 157bf215546Sopenharmony_ci struct gl_context *ctx = st->ctx; 158bf215546Sopenharmony_ci const struct gl_sampler_object *msamp; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci texobj = ctx->Texture.Unit[texUnit]._Current; 161bf215546Sopenharmony_ci assert(texobj); 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci msamp = _mesa_get_samplerobj(ctx, texUnit); 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci st_convert_sampler(st, texobj, msamp, ctx->Texture.Unit[texUnit].LodBiasQuantized, 166bf215546Sopenharmony_ci sampler, ctx->Texture.CubeMapSeamless); 167bf215546Sopenharmony_ci} 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci/** 171bf215546Sopenharmony_ci * Update the gallium driver's sampler state for fragment, vertex or 172bf215546Sopenharmony_ci * geometry shader stage. 173bf215546Sopenharmony_ci */ 174bf215546Sopenharmony_cistatic void 175bf215546Sopenharmony_ciupdate_shader_samplers(struct st_context *st, 176bf215546Sopenharmony_ci enum pipe_shader_type shader_stage, 177bf215546Sopenharmony_ci const struct gl_program *prog, 178bf215546Sopenharmony_ci struct pipe_sampler_state *samplers, 179bf215546Sopenharmony_ci unsigned *out_num_samplers) 180bf215546Sopenharmony_ci{ 181bf215546Sopenharmony_ci struct gl_context *ctx = st->ctx; 182bf215546Sopenharmony_ci GLbitfield samplers_used = prog->SamplersUsed; 183bf215546Sopenharmony_ci GLbitfield free_slots = ~prog->SamplersUsed; 184bf215546Sopenharmony_ci GLbitfield external_samplers_used = prog->ExternalSamplersUsed; 185bf215546Sopenharmony_ci unsigned unit, num_samplers; 186bf215546Sopenharmony_ci struct pipe_sampler_state local_samplers[PIPE_MAX_SAMPLERS]; 187bf215546Sopenharmony_ci const struct pipe_sampler_state *states[PIPE_MAX_SAMPLERS]; 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci if (samplers_used == 0x0) { 190bf215546Sopenharmony_ci if (out_num_samplers) 191bf215546Sopenharmony_ci *out_num_samplers = 0; 192bf215546Sopenharmony_ci return; 193bf215546Sopenharmony_ci } 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci if (!samplers) 196bf215546Sopenharmony_ci samplers = local_samplers; 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci num_samplers = util_last_bit(samplers_used); 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci /* loop over sampler units (aka tex image units) */ 201bf215546Sopenharmony_ci for (unit = 0; samplers_used; unit++, samplers_used >>= 1) { 202bf215546Sopenharmony_ci struct pipe_sampler_state *sampler = samplers + unit; 203bf215546Sopenharmony_ci unsigned tex_unit = prog->SamplerUnits[unit]; 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci /* Don't update the sampler for TBOs. cso_context will not bind sampler 206bf215546Sopenharmony_ci * states that are NULL. 207bf215546Sopenharmony_ci */ 208bf215546Sopenharmony_ci if (samplers_used & 1 && 209bf215546Sopenharmony_ci (ctx->Texture.Unit[tex_unit]._Current->Target != GL_TEXTURE_BUFFER || 210bf215546Sopenharmony_ci st->texture_buffer_sampler)) { 211bf215546Sopenharmony_ci st_convert_sampler_from_unit(st, sampler, tex_unit); 212bf215546Sopenharmony_ci states[unit] = sampler; 213bf215546Sopenharmony_ci } else { 214bf215546Sopenharmony_ci states[unit] = NULL; 215bf215546Sopenharmony_ci } 216bf215546Sopenharmony_ci } 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci /* For any external samplers with multiplaner YUV, stuff the additional 219bf215546Sopenharmony_ci * sampler states we need at the end. 220bf215546Sopenharmony_ci * 221bf215546Sopenharmony_ci * Just re-use the existing sampler-state from the primary slot. 222bf215546Sopenharmony_ci */ 223bf215546Sopenharmony_ci while (unlikely(external_samplers_used)) { 224bf215546Sopenharmony_ci GLuint unit = u_bit_scan(&external_samplers_used); 225bf215546Sopenharmony_ci GLuint extra = 0; 226bf215546Sopenharmony_ci struct gl_texture_object *stObj = 227bf215546Sopenharmony_ci st_get_texture_object(st->ctx, prog, unit); 228bf215546Sopenharmony_ci struct pipe_sampler_state *sampler = samplers + unit; 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci /* if resource format matches then YUV wasn't lowered */ 231bf215546Sopenharmony_ci if (!stObj || st_get_view_format(stObj) == stObj->pt->format) 232bf215546Sopenharmony_ci continue; 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci switch (st_get_view_format(stObj)) { 235bf215546Sopenharmony_ci case PIPE_FORMAT_NV12: 236bf215546Sopenharmony_ci if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) 237bf215546Sopenharmony_ci /* no additional views needed */ 238bf215546Sopenharmony_ci break; 239bf215546Sopenharmony_ci FALLTHROUGH; 240bf215546Sopenharmony_ci case PIPE_FORMAT_P010: 241bf215546Sopenharmony_ci case PIPE_FORMAT_P012: 242bf215546Sopenharmony_ci case PIPE_FORMAT_P016: 243bf215546Sopenharmony_ci case PIPE_FORMAT_Y210: 244bf215546Sopenharmony_ci case PIPE_FORMAT_Y212: 245bf215546Sopenharmony_ci case PIPE_FORMAT_Y216: 246bf215546Sopenharmony_ci case PIPE_FORMAT_YUYV: 247bf215546Sopenharmony_ci case PIPE_FORMAT_UYVY: 248bf215546Sopenharmony_ci if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM || 249bf215546Sopenharmony_ci stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) { 250bf215546Sopenharmony_ci /* no additional views needed */ 251bf215546Sopenharmony_ci break; 252bf215546Sopenharmony_ci } 253bf215546Sopenharmony_ci 254bf215546Sopenharmony_ci /* we need one additional sampler: */ 255bf215546Sopenharmony_ci extra = u_bit_scan(&free_slots); 256bf215546Sopenharmony_ci states[extra] = sampler; 257bf215546Sopenharmony_ci break; 258bf215546Sopenharmony_ci case PIPE_FORMAT_IYUV: 259bf215546Sopenharmony_ci /* we need two additional samplers: */ 260bf215546Sopenharmony_ci extra = u_bit_scan(&free_slots); 261bf215546Sopenharmony_ci states[extra] = sampler; 262bf215546Sopenharmony_ci extra = u_bit_scan(&free_slots); 263bf215546Sopenharmony_ci states[extra] = sampler; 264bf215546Sopenharmony_ci break; 265bf215546Sopenharmony_ci default: 266bf215546Sopenharmony_ci break; 267bf215546Sopenharmony_ci } 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci num_samplers = MAX2(num_samplers, extra + 1); 270bf215546Sopenharmony_ci } 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci cso_set_samplers(st->cso_context, shader_stage, num_samplers, states); 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ci if (out_num_samplers) 275bf215546Sopenharmony_ci *out_num_samplers = num_samplers; 276bf215546Sopenharmony_ci} 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_civoid 280bf215546Sopenharmony_cist_update_vertex_samplers(struct st_context *st) 281bf215546Sopenharmony_ci{ 282bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci update_shader_samplers(st, 285bf215546Sopenharmony_ci PIPE_SHADER_VERTEX, 286bf215546Sopenharmony_ci ctx->VertexProgram._Current, 287bf215546Sopenharmony_ci st->state.vert_samplers, 288bf215546Sopenharmony_ci &st->state.num_vert_samplers); 289bf215546Sopenharmony_ci} 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_civoid 293bf215546Sopenharmony_cist_update_tessctrl_samplers(struct st_context *st) 294bf215546Sopenharmony_ci{ 295bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 296bf215546Sopenharmony_ci 297bf215546Sopenharmony_ci if (ctx->TessCtrlProgram._Current) { 298bf215546Sopenharmony_ci update_shader_samplers(st, 299bf215546Sopenharmony_ci PIPE_SHADER_TESS_CTRL, 300bf215546Sopenharmony_ci ctx->TessCtrlProgram._Current, NULL, NULL); 301bf215546Sopenharmony_ci } 302bf215546Sopenharmony_ci} 303bf215546Sopenharmony_ci 304bf215546Sopenharmony_ci 305bf215546Sopenharmony_civoid 306bf215546Sopenharmony_cist_update_tesseval_samplers(struct st_context *st) 307bf215546Sopenharmony_ci{ 308bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 309bf215546Sopenharmony_ci 310bf215546Sopenharmony_ci if (ctx->TessEvalProgram._Current) { 311bf215546Sopenharmony_ci update_shader_samplers(st, 312bf215546Sopenharmony_ci PIPE_SHADER_TESS_EVAL, 313bf215546Sopenharmony_ci ctx->TessEvalProgram._Current, NULL, NULL); 314bf215546Sopenharmony_ci } 315bf215546Sopenharmony_ci} 316bf215546Sopenharmony_ci 317bf215546Sopenharmony_ci 318bf215546Sopenharmony_civoid 319bf215546Sopenharmony_cist_update_geometry_samplers(struct st_context *st) 320bf215546Sopenharmony_ci{ 321bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 322bf215546Sopenharmony_ci 323bf215546Sopenharmony_ci if (ctx->GeometryProgram._Current) { 324bf215546Sopenharmony_ci update_shader_samplers(st, 325bf215546Sopenharmony_ci PIPE_SHADER_GEOMETRY, 326bf215546Sopenharmony_ci ctx->GeometryProgram._Current, NULL, NULL); 327bf215546Sopenharmony_ci } 328bf215546Sopenharmony_ci} 329bf215546Sopenharmony_ci 330bf215546Sopenharmony_ci 331bf215546Sopenharmony_civoid 332bf215546Sopenharmony_cist_update_fragment_samplers(struct st_context *st) 333bf215546Sopenharmony_ci{ 334bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci update_shader_samplers(st, 337bf215546Sopenharmony_ci PIPE_SHADER_FRAGMENT, 338bf215546Sopenharmony_ci ctx->FragmentProgram._Current, 339bf215546Sopenharmony_ci st->state.frag_samplers, 340bf215546Sopenharmony_ci &st->state.num_frag_samplers); 341bf215546Sopenharmony_ci} 342bf215546Sopenharmony_ci 343bf215546Sopenharmony_ci 344bf215546Sopenharmony_civoid 345bf215546Sopenharmony_cist_update_compute_samplers(struct st_context *st) 346bf215546Sopenharmony_ci{ 347bf215546Sopenharmony_ci const struct gl_context *ctx = st->ctx; 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci if (ctx->ComputeProgram._Current) { 350bf215546Sopenharmony_ci update_shader_samplers(st, 351bf215546Sopenharmony_ci PIPE_SHADER_COMPUTE, 352bf215546Sopenharmony_ci ctx->ComputeProgram._Current, NULL, NULL); 353bf215546Sopenharmony_ci } 354bf215546Sopenharmony_ci} 355