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. #ifndef SVGA_BUFFER_H
  27. #define SVGA_BUFFER_H
  28.  
  29.  
  30. #include "pipe/p_compiler.h"
  31. #include "pipe/p_state.h"
  32. #include "util/u_transfer.h"
  33.  
  34. #include "svga_screen_cache.h"
  35. #include "svga_screen.h"
  36. #include "svga_cmd.h"
  37. #include "svga_context.h"
  38.  
  39.  
  40. /**
  41.  * Maximum number of discontiguous ranges
  42.  */
  43. #define SVGA_BUFFER_MAX_RANGES 32
  44.  
  45.  
  46. struct svga_context;
  47. struct svga_winsys_buffer;
  48. struct svga_winsys_surface;
  49.  
  50.  
  51. extern struct u_resource_vtbl svga_buffer_vtbl;
  52.  
  53. struct svga_buffer_range
  54. {
  55.    unsigned start;
  56.    unsigned end;
  57. };
  58.  
  59. struct svga_3d_update_gb_image;
  60.  
  61. /**
  62.  * SVGA pipe buffer.
  63.  */
  64. struct svga_buffer
  65. {
  66.    struct u_resource b;
  67.  
  68.    /**
  69.     * Regular (non DMA'able) memory.
  70.     *
  71.     * Used for user buffers or for buffers which we know before hand that can
  72.     * never be used by the virtual hardware directly, such as constant buffers.
  73.     */
  74.    void *swbuf;
  75.    
  76.    /**
  77.     * Whether swbuf was created by the user or not.
  78.     */
  79.    boolean user;
  80.    
  81.    /**
  82.     * Creation key for the host surface handle.
  83.     *
  84.     * This structure describes all the host surface characteristics so that it
  85.     * can be looked up in cache, since creating a host surface is often a slow
  86.     * operation.
  87.     */
  88.    struct svga_host_surface_cache_key key;
  89.    
  90.    /**
  91.     * Host surface handle.
  92.     *
  93.     * This is a platform independent abstraction for host SID. We create when
  94.     * trying to bind.
  95.     *
  96.     * Only set for non-user buffers.
  97.     */
  98.    struct svga_winsys_surface *handle;
  99.  
  100.    /**
  101.     * Information about ongoing and past map operations.
  102.     */
  103.    struct {
  104.       /**
  105.        * Number of concurrent mappings.
  106.        */
  107.       unsigned count;
  108.  
  109.       /**
  110.        * Dirty ranges.
  111.        *
  112.        * Ranges that were touched by the application and need to be uploaded to
  113.        * the host.
  114.        *
  115.        * This information will be copied into dma.boxes, when emiting the
  116.        * SVGA3dCmdSurfaceDMA command.
  117.        */
  118.       struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];
  119.       unsigned num_ranges;
  120.    } map;
  121.  
  122.    /**
  123.     * Information about uploaded version of user buffers.
  124.     */
  125.    struct {
  126.       struct pipe_resource *buffer;
  127.  
  128.       /**
  129.        * We combine multiple user buffers into the same hardware buffer. This
  130.        * is the relative offset within that buffer.
  131.        */
  132.       unsigned offset;
  133.  
  134.       /**
  135.        * Range of user buffer that is uploaded in @buffer at @offset.
  136.        */
  137.       unsigned start;
  138.       unsigned end;
  139.    } uploaded;
  140.  
  141.    /**
  142.     * DMA'ble memory.
  143.     *
  144.     * A piece of GMR memory, with the same size of the buffer. It is created
  145.     * when mapping the buffer, and will be used to upload vertex data to the
  146.     * host.
  147.     *
  148.     * Only set for non-user buffers.
  149.     */
  150.    struct svga_winsys_buffer *hwbuf;
  151.  
  152.    /**
  153.     * Information about pending DMA uploads.
  154.     *
  155.     */
  156.    struct {
  157.       /**
  158.        * Whether this buffer has an unfinished DMA upload command.
  159.        *
  160.        * If not set then the rest of the information is null.
  161.        */
  162.       boolean pending;
  163.  
  164.       SVGA3dSurfaceDMAFlags flags;
  165.  
  166.       /**
  167.        * Pointer to the DMA copy box *inside* the command buffer.
  168.        */
  169.       SVGA3dCopyBox *boxes;
  170.  
  171.       /**
  172.        * Pointer to the sequence of update commands
  173.        * *inside* the command buffer.
  174.        */
  175.       struct svga_3d_update_gb_image *updates;
  176.  
  177.       /**
  178.        * Context that has the pending DMA to this buffer.
  179.        */
  180.       struct svga_context *svga;
  181.    } dma;
  182.  
  183.    /**
  184.     * Linked list head, used to gather all buffers with pending dma uploads on
  185.     * a context. It is only valid if the dma.pending is set above.
  186.     */
  187.    struct list_head head;
  188.  
  189.    unsigned size;  /**< Approximate size in bytes */
  190. };
  191.  
  192.  
  193. static INLINE struct svga_buffer *
  194. svga_buffer(struct pipe_resource *buffer)
  195. {
  196.    if (buffer) {
  197.       assert(((struct svga_buffer *)buffer)->b.vtbl == &svga_buffer_vtbl);
  198.       return (struct svga_buffer *)buffer;
  199.    }
  200.    return NULL;
  201. }
  202.  
  203.  
  204. /**
  205.  * Returns TRUE for user buffers.  We may
  206.  * decide to use an alternate upload path for these buffers.
  207.  */
  208. static INLINE boolean
  209. svga_buffer_is_user_buffer( struct pipe_resource *buffer )
  210. {
  211.    if (buffer) {
  212.       return svga_buffer(buffer)->user;
  213.    } else {
  214.       return FALSE;
  215.    }
  216. }
  217.  
  218. /**
  219.  * Returns a pointer to a struct svga_winsys_screen given a
  220.  * struct svga_buffer.
  221.  */
  222. static INLINE struct svga_winsys_screen *
  223. svga_buffer_winsys_screen(struct svga_buffer *sbuf)
  224. {
  225.    return svga_screen(sbuf->b.b.screen)->sws;
  226. }
  227.  
  228.  
  229. /**
  230.  * Returns whether a buffer has hardware storage that is
  231.  * visible to the GPU.
  232.  */
  233. static INLINE boolean
  234. svga_buffer_has_hw_storage(struct svga_buffer *sbuf)
  235. {
  236.    if (svga_buffer_winsys_screen(sbuf)->have_gb_objects)
  237.       return (sbuf->handle ? TRUE : FALSE);
  238.    else
  239.       return (sbuf->hwbuf ? TRUE : FALSE);
  240. }
  241.  
  242. /**
  243.  * Map the hardware storage of a buffer.
  244.  */
  245. static INLINE void *
  246. svga_buffer_hw_storage_map(struct svga_context *svga,
  247.                            struct svga_buffer *sbuf,
  248.                            unsigned flags, boolean *retry)
  249. {
  250.    struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
  251.    if (sws->have_gb_objects) {
  252.       return svga->swc->surface_map(svga->swc, sbuf->handle, flags, retry);
  253.    } else {
  254.       *retry = FALSE;
  255.       return sws->buffer_map(sws, sbuf->hwbuf, flags);
  256.    }
  257. }
  258.  
  259. /**
  260.  * Unmap the hardware storage of a buffer.
  261.  */
  262. static INLINE void
  263. svga_buffer_hw_storage_unmap(struct svga_context *svga,
  264.                              struct svga_buffer *sbuf)
  265. {
  266.    struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
  267.  
  268.    if (sws->have_gb_objects) {
  269.       struct svga_winsys_context *swc = svga->swc;
  270.       boolean rebind;
  271.       swc->surface_unmap(swc, sbuf->handle, &rebind);
  272.       if (rebind) {
  273.          enum pipe_error ret;
  274.          ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
  275.          if (ret != PIPE_OK) {
  276.             /* flush and retry */
  277.             svga_context_flush(svga, NULL);
  278.             ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
  279.             assert(ret == PIPE_OK);
  280.          }
  281.       }
  282.    } else
  283.       sws->buffer_unmap(sws, sbuf->hwbuf);
  284. }
  285.  
  286.  
  287. struct pipe_resource *
  288. svga_user_buffer_create(struct pipe_screen *screen,
  289.                         void *ptr,
  290.                         unsigned bytes,
  291.                         unsigned usage);
  292.  
  293. struct pipe_resource *
  294. svga_buffer_create(struct pipe_screen *screen,
  295.                    const struct pipe_resource *template);
  296.  
  297.  
  298.  
  299. /**
  300.  * Get the host surface handle for this buffer.
  301.  *
  302.  * This will ensure the host surface is updated, issuing DMAs as needed.
  303.  *
  304.  * NOTE: This may insert new commands in the context, so it *must* be called
  305.  * before reserving command buffer space. And, in order to insert commands
  306.  * it may need to call svga_context_flush().
  307.  */
  308. struct svga_winsys_surface *
  309. svga_buffer_handle(struct svga_context *svga,
  310.                    struct pipe_resource *buf);
  311.  
  312. void
  313. svga_context_flush_buffers(struct svga_context *svga);
  314.  
  315. struct svga_winsys_buffer *
  316. svga_winsys_buffer_create(struct svga_context *svga,
  317.                           unsigned alignment,
  318.                           unsigned usage,
  319.                           unsigned size);
  320.  
  321. #endif /* SVGA_BUFFER_H */
  322.