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