freedreno: make hw-query a helper
[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 so->dirty_mask &= ~(1 << index);
107 return;
108 }
109
110 so->enabled_mask |= 1 << index;
111 so->dirty_mask |= 1 << index;
112 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_CONST;
113 ctx->dirty |= FD_DIRTY_CONST;
114 }
115
116 static void
117 fd_set_framebuffer_state(struct pipe_context *pctx,
118 const struct pipe_framebuffer_state *framebuffer)
119 {
120 struct fd_context *ctx = fd_context(pctx);
121 struct pipe_framebuffer_state *cso;
122
123 if (ctx->screen->reorder) {
124 struct fd_batch *batch, *old_batch = NULL;
125
126 fd_batch_reference(&old_batch, ctx->batch);
127
128 if (likely(old_batch))
129 fd_batch_set_stage(old_batch, old_batch->draw, FD_STAGE_NULL);
130
131 batch = fd_batch_from_fb(&ctx->screen->batch_cache, ctx, framebuffer);
132 fd_batch_reference(&ctx->batch, NULL);
133 fd_reset_wfi(batch);
134 ctx->batch = batch;
135 fd_context_all_dirty(ctx);
136
137 if (old_batch && old_batch->blit && !old_batch->back_blit) {
138 /* for blits, there is not really much point in hanging on
139 * to the uncommitted batch (ie. you probably don't blit
140 * multiple times to the same surface), so we might as
141 * well go ahead and flush this one:
142 */
143 fd_batch_flush(old_batch, false);
144 }
145
146 fd_batch_reference(&old_batch, NULL);
147 } else {
148 DBG("%d: cbufs[0]=%p, zsbuf=%p", ctx->batch->needs_flush,
149 framebuffer->cbufs[0], framebuffer->zsbuf);
150 fd_batch_flush(ctx->batch, false);
151 }
152
153 cso = &ctx->batch->framebuffer;
154
155 util_copy_framebuffer_state(cso, framebuffer);
156
157 ctx->dirty |= FD_DIRTY_FRAMEBUFFER;
158
159 ctx->disabled_scissor.minx = 0;
160 ctx->disabled_scissor.miny = 0;
161 ctx->disabled_scissor.maxx = cso->width;
162 ctx->disabled_scissor.maxy = cso->height;
163
164 ctx->dirty |= FD_DIRTY_SCISSOR;
165 }
166
167 static void
168 fd_set_polygon_stipple(struct pipe_context *pctx,
169 const struct pipe_poly_stipple *stipple)
170 {
171 struct fd_context *ctx = fd_context(pctx);
172 ctx->stipple = *stipple;
173 ctx->dirty |= FD_DIRTY_STIPPLE;
174 }
175
176 static void
177 fd_set_scissor_states(struct pipe_context *pctx,
178 unsigned start_slot,
179 unsigned num_scissors,
180 const struct pipe_scissor_state *scissor)
181 {
182 struct fd_context *ctx = fd_context(pctx);
183
184 ctx->scissor = *scissor;
185 ctx->dirty |= FD_DIRTY_SCISSOR;
186 }
187
188 static void
189 fd_set_viewport_states(struct pipe_context *pctx,
190 unsigned start_slot,
191 unsigned num_viewports,
192 const struct pipe_viewport_state *viewport)
193 {
194 struct fd_context *ctx = fd_context(pctx);
195 ctx->viewport = *viewport;
196 ctx->dirty |= FD_DIRTY_VIEWPORT;
197 }
198
199 static void
200 fd_set_vertex_buffers(struct pipe_context *pctx,
201 unsigned start_slot, unsigned count,
202 const struct pipe_vertex_buffer *vb)
203 {
204 struct fd_context *ctx = fd_context(pctx);
205 struct fd_vertexbuf_stateobj *so = &ctx->vtx.vertexbuf;
206 int i;
207
208 /* on a2xx, pitch is encoded in the vtx fetch instruction, so
209 * we need to mark VTXSTATE as dirty as well to trigger patching
210 * and re-emitting the vtx shader:
211 */
212 if (ctx->screen->gpu_id < 300) {
213 for (i = 0; i < count; i++) {
214 bool new_enabled = vb && (vb[i].buffer || vb[i].user_buffer);
215 bool old_enabled = so->vb[i].buffer || so->vb[i].user_buffer;
216 uint32_t new_stride = vb ? vb[i].stride : 0;
217 uint32_t old_stride = so->vb[i].stride;
218 if ((new_enabled != old_enabled) || (new_stride != old_stride)) {
219 ctx->dirty |= FD_DIRTY_VTXSTATE;
220 break;
221 }
222 }
223 }
224
225 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, count);
226 so->count = util_last_bit(so->enabled_mask);
227
228 ctx->dirty |= FD_DIRTY_VTXBUF;
229 }
230
231 static void
232 fd_set_index_buffer(struct pipe_context *pctx,
233 const struct pipe_index_buffer *ib)
234 {
235 struct fd_context *ctx = fd_context(pctx);
236
237 if (ib) {
238 pipe_resource_reference(&ctx->indexbuf.buffer, ib->buffer);
239 ctx->indexbuf.index_size = ib->index_size;
240 ctx->indexbuf.offset = ib->offset;
241 ctx->indexbuf.user_buffer = ib->user_buffer;
242 } else {
243 pipe_resource_reference(&ctx->indexbuf.buffer, NULL);
244 }
245
246 ctx->dirty |= FD_DIRTY_INDEXBUF;
247 }
248
249 static void
250 fd_blend_state_bind(struct pipe_context *pctx, void *hwcso)
251 {
252 struct fd_context *ctx = fd_context(pctx);
253 struct pipe_blend_state *cso = hwcso;
254 bool old_is_dual = ctx->blend ?
255 ctx->blend->rt[0].blend_enable && util_blend_state_is_dual(ctx->blend, 0) :
256 false;
257 bool new_is_dual = cso ?
258 cso->rt[0].blend_enable && util_blend_state_is_dual(cso, 0) :
259 false;
260 ctx->blend = hwcso;
261 ctx->dirty |= FD_DIRTY_BLEND;
262 if (old_is_dual != new_is_dual)
263 ctx->dirty |= FD_DIRTY_BLEND_DUAL;
264 }
265
266 static void
267 fd_blend_state_delete(struct pipe_context *pctx, void *hwcso)
268 {
269 FREE(hwcso);
270 }
271
272 static void
273 fd_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
274 {
275 struct fd_context *ctx = fd_context(pctx);
276 struct pipe_scissor_state *old_scissor = fd_context_get_scissor(ctx);
277
278 ctx->rasterizer = hwcso;
279 ctx->dirty |= FD_DIRTY_RASTERIZER;
280
281 /* if scissor enable bit changed we need to mark scissor
282 * state as dirty as well:
283 * NOTE: we can do a shallow compare, since we only care
284 * if it changed to/from &ctx->disable_scissor
285 */
286 if (old_scissor != fd_context_get_scissor(ctx))
287 ctx->dirty |= FD_DIRTY_SCISSOR;
288 }
289
290 static void
291 fd_rasterizer_state_delete(struct pipe_context *pctx, void *hwcso)
292 {
293 FREE(hwcso);
294 }
295
296 static void
297 fd_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
298 {
299 struct fd_context *ctx = fd_context(pctx);
300 ctx->zsa = hwcso;
301 ctx->dirty |= FD_DIRTY_ZSA;
302 }
303
304 static void
305 fd_zsa_state_delete(struct pipe_context *pctx, void *hwcso)
306 {
307 FREE(hwcso);
308 }
309
310 static void *
311 fd_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
312 const struct pipe_vertex_element *elements)
313 {
314 struct fd_vertex_stateobj *so = CALLOC_STRUCT(fd_vertex_stateobj);
315
316 if (!so)
317 return NULL;
318
319 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
320 so->num_elements = num_elements;
321
322 return so;
323 }
324
325 static void
326 fd_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
327 {
328 FREE(hwcso);
329 }
330
331 static void
332 fd_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
333 {
334 struct fd_context *ctx = fd_context(pctx);
335 ctx->vtx.vtx = hwcso;
336 ctx->dirty |= FD_DIRTY_VTXSTATE;
337 }
338
339 static struct pipe_stream_output_target *
340 fd_create_stream_output_target(struct pipe_context *pctx,
341 struct pipe_resource *prsc, unsigned buffer_offset,
342 unsigned buffer_size)
343 {
344 struct pipe_stream_output_target *target;
345 struct fd_resource *rsc = fd_resource(prsc);
346
347 target = CALLOC_STRUCT(pipe_stream_output_target);
348 if (!target)
349 return NULL;
350
351 pipe_reference_init(&target->reference, 1);
352 pipe_resource_reference(&target->buffer, prsc);
353
354 target->context = pctx;
355 target->buffer_offset = buffer_offset;
356 target->buffer_size = buffer_size;
357
358 assert(rsc->base.b.target == PIPE_BUFFER);
359 util_range_add(&rsc->valid_buffer_range,
360 buffer_offset, buffer_offset + buffer_size);
361
362 return target;
363 }
364
365 static void
366 fd_stream_output_target_destroy(struct pipe_context *pctx,
367 struct pipe_stream_output_target *target)
368 {
369 pipe_resource_reference(&target->buffer, NULL);
370 FREE(target);
371 }
372
373 static void
374 fd_set_stream_output_targets(struct pipe_context *pctx,
375 unsigned num_targets, struct pipe_stream_output_target **targets,
376 const unsigned *offsets)
377 {
378 struct fd_context *ctx = fd_context(pctx);
379 struct fd_streamout_stateobj *so = &ctx->streamout;
380 unsigned i;
381
382 debug_assert(num_targets <= ARRAY_SIZE(so->targets));
383
384 for (i = 0; i < num_targets; i++) {
385 boolean changed = targets[i] != so->targets[i];
386 boolean append = (offsets[i] == (unsigned)-1);
387
388 if (!changed && append)
389 continue;
390
391 if (!append)
392 so->offsets[i] = offsets[i];
393
394 pipe_so_target_reference(&so->targets[i], targets[i]);
395 }
396
397 for (; i < so->num_targets; i++) {
398 pipe_so_target_reference(&so->targets[i], NULL);
399 }
400
401 so->num_targets = num_targets;
402
403 ctx->dirty |= FD_DIRTY_STREAMOUT;
404 }
405
406 void
407 fd_state_init(struct pipe_context *pctx)
408 {
409 pctx->set_blend_color = fd_set_blend_color;
410 pctx->set_stencil_ref = fd_set_stencil_ref;
411 pctx->set_clip_state = fd_set_clip_state;
412 pctx->set_sample_mask = fd_set_sample_mask;
413 pctx->set_constant_buffer = fd_set_constant_buffer;
414 pctx->set_framebuffer_state = fd_set_framebuffer_state;
415 pctx->set_polygon_stipple = fd_set_polygon_stipple;
416 pctx->set_scissor_states = fd_set_scissor_states;
417 pctx->set_viewport_states = fd_set_viewport_states;
418
419 pctx->set_vertex_buffers = fd_set_vertex_buffers;
420 pctx->set_index_buffer = fd_set_index_buffer;
421
422 pctx->bind_blend_state = fd_blend_state_bind;
423 pctx->delete_blend_state = fd_blend_state_delete;
424
425 pctx->bind_rasterizer_state = fd_rasterizer_state_bind;
426 pctx->delete_rasterizer_state = fd_rasterizer_state_delete;
427
428 pctx->bind_depth_stencil_alpha_state = fd_zsa_state_bind;
429 pctx->delete_depth_stencil_alpha_state = fd_zsa_state_delete;
430
431 pctx->create_vertex_elements_state = fd_vertex_state_create;
432 pctx->delete_vertex_elements_state = fd_vertex_state_delete;
433 pctx->bind_vertex_elements_state = fd_vertex_state_bind;
434
435 pctx->create_stream_output_target = fd_create_stream_output_target;
436 pctx->stream_output_target_destroy = fd_stream_output_target_destroy;
437 pctx->set_stream_output_targets = fd_set_stream_output_targets;
438 }