summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/freedreno/freedreno_state.c
blob: 489578b0dc7ecefd8e2f872748d1650262650058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */

/*
 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#include "pipe/p_state.h"
#include "util/u_dual_blend.h"
#include "util/u_string.h"
#include "util/u_memory.h"
#include "util/u_helpers.h"

#include "freedreno_state.h"
#include "freedreno_context.h"
#include "freedreno_resource.h"
#include "freedreno_texture.h"
#include "freedreno_gmem.h"
#include "freedreno_query_hw.h"
#include "freedreno_util.h"

/* All the generic state handling.. In case of CSO's that are specific
 * to the GPU version, when the bind and the delete are common they can
 * go in here.
 */

static void
fd_set_blend_color(struct pipe_context *pctx,
		const struct pipe_blend_color *blend_color)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->blend_color = *blend_color;
	ctx->dirty |= FD_DIRTY_BLEND_COLOR;
}

static void
fd_set_stencil_ref(struct pipe_context *pctx,
		const struct pipe_stencil_ref *stencil_ref)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->stencil_ref =* stencil_ref;
	ctx->dirty |= FD_DIRTY_STENCIL_REF;
}

static void
fd_set_clip_state(struct pipe_context *pctx,
		const struct pipe_clip_state *clip)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->ucp = *clip;
	ctx->dirty |= FD_DIRTY_UCP;
}

static void
fd_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->sample_mask = (uint16_t)sample_mask;
	ctx->dirty |= FD_DIRTY_SAMPLE_MASK;
}

/* notes from calim on #dri-devel:
 * index==0 will be non-UBO (ie. glUniformXYZ()) all packed together padded
 * out to vec4's
 * I should be able to consider that I own the user_ptr until the next
 * set_constant_buffer() call, at which point I don't really care about the
 * previous values.
 * index>0 will be UBO's.. well, I'll worry about that later
 */
static void
fd_set_constant_buffer(struct pipe_context *pctx,
		enum pipe_shader_type shader, uint index,
		const struct pipe_constant_buffer *cb)
{
	struct fd_context *ctx = fd_context(pctx);
	struct fd_constbuf_stateobj *so = &ctx->constbuf[shader];

	util_copy_constant_buffer(&so->cb[index], cb);

	/* Note that the state tracker can unbind constant buffers by
	 * passing NULL here.
	 */
	if (unlikely(!cb)) {
		so->enabled_mask &= ~(1 << index);
		so->dirty_mask &= ~(1 << index);
		return;
	}

	so->enabled_mask |= 1 << index;
	so->dirty_mask |= 1 << index;
	ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_CONST;
	ctx->dirty |= FD_DIRTY_CONST;
}

static void
fd_set_shader_buffers(struct pipe_context *pctx,
		enum pipe_shader_type shader,
		unsigned start, unsigned count,
		const struct pipe_shader_buffer *buffers)
{
	struct fd_context *ctx = fd_context(pctx);
	struct fd_shaderbuf_stateobj *so = &ctx->shaderbuf[shader];
	unsigned mask = 0;

	if (buffers) {
		for (unsigned i = 0; i < count; i++) {
			unsigned n = i + start;
			struct pipe_shader_buffer *buf = &so->sb[n];

			if ((buf->buffer == buffers[i].buffer) &&
					(buf->buffer_offset == buffers[i].buffer_offset) &&
					(buf->buffer_size == buffers[i].buffer_size))
				continue;

			mask |= BIT(n);

			buf->buffer_offset = buffers[i].buffer_offset;
			buf->buffer_size = buffers[i].buffer_size;
			pipe_resource_reference(&buf->buffer, buffers[i].buffer);

			if (buf->buffer)
				so->enabled_mask |= BIT(n);
			else
				so->enabled_mask &= ~BIT(n);
		}
	} else {
		mask = (BIT(count) - 1) << start;

		for (unsigned i = 0; i < count; i++) {
			unsigned n = i + start;
			struct pipe_shader_buffer *buf = &so->sb[n];

			pipe_resource_reference(&buf->buffer, NULL);
		}

		so->enabled_mask &= ~mask;
	}

	so->dirty_mask |= mask;
	ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_SSBO;
}

