2 * Copyright 2003 VMware, Inc.
3 * Copyright © 2007 Intel Corporation
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:
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
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
26 * @file intel_upload.c
28 * Batched upload via BOs.
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"
38 brw_upload_finish(struct brw_uploader
*upload
)
40 assert((upload
->bo
== NULL
) == (upload
->map
== NULL
));
44 brw_bo_unmap(upload
->bo
);
45 brw_bo_unreference(upload
->bo
);
48 upload
->next_offset
= 0;
52 * Interface for getting memory for uploading streamed data to the GPU
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".
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.
63 * \note The returned pointer is valid only until brw_upload_finish().
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
69 * \param out_offset Offset within the buffer object that the data will land.
72 brw_upload_space(struct brw_uploader
*upload
,
75 struct brw_bo
**out_bo
,
80 offset
= ALIGN_NPOT(upload
->next_offset
, alignment
);
81 if (upload
->bo
&& offset
+ size
> upload
->bo
->size
) {
82 brw_upload_finish(upload
);
86 assert((upload
->bo
== NULL
) == (upload
->map
== NULL
));
88 upload
->bo
= brw_bo_alloc(upload
->bufmgr
, "streamed data",
89 MAX2(upload
->default_size
, size
), 4096);
90 upload
->map
= brw_bo_map(NULL
, upload
->bo
,
91 MAP_READ
| MAP_WRITE
|
92 MAP_PERSISTENT
| MAP_ASYNC
);
95 upload
->next_offset
= offset
+ size
;
98 if (*out_bo
!= upload
->bo
) {
99 brw_bo_unreference(*out_bo
);
100 *out_bo
= upload
->bo
;
101 brw_bo_reference(upload
->bo
);
104 return upload
->map
+ offset
;
108 * Handy interface to upload some data to temporary GPU memory quickly.
110 * References to this memory should not be retained across batch flushes.
113 brw_upload_data(struct brw_uploader
*upload
,
117 struct brw_bo
**out_bo
,
118 uint32_t *out_offset
)
120 void *dst
= brw_upload_space(upload
, size
, alignment
, out_bo
, out_offset
);
121 memcpy(dst
, data
, size
);
125 brw_upload_init(struct brw_uploader
*upload
,
126 struct brw_bufmgr
*bufmgr
,
127 unsigned default_size
)
129 upload
->bufmgr
= bufmgr
;
132 upload
->next_offset
= 0;
133 upload
->default_size
= default_size
;