Skip to content

Commit 4d8caa7

Browse files
Arthurfernadesparoj
authored andcommitted
D3D11: add _createShared2DTex() for WPF interop
1 parent 7ade80b commit 4d8caa7

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

OgreMain/include/OgreTexture.h

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ namespace Ogre {
146146
*/
147147
TextureType getTextureType(void) const { return mTextureType; }
148148

149+
/** D3D11 only: set a shared surface to use for this texture before loading
150+
*
151+
* Useful for WPF interop
152+
*/
153+
virtual void _setSurface(void* surface) {}
154+
149155
/** Gets the number of mipmaps to be used for this texture.
150156
*/
151157
uint32 getNumMipmaps(void) const {return mNumMipmaps;}

RenderSystems/Direct3D11/include/OgreD3D11Texture.h

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ namespace Ogre {
9494
void _create1DTex();
9595
/// internal method, create a blank normal 2D texture
9696
void _create2DTex();
97+
/// internal method to create a normal 2D texture with an already existing surface
98+
void _createShared2DTex();
9799
/// internal method, create a blank cube texture
98100
void _create3DTex();
99101

@@ -108,6 +110,8 @@ namespace Ogre {
108110
/// mipmap level. This method must be called after the D3D texture object was created
109111
void _createSurfaceList(void);
110112

113+
void _setSurface(void* surface) override;
114+
111115
void notifyDeviceLost(D3D11Device* device);
112116
void notifyDeviceRestored(D3D11Device* device);
113117

@@ -128,6 +132,8 @@ namespace Ogre {
128132

129133
D3D11_SHADER_RESOURCE_VIEW_DESC mSRVDesc;
130134
bool mAutoMipMapGeneration;
135+
136+
void* mSurface;
131137
};
132138

133139
/// RenderTexture implementation for D3D11

RenderSystems/Direct3D11/src/OgreD3D11Texture.cpp

+91-1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ namespace Ogre
233233
//---------------------------------------------------------------------
234234
void D3D11Texture::_create2DTex()
235235
{
236+
if (mSurface)
237+
{
238+
_createShared2DTex();
239+
return;
240+
}
236241
// we must have those defined here
237242
assert(mSrcWidth > 0 || mSrcHeight > 0);
238243

@@ -303,7 +308,90 @@ namespace Ogre
303308

304309
_create2DResourceView();
305310
}
306-
//----------------------------------------------------------------------------
311+
//----------------------------------------------------------------------------
312+
void D3D11Texture::_createShared2DTex()
313+
{
314+
HRESULT hr = S_OK;
315+
316+
IUnknown* pUnk = (IUnknown*)mSurface;
317+
318+
IDXGIResource* pDXGIResource;
319+
hr = pUnk->QueryInterface(__uuidof(IDXGIResource), (void**)&pDXGIResource);
320+
if (FAILED(hr))
321+
{
322+
this->unloadImpl();
323+
OGRE_EXCEPT_EX(Exception::ERR_RENDERINGAPI_ERROR, hr,
324+
"Error creating texture\nError Description: Failed to query IDXGIResource interface from "
325+
"the provided object.",
326+
"D3D11Texture::_create2DTex");
327+
}
328+
329+
HANDLE sharedHandle;
330+
hr = pDXGIResource->GetSharedHandle(&sharedHandle);
331+
if (FAILED(hr))
332+
{
333+
this->unloadImpl();
334+
OGRE_EXCEPT_EX(Exception::ERR_RENDERINGAPI_ERROR, hr,
335+
"Error creating texture\nError Description: Failed to retrieve the shared handle from "
336+
"IDXGIResource. Ensure the resource was "
337+
"created with the D3D11_RESOURCE_MISC_SHARED flag.",
338+
"D3D11Texture::_create2DTex");
339+
}
340+
341+
pDXGIResource->Release();
342+
343+
IUnknown* tempResource11;
344+
hr = mDevice->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&tempResource11));
345+
if (FAILED(hr))
346+
{
347+
this->unloadImpl();
348+
OGRE_EXCEPT_EX(Exception::ERR_RENDERINGAPI_ERROR, hr,
349+
"Error creating texture\nError Description: Failed to open shared resource using the shared "
350+
"handle. Ensure the handle is "
351+
"valid and the device supports shared resources.",
352+
"D3D11Texture::_create2DTex");
353+
}
354+
355+
ID3D11Texture2D* pOutputResource;
356+
hr = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&pOutputResource));
357+
if (FAILED(hr))
358+
{
359+
this->unloadImpl();
360+
OGRE_EXCEPT_EX(Exception::ERR_RENDERINGAPI_ERROR, hr,
361+
"Error creating texture\nError Description: Failed to query ID3D11Texture2D interface from "
362+
"the shared resource. Ensure the "
363+
"resource is of the correct type.",
364+
"D3D11Texture::_create2DTex");
365+
}
366+
tempResource11->Release();
367+
368+
mp2DTex = pOutputResource;
369+
370+
D3D11_TEXTURE2D_DESC desc;
371+
mp2DTex->GetDesc(&desc);
372+
373+
D3D11_RENDER_TARGET_VIEW_DESC rtDesc;
374+
rtDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
375+
rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
376+
rtDesc.Texture2D.MipSlice = 0;
377+
378+
ComPtr<ID3D11RenderTargetView> renderTargetView;
379+
hr = mDevice->CreateRenderTargetView(mp2DTex.Get(), nullptr, renderTargetView.GetAddressOf());
380+
if (FAILED(hr))
381+
{
382+
this->unloadImpl();
383+
OGRE_EXCEPT_EX(Exception::ERR_RENDERINGAPI_ERROR, hr,
384+
"Error creating texture\nError Description: Failed to create ID3D11RenderTargetView. Verify "
385+
"that the texture is valid, "
386+
"properly initialized, and compatible with RenderTargetView creation.",
387+
"D3D11Texture::_create2DTex");
388+
}
389+
390+
_queryInterface<ID3D11Texture2D, ID3D11Resource>(mp2DTex, &mpTex);
391+
392+
_create2DResourceView();
393+
}
394+
//----------------------------------------------------------------------------
307395
void D3D11Texture::_create2DResourceView()
308396
{
309397
// set final tex. attributes from tex. description
@@ -506,6 +594,8 @@ namespace Ogre
506594
}
507595
}
508596
//---------------------------------------------------------------------
597+
void D3D11Texture ::_setSurface(void* surface) { mSurface = surface; }
598+
//---------------------------------------------------------------------
509599
// D3D11RenderTexture
510600
//---------------------------------------------------------------------
511601
void D3D11RenderTexture::rebind( D3D11HardwarePixelBuffer *buffer )

0 commit comments

Comments
 (0)