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