huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Win32 Vulkan platform
 *//*--------------------------------------------------------------------*/
 
#include "tcuWin32VulkanPlatform.hpp"
#include "tcuWin32Window.hpp"
 
#include "tcuFormatUtil.hpp"
#include "tcuFunctionLibrary.hpp"
#include "tcuVector.hpp"
 
#include "vkWsiPlatform.hpp"
 
#include "deUniquePtr.hpp"
#include "deMemory.h"
 
#pragma comment(lib, "version.lib")
 
namespace tcu
{
namespace win32
{
 
using de::MovePtr;
using de::UniquePtr;
 
DE_STATIC_ASSERT(sizeof(vk::pt::Win32InstanceHandle)    == sizeof(HINSTANCE));
DE_STATIC_ASSERT(sizeof(vk::pt::Win32WindowHandle)        == sizeof(HWND));
 
class VulkanWindow : public vk::wsi::Win32WindowInterface
{
public:
   VulkanWindow (MovePtr<win32::Window> window)
       : vk::wsi::Win32WindowInterface    (vk::pt::Win32WindowHandle(window->getHandle()))
       , m_window                        (window)
   {
   }
 
   void resize (const UVec2& newSize)
   {
       m_window->setSize((int)newSize.x(), (int)newSize.y());
   }
 
private:
   UniquePtr<win32::Window>    m_window;
};
 
class VulkanDisplay : public vk::wsi::Win32DisplayInterface
{
public:
   VulkanDisplay (HINSTANCE instance)
       : vk::wsi::Win32DisplayInterface    (vk::pt::Win32InstanceHandle(instance))
   {
   }
 
   vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
   {
       const HINSTANCE    instance    = (HINSTANCE)m_native.internal;
       const deUint32    width        = !initialSize ? 400 : initialSize->x();
       const deUint32    height        = !initialSize ? 300 : initialSize->y();
 
       return new VulkanWindow(MovePtr<win32::Window>(new win32::Window(instance, (int)width, (int)height)));
   }
};
 
class VulkanLibrary : public vk::Library
{
public:
   VulkanLibrary (void)
       : m_library    ("vulkan-1.dll")
       , m_driver    (m_library)
   {
   }
 
