1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3bf215546Sopenharmony_ci * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "draw/draw_context.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/u_framebuffer.h"
27bf215546Sopenharmony_ci#include "util/half_float.h"
28bf215546Sopenharmony_ci#include "util/u_helpers.h"
29bf215546Sopenharmony_ci#include "util/u_math.h"
30bf215546Sopenharmony_ci#include "util/u_memory.h"
31bf215546Sopenharmony_ci#include "util/u_pack_color.h"
32bf215546Sopenharmony_ci#include "util/u_transfer.h"
33bf215546Sopenharmony_ci#include "util/u_blend.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "pipe/p_config.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "r300_cb.h"
40bf215546Sopenharmony_ci#include "r300_context.h"
41bf215546Sopenharmony_ci#include "r300_emit.h"
42bf215546Sopenharmony_ci#include "r300_reg.h"
43bf215546Sopenharmony_ci#include "r300_screen.h"
44bf215546Sopenharmony_ci#include "r300_screen_buffer.h"
45bf215546Sopenharmony_ci#include "r300_state_inlines.h"
46bf215546Sopenharmony_ci#include "r300_fs.h"
47bf215546Sopenharmony_ci#include "r300_texture.h"
48bf215546Sopenharmony_ci#include "r300_vs.h"
49bf215546Sopenharmony_ci#include "nir.h"
50bf215546Sopenharmony_ci#include "nir/nir_to_tgsi.h"
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci/* r300_state: Functions used to initialize state context by translating
53bf215546Sopenharmony_ci * Gallium state objects into semi-native r300 state objects. */
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci#define UPDATE_STATE(cso, atom) \
56bf215546Sopenharmony_ci    if (cso != atom.state) { \
57bf215546Sopenharmony_ci        atom.state = cso;    \
58bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &(atom));   \
59bf215546Sopenharmony_ci    }
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_cistatic boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
62bf215546Sopenharmony_ci                                            unsigned dstRGB, unsigned dstA)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
65bf215546Sopenharmony_ci     * SRC_ALPHA == 0, and the following state is set, the colorbuffer
66bf215546Sopenharmony_ci     * will not be changed.
67bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
68bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
69bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
70bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
71bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
72bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
73bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
74bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_ZERO) &&
75bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
76bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
77bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
78bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
79bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_ONE);
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistatic boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
83bf215546Sopenharmony_ci                                            unsigned dstRGB, unsigned dstA)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
86bf215546Sopenharmony_ci     * SRC_ALPHA == 1, and the following state is set, the colorbuffer
87bf215546Sopenharmony_ci     * will not be changed.
88bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
89bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
90bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
91bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
92bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
93bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_ZERO) &&
94bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
95bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
96bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
97bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
98bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_ONE);
99bf215546Sopenharmony_ci}
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_cistatic boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
102bf215546Sopenharmony_ci                                            unsigned dstRGB, unsigned dstA)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
105bf215546Sopenharmony_ci     * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
106bf215546Sopenharmony_ci     * will not be changed.
107bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
108bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
109bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
110bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_ZERO) &&
111bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
112bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
113bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_ONE);
114bf215546Sopenharmony_ci}
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
117bf215546Sopenharmony_ci                                            unsigned dstRGB, unsigned dstA)
118bf215546Sopenharmony_ci{
119bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
120bf215546Sopenharmony_ci     * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
121bf215546Sopenharmony_ci     * will not be changed.
122bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
123bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
124bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
125bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_ZERO) &&
126bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
127bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
128bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_ONE);
129bf215546Sopenharmony_ci}
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_cistatic boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
132bf215546Sopenharmony_ci                                                  unsigned dstRGB, unsigned dstA)
133bf215546Sopenharmony_ci{
134bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
135bf215546Sopenharmony_ci     * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
136bf215546Sopenharmony_ci     * the colorbuffer will not be changed.
137bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
138bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
139bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
140bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
141bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
142bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
143bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
144bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
145bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_ZERO) &&
146bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
147bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
148bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
149bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
150bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
151bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_ONE);
152bf215546Sopenharmony_ci}
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_cistatic boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
155bf215546Sopenharmony_ci                                                  unsigned dstRGB, unsigned dstA)
156bf215546Sopenharmony_ci{
157bf215546Sopenharmony_ci    /* If the blend equation is ADD or REVERSE_SUBTRACT,
158bf215546Sopenharmony_ci     * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
159bf215546Sopenharmony_ci     * the colorbuffer will not be changed.
160bf215546Sopenharmony_ci     * Notice that the dst factors are the src factors inverted. */
161bf215546Sopenharmony_ci    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
162bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
163bf215546Sopenharmony_ci            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
164bf215546Sopenharmony_ci           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
165bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
166bf215546Sopenharmony_ci            srcA == PIPE_BLENDFACTOR_ZERO) &&
167bf215546Sopenharmony_ci           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
168bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
169bf215546Sopenharmony_ci            dstRGB == PIPE_BLENDFACTOR_ONE) &&
170bf215546Sopenharmony_ci           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
171bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
172bf215546Sopenharmony_ci            dstA == PIPE_BLENDFACTOR_ONE);
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_cistatic unsigned blend_discard_conditionally(unsigned eqRGB, unsigned eqA,
176bf215546Sopenharmony_ci                                            unsigned dstRGB, unsigned dstA,
177bf215546Sopenharmony_ci                                            unsigned srcRGB, unsigned srcA)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci    unsigned blend_control = 0;
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci    /* Optimization: discard pixels which don't change the colorbuffer.
182bf215546Sopenharmony_ci     *
183bf215546Sopenharmony_ci     * The code below is non-trivial and some math is involved.
184bf215546Sopenharmony_ci     *
185bf215546Sopenharmony_ci     * Discarding pixels must be disabled when FP16 AA is enabled.
186bf215546Sopenharmony_ci     * This is a hardware bug. Also, this implementation wouldn't work
187bf215546Sopenharmony_ci     * with FP blending enabled and equation clamping disabled.
188bf215546Sopenharmony_ci     *
189bf215546Sopenharmony_ci     * Equations other than ADD are rarely used and therefore won't be
190bf215546Sopenharmony_ci     * optimized. */
191bf215546Sopenharmony_ci    if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
192bf215546Sopenharmony_ci        (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
193bf215546Sopenharmony_ci        /* ADD: X+Y
194bf215546Sopenharmony_ci         * REVERSE_SUBTRACT: Y-X
195bf215546Sopenharmony_ci         *
196bf215546Sopenharmony_ci         * The idea is:
197bf215546Sopenharmony_ci         * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
198bf215546Sopenharmony_ci         * then CB will not be changed.
199bf215546Sopenharmony_ci         *
200bf215546Sopenharmony_ci         * Given the srcFactor and dstFactor variables, we can derive
201bf215546Sopenharmony_ci         * what src and dst should be equal to and discard appropriate
202bf215546Sopenharmony_ci         * pixels.
203bf215546Sopenharmony_ci         */
204bf215546Sopenharmony_ci        if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
205bf215546Sopenharmony_ci            blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
206bf215546Sopenharmony_ci        } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
207bf215546Sopenharmony_ci                                                dstRGB, dstA)) {
208bf215546Sopenharmony_ci            blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
209bf215546Sopenharmony_ci        } else if (blend_discard_if_src_color_0(srcRGB, srcA,
210bf215546Sopenharmony_ci                                                dstRGB, dstA)) {
211bf215546Sopenharmony_ci            blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
212bf215546Sopenharmony_ci        } else if (blend_discard_if_src_color_1(srcRGB, srcA,
213bf215546Sopenharmony_ci                                                dstRGB, dstA)) {
214bf215546Sopenharmony_ci            blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
215bf215546Sopenharmony_ci        } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
216bf215546Sopenharmony_ci                                                      dstRGB, dstA)) {
217bf215546Sopenharmony_ci            blend_control |=
218bf215546Sopenharmony_ci                R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
219bf215546Sopenharmony_ci        } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
220bf215546Sopenharmony_ci                                                      dstRGB, dstA)) {
221bf215546Sopenharmony_ci            blend_control |=
222bf215546Sopenharmony_ci                R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
223bf215546Sopenharmony_ci        }
224bf215546Sopenharmony_ci    }
225bf215546Sopenharmony_ci    return blend_control;
226bf215546Sopenharmony_ci}
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci/* The hardware colormask is clunky a must be swizzled depending on the format.
229bf215546Sopenharmony_ci * This was figured out by trial-and-error. */
230bf215546Sopenharmony_cistatic unsigned bgra_cmask(unsigned mask)
231bf215546Sopenharmony_ci{
232bf215546Sopenharmony_ci    return ((mask & PIPE_MASK_R) << 2) |
233bf215546Sopenharmony_ci           ((mask & PIPE_MASK_B) >> 2) |
234bf215546Sopenharmony_ci           (mask & (PIPE_MASK_G | PIPE_MASK_A));
235bf215546Sopenharmony_ci}
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_cistatic unsigned rgba_cmask(unsigned mask)
238bf215546Sopenharmony_ci{
239bf215546Sopenharmony_ci    return mask & PIPE_MASK_RGBA;
240bf215546Sopenharmony_ci}
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_cistatic unsigned rrrr_cmask(unsigned mask)
243bf215546Sopenharmony_ci{
244bf215546Sopenharmony_ci    return (mask & PIPE_MASK_R) |
245bf215546Sopenharmony_ci           ((mask & PIPE_MASK_R) << 1) |
246bf215546Sopenharmony_ci           ((mask & PIPE_MASK_R) << 2) |
247bf215546Sopenharmony_ci           ((mask & PIPE_MASK_R) << 3);
248bf215546Sopenharmony_ci}
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_cistatic unsigned aaaa_cmask(unsigned mask)
251bf215546Sopenharmony_ci{
252bf215546Sopenharmony_ci    return ((mask & PIPE_MASK_A) >> 3) |
253bf215546Sopenharmony_ci           ((mask & PIPE_MASK_A) >> 2) |
254bf215546Sopenharmony_ci           ((mask & PIPE_MASK_A) >> 1) |
255bf215546Sopenharmony_ci           (mask & PIPE_MASK_A);
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_cistatic unsigned grrg_cmask(unsigned mask)
259bf215546Sopenharmony_ci{
260bf215546Sopenharmony_ci    return ((mask & PIPE_MASK_R) << 1) |
261bf215546Sopenharmony_ci           ((mask & PIPE_MASK_R) << 2) |
262bf215546Sopenharmony_ci           ((mask & PIPE_MASK_G) >> 1) |
263bf215546Sopenharmony_ci           ((mask & PIPE_MASK_G) << 2);
264bf215546Sopenharmony_ci}
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_cistatic unsigned arra_cmask(unsigned mask)
267bf215546Sopenharmony_ci{
268bf215546Sopenharmony_ci    return ((mask & PIPE_MASK_R) << 1) |
269bf215546Sopenharmony_ci           ((mask & PIPE_MASK_R) << 2) |
270bf215546Sopenharmony_ci           ((mask & PIPE_MASK_A) >> 3) |
271bf215546Sopenharmony_ci           (mask & PIPE_MASK_A);
272bf215546Sopenharmony_ci}
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_cistatic unsigned blend_read_enable(unsigned eqRGB, unsigned eqA,
275bf215546Sopenharmony_ci                                  unsigned dstRGB, unsigned dstA,
276bf215546Sopenharmony_ci                                  unsigned srcRGB, unsigned srcA,
277bf215546Sopenharmony_ci                                  boolean src_alpha_optz)
278bf215546Sopenharmony_ci{
279bf215546Sopenharmony_ci    unsigned blend_control = 0;
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci    /* Optimization: some operations do not require the destination color.
282bf215546Sopenharmony_ci     *
283bf215546Sopenharmony_ci     * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
284bf215546Sopenharmony_ci     * otherwise blending gives incorrect results. It seems to be
285bf215546Sopenharmony_ci     * a hardware bug. */
286bf215546Sopenharmony_ci    if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
287bf215546Sopenharmony_ci        eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
288bf215546Sopenharmony_ci        dstRGB != PIPE_BLENDFACTOR_ZERO ||
289bf215546Sopenharmony_ci        dstA != PIPE_BLENDFACTOR_ZERO ||
290bf215546Sopenharmony_ci        util_blend_factor_uses_dest(srcRGB, false) ||
291bf215546Sopenharmony_ci        util_blend_factor_uses_dest(srcA, true)) {
292bf215546Sopenharmony_ci        /* Enable reading from the colorbuffer. */
293bf215546Sopenharmony_ci        blend_control |= R300_READ_ENABLE;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci        if (src_alpha_optz) {
296bf215546Sopenharmony_ci            /* Optimization: Depending on incoming pixels, we can
297bf215546Sopenharmony_ci             * conditionally disable the reading in hardware... */
298bf215546Sopenharmony_ci            if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
299bf215546Sopenharmony_ci                eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
300bf215546Sopenharmony_ci                /* Disable reading if SRC_ALPHA == 0. */
301bf215546Sopenharmony_ci                if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
302bf215546Sopenharmony_ci                     dstRGB == PIPE_BLENDFACTOR_ZERO) &&
303bf215546Sopenharmony_ci                    (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
304bf215546Sopenharmony_ci                     dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
305bf215546Sopenharmony_ci                     dstA == PIPE_BLENDFACTOR_ZERO) &&
306bf215546Sopenharmony_ci                    (srcRGB != PIPE_BLENDFACTOR_DST_COLOR &&
307bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_DST_ALPHA &&
308bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_INV_DST_COLOR &&
309bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_INV_DST_ALPHA)) {
310bf215546Sopenharmony_ci                     blend_control |= R500_SRC_ALPHA_0_NO_READ;
311bf215546Sopenharmony_ci                }
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci                /* Disable reading if SRC_ALPHA == 1. */
314bf215546Sopenharmony_ci                if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
315bf215546Sopenharmony_ci                     dstRGB == PIPE_BLENDFACTOR_ZERO) &&
316bf215546Sopenharmony_ci                    (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
317bf215546Sopenharmony_ci                     dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
318bf215546Sopenharmony_ci                     dstA == PIPE_BLENDFACTOR_ZERO) &&
319bf215546Sopenharmony_ci                    (srcRGB != PIPE_BLENDFACTOR_DST_COLOR &&
320bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_DST_ALPHA &&
321bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_INV_DST_COLOR &&
322bf215546Sopenharmony_ci                     srcRGB != PIPE_BLENDFACTOR_INV_DST_ALPHA)) {
323bf215546Sopenharmony_ci                     blend_control |= R500_SRC_ALPHA_1_NO_READ;
324bf215546Sopenharmony_ci                }
325bf215546Sopenharmony_ci            }
326bf215546Sopenharmony_ci        }
327bf215546Sopenharmony_ci    }
328bf215546Sopenharmony_ci    return blend_control;
329bf215546Sopenharmony_ci}
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci/* Create a new blend state based on the CSO blend state.
332bf215546Sopenharmony_ci *
333bf215546Sopenharmony_ci * This encompasses alpha blending, logic/raster ops, and blend dithering. */
334bf215546Sopenharmony_cistatic void* r300_create_blend_state(struct pipe_context* pipe,
335bf215546Sopenharmony_ci                                     const struct pipe_blend_state* state)
336bf215546Sopenharmony_ci{
337bf215546Sopenharmony_ci    struct r300_screen* r300screen = r300_screen(pipe->screen);
338bf215546Sopenharmony_ci    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
339bf215546Sopenharmony_ci    uint32_t blend_control = 0;       /* R300_RB3D_CBLEND: 0x4e04 */
340bf215546Sopenharmony_ci    uint32_t blend_control_noclamp = 0;    /* R300_RB3D_CBLEND: 0x4e04 */
341bf215546Sopenharmony_ci    uint32_t blend_control_noalpha = 0;    /* R300_RB3D_CBLEND: 0x4e04 */
342bf215546Sopenharmony_ci    uint32_t blend_control_noalpha_noclamp = 0;    /* R300_RB3D_CBLEND: 0x4e04 */
343bf215546Sopenharmony_ci    uint32_t alpha_blend_control = 0; /* R300_RB3D_ABLEND: 0x4e08 */
344bf215546Sopenharmony_ci    uint32_t alpha_blend_control_noclamp = 0; /* R300_RB3D_ABLEND: 0x4e08 */
345bf215546Sopenharmony_ci    uint32_t alpha_blend_control_noalpha = 0; /* R300_RB3D_ABLEND: 0x4e08 */
346bf215546Sopenharmony_ci    uint32_t alpha_blend_control_noalpha_noclamp = 0; /* R300_RB3D_ABLEND: 0x4e08 */
347bf215546Sopenharmony_ci    uint32_t rop = 0;                 /* R300_RB3D_ROPCNTL: 0x4e18 */
348bf215546Sopenharmony_ci    uint32_t dither = 0;              /* R300_RB3D_DITHER_CTL: 0x4e50 */
349bf215546Sopenharmony_ci    int i;
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci    const unsigned eqRGB = state->rt[0].rgb_func;
352bf215546Sopenharmony_ci    const unsigned srcRGB = state->rt[0].rgb_src_factor;
353bf215546Sopenharmony_ci    const unsigned dstRGB = state->rt[0].rgb_dst_factor;
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci    const unsigned eqA = state->rt[0].alpha_func;
356bf215546Sopenharmony_ci    const unsigned srcA = state->rt[0].alpha_src_factor;
357bf215546Sopenharmony_ci    const unsigned dstA = state->rt[0].alpha_dst_factor;
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci    unsigned srcRGBX = srcRGB;
360bf215546Sopenharmony_ci    unsigned dstRGBX = dstRGB;
361bf215546Sopenharmony_ci    CB_LOCALS;
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci    blend->state = *state;
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci    /* force DST_ALPHA to ONE where we can */
366bf215546Sopenharmony_ci    switch (srcRGBX) {
367bf215546Sopenharmony_ci    case PIPE_BLENDFACTOR_DST_ALPHA:
368bf215546Sopenharmony_ci        srcRGBX = PIPE_BLENDFACTOR_ONE;
369bf215546Sopenharmony_ci        break;
370bf215546Sopenharmony_ci    case PIPE_BLENDFACTOR_INV_DST_ALPHA:
371bf215546Sopenharmony_ci        srcRGBX = PIPE_BLENDFACTOR_ZERO;
372bf215546Sopenharmony_ci        break;
373bf215546Sopenharmony_ci    }
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci    switch (dstRGBX) {
376bf215546Sopenharmony_ci    case PIPE_BLENDFACTOR_DST_ALPHA:
377bf215546Sopenharmony_ci        dstRGBX = PIPE_BLENDFACTOR_ONE;
378bf215546Sopenharmony_ci        break;
379bf215546Sopenharmony_ci    case PIPE_BLENDFACTOR_INV_DST_ALPHA:
380bf215546Sopenharmony_ci        dstRGBX = PIPE_BLENDFACTOR_ZERO;
381bf215546Sopenharmony_ci        break;
382bf215546Sopenharmony_ci    }
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci    /* Get blending register values. */
385bf215546Sopenharmony_ci    if (state->rt[0].blend_enable) {
386bf215546Sopenharmony_ci        unsigned blend_eq, blend_eq_noclamp;
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci        /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
389bf215546Sopenharmony_ci         * this is just the crappy D3D naming */
390bf215546Sopenharmony_ci        blend_control = blend_control_noclamp =
391bf215546Sopenharmony_ci            R300_ALPHA_BLEND_ENABLE |
392bf215546Sopenharmony_ci            ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
393bf215546Sopenharmony_ci            ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci        blend_control_noalpha = blend_control_noalpha_noclamp =
396bf215546Sopenharmony_ci            R300_ALPHA_BLEND_ENABLE |
397bf215546Sopenharmony_ci            ( r300_translate_blend_factor(srcRGBX) << R300_SRC_BLEND_SHIFT) |
398bf215546Sopenharmony_ci            ( r300_translate_blend_factor(dstRGBX) << R300_DST_BLEND_SHIFT);
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci        blend_eq = r300_translate_blend_function(eqRGB, TRUE);
401bf215546Sopenharmony_ci        blend_eq_noclamp = r300_translate_blend_function(eqRGB, FALSE);
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci        blend_control |= blend_eq;
404bf215546Sopenharmony_ci        blend_control_noalpha |= blend_eq;
405bf215546Sopenharmony_ci        blend_control_noclamp |= blend_eq_noclamp;
406bf215546Sopenharmony_ci        blend_control_noalpha_noclamp |= blend_eq_noclamp;
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci        /* Optimization: some operations do not require the destination color. */
409bf215546Sopenharmony_ci        blend_control |= blend_read_enable(eqRGB, eqA, dstRGB, dstA,
410bf215546Sopenharmony_ci                                           srcRGB, srcA, r300screen->caps.is_r500);
411bf215546Sopenharmony_ci        blend_control_noclamp |= blend_read_enable(eqRGB, eqA, dstRGB, dstA,
412bf215546Sopenharmony_ci                                                   srcRGB, srcA, FALSE);
413bf215546Sopenharmony_ci        blend_control_noalpha |= blend_read_enable(eqRGB, eqA, dstRGBX, dstA,
414bf215546Sopenharmony_ci                                                   srcRGBX, srcA, r300screen->caps.is_r500);
415bf215546Sopenharmony_ci        blend_control_noalpha_noclamp |= blend_read_enable(eqRGB, eqA, dstRGBX, dstA,
416bf215546Sopenharmony_ci                                                           srcRGBX, srcA, FALSE);
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci        /* Optimization: discard pixels which don't change the colorbuffer.
419bf215546Sopenharmony_ci         * It cannot be used with FP16 AA. */
420bf215546Sopenharmony_ci        blend_control |= blend_discard_conditionally(eqRGB, eqA, dstRGB, dstA,
421bf215546Sopenharmony_ci                                                     srcRGB, srcA);
422bf215546Sopenharmony_ci        blend_control_noalpha |= blend_discard_conditionally(eqRGB, eqA, dstRGBX, dstA,
423bf215546Sopenharmony_ci                                                             srcRGBX, srcA);
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci        /* separate alpha */
426bf215546Sopenharmony_ci        if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
427bf215546Sopenharmony_ci            blend_control |= R300_SEPARATE_ALPHA_ENABLE;
428bf215546Sopenharmony_ci            blend_control_noclamp |= R300_SEPARATE_ALPHA_ENABLE;
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci            alpha_blend_control = alpha_blend_control_noclamp =
431bf215546Sopenharmony_ci                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
432bf215546Sopenharmony_ci                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
433bf215546Sopenharmony_ci            alpha_blend_control |= r300_translate_blend_function(eqA, TRUE);
434bf215546Sopenharmony_ci            alpha_blend_control_noclamp |= r300_translate_blend_function(eqA, FALSE);
435bf215546Sopenharmony_ci        }
436bf215546Sopenharmony_ci        if (srcA != srcRGBX || dstA != dstRGBX || eqA != eqRGB) {
437bf215546Sopenharmony_ci            blend_control_noalpha |= R300_SEPARATE_ALPHA_ENABLE;
438bf215546Sopenharmony_ci            blend_control_noalpha_noclamp |= R300_SEPARATE_ALPHA_ENABLE;
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci            alpha_blend_control_noalpha = alpha_blend_control_noalpha_noclamp =
441bf215546Sopenharmony_ci                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
442bf215546Sopenharmony_ci                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
443bf215546Sopenharmony_ci            alpha_blend_control_noalpha |= r300_translate_blend_function(eqA, TRUE);
444bf215546Sopenharmony_ci            alpha_blend_control_noalpha_noclamp |= r300_translate_blend_function(eqA, FALSE);
445bf215546Sopenharmony_ci        }
446bf215546Sopenharmony_ci    }
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
449bf215546Sopenharmony_ci    if (state->logicop_enable) {
450bf215546Sopenharmony_ci        rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
451bf215546Sopenharmony_ci                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
452bf215546Sopenharmony_ci    }
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci    /* Neither fglrx nor classic r300 ever set this, regardless of dithering
455bf215546Sopenharmony_ci     * state. Since it's an optional implementation detail, we can leave it
456bf215546Sopenharmony_ci     * out and never dither.
457bf215546Sopenharmony_ci     *
458bf215546Sopenharmony_ci     * This could be revisited if we ever get quality or conformance hints.
459bf215546Sopenharmony_ci     *
460bf215546Sopenharmony_ci    if (state->dither) {
461bf215546Sopenharmony_ci        dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
462bf215546Sopenharmony_ci                        R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
463bf215546Sopenharmony_ci    }
464bf215546Sopenharmony_ci    */
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci    /* Build a command buffer. */
467bf215546Sopenharmony_ci    {
468bf215546Sopenharmony_ci        unsigned (*func[COLORMASK_NUM_SWIZZLES])(unsigned) = {
469bf215546Sopenharmony_ci            bgra_cmask,
470bf215546Sopenharmony_ci            rgba_cmask,
471bf215546Sopenharmony_ci            rrrr_cmask,
472bf215546Sopenharmony_ci            aaaa_cmask,
473bf215546Sopenharmony_ci            grrg_cmask,
474bf215546Sopenharmony_ci            arra_cmask,
475bf215546Sopenharmony_ci            bgra_cmask,
476bf215546Sopenharmony_ci            rgba_cmask
477bf215546Sopenharmony_ci        };
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci        for (i = 0; i < COLORMASK_NUM_SWIZZLES; i++) {
480bf215546Sopenharmony_ci            boolean has_alpha = i != COLORMASK_RGBX && i != COLORMASK_BGRX;
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_ci            BEGIN_CB(blend->cb_clamp[i], 8);
483bf215546Sopenharmony_ci            OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
484bf215546Sopenharmony_ci            OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
485bf215546Sopenharmony_ci            OUT_CB(has_alpha ? blend_control : blend_control_noalpha);
486bf215546Sopenharmony_ci            OUT_CB(has_alpha ? alpha_blend_control : alpha_blend_control_noalpha);
487bf215546Sopenharmony_ci            OUT_CB(func[i](state->rt[0].colormask));
488bf215546Sopenharmony_ci            OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
489bf215546Sopenharmony_ci            END_CB;
490bf215546Sopenharmony_ci        }
491bf215546Sopenharmony_ci    }
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci    /* Build a command buffer (for RGBA16F). */
494bf215546Sopenharmony_ci    BEGIN_CB(blend->cb_noclamp, 8);
495bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
496bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
497bf215546Sopenharmony_ci    OUT_CB(blend_control_noclamp);
498bf215546Sopenharmony_ci    OUT_CB(alpha_blend_control_noclamp);
499bf215546Sopenharmony_ci    OUT_CB(rgba_cmask(state->rt[0].colormask));
500bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
501bf215546Sopenharmony_ci    END_CB;
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci    /* Build a command buffer (for RGB16F). */
504bf215546Sopenharmony_ci    BEGIN_CB(blend->cb_noclamp_noalpha, 8);
505bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
506bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
507bf215546Sopenharmony_ci    OUT_CB(blend_control_noalpha_noclamp);
508bf215546Sopenharmony_ci    OUT_CB(alpha_blend_control_noalpha_noclamp);
509bf215546Sopenharmony_ci    OUT_CB(rgba_cmask(state->rt[0].colormask));
510bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
511bf215546Sopenharmony_ci    END_CB;
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ci    /* The same as above, but with no colorbuffer reads and writes. */
514bf215546Sopenharmony_ci    BEGIN_CB(blend->cb_no_readwrite, 8);
515bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
516bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
517bf215546Sopenharmony_ci    OUT_CB(0);
518bf215546Sopenharmony_ci    OUT_CB(0);
519bf215546Sopenharmony_ci    OUT_CB(0);
520bf215546Sopenharmony_ci    OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
521bf215546Sopenharmony_ci    END_CB;
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci    return (void*)blend;
524bf215546Sopenharmony_ci}
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci/* Bind blend state. */
527bf215546Sopenharmony_cistatic void r300_bind_blend_state(struct pipe_context* pipe,
528bf215546Sopenharmony_ci                                  void* state)
529bf215546Sopenharmony_ci{
530bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
531bf215546Sopenharmony_ci    struct r300_blend_state *blend  = (struct r300_blend_state*)state;
532bf215546Sopenharmony_ci    boolean last_alpha_to_one = r300->alpha_to_one;
533bf215546Sopenharmony_ci    boolean last_alpha_to_coverage = r300->alpha_to_coverage;
534bf215546Sopenharmony_ci
535bf215546Sopenharmony_ci    UPDATE_STATE(state, r300->blend_state);
536bf215546Sopenharmony_ci
537bf215546Sopenharmony_ci    if (!blend)
538bf215546Sopenharmony_ci        return;
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci    r300->alpha_to_one = blend->state.alpha_to_one;
541bf215546Sopenharmony_ci    r300->alpha_to_coverage = blend->state.alpha_to_coverage;
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci    if (r300->alpha_to_one != last_alpha_to_one && r300->msaa_enable &&
544bf215546Sopenharmony_ci        r300->fs_status == FRAGMENT_SHADER_VALID) {
545bf215546Sopenharmony_ci        r300->fs_status = FRAGMENT_SHADER_MAYBE_DIRTY;
546bf215546Sopenharmony_ci    }
547bf215546Sopenharmony_ci
548bf215546Sopenharmony_ci    if (r300->alpha_to_coverage != last_alpha_to_coverage &&
549bf215546Sopenharmony_ci        r300->msaa_enable) {
550bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->dsa_state);
551bf215546Sopenharmony_ci    }
552bf215546Sopenharmony_ci}
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci/* Free blend state. */
555bf215546Sopenharmony_cistatic void r300_delete_blend_state(struct pipe_context* pipe,
556bf215546Sopenharmony_ci                                    void* state)
557bf215546Sopenharmony_ci{
558bf215546Sopenharmony_ci    FREE(state);
559bf215546Sopenharmony_ci}
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_ci/* Convert float to 10bit integer */
562bf215546Sopenharmony_cistatic unsigned float_to_fixed10(float f)
563bf215546Sopenharmony_ci{
564bf215546Sopenharmony_ci    return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
565bf215546Sopenharmony_ci}
566bf215546Sopenharmony_ci
567bf215546Sopenharmony_ci/* Set blend color.
568bf215546Sopenharmony_ci * Setup both R300 and R500 registers, figure out later which one to write. */
569bf215546Sopenharmony_cistatic void r300_set_blend_color(struct pipe_context* pipe,
570bf215546Sopenharmony_ci                                 const struct pipe_blend_color* color)
571bf215546Sopenharmony_ci{
572bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
573bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb = r300->fb_state.state;
574bf215546Sopenharmony_ci    struct r300_blend_color_state *state =
575bf215546Sopenharmony_ci        (struct r300_blend_color_state*)r300->blend_color_state.state;
576bf215546Sopenharmony_ci    struct pipe_blend_color c;
577bf215546Sopenharmony_ci    struct pipe_surface *cb;
578bf215546Sopenharmony_ci    float tmp;
579bf215546Sopenharmony_ci    CB_LOCALS;
580bf215546Sopenharmony_ci
581bf215546Sopenharmony_ci    state->state = *color; /* Save it, so that we can reuse it in set_fb_state */
582bf215546Sopenharmony_ci    c = *color;
583bf215546Sopenharmony_ci    cb = fb->nr_cbufs ? r300_get_nonnull_cb(fb, 0) : NULL;
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci    /* The blend color is dependent on the colorbuffer format. */
586bf215546Sopenharmony_ci    if (cb) {
587bf215546Sopenharmony_ci        switch (cb->format) {
588bf215546Sopenharmony_ci        case PIPE_FORMAT_R8_UNORM:
589bf215546Sopenharmony_ci        case PIPE_FORMAT_L8_UNORM:
590bf215546Sopenharmony_ci        case PIPE_FORMAT_I8_UNORM:
591bf215546Sopenharmony_ci            c.color[1] = c.color[0];
592bf215546Sopenharmony_ci            break;
593bf215546Sopenharmony_ci
594bf215546Sopenharmony_ci        case PIPE_FORMAT_A8_UNORM:
595bf215546Sopenharmony_ci            c.color[1] = c.color[3];
596bf215546Sopenharmony_ci            break;
597bf215546Sopenharmony_ci
598bf215546Sopenharmony_ci        case PIPE_FORMAT_R8G8_UNORM:
599bf215546Sopenharmony_ci            c.color[2] = c.color[1];
600bf215546Sopenharmony_ci            break;
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci        case PIPE_FORMAT_L8A8_UNORM:
603bf215546Sopenharmony_ci        case PIPE_FORMAT_R8A8_UNORM:
604bf215546Sopenharmony_ci            c.color[2] = c.color[3];
605bf215546Sopenharmony_ci            break;
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_ci        case PIPE_FORMAT_R8G8B8A8_UNORM:
608bf215546Sopenharmony_ci        case PIPE_FORMAT_R8G8B8X8_UNORM:
609bf215546Sopenharmony_ci            tmp = c.color[0];
610bf215546Sopenharmony_ci            c.color[0] = c.color[2];
611bf215546Sopenharmony_ci            c.color[2] = tmp;
612bf215546Sopenharmony_ci            break;
613bf215546Sopenharmony_ci
614bf215546Sopenharmony_ci        default:;
615bf215546Sopenharmony_ci        }
616bf215546Sopenharmony_ci    }
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci    if (r300->screen->caps.is_r500) {
619bf215546Sopenharmony_ci        BEGIN_CB(state->cb, 3);
620bf215546Sopenharmony_ci        OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci        switch (cb ? cb->format : 0) {
623bf215546Sopenharmony_ci        case PIPE_FORMAT_R16G16B16A16_FLOAT:
624bf215546Sopenharmony_ci        case PIPE_FORMAT_R16G16B16X16_FLOAT:
625bf215546Sopenharmony_ci            OUT_CB(_mesa_float_to_half(c.color[2]) |
626bf215546Sopenharmony_ci                   (_mesa_float_to_half(c.color[3]) << 16));
627bf215546Sopenharmony_ci            OUT_CB(_mesa_float_to_half(c.color[0]) |
628bf215546Sopenharmony_ci                   (_mesa_float_to_half(c.color[1]) << 16));
629bf215546Sopenharmony_ci            break;
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci        default:
632bf215546Sopenharmony_ci            OUT_CB(float_to_fixed10(c.color[0]) |
633bf215546Sopenharmony_ci                   (float_to_fixed10(c.color[3]) << 16));
634bf215546Sopenharmony_ci            OUT_CB(float_to_fixed10(c.color[2]) |
635bf215546Sopenharmony_ci                   (float_to_fixed10(c.color[1]) << 16));
636bf215546Sopenharmony_ci        }
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci        END_CB;
639bf215546Sopenharmony_ci    } else {
640bf215546Sopenharmony_ci        union util_color uc;
641bf215546Sopenharmony_ci        util_pack_color(c.color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci        BEGIN_CB(state->cb, 2);
644bf215546Sopenharmony_ci        OUT_CB_REG(R300_RB3D_BLEND_COLOR, uc.ui[0]);
645bf215546Sopenharmony_ci        END_CB;
646bf215546Sopenharmony_ci    }
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->blend_color_state);
649bf215546Sopenharmony_ci}
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_cistatic void r300_set_clip_state(struct pipe_context* pipe,
652bf215546Sopenharmony_ci                                const struct pipe_clip_state* state)
653bf215546Sopenharmony_ci{
654bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
655bf215546Sopenharmony_ci    struct r300_clip_state *clip =
656bf215546Sopenharmony_ci            (struct r300_clip_state*)r300->clip_state.state;
657bf215546Sopenharmony_ci    CB_LOCALS;
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl) {
660bf215546Sopenharmony_ci        BEGIN_CB(clip->cb, r300->clip_state.size);
661bf215546Sopenharmony_ci        OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG,
662bf215546Sopenharmony_ci                   (r300->screen->caps.is_r500 ?
663bf215546Sopenharmony_ci                    R500_PVS_UCP_START : R300_PVS_UCP_START));
664bf215546Sopenharmony_ci        OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, 6 * 4);
665bf215546Sopenharmony_ci        OUT_CB_TABLE(state->ucp, 6 * 4);
666bf215546Sopenharmony_ci        END_CB;
667bf215546Sopenharmony_ci
668bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->clip_state);
669bf215546Sopenharmony_ci    } else {
670bf215546Sopenharmony_ci        draw_set_clip_state(r300->draw, state);
671bf215546Sopenharmony_ci    }
672bf215546Sopenharmony_ci}
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
675bf215546Sopenharmony_ci *
676bf215546Sopenharmony_ci * This contains the depth buffer, stencil buffer, alpha test, and such.
677bf215546Sopenharmony_ci * On the Radeon, depth and stencil buffer setup are intertwined, which is
678bf215546Sopenharmony_ci * the reason for some of the strange-looking assignments across registers. */
679bf215546Sopenharmony_cistatic void* r300_create_dsa_state(struct pipe_context* pipe,
680bf215546Sopenharmony_ci                          const struct pipe_depth_stencil_alpha_state* state)
681bf215546Sopenharmony_ci{
682bf215546Sopenharmony_ci    boolean is_r500 = r300_screen(pipe->screen)->caps.is_r500;
683bf215546Sopenharmony_ci    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
684bf215546Sopenharmony_ci    CB_LOCALS;
685bf215546Sopenharmony_ci    uint32_t alpha_value_fp16 = 0;
686bf215546Sopenharmony_ci    uint32_t z_buffer_control = 0;
687bf215546Sopenharmony_ci    uint32_t z_stencil_control = 0;
688bf215546Sopenharmony_ci    uint32_t stencil_ref_mask = 0;
689bf215546Sopenharmony_ci    uint32_t stencil_ref_bf = 0;
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci    dsa->dsa = *state;
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci    /* Depth test setup. - separate write mask depth for decomp flush */
694bf215546Sopenharmony_ci    if (state->depth_writemask) {
695bf215546Sopenharmony_ci        z_buffer_control |= R300_Z_WRITE_ENABLE;
696bf215546Sopenharmony_ci    }
697bf215546Sopenharmony_ci
698bf215546Sopenharmony_ci    if (state->depth_enabled) {
699bf215546Sopenharmony_ci        z_buffer_control |= R300_Z_ENABLE;
700bf215546Sopenharmony_ci
701bf215546Sopenharmony_ci        z_stencil_control |=
702bf215546Sopenharmony_ci            (r300_translate_depth_stencil_function(state->depth_func) <<
703bf215546Sopenharmony_ci                R300_Z_FUNC_SHIFT);
704bf215546Sopenharmony_ci    }
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci    /* Stencil buffer setup. */
707bf215546Sopenharmony_ci    if (state->stencil[0].enabled) {
708bf215546Sopenharmony_ci        z_buffer_control |= R300_STENCIL_ENABLE;
709bf215546Sopenharmony_ci        z_stencil_control |=
710bf215546Sopenharmony_ci            (r300_translate_depth_stencil_function(state->stencil[0].func) <<
711bf215546Sopenharmony_ci                R300_S_FRONT_FUNC_SHIFT) |
712bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[0].fail_op) <<
713bf215546Sopenharmony_ci                R300_S_FRONT_SFAIL_OP_SHIFT) |
714bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
715bf215546Sopenharmony_ci                R300_S_FRONT_ZPASS_OP_SHIFT) |
716bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
717bf215546Sopenharmony_ci                R300_S_FRONT_ZFAIL_OP_SHIFT);
718bf215546Sopenharmony_ci
719bf215546Sopenharmony_ci        stencil_ref_mask =
720bf215546Sopenharmony_ci                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
721bf215546Sopenharmony_ci                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
722bf215546Sopenharmony_ci
723bf215546Sopenharmony_ci        if (state->stencil[1].enabled) {
724bf215546Sopenharmony_ci            dsa->two_sided = TRUE;
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci            z_buffer_control |= R300_STENCIL_FRONT_BACK;
727bf215546Sopenharmony_ci            z_stencil_control |=
728bf215546Sopenharmony_ci            (r300_translate_depth_stencil_function(state->stencil[1].func) <<
729bf215546Sopenharmony_ci                R300_S_BACK_FUNC_SHIFT) |
730bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[1].fail_op) <<
731bf215546Sopenharmony_ci                R300_S_BACK_SFAIL_OP_SHIFT) |
732bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
733bf215546Sopenharmony_ci                R300_S_BACK_ZPASS_OP_SHIFT) |
734bf215546Sopenharmony_ci            (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
735bf215546Sopenharmony_ci                R300_S_BACK_ZFAIL_OP_SHIFT);
736bf215546Sopenharmony_ci
737bf215546Sopenharmony_ci            stencil_ref_bf =
738bf215546Sopenharmony_ci                (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
739bf215546Sopenharmony_ci                (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci            if (is_r500) {
742bf215546Sopenharmony_ci                z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
743bf215546Sopenharmony_ci            } else {
744bf215546Sopenharmony_ci                dsa->two_sided_stencil_ref =
745bf215546Sopenharmony_ci                  (state->stencil[0].valuemask != state->stencil[1].valuemask ||
746bf215546Sopenharmony_ci                   state->stencil[0].writemask != state->stencil[1].writemask);
747bf215546Sopenharmony_ci            }
748bf215546Sopenharmony_ci        }
749bf215546Sopenharmony_ci    }
750bf215546Sopenharmony_ci
751bf215546Sopenharmony_ci    /* Alpha test setup. */
752bf215546Sopenharmony_ci    if (state->alpha_enabled) {
753bf215546Sopenharmony_ci        dsa->alpha_function =
754bf215546Sopenharmony_ci            r300_translate_alpha_function(state->alpha_func) |
755bf215546Sopenharmony_ci            R300_FG_ALPHA_FUNC_ENABLE;
756bf215546Sopenharmony_ci
757bf215546Sopenharmony_ci        dsa->alpha_function |= float_to_ubyte(state->alpha_ref_value);
758bf215546Sopenharmony_ci        alpha_value_fp16 = _mesa_float_to_half(state->alpha_ref_value);
759bf215546Sopenharmony_ci    }
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_ci    BEGIN_CB(&dsa->cb_begin, 8);
762bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_ZB_CNTL, 3);
763bf215546Sopenharmony_ci    OUT_CB(z_buffer_control);
764bf215546Sopenharmony_ci    OUT_CB(z_stencil_control);
765bf215546Sopenharmony_ci    OUT_CB(stencil_ref_mask);
766bf215546Sopenharmony_ci    OUT_CB_REG(R500_ZB_STENCILREFMASK_BF, stencil_ref_bf);
767bf215546Sopenharmony_ci    OUT_CB_REG(R500_FG_ALPHA_VALUE, alpha_value_fp16);
768bf215546Sopenharmony_ci    END_CB;
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci    BEGIN_CB(dsa->cb_zb_no_readwrite, 8);
771bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_ZB_CNTL, 3);
772bf215546Sopenharmony_ci    OUT_CB(0);
773bf215546Sopenharmony_ci    OUT_CB(0);
774bf215546Sopenharmony_ci    OUT_CB(0);
775bf215546Sopenharmony_ci    OUT_CB_REG(R500_ZB_STENCILREFMASK_BF, 0);
776bf215546Sopenharmony_ci    OUT_CB_REG(R500_FG_ALPHA_VALUE, alpha_value_fp16);
777bf215546Sopenharmony_ci    END_CB;
778bf215546Sopenharmony_ci
779bf215546Sopenharmony_ci    return (void*)dsa;
780bf215546Sopenharmony_ci}
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_cistatic void r300_dsa_inject_stencilref(struct r300_context *r300)
783bf215546Sopenharmony_ci{
784bf215546Sopenharmony_ci    struct r300_dsa_state *dsa =
785bf215546Sopenharmony_ci            (struct r300_dsa_state*)r300->dsa_state.state;
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci    if (!dsa)
788bf215546Sopenharmony_ci        return;
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci    dsa->stencil_ref_mask =
791bf215546Sopenharmony_ci        (dsa->stencil_ref_mask & ~R300_STENCILREF_MASK) |
792bf215546Sopenharmony_ci        r300->stencil_ref.ref_value[0];
793bf215546Sopenharmony_ci    dsa->stencil_ref_bf =
794bf215546Sopenharmony_ci        (dsa->stencil_ref_bf & ~R300_STENCILREF_MASK) |
795bf215546Sopenharmony_ci        r300->stencil_ref.ref_value[1];
796bf215546Sopenharmony_ci}
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci/* Bind DSA state. */
799bf215546Sopenharmony_cistatic void r300_bind_dsa_state(struct pipe_context* pipe,
800bf215546Sopenharmony_ci                                void* state)
801bf215546Sopenharmony_ci{
802bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci    if (!state) {
805bf215546Sopenharmony_ci        return;
806bf215546Sopenharmony_ci    }
807bf215546Sopenharmony_ci
808bf215546Sopenharmony_ci    UPDATE_STATE(state, r300->dsa_state);
809bf215546Sopenharmony_ci
810bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->hyperz_state); /* Will be updated before the emission. */
811bf215546Sopenharmony_ci    r300_dsa_inject_stencilref(r300);
812bf215546Sopenharmony_ci}
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci/* Free DSA state. */
815bf215546Sopenharmony_cistatic void r300_delete_dsa_state(struct pipe_context* pipe,
816bf215546Sopenharmony_ci                                  void* state)
817bf215546Sopenharmony_ci{
818bf215546Sopenharmony_ci    FREE(state);
819bf215546Sopenharmony_ci}
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_cistatic void r300_set_stencil_ref(struct pipe_context* pipe,
822bf215546Sopenharmony_ci                                 const struct pipe_stencil_ref sr)
823bf215546Sopenharmony_ci{
824bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci    r300->stencil_ref = sr;
827bf215546Sopenharmony_ci
828bf215546Sopenharmony_ci    r300_dsa_inject_stencilref(r300);
829bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->dsa_state);
830bf215546Sopenharmony_ci}
831bf215546Sopenharmony_ci
832bf215546Sopenharmony_cistatic void r300_print_fb_surf_info(struct pipe_surface *surf, unsigned index,
833bf215546Sopenharmony_ci                                    const char *binding)
834bf215546Sopenharmony_ci{
835bf215546Sopenharmony_ci    struct pipe_resource *tex = surf->texture;
836bf215546Sopenharmony_ci    struct r300_resource *rtex = r300_resource(tex);
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci    fprintf(stderr,
839bf215546Sopenharmony_ci            "r300:   %s[%i] Dim: %ix%i, Firstlayer: %i, "
840bf215546Sopenharmony_ci            "Lastlayer: %i, Level: %i, Format: %s\n"
841bf215546Sopenharmony_ci
842bf215546Sopenharmony_ci            "r300:     TEX: Macro: %s, Micro: %s, "
843bf215546Sopenharmony_ci            "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
844bf215546Sopenharmony_ci
845bf215546Sopenharmony_ci            binding, index, surf->width, surf->height,
846bf215546Sopenharmony_ci            surf->u.tex.first_layer, surf->u.tex.last_layer, surf->u.tex.level,
847bf215546Sopenharmony_ci            util_format_short_name(surf->format),
848bf215546Sopenharmony_ci
849bf215546Sopenharmony_ci            rtex->tex.macrotile[0] ? "YES" : " NO",
850bf215546Sopenharmony_ci            rtex->tex.microtile ? "YES" : " NO",
851bf215546Sopenharmony_ci            tex->width0, tex->height0, tex->depth0,
852bf215546Sopenharmony_ci            tex->last_level, util_format_short_name(surf->format));
853bf215546Sopenharmony_ci}
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_civoid r300_mark_fb_state_dirty(struct r300_context *r300,
856bf215546Sopenharmony_ci                              enum r300_fb_state_change change)
857bf215546Sopenharmony_ci{
858bf215546Sopenharmony_ci    struct pipe_framebuffer_state *state = r300->fb_state.state;
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->gpu_flush);
861bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->fb_state);
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_ci    /* What is marked as dirty depends on the enum r300_fb_state_change. */
864bf215546Sopenharmony_ci    if (change == R300_CHANGED_FB_STATE) {
865bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->aa_state);
866bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->dsa_state); /* for AlphaRef */
867bf215546Sopenharmony_ci        r300_set_blend_color(&r300->context, r300->blend_color_state.state);
868bf215546Sopenharmony_ci    }
869bf215546Sopenharmony_ci
870bf215546Sopenharmony_ci    if (change == R300_CHANGED_FB_STATE ||
871bf215546Sopenharmony_ci        change == R300_CHANGED_HYPERZ_FLAG) {
872bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->hyperz_state);
873bf215546Sopenharmony_ci    }
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_ci    if (change == R300_CHANGED_FB_STATE ||
876bf215546Sopenharmony_ci        change == R300_CHANGED_MULTIWRITE) {
877bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->fb_state_pipelined);
878bf215546Sopenharmony_ci    }
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci    /* Now compute the fb_state atom size. */
881bf215546Sopenharmony_ci    r300->fb_state.size = 2 + (8 * state->nr_cbufs);
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci    if (r300->cbzb_clear)
884bf215546Sopenharmony_ci        r300->fb_state.size += 10;
885bf215546Sopenharmony_ci    else if (state->zsbuf) {
886bf215546Sopenharmony_ci        r300->fb_state.size += 10;
887bf215546Sopenharmony_ci        if (r300->hyperz_enabled)
888bf215546Sopenharmony_ci            r300->fb_state.size += 8;
889bf215546Sopenharmony_ci    }
890bf215546Sopenharmony_ci
891bf215546Sopenharmony_ci    if (r300->cmask_in_use) {
892bf215546Sopenharmony_ci        r300->fb_state.size += 6;
893bf215546Sopenharmony_ci        if (r300->screen->caps.is_r500) {
894bf215546Sopenharmony_ci            r300->fb_state.size += 3;
895bf215546Sopenharmony_ci        }
896bf215546Sopenharmony_ci    }
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci    /* The size of the rest of atoms stays the same. */
899bf215546Sopenharmony_ci}
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_cistatic void
902bf215546Sopenharmony_cir300_set_framebuffer_state(struct pipe_context* pipe,
903bf215546Sopenharmony_ci                           const struct pipe_framebuffer_state* state)
904bf215546Sopenharmony_ci{
905bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
906bf215546Sopenharmony_ci    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
907bf215546Sopenharmony_ci    struct pipe_framebuffer_state *current_state = r300->fb_state.state;
908bf215546Sopenharmony_ci    unsigned max_width, max_height, i;
909bf215546Sopenharmony_ci    uint32_t zbuffer_bpp = 0;
910bf215546Sopenharmony_ci    boolean unlock_zbuffer = FALSE;
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_ci    if (r300->screen->caps.is_r500) {
913bf215546Sopenharmony_ci        max_width = max_height = 4096;
914bf215546Sopenharmony_ci    } else if (r300->screen->caps.is_r400) {
915bf215546Sopenharmony_ci        max_width = max_height = 4021;
916bf215546Sopenharmony_ci    } else {
917bf215546Sopenharmony_ci        max_width = max_height = 2560;
918bf215546Sopenharmony_ci    }
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci    if (state->width > max_width || state->height > max_height) {
921bf215546Sopenharmony_ci        fprintf(stderr, "r300: Implementation error: Render targets are too "
922bf215546Sopenharmony_ci        "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
923bf215546Sopenharmony_ci        return;
924bf215546Sopenharmony_ci    }
925bf215546Sopenharmony_ci
926bf215546Sopenharmony_ci    if (current_state->zsbuf && r300->zmask_in_use && !r300->locked_zbuffer) {
927bf215546Sopenharmony_ci        /* There is a zmask in use, what are we gonna do? */
928bf215546Sopenharmony_ci        if (state->zsbuf) {
929bf215546Sopenharmony_ci            if (!pipe_surface_equal(current_state->zsbuf, state->zsbuf)) {
930bf215546Sopenharmony_ci                /* Decompress the currently bound zbuffer before we bind another one. */
931bf215546Sopenharmony_ci                r300_decompress_zmask(r300);
932bf215546Sopenharmony_ci                r300->hiz_in_use = FALSE;
933bf215546Sopenharmony_ci            }
934bf215546Sopenharmony_ci        } else {
935bf215546Sopenharmony_ci            /* We don't bind another zbuffer, so lock the current one. */
936bf215546Sopenharmony_ci            pipe_surface_reference(&r300->locked_zbuffer, current_state->zsbuf);
937bf215546Sopenharmony_ci        }
938bf215546Sopenharmony_ci    } else if (r300->locked_zbuffer) {
939bf215546Sopenharmony_ci        /* We have a locked zbuffer now, what are we gonna do? */
940bf215546Sopenharmony_ci        if (state->zsbuf) {
941bf215546Sopenharmony_ci            if (!pipe_surface_equal(r300->locked_zbuffer, state->zsbuf)) {
942bf215546Sopenharmony_ci                /* We are binding some other zbuffer, so decompress the locked one,
943bf215546Sopenharmony_ci                 * it gets unlocked automatically. */
944bf215546Sopenharmony_ci                r300_decompress_zmask_locked_unsafe(r300);
945bf215546Sopenharmony_ci                r300->hiz_in_use = FALSE;
946bf215546Sopenharmony_ci            } else {
947bf215546Sopenharmony_ci                /* We are binding the locked zbuffer again, so unlock it. */
948bf215546Sopenharmony_ci                unlock_zbuffer = TRUE;
949bf215546Sopenharmony_ci            }
950bf215546Sopenharmony_ci        }
951bf215546Sopenharmony_ci    }
952bf215546Sopenharmony_ci    assert(state->zsbuf || (r300->locked_zbuffer && !unlock_zbuffer) || !r300->zmask_in_use);
953bf215546Sopenharmony_ci
954bf215546Sopenharmony_ci    /* If zsbuf is set from NULL to non-NULL or vice versa.. */
955bf215546Sopenharmony_ci    if (!!current_state->zsbuf != !!state->zsbuf) {
956bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->dsa_state);
957bf215546Sopenharmony_ci    }
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_ci    util_copy_framebuffer_state(r300->fb_state.state, state);
960bf215546Sopenharmony_ci
961bf215546Sopenharmony_ci    /* Remove trailing NULL colorbuffers. */
962bf215546Sopenharmony_ci    while (current_state->nr_cbufs && !current_state->cbufs[current_state->nr_cbufs-1])
963bf215546Sopenharmony_ci        current_state->nr_cbufs--;
964bf215546Sopenharmony_ci
965bf215546Sopenharmony_ci    /* Set whether CMASK can be used. */
966bf215546Sopenharmony_ci    r300->cmask_in_use =
967bf215546Sopenharmony_ci        state->nr_cbufs == 1 && state->cbufs[0] &&
968bf215546Sopenharmony_ci        r300->screen->cmask_resource == state->cbufs[0]->texture;
969bf215546Sopenharmony_ci
970bf215546Sopenharmony_ci    /* Need to reset clamping or colormask. */
971bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->blend_state);
972bf215546Sopenharmony_ci
973bf215546Sopenharmony_ci    /* Re-swizzle the blend color. */
974bf215546Sopenharmony_ci    r300_set_blend_color(pipe, &((struct r300_blend_color_state*)r300->blend_color_state.state)->state);
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci    if (unlock_zbuffer) {
977bf215546Sopenharmony_ci        pipe_surface_reference(&r300->locked_zbuffer, NULL);
978bf215546Sopenharmony_ci    }
979bf215546Sopenharmony_ci
980bf215546Sopenharmony_ci    r300_mark_fb_state_dirty(r300, R300_CHANGED_FB_STATE);
981bf215546Sopenharmony_ci
982bf215546Sopenharmony_ci    if (state->zsbuf) {
983bf215546Sopenharmony_ci        switch (util_format_get_blocksize(state->zsbuf->format)) {
984bf215546Sopenharmony_ci        case 2:
985bf215546Sopenharmony_ci            zbuffer_bpp = 16;
986bf215546Sopenharmony_ci            break;
987bf215546Sopenharmony_ci        case 4:
988bf215546Sopenharmony_ci            zbuffer_bpp = 24;
989bf215546Sopenharmony_ci            break;
990bf215546Sopenharmony_ci        }
991bf215546Sopenharmony_ci
992bf215546Sopenharmony_ci        /* Polygon offset depends on the zbuffer bit depth. */
993bf215546Sopenharmony_ci        if (r300->zbuffer_bpp != zbuffer_bpp) {
994bf215546Sopenharmony_ci            r300->zbuffer_bpp = zbuffer_bpp;
995bf215546Sopenharmony_ci
996bf215546Sopenharmony_ci            if (r300->polygon_offset_enabled)
997bf215546Sopenharmony_ci                r300_mark_atom_dirty(r300, &r300->rs_state);
998bf215546Sopenharmony_ci        }
999bf215546Sopenharmony_ci    }
1000bf215546Sopenharmony_ci
1001bf215546Sopenharmony_ci    r300->num_samples = util_framebuffer_get_num_samples(state);
1002bf215546Sopenharmony_ci
1003bf215546Sopenharmony_ci    /* Set up AA config. */
1004bf215546Sopenharmony_ci    if (r300->num_samples > 1) {
1005bf215546Sopenharmony_ci        switch (r300->num_samples) {
1006bf215546Sopenharmony_ci        case 2:
1007bf215546Sopenharmony_ci            aa->aa_config = R300_GB_AA_CONFIG_AA_ENABLE |
1008bf215546Sopenharmony_ci                            R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_2;
1009bf215546Sopenharmony_ci            break;
1010bf215546Sopenharmony_ci        case 4:
1011bf215546Sopenharmony_ci            aa->aa_config = R300_GB_AA_CONFIG_AA_ENABLE |
1012bf215546Sopenharmony_ci                            R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_4;
1013bf215546Sopenharmony_ci            break;
1014bf215546Sopenharmony_ci        case 6:
1015bf215546Sopenharmony_ci            aa->aa_config = R300_GB_AA_CONFIG_AA_ENABLE |
1016bf215546Sopenharmony_ci                            R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_6;
1017bf215546Sopenharmony_ci            break;
1018bf215546Sopenharmony_ci        }
1019bf215546Sopenharmony_ci    } else {
1020bf215546Sopenharmony_ci        aa->aa_config = 0;
1021bf215546Sopenharmony_ci    }
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci    if (DBG_ON(r300, DBG_FB)) {
1024bf215546Sopenharmony_ci        fprintf(stderr, "r300: set_framebuffer_state:\n");
1025bf215546Sopenharmony_ci        for (i = 0; i < state->nr_cbufs; i++) {
1026bf215546Sopenharmony_ci            if (state->cbufs[i])
1027bf215546Sopenharmony_ci                r300_print_fb_surf_info(state->cbufs[i], i, "CB");
1028bf215546Sopenharmony_ci        }
1029bf215546Sopenharmony_ci        if (state->zsbuf) {
1030bf215546Sopenharmony_ci            r300_print_fb_surf_info(state->zsbuf, 0, "ZB");
1031bf215546Sopenharmony_ci        }
1032bf215546Sopenharmony_ci    }
1033bf215546Sopenharmony_ci}
1034bf215546Sopenharmony_ci
1035bf215546Sopenharmony_ci/* Create fragment shader state. */
1036bf215546Sopenharmony_cistatic void* r300_create_fs_state(struct pipe_context* pipe,
1037bf215546Sopenharmony_ci                                  const struct pipe_shader_state* shader)
1038bf215546Sopenharmony_ci{
1039bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1040bf215546Sopenharmony_ci    struct r300_fragment_shader* fs = NULL;
1041bf215546Sopenharmony_ci
1042bf215546Sopenharmony_ci    fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
1043bf215546Sopenharmony_ci
1044bf215546Sopenharmony_ci    /* Copy state directly into shader. */
1045bf215546Sopenharmony_ci    fs->state = *shader;
1046bf215546Sopenharmony_ci
1047bf215546Sopenharmony_ci    if (fs->state.type == PIPE_SHADER_IR_NIR) {
1048bf215546Sopenharmony_ci       if (r300->screen->caps.is_r500)
1049bf215546Sopenharmony_ci           NIR_PASS_V(shader->ir.nir, r300_transform_fs_trig_input);
1050bf215546Sopenharmony_ci       fs->state.tokens = nir_to_tgsi(shader->ir.nir, pipe->screen);
1051bf215546Sopenharmony_ci    } else {
1052bf215546Sopenharmony_ci       assert(fs->state.type == PIPE_SHADER_IR_TGSI);
1053bf215546Sopenharmony_ci       /* we need to keep a local copy of the tokens */
1054bf215546Sopenharmony_ci       fs->state.tokens = tgsi_dup_tokens(fs->state.tokens);
1055bf215546Sopenharmony_ci    }
1056bf215546Sopenharmony_ci
1057bf215546Sopenharmony_ci    /* Precompile the fragment shader at creation time to avoid jank at runtime.
1058bf215546Sopenharmony_ci     * In most cases we won't have anything in the key at draw time.
1059bf215546Sopenharmony_ci     */
1060bf215546Sopenharmony_ci    struct r300_fragment_program_external_state precompile_state;
1061bf215546Sopenharmony_ci    memset(&precompile_state, 0, sizeof(precompile_state));
1062bf215546Sopenharmony_ci
1063bf215546Sopenharmony_ci    struct tgsi_shader_info info;
1064bf215546Sopenharmony_ci    tgsi_scan_shader(fs->state.tokens, &info);
1065bf215546Sopenharmony_ci    for (int i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
1066bf215546Sopenharmony_ci        if (info.sampler_targets[i] == TGSI_TEXTURE_SHADOW1D ||
1067bf215546Sopenharmony_ci            info.sampler_targets[i] == TGSI_TEXTURE_SHADOW2D) {
1068bf215546Sopenharmony_ci            precompile_state.unit[i].compare_mode_enabled = true;
1069bf215546Sopenharmony_ci            precompile_state.unit[i].texture_compare_func = PIPE_FUNC_LESS;
1070bf215546Sopenharmony_ci        }
1071bf215546Sopenharmony_ci    }
1072bf215546Sopenharmony_ci    r300_pick_fragment_shader(r300, fs, &precompile_state);
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci    return (void *)fs;
1075bf215546Sopenharmony_ci}
1076bf215546Sopenharmony_ci
1077bf215546Sopenharmony_civoid r300_mark_fs_code_dirty(struct r300_context *r300)
1078bf215546Sopenharmony_ci{
1079bf215546Sopenharmony_ci    struct r300_fragment_shader* fs = r300_fs(r300);
1080bf215546Sopenharmony_ci
1081bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->fs);
1082bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->fs_rc_constant_state);
1083bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->fs_constants);
1084bf215546Sopenharmony_ci    r300->fs.size = fs->shader->cb_code_size;
1085bf215546Sopenharmony_ci
1086bf215546Sopenharmony_ci    if (r300->screen->caps.is_r500) {
1087bf215546Sopenharmony_ci        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 7;
1088bf215546Sopenharmony_ci        r300->fs_constants.size = fs->shader->externals_count * 4 + 3;
1089bf215546Sopenharmony_ci    } else {
1090bf215546Sopenharmony_ci        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 5;
1091bf215546Sopenharmony_ci        r300->fs_constants.size = fs->shader->externals_count * 4 + 1;
1092bf215546Sopenharmony_ci    }
1093bf215546Sopenharmony_ci
1094bf215546Sopenharmony_ci    ((struct r300_constant_buffer*)r300->fs_constants.state)->remap_table =
1095bf215546Sopenharmony_ci            fs->shader->code.constants_remap_table;
1096bf215546Sopenharmony_ci}
1097bf215546Sopenharmony_ci
1098bf215546Sopenharmony_ci/* Bind fragment shader state. */
1099bf215546Sopenharmony_cistatic void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
1100bf215546Sopenharmony_ci{
1101bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1102bf215546Sopenharmony_ci    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
1103bf215546Sopenharmony_ci
1104bf215546Sopenharmony_ci    if (!fs) {
1105bf215546Sopenharmony_ci        r300->fs.state = NULL;
1106bf215546Sopenharmony_ci        return;
1107bf215546Sopenharmony_ci    }
1108bf215546Sopenharmony_ci
1109bf215546Sopenharmony_ci    r300->fs.state = fs;
1110bf215546Sopenharmony_ci    r300->fs_status = FRAGMENT_SHADER_DIRTY;
1111bf215546Sopenharmony_ci
1112bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->rs_block_state); /* Will be updated before the emission. */
1113bf215546Sopenharmony_ci}
1114bf215546Sopenharmony_ci
1115bf215546Sopenharmony_ci/* Delete fragment shader state. */
1116bf215546Sopenharmony_cistatic void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
1117bf215546Sopenharmony_ci{
1118bf215546Sopenharmony_ci    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
1119bf215546Sopenharmony_ci    struct r300_fragment_shader_code *tmp, *ptr = fs->first;
1120bf215546Sopenharmony_ci
1121bf215546Sopenharmony_ci    while (ptr) {
1122bf215546Sopenharmony_ci        tmp = ptr;
1123bf215546Sopenharmony_ci        ptr = ptr->next;
1124bf215546Sopenharmony_ci        rc_constants_destroy(&tmp->code.constants);
1125bf215546Sopenharmony_ci        FREE(tmp->cb_code);
1126bf215546Sopenharmony_ci        FREE(tmp);
1127bf215546Sopenharmony_ci    }
1128bf215546Sopenharmony_ci    FREE((void*)fs->state.tokens);
1129bf215546Sopenharmony_ci    FREE(shader);
1130bf215546Sopenharmony_ci}
1131bf215546Sopenharmony_ci
1132bf215546Sopenharmony_cistatic void r300_set_polygon_stipple(struct pipe_context* pipe,
1133bf215546Sopenharmony_ci                                     const struct pipe_poly_stipple* state)
1134bf215546Sopenharmony_ci{
1135bf215546Sopenharmony_ci}
1136bf215546Sopenharmony_ci
1137bf215546Sopenharmony_ci/* Create a new rasterizer state based on the CSO rasterizer state.
1138bf215546Sopenharmony_ci *
1139bf215546Sopenharmony_ci * This is a very large chunk of state, and covers most of the graphics
1140bf215546Sopenharmony_ci * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
1141bf215546Sopenharmony_ci *
1142bf215546Sopenharmony_ci * In a not entirely unironic sidenote, this state has nearly nothing to do
1143bf215546Sopenharmony_ci * with the actual block on the Radeon called the rasterizer (RS). */
1144bf215546Sopenharmony_cistatic void* r300_create_rs_state(struct pipe_context* pipe,
1145bf215546Sopenharmony_ci                                  const struct pipe_rasterizer_state* state)
1146bf215546Sopenharmony_ci{
1147bf215546Sopenharmony_ci    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
1148bf215546Sopenharmony_ci    uint32_t vap_control_status;    /* R300_VAP_CNTL_STATUS: 0x2140 */
1149bf215546Sopenharmony_ci    uint32_t vap_clip_cntl;         /* R300_VAP_CLIP_CNTL: 0x221C */
1150bf215546Sopenharmony_ci    uint32_t point_size;            /* R300_GA_POINT_SIZE: 0x421c */
1151bf215546Sopenharmony_ci    uint32_t point_minmax;          /* R300_GA_POINT_MINMAX: 0x4230 */
1152bf215546Sopenharmony_ci    uint32_t line_control;          /* R300_GA_LINE_CNTL: 0x4234 */
1153bf215546Sopenharmony_ci    uint32_t polygon_offset_enable; /* R300_SU_POLY_OFFSET_ENABLE: 0x42b4 */
1154bf215546Sopenharmony_ci    uint32_t cull_mode;             /* R300_SU_CULL_MODE: 0x42b8 */
1155bf215546Sopenharmony_ci    uint32_t line_stipple_config;   /* R300_GA_LINE_STIPPLE_CONFIG: 0x4328 */
1156bf215546Sopenharmony_ci    uint32_t line_stipple_value;    /* R300_GA_LINE_STIPPLE_VALUE: 0x4260 */
1157bf215546Sopenharmony_ci    uint32_t polygon_mode;          /* R300_GA_POLY_MODE: 0x4288 */
1158bf215546Sopenharmony_ci    uint32_t clip_rule;             /* R300_SC_CLIP_RULE: 0x43D0 */
1159bf215546Sopenharmony_ci    uint32_t round_mode;            /* R300_GA_ROUND_MODE: 0x428c */
1160bf215546Sopenharmony_ci
1161bf215546Sopenharmony_ci    /* Point sprites texture coordinates, 0: lower left, 1: upper right */
1162bf215546Sopenharmony_ci    float point_texcoord_left = 0;  /* R300_GA_POINT_S0: 0x4200 */
1163bf215546Sopenharmony_ci    float point_texcoord_bottom = 0;/* R300_GA_POINT_T0: 0x4204 */
1164bf215546Sopenharmony_ci    float point_texcoord_right = 1; /* R300_GA_POINT_S1: 0x4208 */
1165bf215546Sopenharmony_ci    float point_texcoord_top = 0;   /* R300_GA_POINT_T1: 0x420c */
1166bf215546Sopenharmony_ci    boolean vclamp = !r300_context(pipe)->screen->caps.is_r500;
1167bf215546Sopenharmony_ci    CB_LOCALS;
1168bf215546Sopenharmony_ci
1169bf215546Sopenharmony_ci    /* Copy rasterizer state. */
1170bf215546Sopenharmony_ci    rs->rs = *state;
1171bf215546Sopenharmony_ci    rs->rs_draw = *state;
1172bf215546Sopenharmony_ci
1173bf215546Sopenharmony_ci    rs->rs.sprite_coord_enable = state->point_quad_rasterization *
1174bf215546Sopenharmony_ci                                 state->sprite_coord_enable;
1175bf215546Sopenharmony_ci    r300_context(pipe)->is_point = false;
1176bf215546Sopenharmony_ci
1177bf215546Sopenharmony_ci    /* Override some states for Draw. */
1178bf215546Sopenharmony_ci    rs->rs_draw.sprite_coord_enable = 0; /* We can do this in HW. */
1179bf215546Sopenharmony_ci    rs->rs_draw.offset_point = 0;
1180bf215546Sopenharmony_ci    rs->rs_draw.offset_line = 0;
1181bf215546Sopenharmony_ci    rs->rs_draw.offset_tri = 0;
1182bf215546Sopenharmony_ci    rs->rs_draw.offset_clamp = 0;
1183bf215546Sopenharmony_ci
1184bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN
1185bf215546Sopenharmony_ci    vap_control_status = R300_VC_NO_SWAP;
1186bf215546Sopenharmony_ci#else
1187bf215546Sopenharmony_ci    vap_control_status = R300_VC_32BIT_SWAP;
1188bf215546Sopenharmony_ci#endif
1189bf215546Sopenharmony_ci
1190bf215546Sopenharmony_ci    /* If no TCL engine is present, turn off the HW TCL. */
1191bf215546Sopenharmony_ci    if (!r300_screen(pipe->screen)->caps.has_tcl) {
1192bf215546Sopenharmony_ci        vap_control_status |= R300_VAP_TCL_BYPASS;
1193bf215546Sopenharmony_ci    }
1194bf215546Sopenharmony_ci
1195bf215546Sopenharmony_ci    /* Point size width and height. */
1196bf215546Sopenharmony_ci    point_size =
1197bf215546Sopenharmony_ci        pack_float_16_6x(state->point_size) |
1198bf215546Sopenharmony_ci        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
1199bf215546Sopenharmony_ci
1200bf215546Sopenharmony_ci    /* Point size clamping. */
1201bf215546Sopenharmony_ci    if (state->point_size_per_vertex) {
1202bf215546Sopenharmony_ci        /* Per-vertex point size.
1203bf215546Sopenharmony_ci         * Clamp to [0, max FB size] */
1204bf215546Sopenharmony_ci        float min_psiz = util_get_min_point_size(state);
1205bf215546Sopenharmony_ci        float max_psiz = pipe->screen->get_paramf(pipe->screen,
1206bf215546Sopenharmony_ci                                        PIPE_CAPF_MAX_POINT_SIZE);
1207bf215546Sopenharmony_ci        point_minmax =
1208bf215546Sopenharmony_ci            (pack_float_16_6x(min_psiz) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
1209bf215546Sopenharmony_ci            (pack_float_16_6x(max_psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT);
1210bf215546Sopenharmony_ci    } else {
1211bf215546Sopenharmony_ci        /* We cannot disable the point-size vertex output,
1212bf215546Sopenharmony_ci         * so clamp it. */
1213bf215546Sopenharmony_ci        float psiz = state->point_size;
1214bf215546Sopenharmony_ci        point_minmax =
1215bf215546Sopenharmony_ci            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
1216bf215546Sopenharmony_ci            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT);
1217bf215546Sopenharmony_ci    }
1218bf215546Sopenharmony_ci
1219bf215546Sopenharmony_ci    /* Line control. */
1220bf215546Sopenharmony_ci    line_control = pack_float_16_6x(state->line_width) |
1221bf215546Sopenharmony_ci        (state->line_smooth ? R300_GA_LINE_CNTL_END_TYPE_COMP : R300_GA_LINE_CNTL_END_TYPE_SQR);
1222bf215546Sopenharmony_ci
1223bf215546Sopenharmony_ci    /* Enable polygon mode */
1224bf215546Sopenharmony_ci    polygon_mode = 0;
1225bf215546Sopenharmony_ci    if (state->fill_front != PIPE_POLYGON_MODE_FILL ||
1226bf215546Sopenharmony_ci        state->fill_back != PIPE_POLYGON_MODE_FILL) {
1227bf215546Sopenharmony_ci        polygon_mode = R300_GA_POLY_MODE_DUAL;
1228bf215546Sopenharmony_ci    }
1229bf215546Sopenharmony_ci
1230bf215546Sopenharmony_ci    /* Front face */
1231bf215546Sopenharmony_ci    if (state->front_ccw)
1232bf215546Sopenharmony_ci        cull_mode = R300_FRONT_FACE_CCW;
1233bf215546Sopenharmony_ci    else
1234bf215546Sopenharmony_ci        cull_mode = R300_FRONT_FACE_CW;
1235bf215546Sopenharmony_ci
1236bf215546Sopenharmony_ci    /* Polygon offset */
1237bf215546Sopenharmony_ci    polygon_offset_enable = 0;
1238bf215546Sopenharmony_ci    if (util_get_offset(state, state->fill_front)) {
1239bf215546Sopenharmony_ci       polygon_offset_enable |= R300_FRONT_ENABLE;
1240bf215546Sopenharmony_ci    }
1241bf215546Sopenharmony_ci    if (util_get_offset(state, state->fill_back)) {
1242bf215546Sopenharmony_ci       polygon_offset_enable |= R300_BACK_ENABLE;
1243bf215546Sopenharmony_ci    }
1244bf215546Sopenharmony_ci
1245bf215546Sopenharmony_ci    rs->polygon_offset_enable = polygon_offset_enable != 0;
1246bf215546Sopenharmony_ci
1247bf215546Sopenharmony_ci    /* Polygon mode */
1248bf215546Sopenharmony_ci    if (polygon_mode) {
1249bf215546Sopenharmony_ci       polygon_mode |=
1250bf215546Sopenharmony_ci          r300_translate_polygon_mode_front(state->fill_front);
1251bf215546Sopenharmony_ci       polygon_mode |=
1252bf215546Sopenharmony_ci          r300_translate_polygon_mode_back(state->fill_back);
1253bf215546Sopenharmony_ci    }
1254bf215546Sopenharmony_ci
1255bf215546Sopenharmony_ci    if (state->cull_face & PIPE_FACE_FRONT) {
1256bf215546Sopenharmony_ci        cull_mode |= R300_CULL_FRONT;
1257bf215546Sopenharmony_ci    }
1258bf215546Sopenharmony_ci    if (state->cull_face & PIPE_FACE_BACK) {
1259bf215546Sopenharmony_ci        cull_mode |= R300_CULL_BACK;
1260bf215546Sopenharmony_ci    }
1261bf215546Sopenharmony_ci
1262bf215546Sopenharmony_ci    if (state->line_stipple_enable) {
1263bf215546Sopenharmony_ci        line_stipple_config =
1264bf215546Sopenharmony_ci            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
1265bf215546Sopenharmony_ci            (fui((float)state->line_stipple_factor) &
1266bf215546Sopenharmony_ci                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
1267bf215546Sopenharmony_ci        /* XXX this might need to be scaled up */
1268bf215546Sopenharmony_ci        line_stipple_value = state->line_stipple_pattern;
1269bf215546Sopenharmony_ci    } else {
1270bf215546Sopenharmony_ci        line_stipple_config = 0;
1271bf215546Sopenharmony_ci        line_stipple_value = 0;
1272bf215546Sopenharmony_ci    }
1273bf215546Sopenharmony_ci
1274bf215546Sopenharmony_ci    if (state->flatshade) {
1275bf215546Sopenharmony_ci        rs->color_control = R300_SHADE_MODEL_FLAT;
1276bf215546Sopenharmony_ci    } else {
1277bf215546Sopenharmony_ci        rs->color_control = R300_SHADE_MODEL_SMOOTH;
1278bf215546Sopenharmony_ci    }
1279bf215546Sopenharmony_ci
1280bf215546Sopenharmony_ci    clip_rule = state->scissor ? 0xAAAA : 0xFFFF;
1281bf215546Sopenharmony_ci
1282bf215546Sopenharmony_ci    /* Point sprites coord mode */
1283bf215546Sopenharmony_ci    if (rs->rs.sprite_coord_enable) {
1284bf215546Sopenharmony_ci        switch (state->sprite_coord_mode) {
1285bf215546Sopenharmony_ci            case PIPE_SPRITE_COORD_UPPER_LEFT:
1286bf215546Sopenharmony_ci                point_texcoord_top = 0.0f;
1287bf215546Sopenharmony_ci                point_texcoord_bottom = 1.0f;
1288bf215546Sopenharmony_ci                break;
1289bf215546Sopenharmony_ci            case PIPE_SPRITE_COORD_LOWER_LEFT:
1290bf215546Sopenharmony_ci                point_texcoord_top = 1.0f;
1291bf215546Sopenharmony_ci                point_texcoord_bottom = 0.0f;
1292bf215546Sopenharmony_ci                break;
1293bf215546Sopenharmony_ci        }
1294bf215546Sopenharmony_ci    }
1295bf215546Sopenharmony_ci
1296bf215546Sopenharmony_ci    if (r300_screen(pipe->screen)->caps.has_tcl) {
1297bf215546Sopenharmony_ci       vap_clip_cntl = (state->clip_plane_enable & 63) |
1298bf215546Sopenharmony_ci                       R300_PS_UCP_MODE_CLIP_AS_TRIFAN;
1299bf215546Sopenharmony_ci    } else {
1300bf215546Sopenharmony_ci       vap_clip_cntl = R300_CLIP_DISABLE;
1301bf215546Sopenharmony_ci    }
1302bf215546Sopenharmony_ci
1303bf215546Sopenharmony_ci    /* Vertex color clamping. FP20 means no clamping. */
1304bf215546Sopenharmony_ci    round_mode =
1305bf215546Sopenharmony_ci      R300_GA_ROUND_MODE_GEOMETRY_ROUND_NEAREST |
1306bf215546Sopenharmony_ci      (!vclamp ? (R300_GA_ROUND_MODE_RGB_CLAMP_FP20 |
1307bf215546Sopenharmony_ci                  R300_GA_ROUND_MODE_ALPHA_CLAMP_FP20) : 0);
1308bf215546Sopenharmony_ci
1309bf215546Sopenharmony_ci    /* Build the main command buffer. */
1310bf215546Sopenharmony_ci    BEGIN_CB(rs->cb_main, RS_STATE_MAIN_SIZE);
1311bf215546Sopenharmony_ci    OUT_CB_REG(R300_VAP_CNTL_STATUS, vap_control_status);
1312bf215546Sopenharmony_ci    OUT_CB_REG(R300_VAP_CLIP_CNTL, vap_clip_cntl);
1313bf215546Sopenharmony_ci    OUT_CB_REG(R300_GA_POINT_SIZE, point_size);
1314bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_GA_POINT_MINMAX, 2);
1315bf215546Sopenharmony_ci    OUT_CB(point_minmax);
1316bf215546Sopenharmony_ci    OUT_CB(line_control);
1317bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE, 2);
1318bf215546Sopenharmony_ci    OUT_CB(polygon_offset_enable);
1319bf215546Sopenharmony_ci    rs->cull_mode_index = 11;
1320bf215546Sopenharmony_ci    OUT_CB(cull_mode);
1321bf215546Sopenharmony_ci    OUT_CB_REG(R300_GA_LINE_STIPPLE_CONFIG, line_stipple_config);
1322bf215546Sopenharmony_ci    OUT_CB_REG(R300_GA_LINE_STIPPLE_VALUE, line_stipple_value);
1323bf215546Sopenharmony_ci    OUT_CB_REG(R300_GA_POLY_MODE, polygon_mode);
1324bf215546Sopenharmony_ci    OUT_CB_REG(R300_GA_ROUND_MODE, round_mode);
1325bf215546Sopenharmony_ci    OUT_CB_REG(R300_SC_CLIP_RULE, clip_rule);
1326bf215546Sopenharmony_ci    OUT_CB_REG_SEQ(R300_GA_POINT_S0, 4);
1327bf215546Sopenharmony_ci    OUT_CB_32F(point_texcoord_left);
1328bf215546Sopenharmony_ci    OUT_CB_32F(point_texcoord_bottom);
1329bf215546Sopenharmony_ci    OUT_CB_32F(point_texcoord_right);
1330bf215546Sopenharmony_ci    OUT_CB_32F(point_texcoord_top);
1331bf215546Sopenharmony_ci    END_CB;
1332bf215546Sopenharmony_ci
1333bf215546Sopenharmony_ci    /* Build the two command buffers for polygon offset setup. */
1334bf215546Sopenharmony_ci    if (polygon_offset_enable) {
1335bf215546Sopenharmony_ci        float scale = state->offset_scale * 12;
1336bf215546Sopenharmony_ci        float offset = state->offset_units * 4;
1337bf215546Sopenharmony_ci
1338bf215546Sopenharmony_ci        BEGIN_CB(rs->cb_poly_offset_zb16, 5);
1339bf215546Sopenharmony_ci        OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 4);
1340bf215546Sopenharmony_ci        OUT_CB_32F(scale);
1341bf215546Sopenharmony_ci        OUT_CB_32F(offset);
1342bf215546Sopenharmony_ci        OUT_CB_32F(scale);
1343bf215546Sopenharmony_ci        OUT_CB_32F(offset);
1344bf215546Sopenharmony_ci        END_CB;
1345bf215546Sopenharmony_ci
1346bf215546Sopenharmony_ci        offset = state->offset_units * 2;
1347bf215546Sopenharmony_ci
1348bf215546Sopenharmony_ci        BEGIN_CB(rs->cb_poly_offset_zb24, 5);
1349bf215546Sopenharmony_ci        OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 4);
1350bf215546Sopenharmony_ci        OUT_CB_32F(scale);
1351bf215546Sopenharmony_ci        OUT_CB_32F(offset);
1352bf215546Sopenharmony_ci        OUT_CB_32F(scale);
1353bf215546Sopenharmony_ci        OUT_CB_32F(offset);
1354bf215546Sopenharmony_ci        END_CB;
1355bf215546Sopenharmony_ci    }
1356bf215546Sopenharmony_ci
1357bf215546Sopenharmony_ci    return (void*)rs;
1358bf215546Sopenharmony_ci}
1359bf215546Sopenharmony_ci
1360bf215546Sopenharmony_ci/* Bind rasterizer state. */
1361bf215546Sopenharmony_cistatic void r300_bind_rs_state(struct pipe_context* pipe, void* state)
1362bf215546Sopenharmony_ci{
1363bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1364bf215546Sopenharmony_ci    struct r300_rs_state* rs = (struct r300_rs_state*)state;
1365bf215546Sopenharmony_ci    int last_sprite_coord_enable = r300->sprite_coord_enable;
1366bf215546Sopenharmony_ci    boolean last_two_sided_color = r300->two_sided_color;
1367bf215546Sopenharmony_ci    boolean last_msaa_enable = r300->msaa_enable;
1368bf215546Sopenharmony_ci    boolean last_flatshade = r300->flatshade;
1369bf215546Sopenharmony_ci    boolean last_clip_halfz = r300->clip_halfz;
1370bf215546Sopenharmony_ci
1371bf215546Sopenharmony_ci    if (r300->draw && rs) {
1372bf215546Sopenharmony_ci        draw_set_rasterizer_state(r300->draw, &rs->rs_draw, state);
1373bf215546Sopenharmony_ci    }
1374bf215546Sopenharmony_ci
1375bf215546Sopenharmony_ci    if (rs) {
1376bf215546Sopenharmony_ci        r300->polygon_offset_enabled = rs->polygon_offset_enable;
1377bf215546Sopenharmony_ci        r300->sprite_coord_enable = rs->rs.sprite_coord_enable;
1378bf215546Sopenharmony_ci        r300->two_sided_color = rs->rs.light_twoside;
1379bf215546Sopenharmony_ci        r300->msaa_enable = rs->rs.multisample;
1380bf215546Sopenharmony_ci        r300->flatshade = rs->rs.flatshade;
1381bf215546Sopenharmony_ci        r300->clip_halfz = rs->rs.clip_halfz;
1382bf215546Sopenharmony_ci    } else {
1383bf215546Sopenharmony_ci        r300->polygon_offset_enabled = FALSE;
1384bf215546Sopenharmony_ci        r300->sprite_coord_enable = 0;
1385bf215546Sopenharmony_ci        r300->two_sided_color = FALSE;
1386bf215546Sopenharmony_ci        r300->msaa_enable = FALSE;
1387bf215546Sopenharmony_ci        r300->flatshade = FALSE;
1388bf215546Sopenharmony_ci        r300->clip_halfz = FALSE;
1389bf215546Sopenharmony_ci    }
1390bf215546Sopenharmony_ci
1391bf215546Sopenharmony_ci    UPDATE_STATE(state, r300->rs_state);
1392bf215546Sopenharmony_ci    r300->rs_state.size = RS_STATE_MAIN_SIZE + (r300->polygon_offset_enabled ? 5 : 0);
1393bf215546Sopenharmony_ci
1394bf215546Sopenharmony_ci    if (last_sprite_coord_enable != r300->sprite_coord_enable ||
1395bf215546Sopenharmony_ci        last_two_sided_color != r300->two_sided_color ||
1396bf215546Sopenharmony_ci        last_flatshade != r300->flatshade) {
1397bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->rs_block_state);
1398bf215546Sopenharmony_ci    }
1399bf215546Sopenharmony_ci
1400bf215546Sopenharmony_ci    if (last_msaa_enable != r300->msaa_enable) {
1401bf215546Sopenharmony_ci        if (r300->alpha_to_coverage) {
1402bf215546Sopenharmony_ci            r300_mark_atom_dirty(r300, &r300->dsa_state);
1403bf215546Sopenharmony_ci        }
1404bf215546Sopenharmony_ci
1405bf215546Sopenharmony_ci        if (r300->alpha_to_one &&
1406bf215546Sopenharmony_ci            r300->fs_status == FRAGMENT_SHADER_VALID) {
1407bf215546Sopenharmony_ci            r300->fs_status = FRAGMENT_SHADER_MAYBE_DIRTY;
1408bf215546Sopenharmony_ci        }
1409bf215546Sopenharmony_ci    }
1410bf215546Sopenharmony_ci
1411bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl && last_clip_halfz != r300->clip_halfz) {
1412bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->vs_state);
1413bf215546Sopenharmony_ci    }
1414bf215546Sopenharmony_ci}
1415bf215546Sopenharmony_ci
1416bf215546Sopenharmony_ci/* Free rasterizer state. */
1417bf215546Sopenharmony_cistatic void r300_delete_rs_state(struct pipe_context* pipe, void* state)
1418bf215546Sopenharmony_ci{
1419bf215546Sopenharmony_ci    FREE(state);
1420bf215546Sopenharmony_ci}
1421bf215546Sopenharmony_ci
1422bf215546Sopenharmony_cistatic void*
1423bf215546Sopenharmony_ci        r300_create_sampler_state(struct pipe_context* pipe,
1424bf215546Sopenharmony_ci                                  const struct pipe_sampler_state* state)
1425bf215546Sopenharmony_ci{
1426bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1427bf215546Sopenharmony_ci    struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
1428bf215546Sopenharmony_ci    boolean is_r500 = r300->screen->caps.is_r500;
1429bf215546Sopenharmony_ci    int lod_bias;
1430bf215546Sopenharmony_ci
1431bf215546Sopenharmony_ci    sampler->state = *state;
1432bf215546Sopenharmony_ci
1433bf215546Sopenharmony_ci    /* r300 doesn't handle CLAMP and MIRROR_CLAMP correctly when either MAG
1434bf215546Sopenharmony_ci     * or MIN filter is NEAREST. Since texwrap produces same results
1435bf215546Sopenharmony_ci     * for CLAMP and CLAMP_TO_EDGE, we use them instead. */
1436bf215546Sopenharmony_ci    if (sampler->state.min_img_filter == PIPE_TEX_FILTER_NEAREST ||
1437bf215546Sopenharmony_ci        sampler->state.mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
1438bf215546Sopenharmony_ci        /* Wrap S. */
1439bf215546Sopenharmony_ci        if (sampler->state.wrap_s == PIPE_TEX_WRAP_CLAMP)
1440bf215546Sopenharmony_ci            sampler->state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1441bf215546Sopenharmony_ci        else if (sampler->state.wrap_s == PIPE_TEX_WRAP_MIRROR_CLAMP)
1442bf215546Sopenharmony_ci            sampler->state.wrap_s = PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
1443bf215546Sopenharmony_ci
1444bf215546Sopenharmony_ci        /* Wrap T. */
1445bf215546Sopenharmony_ci        if (sampler->state.wrap_t == PIPE_TEX_WRAP_CLAMP)
1446bf215546Sopenharmony_ci            sampler->state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1447bf215546Sopenharmony_ci        else if (sampler->state.wrap_t == PIPE_TEX_WRAP_MIRROR_CLAMP)
1448bf215546Sopenharmony_ci            sampler->state.wrap_t = PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
1449bf215546Sopenharmony_ci
1450bf215546Sopenharmony_ci        /* Wrap R. */
1451bf215546Sopenharmony_ci        if (sampler->state.wrap_r == PIPE_TEX_WRAP_CLAMP)
1452bf215546Sopenharmony_ci            sampler->state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1453bf215546Sopenharmony_ci        else if (sampler->state.wrap_r == PIPE_TEX_WRAP_MIRROR_CLAMP)
1454bf215546Sopenharmony_ci            sampler->state.wrap_r = PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
1455bf215546Sopenharmony_ci    }
1456bf215546Sopenharmony_ci
1457bf215546Sopenharmony_ci    sampler->filter0 |=
1458bf215546Sopenharmony_ci        (r300_translate_wrap(sampler->state.wrap_s) << R300_TX_WRAP_S_SHIFT) |
1459bf215546Sopenharmony_ci        (r300_translate_wrap(sampler->state.wrap_t) << R300_TX_WRAP_T_SHIFT) |
1460bf215546Sopenharmony_ci        (r300_translate_wrap(sampler->state.wrap_r) << R300_TX_WRAP_R_SHIFT);
1461bf215546Sopenharmony_ci
1462bf215546Sopenharmony_ci    sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
1463bf215546Sopenharmony_ci                                                   state->mag_img_filter,
1464bf215546Sopenharmony_ci                                                   state->min_mip_filter,
1465bf215546Sopenharmony_ci                                                   state->max_anisotropy > 1);
1466bf215546Sopenharmony_ci
1467bf215546Sopenharmony_ci    sampler->filter0 |= r300_anisotropy(state->max_anisotropy);
1468bf215546Sopenharmony_ci
1469bf215546Sopenharmony_ci    /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
1470bf215546Sopenharmony_ci    /* We must pass these to the merge function to clamp them properly. */
1471bf215546Sopenharmony_ci    sampler->min_lod = (unsigned)MAX2(state->min_lod, 0);
1472bf215546Sopenharmony_ci    sampler->max_lod = (unsigned)MAX2(ceilf(state->max_lod), 0);
1473bf215546Sopenharmony_ci
1474bf215546Sopenharmony_ci    lod_bias = CLAMP((int)(state->lod_bias * 32 + 1), -(1 << 9), (1 << 9) - 1);
1475bf215546Sopenharmony_ci
1476bf215546Sopenharmony_ci    sampler->filter1 |= (lod_bias << R300_LOD_BIAS_SHIFT) & R300_LOD_BIAS_MASK;
1477bf215546Sopenharmony_ci
1478bf215546Sopenharmony_ci    /* This is very high quality anisotropic filtering for R5xx.
1479bf215546Sopenharmony_ci     * It's good for benchmarking the performance of texturing but
1480bf215546Sopenharmony_ci     * in practice we don't want to slow down the driver because it's
1481bf215546Sopenharmony_ci     * a pretty good performance killer. Feel free to play with it. */
1482bf215546Sopenharmony_ci    if (DBG_ON(r300, DBG_ANISOHQ) && is_r500) {
1483bf215546Sopenharmony_ci        sampler->filter1 |= r500_anisotropy(state->max_anisotropy);
1484bf215546Sopenharmony_ci    }
1485bf215546Sopenharmony_ci
1486bf215546Sopenharmony_ci    /* R500-specific fixups and optimizations */
1487bf215546Sopenharmony_ci    if (r300->screen->caps.is_r500) {
1488bf215546Sopenharmony_ci        sampler->filter1 |= R500_BORDER_FIX;
1489bf215546Sopenharmony_ci    }
1490bf215546Sopenharmony_ci
1491bf215546Sopenharmony_ci    return (void*)sampler;
1492bf215546Sopenharmony_ci}
1493bf215546Sopenharmony_ci
1494bf215546Sopenharmony_cistatic void r300_bind_sampler_states(struct pipe_context* pipe,
1495bf215546Sopenharmony_ci                                     enum pipe_shader_type shader,
1496bf215546Sopenharmony_ci                                     unsigned start, unsigned count,
1497bf215546Sopenharmony_ci                                     void** states)
1498bf215546Sopenharmony_ci{
1499bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1500bf215546Sopenharmony_ci    struct r300_textures_state* state =
1501bf215546Sopenharmony_ci        (struct r300_textures_state*)r300->textures_state.state;
1502bf215546Sopenharmony_ci    unsigned tex_units = r300->screen->caps.num_tex_units;
1503bf215546Sopenharmony_ci
1504bf215546Sopenharmony_ci    assert(start == 0);
1505bf215546Sopenharmony_ci
1506bf215546Sopenharmony_ci    if (shader != PIPE_SHADER_FRAGMENT)
1507bf215546Sopenharmony_ci       return;
1508bf215546Sopenharmony_ci
1509bf215546Sopenharmony_ci    if (count > tex_units)
1510bf215546Sopenharmony_ci       return;
1511bf215546Sopenharmony_ci
1512bf215546Sopenharmony_ci    memcpy(state->sampler_states, states, sizeof(void*) * count);
1513bf215546Sopenharmony_ci    state->sampler_state_count = count;
1514bf215546Sopenharmony_ci
1515bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->textures_state);
1516bf215546Sopenharmony_ci}
1517bf215546Sopenharmony_ci
1518bf215546Sopenharmony_cistatic void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
1519bf215546Sopenharmony_ci{
1520bf215546Sopenharmony_ci    FREE(state);
1521bf215546Sopenharmony_ci}
1522bf215546Sopenharmony_ci
1523bf215546Sopenharmony_cistatic uint32_t r300_assign_texture_cache_region(unsigned index, unsigned num)
1524bf215546Sopenharmony_ci{
1525bf215546Sopenharmony_ci    /* This looks like a hack, but I believe it's suppose to work like
1526bf215546Sopenharmony_ci     * that. To illustrate how this works, let's assume you have 5 textures.
1527bf215546Sopenharmony_ci     * From docs, 5 and the successive numbers are:
1528bf215546Sopenharmony_ci     *
1529bf215546Sopenharmony_ci     * FOURTH_1     = 5
1530bf215546Sopenharmony_ci     * FOURTH_2     = 6
1531bf215546Sopenharmony_ci     * FOURTH_3     = 7
1532bf215546Sopenharmony_ci     * EIGHTH_0     = 8
1533bf215546Sopenharmony_ci     * EIGHTH_1     = 9
1534bf215546Sopenharmony_ci     *
1535bf215546Sopenharmony_ci     * First 3 textures will get 3/4 of size of the cache, divided evenly
1536bf215546Sopenharmony_ci     * between them. The last 1/4 of the cache must be divided between
1537bf215546Sopenharmony_ci     * the last 2 textures, each will therefore get 1/8 of the cache.
1538bf215546Sopenharmony_ci     * Why not just to use "5 + texture_index" ?
1539bf215546Sopenharmony_ci     *
1540bf215546Sopenharmony_ci     * This simple trick works for all "num" <= 16.
1541bf215546Sopenharmony_ci     */
1542bf215546Sopenharmony_ci    if (num <= 1)
1543bf215546Sopenharmony_ci        return R300_TX_CACHE(R300_TX_CACHE_WHOLE);
1544bf215546Sopenharmony_ci    else
1545bf215546Sopenharmony_ci        return R300_TX_CACHE(num + index);
1546bf215546Sopenharmony_ci}
1547bf215546Sopenharmony_ci
1548bf215546Sopenharmony_cistatic void r300_set_sampler_views(struct pipe_context* pipe,
1549bf215546Sopenharmony_ci                                   enum pipe_shader_type shader,
1550bf215546Sopenharmony_ci                                   unsigned start, unsigned count,
1551bf215546Sopenharmony_ci                                   unsigned unbind_num_trailing_slots,
1552bf215546Sopenharmony_ci                                   bool take_ownership,
1553bf215546Sopenharmony_ci                                   struct pipe_sampler_view** views)
1554bf215546Sopenharmony_ci{
1555bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1556bf215546Sopenharmony_ci    struct r300_textures_state* state =
1557bf215546Sopenharmony_ci        (struct r300_textures_state*)r300->textures_state.state;
1558bf215546Sopenharmony_ci    struct r300_resource *texture;
1559bf215546Sopenharmony_ci    unsigned i, real_num_views = 0, view_index = 0;
1560bf215546Sopenharmony_ci    unsigned tex_units = r300->screen->caps.num_tex_units;
1561bf215546Sopenharmony_ci    boolean dirty_tex = FALSE;
1562bf215546Sopenharmony_ci
1563bf215546Sopenharmony_ci    assert(start == 0);  /* non-zero not handled yet */
1564bf215546Sopenharmony_ci
1565bf215546Sopenharmony_ci    if (shader != PIPE_SHADER_FRAGMENT || count > tex_units) {
1566bf215546Sopenharmony_ci       if (take_ownership) {
1567bf215546Sopenharmony_ci          for (unsigned i = 0; i < count; i++) {
1568bf215546Sopenharmony_ci             struct pipe_sampler_view *view = views[i];
1569bf215546Sopenharmony_ci             pipe_sampler_view_reference(&view, NULL);
1570bf215546Sopenharmony_ci          }
1571bf215546Sopenharmony_ci       }
1572bf215546Sopenharmony_ci       return;
1573bf215546Sopenharmony_ci    }
1574bf215546Sopenharmony_ci
1575bf215546Sopenharmony_ci    /* Calculate the real number of views. */
1576bf215546Sopenharmony_ci    for (i = 0; i < count; i++) {
1577bf215546Sopenharmony_ci        if (views[i])
1578bf215546Sopenharmony_ci            real_num_views++;
1579bf215546Sopenharmony_ci    }
1580bf215546Sopenharmony_ci
1581bf215546Sopenharmony_ci    for (i = 0; i < count; i++) {
1582bf215546Sopenharmony_ci        if (take_ownership) {
1583bf215546Sopenharmony_ci            pipe_sampler_view_reference(
1584bf215546Sopenharmony_ci                    (struct pipe_sampler_view**)&state->sampler_views[i], NULL);
1585bf215546Sopenharmony_ci            state->sampler_views[i] = (struct r300_sampler_view*)views[i];
1586bf215546Sopenharmony_ci        } else {
1587bf215546Sopenharmony_ci            pipe_sampler_view_reference(
1588bf215546Sopenharmony_ci                    (struct pipe_sampler_view**)&state->sampler_views[i],
1589bf215546Sopenharmony_ci                    views[i]);
1590bf215546Sopenharmony_ci        }
1591bf215546Sopenharmony_ci
1592bf215546Sopenharmony_ci        if (!views[i]) {
1593bf215546Sopenharmony_ci            continue;
1594bf215546Sopenharmony_ci        }
1595bf215546Sopenharmony_ci
1596bf215546Sopenharmony_ci        /* A new sampler view (= texture)... */
1597bf215546Sopenharmony_ci        dirty_tex = TRUE;
1598bf215546Sopenharmony_ci
1599bf215546Sopenharmony_ci        /* Set the texrect factor in the fragment shader.
1600bf215546Sopenharmony_ci             * Needed for RECT and NPOT fallback. */
1601bf215546Sopenharmony_ci        texture = r300_resource(views[i]->texture);
1602bf215546Sopenharmony_ci        if (texture->tex.is_npot) {
1603bf215546Sopenharmony_ci            r300_mark_atom_dirty(r300, &r300->fs_rc_constant_state);
1604bf215546Sopenharmony_ci        }
1605bf215546Sopenharmony_ci
1606bf215546Sopenharmony_ci        state->sampler_views[i]->texcache_region =
1607bf215546Sopenharmony_ci                r300_assign_texture_cache_region(view_index, real_num_views);
1608bf215546Sopenharmony_ci        view_index++;
1609bf215546Sopenharmony_ci    }
1610bf215546Sopenharmony_ci
1611bf215546Sopenharmony_ci    for (i = count; i < tex_units; i++) {
1612bf215546Sopenharmony_ci        if (state->sampler_views[i]) {
1613bf215546Sopenharmony_ci            pipe_sampler_view_reference(
1614bf215546Sopenharmony_ci                    (struct pipe_sampler_view**)&state->sampler_views[i],
1615bf215546Sopenharmony_ci                    NULL);
1616bf215546Sopenharmony_ci        }
1617bf215546Sopenharmony_ci    }
1618bf215546Sopenharmony_ci
1619bf215546Sopenharmony_ci    state->sampler_view_count = count;
1620bf215546Sopenharmony_ci
1621bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->textures_state);
1622bf215546Sopenharmony_ci
1623bf215546Sopenharmony_ci    if (dirty_tex) {
1624bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->texture_cache_inval);
1625bf215546Sopenharmony_ci    }
1626bf215546Sopenharmony_ci}
1627bf215546Sopenharmony_ci
1628bf215546Sopenharmony_cistruct pipe_sampler_view *
1629bf215546Sopenharmony_cir300_create_sampler_view_custom(struct pipe_context *pipe,
1630bf215546Sopenharmony_ci                         struct pipe_resource *texture,
1631bf215546Sopenharmony_ci                         const struct pipe_sampler_view *templ,
1632bf215546Sopenharmony_ci                         unsigned width0_override,
1633bf215546Sopenharmony_ci                         unsigned height0_override)
1634bf215546Sopenharmony_ci{
1635bf215546Sopenharmony_ci    struct r300_sampler_view *view = CALLOC_STRUCT(r300_sampler_view);
1636bf215546Sopenharmony_ci    struct r300_resource *tex = r300_resource(texture);
1637bf215546Sopenharmony_ci    boolean is_r500 = r300_screen(pipe->screen)->caps.is_r500;
1638bf215546Sopenharmony_ci    boolean dxtc_swizzle = r300_screen(pipe->screen)->caps.dxtc_swizzle;
1639bf215546Sopenharmony_ci
1640bf215546Sopenharmony_ci    if (view) {
1641bf215546Sopenharmony_ci        unsigned hwformat;
1642bf215546Sopenharmony_ci
1643bf215546Sopenharmony_ci        view->base = *templ;
1644bf215546Sopenharmony_ci        view->base.reference.count = 1;
1645bf215546Sopenharmony_ci        view->base.context = pipe;
1646bf215546Sopenharmony_ci        view->base.texture = NULL;
1647bf215546Sopenharmony_ci        pipe_resource_reference(&view->base.texture, texture);
1648bf215546Sopenharmony_ci
1649bf215546Sopenharmony_ci	view->width0_override = width0_override;
1650bf215546Sopenharmony_ci	view->height0_override = height0_override;
1651bf215546Sopenharmony_ci        view->swizzle[0] = templ->swizzle_r;
1652bf215546Sopenharmony_ci        view->swizzle[1] = templ->swizzle_g;
1653bf215546Sopenharmony_ci        view->swizzle[2] = templ->swizzle_b;
1654bf215546Sopenharmony_ci        view->swizzle[3] = templ->swizzle_a;
1655bf215546Sopenharmony_ci
1656bf215546Sopenharmony_ci        hwformat = r300_translate_texformat(templ->format,
1657bf215546Sopenharmony_ci                                            view->swizzle,
1658bf215546Sopenharmony_ci                                            is_r500,
1659bf215546Sopenharmony_ci                                            dxtc_swizzle);
1660bf215546Sopenharmony_ci
1661bf215546Sopenharmony_ci        if (hwformat == ~0) {
1662bf215546Sopenharmony_ci            fprintf(stderr, "r300: Oops. Got unsupported format %s in %s.\n",
1663bf215546Sopenharmony_ci                    util_format_short_name(templ->format), __func__);
1664bf215546Sopenharmony_ci        }
1665bf215546Sopenharmony_ci        assert(hwformat != ~0);
1666bf215546Sopenharmony_ci
1667bf215546Sopenharmony_ci	r300_texture_setup_format_state(r300_screen(pipe->screen), tex,
1668bf215546Sopenharmony_ci					templ->format, 0,
1669bf215546Sopenharmony_ci	                                width0_override, height0_override,
1670bf215546Sopenharmony_ci					&view->format);
1671bf215546Sopenharmony_ci        view->format.format1 |= hwformat;
1672bf215546Sopenharmony_ci        if (is_r500) {
1673bf215546Sopenharmony_ci            view->format.format2 |= r500_tx_format_msb_bit(templ->format);
1674bf215546Sopenharmony_ci        }
1675bf215546Sopenharmony_ci    }
1676bf215546Sopenharmony_ci
1677bf215546Sopenharmony_ci    return (struct pipe_sampler_view*)view;
1678bf215546Sopenharmony_ci}
1679bf215546Sopenharmony_ci
1680bf215546Sopenharmony_cistatic struct pipe_sampler_view *
1681bf215546Sopenharmony_cir300_create_sampler_view(struct pipe_context *pipe,
1682bf215546Sopenharmony_ci                         struct pipe_resource *texture,
1683bf215546Sopenharmony_ci                         const struct pipe_sampler_view *templ)
1684bf215546Sopenharmony_ci{
1685bf215546Sopenharmony_ci    return r300_create_sampler_view_custom(pipe, texture, templ,
1686bf215546Sopenharmony_ci                                           r300_resource(texture)->tex.width0,
1687bf215546Sopenharmony_ci                                           r300_resource(texture)->tex.height0);
1688bf215546Sopenharmony_ci}
1689bf215546Sopenharmony_ci
1690bf215546Sopenharmony_ci
1691bf215546Sopenharmony_cistatic void
1692bf215546Sopenharmony_cir300_sampler_view_destroy(struct pipe_context *pipe,
1693bf215546Sopenharmony_ci                          struct pipe_sampler_view *view)
1694bf215546Sopenharmony_ci{
1695bf215546Sopenharmony_ci   pipe_resource_reference(&view->texture, NULL);
1696bf215546Sopenharmony_ci   FREE(view);
1697bf215546Sopenharmony_ci}
1698bf215546Sopenharmony_ci
1699bf215546Sopenharmony_cistatic void r300_set_sample_mask(struct pipe_context *pipe,
1700bf215546Sopenharmony_ci                                 unsigned mask)
1701bf215546Sopenharmony_ci{
1702bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1703bf215546Sopenharmony_ci
1704bf215546Sopenharmony_ci    *((unsigned*)r300->sample_mask.state) = mask;
1705bf215546Sopenharmony_ci
1706bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->sample_mask);
1707bf215546Sopenharmony_ci}
1708bf215546Sopenharmony_ci
1709bf215546Sopenharmony_cistatic void r300_set_scissor_states(struct pipe_context* pipe,
1710bf215546Sopenharmony_ci                                    unsigned start_slot,
1711bf215546Sopenharmony_ci                                    unsigned num_scissors,
1712bf215546Sopenharmony_ci                                    const struct pipe_scissor_state* state)
1713bf215546Sopenharmony_ci{
1714bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1715bf215546Sopenharmony_ci
1716bf215546Sopenharmony_ci    memcpy(r300->scissor_state.state, state,
1717bf215546Sopenharmony_ci        sizeof(struct pipe_scissor_state));
1718bf215546Sopenharmony_ci
1719bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->scissor_state);
1720bf215546Sopenharmony_ci}
1721bf215546Sopenharmony_ci
1722bf215546Sopenharmony_cistatic void r300_set_viewport_states(struct pipe_context* pipe,
1723bf215546Sopenharmony_ci                                     unsigned start_slot,
1724bf215546Sopenharmony_ci                                     unsigned num_viewports,
1725bf215546Sopenharmony_ci                                     const struct pipe_viewport_state* state)
1726bf215546Sopenharmony_ci{
1727bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1728bf215546Sopenharmony_ci    struct r300_viewport_state* viewport =
1729bf215546Sopenharmony_ci        (struct r300_viewport_state*)r300->viewport_state.state;
1730bf215546Sopenharmony_ci
1731bf215546Sopenharmony_ci    r300->viewport = *state;
1732bf215546Sopenharmony_ci
1733bf215546Sopenharmony_ci    if (r300->draw) {
1734bf215546Sopenharmony_ci        draw_set_viewport_states(r300->draw, start_slot, num_viewports, state);
1735bf215546Sopenharmony_ci        viewport->vte_control = R300_VTX_XY_FMT | R300_VTX_Z_FMT;
1736bf215546Sopenharmony_ci        return;
1737bf215546Sopenharmony_ci    }
1738bf215546Sopenharmony_ci
1739bf215546Sopenharmony_ci    /* Do the transform in HW. */
1740bf215546Sopenharmony_ci    viewport->vte_control = R300_VTX_W0_FMT;
1741bf215546Sopenharmony_ci
1742bf215546Sopenharmony_ci    if (state->scale[0] != 1.0f) {
1743bf215546Sopenharmony_ci        viewport->xscale = state->scale[0];
1744bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
1745bf215546Sopenharmony_ci    }
1746bf215546Sopenharmony_ci    if (state->scale[1] != 1.0f) {
1747bf215546Sopenharmony_ci        viewport->yscale = state->scale[1];
1748bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
1749bf215546Sopenharmony_ci    }
1750bf215546Sopenharmony_ci    if (state->scale[2] != 1.0f) {
1751bf215546Sopenharmony_ci        viewport->zscale = state->scale[2];
1752bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
1753bf215546Sopenharmony_ci    }
1754bf215546Sopenharmony_ci    if (state->translate[0] != 0.0f) {
1755bf215546Sopenharmony_ci        viewport->xoffset = state->translate[0];
1756bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
1757bf215546Sopenharmony_ci    }
1758bf215546Sopenharmony_ci    if (state->translate[1] != 0.0f) {
1759bf215546Sopenharmony_ci        viewport->yoffset = state->translate[1];
1760bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
1761bf215546Sopenharmony_ci    }
1762bf215546Sopenharmony_ci    if (state->translate[2] != 0.0f) {
1763bf215546Sopenharmony_ci        viewport->zoffset = state->translate[2];
1764bf215546Sopenharmony_ci        viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
1765bf215546Sopenharmony_ci    }
1766bf215546Sopenharmony_ci
1767bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->viewport_state);
1768bf215546Sopenharmony_ci    if (r300->fs.state && r300_fs(r300)->shader &&
1769bf215546Sopenharmony_ci        r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED) {
1770bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->fs_rc_constant_state);
1771bf215546Sopenharmony_ci    }
1772bf215546Sopenharmony_ci}
1773bf215546Sopenharmony_ci
1774bf215546Sopenharmony_cistatic void r300_set_vertex_buffers_hwtcl(struct pipe_context* pipe,
1775bf215546Sopenharmony_ci                                    unsigned start_slot, unsigned count,
1776bf215546Sopenharmony_ci                                    unsigned unbind_num_trailing_slots,
1777bf215546Sopenharmony_ci                                    bool take_ownership,
1778bf215546Sopenharmony_ci                                    const struct pipe_vertex_buffer* buffers)
1779bf215546Sopenharmony_ci{
1780bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1781bf215546Sopenharmony_ci
1782bf215546Sopenharmony_ci    util_set_vertex_buffers_count(r300->vertex_buffer,
1783bf215546Sopenharmony_ci                                  &r300->nr_vertex_buffers,
1784bf215546Sopenharmony_ci                                  buffers, start_slot, count,
1785bf215546Sopenharmony_ci                                  unbind_num_trailing_slots, take_ownership);
1786bf215546Sopenharmony_ci
1787bf215546Sopenharmony_ci    /* There must be at least one vertex buffer set, otherwise it locks up. */
1788bf215546Sopenharmony_ci    if (!r300->nr_vertex_buffers) {
1789bf215546Sopenharmony_ci        util_set_vertex_buffers_count(r300->vertex_buffer,
1790bf215546Sopenharmony_ci                                      &r300->nr_vertex_buffers,
1791bf215546Sopenharmony_ci                                      &r300->dummy_vb, 0, 1, 0, false);
1792bf215546Sopenharmony_ci    }
1793bf215546Sopenharmony_ci
1794bf215546Sopenharmony_ci    r300->vertex_arrays_dirty = TRUE;
1795bf215546Sopenharmony_ci}
1796bf215546Sopenharmony_ci
1797bf215546Sopenharmony_cistatic void r300_set_vertex_buffers_swtcl(struct pipe_context* pipe,
1798bf215546Sopenharmony_ci                                    unsigned start_slot, unsigned count,
1799bf215546Sopenharmony_ci                                    unsigned unbind_num_trailing_slots,
1800bf215546Sopenharmony_ci                                    bool take_ownership,
1801bf215546Sopenharmony_ci                                    const struct pipe_vertex_buffer* buffers)
1802bf215546Sopenharmony_ci{
1803bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1804bf215546Sopenharmony_ci    unsigned i;
1805bf215546Sopenharmony_ci
1806bf215546Sopenharmony_ci    util_set_vertex_buffers_count(r300->vertex_buffer,
1807bf215546Sopenharmony_ci                                  &r300->nr_vertex_buffers,
1808bf215546Sopenharmony_ci                                  buffers, start_slot, count,
1809bf215546Sopenharmony_ci                                  unbind_num_trailing_slots, take_ownership);
1810bf215546Sopenharmony_ci    draw_set_vertex_buffers(r300->draw, start_slot, count,
1811bf215546Sopenharmony_ci                            unbind_num_trailing_slots, buffers);
1812bf215546Sopenharmony_ci
1813bf215546Sopenharmony_ci    if (!buffers)
1814bf215546Sopenharmony_ci        return;
1815bf215546Sopenharmony_ci
1816bf215546Sopenharmony_ci    for (i = 0; i < count; i++) {
1817bf215546Sopenharmony_ci        if (buffers[i].is_user_buffer) {
1818bf215546Sopenharmony_ci            draw_set_mapped_vertex_buffer(r300->draw, start_slot + i,
1819bf215546Sopenharmony_ci                                          buffers[i].buffer.user, ~0);
1820bf215546Sopenharmony_ci        } else if (buffers[i].buffer.resource) {
1821bf215546Sopenharmony_ci            draw_set_mapped_vertex_buffer(r300->draw, start_slot + i,
1822bf215546Sopenharmony_ci                                          r300_resource(buffers[i].buffer.resource)->malloced_buffer, ~0);
1823bf215546Sopenharmony_ci        }
1824bf215546Sopenharmony_ci    }
1825bf215546Sopenharmony_ci}
1826bf215546Sopenharmony_ci
1827bf215546Sopenharmony_ci/* Initialize the PSC tables. */
1828bf215546Sopenharmony_cistatic void r300_vertex_psc(struct r300_vertex_element_state *velems)
1829bf215546Sopenharmony_ci{
1830bf215546Sopenharmony_ci    struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
1831bf215546Sopenharmony_ci    uint16_t type, swizzle;
1832bf215546Sopenharmony_ci    enum pipe_format format;
1833bf215546Sopenharmony_ci    unsigned i;
1834bf215546Sopenharmony_ci
1835bf215546Sopenharmony_ci    /* Vertex shaders have no semantics on their inputs,
1836bf215546Sopenharmony_ci     * so PSC should just route stuff based on the vertex elements,
1837bf215546Sopenharmony_ci     * and not on attrib information. */
1838bf215546Sopenharmony_ci    for (i = 0; i < velems->count; i++) {
1839bf215546Sopenharmony_ci        format = velems->velem[i].src_format;
1840bf215546Sopenharmony_ci
1841bf215546Sopenharmony_ci        type = r300_translate_vertex_data_type(format);
1842bf215546Sopenharmony_ci        if (type == R300_INVALID_FORMAT) {
1843bf215546Sopenharmony_ci            fprintf(stderr, "r300: Bad vertex format %s.\n",
1844bf215546Sopenharmony_ci                    util_format_short_name(format));
1845bf215546Sopenharmony_ci            assert(0);
1846bf215546Sopenharmony_ci            abort();
1847bf215546Sopenharmony_ci        }
1848bf215546Sopenharmony_ci
1849bf215546Sopenharmony_ci        type |= i << R300_DST_VEC_LOC_SHIFT;
1850bf215546Sopenharmony_ci        swizzle = r300_translate_vertex_data_swizzle(format);
1851bf215546Sopenharmony_ci
1852bf215546Sopenharmony_ci        if (i & 1) {
1853bf215546Sopenharmony_ci            vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
1854bf215546Sopenharmony_ci            vstream->vap_prog_stream_cntl_ext[i >> 1] |= (uint32_t)swizzle << 16;
1855bf215546Sopenharmony_ci        } else {
1856bf215546Sopenharmony_ci            vstream->vap_prog_stream_cntl[i >> 1] |= type;
1857bf215546Sopenharmony_ci            vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
1858bf215546Sopenharmony_ci        }
1859bf215546Sopenharmony_ci    }
1860bf215546Sopenharmony_ci
1861bf215546Sopenharmony_ci    /* Set the last vector in the PSC. */
1862bf215546Sopenharmony_ci    if (i) {
1863bf215546Sopenharmony_ci        i -= 1;
1864bf215546Sopenharmony_ci    }
1865bf215546Sopenharmony_ci    vstream->vap_prog_stream_cntl[i >> 1] |=
1866bf215546Sopenharmony_ci        (R300_LAST_VEC << (i & 1 ? 16 : 0));
1867bf215546Sopenharmony_ci
1868bf215546Sopenharmony_ci    vstream->count = (i >> 1) + 1;
1869bf215546Sopenharmony_ci}
1870bf215546Sopenharmony_ci
1871bf215546Sopenharmony_cistatic void* r300_create_vertex_elements_state(struct pipe_context* pipe,
1872bf215546Sopenharmony_ci                                               unsigned count,
1873bf215546Sopenharmony_ci                                               const struct pipe_vertex_element* attribs)
1874bf215546Sopenharmony_ci{
1875bf215546Sopenharmony_ci    struct r300_vertex_element_state *velems;
1876bf215546Sopenharmony_ci    unsigned i;
1877bf215546Sopenharmony_ci    struct pipe_vertex_element dummy_attrib = {0};
1878bf215546Sopenharmony_ci
1879bf215546Sopenharmony_ci    /* R300 Programmable Stream Control (PSC) doesn't support 0 vertex elements. */
1880bf215546Sopenharmony_ci    if (!count) {
1881bf215546Sopenharmony_ci        dummy_attrib.src_format = PIPE_FORMAT_R8G8B8A8_UNORM;
1882bf215546Sopenharmony_ci        attribs = &dummy_attrib;
1883bf215546Sopenharmony_ci        count = 1;
1884bf215546Sopenharmony_ci    } else if (count > 16) {
1885bf215546Sopenharmony_ci        fprintf(stderr, "r300: More than 16 vertex elements are not supported,"
1886bf215546Sopenharmony_ci                " requested %i, using 16.\n", count);
1887bf215546Sopenharmony_ci        count = 16;
1888bf215546Sopenharmony_ci    }
1889bf215546Sopenharmony_ci
1890bf215546Sopenharmony_ci    velems = CALLOC_STRUCT(r300_vertex_element_state);
1891bf215546Sopenharmony_ci    if (!velems)
1892bf215546Sopenharmony_ci        return NULL;
1893bf215546Sopenharmony_ci
1894bf215546Sopenharmony_ci    velems->count = count;
1895bf215546Sopenharmony_ci    memcpy(velems->velem, attribs, sizeof(struct pipe_vertex_element) * count);
1896bf215546Sopenharmony_ci
1897bf215546Sopenharmony_ci    if (r300_screen(pipe->screen)->caps.has_tcl) {
1898bf215546Sopenharmony_ci        /* Setup PSC.
1899bf215546Sopenharmony_ci         * The unused components will be replaced by (..., 0, 1). */
1900bf215546Sopenharmony_ci        r300_vertex_psc(velems);
1901bf215546Sopenharmony_ci
1902bf215546Sopenharmony_ci        for (i = 0; i < count; i++) {
1903bf215546Sopenharmony_ci            velems->format_size[i] =
1904bf215546Sopenharmony_ci                align(util_format_get_blocksize(velems->velem[i].src_format), 4);
1905bf215546Sopenharmony_ci            velems->vertex_size_dwords += velems->format_size[i] / 4;
1906bf215546Sopenharmony_ci        }
1907bf215546Sopenharmony_ci    }
1908bf215546Sopenharmony_ci
1909bf215546Sopenharmony_ci    return velems;
1910bf215546Sopenharmony_ci}
1911bf215546Sopenharmony_ci
1912bf215546Sopenharmony_cistatic void r300_bind_vertex_elements_state(struct pipe_context *pipe,
1913bf215546Sopenharmony_ci                                            void *state)
1914bf215546Sopenharmony_ci{
1915bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
1916bf215546Sopenharmony_ci    struct r300_vertex_element_state *velems = state;
1917bf215546Sopenharmony_ci
1918bf215546Sopenharmony_ci    if (!velems) {
1919bf215546Sopenharmony_ci        return;
1920bf215546Sopenharmony_ci    }
1921bf215546Sopenharmony_ci
1922bf215546Sopenharmony_ci    r300->velems = velems;
1923bf215546Sopenharmony_ci
1924bf215546Sopenharmony_ci    if (r300->draw) {
1925bf215546Sopenharmony_ci        draw_set_vertex_elements(r300->draw, velems->count, velems->velem);
1926bf215546Sopenharmony_ci        return;
1927bf215546Sopenharmony_ci    }
1928bf215546Sopenharmony_ci
1929bf215546Sopenharmony_ci    UPDATE_STATE(&velems->vertex_stream, r300->vertex_stream_state);
1930bf215546Sopenharmony_ci    r300->vertex_stream_state.size = (1 + velems->vertex_stream.count) * 2;
1931bf215546Sopenharmony_ci    r300->vertex_arrays_dirty = TRUE;
1932bf215546Sopenharmony_ci}
1933bf215546Sopenharmony_ci
1934bf215546Sopenharmony_cistatic void r300_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
1935bf215546Sopenharmony_ci{
1936bf215546Sopenharmony_ci    FREE(state);
1937bf215546Sopenharmony_ci}
1938bf215546Sopenharmony_ci
1939bf215546Sopenharmony_cistatic void* r300_create_vs_state(struct pipe_context* pipe,
1940bf215546Sopenharmony_ci                                  const struct pipe_shader_state* shader)
1941bf215546Sopenharmony_ci{
1942bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1943bf215546Sopenharmony_ci    struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
1944bf215546Sopenharmony_ci
1945bf215546Sopenharmony_ci    /* Copy state directly into shader. */
1946bf215546Sopenharmony_ci    vs->state = *shader;
1947bf215546Sopenharmony_ci
1948bf215546Sopenharmony_ci    if (vs->state.type == PIPE_SHADER_IR_NIR) {
1949bf215546Sopenharmony_ci       static const struct nir_to_tgsi_options swtcl_options = {0};
1950bf215546Sopenharmony_ci       static const struct nir_to_tgsi_options hwtcl_r300_options = {
1951bf215546Sopenharmony_ci           .lower_cmp = true,
1952bf215546Sopenharmony_ci           .lower_fabs = true,
1953bf215546Sopenharmony_ci       };
1954bf215546Sopenharmony_ci       static const struct nir_to_tgsi_options hwtcl_r500_options = {
1955bf215546Sopenharmony_ci           .lower_cmp = true,
1956bf215546Sopenharmony_ci       };
1957bf215546Sopenharmony_ci       const struct nir_to_tgsi_options *ntt_options;
1958bf215546Sopenharmony_ci       if (r300->screen->caps.has_tcl) {
1959bf215546Sopenharmony_ci           if (r300->screen->caps.is_r500) {
1960bf215546Sopenharmony_ci               ntt_options = &hwtcl_r500_options;
1961bf215546Sopenharmony_ci               NIR_PASS_V(shader->ir.nir, r300_transform_vs_trig_input);
1962bf215546Sopenharmony_ci           }
1963bf215546Sopenharmony_ci            else
1964bf215546Sopenharmony_ci               ntt_options = &hwtcl_r300_options;
1965bf215546Sopenharmony_ci       } else {
1966bf215546Sopenharmony_ci           ntt_options = &swtcl_options;
1967bf215546Sopenharmony_ci       }
1968bf215546Sopenharmony_ci       vs->state.tokens = nir_to_tgsi_options(shader->ir.nir, pipe->screen,
1969bf215546Sopenharmony_ci                                              ntt_options);
1970bf215546Sopenharmony_ci    } else {
1971bf215546Sopenharmony_ci       assert(vs->state.type == PIPE_SHADER_IR_TGSI);
1972bf215546Sopenharmony_ci       /* we need to keep a local copy of the tokens */
1973bf215546Sopenharmony_ci       vs->state.tokens = tgsi_dup_tokens(vs->state.tokens);
1974bf215546Sopenharmony_ci    }
1975bf215546Sopenharmony_ci
1976bf215546Sopenharmony_ci    if (!vs->first)
1977bf215546Sopenharmony_ci        vs->first = vs->shader = CALLOC_STRUCT(r300_vertex_shader_code);
1978bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl) {
1979bf215546Sopenharmony_ci        r300_translate_vertex_shader(r300, vs);
1980bf215546Sopenharmony_ci    } else {
1981bf215546Sopenharmony_ci        r300_draw_init_vertex_shader(r300, vs);
1982bf215546Sopenharmony_ci    }
1983bf215546Sopenharmony_ci
1984bf215546Sopenharmony_ci    return vs;
1985bf215546Sopenharmony_ci}
1986bf215546Sopenharmony_ci
1987bf215546Sopenharmony_cistatic void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
1988bf215546Sopenharmony_ci{
1989bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
1990bf215546Sopenharmony_ci    struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1991bf215546Sopenharmony_ci
1992bf215546Sopenharmony_ci    if (!vs) {
1993bf215546Sopenharmony_ci        r300->vs_state.state = NULL;
1994bf215546Sopenharmony_ci        return;
1995bf215546Sopenharmony_ci    }
1996bf215546Sopenharmony_ci    if (vs == r300->vs_state.state) {
1997bf215546Sopenharmony_ci        return;
1998bf215546Sopenharmony_ci    }
1999bf215546Sopenharmony_ci    r300->vs_state.state = vs;
2000bf215546Sopenharmony_ci
2001bf215546Sopenharmony_ci    /* The majority of the RS block bits is dependent on the vertex shader. */
2002bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->rs_block_state); /* Will be updated before the emission. */
2003bf215546Sopenharmony_ci
2004bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl) {
2005bf215546Sopenharmony_ci        unsigned fc_op_dwords = r300->screen->caps.is_r500 ? 3 : 2;
2006bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->vs_state);
2007bf215546Sopenharmony_ci        r300->vs_state.size = vs->shader->code.length + 9 +
2008bf215546Sopenharmony_ci			(R300_VS_MAX_FC_OPS * fc_op_dwords + 4);
2009bf215546Sopenharmony_ci
2010bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->vs_constants);
2011bf215546Sopenharmony_ci        r300->vs_constants.size =
2012bf215546Sopenharmony_ci                2 +
2013bf215546Sopenharmony_ci                (vs->shader->externals_count ? vs->shader->externals_count * 4 + 3 : 0) +
2014bf215546Sopenharmony_ci                (vs->shader->immediates_count ? vs->shader->immediates_count * 4 + 3 : 0);
2015bf215546Sopenharmony_ci
2016bf215546Sopenharmony_ci        ((struct r300_constant_buffer*)r300->vs_constants.state)->remap_table =
2017bf215546Sopenharmony_ci                vs->shader->code.constants_remap_table;
2018bf215546Sopenharmony_ci
2019bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->pvs_flush);
2020bf215546Sopenharmony_ci    } else {
2021bf215546Sopenharmony_ci        draw_bind_vertex_shader(r300->draw,
2022bf215546Sopenharmony_ci                (struct draw_vertex_shader*)vs->draw_vs);
2023bf215546Sopenharmony_ci    }
2024bf215546Sopenharmony_ci}
2025bf215546Sopenharmony_ci
2026bf215546Sopenharmony_cistatic void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
2027bf215546Sopenharmony_ci{
2028bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
2029bf215546Sopenharmony_ci    struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
2030bf215546Sopenharmony_ci
2031bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl) {
2032bf215546Sopenharmony_ci        while (vs->shader) {
2033bf215546Sopenharmony_ci            rc_constants_destroy(&vs->shader->code.constants);
2034bf215546Sopenharmony_ci            FREE(vs->shader->code.constants_remap_table);
2035bf215546Sopenharmony_ci            vs->shader = vs->shader->next;
2036bf215546Sopenharmony_ci            FREE(vs->first);
2037bf215546Sopenharmony_ci            vs->first = vs->shader;
2038bf215546Sopenharmony_ci	}
2039bf215546Sopenharmony_ci    } else {
2040bf215546Sopenharmony_ci        draw_delete_vertex_shader(r300->draw,
2041bf215546Sopenharmony_ci                (struct draw_vertex_shader*)vs->draw_vs);
2042bf215546Sopenharmony_ci    }
2043bf215546Sopenharmony_ci
2044bf215546Sopenharmony_ci    FREE((void*)vs->state.tokens);
2045bf215546Sopenharmony_ci    FREE(shader);
2046bf215546Sopenharmony_ci}
2047bf215546Sopenharmony_ci
2048bf215546Sopenharmony_cistatic void r300_set_constant_buffer(struct pipe_context *pipe,
2049bf215546Sopenharmony_ci                                     enum pipe_shader_type shader, uint index,
2050bf215546Sopenharmony_ci                                     bool take_ownership,
2051bf215546Sopenharmony_ci                                     const struct pipe_constant_buffer *cb)
2052bf215546Sopenharmony_ci{
2053bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
2054bf215546Sopenharmony_ci    struct r300_constant_buffer *cbuf;
2055bf215546Sopenharmony_ci    uint32_t *mapped;
2056bf215546Sopenharmony_ci
2057bf215546Sopenharmony_ci    if (!cb || (!cb->buffer && !cb->user_buffer))
2058bf215546Sopenharmony_ci        return;
2059bf215546Sopenharmony_ci
2060bf215546Sopenharmony_ci    switch (shader) {
2061bf215546Sopenharmony_ci        case PIPE_SHADER_VERTEX:
2062bf215546Sopenharmony_ci            cbuf = (struct r300_constant_buffer*)r300->vs_constants.state;
2063bf215546Sopenharmony_ci            break;
2064bf215546Sopenharmony_ci        case PIPE_SHADER_FRAGMENT:
2065bf215546Sopenharmony_ci            cbuf = (struct r300_constant_buffer*)r300->fs_constants.state;
2066bf215546Sopenharmony_ci            break;
2067bf215546Sopenharmony_ci        default:
2068bf215546Sopenharmony_ci            return;
2069bf215546Sopenharmony_ci    }
2070bf215546Sopenharmony_ci
2071bf215546Sopenharmony_ci
2072bf215546Sopenharmony_ci    if (cb->user_buffer)
2073bf215546Sopenharmony_ci        mapped = (uint32_t*)cb->user_buffer;
2074bf215546Sopenharmony_ci    else {
2075bf215546Sopenharmony_ci        struct r300_resource *rbuf = r300_resource(cb->buffer);
2076bf215546Sopenharmony_ci
2077bf215546Sopenharmony_ci        if (rbuf && rbuf->malloced_buffer)
2078bf215546Sopenharmony_ci            mapped = (uint32_t*)(rbuf->malloced_buffer + cb->buffer_offset);
2079bf215546Sopenharmony_ci        else
2080bf215546Sopenharmony_ci            return;
2081bf215546Sopenharmony_ci    }
2082bf215546Sopenharmony_ci
2083bf215546Sopenharmony_ci    if (shader == PIPE_SHADER_FRAGMENT ||
2084bf215546Sopenharmony_ci        (shader == PIPE_SHADER_VERTEX && r300->screen->caps.has_tcl)) {
2085bf215546Sopenharmony_ci        cbuf->ptr = mapped;
2086bf215546Sopenharmony_ci    }
2087bf215546Sopenharmony_ci
2088bf215546Sopenharmony_ci    if (shader == PIPE_SHADER_VERTEX) {
2089bf215546Sopenharmony_ci        if (r300->screen->caps.has_tcl) {
2090bf215546Sopenharmony_ci            struct r300_vertex_shader *vs = r300_vs(r300);
2091bf215546Sopenharmony_ci
2092bf215546Sopenharmony_ci            if (!vs) {
2093bf215546Sopenharmony_ci                cbuf->buffer_base = 0;
2094bf215546Sopenharmony_ci                return;
2095bf215546Sopenharmony_ci            }
2096bf215546Sopenharmony_ci
2097bf215546Sopenharmony_ci            cbuf->buffer_base = r300->vs_const_base;
2098bf215546Sopenharmony_ci            r300->vs_const_base += vs->shader->code.constants.Count;
2099bf215546Sopenharmony_ci            if (r300->vs_const_base > R500_MAX_PVS_CONST_VECS) {
2100bf215546Sopenharmony_ci                r300->vs_const_base = vs->shader->code.constants.Count;
2101bf215546Sopenharmony_ci                cbuf->buffer_base = 0;
2102bf215546Sopenharmony_ci                r300_mark_atom_dirty(r300, &r300->pvs_flush);
2103bf215546Sopenharmony_ci            }
2104bf215546Sopenharmony_ci            r300_mark_atom_dirty(r300, &r300->vs_constants);
2105bf215546Sopenharmony_ci        } else if (r300->draw) {
2106bf215546Sopenharmony_ci            draw_set_mapped_constant_buffer(r300->draw, PIPE_SHADER_VERTEX,
2107bf215546Sopenharmony_ci                0, mapped, cb->buffer_size);
2108bf215546Sopenharmony_ci        }
2109bf215546Sopenharmony_ci    } else if (shader == PIPE_SHADER_FRAGMENT) {
2110bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->fs_constants);
2111bf215546Sopenharmony_ci    }
2112bf215546Sopenharmony_ci}
2113bf215546Sopenharmony_ci
2114bf215546Sopenharmony_cistatic void r300_texture_barrier(struct pipe_context *pipe, unsigned flags)
2115bf215546Sopenharmony_ci{
2116bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
2117bf215546Sopenharmony_ci
2118bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->gpu_flush);
2119bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->texture_cache_inval);
2120bf215546Sopenharmony_ci}
2121bf215546Sopenharmony_ci
2122bf215546Sopenharmony_cistatic void r300_memory_barrier(struct pipe_context *pipe, unsigned flags)
2123bf215546Sopenharmony_ci{
2124bf215546Sopenharmony_ci}
2125bf215546Sopenharmony_ci
2126bf215546Sopenharmony_civoid r300_init_state_functions(struct r300_context* r300)
2127bf215546Sopenharmony_ci{
2128bf215546Sopenharmony_ci    r300->context.create_blend_state = r300_create_blend_state;
2129bf215546Sopenharmony_ci    r300->context.bind_blend_state = r300_bind_blend_state;
2130bf215546Sopenharmony_ci    r300->context.delete_blend_state = r300_delete_blend_state;
2131bf215546Sopenharmony_ci
2132bf215546Sopenharmony_ci    r300->context.set_blend_color = r300_set_blend_color;
2133bf215546Sopenharmony_ci
2134bf215546Sopenharmony_ci    r300->context.set_clip_state = r300_set_clip_state;
2135bf215546Sopenharmony_ci    r300->context.set_sample_mask = r300_set_sample_mask;
2136bf215546Sopenharmony_ci
2137bf215546Sopenharmony_ci    r300->context.set_constant_buffer = r300_set_constant_buffer;
2138bf215546Sopenharmony_ci
2139bf215546Sopenharmony_ci    r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
2140bf215546Sopenharmony_ci    r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
2141bf215546Sopenharmony_ci    r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
2142bf215546Sopenharmony_ci
2143bf215546Sopenharmony_ci    r300->context.set_stencil_ref = r300_set_stencil_ref;
2144bf215546Sopenharmony_ci
2145bf215546Sopenharmony_ci    r300->context.set_framebuffer_state = r300_set_framebuffer_state;
2146bf215546Sopenharmony_ci
2147bf215546Sopenharmony_ci    r300->context.create_fs_state = r300_create_fs_state;
2148bf215546Sopenharmony_ci    r300->context.bind_fs_state = r300_bind_fs_state;
2149bf215546Sopenharmony_ci    r300->context.delete_fs_state = r300_delete_fs_state;
2150bf215546Sopenharmony_ci
2151bf215546Sopenharmony_ci    r300->context.set_polygon_stipple = r300_set_polygon_stipple;
2152bf215546Sopenharmony_ci
2153bf215546Sopenharmony_ci    r300->context.create_rasterizer_state = r300_create_rs_state;
2154bf215546Sopenharmony_ci    r300->context.bind_rasterizer_state = r300_bind_rs_state;
2155bf215546Sopenharmony_ci    r300->context.delete_rasterizer_state = r300_delete_rs_state;
2156bf215546Sopenharmony_ci
2157bf215546Sopenharmony_ci    r300->context.create_sampler_state = r300_create_sampler_state;
2158bf215546Sopenharmony_ci    r300->context.bind_sampler_states = r300_bind_sampler_states;
2159bf215546Sopenharmony_ci    r300->context.delete_sampler_state = r300_delete_sampler_state;
2160bf215546Sopenharmony_ci
2161bf215546Sopenharmony_ci    r300->context.set_sampler_views = r300_set_sampler_views;
2162bf215546Sopenharmony_ci    r300->context.create_sampler_view = r300_create_sampler_view;
2163bf215546Sopenharmony_ci    r300->context.sampler_view_destroy = r300_sampler_view_destroy;
2164bf215546Sopenharmony_ci
2165bf215546Sopenharmony_ci    r300->context.set_scissor_states = r300_set_scissor_states;
2166bf215546Sopenharmony_ci
2167bf215546Sopenharmony_ci    r300->context.set_viewport_states = r300_set_viewport_states;
2168bf215546Sopenharmony_ci
2169bf215546Sopenharmony_ci    if (r300->screen->caps.has_tcl) {
2170bf215546Sopenharmony_ci        r300->context.set_vertex_buffers = r300_set_vertex_buffers_hwtcl;
2171bf215546Sopenharmony_ci    } else {
2172bf215546Sopenharmony_ci        r300->context.set_vertex_buffers = r300_set_vertex_buffers_swtcl;
2173bf215546Sopenharmony_ci    }
2174bf215546Sopenharmony_ci
2175bf215546Sopenharmony_ci    r300->context.create_vertex_elements_state = r300_create_vertex_elements_state;
2176bf215546Sopenharmony_ci    r300->context.bind_vertex_elements_state = r300_bind_vertex_elements_state;
2177bf215546Sopenharmony_ci    r300->context.delete_vertex_elements_state = r300_delete_vertex_elements_state;
2178bf215546Sopenharmony_ci
2179bf215546Sopenharmony_ci    r300->context.create_vs_state = r300_create_vs_state;
2180bf215546Sopenharmony_ci    r300->context.bind_vs_state = r300_bind_vs_state;
2181bf215546Sopenharmony_ci    r300->context.delete_vs_state = r300_delete_vs_state;
2182bf215546Sopenharmony_ci
2183bf215546Sopenharmony_ci    r300->context.texture_barrier = r300_texture_barrier;
2184bf215546Sopenharmony_ci    r300->context.memory_barrier = r300_memory_barrier;
2185bf215546Sopenharmony_ci}
2186