utils.c 326 KB
Newer Older
1 2 3 4 5 6
/*
 * Utility functions for the WineD3D Library
 *
 * Copyright 2002-2004 Jason Edmeades
 * Copyright 2003-2004 Raphael Junqueira
 * Copyright 2004 Christian Costa
7
 * Copyright 2005 Oliver Stieber
8
 * Copyright 2006-2008 Henri Verbeet
9
 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10
 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
24
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 26
 */

27 28
#include <stdio.h>

29
#include "wined3d_private.h"
30
#include "wined3d_vk.h"
31 32 33

WINE_DEFAULT_DEBUG_CHANNEL(d3d);

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#define WINED3D_FORMAT_FOURCC_BASE (WINED3DFMT_BC7_UNORM_SRGB + 1)

static const struct
{
    enum wined3d_format_id id;
    unsigned int idx;
}
format_index_remap[] =
{
    {WINED3DFMT_UYVY,         WINED3D_FORMAT_FOURCC_BASE},
    {WINED3DFMT_YUY2,         WINED3D_FORMAT_FOURCC_BASE + 1},
    {WINED3DFMT_YV12,         WINED3D_FORMAT_FOURCC_BASE + 2},
    {WINED3DFMT_DXT1,         WINED3D_FORMAT_FOURCC_BASE + 3},
    {WINED3DFMT_DXT2,         WINED3D_FORMAT_FOURCC_BASE + 4},
    {WINED3DFMT_DXT3,         WINED3D_FORMAT_FOURCC_BASE + 5},
    {WINED3DFMT_DXT4,         WINED3D_FORMAT_FOURCC_BASE + 6},
    {WINED3DFMT_DXT5,         WINED3D_FORMAT_FOURCC_BASE + 7},
    {WINED3DFMT_MULTI2_ARGB8, WINED3D_FORMAT_FOURCC_BASE + 8},
    {WINED3DFMT_G8R8_G8B8,    WINED3D_FORMAT_FOURCC_BASE + 9},
    {WINED3DFMT_R8G8_B8G8,    WINED3D_FORMAT_FOURCC_BASE + 10},
    {WINED3DFMT_ATI1N,        WINED3D_FORMAT_FOURCC_BASE + 11},
    {WINED3DFMT_ATI2N,        WINED3D_FORMAT_FOURCC_BASE + 12},
    {WINED3DFMT_INST,         WINED3D_FORMAT_FOURCC_BASE + 13},
    {WINED3DFMT_NVDB,         WINED3D_FORMAT_FOURCC_BASE + 14},
    {WINED3DFMT_NVHU,         WINED3D_FORMAT_FOURCC_BASE + 15},
    {WINED3DFMT_NVHS,         WINED3D_FORMAT_FOURCC_BASE + 16},
    {WINED3DFMT_INTZ,         WINED3D_FORMAT_FOURCC_BASE + 17},
    {WINED3DFMT_RESZ,         WINED3D_FORMAT_FOURCC_BASE + 18},
    {WINED3DFMT_NULL,         WINED3D_FORMAT_FOURCC_BASE + 19},
    {WINED3DFMT_R16,          WINED3D_FORMAT_FOURCC_BASE + 20},
    {WINED3DFMT_AL16,         WINED3D_FORMAT_FOURCC_BASE + 21},
    {WINED3DFMT_NV12,         WINED3D_FORMAT_FOURCC_BASE + 22},
66
    {WINED3DFMT_ATOC,         WINED3D_FORMAT_FOURCC_BASE + 23},
67 68 69 70
};

#define WINED3D_FORMAT_COUNT (WINED3D_FORMAT_FOURCC_BASE + ARRAY_SIZE(format_index_remap))

71
struct wined3d_format_channels
72
{
73
    enum wined3d_format_id id;
74 75
    DWORD red_size, green_size, blue_size, alpha_size;
    DWORD red_offset, green_offset, blue_offset, alpha_offset;
76
    UINT bpp;
77
    BYTE depth_size, stencil_size;
78
    const char *channels;
79 80
};

81
static const struct wined3d_format_channels formats[] =
82
{
83
    /*                                          size            offset
84 85
     *  format id                           r   g   b   a    r   g   b   a    bpp   z   s   channels */
    {WINED3DFMT_UNKNOWN,                    0,  0,  0,  0,   0,  0,  0,  0,    0,   0,  0},
86
    /* FourCC formats */
87 88 89 90 91 92 93 94 95 96 97 98
    {WINED3DFMT_UYVY,                       0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_YUY2,                       0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_YV12,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_NV12,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_DXT1,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_DXT2,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_DXT3,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_DXT4,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_DXT5,                       0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_MULTI2_ARGB8,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_G8R8_G8B8,                  0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_R8G8_B8G8,                  0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
99
    /* Hmm? */
100 101
    {WINED3DFMT_R8G8_SNORM_Cx,              0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_R11G11B10_FLOAT,           11, 11, 10,  0,   0, 11, 22,  0,    4,   0,  0,   "FFF"},
102
    /* Palettized formats */
103 104
    {WINED3DFMT_P8_UINT_A8_UNORM,           0,  0,  0,  8,   0,  0,  0,  8,    2,   0,  0},
    {WINED3DFMT_P8_UINT,                    0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
105
    /* Standard ARGB formats. */
106 107 108 109 110 111 112 113 114 115 116
    {WINED3DFMT_B8G8R8_UNORM,               8,  8,  8,  0,  16,  8,  0,  0,    3,   0,  0,   "uuu"},
    {WINED3DFMT_B5G6R5_UNORM,               5,  6,  5,  0,  11,  5,  0,  0,    2,   0,  0,   "uuu"},
    {WINED3DFMT_B5G5R5X1_UNORM,             5,  5,  5,  0,  10,  5,  0,  0,    2,   0,  0,   "uuuX"},
    {WINED3DFMT_B5G5R5A1_UNORM,             5,  5,  5,  1,  10,  5,  0, 15,    2,   0,  0,   "uuuu"},
    {WINED3DFMT_B4G4R4A4_UNORM,             4,  4,  4,  4,   8,  4,  0, 12,    2,   0,  0,   "uuuu"},
    {WINED3DFMT_B2G3R3_UNORM,               3,  3,  2,  0,   5,  2,  0,  0,    1,   0,  0,   "uuu"},
    {WINED3DFMT_A8_UNORM,                   0,  0,  0,  8,   0,  0,  0,  0,    1,   0,  0,   "u"},
    {WINED3DFMT_B2G3R3A8_UNORM,             3,  3,  2,  8,   5,  2,  0,  8,    2,   0,  0,   "uuuu"},
    {WINED3DFMT_B4G4R4X4_UNORM,             4,  4,  4,  0,   8,  4,  0,  0,    2,   0,  0,   "uuuX"},
    {WINED3DFMT_R8G8B8X8_UNORM,             8,  8,  8,  0,   0,  8, 16,  0,    4,   0,  0,   "uuuX"},
    {WINED3DFMT_B10G10R10A2_UNORM,         10, 10, 10,  2,  20, 10,  0, 30,    4,   0,  0,   "uuuu"},
117
    /* Luminance */
118 119 120 121
    {WINED3DFMT_L8_UNORM,                   0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_L8A8_UNORM,                 0,  0,  0,  8,   0,  0,  0,  8,    2,   0,  0},
    {WINED3DFMT_L4A4_UNORM,                 0,  0,  0,  4,   0,  0,  0,  4,    1,   0,  0},
    {WINED3DFMT_L16_UNORM,                  0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
122
    /* Bump mapping stuff */
123 124 125 126
    {WINED3DFMT_R5G5_SNORM_L6_UNORM,        5,  5,  0,  0,   0,  5,  0,  0,    2,   0,  0},
    {WINED3DFMT_R8G8_SNORM_L8X8_UNORM,      8,  8,  0,  0,   0,  8,  0,  0,    4,   0,  0},
    {WINED3DFMT_R10G11B11_SNORM,           10, 11, 11,  0,   0, 10, 21,  0,    4,   0,  0,   "iii"},
    {WINED3DFMT_R10G10B10_SNORM_A2_UNORM,  10, 10, 10,  2,   0, 10, 20, 30,    4,   0,  0,   "iiiu"},
127
    /* Depth stencil formats */
128 129 130 131 132 133
    {WINED3DFMT_D16_LOCKABLE,               0,  0,  0,  0,   0,  0,  0,  0,    2,  16,  0,   "D"},
    {WINED3DFMT_D32_UNORM,                  0,  0,  0,  0,   0,  0,  0,  0,    4,  32,  0,   "D"},
    {WINED3DFMT_S1_UINT_D15_UNORM,          0,  0,  0,  0,   0,  0,  0,  0,    2,  15,  1,   "SD"},
    {WINED3DFMT_X8D24_UNORM,                0,  0,  0,  0,   0,  0,  0,  0,    4,  24,  0,   "XD"},
    {WINED3DFMT_S4X4_UINT_D24_UNORM,        0,  0,  0,  0,   0,  0,  0,  0,    4,  24,  4,   "SXD"},
    {WINED3DFMT_S8_UINT_D24_FLOAT,          0,  0,  0,  0,   0,  0,  0,  0,    4,  24,  8,   "SD"},
134
    /* Vendor-specific formats */
135 136 137 138 139 140 141 142 143 144
    {WINED3DFMT_ATI1N,                      0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_ATI2N,                      0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_NVDB,                       0,  0,  0,  0,   0,  0,  0,  0,    0,   0,  0},
    {WINED3DFMT_ATOC,                       0,  0,  0,  0,   0,  0,  0,  0,    0,   0,  0},
    {WINED3DFMT_INST,                       0,  0,  0,  0,   0,  0,  0,  0,    0,   0,  0},
    {WINED3DFMT_INTZ,                       0,  0,  0,  0,   0,  0,  0,  0,    4,  24,  8},
    {WINED3DFMT_RESZ,                       0,  0,  0,  0,   0,  0,  0,  0,    0,   0,  0},
    {WINED3DFMT_NVHU,                       0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_NVHS,                       0,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_NULL,                       8,  8,  8,  8,   0,  8, 16, 24,    4,   0,  0},
145
    /* Unsure about them, could not find a Windows driver that supports them */
146 147
    {WINED3DFMT_R16,                       16,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_AL16,                       0,  0,  0, 16,   0,  0,  0, 16,    4,   0,  0},
148
    /* DirectX 10 HDR formats */
149
    {WINED3DFMT_R9G9B9E5_SHAREDEXP,         0,  0,  0,  0,   0,  0,  0,  0,    4,   0,  0},
150
    /* Typeless */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    {WINED3DFMT_R32G32B32A32_TYPELESS,     32, 32, 32, 32,   0, 32, 64, 96,   16,   0,  0},
    {WINED3DFMT_R32G32B32_TYPELESS,        32, 32, 32,  0,   0, 32, 64,  0,   12,   0,  0},
    {WINED3DFMT_R16G16B16A16_TYPELESS,     16, 16, 16, 16,   0, 16, 32, 48,    8,   0,  0},
    {WINED3DFMT_R32G32_TYPELESS,           32, 32,  0,  0,   0, 32,  0,  0,    8,   0,  0},
    {WINED3DFMT_R32G8X24_TYPELESS,         32,  8,  0,  0,   0,  0,  0,  0,    8,   0,  0},
    {WINED3DFMT_R10G10B10A2_TYPELESS,      10, 10, 10,  2,   0, 10, 20, 30,    4,   0,  0},
    {WINED3DFMT_R10G10B10X2_TYPELESS,      10, 10, 10,  0,   0, 10, 20,  0,    4,   0,  0},
    {WINED3DFMT_R8G8B8A8_TYPELESS,          8,  8,  8,  8,   0,  8, 16, 24,    4,   0,  0},
    {WINED3DFMT_R16G16_TYPELESS,           16, 16,  0,  0,   0, 16,  0,  0,    4,   0,  0},
    {WINED3DFMT_R32_TYPELESS,              32,  0,  0,  0,   0,  0,  0,  0,    4,   0,  0},
    {WINED3DFMT_R24G8_TYPELESS,            24,  8,  0,  0,   0,  0,  0,  0,    4,   0,  0},
    {WINED3DFMT_R8G8_TYPELESS,              8,  8,  0,  0,   0,  8,  0,  0,    2,   0,  0},
    {WINED3DFMT_R16_TYPELESS,              16,  0,  0,  0,   0,  0,  0,  0,    2,   0,  0},
    {WINED3DFMT_R8_TYPELESS,                8,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC1_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC2_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC3_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC4_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC5_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC6H_TYPELESS,              0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_BC7_TYPELESS,               0,  0,  0,  0,   0,  0,  0,  0,    1,   0,  0},
    {WINED3DFMT_B8G8R8A8_TYPELESS,          8,  8,  8,  8,  16,  8,  0, 24,    4,   0,  0},
    {WINED3DFMT_B8G8R8X8_TYPELESS,          8,  8,  8,  0,  16,  8,  0,  0,    4,   0,  0},
174 175
};

176
struct wined3d_typed_format_info
177 178 179
{
    enum wined3d_format_id id;
    enum wined3d_format_id typeless_id;
180
    const char *channels;
181 182 183 184
};

/**
 * The last entry for a given typeless format defines its internal format.
185 186 187 188 189 190 191 192
 *
 * u - WINED3D_CHANNEL_TYPE_UNORM
 * i - WINED3D_CHANNEL_TYPE_SNORM
 * U - WINED3D_CHANNEL_TYPE_UINT
 * I - WINED3D_CHANNEL_TYPE_SINT
 * F - WINED3D_CHANNEL_TYPE_FLOAT
 * D - WINED3D_CHANNEL_TYPE_DEPTH
 * S - WINED3D_CHANNEL_TYPE_STENCIL
193
 * X - WINED3D_CHANNEL_TYPE_UNUSED
194
 */
195
static const struct wined3d_typed_format_info typed_formats[] =
196
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    {WINED3DFMT_R32G32B32A32_UINT,        WINED3DFMT_R32G32B32A32_TYPELESS, "UUUU"},
    {WINED3DFMT_R32G32B32A32_SINT,        WINED3DFMT_R32G32B32A32_TYPELESS, "IIII"},
    {WINED3DFMT_R32G32B32A32_FLOAT,       WINED3DFMT_R32G32B32A32_TYPELESS, "FFFF"},
    {WINED3DFMT_R32G32B32_UINT,           WINED3DFMT_R32G32B32_TYPELESS,    "UUU"},
    {WINED3DFMT_R32G32B32_SINT,           WINED3DFMT_R32G32B32_TYPELESS,    "III"},
    {WINED3DFMT_R32G32B32_FLOAT,          WINED3DFMT_R32G32B32_TYPELESS,    "FFF"},
    {WINED3DFMT_R16G16B16A16_UNORM,       WINED3DFMT_R16G16B16A16_TYPELESS, "uuuu"},
    {WINED3DFMT_R16G16B16A16_SNORM,       WINED3DFMT_R16G16B16A16_TYPELESS, "iiii"},
    {WINED3DFMT_R16G16B16A16_UINT,        WINED3DFMT_R16G16B16A16_TYPELESS, "UUUU"},
    {WINED3DFMT_R16G16B16A16_SINT,        WINED3DFMT_R16G16B16A16_TYPELESS, "IIII"},
    {WINED3DFMT_R16G16B16A16_FLOAT,       WINED3DFMT_R16G16B16A16_TYPELESS, "FFFF"},
    {WINED3DFMT_R32G32_UINT,              WINED3DFMT_R32G32_TYPELESS,       "UU"},
    {WINED3DFMT_R32G32_SINT,              WINED3DFMT_R32G32_TYPELESS,       "II"},
    {WINED3DFMT_R32G32_FLOAT,             WINED3DFMT_R32G32_TYPELESS,       "FF"},
211 212 213
    {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_R32G8X24_TYPELESS,     "FXX"},
    {WINED3DFMT_X32_TYPELESS_G8X24_UINT,  WINED3DFMT_R32G8X24_TYPELESS,     "XUX"},
    {WINED3DFMT_D32_FLOAT_S8X24_UINT,     WINED3DFMT_R32G8X24_TYPELESS,     "DSX"},
214 215 216
    {WINED3DFMT_R10G10B10A2_SNORM,        WINED3DFMT_R10G10B10A2_TYPELESS,  "iiii"},
    {WINED3DFMT_R10G10B10A2_UINT,         WINED3DFMT_R10G10B10A2_TYPELESS,  "UUUU"},
    {WINED3DFMT_R10G10B10A2_UNORM,        WINED3DFMT_R10G10B10A2_TYPELESS,  "uuuu"},
217 218
    {WINED3DFMT_R10G10B10X2_SNORM,        WINED3DFMT_R10G10B10X2_TYPELESS,  "iiiX"},
    {WINED3DFMT_R10G10B10X2_UINT,         WINED3DFMT_R10G10B10X2_TYPELESS,  "UUUX"},
219 220
    {WINED3DFMT_R8G8B8A8_UINT,            WINED3DFMT_R8G8B8A8_TYPELESS,     "UUUU"},
    {WINED3DFMT_R8G8B8A8_SINT,            WINED3DFMT_R8G8B8A8_TYPELESS,     "IIII"},
221
    {WINED3DFMT_R8G8B8A8_SNORM,           WINED3DFMT_R8G8B8A8_TYPELESS,     "iiii"},
222 223 224 225 226 227 228
    {WINED3DFMT_R8G8B8A8_UNORM_SRGB,      WINED3DFMT_R8G8B8A8_TYPELESS,     "uuuu"},
    {WINED3DFMT_R8G8B8A8_UNORM,           WINED3DFMT_R8G8B8A8_TYPELESS,     "uuuu"},
    {WINED3DFMT_R16G16_UNORM,             WINED3DFMT_R16G16_TYPELESS,       "uu"},
    {WINED3DFMT_R16G16_SNORM,             WINED3DFMT_R16G16_TYPELESS,       "ii"},
    {WINED3DFMT_R16G16_UINT,              WINED3DFMT_R16G16_TYPELESS,       "UU"},
    {WINED3DFMT_R16G16_SINT,              WINED3DFMT_R16G16_TYPELESS,       "II"},
    {WINED3DFMT_R16G16_FLOAT,             WINED3DFMT_R16G16_TYPELESS,       "FF"},
229
    {WINED3DFMT_D32_FLOAT,                WINED3DFMT_R32_TYPELESS,          "D"},
230
    {WINED3DFMT_R32_FLOAT,                WINED3DFMT_R32_TYPELESS,          "F"},
231 232
    {WINED3DFMT_R32_UINT,                 WINED3DFMT_R32_TYPELESS,          "U"},
    {WINED3DFMT_R32_SINT,                 WINED3DFMT_R32_TYPELESS,          "I"},
233 234
    {WINED3DFMT_R24_UNORM_X8_TYPELESS,    WINED3DFMT_R24G8_TYPELESS,        "uX"},
    {WINED3DFMT_X24_TYPELESS_G8_UINT,     WINED3DFMT_R24G8_TYPELESS,        "XU"},
235 236 237 238 239
    {WINED3DFMT_D24_UNORM_S8_UINT,        WINED3DFMT_R24G8_TYPELESS,        "DS"},
    {WINED3DFMT_R8G8_SNORM,               WINED3DFMT_R8G8_TYPELESS,         "ii"},
    {WINED3DFMT_R8G8_UNORM,               WINED3DFMT_R8G8_TYPELESS,         "uu"},
    {WINED3DFMT_R8G8_UINT,                WINED3DFMT_R8G8_TYPELESS,         "UU"},
    {WINED3DFMT_R8G8_SINT,                WINED3DFMT_R8G8_TYPELESS,         "II"},
240
    {WINED3DFMT_D16_UNORM,                WINED3DFMT_R16_TYPELESS,          "D"},
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    {WINED3DFMT_R16_UNORM,                WINED3DFMT_R16_TYPELESS,          "u"},
    {WINED3DFMT_R16_SNORM,                WINED3DFMT_R16_TYPELESS,          "i"},
    {WINED3DFMT_R16_UINT,                 WINED3DFMT_R16_TYPELESS,          "U"},
    {WINED3DFMT_R16_SINT,                 WINED3DFMT_R16_TYPELESS,          "I"},
    {WINED3DFMT_R16_FLOAT,                WINED3DFMT_R16_TYPELESS,          "F"},
    {WINED3DFMT_R8_UNORM,                 WINED3DFMT_R8_TYPELESS,           "u"},
    {WINED3DFMT_R8_SNORM,                 WINED3DFMT_R8_TYPELESS,           "i"},
    {WINED3DFMT_R8_UINT,                  WINED3DFMT_R8_TYPELESS,           "U"},
    {WINED3DFMT_R8_SINT,                  WINED3DFMT_R8_TYPELESS,           "I"},
    {WINED3DFMT_BC1_UNORM_SRGB,           WINED3DFMT_BC1_TYPELESS,          ""},
    {WINED3DFMT_BC1_UNORM,                WINED3DFMT_BC1_TYPELESS,          ""},
    {WINED3DFMT_BC2_UNORM_SRGB,           WINED3DFMT_BC2_TYPELESS,          ""},
    {WINED3DFMT_BC2_UNORM,                WINED3DFMT_BC2_TYPELESS,          ""},
    {WINED3DFMT_BC3_UNORM_SRGB,           WINED3DFMT_BC3_TYPELESS,          ""},
    {WINED3DFMT_BC3_UNORM,                WINED3DFMT_BC3_TYPELESS,          ""},
    {WINED3DFMT_BC4_UNORM,                WINED3DFMT_BC4_TYPELESS,          ""},
257
    {WINED3DFMT_BC4_SNORM,                WINED3DFMT_BC4_TYPELESS,          ""},
258
    {WINED3DFMT_BC5_UNORM,                WINED3DFMT_BC5_TYPELESS,          ""},
259
    {WINED3DFMT_BC5_SNORM,                WINED3DFMT_BC5_TYPELESS,          ""},
260 261 262 263 264 265 266 267
    {WINED3DFMT_BC6H_UF16,                WINED3DFMT_BC6H_TYPELESS,         ""},
    {WINED3DFMT_BC6H_SF16,                WINED3DFMT_BC6H_TYPELESS,         ""},
    {WINED3DFMT_BC7_UNORM_SRGB,           WINED3DFMT_BC7_TYPELESS,          ""},
    {WINED3DFMT_BC7_UNORM,                WINED3DFMT_BC7_TYPELESS,          ""},
    {WINED3DFMT_B8G8R8A8_UNORM_SRGB,      WINED3DFMT_B8G8R8A8_TYPELESS,     "uuuu"},
    {WINED3DFMT_B8G8R8A8_UNORM,           WINED3DFMT_B8G8R8A8_TYPELESS,     "uuuu"},
    {WINED3DFMT_B8G8R8X8_UNORM_SRGB,      WINED3DFMT_B8G8R8X8_TYPELESS,     "uuuX"},
    {WINED3DFMT_B8G8R8X8_UNORM,           WINED3DFMT_B8G8R8X8_TYPELESS,     "uuuX"},
268 269
};

270
struct wined3d_typeless_format_depth_stencil_info
271
{
272
    enum wined3d_format_id typeless_id;
273 274 275
    enum wined3d_format_id depth_stencil_id;
    enum wined3d_format_id depth_view_id;
    enum wined3d_format_id stencil_view_id;
276
    BOOL separate_depth_view_format;
277 278
};

279
static const struct wined3d_typeless_format_depth_stencil_info typeless_depth_stencil_formats[] =
280
{
281
    {WINED3DFMT_R32G8X24_TYPELESS, WINED3DFMT_D32_FLOAT_S8X24_UINT,
282
            WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_X32_TYPELESS_G8X24_UINT, TRUE},
283
    {WINED3DFMT_R24G8_TYPELESS,    WINED3DFMT_D24_UNORM_S8_UINT,
284 285 286
            WINED3DFMT_R24_UNORM_X8_TYPELESS,    WINED3DFMT_X24_TYPELESS_G8_UINT,    TRUE},
    {WINED3DFMT_R32_TYPELESS,      WINED3DFMT_D32_FLOAT, WINED3DFMT_R32_FLOAT},
    {WINED3DFMT_R16_TYPELESS,      WINED3DFMT_D16_UNORM, WINED3DFMT_R16_UNORM},
287 288
};

289 290 291 292 293 294 295 296 297 298 299 300 301 302
struct wined3d_format_ddi_info
{
    enum wined3d_format_id id;
    D3DDDIFORMAT ddi_format;
};

static const struct wined3d_format_ddi_info ddi_formats[] =
{
    {WINED3DFMT_B8G8R8_UNORM,       D3DDDIFMT_R8G8B8},
    {WINED3DFMT_B8G8R8A8_UNORM,     D3DDDIFMT_A8R8G8B8},
    {WINED3DFMT_B8G8R8X8_UNORM,     D3DDDIFMT_X8R8G8B8},
    {WINED3DFMT_B5G6R5_UNORM,       D3DDDIFMT_R5G6B5},
    {WINED3DFMT_B5G5R5X1_UNORM,     D3DDDIFMT_X1R5G5B5},
    {WINED3DFMT_B5G5R5A1_UNORM,     D3DDDIFMT_A1R5G5B5},
303 304
    {WINED3DFMT_B4G4R4A4_UNORM,     D3DDDIFMT_A4R4G4B4},
    {WINED3DFMT_B4G4R4X4_UNORM,     D3DDDIFMT_X4R4G4B4},
305 306 307
    {WINED3DFMT_P8_UINT,            D3DDDIFMT_P8},
};

308 309
struct wined3d_format_base_flags
{
310
    enum wined3d_format_id id;
311
    unsigned int attrs;
312
    unsigned int caps;
313 314
};

315 316 317
/* The ATI2N format behaves like an uncompressed format in LockRect(), but
 * still needs to use the correct block based calculation for e.g. the
 * resource size. */
318 319
static const struct wined3d_format_base_flags format_base_flags[] =
{
320 321 322 323
    {WINED3DFMT_ATI1N,                 WINED3D_FORMAT_ATTR_MAPPABLE | WINED3D_FORMAT_ATTR_BROKEN_PITCH},
    {WINED3DFMT_ATI2N,                 WINED3D_FORMAT_ATTR_MAPPABLE | WINED3D_FORMAT_ATTR_BROKEN_PITCH},
    {WINED3DFMT_D16_LOCKABLE,          WINED3D_FORMAT_ATTR_MAPPABLE},
    {WINED3DFMT_INTZ,                  WINED3D_FORMAT_ATTR_MAPPABLE},
324 325 326
    {WINED3DFMT_D32_FLOAT,             WINED3D_FORMAT_ATTR_FLOAT},
    {WINED3DFMT_S8_UINT_D24_FLOAT,     WINED3D_FORMAT_ATTR_FLOAT},
    {WINED3DFMT_D32_FLOAT_S8X24_UINT,  WINED3D_FORMAT_ATTR_FLOAT},
327 328 329 330 331
    {WINED3DFMT_INST,                  WINED3D_FORMAT_ATTR_EXTENSION},
    {WINED3DFMT_NULL,                  WINED3D_FORMAT_ATTR_EXTENSION},
    {WINED3DFMT_NVDB,                  WINED3D_FORMAT_ATTR_EXTENSION},
    {WINED3DFMT_ATOC,                  WINED3D_FORMAT_ATTR_EXTENSION},
    {WINED3DFMT_RESZ,                  WINED3D_FORMAT_ATTR_EXTENSION},
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    {WINED3DFMT_R32G32B32A32_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32B32A32_FLOAT,    WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32B32A32_UINT,     WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32B32A32_SINT,     WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_FLOAT,    WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_UNORM,    WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_UINT,     WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_SNORM,    WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R16G16B16A16_SINT,     WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32_TYPELESS,       WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32_FLOAT,          WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32_UINT,           WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32G32_SINT,           WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32_TYPELESS,          WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32_FLOAT,             WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
    {WINED3DFMT_R32_UINT,              WINED3D_FORMAT_ATTR_CAST_TO_BLOCK,
349
        WINED3D_FORMAT_CAP_INDEX_BUFFER},
350
    {WINED3DFMT_R32_SINT,              WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
351
    {WINED3DFMT_R16_UINT,              0,
352
        WINED3D_FORMAT_CAP_INDEX_BUFFER},
353 354 355 356 357
    {WINED3DFMT_R8G8_SNORM,            WINED3D_FORMAT_ATTR_BUMPMAP},
    {WINED3DFMT_R5G5_SNORM_L6_UNORM,   WINED3D_FORMAT_ATTR_BUMPMAP},
    {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
    {WINED3DFMT_R8G8B8A8_SNORM,        WINED3D_FORMAT_ATTR_BUMPMAP},
    {WINED3DFMT_R16G16_SNORM,          WINED3D_FORMAT_ATTR_BUMPMAP},
358 359
};

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
static void rgb888_from_rgb565(WORD rgb565, BYTE *r, BYTE *g, BYTE *b)
{
    BYTE c;

    /* (2⁸ - 1) / (2⁵ - 1) ≈ 2⁸ / 2⁵ + 2⁸ / 2¹⁰
     * (2⁸ - 1) / (2⁶ - 1) ≈ 2⁸ / 2⁶ + 2⁸ / 2¹² */
    c = rgb565 >> 11;
    *r = (c << 3) + (c >> 2);
    c = (rgb565 >> 5) & 0x3f;
    *g = (c << 2) + (c >> 4);
    c = rgb565 & 0x1f;
    *b = (c << 3) + (c >> 2);
}

static void build_dxtn_colour_table(WORD colour0, WORD colour1,
        DWORD colour_table[4], enum wined3d_format_id format_id)
{
    unsigned int i;
    struct
    {
        BYTE r, g, b;
    } c[4];

    rgb888_from_rgb565(colour0, &c[0].r, &c[0].g, &c[0].b);
    rgb888_from_rgb565(colour1, &c[1].r, &c[1].g, &c[1].b);

    if (format_id == WINED3DFMT_BC1_UNORM && colour0 <= colour1)
    {
        c[2].r = (c[0].r + c[1].r) / 2;
        c[2].g = (c[0].g + c[1].g) / 2;
        c[2].b = (c[0].b + c[1].b) / 2;

        c[3].r = 0;
        c[3].g = 0;
        c[3].b = 0;
    }
    else
    {
        for (i = 0; i < 2; ++i)
        {
            c[i + 2].r = (2 * c[i].r + c[1 - i].r) / 3;
            c[i + 2].g = (2 * c[i].g + c[1 - i].g) / 3;
            c[i + 2].b = (2 * c[i].b + c[1 - i].b) / 3;
        }
    }

    for (i = 0; i < 4; ++i)
    {
        colour_table[i] = (c[i].r << 16) | (c[i].g << 8) | c[i].b;
    }
}

412
static void build_bc3_alpha_table(BYTE alpha0, BYTE alpha1, BYTE alpha_table[8])
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
    unsigned int i;

    alpha_table[0] = alpha0;
    alpha_table[1] = alpha1;

    if (alpha0 > alpha1)
    {
        for (i = 0; i < 6; ++i)
        {
            alpha_table[2 + i] = ((6 - i) * alpha0 + (i + 1) * alpha1) / 7;
        }
        return;
    }
    else
    {
        for (i = 0; i < 4; ++i)
        {
            alpha_table[2 + i] = ((4 - i) * alpha0 + (i + 1) * alpha1) / 5;
        }
        alpha_table[6] = 0x00;
        alpha_table[7] = 0xff;
    }
}

static void decompress_dxtn_block(const BYTE *src, BYTE *dst, unsigned int width,
        unsigned int height, unsigned int dst_row_pitch, enum wined3d_format_id format_id)
{
    const UINT64 *s = (const UINT64 *)src;
    BOOL bc1_alpha = FALSE;
    DWORD colour_table[4];
    BYTE alpha_table[8];
    UINT64 alpha_bits;
    DWORD colour_bits;
    unsigned int x, y;
    BYTE colour_idx;
    DWORD *dst_row;
    BYTE alpha;

    if (format_id == WINED3DFMT_BC1_UNORM)
    {
        WORD colour0, colour1;

        alpha_bits = 0;

        colour0 = s[0] & 0xffff;
        colour1 = (s[0] >> 16) & 0xffff;
        colour_bits = (s[0] >> 32) & 0xffffffff;
        build_dxtn_colour_table(colour0, colour1, colour_table, format_id);
        if (colour0 <= colour1)
            bc1_alpha = TRUE;
    }
    else
    {
        alpha_bits = s[0];
        if (format_id == WINED3DFMT_BC3_UNORM)
        {
470
            build_bc3_alpha_table(alpha_bits & 0xff, (alpha_bits >> 8) & 0xff, alpha_table);
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
            alpha_bits >>= 16;
        }

        colour_bits = (s[1] >> 32) & 0xffffffff;
        build_dxtn_colour_table(s[1] & 0xffff, (s[1] >> 16) & 0xffff, colour_table, format_id);
    }

    for (y = 0; y < height; ++y)
    {
        dst_row = (DWORD *)&dst[y * dst_row_pitch];
        for (x = 0; x < width; ++x)
        {
            colour_idx = (colour_bits >> (y * 8 + x * 2)) & 0x3;
            switch (format_id)
            {
                case WINED3DFMT_BC1_UNORM:
                    alpha = bc1_alpha && colour_idx == 3 ? 0x00 : 0xff;
                    break;

                case WINED3DFMT_BC2_UNORM:
                    alpha = (alpha_bits >> (y * 16 + x * 4)) & 0xf;
                    /* (2⁸ - 1) / (2⁴ - 1) ≈ 2⁸ / 2⁴ + 2⁸ / 2⁸ */
                    alpha |= alpha << 4;
                    break;

                case WINED3DFMT_BC3_UNORM:
                    alpha = alpha_table[(alpha_bits >> (y * 12 + x * 3)) & 0x7];
                    break;

                default:
                    alpha = 0xff;
                    break;
            }
            dst_row[x] = (alpha << 24) | colour_table[colour_idx];
        }
    }
}

static void decompress_dxtn(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
        unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth, enum wined3d_format_id format_id)
{
    unsigned int block_byte_count, block_w, block_h;
    const BYTE *src_row, *src_slice = src;
    BYTE *dst_row, *dst_slice = dst;
    unsigned int x, y, z;

    block_byte_count = format_id == WINED3DFMT_BC1_UNORM ? 8 : 16;

    for (z = 0; z < depth; ++z)
    {
        src_row = src_slice;
        dst_row = dst_slice;
        for (y = 0; y < height; y += 4)
        {
            for (x = 0; x < width; x += 4)
            {
                block_w = min(width - x, 4);
                block_h = min(height - y, 4);
                decompress_dxtn_block(&src_row[x * (block_byte_count / 4)],
                        &dst_row[x * 4], block_w, block_h, dst_row_pitch, format_id);
            }
            src_row += src_row_pitch;
            dst_row += dst_row_pitch * 4;
        }
        src_slice += src_slice_pitch;
        dst_slice += dst_slice_pitch;
    }
}

static void decompress_bc3(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
        unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
{
    decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
            dst_slice_pitch, width, height, depth, WINED3DFMT_BC3_UNORM);
}

static void decompress_bc2(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
        unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
{
    decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
            dst_slice_pitch, width, height, depth, WINED3DFMT_BC2_UNORM);
}

static void decompress_bc1(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
        unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
{
    decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
            dst_slice_pitch, width, height, depth, WINED3DFMT_BC1_UNORM);
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static void build_rgtc_colour_table(uint8_t red0, uint8_t red1, uint8_t colour_table[8])
{
    unsigned int i;

    colour_table[0] = red0;
    colour_table[1] = red1;
    if (red0 <= red1)
    {
        for (i = 0; i < 4; ++i)
        {
            colour_table[i + 2] = ((8 - 2 * i) * red0 + (2 + 2 * i) * red1 + 5) / 10;
        }
        colour_table[6] = 0x00;
        colour_table[7] = 0xff;
    }
    else
    {
        for (i = 0; i < 6; ++i)
        {
            colour_table[i + 2] = ((12 - 2 * i) * red0 + (2 + 2 * i) * red1 + 7) / 14;
        }
    }
}

static void decompress_rgtc_block(const uint8_t *src, uint8_t *dst,
        unsigned int width, unsigned int height, unsigned int dst_row_pitch)
{
    const uint64_t *s = (const uint64_t *)src;
    uint8_t red0, red1, red_idx;
    uint8_t colour_table[8];
    unsigned int x, y;
    uint32_t *dst_row;
    uint64_t bits;

    red0 = s[0] & 0xff;
    red1 = (s[0] >> 8) & 0xff;
    bits = s[0] >> 16;
    build_rgtc_colour_table(red0, red1, colour_table);

    for (y = 0; y < height; ++y)
    {
        dst_row = (uint32_t *)&dst[y * dst_row_pitch];
        for (x = 0; x < width; ++x)
        {
            red_idx = (bits >> (y * 12 + x * 3)) & 0x7;
            /* Decompressing to bgra32 is perhaps not ideal for RGTC formats.
             * It's convenient though. */
            dst_row[x] = 0xff000000 | (colour_table[red_idx] << 16);
        }
    }
}

static void decompress_bc4(const uint8_t *src, uint8_t *dst, unsigned int src_row_pitch,
        unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
{
    unsigned int block_w, block_h, x, y, z;
    const uint8_t *src_row, *src_slice;
    uint8_t *dst_row, *dst_slice;

    for (z = 0; z < depth; ++z)
    {
        src_slice = &src[z * src_slice_pitch];
        dst_slice = &dst[z * dst_slice_pitch];
        for (y = 0; y < height; y += 4)
        {
            src_row = &src_slice[(y / 4) * src_row_pitch];
            dst_row = &dst_slice[y * dst_row_pitch];
            for (x = 0; x < width; x += 4)
            {
                block_w = min(width - x, 4);
                block_h = min(height - y, 4);
                decompress_rgtc_block(&src_row[(x / 4) * 8], &dst_row[x * 4], block_w, block_h, dst_row_pitch);
            }
        }
    }
}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
static const struct wined3d_format_decompress_info
{
    enum wined3d_format_id id;
    void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
            unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
            unsigned int width, unsigned int height, unsigned int depth);
}
format_decompress_info[] =
{
    {WINED3DFMT_DXT1,      decompress_bc1},
    {WINED3DFMT_DXT2,      decompress_bc2},
    {WINED3DFMT_DXT3,      decompress_bc2},
    {WINED3DFMT_DXT4,      decompress_bc3},
    {WINED3DFMT_DXT5,      decompress_bc3},
    {WINED3DFMT_BC1_UNORM, decompress_bc1},
    {WINED3DFMT_BC2_UNORM, decompress_bc2},
    {WINED3DFMT_BC3_UNORM, decompress_bc3},
660
    {WINED3DFMT_BC4_UNORM, decompress_bc4},
661 662
};

663
struct wined3d_format_block_info
664
{
665
    enum wined3d_format_id id;
666 667 668
    UINT block_width;
    UINT block_height;
    UINT block_byte_count;
669
    unsigned int attrs;
670 671
};

672
static const struct wined3d_format_block_info format_block_info[] =
673
{
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    {WINED3DFMT_DXT1,               4, 4, 8,  WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_DXT2,               4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_DXT3,               4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_DXT4,               4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_DXT5,               4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC1_UNORM,          4, 4, 8,  WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC2_UNORM,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC3_UNORM,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC4_UNORM,          4, 4, 8,  WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC4_SNORM,          4, 4, 8,  WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC5_UNORM,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC5_SNORM,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC6H_UF16,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC6H_SF16,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_BC7_UNORM,          4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
    {WINED3DFMT_ATI1N,              4, 4, 8,  WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
    {WINED3DFMT_ATI2N,              4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
691 692
    {WINED3DFMT_YUY2,               2, 1, 4,  WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
    {WINED3DFMT_UYVY,               2, 1, 4,  WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
693
    {WINED3DFMT_R9G9B9E5_SHAREDEXP, 1, 1, 4},
694 695
};

696 697
struct wined3d_format_vertex_info
{
698
    enum wined3d_format_id id;
699 700
    enum wined3d_ffp_emit_idx emit_idx;
    GLenum gl_vtx_type;
701
    enum wined3d_gl_extension extension;
702 703 704 705
};

static const struct wined3d_format_vertex_info format_vertex_info[] =
{
706 707 708 709 710 711 712 713 714 715 716 717 718 719
    {WINED3DFMT_R32_FLOAT,          WINED3D_FFP_EMIT_FLOAT1,    GL_FLOAT},
    {WINED3DFMT_R32G32_FLOAT,       WINED3D_FFP_EMIT_FLOAT2,    GL_FLOAT},
    {WINED3DFMT_R32G32B32_FLOAT,    WINED3D_FFP_EMIT_FLOAT3,    GL_FLOAT},
    {WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_FFP_EMIT_FLOAT4,    GL_FLOAT},
    {WINED3DFMT_B8G8R8A8_UNORM,     WINED3D_FFP_EMIT_D3DCOLOR,  GL_UNSIGNED_BYTE},
    {WINED3DFMT_R8G8B8A8_UINT,      WINED3D_FFP_EMIT_UBYTE4,    GL_UNSIGNED_BYTE},
    {WINED3DFMT_R16G16_UINT,        WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_SHORT},
    {WINED3DFMT_R16G16_SINT,        WINED3D_FFP_EMIT_SHORT2,    GL_SHORT},
    {WINED3DFMT_R16G16B16A16_SINT,  WINED3D_FFP_EMIT_SHORT4,    GL_SHORT},
    {WINED3DFMT_R8G8B8A8_UNORM,     WINED3D_FFP_EMIT_UBYTE4N,   GL_UNSIGNED_BYTE},
    {WINED3DFMT_R16G16_SNORM,       WINED3D_FFP_EMIT_SHORT2N,   GL_SHORT},
    {WINED3DFMT_R16G16B16A16_SNORM, WINED3D_FFP_EMIT_SHORT4N,   GL_SHORT},
    {WINED3DFMT_R16G16_UNORM,       WINED3D_FFP_EMIT_USHORT2N,  GL_UNSIGNED_SHORT},
    {WINED3DFMT_R16G16B16A16_UNORM, WINED3D_FFP_EMIT_USHORT4N,  GL_UNSIGNED_SHORT},
720 721
    {WINED3DFMT_R11G11B10_FLOAT,    WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT_10F_11F_11F_REV,
            ARB_VERTEX_TYPE_10F_11F_11F_REV},
722 723 724
    {WINED3DFMT_R10G10B10X2_UINT,   WINED3D_FFP_EMIT_UDEC3,     GL_UNSIGNED_SHORT},
    {WINED3DFMT_R10G10B10X2_SNORM,  WINED3D_FFP_EMIT_DEC3N,     GL_SHORT},
    {WINED3DFMT_R10G10B10A2_UNORM,  WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT_2_10_10_10_REV,
725
            ARB_VERTEX_TYPE_2_10_10_10_REV},
726 727 728 729 730
    /* Without ARB_half_float_vertex we convert these on upload. */
    {WINED3DFMT_R16G16_FLOAT,       WINED3D_FFP_EMIT_FLOAT16_2, GL_FLOAT},
    {WINED3DFMT_R16G16_FLOAT,       WINED3D_FFP_EMIT_FLOAT16_2, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
    {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_FLOAT},
    {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
731 732
    {WINED3DFMT_R8G8B8A8_SNORM,     WINED3D_FFP_EMIT_INVALID,   GL_BYTE},
    {WINED3DFMT_R8G8B8A8_SINT,      WINED3D_FFP_EMIT_INVALID,   GL_BYTE},
733
    {WINED3DFMT_R8G8_UNORM,         WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_BYTE},
734 735 736 737
    {WINED3DFMT_R16G16B16A16_UINT,  WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_SHORT},
    {WINED3DFMT_R8_UNORM,           WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_BYTE},
    {WINED3DFMT_R8_UINT,            WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_BYTE},
    {WINED3DFMT_R8_SINT,            WINED3D_FFP_EMIT_INVALID,   GL_BYTE},
738
    {WINED3DFMT_R8G8_UINT,          WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_BYTE},
739
    {WINED3DFMT_R16_FLOAT,          WINED3D_FFP_EMIT_INVALID,   GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
740 741 742 743 744 745 746 747 748
    {WINED3DFMT_R16_UINT,           WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_SHORT},
    {WINED3DFMT_R16_SINT,           WINED3D_FFP_EMIT_INVALID,   GL_SHORT},
    {WINED3DFMT_R32_UINT,           WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT},
    {WINED3DFMT_R32_SINT,           WINED3D_FFP_EMIT_INVALID,   GL_INT},
    {WINED3DFMT_R32G32_UINT,        WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT},
    {WINED3DFMT_R32G32_SINT,        WINED3D_FFP_EMIT_INVALID,   GL_INT},
    {WINED3DFMT_R32G32B32_UINT,     WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT},
    {WINED3DFMT_R32G32B32A32_UINT,  WINED3D_FFP_EMIT_INVALID,   GL_UNSIGNED_INT},
    {WINED3DFMT_R32G32B32A32_SINT,  WINED3D_FFP_EMIT_INVALID,   GL_INT},
749 750
};

751 752
struct wined3d_format_texture_info
{
753
    enum wined3d_format_id id;
754 755 756 757 758
    GLint gl_internal;
    GLint gl_srgb_internal;
    GLint gl_rt_internal;
    GLint gl_format;
    GLint gl_type;
759
    unsigned int conv_byte_count;
760
    unsigned int caps;
761
    enum wined3d_gl_extension extension;
762 763 764 765 766 767
    void (*upload)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
            unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
            unsigned int width, unsigned int height, unsigned int depth);
    void (*download)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
            unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
            unsigned int width, unsigned int height, unsigned int depth);
768 769 770
    void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
            unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
            unsigned int width, unsigned int height, unsigned int depth);
771
};
772

773 774
static void convert_l4a4_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
775 776 777 778 779
{
    /* WINED3DFMT_L4A4_UNORM exists as an internal gl format, but for some reason there is not
     * format+type combination to load it. Thus convert it to A8L8, then load it
     * with A4L4 internal, but A8L8 format+type
     */
780
    unsigned int x, y, z;
781 782 783
    const unsigned char *Source;
    unsigned char *Dest;

784 785 786 787 788 789 790 791 792
    for (z = 0; z < depth; z++)
    {
        for (y = 0; y < height; y++)
        {
            Source = src + z * src_slice_pitch + y * src_row_pitch;
            Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
                unsigned char color = (*Source++);
793 794
                /* A */ Dest[1] = (color & 0xf0u) << 0;
                /* L */ Dest[0] = (color & 0x0fu) << 4;
795 796
                Dest += 2;
            }
797 798 799 800
        }
    }
}

801 802
static void convert_r5g5_snorm_l6_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
803
{
804
    unsigned int x, y, z;
805 806 807
    unsigned char r_in, g_in, l_in;
    const unsigned short *texel_in;
    unsigned short *texel_out;
808

809 810 811 812
    /* Emulating signed 5 bit values with unsigned 5 bit values has some precision problems by design:
     * E.g. the signed input value 0 becomes 16. GL normalizes it to 16 / 31 = 0.516. We convert it
     * back to a signed value by subtracting 0.5 and multiplying by 2.0. The resulting value is
     * ((16 / 31) - 0.5) * 2.0 = 0.032, which is quite different from the intended result 0.000. */
813
    for (z = 0; z < depth; z++)
814
    {
815
        for (y = 0; y < height; y++)
816
        {
817 818
            texel_out = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
            texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
819 820
            for (x = 0; x < width; x++ )
            {
821 822 823
                l_in = (*texel_in & 0xfc00u) >> 10;
                g_in = (*texel_in & 0x03e0u) >> 5;
                r_in = *texel_in & 0x001fu;
824

825 826 827
                *texel_out = ((r_in + 16) << 11) | (l_in << 5) | (g_in + 16);
                texel_out++;
                texel_in++;
828
            }
829 830 831 832
        }
    }
}

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static void convert_r5g5_snorm_l6_unorm_ext(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
{
    unsigned int x, y, z;
    unsigned char *texel_out, r_out, g_out, r_in, g_in, l_in;
    const unsigned short *texel_in;

    for (z = 0; z < depth; z++)
    {
        for (y = 0; y < height; y++)
        {
            texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
            texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
848 849 850
                l_in = (*texel_in & 0xfc00u) >> 10;
                g_in = (*texel_in & 0x03e0u) >> 5;
                r_in = *texel_in & 0x001fu;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

                r_out = r_in << 3;
                if (!(r_in & 0x10)) /* r > 0 */
                    r_out |= r_in >> 1;

                g_out = g_in << 3;
                if (!(g_in & 0x10)) /* g > 0 */
                    g_out |= g_in >> 1;

                texel_out[0] = r_out;
                texel_out[1] = g_out;
                texel_out[2] = l_in << 1 | l_in >> 5;
                texel_out[3] = 0;

                texel_out += 4;
                texel_in++;
            }
        }
    }
}

872 873
static void convert_r5g5_snorm_l6_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
874
{
875
    unsigned int x, y, z;
876 877
    unsigned char *texel_out, ds_out, dt_out, r_in, g_in, l_in;
    const unsigned short *texel_in;
878 879 880

    /* This makes the gl surface bigger(24 bit instead of 16), but it works with
     * fixed function and shaders without further conversion once the surface is
881 882 883 884 885 886
     * loaded.
     *
     * The difference between this function and convert_r5g5_snorm_l6_unorm_ext
     * is that convert_r5g5_snorm_l6_unorm_ext creates a 32 bit XRGB texture and
     * this function creates a 24 bit DSDT_MAG texture. Trying to load a DSDT_MAG
     * internal with a 32 bit DSDT_MAG_INTENSITY or DSDT_MAG_VIB format fails. */
887 888 889 890
    for (z = 0; z < depth; z++)
    {
        for (y = 0; y < height; y++)
        {
891 892
            texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
            texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
893 894
            for (x = 0; x < width; x++ )
            {
895 896 897
                l_in = (*texel_in & 0xfc00u) >> 10;
                g_in = (*texel_in & 0x03e0u) >> 5;
                r_in = *texel_in & 0x001fu;
898

899 900 901
                ds_out = r_in << 3;
                if (!(r_in & 0x10)) /* r > 0 */
                    ds_out |= r_in >> 1;
902

903 904 905 906 907 908 909 910 911 912
                dt_out = g_in << 3;
                if (!(g_in & 0x10)) /* g > 0 */
                    dt_out |= g_in >> 1;

                texel_out[0] = ds_out;
                texel_out[1] = dt_out;
                texel_out[2] = l_in << 1 | l_in >> 5;

                texel_out += 3;
                texel_in++;
913
            }
914 915 916 917
        }
    }
}

918 919
static void convert_r8g8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
920
{
921
    unsigned int x, y, z;
922 923 924
    const short *Source;
    unsigned char *Dest;

925
    for (z = 0; z < depth; z++)
926
    {
927
        for (y = 0; y < height; y++)
928
        {
929 930 931 932 933 934 935 936 937 938
            Source = (const short *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
                const short color = (*Source++);
                /* B */ Dest[0] = 0xff;
                /* G */ Dest[1] = (color >> 8) + 128; /* V */
                /* R */ Dest[2] = (color & 0xff) + 128;      /* U */
                Dest += 3;
            }
939 940 941 942
        }
    }
}

943 944
static void convert_r8g8_snorm_l8x8_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
945
{
946
    unsigned int x, y, z;
947 948 949 950 951 952 953
    const DWORD *Source;
    unsigned char *Dest;

    /* Doesn't work correctly with the fixed function pipeline, but can work in
     * shaders if the shader is adjusted. (There's no use for this format in gl's
     * standard fixed function pipeline anyway).
     */
954
    for (z = 0; z < depth; z++)
955
    {
956
        for (y = 0; y < height; y++)
957
        {
958 959 960 961 962 963 964 965 966 967
            Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
                LONG color = (*Source++);
                /* B */ Dest[0] = ((color >> 16) & 0xff);       /* L */
                /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
                /* R */ Dest[2] = (color         & 0xff) + 128; /* U */
                Dest += 4;
            }
968 969 970 971
        }
    }
}

972 973
static void convert_r8g8_snorm_l8x8_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
974
{
975
    unsigned int x, y, z;
976 977 978 979 980 981
    const DWORD *Source;
    unsigned char *Dest;

    /* This implementation works with the fixed function pipeline and shaders
     * without further modification after converting the surface.
     */
982
    for (z = 0; z < depth; z++)
983
    {
984
        for (y = 0; y < height; y++)
985
        {
986 987 988 989 990 991 992 993 994 995 996
            Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
                LONG color = (*Source++);
                /* L */ Dest[2] = ((color >> 16) & 0xff);   /* L */
                /* V */ Dest[1] = ((color >> 8 ) & 0xff);   /* V */
                /* U */ Dest[0] = (color         & 0xff);   /* U */
                /* I */ Dest[3] = 255;                      /* X */
                Dest += 4;
            }
997 998 999 1000
        }
    }
}

1001 1002
static void convert_r8g8b8a8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1003
{
1004
    unsigned int x, y, z;
1005 1006 1007
    const DWORD *Source;
    unsigned char *Dest;

1008
    for (z = 0; z < depth; z++)
1009
    {
1010
        for (y = 0; y < height; y++)
1011
        {
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
            Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
            for (x = 0; x < width; x++ )
            {
                LONG color = (*Source++);
                /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
                /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
                /* R */ Dest[2] = (color         & 0xff) + 128; /* U */
                /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
                Dest += 4;
            }
1023 1024 1025 1026
        }
    }
}

1027 1028
static void convert_r16g16_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1029
{
1030
    unsigned int x, y, z;
1031 1032 1033
    const DWORD *Source;
    unsigned short *Dest;

1034
    for (z = 0; z < depth; z++)
1035
    {
1036
        for (y = 0; y < height; y++)
1037
        {
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
            Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
            for (x = 0; x < width; x++ )
            {
                const DWORD color = (*Source++);
                /* B */ Dest[0] = 0xffff;
                /* G */ Dest[1] = (color >> 16) + 32768; /* V */
                /* R */ Dest[2] = (color & 0xffff) + 32768; /* U */
                Dest += 3;
            }
1048 1049 1050 1051
        }
    }
}

1052 1053
static void convert_r16g16(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1054
{
1055
    unsigned int x, y, z;
1056 1057 1058
    const WORD *Source;
    WORD *Dest;

1059
    for (z = 0; z < depth; z++)
1060
    {
1061
        for (y = 0; y < height; y++)
1062
        {
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
            Source = (const WORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = (WORD *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
            for (x = 0; x < width; x++ )
            {
                WORD green = (*Source++);
                WORD red = (*Source++);
                Dest[0] = green;
                Dest[1] = red;
                /* Strictly speaking not correct for R16G16F, but it doesn't matter because the
                 * shader overwrites it anyway */
                Dest[2] = 0xffff;
                Dest += 3;
            }
1076 1077 1078 1079
        }
    }
}

1080 1081
static void convert_r32g32_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1082
{
1083
    unsigned int x, y, z;
1084 1085 1086
    const float *Source;
    float *Dest;

1087
    for (z = 0; z < depth; z++)
1088
    {
1089
        for (y = 0; y < height; y++)
1090
        {
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
            Source = (const float *)(src + z * src_slice_pitch + y * src_row_pitch);
            Dest = (float *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
            for (x = 0; x < width; x++ )
            {
                float green = (*Source++);
                float red = (*Source++);
                Dest[0] = green;
                Dest[1] = red;
                Dest[2] = 1.0f;
                Dest += 3;
            }
1102 1103 1104 1105
        }
    }
}

1106 1107
static void convert_s8_uint_d24_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
        UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1108
{
1109
    unsigned int x, y, z;
1110

1111
    for (z = 0; z < depth; z++)
1112
    {
1113
        for (y = 0; y < height; ++y)
1114
        {
1115 1116 1117 1118 1119 1120
            const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            float *dest_f = (float *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
            DWORD *dest_s = (DWORD *)dest_f;

            for (x = 0; x < width; ++x)
            {
1121
                dest_f[x * 2] = float_24_to_32((source[x] & 0xffffff00u) >> 8);
1122 1123
                dest_s[x * 2 + 1] = source[x] & 0xff;
            }
1124 1125 1126 1127
        }
    }
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static void x8_d24_unorm_upload(const BYTE *src, BYTE *dst,
        unsigned int src_row_pitch, unsigned int src_slice_pitch,
        unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
{
    unsigned int x, y, z;

    for (z = 0; z < depth; ++z)
    {
        for (y = 0; y < height; ++y)
        {
            const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);

            for (x = 0; x < width; ++x)
            {
                dest[x] = source[x] << 8 | ((source[x] >> 16) & 0xff);
            }
        }
    }
}

static void x8_d24_unorm_download(const BYTE *src, BYTE *dst,
        unsigned int src_row_pitch, unsigned int src_slice_pitch,
        unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
        unsigned int width, unsigned int height, unsigned int depth)
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
{
    unsigned int x, y, z;

    for (z = 0; z < depth; ++z)
    {
        for (y = 0; y < height; ++y)
        {
            const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
            DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);

            for (x = 0; x < width; ++x)
            {
1166
                dest[x] = source[x] >> 8;
1167 1168 1169 1170 1171
            }
        }
    }
}

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
static BOOL color_in_range(const struct wined3d_color_key *color_key, DWORD color)
{
    /* FIXME: Is this really how color keys are supposed to work? I think it
     * makes more sense to compare the individual channels. */
    return color >= color_key->color_space_low_value
            && color <= color_key->color_space_high_value;
}

static void convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
        BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1182
        const struct wined3d_color_key *color_key)
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
{
    const WORD *src_row;
    unsigned int x, y;
    WORD *dst_row;

    for (y = 0; y < height; ++y)
    {
        src_row = (WORD *)&src[src_pitch * y];
        dst_row = (WORD *)&dst[dst_pitch * y];
        for (x = 0; x < width; ++x)
        {
            WORD src_color = src_row[x];
            if (!color_in_range(color_key, src_color))
1196
                dst_row[x] = 0x8000u | ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1197
            else
1198
                dst_row[x] = ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1199 1200 1201 1202 1203 1204
        }
    }
}

static void convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
        BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1205
        const struct wined3d_color_key *color_key)
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
{
    const WORD *src_row;
    unsigned int x, y;
    WORD *dst_row;

    for (y = 0; y < height; ++y)
    {
        src_row = (WORD *)&src[src_pitch * y];
        dst_row = (WORD *)&dst[dst_pitch * y];
        for (x = 0; x < width; ++x)
        {
            WORD src_color = src_row[x];
            if (color_in_range(color_key, src_color))
                dst_row[x] = src_color & ~0x8000;
            else
                dst_row[x] = src_color | 0x8000;
        }
    }
}

static void convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
        BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1228
        const struct wined3d_color_key *color_key)
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
{
    const BYTE *src_row;
    unsigned int x, y;
    DWORD *dst_row;

    for (y = 0; y < height; ++y)
    {
        src_row = &src[src_pitch * y];
        dst_row = (DWORD *)&dst[dst_pitch * y];
        for (x = 0; x < width; ++x)
        {
            DWORD src_color = (src_row[x * 3 + 2] << 16) | (src_row[x * 3 + 1] << 8) | src_row[x * 3];
            if (!color_in_range(color_key, src_color))
                dst_row[x] = src_color | 0xff000000;
        }
    }
}

static void convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
        BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1249
        const struct wined3d_color_key *color_key)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
{
    const DWORD *src_row;
    unsigned int x, y;
    DWORD *dst_row;

    for (y = 0; y < height; ++y)
    {
        src_row = (DWORD *)&src[src_pitch * y];
        dst_row = (DWORD *)&dst[dst_pitch * y];
        for (x = 0; x < width; ++x)
        {
            DWORD src_color = src_row[x];
            if (color_in_range(color_key, src_color))
                dst_row[x] = src_color & ~0xff000000;
            else
                dst_row[x] = src_color | 0xff000000;
        }
    }
}

static void convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
        BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1272
        const struct wined3d_color_key *color_key)
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
{
    const DWORD *src_row;
    unsigned int x, y;
    DWORD *dst_row;

    for (y = 0; y < height; ++y)
    {
        src_row = (DWORD *)&src[src_pitch * y];
        dst_row = (DWORD *)&dst[dst_pitch * y];
        for (x = 0; x < width; ++x)
        {
            DWORD src_color = src_row[x];
            if (color_in_range(color_key, src_color))
                src_color &= ~0xff000000;
            dst_row[x] = src_color;
        }
    }
}

const struct wined3d_color_key_conversion * wined3d_format_get_color_key_conversion(
        const struct wined3d_texture *texture, BOOL need_alpha_ck)
{
    const struct wined3d_format *format = texture->resource.format;
    unsigned int i;

    static const struct
    {
        enum wined3d_format_id src_format;
        struct wined3d_color_key_conversion conversion;
    }
    color_key_info[] =
    {
        {WINED3DFMT_B5G6R5_UNORM,   {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key   }},
        {WINED3DFMT_B5G5R5X1_UNORM, {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key }},
        {WINED3DFMT_B8G8R8_UNORM,   {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key   }},
        {WINED3DFMT_B8G8R8X8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key }},
        {WINED3DFMT_B8G8R8A8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key }},
    };

1312
    if (need_alpha_ck && (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY))
1313
    {
1314
        for (i = 0; i < ARRAY_SIZE(color_key_info); ++i)
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
        {
            if (color_key_info[i].src_format == format->id)
                return &color_key_info[i].conversion;
        }

        FIXME("Color-keying not supported with format %s.\n", debug_d3dformat(format->id));
    }

    return NULL;
}

1326 1327 1328
/* We intentionally don't support WINED3DFMT_D32_UNORM. No hardware driver
 * supports it, and applications get confused when we do.
 *
1329
 * The following formats explicitly don't have WINED3D_FORMAT_CAP_TEXTURE set:
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
 *
 * These are never supported on native.
 *     WINED3DFMT_B8G8R8_UNORM
 *     WINED3DFMT_B2G3R3_UNORM
 *     WINED3DFMT_L4A4_UNORM
 *     WINED3DFMT_S1_UINT_D15_UNORM
 *     WINED3DFMT_S4X4_UINT_D24_UNORM
 *
 * Only some Geforce/Voodoo3/G400 cards offer 8-bit textures in case of ddraw.
 * Since it is not widely available, don't offer it. Further no Windows driver
 * offers WINED3DFMT_P8_UINT_A8_NORM, so don't offer it either.
 *     WINED3DFMT_P8_UINT
 *     WINED3DFMT_P8_UINT_A8_UNORM
 *
 * These formats seem to be similar to the HILO formats in
 * GL_NV_texture_shader. NVHU is said to be GL_UNSIGNED_HILO16,
 * NVHS GL_SIGNED_HILO16. Rumours say that D3D computes a 3rd channel
 * similarly to D3DFMT_CxV8U8 (So NVHS could be called D3DFMT_CxV16U16). ATI
 * refused to support formats which can easily be emulated with pixel shaders,
 * so applications have to deal with not having NVHS and NVHU.
 *     WINED3DFMT_NVHU
 *     WINED3DFMT_NVHS */
1352 1353
static const struct wined3d_format_texture_info format_texture_info[] =
{
1354 1355
    /* format id                        gl_internal                       gl_srgb_internal                      gl_rt_internal
            gl_format                   gl_type                           conv_byte_count
1356
            caps
1357
            extension                   upload                            download */
1358
    /* FourCC formats */
1359 1360 1361
    /* GL_APPLE_ycbcr_422 claims that its '2YUV' format, which is supported via the UNSIGNED_SHORT_8_8_REV_APPLE type
     * is equivalent to 'UYVY' format on Windows, and the 'YUVS' via UNSIGNED_SHORT_8_8_APPLE equates to 'YUY2'. The
     * d3d9 test however shows that the opposite is true. Since the extension is from 2002, it predates the x86 based
1362
     * Macs, so probably the endianness differs. This could be tested as soon as we have a Windows and MacOS on a big
1363 1364
     * endian machine
     */
1365 1366
    {WINED3DFMT_UYVY,                   GL_RG8,                           GL_RG8,                                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1367
            WINED3D_FORMAT_CAP_FILTERING,
1368
            ARB_TEXTURE_RG,             NULL},
1369
    {WINED3DFMT_UYVY,                   GL_LUMINANCE8_ALPHA8,             GL_LUMINANCE8_ALPHA8,                   0,
1370
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 0,
1371
            WINED3D_FORMAT_CAP_FILTERING,
1372
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1373 1374
    {WINED3DFMT_UYVY,                   GL_RGB_RAW_422_APPLE,             GL_RGB_RAW_422_APPLE,                   0,
            GL_RGB_422_APPLE,           GL_UNSIGNED_SHORT_8_8_APPLE,      0,
1375
            WINED3D_FORMAT_CAP_FILTERING,
1376
            APPLE_RGB_422,              NULL},
1377
    {WINED3DFMT_UYVY,                   GL_RGB,                           GL_RGB,                                 0,
1378
            GL_YCBCR_422_APPLE,         GL_UNSIGNED_SHORT_8_8_APPLE,      0,
1379
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_FILTERING,
1380
            APPLE_YCBCR_422,            NULL},
1381 1382
    {WINED3DFMT_YUY2,                   GL_RG8,                           GL_RG8,                                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1383
            WINED3D_FORMAT_CAP_FILTERING,
1384
            ARB_TEXTURE_RG,             NULL},
1385
    {WINED3DFMT_YUY2,                   GL_LUMINANCE8_ALPHA8,             GL_LUMINANCE8_ALPHA8,                   0,
1386
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 0,
1387
            WINED3D_FORMAT_CAP_FILTERING,
1388
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1389 1390
    {WINED3DFMT_YUY2,                   GL_RGB_RAW_422_APPLE,             GL_RGB_RAW_422_APPLE,                   0,
            GL_RGB_422_APPLE,           GL_UNSIGNED_SHORT_8_8_REV_APPLE,  0,
1391
            WINED3D_FORMAT_CAP_FILTERING,
1392
            APPLE_RGB_422,              NULL},
1393
    {WINED3DFMT_YUY2,                   GL_RGB,                           GL_RGB,                                 0,
1394
            GL_YCBCR_422_APPLE,         GL_UNSIGNED_SHORT_8_8_REV_APPLE,  0,
1395
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_FILTERING,
1396
            APPLE_YCBCR_422,            NULL},
1397 1398
    {WINED3DFMT_YV12,                   GL_R8,                            GL_R8,                                  0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1399
            WINED3D_FORMAT_CAP_FILTERING,
1400
            ARB_TEXTURE_RG,             NULL},
1401
    {WINED3DFMT_YV12,                   GL_ALPHA8,                        GL_ALPHA8,                              0,
1402
            GL_ALPHA,                   GL_UNSIGNED_BYTE,                 0,
1403
            WINED3D_FORMAT_CAP_FILTERING,
1404 1405 1406
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
    {WINED3DFMT_NV12,                   GL_R8,                            GL_R8,                                  0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1407
            WINED3D_FORMAT_CAP_FILTERING,
1408
            ARB_TEXTURE_RG,             NULL},
1409
    {WINED3DFMT_NV12,                   GL_ALPHA8,                        GL_ALPHA8,                              0,
1410
            GL_ALPHA,                   GL_UNSIGNED_BYTE,                 0,
1411
            WINED3D_FORMAT_CAP_FILTERING,
1412
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1413
    {WINED3DFMT_DXT1,                   GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1414
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1415 1416
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1417
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1418
    {WINED3DFMT_DXT2,                   GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1419
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1420 1421
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1422
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1423
    {WINED3DFMT_DXT3,                   GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1424
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1425 1426
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1427
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1428
    {WINED3DFMT_DXT4,                   GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1429
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1430 1431
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1432
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1433
    {WINED3DFMT_DXT5,                   GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1434
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1435 1436
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1437
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1438
    {WINED3DFMT_BC1_UNORM,              GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1439
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1440
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1441
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1442
    {WINED3DFMT_BC2_UNORM,              GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1443
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1444
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1445
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1446
    {WINED3DFMT_BC3_UNORM,              GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1447
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1448
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1449
            EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1450 1451
    {WINED3DFMT_BC4_UNORM,              GL_COMPRESSED_RED_RGTC1,          GL_COMPRESSED_RED_RGTC1,                0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1452
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1453
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1454 1455
    {WINED3DFMT_BC4_SNORM,              GL_COMPRESSED_SIGNED_RED_RGTC1,   GL_COMPRESSED_SIGNED_RED_RGTC1,         0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1456
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1457
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1458 1459
    {WINED3DFMT_BC5_UNORM,              GL_COMPRESSED_RG_RGTC2,           GL_COMPRESSED_RG_RGTC2,                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1460
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1461
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1462 1463
    {WINED3DFMT_BC5_SNORM,              GL_COMPRESSED_SIGNED_RG_RGTC2,    GL_COMPRESSED_SIGNED_RG_RGTC2,          0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1464
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1465
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1466 1467
    {WINED3DFMT_BC6H_UF16,              GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, 0,
            GL_RGB,                     GL_UNSIGNED_BYTE,                 0,
1468
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1469 1470 1471
            ARB_TEXTURE_COMPRESSION_BPTC, NULL},
    {WINED3DFMT_BC6H_SF16,              GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, 0,
            GL_RGB,                     GL_UNSIGNED_BYTE,                 0,
1472
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1473
            ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1474 1475
    {WINED3DFMT_BC7_UNORM,              GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB, 0,
            GL_RGBA,                    GL_UNSIGNED_BYTE,                 0,
1476
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1477
            ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1478
    /* IEEE formats */
1479
    {WINED3DFMT_R32_FLOAT,              GL_RGB32F_ARB,                    GL_RGB32F_ARB,                          0,
1480
            GL_RED,                     GL_FLOAT,                         0,
1481
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1482
            ARB_TEXTURE_FLOAT,          NULL},
1483
    {WINED3DFMT_R32_FLOAT,              GL_R32F,                          GL_R32F,                                0,
1484
            GL_RED,                     GL_FLOAT,                         0,
1485
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1486
            ARB_TEXTURE_RG,             NULL},
1487
    {WINED3DFMT_R32G32_FLOAT,           GL_RGB32F_ARB,                    GL_RGB32F_ARB,                          0,
1488
            GL_RGB,                     GL_FLOAT,                         12,
1489
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1490
            ARB_TEXTURE_FLOAT,          convert_r32g32_float},
1491
    {WINED3DFMT_R32G32_FLOAT,           GL_RG32F,                         GL_RG32F,                               0,
1492
            GL_RG,                      GL_FLOAT,                         0,
1493
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1494
            ARB_TEXTURE_RG,             NULL},
1495 1496
    {WINED3DFMT_R32G32B32_FLOAT,        GL_RGB32F,                        GL_RGB32F,                              0,
            GL_RGB,                     GL_FLOAT,                         0,
1497
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1498
            ARB_TEXTURE_FLOAT,          NULL},
1499
    {WINED3DFMT_R32G32B32A32_FLOAT,     GL_RGBA32F_ARB,                   GL_RGBA32F_ARB,                         0,
1500
            GL_RGBA,                    GL_FLOAT,                         0,
1501
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1502
            ARB_TEXTURE_FLOAT,          NULL},
1503
    /* Float */
1504
    {WINED3DFMT_R16_FLOAT,              GL_RGB16F_ARB,                    GL_RGB16F_ARB,                          0,
1505
            GL_RED,                     GL_HALF_FLOAT_ARB,                0,
1506
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1507
            ARB_TEXTURE_FLOAT,          NULL},
1508
    {WINED3DFMT_R16_FLOAT,              GL_R16F,                          GL_R16F,                                0,
1509
            GL_RED,                     GL_HALF_FLOAT_ARB,                0,
1510
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1511
            ARB_TEXTURE_RG,             NULL},
1512
    {WINED3DFMT_R16G16_FLOAT,           GL_RGB16F_ARB,                    GL_RGB16F_ARB,                          0,
1513
            GL_RGB,                     GL_HALF_FLOAT_ARB,                6,
1514
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1515
            ARB_TEXTURE_FLOAT,          convert_r16g16},
1516
    {WINED3DFMT_R16G16_FLOAT,           GL_RG16F,                         GL_RG16F,                               0,
1517
            GL_RG,                      GL_HALF_FLOAT_ARB,                0,
1518
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1519
            ARB_TEXTURE_RG,             NULL},
1520
    {WINED3DFMT_R16G16B16A16_FLOAT,     GL_RGBA16F_ARB,                   GL_RGBA16F_ARB,                         0,
1521
            GL_RGBA,                    GL_HALF_FLOAT_ARB,                0,
1522 1523
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_RENDERTARGET
            | WINED3D_FORMAT_CAP_VTF,
1524
            ARB_TEXTURE_FLOAT,          NULL},
1525 1526
    {WINED3DFMT_R11G11B10_FLOAT,        GL_R11F_G11F_B10F,                GL_R11F_G11F_B10F,                      0,
            GL_RGB,                     GL_UNSIGNED_INT_10F_11F_11F_REV,  0,
1527
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_RENDERTARGET,
1528
            EXT_PACKED_FLOAT},
1529
    /* Palettized formats */
1530
    {WINED3DFMT_P8_UINT,                GL_R8,                            GL_R8,                                  0,
1531 1532 1533
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
            0,
            ARB_TEXTURE_RG,             NULL},
1534
    {WINED3DFMT_P8_UINT,                GL_ALPHA8,                        GL_ALPHA8,                              0,
1535
            GL_ALPHA,                   GL_UNSIGNED_BYTE,                 0,
1536
            0,
1537
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1538
    /* Standard ARGB formats */
1539
    {WINED3DFMT_B8G8R8_UNORM,           GL_RGB8,                          GL_RGB8,                                0,
1540
            GL_BGR,                     GL_UNSIGNED_BYTE,                 0,
1541
            WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING | WINED3D_FORMAT_CAP_RENDERTARGET,
1542
            WINED3D_GL_EXT_NONE,        NULL},
1543
    {WINED3DFMT_B8G8R8A8_UNORM,         GL_RGBA8,                         GL_SRGB8_ALPHA8_EXT,                    0,
1544
            GL_BGRA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1545 1546 1547
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE
            | WINED3D_FORMAT_CAP_VTF,
1548
            WINED3D_GL_EXT_NONE,        NULL},
1549
    {WINED3DFMT_B8G8R8X8_UNORM,         GL_RGB8,                          GL_SRGB8_EXT,                           0,
1550
            GL_BGRA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1551 1552
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE,
1553
            WINED3D_GL_EXT_NONE,        NULL},
1554
    {WINED3DFMT_B5G6R5_UNORM,           GL_RGB5,                          GL_SRGB8_EXT,                     GL_RGB8,
1555
            GL_RGB,                     GL_UNSIGNED_SHORT_5_6_5,          0,
1556 1557
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ,
1558
            WINED3D_GL_EXT_NONE,        NULL},
1559
    {WINED3DFMT_B5G6R5_UNORM,           GL_RGB565,                        GL_SRGB8_EXT,                     GL_RGB8,
1560
            GL_RGB,                     GL_UNSIGNED_SHORT_5_6_5,          0,
1561 1562
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ,
1563
            ARB_ES2_COMPATIBILITY,      NULL},
1564
    {WINED3DFMT_B5G5R5X1_UNORM,         GL_RGB5,                          GL_RGB5,                                0,
1565
            GL_BGRA,                    GL_UNSIGNED_SHORT_1_5_5_5_REV,    0,
1566 1567
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1568
            WINED3D_GL_EXT_NONE,        NULL},
1569
    {WINED3DFMT_B5G5R5A1_UNORM,         GL_RGB5_A1,                       GL_RGB5_A1,                             0,
1570
            GL_BGRA,                    GL_UNSIGNED_SHORT_1_5_5_5_REV,    0,
1571
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1572
            WINED3D_GL_EXT_NONE,        NULL},
1573
    {WINED3DFMT_B4G4R4A4_UNORM,         GL_RGBA4,                         GL_SRGB8_ALPHA8_EXT,                    0,
1574
            GL_BGRA,                    GL_UNSIGNED_SHORT_4_4_4_4_REV,    0,
1575 1576
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1577
            WINED3D_GL_EXT_NONE,        NULL},
1578
    {WINED3DFMT_B2G3R3_UNORM,           GL_R3_G3_B2,                      GL_R3_G3_B2,                            0,
1579
            GL_RGB,                     GL_UNSIGNED_BYTE_3_3_2,           0,
1580
            WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1581
            WINED3D_GL_EXT_NONE,        NULL},
1582 1583
    {WINED3DFMT_R8_UNORM,               GL_R8,                            GL_R8,                                  0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1584 1585
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1586
            ARB_TEXTURE_RG,             NULL},
1587 1588
    {WINED3DFMT_A8_UNORM,               GL_R8,                            GL_R8,                                  0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1589 1590
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1591
            ARB_TEXTURE_RG,             NULL},
1592
    {WINED3DFMT_A8_UNORM,               GL_ALPHA8,                        GL_ALPHA8,                              0,
1593
            GL_ALPHA,                   GL_UNSIGNED_BYTE,                 0,
1594 1595
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1596
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1597
    {WINED3DFMT_B4G4R4X4_UNORM,         GL_RGB4,                          GL_RGB4,                                0,
1598
            GL_BGRA,                    GL_UNSIGNED_SHORT_4_4_4_4_REV,    0,
1599
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1600
            WINED3D_GL_EXT_NONE,        NULL},
1601 1602
    {WINED3DFMT_R10G10B10A2_UINT,       GL_RGB10_A2UI,                    GL_RGB10_A2UI,                          0,
            GL_RGBA_INTEGER,            GL_UNSIGNED_INT_2_10_10_10_REV,   0,
1603
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1604
            ARB_TEXTURE_RGB10_A2UI,     NULL},
1605
    {WINED3DFMT_R10G10B10A2_UNORM,      GL_RGB10_A2,                      GL_RGB10_A2,                            0,
1606
            GL_RGBA,                    GL_UNSIGNED_INT_2_10_10_10_REV,   0,
1607 1608
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1609
            WINED3D_GL_EXT_NONE,        NULL},
1610
    {WINED3DFMT_R8G8B8A8_UNORM,         GL_RGBA8,                         GL_SRGB8_ALPHA8_EXT,                    0,
1611
            GL_RGBA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1612 1613 1614
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET |  WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE
            | WINED3D_FORMAT_CAP_VTF,
1615
            WINED3D_GL_EXT_NONE,        NULL},
1616 1617
    {WINED3DFMT_R8G8B8A8_UINT,          GL_RGBA8UI,                       GL_RGBA8UI,                             0,
            GL_RGBA_INTEGER,            GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1618
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1619
            ARB_TEXTURE_RGB10_A2UI,     NULL},
1620 1621
    {WINED3DFMT_R8G8B8A8_SINT,          GL_RGBA8I,                        GL_RGBA8I,                              0,
            GL_RGBA_INTEGER,            GL_BYTE,                          0,
1622
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1623
            EXT_TEXTURE_INTEGER,        NULL},
1624
    {WINED3DFMT_R8G8B8X8_UNORM,         GL_RGB8,                          GL_RGB8,                                0,
1625
            GL_RGBA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1626
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1627
            WINED3D_GL_EXT_NONE,        NULL},
1628
    {WINED3DFMT_R16G16_UNORM,           GL_RGB16,                         GL_RGB16,                       GL_RGBA16,
1629
            GL_RGB,                     GL_UNSIGNED_SHORT,                6,
1630
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1631
            WINED3D_GL_EXT_NONE,        convert_r16g16},
1632 1633
    {WINED3DFMT_R16G16_UNORM,           GL_RG16,                          GL_RG16,                                0,
            GL_RG,                      GL_UNSIGNED_SHORT,                0,
1634 1635
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1636
            ARB_TEXTURE_RG,             NULL},
1637
    {WINED3DFMT_B10G10R10A2_UNORM,      GL_RGB10_A2,                      GL_RGB10_A2,                            0,
1638
            GL_BGRA,                    GL_UNSIGNED_INT_2_10_10_10_REV,   0,
1639 1640
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1641
            WINED3D_GL_EXT_NONE,        NULL},
1642
    {WINED3DFMT_R16G16B16A16_UNORM,     GL_RGBA16,                        GL_RGBA16,                              0,
1643
            GL_RGBA,                    GL_UNSIGNED_SHORT,                0,
1644 1645
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1646
            WINED3D_GL_EXT_NONE,        NULL},
1647 1648
    {WINED3DFMT_R8G8_UNORM,             GL_RG8,                           GL_RG8,                                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1649 1650
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1651
            ARB_TEXTURE_RG,             NULL},
1652 1653
    {WINED3DFMT_R8G8_UINT,              GL_RG8UI,                         GL_RG8UI,                               0,
            GL_RG_INTEGER,              GL_UNSIGNED_BYTE,                 0,
1654
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1655
            ARB_TEXTURE_RG,             NULL},
1656 1657
    {WINED3DFMT_R8G8_SINT,              GL_RG8I,                          GL_RG8I,                                0,
            GL_RG_INTEGER,              GL_BYTE,                          0,
1658
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1659
            ARB_TEXTURE_RG,             NULL},
1660 1661
    {WINED3DFMT_R16G16B16A16_UINT,      GL_RGBA16UI,                      GL_RGBA16UI,                            0,
            GL_RGBA_INTEGER,            GL_UNSIGNED_SHORT,                0,
1662
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1663
            EXT_TEXTURE_INTEGER,        NULL},
1664 1665
    {WINED3DFMT_R16G16B16A16_SINT,      GL_RGBA16I,                       GL_RGBA16I,                             0,
            GL_RGBA_INTEGER,            GL_SHORT,                         0,
1666
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1667
            EXT_TEXTURE_INTEGER,        NULL},
1668 1669
    {WINED3DFMT_R32G32_UINT,            GL_RG32UI,                        GL_RG32UI,                              0,
            GL_RG_INTEGER,              GL_UNSIGNED_INT,                  0,
1670
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1671
            ARB_TEXTURE_RG,             NULL},
1672 1673
    {WINED3DFMT_R32G32_SINT,            GL_RG32I,                         GL_RG32I,                               0,
            GL_RG_INTEGER,              GL_INT,                           0,
1674
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1675
            ARB_TEXTURE_RG,             NULL},
1676 1677
    {WINED3DFMT_R16G16_UINT,            GL_RG16UI,                        GL_RG16UI,                              0,
            GL_RG_INTEGER,              GL_UNSIGNED_SHORT,                0,
1678
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1679
            ARB_TEXTURE_RG,             NULL},
1680 1681
    {WINED3DFMT_R16G16_SINT,            GL_RG16I,                         GL_RG16I,                               0,
            GL_RG_INTEGER,              GL_SHORT,                         0,
1682
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1683
            ARB_TEXTURE_RG,             NULL},
1684 1685
    {WINED3DFMT_R32_UINT,               GL_R32UI,                         GL_R32UI,                               0,
            GL_RED_INTEGER,             GL_UNSIGNED_INT,                  0,
1686
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1687 1688 1689
            ARB_TEXTURE_RG,             NULL},
    {WINED3DFMT_R32_SINT,               GL_R32I,                          GL_R32I,                                0,
            GL_RED_INTEGER,             GL_INT,                           0,
1690
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1691
            ARB_TEXTURE_RG,             NULL},
1692
    {WINED3DFMT_R16_UNORM,              GL_R16,                           GL_R16,                                 0,
1693
            GL_RED,                     GL_UNSIGNED_SHORT,                0,
1694 1695
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1696
            ARB_TEXTURE_RG,             NULL},
1697 1698
    {WINED3DFMT_R16_UINT,               GL_R16UI,                         GL_R16UI,                               0,
            GL_RED_INTEGER,             GL_UNSIGNED_SHORT,                0,
1699
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1700 1701 1702
            ARB_TEXTURE_RG,             NULL},
    {WINED3DFMT_R16_SINT,               GL_R16I,                          GL_R16I,                                0,
            GL_RED_INTEGER,             GL_SHORT,                         0,
1703
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1704
            ARB_TEXTURE_RG,             NULL},
1705 1706
    {WINED3DFMT_R8_UINT,                GL_R8UI,                          GL_R8UI,                                0,
            GL_RED_INTEGER,             GL_UNSIGNED_BYTE,                 0,
1707
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1708
            ARB_TEXTURE_RG,             NULL},
1709 1710
    {WINED3DFMT_R8_SINT,                GL_R8I,                           GL_R8I,                                 0,
            GL_RED_INTEGER,             GL_BYTE,                          0,
1711
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1712
            ARB_TEXTURE_RG,             NULL},
1713
    /* Luminance */
1714
    {WINED3DFMT_L8_UNORM,               GL_LUMINANCE8,                    GL_SLUMINANCE8_EXT,                     0,
1715
            GL_LUMINANCE,               GL_UNSIGNED_BYTE,                 0,
1716 1717
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1718
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1719 1720
    {WINED3DFMT_L8_UNORM,               GL_R8,                            GL_R8,                                  0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1721 1722
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1723
            ARB_TEXTURE_RG,             NULL},
1724 1725
    {WINED3DFMT_L8A8_UNORM,             GL_RG8,                           GL_RG8,                                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1726
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1727
            ARB_TEXTURE_RG,             NULL},
1728
    {WINED3DFMT_L8A8_UNORM,             GL_LUMINANCE8_ALPHA8,             GL_SLUMINANCE8_ALPHA8_EXT,              0,
1729
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 0,
1730 1731
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_SRGB_READ,
1732 1733 1734
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
    {WINED3DFMT_L4A4_UNORM,             GL_RG8,                           GL_RG8,                                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 2,
1735
            WINED3D_FORMAT_CAP_FILTERING,
1736
            ARB_TEXTURE_RG,             convert_l4a4_unorm},
1737
    {WINED3DFMT_L4A4_UNORM,             GL_LUMINANCE4_ALPHA4,             GL_LUMINANCE4_ALPHA4,                   0,
1738
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 2,
1739
            WINED3D_FORMAT_CAP_FILTERING,
1740 1741 1742
            WINED3D_GL_LEGACY_CONTEXT,  convert_l4a4_unorm},
    {WINED3DFMT_L16_UNORM,              GL_R16,                           GL_R16,                                 0,
            GL_RED,                     GL_UNSIGNED_SHORT,                0,
1743
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1744 1745 1746
            ARB_TEXTURE_RG,  NULL},
    {WINED3DFMT_L16_UNORM,              GL_LUMINANCE16,                   GL_LUMINANCE16,                         0,
            GL_LUMINANCE,               GL_UNSIGNED_SHORT,                0,
1747
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1748
            WINED3D_GL_LEGACY_CONTEXT,  NULL},
1749
    /* Bump mapping stuff */
1750
    {WINED3DFMT_R8G8_SNORM,             GL_RGB8,                          GL_RGB8,                                0,
1751
            GL_BGR,                     GL_UNSIGNED_BYTE,                 3,
1752
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1753
            WINED3D_GL_EXT_NONE,        convert_r8g8_snorm},
1754
    {WINED3DFMT_R8G8_SNORM,             GL_DSDT8_NV,                      GL_DSDT8_NV,                            0,
1755
            GL_DSDT_NV,                 GL_BYTE,                          0,
1756
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1757
            NV_TEXTURE_SHADER,          NULL},
1758 1759
    {WINED3DFMT_R8G8_SNORM,             GL_RG8_SNORM,                     GL_RG8_SNORM,                           0,
            GL_RG,                      GL_BYTE,                          0,
1760 1761
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1762
            EXT_TEXTURE_SNORM,          NULL},
1763
    {WINED3DFMT_R5G5_SNORM_L6_UNORM,    GL_RGB5,                          GL_RGB5,                                0,
1764
            GL_RGB,                     GL_UNSIGNED_SHORT_5_6_5,          2,
1765
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1766
            WINED3D_GL_EXT_NONE,        convert_r5g5_snorm_l6_unorm},
1767
    {WINED3DFMT_R5G5_SNORM_L6_UNORM,    GL_DSDT8_MAG8_NV,                 GL_DSDT8_MAG8_NV,                       0,
1768
            GL_DSDT_MAG_NV,             GL_BYTE,                          3,
1769
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1770
            NV_TEXTURE_SHADER,          convert_r5g5_snorm_l6_unorm_nv},
1771 1772
    {WINED3DFMT_R5G5_SNORM_L6_UNORM,    GL_RGB8_SNORM,                    GL_RGB8_SNORM,                          0,
            GL_RGBA,                    GL_BYTE,                          4,
1773
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1774
            EXT_TEXTURE_SNORM,          convert_r5g5_snorm_l6_unorm_ext},
1775
    {WINED3DFMT_R8G8_SNORM_L8X8_UNORM,  GL_RGB8,                          GL_RGB8,                                0,
1776
            GL_BGRA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      4,
1777
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1778
            WINED3D_GL_EXT_NONE,        convert_r8g8_snorm_l8x8_unorm},
1779
    {WINED3DFMT_R8G8_SNORM_L8X8_UNORM,  GL_DSDT8_MAG8_INTENSITY8_NV,      GL_DSDT8_MAG8_INTENSITY8_NV,            0,
1780
            GL_DSDT_MAG_VIB_NV,         GL_UNSIGNED_INT_8_8_S8_S8_REV_NV, 4,
1781
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1782
            NV_TEXTURE_SHADER,          convert_r8g8_snorm_l8x8_unorm_nv},
1783
    {WINED3DFMT_R8G8B8A8_SNORM,         GL_RGBA8,                         GL_RGBA8,                               0,
1784
            GL_BGRA,                    GL_UNSIGNED_BYTE,                 4,
1785
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1786
            WINED3D_GL_EXT_NONE,        convert_r8g8b8a8_snorm},
1787
    {WINED3DFMT_R8G8B8A8_SNORM,         GL_SIGNED_RGBA8_NV,               GL_SIGNED_RGBA8_NV,                     0,
1788
            GL_RGBA,                    GL_BYTE,                          0,
1789
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1790
            NV_TEXTURE_SHADER,          NULL},
1791 1792
    {WINED3DFMT_R8G8B8A8_SNORM,         GL_RGBA8_SNORM,                   GL_RGBA8_SNORM,                         0,
            GL_RGBA,                    GL_BYTE,                          0,
1793 1794
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1795
            EXT_TEXTURE_SNORM,          NULL},
1796
    {WINED3DFMT_R16G16_SNORM,           GL_RGB16,                         GL_RGB16,                               0,
1797
            GL_BGR,                     GL_UNSIGNED_SHORT,                6,
1798
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1799
            WINED3D_GL_EXT_NONE,        convert_r16g16_snorm},
1800
    {WINED3DFMT_R16G16_SNORM,           GL_SIGNED_HILO16_NV,              GL_SIGNED_HILO16_NV,                    0,
1801
            GL_HILO_NV,                 GL_SHORT,                         0,
1802
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1803
            NV_TEXTURE_SHADER,          NULL},
1804 1805
    {WINED3DFMT_R16G16_SNORM,           GL_RG16_SNORM,                    GL_RG16_SNORM,                          0,
            GL_RG,                      GL_SHORT,                         0,
1806 1807
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1808
            EXT_TEXTURE_SNORM,          NULL},
1809 1810
    {WINED3DFMT_R16G16B16A16_SNORM,     GL_RGBA16_SNORM,                  GL_RGBA16_SNORM,                        0,
            GL_RGBA,                    GL_SHORT,                         0,
1811 1812
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1813
            EXT_TEXTURE_SNORM,          NULL},
1814 1815
    {WINED3DFMT_R16_SNORM,              GL_R16_SNORM,                     GL_R16_SNORM,                           0,
            GL_RED,                     GL_SHORT,                         0,
1816 1817
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1818
            EXT_TEXTURE_SNORM,          NULL},
1819 1820
    {WINED3DFMT_R8_SNORM,               GL_R8_SNORM,                      GL_R8_SNORM,                           0,
            GL_RED,                     GL_BYTE,                          0,
1821 1822
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_RENDERTARGET,
1823
            EXT_TEXTURE_SNORM,          NULL},
1824
    /* Depth stencil formats */
1825 1826
    {WINED3DFMT_D16_LOCKABLE,           GL_DEPTH_COMPONENT,               GL_DEPTH_COMPONENT,                     0,
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_SHORT,                0,
1827
            WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1828
            WINED3D_GL_EXT_NONE,        NULL},
1829
    {WINED3DFMT_D16_LOCKABLE,           GL_DEPTH_COMPONENT16,             GL_DEPTH_COMPONENT16,                   0,
1830
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_SHORT,                0,
1831
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1832
            ARB_DEPTH_TEXTURE,          NULL},
1833
    {WINED3DFMT_D24_UNORM_S8_UINT,      GL_DEPTH_COMPONENT24_ARB,         GL_DEPTH_COMPONENT24_ARB,               0,
1834
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_INT,                  0,
1835 1836
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1837
            ARB_DEPTH_TEXTURE,          NULL},
1838
    {WINED3DFMT_D24_UNORM_S8_UINT,      GL_DEPTH24_STENCIL8,              GL_DEPTH24_STENCIL8,                    0,
1839
            GL_DEPTH_STENCIL,           GL_UNSIGNED_INT_24_8,             0,
1840 1841
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1842
            EXT_PACKED_DEPTH_STENCIL,   NULL},
1843
    {WINED3DFMT_X8D24_UNORM,            GL_DEPTH_COMPONENT,               GL_DEPTH_COMPONENT,                     0,
1844
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_INT,                  4,
1845
            WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1846
            WINED3D_GL_EXT_NONE,        x8_d24_unorm_upload,              x8_d24_unorm_download},
1847
    {WINED3DFMT_X8D24_UNORM,            GL_DEPTH_COMPONENT24_ARB,         GL_DEPTH_COMPONENT24_ARB,               0,
1848
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_INT,                  4,
1849 1850
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1851
            ARB_DEPTH_TEXTURE,          x8_d24_unorm_upload,              x8_d24_unorm_download},
1852 1853
    {WINED3DFMT_D16_UNORM,              GL_DEPTH_COMPONENT,               GL_DEPTH_COMPONENT,                     0,
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_SHORT,                0,
1854
            WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1855
            WINED3D_GL_EXT_NONE,        NULL},
1856
    {WINED3DFMT_D16_UNORM,              GL_DEPTH_COMPONENT16,             GL_DEPTH_COMPONENT16,                   0,
1857
            GL_DEPTH_COMPONENT,         GL_UNSIGNED_SHORT,                0,
1858 1859
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1860
            ARB_DEPTH_TEXTURE,          NULL},
1861
    {WINED3DFMT_D32_FLOAT,              GL_DEPTH_COMPONENT32F,            GL_DEPTH_COMPONENT32F,                  0,
1862
            GL_DEPTH_COMPONENT,         GL_FLOAT,                         0,
1863
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1864
            ARB_DEPTH_BUFFER_FLOAT,     NULL},
1865 1866
    {WINED3DFMT_D32_FLOAT_S8X24_UINT,   GL_DEPTH32F_STENCIL8,             GL_DEPTH32F_STENCIL8,                   0,
            GL_DEPTH_STENCIL,           GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 0,
1867
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1868
            ARB_DEPTH_BUFFER_FLOAT,     NULL},
1869
    {WINED3DFMT_S8_UINT_D24_FLOAT,      GL_DEPTH32F_STENCIL8,             GL_DEPTH32F_STENCIL8,                   0,
1870
            GL_DEPTH_STENCIL,           GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 8,
1871
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1872
            ARB_DEPTH_BUFFER_FLOAT,     convert_s8_uint_d24_float},
1873 1874
    {WINED3DFMT_R32G32B32A32_UINT,      GL_RGBA32UI,                      GL_RGBA32UI,                            0,
            GL_RGBA_INTEGER,            GL_UNSIGNED_INT,                  0,
1875
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1876
            EXT_TEXTURE_INTEGER,        NULL},
1877 1878
    {WINED3DFMT_R32G32B32A32_SINT,      GL_RGBA32I,                       GL_RGBA32I,                             0,
            GL_RGBA_INTEGER,            GL_INT,                           0,
1879
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1880
            EXT_TEXTURE_INTEGER,        NULL},
1881
    /* Vendor-specific formats */
1882 1883
    {WINED3DFMT_ATI1N,                  GL_COMPRESSED_RED_RGTC1,          GL_COMPRESSED_RED_RGTC1,                0,
            GL_RED,                     GL_UNSIGNED_BYTE,                 0,
1884
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1885
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1886
    {WINED3DFMT_ATI2N,                  GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, 0,
1887
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 0,
1888
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1889
            ATI_TEXTURE_COMPRESSION_3DC, NULL},
1890
    {WINED3DFMT_ATI2N,                  GL_COMPRESSED_RG_RGTC2,           GL_COMPRESSED_RG_RGTC2,                 0,
1891
            GL_LUMINANCE_ALPHA,         GL_UNSIGNED_BYTE,                 0,
1892
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1893 1894 1895
            EXT_TEXTURE_COMPRESSION_RGTC, NULL},
    {WINED3DFMT_ATI2N,                  GL_COMPRESSED_RG_RGTC2,           GL_COMPRESSED_RG_RGTC2,                 0,
            GL_RG,                      GL_UNSIGNED_BYTE,                 0,
1896
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1897
            ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1898 1899
    {WINED3DFMT_INTZ,                   GL_DEPTH24_STENCIL8,              GL_DEPTH24_STENCIL8,                    0,
            GL_DEPTH_STENCIL,           GL_UNSIGNED_INT_24_8,             0,
1900 1901
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
            | WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1902
            EXT_PACKED_DEPTH_STENCIL,   NULL},
1903
    {WINED3DFMT_NULL,                   0,                                0,                                      0,
1904
            GL_RGBA,                    GL_UNSIGNED_INT_8_8_8_8_REV,      0,
1905
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FBO_ATTACHABLE,
1906
            ARB_FRAMEBUFFER_OBJECT,     NULL},
1907 1908 1909
    /* DirectX 10 HDR formats */
    {WINED3DFMT_R9G9B9E5_SHAREDEXP,     GL_RGB9_E5_EXT,                   GL_RGB9_E5_EXT,                            0,
            GL_RGB,                     GL_UNSIGNED_INT_5_9_9_9_REV_EXT,  0,
1910
            WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1911
            EXT_TEXTURE_SHARED_EXPONENT, NULL},
1912 1913
};

1914 1915 1916 1917 1918 1919 1920 1921
struct wined3d_format_srgb_info
{
    enum wined3d_format_id srgb_format_id;
    enum wined3d_format_id base_format_id;
};

static const struct wined3d_format_srgb_info format_srgb_info[] =
{
1922
    {WINED3DFMT_R8G8B8A8_UNORM_SRGB, WINED3DFMT_R8G8B8A8_UNORM},
1923 1924 1925
    {WINED3DFMT_BC1_UNORM_SRGB,      WINED3DFMT_BC1_UNORM},
    {WINED3DFMT_BC2_UNORM_SRGB,      WINED3DFMT_BC2_UNORM},
    {WINED3DFMT_BC3_UNORM_SRGB,      WINED3DFMT_BC3_UNORM},
1926 1927 1928
    {WINED3DFMT_B8G8R8A8_UNORM_SRGB, WINED3DFMT_B8G8R8A8_UNORM},
    {WINED3DFMT_B8G8R8X8_UNORM_SRGB, WINED3DFMT_B8G8R8X8_UNORM},
    {WINED3DFMT_BC7_UNORM_SRGB,      WINED3DFMT_BC7_UNORM},
1929 1930
};

1931
static inline int get_format_idx(enum wined3d_format_id format_id)
1932
{
1933 1934 1935
    unsigned int i;

    if (format_id < WINED3D_FORMAT_FOURCC_BASE)
1936 1937
        return format_id;

1938 1939 1940 1941
    for (i = 0; i < ARRAY_SIZE(format_index_remap); ++i)
    {
        if (format_index_remap[i].id == format_id)
            return format_index_remap[i].idx;
1942
    }
1943

1944 1945 1946
    return -1;
}

1947 1948 1949 1950 1951
static struct wined3d_format_gl *wined3d_format_gl_mutable(struct wined3d_format *format)
{
    return CONTAINING_RECORD(format, struct wined3d_format_gl, f);
}

1952 1953 1954 1955 1956
static struct wined3d_format_vk *wined3d_format_vk_mutable(struct wined3d_format *format)
{
    return CONTAINING_RECORD(format, struct wined3d_format_vk, f);
}

1957 1958
static struct wined3d_format *get_format_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
{
1959
    return (struct wined3d_format *)((BYTE *)adapter->formats + fmt_idx * adapter->format_size);
1960 1961
}

1962 1963 1964 1965 1966
static struct wined3d_format_gl *get_format_gl_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
{
    return wined3d_format_gl_mutable(get_format_by_idx(adapter, fmt_idx));
}

1967
static struct wined3d_format *get_format_internal(const struct wined3d_adapter *adapter,
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
        enum wined3d_format_id format_id)
{
    int fmt_idx;

    if ((fmt_idx = get_format_idx(format_id)) == -1)
    {
        ERR("Format %s (%#x) not found.\n", debug_d3dformat(format_id), format_id);
        return NULL;
    }

1978
    return get_format_by_idx(adapter, fmt_idx);
1979 1980
}

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
static struct wined3d_format_gl *get_format_gl_internal(const struct wined3d_adapter *adapter,
        enum wined3d_format_id format_id)
{
    struct wined3d_format *format;

    if ((format = get_format_internal(adapter, format_id)))
        return wined3d_format_gl_mutable(format);

    return NULL;
}

1992 1993
static void copy_format(const struct wined3d_adapter *adapter,
        struct wined3d_format *dst_format, const struct wined3d_format *src_format)
1994 1995
{
    enum wined3d_format_id id = dst_format->id;
1996
    memcpy(dst_format, src_format, adapter->format_size);
1997 1998 1999
    dst_format->id = id;
}

2000
static void format_set_caps(struct wined3d_format *format, unsigned int caps)
2001 2002 2003
{
    unsigned int i;

2004 2005
    for (i = 0; i < ARRAY_SIZE(format->caps); ++i)
        format->caps[i] |= caps;
2006 2007
}

2008
static void format_clear_caps(struct wined3d_format *format, unsigned int caps)
2009 2010 2011
{
    unsigned int i;

2012 2013
    for (i = 0; i < ARRAY_SIZE(format->caps); ++i)
        format->caps[i] &= ~caps;
2014 2015
}

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
static enum wined3d_channel_type map_channel_type(char t)
{
    switch (t)
    {
        case 'u':
            return WINED3D_CHANNEL_TYPE_UNORM;
        case 'i':
            return WINED3D_CHANNEL_TYPE_SNORM;
        case 'U':
            return WINED3D_CHANNEL_TYPE_UINT;
        case 'I':
            return WINED3D_CHANNEL_TYPE_SINT;
        case 'F':
            return WINED3D_CHANNEL_TYPE_FLOAT;
        case 'D':
            return WINED3D_CHANNEL_TYPE_DEPTH;
        case 'S':
            return WINED3D_CHANNEL_TYPE_STENCIL;
2034 2035
        case 'X':
            return WINED3D_CHANNEL_TYPE_UNUSED;
2036 2037 2038 2039 2040 2041
        default:
            ERR("Invalid channel type '%c'.\n", t);
            return WINED3D_CHANNEL_TYPE_NONE;
    }
}

2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
static void parse_channel_desc(struct wined3d_format *format, const char *channel_desc)
{
    unsigned int component_count = 0;
    unsigned int attrs = 0;
    unsigned int j;

    for (j = 0; j < strlen(channel_desc); ++j)
    {
        enum wined3d_channel_type channel_type = map_channel_type(channel_desc[j]);

        if (channel_type == WINED3D_CHANNEL_TYPE_UNORM || channel_type == WINED3D_CHANNEL_TYPE_SNORM)
            attrs |= WINED3D_FORMAT_ATTR_NORMALISED;
        if (channel_type == WINED3D_CHANNEL_TYPE_UINT || channel_type == WINED3D_CHANNEL_TYPE_SINT)
            attrs |= WINED3D_FORMAT_ATTR_INTEGER;
        if (channel_type == WINED3D_CHANNEL_TYPE_FLOAT)
            attrs |= WINED3D_FORMAT_ATTR_FLOAT;
        if (channel_type != WINED3D_CHANNEL_TYPE_UNUSED)
            ++component_count;

        if (channel_type == WINED3D_CHANNEL_TYPE_DEPTH && !format->depth_size)
        {
            format->depth_size = format->red_size;
            format->red_size = format->red_offset = 0;
        }

        if (channel_type == WINED3D_CHANNEL_TYPE_STENCIL && !format->stencil_size)
        {
            format->stencil_size = format->green_size;
            format->green_size = format->green_offset = 0;
        }
    }

    format->attrs |= attrs;
    format->component_count = component_count;
}

2078
static BOOL init_format_base_info(struct wined3d_adapter *adapter)
2079
{
2080
    struct wined3d_format *format;
2081
    unsigned int i;
2082

2083
    for (i = 0; i < ARRAY_SIZE(formats); ++i)
2084
    {
2085
        if (!(format = get_format_internal(adapter, formats[i].id)))
2086
            return FALSE;
2087

2088
        format->id = formats[i].id;
2089 2090 2091 2092 2093 2094 2095 2096
        format->red_size = formats[i].red_size;
        format->green_size = formats[i].green_size;
        format->blue_size = formats[i].blue_size;
        format->alpha_size = formats[i].alpha_size;
        format->red_offset = formats[i].red_offset;
        format->green_offset = formats[i].green_offset;
        format->blue_offset = formats[i].blue_offset;
        format->alpha_offset = formats[i].alpha_offset;
2097
        format->byte_count = formats[i].bpp;
2098 2099
        format->depth_size = formats[i].depth_size;
        format->stencil_size = formats[i].stencil_size;
2100 2101 2102
        format->block_width = 1;
        format->block_height = 1;
        format->block_byte_count = formats[i].bpp;
2103 2104 2105

        if (formats[i].channels)
            parse_channel_desc(format, formats[i].channels);
2106 2107
    }

2108 2109
    for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
    {
2110
        struct wined3d_format *typeless_format;
2111

2112
        if (!(format = get_format_internal(adapter, typed_formats[i].id)))
2113
            return FALSE;
2114

2115
        if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
2116
            return FALSE;
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

        format->id = typed_formats[i].id;
        format->red_size = typeless_format->red_size;
        format->green_size = typeless_format->green_size;
        format->blue_size = typeless_format->blue_size;
        format->alpha_size = typeless_format->alpha_size;
        format->red_offset = typeless_format->red_offset;
        format->green_offset = typeless_format->green_offset;
        format->blue_offset = typeless_format->blue_offset;
        format->alpha_offset = typeless_format->alpha_offset;
        format->byte_count = typeless_format->byte_count;
        format->depth_size = typeless_format->depth_size;
        format->stencil_size = typeless_format->stencil_size;
        format->block_width = typeless_format->block_width;
        format->block_height = typeless_format->block_height;
        format->block_byte_count = typeless_format->block_byte_count;
2133
        format->typeless_id = typeless_format->id;
2134 2135

        typeless_format->typeless_id = typeless_format->id;
2136

2137
        parse_channel_desc(format, typed_formats[i].channels);
2138 2139
    }

2140 2141
    for (i = 0; i < ARRAY_SIZE(ddi_formats); ++i)
    {
2142
        if (!(format = get_format_internal(adapter, ddi_formats[i].id)))
2143
            return FALSE;
2144

2145
        format->ddi_format = ddi_formats[i].ddi_format;
2146 2147
    }

2148
    for (i = 0; i < ARRAY_SIZE(format_base_flags); ++i)
2149
    {
2150
        if (!(format = get_format_internal(adapter, format_base_flags[i].id)))
2151
            return FALSE;
2152

2153
        format->attrs |= format_base_flags[i].attrs;
2154
        format_set_caps(format, format_base_flags[i].caps);
2155 2156 2157 2158 2159
    }

    return TRUE;
}

2160
static BOOL init_format_block_info(struct wined3d_adapter *adapter)
2161
{
2162
    struct wined3d_format *format;
2163
    unsigned int i;
2164

2165
    for (i = 0; i < ARRAY_SIZE(format_block_info); ++i)
2166
    {
2167
        if (!(format = get_format_internal(adapter, format_block_info[i].id)))
2168 2169
            return FALSE;

2170 2171 2172
        format->block_width = format_block_info[i].block_width;
        format->block_height = format_block_info[i].block_height;
        format->block_byte_count = format_block_info[i].block_byte_count;
2173
        format->attrs |= WINED3D_FORMAT_ATTR_BLOCKS | format_block_info[i].attrs;
2174 2175 2176 2177 2178
    }

    return TRUE;
}

2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
/* Most compressed formats are not supported for 3D textures by OpenGL.
 *
 * In the case of the S3TC/DXTn formats, NV_texture_compression_vtc provides
 * these formats for 3D textures, but unfortunately the block layout is
 * different from the one used by Direct3D.
 *
 * Since applications either don't check format availability at all before
 * using these, or refuse to run without them, we decompress them on upload.
 *
 * Affected applications include "Heroes VI", "From Dust", "Halo Online" and
 * "Eldorado". */
2190
static BOOL init_format_decompress_info(struct wined3d_adapter *adapter)
2191 2192 2193 2194 2195 2196
{
    struct wined3d_format *format;
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(format_decompress_info); ++i)
    {
2197
        if (!(format = get_format_internal(adapter, format_decompress_info[i].id)))
2198 2199
            return FALSE;

2200
        format->caps[WINED3D_GL_RES_TYPE_TEX_3D] |= WINED3D_FORMAT_CAP_DECOMPRESS;
2201 2202 2203 2204 2205 2206
        format->decompress = format_decompress_info[i].decompress;
    }

    return TRUE;
}

2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
static BOOL init_srgb_formats(struct wined3d_adapter *adapter)
{
    struct wined3d_format *format, *srgb_format;
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
    {
        if (!(srgb_format = get_format_internal(adapter, format_srgb_info[i].srgb_format_id)))
            return FALSE;
        if (!(format = get_format_internal(adapter, format_srgb_info[i].base_format_id)))
            return FALSE;

        copy_format(adapter, srgb_format, format);
    }

    return TRUE;
}

2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
static GLenum wined3d_gl_type_to_enum(enum wined3d_gl_resource_type type)
{
    switch (type)
    {
        case WINED3D_GL_RES_TYPE_TEX_1D:
            return GL_TEXTURE_1D;
        case WINED3D_GL_RES_TYPE_TEX_2D:
            return GL_TEXTURE_2D;
        case WINED3D_GL_RES_TYPE_TEX_3D:
            return GL_TEXTURE_3D;
        case WINED3D_GL_RES_TYPE_TEX_CUBE:
            return GL_TEXTURE_CUBE_MAP_ARB;
        case WINED3D_GL_RES_TYPE_TEX_RECT:
            return GL_TEXTURE_RECTANGLE_ARB;
        case WINED3D_GL_RES_TYPE_BUFFER:
            return GL_TEXTURE_2D; /* TODO: GL_TEXTURE_BUFFER. */
2241 2242
        case WINED3D_GL_RES_TYPE_RB:
            return GL_RENDERBUFFER;
2243 2244 2245 2246 2247 2248 2249
        case WINED3D_GL_RES_TYPE_COUNT:
            break;
    }
    ERR("Unexpected GL resource type %u.\n", type);
    return 0;
}

2250 2251
static void delete_fbo_attachment(const struct wined3d_gl_info *gl_info,
        enum wined3d_gl_resource_type d3d_type, GLuint object)
2252
{
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
    switch (d3d_type)
    {
        case WINED3D_GL_RES_TYPE_TEX_1D:
        case WINED3D_GL_RES_TYPE_TEX_2D:
        case WINED3D_GL_RES_TYPE_TEX_RECT:
        case WINED3D_GL_RES_TYPE_TEX_3D:
        case WINED3D_GL_RES_TYPE_TEX_CUBE:
            gl_info->gl_ops.gl.p_glDeleteTextures(1, &object);
            break;

        case WINED3D_GL_RES_TYPE_RB:
            gl_info->fbo_ops.glDeleteRenderbuffers(1, &object);
            break;

        case WINED3D_GL_RES_TYPE_BUFFER:
        case WINED3D_GL_RES_TYPE_COUNT:
            break;
    }
2271 2272
}

2273 2274 2275
static void create_and_bind_fbo_attachment(const struct wined3d_gl_info *gl_info,
        const struct wined3d_format_gl *format_gl,
        enum wined3d_gl_resource_type d3d_type, GLuint *object, GLenum internal)
2276
{
2277 2278 2279
    GLenum format = format_gl->format;
    GLenum type = format_gl->type;
    GLenum attach_type;
2280

2281
    attach_type = format_gl->f.depth_size ? GL_DEPTH_ATTACHMENT : GL_COLOR_ATTACHMENT0;
2282 2283 2284
    switch (d3d_type)
    {
        case WINED3D_GL_RES_TYPE_TEX_1D:
2285 2286
            gl_info->gl_ops.gl.p_glGenTextures(1, object);
            gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, *object);
2287 2288 2289 2290
            gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, internal, 16, 0, format, type, NULL);
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

2291 2292 2293 2294
            gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_1D, *object, 0);
            if (format_gl->f.stencil_size)
                gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER,
                        GL_STENCIL_ATTACHMENT, GL_TEXTURE_1D, *object, 0);
2295 2296 2297 2298
            break;

        case WINED3D_GL_RES_TYPE_TEX_2D:
        case WINED3D_GL_RES_TYPE_TEX_RECT:
2299 2300
            gl_info->gl_ops.gl.p_glGenTextures(1, object);
            gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(d3d_type), *object);
2301 2302 2303 2304 2305
            gl_info->gl_ops.gl.p_glTexImage2D(wined3d_gl_type_to_enum(d3d_type), 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MAG_FILTER, GL_NEAREST);

2306
            gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2307
                    wined3d_gl_type_to_enum(d3d_type), *object, 0);
2308
            if (format_gl->f.stencil_size)
2309 2310
                gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
                        wined3d_gl_type_to_enum(d3d_type), *object, 0);
2311 2312 2313
            break;

        case WINED3D_GL_RES_TYPE_TEX_3D:
2314 2315
            gl_info->gl_ops.gl.p_glGenTextures(1, object);
            gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, *object);
2316
            GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, internal, 16, 16, 16, 0, format, type, NULL));
2317 2318 2319
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

2320 2321 2322 2323
            gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_3D, *object, 0, 0);
            if (format_gl->f.stencil_size)
                gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER,
                        GL_STENCIL_ATTACHMENT, GL_TEXTURE_3D, *object, 0, 0);
2324 2325 2326
            break;

        case WINED3D_GL_RES_TYPE_TEX_CUBE:
2327 2328
            gl_info->gl_ops.gl.p_glGenTextures(1, object);
            gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, *object);
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, internal, 16, 16, 0,
                    format, type, NULL);
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

2344
            gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2345
                    GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2346
            if (format_gl->f.stencil_size)
2347 2348
                gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
                        GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2349 2350 2351 2352 2353 2354
            break;

        case WINED3D_GL_RES_TYPE_RB:
            gl_info->fbo_ops.glGenRenderbuffers(1, object);
            gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, *object);
            gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, internal, 16, 16);
2355 2356 2357 2358
            gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, attach_type, GL_RENDERBUFFER, *object);
            if (format_gl->f.stencil_size)
                gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                        GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *object);
2359 2360 2361 2362 2363 2364
            break;

        case WINED3D_GL_RES_TYPE_BUFFER:
        case WINED3D_GL_RES_TYPE_COUNT:
            break;
    }
2365 2366

    /* Ideally we'd skip all formats already known not to work on textures
2367
     * by checking for WINED3D_FORMAT_CAP_TEXTURE here. However, we want to
2368
     * know if we can attach WINED3DFMT_P8_UINT textures to FBOs, and this
2369
     * format never has WINED3D_FORMAT_CAP_TEXTURE set. Instead, swallow GL
2370 2371
     * errors generated by invalid formats. */
    while (gl_info->gl_ops.gl.p_glGetError());
2372 2373
}

2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
static void draw_test_quad(struct wined3d_caps_gl_ctx *ctx, const struct wined3d_vec3 *geometry,
        const struct wined3d_color *color)
{
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
    static const struct wined3d_vec3 default_geometry[] =
    {
        {-1.0f, -1.0f, 0.0f},
        { 1.0f, -1.0f, 0.0f},
        {-1.0f,  1.0f, 0.0f},
        { 1.0f,  1.0f, 0.0f},
    };
    static const char vs_core_header[] =
        "#version 150\n"
        "in vec4 pos;\n"
        "in vec4 color;\n"
        "out vec4 out_color;\n"
        "\n";
    static const char vs_legacy_header[] =
        "#version 120\n"
        "attribute vec4 pos;\n"
        "attribute vec4 color;\n"
        "varying vec4 out_color;\n"
        "\n";
    static const char vs_body[] =
        "void main()\n"
        "{\n"
        "    gl_Position = pos;\n"
        "    out_color = color;\n"
        "}\n";
    static const char fs_core[] =
        "#version 150\n"
        "in vec4 out_color;\n"
        "out vec4 fragment_color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    fragment_color = out_color;\n"
        "}\n";
    static const char fs_legacy[] =
        "#version 120\n"
        "varying vec4 out_color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    gl_FragData[0] = out_color;\n"
        "}\n";
    const char *source[2];
    GLuint vs_id, fs_id;
    unsigned int i;

    if (!geometry)
        geometry = default_geometry;

    if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] || !gl_info->supported[ARB_VERTEX_SHADER]
            || !gl_info->supported[ARB_FRAGMENT_SHADER])
    {
        gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
        gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
        gl_info->gl_ops.gl.p_glLoadIdentity();
        gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
        gl_info->gl_ops.gl.p_glLoadIdentity();

        gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
        gl_info->gl_ops.gl.p_glColor4f(color->r, color->g, color->b, color->a);
        for (i = 0; i < 4; ++i)
            gl_info->gl_ops.gl.p_glVertex3fv(&geometry[i].x);
        gl_info->gl_ops.gl.p_glEnd();
2441
        checkGLcall("draw quad");
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
        return;
    }

    if (!ctx->test_vbo)
        GL_EXTCALL(glGenBuffers(1, &ctx->test_vbo));
    GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, ctx->test_vbo));
    GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(struct wined3d_vec3) * 4, geometry, GL_STREAM_DRAW));
    GL_EXTCALL(glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 0, NULL));
    GL_EXTCALL(glVertexAttrib4f(1, color->r, color->g, color->b, color->a));
    GL_EXTCALL(glEnableVertexAttribArray(0));
    GL_EXTCALL(glDisableVertexAttribArray(1));

    if (!ctx->test_program_id)
    {
2456 2457
        BOOL use_glsl_150 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50);

2458 2459 2460
        ctx->test_program_id = GL_EXTCALL(glCreateProgram());

        vs_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
2461
        source[0] = use_glsl_150 ? vs_core_header : vs_legacy_header;
2462 2463 2464 2465 2466 2467
        source[1] = vs_body;
        GL_EXTCALL(glShaderSource(vs_id, 2, source, NULL));
        GL_EXTCALL(glAttachShader(ctx->test_program_id, vs_id));
        GL_EXTCALL(glDeleteShader(vs_id));

        fs_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
2468
        source[0] = use_glsl_150 ? fs_core : fs_legacy;
2469 2470 2471 2472 2473 2474 2475
        GL_EXTCALL(glShaderSource(fs_id, 1, source, NULL));
        GL_EXTCALL(glAttachShader(ctx->test_program_id, fs_id));
        GL_EXTCALL(glDeleteShader(fs_id));

        GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 0, "pos"));
        GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 1, "color"));

2476
        if (use_glsl_150)
2477 2478
            GL_EXTCALL(glBindFragDataLocation(ctx->test_program_id, 0, "fragment_color"));

2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
        GL_EXTCALL(glCompileShader(vs_id));
        print_glsl_info_log(gl_info, vs_id, FALSE);
        GL_EXTCALL(glCompileShader(fs_id));
        print_glsl_info_log(gl_info, fs_id, FALSE);
        GL_EXTCALL(glLinkProgram(ctx->test_program_id));
        shader_glsl_validate_link(gl_info, ctx->test_program_id);
    }
    GL_EXTCALL(glUseProgram(ctx->test_program_id));

    gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    GL_EXTCALL(glUseProgram(0));
    GL_EXTCALL(glDisableVertexAttribArray(0));
    GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
2493
    checkGLcall("draw quad");
2494 2495
}

2496
/* Context activation is done by the caller. */
2497
static void check_fbo_compat(struct wined3d_caps_gl_ctx *ctx, struct wined3d_format_gl *format)
2498
{
2499 2500 2501 2502
    /* Check if the default internal format is supported as a frame buffer
     * target, otherwise fall back to the render target internal.
     *
     * Try to stick to the standard format if possible, this limits precision differences. */
2503 2504 2505
    static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 1.0f};
    static const struct wined3d_color half_transparent_red = {1.0f, 0.0f, 0.0f, 0.5f};
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
2506
    GLenum status, rt_internal = format->rt_internal;
2507
    GLuint object, color_rb;
2508 2509
    enum wined3d_gl_resource_type type;
    BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2510

2511
    gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2512

2513
    for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2514
    {
2515 2516
        const char *type_string = "color";

2517 2518
        if (type == WINED3D_GL_RES_TYPE_BUFFER)
            continue;
2519

2520
        create_and_bind_fbo_attachment(gl_info, format, type, &object, format->internal);
2521

2522
        if (format->f.caps[type] & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
        {
            gl_info->fbo_ops.glGenRenderbuffers(1, &color_rb);
            gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, color_rb);
            if (type == WINED3D_GL_RES_TYPE_TEX_1D)
                gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 1);
            else
                gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 16);

            gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                    GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_rb);
            checkGLcall("Create and attach color rb attachment");
            type_string = "depth / stencil";
        }

2537 2538
        status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
        checkGLcall("Framebuffer format check");
2539

2540
        if (status == GL_FRAMEBUFFER_COMPLETE)
2541
        {
2542
            TRACE("Format %s is supported as FBO %s attachment, type %u.\n",
2543
                    debug_d3dformat(format->f.id), type_string, type);
2544
            format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE;
2545
            format->rt_internal = format->internal;
2546
            regular_fmt_used = TRUE;
2547 2548 2549
        }
        else
        {
2550
            if (!rt_internal)
2551
            {
2552
                if (format->f.caps[type] & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL))
2553
                {
2554
                    WARN("Format %s with rendertarget flag is not supported as FBO color attachment (type %u),"
2555
                            " and no fallback specified.\n", debug_d3dformat(format->f.id), type);
2556
                    format->f.caps[type] &= ~(WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL);
2557 2558 2559
                }
                else
                {
2560
                    TRACE("Format %s is not supported as FBO %s attachment, type %u.\n",
2561
                            debug_d3dformat(format->f.id), type_string, type);
2562
                }
2563
                format->rt_internal = format->internal;
2564 2565 2566
            }
            else
            {
2567 2568
                TRACE("Format %s is not supported as FBO %s attachment (type %u),"
                        " trying rtInternal format as fallback.\n",
2569
                        debug_d3dformat(format->f.id), type_string, type);
2570 2571 2572

                while (gl_info->gl_ops.gl.p_glGetError());

2573
                delete_fbo_attachment(gl_info, type, object);
2574
                create_and_bind_fbo_attachment(gl_info, format, type, &object, format->rt_internal);
2575 2576 2577 2578 2579 2580

                status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
                checkGLcall("Framebuffer format check");

                if (status == GL_FRAMEBUFFER_COMPLETE)
                {
2581
                    TRACE("Format %s rtInternal format is supported as FBO %s attachment, type %u.\n",
2582
                            debug_d3dformat(format->f.id), type_string, type);
2583 2584 2585 2586
                    fallback_fmt_used = TRUE;
                }
                else
                {
2587
                    WARN("Format %s rtInternal format is not supported as FBO %s attachment, type %u.\n",
2588
                            debug_d3dformat(format->f.id), type_string, type);
2589
                    format->f.caps[type] &= ~(WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL);
2590
                }
2591 2592
            }
        }
2593

2594
        if (status == GL_FRAMEBUFFER_COMPLETE
2595
                && ((format->f.caps[type] & WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING)
2596
                || !(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
2597
                && !(format->f.attrs & WINED3D_FORMAT_ATTR_INTEGER)
2598 2599 2600
                && format->f.id != WINED3DFMT_NULL && format->f.id != WINED3DFMT_P8_UINT
                && format->format != GL_LUMINANCE && format->format != GL_LUMINANCE_ALPHA
                && (format->f.red_size || format->f.alpha_size))
2601
        {
2602 2603
            uint32_t readback[16 * 16 * 16], color = 0;
            unsigned int r_range, a_range;
2604 2605 2606
            BYTE r, a;
            BOOL match = TRUE;
            GLuint rb;
2607

2608
            if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
            {
                gl_info->fbo_ops.glGenRenderbuffers(1, &rb);
                gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, rb);
                if (type == WINED3D_GL_RES_TYPE_TEX_1D)
                    gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 1);
                else
                    gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 16);
                gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
                gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
                checkGLcall("RB attachment");
            }
2620

2621
            gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2622 2623 2624
            gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
            if (gl_info->gl_ops.gl.p_glGetError() == GL_INVALID_FRAMEBUFFER_OPERATION)
2625
            {
2626 2627
                while (gl_info->gl_ops.gl.p_glGetError());
                TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2628
                        debug_d3dformat(format->f.id), type);
2629
                format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2630 2631 2632
            }
            else
            {
2633 2634 2635 2636 2637 2638 2639
                gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
                if (type == WINED3D_GL_RES_TYPE_TEX_1D)
                    gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
                else
                    gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 16);
                gl_info->gl_ops.gl.p_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

2640
                draw_test_quad(ctx, NULL, &black);
2641 2642

                gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2643 2644

                draw_test_quad(ctx, NULL, &half_transparent_red);
2645 2646 2647 2648 2649 2650

                gl_info->gl_ops.gl.p_glDisable(GL_BLEND);

                switch (type)
                {
                    case WINED3D_GL_RES_TYPE_TEX_1D:
2651 2652 2653
                        /* Rebinding texture to workaround a fglrx bug. */
                        gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, object);
                        gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_1D, 0, GL_BGRA,
2654 2655 2656 2657 2658 2659 2660
                                GL_UNSIGNED_INT_8_8_8_8_REV, readback);
                        color = readback[7];
                        break;

                    case WINED3D_GL_RES_TYPE_TEX_2D:
                    case WINED3D_GL_RES_TYPE_TEX_3D:
                    case WINED3D_GL_RES_TYPE_TEX_RECT:
2661 2662
                        /* Rebinding texture to workaround a fglrx bug. */
                        gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(type), object);
2663 2664 2665 2666 2667 2668
                        gl_info->gl_ops.gl.p_glGetTexImage(wined3d_gl_type_to_enum(type), 0, GL_BGRA,
                                GL_UNSIGNED_INT_8_8_8_8_REV, readback);
                        color = readback[7 * 16 + 7];
                        break;

                    case WINED3D_GL_RES_TYPE_TEX_CUBE:
2669 2670
                        /* Rebinding texture to workaround a fglrx bug. */
                        gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, object);
2671 2672 2673 2674 2675
                        gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_BGRA,
                                GL_UNSIGNED_INT_8_8_8_8_REV, readback);
                        color = readback[7 * 16 + 7];
                        break;

2676 2677 2678 2679 2680 2681
                    case WINED3D_GL_RES_TYPE_RB:
                        gl_info->gl_ops.gl.p_glReadPixels(0, 0, 16, 16,
                                GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
                        color = readback[7 * 16 + 7];
                        break;

2682 2683 2684 2685 2686 2687 2688 2689
                    case WINED3D_GL_RES_TYPE_BUFFER:
                    case WINED3D_GL_RES_TYPE_COUNT:
                        color = 0;
                        break;
                }
                checkGLcall("Post-pixelshader blending check");

                a = color >> 24;
2690
                r = (color & 0x00ff0000u) >> 16;
2691

2692 2693 2694
                r_range = format->f.red_size < 8 ? 1u << (8 - format->f.red_size) : 1;
                a_range = format->f.alpha_size < 8 ? 1u << (8 - format->f.alpha_size) : 1;
                if (format->f.red_size && (r < 0x7f - r_range || r > 0x7f + r_range))
2695
                    match = FALSE;
2696
                else if (format->f.alpha_size > 1 && (a < 0xbf - a_range || a > 0xbf + a_range))
2697 2698 2699 2700
                    match = FALSE;
                if (!match)
                {
                    TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2701
                            debug_d3dformat(format->f.id), type);
2702
                    TRACE("Color output: %#x\n", color);
2703
                    format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2704 2705 2706 2707
                }
                else
                {
                    TRACE("Format %s supports post-pixelshader blending, type %u.\n",
2708
                            debug_d3dformat(format->f.id), type);
2709
                    TRACE("Color output: %#x\n", color);
2710
                    format->f.caps[type] |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2711
                }
2712
            }
2713

2714
            if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2715 2716 2717 2718 2719 2720
            {
                gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
                gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
                gl_info->fbo_ops.glDeleteRenderbuffers(1, &rb);
                checkGLcall("RB cleanup");
            }
2721 2722
        }

2723
        if (format->internal != format->srgb_internal)
2724
        {
2725
            delete_fbo_attachment(gl_info, type, object);
2726
            create_and_bind_fbo_attachment(gl_info, format, type, &object, format->srgb_internal);
2727

2728 2729
            status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
            checkGLcall("Framebuffer format check");
2730

2731 2732 2733
            if (status == GL_FRAMEBUFFER_COMPLETE)
            {
                TRACE("Format %s's sRGB format is FBO attachable, type %u.\n",
2734
                        debug_d3dformat(format->f.id), type);
2735
                format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2736
                if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2737
                    format->internal = format->srgb_internal;
2738 2739 2740 2741
            }
            else
            {
                WARN("Format %s's sRGB format is not FBO attachable, type %u.\n",
2742
                        debug_d3dformat(format->f.id), type);
2743
                format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
2744
            }
2745
        }
2746
        else if (status == GL_FRAMEBUFFER_COMPLETE)
2747
            format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2748

2749
        if (format->f.caps[type] & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
2750 2751 2752 2753 2754
        {
            gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
            gl_info->fbo_ops.glDeleteRenderbuffers(1, &color_rb);
        }

2755
        delete_fbo_attachment(gl_info, type, object);
2756
        checkGLcall("Framebuffer format check cleanup");
2757
    }
2758

2759
    if (fallback_fmt_used && regular_fmt_used)
2760
    {
2761
        FIXME("Format %s needs different render target formats for different resource types.\n",
2762
                debug_d3dformat(format->f.id));
2763 2764 2765
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL
                | WINED3D_FORMAT_CAP_FBO_ATTACHABLE | WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB
                | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING);
2766 2767 2768
    }
}

2769 2770
static void query_format_cap(struct wined3d_gl_info *gl_info, struct wined3d_format_gl *format,
        GLint internal, GLenum pname, unsigned int cap, const char *string)
2771 2772
{
    GLint value;
2773
    enum wined3d_gl_resource_type type;
2774

2775
    for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2776
    {
2777 2778 2779
        gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type), internal, pname, 1, &value);
        if (value == GL_FULL_SUPPORT)
        {
2780
            TRACE("Format %s supports %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2781
            format->f.caps[type] |= cap;
2782 2783 2784
        }
        else
        {
2785
            TRACE("Format %s doesn't support %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2786
            format->f.caps[type] &= ~cap;
2787
        }
2788 2789 2790
    }
}

2791
/* Context activation is done by the caller. */
2792 2793
static void init_format_fbo_compat_info(const struct wined3d_adapter *adapter,
        struct wined3d_caps_gl_ctx *ctx)
2794
{
2795
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
2796
    unsigned int i, type;
2797 2798
    GLuint fbo;

2799 2800
    if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
    {
2801
        for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2802
        {
2803
            struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2804
            BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2805
            GLenum rt_internal = format->rt_internal;
2806
            GLint value;
2807

2808
            if (!format->internal)
2809 2810
                continue;

2811
            for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2812
            {
2813
                gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2814
                        format->internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2815
                if (value == GL_FULL_SUPPORT)
2816
                {
2817
                    TRACE("Format %s is supported as FBO color attachment, resource type %u.\n",
2818
                            debug_d3dformat(format->f.id), type);
2819
                    format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE;
2820
                    format->rt_internal = format->internal;
2821 2822 2823
                    regular_fmt_used = TRUE;

                    gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2824
                            format->internal, GL_FRAMEBUFFER_BLEND, 1, &value);
2825
                    if (value == GL_FULL_SUPPORT)
2826
                    {
2827
                        TRACE("Format %s supports post-pixelshader blending, resource type %u.\n",
2828
                                    debug_d3dformat(format->f.id), type);
2829
                        format->f.caps[type] |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2830 2831
                    }
                    else
2832 2833
                    {
                        TRACE("Format %s doesn't support post-pixelshader blending, resource typed %u.\n",
2834
                                debug_d3dformat(format->f.id), type);
2835
                        format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2836
                    }
2837 2838 2839
                }
                else
                {
2840 2841
                    if (!rt_internal)
                    {
2842
                        if (format->f.caps[type] & WINED3D_FORMAT_CAP_RENDERTARGET)
2843 2844 2845
                        {
                            WARN("Format %s with rendertarget flag is not supported as FBO color attachment"
                                    " and no fallback specified, resource type %u.\n",
2846
                                    debug_d3dformat(format->f.id), type);
2847
                            format->f.caps[type] &= ~WINED3D_FORMAT_CAP_RENDERTARGET;
2848 2849 2850
                        }
                        else
                            TRACE("Format %s is not supported as FBO color attachment,"
2851 2852
                            " resource type %u.\n", debug_d3dformat(format->f.id), type);
                        format->rt_internal = format->internal;
2853 2854 2855 2856 2857 2858 2859 2860
                    }
                    else
                    {
                        gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
                                rt_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
                        if (value == GL_FULL_SUPPORT)
                        {
                            TRACE("Format %s rtInternal format is supported as FBO color attachment,"
2861
                                    " resource type %u.\n", debug_d3dformat(format->f.id), type);
2862 2863 2864 2865 2866
                            fallback_fmt_used = TRUE;
                        }
                        else
                        {
                            WARN("Format %s rtInternal format is not supported as FBO color attachment,"
2867
                                    " resource type %u.\n", debug_d3dformat(format->f.id), type);
2868
                            format->f.caps[type] &= ~WINED3D_FORMAT_CAP_RENDERTARGET;
2869 2870 2871 2872
                        }
                    }
                }

2873
                if (format->internal != format->srgb_internal)
2874 2875
                {
                    gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2876
                            format->srgb_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2877 2878
                    if (value == GL_FULL_SUPPORT)
                    {
2879
                        TRACE("Format %s's sRGB format is FBO attachable, resource type %u.\n",
2880
                                debug_d3dformat(format->f.id), type);
2881
                        format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2882
                        if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2883
                            format->internal = format->srgb_internal;
2884 2885 2886
                    }
                    else
                    {
2887
                        WARN("Format %s's sRGB format is not FBO attachable, resource type %u.\n",
2888
                                debug_d3dformat(format->f.id), type);
2889
                        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
2890 2891
                    }
                }
2892 2893
                else if (format->f.caps[type] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
                    format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2894 2895
            }

2896
            if (fallback_fmt_used && regular_fmt_used)
2897
            {
2898
                FIXME("Format %s needs different render target formats for different resource types.\n",
2899
                        debug_d3dformat(format->f.id));
2900 2901
                format_clear_caps(&format->f, WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FBO_ATTACHABLE
                        | WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING);
2902 2903 2904 2905 2906
            }
        }
        return;
    }

2907 2908
    if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
    {
2909 2910
        gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
        gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2911 2912
        gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);
        gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
2913
    }
2914

2915
    for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2916
    {
2917
        struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2918

2919 2920
        if (!format->internal)
            continue;
2921

2922
        if (format->f.attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2923 2924
        {
            TRACE("Skipping format %s because it's a compressed format.\n",
2925
                    debug_d3dformat(format->f.id));
2926 2927 2928
            continue;
        }

2929
        if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2930
        {
2931
            TRACE("Checking if format %s is supported as FBO color attachment...\n", debug_d3dformat(format->f.id));
2932
            check_fbo_compat(ctx, format);
2933 2934 2935
        }
        else
        {
2936
            format->rt_internal = format->internal;
2937 2938
        }
    }
2939 2940

    if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2941
        gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
2942 2943
}

2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
static GLenum lookup_gl_view_class(GLenum internal_format)
{
    static const struct
    {
        GLenum internal_format;
        GLenum view_class;
    }
    view_classes[] =
    {
        /* 128-bit */
        {GL_RGBA32F,        GL_VIEW_CLASS_128_BITS},
        {GL_RGBA32UI,       GL_VIEW_CLASS_128_BITS},
        {GL_RGBA32I,        GL_VIEW_CLASS_128_BITS},
        /* 96-bit */
        {GL_RGB32F,         GL_VIEW_CLASS_96_BITS},
        {GL_RGB32UI,        GL_VIEW_CLASS_96_BITS},
        {GL_RGB32I,         GL_VIEW_CLASS_96_BITS},
        /* 64-bit */
        {GL_RGBA16F,        GL_VIEW_CLASS_64_BITS},
        {GL_RG32F,          GL_VIEW_CLASS_64_BITS},
        {GL_RGBA16UI,       GL_VIEW_CLASS_64_BITS},
        {GL_RG32UI,         GL_VIEW_CLASS_64_BITS},
        {GL_RGBA16I,        GL_VIEW_CLASS_64_BITS},
        {GL_RG32I,          GL_VIEW_CLASS_64_BITS},
        {GL_RGBA16,         GL_VIEW_CLASS_64_BITS},
        {GL_RGBA16_SNORM,   GL_VIEW_CLASS_64_BITS},
        /* 48-bit */
        {GL_RGB16,          GL_VIEW_CLASS_48_BITS},
        {GL_RGB16_SNORM,    GL_VIEW_CLASS_48_BITS},
        {GL_RGB16F,         GL_VIEW_CLASS_48_BITS},
        {GL_RGB16UI,        GL_VIEW_CLASS_48_BITS},
        {GL_RGB16I,         GL_VIEW_CLASS_48_BITS},
        /* 32-bit */
        {GL_RG16F,          GL_VIEW_CLASS_32_BITS},
        {GL_R11F_G11F_B10F, GL_VIEW_CLASS_32_BITS},
        {GL_R32F,           GL_VIEW_CLASS_32_BITS},
        {GL_RGB10_A2UI,     GL_VIEW_CLASS_32_BITS},
        {GL_RGBA8UI,        GL_VIEW_CLASS_32_BITS},
        {GL_RG16UI,         GL_VIEW_CLASS_32_BITS},
        {GL_R32UI,          GL_VIEW_CLASS_32_BITS},
        {GL_RGBA8I,         GL_VIEW_CLASS_32_BITS},
        {GL_RG16I,          GL_VIEW_CLASS_32_BITS},
        {GL_R32I,           GL_VIEW_CLASS_32_BITS},
        {GL_RGB10_A2,       GL_VIEW_CLASS_32_BITS},
        {GL_RGBA8,          GL_VIEW_CLASS_32_BITS},
        {GL_RG16,           GL_VIEW_CLASS_32_BITS},
        {GL_RGBA8_SNORM,    GL_VIEW_CLASS_32_BITS},
        {GL_RG16_SNORM,     GL_VIEW_CLASS_32_BITS},
        {GL_SRGB8_ALPHA8,   GL_VIEW_CLASS_32_BITS},
        {GL_RGB9_E5,        GL_VIEW_CLASS_32_BITS},
        /* 24-bit */
        {GL_RGB8,           GL_VIEW_CLASS_24_BITS},
        {GL_RGB8_SNORM,     GL_VIEW_CLASS_24_BITS},
        {GL_SRGB8,          GL_VIEW_CLASS_24_BITS},
        {GL_RGB8UI,         GL_VIEW_CLASS_24_BITS},
        {GL_RGB8I,          GL_VIEW_CLASS_24_BITS},
        /* 16-bit */
        {GL_R16F,           GL_VIEW_CLASS_16_BITS},
        {GL_RG8UI,          GL_VIEW_CLASS_16_BITS},
        {GL_R16UI,          GL_VIEW_CLASS_16_BITS},
        {GL_RG8I,           GL_VIEW_CLASS_16_BITS},
        {GL_R16I,           GL_VIEW_CLASS_16_BITS},
        {GL_RG8,            GL_VIEW_CLASS_16_BITS},
        {GL_R16,            GL_VIEW_CLASS_16_BITS},
        {GL_RG8_SNORM,      GL_VIEW_CLASS_16_BITS},
        {GL_R16_SNORM,      GL_VIEW_CLASS_16_BITS},
        /* 8-bit */
        {GL_R8UI,           GL_VIEW_CLASS_8_BITS},
        {GL_R8I,            GL_VIEW_CLASS_8_BITS},
        {GL_R8,             GL_VIEW_CLASS_8_BITS},
        {GL_R8_SNORM,       GL_VIEW_CLASS_8_BITS},

        /* RGTC1 */
        {GL_COMPRESSED_RED_RGTC1,        GL_VIEW_CLASS_RGTC1_RED},
        {GL_COMPRESSED_SIGNED_RED_RGTC1, GL_VIEW_CLASS_RGTC1_RED},
        /* RGTC2 */
        {GL_COMPRESSED_RG_RGTC2,         GL_VIEW_CLASS_RGTC2_RG},
        {GL_COMPRESSED_SIGNED_RG_RGTC2,  GL_VIEW_CLASS_RGTC2_RG},

        /* BPTC unorm */
        {GL_COMPRESSED_RGBA_BPTC_UNORM,         GL_VIEW_CLASS_BPTC_UNORM},
        {GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM,   GL_VIEW_CLASS_BPTC_UNORM},
        /* BPTC float */
        {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT,   GL_VIEW_CLASS_BPTC_FLOAT},
        {GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, GL_VIEW_CLASS_BPTC_FLOAT},

        /* DXT1 RGB */
        {GL_COMPRESSED_RGB_S3TC_DXT1_EXT,        GL_VIEW_CLASS_S3TC_DXT1_RGB},
        {GL_COMPRESSED_SRGB_S3TC_DXT1_EXT,       GL_VIEW_CLASS_S3TC_DXT1_RGB},
        /* DXT1 RGBA */
        {GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,       GL_VIEW_CLASS_S3TC_DXT1_RGBA},
        {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGBA},
        /* DXT3 */
        {GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,       GL_VIEW_CLASS_S3TC_DXT3_RGBA},
        {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_VIEW_CLASS_S3TC_DXT3_RGBA},
        /* DXT5 */
        {GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,       GL_VIEW_CLASS_S3TC_DXT5_RGBA},
        {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_VIEW_CLASS_S3TC_DXT5_RGBA},
    };

    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(view_classes); ++i)
    {
        if (view_classes[i].internal_format == internal_format)
            return view_classes[i].view_class;
    }

    return GL_NONE;
}

3055
static void query_view_class(struct wined3d_format_gl *format)
3056 3057 3058
{
    GLenum internal_view_class, gamma_view_class, rt_view_class;

3059 3060 3061
    internal_view_class = lookup_gl_view_class(format->internal);
    gamma_view_class = lookup_gl_view_class(format->srgb_internal);
    rt_view_class = lookup_gl_view_class(format->rt_internal);
3062 3063 3064

    if (internal_view_class == gamma_view_class || gamma_view_class == rt_view_class)
    {
3065
        format->view_class = internal_view_class;
3066
        TRACE("Format %s is member of GL view class %#x.\n",
3067
                debug_d3dformat(format->f.id), format->view_class);
3068 3069 3070
    }
    else
    {
3071
        format->view_class = GL_NONE;
3072 3073 3074
    }
}

3075
static void query_internal_format(struct wined3d_adapter *adapter,
3076
        struct wined3d_format_gl *format, const struct wined3d_format_texture_info *texture_info,
3077
        struct wined3d_gl_info *gl_info, BOOL srgb_write_supported, BOOL srgb_format)
3078
{
3079
    GLint count, multisample_types[8];
3080
    unsigned int i, max_log2;
3081
    GLenum target;
3082 3083 3084

    if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
    {
3085 3086 3087 3088 3089 3090
        query_format_cap(gl_info, format, format->internal, GL_VERTEX_TEXTURE,
                WINED3D_FORMAT_CAP_VTF, "vertex texture usage");
        query_format_cap(gl_info, format, format->internal, GL_FILTER,
                WINED3D_FORMAT_CAP_FILTERING, "filtering");
        query_format_cap(gl_info, format, format->internal, GL_SHADER_IMAGE_STORE,
                WINED3D_FORMAT_CAP_UNORDERED_ACCESS, "unordered access");
3091

3092
        if (srgb_format || format->srgb_internal != format->internal)
3093
        {
3094 3095
            query_format_cap(gl_info, format, format->srgb_internal, GL_SRGB_READ,
                    WINED3D_FORMAT_CAP_SRGB_READ, "sRGB read");
3096 3097

            if (srgb_write_supported)
3098 3099
                query_format_cap(gl_info, format, format->srgb_internal, GL_SRGB_WRITE,
                        WINED3D_FORMAT_CAP_SRGB_WRITE, "sRGB write");
3100
            else
3101
                format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
3102

3103 3104
            if (!(format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D]
                    & (WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE)))
3105
                format->srgb_internal = format->internal;
3106
            else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3107
                format->internal = format->srgb_internal;
3108 3109 3110 3111
        }
    }
    else
    {
3112
        if (!gl_info->limits.samplers[WINED3D_SHADER_TYPE_VERTEX])
3113
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_VTF);
3114 3115

        if (!(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
3116
            format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3117
        else if (format->f.id != WINED3DFMT_R32G32B32A32_FLOAT && format->f.id != WINED3DFMT_R32_FLOAT)
3118
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_VTF);
3119

3120
        if (srgb_format || format->srgb_internal != format->internal)
3121 3122 3123 3124
        {
            /* Filter sRGB capabilities if EXT_texture_sRGB is not supported. */
            if (!gl_info->supported[EXT_TEXTURE_SRGB])
            {
3125
                format->srgb_internal = format->internal;
3126
                format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3127
            }
3128
            else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3129
            {
3130
                format->internal = format->srgb_internal;
3131 3132 3133
            }
        }

3134 3135
        if ((format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_SRGB_WRITE) && !srgb_write_supported)
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
3136
    }
3137

3138 3139 3140 3141
    if ((!gl_info->supported[ARB_DEPTH_TEXTURE] || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
            && (format->f.depth_size || format->f.stencil_size))
    {
        TRACE("Disabling texturing support for depth / stencil format %s.\n", debug_d3dformat(format->f.id));
3142 3143 3144 3145 3146
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] &= ~WINED3D_FORMAT_CAP_TEXTURE;
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_RECT] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3147 3148
    }

3149
    query_view_class(format);
3150

3151 3152
    if (format->internal && format->f.caps[WINED3D_GL_RES_TYPE_RB]
            & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL)
3153 3154
            && (gl_info->supported[ARB_FRAMEBUFFER_OBJECT] || gl_info->supported[EXT_FRAMEBUFFER_MULTISAMPLE])
            && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3155 3156 3157
    {
        if (gl_info->supported[ARB_INTERNALFORMAT_QUERY])
        {
3158
            target = gl_info->supported[ARB_TEXTURE_MULTISAMPLE] ? GL_TEXTURE_2D_MULTISAMPLE : GL_RENDERBUFFER;
3159
            count = 0;
3160
            GL_EXTCALL(glGetInternalformativ(target, format->internal,
3161
                    GL_NUM_SAMPLE_COUNTS, 1, &count));
3162 3163 3164
            if (count > ARRAY_SIZE(multisample_types))
                FIXME("Unexpectedly high number of multisample types %d.\n", count);
            count = min(count, ARRAY_SIZE(multisample_types));
3165
            GL_EXTCALL(glGetInternalformativ(target, format->internal,
3166
                    GL_SAMPLES, count, multisample_types));
3167
            checkGLcall("query sample counts");
3168 3169
            for (i = 0; i < count; ++i)
            {
3170
                if (multisample_types[i] > sizeof(format->f.multisample_types) * CHAR_BIT)
3171
                    continue;
3172
                format->f.multisample_types |= 1u << (multisample_types[i] - 1);
3173 3174 3175 3176 3177
            }
        }
        else
        {
            max_log2 = wined3d_log2i(min(gl_info->limits.samples,
3178
                    sizeof(format->f.multisample_types) * CHAR_BIT));
3179
            for (i = 1; i <= max_log2; ++i)
3180
                format->f.multisample_types |= 1u << ((1u << i) - 1);
3181 3182 3183 3184 3185 3186
        }
    }
}

static BOOL init_format_texture_info(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
{
3187
    struct wined3d_format_gl *format, *srgb_format;
3188 3189
    struct fragment_caps fragment_caps;
    struct shader_caps shader_caps;
3190
    unsigned int i, j;
3191
    BOOL srgb_write;
3192

3193
    adapter->fragment_pipe->get_caps(adapter, &fragment_caps);
3194
    adapter->shader_backend->shader_get_caps(adapter, &shader_caps);
3195 3196 3197
    srgb_write = (fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_SRGB_WRITE)
            && (shader_caps.wined3d_caps & WINED3D_SHADER_CAP_SRGB_WRITE);

3198
    for (i = 0; i < ARRAY_SIZE(format_texture_info); ++i)
3199
    {
3200
        if (!(format = get_format_gl_internal(adapter, format_texture_info[i].id)))
3201
            return FALSE;
3202

3203 3204
        if (!gl_info->supported[format_texture_info[i].extension])
            continue;
3205 3206 3207 3208

        /* ARB_texture_rg defines floating point formats, but only if
         * ARB_texture_float is also supported. */
        if (!gl_info->supported[ARB_TEXTURE_FLOAT]
3209
                && (format->f.attrs & WINED3D_FORMAT_ATTR_FLOAT))
3210 3211
            continue;

3212
        /* ARB_texture_rg defines integer formats if EXT_texture_integer is also supported. */
3213
        if (!gl_info->supported[EXT_TEXTURE_INTEGER]
3214
                && (format->f.attrs & WINED3D_FORMAT_ATTR_INTEGER))
3215 3216
            continue;

3217 3218
        if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
                && (format->f.id == WINED3DFMT_D16_LOCKABLE || format->f.id == WINED3DFMT_NULL))
3219 3220
            continue;

3221 3222 3223 3224 3225 3226 3227 3228
        format->internal = format_texture_info[i].gl_internal;
        format->srgb_internal = format_texture_info[i].gl_srgb_internal;
        format->rt_internal = format_texture_info[i].gl_rt_internal;
        format->format = format_texture_info[i].gl_format;
        format->type = format_texture_info[i].gl_type;
        format->f.color_fixup = COLOR_FIXUP_IDENTITY;
        format->f.height_scale.numerator = 1;
        format->f.height_scale.denominator = 1;
3229

3230 3231 3232
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
        format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
        format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3233 3234 3235

        /* GL_ARB_depth_texture does not support 3D textures. It also says "cube textures are
         * problematic", but doesn't explicitly mandate that an error is generated. */
3236 3237
        if (gl_info->supported[EXT_TEXTURE3D] && !(format_texture_info[i].caps & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
            format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3238

3239
        if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3240
            format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3241

3242
        if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3243
            format->f.caps[WINED3D_GL_RES_TYPE_TEX_RECT] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3244

3245 3246
        format->f.caps[WINED3D_GL_RES_TYPE_RB] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
        format->f.caps[WINED3D_GL_RES_TYPE_RB] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3247

3248
        if (format->srgb_internal != format->internal
3249 3250
                && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
        {
3251
            format->srgb_internal = format->internal;
3252
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3253 3254
        }

3255 3256
        if (!gl_info->supported[ARB_SHADOW] && (format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_SHADOW))
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3257

3258
        query_internal_format(adapter, format, &format_texture_info[i], gl_info, srgb_write, FALSE);
3259

3260
        /* Texture conversion stuff */
3261 3262 3263
        format->f.conv_byte_count = format_texture_info[i].conv_byte_count;
        format->f.upload = format_texture_info[i].upload;
        format->f.download = format_texture_info[i].download;
3264

3265 3266
        srgb_format = NULL;
        for (j = 0; j < ARRAY_SIZE(format_srgb_info); ++j)
3267
        {
3268
            if (format_srgb_info[j].base_format_id == format->f.id)
3269
            {
3270
                if (!(srgb_format = get_format_gl_internal(adapter, format_srgb_info[j].srgb_format_id)))
3271 3272 3273 3274
                    return FALSE;
                break;
            }
        }
3275
        if (!srgb_format)
3276 3277
            continue;

3278
        copy_format(adapter, &srgb_format->f, &format->f);
3279 3280 3281 3282

        if (gl_info->supported[EXT_TEXTURE_SRGB]
                && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
        {
3283 3284
            srgb_format->internal = format_texture_info[i].gl_srgb_internal;
            srgb_format->srgb_internal = format_texture_info[i].gl_srgb_internal;
3285
            format_set_caps(&srgb_format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3286 3287
            query_internal_format(adapter, srgb_format, &format_texture_info[i], gl_info, srgb_write, TRUE);
        }
3288
    }
3289 3290

    return TRUE;
3291 3292
}

3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
static BOOL compare_uint(unsigned int x, unsigned int y, unsigned int max_diff)
{
    unsigned int diff = x > y ? x - y : y - x;

    return diff <= max_diff;
}

static BOOL compare_colour(DWORD c1, DWORD c2, BYTE max_diff)
{
    return compare_uint(c1 & 0xff, c2 & 0xff, max_diff)
            && compare_uint((c1 >> 8) & 0xff, (c2 >> 8) & 0xff, max_diff)
            && compare_uint((c1 >> 16) & 0xff, (c2 >> 16) & 0xff, max_diff)
            && compare_uint((c1 >> 24) & 0xff, (c2 >> 24) & 0xff, max_diff);
3306 3307 3308 3309 3310
}

/* A context is provided by the caller */
static BOOL check_filter(const struct wined3d_gl_info *gl_info, GLenum internal)
{
3311
    static const DWORD data[] = {0x00000000, 0xffffffff};
3312
    GLuint tex, fbo, buffer;
3313
    uint32_t readback[16 * 1];
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
    BOOL ret = FALSE;

    /* Render a filtered texture and see what happens. This is intended to detect the lack of
     * float16 filtering on ATI X1000 class cards. The drivers disable filtering instead of
     * falling back to software. If this changes in the future this code will get fooled and
     * apps might hit the software path due to incorrectly advertised caps.
     *
     * Its unlikely that this changes however. GL Games like Mass Effect depend on the filter
     * disable fallback, if Apple or ATI ever change the driver behavior they will break more
     * than Wine. The Linux binary <= r500 driver is not maintained any more anyway
     */

3326
    while (gl_info->gl_ops.gl.p_glGetError());
3327

3328 3329
    gl_info->gl_ops.gl.p_glGenTextures(1, &buffer);
    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3330
    memset(readback, 0x7e, sizeof(readback));
3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
    gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 16, 1, 0,
            GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, readback);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    gl_info->gl_ops.gl.p_glGenTextures(1, &tex);
    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, tex);
    gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, internal, 2, 1, 0,
            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
3349

3350 3351 3352
    gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffer, 0);
3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
    gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);

    gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
    gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
    gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
    gl_info->gl_ops.gl.p_glLoadIdentity();
    gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
    gl_info->gl_ops.gl.p_glLoadIdentity();

    gl_info->gl_ops.gl.p_glClearColor(0, 1, 0, 0);
    gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);

    gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
    gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 0.0);
    gl_info->gl_ops.gl.p_glVertex2f(-1.0f, -1.0f);
    gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 0.0);
    gl_info->gl_ops.gl.p_glVertex2f(1.0f, -1.0f);
    gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 1.0);
    gl_info->gl_ops.gl.p_glVertex2f(-1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 1.0);
    gl_info->gl_ops.gl.p_glVertex2f(1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glEnd();

    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3377
    memset(readback, 0x7f, sizeof(readback));
3378
    gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3379 3380
    if (compare_colour(readback[6], 0xffffffff, 5) || compare_colour(readback[6], 0x00000000, 5)
            || compare_colour(readback[9], 0xffffffff, 5) || compare_colour(readback[9], 0x00000000, 5))
3381
    {
3382
        TRACE("Read back colors 0x%08x and 0x%08x close to unfiltered color, assuming no filtering\n",
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
              readback[6], readback[9]);
        ret = FALSE;
    }
    else
    {
        TRACE("Read back colors are 0x%08x and 0x%08x, assuming texture is filtered\n",
              readback[6], readback[9]);
        ret = TRUE;
    }

3393 3394
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3395 3396
    gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex);
    gl_info->gl_ops.gl.p_glDeleteTextures(1, &buffer);
3397
    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3398

3399
    if (gl_info->gl_ops.gl.p_glGetError())
3400 3401 3402 3403
    {
        FIXME("Error during filtering test for format %x, returning no filtering\n", internal);
        ret = FALSE;
    }
3404

3405 3406 3407
    return ret;
}

3408 3409
static void init_format_filter_info(struct wined3d_adapter *adapter,
        struct wined3d_gl_info *gl_info)
3410
{
3411
    enum wined3d_pci_vendor vendor = adapter->driver_info.vendor;
3412
    struct wined3d_format_gl *format;
3413
    unsigned int i;
3414
    static const enum wined3d_format_id fmts16[] =
3415
    {
3416 3417 3418 3419 3420 3421
        WINED3DFMT_R16_FLOAT,
        WINED3DFMT_R16G16_FLOAT,
        WINED3DFMT_R16G16B16A16_FLOAT,
    };
    BOOL filtered;

3422
    /* This was already handled by init_format_texture_info(). */
3423 3424 3425
    if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
        return;

3426 3427
    if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
            || !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3428
    {
3429
        if (vendor == HW_VENDOR_NVIDIA && gl_info->supported[ARB_TEXTURE_FLOAT])
3430 3431 3432 3433
        {
            TRACE("Nvidia card with texture_float support: Assuming float16 blending\n");
            filtered = TRUE;
        }
3434
        else if (gl_info->limits.glsl_varyings > 44)
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
        {
            TRACE("More than 44 GLSL varyings - assuming d3d10 card with float16 blending\n");
            filtered = TRUE;
        }
        else
        {
            TRACE("Assuming no float16 blending\n");
            filtered = FALSE;
        }

3445
        if (filtered)
3446
        {
3447
            for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3448
            {
3449
                format = get_format_gl_internal(adapter, fmts16[i]);
3450
                format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3451 3452 3453 3454 3455
            }
        }
        return;
    }

3456
    for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3457
    {
3458 3459
        format = get_format_gl_internal(adapter, fmts16[i]);
        if (!format->internal)
3460
            continue; /* Not supported by GL */
3461

3462
        filtered = check_filter(gl_info, format->internal);
3463
        if (filtered)
3464
        {
3465
            TRACE("Format %s supports filtering.\n", debug_d3dformat(format->f.id));
3466
            format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3467 3468 3469
        }
        else
        {
3470
            TRACE("Format %s does not support filtering.\n", debug_d3dformat(format->f.id));
3471 3472 3473 3474
        }
    }
}

3475
static enum fixup_channel_source fixup_source_from_char(char c)
3476
{
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
    switch (c)
    {
        default:
        case '0':
            return CHANNEL_SOURCE_ZERO;
        case '1':
            return CHANNEL_SOURCE_ONE;
        case 'x':
        case 'X':
            return CHANNEL_SOURCE_X;
        case 'y':
        case 'Y':
            return CHANNEL_SOURCE_Y;
        case 'z':
        case 'Z':
            return CHANNEL_SOURCE_Z;
        case 'w':
        case 'W':
            return CHANNEL_SOURCE_W;
    }
}

static unsigned int fixup_sign_from_char(char c)
{
    if (c == 'x' || c == 'y' || c == 'z' || c == 'w')
        return 1;
    return 0;
}
3505

3506 3507 3508
static struct color_fixup_desc create_color_fixup_desc_from_string(const char *s)
{
    struct color_fixup_desc fixup;
3509

3510 3511 3512 3513 3514
    if (strlen(s) != 4)
    {
        ERR("Invalid fixup string %s.\n", wine_dbgstr_a(s));
        return COLOR_FIXUP_IDENTITY;
    }
3515

3516 3517 3518 3519 3520 3521 3522 3523
    fixup.x_sign_fixup = fixup_sign_from_char(s[0]);
    fixup.x_source = fixup_source_from_char(s[0]);
    fixup.y_sign_fixup = fixup_sign_from_char(s[1]);
    fixup.y_source = fixup_source_from_char(s[1]);
    fixup.z_sign_fixup = fixup_sign_from_char(s[2]);
    fixup.z_source = fixup_source_from_char(s[2]);
    fixup.w_sign_fixup = fixup_sign_from_char(s[3]);
    fixup.w_source = fixup_source_from_char(s[3]);
3524

3525 3526
    return fixup;
}
3527

3528 3529
static void apply_format_fixups(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
{
3530
    const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
3531
    struct wined3d_format_gl *format;
3532
    BOOL use_legacy_fixups;
3533
    unsigned int i;
3534

3535
    static const struct
3536
    {
3537 3538
        enum wined3d_format_id id;
        const char *fixup;
3539
        BOOL legacy;
3540
        enum wined3d_gl_extension extension;
3541
    }
3542
    fixups[] =
3543
    {
3544 3545 3546 3547 3548
        {WINED3DFMT_R16_FLOAT,             "X11W", TRUE,  WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R32_FLOAT,             "X11W", TRUE,  WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R16G16_UNORM,          "XY1W", TRUE,  WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R16G16_FLOAT,          "XY1W", TRUE,  WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R32G32_FLOAT,          "XY1W", TRUE,  WINED3D_GL_EXT_NONE},
3549

3550 3551 3552
        {WINED3DFMT_R8G8_SNORM,            "xy11", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R8G8_SNORM,            "XY11", TRUE,  NV_TEXTURE_SHADER},
        {WINED3DFMT_R8G8_SNORM,            "XY11", TRUE,  EXT_TEXTURE_SNORM},
3553

3554 3555 3556
        {WINED3DFMT_R16G16_SNORM,          "xy11", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R16G16_SNORM,          "XY11", TRUE,  NV_TEXTURE_SHADER},
        {WINED3DFMT_R16G16_SNORM,          "XY11", TRUE,  EXT_TEXTURE_SNORM},
3557

3558 3559 3560
        {WINED3DFMT_R8G8B8A8_SNORM,        "xyzw", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R8G8B8A8_SNORM,        "XYZW", FALSE, NV_TEXTURE_SHADER},
        {WINED3DFMT_R8G8B8A8_SNORM,        "XYZW", FALSE, EXT_TEXTURE_SNORM},
3561

3562 3563 3564
        {WINED3DFMT_R5G5_SNORM_L6_UNORM,   "xzY1", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R5G5_SNORM_L6_UNORM,   "XYZW", FALSE, NV_TEXTURE_SHADER},
        {WINED3DFMT_R5G5_SNORM_L6_UNORM,   "XYZW", FALSE, EXT_TEXTURE_SNORM},
3565

3566 3567
        {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "xyZW", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3568

3569 3570
        {WINED3DFMT_ATI1N,                 "XXXX", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
        {WINED3DFMT_ATI1N,                 "XXXX", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3571

3572 3573 3574
        {WINED3DFMT_ATI2N,                 "XW11", FALSE, ATI_TEXTURE_COMPRESSION_3DC},
        {WINED3DFMT_ATI2N,                 "YX11", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
        {WINED3DFMT_ATI2N,                 "YX11", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3575

3576 3577
        {WINED3DFMT_A8_UNORM,              "000X", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_A8_UNORM,              "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3578

3579 3580
        {WINED3DFMT_L8A8_UNORM,            "XXXY", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_L8A8_UNORM,            "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3581

3582 3583
        {WINED3DFMT_L4A4_UNORM,            "XXXY", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_L4A4_UNORM,            "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3584

3585 3586
        {WINED3DFMT_L16_UNORM,             "XXX1", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_L16_UNORM,             "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3587

3588 3589
        {WINED3DFMT_INTZ,                  "XXXX", FALSE, WINED3D_GL_EXT_NONE},
        {WINED3DFMT_INTZ,                  "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3590

3591
        {WINED3DFMT_L8_UNORM,              "XXX1", FALSE, ARB_TEXTURE_RG},
3592 3593
    };

3594
    use_legacy_fixups = d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR;
3595
    for (i = 0; i < ARRAY_SIZE(fixups); ++i)
3596
    {
3597 3598 3599
        if (fixups[i].legacy && !use_legacy_fixups)
            continue;

3600 3601 3602
        if (!gl_info->supported[fixups[i].extension])
            continue;

3603 3604
        format = get_format_gl_internal(adapter, fixups[i].id);
        format->f.color_fixup = create_color_fixup_desc_from_string(fixups[i].fixup);
3605 3606
    }

3607 3608
    if (!gl_info->supported[APPLE_YCBCR_422] && !gl_info->supported[APPLE_RGB_422]
            && (gl_info->supported[ARB_FRAGMENT_PROGRAM]
3609
            || (gl_info->supported[ARB_FRAGMENT_SHADER] && gl_info->supported[ARB_VERTEX_SHADER])))
3610
    {
3611 3612
        format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUY2);
3613

3614 3615
        format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_UYVY);
3616
    }
3617 3618 3619 3620 3621 3622 3623 3624
    else if (!gl_info->supported[APPLE_YCBCR_422] && gl_info->supported[APPLE_RGB_422])
    {
        format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);

        format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);
    }
3625
    else if (!gl_info->supported[APPLE_YCBCR_422] && (!gl_info->supported[ARB_FRAGMENT_PROGRAM]
3626
            && (!gl_info->supported[ARB_FRAGMENT_SHADER] || !gl_info->supported[ARB_VERTEX_SHADER])))
3627
    {
3628
        format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3629
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3630
        format->internal = 0;
3631

3632
        format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3633
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3634
        format->internal = 0;
3635
    }
3636

3637 3638
    if (gl_info->supported[ARB_FRAGMENT_PROGRAM]
            || (gl_info->supported[ARB_FRAGMENT_SHADER] && gl_info->supported[ARB_VERTEX_SHADER]))
3639
    {
3640
        format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3641
        format->f.attrs |= WINED3D_FORMAT_ATTR_HEIGHT_SCALE;
3642 3643 3644
        format->f.height_scale.numerator = 3;
        format->f.height_scale.denominator = 2;
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YV12);
3645

3646
        format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3647
        format->f.attrs |= WINED3D_FORMAT_ATTR_HEIGHT_SCALE;
3648 3649 3650
        format->f.height_scale.numerator = 3;
        format->f.height_scale.denominator = 2;
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_NV12);
3651 3652 3653
    }
    else
    {
3654
        format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3655
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3656
        format->internal = 0;
3657

3658
        format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3659
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3660
        format->internal = 0;
3661
    }
3662

3663
    if (gl_info->supported[ARB_FRAGMENT_PROGRAM] || gl_info->supported[ARB_FRAGMENT_SHADER])
3664
    {
3665 3666
        format = get_format_gl_internal(adapter, WINED3DFMT_P8_UINT);
        format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_P8);
3667
    }
3668

3669 3670
    if (!gl_info->supported[ARB_HALF_FLOAT_PIXEL])
    {
3671
        format = get_format_gl_internal(adapter, WINED3DFMT_R16_FLOAT);
3672
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3673

3674
        format = get_format_gl_internal(adapter, WINED3DFMT_R16G16_FLOAT);
3675
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3676

3677
        format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_FLOAT);
3678
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3679 3680 3681 3682
    }

    if (gl_info->quirks & WINED3D_QUIRK_BROKEN_RGBA16)
    {
3683
        format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_UNORM);
3684
        format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701
    }

    /* ATI instancing hack: Although ATI cards do not support Shader Model
     * 3.0, they support instancing. To query if the card supports instancing
     * CheckDeviceFormat() with the special format MAKEFOURCC('I','N','S','T')
     * is used. Should an application check for this, provide a proper return
     * value. We can do instancing with all shader versions, but we need
     * vertex shaders.
     *
     * Additionally applications have to set the D3DRS_POINTSIZE render state
     * to MAKEFOURCC('I','N','S','T') once to enable instancing. Wined3d
     * doesn't need that and just ignores it.
     *
     * With Shader Model 3.0 capable cards Instancing 'just works' in Windows. */
    /* FIXME: This should just check the shader backend caps. */
    if (gl_info->supported[ARB_VERTEX_PROGRAM] || gl_info->supported[ARB_VERTEX_SHADER])
    {
3702
        format = get_format_gl_internal(adapter, WINED3DFMT_INST);
3703
        format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
    }

    /* Depth bound test. To query if the card supports it CheckDeviceFormat()
     * with the special format MAKEFOURCC('N','V','D','B') is used. It is
     * enabled by setting D3DRS_ADAPTIVETESS_X render state to
     * MAKEFOURCC('N','V','D','B') and then controlled by setting
     * D3DRS_ADAPTIVETESS_Z (zMin) and D3DRS_ADAPTIVETESS_W (zMax) to test
     * value. */
    if (gl_info->supported[EXT_DEPTH_BOUNDS_TEST])
    {
3714
        format = get_format_gl_internal(adapter, WINED3DFMT_NVDB);
3715
        format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3716 3717
    }

3718 3719 3720
    if (gl_info->supported[ARB_MULTISAMPLE])
    {
        format = get_format_gl_internal(adapter, WINED3DFMT_ATOC);
3721
        format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3722 3723
    }

3724 3725 3726 3727 3728
    /* RESZ aka AMD DX9-level hack for multisampled depth buffer resolve. You query for RESZ
     * support by checking for availability of MAKEFOURCC('R','E','S','Z') surfaces with
     * RENDERTARGET usage. */
    if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
    {
3729
        format = get_format_gl_internal(adapter, WINED3DFMT_RESZ);
3730
        format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET);
3731 3732
    }

3733
    for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3734
    {
3735
        format = get_format_gl_by_idx(adapter, i);
3736

3737
        if (!(format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE))
3738 3739
            continue;

3740
        if (is_identity_fixup(format->f.color_fixup))
3741 3742
            continue;

3743
        TRACE("Checking support for fixup:\n");
3744 3745 3746
        dump_color_fixup_desc(format->f.color_fixup);
        if (!adapter->shader_backend->shader_color_fixup_supported(format->f.color_fixup)
                || !adapter->fragment_pipe->color_fixup_supported(format->f.color_fixup))
3747 3748
        {
            TRACE("[FAILED]\n");
3749
            format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3750 3751 3752 3753 3754
        }
        else
        {
            TRACE("[OK]\n");
        }
3755
    }
3756

3757
    /* These formats are not supported for 3D textures. See also
3758
     * WINED3D_FORMAT_CAP_DECOMPRESS. */
3759
    format = get_format_gl_internal(adapter, WINED3DFMT_ATI1N);
3760
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3761
    format = get_format_gl_internal(adapter, WINED3DFMT_ATI2N);
3762
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3763
    format = get_format_gl_internal(adapter, WINED3DFMT_BC4_SNORM);
3764
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3765
    format = get_format_gl_internal(adapter, WINED3DFMT_BC5_UNORM);
3766
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3767
    format = get_format_gl_internal(adapter, WINED3DFMT_BC5_SNORM);
3768
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3769 3770
}

3771 3772
static BOOL init_format_vertex_info(const struct wined3d_adapter *adapter,
        struct wined3d_gl_info *gl_info)
3773
{
3774
    struct wined3d_format_gl *format;
3775 3776
    unsigned int i;

3777
    for (i = 0; i < ARRAY_SIZE(format_vertex_info); ++i)
3778
    {
3779
        if (!(format = get_format_gl_internal(adapter, format_vertex_info[i].id)))
3780 3781
            return FALSE;

3782 3783 3784
        if (!gl_info->supported[format_vertex_info[i].extension])
            continue;

3785 3786 3787
        format->f.emit_idx = format_vertex_info[i].emit_idx;
        format->vtx_type = format_vertex_info[i].gl_vtx_type;
        format->vtx_format = format->f.component_count;
3788
        format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_VERTEX_ATTRIBUTE;
3789 3790
    }

3791 3792
    if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
    {
3793 3794
        format = get_format_gl_internal(adapter, WINED3DFMT_B8G8R8A8_UNORM);
        format->vtx_format = GL_BGRA;
3795 3796
    }

3797
    return TRUE;
3798 3799
}

3800
static BOOL init_typeless_formats(const struct wined3d_adapter *adapter)
3801
{
3802
    unsigned int caps[WINED3D_GL_RES_TYPE_COUNT];
3803
    unsigned int attrs, i, j;
3804

3805
    for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
3806 3807 3808
    {
        struct wined3d_format *format, *typeless_format;

3809
        if (!(format = get_format_internal(adapter, typed_formats[i].id)))
3810
            return FALSE;
3811
        if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
3812 3813
            return FALSE;

3814
        attrs = typeless_format->attrs;
3815
        memcpy(caps, typeless_format->caps, sizeof(caps));
3816
        copy_format(adapter, typeless_format, format);
3817
        typeless_format->attrs |= attrs;
3818 3819
        for (j = 0; j < ARRAY_SIZE(typeless_format->caps); ++j)
            typeless_format->caps[j] |= caps[j];
3820 3821
    }

3822 3823
    for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
    {
3824 3825 3826
        struct wined3d_format *typeless_format, *typeless_ds_format, *ds_format;
        struct wined3d_format *depth_view_format, *stencil_view_format;
        enum wined3d_format_id format_id;
3827

3828
        if (!(typeless_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].typeless_id)))
3829
            return FALSE;
3830
        if (!(ds_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].depth_stencil_id)))
3831 3832
            return FALSE;

3833
        typeless_ds_format = get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
3834
        typeless_ds_format->id = typeless_depth_stencil_formats[i].typeless_id;
3835
        copy_format(adapter, typeless_ds_format, ds_format);
3836
        typeless_ds_format->attrs = typeless_format->attrs;
3837
        for (j = 0; j < ARRAY_SIZE(typeless_ds_format->caps); ++j)
3838
        {
3839 3840
            typeless_ds_format->caps[j] = typeless_format->caps[j];
            typeless_format->caps[j] &= ~WINED3D_FORMAT_CAP_DEPTH_STENCIL;
3841
        }
3842

3843 3844
        if ((format_id = typeless_depth_stencil_formats[i].depth_view_id)
                && typeless_depth_stencil_formats[i].separate_depth_view_format)
3845
        {
3846
            if (!(depth_view_format = get_format_internal(adapter, format_id)))
3847
                return FALSE;
3848 3849 3850 3851
            copy_format(adapter, depth_view_format, ds_format);
            depth_view_format->red_size = depth_view_format->depth_size;
            depth_view_format->depth_size = 0;
            depth_view_format->stencil_size = 0;
3852 3853 3854
        }
        if ((format_id = typeless_depth_stencil_formats[i].stencil_view_id))
        {
3855
            if (!(stencil_view_format = get_format_internal(adapter, format_id)))
3856
                return FALSE;
3857
            copy_format(adapter, stencil_view_format, ds_format);
3858 3859 3860
            stencil_view_format->green_size = stencil_view_format->stencil_size;
            stencil_view_format->depth_size = 0;
            stencil_view_format->stencil_size = 0;
3861
        }
3862 3863
    }

3864 3865 3866
    return TRUE;
}

3867 3868
static void init_format_gen_mipmap_info(const struct wined3d_adapter *adapter,
        struct wined3d_gl_info *gl_info)
3869 3870 3871
{
    unsigned int i, j;

3872
    if (!gl_info->fbo_ops.glGenerateMipmap)
3873 3874
        return;

3875
    for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3876
    {
3877
        struct wined3d_format *format = get_format_by_idx(adapter, i);
3878

3879 3880 3881
        for (j = 0; j < ARRAY_SIZE(format->caps); ++j)
            if (!(~format->caps[j] & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FILTERING)))
                format->caps[j] |= WINED3D_FORMAT_CAP_GEN_MIPMAP;
3882 3883 3884
    }
}

3885 3886 3887 3888 3889 3890
BOOL wined3d_caps_gl_ctx_test_viewport_subpixel_bits(struct wined3d_caps_gl_ctx *ctx)
{
    static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
    static const float offset = -63.0f / 128.0f;
    GLuint texture, fbo;
3891
    uint32_t readback[4];
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
    unsigned int i;

    gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ARRAY_SIZE(readback), 1, 0,
            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
    gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
            GL_TEXTURE_2D, texture, 0);
    checkGLcall("create resources");

    gl_info->gl_ops.gl.p_glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
    GL_EXTCALL(glViewportIndexedf(0, offset, offset, 4.0f, 1.0f));
    draw_test_quad(ctx, NULL, &red);
    checkGLcall("draw");

    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
    gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
    checkGLcall("readback");

    TRACE("Readback colors are 0x%08x, 0x%08x, 0x%08x, 0x%08x.\n",
            readback[0], readback[1], readback[2], readback[3]);

    gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
    gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    checkGLcall("delete resources");

    for (i = 0; i < ARRAY_SIZE(readback); ++i)
    {
        if (readback[i] != 0xffff0000)
            return FALSE;
    }
    return TRUE;
}

3932 3933 3934 3935 3936 3937
bool wined3d_caps_gl_ctx_test_filling_convention(struct wined3d_caps_gl_ctx *ctx, float offset)
{
    static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
    unsigned int x, y, clear = 0, draw = 0;
    GLuint texture, fbo;
3938
    uint32_t readback[8][8];
3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025

    /* This is a very simple test to find out how GL handles polygon edges:
     * Draw a 1x1 quad exactly through 4 adjacent pixel centers in an 8x8
     * viewport and see which pixel it ends up in. So far we've seen top left
     * and bottom left conventions. This test may produce unexpected results
     * if the driver forces multisampling on us.
     *
     * If we find a bottom-left filling behavior we also move the x-axis
     * by the same amount. This is necessary to keep diagonals that go
     * through the pixel center intact.
     *
     * Note that we are ignoring some settings that might influence the
     * driver: How we switch GL to an upper-left coordinate system,
     * shaders vs fixed function GL. Testing these isn't possible with
     * the current draw_test_quad() infrastructure. Also the test is
     * skipped if we are not using FBOs. Drawing into the onscreen
     * frame buffer may also yield different driver behavior.
     *
     * The minimum offset also depends on the viewport size, although
     * the relation between those two is GPU dependent and not exactly
     * sensible. E.g. a 8192x8192 viewport on a GeForce 9 needs at
     * least an offset of 1/240.9, whereas a 8x8 one needs 1/255.982;
     * 32x32 needs 1/255.935. 4x4 and lower are happy with something
     * below 1/256. The 8x8 size below has been arbitrarily chosen to
     * get a useful result out of that card and avoid allocating a
     * gigantic texture during library init.
     *
     * Newer cards usually do the right thing anyway. In cases where
     * they do not (e.g. Radeon GPUs in a macbookpro14,3 running MacOS)
     * an offset of 1/2^20 is enough. */
    const struct wined3d_vec3 edge_geometry[] =
    {
        {(-1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
        {( 1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
        {(-1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
        {( 1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
    };

    gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0,
            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
    gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
            GL_TEXTURE_2D, texture, 0);
    checkGLcall("create resources");

    gl_info->gl_ops.gl.p_glViewport(0, 0, 8, 8);
    gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);

    draw_test_quad(ctx, edge_geometry, &red);
    checkGLcall("draw");

    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
    gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
            GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
    checkGLcall("readback");

    gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
    gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    checkGLcall("delete resources");

    /* We expect that exactly one fragment is generated. */
    for (y = 0; y < ARRAY_SIZE(readback); ++y)
    {
        for (x = 0; x < ARRAY_SIZE(readback[0]); ++x)
        {
            if (readback[y][x] == 0xff0000ff)
                clear++;
            else if (readback[y][x] == 0xffff0000)
                draw++;
        }
    }

    if (clear != 63 || draw != 1)
    {
        FIXME("Unexpected filling convention test result.\n");
        return FALSE;
    }

    /* One pixel was drawn, check if it is the expected one */
    return readback[3][3] == 0xffff0000;
}
4026
static float wined3d_adapter_find_polyoffset_scale(struct wined3d_caps_gl_ctx *ctx, GLenum format)
4027 4028 4029 4030 4031
{
    const struct wined3d_gl_info *gl_info = ctx->gl_info;
    static const struct wined3d_color blue = {0.0f, 0.0f, 1.0f, 1.0f};
    GLuint fbo, color, depth;
    unsigned int low = 0, high = 32, cur;
4032
    uint32_t readback[256];
4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
    static const struct wined3d_vec3 geometry[] =
    {
        {-1.0f, -1.0f, -1.0f},
        { 1.0f, -1.0f,  0.0f},
        {-1.0f,  1.0f, -1.0f},
        { 1.0f,  1.0f,  0.0f},
    };

    /* Most drivers want 2^23 for fixed point depth buffers, including r300g, r600g,
     * Nvidia. Use this as a fallback if the detection fails. */
    unsigned int fallback = 23;

    if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
    {
        FIXME("No FBOs, assuming polyoffset scale of 2^%u.\n", fallback);
4048
        return (float)(1u << fallback);
4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085
    }

    gl_info->gl_ops.gl.p_glGenTextures(1, &color);
    gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
    gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 1, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);

    gl_info->fbo_ops.glGenRenderbuffers(1, &depth);
    gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, depth);
    gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format, 256, 1);

    gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
    gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
    checkGLcall("Setup framebuffer");

    gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
    gl_info->gl_ops.gl.p_glClearDepth(0.5f);
    gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
    gl_info->gl_ops.gl.p_glEnable(GL_POLYGON_OFFSET_FILL);
    gl_info->gl_ops.gl.p_glViewport(0, 0, 256, 1);
    checkGLcall("Misc parameters");

    for (;;)
    {
        if (high - low <= 1)
        {
            ERR("PolygonOffset scale factor detection failed, using fallback value 2^%u.\n", fallback);
            cur = fallback;
            break;
        }
        cur = (low + high) / 2;

        gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        /* The post viewport transform Z of the geometry runs from 0.0 to 0.5. We want to push it another
         * 0.25 so that the Z buffer content (0.5) cuts the quad off at half the screen. */
4086
        gl_info->gl_ops.gl.p_glPolygonOffset(0.0f, (float)(1u << cur) * 0.25f);
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
        draw_test_quad(ctx, geometry, &blue);
        checkGLcall("Test draw");

        /* Rebinding texture to workaround a fglrx bug. */
        gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
        gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
        checkGLcall("readback");

        TRACE("low %02u, high %02u, cur %2u, 0=0x%08x, 125=0x%08x, 131=0x%08x, 255=0x%08x\n",
                low, high, cur, readback[0], readback[125], readback[131], readback[255]);

        if ((readback[125] & 0xff) < 0xa0)
            high = cur;
        else if ((readback[131] & 0xff) > 0xa0)
            low = cur;
        else
        {
4104
            TRACE("Found scale factor 2^%u for format %x.\n", cur, format);
4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116
            break;
        }
    }

    gl_info->gl_ops.gl.p_glDeleteTextures(1, &color);
    gl_info->fbo_ops.glDeleteRenderbuffers(1, &depth);
    gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
    gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    checkGLcall("Delete framebuffer");

    gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
    gl_info->gl_ops.gl.p_glDisable(GL_POLYGON_OFFSET_FILL);
4117
    return (float)(1u << cur);
4118 4119
}

4120 4121
static void init_format_depth_bias_scale(struct wined3d_adapter *adapter,
        struct wined3d_caps_gl_ctx *ctx)
4122
{
4123
    const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
4124 4125
    unsigned int i;

4126
    for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
4127
    {
4128
        struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
4129

4130
        if (format->f.depth_size && (format->f.caps[WINED3D_GL_RES_TYPE_RB] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
4131
        {
4132 4133
            TRACE("Testing depth bias scale for format %s.\n", debug_d3dformat(format->f.id));
            format->f.depth_bias_scale = wined3d_adapter_find_polyoffset_scale(ctx, format->internal);
4134 4135 4136 4137 4138 4139

            if (!(d3d_info->wined3d_creation_flags & WINED3D_NORMALIZED_DEPTH_BIAS))
            {
                /* The single-precision binary floating-point format has
                 * a significand precision of 24 bits.
                 */
4140
                if (format->f.attrs & WINED3D_FORMAT_ATTR_FLOAT)
4141
                    format->f.depth_bias_scale /= 1u << 24;
4142
                else
4143
                    format->f.depth_bias_scale /= 1u << format->f.depth_size;
4144
            }
4145 4146 4147 4148
        }
    }
}

4149
static BOOL wined3d_adapter_init_format_info(struct wined3d_adapter *adapter, size_t format_size)
4150
{
4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    unsigned int count = WINED3D_FORMAT_COUNT + ARRAY_SIZE(typeless_depth_stencil_formats);

    if (!(adapter->formats = heap_calloc(count, format_size)))
    {
        ERR("Failed to allocate memory.\n");
        return FALSE;
    }
    adapter->format_size = format_size;

    if (!init_format_base_info(adapter))
        goto fail;
    if (!init_format_block_info(adapter))
        goto fail;
    if (!init_format_decompress_info(adapter))
        goto fail;
4166 4167
    if (!init_srgb_formats(adapter))
        goto fail;
4168

4169 4170 4171 4172 4173 4174 4175 4176
    return TRUE;

fail:
    heap_free(adapter->formats);
    adapter->formats = NULL;
    return FALSE;
}

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
BOOL wined3d_adapter_no3d_init_format_info(struct wined3d_adapter *adapter)
{
    struct wined3d_format *format;
    unsigned int i;

    static const enum wined3d_format_id blit_formats[] =
    {
        WINED3DFMT_B8G8R8A8_UNORM,
        WINED3DFMT_B8G8R8X8_UNORM,
        WINED3DFMT_B5G6R5_UNORM,
        WINED3DFMT_B5G5R5X1_UNORM,
        WINED3DFMT_B5G5R5A1_UNORM,
        WINED3DFMT_B4G4R4A4_UNORM,
        WINED3DFMT_B2G3R3_UNORM,
        WINED3DFMT_A8_UNORM,
        WINED3DFMT_B2G3R3A8_UNORM,
        WINED3DFMT_B4G4R4X4_UNORM,
        WINED3DFMT_R10G10B10A2_UNORM,
        WINED3DFMT_R8G8B8A8_UNORM,
        WINED3DFMT_R8G8B8X8_UNORM,
        WINED3DFMT_R16G16_UNORM,
        WINED3DFMT_B10G10R10A2_UNORM,
        WINED3DFMT_R16G16B16A16_UNORM,
        WINED3DFMT_P8_UINT,
    };

    if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format)))
        return FALSE;

    for (i = 0; i < ARRAY_SIZE(blit_formats); ++i)
    {
        if (!(format = get_format_internal(adapter, blit_formats[i])))
            return FALSE;

4211 4212
        format->caps[WINED3D_GL_RES_TYPE_TEX_2D] |= WINED3D_FORMAT_CAP_BLIT;
        format->caps[WINED3D_GL_RES_TYPE_RB] |= WINED3D_FORMAT_CAP_BLIT;
4213 4214 4215 4216 4217
    }

    return TRUE;
}

4218 4219 4220 4221 4222
/* Context activation is done by the caller. */
BOOL wined3d_adapter_gl_init_format_info(struct wined3d_adapter *adapter, struct wined3d_caps_gl_ctx *ctx)
{
    struct wined3d_gl_info *gl_info = &adapter->gl_info;

4223
    if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format_gl)))
4224
        return FALSE;
4225 4226

    if (!init_format_texture_info(adapter, gl_info)) goto fail;
4227
    if (!init_format_vertex_info(adapter, gl_info)) goto fail;
4228 4229

    apply_format_fixups(adapter, gl_info);
4230 4231 4232 4233
    init_format_fbo_compat_info(adapter, ctx);
    init_format_filter_info(adapter, gl_info);
    init_format_gen_mipmap_info(adapter, gl_info);
    init_format_depth_bias_scale(adapter, ctx);
4234

4235 4236
    if (!init_typeless_formats(adapter)) goto fail;

4237 4238 4239
    return TRUE;

fail:
4240 4241
    heap_free(adapter->formats);
    adapter->formats = NULL;
4242 4243 4244
    return FALSE;
}

4245 4246 4247 4248 4249 4250 4251
static void init_vulkan_format_info(struct wined3d_format_vk *format,
        const struct wined3d_vk_info *vk_info, VkPhysicalDevice vk_physical_device)
{
    static const struct
    {
        enum wined3d_format_id id;
        VkFormat vk_format;
4252
        const char *fixup;
4253 4254 4255
    }
    vulkan_formats[] =
    {
4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
        {WINED3DFMT_R32G32B32A32_FLOAT,         VK_FORMAT_R32G32B32A32_SFLOAT,     },
        {WINED3DFMT_R32G32B32A32_UINT,          VK_FORMAT_R32G32B32A32_UINT,       },
        {WINED3DFMT_R32G32B32A32_SINT,          VK_FORMAT_R32G32B32A32_SINT,       },
        {WINED3DFMT_R32G32B32_FLOAT,            VK_FORMAT_R32G32B32_SFLOAT,        },
        {WINED3DFMT_R32G32B32_UINT,             VK_FORMAT_R32G32B32_UINT,          },
        {WINED3DFMT_R32G32B32_SINT,             VK_FORMAT_R32G32B32_SINT,          },
        {WINED3DFMT_R16G16B16A16_FLOAT,         VK_FORMAT_R16G16B16A16_SFLOAT,     },
        {WINED3DFMT_R16G16B16A16_UNORM,         VK_FORMAT_R16G16B16A16_UNORM,      },
        {WINED3DFMT_R16G16B16A16_UINT,          VK_FORMAT_R16G16B16A16_UINT,       },
        {WINED3DFMT_R16G16B16A16_SNORM,         VK_FORMAT_R16G16B16A16_SNORM,      },
        {WINED3DFMT_R16G16B16A16_SINT,          VK_FORMAT_R16G16B16A16_SINT,       },
        {WINED3DFMT_R32G32_FLOAT,               VK_FORMAT_R32G32_SFLOAT,           },
        {WINED3DFMT_R32G32_UINT,                VK_FORMAT_R32G32_UINT,             },
        {WINED3DFMT_R32G32_SINT,                VK_FORMAT_R32G32_SINT,             },
        {WINED3DFMT_R10G10B10A2_UNORM,          VK_FORMAT_A2B10G10R10_UNORM_PACK32,},
        {WINED3DFMT_R11G11B10_FLOAT,            VK_FORMAT_B10G11R11_UFLOAT_PACK32, },
        {WINED3DFMT_R8G8_UNORM,                 VK_FORMAT_R8G8_UNORM,              },
        {WINED3DFMT_R8G8_UINT,                  VK_FORMAT_R8G8_UINT,               },
        {WINED3DFMT_R8G8_SNORM,                 VK_FORMAT_R8G8_SNORM,              },
        {WINED3DFMT_R8G8_SINT,                  VK_FORMAT_R8G8_SINT,               },
        {WINED3DFMT_R8G8B8A8_UNORM,             VK_FORMAT_R8G8B8A8_UNORM,          },
        {WINED3DFMT_R8G8B8A8_UNORM_SRGB,        VK_FORMAT_R8G8B8A8_SRGB,           },
        {WINED3DFMT_R8G8B8A8_UINT,              VK_FORMAT_R8G8B8A8_UINT,           },
        {WINED3DFMT_R8G8B8A8_SNORM,             VK_FORMAT_R8G8B8A8_SNORM,          },
        {WINED3DFMT_R8G8B8A8_SINT,              VK_FORMAT_R8G8B8A8_SINT,           },
        {WINED3DFMT_R16G16_FLOAT,               VK_FORMAT_R16G16_SFLOAT,           },
        {WINED3DFMT_R16G16_UNORM,               VK_FORMAT_R16G16_UNORM,            },
        {WINED3DFMT_R16G16_UINT,                VK_FORMAT_R16G16_UINT,             },
        {WINED3DFMT_R16G16_SNORM,               VK_FORMAT_R16G16_SNORM,            },
        {WINED3DFMT_R16G16_SINT,                VK_FORMAT_R16G16_SINT,             },
        {WINED3DFMT_D32_FLOAT,                  VK_FORMAT_D32_SFLOAT,              },
        {WINED3DFMT_R32_FLOAT,                  VK_FORMAT_R32_SFLOAT,              },
        {WINED3DFMT_R32_UINT,                   VK_FORMAT_R32_UINT,                },
        {WINED3DFMT_R32_SINT,                   VK_FORMAT_R32_SINT,                },
        {WINED3DFMT_R16_FLOAT,                  VK_FORMAT_R16_SFLOAT,              },
        {WINED3DFMT_D16_UNORM,                  VK_FORMAT_D16_UNORM,               },
        {WINED3DFMT_R16_UNORM,                  VK_FORMAT_R16_UNORM,               },
        {WINED3DFMT_R16_UINT,                   VK_FORMAT_R16_UINT,                },
        {WINED3DFMT_R16_SNORM,                  VK_FORMAT_R16_SNORM,               },
        {WINED3DFMT_R16_SINT,                   VK_FORMAT_R16_SINT,                },
        {WINED3DFMT_R8_UNORM,                   VK_FORMAT_R8_UNORM,                },
        {WINED3DFMT_R8_UINT,                    VK_FORMAT_R8_UINT,                 },
        {WINED3DFMT_R8_SNORM,                   VK_FORMAT_R8_SNORM,                },
        {WINED3DFMT_R8_SINT,                    VK_FORMAT_R8_SINT,                 },
4300
        {WINED3DFMT_A8_UNORM,                   VK_FORMAT_R8_UNORM,                "000X"},
4301 4302
        {WINED3DFMT_B8G8R8A8_UNORM,             VK_FORMAT_B8G8R8A8_UNORM,          },
        {WINED3DFMT_B8G8R8A8_UNORM_SRGB,        VK_FORMAT_B8G8R8A8_SRGB,           },
4303
        {WINED3DFMT_B8G8R8X8_UNORM,             VK_FORMAT_B8G8R8A8_UNORM,          "XYZ1"},
4304
        {WINED3DFMT_B8G8R8X8_UNORM_SRGB,        VK_FORMAT_B8G8R8A8_SRGB,           "XYZ1"},
4305
        {WINED3DFMT_B5G6R5_UNORM,               VK_FORMAT_R5G6B5_UNORM_PACK16,     },
4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321
        {WINED3DFMT_BC1_UNORM,                  VK_FORMAT_BC1_RGBA_UNORM_BLOCK,    },
        {WINED3DFMT_BC1_UNORM_SRGB,             VK_FORMAT_BC1_RGBA_SRGB_BLOCK,     },
        {WINED3DFMT_BC2_UNORM,                  VK_FORMAT_BC2_UNORM_BLOCK,         },
        {WINED3DFMT_BC2_UNORM_SRGB,             VK_FORMAT_BC2_SRGB_BLOCK,          },
        {WINED3DFMT_BC3_UNORM,                  VK_FORMAT_BC3_UNORM_BLOCK,         },
        {WINED3DFMT_BC3_UNORM_SRGB,             VK_FORMAT_BC3_SRGB_BLOCK,          },
        {WINED3DFMT_BC4_UNORM,                  VK_FORMAT_BC4_UNORM_BLOCK,         },
        {WINED3DFMT_BC4_SNORM,                  VK_FORMAT_BC4_SNORM_BLOCK,         },
        {WINED3DFMT_BC5_UNORM,                  VK_FORMAT_BC5_UNORM_BLOCK,         },
        {WINED3DFMT_BC5_SNORM,                  VK_FORMAT_BC5_SNORM_BLOCK,         },
        {WINED3DFMT_BC6H_UF16,                  VK_FORMAT_BC6H_UFLOAT_BLOCK,       },
        {WINED3DFMT_BC6H_SF16,                  VK_FORMAT_BC6H_SFLOAT_BLOCK,       },
        {WINED3DFMT_BC7_UNORM,                  VK_FORMAT_BC7_UNORM_BLOCK,         },
        {WINED3DFMT_BC7_UNORM_SRGB,             VK_FORMAT_BC7_SRGB_BLOCK,          },
        {WINED3DFMT_R9G9B9E5_SHAREDEXP,         VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,  },
        {WINED3DFMT_D32_FLOAT_S8X24_UINT,       VK_FORMAT_D32_SFLOAT_S8_UINT,      },
4322
        {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS,   VK_FORMAT_D32_SFLOAT_S8_UINT,      },
4323
        {WINED3DFMT_X32_TYPELESS_G8X24_UINT,    VK_FORMAT_D32_SFLOAT_S8_UINT,      },
4324
        {WINED3DFMT_D24_UNORM_S8_UINT,          VK_FORMAT_D24_UNORM_S8_UINT,       },
4325 4326
    };
    VkFormat vk_format = VK_FORMAT_UNDEFINED;
4327
    VkImageFormatProperties image_properties;
4328 4329
    VkFormatFeatureFlags texture_flags;
    VkFormatProperties properties;
4330
    VkImageUsageFlags vk_usage;
4331
    unsigned int caps;
4332
    const char *fixup;
4333
    unsigned int i;
4334
    uint32_t mask;
4335
    VkResult vr;
4336 4337 4338 4339 4340 4341

    for (i = 0; i < ARRAY_SIZE(vulkan_formats); ++i)
    {
        if (vulkan_formats[i].id == format->f.id)
        {
            vk_format = vulkan_formats[i].vk_format;
4342
            fixup = vulkan_formats[i].fixup;
4343 4344 4345 4346 4347 4348 4349 4350 4351 4352
            break;
        }
    }
    if (!vk_format)
    {
        WARN("Unsupported format %s.\n", debug_d3dformat(format->f.id));
        return;
    }

    format->vk_format = vk_format;
4353 4354 4355 4356
    if (fixup)
        format->f.color_fixup = create_color_fixup_desc_from_string(fixup);
    else
        format->f.color_fixup = COLOR_FIXUP_IDENTITY;
4357 4358

    VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
    if (vk_format == VK_FORMAT_D24_UNORM_S8_UINT)
    {
        if (~properties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
        {
            /* AMD doesn't support VK_FORMAT_D24_UNORM_S8_UINT. */
            WARN("Mapping VK_FORMAT_D24_UNORM_S8_UINT to VK_FORMAT_D32_SFLOAT_S8_UINT.\n");

            format->vk_format = vk_format = VK_FORMAT_D32_SFLOAT_S8_UINT;
            VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
        }
    }
4370 4371

    if (properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)
4372
        format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_VERTEX_ATTRIBUTE;
4373
    if (properties.bufferFeatures & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)
4374
        format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_TEXTURE;
4375

4376
    caps = 0;
4377 4378 4379
    texture_flags = properties.linearTilingFeatures | properties.optimalTilingFeatures;
    if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
    {
4380
        caps |= WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_VTF;
4381 4382 4383
    }
    if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
    {
4384
        caps |= WINED3D_FORMAT_CAP_RENDERTARGET;
4385 4386 4387
    }
    if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)
    {
4388
        caps |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
4389
    }
4390 4391
    if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
    {
4392
        caps |= WINED3D_FORMAT_CAP_DEPTH_STENCIL;
4393
    }
4394 4395
    if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)
    {
4396
        caps |= WINED3D_FORMAT_CAP_FILTERING;
4397
    }
4398 4399
    if (texture_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)
    {
4400
        caps |= WINED3D_FORMAT_CAP_UNORDERED_ACCESS;
4401
    }
4402 4403 4404 4405 4406
    if ((texture_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)
            && (texture_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT))
    {
        caps |= WINED3D_FORMAT_CAP_BLIT;
    }
4407

4408 4409
    if (!(~caps & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FILTERING)))
        caps |= WINED3D_FORMAT_CAP_GEN_MIPMAP;
4410

4411 4412 4413 4414
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] |= caps;
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] |= caps;
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] |= caps;
    format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] |= caps;
4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428

    vk_usage = 0;
    if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
        vk_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    else if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
        vk_usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
    if (vk_usage)
    {
        if ((vr = VK_CALL(vkGetPhysicalDeviceImageFormatProperties(vk_physical_device, vk_format,
                VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, vk_usage, 0, &image_properties))) < 0)
        {
            ERR("Failed to get image format properties, vr %s.\n", wined3d_debug_vkresult(vr));
            return;
        }
4429 4430 4431 4432 4433 4434 4435

        mask = image_properties.sampleCounts & 0x3f;
        while (mask)
        {
            i = (1u << wined3d_bit_scan(&mask)) - 1;
            format->f.multisample_types |= 1u << i;
        }
4436
    }
4437 4438 4439 4440
}

BOOL wined3d_adapter_vk_init_format_info(struct wined3d_adapter_vk *adapter_vk,
        const struct wined3d_vk_info *vk_info)
4441
{
4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465
    VkPhysicalDevice vk_physical_device = adapter_vk->physical_device;
    struct wined3d_adapter *adapter = &adapter_vk->a;
    struct wined3d_format_vk *format;
    unsigned int i;

    if (!wined3d_adapter_init_format_info(adapter, sizeof(*format)))
        return FALSE;

    for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
    {
        format = wined3d_format_vk_mutable(get_format_by_idx(adapter, i));

        if (format->f.id)
            init_vulkan_format_info(format, vk_info, vk_physical_device);
    }

    if (!init_typeless_formats(adapter)) goto fail;

    return TRUE;

fail:
    heap_free(adapter->formats);
    adapter->formats = NULL;
    return FALSE;
4466 4467
}

4468
const struct wined3d_format *wined3d_get_format(const struct wined3d_adapter *adapter,
4469
        enum wined3d_format_id format_id, unsigned int bind_flags)
4470
{
4471
    const struct wined3d_format *format;
4472
    int idx = get_format_idx(format_id);
4473
    unsigned int i;
4474

4475 4476
    if (idx == -1)
    {
4477 4478
        FIXME("Can't find format %s (%#x) in the format lookup table.\n",
                debug_d3dformat(format_id), format_id);
4479
        return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4480 4481
    }

4482
    format = get_format_by_idx(adapter, idx);
4483

4484
    if (bind_flags & WINED3D_BIND_DEPTH_STENCIL && wined3d_format_is_typeless(format))
4485 4486 4487 4488
    {
        for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
        {
            if (typeless_depth_stencil_formats[i].typeless_id == format_id)
4489
                return get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
4490 4491 4492
        }

        FIXME("Cannot find depth/stencil typeless format %s (%#x).\n",
4493
                debug_d3dformat(format_id), format_id);
4494
        return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4495
    }
4496

4497
    return format;
4498 4499
}

4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515
enum wined3d_format_id wined3d_get_typed_format_id(const struct wined3d_adapter *adapter,
        const struct wined3d_format *format, enum wined3d_channel_type channel_type)
{
    const struct wined3d_typed_format_info *info;
    uint32_t i;

    for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
    {
        info = &typed_formats[i];
        if (info->typeless_id == format->typeless_id && map_channel_type(info->channels[0]) == channel_type)
            return info->id;
    }

    return WINED3DFMT_UNKNOWN;
}

4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528
BOOL wined3d_format_is_depth_view(enum wined3d_format_id resource_format_id,
        enum wined3d_format_id view_format_id)
{
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
    {
        if (typeless_depth_stencil_formats[i].typeless_id == resource_format_id)
            return typeless_depth_stencil_formats[i].depth_view_id == view_format_id;
    }
    return FALSE;
}

4529 4530
void wined3d_format_calculate_pitch(const struct wined3d_format *format, unsigned int alignment,
        unsigned int width, unsigned int height, unsigned int *row_pitch, unsigned int *slice_pitch)
4531 4532 4533
{
    /* For block based formats, pitch means the amount of bytes to the next
     * row of blocks rather than the next row of pixels. */
4534
    if (format->attrs & WINED3D_FORMAT_ATTR_BLOCKS)
4535
    {
4536 4537 4538 4539 4540
        unsigned int row_block_count = (width + format->block_width - 1) / format->block_width;
        unsigned int slice_block_count = (height + format->block_height - 1) / format->block_height;
        *row_pitch = row_block_count * format->block_byte_count;
        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
        *slice_pitch = *row_pitch * slice_block_count;
4541 4542 4543
    }
    else
    {
4544 4545 4546
        *row_pitch = format->byte_count * width;  /* Bytes / row */
        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
        *slice_pitch = *row_pitch * height;
4547 4548
    }

4549
    if (format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
4550 4551
    {
        /* The D3D format requirements make sure that the resulting format is an integer again */
4552 4553
        *slice_pitch *= format->height_scale.numerator;
        *slice_pitch /= format->height_scale.denominator;
4554
    }
4555

4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566
    TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
}

UINT wined3d_format_calculate_size(const struct wined3d_format *format, UINT alignment,
        UINT width, UINT height, UINT depth)
{
    unsigned int row_pitch, slice_pitch;

    if (format->id == WINED3DFMT_UNKNOWN)
        return 0;

4567
    if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
4568 4569
        return width * height * depth * format->byte_count;

4570
    wined3d_format_calculate_pitch(format, alignment, width, height, &row_pitch, &slice_pitch);
4571

4572
    return slice_pitch * depth;
4573 4574
}

4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588
BOOL wined3d_formats_are_srgb_variants(enum wined3d_format_id format1, enum wined3d_format_id format2)
{
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
    {
        if (format1 == format_srgb_info[i].srgb_format_id)
            return format2 == format_srgb_info[i].base_format_id;
        if (format1 == format_srgb_info[i].base_format_id)
            return format2 == format_srgb_info[i].srgb_format_id;
    }
    return FALSE;
}

4589 4590 4591
/*****************************************************************************
 * Trace formatting of useful values
 */
4592 4593 4594 4595 4596 4597 4598 4599 4600
const char *debug_box(const struct wined3d_box *box)
{
    if (!box)
        return "(null)";
    return wine_dbg_sprintf("(%u, %u, %u)-(%u, %u, %u)",
            box->left, box->top, box->front,
            box->right, box->bottom, box->back);
}

4601 4602 4603 4604 4605 4606 4607 4608
const char *debug_color(const struct wined3d_color *color)
{
    if (!color)
        return "(null)";
    return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
            color->r, color->g, color->b, color->a);
}

4609 4610 4611 4612 4613 4614 4615 4616
const char *debug_ivec4(const struct wined3d_ivec4 *v)
{
    if (!v)
        return "(null)";
    return wine_dbg_sprintf("{%d, %d, %d, %d}",
            v->x, v->y, v->z, v->w);
}

4617 4618 4619 4620 4621 4622 4623 4624
const char *debug_uvec4(const struct wined3d_uvec4 *v)
{
    if (!v)
        return "(null)";
    return wine_dbg_sprintf("{%u, %u, %u, %u}",
            v->x, v->y, v->z, v->w);
}

4625 4626 4627 4628 4629 4630 4631 4632
const char *debug_vec4(const struct wined3d_vec4 *v)
{
    if (!v)
        return "(null)";
    return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
            v->x, v->y, v->z, v->w);
}

4633 4634 4635 4636
const char *debug_const_bo_address(const struct wined3d_const_bo_address *address)
{
    if (!address)
        return "(null)";
4637
    return wine_dbg_sprintf("{%p:%p}", address->buffer_object, address->addr);
4638 4639 4640 4641 4642 4643 4644
}

const char *debug_bo_address(const struct wined3d_bo_address *address)
{
    return debug_const_bo_address((const struct wined3d_const_bo_address *)address);
}

4645 4646
const char *debug_d3dformat(enum wined3d_format_id format_id)
{
4647 4648
    switch (format_id)
    {
4649
#define FMT_TO_STR(format_id) case format_id: return #format_id
4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666
        FMT_TO_STR(WINED3DFMT_UNKNOWN);
        FMT_TO_STR(WINED3DFMT_B8G8R8_UNORM);
        FMT_TO_STR(WINED3DFMT_B5G5R5X1_UNORM);
        FMT_TO_STR(WINED3DFMT_B4G4R4A4_UNORM);
        FMT_TO_STR(WINED3DFMT_B2G3R3_UNORM);
        FMT_TO_STR(WINED3DFMT_B2G3R3A8_UNORM);
        FMT_TO_STR(WINED3DFMT_B4G4R4X4_UNORM);
        FMT_TO_STR(WINED3DFMT_R8G8B8X8_UNORM);
        FMT_TO_STR(WINED3DFMT_B10G10R10A2_UNORM);
        FMT_TO_STR(WINED3DFMT_P8_UINT_A8_UNORM);
        FMT_TO_STR(WINED3DFMT_P8_UINT);
        FMT_TO_STR(WINED3DFMT_L8_UNORM);
        FMT_TO_STR(WINED3DFMT_L8A8_UNORM);
        FMT_TO_STR(WINED3DFMT_L4A4_UNORM);
        FMT_TO_STR(WINED3DFMT_R5G5_SNORM_L6_UNORM);
        FMT_TO_STR(WINED3DFMT_R8G8_SNORM_L8X8_UNORM);
        FMT_TO_STR(WINED3DFMT_R10G11B11_SNORM);
4667
        FMT_TO_STR(WINED3DFMT_R10G10B10X2_TYPELESS);
4668 4669
        FMT_TO_STR(WINED3DFMT_R10G10B10X2_UINT);
        FMT_TO_STR(WINED3DFMT_R10G10B10X2_SNORM);
4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
        FMT_TO_STR(WINED3DFMT_R10G10B10_SNORM_A2_UNORM);
        FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
        FMT_TO_STR(WINED3DFMT_D32_UNORM);
        FMT_TO_STR(WINED3DFMT_S1_UINT_D15_UNORM);
        FMT_TO_STR(WINED3DFMT_X8D24_UNORM);
        FMT_TO_STR(WINED3DFMT_S4X4_UINT_D24_UNORM);
        FMT_TO_STR(WINED3DFMT_L16_UNORM);
        FMT_TO_STR(WINED3DFMT_S8_UINT_D24_FLOAT);
        FMT_TO_STR(WINED3DFMT_R8G8_SNORM_Cx);
        FMT_TO_STR(WINED3DFMT_R32G32B32A32_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R32G32B32A32_FLOAT);
        FMT_TO_STR(WINED3DFMT_R32G32B32A32_UINT);
        FMT_TO_STR(WINED3DFMT_R32G32B32A32_SINT);
        FMT_TO_STR(WINED3DFMT_R32G32B32_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R32G32B32_FLOAT);
        FMT_TO_STR(WINED3DFMT_R32G32B32_UINT);
        FMT_TO_STR(WINED3DFMT_R32G32B32_SINT);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_FLOAT);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_UNORM);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_UINT);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_SNORM);
        FMT_TO_STR(WINED3DFMT_R16G16B16A16_SINT);
        FMT_TO_STR(WINED3DFMT_R32G32_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R32G32_FLOAT);
        FMT_TO_STR(WINED3DFMT_R32G32_UINT);
        FMT_TO_STR(WINED3DFMT_R32G32_SINT);
        FMT_TO_STR(WINED3DFMT_R32G8X24_TYPELESS);
        FMT_TO_STR(WINED3DFMT_D32_FLOAT_S8X24_UINT);
        FMT_TO_STR(WINED3DFMT_R32_FLOAT_X8X24_TYPELESS);
        FMT_TO_STR(WINED3DFMT_X32_TYPELESS_G8X24_UINT);
        FMT_TO_STR(WINED3DFMT_R10G10B10A2_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R10G10B10A2_UNORM);
        FMT_TO_STR(WINED3DFMT_R10G10B10A2_UINT);
        FMT_TO_STR(WINED3DFMT_R10G10B10A2_SNORM);
4705
        FMT_TO_STR(WINED3DFMT_R10G10B10_XR_BIAS_A2_UNORM);
4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768
        FMT_TO_STR(WINED3DFMT_R11G11B10_FLOAT);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM_SRGB);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_UINT);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_SNORM);
        FMT_TO_STR(WINED3DFMT_R8G8B8A8_SINT);
        FMT_TO_STR(WINED3DFMT_R16G16_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R16G16_FLOAT);
        FMT_TO_STR(WINED3DFMT_R16G16_UNORM);
        FMT_TO_STR(WINED3DFMT_R16G16_UINT);
        FMT_TO_STR(WINED3DFMT_R16G16_SNORM);
        FMT_TO_STR(WINED3DFMT_R16G16_SINT);
        FMT_TO_STR(WINED3DFMT_R32_TYPELESS);
        FMT_TO_STR(WINED3DFMT_D32_FLOAT);
        FMT_TO_STR(WINED3DFMT_R32_FLOAT);
        FMT_TO_STR(WINED3DFMT_R32_UINT);
        FMT_TO_STR(WINED3DFMT_R32_SINT);
        FMT_TO_STR(WINED3DFMT_R24G8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_D24_UNORM_S8_UINT);
        FMT_TO_STR(WINED3DFMT_R24_UNORM_X8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_X24_TYPELESS_G8_UINT);
        FMT_TO_STR(WINED3DFMT_R8G8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R8G8_UNORM);
        FMT_TO_STR(WINED3DFMT_R8G8_UINT);
        FMT_TO_STR(WINED3DFMT_R8G8_SNORM);
        FMT_TO_STR(WINED3DFMT_R8G8_SINT);
        FMT_TO_STR(WINED3DFMT_R16_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R16_FLOAT);
        FMT_TO_STR(WINED3DFMT_D16_UNORM);
        FMT_TO_STR(WINED3DFMT_R16_UNORM);
        FMT_TO_STR(WINED3DFMT_R16_UINT);
        FMT_TO_STR(WINED3DFMT_R16_SNORM);
        FMT_TO_STR(WINED3DFMT_R16_SINT);
        FMT_TO_STR(WINED3DFMT_R8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_R8_UNORM);
        FMT_TO_STR(WINED3DFMT_R8_UINT);
        FMT_TO_STR(WINED3DFMT_R8_SNORM);
        FMT_TO_STR(WINED3DFMT_R8_SINT);
        FMT_TO_STR(WINED3DFMT_A8_UNORM);
        FMT_TO_STR(WINED3DFMT_R1_UNORM);
        FMT_TO_STR(WINED3DFMT_R9G9B9E5_SHAREDEXP);
        FMT_TO_STR(WINED3DFMT_R8G8_B8G8_UNORM);
        FMT_TO_STR(WINED3DFMT_G8R8_G8B8_UNORM);
        FMT_TO_STR(WINED3DFMT_BC1_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC1_UNORM);
        FMT_TO_STR(WINED3DFMT_BC1_UNORM_SRGB);
        FMT_TO_STR(WINED3DFMT_BC2_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC2_UNORM);
        FMT_TO_STR(WINED3DFMT_BC2_UNORM_SRGB);
        FMT_TO_STR(WINED3DFMT_BC3_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC3_UNORM);
        FMT_TO_STR(WINED3DFMT_BC3_UNORM_SRGB);
        FMT_TO_STR(WINED3DFMT_BC4_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC4_UNORM);
        FMT_TO_STR(WINED3DFMT_BC4_SNORM);
        FMT_TO_STR(WINED3DFMT_BC5_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC5_UNORM);
        FMT_TO_STR(WINED3DFMT_BC5_SNORM);
        FMT_TO_STR(WINED3DFMT_B5G6R5_UNORM);
        FMT_TO_STR(WINED3DFMT_B5G5R5A1_UNORM);
        FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM);
        FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM);
4769 4770 4771 4772
        FMT_TO_STR(WINED3DFMT_B8G8R8A8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM_SRGB);
        FMT_TO_STR(WINED3DFMT_B8G8R8X8_TYPELESS);
        FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM_SRGB);
4773 4774 4775
        FMT_TO_STR(WINED3DFMT_BC6H_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC6H_UF16);
        FMT_TO_STR(WINED3DFMT_BC6H_SF16);
4776 4777 4778
        FMT_TO_STR(WINED3DFMT_BC7_TYPELESS);
        FMT_TO_STR(WINED3DFMT_BC7_UNORM);
        FMT_TO_STR(WINED3DFMT_BC7_UNORM_SRGB);
4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795
        FMT_TO_STR(WINED3DFMT_UYVY);
        FMT_TO_STR(WINED3DFMT_YUY2);
        FMT_TO_STR(WINED3DFMT_YV12);
        FMT_TO_STR(WINED3DFMT_DXT1);
        FMT_TO_STR(WINED3DFMT_DXT2);
        FMT_TO_STR(WINED3DFMT_DXT3);
        FMT_TO_STR(WINED3DFMT_DXT4);
        FMT_TO_STR(WINED3DFMT_DXT5);
        FMT_TO_STR(WINED3DFMT_MULTI2_ARGB8);
        FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
        FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
        FMT_TO_STR(WINED3DFMT_ATI1N);
        FMT_TO_STR(WINED3DFMT_ATI2N);
        FMT_TO_STR(WINED3DFMT_INST);
        FMT_TO_STR(WINED3DFMT_NVDB);
        FMT_TO_STR(WINED3DFMT_NVHU);
        FMT_TO_STR(WINED3DFMT_NVHS);
4796
        FMT_TO_STR(WINED3DFMT_INTZ);
4797
        FMT_TO_STR(WINED3DFMT_RESZ);
4798
        FMT_TO_STR(WINED3DFMT_NULL);
4799 4800
        FMT_TO_STR(WINED3DFMT_R16);
        FMT_TO_STR(WINED3DFMT_AL16);
4801
        FMT_TO_STR(WINED3DFMT_NV12);
4802
        FMT_TO_STR(WINED3DFMT_ATOC);
4803
#undef FMT_TO_STR
4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817
        default:
        {
            char fourcc[5];
            fourcc[0] = (char)(format_id);
            fourcc[1] = (char)(format_id >> 8);
            fourcc[2] = (char)(format_id >> 16);
            fourcc[3] = (char)(format_id >> 24);
            fourcc[4] = 0;
            if (isprint(fourcc[0]) && isprint(fourcc[1]) && isprint(fourcc[2]) && isprint(fourcc[3]))
                FIXME("Unrecognized %#x (as fourcc: %s) WINED3DFORMAT!\n", format_id, fourcc);
            else
                FIXME("Unrecognized %#x WINED3DFORMAT!\n", format_id);
        }
        return "unrecognized";
4818
    }
4819
}
4820

4821
const char *debug_d3ddevicetype(enum wined3d_device_type device_type)
4822
{
4823
    switch (device_type)
4824
    {
4825
#define DEVTYPE_TO_STR(dev) case dev: return #dev
4826 4827 4828
        DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_HAL);
        DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_REF);
        DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_SW);
4829
#undef DEVTYPE_TO_STR
4830
        default:
4831
            FIXME("Unrecognized device type %#x.\n", device_type);
4832 4833
            return "unrecognized";
    }
4834 4835
}

4836 4837 4838 4839 4840 4841 4842 4843 4844
struct debug_buffer
{
    char str[200]; /* wine_dbg_sprintf() limits string size to 200 */
    char *ptr;
    int size;
};

static void init_debug_buffer(struct debug_buffer *buffer, const char *default_string)
{
4845
    snprintf(buffer->str, sizeof(buffer->str), "%s", default_string);
4846
    buffer->ptr = buffer->str;
4847
    buffer->size = sizeof(buffer->str);
4848 4849
}

4850
static void debug_append(struct debug_buffer *buffer, const char *str, const char *separator)
4851 4852 4853 4854 4855 4856 4857 4858 4859
{
    int size;

    if (!separator || buffer->ptr == buffer->str)
        separator = "";
    size = snprintf(buffer->ptr, buffer->size, "%s%s", separator, str);
    if (size == -1 || size >= buffer->size)
    {
        buffer->size = 0;
4860
        strcpy(&buffer->str[sizeof(buffer->str) - 4], "...");
4861
        return;
4862 4863 4864 4865 4866 4867
    }

    buffer->ptr += size;
    buffer->size -= size;
}

4868
const char *wined3d_debug_resource_access(uint32_t access)
4869
{
4870
    struct debug_buffer buffer;
4871

4872 4873
    init_debug_buffer(&buffer, "0");
#define ACCESS_TO_STR(x) if (access & x) { debug_append(&buffer, #x, " | "); access &= ~x; }
4874 4875
    ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_GPU);
    ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_CPU);
4876 4877
    ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_R);
    ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_W);
4878 4879 4880 4881
#undef ACCESS_TO_STR
    if (access)
        FIXME("Unrecognised access flag(s) %#x.\n", access);

4882
    return wine_dbg_sprintf("%s", buffer.str);
4883 4884
}

4885
const char *wined3d_debug_bind_flags(uint32_t bind_flags)
4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
{
    struct debug_buffer buffer;

    init_debug_buffer(&buffer, "0");
#define BIND_FLAG_TO_STR(x) if (bind_flags & x) { debug_append(&buffer, #x, " | "); bind_flags &= ~x; }
    BIND_FLAG_TO_STR(WINED3D_BIND_VERTEX_BUFFER);
    BIND_FLAG_TO_STR(WINED3D_BIND_INDEX_BUFFER);
    BIND_FLAG_TO_STR(WINED3D_BIND_CONSTANT_BUFFER);
    BIND_FLAG_TO_STR(WINED3D_BIND_SHADER_RESOURCE);
    BIND_FLAG_TO_STR(WINED3D_BIND_STREAM_OUTPUT);
    BIND_FLAG_TO_STR(WINED3D_BIND_RENDER_TARGET);
    BIND_FLAG_TO_STR(WINED3D_BIND_DEPTH_STENCIL);
    BIND_FLAG_TO_STR(WINED3D_BIND_UNORDERED_ACCESS);
4899
    BIND_FLAG_TO_STR(WINED3D_BIND_INDIRECT_BUFFER);
4900 4901 4902 4903 4904 4905 4906
#undef BIND_FLAG_TO_STR
    if (bind_flags)
        FIXME("Unrecognised bind flag(s) %#x.\n", bind_flags);

    return wine_dbg_sprintf("%s", buffer.str);
}

4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
const char *wined3d_debug_view_desc(const struct wined3d_view_desc *d, const struct wined3d_resource *resource)
{
    struct debug_buffer buffer;
    unsigned int flags = d->flags;

    init_debug_buffer(&buffer, "0");
#define VIEW_FLAG_TO_STR(x) if (flags & x) { debug_append(&buffer, #x, " | "); flags &= ~x; }
    VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_RAW);
    VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_APPEND);
    VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_COUNTER);
    VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_CUBE);
    VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_ARRAY);
4919 4920
    VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_DEPTH);
    VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_STENCIL);
4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933
#undef VIEW_FLAG_TO_STR
    if (flags)
        FIXME("Unrecognised view flag(s) %#x.\n", flags);

    if (resource->type == WINED3D_RTYPE_BUFFER)
        return wine_dbg_sprintf("format %s, flags %s, start_idx %u, count %u",
                debug_d3dformat(d->format_id), buffer.str, d->u.buffer.start_idx, d->u.buffer.count);
    else
        return wine_dbg_sprintf("format %s, flags %s, level_idx %u, level_count %u, layer_idx %u, layer_count %u",
                debug_d3dformat(d->format_id), buffer.str, d->u.texture.level_idx, d->u.texture.level_count,
                d->u.texture.layer_idx, d->u.texture.layer_count);
}

4934
const char *debug_d3dusage(uint32_t usage)
4935
{
4936
    struct debug_buffer buffer;
4937

4938 4939
    init_debug_buffer(&buffer, "0");
#define WINED3DUSAGE_TO_STR(x) if (usage & x) { debug_append(&buffer, #x, " | "); usage &= ~x; }
4940 4941 4942 4943 4944 4945
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
4946 4947 4948
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICTED_CONTENT);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE_DRIVER);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE);
4949
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_DMAP);
4950 4951 4952
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_TEXTAPI);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_LEGACY_CUBEMAP);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_OWNDC);
4953 4954
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_STATICDECL);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_OVERLAY);
4955
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_MANAGED);
4956
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_VIDMEM_ACCOUNTING);
4957 4958 4959 4960 4961 4962 4963 4964
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_FILTER);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_GENMIPMAP);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_LEGACYBUMPMAP);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBREAD);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBWRITE);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_VERTEXTEXTURE);
    WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_WRAPANDMIP);
4965
#undef WINED3DUSAGE_TO_STR
4966 4967
    if (usage)
        FIXME("Unrecognized usage flag(s) %#x.\n", usage);
4968

4969
    return wine_dbg_sprintf("%s", buffer.str);
4970 4971
}

4972 4973 4974 4975
const char *debug_d3ddeclmethod(enum wined3d_decl_method method)
{
    switch (method)
    {
4976
#define WINED3DDECLMETHOD_TO_STR(u) case u: return #u
4977 4978 4979 4980 4981 4982 4983
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_DEFAULT);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_U);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_V);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_CROSS_UV);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_UV);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP);
        WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP_PRESAMPLED);
4984 4985
#undef WINED3DDECLMETHOD_TO_STR
        default:
4986
            FIXME("Unrecognized declaration method %#x.\n", method);
4987 4988 4989 4990
            return "unrecognized";
    }
}

4991 4992 4993 4994
const char *debug_d3ddeclusage(enum wined3d_decl_usage usage)
{
    switch (usage)
    {
4995
#define WINED3DDECLUSAGE_TO_STR(u) case u: return #u
4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITION);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_WEIGHT);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_INDICES);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_NORMAL);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_PSIZE);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TEXCOORD);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TANGENT);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BINORMAL);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TESS_FACTOR);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITIONT);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_COLOR);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_FOG);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_DEPTH);
        WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_SAMPLE);
5010 5011 5012 5013 5014 5015 5016
#undef WINED3DDECLUSAGE_TO_STR
        default:
            FIXME("Unrecognized %u declaration usage!\n", usage);
            return "unrecognized";
    }
}

5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
const char *debug_d3dinput_classification(enum wined3d_input_classification classification)
{
    switch (classification)
    {
#define WINED3D_TO_STR(x) case x: return #x
        WINED3D_TO_STR(WINED3D_INPUT_PER_VERTEX_DATA);
        WINED3D_TO_STR(WINED3D_INPUT_PER_INSTANCE_DATA);
#undef WINED3D_TO_STR
        default:
            FIXME("Unrecognized input classification %#x.\n", classification);
            return "unrecognized";
    }
}

5031
const char *debug_d3dresourcetype(enum wined3d_resource_type resource_type)
5032
{
5033
    switch (resource_type)
5034
    {
5035
#define WINED3D_TO_STR(x) case x: return #x
5036
        WINED3D_TO_STR(WINED3D_RTYPE_NONE);
5037
        WINED3D_TO_STR(WINED3D_RTYPE_BUFFER);
5038
        WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_1D);
5039
        WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_2D);
5040
        WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_3D);
5041
#undef WINED3D_TO_STR
5042
        default:
5043
            FIXME("Unrecognized resource type %#x.\n", resource_type);
5044 5045
            return "unrecognized";
    }
5046
}
5047

5048
const char *debug_d3dprimitivetype(enum wined3d_primitive_type primitive_type)
5049
{
5050
    switch (primitive_type)
5051
    {
5052
#define PRIM_TO_STR(prim) case prim: return #prim
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063
        PRIM_TO_STR(WINED3D_PT_UNDEFINED);
        PRIM_TO_STR(WINED3D_PT_POINTLIST);
        PRIM_TO_STR(WINED3D_PT_LINELIST);
        PRIM_TO_STR(WINED3D_PT_LINESTRIP);
        PRIM_TO_STR(WINED3D_PT_TRIANGLELIST);
        PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP);
        PRIM_TO_STR(WINED3D_PT_TRIANGLEFAN);
        PRIM_TO_STR(WINED3D_PT_LINELIST_ADJ);
        PRIM_TO_STR(WINED3D_PT_LINESTRIP_ADJ);
        PRIM_TO_STR(WINED3D_PT_TRIANGLELIST_ADJ);
        PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP_ADJ);
5064
        PRIM_TO_STR(WINED3D_PT_PATCH);
5065
#undef  PRIM_TO_STR
5066
        default:
5067
            FIXME("Unrecognized primitive type %#x.\n", primitive_type);
5068 5069
            return "unrecognized";
    }
5070
}
5071

5072
const char *debug_d3drenderstate(enum wined3d_render_state state)
5073 5074 5075
{
    switch (state)
    {
5076
#define D3DSTATE_TO_STR(u) case u: return #u
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179
        D3DSTATE_TO_STR(WINED3D_RS_ANTIALIAS);
        D3DSTATE_TO_STR(WINED3D_RS_TEXTUREPERSPECTIVE);
        D3DSTATE_TO_STR(WINED3D_RS_WRAPU);
        D3DSTATE_TO_STR(WINED3D_RS_WRAPV);
        D3DSTATE_TO_STR(WINED3D_RS_ZENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_FILLMODE);
        D3DSTATE_TO_STR(WINED3D_RS_SHADEMODE);
        D3DSTATE_TO_STR(WINED3D_RS_LINEPATTERN);
        D3DSTATE_TO_STR(WINED3D_RS_MONOENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_ROP2);
        D3DSTATE_TO_STR(WINED3D_RS_PLANEMASK);
        D3DSTATE_TO_STR(WINED3D_RS_ZWRITEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_ALPHATESTENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_LASTPIXEL);
        D3DSTATE_TO_STR(WINED3D_RS_SRCBLEND);
        D3DSTATE_TO_STR(WINED3D_RS_DESTBLEND);
        D3DSTATE_TO_STR(WINED3D_RS_CULLMODE);
        D3DSTATE_TO_STR(WINED3D_RS_ZFUNC);
        D3DSTATE_TO_STR(WINED3D_RS_ALPHAREF);
        D3DSTATE_TO_STR(WINED3D_RS_ALPHAFUNC);
        D3DSTATE_TO_STR(WINED3D_RS_DITHERENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_ALPHABLENDENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_FOGENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_SPECULARENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_ZVISIBLE);
        D3DSTATE_TO_STR(WINED3D_RS_SUBPIXEL);
        D3DSTATE_TO_STR(WINED3D_RS_SUBPIXELX);
        D3DSTATE_TO_STR(WINED3D_RS_STIPPLEDALPHA);
        D3DSTATE_TO_STR(WINED3D_RS_FOGCOLOR);
        D3DSTATE_TO_STR(WINED3D_RS_FOGTABLEMODE);
        D3DSTATE_TO_STR(WINED3D_RS_FOGSTART);
        D3DSTATE_TO_STR(WINED3D_RS_FOGEND);
        D3DSTATE_TO_STR(WINED3D_RS_FOGDENSITY);
        D3DSTATE_TO_STR(WINED3D_RS_STIPPLEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_COLORKEYENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_MIPMAPLODBIAS);
        D3DSTATE_TO_STR(WINED3D_RS_RANGEFOGENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_ANISOTROPY);
        D3DSTATE_TO_STR(WINED3D_RS_FLUSHBATCH);
        D3DSTATE_TO_STR(WINED3D_RS_TRANSLUCENTSORTINDEPENDENT);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILFAIL);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILZFAIL);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILPASS);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILFUNC);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILREF);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILMASK);
        D3DSTATE_TO_STR(WINED3D_RS_STENCILWRITEMASK);
        D3DSTATE_TO_STR(WINED3D_RS_TEXTUREFACTOR);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP0);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP1);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP2);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP3);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP4);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP5);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP6);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP7);
        D3DSTATE_TO_STR(WINED3D_RS_CLIPPING);
        D3DSTATE_TO_STR(WINED3D_RS_LIGHTING);
        D3DSTATE_TO_STR(WINED3D_RS_EXTENTS);
        D3DSTATE_TO_STR(WINED3D_RS_AMBIENT);
        D3DSTATE_TO_STR(WINED3D_RS_FOGVERTEXMODE);
        D3DSTATE_TO_STR(WINED3D_RS_COLORVERTEX);
        D3DSTATE_TO_STR(WINED3D_RS_LOCALVIEWER);
        D3DSTATE_TO_STR(WINED3D_RS_NORMALIZENORMALS);
        D3DSTATE_TO_STR(WINED3D_RS_COLORKEYBLENDENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_DIFFUSEMATERIALSOURCE);
        D3DSTATE_TO_STR(WINED3D_RS_SPECULARMATERIALSOURCE);
        D3DSTATE_TO_STR(WINED3D_RS_AMBIENTMATERIALSOURCE);
        D3DSTATE_TO_STR(WINED3D_RS_EMISSIVEMATERIALSOURCE);
        D3DSTATE_TO_STR(WINED3D_RS_VERTEXBLEND);
        D3DSTATE_TO_STR(WINED3D_RS_CLIPPLANEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_SOFTWAREVERTEXPROCESSING);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MIN);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSPRITEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSCALEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_A);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_B);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_C);
        D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEANTIALIAS);
        D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEMASK);
        D3DSTATE_TO_STR(WINED3D_RS_PATCHEDGESTYLE);
        D3DSTATE_TO_STR(WINED3D_RS_PATCHSEGMENTS);
        D3DSTATE_TO_STR(WINED3D_RS_DEBUGMONITORTOKEN);
        D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MAX);
        D3DSTATE_TO_STR(WINED3D_RS_INDEXEDVERTEXBLENDENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_TWEENFACTOR);
        D3DSTATE_TO_STR(WINED3D_RS_BLENDOP);
        D3DSTATE_TO_STR(WINED3D_RS_POSITIONDEGREE);
        D3DSTATE_TO_STR(WINED3D_RS_NORMALDEGREE);
        D3DSTATE_TO_STR(WINED3D_RS_SCISSORTESTENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_SLOPESCALEDEPTHBIAS);
        D3DSTATE_TO_STR(WINED3D_RS_ANTIALIASEDLINEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_MINTESSELLATIONLEVEL);
        D3DSTATE_TO_STR(WINED3D_RS_MAXTESSELLATIONLEVEL);
        D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_X);
        D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Y);
        D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Z);
        D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_W);
        D3DSTATE_TO_STR(WINED3D_RS_ENABLEADAPTIVETESSELLATION);
        D3DSTATE_TO_STR(WINED3D_RS_TWOSIDEDSTENCILMODE);
5180 5181 5182 5183
        D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFAIL);
        D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILZFAIL);
        D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILPASS);
        D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFUNC);
5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200
        D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE1);
        D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE2);
        D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE3);
        D3DSTATE_TO_STR(WINED3D_RS_SRGBWRITEENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_DEPTHBIAS);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP8);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP9);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP10);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP11);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP12);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP13);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP14);
        D3DSTATE_TO_STR(WINED3D_RS_WRAP15);
        D3DSTATE_TO_STR(WINED3D_RS_SEPARATEALPHABLENDENABLE);
        D3DSTATE_TO_STR(WINED3D_RS_SRCBLENDALPHA);
        D3DSTATE_TO_STR(WINED3D_RS_DESTBLENDALPHA);
        D3DSTATE_TO_STR(WINED3D_RS_BLENDOPALPHA);
5201
#undef D3DSTATE_TO_STR
5202 5203 5204 5205
        default:
            FIXME("Unrecognized %u render state!\n", state);
            return "unrecognized";
    }
5206 5207
}

5208
const char *debug_d3dsamplerstate(enum wined3d_sampler_state state)
5209 5210 5211
{
    switch (state)
    {
5212
#define D3DSTATE_TO_STR(u) case u: return #u
5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225
        D3DSTATE_TO_STR(WINED3D_SAMP_BORDER_COLOR);
        D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_U);
        D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_V);
        D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_W);
        D3DSTATE_TO_STR(WINED3D_SAMP_MAG_FILTER);
        D3DSTATE_TO_STR(WINED3D_SAMP_MIN_FILTER);
        D3DSTATE_TO_STR(WINED3D_SAMP_MIP_FILTER);
        D3DSTATE_TO_STR(WINED3D_SAMP_MIPMAP_LOD_BIAS);
        D3DSTATE_TO_STR(WINED3D_SAMP_MAX_MIP_LEVEL);
        D3DSTATE_TO_STR(WINED3D_SAMP_MAX_ANISOTROPY);
        D3DSTATE_TO_STR(WINED3D_SAMP_SRGB_TEXTURE);
        D3DSTATE_TO_STR(WINED3D_SAMP_ELEMENT_INDEX);
        D3DSTATE_TO_STR(WINED3D_SAMP_DMAP_OFFSET);
5226
#undef D3DSTATE_TO_STR
5227 5228 5229 5230
        default:
            FIXME("Unrecognized %u sampler state!\n", state);
            return "unrecognized";
    }
5231 5232
}

5233 5234 5235 5236
const char *debug_d3dtexturefiltertype(enum wined3d_texture_filter_type filter_type)
{
    switch (filter_type)
    {
5237
#define D3DTEXTUREFILTERTYPE_TO_STR(u) case u: return #u
5238 5239 5240 5241 5242 5243 5244 5245
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_NONE);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_POINT);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_LINEAR);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_ANISOTROPIC);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_FLAT_CUBIC);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_CUBIC);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_PYRAMIDAL_QUAD);
        D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_QUAD);
5246 5247
#undef D3DTEXTUREFILTERTYPE_TO_STR
        default:
5248
            FIXME("Unrecognized texture filter type 0x%08x.\n", filter_type);
5249 5250 5251 5252
            return "unrecognized";
    }
}

5253
const char *debug_d3dtexturestate(enum wined3d_texture_stage_state state)
5254 5255 5256
{
    switch (state)
    {
5257
#define D3DSTATE_TO_STR(u) case u: return #u
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
        D3DSTATE_TO_STR(WINED3D_TSS_COLOR_OP);
        D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG1);
        D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG2);
        D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_OP);
        D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG1);
        D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG2);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT00);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT01);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT10);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT11);
        D3DSTATE_TO_STR(WINED3D_TSS_TEXCOORD_INDEX);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LSCALE);
        D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LOFFSET);
        D3DSTATE_TO_STR(WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS);
        D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG0);
        D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG0);
        D3DSTATE_TO_STR(WINED3D_TSS_RESULT_ARG);
        D3DSTATE_TO_STR(WINED3D_TSS_CONSTANT);
5276
#undef D3DSTATE_TO_STR
5277 5278 5279 5280
        default:
            FIXME("Unrecognized %u texture state!\n", state);
            return "unrecognized";
    }
5281 5282
}

5283 5284 5285 5286
const char *debug_d3dtop(enum wined3d_texture_op d3dtop)
{
    switch (d3dtop)
    {
5287
#define D3DTOP_TO_STR(u) case u: return #u
5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313
        D3DTOP_TO_STR(WINED3D_TOP_DISABLE);
        D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG1);
        D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG2);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_2X);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_4X);
        D3DTOP_TO_STR(WINED3D_TOP_ADD);
        D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED);
        D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED_2X);
        D3DTOP_TO_STR(WINED3D_TOP_SUBTRACT);
        D3DTOP_TO_STR(WINED3D_TOP_ADD_SMOOTH);
        D3DTOP_TO_STR(WINED3D_TOP_BLEND_DIFFUSE_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_BLEND_FACTOR_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM);
        D3DTOP_TO_STR(WINED3D_TOP_BLEND_CURRENT_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_PREMODULATE);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR);
        D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA);
        D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP);
        D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP_LUMINANCE);
        D3DTOP_TO_STR(WINED3D_TOP_DOTPRODUCT3);
        D3DTOP_TO_STR(WINED3D_TOP_MULTIPLY_ADD);
        D3DTOP_TO_STR(WINED3D_TOP_LERP);
5314 5315
#undef D3DTOP_TO_STR
        default:
5316
            FIXME("Unrecognized texture op %#x.\n", d3dtop);
5317 5318 5319 5320
            return "unrecognized";
    }
}

5321 5322 5323 5324
const char *debug_d3dtstype(enum wined3d_transform_state tstype)
{
    switch (tstype)
    {
5325
#define TSTYPE_TO_STR(tstype) case tstype: return #tstype
5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336
    TSTYPE_TO_STR(WINED3D_TS_VIEW);
    TSTYPE_TO_STR(WINED3D_TS_PROJECTION);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE0);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE1);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE2);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE3);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE4);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE5);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE6);
    TSTYPE_TO_STR(WINED3D_TS_TEXTURE7);
    TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(0));
5337 5338 5339
    TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(1));
    TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(2));
    TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(3));
5340 5341
#undef TSTYPE_TO_STR
    default:
5342 5343 5344 5345
        if (tstype > 256 && tstype < 512)
        {
            FIXME("WINED3D_TS_WORLD_MATRIX(%u). 1..255 not currently supported.\n", tstype);
            return ("WINED3D_TS_WORLD_MATRIX > 0");
5346
        }
5347
        FIXME("Unrecognized transform state %#x.\n", tstype);
5348 5349 5350 5351
        return "unrecognized";
    }
}

5352
const char *debug_shader_type(enum wined3d_shader_type type)
5353 5354 5355 5356 5357 5358 5359
{
    switch(type)
    {
#define WINED3D_TO_STR(type) case type: return #type
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_PIXEL);
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_VERTEX);
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_GEOMETRY);
5360
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_HULL);
5361
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_DOMAIN);
5362
        WINED3D_TO_STR(WINED3D_SHADER_TYPE_COMPUTE);
5363 5364 5365 5366 5367 5368 5369
#undef WINED3D_TO_STR
        default:
            FIXME("Unrecognized shader type %#x.\n", type);
            return "unrecognized";
    }
}

5370
const char *debug_d3dstate(uint32_t state)
5371 5372 5373 5374 5375
{
    if (STATE_IS_RENDER(state))
        return wine_dbg_sprintf("STATE_RENDER(%s)", debug_d3drenderstate(state - STATE_RENDER(0)));
    if (STATE_IS_TEXTURESTAGE(state))
    {
5376 5377
        unsigned int texture_stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
        uint32_t texture_state = state - STATE_TEXTURESTAGE(texture_stage, 0);
5378 5379 5380 5381 5382
        return wine_dbg_sprintf("STATE_TEXTURESTAGE(%#x, %s)",
                texture_stage, debug_d3dtexturestate(texture_state));
    }
    if (STATE_IS_SAMPLER(state))
        return wine_dbg_sprintf("STATE_SAMPLER(%#x)", state - STATE_SAMPLER(0));
5383 5384 5385
    if (STATE_IS_COMPUTE_SHADER(state))
        return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
    if (STATE_IS_GRAPHICS_SHADER(state))
5386
        return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(state - STATE_SHADER(0)));
5387 5388 5389
    if (STATE_IS_COMPUTE_CONSTANT_BUFFER(state))
        return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
    if (STATE_IS_GRAPHICS_CONSTANT_BUFFER(state))
5390
        return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(state - STATE_CONSTANT_BUFFER(0)));
5391 5392 5393 5394
    if (STATE_IS_COMPUTE_SHADER_RESOURCE_BINDING(state))
        return "STATE_COMPUTE_SHADER_RESOURCE_BINDING";
    if (STATE_IS_GRAPHICS_SHADER_RESOURCE_BINDING(state))
        return "STATE_GRAPHICS_SHADER_RESOURCE_BINDING";
5395 5396
    if (STATE_IS_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING(state))
        return "STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING";
5397 5398
    if (STATE_IS_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING(state))
        return "STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING";
5399 5400 5401 5402 5403 5404 5405 5406 5407 5408
    if (STATE_IS_TRANSFORM(state))
        return wine_dbg_sprintf("STATE_TRANSFORM(%s)", debug_d3dtstype(state - STATE_TRANSFORM(0)));
    if (STATE_IS_STREAMSRC(state))
        return "STATE_STREAMSRC";
    if (STATE_IS_INDEXBUFFER(state))
        return "STATE_INDEXBUFFER";
    if (STATE_IS_VDECL(state))
        return "STATE_VDECL";
    if (STATE_IS_VIEWPORT(state))
        return "STATE_VIEWPORT";
5409 5410
    if (STATE_IS_LIGHT_TYPE(state))
        return "STATE_LIGHT_TYPE";
5411 5412 5413 5414 5415 5416 5417 5418
    if (STATE_IS_ACTIVELIGHT(state))
        return wine_dbg_sprintf("STATE_ACTIVELIGHT(%#x)", state - STATE_ACTIVELIGHT(0));
    if (STATE_IS_SCISSORRECT(state))
        return "STATE_SCISSORRECT";
    if (STATE_IS_CLIPPLANE(state))
        return wine_dbg_sprintf("STATE_CLIPPLANE(%#x)", state - STATE_CLIPPLANE(0));
    if (STATE_IS_MATERIAL(state))
        return "STATE_MATERIAL";
5419 5420
    if (STATE_IS_RASTERIZER(state))
        return "STATE_RASTERIZER";
5421 5422
    if (STATE_IS_DEPTH_BOUNDS(state))
        return "STATE_DEPTH_BOUNDS";
5423 5424
    if (STATE_IS_POINTSPRITECOORDORIGIN(state))
        return "STATE_POINTSPRITECOORDORIGIN";
5425 5426
    if (STATE_IS_BASEVERTEXINDEX(state))
        return "STATE_BASEVERTEXINDEX";
5427 5428
    if (STATE_IS_FRAMEBUFFER(state))
        return "STATE_FRAMEBUFFER";
5429 5430
    if (STATE_IS_POINT_ENABLE(state))
        return "STATE_POINT_ENABLE";
5431 5432
    if (STATE_IS_COLOR_KEY(state))
        return "STATE_COLOR_KEY";
5433 5434
    if (STATE_IS_STREAM_OUTPUT(state))
        return "STATE_STREAM_OUTPUT";
5435 5436
    if (STATE_IS_BLEND(state))
        return "STATE_BLEND";
5437 5438
    if (STATE_IS_BLEND_FACTOR(state))
        return "STATE_BLEND_FACTOR";
5439 5440 5441 5442 5443 5444
    if (STATE_IS_SAMPLE_MASK(state))
        return "STATE_SAMPLE_MASK";
    if (STATE_IS_DEPTH_STENCIL(state))
        return "STATE_DEPTH_STENCIL";
    if (STATE_IS_STENCIL_REF(state))
        return "STATE_STENCIL_REF";
5445 5446 5447 5448

    return wine_dbg_sprintf("UNKNOWN_STATE(%#x)", state);
}

5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477
const char *debug_fboattachment(GLenum attachment)
{
    switch(attachment)
    {
#define WINED3D_TO_STR(x) case x: return #x
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT0);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT1);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT2);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT3);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT4);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT5);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT6);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT7);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT8);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT9);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT10);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT11);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT12);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT13);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT14);
        WINED3D_TO_STR(GL_COLOR_ATTACHMENT15);
        WINED3D_TO_STR(GL_DEPTH_ATTACHMENT);
        WINED3D_TO_STR(GL_STENCIL_ATTACHMENT);
#undef WINED3D_TO_STR
        default:
            return wine_dbg_sprintf("Unknown FBO attachment %#x", attachment);
    }
}

5478 5479 5480
const char *debug_fbostatus(GLenum status) {
    switch(status) {
#define FBOSTATUS_TO_STR(u) case u: return #u
5481 5482 5483
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_COMPLETE);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
5484 5485
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT);
5486 5487 5488
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE);
5489 5490
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB);
5491 5492
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNSUPPORTED);
        FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNDEFINED);
5493 5494
#undef FBOSTATUS_TO_STR
        default:
5495
            FIXME("Unrecognized FBO status 0x%08x.\n", status);
5496 5497 5498 5499
            return "unrecognized";
    }
}

5500 5501 5502 5503 5504 5505 5506 5507 5508 5509
const char *debug_glerror(GLenum error) {
    switch(error) {
#define GLERROR_TO_STR(u) case u: return #u
        GLERROR_TO_STR(GL_NO_ERROR);
        GLERROR_TO_STR(GL_INVALID_ENUM);
        GLERROR_TO_STR(GL_INVALID_VALUE);
        GLERROR_TO_STR(GL_INVALID_OPERATION);
        GLERROR_TO_STR(GL_STACK_OVERFLOW);
        GLERROR_TO_STR(GL_STACK_UNDERFLOW);
        GLERROR_TO_STR(GL_OUT_OF_MEMORY);
5510
        GLERROR_TO_STR(GL_INVALID_FRAMEBUFFER_OPERATION);
5511 5512
#undef GLERROR_TO_STR
        default:
5513
            FIXME("Unrecognized GL error 0x%08x.\n", error);
5514 5515 5516 5517
            return "unrecognized";
    }
}

5518 5519 5520 5521 5522
const char *wined3d_debug_vkresult(VkResult vr)
{
    switch (vr)
    {
#define WINED3D_TO_STR(x) case x: return #x
5523
        WINED3D_TO_STR(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR);
5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556
        WINED3D_TO_STR(VK_ERROR_NOT_PERMITTED_EXT);
        WINED3D_TO_STR(VK_ERROR_FRAGMENTATION_EXT);
        WINED3D_TO_STR(VK_ERROR_INVALID_EXTERNAL_HANDLE);
        WINED3D_TO_STR(VK_ERROR_OUT_OF_POOL_MEMORY);
        WINED3D_TO_STR(VK_ERROR_INVALID_SHADER_NV);
        WINED3D_TO_STR(VK_ERROR_OUT_OF_DATE_KHR);
        WINED3D_TO_STR(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
        WINED3D_TO_STR(VK_ERROR_SURFACE_LOST_KHR);
        WINED3D_TO_STR(VK_ERROR_FRAGMENTED_POOL);
        WINED3D_TO_STR(VK_ERROR_FORMAT_NOT_SUPPORTED);
        WINED3D_TO_STR(VK_ERROR_TOO_MANY_OBJECTS);
        WINED3D_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER);
        WINED3D_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT);
        WINED3D_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT);
        WINED3D_TO_STR(VK_ERROR_LAYER_NOT_PRESENT);
        WINED3D_TO_STR(VK_ERROR_MEMORY_MAP_FAILED);
        WINED3D_TO_STR(VK_ERROR_DEVICE_LOST);
        WINED3D_TO_STR(VK_ERROR_INITIALIZATION_FAILED);
        WINED3D_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
        WINED3D_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY);
        WINED3D_TO_STR(VK_SUCCESS);
        WINED3D_TO_STR(VK_NOT_READY);
        WINED3D_TO_STR(VK_TIMEOUT);
        WINED3D_TO_STR(VK_EVENT_SET);
        WINED3D_TO_STR(VK_EVENT_RESET);
        WINED3D_TO_STR(VK_INCOMPLETE);
        WINED3D_TO_STR(VK_SUBOPTIMAL_KHR);
#undef WINED3D_TO_STR
        default:
            return wine_dbg_sprintf("unrecognised(%d)", vr);
    }
}

5557
static const char *debug_fixup_channel_source(enum fixup_channel_source source)
5558 5559 5560 5561 5562 5563 5564 5565 5566 5567
{
    switch(source)
    {
#define WINED3D_TO_STR(x) case x: return #x
        WINED3D_TO_STR(CHANNEL_SOURCE_ZERO);
        WINED3D_TO_STR(CHANNEL_SOURCE_ONE);
        WINED3D_TO_STR(CHANNEL_SOURCE_X);
        WINED3D_TO_STR(CHANNEL_SOURCE_Y);
        WINED3D_TO_STR(CHANNEL_SOURCE_Z);
        WINED3D_TO_STR(CHANNEL_SOURCE_W);
5568 5569
        WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX0);
        WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX1);
5570 5571 5572 5573 5574 5575 5576
#undef WINED3D_TO_STR
        default:
            FIXME("Unrecognized fixup_channel_source %#x\n", source);
            return "unrecognized";
    }
}

5577
static const char *debug_complex_fixup(enum complex_fixup fixup)
5578
{
5579
    switch(fixup)
5580 5581
    {
#define WINED3D_TO_STR(x) case x: return #x
5582 5583 5584
        WINED3D_TO_STR(COMPLEX_FIXUP_YUY2);
        WINED3D_TO_STR(COMPLEX_FIXUP_UYVY);
        WINED3D_TO_STR(COMPLEX_FIXUP_YV12);
5585
        WINED3D_TO_STR(COMPLEX_FIXUP_NV12);
5586
        WINED3D_TO_STR(COMPLEX_FIXUP_P8);
5587
        WINED3D_TO_STR(COMPLEX_FIXUP_YUV);
5588 5589
#undef WINED3D_TO_STR
        default:
5590
            FIXME("Unrecognized complex fixup %#x\n", fixup);
5591 5592 5593 5594 5595 5596
            return "unrecognized";
    }
}

void dump_color_fixup_desc(struct color_fixup_desc fixup)
{
5597
    if (is_complex_fixup(fixup))
5598
    {
5599
        TRACE("\tComplex: %s\n", debug_complex_fixup(get_complex_fixup(fixup)));
5600 5601 5602 5603 5604 5605 5606 5607 5608
        return;
    }

    TRACE("\tX: %s%s\n", debug_fixup_channel_source(fixup.x_source), fixup.x_sign_fixup ? ", SIGN_FIXUP" : "");
    TRACE("\tY: %s%s\n", debug_fixup_channel_source(fixup.y_source), fixup.y_sign_fixup ? ", SIGN_FIXUP" : "");
    TRACE("\tZ: %s%s\n", debug_fixup_channel_source(fixup.z_source), fixup.z_sign_fixup ? ", SIGN_FIXUP" : "");
    TRACE("\tW: %s%s\n", debug_fixup_channel_source(fixup.w_source), fixup.w_sign_fixup ? ", SIGN_FIXUP" : "");
}

5609
BOOL is_invalid_op(const struct wined3d_state *state, int stage,
5610
        enum wined3d_texture_op op, DWORD arg1, DWORD arg2, DWORD arg3)
5611
{
5612 5613 5614 5615
    if (op == WINED3D_TOP_DISABLE)
        return FALSE;
    if (state->textures[stage])
        return FALSE;
5616

5617
    if ((arg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5618 5619
            && op != WINED3D_TOP_SELECT_ARG2)
        return TRUE;
5620
    if ((arg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5621 5622
            && op != WINED3D_TOP_SELECT_ARG1)
        return TRUE;
5623
    if ((arg3 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5624 5625
            && (op == WINED3D_TOP_MULTIPLY_ADD || op == WINED3D_TOP_LERP))
        return TRUE;
5626 5627 5628 5629

    return FALSE;
}

5630 5631 5632
void get_identity_matrix(struct wined3d_matrix *mat)
{
    static const struct wined3d_matrix identity =
5633
    {
5634 5635 5636 5637
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f,
5638
    };
5639 5640 5641 5642

    *mat = identity;
}

5643 5644
void get_modelview_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
        unsigned int index, struct wined3d_matrix *mat)
5645 5646 5647 5648
{
    if (context->last_was_rhw)
        get_identity_matrix(mat);
    else
5649
        multiply_matrix(mat, &state->transforms[WINED3D_TS_VIEW], &state->transforms[WINED3D_TS_WORLD_MATRIX(index)]);
5650 5651
}

5652 5653 5654
void get_projection_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
        struct wined3d_matrix *mat)
{
5655 5656
    const struct wined3d_d3d_info *d3d_info = context->d3d_info;
    BOOL clip_control, flip;
5657 5658
    float center_offset;

5659 5660 5661 5662
    /* There are a couple of additional things we have to take into account
     * here besides the projection transformation itself:
     *   - We need to flip along the y-axis in case of offscreen rendering.
     *   - OpenGL Z range is {-Wc,...,Wc} while D3D Z range is {0,...,Wc}.
5663
     *   - <= D3D9 coordinates refer to pixel centers while GL coordinates
5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676
     *     refer to pixel corners. D3D10 fixed this particular oddity.
     *   - D3D has a top-left filling convention while GL does not specify
     *     a particular behavior, other than that that the GL implementation
     *     needs to be consistent.
     *
     * In order to handle the pixel center, we translate by 0.5 / VPw and
     * 0.5 / VPh. We test the filling convention during adapter init and
     * add a small offset to correct it if necessary. See
     * wined3d_caps_gl_ctx_test_filling_convention() for more details on how
     * we test GL and considerations regarding the added offset value.
     *
     * If we have GL_ARB_clip_control we take care of all this through
     * viewport properties and don't have to translate geometry. */
5677

5678 5679 5680 5681
    /* Projection matrices are <= d3d9, which all have integer pixel centers. */
    if (!(d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER))
        ERR("Did not expect to enter this codepath without WINED3D_PIXEL_CENTER_INTEGER.\n");

5682 5683
    clip_control = d3d_info->clip_control;
    flip = !clip_control && context->render_offscreen;
5684
    if (!clip_control)
5685
        center_offset = 1.0f + d3d_info->filling_convention_offset;
5686
    else
5687
        center_offset = 0.0f;
5688

5689 5690 5691
    if (context->last_was_rhw)
    {
        /* Transform D3D RHW coordinates to OpenGL clip coordinates. */
5692 5693 5694 5695
        float x = state->viewports[0].x;
        float y = state->viewports[0].y;
        float w = state->viewports[0].width;
        float h = state->viewports[0].height;
5696
        float x_scale = 2.0f / w;
5697
        float x_offset = (center_offset - (2.0f * x) - w) / w;
5698 5699
        float y_scale = flip ? 2.0f / h : 2.0f / -h;
        float y_offset = flip
5700 5701
                ? (center_offset - (2.0f * y) - h) / h
                : (center_offset - (2.0f * y) - h) / -h;
5702 5703
        bool zenable = state->fb.depth_stencil ?
                (state->depth_stencil_state ? state->depth_stencil_state->desc.depth : true) : false;
5704 5705
        float z_scale = zenable ? clip_control ? 1.0f : 2.0f : 0.0f;
        float z_offset = zenable ? clip_control ? 0.0f : -1.0f : 0.0f;
5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717
        const struct wined3d_matrix projection =
        {
             x_scale,     0.0f,      0.0f, 0.0f,
                0.0f,  y_scale,      0.0f, 0.0f,
                0.0f,     0.0f,   z_scale, 0.0f,
            x_offset, y_offset,  z_offset, 1.0f,
        };

        *mat = projection;
    }
    else
    {
5718
        float y_scale = flip ? -1.0f : 1.0f;
5719
        float x_offset = center_offset / state->viewports[0].width;
5720
        float y_offset = flip
5721 5722
                ? center_offset / state->viewports[0].height
                : -center_offset / state->viewports[0].height;
5723 5724
        float z_scale = clip_control ? 1.0f : 2.0f;
        float z_offset = clip_control ? 0.0f : -1.0f;
5725 5726
        const struct wined3d_matrix projection =
        {
5727 5728 5729 5730
                1.0f,     0.0f,     0.0f, 0.0f,
                0.0f,  y_scale,     0.0f, 0.0f,
                0.0f,     0.0f,  z_scale, 0.0f,
            x_offset, y_offset, z_offset, 1.0f,
5731 5732 5733 5734 5735 5736
        };

        multiply_matrix(mat, &projection, &state->transforms[WINED3D_TS_PROJECTION]);
    }
}

5737
/* Setup this textures matrix according to the texture flags. */
5738 5739
static void compute_texture_matrix(const struct wined3d_matrix *matrix, uint32_t flags, BOOL calculated_coords,
        BOOL transformed, enum wined3d_format_id format_id, BOOL ffp_proj_control, struct wined3d_matrix *out_matrix)
5740
{
5741
    struct wined3d_matrix mat;
5742

5743 5744
    if (flags == WINED3D_TTFF_DISABLE || flags == WINED3D_TTFF_COUNT1 || transformed)
    {
5745
        get_identity_matrix(out_matrix);
5746 5747 5748
        return;
    }

5749 5750 5751
    if (flags == (WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED))
    {
        ERR("Invalid texture transform flags: WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED.\n");
5752 5753 5754
        return;
    }

5755
    mat = *matrix;
5756

5757 5758 5759 5760 5761 5762 5763
    if (flags & WINED3D_TTFF_PROJECTED)
    {
        if (!ffp_proj_control)
        {
            switch (flags & ~WINED3D_TTFF_PROJECTED)
            {
                case WINED3D_TTFF_COUNT2:
5764 5765 5766 5767 5768
                    mat._14 = mat._12;
                    mat._24 = mat._22;
                    mat._34 = mat._32;
                    mat._44 = mat._42;
                    mat._12 = mat._22 = mat._32 = mat._42 = 0.0f;
5769 5770
                    break;
                case WINED3D_TTFF_COUNT3:
5771 5772 5773 5774 5775
                    mat._14 = mat._13;
                    mat._24 = mat._23;
                    mat._34 = mat._33;
                    mat._44 = mat._43;
                    mat._13 = mat._23 = mat._33 = mat._43 = 0.0f;
5776
                    break;
5777
            }
5778
        }
5779 5780 5781 5782 5783 5784 5785 5786
    }
    else
    {
        /* Under Direct3D the R/Z coord can be used for translation, under
         * OpenGL we use the Q coord instead. */
        if (!calculated_coords)
        {
            switch (format_id)
5787
            {
5788 5789 5790 5791 5792
                /* Direct3D passes the default 1.0 in the 2nd coord, while GL
                 * passes it in the 4th. Swap 2nd and 4th coord. No need to
                 * store the value of mat._41 in mat._21 because the input
                 * value to the transformation will be 0, so the matrix value
                 * is irrelevant. */
5793
                case WINED3DFMT_R32_FLOAT:
5794 5795 5796 5797
                    mat._41 = mat._21;
                    mat._42 = mat._22;
                    mat._43 = mat._23;
                    mat._44 = mat._24;
5798
                    break;
5799
                /* See above, just 3rd and 4th coord. */
5800
                case WINED3DFMT_R32G32_FLOAT:
5801 5802 5803 5804
                    mat._41 = mat._31;
                    mat._42 = mat._32;
                    mat._43 = mat._33;
                    mat._44 = mat._34;
5805
                    break;
5806 5807
                case WINED3DFMT_R32G32B32_FLOAT: /* Opengl defaults match dx defaults */
                case WINED3DFMT_R32G32B32A32_FLOAT: /* No defaults apply, all app defined */
5808

5809
                /* This is to prevent swapping the matrix lines and put the default 4th coord = 1.0
5810 5811 5812
                 * into a bad place. The division elimination below will apply to make sure the
                 * 1.0 doesn't do anything bad. The caller will set this value if the stride is 0
                 */
5813
                case WINED3DFMT_UNKNOWN: /* No texture coords, 0/0/0/1 defaults are passed */
5814 5815 5816 5817
                    break;
                default:
                    FIXME("Unexpected fixed function texture coord input\n");
            }
5818
        }
5819 5820 5821 5822 5823 5824
        if (!ffp_proj_control)
        {
            switch (flags & ~WINED3D_TTFF_PROJECTED)
            {
                /* case WINED3D_TTFF_COUNT1: Won't ever get here. */
                case WINED3D_TTFF_COUNT2:
5825
                    mat._13 = mat._23 = mat._33 = mat._43 = 0.0f;
5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839
                /* OpenGL divides the first 3 vertex coordinates by the 4th by
                 * default, which is essentially the same as D3DTTFF_PROJECTED.
                 * Make sure that the 4th coordinate evaluates to 1.0 to
                 * eliminate that.
                 *
                 * If the fixed function pipeline is used, the 4th value
                 * remains unused, so there is no danger in doing this. With
                 * vertex shaders we have a problem. Should an application hit
                 * that problem, the code here would have to check for pixel
                 * shaders, and the shader has to undo the default GL divide.
                 *
                 * A more serious problem occurs if the application passes 4
                 * coordinates in, and the 4th is != 1.0 (OpenGL default).
                 * This would have to be fixed with immediate mode draws. */
5840
                default:
5841
                    mat._14 = mat._24 = mat._34 = 0.0f; mat._44 = 1.0f;
5842
            }
5843
        }
5844
    }
5845

5846 5847 5848 5849 5850 5851
    *out_matrix = mat;
}

void get_texture_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
        unsigned int tex, struct wined3d_matrix *mat)
{
5852
    const struct wined3d_device *device = context->device;
5853 5854 5855
    BOOL generated = (state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX] & 0xffff0000)
            != WINED3DTSS_TCI_PASSTHRU;
    unsigned int coord_idx = min(state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX & 0x0000ffff],
5856
            WINED3D_MAX_TEXTURES - 1);
5857

5858
    compute_texture_matrix(&state->transforms[WINED3D_TS_TEXTURE0 + tex],
5859 5860
            state->texture_states[tex][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS],
            generated, context->last_was_rhw,
5861
            context->stream_info.use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))
5862 5863 5864 5865
            ? context->stream_info.elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->id
            : WINED3DFMT_UNKNOWN,
            device->shader_backend->shader_has_ffp_proj_control(device->shader_priv), mat);

5866
    if ((context->lastWasPow2Texture & (1u << tex)) && state->textures[tex])
5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877
    {
        if (generated)
            FIXME("Non-power-of-two texture being used with generated texture coords.\n");
        /* NP2 texcoord fixup is implemented for pixelshaders so only enable the
         * fixed-function-pipeline fixup via pow2Matrix when no PS is used. */
        if (!use_ps(state))
        {
            TRACE("Non-power-of-two texture matrix multiply fixup.\n");
            multiply_matrix(mat, mat, (struct wined3d_matrix *)state->textures[tex]->pow2_matrix);
        }
    }
5878 5879
}

5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898
void get_pointsize_minmax(const struct wined3d_context *context, const struct wined3d_state *state,
        float *out_min, float *out_max)
{
    union
    {
        DWORD d;
        float f;
    } min, max;

    min.d = state->render_states[WINED3D_RS_POINTSIZE_MIN];
    max.d = state->render_states[WINED3D_RS_POINTSIZE_MAX];

    if (min.f > max.f)
        min.f = max.f;

    *out_min = min.f;
    *out_max = max.f;
}

5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919
void get_pointsize(const struct wined3d_context *context, const struct wined3d_state *state,
        float *out_pointsize, float *out_att)
{
    /* POINTSCALEENABLE controls how point size value is treated. If set to
     * true, the point size is scaled with respect to height of viewport.
     * When set to false point size is in pixels. */
    union
    {
        DWORD d;
        float f;
    } pointsize, a, b, c;

    out_att[0] = 1.0f;
    out_att[1] = 0.0f;
    out_att[2] = 0.0f;

    pointsize.d = state->render_states[WINED3D_RS_POINTSIZE];
    a.d = state->render_states[WINED3D_RS_POINTSCALE_A];
    b.d = state->render_states[WINED3D_RS_POINTSCALE_B];
    c.d = state->render_states[WINED3D_RS_POINTSCALE_C];

5920
    /* Always use first viewport, this path does not apply to d3d10/11 multiple viewports case. */
5921 5922
    if (state->render_states[WINED3D_RS_POINTSCALEENABLE])
    {
5923
        float scale_factor = state->viewports[0].height * state->viewports[0].height;
5924 5925 5926 5927 5928 5929 5930 5931

        out_att[0] = a.f / scale_factor;
        out_att[1] = b.f / scale_factor;
        out_att[2] = c.f / scale_factor;
    }
    *out_pointsize = pointsize.f;
}

5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976
void get_fog_start_end(const struct wined3d_context *context, const struct wined3d_state *state,
        float *start, float *end)
{
    union
    {
        DWORD d;
        float f;
    } tmpvalue;

    switch (context->fog_source)
    {
        case FOGSOURCE_VS:
            *start = 1.0f;
            *end = 0.0f;
            break;

        case FOGSOURCE_COORD:
            *start = 255.0f;
            *end = 0.0f;
            break;

        case FOGSOURCE_FFP:
            tmpvalue.d = state->render_states[WINED3D_RS_FOGSTART];
            *start = tmpvalue.f;
            tmpvalue.d = state->render_states[WINED3D_RS_FOGEND];
            *end = tmpvalue.f;
            /* Special handling for fog_start == fog_end. In d3d with vertex
             * fog, everything is fogged. With table fog, everything with
             * fog_coord < fog_start is unfogged, and fog_coord > fog_start
             * is fogged. Windows drivers disagree when fog_coord == fog_start. */
            if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE && *start == *end)
            {
                *start = -INFINITY;
                *end = 0.0f;
            }
            break;

        default:
            /* This should not happen, context->fog_source is set in wined3d, not the app. */
            ERR("Unexpected fog coordinate source.\n");
            *start = 0.0f;
            *end = 0.0f;
    }
}

5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011
static BOOL wined3d_get_primary_display(WCHAR *display)
{
    DISPLAY_DEVICEW display_device;
    DWORD device_idx = 0;

    display_device.cb = sizeof(display_device);
    while (EnumDisplayDevicesW(NULL, device_idx++, &display_device, 0))
    {
        if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
        {
            lstrcpyW(display, display_device.DeviceName);
            return TRUE;
        }
    }

    return FALSE;
}

BOOL wined3d_get_primary_adapter_luid(LUID *luid)
{
    D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME open_adapter_param;
    D3DKMT_CLOSEADAPTER close_adapter_param;

    if (!wined3d_get_primary_display(open_adapter_param.DeviceName))
        return FALSE;

    if (D3DKMTOpenAdapterFromGdiDisplayName(&open_adapter_param))
        return FALSE;

    *luid = open_adapter_param.AdapterLuid;
    close_adapter_param.hAdapter = open_adapter_param.hAdapter;
    D3DKMTCloseAdapter(&close_adapter_param);
    return TRUE;
}

6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023
uint32_t wined3d_format_pack(const struct wined3d_format *format, const struct wined3d_uvec4 *value)
{
    uint32_t p = 0;

    p |= (value->x & wined3d_mask_from_size(format->red_size)) << format->red_offset;
    p |= (value->y & wined3d_mask_from_size(format->green_size)) << format->green_offset;
    p |= (value->z & wined3d_mask_from_size(format->blue_size)) << format->blue_offset;
    p |= (value->w & wined3d_mask_from_size(format->alpha_size)) << format->alpha_offset;

    return p;
}

6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042
void wined3d_format_colour_to_vk(const struct wined3d_format *format, const struct wined3d_color *c,
        VkClearColorValue *retval)
{
    if (format->attrs & WINED3D_FORMAT_ATTR_INTEGER)
    {
        retval->int32[0] = c->r;
        retval->int32[1] = c->g;
        retval->int32[2] = c->b;
        retval->int32[3] = c->a;
    }
    else
    {
        retval->float32[0] = c->r;
        retval->float32[1] = c->g;
        retval->float32[2] = c->b;
        retval->float32[3] = c->a;
    }
}

6043 6044
/* Note: It's the caller's responsibility to ensure values can be expressed
 * in the requested format. UNORM formats for example can only express values
6045 6046 6047 6048 6049 6050 6051 6052
 * in the range 0.0f -> 1.0f.
 *
 * The code below assumes that no component crosses the 32 bit boundary (like
 * e.g. a hypothetical, and totally braindead, B30G30R4 format would).
 * This function writes at least sizeof(uint32_t) bytes, or format->byte_count,
 * whichever is larger. */
void wined3d_format_convert_from_float(const struct wined3d_format *format,
        const struct wined3d_color *color, void *ret)
6053
{
6054 6055 6056
    static const struct
    {
        enum wined3d_format_id format_id;
6057 6058
        struct wined3d_vec4 mul;
        struct wined3d_uvec4 shift;
6059
    }
6060
    float_conv[] =
6061
    {
6062 6063 6064
        {WINED3DFMT_P8_UINT,           {         0.0f,    0.0f,    0.0f, 255.0f}, { 0,  0,  0,  0}},
        {WINED3DFMT_S1_UINT_D15_UNORM, {     32767.0f,    0.0f,    0.0f,   0.0f}, { 0,  0,  0,  0}},
        {WINED3DFMT_D16_UNORM,         {     65535.0f,    0.0f,    0.0f,   0.0f}, { 0,  0,  0,  0}},
6065 6066 6067 6068 6069 6070 6071 6072 6073
    };
    static const struct
    {
        enum wined3d_format_id format_id;
        struct wined3d_dvec4 mul;
        struct wined3d_uvec4 shift;
    }
    double_conv[] =
    {
6074
        {WINED3DFMT_D24_UNORM_S8_UINT, {  16777215.0, 1.0, 0.0, 0.0}, {8, 0, 0, 0}},
6075 6076
        {WINED3DFMT_X8D24_UNORM,       {  16777215.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
        {WINED3DFMT_D32_UNORM,         {4294967295.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
6077
    };
6078 6079
    enum wined3d_format_id format_id = format->id;
    struct wined3d_color colour_srgb;
6080
    struct wined3d_uvec4 idx, shift;
6081
    unsigned int i;
6082

6083
    TRACE("Converting colour %s to format %s.\n", debug_color(color), debug_d3dformat(format_id));
6084
    memset(ret, 0, max(sizeof(uint32_t), format->byte_count));
6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095

    for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
    {
        if (format_id != format_srgb_info[i].srgb_format_id)
            continue;

        wined3d_colour_srgb_from_linear(&colour_srgb, color);
        format_id = format_srgb_info[i].base_format_id;
        color = &colour_srgb;
        break;
    }
6096

6097
    for (i = 0; i < ARRAY_SIZE(float_conv); ++i)
6098
    {
6099 6100
        uint32_t *ret_i = ret;

6101
        if (format_id != float_conv[i].format_id)
6102
            continue;
6103

6104 6105 6106 6107 6108 6109 6110 6111
        idx.x = float_conv[i].shift.x / 32;
        idx.y = float_conv[i].shift.y / 32;
        idx.z = float_conv[i].shift.z / 32;
        idx.w = float_conv[i].shift.w / 32;
        shift.x = float_conv[i].shift.x % 32;
        shift.y = float_conv[i].shift.y % 32;
        shift.z = float_conv[i].shift.z % 32;
        shift.w = float_conv[i].shift.w % 32;
6112

6113 6114 6115 6116
        ret_i[idx.x] = ((uint32_t)((color->r * float_conv[i].mul.x) + 0.5f)) << shift.x;
        ret_i[idx.y] |= ((uint32_t)((color->g * float_conv[i].mul.y) + 0.5f)) << shift.y;
        ret_i[idx.z] |= ((uint32_t)((color->b * float_conv[i].mul.z) + 0.5f)) << shift.z;
        ret_i[idx.w] |= ((uint32_t)((color->a * float_conv[i].mul.w) + 0.5f)) << shift.w;
6117

6118
        return;
6119 6120 6121 6122
    }

    for (i = 0; i < ARRAY_SIZE(double_conv); ++i)
    {
6123 6124
        uint32_t *ret_i;

6125
        if (format_id != double_conv[i].format_id)
6126
            continue;
6127

6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141
        idx.x = double_conv[i].shift.x / 32;
        idx.y = double_conv[i].shift.y / 32;
        idx.z = double_conv[i].shift.z / 32;
        idx.w = double_conv[i].shift.w / 32;
        shift.x = double_conv[i].shift.x % 32;
        shift.y = double_conv[i].shift.y % 32;
        shift.z = double_conv[i].shift.z % 32;
        shift.w = double_conv[i].shift.w % 32;

        ret_i = ret;
        ret_i[idx.x] = ((uint32_t)((color->r * double_conv[i].mul.x) + 0.5)) << shift.x;
        ret_i[idx.y] |= ((uint32_t)((color->g * double_conv[i].mul.y) + 0.5)) << shift.y;
        ret_i[idx.z] |= ((uint32_t)((color->b * double_conv[i].mul.z) + 0.5)) << shift.z;
        ret_i[idx.w] |= ((uint32_t)((color->a * double_conv[i].mul.w) + 0.5)) << shift.w;
6142

6143
        return;
6144
    }
6145

6146
    if (format->attrs & WINED3D_FORMAT_ATTR_NORMALISED)
6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
    {
        uint32_t *ret_i = ret;

        idx.x = format->red_offset / 32;
        idx.y = format->green_offset / 32;
        idx.z = format->blue_offset / 32;
        idx.w = format->alpha_offset / 32;
        shift.x = format->red_offset % 32;
        shift.y = format->green_offset % 32;
        shift.z = format->blue_offset % 32;
        shift.w = format->alpha_offset % 32;

        ret_i[idx.x] = ((uint32_t)((color->r * wined3d_mask_from_size(format->red_size)) + 0.5f)) << shift.x;
        ret_i[idx.y] |= ((uint32_t)((color->g * wined3d_mask_from_size(format->green_size)) + 0.5f)) << shift.y;
        ret_i[idx.z] |= ((uint32_t)((color->b * wined3d_mask_from_size(format->blue_size)) + 0.5f)) << shift.z;
        ret_i[idx.w] |= ((uint32_t)((color->a * wined3d_mask_from_size(format->alpha_size)) + 0.5f)) << shift.w;

        return;
    }

6167 6168 6169 6170
    /* 32 bit float formats. We don't handle D32_FLOAT_S8X24_UINT for now. */
    if ((format->attrs & WINED3D_FORMAT_ATTR_FLOAT)
            && (format->red_size == 32 || format->depth_size == 32)
            && !format->stencil_size)
6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189
    {
        float *ret_f = ret;

        switch (format->byte_count)
        {
            case 16: ret_f[3] = color->a;
            case 12: ret_f[2] = color->b;
            case  8: ret_f[1] = color->g;
            case  4: ret_f[0] = color->r;
                break;

            default:
                ERR("Unexpected byte count %u, format %s.\n", format->byte_count, debug_d3dformat(format_id));
                break;
        }

        return;
    }

6190
    if ((format->attrs & WINED3D_FORMAT_ATTR_FLOAT) && format->red_size == 16)
6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214
    {
        uint16_t *ret_s = ret;

        switch (format->byte_count)
        {
            case 8:
                ret_s[3] = float_32_to_16(&color->a);
                ret_s[2] = float_32_to_16(&color->b);
                /* fall through */
            case 4:
                ret_s[1] = float_32_to_16(&color->g);
                /* fall through */
            case 2:
                ret_s[0] = float_32_to_16(&color->r);
                break;

            default:
                ERR("Unexpected byte count %u, format %s.\n", format->byte_count, debug_d3dformat(format_id));
                break;
        }

        return;
    }

6215
    FIXME("Conversion for format %s not implemented.\n", debug_d3dformat(format_id));
6216 6217
}

6218 6219
static float color_to_float(DWORD color, DWORD size, DWORD offset)
{
6220
    uint32_t mask = wined3d_mask_from_size(size);
6221 6222 6223 6224 6225 6226 6227 6228 6229 6230

    if (!size)
        return 1.0f;

    color >>= offset;
    color &= mask;

    return (float)color / (float)mask;
}

6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255
void wined3d_format_get_float_color_key(const struct wined3d_format *format,
        const struct wined3d_color_key *key, struct wined3d_color *float_colors)
{
    struct wined3d_color slop;

    switch (format->id)
    {
        case WINED3DFMT_B8G8R8_UNORM:
        case WINED3DFMT_B8G8R8A8_UNORM:
        case WINED3DFMT_B8G8R8X8_UNORM:
        case WINED3DFMT_B5G6R5_UNORM:
        case WINED3DFMT_B5G5R5X1_UNORM:
        case WINED3DFMT_B5G5R5A1_UNORM:
        case WINED3DFMT_B4G4R4A4_UNORM:
        case WINED3DFMT_B2G3R3_UNORM:
        case WINED3DFMT_R8_UNORM:
        case WINED3DFMT_A8_UNORM:
        case WINED3DFMT_B2G3R3A8_UNORM:
        case WINED3DFMT_B4G4R4X4_UNORM:
        case WINED3DFMT_R10G10B10A2_UNORM:
        case WINED3DFMT_R10G10B10A2_SNORM:
        case WINED3DFMT_R8G8B8A8_UNORM:
        case WINED3DFMT_R8G8B8X8_UNORM:
        case WINED3DFMT_R16G16_UNORM:
        case WINED3DFMT_B10G10R10A2_UNORM:
6256 6257 6258 6259
            slop.r = 0.5f / wined3d_mask_from_size(format->red_size);
            slop.g = 0.5f / wined3d_mask_from_size(format->green_size);
            slop.b = 0.5f / wined3d_mask_from_size(format->blue_size);
            slop.a = 0.5f / wined3d_mask_from_size(format->alpha_size);
6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296

            float_colors[0].r = color_to_float(key->color_space_low_value, format->red_size, format->red_offset)
                    - slop.r;
            float_colors[0].g = color_to_float(key->color_space_low_value, format->green_size, format->green_offset)
                    - slop.g;
            float_colors[0].b = color_to_float(key->color_space_low_value, format->blue_size, format->blue_offset)
                    - slop.b;
            float_colors[0].a = color_to_float(key->color_space_low_value, format->alpha_size, format->alpha_offset)
                    - slop.a;

            float_colors[1].r = color_to_float(key->color_space_high_value, format->red_size, format->red_offset)
                    + slop.r;
            float_colors[1].g = color_to_float(key->color_space_high_value, format->green_size, format->green_offset)
                    + slop.g;
            float_colors[1].b = color_to_float(key->color_space_high_value, format->blue_size, format->blue_offset)
                    + slop.b;
            float_colors[1].a = color_to_float(key->color_space_high_value, format->alpha_size, format->alpha_offset)
                    + slop.a;
            break;

        case WINED3DFMT_P8_UINT:
            float_colors[0].r = 0.0f;
            float_colors[0].g = 0.0f;
            float_colors[0].b = 0.0f;
            float_colors[0].a = (key->color_space_low_value - 0.5f) / 255.0f;

            float_colors[1].r = 0.0f;
            float_colors[1].g = 0.0f;
            float_colors[1].b = 0.0f;
            float_colors[1].a = (key->color_space_high_value + 0.5f) / 255.0f;
            break;

        default:
            ERR("Unhandled color key to float conversion for format %s.\n", debug_d3dformat(format->id));
    }
}

6297 6298 6299 6300
enum wined3d_format_id pixelformat_for_depth(DWORD depth)
{
    switch (depth)
    {
6301 6302 6303 6304 6305
        case 8:  return WINED3DFMT_P8_UINT;
        case 15: return WINED3DFMT_B5G5R5X1_UNORM;
        case 16: return WINED3DFMT_B5G6R5_UNORM;
        case 24: return WINED3DFMT_B8G8R8X8_UNORM; /* Robots needs 24bit to be WINED3DFMT_B8G8R8X8_UNORM */
        case 32: return WINED3DFMT_B8G8R8X8_UNORM; /* EVE online and the Fur demo need 32bit AdapterDisplayMode to return WINED3DFMT_B8G8R8X8_UNORM */
6306 6307 6308
        default: return WINED3DFMT_UNKNOWN;
    }
}
6309

6310 6311 6312 6313 6314
void wined3d_format_copy_data(const struct wined3d_format *format, const uint8_t *src,
        unsigned int src_row_pitch, unsigned int src_slice_pitch, uint8_t *dst, unsigned int dst_row_pitch,
        unsigned int dst_slice_pitch, unsigned int w, unsigned int h, unsigned int d)
{
    unsigned int row_block_count, row_count, row_size, slice, row;
6315
    unsigned int slice_count = d, slice_size;
6316 6317 6318 6319 6320 6321
    const uint8_t *src_row;
    uint8_t *dst_row;

    row_block_count = (w + format->block_width - 1) / format->block_width;
    row_count = (h + format->block_height - 1) / format->block_height;
    row_size = row_block_count * format->block_byte_count;
6322
    slice_size = row_size * row_count;
6323

6324 6325
    if (src_row_pitch == row_size && dst_row_pitch == row_size
            && ((src_slice_pitch == slice_size && dst_slice_pitch == slice_size) || slice_count == 1))
6326
    {
6327
        memcpy(dst, src, slice_count * slice_size);
6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341
        return;
    }

    for (slice = 0; slice < slice_count; ++slice)
    {
        for (row = 0; row < row_count; ++row)
        {
            src_row = &src[slice * src_slice_pitch + row * src_row_pitch];
            dst_row = &dst[slice * dst_slice_pitch + row * dst_row_pitch];
            memcpy(dst_row, src_row, row_size);
        }
    }
}

6342
void multiply_matrix(struct wined3d_matrix *dst, const struct wined3d_matrix *src1, const struct wined3d_matrix *src2)
6343
{
6344
    struct wined3d_matrix tmp;
6345 6346 6347

    /* Now do the multiplication 'by hand'.
       I know that all this could be optimised, but this will be done later :-) */
6348 6349 6350 6351
    tmp._11 = (src1->_11 * src2->_11) + (src1->_21 * src2->_12) + (src1->_31 * src2->_13) + (src1->_41 * src2->_14);
    tmp._21 = (src1->_11 * src2->_21) + (src1->_21 * src2->_22) + (src1->_31 * src2->_23) + (src1->_41 * src2->_24);
    tmp._31 = (src1->_11 * src2->_31) + (src1->_21 * src2->_32) + (src1->_31 * src2->_33) + (src1->_41 * src2->_34);
    tmp._41 = (src1->_11 * src2->_41) + (src1->_21 * src2->_42) + (src1->_31 * src2->_43) + (src1->_41 * src2->_44);
6352

6353 6354 6355 6356
    tmp._12 = (src1->_12 * src2->_11) + (src1->_22 * src2->_12) + (src1->_32 * src2->_13) + (src1->_42 * src2->_14);
    tmp._22 = (src1->_12 * src2->_21) + (src1->_22 * src2->_22) + (src1->_32 * src2->_23) + (src1->_42 * src2->_24);
    tmp._32 = (src1->_12 * src2->_31) + (src1->_22 * src2->_32) + (src1->_32 * src2->_33) + (src1->_42 * src2->_34);
    tmp._42 = (src1->_12 * src2->_41) + (src1->_22 * src2->_42) + (src1->_32 * src2->_43) + (src1->_42 * src2->_44);
6357

6358 6359 6360 6361
    tmp._13 = (src1->_13 * src2->_11) + (src1->_23 * src2->_12) + (src1->_33 * src2->_13) + (src1->_43 * src2->_14);
    tmp._23 = (src1->_13 * src2->_21) + (src1->_23 * src2->_22) + (src1->_33 * src2->_23) + (src1->_43 * src2->_24);
    tmp._33 = (src1->_13 * src2->_31) + (src1->_23 * src2->_32) + (src1->_33 * src2->_33) + (src1->_43 * src2->_34);
    tmp._43 = (src1->_13 * src2->_41) + (src1->_23 * src2->_42) + (src1->_33 * src2->_43) + (src1->_43 * src2->_44);
6362

6363 6364 6365 6366
    tmp._14 = (src1->_14 * src2->_11) + (src1->_24 * src2->_12) + (src1->_34 * src2->_13) + (src1->_44 * src2->_14);
    tmp._24 = (src1->_14 * src2->_21) + (src1->_24 * src2->_22) + (src1->_34 * src2->_23) + (src1->_44 * src2->_24);
    tmp._34 = (src1->_14 * src2->_31) + (src1->_24 * src2->_32) + (src1->_34 * src2->_33) + (src1->_44 * src2->_34);
    tmp._44 = (src1->_14 * src2->_41) + (src1->_24 * src2->_42) + (src1->_34 * src2->_43) + (src1->_44 * src2->_44);
6367

6368
    *dst = tmp;
6369 6370
}

6371
void wined3d_ffp_get_fs_settings(const struct wined3d_context *context, const struct wined3d_state *state,
6372
        struct ffp_frag_settings *settings, BOOL ignore_textype)
6373
{
6374 6375 6376
#define ARG1 0x01
#define ARG2 0x02
#define ARG0 0x04
6377 6378
    static const unsigned char args[WINED3D_TOP_LERP + 1] =
    {
6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408
        /* undefined                        */  0,
        /* D3DTOP_DISABLE                   */  0,
        /* D3DTOP_SELECTARG1                */  ARG1,
        /* D3DTOP_SELECTARG2                */  ARG2,
        /* D3DTOP_MODULATE                  */  ARG1 | ARG2,
        /* D3DTOP_MODULATE2X                */  ARG1 | ARG2,
        /* D3DTOP_MODULATE4X                */  ARG1 | ARG2,
        /* D3DTOP_ADD                       */  ARG1 | ARG2,
        /* D3DTOP_ADDSIGNED                 */  ARG1 | ARG2,
        /* D3DTOP_ADDSIGNED2X               */  ARG1 | ARG2,
        /* D3DTOP_SUBTRACT                  */  ARG1 | ARG2,
        /* D3DTOP_ADDSMOOTH                 */  ARG1 | ARG2,
        /* D3DTOP_BLENDDIFFUSEALPHA         */  ARG1 | ARG2,
        /* D3DTOP_BLENDTEXTUREALPHA         */  ARG1 | ARG2,
        /* D3DTOP_BLENDFACTORALPHA          */  ARG1 | ARG2,
        /* D3DTOP_BLENDTEXTUREALPHAPM       */  ARG1 | ARG2,
        /* D3DTOP_BLENDCURRENTALPHA         */  ARG1 | ARG2,
        /* D3DTOP_PREMODULATE               */  ARG1 | ARG2,
        /* D3DTOP_MODULATEALPHA_ADDCOLOR    */  ARG1 | ARG2,
        /* D3DTOP_MODULATECOLOR_ADDALPHA    */  ARG1 | ARG2,
        /* D3DTOP_MODULATEINVALPHA_ADDCOLOR */  ARG1 | ARG2,
        /* D3DTOP_MODULATEINVCOLOR_ADDALPHA */  ARG1 | ARG2,
        /* D3DTOP_BUMPENVMAP                */  ARG1 | ARG2,
        /* D3DTOP_BUMPENVMAPLUMINANCE       */  ARG1 | ARG2,
        /* D3DTOP_DOTPRODUCT3               */  ARG1 | ARG2,
        /* D3DTOP_MULTIPLYADD               */  ARG1 | ARG2 | ARG0,
        /* D3DTOP_LERP                      */  ARG1 | ARG2 | ARG0
    };
    unsigned int i;
    DWORD ttff;
6409
    DWORD cop, aop, carg0, carg1, carg2, aarg0, aarg1, aarg2;
6410
    const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6411

6412 6413
    settings->padding = 0;

6414
    for (i = 0; i < d3d_info->limits.ffp_blend_stages; ++i)
6415
    {
6416
        struct wined3d_texture *texture;
6417

6418
        settings->op[i].padding = 0;
6419
        if (state->texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
6420
        {
6421 6422
            settings->op[i].cop = WINED3D_TOP_DISABLE;
            settings->op[i].aop = WINED3D_TOP_DISABLE;
6423 6424
            settings->op[i].carg0 = settings->op[i].carg1 = settings->op[i].carg2 = ARG_UNUSED;
            settings->op[i].aarg0 = settings->op[i].aarg1 = settings->op[i].aarg2 = ARG_UNUSED;
6425
            settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6426
            settings->op[i].tmp_dst = 0;
6427
            settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6428
            settings->op[i].projected = WINED3D_PROJECTION_NONE;
6429 6430 6431
            i++;
            break;
        }
6432

6433
        if ((texture = state->textures[i]))
6434
        {
6435
            if (can_use_texture_swizzle(d3d_info, texture->resource.format))
6436 6437 6438
                settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
            else
                settings->op[i].color_fixup = texture->resource.format->color_fixup;
6439 6440
            if (ignore_textype)
            {
6441
                settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6442 6443 6444
            }
            else
            {
6445
                switch (wined3d_texture_gl(texture)->target)
6446
                {
6447
                    case GL_TEXTURE_1D:
6448
                        settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6449 6450
                        break;
                    case GL_TEXTURE_2D:
6451
                        settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_2D;
6452 6453
                        break;
                    case GL_TEXTURE_3D:
6454
                        settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_3D;
6455 6456
                        break;
                    case GL_TEXTURE_CUBE_MAP_ARB:
6457
                        settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
6458 6459
                        break;
                    case GL_TEXTURE_RECTANGLE_ARB:
6460
                        settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_RECT;
6461 6462
                        break;
                }
6463 6464
            }
        } else {
6465
            settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6466
            settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6467
        }
6468

6469 6470
        cop = state->texture_states[i][WINED3D_TSS_COLOR_OP];
        aop = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
6471

6472 6473 6474
        carg1 = (args[cop] & ARG1) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG1] : ARG_UNUSED;
        carg2 = (args[cop] & ARG2) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG2] : ARG_UNUSED;
        carg0 = (args[cop] & ARG0) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG0] : ARG_UNUSED;
6475

6476
        if (is_invalid_op(state, i, cop, carg1, carg2, carg0))
6477
        {
6478 6479
            carg0 = ARG_UNUSED;
            carg2 = ARG_UNUSED;
6480
            carg1 = WINED3DTA_CURRENT;
6481
            cop = WINED3D_TOP_SELECT_ARG1;
6482 6483
        }

6484 6485
        if (cop == WINED3D_TOP_DOTPRODUCT3)
        {
6486 6487 6488 6489 6490 6491 6492
            /* A dotproduct3 on the colorop overwrites the alphaop operation and replicates
             * the color result to the alpha component of the destination
             */
            aop = cop;
            aarg1 = carg1;
            aarg2 = carg2;
            aarg0 = carg0;
6493 6494 6495
        }
        else
        {
6496 6497 6498
            aarg1 = (args[aop] & ARG1) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] : ARG_UNUSED;
            aarg2 = (args[aop] & ARG2) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] : ARG_UNUSED;
            aarg0 = (args[aop] & ARG0) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] : ARG_UNUSED;
6499
        }
6500

6501
        if (!i && state->textures[0] && state->render_states[WINED3D_RS_COLORKEYENABLE])
6502
        {
6503 6504
            GLenum texture_dimensions;

6505
            texture = state->textures[0];
6506
            texture_dimensions = wined3d_texture_gl(texture)->target;
6507

6508 6509
            if (texture_dimensions == GL_TEXTURE_2D || texture_dimensions == GL_TEXTURE_RECTANGLE_ARB)
            {
6510
                if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT && !texture->resource.format->alpha_size)
6511
                {
6512
                    if (aop == WINED3D_TOP_DISABLE)
6513 6514
                    {
                       aarg1 = WINED3DTA_TEXTURE;
6515
                       aop = WINED3D_TOP_SELECT_ARG1;
6516
                    }
6517
                    else if (aop == WINED3D_TOP_SELECT_ARG1 && aarg1 != WINED3DTA_TEXTURE)
6518
                    {
6519
                        if (state->blend_state && state->blend_state->desc.rt[0].enable)
6520 6521
                        {
                            aarg2 = WINED3DTA_TEXTURE;
6522
                            aop = WINED3D_TOP_MODULATE;
6523 6524 6525
                        }
                        else aarg1 = WINED3DTA_TEXTURE;
                    }
6526
                    else if (aop == WINED3D_TOP_SELECT_ARG2 && aarg2 != WINED3DTA_TEXTURE)
6527
                    {
6528
                        if (state->blend_state && state->blend_state->desc.rt[0].enable)
6529 6530
                        {
                            aarg1 = WINED3DTA_TEXTURE;
6531
                            aop = WINED3D_TOP_MODULATE;
6532 6533
                        }
                        else aarg2 = WINED3DTA_TEXTURE;
6534 6535 6536 6537 6538
                    }
                }
            }
        }

6539
        if (is_invalid_op(state, i, aop, aarg1, aarg2, aarg0))
6540
        {
6541 6542
               aarg0 = ARG_UNUSED;
               aarg2 = ARG_UNUSED;
6543
               aarg1 = WINED3DTA_CURRENT;
6544
               aop = WINED3D_TOP_SELECT_ARG1;
6545 6546
        }

6547 6548 6549
        if (carg1 == WINED3DTA_TEXTURE || carg2 == WINED3DTA_TEXTURE || carg0 == WINED3DTA_TEXTURE
                || aarg1 == WINED3DTA_TEXTURE || aarg2 == WINED3DTA_TEXTURE || aarg0 == WINED3DTA_TEXTURE)
        {
6550
            ttff = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
6551
            if (ttff == (WINED3D_TTFF_PROJECTED | WINED3D_TTFF_COUNT3))
6552
                settings->op[i].projected = WINED3D_PROJECTION_COUNT3;
6553
            else if (ttff & WINED3D_TTFF_PROJECTED)
6554
                settings->op[i].projected = WINED3D_PROJECTION_COUNT4;
6555
            else
6556
                settings->op[i].projected = WINED3D_PROJECTION_NONE;
6557 6558 6559
        }
        else
        {
6560
            settings->op[i].projected = WINED3D_PROJECTION_NONE;
6561
        }
6562

6563 6564 6565 6566 6567 6568 6569 6570
        settings->op[i].cop = cop;
        settings->op[i].aop = aop;
        settings->op[i].carg0 = carg0;
        settings->op[i].carg1 = carg1;
        settings->op[i].carg2 = carg2;
        settings->op[i].aarg0 = aarg0;
        settings->op[i].aarg1 = aarg1;
        settings->op[i].aarg2 = aarg2;
6571
        settings->op[i].tmp_dst = state->texture_states[i][WINED3D_TSS_RESULT_ARG] == WINED3DTA_TEMP;
6572 6573 6574
    }

    /* Clear unsupported stages */
6575
    for(; i < WINED3D_MAX_TEXTURES; i++) {
6576 6577 6578
        memset(&settings->op[i], 0xff, sizeof(settings->op[i]));
    }

6579
    if (!state->render_states[WINED3D_RS_FOGENABLE])
6580
    {
6581
        settings->fog = WINED3D_FFP_PS_FOG_OFF;
6582
    }
6583
    else if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
6584
    {
6585
        if (use_vs(state) || state->vertex_declaration->position_transformed)
6586
        {
6587
            settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6588 6589 6590
        }
        else
        {
6591
            switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
6592
            {
6593 6594
                case WINED3D_FOG_NONE:
                case WINED3D_FOG_LINEAR:
6595
                    settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6596
                    break;
6597
                case WINED3D_FOG_EXP:
6598
                    settings->fog = WINED3D_FFP_PS_FOG_EXP;
6599
                    break;
6600
                case WINED3D_FOG_EXP2:
6601
                    settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6602 6603
                    break;
            }
6604
        }
6605 6606 6607
    }
    else
    {
6608
        switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
6609
        {
6610
            case WINED3D_FOG_LINEAR:
6611
                settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6612
                break;
6613
            case WINED3D_FOG_EXP:
6614
                settings->fog = WINED3D_FFP_PS_FOG_EXP;
6615
                break;
6616
            case WINED3D_FOG_EXP2:
6617
                settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6618 6619
                break;
        }
6620
    }
6621
    settings->sRGB_write = !d3d_info->srgb_write_control && needs_srgb_write(d3d_info, state, &state->fb);
6622
    if (d3d_info->vs_clipping || !use_vs(state) || !state->render_states[WINED3D_RS_CLIPPING]
6623
            || !state->render_states[WINED3D_RS_CLIPPLANEENABLE])
6624
    {
6625
        /* No need to emulate clipplanes if GL supports native vertex shader clipping or if
6626 6627
         * the fixed function vertex pipeline is used(which always supports clipplanes), or
         * if no clipplane is enabled
6628 6629 6630 6631 6632
         */
        settings->emul_clipplanes = 0;
    } else {
        settings->emul_clipplanes = 1;
    }
6633 6634 6635 6636 6637 6638 6639

    if (state->render_states[WINED3D_RS_COLORKEYENABLE] && state->textures[0]
            && state->textures[0]->async.color_key_flags & WINED3D_CKEY_SRC_BLT
            && settings->op[0].cop != WINED3D_TOP_DISABLE)
        settings->color_key_enabled = 1;
    else
        settings->color_key_enabled = 0;
6640 6641 6642 6643 6644 6645 6646 6647

    /* texcoords_initialized is set to meaningful values only when GL doesn't
     * support enough varyings to always pass around all the possible texture
     * coordinates.
     * This is used to avoid reading a varying not written by the vertex shader.
     * Reading uninitialized varyings on core profile contexts results in an
     * error while with builtin varyings on legacy contexts you get undefined
     * behavior. */
6648
    if (d3d_info->limits.varying_count && !d3d_info->full_ffp_varyings)
6649 6650
    {
        settings->texcoords_initialized = 0;
6651
        for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
        {
            if (use_vs(state))
            {
                if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
                    settings->texcoords_initialized |= 1u << i;
            }
            else
            {
                const struct wined3d_stream_info *si = &context->stream_info;
                unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
                if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
                        & WINED3D_FFP_TCI_MASK
6664
                        || (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
6665 6666 6667 6668 6669 6670
                    settings->texcoords_initialized |= 1u << i;
            }
        }
    }
    else
    {
6671
        settings->texcoords_initialized = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6672 6673 6674
    }

    settings->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
6675
            && state->primitive_type == WINED3D_PT_POINTLIST;
6676

6677
    if (d3d_info->ffp_alpha_test)
6678 6679 6680 6681 6682 6683
        settings->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
    else
        settings->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
                ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
                : WINED3D_CMP_ALWAYS) - 1;

6684 6685 6686 6687
    if (d3d_info->emulated_flatshading)
        settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
    else
        settings->flatshading = FALSE;
6688 6689
}

6690
const struct ffp_frag_desc *find_ffp_frag_shader(const struct wine_rb_tree *fragment_shaders,
6691
        const struct ffp_frag_settings *settings)
6692
{
6693 6694
    struct wine_rb_entry *entry = wine_rb_get(fragment_shaders, settings);
    return entry ? WINE_RB_ENTRY_VALUE(entry, struct ffp_frag_desc, entry) : NULL;
6695
}
6696

6697 6698
void add_ffp_frag_shader(struct wine_rb_tree *shaders, struct ffp_frag_desc *desc)
{
6699
    /* Note that the key is the implementation independent part of the ffp_frag_desc structure,
6700 6701 6702 6703 6704
     * whereas desc points to an extended structure with implementation specific parts. */
    if (wine_rb_put(shaders, &desc->settings, &desc->entry) == -1)
    {
        ERR("Failed to insert ffp frag shader.\n");
    }
6705
}
6706

6707 6708 6709 6710
/* Activates the texture dimension according to the bound D3D texture. Does
 * not care for the colorop or correct gl texture unit (when using nvrc).
 * Requires the caller to activate the correct unit. */
/* Context activation is done by the caller (state handler). */
6711
void texture_activate_dimensions(struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
6712
{
6713
    if (texture)
6714
    {
6715
        switch (wined3d_texture_gl(texture)->target)
6716
        {
6717
            case GL_TEXTURE_2D:
6718
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6719
                checkGLcall("glDisable(GL_TEXTURE_3D)");
6720 6721
                if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
                {
6722
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6723 6724
                    checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
                }
6725 6726
                if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
                {
6727
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6728
                    checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6729
                }
6730
                gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
6731
                checkGLcall("glEnable(GL_TEXTURE_2D)");
6732 6733
                break;
            case GL_TEXTURE_RECTANGLE_ARB:
6734
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6735
                checkGLcall("glDisable(GL_TEXTURE_2D)");
6736
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6737
                checkGLcall("glDisable(GL_TEXTURE_3D)");
6738 6739
                if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
                {
6740
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6741
                    checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6742
                }
6743
                gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_RECTANGLE_ARB);
6744
                checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
6745 6746
                break;
            case GL_TEXTURE_3D:
6747 6748
                if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
                {
6749
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6750
                    checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6751
                }
6752 6753
                if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
                {
6754
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6755 6756
                    checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
                }
6757
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6758
                checkGLcall("glDisable(GL_TEXTURE_2D)");
6759
                gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_3D);
6760
                checkGLcall("glEnable(GL_TEXTURE_3D)");
6761 6762
                break;
            case GL_TEXTURE_CUBE_MAP_ARB:
6763
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6764
                checkGLcall("glDisable(GL_TEXTURE_2D)");
6765
                gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6766
                checkGLcall("glDisable(GL_TEXTURE_3D)");
6767 6768
                if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
                {
6769
                    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6770
                    checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6771
                }
6772
                gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_ARB);
6773
                checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
6774 6775
              break;
        }
6776 6777 6778 6779
    }
    else
    {
        gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
6780
        checkGLcall("glEnable(GL_TEXTURE_2D)");
6781
        gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6782
        checkGLcall("glDisable(GL_TEXTURE_3D)");
6783 6784
        if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
        {
6785
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6786 6787
            checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
        }
6788 6789
        if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
        {
6790
            gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6791
            checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6792
        }
6793
        /* Binding textures is done by samplers. A dummy texture will be bound */
6794 6795
    }
}
6796

6797
/* Context activation is done by the caller (state handler). */
6798
void sampler_texdim(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6799
{
6800
    struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6801
    unsigned int sampler = state_id - STATE_SAMPLER(0);
6802
    unsigned int mapped_stage;
6803

6804 6805 6806
    /* No need to enable / disable anything here for unused samplers. The
     * tex_colorop handler takes care. Also no action is needed with pixel
     * shaders, or if tex_colorop will take care of this business. */
6807 6808
    mapped_stage = context_gl->tex_unit_map[sampler];
    if (mapped_stage == WINED3D_UNMAPPED_STAGE || mapped_stage >= context_gl->gl_info->limits.textures)
6809
        return;
6810
    if (sampler >= context->lowest_disabled_stage)
6811
        return;
6812
    if (isStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP)))
6813
        return;
6814

6815
    texture_activate_dimensions(state->textures[sampler], context_gl->gl_info);
6816
}
6817

6818
int wined3d_ffp_frag_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6819
{
6820 6821
    const struct ffp_frag_settings *ka = key;
    const struct ffp_frag_settings *kb = &WINE_RB_ENTRY_VALUE(entry, const struct ffp_frag_desc, entry)->settings;
6822

6823
    return memcmp(ka, kb, sizeof(*ka));
6824
}
6825

6826 6827
void wined3d_ffp_get_vs_settings(const struct wined3d_context *context,
        const struct wined3d_state *state, struct wined3d_ffp_vs_settings *settings)
6828
{
6829
    enum wined3d_material_color_source diffuse_source, emissive_source, ambient_source, specular_source;
6830 6831
    const struct wined3d_stream_info *si = &context->stream_info;
    const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6832 6833
    unsigned int coord_idx, i;

6834 6835
    memset(settings, 0, sizeof(*settings));

6836 6837
    if (si->position_transformed)
    {
6838
        settings->transformed = 1;
6839
        settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6840
        settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6841 6842 6843 6844 6845 6846 6847
        if (!state->render_states[WINED3D_RS_FOGENABLE])
            settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
        else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
            settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
        else
            settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;

6848
        for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6849 6850
        {
            coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6851
            if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6852
                settings->texcoords |= 1u << i;
6853
            settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6854
        }
6855
        if (d3d_info->full_ffp_varyings)
6856
            settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6857 6858 6859 6860 6861 6862

        if (d3d_info->emulated_flatshading)
            settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
        else
            settings->flatshading = FALSE;

6863 6864
        settings->swizzle_map = si->swizzle_map;

6865 6866 6867
        return;
    }

6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880
    switch (state->render_states[WINED3D_RS_VERTEXBLEND])
    {
        case WINED3D_VBF_DISABLE:
        case WINED3D_VBF_1WEIGHTS:
        case WINED3D_VBF_2WEIGHTS:
        case WINED3D_VBF_3WEIGHTS:
            settings->vertexblends = state->render_states[WINED3D_RS_VERTEXBLEND];
            break;
        default:
            FIXME("Unsupported vertex blending: %d\n", state->render_states[WINED3D_RS_VERTEXBLEND]);
            break;
    }

6881 6882
    settings->clipping = state->render_states[WINED3D_RS_CLIPPING]
            && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
6883
    settings->normal = !!(si->use_map & (1u << WINED3D_FFP_NORMAL));
6884 6885 6886
    settings->normalize = settings->normal && state->render_states[WINED3D_RS_NORMALIZENORMALS];
    settings->lighting = !!state->render_states[WINED3D_RS_LIGHTING];
    settings->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
6887
    settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6888
    settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6889

6890 6891 6892 6893 6894 6895
    wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
            &ambient_source, &specular_source, state, si);
    settings->diffuse_source = diffuse_source;
    settings->emissive_source = emissive_source;
    settings->ambient_source = ambient_source;
    settings->specular_source = specular_source;
6896

6897
    for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6898 6899
    {
        coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6900
        if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6901
            settings->texcoords |= 1u << i;
6902
        settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6903
    }
6904
    if (d3d_info->full_ffp_varyings)
6905
        settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6906

6907
    for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
6908
    {
6909
        if (!state->light_state.lights[i])
6910 6911
            continue;

6912
        switch (state->light_state.lights[i]->OriginalParms.type)
6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926
        {
            case WINED3D_LIGHT_POINT:
                ++settings->point_light_count;
                break;
            case WINED3D_LIGHT_SPOT:
                ++settings->spot_light_count;
                break;
            case WINED3D_LIGHT_DIRECTIONAL:
                ++settings->directional_light_count;
                break;
            case WINED3D_LIGHT_PARALLELPOINT:
                ++settings->parallel_point_light_count;
                break;
            default:
6927
                FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
6928 6929
                break;
        }
6930 6931 6932 6933 6934
    }

    if (!state->render_states[WINED3D_RS_FOGENABLE])
        settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
    else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6935
    {
6936
        settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6937

6938 6939 6940 6941
        if (state->transforms[WINED3D_TS_PROJECTION]._14 == 0.0f
                && state->transforms[WINED3D_TS_PROJECTION]._24 == 0.0f
                && state->transforms[WINED3D_TS_PROJECTION]._34 == 0.0f
                && state->transforms[WINED3D_TS_PROJECTION]._44 == 1.0f)
6942 6943
            settings->ortho_fog = 1;
    }
6944 6945 6946 6947 6948 6949 6950
    else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE)
        settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;
    else if (state->render_states[WINED3D_RS_RANGEFOGENABLE])
        settings->fog_mode = WINED3D_FFP_VS_FOG_RANGE;
    else
        settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;

6951 6952 6953 6954 6955
    if (d3d_info->emulated_flatshading)
        settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
    else
        settings->flatshading = FALSE;

6956
    settings->swizzle_map = si->swizzle_map;
6957 6958
}

6959
int wined3d_ffp_vertex_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6960 6961 6962 6963 6964 6965 6966 6967
{
    const struct wined3d_ffp_vs_settings *ka = key;
    const struct wined3d_ffp_vs_settings *kb = &WINE_RB_ENTRY_VALUE(entry,
            const struct wined3d_ffp_vs_desc, entry)->settings;

    return memcmp(ka, kb, sizeof(*ka));
}

6968
const char *wined3d_debug_location(uint32_t location)
6969
{
6970
    struct debug_buffer buffer;
6971 6972
    const char *prefix = "";
    const char *suffix = "";
6973

6974 6975 6976 6977 6978 6979 6980
    if (wined3d_popcount(location) > 16)
    {
        prefix = "~(";
        location = ~location;
        suffix = ")";
    }

6981 6982
    init_debug_buffer(&buffer, "0");
#define LOCATION_TO_STR(x) if (location & x) { debug_append(&buffer, #x, " | "); location &= ~x; }
6983 6984
    LOCATION_TO_STR(WINED3D_LOCATION_DISCARDED);
    LOCATION_TO_STR(WINED3D_LOCATION_SYSMEM);
6985
    LOCATION_TO_STR(WINED3D_LOCATION_CLEARED);
6986
    LOCATION_TO_STR(WINED3D_LOCATION_BUFFER);
6987
    LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_RGB);
6988
    LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_SRGB);
6989 6990 6991
    LOCATION_TO_STR(WINED3D_LOCATION_DRAWABLE);
    LOCATION_TO_STR(WINED3D_LOCATION_RB_MULTISAMPLE);
    LOCATION_TO_STR(WINED3D_LOCATION_RB_RESOLVED);
6992
#undef LOCATION_TO_STR
6993 6994
    if (location)
        FIXME("Unrecognized location flag(s) %#x.\n", location);
6995

6996
    return wine_dbg_sprintf("%s%s%s", prefix, buffer.str, suffix);
6997
}
6998

6999 7000 7001 7002 7003 7004 7005 7006 7007 7008
const char *wined3d_debug_feature_level(enum wined3d_feature_level level)
{
    switch (level)
    {
#define LEVEL_TO_STR(level) case level: return #level
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_5);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_6);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_7);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_8);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_1);
7009 7010
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_2);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_3);
7011 7012 7013 7014 7015 7016 7017 7018 7019 7020
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10_1);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11);
        LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11_1);
#undef LEVEL_TO_STR
        default:
            return wine_dbg_sprintf("%#x", level);
    }
}

7021 7022
/* Print a floating point value with the %.8e format specifier, always using
 * '.' as decimal separator. */
7023 7024
void wined3d_ftoa(float value, char *s)
{
7025
    int idx = 1;
7026 7027

    if (copysignf(1.0f, value) < 0.0f)
7028
        ++idx;
7029

7030 7031 7032
    /* Be sure to allocate a buffer of at least 17 characters for the result
       as sprintf may return a 3 digit exponent when using the MSVC runtime
       instead of a 2 digit exponent. */
7033 7034 7035
    sprintf(s, "%.8e", value);
    if (isfinite(value))
        s[idx] = '.';
7036
}
7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048

void wined3d_release_dc(HWND window, HDC dc)
{
    /* You'd figure ReleaseDC() would fail if the DC doesn't match the window.
     * However, that's not what actually happens, and there are user32 tests
     * that confirm ReleaseDC() with the wrong window is supposed to succeed.
     * So explicitly check that the DC belongs to the window, since we want to
     * avoid releasing a DC that belongs to some other window if the original
     * window was already destroyed. */
    if (WindowFromDC(dc) != window)
        WARN("DC %p does not belong to window %p.\n", dc, window);
    else if (!ReleaseDC(window, dc))
7049
        ERR("Failed to release device context %p, last error %#lx.\n", dc, GetLastError());
7050
}
7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072

BOOL wined3d_clip_blit(const RECT *clip_rect, RECT *clipped, RECT *other)
{
    RECT orig = *clipped;
    float scale_x = (float)(orig.right - orig.left) / (float)(other->right - other->left);
    float scale_y = (float)(orig.bottom - orig.top) / (float)(other->bottom - other->top);

    IntersectRect(clipped, clipped, clip_rect);

    if (IsRectEmpty(clipped))
    {
        SetRectEmpty(other);
        return FALSE;
    }

    other->left += (LONG)((clipped->left - orig.left) / scale_x);
    other->top += (LONG)((clipped->top - orig.top) / scale_y);
    other->right -= (LONG)((orig.right - clipped->right) / scale_x);
    other->bottom -= (LONG)((orig.bottom - clipped->bottom) / scale_y);

    return TRUE;
}
7073 7074 7075 7076

void wined3d_gl_limits_get_uniform_block_range(const struct wined3d_gl_limits *gl_limits,
        enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
{
7077
    unsigned int i;
7078

7079 7080 7081 7082 7083 7084 7085 7086
    *base = 0;
    for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
    {
        *count = gl_limits->uniform_blocks[i];
        if (i == shader_type)
            return;
        *base += *count;
    }
7087

7088
    ERR("Unrecognized shader type %#x.\n", shader_type);
7089 7090
    *count = 0;
}
7091 7092 7093 7094

void wined3d_gl_limits_get_texture_unit_range(const struct wined3d_gl_limits *gl_limits,
        enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
{
7095 7096
    unsigned int i;

7097
    if (shader_type == WINED3D_SHADER_TYPE_COMPUTE)
7098
    {
7099 7100 7101
        if (gl_limits->combined_samplers == gl_limits->graphics_samplers)
            *base = 0;
        else
7102
            *base = gl_limits->graphics_samplers;
7103
        *count = gl_limits->samplers[WINED3D_SHADER_TYPE_COMPUTE];
7104
        return;
7105 7106
    }

7107
    *base = 0;
7108 7109 7110 7111 7112 7113 7114
    for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
    {
        *count = gl_limits->samplers[i];
        if (i == shader_type)
            return;
        *base += *count;
    }
7115

7116
    ERR("Unrecognized shader type %#x.\n", shader_type);
7117
    *count = 0;
7118
}
7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138

BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE_T size)
{
    SIZE_T max_capacity, new_capacity;
    void *new_elements;

    if (count <= *capacity)
        return TRUE;

    max_capacity = ~(SIZE_T)0 / size;
    if (count > max_capacity)
        return FALSE;

    new_capacity = max(1, *capacity);
    while (new_capacity < count && new_capacity <= max_capacity / 2)
        new_capacity *= 2;
    if (new_capacity < count)
        new_capacity = count;

    if (!*elements)
7139
        new_elements = heap_alloc_zero(new_capacity * size);
7140 7141 7142 7143 7144 7145 7146 7147 7148
    else
        new_elements = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *elements, new_capacity * size);
    if (!new_elements)
        return FALSE;

    *elements = new_elements;
    *capacity = new_capacity;
    return TRUE;
}
7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442

static void swap_rows(float **a, float **b)
{
    float *tmp = *a;

    *a = *b;
    *b = tmp;
}

BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
{
    float wtmp[4][8];
    float m0, m1, m2, m3, s;
    float *r0, *r1, *r2, *r3;

    r0 = wtmp[0];
    r1 = wtmp[1];
    r2 = wtmp[2];
    r3 = wtmp[3];

    r0[0] = m->_11;
    r0[1] = m->_12;
    r0[2] = m->_13;
    r0[3] = m->_14;
    r0[4] = 1.0f;
    r0[5] = r0[6] = r0[7] = 0.0f;

    r1[0] = m->_21;
    r1[1] = m->_22;
    r1[2] = m->_23;
    r1[3] = m->_24;
    r1[5] = 1.0f;
    r1[4] = r1[6] = r1[7] = 0.0f;

    r2[0] = m->_31;
    r2[1] = m->_32;
    r2[2] = m->_33;
    r2[3] = m->_34;
    r2[6] = 1.0f;
    r2[4] = r2[5] = r2[7] = 0.0f;

    r3[0] = m->_41;
    r3[1] = m->_42;
    r3[2] = m->_43;
    r3[3] = m->_44;
    r3[7] = 1.0f;
    r3[4] = r3[5] = r3[6] = 0.0f;

    /* Choose pivot - or die. */
    if (fabsf(r3[0]) > fabsf(r2[0]))
        swap_rows(&r3, &r2);
    if (fabsf(r2[0]) > fabsf(r1[0]))
        swap_rows(&r2, &r1);
    if (fabsf(r1[0]) > fabsf(r0[0]))
        swap_rows(&r1, &r0);
    if (r0[0] == 0.0f)
        return FALSE;

    /* Eliminate first variable. */
    m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
    s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
    s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
    s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
    s = r0[4];
    if (s != 0.0f)
    {
        r1[4] -= m1 * s;
        r2[4] -= m2 * s;
        r3[4] -= m3 * s;
    }
    s = r0[5];
    if (s != 0.0f)
    {
        r1[5] -= m1 * s;
        r2[5] -= m2 * s;
        r3[5] -= m3 * s;
    }
    s = r0[6];
    if (s != 0.0f)
    {
        r1[6] -= m1 * s;
        r2[6] -= m2 * s;
        r3[6] -= m3 * s;
    }
    s = r0[7];
    if (s != 0.0f)
    {
        r1[7] -= m1 * s;
        r2[7] -= m2 * s;
        r3[7] -= m3 * s;
    }

    /* Choose pivot - or die. */
    if (fabsf(r3[1]) > fabsf(r2[1]))
        swap_rows(&r3, &r2);
    if (fabsf(r2[1]) > fabsf(r1[1]))
        swap_rows(&r2, &r1);
    if (r1[1] == 0.0f)
        return FALSE;

    /* Eliminate second variable. */
    m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
    r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
    r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
    s = r1[4];
    if (s != 0.0f)
    {
        r2[4] -= m2 * s;
        r3[4] -= m3 * s;
    }
    s = r1[5];
    if (s != 0.0f)
    {
        r2[5] -= m2 * s;
        r3[5] -= m3 * s;
    }
    s = r1[6];
    if (s != 0.0f)
    {
        r2[6] -= m2 * s;
        r3[6] -= m3 * s;
    }
    s = r1[7];
    if (s != 0.0f)
    {
        r2[7] -= m2 * s;
        r3[7] -= m3 * s;
    }

    /* Choose pivot - or die. */
    if (fabsf(r3[2]) > fabsf(r2[2]))
        swap_rows(&r3, &r2);
    if (r2[2] == 0.0f)
        return FALSE;

    /* Eliminate third variable. */
    m3 = r3[2] / r2[2];
    r3[3] -= m3 * r2[3];
    r3[4] -= m3 * r2[4];
    r3[5] -= m3 * r2[5];
    r3[6] -= m3 * r2[6];
    r3[7] -= m3 * r2[7];

    /* Last check. */
    if (r3[3] == 0.0f)
        return FALSE;

    /* Back substitute row 3. */
    s = 1.0f / r3[3];
    r3[4] *= s;
    r3[5] *= s;
    r3[6] *= s;
    r3[7] *= s;

    /* Back substitute row 2. */
    m2 = r2[3];
    s = 1.0f / r2[2];
    r2[4] = s * (r2[4] - r3[4] * m2);
    r2[5] = s * (r2[5] - r3[5] * m2);
    r2[6] = s * (r2[6] - r3[6] * m2);
    r2[7] = s * (r2[7] - r3[7] * m2);
    m1 = r1[3];
    r1[4] -= r3[4] * m1;
    r1[5] -= r3[5] * m1;
    r1[6] -= r3[6] * m1;
    r1[7] -= r3[7] * m1;
    m0 = r0[3];
    r0[4] -= r3[4] * m0;
    r0[5] -= r3[5] * m0;
    r0[6] -= r3[6] * m0;
    r0[7] -= r3[7] * m0;

    /* Back substitute row 1. */
    m1 = r1[2];
    s = 1.0f / r1[1];
    r1[4] = s * (r1[4] - r2[4] * m1);
    r1[5] = s * (r1[5] - r2[5] * m1);
    r1[6] = s * (r1[6] - r2[6] * m1);
    r1[7] = s * (r1[7] - r2[7] * m1);
    m0 = r0[2];
    r0[4] -= r2[4] * m0;
    r0[5] -= r2[5] * m0;
    r0[6] -= r2[6] * m0;
    r0[7] -= r2[7] * m0;

    /* Back substitute row 0. */
    m0 = r0[1];
    s = 1.0f / r0[0];
    r0[4] = s * (r0[4] - r1[4] * m0);
    r0[5] = s * (r0[5] - r1[5] * m0);
    r0[6] = s * (r0[6] - r1[6] * m0);
    r0[7] = s * (r0[7] - r1[7] * m0);

    out->_11 = r0[4];
    out->_12 = r0[5];
    out->_13 = r0[6];
    out->_14 = r0[7];
    out->_21 = r1[4];
    out->_22 = r1[5];
    out->_23 = r1[6];
    out->_24 = r1[7];
    out->_31 = r2[4];
    out->_32 = r2[5];
    out->_33 = r2[6];
    out->_34 = r2[7];
    out->_41 = r3[4];
    out->_42 = r3[5];
    out->_43 = r3[6];
    out->_44 = r3[7];

    return TRUE;
}

/* Taken and adapted from Mesa. */
static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
{
    float pos, neg, t, det;
    struct wined3d_matrix temp;

    /* Calculate the determinant of upper left 3x3 submatrix and
     * determine if the matrix is singular. */
    pos = neg = 0.0f;
    t =  in->_11 * in->_22 * in->_33;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;

    t =  in->_21 * in->_32 * in->_13;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;
    t =  in->_31 * in->_12 * in->_23;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;

    t = -in->_31 * in->_22 * in->_13;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;
    t = -in->_21 * in->_12 * in->_33;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;

    t = -in->_11 * in->_32 * in->_23;
    if (t >= 0.0f)
        pos += t;
    else
        neg += t;

    det = pos + neg;

    if (fabsf(det) < 1e-25f)
        return FALSE;

    det = 1.0f / det;
    temp._11 =  (in->_22 * in->_33 - in->_32 * in->_23) * det;
    temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
    temp._13 =  (in->_12 * in->_23 - in->_22 * in->_13) * det;
    temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
    temp._22 =  (in->_11 * in->_33 - in->_31 * in->_13) * det;
    temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
    temp._31 =  (in->_21 * in->_32 - in->_31 * in->_22) * det;
    temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
    temp._33 =  (in->_11 * in->_22 - in->_21 * in->_12) * det;

    *out = temp;
    return TRUE;
}

void compute_normal_matrix(float *normal_matrix, BOOL legacy_lighting,
        const struct wined3d_matrix *modelview)
{
    struct wined3d_matrix mv;
    unsigned int i, j;

    mv = *modelview;
    if (legacy_lighting)
        invert_matrix_3d(&mv, &mv);
    else
        invert_matrix(&mv, &mv);
    /* Tests show that singular modelview matrices are used unchanged as normal
     * matrices on D3D3 and older. There seems to be no clearly consistent
     * behavior on newer D3D versions so always follow older ddraw behavior. */
    for (i = 0; i < 3; ++i)
        for (j = 0; j < 3; ++j)
            normal_matrix[i * 3 + j] = (&mv._11)[j * 4 + i];
}
7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655

static void wined3d_allocator_release_block(struct wined3d_allocator *allocator,
        struct wined3d_allocator_block *block)
{
    block->parent = allocator->free;
    allocator->free = block;
}

static struct wined3d_allocator_block *wined3d_allocator_acquire_block(struct wined3d_allocator *allocator)
{
    struct wined3d_allocator_block *block;

    if (!allocator->free)
        return heap_alloc(sizeof(*block));

    block = allocator->free;
    allocator->free = block->parent;

    return block;
}

void wined3d_allocator_block_free(struct wined3d_allocator_block *block)
{
    struct wined3d_allocator *allocator = block->chunk->allocator;
    struct wined3d_allocator_block *parent;

    while ((parent = block->parent) && block->sibling->free)
    {
        list_remove(&block->sibling->entry);
        wined3d_allocator_release_block(allocator, block->sibling);
        wined3d_allocator_release_block(allocator, block);
        block = parent;
    }

    block->free = true;
    list_add_head(&block->chunk->available[block->order], &block->entry);
}

static void wined3d_allocator_block_init(struct wined3d_allocator_block *block,
        struct wined3d_allocator_chunk *chunk, struct wined3d_allocator_block *parent,
        struct wined3d_allocator_block *sibling, unsigned int order, size_t offset, bool free)
{
    list_init(&block->entry);
    block->chunk = chunk;
    block->parent = parent;
    block->sibling = sibling;
    block->order = order;
    block->offset = offset;
    block->free = free;
}

void wined3d_allocator_chunk_cleanup(struct wined3d_allocator_chunk *chunk)
{
    struct wined3d_allocator_block *block;
    size_t i;

    if (list_empty(&chunk->available[0]))
    {
        ERR("Chunk %p is not empty.\n", chunk);
        return;
    }

    for (i = 1; i < ARRAY_SIZE(chunk->available); ++i)
    {
        if (!list_empty(&chunk->available[i]))
        {
            ERR("Chunk %p is not empty.\n", chunk);
            return;
        }
    }

    block = LIST_ENTRY(list_head(&chunk->available[0]), struct wined3d_allocator_block, entry);
    wined3d_allocator_release_block(chunk->allocator, block);
}

bool wined3d_allocator_chunk_init(struct wined3d_allocator_chunk *chunk, struct wined3d_allocator *allocator)
{
    struct wined3d_allocator_block *block;
    unsigned int i;

    if (!(block = wined3d_allocator_acquire_block(allocator)))
        return false;
    wined3d_allocator_block_init(block, chunk, NULL, NULL, 0, 0, true);

    list_init(&chunk->entry);
    for (i = 0; i < ARRAY_SIZE(chunk->available); ++i)
    {
        list_init(&chunk->available[i]);
    }
    list_add_head(&chunk->available[0], &block->entry);
    chunk->allocator = allocator;
    chunk->map_count = 0;
    chunk->map_ptr = NULL;

    return true;
}

void wined3d_allocator_cleanup(struct wined3d_allocator *allocator)
{
    struct wined3d_allocator_chunk *chunk, *chunk2;
    struct wined3d_allocator_block *block, *next;
    size_t i;

    for (i = 0; i < allocator->pool_count; ++i)
    {
        LIST_FOR_EACH_ENTRY_SAFE(chunk, chunk2, &allocator->pools[i].chunks, struct wined3d_allocator_chunk, entry)
        {
            list_remove(&chunk->entry);
            allocator->ops->allocator_destroy_chunk(chunk);
        }
    }
    heap_free(allocator->pools);

    next = allocator->free;
    while ((block = next))
    {
        next = block->parent;
        heap_free(block);
    }
}

static struct wined3d_allocator_block *wined3d_allocator_chunk_allocate(struct wined3d_allocator_chunk *chunk,
        unsigned int order)
{
    struct wined3d_allocator_block *block, *left, *right;
    unsigned int i = order;

    while (i)
    {
        if (!list_empty(&chunk->available[i]))
            break;
        --i;
    }

    if (list_empty(&chunk->available[i]))
        return NULL;

    block = LIST_ENTRY(list_head(&chunk->available[i]), struct wined3d_allocator_block, entry);
    list_remove(&block->entry);
    block->free = false;

    while (i < order)
    {
        if (!(left = wined3d_allocator_acquire_block(chunk->allocator)))
        {
            ERR("Failed to allocate left.\n");
            break;
        }

        if (!(right = wined3d_allocator_acquire_block(chunk->allocator)))
        {
            ERR("Failed to allocate right.\n");
            wined3d_allocator_release_block(chunk->allocator, left);
            break;
        }

        wined3d_allocator_block_init(left, chunk, block, right, block->order + 1, block->offset, false);
        wined3d_allocator_block_init(right, chunk, block, left, block->order + 1,
                block->offset + (WINED3D_ALLOCATOR_CHUNK_SIZE >> left->order), true);
        list_add_head(&chunk->available[right->order], &right->entry);

        block = left;
        ++i;
    }

    return block;
}

struct wined3d_allocator_block *wined3d_allocator_allocate(struct wined3d_allocator *allocator,
        struct wined3d_context *context, unsigned int memory_type, size_t size)
{
    struct wined3d_allocator_chunk *chunk;
    struct wined3d_allocator_block *block;
    unsigned int order;

    if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
        return NULL;

    if (size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE)
        order = WINED3D_ALLOCATOR_CHUNK_ORDER_COUNT - 1;
    else
        order = wined3d_log2i(WINED3D_ALLOCATOR_CHUNK_SIZE / size);

    LIST_FOR_EACH_ENTRY(chunk, &allocator->pools[memory_type].chunks, struct wined3d_allocator_chunk, entry)
    {
        if ((block = wined3d_allocator_chunk_allocate(chunk, order)))
            return block;
    }

    if (!(chunk = allocator->ops->allocator_create_chunk(allocator,
            context, memory_type, WINED3D_ALLOCATOR_CHUNK_SIZE)))
        return NULL;

    if (!(block = wined3d_allocator_chunk_allocate(chunk, order)))
        return NULL;

    return block;
}

bool wined3d_allocator_init(struct wined3d_allocator *allocator,
        size_t pool_count, const struct wined3d_allocator_ops *allocator_ops)
{
    size_t i;

    allocator->ops = allocator_ops;
    allocator->pool_count = pool_count;
    if (!(allocator->pools = heap_calloc(pool_count, sizeof(*allocator->pools))))
        return false;
    for (i = 0; i < pool_count; ++i)
    {
        list_init(&allocator->pools[i].chunks);
    }

7656 7657
    allocator->free = NULL;

7658 7659
    return true;
}