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#include "adapter9.h"
24#include "device9ex.h"
25#include "nine_helpers.h"
26#include "nine_defines.h"
27#include "nine_pipe.h"
28#include "nine_dump.h"
29#include "util/u_math.h"
30#include "util/u_format.h"
31#include "util/u_dump.h"
32
33#include "pipe/p_screen.h"
34
35#define DBG_CHANNEL DBG_ADAPTER
36
37HRESULT
38NineAdapter9_ctor( struct NineAdapter9 *This,
39                   struct NineUnknownParams *pParams,
40                   struct d3dadapter9_context *pCTX )
41{
42    struct pipe_screen *hal = pCTX->hal;
43    HRESULT hr = NineUnknown_ctor(&This->base, pParams);
44    if (FAILED(hr)) { return hr; }
45
46    DBG("This=%p pParams=%p pCTX=%p\n", This, pParams, pCTX);
47    nine_dump_D3DADAPTER_IDENTIFIER9(DBG_CHANNEL, &pCTX->identifier);
48
49    This->ctx = pCTX;
50    if (!hal->get_param(hal, PIPE_CAP_CLIP_HALFZ)) {
51        ERR("Driver doesn't support d3d9 coordinates\n");
52        return D3DERR_DRIVERINTERNALERROR;
53    }
54    if (This->ctx->ref &&
55        !This->ctx->ref->get_param(This->ctx->ref, PIPE_CAP_CLIP_HALFZ)) {
56        ERR("Warning: Sotware rendering driver doesn't support d3d9 coordinates\n");
57    }
58    /* Old cards had tricks to bypass some restrictions to implement
59     * everything and fit tight the requirements: number of constants,
60     * number of temp registers, special behaviours, etc. Since we don't
61     * have access to all this, we need a bit more than what dx9 required.
62     * For example we have to use more than 32 temp registers to emulate
63     * behaviours, while some dx9 hw don't have more. As for sm2 hardware,
64     * we could support vs2 / ps2 for them but it needs some more care, and
65     * as these are very old, we choose to drop support for them */
66
67    /* checks minimum requirements, most are vs3/ps3 strict requirements */
68    if (!hal->get_param(hal, PIPE_CAP_SM3) ||
69        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
70                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 256 * sizeof(float[4]) ||
71        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
72                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 244 * sizeof(float[4]) ||
73        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
74                              PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
75        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
76                              PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
77        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
78                              PIPE_SHADER_CAP_MAX_INPUTS) < 16 ||
79        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
80                              PIPE_SHADER_CAP_MAX_INPUTS) < 10 ||
81        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
82                              PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS) < 16) {
83        ERR("Your card is not supported by Gallium Nine. Minimum requirement "
84            "is >= r500, >= nv50, >= i965\n");
85        return D3DERR_DRIVERINTERNALERROR;
86    }
87    /* for r500 */
88    if (hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
89                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 276 * sizeof(float[4]) || /* we put bool and int constants with float constants */
90        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
91                              PIPE_SHADER_CAP_MAX_TEMPS) < 40 || /* we use some more temp registers */
92        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
93                              PIPE_SHADER_CAP_MAX_TEMPS) < 40 ||
94        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
95                              PIPE_SHADER_CAP_MAX_INPUTS) < 20) /* we don't pack inputs as much as we could */
96        ERR("Your card is at the limit of Gallium Nine requirements. Some games "
97            "may run into issues because requirements are too tight\n");
98    return D3D_OK;
99}
100
101void
102NineAdapter9_dtor( struct NineAdapter9 *This )
103{
104    struct d3dadapter9_context *ctx = This->ctx;
105
106    DBG("This=%p\n", This);
107
108    NineUnknown_dtor(&This->base);
109
110    /* special case, call backend-specific dtor AFTER destroying this object
111     * completely. */
112    if (ctx) {
113        if (ctx->destroy) { ctx->destroy(ctx); }
114    }
115}
116
117static HRESULT
118NineAdapter9_GetScreen( struct NineAdapter9 *This,
119                        D3DDEVTYPE DevType,
120                        struct pipe_screen **ppScreen )
121{
122    const char *force_sw = getenv("D3D_ALWAYS_SOFTWARE");
123    switch (DevType) {
124        case D3DDEVTYPE_HAL:
125            if (force_sw && !strcmp(force_sw, "1") && This->ctx->ref) {
126                *ppScreen = This->ctx->ref;
127                break;
128            }
129            *ppScreen = This->ctx->hal;
130            break;
131
132        case D3DDEVTYPE_REF:
133        case D3DDEVTYPE_NULLREF:
134        case D3DDEVTYPE_SW:
135            if (force_sw && !strcmp(force_sw, "0")) {
136                *ppScreen = This->ctx->hal;
137                break;
138            }
139            *ppScreen = This->ctx->ref;
140            break;
141
142        default:
143            user_assert(!"Invalid device type", D3DERR_INVALIDCALL);
144    }
145
146    if (!*ppScreen) { return D3DERR_NOTAVAILABLE; }
147
148    return D3D_OK;
149}
150
151HRESULT NINE_WINAPI
152NineAdapter9_GetAdapterIdentifier( struct NineAdapter9 *This,
153                                   DWORD Flags,
154                                   D3DADAPTER_IDENTIFIER9 *pIdentifier )
155{
156    DBG("This=%p Flags=%x pIdentifier=%p\n", This, Flags, pIdentifier);
157
158    /* regarding flags, MSDN has this to say:
159     *  Flags sets the WHQLLevel member of D3DADAPTER_IDENTIFIER9. Flags can be
160     *  set to either 0 or D3DENUM_WHQL_LEVEL. If D3DENUM_WHQL_LEVEL is
161     *  specified, this call can connect to the Internet to download new
162     *  Microsoft Windows Hardware Quality Labs (WHQL) certificates.
163     * so let's just ignore it. */
164    *pIdentifier = This->ctx->identifier;
165    return D3D_OK;
166}
167
168static inline boolean
169backbuffer_format( D3DFORMAT dfmt,
170                   D3DFORMAT bfmt,
171                   boolean win )
172{
173    if (dfmt == D3DFMT_A2R10G10B10 && win) { return FALSE; }
174
175    if ((dfmt == D3DFMT_A2R10G10B10 && bfmt == dfmt) ||
176        (dfmt == D3DFMT_X8R8G8B8 && (bfmt == dfmt ||
177                                     bfmt == D3DFMT_A8R8G8B8)) ||
178        (dfmt == D3DFMT_X1R5G5B5 && (bfmt == dfmt ||
179                                     bfmt == D3DFMT_A1R5G5B5)) ||
180        (dfmt == D3DFMT_R5G6B5 && bfmt == dfmt)) {
181        return TRUE;
182    }
183
184    return FALSE;
185}
186
187HRESULT NINE_WINAPI
188NineAdapter9_CheckDeviceType( struct NineAdapter9 *This,
189                              D3DDEVTYPE DevType,
190                              D3DFORMAT AdapterFormat,
191                              D3DFORMAT BackBufferFormat,
192                              BOOL bWindowed )
193{
194    struct pipe_screen *screen;
195    enum pipe_format dfmt, bfmt;
196    HRESULT hr;
197
198    DBG("This=%p DevType=%s AdapterFormat=%s BackBufferFormat=%s "
199        "bWindowed=%i\n", This, nine_D3DDEVTYPE_to_str(DevType),
200        d3dformat_to_string(AdapterFormat),
201        d3dformat_to_string(BackBufferFormat), bWindowed);
202
203    user_assert(backbuffer_format(AdapterFormat, BackBufferFormat, bWindowed),
204                D3DERR_NOTAVAILABLE);
205
206    hr = NineAdapter9_GetScreen(This, DevType, &screen);
207    if (FAILED(hr)) { return hr; }
208
209    /* The display format is not handled in Nine. We always present an XRGB8888
210     * buffer (and the display server will eventually do the conversion). We probably
211     * don't need to check for anything for the adapter format support, since if the
212     * display server advertise support, it will likely be able to do the conversion.
213     * We do the approximation that a format is available in the display server if
214     * the format passes with NINE_BIND_BACKBUFFER_FLAGS */
215    dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D,
216                                       1,
217                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
218    bfmt = d3d9_to_pipe_format_checked(screen, BackBufferFormat, PIPE_TEXTURE_2D,
219                                       1,
220                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
221    if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
222        DBG("Unsupported Adapter/BackBufferFormat.\n");
223        return D3DERR_NOTAVAILABLE;
224    }
225
226    return D3D_OK;
227}
228
229static inline boolean
230display_format( D3DFORMAT fmt,
231                boolean win )
232{
233    /* http://msdn.microsoft.com/en-us/library/bb172558(v=VS.85).aspx#BackBuffer_or_Display_Formats */
234    static const D3DFORMAT allowed[] = {
235        D3DFMT_A2R10G10B10,
236        D3DFMT_X8R8G8B8,
237        D3DFMT_X1R5G5B5,
238        D3DFMT_R5G6B5,
239    };
240    unsigned i;
241
242    if (fmt == D3DFMT_A2R10G10B10 && win) { return FALSE; }
243
244    for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
245        if (fmt == allowed[i]) { return TRUE; }
246    }
247    return FALSE;
248}
249
250HRESULT NINE_WINAPI
251NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
252                                D3DDEVTYPE DeviceType,
253                                D3DFORMAT AdapterFormat,
254                                DWORD Usage,
255                                D3DRESOURCETYPE RType,
256                                D3DFORMAT CheckFormat )
257{
258    struct pipe_screen *screen;
259    HRESULT hr;
260    enum pipe_format pf;
261    enum pipe_texture_target target;
262    unsigned bind = 0;
263    boolean srgb;
264
265    /* Check adapter format. */
266
267    DBG("This=%p DeviceType=%s AdapterFormat=%s\n", This,
268        nine_D3DDEVTYPE_to_str(DeviceType), d3dformat_to_string(AdapterFormat));
269    DBG("Usage=%x RType=%u CheckFormat=%s\n", Usage, RType,
270        d3dformat_to_string(CheckFormat));
271
272    user_assert(display_format(AdapterFormat, FALSE), D3DERR_INVALIDCALL);
273
274    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
275    if (FAILED(hr))
276        return hr;
277    pf = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
278                                     PIPE_BIND_DISPLAY_TARGET |
279                                     PIPE_BIND_SHARED, FALSE, FALSE);
280    if (pf == PIPE_FORMAT_NONE) {
281        DBG("AdapterFormat %s not available.\n",
282            d3dformat_to_string(AdapterFormat));
283        return D3DERR_NOTAVAILABLE;
284    }
285
286    /* Check actual format. */
287
288    switch (RType) {
289    case D3DRTYPE_SURFACE:       target = PIPE_TEXTURE_2D; break;
290    case D3DRTYPE_TEXTURE:       target = PIPE_TEXTURE_2D; break;
291    case D3DRTYPE_CUBETEXTURE:   target = PIPE_TEXTURE_CUBE; break;
292    case D3DRTYPE_VOLUME:        target = PIPE_TEXTURE_3D; break;
293    case D3DRTYPE_VOLUMETEXTURE: target = PIPE_TEXTURE_3D; break;
294    case D3DRTYPE_VERTEXBUFFER:  target = PIPE_BUFFER; break;
295    case D3DRTYPE_INDEXBUFFER:   target = PIPE_BUFFER; break;
296    default:
297        user_assert(0, D3DERR_INVALIDCALL);
298    }
299
300    bind = 0;
301    if (Usage & D3DUSAGE_RENDERTARGET) bind |= PIPE_BIND_RENDER_TARGET;
302    if (Usage & D3DUSAGE_DEPTHSTENCIL) {
303        if (!depth_stencil_format(CheckFormat))
304            return D3DERR_NOTAVAILABLE;
305        bind |= d3d9_get_pipe_depth_format_bindings(CheckFormat);
306    }
307
308    /* API hack because setting RT[0] to NULL is forbidden */
309    if (CheckFormat == D3DFMT_NULL && bind == PIPE_BIND_RENDER_TARGET &&
310        (RType == D3DRTYPE_SURFACE ||
311         RType == D3DRTYPE_TEXTURE))
312        return D3D_OK;
313
314    /* RESZ hack */
315    if (CheckFormat == D3DFMT_RESZ && bind == PIPE_BIND_RENDER_TARGET &&
316        RType == D3DRTYPE_SURFACE)
317        return screen->get_param(screen, PIPE_CAP_MULTISAMPLE_Z_RESOLVE) ?
318               D3D_OK : D3DERR_NOTAVAILABLE;
319
320    /* ATOC hack */
321    if (CheckFormat == D3DFMT_ATOC && RType == D3DRTYPE_SURFACE)
322        return D3D_OK;
323
324    if ((Usage & D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING) &&
325        (Usage & D3DUSAGE_RENDERTARGET))
326        bind |= PIPE_BIND_BLENDABLE;
327
328    if (Usage & D3DUSAGE_DMAP) {
329        DBG("D3DUSAGE_DMAP not available\n");
330        return D3DERR_NOTAVAILABLE; /* TODO: displacement mapping */
331    }
332
333    switch (RType) {
334    case D3DRTYPE_TEXTURE:       bind |= PIPE_BIND_SAMPLER_VIEW; break;
335    case D3DRTYPE_CUBETEXTURE:   bind |= PIPE_BIND_SAMPLER_VIEW; break;
336    case D3DRTYPE_VOLUMETEXTURE: bind |= PIPE_BIND_SAMPLER_VIEW; break;
337    case D3DRTYPE_VERTEXBUFFER:  bind |= PIPE_BIND_VERTEX_BUFFER; break;
338    case D3DRTYPE_INDEXBUFFER:   bind |= PIPE_BIND_INDEX_BUFFER; break;
339    case D3DRTYPE_SURFACE:
340        if (!(Usage & D3DUSAGE_DEPTHSTENCIL))
341            bind |= PIPE_BIND_SAMPLER_VIEW; /* StretchRect */
342        /* Offscreen surface support: Usage = 0.
343         * In practice drivers are very restrictive on the formats supported.
344         * Basically a few common formats + YUV and compressed formats. The
345         * reason is that offscreen surface are useful only for directdraw
346         * compatibility (a WONTIMPL of nine) + format conversion (useful in
347         * particular for YUV because the format was not advertised for textures
348         * on NV chips). */
349        if (Usage == 0)
350            bind |= PIPE_BIND_RENDER_TARGET; /* A current requirement of our impl, which we should get rid of. */
351    default:
352        break;
353    }
354
355
356    srgb = (Usage & (D3DUSAGE_QUERY_SRGBREAD | D3DUSAGE_QUERY_SRGBWRITE)) != 0;
357    pf = d3d9_to_pipe_format_checked(screen, CheckFormat, target,
358                                     0, bind, srgb, FALSE);
359    if (pf == PIPE_FORMAT_NONE) {
360        DBG("NOT AVAILABLE\n");
361        return D3DERR_NOTAVAILABLE;
362    }
363
364    /* we support ATI1 and ATI2 hack only for 2D and Cube textures */
365    if (RType != D3DRTYPE_TEXTURE && RType != D3DRTYPE_CUBETEXTURE &&
366        (CheckFormat == D3DFMT_ATI1 || CheckFormat == D3DFMT_ATI2))
367        return D3DERR_NOTAVAILABLE;
368    /* if (Usage & D3DUSAGE_NONSECURE) { don't know the implications of this } */
369    /* if (Usage & D3DUSAGE_SOFTWAREPROCESSING) { we can always support this } */
370
371    if ((Usage & D3DUSAGE_AUTOGENMIPMAP) && !(bind & PIPE_BIND_SAMPLER_VIEW))
372        return D3DOK_NOAUTOGEN;
373    return D3D_OK;
374}
375
376HRESULT NINE_WINAPI
377NineAdapter9_CheckDeviceMultiSampleType( struct NineAdapter9 *This,
378                                         D3DDEVTYPE DeviceType,
379                                         D3DFORMAT SurfaceFormat,
380                                         BOOL Windowed,
381                                         D3DMULTISAMPLE_TYPE MultiSampleType,
382                                         DWORD *pQualityLevels )
383{
384    struct pipe_screen *screen;
385    HRESULT hr;
386    enum pipe_format pf;
387    unsigned bind;
388
389    DBG("This=%p DeviceType=%s SurfaceFormat=%s Windowed=%i MultiSampleType=%u "
390        "pQualityLevels=%p\n", This, nine_D3DDEVTYPE_to_str(DeviceType),
391        d3dformat_to_string(SurfaceFormat), Windowed, MultiSampleType,
392        pQualityLevels);
393
394    if (pQualityLevels) {
395        /* In error cases return only 1 quality level supported */
396        *pQualityLevels = 1;
397    }
398    user_assert(MultiSampleType <= D3DMULTISAMPLE_16_SAMPLES, D3DERR_INVALIDCALL);
399
400    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
401    if (FAILED(hr))
402        return hr;
403
404    if (depth_stencil_format(SurfaceFormat))
405        bind = d3d9_get_pipe_depth_format_bindings(SurfaceFormat);
406    else /* render-target */
407        bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
408
409    pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
410                                     0, PIPE_BIND_SAMPLER_VIEW, FALSE, FALSE);
411
412    if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
413        DBG("%s not available.\n", d3dformat_to_string(SurfaceFormat));
414        return D3DERR_INVALIDCALL;
415    }
416
417    pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
418                                     MultiSampleType, bind, FALSE, FALSE);
419
420    if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
421        DBG("%s with %u samples not available.\n",
422            d3dformat_to_string(SurfaceFormat), MultiSampleType);
423        return D3DERR_NOTAVAILABLE;
424    }
425
426    if (pQualityLevels) {
427        /* NONMASKABLE MultiSampleType might have more than one quality level,
428         * while MASKABLE MultiSampleTypes have only one level.
429         * Advertise quality levels and map each level to a sample count. */
430         (void ) d3dmultisample_type_check(screen, SurfaceFormat,
431                 &MultiSampleType, D3DMULTISAMPLE_16_SAMPLES, pQualityLevels);
432         DBG("advertising %u quality levels\n", *pQualityLevels);
433    }
434
435    return D3D_OK;
436}
437
438HRESULT NINE_WINAPI
439NineAdapter9_CheckDepthStencilMatch( struct NineAdapter9 *This,
440                                     D3DDEVTYPE DeviceType,
441                                     D3DFORMAT AdapterFormat,
442                                     D3DFORMAT RenderTargetFormat,
443                                     D3DFORMAT DepthStencilFormat )
444{
445    struct pipe_screen *screen;
446    enum pipe_format dfmt, bfmt, zsfmt;
447    HRESULT hr;
448
449    DBG("This=%p DeviceType=%s AdapterFormat=%s "
450        "RenderTargetFormat=%s DepthStencilFormat=%s\n", This,
451        nine_D3DDEVTYPE_to_str(DeviceType), d3dformat_to_string(AdapterFormat),
452        d3dformat_to_string(RenderTargetFormat),
453        d3dformat_to_string(DepthStencilFormat));
454
455    user_assert(display_format(AdapterFormat, FALSE), D3DERR_NOTAVAILABLE);
456    user_assert(depth_stencil_format(DepthStencilFormat), D3DERR_NOTAVAILABLE);
457
458    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
459    if (FAILED(hr)) { return hr; }
460
461    dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
462                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
463    bfmt = d3d9_to_pipe_format_checked(screen, RenderTargetFormat,
464                                       PIPE_TEXTURE_2D, 0,
465                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
466    if (RenderTargetFormat == D3DFMT_NULL)
467        bfmt = dfmt;
468    zsfmt = d3d9_to_pipe_format_checked(screen, DepthStencilFormat,
469                                        PIPE_TEXTURE_2D, 0,
470                                        d3d9_get_pipe_depth_format_bindings(DepthStencilFormat),
471                                        FALSE, FALSE);
472    if (dfmt == PIPE_FORMAT_NONE ||
473        bfmt == PIPE_FORMAT_NONE ||
474        zsfmt == PIPE_FORMAT_NONE) {
475        return D3DERR_NOTAVAILABLE;
476    }
477
478    return D3D_OK;
479}
480
481HRESULT NINE_WINAPI
482NineAdapter9_CheckDeviceFormatConversion( struct NineAdapter9 *This,
483                                          D3DDEVTYPE DeviceType,
484                                          D3DFORMAT SourceFormat,
485                                          D3DFORMAT TargetFormat )
486{
487    /* MSDN says this tests whether a certain backbuffer format can be used in
488     * conjunction with a certain front buffer format. It's a little confusing
489     * but some one wiser might be able to figure this one out. XXX */
490    struct pipe_screen *screen;
491    enum pipe_format dfmt, bfmt;
492    HRESULT hr;
493
494    DBG("This=%p DeviceType=%s SourceFormat=%s TargetFormat=%s\n", This,
495        nine_D3DDEVTYPE_to_str(DeviceType),
496        d3dformat_to_string(SourceFormat), d3dformat_to_string(TargetFormat));
497
498    user_assert(backbuffer_format(TargetFormat, SourceFormat, FALSE),
499                D3DERR_NOTAVAILABLE);
500
501    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
502    if (FAILED(hr)) { return hr; }
503
504    dfmt = d3d9_to_pipe_format_checked(screen, TargetFormat, PIPE_TEXTURE_2D, 1,
505                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
506    bfmt = d3d9_to_pipe_format_checked(screen, SourceFormat, PIPE_TEXTURE_2D, 1,
507                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
508
509    if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
510        DBG("%s to %s not supported.\n",
511            d3dformat_to_string(SourceFormat),
512            d3dformat_to_string(TargetFormat));
513        return D3DERR_NOTAVAILABLE;
514    }
515
516    return D3D_OK;
517}
518
519HRESULT NINE_WINAPI
520NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
521                            D3DDEVTYPE DeviceType,
522                            D3DCAPS9 *pCaps )
523{
524    struct pipe_screen *screen;
525    HRESULT hr;
526
527    DBG("This=%p DeviceType=%s pCaps=%p\n", This,
528        nine_D3DDEVTYPE_to_str(DeviceType), pCaps);
529
530    user_assert(pCaps, D3DERR_INVALIDCALL);
531
532    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
533    if (FAILED(hr)) {
534       DBG("Failed to get pipe_screen.\n");
535       return hr;
536    }
537
538#define D3DPIPECAP(pcap, d3dcap) \
539    (screen->get_param(screen, PIPE_CAP_##pcap) ? (d3dcap) : 0)
540
541#define D3DNPIPECAP(pcap, d3dcap) \
542    (screen->get_param(screen, PIPE_CAP_##pcap) ? 0 : (d3dcap))
543
544    pCaps->DeviceType = DeviceType;
545
546    pCaps->AdapterOrdinal = 0;
547
548    pCaps->Caps = D3DCAPS_READ_SCANLINE;
549
550    pCaps->Caps2 = /* D3DCAPS2_CANMANAGERESOURCE | */
551                /* D3DCAPS2_CANSHARERESOURCE | */
552                /* D3DCAPS2_CANCALIBRATEGAMMA | */
553                   D3DCAPS2_DYNAMICTEXTURES |
554                   D3DCAPS2_FULLSCREENGAMMA |
555                   D3DCAPS2_CANAUTOGENMIPMAP;
556
557    /* Note: D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD just means the
558     * backbuffer can be ARGB (instead of only XRGB) when we are fullscreen
559     * and in discard mode. */
560    pCaps->Caps3 = D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD |
561                   D3DCAPS3_COPY_TO_VIDMEM |
562                   D3DCAPS3_COPY_TO_SYSTEMMEM |
563                   D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION;
564
565    pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_DEFAULT |
566                                   D3DPRESENT_INTERVAL_ONE |
567                                   D3DPRESENT_INTERVAL_TWO |
568                                   D3DPRESENT_INTERVAL_THREE |
569                                   D3DPRESENT_INTERVAL_FOUR |
570                                   D3DPRESENT_INTERVAL_IMMEDIATE;
571    pCaps->CursorCaps = D3DCURSORCAPS_COLOR /* | D3DCURSORCAPS_LOWRES*/;
572
573    pCaps->DevCaps = D3DDEVCAPS_CANBLTSYSTONONLOCAL |
574                     D3DDEVCAPS_CANRENDERAFTERFLIP |
575                     D3DDEVCAPS_DRAWPRIMITIVES2 |
576                     D3DDEVCAPS_DRAWPRIMITIVES2EX |
577                     D3DDEVCAPS_DRAWPRIMTLVERTEX |
578                     D3DDEVCAPS_EXECUTESYSTEMMEMORY |
579                     D3DDEVCAPS_EXECUTEVIDEOMEMORY |
580                     D3DDEVCAPS_HWRASTERIZATION |
581                     D3DDEVCAPS_HWTRANSFORMANDLIGHT |
582                     /*D3DDEVCAPS_NPATCHES |*/
583                     D3DDEVCAPS_PUREDEVICE |
584                     /*D3DDEVCAPS_QUINTICRTPATCHES |*/
585                     /*D3DDEVCAPS_RTPATCHES |*/
586                     /*D3DDEVCAPS_RTPATCHHANDLEZERO |*/
587                     /*D3DDEVCAPS_SEPARATETEXTUREMEMORIES |*/
588                     D3DDEVCAPS_TEXTURENONLOCALVIDMEM |
589                     /* D3DDEVCAPS_TEXTURESYSTEMMEMORY |*/
590                     D3DDEVCAPS_TEXTUREVIDEOMEMORY |
591                     D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
592                     D3DDEVCAPS_TLVERTEXVIDEOMEMORY;
593
594    pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_MASKZ |
595                               D3DPMISCCAPS_CULLNONE | /* XXX */
596                               D3DPMISCCAPS_CULLCW |
597                               D3DPMISCCAPS_CULLCCW |
598                               D3DPMISCCAPS_COLORWRITEENABLE |
599                               D3DPMISCCAPS_CLIPPLANESCALEDPOINTS |
600                               /*D3DPMISCCAPS_CLIPTLVERTS |*/
601                               D3DPMISCCAPS_TSSARGTEMP |
602                               D3DPMISCCAPS_BLENDOP |
603                               D3DPIPECAP(INDEP_BLEND_ENABLE, D3DPMISCCAPS_INDEPENDENTWRITEMASKS) |
604                               D3DPMISCCAPS_PERSTAGECONSTANT |
605                               /*D3DPMISCCAPS_POSTBLENDSRGBCONVERT |*/ /* TODO: advertise if Ex and dx10 able card */
606                               D3DPMISCCAPS_FOGANDSPECULARALPHA | /* Note: documentation of the flag is wrong */
607                               D3DPIPECAP(BLEND_EQUATION_SEPARATE, D3DPMISCCAPS_SEPARATEALPHABLEND) |
608                               D3DPIPECAP(MIXED_COLORBUFFER_FORMATS, D3DPMISCCAPS_MRTINDEPENDENTBITDEPTHS) |
609                               D3DPMISCCAPS_MRTPOSTPIXELSHADERBLENDING |
610                               D3DPMISCCAPS_FOGVERTEXCLAMPED;
611    if (!screen->get_param(screen, PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION))
612        pCaps->PrimitiveMiscCaps |= D3DPMISCCAPS_CLIPTLVERTS;
613
614    pCaps->RasterCaps =
615        D3DPIPECAP(ANISOTROPIC_FILTER, D3DPRASTERCAPS_ANISOTROPY) |
616        D3DPRASTERCAPS_COLORPERSPECTIVE |
617        D3DPRASTERCAPS_DITHER |
618        D3DPRASTERCAPS_DEPTHBIAS |
619        D3DPRASTERCAPS_FOGRANGE |
620        D3DPRASTERCAPS_FOGTABLE |
621        D3DPRASTERCAPS_FOGVERTEX |
622        D3DPRASTERCAPS_MIPMAPLODBIAS |
623        D3DPRASTERCAPS_MULTISAMPLE_TOGGLE |
624        D3DPRASTERCAPS_SCISSORTEST |
625        D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
626        /*D3DPRASTERCAPS_WBUFFER |*/
627        D3DPRASTERCAPS_WFOG |
628        /*D3DPRASTERCAPS_ZBUFFERLESSHSR |*/
629        D3DPRASTERCAPS_ZFOG |
630        D3DPRASTERCAPS_ZTEST;
631
632    pCaps->ZCmpCaps = D3DPCMPCAPS_NEVER |
633                      D3DPCMPCAPS_LESS |
634                      D3DPCMPCAPS_EQUAL |
635                      D3DPCMPCAPS_LESSEQUAL |
636                      D3DPCMPCAPS_GREATER |
637                      D3DPCMPCAPS_NOTEQUAL |
638                      D3DPCMPCAPS_GREATEREQUAL |
639                      D3DPCMPCAPS_ALWAYS;
640
641    pCaps->SrcBlendCaps = D3DPBLENDCAPS_ZERO |
642                          D3DPBLENDCAPS_ONE |
643                          D3DPBLENDCAPS_SRCCOLOR |
644                          D3DPBLENDCAPS_INVSRCCOLOR |
645                          D3DPBLENDCAPS_SRCALPHA |
646                          D3DPBLENDCAPS_INVSRCALPHA |
647                          D3DPBLENDCAPS_DESTALPHA |
648                          D3DPBLENDCAPS_INVDESTALPHA |
649                          D3DPBLENDCAPS_DESTCOLOR |
650                          D3DPBLENDCAPS_INVDESTCOLOR |
651                          D3DPBLENDCAPS_SRCALPHASAT |
652                          D3DPBLENDCAPS_BOTHSRCALPHA |
653                          D3DPBLENDCAPS_BOTHINVSRCALPHA |
654                          D3DPBLENDCAPS_BLENDFACTOR |
655                          D3DPIPECAP(MAX_DUAL_SOURCE_RENDER_TARGETS,
656                              D3DPBLENDCAPS_INVSRCCOLOR2 |
657                              D3DPBLENDCAPS_SRCCOLOR2);
658
659    pCaps->DestBlendCaps = pCaps->SrcBlendCaps;
660
661    pCaps->AlphaCmpCaps = D3DPCMPCAPS_NEVER |
662                          D3DPCMPCAPS_LESS |
663                          D3DPCMPCAPS_EQUAL |
664                          D3DPCMPCAPS_LESSEQUAL |
665                          D3DPCMPCAPS_GREATER |
666                          D3DPCMPCAPS_NOTEQUAL |
667                          D3DPCMPCAPS_GREATEREQUAL |
668                          D3DPCMPCAPS_ALWAYS;
669
670    /* FLAT caps not legal for D3D9. */
671    pCaps->ShadeCaps = D3DPSHADECAPS_COLORGOURAUDRGB |
672                       D3DPSHADECAPS_SPECULARGOURAUDRGB |
673                       D3DPSHADECAPS_ALPHAGOURAUDBLEND |
674                       D3DPSHADECAPS_FOGGOURAUD;
675
676    pCaps->TextureCaps =
677        D3DPTEXTURECAPS_ALPHA |
678        D3DPTEXTURECAPS_ALPHAPALETTE |
679        D3DPTEXTURECAPS_PERSPECTIVE |
680        D3DPTEXTURECAPS_PROJECTED |
681        D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
682        D3DPTEXTURECAPS_CUBEMAP |
683        D3DPTEXTURECAPS_VOLUMEMAP |
684        D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_POW2) |
685        D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_NONPOW2CONDITIONAL) |
686        D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_CUBEMAP_POW2) |
687        D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_VOLUMEMAP_POW2) |
688        D3DPIPECAP(MAX_TEXTURE_2D_LEVELS, D3DPTEXTURECAPS_MIPMAP) |
689        D3DPIPECAP(MAX_TEXTURE_3D_LEVELS, D3DPTEXTURECAPS_MIPVOLUMEMAP) |
690        D3DPIPECAP(MAX_TEXTURE_CUBE_LEVELS, D3DPTEXTURECAPS_MIPCUBEMAP);
691
692    pCaps->TextureFilterCaps =
693        D3DPTFILTERCAPS_MINFPOINT |
694        D3DPTFILTERCAPS_MINFLINEAR |
695        D3DPIPECAP(ANISOTROPIC_FILTER, D3DPTFILTERCAPS_MINFANISOTROPIC) |
696        /*D3DPTFILTERCAPS_MINFPYRAMIDALQUAD |*/
697        /*D3DPTFILTERCAPS_MINFGAUSSIANQUAD |*/
698        D3DPTFILTERCAPS_MIPFPOINT |
699        D3DPTFILTERCAPS_MIPFLINEAR |
700        D3DPTFILTERCAPS_MAGFPOINT |
701        D3DPTFILTERCAPS_MAGFLINEAR |
702        D3DPIPECAP(ANISOTROPIC_FILTER, D3DPTFILTERCAPS_MAGFANISOTROPIC) |
703        /*D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD |*/
704        /*D3DPTFILTERCAPS_MAGFGAUSSIANQUAD*/0;
705
706    pCaps->CubeTextureFilterCaps = pCaps->TextureFilterCaps;
707    pCaps->VolumeTextureFilterCaps = pCaps->TextureFilterCaps;
708
709    pCaps->TextureAddressCaps =
710        D3DPTADDRESSCAPS_BORDER |
711        D3DPTADDRESSCAPS_INDEPENDENTUV |
712        D3DPTADDRESSCAPS_WRAP |
713        D3DPTADDRESSCAPS_MIRROR |
714        D3DPTADDRESSCAPS_CLAMP |
715        D3DPIPECAP(TEXTURE_MIRROR_CLAMP, D3DPTADDRESSCAPS_MIRRORONCE);
716
717    pCaps->VolumeTextureAddressCaps = pCaps->TextureAddressCaps;
718
719    pCaps->LineCaps =
720        D3DLINECAPS_ALPHACMP |
721        D3DLINECAPS_BLEND |
722        D3DLINECAPS_TEXTURE |
723        D3DLINECAPS_ZTEST |
724        D3DLINECAPS_FOG;
725    if (screen->get_paramf(screen, PIPE_CAPF_MAX_LINE_WIDTH_AA) > 0.0) {
726        pCaps->LineCaps |= D3DLINECAPS_ANTIALIAS;
727    }
728
729    pCaps->MaxTextureWidth =
730        1 << (screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
731    pCaps->MaxTextureHeight = pCaps->MaxTextureWidth;
732    pCaps->MaxVolumeExtent =
733        1 << (screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_3D_LEVELS) - 1);
734    /* XXX values from wine */
735    pCaps->MaxTextureRepeat = 32768;
736    pCaps->MaxTextureAspectRatio = pCaps->MaxTextureWidth;
737
738    pCaps->MaxAnisotropy =
739        (DWORD)screen->get_paramf(screen, PIPE_CAPF_MAX_TEXTURE_ANISOTROPY);
740
741    /* Values for GeForce 9600 GT */
742    pCaps->MaxVertexW = 1e10f;
743    pCaps->GuardBandLeft = -1e9f;
744    pCaps->GuardBandTop = -1e9f;
745    pCaps->GuardBandRight = 1e9f;
746    pCaps->GuardBandBottom = 1e9f;
747    pCaps->ExtentsAdjust = 0.0f;
748
749    pCaps->StencilCaps =
750        D3DSTENCILCAPS_KEEP |
751        D3DSTENCILCAPS_ZERO |
752        D3DSTENCILCAPS_REPLACE |
753        D3DSTENCILCAPS_INCRSAT |
754        D3DSTENCILCAPS_DECRSAT |
755        D3DSTENCILCAPS_INVERT |
756        D3DSTENCILCAPS_INCR |
757        D3DSTENCILCAPS_DECR |
758        D3DSTENCILCAPS_TWOSIDED;
759
760    pCaps->FVFCaps =
761        8 | /* 8 textures max */
762        /*D3DFVFCAPS_DONOTSTRIPELEMENTS |*/
763        D3DFVFCAPS_PSIZE;
764
765    pCaps->TextureOpCaps = D3DTEXOPCAPS_DISABLE |
766                           D3DTEXOPCAPS_SELECTARG1 |
767                           D3DTEXOPCAPS_SELECTARG2 |
768                           D3DTEXOPCAPS_MODULATE |
769                           D3DTEXOPCAPS_MODULATE2X |
770                           D3DTEXOPCAPS_MODULATE4X |
771                           D3DTEXOPCAPS_ADD |
772                           D3DTEXOPCAPS_ADDSIGNED |
773                           D3DTEXOPCAPS_ADDSIGNED2X |
774                           D3DTEXOPCAPS_SUBTRACT |
775                           D3DTEXOPCAPS_ADDSMOOTH |
776                           D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
777                           D3DTEXOPCAPS_BLENDTEXTUREALPHA |
778                           D3DTEXOPCAPS_BLENDFACTORALPHA |
779                           D3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
780                           D3DTEXOPCAPS_BLENDCURRENTALPHA |
781                           D3DTEXOPCAPS_PREMODULATE |
782                           D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
783                           D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
784                           D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
785                           D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
786                           D3DTEXOPCAPS_BUMPENVMAP |
787                           D3DTEXOPCAPS_BUMPENVMAPLUMINANCE |
788                           D3DTEXOPCAPS_DOTPRODUCT3 |
789                           D3DTEXOPCAPS_MULTIPLYADD |
790                           D3DTEXOPCAPS_LERP;
791
792    pCaps->MaxTextureBlendStages = 8; /* XXX wine */
793        (DWORD)screen->get_param(screen, PIPE_CAP_BLEND_EQUATION_SEPARATE);
794    pCaps->MaxSimultaneousTextures = 8;
795
796    pCaps->VertexProcessingCaps = D3DVTXPCAPS_TEXGEN |
797                                  D3DVTXPCAPS_TEXGEN_SPHEREMAP |
798                                  D3DVTXPCAPS_MATERIALSOURCE7 |
799                                  D3DVTXPCAPS_DIRECTIONALLIGHTS |
800                                  D3DVTXPCAPS_POSITIONALLIGHTS |
801                                  D3DVTXPCAPS_LOCALVIEWER |
802                                  D3DVTXPCAPS_TWEENING |
803                                  /*D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER*/0;
804
805    pCaps->MaxActiveLights = NINE_MAX_LIGHTS_ACTIVE; /* like GL_LIGHTi */
806    pCaps->MaxUserClipPlanes = PIPE_MAX_CLIP_PLANES;
807    pCaps->MaxVertexBlendMatrices = 4; /* 1 vec4 BLENDWEIGHT/INDICES input */
808    pCaps->MaxVertexBlendMatrixIndex = 8; /* D3DTS_WORLDMATRIX(0..8) */
809
810    pCaps->MaxPointSize = screen->get_paramf(screen, PIPE_CAPF_MAX_POINT_WIDTH);
811
812    pCaps->MaxPrimitiveCount = 0x555555; /* <- wine, really 0xFFFFFFFF; */
813    pCaps->MaxVertexIndex = 0xFFFFFF; /* <- wine, really 0xFFFFFFFF */
814    pCaps->MaxStreams =
815        _min(screen->get_shader_param(screen,
816                 PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_MAX_INPUTS),
817             16);
818
819    pCaps->MaxStreamStride = screen->get_param(screen,
820            PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE);
821
822    pCaps->VertexShaderVersion = D3DVS_VERSION(3,0);
823
824    /* VS 2 as well as 3.0 supports a minimum of 256 consts.
825     * Wine and d3d9 drivers for dx1x hw advertise 256. Just as them,
826     * advertise 256. Problem is with hw that can only do 256, because
827     * we need take a few slots for boolean and integer constants. For these
828     * we'll have to fail later if they use complex shaders. */
829    pCaps->MaxVertexShaderConst = NINE_MAX_CONST_F;
830
831    pCaps->PixelShaderVersion = D3DPS_VERSION(3,0);
832    /* Value for GeForce 9600 GT */
833    pCaps->PixelShader1xMaxValue = 65504.f;
834
835    pCaps->DevCaps2 = D3DDEVCAPS2_STREAMOFFSET |
836                      D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET |
837                      D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
838                      /*D3DDEVCAPS2_DMAPNPATCH |*/
839                      /*D3DDEVCAPS2_ADAPTIVETESSRTPATCH |*/
840                      /*D3DDEVCAPS2_ADAPTIVETESSNPATCH |*/
841                      /*D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH*/0;
842
843    pCaps->MasterAdapterOrdinal = 0;
844    pCaps->AdapterOrdinalInGroup = 0;
845    pCaps->NumberOfAdaptersInGroup = 1;
846
847    /* Undocumented ? */
848    pCaps->MaxNpatchTessellationLevel = 0.0f;
849    pCaps->Reserved5 = 0;
850
851    /* XXX: use is_format_supported */
852    pCaps->DeclTypes = D3DDTCAPS_UBYTE4 |
853                       D3DDTCAPS_UBYTE4N |
854                       D3DDTCAPS_SHORT2N |
855                       D3DDTCAPS_SHORT4N |
856                       D3DDTCAPS_USHORT2N |
857                       D3DDTCAPS_USHORT4N |
858                       D3DDTCAPS_UDEC3 |
859                       D3DDTCAPS_DEC3N |
860                       D3DDTCAPS_FLOAT16_2 |
861                       D3DDTCAPS_FLOAT16_4;
862
863    pCaps->NumSimultaneousRTs =
864        screen->get_param(screen, PIPE_CAP_MAX_RENDER_TARGETS);
865    if (pCaps->NumSimultaneousRTs > NINE_MAX_SIMULTANEOUS_RENDERTARGETS)
866        pCaps->NumSimultaneousRTs = NINE_MAX_SIMULTANEOUS_RENDERTARGETS;
867
868    pCaps->StretchRectFilterCaps = D3DPTFILTERCAPS_MINFPOINT |
869                                   D3DPTFILTERCAPS_MINFLINEAR |
870                                   D3DPTFILTERCAPS_MAGFPOINT |
871                                   D3DPTFILTERCAPS_MAGFLINEAR;
872
873
874    pCaps->VS20Caps.Caps = D3DVS20CAPS_PREDICATION;
875    pCaps->VS20Caps.DynamicFlowControlDepth = /* XXX is this dynamic ? */
876        screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
877                                 PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
878    pCaps->VS20Caps.NumTemps =
879        screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
880                                 PIPE_SHADER_CAP_MAX_TEMPS);
881    pCaps->VS20Caps.StaticFlowControlDepth = /* XXX is this static ? */
882        screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
883                                 PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
884
885    /* also check for values < 0, because get_shader_param may return unsigned */
886    if (pCaps->VS20Caps.DynamicFlowControlDepth > D3DVS20_MAX_DYNAMICFLOWCONTROLDEPTH
887        || pCaps->VS20Caps.DynamicFlowControlDepth < 0)
888        pCaps->VS20Caps.DynamicFlowControlDepth = D3DVS20_MAX_DYNAMICFLOWCONTROLDEPTH;
889    if (pCaps->VS20Caps.StaticFlowControlDepth > D3DVS20_MAX_STATICFLOWCONTROLDEPTH
890        || pCaps->VS20Caps.StaticFlowControlDepth < 0)
891        pCaps->VS20Caps.StaticFlowControlDepth = D3DVS20_MAX_STATICFLOWCONTROLDEPTH;
892    if (pCaps->VS20Caps.NumTemps > D3DVS20_MAX_NUMTEMPS)
893        pCaps->VS20Caps.NumTemps = D3DVS20_MAX_NUMTEMPS;
894    assert(pCaps->VS20Caps.DynamicFlowControlDepth >= D3DVS20_MIN_DYNAMICFLOWCONTROLDEPTH);
895    assert(pCaps->VS20Caps.StaticFlowControlDepth >= D3DVS20_MIN_STATICFLOWCONTROLDEPTH);
896    assert(pCaps->VS20Caps.NumTemps >= D3DVS20_MIN_NUMTEMPS);
897
898
899    pCaps->PS20Caps.Caps = D3DPS20CAPS_ARBITRARYSWIZZLE |
900                           D3DPS20CAPS_GRADIENTINSTRUCTIONS |
901                           D3DPS20CAPS_PREDICATION;
902    if (screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
903                                 PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS) ==
904        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
905                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS))
906        pCaps->PS20Caps.Caps |= D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT;
907    if (screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
908                                 PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS) ==
909        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
910                                 PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS))
911        pCaps->PS20Caps.Caps |= D3DPS20CAPS_NODEPENDENTREADLIMIT;
912    pCaps->PS20Caps.DynamicFlowControlDepth = /* XXX is this dynamic ? */
913        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
914                                 PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
915    pCaps->PS20Caps.NumTemps =
916        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
917                                 PIPE_SHADER_CAP_MAX_TEMPS);
918    pCaps->PS20Caps.StaticFlowControlDepth =  /* XXX is this static ? */
919        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
920                                 PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
921    pCaps->PS20Caps.NumInstructionSlots =
922        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
923                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
924
925    if (pCaps->PS20Caps.DynamicFlowControlDepth > D3DPS20_MAX_DYNAMICFLOWCONTROLDEPTH
926        || pCaps->PS20Caps.DynamicFlowControlDepth < 0)
927        pCaps->PS20Caps.DynamicFlowControlDepth = D3DPS20_MAX_DYNAMICFLOWCONTROLDEPTH;
928    if (pCaps->PS20Caps.StaticFlowControlDepth > D3DPS20_MAX_STATICFLOWCONTROLDEPTH
929        || pCaps->PS20Caps.StaticFlowControlDepth < 0)
930        pCaps->PS20Caps.StaticFlowControlDepth = D3DPS20_MAX_STATICFLOWCONTROLDEPTH;
931    if (pCaps->PS20Caps.NumTemps > D3DPS20_MAX_NUMTEMPS)
932        pCaps->PS20Caps.NumTemps = D3DPS20_MAX_NUMTEMPS;
933    if (pCaps->PS20Caps.NumInstructionSlots > D3DPS20_MAX_NUMINSTRUCTIONSLOTS)
934        pCaps->PS20Caps.NumInstructionSlots = D3DPS20_MAX_NUMINSTRUCTIONSLOTS;
935    assert(pCaps->PS20Caps.DynamicFlowControlDepth >= D3DPS20_MIN_DYNAMICFLOWCONTROLDEPTH);
936    assert(pCaps->PS20Caps.StaticFlowControlDepth >= D3DPS20_MIN_STATICFLOWCONTROLDEPTH);
937    assert(pCaps->PS20Caps.NumTemps >= D3DPS20_MIN_NUMTEMPS);
938    assert(pCaps->PS20Caps.NumInstructionSlots >= D3DPS20_MIN_NUMINSTRUCTIONSLOTS);
939
940
941    if (screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
942                                 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS))
943        pCaps->VertexTextureFilterCaps = pCaps->TextureFilterCaps &
944            ~(D3DPTFILTERCAPS_MIPFPOINT |
945              D3DPTFILTERCAPS_MIPFPOINT); /* XXX */
946    else
947        pCaps->VertexTextureFilterCaps = 0;
948
949    pCaps->MaxVertexShader30InstructionSlots =
950        screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
951                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
952    pCaps->MaxPixelShader30InstructionSlots =
953        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
954                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
955    if (pCaps->MaxVertexShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
956        pCaps->MaxVertexShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
957    if (pCaps->MaxPixelShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
958        pCaps->MaxPixelShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
959    assert(pCaps->MaxVertexShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
960    assert(pCaps->MaxPixelShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
961
962    /* 65535 is required, advertise more for GPUs with >= 2048 instruction slots */
963    pCaps->MaxVShaderInstructionsExecuted = MAX2(65535, pCaps->MaxVertexShader30InstructionSlots * 32);
964    pCaps->MaxPShaderInstructionsExecuted = MAX2(65535, pCaps->MaxPixelShader30InstructionSlots * 32);
965
966    if (debug_get_bool_option("NINE_DUMP_CAPS", FALSE))
967        nine_dump_D3DCAPS9(DBG_CHANNEL, pCaps);
968
969    return D3D_OK;
970}
971
972HRESULT NINE_WINAPI
973NineAdapter9_CreateDevice( struct NineAdapter9 *This,
974                           UINT RealAdapter,
975                           D3DDEVTYPE DeviceType,
976                           HWND hFocusWindow,
977                           DWORD BehaviorFlags,
978                           D3DPRESENT_PARAMETERS *pPresentationParameters,
979                           IDirect3D9 *pD3D9,
980                           ID3DPresentGroup *pPresentationGroup,
981                           IDirect3DDevice9 **ppReturnedDeviceInterface )
982{
983    struct pipe_screen *screen;
984    D3DDEVICE_CREATION_PARAMETERS params;
985    D3DCAPS9 caps;
986    int major, minor;
987    HRESULT hr;
988
989    DBG("This=%p RealAdapter=%u DeviceType=%s hFocusWindow=%p "
990        "BehaviourFlags=%x " "pD3D9=%p pPresentationGroup=%p "
991        "ppReturnedDeviceInterface=%p\n", This,
992        RealAdapter, nine_D3DDEVTYPE_to_str(DeviceType), hFocusWindow,
993        BehaviorFlags, pD3D9, pPresentationGroup, ppReturnedDeviceInterface);
994
995    ID3DPresentGroup_GetVersion(pPresentationGroup, &major, &minor);
996    if (major != 1) {
997        ERR("Doesn't support the ID3DPresentGroup version %d %d. Expected 1\n",
998            major, minor);
999        return D3DERR_NOTAVAILABLE;
1000    }
1001
1002    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
1003    if (FAILED(hr)) {
1004        DBG("Failed to get pipe_screen.\n");
1005        return hr;
1006    }
1007
1008    hr = NineAdapter9_GetDeviceCaps(This, DeviceType, &caps);
1009    if (FAILED(hr)) {
1010        DBG("Failed to get device caps.\n");
1011        return hr;
1012    }
1013
1014    params.AdapterOrdinal = RealAdapter;
1015    params.DeviceType = DeviceType;
1016    params.hFocusWindow = hFocusWindow;
1017    params.BehaviorFlags = BehaviorFlags;
1018
1019    hr = NineDevice9_new(screen, &params, &caps, pPresentationParameters,
1020                         pD3D9, pPresentationGroup, This->ctx, FALSE, NULL,
1021                         (struct NineDevice9 **)ppReturnedDeviceInterface,
1022                         minor);
1023    if (FAILED(hr)) {
1024        DBG("Failed to create device.\n");
1025        return hr;
1026    }
1027    DBG("NineDevice9 created successfully.\n");
1028
1029    return D3D_OK;
1030}
1031
1032HRESULT NINE_WINAPI
1033NineAdapter9_CreateDeviceEx( struct NineAdapter9 *This,
1034                             UINT RealAdapter,
1035                             D3DDEVTYPE DeviceType,
1036                             HWND hFocusWindow,
1037                             DWORD BehaviorFlags,
1038                             D3DPRESENT_PARAMETERS *pPresentationParameters,
1039                             D3DDISPLAYMODEEX *pFullscreenDisplayMode,
1040                             IDirect3D9Ex *pD3D9Ex,
1041                             ID3DPresentGroup *pPresentationGroup,
1042                             IDirect3DDevice9Ex **ppReturnedDeviceInterface )
1043{
1044    struct pipe_screen *screen;
1045    D3DDEVICE_CREATION_PARAMETERS params;
1046    D3DCAPS9 caps;
1047    int major, minor;
1048    HRESULT hr;
1049
1050    DBG("This=%p RealAdapter=%u DeviceType=%s hFocusWindow=%p "
1051        "BehaviourFlags=%x " "pD3D9Ex=%p pPresentationGroup=%p "
1052        "ppReturnedDeviceInterface=%p\n", This,
1053        RealAdapter, nine_D3DDEVTYPE_to_str(DeviceType), hFocusWindow,
1054        BehaviorFlags, pD3D9Ex, pPresentationGroup, ppReturnedDeviceInterface);
1055
1056    ID3DPresentGroup_GetVersion(pPresentationGroup, &major, &minor);
1057    if (major != 1) {
1058        ERR("Doesn't support the ID3DPresentGroup version %d %d. Expected 1\n",
1059            major, minor);
1060        return D3DERR_NOTAVAILABLE;
1061    }
1062
1063    hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
1064    if (FAILED(hr)) {
1065        DBG("Failed to get pipe_screen.\n");
1066        return hr;
1067    }
1068
1069    hr = NineAdapter9_GetDeviceCaps(This, DeviceType, &caps);
1070    if (FAILED(hr)) {
1071        DBG("Failed to get device caps.\n");
1072        return hr;
1073    }
1074
1075    params.AdapterOrdinal = RealAdapter;
1076    params.DeviceType = DeviceType;
1077    params.hFocusWindow = hFocusWindow;
1078    params.BehaviorFlags = BehaviorFlags;
1079
1080    hr = NineDevice9Ex_new(screen, &params, &caps, pPresentationParameters,
1081                           pFullscreenDisplayMode,
1082                           pD3D9Ex, pPresentationGroup, This->ctx,
1083                           (struct NineDevice9Ex **)ppReturnedDeviceInterface,
1084                           minor);
1085    if (FAILED(hr)) {
1086        DBG("Failed to create device.\n");
1087        return hr;
1088    }
1089    DBG("NineDevice9Ex created successfully.\n");
1090
1091    return D3D_OK;
1092}
1093
1094ID3DAdapter9Vtbl NineAdapter9_vtable = {
1095    (void *)NineUnknown_QueryInterface,
1096    (void *)NineUnknown_AddRef,
1097    (void *)NineUnknown_Release,
1098    (void *)NineAdapter9_GetAdapterIdentifier,
1099    (void *)NineAdapter9_CheckDeviceType,
1100    (void *)NineAdapter9_CheckDeviceFormat,
1101    (void *)NineAdapter9_CheckDeviceMultiSampleType,
1102    (void *)NineAdapter9_CheckDepthStencilMatch,
1103    (void *)NineAdapter9_CheckDeviceFormatConversion,
1104    (void *)NineAdapter9_GetDeviceCaps,
1105    (void *)NineAdapter9_CreateDevice,
1106    (void *)NineAdapter9_CreateDeviceEx
1107};
1108
1109static const GUID *NineAdapter9_IIDs[] = {
1110    &IID_ID3D9Adapter,
1111    &IID_IUnknown,
1112    NULL
1113};
1114
1115HRESULT
1116NineAdapter9_new( struct d3dadapter9_context *pCTX,
1117                  struct NineAdapter9 **ppOut )
1118{
1119    NINE_NEW(Adapter9, ppOut, FALSE, /* args */ pCTX);
1120}
1121