r600g: always derive alphatest state from the first colorbuffer
[mesa.git] / src / gallium / drivers / r600 / r600_buffer.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
24 * Jerome Glisse
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
26 */
27 #include "r600_pipe.h"
28 #include "util/u_upload_mgr.h"
29 #include "util/u_memory.h"
30
31 static void r600_buffer_destroy(struct pipe_screen *screen,
32 struct pipe_resource *buf)
33 {
34 struct r600_resource *rbuffer = r600_resource(buf);
35
36 pb_reference(&rbuffer->buf, NULL);
37 FREE(rbuffer);
38 }
39
40 static struct pipe_transfer *r600_get_transfer(struct pipe_context *ctx,
41 struct pipe_resource *resource,
42 unsigned level,
43 unsigned usage,
44 const struct pipe_box *box)
45 {
46 struct r600_context *rctx = (struct r600_context*)ctx;
47 struct pipe_transfer *transfer = util_slab_alloc(&rctx->pool_transfers);
48
49 transfer->resource = resource;
50 transfer->level = level;
51 transfer->usage = usage;
52 transfer->box = *box;
53 transfer->stride = 0;
54 transfer->layer_stride = 0;
55 transfer->data = NULL;
56
57 /* Note strides are zero, this is ok for buffers, but not for
58 * textures 2d & higher at least.
59 */
60 return transfer;
61 }
62
63 static void r600_set_constants_dirty_if_bound(struct r600_context *rctx,
64 struct r600_constbuf_state *state,
65 struct r600_resource *rbuffer)
66 {
67 bool found = false;
68 uint32_t mask = state->enabled_mask;
69
70 while (mask) {
71 unsigned i = u_bit_scan(&mask);
72 if (state->cb[i].buffer == &rbuffer->b.b) {
73 found = true;
74 state->dirty_mask |= 1 << i;
75 }
76 }
77 if (found) {
78 r600_constant_buffers_dirty(rctx, state);
79 }
80 }
81
82 static void *r600_buffer_transfer_map(struct pipe_context *pipe,
83 struct pipe_transfer *transfer)
84 {
85 struct r600_resource *rbuffer = r600_resource(transfer->resource);
86 struct r600_context *rctx = (struct r600_context*)pipe;
87 uint8_t *data;
88
89 if (transfer->usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE &&
90 !(transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
91 assert(transfer->usage & PIPE_TRANSFER_WRITE);
92
93 /* Check if mapping this buffer would cause waiting for the GPU. */
94 if (rctx->ws->cs_is_buffer_referenced(rctx->cs, rbuffer->cs_buf, RADEON_USAGE_READWRITE) ||
95 rctx->ws->buffer_is_busy(rbuffer->buf, RADEON_USAGE_READWRITE)) {
96 unsigned i, mask;
97
98 /* Discard the buffer. */
99 pb_reference(&rbuffer->buf, NULL);
100
101 /* Create a new one in the same pipe_resource. */
102 /* XXX We probably want a different alignment for buffers and textures. */
103 r600_init_resource(rctx->screen, rbuffer, rbuffer->b.b.width0, 4096,
104 rbuffer->b.b.bind, rbuffer->b.b.usage);
105
106 /* We changed the buffer, now we need to bind it where the old one was bound. */
107 /* Vertex buffers. */
108 mask = rctx->vertex_buffer_state.enabled_mask;
109 while (mask) {
110 i = u_bit_scan(&mask);
111 if (rctx->vertex_buffer_state.vb[i].buffer == &rbuffer->b.b) {
112 rctx->vertex_buffer_state.dirty_mask |= 1 << i;
113 r600_vertex_buffers_dirty(rctx);
114 }
115 }
116 /* Streamout buffers. */
117 for (i = 0; i < rctx->num_so_targets; i++) {
118 if (rctx->so_targets[i]->b.buffer == &rbuffer->b.b) {
119 r600_context_streamout_end(rctx);
120 rctx->streamout_start = TRUE;
121 rctx->streamout_append_bitmask = ~0;
122 }
123 }
124 /* Constant buffers. */
125 r600_set_constants_dirty_if_bound(rctx, &rctx->vs_constbuf_state, rbuffer);
126 r600_set_constants_dirty_if_bound(rctx, &rctx->ps_constbuf_state, rbuffer);
127 }
128 }
129
130 data = rctx->ws->buffer_map(rbuffer->cs_buf, rctx->cs, transfer->usage);
131 if (!data)
132 return NULL;
133
134 return (uint8_t*)data + transfer->box.x;
135 }
136
137 static void r600_buffer_transfer_unmap(struct pipe_context *pipe,
138 struct pipe_transfer *transfer)
139 {
140 /* no-op */
141 }
142
143 static void r600_transfer_destroy(struct pipe_context *ctx,
144 struct pipe_transfer *transfer)
145 {
146 struct r600_context *rctx = (struct r600_context*)ctx;
147 util_slab_free(&rctx->pool_transfers, transfer);
148 }
149
150 static const struct u_resource_vtbl r600_buffer_vtbl =
151 {
152 u_default_resource_get_handle, /* get_handle */
153 r600_buffer_destroy, /* resource_destroy */
154 r600_get_transfer, /* get_transfer */
155 r600_transfer_destroy, /* transfer_destroy */
156 r600_buffer_transfer_map, /* transfer_map */
157 NULL, /* transfer_flush_region */
158 r600_buffer_transfer_unmap, /* transfer_unmap */
159 NULL /* transfer_inline_write */
160 };
161
162 bool r600_init_resource(struct r600_screen *rscreen,
163 struct r600_resource *res,
164 unsigned size, unsigned alignment,
165 unsigned bind, unsigned usage)
166 {
167 uint32_t initial_domain, domains;
168
169 /* Staging resources particpate in transfers and blits only
170 * and are used for uploads and downloads from regular
171 * resources. We generate them internally for some transfers.
172 */
173 if (usage == PIPE_USAGE_STAGING) {
174 domains = RADEON_DOMAIN_GTT;
175 initial_domain = RADEON_DOMAIN_GTT;
176 } else {
177 domains = RADEON_DOMAIN_GTT | RADEON_DOMAIN_VRAM;
178
179 switch(usage) {
180 case PIPE_USAGE_DYNAMIC:
181 case PIPE_USAGE_STREAM:
182 case PIPE_USAGE_STAGING:
183 initial_domain = RADEON_DOMAIN_GTT;
184 break;
185 case PIPE_USAGE_DEFAULT:
186 case PIPE_USAGE_STATIC:
187 case PIPE_USAGE_IMMUTABLE:
188 default:
189 initial_domain = RADEON_DOMAIN_VRAM;
190 break;
191 }
192 }
193
194 res->buf = rscreen->ws->buffer_create(rscreen->ws, size, alignment, bind, initial_domain);
195 if (!res->buf) {
196 return false;
197 }
198
199 res->cs_buf = rscreen->ws->buffer_get_cs_handle(res->buf);
200 res->domains = domains;
201 return true;
202 }
203
204 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
205 const struct pipe_resource *templ)
206 {
207 struct r600_screen *rscreen = (struct r600_screen*)screen;
208 struct r600_resource *rbuffer;
209 /* XXX We probably want a different alignment for buffers and textures. */
210 unsigned alignment = 4096;
211
212 rbuffer = MALLOC_STRUCT(r600_resource);
213
214 rbuffer->b.b = *templ;
215 pipe_reference_init(&rbuffer->b.b.reference, 1);
216 rbuffer->b.b.screen = screen;
217 rbuffer->b.vtbl = &r600_buffer_vtbl;
218
219 if (!r600_init_resource(rscreen, rbuffer, templ->width0, alignment, templ->bind, templ->usage)) {
220 FREE(rbuffer);
221 return NULL;
222 }
223 return &rbuffer->b.b;
224 }