lima_texture.c revision 7ec681f3
1/* 2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be> 3 * Copyright (c) 2018-2019 Lima Project 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sub license, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the 13 * next paragraph) shall be included in all copies or substantial portions 14 * of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 26#include "util/u_memory.h" 27#include "util/u_upload_mgr.h" 28#include "util/u_math.h" 29#include "util/u_debug.h" 30#include "util/u_transfer.h" 31 32#include "lima_bo.h" 33#include "lima_context.h" 34#include "lima_screen.h" 35#include "lima_texture.h" 36#include "lima_resource.h" 37#include "lima_job.h" 38#include "lima_util.h" 39#include "lima_format.h" 40 41#include <drm-uapi/lima_drm.h> 42 43 44#define lima_tex_list_size 64 45 46static_assert(offsetof(lima_tex_desc, va) == 24, "lima_tex_desc->va offset isn't 24"); 47 48 49static void 50lima_texture_desc_set_va(lima_tex_desc *desc, 51 int idx, 52 uint32_t va) 53{ 54 unsigned va_bit_idx = VA_BIT_OFFSET + (VA_BIT_SIZE * idx); 55 unsigned va_idx = va_bit_idx / 32; 56 va_bit_idx %= 32; 57 58 va >>= 6; 59 60 desc->va[va_idx] |= va << va_bit_idx; 61 if (va_bit_idx <= 6) 62 return; 63 desc->va[va_idx + 1] |= va >> (32 - va_bit_idx); 64} 65 66/* 67 * Note: this function is used by both draw and flush code path, 68 * make sure no lima_job_get() is called inside this. 69 */ 70void 71lima_texture_desc_set_res(struct lima_context *ctx, lima_tex_desc *desc, 72 struct pipe_resource *prsc, 73 unsigned first_level, unsigned last_level, unsigned first_layer) 74{ 75 unsigned width, height, layout, i; 76 struct lima_resource *lima_res = lima_resource(prsc); 77 78 width = prsc->width0; 79 height = prsc->height0; 80 if (first_level != 0) { 81 width = u_minify(width, first_level); 82 height = u_minify(height, first_level); 83 } 84 85 desc->format = lima_format_get_texel(prsc->format); 86 desc->swap_r_b = lima_format_get_texel_swap_rb(prsc->format); 87 desc->width = width; 88 desc->height = height; 89 desc->unknown_3_1 = 1; 90 91 if (lima_res->tiled) 92 layout = 3; 93 else { 94 desc->stride = lima_res->levels[first_level].stride; 95 desc->has_stride = 1; 96 layout = 0; 97 } 98 99 uint32_t base_va = lima_res->bo->va; 100 101 /* attach first level */ 102 uint32_t first_va = base_va + lima_res->levels[first_level].offset + first_layer * lima_res->levels[first_level].layer_stride; 103 desc->va_s.va_0 = first_va >> 6; 104 desc->va_s.layout = layout; 105 106 /* Attach remaining levels. 107 * Each subsequent mipmap address is specified using the 26 msbs. 108 * These addresses are then packed continuously in memory */ 109 for (i = 1; i <= (last_level - first_level); i++) { 110 uint32_t address = base_va + lima_res->levels[first_level + i].offset; 111 lima_texture_desc_set_va(desc, i, address); 112 } 113} 114 115static void 116lima_update_tex_desc(struct lima_context *ctx, struct lima_sampler_state *sampler, 117 struct lima_sampler_view *texture, void *pdesc, 118 unsigned desc_size) 119{ 120 /* unit is 1/16 since lod_bias is in fixed format */ 121 int lod_bias_delta = 0; 122 lima_tex_desc *desc = pdesc; 123 unsigned first_level; 124 unsigned last_level; 125 unsigned first_layer; 126 float max_lod; 127 128 memset(desc, 0, desc_size); 129 130 switch (texture->base.target) { 131 case PIPE_TEXTURE_2D: 132 case PIPE_TEXTURE_RECT: 133 desc->texture_type = LIMA_TEXTURE_TYPE_2D; 134 break; 135 case PIPE_TEXTURE_CUBE: 136 desc->texture_type = LIMA_TEXTURE_TYPE_CUBE; 137 break; 138 default: 139 break; 140 } 141 142 if (!sampler->base.normalized_coords) 143 desc->unnorm_coords = 1; 144 145 first_level = texture->base.u.tex.first_level; 146 last_level = texture->base.u.tex.last_level; 147 first_layer = texture->base.u.tex.first_layer; 148 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS) 149 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1; 150 151 desc->min_lod = lima_float_to_fixed8(sampler->base.min_lod); 152 max_lod = MIN2(sampler->base.max_lod, sampler->base.min_lod + 153 (last_level - first_level)); 154 desc->max_lod = lima_float_to_fixed8(max_lod); 155 desc->lod_bias = lima_float_to_fixed8(sampler->base.lod_bias); 156 157 switch (sampler->base.min_mip_filter) { 158 case PIPE_TEX_MIPFILTER_LINEAR: 159 desc->min_mipfilter_2 = 3; 160 break; 161 case PIPE_TEX_MIPFILTER_NEAREST: 162 desc->min_mipfilter_2 = 0; 163 break; 164 case PIPE_TEX_MIPFILTER_NONE: 165 desc->max_lod = desc->min_lod; 166 break; 167 default: 168 break; 169 } 170 171 switch (sampler->base.mag_img_filter) { 172 case PIPE_TEX_FILTER_LINEAR: 173 desc->mag_img_filter_nearest = 0; 174 break; 175 case PIPE_TEX_FILTER_NEAREST: 176 default: 177 desc->mag_img_filter_nearest = 1; 178 break; 179 } 180 181 switch (sampler->base.min_img_filter) { 182 break; 183 case PIPE_TEX_FILTER_LINEAR: 184 desc->min_img_filter_nearest = 0; 185 break; 186 case PIPE_TEX_FILTER_NEAREST: 187 default: 188 lod_bias_delta = 8; 189 desc->min_img_filter_nearest = 1; 190 break; 191 } 192 193 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */ 194 switch (sampler->base.wrap_s) { 195 case PIPE_TEX_WRAP_CLAMP: 196 desc->wrap_s_clamp = 1; 197 break; 198 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 199 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 200 desc->wrap_s_clamp_to_edge = 1; 201 break; 202 case PIPE_TEX_WRAP_MIRROR_REPEAT: 203 desc->wrap_s_mirror_repeat = 1; 204 break; 205 case PIPE_TEX_WRAP_REPEAT: 206 default: 207 break; 208 } 209 210 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */ 211 switch (sampler->base.wrap_t) { 212 case PIPE_TEX_WRAP_CLAMP: 213 desc->wrap_t_clamp = 1; 214 break; 215 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 216 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 217 desc->wrap_t_clamp_to_edge = 1; 218 break; 219 case PIPE_TEX_WRAP_MIRROR_REPEAT: 220 desc->wrap_t_mirror_repeat = 1; 221 break; 222 case PIPE_TEX_WRAP_REPEAT: 223 default: 224 break; 225 } 226 227 if (desc->min_img_filter_nearest && desc->mag_img_filter_nearest && 228 desc->min_mipfilter_2 == 0 && 229 (desc->min_lod != desc->max_lod)) 230 lod_bias_delta = -1; 231 232 desc->lod_bias += lod_bias_delta; 233 234 lima_texture_desc_set_res(ctx, desc, texture->base.texture, 235 first_level, last_level, first_layer); 236} 237 238static unsigned 239lima_calc_tex_desc_size(struct lima_sampler_view *texture) 240{ 241 unsigned size = offsetof(lima_tex_desc, va); 242 unsigned va_bit_size; 243 unsigned first_level = texture->base.u.tex.first_level; 244 unsigned last_level = texture->base.u.tex.last_level; 245 246 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS) 247 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1; 248 249 va_bit_size = VA_BIT_OFFSET + VA_BIT_SIZE * (last_level - first_level + 1); 250 size += (va_bit_size + 7) >> 3; 251 size = align(size, lima_min_tex_desc_size); 252 253 return size; 254} 255 256void 257lima_update_textures(struct lima_context *ctx) 258{ 259 struct lima_job *job = lima_job_get(ctx); 260 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj; 261 262 assert (lima_tex->num_samplers <= 16); 263 264 /* Nothing to do - we have no samplers or textures */ 265 if (!lima_tex->num_samplers || !lima_tex->num_textures) 266 return; 267 268 /* we always need to add texture bo to job */ 269 for (int i = 0; i < lima_tex->num_samplers; i++) { 270 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]); 271 struct lima_resource *rsc = lima_resource(texture->base.texture); 272 lima_flush_previous_job_writing_resource(ctx, texture->base.texture); 273 lima_job_add_bo(job, LIMA_PIPE_PP, rsc->bo, LIMA_SUBMIT_BO_READ); 274 } 275 276 /* do not regenerate texture desc if no change */ 277 if (!(ctx->dirty & LIMA_CONTEXT_DIRTY_TEXTURES)) 278 return; 279 280 unsigned size = lima_tex_list_size; 281 for (int i = 0; i < lima_tex->num_samplers; i++) { 282 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]); 283 size += lima_calc_tex_desc_size(texture); 284 } 285 286 uint32_t *descs = 287 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_tex_desc, size); 288 289 off_t offset = lima_tex_list_size; 290 for (int i = 0; i < lima_tex->num_samplers; i++) { 291 struct lima_sampler_state *sampler = lima_sampler_state(lima_tex->samplers[i]); 292 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]); 293 unsigned desc_size = lima_calc_tex_desc_size(texture); 294 295 descs[i] = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + offset; 296 lima_update_tex_desc(ctx, sampler, texture, (void *)descs + offset, desc_size); 297 offset += desc_size; 298 } 299 300 lima_dump_command_stream_print( 301 job->dump, descs, size, false, "add textures_desc at va %x\n", 302 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc)); 303 304 lima_dump_texture_descriptor( 305 job->dump, descs, size, 306 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + lima_tex_list_size, 307 lima_tex_list_size); 308} 309