1/* 2 * Copyright © 2014-2017 Broadcom 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#include "util/u_pack_color.h" 25#include "util/u_upload_mgr.h" 26#include "util/format_srgb.h" 27 28#include "v3d_context.h" 29#include "compiler/v3d_compiler.h" 30#include "broadcom/cle/v3d_packet_v33_pack.h" 31 32static uint32_t 33get_texrect_scale(struct v3d_texture_stateobj *texstate, 34 enum quniform_contents contents, 35 uint32_t data) 36{ 37 struct pipe_sampler_view *texture = texstate->textures[data]; 38 uint32_t dim; 39 40 if (contents == QUNIFORM_TEXRECT_SCALE_X) 41 dim = texture->texture->width0; 42 else 43 dim = texture->texture->height0; 44 45 return fui(1.0f / dim); 46} 47 48static uint32_t 49get_texture_size(struct v3d_texture_stateobj *texstate, 50 enum quniform_contents contents, 51 uint32_t data) 52{ 53 struct pipe_sampler_view *texture = texstate->textures[data]; 54 55 switch (contents) { 56 case QUNIFORM_TEXTURE_WIDTH: 57 return u_minify(texture->texture->width0, 58 texture->u.tex.first_level); 59 case QUNIFORM_TEXTURE_HEIGHT: 60 return u_minify(texture->texture->height0, 61 texture->u.tex.first_level); 62 case QUNIFORM_TEXTURE_DEPTH: 63 return u_minify(texture->texture->depth0, 64 texture->u.tex.first_level); 65 case QUNIFORM_TEXTURE_ARRAY_SIZE: 66 return texture->texture->array_size; 67 case QUNIFORM_TEXTURE_LEVELS: 68 return (texture->u.tex.last_level - 69 texture->u.tex.first_level) + 1; 70 default: 71 unreachable("Bad texture size field"); 72 } 73} 74 75static uint32_t 76get_image_size(struct v3d_shaderimg_stateobj *shaderimg, 77 enum quniform_contents contents, 78 uint32_t data) 79{ 80 struct v3d_image_view *image = &shaderimg->si[data]; 81 82 switch (contents) { 83 case QUNIFORM_IMAGE_WIDTH: 84 return u_minify(image->base.resource->width0, 85 image->base.u.tex.level); 86 case QUNIFORM_IMAGE_HEIGHT: 87 return u_minify(image->base.resource->height0, 88 image->base.u.tex.level); 89 case QUNIFORM_IMAGE_DEPTH: 90 return u_minify(image->base.resource->depth0, 91 image->base.u.tex.level); 92 case QUNIFORM_IMAGE_ARRAY_SIZE: 93 return image->base.resource->array_size; 94 default: 95 unreachable("Bad texture size field"); 96 } 97} 98 99/** 100 * Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter. 101 * 102 * Some bits of this field are dependent on the type of sample being done by 103 * the shader, while other bits are dependent on the sampler state. We OR the 104 * two together here. 105 */ 106static void 107write_texture_p0(struct v3d_job *job, 108 struct v3d_cl_out **uniforms, 109 struct v3d_texture_stateobj *texstate, 110 uint32_t unit, 111 uint32_t shader_data) 112{ 113 struct pipe_sampler_state *psampler = texstate->samplers[unit]; 114 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler); 115 116 cl_aligned_u32(uniforms, shader_data | sampler->p0); 117} 118 119/** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */ 120static void 121write_texture_p1(struct v3d_job *job, 122 struct v3d_cl_out **uniforms, 123 struct v3d_texture_stateobj *texstate, 124 uint32_t data) 125{ 126 /* Extract the texture unit from the top bits, and the compiler's 127 * packed p1 from the bottom. 128 */ 129 uint32_t unit = data >> 5; 130 uint32_t p1 = data & 0x1f; 131 132 struct pipe_sampler_view *psview = texstate->textures[unit]; 133 struct v3d_sampler_view *sview = v3d_sampler_view(psview); 134 135 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = { 136 .texture_state_record_base_address = texstate->texture_state[unit], 137 }; 138 139 uint32_t packed; 140 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect, 141 (uint8_t *)&packed, 142 &unpacked); 143 144 cl_aligned_u32(uniforms, p1 | packed | sview->p1); 145} 146 147/** Writes the V3D 4.x TMU configuration parameter 0. */ 148static void 149write_tmu_p0(struct v3d_job *job, 150 struct v3d_cl_out **uniforms, 151 struct v3d_texture_stateobj *texstate, 152 uint32_t data) 153{ 154 int unit = v3d_unit_data_get_unit(data); 155 struct pipe_sampler_view *psview = texstate->textures[unit]; 156 struct v3d_sampler_view *sview = v3d_sampler_view(psview); 157 struct v3d_resource *rsc = v3d_resource(sview->texture); 158 159 cl_aligned_reloc(&job->indirect, uniforms, sview->bo, 160 v3d_unit_data_get_offset(data)); 161 v3d_job_add_bo(job, rsc->bo); 162} 163 164static void 165write_image_tmu_p0(struct v3d_job *job, 166 struct v3d_cl_out **uniforms, 167 struct v3d_shaderimg_stateobj *img, 168 uint32_t data) 169{ 170 /* Extract the image unit from the top bits, and the compiler's 171 * packed p0 from the bottom. 172 */ 173 uint32_t unit = data >> 24; 174 uint32_t p0 = data & 0x00ffffff; 175 176 struct v3d_image_view *iview = &img->si[unit]; 177 struct v3d_resource *rsc = v3d_resource(iview->base.resource); 178 179 cl_aligned_reloc(&job->indirect, uniforms, 180 v3d_resource(iview->tex_state)->bo, 181 iview->tex_state_offset | p0); 182 v3d_job_add_bo(job, rsc->bo); 183} 184 185/** Writes the V3D 4.x TMU configuration parameter 1. */ 186static void 187write_tmu_p1(struct v3d_job *job, 188 struct v3d_cl_out **uniforms, 189 struct v3d_texture_stateobj *texstate, 190 uint32_t data) 191{ 192 uint32_t unit = v3d_unit_data_get_unit(data); 193 struct pipe_sampler_state *psampler = texstate->samplers[unit]; 194 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler); 195 struct pipe_sampler_view *psview = texstate->textures[unit]; 196 struct v3d_sampler_view *sview = v3d_sampler_view(psview); 197 int variant = 0; 198 199 if (sampler->border_color_variants) 200 variant = sview->sampler_variant; 201 202 cl_aligned_reloc(&job->indirect, uniforms, 203 v3d_resource(sampler->sampler_state)->bo, 204 sampler->sampler_state_offset[variant] | 205 v3d_unit_data_get_offset(data)); 206} 207 208struct v3d_cl_reloc 209v3d_write_uniforms(struct v3d_context *v3d, struct v3d_compiled_shader *shader, 210 enum pipe_shader_type stage) 211{ 212 struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage]; 213 struct v3d_texture_stateobj *texstate = &v3d->tex[stage]; 214 struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms; 215 struct v3d_job *job = v3d->job; 216 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer; 217 218 /* We always need to return some space for uniforms, because the HW 219 * will be prefetching, even if we don't read any in the program. 220 */ 221 v3d_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4); 222 223 struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect); 224 v3d_bo_reference(uniform_stream.bo); 225 226 struct v3d_cl_out *uniforms = 227 cl_start(&job->indirect); 228 229 for (int i = 0; i < uinfo->count; i++) { 230 uint32_t data = uinfo->data[i]; 231 232 switch (uinfo->contents[i]) { 233 case QUNIFORM_CONSTANT: 234 cl_aligned_u32(&uniforms, data); 235 break; 236 case QUNIFORM_UNIFORM: 237 cl_aligned_u32(&uniforms, gallium_uniforms[data]); 238 break; 239 case QUNIFORM_VIEWPORT_X_SCALE: 240 cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f); 241 break; 242 case QUNIFORM_VIEWPORT_Y_SCALE: 243 cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f); 244 break; 245 246 case QUNIFORM_VIEWPORT_Z_OFFSET: 247 cl_aligned_f(&uniforms, v3d->viewport.translate[2]); 248 break; 249 case QUNIFORM_VIEWPORT_Z_SCALE: 250 cl_aligned_f(&uniforms, v3d->viewport.scale[2]); 251 break; 252 253 case QUNIFORM_USER_CLIP_PLANE: 254 cl_aligned_f(&uniforms, 255 v3d->clip.ucp[data / 4][data % 4]); 256 break; 257 258 case QUNIFORM_TMU_CONFIG_P0: 259 write_tmu_p0(job, &uniforms, texstate, data); 260 break; 261 262 case QUNIFORM_TMU_CONFIG_P1: 263 write_tmu_p1(job, &uniforms, texstate, data); 264 break; 265 266 case QUNIFORM_IMAGE_TMU_CONFIG_P0: 267 write_image_tmu_p0(job, &uniforms, 268 &v3d->shaderimg[stage], data); 269 break; 270 271 case QUNIFORM_TEXTURE_CONFIG_P1: 272 write_texture_p1(job, &uniforms, texstate, 273 data); 274 break; 275 276 case QUNIFORM_TEXRECT_SCALE_X: 277 case QUNIFORM_TEXRECT_SCALE_Y: 278 cl_aligned_u32(&uniforms, 279 get_texrect_scale(texstate, 280 uinfo->contents[i], 281 data)); 282 break; 283 284 case QUNIFORM_TEXTURE_WIDTH: 285 case QUNIFORM_TEXTURE_HEIGHT: 286 case QUNIFORM_TEXTURE_DEPTH: 287 case QUNIFORM_TEXTURE_ARRAY_SIZE: 288 case QUNIFORM_TEXTURE_LEVELS: 289 cl_aligned_u32(&uniforms, 290 get_texture_size(texstate, 291 uinfo->contents[i], 292 data)); 293 break; 294 295 case QUNIFORM_IMAGE_WIDTH: 296 case QUNIFORM_IMAGE_HEIGHT: 297 case QUNIFORM_IMAGE_DEPTH: 298 case QUNIFORM_IMAGE_ARRAY_SIZE: 299 cl_aligned_u32(&uniforms, 300 get_image_size(&v3d->shaderimg[stage], 301 uinfo->contents[i], 302 data)); 303 break; 304 305 case QUNIFORM_ALPHA_REF: 306 cl_aligned_f(&uniforms, 307 v3d->zsa->base.alpha.ref_value); 308 break; 309 310 case QUNIFORM_UBO_ADDR: { 311 uint32_t unit = v3d_unit_data_get_unit(data); 312 /* Constant buffer 0 may be a system memory pointer, 313 * in which case we want to upload a shadow copy to 314 * the GPU. 315 */ 316 if (!cb->cb[unit].buffer) { 317 u_upload_data(v3d->uploader, 0, 318 cb->cb[unit].buffer_size, 16, 319 cb->cb[unit].user_buffer, 320 &cb->cb[unit].buffer_offset, 321 &cb->cb[unit].buffer); 322 } 323 324 cl_aligned_reloc(&job->indirect, &uniforms, 325 v3d_resource(cb->cb[unit].buffer)->bo, 326 cb->cb[unit].buffer_offset + 327 v3d_unit_data_get_offset(data)); 328 break; 329 } 330 331 case QUNIFORM_SSBO_OFFSET: { 332 struct pipe_shader_buffer *sb = 333 &v3d->ssbo[stage].sb[data]; 334 335 cl_aligned_reloc(&job->indirect, &uniforms, 336 v3d_resource(sb->buffer)->bo, 337 sb->buffer_offset); 338 break; 339 } 340 341 case QUNIFORM_GET_BUFFER_SIZE: 342 cl_aligned_u32(&uniforms, 343 v3d->ssbo[stage].sb[data].buffer_size); 344 break; 345 346 case QUNIFORM_TEXTURE_FIRST_LEVEL: 347 cl_aligned_f(&uniforms, 348 texstate->textures[data]->u.tex.first_level); 349 break; 350 351 case QUNIFORM_SPILL_OFFSET: 352 cl_aligned_reloc(&job->indirect, &uniforms, 353 v3d->prog.spill_bo, 0); 354 break; 355 356 case QUNIFORM_SPILL_SIZE_PER_THREAD: 357 cl_aligned_u32(&uniforms, 358 v3d->prog.spill_size_per_thread); 359 break; 360 361 case QUNIFORM_NUM_WORK_GROUPS: 362 cl_aligned_u32(&uniforms, 363 v3d->compute_num_workgroups[data]); 364 break; 365 366 case QUNIFORM_SHARED_OFFSET: 367 cl_aligned_reloc(&job->indirect, &uniforms, 368 v3d->compute_shared_memory, 0); 369 break; 370 371 default: 372 assert(quniform_contents_is_texture_p0(uinfo->contents[i])); 373 374 write_texture_p0(job, &uniforms, texstate, 375 uinfo->contents[i] - 376 QUNIFORM_TEXTURE_CONFIG_P0_0, 377 data); 378 break; 379 380 } 381#if 0 382 uint32_t written_val = *((uint32_t *)uniforms - 1); 383 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ", 384 shader, i, __gen_address_offset(&uniform_stream) + i * 4, 385 written_val, uif(written_val)); 386 vir_dump_uniform(uinfo->contents[i], data); 387 fprintf(stderr, "\n"); 388#endif 389 } 390 391 cl_end(&job->indirect, uniforms); 392 393 return uniform_stream; 394} 395 396void 397v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader) 398{ 399 uint32_t dirty = 0; 400 401 for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) { 402 switch (shader->prog_data.base->uniforms.contents[i]) { 403 case QUNIFORM_CONSTANT: 404 break; 405 case QUNIFORM_UNIFORM: 406 case QUNIFORM_UBO_ADDR: 407 dirty |= VC5_DIRTY_CONSTBUF; 408 break; 409 410 case QUNIFORM_VIEWPORT_X_SCALE: 411 case QUNIFORM_VIEWPORT_Y_SCALE: 412 case QUNIFORM_VIEWPORT_Z_OFFSET: 413 case QUNIFORM_VIEWPORT_Z_SCALE: 414 dirty |= VC5_DIRTY_VIEWPORT; 415 break; 416 417 case QUNIFORM_USER_CLIP_PLANE: 418 dirty |= VC5_DIRTY_CLIP; 419 break; 420 421 case QUNIFORM_TMU_CONFIG_P0: 422 case QUNIFORM_TMU_CONFIG_P1: 423 case QUNIFORM_TEXTURE_CONFIG_P1: 424 case QUNIFORM_TEXTURE_FIRST_LEVEL: 425 case QUNIFORM_TEXRECT_SCALE_X: 426 case QUNIFORM_TEXRECT_SCALE_Y: 427 case QUNIFORM_TEXTURE_WIDTH: 428 case QUNIFORM_TEXTURE_HEIGHT: 429 case QUNIFORM_TEXTURE_DEPTH: 430 case QUNIFORM_TEXTURE_ARRAY_SIZE: 431 case QUNIFORM_TEXTURE_LEVELS: 432 case QUNIFORM_SPILL_OFFSET: 433 case QUNIFORM_SPILL_SIZE_PER_THREAD: 434 /* We could flag this on just the stage we're 435 * compiling for, but it's not passed in. 436 */ 437 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX; 438 break; 439 440 case QUNIFORM_SSBO_OFFSET: 441 case QUNIFORM_GET_BUFFER_SIZE: 442 dirty |= VC5_DIRTY_SSBO; 443 break; 444 445 case QUNIFORM_IMAGE_TMU_CONFIG_P0: 446 case QUNIFORM_IMAGE_WIDTH: 447 case QUNIFORM_IMAGE_HEIGHT: 448 case QUNIFORM_IMAGE_DEPTH: 449 case QUNIFORM_IMAGE_ARRAY_SIZE: 450 dirty |= VC5_DIRTY_SHADER_IMAGE; 451 break; 452 453 case QUNIFORM_ALPHA_REF: 454 dirty |= VC5_DIRTY_ZSA; 455 break; 456 457 case QUNIFORM_NUM_WORK_GROUPS: 458 case QUNIFORM_SHARED_OFFSET: 459 /* Compute always recalculates uniforms. */ 460 break; 461 462 default: 463 assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i])); 464 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX; 465 break; 466 } 467 } 468 469 shader->uniform_dirty_bits = dirty; 470} 471