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/macros.h"
32 #include "brw_bufmgr.h"
33 #include "brw_context.h"
34 #include "brw_buffer_objects.h"
35
36 void
brw_upload_finish(struct brw_uploader * upload)37 brw_upload_finish(struct brw_uploader *upload)
38 {
39 assert((upload->bo == NULL) == (upload->map == NULL));
40 if (!upload->bo)
41 return;
42
43 brw_bo_unmap(upload->bo);
44 brw_bo_unreference(upload->bo);
45 upload->bo = NULL;
46 upload->map = NULL;
47 upload->next_offset = 0;
48 }
49
50 /**
51 * Interface for getting memory for uploading streamed data to the GPU
52 *
53 * In most cases, streamed data (for GPU state structures, for example) is
54 * uploaded through brw_state_batch(), since that interface allows relocations
55 * from the streamed space returned to other BOs. However, that interface has
56 * the restriction that the amount of space allocated has to be "small".
57 *
58 * This interface, on the other hand, is able to handle arbitrary sized
59 * allocation requests, though it will batch small allocations into the same
60 * BO for efficiency and reduced memory footprint.
61 *
62 * \note The returned pointer is valid only until brw_upload_finish().
63 *
64 * \param out_bo Pointer to a BO, which must point to a valid BO or NULL on
65 * entry, and will have a reference to the new BO containing the state on
66 * return.
67 *
68 * \param out_offset Offset within the buffer object that the data will land.
69 */
70 void *
brw_upload_space(struct brw_uploader * upload,uint32_t size,uint32_t alignment,struct brw_bo ** out_bo,uint32_t * out_offset)71 brw_upload_space(struct brw_uploader *upload,
72 uint32_t size,
73 uint32_t alignment,
74 struct brw_bo **out_bo,
75 uint32_t *out_offset)
76 {
77 uint32_t offset;
78
79 offset = ALIGN_NPOT(upload->next_offset, alignment);
80 if (upload->bo && offset + size > upload->bo->size) {
81 brw_upload_finish(upload);
82 offset = 0;
83 }
84
85 assert((upload->bo == NULL) == (upload->map == NULL));
86 if (!upload->bo) {
87 upload->bo = brw_bo_alloc(upload->bufmgr, "streamed data",
88 MAX2(upload->default_size, size),
89 BRW_MEMZONE_OTHER);
90 upload->map = brw_bo_map(NULL, upload->bo,
91 MAP_READ | MAP_WRITE |
92 MAP_PERSISTENT | MAP_ASYNC);
93 }
94
95 upload->next_offset = offset + size;
96
97 *out_offset = offset;
98 if (*out_bo != upload->bo) {
99 brw_bo_unreference(*out_bo);
100 *out_bo = upload->bo;
101 brw_bo_reference(upload->bo);
102 }
103
104 return upload->map + offset;
105 }
106
107 /**
108 * Handy interface to upload some data to temporary GPU memory quickly.
109 *
110 * References to this memory should not be retained across batch flushes.
111 */
112 void
brw_upload_data(struct brw_uploader * upload,const void * data,uint32_t size,uint32_t alignment,struct brw_bo ** out_bo,uint32_t * out_offset)113 brw_upload_data(struct brw_uploader *upload,
114 const void *data,
115 uint32_t size,
116 uint32_t alignment,
117 struct brw_bo **out_bo,
118 uint32_t *out_offset)
119 {
120 void *dst = brw_upload_space(upload, size, alignment, out_bo, out_offset);
121 memcpy(dst, data, size);
122 }
123
124 void
brw_upload_init(struct brw_uploader * upload,struct brw_bufmgr * bufmgr,unsigned default_size)125 brw_upload_init(struct brw_uploader *upload,
126 struct brw_bufmgr *bufmgr,
127 unsigned default_size)
128 {
129 upload->bufmgr = bufmgr;
130 upload->bo = NULL;
131 upload->map = NULL;
132 upload->next_offset = 0;
133 upload->default_size = default_size;
134 }
135