1/*
2 * Copyright 2003 VMware, Inc.
3 * Copyright © 2007 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25/**
26 * @file intel_upload.c
27 *
28 * Batched upload via BOs.
29 */
30
31#include "main/imports.h"
32#include "main/macros.h"
33#include "brw_bufmgr.h"
34#include "brw_context.h"
35#include "intel_buffer_objects.h"
36
37void
38brw_upload_finish(struct brw_uploader *upload)
39{
40   assert((upload->bo == NULL) == (upload->map == NULL));
41   if (!upload->bo)
42      return;
43
44   brw_bo_unmap(upload->bo);
45   brw_bo_unreference(upload->bo);
46   upload->bo = NULL;
47   upload->map = NULL;
48   upload->next_offset = 0;
49}
50
51/**
52 * Interface for getting memory for uploading streamed data to the GPU
53 *
54 * In most cases, streamed data (for GPU state structures, for example) is
55 * uploaded through brw_state_batch(), since that interface allows relocations
56 * from the streamed space returned to other BOs.  However, that interface has
57 * the restriction that the amount of space allocated has to be "small".
58 *
59 * This interface, on the other hand, is able to handle arbitrary sized
60 * allocation requests, though it will batch small allocations into the same
61 * BO for efficiency and reduced memory footprint.
62 *
63 * \note The returned pointer is valid only until brw_upload_finish().
64 *
65 * \param out_bo Pointer to a BO, which must point to a valid BO or NULL on
66 * entry, and will have a reference to the new BO containing the state on
67 * return.
68 *
69 * \param out_offset Offset within the buffer object that the data will land.
70 */
71void *
72brw_upload_space(struct brw_uploader *upload,
73                 uint32_t size,
74                 uint32_t alignment,
75                 struct brw_bo **out_bo,
76                 uint32_t *out_offset)
77{
78   uint32_t offset;
79
80   offset = ALIGN_NPOT(upload->next_offset, alignment);
81   if (upload->bo && offset + size > upload->bo->size) {
82      brw_upload_finish(upload);
83      offset = 0;
84   }
85
86   assert((upload->bo == NULL) == (upload->map == NULL));
87   if (!upload->bo) {
88      upload->bo = brw_bo_alloc(upload->bufmgr, "streamed data",
89                                MAX2(upload->default_size, size),
90                                BRW_MEMZONE_OTHER);
91      upload->map = brw_bo_map(NULL, upload->bo,
92                               MAP_READ | MAP_WRITE |
93                               MAP_PERSISTENT | MAP_ASYNC);
94   }
95
96   upload->next_offset = offset + size;
97
98   *out_offset = offset;
99   if (*out_bo != upload->bo) {
100      brw_bo_unreference(*out_bo);
101      *out_bo = upload->bo;
102      brw_bo_reference(upload->bo);
103   }
104
105   return upload->map + offset;
106}
107
108/**
109 * Handy interface to upload some data to temporary GPU memory quickly.
110 *
111 * References to this memory should not be retained across batch flushes.
112 */
113void
114brw_upload_data(struct brw_uploader *upload,
115                const void *data,
116                uint32_t size,
117                uint32_t alignment,
118                struct brw_bo **out_bo,
119                uint32_t *out_offset)
120{
121   void *dst = brw_upload_space(upload, size, alignment, out_bo, out_offset);
122   memcpy(dst, data, size);
123}
124
125void
126brw_upload_init(struct brw_uploader *upload,
127                struct brw_bufmgr *bufmgr,
128                unsigned default_size)
129{
130   upload->bufmgr = bufmgr;
131   upload->bo = NULL;
132   upload->map = NULL;
133   upload->next_offset = 0;
134   upload->default_size = default_size;
135}
136