1 /*
2 * Copyright 2010 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 */
25
26 #include <stdio.h>
27
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include "util/u_math.h"
32
33 #include "r300_screen_buffer.h"
34
r300_upload_index_buffer(struct r300_context * r300,struct pipe_resource ** index_buffer,unsigned index_size,unsigned * start,unsigned count,const uint8_t * ptr)35 void r300_upload_index_buffer(struct r300_context *r300,
36 struct pipe_resource **index_buffer,
37 unsigned index_size, unsigned *start,
38 unsigned count, const uint8_t *ptr)
39 {
40 unsigned index_offset;
41
42 *index_buffer = NULL;
43
44 u_upload_data(r300->uploader,
45 0, count * index_size, 4,
46 ptr + (*start * index_size),
47 &index_offset,
48 index_buffer);
49
50 *start = index_offset / index_size;
51 }
52
r300_resource_destroy(struct pipe_screen * screen,struct pipe_resource * buf)53 void r300_resource_destroy(struct pipe_screen *screen,
54 struct pipe_resource *buf)
55 {
56 struct r300_screen *rscreen = r300_screen(screen);
57
58 if (buf->target == PIPE_BUFFER) {
59 struct r300_resource *rbuf = r300_resource(buf);
60
61 align_free(rbuf->malloced_buffer);
62
63 if (rbuf->buf)
64 radeon_bo_reference(rscreen->rws, &rbuf->buf, NULL);
65
66 FREE(rbuf);
67 } else {
68 struct r300_resource* tex = (struct r300_resource*)buf;
69
70 if (tex->tex.cmask_dwords) {
71 mtx_lock(&rscreen->cmask_mutex);
72 if (buf == rscreen->cmask_resource) {
73 rscreen->cmask_resource = NULL;
74 }
75 mtx_unlock(&rscreen->cmask_mutex);
76 }
77 radeon_bo_reference(rscreen->rws, &tex->buf, NULL);
78 FREE(tex);
79 }
80 }
81
82 void *
r300_buffer_transfer_map(struct pipe_context * context,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)83 r300_buffer_transfer_map( struct pipe_context *context,
84 struct pipe_resource *resource,
85 unsigned level,
86 unsigned usage,
87 const struct pipe_box *box,
88 struct pipe_transfer **ptransfer )
89 {
90 struct r300_context *r300 = r300_context(context);
91 struct radeon_winsys *rws = r300->screen->rws;
92 struct r300_resource *rbuf = r300_resource(resource);
93 struct pipe_transfer *transfer;
94 uint8_t *map;
95
96 transfer = slab_alloc(&r300->pool_transfers);
97 transfer->resource = resource;
98 transfer->level = level;
99 transfer->usage = usage;
100 transfer->box = *box;
101 transfer->stride = 0;
102 transfer->layer_stride = 0;
103
104 if (rbuf->malloced_buffer) {
105 *ptransfer = transfer;
106 return rbuf->malloced_buffer + box->x;
107 }
108
109 if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
110 !(usage & PIPE_MAP_UNSYNCHRONIZED)) {
111 assert(usage & PIPE_MAP_WRITE);
112
113 /* Check if mapping this buffer would cause waiting for the GPU. */
114 if (r300->rws->cs_is_buffer_referenced(&r300->cs, rbuf->buf, RADEON_USAGE_READWRITE) ||
115 !r300->rws->buffer_wait(r300->rws, rbuf->buf, 0, RADEON_USAGE_READWRITE)) {
116 unsigned i;
117 struct pb_buffer_lean *new_buf;
118
119 /* Create a new one in the same pipe_resource. */
120 new_buf = r300->rws->buffer_create(r300->rws, rbuf->b.width0,
121 R300_BUFFER_ALIGNMENT,
122 rbuf->domain,
123 RADEON_FLAG_NO_INTERPROCESS_SHARING);
124 if (new_buf) {
125 /* Discard the old buffer. */
126 radeon_bo_reference(r300->rws, &rbuf->buf, NULL);
127 rbuf->buf = new_buf;
128
129 /* We changed the buffer, now we need to bind it where the old one was bound. */
130 for (i = 0; i < r300->nr_vertex_buffers; i++) {
131 if (r300->vertex_buffer[i].buffer.resource == &rbuf->b) {
132 r300->vertex_arrays_dirty = true;
133 break;
134 }
135 }
136 }
137 }
138 }
139
140 /* Buffers are never used for write, therefore mapping for read can be
141 * unsynchronized. */
142 if (!(usage & PIPE_MAP_WRITE)) {
143 usage |= PIPE_MAP_UNSYNCHRONIZED;
144 }
145
146 map = rws->buffer_map(rws, rbuf->buf, &r300->cs, usage);
147
148 if (!map) {
149 slab_free(&r300->pool_transfers, transfer);
150 return NULL;
151 }
152
153 *ptransfer = transfer;
154 return map + box->x;
155 }
156
r300_buffer_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)157 void r300_buffer_transfer_unmap( struct pipe_context *pipe,
158 struct pipe_transfer *transfer )
159 {
160 struct r300_context *r300 = r300_context(pipe);
161
162 slab_free(&r300->pool_transfers, transfer);
163 }
164
r300_buffer_create(struct pipe_screen * screen,const struct pipe_resource * templ)165 struct pipe_resource *r300_buffer_create(struct pipe_screen *screen,
166 const struct pipe_resource *templ)
167 {
168 struct r300_screen *r300screen = r300_screen(screen);
169 struct r300_resource *rbuf;
170
171 rbuf = MALLOC_STRUCT(r300_resource);
172
173 rbuf->b = *templ;
174 pipe_reference_init(&rbuf->b.reference, 1);
175 rbuf->b.screen = screen;
176 rbuf->domain = RADEON_DOMAIN_GTT;
177 rbuf->buf = NULL;
178 rbuf->malloced_buffer = NULL;
179
180 /* Allocate constant buffers and SWTCL vertex and index buffers in RAM.
181 * Note that uploaded index buffers use the flag PIPE_BIND_CUSTOM, so that
182 * we can distinguish them from user-created buffers.
183 */
184 if (templ->bind & PIPE_BIND_CONSTANT_BUFFER ||
185 (!r300screen->caps.has_tcl && !(templ->bind & PIPE_BIND_CUSTOM))) {
186 rbuf->malloced_buffer = align_malloc(templ->width0, 64);
187 return &rbuf->b;
188 }
189
190 rbuf->buf =
191 r300screen->rws->buffer_create(r300screen->rws, rbuf->b.width0,
192 R300_BUFFER_ALIGNMENT,
193 rbuf->domain,
194 RADEON_FLAG_NO_INTERPROCESS_SHARING);
195 if (!rbuf->buf) {
196 FREE(rbuf);
197 return NULL;
198 }
199 return &rbuf->b;
200 }
201