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