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