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