driconfig: add a new engine name/version parameter
[mesa.git] / src / gallium / targets / d3dadapter9 / drm.c
index cb0c48f12bb0a02c454738b80527d9887fa4b1c9..819aa59468cd2b86223e63b557792fd29a7dd186 100644 (file)
@@ -20,6 +20,7 @@
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
+/* XXX: header order is slightly screwy here */
 #include "loader.h"
 
 #include "adapter9.h"
 #include "pipe/p_screen.h"
 #include "pipe/p_state.h"
 
-#include "target-helpers/inline_drm_helper.h"
-#include "target-helpers/inline_sw_helper.h"
+#include "target-helpers/drm_helper.h"
+#include "target-helpers/sw_helper.h"
 #include "state_tracker/drm_driver.h"
 
 #include "d3dadapter/d3dadapter9.h"
 #include "d3dadapter/drm.h"
 
-#include "xmlconfig.h"
-#include "xmlpool.h"
+#include "util/xmlconfig.h"
+#include "util/xmlpool.h"
 
-#include <libdrm/drm.h>
+#include "drm-uapi/drm.h"
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <stdio.h>
 
 #define DBG_CHANNEL DBG_ADAPTER
 
-#define VERSION_DWORD(hi, lo) \
-    ((DWORD)( \
-        ((DWORD)((hi) & 0xFFFF) << 16) | \
-         (DWORD)((lo) & 0xFFFF) \
-    ))
-
 const char __driConfigOptionsNine[] =
 DRI_CONF_BEGIN
     DRI_CONF_SECTION_PERFORMANCE
          DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
     DRI_CONF_SECTION_END
     DRI_CONF_SECTION_NINE
+        DRI_CONF_NINE_OVERRIDEVENDOR(-1)
         DRI_CONF_NINE_THROTTLE(-2)
         DRI_CONF_NINE_THREADSUBMIT("false")
+        DRI_CONF_NINE_ALLOWDISCARDDELAYEDRELEASE("true")
+        DRI_CONF_NINE_TEARFREEDISCARD("false")
+        DRI_CONF_NINE_CSMT(-1)
+        DRI_CONF_NINE_DYNAMICTEXTUREWORKAROUND("false")
+        DRI_CONF_NINE_SHADERINLINECONSTANTS("false")
     DRI_CONF_SECTION_END
 DRI_CONF_END;
 
