diff --git a/MobileGL/BackendCaps.h b/MobileGL/BackendCaps.h index fbd267af..42fdb353 100644 --- a/MobileGL/BackendCaps.h +++ b/MobileGL/BackendCaps.h @@ -103,9 +103,9 @@ namespace MobileGL { // GL_EXT_clip_control on DirectGLES. DirectVulkan applies clip control in its own // position fixup and does not read this. Bool ClipControl = false; - // GL_OVR_multiview and GL_OVR_multiview2, each only where the driver has that same - // extension (and, for the first, glFramebufferTextureMultiviewOVR). DirectGLES only: - // DirectVulkan has no multiview render pass route and leaves both false. + // GL_OVR_multiview and GL_OVR_multiview2. DirectGLES: each where the driver has that + // same extension (and, for the first, glFramebufferTextureMultiviewOVR). DirectVulkan: + // both where the device was created with the multiview feature. Bool Multiview = false; Bool Multiview2 = false; }; diff --git a/MobileGL/MG_Backend/DirectVulkan/BackendObject_DirectVulkan.cpp b/MobileGL/MG_Backend/DirectVulkan/BackendObject_DirectVulkan.cpp index c37a979d..2902517c 100644 --- a/MobileGL/MG_Backend/DirectVulkan/BackendObject_DirectVulkan.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/BackendObject_DirectVulkan.cpp @@ -921,6 +921,15 @@ namespace MobileGL::MG_Backend::DirectVulkan { if (features.CullDistance) { extensions.push_back(E_GL_ARB_cull_distance); } + // Multiview render passes (VkRenderPassMultiviewCreateInfo) and the ViewIndex built-in. + // Vulkan puts no restriction on what ViewIndex may feed, so the second string comes with + // the first. + if (features.Multiview) { + extensions.push_back(E_GL_OVR_multiview); + if (features.Multiview2) { + extensions.push_back(E_GL_OVR_multiview2); + } + } // Vulkan's drawIndirectFirstInstance feature is optional. Direct base-instance calls work // without it, but ARB_base_instance also promises non-zero firstInstance in GPU indirect // commands; the renderer supplies true only when that word is legal and gl_InstanceID can @@ -1085,6 +1094,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { } void BackendObject_DirectVulkan::UpdateAdvertisedExtensions() { + // GL_MAX_VIEWS_OVR beside the strings it decides, so the two cannot disagree. + m_dynamicParameters.MaxViews = + pVulkanRenderer != nullptr ? static_cast(pVulkanRenderer->GetMultiviewViewCount()) : 0; // GL_ARB_timer_query gates MC's F3 GPU% (LWJGL checks the extension // string). InitCapabilities runs after InitWindowSurface has created // and initialized the renderer, so the advertisement can be gated on @@ -1105,7 +1117,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { m_vulkanCaps.SupportsImageCubeArray, AdvertisedFeatureSupport{.TessellationShader = m_vulkanCaps.SupportsTessellationShader, .CullDistance = - m_vulkanCaps.SupportsShaderCullDistance && m_vulkanCaps.MaxCullDistances > 0}); + m_vulkanCaps.SupportsShaderCullDistance && m_vulkanCaps.MaxCullDistances > 0, + .Multiview = m_dynamicParameters.MaxViews > 0, + .Multiview2 = m_dynamicParameters.MaxViews > 0}); // GL_EXT_fragment_shading_rate rides on VK_KHR_fragment_shading_rate's pipeline rate, and // its _primitive companion on the primitive rate; both only when the renderer enabled them. // _attachment is not advertised: no framebuffer carries a rate attachment to this backend. diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.h index a5046cff..6b671fdd 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.h @@ -45,6 +45,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { IntVec2 extent = {0, 0}; // VkFramebufferCreateInfo::layers of the entry's framebuffer (>1 for layered GL attachments). Uint32 layers = 1; + // VkRenderPassMultiviewCreateInfo's view mask, zero when the pass is not multiview. Its + // clear variants carry the same mask, which render-pass compatibility requires. + Uint32 viewMask = 0; // Frame counter value of the last GetOrCreateRenderPass hit; drives cache eviction. Uint64 lastUsedFrame = 0; // P15 S0 (wire passes only): what the render pass was created from, and the render @@ -69,6 +72,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { std::swap(sampleCount, that.sampleCount); std::swap(extent, that.extent); std::swap(layers, that.layers); + std::swap(viewMask, that.viewMask); std::swap(lastUsedFrame, that.lastUsedFrame); std::swap(wireAttachments, that.wireAttachments); std::swap(wireColors, that.wireColors); @@ -105,6 +109,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { std::swap(sampleCount, that.sampleCount); std::swap(extent, that.extent); std::swap(layers, that.layers); + std::swap(viewMask, that.viewMask); std::swap(lastUsedFrame, that.lastUsedFrame); std::swap(wireAttachments, that.wireAttachments); std::swap(wireColors, that.wireColors); diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h index 11209a6e..d09a6e9a 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h @@ -407,6 +407,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { // honored rather than accepted-and-ignored. Bool IsSamplerAnisotropySupported() const { return m_samplerAnisotropyFeatureEnabled; } const FragmentShadingRateSupport& GetFragmentShadingRateSupport() const { return m_fragmentShadingRate; } + // GL_MAX_VIEWS_OVR: the multiview feature's maxMultiviewViewCount, capped at the 32 views + // a render pass view mask can name; zero when the device was created without multiview. + Uint32 GetMultiviewViewCount() const { return m_multiviewViewCount; } // ARB_base_instance extends indirect command records with a non-zero firstInstance and // requires gl_InstanceID to remain zero-based. Vulkan needs both features to honor that // complete contract: one legalizes the command word, the other enables the shader rebase. @@ -589,6 +592,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { // Zero for the default framebuffer, which no arm that reads this ever reaches. VkImageUsageFlags usageFlags = 0; Uint32 level = 0, layer = 0, layers = 1, levels = 1; + // GL_OVR_multiview: the surface's view count, zero when it is not multiview. A + // multiview surface's layers are its views, from `layer` on. + Uint32 views = 0; Bool isDefault = false; // Set when isDefault: the swapchain image index this role resolved to. Uint32 swapchainImageIndex = 0; @@ -877,6 +883,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { Vector colorReferences; Uint32 depthReference = VK_ATTACHMENT_UNUSED; Uint32 width = 0, height = 0, layers = 1; + // VkRenderPassMultiviewCreateInfo's view mask, zero for a pass that is not multiview + // (whose framebuffer then has `layers` 1). + Uint32 viewMask = 0; // The rasterization sample count of a pass with no attachments (0 otherwise), where // nothing else in the key would tell two such passes apart. Uint32 noAttachmentSamples = 0; @@ -1262,6 +1271,10 @@ namespace MobileGL::MG_Backend::DirectVulkan { // Null / all-false on a device without pipelineFragmentShadingRate. PFN_vkCmdSetFragmentShadingRateKHR s_vkCmdSetFragmentShadingRateKHR = nullptr; FragmentShadingRateSupport m_fragmentShadingRate{}; + // The multiview feature (Vulkan 1.1 core), which backs GL_OVR_multiview: chained into the + // device create info, so it lives as long as the device creation that reads it. + VkPhysicalDeviceMultiviewFeatures m_multiviewFeatures{}; + Uint32 m_multiviewViewCount = 0; // VK_EXT_transform_feedback (GL transform feedback capture) Bool m_transformFeedbackFeatureEnabled = false; @@ -1752,6 +1765,9 @@ namespace MobileGL::MG_Backend::DirectVulkan { static Bool EnableOptionalDeviceExtension(const Vector& availableExtensions, Vector& inOutEnabledExtensions, const char* extensionName); + void ChainMultiviewFeature(PFN_vkGetPhysicalDeviceFeatures2 getFeatures2, + PFN_vkGetPhysicalDeviceProperties2 getProperties2, + VkDeviceCreateInfo& deviceCreateInfo); void ResolveOptionalDeviceExtensions(const Vector& availableExtensions, Vector& inOutEnabledExtensions); static Bool IsNecessaryDeviceExtensionSupported(VkPhysicalDevice device); diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRendererDevice.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRendererDevice.cpp index da60e268..a1a98d72 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRendererDevice.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRendererDevice.cpp @@ -912,6 +912,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { if (!m_shaderDrawParametersFeatureEnabled) { MGLOG_W("shaderDrawParameters is unavailable; shaders using gl_DrawID/gl_BaseInstance will not work"); } + ChainMultiviewFeature(getPhysicalDeviceFeatures2, getPhysicalDeviceProperties2, deviceCreateInfo); // primitiveTopologyListRestart lets primitive restart work on *list* topologies (strip/fan // restart needs no feature). Optional; enabled via VK_EXT_primitive_topology_list_restart. @@ -2335,6 +2336,39 @@ namespace MobileGL::MG_Backend::DirectVulkan { return true; } + // Multiview for GL_OVR_multiview, on a Vulkan 1.1 device that has it and at least the two views + // GL requires. Only the base feature: GL refuses a multiview draw with a geometry or + // tessellation stage, so their multiview features are never needed. + void VulkanRenderer::ChainMultiviewFeature(PFN_vkGetPhysicalDeviceFeatures2 getFeatures2, + PFN_vkGetPhysicalDeviceProperties2 getProperties2, + VkDeviceCreateInfo& deviceCreateInfo) { + m_multiviewViewCount = 0; + m_multiviewFeatures = {}; + m_multiviewFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES; + if (m_physicalDevice.properties.apiVersion < VK_API_VERSION_1_1 || getFeatures2 == nullptr || + getProperties2 == nullptr) + return; + VkPhysicalDeviceFeatures2 featureQuery{}; + featureQuery.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; + featureQuery.pNext = &m_multiviewFeatures; + getFeatures2(m_physicalDevice.handle, &featureQuery); + VkPhysicalDeviceMultiviewProperties multiviewProperties{}; + multiviewProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES; + VkPhysicalDeviceProperties2 propertyQuery{}; + propertyQuery.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; + propertyQuery.pNext = &multiviewProperties; + getProperties2(m_physicalDevice.handle, &propertyQuery); + const Bool usable = m_multiviewFeatures.multiview == VK_TRUE && multiviewProperties.maxMultiviewViewCount >= 2; + MGLOG_I("Multiview (GL_OVR_multiview): %s, maxMultiviewViewCount %u", usable ? "enabled" : "unavailable", + multiviewProperties.maxMultiviewViewCount); + if (!usable) return; + m_multiviewFeatures.pNext = const_cast(deviceCreateInfo.pNext); + m_multiviewFeatures.multiviewGeometryShader = VK_FALSE; + m_multiviewFeatures.multiviewTessellationShader = VK_FALSE; + deviceCreateInfo.pNext = &m_multiviewFeatures; + m_multiviewViewCount = std::min(multiviewProperties.maxMultiviewViewCount, 32u); + } + void VulkanRenderer::ResolveOptionalDeviceExtensions(const Vector& availableExtensions, Vector& inOutEnabledExtensions) { m_drawIndirectCountExtensionEnabled = EnableOptionalDeviceExtension(availableExtensions, inOutEnabledExtensions, diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireDraw.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireDraw.cpp index 69906b20..4e544c7e 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireDraw.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireDraw.cpp @@ -142,6 +142,17 @@ namespace MobileGL::MG_Backend::DirectVulkan { return layered(fbo.Depth) || layered(fbo.Stencil); } + // The multiview chain of a wire draw pass and of its clear variants: one subpass that renders + // the views `viewMask` names. The mask is read through the pointer, so it has to outlive the + // render pass creation, which is why it is taken by reference. + static VkRenderPassMultiviewCreateInfo MakeWireMultiviewInfo(const Uint32& viewMask) { + VkRenderPassMultiviewCreateInfo info{}; + info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO; + info.subpassCount = 1; + info.pViewMasks = &viewMask; + return info; + } + [[noreturn]] static void WireBufferLegacyFatal() { MG_Pipe::MGPipeSessionFail(MG_Pipe::MGPipeFatalFamily::RoleViolation, "MGPipe: Fatal{RoleViolation, \"buffer-legacy-arm\"} (Magma P7 buffer consumer)"); @@ -473,6 +484,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { VkAttachmentReference depth{VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL}; VkExtent2D extent{~0u, ~0u}; Uint32 layers = ~0u; + Uint32 views = 0; VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT; Uint64 passHash = 0; auto& passKey = scratch.passKey; @@ -499,6 +511,18 @@ namespace MobileGL::MG_Backend::DirectVulkan { image = ResolveWireImage(fbo, surface, aspect, /*isWriteTarget=*/true); } if (!image.image) return VK_ATTACHMENT_UNUSED; + // Only a pending clear of exactly this attachment's level and layers can become its + // load op. One of other layers (another layer of a multiview or layered attachment) + // would otherwise stay pending past this pass and land on top of its draws. + if (image.resource != nullptr && + std::any_of(image.resource->pendingClears.begin(), image.resource->pendingClears.end(), + [&](const VkTextureManager::WirePendingClear& entry) { + return entry.level != image.level || entry.layer != image.layer || + entry.layers != image.layers; + })) { + MaterializeWirePendingClears(*image.resource); + image.layout = image.resource->layout; + } m_textureManager->FlushPendingUploads(); m_wirePassMemo.attachments.push_back( {image.resource, image.storage, surface.Kind == MG_Pipe::kMGPipeSurfaceKindRenderbuffer}); @@ -539,6 +563,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { extent.width = std::min(extent.width, image.extent.width); extent.height = std::min(extent.height, image.extent.height); layers = std::min(layers, image.layers); + views = std::max(views, image.views); samples = image.samples; passHash = XXH64(&description, sizeof(description), passHash); // Marked below, once the pass is known: a continued pass repeats the same marks. @@ -603,6 +628,14 @@ namespace MobileGL::MG_Backend::DirectVulkan { passKey.noAttachmentSamples = static_cast(samples); passHash = XXH64(&samples, sizeof(samples), passHash); } + // GL_OVR_multiview: one view per attachment layer, rendered through a single framebuffer + // layer, which is what a multiview render pass requires of its framebuffer. + const Uint32 viewMask = views == 0 ? 0u : (views >= 32 ? ~0u : (1u << views) - 1u); + if (viewMask != 0) { + layers = 1; + passHash = XXH64(&viewMask, sizeof(viewMask), passHash); + } + passKey.viewMask = viewMask; passKey.width = extent.width; passKey.height = extent.height; passKey.layers = layers; @@ -623,6 +656,8 @@ namespace MobileGL::MG_Backend::DirectVulkan { passInfo.pAttachments = attachments.data(); passInfo.subpassCount = 1; passInfo.pSubpasses = &subpass; + const VkRenderPassMultiviewCreateInfo multiview = MakeWireMultiviewInfo(passKey.viewMask); + if (viewMask != 0) passInfo.pNext = &multiview; continuing = m_wireDrawPass && m_wireDrawPassKey == passKey; { const auto* active = VkRenderPassManager::GetActiveRenderPass(); @@ -665,6 +700,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { created.sampleCount = samples; created.extent = {static_cast(extent.width), static_cast(extent.height)}; created.layers = layers; + created.viewMask = viewMask; created.hash = created.compatibilityHash = XXH64(colors.data(), colors.size() * sizeof(colors[0]), passHash); created.wireAttachments.assign(attachments.begin(), attachments.end()); @@ -709,6 +745,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { for (const auto& color : colors) pipelinePassKey.colorReferences.push_back(color.attachment); pipelinePassKey.depthReference = depth.attachment; + pipelinePassKey.viewMask = viewMask; m_wireDrawPassCompatibilityId = m_wireRenderPassCompatibility.Intern(pipelinePassKey); } } // !passContinues @@ -1401,6 +1438,8 @@ namespace MobileGL::MG_Backend::DirectVulkan { info.pAttachments = descriptions.data(); info.subpassCount = 1; info.pSubpasses = &subpass; + const VkRenderPassMultiviewCreateInfo multiview = MakeWireMultiviewInfo(begun.viewMask); + if (begun.viewMask != 0) info.pNext = &multiview; VkRenderPass variant = VK_NULL_HANDLE; VK_VERIFY(vkCreateRenderPass(m_device, &info, nullptr, &variant)); begun.clearVariants.push_back({variantKey, variant}); diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.cpp index a5e4e716..088b1b1f 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.cpp @@ -721,6 +721,10 @@ namespace MobileGL::MG_Backend::DirectVulkan { out.layer = layer; out.is3D = resource->depth > 1; out.layers = surface.Layered ? (out.is3D ? std::max(resource->depth >> level, 1u) : availableLayers) : 1; + if (!renderbuffer && surface.NumViews != 0) { + out.views = surface.NumViews; + out.layers = surface.NumViews; + } const Bool oneDimensional = resource->viewType == VK_IMAGE_VIEW_TYPE_1D || resource->viewType == VK_IMAGE_VIEW_TYPE_1D_ARRAY; out.viewType = oneDimensional ? (out.layers > 1 ? VK_IMAGE_VIEW_TYPE_1D_ARRAY : VK_IMAGE_VIEW_TYPE_1D) diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireRenderPassCompatibility.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireRenderPassCompatibility.h index 41503374..bbe00e1f 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireRenderPassCompatibility.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireRenderPassCompatibility.h @@ -11,14 +11,16 @@ #include "../VkIncludes.h" namespace MobileGL::MG_Backend::DirectVulkan { - // The wire draw pass has one graphics subpass, no input attachments, - // multiview or extensions. Its Vulkan pipeline compatibility is formats, - // sample counts and the ordered attachment references, not native handles, - // image identity, dimensions, load/store operations or image layouts. + // The wire draw pass has one graphics subpass, no input attachments and, + // besides multiview, no extensions. Its Vulkan pipeline compatibility is + // formats, sample counts, the ordered attachment references and the view + // mask, not native handles, image identity, dimensions, load/store + // operations or image layouts. struct WireRenderPassCompatibilityKey { Vector attachmentFormatsAndSamples; Vector colorReferences; Uint32 depthReference = VK_ATTACHMENT_UNUSED; + Uint32 viewMask = 0; Bool operator==(const WireRenderPassCompatibilityKey&) const = default; }; @@ -35,6 +37,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { for (const auto value : key.colorReferences) mix(value); mix(key.depthReference); + mix(key.viewMask); return hash; } }; diff --git a/MobileGL/MG_IntegrationTest/CMakeLists.txt b/MobileGL/MG_IntegrationTest/CMakeLists.txt index 561e7953..984daa7f 100644 --- a/MobileGL/MG_IntegrationTest/CMakeLists.txt +++ b/MobileGL/MG_IntegrationTest/CMakeLists.txt @@ -96,6 +96,7 @@ add_executable(MobileGLIntegrationTest Scenarios/PrimitiveRestartScenario.cpp Scenarios/FragCoordOriginScenario.cpp Scenarios/ClipControlScenario.cpp + Scenarios/MultiviewScenario.cpp Scenarios/ClientVertexArrayScenario.cpp Scenarios/ClearThenReadPixelsScenario.cpp # P15 S0: a clear between draws into the same framebuffer (DirectVulkan records it in the pass). diff --git a/MobileGL/MG_IntegrationTest/Scenarios/MultiviewScenario.cpp b/MobileGL/MG_IntegrationTest/Scenarios/MultiviewScenario.cpp new file mode 100644 index 00000000..5a312326 --- /dev/null +++ b/MobileGL/MG_IntegrationTest/Scenarios/MultiviewScenario.cpp @@ -0,0 +1,215 @@ +// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/MultiviewScenario.cpp +// Copyright (c) 2026 MobileGL-Dev +// Licensed under the GNU Lesser General Public License v3.0: +// https://www.gnu.org/licenses/gpl-3.0.txt +// https://www.gnu.org/licenses/lgpl-3.0.txt +// SPDX-License-Identifier: LGPL-3.0-only +// End of Source File Header +// +// Scenario - GL_OVR_multiview RENDERS EACH VIEW INTO ITS OWN LAYER. +// +// A two-view attachment of layers 1 and 2 of a three-layer array texture, read back one layer at +// a time through glFramebufferTextureLayer: +// +// - a draw reaches both views, with gl_ViewID_OVR telling them apart in the vertex stage (its +// position and a varying) and in the fragment stage; +// - a clear reaches both views, and layer 0, outside the views, is never written; +// - a scissored clear issued after a draw, while that draw's pass is still open, reaches both +// views too. +// +// Skipped where the backend does not advertise GL_OVR_multiview2. + +#include +#include + +#include "../Harness/HeadlessGL.h" +#include "../Harness/ScenarioFixture.h" + +#ifdef GLAPI +#undef GLAPI +#endif +#define GL_GLEXT_PROTOTYPES +#include +#include +#undef GL_GLEXT_PROTOTYPES + +namespace MGITest { + namespace { + constexpr int kSize = 32; + + // View 0 covers the whole target in red; view 1 only its left half, in green. + constexpr const char* kVaryingVS = R"(#version 330 core +#extension GL_OVR_multiview2 : require +layout(num_views = 2) in; +layout(location = 0) in vec2 aPos; +out vec4 vColor; +void main() { + bool second = gl_ViewID_OVR == 1u; + vColor = second ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0); + float x = second ? aPos.x * 0.5 - 0.5 : aPos.x; + gl_Position = vec4(x, aPos.y, 0.0, 1.0); +} +)"; + constexpr const char* kVaryingFS = R"(#version 330 core +in vec4 vColor; +out vec4 o_color; +void main() { o_color = vColor; } +)"; + + constexpr const char* kPlainVS = R"(#version 330 core +#extension GL_OVR_multiview2 : require +layout(num_views = 2) in; +layout(location = 0) in vec2 aPos; +void main() { gl_Position = vec4(aPos, 0.0, 1.0); } +)"; + // The view seen from the fragment stage: red for view 0, green for view 1. + constexpr const char* kViewFS = R"(#version 330 core +#extension GL_OVR_multiview2 : require +out vec4 o_color; +void main() { o_color = gl_ViewID_OVR == 1u ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0); } +)"; + + bool ExtensionAdvertised(const char* name) { + GLint count = 0; + glGetIntegerv(GL_NUM_EXTENSIONS, &count); + for (GLint i = 0; i < count; ++i) { + const auto* extension = reinterpret_cast(glGetStringi(GL_EXTENSIONS, i)); + if (extension && std::strcmp(extension, name) == 0) return true; + } + return false; + } + + // llvmpipe's OpenGL ES driver corrupts its heap when a multiview framebuffer is cleared + // through a scissor: the context's teardown then crashes, with or without MobileGL in the + // process (reproduced on llvmpipe 25.2 with nothing but the clear and eglDestroyContext). + bool DriverBreaksScissoredMultiviewClears() { + const auto* renderer = reinterpret_cast(glGetString(GL_RENDERER)); + return renderer && std::strstr(renderer, "Espryt") && std::strstr(renderer, "llvmpipe"); + } + + void DrawFullScreen(unsigned int program) { + const float quad[] = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f}; + GLuint vao = 0, vbo = 0; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr); + glUseProgram(program); + glDrawArrays(GL_TRIANGLES, 0, 6); + glUseProgram(0); + glBindVertexArray(0); + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &vao); + } + + class MultiviewScenario : public ScenarioTest { + protected: + void SetUp() override { + ScenarioTest::SetUp(); + if (!Ready()) return; + if (!ExtensionAdvertised("GL_OVR_multiview2")) { + GTEST_SKIP() << "GL_OVR_multiview2 is not advertised by this backend/driver"; + } + glGenTextures(1, &m_texture); + glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture); + glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, kSize, kSize, 3); + glBindTexture(GL_TEXTURE_2D_ARRAY, 0); + glGenFramebuffers(1, &m_readFbo); + glGenFramebuffers(1, &m_multiviewFbo); + // Every layer black first, one at a time, so layer 0 has a known value to keep. + for (GLint layer = 0; layer < 3; ++layer) { + glBindFramebuffer(GL_FRAMEBUFFER, m_readFbo); + glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture, 0, layer); + ClearTo(0.0f, 0.0f, 0.0f, 1.0f); + } + glBindFramebuffer(GL_FRAMEBUFFER, m_multiviewFbo); + glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture, 0, 1, 2); + ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), static_cast(GL_FRAMEBUFFER_COMPLETE)); + glViewport(0, 0, kSize, kSize); + } + + void TearDown() override { + if (m_texture == 0) return; + glDisable(GL_SCISSOR_TEST); + BindDefaultFramebuffer(); + glDeleteFramebuffers(1, &m_multiviewFbo); + glDeleteFramebuffers(1, &m_readFbo); + glDeleteTextures(1, &m_texture); + } + + Image ReadLayer(GLint layer) { + glBindFramebuffer(GL_FRAMEBUFFER, m_readFbo); + glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture, 0, layer); + Image image = ReadPixels(kSize, kSize); + glBindFramebuffer(GL_FRAMEBUFFER, m_multiviewFbo); + return image; + } + + ::testing::AssertionResult HalvesAre(GLint layer, const char* left, const char* right, + const std::string& when) { + const Image image = ReadLayer(layer); + const std::string where = when + ", layer " + std::to_string(layer); + auto first = RegionIsMostly(image, 2, kSize / 2 - 3, 2, kSize - 3, left, 0.0, where + " (left half)"); + if (!first) return first; + return RegionIsMostly(image, kSize / 2 + 2, kSize - 3, 2, kSize - 3, right, 0.0, + where + " (right half)"); + } + + GLuint m_texture = 0; + GLuint m_readFbo = 0; + GLuint m_multiviewFbo = 0; + }; + } // namespace + + TEST_F(MultiviewScenario, ADrawReachesEveryViewAndTheVertexStageTellsThemApart) { + if (!Ready() || IsSkipped()) return; + std::string error; + const unsigned int program = CompileProgram(kVaryingVS, kVaryingFS, &error); + ASSERT_NE(program, 0u) << error; + ClearTo(0.0f, 0.0f, 1.0f, 1.0f); + DrawFullScreen(program); + EXPECT_EQ(FirstGLError(), 0u); + EXPECT_TRUE(HalvesAre(0, "black", "black", "outside the views")); + EXPECT_TRUE(HalvesAre(1, "red", "red", "view 0")); + EXPECT_TRUE(HalvesAre(2, "green", "blue", "view 1")); + glDeleteProgram(program); + EXPECT_EQ(FirstGLError(), 0u); + } + + TEST_F(MultiviewScenario, TheFragmentStageSeesItsView) { + if (!Ready() || IsSkipped()) return; + std::string error; + const unsigned int program = CompileProgram(kPlainVS, kViewFS, &error); + ASSERT_NE(program, 0u) << error; + DrawFullScreen(program); + EXPECT_EQ(FirstGLError(), 0u); + EXPECT_TRUE(HalvesAre(1, "red", "red", "view 0")); + EXPECT_TRUE(HalvesAre(2, "green", "green", "view 1")); + glDeleteProgram(program); + EXPECT_EQ(FirstGLError(), 0u); + } + + TEST_F(MultiviewScenario, AScissoredClearAfterADrawReachesEveryView) { + if (!Ready() || IsSkipped()) return; + if (DriverBreaksScissoredMultiviewClears()) { + GTEST_SKIP() << "llvmpipe's ES driver crashes after a scissored clear of a multiview framebuffer"; + } + std::string error; + const unsigned int program = CompileProgram(kPlainVS, kViewFS, &error); + ASSERT_NE(program, 0u) << error; + DrawFullScreen(program); + glEnable(GL_SCISSOR_TEST); + glScissor(kSize / 2, 0, kSize / 2, kSize); + ClearTo(0.0f, 0.0f, 1.0f, 1.0f); + glDisable(GL_SCISSOR_TEST); + EXPECT_EQ(FirstGLError(), 0u); + EXPECT_TRUE(HalvesAre(1, "red", "blue", "view 0")); + EXPECT_TRUE(HalvesAre(2, "green", "blue", "view 1")); + EXPECT_TRUE(HalvesAre(0, "black", "black", "outside the views")); + glDeleteProgram(program); + EXPECT_EQ(FirstGLError(), 0u); + } +} // namespace MGITest diff --git a/MobileGL/MG_Test/BackendLoader/BackendLoaderTest.cpp b/MobileGL/MG_Test/BackendLoader/BackendLoaderTest.cpp index 433e9b24..4e9e8c5b 100644 --- a/MobileGL/MG_Test/BackendLoader/BackendLoaderTest.cpp +++ b/MobileGL/MG_Test/BackendLoader/BackendLoaderTest.cpp @@ -1442,8 +1442,8 @@ TEST(MultiviewCapabilities, NeedTheExtensionTheEntryPointAndTwoViews) { EXPECT_TRUE(both.SupportsMultiview2); } -// Each multiview string follows the DirectGLES driver's own; DirectVulkan has no multiview route -// and never advertises either. +// Each multiview string follows the DirectGLES driver's own; DirectVulkan advertises both where the +// device has the multiview feature, and neither without it. TEST(CoreSurfaceAdvertisement, MultiviewFollowsTheDriver) { const auto contains = [](const MobileGL::Vector& extensions, MobileGL::GLExtension wanted) { return std::find(extensions.begin(), extensions.end(), wanted) != extensions.end(); @@ -1469,10 +1469,13 @@ TEST(CoreSurfaceAdvertisement, MultiviewFollowsTheDriver) { DirectGLES::BuildAdvertisedExtensions(false, false, false, false, false, false, {.Multiview2 = true}); EXPECT_FALSE(contains(secondOnly, MobileGL::E_GL_OVR_multiview2)); + const auto vulkanNone = DirectVulkan::BuildAdvertisedExtensions(false, false, false, false, false); + EXPECT_FALSE(contains(vulkanNone, MobileGL::E_GL_OVR_multiview)); + EXPECT_FALSE(contains(vulkanNone, MobileGL::E_GL_OVR_multiview2)); const auto vulkan = DirectVulkan::BuildAdvertisedExtensions(false, false, false, false, false, {.Multiview = true, .Multiview2 = true}); - EXPECT_FALSE(contains(vulkan, MobileGL::E_GL_OVR_multiview)); - EXPECT_FALSE(contains(vulkan, MobileGL::E_GL_OVR_multiview2)); + EXPECT_TRUE(contains(vulkan, MobileGL::E_GL_OVR_multiview)); + EXPECT_TRUE(contains(vulkan, MobileGL::E_GL_OVR_multiview2)); } // Minecraft 26.3 checks ARB_draw_indirect before it considers the already-advertised