st/nine: Fix depth stencil formats bindings flags.
[mesa.git] / src / gallium / state_trackers / nine / nine_pipe.h
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 "pipe/p_format.h"
28 #include "pipe/p_screen.h"
29 #include "pipe/p_state.h" /* pipe_box */
30 #include "util/u_rect.h"
31 #include "util/u_format.h"
32 #include "nine_helpers.h"
33
34 struct cso_context;
35
36 extern const enum pipe_format nine_d3d9_to_pipe_format_map[120];
37 extern const D3DFORMAT nine_pipe_to_d3d9_format_map[PIPE_FORMAT_COUNT];
38
39 void nine_convert_dsa_state(struct cso_context *, const DWORD *);
40 void nine_convert_rasterizer_state(struct cso_context *, const DWORD *);
41 void nine_convert_blend_state(struct cso_context *, const DWORD *);
42 void nine_convert_sampler_state(struct cso_context *, int idx, const DWORD *);
43
44 void nine_pipe_context_clear(struct NineDevice9 *);
45
46 static INLINE unsigned d3dlock_buffer_to_pipe_transfer_usage(DWORD Flags)
47 {
48 unsigned usage;
49
50 if (Flags & D3DLOCK_DISCARD)
51 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
52 else
53 if (Flags & D3DLOCK_READONLY)
54 usage = PIPE_TRANSFER_READ;
55 else
56 usage = PIPE_TRANSFER_READ_WRITE;
57
58 if (Flags & D3DLOCK_NOOVERWRITE)
59 usage = (PIPE_TRANSFER_UNSYNCHRONIZED |
60 PIPE_TRANSFER_DISCARD_RANGE | usage) & ~PIPE_TRANSFER_READ;
61 else
62 if (Flags & D3DLOCK_DONOTWAIT)
63 usage |= PIPE_TRANSFER_DONTBLOCK;
64
65 /*
66 if (Flags & D3DLOCK_NO_DIRTY_UPDATE)
67 usage |= PIPE_TRANSFER_FLUSH_EXPLICIT;
68 */
69
70 return usage;
71 }
72
73 static INLINE void
74 rect_to_pipe_box(struct pipe_box *dst, const RECT *src)
75 {
76 dst->x = src->left;
77 dst->y = src->top;
78 dst->z = 0;
79 dst->width = src->right - src->left;
80 dst->height = src->bottom - src->top;
81 dst->depth = 1;
82 }
83
84 static INLINE boolean
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 boolean
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
111 nine_u_rect_to_pipe_box(struct pipe_box *dst, const struct u_rect *rect, int z)
112 {
113 dst->x = rect->x0;
114 dst->y = rect->y0;
115 dst->z = z;
116 dst->width = rect->x1 - rect->x0;
117 dst->height = rect->y1 - rect->y0;
118 dst->depth = 1;
119 }
120
121 static INLINE void
122 rect_to_pipe_box_xy_only(struct pipe_box *dst, const RECT *src)
123 {
124 user_warn(src->left > src->right || src->top > src->bottom);
125
126 dst->x = src->left;
127 dst->y = src->top;
128 dst->width = src->right - src->left;
129 dst->height = src->bottom - src->top;
130 }
131
132 static INLINE boolean
133 rect_to_pipe_box_xy_only_clamp(struct pipe_box *dst, const RECT *src)
134 {
135 rect_to_pipe_box_xy_only(dst, src);
136
137 if (dst->width <= 0 || dst->height <= 0) {
138 DBG_FLAG(DBG_UNKNOWN, "Warning: NULL box");
139 dst->width = MAX2(dst->width, 0);
140 dst->height = MAX2(dst->height, 0);
141 return TRUE;
142 }
143 return FALSE;
144 }
145
146 static INLINE void
147 rect_to_g3d_u_rect(struct u_rect *dst, const RECT *src)
148 {
149 user_warn(src->left > src->right || src->top > src->bottom);
150
151 dst->x0 = src->left;
152 dst->x1 = src->right;
153 dst->y0 = src->top;
154 dst->y1 = src->bottom;
155 }
156
157 static INLINE void
158 d3dbox_to_pipe_box(struct pipe_box *dst, const D3DBOX *src)
159 {
160 user_warn(src->Left > src->Right);
161 user_warn(src->Top > src->Bottom);
162 user_warn(src->Front > src->Back);
163
164 dst->x = src->Left;
165 dst->y = src->Top;
166 dst->z = src->Front;
167 dst->width = src->Right - src->Left;
168 dst->height = src->Bottom - src->Top;
169 dst->depth = src->Back - src->Front;
170 }
171
172 static INLINE D3DFORMAT
173 pipe_to_d3d9_format(enum pipe_format format)
174 {
175 return nine_pipe_to_d3d9_format_map[format];
176 }
177
178 static INLINE boolean
179 depth_stencil_format( D3DFORMAT fmt )
180 {
181 static D3DFORMAT allowed[] = {
182 D3DFMT_D16_LOCKABLE,
183 D3DFMT_D32,
184 D3DFMT_D15S1,
185 D3DFMT_D24S8,
186 D3DFMT_D24X8,
187 D3DFMT_D24X4S4,
188 D3DFMT_D16,
189 D3DFMT_D32F_LOCKABLE,
190 D3DFMT_D24FS8,
191 D3DFMT_D32_LOCKABLE,
192 D3DFMT_DF16,
193 D3DFMT_DF24,
194 D3DFMT_INTZ
195 };
196 unsigned i;
197
198 for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
199 if (fmt == allowed[i]) { return TRUE; }
200 }
201 return FALSE;
202 }
203
204 static INLINE unsigned
205 d3d9_get_pipe_depth_format_bindings(D3DFORMAT format)
206 {
207 switch (format) {
208 case D3DFMT_D32:
209 case D3DFMT_D15S1:
210 case D3DFMT_D24S8:
211 case D3DFMT_D24X8:
212 case D3DFMT_D24X4S4:
213 case D3DFMT_D16:
214 case D3DFMT_D24FS8:
215 return PIPE_BIND_DEPTH_STENCIL;
216 case D3DFMT_D32F_LOCKABLE:
217 case D3DFMT_D16_LOCKABLE:
218 case D3DFMT_D32_LOCKABLE:
219 return PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_TRANSFER_READ |
220 PIPE_BIND_TRANSFER_WRITE;
221 case D3DFMT_DF16:
222 case D3DFMT_DF24:
223 case D3DFMT_INTZ:
224 return PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_SAMPLER_VIEW;
225 default: assert(0);
226 }
227 }
228
229 static INLINE enum pipe_format
230 d3d9_to_pipe_format_internal(D3DFORMAT format)
231 {
232 if (format <= D3DFMT_A2B10G10R10_XR_BIAS)
233 return nine_d3d9_to_pipe_format_map[format];
234 switch (format) {
235 case D3DFMT_INTZ: return PIPE_FORMAT_S8_UINT_Z24_UNORM;
236 case D3DFMT_DF16: return PIPE_FORMAT_Z16_UNORM;
237 case D3DFMT_DXT1: return PIPE_FORMAT_DXT1_RGBA;
238 case D3DFMT_DXT2: return PIPE_FORMAT_DXT3_RGBA; /* XXX */
239 case D3DFMT_DXT3: return PIPE_FORMAT_DXT3_RGBA;
240 case D3DFMT_DXT4: return PIPE_FORMAT_DXT5_RGBA; /* XXX */
241 case D3DFMT_DXT5: return PIPE_FORMAT_DXT5_RGBA;
242 case D3DFMT_ATI1: return PIPE_FORMAT_RGTC1_UNORM;
243 case D3DFMT_ATI2: return PIPE_FORMAT_RGTC2_UNORM;
244 case D3DFMT_UYVY: return PIPE_FORMAT_UYVY;
245 case D3DFMT_YUY2: return PIPE_FORMAT_YUYV; /* XXX check */
246 case D3DFMT_NV12: return PIPE_FORMAT_NV12;
247 case D3DFMT_G8R8_G8B8: return PIPE_FORMAT_G8R8_G8B8_UNORM; /* XXX order ? */
248 case D3DFMT_R8G8_B8G8: return PIPE_FORMAT_R8G8_B8G8_UNORM; /* XXX order ? */
249 case D3DFMT_BINARYBUFFER: return PIPE_FORMAT_NONE; /* not a format */
250 case D3DFMT_MULTI2_ARGB8: return PIPE_FORMAT_NONE; /* not supported */
251 case D3DFMT_Y210: /* XXX */
252 case D3DFMT_Y216:
253 case D3DFMT_NV11:
254 case D3DFMT_DF24: /* Similar to D3DFMT_DF16 but for 24-bits.
255 We don't advertise it because when it is supported, Fetch-4 is
256 supposed to be supported, which we don't support yet. */
257 case D3DFMT_NULL: /* special cased, only for surfaces */
258 return PIPE_FORMAT_NONE;
259 default:
260 DBG_FLAG(DBG_UNKNOWN, "unknown D3DFORMAT: 0x%x/%c%c%c%c\n",
261 format, (char)format, (char)(format >> 8),
262 (char)(format >> 16), (char)(format >> 24));
263 return PIPE_FORMAT_NONE;
264 }
265 }
266
267 #define format_check_internal(pipe_format) \
268 screen->is_format_supported(screen, pipe_format, target, \
269 sample_count, bindings)
270
271 static INLINE enum pipe_format
272 d3d9_to_pipe_format_checked(struct pipe_screen *screen,
273 D3DFORMAT format,
274 enum pipe_texture_target target,
275 unsigned sample_count,
276 unsigned bindings,
277 boolean srgb)
278 {
279 enum pipe_format result;
280
281 result = d3d9_to_pipe_format_internal(format);
282 if (result == PIPE_FORMAT_NONE)
283 return PIPE_FORMAT_NONE;
284
285 if (srgb)
286 result = util_format_srgb(result);
287
288 if (format_check_internal(result))
289 return result;
290
291 /* fallback to another format for formats
292 * that match several pipe_format */
293 switch(format) {
294 /* depth buffer formats are not lockable (except those for which it
295 * is precised in the name), so it is ok to match to another similar
296 * format. In all cases, if the app reads the texture with a shader,
297 * it gets depth on r and doesn't get stencil.*/
298 case D3DFMT_INTZ:
299 case D3DFMT_D24S8:
300 if (format_check_internal(PIPE_FORMAT_Z24_UNORM_S8_UINT))
301 return PIPE_FORMAT_Z24_UNORM_S8_UINT;
302 break;
303 case D3DFMT_D24X8:
304 if (format_check_internal(PIPE_FORMAT_Z24X8_UNORM))
305 return PIPE_FORMAT_Z24X8_UNORM;
306 default:
307 break;
308 }
309 return PIPE_FORMAT_NONE;
310 }
311
312 /* same that above, but determines binding flags */
313 static INLINE enum pipe_format
314 d3d9_to_pipe_format_checked_no_bind(struct pipe_screen *screen,
315 D3DFORMAT format,
316 enum pipe_texture_target target,
317 unsigned sample_count,
318 boolean srgb)
319 {
320 enum pipe_format result;
321 unsigned bindings;
322
323 result = d3d9_to_pipe_format_internal(format);
324 if (result == PIPE_FORMAT_NONE)
325 return PIPE_FORMAT_NONE;
326
327 bindings = util_format_is_depth_or_stencil(result) ?
328 PIPE_BIND_DEPTH_STENCIL : PIPE_BIND_RENDER_TARGET;
329 if (srgb)
330 result = util_format_srgb(result);
331
332 if (format_check_internal(result))
333 return result;
334 return PIPE_FORMAT_NONE;
335 }
336
337 static INLINE const char *
338 d3dformat_to_string(D3DFORMAT fmt)
339 {
340 switch (fmt) {
341 case D3DFMT_UNKNOWN: return "D3DFMT_UNKNOWN";
342 case D3DFMT_R8G8B8: return "D3DFMT_R8G8B8";
343 case D3DFMT_A8R8G8B8: return "D3DFMT_A8R8G8B8";
344 case D3DFMT_X8R8G8B8: return "D3DFMT_X8R8G8B8";
345 case D3DFMT_R5G6B5: return "D3DFMT_R5G6B5";
346 case D3DFMT_X1R5G5B5: return "D3DFMT_X1R5G5B5";
347 case D3DFMT_A1R5G5B5: return "D3DFMT_A1R5G5B5";
348 case D3DFMT_A4R4G4B4: return "D3DFMT_A4R4G4B4";
349 case D3DFMT_R3G3B2: return "D3DFMT_R3G3B2";
350 case D3DFMT_A8: return "D3DFMT_A8";
351 case D3DFMT_A8R3G3B2: return "D3DFMT_A8R3G3B2";
352 case D3DFMT_X4R4G4B4: return "D3DFMT_X4R4G4B4";
353 case D3DFMT_A2B10G10R10: return "D3DFMT_A2B10G10R10";
354 case D3DFMT_A8B8G8R8: return "D3DFMT_A8B8G8R8";
355 case D3DFMT_X8B8G8R8: return "D3DFMT_X8B8G8R8";
356 case D3DFMT_G16R16: return "D3DFMT_G16R16";
357 case D3DFMT_A2R10G10B10: return "D3DFMT_A2R10G10B10";
358 case D3DFMT_A16B16G16R16: return "D3DFMT_A16B16G16R16";
359 case D3DFMT_A8P8: return "D3DFMT_A8P8";
360 case D3DFMT_P8: return "D3DFMT_P8";
361 case D3DFMT_L8: return "D3DFMT_L8";
362 case D3DFMT_A8L8: return "D3DFMT_A8L8";
363 case D3DFMT_A4L4: return "D3DFMT_A4L4";
364 case D3DFMT_V8U8: return "D3DFMT_V8U8";
365 case D3DFMT_L6V5U5: return "D3DFMT_L6V5U5";
366 case D3DFMT_X8L8V8U8: return "D3DFMT_X8L8V8U8";
367 case D3DFMT_Q8W8V8U8: return "D3DFMT_Q8W8V8U8";
368 case D3DFMT_V16U16: return "D3DFMT_V16U16";
369 case D3DFMT_A2W10V10U10: return "D3DFMT_A2W10V10U10";
370 case D3DFMT_UYVY: return "D3DFMT_UYVY";
371 case D3DFMT_R8G8_B8G8: return "D3DFMT_R8G8_B8G8";
372 case D3DFMT_YUY2: return "D3DFMT_YUY2";
373 case D3DFMT_G8R8_G8B8: return "D3DFMT_G8R8_G8B8";
374 case D3DFMT_DXT1: return "D3DFMT_DXT1";
375 case D3DFMT_DXT2: return "D3DFMT_DXT2";
376 case D3DFMT_DXT3: return "D3DFMT_DXT3";
377 case D3DFMT_DXT4: return "D3DFMT_DXT4";
378 case D3DFMT_DXT5: return "D3DFMT_DXT5";
379 case D3DFMT_ATI1: return "D3DFMT_ATI1";
380 case D3DFMT_ATI2: return "D3DFMT_ATI2";
381 case D3DFMT_D16_LOCKABLE: return "D3DFMT_D16_LOCKABLE";
382 case D3DFMT_D32: return "D3DFMT_D32";
383 case D3DFMT_D15S1: return "D3DFMT_D15S1";
384 case D3DFMT_D24S8: return "D3DFMT_D24S8";
385 case D3DFMT_D24X8: return "D3DFMT_D24X8";
386 case D3DFMT_D24X4S4: return "D3DFMT_D24X4S4";
387 case D3DFMT_D16: return "D3DFMT_D16";
388 case D3DFMT_D32F_LOCKABLE: return "D3DFMT_D32F_LOCKABLE";
389 case D3DFMT_D24FS8: return "D3DFMT_D24FS8";
390 case D3DFMT_D32_LOCKABLE: return "D3DFMT_D32_LOCKABLE";
391 case D3DFMT_S8_LOCKABLE: return "D3DFMT_S8_LOCKABLE";
392 case D3DFMT_L16: return "D3DFMT_L16";
393 case D3DFMT_VERTEXDATA: return "D3DFMT_VERTEXDATA";
394 case D3DFMT_INDEX16: return "D3DFMT_INDEX16";
395 case D3DFMT_INDEX32: return "D3DFMT_INDEX32";
396 case D3DFMT_Q16W16V16U16: return "D3DFMT_Q16W16V16U16";
397 case D3DFMT_MULTI2_ARGB8: return "D3DFMT_MULTI2_ARGB8";
398 case D3DFMT_R16F: return "D3DFMT_R16F";
399 case D3DFMT_G16R16F: return "D3DFMT_G16R16F";
400 case D3DFMT_A16B16G16R16F: return "D3DFMT_A16B16G16R16F";
401 case D3DFMT_R32F: return "D3DFMT_R32F";
402 case D3DFMT_G32R32F: return "D3DFMT_G32R32F";
403 case D3DFMT_A32B32G32R32F: return "D3DFMT_A32B32G32R32F";
404 case D3DFMT_CxV8U8: return "D3DFMT_CxV8U8";
405 case D3DFMT_A1: return "D3DFMT_A1";
406 case D3DFMT_A2B10G10R10_XR_BIAS: return "D3DFMT_A2B10G10R10_XR_BIAS";
407 case D3DFMT_BINARYBUFFER: return "D3DFMT_BINARYBUFFER";
408 case D3DFMT_DF16: return "D3DFMT_DF16";
409 case D3DFMT_DF24: return "D3DFMT_DF24";
410 case D3DFMT_INTZ: return "D3DFMT_INTZ";
411 case D3DFMT_NVDB: return "D3DFMT_NVDB";
412 case D3DFMT_RESZ: return "D3DFMT_RESZ";
413 case D3DFMT_NULL: return "D3DFMT_NULL";
414 default:
415 break;
416 }
417 return "Unknown";
418 }
419
420 static INLINE unsigned
421 nine_fvf_stride( DWORD fvf )
422 {
423 unsigned texcount, i, size = 0;
424
425 switch (fvf & D3DFVF_POSITION_MASK) {
426 case D3DFVF_XYZ: size += 3*4; break;
427 case D3DFVF_XYZRHW: size += 4*4; break;
428 case D3DFVF_XYZB1: size += 4*4; break;
429 case D3DFVF_XYZB2: size += 5*4; break;
430 case D3DFVF_XYZB3: size += 6*4; break;
431 case D3DFVF_XYZB4: size += 7*4; break;
432 case D3DFVF_XYZB5: size += 8*4; break;
433 case D3DFVF_XYZW: size += 4*4; break;
434 default:
435 user_warn("Position doesn't match any known combination.");
436 break;
437 }
438
439 if (fvf & D3DFVF_NORMAL) { size += 3*4; }
440 if (fvf & D3DFVF_PSIZE) { size += 1*4; }
441 if (fvf & D3DFVF_DIFFUSE) { size += 1*4; }
442 if (fvf & D3DFVF_SPECULAR) { size += 1*4; }
443
444 texcount = (fvf >> D3DFVF_TEXCOUNT_SHIFT) & D3DFVF_TEXCOUNT_MASK;
445 if (user_error(texcount <= 8))
446 texcount = 8;
447
448 for (i = 0; i < texcount; ++i) {
449 unsigned texformat = (fvf>>(16+i*2))&0x3;
450 /* texformats are defined having been shifted around so 1=3,2=0,3=1,4=2
451 * meaning we can just do this instead of the switch below */
452 size += (((texformat+1)&0x3)+1)*4;
453
454 /*
455 switch (texformat) {
456 case D3DFVF_TEXTUREFORMAT1: size += 1*4;
457 case D3DFVF_TEXTUREFORMAT2: size += 2*4;
458 case D3DFVF_TEXTUREFORMAT3: size += 3*4;
459 case D3DFVF_TEXTUREFORMAT4: size += 4*4;
460 }
461 */
462 }
463
464 return size;
465 }
466
467 static INLINE void
468 d3dcolor_to_rgba(float *rgba, D3DCOLOR color)
469 {
470 rgba[0] = (float)((color >> 16) & 0xFF) / 0xFF;
471 rgba[1] = (float)((color >> 8) & 0xFF) / 0xFF;
472 rgba[2] = (float)((color >> 0) & 0xFF) / 0xFF;
473 rgba[3] = (float)((color >> 24) & 0xFF) / 0xFF;
474 }
475
476 static INLINE void
477 d3dcolor_to_pipe_color_union(union pipe_color_union *rgba, D3DCOLOR color)
478 {
479 d3dcolor_to_rgba(&rgba->f[0], color);
480 }
481
482 static INLINE unsigned
483 d3dprimitivetype_to_pipe_prim(D3DPRIMITIVETYPE prim)
484 {
485 switch (prim) {
486 case D3DPT_POINTLIST: return PIPE_PRIM_POINTS;
487 case D3DPT_LINELIST: return PIPE_PRIM_LINES;
488 case D3DPT_LINESTRIP: return PIPE_PRIM_LINE_STRIP;
489 case D3DPT_TRIANGLELIST: return PIPE_PRIM_TRIANGLES;
490 case D3DPT_TRIANGLESTRIP: return PIPE_PRIM_TRIANGLE_STRIP;
491 case D3DPT_TRIANGLEFAN: return PIPE_PRIM_TRIANGLE_FAN;
492 default:
493 assert(0);
494 return PIPE_PRIM_POINTS;
495 }
496 }
497
498 static INLINE unsigned
499 prim_count_to_vertex_count(D3DPRIMITIVETYPE prim, UINT count)
500 {
501 switch (prim) {
502 case D3DPT_POINTLIST: return count;
503 case D3DPT_LINELIST: return count * 2;
504 case D3DPT_LINESTRIP: return count + 1;
505 case D3DPT_TRIANGLELIST: return count * 3;
506 case D3DPT_TRIANGLESTRIP: return count + 2;
507 case D3DPT_TRIANGLEFAN: return count + 2;
508 default:
509 assert(0);
510 return 0;
511 }
512 }
513
514 static INLINE unsigned
515 d3dcmpfunc_to_pipe_func(D3DCMPFUNC func)
516 {
517 switch (func) {
518 case D3DCMP_NEVER: return PIPE_FUNC_NEVER;
519 case D3DCMP_LESS: return PIPE_FUNC_LESS;
520 case D3DCMP_EQUAL: return PIPE_FUNC_EQUAL;
521 case D3DCMP_LESSEQUAL: return PIPE_FUNC_LEQUAL;
522 case D3DCMP_GREATER: return PIPE_FUNC_GREATER;
523 case D3DCMP_NOTEQUAL: return PIPE_FUNC_NOTEQUAL;
524 case D3DCMP_GREATEREQUAL: return PIPE_FUNC_GEQUAL;
525 case D3DCMP_ALWAYS: return PIPE_FUNC_ALWAYS;
526 case D3DCMP_NEVER_ZERO: return PIPE_FUNC_NEVER; // Tested on windows + ATI HD5770
527 default:
528 assert(0);
529 return PIPE_FUNC_NEVER;
530 }
531 }
532
533 static INLINE unsigned
534 d3dstencilop_to_pipe_stencil_op(D3DSTENCILOP op)
535 {
536 switch (op) {
537 case D3DSTENCILOP_KEEP: return PIPE_STENCIL_OP_KEEP;
538 case D3DSTENCILOP_ZERO: return PIPE_STENCIL_OP_ZERO;
539 case D3DSTENCILOP_REPLACE: return PIPE_STENCIL_OP_REPLACE;
540 case D3DSTENCILOP_INCRSAT: return PIPE_STENCIL_OP_INCR;
541 case D3DSTENCILOP_DECRSAT: return PIPE_STENCIL_OP_DECR;
542 case D3DSTENCILOP_INVERT: return PIPE_STENCIL_OP_INVERT;
543 case D3DSTENCILOP_INCR: return PIPE_STENCIL_OP_INCR_WRAP;
544 case D3DSTENCILOP_DECR: return PIPE_STENCIL_OP_DECR_WRAP;
545 default:
546 return PIPE_STENCIL_OP_ZERO;
547 }
548 }
549
550 static INLINE unsigned
551 d3dcull_to_pipe_face(D3DCULL cull)
552 {
553 switch (cull) {
554 case D3DCULL_NONE: return PIPE_FACE_NONE;
555 case D3DCULL_CW: return PIPE_FACE_FRONT;
556 case D3DCULL_CCW: return PIPE_FACE_BACK;
557 default:
558 assert(0);
559 return PIPE_FACE_NONE;
560 }
561 }
562
563 static INLINE unsigned
564 d3dfillmode_to_pipe_polygon_mode(D3DFILLMODE mode)
565 {
566 switch (mode) {
567 case D3DFILL_POINT: return PIPE_POLYGON_MODE_POINT;
568 case D3DFILL_WIREFRAME: return PIPE_POLYGON_MODE_LINE;
569 case D3DFILL_SOLID: return PIPE_POLYGON_MODE_FILL;
570 case D3DFILL_SOLID_ZERO:return PIPE_POLYGON_MODE_FILL;
571 default:
572 assert(0);
573 return PIPE_POLYGON_MODE_FILL;
574 }
575 }
576
577 static INLINE unsigned
578 d3dblendop_to_pipe_blend(D3DBLENDOP op)
579 {
580 switch (op) {
581 case D3DBLENDOP_ADD: return PIPE_BLEND_ADD;
582 case D3DBLENDOP_SUBTRACT: return PIPE_BLEND_SUBTRACT;
583 case D3DBLENDOP_REVSUBTRACT: return PIPE_BLEND_REVERSE_SUBTRACT;
584 case D3DBLENDOP_MIN: return PIPE_BLEND_MIN;
585 case D3DBLENDOP_MAX: return PIPE_BLEND_MAX;
586 default:
587 assert(0);
588 return PIPE_BLEND_ADD;
589 }
590 }
591
592 /* NOTE: The COLOR factors for are equal to the ALPHA ones for alpha.
593 * Drivers may check RGB and ALPHA factors for equality so we should not
594 * simply substitute the ALPHA variants.
595 */
596 static INLINE unsigned
597 d3dblend_alpha_to_pipe_blendfactor(D3DBLEND b)
598 {
599 switch (b) {
600 case D3DBLEND_ZERO: return PIPE_BLENDFACTOR_ZERO;
601 case D3DBLEND_ONE: return PIPE_BLENDFACTOR_ONE;
602 case D3DBLEND_SRCCOLOR: return PIPE_BLENDFACTOR_SRC_COLOR/*ALPHA*/;
603 case D3DBLEND_INVSRCCOLOR: return PIPE_BLENDFACTOR_INV_SRC_COLOR/*ALPHA*/;
604 case D3DBLEND_SRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
605 case D3DBLEND_INVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
606 case D3DBLEND_DESTALPHA: return PIPE_BLENDFACTOR_DST_ALPHA;
607 case D3DBLEND_INVDESTALPHA: return PIPE_BLENDFACTOR_INV_DST_ALPHA;
608 case D3DBLEND_DESTCOLOR: return PIPE_BLENDFACTOR_DST_COLOR/*ALPHA*/;
609 case D3DBLEND_INVDESTCOLOR: return PIPE_BLENDFACTOR_INV_DST_COLOR/*ALPHA*/;
610 case D3DBLEND_SRCALPHASAT: return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
611 case D3DBLEND_BOTHSRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
612 case D3DBLEND_BOTHINVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
613 case D3DBLEND_BLENDFACTOR: return PIPE_BLENDFACTOR_CONST_COLOR/*ALPHA*/;
614 case D3DBLEND_INVBLENDFACTOR: return PIPE_BLENDFACTOR_INV_CONST_COLOR/*ALPHA*/;
615 case D3DBLEND_SRCCOLOR2: return PIPE_BLENDFACTOR_ONE; /* XXX */
616 case D3DBLEND_INVSRCCOLOR2: return PIPE_BLENDFACTOR_ZERO; /* XXX */
617 default:
618 DBG_FLAG(DBG_UNKNOWN, "Unhandled blend factor %d\n", b);
619 return PIPE_BLENDFACTOR_ZERO;
620 }
621 }
622
623 static INLINE unsigned
624 d3dblend_color_to_pipe_blendfactor(D3DBLEND b)
625 {
626 switch (b) {
627 case D3DBLEND_ZERO: return PIPE_BLENDFACTOR_ZERO;
628 case D3DBLEND_ONE: return PIPE_BLENDFACTOR_ONE;
629 case D3DBLEND_SRCCOLOR: return PIPE_BLENDFACTOR_SRC_COLOR;
630 case D3DBLEND_INVSRCCOLOR: return PIPE_BLENDFACTOR_INV_SRC_COLOR;
631 case D3DBLEND_SRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
632 case D3DBLEND_INVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
633 case D3DBLEND_DESTALPHA: return PIPE_BLENDFACTOR_DST_ALPHA;
634 case D3DBLEND_INVDESTALPHA: return PIPE_BLENDFACTOR_INV_DST_ALPHA;
635 case D3DBLEND_DESTCOLOR: return PIPE_BLENDFACTOR_DST_COLOR;
636 case D3DBLEND_INVDESTCOLOR: return PIPE_BLENDFACTOR_INV_DST_COLOR;
637 case D3DBLEND_SRCALPHASAT: return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
638 case D3DBLEND_BOTHSRCALPHA: return PIPE_BLENDFACTOR_SRC_ALPHA;
639 case D3DBLEND_BOTHINVSRCALPHA: return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
640 case D3DBLEND_BLENDFACTOR: return PIPE_BLENDFACTOR_CONST_COLOR;
641 case D3DBLEND_INVBLENDFACTOR: return PIPE_BLENDFACTOR_INV_CONST_COLOR;
642 case D3DBLEND_SRCCOLOR2: return PIPE_BLENDFACTOR_SRC1_COLOR;
643 case D3DBLEND_INVSRCCOLOR2: return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
644 default:
645 DBG_FLAG(DBG_UNKNOWN, "Unhandled blend factor %d\n", b);
646 return PIPE_BLENDFACTOR_ZERO;
647 }
648 }
649
650 static INLINE unsigned
651 d3dtextureaddress_to_pipe_tex_wrap(D3DTEXTUREADDRESS addr)
652 {
653 switch (addr) {
654 case D3DTADDRESS_WRAP: return PIPE_TEX_WRAP_REPEAT;
655 case D3DTADDRESS_MIRROR: return PIPE_TEX_WRAP_MIRROR_REPEAT;
656 case D3DTADDRESS_CLAMP: return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
657 case D3DTADDRESS_BORDER: return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
658 case D3DTADDRESS_MIRRORONCE: return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
659 default:
660 assert(0);
661 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
662 }
663 }
664
665 static INLINE unsigned
666 d3dtexturefiltertype_to_pipe_tex_filter(D3DTEXTUREFILTERTYPE filter)
667 {
668 switch (filter) {
669 case D3DTEXF_POINT: return PIPE_TEX_FILTER_NEAREST;
670 case D3DTEXF_LINEAR: return PIPE_TEX_FILTER_LINEAR;
671 case D3DTEXF_ANISOTROPIC: return PIPE_TEX_FILTER_LINEAR;
672
673 case D3DTEXF_NONE:
674 case D3DTEXF_PYRAMIDALQUAD:
675 case D3DTEXF_GAUSSIANQUAD:
676 case D3DTEXF_CONVOLUTIONMONO:
677 default:
678 assert(0);
679 return PIPE_TEX_FILTER_NEAREST;
680 }
681 }
682
683 static INLINE unsigned
684 d3dtexturefiltertype_to_pipe_tex_mipfilter(D3DTEXTUREFILTERTYPE filter)
685 {
686 switch (filter) {
687 case D3DTEXF_NONE: return PIPE_TEX_MIPFILTER_NONE;
688 case D3DTEXF_POINT: return PIPE_TEX_FILTER_NEAREST;
689 case D3DTEXF_LINEAR: return PIPE_TEX_FILTER_LINEAR;
690 case D3DTEXF_ANISOTROPIC: return PIPE_TEX_FILTER_LINEAR;
691
692 case D3DTEXF_PYRAMIDALQUAD:
693 case D3DTEXF_GAUSSIANQUAD:
694 case D3DTEXF_CONVOLUTIONMONO:
695 default:
696 assert(0);
697 return PIPE_TEX_MIPFILTER_NONE;
698 }
699 }
700
701 #endif /* _NINE_PIPE_H_ */