   const vk::PlatformInterface&    getPlatformInterface    (void) const
   {
       return m_driver;
   }
   const tcu::FunctionLibrary&        getFunctionLibrary        (void) const
   {
       return m_library;
   }
 
private:
   const tcu::DynamicFunctionLibrary    m_library;
   const vk::PlatformDriver            m_driver;
};
 
VulkanPlatform::VulkanPlatform (HINSTANCE instance)
   : m_instance(instance)
{
}
 
VulkanPlatform::~VulkanPlatform (void)
{
}
 
vk::Library* VulkanPlatform::createLibrary (void) const
{
   return new VulkanLibrary();
}
 
ULONG getStringRegKey (const std::string& regKey, const std::string& strValueName, std::string& strValue)
{
   HKEY    hKey;
   ULONG    nError;
   CHAR    szBuffer[512];
   DWORD    dwBufferSize = sizeof(szBuffer);
 
   nError = RegOpenKeyExA(HKEY_LOCAL_MACHINE, regKey.c_str(), 0, KEY_READ, &hKey);
 
   if (ERROR_SUCCESS == nError)
       nError = RegQueryValueExA(hKey, strValueName.c_str(), 0, DE_NULL, (LPBYTE)szBuffer, &dwBufferSize);
 
   if (ERROR_SUCCESS == nError)
       strValue = szBuffer;
 
   return nError;
}
 
void getWindowsBits (std::ostream& dst)
{
#if defined(_WIN64)
   dst << "64"; // 64-bit programs run only on Win64
   return;
#elif defined(_WIN32)
   BOOL is64 = false;
   // 32-bit programs run on both 32-bit and 64-bit Windows.
   // Function is defined from XP SP2 onwards, so we don't need to
   // check if it exists.
   if (IsWow64Process(GetCurrentProcess(), &is64))
   {
       if (is64)
           dst << "64";
       else
           dst << "32";
       return;
   }
#endif
#if !defined(_WIN64)
   // IsWow64Process returns failure or neither of
   // _WIN64 or _WIN32 is defined
   dst << "Unknown";
#endif
}
 
void getOSNameFromRegistry (std::ostream& dst)
{
   const char* keypath        = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
   std::string productname    = "Unknown";
   std::string releaseid    = "Unknown";
   std::string optional;
 
   getStringRegKey(keypath, "ProductName", productname);
   getStringRegKey(keypath, "ReleaseId", releaseid);
 
   getWindowsBits(dst);
 
   dst << " bit Windows Product: " << productname << ", Release: " << releaseid;
 
   if (ERROR_SUCCESS == getStringRegKey(keypath, "EditionID", optional))
   {
       dst << ", Edition: " << optional;
       if (ERROR_SUCCESS == getStringRegKey(keypath, "EditionSubstring", optional)
           && optional.length() > 0)
           dst << " " << optional;
   }
}
 
void getOSVersionFromDLL (std::ostream& dst)
{
   DWORD    buffer_size    = GetFileVersionInfoSize(("kernel32.dll"), DE_NULL);
   char*    buffer        = 0;
 
   if (buffer_size != 0)
   {
       buffer = new char[buffer_size];
       if (buffer != 0)
       {
           if (GetFileVersionInfo("kernel32.dll", 0, buffer_size, buffer))
           {
               VS_FIXEDFILEINFO*    version        = DE_NULL;
               UINT                version_len    = 0;
 
               if (VerQueryValue(buffer, "\\", (LPVOID*)&version, &version_len))
               {
                   dst << ", DLL Version: " << HIWORD(version->dwProductVersionMS) << "." << LOWORD(version->dwProductVersionMS)
                       << ", DLL Build: "   << HIWORD(version->dwProductVersionLS) << "." << LOWORD(version->dwProductVersionLS);
               }
           }
           delete[] buffer;
       }
   }
}
 
// Old windows version query APIs lie about the version number. There's no replacement
// API, and instead applications are supposed to queriy about capabilities instead of
// relying on operating system version numbers.
//
// Since we want to actually know the version number for debugging purposes, we need
// to use roundabout ways to fetch the information.
//
// The registry contains some useful strings, which we print out if the keys
// are available. The current official way to get version number is to look at a
// system DLL file and read its version number, so we do that too, in case the
// registry becomes unreliable in the future.
//
// If the DLL method fails, we simply don't print out anything about it.
// The minimal output from this function is "Windows Product: Unknown, Release: Unknown"
static void getOSInfo (std::ostream& dst)
{
   getOSNameFromRegistry(dst);
   getOSVersionFromDLL(dst);
}
 
const char* getProcessorArchitectureName (WORD arch)
{
   switch (arch)
   {
       case PROCESSOR_ARCHITECTURE_AMD64:        return "AMD64";
       case PROCESSOR_ARCHITECTURE_ARM:        return "ARM";
       case PROCESSOR_ARCHITECTURE_IA64:        return "IA64";
       case PROCESSOR_ARCHITECTURE_INTEL:        return "INTEL";
       case PROCESSOR_ARCHITECTURE_UNKNOWN:    return "UNKNOWN";
       default:                                return DE_NULL;
   }
}
 
static void getProcessorInfo (std::ostream& dst)
{
   SYSTEM_INFO    sysInfo;
 
   deMemset(&sysInfo, 0, sizeof(sysInfo));
   GetSystemInfo(&sysInfo);
 
   dst << "arch ";
   {
       const char* const    archName    = getProcessorArchitectureName(sysInfo.wProcessorArchitecture);
 
       if (archName)
           dst << archName;
       else
           dst << tcu::toHex(sysInfo.wProcessorArchitecture);
   }
 
   dst << ", level " << tcu::toHex(sysInfo.wProcessorLevel) << ", revision " << tcu::toHex(sysInfo.wProcessorRevision);
}
 
void VulkanPlatform::describePlatform (std::ostream& dst) const
{
   dst << "OS: ";
   getOSInfo(dst);
   dst << "\n";
 
   dst << "CPU: ";
   getProcessorInfo(dst);
   dst << "\n";
}
 
void VulkanPlatform::getMemoryLimits (vk::PlatformMemoryLimits& limits) const
{
   limits.totalSystemMemory                    = 256*1024*1024;
   limits.totalDeviceLocalMemory                = 128*1024*1024;
   limits.deviceMemoryAllocationGranularity    = 64*1024;
   limits.devicePageSize                        = 4096;
   limits.devicePageTableEntrySize                = 8;
   limits.devicePageTableHierarchyLevels        = 3;
}
 
vk::wsi::Display* VulkanPlatform::createWsiDisplay (vk::wsi::Type wsiType) const
{
   if (wsiType != vk::wsi::TYPE_WIN32)
       TCU_THROW(NotSupportedError, "WSI type not supported");
 
   return new VulkanDisplay(m_instance);
}
 
bool VulkanPlatform::hasDisplay (vk::wsi::Type wsiType)  const
{
   if (wsiType != vk::wsi::TYPE_WIN32)
       return false;
 
   return true;
}
 
} // win32
} // tcu