freedreno: add transform-feedback state
[mesa.git] / src / gallium / drivers / freedreno / freedreno_state.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_helpers.h"
33
34 #include "freedreno_state.h"
35 #include "freedreno_context.h"
36 #include "freedreno_resource.h"
37 #include "freedreno_texture.h"
38 #include "freedreno_gmem.h"
39 #include "freedreno_util.h"
40
41 /* All the generic state handling.. In case of CSO's that are specific
42 * to the GPU version, when the bind and the delete are common they can
43 * go in here.
44 */
45
46 static void
47 fd_set_blend_color(struct pipe_context *pctx,
48 const struct pipe_blend_color *blend_color)
49 {
50 struct fd_context *ctx = fd_context(pctx);
51 ctx->blend_color = *blend_color;
52 ctx->dirty |= FD_DIRTY_BLEND_COLOR;
53 }
54
55 static void
56 fd_set_stencil_ref(struct pipe_context *pctx,
57 const struct pipe_stencil_ref *stencil_ref)
58 {
59 struct fd_context *ctx = fd_context(pctx);
60 ctx->stencil_ref =* stencil_ref;
61 ctx->dirty |= FD_DIRTY_STENCIL_REF;
62 }
63
64 static void
65 fd_set_clip_state(struct pipe_context *pctx,
66 const struct pipe_clip_state *clip)
67 {
68 DBG("TODO: ");
69 }
70
71 static void
72 fd_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
73 {
74 struct fd_context *ctx = fd_context(pctx);
75 ctx->sample_mask = (uint16_t)sample_mask;
76 ctx->dirty |= FD_DIRTY_SAMPLE_MASK;
77 }
78
79 /* notes from calim on #dri-devel:
80 * index==0 will be non-UBO (ie. glUniformXYZ()) all packed together padded
81 * out to vec4's
82 * I should be able to consider that I own the user_ptr until the next
83 * set_constant_buffer() call, at which point I don't really care about the
84 * previous values.
85 * index>0 will be UBO's.. well, I'll worry about that later
86 */
87 static void
88 fd_set_constant_buffer(struct pipe_context *pctx, uint shader, uint index,
89 struct pipe_constant_buffer *cb)
90 {
91 struct fd_context *ctx = fd_context(pctx);
92 struct fd_constbuf_stateobj *so = &ctx->constbuf[shader];
93
94 /* Note that the state tracker can unbind constant buffers by
95 * passing NULL here.
96 */
97 if (unlikely(!cb)) {
98 so->enabled_mask &= ~(1 << index);
99 so->dirty_mask &= ~(1 << index);
100 pipe_resource_reference(&so->cb[index].buffer, NULL);
101 return;
102 }
103
104 pipe_resource_reference(&so->cb[index].buffer, cb->buffer);
105 so->cb[index].buffer_offset = cb->buffer_offset;
106 so->cb[index].buffer_size = cb->buffer_size;
107 so->cb[index].user_buffer = cb->user_buffer;
108
109 so->enabled_mask |= 1 << index;
110 so->dirty_mask |= 1 << index;
111 ctx->dirty |= FD_DIRTY_CONSTBUF;
112 }
113
114 static void
115 fd_set_framebuffer_state(struct pipe_context *pctx,
116 const struct pipe_framebuffer_state *framebuffer)
117 {
118 struct fd_context *ctx = fd_context(pctx);
119 struct pipe_framebuffer_state *cso = &ctx->framebuffer;
120
121 DBG("%d: cbufs[0]=%p, zsbuf=%p", ctx->needs_flush,
122 framebuffer->cbufs[0], framebuffer->zsbuf);
123
124 fd_context_render(pctx);
125
126 if ((cso->width != framebuffer->width) ||
127 (cso->height != framebuffer->height))
128 ctx->needs_rb_fbd = true;
129
130 util_copy_framebuffer_state(cso, framebuffer);
131
132 ctx->dirty |= FD_DIRTY_FRAMEBUFFER;
133
134 ctx->disabled_scissor.minx = 0;
135 ctx->disabled_scissor.miny = 0;
136 ctx->disabled_scissor.maxx = cso->width;
137 ctx->disabled_scissor.maxy = cso->height;
138
139 ctx->dirty |= FD_DIRTY_SCISSOR;
140 }
141
142 static void
143 fd_set_polygon_stipple(struct pipe_context *pctx,
144 const struct pipe_poly_stipple *stipple)
145 {
146 struct fd_context *ctx = fd_context(pctx);
147 ctx->stipple = *stipple;
148 ctx->dirty |= FD_DIRTY_STIPPLE;
149 }
150
151 static void
152 fd_set_scissor_states(struct pipe_context *pctx,
153 unsigned start_slot,
154 unsigned num_scissors,
155 const struct pipe_scissor_state *scissor)
156 {
157 struct fd_context *ctx = fd_context(pctx);
158
159 ctx->scissor = *scissor;
160 ctx->dirty |= FD_DIRTY_SCISSOR;
161 }
162
163 static void
164 fd_set_viewport_states(struct pipe_context *pctx,
165 unsigned start_slot,
166 unsigned num_viewports,
167 const struct pipe_viewport_state *viewport)
168 {
169 struct fd_context *ctx = fd_context(pctx);
170 ctx->viewport = *viewport;
171 ctx->dirty |= FD_DIRTY_VIEWPORT;
172 }
173
174 static void
175 fd_set_vertex_buffers(struct pipe_context *pctx,
176 unsigned start_slot, unsigned count,
177 const struct pipe_vertex_buffer *vb)
178 {
179 struct fd_context *ctx = fd_context(pctx);
180 struct fd_vertexbuf_stateobj *so = &ctx->vtx.vertexbuf;
181 int i;
182
183 /* on a2xx, pitch is encoded in the vtx fetch instruction, so
184 * we need to mark VTXSTATE as dirty as well to trigger patching
185 * and re-emitting the vtx shader:
186 */
187 for (i = 0; i < count; i++) {
188 bool new_enabled = vb && (vb[i].buffer || vb[i].user_buffer);
189 bool old_enabled = so->vb[i].buffer || so->vb[i].user_buffer;
190 uint32_t new_stride = vb ? vb[i].stride : 0;
191 uint32_t old_stride = so->vb[i].stride;
192 if ((new_enabled != old_enabled) || (new_stride != old_stride)) {
193 ctx->dirty |= FD_DIRTY_VTXSTATE;
194 break;
195 }
196 }
197
198 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, count);
199 so->count = util_last_bit(so->enabled_mask);
200
201 ctx->dirty |= FD_DIRTY_VTXBUF;
202 }
203
204 static void
205 fd_set_index_buffer(struct pipe_context *pctx,
206 const struct pipe_index_buffer *ib)
207 {
208 struct fd_context *ctx = fd_context(pctx);
209
210 if (ib) {
211 pipe_resource_reference(&ctx->indexbuf.buffer, ib->buffer);
212 ctx->indexbuf.index_size = ib->index_size;
213 ctx->indexbuf.offset = ib->offset;
214 ctx->indexbuf.user_buffer = ib->user_buffer;
215 } else {
216 pipe_resource_reference(&ctx->indexbuf.buffer, NULL);
217 }
218
219 ctx->dirty |= FD_DIRTY_INDEXBUF;
220 }
221
222 static void
223 fd_blend_state_bind(struct pipe_context *pctx, void *hwcso)
224 {
225 struct fd_context *ctx = fd_context(pctx);
226 ctx->blend = hwcso;
227 ctx->dirty |= FD_DIRTY_BLEND;
228 }
229
230 static void
231 fd_blend_state_delete(struct pipe_context *pctx, void *hwcso)
232 {
233 FREE(hwcso);
234 }
235
236 static void
237 fd_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
238 {
239 struct fd_context *ctx = fd_context(pctx);
240 struct pipe_scissor_state *old_scissor = fd_context_get_scissor(ctx);
241
242 ctx->rasterizer = hwcso;
243 ctx->dirty |= FD_DIRTY_RASTERIZER;
244
245 /* if scissor enable bit changed we need to mark scissor
246 * state as dirty as well:
247 * NOTE: we can do a shallow compare, since we only care
248 * if it changed to/from &ctx->disable_scissor
249 */
250 if (old_scissor != fd_context_get_scissor(ctx))
251 ctx->dirty |= FD_DIRTY_SCISSOR;
252 }
253
254 static void
255 fd_rasterizer_state_delete(struct pipe_context *pctx, void *hwcso)
256 {
257 FREE(hwcso);
258 }
259
260 static void
261 fd_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
262 {
263 struct fd_context *ctx = fd_context(pctx);
264 ctx->zsa = hwcso;
265 ctx->dirty |= FD_DIRTY_ZSA;
266 }
267
268 static void
269 fd_zsa_state_delete(struct pipe_context *pctx, void *hwcso)
270 {
271 FREE(hwcso);
272 }
273
274 static void *
275 fd_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
276 const struct pipe_vertex_element *elements)
277 {
278 struct fd_vertex_stateobj *so = CALLOC_STRUCT(fd_vertex_stateobj);
279
280 if (!so)
281 return NULL;
282
283 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
284 so->num_elements = num_elements;
285
286 return so;
287 }
288
289 static void
290 fd_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
291 {
292 FREE(hwcso);
293 }
294
295 static void
296 fd_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
297 {
298 struct fd_context *ctx = fd_context(pctx);
299 ctx->vtx.vtx = hwcso;
300 ctx->dirty |= FD_DIRTY_VTXSTATE;
301 }
302
303 static struct pipe_stream_output_target *
304 fd_create_stream_output_target(struct pipe_context *pctx,
305 struct pipe_resource *prsc, unsigned buffer_offset,
306 unsigned buffer_size)
307 {
308 struct pipe_stream_output_target *target;
309
310 target = CALLOC_STRUCT(pipe_stream_output_target);
311 if (!target)
312 return NULL;
313
314 pipe_reference_init(&target->reference, 1);
315 pipe_resource_reference(&target->buffer, prsc);
316
317 target->context = pctx;
318 target->buffer_offset = buffer_offset;
319 target->buffer_size = buffer_size;
320
321 return target;
322 }
323
324 static void
325 fd_stream_output_target_destroy(struct pipe_context *pctx,
326 struct pipe_stream_output_target *target)
327 {
328 pipe_resource_reference(&target->buffer, NULL);
329 FREE(target);
330 }
331
332 static void
333 fd_set_stream_output_targets(struct pipe_context *pctx,
334 unsigned num_targets, struct pipe_stream_output_target **targets,
335 const unsigned *offsets)
336 {
337 struct fd_context *ctx = fd_context(pctx);
338 struct fd_streamout_stateobj *so = &ctx->streamout;
339 unsigned i;
340
341 debug_assert(num_targets <= ARRAY_SIZE(so->targets));
342
343 for (i = 0; i < num_targets; i++) {
344 boolean changed = targets[i] != so->targets[i];
345 boolean append = (offsets[i] == (unsigned)-1);
346
347 if (!changed && append)
348 continue;
349
350 so->offsets[i] = 0;
351
352 pipe_so_target_reference(&so->targets[i], targets[i]);
353 }
354
355 for (; i < so->num_targets; i++) {
356 pipe_so_target_reference(&so->targets[i], NULL);
357 }
358
359 so->num_targets = num_targets;
360
361 ctx->dirty |= FD_DIRTY_STREAMOUT;
362 }
363
364 void
365 fd_state_init(struct pipe_context *pctx)
366 {
367 pctx->set_blend_color = fd_set_blend_color;
368 pctx->set_stencil_ref = fd_set_stencil_ref;
369 pctx->set_clip_state = fd_set_clip_state;
370 pctx->set_sample_mask = fd_set_sample_mask;
371 pctx->set_constant_buffer = fd_set_constant_buffer;
372 pctx->set_framebuffer_state = fd_set_framebuffer_state;
373 pctx->set_polygon_stipple = fd_set_polygon_stipple;
374 pctx->set_scissor_states = fd_set_scissor_states;
375 pctx->set_viewport_states = fd_set_viewport_states;
376
377 pctx->set_vertex_buffers = fd_set_vertex_buffers;
378 pctx->set_index_buffer = fd_set_index_buffer;
379
380 pctx->bind_blend_state = fd_blend_state_bind;
381 pctx->delete_blend_state = fd_blend_state_delete;
382
383 pctx->bind_rasterizer_state = fd_rasterizer_state_bind;
384 pctx->delete_rasterizer_state = fd_rasterizer_state_delete;
385
386 pctx->bind_depth_stencil_alpha_state = fd_zsa_state_bind;
387 pctx->delete_depth_stencil_alpha_state = fd_zsa_state_delete;
388
389 pctx->create_vertex_elements_state = fd_vertex_state_create;
390 pctx->delete_vertex_elements_state = fd_vertex_state_delete;
391 pctx->bind_vertex_elements_state = fd_vertex_state_bind;
392
393 pctx->create_stream_output_target = fd_create_stream_output_target;
394 pctx->stream_output_target_destroy = fd_stream_output_target_destroy;
395 pctx->set_stream_output_targets = fd_set_stream_output_targets;
396 }