-/* Regarding os versions, we should not define our own as that would simply be
- * weird. Defaulting to Win2k/XP seems sane considering the origin of D3D9. The
- * driver also defaults to being a generic D3D9 driver, which of course only
- * matters if you're actually using the DDI. */
-#define VERSION_HIGH    VERSION_DWORD(0x0006, 0x000E) /* winxp, d3d9 */
-#define VERSION_LOW     VERSION_DWORD(0x0000, 0x0001) /* version, build */
+struct fallback_card_config {
+    const char *name;
+    unsigned vendor_id;
+    unsigned device_id;
+} fallback_cards[] = {
+        {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */
+        {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */
+        {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */
+        {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */
+};
+
+/* prototypes */
+void
+d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
+                     unsigned fallback_ven,
+                     unsigned fallback_dev,
+                     const char* fallback_name );
+
+void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
+
+void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
 
 struct d3dadapter9drm_context
 {
@@ -88,54 +104,16 @@ drm_destroy( struct d3dadapter9_context *ctx )
     else if (ctx->hal)
         ctx->hal->destroy(ctx->hal);
 
-#if !GALLIUM_STATIC_TARGETS
     if (drm->swdev)
         pipe_loader_release(&drm->swdev, 1);
     if (drm->dev)
         pipe_loader_release(&drm->dev, 1);
-#endif
 
     close(drm->fd);
     FREE(ctx);
 }
 
-/* read a DWORD in the form 0xnnnnnnnn, which is how sysfs pci id stuff is
- * formatted. */
-static INLINE DWORD
-read_file_dword( const char *name )
-{
-    char buf[32];
-    int fd, r;
-
-    fd = open(name, O_RDONLY);
-    if (fd < 0) {
-        DBG("Unable to get PCI information from `%s'\n", name);
-        return 0;
-    }
-
-    r = read(fd, buf, 32);
-    close(fd);
-
-    return (r > 0) ? (DWORD)strtol(buf, NULL, 0) : 0;
-}
-
-/* sysfs doesn't expose the revision as its own file, so this function grabs a
- * dword at an offset in the raw PCI header. The reason this isn't used for all
- * data is that the kernel will make corrections but not expose them in the raw
- * header bytes. */
-static INLINE DWORD
-read_config_dword( int fd,
-                   unsigned offset )
-{
-    DWORD r = 0;
-
-    if (lseek(fd, offset, SEEK_SET) != offset) { return 0; }
-    if (read(fd, &r, 4) != 4) { return 0; }
-
-    return r;
-}
-
-static INLINE void
+static inline void
 get_bus_info( int fd,
               DWORD *vendorid,
               DWORD *deviceid,
@@ -152,50 +130,63 @@ get_bus_info( int fd,
         *subsysid = 0;
         *revision = 0;
     } else {
-        DBG("Unable to detect card. Fake GTX 680.\n");
-        *vendorid = 0x10de; /* NV GTX 680 */
-        *deviceid = 0x1180;
+        DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name);
+        *vendorid = fallback_cards[0].vendor_id;
+        *deviceid = fallback_cards[0].device_id;
         *subsysid = 0;
         *revision = 0;
     }
 }
 
-static INLINE void
+static inline void
 read_descriptor( struct d3dadapter9_context *ctx,
-                 int fd )
+                 int fd, int override_vendorid )
 {
+    unsigned i;
+    BOOL found;
     D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
 
     memset(drvid, 0, sizeof(*drvid));
     get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
                  &drvid->SubSysId, &drvid->Revision);
-
-    strncpy(drvid->Driver, "libd3dadapter9.so", sizeof(drvid->Driver));
-    strncpy(drvid->DeviceName, ctx->hal->get_name(ctx->hal), 32);
+    snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
+                 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
     snprintf(drvid->Description, sizeof(drvid->Description),
-             "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
-
-    drvid->DriverVersionLowPart = VERSION_LOW;
-    drvid->DriverVersionHighPart = VERSION_HIGH;
-
-    /* To make a pseudo-real GUID we use the PCI bus data and some string */
-    drvid->DeviceIdentifier.Data1 = drvid->VendorId;
-    drvid->DeviceIdentifier.Data2 = drvid->DeviceId;
-    drvid->DeviceIdentifier.Data3 = drvid->SubSysId;
-    memcpy(drvid->DeviceIdentifier.Data4, "Gallium3D", 8);
-
-    drvid->WHQLLevel = 1; /* This fakes WHQL validaion */
-
-    /* XXX Fake NVIDIA binary driver on Windows.
-     *
-     * OS version: 4=95/98/NT4, 5=2000, 6=2000/XP, 7=Vista, 8=Win7
-     */
-    strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
-    strncpy(drvid->Description, "NVIDIA GeForce GTX 680", sizeof(drvid->Description));
-    drvid->DriverVersionLowPart = VERSION_DWORD(12, 6658); /* minor, build */
-    drvid->DriverVersionHighPart = VERSION_DWORD(6, 15); /* OS, major */
-    drvid->SubSysId = 0;
-    drvid->Revision = 0;
+                 "%s", ctx->hal->get_name(ctx->hal));
+
+    if (override_vendorid > 0) {
+        found = FALSE;
+        /* fill in device_id and card name for fake vendor */
+        for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) {
+            if (fallback_cards[i].vendor_id == override_vendorid) {
+                DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n",
+                        fallback_cards[i].name,
+                        fallback_cards[i].vendor_id,
+                        fallback_cards[i].device_id);
+                drvid->VendorId = fallback_cards[i].vendor_id;
+                drvid->DeviceId = fallback_cards[i].device_id;
+                snprintf(drvid->Description, sizeof(drvid->Description),
+                             "%s", fallback_cards[i].name);
+                found = TRUE;
+                break;
+            }
+        }
+        if (!found) {
+            DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid);
+        }
+    }
+    /* choose fall-back vendor if necessary to allow
+     * the following functions to return sane results */
+    d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name);
+    /* fill in driver name and version info */
+    d3d_fill_driver_version(drvid);
+    /* override Description field with Windows like names */
+    d3d_fill_cardname(drvid);
+
+    /* this driver isn't WHQL certified */
+    drvid->WHQLLevel = 0;
+
+    /* this value is fixed */
     drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
     drvid->DeviceIdentifier.Data2 = 0x6e41;
     drvid->DeviceIdentifier.Data3 = 0x43ea;
