tu: Implement fallback linear staging blit for CopyImage
[mesa.git] / src / gallium / state_trackers / nine / adapter9.c
index 481f86362f8f8546dc4e1fb56c50cd075b334653..e8d4697986c4e1758066e468aee5592a0c346ab2 100644 (file)
 #include "nine_pipe.h"
 #include "nine_dump.h"
 #include "util/u_math.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_dump.h"
 
 #include "pipe/p_screen.h"
 
 #define DBG_CHANNEL DBG_ADAPTER
 
+static bool
+has_sm3(struct pipe_screen *hal)
+{
+    return hal->get_param(hal, PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD) &&
+           hal->get_param(hal, PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES) &&
+           hal->get_param(hal, PIPE_CAP_VERTEX_SHADER_SATURATE);
+}
+
 HRESULT
 NineAdapter9_ctor( struct NineAdapter9 *This,
                    struct NineUnknownParams *pParams,
                    struct d3dadapter9_context *pCTX )
 {
+    struct pipe_screen *hal = pCTX->hal;
     HRESULT hr = NineUnknown_ctor(&This->base, pParams);
     if (FAILED(hr)) { return hr; }
 
@@ -46,7 +55,7 @@ NineAdapter9_ctor( struct NineAdapter9 *This,
     nine_dump_D3DADAPTER_IDENTIFIER9(DBG_CHANNEL, &pCTX->identifier);
 
     This->ctx = pCTX;
-    if (!This->ctx->hal->get_param(This->ctx->hal, PIPE_CAP_CLIP_HALFZ)) {
+    if (!hal->get_param(hal, PIPE_CAP_CLIP_HALFZ)) {
         ERR("Driver doesn't support d3d9 coordinates\n");
         return D3DERR_DRIVERINTERNALERROR;
     }
@@ -54,7 +63,46 @@ NineAdapter9_ctor( struct NineAdapter9 *This,
         !This->ctx->ref->get_param(This->ctx->ref, PIPE_CAP_CLIP_HALFZ)) {
         ERR("Warning: Sotware rendering driver doesn't support d3d9 coordinates\n");
     }
-
+    /* Old cards had tricks to bypass some restrictions to implement
+     * everything and fit tight the requirements: number of constants,
+     * number of temp registers, special behaviours, etc. Since we don't
+     * have access to all this, we need a bit more than what dx9 required.
+     * For example we have to use more than 32 temp registers to emulate
+     * behaviours, while some dx9 hw don't have more. As for sm2 hardware,
+     * we could support vs2 / ps2 for them but it needs some more care, and
+     * as these are very old, we choose to drop support for them */
+
+    /* checks minimum requirements, most are vs3/ps3 strict requirements */
+    if (!has_sm3(hal) ||
+        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
+                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 256 * sizeof(float[4]) ||
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 244 * sizeof(float[4]) ||
+        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
+                              PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
+        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
+                              PIPE_SHADER_CAP_MAX_INPUTS) < 16 ||
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_INPUTS) < 10 ||
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS) < 16) {
+        ERR("Your card is not supported by Gallium Nine. Minimum requirement "
+            "is >= r500, >= nv50, >= i965\n");
+        return D3DERR_DRIVERINTERNALERROR;
+    }
+    /* for r500 */
+    if (hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
+                              PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) < 276 * sizeof(float[4]) || /* we put bool and int constants with float constants */
+        hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
+                              PIPE_SHADER_CAP_MAX_TEMPS) < 40 || /* we use some more temp registers */
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_TEMPS) < 40 ||
+        hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
+                              PIPE_SHADER_CAP_MAX_INPUTS) < 20) /* we don't pack inputs as much as we could */
+        ERR("Your card is at the limit of Gallium Nine requirements. Some games "
+            "may run into issues because requirements are too tight\n");
     return D3D_OK;
 }
 
