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