@@ -207,7 +198,6 @@ read_descriptor( struct d3dadapter9_context *ctx,
     drvid->DeviceIdentifier.Data4[5] = 0x76;
     drvid->DeviceIdentifier.Data4[6] = 0x07;
     drvid->DeviceIdentifier.Data4[7] = 0x81;
-    drvid->WHQLLevel = 0;
 }
 
 static HRESULT WINAPI
@@ -216,75 +206,52 @@ drm_create_adapter( int fd,
 {
     struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
     HRESULT hr;
-    int i, different_device;
-    const struct drm_conf_ret *throttle_ret = NULL;
-    const struct drm_conf_ret *dmabuf_ret = NULL;
+    bool different_device;
     driOptionCache defaultInitOptions;
     driOptionCache userInitOptions;
     int throttling_value_user = -2;
-
-#if !GALLIUM_STATIC_TARGETS
-    const char *paths[] = {
-        getenv("D3D9_DRIVERS_PATH"),
-        getenv("D3D9_DRIVERS_DIR"),
-        PIPE_SEARCH_DIR
-    };
-#endif
+    int override_vendorid = -1;
 
     if (!ctx) { return E_OUTOFMEMORY; }
 
     ctx->base.destroy = drm_destroy;
 
+    /* Although the fd is provided from external source, mesa/nine
+     * takes ownership of it. */
     fd = loader_get_user_preferred_fd(fd, &different_device);
     ctx->fd = fd;
-    ctx->base.linear_framebuffer = !!different_device;
+    ctx->base.linear_framebuffer = different_device;
 
-#if GALLIUM_STATIC_TARGETS
-    ctx->base.hal = dd_create_screen(fd);
-#else
-    /* use pipe-loader to dlopen appropriate drm driver */
-    if (!pipe_loader_drm_probe_fd(&ctx->dev, fd, FALSE)) {
+    if (!pipe_loader_drm_probe_fd(&ctx->dev, fd)) {
         ERR("Failed to probe drm fd %d.\n", fd);
         FREE(ctx);
         close(fd);
         return D3DERR_DRIVERINTERNALERROR;
     }
 
-    /* use pipe-loader to create a drm screen (hal) */
-    ctx->base.hal = NULL;
-    for (i = 0; !ctx->base.hal && i < Elements(paths); ++i) {
-        if (!paths[i]) { continue; }
-        ctx->base.hal = pipe_loader_create_screen(ctx->dev, paths[i]);
-    }
-#endif
+    ctx->base.hal = pipe_loader_create_screen(ctx->dev);
     if (!ctx->base.hal) {
         ERR("Unable to load requested driver.\n");
         drm_destroy(&ctx->base);
         return D3DERR_DRIVERINTERNALERROR;
     }
 
-#if GALLIUM_STATIC_TARGETS
-    dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
-    throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
-#else
-    dmabuf_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_SHARE_FD);
-    throttle_ret = pipe_loader_configuration(ctx->dev, DRM_CONF_THROTTLE);
-#endif // GALLIUM_STATIC_TARGETS
-    if (!dmabuf_ret || !dmabuf_ret->val.val_bool) {
+    if (!ctx->base.hal->get_param(ctx->base.hal, PIPE_CAP_DMABUF)) {
         ERR("The driver is not capable of dma-buf sharing."
             "Abandon to load nine state tracker\n");
         drm_destroy(&ctx->base);
         return D3DERR_DRIVERINTERNALERROR;
     }
 
-    if (throttle_ret && throttle_ret->val.val_int != -1) {
-        ctx->base.throttling = TRUE;
-        ctx->base.throttling_value = throttle_ret->val.val_int;
-    } else
-        ctx->base.throttling = FALSE;
+    /* Previously was set to PIPE_CAP_MAX_FRAMES_IN_FLIGHT,
+     * but the change of value of this cap to 1 seems to cause
+     * regressions. */
+    ctx->base.throttling_value = 2;
+    ctx->base.throttling = ctx->base.throttling_value > 0;
 
     driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
-    driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine");
+    driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
+                        "nine", NULL, NULL, 0);
     if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
         throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
         if (throttling_value_user == -1)
@@ -300,41 +267,59 @@ drm_create_adapter( int fd,
     else
         ctx->base.vblank_mode = 1;
 
-    if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL)) {
+    if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL))
         ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit");
