lima_texture.c revision b8e80941
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_submit.h" 38#include "lima_util.h" 39 40#include <drm-uapi/lima_drm.h> 41 42#define LIMA_TEXEL_FORMAT_BGR_565 0x0e 43#define LIMA_TEXEL_FORMAT_Z16 0x12 44#define LIMA_TEXEL_FORMAT_RGB_888 0x15 45#define LIMA_TEXEL_FORMAT_RGBA_8888 0x16 46#define LIMA_TEXEL_FORMAT_RGBX_8888 0x17 47#define LIMA_TEXEL_FORMAT_Z24S8 0x2c 48 49#define lima_tex_list_size 64 50 51static uint32_t pipe_format_to_lima(enum pipe_format pformat) 52{ 53 unsigned swap_chans = 0, flag1 = 0, format; 54 55 switch (pformat) { 56 case PIPE_FORMAT_R8G8B8A8_UNORM: 57 swap_chans = 1; 58 case PIPE_FORMAT_B8G8R8A8_UNORM: 59 format = LIMA_TEXEL_FORMAT_RGBA_8888; 60 break; 61 case PIPE_FORMAT_R8G8B8X8_UNORM: 62 swap_chans = 1; 63 case PIPE_FORMAT_B8G8R8X8_UNORM: 64 format = LIMA_TEXEL_FORMAT_RGBX_8888; 65 break; 66 case PIPE_FORMAT_R8G8B8_UNORM: 67 swap_chans = 1; 68 format = LIMA_TEXEL_FORMAT_RGB_888; 69 break; 70 case PIPE_FORMAT_B5G6R5_UNORM: 71 format = LIMA_TEXEL_FORMAT_BGR_565; 72 break; 73 case PIPE_FORMAT_Z24_UNORM_S8_UINT: 74 case PIPE_FORMAT_Z24X8_UNORM: 75 format = LIMA_TEXEL_FORMAT_Z24S8; 76 break; 77 case PIPE_FORMAT_Z16_UNORM: 78 format = LIMA_TEXEL_FORMAT_Z16; 79 break; 80 default: 81 assert(0); 82 break; 83 } 84 85 return (swap_chans << 7) | (flag1 << 6) | format; 86} 87 88void 89lima_texture_desc_set_res(struct lima_context *ctx, uint32_t *desc, 90 struct pipe_resource *prsc, 91 unsigned first_level, unsigned last_level) 92{ 93 unsigned width, height, layout, i; 94 struct lima_resource *lima_res = lima_resource(prsc); 95 96 width = prsc->width0; 97 height = prsc->height0; 98 if (first_level != 0) { 99 width = u_minify(width, first_level); 100 height = u_minify(height, first_level); 101 } 102 103 desc[0] |= pipe_format_to_lima(prsc->format); 104 desc[2] |= (width << 22); 105 desc[3] |= 0x10000 | (height << 3) | (width >> 10); 106 107 if (lima_res->tiled) 108 layout = 3; 109 else { 110 /* for padded linear texture */ 111 if (lima_res->levels[first_level].width != width) { 112 desc[0] |= lima_res->levels[first_level].width << 18; 113 desc[2] |= 0x100; 114 } 115 layout = 0; 116 } 117 118 lima_submit_add_bo(ctx->pp_submit, lima_res->bo, LIMA_SUBMIT_BO_READ); 119 120 uint32_t base_va = lima_res->bo->va; 121 122 /* attach first level */ 123 uint32_t first_va = base_va + lima_res->levels[first_level].offset; 124 desc[6] |= (first_va << 24) | (layout << 13); 125 desc[7] |= first_va >> 8; 126 127 /* Attach remaining levels. 128 * Each subsequent mipmap address is specified using the 26 msbs. 129 * These addresses are then packed continuously in memory */ 130 unsigned current_desc_index = 7; 131 unsigned current_desc_bit_index = 24; 132 for (i = first_level + 1; i <= last_level; i++) { 133 uint32_t address = base_va + lima_res->levels[i].offset; 134 address = (address >> 6); 135 desc[current_desc_index] |= (address << current_desc_bit_index); 136 if (current_desc_bit_index <= 6) { 137 current_desc_bit_index += 26; 138 if (current_desc_bit_index >= 32) { 139 current_desc_bit_index &= 0x1F; 140 current_desc_index++; 141 } 142 continue; 143 } 144 desc[current_desc_index + 1] |= (address >> (32 - current_desc_bit_index)); 145 current_desc_bit_index = (current_desc_bit_index + 26) & 0x1F; 146 current_desc_index++; 147 } 148} 149 150static void 151lima_update_tex_desc(struct lima_context *ctx, struct lima_sampler_state *sampler, 152 struct lima_sampler_view *texture, void *pdesc) 153{ 154 uint32_t *desc = pdesc; 155 unsigned first_level; 156 unsigned last_level; 157 bool mipmapping; 158 159 memset(desc, 0, lima_tex_desc_size); 160 161 /* 2D texture */ 162 desc[1] |= 0x400; 163 164 first_level = texture->base.u.tex.first_level; 165 last_level = texture->base.u.tex.last_level; 166 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS) 167 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1; 168 169 switch (sampler->base.min_mip_filter) { 170 case PIPE_TEX_MIPFILTER_LINEAR: 171 desc[2] |= 0x0600; 172 case PIPE_TEX_MIPFILTER_NEAREST: 173 mipmapping = true; 174 desc[1] |= ((last_level - first_level) << 24); 175 break; 176 case PIPE_TEX_MIPFILTER_NONE: 177 default: 178 mipmapping = false; 179 break; 180 } 181 182 switch (sampler->base.mag_img_filter) { 183 case PIPE_TEX_FILTER_LINEAR: 184 desc[2] &= ~0x1000; 185 /* no mipmap, filter_mag = linear */ 186 if (!mipmapping) 187 desc[1] |= 0x80000000; 188 break; 189 case PIPE_TEX_FILTER_NEAREST: 190 default: 191 desc[2] |= 0x1000; 192 break; 193 } 194 195 switch (sampler->base.min_img_filter) { 196 break; 197 case PIPE_TEX_FILTER_LINEAR: 198 desc[2] &= ~0x0800; 199 break; 200 case PIPE_TEX_FILTER_NEAREST: 201 default: 202 desc[2] |= 0x0800; 203 break; 204 } 205 206 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */ 207 desc[2] &= ~0xe000; 208 switch (sampler->base.wrap_s) { 209 case PIPE_TEX_WRAP_CLAMP: 210 desc[2] |= 0x4000; 211 break; 212 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 213 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 214 desc[2] |= 0x2000; 215 break; 216 case PIPE_TEX_WRAP_MIRROR_REPEAT: 217 desc[2] |= 0x8000; 218 break; 219 case PIPE_TEX_WRAP_REPEAT: 220 default: 221 break; 222 } 223 224 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */ 225 desc[2] &= ~0x070000; 226 switch (sampler->base.wrap_t) { 227 case PIPE_TEX_WRAP_CLAMP: 228 desc[2] |= 0x020000; 229 break; 230 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 231 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 232 desc[2] |= 0x010000; 233 break; 234 case PIPE_TEX_WRAP_MIRROR_REPEAT: 235 desc[2] |= 0x040000; 236 break; 237 case PIPE_TEX_WRAP_REPEAT: 238 default: 239 break; 240 } 241 242 lima_texture_desc_set_res(ctx, desc, texture->base.texture, 243 first_level, last_level); 244} 245 246void 247lima_update_textures(struct lima_context *ctx) 248{ 249 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj; 250 251 assert (lima_tex->num_samplers <= 16); 252 253 /* Nothing to do - we have no samplers or textures */ 254 if (!lima_tex->num_samplers || !lima_tex->num_textures) 255 return; 256 257 unsigned size = lima_tex_list_size + lima_tex->num_samplers * lima_tex_desc_size; 258 uint32_t *descs = 259 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_tex_desc, size, true); 260 261 for (int i = 0; i < lima_tex->num_samplers; i++) { 262 off_t offset = lima_tex_desc_size * i + lima_tex_list_size; 263 struct lima_sampler_state *sampler = lima_sampler_state(lima_tex->samplers[i]); 264 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]); 265 266 descs[i] = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc, 267 LIMA_CTX_BUFF_SUBMIT_PP) + offset; 268 lima_update_tex_desc(ctx, sampler, texture, (void *)descs + offset); 269 } 270 271 lima_dump_command_stream_print( 272 descs, size, false, "add textures_desc at va %x\n", 273 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc, 0)); 274} 275