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