wined3d: Spawn a separate thread to adjust the window state of windows belonging...
[wine.git] / dlls / wined3d / utils.c
blob401d815a365ebb72a67010d4451d26dd4c56e4fe
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"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
33 #define WINED3D_FORMAT_FOURCC_BASE (WINED3DFMT_BC7_UNORM_SRGB + 1)
35 static const struct
37 enum wined3d_format_id id;
38 unsigned int idx;
40 format_index_remap[] =
42 {WINED3DFMT_UYVY, WINED3D_FORMAT_FOURCC_BASE},
43 {WINED3DFMT_YUY2, WINED3D_FORMAT_FOURCC_BASE + 1},
44 {WINED3DFMT_YV12, WINED3D_FORMAT_FOURCC_BASE + 2},
45 {WINED3DFMT_DXT1, WINED3D_FORMAT_FOURCC_BASE + 3},
46 {WINED3DFMT_DXT2, WINED3D_FORMAT_FOURCC_BASE + 4},
47 {WINED3DFMT_DXT3, WINED3D_FORMAT_FOURCC_BASE + 5},
48 {WINED3DFMT_DXT4, WINED3D_FORMAT_FOURCC_BASE + 6},
49 {WINED3DFMT_DXT5, WINED3D_FORMAT_FOURCC_BASE + 7},
50 {WINED3DFMT_MULTI2_ARGB8, WINED3D_FORMAT_FOURCC_BASE + 8},
51 {WINED3DFMT_G8R8_G8B8, WINED3D_FORMAT_FOURCC_BASE + 9},
52 {WINED3DFMT_R8G8_B8G8, WINED3D_FORMAT_FOURCC_BASE + 10},
53 {WINED3DFMT_ATI1N, WINED3D_FORMAT_FOURCC_BASE + 11},
54 {WINED3DFMT_ATI2N, WINED3D_FORMAT_FOURCC_BASE + 12},
55 {WINED3DFMT_INST, WINED3D_FORMAT_FOURCC_BASE + 13},
56 {WINED3DFMT_NVDB, WINED3D_FORMAT_FOURCC_BASE + 14},
57 {WINED3DFMT_NVHU, WINED3D_FORMAT_FOURCC_BASE + 15},
58 {WINED3DFMT_NVHS, WINED3D_FORMAT_FOURCC_BASE + 16},
59 {WINED3DFMT_INTZ, WINED3D_FORMAT_FOURCC_BASE + 17},
60 {WINED3DFMT_RESZ, WINED3D_FORMAT_FOURCC_BASE + 18},
61 {WINED3DFMT_NULL, WINED3D_FORMAT_FOURCC_BASE + 19},
62 {WINED3DFMT_R16, WINED3D_FORMAT_FOURCC_BASE + 20},
63 {WINED3DFMT_AL16, WINED3D_FORMAT_FOURCC_BASE + 21},
64 {WINED3DFMT_NV12, WINED3D_FORMAT_FOURCC_BASE + 22},
65 {WINED3DFMT_ATOC, WINED3D_FORMAT_FOURCC_BASE + 23},
68 #define WINED3D_FORMAT_COUNT (WINED3D_FORMAT_FOURCC_BASE + ARRAY_SIZE(format_index_remap))
70 struct wined3d_format_channels
72 enum wined3d_format_id id;
73 DWORD red_size, green_size, blue_size, alpha_size;
74 DWORD red_offset, green_offset, blue_offset, alpha_offset;
75 UINT bpp;
76 BYTE depth_size, stencil_size;
79 static const struct wined3d_format_channels formats[] =
81 /* size offset
82 * format id r g b a r g b a bpp depth stencil */
83 {WINED3DFMT_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
84 /* FourCC formats */
85 {WINED3DFMT_UYVY, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
86 {WINED3DFMT_YUY2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
87 {WINED3DFMT_YV12, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
88 {WINED3DFMT_NV12, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
89 {WINED3DFMT_DXT1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
90 {WINED3DFMT_DXT2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
91 {WINED3DFMT_DXT3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
92 {WINED3DFMT_DXT4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
93 {WINED3DFMT_DXT5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
94 {WINED3DFMT_MULTI2_ARGB8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
95 {WINED3DFMT_G8R8_G8B8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
96 {WINED3DFMT_R8G8_B8G8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
97 /* Hmm? */
98 {WINED3DFMT_R8G8_SNORM_Cx, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
99 {WINED3DFMT_R11G11B10_FLOAT, 11, 11, 10, 0, 0, 11, 22, 0, 4, 0, 0},
100 /* Palettized formats */
101 {WINED3DFMT_P8_UINT_A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 8, 2, 0, 0},
102 {WINED3DFMT_P8_UINT, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
103 /* Standard ARGB formats. */
104 {WINED3DFMT_B8G8R8_UNORM, 8, 8, 8, 0, 16, 8, 0, 0, 3, 0, 0},
105 {WINED3DFMT_B5G6R5_UNORM, 5, 6, 5, 0, 11, 5, 0, 0, 2, 0, 0},
106 {WINED3DFMT_B5G5R5X1_UNORM, 5, 5, 5, 0, 10, 5, 0, 0, 2, 0, 0},
107 {WINED3DFMT_B5G5R5A1_UNORM, 5, 5, 5, 1, 10, 5, 0, 15, 2, 0, 0},
108 {WINED3DFMT_B4G4R4A4_UNORM, 4, 4, 4, 4, 8, 4, 0, 12, 2, 0, 0},
109 {WINED3DFMT_B2G3R3_UNORM, 3, 3, 2, 0, 5, 2, 0, 0, 1, 0, 0},
110 {WINED3DFMT_A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 0, 1, 0, 0},
111 {WINED3DFMT_B2G3R3A8_UNORM, 3, 3, 2, 8, 5, 2, 0, 8, 2, 0, 0},
112 {WINED3DFMT_B4G4R4X4_UNORM, 4, 4, 4, 0, 8, 4, 0, 0, 2, 0, 0},
113 {WINED3DFMT_R8G8B8X8_UNORM, 8, 8, 8, 0, 0, 8, 16, 0, 4, 0, 0},
114 {WINED3DFMT_B10G10R10A2_UNORM, 10, 10, 10, 2, 20, 10, 0, 30, 4, 0, 0},
115 /* Luminance */
116 {WINED3DFMT_L8_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
117 {WINED3DFMT_L8A8_UNORM, 0, 0, 0, 8, 0, 0, 0, 8, 2, 0, 0},
118 {WINED3DFMT_L4A4_UNORM, 0, 0, 0, 4, 0, 0, 0, 4, 1, 0, 0},
119 {WINED3DFMT_L16_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
120 /* Bump mapping stuff */
121 {WINED3DFMT_R5G5_SNORM_L6_UNORM, 5, 5, 0, 0, 0, 5, 0, 0, 2, 0, 0},
122 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, 8, 8, 0, 0, 0, 8, 0, 0, 4, 0, 0},
123 {WINED3DFMT_R10G11B11_SNORM, 10, 11, 11, 0, 0, 10, 21, 0, 4, 0, 0},
124 {WINED3DFMT_R10G10B10_SNORM_A2_UNORM, 10, 10, 10, 2, 0, 10, 20, 30, 4, 0, 0},
125 /* Depth stencil formats */
126 {WINED3DFMT_D16_LOCKABLE, 0, 0, 0, 0, 0, 0, 0, 0, 2, 16, 0},
127 {WINED3DFMT_D32_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 32, 0},
128 {WINED3DFMT_S1_UINT_D15_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 2, 15, 1},
129 {WINED3DFMT_X8D24_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 0},
130 {WINED3DFMT_S4X4_UINT_D24_UNORM, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 4},
131 {WINED3DFMT_S8_UINT_D24_FLOAT, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 8},
132 /* Vendor-specific formats */
133 {WINED3DFMT_ATI1N, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
134 {WINED3DFMT_ATI2N, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
135 {WINED3DFMT_NVDB, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
136 {WINED3DFMT_ATOC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
137 {WINED3DFMT_INST, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
138 {WINED3DFMT_INTZ, 0, 0, 0, 0, 0, 0, 0, 0, 4, 24, 8},
139 {WINED3DFMT_RESZ, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
140 {WINED3DFMT_NVHU, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
141 {WINED3DFMT_NVHS, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
142 {WINED3DFMT_NULL, 8, 8, 8, 8, 0, 8, 16, 24, 4, 0, 0},
143 /* Unsure about them, could not find a Windows driver that supports them */
144 {WINED3DFMT_R16, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
145 {WINED3DFMT_AL16, 0, 0, 0, 16, 0, 0, 0, 16, 4, 0, 0},
146 /* DirectX 10 HDR formats */
147 {WINED3DFMT_R9G9B9E5_SHAREDEXP, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
148 /* Typeless */
149 {WINED3DFMT_R32G32B32A32_TYPELESS, 32, 32, 32, 32, 0, 32, 64, 96, 16, 0, 0},
150 {WINED3DFMT_R32G32B32_TYPELESS, 32, 32, 32, 0, 0, 32, 64, 0, 12, 0, 0},
151 {WINED3DFMT_R16G16B16A16_TYPELESS, 16, 16, 16, 16, 0, 16, 32, 48, 8, 0, 0},
152 {WINED3DFMT_R32G32_TYPELESS, 32, 32, 0, 0, 0, 32, 0, 0, 8, 0, 0},
153 {WINED3DFMT_R32G8X24_TYPELESS, 32, 8, 0, 0, 0, 0, 0, 0, 8, 0, 0},
154 {WINED3DFMT_R10G10B10A2_TYPELESS, 10, 10, 10, 2, 0, 10, 20, 30, 4, 0, 0},
155 {WINED3DFMT_R10G10B10X2_TYPELESS, 10, 10, 10, 0, 0, 10, 20, 0, 4, 0, 0},
156 {WINED3DFMT_R8G8B8A8_TYPELESS, 8, 8, 8, 8, 0, 8, 16, 24, 4, 0, 0},
157 {WINED3DFMT_R16G16_TYPELESS, 16, 16, 0, 0, 0, 16, 0, 0, 4, 0, 0},
158 {WINED3DFMT_R32_TYPELESS, 32, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
159 {WINED3DFMT_R24G8_TYPELESS, 24, 8, 0, 0, 0, 0, 0, 0, 4, 0, 0},
160 {WINED3DFMT_R8G8_TYPELESS, 8, 8, 0, 0, 0, 8, 0, 0, 2, 0, 0},
161 {WINED3DFMT_R16_TYPELESS, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
162 {WINED3DFMT_R8_TYPELESS, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
163 {WINED3DFMT_BC1_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
164 {WINED3DFMT_BC2_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
165 {WINED3DFMT_BC3_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
166 {WINED3DFMT_BC4_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
167 {WINED3DFMT_BC5_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
168 {WINED3DFMT_BC6H_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
169 {WINED3DFMT_BC7_TYPELESS, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
170 {WINED3DFMT_B8G8R8A8_TYPELESS, 8, 8, 8, 8, 16, 8, 0, 24, 4, 0, 0},
171 {WINED3DFMT_B8G8R8X8_TYPELESS, 8, 8, 8, 0, 16, 8, 0, 0, 4, 0, 0},
174 struct wined3d_typed_format_info
176 enum wined3d_format_id id;
177 enum wined3d_format_id typeless_id;
178 const char *channels;
182 * The last entry for a given typeless format defines its internal format.
184 * u - WINED3D_CHANNEL_TYPE_UNORM
185 * i - WINED3D_CHANNEL_TYPE_SNORM
186 * U - WINED3D_CHANNEL_TYPE_UINT
187 * I - WINED3D_CHANNEL_TYPE_SINT
188 * F - WINED3D_CHANNEL_TYPE_FLOAT
189 * D - WINED3D_CHANNEL_TYPE_DEPTH
190 * S - WINED3D_CHANNEL_TYPE_STENCIL
191 * X - WINED3D_CHANNEL_TYPE_UNUSED
193 static const struct wined3d_typed_format_info typed_formats[] =
195 {WINED3DFMT_R32G32B32A32_UINT, WINED3DFMT_R32G32B32A32_TYPELESS, "UUUU"},
196 {WINED3DFMT_R32G32B32A32_SINT, WINED3DFMT_R32G32B32A32_TYPELESS, "IIII"},
197 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3DFMT_R32G32B32A32_TYPELESS, "FFFF"},
198 {WINED3DFMT_R32G32B32_UINT, WINED3DFMT_R32G32B32_TYPELESS, "UUU"},
199 {WINED3DFMT_R32G32B32_SINT, WINED3DFMT_R32G32B32_TYPELESS, "III"},
200 {WINED3DFMT_R32G32B32_FLOAT, WINED3DFMT_R32G32B32_TYPELESS, "FFF"},
201 {WINED3DFMT_R16G16B16A16_UNORM, WINED3DFMT_R16G16B16A16_TYPELESS, "uuuu"},
202 {WINED3DFMT_R16G16B16A16_SNORM, WINED3DFMT_R16G16B16A16_TYPELESS, "iiii"},
203 {WINED3DFMT_R16G16B16A16_UINT, WINED3DFMT_R16G16B16A16_TYPELESS, "UUUU"},
204 {WINED3DFMT_R16G16B16A16_SINT, WINED3DFMT_R16G16B16A16_TYPELESS, "IIII"},
205 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3DFMT_R16G16B16A16_TYPELESS, "FFFF"},
206 {WINED3DFMT_R32G32_UINT, WINED3DFMT_R32G32_TYPELESS, "UU"},
207 {WINED3DFMT_R32G32_SINT, WINED3DFMT_R32G32_TYPELESS, "II"},
208 {WINED3DFMT_R32G32_FLOAT, WINED3DFMT_R32G32_TYPELESS, "FF"},
209 {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_R32G8X24_TYPELESS, "FXX"},
210 {WINED3DFMT_X32_TYPELESS_G8X24_UINT, WINED3DFMT_R32G8X24_TYPELESS, "XUX"},
211 {WINED3DFMT_D32_FLOAT_S8X24_UINT, WINED3DFMT_R32G8X24_TYPELESS, "DSX"},
212 {WINED3DFMT_R10G10B10A2_SNORM, WINED3DFMT_R10G10B10A2_TYPELESS, "iiii"},
213 {WINED3DFMT_R10G10B10A2_UINT, WINED3DFMT_R10G10B10A2_TYPELESS, "UUUU"},
214 {WINED3DFMT_R10G10B10A2_UNORM, WINED3DFMT_R10G10B10A2_TYPELESS, "uuuu"},
215 {WINED3DFMT_R10G10B10X2_SNORM, WINED3DFMT_R10G10B10X2_TYPELESS, "iiiX"},
216 {WINED3DFMT_R10G10B10X2_UINT, WINED3DFMT_R10G10B10X2_TYPELESS, "UUUX"},
217 {WINED3DFMT_R8G8B8A8_UINT, WINED3DFMT_R8G8B8A8_TYPELESS, "UUUU"},
218 {WINED3DFMT_R8G8B8A8_SINT, WINED3DFMT_R8G8B8A8_TYPELESS, "IIII"},
219 {WINED3DFMT_R8G8B8A8_SNORM, WINED3DFMT_R8G8B8A8_TYPELESS, "iiii"},
220 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, WINED3DFMT_R8G8B8A8_TYPELESS, "uuuu"},
221 {WINED3DFMT_R8G8B8A8_UNORM, WINED3DFMT_R8G8B8A8_TYPELESS, "uuuu"},
222 {WINED3DFMT_R16G16_UNORM, WINED3DFMT_R16G16_TYPELESS, "uu"},
223 {WINED3DFMT_R16G16_SNORM, WINED3DFMT_R16G16_TYPELESS, "ii"},
224 {WINED3DFMT_R16G16_UINT, WINED3DFMT_R16G16_TYPELESS, "UU"},
225 {WINED3DFMT_R16G16_SINT, WINED3DFMT_R16G16_TYPELESS, "II"},
226 {WINED3DFMT_R16G16_FLOAT, WINED3DFMT_R16G16_TYPELESS, "FF"},
227 {WINED3DFMT_D32_FLOAT, WINED3DFMT_R32_TYPELESS, "D"},
228 {WINED3DFMT_R32_FLOAT, WINED3DFMT_R32_TYPELESS, "F"},
229 {WINED3DFMT_R32_UINT, WINED3DFMT_R32_TYPELESS, "U"},
230 {WINED3DFMT_R32_SINT, WINED3DFMT_R32_TYPELESS, "I"},
231 {WINED3DFMT_R24_UNORM_X8_TYPELESS, WINED3DFMT_R24G8_TYPELESS, "uX"},
232 {WINED3DFMT_X24_TYPELESS_G8_UINT, WINED3DFMT_R24G8_TYPELESS, "XU"},
233 {WINED3DFMT_D24_UNORM_S8_UINT, WINED3DFMT_R24G8_TYPELESS, "DS"},
234 {WINED3DFMT_R8G8_SNORM, WINED3DFMT_R8G8_TYPELESS, "ii"},
235 {WINED3DFMT_R8G8_UNORM, WINED3DFMT_R8G8_TYPELESS, "uu"},
236 {WINED3DFMT_R8G8_UINT, WINED3DFMT_R8G8_TYPELESS, "UU"},
237 {WINED3DFMT_R8G8_SINT, WINED3DFMT_R8G8_TYPELESS, "II"},
238 {WINED3DFMT_D16_UNORM, WINED3DFMT_R16_TYPELESS, "D"},
239 {WINED3DFMT_R16_UNORM, WINED3DFMT_R16_TYPELESS, "u"},
240 {WINED3DFMT_R16_SNORM, WINED3DFMT_R16_TYPELESS, "i"},
241 {WINED3DFMT_R16_UINT, WINED3DFMT_R16_TYPELESS, "U"},
242 {WINED3DFMT_R16_SINT, WINED3DFMT_R16_TYPELESS, "I"},
243 {WINED3DFMT_R16_FLOAT, WINED3DFMT_R16_TYPELESS, "F"},
244 {WINED3DFMT_R8_UNORM, WINED3DFMT_R8_TYPELESS, "u"},
245 {WINED3DFMT_R8_SNORM, WINED3DFMT_R8_TYPELESS, "i"},
246 {WINED3DFMT_R8_UINT, WINED3DFMT_R8_TYPELESS, "U"},
247 {WINED3DFMT_R8_SINT, WINED3DFMT_R8_TYPELESS, "I"},
248 {WINED3DFMT_BC1_UNORM_SRGB, WINED3DFMT_BC1_TYPELESS, ""},
249 {WINED3DFMT_BC1_UNORM, WINED3DFMT_BC1_TYPELESS, ""},
250 {WINED3DFMT_BC2_UNORM_SRGB, WINED3DFMT_BC2_TYPELESS, ""},
251 {WINED3DFMT_BC2_UNORM, WINED3DFMT_BC2_TYPELESS, ""},
252 {WINED3DFMT_BC3_UNORM_SRGB, WINED3DFMT_BC3_TYPELESS, ""},
253 {WINED3DFMT_BC3_UNORM, WINED3DFMT_BC3_TYPELESS, ""},
254 {WINED3DFMT_BC4_UNORM, WINED3DFMT_BC4_TYPELESS, ""},
255 {WINED3DFMT_BC4_SNORM, WINED3DFMT_BC4_TYPELESS, ""},
256 {WINED3DFMT_BC5_UNORM, WINED3DFMT_BC5_TYPELESS, ""},
257 {WINED3DFMT_BC5_SNORM, WINED3DFMT_BC5_TYPELESS, ""},
258 {WINED3DFMT_BC6H_UF16, WINED3DFMT_BC6H_TYPELESS, ""},
259 {WINED3DFMT_BC6H_SF16, WINED3DFMT_BC6H_TYPELESS, ""},
260 {WINED3DFMT_BC7_UNORM_SRGB, WINED3DFMT_BC7_TYPELESS, ""},
261 {WINED3DFMT_BC7_UNORM, WINED3DFMT_BC7_TYPELESS, ""},
262 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, WINED3DFMT_B8G8R8A8_TYPELESS, "uuuu"},
263 {WINED3DFMT_B8G8R8A8_UNORM, WINED3DFMT_B8G8R8A8_TYPELESS, "uuuu"},
264 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, WINED3DFMT_B8G8R8X8_TYPELESS, "uuuX"},
265 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8X8_TYPELESS, "uuuX"},
268 struct wined3d_typeless_format_depth_stencil_info
270 enum wined3d_format_id typeless_id;
271 enum wined3d_format_id depth_stencil_id;
272 enum wined3d_format_id depth_view_id;
273 enum wined3d_format_id stencil_view_id;
274 BOOL separate_depth_view_format;
277 static const struct wined3d_typeless_format_depth_stencil_info typeless_depth_stencil_formats[] =
279 {WINED3DFMT_R32G8X24_TYPELESS, WINED3DFMT_D32_FLOAT_S8X24_UINT,
280 WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, WINED3DFMT_X32_TYPELESS_G8X24_UINT, TRUE},
281 {WINED3DFMT_R24G8_TYPELESS, WINED3DFMT_D24_UNORM_S8_UINT,
282 WINED3DFMT_R24_UNORM_X8_TYPELESS, WINED3DFMT_X24_TYPELESS_G8_UINT, TRUE},
283 {WINED3DFMT_R32_TYPELESS, WINED3DFMT_D32_FLOAT, WINED3DFMT_R32_FLOAT},
284 {WINED3DFMT_R16_TYPELESS, WINED3DFMT_D16_UNORM, WINED3DFMT_R16_UNORM},
287 struct wined3d_format_ddi_info
289 enum wined3d_format_id id;
290 D3DDDIFORMAT ddi_format;
293 static const struct wined3d_format_ddi_info ddi_formats[] =
295 {WINED3DFMT_B8G8R8_UNORM, D3DDDIFMT_R8G8B8},
296 {WINED3DFMT_B8G8R8A8_UNORM, D3DDDIFMT_A8R8G8B8},
297 {WINED3DFMT_B8G8R8X8_UNORM, D3DDDIFMT_X8R8G8B8},
298 {WINED3DFMT_B5G6R5_UNORM, D3DDDIFMT_R5G6B5},
299 {WINED3DFMT_B5G5R5X1_UNORM, D3DDDIFMT_X1R5G5B5},
300 {WINED3DFMT_B5G5R5A1_UNORM, D3DDDIFMT_A1R5G5B5},
301 {WINED3DFMT_B4G4R4A4_UNORM, D3DDDIFMT_A4R4G4B4},
302 {WINED3DFMT_B4G4R4X4_UNORM, D3DDDIFMT_X4R4G4B4},
303 {WINED3DFMT_P8_UINT, D3DDDIFMT_P8},
306 struct wined3d_format_base_flags
308 enum wined3d_format_id id;
309 DWORD flags;
312 /* The ATI2N format behaves like an uncompressed format in LockRect(), but
313 * still needs to use the correct block based calculation for e.g. the
314 * resource size. */
315 static const struct wined3d_format_base_flags format_base_flags[] =
317 {WINED3DFMT_ATI1N, WINED3DFMT_FLAG_MAPPABLE | WINED3DFMT_FLAG_BROKEN_PITCH},
318 {WINED3DFMT_ATI2N, WINED3DFMT_FLAG_MAPPABLE | WINED3DFMT_FLAG_BROKEN_PITCH},
319 {WINED3DFMT_D16_LOCKABLE, WINED3DFMT_FLAG_MAPPABLE},
320 {WINED3DFMT_INTZ, WINED3DFMT_FLAG_MAPPABLE},
321 {WINED3DFMT_R11G11B10_FLOAT, WINED3DFMT_FLAG_FLOAT},
322 {WINED3DFMT_D32_FLOAT, WINED3DFMT_FLAG_FLOAT},
323 {WINED3DFMT_S8_UINT_D24_FLOAT, WINED3DFMT_FLAG_FLOAT},
324 {WINED3DFMT_D32_FLOAT_S8X24_UINT, WINED3DFMT_FLAG_FLOAT},
325 {WINED3DFMT_INST, WINED3DFMT_FLAG_EXTENSION},
326 {WINED3DFMT_NULL, WINED3DFMT_FLAG_EXTENSION},
327 {WINED3DFMT_NVDB, WINED3DFMT_FLAG_EXTENSION},
328 {WINED3DFMT_ATOC, WINED3DFMT_FLAG_EXTENSION},
329 {WINED3DFMT_RESZ, WINED3DFMT_FLAG_EXTENSION},
330 {WINED3DFMT_R32G32B32A32_TYPELESS, WINED3DFMT_FLAG_CAST_TO_BLOCK},
331 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
332 {WINED3DFMT_R32G32B32A32_UINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
333 {WINED3DFMT_R32G32B32A32_SINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
334 {WINED3DFMT_R16G16B16A16_TYPELESS, WINED3DFMT_FLAG_CAST_TO_BLOCK},
335 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
336 {WINED3DFMT_R16G16B16A16_UNORM, WINED3DFMT_FLAG_CAST_TO_BLOCK},
337 {WINED3DFMT_R16G16B16A16_UINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
338 {WINED3DFMT_R16G16B16A16_SNORM, WINED3DFMT_FLAG_CAST_TO_BLOCK},
339 {WINED3DFMT_R16G16B16A16_SINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
340 {WINED3DFMT_R32G32_TYPELESS, WINED3DFMT_FLAG_CAST_TO_BLOCK},
341 {WINED3DFMT_R32G32_FLOAT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
342 {WINED3DFMT_R32G32_UINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
343 {WINED3DFMT_R32G32_SINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
344 {WINED3DFMT_R32_TYPELESS, WINED3DFMT_FLAG_CAST_TO_BLOCK},
345 {WINED3DFMT_R32_FLOAT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
346 {WINED3DFMT_R32_UINT, WINED3DFMT_FLAG_CAST_TO_BLOCK | WINED3DFMT_FLAG_INDEX_BUFFER},
347 {WINED3DFMT_R32_SINT, WINED3DFMT_FLAG_CAST_TO_BLOCK},
348 {WINED3DFMT_R16_UINT, WINED3DFMT_FLAG_INDEX_BUFFER},
351 static void rgb888_from_rgb565(WORD rgb565, BYTE *r, BYTE *g, BYTE *b)
353 BYTE c;
355 /* (2⁸ - 1) / (2⁵ - 1) ≈ 2⁸ / 2⁵ + 2⁸ / 2¹⁰
356 * (2⁸ - 1) / (2⁶ - 1) ≈ 2⁸ / 2⁶ + 2⁸ / 2¹² */
357 c = rgb565 >> 11;
358 *r = (c << 3) + (c >> 2);
359 c = (rgb565 >> 5) & 0x3f;
360 *g = (c << 2) + (c >> 4);
361 c = rgb565 & 0x1f;
362 *b = (c << 3) + (c >> 2);
365 static void build_dxtn_colour_table(WORD colour0, WORD colour1,
366 DWORD colour_table[4], enum wined3d_format_id format_id)
368 unsigned int i;
369 struct
371 BYTE r, g, b;
372 } c[4];
374 rgb888_from_rgb565(colour0, &c[0].r, &c[0].g, &c[0].b);
375 rgb888_from_rgb565(colour1, &c[1].r, &c[1].g, &c[1].b);
377 if (format_id == WINED3DFMT_BC1_UNORM && colour0 <= colour1)
379 c[2].r = (c[0].r + c[1].r) / 2;
380 c[2].g = (c[0].g + c[1].g) / 2;
381 c[2].b = (c[0].b + c[1].b) / 2;
383 c[3].r = 0;
384 c[3].g = 0;
385 c[3].b = 0;
387 else
389 for (i = 0; i < 2; ++i)
391 c[i + 2].r = (2 * c[i].r + c[1 - i].r) / 3;
392 c[i + 2].g = (2 * c[i].g + c[1 - i].g) / 3;
393 c[i + 2].b = (2 * c[i].b + c[1 - i].b) / 3;
397 for (i = 0; i < 4; ++i)
399 colour_table[i] = (c[i].r << 16) | (c[i].g << 8) | c[i].b;
403 static void build_bc3_alpha_table(BYTE alpha0, BYTE alpha1, BYTE alpha_table[8])
405 unsigned int i;
407 alpha_table[0] = alpha0;
408 alpha_table[1] = alpha1;
410 if (alpha0 > alpha1)
412 for (i = 0; i < 6; ++i)
414 alpha_table[2 + i] = ((6 - i) * alpha0 + (i + 1) * alpha1) / 7;
416 return;
418 else
420 for (i = 0; i < 4; ++i)
422 alpha_table[2 + i] = ((4 - i) * alpha0 + (i + 1) * alpha1) / 5;
424 alpha_table[6] = 0x00;
425 alpha_table[7] = 0xff;
429 static void decompress_dxtn_block(const BYTE *src, BYTE *dst, unsigned int width,
430 unsigned int height, unsigned int dst_row_pitch, enum wined3d_format_id format_id)
432 const UINT64 *s = (const UINT64 *)src;
433 BOOL bc1_alpha = FALSE;
434 DWORD colour_table[4];
435 BYTE alpha_table[8];
436 UINT64 alpha_bits;
437 DWORD colour_bits;
438 unsigned int x, y;
439 BYTE colour_idx;
440 DWORD *dst_row;
441 BYTE alpha;
443 if (format_id == WINED3DFMT_BC1_UNORM)
445 WORD colour0, colour1;
447 alpha_bits = 0;
449 colour0 = s[0] & 0xffff;
450 colour1 = (s[0] >> 16) & 0xffff;
451 colour_bits = (s[0] >> 32) & 0xffffffff;
452 build_dxtn_colour_table(colour0, colour1, colour_table, format_id);
453 if (colour0 <= colour1)
454 bc1_alpha = TRUE;
456 else
458 alpha_bits = s[0];
459 if (format_id == WINED3DFMT_BC3_UNORM)
461 build_bc3_alpha_table(alpha_bits & 0xff, (alpha_bits >> 8) & 0xff, alpha_table);
462 alpha_bits >>= 16;
465 colour_bits = (s[1] >> 32) & 0xffffffff;
466 build_dxtn_colour_table(s[1] & 0xffff, (s[1] >> 16) & 0xffff, colour_table, format_id);
469 for (y = 0; y < height; ++y)
471 dst_row = (DWORD *)&dst[y * dst_row_pitch];
472 for (x = 0; x < width; ++x)
474 colour_idx = (colour_bits >> (y * 8 + x * 2)) & 0x3;
475 switch (format_id)
477 case WINED3DFMT_BC1_UNORM:
478 alpha = bc1_alpha && colour_idx == 3 ? 0x00 : 0xff;
479 break;
481 case WINED3DFMT_BC2_UNORM:
482 alpha = (alpha_bits >> (y * 16 + x * 4)) & 0xf;
483 /* (2⁸ - 1) / (2⁴ - 1) ≈ 2⁸ / 2⁴ + 2⁸ / 2⁸ */
484 alpha |= alpha << 4;
485 break;
487 case WINED3DFMT_BC3_UNORM:
488 alpha = alpha_table[(alpha_bits >> (y * 12 + x * 3)) & 0x7];
489 break;
491 default:
492 alpha = 0xff;
493 break;
495 dst_row[x] = (alpha << 24) | colour_table[colour_idx];
500 static void decompress_dxtn(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
501 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
502 unsigned int width, unsigned int height, unsigned int depth, enum wined3d_format_id format_id)
504 unsigned int block_byte_count, block_w, block_h;
505 const BYTE *src_row, *src_slice = src;
506 BYTE *dst_row, *dst_slice = dst;
507 unsigned int x, y, z;
509 block_byte_count = format_id == WINED3DFMT_BC1_UNORM ? 8 : 16;
511 for (z = 0; z < depth; ++z)
513 src_row = src_slice;
514 dst_row = dst_slice;
515 for (y = 0; y < height; y += 4)
517 for (x = 0; x < width; x += 4)
519 block_w = min(width - x, 4);
520 block_h = min(height - y, 4);
521 decompress_dxtn_block(&src_row[x * (block_byte_count / 4)],
522 &dst_row[x * 4], block_w, block_h, dst_row_pitch, format_id);
524 src_row += src_row_pitch;
525 dst_row += dst_row_pitch * 4;
527 src_slice += src_slice_pitch;
528 dst_slice += dst_slice_pitch;
532 static void decompress_bc3(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
533 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
534 unsigned int width, unsigned int height, unsigned int depth)
536 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
537 dst_slice_pitch, width, height, depth, WINED3DFMT_BC3_UNORM);
540 static void decompress_bc2(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
541 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
542 unsigned int width, unsigned int height, unsigned int depth)
544 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
545 dst_slice_pitch, width, height, depth, WINED3DFMT_BC2_UNORM);
548 static void decompress_bc1(const BYTE *src, BYTE *dst, unsigned int src_row_pitch,
549 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
550 unsigned int width, unsigned int height, unsigned int depth)
552 decompress_dxtn(src, dst, src_row_pitch, src_slice_pitch, dst_row_pitch,
553 dst_slice_pitch, width, height, depth, WINED3DFMT_BC1_UNORM);
556 static void build_rgtc_colour_table(uint8_t red0, uint8_t red1, uint8_t colour_table[8])
558 unsigned int i;
560 colour_table[0] = red0;
561 colour_table[1] = red1;
562 if (red0 <= red1)
564 for (i = 0; i < 4; ++i)
566 colour_table[i + 2] = ((8 - 2 * i) * red0 + (2 + 2 * i) * red1 + 5) / 10;
568 colour_table[6] = 0x00;
569 colour_table[7] = 0xff;
571 else
573 for (i = 0; i < 6; ++i)
575 colour_table[i + 2] = ((12 - 2 * i) * red0 + (2 + 2 * i) * red1 + 7) / 14;
580 static void decompress_rgtc_block(const uint8_t *src, uint8_t *dst,
581 unsigned int width, unsigned int height, unsigned int dst_row_pitch)
583 const uint64_t *s = (const uint64_t *)src;
584 uint8_t red0, red1, red_idx;
585 uint8_t colour_table[8];
586 unsigned int x, y;
587 uint32_t *dst_row;
588 uint64_t bits;
590 red0 = s[0] & 0xff;
591 red1 = (s[0] >> 8) & 0xff;
592 bits = s[0] >> 16;
593 build_rgtc_colour_table(red0, red1, colour_table);
595 for (y = 0; y < height; ++y)
597 dst_row = (uint32_t *)&dst[y * dst_row_pitch];
598 for (x = 0; x < width; ++x)
600 red_idx = (bits >> (y * 12 + x * 3)) & 0x7;
601 /* Decompressing to bgra32 is perhaps not ideal for RGTC formats.
602 * It's convenient though. */
603 dst_row[x] = 0xff000000 | (colour_table[red_idx] << 16);
608 static void decompress_bc4(const uint8_t *src, uint8_t *dst, unsigned int src_row_pitch,
609 unsigned int src_slice_pitch, unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
610 unsigned int width, unsigned int height, unsigned int depth)
612 unsigned int block_w, block_h, x, y, z;
613 const uint8_t *src_row, *src_slice;
614 uint8_t *dst_row, *dst_slice;
616 for (z = 0; z < depth; ++z)
618 src_slice = &src[z * src_slice_pitch];
619 dst_slice = &dst[z * dst_slice_pitch];
620 for (y = 0; y < height; y += 4)
622 src_row = &src_slice[(y / 4) * src_row_pitch];
623 dst_row = &dst_slice[y * dst_row_pitch];
624 for (x = 0; x < width; x += 4)
626 block_w = min(width - x, 4);
627 block_h = min(height - y, 4);
628 decompress_rgtc_block(&src_row[(x / 4) * 8], &dst_row[x * 4], block_w, block_h, dst_row_pitch);
634 static const struct wined3d_format_decompress_info
636 enum wined3d_format_id id;
637 void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
638 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
639 unsigned int width, unsigned int height, unsigned int depth);
641 format_decompress_info[] =
643 {WINED3DFMT_DXT1, decompress_bc1},
644 {WINED3DFMT_DXT2, decompress_bc2},
645 {WINED3DFMT_DXT3, decompress_bc2},
646 {WINED3DFMT_DXT4, decompress_bc3},
647 {WINED3DFMT_DXT5, decompress_bc3},
648 {WINED3DFMT_BC1_UNORM, decompress_bc1},
649 {WINED3DFMT_BC2_UNORM, decompress_bc2},
650 {WINED3DFMT_BC3_UNORM, decompress_bc3},
651 {WINED3DFMT_BC4_UNORM, decompress_bc4},
654 struct wined3d_format_block_info
656 enum wined3d_format_id id;
657 UINT block_width;
658 UINT block_height;
659 UINT block_byte_count;
660 unsigned int flags;
663 static const struct wined3d_format_block_info format_block_info[] =
665 {WINED3DFMT_DXT1, 4, 4, 8, WINED3DFMT_FLAG_COMPRESSED},
666 {WINED3DFMT_DXT2, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
667 {WINED3DFMT_DXT3, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
668 {WINED3DFMT_DXT4, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
669 {WINED3DFMT_DXT5, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
670 {WINED3DFMT_BC1_UNORM, 4, 4, 8, WINED3DFMT_FLAG_COMPRESSED},
671 {WINED3DFMT_BC2_UNORM, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
672 {WINED3DFMT_BC3_UNORM, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
673 {WINED3DFMT_BC4_UNORM, 4, 4, 8, WINED3DFMT_FLAG_COMPRESSED},
674 {WINED3DFMT_BC4_SNORM, 4, 4, 8, WINED3DFMT_FLAG_COMPRESSED},
675 {WINED3DFMT_BC5_UNORM, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
676 {WINED3DFMT_BC5_SNORM, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
677 {WINED3DFMT_BC6H_UF16, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
678 {WINED3DFMT_BC6H_SF16, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
679 {WINED3DFMT_BC7_UNORM, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED},
680 {WINED3DFMT_ATI1N, 4, 4, 8, WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY},
681 {WINED3DFMT_ATI2N, 4, 4, 16, WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY},
682 {WINED3DFMT_YUY2, 2, 1, 4, WINED3DFMT_FLAG_BLOCKS_NO_VERIFY},
683 {WINED3DFMT_UYVY, 2, 1, 4, WINED3DFMT_FLAG_BLOCKS_NO_VERIFY},
684 {WINED3DFMT_R9G9B9E5_SHAREDEXP, 1, 1, 4},
687 struct wined3d_format_vertex_info
689 enum wined3d_format_id id;
690 enum wined3d_ffp_emit_idx emit_idx;
691 GLenum gl_vtx_type;
692 enum wined3d_gl_extension extension;
695 static const struct wined3d_format_vertex_info format_vertex_info[] =
697 {WINED3DFMT_R32_FLOAT, WINED3D_FFP_EMIT_FLOAT1, GL_FLOAT},
698 {WINED3DFMT_R32G32_FLOAT, WINED3D_FFP_EMIT_FLOAT2, GL_FLOAT},
699 {WINED3DFMT_R32G32B32_FLOAT, WINED3D_FFP_EMIT_FLOAT3, GL_FLOAT},
700 {WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_FFP_EMIT_FLOAT4, GL_FLOAT},
701 {WINED3DFMT_B8G8R8A8_UNORM, WINED3D_FFP_EMIT_D3DCOLOR, GL_UNSIGNED_BYTE},
702 {WINED3DFMT_R8G8B8A8_UINT, WINED3D_FFP_EMIT_UBYTE4, GL_UNSIGNED_BYTE},
703 {WINED3DFMT_R16G16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
704 {WINED3DFMT_R16G16_SINT, WINED3D_FFP_EMIT_SHORT2, GL_SHORT},
705 {WINED3DFMT_R16G16B16A16_SINT, WINED3D_FFP_EMIT_SHORT4, GL_SHORT},
706 {WINED3DFMT_R8G8B8A8_UNORM, WINED3D_FFP_EMIT_UBYTE4N, GL_UNSIGNED_BYTE},
707 {WINED3DFMT_R16G16_SNORM, WINED3D_FFP_EMIT_SHORT2N, GL_SHORT},
708 {WINED3DFMT_R16G16B16A16_SNORM, WINED3D_FFP_EMIT_SHORT4N, GL_SHORT},
709 {WINED3DFMT_R16G16_UNORM, WINED3D_FFP_EMIT_USHORT2N, GL_UNSIGNED_SHORT},
710 {WINED3DFMT_R16G16B16A16_UNORM, WINED3D_FFP_EMIT_USHORT4N, GL_UNSIGNED_SHORT},
711 {WINED3DFMT_R11G11B10_FLOAT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT_10F_11F_11F_REV,
712 ARB_VERTEX_TYPE_10F_11F_11F_REV},
713 {WINED3DFMT_R10G10B10X2_UINT, WINED3D_FFP_EMIT_UDEC3, GL_UNSIGNED_SHORT},
714 {WINED3DFMT_R10G10B10X2_SNORM, WINED3D_FFP_EMIT_DEC3N, GL_SHORT},
715 {WINED3DFMT_R10G10B10A2_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT_2_10_10_10_REV,
716 ARB_VERTEX_TYPE_2_10_10_10_REV},
717 /* Without ARB_half_float_vertex we convert these on upload. */
718 {WINED3DFMT_R16G16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_2, GL_FLOAT},
719 {WINED3DFMT_R16G16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_2, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
720 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_FLOAT},
721 {WINED3DFMT_R16G16B16A16_FLOAT, WINED3D_FFP_EMIT_FLOAT16_4, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
722 {WINED3DFMT_R8G8B8A8_SNORM, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
723 {WINED3DFMT_R8G8B8A8_SINT, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
724 {WINED3DFMT_R8G8_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
725 {WINED3DFMT_R16G16B16A16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
726 {WINED3DFMT_R8_UNORM, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
727 {WINED3DFMT_R8_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
728 {WINED3DFMT_R8_SINT, WINED3D_FFP_EMIT_INVALID, GL_BYTE},
729 {WINED3DFMT_R8G8_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_BYTE},
730 {WINED3DFMT_R16_FLOAT, WINED3D_FFP_EMIT_INVALID, GL_HALF_FLOAT, ARB_HALF_FLOAT_VERTEX},
731 {WINED3DFMT_R16_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_SHORT},
732 {WINED3DFMT_R16_SINT, WINED3D_FFP_EMIT_INVALID, GL_SHORT},
733 {WINED3DFMT_R32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
734 {WINED3DFMT_R32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
735 {WINED3DFMT_R32G32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
736 {WINED3DFMT_R32G32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
737 {WINED3DFMT_R32G32B32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
738 {WINED3DFMT_R32G32B32A32_UINT, WINED3D_FFP_EMIT_INVALID, GL_UNSIGNED_INT},
739 {WINED3DFMT_R32G32B32A32_SINT, WINED3D_FFP_EMIT_INVALID, GL_INT},
742 struct wined3d_format_texture_info
744 enum wined3d_format_id id;
745 GLint gl_internal;
746 GLint gl_srgb_internal;
747 GLint gl_rt_internal;
748 GLint gl_format;
749 GLint gl_type;
750 unsigned int conv_byte_count;
751 unsigned int flags;
752 enum wined3d_gl_extension extension;
753 void (*upload)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
754 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
755 unsigned int width, unsigned int height, unsigned int depth);
756 void (*download)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
757 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
758 unsigned int width, unsigned int height, unsigned int depth);
759 void (*decompress)(const BYTE *src, BYTE *dst, unsigned int src_row_pitch, unsigned int src_slice_pitch,
760 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
761 unsigned int width, unsigned int height, unsigned int depth);
764 static void convert_l4a4_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
765 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
767 /* WINED3DFMT_L4A4_UNORM exists as an internal gl format, but for some reason there is not
768 * format+type combination to load it. Thus convert it to A8L8, then load it
769 * with A4L4 internal, but A8L8 format+type
771 unsigned int x, y, z;
772 const unsigned char *Source;
773 unsigned char *Dest;
775 for (z = 0; z < depth; z++)
777 for (y = 0; y < height; y++)
779 Source = src + z * src_slice_pitch + y * src_row_pitch;
780 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
781 for (x = 0; x < width; x++ )
783 unsigned char color = (*Source++);
784 /* A */ Dest[1] = (color & 0xf0u) << 0;
785 /* L */ Dest[0] = (color & 0x0fu) << 4;
786 Dest += 2;
792 static void convert_r5g5_snorm_l6_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
793 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
795 unsigned int x, y, z;
796 unsigned char r_in, g_in, l_in;
797 const unsigned short *texel_in;
798 unsigned short *texel_out;
800 /* Emulating signed 5 bit values with unsigned 5 bit values has some precision problems by design:
801 * E.g. the signed input value 0 becomes 16. GL normalizes it to 16 / 31 = 0.516. We convert it
802 * back to a signed value by subtracting 0.5 and multiplying by 2.0. The resulting value is
803 * ((16 / 31) - 0.5) * 2.0 = 0.032, which is quite different from the intended result 0.000. */
804 for (z = 0; z < depth; z++)
806 for (y = 0; y < height; y++)
808 texel_out = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
809 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
810 for (x = 0; x < width; x++ )
812 l_in = (*texel_in & 0xfc00u) >> 10;
813 g_in = (*texel_in & 0x03e0u) >> 5;
814 r_in = *texel_in & 0x001fu;
816 *texel_out = ((r_in + 16) << 11) | (l_in << 5) | (g_in + 16);
817 texel_out++;
818 texel_in++;
824 static void convert_r5g5_snorm_l6_unorm_ext(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
825 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
827 unsigned int x, y, z;
828 unsigned char *texel_out, r_out, g_out, r_in, g_in, l_in;
829 const unsigned short *texel_in;
831 for (z = 0; z < depth; z++)
833 for (y = 0; y < height; y++)
835 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
836 texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
837 for (x = 0; x < width; x++ )
839 l_in = (*texel_in & 0xfc00u) >> 10;
840 g_in = (*texel_in & 0x03e0u) >> 5;
841 r_in = *texel_in & 0x001fu;
843 r_out = r_in << 3;
844 if (!(r_in & 0x10)) /* r > 0 */
845 r_out |= r_in >> 1;
847 g_out = g_in << 3;
848 if (!(g_in & 0x10)) /* g > 0 */
849 g_out |= g_in >> 1;
851 texel_out[0] = r_out;
852 texel_out[1] = g_out;
853 texel_out[2] = l_in << 1 | l_in >> 5;
854 texel_out[3] = 0;
856 texel_out += 4;
857 texel_in++;
863 static void convert_r5g5_snorm_l6_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
864 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
866 unsigned int x, y, z;
867 unsigned char *texel_out, ds_out, dt_out, r_in, g_in, l_in;
868 const unsigned short *texel_in;
870 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
871 * fixed function and shaders without further conversion once the surface is
872 * loaded.
874 * The difference between this function and convert_r5g5_snorm_l6_unorm_ext
875 * is that convert_r5g5_snorm_l6_unorm_ext creates a 32 bit XRGB texture and
876 * this function creates a 24 bit DSDT_MAG texture. Trying to load a DSDT_MAG
877 * internal with a 32 bit DSDT_MAG_INTENSITY or DSDT_MAG_VIB format fails. */
878 for (z = 0; z < depth; z++)
880 for (y = 0; y < height; y++)
882 texel_in = (const unsigned short *)(src + z * src_slice_pitch + y * src_row_pitch);
883 texel_out = dst + z * dst_slice_pitch + y * dst_row_pitch;
884 for (x = 0; x < width; x++ )
886 l_in = (*texel_in & 0xfc00u) >> 10;
887 g_in = (*texel_in & 0x03e0u) >> 5;
888 r_in = *texel_in & 0x001fu;
890 ds_out = r_in << 3;
891 if (!(r_in & 0x10)) /* r > 0 */
892 ds_out |= r_in >> 1;
894 dt_out = g_in << 3;
895 if (!(g_in & 0x10)) /* g > 0 */
896 dt_out |= g_in >> 1;
898 texel_out[0] = ds_out;
899 texel_out[1] = dt_out;
900 texel_out[2] = l_in << 1 | l_in >> 5;
902 texel_out += 3;
903 texel_in++;
909 static void convert_r8g8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
910 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
912 unsigned int x, y, z;
913 const short *Source;
914 unsigned char *Dest;
916 for (z = 0; z < depth; z++)
918 for (y = 0; y < height; y++)
920 Source = (const short *)(src + z * src_slice_pitch + y * src_row_pitch);
921 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
922 for (x = 0; x < width; x++ )
924 const short color = (*Source++);
925 /* B */ Dest[0] = 0xff;
926 /* G */ Dest[1] = (color >> 8) + 128; /* V */
927 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
928 Dest += 3;
934 static void convert_r8g8_snorm_l8x8_unorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
935 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
937 unsigned int x, y, z;
938 const DWORD *Source;
939 unsigned char *Dest;
941 /* Doesn't work correctly with the fixed function pipeline, but can work in
942 * shaders if the shader is adjusted. (There's no use for this format in gl's
943 * standard fixed function pipeline anyway).
945 for (z = 0; z < depth; z++)
947 for (y = 0; y < height; y++)
949 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
950 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
951 for (x = 0; x < width; x++ )
953 LONG color = (*Source++);
954 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
955 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
956 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
957 Dest += 4;
963 static void convert_r8g8_snorm_l8x8_unorm_nv(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
964 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
966 unsigned int x, y, z;
967 const DWORD *Source;
968 unsigned char *Dest;
970 /* This implementation works with the fixed function pipeline and shaders
971 * without further modification after converting the surface.
973 for (z = 0; z < depth; z++)
975 for (y = 0; y < height; y++)
977 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
978 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
979 for (x = 0; x < width; x++ )
981 LONG color = (*Source++);
982 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
983 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
984 /* U */ Dest[0] = (color & 0xff); /* U */
985 /* I */ Dest[3] = 255; /* X */
986 Dest += 4;
992 static void convert_r8g8b8a8_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
993 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
995 unsigned int x, y, z;
996 const DWORD *Source;
997 unsigned char *Dest;
999 for (z = 0; z < depth; z++)
1001 for (y = 0; y < height; y++)
1003 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1004 Dest = dst + z * dst_slice_pitch + y * dst_row_pitch;
1005 for (x = 0; x < width; x++ )
1007 LONG color = (*Source++);
1008 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1009 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1010 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1011 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1012 Dest += 4;
1018 static void convert_r16g16_snorm(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1019 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1021 unsigned int x, y, z;
1022 const DWORD *Source;
1023 unsigned short *Dest;
1025 for (z = 0; z < depth; z++)
1027 for (y = 0; y < height; y++)
1029 Source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1030 Dest = (unsigned short *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1031 for (x = 0; x < width; x++ )
1033 const DWORD color = (*Source++);
1034 /* B */ Dest[0] = 0xffff;
1035 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1036 /* R */ Dest[2] = (color & 0xffff) + 32768; /* U */
1037 Dest += 3;
1043 static void convert_r16g16(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1044 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1046 unsigned int x, y, z;
1047 const WORD *Source;
1048 WORD *Dest;
1050 for (z = 0; z < depth; z++)
1052 for (y = 0; y < height; y++)
1054 Source = (const WORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1055 Dest = (WORD *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1056 for (x = 0; x < width; x++ )
1058 WORD green = (*Source++);
1059 WORD red = (*Source++);
1060 Dest[0] = green;
1061 Dest[1] = red;
1062 /* Strictly speaking not correct for R16G16F, but it doesn't matter because the
1063 * shader overwrites it anyway */
1064 Dest[2] = 0xffff;
1065 Dest += 3;
1071 static void convert_r32g32_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1072 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1074 unsigned int x, y, z;
1075 const float *Source;
1076 float *Dest;
1078 for (z = 0; z < depth; z++)
1080 for (y = 0; y < height; y++)
1082 Source = (const float *)(src + z * src_slice_pitch + y * src_row_pitch);
1083 Dest = (float *) (dst + z * dst_slice_pitch + y * dst_row_pitch);
1084 for (x = 0; x < width; x++ )
1086 float green = (*Source++);
1087 float red = (*Source++);
1088 Dest[0] = green;
1089 Dest[1] = red;
1090 Dest[2] = 1.0f;
1091 Dest += 3;
1097 static void convert_s8_uint_d24_float(const BYTE *src, BYTE *dst, UINT src_row_pitch, UINT src_slice_pitch,
1098 UINT dst_row_pitch, UINT dst_slice_pitch, UINT width, UINT height, UINT depth)
1100 unsigned int x, y, z;
1102 for (z = 0; z < depth; z++)
1104 for (y = 0; y < height; ++y)
1106 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1107 float *dest_f = (float *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1108 DWORD *dest_s = (DWORD *)dest_f;
1110 for (x = 0; x < width; ++x)
1112 dest_f[x * 2] = float_24_to_32((source[x] & 0xffffff00u) >> 8);
1113 dest_s[x * 2 + 1] = source[x] & 0xff;
1119 static void x8_d24_unorm_upload(const BYTE *src, BYTE *dst,
1120 unsigned int src_row_pitch, unsigned int src_slice_pitch,
1121 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
1122 unsigned int width, unsigned int height, unsigned int depth)
1124 unsigned int x, y, z;
1126 for (z = 0; z < depth; ++z)
1128 for (y = 0; y < height; ++y)
1130 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1131 DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1133 for (x = 0; x < width; ++x)
1135 dest[x] = source[x] << 8 | ((source[x] >> 16) & 0xff);
1141 static void x8_d24_unorm_download(const BYTE *src, BYTE *dst,
1142 unsigned int src_row_pitch, unsigned int src_slice_pitch,
1143 unsigned int dst_row_pitch, unsigned int dst_slice_pitch,
1144 unsigned int width, unsigned int height, unsigned int depth)
1146 unsigned int x, y, z;
1148 for (z = 0; z < depth; ++z)
1150 for (y = 0; y < height; ++y)
1152 const DWORD *source = (const DWORD *)(src + z * src_slice_pitch + y * src_row_pitch);
1153 DWORD *dest = (DWORD *)(dst + z * dst_slice_pitch + y * dst_row_pitch);
1155 for (x = 0; x < width; ++x)
1157 dest[x] = source[x] >> 8;
1163 static BOOL color_in_range(const struct wined3d_color_key *color_key, DWORD color)
1165 /* FIXME: Is this really how color keys are supposed to work? I think it
1166 * makes more sense to compare the individual channels. */
1167 return color >= color_key->color_space_low_value
1168 && color <= color_key->color_space_high_value;
1171 static void convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1172 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1173 const struct wined3d_color_key *color_key)
1175 const WORD *src_row;
1176 unsigned int x, y;
1177 WORD *dst_row;
1179 for (y = 0; y < height; ++y)
1181 src_row = (WORD *)&src[src_pitch * y];
1182 dst_row = (WORD *)&dst[dst_pitch * y];
1183 for (x = 0; x < width; ++x)
1185 WORD src_color = src_row[x];
1186 if (!color_in_range(color_key, src_color))
1187 dst_row[x] = 0x8000u | ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1188 else
1189 dst_row[x] = ((src_color & 0xffc0u) >> 1) | (src_color & 0x1fu);
1194 static void convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1195 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1196 const struct wined3d_color_key *color_key)
1198 const WORD *src_row;
1199 unsigned int x, y;
1200 WORD *dst_row;
1202 for (y = 0; y < height; ++y)
1204 src_row = (WORD *)&src[src_pitch * y];
1205 dst_row = (WORD *)&dst[dst_pitch * y];
1206 for (x = 0; x < width; ++x)
1208 WORD src_color = src_row[x];
1209 if (color_in_range(color_key, src_color))
1210 dst_row[x] = src_color & ~0x8000;
1211 else
1212 dst_row[x] = src_color | 0x8000;
1217 static void convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1218 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1219 const struct wined3d_color_key *color_key)
1221 const BYTE *src_row;
1222 unsigned int x, y;
1223 DWORD *dst_row;
1225 for (y = 0; y < height; ++y)
1227 src_row = &src[src_pitch * y];
1228 dst_row = (DWORD *)&dst[dst_pitch * y];
1229 for (x = 0; x < width; ++x)
1231 DWORD src_color = (src_row[x * 3 + 2] << 16) | (src_row[x * 3 + 1] << 8) | src_row[x * 3];
1232 if (!color_in_range(color_key, src_color))
1233 dst_row[x] = src_color | 0xff000000;
1238 static void convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1239 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1240 const struct wined3d_color_key *color_key)
1242 const DWORD *src_row;
1243 unsigned int x, y;
1244 DWORD *dst_row;
1246 for (y = 0; y < height; ++y)
1248 src_row = (DWORD *)&src[src_pitch * y];
1249 dst_row = (DWORD *)&dst[dst_pitch * y];
1250 for (x = 0; x < width; ++x)
1252 DWORD src_color = src_row[x];
1253 if (color_in_range(color_key, src_color))
1254 dst_row[x] = src_color & ~0xff000000;
1255 else
1256 dst_row[x] = src_color | 0xff000000;
1261 static void convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key(const BYTE *src, unsigned int src_pitch,
1262 BYTE *dst, unsigned int dst_pitch, unsigned int width, unsigned int height,
1263 const struct wined3d_color_key *color_key)
1265 const DWORD *src_row;
1266 unsigned int x, y;
1267 DWORD *dst_row;
1269 for (y = 0; y < height; ++y)
1271 src_row = (DWORD *)&src[src_pitch * y];
1272 dst_row = (DWORD *)&dst[dst_pitch * y];
1273 for (x = 0; x < width; ++x)
1275 DWORD src_color = src_row[x];
1276 if (color_in_range(color_key, src_color))
1277 src_color &= ~0xff000000;
1278 dst_row[x] = src_color;
1283 const struct wined3d_color_key_conversion * wined3d_format_get_color_key_conversion(
1284 const struct wined3d_texture *texture, BOOL need_alpha_ck)
1286 const struct wined3d_format *format = texture->resource.format;
1287 unsigned int i;
1289 static const struct
1291 enum wined3d_format_id src_format;
1292 struct wined3d_color_key_conversion conversion;
1294 color_key_info[] =
1296 {WINED3DFMT_B5G6R5_UNORM, {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g6r5_unorm_b5g5r5a1_unorm_color_key }},
1297 {WINED3DFMT_B5G5R5X1_UNORM, {WINED3DFMT_B5G5R5A1_UNORM, convert_b5g5r5x1_unorm_b5g5r5a1_unorm_color_key }},
1298 {WINED3DFMT_B8G8R8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8_unorm_b8g8r8a8_unorm_color_key }},
1299 {WINED3DFMT_B8G8R8X8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8x8_unorm_b8g8r8a8_unorm_color_key }},
1300 {WINED3DFMT_B8G8R8A8_UNORM, {WINED3DFMT_B8G8R8A8_UNORM, convert_b8g8r8a8_unorm_b8g8r8a8_unorm_color_key }},
1303 if (need_alpha_ck && (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY))
1305 for (i = 0; i < ARRAY_SIZE(color_key_info); ++i)
1307 if (color_key_info[i].src_format == format->id)
1308 return &color_key_info[i].conversion;
1311 FIXME("Color-keying not supported with format %s.\n", debug_d3dformat(format->id));
1314 return NULL;
1317 /* We intentionally don't support WINED3DFMT_D32_UNORM. No hardware driver
1318 * supports it, and applications get confused when we do.
1320 * The following formats explicitly don't have WINED3DFMT_FLAG_TEXTURE set:
1322 * These are never supported on native.
1323 * WINED3DFMT_B8G8R8_UNORM
1324 * WINED3DFMT_B2G3R3_UNORM
1325 * WINED3DFMT_L4A4_UNORM
1326 * WINED3DFMT_S1_UINT_D15_UNORM
1327 * WINED3DFMT_S4X4_UINT_D24_UNORM
1329 * Only some Geforce/Voodoo3/G400 cards offer 8-bit textures in case of ddraw.
1330 * Since it is not widely available, don't offer it. Further no Windows driver
1331 * offers WINED3DFMT_P8_UINT_A8_NORM, so don't offer it either.
1332 * WINED3DFMT_P8_UINT
1333 * WINED3DFMT_P8_UINT_A8_UNORM
1335 * These formats seem to be similar to the HILO formats in
1336 * GL_NV_texture_shader. NVHU is said to be GL_UNSIGNED_HILO16,
1337 * NVHS GL_SIGNED_HILO16. Rumours say that D3D computes a 3rd channel
1338 * similarly to D3DFMT_CxV8U8 (So NVHS could be called D3DFMT_CxV16U16). ATI
1339 * refused to support formats which can easily be emulated with pixel shaders,
1340 * so applications have to deal with not having NVHS and NVHU.
1341 * WINED3DFMT_NVHU
1342 * WINED3DFMT_NVHS */
1343 static const struct wined3d_format_texture_info format_texture_info[] =
1345 /* format id gl_internal gl_srgb_internal gl_rt_internal
1346 gl_format gl_type conv_byte_count
1347 flags
1348 extension upload download */
1349 /* FourCC formats */
1350 /* GL_APPLE_ycbcr_422 claims that its '2YUV' format, which is supported via the UNSIGNED_SHORT_8_8_REV_APPLE type
1351 * is equivalent to 'UYVY' format on Windows, and the 'YUVS' via UNSIGNED_SHORT_8_8_APPLE equates to 'YUY2'. The
1352 * d3d9 test however shows that the opposite is true. Since the extension is from 2002, it predates the x86 based
1353 * Macs, so probably the endianness differs. This could be tested as soon as we have a Windows and MacOS on a big
1354 * endian machine
1356 {WINED3DFMT_UYVY, GL_RG8, GL_RG8, 0,
1357 GL_RG, GL_UNSIGNED_BYTE, 0,
1358 WINED3DFMT_FLAG_FILTERING,
1359 ARB_TEXTURE_RG, NULL},
1360 {WINED3DFMT_UYVY, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE8_ALPHA8, 0,
1361 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1362 WINED3DFMT_FLAG_FILTERING,
1363 WINED3D_GL_LEGACY_CONTEXT, NULL},
1364 {WINED3DFMT_UYVY, GL_RGB_RAW_422_APPLE, GL_RGB_RAW_422_APPLE, 0,
1365 GL_RGB_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE, 0,
1366 WINED3DFMT_FLAG_FILTERING,
1367 APPLE_RGB_422, NULL},
1368 {WINED3DFMT_UYVY, GL_RGB, GL_RGB, 0,
1369 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE, 0,
1370 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_FILTERING,
1371 APPLE_YCBCR_422, NULL},
1372 {WINED3DFMT_YUY2, GL_RG8, GL_RG8, 0,
1373 GL_RG, GL_UNSIGNED_BYTE, 0,
1374 WINED3DFMT_FLAG_FILTERING,
1375 ARB_TEXTURE_RG, NULL},
1376 {WINED3DFMT_YUY2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE8_ALPHA8, 0,
1377 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1378 WINED3DFMT_FLAG_FILTERING,
1379 WINED3D_GL_LEGACY_CONTEXT, NULL},
1380 {WINED3DFMT_YUY2, GL_RGB_RAW_422_APPLE, GL_RGB_RAW_422_APPLE, 0,
1381 GL_RGB_422_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, 0,
1382 WINED3DFMT_FLAG_FILTERING,
1383 APPLE_RGB_422, NULL},
1384 {WINED3DFMT_YUY2, GL_RGB, GL_RGB, 0,
1385 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, 0,
1386 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_FILTERING,
1387 APPLE_YCBCR_422, NULL},
1388 {WINED3DFMT_YV12, GL_R8, GL_R8, 0,
1389 GL_RED, GL_UNSIGNED_BYTE, 0,
1390 WINED3DFMT_FLAG_FILTERING,
1391 ARB_TEXTURE_RG, NULL},
1392 {WINED3DFMT_YV12, GL_ALPHA8, GL_ALPHA8, 0,
1393 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1394 WINED3DFMT_FLAG_FILTERING,
1395 WINED3D_GL_LEGACY_CONTEXT, NULL},
1396 {WINED3DFMT_NV12, GL_R8, GL_R8, 0,
1397 GL_RED, GL_UNSIGNED_BYTE, 0,
1398 WINED3DFMT_FLAG_FILTERING,
1399 ARB_TEXTURE_RG, NULL},
1400 {WINED3DFMT_NV12, GL_ALPHA8, GL_ALPHA8, 0,
1401 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1402 WINED3DFMT_FLAG_FILTERING,
1403 WINED3D_GL_LEGACY_CONTEXT, NULL},
1404 {WINED3DFMT_DXT1, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1405 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1406 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1407 | WINED3DFMT_FLAG_SRGB_READ,
1408 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1409 {WINED3DFMT_DXT2, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1410 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1411 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1412 | WINED3DFMT_FLAG_SRGB_READ,
1413 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1414 {WINED3DFMT_DXT3, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1415 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1416 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1417 | WINED3DFMT_FLAG_SRGB_READ,
1418 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1419 {WINED3DFMT_DXT4, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1420 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1421 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1422 | WINED3DFMT_FLAG_SRGB_READ,
1423 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1424 {WINED3DFMT_DXT5, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1425 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1426 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1427 | WINED3DFMT_FLAG_SRGB_READ,
1428 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1429 {WINED3DFMT_BC1_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0,
1430 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1431 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1432 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1433 {WINED3DFMT_BC2_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0,
1434 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1435 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1436 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1437 {WINED3DFMT_BC3_UNORM, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0,
1438 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1439 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1440 EXT_TEXTURE_COMPRESSION_S3TC, NULL},
1441 {WINED3DFMT_BC4_UNORM, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1, 0,
1442 GL_RED, GL_UNSIGNED_BYTE, 0,
1443 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1444 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1445 {WINED3DFMT_BC4_SNORM, GL_COMPRESSED_SIGNED_RED_RGTC1, GL_COMPRESSED_SIGNED_RED_RGTC1, 0,
1446 GL_RED, GL_UNSIGNED_BYTE, 0,
1447 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1448 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1449 {WINED3DFMT_BC5_UNORM, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1450 GL_RG, GL_UNSIGNED_BYTE, 0,
1451 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1452 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1453 {WINED3DFMT_BC5_SNORM, GL_COMPRESSED_SIGNED_RG_RGTC2, GL_COMPRESSED_SIGNED_RG_RGTC2, 0,
1454 GL_RG, GL_UNSIGNED_BYTE, 0,
1455 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1456 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1457 {WINED3DFMT_BC6H_UF16, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, 0,
1458 GL_RGB, GL_UNSIGNED_BYTE, 0,
1459 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1460 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1461 {WINED3DFMT_BC6H_SF16, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, 0,
1462 GL_RGB, GL_UNSIGNED_BYTE, 0,
1463 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1464 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1465 {WINED3DFMT_BC7_UNORM, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB, 0,
1466 GL_RGBA, GL_UNSIGNED_BYTE, 0,
1467 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1468 ARB_TEXTURE_COMPRESSION_BPTC, NULL},
1469 /* IEEE formats */
1470 {WINED3DFMT_R32_FLOAT, GL_RGB32F_ARB, GL_RGB32F_ARB, 0,
1471 GL_RED, GL_FLOAT, 0,
1472 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1473 ARB_TEXTURE_FLOAT, NULL},
1474 {WINED3DFMT_R32_FLOAT, GL_R32F, GL_R32F, 0,
1475 GL_RED, GL_FLOAT, 0,
1476 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1477 ARB_TEXTURE_RG, NULL},
1478 {WINED3DFMT_R32G32_FLOAT, GL_RGB32F_ARB, GL_RGB32F_ARB, 0,
1479 GL_RGB, GL_FLOAT, 12,
1480 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1481 ARB_TEXTURE_FLOAT, convert_r32g32_float},
1482 {WINED3DFMT_R32G32_FLOAT, GL_RG32F, GL_RG32F, 0,
1483 GL_RG, GL_FLOAT, 0,
1484 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1485 ARB_TEXTURE_RG, NULL},
1486 {WINED3DFMT_R32G32B32_FLOAT, GL_RGB32F, GL_RGB32F, 0,
1487 GL_RGB, GL_FLOAT, 0,
1488 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1489 ARB_TEXTURE_FLOAT, NULL},
1490 {WINED3DFMT_R32G32B32A32_FLOAT, GL_RGBA32F_ARB, GL_RGBA32F_ARB, 0,
1491 GL_RGBA, GL_FLOAT, 0,
1492 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1493 ARB_TEXTURE_FLOAT, NULL},
1494 /* Float */
1495 {WINED3DFMT_R16_FLOAT, GL_RGB16F_ARB, GL_RGB16F_ARB, 0,
1496 GL_RED, GL_HALF_FLOAT_ARB, 0,
1497 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1498 ARB_TEXTURE_FLOAT, NULL},
1499 {WINED3DFMT_R16_FLOAT, GL_R16F, GL_R16F, 0,
1500 GL_RED, GL_HALF_FLOAT_ARB, 0,
1501 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1502 ARB_TEXTURE_RG, NULL},
1503 {WINED3DFMT_R16G16_FLOAT, GL_RGB16F_ARB, GL_RGB16F_ARB, 0,
1504 GL_RGB, GL_HALF_FLOAT_ARB, 6,
1505 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1506 ARB_TEXTURE_FLOAT, convert_r16g16},
1507 {WINED3DFMT_R16G16_FLOAT, GL_RG16F, GL_RG16F, 0,
1508 GL_RG, GL_HALF_FLOAT_ARB, 0,
1509 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1510 ARB_TEXTURE_RG, NULL},
1511 {WINED3DFMT_R16G16B16A16_FLOAT, GL_RGBA16F_ARB, GL_RGBA16F_ARB, 0,
1512 GL_RGBA, GL_HALF_FLOAT_ARB, 0,
1513 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_RENDERTARGET
1514 | WINED3DFMT_FLAG_VTF,
1515 ARB_TEXTURE_FLOAT, NULL},
1516 {WINED3DFMT_R11G11B10_FLOAT, GL_R11F_G11F_B10F, GL_R11F_G11F_B10F, 0,
1517 GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, 0,
1518 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_RENDERTARGET,
1519 EXT_PACKED_FLOAT},
1520 /* Palettized formats */
1521 {WINED3DFMT_P8_UINT, GL_R8, GL_R8, 0,
1522 GL_RED, GL_UNSIGNED_BYTE, 0,
1524 ARB_TEXTURE_RG, NULL},
1525 {WINED3DFMT_P8_UINT, GL_ALPHA8, GL_ALPHA8, 0,
1526 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1528 WINED3D_GL_LEGACY_CONTEXT, NULL},
1529 /* Standard ARGB formats */
1530 {WINED3DFMT_B8G8R8_UNORM, GL_RGB8, GL_RGB8, 0,
1531 GL_BGR, GL_UNSIGNED_BYTE, 0,
1532 WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING | WINED3DFMT_FLAG_RENDERTARGET,
1533 WINED3D_GL_EXT_NONE, NULL},
1534 {WINED3DFMT_B8G8R8A8_UNORM, GL_RGBA8, GL_SRGB8_ALPHA8_EXT, 0,
1535 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1536 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1537 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE
1538 | WINED3DFMT_FLAG_VTF,
1539 WINED3D_GL_EXT_NONE, NULL},
1540 {WINED3DFMT_B8G8R8X8_UNORM, GL_RGB8, GL_SRGB8_EXT, 0,
1541 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1542 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1543 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE,
1544 WINED3D_GL_EXT_NONE, NULL},
1545 {WINED3DFMT_B5G6R5_UNORM, GL_RGB5, GL_SRGB8_EXT, GL_RGB8,
1546 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0,
1547 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1548 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_SRGB_READ,
1549 WINED3D_GL_EXT_NONE, NULL},
1550 {WINED3DFMT_B5G6R5_UNORM, GL_RGB565, GL_SRGB8_EXT, GL_RGB8,
1551 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0,
1552 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1553 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_SRGB_READ,
1554 ARB_ES2_COMPATIBILITY, NULL},
1555 {WINED3DFMT_B5G5R5X1_UNORM, GL_RGB5, GL_RGB5, 0,
1556 GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0,
1557 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1558 | WINED3DFMT_FLAG_RENDERTARGET,
1559 WINED3D_GL_EXT_NONE, NULL},
1560 {WINED3DFMT_B5G5R5A1_UNORM, GL_RGB5_A1, GL_RGB5_A1, 0,
1561 GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0,
1562 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1563 WINED3D_GL_EXT_NONE, NULL},
1564 {WINED3DFMT_B4G4R4A4_UNORM, GL_RGBA4, GL_SRGB8_ALPHA8_EXT, 0,
1565 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 0,
1566 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1567 | WINED3DFMT_FLAG_SRGB_READ,
1568 WINED3D_GL_EXT_NONE, NULL},
1569 {WINED3DFMT_B2G3R3_UNORM, GL_R3_G3_B2, GL_R3_G3_B2, 0,
1570 GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 0,
1571 WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1572 WINED3D_GL_EXT_NONE, NULL},
1573 {WINED3DFMT_R8_UNORM, GL_R8, GL_R8, 0,
1574 GL_RED, GL_UNSIGNED_BYTE, 0,
1575 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1576 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_VTF,
1577 ARB_TEXTURE_RG, NULL},
1578 {WINED3DFMT_A8_UNORM, GL_R8, GL_R8, 0,
1579 GL_RED, GL_UNSIGNED_BYTE, 0,
1580 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1581 | WINED3DFMT_FLAG_RENDERTARGET,
1582 ARB_TEXTURE_RG, NULL},
1583 {WINED3DFMT_A8_UNORM, GL_ALPHA8, GL_ALPHA8, 0,
1584 GL_ALPHA, GL_UNSIGNED_BYTE, 0,
1585 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1586 | WINED3DFMT_FLAG_RENDERTARGET,
1587 WINED3D_GL_LEGACY_CONTEXT, NULL},
1588 {WINED3DFMT_B4G4R4X4_UNORM, GL_RGB4, GL_RGB4, 0,
1589 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 0,
1590 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1591 WINED3D_GL_EXT_NONE, NULL},
1592 {WINED3DFMT_R10G10B10A2_UINT, GL_RGB10_A2UI, GL_RGB10_A2UI, 0,
1593 GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1594 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1595 ARB_TEXTURE_RGB10_A2UI, NULL},
1596 {WINED3DFMT_R10G10B10A2_UNORM, GL_RGB10_A2, GL_RGB10_A2, 0,
1597 GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1598 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1599 | WINED3DFMT_FLAG_RENDERTARGET,
1600 WINED3D_GL_EXT_NONE, NULL},
1601 {WINED3DFMT_R8G8B8A8_UNORM, GL_RGBA8, GL_SRGB8_ALPHA8_EXT, 0,
1602 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1603 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1604 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE
1605 | WINED3DFMT_FLAG_VTF,
1606 WINED3D_GL_EXT_NONE, NULL},
1607 {WINED3DFMT_R8G8B8A8_UINT, GL_RGBA8UI, GL_RGBA8UI, 0,
1608 GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1609 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1610 ARB_TEXTURE_RGB10_A2UI, NULL},
1611 {WINED3DFMT_R8G8B8A8_SINT, GL_RGBA8I, GL_RGBA8I, 0,
1612 GL_RGBA_INTEGER, GL_BYTE, 0,
1613 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1614 EXT_TEXTURE_INTEGER, NULL},
1615 {WINED3DFMT_R8G8B8X8_UNORM, GL_RGB8, GL_RGB8, 0,
1616 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1617 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1618 WINED3D_GL_EXT_NONE, NULL},
1619 {WINED3DFMT_R16G16_UNORM, GL_RGB16, GL_RGB16, GL_RGBA16,
1620 GL_RGB, GL_UNSIGNED_SHORT, 6,
1621 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1622 WINED3D_GL_EXT_NONE, convert_r16g16},
1623 {WINED3DFMT_R16G16_UNORM, GL_RG16, GL_RG16, 0,
1624 GL_RG, GL_UNSIGNED_SHORT, 0,
1625 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1626 | WINED3DFMT_FLAG_RENDERTARGET,
1627 ARB_TEXTURE_RG, NULL},
1628 {WINED3DFMT_B10G10R10A2_UNORM, GL_RGB10_A2, GL_RGB10_A2, 0,
1629 GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV, 0,
1630 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1631 | WINED3DFMT_FLAG_RENDERTARGET,
1632 WINED3D_GL_EXT_NONE, NULL},
1633 {WINED3DFMT_R16G16B16A16_UNORM, GL_RGBA16, GL_RGBA16, 0,
1634 GL_RGBA, GL_UNSIGNED_SHORT, 0,
1635 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1636 | WINED3DFMT_FLAG_RENDERTARGET,
1637 WINED3D_GL_EXT_NONE, NULL},
1638 {WINED3DFMT_R8G8_UNORM, GL_RG8, GL_RG8, 0,
1639 GL_RG, GL_UNSIGNED_BYTE, 0,
1640 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1641 | WINED3DFMT_FLAG_RENDERTARGET,
1642 ARB_TEXTURE_RG, NULL},
1643 {WINED3DFMT_R8G8_UINT, GL_RG8UI, GL_RG8UI, 0,
1644 GL_RG_INTEGER, GL_UNSIGNED_BYTE, 0,
1645 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1646 ARB_TEXTURE_RG, NULL},
1647 {WINED3DFMT_R8G8_SINT, GL_RG8I, GL_RG8I, 0,
1648 GL_RG_INTEGER, GL_BYTE, 0,
1649 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1650 ARB_TEXTURE_RG, NULL},
1651 {WINED3DFMT_R16G16B16A16_UINT, GL_RGBA16UI, GL_RGBA16UI, 0,
1652 GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 0,
1653 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1654 EXT_TEXTURE_INTEGER, NULL},
1655 {WINED3DFMT_R16G16B16A16_SINT, GL_RGBA16I, GL_RGBA16I, 0,
1656 GL_RGBA_INTEGER, GL_SHORT, 0,
1657 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1658 EXT_TEXTURE_INTEGER, NULL},
1659 {WINED3DFMT_R32G32_UINT, GL_RG32UI, GL_RG32UI, 0,
1660 GL_RG_INTEGER, GL_UNSIGNED_INT, 0,
1661 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1662 ARB_TEXTURE_RG, NULL},
1663 {WINED3DFMT_R32G32_SINT, GL_RG32I, GL_RG32I, 0,
1664 GL_RG_INTEGER, GL_INT, 0,
1665 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1666 ARB_TEXTURE_RG, NULL},
1667 {WINED3DFMT_R16G16_UINT, GL_RG16UI, GL_RG16UI, 0,
1668 GL_RG_INTEGER, GL_UNSIGNED_SHORT, 0,
1669 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1670 ARB_TEXTURE_RG, NULL},
1671 {WINED3DFMT_R16G16_SINT, GL_RG16I, GL_RG16I, 0,
1672 GL_RG_INTEGER, GL_SHORT, 0,
1673 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1674 ARB_TEXTURE_RG, NULL},
1675 {WINED3DFMT_R32_UINT, GL_R32UI, GL_R32UI, 0,
1676 GL_RED_INTEGER, GL_UNSIGNED_INT, 0,
1677 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1678 ARB_TEXTURE_RG, NULL},
1679 {WINED3DFMT_R32_SINT, GL_R32I, GL_R32I, 0,
1680 GL_RED_INTEGER, GL_INT, 0,
1681 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1682 ARB_TEXTURE_RG, NULL},
1683 {WINED3DFMT_R16_UNORM, GL_R16, GL_R16, 0,
1684 GL_RED, GL_UNSIGNED_SHORT, 0,
1685 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1686 | WINED3DFMT_FLAG_RENDERTARGET,
1687 ARB_TEXTURE_RG, NULL},
1688 {WINED3DFMT_R16_UINT, GL_R16UI, GL_R16UI, 0,
1689 GL_RED_INTEGER, GL_UNSIGNED_SHORT, 0,
1690 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1691 ARB_TEXTURE_RG, NULL},
1692 {WINED3DFMT_R16_SINT, GL_R16I, GL_R16I, 0,
1693 GL_RED_INTEGER, GL_SHORT, 0,
1694 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1695 ARB_TEXTURE_RG, NULL},
1696 {WINED3DFMT_R8_UINT, GL_R8UI, GL_R8UI, 0,
1697 GL_RED_INTEGER, GL_UNSIGNED_BYTE, 0,
1698 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1699 ARB_TEXTURE_RG, NULL},
1700 {WINED3DFMT_R8_SINT, GL_R8I, GL_R8I, 0,
1701 GL_RED_INTEGER, GL_BYTE, 0,
1702 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1703 ARB_TEXTURE_RG, NULL},
1704 /* Luminance */
1705 {WINED3DFMT_L8_UNORM, GL_LUMINANCE8, GL_SLUMINANCE8_EXT, 0,
1706 GL_LUMINANCE, GL_UNSIGNED_BYTE, 0,
1707 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1708 | WINED3DFMT_FLAG_SRGB_READ,
1709 WINED3D_GL_LEGACY_CONTEXT, NULL},
1710 {WINED3DFMT_L8_UNORM, GL_R8, GL_R8, 0,
1711 GL_RED, GL_UNSIGNED_BYTE, 0,
1712 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1713 | WINED3DFMT_FLAG_RENDERTARGET,
1714 ARB_TEXTURE_RG, NULL},
1715 {WINED3DFMT_L8A8_UNORM, GL_RG8, GL_RG8, 0,
1716 GL_RG, GL_UNSIGNED_BYTE, 0,
1717 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1718 ARB_TEXTURE_RG, NULL},
1719 {WINED3DFMT_L8A8_UNORM, GL_LUMINANCE8_ALPHA8, GL_SLUMINANCE8_ALPHA8_EXT, 0,
1720 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1721 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1722 | WINED3DFMT_FLAG_SRGB_READ,
1723 WINED3D_GL_LEGACY_CONTEXT, NULL},
1724 {WINED3DFMT_L4A4_UNORM, GL_RG8, GL_RG8, 0,
1725 GL_RG, GL_UNSIGNED_BYTE, 2,
1726 WINED3DFMT_FLAG_FILTERING,
1727 ARB_TEXTURE_RG, convert_l4a4_unorm},
1728 {WINED3DFMT_L4A4_UNORM, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE4_ALPHA4, 0,
1729 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 2,
1730 WINED3DFMT_FLAG_FILTERING,
1731 WINED3D_GL_LEGACY_CONTEXT, convert_l4a4_unorm},
1732 {WINED3DFMT_L16_UNORM, GL_R16, GL_R16, 0,
1733 GL_RED, GL_UNSIGNED_SHORT, 0,
1734 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1735 ARB_TEXTURE_RG, NULL},
1736 {WINED3DFMT_L16_UNORM, GL_LUMINANCE16, GL_LUMINANCE16, 0,
1737 GL_LUMINANCE, GL_UNSIGNED_SHORT, 0,
1738 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1739 WINED3D_GL_LEGACY_CONTEXT, NULL},
1740 /* Bump mapping stuff */
1741 {WINED3DFMT_R8G8_SNORM, GL_RGB8, GL_RGB8, 0,
1742 GL_BGR, GL_UNSIGNED_BYTE, 3,
1743 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1744 | WINED3DFMT_FLAG_BUMPMAP,
1745 WINED3D_GL_EXT_NONE, convert_r8g8_snorm},
1746 {WINED3DFMT_R8G8_SNORM, GL_DSDT8_NV, GL_DSDT8_NV, 0,
1747 GL_DSDT_NV, GL_BYTE, 0,
1748 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1749 | WINED3DFMT_FLAG_BUMPMAP,
1750 NV_TEXTURE_SHADER, NULL},
1751 {WINED3DFMT_R8G8_SNORM, GL_RG8_SNORM, GL_RG8_SNORM, 0,
1752 GL_RG, GL_BYTE, 0,
1753 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1754 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_BUMPMAP,
1755 EXT_TEXTURE_SNORM, NULL},
1756 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_RGB5, GL_RGB5, 0,
1757 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2,
1758 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1759 | WINED3DFMT_FLAG_BUMPMAP,
1760 WINED3D_GL_EXT_NONE, convert_r5g5_snorm_l6_unorm},
1761 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_DSDT8_MAG8_NV, GL_DSDT8_MAG8_NV, 0,
1762 GL_DSDT_MAG_NV, GL_BYTE, 3,
1763 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1764 | WINED3DFMT_FLAG_BUMPMAP,
1765 NV_TEXTURE_SHADER, convert_r5g5_snorm_l6_unorm_nv},
1766 {WINED3DFMT_R5G5_SNORM_L6_UNORM, GL_RGB8_SNORM, GL_RGB8_SNORM, 0,
1767 GL_RGBA, GL_BYTE, 4,
1768 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1769 | WINED3DFMT_FLAG_BUMPMAP,
1770 EXT_TEXTURE_SNORM, convert_r5g5_snorm_l6_unorm_ext},
1771 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, GL_RGB8, GL_RGB8, 0,
1772 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4,
1773 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1774 | WINED3DFMT_FLAG_BUMPMAP,
1775 WINED3D_GL_EXT_NONE, convert_r8g8_snorm_l8x8_unorm},
1776 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, GL_DSDT8_MAG8_INTENSITY8_NV, GL_DSDT8_MAG8_INTENSITY8_NV, 0,
1777 GL_DSDT_MAG_VIB_NV, GL_UNSIGNED_INT_8_8_S8_S8_REV_NV, 4,
1778 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1779 | WINED3DFMT_FLAG_BUMPMAP,
1780 NV_TEXTURE_SHADER, convert_r8g8_snorm_l8x8_unorm_nv},
1781 {WINED3DFMT_R8G8B8A8_SNORM, GL_RGBA8, GL_RGBA8, 0,
1782 GL_BGRA, GL_UNSIGNED_BYTE, 4,
1783 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1784 | WINED3DFMT_FLAG_BUMPMAP,
1785 WINED3D_GL_EXT_NONE, convert_r8g8b8a8_snorm},
1786 {WINED3DFMT_R8G8B8A8_SNORM, GL_SIGNED_RGBA8_NV, GL_SIGNED_RGBA8_NV, 0,
1787 GL_RGBA, GL_BYTE, 0,
1788 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1789 | WINED3DFMT_FLAG_BUMPMAP,
1790 NV_TEXTURE_SHADER, NULL},
1791 {WINED3DFMT_R8G8B8A8_SNORM, GL_RGBA8_SNORM, GL_RGBA8_SNORM, 0,
1792 GL_RGBA, GL_BYTE, 0,
1793 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1794 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_BUMPMAP,
1795 EXT_TEXTURE_SNORM, NULL},
1796 {WINED3DFMT_R16G16_SNORM, GL_RGB16, GL_RGB16, 0,
1797 GL_BGR, GL_UNSIGNED_SHORT, 6,
1798 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1799 | WINED3DFMT_FLAG_BUMPMAP,
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 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1804 | WINED3DFMT_FLAG_BUMPMAP,
1805 NV_TEXTURE_SHADER, NULL},
1806 {WINED3DFMT_R16G16_SNORM, GL_RG16_SNORM, GL_RG16_SNORM, 0,
1807 GL_RG, GL_SHORT, 0,
1808 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1809 | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_BUMPMAP,
1810 EXT_TEXTURE_SNORM, NULL},
1811 {WINED3DFMT_R16G16B16A16_SNORM, GL_RGBA16_SNORM, GL_RGBA16_SNORM, 0,
1812 GL_RGBA, GL_SHORT, 0,
1813 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1814 | WINED3DFMT_FLAG_RENDERTARGET,
1815 EXT_TEXTURE_SNORM, NULL},
1816 {WINED3DFMT_R16_SNORM, GL_R16_SNORM, GL_R16_SNORM, 0,
1817 GL_RED, GL_SHORT, 0,
1818 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1819 | WINED3DFMT_FLAG_RENDERTARGET,
1820 EXT_TEXTURE_SNORM, NULL},
1821 {WINED3DFMT_R8_SNORM, GL_R8_SNORM, GL_R8_SNORM, 0,
1822 GL_RED, GL_BYTE, 0,
1823 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1824 | WINED3DFMT_FLAG_RENDERTARGET,
1825 EXT_TEXTURE_SNORM, NULL},
1826 /* Depth stencil formats */
1827 {WINED3DFMT_D16_LOCKABLE, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1828 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1829 WINED3DFMT_FLAG_DEPTH_STENCIL,
1830 WINED3D_GL_EXT_NONE, NULL},
1831 {WINED3DFMT_D16_LOCKABLE, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT16, 0,
1832 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1833 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1834 ARB_DEPTH_TEXTURE, NULL},
1835 {WINED3DFMT_D24_UNORM_S8_UINT, GL_DEPTH_COMPONENT24_ARB, GL_DEPTH_COMPONENT24_ARB, 0,
1836 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0,
1837 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1838 | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1839 ARB_DEPTH_TEXTURE, NULL},
1840 {WINED3DFMT_D24_UNORM_S8_UINT, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8, 0,
1841 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0,
1842 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1843 | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1844 EXT_PACKED_DEPTH_STENCIL, NULL},
1845 {WINED3DFMT_X8D24_UNORM, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1846 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4,
1847 WINED3DFMT_FLAG_DEPTH_STENCIL,
1848 WINED3D_GL_EXT_NONE, x8_d24_unorm_upload, x8_d24_unorm_download},
1849 {WINED3DFMT_X8D24_UNORM, GL_DEPTH_COMPONENT24_ARB, GL_DEPTH_COMPONENT24_ARB, 0,
1850 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4,
1851 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1852 | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1853 ARB_DEPTH_TEXTURE, x8_d24_unorm_upload, x8_d24_unorm_download},
1854 {WINED3DFMT_D16_UNORM, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, 0,
1855 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1856 WINED3DFMT_FLAG_DEPTH_STENCIL,
1857 WINED3D_GL_EXT_NONE, NULL},
1858 {WINED3DFMT_D16_UNORM, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT16, 0,
1859 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0,
1860 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1861 | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1862 ARB_DEPTH_TEXTURE, NULL},
1863 {WINED3DFMT_D32_FLOAT, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT32F, 0,
1864 GL_DEPTH_COMPONENT, GL_FLOAT, 0,
1865 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1866 ARB_DEPTH_BUFFER_FLOAT, NULL},
1867 {WINED3DFMT_D32_FLOAT_S8X24_UINT, GL_DEPTH32F_STENCIL8, GL_DEPTH32F_STENCIL8, 0,
1868 GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 0,
1869 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1870 ARB_DEPTH_BUFFER_FLOAT, NULL},
1871 {WINED3DFMT_S8_UINT_D24_FLOAT, GL_DEPTH32F_STENCIL8, GL_DEPTH32F_STENCIL8, 0,
1872 GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 8,
1873 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_DEPTH_STENCIL | WINED3DFMT_FLAG_SHADOW,
1874 ARB_DEPTH_BUFFER_FLOAT, convert_s8_uint_d24_float},
1875 {WINED3DFMT_R32G32B32A32_UINT, GL_RGBA32UI, GL_RGBA32UI, 0,
1876 GL_RGBA_INTEGER, GL_UNSIGNED_INT, 0,
1877 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1878 EXT_TEXTURE_INTEGER, NULL},
1879 {WINED3DFMT_R32G32B32A32_SINT, GL_RGBA32I, GL_RGBA32I, 0,
1880 GL_RGBA_INTEGER, GL_INT, 0,
1881 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET,
1882 EXT_TEXTURE_INTEGER, NULL},
1883 /* Vendor-specific formats */
1884 {WINED3DFMT_ATI1N, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1, 0,
1885 GL_RED, GL_UNSIGNED_BYTE, 0,
1886 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1887 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1888 {WINED3DFMT_ATI2N, GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, 0,
1889 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1890 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1891 ATI_TEXTURE_COMPRESSION_3DC, NULL},
1892 {WINED3DFMT_ATI2N, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1893 GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0,
1894 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1895 EXT_TEXTURE_COMPRESSION_RGTC, NULL},
1896 {WINED3DFMT_ATI2N, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2, 0,
1897 GL_RG, GL_UNSIGNED_BYTE, 0,
1898 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1899 ARB_TEXTURE_COMPRESSION_RGTC, NULL},
1900 {WINED3DFMT_INTZ, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8, 0,
1901 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0,
1902 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING
1903 | WINED3DFMT_FLAG_DEPTH_STENCIL,
1904 EXT_PACKED_DEPTH_STENCIL, NULL},
1905 {WINED3DFMT_NULL, 0, 0, 0,
1906 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0,
1907 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_FBO_ATTACHABLE,
1908 ARB_FRAMEBUFFER_OBJECT, NULL},
1909 /* DirectX 10 HDR formats */
1910 {WINED3DFMT_R9G9B9E5_SHAREDEXP, GL_RGB9_E5_EXT, GL_RGB9_E5_EXT, 0,
1911 GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV_EXT, 0,
1912 WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING | WINED3DFMT_FLAG_FILTERING,
1913 EXT_TEXTURE_SHARED_EXPONENT, NULL},
1916 struct wined3d_format_srgb_info
1918 enum wined3d_format_id srgb_format_id;
1919 enum wined3d_format_id base_format_id;
1922 static const struct wined3d_format_srgb_info format_srgb_info[] =
1924 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, WINED3DFMT_R8G8B8A8_UNORM},
1925 {WINED3DFMT_BC1_UNORM_SRGB, WINED3DFMT_BC1_UNORM},
1926 {WINED3DFMT_BC2_UNORM_SRGB, WINED3DFMT_BC2_UNORM},
1927 {WINED3DFMT_BC3_UNORM_SRGB, WINED3DFMT_BC3_UNORM},
1928 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, WINED3DFMT_B8G8R8A8_UNORM},
1929 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, WINED3DFMT_B8G8R8X8_UNORM},
1930 {WINED3DFMT_BC7_UNORM_SRGB, WINED3DFMT_BC7_UNORM},
1933 static inline int get_format_idx(enum wined3d_format_id format_id)
1935 unsigned int i;
1937 if (format_id < WINED3D_FORMAT_FOURCC_BASE)
1938 return format_id;
1940 for (i = 0; i < ARRAY_SIZE(format_index_remap); ++i)
1942 if (format_index_remap[i].id == format_id)
1943 return format_index_remap[i].idx;
1946 return -1;
1949 static struct wined3d_format_gl *wined3d_format_gl_mutable(struct wined3d_format *format)
1951 return CONTAINING_RECORD(format, struct wined3d_format_gl, f);
1954 static struct wined3d_format_vk *wined3d_format_vk_mutable(struct wined3d_format *format)
1956 return CONTAINING_RECORD(format, struct wined3d_format_vk, f);
1959 static struct wined3d_format *get_format_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
1961 return (struct wined3d_format *)((BYTE *)adapter->formats + fmt_idx * adapter->format_size);
1964 static struct wined3d_format_gl *get_format_gl_by_idx(const struct wined3d_adapter *adapter, int fmt_idx)
1966 return wined3d_format_gl_mutable(get_format_by_idx(adapter, fmt_idx));
1969 static struct wined3d_format *get_format_internal(const struct wined3d_adapter *adapter,
1970 enum wined3d_format_id format_id)
1972 int fmt_idx;
1974 if ((fmt_idx = get_format_idx(format_id)) == -1)
1976 ERR("Format %s (%#x) not found.\n", debug_d3dformat(format_id), format_id);
1977 return NULL;
1980 return get_format_by_idx(adapter, fmt_idx);
1983 static struct wined3d_format_gl *get_format_gl_internal(const struct wined3d_adapter *adapter,
1984 enum wined3d_format_id format_id)
1986 struct wined3d_format *format;
1988 if ((format = get_format_internal(adapter, format_id)))
1989 return wined3d_format_gl_mutable(format);
1991 return NULL;
1994 static void copy_format(const struct wined3d_adapter *adapter,
1995 struct wined3d_format *dst_format, const struct wined3d_format *src_format)
1997 enum wined3d_format_id id = dst_format->id;
1998 memcpy(dst_format, src_format, adapter->format_size);
1999 dst_format->id = id;
2002 static void format_set_flag(struct wined3d_format *format, unsigned int flag)
2004 unsigned int i;
2006 for (i = 0; i < ARRAY_SIZE(format->flags); ++i)
2007 format->flags[i] |= flag;
2010 static void format_clear_flag(struct wined3d_format *format, unsigned int flag)
2012 unsigned int i;
2014 for (i = 0; i < ARRAY_SIZE(format->flags); ++i)
2015 format->flags[i] &= ~flag;
2018 static enum wined3d_channel_type map_channel_type(char t)
2020 switch (t)
2022 case 'u':
2023 return WINED3D_CHANNEL_TYPE_UNORM;
2024 case 'i':
2025 return WINED3D_CHANNEL_TYPE_SNORM;
2026 case 'U':
2027 return WINED3D_CHANNEL_TYPE_UINT;
2028 case 'I':
2029 return WINED3D_CHANNEL_TYPE_SINT;
2030 case 'F':
2031 return WINED3D_CHANNEL_TYPE_FLOAT;
2032 case 'D':
2033 return WINED3D_CHANNEL_TYPE_DEPTH;
2034 case 'S':
2035 return WINED3D_CHANNEL_TYPE_STENCIL;
2036 case 'X':
2037 return WINED3D_CHANNEL_TYPE_UNUSED;
2038 default:
2039 ERR("Invalid channel type '%c'.\n", t);
2040 return WINED3D_CHANNEL_TYPE_NONE;
2044 static BOOL init_format_base_info(struct wined3d_adapter *adapter)
2046 struct wined3d_format *format;
2047 unsigned int i, j;
2049 for (i = 0; i < ARRAY_SIZE(formats); ++i)
2051 if (!(format = get_format_internal(adapter, formats[i].id)))
2052 return FALSE;
2054 format->id = formats[i].id;
2055 format->red_size = formats[i].red_size;
2056 format->green_size = formats[i].green_size;
2057 format->blue_size = formats[i].blue_size;
2058 format->alpha_size = formats[i].alpha_size;
2059 format->red_offset = formats[i].red_offset;
2060 format->green_offset = formats[i].green_offset;
2061 format->blue_offset = formats[i].blue_offset;
2062 format->alpha_offset = formats[i].alpha_offset;
2063 format->byte_count = formats[i].bpp;
2064 format->depth_size = formats[i].depth_size;
2065 format->stencil_size = formats[i].stencil_size;
2066 format->block_width = 1;
2067 format->block_height = 1;
2068 format->block_byte_count = formats[i].bpp;
2071 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
2073 struct wined3d_format *typeless_format;
2074 unsigned int component_count = 0;
2075 DWORD flags = 0;
2077 if (!(format = get_format_internal(adapter, typed_formats[i].id)))
2078 return FALSE;
2080 if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
2081 return FALSE;
2083 format->id = typed_formats[i].id;
2084 format->red_size = typeless_format->red_size;
2085 format->green_size = typeless_format->green_size;
2086 format->blue_size = typeless_format->blue_size;
2087 format->alpha_size = typeless_format->alpha_size;
2088 format->red_offset = typeless_format->red_offset;
2089 format->green_offset = typeless_format->green_offset;
2090 format->blue_offset = typeless_format->blue_offset;
2091 format->alpha_offset = typeless_format->alpha_offset;
2092 format->byte_count = typeless_format->byte_count;
2093 format->depth_size = typeless_format->depth_size;
2094 format->stencil_size = typeless_format->stencil_size;
2095 format->block_width = typeless_format->block_width;
2096 format->block_height = typeless_format->block_height;
2097 format->block_byte_count = typeless_format->block_byte_count;
2098 format->typeless_id = typeless_format->id;
2100 typeless_format->typeless_id = typeless_format->id;
2102 for (j = 0; j < strlen(typed_formats[i].channels); ++j)
2104 enum wined3d_channel_type channel_type = map_channel_type(typed_formats[i].channels[j]);
2106 if (channel_type == WINED3D_CHANNEL_TYPE_UNORM || channel_type == WINED3D_CHANNEL_TYPE_SNORM)
2107 flags |= WINED3DFMT_FLAG_NORMALISED;
2108 if (channel_type == WINED3D_CHANNEL_TYPE_UINT || channel_type == WINED3D_CHANNEL_TYPE_SINT)
2109 flags |= WINED3DFMT_FLAG_INTEGER;
2110 if (channel_type == WINED3D_CHANNEL_TYPE_FLOAT)
2111 flags |= WINED3DFMT_FLAG_FLOAT;
2112 if (channel_type != WINED3D_CHANNEL_TYPE_UNUSED)
2113 ++component_count;
2115 if (channel_type == WINED3D_CHANNEL_TYPE_DEPTH && !format->depth_size)
2117 format->depth_size = format->red_size;
2118 format->red_size = format->red_offset = 0;
2121 if (channel_type == WINED3D_CHANNEL_TYPE_STENCIL && !format->stencil_size)
2123 format->stencil_size = format->green_size;
2124 format->green_size = format->green_offset = 0;
2128 format->component_count = component_count;
2129 format_set_flag(format, flags);
2132 for (i = 0; i < ARRAY_SIZE(ddi_formats); ++i)
2134 if (!(format = get_format_internal(adapter, ddi_formats[i].id)))
2135 return FALSE;
2137 format->ddi_format = ddi_formats[i].ddi_format;
2140 for (i = 0; i < ARRAY_SIZE(format_base_flags); ++i)
2142 if (!(format = get_format_internal(adapter, format_base_flags[i].id)))
2143 return FALSE;
2145 format_set_flag(format, format_base_flags[i].flags);
2148 return TRUE;
2151 static BOOL init_format_block_info(struct wined3d_adapter *adapter)
2153 struct wined3d_format *format;
2154 unsigned int i;
2156 for (i = 0; i < ARRAY_SIZE(format_block_info); ++i)
2158 if (!(format = get_format_internal(adapter, format_block_info[i].id)))
2159 return FALSE;
2161 format->block_width = format_block_info[i].block_width;
2162 format->block_height = format_block_info[i].block_height;
2163 format->block_byte_count = format_block_info[i].block_byte_count;
2164 format_set_flag(format, WINED3DFMT_FLAG_BLOCKS | format_block_info[i].flags);
2167 return TRUE;
2170 /* Most compressed formats are not supported for 3D textures by OpenGL.
2172 * In the case of the S3TC/DXTn formats, NV_texture_compression_vtc provides
2173 * these formats for 3D textures, but unfortunately the block layout is
2174 * different from the one used by Direct3D.
2176 * Since applications either don't check format availability at all before
2177 * using these, or refuse to run without them, we decompress them on upload.
2179 * Affected applications include "Heroes VI", "From Dust", "Halo Online" and
2180 * "Eldorado". */
2181 static BOOL init_format_decompress_info(struct wined3d_adapter *adapter)
2183 struct wined3d_format *format;
2184 unsigned int i;
2186 for (i = 0; i < ARRAY_SIZE(format_decompress_info); ++i)
2188 if (!(format = get_format_internal(adapter, format_decompress_info[i].id)))
2189 return FALSE;
2191 format->flags[WINED3D_GL_RES_TYPE_TEX_3D] |= WINED3DFMT_FLAG_DECOMPRESS;
2192 format->decompress = format_decompress_info[i].decompress;
2195 return TRUE;
2198 static BOOL init_srgb_formats(struct wined3d_adapter *adapter)
2200 struct wined3d_format *format, *srgb_format;
2201 unsigned int i;
2203 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
2205 if (!(srgb_format = get_format_internal(adapter, format_srgb_info[i].srgb_format_id)))
2206 return FALSE;
2207 if (!(format = get_format_internal(adapter, format_srgb_info[i].base_format_id)))
2208 return FALSE;
2210 copy_format(adapter, srgb_format, format);
2213 return TRUE;
2216 static GLenum wined3d_gl_type_to_enum(enum wined3d_gl_resource_type type)
2218 switch (type)
2220 case WINED3D_GL_RES_TYPE_TEX_1D:
2221 return GL_TEXTURE_1D;
2222 case WINED3D_GL_RES_TYPE_TEX_2D:
2223 return GL_TEXTURE_2D;
2224 case WINED3D_GL_RES_TYPE_TEX_3D:
2225 return GL_TEXTURE_3D;
2226 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2227 return GL_TEXTURE_CUBE_MAP_ARB;
2228 case WINED3D_GL_RES_TYPE_TEX_RECT:
2229 return GL_TEXTURE_RECTANGLE_ARB;
2230 case WINED3D_GL_RES_TYPE_BUFFER:
2231 return GL_TEXTURE_2D; /* TODO: GL_TEXTURE_BUFFER. */
2232 case WINED3D_GL_RES_TYPE_RB:
2233 return GL_RENDERBUFFER;
2234 case WINED3D_GL_RES_TYPE_COUNT:
2235 break;
2237 ERR("Unexpected GL resource type %u.\n", type);
2238 return 0;
2241 static void delete_fbo_attachment(const struct wined3d_gl_info *gl_info,
2242 enum wined3d_gl_resource_type d3d_type, GLuint object)
2244 switch (d3d_type)
2246 case WINED3D_GL_RES_TYPE_TEX_1D:
2247 case WINED3D_GL_RES_TYPE_TEX_2D:
2248 case WINED3D_GL_RES_TYPE_TEX_RECT:
2249 case WINED3D_GL_RES_TYPE_TEX_3D:
2250 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2251 gl_info->gl_ops.gl.p_glDeleteTextures(1, &object);
2252 break;
2254 case WINED3D_GL_RES_TYPE_RB:
2255 gl_info->fbo_ops.glDeleteRenderbuffers(1, &object);
2256 break;
2258 case WINED3D_GL_RES_TYPE_BUFFER:
2259 case WINED3D_GL_RES_TYPE_COUNT:
2260 break;
2264 static void create_and_bind_fbo_attachment(const struct wined3d_gl_info *gl_info,
2265 const struct wined3d_format_gl *format_gl,
2266 enum wined3d_gl_resource_type d3d_type, GLuint *object, GLenum internal)
2268 GLenum format = format_gl->format;
2269 GLenum type = format_gl->type;
2270 GLenum attach_type;
2272 attach_type = format_gl->f.depth_size ? GL_DEPTH_ATTACHMENT : GL_COLOR_ATTACHMENT0;
2273 switch (d3d_type)
2275 case WINED3D_GL_RES_TYPE_TEX_1D:
2276 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2277 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, *object);
2278 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, internal, 16, 0, format, type, NULL);
2279 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2280 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2282 gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_1D, *object, 0);
2283 if (format_gl->f.stencil_size)
2284 gl_info->fbo_ops.glFramebufferTexture1D(GL_FRAMEBUFFER,
2285 GL_STENCIL_ATTACHMENT, GL_TEXTURE_1D, *object, 0);
2286 break;
2288 case WINED3D_GL_RES_TYPE_TEX_2D:
2289 case WINED3D_GL_RES_TYPE_TEX_RECT:
2290 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2291 gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(d3d_type), *object);
2292 gl_info->gl_ops.gl.p_glTexImage2D(wined3d_gl_type_to_enum(d3d_type), 0, internal, 16, 16, 0,
2293 format, type, NULL);
2294 gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2295 gl_info->gl_ops.gl.p_glTexParameteri(wined3d_gl_type_to_enum(d3d_type), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2297 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2298 wined3d_gl_type_to_enum(d3d_type), *object, 0);
2299 if (format_gl->f.stencil_size)
2300 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
2301 wined3d_gl_type_to_enum(d3d_type), *object, 0);
2302 break;
2304 case WINED3D_GL_RES_TYPE_TEX_3D:
2305 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2306 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, *object);
2307 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, internal, 16, 16, 16, 0, format, type, NULL));
2308 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2309 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2311 gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER, attach_type, GL_TEXTURE_3D, *object, 0, 0);
2312 if (format_gl->f.stencil_size)
2313 gl_info->fbo_ops.glFramebufferTexture3D(GL_FRAMEBUFFER,
2314 GL_STENCIL_ATTACHMENT, GL_TEXTURE_3D, *object, 0, 0);
2315 break;
2317 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2318 gl_info->gl_ops.gl.p_glGenTextures(1, object);
2319 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, *object);
2320 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, internal, 16, 16, 0,
2321 format, type, NULL);
2322 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, internal, 16, 16, 0,
2323 format, type, NULL);
2324 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, internal, 16, 16, 0,
2325 format, type, NULL);
2326 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, internal, 16, 16, 0,
2327 format, type, NULL);
2328 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, internal, 16, 16, 0,
2329 format, type, NULL);
2330 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, internal, 16, 16, 0,
2331 format, type, NULL);
2332 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2333 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2335 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, attach_type,
2336 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2337 if (format_gl->f.stencil_size)
2338 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
2339 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, *object, 0);
2340 break;
2342 case WINED3D_GL_RES_TYPE_RB:
2343 gl_info->fbo_ops.glGenRenderbuffers(1, object);
2344 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, *object);
2345 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, internal, 16, 16);
2346 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, attach_type, GL_RENDERBUFFER, *object);
2347 if (format_gl->f.stencil_size)
2348 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
2349 GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *object);
2350 break;
2352 case WINED3D_GL_RES_TYPE_BUFFER:
2353 case WINED3D_GL_RES_TYPE_COUNT:
2354 break;
2357 /* Ideally we'd skip all formats already known not to work on textures
2358 * by checking for WINED3DFMT_FLAG_TEXTURE here. However, we want to
2359 * know if we can attach WINED3DFMT_P8_UINT textures to FBOs, and this
2360 * format never has WINED3DFMT_FLAG_TEXTURE set. Instead, swallow GL
2361 * errors generated by invalid formats. */
2362 while (gl_info->gl_ops.gl.p_glGetError());
2365 static void draw_test_quad(struct wined3d_caps_gl_ctx *ctx, const struct wined3d_vec3 *geometry,
2366 const struct wined3d_color *color)
2368 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2369 static const struct wined3d_vec3 default_geometry[] =
2371 {-1.0f, -1.0f, 0.0f},
2372 { 1.0f, -1.0f, 0.0f},
2373 {-1.0f, 1.0f, 0.0f},
2374 { 1.0f, 1.0f, 0.0f},
2376 static const char vs_core_header[] =
2377 "#version 150\n"
2378 "in vec4 pos;\n"
2379 "in vec4 color;\n"
2380 "out vec4 out_color;\n"
2381 "\n";
2382 static const char vs_legacy_header[] =
2383 "#version 120\n"
2384 "attribute vec4 pos;\n"
2385 "attribute vec4 color;\n"
2386 "varying vec4 out_color;\n"
2387 "\n";
2388 static const char vs_body[] =
2389 "void main()\n"
2390 "{\n"
2391 " gl_Position = pos;\n"
2392 " out_color = color;\n"
2393 "}\n";
2394 static const char fs_core[] =
2395 "#version 150\n"
2396 "in vec4 out_color;\n"
2397 "out vec4 fragment_color;\n"
2398 "\n"
2399 "void main()\n"
2400 "{\n"
2401 " fragment_color = out_color;\n"
2402 "}\n";
2403 static const char fs_legacy[] =
2404 "#version 120\n"
2405 "varying vec4 out_color;\n"
2406 "\n"
2407 "void main()\n"
2408 "{\n"
2409 " gl_FragData[0] = out_color;\n"
2410 "}\n";
2411 const char *source[2];
2412 GLuint vs_id, fs_id;
2413 unsigned int i;
2415 if (!geometry)
2416 geometry = default_geometry;
2418 if (!gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] || !gl_info->supported[ARB_VERTEX_SHADER]
2419 || !gl_info->supported[ARB_FRAGMENT_SHADER])
2421 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2422 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2423 gl_info->gl_ops.gl.p_glLoadIdentity();
2424 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2425 gl_info->gl_ops.gl.p_glLoadIdentity();
2427 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
2428 gl_info->gl_ops.gl.p_glColor4f(color->r, color->g, color->b, color->a);
2429 for (i = 0; i < 4; ++i)
2430 gl_info->gl_ops.gl.p_glVertex3fv(&geometry[i].x);
2431 gl_info->gl_ops.gl.p_glEnd();
2432 checkGLcall("draw quad");
2433 return;
2436 if (!ctx->test_vbo)
2437 GL_EXTCALL(glGenBuffers(1, &ctx->test_vbo));
2438 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, ctx->test_vbo));
2439 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(struct wined3d_vec3) * 4, geometry, GL_STREAM_DRAW));
2440 GL_EXTCALL(glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 0, NULL));
2441 GL_EXTCALL(glVertexAttrib4f(1, color->r, color->g, color->b, color->a));
2442 GL_EXTCALL(glEnableVertexAttribArray(0));
2443 GL_EXTCALL(glDisableVertexAttribArray(1));
2445 if (!ctx->test_program_id)
2447 BOOL use_glsl_150 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50);
2449 ctx->test_program_id = GL_EXTCALL(glCreateProgram());
2451 vs_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
2452 source[0] = use_glsl_150 ? vs_core_header : vs_legacy_header;
2453 source[1] = vs_body;
2454 GL_EXTCALL(glShaderSource(vs_id, 2, source, NULL));
2455 GL_EXTCALL(glAttachShader(ctx->test_program_id, vs_id));
2456 GL_EXTCALL(glDeleteShader(vs_id));
2458 fs_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
2459 source[0] = use_glsl_150 ? fs_core : fs_legacy;
2460 GL_EXTCALL(glShaderSource(fs_id, 1, source, NULL));
2461 GL_EXTCALL(glAttachShader(ctx->test_program_id, fs_id));
2462 GL_EXTCALL(glDeleteShader(fs_id));
2464 GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 0, "pos"));
2465 GL_EXTCALL(glBindAttribLocation(ctx->test_program_id, 1, "color"));
2467 if (use_glsl_150)
2468 GL_EXTCALL(glBindFragDataLocation(ctx->test_program_id, 0, "fragment_color"));
2470 GL_EXTCALL(glCompileShader(vs_id));
2471 print_glsl_info_log(gl_info, vs_id, FALSE);
2472 GL_EXTCALL(glCompileShader(fs_id));
2473 print_glsl_info_log(gl_info, fs_id, FALSE);
2474 GL_EXTCALL(glLinkProgram(ctx->test_program_id));
2475 shader_glsl_validate_link(gl_info, ctx->test_program_id);
2477 GL_EXTCALL(glUseProgram(ctx->test_program_id));
2479 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2481 GL_EXTCALL(glUseProgram(0));
2482 GL_EXTCALL(glDisableVertexAttribArray(0));
2483 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
2484 checkGLcall("draw quad");
2487 /* Context activation is done by the caller. */
2488 static void check_fbo_compat(struct wined3d_caps_gl_ctx *ctx, struct wined3d_format_gl *format)
2490 /* Check if the default internal format is supported as a frame buffer
2491 * target, otherwise fall back to the render target internal.
2493 * Try to stick to the standard format if possible, this limits precision differences. */
2494 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 1.0f};
2495 static const struct wined3d_color half_transparent_red = {1.0f, 0.0f, 0.0f, 0.5f};
2496 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2497 GLenum status, rt_internal = format->rt_internal;
2498 GLuint object, color_rb;
2499 enum wined3d_gl_resource_type type;
2500 BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2502 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2504 for (type = 0; type < ARRAY_SIZE(format->f.flags); ++type)
2506 const char *type_string = "color";
2508 if (type == WINED3D_GL_RES_TYPE_BUFFER)
2509 continue;
2511 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->internal);
2513 if (format->f.flags[type] & WINED3DFMT_FLAG_DEPTH_STENCIL)
2515 gl_info->fbo_ops.glGenRenderbuffers(1, &color_rb);
2516 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, color_rb);
2517 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2518 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 1);
2519 else
2520 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 16);
2522 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
2523 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_rb);
2524 checkGLcall("Create and attach color rb attachment");
2525 type_string = "depth / stencil";
2528 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2529 checkGLcall("Framebuffer format check");
2531 if (status == GL_FRAMEBUFFER_COMPLETE)
2533 TRACE("Format %s is supported as FBO %s attachment, type %u.\n",
2534 debug_d3dformat(format->f.id), type_string, type);
2535 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE;
2536 format->rt_internal = format->internal;
2537 regular_fmt_used = TRUE;
2539 else
2541 if (!rt_internal)
2543 if (format->f.flags[type] & (WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_DEPTH_STENCIL))
2545 WARN("Format %s with rendertarget flag is not supported as FBO color attachment (type %u),"
2546 " and no fallback specified.\n", debug_d3dformat(format->f.id), type);
2547 format->f.flags[type] &= ~(WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_DEPTH_STENCIL);
2549 else
2551 TRACE("Format %s is not supported as FBO %s attachment, type %u.\n",
2552 debug_d3dformat(format->f.id), type_string, type);
2554 format->rt_internal = format->internal;
2556 else
2558 TRACE("Format %s is not supported as FBO %s attachment (type %u),"
2559 " trying rtInternal format as fallback.\n",
2560 debug_d3dformat(format->f.id), type_string, type);
2562 while (gl_info->gl_ops.gl.p_glGetError());
2564 delete_fbo_attachment(gl_info, type, object);
2565 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->rt_internal);
2567 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2568 checkGLcall("Framebuffer format check");
2570 if (status == GL_FRAMEBUFFER_COMPLETE)
2572 TRACE("Format %s rtInternal format is supported as FBO %s attachment, type %u.\n",
2573 debug_d3dformat(format->f.id), type_string, type);
2574 fallback_fmt_used = TRUE;
2576 else
2578 WARN("Format %s rtInternal format is not supported as FBO %s attachment, type %u.\n",
2579 debug_d3dformat(format->f.id), type_string, type);
2580 format->f.flags[type] &= ~(WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_DEPTH_STENCIL);
2585 if (status == GL_FRAMEBUFFER_COMPLETE
2586 && ((format->f.flags[type] & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING)
2587 || !(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
2588 && !(format->f.flags[type] & WINED3DFMT_FLAG_INTEGER)
2589 && format->f.id != WINED3DFMT_NULL && format->f.id != WINED3DFMT_P8_UINT
2590 && format->format != GL_LUMINANCE && format->format != GL_LUMINANCE_ALPHA
2591 && (format->f.red_size || format->f.alpha_size))
2593 DWORD readback[16 * 16 * 16], color = 0, r_range, a_range;
2594 BYTE r, a;
2595 BOOL match = TRUE;
2596 GLuint rb;
2598 if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2600 gl_info->fbo_ops.glGenRenderbuffers(1, &rb);
2601 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, rb);
2602 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2603 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 1);
2604 else
2605 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 16, 16);
2606 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
2607 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
2608 checkGLcall("RB attachment");
2611 gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2612 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2613 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
2614 if (gl_info->gl_ops.gl.p_glGetError() == GL_INVALID_FRAMEBUFFER_OPERATION)
2616 while (gl_info->gl_ops.gl.p_glGetError());
2617 TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2618 debug_d3dformat(format->f.id), type);
2619 format->f.flags[type] &= ~WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
2621 else
2623 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2624 if (type == WINED3D_GL_RES_TYPE_TEX_1D)
2625 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
2626 else
2627 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 16);
2628 gl_info->gl_ops.gl.p_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2630 draw_test_quad(ctx, NULL, &black);
2632 gl_info->gl_ops.gl.p_glEnable(GL_BLEND);
2634 draw_test_quad(ctx, NULL, &half_transparent_red);
2636 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2638 switch (type)
2640 case WINED3D_GL_RES_TYPE_TEX_1D:
2641 /* Rebinding texture to workaround a fglrx bug. */
2642 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, object);
2643 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_1D, 0, GL_BGRA,
2644 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2645 color = readback[7];
2646 break;
2648 case WINED3D_GL_RES_TYPE_TEX_2D:
2649 case WINED3D_GL_RES_TYPE_TEX_3D:
2650 case WINED3D_GL_RES_TYPE_TEX_RECT:
2651 /* Rebinding texture to workaround a fglrx bug. */
2652 gl_info->gl_ops.gl.p_glBindTexture(wined3d_gl_type_to_enum(type), object);
2653 gl_info->gl_ops.gl.p_glGetTexImage(wined3d_gl_type_to_enum(type), 0, GL_BGRA,
2654 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2655 color = readback[7 * 16 + 7];
2656 break;
2658 case WINED3D_GL_RES_TYPE_TEX_CUBE:
2659 /* Rebinding texture to workaround a fglrx bug. */
2660 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, object);
2661 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_BGRA,
2662 GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2663 color = readback[7 * 16 + 7];
2664 break;
2666 case WINED3D_GL_RES_TYPE_RB:
2667 gl_info->gl_ops.gl.p_glReadPixels(0, 0, 16, 16,
2668 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
2669 color = readback[7 * 16 + 7];
2670 break;
2672 case WINED3D_GL_RES_TYPE_BUFFER:
2673 case WINED3D_GL_RES_TYPE_COUNT:
2674 color = 0;
2675 break;
2677 checkGLcall("Post-pixelshader blending check");
2679 a = color >> 24;
2680 r = (color & 0x00ff0000u) >> 16;
2682 r_range = format->f.red_size < 8 ? 1u << (8 - format->f.red_size) : 1;
2683 a_range = format->f.alpha_size < 8 ? 1u << (8 - format->f.alpha_size) : 1;
2684 if (format->f.red_size && (r < 0x7f - r_range || r > 0x7f + r_range))
2685 match = FALSE;
2686 else if (format->f.alpha_size > 1 && (a < 0xbf - a_range || a > 0xbf + a_range))
2687 match = FALSE;
2688 if (!match)
2690 TRACE("Format %s doesn't support post-pixelshader blending, type %u.\n",
2691 debug_d3dformat(format->f.id), type);
2692 TRACE("Color output: %#x\n", color);
2693 format->f.flags[type] &= ~WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
2695 else
2697 TRACE("Format %s supports post-pixelshader blending, type %u.\n",
2698 debug_d3dformat(format->f.id), type);
2699 TRACE("Color output: %#x\n", color);
2700 format->f.flags[type] |= WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
2704 if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL])
2706 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
2707 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
2708 gl_info->fbo_ops.glDeleteRenderbuffers(1, &rb);
2709 checkGLcall("RB cleanup");
2713 if (format->internal != format->srgb_internal)
2715 delete_fbo_attachment(gl_info, type, object);
2716 create_and_bind_fbo_attachment(gl_info, format, type, &object, format->srgb_internal);
2718 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
2719 checkGLcall("Framebuffer format check");
2721 if (status == GL_FRAMEBUFFER_COMPLETE)
2723 TRACE("Format %s's sRGB format is FBO attachable, type %u.\n",
2724 debug_d3dformat(format->f.id), type);
2725 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
2726 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2727 format->internal = format->srgb_internal;
2729 else
2731 WARN("Format %s's sRGB format is not FBO attachable, type %u.\n",
2732 debug_d3dformat(format->f.id), type);
2733 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_WRITE);
2736 else if (status == GL_FRAMEBUFFER_COMPLETE)
2737 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
2739 if (format->f.flags[type] & WINED3DFMT_FLAG_DEPTH_STENCIL)
2741 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
2742 gl_info->fbo_ops.glDeleteRenderbuffers(1, &color_rb);
2745 delete_fbo_attachment(gl_info, type, object);
2746 checkGLcall("Framebuffer format check cleanup");
2749 if (fallback_fmt_used && regular_fmt_used)
2751 FIXME("Format %s needs different render target formats for different resource types.\n",
2752 debug_d3dformat(format->f.id));
2753 format_clear_flag(&format->f, WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_DEPTH_STENCIL
2754 | WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB
2755 | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING);
2759 static void query_format_flag(struct wined3d_gl_info *gl_info, struct wined3d_format_gl *format,
2760 GLint internal, GLenum pname, DWORD flag, const char *string)
2762 GLint value;
2763 enum wined3d_gl_resource_type type;
2765 for (type = 0; type < ARRAY_SIZE(format->f.flags); ++type)
2767 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type), internal, pname, 1, &value);
2768 if (value == GL_FULL_SUPPORT)
2770 TRACE("Format %s supports %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2771 format->f.flags[type] |= flag;
2773 else
2775 TRACE("Format %s doesn't support %s, resource type %u.\n", debug_d3dformat(format->f.id), string, type);
2776 format->f.flags[type] &= ~flag;
2781 /* Context activation is done by the caller. */
2782 static void init_format_fbo_compat_info(const struct wined3d_adapter *adapter,
2783 struct wined3d_caps_gl_ctx *ctx)
2785 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2786 unsigned int i, type;
2787 GLuint fbo;
2789 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
2791 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2793 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2794 BOOL fallback_fmt_used = FALSE, regular_fmt_used = FALSE;
2795 GLenum rt_internal = format->rt_internal;
2796 GLint value;
2798 if (!format->internal)
2799 continue;
2801 for (type = 0; type < ARRAY_SIZE(format->f.flags); ++type)
2803 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2804 format->internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2805 if (value == GL_FULL_SUPPORT)
2807 TRACE("Format %s is supported as FBO color attachment, resource type %u.\n",
2808 debug_d3dformat(format->f.id), type);
2809 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE;
2810 format->rt_internal = format->internal;
2811 regular_fmt_used = TRUE;
2813 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2814 format->internal, GL_FRAMEBUFFER_BLEND, 1, &value);
2815 if (value == GL_FULL_SUPPORT)
2817 TRACE("Format %s supports post-pixelshader blending, resource type %u.\n",
2818 debug_d3dformat(format->f.id), type);
2819 format->f.flags[type] |= WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
2821 else
2823 TRACE("Format %s doesn't support post-pixelshader blending, resource typed %u.\n",
2824 debug_d3dformat(format->f.id), type);
2825 format->f.flags[type] &= ~WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
2828 else
2830 if (!rt_internal)
2832 if (format->f.flags[type] & WINED3DFMT_FLAG_RENDERTARGET)
2834 WARN("Format %s with rendertarget flag is not supported as FBO color attachment"
2835 " and no fallback specified, resource type %u.\n",
2836 debug_d3dformat(format->f.id), type);
2837 format->f.flags[type] &= ~WINED3DFMT_FLAG_RENDERTARGET;
2839 else
2840 TRACE("Format %s is not supported as FBO color attachment,"
2841 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2842 format->rt_internal = format->internal;
2844 else
2846 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2847 rt_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2848 if (value == GL_FULL_SUPPORT)
2850 TRACE("Format %s rtInternal format is supported as FBO color attachment,"
2851 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2852 fallback_fmt_used = TRUE;
2854 else
2856 WARN("Format %s rtInternal format is not supported as FBO color attachment,"
2857 " resource type %u.\n", debug_d3dformat(format->f.id), type);
2858 format->f.flags[type] &= ~WINED3DFMT_FLAG_RENDERTARGET;
2863 if (format->internal != format->srgb_internal)
2865 gl_info->gl_ops.ext.p_glGetInternalformativ(wined3d_gl_type_to_enum(type),
2866 format->srgb_internal, GL_FRAMEBUFFER_RENDERABLE, 1, &value);
2867 if (value == GL_FULL_SUPPORT)
2869 TRACE("Format %s's sRGB format is FBO attachable, resource type %u.\n",
2870 debug_d3dformat(format->f.id), type);
2871 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
2872 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
2873 format->internal = format->srgb_internal;
2875 else
2877 WARN("Format %s's sRGB format is not FBO attachable, resource type %u.\n",
2878 debug_d3dformat(format->f.id), type);
2879 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_WRITE);
2882 else if (format->f.flags[type] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
2883 format->f.flags[type] |= WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
2886 if (fallback_fmt_used && regular_fmt_used)
2888 FIXME("Format %s needs different render target formats for different resource types.\n",
2889 debug_d3dformat(format->f.id));
2890 format_clear_flag(&format->f, WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_FBO_ATTACHABLE
2891 | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB | WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING);
2894 return;
2897 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2899 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
2900 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2901 gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);
2902 gl_info->gl_ops.gl.p_glReadBuffer(GL_COLOR_ATTACHMENT0);
2905 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
2907 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
2909 if (!format->internal)
2910 continue;
2912 if (format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2914 TRACE("Skipping format %s because it's a compressed format.\n",
2915 debug_d3dformat(format->f.id));
2916 continue;
2919 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2921 TRACE("Checking if format %s is supported as FBO color attachment...\n", debug_d3dformat(format->f.id));
2922 check_fbo_compat(ctx, format);
2924 else
2926 format->rt_internal = format->internal;
2930 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2931 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
2934 static GLenum lookup_gl_view_class(GLenum internal_format)
2936 static const struct
2938 GLenum internal_format;
2939 GLenum view_class;
2941 view_classes[] =
2943 /* 128-bit */
2944 {GL_RGBA32F, GL_VIEW_CLASS_128_BITS},
2945 {GL_RGBA32UI, GL_VIEW_CLASS_128_BITS},
2946 {GL_RGBA32I, GL_VIEW_CLASS_128_BITS},
2947 /* 96-bit */
2948 {GL_RGB32F, GL_VIEW_CLASS_96_BITS},
2949 {GL_RGB32UI, GL_VIEW_CLASS_96_BITS},
2950 {GL_RGB32I, GL_VIEW_CLASS_96_BITS},
2951 /* 64-bit */
2952 {GL_RGBA16F, GL_VIEW_CLASS_64_BITS},
2953 {GL_RG32F, GL_VIEW_CLASS_64_BITS},
2954 {GL_RGBA16UI, GL_VIEW_CLASS_64_BITS},
2955 {GL_RG32UI, GL_VIEW_CLASS_64_BITS},
2956 {GL_RGBA16I, GL_VIEW_CLASS_64_BITS},
2957 {GL_RG32I, GL_VIEW_CLASS_64_BITS},
2958 {GL_RGBA16, GL_VIEW_CLASS_64_BITS},
2959 {GL_RGBA16_SNORM, GL_VIEW_CLASS_64_BITS},
2960 /* 48-bit */
2961 {GL_RGB16, GL_VIEW_CLASS_48_BITS},
2962 {GL_RGB16_SNORM, GL_VIEW_CLASS_48_BITS},
2963 {GL_RGB16F, GL_VIEW_CLASS_48_BITS},
2964 {GL_RGB16UI, GL_VIEW_CLASS_48_BITS},
2965 {GL_RGB16I, GL_VIEW_CLASS_48_BITS},
2966 /* 32-bit */
2967 {GL_RG16F, GL_VIEW_CLASS_32_BITS},
2968 {GL_R11F_G11F_B10F, GL_VIEW_CLASS_32_BITS},
2969 {GL_R32F, GL_VIEW_CLASS_32_BITS},
2970 {GL_RGB10_A2UI, GL_VIEW_CLASS_32_BITS},
2971 {GL_RGBA8UI, GL_VIEW_CLASS_32_BITS},
2972 {GL_RG16UI, GL_VIEW_CLASS_32_BITS},
2973 {GL_R32UI, GL_VIEW_CLASS_32_BITS},
2974 {GL_RGBA8I, GL_VIEW_CLASS_32_BITS},
2975 {GL_RG16I, GL_VIEW_CLASS_32_BITS},
2976 {GL_R32I, GL_VIEW_CLASS_32_BITS},
2977 {GL_RGB10_A2, GL_VIEW_CLASS_32_BITS},
2978 {GL_RGBA8, GL_VIEW_CLASS_32_BITS},
2979 {GL_RG16, GL_VIEW_CLASS_32_BITS},
2980 {GL_RGBA8_SNORM, GL_VIEW_CLASS_32_BITS},
2981 {GL_RG16_SNORM, GL_VIEW_CLASS_32_BITS},
2982 {GL_SRGB8_ALPHA8, GL_VIEW_CLASS_32_BITS},
2983 {GL_RGB9_E5, GL_VIEW_CLASS_32_BITS},
2984 /* 24-bit */
2985 {GL_RGB8, GL_VIEW_CLASS_24_BITS},
2986 {GL_RGB8_SNORM, GL_VIEW_CLASS_24_BITS},
2987 {GL_SRGB8, GL_VIEW_CLASS_24_BITS},
2988 {GL_RGB8UI, GL_VIEW_CLASS_24_BITS},
2989 {GL_RGB8I, GL_VIEW_CLASS_24_BITS},
2990 /* 16-bit */
2991 {GL_R16F, GL_VIEW_CLASS_16_BITS},
2992 {GL_RG8UI, GL_VIEW_CLASS_16_BITS},
2993 {GL_R16UI, GL_VIEW_CLASS_16_BITS},
2994 {GL_RG8I, GL_VIEW_CLASS_16_BITS},
2995 {GL_R16I, GL_VIEW_CLASS_16_BITS},
2996 {GL_RG8, GL_VIEW_CLASS_16_BITS},
2997 {GL_R16, GL_VIEW_CLASS_16_BITS},
2998 {GL_RG8_SNORM, GL_VIEW_CLASS_16_BITS},
2999 {GL_R16_SNORM, GL_VIEW_CLASS_16_BITS},
3000 /* 8-bit */
3001 {GL_R8UI, GL_VIEW_CLASS_8_BITS},
3002 {GL_R8I, GL_VIEW_CLASS_8_BITS},
3003 {GL_R8, GL_VIEW_CLASS_8_BITS},
3004 {GL_R8_SNORM, GL_VIEW_CLASS_8_BITS},
3006 /* RGTC1 */
3007 {GL_COMPRESSED_RED_RGTC1, GL_VIEW_CLASS_RGTC1_RED},
3008 {GL_COMPRESSED_SIGNED_RED_RGTC1, GL_VIEW_CLASS_RGTC1_RED},
3009 /* RGTC2 */
3010 {GL_COMPRESSED_RG_RGTC2, GL_VIEW_CLASS_RGTC2_RG},
3011 {GL_COMPRESSED_SIGNED_RG_RGTC2, GL_VIEW_CLASS_RGTC2_RG},
3013 /* BPTC unorm */
3014 {GL_COMPRESSED_RGBA_BPTC_UNORM, GL_VIEW_CLASS_BPTC_UNORM},
3015 {GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, GL_VIEW_CLASS_BPTC_UNORM},
3016 /* BPTC float */
3017 {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_VIEW_CLASS_BPTC_FLOAT},
3018 {GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, GL_VIEW_CLASS_BPTC_FLOAT},
3020 /* DXT1 RGB */
3021 {GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGB},
3022 {GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGB},
3023 /* DXT1 RGBA */
3024 {GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGBA},
3025 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_VIEW_CLASS_S3TC_DXT1_RGBA},
3026 /* DXT3 */
3027 {GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_VIEW_CLASS_S3TC_DXT3_RGBA},
3028 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_VIEW_CLASS_S3TC_DXT3_RGBA},
3029 /* DXT5 */
3030 {GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_VIEW_CLASS_S3TC_DXT5_RGBA},
3031 {GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_VIEW_CLASS_S3TC_DXT5_RGBA},
3034 unsigned int i;
3036 for (i = 0; i < ARRAY_SIZE(view_classes); ++i)
3038 if (view_classes[i].internal_format == internal_format)
3039 return view_classes[i].view_class;
3042 return GL_NONE;
3045 static void query_view_class(struct wined3d_format_gl *format)
3047 GLenum internal_view_class, gamma_view_class, rt_view_class;
3049 internal_view_class = lookup_gl_view_class(format->internal);
3050 gamma_view_class = lookup_gl_view_class(format->srgb_internal);
3051 rt_view_class = lookup_gl_view_class(format->rt_internal);
3053 if (internal_view_class == gamma_view_class || gamma_view_class == rt_view_class)
3055 format->view_class = internal_view_class;
3056 TRACE("Format %s is member of GL view class %#x.\n",
3057 debug_d3dformat(format->f.id), format->view_class);
3059 else
3061 format->view_class = GL_NONE;
3065 static void query_internal_format(struct wined3d_adapter *adapter,
3066 struct wined3d_format_gl *format, const struct wined3d_format_texture_info *texture_info,
3067 struct wined3d_gl_info *gl_info, BOOL srgb_write_supported, BOOL srgb_format)
3069 GLint count, multisample_types[8];
3070 unsigned int i, max_log2;
3071 GLenum target;
3073 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
3075 query_format_flag(gl_info, format, format->internal, GL_VERTEX_TEXTURE,
3076 WINED3DFMT_FLAG_VTF, "vertex texture usage");
3077 query_format_flag(gl_info, format, format->internal, GL_FILTER,
3078 WINED3DFMT_FLAG_FILTERING, "filtering");
3079 query_format_flag(gl_info, format, format->internal, GL_SHADER_IMAGE_STORE,
3080 WINED3DFMT_FLAG_UNORDERED_ACCESS, "unordered access");
3082 if (srgb_format || format->srgb_internal != format->internal)
3084 query_format_flag(gl_info, format, format->srgb_internal, GL_SRGB_READ,
3085 WINED3DFMT_FLAG_SRGB_READ, "sRGB read");
3087 if (srgb_write_supported)
3088 query_format_flag(gl_info, format, format->srgb_internal, GL_SRGB_WRITE,
3089 WINED3DFMT_FLAG_SRGB_WRITE, "sRGB write");
3090 else
3091 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_WRITE);
3093 if (!(format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D]
3094 & (WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE)))
3095 format->srgb_internal = format->internal;
3096 else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3097 format->internal = format->srgb_internal;
3100 else
3102 if (!gl_info->limits.samplers[WINED3D_SHADER_TYPE_VERTEX])
3103 format_clear_flag(&format->f, WINED3DFMT_FLAG_VTF);
3105 if (!(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
3106 format_set_flag(&format->f, WINED3DFMT_FLAG_FILTERING);
3107 else if (format->f.id != WINED3DFMT_R32G32B32A32_FLOAT && format->f.id != WINED3DFMT_R32_FLOAT)
3108 format_clear_flag(&format->f, WINED3DFMT_FLAG_VTF);
3110 if (srgb_format || format->srgb_internal != format->internal)
3112 /* Filter sRGB capabilities if EXT_texture_sRGB is not supported. */
3113 if (!gl_info->supported[EXT_TEXTURE_SRGB])
3115 format->srgb_internal = format->internal;
3116 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE);
3118 else if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
3120 format->internal = format->srgb_internal;
3124 if ((format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_SRGB_WRITE) && !srgb_write_supported)
3125 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_WRITE);
3128 if ((!gl_info->supported[ARB_DEPTH_TEXTURE] || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3129 && (format->f.depth_size || format->f.stencil_size))
3131 TRACE("Disabling texturing support for depth / stencil format %s.\n", debug_d3dformat(format->f.id));
3132 format->f.flags[WINED3D_GL_RES_TYPE_TEX_1D] &= ~WINED3DFMT_FLAG_TEXTURE;
3133 format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] &= ~WINED3DFMT_FLAG_TEXTURE;
3134 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3135 format->f.flags[WINED3D_GL_RES_TYPE_TEX_CUBE] &= ~WINED3DFMT_FLAG_TEXTURE;
3136 format->f.flags[WINED3D_GL_RES_TYPE_TEX_RECT] &= ~WINED3DFMT_FLAG_TEXTURE;
3139 query_view_class(format);
3141 if (format->internal && format->f.flags[WINED3D_GL_RES_TYPE_RB]
3142 & (WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_DEPTH_STENCIL)
3143 && (gl_info->supported[ARB_FRAMEBUFFER_OBJECT] || gl_info->supported[EXT_FRAMEBUFFER_MULTISAMPLE])
3144 && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3146 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY])
3148 target = gl_info->supported[ARB_TEXTURE_MULTISAMPLE] ? GL_TEXTURE_2D_MULTISAMPLE : GL_RENDERBUFFER;
3149 count = 0;
3150 GL_EXTCALL(glGetInternalformativ(target, format->internal,
3151 GL_NUM_SAMPLE_COUNTS, 1, &count));
3152 if (count > ARRAY_SIZE(multisample_types))
3153 FIXME("Unexpectedly high number of multisample types %d.\n", count);
3154 count = min(count, ARRAY_SIZE(multisample_types));
3155 GL_EXTCALL(glGetInternalformativ(target, format->internal,
3156 GL_SAMPLES, count, multisample_types));
3157 checkGLcall("query sample counts");
3158 for (i = 0; i < count; ++i)
3160 if (multisample_types[i] > sizeof(format->f.multisample_types) * CHAR_BIT)
3161 continue;
3162 format->f.multisample_types |= 1u << (multisample_types[i] - 1);
3165 else
3167 max_log2 = wined3d_log2i(min(gl_info->limits.samples,
3168 sizeof(format->f.multisample_types) * CHAR_BIT));
3169 for (i = 1; i <= max_log2; ++i)
3170 format->f.multisample_types |= 1u << ((1u << i) - 1);
3175 static BOOL init_format_texture_info(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
3177 struct wined3d_format_gl *format, *srgb_format;
3178 struct fragment_caps fragment_caps;
3179 struct shader_caps shader_caps;
3180 unsigned int i, j;
3181 BOOL srgb_write;
3183 adapter->fragment_pipe->get_caps(adapter, &fragment_caps);
3184 adapter->shader_backend->shader_get_caps(adapter, &shader_caps);
3185 srgb_write = (fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_SRGB_WRITE)
3186 && (shader_caps.wined3d_caps & WINED3D_SHADER_CAP_SRGB_WRITE);
3188 for (i = 0; i < ARRAY_SIZE(format_texture_info); ++i)
3190 if (!(format = get_format_gl_internal(adapter, format_texture_info[i].id)))
3191 return FALSE;
3193 if (!gl_info->supported[format_texture_info[i].extension])
3194 continue;
3196 /* ARB_texture_rg defines floating point formats, but only if
3197 * ARB_texture_float is also supported. */
3198 if (!gl_info->supported[ARB_TEXTURE_FLOAT]
3199 && (format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT))
3200 continue;
3202 /* ARB_texture_rg defines integer formats if EXT_texture_integer is also supported. */
3203 if (!gl_info->supported[EXT_TEXTURE_INTEGER]
3204 && (format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_INTEGER))
3205 continue;
3207 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
3208 && (format->f.id == WINED3DFMT_D16_LOCKABLE || format->f.id == WINED3DFMT_NULL))
3209 continue;
3211 format->internal = format_texture_info[i].gl_internal;
3212 format->srgb_internal = format_texture_info[i].gl_srgb_internal;
3213 format->rt_internal = format_texture_info[i].gl_rt_internal;
3214 format->format = format_texture_info[i].gl_format;
3215 format->type = format_texture_info[i].gl_type;
3216 format->f.color_fixup = COLOR_FIXUP_IDENTITY;
3217 format->f.height_scale.numerator = 1;
3218 format->f.height_scale.denominator = 1;
3220 format->f.flags[WINED3D_GL_RES_TYPE_TEX_1D] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3221 format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3222 format->f.flags[WINED3D_GL_RES_TYPE_BUFFER] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3224 /* GL_ARB_depth_texture does not support 3D textures. It also says "cube textures are
3225 * problematic", but doesn't explicitly mandate that an error is generated. */
3226 if (gl_info->supported[EXT_TEXTURE3D] && !(format_texture_info[i].flags & WINED3DFMT_FLAG_DEPTH_STENCIL))
3227 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3229 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3230 format->f.flags[WINED3D_GL_RES_TYPE_TEX_CUBE] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3232 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3233 format->f.flags[WINED3D_GL_RES_TYPE_TEX_RECT] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3235 format->f.flags[WINED3D_GL_RES_TYPE_RB] |= format_texture_info[i].flags | WINED3DFMT_FLAG_BLIT;
3236 format->f.flags[WINED3D_GL_RES_TYPE_RB] &= ~WINED3DFMT_FLAG_TEXTURE;
3238 if (format->srgb_internal != format->internal
3239 && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
3241 format->srgb_internal = format->internal;
3242 format_clear_flag(&format->f, WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE);
3245 if (!gl_info->supported[ARB_SHADOW] && (format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_SHADOW))
3246 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3248 query_internal_format(adapter, format, &format_texture_info[i], gl_info, srgb_write, FALSE);
3250 /* Texture conversion stuff */
3251 format->f.conv_byte_count = format_texture_info[i].conv_byte_count;
3252 format->f.upload = format_texture_info[i].upload;
3253 format->f.download = format_texture_info[i].download;
3255 srgb_format = NULL;
3256 for (j = 0; j < ARRAY_SIZE(format_srgb_info); ++j)
3258 if (format_srgb_info[j].base_format_id == format->f.id)
3260 if (!(srgb_format = get_format_gl_internal(adapter, format_srgb_info[j].srgb_format_id)))
3261 return FALSE;
3262 break;
3265 if (!srgb_format)
3266 continue;
3268 copy_format(adapter, &srgb_format->f, &format->f);
3270 if (gl_info->supported[EXT_TEXTURE_SRGB]
3271 && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
3273 srgb_format->internal = format_texture_info[i].gl_srgb_internal;
3274 srgb_format->srgb_internal = format_texture_info[i].gl_srgb_internal;
3275 format_set_flag(&srgb_format->f, WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE);
3276 query_internal_format(adapter, srgb_format, &format_texture_info[i], gl_info, srgb_write, TRUE);
3280 return TRUE;
3283 static BOOL compare_uint(unsigned int x, unsigned int y, unsigned int max_diff)
3285 unsigned int diff = x > y ? x - y : y - x;
3287 return diff <= max_diff;
3290 static BOOL compare_colour(DWORD c1, DWORD c2, BYTE max_diff)
3292 return compare_uint(c1 & 0xff, c2 & 0xff, max_diff)
3293 && compare_uint((c1 >> 8) & 0xff, (c2 >> 8) & 0xff, max_diff)
3294 && compare_uint((c1 >> 16) & 0xff, (c2 >> 16) & 0xff, max_diff)
3295 && compare_uint((c1 >> 24) & 0xff, (c2 >> 24) & 0xff, max_diff);
3298 /* A context is provided by the caller */
3299 static BOOL check_filter(const struct wined3d_gl_info *gl_info, GLenum internal)
3301 static const DWORD data[] = {0x00000000, 0xffffffff};
3302 GLuint tex, fbo, buffer;
3303 DWORD readback[16 * 1];
3304 BOOL ret = FALSE;
3306 /* Render a filtered texture and see what happens. This is intended to detect the lack of
3307 * float16 filtering on ATI X1000 class cards. The drivers disable filtering instead of
3308 * falling back to software. If this changes in the future this code will get fooled and
3309 * apps might hit the software path due to incorrectly advertised caps.
3311 * Its unlikely that this changes however. GL Games like Mass Effect depend on the filter
3312 * disable fallback, if Apple or ATI ever change the driver behavior they will break more
3313 * than Wine. The Linux binary <= r500 driver is not maintained any more anyway
3316 while (gl_info->gl_ops.gl.p_glGetError());
3318 gl_info->gl_ops.gl.p_glGenTextures(1, &buffer);
3319 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3320 memset(readback, 0x7e, sizeof(readback));
3321 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 16, 1, 0,
3322 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, readback);
3323 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3324 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3325 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3326 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3327 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
3329 gl_info->gl_ops.gl.p_glGenTextures(1, &tex);
3330 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, tex);
3331 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, internal, 2, 1, 0,
3332 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
3333 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3334 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3335 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3336 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3337 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
3338 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
3340 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3341 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3342 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffer, 0);
3343 gl_info->gl_ops.gl.p_glDrawBuffer(GL_COLOR_ATTACHMENT0);
3345 gl_info->gl_ops.gl.p_glViewport(0, 0, 16, 1);
3346 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3347 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3348 gl_info->gl_ops.gl.p_glLoadIdentity();
3349 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3350 gl_info->gl_ops.gl.p_glLoadIdentity();
3352 gl_info->gl_ops.gl.p_glClearColor(0, 1, 0, 0);
3353 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3355 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
3356 gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 0.0);
3357 gl_info->gl_ops.gl.p_glVertex2f(-1.0f, -1.0f);
3358 gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 0.0);
3359 gl_info->gl_ops.gl.p_glVertex2f(1.0f, -1.0f);
3360 gl_info->gl_ops.gl.p_glTexCoord2f(0.0, 1.0);
3361 gl_info->gl_ops.gl.p_glVertex2f(-1.0f, 1.0f);
3362 gl_info->gl_ops.gl.p_glTexCoord2f(1.0, 1.0);
3363 gl_info->gl_ops.gl.p_glVertex2f(1.0f, 1.0f);
3364 gl_info->gl_ops.gl.p_glEnd();
3366 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, buffer);
3367 memset(readback, 0x7f, sizeof(readback));
3368 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3369 if (compare_colour(readback[6], 0xffffffff, 5) || compare_colour(readback[6], 0x00000000, 5)
3370 || compare_colour(readback[9], 0xffffffff, 5) || compare_colour(readback[9], 0x00000000, 5))
3372 TRACE("Read back colors 0x%08x and 0x%08x close to unfiltered color, assuming no filtering\n",
3373 readback[6], readback[9]);
3374 ret = FALSE;
3376 else
3378 TRACE("Read back colors are 0x%08x and 0x%08x, assuming texture is filtered\n",
3379 readback[6], readback[9]);
3380 ret = TRUE;
3383 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3384 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3385 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex);
3386 gl_info->gl_ops.gl.p_glDeleteTextures(1, &buffer);
3387 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3389 if (gl_info->gl_ops.gl.p_glGetError())
3391 FIXME("Error during filtering test for format %x, returning no filtering\n", internal);
3392 ret = FALSE;
3395 return ret;
3398 static void init_format_filter_info(struct wined3d_adapter *adapter,
3399 struct wined3d_gl_info *gl_info)
3401 enum wined3d_pci_vendor vendor = adapter->driver_info.vendor;
3402 struct wined3d_format_gl *format;
3403 unsigned int i;
3404 static const enum wined3d_format_id fmts16[] =
3406 WINED3DFMT_R16_FLOAT,
3407 WINED3DFMT_R16G16_FLOAT,
3408 WINED3DFMT_R16G16B16A16_FLOAT,
3410 BOOL filtered;
3412 /* This was already handled by init_format_texture_info(). */
3413 if (gl_info->supported[ARB_INTERNALFORMAT_QUERY2])
3414 return;
3416 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
3417 || !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3419 if (vendor == HW_VENDOR_NVIDIA && gl_info->supported[ARB_TEXTURE_FLOAT])
3421 TRACE("Nvidia card with texture_float support: Assuming float16 blending\n");
3422 filtered = TRUE;
3424 else if (gl_info->limits.glsl_varyings > 44)
3426 TRACE("More than 44 GLSL varyings - assuming d3d10 card with float16 blending\n");
3427 filtered = TRUE;
3429 else
3431 TRACE("Assuming no float16 blending\n");
3432 filtered = FALSE;
3435 if (filtered)
3437 for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3439 format = get_format_gl_internal(adapter, fmts16[i]);
3440 format_set_flag(&format->f, WINED3DFMT_FLAG_FILTERING);
3443 return;
3446 for (i = 0; i < ARRAY_SIZE(fmts16); ++i)
3448 format = get_format_gl_internal(adapter, fmts16[i]);
3449 if (!format->internal)
3450 continue; /* Not supported by GL */
3452 filtered = check_filter(gl_info, format->internal);
3453 if (filtered)
3455 TRACE("Format %s supports filtering.\n", debug_d3dformat(format->f.id));
3456 format_set_flag(&format->f, WINED3DFMT_FLAG_FILTERING);
3458 else
3460 TRACE("Format %s does not support filtering.\n", debug_d3dformat(format->f.id));
3465 static enum fixup_channel_source fixup_source_from_char(char c)
3467 switch (c)
3469 default:
3470 case '0':
3471 return CHANNEL_SOURCE_ZERO;
3472 case '1':
3473 return CHANNEL_SOURCE_ONE;
3474 case 'x':
3475 case 'X':
3476 return CHANNEL_SOURCE_X;
3477 case 'y':
3478 case 'Y':
3479 return CHANNEL_SOURCE_Y;
3480 case 'z':
3481 case 'Z':
3482 return CHANNEL_SOURCE_Z;
3483 case 'w':
3484 case 'W':
3485 return CHANNEL_SOURCE_W;
3489 static unsigned int fixup_sign_from_char(char c)
3491 if (c == 'x' || c == 'y' || c == 'z' || c == 'w')
3492 return 1;
3493 return 0;
3496 static struct color_fixup_desc create_color_fixup_desc_from_string(const char *s)
3498 struct color_fixup_desc fixup;
3500 if (strlen(s) != 4)
3502 ERR("Invalid fixup string %s.\n", wine_dbgstr_a(s));
3503 return COLOR_FIXUP_IDENTITY;
3506 fixup.x_sign_fixup = fixup_sign_from_char(s[0]);
3507 fixup.x_source = fixup_source_from_char(s[0]);
3508 fixup.y_sign_fixup = fixup_sign_from_char(s[1]);
3509 fixup.y_source = fixup_source_from_char(s[1]);
3510 fixup.z_sign_fixup = fixup_sign_from_char(s[2]);
3511 fixup.z_source = fixup_source_from_char(s[2]);
3512 fixup.w_sign_fixup = fixup_sign_from_char(s[3]);
3513 fixup.w_source = fixup_source_from_char(s[3]);
3515 return fixup;
3518 static void apply_format_fixups(struct wined3d_adapter *adapter, struct wined3d_gl_info *gl_info)
3520 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
3521 struct wined3d_format_gl *format;
3522 BOOL use_legacy_fixups;
3523 unsigned int i;
3525 static const struct
3527 enum wined3d_format_id id;
3528 const char *fixup;
3529 BOOL legacy;
3530 enum wined3d_gl_extension extension;
3532 fixups[] =
3534 {WINED3DFMT_R16_FLOAT, "X11W", TRUE, WINED3D_GL_EXT_NONE},
3535 {WINED3DFMT_R32_FLOAT, "X11W", TRUE, WINED3D_GL_EXT_NONE},
3536 {WINED3DFMT_R16G16_UNORM, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3537 {WINED3DFMT_R16G16_FLOAT, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3538 {WINED3DFMT_R32G32_FLOAT, "XY1W", TRUE, WINED3D_GL_EXT_NONE},
3540 {WINED3DFMT_R8G8_SNORM, "xy11", FALSE, WINED3D_GL_EXT_NONE},
3541 {WINED3DFMT_R8G8_SNORM, "XY11", TRUE, NV_TEXTURE_SHADER},
3542 {WINED3DFMT_R8G8_SNORM, "XY11", TRUE, EXT_TEXTURE_SNORM},
3544 {WINED3DFMT_R16G16_SNORM, "xy11", FALSE, WINED3D_GL_EXT_NONE},
3545 {WINED3DFMT_R16G16_SNORM, "XY11", TRUE, NV_TEXTURE_SHADER},
3546 {WINED3DFMT_R16G16_SNORM, "XY11", TRUE, EXT_TEXTURE_SNORM},
3548 {WINED3DFMT_R8G8B8A8_SNORM, "xyzw", FALSE, WINED3D_GL_EXT_NONE},
3549 {WINED3DFMT_R8G8B8A8_SNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3550 {WINED3DFMT_R8G8B8A8_SNORM, "XYZW", FALSE, EXT_TEXTURE_SNORM},
3552 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "xzY1", FALSE, WINED3D_GL_EXT_NONE},
3553 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3554 {WINED3DFMT_R5G5_SNORM_L6_UNORM, "XYZW", FALSE, EXT_TEXTURE_SNORM},
3556 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "xyZW", FALSE, WINED3D_GL_EXT_NONE},
3557 {WINED3DFMT_R8G8_SNORM_L8X8_UNORM, "XYZW", FALSE, NV_TEXTURE_SHADER},
3559 {WINED3DFMT_ATI1N, "XXXX", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
3560 {WINED3DFMT_ATI1N, "XXXX", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3562 {WINED3DFMT_ATI2N, "XW11", FALSE, ATI_TEXTURE_COMPRESSION_3DC},
3563 {WINED3DFMT_ATI2N, "YX11", FALSE, EXT_TEXTURE_COMPRESSION_RGTC},
3564 {WINED3DFMT_ATI2N, "YX11", FALSE, ARB_TEXTURE_COMPRESSION_RGTC},
3566 {WINED3DFMT_A8_UNORM, "000X", FALSE, WINED3D_GL_EXT_NONE},
3567 {WINED3DFMT_A8_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3569 {WINED3DFMT_L8A8_UNORM, "XXXY", FALSE, WINED3D_GL_EXT_NONE},
3570 {WINED3DFMT_L8A8_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3572 {WINED3DFMT_L4A4_UNORM, "XXXY", FALSE, WINED3D_GL_EXT_NONE},
3573 {WINED3DFMT_L4A4_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3575 {WINED3DFMT_L16_UNORM, "XXX1", FALSE, WINED3D_GL_EXT_NONE},
3576 {WINED3DFMT_L16_UNORM, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3578 {WINED3DFMT_INTZ, "XXXX", FALSE, WINED3D_GL_EXT_NONE},
3579 {WINED3DFMT_INTZ, "XYZW", FALSE, WINED3D_GL_LEGACY_CONTEXT},
3581 {WINED3DFMT_L8_UNORM, "XXX1", FALSE, ARB_TEXTURE_RG},
3584 use_legacy_fixups = d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR;
3585 for (i = 0; i < ARRAY_SIZE(fixups); ++i)
3587 if (fixups[i].legacy && !use_legacy_fixups)
3588 continue;
3590 if (!gl_info->supported[fixups[i].extension])
3591 continue;
3593 format = get_format_gl_internal(adapter, fixups[i].id);
3594 format->f.color_fixup = create_color_fixup_desc_from_string(fixups[i].fixup);
3597 if (!gl_info->supported[APPLE_YCBCR_422] && !gl_info->supported[APPLE_RGB_422]
3598 && (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->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUY2);
3604 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3605 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_UYVY);
3607 else if (!gl_info->supported[APPLE_YCBCR_422] && gl_info->supported[APPLE_RGB_422])
3609 format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3610 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);
3612 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3613 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YUV);
3615 else if (!gl_info->supported[APPLE_YCBCR_422] && (!gl_info->supported[ARB_FRAGMENT_PROGRAM]
3616 && (!gl_info->supported[ARB_FRAGMENT_SHADER] || !gl_info->supported[ARB_VERTEX_SHADER])))
3618 format = get_format_gl_internal(adapter, WINED3DFMT_YUY2);
3619 format_clear_flag(&format->f, WINED3DFMT_FLAG_BLIT);
3620 format->internal = 0;
3622 format = get_format_gl_internal(adapter, WINED3DFMT_UYVY);
3623 format_clear_flag(&format->f, WINED3DFMT_FLAG_BLIT);
3624 format->internal = 0;
3627 if (gl_info->supported[ARB_FRAGMENT_PROGRAM]
3628 || (gl_info->supported[ARB_FRAGMENT_SHADER] && gl_info->supported[ARB_VERTEX_SHADER]))
3630 format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3631 format_set_flag(&format->f, WINED3DFMT_FLAG_HEIGHT_SCALE);
3632 format->f.height_scale.numerator = 3;
3633 format->f.height_scale.denominator = 2;
3634 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_YV12);
3636 format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3637 format_set_flag(&format->f, WINED3DFMT_FLAG_HEIGHT_SCALE);
3638 format->f.height_scale.numerator = 3;
3639 format->f.height_scale.denominator = 2;
3640 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_NV12);
3642 else
3644 format = get_format_gl_internal(adapter, WINED3DFMT_YV12);
3645 format_clear_flag(&format->f, WINED3DFMT_FLAG_BLIT);
3646 format->internal = 0;
3648 format = get_format_gl_internal(adapter, WINED3DFMT_NV12);
3649 format_clear_flag(&format->f, WINED3DFMT_FLAG_BLIT);
3650 format->internal = 0;
3653 if (gl_info->supported[ARB_FRAGMENT_PROGRAM] || gl_info->supported[ARB_FRAGMENT_SHADER])
3655 format = get_format_gl_internal(adapter, WINED3DFMT_P8_UINT);
3656 format->f.color_fixup = create_complex_fixup_desc(COMPLEX_FIXUP_P8);
3659 if (!gl_info->supported[ARB_HALF_FLOAT_PIXEL])
3661 format = get_format_gl_internal(adapter, WINED3DFMT_R16_FLOAT);
3662 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3664 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16_FLOAT);
3665 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3667 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_FLOAT);
3668 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3671 if (gl_info->quirks & WINED3D_QUIRK_BROKEN_RGBA16)
3673 format = get_format_gl_internal(adapter, WINED3DFMT_R16G16B16A16_UNORM);
3674 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3677 /* ATI instancing hack: Although ATI cards do not support Shader Model
3678 * 3.0, they support instancing. To query if the card supports instancing
3679 * CheckDeviceFormat() with the special format MAKEFOURCC('I','N','S','T')
3680 * is used. Should an application check for this, provide a proper return
3681 * value. We can do instancing with all shader versions, but we need
3682 * vertex shaders.
3684 * Additionally applications have to set the D3DRS_POINTSIZE render state
3685 * to MAKEFOURCC('I','N','S','T') once to enable instancing. Wined3d
3686 * doesn't need that and just ignores it.
3688 * With Shader Model 3.0 capable cards Instancing 'just works' in Windows. */
3689 /* FIXME: This should just check the shader backend caps. */
3690 if (gl_info->supported[ARB_VERTEX_PROGRAM] || gl_info->supported[ARB_VERTEX_SHADER])
3692 format = get_format_gl_internal(adapter, WINED3DFMT_INST);
3693 format_set_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3696 /* Depth bound test. To query if the card supports it CheckDeviceFormat()
3697 * with the special format MAKEFOURCC('N','V','D','B') is used. It is
3698 * enabled by setting D3DRS_ADAPTIVETESS_X render state to
3699 * MAKEFOURCC('N','V','D','B') and then controlled by setting
3700 * D3DRS_ADAPTIVETESS_Z (zMin) and D3DRS_ADAPTIVETESS_W (zMax) to test
3701 * value. */
3702 if (gl_info->supported[EXT_DEPTH_BOUNDS_TEST])
3704 format = get_format_gl_internal(adapter, WINED3DFMT_NVDB);
3705 format_set_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3708 if (gl_info->supported[ARB_MULTISAMPLE])
3710 format = get_format_gl_internal(adapter, WINED3DFMT_ATOC);
3711 format_set_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3714 /* RESZ aka AMD DX9-level hack for multisampled depth buffer resolve. You query for RESZ
3715 * support by checking for availability of MAKEFOURCC('R','E','S','Z') surfaces with
3716 * RENDERTARGET usage. */
3717 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
3719 format = get_format_gl_internal(adapter, WINED3DFMT_RESZ);
3720 format_set_flag(&format->f, WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_RENDERTARGET);
3723 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3725 format = get_format_gl_by_idx(adapter, i);
3727 if (!(format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE))
3728 continue;
3730 if (is_identity_fixup(format->f.color_fixup))
3731 continue;
3733 TRACE("Checking support for fixup:\n");
3734 dump_color_fixup_desc(format->f.color_fixup);
3735 if (!adapter->shader_backend->shader_color_fixup_supported(format->f.color_fixup)
3736 || !adapter->fragment_pipe->color_fixup_supported(format->f.color_fixup))
3738 TRACE("[FAILED]\n");
3739 format_clear_flag(&format->f, WINED3DFMT_FLAG_TEXTURE);
3741 else
3743 TRACE("[OK]\n");
3747 /* These formats are not supported for 3D textures. See also
3748 * WINED3DFMT_FLAG_DECOMPRESS. */
3749 format = get_format_gl_internal(adapter, WINED3DFMT_ATI1N);
3750 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3751 format = get_format_gl_internal(adapter, WINED3DFMT_ATI2N);
3752 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3753 format = get_format_gl_internal(adapter, WINED3DFMT_BC4_SNORM);
3754 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3755 format = get_format_gl_internal(adapter, WINED3DFMT_BC5_UNORM);
3756 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3757 format = get_format_gl_internal(adapter, WINED3DFMT_BC5_SNORM);
3758 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] &= ~WINED3DFMT_FLAG_TEXTURE;
3761 static BOOL init_format_vertex_info(const struct wined3d_adapter *adapter,
3762 struct wined3d_gl_info *gl_info)
3764 struct wined3d_format_gl *format;
3765 unsigned int i;
3767 for (i = 0; i < ARRAY_SIZE(format_vertex_info); ++i)
3769 if (!(format = get_format_gl_internal(adapter, format_vertex_info[i].id)))
3770 return FALSE;
3772 if (!gl_info->supported[format_vertex_info[i].extension])
3773 continue;
3775 format->f.emit_idx = format_vertex_info[i].emit_idx;
3776 format->vtx_type = format_vertex_info[i].gl_vtx_type;
3777 format->vtx_format = format->f.component_count;
3778 format->f.flags[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3DFMT_FLAG_VERTEX_ATTRIBUTE;
3781 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
3783 format = get_format_gl_internal(adapter, WINED3DFMT_B8G8R8A8_UNORM);
3784 format->vtx_format = GL_BGRA;
3787 return TRUE;
3790 static BOOL init_typeless_formats(const struct wined3d_adapter *adapter)
3792 unsigned int flags[WINED3D_GL_RES_TYPE_COUNT];
3793 unsigned int i, j;
3795 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
3797 struct wined3d_format *format, *typeless_format;
3799 if (!(format = get_format_internal(adapter, typed_formats[i].id)))
3800 return FALSE;
3801 if (!(typeless_format = get_format_internal(adapter, typed_formats[i].typeless_id)))
3802 return FALSE;
3804 memcpy(flags, typeless_format->flags, sizeof(flags));
3805 copy_format(adapter, typeless_format, format);
3806 for (j = 0; j < ARRAY_SIZE(typeless_format->flags); ++j)
3807 typeless_format->flags[j] |= flags[j];
3810 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
3812 struct wined3d_format *typeless_format, *typeless_ds_format, *ds_format;
3813 struct wined3d_format *depth_view_format, *stencil_view_format;
3814 enum wined3d_format_id format_id;
3816 if (!(typeless_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].typeless_id)))
3817 return FALSE;
3818 if (!(ds_format = get_format_internal(adapter, typeless_depth_stencil_formats[i].depth_stencil_id)))
3819 return FALSE;
3821 typeless_ds_format = get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
3822 typeless_ds_format->id = typeless_depth_stencil_formats[i].typeless_id;
3823 copy_format(adapter, typeless_ds_format, ds_format);
3824 for (j = 0; j < ARRAY_SIZE(typeless_ds_format->flags); ++j)
3826 typeless_ds_format->flags[j] = typeless_format->flags[j];
3827 typeless_format->flags[j] &= ~WINED3DFMT_FLAG_DEPTH_STENCIL;
3830 if ((format_id = typeless_depth_stencil_formats[i].depth_view_id)
3831 && typeless_depth_stencil_formats[i].separate_depth_view_format)
3833 if (!(depth_view_format = get_format_internal(adapter, format_id)))
3834 return FALSE;
3835 copy_format(adapter, depth_view_format, ds_format);
3836 depth_view_format->red_size = depth_view_format->depth_size;
3837 depth_view_format->depth_size = 0;
3838 depth_view_format->stencil_size = 0;
3840 if ((format_id = typeless_depth_stencil_formats[i].stencil_view_id))
3842 if (!(stencil_view_format = get_format_internal(adapter, format_id)))
3843 return FALSE;
3844 copy_format(adapter, stencil_view_format, ds_format);
3845 stencil_view_format->green_size = stencil_view_format->stencil_size;
3846 stencil_view_format->depth_size = 0;
3847 stencil_view_format->stencil_size = 0;
3851 return TRUE;
3854 static void init_format_gen_mipmap_info(const struct wined3d_adapter *adapter,
3855 struct wined3d_gl_info *gl_info)
3857 unsigned int i, j;
3859 if (!gl_info->fbo_ops.glGenerateMipmap)
3860 return;
3862 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
3864 struct wined3d_format *format = get_format_by_idx(adapter, i);
3866 for (j = 0; j < ARRAY_SIZE(format->flags); ++j)
3867 if (!(~format->flags[j] & (WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_FILTERING)))
3868 format->flags[j] |= WINED3DFMT_FLAG_GEN_MIPMAP;
3872 BOOL wined3d_caps_gl_ctx_test_viewport_subpixel_bits(struct wined3d_caps_gl_ctx *ctx)
3874 static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
3875 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3876 static const float offset = -63.0f / 128.0f;
3877 GLuint texture, fbo;
3878 DWORD readback[4];
3879 unsigned int i;
3881 gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
3882 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3883 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
3884 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ARRAY_SIZE(readback), 1, 0,
3885 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
3886 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3887 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3888 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
3889 GL_TEXTURE_2D, texture, 0);
3890 checkGLcall("create resources");
3892 gl_info->gl_ops.gl.p_glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
3893 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3894 GL_EXTCALL(glViewportIndexedf(0, offset, offset, 4.0f, 1.0f));
3895 draw_test_quad(ctx, NULL, &red);
3896 checkGLcall("draw");
3898 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3899 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
3900 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3901 checkGLcall("readback");
3903 TRACE("Readback colors are 0x%08x, 0x%08x, 0x%08x, 0x%08x.\n",
3904 readback[0], readback[1], readback[2], readback[3]);
3906 gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
3907 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3908 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3909 checkGLcall("delete resources");
3911 for (i = 0; i < ARRAY_SIZE(readback); ++i)
3913 if (readback[i] != 0xffff0000)
3914 return FALSE;
3916 return TRUE;
3919 bool wined3d_caps_gl_ctx_test_filling_convention(struct wined3d_caps_gl_ctx *ctx, float offset)
3921 static const struct wined3d_color red = {1.0f, 0.0f, 0.0f, 1.0f};
3922 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3923 unsigned int x, y, clear = 0, draw = 0;
3924 GLuint texture, fbo;
3925 DWORD readback[8][8];
3927 /* This is a very simple test to find out how GL handles polygon edges:
3928 * Draw a 1x1 quad exactly through 4 adjacent pixel centers in an 8x8
3929 * viewport and see which pixel it ends up in. So far we've seen top left
3930 * and bottom left conventions. This test may produce unexpected results
3931 * if the driver forces multisampling on us.
3933 * If we find a bottom-left filling behavior we also move the x-axis
3934 * by the same amount. This is necessary to keep diagonals that go
3935 * through the pixel center intact.
3937 * Note that we are ignoring some settings that might influence the
3938 * driver: How we switch GL to an upper-left coordinate system,
3939 * shaders vs fixed function GL. Testing these isn't possible with
3940 * the current draw_test_quad() infrastructure. Also the test is
3941 * skipped if we are not using FBOs. Drawing into the onscreen
3942 * frame buffer may also yield different driver behavior.
3944 * The minimum offset also depends on the viewport size, although
3945 * the relation between those two is GPU dependent and not exactly
3946 * sensible. E.g. a 8192x8192 viewport on a GeForce 9 needs at
3947 * least an offset of 1/240.9, whereas a 8x8 one needs 1/255.982;
3948 * 32x32 needs 1/255.935. 4x4 and lower are happy with something
3949 * below 1/256. The 8x8 size below has been arbitrarily chosen to
3950 * get a useful result out of that card and avoid allocating a
3951 * gigantic texture during library init.
3953 * Newer cards usually do the right thing anyway. In cases where
3954 * they do not (e.g. Radeon GPUs in a macbookpro14,3 running MacOS)
3955 * an offset of 1/2^20 is enough. */
3956 const struct wined3d_vec3 edge_geometry[] =
3958 {(-1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
3959 {( 1.0f + offset) / 8.0f, (-1.0f + offset) / 8.0f, 0.0f},
3960 {(-1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
3961 {( 1.0f + offset) / 8.0f, ( 1.0f + offset) / 8.0f, 0.0f},
3964 gl_info->gl_ops.gl.p_glGenTextures(1, &texture);
3965 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3966 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
3967 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0,
3968 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
3969 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
3970 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3971 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
3972 GL_TEXTURE_2D, texture, 0);
3973 checkGLcall("create resources");
3975 gl_info->gl_ops.gl.p_glViewport(0, 0, 8, 8);
3976 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
3977 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT);
3979 draw_test_quad(ctx, edge_geometry, &red);
3980 checkGLcall("draw");
3982 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, texture);
3983 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0,
3984 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
3985 checkGLcall("readback");
3987 gl_info->gl_ops.gl.p_glDeleteTextures(1, &texture);
3988 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
3989 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
3990 checkGLcall("delete resources");
3992 /* We expect that exactly one fragment is generated. */
3993 for (y = 0; y < ARRAY_SIZE(readback); ++y)
3995 for (x = 0; x < ARRAY_SIZE(readback[0]); ++x)
3997 if (readback[y][x] == 0xff0000ff)
3998 clear++;
3999 else if (readback[y][x] == 0xffff0000)
4000 draw++;
4004 if (clear != 63 || draw != 1)
4006 FIXME("Unexpected filling convention test result.\n");
4007 return FALSE;
4010 /* One pixel was drawn, check if it is the expected one */
4011 return readback[3][3] == 0xffff0000;
4013 static float wined3d_adapter_find_polyoffset_scale(struct wined3d_caps_gl_ctx *ctx, GLenum format)
4015 const struct wined3d_gl_info *gl_info = ctx->gl_info;
4016 static const struct wined3d_color blue = {0.0f, 0.0f, 1.0f, 1.0f};
4017 GLuint fbo, color, depth;
4018 unsigned int low = 0, high = 32, cur;
4019 DWORD readback[256];
4020 static const struct wined3d_vec3 geometry[] =
4022 {-1.0f, -1.0f, -1.0f},
4023 { 1.0f, -1.0f, 0.0f},
4024 {-1.0f, 1.0f, -1.0f},
4025 { 1.0f, 1.0f, 0.0f},
4028 /* Most drivers want 2^23 for fixed point depth buffers, including r300g, r600g,
4029 * Nvidia. Use this as a fallback if the detection fails. */
4030 unsigned int fallback = 23;
4032 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
4034 FIXME("No FBOs, assuming polyoffset scale of 2^%u.\n", fallback);
4035 return (float)(1u << fallback);
4038 gl_info->gl_ops.gl.p_glGenTextures(1, &color);
4039 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
4040 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
4041 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);
4043 gl_info->fbo_ops.glGenRenderbuffers(1, &depth);
4044 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, depth);
4045 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format, 256, 1);
4047 gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
4048 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
4049 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
4050 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
4051 checkGLcall("Setup framebuffer");
4053 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
4054 gl_info->gl_ops.gl.p_glClearDepth(0.5f);
4055 gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
4056 gl_info->gl_ops.gl.p_glEnable(GL_POLYGON_OFFSET_FILL);
4057 gl_info->gl_ops.gl.p_glViewport(0, 0, 256, 1);
4058 checkGLcall("Misc parameters");
4060 for (;;)
4062 if (high - low <= 1)
4064 ERR("PolygonOffset scale factor detection failed, using fallback value 2^%u.\n", fallback);
4065 cur = fallback;
4066 break;
4068 cur = (low + high) / 2;
4070 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4071 /* The post viewport transform Z of the geometry runs from 0.0 to 0.5. We want to push it another
4072 * 0.25 so that the Z buffer content (0.5) cuts the quad off at half the screen. */
4073 gl_info->gl_ops.gl.p_glPolygonOffset(0.0f, (float)(1u << cur) * 0.25f);
4074 draw_test_quad(ctx, geometry, &blue);
4075 checkGLcall("Test draw");
4077 /* Rebinding texture to workaround a fglrx bug. */
4078 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, color);
4079 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, readback);
4080 checkGLcall("readback");
4082 TRACE("low %02u, high %02u, cur %2u, 0=0x%08x, 125=0x%08x, 131=0x%08x, 255=0x%08x\n",
4083 low, high, cur, readback[0], readback[125], readback[131], readback[255]);
4085 if ((readback[125] & 0xff) < 0xa0)
4086 high = cur;
4087 else if ((readback[131] & 0xff) > 0xa0)
4088 low = cur;
4089 else
4091 TRACE("Found scale factor 2^%u for format %x.\n", cur, format);
4092 break;
4096 gl_info->gl_ops.gl.p_glDeleteTextures(1, &color);
4097 gl_info->fbo_ops.glDeleteRenderbuffers(1, &depth);
4098 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
4099 gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
4100 checkGLcall("Delete framebuffer");
4102 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
4103 gl_info->gl_ops.gl.p_glDisable(GL_POLYGON_OFFSET_FILL);
4104 return (float)(1u << cur);
4107 static void init_format_depth_bias_scale(struct wined3d_adapter *adapter,
4108 struct wined3d_caps_gl_ctx *ctx)
4110 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
4111 unsigned int i;
4113 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
4115 struct wined3d_format_gl *format = get_format_gl_by_idx(adapter, i);
4117 if (format->f.depth_size && (format->f.flags[WINED3D_GL_RES_TYPE_RB] & WINED3DFMT_FLAG_DEPTH_STENCIL))
4119 TRACE("Testing depth bias scale for format %s.\n", debug_d3dformat(format->f.id));
4120 format->f.depth_bias_scale = wined3d_adapter_find_polyoffset_scale(ctx, format->internal);
4122 if (!(d3d_info->wined3d_creation_flags & WINED3D_NORMALIZED_DEPTH_BIAS))
4124 /* The single-precision binary floating-point format has
4125 * a significand precision of 24 bits.
4127 if (format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT)
4128 format->f.depth_bias_scale /= 1u << 24;
4129 else
4130 format->f.depth_bias_scale /= 1u << format->f.depth_size;
4136 static BOOL wined3d_adapter_init_format_info(struct wined3d_adapter *adapter, size_t format_size)
4138 unsigned int count = WINED3D_FORMAT_COUNT + ARRAY_SIZE(typeless_depth_stencil_formats);
4140 if (!(adapter->formats = heap_calloc(count, format_size)))
4142 ERR("Failed to allocate memory.\n");
4143 return FALSE;
4145 adapter->format_size = format_size;
4147 if (!init_format_base_info(adapter))
4148 goto fail;
4149 if (!init_format_block_info(adapter))
4150 goto fail;
4151 if (!init_format_decompress_info(adapter))
4152 goto fail;
4153 if (!init_srgb_formats(adapter))
4154 goto fail;
4156 return TRUE;
4158 fail:
4159 heap_free(adapter->formats);
4160 adapter->formats = NULL;
4161 return FALSE;
4164 BOOL wined3d_adapter_no3d_init_format_info(struct wined3d_adapter *adapter)
4166 struct wined3d_format *format;
4167 unsigned int i;
4169 static const enum wined3d_format_id blit_formats[] =
4171 WINED3DFMT_B8G8R8A8_UNORM,
4172 WINED3DFMT_B8G8R8X8_UNORM,
4173 WINED3DFMT_B5G6R5_UNORM,
4174 WINED3DFMT_B5G5R5X1_UNORM,
4175 WINED3DFMT_B5G5R5A1_UNORM,
4176 WINED3DFMT_B4G4R4A4_UNORM,
4177 WINED3DFMT_B2G3R3_UNORM,
4178 WINED3DFMT_A8_UNORM,
4179 WINED3DFMT_B2G3R3A8_UNORM,
4180 WINED3DFMT_B4G4R4X4_UNORM,
4181 WINED3DFMT_R10G10B10A2_UNORM,
4182 WINED3DFMT_R8G8B8A8_UNORM,
4183 WINED3DFMT_R8G8B8X8_UNORM,
4184 WINED3DFMT_R16G16_UNORM,
4185 WINED3DFMT_B10G10R10A2_UNORM,
4186 WINED3DFMT_R16G16B16A16_UNORM,
4187 WINED3DFMT_P8_UINT,
4190 if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format)))
4191 return FALSE;
4193 for (i = 0; i < ARRAY_SIZE(blit_formats); ++i)
4195 if (!(format = get_format_internal(adapter, blit_formats[i])))
4196 return FALSE;
4198 format->flags[WINED3D_GL_RES_TYPE_TEX_2D] |= WINED3DFMT_FLAG_BLIT;
4199 format->flags[WINED3D_GL_RES_TYPE_RB] |= WINED3DFMT_FLAG_BLIT;
4202 return TRUE;
4205 /* Context activation is done by the caller. */
4206 BOOL wined3d_adapter_gl_init_format_info(struct wined3d_adapter *adapter, struct wined3d_caps_gl_ctx *ctx)
4208 struct wined3d_gl_info *gl_info = &adapter->gl_info;
4210 if (!wined3d_adapter_init_format_info(adapter, sizeof(struct wined3d_format_gl)))
4211 return FALSE;
4213 if (!init_format_texture_info(adapter, gl_info)) goto fail;
4214 if (!init_format_vertex_info(adapter, gl_info)) goto fail;
4216 apply_format_fixups(adapter, gl_info);
4217 init_format_fbo_compat_info(adapter, ctx);
4218 init_format_filter_info(adapter, gl_info);
4219 init_format_gen_mipmap_info(adapter, gl_info);
4220 init_format_depth_bias_scale(adapter, ctx);
4222 if (!init_typeless_formats(adapter)) goto fail;
4224 return TRUE;
4226 fail:
4227 heap_free(adapter->formats);
4228 adapter->formats = NULL;
4229 return FALSE;
4232 static void init_vulkan_format_info(struct wined3d_format_vk *format,
4233 const struct wined3d_vk_info *vk_info, VkPhysicalDevice vk_physical_device)
4235 static const struct
4237 enum wined3d_format_id id;
4238 VkFormat vk_format;
4239 const char *fixup;
4241 vulkan_formats[] =
4243 {WINED3DFMT_R32G32B32A32_FLOAT, VK_FORMAT_R32G32B32A32_SFLOAT, },
4244 {WINED3DFMT_R32G32B32A32_UINT, VK_FORMAT_R32G32B32A32_UINT, },
4245 {WINED3DFMT_R32G32B32A32_SINT, VK_FORMAT_R32G32B32A32_SINT, },
4246 {WINED3DFMT_R32G32B32_FLOAT, VK_FORMAT_R32G32B32_SFLOAT, },
4247 {WINED3DFMT_R32G32B32_UINT, VK_FORMAT_R32G32B32_UINT, },
4248 {WINED3DFMT_R32G32B32_SINT, VK_FORMAT_R32G32B32_SINT, },
4249 {WINED3DFMT_R16G16B16A16_FLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, },
4250 {WINED3DFMT_R16G16B16A16_UNORM, VK_FORMAT_R16G16B16A16_UNORM, },
4251 {WINED3DFMT_R16G16B16A16_UINT, VK_FORMAT_R16G16B16A16_UINT, },
4252 {WINED3DFMT_R16G16B16A16_SNORM, VK_FORMAT_R16G16B16A16_SNORM, },
4253 {WINED3DFMT_R16G16B16A16_SINT, VK_FORMAT_R16G16B16A16_SINT, },
4254 {WINED3DFMT_R32G32_FLOAT, VK_FORMAT_R32G32_SFLOAT, },
4255 {WINED3DFMT_R32G32_UINT, VK_FORMAT_R32G32_UINT, },
4256 {WINED3DFMT_R32G32_SINT, VK_FORMAT_R32G32_SINT, },
4257 {WINED3DFMT_R10G10B10A2_UNORM, VK_FORMAT_A2B10G10R10_UNORM_PACK32,},
4258 {WINED3DFMT_R11G11B10_FLOAT, VK_FORMAT_B10G11R11_UFLOAT_PACK32, },
4259 {WINED3DFMT_R8G8_UNORM, VK_FORMAT_R8G8_UNORM, },
4260 {WINED3DFMT_R8G8_UINT, VK_FORMAT_R8G8_UINT, },
4261 {WINED3DFMT_R8G8_SNORM, VK_FORMAT_R8G8_SNORM, },
4262 {WINED3DFMT_R8G8_SINT, VK_FORMAT_R8G8_SINT, },
4263 {WINED3DFMT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, },
4264 {WINED3DFMT_R8G8B8A8_UNORM_SRGB, VK_FORMAT_R8G8B8A8_SRGB, },
4265 {WINED3DFMT_R8G8B8A8_UINT, VK_FORMAT_R8G8B8A8_UINT, },
4266 {WINED3DFMT_R8G8B8A8_SNORM, VK_FORMAT_R8G8B8A8_SNORM, },
4267 {WINED3DFMT_R8G8B8A8_SINT, VK_FORMAT_R8G8B8A8_SINT, },
4268 {WINED3DFMT_R16G16_FLOAT, VK_FORMAT_R16G16_SFLOAT, },
4269 {WINED3DFMT_R16G16_UNORM, VK_FORMAT_R16G16_UNORM, },
4270 {WINED3DFMT_R16G16_UINT, VK_FORMAT_R16G16_UINT, },
4271 {WINED3DFMT_R16G16_SNORM, VK_FORMAT_R16G16_SNORM, },
4272 {WINED3DFMT_R16G16_SINT, VK_FORMAT_R16G16_SINT, },
4273 {WINED3DFMT_D32_FLOAT, VK_FORMAT_D32_SFLOAT, },
4274 {WINED3DFMT_R32_FLOAT, VK_FORMAT_R32_SFLOAT, },
4275 {WINED3DFMT_R32_UINT, VK_FORMAT_R32_UINT, },
4276 {WINED3DFMT_R32_SINT, VK_FORMAT_R32_SINT, },
4277 {WINED3DFMT_R16_FLOAT, VK_FORMAT_R16_SFLOAT, },
4278 {WINED3DFMT_D16_UNORM, VK_FORMAT_D16_UNORM, },
4279 {WINED3DFMT_R16_UNORM, VK_FORMAT_R16_UNORM, },
4280 {WINED3DFMT_R16_UINT, VK_FORMAT_R16_UINT, },
4281 {WINED3DFMT_R16_SNORM, VK_FORMAT_R16_SNORM, },
4282 {WINED3DFMT_R16_SINT, VK_FORMAT_R16_SINT, },
4283 {WINED3DFMT_R8_UNORM, VK_FORMAT_R8_UNORM, },
4284 {WINED3DFMT_R8_UINT, VK_FORMAT_R8_UINT, },
4285 {WINED3DFMT_R8_SNORM, VK_FORMAT_R8_SNORM, },
4286 {WINED3DFMT_R8_SINT, VK_FORMAT_R8_SINT, },
4287 {WINED3DFMT_A8_UNORM, VK_FORMAT_R8_UNORM, "000X"},
4288 {WINED3DFMT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, },
4289 {WINED3DFMT_B8G8R8A8_UNORM_SRGB, VK_FORMAT_B8G8R8A8_SRGB, },
4290 {WINED3DFMT_B8G8R8X8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, "XYZ1"},
4291 {WINED3DFMT_B8G8R8X8_UNORM_SRGB, VK_FORMAT_B8G8R8A8_SRGB, "XYZ1"},
4292 {WINED3DFMT_BC1_UNORM, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, },
4293 {WINED3DFMT_BC1_UNORM_SRGB, VK_FORMAT_BC1_RGBA_SRGB_BLOCK, },
4294 {WINED3DFMT_BC2_UNORM, VK_FORMAT_BC2_UNORM_BLOCK, },
4295 {WINED3DFMT_BC2_UNORM_SRGB, VK_FORMAT_BC2_SRGB_BLOCK, },
4296 {WINED3DFMT_BC3_UNORM, VK_FORMAT_BC3_UNORM_BLOCK, },
4297 {WINED3DFMT_BC3_UNORM_SRGB, VK_FORMAT_BC3_SRGB_BLOCK, },
4298 {WINED3DFMT_BC4_UNORM, VK_FORMAT_BC4_UNORM_BLOCK, },
4299 {WINED3DFMT_BC4_SNORM, VK_FORMAT_BC4_SNORM_BLOCK, },
4300 {WINED3DFMT_BC5_UNORM, VK_FORMAT_BC5_UNORM_BLOCK, },
4301 {WINED3DFMT_BC5_SNORM, VK_FORMAT_BC5_SNORM_BLOCK, },
4302 {WINED3DFMT_BC6H_UF16, VK_FORMAT_BC6H_UFLOAT_BLOCK, },
4303 {WINED3DFMT_BC6H_SF16, VK_FORMAT_BC6H_SFLOAT_BLOCK, },
4304 {WINED3DFMT_BC7_UNORM, VK_FORMAT_BC7_UNORM_BLOCK, },
4305 {WINED3DFMT_BC7_UNORM_SRGB, VK_FORMAT_BC7_SRGB_BLOCK, },
4306 {WINED3DFMT_R9G9B9E5_SHAREDEXP, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, },
4307 {WINED3DFMT_D32_FLOAT_S8X24_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4308 {WINED3DFMT_R32_FLOAT_X8X24_TYPELESS, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4309 {WINED3DFMT_X32_TYPELESS_G8X24_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT, },
4310 {WINED3DFMT_D24_UNORM_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT, },
4312 VkFormat vk_format = VK_FORMAT_UNDEFINED;
4313 VkImageFormatProperties image_properties;
4314 VkFormatFeatureFlags texture_flags;
4315 VkFormatProperties properties;
4316 VkImageUsageFlags vk_usage;
4317 unsigned int flags;
4318 const char *fixup;
4319 unsigned int i;
4320 uint32_t mask;
4321 VkResult vr;
4323 for (i = 0; i < ARRAY_SIZE(vulkan_formats); ++i)
4325 if (vulkan_formats[i].id == format->f.id)
4327 vk_format = vulkan_formats[i].vk_format;
4328 fixup = vulkan_formats[i].fixup;
4329 break;
4332 if (!vk_format)
4334 WARN("Unsupported format %s.\n", debug_d3dformat(format->f.id));
4335 return;
4338 format->vk_format = vk_format;
4339 if (fixup)
4340 format->f.color_fixup = create_color_fixup_desc_from_string(fixup);
4341 else
4342 format->f.color_fixup = COLOR_FIXUP_IDENTITY;
4344 VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
4345 if (vk_format == VK_FORMAT_D24_UNORM_S8_UINT)
4347 if (~properties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4349 /* AMD doesn't support VK_FORMAT_D24_UNORM_S8_UINT. */
4350 WARN("Mapping VK_FORMAT_D24_UNORM_S8_UINT to VK_FORMAT_D32_SFLOAT_S8_UINT.\n");
4352 format->vk_format = vk_format = VK_FORMAT_D32_SFLOAT_S8_UINT;
4353 VK_CALL(vkGetPhysicalDeviceFormatProperties(vk_physical_device, vk_format, &properties));
4357 if (properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)
4358 format->f.flags[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3DFMT_FLAG_VERTEX_ATTRIBUTE;
4359 if (properties.bufferFeatures & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)
4360 format->f.flags[WINED3D_GL_RES_TYPE_BUFFER] |= WINED3DFMT_FLAG_TEXTURE;
4362 flags = 0;
4363 texture_flags = properties.linearTilingFeatures | properties.optimalTilingFeatures;
4364 if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
4366 flags |= WINED3DFMT_FLAG_TEXTURE | WINED3DFMT_FLAG_VTF;
4368 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
4370 flags |= WINED3DFMT_FLAG_RENDERTARGET;
4372 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)
4374 flags |= WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING;
4376 if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4378 flags |= WINED3DFMT_FLAG_DEPTH_STENCIL;
4380 if (texture_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)
4382 flags |= WINED3DFMT_FLAG_FILTERING;
4384 if (texture_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)
4386 flags |= WINED3DFMT_FLAG_UNORDERED_ACCESS;
4389 if (!(~flags & (WINED3DFMT_FLAG_RENDERTARGET | WINED3DFMT_FLAG_FILTERING)))
4390 flags |= WINED3DFMT_FLAG_GEN_MIPMAP;
4392 format->f.flags[WINED3D_GL_RES_TYPE_TEX_1D] |= flags;
4393 format->f.flags[WINED3D_GL_RES_TYPE_TEX_2D] |= flags;
4394 format->f.flags[WINED3D_GL_RES_TYPE_TEX_3D] |= flags;
4395 format->f.flags[WINED3D_GL_RES_TYPE_TEX_CUBE] |= flags;
4397 vk_usage = 0;
4398 if (texture_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
4399 vk_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4400 else if (texture_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
4401 vk_usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4402 if (vk_usage)
4404 if ((vr = VK_CALL(vkGetPhysicalDeviceImageFormatProperties(vk_physical_device, vk_format,
4405 VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, vk_usage, 0, &image_properties))) < 0)
4407 ERR("Failed to get image format properties, vr %s.\n", wined3d_debug_vkresult(vr));
4408 return;
4411 mask = image_properties.sampleCounts & 0x3f;
4412 while (mask)
4414 i = (1u << wined3d_bit_scan(&mask)) - 1;
4415 format->f.multisample_types |= 1u << i;
4420 BOOL wined3d_adapter_vk_init_format_info(struct wined3d_adapter_vk *adapter_vk,
4421 const struct wined3d_vk_info *vk_info)
4423 VkPhysicalDevice vk_physical_device = adapter_vk->physical_device;
4424 struct wined3d_adapter *adapter = &adapter_vk->a;
4425 struct wined3d_format_vk *format;
4426 unsigned int i;
4428 if (!wined3d_adapter_init_format_info(adapter, sizeof(*format)))
4429 return FALSE;
4431 for (i = 0; i < WINED3D_FORMAT_COUNT; ++i)
4433 format = wined3d_format_vk_mutable(get_format_by_idx(adapter, i));
4435 if (format->f.id)
4436 init_vulkan_format_info(format, vk_info, vk_physical_device);
4439 if (!init_typeless_formats(adapter)) goto fail;
4441 return TRUE;
4443 fail:
4444 heap_free(adapter->formats);
4445 adapter->formats = NULL;
4446 return FALSE;
4449 const struct wined3d_format *wined3d_get_format(const struct wined3d_adapter *adapter,
4450 enum wined3d_format_id format_id, unsigned int bind_flags)
4452 const struct wined3d_format *format;
4453 int idx = get_format_idx(format_id);
4454 unsigned int i;
4456 if (idx == -1)
4458 FIXME("Can't find format %s (%#x) in the format lookup table.\n",
4459 debug_d3dformat(format_id), format_id);
4460 return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4463 format = get_format_by_idx(adapter, idx);
4465 if (bind_flags & WINED3D_BIND_DEPTH_STENCIL && wined3d_format_is_typeless(format))
4467 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
4469 if (typeless_depth_stencil_formats[i].typeless_id == format_id)
4470 return get_format_by_idx(adapter, WINED3D_FORMAT_COUNT + i);
4473 FIXME("Cannot find depth/stencil typeless format %s (%#x).\n",
4474 debug_d3dformat(format_id), format_id);
4475 return get_format_internal(adapter, WINED3DFMT_UNKNOWN);
4478 return format;
4481 enum wined3d_format_id wined3d_get_typed_format_id(const struct wined3d_adapter *adapter,
4482 const struct wined3d_format *format, enum wined3d_channel_type channel_type)
4484 const struct wined3d_typed_format_info *info;
4485 uint32_t i;
4487 for (i = 0; i < ARRAY_SIZE(typed_formats); ++i)
4489 info = &typed_formats[i];
4490 if (info->typeless_id == format->typeless_id && map_channel_type(info->channels[0]) == channel_type)
4491 return info->id;
4494 return WINED3DFMT_UNKNOWN;
4497 BOOL wined3d_format_is_depth_view(enum wined3d_format_id resource_format_id,
4498 enum wined3d_format_id view_format_id)
4500 unsigned int i;
4502 for (i = 0; i < ARRAY_SIZE(typeless_depth_stencil_formats); ++i)
4504 if (typeless_depth_stencil_formats[i].typeless_id == resource_format_id)
4505 return typeless_depth_stencil_formats[i].depth_view_id == view_format_id;
4507 return FALSE;
4510 void wined3d_format_calculate_pitch(const struct wined3d_format *format, unsigned int alignment,
4511 unsigned int width, unsigned int height, unsigned int *row_pitch, unsigned int *slice_pitch)
4513 /* For block based formats, pitch means the amount of bytes to the next
4514 * row of blocks rather than the next row of pixels. */
4515 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BLOCKS)
4517 unsigned int row_block_count = (width + format->block_width - 1) / format->block_width;
4518 unsigned int slice_block_count = (height + format->block_height - 1) / format->block_height;
4519 *row_pitch = row_block_count * format->block_byte_count;
4520 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
4521 *slice_pitch = *row_pitch * slice_block_count;
4523 else
4525 *row_pitch = format->byte_count * width; /* Bytes / row */
4526 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
4527 *slice_pitch = *row_pitch * height;
4530 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
4532 /* The D3D format requirements make sure that the resulting format is an integer again */
4533 *slice_pitch *= format->height_scale.numerator;
4534 *slice_pitch /= format->height_scale.denominator;
4537 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
4540 UINT wined3d_format_calculate_size(const struct wined3d_format *format, UINT alignment,
4541 UINT width, UINT height, UINT depth)
4543 unsigned int row_pitch, slice_pitch;
4545 if (format->id == WINED3DFMT_UNKNOWN)
4546 return 0;
4548 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BROKEN_PITCH)
4549 return width * height * depth * format->byte_count;
4551 wined3d_format_calculate_pitch(format, alignment, width, height, &row_pitch, &slice_pitch);
4553 return slice_pitch * depth;
4556 BOOL wined3d_formats_are_srgb_variants(enum wined3d_format_id format1, enum wined3d_format_id format2)
4558 unsigned int i;
4560 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
4562 if (format1 == format_srgb_info[i].srgb_format_id)
4563 return format2 == format_srgb_info[i].base_format_id;
4564 if (format1 == format_srgb_info[i].base_format_id)
4565 return format2 == format_srgb_info[i].srgb_format_id;
4567 return FALSE;
4570 /*****************************************************************************
4571 * Trace formatting of useful values
4573 const char *debug_box(const struct wined3d_box *box)
4575 if (!box)
4576 return "(null)";
4577 return wine_dbg_sprintf("(%u, %u, %u)-(%u, %u, %u)",
4578 box->left, box->top, box->front,
4579 box->right, box->bottom, box->back);
4582 const char *debug_color(const struct wined3d_color *color)
4584 if (!color)
4585 return "(null)";
4586 return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
4587 color->r, color->g, color->b, color->a);
4590 const char *debug_ivec4(const struct wined3d_ivec4 *v)
4592 if (!v)
4593 return "(null)";
4594 return wine_dbg_sprintf("{%d, %d, %d, %d}",
4595 v->x, v->y, v->z, v->w);
4598 const char *debug_uvec4(const struct wined3d_uvec4 *v)
4600 if (!v)
4601 return "(null)";
4602 return wine_dbg_sprintf("{%u, %u, %u, %u}",
4603 v->x, v->y, v->z, v->w);
4606 const char *debug_vec4(const struct wined3d_vec4 *v)
4608 if (!v)
4609 return "(null)";
4610 return wine_dbg_sprintf("{%.8e, %.8e, %.8e, %.8e}",
4611 v->x, v->y, v->z, v->w);
4614 const char *debug_const_bo_address(const struct wined3d_const_bo_address *address)
4616 if (!address)
4617 return "(null)";
4618 return wine_dbg_sprintf("{%p:%p}", address->buffer_object, address->addr);
4621 const char *debug_bo_address(const struct wined3d_bo_address *address)
4623 return debug_const_bo_address((const struct wined3d_const_bo_address *)address);
4626 const char *debug_d3dformat(enum wined3d_format_id format_id)
4628 switch (format_id)
4630 #define FMT_TO_STR(format_id) case format_id: return #format_id
4631 FMT_TO_STR(WINED3DFMT_UNKNOWN);
4632 FMT_TO_STR(WINED3DFMT_B8G8R8_UNORM);
4633 FMT_TO_STR(WINED3DFMT_B5G5R5X1_UNORM);
4634 FMT_TO_STR(WINED3DFMT_B4G4R4A4_UNORM);
4635 FMT_TO_STR(WINED3DFMT_B2G3R3_UNORM);
4636 FMT_TO_STR(WINED3DFMT_B2G3R3A8_UNORM);
4637 FMT_TO_STR(WINED3DFMT_B4G4R4X4_UNORM);
4638 FMT_TO_STR(WINED3DFMT_R8G8B8X8_UNORM);
4639 FMT_TO_STR(WINED3DFMT_B10G10R10A2_UNORM);
4640 FMT_TO_STR(WINED3DFMT_P8_UINT_A8_UNORM);
4641 FMT_TO_STR(WINED3DFMT_P8_UINT);
4642 FMT_TO_STR(WINED3DFMT_L8_UNORM);
4643 FMT_TO_STR(WINED3DFMT_L8A8_UNORM);
4644 FMT_TO_STR(WINED3DFMT_L4A4_UNORM);
4645 FMT_TO_STR(WINED3DFMT_R5G5_SNORM_L6_UNORM);
4646 FMT_TO_STR(WINED3DFMT_R8G8_SNORM_L8X8_UNORM);
4647 FMT_TO_STR(WINED3DFMT_R10G11B11_SNORM);
4648 FMT_TO_STR(WINED3DFMT_R10G10B10X2_TYPELESS);
4649 FMT_TO_STR(WINED3DFMT_R10G10B10X2_UINT);
4650 FMT_TO_STR(WINED3DFMT_R10G10B10X2_SNORM);
4651 FMT_TO_STR(WINED3DFMT_R10G10B10_SNORM_A2_UNORM);
4652 FMT_TO_STR(WINED3DFMT_D16_LOCKABLE);
4653 FMT_TO_STR(WINED3DFMT_D32_UNORM);
4654 FMT_TO_STR(WINED3DFMT_S1_UINT_D15_UNORM);
4655 FMT_TO_STR(WINED3DFMT_X8D24_UNORM);
4656 FMT_TO_STR(WINED3DFMT_S4X4_UINT_D24_UNORM);
4657 FMT_TO_STR(WINED3DFMT_L16_UNORM);
4658 FMT_TO_STR(WINED3DFMT_S8_UINT_D24_FLOAT);
4659 FMT_TO_STR(WINED3DFMT_R8G8_SNORM_Cx);
4660 FMT_TO_STR(WINED3DFMT_R32G32B32A32_TYPELESS);
4661 FMT_TO_STR(WINED3DFMT_R32G32B32A32_FLOAT);
4662 FMT_TO_STR(WINED3DFMT_R32G32B32A32_UINT);
4663 FMT_TO_STR(WINED3DFMT_R32G32B32A32_SINT);
4664 FMT_TO_STR(WINED3DFMT_R32G32B32_TYPELESS);
4665 FMT_TO_STR(WINED3DFMT_R32G32B32_FLOAT);
4666 FMT_TO_STR(WINED3DFMT_R32G32B32_UINT);
4667 FMT_TO_STR(WINED3DFMT_R32G32B32_SINT);
4668 FMT_TO_STR(WINED3DFMT_R16G16B16A16_TYPELESS);
4669 FMT_TO_STR(WINED3DFMT_R16G16B16A16_FLOAT);
4670 FMT_TO_STR(WINED3DFMT_R16G16B16A16_UNORM);
4671 FMT_TO_STR(WINED3DFMT_R16G16B16A16_UINT);
4672 FMT_TO_STR(WINED3DFMT_R16G16B16A16_SNORM);
4673 FMT_TO_STR(WINED3DFMT_R16G16B16A16_SINT);
4674 FMT_TO_STR(WINED3DFMT_R32G32_TYPELESS);
4675 FMT_TO_STR(WINED3DFMT_R32G32_FLOAT);
4676 FMT_TO_STR(WINED3DFMT_R32G32_UINT);
4677 FMT_TO_STR(WINED3DFMT_R32G32_SINT);
4678 FMT_TO_STR(WINED3DFMT_R32G8X24_TYPELESS);
4679 FMT_TO_STR(WINED3DFMT_D32_FLOAT_S8X24_UINT);
4680 FMT_TO_STR(WINED3DFMT_R32_FLOAT_X8X24_TYPELESS);
4681 FMT_TO_STR(WINED3DFMT_X32_TYPELESS_G8X24_UINT);
4682 FMT_TO_STR(WINED3DFMT_R10G10B10A2_TYPELESS);
4683 FMT_TO_STR(WINED3DFMT_R10G10B10A2_UNORM);
4684 FMT_TO_STR(WINED3DFMT_R10G10B10A2_UINT);
4685 FMT_TO_STR(WINED3DFMT_R10G10B10A2_SNORM);
4686 FMT_TO_STR(WINED3DFMT_R10G10B10_XR_BIAS_A2_UNORM);
4687 FMT_TO_STR(WINED3DFMT_R11G11B10_FLOAT);
4688 FMT_TO_STR(WINED3DFMT_R8G8B8A8_TYPELESS);
4689 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM);
4690 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UNORM_SRGB);
4691 FMT_TO_STR(WINED3DFMT_R8G8B8A8_UINT);
4692 FMT_TO_STR(WINED3DFMT_R8G8B8A8_SNORM);
4693 FMT_TO_STR(WINED3DFMT_R8G8B8A8_SINT);
4694 FMT_TO_STR(WINED3DFMT_R16G16_TYPELESS);
4695 FMT_TO_STR(WINED3DFMT_R16G16_FLOAT);
4696 FMT_TO_STR(WINED3DFMT_R16G16_UNORM);
4697 FMT_TO_STR(WINED3DFMT_R16G16_UINT);
4698 FMT_TO_STR(WINED3DFMT_R16G16_SNORM);
4699 FMT_TO_STR(WINED3DFMT_R16G16_SINT);
4700 FMT_TO_STR(WINED3DFMT_R32_TYPELESS);
4701 FMT_TO_STR(WINED3DFMT_D32_FLOAT);
4702 FMT_TO_STR(WINED3DFMT_R32_FLOAT);
4703 FMT_TO_STR(WINED3DFMT_R32_UINT);
4704 FMT_TO_STR(WINED3DFMT_R32_SINT);
4705 FMT_TO_STR(WINED3DFMT_R24G8_TYPELESS);
4706 FMT_TO_STR(WINED3DFMT_D24_UNORM_S8_UINT);
4707 FMT_TO_STR(WINED3DFMT_R24_UNORM_X8_TYPELESS);
4708 FMT_TO_STR(WINED3DFMT_X24_TYPELESS_G8_UINT);
4709 FMT_TO_STR(WINED3DFMT_R8G8_TYPELESS);
4710 FMT_TO_STR(WINED3DFMT_R8G8_UNORM);
4711 FMT_TO_STR(WINED3DFMT_R8G8_UINT);
4712 FMT_TO_STR(WINED3DFMT_R8G8_SNORM);
4713 FMT_TO_STR(WINED3DFMT_R8G8_SINT);
4714 FMT_TO_STR(WINED3DFMT_R16_TYPELESS);
4715 FMT_TO_STR(WINED3DFMT_R16_FLOAT);
4716 FMT_TO_STR(WINED3DFMT_D16_UNORM);
4717 FMT_TO_STR(WINED3DFMT_R16_UNORM);
4718 FMT_TO_STR(WINED3DFMT_R16_UINT);
4719 FMT_TO_STR(WINED3DFMT_R16_SNORM);
4720 FMT_TO_STR(WINED3DFMT_R16_SINT);
4721 FMT_TO_STR(WINED3DFMT_R8_TYPELESS);
4722 FMT_TO_STR(WINED3DFMT_R8_UNORM);
4723 FMT_TO_STR(WINED3DFMT_R8_UINT);
4724 FMT_TO_STR(WINED3DFMT_R8_SNORM);
4725 FMT_TO_STR(WINED3DFMT_R8_SINT);
4726 FMT_TO_STR(WINED3DFMT_A8_UNORM);
4727 FMT_TO_STR(WINED3DFMT_R1_UNORM);
4728 FMT_TO_STR(WINED3DFMT_R9G9B9E5_SHAREDEXP);
4729 FMT_TO_STR(WINED3DFMT_R8G8_B8G8_UNORM);
4730 FMT_TO_STR(WINED3DFMT_G8R8_G8B8_UNORM);
4731 FMT_TO_STR(WINED3DFMT_BC1_TYPELESS);
4732 FMT_TO_STR(WINED3DFMT_BC1_UNORM);
4733 FMT_TO_STR(WINED3DFMT_BC1_UNORM_SRGB);
4734 FMT_TO_STR(WINED3DFMT_BC2_TYPELESS);
4735 FMT_TO_STR(WINED3DFMT_BC2_UNORM);
4736 FMT_TO_STR(WINED3DFMT_BC2_UNORM_SRGB);
4737 FMT_TO_STR(WINED3DFMT_BC3_TYPELESS);
4738 FMT_TO_STR(WINED3DFMT_BC3_UNORM);
4739 FMT_TO_STR(WINED3DFMT_BC3_UNORM_SRGB);
4740 FMT_TO_STR(WINED3DFMT_BC4_TYPELESS);
4741 FMT_TO_STR(WINED3DFMT_BC4_UNORM);
4742 FMT_TO_STR(WINED3DFMT_BC4_SNORM);
4743 FMT_TO_STR(WINED3DFMT_BC5_TYPELESS);
4744 FMT_TO_STR(WINED3DFMT_BC5_UNORM);
4745 FMT_TO_STR(WINED3DFMT_BC5_SNORM);
4746 FMT_TO_STR(WINED3DFMT_B5G6R5_UNORM);
4747 FMT_TO_STR(WINED3DFMT_B5G5R5A1_UNORM);
4748 FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM);
4749 FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM);
4750 FMT_TO_STR(WINED3DFMT_B8G8R8A8_TYPELESS);
4751 FMT_TO_STR(WINED3DFMT_B8G8R8A8_UNORM_SRGB);
4752 FMT_TO_STR(WINED3DFMT_B8G8R8X8_TYPELESS);
4753 FMT_TO_STR(WINED3DFMT_B8G8R8X8_UNORM_SRGB);
4754 FMT_TO_STR(WINED3DFMT_BC6H_TYPELESS);
4755 FMT_TO_STR(WINED3DFMT_BC6H_UF16);
4756 FMT_TO_STR(WINED3DFMT_BC6H_SF16);
4757 FMT_TO_STR(WINED3DFMT_BC7_TYPELESS);
4758 FMT_TO_STR(WINED3DFMT_BC7_UNORM);
4759 FMT_TO_STR(WINED3DFMT_BC7_UNORM_SRGB);
4760 FMT_TO_STR(WINED3DFMT_UYVY);
4761 FMT_TO_STR(WINED3DFMT_YUY2);
4762 FMT_TO_STR(WINED3DFMT_YV12);
4763 FMT_TO_STR(WINED3DFMT_DXT1);
4764 FMT_TO_STR(WINED3DFMT_DXT2);
4765 FMT_TO_STR(WINED3DFMT_DXT3);
4766 FMT_TO_STR(WINED3DFMT_DXT4);
4767 FMT_TO_STR(WINED3DFMT_DXT5);
4768 FMT_TO_STR(WINED3DFMT_MULTI2_ARGB8);
4769 FMT_TO_STR(WINED3DFMT_G8R8_G8B8);
4770 FMT_TO_STR(WINED3DFMT_R8G8_B8G8);
4771 FMT_TO_STR(WINED3DFMT_ATI1N);
4772 FMT_TO_STR(WINED3DFMT_ATI2N);
4773 FMT_TO_STR(WINED3DFMT_INST);
4774 FMT_TO_STR(WINED3DFMT_NVDB);
4775 FMT_TO_STR(WINED3DFMT_NVHU);
4776 FMT_TO_STR(WINED3DFMT_NVHS);
4777 FMT_TO_STR(WINED3DFMT_INTZ);
4778 FMT_TO_STR(WINED3DFMT_RESZ);
4779 FMT_TO_STR(WINED3DFMT_NULL);
4780 FMT_TO_STR(WINED3DFMT_R16);
4781 FMT_TO_STR(WINED3DFMT_AL16);
4782 FMT_TO_STR(WINED3DFMT_NV12);
4783 FMT_TO_STR(WINED3DFMT_ATOC);
4784 #undef FMT_TO_STR
4785 default:
4787 char fourcc[5];
4788 fourcc[0] = (char)(format_id);
4789 fourcc[1] = (char)(format_id >> 8);
4790 fourcc[2] = (char)(format_id >> 16);
4791 fourcc[3] = (char)(format_id >> 24);
4792 fourcc[4] = 0;
4793 if (isprint(fourcc[0]) && isprint(fourcc[1]) && isprint(fourcc[2]) && isprint(fourcc[3]))
4794 FIXME("Unrecognized %#x (as fourcc: %s) WINED3DFORMAT!\n", format_id, fourcc);
4795 else
4796 FIXME("Unrecognized %#x WINED3DFORMAT!\n", format_id);
4798 return "unrecognized";
4802 const char *debug_d3ddevicetype(enum wined3d_device_type device_type)
4804 switch (device_type)
4806 #define DEVTYPE_TO_STR(dev) case dev: return #dev
4807 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_HAL);
4808 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_REF);
4809 DEVTYPE_TO_STR(WINED3D_DEVICE_TYPE_SW);
4810 #undef DEVTYPE_TO_STR
4811 default:
4812 FIXME("Unrecognized device type %#x.\n", device_type);
4813 return "unrecognized";
4817 struct debug_buffer
4819 char str[200]; /* wine_dbg_sprintf() limits string size to 200 */
4820 char *ptr;
4821 int size;
4824 static void init_debug_buffer(struct debug_buffer *buffer, const char *default_string)
4826 snprintf(buffer->str, sizeof(buffer->str), "%s", default_string);
4827 buffer->ptr = buffer->str;
4828 buffer->size = sizeof(buffer->str);
4831 static void debug_append(struct debug_buffer *buffer, const char *str, const char *separator)
4833 int size;
4835 if (!separator || buffer->ptr == buffer->str)
4836 separator = "";
4837 size = snprintf(buffer->ptr, buffer->size, "%s%s", separator, str);
4838 if (size == -1 || size >= buffer->size)
4840 buffer->size = 0;
4841 strcpy(&buffer->str[sizeof(buffer->str) - 4], "...");
4842 return;
4845 buffer->ptr += size;
4846 buffer->size -= size;
4849 const char *wined3d_debug_resource_access(DWORD access)
4851 struct debug_buffer buffer;
4853 init_debug_buffer(&buffer, "0");
4854 #define ACCESS_TO_STR(x) if (access & x) { debug_append(&buffer, #x, " | "); access &= ~x; }
4855 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_GPU);
4856 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_CPU);
4857 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_R);
4858 ACCESS_TO_STR(WINED3D_RESOURCE_ACCESS_MAP_W);
4859 #undef ACCESS_TO_STR
4860 if (access)
4861 FIXME("Unrecognised access flag(s) %#x.\n", access);
4863 return wine_dbg_sprintf("%s", buffer.str);
4866 const char *wined3d_debug_bind_flags(DWORD bind_flags)
4868 struct debug_buffer buffer;
4870 init_debug_buffer(&buffer, "0");
4871 #define BIND_FLAG_TO_STR(x) if (bind_flags & x) { debug_append(&buffer, #x, " | "); bind_flags &= ~x; }
4872 BIND_FLAG_TO_STR(WINED3D_BIND_VERTEX_BUFFER);
4873 BIND_FLAG_TO_STR(WINED3D_BIND_INDEX_BUFFER);
4874 BIND_FLAG_TO_STR(WINED3D_BIND_CONSTANT_BUFFER);
4875 BIND_FLAG_TO_STR(WINED3D_BIND_SHADER_RESOURCE);
4876 BIND_FLAG_TO_STR(WINED3D_BIND_STREAM_OUTPUT);
4877 BIND_FLAG_TO_STR(WINED3D_BIND_RENDER_TARGET);
4878 BIND_FLAG_TO_STR(WINED3D_BIND_DEPTH_STENCIL);
4879 BIND_FLAG_TO_STR(WINED3D_BIND_UNORDERED_ACCESS);
4880 BIND_FLAG_TO_STR(WINED3D_BIND_INDIRECT_BUFFER);
4881 #undef BIND_FLAG_TO_STR
4882 if (bind_flags)
4883 FIXME("Unrecognised bind flag(s) %#x.\n", bind_flags);
4885 return wine_dbg_sprintf("%s", buffer.str);
4888 const char *wined3d_debug_view_desc(const struct wined3d_view_desc *d, const struct wined3d_resource *resource)
4890 struct debug_buffer buffer;
4891 unsigned int flags = d->flags;
4893 init_debug_buffer(&buffer, "0");
4894 #define VIEW_FLAG_TO_STR(x) if (flags & x) { debug_append(&buffer, #x, " | "); flags &= ~x; }
4895 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_RAW);
4896 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_APPEND);
4897 VIEW_FLAG_TO_STR(WINED3D_VIEW_BUFFER_COUNTER);
4898 VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_CUBE);
4899 VIEW_FLAG_TO_STR(WINED3D_VIEW_TEXTURE_ARRAY);
4900 VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_DEPTH);
4901 VIEW_FLAG_TO_STR(WINED3D_VIEW_READ_ONLY_STENCIL);
4902 #undef VIEW_FLAG_TO_STR
4903 if (flags)
4904 FIXME("Unrecognised view flag(s) %#x.\n", flags);
4906 if (resource->type == WINED3D_RTYPE_BUFFER)
4907 return wine_dbg_sprintf("format %s, flags %s, start_idx %u, count %u",
4908 debug_d3dformat(d->format_id), buffer.str, d->u.buffer.start_idx, d->u.buffer.count);
4909 else
4910 return wine_dbg_sprintf("format %s, flags %s, level_idx %u, level_count %u, layer_idx %u, layer_count %u",
4911 debug_d3dformat(d->format_id), buffer.str, d->u.texture.level_idx, d->u.texture.level_count,
4912 d->u.texture.layer_idx, d->u.texture.layer_count);
4915 const char *debug_d3dusage(DWORD usage)
4917 struct debug_buffer buffer;
4919 init_debug_buffer(&buffer, "0");
4920 #define WINED3DUSAGE_TO_STR(x) if (usage & x) { debug_append(&buffer, #x, " | "); usage &= ~x; }
4921 WINED3DUSAGE_TO_STR(WINED3DUSAGE_SOFTWAREPROCESSING);
4922 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DONOTCLIP);
4923 WINED3DUSAGE_TO_STR(WINED3DUSAGE_POINTS);
4924 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RTPATCHES);
4925 WINED3DUSAGE_TO_STR(WINED3DUSAGE_NPATCHES);
4926 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DYNAMIC);
4927 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICTED_CONTENT);
4928 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE_DRIVER);
4929 WINED3DUSAGE_TO_STR(WINED3DUSAGE_RESTRICT_SHARED_RESOURCE);
4930 WINED3DUSAGE_TO_STR(WINED3DUSAGE_DMAP);
4931 WINED3DUSAGE_TO_STR(WINED3DUSAGE_TEXTAPI);
4932 WINED3DUSAGE_TO_STR(WINED3DUSAGE_LEGACY_CUBEMAP);
4933 WINED3DUSAGE_TO_STR(WINED3DUSAGE_OWNDC);
4934 WINED3DUSAGE_TO_STR(WINED3DUSAGE_STATICDECL);
4935 WINED3DUSAGE_TO_STR(WINED3DUSAGE_OVERLAY);
4936 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_FILTER);
4937 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_GENMIPMAP);
4938 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_LEGACYBUMPMAP);
4939 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING);
4940 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBREAD);
4941 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_SRGBWRITE);
4942 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_VERTEXTEXTURE);
4943 WINED3DUSAGE_TO_STR(WINED3DUSAGE_QUERY_WRAPANDMIP);
4944 #undef WINED3DUSAGE_TO_STR
4945 if (usage)
4946 FIXME("Unrecognized usage flag(s) %#x.\n", usage);
4948 return wine_dbg_sprintf("%s", buffer.str);
4951 const char *debug_d3ddeclmethod(enum wined3d_decl_method method)
4953 switch (method)
4955 #define WINED3DDECLMETHOD_TO_STR(u) case u: return #u
4956 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_DEFAULT);
4957 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_U);
4958 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_PARTIAL_V);
4959 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_CROSS_UV);
4960 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_UV);
4961 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP);
4962 WINED3DDECLMETHOD_TO_STR(WINED3D_DECL_METHOD_LOOKUP_PRESAMPLED);
4963 #undef WINED3DDECLMETHOD_TO_STR
4964 default:
4965 FIXME("Unrecognized declaration method %#x.\n", method);
4966 return "unrecognized";
4970 const char *debug_d3ddeclusage(enum wined3d_decl_usage usage)
4972 switch (usage)
4974 #define WINED3DDECLUSAGE_TO_STR(u) case u: return #u
4975 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITION);
4976 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_WEIGHT);
4977 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BLEND_INDICES);
4978 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_NORMAL);
4979 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_PSIZE);
4980 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TEXCOORD);
4981 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TANGENT);
4982 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_BINORMAL);
4983 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_TESS_FACTOR);
4984 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_POSITIONT);
4985 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_COLOR);
4986 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_FOG);
4987 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_DEPTH);
4988 WINED3DDECLUSAGE_TO_STR(WINED3D_DECL_USAGE_SAMPLE);
4989 #undef WINED3DDECLUSAGE_TO_STR
4990 default:
4991 FIXME("Unrecognized %u declaration usage!\n", usage);
4992 return "unrecognized";
4996 const char *debug_d3dinput_classification(enum wined3d_input_classification classification)
4998 switch (classification)
5000 #define WINED3D_TO_STR(x) case x: return #x
5001 WINED3D_TO_STR(WINED3D_INPUT_PER_VERTEX_DATA);
5002 WINED3D_TO_STR(WINED3D_INPUT_PER_INSTANCE_DATA);
5003 #undef WINED3D_TO_STR
5004 default:
5005 FIXME("Unrecognized input classification %#x.\n", classification);
5006 return "unrecognized";
5010 const char *debug_d3dresourcetype(enum wined3d_resource_type resource_type)
5012 switch (resource_type)
5014 #define WINED3D_TO_STR(x) case x: return #x
5015 WINED3D_TO_STR(WINED3D_RTYPE_NONE);
5016 WINED3D_TO_STR(WINED3D_RTYPE_BUFFER);
5017 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_1D);
5018 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_2D);
5019 WINED3D_TO_STR(WINED3D_RTYPE_TEXTURE_3D);
5020 #undef WINED3D_TO_STR
5021 default:
5022 FIXME("Unrecognized resource type %#x.\n", resource_type);
5023 return "unrecognized";
5027 const char *debug_d3dprimitivetype(enum wined3d_primitive_type primitive_type)
5029 switch (primitive_type)
5031 #define PRIM_TO_STR(prim) case prim: return #prim
5032 PRIM_TO_STR(WINED3D_PT_UNDEFINED);
5033 PRIM_TO_STR(WINED3D_PT_POINTLIST);
5034 PRIM_TO_STR(WINED3D_PT_LINELIST);
5035 PRIM_TO_STR(WINED3D_PT_LINESTRIP);
5036 PRIM_TO_STR(WINED3D_PT_TRIANGLELIST);
5037 PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP);
5038 PRIM_TO_STR(WINED3D_PT_TRIANGLEFAN);
5039 PRIM_TO_STR(WINED3D_PT_LINELIST_ADJ);
5040 PRIM_TO_STR(WINED3D_PT_LINESTRIP_ADJ);
5041 PRIM_TO_STR(WINED3D_PT_TRIANGLELIST_ADJ);
5042 PRIM_TO_STR(WINED3D_PT_TRIANGLESTRIP_ADJ);
5043 PRIM_TO_STR(WINED3D_PT_PATCH);
5044 #undef PRIM_TO_STR
5045 default:
5046 FIXME("Unrecognized primitive type %#x.\n", primitive_type);
5047 return "unrecognized";
5051 const char *debug_d3drenderstate(enum wined3d_render_state state)
5053 switch (state)
5055 #define D3DSTATE_TO_STR(u) case u: return #u
5056 D3DSTATE_TO_STR(WINED3D_RS_ANTIALIAS);
5057 D3DSTATE_TO_STR(WINED3D_RS_TEXTUREPERSPECTIVE);
5058 D3DSTATE_TO_STR(WINED3D_RS_WRAPU);
5059 D3DSTATE_TO_STR(WINED3D_RS_WRAPV);
5060 D3DSTATE_TO_STR(WINED3D_RS_ZENABLE);
5061 D3DSTATE_TO_STR(WINED3D_RS_FILLMODE);
5062 D3DSTATE_TO_STR(WINED3D_RS_SHADEMODE);
5063 D3DSTATE_TO_STR(WINED3D_RS_LINEPATTERN);
5064 D3DSTATE_TO_STR(WINED3D_RS_MONOENABLE);
5065 D3DSTATE_TO_STR(WINED3D_RS_ROP2);
5066 D3DSTATE_TO_STR(WINED3D_RS_PLANEMASK);
5067 D3DSTATE_TO_STR(WINED3D_RS_ZWRITEENABLE);
5068 D3DSTATE_TO_STR(WINED3D_RS_ALPHATESTENABLE);
5069 D3DSTATE_TO_STR(WINED3D_RS_LASTPIXEL);
5070 D3DSTATE_TO_STR(WINED3D_RS_SRCBLEND);
5071 D3DSTATE_TO_STR(WINED3D_RS_DESTBLEND);
5072 D3DSTATE_TO_STR(WINED3D_RS_CULLMODE);
5073 D3DSTATE_TO_STR(WINED3D_RS_ZFUNC);
5074 D3DSTATE_TO_STR(WINED3D_RS_ALPHAREF);
5075 D3DSTATE_TO_STR(WINED3D_RS_ALPHAFUNC);
5076 D3DSTATE_TO_STR(WINED3D_RS_DITHERENABLE);
5077 D3DSTATE_TO_STR(WINED3D_RS_ALPHABLENDENABLE);
5078 D3DSTATE_TO_STR(WINED3D_RS_FOGENABLE);
5079 D3DSTATE_TO_STR(WINED3D_RS_SPECULARENABLE);
5080 D3DSTATE_TO_STR(WINED3D_RS_ZVISIBLE);
5081 D3DSTATE_TO_STR(WINED3D_RS_SUBPIXEL);
5082 D3DSTATE_TO_STR(WINED3D_RS_SUBPIXELX);
5083 D3DSTATE_TO_STR(WINED3D_RS_STIPPLEDALPHA);
5084 D3DSTATE_TO_STR(WINED3D_RS_FOGCOLOR);
5085 D3DSTATE_TO_STR(WINED3D_RS_FOGTABLEMODE);
5086 D3DSTATE_TO_STR(WINED3D_RS_FOGSTART);
5087 D3DSTATE_TO_STR(WINED3D_RS_FOGEND);
5088 D3DSTATE_TO_STR(WINED3D_RS_FOGDENSITY);
5089 D3DSTATE_TO_STR(WINED3D_RS_STIPPLEENABLE);
5090 D3DSTATE_TO_STR(WINED3D_RS_COLORKEYENABLE);
5091 D3DSTATE_TO_STR(WINED3D_RS_MIPMAPLODBIAS);
5092 D3DSTATE_TO_STR(WINED3D_RS_RANGEFOGENABLE);
5093 D3DSTATE_TO_STR(WINED3D_RS_ANISOTROPY);
5094 D3DSTATE_TO_STR(WINED3D_RS_FLUSHBATCH);
5095 D3DSTATE_TO_STR(WINED3D_RS_TRANSLUCENTSORTINDEPENDENT);
5096 D3DSTATE_TO_STR(WINED3D_RS_STENCILENABLE);
5097 D3DSTATE_TO_STR(WINED3D_RS_STENCILFAIL);
5098 D3DSTATE_TO_STR(WINED3D_RS_STENCILZFAIL);
5099 D3DSTATE_TO_STR(WINED3D_RS_STENCILPASS);
5100 D3DSTATE_TO_STR(WINED3D_RS_STENCILFUNC);
5101 D3DSTATE_TO_STR(WINED3D_RS_STENCILREF);
5102 D3DSTATE_TO_STR(WINED3D_RS_STENCILMASK);
5103 D3DSTATE_TO_STR(WINED3D_RS_STENCILWRITEMASK);
5104 D3DSTATE_TO_STR(WINED3D_RS_TEXTUREFACTOR);
5105 D3DSTATE_TO_STR(WINED3D_RS_WRAP0);
5106 D3DSTATE_TO_STR(WINED3D_RS_WRAP1);
5107 D3DSTATE_TO_STR(WINED3D_RS_WRAP2);
5108 D3DSTATE_TO_STR(WINED3D_RS_WRAP3);
5109 D3DSTATE_TO_STR(WINED3D_RS_WRAP4);
5110 D3DSTATE_TO_STR(WINED3D_RS_WRAP5);
5111 D3DSTATE_TO_STR(WINED3D_RS_WRAP6);
5112 D3DSTATE_TO_STR(WINED3D_RS_WRAP7);
5113 D3DSTATE_TO_STR(WINED3D_RS_CLIPPING);
5114 D3DSTATE_TO_STR(WINED3D_RS_LIGHTING);
5115 D3DSTATE_TO_STR(WINED3D_RS_EXTENTS);
5116 D3DSTATE_TO_STR(WINED3D_RS_AMBIENT);
5117 D3DSTATE_TO_STR(WINED3D_RS_FOGVERTEXMODE);
5118 D3DSTATE_TO_STR(WINED3D_RS_COLORVERTEX);
5119 D3DSTATE_TO_STR(WINED3D_RS_LOCALVIEWER);
5120 D3DSTATE_TO_STR(WINED3D_RS_NORMALIZENORMALS);
5121 D3DSTATE_TO_STR(WINED3D_RS_COLORKEYBLENDENABLE);
5122 D3DSTATE_TO_STR(WINED3D_RS_DIFFUSEMATERIALSOURCE);
5123 D3DSTATE_TO_STR(WINED3D_RS_SPECULARMATERIALSOURCE);
5124 D3DSTATE_TO_STR(WINED3D_RS_AMBIENTMATERIALSOURCE);
5125 D3DSTATE_TO_STR(WINED3D_RS_EMISSIVEMATERIALSOURCE);
5126 D3DSTATE_TO_STR(WINED3D_RS_VERTEXBLEND);
5127 D3DSTATE_TO_STR(WINED3D_RS_CLIPPLANEENABLE);
5128 D3DSTATE_TO_STR(WINED3D_RS_SOFTWAREVERTEXPROCESSING);
5129 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE);
5130 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MIN);
5131 D3DSTATE_TO_STR(WINED3D_RS_POINTSPRITEENABLE);
5132 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALEENABLE);
5133 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_A);
5134 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_B);
5135 D3DSTATE_TO_STR(WINED3D_RS_POINTSCALE_C);
5136 D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEANTIALIAS);
5137 D3DSTATE_TO_STR(WINED3D_RS_MULTISAMPLEMASK);
5138 D3DSTATE_TO_STR(WINED3D_RS_PATCHEDGESTYLE);
5139 D3DSTATE_TO_STR(WINED3D_RS_PATCHSEGMENTS);
5140 D3DSTATE_TO_STR(WINED3D_RS_DEBUGMONITORTOKEN);
5141 D3DSTATE_TO_STR(WINED3D_RS_POINTSIZE_MAX);
5142 D3DSTATE_TO_STR(WINED3D_RS_INDEXEDVERTEXBLENDENABLE);
5143 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE);
5144 D3DSTATE_TO_STR(WINED3D_RS_TWEENFACTOR);
5145 D3DSTATE_TO_STR(WINED3D_RS_BLENDOP);
5146 D3DSTATE_TO_STR(WINED3D_RS_POSITIONDEGREE);
5147 D3DSTATE_TO_STR(WINED3D_RS_NORMALDEGREE);
5148 D3DSTATE_TO_STR(WINED3D_RS_SCISSORTESTENABLE);
5149 D3DSTATE_TO_STR(WINED3D_RS_SLOPESCALEDEPTHBIAS);
5150 D3DSTATE_TO_STR(WINED3D_RS_ANTIALIASEDLINEENABLE);
5151 D3DSTATE_TO_STR(WINED3D_RS_MINTESSELLATIONLEVEL);
5152 D3DSTATE_TO_STR(WINED3D_RS_MAXTESSELLATIONLEVEL);
5153 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_X);
5154 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Y);
5155 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_Z);
5156 D3DSTATE_TO_STR(WINED3D_RS_ADAPTIVETESS_W);
5157 D3DSTATE_TO_STR(WINED3D_RS_ENABLEADAPTIVETESSELLATION);
5158 D3DSTATE_TO_STR(WINED3D_RS_TWOSIDEDSTENCILMODE);
5159 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFAIL);
5160 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILZFAIL);
5161 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILPASS);
5162 D3DSTATE_TO_STR(WINED3D_RS_BACK_STENCILFUNC);
5163 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE1);
5164 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE2);
5165 D3DSTATE_TO_STR(WINED3D_RS_COLORWRITEENABLE3);
5166 D3DSTATE_TO_STR(WINED3D_RS_SRGBWRITEENABLE);
5167 D3DSTATE_TO_STR(WINED3D_RS_DEPTHBIAS);
5168 D3DSTATE_TO_STR(WINED3D_RS_WRAP8);
5169 D3DSTATE_TO_STR(WINED3D_RS_WRAP9);
5170 D3DSTATE_TO_STR(WINED3D_RS_WRAP10);
5171 D3DSTATE_TO_STR(WINED3D_RS_WRAP11);
5172 D3DSTATE_TO_STR(WINED3D_RS_WRAP12);
5173 D3DSTATE_TO_STR(WINED3D_RS_WRAP13);
5174 D3DSTATE_TO_STR(WINED3D_RS_WRAP14);
5175 D3DSTATE_TO_STR(WINED3D_RS_WRAP15);
5176 D3DSTATE_TO_STR(WINED3D_RS_SEPARATEALPHABLENDENABLE);
5177 D3DSTATE_TO_STR(WINED3D_RS_SRCBLENDALPHA);
5178 D3DSTATE_TO_STR(WINED3D_RS_DESTBLENDALPHA);
5179 D3DSTATE_TO_STR(WINED3D_RS_BLENDOPALPHA);
5180 #undef D3DSTATE_TO_STR
5181 default:
5182 FIXME("Unrecognized %u render state!\n", state);
5183 return "unrecognized";
5187 const char *debug_d3dsamplerstate(enum wined3d_sampler_state state)
5189 switch (state)
5191 #define D3DSTATE_TO_STR(u) case u: return #u
5192 D3DSTATE_TO_STR(WINED3D_SAMP_BORDER_COLOR);
5193 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_U);
5194 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_V);
5195 D3DSTATE_TO_STR(WINED3D_SAMP_ADDRESS_W);
5196 D3DSTATE_TO_STR(WINED3D_SAMP_MAG_FILTER);
5197 D3DSTATE_TO_STR(WINED3D_SAMP_MIN_FILTER);
5198 D3DSTATE_TO_STR(WINED3D_SAMP_MIP_FILTER);
5199 D3DSTATE_TO_STR(WINED3D_SAMP_MIPMAP_LOD_BIAS);
5200 D3DSTATE_TO_STR(WINED3D_SAMP_MAX_MIP_LEVEL);
5201 D3DSTATE_TO_STR(WINED3D_SAMP_MAX_ANISOTROPY);
5202 D3DSTATE_TO_STR(WINED3D_SAMP_SRGB_TEXTURE);
5203 D3DSTATE_TO_STR(WINED3D_SAMP_ELEMENT_INDEX);
5204 D3DSTATE_TO_STR(WINED3D_SAMP_DMAP_OFFSET);
5205 #undef D3DSTATE_TO_STR
5206 default:
5207 FIXME("Unrecognized %u sampler state!\n", state);
5208 return "unrecognized";
5212 const char *debug_d3dtexturefiltertype(enum wined3d_texture_filter_type filter_type)
5214 switch (filter_type)
5216 #define D3DTEXTUREFILTERTYPE_TO_STR(u) case u: return #u
5217 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_NONE);
5218 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_POINT);
5219 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_LINEAR);
5220 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_ANISOTROPIC);
5221 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_FLAT_CUBIC);
5222 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_CUBIC);
5223 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_PYRAMIDAL_QUAD);
5224 D3DTEXTUREFILTERTYPE_TO_STR(WINED3D_TEXF_GAUSSIAN_QUAD);
5225 #undef D3DTEXTUREFILTERTYPE_TO_STR
5226 default:
5227 FIXME("Unrecognized texture filter type 0x%08x.\n", filter_type);
5228 return "unrecognized";
5232 const char *debug_d3dtexturestate(enum wined3d_texture_stage_state state)
5234 switch (state)
5236 #define D3DSTATE_TO_STR(u) case u: return #u
5237 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_OP);
5238 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG1);
5239 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG2);
5240 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_OP);
5241 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG1);
5242 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG2);
5243 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT00);
5244 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT01);
5245 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT10);
5246 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_MAT11);
5247 D3DSTATE_TO_STR(WINED3D_TSS_TEXCOORD_INDEX);
5248 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LSCALE);
5249 D3DSTATE_TO_STR(WINED3D_TSS_BUMPENV_LOFFSET);
5250 D3DSTATE_TO_STR(WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS);
5251 D3DSTATE_TO_STR(WINED3D_TSS_COLOR_ARG0);
5252 D3DSTATE_TO_STR(WINED3D_TSS_ALPHA_ARG0);
5253 D3DSTATE_TO_STR(WINED3D_TSS_RESULT_ARG);
5254 D3DSTATE_TO_STR(WINED3D_TSS_CONSTANT);
5255 #undef D3DSTATE_TO_STR
5256 default:
5257 FIXME("Unrecognized %u texture state!\n", state);
5258 return "unrecognized";
5262 const char *debug_d3dtop(enum wined3d_texture_op d3dtop)
5264 switch (d3dtop)
5266 #define D3DTOP_TO_STR(u) case u: return #u
5267 D3DTOP_TO_STR(WINED3D_TOP_DISABLE);
5268 D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG1);
5269 D3DTOP_TO_STR(WINED3D_TOP_SELECT_ARG2);
5270 D3DTOP_TO_STR(WINED3D_TOP_MODULATE);
5271 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_2X);
5272 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_4X);
5273 D3DTOP_TO_STR(WINED3D_TOP_ADD);
5274 D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED);
5275 D3DTOP_TO_STR(WINED3D_TOP_ADD_SIGNED_2X);
5276 D3DTOP_TO_STR(WINED3D_TOP_SUBTRACT);
5277 D3DTOP_TO_STR(WINED3D_TOP_ADD_SMOOTH);
5278 D3DTOP_TO_STR(WINED3D_TOP_BLEND_DIFFUSE_ALPHA);
5279 D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA);
5280 D3DTOP_TO_STR(WINED3D_TOP_BLEND_FACTOR_ALPHA);
5281 D3DTOP_TO_STR(WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM);
5282 D3DTOP_TO_STR(WINED3D_TOP_BLEND_CURRENT_ALPHA);
5283 D3DTOP_TO_STR(WINED3D_TOP_PREMODULATE);
5284 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR);
5285 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA);
5286 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR);
5287 D3DTOP_TO_STR(WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA);
5288 D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP);
5289 D3DTOP_TO_STR(WINED3D_TOP_BUMPENVMAP_LUMINANCE);
5290 D3DTOP_TO_STR(WINED3D_TOP_DOTPRODUCT3);
5291 D3DTOP_TO_STR(WINED3D_TOP_MULTIPLY_ADD);
5292 D3DTOP_TO_STR(WINED3D_TOP_LERP);
5293 #undef D3DTOP_TO_STR
5294 default:
5295 FIXME("Unrecognized texture op %#x.\n", d3dtop);
5296 return "unrecognized";
5300 const char *debug_d3dtstype(enum wined3d_transform_state tstype)
5302 switch (tstype)
5304 #define TSTYPE_TO_STR(tstype) case tstype: return #tstype
5305 TSTYPE_TO_STR(WINED3D_TS_VIEW);
5306 TSTYPE_TO_STR(WINED3D_TS_PROJECTION);
5307 TSTYPE_TO_STR(WINED3D_TS_TEXTURE0);
5308 TSTYPE_TO_STR(WINED3D_TS_TEXTURE1);
5309 TSTYPE_TO_STR(WINED3D_TS_TEXTURE2);
5310 TSTYPE_TO_STR(WINED3D_TS_TEXTURE3);
5311 TSTYPE_TO_STR(WINED3D_TS_TEXTURE4);
5312 TSTYPE_TO_STR(WINED3D_TS_TEXTURE5);
5313 TSTYPE_TO_STR(WINED3D_TS_TEXTURE6);
5314 TSTYPE_TO_STR(WINED3D_TS_TEXTURE7);
5315 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(0));
5316 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(1));
5317 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(2));
5318 TSTYPE_TO_STR(WINED3D_TS_WORLD_MATRIX(3));
5319 #undef TSTYPE_TO_STR
5320 default:
5321 if (tstype > 256 && tstype < 512)
5323 FIXME("WINED3D_TS_WORLD_MATRIX(%u). 1..255 not currently supported.\n", tstype);
5324 return ("WINED3D_TS_WORLD_MATRIX > 0");
5326 FIXME("Unrecognized transform state %#x.\n", tstype);
5327 return "unrecognized";
5331 const char *debug_shader_type(enum wined3d_shader_type type)
5333 switch(type)
5335 #define WINED3D_TO_STR(type) case type: return #type
5336 WINED3D_TO_STR(WINED3D_SHADER_TYPE_PIXEL);
5337 WINED3D_TO_STR(WINED3D_SHADER_TYPE_VERTEX);
5338 WINED3D_TO_STR(WINED3D_SHADER_TYPE_GEOMETRY);
5339 WINED3D_TO_STR(WINED3D_SHADER_TYPE_HULL);
5340 WINED3D_TO_STR(WINED3D_SHADER_TYPE_DOMAIN);
5341 WINED3D_TO_STR(WINED3D_SHADER_TYPE_COMPUTE);
5342 #undef WINED3D_TO_STR
5343 default:
5344 FIXME("Unrecognized shader type %#x.\n", type);
5345 return "unrecognized";
5349 const char *debug_d3dstate(DWORD state)
5351 if (STATE_IS_RENDER(state))
5352 return wine_dbg_sprintf("STATE_RENDER(%s)", debug_d3drenderstate(state - STATE_RENDER(0)));
5353 if (STATE_IS_TEXTURESTAGE(state))
5355 DWORD texture_stage = (state - STATE_TEXTURESTAGE(0, 0)) / (WINED3D_HIGHEST_TEXTURE_STATE + 1);
5356 DWORD texture_state = state - STATE_TEXTURESTAGE(texture_stage, 0);
5357 return wine_dbg_sprintf("STATE_TEXTURESTAGE(%#x, %s)",
5358 texture_stage, debug_d3dtexturestate(texture_state));
5360 if (STATE_IS_SAMPLER(state))
5361 return wine_dbg_sprintf("STATE_SAMPLER(%#x)", state - STATE_SAMPLER(0));
5362 if (STATE_IS_COMPUTE_SHADER(state))
5363 return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
5364 if (STATE_IS_GRAPHICS_SHADER(state))
5365 return wine_dbg_sprintf("STATE_SHADER(%s)", debug_shader_type(state - STATE_SHADER(0)));
5366 if (STATE_IS_COMPUTE_CONSTANT_BUFFER(state))
5367 return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(WINED3D_SHADER_TYPE_COMPUTE));
5368 if (STATE_IS_GRAPHICS_CONSTANT_BUFFER(state))
5369 return wine_dbg_sprintf("STATE_CONSTANT_BUFFER(%s)", debug_shader_type(state - STATE_CONSTANT_BUFFER(0)));
5370 if (STATE_IS_COMPUTE_SHADER_RESOURCE_BINDING(state))
5371 return "STATE_COMPUTE_SHADER_RESOURCE_BINDING";
5372 if (STATE_IS_GRAPHICS_SHADER_RESOURCE_BINDING(state))
5373 return "STATE_GRAPHICS_SHADER_RESOURCE_BINDING";
5374 if (STATE_IS_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING(state))
5375 return "STATE_COMPUTE_UNORDERED_ACCESS_VIEW_BINDING";
5376 if (STATE_IS_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING(state))
5377 return "STATE_GRAPHICS_UNORDERED_ACCESS_VIEW_BINDING";
5378 if (STATE_IS_TRANSFORM(state))
5379 return wine_dbg_sprintf("STATE_TRANSFORM(%s)", debug_d3dtstype(state - STATE_TRANSFORM(0)));
5380 if (STATE_IS_STREAMSRC(state))
5381 return "STATE_STREAMSRC";
5382 if (STATE_IS_INDEXBUFFER(state))
5383 return "STATE_INDEXBUFFER";
5384 if (STATE_IS_VDECL(state))
5385 return "STATE_VDECL";
5386 if (STATE_IS_VIEWPORT(state))
5387 return "STATE_VIEWPORT";
5388 if (STATE_IS_LIGHT_TYPE(state))
5389 return "STATE_LIGHT_TYPE";
5390 if (STATE_IS_ACTIVELIGHT(state))
5391 return wine_dbg_sprintf("STATE_ACTIVELIGHT(%#x)", state - STATE_ACTIVELIGHT(0));
5392 if (STATE_IS_SCISSORRECT(state))
5393 return "STATE_SCISSORRECT";
5394 if (STATE_IS_CLIPPLANE(state))
5395 return wine_dbg_sprintf("STATE_CLIPPLANE(%#x)", state - STATE_CLIPPLANE(0));
5396 if (STATE_IS_MATERIAL(state))
5397 return "STATE_MATERIAL";
5398 if (STATE_IS_RASTERIZER(state))
5399 return "STATE_RASTERIZER";
5400 if (STATE_IS_POINTSPRITECOORDORIGIN(state))
5401 return "STATE_POINTSPRITECOORDORIGIN";
5402 if (STATE_IS_BASEVERTEXINDEX(state))
5403 return "STATE_BASEVERTEXINDEX";
5404 if (STATE_IS_FRAMEBUFFER(state))
5405 return "STATE_FRAMEBUFFER";
5406 if (STATE_IS_POINT_ENABLE(state))
5407 return "STATE_POINT_ENABLE";
5408 if (STATE_IS_COLOR_KEY(state))
5409 return "STATE_COLOR_KEY";
5410 if (STATE_IS_STREAM_OUTPUT(state))
5411 return "STATE_STREAM_OUTPUT";
5412 if (STATE_IS_BLEND(state))
5413 return "STATE_BLEND";
5414 if (STATE_IS_BLEND_FACTOR(state))
5415 return "STATE_BLEND_FACTOR";
5416 if (STATE_IS_SAMPLE_MASK(state))
5417 return "STATE_SAMPLE_MASK";
5418 if (STATE_IS_DEPTH_STENCIL(state))
5419 return "STATE_DEPTH_STENCIL";
5420 if (STATE_IS_STENCIL_REF(state))
5421 return "STATE_STENCIL_REF";
5423 return wine_dbg_sprintf("UNKNOWN_STATE(%#x)", state);
5426 const char *debug_fboattachment(GLenum attachment)
5428 switch(attachment)
5430 #define WINED3D_TO_STR(x) case x: return #x
5431 WINED3D_TO_STR(GL_COLOR_ATTACHMENT0);
5432 WINED3D_TO_STR(GL_COLOR_ATTACHMENT1);
5433 WINED3D_TO_STR(GL_COLOR_ATTACHMENT2);
5434 WINED3D_TO_STR(GL_COLOR_ATTACHMENT3);
5435 WINED3D_TO_STR(GL_COLOR_ATTACHMENT4);
5436 WINED3D_TO_STR(GL_COLOR_ATTACHMENT5);
5437 WINED3D_TO_STR(GL_COLOR_ATTACHMENT6);
5438 WINED3D_TO_STR(GL_COLOR_ATTACHMENT7);
5439 WINED3D_TO_STR(GL_COLOR_ATTACHMENT8);
5440 WINED3D_TO_STR(GL_COLOR_ATTACHMENT9);
5441 WINED3D_TO_STR(GL_COLOR_ATTACHMENT10);
5442 WINED3D_TO_STR(GL_COLOR_ATTACHMENT11);
5443 WINED3D_TO_STR(GL_COLOR_ATTACHMENT12);
5444 WINED3D_TO_STR(GL_COLOR_ATTACHMENT13);
5445 WINED3D_TO_STR(GL_COLOR_ATTACHMENT14);
5446 WINED3D_TO_STR(GL_COLOR_ATTACHMENT15);
5447 WINED3D_TO_STR(GL_DEPTH_ATTACHMENT);
5448 WINED3D_TO_STR(GL_STENCIL_ATTACHMENT);
5449 #undef WINED3D_TO_STR
5450 default:
5451 return wine_dbg_sprintf("Unknown FBO attachment %#x", attachment);
5455 const char *debug_fbostatus(GLenum status) {
5456 switch(status) {
5457 #define FBOSTATUS_TO_STR(u) case u: return #u
5458 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_COMPLETE);
5459 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
5460 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
5461 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT);
5462 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT);
5463 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
5464 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
5465 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE);
5466 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS);
5467 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB);
5468 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNSUPPORTED);
5469 FBOSTATUS_TO_STR(GL_FRAMEBUFFER_UNDEFINED);
5470 #undef FBOSTATUS_TO_STR
5471 default:
5472 FIXME("Unrecognized FBO status 0x%08x.\n", status);
5473 return "unrecognized";
5477 const char *debug_glerror(GLenum error) {
5478 switch(error) {
5479 #define GLERROR_TO_STR(u) case u: return #u
5480 GLERROR_TO_STR(GL_NO_ERROR);
5481 GLERROR_TO_STR(GL_INVALID_ENUM);
5482 GLERROR_TO_STR(GL_INVALID_VALUE);
5483 GLERROR_TO_STR(GL_INVALID_OPERATION);
5484 GLERROR_TO_STR(GL_STACK_OVERFLOW);
5485 GLERROR_TO_STR(GL_STACK_UNDERFLOW);
5486 GLERROR_TO_STR(GL_OUT_OF_MEMORY);
5487 GLERROR_TO_STR(GL_INVALID_FRAMEBUFFER_OPERATION);
5488 #undef GLERROR_TO_STR
5489 default:
5490 FIXME("Unrecognized GL error 0x%08x.\n", error);
5491 return "unrecognized";
5495 const char *wined3d_debug_vkresult(VkResult vr)
5497 switch (vr)
5499 #define WINED3D_TO_STR(x) case x: return #x
5500 WINED3D_TO_STR(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR);
5501 WINED3D_TO_STR(VK_ERROR_NOT_PERMITTED_EXT);
5502 WINED3D_TO_STR(VK_ERROR_FRAGMENTATION_EXT);
5503 WINED3D_TO_STR(VK_ERROR_INVALID_EXTERNAL_HANDLE);
5504 WINED3D_TO_STR(VK_ERROR_OUT_OF_POOL_MEMORY);
5505 WINED3D_TO_STR(VK_ERROR_INVALID_SHADER_NV);
5506 WINED3D_TO_STR(VK_ERROR_OUT_OF_DATE_KHR);
5507 WINED3D_TO_STR(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
5508 WINED3D_TO_STR(VK_ERROR_SURFACE_LOST_KHR);
5509 WINED3D_TO_STR(VK_ERROR_FRAGMENTED_POOL);
5510 WINED3D_TO_STR(VK_ERROR_FORMAT_NOT_SUPPORTED);
5511 WINED3D_TO_STR(VK_ERROR_TOO_MANY_OBJECTS);
5512 WINED3D_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER);
5513 WINED3D_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT);
5514 WINED3D_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT);
5515 WINED3D_TO_STR(VK_ERROR_LAYER_NOT_PRESENT);
5516 WINED3D_TO_STR(VK_ERROR_MEMORY_MAP_FAILED);
5517 WINED3D_TO_STR(VK_ERROR_DEVICE_LOST);
5518 WINED3D_TO_STR(VK_ERROR_INITIALIZATION_FAILED);
5519 WINED3D_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
5520 WINED3D_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY);
5521 WINED3D_TO_STR(VK_SUCCESS);
5522 WINED3D_TO_STR(VK_NOT_READY);
5523 WINED3D_TO_STR(VK_TIMEOUT);
5524 WINED3D_TO_STR(VK_EVENT_SET);
5525 WINED3D_TO_STR(VK_EVENT_RESET);
5526 WINED3D_TO_STR(VK_INCOMPLETE);
5527 WINED3D_TO_STR(VK_SUBOPTIMAL_KHR);
5528 #undef WINED3D_TO_STR
5529 default:
5530 return wine_dbg_sprintf("unrecognised(%d)", vr);
5534 static const char *debug_fixup_channel_source(enum fixup_channel_source source)
5536 switch(source)
5538 #define WINED3D_TO_STR(x) case x: return #x
5539 WINED3D_TO_STR(CHANNEL_SOURCE_ZERO);
5540 WINED3D_TO_STR(CHANNEL_SOURCE_ONE);
5541 WINED3D_TO_STR(CHANNEL_SOURCE_X);
5542 WINED3D_TO_STR(CHANNEL_SOURCE_Y);
5543 WINED3D_TO_STR(CHANNEL_SOURCE_Z);
5544 WINED3D_TO_STR(CHANNEL_SOURCE_W);
5545 WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX0);
5546 WINED3D_TO_STR(CHANNEL_SOURCE_COMPLEX1);
5547 #undef WINED3D_TO_STR
5548 default:
5549 FIXME("Unrecognized fixup_channel_source %#x\n", source);
5550 return "unrecognized";
5554 static const char *debug_complex_fixup(enum complex_fixup fixup)
5556 switch(fixup)
5558 #define WINED3D_TO_STR(x) case x: return #x
5559 WINED3D_TO_STR(COMPLEX_FIXUP_YUY2);
5560 WINED3D_TO_STR(COMPLEX_FIXUP_UYVY);
5561 WINED3D_TO_STR(COMPLEX_FIXUP_YV12);
5562 WINED3D_TO_STR(COMPLEX_FIXUP_NV12);
5563 WINED3D_TO_STR(COMPLEX_FIXUP_P8);
5564 WINED3D_TO_STR(COMPLEX_FIXUP_YUV);
5565 #undef WINED3D_TO_STR
5566 default:
5567 FIXME("Unrecognized complex fixup %#x\n", fixup);
5568 return "unrecognized";
5572 void dump_color_fixup_desc(struct color_fixup_desc fixup)
5574 if (is_complex_fixup(fixup))
5576 TRACE("\tComplex: %s\n", debug_complex_fixup(get_complex_fixup(fixup)));
5577 return;
5580 TRACE("\tX: %s%s\n", debug_fixup_channel_source(fixup.x_source), fixup.x_sign_fixup ? ", SIGN_FIXUP" : "");
5581 TRACE("\tY: %s%s\n", debug_fixup_channel_source(fixup.y_source), fixup.y_sign_fixup ? ", SIGN_FIXUP" : "");
5582 TRACE("\tZ: %s%s\n", debug_fixup_channel_source(fixup.z_source), fixup.z_sign_fixup ? ", SIGN_FIXUP" : "");
5583 TRACE("\tW: %s%s\n", debug_fixup_channel_source(fixup.w_source), fixup.w_sign_fixup ? ", SIGN_FIXUP" : "");
5586 BOOL is_invalid_op(const struct wined3d_state *state, int stage,
5587 enum wined3d_texture_op op, DWORD arg1, DWORD arg2, DWORD arg3)
5589 if (op == WINED3D_TOP_DISABLE)
5590 return FALSE;
5591 if (state->textures[stage])
5592 return FALSE;
5594 if ((arg1 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5595 && op != WINED3D_TOP_SELECT_ARG2)
5596 return TRUE;
5597 if ((arg2 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5598 && op != WINED3D_TOP_SELECT_ARG1)
5599 return TRUE;
5600 if ((arg3 & WINED3DTA_SELECTMASK) == WINED3DTA_TEXTURE
5601 && (op == WINED3D_TOP_MULTIPLY_ADD || op == WINED3D_TOP_LERP))
5602 return TRUE;
5604 return FALSE;
5607 void get_identity_matrix(struct wined3d_matrix *mat)
5609 static const struct wined3d_matrix identity =
5611 1.0f, 0.0f, 0.0f, 0.0f,
5612 0.0f, 1.0f, 0.0f, 0.0f,
5613 0.0f, 0.0f, 1.0f, 0.0f,
5614 0.0f, 0.0f, 0.0f, 1.0f,
5617 *mat = identity;
5620 void get_modelview_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5621 unsigned int index, struct wined3d_matrix *mat)
5623 if (context->last_was_rhw)
5624 get_identity_matrix(mat);
5625 else
5626 multiply_matrix(mat, &state->transforms[WINED3D_TS_VIEW], &state->transforms[WINED3D_TS_WORLD_MATRIX(index)]);
5629 void get_projection_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5630 struct wined3d_matrix *mat)
5632 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
5633 BOOL clip_control, flip;
5634 float center_offset;
5636 /* There are a couple of additional things we have to take into account
5637 * here besides the projection transformation itself:
5638 * - We need to flip along the y-axis in case of offscreen rendering.
5639 * - OpenGL Z range is {-Wc,...,Wc} while D3D Z range is {0,...,Wc}.
5640 * - <= D3D9 coordinates refer to pixel centers while GL coordinates
5641 * refer to pixel corners. D3D10 fixed this particular oddity.
5642 * - D3D has a top-left filling convention while GL does not specify
5643 * a particular behavior, other than that that the GL implementation
5644 * needs to be consistent.
5646 * In order to handle the pixel center, we translate by 0.5 / VPw and
5647 * 0.5 / VPh. We test the filling convention during adapter init and
5648 * add a small offset to correct it if necessary. See
5649 * wined3d_caps_gl_ctx_test_filling_convention() for more details on how
5650 * we test GL and considerations regarding the added offset value.
5652 * If we have GL_ARB_clip_control we take care of all this through
5653 * viewport properties and don't have to translate geometry. */
5655 /* Projection matrices are <= d3d9, which all have integer pixel centers. */
5656 if (!(d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER))
5657 ERR("Did not expect to enter this codepath without WINED3D_PIXEL_CENTER_INTEGER.\n");
5659 clip_control = d3d_info->clip_control;
5660 flip = !clip_control && context->render_offscreen;
5661 if (!clip_control)
5662 center_offset = 1.0f + d3d_info->filling_convention_offset;
5663 else
5664 center_offset = 0.0f;
5666 if (context->last_was_rhw)
5668 /* Transform D3D RHW coordinates to OpenGL clip coordinates. */
5669 float x = state->viewports[0].x;
5670 float y = state->viewports[0].y;
5671 float w = state->viewports[0].width;
5672 float h = state->viewports[0].height;
5673 float x_scale = 2.0f / w;
5674 float x_offset = (center_offset - (2.0f * x) - w) / w;
5675 float y_scale = flip ? 2.0f / h : 2.0f / -h;
5676 float y_offset = flip
5677 ? (center_offset - (2.0f * y) - h) / h
5678 : (center_offset - (2.0f * y) - h) / -h;
5679 bool zenable = state->fb.depth_stencil ?
5680 (state->depth_stencil_state ? state->depth_stencil_state->desc.depth : true) : false;
5681 float z_scale = zenable ? clip_control ? 1.0f : 2.0f : 0.0f;
5682 float z_offset = zenable ? clip_control ? 0.0f : -1.0f : 0.0f;
5683 const struct wined3d_matrix projection =
5685 x_scale, 0.0f, 0.0f, 0.0f,
5686 0.0f, y_scale, 0.0f, 0.0f,
5687 0.0f, 0.0f, z_scale, 0.0f,
5688 x_offset, y_offset, z_offset, 1.0f,
5691 *mat = projection;
5693 else
5695 float y_scale = flip ? -1.0f : 1.0f;
5696 float x_offset = center_offset / state->viewports[0].width;
5697 float y_offset = flip
5698 ? center_offset / state->viewports[0].height
5699 : -center_offset / state->viewports[0].height;
5700 float z_scale = clip_control ? 1.0f : 2.0f;
5701 float z_offset = clip_control ? 0.0f : -1.0f;
5702 const struct wined3d_matrix projection =
5704 1.0f, 0.0f, 0.0f, 0.0f,
5705 0.0f, y_scale, 0.0f, 0.0f,
5706 0.0f, 0.0f, z_scale, 0.0f,
5707 x_offset, y_offset, z_offset, 1.0f,
5710 multiply_matrix(mat, &projection, &state->transforms[WINED3D_TS_PROJECTION]);
5714 /* Setup this textures matrix according to the texture flags. */
5715 static void compute_texture_matrix(const struct wined3d_matrix *matrix, uint32_t flags, BOOL calculated_coords,
5716 BOOL transformed, enum wined3d_format_id format_id, BOOL ffp_proj_control, struct wined3d_matrix *out_matrix)
5718 struct wined3d_matrix mat;
5720 if (flags == WINED3D_TTFF_DISABLE || flags == WINED3D_TTFF_COUNT1 || transformed)
5722 get_identity_matrix(out_matrix);
5723 return;
5726 if (flags == (WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED))
5728 ERR("Invalid texture transform flags: WINED3D_TTFF_COUNT1 | WINED3D_TTFF_PROJECTED.\n");
5729 return;
5732 mat = *matrix;
5734 if (flags & WINED3D_TTFF_PROJECTED)
5736 if (!ffp_proj_control)
5738 switch (flags & ~WINED3D_TTFF_PROJECTED)
5740 case WINED3D_TTFF_COUNT2:
5741 mat._14 = mat._12;
5742 mat._24 = mat._22;
5743 mat._34 = mat._32;
5744 mat._44 = mat._42;
5745 mat._12 = mat._22 = mat._32 = mat._42 = 0.0f;
5746 break;
5747 case WINED3D_TTFF_COUNT3:
5748 mat._14 = mat._13;
5749 mat._24 = mat._23;
5750 mat._34 = mat._33;
5751 mat._44 = mat._43;
5752 mat._13 = mat._23 = mat._33 = mat._43 = 0.0f;
5753 break;
5757 else
5759 /* Under Direct3D the R/Z coord can be used for translation, under
5760 * OpenGL we use the Q coord instead. */
5761 if (!calculated_coords)
5763 switch (format_id)
5765 /* Direct3D passes the default 1.0 in the 2nd coord, while GL
5766 * passes it in the 4th. Swap 2nd and 4th coord. No need to
5767 * store the value of mat._41 in mat._21 because the input
5768 * value to the transformation will be 0, so the matrix value
5769 * is irrelevant. */
5770 case WINED3DFMT_R32_FLOAT:
5771 mat._41 = mat._21;
5772 mat._42 = mat._22;
5773 mat._43 = mat._23;
5774 mat._44 = mat._24;
5775 break;
5776 /* See above, just 3rd and 4th coord. */
5777 case WINED3DFMT_R32G32_FLOAT:
5778 mat._41 = mat._31;
5779 mat._42 = mat._32;
5780 mat._43 = mat._33;
5781 mat._44 = mat._34;
5782 break;
5783 case WINED3DFMT_R32G32B32_FLOAT: /* Opengl defaults match dx defaults */
5784 case WINED3DFMT_R32G32B32A32_FLOAT: /* No defaults apply, all app defined */
5786 /* This is to prevent swapping the matrix lines and put the default 4th coord = 1.0
5787 * into a bad place. The division elimination below will apply to make sure the
5788 * 1.0 doesn't do anything bad. The caller will set this value if the stride is 0
5790 case WINED3DFMT_UNKNOWN: /* No texture coords, 0/0/0/1 defaults are passed */
5791 break;
5792 default:
5793 FIXME("Unexpected fixed function texture coord input\n");
5796 if (!ffp_proj_control)
5798 switch (flags & ~WINED3D_TTFF_PROJECTED)
5800 /* case WINED3D_TTFF_COUNT1: Won't ever get here. */
5801 case WINED3D_TTFF_COUNT2:
5802 mat._13 = mat._23 = mat._33 = mat._43 = 0.0f;
5803 /* OpenGL divides the first 3 vertex coordinates by the 4th by
5804 * default, which is essentially the same as D3DTTFF_PROJECTED.
5805 * Make sure that the 4th coordinate evaluates to 1.0 to
5806 * eliminate that.
5808 * If the fixed function pipeline is used, the 4th value
5809 * remains unused, so there is no danger in doing this. With
5810 * vertex shaders we have a problem. Should an application hit
5811 * that problem, the code here would have to check for pixel
5812 * shaders, and the shader has to undo the default GL divide.
5814 * A more serious problem occurs if the application passes 4
5815 * coordinates in, and the 4th is != 1.0 (OpenGL default).
5816 * This would have to be fixed with immediate mode draws. */
5817 default:
5818 mat._14 = mat._24 = mat._34 = 0.0f; mat._44 = 1.0f;
5823 *out_matrix = mat;
5826 void get_texture_matrix(const struct wined3d_context *context, const struct wined3d_state *state,
5827 unsigned int tex, struct wined3d_matrix *mat)
5829 const struct wined3d_device *device = context->device;
5830 BOOL generated = (state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX] & 0xffff0000)
5831 != WINED3DTSS_TCI_PASSTHRU;
5832 unsigned int coord_idx = min(state->texture_states[tex][WINED3D_TSS_TEXCOORD_INDEX & 0x0000ffff],
5833 WINED3D_MAX_TEXTURES - 1);
5835 compute_texture_matrix(&state->transforms[WINED3D_TS_TEXTURE0 + tex],
5836 state->texture_states[tex][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS],
5837 generated, context->last_was_rhw,
5838 context->stream_info.use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))
5839 ? context->stream_info.elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->id
5840 : WINED3DFMT_UNKNOWN,
5841 device->shader_backend->shader_has_ffp_proj_control(device->shader_priv), mat);
5843 if ((context->lastWasPow2Texture & (1u << tex)) && state->textures[tex])
5845 if (generated)
5846 FIXME("Non-power-of-two texture being used with generated texture coords.\n");
5847 /* NP2 texcoord fixup is implemented for pixelshaders so only enable the
5848 * fixed-function-pipeline fixup via pow2Matrix when no PS is used. */
5849 if (!use_ps(state))
5851 TRACE("Non-power-of-two texture matrix multiply fixup.\n");
5852 multiply_matrix(mat, mat, (struct wined3d_matrix *)state->textures[tex]->pow2_matrix);
5857 void get_pointsize_minmax(const struct wined3d_context *context, const struct wined3d_state *state,
5858 float *out_min, float *out_max)
5860 union
5862 DWORD d;
5863 float f;
5864 } min, max;
5866 min.d = state->render_states[WINED3D_RS_POINTSIZE_MIN];
5867 max.d = state->render_states[WINED3D_RS_POINTSIZE_MAX];
5869 if (min.f > max.f)
5870 min.f = max.f;
5872 *out_min = min.f;
5873 *out_max = max.f;
5876 void get_pointsize(const struct wined3d_context *context, const struct wined3d_state *state,
5877 float *out_pointsize, float *out_att)
5879 /* POINTSCALEENABLE controls how point size value is treated. If set to
5880 * true, the point size is scaled with respect to height of viewport.
5881 * When set to false point size is in pixels. */
5882 union
5884 DWORD d;
5885 float f;
5886 } pointsize, a, b, c;
5888 out_att[0] = 1.0f;
5889 out_att[1] = 0.0f;
5890 out_att[2] = 0.0f;
5892 pointsize.d = state->render_states[WINED3D_RS_POINTSIZE];
5893 a.d = state->render_states[WINED3D_RS_POINTSCALE_A];
5894 b.d = state->render_states[WINED3D_RS_POINTSCALE_B];
5895 c.d = state->render_states[WINED3D_RS_POINTSCALE_C];
5897 /* Always use first viewport, this path does not apply to d3d10/11 multiple viewports case. */
5898 if (state->render_states[WINED3D_RS_POINTSCALEENABLE])
5900 float scale_factor = state->viewports[0].height * state->viewports[0].height;
5902 out_att[0] = a.f / scale_factor;
5903 out_att[1] = b.f / scale_factor;
5904 out_att[2] = c.f / scale_factor;
5906 *out_pointsize = pointsize.f;
5909 void get_fog_start_end(const struct wined3d_context *context, const struct wined3d_state *state,
5910 float *start, float *end)
5912 union
5914 DWORD d;
5915 float f;
5916 } tmpvalue;
5918 switch (context->fog_source)
5920 case FOGSOURCE_VS:
5921 *start = 1.0f;
5922 *end = 0.0f;
5923 break;
5925 case FOGSOURCE_COORD:
5926 *start = 255.0f;
5927 *end = 0.0f;
5928 break;
5930 case FOGSOURCE_FFP:
5931 tmpvalue.d = state->render_states[WINED3D_RS_FOGSTART];
5932 *start = tmpvalue.f;
5933 tmpvalue.d = state->render_states[WINED3D_RS_FOGEND];
5934 *end = tmpvalue.f;
5935 /* Special handling for fog_start == fog_end. In d3d with vertex
5936 * fog, everything is fogged. With table fog, everything with
5937 * fog_coord < fog_start is unfogged, and fog_coord > fog_start
5938 * is fogged. Windows drivers disagree when fog_coord == fog_start. */
5939 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE && *start == *end)
5941 *start = -INFINITY;
5942 *end = 0.0f;
5944 break;
5946 default:
5947 /* This should not happen, context->fog_source is set in wined3d, not the app. */
5948 ERR("Unexpected fog coordinate source.\n");
5949 *start = 0.0f;
5950 *end = 0.0f;
5954 static BOOL wined3d_get_primary_display(WCHAR *display)
5956 DISPLAY_DEVICEW display_device;
5957 DWORD device_idx = 0;
5959 display_device.cb = sizeof(display_device);
5960 while (EnumDisplayDevicesW(NULL, device_idx++, &display_device, 0))
5962 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
5964 lstrcpyW(display, display_device.DeviceName);
5965 return TRUE;
5969 return FALSE;
5972 BOOL wined3d_get_primary_adapter_luid(LUID *luid)
5974 D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME open_adapter_param;
5975 D3DKMT_CLOSEADAPTER close_adapter_param;
5977 if (!wined3d_get_primary_display(open_adapter_param.DeviceName))
5978 return FALSE;
5980 if (D3DKMTOpenAdapterFromGdiDisplayName(&open_adapter_param))
5981 return FALSE;
5983 *luid = open_adapter_param.AdapterLuid;
5984 close_adapter_param.hAdapter = open_adapter_param.hAdapter;
5985 D3DKMTCloseAdapter(&close_adapter_param);
5986 return TRUE;
5989 uint32_t wined3d_format_pack(const struct wined3d_format *format, const struct wined3d_uvec4 *value)
5991 uint32_t p = 0;
5993 p |= (value->x & wined3d_mask_from_size(format->red_size)) << format->red_offset;
5994 p |= (value->y & wined3d_mask_from_size(format->green_size)) << format->green_offset;
5995 p |= (value->z & wined3d_mask_from_size(format->blue_size)) << format->blue_offset;
5996 p |= (value->w & wined3d_mask_from_size(format->alpha_size)) << format->alpha_offset;
5998 return p;
6001 /* Note: It's the caller's responsibility to ensure values can be expressed
6002 * in the requested format. UNORM formats for example can only express values
6003 * in the range 0.0f -> 1.0f. */
6004 DWORD wined3d_format_convert_from_float(const struct wined3d_format *format, const struct wined3d_color *color)
6006 static const struct
6008 enum wined3d_format_id format_id;
6009 struct wined3d_vec4 mul;
6010 struct wined3d_uvec4 shift;
6012 float_conv[] =
6014 {WINED3DFMT_B8G8R8A8_UNORM, { 255.0f, 255.0f, 255.0f, 255.0f}, {16, 8, 0, 24}},
6015 {WINED3DFMT_B8G8R8X8_UNORM, { 255.0f, 255.0f, 255.0f, 255.0f}, {16, 8, 0, 24}},
6016 {WINED3DFMT_B8G8R8_UNORM, { 255.0f, 255.0f, 255.0f, 255.0f}, {16, 8, 0, 24}},
6017 {WINED3DFMT_B5G6R5_UNORM, { 31.0f, 63.0f, 31.0f, 0.0f}, {11, 5, 0, 0}},
6018 {WINED3DFMT_B5G5R5A1_UNORM, { 31.0f, 31.0f, 31.0f, 1.0f}, {10, 5, 0, 15}},
6019 {WINED3DFMT_B5G5R5X1_UNORM, { 31.0f, 31.0f, 31.0f, 1.0f}, {10, 5, 0, 15}},
6020 {WINED3DFMT_R8_UNORM, { 255.0f, 0.0f, 0.0f, 0.0f}, { 0, 0, 0, 0}},
6021 {WINED3DFMT_A8_UNORM, { 0.0f, 0.0f, 0.0f, 255.0f}, { 0, 0, 0, 0}},
6022 {WINED3DFMT_B4G4R4A4_UNORM, { 15.0f, 15.0f, 15.0f, 15.0f}, { 8, 4, 0, 12}},
6023 {WINED3DFMT_B4G4R4X4_UNORM, { 15.0f, 15.0f, 15.0f, 15.0f}, { 8, 4, 0, 12}},
6024 {WINED3DFMT_B2G3R3_UNORM, { 7.0f, 7.0f, 3.0f, 0.0f}, { 5, 2, 0, 0}},
6025 {WINED3DFMT_R8G8B8A8_UNORM, { 255.0f, 255.0f, 255.0f, 255.0f}, { 0, 8, 16, 24}},
6026 {WINED3DFMT_R8G8B8X8_UNORM, { 255.0f, 255.0f, 255.0f, 255.0f}, { 0, 8, 16, 24}},
6027 {WINED3DFMT_B10G10R10A2_UNORM, { 1023.0f, 1023.0f, 1023.0f, 3.0f}, {20, 10, 0, 30}},
6028 {WINED3DFMT_R10G10B10A2_UNORM, { 1023.0f, 1023.0f, 1023.0f, 3.0f}, { 0, 10, 20, 30}},
6029 {WINED3DFMT_P8_UINT, { 0.0f, 0.0f, 0.0f, 255.0f}, { 0, 0, 0, 0}},
6030 {WINED3DFMT_S1_UINT_D15_UNORM, { 32767.0f, 0.0f, 0.0f, 0.0f}, { 0, 0, 0, 0}},
6031 {WINED3DFMT_D16_UNORM, { 65535.0f, 0.0f, 0.0f, 0.0f}, { 0, 0, 0, 0}},
6033 static const struct
6035 enum wined3d_format_id format_id;
6036 struct wined3d_dvec4 mul;
6037 struct wined3d_uvec4 shift;
6039 double_conv[] =
6041 {WINED3DFMT_D24_UNORM_S8_UINT, { 16777215.0, 1.0, 0.0, 0.0}, {8, 0, 0, 0}},
6042 {WINED3DFMT_X8D24_UNORM, { 16777215.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
6043 {WINED3DFMT_D32_UNORM, {4294967295.0, 0.0, 0.0, 0.0}, {0, 0, 0, 0}},
6045 enum wined3d_format_id format_id = format->id;
6046 struct wined3d_color colour_srgb;
6047 unsigned int i;
6048 DWORD ret;
6050 TRACE("Converting colour %s to format %s.\n", debug_color(color), debug_d3dformat(format_id));
6052 for (i = 0; i < ARRAY_SIZE(format_srgb_info); ++i)
6054 if (format_id != format_srgb_info[i].srgb_format_id)
6055 continue;
6057 wined3d_colour_srgb_from_linear(&colour_srgb, color);
6058 format_id = format_srgb_info[i].base_format_id;
6059 color = &colour_srgb;
6060 break;
6063 for (i = 0; i < ARRAY_SIZE(float_conv); ++i)
6065 if (format_id != float_conv[i].format_id)
6066 continue;
6068 ret = ((DWORD)((color->r * float_conv[i].mul.x) + 0.5f)) << float_conv[i].shift.x;
6069 ret |= ((DWORD)((color->g * float_conv[i].mul.y) + 0.5f)) << float_conv[i].shift.y;
6070 ret |= ((DWORD)((color->b * float_conv[i].mul.z) + 0.5f)) << float_conv[i].shift.z;
6071 ret |= ((DWORD)((color->a * float_conv[i].mul.w) + 0.5f)) << float_conv[i].shift.w;
6073 TRACE("Returning 0x%08x.\n", ret);
6075 return ret;
6078 for (i = 0; i < ARRAY_SIZE(double_conv); ++i)
6080 if (format_id != double_conv[i].format_id)
6081 continue;
6083 ret = ((DWORD)((color->r * double_conv[i].mul.x) + 0.5)) << double_conv[i].shift.x;
6084 ret |= ((DWORD)((color->g * double_conv[i].mul.y) + 0.5)) << double_conv[i].shift.y;
6085 ret |= ((DWORD)((color->b * double_conv[i].mul.z) + 0.5)) << double_conv[i].shift.z;
6086 ret |= ((DWORD)((color->a * double_conv[i].mul.w) + 0.5)) << double_conv[i].shift.w;
6088 TRACE("Returning 0x%08x.\n", ret);
6090 return ret;
6093 FIXME("Conversion for format %s not implemented.\n", debug_d3dformat(format_id));
6095 return 0;
6098 static float color_to_float(DWORD color, DWORD size, DWORD offset)
6100 uint32_t mask = wined3d_mask_from_size(size);
6102 if (!size)
6103 return 1.0f;
6105 color >>= offset;
6106 color &= mask;
6108 return (float)color / (float)mask;
6111 void wined3d_format_get_float_color_key(const struct wined3d_format *format,
6112 const struct wined3d_color_key *key, struct wined3d_color *float_colors)
6114 struct wined3d_color slop;
6116 switch (format->id)
6118 case WINED3DFMT_B8G8R8_UNORM:
6119 case WINED3DFMT_B8G8R8A8_UNORM:
6120 case WINED3DFMT_B8G8R8X8_UNORM:
6121 case WINED3DFMT_B5G6R5_UNORM:
6122 case WINED3DFMT_B5G5R5X1_UNORM:
6123 case WINED3DFMT_B5G5R5A1_UNORM:
6124 case WINED3DFMT_B4G4R4A4_UNORM:
6125 case WINED3DFMT_B2G3R3_UNORM:
6126 case WINED3DFMT_R8_UNORM:
6127 case WINED3DFMT_A8_UNORM:
6128 case WINED3DFMT_B2G3R3A8_UNORM:
6129 case WINED3DFMT_B4G4R4X4_UNORM:
6130 case WINED3DFMT_R10G10B10A2_UNORM:
6131 case WINED3DFMT_R10G10B10A2_SNORM:
6132 case WINED3DFMT_R8G8B8A8_UNORM:
6133 case WINED3DFMT_R8G8B8X8_UNORM:
6134 case WINED3DFMT_R16G16_UNORM:
6135 case WINED3DFMT_B10G10R10A2_UNORM:
6136 slop.r = 0.5f / wined3d_mask_from_size(format->red_size);
6137 slop.g = 0.5f / wined3d_mask_from_size(format->green_size);
6138 slop.b = 0.5f / wined3d_mask_from_size(format->blue_size);
6139 slop.a = 0.5f / wined3d_mask_from_size(format->alpha_size);
6141 float_colors[0].r = color_to_float(key->color_space_low_value, format->red_size, format->red_offset)
6142 - slop.r;
6143 float_colors[0].g = color_to_float(key->color_space_low_value, format->green_size, format->green_offset)
6144 - slop.g;
6145 float_colors[0].b = color_to_float(key->color_space_low_value, format->blue_size, format->blue_offset)
6146 - slop.b;
6147 float_colors[0].a = color_to_float(key->color_space_low_value, format->alpha_size, format->alpha_offset)
6148 - slop.a;
6150 float_colors[1].r = color_to_float(key->color_space_high_value, format->red_size, format->red_offset)
6151 + slop.r;
6152 float_colors[1].g = color_to_float(key->color_space_high_value, format->green_size, format->green_offset)
6153 + slop.g;
6154 float_colors[1].b = color_to_float(key->color_space_high_value, format->blue_size, format->blue_offset)
6155 + slop.b;
6156 float_colors[1].a = color_to_float(key->color_space_high_value, format->alpha_size, format->alpha_offset)
6157 + slop.a;
6158 break;
6160 case WINED3DFMT_P8_UINT:
6161 float_colors[0].r = 0.0f;
6162 float_colors[0].g = 0.0f;
6163 float_colors[0].b = 0.0f;
6164 float_colors[0].a = (key->color_space_low_value - 0.5f) / 255.0f;
6166 float_colors[1].r = 0.0f;
6167 float_colors[1].g = 0.0f;
6168 float_colors[1].b = 0.0f;
6169 float_colors[1].a = (key->color_space_high_value + 0.5f) / 255.0f;
6170 break;
6172 default:
6173 ERR("Unhandled color key to float conversion for format %s.\n", debug_d3dformat(format->id));
6177 enum wined3d_format_id pixelformat_for_depth(DWORD depth)
6179 switch (depth)
6181 case 8: return WINED3DFMT_P8_UINT;
6182 case 15: return WINED3DFMT_B5G5R5X1_UNORM;
6183 case 16: return WINED3DFMT_B5G6R5_UNORM;
6184 case 24: return WINED3DFMT_B8G8R8X8_UNORM; /* Robots needs 24bit to be WINED3DFMT_B8G8R8X8_UNORM */
6185 case 32: return WINED3DFMT_B8G8R8X8_UNORM; /* EVE online and the Fur demo need 32bit AdapterDisplayMode to return WINED3DFMT_B8G8R8X8_UNORM */
6186 default: return WINED3DFMT_UNKNOWN;
6190 void wined3d_format_copy_data(const struct wined3d_format *format, const uint8_t *src,
6191 unsigned int src_row_pitch, unsigned int src_slice_pitch, uint8_t *dst, unsigned int dst_row_pitch,
6192 unsigned int dst_slice_pitch, unsigned int w, unsigned int h, unsigned int d)
6194 unsigned int row_block_count, row_count, row_size, slice, row;
6195 unsigned int slice_count = d, slice_size;
6196 const uint8_t *src_row;
6197 uint8_t *dst_row;
6199 row_block_count = (w + format->block_width - 1) / format->block_width;
6200 row_count = (h + format->block_height - 1) / format->block_height;
6201 row_size = row_block_count * format->block_byte_count;
6202 slice_size = row_size * row_count;
6204 if (src_row_pitch == row_size && dst_row_pitch == row_size
6205 && ((src_slice_pitch == slice_size && dst_slice_pitch == slice_size) || slice_count == 1))
6207 memcpy(dst, src, slice_count * slice_size);
6208 return;
6211 for (slice = 0; slice < slice_count; ++slice)
6213 for (row = 0; row < row_count; ++row)
6215 src_row = &src[slice * src_slice_pitch + row * src_row_pitch];
6216 dst_row = &dst[slice * dst_slice_pitch + row * dst_row_pitch];
6217 memcpy(dst_row, src_row, row_size);
6222 void multiply_matrix(struct wined3d_matrix *dst, const struct wined3d_matrix *src1, const struct wined3d_matrix *src2)
6224 struct wined3d_matrix tmp;
6226 /* Now do the multiplication 'by hand'.
6227 I know that all this could be optimised, but this will be done later :-) */
6228 tmp._11 = (src1->_11 * src2->_11) + (src1->_21 * src2->_12) + (src1->_31 * src2->_13) + (src1->_41 * src2->_14);
6229 tmp._21 = (src1->_11 * src2->_21) + (src1->_21 * src2->_22) + (src1->_31 * src2->_23) + (src1->_41 * src2->_24);
6230 tmp._31 = (src1->_11 * src2->_31) + (src1->_21 * src2->_32) + (src1->_31 * src2->_33) + (src1->_41 * src2->_34);
6231 tmp._41 = (src1->_11 * src2->_41) + (src1->_21 * src2->_42) + (src1->_31 * src2->_43) + (src1->_41 * src2->_44);
6233 tmp._12 = (src1->_12 * src2->_11) + (src1->_22 * src2->_12) + (src1->_32 * src2->_13) + (src1->_42 * src2->_14);
6234 tmp._22 = (src1->_12 * src2->_21) + (src1->_22 * src2->_22) + (src1->_32 * src2->_23) + (src1->_42 * src2->_24);
6235 tmp._32 = (src1->_12 * src2->_31) + (src1->_22 * src2->_32) + (src1->_32 * src2->_33) + (src1->_42 * src2->_34);
6236 tmp._42 = (src1->_12 * src2->_41) + (src1->_22 * src2->_42) + (src1->_32 * src2->_43) + (src1->_42 * src2->_44);
6238 tmp._13 = (src1->_13 * src2->_11) + (src1->_23 * src2->_12) + (src1->_33 * src2->_13) + (src1->_43 * src2->_14);
6239 tmp._23 = (src1->_13 * src2->_21) + (src1->_23 * src2->_22) + (src1->_33 * src2->_23) + (src1->_43 * src2->_24);
6240 tmp._33 = (src1->_13 * src2->_31) + (src1->_23 * src2->_32) + (src1->_33 * src2->_33) + (src1->_43 * src2->_34);
6241 tmp._43 = (src1->_13 * src2->_41) + (src1->_23 * src2->_42) + (src1->_33 * src2->_43) + (src1->_43 * src2->_44);
6243 tmp._14 = (src1->_14 * src2->_11) + (src1->_24 * src2->_12) + (src1->_34 * src2->_13) + (src1->_44 * src2->_14);
6244 tmp._24 = (src1->_14 * src2->_21) + (src1->_24 * src2->_22) + (src1->_34 * src2->_23) + (src1->_44 * src2->_24);
6245 tmp._34 = (src1->_14 * src2->_31) + (src1->_24 * src2->_32) + (src1->_34 * src2->_33) + (src1->_44 * src2->_34);
6246 tmp._44 = (src1->_14 * src2->_41) + (src1->_24 * src2->_42) + (src1->_34 * src2->_43) + (src1->_44 * src2->_44);
6248 *dst = tmp;
6251 void wined3d_ffp_get_fs_settings(const struct wined3d_context *context, const struct wined3d_state *state,
6252 struct ffp_frag_settings *settings, BOOL ignore_textype)
6254 #define ARG1 0x01
6255 #define ARG2 0x02
6256 #define ARG0 0x04
6257 static const unsigned char args[WINED3D_TOP_LERP + 1] =
6259 /* undefined */ 0,
6260 /* D3DTOP_DISABLE */ 0,
6261 /* D3DTOP_SELECTARG1 */ ARG1,
6262 /* D3DTOP_SELECTARG2 */ ARG2,
6263 /* D3DTOP_MODULATE */ ARG1 | ARG2,
6264 /* D3DTOP_MODULATE2X */ ARG1 | ARG2,
6265 /* D3DTOP_MODULATE4X */ ARG1 | ARG2,
6266 /* D3DTOP_ADD */ ARG1 | ARG2,
6267 /* D3DTOP_ADDSIGNED */ ARG1 | ARG2,
6268 /* D3DTOP_ADDSIGNED2X */ ARG1 | ARG2,
6269 /* D3DTOP_SUBTRACT */ ARG1 | ARG2,
6270 /* D3DTOP_ADDSMOOTH */ ARG1 | ARG2,
6271 /* D3DTOP_BLENDDIFFUSEALPHA */ ARG1 | ARG2,
6272 /* D3DTOP_BLENDTEXTUREALPHA */ ARG1 | ARG2,
6273 /* D3DTOP_BLENDFACTORALPHA */ ARG1 | ARG2,
6274 /* D3DTOP_BLENDTEXTUREALPHAPM */ ARG1 | ARG2,
6275 /* D3DTOP_BLENDCURRENTALPHA */ ARG1 | ARG2,
6276 /* D3DTOP_PREMODULATE */ ARG1 | ARG2,
6277 /* D3DTOP_MODULATEALPHA_ADDCOLOR */ ARG1 | ARG2,
6278 /* D3DTOP_MODULATECOLOR_ADDALPHA */ ARG1 | ARG2,
6279 /* D3DTOP_MODULATEINVALPHA_ADDCOLOR */ ARG1 | ARG2,
6280 /* D3DTOP_MODULATEINVCOLOR_ADDALPHA */ ARG1 | ARG2,
6281 /* D3DTOP_BUMPENVMAP */ ARG1 | ARG2,
6282 /* D3DTOP_BUMPENVMAPLUMINANCE */ ARG1 | ARG2,
6283 /* D3DTOP_DOTPRODUCT3 */ ARG1 | ARG2,
6284 /* D3DTOP_MULTIPLYADD */ ARG1 | ARG2 | ARG0,
6285 /* D3DTOP_LERP */ ARG1 | ARG2 | ARG0
6287 unsigned int i;
6288 DWORD ttff;
6289 DWORD cop, aop, carg0, carg1, carg2, aarg0, aarg1, aarg2;
6290 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6292 settings->padding = 0;
6294 for (i = 0; i < d3d_info->limits.ffp_blend_stages; ++i)
6296 struct wined3d_texture *texture;
6298 settings->op[i].padding = 0;
6299 if (state->texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
6301 settings->op[i].cop = WINED3D_TOP_DISABLE;
6302 settings->op[i].aop = WINED3D_TOP_DISABLE;
6303 settings->op[i].carg0 = settings->op[i].carg1 = settings->op[i].carg2 = ARG_UNUSED;
6304 settings->op[i].aarg0 = settings->op[i].aarg1 = settings->op[i].aarg2 = ARG_UNUSED;
6305 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6306 settings->op[i].tmp_dst = 0;
6307 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6308 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6309 i++;
6310 break;
6313 if ((texture = state->textures[i]))
6315 if (can_use_texture_swizzle(d3d_info, texture->resource.format))
6316 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6317 else
6318 settings->op[i].color_fixup = texture->resource.format->color_fixup;
6319 if (ignore_textype)
6321 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6323 else
6325 switch (wined3d_texture_gl(texture)->target)
6327 case GL_TEXTURE_1D:
6328 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6329 break;
6330 case GL_TEXTURE_2D:
6331 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_2D;
6332 break;
6333 case GL_TEXTURE_3D:
6334 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_3D;
6335 break;
6336 case GL_TEXTURE_CUBE_MAP_ARB:
6337 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
6338 break;
6339 case GL_TEXTURE_RECTANGLE_ARB:
6340 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_RECT;
6341 break;
6344 } else {
6345 settings->op[i].color_fixup = COLOR_FIXUP_IDENTITY;
6346 settings->op[i].tex_type = WINED3D_GL_RES_TYPE_TEX_1D;
6349 cop = state->texture_states[i][WINED3D_TSS_COLOR_OP];
6350 aop = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
6352 carg1 = (args[cop] & ARG1) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG1] : ARG_UNUSED;
6353 carg2 = (args[cop] & ARG2) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG2] : ARG_UNUSED;
6354 carg0 = (args[cop] & ARG0) ? state->texture_states[i][WINED3D_TSS_COLOR_ARG0] : ARG_UNUSED;
6356 if (is_invalid_op(state, i, cop, carg1, carg2, carg0))
6358 carg0 = ARG_UNUSED;
6359 carg2 = ARG_UNUSED;
6360 carg1 = WINED3DTA_CURRENT;
6361 cop = WINED3D_TOP_SELECT_ARG1;
6364 if (cop == WINED3D_TOP_DOTPRODUCT3)
6366 /* A dotproduct3 on the colorop overwrites the alphaop operation and replicates
6367 * the color result to the alpha component of the destination
6369 aop = cop;
6370 aarg1 = carg1;
6371 aarg2 = carg2;
6372 aarg0 = carg0;
6374 else
6376 aarg1 = (args[aop] & ARG1) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] : ARG_UNUSED;
6377 aarg2 = (args[aop] & ARG2) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] : ARG_UNUSED;
6378 aarg0 = (args[aop] & ARG0) ? state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] : ARG_UNUSED;
6381 if (!i && state->textures[0] && state->render_states[WINED3D_RS_COLORKEYENABLE])
6383 GLenum texture_dimensions;
6385 texture = state->textures[0];
6386 texture_dimensions = wined3d_texture_gl(texture)->target;
6388 if (texture_dimensions == GL_TEXTURE_2D || texture_dimensions == GL_TEXTURE_RECTANGLE_ARB)
6390 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT && !texture->resource.format->alpha_size)
6392 if (aop == WINED3D_TOP_DISABLE)
6394 aarg1 = WINED3DTA_TEXTURE;
6395 aop = WINED3D_TOP_SELECT_ARG1;
6397 else if (aop == WINED3D_TOP_SELECT_ARG1 && aarg1 != WINED3DTA_TEXTURE)
6399 if (state->blend_state && state->blend_state->desc.rt[0].enable)
6401 aarg2 = WINED3DTA_TEXTURE;
6402 aop = WINED3D_TOP_MODULATE;
6404 else aarg1 = WINED3DTA_TEXTURE;
6406 else if (aop == WINED3D_TOP_SELECT_ARG2 && aarg2 != WINED3DTA_TEXTURE)
6408 if (state->blend_state && state->blend_state->desc.rt[0].enable)
6410 aarg1 = WINED3DTA_TEXTURE;
6411 aop = WINED3D_TOP_MODULATE;
6413 else aarg2 = WINED3DTA_TEXTURE;
6419 if (is_invalid_op(state, i, aop, aarg1, aarg2, aarg0))
6421 aarg0 = ARG_UNUSED;
6422 aarg2 = ARG_UNUSED;
6423 aarg1 = WINED3DTA_CURRENT;
6424 aop = WINED3D_TOP_SELECT_ARG1;
6427 if (carg1 == WINED3DTA_TEXTURE || carg2 == WINED3DTA_TEXTURE || carg0 == WINED3DTA_TEXTURE
6428 || aarg1 == WINED3DTA_TEXTURE || aarg2 == WINED3DTA_TEXTURE || aarg0 == WINED3DTA_TEXTURE)
6430 ttff = state->texture_states[i][WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS];
6431 if (ttff == (WINED3D_TTFF_PROJECTED | WINED3D_TTFF_COUNT3))
6432 settings->op[i].projected = WINED3D_PROJECTION_COUNT3;
6433 else if (ttff & WINED3D_TTFF_PROJECTED)
6434 settings->op[i].projected = WINED3D_PROJECTION_COUNT4;
6435 else
6436 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6438 else
6440 settings->op[i].projected = WINED3D_PROJECTION_NONE;
6443 settings->op[i].cop = cop;
6444 settings->op[i].aop = aop;
6445 settings->op[i].carg0 = carg0;
6446 settings->op[i].carg1 = carg1;
6447 settings->op[i].carg2 = carg2;
6448 settings->op[i].aarg0 = aarg0;
6449 settings->op[i].aarg1 = aarg1;
6450 settings->op[i].aarg2 = aarg2;
6451 settings->op[i].tmp_dst = state->texture_states[i][WINED3D_TSS_RESULT_ARG] == WINED3DTA_TEMP;
6454 /* Clear unsupported stages */
6455 for(; i < WINED3D_MAX_TEXTURES; i++) {
6456 memset(&settings->op[i], 0xff, sizeof(settings->op[i]));
6459 if (!state->render_states[WINED3D_RS_FOGENABLE])
6461 settings->fog = WINED3D_FFP_PS_FOG_OFF;
6463 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
6465 if (use_vs(state) || state->vertex_declaration->position_transformed)
6467 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6469 else
6471 switch (state->render_states[WINED3D_RS_FOGVERTEXMODE])
6473 case WINED3D_FOG_NONE:
6474 case WINED3D_FOG_LINEAR:
6475 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6476 break;
6477 case WINED3D_FOG_EXP:
6478 settings->fog = WINED3D_FFP_PS_FOG_EXP;
6479 break;
6480 case WINED3D_FOG_EXP2:
6481 settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6482 break;
6486 else
6488 switch (state->render_states[WINED3D_RS_FOGTABLEMODE])
6490 case WINED3D_FOG_LINEAR:
6491 settings->fog = WINED3D_FFP_PS_FOG_LINEAR;
6492 break;
6493 case WINED3D_FOG_EXP:
6494 settings->fog = WINED3D_FFP_PS_FOG_EXP;
6495 break;
6496 case WINED3D_FOG_EXP2:
6497 settings->fog = WINED3D_FFP_PS_FOG_EXP2;
6498 break;
6501 settings->sRGB_write = !d3d_info->srgb_write_control && needs_srgb_write(d3d_info, state, &state->fb);
6502 if (d3d_info->vs_clipping || !use_vs(state) || !state->render_states[WINED3D_RS_CLIPPING]
6503 || !state->render_states[WINED3D_RS_CLIPPLANEENABLE])
6505 /* No need to emulate clipplanes if GL supports native vertex shader clipping or if
6506 * the fixed function vertex pipeline is used(which always supports clipplanes), or
6507 * if no clipplane is enabled
6509 settings->emul_clipplanes = 0;
6510 } else {
6511 settings->emul_clipplanes = 1;
6514 if (state->render_states[WINED3D_RS_COLORKEYENABLE] && state->textures[0]
6515 && state->textures[0]->async.color_key_flags & WINED3D_CKEY_SRC_BLT
6516 && settings->op[0].cop != WINED3D_TOP_DISABLE)
6517 settings->color_key_enabled = 1;
6518 else
6519 settings->color_key_enabled = 0;
6521 /* texcoords_initialized is set to meaningful values only when GL doesn't
6522 * support enough varyings to always pass around all the possible texture
6523 * coordinates.
6524 * This is used to avoid reading a varying not written by the vertex shader.
6525 * Reading uninitialized varyings on core profile contexts results in an
6526 * error while with builtin varyings on legacy contexts you get undefined
6527 * behavior. */
6528 if (d3d_info->limits.varying_count && !d3d_info->full_ffp_varyings)
6530 settings->texcoords_initialized = 0;
6531 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6533 if (use_vs(state))
6535 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.output_registers & (1u << i))
6536 settings->texcoords_initialized |= 1u << i;
6538 else
6540 const struct wined3d_stream_info *si = &context->stream_info;
6541 unsigned int coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6542 if ((state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX] >> WINED3D_FFP_TCI_SHIFT)
6543 & WINED3D_FFP_TCI_MASK
6544 || (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))))
6545 settings->texcoords_initialized |= 1u << i;
6549 else
6551 settings->texcoords_initialized = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6554 settings->pointsprite = state->render_states[WINED3D_RS_POINTSPRITEENABLE]
6555 && state->primitive_type == WINED3D_PT_POINTLIST;
6557 if (d3d_info->ffp_alpha_test)
6558 settings->alpha_test_func = WINED3D_CMP_ALWAYS - 1;
6559 else
6560 settings->alpha_test_func = (state->render_states[WINED3D_RS_ALPHATESTENABLE]
6561 ? wined3d_sanitize_cmp_func(state->render_states[WINED3D_RS_ALPHAFUNC])
6562 : WINED3D_CMP_ALWAYS) - 1;
6564 if (d3d_info->emulated_flatshading)
6565 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6566 else
6567 settings->flatshading = FALSE;
6570 const struct ffp_frag_desc *find_ffp_frag_shader(const struct wine_rb_tree *fragment_shaders,
6571 const struct ffp_frag_settings *settings)
6573 struct wine_rb_entry *entry = wine_rb_get(fragment_shaders, settings);
6574 return entry ? WINE_RB_ENTRY_VALUE(entry, struct ffp_frag_desc, entry) : NULL;
6577 void add_ffp_frag_shader(struct wine_rb_tree *shaders, struct ffp_frag_desc *desc)
6579 /* Note that the key is the implementation independent part of the ffp_frag_desc structure,
6580 * whereas desc points to an extended structure with implementation specific parts. */
6581 if (wine_rb_put(shaders, &desc->settings, &desc->entry) == -1)
6583 ERR("Failed to insert ffp frag shader.\n");
6587 /* Activates the texture dimension according to the bound D3D texture. Does
6588 * not care for the colorop or correct gl texture unit (when using nvrc).
6589 * Requires the caller to activate the correct unit. */
6590 /* Context activation is done by the caller (state handler). */
6591 void texture_activate_dimensions(struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
6593 if (texture)
6595 switch (wined3d_texture_gl(texture)->target)
6597 case GL_TEXTURE_2D:
6598 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6599 checkGLcall("glDisable(GL_TEXTURE_3D)");
6600 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6602 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6603 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6605 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6607 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6608 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6610 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
6611 checkGLcall("glEnable(GL_TEXTURE_2D)");
6612 break;
6613 case GL_TEXTURE_RECTANGLE_ARB:
6614 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6615 checkGLcall("glDisable(GL_TEXTURE_2D)");
6616 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6617 checkGLcall("glDisable(GL_TEXTURE_3D)");
6618 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6620 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6621 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6623 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_RECTANGLE_ARB);
6624 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
6625 break;
6626 case GL_TEXTURE_3D:
6627 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6629 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6630 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6632 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6634 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6635 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6637 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6638 checkGLcall("glDisable(GL_TEXTURE_2D)");
6639 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_3D);
6640 checkGLcall("glEnable(GL_TEXTURE_3D)");
6641 break;
6642 case GL_TEXTURE_CUBE_MAP_ARB:
6643 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6644 checkGLcall("glDisable(GL_TEXTURE_2D)");
6645 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6646 checkGLcall("glDisable(GL_TEXTURE_3D)");
6647 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6649 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6650 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6652 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_ARB);
6653 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
6654 break;
6657 else
6659 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
6660 checkGLcall("glEnable(GL_TEXTURE_2D)");
6661 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
6662 checkGLcall("glDisable(GL_TEXTURE_3D)");
6663 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6665 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6666 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6668 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6670 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6671 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6673 /* Binding textures is done by samplers. A dummy texture will be bound */
6677 /* Context activation is done by the caller (state handler). */
6678 void sampler_texdim(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
6680 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6681 unsigned int sampler = state_id - STATE_SAMPLER(0);
6682 unsigned int mapped_stage;
6684 /* No need to enable / disable anything here for unused samplers. The
6685 * tex_colorop handler takes care. Also no action is needed with pixel
6686 * shaders, or if tex_colorop will take care of this business. */
6687 mapped_stage = context_gl->tex_unit_map[sampler];
6688 if (mapped_stage == WINED3D_UNMAPPED_STAGE || mapped_stage >= context_gl->gl_info->limits.textures)
6689 return;
6690 if (sampler >= context->lowest_disabled_stage)
6691 return;
6692 if (isStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP)))
6693 return;
6695 texture_activate_dimensions(state->textures[sampler], context_gl->gl_info);
6698 int wined3d_ffp_frag_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6700 const struct ffp_frag_settings *ka = key;
6701 const struct ffp_frag_settings *kb = &WINE_RB_ENTRY_VALUE(entry, const struct ffp_frag_desc, entry)->settings;
6703 return memcmp(ka, kb, sizeof(*ka));
6706 void wined3d_ffp_get_vs_settings(const struct wined3d_context *context,
6707 const struct wined3d_state *state, struct wined3d_ffp_vs_settings *settings)
6709 enum wined3d_material_color_source diffuse_source, emissive_source, ambient_source, specular_source;
6710 const struct wined3d_stream_info *si = &context->stream_info;
6711 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6712 unsigned int coord_idx, i;
6714 memset(settings, 0, sizeof(*settings));
6716 if (si->position_transformed)
6718 settings->transformed = 1;
6719 settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6720 settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6721 if (!state->render_states[WINED3D_RS_FOGENABLE])
6722 settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
6723 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6724 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6725 else
6726 settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;
6728 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6730 coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6731 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6732 settings->texcoords |= 1u << i;
6733 settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6735 if (d3d_info->full_ffp_varyings)
6736 settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6738 if (d3d_info->emulated_flatshading)
6739 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6740 else
6741 settings->flatshading = FALSE;
6743 settings->swizzle_map = si->swizzle_map;
6745 return;
6748 switch (state->render_states[WINED3D_RS_VERTEXBLEND])
6750 case WINED3D_VBF_DISABLE:
6751 case WINED3D_VBF_1WEIGHTS:
6752 case WINED3D_VBF_2WEIGHTS:
6753 case WINED3D_VBF_3WEIGHTS:
6754 settings->vertexblends = state->render_states[WINED3D_RS_VERTEXBLEND];
6755 break;
6756 default:
6757 FIXME("Unsupported vertex blending: %d\n", state->render_states[WINED3D_RS_VERTEXBLEND]);
6758 break;
6761 settings->clipping = state->render_states[WINED3D_RS_CLIPPING]
6762 && state->render_states[WINED3D_RS_CLIPPLANEENABLE];
6763 settings->normal = !!(si->use_map & (1u << WINED3D_FFP_NORMAL));
6764 settings->normalize = settings->normal && state->render_states[WINED3D_RS_NORMALIZENORMALS];
6765 settings->lighting = !!state->render_states[WINED3D_RS_LIGHTING];
6766 settings->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
6767 settings->point_size = state->primitive_type == WINED3D_PT_POINTLIST;
6768 settings->per_vertex_point_size = !!(si->use_map & 1u << WINED3D_FFP_PSIZE);
6770 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
6771 &ambient_source, &specular_source, state, si);
6772 settings->diffuse_source = diffuse_source;
6773 settings->emissive_source = emissive_source;
6774 settings->ambient_source = ambient_source;
6775 settings->specular_source = specular_source;
6777 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
6779 coord_idx = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6780 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
6781 settings->texcoords |= 1u << i;
6782 settings->texgen[i] = state->texture_states[i][WINED3D_TSS_TEXCOORD_INDEX];
6784 if (d3d_info->full_ffp_varyings)
6785 settings->texcoords = wined3d_mask_from_size(WINED3D_MAX_TEXTURES);
6787 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
6789 if (!state->light_state.lights[i])
6790 continue;
6792 switch (state->light_state.lights[i]->OriginalParms.type)
6794 case WINED3D_LIGHT_POINT:
6795 ++settings->point_light_count;
6796 break;
6797 case WINED3D_LIGHT_SPOT:
6798 ++settings->spot_light_count;
6799 break;
6800 case WINED3D_LIGHT_DIRECTIONAL:
6801 ++settings->directional_light_count;
6802 break;
6803 case WINED3D_LIGHT_PARALLELPOINT:
6804 ++settings->parallel_point_light_count;
6805 break;
6806 default:
6807 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
6808 break;
6812 if (!state->render_states[WINED3D_RS_FOGENABLE])
6813 settings->fog_mode = WINED3D_FFP_VS_FOG_OFF;
6814 else if (state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6816 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6818 if (state->transforms[WINED3D_TS_PROJECTION]._14 == 0.0f
6819 && state->transforms[WINED3D_TS_PROJECTION]._24 == 0.0f
6820 && state->transforms[WINED3D_TS_PROJECTION]._34 == 0.0f
6821 && state->transforms[WINED3D_TS_PROJECTION]._44 == 1.0f)
6822 settings->ortho_fog = 1;
6824 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE)
6825 settings->fog_mode = WINED3D_FFP_VS_FOG_FOGCOORD;
6826 else if (state->render_states[WINED3D_RS_RANGEFOGENABLE])
6827 settings->fog_mode = WINED3D_FFP_VS_FOG_RANGE;
6828 else
6829 settings->fog_mode = WINED3D_FFP_VS_FOG_DEPTH;
6831 if (d3d_info->emulated_flatshading)
6832 settings->flatshading = state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT;
6833 else
6834 settings->flatshading = FALSE;
6836 settings->swizzle_map = si->swizzle_map;
6839 int wined3d_ffp_vertex_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6841 const struct wined3d_ffp_vs_settings *ka = key;
6842 const struct wined3d_ffp_vs_settings *kb = &WINE_RB_ENTRY_VALUE(entry,
6843 const struct wined3d_ffp_vs_desc, entry)->settings;
6845 return memcmp(ka, kb, sizeof(*ka));
6848 const char *wined3d_debug_location(DWORD location)
6850 struct debug_buffer buffer;
6851 const char *prefix = "";
6852 const char *suffix = "";
6854 if (wined3d_popcount(location) > 16)
6856 prefix = "~(";
6857 location = ~location;
6858 suffix = ")";
6861 init_debug_buffer(&buffer, "0");
6862 #define LOCATION_TO_STR(x) if (location & x) { debug_append(&buffer, #x, " | "); location &= ~x; }
6863 LOCATION_TO_STR(WINED3D_LOCATION_DISCARDED);
6864 LOCATION_TO_STR(WINED3D_LOCATION_SYSMEM);
6865 LOCATION_TO_STR(WINED3D_LOCATION_CLEARED);
6866 LOCATION_TO_STR(WINED3D_LOCATION_BUFFER);
6867 LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_RGB);
6868 LOCATION_TO_STR(WINED3D_LOCATION_TEXTURE_SRGB);
6869 LOCATION_TO_STR(WINED3D_LOCATION_DRAWABLE);
6870 LOCATION_TO_STR(WINED3D_LOCATION_RB_MULTISAMPLE);
6871 LOCATION_TO_STR(WINED3D_LOCATION_RB_RESOLVED);
6872 #undef LOCATION_TO_STR
6873 if (location)
6874 FIXME("Unrecognized location flag(s) %#x.\n", location);
6876 return wine_dbg_sprintf("%s%s%s", prefix, buffer.str, suffix);
6879 const char *wined3d_debug_feature_level(enum wined3d_feature_level level)
6881 switch (level)
6883 #define LEVEL_TO_STR(level) case level: return #level
6884 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_5);
6885 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_6);
6886 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_7);
6887 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_8);
6888 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_1);
6889 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_2);
6890 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_9_3);
6891 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10);
6892 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_10_1);
6893 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11);
6894 LEVEL_TO_STR(WINED3D_FEATURE_LEVEL_11_1);
6895 #undef LEVEL_TO_STR
6896 default:
6897 return wine_dbg_sprintf("%#x", level);
6901 /* Print a floating point value with the %.8e format specifier, always using
6902 * '.' as decimal separator. */
6903 void wined3d_ftoa(float value, char *s)
6905 int idx = 1;
6907 if (copysignf(1.0f, value) < 0.0f)
6908 ++idx;
6910 /* Be sure to allocate a buffer of at least 17 characters for the result
6911 as sprintf may return a 3 digit exponent when using the MSVC runtime
6912 instead of a 2 digit exponent. */
6913 sprintf(s, "%.8e", value);
6914 if (isfinite(value))
6915 s[idx] = '.';
6918 void wined3d_release_dc(HWND window, HDC dc)
6920 /* You'd figure ReleaseDC() would fail if the DC doesn't match the window.
6921 * However, that's not what actually happens, and there are user32 tests
6922 * that confirm ReleaseDC() with the wrong window is supposed to succeed.
6923 * So explicitly check that the DC belongs to the window, since we want to
6924 * avoid releasing a DC that belongs to some other window if the original
6925 * window was already destroyed. */
6926 if (WindowFromDC(dc) != window)
6927 WARN("DC %p does not belong to window %p.\n", dc, window);
6928 else if (!ReleaseDC(window, dc))
6929 ERR("Failed to release device context %p, last error %#x.\n", dc, GetLastError());
6932 BOOL wined3d_clip_blit(const RECT *clip_rect, RECT *clipped, RECT *other)
6934 RECT orig = *clipped;
6935 float scale_x = (float)(orig.right - orig.left) / (float)(other->right - other->left);
6936 float scale_y = (float)(orig.bottom - orig.top) / (float)(other->bottom - other->top);
6938 IntersectRect(clipped, clipped, clip_rect);
6940 if (IsRectEmpty(clipped))
6942 SetRectEmpty(other);
6943 return FALSE;
6946 other->left += (LONG)((clipped->left - orig.left) / scale_x);
6947 other->top += (LONG)((clipped->top - orig.top) / scale_y);
6948 other->right -= (LONG)((orig.right - clipped->right) / scale_x);
6949 other->bottom -= (LONG)((orig.bottom - clipped->bottom) / scale_y);
6951 return TRUE;
6954 void wined3d_gl_limits_get_uniform_block_range(const struct wined3d_gl_limits *gl_limits,
6955 enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
6957 unsigned int i;
6959 *base = 0;
6960 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
6962 *count = gl_limits->uniform_blocks[i];
6963 if (i == shader_type)
6964 return;
6965 *base += *count;
6968 ERR("Unrecognized shader type %#x.\n", shader_type);
6969 *count = 0;
6972 void wined3d_gl_limits_get_texture_unit_range(const struct wined3d_gl_limits *gl_limits,
6973 enum wined3d_shader_type shader_type, unsigned int *base, unsigned int *count)
6975 unsigned int i;
6977 if (shader_type == WINED3D_SHADER_TYPE_COMPUTE)
6979 if (gl_limits->combined_samplers == gl_limits->graphics_samplers)
6980 *base = 0;
6981 else
6982 *base = gl_limits->graphics_samplers;
6983 *count = gl_limits->samplers[WINED3D_SHADER_TYPE_COMPUTE];
6984 return;
6987 *base = 0;
6988 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
6990 *count = gl_limits->samplers[i];
6991 if (i == shader_type)
6992 return;
6993 *base += *count;
6996 ERR("Unrecognized shader type %#x.\n", shader_type);
6997 *count = 0;
7000 BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE_T size)
7002 SIZE_T max_capacity, new_capacity;
7003 void *new_elements;
7005 if (count <= *capacity)
7006 return TRUE;
7008 max_capacity = ~(SIZE_T)0 / size;
7009 if (count > max_capacity)
7010 return FALSE;
7012 new_capacity = max(1, *capacity);
7013 while (new_capacity < count && new_capacity <= max_capacity / 2)
7014 new_capacity *= 2;
7015 if (new_capacity < count)
7016 new_capacity = count;
7018 if (!*elements)
7019 new_elements = heap_alloc_zero(new_capacity * size);
7020 else
7021 new_elements = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *elements, new_capacity * size);
7022 if (!new_elements)
7023 return FALSE;
7025 *elements = new_elements;
7026 *capacity = new_capacity;
7027 return TRUE;
7030 static void swap_rows(float **a, float **b)
7032 float *tmp = *a;
7034 *a = *b;
7035 *b = tmp;
7038 BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
7040 float wtmp[4][8];
7041 float m0, m1, m2, m3, s;
7042 float *r0, *r1, *r2, *r3;
7044 r0 = wtmp[0];
7045 r1 = wtmp[1];
7046 r2 = wtmp[2];
7047 r3 = wtmp[3];
7049 r0[0] = m->_11;
7050 r0[1] = m->_12;
7051 r0[2] = m->_13;
7052 r0[3] = m->_14;
7053 r0[4] = 1.0f;
7054 r0[5] = r0[6] = r0[7] = 0.0f;
7056 r1[0] = m->_21;
7057 r1[1] = m->_22;
7058 r1[2] = m->_23;
7059 r1[3] = m->_24;
7060 r1[5] = 1.0f;
7061 r1[4] = r1[6] = r1[7] = 0.0f;
7063 r2[0] = m->_31;
7064 r2[1] = m->_32;
7065 r2[2] = m->_33;
7066 r2[3] = m->_34;
7067 r2[6] = 1.0f;
7068 r2[4] = r2[5] = r2[7] = 0.0f;
7070 r3[0] = m->_41;
7071 r3[1] = m->_42;
7072 r3[2] = m->_43;
7073 r3[3] = m->_44;
7074 r3[7] = 1.0f;
7075 r3[4] = r3[5] = r3[6] = 0.0f;
7077 /* Choose pivot - or die. */
7078 if (fabsf(r3[0]) > fabsf(r2[0]))
7079 swap_rows(&r3, &r2);
7080 if (fabsf(r2[0]) > fabsf(r1[0]))
7081 swap_rows(&r2, &r1);
7082 if (fabsf(r1[0]) > fabsf(r0[0]))
7083 swap_rows(&r1, &r0);
7084 if (r0[0] == 0.0f)
7085 return FALSE;
7087 /* Eliminate first variable. */
7088 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
7089 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
7090 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
7091 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
7092 s = r0[4];
7093 if (s != 0.0f)
7095 r1[4] -= m1 * s;
7096 r2[4] -= m2 * s;
7097 r3[4] -= m3 * s;
7099 s = r0[5];
7100 if (s != 0.0f)
7102 r1[5] -= m1 * s;
7103 r2[5] -= m2 * s;
7104 r3[5] -= m3 * s;
7106 s = r0[6];
7107 if (s != 0.0f)
7109 r1[6] -= m1 * s;
7110 r2[6] -= m2 * s;
7111 r3[6] -= m3 * s;
7113 s = r0[7];
7114 if (s != 0.0f)
7116 r1[7] -= m1 * s;
7117 r2[7] -= m2 * s;
7118 r3[7] -= m3 * s;
7121 /* Choose pivot - or die. */
7122 if (fabsf(r3[1]) > fabsf(r2[1]))
7123 swap_rows(&r3, &r2);
7124 if (fabsf(r2[1]) > fabsf(r1[1]))
7125 swap_rows(&r2, &r1);
7126 if (r1[1] == 0.0f)
7127 return FALSE;
7129 /* Eliminate second variable. */
7130 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
7131 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
7132 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
7133 s = r1[4];
7134 if (s != 0.0f)
7136 r2[4] -= m2 * s;
7137 r3[4] -= m3 * s;
7139 s = r1[5];
7140 if (s != 0.0f)
7142 r2[5] -= m2 * s;
7143 r3[5] -= m3 * s;
7145 s = r1[6];
7146 if (s != 0.0f)
7148 r2[6] -= m2 * s;
7149 r3[6] -= m3 * s;
7151 s = r1[7];
7152 if (s != 0.0f)
7154 r2[7] -= m2 * s;
7155 r3[7] -= m3 * s;
7158 /* Choose pivot - or die. */
7159 if (fabsf(r3[2]) > fabsf(r2[2]))
7160 swap_rows(&r3, &r2);
7161 if (r2[2] == 0.0f)
7162 return FALSE;
7164 /* Eliminate third variable. */
7165 m3 = r3[2] / r2[2];
7166 r3[3] -= m3 * r2[3];
7167 r3[4] -= m3 * r2[4];
7168 r3[5] -= m3 * r2[5];
7169 r3[6] -= m3 * r2[6];
7170 r3[7] -= m3 * r2[7];
7172 /* Last check. */
7173 if (r3[3] == 0.0f)
7174 return FALSE;
7176 /* Back substitute row 3. */
7177 s = 1.0f / r3[3];
7178 r3[4] *= s;
7179 r3[5] *= s;
7180 r3[6] *= s;
7181 r3[7] *= s;
7183 /* Back substitute row 2. */
7184 m2 = r2[3];
7185 s = 1.0f / r2[2];
7186 r2[4] = s * (r2[4] - r3[4] * m2);
7187 r2[5] = s * (r2[5] - r3[5] * m2);
7188 r2[6] = s * (r2[6] - r3[6] * m2);
7189 r2[7] = s * (r2[7] - r3[7] * m2);
7190 m1 = r1[3];
7191 r1[4] -= r3[4] * m1;
7192 r1[5] -= r3[5] * m1;
7193 r1[6] -= r3[6] * m1;
7194 r1[7] -= r3[7] * m1;
7195 m0 = r0[3];
7196 r0[4] -= r3[4] * m0;
7197 r0[5] -= r3[5] * m0;
7198 r0[6] -= r3[6] * m0;
7199 r0[7] -= r3[7] * m0;
7201 /* Back substitute row 1. */
7202 m1 = r1[2];
7203 s = 1.0f / r1[1];
7204 r1[4] = s * (r1[4] - r2[4] * m1);
7205 r1[5] = s * (r1[5] - r2[5] * m1);
7206 r1[6] = s * (r1[6] - r2[6] * m1);
7207 r1[7] = s * (r1[7] - r2[7] * m1);
7208 m0 = r0[2];
7209 r0[4] -= r2[4] * m0;
7210 r0[5] -= r2[5] * m0;
7211 r0[6] -= r2[6] * m0;
7212 r0[7] -= r2[7] * m0;
7214 /* Back substitute row 0. */
7215 m0 = r0[1];
7216 s = 1.0f / r0[0];
7217 r0[4] = s * (r0[4] - r1[4] * m0);
7218 r0[5] = s * (r0[5] - r1[5] * m0);
7219 r0[6] = s * (r0[6] - r1[6] * m0);
7220 r0[7] = s * (r0[7] - r1[7] * m0);
7222 out->_11 = r0[4];
7223 out->_12 = r0[5];
7224 out->_13 = r0[6];
7225 out->_14 = r0[7];
7226 out->_21 = r1[4];
7227 out->_22 = r1[5];
7228 out->_23 = r1[6];
7229 out->_24 = r1[7];
7230 out->_31 = r2[4];
7231 out->_32 = r2[5];
7232 out->_33 = r2[6];
7233 out->_34 = r2[7];
7234 out->_41 = r3[4];
7235 out->_42 = r3[5];
7236 out->_43 = r3[6];
7237 out->_44 = r3[7];
7239 return TRUE;
7242 /* Taken and adapted from Mesa. */
7243 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
7245 float pos, neg, t, det;
7246 struct wined3d_matrix temp;
7248 /* Calculate the determinant of upper left 3x3 submatrix and
7249 * determine if the matrix is singular. */
7250 pos = neg = 0.0f;
7251 t = in->_11 * in->_22 * in->_33;
7252 if (t >= 0.0f)
7253 pos += t;
7254 else
7255 neg += t;
7257 t = in->_21 * in->_32 * in->_13;
7258 if (t >= 0.0f)
7259 pos += t;
7260 else
7261 neg += t;
7262 t = in->_31 * in->_12 * in->_23;
7263 if (t >= 0.0f)
7264 pos += t;
7265 else
7266 neg += t;
7268 t = -in->_31 * in->_22 * in->_13;
7269 if (t >= 0.0f)
7270 pos += t;
7271 else
7272 neg += t;
7273 t = -in->_21 * in->_12 * in->_33;
7274 if (t >= 0.0f)
7275 pos += t;
7276 else
7277 neg += t;
7279 t = -in->_11 * in->_32 * in->_23;
7280 if (t >= 0.0f)
7281 pos += t;
7282 else
7283 neg += t;
7285 det = pos + neg;
7287 if (fabsf(det) < 1e-25f)
7288 return FALSE;
7290 det = 1.0f / det;
7291 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
7292 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
7293 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
7294 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
7295 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
7296 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
7297 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
7298 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
7299 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
7301 *out = temp;
7302 return TRUE;
7305 void compute_normal_matrix(float *normal_matrix, BOOL legacy_lighting,
7306 const struct wined3d_matrix *modelview)
7308 struct wined3d_matrix mv;
7309 unsigned int i, j;
7311 mv = *modelview;
7312 if (legacy_lighting)
7313 invert_matrix_3d(&mv, &mv);
7314 else
7315 invert_matrix(&mv, &mv);
7316 /* Tests show that singular modelview matrices are used unchanged as normal
7317 * matrices on D3D3 and older. There seems to be no clearly consistent
7318 * behavior on newer D3D versions so always follow older ddraw behavior. */
7319 for (i = 0; i < 3; ++i)
7320 for (j = 0; j < 3; ++j)
7321 normal_matrix[i * 3 + j] = (&mv._11)[j * 4 + i];
7324 static void wined3d_allocator_release_block(struct wined3d_allocator *allocator,
7325 struct wined3d_allocator_block *block)
7327 block->parent = allocator->free;
7328 allocator->free = block;
7331 static struct wined3d_allocator_block *wined3d_allocator_acquire_block(struct wined3d_allocator *allocator)
7333 struct wined3d_allocator_block *block;
7335 if (!allocator->free)
7336 return heap_alloc(sizeof(*block));
7338 block = allocator->free;
7339 allocator->free = block->parent;
7341 return block;
7344 void wined3d_allocator_block_free(struct wined3d_allocator_block *block)
7346 struct wined3d_allocator *allocator = block->chunk->allocator;
7347 struct wined3d_allocator_block *parent;
7349 while ((parent = block->parent) && block->sibling->free)
7351 list_remove(&block->sibling->entry);
7352 wined3d_allocator_release_block(allocator, block->sibling);
7353 wined3d_allocator_release_block(allocator, block);
7354 block = parent;
7357 block->free = true;
7358 list_add_head(&block->chunk->available[block->order], &block->entry);
7361 static void wined3d_allocator_block_init(struct wined3d_allocator_block *block,
7362 struct wined3d_allocator_chunk *chunk, struct wined3d_allocator_block *parent,
7363 struct wined3d_allocator_block *sibling, unsigned int order, size_t offset, bool free)
7365 list_init(&block->entry);
7366 block->chunk = chunk;
7367 block->parent = parent;
7368 block->sibling = sibling;
7369 block->order = order;
7370 block->offset = offset;
7371 block->free = free;
7374 void wined3d_allocator_chunk_cleanup(struct wined3d_allocator_chunk *chunk)
7376 struct wined3d_allocator_block *block;
7377 size_t i;
7379 if (list_empty(&chunk->available[0]))
7381 ERR("Chunk %p is not empty.\n", chunk);
7382 return;
7385 for (i = 1; i < ARRAY_SIZE(chunk->available); ++i)
7387 if (!list_empty(&chunk->available[i]))
7389 ERR("Chunk %p is not empty.\n", chunk);
7390 return;
7394 block = LIST_ENTRY(list_head(&chunk->available[0]), struct wined3d_allocator_block, entry);
7395 wined3d_allocator_release_block(chunk->allocator, block);
7398 bool wined3d_allocator_chunk_init(struct wined3d_allocator_chunk *chunk, struct wined3d_allocator *allocator)
7400 struct wined3d_allocator_block *block;
7401 unsigned int i;
7403 if (!(block = wined3d_allocator_acquire_block(allocator)))
7404 return false;
7405 wined3d_allocator_block_init(block, chunk, NULL, NULL, 0, 0, true);
7407 list_init(&chunk->entry);
7408 for (i = 0; i < ARRAY_SIZE(chunk->available); ++i)
7410 list_init(&chunk->available[i]);
7412 list_add_head(&chunk->available[0], &block->entry);
7413 chunk->allocator = allocator;
7414 chunk->map_count = 0;
7415 chunk->map_ptr = NULL;
7417 return true;
7420 void wined3d_allocator_cleanup(struct wined3d_allocator *allocator)
7422 struct wined3d_allocator_chunk *chunk, *chunk2;
7423 struct wined3d_allocator_block *block, *next;
7424 size_t i;
7426 for (i = 0; i < allocator->pool_count; ++i)
7428 LIST_FOR_EACH_ENTRY_SAFE(chunk, chunk2, &allocator->pools[i].chunks, struct wined3d_allocator_chunk, entry)
7430 list_remove(&chunk->entry);
7431 allocator->ops->allocator_destroy_chunk(chunk);
7434 heap_free(allocator->pools);
7436 next = allocator->free;
7437 while ((block = next))
7439 next = block->parent;
7440 heap_free(block);
7444 static struct wined3d_allocator_block *wined3d_allocator_chunk_allocate(struct wined3d_allocator_chunk *chunk,
7445 unsigned int order)
7447 struct wined3d_allocator_block *block, *left, *right;
7448 unsigned int i = order;
7450 while (i)
7452 if (!list_empty(&chunk->available[i]))
7453 break;
7454 --i;
7457 if (list_empty(&chunk->available[i]))
7458 return NULL;
7460 block = LIST_ENTRY(list_head(&chunk->available[i]), struct wined3d_allocator_block, entry);
7461 list_remove(&block->entry);
7462 block->free = false;
7464 while (i < order)
7466 if (!(left = wined3d_allocator_acquire_block(chunk->allocator)))
7468 ERR("Failed to allocate left.\n");
7469 break;
7472 if (!(right = wined3d_allocator_acquire_block(chunk->allocator)))
7474 ERR("Failed to allocate right.\n");
7475 wined3d_allocator_release_block(chunk->allocator, left);
7476 break;
7479 wined3d_allocator_block_init(left, chunk, block, right, block->order + 1, block->offset, false);
7480 wined3d_allocator_block_init(right, chunk, block, left, block->order + 1,
7481 block->offset + (WINED3D_ALLOCATOR_CHUNK_SIZE >> left->order), true);
7482 list_add_head(&chunk->available[right->order], &right->entry);
7484 block = left;
7485 ++i;
7488 return block;
7491 struct wined3d_allocator_block *wined3d_allocator_allocate(struct wined3d_allocator *allocator,
7492 struct wined3d_context *context, unsigned int memory_type, size_t size)
7494 struct wined3d_allocator_chunk *chunk;
7495 struct wined3d_allocator_block *block;
7496 unsigned int order;
7498 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
7499 return NULL;
7501 if (size < WINED3D_ALLOCATOR_MIN_BLOCK_SIZE)
7502 order = WINED3D_ALLOCATOR_CHUNK_ORDER_COUNT - 1;
7503 else
7504 order = wined3d_log2i(WINED3D_ALLOCATOR_CHUNK_SIZE / size);
7506 LIST_FOR_EACH_ENTRY(chunk, &allocator->pools[memory_type].chunks, struct wined3d_allocator_chunk, entry)
7508 if ((block = wined3d_allocator_chunk_allocate(chunk, order)))
7509 return block;
7512 if (!(chunk = allocator->ops->allocator_create_chunk(allocator,
7513 context, memory_type, WINED3D_ALLOCATOR_CHUNK_SIZE)))
7514 return NULL;
7516 if (!(block = wined3d_allocator_chunk_allocate(chunk, order)))
7517 return NULL;
7519 return block;
7522 bool wined3d_allocator_init(struct wined3d_allocator *allocator,
7523 size_t pool_count, const struct wined3d_allocator_ops *allocator_ops)
7525 size_t i;
7527 allocator->ops = allocator_ops;
7528 allocator->pool_count = pool_count;
7529 if (!(allocator->pools = heap_calloc(pool_count, sizeof(*allocator->pools))))
7530 return false;
7531 for (i = 0; i < pool_count; ++i)
7533 list_init(&allocator->pools[i].chunks);
7536 allocator->free = NULL;
7538 return true;