static void
fd_set_framebuffer_state(struct pipe_context *pctx,
		const struct pipe_framebuffer_state *framebuffer)
{
	struct fd_context *ctx = fd_context(pctx);
	struct pipe_framebuffer_state *cso;

	if (ctx->screen->reorder) {
		struct fd_batch *batch, *old_batch = NULL;

		fd_batch_reference(&old_batch, ctx->batch);

		if (likely(old_batch))
			fd_batch_set_stage(old_batch, FD_STAGE_NULL);

		batch = fd_batch_from_fb(&ctx->screen->batch_cache, ctx, framebuffer);
		fd_batch_reference(&ctx->batch, NULL);
		fd_reset_wfi(batch);
		ctx->batch = batch;
		fd_context_all_dirty(ctx);

		if (old_batch && old_batch->blit && !old_batch->back_blit) {
			/* for blits, there is not really much point in hanging on
			 * to the uncommitted batch (ie. you probably don't blit
			 * multiple times to the same surface), so we might as
			 * well go ahead and flush this one:
			 */
			fd_batch_flush(old_batch, false);
		}

		fd_batch_reference(&old_batch, NULL);
	} else {
		DBG("%d: cbufs[0]=%p, zsbuf=%p", ctx->batch->needs_flush,
				framebuffer->cbufs[0], framebuffer->zsbuf);
		fd_batch_flush(ctx->batch, false);
	}

	cso = &ctx->batch->framebuffer;

	util_copy_framebuffer_state(cso, framebuffer);

	ctx->dirty |= FD_DIRTY_FRAMEBUFFER;

	ctx->disabled_scissor.minx = 0;
	ctx->disabled_scissor.miny = 0;
	ctx->disabled_scissor.maxx = cso->width;
	ctx->disabled_scissor.maxy = cso->height;

	ctx->dirty |= FD_DIRTY_SCISSOR;
}

static void
fd_set_polygon_stipple(struct pipe_context *pctx,
		const struct pipe_poly_stipple *stipple)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->stipple = *stipple;
	ctx->dirty |= FD_DIRTY_STIPPLE;
}

static void
fd_set_scissor_states(struct pipe_context *pctx,
		unsigned start_slot,
		unsigned num_scissors,
		const struct pipe_scissor_state *scissor)
{
	struct fd_context *ctx = fd_context(pctx);

	ctx->scissor = *scissor;
	ctx->dirty |= FD_DIRTY_SCISSOR;
}

static void
fd_set_viewport_states(struct pipe_context *pctx,
		unsigned start_slot,
		unsigned num_viewports,
		const struct pipe_viewport_state *viewport)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->viewport = *viewport;
	ctx->dirty |= FD_DIRTY_VIEWPORT;
}

static void
fd_set_vertex_buffers(struct pipe_context *pctx,
		unsigned start_slot, unsigned count,
		const struct pipe_vertex_buffer *vb)
{
	struct fd_context *ctx = fd_context(pctx);
	struct fd_vertexbuf_stateobj *so = &ctx->vtx.vertexbuf;
	int i;

	/* on a2xx, pitch is encoded in the vtx fetch instruction, so
	 * we need to mark VTXSTATE as dirty as well to trigger patching
	 * and re-emitting the vtx shader:
	 */
	if (ctx->screen->gpu_id < 300) {
		for (i = 0; i < count; i++) {
			bool new_enabled = vb && vb[i].buffer.resource;
			bool old_enabled = so->vb[i].buffer.resource != NULL;
			uint32_t new_stride = vb ? vb[i].stride : 0;
			uint32_t old_stride = so->vb[i].stride;
			if ((new_enabled != old_enabled) || (new_stride != old_stride)) {
				ctx->dirty |= FD_DIRTY_VTXSTATE;
				break;
			}
		}
	}

	util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, count);
	so->count = util_last_bit(so->enabled_mask);

	ctx->dirty |= FD_DIRTY_VTXBUF;
}

