freedreno/a3xx: fix blend state corruption issue
[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_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
84 OUT_RING(ring, info->min_index); /* VFD_INDEX_MIN */
85 OUT_RING(ring, info->max_index); /* VFD_INDEX_MAX */
86 OUT_RING(ring, info->start_instance); /* VFD_INSTANCEID_OFFSET */
87 OUT_RING(ring, info->start); /* VFD_INDEX_OFFSET */
88
89 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
90 OUT_RING(ring, info->primitive_restart ? /* PC_RESTART_INDEX */
91 info->restart_index : 0xffffffff);
92
93 fd_draw_emit(ctx, info);
94 }
95
96 static void
97 fd3_clear(struct fd_context *ctx, unsigned buffers,
98 const union pipe_color_union *color, double depth, unsigned stencil)
99 {
100 struct fd3_context *fd3_ctx = fd3_context(ctx);
101 struct fd_ringbuffer *ring = ctx->ring;
102 unsigned ce, i;
103
104 /* emit generic state now: */
105 fd3_emit_state(ctx, ctx->dirty & (FD_DIRTY_VIEWPORT |
106 FD_DIRTY_FRAMEBUFFER | FD_DIRTY_SCISSOR));
107
108 OUT_PKT0(ring, REG_A3XX_RB_BLEND_ALPHA, 1);
109 OUT_RING(ring, A3XX_RB_BLEND_ALPHA_UINT(0xff) |
110 A3XX_RB_BLEND_ALPHA_FLOAT(1.0));
111
112 fd3_emit_rbrc_draw_state(ctx, ring,
113 A3XX_RB_RENDER_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER));
114
115 if (buffers & PIPE_CLEAR_DEPTH) {
116 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
117 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
118 A3XX_RB_DEPTH_CONTROL_Z_ENABLE |
119 A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS));
120
121 OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_ZOFFSET, 2);
122 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZOFFSET(0.0));
123 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZSCALE(depth));
124 ctx->dirty |= FD_DIRTY_VIEWPORT;
125 } else {
126 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
127 OUT_RING(ring, A3XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
128 }
129
130 if (buffers & PIPE_CLEAR_STENCIL) {
131 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
132 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(stencil) |
133 A3XX_RB_STENCILREFMASK_STENCILMASK(stencil) |
134 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
135 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
136 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
137 0xff000000 | // XXX ???
138 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
139
140 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
141 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_STENCIL_ENABLE |
142 A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
143 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
144 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_REPLACE) |
145 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
146 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
147 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
148 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
149 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
150 } else {
151 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
152 OUT_RING(ring, A3XX_RB_STENCILREFMASK_STENCILREF(0) |
153 A3XX_RB_STENCILREFMASK_STENCILMASK(0) |
154 A3XX_RB_STENCILREFMASK_STENCILWRITEMASK(0));
155 OUT_RING(ring, A3XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
156 A3XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
157 A3XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0));
158
159 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
160 OUT_RING(ring, A3XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
161 A3XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
162 A3XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
163 A3XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
164 A3XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
165 A3XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
166 A3XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
167 A3XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
168 }
169
170 if (buffers & PIPE_CLEAR_COLOR) {
171 ce = 0xf;
172 } else {
173 ce = 0x0;
174 }
175
176 for (i = 0; i < 4; i++) {
177 OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
178 OUT_RING(ring, A3XX_RB_MRT_CONTROL_ROP_CODE(12) |
179 A3XX_RB_MRT_CONTROL_DITHER_MODE(DITHER_ALWAYS) |
180 A3XX_RB_MRT_CONTROL_COMPONENT_ENABLE(ce));
181
182 OUT_PKT0(ring, REG_A3XX_RB_MRT_BLEND_CONTROL(i), 1);
183 OUT_RING(ring, A3XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE) |
184 A3XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
185 A3XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO) |
186 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE) |
187 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
188 A3XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO) |
189 A3XX_RB_MRT_BLEND_CONTROL_CLAMP_ENABLE);
190 }
191
192 OUT_PKT0(ring, REG_A3XX_GRAS_SU_MODE_CONTROL, 1);
193 OUT_RING(ring, A3XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0));
194
195 fd3_program_emit(ring, &ctx->solid_prog);
196
197 fd3_emit_vertex_bufs(ring, &ctx->solid_prog, (struct fd3_vertex_buf[]) {
198 { .prsc = fd3_ctx->solid_vbuf, .stride = 12, .format = PIPE_FORMAT_R32G32B32_FLOAT },
199 }, 1);
200
201 fd3_emit_constant(ring, SB_FRAG_SHADER, 0, 0, 4, color->ui, NULL);
202
203 OUT_PKT0(ring, REG_A3XX_PC_PRIM_VTX_CNTL, 1);
204 OUT_RING(ring, A3XX_PC_PRIM_VTX_CNTL_STRIDE_IN_VPC(0) |
205 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_FRONT_PTYPE(PC_DRAW_TRIANGLES) |
206 A3XX_PC_PRIM_VTX_CNTL_POLYMODE_BACK_PTYPE(PC_DRAW_TRIANGLES) |
207 A3XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
208 OUT_PKT0(ring, REG_A3XX_VFD_INDEX_MIN, 4);
209 OUT_RING(ring, 0); /* VFD_INDEX_MIN */
210 OUT_RING(ring, 2); /* VFD_INDEX_MAX */
211 OUT_RING(ring, 0); /* VFD_INSTANCEID_OFFSET */
212 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
213 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
214 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
215
216 OUT_PKT3(ring, CP_EVENT_WRITE, 1);
217 OUT_RING(ring, PERFCOUNTER_STOP);
218
219 fd_draw(ctx, DI_PT_RECTLIST, DI_SRC_SEL_AUTO_INDEX, 2,
220 INDEX_SIZE_IGN, 0, 0, NULL);
221 }
222
223 void
224 fd3_draw_init(struct pipe_context *pctx)
225 {
226 struct fd_context *ctx = fd_context(pctx);
227 ctx->draw = fd3_draw;
228 ctx->clear = fd3_clear;
229 }