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 307 308 309 310 311 312
|
/*
** Copyright (c) 2021 Broadcom, Inc.
** Copyright (c) 2021 LunarG, Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/
#include "application/display_window.h"
#include "application/application.h"
#include "util/logging.h"
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(application)
DisplayWindow::DisplayWindow(DisplayContext* display_context) : display_context_(display_context)
{
assert(display_context_ != nullptr);
}
VkResult DisplayWindow::SelectPhysicalDevice(const encode::InstanceTable* table,
VkInstance instance,
VkPhysicalDevice* physical_device) const
{
assert(table);
uint32_t physical_device_count = 0;
VkResult error = table->EnumeratePhysicalDevices(instance, &physical_device_count, nullptr);
if (error)
{
return error;
}
if (!physical_device_count)
{
return VK_ERROR_INITIALIZATION_FAILED;
}
std::vector<VkPhysicalDevice> physical_devices(physical_device_count);
error = table->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.data());
if (error)
{
return error;
}
// choose a 1st discrete GPU or just the 1st one
*physical_device = physical_devices[0];
for (auto device : physical_devices)
{
VkPhysicalDeviceProperties properties;
table->GetPhysicalDeviceProperties(device, &properties);
if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
{
*physical_device = device;
break;
}
}
return VK_SUCCESS;
}
VkResult DisplayWindow::SelectDisplay(const encode::InstanceTable* table,
VkPhysicalDevice physical_device,
VkDisplayKHR* display) const
{
assert(table);
uint32_t num_displays;
VkResult error = table->GetPhysicalDeviceDisplayPropertiesKHR(physical_device, &num_displays, nullptr);
if (error)
{
return error;
}
if (!num_displays)
{
return VK_ERROR_INITIALIZATION_FAILED;
}
num_displays = 1;
VkDisplayPropertiesKHR display_properties;
error = table->GetPhysicalDeviceDisplayPropertiesKHR(physical_device, &num_displays, &display_properties);
if (error && error != VK_INCOMPLETE)
{
return error;
}
*display = display_properties.display;
return VK_SUCCESS;
}
VkResult DisplayWindow::SelectMode(const encode::InstanceTable* table,
VkPhysicalDevice physical_device,
VkDisplayKHR display,
VkDisplayModePropertiesKHR* mode_props) const
{
assert(table);
uint32_t num_modes;
VkResult error = table->GetDisplayModePropertiesKHR(physical_device, display, &num_modes, nullptr);
if (error)
{
return error;
}
if (!num_modes)
{
return VK_ERROR_INITIALIZATION_FAILED;
}
num_modes = 1;
error = table->GetDisplayModePropertiesKHR(physical_device, display, &num_modes, mode_props);
if (error && error != VK_INCOMPLETE)
{
return error;
}
return VK_SUCCESS;
}
VkResult DisplayWindow::SelectPlane(const encode::InstanceTable* table,
VkPhysicalDevice physical_device,
VkDisplayKHR display,
uint32_t* plane_index,
VkDisplayPlanePropertiesKHR* plane_props) const
{
assert(table);
uint32_t num_planes;
VkResult error = table->GetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &num_planes, nullptr);
if (error)
{
return error;
}
if (!num_planes)
{
return VK_ERROR_INITIALIZATION_FAILED;
}
std::vector<VkDisplayPlanePropertiesKHR> plane_properties(num_planes);
error = table->GetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &num_planes, plane_properties.data());
if (error && error != VK_INCOMPLETE)
{
return error;
}
for (uint32_t i = 0; i < plane_properties.size(); ++i)
{
if ((plane_properties[i].currentDisplay != VK_NULL_HANDLE) && (plane_properties[i].currentDisplay != display))
{
continue;
}
uint32_t num_supported;
error = table->GetDisplayPlaneSupportedDisplaysKHR(physical_device, i, &num_supported, nullptr);
if (error)
{
return error;
}
if (!num_supported)
{
continue;
}
std::vector<VkDisplayKHR> supported_displays(num_supported);
error =
table->GetDisplayPlaneSupportedDisplaysKHR(physical_device, i, &num_supported, supported_displays.data());
if (error)
{
return error;
}
for (uint32_t j = 0; j < num_supported; ++j)
{
if (supported_displays[j] == display)
{
*plane_index = i;
*plane_props = plane_properties[i];
return VK_SUCCESS;
}
}
}
return VK_ERROR_INITIALIZATION_FAILED;
}
std::string DisplayWindow::GetWsiExtension() const
{
return VK_KHR_DISPLAY_EXTENSION_NAME;
}
VkResult DisplayWindow::CreateSurface(const encode::InstanceTable* table,
VkInstance instance,
VkFlags flags,
VkSurfaceKHR* pSurface)
{
VkResult error = VK_ERROR_INITIALIZATION_FAILED;
if (table)
{
VkPhysicalDevice physical_device;
error = SelectPhysicalDevice(table, instance, &physical_device);
if (error)
{
GFXRECON_LOG_ERROR("Failed to select physical device");
return error;
}
VkDisplayKHR display;
error = SelectDisplay(table, physical_device, &display);
if (error)
{
GFXRECON_LOG_ERROR("Failed to select display");
return error;
}
VkDisplayModePropertiesKHR mode_props;
error = SelectMode(table, physical_device, display, &mode_props);
if (error)
{
GFXRECON_LOG_ERROR("Failed to select display mode");
return error;
}
uint32_t plane_index;
VkDisplayPlanePropertiesKHR plane_props;
error = SelectPlane(table, physical_device, display, &plane_index, &plane_props);
if (error)
{
GFXRECON_LOG_ERROR("Failed to select display plane");
return error;
}
VkExtent2D image_extent;
image_extent.width = mode_props.parameters.visibleRegion.width;
image_extent.height = mode_props.parameters.visibleRegion.height;
VkDisplaySurfaceCreateInfoKHR surface_create_info{ VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR,
nullptr,
0,
mode_props.displayMode,
plane_index,
plane_props.currentStackIndex,
VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
1.0,
VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
image_extent };
error = table->CreateDisplayPlaneSurfaceKHR(instance, &surface_create_info, nullptr, pSurface);
if (error)
{
GFXRECON_LOG_ERROR("Failed to create display plane surface");
return error;
}
}
return error;
}
void DisplayWindow::DestroySurface(const encode::InstanceTable* table, VkInstance instance, VkSurfaceKHR surface)
{
if (table)
{
table->DestroySurfaceKHR(instance, surface, nullptr);
}
}
DisplayWindowFactory::DisplayWindowFactory(DisplayContext* display_context) : display_context_(display_context)
{
assert(display_context_);
}
DisplayWindowFactory::~DisplayWindowFactory() {}
decode::Window*
DisplayWindowFactory::Create(const int32_t x, const int32_t y, const uint32_t width, const uint32_t height)
{
GFXRECON_UNREFERENCED_PARAMETER(x);
GFXRECON_UNREFERENCED_PARAMETER(y);
GFXRECON_UNREFERENCED_PARAMETER(width);
GFXRECON_UNREFERENCED_PARAMETER(height);
// Display has a single "window" whose lifetime is managed by DisplayContext.
return display_context_->GetWindow();
}
void DisplayWindowFactory::Destroy(decode::Window* window)
{
// Display has a single "window" whose lifetime is managed by DisplayContext.
GFXRECON_UNREFERENCED_PARAMETER(window);
}
VkBool32 DisplayWindowFactory::GetPhysicalDevicePresentationSupport(const encode::InstanceTable* table,
VkPhysicalDevice physical_device,
uint32_t queue_family_index)
{
GFXRECON_UNREFERENCED_PARAMETER(table);
GFXRECON_UNREFERENCED_PARAMETER(physical_device);
GFXRECON_UNREFERENCED_PARAMETER(queue_family_index);
return VK_TRUE;
}
GFXRECON_END_NAMESPACE(application)
GFXRECON_END_NAMESPACE(gfxrecon)
|