-        if (ctx->base.thread_submit && (throttling_value_user == -2 || throttling_value_user == 0)) {
-            ctx->base.throttling_value = 0;
-        } else if (ctx->base.thread_submit) {
-            DBG("You have set a non standard throttling value in combination with thread_submit."
-                "We advise to use a throttling value of -2/0");
-        }
-        if (ctx->base.thread_submit && !different_device)
-            DBG("You have set thread_submit but do not use a different device than the server."
-                "You should not expect any benefit.");
+    else
+        ctx->base.thread_submit = different_device;
+
+    if (driCheckOption(&userInitOptions, "override_vendorid", DRI_INT)) {
+        override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid");
+    }
+
+    if (driCheckOption(&userInitOptions, "discard_delayed_release", DRI_BOOL))
+        ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release");
+    else
+        ctx->base.discard_delayed_release = TRUE;
+
+    if (driCheckOption(&userInitOptions, "tearfree_discard", DRI_BOOL))
+        ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard");
+    else
+        ctx->base.tearfree_discard = FALSE;
+
+    if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) {
+        ERR("tearfree_discard requires discard_delayed_release\n");
+        ctx->base.tearfree_discard = FALSE;
     }
 
+    if (driCheckOption(&userInitOptions, "csmt_force", DRI_INT))
+        ctx->base.csmt_force = driQueryOptioni(&userInitOptions, "csmt_force");
+    else
+        ctx->base.csmt_force = -1;
+
+    if (driCheckOption(&userInitOptions, "dynamic_texture_workaround", DRI_BOOL))
+        ctx->base.dynamic_texture_workaround = driQueryOptionb(&userInitOptions, "dynamic_texture_workaround");
+    else
+        ctx->base.dynamic_texture_workaround = FALSE;
+
+    if (driCheckOption(&userInitOptions, "shader_inline_constants", DRI_BOOL))
+        ctx->base.shader_inline_constants = driQueryOptionb(&userInitOptions, "shader_inline_constants");
+    else
+        ctx->base.shader_inline_constants = FALSE;
+
     driDestroyOptionCache(&userInitOptions);
     driDestroyOptionInfo(&defaultInitOptions);
 
-#if GALLIUM_STATIC_TARGETS
-    ctx->base.ref = ninesw_create_screen(ctx->base.hal);
-#else
     /* wrap it to create a software screen that can share resources */
-    if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal)) {
-        ctx->base.ref = NULL;
-        for (i = 0; !ctx->base.ref && i < Elements(paths); ++i) {
-            if (!paths[i]) { continue; }
-            ctx->base.ref = pipe_loader_create_screen(ctx->swdev, paths[i]);
-        }
-    }
-#endif
+    if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal))
+        ctx->base.ref = pipe_loader_create_screen(ctx->swdev);
+
     if (!ctx->base.ref) {
         ERR("Couldn't wrap drm screen to swrast screen. Software devices "
             "will be unavailable.\n");
     }
 
     /* read out PCI info */
-    read_descriptor(&ctx->base, fd);
+    read_descriptor(&ctx->base, fd, override_vendorid);
 
     /* create and return new ID3DAdapter9 */
     hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);