freedreno/blitter: Save tessellation state
[mesa.git] / src / gallium / drivers / freedreno / freedreno_state.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "util/u_dual_blend.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_helpers.h"
32
33 #include "freedreno_state.h"
34 #include "freedreno_context.h"
35 #include "freedreno_resource.h"
36 #include "freedreno_texture.h"
37 #include "freedreno_gmem.h"
38 #include "freedreno_query_hw.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 struct fd_context *ctx = fd_context(pctx);
69 ctx->ucp = *clip;
70 ctx->dirty |= FD_DIRTY_UCP;
71 }
72
73 static void
74 fd_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
75 {
76 struct fd_context *ctx = fd_context(pctx);
77 ctx->sample_mask = (uint16_t)sample_mask;
78 ctx->dirty |= FD_DIRTY_SAMPLE_MASK;
79 }
80
81 static void
82 fd_set_min_samples(struct pipe_context *pctx, unsigned min_samples)
83 {
84 struct fd_context *ctx = fd_context(pctx);
85 ctx->min_samples = min_samples;
86 ctx->dirty |= FD_DIRTY_MIN_SAMPLES;
87 }
88
89 /* notes from calim on #dri-devel:
90 * index==0 will be non-UBO (ie. glUniformXYZ()) all packed together padded
91 * out to vec4's
92 * I should be able to consider that I own the user_ptr until the next
93 * set_constant_buffer() call, at which point I don't really care about the
94 * previous values.
95 * index>0 will be UBO's.. well, I'll worry about that later
96 */
97 static void
98 fd_set_constant_buffer(struct pipe_context *pctx,
99 enum pipe_shader_type shader, uint index,
100 const struct pipe_constant_buffer *cb)
101 {
102 struct fd_context *ctx = fd_context(pctx);
103 struct fd_constbuf_stateobj *so = &ctx->constbuf[shader];
104
105 util_copy_constant_buffer(&so->cb[index], cb);
106
107 /* Note that the state tracker can unbind constant buffers by
108 * passing NULL here.
109 */
110 if (unlikely(!cb)) {
111 so->enabled_mask &= ~(1 << index);
112 return;
113 }
114
115 so->enabled_mask |= 1 << index;
116 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_CONST;
117 ctx->dirty |= FD_DIRTY_CONST;
118 }
119
120 static void
121 fd_set_shader_buffers(struct pipe_context *pctx,
122 enum pipe_shader_type shader,
123 unsigned start, unsigned count,
124 const struct pipe_shader_buffer *buffers,
125 unsigned writable_bitmask)
126 {
127 struct fd_context *ctx = fd_context(pctx);
128 struct fd_shaderbuf_stateobj *so = &ctx->shaderbuf[shader];
129 unsigned mask = 0;
130
131 if (buffers) {
132 for (unsigned i = 0; i < count; i++) {
133 unsigned n = i + start;
134 struct pipe_shader_buffer *buf = &so->sb[n];
135
136 if ((buf->buffer == buffers[i].buffer) &&
137 (buf->buffer_offset == buffers[i].buffer_offset) &&
138 (buf->buffer_size == buffers[i].buffer_size))
139 continue;
140
141 mask |= BIT(n);
142
143 buf->buffer_offset = buffers[i].buffer_offset;
144 buf->buffer_size = buffers[i].buffer_size;
145 pipe_resource_reference(&buf->buffer, buffers[i].buffer);
146
147 if (buf->buffer)
148 so->enabled_mask |= BIT(n);
149 else
150 so->enabled_mask &= ~BIT(n);
151 }
152 } else {
153 mask = (BIT(count) - 1) << start;
154
155 for (unsigned i = 0; i < count; i++) {
156 unsigned n = i + start;
157 struct pipe_shader_buffer *buf = &so->sb[n];
158
159 pipe_resource_reference(&buf->buffer, NULL);
160 }
161
162 so->enabled_mask &= ~mask;
163 }
164
165 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_SSBO;
166 }
167
168 void
169 fd_set_shader_images(struct pipe_context *pctx,
170 enum pipe_shader_type shader,
171 unsigned start, unsigned count,
172 const struct pipe_image_view *images)
173 {
174 struct fd_context *ctx = fd_context(pctx);
175 struct fd_shaderimg_stateobj *so = &ctx->shaderimg[shader];
176
177 unsigned mask = 0;
178
179 if (images) {
180 for (unsigned i = 0; i < count; i++) {
181 unsigned n = i + start;
182 struct pipe_image_view *buf = &so->si[n];
183
184 if ((buf->resource == images[i].resource) &&
185 (buf->format == images[i].format) &&
186 (buf->access == images[i].access) &&
187 !memcmp(&buf->u, &images[i].u, sizeof(buf->u)))
188 continue;
189
190 mask |= BIT(n);
191 util_copy_image_view(buf, &images[i]);
192
193 if (buf->resource)
194 so->enabled_mask |= BIT(n);
195 else
196 so->enabled_mask &= ~BIT(n);
197 }
198 } else {
199 mask = (BIT(count) - 1) << start;
200
201 for (unsigned i = 0; i < count; i++) {
202 unsigned n = i + start;
203 struct pipe_image_view *img = &so->si[n];
204
205 pipe_resource_reference(&img->resource, NULL);
206 }
207
208 so->enabled_mask &= ~mask;
209 }
210
211 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_IMAGE;
212 }
213
214 static void
215 fd_set_framebuffer_state(struct pipe_context *pctx,
216 const struct pipe_framebuffer_state *framebuffer)
217 {
218 struct fd_context *ctx = fd_context(pctx);
219 struct pipe_framebuffer_state *cso;
220
221 DBG("%ux%u, %u layers, %u samples",
222 framebuffer->width, framebuffer->height,
223 framebuffer->layers, framebuffer->samples);
224
225 cso = &ctx->framebuffer;
226
227 if (util_framebuffer_state_equal(cso, framebuffer))
228 return;
229
230 util_copy_framebuffer_state(cso, framebuffer);
231
232 cso->samples = util_framebuffer_get_num_samples(cso);
233
234 if (ctx->screen->reorder) {
235 struct fd_batch *old_batch = NULL;
236
237 fd_batch_reference(&old_batch, ctx->batch);
238
239 if (likely(old_batch))
240 fd_batch_set_stage(old_batch, FD_STAGE_NULL);
241
242 fd_batch_reference(&ctx->batch, NULL);
243 fd_context_all_dirty(ctx);
244
245 if (old_batch && old_batch->blit && !old_batch->back_blit) {
246 /* for blits, there is not really much point in hanging on
247 * to the uncommitted batch (ie. you probably don't blit
248 * multiple times to the same surface), so we might as
249 * well go ahead and flush this one:
250 */
251 fd_batch_flush(old_batch, false);
252 }
253
254 fd_batch_reference(&old_batch, NULL);
255 } else {
256 DBG("%d: cbufs[0]=%p, zsbuf=%p", ctx->batch->needs_flush,
257 framebuffer->cbufs[0], framebuffer->zsbuf);
258 fd_batch_flush(ctx->batch, false);
259 util_copy_framebuffer_state(&ctx->batch->framebuffer, cso);
260 }
261
262 ctx->dirty |= FD_DIRTY_FRAMEBUFFER;
263
264 ctx->disabled_scissor.minx = 0;
265 ctx->disabled_scissor.miny = 0;
266 ctx->disabled_scissor.maxx = cso->width;
267 ctx->disabled_scissor.maxy = cso->height;
268
269 ctx->dirty |= FD_DIRTY_SCISSOR;
270 }
271
272 static void
273 fd_set_polygon_stipple(struct pipe_context *pctx,
274 const struct pipe_poly_stipple *stipple)
275 {
276 struct fd_context *ctx = fd_context(pctx);
277 ctx->stipple = *stipple;
278 ctx->dirty |= FD_DIRTY_STIPPLE;
279 }
280
281 static void
282 fd_set_scissor_states(struct pipe_context *pctx,
283 unsigned start_slot,
284 unsigned num_scissors,
285 const struct pipe_scissor_state *scissor)
286 {
287 struct fd_context *ctx = fd_context(pctx);
288
289 ctx->scissor = *scissor;
290 ctx->dirty |= FD_DIRTY_SCISSOR;
291 }
292
293 static void
294 fd_set_viewport_states(struct pipe_context *pctx,
295 unsigned start_slot,
296 unsigned num_viewports,
297 const struct pipe_viewport_state *viewport)
298 {
299 struct fd_context *ctx = fd_context(pctx);
300 struct pipe_scissor_state *scissor = &ctx->viewport_scissor;
301 float minx, miny, maxx, maxy;
302
303 ctx->viewport = *viewport;
304
305 /* see si_get_scissor_from_viewport(): */
306
307 /* Convert (-1, -1) and (1, 1) from clip space into window space. */
308 minx = -viewport->scale[0] + viewport->translate[0];
309 miny = -viewport->scale[1] + viewport->translate[1];
310 maxx = viewport->scale[0] + viewport->translate[0];
311 maxy = viewport->scale[1] + viewport->translate[1];
312
313 /* Handle inverted viewports. */
314 if (minx > maxx) {
315 swap(minx, maxx);
316 }
317 if (miny > maxy) {
318 swap(miny, maxy);
319 }
320
321 debug_assert(miny >= 0);
322 debug_assert(maxy >= 0);
323
324 /* Convert to integer and round up the max bounds. */
325 scissor->minx = minx;
326 scissor->miny = miny;
327 scissor->maxx = ceilf(maxx);
328 scissor->maxy = ceilf(maxy);
329
330 ctx->dirty |= FD_DIRTY_VIEWPORT;
331 }
332
333 static void
334 fd_set_vertex_buffers(struct pipe_context *pctx,
335 unsigned start_slot, unsigned count,
336 const struct pipe_vertex_buffer *vb)
337 {
338 struct fd_context *ctx = fd_context(pctx);
339 struct fd_vertexbuf_stateobj *so = &ctx->vtx.vertexbuf;
340 int i;
341
342 /* on a2xx, pitch is encoded in the vtx fetch instruction, so
343 * we need to mark VTXSTATE as dirty as well to trigger patching
344 * and re-emitting the vtx shader:
345 */
346 if (ctx->screen->gpu_id < 300) {
347 for (i = 0; i < count; i++) {
348 bool new_enabled = vb && vb[i].buffer.resource;
349 bool old_enabled = so->vb[i].buffer.resource != NULL;
350 uint32_t new_stride = vb ? vb[i].stride : 0;
351 uint32_t old_stride = so->vb[i].stride;
352 if ((new_enabled != old_enabled) || (new_stride != old_stride)) {
353 ctx->dirty |= FD_DIRTY_VTXSTATE;
354 break;
355 }
356 }
357 }
358
359 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, count);
360 so->count = util_last_bit(so->enabled_mask);
361
362 ctx->dirty |= FD_DIRTY_VTXBUF;
363 }
364
365 static void
366 fd_blend_state_bind(struct pipe_context *pctx, void *hwcso)
367 {
368 struct fd_context *ctx = fd_context(pctx);
369 struct pipe_blend_state *cso = hwcso;
370 bool old_is_dual = ctx->blend ?
371 ctx->blend->rt[0].blend_enable && util_blend_state_is_dual(ctx->blend, 0) :
372 false;
373 bool new_is_dual = cso ?
374 cso->rt[0].blend_enable && util_blend_state_is_dual(cso, 0) :
375 false;
376 ctx->blend = hwcso;
377 ctx->dirty |= FD_DIRTY_BLEND;
378 if (old_is_dual != new_is_dual)
379 ctx->dirty |= FD_DIRTY_BLEND_DUAL;
380 }
381
382 static void
383 fd_blend_state_delete(struct pipe_context *pctx, void *hwcso)
384 {
385 FREE(hwcso);
386 }
387
388 static void
389 fd_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
390 {
391 struct fd_context *ctx = fd_context(pctx);
392 struct pipe_scissor_state *old_scissor = fd_context_get_scissor(ctx);
393
394 ctx->rasterizer = hwcso;
395 ctx->dirty |= FD_DIRTY_RASTERIZER;
396
397 /* if scissor enable bit changed we need to mark scissor
398 * state as dirty as well:
399 * NOTE: we can do a shallow compare, since we only care
400 * if it changed to/from &ctx->disable_scissor
401 */
402 if (old_scissor != fd_context_get_scissor(ctx))
403 ctx->dirty |= FD_DIRTY_SCISSOR;
404 }
405
406 static void
407 fd_rasterizer_state_delete(struct pipe_context *pctx, void *hwcso)
408 {
409 FREE(hwcso);
410 }
411
412 static void
413 fd_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
414 {
415 struct fd_context *ctx = fd_context(pctx);
416 ctx->zsa = hwcso;
417 ctx->dirty |= FD_DIRTY_ZSA;
418 }
419
420 static void
421 fd_zsa_state_delete(struct pipe_context *pctx, void *hwcso)
422 {
423 FREE(hwcso);
424 }
425
426 static void *
427 fd_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
428 const struct pipe_vertex_element *elements)
429 {
430 struct fd_vertex_stateobj *so = CALLOC_STRUCT(fd_vertex_stateobj);
431
432 if (!so)
433 return NULL;
434
435 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
436 so->num_elements = num_elements;
437
438 return so;
439 }
440
441 static void
442 fd_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
443 {
444 FREE(hwcso);
445 }
446
447 static void
448 fd_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
449 {
450 struct fd_context *ctx = fd_context(pctx);
451 ctx->vtx.vtx = hwcso;
452 ctx->dirty |= FD_DIRTY_VTXSTATE;
453 }
454
455 static struct pipe_stream_output_target *
456 fd_create_stream_output_target(struct pipe_context *pctx,
457 struct pipe_resource *prsc, unsigned buffer_offset,
458 unsigned buffer_size)
459 {
460 struct pipe_stream_output_target *target;
461 struct fd_resource *rsc = fd_resource(prsc);
462
463 target = CALLOC_STRUCT(pipe_stream_output_target);
464 if (!target)
465 return NULL;
466
467 pipe_reference_init(&target->reference, 1);
468 pipe_resource_reference(&target->buffer, prsc);
469
470 target->context = pctx;
471 target->buffer_offset = buffer_offset;
472 target->buffer_size = buffer_size;
473
474 assert(rsc->base.target == PIPE_BUFFER);
475 util_range_add(&rsc->base, &rsc->valid_buffer_range,
476 buffer_offset, buffer_offset + buffer_size);
477
478 return target;
479 }
480
481 static void
482 fd_stream_output_target_destroy(struct pipe_context *pctx,
483 struct pipe_stream_output_target *target)
484 {
485 pipe_resource_reference(&target->buffer, NULL);
486 FREE(target);
487 }
488
489 static void
490 fd_set_stream_output_targets(struct pipe_context *pctx,
491 unsigned num_targets, struct pipe_stream_output_target **targets,
492 const unsigned *offsets)
493 {
494 struct fd_context *ctx = fd_context(pctx);
495 struct fd_streamout_stateobj *so = &ctx->streamout;
496 unsigned i;
497
498 debug_assert(num_targets <= ARRAY_SIZE(so->targets));
499
500 for (i = 0; i < num_targets; i++) {
501 boolean changed = targets[i] != so->targets[i];
502 boolean reset = (offsets[i] != (unsigned)-1);
503
504 so->reset |= (reset << i);
505
506 if (!changed && !reset)
507 continue;
508
509 if (reset)
510 so->offsets[i] = offsets[i];
511
512 pipe_so_target_reference(&so->targets[i], targets[i]);
513 }
514
515 for (; i < so->num_targets; i++) {
516 pipe_so_target_reference(&so->targets[i], NULL);
517 }
518
519 so->num_targets = num_targets;
520
521 ctx->dirty |= FD_DIRTY_STREAMOUT;
522 }
523
524 static void
525 fd_bind_compute_state(struct pipe_context *pctx, void *state)
526 {
527 struct fd_context *ctx = fd_context(pctx);
528 ctx->compute = state;
529 ctx->dirty_shader[PIPE_SHADER_COMPUTE] |= FD_DIRTY_SHADER_PROG;
530 }
531
532 static void
533 fd_set_compute_resources(struct pipe_context *pctx,
534 unsigned start, unsigned count, struct pipe_surface **prscs)
535 {
536 // TODO
537 }
538
539 /* used by clover to bind global objects, returning the bo address
540 * via handles[n]
541 */
542 static void
543 fd_set_global_binding(struct pipe_context *pctx,
544 unsigned first, unsigned count, struct pipe_resource **prscs,
545 uint32_t **handles)
546 {
547 struct fd_context *ctx = fd_context(pctx);
548 struct fd_global_bindings_stateobj *so = &ctx->global_bindings;
549 unsigned mask = 0;
550
551 if (prscs) {
552 for (unsigned i = 0; i < count; i++) {
553 unsigned n = i + first;
554
555 mask |= BIT(n);
556
557 pipe_resource_reference(&so->buf[n], prscs[i]);
558
559 if (so->buf[n]) {
560 struct fd_resource *rsc = fd_resource(so->buf[n]);
561 uint64_t iova = fd_bo_get_iova(rsc->bo);
562 // TODO need to scream if iova > 32b or fix gallium API..
563 *handles[i] += iova;
564 }
565
566 if (prscs[i])
567 so->enabled_mask |= BIT(n);
568 else
569 so->enabled_mask &= ~BIT(n);
570 }
571 } else {
572 mask = (BIT(count) - 1) << first;
573
574 for (unsigned i = 0; i < count; i++) {
575 unsigned n = i + first;
576 if (so->buf[n]) {
577 struct fd_resource *rsc = fd_resource(so->buf[n]);
578 fd_bo_put_iova(rsc->bo);
579 }
580 pipe_resource_reference(&so->buf[n], NULL);
581 }
582
583 so->enabled_mask &= ~mask;
584 }
585
586 }
587
588 void
589 fd_state_init(struct pipe_context *pctx)
590 {
591 pctx->set_blend_color = fd_set_blend_color;
592 pctx->set_stencil_ref = fd_set_stencil_ref;
593 pctx->set_clip_state = fd_set_clip_state;
594 pctx->set_sample_mask = fd_set_sample_mask;
595 pctx->set_min_samples = fd_set_min_samples;
596 pctx->set_constant_buffer = fd_set_constant_buffer;
597 pctx->set_shader_buffers = fd_set_shader_buffers;
598 pctx->set_shader_images = fd_set_shader_images;
599 pctx->set_framebuffer_state = fd_set_framebuffer_state;
600 pctx->set_polygon_stipple = fd_set_polygon_stipple;
601 pctx->set_scissor_states = fd_set_scissor_states;
602 pctx->set_viewport_states = fd_set_viewport_states;
603
604 pctx->set_vertex_buffers = fd_set_vertex_buffers;
605
606 pctx->bind_blend_state = fd_blend_state_bind;
607 pctx->delete_blend_state = fd_blend_state_delete;
608
609 pctx->bind_rasterizer_state = fd_rasterizer_state_bind;
610 pctx->delete_rasterizer_state = fd_rasterizer_state_delete;
611
612 pctx->bind_depth_stencil_alpha_state = fd_zsa_state_bind;
613 pctx->delete_depth_stencil_alpha_state = fd_zsa_state_delete;
614
615 pctx->create_vertex_elements_state = fd_vertex_state_create;
616 pctx->delete_vertex_elements_state = fd_vertex_state_delete;
617 pctx->bind_vertex_elements_state = fd_vertex_state_bind;
618
619 pctx->create_stream_output_target = fd_create_stream_output_target;
620 pctx->stream_output_target_destroy = fd_stream_output_target_destroy;
621 pctx->set_stream_output_targets = fd_set_stream_output_targets;
622
623 if (has_compute(fd_screen(pctx->screen))) {
624 pctx->bind_compute_state = fd_bind_compute_state;
625 pctx->set_compute_resources = fd_set_compute_resources;
626 pctx->set_global_binding = fd_set_global_binding;
627 }
628 }