The open source OpenXR runtime

a/ogl: Add helper to import native images

+115
+83
src/xrt/auxiliary/ogl/ogl_helpers.c
··· 7 7 * @ingroup aux_ogl 8 8 */ 9 9 10 + #include "util/u_handles.h" 10 11 #include "util/u_logging.h" 11 12 12 13 #include "ogl_helpers.h" ··· 14 15 15 16 #include <inttypes.h> 16 17 18 + /*! 19 + * Check for OpenGL errors, context needs to be current. 20 + * 21 + * @ingroup sdl_test 22 + */ 23 + #define CHECK_GL() \ 24 + do { \ 25 + GLint err = glGetError(); \ 26 + if (err != 0) { \ 27 + U_LOG_RAW("%s:%u: error: 0x%04x", __func__, __LINE__, err); \ 28 + } \ 29 + } while (false) 30 + 31 + 32 + /* 33 + * 34 + * 'Exported' functions. 35 + * 36 + */ 17 37 18 38 void 19 39 ogl_texture_target_for_swapchain_info(const struct xrt_swapchain_create_info *info, ··· 72 92 default: U_LOG_W("Cannot convert VK format %" PRIu64 " to GL format!", vk_format); return 0; 73 93 } 74 94 } 95 + 96 + XRT_CHECK_RESULT bool 97 + ogl_import_from_native(struct xrt_image_native *natives, 98 + uint32_t native_count, 99 + const struct xrt_swapchain_create_info *info, 100 + struct ogl_import_results *results) 101 + { 102 + // Setup fields. 103 + results->width = info->width; 104 + results->height = info->height; 105 + results->image_count = native_count; 106 + 107 + GLuint binding_enum = 0; 108 + GLuint tex_target = 0; 109 + ogl_texture_target_for_swapchain_info(info, &tex_target, &binding_enum); 110 + 111 + GLuint gl_format = ogl_vk_format_to_gl(info->format); 112 + 113 + glCreateTextures(tex_target, native_count, results->textures); 114 + CHECK_GL(); 115 + glCreateMemoryObjectsEXT(native_count, results->memories); 116 + CHECK_GL(); 117 + 118 + for (uint32_t i = 0; i < native_count; i++) { 119 + GLint dedicated = natives[i].use_dedicated_allocation ? GL_TRUE : GL_FALSE; 120 + glMemoryObjectParameterivEXT(results->memories[i], GL_DEDICATED_MEMORY_OBJECT_EXT, &dedicated); 121 + CHECK_GL(); 122 + 123 + // The below function consumes the handle, need to reference it. 124 + xrt_graphics_buffer_handle_t handle = u_graphics_buffer_ref(natives[i].handle); 125 + 126 + glImportMemoryFdEXT( // 127 + results->memories[i], // 128 + natives[i].size, // 129 + GL_HANDLE_TYPE_OPAQUE_FD_EXT, // 130 + (GLint)handle); // 131 + CHECK_GL(); 132 + 133 + if (info->array_size == 1) { 134 + glTextureStorageMem2DEXT( // 135 + results->textures[i], // 136 + info->mip_count, // 137 + gl_format, // 138 + info->width, // 139 + info->height, // 140 + results->memories[i], // 141 + 0); // 142 + } else { 143 + glTextureStorageMem3DEXT( // 144 + results->textures[i], // 145 + info->mip_count, // 146 + gl_format, // 147 + info->width, // 148 + info->height, // 149 + info->array_size, // 150 + results->memories[i], // 151 + 0); // 152 + } 153 + CHECK_GL(); 154 + } 155 + 156 + return true; 157 + }
+32
src/xrt/auxiliary/ogl/ogl_helpers.h
··· 16 16 extern "C" { 17 17 #endif 18 18 19 + 20 + /*! 21 + * Results from a import, nicer then having to pass in multiple arrays. 22 + * 23 + * @ingroup aux_ogl 24 + */ 25 + struct ogl_import_results 26 + { 27 + //! Imported textures. 28 + uint32_t textures[XRT_MAX_SWAPCHAIN_IMAGES]; 29 + 30 + //! Memory objects for imported textures. 31 + uint32_t memories[XRT_MAX_SWAPCHAIN_IMAGES]; 32 + 33 + //! The count of textures and memories. 34 + uint32_t image_count; 35 + 36 + //! Dimensions. 37 + uint32_t width, height; 38 + }; 39 + 19 40 /*! 20 41 * Determine the texture target and the texture binding parameter to 21 42 * save/restore for creation/use of an OpenGL texture from the given info. ··· 35 56 */ 36 57 XRT_CHECK_RESULT uint32_t 37 58 ogl_vk_format_to_gl(int64_t vk_format); 59 + 60 + /*! 61 + * Import native images, a context needs to be current when called. 62 + * 63 + * @ingroup aux_ogl 64 + */ 65 + XRT_CHECK_RESULT bool 66 + ogl_import_from_native(struct xrt_image_native *natives, 67 + uint32_t native_count, 68 + const struct xrt_swapchain_create_info *info, 69 + struct ogl_import_results *results); 38 70 39 71 40 72 #ifdef __cplusplus