f597b5015e46c14ee4fb84ccf93055e5c02c7567
[mesa.git] / src / gallium / drivers / swr / swr_loader.cpp
1 /****************************************************************************
2 * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #include "memory/InitMemory.h"
25 #include "util/u_cpu_detect.h"
26 #include "util/u_dl.h"
27 #include "swr_public.h"
28 #include "swr_screen.h"
29
30 #include <stdio.h>
31
32 // Helper function to resolve the backend filename based on architecture
33 static bool
34 swr_initialize_screen_interface(struct swr_screen *screen, const char arch[])
35 {
36 #ifdef HAVE_SWR_BUILTIN
37 screen->pLibrary = NULL;
38 screen->pfnSwrGetInterface = SwrGetInterface;
39 screen->pfnSwrGetInterface = SwrGetTileInterface;
40 InitTilesTable();
41 fprintf(stderr, "(using: builtin).\n");
42 #else
43 char filename[256] = { 0 };
44 sprintf(filename, "%sswr%s%s", UTIL_DL_PREFIX, arch, UTIL_DL_EXT);
45
46 screen->pLibrary = util_dl_open(filename);
47 if (!screen->pLibrary) {
48 fprintf(stderr, "(skipping: %s).\n", util_dl_error());
49 return false;
50 }
51
52 util_dl_proc pApiProc = util_dl_get_proc_address(screen->pLibrary,
53 "SwrGetInterface");
54 util_dl_proc pTileApiProc = util_dl_get_proc_address(screen->pLibrary,
55 "SwrGetTileIterface");
56 util_dl_proc pInitFunc = util_dl_get_proc_address(screen->pLibrary,
57 "InitTilesTable");
58 if (!pApiProc || !pInitFunc || !pTileApiProc) {
59 fprintf(stderr, "(skipping: %s).\n", util_dl_error());
60 util_dl_close(screen->pLibrary);
61 screen->pLibrary = NULL;
62 return false;
63 }
64
65 screen->pfnSwrGetInterface = (PFNSwrGetInterface)pApiProc;
66 screen->pfnSwrGetTileInterface = (PFNSwrGetTileInterface)pTileApiProc;
67
68 SWR_ASSERT(screen->pfnSwrGetInterface != nullptr);
69 SWR_ASSERT(screen->pfnSwrGetTileInterface != nullptr);
70 SWR_ASSERT(pInitFunc != nullptr);
71
72 pInitFunc();
73
74 fprintf(stderr, "(using: %s).\n", filename);
75 #endif
76 return true;
77 }
78
79
80 struct pipe_screen *
81 swr_create_screen(struct sw_winsys *winsys)
82 {
83 struct pipe_screen *p_screen = swr_create_screen_internal(winsys);
84 if (!p_screen) {
85 return NULL;
86 }
87
88 struct swr_screen *screen = swr_screen(p_screen);
89 screen->is_knl = false;
90
91 util_cpu_detect();
92
93 if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512er) {
94 fprintf(stderr, "SWR detected KNL instruction support ");
95 #ifndef HAVE_SWR_KNL
96 fprintf(stderr, "(skipping: not built).\n");
97 #else
98 if (swr_initialize_screen_interface(screen, "KNL")) {
99 screen->is_knl = true;
100 return p_screen;
101 }
102 #endif
103 }
104
105 if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512bw) {
106 fprintf(stderr, "SWR detected SKX instruction support ");
107 #ifndef HAVE_SWR_SKX
108 fprintf(stderr, "(skipping not built).\n");
109 #else
110 if (swr_initialize_screen_interface(screen, "SKX"))
111 return p_screen;
112 #endif
113 }
114
115 if (util_cpu_caps.has_avx2) {
116 fprintf(stderr, "SWR detected AVX2 instruction support ");
117 #ifndef HAVE_SWR_AVX2
118 fprintf(stderr, "(skipping not built).\n");
119 #else
120 if (swr_initialize_screen_interface(screen, "AVX2"))
121 return p_screen;
122 #endif
123 }
124
125 if (util_cpu_caps.has_avx) {
126 fprintf(stderr, "SWR detected AVX instruction support ");
127 #ifndef HAVE_SWR_AVX
128 fprintf(stderr, "(skipping not built).\n");
129 #else
130 if (swr_initialize_screen_interface(screen, "AVX"))
131 return p_screen;
132 #endif
133 }
134
135 fprintf(stderr, "SWR could not initialize a supported CPU architecture.\n");
136 swr_destroy_screen_internal(&screen);
137
138 return NULL;
139 }
140
141
142 #ifdef _WIN32
143 // swap function called from libl_gdi.c
144
145 void
146 swr_gdi_swap(struct pipe_screen *screen,
147 struct pipe_resource *res,
148 void *hDC)
149 {
150 screen->flush_frontbuffer(screen,
151 res,
152 0, 0,
153 hDC,
154 NULL);
155 }
156
157 #endif /* _WIN32 */