@@ -108,7 +156,7 @@ NineAdapter9_GetScreen( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_GetAdapterIdentifier( struct NineAdapter9 *This,
                                    DWORD Flags,
                                    D3DADAPTER_IDENTIFIER9 *pIdentifier )
@@ -125,7 +173,7 @@ NineAdapter9_GetAdapterIdentifier( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-static INLINE boolean
+static inline boolean
 backbuffer_format( D3DFORMAT dfmt,
                    D3DFORMAT bfmt,
                    boolean win )
@@ -144,7 +192,7 @@ backbuffer_format( D3DFORMAT dfmt,
     return FALSE;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CheckDeviceType( struct NineAdapter9 *This,
                               D3DDEVTYPE DevType,
                               D3DFORMAT AdapterFormat,
@@ -166,19 +214,19 @@ NineAdapter9_CheckDeviceType( struct NineAdapter9 *This,
     hr = NineAdapter9_GetScreen(This, DevType, &screen);
     if (FAILED(hr)) { return hr; }
 
-    dfmt = d3d9_to_pipe_format(AdapterFormat);
-    bfmt = d3d9_to_pipe_format(BackBufferFormat);
+    /* The display format is not handled in Nine. We always present an XRGB8888
+     * buffer (and the display server will eventually do the conversion). We probably
+     * don't need to check for anything for the adapter format support, since if the
+     * display server advertise support, it will likely be able to do the conversion.
+     * We do the approximation that a format is available in the display server if
+     * the format passes with NINE_BIND_BACKBUFFER_FLAGS */
+    dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D,
+                                       1,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
+    bfmt = d3d9_to_pipe_format_checked(screen, BackBufferFormat, PIPE_TEXTURE_2D,
+                                       1,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
     if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
-        DBG("Invalid Adapter/BackBufferFormat.\n");
-        return D3DERR_NOTAVAILABLE;
-    }
-
-    if (!screen->is_format_supported(screen, dfmt, PIPE_TEXTURE_2D, 1,
-                                     PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED) ||
-        !screen->is_format_supported(screen, bfmt, PIPE_TEXTURE_2D, 1,
-                                     PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED)) {
         DBG("Unsupported Adapter/BackBufferFormat.\n");
         return D3DERR_NOTAVAILABLE;
     }
@@ -186,7 +234,7 @@ NineAdapter9_CheckDeviceType( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-static INLINE boolean
+static inline boolean
 display_format( D3DFORMAT fmt,
                 boolean win )
 {
@@ -207,7 +255,7 @@ display_format( D3DFORMAT fmt,
     return FALSE;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
                                 D3DDEVTYPE DeviceType,
                                 D3DFORMAT AdapterFormat,
@@ -220,24 +268,24 @@ NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
     enum pipe_format pf;
     enum pipe_texture_target target;
     unsigned bind = 0;
+    boolean srgb;
 
     /* Check adapter format. */
 
-    /* Nicer output if we only have the line at the end. */
-#if 1
     DBG("This=%p DeviceType=%s AdapterFormat=%s\n", This,
         nine_D3DDEVTYPE_to_str(DeviceType), d3dformat_to_string(AdapterFormat));
-#endif
+    DBG("Usage=%x RType=%u CheckFormat=%s\n", Usage, RType,
+        d3dformat_to_string(CheckFormat));
+
     user_assert(display_format(AdapterFormat, FALSE), D3DERR_INVALIDCALL);
 
     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
     if (FAILED(hr))
         return hr;
-    pf = d3d9_to_pipe_format(AdapterFormat);
-    if (pf == PIPE_FORMAT_NONE ||
-        !screen->is_format_supported(screen, pf, PIPE_TEXTURE_2D, 0,
+    pf = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
                                      PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED)) {
+                                     PIPE_BIND_SHARED, FALSE, FALSE);
+    if (pf == PIPE_FORMAT_NONE) {
         DBG("AdapterFormat %s not available.\n",
             d3dformat_to_string(AdapterFormat));
         return D3DERR_NOTAVAILABLE;
@@ -259,7 +307,11 @@ NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
 
     bind = 0;
     if (Usage & D3DUSAGE_RENDERTARGET) bind |= PIPE_BIND_RENDER_TARGET;
-    if (Usage & D3DUSAGE_DEPTHSTENCIL) bind |= PIPE_BIND_DEPTH_STENCIL;
+    if (Usage & D3DUSAGE_DEPTHSTENCIL) {
+        if (!depth_stencil_format(CheckFormat))
+            return D3DERR_NOTAVAILABLE;
+        bind |= d3d9_get_pipe_depth_format_bindings(CheckFormat);
+    }
 
     /* API hack because setting RT[0] to NULL is forbidden */
     if (CheckFormat == D3DFMT_NULL && bind == PIPE_BIND_RENDER_TARGET &&
@@ -267,7 +319,18 @@ NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
          RType == D3DRTYPE_TEXTURE))
         return D3D_OK;
 
-    if (Usage & D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING)
+    /* RESZ hack */
+    if (CheckFormat == D3DFMT_RESZ && bind == PIPE_BIND_RENDER_TARGET &&
+        RType == D3DRTYPE_SURFACE)
+        return screen->get_param(screen, PIPE_CAP_MULTISAMPLE_Z_RESOLVE) ?
+               D3D_OK : D3DERR_NOTAVAILABLE;
+
+    /* ATOC hack */
+    if (CheckFormat == D3DFMT_ATOC && RType == D3DRTYPE_SURFACE)
+        return D3D_OK;
+
+    if ((Usage & D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING) &&
+        (Usage & D3DUSAGE_RENDERTARGET))
         bind |= PIPE_BIND_BLENDABLE;
 
     if (Usage & D3DUSAGE_DMAP) {
@@ -281,29 +344,34 @@ NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
     case D3DRTYPE_VOLUMETEXTURE: bind |= PIPE_BIND_SAMPLER_VIEW; break;
     case D3DRTYPE_VERTEXBUFFER:  bind |= PIPE_BIND_VERTEX_BUFFER; break;
     case D3DRTYPE_INDEXBUFFER:   bind |= PIPE_BIND_INDEX_BUFFER; break;
+    case D3DRTYPE_SURFACE:
+        if (!(Usage & D3DUSAGE_DEPTHSTENCIL))
+            bind |= PIPE_BIND_SAMPLER_VIEW; /* StretchRect */
+        /* Offscreen surface support: Usage = 0.
+         * In practice drivers are very restrictive on the formats supported.
+         * Basically a few common formats + YUV and compressed formats. The
+         * reason is that offscreen surface are useful only for directdraw
+         * compatibility (a WONTIMPL of nine) + format conversion (useful in
+         * particular for YUV because the format was not advertised for textures
+         * on NV chips). */
+        if (Usage == 0)
+            bind |= PIPE_BIND_RENDER_TARGET; /* A current requirement of our impl, which we should get rid of. */
     default:
         break;
     }
 
 
-    pf = d3d9_to_pipe_format(CheckFormat);
-    if (Usage & (D3DUSAGE_QUERY_SRGBREAD | D3DUSAGE_QUERY_SRGBWRITE))
-        pf = util_format_srgb(pf);
-
-    DBG("Format=%s/%s Usage/Bind=%x/%d RType/Target=%u/%s\n", // replace %d to %s
-        d3dformat_to_string(CheckFormat), util_format_name(pf),
-        Usage, bind, // temporary simplified for merge, FIXME
-        /* Usage, util_dump_bind_flags(bind), */
-        RType, util_dump_tex_target(target, TRUE));
-
-    if (pf == PIPE_FORMAT_NONE ||
-        !screen->is_format_supported(screen, pf, target, 0, bind)) {
+    srgb = (Usage & (D3DUSAGE_QUERY_SRGBREAD | D3DUSAGE_QUERY_SRGBWRITE)) != 0;
+    pf = d3d9_to_pipe_format_checked(screen, CheckFormat, target,
+                                     0, bind, srgb, FALSE);
+    if (pf == PIPE_FORMAT_NONE) {
         DBG("NOT AVAILABLE\n");
         return D3DERR_NOTAVAILABLE;
     }
 
-    /* we support ATI1 and ATI2 hack only for 2D textures */
-    if (RType != D3DRTYPE_TEXTURE && (CheckFormat == D3DFMT_ATI1 || CheckFormat == D3DFMT_ATI2))
+    /* we support ATI1 and ATI2 hack only for 2D and Cube textures */
+    if (RType != D3DRTYPE_TEXTURE && RType != D3DRTYPE_CUBETEXTURE &&
+        (CheckFormat == D3DFMT_ATI1 || CheckFormat == D3DFMT_ATI2))
         return D3DERR_NOTAVAILABLE;
     /* if (Usage & D3DUSAGE_NONSECURE) { don't know the implications of this } */
     /* if (Usage & D3DUSAGE_SOFTWAREPROCESSING) { we can always support this } */
@@ -313,7 +381,7 @@ NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CheckDeviceMultiSampleType( struct NineAdapter9 *This,
                                          D3DDEVTYPE DeviceType,
                                          D3DFORMAT SurfaceFormat,
@@ -331,52 +399,51 @@ NineAdapter9_CheckDeviceMultiSampleType( struct NineAdapter9 *This,
         d3dformat_to_string(SurfaceFormat), Windowed, MultiSampleType,
         pQualityLevels);
 
+    if (pQualityLevels) {
+        /* In error cases return only 1 quality level supported */
+        *pQualityLevels = 1;
+    }
+    user_assert(MultiSampleType <= D3DMULTISAMPLE_16_SAMPLES, D3DERR_INVALIDCALL);
+
     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
     if (FAILED(hr))
         return hr;
 
-    pf = d3d9_to_pipe_format(SurfaceFormat);
-    bind = util_format_is_depth_or_stencil(pf) ?
-        PIPE_BIND_DEPTH_STENCIL : PIPE_BIND_RENDER_TARGET;
+    if (depth_stencil_format(SurfaceFormat))
+        bind = d3d9_get_pipe_depth_format_bindings(SurfaceFormat);
+    else /* render-target */
+        bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
 
-    if (pf == PIPE_FORMAT_NONE ||
-        !screen->is_format_supported(screen, pf, PIPE_TEXTURE_2D,
-                                     MultiSampleType, bind)) {
+    pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
+                                     0, PIPE_BIND_SAMPLER_VIEW, FALSE, FALSE);
+
+    if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
+        DBG("%s not available.\n", d3dformat_to_string(SurfaceFormat));
+        return D3DERR_INVALIDCALL;
+    }
+
+    pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
+                                     MultiSampleType, bind, FALSE, FALSE);
+
+    if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
         DBG("%s with %u samples not available.\n",
             d3dformat_to_string(SurfaceFormat), MultiSampleType);
         return D3DERR_NOTAVAILABLE;
     }
 
-    if (pQualityLevels)
-        *pQualityLevels = 1; /* gallium doesn't have quality levels */
+    if (pQualityLevels) {
+        /* NONMASKABLE MultiSampleType might have more than one quality level,
+         * while MASKABLE MultiSampleTypes have only one level.
+         * Advertise quality levels and map each level to a sample count. */
+         (void ) d3dmultisample_type_check(screen, SurfaceFormat,
+                 &MultiSampleType, D3DMULTISAMPLE_16_SAMPLES, pQualityLevels);
+         DBG("advertising %u quality levels\n", *pQualityLevels);
+    }
 
     return D3D_OK;
 }
 
-static INLINE boolean
-depth_stencil_format( D3DFORMAT fmt )
-{
-    static D3DFORMAT allowed[] = {
-        D3DFMT_D16_LOCKABLE,
-        D3DFMT_D32,
-        D3DFMT_D15S1,
-        D3DFMT_D24S8,
-        D3DFMT_D24X8,
-        D3DFMT_D24X4S4,
-        D3DFMT_D16,
-        D3DFMT_D32F_LOCKABLE,
-        D3DFMT_D24FS8,
-        D3DFMT_D32_LOCKABLE
-    };
-    unsigned i;
-
-    for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
-        if (fmt == allowed[i]) { return TRUE; }
-    }
-    return FALSE;
-}
-
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CheckDepthStencilMatch( struct NineAdapter9 *This,
                                      D3DDEVTYPE DeviceType,
                                      D3DFORMAT AdapterFormat,
@@ -399,31 +466,27 @@ NineAdapter9_CheckDepthStencilMatch( struct NineAdapter9 *This,
     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
     if (FAILED(hr)) { return hr; }
 
-    dfmt = d3d9_to_pipe_format(AdapterFormat);
-    bfmt = d3d9_to_pipe_format(RenderTargetFormat);
+    dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
+    bfmt = d3d9_to_pipe_format_checked(screen, RenderTargetFormat,
+                                       PIPE_TEXTURE_2D, 0,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
     if (RenderTargetFormat == D3DFMT_NULL)
         bfmt = dfmt;
-    zsfmt = d3d9_to_pipe_format(DepthStencilFormat);
+    zsfmt = d3d9_to_pipe_format_checked(screen, DepthStencilFormat,
+                                        PIPE_TEXTURE_2D, 0,
+                                        d3d9_get_pipe_depth_format_bindings(DepthStencilFormat),
+                                        FALSE, FALSE);
     if (dfmt == PIPE_FORMAT_NONE ||
         bfmt == PIPE_FORMAT_NONE ||
         zsfmt == PIPE_FORMAT_NONE) {
         return D3DERR_NOTAVAILABLE;
     }
 
-    if (!screen->is_format_supported(screen, dfmt, PIPE_TEXTURE_2D, 0,
-                                     PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED) ||
-        !screen->is_format_supported(screen, bfmt, PIPE_TEXTURE_2D, 0,
-                                     PIPE_BIND_RENDER_TARGET) ||
-        !screen->is_format_supported(screen, zsfmt, PIPE_TEXTURE_2D, 0,
-                                     PIPE_BIND_DEPTH_STENCIL)) {
-        return D3DERR_NOTAVAILABLE;
-    }
-
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CheckDeviceFormatConversion( struct NineAdapter9 *This,
                                           D3DDEVTYPE DeviceType,
                                           D3DFORMAT SourceFormat,
@@ -446,17 +509,12 @@ NineAdapter9_CheckDeviceFormatConversion( struct NineAdapter9 *This,
     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
     if (FAILED(hr)) { return hr; }
 
-    dfmt = d3d9_to_pipe_format(TargetFormat);
-    bfmt = d3d9_to_pipe_format(SourceFormat);
+    dfmt = d3d9_to_pipe_format_checked(screen, TargetFormat, PIPE_TEXTURE_2D, 1,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
+    bfmt = d3d9_to_pipe_format_checked(screen, SourceFormat, PIPE_TEXTURE_2D, 1,
+                                       NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
+
     if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
-        return D3DERR_NOTAVAILABLE;
-    }
-    if (!screen->is_format_supported(screen, dfmt, PIPE_TEXTURE_2D, 1,
-                                     PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED) ||
-        !screen->is_format_supported(screen, bfmt, PIPE_TEXTURE_2D, 1,
-                                     PIPE_BIND_DISPLAY_TARGET |
-                                     PIPE_BIND_SHARED)) {
         DBG("%s to %s not supported.\n",
             d3dformat_to_string(SourceFormat),
             d3dformat_to_string(TargetFormat));
@@ -466,13 +524,12 @@ NineAdapter9_CheckDeviceFormatConversion( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
                             D3DDEVTYPE DeviceType,
                             D3DCAPS9 *pCaps )
 {
     struct pipe_screen *screen;
-    boolean sm3, vs;
     HRESULT hr;
 
     DBG("This=%p DeviceType=%s pCaps=%p\n", This,
@@ -492,17 +549,13 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
 #define D3DNPIPECAP(pcap, d3dcap) \
     (screen->get_param(screen, PIPE_CAP_##pcap) ? 0 : (d3dcap))
 
-    sm3 = screen->get_param(screen, PIPE_CAP_SM3);
-    vs = !!(screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
-                                     PIPE_SHADER_CAP_MAX_INSTRUCTIONS));
-
     pCaps->DeviceType = DeviceType;
 
     pCaps->AdapterOrdinal = 0;
 
-    pCaps->Caps = 0;
+    pCaps->Caps = D3DCAPS_READ_SCANLINE;
 
-    pCaps->Caps2 = D3DCAPS2_CANMANAGERESOURCE |
+    pCaps->Caps2 = /* D3DCAPS2_CANMANAGERESOURCE | */
                 /* D3DCAPS2_CANSHARERESOURCE | */
                 /* D3DCAPS2_CANCALIBRATEGAMMA | */
                    D3DCAPS2_DYNAMICTEXTURES |
@@ -523,7 +576,7 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
                                    D3DPRESENT_INTERVAL_THREE |
                                    D3DPRESENT_INTERVAL_FOUR |
                                    D3DPRESENT_INTERVAL_IMMEDIATE;
-    pCaps->CursorCaps = D3DCURSORCAPS_COLOR | D3DCURSORCAPS_LOWRES;
+    pCaps->CursorCaps = D3DCURSORCAPS_COLOR /* | D3DCURSORCAPS_LOWRES*/;
 
     pCaps->DevCaps = D3DDEVCAPS_CANBLTSYSTONONLOCAL |
                      D3DDEVCAPS_CANRENDERAFTERFLIP |
@@ -540,8 +593,8 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
                      /*D3DDEVCAPS_RTPATCHES |*/
                      /*D3DDEVCAPS_RTPATCHHANDLEZERO |*/
                      /*D3DDEVCAPS_SEPARATETEXTUREMEMORIES |*/
-                     /*D3DDEVCAPS_TEXTURENONLOCALVIDMEM |*/
-                     D3DDEVCAPS_TEXTURESYSTEMMEMORY |
+                     D3DDEVCAPS_TEXTURENONLOCALVIDMEM |
+                     /* D3DDEVCAPS_TEXTURESYSTEMMEMORY |*/
                      D3DDEVCAPS_TEXTUREVIDEOMEMORY |
                      D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
                      D3DDEVCAPS_TLVERTEXVIDEOMEMORY;
@@ -556,32 +609,32 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
                                D3DPMISCCAPS_TSSARGTEMP |
                                D3DPMISCCAPS_BLENDOP |
                                D3DPIPECAP(INDEP_BLEND_ENABLE, D3DPMISCCAPS_INDEPENDENTWRITEMASKS) |
-                               /*D3DPMISCCAPS_PERSTAGECONSTANT |*/
-                               /*D3DPMISCCAPS_POSTBLENDSRGBCONVERT |*/ /* TODO */
-                               D3DPMISCCAPS_FOGANDSPECULARALPHA |
+                               D3DPMISCCAPS_PERSTAGECONSTANT |
+                               /*D3DPMISCCAPS_POSTBLENDSRGBCONVERT |*/ /* TODO: advertise if Ex and dx10 able card */
+                               D3DPMISCCAPS_FOGANDSPECULARALPHA | /* Note: documentation of the flag is wrong */
                                D3DPIPECAP(BLEND_EQUATION_SEPARATE, D3DPMISCCAPS_SEPARATEALPHABLEND) |
                                D3DPIPECAP(MIXED_COLORBUFFER_FORMATS, D3DPMISCCAPS_MRTINDEPENDENTBITDEPTHS) |
                                D3DPMISCCAPS_MRTPOSTPIXELSHADERBLENDING |
-                               /*D3DPMISCCAPS_FOGVERTEXCLAMPED*/0;
+                               D3DPMISCCAPS_FOGVERTEXCLAMPED;
     if (!screen->get_param(screen, PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION))
         pCaps->PrimitiveMiscCaps |= D3DPMISCCAPS_CLIPTLVERTS;
 
     pCaps->RasterCaps =
         D3DPIPECAP(ANISOTROPIC_FILTER, D3DPRASTERCAPS_ANISOTROPY) |
-        /*D3DPRASTERCAPS_COLORPERSPECTIVE |*/
+        D3DPRASTERCAPS_COLORPERSPECTIVE |
         D3DPRASTERCAPS_DITHER |
         D3DPRASTERCAPS_DEPTHBIAS |
-        /*D3DPRASTERCAPS_FOGRANGE |*/
-        /*D3DPRASTERCAPS_FOGTABLE |*/
-        /*D3DPRASTERCAPS_FOGVERTEX |*/
+        D3DPRASTERCAPS_FOGRANGE |
+        D3DPRASTERCAPS_FOGTABLE |
+        D3DPRASTERCAPS_FOGVERTEX |
         D3DPRASTERCAPS_MIPMAPLODBIAS |
         D3DPRASTERCAPS_MULTISAMPLE_TOGGLE |
         D3DPRASTERCAPS_SCISSORTEST |
         D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
         /*D3DPRASTERCAPS_WBUFFER |*/
-        /*D3DPRASTERCAPS_WFOG |*/
+        D3DPRASTERCAPS_WFOG |
         /*D3DPRASTERCAPS_ZBUFFERLESSHSR |*/
-        /*D3DPRASTERCAPS_ZFOG |*/
+        D3DPRASTERCAPS_ZFOG |
         D3DPRASTERCAPS_ZTEST;
 
     pCaps->ZCmpCaps = D3DPCMPCAPS_NEVER |
@@ -613,7 +666,8 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
 
     pCaps->DestBlendCaps = pCaps->SrcBlendCaps;
 
-    pCaps->AlphaCmpCaps = D3DPCMPCAPS_LESS |
+    pCaps->AlphaCmpCaps = D3DPCMPCAPS_NEVER |
+                          D3DPCMPCAPS_LESS |
                           D3DPCMPCAPS_EQUAL |
                           D3DPCMPCAPS_LESSEQUAL |
                           D3DPCMPCAPS_GREATER |
@@ -632,14 +686,14 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
         D3DPTEXTURECAPS_ALPHAPALETTE |
         D3DPTEXTURECAPS_PERSPECTIVE |
         D3DPTEXTURECAPS_PROJECTED |
-        /*D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |*/
+        D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
         D3DPTEXTURECAPS_CUBEMAP |
         D3DPTEXTURECAPS_VOLUMEMAP |
         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_POW2) |
         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_NONPOW2CONDITIONAL) |
         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_CUBEMAP_POW2) |
         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_VOLUMEMAP_POW2) |
-        D3DPIPECAP(MAX_TEXTURE_2D_LEVELS, D3DPTEXTURECAPS_MIPMAP) |
+        D3DPIPECAP(MAX_TEXTURE_2D_SIZE, D3DPTEXTURECAPS_MIPMAP) |
         D3DPIPECAP(MAX_TEXTURE_3D_LEVELS, D3DPTEXTURECAPS_MIPVOLUMEMAP) |
         D3DPIPECAP(MAX_TEXTURE_CUBE_LEVELS, D3DPTEXTURECAPS_MIPCUBEMAP);
 
@@ -680,8 +734,8 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
         pCaps->LineCaps |= D3DLINECAPS_ANTIALIAS;
     }
 
-    pCaps->MaxTextureWidth =
-        1 << (screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
+    pCaps->MaxTextureWidth =screen->get_param(screen,
+                                              PIPE_CAP_MAX_TEXTURE_2D_SIZE);
     pCaps->MaxTextureHeight = pCaps->MaxTextureWidth;
     pCaps->MaxVolumeExtent =
         1 << (screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_3D_LEVELS) - 1);
@@ -692,15 +746,12 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
     pCaps->MaxAnisotropy =
         (DWORD)screen->get_paramf(screen, PIPE_CAPF_MAX_TEXTURE_ANISOTROPY);
 
-    pCaps->MaxVertexW = 1.0f; /* XXX */
-    pCaps->GuardBandLeft = screen->get_paramf(screen,
-                                              PIPE_CAPF_GUARD_BAND_LEFT);
-    pCaps->GuardBandTop = screen->get_paramf(screen,
-                                             PIPE_CAPF_GUARD_BAND_TOP);
-    pCaps->GuardBandRight = screen->get_paramf(screen,
-                                               PIPE_CAPF_GUARD_BAND_RIGHT);
-    pCaps->GuardBandBottom = screen->get_paramf(screen,
-                                                PIPE_CAPF_GUARD_BAND_BOTTOM);
+    /* Values for GeForce 9600 GT */
+    pCaps->MaxVertexW = 1e10f;
+    pCaps->GuardBandLeft = -1e9f;
+    pCaps->GuardBandTop = -1e9f;
+    pCaps->GuardBandRight = 1e9f;
+    pCaps->GuardBandBottom = 1e9f;
     pCaps->ExtentsAdjust = 0.0f;
 
     pCaps->StencilCaps =
@@ -712,15 +763,13 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
         D3DSTENCILCAPS_INVERT |
         D3DSTENCILCAPS_INCR |
         D3DSTENCILCAPS_DECR |
-        D3DPIPECAP(TWO_SIDED_STENCIL, D3DSTENCILCAPS_TWOSIDED);
+        D3DSTENCILCAPS_TWOSIDED;
 
     pCaps->FVFCaps =
-        (D3DFVFCAPS_TEXCOORDCOUNTMASK & 0xff) |
+        8 | /* 8 textures max */
         /*D3DFVFCAPS_DONOTSTRIPELEMENTS |*/
         D3DFVFCAPS_PSIZE;
 
-    /* XXX: Some of these are probably not in SM2.0 so cap them when I figure
-     * them out. For now leave them all enabled. */
     pCaps->TextureOpCaps = D3DTEXOPCAPS_DISABLE |
                            D3DTEXOPCAPS_SELECTARG1 |
                            D3DTEXOPCAPS_SELECTARG2 |
@@ -750,13 +799,10 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
 
     pCaps->MaxTextureBlendStages = 8; /* XXX wine */
         (DWORD)screen->get_param(screen, PIPE_CAP_BLEND_EQUATION_SEPARATE);
-    pCaps->MaxSimultaneousTextures = screen->get_shader_param(screen,
-        PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
-    if (pCaps->MaxSimultaneousTextures > NINE_MAX_SAMPLERS_PS)
-        pCaps->MaxSimultaneousTextures = NINE_MAX_SAMPLERS_PS;
+    pCaps->MaxSimultaneousTextures = 8;
 
     pCaps->VertexProcessingCaps = D3DVTXPCAPS_TEXGEN |
-                                  /*D3DVTXPCAPS_TEXGEN_SPHEREMAP |*/
+                                  D3DVTXPCAPS_TEXGEN_SPHEREMAP |
                                   D3DVTXPCAPS_MATERIALSOURCE7 |
                                   D3DVTXPCAPS_DIRECTIONALLIGHTS |
                                   D3DVTXPCAPS_POSITIONALLIGHTS |
@@ -767,12 +813,12 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
     pCaps->MaxActiveLights = NINE_MAX_LIGHTS_ACTIVE; /* like GL_LIGHTi */
     pCaps->MaxUserClipPlanes = PIPE_MAX_CLIP_PLANES;
     pCaps->MaxVertexBlendMatrices = 4; /* 1 vec4 BLENDWEIGHT/INDICES input */
-    pCaps->MaxVertexBlendMatrixIndex = 7; /* D3DTS_WORLDMATRIX(0..7) */
+    pCaps->MaxVertexBlendMatrixIndex = 8; /* D3DTS_WORLDMATRIX(0..8) */
 
     pCaps->MaxPointSize = screen->get_paramf(screen, PIPE_CAPF_MAX_POINT_WIDTH);
 
-    pCaps->MaxPrimitiveCount = 0xFFFFF; /* <- wine, really 0xFFFFFFFF; */
-    pCaps->MaxVertexIndex = 0xFFFFF; /* <- wine, really 0xFFFFFFFF */
+    pCaps->MaxPrimitiveCount = 0x555555; /* <- wine, really 0xFFFFFFFF; */
+    pCaps->MaxVertexIndex = 0xFFFFFF; /* <- wine, really 0xFFFFFFFF */
     pCaps->MaxStreams =
         _min(screen->get_shader_param(screen,
                  PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_MAX_INPUTS),
@@ -781,29 +827,18 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
     pCaps->MaxStreamStride = screen->get_param(screen,
             PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE);
 
-    pCaps->VertexShaderVersion = sm3 ? D3DVS_VERSION(3,0) : D3DVS_VERSION(2,0);
-    if (vs) {
-        /* VS 2 as well as 3.0 supports a minimum of 256 consts, no matter how
-         * much our architecture moans about it. The problem is that D3D9
-         * expects access to 16 int consts (i#), containing 3 components and
-         * 16 booleans (b#), containing only 1 component. This should be packed
-         * into 20 float vectors (16 for i# and 16/4 for b#), since gallium has
-         * removed support for the loop counter/boolean files. */
-        pCaps->MaxVertexShaderConst =
-            _min((screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
-                     PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) /
-                     sizeof(float[4])) - 20,
-                NINE_MAX_CONST_F);
-        /* Fake the minimum cap for Windows. */
-        if (QUIRK(FAKE_CAPS)) {
-            pCaps->MaxVertexShaderConst = 256;
-        }
-    } else {
-        pCaps->MaxVertexShaderConst = 0;
-    }
+    pCaps->VertexShaderVersion = D3DVS_VERSION(3,0);
 
-    pCaps->PixelShaderVersion = sm3 ? D3DPS_VERSION(3,0) : D3DPS_VERSION(2,0);
-    pCaps->PixelShader1xMaxValue = 8.0f; /* XXX: wine */
+    /* VS 2 as well as 3.0 supports a minimum of 256 consts.
+     * Wine and d3d9 drivers for dx1x hw advertise 256. Just as them,
+     * advertise 256. Problem is with hw that can only do 256, because
+     * we need take a few slots for boolean and integer constants. For these
+     * we'll have to fail later if they use complex shaders. */
+    pCaps->MaxVertexShaderConst = NINE_MAX_CONST_F;
+
+    pCaps->PixelShaderVersion = D3DPS_VERSION(3,0);
+    /* Value for GeForce 9600 GT */
+    pCaps->PixelShader1xMaxValue = 65504.f;
 
     pCaps->DevCaps2 = D3DDEVCAPS2_STREAMOFFSET |
                       D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET |
@@ -919,23 +954,18 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
     else
         pCaps->VertexTextureFilterCaps = 0;
 
-    if (sm3) {
-        pCaps->MaxVertexShader30InstructionSlots =
-            screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
-                                     PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
-        pCaps->MaxPixelShader30InstructionSlots =
-            screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
-                                     PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
-        if (pCaps->MaxVertexShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
-            pCaps->MaxVertexShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
-        if (pCaps->MaxPixelShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
-            pCaps->MaxPixelShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
-        assert(pCaps->MaxVertexShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
-        assert(pCaps->MaxPixelShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
-    } else {
-        pCaps->MaxVertexShader30InstructionSlots = 0;
-        pCaps->MaxPixelShader30InstructionSlots = 0;
-    }
+    pCaps->MaxVertexShader30InstructionSlots =
+        screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
+                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
+    pCaps->MaxPixelShader30InstructionSlots =
+        screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
+                                 PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
+    if (pCaps->MaxVertexShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
+        pCaps->MaxVertexShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
+    if (pCaps->MaxPixelShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
+        pCaps->MaxPixelShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
+    assert(pCaps->MaxVertexShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
+    assert(pCaps->MaxPixelShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
 
     /* 65535 is required, advertise more for GPUs with >= 2048 instruction slots */
     pCaps->MaxVShaderInstructionsExecuted = MAX2(65535, pCaps->MaxVertexShader30InstructionSlots * 32);
@@ -947,7 +977,7 @@ NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CreateDevice( struct NineAdapter9 *This,
                            UINT RealAdapter,
                            D3DDEVTYPE DeviceType,
@@ -996,7 +1026,8 @@ NineAdapter9_CreateDevice( struct NineAdapter9 *This,
 
     hr = NineDevice9_new(screen, &params, &caps, pPresentationParameters,
                          pD3D9, pPresentationGroup, This->ctx, FALSE, NULL,
-                         (struct NineDevice9 **)ppReturnedDeviceInterface);
+                         (struct NineDevice9 **)ppReturnedDeviceInterface,
+                         minor);
     if (FAILED(hr)) {
         DBG("Failed to create device.\n");
         return hr;
@@ -1006,7 +1037,7 @@ NineAdapter9_CreateDevice( struct NineAdapter9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineAdapter9_CreateDeviceEx( struct NineAdapter9 *This,
                              UINT RealAdapter,
                              D3DDEVTYPE DeviceType,
@@ -1057,7 +1088,8 @@ NineAdapter9_CreateDeviceEx( struct NineAdapter9 *This,
     hr = NineDevice9Ex_new(screen, &params, &caps, pPresentationParameters,
                            pFullscreenDisplayMode,
                            pD3D9Ex, pPresentationGroup, This->ctx,
-                           (struct NineDevice9Ex **)ppReturnedDeviceInterface);
+                           (struct NineDevice9Ex **)ppReturnedDeviceInterface,
+                           minor);
     if (FAILED(hr)) {
         DBG("Failed to create device.\n");
         return hr;