1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_prim.h"
34 #include "freedreno_state.h"
35 #include "freedreno_resource.h"
38 #include "fd4_context.h"
40 #include "fd4_program.h"
41 #include "fd4_format.h"
46 draw_impl(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
47 struct fd4_emit
*emit
)
49 const struct pipe_draw_info
*info
= emit
->info
;
50 enum pc_di_primtype primtype
= ctx
->primtypes
[info
->mode
];
52 fd4_emit_state(ctx
, ring
, emit
);
54 if (emit
->dirty
& (FD_DIRTY_VTXBUF
| FD_DIRTY_VTXSTATE
))
55 fd4_emit_vertex_bufs(ring
, emit
);
57 OUT_PKT0(ring
, REG_A4XX_VFD_INDEX_OFFSET
, 2);
58 OUT_RING(ring
, info
->indexed
? info
->index_bias
: info
->start
); /* VFD_INDEX_OFFSET */
59 OUT_RING(ring
, info
->start_instance
); /* ??? UNKNOWN_2209 */
61 OUT_PKT0(ring
, REG_A4XX_PC_RESTART_INDEX
, 1);
62 OUT_RING(ring
, info
->primitive_restart
? /* PC_RESTART_INDEX */
63 info
->restart_index
: 0xffffffff);
65 /* points + psize -> spritelist: */
66 if (ctx
->rasterizer
->point_size_per_vertex
&&
67 fd4_emit_get_vp(emit
)->writes_psize
&&
68 (info
->mode
== PIPE_PRIM_POINTS
))
69 primtype
= DI_PT_POINTLIST_PSIZE
;
71 fd4_draw_emit(ctx
, ring
,
73 emit
->key
.binning_pass
? IGNORE_VISIBILITY
: USE_VISIBILITY
,
77 /* fixup dirty shader state in case some "unrelated" (from the state-
78 * tracker's perspective) state change causes us to switch to a
82 fixup_shader_state(struct fd_context
*ctx
, struct ir3_shader_key
*key
)
84 struct fd4_context
*fd4_ctx
= fd4_context(ctx
);
85 struct ir3_shader_key
*last_key
= &fd4_ctx
->last_key
;
87 if (!ir3_shader_key_equal(last_key
, key
)) {
88 if (last_key
->has_per_samp
|| key
->has_per_samp
) {
89 if ((last_key
->vsaturate_s
!= key
->vsaturate_s
) ||
90 (last_key
->vsaturate_t
!= key
->vsaturate_t
) ||
91 (last_key
->vsaturate_r
!= key
->vsaturate_r
) ||
92 (last_key
->vastc_srgb
!= key
->vastc_srgb
))
93 ctx
->dirty
|= FD_SHADER_DIRTY_VP
;
95 if ((last_key
->fsaturate_s
!= key
->fsaturate_s
) ||
96 (last_key
->fsaturate_t
!= key
->fsaturate_t
) ||
97 (last_key
->fsaturate_r
!= key
->fsaturate_r
) ||
98 (last_key
->fastc_srgb
!= key
->fastc_srgb
))
99 ctx
->dirty
|= FD_SHADER_DIRTY_FP
;
102 if (last_key
->vclamp_color
!= key
->vclamp_color
)
103 ctx
->dirty
|= FD_SHADER_DIRTY_VP
;
105 if (last_key
->fclamp_color
!= key
->fclamp_color
)
106 ctx
->dirty
|= FD_SHADER_DIRTY_FP
;
108 if (last_key
->color_two_side
!= key
->color_two_side
)
109 ctx
->dirty
|= FD_SHADER_DIRTY_FP
;
111 if (last_key
->half_precision
!= key
->half_precision
)
112 ctx
->dirty
|= FD_SHADER_DIRTY_FP
;
114 if (last_key
->rasterflat
!= key
->rasterflat
)
115 ctx
->dirty
|= FD_SHADER_DIRTY_FP
;
117 fd4_ctx
->last_key
= *key
;
122 fd4_draw_vbo(struct fd_context
*ctx
, const struct pipe_draw_info
*info
)
124 struct fd4_context
*fd4_ctx
= fd4_context(ctx
);
125 struct fd4_emit emit
= {
126 .debug
= &ctx
->debug
,
131 .color_two_side
= ctx
->rasterizer
->light_twoside
,
132 .vclamp_color
= ctx
->rasterizer
->clamp_vertex_color
,
133 .fclamp_color
= ctx
->rasterizer
->clamp_fragment_color
,
134 .rasterflat
= ctx
->rasterizer
->flatshade
,
135 // TODO set .half_precision based on render target format,
136 // ie. float16 and smaller use half, float32 use full..
137 .half_precision
= !!(fd_mesa_debug
& FD_DBG_FRAGHALF
),
138 .ucp_enables
= ctx
->rasterizer
->clip_plane_enable
,
139 .has_per_samp
= (fd4_ctx
->fsaturate
|| fd4_ctx
->vsaturate
||
140 fd4_ctx
->fastc_srgb
|| fd4_ctx
->vastc_srgb
),
141 .vsaturate_s
= fd4_ctx
->vsaturate_s
,
142 .vsaturate_t
= fd4_ctx
->vsaturate_t
,
143 .vsaturate_r
= fd4_ctx
->vsaturate_r
,
144 .fsaturate_s
= fd4_ctx
->fsaturate_s
,
145 .fsaturate_t
= fd4_ctx
->fsaturate_t
,
146 .fsaturate_r
= fd4_ctx
->fsaturate_r
,
147 .vastc_srgb
= fd4_ctx
->vastc_srgb
,
148 .fastc_srgb
= fd4_ctx
->fastc_srgb
,
150 .rasterflat
= ctx
->rasterizer
->flatshade
,
151 .sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
,
152 .sprite_coord_mode
= ctx
->rasterizer
->sprite_coord_mode
,
155 fixup_shader_state(ctx
, &emit
.key
);
157 unsigned dirty
= ctx
->dirty
;
159 /* do regular pass first, since that is more likely to fail compiling: */
161 if (!(fd4_emit_get_vp(&emit
) && fd4_emit_get_fp(&emit
)))
164 emit
.key
.binning_pass
= false;
167 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
169 if (ctx
->rasterizer
->rasterizer_discard
) {
171 OUT_PKT3(ring
, CP_REG_RMW
, 3);
172 OUT_RING(ring
, REG_A4XX_RB_RENDER_CONTROL
);
173 OUT_RING(ring
, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE
);
174 OUT_RING(ring
, A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE
);
177 draw_impl(ctx
, ctx
->batch
->draw
, &emit
);
179 if (ctx
->rasterizer
->rasterizer_discard
) {
181 OUT_PKT3(ring
, CP_REG_RMW
, 3);
182 OUT_RING(ring
, REG_A4XX_RB_RENDER_CONTROL
);
183 OUT_RING(ring
, ~A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE
);
187 /* and now binning pass: */
188 emit
.key
.binning_pass
= true;
189 emit
.dirty
= dirty
& ~(FD_DIRTY_BLEND
);
190 emit
.vp
= NULL
; /* we changed key so need to refetch vp */
192 draw_impl(ctx
, ctx
->batch
->binning
, &emit
);
197 /* clear operations ignore viewport state, so we need to reset it
198 * based on framebuffer state:
201 reset_viewport(struct fd_ringbuffer
*ring
, struct pipe_framebuffer_state
*pfb
)
203 float half_width
= pfb
->width
* 0.5f
;
204 float half_height
= pfb
->height
* 0.5f
;
206 OUT_PKT0(ring
, REG_A4XX_GRAS_CL_VPORT_XOFFSET_0
, 4);
207 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_XOFFSET_0(half_width
));
208 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_XSCALE_0(half_width
));
209 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_YOFFSET_0(half_height
));
210 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_YSCALE_0(-half_height
));
213 /* TODO maybe we should just migrate u_blitter for clear and do it in
214 * core (so we get normal draw pass state mgmt and binning).. That should
215 * work well enough for a3xx/a4xx (but maybe not a2xx?)
219 fd4_clear_binning(struct fd_context
*ctx
, unsigned dirty
)
221 struct fd4_context
*fd4_ctx
= fd4_context(ctx
);
222 struct fd_ringbuffer
*ring
= ctx
->batch
->binning
;
223 struct fd4_emit emit
= {
224 .debug
= &ctx
->debug
,
225 .vtx
= &fd4_ctx
->solid_vbuf_state
,
226 .prog
= &ctx
->solid_prog
,
228 .binning_pass
= true,
229 .half_precision
= true,
234 fd4_emit_state(ctx
, ring
, &emit
);
235 fd4_emit_vertex_bufs(ring
, &emit
);
236 reset_viewport(ring
, &ctx
->framebuffer
);
238 OUT_PKT0(ring
, REG_A4XX_PC_PRIM_VTX_CNTL
, 2);
239 OUT_RING(ring
, A4XX_PC_PRIM_VTX_CNTL_VAROUT(0) |
240 A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST
);
241 OUT_RING(ring
, A4XX_PC_PRIM_VTX_CNTL2_POLYMODE_FRONT_PTYPE(PC_DRAW_TRIANGLES
) |
242 A4XX_PC_PRIM_VTX_CNTL2_POLYMODE_BACK_PTYPE(PC_DRAW_TRIANGLES
));
244 OUT_PKT0(ring
, REG_A4XX_GRAS_ALPHA_CONTROL
, 1);
245 OUT_RING(ring
, 0x00000002);
247 fd4_draw(ctx
, ring
, DI_PT_RECTLIST
, IGNORE_VISIBILITY
,
248 DI_SRC_SEL_AUTO_INDEX
, 2, 1, INDEX_SIZE_IGN
, 0, 0, NULL
);
252 fd4_clear(struct fd_context
*ctx
, unsigned buffers
,
253 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
255 struct fd4_context
*fd4_ctx
= fd4_context(ctx
);
256 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
257 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
258 unsigned char mrt_comp
[A4XX_MAX_RENDER_TARGETS
] = {0};
259 unsigned dirty
= ctx
->dirty
;
261 struct fd4_emit emit
= {
262 .debug
= &ctx
->debug
,
263 .vtx
= &fd4_ctx
->solid_vbuf_state
,
264 .prog
= &ctx
->solid_prog
,
266 .half_precision
= fd_half_precision(pfb
),
270 dirty
&= FD_DIRTY_FRAMEBUFFER
| FD_DIRTY_SCISSOR
;
271 dirty
|= FD_DIRTY_PROG
;
274 fd4_clear_binning(ctx
, dirty
);
276 OUT_PKT0(ring
, REG_A4XX_PC_PRIM_VTX_CNTL
, 1);
277 OUT_RING(ring
, A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST
);
279 /* emit generic state now: */
280 fd4_emit_state(ctx
, ring
, &emit
);
281 reset_viewport(ring
, pfb
);
283 if (buffers
& PIPE_CLEAR_DEPTH
) {
284 OUT_PKT0(ring
, REG_A4XX_RB_DEPTH_CONTROL
, 1);
285 OUT_RING(ring
, A4XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE
|
286 A4XX_RB_DEPTH_CONTROL_Z_ENABLE
|
287 A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS
));
290 OUT_PKT0(ring
, REG_A4XX_GRAS_CL_VPORT_ZOFFSET_0
, 2);
291 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_ZOFFSET_0(0.0));
292 OUT_RING(ring
, A4XX_GRAS_CL_VPORT_ZSCALE_0(depth
));
293 ctx
->dirty
|= FD_DIRTY_VIEWPORT
;
295 OUT_PKT0(ring
, REG_A4XX_RB_DEPTH_CONTROL
, 1);
296 OUT_RING(ring
, A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER
));
299 if (buffers
& PIPE_CLEAR_STENCIL
) {
300 OUT_PKT0(ring
, REG_A4XX_RB_STENCILREFMASK
, 2);
301 OUT_RING(ring
, A4XX_RB_STENCILREFMASK_STENCILREF(stencil
) |
302 A4XX_RB_STENCILREFMASK_STENCILMASK(stencil
) |
303 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
304 OUT_RING(ring
, A4XX_RB_STENCILREFMASK_STENCILREF(0) |
305 A4XX_RB_STENCILREFMASK_STENCILMASK(0) |
306 0xff000000 | // XXX ???
307 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
309 OUT_PKT0(ring
, REG_A4XX_RB_STENCIL_CONTROL
, 2);
310 OUT_RING(ring
, A4XX_RB_STENCIL_CONTROL_STENCIL_ENABLE
|
311 A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS
) |
312 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP
) |
313 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_REPLACE
) |
314 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP
) |
315 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER
) |
316 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP
) |
317 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP
) |
318 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP
));
319 OUT_RING(ring
, A4XX_RB_STENCIL_CONTROL2_STENCIL_BUFFER
);
321 OUT_PKT0(ring
, REG_A4XX_RB_STENCILREFMASK
, 2);
322 OUT_RING(ring
, A4XX_RB_STENCILREFMASK_STENCILREF(0) |
323 A4XX_RB_STENCILREFMASK_STENCILMASK(0) |
324 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0));
325 OUT_RING(ring
, A4XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
326 A4XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
327 A4XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0));
329 OUT_PKT0(ring
, REG_A4XX_RB_STENCIL_CONTROL
, 2);
330 OUT_RING(ring
, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER
) |
331 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP
) |
332 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP
) |
333 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP
) |
334 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER
) |
335 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP
) |
336 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP
) |
337 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP
));
338 OUT_RING(ring
, 0x00000000); /* RB_STENCIL_CONTROL2 */
341 if (buffers
& PIPE_CLEAR_COLOR
) {
342 OUT_PKT0(ring
, REG_A4XX_RB_ALPHA_CONTROL
, 1);
343 OUT_RING(ring
, A4XX_RB_ALPHA_CONTROL_ALPHA_TEST_FUNC(FUNC_NEVER
));
346 for (i
= 0; i
< A4XX_MAX_RENDER_TARGETS
; i
++) {
347 mrt_comp
[i
] = (buffers
& (PIPE_CLEAR_COLOR0
<< i
)) ? 0xf : 0x0;
349 OUT_PKT0(ring
, REG_A4XX_RB_MRT_CONTROL(i
), 1);
350 OUT_RING(ring
, A4XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY
) |
351 A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
353 OUT_PKT0(ring
, REG_A4XX_RB_MRT_BLEND_CONTROL(i
), 1);
354 OUT_RING(ring
, A4XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE
) |
355 A4XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC
) |
356 A4XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO
) |
357 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE
) |
358 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC
) |
359 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO
));
362 OUT_PKT0(ring
, REG_A4XX_RB_RENDER_COMPONENTS
, 1);
363 OUT_RING(ring
, A4XX_RB_RENDER_COMPONENTS_RT0(mrt_comp
[0]) |
364 A4XX_RB_RENDER_COMPONENTS_RT1(mrt_comp
[1]) |
365 A4XX_RB_RENDER_COMPONENTS_RT2(mrt_comp
[2]) |
366 A4XX_RB_RENDER_COMPONENTS_RT3(mrt_comp
[3]) |
367 A4XX_RB_RENDER_COMPONENTS_RT4(mrt_comp
[4]) |
368 A4XX_RB_RENDER_COMPONENTS_RT5(mrt_comp
[5]) |
369 A4XX_RB_RENDER_COMPONENTS_RT6(mrt_comp
[6]) |
370 A4XX_RB_RENDER_COMPONENTS_RT7(mrt_comp
[7]));
372 fd4_emit_vertex_bufs(ring
, &emit
);
374 OUT_PKT0(ring
, REG_A4XX_GRAS_ALPHA_CONTROL
, 1);
375 OUT_RING(ring
, 0x0); /* XXX GRAS_ALPHA_CONTROL */
377 OUT_PKT0(ring
, REG_A4XX_GRAS_CLEAR_CNTL
, 1);
378 OUT_RING(ring
, 0x00000000);
380 /* until fastclear works: */
381 fd4_emit_const(ring
, SHADER_FRAGMENT
, 0, 0, 4, color
->ui
, NULL
);
383 OUT_PKT0(ring
, REG_A4XX_VFD_INDEX_OFFSET
, 2);
384 OUT_RING(ring
, 0); /* VFD_INDEX_OFFSET */
385 OUT_RING(ring
, 0); /* ??? UNKNOWN_2209 */
387 OUT_PKT0(ring
, REG_A4XX_PC_RESTART_INDEX
, 1);
388 OUT_RING(ring
, 0xffffffff); /* PC_RESTART_INDEX */
390 OUT_PKT3(ring
, CP_UNKNOWN_1A
, 1);
391 OUT_RING(ring
, 0x00000001);
393 fd4_draw(ctx
, ring
, DI_PT_RECTLIST
, USE_VISIBILITY
,
394 DI_SRC_SEL_AUTO_INDEX
, 2, 1, INDEX_SIZE_IGN
, 0, 0, NULL
);
396 OUT_PKT3(ring
, CP_UNKNOWN_1A
, 1);
397 OUT_RING(ring
, 0x00000000);
399 OUT_PKT0(ring
, REG_A4XX_GRAS_CLEAR_CNTL
, 1);
400 OUT_RING(ring
, A4XX_GRAS_CLEAR_CNTL_NOT_FASTCLEAR
);
402 OUT_PKT0(ring
, REG_A4XX_GRAS_SC_CONTROL
, 1);
403 OUT_RING(ring
, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS
) |
404 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE
|
405 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE
) |
406 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
410 fd4_draw_init(struct pipe_context
*pctx
)
412 struct fd_context
*ctx
= fd_context(pctx
);
413 ctx
->draw_vbo
= fd4_draw_vbo
;
414 ctx
->clear
= fd4_clear
;