static void
fd_set_index_buffer(struct pipe_context *pctx,
		const struct pipe_index_buffer *ib)
{
	struct fd_context *ctx = fd_context(pctx);

	if (ib) {
		pipe_resource_reference(&ctx->indexbuf.buffer, ib->buffer);
		ctx->indexbuf.index_size = ib->index_size;
		ctx->indexbuf.offset = ib->offset;
		ctx->indexbuf.user_buffer = ib->user_buffer;
	} else {
		pipe_resource_reference(&ctx->indexbuf.buffer, NULL);
	}

	ctx->dirty |= FD_DIRTY_INDEXBUF;
}

static void
fd_blend_state_bind(struct pipe_context *pctx, void *hwcso)
{
	struct fd_context *ctx = fd_context(pctx);
	struct pipe_blend_state *cso = hwcso;
	bool old_is_dual = ctx->blend ?
		ctx->blend->rt[0].blend_enable && util_blend_state_is_dual(ctx->blend, 0) :
		false;
	bool new_is_dual = cso ?
		cso->rt[0].blend_enable && util_blend_state_is_dual(cso, 0) :
		false;
	ctx->blend = hwcso;
	ctx->dirty |= FD_DIRTY_BLEND;
	if (old_is_dual != new_is_dual)
		ctx->dirty |= FD_DIRTY_BLEND_DUAL;
}

static void
fd_blend_state_delete(struct pipe_context *pctx, void *hwcso)
{
	FREE(hwcso);
}

static void
fd_rasterizer_state_bind(struct pipe_context *pctx, void *hwcso)
{
	struct fd_context *ctx = fd_context(pctx);
	struct pipe_scissor_state *old_scissor = fd_context_get_scissor(ctx);

	ctx->rasterizer = hwcso;
	ctx->dirty |= FD_DIRTY_RASTERIZER;

	/* if scissor enable bit changed we need to mark scissor
	 * state as dirty as well:
	 * NOTE: we can do a shallow compare, since we only care
	 * if it changed to/from &ctx->disable_scissor
	 */
	if (old_scissor != fd_context_get_scissor(ctx))
		ctx->dirty |= FD_DIRTY_SCISSOR;
}

static void
fd_rasterizer_state_delete(struct pipe_context *pctx, void *hwcso)
{
	FREE(hwcso);
}

static void
fd_zsa_state_bind(struct pipe_context *pctx, void *hwcso)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->zsa = hwcso;
	ctx->dirty |= FD_DIRTY_ZSA;
}

static void
fd_zsa_state_delete(struct pipe_context *pctx, void *hwcso)
{
	FREE(hwcso);
}

static void *
fd_vertex_state_create(struct pipe_context *pctx, unsigned num_elements,
		const struct pipe_vertex_element *elements)
{
	struct fd_vertex_stateobj *so = CALLOC_STRUCT(fd_vertex_stateobj);

	if (!so)
		return NULL;

	memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
	so->num_elements = num_elements;

	return so;
}

static void
fd_vertex_state_delete(struct pipe_context *pctx, void *hwcso)
{
	FREE(hwcso);
}

static void
fd_vertex_state_bind(struct pipe_context *pctx, void *hwcso)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->vtx.vtx = hwcso;
	ctx->dirty |= FD_DIRTY_VTXSTATE;
}

static struct pipe_stream_output_target *
fd_create_stream_output_target(struct pipe_context *pctx,
		struct pipe_resource *prsc, unsigned buffer_offset,
		unsigned buffer_size)
{
	struct pipe_stream_output_target *target;
	struct fd_resource *rsc = fd_resource(prsc);

	target = CALLOC_STRUCT(pipe_stream_output_target);
	if (!target)
		return NULL;

	pipe_reference_init(&target->reference, 1);
	pipe_resource_reference(&target->buffer, prsc);

	target->context = pctx;
	target->buffer_offset = buffer_offset;
	target->buffer_size = buffer_size;

	assert(rsc->base.b.target == PIPE_BUFFER);
	util_range_add(&rsc->valid_buffer_range,
		buffer_offset, buffer_offset + buffer_size);

	return target;
}

