etnaviv_context.c revision 01e04c3f
1/* 2 * Copyright (c) 2012-2015 Etnaviv Project 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, sub license, 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 12 * next paragraph) shall be included in all copies or substantial portions 13 * of the 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 NON-INFRINGEMENT. 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 21 * DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * Wladimir J. van der Laan <laanwj@gmail.com> 25 * Christian Gmeiner <christian.gmeiner@gmail.com> 26 */ 27 28#include "etnaviv_context.h" 29 30#include "etnaviv_blend.h" 31#include "etnaviv_clear_blit.h" 32#include "etnaviv_compiler.h" 33#include "etnaviv_debug.h" 34#include "etnaviv_emit.h" 35#include "etnaviv_fence.h" 36#include "etnaviv_query.h" 37#include "etnaviv_query_hw.h" 38#include "etnaviv_rasterizer.h" 39#include "etnaviv_screen.h" 40#include "etnaviv_shader.h" 41#include "etnaviv_state.h" 42#include "etnaviv_surface.h" 43#include "etnaviv_texture.h" 44#include "etnaviv_transfer.h" 45#include "etnaviv_translate.h" 46#include "etnaviv_zsa.h" 47 48#include "pipe/p_context.h" 49#include "pipe/p_state.h" 50#include "util/u_blitter.h" 51#include "util/u_helpers.h" 52#include "util/u_memory.h" 53#include "util/u_prim.h" 54#include "util/u_upload_mgr.h" 55 56#include "hw/common.xml.h" 57 58static void 59etna_context_destroy(struct pipe_context *pctx) 60{ 61 struct etna_context *ctx = etna_context(pctx); 62 63 util_copy_framebuffer_state(&ctx->framebuffer_s, NULL); 64 65 if (ctx->primconvert) 66 util_primconvert_destroy(ctx->primconvert); 67 68 if (ctx->blitter) 69 util_blitter_destroy(ctx->blitter); 70 71 if (pctx->stream_uploader) 72 u_upload_destroy(pctx->stream_uploader); 73 74 if (ctx->stream) 75 etna_cmd_stream_del(ctx->stream); 76 77 slab_destroy_child(&ctx->transfer_pool); 78 79 if (ctx->in_fence_fd != -1) 80 close(ctx->in_fence_fd); 81 82 FREE(pctx); 83} 84 85/* Update render state where needed based on draw operation */ 86static void 87etna_update_state_for_draw(struct etna_context *ctx, const struct pipe_draw_info *info) 88{ 89 /* Handle primitive restart: 90 * - If not an indexed draw, we don't care about the state of the primitive restart bit. 91 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state 92 * accordingly 93 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or 94 * primitive restart is enabled and the restart index changed, mark the index 95 * buffer state as dirty 96 */ 97 98 if (info->index_size) { 99 uint32_t new_control = ctx->index_buffer.FE_INDEX_STREAM_CONTROL; 100 101 if (info->primitive_restart) 102 new_control |= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART; 103 else 104 new_control &= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART; 105 106 if (ctx->index_buffer.FE_INDEX_STREAM_CONTROL != new_control || 107 (info->primitive_restart && ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX != info->restart_index)) { 108 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = new_control; 109 ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX = info->restart_index; 110 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER; 111 } 112 } 113} 114 115static bool 116etna_get_vs(struct etna_context *ctx, struct etna_shader_key key) 117{ 118 const struct etna_shader_variant *old = ctx->shader.vs; 119 120 ctx->shader.vs = etna_shader_variant(ctx->shader.bind_vs, key, &ctx->debug); 121 122 if (!ctx->shader.vs) 123 return false; 124 125 if (old != ctx->shader.vs) 126 ctx->dirty |= ETNA_DIRTY_SHADER; 127 128 return true; 129} 130 131static bool 132etna_get_fs(struct etna_context *ctx, struct etna_shader_key key) 133{ 134 const struct etna_shader_variant *old = ctx->shader.fs; 135 136 ctx->shader.fs = etna_shader_variant(ctx->shader.bind_fs, key, &ctx->debug); 137 138 if (!ctx->shader.fs) 139 return false; 140 141 if (old != ctx->shader.fs) 142 ctx->dirty |= ETNA_DIRTY_SHADER; 143 144 return true; 145} 146 147static void 148etna_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info) 149{ 150 struct etna_context *ctx = etna_context(pctx); 151 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s; 152 uint32_t draw_mode; 153 unsigned i; 154 155 if (!info->count_from_stream_output && !info->indirect && 156 !info->primitive_restart && 157 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count)) 158 return; 159 160 if (ctx->vertex_elements == NULL || ctx->vertex_elements->num_elements == 0) 161 return; /* Nothing to do */ 162 163 if (!(ctx->prim_hwsupport & (1 << info->mode))) { 164 struct primconvert_context *primconvert = ctx->primconvert; 165 util_primconvert_save_rasterizer_state(primconvert, ctx->rasterizer); 166 util_primconvert_draw_vbo(primconvert, info); 167 return; 168 } 169 170 int prims = u_decomposed_prims_for_vertices(info->mode, info->count); 171 if (unlikely(prims <= 0)) { 172 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info->mode); 173 return; 174 } 175 176 draw_mode = translate_draw_mode(info->mode); 177 if (draw_mode == ETNA_NO_MATCH) { 178 BUG("Unsupported draw mode"); 179 return; 180 } 181 182 /* Upload a user index buffer. */ 183 unsigned index_offset = 0; 184 struct pipe_resource *indexbuf = NULL; 185 186 if (info->index_size) { 187 indexbuf = info->has_user_indices ? NULL : info->index.resource; 188 if (info->has_user_indices && 189 !util_upload_index_buffer(pctx, info, &indexbuf, &index_offset)) { 190 BUG("Index buffer upload failed."); 191 return; 192 } 193 /* Add start to index offset, when rendering indexed */ 194 index_offset += info->start * info->index_size; 195 196 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = etna_resource(indexbuf)->bo; 197 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = index_offset; 198 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ; 199 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = translate_index_size(info->index_size); 200 201 if (!ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo) { 202 BUG("Unsupported or no index buffer"); 203 return; 204 } 205 } else { 206 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = 0; 207 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = 0; 208 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = 0; 209 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = 0; 210 } 211 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER; 212 213 struct etna_shader_key key = {}; 214 struct etna_surface *cbuf = etna_surface(pfb->cbufs[0]); 215 216 if (cbuf) { 217 struct etna_resource *res = etna_resource(cbuf->base.texture); 218 219 key.frag_rb_swap = !!translate_rs_format_rb_swap(res->base.format); 220 } 221 222 if (!etna_get_vs(ctx, key) || !etna_get_fs(ctx, key)) { 223 BUG("compiled shaders are not okay"); 224 return; 225 } 226 227 /* Update any derived state */ 228 if (!etna_state_update(ctx)) 229 return; 230 231 /* 232 * Figure out the buffers/features we need: 233 */ 234 if (etna_depth_enabled(ctx)) 235 resource_written(ctx, pfb->zsbuf->texture); 236 237 if (etna_stencil_enabled(ctx)) 238 resource_written(ctx, pfb->zsbuf->texture); 239 240 for (i = 0; i < pfb->nr_cbufs; i++) { 241 struct pipe_resource *surf; 242 243 if (!pfb->cbufs[i]) 244 continue; 245 246 surf = pfb->cbufs[i]->texture; 247 resource_written(ctx, surf); 248 } 249 250 /* Mark constant buffers as being read */ 251 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_VERTEX].buffer); 252 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].buffer); 253 254 /* Mark VBOs as being read */ 255 for (i = 0; i < ctx->vertex_buffer.count; i++) { 256 assert(!ctx->vertex_buffer.vb[i].is_user_buffer); 257 resource_read(ctx, ctx->vertex_buffer.vb[i].buffer.resource); 258 } 259 260 /* Mark index buffer as being read */ 261 resource_read(ctx, indexbuf); 262 263 /* Mark textures as being read */ 264 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) 265 if (ctx->sampler_view[i]) 266 resource_read(ctx, ctx->sampler_view[i]->texture); 267 268 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node) 269 resource_written(ctx, hq->prsc); 270 271 ctx->stats.prims_emitted += u_reduced_prims_for_vertices(info->mode, info->count); 272 ctx->stats.draw_calls++; 273 274 /* Update state for this draw operation */ 275 etna_update_state_for_draw(ctx, info); 276 277 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */ 278 etna_emit_state(ctx); 279 280 if (ctx->specs.halti >= 2) { 281 /* On HALTI2+ (GC3000 and higher) only use instanced drawing commands, as the blob does */ 282 etna_draw_instanced(ctx->stream, info->index_size, draw_mode, 1, 283 info->count, info->index_size ? info->index_bias : info->start); 284 } else { 285 if (info->index_size) 286 etna_draw_indexed_primitives(ctx->stream, draw_mode, 0, prims, info->index_bias); 287 else 288 etna_draw_primitives(ctx->stream, draw_mode, info->start, prims); 289 } 290 291 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL)) { 292 /* Stall the FE after every draw operation. This allows better 293 * debug of GPU hang conditions, as the FE will indicate which 294 * draw op has caused the hang. */ 295 etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE); 296 } 297 298 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL)) 299 pctx->flush(pctx, NULL, 0); 300 301 if (ctx->framebuffer_s.cbufs[0]) 302 etna_resource(ctx->framebuffer_s.cbufs[0]->texture)->seqno++; 303 if (ctx->framebuffer_s.zsbuf) 304 etna_resource(ctx->framebuffer_s.zsbuf->texture)->seqno++; 305 if (info->index_size && indexbuf != info->index.resource) 306 pipe_resource_reference(&indexbuf, NULL); 307} 308 309static void 310etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence, 311 enum pipe_flush_flags flags) 312{ 313 struct etna_context *ctx = etna_context(pctx); 314 int out_fence_fd = -1; 315 316 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node) 317 etna_hw_query_suspend(hq, ctx); 318 319 etna_cmd_stream_flush2(ctx->stream, ctx->in_fence_fd, 320 (flags & PIPE_FLUSH_FENCE_FD) ? &out_fence_fd : 321 NULL); 322 323 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node) 324 etna_hw_query_resume(hq, ctx); 325 326 if (fence) 327 *fence = etna_fence_create(pctx, out_fence_fd); 328} 329 330static void 331etna_cmd_stream_reset_notify(struct etna_cmd_stream *stream, void *priv) 332{ 333 struct etna_context *ctx = priv; 334 struct etna_resource *rsc, *rsc_tmp; 335 336 etna_set_state(stream, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENGL); 337 etna_set_state(stream, VIVS_GL_VERTEX_ELEMENT_CONFIG, 0x00000001); 338 /* blob sets this to 0x40000031 on GC7000, seems to make no difference, 339 * but keep it in mind if depth behaves strangely. */ 340 etna_set_state(stream, VIVS_RA_EARLY_DEPTH, 0x00000031); 341 etna_set_state(stream, VIVS_PA_W_CLIP_LIMIT, 0x34000001); 342 etna_set_state(stream, VIVS_PA_FLAGS, 0x00000000); /* blob sets ZCONVERT_BYPASS on GC3000+, this messes up z for us */ 343 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A80, 0x38a01404); 344 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A84, fui(8192.0)); 345 etna_set_state(stream, VIVS_PA_ZFARCLIPPING, 0x00000000); 346 etna_set_state(stream, VIVS_PE_ALPHA_COLOR_EXT0, 0x00000000); 347 etna_set_state(stream, VIVS_PE_ALPHA_COLOR_EXT1, 0x00000000); 348 etna_set_state(stream, VIVS_RA_HDEPTH_CONTROL, 0x00007000); 349 etna_set_state(stream, VIVS_PE_STENCIL_CONFIG_EXT2, 0x00000000); 350 etna_set_state(stream, VIVS_PS_CONTROL_EXT, 0x00000000); 351 352 /* There is no HALTI0 specific state */ 353 if (ctx->specs.halti >= 1) { /* Only on HALTI1+ */ 354 etna_set_state(stream, VIVS_VS_HALTI1_UNK00884, 0x00000808); 355 } 356 if (ctx->specs.halti >= 2) { /* Only on HALTI2+ */ 357 etna_set_state(stream, VIVS_RA_UNK00E0C, 0x00000000); 358 } 359 if (ctx->specs.halti >= 3) { /* Only on HALTI3+ */ 360 etna_set_state(stream, VIVS_PE_MEM_CONFIG, 0x00000000); /* TODO: cache modes */ 361 etna_set_state(stream, VIVS_PS_HALTI3_UNK0103C, 0x76543210); 362 } 363 if (ctx->specs.halti >= 4) { /* Only on HALTI4+ */ 364 etna_set_state(stream, VIVS_PS_MSAA_CONFIG, 0x6fffffff & 0xf70fffff & 0xfff6ffff & 365 0xffff6fff & 0xfffff6ff & 0xffffff7f); 366 etna_set_state(stream, VIVS_PE_HALTI4_UNK014C0, 0x00000000); 367 } 368 if (ctx->specs.halti >= 5) { /* Only on HALTI5+ */ 369 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_UNK14C40, 0x00000001); 370 etna_set_state(stream, VIVS_FE_HALTI5_UNK007D8, 0x00000002); 371 etna_set_state(stream, VIVS_FE_HALTI5_UNK007C4, 0x00000000); 372 etna_set_state(stream, VIVS_PS_SAMPLER_BASE, 0x00000000); 373 etna_set_state(stream, VIVS_VS_SAMPLER_BASE, 0x00000020); 374 etna_set_state(stream, VIVS_SH_CONFIG, VIVS_SH_CONFIG_RTNE_ROUNDING); 375 } else { /* Only on pre-HALTI5 */ 376 etna_set_state(stream, VIVS_GL_UNK03834, 0x00000000); 377 etna_set_state(stream, VIVS_GL_UNK03838, 0x00000000); 378 etna_set_state(stream, VIVS_GL_UNK03854, 0x00000000); 379 } 380 381 if (!ctx->specs.use_blt) { 382 /* Enable SINGLE_BUFFER for resolve, if supported */ 383 etna_set_state(stream, VIVS_RS_SINGLE_BUFFER, COND(ctx->specs.single_buffer, VIVS_RS_SINGLE_BUFFER_ENABLE)); 384 } 385 386 ctx->dirty = ~0L; 387 ctx->dirty_sampler_views = ~0L; 388 389 /* go through all the used resources and clear their status flag */ 390 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &ctx->used_resources, list) 391 { 392 debug_assert(rsc->status != 0); 393 rsc->status = 0; 394 rsc->pending_ctx = NULL; 395 list_delinit(&rsc->list); 396 } 397 398 assert(LIST_IS_EMPTY(&ctx->used_resources)); 399} 400 401static void 402etna_set_debug_callback(struct pipe_context *pctx, 403 const struct pipe_debug_callback *cb) 404{ 405 struct etna_context *ctx = etna_context(pctx); 406 407 if (cb) 408 ctx->debug = *cb; 409 else 410 memset(&ctx->debug, 0, sizeof(ctx->debug)); 411} 412 413struct pipe_context * 414etna_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags) 415{ 416 struct etna_context *ctx = CALLOC_STRUCT(etna_context); 417 struct etna_screen *screen; 418 struct pipe_context *pctx; 419 420 if (ctx == NULL) 421 return NULL; 422 423 pctx = &ctx->base; 424 pctx->priv = ctx; 425 pctx->screen = pscreen; 426 pctx->stream_uploader = u_upload_create_default(pctx); 427 if (!pctx->stream_uploader) 428 goto fail; 429 pctx->const_uploader = pctx->stream_uploader; 430 431 screen = etna_screen(pscreen); 432 ctx->stream = etna_cmd_stream_new(screen->pipe, 0x2000, &etna_cmd_stream_reset_notify, ctx); 433 if (ctx->stream == NULL) 434 goto fail; 435 436 /* context ctxate setup */ 437 ctx->specs = screen->specs; 438 ctx->screen = screen; 439 /* need some sane default in case state tracker doesn't set some state: */ 440 ctx->sample_mask = 0xffff; 441 442 list_inithead(&ctx->used_resources); 443 444 /* Set sensible defaults for state */ 445 etna_cmd_stream_reset_notify(ctx->stream, ctx); 446 447 ctx->in_fence_fd = -1; 448 449 pctx->destroy = etna_context_destroy; 450 pctx->draw_vbo = etna_draw_vbo; 451 pctx->flush = etna_flush; 452 pctx->set_debug_callback = etna_set_debug_callback; 453 pctx->create_fence_fd = etna_create_fence_fd; 454 pctx->fence_server_sync = etna_fence_server_sync; 455 456 /* creation of compile states */ 457 pctx->create_blend_state = etna_blend_state_create; 458 pctx->create_rasterizer_state = etna_rasterizer_state_create; 459 pctx->create_depth_stencil_alpha_state = etna_zsa_state_create; 460 461 etna_clear_blit_init(pctx); 462 etna_query_context_init(pctx); 463 etna_state_init(pctx); 464 etna_surface_init(pctx); 465 etna_shader_init(pctx); 466 etna_texture_init(pctx); 467 etna_transfer_init(pctx); 468 469 ctx->blitter = util_blitter_create(pctx); 470 if (!ctx->blitter) 471 goto fail; 472 473 /* Generate the bitmask of supported draw primitives. */ 474 ctx->prim_hwsupport = 1 << PIPE_PRIM_POINTS | 475 1 << PIPE_PRIM_LINES | 476 1 << PIPE_PRIM_LINE_STRIP | 477 1 << PIPE_PRIM_TRIANGLES | 478 1 << PIPE_PRIM_TRIANGLE_STRIP | 479 1 << PIPE_PRIM_TRIANGLE_FAN; 480 481 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, LINE_LOOP)) 482 ctx->prim_hwsupport |= 1 << PIPE_PRIM_LINE_LOOP; 483 484 ctx->primconvert = util_primconvert_create(pctx, ctx->prim_hwsupport); 485 if (!ctx->primconvert) 486 goto fail; 487 488 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool); 489 list_inithead(&ctx->active_hw_queries); 490 491 return pctx; 492 493fail: 494 pctx->destroy(pctx); 495 496 return NULL; 497} 498