This commit is contained in:
xsl
2025-09-04 10:54:47 +08:00
commit 6bc8f61b18
1808 changed files with 208268 additions and 0 deletions
@@ -0,0 +1,64 @@
# Copyright (c) 2020-2024, Bradley Austin Davis
# Copyright (c) 2020-2024, Arm Limited
#
# SPDX-License-Identifier: Apache-2.0
#
# 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.
#
# The OpenGL interoperability only works on Linux and Windows, and only if glfw is present
# (implying NOT DIRECT_TO_DISPLAY)
# In theory it could work on Android as well, but would need a compeltely different set of code to
# create an offscreen OpenGL context. Further, only a tiny fraction of devices (none of which I have
# access to) appear to support the required semaphore extension:
# https://opengles.gpuinfo.org/listreports.php?extension=GL_EXT_semaphore
#
get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH)
get_filename_component(CATEGORY_NAME ${PARENT_DIR} NAME)
# Needed for the OpenGL interoperability example
set(GLAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/glad")
set(GLAD_SOURCES
${GLAD_DIR}/src/glad.c
${GLAD_DIR}/include/glad/glad.h
${GLAD_DIR}/include/KHR/khrplatform.h
)
add_library(glad STATIC ${GLAD_SOURCES})
target_sources(glad PRIVATE ${GLAD_SOURCES})
target_include_directories(glad PUBLIC ${GLAD_DIR}/include)
set_target_properties(glad PROPERTIES FOLDER "ThirdParty" POSITION_INDEPENDENT_CODE ON)
# Only Compile on platforms that support glfw
if (NOT (ANDROID OR DIRECT_TO_DISPLAY OR IOS))
target_link_libraries( glad PUBLIC glfw )
add_sample_with_tags(
ID ${FOLDER_NAME}
CATEGORY ${CATEGORY_NAME}
AUTHOR "Bradley Austin Davis"
NAME "OpenGL Interoperability"
DESCRIPTION "Example showing sharing resources between OpenGL and Vulkan"
TAGS
"opengl"
LIBS
glad
FILES
offscreen_context.h
offscreen_context.cpp
SHADER_FILES_GLSL
"texture_loading/glsl/texture.vert"
"texture_loading/glsl/texture.frag"
SHADER_FILES_HLSL
"texture_loading/hlsl/texture.vert.hlsl"
"texture_loading/hlsl/texture.frag.hlsl")
endif()
@@ -0,0 +1,30 @@
////
- Copyright (c) 2020-2023, The Khronos Group
-
- SPDX-License-Identifier: Apache-2.0
-
- 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.
-
////
= OpenGL interoperability
ifdef::site-gen-antora[]
TIP: The source for this sample can be found in the https://github.com/KhronosGroup/Vulkan-Samples/tree/main/samples/extensions/open_gl_interop[Khronos Vulkan samples github repository].
endif::[]
*Extensions*: https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_KHR_external_memory.html[`VK_KHR_external_memory`], https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_KHR_external_semaphore.html[`VK_KHR_external_semaphore`]
Render a procedural image using OpenGL and incorporate that rendered content into a Vulkan scene.
Demonstrates using the same backing memory for a texture in both OpenGL and Vulkan and how to synchronize the APIs using shared semaphores and barriers.
@@ -0,0 +1,174 @@
/* Copyright (c) 2020-2024, Arm Limited
* Copyright (c) 2020-2024, Bradley Austin Davis
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#include "offscreen_context.h"
#include <common/error.h>
#include <string>
#include <vector>
#include "core/util/logging.hpp"
void APIENTRY debug_message_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user_param)
{
switch (severity)
{
case GL_DEBUG_SEVERITY_HIGH:
LOGE("OpenGL: {}", message);
break;
case GL_DEBUG_SEVERITY_MEDIUM:
LOGW("OpenGL: {}", message);
break;
case GL_DEBUG_SEVERITY_LOW:
LOGI("OpenGL: {}", message);
break;
default:
case GL_DEBUG_SEVERITY_NOTIFICATION:
LOGD("OpenGL: {}", message);
}
}
OffscreenContext::OffscreenContext()
{
init_context();
glDebugMessageCallback(debug_message_callback, NULL);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
}
OffscreenContext::~OffscreenContext()
{
destroy_context();
}
GLuint OffscreenContext::load_shader(const char *shader_source, GLenum shader_type)
{
std::string source = get_shader_header() + "\n" + std::string(shader_source);
const char *source_ptr = source.c_str();
const GLint size = static_cast<GLint>(source.size());
GLuint shader = glCreateShader(shader_type);
glShaderSource(shader, 1, &source_ptr, &size);
glCompileShader(shader);
GLint is_compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &is_compiled);
if (is_compiled == GL_FALSE)
{
GLint max_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length);
// The maxLength includes the NULL character
std::vector<GLchar> error_log(max_length);
glGetShaderInfoLog(shader, max_length, &max_length, &error_log[0]);
std::string str_error;
str_error.insert(str_error.end(), error_log.begin(), error_log.end());
// Provide the infolog in whatever manor you deem best.
// Exit with failure.
glDeleteShader(shader); // Don't leak the shader.
LOGE("OpenGL: Shader compilation failed", str_error.c_str());
}
return shader;
}
GLuint OffscreenContext::build_program(const char *vertex_shader_source, const char *fragment_shader_source)
{
GLuint program = glCreateProgram();
GLuint vs = load_shader(vertex_shader_source, GL_VERTEX_SHADER);
GLuint fs = load_shader(fragment_shader_source, GL_FRAGMENT_SHADER);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
#ifdef VK_USE_PLATFORM_ANDROID_KHR
void OffscreenContext::init_context()
{
EGLint egl_maj_vers{0},
egl_min_vers{0};
data.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(data.display, &egl_maj_vers, &egl_min_vers);
constexpr EGLint conf_attr[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
EGL_NONE};
EGLint num_configs;
eglChooseConfig(data.display, conf_attr, &data.config, 1, &num_configs);
// Create a EGL context
constexpr EGLint ctx_attr[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
data.context = eglCreateContext(data.display, data.config, EGL_NO_CONTEXT, ctx_attr);
if (data.context == EGL_NO_CONTEXT)
{
throw std::runtime_error{"Failed to create EGL context"};
}
// Create an offscreen pbuffer surface, and then make it current
constexpr EGLint surface_attr[] = {EGL_WIDTH, 10, EGL_HEIGHT, 10, EGL_NONE};
data.surface = eglCreatePbufferSurface(data.display, data.config, surface_attr);
eglMakeCurrent(data.display, data.surface, data.surface, data.context);
gladLoadGLES2Loader((GLADloadproc) &eglGetProcAddress);
LOGD("EGL init with version {}.{}", egl_maj_vers, egl_min_vers);
}
void OffscreenContext::destroy_context()
{
eglDestroySurface(data.display, data.surface);
eglDestroyContext(data.display, data.context);
}
std::string OffscreenContext::get_shader_header()
{
return "#version 320 es";
}
#else
void OffscreenContext::init_context()
{
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
data.window = glfwCreateWindow(SHARED_TEXTURE_DIMENSION, SHARED_TEXTURE_DIMENSION, "OpenGL Window", nullptr, nullptr);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwMakeContextCurrent(data.window);
gladLoadGL();
if (glGenSemaphoresEXT == nullptr)
{
LOGE("Required openGL extension glGenSemaphoresEXT not available, cannot run");
throw vkb::VulkanException(VK_ERROR_EXTENSION_NOT_PRESENT, "Extensions not present");
}
}
void OffscreenContext::destroy_context()
{
glfwDestroyWindow(data.window);
}
std::string OffscreenContext::get_shader_header()
{
return "#version 450 core";
}
#endif
@@ -0,0 +1,90 @@
/* Copyright (c) 2020, Arm Limited
* Copyright (c) 2020, Bradley Austin Davis
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#pragma once
#include <memory>
#include <glad/glad.h>
#include "common/vk_common.h"
constexpr uint32_t SHARED_TEXTURE_DIMENSION = 512;
#ifdef WIN32
constexpr const char *HOST_MEMORY_EXTENSION_NAME = VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME;
constexpr const char *HOST_SEMAPHORE_EXTENSION_NAME = VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME;
# define glImportSemaphore glImportSemaphoreWin32HandleEXT
# define glImportMemory glImportMemoryWin32HandleEXT
# define GL_HANDLE_TYPE GL_HANDLE_TYPE_OPAQUE_WIN32_EXT
# define VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT
# define VK_EXTERNAL_MEMORY_HANDLE_TYPE VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT
#else
constexpr const char *HOST_MEMORY_EXTENSION_NAME = VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME;
constexpr const char *HOST_SEMAPHORE_EXTENSION_NAME = VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME;
# define glImportSemaphore glImportSemaphoreFdEXT
# define glImportMemory glImportMemoryFdEXT
# define GL_HANDLE_TYPE GL_HANDLE_TYPE_OPAQUE_FD_EXT
# define VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
# define VK_EXTERNAL_MEMORY_HANDLE_TYPE VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
#endif
#ifdef VK_USE_PLATFORM_ANDROID_KHR
// Android
# include <EGL/egl.h>
# include <EGL/eglext.h>
struct ContextData
{
EGLConfig config{nullptr};
EGLSurface surface{EGL_NO_SURFACE};
EGLContext context{EGL_NO_CONTEXT};
EGLDisplay display{EGL_NO_DISPLAY};
};
#else
// Desktop
# include <GLFW/glfw3.h>
# include <GLFW/glfw3native.h>
struct ContextData
{
GLFWwindow *window;
};
#endif
class OffscreenContext
{
public:
OffscreenContext();
~OffscreenContext();
// Shared
GLuint build_program(const char *vertex_shader_source, const char *fragment_shader_source);
private:
// Platform specific
void init_context();
void destroy_context();
std::string get_shader_header();
ContextData data;
GLuint load_shader(const char *shader_source, GLenum shader_type);
};
@@ -0,0 +1,782 @@
/* Copyright (c) 2020-2025, Bradley Austin Davis
* Copyright (c) 2020-2025, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#include "open_gl_interop.h"
#include "common/vk_common.h"
#include "filesystem/legacy.h"
#include "gltf_loader.h"
#include "gui.h"
#include "rendering/subpasses/forward_subpass.h"
#include "offscreen_context.h"
constexpr const char *OPENGL_VERTEX_SHADER =
R"SHADER(
const vec4 VERTICES[] = vec4[](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4( 1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4( 1.0, 1.0, 0.0, 1.0)
);
void main() { gl_Position = VERTICES[gl_VertexID]; }
)SHADER";
// Derived from Shadertoy Vornoi noise shader by Inigo Quilez
// https://www.shadertoy.com/view/Xd23Dh
constexpr const char *OPENGL_FRAGMENT_SHADER =
R"SHADER(
const vec4 iMouse = vec4(0.0);
layout(location = 0) out vec4 outColor;
layout(location = 0) uniform vec3 iResolution;
layout(location = 1) uniform float iTime;
vec3 hash3( vec2 p )
{
vec3 q = vec3( dot(p,vec2(127.1,311.7)),
dot(p,vec2(269.5,183.3)),
dot(p,vec2(419.2,371.9)) );
return fract(sin(q)*43758.5453);
}
float iqnoise( in vec2 x, float u, float v )
{
vec2 p = floor(x);
vec2 f = fract(x);
float k = 1.0+63.0*pow(1.0-v,4.0);
float va = 0.0;
float wt = 0.0;
for( int j=-2; j<=2; j++ )
for( int i=-2; i<=2; i++ )
{
vec2 g = vec2( float(i),float(j) );
vec3 o = hash3( p + g )*vec3(u,u,1.0);
vec2 r = g - f + o.xy;
float d = dot(r,r);
float ww = pow( 1.0-smoothstep(0.0,1.414,sqrt(d)), k );
va += o.z*ww;
wt += ww;
}
return va/wt;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xx;
vec2 p = 0.5 - 0.5*sin( iTime*vec2(1.01,1.71) );
if( iMouse.w>0.001 ) p = vec2(0.0,1.0) + vec2(1.0,-1.0)*iMouse.xy/iResolution.xy;
p = p*p*(3.0-2.0*p);
p = p*p*(3.0-2.0*p);
p = p*p*(3.0-2.0*p);
float f = iqnoise( 24.0*uv, p.x, p.y );
fragColor = vec4( f, f, f, 1.0 );
}
void main() { mainImage(outColor, gl_FragCoord.xy); }
)SHADER";
struct GLData
{
// Shader
GLuint program{0};
// Semaphores
GLuint gl_ready{0}, gl_complete{0};
// Memory Object
GLuint mem{0};
// Texture
GLuint color{0};
// Quad
GLuint fbo{0};
GLuint vao{0};
};
OpenGLInterop::OpenGLInterop()
{
zoom = -2.5f;
title = "Interoperability with OpenGL";
add_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
add_instance_extension(VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME);
add_instance_extension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME);
add_device_extension(VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME);
add_device_extension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME);
add_device_extension(HOST_SEMAPHORE_EXTENSION_NAME);
add_device_extension(HOST_MEMORY_EXTENSION_NAME);
}
void OpenGLInterop::prepare_shared_resources()
{
auto deviceHandle = get_device().get_handle();
auto physicalDeviceHandle = get_device().get_gpu().get_handle();
{
VkExternalSemaphoreHandleTypeFlagBits flags[] = {
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT,
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT,
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT};
VkPhysicalDeviceExternalSemaphoreInfo zzzz{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, nullptr};
VkExternalSemaphoreProperties aaaa{VK_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_PROPERTIES,
nullptr};
bool found = false;
VkExternalSemaphoreHandleTypeFlagBits compatable_semaphore_type;
for (size_t i = 0; i < 5; i++)
{
zzzz.handleType = flags[i];
vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(physicalDeviceHandle, &zzzz, &aaaa);
if (aaaa.compatibleHandleTypes & flags[i] && aaaa.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT)
{
compatable_semaphore_type = flags[i];
found = true;
break;
}
}
if (!found)
{
throw;
}
VkExportSemaphoreCreateInfo exportSemaphoreCreateInfo{
VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, nullptr,
static_cast<VkExternalSemaphoreHandleTypeFlags>(compatable_semaphore_type)};
VkSemaphoreCreateInfo semaphoreCreateInfo{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
&exportSemaphoreCreateInfo};
VK_CHECK(vkCreateSemaphore(deviceHandle, &semaphoreCreateInfo, nullptr,
&sharedSemaphores.gl_complete));
VK_CHECK(vkCreateSemaphore(deviceHandle, &semaphoreCreateInfo, nullptr,
&sharedSemaphores.gl_ready));
#if WIN32
VkSemaphoreGetWin32HandleInfoKHR semaphoreGetHandleInfo{
VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR, nullptr,
VK_NULL_HANDLE, compatable_semaphore_type};
semaphoreGetHandleInfo.semaphore = sharedSemaphores.gl_ready;
VK_CHECK(vkGetSemaphoreWin32HandleKHR(deviceHandle, &semaphoreGetHandleInfo, &shareHandles.gl_ready));
semaphoreGetHandleInfo.semaphore = sharedSemaphores.gl_complete;
VK_CHECK(vkGetSemaphoreWin32HandleKHR(deviceHandle, &semaphoreGetHandleInfo, &shareHandles.gl_complete));
#else
VkSemaphoreGetFdInfoKHR semaphoreGetFdInfo{
VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, nullptr,
VK_NULL_HANDLE, compatable_semaphore_type};
semaphoreGetFdInfo.semaphore = sharedSemaphores.gl_ready;
VK_CHECK(vkGetSemaphoreFdKHR(deviceHandle, &semaphoreGetFdInfo, &shareHandles.gl_ready));
semaphoreGetFdInfo.semaphore = sharedSemaphores.gl_complete;
VK_CHECK(vkGetSemaphoreFdKHR(deviceHandle, &semaphoreGetFdInfo, &shareHandles.gl_complete));
#endif
}
{
VkExternalMemoryImageCreateInfo external_memory_image_create_info{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO};
#if WIN32
external_memory_image_create_info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR;
#else
external_memory_image_create_info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
#endif
VkImageCreateInfo imageCreateInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
imageCreateInfo.pNext = &external_memory_image_create_info;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.extent.width = SHARED_TEXTURE_DIMENSION;
imageCreateInfo.extent.height = SHARED_TEXTURE_DIMENSION;
imageCreateInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
VK_CHECK(vkCreateImage(deviceHandle, &imageCreateInfo, nullptr, &sharedTexture.image));
VkMemoryDedicatedAllocateInfo dedicated_allocate_info;
dedicated_allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
dedicated_allocate_info.pNext = nullptr;
dedicated_allocate_info.buffer = VK_NULL_HANDLE;
dedicated_allocate_info.image = sharedTexture.image;
VkMemoryRequirements memReqs{};
vkGetImageMemoryRequirements(get_device().get_handle(), sharedTexture.image, &memReqs);
// In order to export an external handle later, we need to tell it explicitly during memory allocation
VkExportMemoryAllocateInfo export_memory_allocate_Info;
export_memory_allocate_Info.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
export_memory_allocate_Info.pNext = &dedicated_allocate_info;
#if WIN32
export_memory_allocate_Info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR;
#else
export_memory_allocate_Info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
#endif
VkMemoryAllocateInfo memAllocInfo{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &export_memory_allocate_Info};
memAllocInfo.allocationSize = sharedTexture.allocationSize = memReqs.size;
memAllocInfo.memoryTypeIndex = get_device().get_gpu().get_memory_type(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK(vkAllocateMemory(deviceHandle, &memAllocInfo, nullptr, &sharedTexture.memory));
VK_CHECK(vkBindImageMemory(deviceHandle, sharedTexture.image, sharedTexture.memory, 0));
#if WIN32
VkMemoryGetWin32HandleInfoKHR memoryFdInfo{VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR, nullptr,
sharedTexture.memory,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT};
VK_CHECK(vkGetMemoryWin32HandleKHR(deviceHandle, &memoryFdInfo, &shareHandles.memory));
#else
VkMemoryGetFdInfoKHR memoryFdInfo{VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, nullptr,
sharedTexture.memory,
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT};
VK_CHECK(vkGetMemoryFdKHR(deviceHandle, &memoryFdInfo, &shareHandles.memory));
#endif
// Calculate valid filter and mipmap modes
VkFilter filter = VK_FILTER_LINEAR;
VkSamplerMipmapMode mipmap_mode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
vkb::make_filters_valid(get_device().get_gpu().get_handle(), imageCreateInfo.format, &filter, &mipmap_mode);
// Create sampler
VkSamplerCreateInfo samplerCreateInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
samplerCreateInfo.magFilter = filter;
samplerCreateInfo.minFilter = filter;
samplerCreateInfo.mipmapMode = mipmap_mode;
samplerCreateInfo.maxLod = static_cast<float>(1);
// samplerCreateInfo.maxAnisotropy = context.deviceFeatures.samplerAnisotropy ? context.deviceProperties.limits.maxSamplerAnisotropy : 1.0f;
// samplerCreateInfo.anisotropyEnable = context.deviceFeatures.samplerAnisotropy;
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
vkCreateSampler(deviceHandle, &samplerCreateInfo, nullptr, &sharedTexture.sampler);
// Create image view
VkImageViewCreateInfo viewCreateInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
viewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewCreateInfo.image = sharedTexture.image;
viewCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
viewCreateInfo.subresourceRange = VkImageSubresourceRange{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1,
0, 1};
vkCreateImageView(deviceHandle, &viewCreateInfo, nullptr, &sharedTexture.view);
with_command_buffer(
[&](VkCommandBuffer image_command_buffer) { vkb::image_layout_transition(image_command_buffer, sharedTexture.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); },
sharedSemaphores.gl_ready);
}
}
void OpenGLInterop::generate_quad()
{
// Setup vertices for a single uv-mapped quad made from two triangles
std::vector<VertexStructure> vertices =
{
{{1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}},
{{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}},
{{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
};
// Setup indices
std::vector<uint32_t> indices = {0, 1, 2, 2, 3, 0};
index_count = static_cast<uint32_t>(indices.size());
auto vertex_buffer_size = vkb::to_u32(vertices.size() * sizeof(VertexStructure));
auto index_buffer_size = vkb::to_u32(indices.size() * sizeof(uint32_t));
// Create buffers
// For the sake of simplicity we won't stage the vertex data to the gpu memory
// Vertex buffer
vertex_buffer = std::make_unique<vkb::core::BufferC>(get_device(),
vertex_buffer_size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VMA_MEMORY_USAGE_CPU_TO_GPU);
vertex_buffer->update(vertices.data(), vertex_buffer_size);
index_buffer = std::make_unique<vkb::core::BufferC>(get_device(),
index_buffer_size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VMA_MEMORY_USAGE_CPU_TO_GPU);
index_buffer->update(indices.data(), index_buffer_size);
}
void OpenGLInterop::setup_descriptor_pool()
{
// Example uses one ubo and one image sampler
std::vector<VkDescriptorPoolSize> pool_sizes =
{
vkb::initializers::descriptor_pool_size(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
vkb::initializers::descriptor_pool_size(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1)};
VkDescriptorPoolCreateInfo descriptor_pool_create_info =
vkb::initializers::descriptor_pool_create_info(
static_cast<uint32_t>(pool_sizes.size()),
pool_sizes.data(),
2);
VK_CHECK(
vkCreateDescriptorPool(get_device().get_handle(), &descriptor_pool_create_info, nullptr,
&descriptor_pool));
}
void OpenGLInterop::setup_descriptor_set_layout()
{
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings{
// Binding 0 : Vertex shader uniform buffer
vkb::initializers::descriptor_set_layout_binding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_VERTEX_BIT,
0),
// Binding 1 : Fragment shader image sampler
vkb::initializers::descriptor_set_layout_binding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT,
1),
};
VkDescriptorSetLayoutCreateInfo descriptor_layout =
vkb::initializers::descriptor_set_layout_create_info(
set_layout_bindings.data(),
vkb::to_u32(set_layout_bindings.size()));
VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &descriptor_layout, nullptr,
&descriptor_set_layout));
VkPipelineLayoutCreateInfo pipeline_layout_create_info = vkb::initializers::pipeline_layout_create_info(
&descriptor_set_layout, 1);
VK_CHECK(
vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr,
&pipeline_layout));
}
void OpenGLInterop::setup_descriptor_set()
{
VkDescriptorSetAllocateInfo alloc_info =
vkb::initializers::descriptor_set_allocate_info(
descriptor_pool,
&descriptor_set_layout,
1);
VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &descriptor_set));
VkDescriptorBufferInfo buffer_descriptor = create_descriptor(*uniform_buffer_vs);
// Setup a descriptor image info for the current texture to be used as a combined image sampler
VkDescriptorImageInfo image_descriptor;
image_descriptor.imageView = sharedTexture.view; // The image's view (images are never directly accessed by the shader, but rather through views defining subresources)
image_descriptor.sampler = sharedTexture.sampler; // The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.)
image_descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // The current layout of the image (Note: Should always fit the actual use, e.g. shader read)
std::vector<VkWriteDescriptorSet> write_descriptor_sets =
{
// Binding 0 : Vertex shader uniform buffer
vkb::initializers::write_descriptor_set(
descriptor_set,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&buffer_descriptor),
// Binding 1 : Fragment shader texture sampler
// Fragment shader: layout (binding = 1) uniform sampler2D samplerColor;
vkb::initializers::write_descriptor_set(
descriptor_set,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split)
1, // Shader binding point 1
&image_descriptor) // Pointer to the descriptor image for our texture
};
vkUpdateDescriptorSets(get_device().get_handle(), vkb::to_u32(write_descriptor_sets.size()),
write_descriptor_sets.data(), 0, NULL);
}
void OpenGLInterop::prepare_pipelines()
{
VkPipelineInputAssemblyStateCreateInfo input_assembly_state =
vkb::initializers::pipeline_input_assembly_state_create_info(
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
0,
VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterization_state =
vkb::initializers::pipeline_rasterization_state_create_info(
VK_POLYGON_MODE_FILL,
VK_CULL_MODE_NONE,
VK_FRONT_FACE_COUNTER_CLOCKWISE,
0);
VkPipelineColorBlendAttachmentState blend_attachment_state =
vkb::initializers::pipeline_color_blend_attachment_state(
0xf,
VK_FALSE);
VkPipelineColorBlendStateCreateInfo color_blend_state =
vkb::initializers::pipeline_color_blend_state_create_info(
1,
&blend_attachment_state);
// Note: Using reversed depth-buffer for increased precision, so Greater depth values are kept
VkPipelineDepthStencilStateCreateInfo depth_stencil_state =
vkb::initializers::pipeline_depth_stencil_state_create_info(
VK_TRUE,
VK_TRUE,
VK_COMPARE_OP_GREATER);
VkPipelineViewportStateCreateInfo viewport_state =
vkb::initializers::pipeline_viewport_state_create_info(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisample_state =
vkb::initializers::pipeline_multisample_state_create_info(
VK_SAMPLE_COUNT_1_BIT,
0);
std::vector<VkDynamicState> dynamic_state_enables = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo dynamic_state =
vkb::initializers::pipeline_dynamic_state_create_info(
dynamic_state_enables.data(),
vkb::to_u32(dynamic_state_enables.size()),
0);
// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shader_stages;
shader_stages[0] = load_shader("texture_loading", "texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shader_stages[1] = load_shader("texture_loading", "texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
// Vertex bindings and attributes
const std::vector<VkVertexInputBindingDescription> vertex_input_bindings = {
vkb::initializers::vertex_input_binding_description(0, sizeof(VertexStructure),
VK_VERTEX_INPUT_RATE_VERTEX),
};
const std::vector<VkVertexInputAttributeDescription> vertex_input_attributes = {
vkb::initializers::vertex_input_attribute_description(0, 0, VK_FORMAT_R32G32B32_SFLOAT,
offsetof(VertexStructure, pos)),
vkb::initializers::vertex_input_attribute_description(0, 1, VK_FORMAT_R32G32_SFLOAT,
offsetof(VertexStructure, uv)),
vkb::initializers::vertex_input_attribute_description(0, 2, VK_FORMAT_R32G32B32_SFLOAT,
offsetof(VertexStructure,
normal)),
};
VkPipelineVertexInputStateCreateInfo vertex_input_state = vkb::initializers::pipeline_vertex_input_state_create_info();
vertex_input_state.vertexBindingDescriptionCount = vkb::to_u32(vertex_input_bindings.size());
vertex_input_state.pVertexBindingDescriptions = vertex_input_bindings.data();
vertex_input_state.vertexAttributeDescriptionCount = vkb::to_u32(
vertex_input_attributes.size());
vertex_input_state.pVertexAttributeDescriptions = vertex_input_attributes.data();
VkGraphicsPipelineCreateInfo pipeline_create_info =
vkb::initializers::pipeline_create_info(
pipeline_layout,
render_pass,
0);
pipeline_create_info.pVertexInputState = &vertex_input_state;
pipeline_create_info.pInputAssemblyState = &input_assembly_state;
pipeline_create_info.pRasterizationState = &rasterization_state;
pipeline_create_info.pColorBlendState = &color_blend_state;
pipeline_create_info.pMultisampleState = &multisample_state;
pipeline_create_info.pViewportState = &viewport_state;
pipeline_create_info.pDepthStencilState = &depth_stencil_state;
pipeline_create_info.pDynamicState = &dynamic_state;
pipeline_create_info.stageCount = vkb::to_u32(shader_stages.size());
pipeline_create_info.pStages = shader_stages.data();
VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1,
&pipeline_create_info, nullptr, &pipeline));
}
// Prepare and initialize uniform buffer containing shader uniforms
void OpenGLInterop::prepare_uniform_buffers()
{
// Vertex shader uniform buffer block
uniform_buffer_vs = std::make_unique<vkb::core::BufferC>(get_device(),
sizeof(ubo_vs),
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VMA_MEMORY_USAGE_CPU_TO_GPU);
update_uniform_buffers();
}
void OpenGLInterop::update_uniform_buffers()
{
// Vertex shader
ubo_vs.projection = glm::perspective(glm::radians(60.0f), static_cast<float>(width) / static_cast<float>(height),
0.001f, 256.0f);
glm::mat4 view_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom));
ubo_vs.model = view_matrix * glm::translate(glm::mat4(1.0f), camera_pos);
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
ubo_vs.view_pos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f);
uniform_buffer_vs->convert_and_update(ubo_vs);
}
bool OpenGLInterop::prepare(const vkb::ApplicationOptions &options)
{
if (!ApiVulkanSample::prepare(options))
{
return false;
}
// Create off screen context
gl_context = new OffscreenContext{};
gl_data = new GLData{};
prepare_shared_resources();
gl_data->program = gl_context->build_program(OPENGL_VERTEX_SHADER, OPENGL_FRAGMENT_SHADER);
timer.start();
glDisable(GL_DEPTH_TEST);
// Create the texture for the FBO color attachment.
// This only reserves the ID, it doesn't allocate memory
glGenTextures(1, &gl_data->color);
glBindTexture(GL_TEXTURE_2D, gl_data->color);
// Create the GL identifiers
// semaphores
glGenSemaphoresEXT(1, &gl_data->gl_ready);
glGenSemaphoresEXT(1, &gl_data->gl_complete);
// memory
glCreateMemoryObjectsEXT(1, &gl_data->mem);
GLint dedicated = GL_TRUE;
glMemoryObjectParameterivEXT(gl_data->mem, GL_DEDICATED_MEMORY_OBJECT_EXT, &dedicated);
// Platform specific import.
glImportSemaphore(gl_data->gl_ready, GL_HANDLE_TYPE, shareHandles.gl_ready);
glImportSemaphore(gl_data->gl_complete, GL_HANDLE_TYPE, shareHandles.gl_complete);
glImportMemory(gl_data->mem, sharedTexture.allocationSize, GL_HANDLE_TYPE, shareHandles.memory);
// Use the imported memory as backing for the OpenGL texture. The internalFormat, dimensions
// and mip count should match the ones used by Vulkan to create the image and determine it's memory
// allocation.
glTextureStorageMem2DEXT(gl_data->color, 1, GL_RGBA8, SHARED_TEXTURE_DIMENSION,
SHARED_TEXTURE_DIMENSION, gl_data->mem, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// The remaining initialization code is all standard OpenGL
glGenVertexArrays(1, &gl_data->vao);
glBindVertexArray(gl_data->vao);
glGenFramebuffers(1, &gl_data->fbo);
glBindFramebuffer(GL_FRAMEBUFFER, gl_data->fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, gl_data->color, 0);
glUseProgram(gl_data->program);
glProgramUniform3f(gl_data->program, 0, static_cast<float>(SHARED_TEXTURE_DIMENSION),
static_cast<float>(SHARED_TEXTURE_DIMENSION), 0.0f);
glViewport(0, 0, SHARED_TEXTURE_DIMENSION, SHARED_TEXTURE_DIMENSION);
generate_quad();
prepare_uniform_buffers();
setup_descriptor_set_layout();
prepare_pipelines();
setup_descriptor_pool();
setup_descriptor_set();
build_command_buffers();
prepared = true;
return true;
}
void OpenGLInterop::render(float)
{
if (!prepared)
{
return;
}
ApiVulkanSample::prepare_frame();
// RENDER
float time = static_cast<float>(timer.elapsed());
// The GL shader animates the image, so provide the time as input
glProgramUniform1f(gl_data->program, 1, time);
// Wait (on the GPU side) for the Vulkan semaphore to be signaled
GLenum srcLayout = GL_LAYOUT_COLOR_ATTACHMENT_EXT;
glWaitSemaphoreEXT(gl_data->gl_ready, 0, nullptr, 1, &gl_data->color, &srcLayout);
// Draw to the framebuffer
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Once drawing is complete, signal the Vulkan semaphore indicating
// it can continue with it's render
GLenum dstLayout = GL_LAYOUT_SHADER_READ_ONLY_EXT;
glSignalSemaphoreEXT(gl_data->gl_complete, 0, nullptr, 1, &gl_data->color, &dstLayout);
// When using synchronization across multiple GL context, or in this case
// across OpenGL and another API, it's critical that an operation on a
// synchronization object that will be waited on in another context or API
// is flushed to the GL server.
//
// Failure to flush the operation can cause the GL driver to sit and wait for
// sufficient additional commands in the buffer before it flushes automatically
// but depending on how the waits and signals are structured, this may never
// occur.
glFlush();
// RENDER
std::array<VkPipelineStageFlags, 2> waitStages{{VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT}};
std::array<VkSemaphore, 2> waitSemaphores{{semaphores.acquired_image_ready, sharedSemaphores.gl_complete}};
std::array<VkSemaphore, 2> signalSemaphores{{semaphores.render_complete, sharedSemaphores.gl_ready}};
// Command buffer to be submitted to the queue
submit_info.waitSemaphoreCount = vkb::to_u32(waitSemaphores.size());
submit_info.pWaitSemaphores = waitSemaphores.data();
submit_info.pWaitDstStageMask = waitStages.data();
submit_info.signalSemaphoreCount = vkb::to_u32(signalSemaphores.size());
submit_info.pSignalSemaphores = signalSemaphores.data();
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &draw_cmd_buffers[current_buffer];
// Submit to queue
VK_CHECK(vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE));
ApiVulkanSample::submit_frame();
}
void OpenGLInterop::view_changed()
{
update_uniform_buffers();
}
void OpenGLInterop::on_update_ui_overlay(vkb::Drawer &drawer)
{
if (drawer.header("Settings"))
{
}
}
void OpenGLInterop::build_command_buffers()
{
VkCommandBufferBeginInfo command_buffer_begin_info = vkb::initializers::command_buffer_begin_info();
VkClearValue clear_values[2];
clear_values[0].color = default_clear_color;
clear_values[1].depthStencil = {0.0f, 0};
VkRenderPassBeginInfo render_pass_begin_info = vkb::initializers::render_pass_begin_info();
render_pass_begin_info.renderPass = render_pass;
render_pass_begin_info.renderArea.offset.x = 0;
render_pass_begin_info.renderArea.offset.y = 0;
render_pass_begin_info.renderArea.extent.width = width;
render_pass_begin_info.renderArea.extent.height = height;
render_pass_begin_info.clearValueCount = 2;
render_pass_begin_info.pClearValues = clear_values;
for (int32_t i = 0; i < draw_cmd_buffers.size(); ++i)
{
// Set target frame buffer
render_pass_begin_info.framebuffer = framebuffers[i];
VK_CHECK(vkBeginCommandBuffer(draw_cmd_buffers[i], &command_buffer_begin_info));
vkb::image_layout_transition(draw_cmd_buffers[i], sharedTexture.image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
vkCmdBeginRenderPass(draw_cmd_buffers[i], &render_pass_begin_info,
VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkb::initializers::viewport(static_cast<float>(width), static_cast<float>(height), 0.0f,
1.0f);
vkCmdSetViewport(draw_cmd_buffers[i], 0, 1, &viewport);
VkRect2D scissor = vkb::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(draw_cmd_buffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline_layout, 0, 1, &descriptor_set, 0, NULL);
vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
VkDeviceSize offsets[1] = {0};
vkCmdBindVertexBuffers(draw_cmd_buffers[i], 0, 1, vertex_buffer->get(), offsets);
vkCmdBindIndexBuffer(draw_cmd_buffers[i], index_buffer->get_handle(), 0,
VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(draw_cmd_buffers[i], index_count, 1, 0, 0, 0);
draw_ui(draw_cmd_buffers[i]);
vkCmdEndRenderPass(draw_cmd_buffers[i]);
vkb::image_layout_transition(draw_cmd_buffers[i], sharedTexture.image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VK_CHECK(vkEndCommandBuffer(draw_cmd_buffers[i]));
}
}
OpenGLInterop::~OpenGLInterop()
{
if (gl_context != nullptr)
{
glFinish();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
glDeleteFramebuffers(1, &gl_data->fbo);
glDeleteTextures(1, &gl_data->color);
glDeleteSemaphoresEXT(1, &gl_data->gl_ready);
glDeleteSemaphoresEXT(1, &gl_data->gl_complete);
glDeleteVertexArrays(1, &gl_data->vao);
glDeleteProgram(gl_data->program);
glFlush();
glFinish();
// Destroy OpenGl Context
delete gl_context;
delete gl_data;
}
vertex_buffer.reset();
index_buffer.reset();
uniform_buffer_vs.reset();
if (has_device())
{
get_device().wait_idle();
auto deviceHandle = get_device().get_handle();
vkDestroySemaphore(deviceHandle, sharedSemaphores.gl_ready, nullptr);
vkDestroySemaphore(deviceHandle, sharedSemaphores.gl_complete, nullptr);
vkDestroyImage(deviceHandle, sharedTexture.image, nullptr);
vkDestroySampler(deviceHandle, sharedTexture.sampler, nullptr);
vkDestroyImageView(deviceHandle, sharedTexture.view, nullptr);
vkFreeMemory(deviceHandle, sharedTexture.memory, nullptr);
vkDestroyPipeline(deviceHandle, pipeline, nullptr);
vkDestroyPipelineLayout(deviceHandle, pipeline_layout, nullptr);
vkDestroyDescriptorSetLayout(deviceHandle, descriptor_set_layout, nullptr);
}
}
std::unique_ptr<vkb::VulkanSampleC> create_open_gl_interop()
{
return std::make_unique<OpenGLInterop>();
}
@@ -0,0 +1,116 @@
/* Copyright (c) 2020-2024, Bradley Austin Davis
* Copyright (c) 2020-2024, Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#pragma once
#include "api_vulkan_sample.h"
#include "rendering/render_pipeline.h"
#include "scene_graph/components/camera.h"
#include "timer.h"
class OpenGLWindow;
#if defined(WIN32)
using Handle = HANDLE;
#else
using Handle = int;
constexpr Handle INVALID_HANDLE_VALUE = static_cast<Handle>(-1);
#endif
// Vertex layout for this example
struct VertexStructure
{
float pos[3];
float uv[2];
float normal[3];
};
class OffscreenContext;
struct GLData;
class OpenGLInterop : public ApiVulkanSample
{
public:
OpenGLInterop();
~OpenGLInterop();
virtual bool prepare(const vkb::ApplicationOptions &options) override;
void render(float delta_time) override;
void build_command_buffers() override;
void view_changed() override;
void on_update_ui_overlay(vkb::Drawer &drawer) override;
private:
void prepare_shared_resources();
void generate_quad();
void setup_descriptor_pool();
void setup_descriptor_set_layout();
void setup_descriptor_set();
void prepare_pipelines();
void prepare_uniform_buffers();
void update_uniform_buffers();
void draw();
vkb::Timer timer;
OffscreenContext *gl_context{nullptr};
GLData *gl_data{nullptr};
struct ShareHandles
{
Handle memory{INVALID_HANDLE_VALUE};
Handle gl_ready{INVALID_HANDLE_VALUE};
Handle gl_complete{INVALID_HANDLE_VALUE};
} shareHandles;
struct SharedTexture
{
VkImage image{VK_NULL_HANDLE};
VkDeviceMemory memory{VK_NULL_HANDLE};
VkDeviceSize size{0};
VkDeviceSize allocationSize{0};
VkSampler sampler{VK_NULL_HANDLE};
VkImageView view{VK_NULL_HANDLE};
} sharedTexture;
struct Semaphores
{
VkSemaphore gl_ready{VK_NULL_HANDLE};
VkSemaphore gl_complete{VK_NULL_HANDLE};
} sharedSemaphores;
std::unique_ptr<vkb::core::BufferC> vertex_buffer;
std::unique_ptr<vkb::core::BufferC> index_buffer;
uint32_t index_count;
std::unique_ptr<vkb::core::BufferC> uniform_buffer_vs;
struct UniformBufferData
{
glm::mat4 projection;
glm::mat4 model;
glm::vec4 view_pos;
} ubo_vs;
VkPipeline pipeline{VK_NULL_HANDLE};
VkPipelineLayout pipeline_layout{VK_NULL_HANDLE};
VkDescriptorSet descriptor_set{VK_NULL_HANDLE};
VkDescriptorSetLayout descriptor_set_layout{VK_NULL_HANDLE};
};
std::unique_ptr<vkb::VulkanSampleC> create_open_gl_interop();
@@ -0,0 +1,289 @@
#ifndef __khrplatform_h_
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are 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 Materials.
**
** THE MATERIALS ARE 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
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APICALL
*-------------------------------------------------------------------------
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#elif defined(__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIENTRY
*-------------------------------------------------------------------------
* This follows the return type of the function and precedes the function
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIATTRIBUTES
*-------------------------------------------------------------------------
* This follows the closing parenthesis of the function prototype arguments.
*/
#if defined(__ARMCC_2__)
# define KHRONOS_APIATTRIBUTES __softfp
#else
# define KHRONOS_APIATTRIBUTES
#endif
/*-------------------------------------------------------------------------
* basic type definitions
*-----------------------------------------------------------------------*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
/*
* Using <stdint.h>
*/
# include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
# define KHRONOS_SUPPORT_INT64 1
# define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__VMS) || defined(__sgi)
/*
* Using <inttypes.h>
*/
# include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
# define KHRONOS_SUPPORT_INT64 1
# define KHRONOS_SUPPORT_FLOAT 1
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
# define KHRONOS_SUPPORT_INT64 1
# define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__sun__) || defined(__digital__)
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
# if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
# else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
# endif /* __arch64__ */
# define KHRONOS_SUPPORT_INT64 1
# define KHRONOS_SUPPORT_FLOAT 1
#elif 0
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
# define KHRONOS_SUPPORT_INT64 0
# define KHRONOS_SUPPORT_FLOAT 0
#else
/*
* Generic fallback
*/
# include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
# define KHRONOS_SUPPORT_INT64 1
# define KHRONOS_SUPPORT_FLOAT 1
#endif
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef _WIN64
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
* Dummy value used to pad enum types to 32 bits.
*/
#ifndef KHRONOS_MAX_ENUM
# define KHRONOS_MAX_ENUM 0x7FFFFFFF
#endif
/*
* Enumerated boolean type
*
* Values other than zero should be considered to be true. Therefore
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum
{
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff