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
33static bool
34swr_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->pfnSwrGetTileInterface = SwrGetTileIterface;
40   InitTilesTable();
41   swr_print_info("(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   swr_print_info("(using: %s).\n", filename);
75#endif
76
77   return true;
78}
79
80
81struct pipe_screen *
82swr_create_screen(struct sw_winsys *winsys)
83{
84   struct pipe_screen *p_screen = swr_create_screen_internal(winsys);
85   if (!p_screen) {
86      return NULL;
87   }
88
89   struct swr_screen *screen = swr_screen(p_screen);
90   screen->is_knl = false;
91
92   util_cpu_detect();
93
94   if (util_get_cpu_caps()->has_avx512f && util_get_cpu_caps()->has_avx512er) {
95      swr_print_info("SWR detected KNL instruction support ");
96#ifndef HAVE_SWR_KNL
97      swr_print_info("(skipping: not built).\n");
98#else
99      if (swr_initialize_screen_interface(screen, "KNL")) {
100         screen->is_knl = true;
101         return p_screen;
102      }
103#endif
104   }
105
106   if (util_get_cpu_caps()->has_avx512f && util_get_cpu_caps()->has_avx512bw) {
107      swr_print_info("SWR detected SKX instruction support ");
108#ifndef HAVE_SWR_SKX
109      swr_print_info("(skipping not built).\n");
110#else
111      if (swr_initialize_screen_interface(screen, "SKX"))
112         return p_screen;
113#endif
114   }
115
116   if (util_get_cpu_caps()->has_avx2) {
117      swr_print_info("SWR detected AVX2 instruction support ");
118#ifndef HAVE_SWR_AVX2
119      swr_print_info("(skipping not built).\n");
120#else
121      if (swr_initialize_screen_interface(screen, "AVX2"))
122         return p_screen;
123#endif
124   }
125
126   if (util_get_cpu_caps()->has_avx) {
127      swr_print_info("SWR detected AVX instruction support ");
128#ifndef HAVE_SWR_AVX
129      swr_print_info("(skipping not built).\n");
130#else
131      if (swr_initialize_screen_interface(screen, "AVX"))
132         return p_screen;
133#endif
134   }
135
136   fprintf(stderr, "SWR could not initialize a supported CPU architecture.\n");
137   swr_destroy_screen_internal(&screen);
138
139   return NULL;
140}
141
142
143#ifdef _WIN32
144// swap function called from libl_gdi.c
145
146void
147swr_gdi_swap(struct pipe_screen *screen,
148             struct pipe_context *ctx,
149             struct pipe_resource *res,
150             void *hDC)
151{
152   screen->flush_frontbuffer(screen,
153                             ctx,
154                             res,
155                             0, 0,
156                             hDC,
157                             NULL);
158}
159
160#endif /* _WIN32 */
161