panfrost: Remove MRT indirection in blend shaders
[mesa.git] / src / gallium / drivers / panfrost / pan_attributes.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "pan_bo.h"
27 #include "pan_context.h"
28
29 void
30 panfrost_emit_vertex_data(struct panfrost_batch *batch)
31 {
32 struct panfrost_context *ctx = batch->ctx;
33 struct panfrost_vertex_state *so = ctx->vertex;
34
35 /* Staged mali_attr, and index into them. i =/= k, depending on the
36 * vertex buffer mask and instancing. Twice as much room is allocated,
37 * for a worst case of NPOT_DIVIDEs which take up extra slot */
38 union mali_attr attrs[PIPE_MAX_ATTRIBS * 2];
39 unsigned k = 0;
40
41 for (unsigned i = 0; i < so->num_elements; ++i) {
42 /* We map a mali_attr to be 1:1 with the mali_attr_meta, which
43 * means duplicating some vertex buffers (who cares? aside from
44 * maybe some caching implications but I somehow doubt that
45 * matters) */
46
47 struct pipe_vertex_element *elem = &so->pipe[i];
48 unsigned vbi = elem->vertex_buffer_index;
49
50 /* The exception to 1:1 mapping is that we can have multiple
51 * entries (NPOT divisors), so we fixup anyways */
52
53 so->hw[i].index = k;
54
55 if (!(ctx->vb_mask & (1 << vbi))) continue;
56
57 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
58 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
59
60 if (!rsrc) continue;
61
62 /* Align to 64 bytes by masking off the lower bits. This
63 * will be adjusted back when we fixup the src_offset in
64 * mali_attr_meta */
65
66 mali_ptr raw_addr = rsrc->bo->gpu + buf->buffer_offset;
67 mali_ptr addr = raw_addr & ~63;
68 unsigned chopped_addr = raw_addr - addr;
69
70 /* Add a dependency of the batch on the vertex buffer */
71 panfrost_batch_add_bo(batch, rsrc->bo,
72 PAN_BO_ACCESS_SHARED |
73 PAN_BO_ACCESS_READ |
74 PAN_BO_ACCESS_VERTEX_TILER);
75
76 /* Set common fields */
77 attrs[k].elements = addr;
78 attrs[k].stride = buf->stride;
79
80 /* Since we advanced the base pointer, we shrink the buffer
81 * size */
82 attrs[k].size = rsrc->base.width0 - buf->buffer_offset;
83
84 /* We need to add the extra size we masked off (for
85 * correctness) so the data doesn't get clamped away */
86 attrs[k].size += chopped_addr;
87
88 /* For non-instancing make sure we initialize */
89 attrs[k].shift = attrs[k].extra_flags = 0;
90
91 /* Instancing uses a dramatically different code path than
92 * linear, so dispatch for the actual emission now that the
93 * common code is finished */
94
95 unsigned divisor = elem->instance_divisor;
96
97 if (divisor && ctx->instance_count == 1) {
98 /* Silly corner case where there's a divisor(=1) but
99 * there's no legitimate instancing. So we want *every*
100 * attribute to be the same. So set stride to zero so
101 * we don't go anywhere. */
102
103 attrs[k].size = attrs[k].stride + chopped_addr;
104 attrs[k].stride = 0;
105 attrs[k++].elements |= MALI_ATTR_LINEAR;
106 } else if (ctx->instance_count <= 1) {
107 /* Normal, non-instanced attributes */
108 attrs[k++].elements |= MALI_ATTR_LINEAR;
109 } else {
110 unsigned instance_shift = batch->ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift;
111 unsigned instance_odd = batch->ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd;
112
113 k += panfrost_vertex_instanced(batch->ctx->padded_count,
114 instance_shift, instance_odd, divisor, &attrs[k]);
115 }
116 }
117
118 /* Add special gl_VertexID/gl_InstanceID buffers */
119
120 panfrost_vertex_id(ctx->padded_count, &attrs[k]);
121 so->hw[PAN_VERTEX_ID].index = k++;
122
123 panfrost_instance_id(ctx->padded_count, &attrs[k]);
124 so->hw[PAN_INSTANCE_ID].index = k++;
125
126 /* Upload whatever we emitted and go */
127
128 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attributes =
129 panfrost_upload_transient(batch, attrs, k * sizeof(union mali_attr));
130 }
131
132