diff --git a/libobs-d3d11/d3d11-subsystem.cpp b/libobs-d3d11/d3d11-subsystem.cpp index 45a0fe2a1a2f6e2e22fde1e31f194f2dff8b30e1..dce761afff98d8ea1c110d5b93de77b94272a2a8 100644 --- a/libobs-d3d11/d3d11-subsystem.cpp +++ b/libobs-d3d11/d3d11-subsystem.cpp @@ -2670,6 +2670,23 @@ extern "C" EXPORT uint32_t device_texture_get_shared_handle(gs_texture_t *tex) return tex2d->isShared ? tex2d->sharedHandle : GS_INVALID_HANDLE; } +extern "C" EXPORT gs_texture_t *device_texture_wrap_obj(gs_device_t *device, + void *obj) +{ + gs_texture *texture = nullptr; + try { + texture = new gs_texture_2d(device, (ID3D11Texture2D *)obj); + } catch (const HRError &error) { + blog(LOG_ERROR, "gs_texture_wrap_obj (D3D11): %s (%08lX)", + error.str, error.hr); + LogD3D11ErrorDetails(error, device); + } catch (const char *error) { + blog(LOG_ERROR, "gs_texture_wrap_obj (D3D11): %s", error); + } + + return texture; +} + int device_texture_acquire_sync(gs_texture_t *tex, uint64_t key, uint32_t ms) { gs_texture_2d *tex2d = reinterpret_cast(tex); diff --git a/libobs-d3d11/d3d11-subsystem.hpp b/libobs-d3d11/d3d11-subsystem.hpp index a2c260cf5116db528e5f66fc8efb5db40fc448d1..28729a5c90c68a7d048270628f8ed5c2baa8df5c 100644 --- a/libobs-d3d11/d3d11-subsystem.hpp +++ b/libobs-d3d11/d3d11-subsystem.hpp @@ -490,6 +490,7 @@ struct gs_texture_2d : gs_texture { gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12, uint32_t flags); gs_texture_2d(gs_device_t *device, uint32_t handle); + gs_texture_2d(gs_device_t *device, ID3D11Texture2D *obj); }; struct gs_texture_3d : gs_texture { diff --git a/libobs-d3d11/d3d11-texture2d.cpp b/libobs-d3d11/d3d11-texture2d.cpp index c249e208dc339165cefdbfffa620f03894b69d16..975d5b3c1efe4eb004f66808d670f9cf79c9d765 100644 --- a/libobs-d3d11/d3d11-texture2d.cpp +++ b/libobs-d3d11/d3d11-texture2d.cpp @@ -311,3 +311,30 @@ gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle) if (FAILED(hr)) throw HRError("Failed to create shader resource view", hr); } + +gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *obj) + : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D) +{ + texture = obj; + + texture->GetDesc(&td); + + this->type = GS_TEXTURE_2D; + this->format = ConvertDXGITextureFormat(td.Format); + this->levels = 1; + this->device = device; + + this->width = td.Width; + this->height = td.Height; + this->dxgiFormat = td.Format; + + memset(&resourceDesc, 0, sizeof(resourceDesc)); + resourceDesc.Format = td.Format; + resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + resourceDesc.Texture2D.MipLevels = 1; + + HRESULT hr = device->device->CreateShaderResourceView( + texture, &resourceDesc, shaderRes.Assign()); + if (FAILED(hr)) + throw HRError("Failed to create shader resource view", hr); +} diff --git a/libobs/graphics/graphics-imports.c b/libobs/graphics/graphics-imports.c index 0d3c2593926502ad616e7eb7970dda1b60c18d66..8259366b8c3a02efa9e7683b3a62cce8a9739bb9 100644 --- a/libobs/graphics/graphics-imports.c +++ b/libobs/graphics/graphics-imports.c @@ -210,6 +210,7 @@ bool load_graphics_imports(struct gs_exports *exports, void *module, GRAPHICS_IMPORT_OPTIONAL(gs_texture_release_dc); GRAPHICS_IMPORT_OPTIONAL(device_texture_open_shared); GRAPHICS_IMPORT_OPTIONAL(device_texture_get_shared_handle); + GRAPHICS_IMPORT_OPTIONAL(device_texture_wrap_obj); GRAPHICS_IMPORT_OPTIONAL(device_texture_acquire_sync); GRAPHICS_IMPORT_OPTIONAL(device_texture_release_sync); GRAPHICS_IMPORT_OPTIONAL(device_texture_create_nv12); diff --git a/libobs/graphics/graphics-internal.h b/libobs/graphics/graphics-internal.h index 4154688c5c246cf50efa3cfb3faed6619d2a6372..865a3f3ffe1a6cf7abaf6bc63062bc7d5172e215 100644 --- a/libobs/graphics/graphics-internal.h +++ b/libobs/graphics/graphics-internal.h @@ -299,6 +299,8 @@ struct gs_exports { gs_texture_t *(*device_texture_open_shared)(gs_device_t *device, uint32_t handle); uint32_t (*device_texture_get_shared_handle)(gs_texture_t *tex); + gs_texture_t *(*device_texture_wrap_obj)(gs_device_t *device, + void *obj); int (*device_texture_acquire_sync)(gs_texture_t *tex, uint64_t key, uint32_t ms); int (*device_texture_release_sync)(gs_texture_t *tex, uint64_t key); diff --git a/libobs/graphics/graphics.c b/libobs/graphics/graphics.c index e403edb04c81f7e6fece2702cac598e18bea7ba2..5cc55ec71a8c9e62a39a8955b28e684f44858b0c 100644 --- a/libobs/graphics/graphics.c +++ b/libobs/graphics/graphics.c @@ -2885,6 +2885,18 @@ uint32_t gs_texture_get_shared_handle(gs_texture_t *tex) return GS_INVALID_HANDLE; } +gs_texture_t *gs_texture_wrap_obj(void *obj) +{ + graphics_t *graphics = thread_graphics; + if (!gs_valid("gs_texture_wrap_obj")) + return NULL; + + if (graphics->exports.device_texture_wrap_obj) + return graphics->exports.device_texture_wrap_obj( + graphics->device, obj); + return NULL; +} + int gs_texture_acquire_sync(gs_texture_t *tex, uint64_t key, uint32_t ms) { graphics_t *graphics = thread_graphics; diff --git a/libobs/graphics/graphics.h b/libobs/graphics/graphics.h index 341a55d1a4be11782c6b099e2fe52fc7eabb13ae..2746a87b478eb32b064739fdfabac92f58098e09 100644 --- a/libobs/graphics/graphics.h +++ b/libobs/graphics/graphics.h @@ -867,6 +867,8 @@ EXPORT gs_texture_t *gs_texture_open_shared(uint32_t handle); #define GS_INVALID_HANDLE (uint32_t) - 1 EXPORT uint32_t gs_texture_get_shared_handle(gs_texture_t *tex); +EXPORT gs_texture_t *gs_texture_wrap_obj(void *obj); + #define GS_WAIT_INFINITE (uint32_t) - 1 /**