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