Subversion Repositories Kolibri OS

Rev

Go to most recent revision | 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, unsigned query_type )
  78. {
  79.    struct svga_context *svga = svga_context( pipe );
  80.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  81.    struct svga_winsys_screen *sws = svgascreen->sws;
  82.    struct svga_query *sq;
  83.  
  84.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  85.  
  86.    sq = CALLOC_STRUCT(svga_query);
  87.    if (!sq)
  88.       goto no_sq;
  89.  
  90.    switch (query_type) {
  91.    case PIPE_QUERY_OCCLUSION_COUNTER:
  92.       sq->svga_type = SVGA3D_QUERYTYPE_OCCLUSION;
  93.  
  94.       sq->hwbuf = svga_winsys_buffer_create(svga, 1,
  95.                                             SVGA_BUFFER_USAGE_PINNED,
  96.                                             sizeof *sq->queryResult);
  97.       if (!sq->hwbuf)
  98.          goto no_hwbuf;
  99.  
  100.       sq->queryResult = (SVGA3dQueryResult *)
  101.          sws->buffer_map(sws, sq->hwbuf, PIPE_TRANSFER_WRITE);
  102.       if (!sq->queryResult)
  103.          goto no_query_result;
  104.  
  105.       sq->queryResult->totalSize = sizeof *sq->queryResult;
  106.       sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
  107.  
  108.       /* We request the buffer to be pinned and assume it is always mapped.
  109.        * The reason is that we don't want to wait for fences when checking the
  110.        * query status.
  111.        */
  112.       sws->buffer_unmap(sws, sq->hwbuf);
  113.       break;
  114.    case SVGA_QUERY_DRAW_CALLS:
  115.    case SVGA_QUERY_FALLBACKS:
  116.    case SVGA_QUERY_MEMORY_USED:
  117.       break;
  118.    default:
  119.       assert(!"unexpected query type in svga_create_query()");
  120.    }
  121.  
  122.    sq->type = query_type;
  123.  
  124.    return &sq->base;
  125.  
  126. no_query_result:
  127.    sws->buffer_destroy(sws, sq->hwbuf);
  128. no_hwbuf:
  129.    FREE(sq);
  130. no_sq:
  131.    return NULL;
  132. }
  133.  
  134.  
  135. static void
  136. svga_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
  137. {
  138.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  139.    struct svga_winsys_screen *sws = svgascreen->sws;
  140.    struct svga_query *sq = svga_query( q );
  141.  
  142.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  143.  
  144.    switch (sq->type) {
  145.    case PIPE_QUERY_OCCLUSION_COUNTER:
  146.       sws->buffer_destroy(sws, sq->hwbuf);
  147.       sws->fence_reference(sws, &sq->fence, NULL);
  148.       break;
  149.    case SVGA_QUERY_DRAW_CALLS:
  150.    case SVGA_QUERY_FALLBACKS:
  151.    case SVGA_QUERY_MEMORY_USED:
  152.       /* nothing */
  153.       break;
  154.    default:
  155.       assert(!"svga: unexpected query type in svga_destroy_query()");
  156.    }
  157.  
  158.    FREE(sq);
  159. }
  160.  
  161.  
  162. static void
  163. svga_begin_query(struct pipe_context *pipe, struct pipe_query *q)
  164. {
  165.    struct svga_screen *svgascreen = svga_screen(pipe->screen);
  166.    struct svga_winsys_screen *sws = svgascreen->sws;
  167.    struct svga_context *svga = svga_context( pipe );
  168.    struct svga_query *sq = svga_query( q );
  169.    enum pipe_error ret;
  170.  
  171.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  172.  
  173.    /* Need to flush out buffered drawing commands so that they don't
  174.     * get counted in the query results.
  175.     */
  176.    svga_hwtnl_flush_retry(svga);
  177.  
  178.    switch (sq->type) {
  179.    case PIPE_QUERY_OCCLUSION_COUNTER:
  180.       assert(!svga->sq);
  181.       if (sq->queryResult->state == SVGA3D_QUERYSTATE_PENDING) {
  182.          /* The application doesn't care for the pending query result.
  183.           * We cannot let go of the existing buffer and just get a new one
  184.           * because its storage may be reused for other purposes and clobbered
  185.           * by the host when it determines the query result.  So the only
  186.           * option here is to wait for the existing query's result -- not a
  187.           * big deal, given that no sane application would do this.
  188.           */
  189.          uint64_t result;
  190.          svga_get_query_result(pipe, q, TRUE, (void*)&result);
  191.          assert(sq->queryResult->state != SVGA3D_QUERYSTATE_PENDING);
  192.       }
  193.  
  194.       sq->queryResult->state = SVGA3D_QUERYSTATE_NEW;
  195.       sws->fence_reference(sws, &sq->fence, NULL);
  196.  
  197.       ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
  198.       if (ret != PIPE_OK) {
  199.          svga_context_flush(svga, NULL);
  200.          ret = SVGA3D_BeginQuery(svga->swc, sq->svga_type);
  201.          assert(ret == PIPE_OK);
  202.       }
  203.  
  204.       svga->sq = sq;
  205.       break;
  206.    case SVGA_QUERY_DRAW_CALLS:
  207.       sq->begin_count = svga->num_draw_calls;
  208.       break;
  209.    case SVGA_QUERY_FALLBACKS:
  210.       sq->begin_count = svga->num_fallbacks;
  211.       break;
  212.    case SVGA_QUERY_MEMORY_USED:
  213.       /* nothing */
  214.       break;
  215.    default:
  216.       assert(!"unexpected query type in svga_begin_query()");
  217.    }
  218. }
  219.  
  220.  
  221. static void
  222. svga_end_query(struct pipe_context *pipe, struct pipe_query *q)
  223. {
  224.    struct svga_context *svga = svga_context( pipe );
  225.    struct svga_query *sq = svga_query( q );
  226.    enum pipe_error ret;
  227.  
  228.    SVGA_DBG(DEBUG_QUERY, "%s\n", __FUNCTION__);
  229.  
  230.    svga_hwtnl_flush_retry(svga);
  231.  
  232.    switch (sq->type) {
  233.    case PIPE_QUERY_OCCLUSION_COUNTER:
  234.       assert(svga->sq == sq);
  235.  
  236.       /* Set to PENDING before sending EndQuery. */
  237.       sq->queryResult->state = SVGA3D_QUERYSTATE_PENDING;
  238.  
  239.       ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
  240.       if (ret != PIPE_OK) {
  241.          svga_context_flush(svga, NULL);
  242.          ret = SVGA3D_EndQuery( svga->swc, sq->svga_type, sq->hwbuf);
  243.          assert(ret == PIPE_OK);
  244.       }
  245.  
  246.       /* TODO: Delay flushing. We don't really need to flush here, just ensure
  247.        * that there is one flush before svga_get_query_result attempts to get
  248.        * the result.
  249.        */
  250.       svga_context_flush(svga, NULL);
  251.  
  252.       svga->sq = NULL;
  253.       break;
  254.    case SVGA_QUERY_DRAW_CALLS:
  255.       sq->end_count = svga->num_draw_calls;
  256.       break;
  257.    case SVGA_QUERY_FALLBACKS:
  258.       sq->end_count = svga->num_fallbacks;
  259.       break;
  260.    case SVGA_QUERY_MEMORY_USED:
  261.       /* nothing */
  262.       break;
  263.    default:
  264.       assert(!"unexpected query type in svga_end_query()");
  265.    }
  266. }
  267.  
  268.  
  269. static boolean
  270. svga_get_query_result(struct pipe_context *pipe,
  271.                       struct pipe_query *q,
  272.                       boolean wait,
  273.                       union pipe_query_result *vresult)
  274. {
  275.    struct svga_context *svga = svga_context( pipe );
  276.    struct svga_screen *svgascreen = svga_screen( pipe->screen );
  277.    struct svga_winsys_screen *sws = svgascreen->sws;
  278.    struct svga_query *sq = svga_query( q );
  279.    SVGA3dQueryState state;
  280.    uint64_t *result = (uint64_t *) vresult;
  281.  
  282.    SVGA_DBG(DEBUG_QUERY, "%s wait: %d\n", __FUNCTION__);
  283.  
  284.    switch (sq->type) {
  285.    case PIPE_QUERY_OCCLUSION_COUNTER:
  286.       /* The query status won't be updated by the host unless
  287.        * SVGA_3D_CMD_WAIT_FOR_QUERY is emitted. Unfortunately this will cause
  288.        * a synchronous wait on the host.
  289.        */
  290.       if (!sq->fence) {
  291.          enum pipe_error ret;
  292.  
  293.          ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
  294.          if (ret != PIPE_OK) {
  295.             svga_context_flush(svga, NULL);
  296.             ret = SVGA3D_WaitForQuery( svga->swc, sq->svga_type, sq->hwbuf);
  297.             assert(ret == PIPE_OK);
  298.          }
  299.  
  300.          svga_context_flush(svga, &sq->fence);
  301.  
  302.          assert(sq->fence);
  303.       }
  304.  
  305.       state = sq->queryResult->state;
  306.       if (state == SVGA3D_QUERYSTATE_PENDING) {
  307.          if (!wait)
  308.             return FALSE;
  309.          sws->fence_finish(sws, sq->fence, SVGA_FENCE_FLAG_QUERY);
  310.          state = sq->queryResult->state;
  311.       }
  312.  
  313.       assert(state == SVGA3D_QUERYSTATE_SUCCEEDED ||
  314.              state == SVGA3D_QUERYSTATE_FAILED);
  315.  
  316.       *result = (uint64_t) sq->queryResult->result32;
  317.       break;
  318.    case SVGA_QUERY_DRAW_CALLS:
  319.       /* fall-through */
  320.    case SVGA_QUERY_FALLBACKS:
  321.       vresult->u64 = sq->end_count - sq->begin_count;
  322.       break;
  323.    case SVGA_QUERY_MEMORY_USED:
  324.       vresult->u64 = svgascreen->total_resource_bytes;
  325.       break;
  326.    default:
  327.       assert(!"unexpected query type in svga_get_query_result");
  328.    }
  329.  
  330.    SVGA_DBG(DEBUG_QUERY, "%s result %d\n", __FUNCTION__, (unsigned)*result);
  331.  
  332.    return TRUE;
  333. }
  334.  
  335.  
  336. void
  337. svga_init_query_functions(struct svga_context *svga)
  338. {
  339.    svga->pipe.create_query = svga_create_query;
  340.    svga->pipe.destroy_query = svga_destroy_query;
  341.    svga->pipe.begin_query = svga_begin_query;
  342.    svga->pipe.end_query = svga_end_query;
  343.    svga->pipe.get_query_result = svga_get_query_result;
  344. }
  345.