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