wined3d: Remove the no longer needed fragment_caps.srgb_texture flag.
[wine.git] / dlls / wined3d / utils.c
blob7e4fe21e727d2eac4088f266db2aee8e5e418a38
1 /*
2 * Utility functions for the WineD3D Library
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * 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
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * 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
27 #include <stdio.h>
29 #include "wined3d_private.h"
30 #include "wined3d_gl.h"
31 #include "wined3d_vk.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35 #define WINED3D_FORMAT_FOURCC_BASE (WINED3DFMT_BC7_UNORM_SRGB + 1)
37 static const struct
39 enum wined3d_format_id id;
40 unsigned int idx;
42 format_index_remap[] =
44 {WINED3DFMT_UYVY, WINED3D_FORMAT_FOURCC_BASE},
45 {WINED3DFMT_YUY2, WINED3D_FORMAT_FOURCC_BASE + 1},
46 {WINED3DFMT_YV12, WINED3D_FORMAT_FOURCC_BASE + 2},
47 {WINED3DFMT_DXT1, WINED3D_FORMAT_FOURCC_BASE + 3},
48 {WINED3DFMT_DXT2, WINED3D_FORMAT_FOURCC_BASE + 4},
49 {WINED3DFMT_DXT3, WINED3D_FORMAT_FOURCC_BASE + 5},
50 {WINED3DFMT_DXT4, WINED3D_FORMAT_FOURCC_BASE + 6},
51 {WINED3DFMT_DXT5, WINED3D_FORMAT_FOURCC_BASE + 7},
52 {WINED3DFMT_MULTI2_ARGB8, WINED3D_FORMAT_FOURCC_BASE + 8},
53 {WINED3DFMT_G8R8_G8B8, WINED3D_FORMAT_FOURCC_BASE + 9},
54 {WINED3DFMT_R8G8_B8G8, WINED3D_FORMAT_FOURCC_BASE + 10},
55 {WINED3DFMT_ATI1N, WINED3D_FORMAT_FOURCC_BASE + 11},
56 {WINED3DFMT_ATI2N, WINED3D_FORMAT_FOURCC_BASE + 12},
57 {WINED3DFMT_INST, WINED3D_FORMAT_FOURCC_BASE + 13},
58 {WINED3DFMT_NVDB, WINED3D_FORMAT_FOURCC_BASE + 14},
59 {WINED3DFMT_NVHU, WINED3D_FORMAT_FOURCC_BASE + 15},
60 {WINED3DFMT_NVHS, WINED3D_FORMAT_FOURCC_BASE + 16},
61 {WINED3DFMT_INTZ, WINED3D_FORMAT_FOURCC_BASE + 17},
62 {WINED3DFMT_RESZ, WINED3D_FORMAT_FOURCC_BASE + 18},
63 {WINED3DFMT_NULL, WINED3D_FORMAT_FOURCC_BASE + 19},
64 {WINED3DFMT_R16, WINED3D_FORMAT_FOURCC_BASE + 20},
65 {WINED3DFMT_AL16, WINED3D_FORMAT_FOURCC_BASE + 21},
66 {WINED3DFMT_NV12, WINED3D_FORMAT_FOURCC_BASE + 22},
67 {WINED3DFMT_ATOC, WINED3D_FORMAT_FOURCC_BASE + 23},
70 #define WINED3D_FORMAT_COUNT (WINED3D_FORMAT_FOURCC_BASE + ARRAY_SIZE(format_index_remap))
72 struct wined3d_format_channels
74 enum wined3d_format_id id;
75 DWORD red_size, green_size, blue_size, alpha_size;
76 DWORD red_offset, green_offset, blue_offset, alpha_offset;
77 UINT bpp;
78 BYTE depth_size, stencil_size;
79 const char *channels;
82 static const struct wined3d_format_channels formats[] =
84 /* size offset
85 * format id r g b a r g b a bpp z s channels */
86 {WINED3DFMT_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
87 /* FourCC formats */
88 {WINED3DFMT_UYVY, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
89 {WINED3DFMT_YUY2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
90 {WINED3DFMT_YV12, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
91 {WINED3DFMT_NV12, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
92 {WINED3DFMT_DXT1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
93 {WINED3DFMT_DXT2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
94 {WINED3DFMT_DXT3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
95 {WINED3DFMT_DXT4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
96 {WINED3DFMT_DXT5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
97 {WINED3DFMT_MULTI2_ARGB8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
98 {WINED3DFMT_G8R8_G8B8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
99 {WINED3DFMT_R8G8_B8G8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
100 /* Hmm? */
101 {WINED3DFMT_R8G8_SNORM_Cx, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
102 {WINED3DFMT_R11G11B10_FLOAT, 11, 11, 10, 0, 0, 11, 22, 0, 4, 0, 0, "FFF"},
103 /* Palettized formats */
104 {WINED3DFMT_P8_UINT_A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 8, 2, 0, 0},
105 {WINED3DFMT_P8_UINT, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
106 /* Standard ARGB formats. */
107 {WINED3DFMT_B8G8R8_UNORM, 8, 8, 8, 0, 16, 8, 0, 0, 3, 0, 0, "uuu"},
108 {WINED3DFMT_B5G6R5_UNORM, 5, 6, 5, 0, 11, 5, 0, 0, 2, 0, 0, "uuu"},
109 {WINED3DFMT_B5G5R5X1_UNORM, 5, 5, 5, 0, 10, 5, 0, 0, 2, 0, 0, "uuuX"},
110 {WINED3DFMT_B5G5R5A1_UNORM, 5, 5, 5, 1, 10, 5, 0, 15, 2, 0, 0, "uuuu"},
111 {WINED3DFMT_B4G4R4A4_UNORM, 4, 4, 4, 4, 8, 4, 0, 12, 2, 0, 0, "uuuu"},
112 {WINED3DFMT_B2G3R3_UNORM, 3, 3, 2, 0, 5, 2, 0, 0, 1, 0, 0, "uuu"},
113 {WINED3DFMT_A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 0, 1, 0, 0, "u"},
114 {WINED3DFMT_B2G3R3A8_UNORM, 3, 3, 2, 8, 5, 2, 0, 8, 2, 0, 0, "uuuu"},
115 {WINED3DFMT_B4G4R4X4_UNORM, 4, 4, 4, 0, 8, 4, 0, 0, 2, 0, 0, "uuuX"},
116 {WINED3DFMT_R8G8B8X8_UNORM, 8, 8, 8, 0, 0, 8, 16, 0, 4, 0, 0, "uuuX"},
117 {WINED3DFMT_B10G10R10A2_UNORM, 10, 10, 10, 2, 20, 10, 0, 30, 4, 0, 0, "uuuu"},
118 /* Luminance */
119 {WINED3DFMT_L8_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
120 {WINED3DFMT_L8A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 8, 2, 0, 0},
121 {WINED3DFMT_L4A4_UNORM, 0, 0, 0, 4, 0, 0, 0, 4, 1, 0, 0},
122 {WINED3DFMT_L16_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
123 /* Bump mapping stuff */
124 {WINED3DFMT_R5G5_SNORM_L6_UNORM, 5, 5, 0, 0, 0, 5, 0, 0, 2, 0, 0},
125 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, 8, 8, 0, 0, 0, 8, 0, 0, 4, 0, 0},
126 {WINED3DFMT_R10G11B11_SNORM, 10, 11, 11, 0, 0, 10, 21, 0, 4, 0, 0, "iii"},
127 {WINED3DFMT_R10G10B10_SNORM_A2_UNORM, 10, 10, 10, 2, 0, 10, 20, 30, 4, 0, 0, "iiiu"},
128 /* Depth stencil formats */
129 {WINED3DFMT_D16_LOCKABLE, 0, 0, 0, 0, 0, 0, 0, 0, 2, 16, 0, "D"},
130 {WINED3DFMT_D32_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 32, 0, "D"},
131 {WINED3DFMT_S1_UINT_D15_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 2, 15, 1, "SD"},
132 {WINED3DFMT_X8D24_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 0, "XD"},
133 {WINED3DFMT_S4X4_UINT_D24_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 4, "SXD"},
134 {WINED3DFMT_S8_UINT_D24_FLOAT, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 8, "SD"},
135 /* Vendor-specific formats */
136 {WINED3DFMT_ATI1N, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
137 {WINED3DFMT_ATI2N, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
138 {WINED3DFMT_NVDB, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
139 {WINED3DFMT_ATOC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
140 {WINED3DFMT_INST, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
141 {WINED3DFMT_INTZ, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 8},
142 {WINED3DFMT_RESZ, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
143 {WINED3DFMT_NVHU, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
144 {WINED3DFMT_NVHS, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
145 {WINED3DFMT_NULL, 8, 8, 8, 8, 0, 8, 16, 24, 4, 0, 0},
146 /* Unsure about them, could not find a Windows driver that supports them */
147 {WINED3DFMT_R16, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
148 {WINED3DFMT_AL16, 0, 0, 0, 16, 0, 0, 0, 16, 4, 0, 0},
149 /* DirectX 10 HDR formats */
150 {WINED3DFMT_R9G9B9E5_SHAREDEXP, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
151 /* Typeless */
152 {WINED3DFMT_R32G32B32A32_TYPELESS, 32, 32, 32, 32, 0, 32, 64, 96, 16, 0, 0},
153 {WINED3DFMT_R32G32B32_TYPELESS, 32, 32, 32, 0, 0, 32, 64, 0, 12, 0, 0},
154 {WINED3DFMT_R16G16B16A16_TYPELESS, 16, 16, 16, 16, 0, 16, 32, 48, 8, 0, 0},
155 {WINED3DFMT_R32G32_TYPELESS, 32, 32, 0, 0, 0, 32, 0, 0, 8, 0, 0},
156 {WINED3DFMT_R32G8X24_TYPELESS, 32, 8, 0, 0, 0, 0, 0, 0, 8, 0, 0},
157 {WINED3DFMT_R10G10B10A2_TYPELESS, 10, 10, 10, 2, 0, 10, 20, 30, 4, 0, 0},
158 {WINED3DFMT_R10G10B10X2_TYPELESS, 10, 10, 10, 0, 0, 10, 20, 0, 4, 0, 0},
159 {WINED3DFMT_R8G8B8A8_TYPELESS, 8, 8, 8, 8, 0, 8, 16, 24, 4, 0, 0},
160 {WINED3DFMT_R16G16_TYPELESS, 16, 16, 0, 0, 0, 16, 0, 0, 4, 0, 0},
161 {WINED3DFMT_R32_TYPELESS, 32, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
162 {WINED3DFMT_R24G8_TYPELESS, 24, 8, 0, 0, 0, 0, 0, 0, 4, 0, 0},
163 {WINED3DFMT_R8G8_TYPELESS, 8, 8, 0, 0, 0, 8, 0, 0, 2, 0, 0},
164 {WINED3DFMT_R16_TYPELESS, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
165 {WINED3DFMT_R8_TYPELESS, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
166 {WINED3DFMT_BC1_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
167 {WINED3DFMT_BC2_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
168 {WINED3DFMT_BC3_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
169 {WINED3DFMT_BC4_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
170 {WINED3DFMT_BC5_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
171 {WINED3DFMT_BC6H_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
172 {WINED3DFMT_BC7_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
173 {WINED3DFMT_B8G8R8A8_TYPELESS, 8, 8, 8, 8, 16, 8, 0, 24, 4, 0, 0},
174 {WINED3DFMT_B8G8R8X8_TYPELESS, 8, 8, 8, 0, 16, 8, 0, 0, 4, 0, 0},
177 struct wined3d_typed_format_info
179 enum wined3d_format_id id;
180 enum wined3d_format_id typeless_id;
181 const char *channels;
185 * The last entry for a given typeless format defines its internal format.
187 * u - WINED3D_CHANNEL_TYPE_UNORM
188 * i - WINED3D_CHANNEL_TYPE_SNORM
189 * U - WINED3D_CHANNEL_TYPE_UINT
190 * I - WINED3D_CHANNEL_TYPE_SINT
191 * F - WINED3D_CHANNEL_TYPE_FLOAT
192 * D - WINED3D_CHANNEL_TYPE_DEPTH
193 * S - WINED3D_CHANNEL_TYPE_STENCIL
194 * X - WINED3D_CHANNEL_TYPE_UNUSED
196 static const struct wined3d_typed_format_info typed_formats[] =
198 {WINED3DFMT_R32G32B32A32_UINT, WINED3DFMT_R32G32B32A32_TYPELESS, "UUUU"},
199 {WINED3DFMT_R32G32B32A32_SINT, WINED3DFMT_R32G32B32A32_TYPELESS, "IIII"},
200 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3DFMT_R32G32B32A32_TYPELESS, "FFFF"},
201 {WINED3DFMT_R32G32B32_UINT, WINED3DFMT_R32G32B32_TYPELESS, "UUU"},
202 {WINED3DFMT_R32G32B32_SINT, WINED3DFMT_R32G32B32_TYPELESS, "III"},
203 {WINED3DFMT_R32G32B32_FLOAT, WINED3DFMT_R32G32B32_TYPELESS, "FFF"},
204 {WINED3DFMT_R16G16B16A16_UNORM, WINED3DFMT_R16G16B16A16_TYPELESS, "uuuu"},
205 {WINED3DFMT_R16G16B16A16_SNORM, WINED3DFMT_R16G16B16A16_TYPELESS, "iiii"},
206 {WINED3DFMT_R16G16B16A16_UINT, WINED3DFMT_R16G16B16A16_TYPELESS, "UUUU"},
207 {WINED3DFMT_R16G16B16A16_SINT, WINED3DFMT_R16G16B16A16_TYPELESS, "IIII"},
208 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3DFMT_R16G16B16A16_TYPELESS, "FFFF"},
209 {WINED3DFMT_R32G32_UINT, WINED3DFMT_R32G32_TYPELESS, "UU"},
210 {WINED3DFMT_R32G32_SINT, WINED3DFMT_R32G32_TYPELESS, "II"},
211 {WINED3DFMT_R32G32_FLOAT, WINED3DFMT_R32G32_TYPELESS, "FF"},
212 {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_R32G8X24_TYPELESS, "FXX"},
213 {WINED3DFMT_X32_TYPELESS_G8X24_UINT, WINED3DFMT_R32G8X24_TYPELESS, "XUX"},
214 {WINED3DFMT_D32_FLOAT_S8X24_UINT, WINED3DFMT_R32G8X24_TYPELESS, "DSX"},
215 {WINED3DFMT_R10G10B10A2_SNORM, WINED3DFMT_R10G10B10A2_TYPELESS, "iiii"},
216 {WINED3DFMT_R10G10B10A2_UINT, WINED3DFMT_R10G10B10A2_TYPELESS, "UUUU"},
217 {WINED3DFMT_R10G10B10A2_UNORM, WINED3DFMT_R10G10B10A2_TYPELESS, "uuuu"},
218 {WINED3DFMT_R10G10B10X2_SNORM, WINED3DFMT_R10G10B10X2_TYPELESS, "iiiX"},
219 {WINED3DFMT_R10G10B10X2_UINT, WINED3DFMT_R10G10B10X2_TYPELESS, "UUUX"},
220 {WINED3DFMT_R8G8B8A8_UINT, WINED3DFMT_R8G8B8A8_TYPELESS, "UUUU"},
221 {WINED3DFMT_R8G8B8A8_SINT, WINED3DFMT_R8G8B8A8_TYPELESS, "IIII"},
222 {WINED3DFMT_R8G8B8A8_SNORM, WINED3DFMT_R8G8B8A8_TYPELESS, "iiii"},
223 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, WINED3DFMT_R8G8B8A8_TYPELESS, "uuuu"},
224 {WINED3DFMT_R8G8B8A8_UNORM, WINED3DFMT_R8G8B8A8_TYPELESS, "uuuu"},
225 {WINED3DFMT_R16G16_UNORM, WINED3DFMT_R16G16_TYPELESS, "uu"},
226 {WINED3DFMT_R16G16_SNORM, WINED3DFMT_R16G16_TYPELESS, "ii"},
227 {WINED3DFMT_R16G16_UINT, WINED3DFMT_R16G16_TYPELESS, "UU"},
228 {WINED3DFMT_R16G16_SINT, WINED3DFMT_R16G16_TYPELESS, "II"},
229 {WINED3DFMT_R16G16_FLOAT, WINED3DFMT_R16G16_TYPELESS, "FF"},
230 {WINED3DFMT_D32_FLOAT, WINED3DFMT_R32_TYPELESS, "D"},
231 {WINED3DFMT_R32_FLOAT, WINED3DFMT_R32_TYPELESS, "F"},
232 {WINED3DFMT_R32_UINT, WINED3DFMT_R32_TYPELESS, "U"},
233 {WINED3DFMT_R32_SINT, WINED3DFMT_R32_TYPELESS, "I"},
234 {WINED3DFMT_R24_UNORM_X8_TYPELESS, WINED3DFMT_R24G8_TYPELESS, "uX"},
235 {WINED3DFMT_X24_TYPELESS_G8_UINT, WINED3DFMT_R24G8_TYPELESS, "XU"},
236 {WINED3DFMT_D24_UNORM_S8_UINT, WINED3DFMT_R24G8_TYPELESS, "DS"},
237 {WINED3DFMT_R8G8_SNORM, WINED3DFMT_R8G8_TYPELESS, "ii"},
238 {WINED3DFMT_R8G8_UNORM, WINED3DFMT_R8G8_TYPELESS, "uu"},
239 {WINED3DFMT_R8G8_UINT, WINED3DFMT_R8G8_TYPELESS, "UU"},
240 {WINED3DFMT_R8G8_SINT, WINED3DFMT_R8G8_TYPELESS, "II"},
241 {WINED3DFMT_D16_UNORM, WINED3DFMT_R16_TYPELESS, "D"},
242 {WINED3DFMT_R16_UNORM, WINED3DFMT_R16_TYPELESS, "u"},
243 {WINED3DFMT_R16_SNORM, WINED3DFMT_R16_TYPELESS, "i"},
244 {WINED3DFMT_R16_UINT, WINED3DFMT_R16_TYPELESS, "U"},
245 {WINED3DFMT_R16_SINT, WINED3DFMT_R16_TYPELESS, "I"},
246 {WINED3DFMT_R16_FLOAT, WINED3DFMT_R16_TYPELESS, "F"},
247 {WINED3DFMT_R8_UNORM, WINED3DFMT_R8_TYPELESS, "u"},
248 {WINED3DFMT_R8_SNORM, WINED3DFMT_R8_TYPELESS, "i"},
249 {WINED3DFMT_R8_UINT, WINED3DFMT_R8_TYPELESS, "U"},
250 {WINED3DFMT_R8_SINT, WINED3DFMT_R8_TYPELESS, "I"},
251 {WINED3DFMT_BC1_UNORM_SRGB, WINED3DFMT_BC1_TYPELESS, ""},
252 {WINED3DFMT_BC1_UNORM, WINED3DFMT_BC1_TYPELESS, ""},
253 {WINED3DFMT_BC2_UNORM_SRGB, WINED3DFMT_BC2_TYPELESS, ""},
254 {WINED3DFMT_BC2_UNORM, WINED3DFMT_BC2_TYPELESS, ""},
255 {WINED3DFMT_BC3_UNORM_SRGB, WINED3DFMT_BC3_TYPELESS, ""},
256 {WINED3DFMT_BC3_UNORM, WINED3DFMT_BC3_TYPELESS, ""},
257 {WINED3DFMT_BC4_UNORM, WINED3DFMT_BC4_TYPELESS, ""},
258 {WINED3DFMT_BC4_SNORM, WINED3DFMT_BC4_TYPELESS, ""},
259 {WINED3DFMT_BC5_UNORM, WINED3DFMT_BC5_TYPELESS, ""},
260 {WINED3DFMT_BC5_SNORM, WINED3DFMT_BC5_TYPELESS, ""},
261 {WINED3DFMT_BC6H_UF16, WINED3DFMT_BC6H_TYPELESS, ""},
262 {WINED3DFMT_BC6H_SF16, WINED3DFMT_BC6H_TYPELESS, ""},
263 {WINED3DFMT_BC7_UNORM_SRGB, WINED3DFMT_BC7_TYPELESS, ""},
264 {WINED3DFMT_BC7_UNORM, WINED3DFMT_BC7_TYPELESS, ""},
265 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, WINED3DFMT_B8G8R8A8_TYPELESS, "uuuu"},
266 {WINED3DFMT_B8G8R8A8_UNORM, WINED3DFMT_B8G8R8A8_TYPELESS, "uuuu"},
267 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, WINED3DFMT_B8G8R8X8_TYPELESS, "uuuX"},
268 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8X8_TYPELESS, "uuuX"},
271 struct wined3d_typeless_format_depth_stencil_info
273 enum wined3d_format_id typeless_id;
274 enum wined3d_format_id depth_stencil_id;
275 enum wined3d_format_id depth_view_id;
276 enum wined3d_format_id stencil_view_id;
277 BOOL separate_depth_view_format;
280 static const struct wined3d_typeless_format_depth_stencil_info typeless_depth_stencil_formats[] =
282 {WINED3DFMT_R32G8X24_TYPELESS, WINED3DFMT_D32_FLOAT_S8X24_UINT,
283 WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_X32_TYPELESS_G8X24_UINT, TRUE},
284 {WINED3DFMT_R24G8_TYPELESS, WINED3DFMT_D24_UNORM_S8_UINT,
285 WINED3DFMT_R24_UNORM_X8_TYPELESS, WINED3DFMT_X24_TYPELESS_G8_UINT, TRUE},
286 {WINED3DFMT_R32_TYPELESS, WINED3DFMT_D32_FLOAT, WINED3DFMT_R32_FLOAT},
287 {WINED3DFMT_R16_TYPELESS, WINED3DFMT_D16_UNORM, WINED3DFMT_R16_UNORM},
290 struct wined3d_format_ddi_info
292 enum wined3d_format_id id;
293 D3DDDIFORMAT ddi_format;
296 static const struct wined3d_format_ddi_info ddi_formats[] =
298 {WINED3DFMT_B8G8R8_UNORM, D3DDDIFMT_R8G8B8},
299 {WINED3DFMT_B8G8R8A8_UNORM, D3DDDIFMT_A8R8G8B8},
300 {WINED3DFMT_B8G8R8X8_UNORM, D3DDDIFMT_X8R8G8B8},
301 {WINED3DFMT_B5G6R5_UNORM, D3DDDIFMT_R5G6B5},
302 {WINED3DFMT_B5G5R5X1_UNORM, D3DDDIFMT_X1R5G5B5},
303 {WINED3DFMT_B5G5R5A1_UNORM, D3DDDIFMT_A1R5G5B5},
304 {WINED3DFMT_B4G4R4A4_UNORM, D3DDDIFMT_A4R4G4B4},
305 {WINED3DFMT_B4G4R4X4_UNORM, D3DDDIFMT_X4R4G4B4},
306 {WINED3DFMT_P8_UINT, D3DDDIFMT_P8},
309 struct wined3d_format_base_flags
311 enum wined3d_format_id id;
312 unsigned int attrs;
313 unsigned int caps;
316 /* The ATI2N format behaves like an uncompressed format in LockRect(), but
317 * still needs to use the correct block based calculation for e.g. the
318 * resource size. */
319 static const struct wined3d_format_base_flags format_base_flags[] =
321 {WINED3DFMT_ATI1N, WINED3D_FORMAT_ATTR_MAPPABLE | WINED3D_FORMAT_ATTR_BROKEN_PITCH},
322 {WINED3DFMT_ATI2N, WINED3D_FORMAT_ATTR_MAPPABLE | WINED3D_FORMAT_ATTR_BROKEN_PITCH},
323 {WINED3DFMT_D16_LOCKABLE, WINED3D_FORMAT_ATTR_MAPPABLE},
324 {WINED3DFMT_INTZ, WINED3D_FORMAT_ATTR_MAPPABLE},
325 {WINED3DFMT_D32_FLOAT, WINED3D_FORMAT_ATTR_FLOAT},
326 {WINED3DFMT_S8_UINT_D24_FLOAT, WINED3D_FORMAT_ATTR_FLOAT},
327 {WINED3DFMT_D32_FLOAT_S8X24_UINT, WINED3D_FORMAT_ATTR_FLOAT},
328 {WINED3DFMT_INST, WINED3D_FORMAT_ATTR_EXTENSION},
329 {WINED3DFMT_NULL, WINED3D_FORMAT_ATTR_EXTENSION},
330 {WINED3DFMT_NVDB, WINED3D_FORMAT_ATTR_EXTENSION},
331 {WINED3DFMT_ATOC, WINED3D_FORMAT_ATTR_EXTENSION},
332 {WINED3DFMT_RESZ, WINED3D_FORMAT_ATTR_EXTENSION},
333 {WINED3DFMT_R32G32B32A32_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
334 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
335 {WINED3DFMT_R32G32B32A32_UINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
336 {WINED3DFMT_R32G32B32A32_SINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
337 {WINED3DFMT_R16G16B16A16_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
338 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
339 {WINED3DFMT_R16G16B16A16_UNORM, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
340 {WINED3DFMT_R16G16B16A16_UINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
341 {WINED3DFMT_R16G16B16A16_SNORM, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
342 {WINED3DFMT_R16G16B16A16_SINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
343 {WINED3DFMT_R32G32_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
344 {WINED3DFMT_R32G32_FLOAT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
345 {WINED3DFMT_R32G32_UINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
346 {WINED3DFMT_R32G32_SINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
347 {WINED3DFMT_R32_TYPELESS, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
348 {WINED3DFMT_R32_FLOAT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
349 {WINED3DFMT_R32_UINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK,
350 WINED3D_FORMAT_CAP_INDEX_BUFFER},
351 {WINED3DFMT_R32_SINT, WINED3D_FORMAT_ATTR_CAST_TO_BLOCK},
352 {WINED3DFMT_R16_UINT, 0,
353 WINED3D_FORMAT_CAP_INDEX_BUFFER},
354 {WINED3DFMT_R8G8_SNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
355 {WINED3DFMT_R5G5_SNORM_L6_UNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
356 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
357 {WINED3DFMT_R8G8B8A8_SNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
358 {WINED3DFMT_R16G16_SNORM, WINED3D_FORMAT_ATTR_BUMPMAP},
361 static void rgb888_from_rgb565(WORD rgb565, BYTE *r, BYTE *g, BYTE *b)
363 BYTE c;
365 /* (2⁸ - 1) / (2⁵ - 1) ≈ 2⁸ / 2⁵ + 2⁸ / 2¹⁰
366 * (2⁸ - 1) / (2⁶ - 1) ≈ 2⁸ / 2⁶ + 2⁸ / 2¹² */
367 c = rgb565 >> 11;
368 *r = (c << 3) + (c >> 2);
369 c = (rgb565 >> 5) & 0x3f;
370 *g = (c << 2) + (c >> 4);
371 c = rgb565 & 0x1f;
372 *b = (c << 3) + (c >> 2);
375 static void build_dxtn_colour_table(WORD colour0, WORD colour1,
376 DWORD colour_table[4], enum wined3d_format_id format_id)
378 unsigned int i;
379 struct
381 BYTE r, g, b;
382 } c[4];
384 rgb888_from_rgb565(colour0, &c[0].r, &c[0].g, &c[0].b);
385 rgb888_from_rgb565(colour1, &c[1].r, &c[1].g, &c[1].b);
387 if (format_id == WINED3DFMT_BC1_UNORM && colour0 <= colour1)
389 c[2].r = (c[0].r + c[1].r) / 2;
390 c[2].g = (c[0].g + c[1].g) / 2;
391 c[2].b = (c[0].b + c[1].b) / 2;
393 c[3].r = 0;
394 c[3].g = 0;
395 c[3].b = 0;
397 else
399 for (i = 0; i < 2; ++i)
401 c[i + 2].r = (2 * c[i].r + c[1 - i].r) / 3;
402 c[i + 2].g = (2 * c[i].g + c[1 - i].g) / 3;
403 c[i + 2].b = (2 * c[i].b + c[1 - i].b) / 3;
407 for (i = 0; i < 4; ++i)
409 colour_table[i] = (c[i].r << 16) | (c[i].g << 8) | c[i].b;
413 static void build_bc3_alpha_table(BYTE alpha0, BYTE alpha1, BYTE alpha_table[8])
415 unsigned int i;
417 alpha_table[0] = alpha0;
418 alpha_table[1] = alpha1;
420 if (alpha0 > alpha1)
422 for (i = 0; i < 6; ++i)
424 alpha_table[2 + i] = ((6 - i) * alpha0 + (i + 1) * alpha1) / 7;
426 return;
428 else
430 for (i = 0; i < 4; ++i)
432 alpha_table[2 + i] = ((4 - i) * alpha0 + (i + 1) * alpha1) / 5;
434 alpha_table[6] = 0x00;
435 alpha_table[7] = 0xff;
439 static void decompress_dxtn_block(const BYTE *src, BYTE *dst, unsigned int width,
440 unsigned int height, unsigned int dst_row_pitch, enum wined3d_format_id format_id)
442 const UINT64 *s = (const UINT64 *)src;
443 BOOL bc1_alpha = FALSE;
444 DWORD colour_table[4];
445 BYTE alpha_table[8];
446 UINT64 alpha_bits;
447 DWORD colour_bits;
448 unsigned int x, y;
449 BYTE colour_idx;
450 DWORD *dst_row;
451 BYTE alpha;
453 if (format_id == WINED3DFMT_BC1_UNORM)
455 WORD colour0, colour1;
457 alpha_bits = 0;
459 colour0 = s[0] & 0xffff;
460 colour1 = (s[0] >> 16) & 0xffff;
461 colour_bits = (s[0] >> 32) & 0xffffffff;
462 build_dxtn_colour_table(colour0, colour1, colour_table, format_id);
463 if (colour0 <= colour1)
464 bc1_alpha = TRUE;
466 else
468 alpha_bits = s[0];
469 if (format_id == WINED3DFMT_BC3_UNORM)
471 build_bc3_alpha_table(alpha_bits & 0xff, (alpha_bits >> 8) & 0xff, alpha_table);
472 alpha_bits >>= 16;
475 colour_bits = (s[1] >> 32) & 0xffffffff;
476 build_dxtn_colour_table(s[1] & 0xffff, (s[1] >> 16) & 0xffff, colour_table, format_id);
479 for (y = 0; y < height; ++y)
481 dst_row = (DWORD *)&dst[y * dst_row_pitch];
482 for (x = 0; x < width; ++x)
484 colour_idx = (colour_bits >> (y * 8 + x * 2)) & 0x3;
485 switch (format_id)
487 case WINED3DFMT_BC1_UNORM:
488 alpha = bc1_alpha && colour_idx == 3 ? 0x00 : 0xff;
489 break;
491 case WINED3DFMT_BC2_UNORM:
492 alpha = (alpha_bits >> (y * 16 + x * 4)) & 0xf;
493 /* (2⁸ - 1) / (2⁴ - 1) ≈ 2⁸ / 2⁴ + 2⁸ / 2⁸ */
494 alpha |= alpha << 4;
495 break;
497 case WINED3DFMT_BC3_UNORM:
498 alpha = alpha_table[(alpha_bits >> (y * 12 + x * 3)) & 0x7];
499 break;
501 default:
502 alpha = 0xff;
503 break;
505 dst_row[x] = (alpha << 24) | colour_table[colour_idx];
510 static void decompress_dxtn(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
511 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
512 unsigned int width, unsigned int height, unsigned int depth, enum wined3d_format_id format_id)
514 unsigned int block_byte_count, block_w, block_h;
515 const BYTE *src_row, *src_slice = src;
516 BYTE *dst_row, *dst_slice = dst;
517 unsigned int x, y, z;
519 block_byte_count = format_id == WINED3DFMT_BC1_UNORM ? 8 : 16;
521 for (z = 0; z < depth; ++z)
523 src_row = src_slice;
524 dst_row = dst_slice;
525 for (y = 0; y < height; y += 4)
527 for (x = 0; x < width; x += 4)
529 block_w = min(width - x, 4);
530 block_h = min(height - y, 4);
531 decompress_dxtn_block(&src_row[x * (block_byte_count / 4)],
532 &dst_row[x * 4], block_w, block_h, dst_row_pitch, format_id);
534 src_row += src_row_pitch;
535 dst_row += dst_row_pitch * 4;
537 src_slice += src_slice_pitch;
538 dst_slice += dst_slice_pitch;
542 static void decompress_bc3(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
543 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
544 unsigned int width, unsigned int height, unsigned int depth)
546 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
547 dst_slice_pitch, width, height, depth, WINED3DFMT_BC3_UNORM);
550 static void decompress_bc2(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
551 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
552 unsigned int width, unsigned int height, unsigned int depth)
554 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
555 dst_slice_pitch, width, height, depth, WINED3DFMT_BC2_UNORM);
558 static void decompress_bc1(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
559 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
560 unsigned int width, unsigned int height, unsigned int depth)
562 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
563 dst_slice_pitch, width, height, depth, WINED3DFMT_BC1_UNORM);
566 static void build_rgtc_colour_table(uint8_t red0, uint8_t red1, uint8_t colour_table[8])
568 unsigned int i;
570 colour_table[0] = red0;
571 colour_table[1] = red1;
572 if (red0 <= red1)
574 for (i = 0; i < 4; ++i)
576 colour_table[i + 2] = ((8 - 2 * i) * red0 + (2 + 2 * i) * red1 + 5) / 10;
578 colour_table[6] = 0x00;
579 colour_table[7] = 0xff;
581 else
583 for (i = 0; i < 6; ++i)
585 colour_table[i + 2] = ((12 - 2 * i) * red0 + (2 + 2 * i) * red1 + 7) / 14;
590 static void decompress_rgtc_block(const uint8_t *src, uint8_t *dst,
591 unsigned int width, unsigned int height, unsigned int dst_row_pitch)
593 const uint64_t *s = (const uint64_t *)src;
594 uint8_t red0, red1, red_idx;
595 uint8_t colour_table[8];
596 unsigned int x, y;
597 uint32_t *dst_row;
598 uint64_t bits;
600 red0 = s[0] & 0xff;
601 red1 = (s[0] >> 8) & 0xff;
602 bits = s[0] >> 16;
603 build_rgtc_colour_table(red0, red1, colour_table);
605 for (y = 0; y < height; ++y)
607 dst_row = (uint32_t *)&dst[y * dst_row_pitch];
608 for (x = 0; x < width; ++x)
610 red_idx = (bits >> (y * 12 + x * 3)) & 0x7;
611 /* Decompressing to bgra32 is perhaps not ideal for RGTC formats.
612 * It's convenient though. */
613 dst_row[x] = 0xff000000 | (colour_table[red_idx] << 16);
618 static void decompress_bc4(const uint8_t *src, uint8_t *dst, unsigned int src_row_pitch,
619 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
620 unsigned int width, unsigned int height, unsigned int depth)
622 unsigned int block_w, block_h, x, y, z;
623 const uint8_t *src_row, *src_slice;
624 uint8_t *dst_row, *dst_slice;
626 for (z = 0; z < depth; ++z)
628 src_slice = &src[z * src_slice_pitch];
629 dst_slice = &dst[z * dst_slice_pitch];
630 for (y = 0; y < height; y += 4)
632 src_row = &src_slice[(y / 4) * src_row_pitch];
633 dst_row = &dst_slice[y * dst_row_pitch];
634 for (x = 0; x < width; x += 4)
636 block_w = min(width - x, 4);
637 block_h = min(height - y, 4);
638 decompress_rgtc_block(&src_row[(x / 4) * 8], &dst_row[x * 4], block_w, block_h, dst_row_pitch);
644 static const struct wined3d_format_decompress_info
646 enum wined3d_format_id id;
647 void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
648 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
649 unsigned int width, unsigned int height, unsigned int depth);
651 format_decompress_info[] =
653 {WINED3DFMT_DXT1, decompress_bc1},
654 {WINED3DFMT_DXT2, decompress_bc2},
655 {WINED3DFMT_DXT3, decompress_bc2},
656 {WINED3DFMT_DXT4, decompress_bc3},
657 {WINED3DFMT_DXT5, decompress_bc3},
658 {WINED3DFMT_BC1_UNORM, decompress_bc1},
659 {WINED3DFMT_BC2_UNORM, decompress_bc2},
660 {WINED3DFMT_BC3_UNORM, decompress_bc3},
661 {WINED3DFMT_BC4_UNORM, decompress_bc4},
664 struct wined3d_format_block_info
666 enum wined3d_format_id id;
667 UINT block_width;
668 UINT block_height;
669 UINT block_byte_count;
670 unsigned int attrs;
673 static const struct wined3d_format_block_info format_block_info[] =
675 {WINED3DFMT_DXT1, 4, 4, 8, WINED3D_FORMAT_ATTR_COMPRESSED},
676 {WINED3DFMT_DXT2, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
677 {WINED3DFMT_DXT3, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
678 {WINED3DFMT_DXT4, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
679 {WINED3DFMT_DXT5, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
680 {WINED3DFMT_BC1_UNORM, 4, 4, 8, WINED3D_FORMAT_ATTR_COMPRESSED},
681 {WINED3DFMT_BC2_UNORM, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
682 {WINED3DFMT_BC3_UNORM, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
683 {WINED3DFMT_BC4_UNORM, 4, 4, 8, WINED3D_FORMAT_ATTR_COMPRESSED},
684 {WINED3DFMT_BC4_SNORM, 4, 4, 8, WINED3D_FORMAT_ATTR_COMPRESSED},
685 {WINED3DFMT_BC5_UNORM, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
686 {WINED3DFMT_BC5_SNORM, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
687 {WINED3DFMT_BC6H_UF16, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
688 {WINED3DFMT_BC6H_SF16, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
689 {WINED3DFMT_BC7_UNORM, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED},
690 {WINED3DFMT_ATI1N, 4, 4, 8, WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
691 {WINED3DFMT_ATI2N, 4, 4, 16, WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
692 {WINED3DFMT_YUY2, 2, 1, 4, WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
693 {WINED3DFMT_UYVY, 2, 1, 4, WINED3D_FORMAT_ATTR_BLOCKS_NO_VERIFY},
694 {WINED3DFMT_R9G9B9E5_SHAREDEXP, 1, 1, 4},
697 struct wined3d_format_vertex_info
699 enum wined3d_format_id id;
700 enum wined3d_ffp_emit_idx emit_idx;
701 GLenum gl_vtx_type;
702 enum wined3d_gl_extension extension;
705 static const struct wined3d_format_vertex_info format_vertex_info[] =
707 {WINED3DFMT_R32_FLOAT, WINED3D_FFP_EMIT_FLOAT1, GL_FLOAT},
708 {WINED3DFMT_R32G32_FLOAT, WINED3D_FFP_EMIT_FLOAT2, GL_FLOAT},
709 {WINED3DFMT_R32G32B32_FLOAT, WINED3D_FFP_EMIT_FLOAT3, GL_FLOAT},
710 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_FFP_EMIT_FLOAT4, GL_FLOAT},
711 {WINED3DFMT_B8G8R8A8_UNORM, WINED3D_FFP_EMIT_D3DCOLOR, GL_UNSIGNED_BYTE},
712 {WINED3DFMT_R8G8B8A8_UINT, WINED3D_FFP_EMIT_UBYTE4, GL_UNSIGNED_BYTE},
713 {WINED3DFMT_R16G16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
714 {WINED3DFMT_R16G16_SINT, WINED3D_FFP_EMIT_SHORT2, GL_SHORT},
715 {WINED3DFMT_R16G16B16A16_SINT, WINED3D_FFP_EMIT_SHORT4, GL_SHORT},
716 {WINED3DFMT_R8G8B8A8_UNORM, WINED3D_FFP_EMIT_UBYTE4N, GL_UNSIGNED_BYTE},
717 {WINED3DFMT_R16G16_SNORM, WINED3D_FFP_EMIT_SHORT2N, GL_SHORT},
718 {WINED3DFMT_R16G16B16A16_SNORM, WINED3D_FFP_EMIT_SHORT4N, GL_SHORT},
719 {WINED3DFMT_R16G16_UNORM, WINED3D_FFP_EMIT_USHORT2N, GL_UNSIGNED_SHORT},
720 {WINED3DFMT_R16G16B16A16_UNORM, WINED3D_FFP_EMIT_USHORT4N, GL_UNSIGNED_SHORT},
721 {WINED3DFMT_R11G11B10_FLOAT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT_10F_11F_11F_REV,
722 ARB_VERTEX_TYPE_10F_11F_11F_REV},
723 {WINED3DFMT_R10G10B10X2_UINT, WINED3D_FFP_EMIT_UDEC3, GL_UNSIGNED_SHORT},
724 {WINED3DFMT_R10G10B10X2_SNORM, WINED3D_FFP_EMIT_DEC3N, GL_SHORT},
725 {WINED3DFMT_R10G10B10A2_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT_2_10_10_10_REV,
726 ARB_VERTEX_TYPE_2_10_10_10_REV},
727 /* Without ARB_half_float_vertex we convert these on upload. */
728 {WINED3DFMT_R16G16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_2, GL_FLOAT},
729 {WINED3DFMT_R16G16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_2, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
730 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_FLOAT},
731 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
732 {WINED3DFMT_R8G8B8A8_SNORM, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
733 {WINED3DFMT_R8G8B8A8_SINT, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
734 {WINED3DFMT_R8G8_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
735 {WINED3DFMT_R16G16B16A16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
736 {WINED3DFMT_R8_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
737 {WINED3DFMT_R8_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
738 {WINED3DFMT_R8_SINT, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
739 {WINED3DFMT_R8G8_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
740 {WINED3DFMT_R16_FLOAT, WINED3D_FFP_EMIT_INVALID, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
741 {WINED3DFMT_R16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
742 {WINED3DFMT_R16_SINT, WINED3D_FFP_EMIT_INVALID, GL_SHORT},
743 {WINED3DFMT_R32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
744 {WINED3DFMT_R32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
745 {WINED3DFMT_R32G32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
746 {WINED3DFMT_R32G32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
747 {WINED3DFMT_R32G32B32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
748 {WINED3DFMT_R32G32B32A32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
749 {WINED3DFMT_R32G32B32A32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
752 struct wined3d_format_texture_info
754 enum wined3d_format_id id;
755 GLint gl_internal;
756 GLint gl_srgb_internal;
757 GLint gl_rt_internal;
758 GLint gl_format;
759 GLint gl_type;
760 unsigned int conv_byte_count;
761 unsigned int caps;
762 enum wined3d_gl_extension extension;
763 void (*upload)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
764 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
765 unsigned int width, unsigned int height, unsigned int depth);
766 void (*download)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
767 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
768 unsigned int width, unsigned int height, unsigned int depth);
769 void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
770 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
771 unsigned int width, unsigned int height, unsigned int depth);
774 static void convert_l4a4_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
775 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
777 /* WINED3DFMT_L4A4_UNORM exists as an internal gl format, but for some reason there is not
778 * format+type combination to load it. Thus convert it to A8L8, then load it
779 * with A4L4 internal, but A8L8 format+type
781 unsigned int x, y, z;
782 const unsigned char *Source;
783 unsigned char *Dest;
785 for (z = 0; z < depth; z++)
787 for (y = 0; y < height; y++)
789 Source = src + z * src_slice_pitch + y * src_row_pitch;
790 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
791 for (x = 0; x < width; x++ )
793 unsigned char color = (*Source++);
794 /* A */ Dest[1] = (color & 0xf0u) << 0;
795 /* L */ Dest[0] = (color & 0x0fu) << 4;
796 Dest += 2;
802 static void convert_r5g5_snorm_l6_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
803 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
805 unsigned int x, y, z;
806 unsigned char r_in, g_in, l_in;
807 const unsigned short *texel_in;
808 unsigned short *texel_out;
810 /* Emulating signed 5 bit values with unsigned 5 bit values has some precision problems by design:
811 * E.g. the signed input value 0 becomes 16. GL normalizes it to 16 / 31 = 0.516. We convert it
812 * back to a signed value by subtracting 0.5 and multiplying by 2.0. The resulting value is
813 * ((16 / 31) - 0.5) * 2.0 = 0.032, which is quite different from the intended result 0.000. */
814 for (z = 0; z < depth; z++)
816 for (y = 0; y < height; y++)
818 texel_out = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
819 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
820 for (x = 0; x < width; x++ )
822 l_in = (*texel_in & 0xfc00u) >> 10;
823 g_in = (*texel_in & 0x03e0u) >> 5;
824 r_in = *texel_in & 0x001fu;
826 *texel_out = ((r_in + 16) << 11) | (l_in << 5) | (g_in + 16);
827 texel_out++;
828 texel_in++;
834 static void convert_r5g5_snorm_l6_unorm_ext(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
835 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
837 unsigned int x, y, z;
838 unsigned char *texel_out, r_out, g_out, r_in, g_in, l_in;
839 const unsigned short *texel_in;
841 for (z = 0; z < depth; z++)
843 for (y = 0; y < height; y++)
845 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
846 texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
847 for (x = 0; x < width; x++ )
849 l_in = (*texel_in & 0xfc00u) >> 10;
850 g_in = (*texel_in & 0x03e0u) >> 5;
851 r_in = *texel_in & 0x001fu;
853 r_out = r_in << 3;
854 if (!(r_in & 0x10)) /* r > 0 */
855 r_out |= r_in >> 1;
857 g_out = g_in << 3;
858 if (!(g_in & 0x10)) /* g > 0 */
859 g_out |= g_in >> 1;
861 texel_out[0] = r_out;
862 texel_out[1] = g_out;
863 texel_out[2] = l_in << 1 | l_in >> 5;
864 texel_out[3] = 0;
866 texel_out += 4;
867 texel_in++;
873 static void convert_r5g5_snorm_l6_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
874 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
876 unsigned int x, y, z;
877 unsigned char *texel_out, ds_out, dt_out, r_in, g_in, l_in;
878 const unsigned short *texel_in;
880 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
881 * fixed function and shaders without further conversion once the surface is
882 * loaded.
884 * The difference between this function and convert_r5g5_snorm_l6_unorm_ext
885 * is that convert_r5g5_snorm_l6_unorm_ext creates a 32 bit XRGB texture and
886 * this function creates a 24 bit DSDT_MAG texture. Trying to load a DSDT_MAG
887 * internal with a 32 bit DSDT_MAG_INTENSITY or DSDT_MAG_VIB format fails. */
888 for (z = 0; z < depth; z++)
890 for (y = 0; y < height; y++)
892 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
893 texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
894 for (x = 0; x < width; x++ )
896 l_in = (*texel_in & 0xfc00u) >> 10;
897 g_in = (*texel_in & 0x03e0u) >> 5;
898 r_in = *texel_in & 0x001fu;
900 ds_out = r_in << 3;
901 if (!(r_in & 0x10)) /* r > 0 */
902 ds_out |= r_in >> 1;
904 dt_out = g_in << 3;
905 if (!(g_in & 0x10)) /* g > 0 */
906 dt_out |= g_in >> 1;
908 texel_out[0] = ds_out;
909 texel_out[1] = dt_out;
910 texel_out[2] = l_in << 1 | l_in >> 5;
912 texel_out += 3;
913 texel_in++;
919 static void convert_r8g8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
920 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
922 unsigned int x, y, z;
923 const short *Source;
924 unsigned char *Dest;
926 for (z = 0; z < depth; z++)
928 for (y = 0; y < height; y++)
930 Source = (const short *)(src + z * src_slice_pitch + y * src_row_pitch);
931 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
932 for (x = 0; x < width; x++ )
934 const short color = (*Source++);
935 /* B */ Dest[0] = 0xff;
936 /* G */ Dest[1] = (color >> 8) + 128; /* V */
937 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
938 Dest += 3;
944 static void convert_r8g8_snorm_l8x8_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
945 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
947 unsigned int x, y, z;
948 const DWORD *Source;
949 unsigned char *Dest;
951 /* Doesn't work correctly with the fixed function pipeline, but can work in
952 * shaders if the shader is adjusted. (There's no use for this format in gl's
953 * standard fixed function pipeline anyway).
955 for (z = 0; z < depth; z++)
957 for (y = 0; y < height; y++)
959 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
960 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
961 for (x = 0; x < width; x++ )
963 LONG color = (*Source++);
964 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
965 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
966 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
967 Dest += 4;
973 static void convert_r8g8_snorm_l8x8_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
974 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
976 unsigned int x, y, z;
977 const DWORD *Source;
978 unsigned char *Dest;
980 /* This implementation works with the fixed function pipeline and shaders
981 * without further modification after converting the surface.
983 for (z = 0; z < depth; z++)
985 for (y = 0; y < height; y++)
987 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
988 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
989 for (x = 0; x < width; x++ )
991 LONG color = (*Source++);
992 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
993 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
994 /* U */ Dest[0] = (color & 0xff); /* U */
995 /* I */ Dest[3] = 255; /* X */
996 Dest += 4;
1002 static void convert_r8g8b8a8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1003 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1005 unsigned int x, y, z;
1006 const DWORD *Source;
1007 unsigned char *Dest;
1009 for (z = 0; z < depth; z++)
1011 for (y = 0; y < height; y++)
1013 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1014 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
1015 for (x = 0; x < width; x++ )
1017 LONG color = (*Source++);
1018 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1019 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1020 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1021 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1022 Dest += 4;
1028 static void convert_r16g16_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1029 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1031 unsigned int x, y, z;
1032 const DWORD *Source;
1033 unsigned short *Dest;
1035 for (z = 0; z < depth; z++)
1037 for (y = 0; y < height; y++)
1039 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1040 Dest = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1041 for (x = 0; x < width; x++ )
1043 const DWORD color = (*Source++);
1044 /* B */ Dest[0] = 0xffff;
1045 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1046 /* R */ Dest[2] = (color & 0xffff) + 32768; /* U */
1047 Dest += 3;
1053 static void convert_r16g16(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1054 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1056 unsigned int x, y, z;
1057 const WORD *Source;
1058 WORD *Dest;
1060 for (z = 0; z < depth; z++)
1062 for (y = 0; y < height; y++)
1064 Source = (const WORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1065 Dest = (WORD *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1066 for (x = 0; x < width; x++ )
1068 WORD green = (*Source++);
1069 WORD red = (*Source++);
1070 Dest[0] = green;
1071 Dest[1] = red;
1072 /* Strictly speaking not correct for R16G16F, but it doesn't matter because the
1073 * shader overwrites it anyway */
1074 Dest[2] = 0xffff;
1075 Dest += 3;
1081 static void convert_r32g32_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1082 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1084 unsigned int x, y, z;
1085 const float *Source;
1086 float *Dest;
1088 for (z = 0; z < depth; z++)
1090 for (y = 0; y < height; y++)
1092 Source = (const float *)(src + z * src_slice_pitch + y * src_row_pitch);
1093 Dest = (float *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1094 for (x = 0; x < width; x++ )
1096 float green = (*Source++);
1097 float red = (*Source++);
1098 Dest[0] = green;
1099 Dest[1] = red;
1100 Dest[2] = 1.0f;
1101 Dest += 3;
1107 static void convert_s8_uint_d24_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1108 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1110 unsigned int x, y, z;
1112 for (z = 0; z < depth; z++)
1114 for (y = 0; y < height; ++y)
1116 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1117 float *dest_f = (float *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1118 DWORD *dest_s = (DWORD *)dest_f;
1120 for (x = 0; x < width; ++x)
1122 dest_f[x * 2] = float_24_to_32((source[x] & 0xffffff00u) >> 8);
1123 dest_s[x * 2 + 1] = source[x] & 0xff;
1129 static void x8_d24_unorm_upload(const BYTE *src, BYTE *dst,
1130 unsigned int src_row_pitch, unsigned int src_slice_pitch,
1131 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
1132 unsigned int width, unsigned int height, unsigned int depth)
1134 unsigned int x, y, z;
1136 for (z = 0; z < depth; ++z)
1138 for (y = 0; y < height; ++y)
1140 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1141 DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1143 for (x = 0; x < width; ++x)
1145 dest[x] = source[x] << 8 | ((source[x] >> 16) & 0xff);
1151 static void x8_d24_unorm_download(const BYTE *src, BYTE *dst,
1152 unsigned int src_row_pitch, unsigned int src_slice_pitch,
1153 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
1154 unsigned int width, unsigned int height, unsigned int depth)
1156 unsigned int x, y, z;
1158 for (z = 0; z < depth; ++z)
1160 for (y = 0; y < height; ++y)
1162 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1163 DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1165 for (x = 0; x < width; ++x)
1167 dest[x] = source[x] >> 8;
1173 static BOOL color_in_range(const struct wined3d_color_key *color_key, DWORD color)
1175 /* FIXME: Is this really how color keys are supposed to work? I think it
1176 * makes more sense to compare the individual channels. */
1177 return color >= color_key->color_space_low_value
1178 && color <= color_key->color_space_high_value;
1181 static void convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1182 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1183 const struct wined3d_color_key *color_key)
1185 const WORD *src_row;
1186 unsigned int x, y;
1187 WORD *dst_row;
1189 for (y = 0; y < height; ++y)
1191 src_row = (WORD *)&src[src_pitch * y];
1192 dst_row = (WORD *)&dst[dst_pitch * y];
1193 for (x = 0; x < width; ++x)
1195 WORD src_color = src_row[x];
1196 if (!color_in_range(color_key, src_color))
1197 dst_row[x] = 0x8000u | ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1198 else
1199 dst_row[x] = ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1204 static void convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1205 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1206 const struct wined3d_color_key *color_key)
1208 const WORD *src_row;
1209 unsigned int x, y;
1210 WORD *dst_row;
1212 for (y = 0; y < height; ++y)
1214 src_row = (WORD *)&src[src_pitch * y];
1215 dst_row = (WORD *)&dst[dst_pitch * y];
1216 for (x = 0; x < width; ++x)
1218 WORD src_color = src_row[x];
1219 if (color_in_range(color_key, src_color))
1220 dst_row[x] = src_color & ~0x8000;
1221 else
1222 dst_row[x] = src_color | 0x8000;
1227 static void convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1228 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1229 const struct wined3d_color_key *color_key)
1231 const BYTE *src_row;
1232 unsigned int x, y;
1233 DWORD *dst_row;
1235 for (y = 0; y < height; ++y)
1237 src_row = &src[src_pitch * y];
1238 dst_row = (DWORD *)&dst[dst_pitch * y];
1239 for (x = 0; x < width; ++x)
1241 DWORD src_color = (src_row[x * 3 + 2] << 16) | (src_row[x * 3 + 1] << 8) | src_row[x * 3];
1242 if (!color_in_range(color_key, src_color))
1243 dst_row[x] = src_color | 0xff000000;
1248 static void convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1249 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1250 const struct wined3d_color_key *color_key)
1252 const DWORD *src_row;
1253 unsigned int x, y;
1254 DWORD *dst_row;
1256 for (y = 0; y < height; ++y)
1258 src_row = (DWORD *)&src[src_pitch * y];
1259 dst_row = (DWORD *)&dst[dst_pitch * y];
1260 for (x = 0; x < width; ++x)
1262 DWORD src_color = src_row[x];
1263 if (color_in_range(color_key, src_color))
1264 dst_row[x] = src_color & ~0xff000000;
1265 else
1266 dst_row[x] = src_color | 0xff000000;
1271 static void convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1272 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1273 const struct wined3d_color_key *color_key)
1275 const DWORD *src_row;
1276 unsigned int x, y;
1277 DWORD *dst_row;
1279 for (y = 0; y < height; ++y)
1281 src_row = (DWORD *)&src[src_pitch * y];
1282 dst_row = (DWORD *)&dst[dst_pitch * y];
1283 for (x = 0; x < width; ++x)
1285 DWORD src_color = src_row[x];
1286 if (color_in_range(color_key, src_color))
1287 src_color &= ~0xff000000;
1288 dst_row[x] = src_color;
1293 const struct wined3d_color_key_conversion * wined3d_format_get_color_key_conversion(
1294 const struct wined3d_texture *texture, BOOL need_alpha_ck)
1296 const struct wined3d_format *format = texture->resource.format;
1297 unsigned int i;
1299 static const struct
1301 enum wined3d_format_id src_format;
1302 struct wined3d_color_key_conversion conversion;
1304 color_key_info[] =
1306 {WINED3DFMT_B5G6R5_UNORM, {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key }},
1307 {WINED3DFMT_B5G5R5X1_UNORM, {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key }},
1308 {WINED3DFMT_B8G8R8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key }},
1309 {WINED3DFMT_B8G8R8X8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key }},
1310 {WINED3DFMT_B8G8R8A8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key }},
1313 if (need_alpha_ck && (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY))
1315 for (i = 0; i < ARRAY_SIZE(color_key_info); ++i)
1317 if (color_key_info[i].src_format == format->id)
1318 return &color_key_info[i].conversion;
1321 FIXME("Color-keying not supported with format %s.\n", debug_d3dformat(format->id));
1324 return NULL;
1327 /* We intentionally don't support WINED3DFMT_D32_UNORM. No hardware driver
1328 * supports it, and applications get confused when we do.
1330 * The following formats explicitly don't have WINED3D_FORMAT_CAP_TEXTURE set:
1332 * These are never supported on native.
1333 * WINED3DFMT_B8G8R8_UNORM
1334 * WINED3DFMT_B2G3R3_UNORM
1335 * WINED3DFMT_L4A4_UNORM
1336 * WINED3DFMT_S1_UINT_D15_UNORM
1337 * WINED3DFMT_S4X4_UINT_D24_UNORM
1339 * Only some Geforce/Voodoo3/G400 cards offer 8-bit textures in case of ddraw.
1340 * Since it is not widely available, don't offer it. Further no Windows driver
1341 * offers WINED3DFMT_P8_UINT_A8_NORM, so don't offer it either.
1342 * WINED3DFMT_P8_UINT
1343 * WINED3DFMT_P8_UINT_A8_UNORM
1345 * These formats seem to be similar to the HILO formats in
1346 * GL_NV_texture_shader. NVHU is said to be GL_UNSIGNED_HILO16,
1347 * NVHS GL_SIGNED_HILO16. Rumours say that D3D computes a 3rd channel
1348 * similarly to D3DFMT_CxV8U8 (So NVHS could be called D3DFMT_CxV16U16). ATI
1349 * refused to support formats which can easily be emulated with pixel shaders,
1350 * so applications have to deal with not having NVHS and NVHU.
1351 * WINED3DFMT_NVHU
1352 * WINED3DFMT_NVHS */
1353 static const struct wined3d_format_texture_info format_texture_info[] =
1355 /* format id gl_internal gl_srgb_internal gl_rt_internal
1356 gl_format gl_type conv_byte_count
1357 caps
1358 extension upload download */
1359 /* FourCC formats */
1360 /* GL_APPLE_ycbcr_422 claims that its '2YUV' format, which is supported via the UNSIGNED_SHORT_8_8_REV_APPLE type
1361 * is equivalent to 'UYVY' format on Windows, and the 'YUVS' via UNSIGNED_SHORT_8_8_APPLE equates to 'YUY2'. The
1362 * d3d9 test however shows that the opposite is true. Since the extension is from 2002, it predates the x86 based
1363 * Macs, so probably the endianness differs. This could be tested as soon as we have a Windows and MacOS on a big
1364 * endian machine
1366 {WINED3DFMT_UYVY, GL_RG8, GL_RG8, 0,
1367 GL_RG, GL_UNSIGNED_BYTE, 0,
1368 WINED3D_FORMAT_CAP_FILTERING,
1369 ARB_TEXTURE_RG, NULL},
1370 {WINED3DFMT_UYVY, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE8_ALPHA8, 0,
1371 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1372 WINED3D_FORMAT_CAP_FILTERING,
1373 WINED3D_GL_LEGACY_CONTEXT, NULL},
1374 {WINED3DFMT_UYVY, GL_RGB_RAW_422_APPLE, GL_RGB_RAW_422_APPLE, 0,
1375 GL_RGB_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE, 0,
1376 WINED3D_FORMAT_CAP_FILTERING,
1377 APPLE_RGB_422, NULL},
1378 {WINED3DFMT_UYVY, GL_RGB, GL_RGB, 0,
1379 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE, 0,
1380 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_FILTERING,
1381 APPLE_YCBCR_422, NULL},
1382 {WINED3DFMT_YUY2, GL_RG8, GL_RG8, 0,
1383 GL_RG, GL_UNSIGNED_BYTE, 0,
1384 WINED3D_FORMAT_CAP_FILTERING,
1385 ARB_TEXTURE_RG, NULL},
1386 {WINED3DFMT_YUY2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE8_ALPHA8, 0,
1387 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1388 WINED3D_FORMAT_CAP_FILTERING,
1389 WINED3D_GL_LEGACY_CONTEXT, NULL},
1390 {WINED3DFMT_YUY2, GL_RGB_RAW_422_APPLE, GL_RGB_RAW_422_APPLE, 0,
1391 GL_RGB_422_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, 0,
1392 WINED3D_FORMAT_CAP_FILTERING,
1393 APPLE_RGB_422, NULL},
1394 {WINED3DFMT_YUY2, GL_RGB, GL_RGB, 0,
1395 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, 0,
1396 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_FILTERING,
1397 APPLE_YCBCR_422, NULL},
1398 {WINED3DFMT_YV12, GL_R8, GL_R8, 0,
1399 GL_RED, GL_UNSIGNED_BYTE, 0,
1400 WINED3D_FORMAT_CAP_FILTERING,
1401 ARB_TEXTURE_RG, NULL},
1402 {WINED3DFMT_YV12, GL_ALPHA8, GL_ALPHA8, 0,
1403 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1404 WINED3D_FORMAT_CAP_FILTERING,
1405 WINED3D_GL_LEGACY_CONTEXT, NULL},
1406 {WINED3DFMT_NV12, GL_R8, GL_R8, 0,
1407 GL_RED, GL_UNSIGNED_BYTE, 0,
1408 WINED3D_FORMAT_CAP_FILTERING,
1409 ARB_TEXTURE_RG, NULL},
1410 {WINED3DFMT_NV12, GL_ALPHA8, GL_ALPHA8, 0,
1411 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1412 WINED3D_FORMAT_CAP_FILTERING,
1413 WINED3D_GL_LEGACY_CONTEXT, NULL},
1414 {WINED3DFMT_DXT1, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1415 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1416 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1417 | WINED3D_FORMAT_CAP_SRGB_READ,
1418 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1419 {WINED3DFMT_DXT2, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1420 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1421 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1422 | WINED3D_FORMAT_CAP_SRGB_READ,
1423 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1424 {WINED3DFMT_DXT3, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1425 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1426 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1427 | WINED3D_FORMAT_CAP_SRGB_READ,
1428 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1429 {WINED3DFMT_DXT4, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1430 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1431 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1432 | WINED3D_FORMAT_CAP_SRGB_READ,
1433 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1434 {WINED3DFMT_DXT5, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1435 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1436 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1437 | WINED3D_FORMAT_CAP_SRGB_READ,
1438 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1439 {WINED3DFMT_BC1_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1440 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1441 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1442 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1443 {WINED3DFMT_BC2_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1444 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1445 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1446 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1447 {WINED3DFMT_BC3_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1448 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1449 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1450 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1451 {WINED3DFMT_BC4_UNORM, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1, 0,
1452 GL_RED, GL_UNSIGNED_BYTE, 0,
1453 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1454 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1455 {WINED3DFMT_BC4_SNORM, GL_COMPRESSED_SIGNED_RED_RGTC1, GL_COMPRESSED_SIGNED_RED_RGTC1, 0,
1456 GL_RED, GL_UNSIGNED_BYTE, 0,
1457 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1458 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1459 {WINED3DFMT_BC5_UNORM, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1460 GL_RG, GL_UNSIGNED_BYTE, 0,
1461 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1462 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1463 {WINED3DFMT_BC5_SNORM, GL_COMPRESSED_SIGNED_RG_RGTC2, GL_COMPRESSED_SIGNED_RG_RGTC2, 0,
1464 GL_RG, GL_UNSIGNED_BYTE, 0,
1465 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1466 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1467 {WINED3DFMT_BC6H_UF16, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, 0,
1468 GL_RGB, GL_UNSIGNED_BYTE, 0,
1469 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1470 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1471 {WINED3DFMT_BC6H_SF16, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, 0,
1472 GL_RGB, GL_UNSIGNED_BYTE, 0,
1473 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1474 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1475 {WINED3DFMT_BC7_UNORM, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB, 0,
1476 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1477 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1478 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1479 /* IEEE formats */
1480 {WINED3DFMT_R32_FLOAT, GL_RGB32F_ARB, GL_RGB32F_ARB, 0,
1481 GL_RED, GL_FLOAT, 0,
1482 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1483 ARB_TEXTURE_FLOAT, NULL},
1484 {WINED3DFMT_R32_FLOAT, GL_R32F, GL_R32F, 0,
1485 GL_RED, GL_FLOAT, 0,
1486 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1487 ARB_TEXTURE_RG, NULL},
1488 {WINED3DFMT_R32G32_FLOAT, GL_RGB32F_ARB, GL_RGB32F_ARB, 0,
1489 GL_RGB, GL_FLOAT, 12,
1490 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1491 ARB_TEXTURE_FLOAT, convert_r32g32_float},
1492 {WINED3DFMT_R32G32_FLOAT, GL_RG32F, GL_RG32F, 0,
1493 GL_RG, GL_FLOAT, 0,
1494 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1495 ARB_TEXTURE_RG, NULL},
1496 {WINED3DFMT_R32G32B32_FLOAT, GL_RGB32F, GL_RGB32F, 0,
1497 GL_RGB, GL_FLOAT, 0,
1498 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1499 ARB_TEXTURE_FLOAT, NULL},
1500 {WINED3DFMT_R32G32B32A32_FLOAT, GL_RGBA32F_ARB, GL_RGBA32F_ARB, 0,
1501 GL_RGBA, GL_FLOAT, 0,
1502 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1503 ARB_TEXTURE_FLOAT, NULL},
1504 /* Float */
1505 {WINED3DFMT_R16_FLOAT, GL_RGB16F_ARB, GL_RGB16F_ARB, 0,
1506 GL_RED, GL_HALF_FLOAT_ARB, 0,
1507 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1508 ARB_TEXTURE_FLOAT, NULL},
1509 {WINED3DFMT_R16_FLOAT, GL_R16F, GL_R16F, 0,
1510 GL_RED, GL_HALF_FLOAT_ARB, 0,
1511 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1512 ARB_TEXTURE_RG, NULL},
1513 {WINED3DFMT_R16G16_FLOAT, GL_RGB16F_ARB, GL_RGB16F_ARB, 0,
1514 GL_RGB, GL_HALF_FLOAT_ARB, 6,
1515 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1516 ARB_TEXTURE_FLOAT, convert_r16g16},
1517 {WINED3DFMT_R16G16_FLOAT, GL_RG16F, GL_RG16F, 0,
1518 GL_RG, GL_HALF_FLOAT_ARB, 0,
1519 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1520 ARB_TEXTURE_RG, NULL},
1521 {WINED3DFMT_R16G16B16A16_FLOAT, GL_RGBA16F_ARB, GL_RGBA16F_ARB, 0,
1522 GL_RGBA, GL_HALF_FLOAT_ARB, 0,
1523 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_RENDERTARGET
1524 | WINED3D_FORMAT_CAP_VTF,
1525 ARB_TEXTURE_FLOAT, NULL},
1526 {WINED3DFMT_R11G11B10_FLOAT, GL_R11F_G11F_B10F, GL_R11F_G11F_B10F, 0,
1527 GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, 0,
1528 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_RENDERTARGET,
1529 EXT_PACKED_FLOAT},
1530 /* Palettized formats */
1531 {WINED3DFMT_P8_UINT, GL_R8, GL_R8, 0,
1532 GL_RED, GL_UNSIGNED_BYTE, 0,
1534 ARB_TEXTURE_RG, NULL},
1535 {WINED3DFMT_P8_UINT, GL_ALPHA8, GL_ALPHA8, 0,
1536 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1538 WINED3D_GL_LEGACY_CONTEXT, NULL},
1539 /* Standard ARGB formats */
1540 {WINED3DFMT_B8G8R8_UNORM, GL_RGB8, GL_RGB8, 0,
1541 GL_BGR, GL_UNSIGNED_BYTE, 0,
1542 WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING | WINED3D_FORMAT_CAP_RENDERTARGET,
1543 WINED3D_GL_EXT_NONE, NULL},
1544 {WINED3DFMT_B8G8R8A8_UNORM, GL_RGBA8, GL_SRGB8_ALPHA8_EXT, 0,
1545 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1546 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1547 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE
1548 | WINED3D_FORMAT_CAP_VTF,
1549 WINED3D_GL_EXT_NONE, NULL},
1550 {WINED3DFMT_B8G8R8X8_UNORM, GL_RGB8, GL_SRGB8_EXT, 0,
1551 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1552 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1553 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE,
1554 WINED3D_GL_EXT_NONE, NULL},
1555 {WINED3DFMT_B5G6R5_UNORM, GL_RGB5, GL_SRGB8_EXT, GL_RGB8,
1556 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0,
1557 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1558 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ,
1559 WINED3D_GL_EXT_NONE, NULL},
1560 {WINED3DFMT_B5G6R5_UNORM, GL_RGB565, GL_SRGB8_EXT, GL_RGB8,
1561 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0,
1562 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1563 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ,
1564 ARB_ES2_COMPATIBILITY, NULL},
1565 {WINED3DFMT_B5G5R5X1_UNORM, GL_RGB5, GL_RGB5, 0,
1566 GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0,
1567 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1568 | WINED3D_FORMAT_CAP_RENDERTARGET,
1569 WINED3D_GL_EXT_NONE, NULL},
1570 {WINED3DFMT_B5G5R5A1_UNORM, GL_RGB5_A1, GL_RGB5_A1, 0,
1571 GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0,
1572 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1573 WINED3D_GL_EXT_NONE, NULL},
1574 {WINED3DFMT_B4G4R4A4_UNORM, GL_RGBA4, GL_SRGB8_ALPHA8_EXT, 0,
1575 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 0,
1576 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1577 | WINED3D_FORMAT_CAP_SRGB_READ,
1578 WINED3D_GL_EXT_NONE, NULL},
1579 {WINED3DFMT_B2G3R3_UNORM, GL_R3_G3_B2, GL_R3_G3_B2, 0,
1580 GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 0,
1581 WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1582 WINED3D_GL_EXT_NONE, NULL},
1583 {WINED3DFMT_R8_UNORM, GL_R8, GL_R8, 0,
1584 GL_RED, GL_UNSIGNED_BYTE, 0,
1585 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1586 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_VTF,
1587 ARB_TEXTURE_RG, NULL},
1588 {WINED3DFMT_A8_UNORM, GL_R8, GL_R8, 0,
1589 GL_RED, GL_UNSIGNED_BYTE, 0,
1590 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1591 | WINED3D_FORMAT_CAP_RENDERTARGET,
1592 ARB_TEXTURE_RG, NULL},
1593 {WINED3DFMT_A8_UNORM, GL_ALPHA8, GL_ALPHA8, 0,
1594 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1595 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1596 | WINED3D_FORMAT_CAP_RENDERTARGET,
1597 WINED3D_GL_LEGACY_CONTEXT, NULL},
1598 {WINED3DFMT_B4G4R4X4_UNORM, GL_RGB4, GL_RGB4, 0,
1599 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 0,
1600 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1601 WINED3D_GL_EXT_NONE, NULL},
1602 {WINED3DFMT_R10G10B10A2_UINT, GL_RGB10_A2UI, GL_RGB10_A2UI, 0,
1603 GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1604 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1605 ARB_TEXTURE_RGB10_A2UI, NULL},
1606 {WINED3DFMT_R10G10B10A2_UNORM, GL_RGB10_A2, GL_RGB10_A2, 0,
1607 GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1608 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1609 | WINED3D_FORMAT_CAP_RENDERTARGET,
1610 WINED3D_GL_EXT_NONE, NULL},
1611 {WINED3DFMT_R8G8B8A8_UNORM, GL_RGBA8, GL_SRGB8_ALPHA8_EXT, 0,
1612 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1613 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1614 | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE
1615 | WINED3D_FORMAT_CAP_VTF,
1616 WINED3D_GL_EXT_NONE, NULL},
1617 {WINED3DFMT_R8G8B8A8_UINT, GL_RGBA8UI, GL_RGBA8UI, 0,
1618 GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1619 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1620 ARB_TEXTURE_RGB10_A2UI, NULL},
1621 {WINED3DFMT_R8G8B8A8_SINT, GL_RGBA8I, GL_RGBA8I, 0,
1622 GL_RGBA_INTEGER, GL_BYTE, 0,
1623 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1624 EXT_TEXTURE_INTEGER, NULL},
1625 {WINED3DFMT_R8G8B8X8_UNORM, GL_RGB8, GL_RGB8, 0,
1626 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1627 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1628 WINED3D_GL_EXT_NONE, NULL},
1629 {WINED3DFMT_R16G16_UNORM, GL_RGB16, GL_RGB16, GL_RGBA16,
1630 GL_RGB, GL_UNSIGNED_SHORT, 6,
1631 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1632 WINED3D_GL_EXT_NONE, convert_r16g16},
1633 {WINED3DFMT_R16G16_UNORM, GL_RG16, GL_RG16, 0,
1634 GL_RG, GL_UNSIGNED_SHORT, 0,
1635 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1636 | WINED3D_FORMAT_CAP_RENDERTARGET,
1637 ARB_TEXTURE_RG, NULL},
1638 {WINED3DFMT_B10G10R10A2_UNORM, GL_RGB10_A2, GL_RGB10_A2, 0,
1639 GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1640 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1641 | WINED3D_FORMAT_CAP_RENDERTARGET,
1642 WINED3D_GL_EXT_NONE, NULL},
1643 {WINED3DFMT_R16G16B16A16_UNORM, GL_RGBA16, GL_RGBA16, 0,
1644 GL_RGBA, GL_UNSIGNED_SHORT, 0,
1645 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1646 | WINED3D_FORMAT_CAP_RENDERTARGET,
1647 WINED3D_GL_EXT_NONE, NULL},
1648 {WINED3DFMT_R8G8_UNORM, GL_RG8, GL_RG8, 0,
1649 GL_RG, GL_UNSIGNED_BYTE, 0,
1650 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1651 | WINED3D_FORMAT_CAP_RENDERTARGET,
1652 ARB_TEXTURE_RG, NULL},
1653 {WINED3DFMT_R8G8_UINT, GL_RG8UI, GL_RG8UI, 0,
1654 GL_RG_INTEGER, GL_UNSIGNED_BYTE, 0,
1655 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1656 ARB_TEXTURE_RG, NULL},
1657 {WINED3DFMT_R8G8_SINT, GL_RG8I, GL_RG8I, 0,
1658 GL_RG_INTEGER, GL_BYTE, 0,
1659 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1660 ARB_TEXTURE_RG, NULL},
1661 {WINED3DFMT_R16G16B16A16_UINT, GL_RGBA16UI, GL_RGBA16UI, 0,
1662 GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 0,
1663 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1664 EXT_TEXTURE_INTEGER, NULL},
1665 {WINED3DFMT_R16G16B16A16_SINT, GL_RGBA16I, GL_RGBA16I, 0,
1666 GL_RGBA_INTEGER, GL_SHORT, 0,
1667 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1668 EXT_TEXTURE_INTEGER, NULL},
1669 {WINED3DFMT_R32G32_UINT, GL_RG32UI, GL_RG32UI, 0,
1670 GL_RG_INTEGER, GL_UNSIGNED_INT, 0,
1671 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1672 ARB_TEXTURE_RG, NULL},
1673 {WINED3DFMT_R32G32_SINT, GL_RG32I, GL_RG32I, 0,
1674 GL_RG_INTEGER, GL_INT, 0,
1675 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1676 ARB_TEXTURE_RG, NULL},
1677 {WINED3DFMT_R16G16_UINT, GL_RG16UI, GL_RG16UI, 0,
1678 GL_RG_INTEGER, GL_UNSIGNED_SHORT, 0,
1679 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1680 ARB_TEXTURE_RG, NULL},
1681 {WINED3DFMT_R16G16_SINT, GL_RG16I, GL_RG16I, 0,
1682 GL_RG_INTEGER, GL_SHORT, 0,
1683 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1684 ARB_TEXTURE_RG, NULL},
1685 {WINED3DFMT_R32_UINT, GL_R32UI, GL_R32UI, 0,
1686 GL_RED_INTEGER, GL_UNSIGNED_INT, 0,
1687 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1688 ARB_TEXTURE_RG, NULL},
1689 {WINED3DFMT_R32_SINT, GL_R32I, GL_R32I, 0,
1690 GL_RED_INTEGER, GL_INT, 0,
1691 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1692 ARB_TEXTURE_RG, NULL},
1693 {WINED3DFMT_R16_UNORM, GL_R16, GL_R16, 0,
1694 GL_RED, GL_UNSIGNED_SHORT, 0,
1695 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1696 | WINED3D_FORMAT_CAP_RENDERTARGET,
1697 ARB_TEXTURE_RG, NULL},
1698 {WINED3DFMT_R16_UINT, GL_R16UI, GL_R16UI, 0,
1699 GL_RED_INTEGER, GL_UNSIGNED_SHORT, 0,
1700 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1701 ARB_TEXTURE_RG, NULL},
1702 {WINED3DFMT_R16_SINT, GL_R16I, GL_R16I, 0,
1703 GL_RED_INTEGER, GL_SHORT, 0,
1704 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1705 ARB_TEXTURE_RG, NULL},
1706 {WINED3DFMT_R8_UINT, GL_R8UI, GL_R8UI, 0,
1707 GL_RED_INTEGER, GL_UNSIGNED_BYTE, 0,
1708 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1709 ARB_TEXTURE_RG, NULL},
1710 {WINED3DFMT_R8_SINT, GL_R8I, GL_R8I, 0,
1711 GL_RED_INTEGER, GL_BYTE, 0,
1712 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1713 ARB_TEXTURE_RG, NULL},
1714 /* Luminance */
1715 {WINED3DFMT_L8_UNORM, GL_LUMINANCE8, GL_SLUMINANCE8_EXT, 0,
1716 GL_LUMINANCE, GL_UNSIGNED_BYTE, 0,
1717 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1718 | WINED3D_FORMAT_CAP_SRGB_READ,
1719 WINED3D_GL_LEGACY_CONTEXT, NULL},
1720 {WINED3DFMT_L8_UNORM, GL_R8, GL_R8, 0,
1721 GL_RED, GL_UNSIGNED_BYTE, 0,
1722 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1723 | WINED3D_FORMAT_CAP_RENDERTARGET,
1724 ARB_TEXTURE_RG, NULL},
1725 {WINED3DFMT_L8A8_UNORM, GL_RG8, GL_RG8, 0,
1726 GL_RG, GL_UNSIGNED_BYTE, 0,
1727 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1728 ARB_TEXTURE_RG, NULL},
1729 {WINED3DFMT_L8A8_UNORM, GL_LUMINANCE8_ALPHA8, GL_SLUMINANCE8_ALPHA8_EXT, 0,
1730 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1731 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1732 | WINED3D_FORMAT_CAP_SRGB_READ,
1733 WINED3D_GL_LEGACY_CONTEXT, NULL},
1734 {WINED3DFMT_L4A4_UNORM, GL_RG8, GL_RG8, 0,
1735 GL_RG, GL_UNSIGNED_BYTE, 2,
1736 WINED3D_FORMAT_CAP_FILTERING,
1737 ARB_TEXTURE_RG, convert_l4a4_unorm},
1738 {WINED3DFMT_L4A4_UNORM, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE4_ALPHA4, 0,
1739 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 2,
1740 WINED3D_FORMAT_CAP_FILTERING,
1741 WINED3D_GL_LEGACY_CONTEXT, convert_l4a4_unorm},
1742 {WINED3DFMT_L16_UNORM, GL_R16, GL_R16, 0,
1743 GL_RED, GL_UNSIGNED_SHORT, 0,
1744 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1745 ARB_TEXTURE_RG, NULL},
1746 {WINED3DFMT_L16_UNORM, GL_LUMINANCE16, GL_LUMINANCE16, 0,
1747 GL_LUMINANCE, GL_UNSIGNED_SHORT, 0,
1748 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1749 WINED3D_GL_LEGACY_CONTEXT, NULL},
1750 /* Bump mapping stuff */
1751 {WINED3DFMT_R8G8_SNORM, GL_RGB8, GL_RGB8, 0,
1752 GL_BGR, GL_UNSIGNED_BYTE, 3,
1753 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1754 WINED3D_GL_EXT_NONE, convert_r8g8_snorm},
1755 {WINED3DFMT_R8G8_SNORM, GL_DSDT8_NV, GL_DSDT8_NV, 0,
1756 GL_DSDT_NV, GL_BYTE, 0,
1757 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1758 NV_TEXTURE_SHADER, NULL},
1759 {WINED3DFMT_R8G8_SNORM, GL_RG8_SNORM, GL_RG8_SNORM, 0,
1760 GL_RG, GL_BYTE, 0,
1761 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1762 | WINED3D_FORMAT_CAP_RENDERTARGET,
1763 EXT_TEXTURE_SNORM, NULL},
1764 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_RGB5, GL_RGB5, 0,
1765 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2,
1766 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1767 WINED3D_GL_EXT_NONE, convert_r5g5_snorm_l6_unorm},
1768 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_DSDT8_MAG8_NV, GL_DSDT8_MAG8_NV, 0,
1769 GL_DSDT_MAG_NV, GL_BYTE, 3,
1770 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1771 NV_TEXTURE_SHADER, convert_r5g5_snorm_l6_unorm_nv},
1772 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_RGB8_SNORM, GL_RGB8_SNORM, 0,
1773 GL_RGBA, GL_BYTE, 4,
1774 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1775 EXT_TEXTURE_SNORM, convert_r5g5_snorm_l6_unorm_ext},
1776 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, GL_RGB8, GL_RGB8, 0,
1777 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4,
1778 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1779 WINED3D_GL_EXT_NONE, convert_r8g8_snorm_l8x8_unorm},
1780 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, GL_DSDT8_MAG8_INTENSITY8_NV, GL_DSDT8_MAG8_INTENSITY8_NV, 0,
1781 GL_DSDT_MAG_VIB_NV, GL_UNSIGNED_INT_8_8_S8_S8_REV_NV, 4,
1782 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1783 NV_TEXTURE_SHADER, convert_r8g8_snorm_l8x8_unorm_nv},
1784 {WINED3DFMT_R8G8B8A8_SNORM, GL_RGBA8, GL_RGBA8, 0,
1785 GL_BGRA, GL_UNSIGNED_BYTE, 4,
1786 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1787 WINED3D_GL_EXT_NONE, convert_r8g8b8a8_snorm},
1788 {WINED3DFMT_R8G8B8A8_SNORM, GL_SIGNED_RGBA8_NV, GL_SIGNED_RGBA8_NV, 0,
1789 GL_RGBA, GL_BYTE, 0,
1790 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1791 NV_TEXTURE_SHADER, NULL},
1792 {WINED3DFMT_R8G8B8A8_SNORM, GL_RGBA8_SNORM, GL_RGBA8_SNORM, 0,
1793 GL_RGBA, GL_BYTE, 0,
1794 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1795 | WINED3D_FORMAT_CAP_RENDERTARGET,
1796 EXT_TEXTURE_SNORM, NULL},
1797 {WINED3DFMT_R16G16_SNORM, GL_RGB16, GL_RGB16, 0,
1798 GL_BGR, GL_UNSIGNED_SHORT, 6,
1799 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1800 WINED3D_GL_EXT_NONE, convert_r16g16_snorm},
1801 {WINED3DFMT_R16G16_SNORM, GL_SIGNED_HILO16_NV, GL_SIGNED_HILO16_NV, 0,
1802 GL_HILO_NV, GL_SHORT, 0,
1803 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1804 NV_TEXTURE_SHADER, NULL},
1805 {WINED3DFMT_R16G16_SNORM, GL_RG16_SNORM, GL_RG16_SNORM, 0,
1806 GL_RG, GL_SHORT, 0,
1807 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1808 | WINED3D_FORMAT_CAP_RENDERTARGET,
1809 EXT_TEXTURE_SNORM, NULL},
1810 {WINED3DFMT_R16G16B16A16_SNORM, GL_RGBA16_SNORM, GL_RGBA16_SNORM, 0,
1811 GL_RGBA, GL_SHORT, 0,
1812 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1813 | WINED3D_FORMAT_CAP_RENDERTARGET,
1814 EXT_TEXTURE_SNORM, NULL},
1815 {WINED3DFMT_R16_SNORM, GL_R16_SNORM, GL_R16_SNORM, 0,
1816 GL_RED, GL_SHORT, 0,
1817 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1818 | WINED3D_FORMAT_CAP_RENDERTARGET,
1819 EXT_TEXTURE_SNORM, NULL},
1820 {WINED3DFMT_R8_SNORM, GL_R8_SNORM, GL_R8_SNORM, 0,
1821 GL_RED, GL_BYTE, 0,
1822 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1823 | WINED3D_FORMAT_CAP_RENDERTARGET,
1824 EXT_TEXTURE_SNORM, NULL},
1825 /* Depth stencil formats */
1826 {WINED3DFMT_D16_LOCKABLE, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1827 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1828 WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1829 WINED3D_GL_EXT_NONE, NULL},
1830 {WINED3DFMT_D16_LOCKABLE, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT16, 0,
1831 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1832 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1833 ARB_DEPTH_TEXTURE, NULL},
1834 {WINED3DFMT_D24_UNORM_S8_UINT, GL_DEPTH_COMPONENT24_ARB, GL_DEPTH_COMPONENT24_ARB, 0,
1835 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0,
1836 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1837 | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1838 ARB_DEPTH_TEXTURE, NULL},
1839 {WINED3DFMT_D24_UNORM_S8_UINT, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8, 0,
1840 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0,
1841 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1842 | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1843 EXT_PACKED_DEPTH_STENCIL, NULL},
1844 {WINED3DFMT_X8D24_UNORM, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1845 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4,
1846 WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1847 WINED3D_GL_EXT_NONE, x8_d24_unorm_upload, x8_d24_unorm_download},
1848 {WINED3DFMT_X8D24_UNORM, GL_DEPTH_COMPONENT24_ARB, GL_DEPTH_COMPONENT24_ARB, 0,
1849 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4,
1850 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1851 | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1852 ARB_DEPTH_TEXTURE, x8_d24_unorm_upload, x8_d24_unorm_download},
1853 {WINED3DFMT_D16_UNORM, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1854 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1855 WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1856 WINED3D_GL_EXT_NONE, NULL},
1857 {WINED3DFMT_D16_UNORM, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT16, 0,
1858 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1859 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1860 | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1861 ARB_DEPTH_TEXTURE, NULL},
1862 {WINED3DFMT_D32_FLOAT, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT32F, 0,
1863 GL_DEPTH_COMPONENT, GL_FLOAT, 0,
1864 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1865 ARB_DEPTH_BUFFER_FLOAT, NULL},
1866 {WINED3DFMT_D32_FLOAT_S8X24_UINT, GL_DEPTH32F_STENCIL8, GL_DEPTH32F_STENCIL8, 0,
1867 GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 0,
1868 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1869 ARB_DEPTH_BUFFER_FLOAT, NULL},
1870 {WINED3DFMT_S8_UINT_D24_FLOAT, GL_DEPTH32F_STENCIL8, GL_DEPTH32F_STENCIL8, 0,
1871 GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 8,
1872 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_DEPTH_STENCIL | WINED3D_FORMAT_CAP_SHADOW,
1873 ARB_DEPTH_BUFFER_FLOAT, convert_s8_uint_d24_float},
1874 {WINED3DFMT_R32G32B32A32_UINT, GL_RGBA32UI, GL_RGBA32UI, 0,
1875 GL_RGBA_INTEGER, GL_UNSIGNED_INT, 0,
1876 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1877 EXT_TEXTURE_INTEGER, NULL},
1878 {WINED3DFMT_R32G32B32A32_SINT, GL_RGBA32I, GL_RGBA32I, 0,
1879 GL_RGBA_INTEGER, GL_INT, 0,
1880 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET,
1881 EXT_TEXTURE_INTEGER, NULL},
1882 /* Vendor-specific formats */
1883 {WINED3DFMT_ATI1N, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1, 0,
1884 GL_RED, GL_UNSIGNED_BYTE, 0,
1885 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1886 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1887 {WINED3DFMT_ATI2N, GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, 0,
1888 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1889 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1890 ATI_TEXTURE_COMPRESSION_3DC, NULL},
1891 {WINED3DFMT_ATI2N, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1892 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1893 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1894 EXT_TEXTURE_COMPRESSION_RGTC, NULL},
1895 {WINED3DFMT_ATI2N, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1896 GL_RG, GL_UNSIGNED_BYTE, 0,
1897 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1898 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1899 {WINED3DFMT_INTZ, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8, 0,
1900 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0,
1901 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING
1902 | WINED3D_FORMAT_CAP_DEPTH_STENCIL,
1903 EXT_PACKED_DEPTH_STENCIL, NULL},
1904 {WINED3DFMT_NULL, 0, 0, 0,
1905 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1906 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FBO_ATTACHABLE,
1907 ARB_FRAMEBUFFER_OBJECT, NULL},
1908 /* DirectX 10 HDR formats */
1909 {WINED3DFMT_R9G9B9E5_SHAREDEXP, GL_RGB9_E5_EXT, GL_RGB9_E5_EXT, 0,
1910 GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV_EXT, 0,
1911 WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING | WINED3D_FORMAT_CAP_FILTERING,
1912 EXT_TEXTURE_SHARED_EXPONENT, NULL},
1915 struct wined3d_format_srgb_info
1917 enum wined3d_format_id srgb_format_id;
1918 enum wined3d_format_id base_format_id;
1921 static const struct wined3d_format_srgb_info format_srgb_info[] =
1923 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, WINED3DFMT_R8G8B8A8_UNORM},
1924 {WINED3DFMT_BC1_UNORM_SRGB, WINED3DFMT_BC1_UNORM},
1925 {WINED3DFMT_BC2_UNORM_SRGB, WINED3DFMT_BC2_UNORM},
1926 {WINED3DFMT_BC3_UNORM_SRGB, WINED3DFMT_BC3_UNORM},
1927 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, WINED3DFMT_B8G8R8A8_UNORM},
1928 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, WINED3DFMT_B8G8R8X8_UNORM},
1929 {WINED3DFMT_BC7_UNORM_SRGB, WINED3DFMT_BC7_UNORM},
1932 static inline int get_format_idx(enum wined3d_format_id format_id)
1934 unsigned int i;
1936 if (format_id < WINED3D_FORMAT_FOURCC_BASE)
1937 return format_id;
1939 for (i = 0; i < ARRAY_SIZE(format_index_remap); ++i)
1941 if (format_index_remap[i].id == format_id)
1942 return format_index_remap[i].idx;
1945 return -1;
1948 static struct wined3d_format_gl *wined3d_format_gl_mutable(struct wined3d_format *format)
1950 return CONTAINING_RECORD(format, struct wined3d_format_gl, f);
1953 static struct wined3d_format_vk *wined3d_format_vk_mutable(struct wined3d_format *format)
1955 return CONTAINING_RECORD(format, struct wined3d_format_vk, f);
1958 static struct wined3d_format *get_format_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
1960 return (struct wined3d_format *)((BYTE *)adapter->formats + fmt_idx * adapter->format_size);
1963 static struct wined3d_format_gl *get_format_gl_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
1965 return wined3d_format_gl_mutable(get_format_by_idx(adapter, fmt_idx));
1968 static struct wined3d_format *get_format_internal(const struct wined3d_adapter *adapter,
1969 enum wined3d_format_id format_id)
1971 int fmt_idx;
1973 if ((fmt_idx = get_format_idx(format_id)) == -1)
1975 ERR("Format %s (%#x) not found.\n", debug_d3dformat(format_id), format_id);
1976 return NULL;
1979 return get_format_by_idx(adapter, fmt_idx);
1982 static struct wined3d_format_gl *get_format_gl_internal(const struct wined3d_adapter *adapter,
1983 enum wined3d_format_id format_id)
1985 struct wined3d_format *format;
1987 if ((format = get_format_internal(adapter, format_id)))
1988 return wined3d_format_gl_mutable(format);
1990 return NULL;
1993 static void copy_format(const struct wined3d_adapter *adapter,
1994 struct wined3d_format *dst_format, const struct wined3d_format *src_format)
1996 enum wined3d_format_id id = dst_format->id;
1997 memcpy(dst_format, src_format, adapter->format_size);
1998 dst_format->id = id;
2001 static void format_set_caps(struct wined3d_format *format, unsigned int caps)
2003 unsigned int i;
2005 for (i = 0; i < ARRAY_SIZE(format->caps); ++i)
2006 format->caps[i] |= caps;
2009 static void format_clear_caps(struct wined3d_format *format, unsigned int caps)
2011 unsigned int i;
2013 for (i = 0; i < ARRAY_SIZE(format->caps); ++i)
2014 format->caps[i] &= ~caps;
2017 static enum wined3d_channel_type map_channel_type(char t)
2019 switch (t)
2021 case 'u':
2022 return WINED3D_CHANNEL_TYPE_UNORM;
2023 case 'i':
2024 return WINED3D_CHANNEL_TYPE_SNORM;
2025 case 'U':
2026 return WINED3D_CHANNEL_TYPE_UINT;
2027 case 'I':
2028 return WINED3D_CHANNEL_TYPE_SINT;
2029 case 'F':
2030 return WINED3D_CHANNEL_TYPE_FLOAT;
2031 case 'D':
2032 return WINED3D_CHANNEL_TYPE_DEPTH;
2033 case 'S':
2034 return WINED3D_CHANNEL_TYPE_STENCIL;
2035 case 'X':
2036 return WINED3D_CHANNEL_TYPE_UNUSED;
2037 default:
2038 ERR("Invalid channel type '%c'.\n", t);
2039 return WINED3D_CHANNEL_TYPE_NONE;
2043 static void parse_channel_desc(struct wined3d_format *format, const char *channel_desc)
2045 unsigned int component_count = 0;
2046 unsigned int attrs = 0;
2047 unsigned int j;
2049 for (j = 0; j < strlen(channel_desc); ++j)
2051 enum wined3d_channel_type channel_type = map_channel_type(channel_desc[j]);
2053 if (channel_type == WINED3D_CHANNEL_TYPE_UNORM || channel_type == WINED3D_CHANNEL_TYPE_SNORM)
2054 attrs |= WINED3D_FORMAT_ATTR_NORMALISED;
2055 if (channel_type == WINED3D_CHANNEL_TYPE_UINT || channel_type == WINED3D_CHANNEL_TYPE_SINT)
2056 attrs |= WINED3D_FORMAT_ATTR_INTEGER;
2057 if (channel_type == WINED3D_CHANNEL_TYPE_FLOAT)
2058 attrs |= WINED3D_FORMAT_ATTR_FLOAT;
2059 if (channel_type != WINED3D_CHANNEL_TYPE_UNUSED)
2060 ++component_count;
2062 if (channel_type == WINED3D_CHANNEL_TYPE_DEPTH && !format->depth_size)
2064 format->depth_size = format->red_size;
2065 format->red_size = format->red_offset = 0;
2068 if (channel_type == WINED3D_CHANNEL_TYPE_STENCIL && !format->stencil_size)
2070 format->stencil_size = format->green_size;
2071 format->green_size = format->green_offset = 0;
2075 format->attrs |= attrs;
2076 format->component_count = component_count;
2079 static BOOL init_format_base_info(struct wined3d_adapter *adapter)
2081 struct wined3d_format *format;
2082 unsigned int i;
2084 for (i = 0; i < ARRAY_SIZE(formats); ++i)
2086 if (!(format = get_format_internal(adapter, formats[i].id)))
2087 return FALSE;
2089 format->id = formats[i].id;
2090 format->red_size = formats[i].red_size;
2091 format->green_size = formats[i].green_size;
2092 format->blue_size = formats[i].blue_size;
2093 format->alpha_size = formats[i].alpha_size;
2094 format->red_offset = formats[i].red_offset;
2095 format->green_offset = formats[i].green_offset;
2096 format->blue_offset = formats[i].blue_offset;
2097 format->alpha_offset = formats[i].alpha_offset;
2098 format->byte_count = formats[i].bpp;
2099 format->depth_size = formats[i].depth_size;
2100 format->stencil_size = formats[i].stencil_size;
2101 format->block_width = 1;
2102 format->block_height = 1;
2103 format->block_byte_count = formats[i].bpp;
2105 if (formats[i].channels)
2106 parse_channel_desc(format, formats[i].channels);
2109 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
2111 struct wined3d_format *typeless_format;
2113 if (!(format = get_format_internal(adapter, typed_formats[i].id)))
2114 return FALSE;
2116 if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
2117 return FALSE;
2119 format->id = typed_formats[i].id;
2120 format->red_size = typeless_format->red_size;
2121 format->green_size = typeless_format->green_size;
2122 format->blue_size = typeless_format->blue_size;
2123 format->alpha_size = typeless_format->alpha_size;
2124 format->red_offset = typeless_format->red_offset;
2125 format->green_offset = typeless_format->green_offset;
2126 format->blue_offset = typeless_format->blue_offset;
2127 format->alpha_offset = typeless_format->alpha_offset;
2128 format->byte_count = typeless_format->byte_count;
2129 format->depth_size = typeless_format->depth_size;
2130 format->stencil_size = typeless_format->stencil_size;
2131 format->block_width = typeless_format->block_width;
2132 format->block_height = typeless_format->block_height;
2133 format->block_byte_count = typeless_format->block_byte_count;
2134 format->typeless_id = typeless_format->id;
2136 typeless_format->typeless_id = typeless_format->id;
2138 parse_channel_desc(format, typed_formats[i].channels);
2141 for (i = 0; i < ARRAY_SIZE(ddi_formats); ++i)
2143 if (!(format = get_format_internal(adapter, ddi_formats[i].id)))
2144 return FALSE;
2146 format->ddi_format = ddi_formats[i].ddi_format;
2149 for (i = 0; i < ARRAY_SIZE(format_base_flags); ++i)
2151 if (!(format = get_format_internal(adapter, format_base_flags[i].id)))
2152 return FALSE;
2154 format->attrs |= format_base_flags[i].attrs;
2155 format_set_caps(format, format_base_flags[i].caps);
2158 return TRUE;
2161 static BOOL init_format_block_info(struct wined3d_adapter *adapter)
2163 struct wined3d_format *format;
2164 unsigned int i;
2166 for (i = 0; i < ARRAY_SIZE(format_block_info); ++i)
2168 if (!(format = get_format_internal(adapter, format_block_info[i].id)))
2169 return FALSE;
2171 format->block_width = format_block_info[i].block_width;
2172 format->block_height = format_block_info[i].block_height;
2173 format->block_byte_count = format_block_info[i].block_byte_count;
2174 format->attrs |= WINED3D_FORMAT_ATTR_BLOCKS | format_block_info[i].attrs;
2177 return TRUE;
2180 /* Most compressed formats are not supported for 3D textures by OpenGL.
2182 * In the case of the S3TC/DXTn formats, NV_texture_compression_vtc provides
2183 * these formats for 3D textures, but unfortunately the block layout is
2184 * different from the one used by Direct3D.
2186 * Since applications either don't check format availability at all before
2187 * using these, or refuse to run without them, we decompress them on upload.
2189 * Affected applications include "Heroes VI", "From Dust", "Halo Online" and
2190 * "Eldorado". */
2191 static BOOL init_format_decompress_info(struct wined3d_adapter *adapter)
2193 struct wined3d_format *format;
2194 unsigned int i;
2196 for (i = 0; i < ARRAY_SIZE(format_decompress_info); ++i)
2198 if (!(format = get_format_internal(adapter, format_decompress_info[i].id)))
2199 return FALSE;
2201 format->caps[WINED3D_GL_RES_TYPE_TEX_3D] |= WINED3D_FORMAT_CAP_DECOMPRESS;
2202 format->decompress = format_decompress_info[i].decompress;
2205 return TRUE;
2208 static BOOL init_srgb_formats(struct wined3d_adapter *adapter)
2210 struct wined3d_format *format, *srgb_format;
2211 unsigned int i;
2213 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
2215 if (!(srgb_format = get_format_internal(adapter, format_srgb_info[i].srgb_format_id)))
2216 return FALSE;
2217 if (!(format = get_format_internal(adapter, format_srgb_info[i].base_format_id)))
2218 return FALSE;
2220 copy_format(adapter, srgb_format, format);
2223 return TRUE;
2226 static GLenum wined3d_gl_type_to_enum(enum wined3d_gl_resource_type type)
2228 switch (type)
2230 case WINED3D_GL_RES_TYPE_TEX_1D:
2231 return GL_TEXTURE_1D;
2232 case WINED3D_GL_RES_TYPE_TEX_2D:
2233 return GL_TEXTURE_2D;
2234 case WINED3D_GL_RES_TYPE_TEX_3D:
2235 return GL_TEXTURE_3D;
2236 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2237 return GL_TEXTURE_CUBE_MAP_ARB;
2238 case WINED3D_GL_RES_TYPE_BUFFER:
2239 return GL_TEXTURE_2D; /* TODO: GL_TEXTURE_BUFFER. */
2240 case WINED3D_GL_RES_TYPE_RB:
2241 return GL_RENDERBUFFER;
2242 case WINED3D_GL_RES_TYPE_COUNT:
2243 break;
2245 ERR("Unexpected GL resource type %u.\n", type);
2246 return 0;
2249 static void delete_fbo_attachment(const struct wined3d_gl_info *gl_info,
2250 enum wined3d_gl_resource_type d3d_type, GLuint object)
2252 switch (d3d_type)
2254 case WINED3D_GL_RES_TYPE_TEX_1D:
2255 case WINED3D_GL_RES_TYPE_TEX_2D:
2256 case WINED3D_GL_RES_TYPE_TEX_3D:
2257 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2258 gl_info->gl_ops.gl.p_glDeleteTextures(1, &object);
2259 break;
2261 case WINED3D_GL_RES_TYPE_RB:
2262 gl_info->fbo_ops.glDeleteRenderbuffers(1, &object);
2263 break;
2265 case WINED3D_GL_RES_TYPE_BUFFER:
2266 case WINED3D_GL_RES_TYPE_COUNT:
2267 break;
2271 static void create_and_bind_fbo_attachment(const struct wined3d_gl_info *gl_info,
2272 const struct wined3d_format_gl *format_gl,
2273 enum wined3d_gl_resource_type d3d_type, GLuint *object, GLenum internal)
2275 GLenum format = format_gl->format;
2276 GLenum type = format_gl->type;
2277 GLenum attach_type;
2279 attach_type = format_gl->f.depth_size ? GL_DEPTH_ATTACHMENT : GL_COLOR_ATTACHMENT0;
2280 switch (d3d_type)
2282 case WINED3D_GL_RES_TYPE_TEX_1D:
2283 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2284 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, *object);
2285 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, internal, 16, 0, format, type, NULL);
2286 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2287 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2289 gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_1D, *object, 0);
2290 if (format_gl->f.stencil_size)
2291 gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER,
2292 GL_STENCIL_ATTACHMENT, GL_TEXTURE_1D, *object, 0);
2293 break;
2295 case WINED3D_GL_RES_TYPE_TEX_2D:
2296 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2297 gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(d3d_type), *object);
2298 gl_info->gl_ops.gl.p_glTexImage2D(wined3d_gl_type_to_enum(d3d_type), 0, internal, 16, 16, 0,
2299 format, type, NULL);
2300 gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2301 gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2303 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2304 wined3d_gl_type_to_enum(d3d_type), *object, 0);
2305 if (format_gl->f.stencil_size)
2306 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
2307 wined3d_gl_type_to_enum(d3d_type), *object, 0);
2308 break;
2310 case WINED3D_GL_RES_TYPE_TEX_3D:
2311 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2312 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, *object);
2313 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, internal, 16, 16, 16, 0, format, type, NULL));
2314 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2315 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2317 gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_3D, *object, 0, 0);
2318 if (format_gl->f.stencil_size)
2319 gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER,
2320 GL_STENCIL_ATTACHMENT, GL_TEXTURE_3D, *object, 0, 0);
2321 break;
2323 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2324 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2325 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, *object);
2326 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, internal, 16, 16, 0,
2327 format, type, NULL);
2328 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, internal, 16, 16, 0,
2329 format, type, NULL);
2330 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, internal, 16, 16, 0,
2331 format, type, NULL);
2332 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, internal, 16, 16, 0,
2333 format, type, NULL);
2334 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, internal, 16, 16, 0,
2335 format, type, NULL);
2336 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, internal, 16, 16, 0,
2337 format, type, NULL);
2338 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2339 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2341 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2342 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2343 if (format_gl->f.stencil_size)
2344 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
2345 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2346 break;
2348 case WINED3D_GL_RES_TYPE_RB:
2349 gl_info->fbo_ops.glGenRenderbuffers(1, object);
2350 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, *object);
2351 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, internal, 16, 16);
2352 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, attach_type, GL_RENDERBUFFER, *object);
2353 if (format_gl->f.stencil_size)
2354 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
2355 GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *object);
2356 break;
2358 case WINED3D_GL_RES_TYPE_BUFFER:
2359 case WINED3D_GL_RES_TYPE_COUNT:
2360 break;
2363 /* Ideally we'd skip all formats already known not to work on textures
2364 * by checking for WINED3D_FORMAT_CAP_TEXTURE here. However, we want to
2365 * know if we can attach WINED3DFMT_P8_UINT textures to FBOs, and this
2366 * format never has WINED3D_FORMAT_CAP_TEXTURE set. Instead, swallow GL
2367 * errors generated by invalid formats. */
2368 while (gl_info->gl_ops.gl.p_glGetError());
2371 static void draw_test_quad(struct wined3d_caps_gl_ctx *ctx, const struct wined3d_vec3 *geometry,
2372 const struct wined3d_color *color)
2374 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2375 static const struct wined3d_vec3 default_geometry[] =
2377 {-1.0f, -1.0f, 0.0f},
2378 { 1.0f, -1.0f, 0.0f},
2379 {-1.0f, 1.0f, 0.0f},
2380 { 1.0f, 1.0f, 0.0f},
2382 static const char vs_core_header[] =
2383 "#version 150\n"
2384 "in vec4 pos;\n"
2385 "in vec4 color;\n"
2386 "out vec4 out_color;\n"
2387 "\n";
2388 static const char vs_legacy_header[] =
2389 "#version 120\n"
2390 "attribute vec4 pos;\n"
2391 "attribute vec4 color;\n"
2392 "varying vec4 out_color;\n"
2393 "\n";
2394 static const char vs_body[] =
2395 "void main()\n"
2396 "{\n"
2397 " gl_Position = pos;\n"
2398 " out_color = color;\n"
2399 "}\n";
2400 static const char fs_core[] =
2401 "#version 150\n"
2402 "in vec4 out_color;\n"
2403 "out vec4 fragment_color;\n"
2404 "\n"
2405 "void main()\n"
2406 "{\n"
2407 " fragment_color = out_color;\n"
2408 "}\n";
2409 static const char fs_legacy[] =
2410 "#version 120\n"
2411 "varying vec4 out_color;\n"
2412 "\n"
2413 "void main()\n"
2414 "{\n"
2415 " gl_FragData[0] = out_color;\n"
2416 "}\n";
2417 const char *source[2];
2418 GLuint vs_id, fs_id;
2419 unsigned int i;
2421 if (!geometry)
2422 geometry = default_geometry;
2424 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] || !gl_info->supported[ARB_VERTEX_SHADER]
2425 || !gl_info->supported[ARB_FRAGMENT_SHADER])
2427 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2428 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2429 gl_info->gl_ops.gl.p_glLoadIdentity();
2430 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2431 gl_info->gl_ops.gl.p_glLoadIdentity();
2433 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
2434 gl_info->gl_ops.gl.p_glColor4f(color->r, color->g, color->b, color->a);
2435 for (i = 0; i < 4; ++i)
2436 gl_info->gl_ops.gl.p_glVertex3fv(&geometry[i].x);
2437 gl_info->gl_ops.gl.p_glEnd();
2438 checkGLcall("draw quad");
2439 return;
2442 if (!ctx->test_vbo)
2443 GL_EXTCALL(glGenBuffers(1, &ctx->test_vbo));
2444 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, ctx->test_vbo));
2445 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(struct wined3d_vec3) * 4, geometry, GL_STREAM_DRAW));
2446 GL_EXTCALL(glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 0, NULL));
2447 GL_EXTCALL(glVertexAttrib4f(1, color->r, color->g, color->b, color->a));
2448 GL_EXTCALL(glEnableVertexAttribArray(0));
2449 GL_EXTCALL(glDisableVertexAttribArray(1));
2451 if (!ctx->test_program_id)
2453 BOOL use_glsl_150 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50);
2455 ctx->test_program_id = GL_EXTCALL(glCreateProgram());
2457 vs_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
2458 source[0] = use_glsl_150 ? vs_core_header : vs_legacy_header;
2459 source[1] = vs_body;
2460 GL_EXTCALL(glShaderSource(vs_id, 2, source, NULL));
2461 GL_EXTCALL(glAttachShader(ctx->test_program_id, vs_id));
2462 GL_EXTCALL(glDeleteShader(vs_id));
2464 fs_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
2465 source[0] = use_glsl_150 ? fs_core : fs_legacy;
2466 GL_EXTCALL(glShaderSource(fs_id, 1, source, NULL));
2467 GL_EXTCALL(glAttachShader(ctx->test_program_id, fs_id));
2468 GL_EXTCALL(glDeleteShader(fs_id));
2470 GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 0, "pos"));
2471 GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 1, "color"));
2473 if (use_glsl_150)
2474 GL_EXTCALL(glBindFragDataLocation(ctx->test_program_id, 0, "fragment_color"));
2476 GL_EXTCALL(glCompileShader(vs_id));
2477 print_glsl_info_log(gl_info, vs_id, FALSE);
2478 GL_EXTCALL(glCompileShader(fs_id));
2479 print_glsl_info_log(gl_info, fs_id, FALSE);
2480 GL_EXTCALL(glLinkProgram(ctx->test_program_id));
2481 shader_glsl_validate_link(gl_info, ctx->test_program_id);
2483 GL_EXTCALL(glUseProgram(ctx->test_program_id));
2485 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2487 GL_EXTCALL(glUseProgram(0));
2488 GL_EXTCALL(glDisableVertexAttribArray(0));
2489 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
2490 checkGLcall("draw quad");
2493 /* Context activation is done by the caller. */
2494 static void check_fbo_compat(struct wined3d_caps_gl_ctx *ctx, struct wined3d_format_gl *format)
2496 /* Check if the default internal format is supported as a frame buffer
2497 * target, otherwise fall back to the render target internal.
2499 * Try to stick to the standard format if possible, this limits precision differences. */
2500 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 1.0f};
2501 static const struct wined3d_color half_transparent_red = {1.0f, 0.0f, 0.0f, 0.5f};
2502 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2503 GLenum status, rt_internal = format->rt_internal;
2504 GLuint object, color_rb;
2505 enum wined3d_gl_resource_type type;
2506 BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2508 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2510 for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2512 const char *type_string = "color";
2514 if (type == WINED3D_GL_RES_TYPE_BUFFER)
2515 continue;
2517 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->internal);
2519 if (format->f.caps[type] & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
2521 gl_info->fbo_ops.glGenRenderbuffers(1, &color_rb);
2522 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, color_rb);
2523 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2524 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 1);
2525 else
2526 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 16);
2528 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
2529 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_rb);
2530 checkGLcall("Create and attach color rb attachment");
2531 type_string = "depth / stencil";
2534 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2535 checkGLcall("Framebuffer format check");
2537 if (status == GL_FRAMEBUFFER_COMPLETE)
2539 TRACE("Format %s is supported as FBO %s attachment, type %u.\n",
2540 debug_d3dformat(format->f.id), type_string, type);
2541 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE;
2542 format->rt_internal = format->internal;
2543 regular_fmt_used = TRUE;
2545 else
2547 if (!rt_internal)
2549 if (format->f.caps[type] & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL))
2551 WARN("Format %s with rendertarget flag is not supported as FBO color attachment (type %u),"
2552 " and no fallback specified.\n", debug_d3dformat(format->f.id), type);
2553 format->f.caps[type] &= ~(WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL);
2555 else
2557 TRACE("Format %s is not supported as FBO %s attachment, type %u.\n",
2558 debug_d3dformat(format->f.id), type_string, type);
2560 format->rt_internal = format->internal;
2562 else
2564 TRACE("Format %s is not supported as FBO %s attachment (type %u),"
2565 " trying rtInternal format as fallback.\n",
2566 debug_d3dformat(format->f.id), type_string, type);
2568 while (gl_info->gl_ops.gl.p_glGetError());
2570 delete_fbo_attachment(gl_info, type, object);
2571 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->rt_internal);
2573 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2574 checkGLcall("Framebuffer format check");
2576 if (status == GL_FRAMEBUFFER_COMPLETE)
2578 TRACE("Format %s rtInternal format is supported as FBO %s attachment, type %u.\n",
2579 debug_d3dformat(format->f.id), type_string, type);
2580 fallback_fmt_used = TRUE;
2582 else
2584 WARN("Format %s rtInternal format is not supported as FBO %s attachment, type %u.\n",
2585 debug_d3dformat(format->f.id), type_string, type);
2586 format->f.caps[type] &= ~(WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL);
2591 if (status == GL_FRAMEBUFFER_COMPLETE
2592 && ((format->f.caps[type] & WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING)
2593 || !(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
2594 && !(format->f.attrs & WINED3D_FORMAT_ATTR_INTEGER)
2595 && format->f.id != WINED3DFMT_NULL && format->f.id != WINED3DFMT_P8_UINT
2596 && format->format != GL_LUMINANCE && format->format != GL_LUMINANCE_ALPHA
2597 && (format->f.red_size || format->f.alpha_size))
2599 uint32_t readback[16 * 16 * 16], color = 0;
2600 unsigned int r_range, a_range;
2601 BYTE r, a;
2602 BOOL match = TRUE;
2603 GLuint rb;
2605 if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2607 gl_info->fbo_ops.glGenRenderbuffers(1, &rb);
2608 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, rb);
2609 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2610 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 1);
2611 else
2612 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 16);
2613 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
2614 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
2615 checkGLcall("RB attachment");
2618 gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2619 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2620 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
2621 if (gl_info->gl_ops.gl.p_glGetError() == GL_INVALID_FRAMEBUFFER_OPERATION)
2623 while (gl_info->gl_ops.gl.p_glGetError());
2624 TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2625 debug_d3dformat(format->f.id), type);
2626 format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2628 else
2630 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2631 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2632 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
2633 else
2634 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 16);
2635 gl_info->gl_ops.gl.p_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2637 draw_test_quad(ctx, NULL, &black);
2639 gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2641 draw_test_quad(ctx, NULL, &half_transparent_red);
2643 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2645 switch (type)
2647 case WINED3D_GL_RES_TYPE_TEX_1D:
2648 /* Rebinding texture to workaround a fglrx bug. */
2649 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, object);
2650 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_1D, 0, GL_BGRA,
2651 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2652 color = readback[7];
2653 break;
2655 case WINED3D_GL_RES_TYPE_TEX_2D:
2656 case WINED3D_GL_RES_TYPE_TEX_3D:
2657 /* Rebinding texture to workaround a fglrx bug. */
2658 gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(type), object);
2659 gl_info->gl_ops.gl.p_glGetTexImage(wined3d_gl_type_to_enum(type), 0, GL_BGRA,
2660 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2661 color = readback[7 * 16 + 7];
2662 break;
2664 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2665 /* Rebinding texture to workaround a fglrx bug. */
2666 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, object);
2667 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_BGRA,
2668 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2669 color = readback[7 * 16 + 7];
2670 break;
2672 case WINED3D_GL_RES_TYPE_RB:
2673 gl_info->gl_ops.gl.p_glReadPixels(0, 0, 16, 16,
2674 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2675 color = readback[7 * 16 + 7];
2676 break;
2678 case WINED3D_GL_RES_TYPE_BUFFER:
2679 case WINED3D_GL_RES_TYPE_COUNT:
2680 color = 0;
2681 break;
2683 checkGLcall("Post-pixelshader blending check");
2685 a = color >> 24;
2686 r = (color & 0x00ff0000u) >> 16;
2688 r_range = format->f.red_size < 8 ? 1u << (8 - format->f.red_size) : 1;
2689 a_range = format->f.alpha_size < 8 ? 1u << (8 - format->f.alpha_size) : 1;
2690 if (format->f.red_size && (r < 0x7f - r_range || r > 0x7f + r_range))
2691 match = FALSE;
2692 else if (format->f.alpha_size > 1 && (a < 0xbf - a_range || a > 0xbf + a_range))
2693 match = FALSE;
2694 if (!match)
2696 TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2697 debug_d3dformat(format->f.id), type);
2698 TRACE("Color output: %#x\n", color);
2699 format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2701 else
2703 TRACE("Format %s supports post-pixelshader blending, type %u.\n",
2704 debug_d3dformat(format->f.id), type);
2705 TRACE("Color output: %#x\n", color);
2706 format->f.caps[type] |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2710 if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2712 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
2713 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
2714 gl_info->fbo_ops.glDeleteRenderbuffers(1, &rb);
2715 checkGLcall("RB cleanup");
2719 if (format->internal != format->srgb_internal)
2721 delete_fbo_attachment(gl_info, type, object);
2722 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->srgb_internal);
2724 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2725 checkGLcall("Framebuffer format check");
2727 if (status == GL_FRAMEBUFFER_COMPLETE)
2729 TRACE("Format %s's sRGB format is FBO attachable, type %u.\n",
2730 debug_d3dformat(format->f.id), type);
2731 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2732 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2733 format->internal = format->srgb_internal;
2735 else
2737 WARN("Format %s's sRGB format is not FBO attachable, type %u.\n",
2738 debug_d3dformat(format->f.id), type);
2739 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
2742 else if (status == GL_FRAMEBUFFER_COMPLETE)
2743 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2745 if (format->f.caps[type] & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
2747 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
2748 gl_info->fbo_ops.glDeleteRenderbuffers(1, &color_rb);
2751 delete_fbo_attachment(gl_info, type, object);
2752 checkGLcall("Framebuffer format check cleanup");
2755 if (fallback_fmt_used && regular_fmt_used)
2757 FIXME("Format %s needs different render target formats for different resource types.\n",
2758 debug_d3dformat(format->f.id));
2759 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL
2760 | WINED3D_FORMAT_CAP_FBO_ATTACHABLE | WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB
2761 | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING);
2765 static void query_format_cap(struct wined3d_gl_info *gl_info, struct wined3d_format_gl *format,
2766 GLint internal, GLenum pname, unsigned int cap, const char *string)
2768 GLint value;
2769 enum wined3d_gl_resource_type type;
2771 for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2773 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type), internal, pname, 1, &value);
2774 if (value == GL_FULL_SUPPORT)
2776 TRACE("Format %s supports %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2777 format->f.caps[type] |= cap;
2779 else
2781 TRACE("Format %s doesn't support %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2782 format->f.caps[type] &= ~cap;
2787 /* Context activation is done by the caller. */
2788 static void init_format_fbo_compat_info(const struct wined3d_adapter *adapter,
2789 struct wined3d_caps_gl_ctx *ctx)
2791 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2792 unsigned int i, type;
2793 GLuint fbo;
2795 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
2797 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2799 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2800 BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2801 GLenum rt_internal = format->rt_internal;
2802 GLint value;
2804 if (!format->internal)
2805 continue;
2807 for (type = 0; type < ARRAY_SIZE(format->f.caps); ++type)
2809 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2810 format->internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2811 if (value == GL_FULL_SUPPORT)
2813 TRACE("Format %s is supported as FBO color attachment, resource type %u.\n",
2814 debug_d3dformat(format->f.id), type);
2815 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE;
2816 format->rt_internal = format->internal;
2817 regular_fmt_used = TRUE;
2819 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2820 format->internal, GL_FRAMEBUFFER_BLEND, 1, &value);
2821 if (value == GL_FULL_SUPPORT)
2823 TRACE("Format %s supports post-pixelshader blending, resource type %u.\n",
2824 debug_d3dformat(format->f.id), type);
2825 format->f.caps[type] |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2827 else
2829 TRACE("Format %s doesn't support post-pixelshader blending, resource typed %u.\n",
2830 debug_d3dformat(format->f.id), type);
2831 format->f.caps[type] &= ~WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
2834 else
2836 if (!rt_internal)
2838 if (format->f.caps[type] & WINED3D_FORMAT_CAP_RENDERTARGET)
2840 WARN("Format %s with rendertarget flag is not supported as FBO color attachment"
2841 " and no fallback specified, resource type %u.\n",
2842 debug_d3dformat(format->f.id), type);
2843 format->f.caps[type] &= ~WINED3D_FORMAT_CAP_RENDERTARGET;
2845 else
2846 TRACE("Format %s is not supported as FBO color attachment,"
2847 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2848 format->rt_internal = format->internal;
2850 else
2852 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2853 rt_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2854 if (value == GL_FULL_SUPPORT)
2856 TRACE("Format %s rtInternal format is supported as FBO color attachment,"
2857 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2858 fallback_fmt_used = TRUE;
2860 else
2862 WARN("Format %s rtInternal format is not supported as FBO color attachment,"
2863 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2864 format->f.caps[type] &= ~WINED3D_FORMAT_CAP_RENDERTARGET;
2869 if (format->internal != format->srgb_internal)
2871 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2872 format->srgb_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2873 if (value == GL_FULL_SUPPORT)
2875 TRACE("Format %s's sRGB format is FBO attachable, resource type %u.\n",
2876 debug_d3dformat(format->f.id), type);
2877 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2878 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2879 format->internal = format->srgb_internal;
2881 else
2883 WARN("Format %s's sRGB format is not FBO attachable, resource type %u.\n",
2884 debug_d3dformat(format->f.id), type);
2885 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
2888 else if (format->f.caps[type] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
2889 format->f.caps[type] |= WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB;
2892 if (fallback_fmt_used && regular_fmt_used)
2894 FIXME("Format %s needs different render target formats for different resource types.\n",
2895 debug_d3dformat(format->f.id));
2896 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FBO_ATTACHABLE
2897 | WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB | WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING);
2900 return;
2903 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
2904 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2905 gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);
2906 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
2908 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2910 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2912 if (!format->internal)
2913 continue;
2915 if (format->f.attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2917 TRACE("Skipping format %s because it's a compressed format.\n",
2918 debug_d3dformat(format->f.id));
2919 continue;
2922 TRACE("Checking if format %s is supported as FBO color attachment...\n", debug_d3dformat(format->f.id));
2923 check_fbo_compat(ctx, format);
2926 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
2929 static GLenum lookup_gl_view_class(GLenum internal_format)
2931 static const struct
2933 GLenum internal_format;
2934 GLenum view_class;
2936 view_classes[] =
2938 /* 128-bit */
2939 {GL_RGBA32F, GL_VIEW_CLASS_128_BITS},
2940 {GL_RGBA32UI, GL_VIEW_CLASS_128_BITS},
2941 {GL_RGBA32I, GL_VIEW_CLASS_128_BITS},
2942 /* 96-bit */
2943 {GL_RGB32F, GL_VIEW_CLASS_96_BITS},
2944 {GL_RGB32UI, GL_VIEW_CLASS_96_BITS},
2945 {GL_RGB32I, GL_VIEW_CLASS_96_BITS},
2946 /* 64-bit */
2947 {GL_RGBA16F, GL_VIEW_CLASS_64_BITS},
2948 {GL_RG32F, GL_VIEW_CLASS_64_BITS},
2949 {GL_RGBA16UI, GL_VIEW_CLASS_64_BITS},
2950 {GL_RG32UI, GL_VIEW_CLASS_64_BITS},
2951 {GL_RGBA16I, GL_VIEW_CLASS_64_BITS},
2952 {GL_RG32I, GL_VIEW_CLASS_64_BITS},
2953 {GL_RGBA16, GL_VIEW_CLASS_64_BITS},
2954 {GL_RGBA16_SNORM, GL_VIEW_CLASS_64_BITS},
2955 /* 48-bit */
2956 {GL_RGB16, GL_VIEW_CLASS_48_BITS},
2957 {GL_RGB16_SNORM, GL_VIEW_CLASS_48_BITS},
2958 {GL_RGB16F, GL_VIEW_CLASS_48_BITS},
2959 {GL_RGB16UI, GL_VIEW_CLASS_48_BITS},
2960 {GL_RGB16I, GL_VIEW_CLASS_48_BITS},
2961 /* 32-bit */
2962 {GL_RG16F, GL_VIEW_CLASS_32_BITS},
2963 {GL_R11F_G11F_B10F, GL_VIEW_CLASS_32_BITS},
2964 {GL_R32F, GL_VIEW_CLASS_32_BITS},
2965 {GL_RGB10_A2UI, GL_VIEW_CLASS_32_BITS},
2966 {GL_RGBA8UI, GL_VIEW_CLASS_32_BITS},
2967 {GL_RG16UI, GL_VIEW_CLASS_32_BITS},
2968 {GL_R32UI, GL_VIEW_CLASS_32_BITS},
2969 {GL_RGBA8I, GL_VIEW_CLASS_32_BITS},
2970 {GL_RG16I, GL_VIEW_CLASS_32_BITS},
2971 {GL_R32I, GL_VIEW_CLASS_32_BITS},
2972 {GL_RGB10_A2, GL_VIEW_CLASS_32_BITS},
2973 {GL_RGBA8, GL_VIEW_CLASS_32_BITS},
2974 {GL_RG16, GL_VIEW_CLASS_32_BITS},
2975 {GL_RGBA8_SNORM, GL_VIEW_CLASS_32_BITS},
2976 {GL_RG16_SNORM, GL_VIEW_CLASS_32_BITS},
2977 {GL_SRGB8_ALPHA8, GL_VIEW_CLASS_32_BITS},
2978 {GL_RGB9_E5, GL_VIEW_CLASS_32_BITS},
2979 /* 24-bit */
2980 {GL_RGB8, GL_VIEW_CLASS_24_BITS},
2981 {GL_RGB8_SNORM, GL_VIEW_CLASS_24_BITS},
2982 {GL_SRGB8, GL_VIEW_CLASS_24_BITS},
2983 {GL_RGB8UI, GL_VIEW_CLASS_24_BITS},
2984 {GL_RGB8I, GL_VIEW_CLASS_24_BITS},
2985 /* 16-bit */
2986 {GL_R16F, GL_VIEW_CLASS_16_BITS},
2987 {GL_RG8UI, GL_VIEW_CLASS_16_BITS},
2988 {GL_R16UI, GL_VIEW_CLASS_16_BITS},
2989 {GL_RG8I, GL_VIEW_CLASS_16_BITS},
2990 {GL_R16I, GL_VIEW_CLASS_16_BITS},
2991 {GL_RG8, GL_VIEW_CLASS_16_BITS},
2992 {GL_R16, GL_VIEW_CLASS_16_BITS},
2993 {GL_RG8_SNORM, GL_VIEW_CLASS_16_BITS},
2994 {GL_R16_SNORM, GL_VIEW_CLASS_16_BITS},
2995 /* 8-bit */
2996 {GL_R8UI, GL_VIEW_CLASS_8_BITS},
2997 {GL_R8I, GL_VIEW_CLASS_8_BITS},
2998 {GL_R8, GL_VIEW_CLASS_8_BITS},
2999 {GL_R8_SNORM, GL_VIEW_CLASS_8_BITS},
3001 /* RGTC1 */
3002 {GL_COMPRESSED_RED_RGTC1, GL_VIEW_CLASS_RGTC1_RED},
3003 {GL_COMPRESSED_SIGNED_RED_RGTC1, GL_VIEW_CLASS_RGTC1_RED},
3004 /* RGTC2 */
3005 {GL_COMPRESSED_RG_RGTC2, GL_VIEW_CLASS_RGTC2_RG},
3006 {GL_COMPRESSED_SIGNED_RG_RGTC2, GL_VIEW_CLASS_RGTC2_RG},
3008 /* BPTC unorm */
3009 {GL_COMPRESSED_RGBA_BPTC_UNORM, GL_VIEW_CLASS_BPTC_UNORM},
3010 {GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, GL_VIEW_CLASS_BPTC_UNORM},
3011 /* BPTC float */
3012 {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_VIEW_CLASS_BPTC_FLOAT},
3013 {GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, GL_VIEW_CLASS_BPTC_FLOAT},
3015 /* DXT1 RGB */
3016 {GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGB},
3017 {GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGB},
3018 /* DXT1 RGBA */
3019 {GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGBA},
3020 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGBA},
3021 /* DXT3 */
3022 {GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_VIEW_CLASS_S3TC_DXT3_RGBA},
3023 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_VIEW_CLASS_S3TC_DXT3_RGBA},
3024 /* DXT5 */
3025 {GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_VIEW_CLASS_S3TC_DXT5_RGBA},
3026 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_VIEW_CLASS_S3TC_DXT5_RGBA},
3029 unsigned int i;
3031 for (i = 0; i < ARRAY_SIZE(view_classes); ++i)
3033 if (view_classes[i].internal_format == internal_format)
3034 return view_classes[i].view_class;
3037 return GL_NONE;
3040 static void query_view_class(struct wined3d_format_gl *format)
3042 GLenum internal_view_class, gamma_view_class, rt_view_class;
3044 internal_view_class = lookup_gl_view_class(format->internal);
3045 gamma_view_class = lookup_gl_view_class(format->srgb_internal);
3046 rt_view_class = lookup_gl_view_class(format->rt_internal);
3048 if (internal_view_class == gamma_view_class || gamma_view_class == rt_view_class)
3050 format->view_class = internal_view_class;
3051 TRACE("Format %s is member of GL view class %#x.\n",
3052 debug_d3dformat(format->f.id), format->view_class);
3054 else
3056 format->view_class = GL_NONE;
3060 static void query_internal_format(struct wined3d_adapter *adapter,
3061 struct wined3d_format_gl *format, const struct wined3d_format_texture_info *texture_info,
3062 struct wined3d_gl_info *gl_info, BOOL srgb_write_supported, BOOL srgb_format)
3064 GLint count, multisample_types[8];
3065 unsigned int i, max_log2;
3066 GLenum target;
3068 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
3070 query_format_cap(gl_info, format, format->internal, GL_VERTEX_TEXTURE,
3071 WINED3D_FORMAT_CAP_VTF, "vertex texture usage");
3072 query_format_cap(gl_info, format, format->internal, GL_FILTER,
3073 WINED3D_FORMAT_CAP_FILTERING, "filtering");
3074 query_format_cap(gl_info, format, format->internal, GL_SHADER_IMAGE_STORE,
3075 WINED3D_FORMAT_CAP_UNORDERED_ACCESS, "unordered access");
3077 if (srgb_format || format->srgb_internal != format->internal)
3079 query_format_cap(gl_info, format, format->srgb_internal, GL_SRGB_READ,
3080 WINED3D_FORMAT_CAP_SRGB_READ, "sRGB read");
3082 if (srgb_write_supported)
3083 query_format_cap(gl_info, format, format->srgb_internal, GL_SRGB_WRITE,
3084 WINED3D_FORMAT_CAP_SRGB_WRITE, "sRGB write");
3085 else
3086 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
3088 if (!(format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D]
3089 & (WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE)))
3090 format->srgb_internal = format->internal;
3091 else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3092 format->internal = format->srgb_internal;
3095 else
3097 if (!gl_info->limits.samplers[WINED3D_SHADER_TYPE_VERTEX])
3098 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_VTF);
3100 if (!(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
3101 format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3102 else if (format->f.id != WINED3DFMT_R32G32B32A32_FLOAT && format->f.id != WINED3DFMT_R32_FLOAT)
3103 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_VTF);
3105 if (srgb_format || format->srgb_internal != format->internal)
3107 /* Filter sRGB capabilities if EXT_texture_sRGB is not supported. */
3108 if (!gl_info->supported[EXT_TEXTURE_SRGB])
3110 format->srgb_internal = format->internal;
3111 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3113 else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3115 format->internal = format->srgb_internal;
3119 if ((format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_SRGB_WRITE) && !srgb_write_supported)
3120 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_WRITE);
3123 if (!gl_info->supported[ARB_DEPTH_TEXTURE] && (format->f.depth_size || format->f.stencil_size))
3125 TRACE("Disabling texturing support for depth / stencil format %s.\n", debug_d3dformat(format->f.id));
3126 format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3127 format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3128 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3129 format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3132 query_view_class(format);
3134 if (format->internal && format->f.caps[WINED3D_GL_RES_TYPE_RB]
3135 & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_DEPTH_STENCIL)
3136 && (gl_info->supported[ARB_FRAMEBUFFER_OBJECT] || gl_info->supported[EXT_FRAMEBUFFER_MULTISAMPLE]))
3138 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY])
3140 target = gl_info->supported[ARB_TEXTURE_MULTISAMPLE] ? GL_TEXTURE_2D_MULTISAMPLE : GL_RENDERBUFFER;
3141 count = 0;
3142 GL_EXTCALL(glGetInternalformativ(target, format->internal,
3143 GL_NUM_SAMPLE_COUNTS, 1, &count));
3144 if (count > ARRAY_SIZE(multisample_types))
3145 FIXME("Unexpectedly high number of multisample types %d.\n", count);
3146 count = min(count, ARRAY_SIZE(multisample_types));
3147 GL_EXTCALL(glGetInternalformativ(target, format->internal,
3148 GL_SAMPLES, count, multisample_types));
3149 checkGLcall("query sample counts");
3150 for (i = 0; i < count; ++i)
3152 if (multisample_types[i] > sizeof(format->f.multisample_types) * CHAR_BIT)
3153 continue;
3154 format->f.multisample_types |= 1u << (multisample_types[i] - 1);
3157 else
3159 max_log2 = wined3d_log2i(min(gl_info->limits.samples,
3160 sizeof(format->f.multisample_types) * CHAR_BIT));
3161 for (i = 1; i <= max_log2; ++i)
3162 format->f.multisample_types |= 1u << ((1u << i) - 1);
3167 static BOOL init_format_texture_info(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
3169 struct wined3d_format_gl *format, *srgb_format;
3170 struct fragment_caps fragment_caps;
3171 struct shader_caps shader_caps;
3172 unsigned int i, j;
3173 BOOL srgb_write;
3175 adapter->fragment_pipe->get_caps(adapter, &fragment_caps);
3176 adapter->shader_backend->shader_get_caps(adapter, &shader_caps);
3177 srgb_write = (!shader_caps.ps_version || (shader_caps.wined3d_caps & WINED3D_SHADER_CAP_SRGB_WRITE));
3179 for (i = 0; i < ARRAY_SIZE(format_texture_info); ++i)
3181 if (!(format = get_format_gl_internal(adapter, format_texture_info[i].id)))
3182 return FALSE;
3184 if (!gl_info->supported[format_texture_info[i].extension])
3185 continue;
3187 /* ARB_texture_rg defines floating point formats, but only if
3188 * ARB_texture_float is also supported. */
3189 if (!gl_info->supported[ARB_TEXTURE_FLOAT]
3190 && (format->f.attrs & WINED3D_FORMAT_ATTR_FLOAT))
3191 continue;
3193 /* ARB_texture_rg defines integer formats if EXT_texture_integer is also supported. */
3194 if (!gl_info->supported[EXT_TEXTURE_INTEGER]
3195 && (format->f.attrs & WINED3D_FORMAT_ATTR_INTEGER))
3196 continue;
3198 format->internal = format_texture_info[i].gl_internal;
3199 format->srgb_internal = format_texture_info[i].gl_srgb_internal;
3200 format->rt_internal = format_texture_info[i].gl_rt_internal;
3201 format->format = format_texture_info[i].gl_format;
3202 format->type = format_texture_info[i].gl_type;
3203 format->f.color_fixup = COLOR_FIXUP_IDENTITY;
3204 format->f.height_scale.numerator = 1;
3205 format->f.height_scale.denominator = 1;
3207 format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3208 format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3209 format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3211 /* GL_ARB_depth_texture does not support 3D textures. It also says "cube textures are
3212 * problematic", but doesn't explicitly mandate that an error is generated. */
3213 if (gl_info->supported[EXT_TEXTURE3D] && !(format_texture_info[i].caps & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
3214 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3216 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3217 format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3219 format->f.caps[WINED3D_GL_RES_TYPE_RB] |= format_texture_info[i].caps | WINED3D_FORMAT_CAP_BLIT;
3220 format->f.caps[WINED3D_GL_RES_TYPE_RB] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3222 if (format->srgb_internal != format->internal
3223 && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
3225 format->srgb_internal = format->internal;
3226 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3229 if (!gl_info->supported[ARB_SHADOW] && (format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_SHADOW))
3230 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3232 query_internal_format(adapter, format, &format_texture_info[i], gl_info, srgb_write, FALSE);
3234 /* Texture conversion stuff */
3235 format->f.conv_byte_count = format_texture_info[i].conv_byte_count;
3236 format->f.upload = format_texture_info[i].upload;
3237 format->f.download = format_texture_info[i].download;
3239 srgb_format = NULL;
3240 for (j = 0; j < ARRAY_SIZE(format_srgb_info); ++j)
3242 if (format_srgb_info[j].base_format_id == format->f.id)
3244 if (!(srgb_format = get_format_gl_internal(adapter, format_srgb_info[j].srgb_format_id)))
3245 return FALSE;
3246 break;
3249 if (!srgb_format)
3250 continue;
3252 copy_format(adapter, &srgb_format->f, &format->f);
3254 if (gl_info->supported[EXT_TEXTURE_SRGB]
3255 && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
3257 srgb_format->internal = format_texture_info[i].gl_srgb_internal;
3258 srgb_format->srgb_internal = format_texture_info[i].gl_srgb_internal;
3259 format_set_caps(&srgb_format->f, WINED3D_FORMAT_CAP_SRGB_READ | WINED3D_FORMAT_CAP_SRGB_WRITE);
3260 query_internal_format(adapter, srgb_format, &format_texture_info[i], gl_info, srgb_write, TRUE);
3264 return TRUE;
3267 static BOOL compare_uint(unsigned int x, unsigned int y, unsigned int max_diff)
3269 unsigned int diff = x > y ? x - y : y - x;
3271 return diff <= max_diff;
3274 static BOOL compare_colour(DWORD c1, DWORD c2, BYTE max_diff)
3276 return compare_uint(c1 & 0xff, c2 & 0xff, max_diff)
3277 && compare_uint((c1 >> 8) & 0xff, (c2 >> 8) & 0xff, max_diff)
3278 && compare_uint((c1 >> 16) & 0xff, (c2 >> 16) & 0xff, max_diff)
3279 && compare_uint((c1 >> 24) & 0xff, (c2 >> 24) & 0xff, max_diff);
3282 /* A context is provided by the caller */
3283 static BOOL check_filter(const struct wined3d_gl_info *gl_info, GLenum internal)
3285 static const DWORD data[] = {0x00000000, 0xffffffff};
3286 GLuint tex, fbo, buffer;
3287 uint32_t readback[16 * 1];
3288 BOOL ret = FALSE;
3290 /* Render a filtered texture and see what happens. This is intended to detect the lack of
3291 * float16 filtering on ATI X1000 class cards. The drivers disable filtering instead of
3292 * falling back to software. If this changes in the future this code will get fooled and
3293 * apps might hit the software path due to incorrectly advertised caps.
3295 * Its unlikely that this changes however. GL Games like Mass Effect depend on the filter
3296 * disable fallback, if Apple or ATI ever change the driver behavior they will break more
3297 * than Wine. The Linux binary <= r500 driver is not maintained any more anyway
3300 while (gl_info->gl_ops.gl.p_glGetError());
3302 gl_info->gl_ops.gl.p_glGenTextures(1, &buffer);
3303 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3304 memset(readback, 0x7e, sizeof(readback));
3305 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 16, 1, 0,
3306 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, readback);
3307 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3308 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3309 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3310 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3311 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
3313 gl_info->gl_ops.gl.p_glGenTextures(1, &tex);
3314 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, tex);
3315 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, internal, 2, 1, 0,
3316 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
3317 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3318 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3319 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3320 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3321 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
3322 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
3324 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3325 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3326 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffer, 0);
3327 gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);
3329 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
3330 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3331 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3332 gl_info->gl_ops.gl.p_glLoadIdentity();
3333 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3334 gl_info->gl_ops.gl.p_glLoadIdentity();
3336 gl_info->gl_ops.gl.p_glClearColor(0, 1, 0, 0);
3337 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3339 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
3340 gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 0.0);
3341 gl_info->gl_ops.gl.p_glVertex2f(-1.0f, -1.0f);
3342 gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 0.0);
3343 gl_info->gl_ops.gl.p_glVertex2f(1.0f, -1.0f);
3344 gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 1.0);
3345 gl_info->gl_ops.gl.p_glVertex2f(-1.0f, 1.0f);
3346 gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 1.0);
3347 gl_info->gl_ops.gl.p_glVertex2f(1.0f, 1.0f);
3348 gl_info->gl_ops.gl.p_glEnd();
3350 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3351 memset(readback, 0x7f, sizeof(readback));
3352 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3353 if (compare_colour(readback[6], 0xffffffff, 5) || compare_colour(readback[6], 0x00000000, 5)
3354 || compare_colour(readback[9], 0xffffffff, 5) || compare_colour(readback[9], 0x00000000, 5))
3356 TRACE("Read back colors 0x%08x and 0x%08x close to unfiltered color, assuming no filtering\n",
3357 readback[6], readback[9]);
3358 ret = FALSE;
3360 else
3362 TRACE("Read back colors are 0x%08x and 0x%08x, assuming texture is filtered\n",
3363 readback[6], readback[9]);
3364 ret = TRUE;
3367 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3368 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3369 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex);
3370 gl_info->gl_ops.gl.p_glDeleteTextures(1, &buffer);
3371 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3373 if (gl_info->gl_ops.gl.p_glGetError())
3375 FIXME("Error during filtering test for format %x, returning no filtering\n", internal);
3376 ret = FALSE;
3379 return ret;
3382 static void init_format_filter_info(struct wined3d_adapter *adapter,
3383 struct wined3d_gl_info *gl_info)
3385 enum wined3d_pci_vendor vendor = adapter->driver_info.vendor;
3386 struct wined3d_format_gl *format;
3387 unsigned int i;
3388 static const enum wined3d_format_id fmts16[] =
3390 WINED3DFMT_R16_FLOAT,
3391 WINED3DFMT_R16G16_FLOAT,
3392 WINED3DFMT_R16G16B16A16_FLOAT,
3394 BOOL filtered;
3396 /* This was already handled by init_format_texture_info(). */
3397 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
3398 return;
3400 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3402 if (vendor == HW_VENDOR_NVIDIA && gl_info->supported[ARB_TEXTURE_FLOAT])
3404 TRACE("Nvidia card with texture_float support: Assuming float16 blending\n");
3405 filtered = TRUE;
3407 else if (gl_info->limits.glsl_varyings > 44)
3409 TRACE("More than 44 GLSL varyings - assuming d3d10 card with float16 blending\n");
3410 filtered = TRUE;
3412 else
3414 TRACE("Assuming no float16 blending\n");
3415 filtered = FALSE;
3418 if (filtered)
3420 for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3422 format = get_format_gl_internal(adapter, fmts16[i]);
3423 format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3426 return;
3429 for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3431 format = get_format_gl_internal(adapter, fmts16[i]);
3432 if (!format->internal)
3433 continue; /* Not supported by GL */
3435 filtered = check_filter(gl_info, format->internal);
3436 if (filtered)
3438 TRACE("Format %s supports filtering.\n", debug_d3dformat(format->f.id));
3439 format_set_caps(&format->f, WINED3D_FORMAT_CAP_FILTERING);
3441 else
3443 TRACE("Format %s does not support filtering.\n", debug_d3dformat(format->f.id));
3448 static enum fixup_channel_source fixup_source_from_char(char c)
3450 switch (c)
3452 default:
3453 case '0':
3454 return CHANNEL_SOURCE_ZERO;
3455 case '1':
3456 return CHANNEL_SOURCE_ONE;
3457 case 'x':
3458 case 'X':
3459 return CHANNEL_SOURCE_X;
3460 case 'y':
3461 case 'Y':
3462 return CHANNEL_SOURCE_Y;
3463 case 'z':
3464 case 'Z':
3465 return CHANNEL_SOURCE_Z;
3466 case 'w':
3467 case 'W':
3468 return CHANNEL_SOURCE_W;
3472 static unsigned int fixup_sign_from_char(char c)
3474 if (c == 'x' || c == 'y' || c == 'z' || c == 'w')
3475 return 1;
3476 return 0;
3479 static struct color_fixup_desc create_color_fixup_desc_from_string(const char *s)
3481 struct color_fixup_desc fixup;
3483 if (strlen(s) != 4)
3485 ERR("Invalid fixup string %s.\n", wine_dbgstr_a(s));
3486 return COLOR_FIXUP_IDENTITY;
3489 fixup.x_sign_fixup = fixup_sign_from_char(s[0]);
3490 fixup.x_source = fixup_source_from_char(s[0]);
3491 fixup.y_sign_fixup = fixup_sign_from_char(s[1]);
3492 fixup.y_source = fixup_source_from_char(s[1]);
3493 fixup.z_sign_fixup = fixup_sign_from_char(s[2]);
3494 fixup.z_source = fixup_source_from_char(s[2]);
3495 fixup.w_sign_fixup = fixup_sign_from_char(s[3]);
3496 fixup.w_source = fixup_source_from_char(s[3]);
3498 return fixup;
3501 static void apply_format_fixups(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
3503 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
3504 struct wined3d_format_gl *format;
3505 BOOL use_legacy_fixups;
3506 unsigned int i;
3508 static const struct
3510 enum wined3d_format_id id;
3511 const char *fixup;
3512 BOOL legacy;
3513 enum wined3d_gl_extension extension;
3515 fixups[] =
3517 {WINED3DFMT_R16_FLOAT, "X11W", TRUE, WINED3D_GL_EXT_NONE},
3518 {WINED3DFMT_R32_FLOAT, "X11W", TRUE, WINED3D_GL_EXT_NONE},
3519 {WINED3DFMT_R16G16_UNORM, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3520 {WINED3DFMT_R16G16_FLOAT, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3521 {WINED3DFMT_R32G32_FLOAT, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3523 {WINED3DFMT_R8G8_SNORM, "xy11", FALSE, WINED3D_GL_EXT_NONE},
3524 {WINED3DFMT_R8G8_SNORM, "XY11", TRUE, NV_TEXTURE_SHADER},
3525 {WINED3DFMT_R8G8_SNORM, "XY11", TRUE, EXT_TEXTURE_SNORM},
3527 {WINED3DFMT_R16G16_SNORM, "xy11", FALSE, WINED3D_GL_EXT_NONE},
3528 {WINED3DFMT_R16G16_SNORM, "XY11", TRUE, NV_TEXTURE_SHADER},
3529 {WINED3DFMT_R16G16_SNORM, "XY11", TRUE, EXT_TEXTURE_SNORM},
3531 {WINED3DFMT_R8G8B8A8_SNORM, "xyzw", FALSE, WINED3D_GL_EXT_NONE},
3532 {WINED3DFMT_R8G8B8A8_SNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3533 {WINED3DFMT_R8G8B8A8_SNORM, "XYZW", FALSE, EXT_TEXTURE_SNORM},
3535 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "xzY1", FALSE, WINED3D_GL_EXT_NONE},
3536 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3537 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "XYZW", FALSE, EXT_TEXTURE_SNORM},
3539 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "xyZW", FALSE, WINED3D_GL_EXT_NONE},
3540 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3542 {WINED3DFMT_ATI1N, "XXXX", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
3543 {WINED3DFMT_ATI1N, "XXXX", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3545 {WINED3DFMT_ATI2N, "XW11", FALSE, ATI_TEXTURE_COMPRESSION_3DC},
3546 {WINED3DFMT_ATI2N, "YX11", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
3547 {WINED3DFMT_ATI2N, "YX11", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3549 {WINED3DFMT_A8_UNORM, "000X", FALSE, WINED3D_GL_EXT_NONE},
3550 {WINED3DFMT_A8_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3552 {WINED3DFMT_L8A8_UNORM, "XXXY", FALSE, WINED3D_GL_EXT_NONE},
3553 {WINED3DFMT_L8A8_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3555 {WINED3DFMT_L4A4_UNORM, "XXXY", FALSE, WINED3D_GL_EXT_NONE},
3556 {WINED3DFMT_L4A4_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3558 {WINED3DFMT_L16_UNORM, "XXX1", FALSE, WINED3D_GL_EXT_NONE},
3559 {WINED3DFMT_L16_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3561 {WINED3DFMT_INTZ, "XXXX", FALSE, WINED3D_GL_EXT_NONE},
3562 {WINED3DFMT_INTZ, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3564 {WINED3DFMT_L8_UNORM, "XXX1", FALSE, ARB_TEXTURE_RG},
3567 use_legacy_fixups = d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR;
3568 for (i = 0; i < ARRAY_SIZE(fixups); ++i)
3570 if (fixups[i].legacy && !use_legacy_fixups)
3571 continue;
3573 if (!gl_info->supported[fixups[i].extension])
3574 continue;
3576 format = get_format_gl_internal(adapter, fixups[i].id);
3577 format->f.color_fixup = create_color_fixup_desc_from_string(fixups[i].fixup);
3580 if (!gl_info->supported[APPLE_YCBCR_422] && !gl_info->supported[APPLE_RGB_422]
3581 && (gl_info->supported[ARB_FRAGMENT_PROGRAM]
3582 || (gl_info->supported[ARB_FRAGMENT_SHADER] && gl_info->supported[ARB_VERTEX_SHADER])))
3584 format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3585 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUY2);
3587 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3588 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_UYVY);
3590 else if (!gl_info->supported[APPLE_YCBCR_422] && gl_info->supported[APPLE_RGB_422])
3592 format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3593 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);
3595 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3596 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);
3598 else if (!gl_info->supported[APPLE_YCBCR_422] && (!gl_info->supported[ARB_FRAGMENT_PROGRAM]
3599 && (!gl_info->supported[ARB_FRAGMENT_SHADER] || !gl_info->supported[ARB_VERTEX_SHADER])))
3601 format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3602 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3603 format->internal = 0;
3605 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3606 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3607 format->internal = 0;
3610 if (gl_info->supported[ARB_FRAGMENT_PROGRAM]
3611 || (gl_info->supported[ARB_FRAGMENT_SHADER] && gl_info->supported[ARB_VERTEX_SHADER]))
3613 format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3614 format->f.attrs |= WINED3D_FORMAT_ATTR_HEIGHT_SCALE;
3615 format->f.height_scale.numerator = 3;
3616 format->f.height_scale.denominator = 2;
3617 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YV12);
3619 format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3620 format->f.attrs |= WINED3D_FORMAT_ATTR_HEIGHT_SCALE;
3621 format->f.height_scale.numerator = 3;
3622 format->f.height_scale.denominator = 2;
3623 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_NV12);
3625 else
3627 format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3628 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3629 format->internal = 0;
3631 format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3632 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_BLIT);
3633 format->internal = 0;
3636 if (gl_info->supported[ARB_FRAGMENT_PROGRAM] || gl_info->supported[ARB_FRAGMENT_SHADER])
3638 format = get_format_gl_internal(adapter, WINED3DFMT_P8_UINT);
3639 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_P8);
3642 if (!gl_info->supported[ARB_HALF_FLOAT_PIXEL])
3644 format = get_format_gl_internal(adapter, WINED3DFMT_R16_FLOAT);
3645 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3647 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16_FLOAT);
3648 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3650 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_FLOAT);
3651 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3654 if (gl_info->quirks & WINED3D_QUIRK_BROKEN_RGBA16)
3656 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_UNORM);
3657 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3660 /* ATI instancing hack: Although ATI cards do not support Shader Model
3661 * 3.0, they support instancing. To query if the card supports instancing
3662 * CheckDeviceFormat() with the special format MAKEFOURCC('I','N','S','T')
3663 * is used. Should an application check for this, provide a proper return
3664 * value. We can do instancing with all shader versions, but we need
3665 * vertex shaders.
3667 * Additionally applications have to set the D3DRS_POINTSIZE render state
3668 * to MAKEFOURCC('I','N','S','T') once to enable instancing. Wined3d
3669 * doesn't need that and just ignores it.
3671 * With Shader Model 3.0 capable cards Instancing 'just works' in Windows. */
3672 /* FIXME: This should just check the shader backend caps. */
3673 if (gl_info->supported[ARB_VERTEX_PROGRAM] || gl_info->supported[ARB_VERTEX_SHADER])
3675 format = get_format_gl_internal(adapter, WINED3DFMT_INST);
3676 format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3679 /* Depth bound test. To query if the card supports it CheckDeviceFormat()
3680 * with the special format MAKEFOURCC('N','V','D','B') is used. It is
3681 * enabled by setting D3DRS_ADAPTIVETESS_X render state to
3682 * MAKEFOURCC('N','V','D','B') and then controlled by setting
3683 * D3DRS_ADAPTIVETESS_Z (zMin) and D3DRS_ADAPTIVETESS_W (zMax) to test
3684 * value. */
3685 if (gl_info->supported[EXT_DEPTH_BOUNDS_TEST])
3687 format = get_format_gl_internal(adapter, WINED3DFMT_NVDB);
3688 format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3691 if (gl_info->supported[ARB_MULTISAMPLE])
3693 format = get_format_gl_internal(adapter, WINED3DFMT_ATOC);
3694 format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3697 /* RESZ aka AMD DX9-level hack for multisampled depth buffer resolve. You query for RESZ
3698 * support by checking for availability of MAKEFOURCC('R','E','S','Z') surfaces with
3699 * RENDERTARGET usage. */
3700 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
3702 format = get_format_gl_internal(adapter, WINED3DFMT_RESZ);
3703 format_set_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_RENDERTARGET);
3706 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3708 format = get_format_gl_by_idx(adapter, i);
3710 if (!(format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE))
3711 continue;
3713 if (is_identity_fixup(format->f.color_fixup))
3714 continue;
3716 TRACE("Checking support for fixup:\n");
3717 dump_color_fixup_desc(format->f.color_fixup);
3718 if (!adapter->shader_backend->shader_color_fixup_supported(format->f.color_fixup)
3719 || !adapter->fragment_pipe->color_fixup_supported(format->f.color_fixup))
3721 TRACE("[FAILED]\n");
3722 format_clear_caps(&format->f, WINED3D_FORMAT_CAP_TEXTURE);
3724 else
3726 TRACE("[OK]\n");
3730 /* These formats are not supported for 3D textures. See also
3731 * WINED3D_FORMAT_CAP_DECOMPRESS. */
3732 format = get_format_gl_internal(adapter, WINED3DFMT_ATI1N);
3733 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3734 format = get_format_gl_internal(adapter, WINED3DFMT_ATI2N);
3735 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3736 format = get_format_gl_internal(adapter, WINED3DFMT_BC4_SNORM);
3737 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3738 format = get_format_gl_internal(adapter, WINED3DFMT_BC5_UNORM);
3739 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3740 format = get_format_gl_internal(adapter, WINED3DFMT_BC5_SNORM);
3741 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3D_FORMAT_CAP_TEXTURE;
3744 static BOOL init_format_vertex_info(const struct wined3d_adapter *adapter,
3745 struct wined3d_gl_info *gl_info)
3747 struct wined3d_format_gl *format;
3748 unsigned int i;
3750 for (i = 0; i < ARRAY_SIZE(format_vertex_info); ++i)
3752 if (!(format = get_format_gl_internal(adapter, format_vertex_info[i].id)))
3753 return FALSE;
3755 if (!gl_info->supported[format_vertex_info[i].extension])
3756 continue;
3758 format->f.emit_idx = format_vertex_info[i].emit_idx;
3759 format->vtx_type = format_vertex_info[i].gl_vtx_type;
3760 format->vtx_format = format->f.component_count;
3761 format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_VERTEX_ATTRIBUTE;
3764 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
3766 format = get_format_gl_internal(adapter, WINED3DFMT_B8G8R8A8_UNORM);
3767 format->vtx_format = GL_BGRA;
3770 return TRUE;
3773 static BOOL init_typeless_formats(const struct wined3d_adapter *adapter)
3775 unsigned int caps[WINED3D_GL_RES_TYPE_COUNT];
3776 unsigned int attrs, i, j;
3778 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
3780 struct wined3d_format *format, *typeless_format;
3782 if (!(format = get_format_internal(adapter, typed_formats[i].id)))
3783 return FALSE;
3784 if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
3785 return FALSE;
3787 attrs = typeless_format->attrs;
3788 memcpy(caps, typeless_format->caps, sizeof(caps));
3789 copy_format(adapter, typeless_format, format);
3790 typeless_format->attrs |= attrs;
3791 for (j = 0; j < ARRAY_SIZE(typeless_format->caps); ++j)
3792 typeless_format->caps[j] |= caps[j];
3795 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
3797 struct wined3d_format *typeless_format, *typeless_ds_format, *ds_format;
3798 struct wined3d_format *depth_view_format, *stencil_view_format;
3799 enum wined3d_format_id format_id;
3801 if (!(typeless_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].typeless_id)))
3802 return FALSE;
3803 if (!(ds_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].depth_stencil_id)))
3804 return FALSE;
3806 typeless_ds_format = get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
3807 typeless_ds_format->id = typeless_depth_stencil_formats[i].typeless_id;
3808 copy_format(adapter, typeless_ds_format, ds_format);
3809 typeless_ds_format->attrs = typeless_format->attrs;
3810 for (j = 0; j < ARRAY_SIZE(typeless_ds_format->caps); ++j)
3812 typeless_ds_format->caps[j] = typeless_format->caps[j];
3813 typeless_format->caps[j] &= ~WINED3D_FORMAT_CAP_DEPTH_STENCIL;
3816 if ((format_id = typeless_depth_stencil_formats[i].depth_view_id)
3817 && typeless_depth_stencil_formats[i].separate_depth_view_format)
3819 if (!(depth_view_format = get_format_internal(adapter, format_id)))
3820 return FALSE;
3821 copy_format(adapter, depth_view_format, ds_format);
3822 depth_view_format->red_size = depth_view_format->depth_size;
3823 depth_view_format->depth_size = 0;
3824 depth_view_format->stencil_size = 0;
3826 if ((format_id = typeless_depth_stencil_formats[i].stencil_view_id))
3828 if (!(stencil_view_format = get_format_internal(adapter, format_id)))
3829 return FALSE;
3830 copy_format(adapter, stencil_view_format, ds_format);
3831 stencil_view_format->green_size = stencil_view_format->stencil_size;
3832 stencil_view_format->depth_size = 0;
3833 stencil_view_format->stencil_size = 0;
3837 return TRUE;
3840 static void init_format_gen_mipmap_info(const struct wined3d_adapter *adapter,
3841 struct wined3d_gl_info *gl_info)
3843 unsigned int i, j;
3845 if (!gl_info->fbo_ops.glGenerateMipmap)
3846 return;
3848 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3850 struct wined3d_format *format = get_format_by_idx(adapter, i);
3852 for (j = 0; j < ARRAY_SIZE(format->caps); ++j)
3853 if (!(~format->caps[j] & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FILTERING)))
3854 format->caps[j] |= WINED3D_FORMAT_CAP_GEN_MIPMAP;
3858 BOOL wined3d_caps_gl_ctx_test_viewport_subpixel_bits(struct wined3d_caps_gl_ctx *ctx)
3860 static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
3861 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3862 static const float offset = -63.0f / 128.0f;
3863 GLuint texture, fbo;
3864 uint32_t readback[4];
3865 unsigned int i;
3867 gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
3868 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3869 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
3870 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ARRAY_SIZE(readback), 1, 0,
3871 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
3872 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3873 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3874 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
3875 GL_TEXTURE_2D, texture, 0);
3876 checkGLcall("create resources");
3878 gl_info->gl_ops.gl.p_glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
3879 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3880 GL_EXTCALL(glViewportIndexedf(0, offset, offset, 4.0f, 1.0f));
3881 draw_test_quad(ctx, NULL, &red);
3882 checkGLcall("draw");
3884 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3885 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
3886 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3887 checkGLcall("readback");
3889 TRACE("Readback colors are 0x%08x, 0x%08x, 0x%08x, 0x%08x.\n",
3890 readback[0], readback[1], readback[2], readback[3]);
3892 gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
3893 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3894 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3895 checkGLcall("delete resources");
3897 for (i = 0; i < ARRAY_SIZE(readback); ++i)
3899 if (readback[i] != 0xffff0000)
3900 return FALSE;
3902 return TRUE;
3905 bool wined3d_caps_gl_ctx_test_filling_convention(struct wined3d_caps_gl_ctx *ctx, float offset)
3907 static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
3908 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3909 unsigned int x, y, clear = 0, draw = 0;
3910 GLuint texture, fbo;
3911 uint32_t readback[8][8];
3913 /* This is a very simple test to find out how GL handles polygon edges:
3914 * Draw a 1x1 quad exactly through 4 adjacent pixel centers in an 8x8
3915 * viewport and see which pixel it ends up in. So far we've seen top left
3916 * and bottom left conventions. This test may produce unexpected results
3917 * if the driver forces multisampling on us.
3919 * If we find a bottom-left filling behavior we also move the x-axis
3920 * by the same amount. This is necessary to keep diagonals that go
3921 * through the pixel center intact.
3923 * Note that we are ignoring some settings that might influence the
3924 * driver: How we switch GL to an upper-left coordinate system,
3925 * shaders vs fixed function GL. Testing these isn't possible with
3926 * the current draw_test_quad() infrastructure. Also the test is
3927 * skipped if we are not using FBOs. Drawing into the onscreen
3928 * frame buffer may also yield different driver behavior.
3930 * The minimum offset also depends on the viewport size, although
3931 * the relation between those two is GPU dependent and not exactly
3932 * sensible. E.g. a 8192x8192 viewport on a GeForce 9 needs at
3933 * least an offset of 1/240.9, whereas a 8x8 one needs 1/255.982;
3934 * 32x32 needs 1/255.935. 4x4 and lower are happy with something
3935 * below 1/256. The 8x8 size below has been arbitrarily chosen to
3936 * get a useful result out of that card and avoid allocating a
3937 * gigantic texture during library init.
3939 * Newer cards usually do the right thing anyway. In cases where
3940 * they do not (e.g. Radeon GPUs in a macbookpro14,3 running MacOS)
3941 * an offset of 1/2^20 is enough. */
3942 const struct wined3d_vec3 edge_geometry[] =
3944 {(-1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
3945 {( 1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
3946 {(-1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
3947 {( 1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
3950 gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
3951 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3952 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
3953 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0,
3954 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
3955 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3956 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3957 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
3958 GL_TEXTURE_2D, texture, 0);
3959 checkGLcall("create resources");
3961 gl_info->gl_ops.gl.p_glViewport(0, 0, 8, 8);
3962 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
3963 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3965 draw_test_quad(ctx, edge_geometry, &red);
3966 checkGLcall("draw");
3968 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3969 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
3970 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3971 checkGLcall("readback");
3973 gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
3974 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3975 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3976 checkGLcall("delete resources");
3978 /* We expect that exactly one fragment is generated. */
3979 for (y = 0; y < ARRAY_SIZE(readback); ++y)
3981 for (x = 0; x < ARRAY_SIZE(readback[0]); ++x)
3983 if (readback[y][x] == 0xff0000ff)
3984 clear++;
3985 else if (readback[y][x] == 0xffff0000)
3986 draw++;
3990 if (clear != 63 || draw != 1)
3992 FIXME("Unexpected filling convention test result.\n");
3993 return FALSE;
3996 /* One pixel was drawn, check if it is the expected one */
3997 return readback[3][3] == 0xffff0000;
3999 static float wined3d_adapter_find_polyoffset_scale(struct wined3d_caps_gl_ctx *ctx, GLenum format)
4001 const struct wined3d_gl_info *gl_info = ctx->gl_info;
4002 static const struct wined3d_color blue = {0.0f, 0.0f, 1.0f, 1.0f};
4003 GLuint fbo, color, depth;
4004 unsigned int low = 0, high = 32, cur;
4005 uint32_t readback[256];
4006 static const struct wined3d_vec3 geometry[] =
4008 {-1.0f, -1.0f, -1.0f},
4009 { 1.0f, -1.0f, 0.0f},
4010 {-1.0f, 1.0f, -1.0f},
4011 { 1.0f, 1.0f, 0.0f},
4014 /* Most drivers want 2^23 for fixed point depth buffers, including r300g, r600g,
4015 * Nvidia. Use this as a fallback if the detection fails. */
4016 unsigned int fallback = 23;
4018 gl_info->gl_ops.gl.p_glGenTextures(1, &color);
4019 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
4020 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
4021 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);
4023 gl_info->fbo_ops.glGenRenderbuffers(1, &depth);
4024 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, depth);
4025 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format, 256, 1);
4027 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
4028 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
4029 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
4030 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
4031 checkGLcall("Setup framebuffer");
4033 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
4034 gl_info->gl_ops.gl.p_glClearDepth(0.5f);
4035 gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
4036 gl_info->gl_ops.gl.p_glEnable(GL_POLYGON_OFFSET_FILL);
4037 gl_info->gl_ops.gl.p_glViewport(0, 0, 256, 1);
4038 checkGLcall("Misc parameters");
4040 for (;;)
4042 if (high - low <= 1)
4044 ERR("PolygonOffset scale factor detection failed, using fallback value 2^%u.\n", fallback);
4045 cur = fallback;
4046 break;
4048 cur = (low + high) / 2;
4050 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4051 /* The post viewport transform Z of the geometry runs from 0.0 to 0.5. We want to push it another
4052 * 0.25 so that the Z buffer content (0.5) cuts the quad off at half the screen. */
4053 gl_info->gl_ops.gl.p_glPolygonOffset(0.0f, (float)(1u << cur) * 0.25f);
4054 draw_test_quad(ctx, geometry, &blue);
4055 checkGLcall("Test draw");
4057 /* Rebinding texture to workaround a fglrx bug. */
4058 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
4059 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
4060 checkGLcall("readback");
4062 TRACE("low %02u, high %02u, cur %2u, 0=0x%08x, 125=0x%08x, 131=0x%08x, 255=0x%08x\n",
4063 low, high, cur, readback[0], readback[125], readback[131], readback[255]);
4065 if ((readback[125] & 0xff) < 0xa0)
4066 high = cur;
4067 else if ((readback[131] & 0xff) > 0xa0)
4068 low = cur;
4069 else
4071 TRACE("Found scale factor 2^%u for format %x.\n", cur, format);
4072 break;
4076 gl_info->gl_ops.gl.p_glDeleteTextures(1, &color);
4077 gl_info->fbo_ops.glDeleteRenderbuffers(1, &depth);
4078 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
4079 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
4080 checkGLcall("Delete framebuffer");
4082 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
4083 gl_info->gl_ops.gl.p_glDisable(GL_POLYGON_OFFSET_FILL);
4084 return (float)(1u << cur);
4087 static void init_format_depth_bias_scale(struct wined3d_adapter *adapter,
4088 struct wined3d_caps_gl_ctx *ctx)
4090 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
4091 unsigned int i;
4093 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
4095 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
4097 if (format->f.depth_size && (format->f.caps[WINED3D_GL_RES_TYPE_RB] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
4099 TRACE("Testing depth bias scale for format %s.\n", debug_d3dformat(format->f.id));
4100 format->f.depth_bias_scale = wined3d_adapter_find_polyoffset_scale(ctx, format->internal);
4102 if (!(d3d_info->wined3d_creation_flags & WINED3D_NORMALIZED_DEPTH_BIAS))
4104 /* The single-precision binary floating-point format has
4105 * a significand precision of 24 bits.
4107 if (format->f.attrs & WINED3D_FORMAT_ATTR_FLOAT)
4108 format->f.depth_bias_scale /= 1u << 24;
4109 else
4110 format->f.depth_bias_scale /= 1u << format->f.depth_size;
4116 static BOOL wined3d_adapter_init_format_info(struct wined3d_adapter *adapter, size_t format_size)
4118 unsigned int count = WINED3D_FORMAT_COUNT + ARRAY_SIZE(typeless_depth_stencil_formats);
4120 if (!(adapter->formats = calloc(count, format_size)))
4122 ERR("Failed to allocate memory.\n");
4123 return FALSE;
4125 adapter->format_size = format_size;
4127 if (!init_format_base_info(adapter))
4128 goto fail;
4129 if (!init_format_block_info(adapter))
4130 goto fail;
4131 if (!init_format_decompress_info(adapter))
4132 goto fail;
4133 if (!init_srgb_formats(adapter))
4134 goto fail;
4136 return TRUE;
4138 fail:
4139 free(adapter->formats);
4140 adapter->formats = NULL;
4141 return FALSE;
4144 BOOL wined3d_adapter_no3d_init_format_info(struct wined3d_adapter *adapter)
4146 struct wined3d_format *format;
4147 unsigned int i;
4149 static const enum wined3d_format_id blit_formats[] =
4151 WINED3DFMT_B8G8R8A8_UNORM,
4152 WINED3DFMT_B8G8R8X8_UNORM,
4153 WINED3DFMT_B5G6R5_UNORM,
4154 WINED3DFMT_B5G5R5X1_UNORM,
4155 WINED3DFMT_B5G5R5A1_UNORM,
4156 WINED3DFMT_B4G4R4A4_UNORM,
4157 WINED3DFMT_B2G3R3_UNORM,
4158 WINED3DFMT_A8_UNORM,
4159 WINED3DFMT_B2G3R3A8_UNORM,
4160 WINED3DFMT_B4G4R4X4_UNORM,
4161 WINED3DFMT_R10G10B10A2_UNORM,
4162 WINED3DFMT_R8G8B8A8_UNORM,
4163 WINED3DFMT_R8G8B8X8_UNORM,
4164 WINED3DFMT_R16G16_UNORM,
4165 WINED3DFMT_B10G10R10A2_UNORM,
4166 WINED3DFMT_R16G16B16A16_UNORM,
4167 WINED3DFMT_P8_UINT,
4170 if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format)))
4171 return FALSE;
4173 for (i = 0; i < ARRAY_SIZE(blit_formats); ++i)
4175 if (!(format = get_format_internal(adapter, blit_formats[i])))
4176 return FALSE;
4178 format->caps[WINED3D_GL_RES_TYPE_TEX_2D] |= WINED3D_FORMAT_CAP_BLIT;
4179 format->caps[WINED3D_GL_RES_TYPE_RB] |= WINED3D_FORMAT_CAP_BLIT;
4182 return TRUE;
4185 /* Context activation is done by the caller. */
4186 BOOL wined3d_adapter_gl_init_format_info(struct wined3d_adapter *adapter, struct wined3d_caps_gl_ctx *ctx)
4188 struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(adapter)->gl_info;
4190 if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format_gl)))
4191 return FALSE;
4193 if (!init_format_texture_info(adapter, gl_info)) goto fail;
4194 if (!init_format_vertex_info(adapter, gl_info)) goto fail;
4196 apply_format_fixups(adapter, gl_info);
4197 init_format_fbo_compat_info(adapter, ctx);
4198 init_format_filter_info(adapter, gl_info);
4199 init_format_gen_mipmap_info(adapter, gl_info);
4200 init_format_depth_bias_scale(adapter, ctx);
4202 if (!init_typeless_formats(adapter)) goto fail;
4204 return TRUE;
4206 fail:
4207 free(adapter->formats);
4208 adapter->formats = NULL;
4209 return FALSE;
4212 static void init_vulkan_format_info(struct wined3d_format_vk *format,
4213 const struct wined3d_vk_info *vk_info, VkPhysicalDevice vk_physical_device)
4215 static const struct
4217 enum wined3d_format_id id;
4218 VkFormat vk_format;
4219 const char *fixup;
4221 vulkan_formats[] =
4223 {WINED3DFMT_R32G32B32A32_FLOAT, VK_FORMAT_R32G32B32A32_SFLOAT, },
4224 {WINED3DFMT_R32G32B32A32_UINT, VK_FORMAT_R32G32B32A32_UINT, },
4225 {WINED3DFMT_R32G32B32A32_SINT, VK_FORMAT_R32G32B32A32_SINT, },
4226 {WINED3DFMT_R32G32B32_FLOAT, VK_FORMAT_R32G32B32_SFLOAT, },
4227 {WINED3DFMT_R32G32B32_UINT, VK_FORMAT_R32G32B32_UINT, },
4228 {WINED3DFMT_R32G32B32_SINT, VK_FORMAT_R32G32B32_SINT, },
4229 {WINED3DFMT_R16G16B16A16_FLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, },
4230 {WINED3DFMT_R16G16B16A16_UNORM, VK_FORMAT_R16G16B16A16_UNORM, },
4231 {WINED3DFMT_R16G16B16A16_UINT, VK_FORMAT_R16G16B16A16_UINT, },
4232 {WINED3DFMT_R16G16B16A16_SNORM, VK_FORMAT_R16G16B16A16_SNORM, },
4233 {WINED3DFMT_R16G16B16A16_SINT, VK_FORMAT_R16G16B16A16_SINT, },
4234 {WINED3DFMT_R32G32_FLOAT, VK_FORMAT_R32G32_SFLOAT, },
4235 {WINED3DFMT_R32G32_UINT, VK_FORMAT_R32G32_UINT, },
4236 {WINED3DFMT_R32G32_SINT, VK_FORMAT_R32G32_SINT, },
4237 {WINED3DFMT_R10G10B10A2_UNORM, VK_FORMAT_A2B10G10R10_UNORM_PACK32,},
4238 {WINED3DFMT_R11G11B10_FLOAT, VK_FORMAT_B10G11R11_UFLOAT_PACK32, },
4239 {WINED3DFMT_R8G8_UNORM, VK_FORMAT_R8G8_UNORM, },
4240 {WINED3DFMT_R8G8_UINT, VK_FORMAT_R8G8_UINT, },
4241 {WINED3DFMT_R8G8_SNORM, VK_FORMAT_R8G8_SNORM, },
4242 {WINED3DFMT_R8G8_SINT, VK_FORMAT_R8G8_SINT, },
4243 {WINED3DFMT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, },
4244 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, VK_FORMAT_R8G8B8A8_SRGB, },
4245 {WINED3DFMT_R8G8B8A8_UINT, VK_FORMAT_R8G8B8A8_UINT, },
4246 {WINED3DFMT_R8G8B8A8_SNORM, VK_FORMAT_R8G8B8A8_SNORM, },
4247 {WINED3DFMT_R8G8B8A8_SINT, VK_FORMAT_R8G8B8A8_SINT, },
4248 {WINED3DFMT_R16G16_FLOAT, VK_FORMAT_R16G16_SFLOAT, },
4249 {WINED3DFMT_R16G16_UNORM, VK_FORMAT_R16G16_UNORM, },
4250 {WINED3DFMT_R16G16_UINT, VK_FORMAT_R16G16_UINT, },
4251 {WINED3DFMT_R16G16_SNORM, VK_FORMAT_R16G16_SNORM, },
4252 {WINED3DFMT_R16G16_SINT, VK_FORMAT_R16G16_SINT, },
4253 {WINED3DFMT_D32_FLOAT, VK_FORMAT_D32_SFLOAT, },
4254 {WINED3DFMT_R32_FLOAT, VK_FORMAT_R32_SFLOAT, },
4255 {WINED3DFMT_R32_UINT, VK_FORMAT_R32_UINT, },
4256 {WINED3DFMT_R32_SINT, VK_FORMAT_R32_SINT, },
4257 {WINED3DFMT_R16_FLOAT, VK_FORMAT_R16_SFLOAT, },
4258 {WINED3DFMT_D16_UNORM, VK_FORMAT_D16_UNORM, },
4259 {WINED3DFMT_R16_UNORM, VK_FORMAT_R16_UNORM, },
4260 {WINED3DFMT_R16_UINT, VK_FORMAT_R16_UINT, },
4261 {WINED3DFMT_R16_SNORM, VK_FORMAT_R16_SNORM, },
4262 {WINED3DFMT_R16_SINT, VK_FORMAT_R16_SINT, },
4263 {WINED3DFMT_R8_UNORM, VK_FORMAT_R8_UNORM, },
4264 {WINED3DFMT_R8_UINT, VK_FORMAT_R8_UINT, },
4265 {WINED3DFMT_R8_SNORM, VK_FORMAT_R8_SNORM, },
4266 {WINED3DFMT_R8_SINT, VK_FORMAT_R8_SINT, },
4267 {WINED3DFMT_A8_UNORM, VK_FORMAT_R8_UNORM, "000X"},
4268 {WINED3DFMT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, },
4269 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, VK_FORMAT_B8G8R8A8_SRGB, },
4270 {WINED3DFMT_B8G8R8X8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, "XYZ1"},
4271 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, VK_FORMAT_B8G8R8A8_SRGB, "XYZ1"},
4272 {WINED3DFMT_B5G6R5_UNORM, VK_FORMAT_R5G6B5_UNORM_PACK16, },
4273 {WINED3DFMT_BC1_UNORM, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, },
4274 {WINED3DFMT_BC1_UNORM_SRGB, VK_FORMAT_BC1_RGBA_SRGB_BLOCK, },
4275 {WINED3DFMT_BC2_UNORM, VK_FORMAT_BC2_UNORM_BLOCK, },
4276 {WINED3DFMT_BC2_UNORM_SRGB, VK_FORMAT_BC2_SRGB_BLOCK, },
4277 {WINED3DFMT_BC3_UNORM, VK_FORMAT_BC3_UNORM_BLOCK, },
4278 {WINED3DFMT_BC3_UNORM_SRGB, VK_FORMAT_BC3_SRGB_BLOCK, },
4279 {WINED3DFMT_BC4_UNORM, VK_FORMAT_BC4_UNORM_BLOCK, },
4280 {WINED3DFMT_BC4_SNORM, VK_FORMAT_BC4_SNORM_BLOCK, },
4281 {WINED3DFMT_BC5_UNORM, VK_FORMAT_BC5_UNORM_BLOCK, },
4282 {WINED3DFMT_BC5_SNORM, VK_FORMAT_BC5_SNORM_BLOCK, },
4283 {WINED3DFMT_BC6H_UF16, VK_FORMAT_BC6H_UFLOAT_BLOCK, },
4284 {WINED3DFMT_BC6H_SF16, VK_FORMAT_BC6H_SFLOAT_BLOCK, },
4285 {WINED3DFMT_BC7_UNORM, VK_FORMAT_BC7_UNORM_BLOCK, },
4286 {WINED3DFMT_BC7_UNORM_SRGB, VK_FORMAT_BC7_SRGB_BLOCK, },
4287 {WINED3DFMT_R9G9B9E5_SHAREDEXP, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, },
4288 {WINED3DFMT_D32_FLOAT_S8X24_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4289 {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4290 {WINED3DFMT_X32_TYPELESS_G8X24_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4291 {WINED3DFMT_D24_UNORM_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT, },
4293 VkFormat vk_format = VK_FORMAT_UNDEFINED;
4294 VkImageFormatProperties image_properties;
4295 VkFormatFeatureFlags texture_flags;
4296 VkFormatProperties properties;
4297 VkImageUsageFlags vk_usage;
4298 unsigned int caps;
4299 const char *fixup;
4300 unsigned int i;
4301 uint32_t mask;
4302 VkResult vr;
4304 for (i = 0; i < ARRAY_SIZE(vulkan_formats); ++i)
4306 if (vulkan_formats[i].id == format->f.id)
4308 vk_format = vulkan_formats[i].vk_format;
4309 fixup = vulkan_formats[i].fixup;
4310 break;
4313 if (!vk_format)
4315 WARN("Unsupported format %s.\n", debug_d3dformat(format->f.id));
4316 return;
4319 format->vk_format = vk_format;
4320 if (fixup)
4321 format->f.color_fixup = create_color_fixup_desc_from_string(fixup);
4322 else
4323 format->f.color_fixup = COLOR_FIXUP_IDENTITY;
4325 VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
4326 if (vk_format == VK_FORMAT_D24_UNORM_S8_UINT)
4328 if (~properties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4330 /* AMD doesn't support VK_FORMAT_D24_UNORM_S8_UINT. */
4331 WARN("Mapping VK_FORMAT_D24_UNORM_S8_UINT to VK_FORMAT_D32_SFLOAT_S8_UINT.\n");
4333 format->vk_format = vk_format = VK_FORMAT_D32_SFLOAT_S8_UINT;
4334 VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
4338 if (properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)
4339 format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_VERTEX_ATTRIBUTE;
4340 if (properties.bufferFeatures & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)
4341 format->f.caps[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3D_FORMAT_CAP_TEXTURE;
4343 caps = 0;
4344 texture_flags = properties.linearTilingFeatures | properties.optimalTilingFeatures;
4345 if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
4347 caps |= WINED3D_FORMAT_CAP_TEXTURE | WINED3D_FORMAT_CAP_VTF;
4349 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
4351 caps |= WINED3D_FORMAT_CAP_RENDERTARGET;
4353 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)
4355 caps |= WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING;
4357 if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4359 caps |= WINED3D_FORMAT_CAP_DEPTH_STENCIL;
4361 if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)
4363 caps |= WINED3D_FORMAT_CAP_FILTERING;
4365 if (texture_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)
4367 caps |= WINED3D_FORMAT_CAP_UNORDERED_ACCESS;
4369 if ((texture_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)
4370 && (texture_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT))
4372 caps |= WINED3D_FORMAT_CAP_BLIT;
4375 if (!(~caps & (WINED3D_FORMAT_CAP_RENDERTARGET | WINED3D_FORMAT_CAP_FILTERING)))
4376 caps |= WINED3D_FORMAT_CAP_GEN_MIPMAP;
4378 format->f.caps[WINED3D_GL_RES_TYPE_TEX_1D] |= caps;
4379 format->f.caps[WINED3D_GL_RES_TYPE_TEX_2D] |= caps;
4380 format->f.caps[WINED3D_GL_RES_TYPE_TEX_3D] |= caps;
4381 format->f.caps[WINED3D_GL_RES_TYPE_TEX_CUBE] |= caps;
4383 vk_usage = 0;
4384 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
4385 vk_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4386 else if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4387 vk_usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4388 if (vk_usage)
4390 if ((vr = VK_CALL(vkGetPhysicalDeviceImageFormatProperties(vk_physical_device, vk_format,
4391 VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, vk_usage, 0, &image_properties))) < 0)
4393 ERR("Failed to get image format properties, vr %s.\n", wined3d_debug_vkresult(vr));
4394 return;
4397 mask = image_properties.sampleCounts & 0x3f;
4398 while (mask)
4400 i = (1u << wined3d_bit_scan(&mask)) - 1;
4401 format->f.multisample_types |= 1u << i;
4406 BOOL wined3d_adapter_vk_init_format_info(struct wined3d_adapter_vk *adapter_vk,
4407 const struct wined3d_vk_info *vk_info)
4409 VkPhysicalDevice vk_physical_device = adapter_vk->physical_device;
4410 struct wined3d_adapter *adapter = &adapter_vk->a;
4411 struct wined3d_format_vk *format;
4412 unsigned int i;
4414 if (!wined3d_adapter_init_format_info(adapter, sizeof(*format)))
4415 return FALSE;
4417 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
4419 format = wined3d_format_vk_mutable(get_format_by_idx(adapter, i));
4421 if (format->f.id)
4422 init_vulkan_format_info(format, vk_info, vk_physical_device);
4425 if (!init_typeless_formats(adapter)) goto fail;
4427 return TRUE;
4429 fail:
4430 free(adapter->formats);
4431 adapter->formats = NULL;
4432 return FALSE;
4435 const struct wined3d_format *wined3d_get_format(const struct wined3d_adapter *adapter,
4436 enum wined3d_format_id format_id, unsigned int bind_flags)
4438 const struct wined3d_format *format;
4439 int idx = get_format_idx(format_id);
4440 unsigned int i;
4442 if (idx == -1)
4444 FIXME("Can't find format %s (%#x) in the format lookup table.\n",
4445 debug_d3dformat(format_id), format_id);
4446 return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4449 format = get_format_by_idx(adapter, idx);
4451 if (bind_flags & WINED3D_BIND_DEPTH_STENCIL && wined3d_format_is_typeless(format))
4453 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
4455 if (typeless_depth_stencil_formats[i].typeless_id == format_id)
4456 return get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
4459 FIXME("Cannot find depth/stencil typeless format %s (%#x).\n",
4460 debug_d3dformat(format_id), format_id);
4461 return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4464 return format;
4467 enum wined3d_format_id wined3d_get_typed_format_id(const struct wined3d_adapter *adapter,
4468 const struct wined3d_format *format, enum wined3d_channel_type channel_type)
4470 const struct wined3d_typed_format_info *info;
4471 uint32_t i;
4473 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
4475 info = &typed_formats[i];
4476 if (info->typeless_id == format->typeless_id && map_channel_type(info->channels[0]) == channel_type)
4477 return info->id;
4480 return WINED3DFMT_UNKNOWN;
4483 BOOL wined3d_format_is_depth_view(enum wined3d_format_id resource_format_id,
4484 enum wined3d_format_id view_format_id)
4486 unsigned int i;
4488 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
4490 if (typeless_depth_stencil_formats[i].typeless_id == resource_format_id)
4491 return typeless_depth_stencil_formats[i].depth_view_id == view_format_id;
4493 return FALSE;
4496 void wined3d_format_calculate_pitch(const struct wined3d_format *format, unsigned int alignment,
4497 unsigned int width, unsigned int height, unsigned int *row_pitch, unsigned int *slice_pitch)
4499 /* For block based formats, pitch means the amount of bytes to the next
4500 * row of blocks rather than the next row of pixels. */
4501 if (format->attrs & WINED3D_FORMAT_ATTR_BLOCKS)
4503 unsigned int row_block_count = (width + format->block_width - 1) / format->block_width;
4504 unsigned int slice_block_count = (height + format->block_height - 1) / format->block_height;
4505 *row_pitch = row_block_count * format->block_byte_count;
4506 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
4507 *slice_pitch = *row_pitch * slice_block_count;
4509 else
4511 *row_pitch = format->byte_count * width; /* Bytes / row */
4512 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
4513 *slice_pitch = *row_pitch * height;
4516 if (format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
4518 /* The D3D format requirements make sure that the resulting format is an integer again */
4519 *slice_pitch *= format->height_scale.numerator;
4520 *slice_pitch /= format->height_scale.denominator;
4523 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
4526 UINT wined3d_format_calculate_size(const struct wined3d_format *format, UINT alignment,
4527 UINT width, UINT height, UINT depth)
4529 unsigned int row_pitch, slice_pitch;
4531 if (format->id == WINED3DFMT_UNKNOWN)
4532 return 0;
4534 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
4535 return width * height * depth * format->byte_count;
4537 wined3d_format_calculate_pitch(format, alignment, width, height, &row_pitch, &slice_pitch);
4539 return slice_pitch * depth;
4542 BOOL wined3d_formats_are_srgb_variants(enum wined3d_format_id format1, enum wined3d_format_id format2)
4544 unsigned int i;
4546 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
4548 if (format1 == format_srgb_info[i].srgb_format_id)
4549 return format2 == format_srgb_info[i].base_format_id;
4550 if (format1 == format_srgb_info[i].base_format_id)
4551 return format2 == format_srgb_info[i].srgb_format_id;
4553 return FALSE;
4556 /*****************************************************************************
4557 * Trace formatting of useful values
4559 const char *debug_box(const struct wined3d_box *box)
4561 if (!box)
4562 return "(null)";
4563 return wine_dbg_sprintf("(%u, %u, %u)-(%u, %u, %u)",
4564 box->left, box->top, box->front,
4565 box->right, box->bottom, box->back);
4568 const char *debug_color(const struct wined3d_color *color)
4570 if (!color)
4571 return "(null)";
4572 return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
4573 color->r, color->g, color->b, color->a);
4576 const char *debug_ivec4(const struct wined3d_ivec4 *v)
4578 if (!v)
4579 return "(null)";
4580 return wine_dbg_sprintf("{%d, %d, %d, %d}",
4581 v->x, v->y, v->z, v->w);
4584 const char *debug_uvec4(const struct wined3d_uvec4 *v)
4586 if (!v)
4587 return "(null)";
4588 return wine_dbg_sprintf("{%u, %u, %u, %u}",
4589 v->x, v->y, v->z, v->w);
4592 const char *debug_vec4(const struct wined3d_vec4 *v)
4594 if (!v)
4595 return "(null)";
4596 return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
4597 v->x, v->y, v->z, v->w);
4600 const char *debug_const_bo_address(const struct wined3d_const_bo_address *address)
4602 if (!address)
4603 return "(null)";
4604 return wine_dbg_sprintf("{%p:%p}", address->buffer_object, address->addr);
4607 const char *debug_bo_address(const struct wined3d_bo_address *address)
4609 return debug_const_bo_address((const struct wined3d_const_bo_address *)address);
4612 const char *debug_d3dformat(enum wined3d_format_id format_id)
4614 switch (format_id)
4616 #define FMT_TO_STR(format_id) case format_id: return #format_id
4617 FMT_TO_STR(WINED3DFMT_UNKNOWN);
4618 FMT_TO_STR(WINED3DFMT_B8G8R8_UNORM);
4619 FMT_TO_STR(WINED3DFMT_B5G5R5X1_UNORM);
4620 FMT_TO_STR(WINED3DFMT_B4G4R4A4_UNORM);
4621 FMT_TO_STR(WINED3DFMT_B2G3R3_UNORM);
4622 FMT_TO_STR(WINED3DFMT_B2G3R3A8_UNORM);
4623 FMT_TO_STR(WINED3DFMT_B4G4R4X4_UNORM);
4624 FMT_TO_STR(WINED3DFMT_R8G8B8X8_UNORM);
4625 FMT_TO_STR(WINED3DFMT_B10G10R10A2_UNORM);
4626 FMT_TO_STR(WINED3DFMT_P8_UINT_A8_UNORM);
4627 FMT_TO_STR(WINED3DFMT_P8_UINT);
4628 FMT_TO_STR(WINED3DFMT_L8_UNORM);
4629 FMT_TO_STR(WINED3DFMT_L8A8_UNORM);
4630 FMT_TO_STR(WINED3DFMT_L4A4_UNORM);
4631 FMT_TO_STR(WINED3DFMT_R5G5_SNORM_L6_UNORM);
4632 FMT_TO_STR(WINED3DFMT_R8G8_SNORM_L8X8_UNORM);
4633 FMT_TO_STR(WINED3DFMT_R10G11B11_SNORM);
4634 FMT_TO_STR(WINED3DFMT_R10G10B10X2_TYPELESS);
4635 FMT_TO_STR(WINED3DFMT_R10G10B10X2_UINT);
4636 FMT_TO_STR(WINED3DFMT_R10G10B10X2_SNORM);
4637 FMT_TO_STR(WINED3DFMT_R10G10B10_SNORM_A2_UNORM);
4638 FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
4639 FMT_TO_STR(WINED3DFMT_D32_UNORM);
4640 FMT_TO_STR(WINED3DFMT_S1_UINT_D15_UNORM);
4641 FMT_TO_STR(WINED3DFMT_X8D24_UNORM);
4642 FMT_TO_STR(WINED3DFMT_S4X4_UINT_D24_UNORM);
4643 FMT_TO_STR(WINED3DFMT_L16_UNORM);
4644 FMT_TO_STR(WINED3DFMT_S8_UINT_D24_FLOAT);
4645 FMT_TO_STR(WINED3DFMT_R8G8_SNORM_Cx);
4646 FMT_TO_STR(WINED3DFMT_R32G32B32A32_TYPELESS);
4647 FMT_TO_STR(WINED3DFMT_R32G32B32A32_FLOAT);
4648 FMT_TO_STR(WINED3DFMT_R32G32B32A32_UINT);
4649 FMT_TO_STR(WINED3DFMT_R32G32B32A32_SINT);
4650 FMT_TO_STR(WINED3DFMT_R32G32B32_TYPELESS);
4651 FMT_TO_STR(WINED3DFMT_R32G32B32_FLOAT);
4652 FMT_TO_STR(WINED3DFMT_R32G32B32_UINT);
4653 FMT_TO_STR(WINED3DFMT_R32G32B32_SINT);
4654 FMT_TO_STR(WINED3DFMT_R16G16B16A16_TYPELESS);
4655 FMT_TO_STR(WINED3DFMT_R16G16B16A16_FLOAT);
4656 FMT_TO_STR(WINED3DFMT_R16G16B16A16_UNORM);
4657 FMT_TO_STR(WINED3DFMT_R16G16B16A16_UINT);
4658 FMT_TO_STR(WINED3DFMT_R16G16B16A16_SNORM);
4659 FMT_TO_STR(WINED3DFMT_R16G16B16A16_SINT);
4660 FMT_TO_STR(WINED3DFMT_R32G32_TYPELESS);
4661 FMT_TO_STR(WINED3DFMT_R32G32_FLOAT);
4662 FMT_TO_STR(WINED3DFMT_R32G32_UINT);
4663 FMT_TO_STR(WINED3DFMT_R32G32_SINT);
4664 FMT_TO_STR(WINED3DFMT_R32G8X24_TYPELESS);
4665 FMT_TO_STR(WINED3DFMT_D32_FLOAT_S8X24_UINT);
4666 FMT_TO_STR(WINED3DFMT_R32_FLOAT_X8X24_TYPELESS);
4667 FMT_TO_STR(WINED3DFMT_X32_TYPELESS_G8X24_UINT);
4668 FMT_TO_STR(WINED3DFMT_R10G10B10A2_TYPELESS);
4669 FMT_TO_STR(WINED3DFMT_R10G10B10A2_UNORM);
4670 FMT_TO_STR(WINED3DFMT_R10G10B10A2_UINT);
4671 FMT_TO_STR(WINED3DFMT_R10G10B10A2_SNORM);
4672 FMT_TO_STR(WINED3DFMT_R10G10B10_XR_BIAS_A2_UNORM);
4673 FMT_TO_STR(WINED3DFMT_R11G11B10_FLOAT);
4674 FMT_TO_STR(WINED3DFMT_R8G8B8A8_TYPELESS);
4675 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM);
4676 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM_SRGB);
4677 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UINT);
4678 FMT_TO_STR(WINED3DFMT_R8G8B8A8_SNORM);
4679 FMT_TO_STR(WINED3DFMT_R8G8B8A8_SINT);
4680 FMT_TO_STR(WINED3DFMT_R16G16_TYPELESS);
4681 FMT_TO_STR(WINED3DFMT_R16G16_FLOAT);
4682 FMT_TO_STR(WINED3DFMT_R16G16_UNORM);
4683 FMT_TO_STR(WINED3DFMT_R16G16_UINT);
4684 FMT_TO_STR(WINED3DFMT_R16G16_SNORM);
4685 FMT_TO_STR(WINED3DFMT_R16G16_SINT);
4686 FMT_TO_STR(WINED3DFMT_R32_TYPELESS);
4687 FMT_TO_STR(WINED3DFMT_D32_FLOAT);
4688 FMT_TO_STR(WINED3DFMT_R32_FLOAT);
4689 FMT_TO_STR(WINED3DFMT_R32_UINT);
4690 FMT_TO_STR(WINED3DFMT_R32_SINT);
4691 FMT_TO_STR(WINED3DFMT_R24G8_TYPELESS);
4692 FMT_TO_STR(WINED3DFMT_D24_UNORM_S8_UINT);
4693 FMT_TO_STR(WINED3DFMT_R24_UNORM_X8_TYPELESS);
4694 FMT_TO_STR(WINED3DFMT_X24_TYPELESS_G8_UINT);
4695 FMT_TO_STR(WINED3DFMT_R8G8_TYPELESS);
4696 FMT_TO_STR(WINED3DFMT_R8G8_UNORM);
4697 FMT_TO_STR(WINED3DFMT_R8G8_UINT);
4698 FMT_TO_STR(WINED3DFMT_R8G8_SNORM);
4699 FMT_TO_STR(WINED3DFMT_R8G8_SINT);
4700 FMT_TO_STR(WINED3DFMT_R16_TYPELESS);
4701 FMT_TO_STR(WINED3DFMT_R16_FLOAT);
4702 FMT_TO_STR(WINED3DFMT_D16_UNORM);
4703 FMT_TO_STR(WINED3DFMT_R16_UNORM);
4704 FMT_TO_STR(WINED3DFMT_R16_UINT);
4705 FMT_TO_STR(WINED3DFMT_R16_SNORM);
4706 FMT_TO_STR(WINED3DFMT_R16_SINT);
4707 FMT_TO_STR(WINED3DFMT_R8_TYPELESS);
4708 FMT_TO_STR(WINED3DFMT_R8_UNORM);
4709 FMT_TO_STR(WINED3DFMT_R8_UINT);
4710 FMT_TO_STR(WINED3DFMT_R8_SNORM);
4711 FMT_TO_STR(WINED3DFMT_R8_SINT);
4712 FMT_TO_STR(WINED3DFMT_A8_UNORM);
4713 FMT_TO_STR(WINED3DFMT_R1_UNORM);
4714 FMT_TO_STR(WINED3DFMT_R9G9B9E5_SHAREDEXP);
4715 FMT_TO_STR(WINED3DFMT_R8G8_B8G8_UNORM);
4716 FMT_TO_STR(WINED3DFMT_G8R8_G8B8_UNORM);
4717 FMT_TO_STR(WINED3DFMT_BC1_TYPELESS);
4718 FMT_TO_STR(WINED3DFMT_BC1_UNORM);
4719 FMT_TO_STR(WINED3DFMT_BC1_UNORM_SRGB);
4720 FMT_TO_STR(WINED3DFMT_BC2_TYPELESS);
4721 FMT_TO_STR(WINED3DFMT_BC2_UNORM);
4722 FMT_TO_STR(WINED3DFMT_BC2_UNORM_SRGB);
4723 FMT_TO_STR(WINED3DFMT_BC3_TYPELESS);
4724 FMT_TO_STR(WINED3DFMT_BC3_UNORM);
4725 FMT_TO_STR(WINED3DFMT_BC3_UNORM_SRGB);
4726 FMT_TO_STR(WINED3DFMT_BC4_TYPELESS);
4727 FMT_TO_STR(WINED3DFMT_BC4_UNORM);
4728 FMT_TO_STR(WINED3DFMT_BC4_SNORM);
4729 FMT_TO_STR(WINED3DFMT_BC5_TYPELESS);
4730 FMT_TO_STR(WINED3DFMT_BC5_UNORM);
4731 FMT_TO_STR(WINED3DFMT_BC5_SNORM);
4732 FMT_TO_STR(WINED3DFMT_B5G6R5_UNORM);
4733 FMT_TO_STR(WINED3DFMT_B5G5R5A1_UNORM);
4734 FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM);
4735 FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM);
4736 FMT_TO_STR(WINED3DFMT_B8G8R8A8_TYPELESS);
4737 FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM_SRGB);
4738 FMT_TO_STR(WINED3DFMT_B8G8R8X8_TYPELESS);
4739 FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM_SRGB);
4740 FMT_TO_STR(WINED3DFMT_BC6H_TYPELESS);
4741 FMT_TO_STR(WINED3DFMT_BC6H_UF16);
4742 FMT_TO_STR(WINED3DFMT_BC6H_SF16);
4743 FMT_TO_STR(WINED3DFMT_BC7_TYPELESS);
4744 FMT_TO_STR(WINED3DFMT_BC7_UNORM);
4745 FMT_TO_STR(WINED3DFMT_BC7_UNORM_SRGB);
4746 FMT_TO_STR(WINED3DFMT_UYVY);
4747 FMT_TO_STR(WINED3DFMT_YUY2);
4748 FMT_TO_STR(WINED3DFMT_YV12);
4749 FMT_TO_STR(WINED3DFMT_DXT1);
4750 FMT_TO_STR(WINED3DFMT_DXT2);
4751 FMT_TO_STR(WINED3DFMT_DXT3);
4752 FMT_TO_STR(WINED3DFMT_DXT4);
4753 FMT_TO_STR(WINED3DFMT_DXT5);
4754 FMT_TO_STR(WINED3DFMT_MULTI2_ARGB8);
4755 FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
4756 FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
4757 FMT_TO_STR(WINED3DFMT_ATI1N);
4758 FMT_TO_STR(WINED3DFMT_ATI2N);
4759 FMT_TO_STR(WINED3DFMT_INST);
4760 FMT_TO_STR(WINED3DFMT_NVDB);
4761 FMT_TO_STR(WINED3DFMT_NVHU);
4762 FMT_TO_STR(WINED3DFMT_NVHS);
4763 FMT_TO_STR(WINED3DFMT_INTZ);
4764 FMT_TO_STR(WINED3DFMT_RESZ);
4765 FMT_TO_STR(WINED3DFMT_NULL);
4766 FMT_TO_STR(WINED3DFMT_R16);
4767 FMT_TO_STR(WINED3DFMT_AL16);
4768 FMT_TO_STR(WINED3DFMT_NV12);
4769 FMT_TO_STR(WINED3DFMT_ATOC);
4770 #undef FMT_TO_STR
4771 default:
4773 char fourcc[5];
4774 fourcc[0] = (char)(format_id);
4775 fourcc[1] = (char)(format_id >> 8);
4776 fourcc[2] = (char)(format_id >> 16);
4777 fourcc[3] = (char)(format_id >> 24);
4778 fourcc[4] = 0;
4779 if (isprint(fourcc[0]) && isprint(fourcc[1]) && isprint(fourcc[2]) && isprint(fourcc[3]))
4780 FIXME("Unrecognized %#x (as fourcc: %s) WINED3DFORMAT!\n", format_id, fourcc);
4781 else
4782 FIXME("Unrecognized %#x WINED3DFORMAT!\n", format_id);
4784 return "unrecognized";
4788 const char *debug_d3ddevicetype(enum wined3d_device_type device_type)
4790 switch (device_type)
4792 #define DEVTYPE_TO_STR(dev) case dev: return #dev
4793 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_HAL);
4794 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_REF);
4795 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_SW);
4796 #undef DEVTYPE_TO_STR
4797 default:
4798 FIXME("Unrecognized device type %#x.\n", device_type);
4799 return "unrecognized";
4803 struct debug_buffer
4805 char str[200]; /* wine_dbg_sprintf() limits string size to 200 */
4806 char *ptr;
4807 int size;
4810 static void init_debug_buffer(struct debug_buffer *buffer, const char *default_string)
4812 snprintf(buffer->str, sizeof(buffer->str), "%s", default_string);
4813 buffer->ptr = buffer->str;
4814 buffer->size = sizeof(buffer->str);
4817 static void debug_append(struct debug_buffer *buffer, const char *str, const char *separator)
4819 int size;
4821 if (!separator || buffer->ptr == buffer->str)
4822 separator = "";
4823 size = snprintf(buffer->ptr, buffer->size, "%s%s", separator, str);
4824 if (size == -1 || size >= buffer->size)
4826 buffer->size = 0;
4827 strcpy(&buffer->str[sizeof(buffer->str) - 4], "...");
4828 return;
4831 buffer->ptr += size;
4832 buffer->size -= size;
4835 const char *wined3d_debug_resource_access(uint32_t access)
4837 struct debug_buffer buffer;
4839 init_debug_buffer(&buffer, "0");
4840 #define ACCESS_TO_STR(x) if (access & x) { debug_append(&buffer, #x, " | "); access &= ~x; }
4841 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_GPU);
4842 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_CPU);
4843 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_R);
4844 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_W);
4845 #undef ACCESS_TO_STR
4846 if (access)
4847 FIXME("Unrecognised access flag(s) %#x.\n", access);
4849 return wine_dbg_sprintf("%s", buffer.str);
4852 const char *wined3d_debug_bind_flags(uint32_t bind_flags)
4854 struct debug_buffer buffer;
4856 init_debug_buffer(&buffer, "0");
4857 #define BIND_FLAG_TO_STR(x) if (bind_flags & x) { debug_append(&buffer, #x, " | "); bind_flags &= ~x; }
4858 BIND_FLAG_TO_STR(WINED3D_BIND_VERTEX_BUFFER);
4859 BIND_FLAG_TO_STR(WINED3D_BIND_INDEX_BUFFER);
4860 BIND_FLAG_TO_STR(WINED3D_BIND_CONSTANT_BUFFER);
4861 BIND_FLAG_TO_STR(WINED3D_BIND_SHADER_RESOURCE);
4862 BIND_FLAG_TO_STR(WINED3D_BIND_STREAM_OUTPUT);
4863 BIND_FLAG_TO_STR(WINED3D_BIND_RENDER_TARGET);
4864 BIND_FLAG_TO_STR(WINED3D_BIND_DEPTH_STENCIL);
4865 BIND_FLAG_TO_STR(WINED3D_BIND_UNORDERED_ACCESS);
4866 BIND_FLAG_TO_STR(WINED3D_BIND_INDIRECT_BUFFER);
4867 #undef BIND_FLAG_TO_STR
4868 if (bind_flags)
4869 FIXME("Unrecognised bind flag(s) %#x.\n", bind_flags);
4871 return wine_dbg_sprintf("%s", buffer.str);
4874 const char *wined3d_debug_view_desc(const struct wined3d_view_desc *d, const struct wined3d_resource *resource)
4876 struct debug_buffer buffer;
4877 unsigned int flags = d->flags;
4879 init_debug_buffer(&buffer, "0");
4880 #define VIEW_FLAG_TO_STR(x) if (flags & x) { debug_append(&buffer, #x, " | "); flags &= ~x; }
4881 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_RAW);
4882 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_APPEND);
4883 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_COUNTER);
4884 VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_CUBE);
4885 VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_ARRAY);
4886 VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_DEPTH);
4887 VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_STENCIL);
4888 VIEW_FLAG_TO_STR(WINED3D_VIEW_FORWARD_REFERENCE);
4889 #undef VIEW_FLAG_TO_STR
4890 if (flags)
4891 FIXME("Unrecognised view flag(s) %#x.\n", flags);
4893 if (resource->type == WINED3D_RTYPE_BUFFER)
4894 return wine_dbg_sprintf("format %s, flags %s, start_idx %u, count %u",
4895 debug_d3dformat(d->format_id), buffer.str, d->u.buffer.start_idx, d->u.buffer.count);
4896 else
4897 return wine_dbg_sprintf("format %s, flags %s, level_idx %u, level_count %u, layer_idx %u, layer_count %u",
4898 debug_d3dformat(d->format_id), buffer.str, d->u.texture.level_idx, d->u.texture.level_count,
4899 d->u.texture.layer_idx, d->u.texture.layer_count);
4902 const char *debug_d3dusage(uint32_t usage)
4904 struct debug_buffer buffer;
4906 init_debug_buffer(&buffer, "0");
4907 #define WINED3DUSAGE_TO_STR(x) if (usage & x) { debug_append(&buffer, #x, " | "); usage &= ~x; }
4908 WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
4909 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
4910 WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
4911 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
4912 WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
4913 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
4914 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICTED_CONTENT);
4915 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE_DRIVER);
4916 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE);
4917 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DMAP);
4918 WINED3DUSAGE_TO_STR(WINED3DUSAGE_TEXTAPI);
4919 WINED3DUSAGE_TO_STR(WINED3DUSAGE_LEGACY_CUBEMAP);
4920 WINED3DUSAGE_TO_STR(WINED3DUSAGE_OWNDC);
4921 WINED3DUSAGE_TO_STR(WINED3DUSAGE_STATICDECL);
4922 WINED3DUSAGE_TO_STR(WINED3DUSAGE_OVERLAY);
4923 WINED3DUSAGE_TO_STR(WINED3DUSAGE_MANAGED);
4924 WINED3DUSAGE_TO_STR(WINED3DUSAGE_VIDMEM_ACCOUNTING);
4925 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_FILTER);
4926 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_GENMIPMAP);
4927 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_LEGACYBUMPMAP);
4928 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING);
4929 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBREAD);
4930 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBWRITE);
4931 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_VERTEXTEXTURE);
4932 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_WRAPANDMIP);
4933 #undef WINED3DUSAGE_TO_STR
4934 if (usage)
4935 FIXME("Unrecognized usage flag(s) %#x.\n", usage);
4937 return wine_dbg_sprintf("%s", buffer.str);
4940 const char *debug_d3ddeclmethod(enum wined3d_decl_method method)
4942 switch (method)
4944 #define WINED3DDECLMETHOD_TO_STR(u) case u: return #u
4945 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_DEFAULT);
4946 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_U);
4947 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_V);
4948 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_CROSS_UV);
4949 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_UV);
4950 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP);
4951 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP_PRESAMPLED);
4952 #undef WINED3DDECLMETHOD_TO_STR
4953 default:
4954 FIXME("Unrecognized declaration method %#x.\n", method);
4955 return "unrecognized";
4959 const char *debug_d3ddeclusage(enum wined3d_decl_usage usage)
4961 switch (usage)
4963 #define WINED3DDECLUSAGE_TO_STR(u) case u: return #u
4964 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITION);
4965 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_WEIGHT);
4966 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_INDICES);
4967 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_NORMAL);
4968 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_PSIZE);
4969 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TEXCOORD);
4970 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TANGENT);
4971 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BINORMAL);
4972 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TESS_FACTOR);
4973 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITIONT);
4974 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_COLOR);
4975 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_FOG);
4976 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_DEPTH);
4977 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_SAMPLE);
4978 #undef WINED3DDECLUSAGE_TO_STR
4979 default:
4980 FIXME("Unrecognized %u declaration usage!\n", usage);
4981 return "unrecognized";
4985 const char *debug_d3dinput_classification(enum wined3d_input_classification classification)
4987 switch (classification)
4989 #define WINED3D_TO_STR(x) case x: return #x
4990 WINED3D_TO_STR(WINED3D_INPUT_PER_VERTEX_DATA);
4991 WINED3D_TO_STR(WINED3D_INPUT_PER_INSTANCE_DATA);
4992 #undef WINED3D_TO_STR
4993 default:
4994 FIXME("Unrecognized input classification %#x.\n", classification);
4995 return "unrecognized";
4999 const char *debug_d3dresourcetype(enum wined3d_resource_type resource_type)
5001 switch (resource_type)
5003 #define WINED3D_TO_STR(x) case x: return #x
5004 WINED3D_TO_STR(WINED3D_RTYPE_NONE);
5005 WINED3D_TO_STR(WINED3D_RTYPE_BUFFER);
5006 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_1D);
5007 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_2D);
5008 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_3D);
5009 #undef WINED3D_TO_STR
5010 default:
5011 FIXME("Unrecognized resource type %#x.\n", resource_type);
5012 return "unrecognized";
5016 const char *debug_d3dprimitivetype(enum wined3d_primitive_type primitive_type)
5018 switch (primitive_type)
5020 #define PRIM_TO_STR(prim) case prim: return #prim
5021 PRIM_TO_STR(WINED3D_PT_UNDEFINED);
5022 PRIM_TO_STR(WINED3D_PT_POINTLIST);
5023 PRIM_TO_STR(WINED3D_PT_LINELIST);
5024 PRIM_TO_STR(WINED3D_PT_LINESTRIP);
5025 PRIM_TO_STR(WINED3D_PT_TRIANGLELIST);
5026 PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP);
5027 PRIM_TO_STR(WINED3D_PT_TRIANGLEFAN);
5028 PRIM_TO_STR(WINED3D_PT_LINELIST_ADJ);
5029 PRIM_TO_STR(WINED3D_PT_LINESTRIP_ADJ);
5030 PRIM_TO_STR(WINED3D_PT_TRIANGLELIST_ADJ);
5031 PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP_ADJ);
5032 PRIM_TO_STR(WINED3D_PT_PATCH);
5033 #undef PRIM_TO_STR
5034 default:
5035 FIXME("Unrecognized primitive type %#x.\n", primitive_type);
5036 return "unrecognized";
5040 const char *debug_d3drenderstate(enum wined3d_render_state state)
5042 switch (state)
5044 #define D3DSTATE_TO_STR(u) case u: return #u
5045 D3DSTATE_TO_STR(WINED3D_RS_ANTIALIAS);
5046 D3DSTATE_TO_STR(WINED3D_RS_TEXTUREPERSPECTIVE);
5047 D3DSTATE_TO_STR(WINED3D_RS_WRAPU);
5048 D3DSTATE_TO_STR(WINED3D_RS_WRAPV);
5049 D3DSTATE_TO_STR(WINED3D_RS_ZENABLE);
5050 D3DSTATE_TO_STR(WINED3D_RS_FILLMODE);
5051 D3DSTATE_TO_STR(WINED3D_RS_SHADEMODE);
5052 D3DSTATE_TO_STR(WINED3D_RS_LINEPATTERN);
5053 D3DSTATE_TO_STR(WINED3D_RS_MONOENABLE);
5054 D3DSTATE_TO_STR(WINED3D_RS_ROP2);
5055 D3DSTATE_TO_STR(WINED3D_RS_PLANEMASK);
5056 D3DSTATE_TO_STR(WINED3D_RS_ZWRITEENABLE);
5057 D3DSTATE_TO_STR(WINED3D_RS_ALPHATESTENABLE);
5058 D3DSTATE_TO_STR(WINED3D_RS_LASTPIXEL);
5059 D3DSTATE_TO_STR(WINED3D_RS_SRCBLEND);
5060 D3DSTATE_TO_STR(WINED3D_RS_DESTBLEND);
5061 D3DSTATE_TO_STR(WINED3D_RS_CULLMODE);
5062 D3DSTATE_TO_STR(WINED3D_RS_ZFUNC);
5063 D3DSTATE_TO_STR(WINED3D_RS_ALPHAREF);
5064 D3DSTATE_TO_STR(WINED3D_RS_ALPHAFUNC);
5065 D3DSTATE_TO_STR(WINED3D_RS_DITHERENABLE);
5066 D3DSTATE_TO_STR(WINED3D_RS_ALPHABLENDENABLE);
5067 D3DSTATE_TO_STR(WINED3D_RS_FOGENABLE);
5068 D3DSTATE_TO_STR(WINED3D_RS_SPECULARENABLE);
5069 D3DSTATE_TO_STR(WINED3D_RS_ZVISIBLE);
5070 D3DSTATE_TO_STR(WINED3D_RS_SUBPIXEL);
5071 D3DSTATE_TO_STR(WINED3D_RS_SUBPIXELX);
5072 D3DSTATE_TO_STR(WINED3D_RS_STIPPLEDALPHA);
5073 D3DSTATE_TO_STR(WINED3D_RS_FOGCOLOR);
5074 D3DSTATE_TO_STR(WINED3D_RS_FOGTABLEMODE);
5075 D3DSTATE_TO_STR(WINED3D_RS_FOGSTART);
5076 D3DSTATE_TO_STR(WINED3D_RS_FOGEND);
5077 D3DSTATE_TO_STR(WINED3D_RS_FOGDENSITY);
5078 D3DSTATE_TO_STR(WINED3D_RS_STIPPLEENABLE);
5079 D3DSTATE_TO_STR(WINED3D_RS_COLORKEYENABLE);
5080 D3DSTATE_TO_STR(WINED3D_RS_MIPMAPLODBIAS);
5081 D3DSTATE_TO_STR(WINED3D_RS_RANGEFOGENABLE);
5082 D3DSTATE_TO_STR(WINED3D_RS_ANISOTROPY);
5083 D3DSTATE_TO_STR(WINED3D_RS_FLUSHBATCH);
5084 D3DSTATE_TO_STR(WINED3D_RS_TRANSLUCENTSORTINDEPENDENT);
5085 D3DSTATE_TO_STR(WINED3D_RS_STENCILENABLE);
5086 D3DSTATE_TO_STR(WINED3D_RS_STENCILFAIL);
5087 D3DSTATE_TO_STR(WINED3D_RS_STENCILZFAIL);
5088 D3DSTATE_TO_STR(WINED3D_RS_STENCILPASS);
5089 D3DSTATE_TO_STR(WINED3D_RS_STENCILFUNC);
5090 D3DSTATE_TO_STR(WINED3D_RS_STENCILREF);
5091 D3DSTATE_TO_STR(WINED3D_RS_STENCILMASK);
5092 D3DSTATE_TO_STR(WINED3D_RS_STENCILWRITEMASK);
5093 D3DSTATE_TO_STR(WINED3D_RS_TEXTUREFACTOR);
5094 D3DSTATE_TO_STR(WINED3D_RS_WRAP0);
5095 D3DSTATE_TO_STR(WINED3D_RS_WRAP1);
5096 D3DSTATE_TO_STR(WINED3D_RS_WRAP2);
5097 D3DSTATE_TO_STR(WINED3D_RS_WRAP3);
5098 D3DSTATE_TO_STR(WINED3D_RS_WRAP4);
5099 D3DSTATE_TO_STR(WINED3D_RS_WRAP5);
5100 D3DSTATE_TO_STR(WINED3D_RS_WRAP6);
5101 D3DSTATE_TO_STR(WINED3D_RS_WRAP7);
5102 D3DSTATE_TO_STR(WINED3D_RS_CLIPPING);
5103 D3DSTATE_TO_STR(WINED3D_RS_LIGHTING);
5104 D3DSTATE_TO_STR(WINED3D_RS_EXTENTS);
5105 D3DSTATE_TO_STR(WINED3D_RS_AMBIENT);
5106 D3DSTATE_TO_STR(WINED3D_RS_FOGVERTEXMODE);
5107 D3DSTATE_TO_STR(WINED3D_RS_COLORVERTEX);
5108 D3DSTATE_TO_STR(WINED3D_RS_LOCALVIEWER);
5109 D3DSTATE_TO_STR(WINED3D_RS_NORMALIZENORMALS);
5110 D3DSTATE_TO_STR(WINED3D_RS_COLORKEYBLENDENABLE);
5111 D3DSTATE_TO_STR(WINED3D_RS_DIFFUSEMATERIALSOURCE);
5112 D3DSTATE_TO_STR(WINED3D_RS_SPECULARMATERIALSOURCE);
5113 D3DSTATE_TO_STR(WINED3D_RS_AMBIENTMATERIALSOURCE);
5114 D3DSTATE_TO_STR(WINED3D_RS_EMISSIVEMATERIALSOURCE);
5115 D3DSTATE_TO_STR(WINED3D_RS_VERTEXBLEND);
5116 D3DSTATE_TO_STR(WINED3D_RS_CLIPPLANEENABLE);
5117 D3DSTATE_TO_STR(WINED3D_RS_SOFTWAREVERTEXPROCESSING);
5118 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE);
5119 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MIN);
5120 D3DSTATE_TO_STR(WINED3D_RS_POINTSPRITEENABLE);
5121 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALEENABLE);
5122 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_A);
5123 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_B);
5124 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_C);
5125 D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEANTIALIAS);
5126 D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEMASK);
5127 D3DSTATE_TO_STR(WINED3D_RS_PATCHEDGESTYLE);
5128 D3DSTATE_TO_STR(WINED3D_RS_PATCHSEGMENTS);
5129 D3DSTATE_TO_STR(WINED3D_RS_DEBUGMONITORTOKEN);
5130 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MAX);
5131 D3DSTATE_TO_STR(WINED3D_RS_INDEXEDVERTEXBLENDENABLE);
5132 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE);
5133 D3DSTATE_TO_STR(WINED3D_RS_TWEENFACTOR);
5134 D3DSTATE_TO_STR(WINED3D_RS_BLENDOP);
5135 D3DSTATE_TO_STR(WINED3D_RS_POSITIONDEGREE);
5136 D3DSTATE_TO_STR(WINED3D_RS_NORMALDEGREE);
5137 D3DSTATE_TO_STR(WINED3D_RS_SCISSORTESTENABLE);
5138 D3DSTATE_TO_STR(WINED3D_RS_SLOPESCALEDEPTHBIAS);
5139 D3DSTATE_TO_STR(WINED3D_RS_ANTIALIASEDLINEENABLE);
5140 D3DSTATE_TO_STR(WINED3D_RS_MINTESSELLATIONLEVEL);
5141 D3DSTATE_TO_STR(WINED3D_RS_MAXTESSELLATIONLEVEL);
5142 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_X);
5143 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Y);
5144 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Z);
5145 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_W);
5146 D3DSTATE_TO_STR(WINED3D_RS_ENABLEADAPTIVETESSELLATION);
5147 D3DSTATE_TO_STR(WINED3D_RS_TWOSIDEDSTENCILMODE);
5148 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFAIL);
5149 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILZFAIL);
5150 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILPASS);
5151 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFUNC);
5152 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE1);
5153 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE2);
5154 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE3);
5155 D3DSTATE_TO_STR(WINED3D_RS_SRGBWRITEENABLE);
5156 D3DSTATE_TO_STR(WINED3D_RS_DEPTHBIAS);
5157 D3DSTATE_TO_STR(WINED3D_RS_WRAP8);
5158 D3DSTATE_TO_STR(WINED3D_RS_WRAP9);
5159 D3DSTATE_TO_STR(WINED3D_RS_WRAP10);
5160 D3DSTATE_TO_STR(WINED3D_RS_WRAP11);
5161 D3DSTATE_TO_STR(WINED3D_RS_WRAP12);
5162 D3DSTATE_TO_STR(WINED3D_RS_WRAP13);
5163 D3DSTATE_TO_STR(WINED3D_RS_WRAP14);
5164 D3DSTATE_TO_STR(WINED3D_RS_WRAP15);
5165 D3DSTATE_TO_STR(WINED3D_RS_SEPARATEALPHABLENDENABLE);
5166 D3DSTATE_TO_STR(WINED3D_RS_SRCBLENDALPHA);
5167 D3DSTATE_TO_STR(WINED3D_RS_DESTBLENDALPHA);
5168 D3DSTATE_TO_STR(WINED3D_RS_BLENDOPALPHA);
5169 #undef D3DSTATE_TO_STR
5170 default:
5171 FIXME("Unrecognized %u render state!\n", state);
5172 return "unrecognized";
5176 const char *debug_d3dsamplerstate(enum wined3d_sampler_state state)
5178 switch (state)
5180 #define D3DSTATE_TO_STR(u) case u: return #u
5181 D3DSTATE_TO_STR(WINED3D_SAMP_BORDER_COLOR);
5182 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_U);
5183 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_V);
5184 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_W);
5185 D3DSTATE_TO_STR(WINED3D_SAMP_MAG_FILTER);
5186 D3DSTATE_TO_STR(WINED3D_SAMP_MIN_FILTER);
5187 D3DSTATE_TO_STR(WINED3D_SAMP_MIP_FILTER);
5188 D3DSTATE_TO_STR(WINED3D_SAMP_MIPMAP_LOD_BIAS);
5189 D3DSTATE_TO_STR(WINED3D_SAMP_MAX_MIP_LEVEL);
5190 D3DSTATE_TO_STR(WINED3D_SAMP_MAX_ANISOTROPY);
5191 D3DSTATE_TO_STR(WINED3D_SAMP_SRGB_TEXTURE);
5192 D3DSTATE_TO_STR(WINED3D_SAMP_ELEMENT_INDEX);
5193 D3DSTATE_TO_STR(WINED3D_SAMP_DMAP_OFFSET);
5194 #undef D3DSTATE_TO_STR
5195 default:
5196 FIXME("Unrecognized %u sampler state!\n", state);
5197 return "unrecognized";
5201 const char *debug_d3dtexturefiltertype(enum wined3d_texture_filter_type filter_type)
5203 switch (filter_type)
5205 #define D3DTEXTUREFILTERTYPE_TO_STR(u) case u: return #u
5206 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_NONE);
5207 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_POINT);
5208 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_LINEAR);
5209 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_ANISOTROPIC);
5210 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_FLAT_CUBIC);
5211 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_CUBIC);
5212 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_PYRAMIDAL_QUAD);
5213 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_QUAD);
5214 #undef D3DTEXTUREFILTERTYPE_TO_STR
5215 default:
5216 FIXME("Unrecognized texture filter type 0x%08x.\n", filter_type);
5217 return "unrecognized";
5221 const char *debug_d3dtexturestate(enum wined3d_texture_stage_state state)
5223 switch (state)
5225 #define D3DSTATE_TO_STR(u) case u: return #u
5226 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_OP);
5227 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG1);
5228 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG2);
5229 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_OP);
5230 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG1);
5231 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG2);
5232 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT00);
5233 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT01);
5234 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT10);
5235 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT11);
5236 D3DSTATE_TO_STR(WINED3D_TSS_TEXCOORD_INDEX);
5237 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LSCALE);
5238 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LOFFSET);
5239 D3DSTATE_TO_STR(WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS);
5240 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG0);
5241 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG0);
5242 D3DSTATE_TO_STR(WINED3D_TSS_RESULT_ARG);
5243 D3DSTATE_TO_STR(WINED3D_TSS_CONSTANT);
5244 #undef D3DSTATE_TO_STR
5245 default:
5246 FIXME("Unrecognized %u texture state!\n", state);
5247 return "unrecognized";
5251 const char *debug_d3dtop(enum wined3d_texture_op d3dtop)
5253 switch (d3dtop)
5255 #define D3DTOP_TO_STR(u) case u: return #u
5256 D3DTOP_TO_STR(WINED3D_TOP_DISABLE);
5257 D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG1);
5258 D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG2);
5259 D3DTOP_TO_STR(WINED3D_TOP_MODULATE);
5260 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_2X);
5261 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_4X);
5262 D3DTOP_TO_STR(WINED3D_TOP_ADD);
5263 D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED);
5264 D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED_2X);
5265 D3DTOP_TO_STR(WINED3D_TOP_SUBTRACT);
5266 D3DTOP_TO_STR(WINED3D_TOP_ADD_SMOOTH);
5267 D3DTOP_TO_STR(WINED3D_TOP_BLEND_DIFFUSE_ALPHA);
5268 D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA);
5269 D3DTOP_TO_STR(WINED3D_TOP_BLEND_FACTOR_ALPHA);
5270 D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM);
5271 D3DTOP_TO_STR(WINED3D_TOP_BLEND_CURRENT_ALPHA);
5272 D3DTOP_TO_STR(WINED3D_TOP_PREMODULATE);
5273 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR);
5274 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA);
5275 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR);
5276 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA);
5277 D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP);
5278 D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP_LUMINANCE);
5279 D3DTOP_TO_STR(WINED3D_TOP_DOTPRODUCT3);
5280 D3DTOP_TO_STR(WINED3D_TOP_MULTIPLY_ADD);
5281 D3DTOP_TO_STR(WINED3D_TOP_LERP);
5282 #undef D3DTOP_TO_STR
5283 default:
5284 FIXME("Unrecognized texture op %#x.\n", d3dtop);
5285 return "unrecognized";
5289 const char *debug_d3dtstype(enum wined3d_transform_state tstype)
5291 switch (tstype)
5293 #define TSTYPE_TO_STR(tstype) case tstype: return #tstype
5294 TSTYPE_TO_STR(WINED3D_TS_VIEW);
5295 TSTYPE_TO_STR(WINED3D_TS_PROJECTION);
5296 TSTYPE_TO_STR(WINED3D_TS_TEXTURE0);
5297 TSTYPE_TO_STR(WINED3D_TS_TEXTURE1);
5298 TSTYPE_TO_STR(WINED3D_TS_TEXTURE2);
5299 TSTYPE_TO_STR(WINED3D_TS_TEXTURE3);
5300 TSTYPE_TO_STR(WINED3D_TS_TEXTURE4);
5301 TSTYPE_TO_STR(WINED3D_TS_TEXTURE5);
5302 TSTYPE_TO_STR(WINED3D_TS_TEXTURE6);
5303 TSTYPE_TO_STR(WINED3D_TS_TEXTURE7);
5304 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(0));
5305 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(1));
5306 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(2));
5307 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(3));
5308 #undef TSTYPE_TO_STR
5309 default:
5310 if (tstype > 256 && tstype < 512)
5312 FIXME("WINED3D_TS_WORLD_MATRIX(%u). 1..255 not currently supported.\n", tstype);
5313 return ("WINED3D_TS_WORLD_MATRIX > 0");
5315 FIXME("Unrecognized transform state %#x.\n", tstype);
5316 return "unrecognized";
5320 const char *debug_shader_type(enum wined3d_shader_type type)
5322 switch(type)
5324 #define WINED3D_TO_STR(type) case type: return #type
5325 WINED3D_TO_STR(WINED3D_SHADER_TYPE_PIXEL);
5326 WINED3D_TO_STR(WINED3D_SHADER_TYPE_VERTEX);
5327 WINED3D_TO_STR(WINED3D_SHADER_TYPE_GEOMETRY);
5328 WINED3D_TO_STR(WINED3D_SHADER_TYPE_HULL);
5329 WINED3D_TO_STR(WINED3D_SHADER_TYPE_DOMAIN);
5330 WINED3D_TO_STR(WINED3D_SHADER_TYPE_COMPUTE);
5331 #undef WINED3D_TO_STR
5332 default:
5333 FIXME("Unrecognized shader type %#x.\n", type);
5334 return "unrecognized";
5338 const char *debug_d3dstate(uint32_t state)
5340 if (STATE_IS_RENDER(state))
5341 return wine_dbg_sprintf("STATE_RENDER(%s)", debug_d3drenderstate(state - STATE_RENDER(0)));
5342 if (STATE_IS_TEXTURESTAGE(state))
5344 unsigned int texture_stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5345 uint32_t texture_state = state - STATE_TEXTURESTAGE(texture_stage, 0);
5346 return wine_dbg_sprintf("STATE_TEXTURESTAGE(%#x, %s)",
5347 texture_stage, debug_d3dtexturestate(texture_state));
5349 if (STATE_IS_COMPUTE_SHADER(state))
5350 return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
5351 if (STATE_IS_GRAPHICS_SHADER(state))
5352 return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(state - STATE_SHADER(0)));
5353 if (STATE_IS_COMPUTE_CONSTANT_BUFFER(state))
5354 return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
5355 if (STATE_IS_GRAPHICS_CONSTANT_BUFFER(state))
5356 return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(state - STATE_CONSTANT_BUFFER(0)));
5357 if (STATE_IS_COMPUTE_SHADER_RESOURCE_BINDING(state))
5358 return "STATE_COMPUTE_SHADER_RESOURCE_BINDING";
5359 if (STATE_IS_GRAPHICS_SHADER_RESOURCE_BINDING(state))
5360 return "STATE_GRAPHICS_SHADER_RESOURCE_BINDING";
5361 if (STATE_IS_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING(state))
5362 return "STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING";
5363 if (STATE_IS_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING(state))
5364 return "STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING";
5365 if (STATE_IS_TRANSFORM(state))
5366 return wine_dbg_sprintf("STATE_TRANSFORM(%s)", debug_d3dtstype(state - STATE_TRANSFORM(0)));
5367 if (STATE_IS_STREAMSRC(state))
5368 return "STATE_STREAMSRC";
5369 if (STATE_IS_INDEXBUFFER(state))
5370 return "STATE_INDEXBUFFER";
5371 if (STATE_IS_VDECL(state))
5372 return "STATE_VDECL";
5373 if (STATE_IS_VIEWPORT(state))
5374 return "STATE_VIEWPORT";
5375 if (STATE_IS_LIGHT_TYPE(state))
5376 return "STATE_LIGHT_TYPE";
5377 if (STATE_IS_ACTIVELIGHT(state))
5378 return wine_dbg_sprintf("STATE_ACTIVELIGHT(%#x)", state - STATE_ACTIVELIGHT(0));
5379 if (STATE_IS_SCISSORRECT(state))
5380 return "STATE_SCISSORRECT";
5381 if (STATE_IS_CLIPPLANE(state))
5382 return wine_dbg_sprintf("STATE_CLIPPLANE(%#x)", state - STATE_CLIPPLANE(0));
5383 if (STATE_IS_MATERIAL(state))
5384 return "STATE_MATERIAL";
5385 if (STATE_IS_RASTERIZER(state))
5386 return "STATE_RASTERIZER";
5387 if (STATE_IS_DEPTH_BOUNDS(state))
5388 return "STATE_DEPTH_BOUNDS";
5389 if (STATE_IS_BASEVERTEXINDEX(state))
5390 return "STATE_BASEVERTEXINDEX";
5391 if (STATE_IS_FRAMEBUFFER(state))
5392 return "STATE_FRAMEBUFFER";
5393 if (STATE_IS_POINT_ENABLE(state))
5394 return "STATE_POINT_ENABLE";
5395 if (STATE_IS_COLOR_KEY(state))
5396 return "STATE_COLOR_KEY";
5397 if (STATE_IS_STREAM_OUTPUT(state))
5398 return "STATE_STREAM_OUTPUT";
5399 if (STATE_IS_BLEND(state))
5400 return "STATE_BLEND";
5401 if (STATE_IS_BLEND_FACTOR(state))
5402 return "STATE_BLEND_FACTOR";
5403 if (STATE_IS_SAMPLE_MASK(state))
5404 return "STATE_SAMPLE_MASK";
5405 if (STATE_IS_DEPTH_STENCIL(state))
5406 return "STATE_DEPTH_STENCIL";
5407 if (STATE_IS_STENCIL_REF(state))
5408 return "STATE_STENCIL_REF";
5410 return wine_dbg_sprintf("UNKNOWN_STATE(%#x)", state);
5413 const char *debug_fboattachment(GLenum attachment)
5415 switch(attachment)
5417 #define WINED3D_TO_STR(x) case x: return #x
5418 WINED3D_TO_STR(GL_COLOR_ATTACHMENT0);
5419 WINED3D_TO_STR(GL_COLOR_ATTACHMENT1);
5420 WINED3D_TO_STR(GL_COLOR_ATTACHMENT2);
5421 WINED3D_TO_STR(GL_COLOR_ATTACHMENT3);
5422 WINED3D_TO_STR(GL_COLOR_ATTACHMENT4);
5423 WINED3D_TO_STR(GL_COLOR_ATTACHMENT5);
5424 WINED3D_TO_STR(GL_COLOR_ATTACHMENT6);
5425 WINED3D_TO_STR(GL_COLOR_ATTACHMENT7);
5426 WINED3D_TO_STR(GL_COLOR_ATTACHMENT8);
5427 WINED3D_TO_STR(GL_COLOR_ATTACHMENT9);
5428 WINED3D_TO_STR(GL_COLOR_ATTACHMENT10);
5429 WINED3D_TO_STR(GL_COLOR_ATTACHMENT11);
5430 WINED3D_TO_STR(GL_COLOR_ATTACHMENT12);
5431 WINED3D_TO_STR(GL_COLOR_ATTACHMENT13);
5432 WINED3D_TO_STR(GL_COLOR_ATTACHMENT14);
5433 WINED3D_TO_STR(GL_COLOR_ATTACHMENT15);
5434 WINED3D_TO_STR(GL_DEPTH_ATTACHMENT);
5435 WINED3D_TO_STR(GL_STENCIL_ATTACHMENT);
5436 #undef WINED3D_TO_STR
5437 default:
5438 return wine_dbg_sprintf("Unknown FBO attachment %#x", attachment);
5442 const char *debug_fbostatus(GLenum status) {
5443 switch(status) {
5444 #define FBOSTATUS_TO_STR(u) case u: return #u
5445 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_COMPLETE);
5446 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
5447 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
5448 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT);
5449 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT);
5450 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
5451 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
5452 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE);
5453 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS);
5454 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB);
5455 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNSUPPORTED);
5456 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNDEFINED);
5457 #undef FBOSTATUS_TO_STR
5458 default:
5459 FIXME("Unrecognized FBO status 0x%08x.\n", status);
5460 return "unrecognized";
5464 const char *debug_glerror(GLenum error) {
5465 switch(error) {
5466 #define GLERROR_TO_STR(u) case u: return #u
5467 GLERROR_TO_STR(GL_NO_ERROR);
5468 GLERROR_TO_STR(GL_INVALID_ENUM);
5469 GLERROR_TO_STR(GL_INVALID_VALUE);
5470 GLERROR_TO_STR(GL_INVALID_OPERATION);
5471 GLERROR_TO_STR(GL_STACK_OVERFLOW);
5472 GLERROR_TO_STR(GL_STACK_UNDERFLOW);
5473 GLERROR_TO_STR(GL_OUT_OF_MEMORY);
5474 GLERROR_TO_STR(GL_INVALID_FRAMEBUFFER_OPERATION);
5475 #undef GLERROR_TO_STR
5476 default:
5477 FIXME("Unrecognized GL error 0x%08x.\n", error);
5478 return "unrecognized";
5482 const char *wined3d_debug_vkresult(VkResult vr)
5484 switch (vr)
5486 #define WINED3D_TO_STR(x) case x: return #x
5487 WINED3D_TO_STR(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR);
5488 WINED3D_TO_STR(VK_ERROR_NOT_PERMITTED_EXT);
5489 WINED3D_TO_STR(VK_ERROR_FRAGMENTATION_EXT);
5490 WINED3D_TO_STR(VK_ERROR_INVALID_EXTERNAL_HANDLE);
5491 WINED3D_TO_STR(VK_ERROR_OUT_OF_POOL_MEMORY);
5492 WINED3D_TO_STR(VK_ERROR_INVALID_SHADER_NV);
5493 WINED3D_TO_STR(VK_ERROR_OUT_OF_DATE_KHR);
5494 WINED3D_TO_STR(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
5495 WINED3D_TO_STR(VK_ERROR_SURFACE_LOST_KHR);
5496 WINED3D_TO_STR(VK_ERROR_FRAGMENTED_POOL);
5497 WINED3D_TO_STR(VK_ERROR_FORMAT_NOT_SUPPORTED);
5498 WINED3D_TO_STR(VK_ERROR_TOO_MANY_OBJECTS);
5499 WINED3D_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER);
5500 WINED3D_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT);
5501 WINED3D_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT);
5502 WINED3D_TO_STR(VK_ERROR_LAYER_NOT_PRESENT);
5503 WINED3D_TO_STR(VK_ERROR_MEMORY_MAP_FAILED);
5504 WINED3D_TO_STR(VK_ERROR_DEVICE_LOST);
5505 WINED3D_TO_STR(VK_ERROR_INITIALIZATION_FAILED);
5506 WINED3D_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
5507 WINED3D_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY);
5508 WINED3D_TO_STR(VK_SUCCESS);
5509 WINED3D_TO_STR(VK_NOT_READY);
5510 WINED3D_TO_STR(VK_TIMEOUT);
5511 WINED3D_TO_STR(VK_EVENT_SET);
5512 WINED3D_TO_STR(VK_EVENT_RESET);
5513 WINED3D_TO_STR(VK_INCOMPLETE);
5514 WINED3D_TO_STR(VK_SUBOPTIMAL_KHR);
5515 #undef WINED3D_TO_STR
5516 default:
5517 return wine_dbg_sprintf("unrecognised(%d)", vr);
5521 static const char *debug_fixup_channel_source(enum fixup_channel_source source)
5523 switch(source)
5525 #define WINED3D_TO_STR(x) case x: return #x
5526 WINED3D_TO_STR(CHANNEL_SOURCE_ZERO);
5527 WINED3D_TO_STR(CHANNEL_SOURCE_ONE);
5528 WINED3D_TO_STR(CHANNEL_SOURCE_X);
5529 WINED3D_TO_STR(CHANNEL_SOURCE_Y);
5530 WINED3D_TO_STR(CHANNEL_SOURCE_Z);
5531 WINED3D_TO_STR(CHANNEL_SOURCE_W);
5532 WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX0);
5533 WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX1);
5534 #undef WINED3D_TO_STR
5535 default:
5536 FIXME("Unrecognized fixup_channel_source %#x\n", source);
5537 return "unrecognized";
5541 static const char *debug_complex_fixup(enum complex_fixup fixup)
5543 switch(fixup)
5545 #define WINED3D_TO_STR(x) case x: return #x
5546 WINED3D_TO_STR(COMPLEX_FIXUP_YUY2);
5547 WINED3D_TO_STR(COMPLEX_FIXUP_UYVY);
5548 WINED3D_TO_STR(COMPLEX_FIXUP_YV12);
5549 WINED3D_TO_STR(COMPLEX_FIXUP_NV12);
5550 WINED3D_TO_STR(COMPLEX_FIXUP_P8);
5551 WINED3D_TO_STR(COMPLEX_FIXUP_YUV);
5552 #undef WINED3D_TO_STR
5553 default:
5554 FIXME("Unrecognized complex fixup %#x\n", fixup);
5555 return "unrecognized";
5559 void dump_color_fixup_desc(struct color_fixup_desc fixup)
5561 if (is_complex_fixup(fixup))
5563 TRACE("\tComplex: %s\n", debug_complex_fixup(get_complex_fixup(fixup)));
5564 return;
5567 TRACE("\tX: %s%s\n", debug_fixup_channel_source(fixup.x_source), fixup.x_sign_fixup ? ", SIGN_FIXUP" : "");
5568 TRACE("\tY: %s%s\n", debug_fixup_channel_source(fixup.y_source), fixup.y_sign_fixup ? ", SIGN_FIXUP" : "");
5569 TRACE("\tZ: %s%s\n", debug_fixup_channel_source(fixup.z_source), fixup.z_sign_fixup ? ", SIGN_FIXUP" : "");
5570 TRACE("\tW: %s%s\n", debug_fixup_channel_source(fixup.w_source), fixup.w_sign_fixup ? ", SIGN_FIXUP" : "");
5573 BOOL is_invalid_op(const struct wined3d_state *state, int stage,
5574 enum wined3d_texture_op op, DWORD arg1, DWORD arg2, DWORD arg3)
5576 if (op == WINED3D_TOP_DISABLE)
5577 return FALSE;
5578 if (wined3d_state_get_ffp_texture(state, stage))
5579 return FALSE;
5581 if ((arg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5582 && op != WINED3D_TOP_SELECT_ARG2)
5583 return TRUE;
5584 if ((arg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5585 && op != WINED3D_TOP_SELECT_ARG1)
5586 return TRUE;
5587 if ((arg3 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5588 && (op == WINED3D_TOP_MULTIPLY_ADD || op == WINED3D_TOP_LERP))
5589 return TRUE;
5591 return FALSE;
5594 void get_identity_matrix(struct wined3d_matrix *mat)
5596 static const struct wined3d_matrix identity =
5598 1.0f, 0.0f, 0.0f, 0.0f,
5599 0.0f, 1.0f, 0.0f, 0.0f,
5600 0.0f, 0.0f, 1.0f, 0.0f,
5601 0.0f, 0.0f, 0.0f, 1.0f,
5604 *mat = identity;
5607 void get_modelview_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5608 unsigned int index, struct wined3d_matrix *mat)
5610 if (context->stream_info.position_transformed)
5611 get_identity_matrix(mat);
5612 else
5613 multiply_matrix(mat, &state->transforms[WINED3D_TS_VIEW], &state->transforms[WINED3D_TS_WORLD_MATRIX(index)]);
5616 void get_projection_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5617 struct wined3d_matrix *mat)
5619 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
5620 BOOL clip_control, flip;
5621 float center_offset;
5623 /* There are a couple of additional things we have to take into account
5624 * here besides the projection transformation itself:
5625 * - We need to flip along the y-axis in case of offscreen rendering.
5626 * - OpenGL Z range is {-Wc,...,Wc} while D3D Z range is {0,...,Wc}.
5627 * - <= D3D9 coordinates refer to pixel centers while GL coordinates
5628 * refer to pixel corners. D3D10 fixed this particular oddity.
5629 * - D3D has a top-left filling convention while GL does not specify
5630 * a particular behavior, other than that that the GL implementation
5631 * needs to be consistent.
5633 * In order to handle the pixel center, we translate by 0.5 / VPw and
5634 * 0.5 / VPh. We test the filling convention during adapter init and
5635 * add a small offset to correct it if necessary. See
5636 * wined3d_caps_gl_ctx_test_filling_convention() for more details on how
5637 * we test GL and considerations regarding the added offset value.
5639 * If we have GL_ARB_clip_control we take care of all this through
5640 * viewport properties and don't have to translate geometry. */
5642 /* Projection matrices are <= d3d9, which all have integer pixel centers. */
5643 if (!(d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER))
5644 ERR("Did not expect to enter this codepath without WINED3D_PIXEL_CENTER_INTEGER.\n");
5646 clip_control = d3d_info->clip_control;
5647 flip = !clip_control;
5648 if (!clip_control)
5649 center_offset = 1.0f + d3d_info->filling_convention_offset;
5650 else
5651 center_offset = 0.0f;
5653 if (context->stream_info.position_transformed)
5655 /* Transform D3D RHW coordinates to OpenGL clip coordinates. */
5656 float x = state->viewports[0].x;
5657 float y = state->viewports[0].y;
5658 float w = state->viewports[0].width;
5659 float h = state->viewports[0].height;
5660 float x_scale = 2.0f / w;
5661 float x_offset = (center_offset - (2.0f * x) - w) / w;
5662 float y_scale = flip ? 2.0f / h : 2.0f / -h;
5663 float y_offset = flip
5664 ? (center_offset - (2.0f * y) - h) / h
5665 : (center_offset - (2.0f * y) - h) / -h;
5666 bool zenable = state->fb.depth_stencil ?
5667 (state->depth_stencil_state ? state->depth_stencil_state->desc.depth : true) : false;
5668 float z_scale = zenable ? clip_control ? 1.0f : 2.0f : 0.0f;
5669 float z_offset = zenable ? clip_control ? 0.0f : -1.0f : 0.0f;
5670 const struct wined3d_matrix projection =
5672 x_scale, 0.0f, 0.0f, 0.0f,
5673 0.0f, y_scale, 0.0f, 0.0f,
5674 0.0f, 0.0f, z_scale, 0.0f,
5675 x_offset, y_offset, z_offset, 1.0f,
5678 *mat = projection;
5680 else
5682 float y_scale = flip ? -1.0f : 1.0f;
5683 float x_offset = center_offset / state->viewports[0].width;
5684 float y_offset = flip
5685 ? center_offset / state->viewports[0].height
5686 : -center_offset / state->viewports[0].height;
5687 float z_scale = clip_control ? 1.0f : 2.0f;
5688 float z_offset = clip_control ? 0.0f : -1.0f;
5689 const struct wined3d_matrix projection =
5691 1.0f, 0.0f, 0.0f, 0.0f,
5692 0.0f, y_scale, 0.0f, 0.0f,
5693 0.0f, 0.0f, z_scale, 0.0f,
5694 x_offset, y_offset, z_offset, 1.0f,
5697 multiply_matrix(mat, &projection, &state->transforms[WINED3D_TS_PROJECTION]);
5701 /* Setup this textures matrix according to the texture flags. */
5702 static void compute_texture_matrix(const struct wined3d_matrix *matrix, uint32_t flags, BOOL calculated_coords,
5703 BOOL transformed, enum wined3d_format_id format_id, struct wined3d_matrix *out_matrix)
5705 struct wined3d_matrix mat;
5707 if (flags == WINED3D_TTFF_DISABLE || flags == WINED3D_TTFF_COUNT1 || transformed)
5709 get_identity_matrix(out_matrix);
5710 return;
5713 if (flags == (WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED))
5715 ERR("Invalid texture transform flags: WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED.\n");
5716 return;
5719 mat = *matrix;
5721 /* Under Direct3D the R/Z coord can be used for translation, under
5722 * OpenGL we use the Q coord instead. */
5723 if (!(flags & WINED3D_TTFF_PROJECTED) && !calculated_coords)
5725 switch (format_id)
5727 /* Direct3D passes the default 1.0 in the 2nd coord, while GL
5728 * passes it in the 4th. Swap 2nd and 4th coord. No need to
5729 * store the value of mat._41 in mat._21 because the input
5730 * value to the transformation will be 0, so the matrix value
5731 * is irrelevant. */
5732 case WINED3DFMT_R32_FLOAT:
5733 mat._41 = mat._21;
5734 mat._42 = mat._22;
5735 mat._43 = mat._23;
5736 mat._44 = mat._24;
5737 break;
5738 /* See above, just 3rd and 4th coord. */
5739 case WINED3DFMT_R32G32_FLOAT:
5740 mat._41 = mat._31;
5741 mat._42 = mat._32;
5742 mat._43 = mat._33;
5743 mat._44 = mat._34;
5744 break;
5745 case WINED3DFMT_R32G32B32_FLOAT: /* Opengl defaults match dx defaults */
5746 case WINED3DFMT_R32G32B32A32_FLOAT: /* No defaults apply, all app defined */
5748 /* This is to prevent swapping the matrix lines and put the default 4th coord = 1.0
5749 * into a bad place. The division elimination below will apply to make sure the
5750 * 1.0 doesn't do anything bad. The caller will set this value if the stride is 0
5752 case WINED3DFMT_UNKNOWN: /* No texture coords, 0/0/0/1 defaults are passed */
5753 break;
5754 default:
5755 FIXME("Unexpected fixed function texture coord input\n");
5759 *out_matrix = mat;
5762 void get_texture_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5763 const unsigned int tex, struct wined3d_matrix *mat)
5765 BOOL generated = (state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX] & 0xffff0000)
5766 != WINED3DTSS_TCI_PASSTHRU;
5767 unsigned int coord_idx = min(state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX] & 0x0000ffff,
5768 WINED3D_MAX_FFP_TEXTURES - 1);
5770 compute_texture_matrix(&state->transforms[WINED3D_TS_TEXTURE0 + tex],
5771 state->texture_states[tex][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS],
5772 generated, context->stream_info.position_transformed,
5773 context->stream_info.use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))
5774 ? context->stream_info.elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->id
5775 : WINED3DFMT_UNKNOWN, mat);
5778 void get_pointsize_minmax(const struct wined3d_context *context, const struct wined3d_state *state,
5779 float *out_min, float *out_max)
5781 union
5783 DWORD d;
5784 float f;
5785 } min, max;
5787 min.d = state->render_states[WINED3D_RS_POINTSIZE_MIN];
5788 max.d = state->render_states[WINED3D_RS_POINTSIZE_MAX];
5790 if (min.f > max.f)
5791 min.f = max.f;
5793 *out_min = min.f;
5794 *out_max = max.f;
5797 void get_pointsize(const struct wined3d_context *context, const struct wined3d_state *state,
5798 float *out_pointsize, float *out_att)
5800 /* POINTSCALEENABLE controls how point size value is treated. If set to
5801 * true, the point size is scaled with respect to height of viewport.
5802 * When set to false point size is in pixels. */
5803 union
5805 DWORD d;
5806 float f;
5807 } pointsize, a, b, c;
5809 out_att[0] = 1.0f;
5810 out_att[1] = 0.0f;
5811 out_att[2] = 0.0f;
5813 pointsize.d = state->render_states[WINED3D_RS_POINTSIZE];
5814 a.d = state->render_states[WINED3D_RS_POINTSCALE_A];
5815 b.d = state->render_states[WINED3D_RS_POINTSCALE_B];
5816 c.d = state->render_states[WINED3D_RS_POINTSCALE_C];
5818 /* Always use first viewport, this path does not apply to d3d10/11 multiple viewports case. */
5819 if (state->render_states[WINED3D_RS_POINTSCALEENABLE])
5821 float scale_factor = state->viewports[0].height * state->viewports[0].height;
5823 out_att[0] = a.f / scale_factor;
5824 out_att[1] = b.f / scale_factor;
5825 out_att[2] = c.f / scale_factor;
5827 *out_pointsize = pointsize.f;
5830 void get_fog_start_end(const struct wined3d_context *context, const struct wined3d_state *state,
5831 float *start, float *end)
5833 union
5835 DWORD d;
5836 float f;
5837 } tmpvalue;
5839 switch (context->fog_source)
5841 case FOGSOURCE_VS:
5842 *start = 1.0f;
5843 *end = 0.0f;
5844 break;
5846 case FOGSOURCE_COORD:
5847 *start = 255.0f;
5848 *end = 0.0f;
5849 break;
5851 case FOGSOURCE_FFP:
5852 tmpvalue.d = state->render_states[WINED3D_RS_FOGSTART];
5853 *start = tmpvalue.f;
5854 tmpvalue.d = state->render_states[WINED3D_RS_FOGEND];
5855 *end = tmpvalue.f;
5856 /* Special handling for fog_start == fog_end. In d3d with vertex
5857 * fog, everything is fogged. With table fog, everything with
5858 * fog_coord < fog_start is unfogged, and fog_coord > fog_start
5859 * is fogged. Windows drivers disagree when fog_coord == fog_start. */
5860 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE && *start == *end)
5862 *start = -INFINITY;
5863 *end = 0.0f;
5865 break;
5867 default:
5868 /* This should not happen, context->fog_source is set in wined3d, not the app. */
5869 ERR("Unexpected fog coordinate source.\n");
5870 *start = 0.0f;
5871 *end = 0.0f;
5875 static BOOL wined3d_get_primary_display(WCHAR *display)
5877 DISPLAY_DEVICEW display_device;
5878 DWORD device_idx = 0;
5880 display_device.cb = sizeof(display_device);
5881 while (EnumDisplayDevicesW(NULL, device_idx++, &display_device, 0))
5883 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
5885 lstrcpyW(display, display_device.DeviceName);
5886 return TRUE;
5890 return FALSE;
5893 BOOL wined3d_get_primary_adapter_luid(LUID *luid)
5895 D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME open_adapter_param;
5896 D3DKMT_CLOSEADAPTER close_adapter_param;
5898 if (!wined3d_get_primary_display(open_adapter_param.DeviceName))
5899 return FALSE;
5901 if (D3DKMTOpenAdapterFromGdiDisplayName(&open_adapter_param))
5902 return FALSE;
5904 *luid = open_adapter_param.AdapterLuid;
5905 close_adapter_param.hAdapter = open_adapter_param.hAdapter;
5906 D3DKMTCloseAdapter(&close_adapter_param);
5907 return TRUE;
5910 uint32_t wined3d_format_pack(const struct wined3d_format *format, const struct wined3d_uvec4 *value)
5912 uint32_t p = 0;
5914 p |= (value->x & wined3d_mask_from_size(format->red_size)) << format->red_offset;
5915 p |= (value->y & wined3d_mask_from_size(format->green_size)) << format->green_offset;
5916 p |= (value->z & wined3d_mask_from_size(format->blue_size)) << format->blue_offset;
5917 p |= (value->w & wined3d_mask_from_size(format->alpha_size)) << format->alpha_offset;
5919 return p;
5922 void wined3d_format_colour_to_vk(const struct wined3d_format *format, const struct wined3d_color *c,
5923 VkClearColorValue *retval)
5925 if (format->attrs & WINED3D_FORMAT_ATTR_INTEGER)
5927 retval->int32[0] = c->r;
5928 retval->int32[1] = c->g;
5929 retval->int32[2] = c->b;
5930 retval->int32[3] = c->a;
5932 else
5934 retval->float32[0] = c->r;
5935 retval->float32[1] = c->g;
5936 retval->float32[2] = c->b;
5937 retval->float32[3] = c->a;
5941 /* Note: It's the caller's responsibility to ensure values can be expressed
5942 * in the requested format. UNORM formats for example can only express values
5943 * in the range 0.0f -> 1.0f.
5945 * The code below assumes that no component crosses the 32 bit boundary (like
5946 * e.g. a hypothetical, and totally braindead, B30G30R4 format would).
5947 * This function writes at least sizeof(uint32_t) bytes, or format->byte_count,
5948 * whichever is larger. */
5949 void wined3d_format_convert_from_float(const struct wined3d_format *format,
5950 const struct wined3d_color *color, void *ret)
5952 static const struct
5954 enum wined3d_format_id format_id;
5955 struct wined3d_vec4 mul;
5956 struct wined3d_uvec4 shift;
5958 float_conv[] =
5960 {WINED3DFMT_P8_UINT, { 0.0f, 0.0f, 0.0f, 255.0f}, { 0, 0, 0, 0}},
5961 {WINED3DFMT_S1_UINT_D15_UNORM, { 32767.0f, 0.0f, 0.0f, 0.0f}, { 0, 0, 0, 0}},
5962 {WINED3DFMT_D16_UNORM, { 65535.0f, 0.0f, 0.0f, 0.0f}, { 0, 0, 0, 0}},
5964 static const struct
5966 enum wined3d_format_id format_id;
5967 struct wined3d_dvec4 mul;
5968 struct wined3d_uvec4 shift;
5970 double_conv[] =
5972 {WINED3DFMT_D24_UNORM_S8_UINT, { 16777215.0, 1.0, 0.0, 0.0}, {8, 0, 0, 0}},
5973 {WINED3DFMT_X8D24_UNORM, { 16777215.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
5974 {WINED3DFMT_D32_UNORM, {4294967295.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
5976 enum wined3d_format_id format_id = format->id;
5977 struct wined3d_color colour_srgb;
5978 struct wined3d_uvec4 idx, shift;
5979 unsigned int i;
5981 TRACE("Converting colour %s to format %s.\n", debug_color(color), debug_d3dformat(format_id));
5982 memset(ret, 0, max(sizeof(uint32_t), format->byte_count));
5984 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
5986 if (format_id != format_srgb_info[i].srgb_format_id)
5987 continue;
5989 wined3d_colour_srgb_from_linear(&colour_srgb, color);
5990 format_id = format_srgb_info[i].base_format_id;
5991 color = &colour_srgb;
5992 break;
5995 for (i = 0; i < ARRAY_SIZE(float_conv); ++i)
5997 uint32_t *ret_i = ret;
5999 if (format_id != float_conv[i].format_id)
6000 continue;
6002 idx.x = float_conv[i].shift.x / 32;
6003 idx.y = float_conv[i].shift.y / 32;
6004 idx.z = float_conv[i].shift.z / 32;
6005 idx.w = float_conv[i].shift.w / 32;
6006 shift.x = float_conv[i].shift.x % 32;
6007 shift.y = float_conv[i].shift.y % 32;
6008 shift.z = float_conv[i].shift.z % 32;
6009 shift.w = float_conv[i].shift.w % 32;
6011 ret_i[idx.x] = ((uint32_t)((color->r * float_conv[i].mul.x) + 0.5f)) << shift.x;
6012 ret_i[idx.y] |= ((uint32_t)((color->g * float_conv[i].mul.y) + 0.5f)) << shift.y;
6013 ret_i[idx.z] |= ((uint32_t)((color->b * float_conv[i].mul.z) + 0.5f)) << shift.z;
6014 ret_i[idx.w] |= ((uint32_t)((color->a * float_conv[i].mul.w) + 0.5f)) << shift.w;
6016 return;
6019 for (i = 0; i < ARRAY_SIZE(double_conv); ++i)
6021 uint32_t *ret_i;
6023 if (format_id != double_conv[i].format_id)
6024 continue;
6026 idx.x = double_conv[i].shift.x / 32;
6027 idx.y = double_conv[i].shift.y / 32;
6028 idx.z = double_conv[i].shift.z / 32;
6029 idx.w = double_conv[i].shift.w / 32;
6030 shift.x = double_conv[i].shift.x % 32;
6031 shift.y = double_conv[i].shift.y % 32;
6032 shift.z = double_conv[i].shift.z % 32;
6033 shift.w = double_conv[i].shift.w % 32;
6035 ret_i = ret;
6036 ret_i[idx.x] = ((uint32_t)((color->r * double_conv[i].mul.x) + 0.5)) << shift.x;
6037 ret_i[idx.y] |= ((uint32_t)((color->g * double_conv[i].mul.y) + 0.5)) << shift.y;
6038 ret_i[idx.z] |= ((uint32_t)((color->b * double_conv[i].mul.z) + 0.5)) << shift.z;
6039 ret_i[idx.w] |= ((uint32_t)((color->a * double_conv[i].mul.w) + 0.5)) << shift.w;
6041 return;
6044 if (format->attrs & WINED3D_FORMAT_ATTR_NORMALISED)
6046 uint32_t *ret_i = ret;
6048 idx.x = format->red_offset / 32;
6049 idx.y = format->green_offset / 32;
6050 idx.z = format->blue_offset / 32;
6051 idx.w = format->alpha_offset / 32;
6052 shift.x = format->red_offset % 32;
6053 shift.y = format->green_offset % 32;
6054 shift.z = format->blue_offset % 32;
6055 shift.w = format->alpha_offset % 32;
6057 ret_i[idx.x] = ((uint32_t)((color->r * wined3d_mask_from_size(format->red_size)) + 0.5f)) << shift.x;
6058 ret_i[idx.y] |= ((uint32_t)((color->g * wined3d_mask_from_size(format->green_size)) + 0.5f)) << shift.y;
6059 ret_i[idx.z] |= ((uint32_t)((color->b * wined3d_mask_from_size(format->blue_size)) + 0.5f)) << shift.z;
6060 ret_i[idx.w] |= ((uint32_t)((color->a * wined3d_mask_from_size(format->alpha_size)) + 0.5f)) << shift.w;
6062 return;
6065 /* 32 bit float formats. We don't handle D32_FLOAT_S8X24_UINT for now. */
6066 if ((format->attrs & WINED3D_FORMAT_ATTR_FLOAT)
6067 && (format->red_size == 32 || format->depth_size == 32)
6068 && !format->stencil_size)
6070 float *ret_f = ret;
6072 switch (format->byte_count)
6074 case 16: ret_f[3] = color->a;
6075 case 12: ret_f[2] = color->b;
6076 case 8: ret_f[1] = color->g;
6077 case 4: ret_f[0] = color->r;
6078 break;
6080 default:
6081 ERR("Unexpected byte count %u, format %s.\n", format->byte_count, debug_d3dformat(format_id));
6082 break;
6085 return;
6088 if ((format->attrs & WINED3D_FORMAT_ATTR_FLOAT) && format->red_size == 16)
6090 uint16_t *ret_s = ret;
6092 switch (format->byte_count)
6094 case 8:
6095 ret_s[3] = float_32_to_16(&color->a);
6096 ret_s[2] = float_32_to_16(&color->b);
6097 /* fall through */
6098 case 4:
6099 ret_s[1] = float_32_to_16(&color->g);
6100 /* fall through */
6101 case 2:
6102 ret_s[0] = float_32_to_16(&color->r);
6103 break;
6105 default:
6106 ERR("Unexpected byte count %u, format %s.\n", format->byte_count, debug_d3dformat(format_id));
6107 break;
6110 return;
6113 FIXME("Conversion for format %s not implemented.\n", debug_d3dformat(format_id));
6116 static float color_to_float(DWORD color, DWORD size, DWORD offset)
6118 uint32_t mask = wined3d_mask_from_size(size);
6120 if (!size)
6121 return 1.0f;
6123 color >>= offset;
6124 color &= mask;
6126 return (float)color / (float)mask;
6129 void wined3d_format_get_float_color_key(const struct wined3d_format *format,
6130 const struct wined3d_color_key *key, struct wined3d_color *float_colors)
6132 struct wined3d_color slop;
6134 switch (format->id)
6136 case WINED3DFMT_B8G8R8_UNORM:
6137 case WINED3DFMT_B8G8R8A8_UNORM:
6138 case WINED3DFMT_B8G8R8X8_UNORM:
6139 case WINED3DFMT_B5G6R5_UNORM:
6140 case WINED3DFMT_B5G5R5X1_UNORM:
6141 case WINED3DFMT_B5G5R5A1_UNORM:
6142 case WINED3DFMT_B4G4R4A4_UNORM:
6143 case WINED3DFMT_B2G3R3_UNORM:
6144 case WINED3DFMT_R8_UNORM:
6145 case WINED3DFMT_A8_UNORM:
6146 case WINED3DFMT_B2G3R3A8_UNORM:
6147 case WINED3DFMT_B4G4R4X4_UNORM:
6148 case WINED3DFMT_R10G10B10A2_UNORM:
6149 case WINED3DFMT_R10G10B10A2_SNORM:
6150 case WINED3DFMT_R8G8B8A8_UNORM:
6151 case WINED3DFMT_R8G8B8X8_UNORM:
6152 case WINED3DFMT_R16G16_UNORM:
6153 case WINED3DFMT_B10G10R10A2_UNORM:
6154 slop.r = 0.5f / wined3d_mask_from_size(format->red_size);
6155 slop.g = 0.5f / wined3d_mask_from_size(format->green_size);
6156 slop.b = 0.5f / wined3d_mask_from_size(format->blue_size);
6157 slop.a = 0.5f / wined3d_mask_from_size(format->alpha_size);
6159 float_colors[0].r = color_to_float(key->color_space_low_value, format->red_size, format->red_offset)
6160 - slop.r;
6161 float_colors[0].g = color_to_float(key->color_space_low_value, format->green_size, format->green_offset)
6162 - slop.g;
6163 float_colors[0].b = color_to_float(key->color_space_low_value, format->blue_size, format->blue_offset)
6164 - slop.b;
6165 float_colors[0].a = color_to_float(key->color_space_low_value, format->alpha_size, format->alpha_offset)
6166 - slop.a;
6168 float_colors[1].r = color_to_float(key->color_space_high_value, format->red_size, format->red_offset)
6169 + slop.r;
6170 float_colors[1].g = color_to_float(key->color_space_high_value, format->green_size, format->green_offset)
6171 + slop.g;
6172 float_colors[1].b = color_to_float(key->color_space_high_value, format->blue_size, format->blue_offset)
6173 + slop.b;
6174 float_colors[1].a = color_to_float(key->color_space_high_value, format->alpha_size, format->alpha_offset)
6175 + slop.a;
6176 break;
6178 case WINED3DFMT_P8_UINT:
6179 float_colors[0].r = 0.0f;
6180 float_colors[0].g = 0.0f;
6181 float_colors[0].b = 0.0f;
6182 float_colors[0].a = (key->color_space_low_value - 0.5f) / 255.0f;
6184 float_colors[1].r = 0.0f;
6185 float_colors[1].g = 0.0f;
6186 float_colors[1].b = 0.0f;
6187 float_colors[1].a = (key->color_space_high_value + 0.5f) / 255.0f;
6188 break;
6190 default:
6191 ERR("Unhandled color key to float conversion for format %s.\n", debug_d3dformat(format->id));
6195 enum wined3d_format_id pixelformat_for_depth(DWORD depth)
6197 switch (depth)
6199 case 8: return WINED3DFMT_P8_UINT;
6200 case 15: return WINED3DFMT_B5G5R5X1_UNORM;
6201 case 16: return WINED3DFMT_B5G6R5_UNORM;
6202 case 24: return WINED3DFMT_B8G8R8X8_UNORM; /* Robots needs 24bit to be WINED3DFMT_B8G8R8X8_UNORM */
6203 case 32: return WINED3DFMT_B8G8R8X8_UNORM; /* EVE online and the Fur demo need 32bit AdapterDisplayMode to return WINED3DFMT_B8G8R8X8_UNORM */
6204 default: return WINED3DFMT_UNKNOWN;
6208 void wined3d_format_copy_data(const struct wined3d_format *format, const uint8_t *src,
6209 unsigned int src_row_pitch, unsigned int src_slice_pitch, uint8_t *dst, unsigned int dst_row_pitch,
6210 unsigned int dst_slice_pitch, unsigned int w, unsigned int h, unsigned int d)
6212 unsigned int row_block_count, row_count, row_size, slice, row;
6213 unsigned int slice_count = d, slice_size;
6214 const uint8_t *src_row;
6215 uint8_t *dst_row;
6217 row_block_count = (w + format->block_width - 1) / format->block_width;
6218 row_count = (h + format->block_height - 1) / format->block_height;
6219 row_size = row_block_count * format->block_byte_count;
6220 slice_size = row_size * row_count;
6222 if (src_row_pitch == row_size && dst_row_pitch == row_size
6223 && ((src_slice_pitch == slice_size && dst_slice_pitch == slice_size) || slice_count == 1))
6225 memcpy(dst, src, slice_count * slice_size);
6226 return;
6229 for (slice = 0; slice < slice_count; ++slice)
6231 for (row = 0; row < row_count; ++row)
6233 src_row = &src[slice * src_slice_pitch + row * src_row_pitch];
6234 dst_row = &dst[slice * dst_slice_pitch + row * dst_row_pitch];
6235 memcpy(dst_row, src_row, row_size);
6240 void multiply_matrix(struct wined3d_matrix *dst, const struct wined3d_matrix *src1, const struct wined3d_matrix *src2)
6242 struct wined3d_matrix tmp;
6244 /* Now do the multiplication 'by hand'.
6245 I know that all this could be optimised, but this will be done later :-) */
6246 tmp._11 = (src1->_11 * src2->_11) + (src1->_21 * src2->_12) + (src1->_31 * src2->_13) + (src1->_41 * src2->_14);
6247 tmp._21 = (src1->_11 * src2->_21) + (src1->_21 * src2->_22) + (src1->_31 * src2->_23) + (src1->_41 * src2->_24);
6248 tmp._31 = (src1->_11 * src2->_31) + (src1->_21 * src2->_32) + (src1->_31 * src2->_33) + (src1->_41 * src2->_34);
6249 tmp._41 = (src1->_11 * src2->_41) + (src1->_21 * src2->_42) + (src1->_31 * src2->_43) + (src1->_41 * src2->_44);
6251 tmp._12 = (src1->_12 * src2->_11) + (src1->_22 * src2->_12) + (src1->_32 * src2->_13) + (src1->_42 * src2->_14);
6252 tmp._22 = (src1->_12 * src2->_21) + (src1->_22 * src2->_22) + (src1->_32 * src2->_23) + (src1->_42 * src2->_24);
6253 tmp._32 = (src1->_12 * src2->_31) + (src1->_22 * src2->_32) + (src1->_32 * src2->_33) + (src1->_42 * src2->_34);
6254 tmp._42 = (src1->_12 * src2->_41) + (src1->_22 * src2->_42) + (src1->_32 * src2->_43) + (src1->_42 * src2->_44);
6256 tmp._13 = (src1->_13 * src2->_11) + (src1->_23 * src2->_12) + (src1->_33 * src2->_13) + (src1->_43 * src2->_14);
6257 tmp._23 = (src1->_13 * src2->_21) + (src1->_23 * src2->_22) + (src1->_33 * src2->_23) + (src1->_43 * src2->_24);
6258 tmp._33 = (src1->_13 * src2->_31) + (src1->_23 * src2->_32) + (src1->_33 * src2->_33) + (src1->_43 * src2->_34);
6259 tmp._43 = (src1->_13 * src2->_41) + (src1->_23 * src2->_42) + (src1->_33 * src2->_43) + (src1->_43 * src2->_44);
6261 tmp._14 = (src1->_14 * src2->_11) + (src1->_24 * src2->_12) + (src1->_34 * src2->_13) + (src1->_44 * src2->_14);
6262 tmp._24 = (src1->_14 * src2->_21) + (src1->_24 * src2->_22) + (src1->_34 * src2->_23) + (src1->_44 * src2->_24);
6263 tmp._34 = (src1->_14 * src2->_31) + (src1->_24 * src2->_32) + (src1->_34 * src2->_33) + (src1->_44 * src2->_34);
6264 tmp._44 = (src1->_14 * src2->_41) + (src1->_24 * src2->_42) + (src1->_34 * src2->_43) + (src1->_44 * src2->_44);
6266 *dst = tmp;
6269 void wined3d_ffp_get_fs_settings(const struct wined3d_context *context, const struct wined3d_state *state,
6270 struct ffp_frag_settings *settings, BOOL ignore_textype)
6272 #define ARG1 0x01
6273 #define ARG2 0x02
6274 #define ARG0 0x04
6275 static const unsigned char args[WINED3D_TOP_LERP + 1] =
6277 /* undefined */ 0,
6278 /* D3DTOP_DISABLE */ 0,
6279 /* D3DTOP_SELECTARG1 */ ARG1,
6280 /* D3DTOP_SELECTARG2 */ ARG2,
6281 /* D3DTOP_MODULATE */ ARG1 | ARG2,
6282 /* D3DTOP_MODULATE2X */ ARG1 | ARG2,
6283 /* D3DTOP_MODULATE4X */ ARG1 | ARG2,
6284 /* D3DTOP_ADD */ ARG1 | ARG2,
6285 /* D3DTOP_ADDSIGNED */ ARG1 | ARG2,
6286 /* D3DTOP_ADDSIGNED2X */ ARG1 | ARG2,
6287 /* D3DTOP_SUBTRACT */ ARG1 | ARG2,
6288 /* D3DTOP_ADDSMOOTH */ ARG1 | ARG2,
6289 /* D3DTOP_BLENDDIFFUSEALPHA */ ARG1 | ARG2,
6290 /* D3DTOP_BLENDTEXTUREALPHA */ ARG1 | ARG2,
6291 /* D3DTOP_BLENDFACTORALPHA */ ARG1 | ARG2,
6292 /* D3DTOP_BLENDTEXTUREALPHAPM */ ARG1 | ARG2,
6293 /* D3DTOP_BLENDCURRENTALPHA */ ARG1 | ARG2,
6294 /* D3DTOP_PREMODULATE */ ARG1 | ARG2,
6295 /* D3DTOP_MODULATEALPHA_ADDCOLOR */ ARG1 | ARG2,
6296 /* D3DTOP_MODULATECOLOR_ADDALPHA */ ARG1 | ARG2,
6297 /* D3DTOP_MODULATEINVALPHA_ADDCOLOR */ ARG1 | ARG2,
6298 /* D3DTOP_MODULATEINVCOLOR_ADDALPHA */ ARG1 | ARG2,
6299 /* D3DTOP_BUMPENVMAP */ ARG1 | ARG2,
6300 /* D3DTOP_BUMPENVMAPLUMINANCE */ ARG1 | ARG2,
6301 /* D3DTOP_DOTPRODUCT3 */ ARG1 | ARG2,
6302 /* D3DTOP_MULTIPLYADD */ ARG1 | ARG2 | ARG0,
6303 /* D3DTOP_LERP */ ARG1 | ARG2 | ARG0
6305 unsigned int i;
6306 DWORD ttff;
6307 DWORD cop, aop, carg0, carg1, carg2, aarg0, aarg1, aarg2;
6308 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6309 struct wined3d_texture *texture;
6311 settings->padding = 0;
6313 for (i = 0; i < d3d_info->ffp_fragment_caps.max_blend_stages; ++i)
6315 settings->op[i].padding = 0;
6316 if (state->texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
6318 settings->op[i].cop = WINED3D_TOP_DISABLE;
6319 settings->op[i].aop = WINED3D_TOP_DISABLE;
6320 settings->op[i].carg0 = settings->op[i].carg1 = settings->op[i].carg2 = ARG_UNUSED;
6321 settings->op[i].aarg0 = settings->op[i].aarg1 = settings->op[i].aarg2 = ARG_UNUSED;
6322 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6323 settings->op[i].tmp_dst = 0;
6324 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6325 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6326 i++;
6327 break;
6330 if ((texture = wined3d_state_get_ffp_texture(state, i)))
6332 if (can_use_texture_swizzle(d3d_info, texture->resource.format))
6333 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6334 else
6335 settings->op[i].color_fixup = texture->resource.format->color_fixup;
6336 if (ignore_textype)
6337 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6338 else
6339 settings->op[i].tex_type = texture->resource.gl_type;
6340 } else {
6341 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6342 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6345 cop = state->texture_states[i][WINED3D_TSS_COLOR_OP];
6346 aop = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
6348 carg1 = (args[cop] & ARG1) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG1] : ARG_UNUSED;
6349 carg2 = (args[cop] & ARG2) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG2] : ARG_UNUSED;
6350 carg0 = (args[cop] & ARG0) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG0] : ARG_UNUSED;
6352 if (is_invalid_op(state, i, cop, carg1, carg2, carg0))
6354 carg0 = ARG_UNUSED;
6355 carg2 = ARG_UNUSED;
6356 carg1 = WINED3DTA_CURRENT;
6357 cop = WINED3D_TOP_SELECT_ARG1;
6360 if (cop == WINED3D_TOP_DOTPRODUCT3)
6362 /* A dotproduct3 on the colorop overwrites the alphaop operation and replicates
6363 * the color result to the alpha component of the destination
6365 aop = cop;
6366 aarg1 = carg1;
6367 aarg2 = carg2;
6368 aarg0 = carg0;
6370 else
6372 aarg1 = (args[aop] & ARG1) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] : ARG_UNUSED;
6373 aarg2 = (args[aop] & ARG2) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] : ARG_UNUSED;
6374 aarg0 = (args[aop] & ARG0) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] : ARG_UNUSED;
6377 if (!i && state->render_states[WINED3D_RS_COLORKEYENABLE]
6378 && texture && !(texture->resource.usage & WINED3DUSAGE_LEGACY_CUBEMAP)
6379 && (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT) && !texture->resource.format->alpha_size)
6381 if (aop == WINED3D_TOP_DISABLE)
6383 aarg1 = WINED3DTA_TEXTURE;
6384 aop = WINED3D_TOP_SELECT_ARG1;
6386 else if (aop == WINED3D_TOP_SELECT_ARG1 && aarg1 != WINED3DTA_TEXTURE)
6388 if (state->blend_state && state->blend_state->desc.rt[0].enable)
6390 aarg2 = WINED3DTA_TEXTURE;
6391 aop = WINED3D_TOP_MODULATE;
6393 else
6395 aarg1 = WINED3DTA_TEXTURE;
6398 else if (aop == WINED3D_TOP_SELECT_ARG2 && aarg2 != WINED3DTA_TEXTURE)
6400 if (state->blend_state && state->blend_state->desc.rt[0].enable)
6402 aarg1 = WINED3DTA_TEXTURE;
6403 aop = WINED3D_TOP_MODULATE;
6405 else
6407 aarg2 = WINED3DTA_TEXTURE;
6412 if (is_invalid_op(state, i, aop, aarg1, aarg2, aarg0))
6414 aarg0 = ARG_UNUSED;
6415 aarg2 = ARG_UNUSED;
6416 aarg1 = WINED3DTA_CURRENT;
6417 aop = WINED3D_TOP_SELECT_ARG1;
6420 if (carg1 == WINED3DTA_TEXTURE || carg2 == WINED3DTA_TEXTURE || carg0 == WINED3DTA_TEXTURE
6421 || aarg1 == WINED3DTA_TEXTURE || aarg2 == WINED3DTA_TEXTURE || aarg0 == WINED3DTA_TEXTURE)
6423 ttff = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
6424 if (ttff == (WINED3D_TTFF_PROJECTED | WINED3D_TTFF_COUNT3))
6425 settings->op[i].projected = WINED3D_PROJECTION_COUNT3;
6426 else if (ttff & WINED3D_TTFF_PROJECTED)
6427 settings->op[i].projected = WINED3D_PROJECTION_COUNT4;
6428 else
6429 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6431 else
6433 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6436 settings->op[i].cop = cop;
6437 settings->op[i].aop = aop;
6438 settings->op[i].carg0 = carg0;
6439 settings->op[i].carg1 = carg1;
6440 settings->op[i].carg2 = carg2;
6441 settings->op[i].aarg0 = aarg0;
6442 settings->op[i].aarg1 = aarg1;
6443 settings->op[i].aarg2 = aarg2;
6444 settings->op[i].tmp_dst = state->texture_states[i][WINED3D_TSS_RESULT_ARG] == WINED3DTA_TEMP;
6447 /* Clear unsupported stages */
6448 for (; i < WINED3D_MAX_FFP_TEXTURES; ++i)
6449 memset(&settings->op[i], 0xff, sizeof(settings->op[i]));
6451 if (!state->render_states[WINED3D_RS_FOGENABLE])
6453 settings->fog = WINED3D_FFP_PS_FOG_OFF;
6455 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
6457 if (use_vs(state) || state->vertex_declaration->position_transformed)
6459 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6461 else
6463 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
6465 case WINED3D_FOG_NONE:
6466 case WINED3D_FOG_LINEAR:
6467 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6468 break;
6469 case WINED3D_FOG_EXP:
6470 settings->fog = WINED3D_FFP_PS_FOG_EXP;
6471 break;
6472 case WINED3D_FOG_EXP2:
6473 settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6474 break;
6478 else
6480 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
6482 case WINED3D_FOG_LINEAR:
6483 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6484 break;
6485 case WINED3D_FOG_EXP:
6486 settings->fog = WINED3D_FFP_PS_FOG_EXP;
6487 break;
6488 case WINED3D_FOG_EXP2:
6489 settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6490 break;
6493 settings->sRGB_write = !d3d_info->srgb_write_control && needs_srgb_write(d3d_info, state, &state->fb);
6494 if (d3d_info->vs_clipping || !use_vs(state) || !state->render_states[WINED3D_RS_CLIPPING]
6495 || !state->render_states[WINED3D_RS_CLIPPLANEENABLE])
6497 /* No need to emulate clipplanes if GL supports native vertex shader clipping or if
6498 * the fixed function vertex pipeline is used(which always supports clipplanes), or
6499 * if no clipplane is enabled
6501 settings->emul_clipplanes = 0;
6502 } else {
6503 settings->emul_clipplanes = 1;
6506 texture = wined3d_state_get_ffp_texture(state, 0);
6507 if (state->render_states[WINED3D_RS_COLORKEYENABLE]
6508 && texture && (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
6509 && settings->op[0].cop != WINED3D_TOP_DISABLE)
6510 settings->color_key_enabled = 1;
6511 else
6512 settings->color_key_enabled = 0;
6514 /* texcoords_initialized is set to meaningful values only when GL doesn't
6515 * support enough varyings to always pass around all the possible texture
6516 * coordinates.
6517 * This is used to avoid reading a varying not written by the vertex shader.
6518 * Reading uninitialized varyings on core profile contexts results in an
6519 * error while with builtin varyings on legacy contexts you get undefined
6520 * behavior. */
6521 if (d3d_info->limits.varying_count && !d3d_info->full_ffp_varyings)
6523 settings->texcoords_initialized = 0;
6524 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
6526 if (use_vs(state))
6528 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
6529 settings->texcoords_initialized |= 1u << i;
6531 else
6533 const struct wined3d_stream_info *si = &context->stream_info;
6534 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6535 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
6536 & WINED3D_FFP_TCI_MASK
6537 || (coord_idx < WINED3D_MAX_FFP_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
6538 settings->texcoords_initialized |= 1u << i;
6542 else
6544 settings->texcoords_initialized = wined3d_mask_from_size(WINED3D_MAX_FFP_TEXTURES);
6547 settings->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
6548 && state->primitive_type == WINED3D_PT_POINTLIST;
6550 if (d3d_info->ffp_alpha_test)
6551 settings->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
6552 else
6553 settings->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
6554 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
6555 : WINED3D_CMP_ALWAYS) - 1;
6557 if (d3d_info->emulated_flatshading)
6558 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6559 else
6560 settings->flatshading = FALSE;
6563 const struct ffp_frag_desc *find_ffp_frag_shader(const struct wine_rb_tree *fragment_shaders,
6564 const struct ffp_frag_settings *settings)
6566 struct wine_rb_entry *entry = wine_rb_get(fragment_shaders, settings);
6567 return entry ? WINE_RB_ENTRY_VALUE(entry, struct ffp_frag_desc, entry) : NULL;
6570 void add_ffp_frag_shader(struct wine_rb_tree *shaders, struct ffp_frag_desc *desc)
6572 /* Note that the key is the implementation independent part of the ffp_frag_desc structure,
6573 * whereas desc points to an extended structure with implementation specific parts. */
6574 if (wine_rb_put(shaders, &desc->settings, &desc->entry) == -1)
6576 ERR("Failed to insert ffp frag shader.\n");
6580 int wined3d_ffp_frag_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6582 const struct ffp_frag_settings *ka = key;
6583 const struct ffp_frag_settings *kb = &WINE_RB_ENTRY_VALUE(entry, const struct ffp_frag_desc, entry)->settings;
6585 return memcmp(ka, kb, sizeof(*ka));
6588 void wined3d_ffp_get_vs_settings(const struct wined3d_context *context,
6589 const struct wined3d_state *state, struct wined3d_ffp_vs_settings *settings)
6591 enum wined3d_material_color_source diffuse_source, emissive_source, ambient_source, specular_source;
6592 const struct wined3d_stream_info *si = &context->stream_info;
6593 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6594 unsigned int coord_idx, i;
6596 memset(settings, 0, sizeof(*settings));
6598 if (si->position_transformed)
6600 settings->transformed = 1;
6601 settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6602 settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6603 if (!state->render_states[WINED3D_RS_FOGENABLE])
6604 settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
6605 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6606 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6607 else
6608 settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;
6610 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
6612 coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6613 if (coord_idx < WINED3D_MAX_FFP_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6614 settings->texcoords |= 1u << i;
6615 settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6617 if (d3d_info->full_ffp_varyings)
6618 settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_FFP_TEXTURES);
6620 if (d3d_info->emulated_flatshading)
6621 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6622 else
6623 settings->flatshading = FALSE;
6625 settings->swizzle_map = si->swizzle_map;
6627 return;
6630 switch (state->render_states[WINED3D_RS_VERTEXBLEND])
6632 case WINED3D_VBF_DISABLE:
6633 case WINED3D_VBF_1WEIGHTS:
6634 case WINED3D_VBF_2WEIGHTS:
6635 case WINED3D_VBF_3WEIGHTS:
6636 settings->vertexblends = state->render_states[WINED3D_RS_VERTEXBLEND];
6637 break;
6638 default:
6639 FIXME("Unsupported vertex blending: %d\n", state->render_states[WINED3D_RS_VERTEXBLEND]);
6640 break;
6643 settings->clipping = state->render_states[WINED3D_RS_CLIPPING]
6644 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
6645 settings->normal = !!(si->use_map & (1u << WINED3D_FFP_NORMAL));
6646 settings->normalize = settings->normal && state->render_states[WINED3D_RS_NORMALIZENORMALS];
6647 settings->lighting = !!state->render_states[WINED3D_RS_LIGHTING];
6648 settings->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
6649 settings->specular_enable = !!state->render_states[WINED3D_RS_SPECULARENABLE];
6650 settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6651 settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6653 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
6654 &ambient_source, &specular_source, state, si);
6655 settings->diffuse_source = diffuse_source;
6656 settings->emissive_source = emissive_source;
6657 settings->ambient_source = ambient_source;
6658 settings->specular_source = specular_source;
6660 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
6662 coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6663 if (coord_idx < WINED3D_MAX_FFP_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6664 settings->texcoords |= 1u << i;
6665 settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6667 if (d3d_info->full_ffp_varyings)
6668 settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_FFP_TEXTURES);
6670 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
6672 if (!state->light_state.lights[i])
6673 continue;
6675 switch (state->light_state.lights[i]->OriginalParms.type)
6677 case WINED3D_LIGHT_POINT:
6678 ++settings->point_light_count;
6679 break;
6680 case WINED3D_LIGHT_SPOT:
6681 ++settings->spot_light_count;
6682 break;
6683 case WINED3D_LIGHT_DIRECTIONAL:
6684 ++settings->directional_light_count;
6685 break;
6686 case WINED3D_LIGHT_PARALLELPOINT:
6687 ++settings->parallel_point_light_count;
6688 break;
6689 default:
6690 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
6691 break;
6695 if (!state->render_states[WINED3D_RS_FOGENABLE])
6696 settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
6697 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6699 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6701 if (state->transforms[WINED3D_TS_PROJECTION]._14 == 0.0f
6702 && state->transforms[WINED3D_TS_PROJECTION]._24 == 0.0f
6703 && state->transforms[WINED3D_TS_PROJECTION]._34 == 0.0f
6704 && state->transforms[WINED3D_TS_PROJECTION]._44 == 1.0f)
6705 settings->ortho_fog = 1;
6707 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE)
6708 settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;
6709 else if (state->render_states[WINED3D_RS_RANGEFOGENABLE])
6710 settings->fog_mode = WINED3D_FFP_VS_FOG_RANGE;
6711 else
6712 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6714 if (d3d_info->emulated_flatshading)
6715 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6716 else
6717 settings->flatshading = FALSE;
6719 settings->swizzle_map = si->swizzle_map;
6722 int wined3d_ffp_vertex_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6724 const struct wined3d_ffp_vs_settings *ka = key;
6725 const struct wined3d_ffp_vs_settings *kb = &WINE_RB_ENTRY_VALUE(entry,
6726 const struct wined3d_ffp_vs_desc, entry)->settings;
6728 return memcmp(ka, kb, sizeof(*ka));
6731 const char *wined3d_debug_location(uint32_t location)
6733 struct debug_buffer buffer;
6734 const char *prefix = "";
6735 const char *suffix = "";
6737 if (wined3d_popcount(location) > 16)
6739 prefix = "~(";
6740 location = ~location;
6741 suffix = ")";
6744 init_debug_buffer(&buffer, "0");
6745 #define LOCATION_TO_STR(x) if (location & x) { debug_append(&buffer, #x, " | "); location &= ~x; }
6746 LOCATION_TO_STR(WINED3D_LOCATION_DISCARDED);
6747 LOCATION_TO_STR(WINED3D_LOCATION_SYSMEM);
6748 LOCATION_TO_STR(WINED3D_LOCATION_CLEARED);
6749 LOCATION_TO_STR(WINED3D_LOCATION_BUFFER);
6750 LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_RGB);
6751 LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_SRGB);
6752 LOCATION_TO_STR(WINED3D_LOCATION_DRAWABLE);
6753 LOCATION_TO_STR(WINED3D_LOCATION_RB_MULTISAMPLE);
6754 LOCATION_TO_STR(WINED3D_LOCATION_RB_RESOLVED);
6755 #undef LOCATION_TO_STR
6756 if (location)
6757 FIXME("Unrecognized location flag(s) %#x.\n", location);
6759 return wine_dbg_sprintf("%s%s%s", prefix, buffer.str, suffix);
6762 const char *wined3d_debug_feature_level(enum wined3d_feature_level level)
6764 switch (level)
6766 #define LEVEL_TO_STR(level) case level: return #level
6767 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_5);
6768 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_6);
6769 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_7);
6770 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_8);
6771 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_1);
6772 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_2);
6773 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_3);
6774 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10);
6775 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10_1);
6776 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11);
6777 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11_1);
6778 #undef LEVEL_TO_STR
6779 default:
6780 return wine_dbg_sprintf("%#x", level);
6784 /* Print a floating point value with the %.8e format specifier, always using
6785 * '.' as decimal separator. */
6786 void wined3d_ftoa(float value, char *s)
6788 int idx = 1;
6790 if (copysignf(1.0f, value) < 0.0f)
6791 ++idx;
6793 /* Be sure to allocate a buffer of at least 17 characters for the result
6794 as sprintf may return a 3 digit exponent when using the MSVC runtime
6795 instead of a 2 digit exponent. */
6796 sprintf(s, "%.8e", value);
6797 if (isfinite(value))
6798 s[idx] = '.';
6801 void wined3d_release_dc(HWND window, HDC dc)
6803 /* You'd figure ReleaseDC() would fail if the DC doesn't match the window.
6804 * However, that's not what actually happens, and there are user32 tests
6805 * that confirm ReleaseDC() with the wrong window is supposed to succeed.
6806 * So explicitly check that the DC belongs to the window, since we want to
6807 * avoid releasing a DC that belongs to some other window if the original
6808 * window was already destroyed. */
6809 if (WindowFromDC(dc) != window)
6810 WARN("DC %p does not belong to window %p.\n", dc, window);
6811 else if (!ReleaseDC(window, dc))
6812 ERR("Failed to release device context %p, last error %#lx.\n", dc, GetLastError());
6815 BOOL wined3d_clip_blit(const RECT *clip_rect, RECT *clipped, RECT *other)
6817 RECT orig = *clipped;
6818 float scale_x = (float)(orig.right - orig.left) / (float)(other->right - other->left);
6819 float scale_y = (float)(orig.bottom - orig.top) / (float)(other->bottom - other->top);
6821 IntersectRect(clipped, clipped, clip_rect);
6823 if (IsRectEmpty(clipped))
6825 SetRectEmpty(other);
6826 return FALSE;
6829 other->left += (LONG)((clipped->left - orig.left) / scale_x);
6830 other->top += (LONG)((clipped->top - orig.top) / scale_y);
6831 other->right -= (LONG)((orig.right - clipped->right) / scale_x);
6832 other->bottom -= (LONG)((orig.bottom - clipped->bottom) / scale_y);
6834 return TRUE;
6837 void wined3d_gl_limits_get_uniform_block_range(const struct wined3d_gl_limits *gl_limits,
6838 enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
6840 unsigned int i;
6842 *base = 0;
6843 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
6845 *count = gl_limits->uniform_blocks[i];
6846 if (i == shader_type)
6847 return;
6848 *base += *count;
6851 ERR("Unrecognized shader type %#x.\n", shader_type);
6852 *count = 0;
6855 void wined3d_gl_limits_get_texture_unit_range(const struct wined3d_gl_limits *gl_limits,
6856 enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
6858 unsigned int i;
6860 if (shader_type == WINED3D_SHADER_TYPE_COMPUTE)
6862 if (gl_limits->combined_samplers == gl_limits->graphics_samplers)
6863 *base = 0;
6864 else
6865 *base = gl_limits->graphics_samplers;
6866 *count = gl_limits->samplers[WINED3D_SHADER_TYPE_COMPUTE];
6867 return;
6870 *base = 0;
6871 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
6873 *count = gl_limits->samplers[i];
6874 if (i == shader_type)
6875 return;
6876 *base += *count;
6879 ERR("Unrecognized shader type %#x.\n", shader_type);
6880 *count = 0;
6883 BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE_T size)
6885 SIZE_T max_capacity, new_capacity;
6886 void *new_elements;
6888 if (count <= *capacity)
6889 return TRUE;
6891 max_capacity = ~(SIZE_T)0 / size;
6892 if (count > max_capacity)
6893 return FALSE;
6895 new_capacity = max(1, *capacity);
6896 while (new_capacity < count && new_capacity <= max_capacity / 2)
6897 new_capacity *= 2;
6898 if (new_capacity < count)
6899 new_capacity = count;
6901 new_elements = _recalloc(*elements, new_capacity, size);
6902 if (!new_elements)
6903 return FALSE;
6905 *elements = new_elements;
6906 *capacity = new_capacity;
6907 return TRUE;
6910 static void swap_rows(float **a, float **b)
6912 float *tmp = *a;
6914 *a = *b;
6915 *b = tmp;
6918 BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
6920 float wtmp[4][8];
6921 float m0, m1, m2, m3, s;
6922 float *r0, *r1, *r2, *r3;
6924 r0 = wtmp[0];
6925 r1 = wtmp[1];
6926 r2 = wtmp[2];
6927 r3 = wtmp[3];
6929 r0[0] = m->_11;
6930 r0[1] = m->_12;
6931 r0[2] = m->_13;
6932 r0[3] = m->_14;
6933 r0[4] = 1.0f;
6934 r0[5] = r0[6] = r0[7] = 0.0f;
6936 r1[0] = m->_21;
6937 r1[1] = m->_22;
6938 r1[2] = m->_23;
6939 r1[3] = m->_24;
6940 r1[5] = 1.0f;
6941 r1[4] = r1[6] = r1[7] = 0.0f;
6943 r2[0] = m->_31;
6944 r2[1] = m->_32;
6945 r2[2] = m->_33;
6946 r2[3] = m->_34;
6947 r2[6] = 1.0f;
6948 r2[4] = r2[5] = r2[7] = 0.0f;
6950 r3[0] = m->_41;
6951 r3[1] = m->_42;
6952 r3[2] = m->_43;
6953 r3[3] = m->_44;
6954 r3[7] = 1.0f;
6955 r3[4] = r3[5] = r3[6] = 0.0f;
6957 /* Choose pivot - or die. */
6958 if (fabsf(r3[0]) > fabsf(r2[0]))
6959 swap_rows(&r3, &r2);
6960 if (fabsf(r2[0]) > fabsf(r1[0]))
6961 swap_rows(&r2, &r1);
6962 if (fabsf(r1[0]) > fabsf(r0[0]))
6963 swap_rows(&r1, &r0);
6964 if (r0[0] == 0.0f)
6965 return FALSE;
6967 /* Eliminate first variable. */
6968 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
6969 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
6970 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
6971 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
6972 s = r0[4];
6973 if (s != 0.0f)
6975 r1[4] -= m1 * s;
6976 r2[4] -= m2 * s;
6977 r3[4] -= m3 * s;
6979 s = r0[5];
6980 if (s != 0.0f)
6982 r1[5] -= m1 * s;
6983 r2[5] -= m2 * s;
6984 r3[5] -= m3 * s;
6986 s = r0[6];
6987 if (s != 0.0f)
6989 r1[6] -= m1 * s;
6990 r2[6] -= m2 * s;
6991 r3[6] -= m3 * s;
6993 s = r0[7];
6994 if (s != 0.0f)
6996 r1[7] -= m1 * s;
6997 r2[7] -= m2 * s;
6998 r3[7] -= m3 * s;
7001 /* Choose pivot - or die. */
7002 if (fabsf(r3[1]) > fabsf(r2[1]))
7003 swap_rows(&r3, &r2);
7004 if (fabsf(r2[1]) > fabsf(r1[1]))
7005 swap_rows(&r2, &r1);
7006 if (r1[1] == 0.0f)
7007 return FALSE;
7009 /* Eliminate second variable. */
7010 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
7011 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
7012 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
7013 s = r1[4];
7014 if (s != 0.0f)
7016 r2[4] -= m2 * s;
7017 r3[4] -= m3 * s;
7019 s = r1[5];
7020 if (s != 0.0f)
7022 r2[5] -= m2 * s;
7023 r3[5] -= m3 * s;
7025 s = r1[6];
7026 if (s != 0.0f)
7028 r2[6] -= m2 * s;
7029 r3[6] -= m3 * s;
7031 s = r1[7];
7032 if (s != 0.0f)
7034 r2[7] -= m2 * s;
7035 r3[7] -= m3 * s;
7038 /* Choose pivot - or die. */
7039 if (fabsf(r3[2]) > fabsf(r2[2]))
7040 swap_rows(&r3, &r2);
7041 if (r2[2] == 0.0f)
7042 return FALSE;
7044 /* Eliminate third variable. */
7045 m3 = r3[2] / r2[2];
7046 r3[3] -= m3 * r2[3];
7047 r3[4] -= m3 * r2[4];
7048 r3[5] -= m3 * r2[5];
7049 r3[6] -= m3 * r2[6];
7050 r3[7] -= m3 * r2[7];
7052 /* Last check. */
7053 if (r3[3] == 0.0f)
7054 return FALSE;
7056 /* Back substitute row 3. */
7057 s = 1.0f / r3[3];
7058 r3[4] *= s;
7059 r3[5] *= s;
7060 r3[6] *= s;
7061 r3[7] *= s;
7063 /* Back substitute row 2. */
7064 m2 = r2[3];
7065 s = 1.0f / r2[2];
7066 r2[4] = s * (r2[4] - r3[4] * m2);
7067 r2[5] = s * (r2[5] - r3[5] * m2);
7068 r2[6] = s * (r2[6] - r3[6] * m2);
7069 r2[7] = s * (r2[7] - r3[7] * m2);
7070 m1 = r1[3];
7071 r1[4] -= r3[4] * m1;
7072 r1[5] -= r3[5] * m1;
7073 r1[6] -= r3[6] * m1;
7074 r1[7] -= r3[7] * m1;
7075 m0 = r0[3];
7076 r0[4] -= r3[4] * m0;
7077 r0[5] -= r3[5] * m0;
7078 r0[6] -= r3[6] * m0;
7079 r0[7] -= r3[7] * m0;
7081 /* Back substitute row 1. */
7082 m1 = r1[2];
7083 s = 1.0f / r1[1];
7084 r1[4] = s * (r1[4] - r2[4] * m1);
7085 r1[5] = s * (r1[5] - r2[5] * m1);
7086 r1[6] = s * (r1[6] - r2[6] * m1);
7087 r1[7] = s * (r1[7] - r2[7] * m1);
7088 m0 = r0[2];
7089 r0[4] -= r2[4] * m0;
7090 r0[5] -= r2[5] * m0;
7091 r0[6] -= r2[6] * m0;
7092 r0[7] -= r2[7] * m0;
7094 /* Back substitute row 0. */
7095 m0 = r0[1];
7096 s = 1.0f / r0[0];
7097 r0[4] = s * (r0[4] - r1[4] * m0);
7098 r0[5] = s * (r0[5] - r1[5] * m0);
7099 r0[6] = s * (r0[6] - r1[6] * m0);
7100 r0[7] = s * (r0[7] - r1[7] * m0);
7102 out->_11 = r0[4];
7103 out->_12 = r0[5];
7104 out->_13 = r0[6];
7105 out->_14 = r0[7];
7106 out->_21 = r1[4];
7107 out->_22 = r1[5];
7108 out->_23 = r1[6];
7109 out->_24 = r1[7];
7110 out->_31 = r2[4];
7111 out->_32 = r2[5];
7112 out->_33 = r2[6];
7113 out->_34 = r2[7];
7114 out->_41 = r3[4];
7115 out->_42 = r3[5];
7116 out->_43 = r3[6];
7117 out->_44 = r3[7];
7119 return TRUE;
7122 /* Taken and adapted from Mesa. */
7123 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
7125 float pos, neg, t, det;
7126 struct wined3d_matrix temp;
7128 /* Calculate the determinant of upper left 3x3 submatrix and
7129 * determine if the matrix is singular. */
7130 pos = neg = 0.0f;
7131 t = in->_11 * in->_22 * in->_33;
7132 if (t >= 0.0f)
7133 pos += t;
7134 else
7135 neg += t;
7137 t = in->_21 * in->_32 * in->_13;
7138 if (t >= 0.0f)
7139 pos += t;
7140 else
7141 neg += t;
7142 t = in->_31 * in->_12 * in->_23;
7143 if (t >= 0.0f)
7144 pos += t;
7145 else
7146 neg += t;
7148 t = -in->_31 * in->_22 * in->_13;
7149 if (t >= 0.0f)
7150 pos += t;
7151 else
7152 neg += t;
7153 t = -in->_21 * in->_12 * in->_33;
7154 if (t >= 0.0f)
7155 pos += t;
7156 else
7157 neg += t;
7159 t = -in->_11 * in->_32 * in->_23;
7160 if (t >= 0.0f)
7161 pos += t;
7162 else
7163 neg += t;
7165 det = pos + neg;
7167 if (fabsf(det) < 1e-25f)
7168 return FALSE;
7170 det = 1.0f / det;
7171 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
7172 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
7173 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
7174 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
7175 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
7176 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
7177 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
7178 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
7179 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
7181 *out = temp;
7182 return TRUE;
7185 void compute_normal_matrix(float *normal_matrix, BOOL legacy_lighting,
7186 const struct wined3d_matrix *modelview)
7188 struct wined3d_matrix mv;
7189 unsigned int i, j;
7191 mv = *modelview;
7192 if (legacy_lighting)
7193 invert_matrix_3d(&mv, &mv);
7194 else
7195 invert_matrix(&mv, &mv);
7196 /* Tests show that singular modelview matrices are used unchanged as normal
7197 * matrices on D3D3 and older. There seems to be no clearly consistent
7198 * behavior on newer D3D versions so always follow older ddraw behavior. */
7199 for (i = 0; i < 3; ++i)
7200 for (j = 0; j < 3; ++j)
7201 normal_matrix[i * 3 + j] = (&mv._11)[j * 4 + i];
7204 static void wined3d_allocator_release_block(struct wined3d_allocator *allocator,
7205 struct wined3d_allocator_block *block)
7207 block->parent = allocator->free;
7208 allocator->free = block;
7211 static struct wined3d_allocator_block *wined3d_allocator_acquire_block(struct wined3d_allocator *allocator)
7213 struct wined3d_allocator_block *block;
7215 if (!allocator->free)
7216 return malloc(sizeof(*block));
7218 block = allocator->free;
7219 allocator->free = block->parent;
7221 return block;
7224 void wined3d_allocator_block_free(struct wined3d_allocator_block *block)
7226 struct wined3d_allocator *allocator = block->chunk->allocator;
7227 struct wined3d_allocator_block *parent;
7229 while ((parent = block->parent) && block->sibling->free)
7231 list_remove(&block->sibling->entry);
7232 wined3d_allocator_release_block(allocator, block->sibling);
7233 wined3d_allocator_release_block(allocator, block);
7234 block = parent;
7237 block->free = true;
7238 list_add_head(&block->chunk->available[block->order], &block->entry);
7241 static void wined3d_allocator_block_init(struct wined3d_allocator_block *block,
7242 struct wined3d_allocator_chunk *chunk, struct wined3d_allocator_block *parent,
7243 struct wined3d_allocator_block *sibling, unsigned int order, size_t offset, bool free)
7245 list_init(&block->entry);
7246 block->chunk = chunk;
7247 block->parent = parent;
7248 block->sibling = sibling;
7249 block->order = order;
7250 block->offset = offset;
7251 block->free = free;
7254 void wined3d_allocator_chunk_cleanup(struct wined3d_allocator_chunk *chunk)
7256 struct wined3d_allocator_block *block;
7257 size_t i;
7259 if (list_empty(&chunk->available[0]))
7261 ERR("Chunk %p is not empty.\n", chunk);
7262 return;
7265 for (i = 1; i < ARRAY_SIZE(chunk->available); ++i)
7267 if (!list_empty(&chunk->available[i]))
7269 ERR("Chunk %p is not empty.\n", chunk);
7270 return;
7274 block = LIST_ENTRY(list_head(&chunk->available[0]), struct wined3d_allocator_block, entry);
7275 wined3d_allocator_release_block(chunk->allocator, block);
7278 bool wined3d_allocator_chunk_init(struct wined3d_allocator_chunk *chunk, struct wined3d_allocator *allocator)
7280 struct wined3d_allocator_block *block;
7281 unsigned int i;
7283 if (!(block = wined3d_allocator_acquire_block(allocator)))
7284 return false;
7285 wined3d_allocator_block_init(block, chunk, NULL, NULL, 0, 0, true);
7287 list_init(&chunk->entry);
7288 for (i = 0; i < ARRAY_SIZE(chunk->available); ++i)
7290 list_init(&chunk->available[i]);
7292 list_add_head(&chunk->available[0], &block->entry);
7293 chunk->allocator = allocator;
7294 chunk->map_count = 0;
7295 chunk->map_ptr = NULL;
7297 return true;
7300 void wined3d_allocator_cleanup(struct wined3d_allocator *allocator)
7302 struct wined3d_allocator_chunk *chunk, *chunk2;
7303 struct wined3d_allocator_block *block, *next;
7304 size_t i;
7306 for (i = 0; i < allocator->pool_count; ++i)
7308 LIST_FOR_EACH_ENTRY_SAFE(chunk, chunk2, &allocator->pools[i].chunks, struct wined3d_allocator_chunk, entry)
7310 list_remove(&chunk->entry);
7311 allocator->ops->allocator_destroy_chunk(chunk);
7314 free(allocator->pools);
7316 next = allocator->free;
7317 while ((block = next))
7319 next = block->parent;
7320 free(block);
7324 static struct wined3d_allocator_block *wined3d_allocator_chunk_allocate(struct wined3d_allocator_chunk *chunk,
7325 unsigned int order)
7327 struct wined3d_allocator_block *block, *left, *right;
7328 unsigned int i = order;
7330 while (i)
7332 if (!list_empty(&chunk->available[i]))
7333 break;
7334 --i;
7337 if (list_empty(&chunk->available[i]))
7338 return NULL;
7340 block = LIST_ENTRY(list_head(&chunk->available[i]), struct wined3d_allocator_block, entry);
7341 list_remove(&block->entry);
7342 block->free = false;
7344 while (i < order)
7346 if (!(left = wined3d_allocator_acquire_block(chunk->allocator)))
7348 ERR("Failed to allocate left.\n");
7349 break;
7352 if (!(right = wined3d_allocator_acquire_block(chunk->allocator)))
7354 ERR("Failed to allocate right.\n");
7355 wined3d_allocator_release_block(chunk->allocator, left);
7356 break;
7359 wined3d_allocator_block_init(left, chunk, block, right, block->order + 1, block->offset, false);
7360 wined3d_allocator_block_init(right, chunk, block, left, block->order + 1,
7361 block->offset + (WINED3D_ALLOCATOR_CHUNK_SIZE >> left->order), true);
7362 list_add_head(&chunk->available[right->order], &right->entry);
7364 block = left;
7365 ++i;
7368 return block;
7371 struct wined3d_allocator_block *wined3d_allocator_allocate(struct wined3d_allocator *allocator,
7372 struct wined3d_context *context, unsigned int memory_type, size_t size)
7374 struct wined3d_allocator_chunk *chunk;
7375 struct wined3d_allocator_block *block;
7376 unsigned int order;
7378 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
7379 return NULL;
7381 if (size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE)
7382 order = WINED3D_ALLOCATOR_CHUNK_ORDER_COUNT - 1;
7383 else
7384 order = wined3d_log2i(WINED3D_ALLOCATOR_CHUNK_SIZE / size);
7386 LIST_FOR_EACH_ENTRY(chunk, &allocator->pools[memory_type].chunks, struct wined3d_allocator_chunk, entry)
7388 if ((block = wined3d_allocator_chunk_allocate(chunk, order)))
7389 return block;
7392 if (!(chunk = allocator->ops->allocator_create_chunk(allocator,
7393 context, memory_type, WINED3D_ALLOCATOR_CHUNK_SIZE)))
7394 return NULL;
7396 if (!(block = wined3d_allocator_chunk_allocate(chunk, order)))
7397 return NULL;
7399 return block;
7402 bool wined3d_allocator_init(struct wined3d_allocator *allocator,
7403 size_t pool_count, const struct wined3d_allocator_ops *allocator_ops)
7405 size_t i;
7407 allocator->ops = allocator_ops;
7408 allocator->pool_count = pool_count;
7409 if (!(allocator->pools = calloc(pool_count, sizeof(*allocator->pools))))
7410 return false;
7411 for (i = 0; i < pool_count; ++i)
7413 list_init(&allocator->pools[i].chunks);
7416 allocator->free = NULL;
7418 return true;