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