1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2012 Advanced Micro Devices, Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "r600_cs.h"
25bf215546Sopenharmony_ci#include "util/u_viewport.h"
26bf215546Sopenharmony_ci#include "tgsi/tgsi_scan.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#define R600_R_028C0C_PA_CL_GB_VERT_CLIP_ADJ         0x028C0C
29bf215546Sopenharmony_ci#define CM_R_028BE8_PA_CL_GB_VERT_CLIP_ADJ           0x28be8
30bf215546Sopenharmony_ci#define R_02843C_PA_CL_VPORT_XSCALE                  0x02843C
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#define R_028250_PA_SC_VPORT_SCISSOR_0_TL                               0x028250
33bf215546Sopenharmony_ci#define   S_028250_TL_X(x)                                            (((unsigned)(x) & 0x7FFF) << 0)
34bf215546Sopenharmony_ci#define   G_028250_TL_X(x)                                            (((x) >> 0) & 0x7FFF)
35bf215546Sopenharmony_ci#define   C_028250_TL_X                                               0xFFFF8000
36bf215546Sopenharmony_ci#define   S_028250_TL_Y(x)                                            (((unsigned)(x) & 0x7FFF) << 16)
37bf215546Sopenharmony_ci#define   G_028250_TL_Y(x)                                            (((x) >> 16) & 0x7FFF)
38bf215546Sopenharmony_ci#define   C_028250_TL_Y                                               0x8000FFFF
39bf215546Sopenharmony_ci#define   S_028250_WINDOW_OFFSET_DISABLE(x)                           (((unsigned)(x) & 0x1) << 31)
40bf215546Sopenharmony_ci#define   G_028250_WINDOW_OFFSET_DISABLE(x)                           (((x) >> 31) & 0x1)
41bf215546Sopenharmony_ci#define   C_028250_WINDOW_OFFSET_DISABLE                              0x7FFFFFFF
42bf215546Sopenharmony_ci#define   S_028254_BR_X(x)                                            (((unsigned)(x) & 0x7FFF) << 0)
43bf215546Sopenharmony_ci#define   G_028254_BR_X(x)                                            (((x) >> 0) & 0x7FFF)
44bf215546Sopenharmony_ci#define   C_028254_BR_X                                               0xFFFF8000
45bf215546Sopenharmony_ci#define   S_028254_BR_Y(x)                                            (((unsigned)(x) & 0x7FFF) << 16)
46bf215546Sopenharmony_ci#define   G_028254_BR_Y(x)                                            (((x) >> 16) & 0x7FFF)
47bf215546Sopenharmony_ci#define   C_028254_BR_Y                                               0x8000FFFF
48bf215546Sopenharmony_ci#define R_0282D0_PA_SC_VPORT_ZMIN_0                                     0x0282D0
49bf215546Sopenharmony_ci#define R_0282D4_PA_SC_VPORT_ZMAX_0                                     0x0282D4
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#define GET_MAX_SCISSOR(rctx) (rctx->gfx_level >= EVERGREEN ? 16384 : 8192)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void r600_set_scissor_states(struct pipe_context *ctx,
54bf215546Sopenharmony_ci				    unsigned start_slot,
55bf215546Sopenharmony_ci				    unsigned num_scissors,
56bf215546Sopenharmony_ci				    const struct pipe_scissor_state *state)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
59bf215546Sopenharmony_ci	int i;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci	for (i = 0; i < num_scissors; i++)
62bf215546Sopenharmony_ci		rctx->scissors.states[start_slot + i] = state[i];
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci	if (!rctx->scissor_enabled)
65bf215546Sopenharmony_ci		return;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci	rctx->scissors.dirty_mask |= ((1 << num_scissors) - 1) << start_slot;
68bf215546Sopenharmony_ci	rctx->set_atom_dirty(rctx, &rctx->scissors.atom, true);
69bf215546Sopenharmony_ci}
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci/* Since the guard band disables clipping, we have to clip per-pixel
72bf215546Sopenharmony_ci * using a scissor.
73bf215546Sopenharmony_ci */
74bf215546Sopenharmony_cistatic void r600_get_scissor_from_viewport(struct r600_common_context *rctx,
75bf215546Sopenharmony_ci					   const struct pipe_viewport_state *vp,
76bf215546Sopenharmony_ci					   struct r600_signed_scissor *scissor)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci	float tmp, minx, miny, maxx, maxy;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci	/* Convert (-1, -1) and (1, 1) from clip space into window space. */
81bf215546Sopenharmony_ci	minx = -vp->scale[0] + vp->translate[0];
82bf215546Sopenharmony_ci	miny = -vp->scale[1] + vp->translate[1];
83bf215546Sopenharmony_ci	maxx = vp->scale[0] + vp->translate[0];
84bf215546Sopenharmony_ci	maxy = vp->scale[1] + vp->translate[1];
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci	/* r600_draw_rectangle sets this. Disable the scissor. */
87bf215546Sopenharmony_ci	if (minx == -1 && miny == -1 && maxx == 1 && maxy == 1) {
88bf215546Sopenharmony_ci		scissor->minx = scissor->miny = 0;
89bf215546Sopenharmony_ci		scissor->maxx = scissor->maxy = GET_MAX_SCISSOR(rctx);
90bf215546Sopenharmony_ci		return;
91bf215546Sopenharmony_ci	}
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci	/* Handle inverted viewports. */
94bf215546Sopenharmony_ci	if (minx > maxx) {
95bf215546Sopenharmony_ci		tmp = minx;
96bf215546Sopenharmony_ci		minx = maxx;
97bf215546Sopenharmony_ci		maxx = tmp;
98bf215546Sopenharmony_ci	}
99bf215546Sopenharmony_ci	if (miny > maxy) {
100bf215546Sopenharmony_ci		tmp = miny;
101bf215546Sopenharmony_ci		miny = maxy;
102bf215546Sopenharmony_ci		maxy = tmp;
103bf215546Sopenharmony_ci	}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci	/* Convert to integer and round up the max bounds. */
106bf215546Sopenharmony_ci	scissor->minx = minx;
107bf215546Sopenharmony_ci	scissor->miny = miny;
108bf215546Sopenharmony_ci	scissor->maxx = ceilf(maxx);
109bf215546Sopenharmony_ci	scissor->maxy = ceilf(maxy);
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistatic void r600_clamp_scissor(struct r600_common_context *rctx,
113bf215546Sopenharmony_ci			       struct pipe_scissor_state *out,
114bf215546Sopenharmony_ci			       struct r600_signed_scissor *scissor)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci	unsigned max_scissor = GET_MAX_SCISSOR(rctx);
117bf215546Sopenharmony_ci	out->minx = CLAMP(scissor->minx, 0, max_scissor);
118bf215546Sopenharmony_ci	out->miny = CLAMP(scissor->miny, 0, max_scissor);
119bf215546Sopenharmony_ci	out->maxx = CLAMP(scissor->maxx, 0, max_scissor);
120bf215546Sopenharmony_ci	out->maxy = CLAMP(scissor->maxy, 0, max_scissor);
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistatic void r600_clip_scissor(struct pipe_scissor_state *out,
124bf215546Sopenharmony_ci			      struct pipe_scissor_state *clip)
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci	out->minx = MAX2(out->minx, clip->minx);
127bf215546Sopenharmony_ci	out->miny = MAX2(out->miny, clip->miny);
128bf215546Sopenharmony_ci	out->maxx = MIN2(out->maxx, clip->maxx);
129bf215546Sopenharmony_ci	out->maxy = MIN2(out->maxy, clip->maxy);
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_cistatic void r600_scissor_make_union(struct r600_signed_scissor *out,
133bf215546Sopenharmony_ci				    struct r600_signed_scissor *in)
134bf215546Sopenharmony_ci{
135bf215546Sopenharmony_ci	out->minx = MIN2(out->minx, in->minx);
136bf215546Sopenharmony_ci	out->miny = MIN2(out->miny, in->miny);
137bf215546Sopenharmony_ci	out->maxx = MAX2(out->maxx, in->maxx);
138bf215546Sopenharmony_ci	out->maxy = MAX2(out->maxy, in->maxy);
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_civoid evergreen_apply_scissor_bug_workaround(struct r600_common_context *rctx,
142bf215546Sopenharmony_ci					    struct pipe_scissor_state *scissor)
143bf215546Sopenharmony_ci{
144bf215546Sopenharmony_ci	if (rctx->gfx_level == EVERGREEN || rctx->gfx_level == CAYMAN) {
145bf215546Sopenharmony_ci		if (scissor->maxx == 0)
146bf215546Sopenharmony_ci			scissor->minx = 1;
147bf215546Sopenharmony_ci		if (scissor->maxy == 0)
148bf215546Sopenharmony_ci			scissor->miny = 1;
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci		if (rctx->gfx_level == CAYMAN &&
151bf215546Sopenharmony_ci		    scissor->maxx == 1 && scissor->maxy == 1)
152bf215546Sopenharmony_ci			scissor->maxx = 2;
153bf215546Sopenharmony_ci	}
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_cistatic void r600_emit_one_scissor(struct r600_common_context *rctx,
157bf215546Sopenharmony_ci				  struct radeon_cmdbuf *cs,
158bf215546Sopenharmony_ci				  struct r600_signed_scissor *vp_scissor,
159bf215546Sopenharmony_ci				  struct pipe_scissor_state *scissor)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci	struct pipe_scissor_state final;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci	if (rctx->vs_disables_clipping_viewport) {
164bf215546Sopenharmony_ci		final.minx = final.miny = 0;
165bf215546Sopenharmony_ci		final.maxx = final.maxy = GET_MAX_SCISSOR(rctx);
166bf215546Sopenharmony_ci	} else {
167bf215546Sopenharmony_ci		r600_clamp_scissor(rctx, &final, vp_scissor);
168bf215546Sopenharmony_ci	}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci	if (scissor)
171bf215546Sopenharmony_ci		r600_clip_scissor(&final, scissor);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci	evergreen_apply_scissor_bug_workaround(rctx, &final);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci	radeon_emit(cs, S_028250_TL_X(final.minx) |
176bf215546Sopenharmony_ci			S_028250_TL_Y(final.miny) |
177bf215546Sopenharmony_ci			S_028250_WINDOW_OFFSET_DISABLE(1));
178bf215546Sopenharmony_ci	radeon_emit(cs, S_028254_BR_X(final.maxx) |
179bf215546Sopenharmony_ci			S_028254_BR_Y(final.maxy));
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci/* the range is [-MAX, MAX] */
183bf215546Sopenharmony_ci#define GET_MAX_VIEWPORT_RANGE(rctx) (rctx->gfx_level >= EVERGREEN ? 32768 : 16384)
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_cistatic void r600_emit_guardband(struct r600_common_context *rctx,
186bf215546Sopenharmony_ci				struct r600_signed_scissor *vp_as_scissor)
187bf215546Sopenharmony_ci{
188bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->gfx.cs;
189bf215546Sopenharmony_ci	struct pipe_viewport_state vp;
190bf215546Sopenharmony_ci	float left, top, right, bottom, max_range, guardband_x, guardband_y;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci	/* Reconstruct the viewport transformation from the scissor. */
193bf215546Sopenharmony_ci	vp.translate[0] = (vp_as_scissor->minx + vp_as_scissor->maxx) / 2.0;
194bf215546Sopenharmony_ci	vp.translate[1] = (vp_as_scissor->miny + vp_as_scissor->maxy) / 2.0;
195bf215546Sopenharmony_ci	vp.scale[0] = vp_as_scissor->maxx - vp.translate[0];
196bf215546Sopenharmony_ci	vp.scale[1] = vp_as_scissor->maxy - vp.translate[1];
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci	/* Treat a 0x0 viewport as 1x1 to prevent division by zero. */
199bf215546Sopenharmony_ci	if (vp_as_scissor->minx == vp_as_scissor->maxx)
200bf215546Sopenharmony_ci		vp.scale[0] = 0.5;
201bf215546Sopenharmony_ci	if (vp_as_scissor->miny == vp_as_scissor->maxy)
202bf215546Sopenharmony_ci		vp.scale[1] = 0.5;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci	/* Find the biggest guard band that is inside the supported viewport
205bf215546Sopenharmony_ci	 * range. The guard band is specified as a horizontal and vertical
206bf215546Sopenharmony_ci	 * distance from (0,0) in clip space.
207bf215546Sopenharmony_ci	 *
208bf215546Sopenharmony_ci	 * This is done by applying the inverse viewport transformation
209bf215546Sopenharmony_ci	 * on the viewport limits to get those limits in clip space.
210bf215546Sopenharmony_ci	 *
211bf215546Sopenharmony_ci	 * Use a limit one pixel smaller to allow for some precision error.
212bf215546Sopenharmony_ci	 */
213bf215546Sopenharmony_ci	max_range = GET_MAX_VIEWPORT_RANGE(rctx) - 1;
214bf215546Sopenharmony_ci	left   = (-max_range - vp.translate[0]) / vp.scale[0];
215bf215546Sopenharmony_ci	right  = ( max_range - vp.translate[0]) / vp.scale[0];
216bf215546Sopenharmony_ci	top    = (-max_range - vp.translate[1]) / vp.scale[1];
217bf215546Sopenharmony_ci	bottom = ( max_range - vp.translate[1]) / vp.scale[1];
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci	assert(left <= -1 && top <= -1 && right >= 1 && bottom >= 1);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci	guardband_x = MIN2(-left, right);
222bf215546Sopenharmony_ci	guardband_y = MIN2(-top, bottom);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci	/* If any of the GB registers is updated, all of them must be updated. */
225bf215546Sopenharmony_ci	if (rctx->gfx_level >= CAYMAN)
226bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, CM_R_028BE8_PA_CL_GB_VERT_CLIP_ADJ, 4);
227bf215546Sopenharmony_ci	else
228bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R600_R_028C0C_PA_CL_GB_VERT_CLIP_ADJ, 4);
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci	radeon_emit(cs, fui(guardband_y)); /* R_028BE8_PA_CL_GB_VERT_CLIP_ADJ */
231bf215546Sopenharmony_ci	radeon_emit(cs, fui(1.0));         /* R_028BEC_PA_CL_GB_VERT_DISC_ADJ */
232bf215546Sopenharmony_ci	radeon_emit(cs, fui(guardband_x)); /* R_028BF0_PA_CL_GB_HORZ_CLIP_ADJ */
233bf215546Sopenharmony_ci	radeon_emit(cs, fui(1.0));         /* R_028BF4_PA_CL_GB_HORZ_DISC_ADJ */
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_cistatic void r600_emit_scissors(struct r600_common_context *rctx, struct r600_atom *atom)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->gfx.cs;
239bf215546Sopenharmony_ci	struct pipe_scissor_state *states = rctx->scissors.states;
240bf215546Sopenharmony_ci	unsigned mask = rctx->scissors.dirty_mask;
241bf215546Sopenharmony_ci	bool scissor_enabled = rctx->scissor_enabled;
242bf215546Sopenharmony_ci	struct r600_signed_scissor max_vp_scissor;
243bf215546Sopenharmony_ci	int i;
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci	/* The simple case: Only 1 viewport is active. */
246bf215546Sopenharmony_ci	if (!rctx->vs_writes_viewport_index) {
247bf215546Sopenharmony_ci		struct r600_signed_scissor *vp = &rctx->viewports.as_scissor[0];
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci		if (!(mask & 1))
250bf215546Sopenharmony_ci			return;
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_028250_PA_SC_VPORT_SCISSOR_0_TL, 2);
253bf215546Sopenharmony_ci		r600_emit_one_scissor(rctx, cs, vp, scissor_enabled ? &states[0] : NULL);
254bf215546Sopenharmony_ci		r600_emit_guardband(rctx, vp);
255bf215546Sopenharmony_ci		rctx->scissors.dirty_mask &= ~1; /* clear one bit */
256bf215546Sopenharmony_ci		return;
257bf215546Sopenharmony_ci	}
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci	/* Shaders can draw to any viewport. Make a union of all viewports. */
260bf215546Sopenharmony_ci	max_vp_scissor = rctx->viewports.as_scissor[0];
261bf215546Sopenharmony_ci	for (i = 1; i < R600_MAX_VIEWPORTS; i++)
262bf215546Sopenharmony_ci		r600_scissor_make_union(&max_vp_scissor,
263bf215546Sopenharmony_ci				      &rctx->viewports.as_scissor[i]);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci	while (mask) {
266bf215546Sopenharmony_ci		int start, count, i;
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci		u_bit_scan_consecutive_range(&mask, &start, &count);
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_028250_PA_SC_VPORT_SCISSOR_0_TL +
271bf215546Sopenharmony_ci					       start * 4 * 2, count * 2);
272bf215546Sopenharmony_ci		for (i = start; i < start+count; i++) {
273bf215546Sopenharmony_ci			r600_emit_one_scissor(rctx, cs, &rctx->viewports.as_scissor[i],
274bf215546Sopenharmony_ci					      scissor_enabled ? &states[i] : NULL);
275bf215546Sopenharmony_ci		}
276bf215546Sopenharmony_ci	}
277bf215546Sopenharmony_ci	r600_emit_guardband(rctx, &max_vp_scissor);
278bf215546Sopenharmony_ci	rctx->scissors.dirty_mask = 0;
279bf215546Sopenharmony_ci}
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_cistatic void r600_set_viewport_states(struct pipe_context *ctx,
282bf215546Sopenharmony_ci				     unsigned start_slot,
283bf215546Sopenharmony_ci				     unsigned num_viewports,
284bf215546Sopenharmony_ci				     const struct pipe_viewport_state *state)
285bf215546Sopenharmony_ci{
286bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
287bf215546Sopenharmony_ci	unsigned mask;
288bf215546Sopenharmony_ci	int i;
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci	for (i = 0; i < num_viewports; i++) {
291bf215546Sopenharmony_ci		unsigned index = start_slot + i;
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci		rctx->viewports.states[index] = state[i];
294bf215546Sopenharmony_ci		r600_get_scissor_from_viewport(rctx, &state[i],
295bf215546Sopenharmony_ci					       &rctx->viewports.as_scissor[index]);
296bf215546Sopenharmony_ci	}
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci	mask = ((1 << num_viewports) - 1) << start_slot;
299bf215546Sopenharmony_ci	rctx->viewports.dirty_mask |= mask;
300bf215546Sopenharmony_ci	rctx->viewports.depth_range_dirty_mask |= mask;
301bf215546Sopenharmony_ci	rctx->scissors.dirty_mask |= mask;
302bf215546Sopenharmony_ci	rctx->set_atom_dirty(rctx, &rctx->viewports.atom, true);
303bf215546Sopenharmony_ci	rctx->set_atom_dirty(rctx, &rctx->scissors.atom, true);
304bf215546Sopenharmony_ci}
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_cistatic void r600_emit_one_viewport(struct r600_common_context *rctx,
307bf215546Sopenharmony_ci				   struct pipe_viewport_state *state)
308bf215546Sopenharmony_ci{
309bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->gfx.cs;
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->scale[0]));
312bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->translate[0]));
313bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->scale[1]));
314bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->translate[1]));
315bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->scale[2]));
316bf215546Sopenharmony_ci	radeon_emit(cs, fui(state->translate[2]));
317bf215546Sopenharmony_ci}
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_cistatic void r600_emit_viewports(struct r600_common_context *rctx)
320bf215546Sopenharmony_ci{
321bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->gfx.cs;
322bf215546Sopenharmony_ci	struct pipe_viewport_state *states = rctx->viewports.states;
323bf215546Sopenharmony_ci	unsigned mask = rctx->viewports.dirty_mask;
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci	/* The simple case: Only 1 viewport is active. */
326bf215546Sopenharmony_ci	if (!rctx->vs_writes_viewport_index) {
327bf215546Sopenharmony_ci		if (!(mask & 1))
328bf215546Sopenharmony_ci			return;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_02843C_PA_CL_VPORT_XSCALE, 6);
331bf215546Sopenharmony_ci		r600_emit_one_viewport(rctx, &states[0]);
332bf215546Sopenharmony_ci		rctx->viewports.dirty_mask &= ~1; /* clear one bit */
333bf215546Sopenharmony_ci		return;
334bf215546Sopenharmony_ci	}
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci	while (mask) {
337bf215546Sopenharmony_ci		int start, count, i;
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci		u_bit_scan_consecutive_range(&mask, &start, &count);
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_02843C_PA_CL_VPORT_XSCALE +
342bf215546Sopenharmony_ci					       start * 4 * 6, count * 6);
343bf215546Sopenharmony_ci		for (i = start; i < start+count; i++)
344bf215546Sopenharmony_ci			r600_emit_one_viewport(rctx, &states[i]);
345bf215546Sopenharmony_ci	}
346bf215546Sopenharmony_ci	rctx->viewports.dirty_mask = 0;
347bf215546Sopenharmony_ci}
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_cistatic void r600_emit_depth_ranges(struct r600_common_context *rctx)
350bf215546Sopenharmony_ci{
351bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->gfx.cs;
352bf215546Sopenharmony_ci	struct pipe_viewport_state *states = rctx->viewports.states;
353bf215546Sopenharmony_ci	unsigned mask = rctx->viewports.depth_range_dirty_mask;
354bf215546Sopenharmony_ci	float zmin, zmax;
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci	/* The simple case: Only 1 viewport is active. */
357bf215546Sopenharmony_ci	if (!rctx->vs_writes_viewport_index) {
358bf215546Sopenharmony_ci		if (!(mask & 1))
359bf215546Sopenharmony_ci			return;
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci		util_viewport_zmin_zmax(&states[0], rctx->clip_halfz, &zmin, &zmax);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_0282D0_PA_SC_VPORT_ZMIN_0, 2);
364bf215546Sopenharmony_ci		radeon_emit(cs, fui(zmin));
365bf215546Sopenharmony_ci		radeon_emit(cs, fui(zmax));
366bf215546Sopenharmony_ci		rctx->viewports.depth_range_dirty_mask &= ~1; /* clear one bit */
367bf215546Sopenharmony_ci		return;
368bf215546Sopenharmony_ci	}
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci	while (mask) {
371bf215546Sopenharmony_ci		int start, count, i;
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci		u_bit_scan_consecutive_range(&mask, &start, &count);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci		radeon_set_context_reg_seq(cs, R_0282D0_PA_SC_VPORT_ZMIN_0 +
376bf215546Sopenharmony_ci					   start * 4 * 2, count * 2);
377bf215546Sopenharmony_ci		for (i = start; i < start+count; i++) {
378bf215546Sopenharmony_ci			util_viewport_zmin_zmax(&states[i], rctx->clip_halfz, &zmin, &zmax);
379bf215546Sopenharmony_ci			radeon_emit(cs, fui(zmin));
380bf215546Sopenharmony_ci			radeon_emit(cs, fui(zmax));
381bf215546Sopenharmony_ci		}
382bf215546Sopenharmony_ci	}
383bf215546Sopenharmony_ci	rctx->viewports.depth_range_dirty_mask = 0;
384bf215546Sopenharmony_ci}
385bf215546Sopenharmony_ci
386bf215546Sopenharmony_cistatic void r600_emit_viewport_states(struct r600_common_context *rctx,
387bf215546Sopenharmony_ci				      struct r600_atom *atom)
388bf215546Sopenharmony_ci{
389bf215546Sopenharmony_ci	r600_emit_viewports(rctx);
390bf215546Sopenharmony_ci	r600_emit_depth_ranges(rctx);
391bf215546Sopenharmony_ci}
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci/* Set viewport dependencies on pipe_rasterizer_state. */
394bf215546Sopenharmony_civoid r600_viewport_set_rast_deps(struct r600_common_context *rctx,
395bf215546Sopenharmony_ci				 bool scissor_enable, bool clip_halfz)
396bf215546Sopenharmony_ci{
397bf215546Sopenharmony_ci	if (rctx->scissor_enabled != scissor_enable) {
398bf215546Sopenharmony_ci		rctx->scissor_enabled = scissor_enable;
399bf215546Sopenharmony_ci		rctx->scissors.dirty_mask = (1 << R600_MAX_VIEWPORTS) - 1;
400bf215546Sopenharmony_ci		rctx->set_atom_dirty(rctx, &rctx->scissors.atom, true);
401bf215546Sopenharmony_ci	}
402bf215546Sopenharmony_ci	if (rctx->clip_halfz != clip_halfz) {
403bf215546Sopenharmony_ci		rctx->clip_halfz = clip_halfz;
404bf215546Sopenharmony_ci		rctx->viewports.depth_range_dirty_mask = (1 << R600_MAX_VIEWPORTS) - 1;
405bf215546Sopenharmony_ci		rctx->set_atom_dirty(rctx, &rctx->viewports.atom, true);
406bf215546Sopenharmony_ci	}
407bf215546Sopenharmony_ci}
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_ci/**
410bf215546Sopenharmony_ci * Normally, we only emit 1 viewport and 1 scissor if no shader is using
411bf215546Sopenharmony_ci * the VIEWPORT_INDEX output, and emitting the other viewports and scissors
412bf215546Sopenharmony_ci * is delayed. When a shader with VIEWPORT_INDEX appears, this should be
413bf215546Sopenharmony_ci * called to emit the rest.
414bf215546Sopenharmony_ci */
415bf215546Sopenharmony_civoid r600_update_vs_writes_viewport_index(struct r600_common_context *rctx,
416bf215546Sopenharmony_ci					  struct tgsi_shader_info *info)
417bf215546Sopenharmony_ci{
418bf215546Sopenharmony_ci	bool vs_window_space;
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci	if (!info)
421bf215546Sopenharmony_ci		return;
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci	/* When the VS disables clipping and viewport transformation. */
424bf215546Sopenharmony_ci	vs_window_space =
425bf215546Sopenharmony_ci		info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci	if (rctx->vs_disables_clipping_viewport != vs_window_space) {
428bf215546Sopenharmony_ci		rctx->vs_disables_clipping_viewport = vs_window_space;
429bf215546Sopenharmony_ci		rctx->scissors.dirty_mask = (1 << R600_MAX_VIEWPORTS) - 1;
430bf215546Sopenharmony_ci		rctx->set_atom_dirty(rctx, &rctx->scissors.atom, true);
431bf215546Sopenharmony_ci	}
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci	/* Viewport index handling. */
434bf215546Sopenharmony_ci	rctx->vs_writes_viewport_index = info->writes_viewport_index;
435bf215546Sopenharmony_ci	if (!rctx->vs_writes_viewport_index)
436bf215546Sopenharmony_ci		return;
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci	if (rctx->scissors.dirty_mask)
439bf215546Sopenharmony_ci	    rctx->set_atom_dirty(rctx, &rctx->scissors.atom, true);
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci	if (rctx->viewports.dirty_mask ||
442bf215546Sopenharmony_ci	    rctx->viewports.depth_range_dirty_mask)
443bf215546Sopenharmony_ci	    rctx->set_atom_dirty(rctx, &rctx->viewports.atom, true);
444bf215546Sopenharmony_ci}
445bf215546Sopenharmony_ci
446bf215546Sopenharmony_civoid r600_init_viewport_functions(struct r600_common_context *rctx)
447bf215546Sopenharmony_ci{
448bf215546Sopenharmony_ci	rctx->scissors.atom.emit = r600_emit_scissors;
449bf215546Sopenharmony_ci	rctx->viewports.atom.emit = r600_emit_viewport_states;
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci	rctx->scissors.atom.num_dw = (2 + 16 * 2) + 6;
452bf215546Sopenharmony_ci	rctx->viewports.atom.num_dw = 2 + 16 * 6;
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci	rctx->b.set_scissor_states = r600_set_scissor_states;
455bf215546Sopenharmony_ci	rctx->b.set_viewport_states = r600_set_viewport_states;
456bf215546Sopenharmony_ci}
457