1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org> 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci * Authors: 24bf215546Sopenharmony_ci * Rob Clark <robclark@freedesktop.org> 25bf215546Sopenharmony_ci */ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "pipe/p_state.h" 28bf215546Sopenharmony_ci#include "util/format/u_format.h" 29bf215546Sopenharmony_ci#include "util/u_inlines.h" 30bf215546Sopenharmony_ci#include "util/u_memory.h" 31bf215546Sopenharmony_ci#include "util/u_string.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "fd4_format.h" 34bf215546Sopenharmony_ci#include "fd4_texture.h" 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_cistatic enum a4xx_tex_clamp 37bf215546Sopenharmony_citex_clamp(unsigned wrap, bool *needs_border) 38bf215546Sopenharmony_ci{ 39bf215546Sopenharmony_ci switch (wrap) { 40bf215546Sopenharmony_ci case PIPE_TEX_WRAP_REPEAT: 41bf215546Sopenharmony_ci return A4XX_TEX_REPEAT; 42bf215546Sopenharmony_ci case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 43bf215546Sopenharmony_ci return A4XX_TEX_CLAMP_TO_EDGE; 44bf215546Sopenharmony_ci case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 45bf215546Sopenharmony_ci *needs_border = true; 46bf215546Sopenharmony_ci return A4XX_TEX_CLAMP_TO_BORDER; 47bf215546Sopenharmony_ci case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE: 48bf215546Sopenharmony_ci /* only works for PoT.. need to emulate otherwise! */ 49bf215546Sopenharmony_ci return A4XX_TEX_MIRROR_CLAMP; 50bf215546Sopenharmony_ci case PIPE_TEX_WRAP_MIRROR_REPEAT: 51bf215546Sopenharmony_ci return A4XX_TEX_MIRROR_REPEAT; 52bf215546Sopenharmony_ci case PIPE_TEX_WRAP_MIRROR_CLAMP: 53bf215546Sopenharmony_ci case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER: 54bf215546Sopenharmony_ci /* these two we could perhaps emulate, but we currently 55bf215546Sopenharmony_ci * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP 56bf215546Sopenharmony_ci */ 57bf215546Sopenharmony_ci default: 58bf215546Sopenharmony_ci DBG("invalid wrap: %u", wrap); 59bf215546Sopenharmony_ci return 0; 60bf215546Sopenharmony_ci } 61bf215546Sopenharmony_ci} 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistatic enum a4xx_tex_filter 64bf215546Sopenharmony_citex_filter(unsigned filter, bool aniso) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci switch (filter) { 67bf215546Sopenharmony_ci case PIPE_TEX_FILTER_NEAREST: 68bf215546Sopenharmony_ci return A4XX_TEX_NEAREST; 69bf215546Sopenharmony_ci case PIPE_TEX_FILTER_LINEAR: 70bf215546Sopenharmony_ci return aniso ? A4XX_TEX_ANISO : A4XX_TEX_LINEAR; 71bf215546Sopenharmony_ci default: 72bf215546Sopenharmony_ci DBG("invalid filter: %u", filter); 73bf215546Sopenharmony_ci return 0; 74bf215546Sopenharmony_ci } 75bf215546Sopenharmony_ci} 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_cistatic void * 78bf215546Sopenharmony_cifd4_sampler_state_create(struct pipe_context *pctx, 79bf215546Sopenharmony_ci const struct pipe_sampler_state *cso) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci struct fd4_sampler_stateobj *so = CALLOC_STRUCT(fd4_sampler_stateobj); 82bf215546Sopenharmony_ci unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8)); 83bf215546Sopenharmony_ci bool miplinear = false; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci if (!so) 86bf215546Sopenharmony_ci return NULL; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR) 89bf215546Sopenharmony_ci miplinear = true; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci so->base = *cso; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci so->needs_border = false; 94bf215546Sopenharmony_ci so->texsamp0 = 95bf215546Sopenharmony_ci COND(miplinear, A4XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) | 96bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) | 97bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) | 98bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_ANISO(aniso) | 99bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias) | 100bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) | 101bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) | 102bf215546Sopenharmony_ci A4XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border)); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci so->texsamp1 = 105bf215546Sopenharmony_ci // COND(miplinear, A4XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) | 106bf215546Sopenharmony_ci COND(!cso->seamless_cube_map, A4XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | 107bf215546Sopenharmony_ci COND(!cso->normalized_coords, A4XX_TEX_SAMP_1_UNNORM_COORDS); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) { 110bf215546Sopenharmony_ci so->texsamp1 |= A4XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) | 111bf215546Sopenharmony_ci A4XX_TEX_SAMP_1_MAX_LOD(cso->max_lod); 112bf215546Sopenharmony_ci } else { 113bf215546Sopenharmony_ci /* If we're not doing mipmap filtering, we still need a slightly > 0 114bf215546Sopenharmony_ci * LOD clamp so the HW can decide between min and mag filtering of 115bf215546Sopenharmony_ci * level 0. 116bf215546Sopenharmony_ci */ 117bf215546Sopenharmony_ci so->texsamp1 |= A4XX_TEX_SAMP_1_MIN_LOD(MIN2(cso->min_lod, 0.125f)) | 118bf215546Sopenharmony_ci A4XX_TEX_SAMP_1_MAX_LOD(MIN2(cso->max_lod, 0.125f)); 119bf215546Sopenharmony_ci } 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci if (cso->compare_mode) 122bf215546Sopenharmony_ci so->texsamp1 |= 123bf215546Sopenharmony_ci A4XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */ 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci return so; 126bf215546Sopenharmony_ci} 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_cistatic bool 129bf215546Sopenharmony_ciuse_astc_srgb_workaround(struct pipe_context *pctx, enum pipe_format format) 130bf215546Sopenharmony_ci{ 131bf215546Sopenharmony_ci return (fd_screen(pctx->screen)->gpu_id == 420) && 132bf215546Sopenharmony_ci (util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_ASTC); 133bf215546Sopenharmony_ci} 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_cistatic struct pipe_sampler_view * 136bf215546Sopenharmony_cifd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc, 137bf215546Sopenharmony_ci const struct pipe_sampler_view *cso) 138bf215546Sopenharmony_ci{ 139bf215546Sopenharmony_ci struct fd4_pipe_sampler_view *so = CALLOC_STRUCT(fd4_pipe_sampler_view); 140bf215546Sopenharmony_ci struct fd_resource *rsc = fd_resource(prsc); 141bf215546Sopenharmony_ci enum pipe_format format = cso->format; 142bf215546Sopenharmony_ci unsigned lvl, layers = 0; 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci if (!so) 145bf215546Sopenharmony_ci return NULL; 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci if (format == PIPE_FORMAT_X32_S8X24_UINT) { 148bf215546Sopenharmony_ci rsc = rsc->stencil; 149bf215546Sopenharmony_ci format = rsc->b.b.format; 150bf215546Sopenharmony_ci } 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci so->base = *cso; 153bf215546Sopenharmony_ci pipe_reference(NULL, &prsc->reference); 154bf215546Sopenharmony_ci so->base.texture = prsc; 155bf215546Sopenharmony_ci so->base.reference.count = 1; 156bf215546Sopenharmony_ci so->base.context = pctx; 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci so->swizzle = fd4_tex_swiz(format, cso->swizzle_r, cso->swizzle_g, 159bf215546Sopenharmony_ci cso->swizzle_b, cso->swizzle_a); 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci so->texconst0 = A4XX_TEX_CONST_0_TYPE(fd4_tex_type(cso->target)) | 162bf215546Sopenharmony_ci A4XX_TEX_CONST_0_FMT(fd4_pipe2tex(format)) | 163bf215546Sopenharmony_ci so->swizzle; 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci if (util_format_is_srgb(format)) { 166bf215546Sopenharmony_ci if (use_astc_srgb_workaround(pctx, format)) 167bf215546Sopenharmony_ci so->astc_srgb = true; 168bf215546Sopenharmony_ci so->texconst0 |= A4XX_TEX_CONST_0_SRGB; 169bf215546Sopenharmony_ci } 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci if (cso->target == PIPE_BUFFER) { 172bf215546Sopenharmony_ci unsigned elements = cso->u.buf.size / util_format_get_blocksize(format); 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci lvl = 0; 175bf215546Sopenharmony_ci so->texconst1 = 176bf215546Sopenharmony_ci A4XX_TEX_CONST_1_WIDTH(elements & MASK(15)) | 177bf215546Sopenharmony_ci A4XX_TEX_CONST_1_HEIGHT(elements >> 15); 178bf215546Sopenharmony_ci so->texconst2 = A4XX_TEX_CONST_2_BUFFER; 179bf215546Sopenharmony_ci so->offset = cso->u.buf.offset; 180bf215546Sopenharmony_ci } else { 181bf215546Sopenharmony_ci unsigned miplevels; 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ci lvl = fd_sampler_first_level(cso); 184bf215546Sopenharmony_ci miplevels = fd_sampler_last_level(cso) - lvl; 185bf215546Sopenharmony_ci layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1; 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci so->texconst0 |= A4XX_TEX_CONST_0_MIPLVLS(miplevels); 188bf215546Sopenharmony_ci so->texconst1 = A4XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) | 189bf215546Sopenharmony_ci A4XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl)); 190bf215546Sopenharmony_ci so->texconst2 = A4XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 5) | 191bf215546Sopenharmony_ci A4XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl)); 192bf215546Sopenharmony_ci so->offset = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer); 193bf215546Sopenharmony_ci } 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci /* NOTE: since we sample z24s8 using 8888_UINT format, the swizzle 196bf215546Sopenharmony_ci * we get isn't quite right. Use SWAP(XYZW) as a cheap and cheerful 197bf215546Sopenharmony_ci * way to re-arrange things so stencil component is where the swiz 198bf215546Sopenharmony_ci * expects. 199bf215546Sopenharmony_ci * 200bf215546Sopenharmony_ci * Note that gallium expects stencil sampler to return (s,s,s,s) 201bf215546Sopenharmony_ci * which isn't quite true. To make that happen we'd have to massage 202bf215546Sopenharmony_ci * the swizzle. But in practice only the .x component is used. 203bf215546Sopenharmony_ci */ 204bf215546Sopenharmony_ci if (format == PIPE_FORMAT_X24S8_UINT) 205bf215546Sopenharmony_ci so->texconst2 |= A4XX_TEX_CONST_2_SWAP(XYZW); 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci switch (cso->target) { 208bf215546Sopenharmony_ci case PIPE_TEXTURE_1D_ARRAY: 209bf215546Sopenharmony_ci case PIPE_TEXTURE_2D_ARRAY: 210bf215546Sopenharmony_ci so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers) | 211bf215546Sopenharmony_ci A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size); 212bf215546Sopenharmony_ci break; 213bf215546Sopenharmony_ci case PIPE_TEXTURE_CUBE: 214bf215546Sopenharmony_ci case PIPE_TEXTURE_CUBE_ARRAY: 215bf215546Sopenharmony_ci so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers / 6) | 216bf215546Sopenharmony_ci A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size); 217bf215546Sopenharmony_ci break; 218bf215546Sopenharmony_ci case PIPE_TEXTURE_3D: 219bf215546Sopenharmony_ci so->texconst3 = 220bf215546Sopenharmony_ci A4XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) | 221bf215546Sopenharmony_ci A4XX_TEX_CONST_3_LAYERSZ(fd_resource_slice(rsc, lvl)->size0); 222bf215546Sopenharmony_ci so->texconst4 = A4XX_TEX_CONST_4_LAYERSZ( 223bf215546Sopenharmony_ci fd_resource_slice(rsc, prsc->last_level)->size0); 224bf215546Sopenharmony_ci break; 225bf215546Sopenharmony_ci default: 226bf215546Sopenharmony_ci so->texconst3 = 0x00000000; 227bf215546Sopenharmony_ci break; 228bf215546Sopenharmony_ci } 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci return &so->base; 231bf215546Sopenharmony_ci} 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_cistatic void 234bf215546Sopenharmony_cifd4_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader, 235bf215546Sopenharmony_ci unsigned start, unsigned nr, 236bf215546Sopenharmony_ci unsigned unbind_num_trailing_slots, 237bf215546Sopenharmony_ci bool take_ownership, 238bf215546Sopenharmony_ci struct pipe_sampler_view **views) 239bf215546Sopenharmony_ci{ 240bf215546Sopenharmony_ci struct fd_context *ctx = fd_context(pctx); 241bf215546Sopenharmony_ci struct fd4_context *fd4_ctx = fd4_context(ctx); 242bf215546Sopenharmony_ci uint16_t astc_srgb = 0; 243bf215546Sopenharmony_ci uint16_t *sampler_swizzles; 244bf215546Sopenharmony_ci unsigned i; 245bf215546Sopenharmony_ci 246bf215546Sopenharmony_ci if (shader == PIPE_SHADER_FRAGMENT) { 247bf215546Sopenharmony_ci sampler_swizzles = fd4_ctx->fsampler_swizzles; 248bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_VERTEX) { 249bf215546Sopenharmony_ci sampler_swizzles = fd4_ctx->vsampler_swizzles; 250bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_COMPUTE) { 251bf215546Sopenharmony_ci sampler_swizzles = fd4_ctx->csampler_swizzles; 252bf215546Sopenharmony_ci } else { 253bf215546Sopenharmony_ci assert(0); 254bf215546Sopenharmony_ci sampler_swizzles = fd4_ctx->csampler_swizzles; 255bf215546Sopenharmony_ci } 256bf215546Sopenharmony_ci 257bf215546Sopenharmony_ci for (i = 0; i < nr; i++) { 258bf215546Sopenharmony_ci if (views[i]) { 259bf215546Sopenharmony_ci struct fd4_pipe_sampler_view *view = fd4_pipe_sampler_view(views[i]); 260bf215546Sopenharmony_ci if (view->astc_srgb) 261bf215546Sopenharmony_ci astc_srgb |= (1 << (start + i)); 262bf215546Sopenharmony_ci sampler_swizzles[start + i] = view->swizzle >> 4; 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci const struct util_format_description *desc = 265bf215546Sopenharmony_ci util_format_description(view->base.format); 266bf215546Sopenharmony_ci int c = util_format_get_first_non_void_channel(desc->format); 267bf215546Sopenharmony_ci if (c >= 0 && desc->channel[c].pure_integer) { 268bf215546Sopenharmony_ci switch (desc->channel[c].size) { 269bf215546Sopenharmony_ci case 8: 270bf215546Sopenharmony_ci sampler_swizzles[start + i] |= 0x1000; 271bf215546Sopenharmony_ci break; 272bf215546Sopenharmony_ci case 16: 273bf215546Sopenharmony_ci sampler_swizzles[start + i] |= 0x2000; 274bf215546Sopenharmony_ci break; 275bf215546Sopenharmony_ci case 32: 276bf215546Sopenharmony_ci sampler_swizzles[start + i] |= 0x3000; 277bf215546Sopenharmony_ci break; 278bf215546Sopenharmony_ci case 10: 279bf215546Sopenharmony_ci sampler_swizzles[start + i] |= 0x4000; 280bf215546Sopenharmony_ci break; 281bf215546Sopenharmony_ci default: 282bf215546Sopenharmony_ci assert(0); 283bf215546Sopenharmony_ci } 284bf215546Sopenharmony_ci } 285bf215546Sopenharmony_ci } 286bf215546Sopenharmony_ci } 287bf215546Sopenharmony_ci 288bf215546Sopenharmony_ci fd_set_sampler_views(pctx, shader, start, nr, unbind_num_trailing_slots, 289bf215546Sopenharmony_ci take_ownership, views); 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci for (i = 0; i < unbind_num_trailing_slots; i++) { 292bf215546Sopenharmony_ci astc_srgb &= ~(1 << (start + nr + i)); 293bf215546Sopenharmony_ci sampler_swizzles[start + nr + i] = 0x688; 294bf215546Sopenharmony_ci } 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_ci if (shader == PIPE_SHADER_FRAGMENT) { 297bf215546Sopenharmony_ci fd4_ctx->fastc_srgb = astc_srgb; 298bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_VERTEX) { 299bf215546Sopenharmony_ci fd4_ctx->vastc_srgb = astc_srgb; 300bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_COMPUTE) { 301bf215546Sopenharmony_ci fd4_ctx->castc_srgb = astc_srgb; 302bf215546Sopenharmony_ci } 303bf215546Sopenharmony_ci} 304bf215546Sopenharmony_ci 305bf215546Sopenharmony_civoid 306bf215546Sopenharmony_cifd4_texture_init(struct pipe_context *pctx) 307bf215546Sopenharmony_ci{ 308bf215546Sopenharmony_ci pctx->create_sampler_state = fd4_sampler_state_create; 309bf215546Sopenharmony_ci pctx->bind_sampler_states = fd_sampler_states_bind; 310bf215546Sopenharmony_ci pctx->create_sampler_view = fd4_sampler_view_create; 311bf215546Sopenharmony_ci pctx->set_sampler_views = fd4_set_sampler_views; 312bf215546Sopenharmony_ci} 313