swr_draw.cpp revision 2b2d3680bf164ec4f8b50436b96c3fc195318ea5
12b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley/****************************************************************************
22b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
32b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley *
42b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Permission is hereby granted, free of charge, to any person obtaining a
52b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * copy of this software and associated documentation files (the "Software"),
62b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * to deal in the Software without restriction, including without limitation
72b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * and/or sell copies of the Software, and to permit persons to whom the
92b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Software is furnished to do so, subject to the following conditions:
102b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley *
112b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * The above copyright notice and this permission notice (including the next
122b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * paragraph) shall be included in all copies or substantial portions of the
132b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Software.
142b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley *
152b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
212b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * IN THE SOFTWARE.
222b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley ***************************************************************************/
232b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
242b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "swr_screen.h"
252b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "swr_context.h"
262b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "swr_resource.h"
272b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "swr_fence.h"
282b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "swr_query.h"
292b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "jit_api.h"
302b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
312b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "util/u_draw.h"
322b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley#include "util/u_prim.h"
332b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
342b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley/*
352b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Convert mesa PIPE_PRIM_X to SWR enum PRIMITIVE_TOPOLOGY
362b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley */
372b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleystatic INLINE enum PRIMITIVE_TOPOLOGY
382b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_convert_prim_topology(const unsigned mode)
392b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
402b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   switch (mode) {
412b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_POINTS:
422b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_POINT_LIST;
432b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_LINES:
442b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_LINE_LIST;
452b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_LINE_LOOP:
462b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_LINE_LOOP;
472b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_LINE_STRIP:
482b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_LINE_STRIP;
492b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_TRIANGLES:
502b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRIANGLE_LIST;
512b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_TRIANGLE_STRIP:
522b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRIANGLE_STRIP;
532b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_TRIANGLE_FAN:
542b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRIANGLE_FAN;
552b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_QUADS:
562b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_QUAD_LIST;
572b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_QUAD_STRIP:
582b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_QUAD_STRIP;
592b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_POLYGON:
602b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRIANGLE_FAN; /* XXX TOP_POLYGON; */
612b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_LINES_ADJACENCY:
622b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_LINE_LIST_ADJ;
632b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_LINE_STRIP_ADJACENCY:
642b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_LISTSTRIP_ADJ;
652b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_TRIANGLES_ADJACENCY:
662b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRI_LIST_ADJ;
672b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
682b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_TRI_STRIP_ADJ;
692b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   default:
702b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      assert(0 && "Unknown topology");
712b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return TOP_UNKNOWN;
722b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
732b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley};
742b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
752b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
762b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley/*
772b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Draw vertex arrays, with optional indexing, optional instancing.
782b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley */
792b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleystatic void
802b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
812b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
822b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_context *ctx = swr_context(pipe);
832b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
842b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (!swr_check_render_cond(pipe))
852b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return;
862b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
872b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (info->indirect) {
882b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      util_draw_indirect(pipe, info);
892b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      return;
902b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
912b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
922b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   /* Update derived state, pass draw info to update function */
932b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (ctx->dirty)
942b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      swr_update_derived(ctx, info);
952b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
962b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   swr_update_draw_context(ctx);
972b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
982b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (ctx->vs->pipe.stream_output.num_outputs) {
992b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      if (!ctx->vs->soFunc[info->mode]) {
1002b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         STREAMOUT_COMPILE_STATE state = {0};
1012b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         struct pipe_stream_output_info *so = &ctx->vs->pipe.stream_output;
1022b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1032b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         state.numVertsPerPrim = u_vertices_per_prim(info->mode);
1042b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1052b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         uint32_t offsets[MAX_SO_STREAMS] = {0};
1062b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         uint32_t num = 0;
1072b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1082b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         for (uint32_t i = 0; i < so->num_outputs; i++) {
1092b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            assert(so->output[i].stream == 0); // @todo
1102b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            uint32_t output_buffer = so->output[i].output_buffer;
1112b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            if (so->output[i].dst_offset != offsets[output_buffer]) {
1122b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               // hole - need to fill
1132b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               state.stream.decl[num].bufferIndex = output_buffer;
1142b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               state.stream.decl[num].hole = true;
1152b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               state.stream.decl[num].componentMask =
1162b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                  (1 << (so->output[i].dst_offset - offsets[output_buffer]))
1172b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                  - 1;
1182b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               num++;
1192b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               offsets[output_buffer] = so->output[i].dst_offset;
1202b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            }
1212b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1222b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            state.stream.decl[num].bufferIndex = output_buffer;
1232b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            state.stream.decl[num].attribSlot = so->output[i].register_index - 1;
1242b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            state.stream.decl[num].componentMask =
1252b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               ((1 << so->output[i].num_components) - 1)
1262b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley               << so->output[i].start_component;
1272b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            state.stream.decl[num].hole = false;
1282b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            num++;
1292b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1302b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley            offsets[output_buffer] += so->output[i].num_components;
1312b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         }
1322b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1332b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         state.stream.numDecls = num;
1342b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1352b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         HANDLE hJitMgr = swr_screen(pipe->screen)->hJitMgr;
1362b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         ctx->vs->soFunc[info->mode] = JitCompileStreamout(hJitMgr, state);
1372b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         debug_printf("so shader    %p\n", ctx->vs->soFunc[info->mode]);
1382b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         assert(ctx->vs->soFunc[info->mode] && "Error: SoShader = NULL");
1392b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      }
1402b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1412b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      SwrSetSoFunc(ctx->swrContext, ctx->vs->soFunc[info->mode], 0);
1422b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
1432b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1442b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_vertex_element_state *velems = ctx->velems;
1452b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (!velems->fsFunc
1462b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley       || (velems->fsState.cutIndex != info->restart_index)
1472b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley       || (velems->fsState.bEnableCutIndex != info->primitive_restart)) {
1482b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1492b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      velems->fsState.cutIndex = info->restart_index;
1502b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      velems->fsState.bEnableCutIndex = info->primitive_restart;
1512b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1522b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      /* Create Fetch Shader */
1532b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      HANDLE hJitMgr = swr_screen(ctx->pipe.screen)->hJitMgr;
1542b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      velems->fsFunc = JitCompileFetch(hJitMgr, velems->fsState);
1552b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1562b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      debug_printf("fetch shader %p\n", velems->fsFunc);
1572b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      assert(velems->fsFunc && "Error: FetchShader = NULL");
1582b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
1592b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1602b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   SwrSetFetchFunc(ctx->swrContext, velems->fsFunc);
1612b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1622b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (info->indexed)
1632b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      SwrDrawIndexedInstanced(ctx->swrContext,
1642b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              swr_convert_prim_topology(info->mode),
1652b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              info->count,
1662b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              info->instance_count,
1672b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              info->start,
1682b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              info->index_bias,
1692b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                              info->start_instance);
1702b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   else
1712b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      SwrDrawInstanced(ctx->swrContext,
1722b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                       swr_convert_prim_topology(info->mode),
1732b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                       info->count,
1742b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                       info->instance_count,
1752b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                       info->start,
1762b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                       info->start_instance);
1772b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley}
1782b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1792b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1802b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleystatic void
1812b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_flush(struct pipe_context *pipe,
1822b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley          struct pipe_fence_handle **fence,
1832b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley          unsigned flags)
1842b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
1852b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_context *ctx = swr_context(pipe);
1862b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_screen *screen = swr_screen(pipe->screen);
1872b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1882b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   /* If the current renderTarget is the display surface, store tiles back to
1892b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley    * the surface, in
1902b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley    * preparation for present (swr_flush_frontbuffer)
1912b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley    */
1922b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct pipe_surface *cb = ctx->framebuffer.cbufs[0];
1932b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (cb && swr_resource(cb->texture)->display_target) {
1942b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      swr_store_render_target(ctx, SWR_ATTACHMENT_COLOR0, SWR_TILE_RESOLVED);
1952b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      swr_resource(cb->texture)->bound_to_context = (void*)pipe;
1962b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
1972b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
1982b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   // SwrStoreTiles is asynchronous, always submit the "flush" fence.
1992b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   // flush_frontbuffer needs it.
2002b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   swr_fence_submit(ctx, screen->flush_fence);
2012b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2022b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (fence)
2032b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      swr_fence_reference(pipe->screen, fence, screen->flush_fence);
2042b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley}
2052b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2062b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyvoid
2072b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_finish(struct pipe_context *pipe)
2082b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
2092b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_screen *screen = swr_screen(pipe->screen);
2102b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct pipe_fence_handle *fence = NULL;
2112b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2122b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   swr_flush(pipe, &fence, 0);
2132b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   swr_fence_finish(&screen->base, fence, 0);
2142b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   swr_fence_reference(&screen->base, &fence, NULL);
2152b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley}
2162b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2172b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2182b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley/*
2192b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley * Store SWR HotTiles back to RenderTarget surface.
2202b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley */
2212b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyvoid
2222b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_store_render_target(struct swr_context *ctx,
2232b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                        uint32_t attachment,
2242b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                        enum SWR_TILE_STATE post_tile_state)
2252b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
2262b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct swr_draw_context *pDC = &ctx->swrDC;
2272b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   struct SWR_SURFACE_STATE *renderTarget = &pDC->renderTargets[attachment];
2282b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2292b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   /* Only proceed if there's a valid surface to store to */
2302b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   if (renderTarget->pBaseAddress) {
2312b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      /* Set viewport to full renderTarget width/height and disable scissor
2322b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley       * before StoreTiles */
2332b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      boolean change_viewport =
2342b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         (ctx->derived.vp.x != 0.0f || ctx->derived.vp.y != 0.0f
2352b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley          || ctx->derived.vp.width != renderTarget->width
2362b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley          || ctx->derived.vp.height != renderTarget->height);
2372b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      if (change_viewport) {
2382b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         SWR_VIEWPORT vp = {0};
2392b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         vp.width = renderTarget->width;
2402b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         vp.height = renderTarget->height;
2412b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         SwrSetViewports(ctx->swrContext, 1, &vp, NULL);
2422b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      }
2432b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2442b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      boolean scissor_enable = ctx->derived.rastState.scissorEnable;
2452b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      if (scissor_enable) {
2462b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         ctx->derived.rastState.scissorEnable = FALSE;
2472b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         SwrSetRastState(ctx->swrContext, &ctx->derived.rastState);
2482b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      }
2492b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2502b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      swr_update_draw_context(ctx);
2512b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      SwrStoreTiles(ctx->swrContext,
2522b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                    (enum SWR_RENDERTARGET_ATTACHMENT)attachment,
2532b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley                    post_tile_state);
2542b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2552b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      /* Restore viewport and scissor enable */
2562b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      if (change_viewport)
2572b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         SwrSetViewports(ctx->swrContext, 1, &ctx->derived.vp, &ctx->derived.vpm);
2582b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      if (scissor_enable) {
2592b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         ctx->derived.rastState.scissorEnable = scissor_enable;
2602b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley         SwrSetRastState(ctx->swrContext, &ctx->derived.rastState);
2612b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley      }
2622b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   }
2632b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley}
2642b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2652b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley
2662b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyvoid
2672b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowleyswr_draw_init(struct pipe_context *pipe)
2682b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley{
2692b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   pipe->draw_vbo = swr_draw_vbo;
2702b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley   pipe->flush = swr_flush;
2712b2d3680bf164ec4f8b50436b96c3fc195318ea5Tim Rowley}
272