1 /*
2 * Copyright 2011 Joakim Sindholt <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef _NINE_PIPE_H_
7 #define _NINE_PIPE_H_
8
9 #include "d3d9.h"
10 #include "util/format/u_formats.h"
11 #include "pipe/p_screen.h"
12 #include "pipe/p_state.h" /* pipe_box */
13 #include "util/macros.h"
14 #include "util/u_rect.h"
15 #include "util/format/u_format.h"
16 #include "nine_helpers.h"
17
18 struct cso_context;
19
20 extern const enum pipe_format nine_d3d9_to_pipe_format_map[120];
21 extern const D3DFORMAT nine_pipe_to_d3d9_format_map[PIPE_FORMAT_COUNT];
22
23 void nine_convert_dsa_state(struct pipe_depth_stencil_alpha_state *, const DWORD *);
24 void nine_convert_rasterizer_state(struct NineDevice9 *, struct pipe_rasterizer_state *, const DWORD *);
25 void nine_convert_blend_state(struct pipe_blend_state *, const DWORD *);
26 void nine_convert_sampler_state(struct cso_context *, int idx, const DWORD *);
27
28 #define is_ATI1_ATI2(format) (format == PIPE_FORMAT_RGTC1_UNORM || format == PIPE_FORMAT_RGTC2_UNORM)
29
30 static inline void
rect_to_pipe_box(struct pipe_box * dst,const RECT * src)31 rect_to_pipe_box(struct pipe_box *dst, const RECT *src)
32 {
33 dst->x = src->left;
34 dst->y = src->top;
35 dst->z = 0;
36 dst->width = src->right - src->left;
37 dst->height = src->bottom - src->top;
38 dst->depth = 1;
39 }
40
41 static inline void
pipe_box_to_rect(RECT * dst,const struct pipe_box * src)42 pipe_box_to_rect(RECT *dst, const struct pipe_box *src)
43 {
44 dst->left = src->x;
45 dst->right = src->x + src->width;
46 dst->top = src->y;
47 dst->bottom = src->y + src->height;
48 }
49
50 static inline void
rect_minify_inclusive(RECT * rect)51 rect_minify_inclusive(RECT *rect)
52 {
53 rect->left = rect->left >> 2;
54 rect->top = rect->top >> 2;
55 rect->right = DIV_ROUND_UP(rect->right, 2);
56 rect->bottom = DIV_ROUND_UP(rect->bottom, 2);
57 }
58
59 /* We suppose:
60 * 0 <= rect->left < rect->right
61 * 0 <= rect->top < rect->bottom
62 */
63 static inline void
fit_rect_format_inclusive(enum pipe_format format,RECT * rect,int width,int height)64 fit_rect_format_inclusive(enum pipe_format format, RECT *rect, int width, int height)
65 {
66 const unsigned w = util_format_get_blockwidth(format);
67 const unsigned h = util_format_get_blockheight(format);
68
69 if (util_format_is_compressed(format)) {
70 rect->left = rect->left - rect->left % w;
71 rect->top = rect->top - rect->top % h;
72 rect->right = (rect->right % w) == 0 ?
73 rect->right :
74 rect->right - (rect->right % w) + w;
75 rect->bottom = (rect->bottom % h) == 0 ?
76 rect->bottom :
77 rect->bottom - (rect->bottom % h) + h;
78 }
79
80 rect->right = MIN2(rect->right, width);
81 rect->bottom = MIN2(rect->bottom, height);
82 }
83
84 static inline bool
rect_to_pipe_box_clamp(struct pipe_box * dst,const RECT * src)85 rect_to_pipe_box_clamp(struct pipe_box *dst, const RECT *src)
86 {
87 rect_to_pipe_box(dst, src);
88
89 if (dst->width <= 0 || dst->height <= 0) {
90 DBG_FLAG(DBG_UNKNOWN, "Warning: NULL box");
91 dst->width = MAX2(dst->width, 0);
92 dst->height = MAX2(dst->height, 0);
93 return true;
94 }
95 return false;
96 }
97
98 static inline bool
rect_to_pipe_box_flip(struct pipe_box * dst,const RECT * src)99 rect_to_pipe_box_flip(struct pipe_box *dst, const RECT *src)
100 {
101 rect_to_pipe_box(dst, src);
102
103 if (dst->width >= 0 && dst->height >= 0)
104 return false;
105 if (dst->width < 0) dst->width = -dst->width;
106 if (dst->height < 0) dst->height = -dst->height;
107 return true;
108 }
109
110 static inline void
rect_to_pipe_box_xy_only(struct pipe_box * dst,const RECT * src)111 rect_to_pipe_box_xy_only(struct pipe_box *dst, const RECT *src)
112 {
113 user_warn(src->left > src->right || src->top > src->bottom);
114
115 dst->x = src->left;
116 dst->y = src->top;
117 dst->width = src->right - src->left;
118 dst->height = src->bottom - src->top;
119 }
120
121 static inline bool
rect_to_pipe_box_xy_only_clamp(struct pipe_box * dst,const RECT * src)122 rect_to_pipe_box_xy_only_clamp(struct pipe_box *dst, const RECT *src)
123 {
124 rect_to_pipe_box_xy_only(dst, src);
125
126 if (dst->width <= 0 || dst->height <= 0) {
127 DBG_FLAG(DBG_UNKNOWN, "Warning: NULL box");
128 dst->width = MAX2(dst->width, 0);
129 dst->height = MAX2(dst->height, 0);
130 return true;
131 }
132 return false;
133 }
134
135 static inline void
rect_to_g3d_u_rect(struct u_rect * dst,const RECT * src)136 rect_to_g3d_u_rect(struct u_rect *dst, const RECT *src)
137 {
138 user_warn(src->left > src->right || src->top > src->bottom);
139
140 dst->x0 = src->left;
141 dst->x1 = src->right;
142 dst->y0 = src->top;
143 dst->y1 = src->bottom;
144 }
145
146 static inline void
d3dbox_to_pipe_box(struct pipe_box * dst,const D3DBOX * src)147 d3dbox_to_pipe_box(struct pipe_box *dst, const D3DBOX *src)
148 {
149 user_warn(src->Left > src->Right);
150 user_warn(src->Top > src->Bottom);
151 user_warn(src->Front > src->Back);
152
153 dst->x = src->Left;
154 dst->y = src->Top;
155 dst->z = src->Front;
156 dst->width = src->Right - src->Left;
157 dst->height = src->Bottom - src->Top;
158 dst->depth = src->Back - src->Front;
159 }
160
161 static inline D3DFORMAT
pipe_to_d3d9_format(enum pipe_format format)162 pipe_to_d3d9_format(enum pipe_format format)
163 {
164 return nine_pipe_to_d3d9_format_map[format];
165 }
166
167 static inline bool
fetch4_compatible_format(D3DFORMAT fmt)168 fetch4_compatible_format( D3DFORMAT fmt )
169 {
170 /* Basically formats with only red channel are allowed (with some exceptions) */
171 static const D3DFORMAT allowed[] = { /* TODO: list incomplete */
172 D3DFMT_L8,
173 D3DFMT_L16,
174 D3DFMT_R16F,
175 D3DFMT_R32F,
176 D3DFMT_A8,
177 D3DFMT_DF16,
178 D3DFMT_DF24,
179 D3DFMT_INTZ
180 };
181 unsigned i;
182
183 for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
184 if (fmt == allowed[i]) { return true; }
185 }
186 return false;
187 }
188
189 /* ATI1 and ATI2 are not officially compressed in d3d9 */
190 static inline bool
compressed_format(D3DFORMAT fmt)191 compressed_format( D3DFORMAT fmt )
192 {
193 switch (fmt) {
194 case D3DFMT_DXT1:
195 case D3DFMT_DXT2:
196 case D3DFMT_DXT3:
197 case D3DFMT_DXT4:
198 case D3DFMT_DXT5:
199 return true;
200 default:
201 break;
202 }
203 return false;
204 }
205
206 static inline bool
depth_stencil_format(D3DFORMAT fmt)207 depth_stencil_format( D3DFORMAT fmt )
208 {
209 static const D3DFORMAT allowed[] = {
210 D3DFMT_D16_LOCKABLE,
211 D3DFMT_D32,
212 D3DFMT_D15S1,
213 D3DFMT_D24S8,
214 D3DFMT_D24X8,
215 D3DFMT_D24X4S4,
216 D3DFMT_D16,
217 D3DFMT_D32F_LOCKABLE,
218 D3DFMT_D24FS8,
219 D3DFMT_D32_LOCKABLE,
220 D3DFMT_DF16,
221 D3DFMT_DF24,
222 D3DFMT_INTZ
223 };
224 unsigned i;
225
226 for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
227 if (fmt == allowed[i]) { return true; }
228 }
229 return false;
230 }
231
232 static inline unsigned
d3d9_get_pipe_depth_format_bindings(D3DFORMAT format)233 d3d9_get_pipe_depth_format_bindings(D3DFORMAT format)
234 {
235 switch (format) {
236 case D3DFMT_D32:
237 case D3DFMT_D15S1:
238 case D3DFMT_D24S8:
239 case D3DFMT_D24X8:
240 case D3DFMT_D24X4S4:
241 case D3DFMT_D16:
242 case D3DFMT_D24FS8:
243 return PIPE_BIND_DEPTH_STENCIL;
244 case D3DFMT_D32F_LOCKABLE:
245 case D3DFMT_D16_LOCKABLE:
246 case D3DFMT_D32_LOCKABLE:
247 return PIPE_BIND_DEPTH_STENCIL;
248 case D3DFMT_DF16:
249 case D3DFMT_DF24:
250 case D3DFMT_INTZ:
251 return PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_SAMPLER_VIEW;
252 default: unreachable("Unexpected format");
253 }
254 }
255
256 static inline enum pipe_format
d3d9_to_pipe_format_internal(D3DFORMAT format)257 d3d9_to_pipe_format_internal(D3DFORMAT format)
258 {
259 if (format <= D3DFMT_A2B10G10R10_XR_BIAS)
260 return nine_d3d9_to_pipe_format_map[format];
261 switch (format) {
262 case D3DFMT_INTZ: return PIPE_FORMAT_S8_UINT_Z24_UNORM;
263 case D3DFMT_DF16: return PIPE_FORMAT_Z16_UNORM;
264 case D3DFMT_DF24: return PIPE_FORMAT_X8Z24_UNORM;
265 case D3DFMT_DXT1: return PIPE_FORMAT_DXT1_RGBA;
266 case D3DFMT_DXT2: return PIPE_FORMAT_DXT3_RGBA; /* XXX */
267 case D3DFMT_DXT3: return PIPE_FORMAT_DXT3_RGBA;
268 case D3DFMT_DXT4: return PIPE_FORMAT_DXT5_RGBA; /* XXX */
269 case D3DFMT_DXT5: return PIPE_FORMAT_DXT5_RGBA;
270 case D3DFMT_ATI1: return PIPE_FORMAT_RGTC1_UNORM;
271 case D3DFMT_ATI2: return PIPE_FORMAT_RGTC2_UNORM;
272 case D3DFMT_UYVY: return PIPE_FORMAT_UYVY;
273 case D3DFMT_YUY2: return PIPE_FORMAT_YUYV; /* XXX check */
274 case D3DFMT_NV12: return PIPE_FORMAT_NV12;
275 case D3DFMT_G8R8_G8B8: return PIPE_FORMAT_G8R8_G8B8_UNORM; /* XXX order ? */
276 case D3DFMT_R8G8_B8G8: return PIPE_FORMAT_R8G8_B8G8_UNORM; /* XXX order ? */
277 case D3DFMT_BINARYBUFFER: return PIPE_FORMAT_NONE; /* not a format */
278 case D3DFMT_MULTI2_ARGB8: return PIPE_FORMAT_NONE; /* not supported */
279 case D3DFMT_Y210: /* XXX */
280 case D3DFMT_Y216:
281 case D3DFMT_NV11:
282 case D3DFMT_NULL: /* special cased, only for surfaces */
283 return PIPE_FORMAT_NONE;
284 default:
285 DBG_FLAG(DBG_UNKNOWN, "unknown D3DFORMAT: 0x%x/%c%c%c%c\n",
286 format, (char)format, (char)(format >> 8),
287 (char)(format >> 16), (char)(format >> 24));
288 return PIPE_FORMAT_NONE;
289 }
290 }
291
292 #define format_check_internal(pipe_format) \
293 screen->is_format_supported(screen, pipe_format, target, \
294 sample_count, sample_count, bindings)
295
296 static inline enum pipe_format
d3d9_to_pipe_format_checked(struct pipe_screen * screen,D3DFORMAT format,enum pipe_texture_target target,unsigned sample_count,unsigned bindings,bool srgb,bool bypass_check)297 d3d9_to_pipe_format_checked(struct pipe_screen *screen,
298 D3DFORMAT format,
299 enum pipe_texture_target target,
300 unsigned sample_count,
301 unsigned bindings,
302 bool srgb,
303 bool bypass_check)
304 {
305 enum pipe_format result;
306
307 /* We cannot render to depth textures as a render target */
308 if (depth_stencil_format(format) && (bindings & PIPE_BIND_RENDER_TARGET))
309 return PIPE_FORMAT_NONE;
310
311 result = d3d9_to_pipe_format_internal(format);
312 if (result == PIPE_FORMAT_NONE)
313 return PIPE_FORMAT_NONE;
314
315 if (srgb)
316 result = util_format_srgb(result);
317
318 /* bypass_check: Used for D3DPOOL_SCRATCH, which
319 * isn't limited to the formats supported by the
320 * device, and to check we are not using a format
321 * fallback. */
322 if (bypass_check || format_check_internal(result))
323 return result;
324
325 /* fallback to another format for formats
326 * that match several pipe_format */
327 switch(format) {
328 /* depth buffer formats are not lockable (except those for which it
329 * is precised in the name), so it is ok to match to another similar
330 * format. In all cases, if the app reads the texture with a shader,
331 * it gets depth on r and doesn't get stencil.*/
332 case D3DFMT_D16:
333 /* D16 support is a requirement, but as it cannot be locked,
334 * it is ok to revert to D24 */
335 if (format_check_internal(PIPE_FORMAT_Z24X8_UNORM))
336 return PIPE_FORMAT_Z24X8_UNORM;
337 if (format_check_internal(PIPE_FORMAT_X8Z24_UNORM))
338 return PIPE_FORMAT_X8Z24_UNORM;
339 break;
340 case D3DFMT_INTZ:
341 case D3DFMT_D24S8:
342 if (format_check_internal(PIPE_FORMAT_Z24_UNORM_S8_UINT))
343 return PIPE_FORMAT_Z24_UNORM_S8_UINT;
344 break;
345 case D3DFMT_DF24:
346 case D3DFMT_D24X8:
347 if (format_check_internal(PIPE_FORMAT_Z24X8_UNORM))
348 return PIPE_FORMAT_Z24X8_UNORM;
349 break;
350 /* Support for X8L8V8U8 bumpenvmap format with lighting bits.
351 * X8L8V8U8 is commonly supported among dx9 cards.
352 * To avoid precision loss, we use PIPE_FORMAT_R32G32B32X32_FLOAT,
353 * however using PIPE_FORMAT_R8G8B8A8_SNORM should be ok */
354 case D3DFMT_X8L8V8U8:
355 if (bindings & PIPE_BIND_RENDER_TARGET)
356 return PIPE_FORMAT_NONE;
357 if (format_check_internal(PIPE_FORMAT_R32G32B32X32_FLOAT))
358 return PIPE_FORMAT_R32G32B32X32_FLOAT;
359 break;
360 /* Fallback for YUV formats */
361 case D3DFMT_UYVY:
362 case D3DFMT_YUY2:
363 case D3DFMT_NV12:
364 if (bindings & PIPE_BIND_RENDER_TARGET)
365 return PIPE_FORMAT_NONE;
366 if (format_check_internal(PIPE_FORMAT_R8G8B8X8_UNORM))
367 return PIPE_FORMAT_R8G8B8X8_UNORM;
368 break;
369 default:
370 break;
371 }
372 return PIPE_FORMAT_NONE;
373 }
374
375 /* The quality levels are vendor dependent, so we set our own.
376 * Every quality level has its own sample count and sample
377 * position matrix.
378 * The exact mapping might differ from system to system but that's OK,
379 * as there's no way to gather more information about quality levels
380 * in D3D9.
381 * In case of NONMASKABLE multisample map every quality-level
382 * to a MASKABLE MultiSampleType:
383 * 0: no MSAA
384 * 1: 2x MSAA
385 * 2: 4x MSAA
386 * ...
387 * If the requested quality level is not available to nearest
388 * matching quality level is used.
389 * If no multisample is available the function sets
390 * multisample to D3DMULTISAMPLE_NONE and returns zero.
391 */
392 static inline HRESULT
d3dmultisample_type_check(struct pipe_screen * screen,D3DFORMAT format,D3DMULTISAMPLE_TYPE * multisample,DWORD multisamplequality,DWORD * levels)393 d3dmultisample_type_check(struct pipe_screen *screen,
394 D3DFORMAT format,
395 D3DMULTISAMPLE_TYPE *multisample,
396 DWORD multisamplequality,
397 DWORD *levels)
398 {
399 unsigned bind, i;
400
401 assert(multisample);
402
403 if (levels)
404 *levels = 1;
405
406 /* Ignores multisamplequality */
407 if (*multisample == D3DMULTISAMPLE_NONE)
408 return D3D_OK;
409
410 if (*multisample == D3DMULTISAMPLE_NONMASKABLE) {
411 if (depth_stencil_format(format))
412 bind = d3d9_get_pipe_depth_format_bindings(format);
413 else /* render-target */
414 bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
415
416 *multisample = 0;
417 for (i = D3DMULTISAMPLE_2_SAMPLES; i < D3DMULTISAMPLE_16_SAMPLES &&
418 multisamplequality; ++i) {
419 if (d3d9_to_pipe_format_checked(screen, format, PIPE_TEXTURE_2D,
420 i, bind, false, false) != PIPE_FORMAT_NONE) {
421 multisamplequality--;
422 if (levels)
423 (*levels)++;
424 *multisample = i;
425 }
426 }
427 }
428 /* Make sure to get an exact match */
429 if (multisamplequality)
430 return D3DERR_INVALIDCALL;
431 return D3D_OK;
432 }
433
434 static inline const char *
d3dformat_to_string(D3DFORMAT fmt)435 d3dformat_to_string(D3DFORMAT fmt)
436 {
437 switch (fmt) {
438 case D3DFMT_UNKNOWN: return "D3DFMT_UNKNOWN";
439 case D3DFMT_R8G8B8: return "D3DFMT_R8G8B8";
440 case D3DFMT_A8R8G8B8: return "D3DFMT_A8R8G8B8";
441 case D3DFMT_X8R8G8B8: return "D3DFMT_X8R8G8B8";
442 case D3DFMT_R5G6B5: return "D3DFMT_R5G6B5";
443 case D3DFMT_X1R5G5B5: return "D3DFMT_X1R5G5B5";
444 case D3DFMT_A1R5G5B5: return "D3DFMT_A1R5G5B5";
445 case D3DFMT_A4R4G4B4: return "D3DFMT_A4R4G4B4";
446 case D3DFMT_R3G3B2: return "D3DFMT_R3G3B2";
447 case D3DFMT_A8: return "D3DFMT_A8";
448 case D3DFMT_A8R3G3B2: return "D3DFMT_A8R3G3B2";
449 case D3DFMT_X4R4G4B4: return "D3DFMT_X4R4G4B4";
450 case D3DFMT_A2B10G10R10: return "D3DFMT_A2B10G10R10";
451 case D3DFMT_A8B8G8R8: return "D3DFMT_A8B8G8R8";
452 case D3DFMT_X8B8G8R8: return "D3DFMT_X8B8G8R8";
453 case D3DFMT_G16R16: return "D3DFMT_G16R16";
454 case D3DFMT_A2R10G10B10: return "D3DFMT_A2R10G10B10";
455 case D3DFMT_A16B16G16R16: return "D3DFMT_A16B16G16R16";
456 case D3DFMT_A8P8: return "D3DFMT_A8P8";
457 case D3DFMT_P8: return "D3DFMT_P8";
458 case D3DFMT_L8: return "D3DFMT_L8";
459 case D3DFMT_A8L8: return "D3DFMT_A8L8";
460 case D3DFMT_A4L4: return "D3DFMT_A4L4";
461 case D3DFMT_V8U8: return "D3DFMT_V8U8";
462 case D3DFMT_L6V5U5: return "D3DFMT_L6V5U5";
463 case D3DFMT_X8L8V8U8: return "D3DFMT_X8L8V8U8";
464 case D3DFMT_Q8W8V8U8: return "D3DFMT_Q8W8V8U8";
465 case D3DFMT_V16U16: return "D3DFMT_V16U16";
466 case D3DFMT_A2W10V10U10: return "D3DFMT_A2W10V10U10";
467 case D3DFMT_UYVY: return "D3DFMT_UYVY";
468 case D3DFMT_R8G8_B8G8: return "D3DFMT_R8G8_B8G8";
469 case D3DFMT_YUY2: return "D3DFMT_YUY2";
470 case D3DFMT_G8R8_G8B8: return "D3DFMT_G8R8_G8B8";
471 case D3DFMT_DXT1: return "D3DFMT_DXT1";
472 case D3DFMT_DXT2: return "D3DFMT_DXT2";
473 case D3DFMT_DXT3: return "D3DFMT_DXT3";
474 case D3DFMT_DXT4: return "D3DFMT_DXT4";
475 case D3DFMT_DXT5: return "D3DFMT_DXT5";
476 case D3DFMT_ATI1: return "D3DFMT_ATI1";
477 case D3DFMT_ATI2: return "D3DFMT_ATI2";
478 case D3DFMT_D16_LOCKABLE: return "D3DFMT_D16_LOCKABLE";
479 case D3DFMT_D32: return "D3DFMT_D32";
480 case D3DFMT_D15S1: return "D3DFMT_D15S1";
481 case D3DFMT_D24S8: return "D3DFMT_D24S8";
482 case D3DFMT_D24X8: return "D3DFMT_D24X8";
483 case D3DFMT_D24X4S4: return "D3DFMT_D24X4S4";
484 case D3DFMT_D16: return "D3DFMT_D16";
485 case D3DFMT_D32F_LOCKABLE: return "D3DFMT_D32F_LOCKABLE";
486 case D3DFMT_D24FS8: return "D3DFMT_D24FS8";
487 case D3DFMT_D32_LOCKABLE: return "D3DFMT_D32_LOCKABLE";
488 case D3DFMT_S8_LOCKABLE: return "D3DFMT_S8_LOCKABLE";
489 case D3DFMT_L16: return "D3DFMT_L16";
490 case D3DFMT_VERTEXDATA: return "D3DFMT_VERTEXDATA";
491 case D3DFMT_INDEX16: return "D3DFMT_INDEX16";
492 case D3DFMT_INDEX32: return "D3DFMT_INDEX32";
493 case D3DFMT_Q16W16V16U16: return "D3DFMT_Q16W16V16U16";
494 case D3DFMT_MULTI2_ARGB8: return "D3DFMT_MULTI2_ARGB8";
495 case D3DFMT_R16F: return "D3DFMT_R16F";
496 case D3DFMT_G16R16F: return "D3DFMT_G16R16F";
497 case D3DFMT_A16B16G16R16F: return "D3DFMT_A16B16G16R16F";
498 case D3DFMT_R32F: return "D3DFMT_R32F";
499 case D3DFMT_G32R32F: return "D3DFMT_G32R32F";
500 case D3DFMT_A32B32G32R32F: return "D3DFMT_A32B32G32R32F";
501 case D3DFMT_CxV8U8: return "D3DFMT_CxV8U8";
502 case D3DFMT_A1: return "D3DFMT_A1";
503 case D3DFMT_A2B10G10R10_XR_BIAS: return "D3DFMT_A2B10G10R10_XR_BIAS";
504 case D3DFMT_BINARYBUFFER: return "D3DFMT_BINARYBUFFER";
505 case D3DFMT_DF16: return "D3DFMT_DF16";
506 case D3DFMT_DF24: return "D3DFMT_DF24";
507 case D3DFMT_INTZ: return "D3DFMT_INTZ";
508 case D3DFMT_NVDB: return "D3DFMT_NVDB";
509 case D3DFMT_RESZ: return "D3DFMT_RESZ";
510 case D3DFMT_NULL: return "D3DFMT_NULL";
511 case D3DFMT_ATOC: return "D3DFMT_ATOC";
512 default:
513 break;
514 }
515 return "Unknown";
516 }
517
518 static inline unsigned
nine_fvf_stride(DWORD fvf)519 nine_fvf_stride( DWORD fvf )
520 {
521 unsigned texcount, i, size = 0;
522
523 switch (fvf & D3DFVF_POSITION_MASK) {
524 case D3DFVF_XYZ: size += 3*4; break;
525 case D3DFVF_XYZRHW: size += 4*4; break;
526 case D3DFVF_XYZB1: size += 4*4; break;
527 case D3DFVF_XYZB2: size += 5*4; break;
528 case D3DFVF_XYZB3: size += 6*4; break;
529 case D3DFVF_XYZB4: size += 7*4; break;
530 case D3DFVF_XYZB5: size += 8*4; break;
531 case D3DFVF_XYZW: size += 4*4; break;
532 default:
533 user_warn("Position doesn't match any known combination.");
534 break;
535 }
536
537 if (fvf & D3DFVF_NORMAL) { size += 3*4; }
538 if (fvf & D3DFVF_PSIZE) { size += 1*4; }
539 if (fvf & D3DFVF_DIFFUSE) { size += 1*4; }
540 if (fvf & D3DFVF_SPECULAR) { size += 1*4; }
541
542 texcount = (fvf >> D3DFVF_TEXCOUNT_SHIFT) & D3DFVF_TEXCOUNT_MASK;
543 if (user_error(texcount <= 8))
544 texcount = 8;
545
546 for (i = 0; i < texcount; ++i) {
547 unsigned texformat = (fvf>>(16+i*2))&0x3;
548 /* texformats are defined having been shifted around so 1=3,2=0,3=1,4=2
549 * meaning we can just do this instead of the switch below */
550 size += (((texformat+1)&0x3)+1)*4;
551
552 /*
553 switch (texformat) {
554 case D3DFVF_TEXTUREFORMAT1: size += 1*4;
555 case D3DFVF_TEXTUREFORMAT2: size += 2*4;
556 case D3DFVF_TEXTUREFORMAT3: size += 3*4;
557 case D3DFVF_TEXTUREFORMAT4: size += 4*4;
558 }
559 */
560 }
561
562 return size;
563 }
564
565 static inline void
d3dcolor_to_rgba(float * rgba,D3DCOLOR color)566 d3dcolor_to_rgba(float *rgba, D3DCOLOR color)
567 {
568 rgba[0] = (float)((color >> 16) & 0xFF) / 0xFF;
569 rgba[1] = (float)((color >> 8) & 0xFF) / 0xFF;
570 rgba[2] = (float)((color >> 0) & 0xFF) / 0xFF;
571 rgba[3] = (float)((color >> 24) & 0xFF) / 0xFF;
572 }
573
574 static inline void
d3dcolor_to_pipe_color_union(union pipe_color_union * rgba,D3DCOLOR color)575 d3dcolor_to_pipe_color_union(union pipe_color_union *rgba, D3DCOLOR color)
576 {
577 d3dcolor_to_rgba(&rgba->f[0], color);
578 }
579
580 static inline unsigned
d3dprimitivetype_to_pipe_prim(D3DPRIMITIVETYPE prim)581 d3dprimitivetype_to_pipe_prim(D3DPRIMITIVETYPE prim)
582 {
583 switch (prim) {
584 case D3DPT_POINTLIST: return MESA_PRIM_POINTS;
585 case D3DPT_LINELIST: return MESA_PRIM_LINES;
586 case D3DPT_LINESTRIP: return MESA_PRIM_LINE_STRIP;
587 case D3DPT_TRIANGLELIST: return MESA_PRIM_TRIANGLES;
588 case D3DPT_TRIANGLESTRIP: return MESA_PRIM_TRIANGLE_STRIP;
589 case D3DPT_TRIANGLEFAN: return MESA_PRIM_TRIANGLE_FAN;
590 default:
591 assert(0);
592 return MESA_PRIM_POINTS;
593 }
594 }
595
596 static inline unsigned
prim_count_to_vertex_count(D3DPRIMITIVETYPE prim,UINT count)597 prim_count_to_vertex_count(D3DPRIMITIVETYPE prim, UINT count)
598 {
599 switch (prim) {
600 case D3DPT_POINTLIST: return count;
601 case D3DPT_LINELIST: return count * 2;
602 case D3DPT_LINESTRIP: return count + 1;
603 case D3DPT_TRIANGLELIST: return count * 3;
604 case D3DPT_TRIANGLESTRIP: return count + 2;
605 case D3DPT_TRIANGLEFAN: return count + 2;
606 default:
607 assert(0);
608 return 0;
609 }
610 }
611
612 static inline unsigned
d3dcmpfunc_to_pipe_func(D3DCMPFUNC func)613 d3dcmpfunc_to_pipe_func(D3DCMPFUNC func)
614 {
615 switch (func) {
616 case D3DCMP_NEVER: return PIPE_FUNC_NEVER;
617 case D3DCMP_LESS: return PIPE_FUNC_LESS;
618 case D3DCMP_EQUAL: return PIPE_FUNC_EQUAL;
619 case D3DCMP_LESSEQUAL: return PIPE_FUNC_LEQUAL;
620 case D3DCMP_GREATER: return PIPE_FUNC_GREATER;
621 case D3DCMP_NOTEQUAL: return PIPE_FUNC_NOTEQUAL;
622 case D3DCMP_GREATEREQUAL: return PIPE_FUNC_GEQUAL;
623 case D3DCMP_ALWAYS: return PIPE_FUNC_ALWAYS;
624 case D3DCMP_NEVER_ZERO: return PIPE_FUNC_NEVER; // Tested on windows + ATI HD5770
625 default:
626 assert(0);
627 return PIPE_FUNC_NEVER;
628 }
629 }
630
631 static inline unsigned
d3dstencilop_to_pipe_stencil_op(D3DSTENCILOP op)632 d3dstencilop_to_pipe_stencil_op(D3DSTENCILOP op)
633 {
634 switch (op) {
635 case D3DSTENCILOP_KEEP: return PIPE_STENCIL_OP_KEEP;
636 case D3DSTENCILOP_ZERO: return PIPE_STENCIL_OP_ZERO;
637 case D3DSTENCILOP_REPLACE: return PIPE_STENCIL_OP_REPLACE;
638 case D3DSTENCILOP_INCRSAT: return PIPE_STENCIL_OP_INCR;
639 case D3DSTENCILOP_DECRSAT: return PIPE_STENCIL_OP_DECR;
640 case D3DSTENCILOP_INVERT: return PIPE_STENCIL_OP_INVERT;
641 case D3DSTENCILOP_INCR: return PIPE_STENCIL_OP_INCR_WRAP;
642 case D3DSTENCILOP_DECR: return PIPE_STENCIL_OP_DECR_WRAP;
643 default:
644 return PIPE_STENCIL_OP_ZERO;
645 }
646 }
647
648 static inline unsigned
d3dcull_to_pipe_face(D3DCULL cull)649 d3dcull_to_pipe_face(D3DCULL cull)
650 {
651 switch (cull) {
652 case D3DCULL_NONE: return PIPE_FACE_NONE;
653 case D3DCULL_CW: return PIPE_FACE_FRONT;
654 case D3DCULL_CCW: return PIPE_FACE_BACK;
655 default:
656 assert(0);
657 return PIPE_FACE_NONE;
658 }
659 }
660
661 static inline unsigned
d3dfillmode_to_pipe_polygon_mode(D3DFILLMODE mode)662 d3dfillmode_to_pipe_polygon_mode(D3DFILLMODE mode)
663 {
664 switch (mode) {
665 case D3DFILL_POINT: return PIPE_POLYGON_MODE_POINT;
666 case D3DFILL_WIREFRAME: return PIPE_POLYGON_MODE_LINE;
667 case D3DFILL_SOLID: return PIPE_POLYGON_MODE_FILL;
668 case D3DFILL_SOLID_ZERO:return PIPE_POLYGON_MODE_FILL;
669 default:
670 assert(0);
671 return PIPE_POLYGON_MODE_FILL;
672 }
673 }
674
675 static inline unsigned
d3dblendop_to_pipe_blend(D3DBLENDOP op)676 d3dblendop_to_pipe_blend(D3DBLENDOP op)
677 {
678 switch (op) {
679 case D3DBLENDOP_ADD: return PIPE_BLEND_ADD;
680 case D3DBLENDOP_SUBTRACT: return PIPE_BLEND_SUBTRACT;
681 case D3DBLENDOP_REVSUBTRACT: return PIPE_BLEND_REVERSE_SUBTRACT;
682 case D3DBLENDOP_MIN: return PIPE_BLEND_MIN;
683 case D3DBLENDOP_MAX: return PIPE_BLEND_MAX;
684 default:
685 assert(0);
686 return PIPE_BLEND_ADD;
687 }
688 }
689
690 /* NOTE: The COLOR factors for are equal to the ALPHA ones for alpha.
691 * Drivers may check RGB and ALPHA factors for equality so we should not
692 * simply substitute the ALPHA variants.
693 */
694 static inline unsigned
d3dblend_alpha_to_pipe_blendfactor(D3DBLEND b)695 d3dblend_alpha_to_pipe_blendfactor(D3DBLEND b)
696 {
697 switch (b) {
698 case D3DBLEND_ZERO: return PIPE_BLENDFACTOR_ZERO;
699 case D3DBLEND_ONE: return PIPE_BLENDFACTOR_ONE;
700 case D3DBLEND_SRCCOLOR: return PIPE_BLENDFACTOR_SRC_COLOR/*ALPHA*/;
701 case D3DBLEND_INVSRCCOLOR: return PIPE_BLENDFACTOR_INV_SRC_COLOR/*ALPHA*/;
702 case D3DBLEND_SRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
703 case D3DBLEND_INVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
704 case D3DBLEND_DESTALPHA: return PIPE_BLENDFACTOR_DST_ALPHA;
705 case D3DBLEND_INVDESTALPHA: return PIPE_BLENDFACTOR_INV_DST_ALPHA;
706 case D3DBLEND_DESTCOLOR: return PIPE_BLENDFACTOR_DST_COLOR/*ALPHA*/;
707 case D3DBLEND_INVDESTCOLOR: return PIPE_BLENDFACTOR_INV_DST_COLOR/*ALPHA*/;
708 case D3DBLEND_SRCALPHASAT: return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
709 case D3DBLEND_BOTHSRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
710 case D3DBLEND_BOTHINVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
711 case D3DBLEND_BLENDFACTOR: return PIPE_BLENDFACTOR_CONST_COLOR/*ALPHA*/;
712 case D3DBLEND_INVBLENDFACTOR: return PIPE_BLENDFACTOR_INV_CONST_COLOR/*ALPHA*/;
713 case D3DBLEND_SRCCOLOR2: return PIPE_BLENDFACTOR_ONE; /* XXX */
714 case D3DBLEND_INVSRCCOLOR2: return PIPE_BLENDFACTOR_ZERO; /* XXX */
715 default:
716 DBG_FLAG(DBG_UNKNOWN, "Unhandled blend factor %d\n", b);
717 return PIPE_BLENDFACTOR_ZERO;
718 }
719 }
720
721 static inline unsigned
d3dblend_color_to_pipe_blendfactor(D3DBLEND b)722 d3dblend_color_to_pipe_blendfactor(D3DBLEND b)
723 {
724 switch (b) {
725 case D3DBLEND_ZERO: return PIPE_BLENDFACTOR_ZERO;
726 case D3DBLEND_ONE: return PIPE_BLENDFACTOR_ONE;
727 case D3DBLEND_SRCCOLOR: return PIPE_BLENDFACTOR_SRC_COLOR;
728 case D3DBLEND_INVSRCCOLOR: return PIPE_BLENDFACTOR_INV_SRC_COLOR;
729 case D3DBLEND_SRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
730 case D3DBLEND_INVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
731 case D3DBLEND_DESTALPHA: return PIPE_BLENDFACTOR_DST_ALPHA;
732 case D3DBLEND_INVDESTALPHA: return PIPE_BLENDFACTOR_INV_DST_ALPHA;
733 case D3DBLEND_DESTCOLOR: return PIPE_BLENDFACTOR_DST_COLOR;
734 case D3DBLEND_INVDESTCOLOR: return PIPE_BLENDFACTOR_INV_DST_COLOR;
735 case D3DBLEND_SRCALPHASAT: return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
736 case D3DBLEND_BOTHSRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
737 case D3DBLEND_BOTHINVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
738 case D3DBLEND_BLENDFACTOR: return PIPE_BLENDFACTOR_CONST_COLOR;
739 case D3DBLEND_INVBLENDFACTOR: return PIPE_BLENDFACTOR_INV_CONST_COLOR;
740 case D3DBLEND_SRCCOLOR2: return PIPE_BLENDFACTOR_SRC1_COLOR;
741 case D3DBLEND_INVSRCCOLOR2: return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
742 default:
743 DBG_FLAG(DBG_UNKNOWN, "Unhandled blend factor %d\n", b);
744 return PIPE_BLENDFACTOR_ZERO;
745 }
746 }
747
748 static inline unsigned
d3dtextureaddress_to_pipe_tex_wrap(D3DTEXTUREADDRESS addr)749 d3dtextureaddress_to_pipe_tex_wrap(D3DTEXTUREADDRESS addr)
750 {
751 switch (addr) {
752 case D3DTADDRESS_WRAP: return PIPE_TEX_WRAP_REPEAT;
753 case D3DTADDRESS_MIRROR: return PIPE_TEX_WRAP_MIRROR_REPEAT;
754 case D3DTADDRESS_CLAMP: return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
755 case D3DTADDRESS_BORDER: return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
756 case D3DTADDRESS_MIRRORONCE: return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
757 default:
758 assert(0);
759 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
760 }
761 }
762
763 static inline unsigned
d3dtexturefiltertype_to_pipe_tex_filter(D3DTEXTUREFILTERTYPE filter)764 d3dtexturefiltertype_to_pipe_tex_filter(D3DTEXTUREFILTERTYPE filter)
765 {
766 switch (filter) {
767 case D3DTEXF_POINT: return PIPE_TEX_FILTER_NEAREST;
768 case D3DTEXF_LINEAR: return PIPE_TEX_FILTER_LINEAR;
769 case D3DTEXF_ANISOTROPIC: return PIPE_TEX_FILTER_LINEAR;
770
771 case D3DTEXF_NONE:
772 case D3DTEXF_PYRAMIDALQUAD:
773 case D3DTEXF_GAUSSIANQUAD:
774 case D3DTEXF_CONVOLUTIONMONO:
775 default:
776 assert(0);
777 return PIPE_TEX_FILTER_NEAREST;
778 }
779 }
780
781 static inline unsigned
d3dtexturefiltertype_to_pipe_tex_mipfilter(D3DTEXTUREFILTERTYPE filter)782 d3dtexturefiltertype_to_pipe_tex_mipfilter(D3DTEXTUREFILTERTYPE filter)
783 {
784 switch (filter) {
785 case D3DTEXF_NONE: return PIPE_TEX_MIPFILTER_NONE;
786 case D3DTEXF_POINT: return PIPE_TEX_FILTER_NEAREST;
787 case D3DTEXF_LINEAR: return PIPE_TEX_FILTER_LINEAR;
788 case D3DTEXF_ANISOTROPIC: return PIPE_TEX_FILTER_LINEAR;
789
790 case D3DTEXF_PYRAMIDALQUAD:
791 case D3DTEXF_GAUSSIANQUAD:
792 case D3DTEXF_CONVOLUTIONMONO:
793 default:
794 assert(0);
795 return PIPE_TEX_MIPFILTER_NONE;
796 }
797 }
798
nine_format_get_stride(enum pipe_format format,unsigned width)799 static inline unsigned nine_format_get_stride(enum pipe_format format,
800 unsigned width)
801 {
802 unsigned stride = util_format_get_stride(format, width);
803
804 return align(stride, 4);
805 }
806
nine_format_get_level_alloc_size(enum pipe_format format,unsigned width,unsigned height,unsigned level)807 static inline unsigned nine_format_get_level_alloc_size(enum pipe_format format,
808 unsigned width,
809 unsigned height,
810 unsigned level)
811 {
812 unsigned w, h, size;
813
814 w = u_minify(width, level);
815 h = u_minify(height, level);
816 if (is_ATI1_ATI2(format)) {
817 /* For "unknown" formats like ATIx use width * height bytes */
818 size = w * h;
819 } else if (format == PIPE_FORMAT_NONE) { /* D3DFMT_NULL */
820 size = w * h * 4;
821 } else {
822 size = nine_format_get_stride(format, w) *
823 util_format_get_nblocksy(format, h);
824 }
825
826 return size;
827 }
828
nine_format_get_size_and_offsets(enum pipe_format format,unsigned * offsets,unsigned width,unsigned height,unsigned last_level)829 static inline unsigned nine_format_get_size_and_offsets(enum pipe_format format,
830 unsigned *offsets,
831 unsigned width,
832 unsigned height,
833 unsigned last_level)
834 {
835 unsigned l, w, h, size = 0;
836
837 for (l = 0; l <= last_level; ++l) {
838 w = u_minify(width, l);
839 h = u_minify(height, l);
840 offsets[l] = size;
841 if (is_ATI1_ATI2(format)) {
842 /* For "unknown" formats like ATIx use width * height bytes */
843 size += w * h;
844 } else {
845 size += nine_format_get_stride(format, w) *
846 util_format_get_nblocksy(format, h);
847 }
848 }
849
850 return size;
851 }
852
853 #endif /* _NINE_PIPE_H_ */
854