17ec681f3Smrg/*
27ec681f3Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
37ec681f3Smrg * Copyright 2013 Christoph Bumiller
47ec681f3Smrg *
57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
67ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
77ec681f3Smrg * to deal in the Software without restriction, including without limitation
87ec681f3Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
97ec681f3Smrg * license, and/or sell copies of the Software, and to permit persons to whom
107ec681f3Smrg * the Software is furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice (including the next
137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
147ec681f3Smrg * Software.
157ec681f3Smrg *
167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
197ec681f3Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
207ec681f3Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
217ec681f3Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
227ec681f3Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
237ec681f3Smrg
247ec681f3Smrg#include "device9.h"
257ec681f3Smrg#include "nine_pipe.h"
267ec681f3Smrg
277ec681f3Smrg#include "cso_cache/cso_context.h"
287ec681f3Smrg
297ec681f3Smrgvoid
307ec681f3Smrgnine_convert_dsa_state(struct pipe_depth_stencil_alpha_state *dsa_state,
317ec681f3Smrg                       const DWORD *rs)
327ec681f3Smrg{
337ec681f3Smrg    struct pipe_depth_stencil_alpha_state dsa;
347ec681f3Smrg
357ec681f3Smrg    memset(&dsa, 0, sizeof(dsa)); /* memcmp safety */
367ec681f3Smrg
377ec681f3Smrg    if (rs[D3DRS_ZENABLE]) {
387ec681f3Smrg        dsa.depth_enabled = 1;
397ec681f3Smrg        dsa.depth_func = d3dcmpfunc_to_pipe_func(rs[D3DRS_ZFUNC]);
407ec681f3Smrg        /* Disable depth write if no change can occur */
417ec681f3Smrg        dsa.depth_writemask = !!rs[D3DRS_ZWRITEENABLE] &&
427ec681f3Smrg            dsa.depth_func != PIPE_FUNC_EQUAL &&
437ec681f3Smrg            dsa.depth_func != PIPE_FUNC_NEVER;
447ec681f3Smrg    }
457ec681f3Smrg
467ec681f3Smrg    if (rs[D3DRS_STENCILENABLE]) {
477ec681f3Smrg        dsa.stencil[0].enabled = 1;
487ec681f3Smrg        dsa.stencil[0].func = d3dcmpfunc_to_pipe_func(rs[D3DRS_STENCILFUNC]);
497ec681f3Smrg        dsa.stencil[0].fail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILFAIL]);
507ec681f3Smrg        dsa.stencil[0].zpass_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILPASS]);
517ec681f3Smrg        dsa.stencil[0].zfail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_STENCILZFAIL]);
527ec681f3Smrg        dsa.stencil[0].valuemask = rs[D3DRS_STENCILMASK];
537ec681f3Smrg        dsa.stencil[0].writemask = rs[D3DRS_STENCILWRITEMASK];
547ec681f3Smrg
557ec681f3Smrg        if (rs[D3DRS_TWOSIDEDSTENCILMODE]) {
567ec681f3Smrg            dsa.stencil[1].enabled = 1;
577ec681f3Smrg            dsa.stencil[1].func = d3dcmpfunc_to_pipe_func(rs[D3DRS_CCW_STENCILFUNC]);
587ec681f3Smrg            dsa.stencil[1].fail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILFAIL]);
597ec681f3Smrg            dsa.stencil[1].zpass_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILPASS]);
607ec681f3Smrg            dsa.stencil[1].zfail_op = d3dstencilop_to_pipe_stencil_op(rs[D3DRS_CCW_STENCILZFAIL]);
617ec681f3Smrg            dsa.stencil[1].valuemask = dsa.stencil[0].valuemask;
627ec681f3Smrg            dsa.stencil[1].writemask = dsa.stencil[0].writemask;
637ec681f3Smrg        }
647ec681f3Smrg    }
657ec681f3Smrg
667ec681f3Smrg    if (rs[D3DRS_ALPHATESTENABLE]) {
677ec681f3Smrg        dsa.alpha_enabled = 1;
687ec681f3Smrg        dsa.alpha_func = d3dcmpfunc_to_pipe_func(rs[D3DRS_ALPHAFUNC]);
697ec681f3Smrg        dsa.alpha_ref_value = (float)rs[D3DRS_ALPHAREF] / 255.0f;
707ec681f3Smrg    }
717ec681f3Smrg
727ec681f3Smrg    *dsa_state = dsa;
737ec681f3Smrg}
747ec681f3Smrg
757ec681f3Smrgvoid
767ec681f3Smrgnine_convert_rasterizer_state(struct NineDevice9 *device,
777ec681f3Smrg                              struct pipe_rasterizer_state *rast_state,
787ec681f3Smrg                              const DWORD *rs)
797ec681f3Smrg{
807ec681f3Smrg    struct pipe_rasterizer_state rast;
817ec681f3Smrg
827ec681f3Smrg    memset(&rast, 0, sizeof(rast));
837ec681f3Smrg
847ec681f3Smrg    rast.flatshade = rs[D3DRS_SHADEMODE] == D3DSHADE_FLAT;
857ec681f3Smrg /* rast.light_twoside = 0; */
867ec681f3Smrg /* rast.clamp_fragment_color = 0; */
877ec681f3Smrg /* rast.clamp_vertex_color = 0; */
887ec681f3Smrg /* rast.front_ccw = 0; */
897ec681f3Smrg    rast.cull_face = d3dcull_to_pipe_face(rs[D3DRS_CULLMODE]);
907ec681f3Smrg    rast.fill_front = d3dfillmode_to_pipe_polygon_mode(rs[D3DRS_FILLMODE]);
917ec681f3Smrg    rast.fill_back = rast.fill_front;
927ec681f3Smrg    rast.offset_tri = !!(rs[D3DRS_DEPTHBIAS] | rs[D3DRS_SLOPESCALEDEPTHBIAS]);
937ec681f3Smrg    rast.offset_line = rast.offset_tri; /* triangles in wireframe mode */
947ec681f3Smrg    rast.offset_point = 0; /* XXX ? */
957ec681f3Smrg    rast.scissor = !!rs[D3DRS_SCISSORTESTENABLE];
967ec681f3Smrg /* rast.poly_smooth = 0; */
977ec681f3Smrg /* rast.poly_stipple_enable = 0; */
987ec681f3Smrg /* rast.point_smooth = 0; */
997ec681f3Smrg    rast.sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
1007ec681f3Smrg    rast.point_quad_rasterization = 1;
1017ec681f3Smrg    rast.point_size_per_vertex = rs[NINED3DRS_VSPOINTSIZE];
1027ec681f3Smrg    rast.multisample = rs[NINED3DRS_MULTISAMPLE];
1037ec681f3Smrg    rast.line_smooth = !!rs[D3DRS_ANTIALIASEDLINEENABLE];
1047ec681f3Smrg /* rast.line_stipple_enable = 0; */
1057ec681f3Smrg    rast.line_last_pixel = !!rs[D3DRS_LASTPIXEL];
1067ec681f3Smrg    rast.flatshade_first = 1;
1077ec681f3Smrg /* rast.half_pixel_center = 0; */
1087ec681f3Smrg /* rast.lower_left_origin = 0; */
1097ec681f3Smrg /* rast.bottom_edge_rule = 0; */
1107ec681f3Smrg /* rast.rasterizer_discard = 0; */
1117ec681f3Smrg    rast.depth_clip_near = 1;
1127ec681f3Smrg    rast.depth_clip_far = 1;
1137ec681f3Smrg    rast.clip_halfz = 1;
1147ec681f3Smrg    rast.clip_plane_enable = rs[D3DRS_CLIPPLANEENABLE];
1157ec681f3Smrg /* rast.line_stipple_factor = 0; */
1167ec681f3Smrg /* rast.line_stipple_pattern = 0; */
1177ec681f3Smrg    rast.sprite_coord_enable = rs[D3DRS_POINTSPRITEENABLE] ? 0xff : 0x00;
1187ec681f3Smrg    rast.line_width = 1.0f;
1197ec681f3Smrg    rast.line_rectangular = 0;
1207ec681f3Smrg    if (rs[NINED3DRS_VSPOINTSIZE]) {
1217ec681f3Smrg        rast.point_size = 1.0f;
1227ec681f3Smrg    } else {
1237ec681f3Smrg        rast.point_size = CLAMP(asfloat(rs[D3DRS_POINTSIZE]),
1247ec681f3Smrg                asfloat(rs[D3DRS_POINTSIZE_MIN]),
1257ec681f3Smrg                asfloat(rs[D3DRS_POINTSIZE_MAX]));
1267ec681f3Smrg    }
1277ec681f3Smrg    /* offset_units has the ogl/d3d11 meaning.
1287ec681f3Smrg     * d3d9: offset = scale * dz + bias
1297ec681f3Smrg     * ogl/d3d11: offset = scale * dz + r * bias
1307ec681f3Smrg     * with r implementation dependent (+ different formula for float depth
1317ec681f3Smrg     * buffers). r=2^-23 is often the right value for gallium drivers.
1327ec681f3Smrg     * If possible, use offset_units_unscaled, which gives the d3d9
1337ec681f3Smrg     * behaviour, else scale by 1 << 23 */
1347ec681f3Smrg    rast.offset_units = asfloat(rs[D3DRS_DEPTHBIAS]) * (device->driver_caps.offset_units_unscaled ? 1.0f : (float)(1 << 23));
1357ec681f3Smrg    rast.offset_units_unscaled = device->driver_caps.offset_units_unscaled;
1367ec681f3Smrg    rast.offset_scale = asfloat(rs[D3DRS_SLOPESCALEDEPTHBIAS]);
1377ec681f3Smrg /* rast.offset_clamp = 0.0f; */
1387ec681f3Smrg
1397ec681f3Smrg    *rast_state = rast;
1407ec681f3Smrg}
1417ec681f3Smrg
1427ec681f3Smrgstatic inline void
1437ec681f3Smrgnine_convert_blend_state_fixup(struct pipe_blend_state *blend, const DWORD *rs)
1447ec681f3Smrg{
1457ec681f3Smrg    if (unlikely(rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHSRCALPHA ||
1467ec681f3Smrg                 rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHINVSRCALPHA)) {
1477ec681f3Smrg        blend->rt[0].rgb_dst_factor = (rs[D3DRS_SRCBLEND] == D3DBLEND_BOTHSRCALPHA) ?
1487ec681f3Smrg            PIPE_BLENDFACTOR_INV_SRC_ALPHA : PIPE_BLENDFACTOR_SRC_ALPHA;
1497ec681f3Smrg        if (!rs[D3DRS_SEPARATEALPHABLENDENABLE])
1507ec681f3Smrg            blend->rt[0].alpha_dst_factor = blend->rt[0].rgb_dst_factor;
1517ec681f3Smrg    } else
1527ec681f3Smrg    if (unlikely(rs[D3DRS_SEPARATEALPHABLENDENABLE] &&
1537ec681f3Smrg                 (rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHSRCALPHA ||
1547ec681f3Smrg                  rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHINVSRCALPHA))) {
1557ec681f3Smrg        blend->rt[0].alpha_dst_factor = (rs[D3DRS_SRCBLENDALPHA] == D3DBLEND_BOTHSRCALPHA) ?
1567ec681f3Smrg            PIPE_BLENDFACTOR_INV_SRC_ALPHA : PIPE_BLENDFACTOR_SRC_ALPHA;
1577ec681f3Smrg    }
1587ec681f3Smrg}
1597ec681f3Smrg
1607ec681f3Smrgvoid
1617ec681f3Smrgnine_convert_blend_state(struct pipe_blend_state *blend_state, const DWORD *rs)
1627ec681f3Smrg{
1637ec681f3Smrg    struct pipe_blend_state blend;
1647ec681f3Smrg
1657ec681f3Smrg    memset(&blend, 0, sizeof(blend)); /* memcmp safety */
1667ec681f3Smrg
1677ec681f3Smrg    blend.dither = !!rs[D3DRS_DITHERENABLE];
1687ec681f3Smrg
1697ec681f3Smrg /* blend.alpha_to_one = 0; */
1707ec681f3Smrg    blend.alpha_to_coverage = !!(rs[NINED3DRS_ALPHACOVERAGE] & 5);
1717ec681f3Smrg
1727ec681f3Smrg    blend.rt[0].blend_enable = !!rs[D3DRS_ALPHABLENDENABLE];
1737ec681f3Smrg    if (blend.rt[0].blend_enable) {
1747ec681f3Smrg        blend.rt[0].rgb_func = d3dblendop_to_pipe_blend(rs[D3DRS_BLENDOP]);
1757ec681f3Smrg        blend.rt[0].rgb_src_factor = d3dblend_color_to_pipe_blendfactor(rs[D3DRS_SRCBLEND]);
1767ec681f3Smrg        blend.rt[0].rgb_dst_factor = d3dblend_color_to_pipe_blendfactor(rs[D3DRS_DESTBLEND]);
1777ec681f3Smrg        if (rs[D3DRS_SEPARATEALPHABLENDENABLE]) {
1787ec681f3Smrg            blend.rt[0].alpha_func = d3dblendop_to_pipe_blend(rs[D3DRS_BLENDOPALPHA]);
1797ec681f3Smrg            blend.rt[0].alpha_src_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_SRCBLENDALPHA]);
1807ec681f3Smrg            blend.rt[0].alpha_dst_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_DESTBLENDALPHA]);
1817ec681f3Smrg        } else {
1827ec681f3Smrg            /* TODO: Just copy the rgb values ? SRC1_x may differ ... */
1837ec681f3Smrg            blend.rt[0].alpha_func = blend.rt[0].rgb_func;
1847ec681f3Smrg            blend.rt[0].alpha_src_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_SRCBLEND]);
1857ec681f3Smrg            blend.rt[0].alpha_dst_factor = d3dblend_alpha_to_pipe_blendfactor(rs[D3DRS_DESTBLEND]);
1867ec681f3Smrg        }
1877ec681f3Smrg        nine_convert_blend_state_fixup(&blend, rs); /* for BOTH[INV]SRCALPHA */
1887ec681f3Smrg    }
1897ec681f3Smrg
1907ec681f3Smrg    blend.max_rt = 3; /* Upper bound. Could be optimized to fb->nr_cbufs for example */
1917ec681f3Smrg    blend.rt[0].colormask = rs[D3DRS_COLORWRITEENABLE];
1927ec681f3Smrg
1937ec681f3Smrg    if (rs[D3DRS_COLORWRITEENABLE1] != rs[D3DRS_COLORWRITEENABLE] ||
1947ec681f3Smrg        rs[D3DRS_COLORWRITEENABLE2] != rs[D3DRS_COLORWRITEENABLE] ||
1957ec681f3Smrg        rs[D3DRS_COLORWRITEENABLE3] != rs[D3DRS_COLORWRITEENABLE]) {
1967ec681f3Smrg        unsigned i;
1977ec681f3Smrg        blend.independent_blend_enable = TRUE;
1987ec681f3Smrg        for (i = 1; i < 4; ++i)
1997ec681f3Smrg            blend.rt[i] = blend.rt[0];
2007ec681f3Smrg        blend.rt[1].colormask = rs[D3DRS_COLORWRITEENABLE1];
2017ec681f3Smrg        blend.rt[2].colormask = rs[D3DRS_COLORWRITEENABLE2];
2027ec681f3Smrg        blend.rt[3].colormask = rs[D3DRS_COLORWRITEENABLE3];
2037ec681f3Smrg    }
2047ec681f3Smrg
2057ec681f3Smrg    /* blend.force_srgb = !!rs[D3DRS_SRGBWRITEENABLE]; */
2067ec681f3Smrg
2077ec681f3Smrg    *blend_state = blend;
2087ec681f3Smrg}
2097ec681f3Smrg
2107ec681f3Smrgvoid
2117ec681f3Smrgnine_convert_sampler_state(struct cso_context *ctx, int idx, const DWORD *ss)
2127ec681f3Smrg{
2137ec681f3Smrg    struct pipe_sampler_state samp;
2147ec681f3Smrg
2157ec681f3Smrg    assert(idx >= 0 &&
2167ec681f3Smrg           (idx < NINE_MAX_SAMPLERS_PS || idx >= NINE_SAMPLER_VS(0)) &&
2177ec681f3Smrg           (idx < NINE_MAX_SAMPLERS));
2187ec681f3Smrg
2197ec681f3Smrg    if (ss[D3DSAMP_MIPFILTER] != D3DTEXF_NONE) {
2207ec681f3Smrg        samp.lod_bias = asfloat(ss[D3DSAMP_MIPMAPLODBIAS]);
2217ec681f3Smrg        samp.min_lod = ss[NINED3DSAMP_MINLOD];
2227ec681f3Smrg        samp.min_mip_filter = (ss[D3DSAMP_MIPFILTER] == D3DTEXF_POINT) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
2237ec681f3Smrg    } else {
2247ec681f3Smrg        samp.min_lod = 0.0;
2257ec681f3Smrg        samp.lod_bias = 0.0;
2267ec681f3Smrg        samp.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
2277ec681f3Smrg    }
2287ec681f3Smrg    samp.max_lod = 15.0f;
2297ec681f3Smrg
2307ec681f3Smrg    if (ss[NINED3DSAMP_CUBETEX]) {
2317ec681f3Smrg        /* Cube textures are always clamped to edge on D3D */
2327ec681f3Smrg        samp.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2337ec681f3Smrg        samp.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2347ec681f3Smrg        samp.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2357ec681f3Smrg    } else {
2367ec681f3Smrg        samp.wrap_s = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSU]);
2377ec681f3Smrg        samp.wrap_t = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSV]);
2387ec681f3Smrg        samp.wrap_r = d3dtextureaddress_to_pipe_tex_wrap(ss[D3DSAMP_ADDRESSW]);
2397ec681f3Smrg    }
2407ec681f3Smrg    samp.min_img_filter = (ss[D3DSAMP_MINFILTER] == D3DTEXF_POINT && !ss[NINED3DSAMP_SHADOW]) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
2417ec681f3Smrg    samp.mag_img_filter = (ss[D3DSAMP_MAGFILTER] == D3DTEXF_POINT && !ss[NINED3DSAMP_SHADOW]) ? PIPE_TEX_FILTER_NEAREST : PIPE_TEX_FILTER_LINEAR;
2427ec681f3Smrg    if (ss[D3DSAMP_MINFILTER] == D3DTEXF_ANISOTROPIC ||
2437ec681f3Smrg        ss[D3DSAMP_MAGFILTER] == D3DTEXF_ANISOTROPIC)
2447ec681f3Smrg        samp.max_anisotropy = MIN2(16, ss[D3DSAMP_MAXANISOTROPY]);
2457ec681f3Smrg    else
2467ec681f3Smrg        samp.max_anisotropy = 0;
2477ec681f3Smrg    samp.compare_mode = ss[NINED3DSAMP_SHADOW] ? PIPE_TEX_COMPARE_R_TO_TEXTURE : PIPE_TEX_COMPARE_NONE;
2487ec681f3Smrg    samp.compare_func = PIPE_FUNC_LEQUAL;
2497ec681f3Smrg    samp.normalized_coords = 1;
2507ec681f3Smrg    samp.seamless_cube_map = 0;
2517ec681f3Smrg    samp.border_color_is_integer = 0;
2527ec681f3Smrg    samp.reduction_mode = 0;
2537ec681f3Smrg    samp.pad = 0;
2547ec681f3Smrg    d3dcolor_to_pipe_color_union(&samp.border_color, ss[D3DSAMP_BORDERCOLOR]);
2557ec681f3Smrg
2567ec681f3Smrg    /* see nine_state.h */
2577ec681f3Smrg    if (idx < NINE_MAX_SAMPLERS_PS)
2587ec681f3Smrg        cso_single_sampler(ctx, PIPE_SHADER_FRAGMENT, idx - NINE_SAMPLER_PS(0), &samp);
2597ec681f3Smrg    else
2607ec681f3Smrg        cso_single_sampler(ctx, PIPE_SHADER_VERTEX, idx - NINE_SAMPLER_VS(0), &samp);
2617ec681f3Smrg}
2627ec681f3Smrg
2637ec681f3Smrgconst enum pipe_format nine_d3d9_to_pipe_format_map[120] =
2647ec681f3Smrg{
2657ec681f3Smrg   [D3DFMT_UNKNOWN]       = PIPE_FORMAT_NONE,
2667ec681f3Smrg   [D3DFMT_R8G8B8]        = PIPE_FORMAT_R8G8B8_UNORM,
2677ec681f3Smrg   [D3DFMT_A8R8G8B8]      = PIPE_FORMAT_B8G8R8A8_UNORM,
2687ec681f3Smrg   [D3DFMT_X8R8G8B8]      = PIPE_FORMAT_B8G8R8X8_UNORM,
2697ec681f3Smrg   [D3DFMT_R5G6B5]        = PIPE_FORMAT_B5G6R5_UNORM,
2707ec681f3Smrg   [D3DFMT_X1R5G5B5]      = PIPE_FORMAT_B5G5R5X1_UNORM,
2717ec681f3Smrg   [D3DFMT_A1R5G5B5]      = PIPE_FORMAT_B5G5R5A1_UNORM,
2727ec681f3Smrg   [D3DFMT_A4R4G4B4]      = PIPE_FORMAT_B4G4R4A4_UNORM,
2737ec681f3Smrg   [D3DFMT_R3G3B2]        = PIPE_FORMAT_B2G3R3_UNORM,
2747ec681f3Smrg   [D3DFMT_A8]            = PIPE_FORMAT_A8_UNORM,
2757ec681f3Smrg   [D3DFMT_A8R3G3B2]      = PIPE_FORMAT_NONE,
2767ec681f3Smrg   [D3DFMT_X4R4G4B4]      = PIPE_FORMAT_B4G4R4X4_UNORM,
2777ec681f3Smrg   [D3DFMT_A2B10G10R10]   = PIPE_FORMAT_R10G10B10A2_UNORM,
2787ec681f3Smrg   [D3DFMT_A8B8G8R8]      = PIPE_FORMAT_R8G8B8A8_UNORM,
2797ec681f3Smrg   [D3DFMT_X8B8G8R8]      = PIPE_FORMAT_R8G8B8X8_UNORM,
2807ec681f3Smrg   [D3DFMT_G16R16]        = PIPE_FORMAT_R16G16_UNORM,
2817ec681f3Smrg   [D3DFMT_A2R10G10B10]   = PIPE_FORMAT_B10G10R10A2_UNORM,
2827ec681f3Smrg   [D3DFMT_A16B16G16R16]  = PIPE_FORMAT_R16G16B16A16_UNORM,
2837ec681f3Smrg   [D3DFMT_A8P8]          = PIPE_FORMAT_NONE,
2847ec681f3Smrg   [D3DFMT_P8]            = PIPE_FORMAT_NONE,
2857ec681f3Smrg   [D3DFMT_L8]            = PIPE_FORMAT_L8_UNORM,
2867ec681f3Smrg   [D3DFMT_A8L8]          = PIPE_FORMAT_L8A8_UNORM,
2877ec681f3Smrg   [D3DFMT_A4L4]          = PIPE_FORMAT_L4A4_UNORM,
2887ec681f3Smrg   [D3DFMT_V8U8]          = PIPE_FORMAT_R8G8_SNORM,
2897ec681f3Smrg   [D3DFMT_L6V5U5]        = PIPE_FORMAT_NONE, /* Should be PIPE_FORMAT_R5SG5SB6U_NORM, but interpretation of the data differs a bit. */
2907ec681f3Smrg   [D3DFMT_X8L8V8U8]      = PIPE_FORMAT_R8SG8SB8UX8U_NORM,
2917ec681f3Smrg   [D3DFMT_Q8W8V8U8]      = PIPE_FORMAT_R8G8B8A8_SNORM,
2927ec681f3Smrg   [D3DFMT_V16U16]        = PIPE_FORMAT_R16G16_SNORM,
2937ec681f3Smrg   [D3DFMT_A2W10V10U10]   = PIPE_FORMAT_R10SG10SB10SA2U_NORM,
2947ec681f3Smrg   [D3DFMT_D16_LOCKABLE]  = PIPE_FORMAT_Z16_UNORM,
2957ec681f3Smrg   [D3DFMT_D32]           = PIPE_FORMAT_Z32_UNORM,
2967ec681f3Smrg   [D3DFMT_D15S1]         = PIPE_FORMAT_NONE,
2977ec681f3Smrg   [D3DFMT_D24S8]         = PIPE_FORMAT_S8_UINT_Z24_UNORM,
2987ec681f3Smrg   [D3DFMT_D24X8]         = PIPE_FORMAT_X8Z24_UNORM,
2997ec681f3Smrg   [D3DFMT_D24X4S4]       = PIPE_FORMAT_NONE,
3007ec681f3Smrg   [D3DFMT_D16]           = PIPE_FORMAT_Z16_UNORM,
3017ec681f3Smrg   [D3DFMT_D32F_LOCKABLE] = PIPE_FORMAT_Z32_FLOAT,
3027ec681f3Smrg   [D3DFMT_D24FS8]        = PIPE_FORMAT_Z32_FLOAT_S8X24_UINT,
3037ec681f3Smrg   [D3DFMT_D32_LOCKABLE]  = PIPE_FORMAT_NONE,
3047ec681f3Smrg   [D3DFMT_S8_LOCKABLE]   = PIPE_FORMAT_NONE,
3057ec681f3Smrg   [D3DFMT_L16]           = PIPE_FORMAT_L16_UNORM,
3067ec681f3Smrg   [D3DFMT_VERTEXDATA]    = PIPE_FORMAT_NONE,
3077ec681f3Smrg   [D3DFMT_INDEX16]       = PIPE_FORMAT_R16_UINT,
3087ec681f3Smrg   [D3DFMT_INDEX32]       = PIPE_FORMAT_R32_UINT,
3097ec681f3Smrg   [D3DFMT_Q16W16V16U16]  = PIPE_FORMAT_R16G16B16A16_SNORM,
3107ec681f3Smrg   [D3DFMT_R16F]          = PIPE_FORMAT_R16_FLOAT,
3117ec681f3Smrg   [D3DFMT_G16R16F]       = PIPE_FORMAT_R16G16_FLOAT,
3127ec681f3Smrg   [D3DFMT_A16B16G16R16F] = PIPE_FORMAT_R16G16B16A16_FLOAT,
3137ec681f3Smrg   [D3DFMT_R32F]          = PIPE_FORMAT_R32_FLOAT,
3147ec681f3Smrg   [D3DFMT_G32R32F]       = PIPE_FORMAT_R32G32_FLOAT,
3157ec681f3Smrg   [D3DFMT_A32B32G32R32F] = PIPE_FORMAT_R32G32B32A32_FLOAT,
3167ec681f3Smrg   [D3DFMT_CxV8U8]        = PIPE_FORMAT_NONE,
3177ec681f3Smrg   [D3DFMT_A1]            = PIPE_FORMAT_NONE,
3187ec681f3Smrg   [D3DFMT_A2B10G10R10_XR_BIAS] = PIPE_FORMAT_NONE,
3197ec681f3Smrg};
3207ec681f3Smrg
3217ec681f3Smrgconst D3DFORMAT nine_pipe_to_d3d9_format_map[PIPE_FORMAT_COUNT] =
3227ec681f3Smrg{
3237ec681f3Smrg   [PIPE_FORMAT_NONE]               = D3DFMT_UNKNOWN,
3247ec681f3Smrg   /* TODO: rename PIPE_FORMAT_R8G8B8_UNORM to PIPE_FORMAT_B8G8R8_UNORM */
3257ec681f3Smrg   [PIPE_FORMAT_R8G8B8_UNORM]       = D3DFMT_R8G8B8,
3267ec681f3Smrg   [PIPE_FORMAT_B8G8R8A8_UNORM]     = D3DFMT_A8R8G8B8,
3277ec681f3Smrg   [PIPE_FORMAT_B8G8R8X8_UNORM]     = D3DFMT_X8R8G8B8,
3287ec681f3Smrg   [PIPE_FORMAT_B5G6R5_UNORM]       = D3DFMT_R5G6B5,
3297ec681f3Smrg   [PIPE_FORMAT_B5G5R5X1_UNORM]     = D3DFMT_X1R5G5B5,
3307ec681f3Smrg   [PIPE_FORMAT_B5G5R5A1_UNORM]     = D3DFMT_A1R5G5B5,
3317ec681f3Smrg   [PIPE_FORMAT_B4G4R4A4_UNORM]     = D3DFMT_A4R4G4B4,
3327ec681f3Smrg   [PIPE_FORMAT_B2G3R3_UNORM]       = D3DFMT_R3G3B2,
3337ec681f3Smrg   [PIPE_FORMAT_A8_UNORM]           = D3DFMT_A8,
3347ec681f3Smrg/* [PIPE_FORMAT_B2G3R3A8_UNORM]     = D3DFMT_A8R3G3B2, */
3357ec681f3Smrg   [PIPE_FORMAT_B4G4R4X4_UNORM]     = D3DFMT_X4R4G4B4,
3367ec681f3Smrg   [PIPE_FORMAT_R10G10B10A2_UNORM]  = D3DFMT_A2B10G10R10,
3377ec681f3Smrg   [PIPE_FORMAT_R8G8B8A8_UNORM]     = D3DFMT_A8B8G8R8,
3387ec681f3Smrg   [PIPE_FORMAT_R8G8B8X8_UNORM]     = D3DFMT_X8B8G8R8,
3397ec681f3Smrg   [PIPE_FORMAT_R16G16_UNORM]       = D3DFMT_G16R16,
3407ec681f3Smrg   [PIPE_FORMAT_B10G10R10A2_UNORM]  = D3DFMT_A2R10G10B10,
3417ec681f3Smrg   [PIPE_FORMAT_R16G16B16A16_UNORM] = D3DFMT_A16B16G16R16,
3427ec681f3Smrg
3437ec681f3Smrg   [PIPE_FORMAT_R8_UINT]            = D3DFMT_P8,
3447ec681f3Smrg   [PIPE_FORMAT_R8A8_UINT]          = D3DFMT_A8P8,
3457ec681f3Smrg
3467ec681f3Smrg   [PIPE_FORMAT_L8_UNORM]           = D3DFMT_L8,
3477ec681f3Smrg   [PIPE_FORMAT_L8A8_UNORM]         = D3DFMT_A8L8,
3487ec681f3Smrg   [PIPE_FORMAT_L4A4_UNORM]         = D3DFMT_A4L4,
3497ec681f3Smrg
3507ec681f3Smrg   [PIPE_FORMAT_R8G8_SNORM]           = D3DFMT_V8U8,
3517ec681f3Smrg/* [PIPE_FORMAT_?]                    = D3DFMT_L6V5U5, */
3527ec681f3Smrg/* [PIPE_FORMAT_?]                    = D3DFMT_X8L8V8U8, */
3537ec681f3Smrg   [PIPE_FORMAT_R8G8B8A8_SNORM]       = D3DFMT_Q8W8V8U8,
3547ec681f3Smrg   [PIPE_FORMAT_R16G16_SNORM]         = D3DFMT_V16U16,
3557ec681f3Smrg   [PIPE_FORMAT_R10SG10SB10SA2U_NORM] = D3DFMT_A2W10V10U10,
3567ec681f3Smrg
3577ec681f3Smrg   [PIPE_FORMAT_YUYV]               = D3DFMT_UYVY,
3587ec681f3Smrg/* [PIPE_FORMAT_YUY2]               = D3DFMT_YUY2, */
3597ec681f3Smrg   [PIPE_FORMAT_DXT1_RGBA]          = D3DFMT_DXT1,
3607ec681f3Smrg/* [PIPE_FORMAT_DXT2_RGBA]          = D3DFMT_DXT2, */
3617ec681f3Smrg   [PIPE_FORMAT_DXT3_RGBA]          = D3DFMT_DXT3,
3627ec681f3Smrg/* [PIPE_FORMAT_DXT4_RGBA]          = D3DFMT_DXT4, */
3637ec681f3Smrg   [PIPE_FORMAT_DXT5_RGBA]          = D3DFMT_DXT5,
3647ec681f3Smrg/* [PIPE_FORMAT_?]                  = D3DFMT_MULTI2_ARGB8, (MET) */
3657ec681f3Smrg   [PIPE_FORMAT_R8G8_B8G8_UNORM]    = D3DFMT_R8G8_B8G8, /* XXX: order */
3667ec681f3Smrg   [PIPE_FORMAT_G8R8_G8B8_UNORM]    = D3DFMT_G8R8_G8B8,
3677ec681f3Smrg
3687ec681f3Smrg   [PIPE_FORMAT_Z16_UNORM]          = D3DFMT_D16_LOCKABLE,
3697ec681f3Smrg   [PIPE_FORMAT_Z32_UNORM]          = D3DFMT_D32,
3707ec681f3Smrg/* [PIPE_FORMAT_Z15_UNORM_S1_UINT]  = D3DFMT_D15S1, */
3717ec681f3Smrg   [PIPE_FORMAT_S8_UINT_Z24_UNORM]  = D3DFMT_D24S8,
3727ec681f3Smrg   [PIPE_FORMAT_X8Z24_UNORM]        = D3DFMT_D24X8,
3737ec681f3Smrg   [PIPE_FORMAT_L16_UNORM]          = D3DFMT_L16,
3747ec681f3Smrg   [PIPE_FORMAT_Z32_FLOAT]          = D3DFMT_D32F_LOCKABLE,
3757ec681f3Smrg/* [PIPE_FORMAT_Z24_FLOAT_S8_UINT]  = D3DFMT_D24FS8, */
3767ec681f3Smrg
3777ec681f3Smrg   [PIPE_FORMAT_R16_UINT]           = D3DFMT_INDEX16,
3787ec681f3Smrg   [PIPE_FORMAT_R32_UINT]           = D3DFMT_INDEX32,
3797ec681f3Smrg   [PIPE_FORMAT_R16G16B16A16_SNORM] = D3DFMT_Q16W16V16U16,
3807ec681f3Smrg
3817ec681f3Smrg   [PIPE_FORMAT_R16_FLOAT]          = D3DFMT_R16F,
3827ec681f3Smrg   [PIPE_FORMAT_R32_FLOAT]          = D3DFMT_R32F,
3837ec681f3Smrg   [PIPE_FORMAT_R16G16_FLOAT]       = D3DFMT_G16R16F,
3847ec681f3Smrg   [PIPE_FORMAT_R32G32_FLOAT]       = D3DFMT_G32R32F,
3857ec681f3Smrg   [PIPE_FORMAT_R16G16B16A16_FLOAT] = D3DFMT_A16B16G16R16F,
3867ec681f3Smrg   [PIPE_FORMAT_R32G32B32A32_FLOAT] = D3DFMT_A32B32G32R32F,
3877ec681f3Smrg
3887ec681f3Smrg/* [PIPE_FORMAT_?]                  = D3DFMT_CxV8U8, */
3897ec681f3Smrg};
390