1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3bf215546Sopenharmony_ci * Copyright 2013 Christoph Bumiller
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 "device9.h"
25bf215546Sopenharmony_ci#include "nine_pipe.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "cso_cache/cso_context.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_civoid
30bf215546Sopenharmony_cinine_convert_dsa_state(struct pipe_depth_stencil_alpha_state *dsa_state,
31bf215546Sopenharmony_ci                       const DWORD *rs)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci    struct pipe_depth_stencil_alpha_state dsa;
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci    memset(&dsa, 0, sizeof(dsa)); /* memcmp safety */
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci    if (rs[D3DRS_ZENABLE]) {
38bf215546Sopenharmony_ci        dsa.depth_enabled = 1;
39bf215546Sopenharmony_ci        dsa.depth_func = d3dcmpfunc_to_pipe_func(rs[D3DRS_ZFUNC]);
40bf215546Sopenharmony_ci        /* Disable depth write if no change can occur */
41bf215546Sopenharmony_ci        dsa.depth_writemask = !!rs[D3DRS_ZWRITEENABLE] &&
42bf215546Sopenharmony_ci            dsa.depth_func != PIPE_FUNC_EQUAL &&
43bf215546Sopenharmony_ci            dsa.depth_func != PIPE_FUNC_NEVER;
44bf215546Sopenharmony_ci    }
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci    if (rs[D3DRS_STENCILENABLE]) {
47bf215546Sopenharmony_ci        dsa.stencil[0].enabled = 1;
48bf215546Sopenharmony_ci        dsa.stencil[0].func = d3dcmpfunc_to_pipe_func(rs[D3DRS_STENCILFUNC]);
49bf215546Sopenharmony_ci        dsa.stencil[0].fail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILFAIL]);
50bf215546Sopenharmony_ci        dsa.stencil[0].zpass_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILPASS]);
51bf215546Sopenharmony_ci        dsa.stencil[0].zfail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILZFAIL]);
52bf215546Sopenharmony_ci        dsa.stencil[0].valuemask = rs[D3DRS_STENCILMASK];
53bf215546Sopenharmony_ci        dsa.stencil[0].writemask = rs[D3DRS_STENCILWRITEMASK];
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci        if (rs[D3DRS_TWOSIDEDSTENCILMODE]) {
56bf215546Sopenharmony_ci            dsa.stencil[1].enabled = 1;
57bf215546Sopenharmony_ci            dsa.stencil[1].func = d3dcmpfunc_to_pipe_func(rs[D3DRS_CCW_STENCILFUNC]);
58bf215546Sopenharmony_ci            dsa.stencil[1].fail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILFAIL]);
59bf215546Sopenharmony_ci            dsa.stencil[1].zpass_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILPASS]);
60bf215546Sopenharmony_ci            dsa.stencil[1].zfail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILZFAIL]);
61bf215546Sopenharmony_ci            dsa.stencil[1].valuemask = dsa.stencil[0].valuemask;
62bf215546Sopenharmony_ci            dsa.stencil[1].writemask = dsa.stencil[0].writemask;
63bf215546Sopenharmony_ci        }
64bf215546Sopenharmony_ci    }
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci    if (rs[D3DRS_ALPHATESTENABLE]) {
67bf215546Sopenharmony_ci        dsa.alpha_enabled = 1;
68bf215546Sopenharmony_ci        dsa.alpha_func = d3dcmpfunc_to_pipe_func(rs[D3DRS_ALPHAFUNC]);
69bf215546Sopenharmony_ci        dsa.alpha_ref_value = (float)rs[D3DRS_ALPHAREF] / 255.0f;
70bf215546Sopenharmony_ci    }
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci    *dsa_state = dsa;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_civoid
76bf215546Sopenharmony_cinine_convert_rasterizer_state(struct NineDevice9 *device,
77bf215546Sopenharmony_ci                              struct pipe_rasterizer_state *rast_state,
78bf215546Sopenharmony_ci                              const DWORD *rs)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci    struct pipe_rasterizer_state rast;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci    memset(&rast, 0, sizeof(rast));
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci    rast.flatshade = rs[D3DRS_SHADEMODE] == D3DSHADE_FLAT;
85bf215546Sopenharmony_ci /* rast.light_twoside = 0; */
86bf215546Sopenharmony_ci /* rast.clamp_fragment_color = 0; */
87bf215546Sopenharmony_ci /* rast.clamp_vertex_color = 0; */
88bf215546Sopenharmony_ci /* rast.front_ccw = 0; */
89bf215546Sopenharmony_ci    rast.cull_face = d3dcull_to_pipe_face(rs[D3DRS_CULLMODE]);
90bf215546Sopenharmony_ci    rast.fill_front = d3dfillmode_to_pipe_polygon_mode(rs[D3DRS_FILLMODE]);
91bf215546Sopenharmony_ci    rast.fill_back = rast.fill_front;
92bf215546Sopenharmony_ci    rast.offset_tri = !!(rs[D3DRS_DEPTHBIAS] | rs[D3DRS_SLOPESCALEDEPTHBIAS]);
93bf215546Sopenharmony_ci    rast.offset_line = rast.offset_tri; /* triangles in wireframe mode */
94bf215546Sopenharmony_ci    rast.offset_point = 0; /* XXX ? */
95bf215546Sopenharmony_ci    rast.scissor = !!rs[D3DRS_SCISSORTESTENABLE];
96bf215546Sopenharmony_ci /* rast.poly_smooth = 0; */
97bf215546Sopenharmony_ci /* rast.poly_stipple_enable = 0; */
98bf215546Sopenharmony_ci /* rast.point_smooth = 0; */
99bf215546Sopenharmony_ci    rast.sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
100bf215546Sopenharmony_ci    rast.point_quad_rasterization = 1;
101bf215546Sopenharmony_ci    rast.point_size_per_vertex = rs[NINED3DRS_VSPOINTSIZE];
102bf215546Sopenharmony_ci    rast.multisample = rs[NINED3DRS_MULTISAMPLE];
103bf215546Sopenharmony_ci    rast.line_smooth = !!rs[D3DRS_ANTIALIASEDLINEENABLE];
104bf215546Sopenharmony_ci /* rast.line_stipple_enable = 0; */
105bf215546Sopenharmony_ci    rast.line_last_pixel = !!rs[D3DRS_LASTPIXEL];
106bf215546Sopenharmony_ci    rast.flatshade_first = 1;
107bf215546Sopenharmony_ci /* rast.half_pixel_center = 0; */
108bf215546Sopenharmony_ci /* rast.lower_left_origin = 0; */
109bf215546Sopenharmony_ci /* rast.bottom_edge_rule = 0; */
110bf215546Sopenharmony_ci /* rast.rasterizer_discard = 0; */
111bf215546Sopenharmony_ci    rast.depth_clip_near = 1;
112bf215546Sopenharmony_ci    rast.depth_clip_far = 1;
113bf215546Sopenharmony_ci    rast.clip_halfz = 1;
114bf215546Sopenharmony_ci    rast.clip_plane_enable = rs[D3DRS_CLIPPLANEENABLE];
115bf215546Sopenharmony_ci /* rast.line_stipple_factor = 0; */
116bf215546Sopenharmony_ci /* rast.line_stipple_pattern = 0; */
117bf215546Sopenharmony_ci    rast.sprite_coord_enable = rs[D3DRS_POINTSPRITEENABLE] ? 0xff : 0x00;
118bf215546Sopenharmony_ci    rast.line_width = 1.0f;
119bf215546Sopenharmony_ci    rast.line_rectangular = 0;
120bf215546Sopenharmony_ci    if (rs[NINED3DRS_VSPOINTSIZE]) {
121bf215546Sopenharmony_ci        rast.point_size = 1.0f;
122bf215546Sopenharmony_ci    } else {
123bf215546Sopenharmony_ci        rast.point_size = CLAMP(asfloat(rs[D3DRS_POINTSIZE]),
124bf215546Sopenharmony_ci                asfloat(rs[D3DRS_POINTSIZE_MIN]),
125bf215546Sopenharmony_ci                asfloat(rs[D3DRS_POINTSIZE_MAX]));
126bf215546Sopenharmony_ci    }
127bf215546Sopenharmony_ci    /* offset_units has the ogl/d3d11 meaning.
128bf215546Sopenharmony_ci     * d3d9: offset = scale * dz + bias
129bf215546Sopenharmony_ci     * ogl/d3d11: offset = scale * dz + r * bias
130bf215546Sopenharmony_ci     * with r implementation dependent (+ different formula for float depth
131bf215546Sopenharmony_ci     * buffers). r=2^-23 is often the right value for gallium drivers.
132bf215546Sopenharmony_ci     * If possible, use offset_units_unscaled, which gives the d3d9
133bf215546Sopenharmony_ci     * behaviour, else scale by 1 << 23 */
134bf215546Sopenharmony_ci    rast.offset_units = asfloat(rs[D3DRS_DEPTHBIAS]) * (device->driver_caps.offset_units_unscaled ? 1.0f : (float)(1 << 23));
135bf215546Sopenharmony_ci    rast.offset_units_unscaled = device->driver_caps.offset_units_unscaled;
136bf215546Sopenharmony_ci    rast.offset_scale = asfloat(rs[D3DRS_SLOPESCALEDEPTHBIAS]);
137bf215546Sopenharmony_ci /* rast.offset_clamp = 0.0f; */
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci    *rast_state = rast;
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_cistatic inline void
143bf215546Sopenharmony_cinine_convert_blend_state_fixup(struct pipe_blend_state *blend, const DWORD *rs)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci    if (unlikely(rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHSRCALPHA ||
146bf215546Sopenharmony_ci                 rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHINVSRCALPHA)) {
147bf215546Sopenharmony_ci        blend->rt[0].rgb_dst_factor = (rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHSRCALPHA) ?
148bf215546Sopenharmony_ci            PIPE_BLENDFACTOR_INV_SRC_ALPHA : PIPE_BLENDFACTOR_SRC_ALPHA;
149bf215546Sopenharmony_ci        if (!rs[D3DRS_SEPARATEALPHABLENDENABLE])
150bf215546Sopenharmony_ci            blend->rt[0].alpha_dst_factor = blend->rt[0].rgb_dst_factor;
151bf215546Sopenharmony_ci    } else
152bf215546Sopenharmony_ci    if (unlikely(rs[D3DRS_SEPARATEALPHABLENDENABLE] &&
153bf215546Sopenharmony_ci                 (rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHSRCALPHA ||
154bf215546Sopenharmony_ci                  rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHINVSRCALPHA))) {
155bf215546Sopenharmony_ci        blend->rt[0].alpha_dst_factor = (rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHSRCALPHA) ?
156bf215546Sopenharmony_ci            PIPE_BLENDFACTOR_INV_SRC_ALPHA : PIPE_BLENDFACTOR_SRC_ALPHA;
157bf215546Sopenharmony_ci    }
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_civoid
161bf215546Sopenharmony_cinine_convert_blend_state(struct pipe_blend_state *blend_state, const DWORD *rs)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci    struct pipe_blend_state blend;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci    memset(&blend, 0, sizeof(blend)); /* memcmp safety */
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci    blend.dither = !!rs[D3DRS_DITHERENABLE];
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci /* blend.alpha_to_one = 0; */
170bf215546Sopenharmony_ci    blend.alpha_to_coverage = !!(rs[NINED3DRS_ALPHACOVERAGE] & 5);
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci    blend.rt[0].blend_enable = !!rs[D3DRS_ALPHABLENDENABLE];
173bf215546Sopenharmony_ci    if (blend.rt[0].blend_enable) {
174bf215546Sopenharmony_ci        blend.rt[0].rgb_func = d3dblendop_to_pipe_blend(rs[D3DRS_BLENDOP]);
175bf215546Sopenharmony_ci        blend.rt[0].rgb_src_factor = d3dblend_color_to_pipe_blendfactor(rs[D3DRS_SRCBLEND]);
176bf215546Sopenharmony_ci        blend.rt[0].rgb_dst_factor = d3dblend_color_to_pipe_blendfactor(rs[D3DRS_DESTBLEND]);
177bf215546Sopenharmony_ci        if (rs[D3DRS_SEPARATEALPHABLENDENABLE]) {
178bf215546Sopenharmony_ci            blend.rt[0].alpha_func = d3dblendop_to_pipe_blend(rs[D3DRS_BLENDOPALPHA]);
179bf215546Sopenharmony_ci            blend.rt[0].alpha_src_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_SRCBLENDALPHA]);
180bf215546Sopenharmony_ci            blend.rt[0].alpha_dst_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_DESTBLENDALPHA]);
181bf215546Sopenharmony_ci        } else {
182bf215546Sopenharmony_ci            /* TODO: Just copy the rgb values ? SRC1_x may differ ... */
183bf215546Sopenharmony_ci            blend.rt[0].alpha_func = blend.rt[0].rgb_func;
184bf215546Sopenharmony_ci            blend.rt[0].alpha_src_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_SRCBLEND]);
185bf215546Sopenharmony_ci            blend.rt[0].alpha_dst_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_DESTBLEND]);
186bf215546Sopenharmony_ci        }
187bf215546Sopenharmony_ci        nine_convert_blend_state_fixup(&blend, rs); /* for BOTH[INV]SRCALPHA */
188bf215546Sopenharmony_ci    }
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci    blend.max_rt = 3; /* Upper bound. Could be optimized to fb->nr_cbufs for example */
191bf215546Sopenharmony_ci    blend.rt[0].colormask = rs[D3DRS_COLORWRITEENABLE];
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci    if (rs[D3DRS_COLORWRITEENABLE1] != rs[D3DRS_COLORWRITEENABLE] ||
194bf215546Sopenharmony_ci        rs[D3DRS_COLORWRITEENABLE2] != rs[D3DRS_COLORWRITEENABLE] ||
195bf215546Sopenharmony_ci        rs[D3DRS_COLORWRITEENABLE3] != rs[D3DRS_COLORWRITEENABLE]) {
196bf215546Sopenharmony_ci        unsigned i;
197bf215546Sopenharmony_ci        blend.independent_blend_enable = TRUE;
198bf215546Sopenharmony_ci        for (i = 1; i < 4; ++i)
199bf215546Sopenharmony_ci            blend.rt[i] = blend.rt[0];
200bf215546Sopenharmony_ci        blend.rt[1].colormask = rs[D3DRS_COLORWRITEENABLE1];
201bf215546Sopenharmony_ci        blend.rt[2].colormask = rs[D3DRS_COLORWRITEENABLE2];
202bf215546Sopenharmony_ci        blend.rt[3].colormask = rs[D3DRS_COLORWRITEENABLE3];
203bf215546Sopenharmony_ci    }
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci    /* blend.force_srgb = !!rs[D3DRS_SRGBWRITEENABLE]; */
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci    *blend_state = blend;
208bf215546Sopenharmony_ci}
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_civoid
211bf215546Sopenharmony_cinine_convert_sampler_state(struct cso_context *ctx, int idx, const DWORD *ss)
212bf215546Sopenharmony_ci{
213bf215546Sopenharmony_ci    struct pipe_sampler_state samp;
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci    assert(idx >= 0 &&
216bf215546Sopenharmony_ci           (idx < NINE_MAX_SAMPLERS_PS || idx >= NINE_SAMPLER_VS(0)) &&
217bf215546Sopenharmony_ci           (idx < NINE_MAX_SAMPLERS));
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci    if (ss[D3DSAMP_MIPFILTER] != D3DTEXF_NONE) {
220bf215546Sopenharmony_ci        samp.lod_bias = asfloat(ss[D3DSAMP_MIPMAPLODBIAS]);
221bf215546Sopenharmony_ci        samp.min_lod = ss[NINED3DSAMP_MINLOD];
222bf215546Sopenharmony_ci        samp.min_mip_filter = (ss[D3DSAMP_MIPFILTER] == D3DTEXF_POINT) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
223bf215546Sopenharmony_ci    } else {
224bf215546Sopenharmony_ci        samp.min_lod = 0.0;
225bf215546Sopenharmony_ci        samp.lod_bias = 0.0;
226bf215546Sopenharmony_ci        samp.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
227bf215546Sopenharmony_ci    }
228bf215546Sopenharmony_ci    samp.max_lod = 15.0f;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci    if (ss[NINED3DSAMP_CUBETEX]) {
231bf215546Sopenharmony_ci        /* Cube textures are always clamped to edge on D3D */
232bf215546Sopenharmony_ci        samp.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
233bf215546Sopenharmony_ci        samp.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
234bf215546Sopenharmony_ci        samp.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
235bf215546Sopenharmony_ci    } else {
236bf215546Sopenharmony_ci        samp.wrap_s = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSU]);
237bf215546Sopenharmony_ci        samp.wrap_t = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSV]);
238bf215546Sopenharmony_ci        samp.wrap_r = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSW]);
239bf215546Sopenharmony_ci    }
240bf215546Sopenharmony_ci    samp.min_img_filter = (ss[D3DSAMP_MINFILTER] == D3DTEXF_POINT && !ss[NINED3DSAMP_SHADOW]) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
241bf215546Sopenharmony_ci    samp.mag_img_filter = (ss[D3DSAMP_MAGFILTER] == D3DTEXF_POINT && !ss[NINED3DSAMP_SHADOW]) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
242bf215546Sopenharmony_ci    if (ss[D3DSAMP_MINFILTER] == D3DTEXF_ANISOTROPIC ||
243bf215546Sopenharmony_ci        ss[D3DSAMP_MAGFILTER] == D3DTEXF_ANISOTROPIC)
244bf215546Sopenharmony_ci        samp.max_anisotropy = MIN2(16, ss[D3DSAMP_MAXANISOTROPY]);
245bf215546Sopenharmony_ci    else
246bf215546Sopenharmony_ci        samp.max_anisotropy = 0;
247bf215546Sopenharmony_ci    samp.compare_mode = ss[NINED3DSAMP_SHADOW] ? PIPE_TEX_COMPARE_R_TO_TEXTURE : PIPE_TEX_COMPARE_NONE;
248bf215546Sopenharmony_ci    samp.compare_func = PIPE_FUNC_LEQUAL;
249bf215546Sopenharmony_ci    samp.normalized_coords = 1;
250bf215546Sopenharmony_ci    samp.seamless_cube_map = 0;
251bf215546Sopenharmony_ci    samp.border_color_is_integer = 0;
252bf215546Sopenharmony_ci    samp.reduction_mode = 0;
253bf215546Sopenharmony_ci    samp.pad = 0;
254bf215546Sopenharmony_ci    samp.border_color_format = PIPE_FORMAT_NONE;
255bf215546Sopenharmony_ci    d3dcolor_to_pipe_color_union(&samp.border_color, ss[D3DSAMP_BORDERCOLOR]);
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci    /* see nine_state.h */
258bf215546Sopenharmony_ci    if (idx < NINE_MAX_SAMPLERS_PS)
259bf215546Sopenharmony_ci        cso_single_sampler(ctx, PIPE_SHADER_FRAGMENT, idx - NINE_SAMPLER_PS(0), &samp);
260bf215546Sopenharmony_ci    else
261bf215546Sopenharmony_ci        cso_single_sampler(ctx, PIPE_SHADER_VERTEX, idx - NINE_SAMPLER_VS(0), &samp);
262bf215546Sopenharmony_ci}
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ciconst enum pipe_format nine_d3d9_to_pipe_format_map[120] =
265bf215546Sopenharmony_ci{
266bf215546Sopenharmony_ci   [D3DFMT_UNKNOWN]       = PIPE_FORMAT_NONE,
267bf215546Sopenharmony_ci   [D3DFMT_R8G8B8]        = PIPE_FORMAT_R8G8B8_UNORM,
268bf215546Sopenharmony_ci   [D3DFMT_A8R8G8B8]      = PIPE_FORMAT_B8G8R8A8_UNORM,
269bf215546Sopenharmony_ci   [D3DFMT_X8R8G8B8]      = PIPE_FORMAT_B8G8R8X8_UNORM,
270bf215546Sopenharmony_ci   [D3DFMT_R5G6B5]        = PIPE_FORMAT_B5G6R5_UNORM,
271bf215546Sopenharmony_ci   [D3DFMT_X1R5G5B5]      = PIPE_FORMAT_B5G5R5X1_UNORM,
272bf215546Sopenharmony_ci   [D3DFMT_A1R5G5B5]      = PIPE_FORMAT_B5G5R5A1_UNORM,
273bf215546Sopenharmony_ci   [D3DFMT_A4R4G4B4]      = PIPE_FORMAT_B4G4R4A4_UNORM,
274bf215546Sopenharmony_ci   [D3DFMT_R3G3B2]        = PIPE_FORMAT_B2G3R3_UNORM,
275bf215546Sopenharmony_ci   [D3DFMT_A8]            = PIPE_FORMAT_A8_UNORM,
276bf215546Sopenharmony_ci   [D3DFMT_A8R3G3B2]      = PIPE_FORMAT_NONE,
277bf215546Sopenharmony_ci   [D3DFMT_X4R4G4B4]      = PIPE_FORMAT_B4G4R4X4_UNORM,
278bf215546Sopenharmony_ci   [D3DFMT_A2B10G10R10]   = PIPE_FORMAT_R10G10B10A2_UNORM,
279bf215546Sopenharmony_ci   [D3DFMT_A8B8G8R8]      = PIPE_FORMAT_R8G8B8A8_UNORM,
280bf215546Sopenharmony_ci   [D3DFMT_X8B8G8R8]      = PIPE_FORMAT_R8G8B8X8_UNORM,
281bf215546Sopenharmony_ci   [D3DFMT_G16R16]        = PIPE_FORMAT_R16G16_UNORM,
282bf215546Sopenharmony_ci   [D3DFMT_A2R10G10B10]   = PIPE_FORMAT_B10G10R10A2_UNORM,
283bf215546Sopenharmony_ci   [D3DFMT_A16B16G16R16]  = PIPE_FORMAT_R16G16B16A16_UNORM,
284bf215546Sopenharmony_ci   [D3DFMT_A8P8]          = PIPE_FORMAT_NONE,
285bf215546Sopenharmony_ci   [D3DFMT_P8]            = PIPE_FORMAT_NONE,
286bf215546Sopenharmony_ci   [D3DFMT_L8]            = PIPE_FORMAT_L8_UNORM,
287bf215546Sopenharmony_ci   [D3DFMT_A8L8]          = PIPE_FORMAT_L8A8_UNORM,
288bf215546Sopenharmony_ci   [D3DFMT_A4L4]          = PIPE_FORMAT_L4A4_UNORM,
289bf215546Sopenharmony_ci   [D3DFMT_V8U8]          = PIPE_FORMAT_R8G8_SNORM,
290bf215546Sopenharmony_ci   [D3DFMT_L6V5U5]        = PIPE_FORMAT_NONE, /* Should be PIPE_FORMAT_R5SG5SB6U_NORM, but interpretation of the data differs a bit. */
291bf215546Sopenharmony_ci   [D3DFMT_X8L8V8U8]      = PIPE_FORMAT_R8SG8SB8UX8U_NORM,
292bf215546Sopenharmony_ci   [D3DFMT_Q8W8V8U8]      = PIPE_FORMAT_R8G8B8A8_SNORM,
293bf215546Sopenharmony_ci   [D3DFMT_V16U16]        = PIPE_FORMAT_R16G16_SNORM,
294bf215546Sopenharmony_ci   [D3DFMT_A2W10V10U10]   = PIPE_FORMAT_R10SG10SB10SA2U_NORM,
295bf215546Sopenharmony_ci   [D3DFMT_D16_LOCKABLE]  = PIPE_FORMAT_Z16_UNORM,
296bf215546Sopenharmony_ci   [D3DFMT_D32]           = PIPE_FORMAT_Z32_UNORM,
297bf215546Sopenharmony_ci   [D3DFMT_D15S1]         = PIPE_FORMAT_NONE,
298bf215546Sopenharmony_ci   [D3DFMT_D24S8]         = PIPE_FORMAT_S8_UINT_Z24_UNORM,
299bf215546Sopenharmony_ci   [D3DFMT_D24X8]         = PIPE_FORMAT_X8Z24_UNORM,
300bf215546Sopenharmony_ci   [D3DFMT_D24X4S4]       = PIPE_FORMAT_NONE,
301bf215546Sopenharmony_ci   [D3DFMT_D16]           = PIPE_FORMAT_Z16_UNORM,
302bf215546Sopenharmony_ci   [D3DFMT_D32F_LOCKABLE] = PIPE_FORMAT_Z32_FLOAT,
303bf215546Sopenharmony_ci   [D3DFMT_D24FS8]        = PIPE_FORMAT_Z32_FLOAT_S8X24_UINT,
304bf215546Sopenharmony_ci   [D3DFMT_D32_LOCKABLE]  = PIPE_FORMAT_NONE,
305bf215546Sopenharmony_ci   [D3DFMT_S8_LOCKABLE]   = PIPE_FORMAT_NONE,
306bf215546Sopenharmony_ci   [D3DFMT_L16]           = PIPE_FORMAT_L16_UNORM,
307bf215546Sopenharmony_ci   [D3DFMT_VERTEXDATA]    = PIPE_FORMAT_NONE,
308bf215546Sopenharmony_ci   [D3DFMT_INDEX16]       = PIPE_FORMAT_R16_UINT,
309bf215546Sopenharmony_ci   [D3DFMT_INDEX32]       = PIPE_FORMAT_R32_UINT,
310bf215546Sopenharmony_ci   [D3DFMT_Q16W16V16U16]  = PIPE_FORMAT_R16G16B16A16_SNORM,
311bf215546Sopenharmony_ci   [D3DFMT_R16F]          = PIPE_FORMAT_R16_FLOAT,
312bf215546Sopenharmony_ci   [D3DFMT_G16R16F]       = PIPE_FORMAT_R16G16_FLOAT,
313bf215546Sopenharmony_ci   [D3DFMT_A16B16G16R16F] = PIPE_FORMAT_R16G16B16A16_FLOAT,
314bf215546Sopenharmony_ci   [D3DFMT_R32F]          = PIPE_FORMAT_R32_FLOAT,
315bf215546Sopenharmony_ci   [D3DFMT_G32R32F]       = PIPE_FORMAT_R32G32_FLOAT,
316bf215546Sopenharmony_ci   [D3DFMT_A32B32G32R32F] = PIPE_FORMAT_R32G32B32A32_FLOAT,
317bf215546Sopenharmony_ci   [D3DFMT_CxV8U8]        = PIPE_FORMAT_NONE,
318bf215546Sopenharmony_ci   [D3DFMT_A1]            = PIPE_FORMAT_NONE,
319bf215546Sopenharmony_ci   [D3DFMT_A2B10G10R10_XR_BIAS] = PIPE_FORMAT_NONE,
320bf215546Sopenharmony_ci};
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ciconst D3DFORMAT nine_pipe_to_d3d9_format_map[PIPE_FORMAT_COUNT] =
323bf215546Sopenharmony_ci{
324bf215546Sopenharmony_ci   [PIPE_FORMAT_NONE]               = D3DFMT_UNKNOWN,
325bf215546Sopenharmony_ci   /* TODO: rename PIPE_FORMAT_R8G8B8_UNORM to PIPE_FORMAT_B8G8R8_UNORM */
326bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8B8_UNORM]       = D3DFMT_R8G8B8,
327bf215546Sopenharmony_ci   [PIPE_FORMAT_B8G8R8A8_UNORM]     = D3DFMT_A8R8G8B8,
328bf215546Sopenharmony_ci   [PIPE_FORMAT_B8G8R8X8_UNORM]     = D3DFMT_X8R8G8B8,
329bf215546Sopenharmony_ci   [PIPE_FORMAT_B5G6R5_UNORM]       = D3DFMT_R5G6B5,
330bf215546Sopenharmony_ci   [PIPE_FORMAT_B5G5R5X1_UNORM]     = D3DFMT_X1R5G5B5,
331bf215546Sopenharmony_ci   [PIPE_FORMAT_B5G5R5A1_UNORM]     = D3DFMT_A1R5G5B5,
332bf215546Sopenharmony_ci   [PIPE_FORMAT_B4G4R4A4_UNORM]     = D3DFMT_A4R4G4B4,
333bf215546Sopenharmony_ci   [PIPE_FORMAT_B2G3R3_UNORM]       = D3DFMT_R3G3B2,
334bf215546Sopenharmony_ci   [PIPE_FORMAT_A8_UNORM]           = D3DFMT_A8,
335bf215546Sopenharmony_ci/* [PIPE_FORMAT_B2G3R3A8_UNORM]     = D3DFMT_A8R3G3B2, */
336bf215546Sopenharmony_ci   [PIPE_FORMAT_B4G4R4X4_UNORM]     = D3DFMT_X4R4G4B4,
337bf215546Sopenharmony_ci   [PIPE_FORMAT_R10G10B10A2_UNORM]  = D3DFMT_A2B10G10R10,
338bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8B8A8_UNORM]     = D3DFMT_A8B8G8R8,
339bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8B8X8_UNORM]     = D3DFMT_X8B8G8R8,
340bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16_UNORM]       = D3DFMT_G16R16,
341bf215546Sopenharmony_ci   [PIPE_FORMAT_B10G10R10A2_UNORM]  = D3DFMT_A2R10G10B10,
342bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16B16A16_UNORM] = D3DFMT_A16B16G16R16,
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci   [PIPE_FORMAT_R8_UINT]            = D3DFMT_P8,
345bf215546Sopenharmony_ci   [PIPE_FORMAT_R8A8_UINT]          = D3DFMT_A8P8,
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   [PIPE_FORMAT_L8_UNORM]           = D3DFMT_L8,
348bf215546Sopenharmony_ci   [PIPE_FORMAT_L8A8_UNORM]         = D3DFMT_A8L8,
349bf215546Sopenharmony_ci   [PIPE_FORMAT_L4A4_UNORM]         = D3DFMT_A4L4,
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8_SNORM]           = D3DFMT_V8U8,
352bf215546Sopenharmony_ci/* [PIPE_FORMAT_?]                    = D3DFMT_L6V5U5, */
353bf215546Sopenharmony_ci/* [PIPE_FORMAT_?]                    = D3DFMT_X8L8V8U8, */
354bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8B8A8_SNORM]       = D3DFMT_Q8W8V8U8,
355bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16_SNORM]         = D3DFMT_V16U16,
356bf215546Sopenharmony_ci   [PIPE_FORMAT_R10SG10SB10SA2U_NORM] = D3DFMT_A2W10V10U10,
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   [PIPE_FORMAT_YUYV]               = D3DFMT_UYVY,
359bf215546Sopenharmony_ci/* [PIPE_FORMAT_YUY2]               = D3DFMT_YUY2, */
360bf215546Sopenharmony_ci   [PIPE_FORMAT_DXT1_RGBA]          = D3DFMT_DXT1,
361bf215546Sopenharmony_ci/* [PIPE_FORMAT_DXT2_RGBA]          = D3DFMT_DXT2, */
362bf215546Sopenharmony_ci   [PIPE_FORMAT_DXT3_RGBA]          = D3DFMT_DXT3,
363bf215546Sopenharmony_ci/* [PIPE_FORMAT_DXT4_RGBA]          = D3DFMT_DXT4, */
364bf215546Sopenharmony_ci   [PIPE_FORMAT_DXT5_RGBA]          = D3DFMT_DXT5,
365bf215546Sopenharmony_ci/* [PIPE_FORMAT_?]                  = D3DFMT_MULTI2_ARGB8, (MET) */
366bf215546Sopenharmony_ci   [PIPE_FORMAT_R8G8_B8G8_UNORM]    = D3DFMT_R8G8_B8G8, /* XXX: order */
367bf215546Sopenharmony_ci   [PIPE_FORMAT_G8R8_G8B8_UNORM]    = D3DFMT_G8R8_G8B8,
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   [PIPE_FORMAT_Z16_UNORM]          = D3DFMT_D16_LOCKABLE,
370bf215546Sopenharmony_ci   [PIPE_FORMAT_Z32_UNORM]          = D3DFMT_D32,
371bf215546Sopenharmony_ci/* [PIPE_FORMAT_Z15_UNORM_S1_UINT]  = D3DFMT_D15S1, */
372bf215546Sopenharmony_ci   [PIPE_FORMAT_S8_UINT_Z24_UNORM]  = D3DFMT_D24S8,
373bf215546Sopenharmony_ci   [PIPE_FORMAT_X8Z24_UNORM]        = D3DFMT_D24X8,
374bf215546Sopenharmony_ci   [PIPE_FORMAT_L16_UNORM]          = D3DFMT_L16,
375bf215546Sopenharmony_ci   [PIPE_FORMAT_Z32_FLOAT]          = D3DFMT_D32F_LOCKABLE,
376bf215546Sopenharmony_ci/* [PIPE_FORMAT_Z24_FLOAT_S8_UINT]  = D3DFMT_D24FS8, */
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   [PIPE_FORMAT_R16_UINT]           = D3DFMT_INDEX16,
379bf215546Sopenharmony_ci   [PIPE_FORMAT_R32_UINT]           = D3DFMT_INDEX32,
380bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16B16A16_SNORM] = D3DFMT_Q16W16V16U16,
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci   [PIPE_FORMAT_R16_FLOAT]          = D3DFMT_R16F,
383bf215546Sopenharmony_ci   [PIPE_FORMAT_R32_FLOAT]          = D3DFMT_R32F,
384bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16_FLOAT]       = D3DFMT_G16R16F,
385bf215546Sopenharmony_ci   [PIPE_FORMAT_R32G32_FLOAT]       = D3DFMT_G32R32F,
386bf215546Sopenharmony_ci   [PIPE_FORMAT_R16G16B16A16_FLOAT] = D3DFMT_A16B16G16R16F,
387bf215546Sopenharmony_ci   [PIPE_FORMAT_R32G32B32A32_FLOAT] = D3DFMT_A32B32G32R32F,
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci/* [PIPE_FORMAT_?]                  = D3DFMT_CxV8U8, */
390bf215546Sopenharmony_ci};
391