Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**********************************************************
  2.  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person
  5.  * obtaining a copy of this software and associated documentation
  6.  * files (the "Software"), to deal in the Software without
  7.  * restriction, including without limitation the rights to use, copy,
  8.  * modify, merge, publish, distribute, sublicense, and/or sell copies
  9.  * of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be
  13.  * included in all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19.  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20.  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  *
  24.  **********************************************************/
  25.  
  26. #include "pipe/p_state.h"
  27. #include "pipe/p_context.h"
  28. #include "util/u_memory.h"
  29.  
  30. #include "svga_cmd.h"
  31. #include "svga_context.h"
  32. #include "svga_screen.h"
  33. #include "svga_resource_buffer.h"
  34. #include "svga_winsys.h"
  35. #include "svga_debug.h"
  36.  
  37.  
  38. /* Fixme: want a public base class for all pipe structs, even if there
  39.  * isn't much in them.
  40.  */
  41. struct pipe_query {
  42.    int dummy;
  43. };
  44.  
  45.  
  46. struct svga_query {
  47.    struct pipe_query base;
  48.    unsigned type;                  /**< PIPE_QUERY_x or SVGA_QUERY_x */
  49.    SVGA3dQueryType svga_type;      /**< SVGA3D_QUERYTYPE_x or unused */
  50.  
  51.    /** For PIPE_QUERY_OCCLUSION_COUNTER / SVGA3D_QUERYTYPE_OCCLUSION */
  52.    struct svga_winsys_buffer *hwbuf;
  53.    volatile SVGA3dQueryResult *queryResult;
  54.    struct pipe_fence_handle *fence;
  55.  
  56.    /** For non-GPU SVGA_QUERY_x queries */
  57.    uint64_t begin_count, end_count;
  58. };
  59.  
  60.  
  61. /** cast wrapper */
  62. static INLINE struct svga_query *
  63. svga_query( struct pipe_query *q )
  64. {
  65.    return (struct svga_query *)q;
  66. }
  67.  
  68.  
  69. static boolean
  70. svga_get_query_result(struct pipe_context *pipe,
  71.                       struct pipe_query *q,
  72.                       boolean wait,
  73.                       union pipe_query_result *result);
  74.  
  75.  
  76. static struct pipe_query *
  77. svga_create_query(struct pipe_context *pipe,
  78.                   unsigned query_type,
  79.                   unsigned index)
  80. {
  81.    struct svga_context *svga = svga_context( pipe );
  82.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  83.    struct svga_winsys_screen *sws = svgascreen->sws;
  84.    struct svga_query *sq;
  85.  
  86.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  87.  
  88.    sq = CALLOC_STRUCT(svga_query);
  89.    if (!sq)
  90.       goto no_sq;
  91.  
  92.    switch (query_type) {
  93.    case PIPE_QUERY_OCCLUSION_COUNTER:
  94.       sq->svga_type = SVGA3D_QUERYTYPE_OCCLUSION;
  95.  
  96.       sq->hwbuf = svga_winsys_buffer_create(svga, 1,
  97.                                             SVGA_BUFFER_USAGE_PINNED,
  98.                                             sizeof *sq->queryResult);
  99.       if (!sq->hwbuf) {
  100.          debug_printf("svga: failed to alloc query object!\n");
  101.          goto no_hwbuf;
  102.       }
  103.  
  104.       sq->queryResult = (SVGA3dQueryResult *)
  105.          sws->buffer_map(sws, sq->hwbuf, PIPE_TRANSFER_WRITE);
  106.       if (!sq->queryResult) {
  107.          debug_printf("svga: failed to map query object!\n");
  108.          goto no_query_result;
  109.       }
  110.  
  111.       sq->queryResult->totalSize = sizeof *sq->queryResult;
  112.       sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
  113.  
  114.       /* We request the buffer to be pinned and assume it is always mapped.
  115.        * The reason is that we don't want to wait for fences when checking the
  116.        * query status.
  117.        */
  118.       sws->buffer_unmap(sws, sq->hwbuf);
  119.       break;
  120.    case SVGA_QUERY_DRAW_CALLS:
  121.    case SVGA_QUERY_FALLBACKS:
  122.    case SVGA_QUERY_MEMORY_USED:
  123.       break;
  124.    default:
  125.       assert(!"unexpected query type in svga_create_query()");
  126.    }
  127.  
  128.    sq->type = query_type;
  129.  
  130.    return &sq->base;
  131.  
  132. no_query_result:
  133.    sws->buffer_destroy(sws, sq->hwbuf);
  134. no_hwbuf:
  135.    FREE(sq);
  136. no_sq:
  137.    return NULL;
  138. }
  139.  
  140.  
  141. static void
  142. svga_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
  143. {
  144.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  145.    struct svga_winsys_screen *sws = svgascreen->sws;
  146.    struct svga_query *sq = svga_query( q );
  147.  
  148.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  149.  
  150.    switch (sq->type) {
  151.    case PIPE_QUERY_OCCLUSION_COUNTER:
  152.       sws->buffer_destroy(sws, sq->hwbuf);
  153.       sq->hwbuf = NULL;
  154.       sws->fence_reference(sws, &sq->fence, NULL);
  155.       break;
  156.    case SVGA_QUERY_DRAW_CALLS:
  157.    case SVGA_QUERY_FALLBACKS:
  158.    case SVGA_QUERY_MEMORY_USED:
  159.       /* nothing */
  160.       break;
  161.    default:
  162.       assert(!"svga: unexpected query type in svga_destroy_query()");
  163.    }
  164.  
  165.    FREE(sq);
  166. }
  167.  
  168.  
  169. static boolean
  170. svga_begin_query(struct pipe_context *pipe, struct pipe_query *q)
  171. {
  172.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  173.    struct svga_winsys_screen *sws = svgascreen->sws;
  174.    struct svga_context *svga = svga_context( pipe );
  175.    struct svga_query *sq = svga_query( q );
  176.    enum pipe_error ret;
  177.  
  178.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  179.  
  180.    /* Need to flush out buffered drawing commands so that they don't
  181.     * get counted in the query results.
  182.     */
  183.    svga_hwtnl_flush_retry(svga);
  184.  
  185.    switch (sq->type) {
  186.    case PIPE_QUERY_OCCLUSION_COUNTER:
  187.       assert(!svga->sq);
  188.       if (sq->queryResult->state == SVGA3D_QUERYSTATE_PENDING) {
  189.          /* The application doesn't care for the pending query result.
  190.           * We cannot let go of the existing buffer and just get a new one
  191.           * because its storage may be reused for other purposes and clobbered
  192.           * by the host when it determines the query result.  So the only
  193.           * option here is to wait for the existing query's result -- not a
  194.           * big deal, given that no sane application would do this.
  195.           */
  196.          uint64_t result;
  197.          svga_get_query_result(pipe, q, TRUE, (void*)&result);
  198.          assert(sq->queryResult->state != SVGA3D_QUERYSTATE_PENDING);
  199.       }
  200.  
  201.       sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
  202.       sws->fence_reference(sws, &sq->fence, NULL);
  203.  
  204.       ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
  205.       if (ret != PIPE_OK) {
  206.          svga_context_flush(svga, NULL);
  207.          ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
  208.          assert(ret == PIPE_OK);
  209.       }
  210.  
  211.       svga->sq = sq;
  212.       break;
  213.    case SVGA_QUERY_DRAW_CALLS:
  214.       sq->begin_count = svga->num_draw_calls;
  215.       break;
  216.    case SVGA_QUERY_FALLBACKS:
  217.       sq->begin_count = svga->num_fallbacks;
  218.       break;
  219.    case SVGA_QUERY_MEMORY_USED:
  220.       /* nothing */
  221.       break;
  222.    default:
  223.       assert(!"unexpected query type in svga_begin_query()");
  224.    }
  225.    return true;
  226. }
  227.  
  228.  
  229. static void
  230. svga_end_query(struct pipe_context *pipe, struct pipe_query *q)
  231. {
  232.    struct svga_context *svga = svga_context( pipe );
  233.    struct svga_query *sq = svga_query( q );
  234.    enum pipe_error ret;
  235.  
  236.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  237.  
  238.    svga_hwtnl_flush_retry(svga);
  239.  
  240.    switch (sq->type) {
  241.    case PIPE_QUERY_OCCLUSION_COUNTER:
  242.       assert(svga->sq == sq);
  243.  
  244.       /* Set to PENDING before sending EndQuery. */
  245.       sq->queryResult->state = SVGA3D_QUERYSTATE_PENDING;
  246.  
  247.       ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
  248.       if (ret != PIPE_OK) {
  249.          svga_context_flush(svga, NULL);
  250.          ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
  251.          assert(ret == PIPE_OK);
  252.       }
  253.  
  254.       /* TODO: Delay flushing. We don't really need to flush here, just ensure
  255.        * that there is one flush before svga_get_query_result attempts to get
  256.        * the result.
  257.        */
  258.       svga_context_flush(svga, NULL);
  259.  
  260.       svga->sq = NULL;
  261.       break;
  262.    case SVGA_QUERY_DRAW_CALLS:
  263.       sq->end_count = svga->num_draw_calls;
  264.       break;
  265.    case SVGA_QUERY_FALLBACKS:
  266.       sq->end_count = svga->num_fallbacks;
  267.       break;
  268.    case SVGA_QUERY_MEMORY_USED:
  269.       /* nothing */
  270.       break;
  271.    default:
  272.       assert(!"unexpected query type in svga_end_query()");
  273.    }
  274. }
  275.  
  276.  
  277. static boolean
  278. svga_get_query_result(struct pipe_context *pipe,
  279.                       struct pipe_query *q,
  280.                       boolean wait,
  281.                       union pipe_query_result *vresult)
  282. {
  283.    struct svga_context *svga = svga_context( pipe );
  284.    struct svga_screen *svgascreen = svga_screen( pipe->screen );
  285.    struct svga_winsys_screen *sws = svgascreen->sws;
  286.    struct svga_query *sq = svga_query( q );
  287.    SVGA3dQueryState state;
  288.    uint64_t *result = (uint64_t *) vresult;
  289.  
  290.    SVGA_DBG(DEBUG_QUERY, "%s wait: %d\n", __FUNCTION__);
  291.  
  292.    switch (sq->type) {
  293.    case PIPE_QUERY_OCCLUSION_COUNTER:
  294.       /* The query status won't be updated by the host unless
  295.        * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
  296.        * a synchronous wait on the host.
  297.        */
  298.       if (!sq->fence) {
  299.          enum pipe_error ret;
  300.  
  301.          ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
  302.          if (ret != PIPE_OK) {
  303.             svga_context_flush(svga, NULL);
  304.             ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
  305.             assert(ret == PIPE_OK);
  306.          }
  307.  
  308.          svga_context_flush(svga, &sq->fence);
  309.  
  310.          assert(sq->fence);
  311.       }
  312.  
  313.       state = sq->queryResult->state;
  314.       if (state == SVGA3D_QUERYSTATE_PENDING) {
  315.          if (!wait)
  316.             return FALSE;
  317.          sws->fence_finish(sws, sq->fence, SVGA_FENCE_FLAG_QUERY);
  318.          state = sq->queryResult->state;
  319.       }
  320.  
  321.       assert(state == SVGA3D_QUERYSTATE_SUCCEEDED ||
  322.              state == SVGA3D_QUERYSTATE_FAILED);
  323.  
  324.       *result = (uint64_t) sq->queryResult->result32;
  325.       break;
  326.    case SVGA_QUERY_DRAW_CALLS:
  327.       /* fall-through */
  328.    case SVGA_QUERY_FALLBACKS:
  329.       vresult->u64 = sq->end_count - sq->begin_count;
  330.       break;
  331.    case SVGA_QUERY_MEMORY_USED:
  332.       vresult->u64 = svgascreen->total_resource_bytes;
  333.       break;
  334.    default:
  335.       assert(!"unexpected query type in svga_get_query_result");
  336.    }
  337.  
  338.    SVGA_DBG(DEBUG_QUERY, "%s result %d\n", __FUNCTION__, (unsigned)*result);
  339.  
  340.    return TRUE;
  341. }
  342.  
  343.  
  344. void
  345. svga_init_query_functions(struct svga_context *svga)
  346. {
  347.    svga->pipe.create_query = svga_create_query;
  348.    svga->pipe.destroy_query = svga_destroy_query;
  349.    svga->pipe.begin_query = svga_begin_query;
  350.    svga->pipe.end_query = svga_end_query;
  351.    svga->pipe.get_query_result = svga_get_query_result;
  352. }
  353.