freedreno/a3xx: add blend state
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_draw.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2013 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_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_prim.h"
33
34 #include "freedreno_state.h"
35 #include "freedreno_resource.h"
36
37 #include "fd3_draw.h"
38 #include "fd3_context.h"
39 #include "fd3_emit.h"
40 #include "fd3_program.h"
41 #include "fd3_util.h"
42 #include "fd3_zsa.h"
43
44
45 static void
46 emit_vertexbufs(struct fd_context *ctx)
47 {
48 struct fd_vertex_stateobj *vtx = ctx->vtx;
49 struct fd_vertexbuf_stateobj *vertexbuf = &ctx->vertexbuf;
50 struct fd3_vertex_buf bufs[PIPE_MAX_ATTRIBS];
51 unsigned i;
52
53 if (!vtx->num_elements)
54 return;
55
56 for (i = 0; i < vtx->num_elements; i++) {
57 struct pipe_vertex_element *elem = &vtx->pipe[i];
58 struct pipe_vertex_buffer *vb =
59 &vertexbuf->vb[elem->vertex_buffer_index];
60 bufs[i].offset = vb->buffer_offset + elem->src_offset;
61 bufs[i].stride = vb->stride;
62 bufs[i].prsc = vb->buffer;
63 bufs[i].format = elem->src_format;
64 }
65
66 fd3_emit_vertex_bufs(ctx->ring, &ctx->prog, bufs, vtx->num_elements);
67 }
68
69 static void
70 fd3_draw(struct fd_context *ctx, const struct pipe_draw_info *info)
71 {
72 struct fd_ringbuffer *ring = ctx->ring;
73 unsigned dirty = ctx->dirty;
74
75 fd3_emit_state(ctx, dirty);
76
77 if (dirty & FD_DIRTY_VTXBUF)
78 emit_vertexbufs(ctx);
79
80 OUT_PKT0(ring, REG_A3XX_PC_VERTEX_REUSE_BLOCK_CNTL, 1);
81 OUT_RING(ring, 0x0000000b); /* PC_VERTEX_REUSE_BLOCK_CNTL */
82
83 OUT_WFI (ring);
84
85 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
86 OUT_RING(ring, info->min_index); /* VFD_INDEX_MIN */
87 OUT_RING(ring, info->max_index); /* VFD_INDEX_MAX */
88 OUT_RING(ring, info->start_instance); /* VFD_INSTANCEID_OFFSET */
89 OUT_RING(ring, info->start); /* VFD_INDEX_OFFSET */
90
91 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
92 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
93 info->restart_index : 0xffffffff);
94
95 fd_draw_emit(ctx, info);
96 }
97
98 static void
99 fd3_clear(struct fd_context *ctx, unsigned buffers,
100 const union pipe_color_union *color, double depth, unsigned stencil)
101 {
102 struct fd3_context *fd3_ctx = fd3_context(ctx);
103 struct fd_ringbuffer *ring = ctx->ring;
104 unsigned ce, i;
105
106 /* emit generic state now: */
107 fd3_emit_state(ctx, ctx->dirty & (FD_DIRTY_VIEWPORT |
108 FD_DIRTY_FRAMEBUFFER | FD_DIRTY_SCISSOR));
109
110 OUT_PKT0(ring, REG_A3XX_RB_BLEND_ALPHA, 1);
111 OUT_RING(ring, A3XX_RB_BLEND_ALPHA_UINT(0xff) |
112 A3XX_RB_BLEND_ALPHA_FLOAT(1.0));
113
114 fd3_emit_rbrc_draw_state(ring,
115 A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER));
116
117 if (buffers & PIPE_CLEAR_DEPTH) {
118 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
119 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
120 A3XX_RB_DEPTH_CONTROL_Z_ENABLE |
121 A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS));
122
123 OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_ZOFFSET, 2);
124 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZOFFSET(0.0));
125 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZSCALE(depth));
126 ctx->dirty |= FD_DIRTY_VIEWPORT;
127 } else {
128 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
129 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
130 }
131
132 if (buffers & PIPE_CLEAR_STENCIL) {
133 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
134 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(stencil) |
135 A3XX_RB_STENCILREFMASK_STENCILMASK(stencil) |
136 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
137 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
138 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
139 0xff000000 | // XXX ???
140 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
141
142 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
143 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
144 A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
145 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
146 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_REPLACE) |
147 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
148 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
149 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
150 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
151 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
152 } else {
153 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
154 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
155 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
156 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0));
157 OUT_RING(ring, A3XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
158 A3XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
159 A3XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0));
160
161 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
162 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
163 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
164 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
165 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
166 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
167 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
168 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
169 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
170 }
171
172 if (buffers & PIPE_CLEAR_COLOR) {
173 ce = 0xf;
174 } else {
175 ce = 0x0;
176 }
177
178 for (i = 0; i < 4; i++) {
179 OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
180 OUT_RING(ring, A3XX_RB_MRT_CONTROL_ROP_CODE(12) |
181 A3XX_RB_MRT_CONTROL_DITHER_MODE(DITHER_ALWAYS) |
182 A3XX_RB_MRT_CONTROL_COMPONENT_ENABLE(ce));
183
184 OUT_PKT0(ring, REG_A3XX_RB_MRT_BLEND_CONTROL(i), 1);
185 OUT_RING(ring, A3XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE) |
186 A3XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
187 A3XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO) |
188 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE) |
189 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
190 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO) |
191 A3XX_RB_MRT_BLEND_CONTROL_CLAMP_ENABLE);
192 }
193
194 OUT_PKT0(ring, REG_A3XX_GRAS_SU_MODE_CONTROL, 1);
195 OUT_RING(ring, A3XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0));
196
197 fd3_program_emit(ring, &ctx->solid_prog);
198
199 fd3_emit_vertex_bufs(ring, &ctx->solid_prog, (struct fd3_vertex_buf[]) {
200 { .prsc = fd3_ctx->solid_vbuf, .stride = 12, .format = PIPE_FORMAT_R32G32B32_FLOAT },
201 }, 1);
202
203 fd3_emit_constant(ring, SB_FRAG_SHADER, 0, 0, 4, color->ui, NULL);
204
205 OUT_PKT0(ring, REG_A3XX_PC_PRIM_VTX_CNTL, 1);
206 OUT_RING(ring, A3XX_PC_PRIM_VTX_CNTL_STRIDE_IN_VPC(0) |
207 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_FRONT_PTYPE(PC_DRAW_TRIANGLES) |
208 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_BACK_PTYPE(PC_DRAW_TRIANGLES) |
209 A3XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
210 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
211 OUT_RING(ring, 0); /* VFD_INDEX_MIN */
212 OUT_RING(ring, 2); /* VFD_INDEX_MAX */
213 OUT_RING(ring, 0); /* VFD_INSTANCEID_OFFSET */
214 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
215 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
216 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
217
218 OUT_PKT3(ring, CP_EVENT_WRITE, 1);
219 OUT_RING(ring, PERFCOUNTER_STOP);
220
221 fd_draw(ctx, DI_PT_RECTLIST, DI_SRC_SEL_AUTO_INDEX, 2,
222 INDEX_SIZE_IGN, 0, 0, NULL);
223
224 OUT_WFI (ring);
225 }
226
227 void
228 fd3_draw_init(struct pipe_context *pctx)
229 {
230 struct fd_context *ctx = fd_context(pctx);
231 ctx->draw = fd3_draw;
232 ctx->clear = fd3_clear;
233 }