static void
fd_stream_output_target_destroy(struct pipe_context *pctx,
		struct pipe_stream_output_target *target)
{
	pipe_resource_reference(&target->buffer, NULL);
	FREE(target);
}

static void
fd_set_stream_output_targets(struct pipe_context *pctx,
		unsigned num_targets, struct pipe_stream_output_target **targets,
		const unsigned *offsets)
{
	struct fd_context *ctx = fd_context(pctx);
	struct fd_streamout_stateobj *so = &ctx->streamout;
	unsigned i;

	debug_assert(num_targets <= ARRAY_SIZE(so->targets));

	for (i = 0; i < num_targets; i++) {
		boolean changed = targets[i] != so->targets[i];
		boolean append = (offsets[i] == (unsigned)-1);

		if (!changed && append)
			continue;

		if (!append)
			so->offsets[i] = offsets[i];

		pipe_so_target_reference(&so->targets[i], targets[i]);
	}

	for (; i < so->num_targets; i++) {
		pipe_so_target_reference(&so->targets[i], NULL);
	}

	so->num_targets = num_targets;

	ctx->dirty |= FD_DIRTY_STREAMOUT;
}

static void
fd_bind_compute_state(struct pipe_context *pctx, void *state)
{
	struct fd_context *ctx = fd_context(pctx);
	ctx->compute = state;
	ctx->dirty_shader[PIPE_SHADER_COMPUTE] |= FD_DIRTY_SHADER_PROG;
}

static void
fd_set_compute_resources(struct pipe_context *pctx,
		unsigned start, unsigned count, struct pipe_surface **prscs)
{
	// TODO
}

static void
fd_set_global_binding(struct pipe_context *pctx,
		unsigned first, unsigned count, struct pipe_resource **prscs,
		uint32_t **handles)
{
	/* TODO only used by clover.. seems to need us to return the actual
	 * gpuaddr of the buffer.. which isn't really exposed to mesa atm.
	 * How is this used?
	 */
}

void
fd_state_init(struct pipe_context *pctx)
{
	pctx->set_blend_color = fd_set_blend_color;
	pctx->set_stencil_ref = fd_set_stencil_ref;
	pctx->set_clip_state = fd_set_clip_state;
	pctx->set_sample_mask = fd_set_sample_mask;
	pctx->set_constant_buffer = fd_set_constant_buffer;
	pctx->set_shader_buffers = fd_set_shader_buffers;
	pctx->set_framebuffer_state = fd_set_framebuffer_state;
	pctx->set_polygon_stipple = fd_set_polygon_stipple;
	pctx->set_scissor_states = fd_set_scissor_states;
	pctx->set_viewport_states = fd_set_viewport_states;

	pctx->set_vertex_buffers = fd_set_vertex_buffers;
	pctx->set_index_buffer = fd_set_index_buffer;

	pctx->bind_blend_state = fd_blend_state_bind;
	pctx->delete_blend_state = fd_blend_state_delete;

	pctx->bind_rasterizer_state = fd_rasterizer_state_bind;
	pctx->delete_rasterizer_state = fd_rasterizer_state_delete;

	pctx->bind_depth_stencil_alpha_state = fd_zsa_state_bind;
	pctx->delete_depth_stencil_alpha_state = fd_zsa_state_delete;

	pctx->create_vertex_elements_state = fd_vertex_state_create;
	pctx->delete_vertex_elements_state = fd_vertex_state_delete;
	pctx->bind_vertex_elements_state = fd_vertex_state_bind;

	pctx->create_stream_output_target = fd_create_stream_output_target;
	pctx->stream_output_target_destroy = fd_stream_output_target_destroy;
	pctx->set_stream_output_targets = fd_set_stream_output_targets;

	if (has_compute(fd_screen(pctx->screen))) {
		pctx->bind_compute_state = fd_bind_compute_state;
		pctx->set_compute_resources = fd_set_compute_resources;
		pctx->set_global_binding = fd_set_global_binding;
	}
}