Maybe a little bit of background is necessary to explain the error more clearly.
I created a whole new method to use in Ogre. Basically, the D3D11Image component provides a DX11 texture as a generic pointer for rendering. So, I created a method that receives this pointer and uses it to create an Ogre texture. Perhaps the code will explain it better:
Code: Select all
void D3D11Texture::_create2DTexSurface()
{
if (!mSurface)
throw std::runtime_error("Invalid resource provided to _create2DTex.");
HRESULT hr = S_OK;
// Convert void* to IUnknown* and than to ID3D11Texture2D*
IUnknown* pUnk = (IUnknown*)mSurface;
IDXGIResource* pDXGIResource;
hr = pUnk->QueryInterface(__uuidof(IDXGIResource), (void**)&pDXGIResource);
if (FAILED(hr))
{
throw std::runtime_error("Failed to create RenderTargetView for D3D11 texture.");
}
HANDLE sharedHandle;
hr = pDXGIResource->GetSharedHandle(&sharedHandle);
if (FAILED(hr))
{
throw std::runtime_error("Failed to create RenderTargetView for D3D11 texture.");
}
pDXGIResource->Release();
IUnknown* tempResource11;
hr = mDevice->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&tempResource11));
if (FAILED(hr))
{
throw std::runtime_error("Failed to create RenderTargetView for D3D11 texture.");
}
ID3D11Texture2D* pOutputResource;
hr = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&pOutputResource));
if (FAILED(hr))
{
throw std::runtime_error("Failed to create RenderTargetView for D3D11 texture.");
}
tempResource11->Release();
mp2DTex = pOutputResource;
D3D11_TEXTURE2D_DESC texDesc;
mp2DTex->GetDesc(&texDesc);
ComPtr<ID3D11RenderTargetView> renderTargetView;
hr = mDevice->CreateRenderTargetView(mp2DTex.Get(), nullptr, renderTargetView.GetAddressOf());
if (FAILED(hr))
throw std::runtime_error("Failed to create RenderTargetView for D3D11 texture.");
// set the base texture we'll use in the render system
_queryInterface<ID3D11Texture2D, ID3D11Resource>(mp2DTex, &mpTex);
_create2DResourceView();
}
I'm using the enum TEX_TYPE_2D_SHARED with this method.
Code: Select all
case TEX_TYPE_2D_SHARED:
this->_create2DTexSurface();
I believe I made the necessary changes to use it correctly. In the TextureManager, I created a new method to manually create a texture using a surface pointer. It's similar to the existing method, but the difference is the new attribute: surface.
Code: Select all
TexturePtr createManualWithSurface(const String& name, const String& group, TextureType texType, uint width, uint height,
int numMipmaps, PixelFormat format, int usage = TU_DEFAULT, void* surface = nullptr,
ManualResourceLoader* loader = 0, bool hwGammaCorrection = false, uint fsaa = 0,
const String& fsaaHint = BLANKSTRING)
{
return createManualWithSurface(name, group, texType, width, height, 1, numMipmaps, format, usage, surface, loader,
hwGammaCorrection, fsaa, fsaaHint);
}
In my C# code I'm using like this:
Code: Select all
texturePtr = TextureManager.getSingleton().createManualWithSurface(
"Ogre Render",
ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME,
TextureType.TEX_TYPE_2D_SHARED,
(uint)ViewportSize.Width,
(uint)ViewportSize.Height,
32,
0,
PixelFormat.PF_B8G8R8A8,
0x20,
surface);
renderTarget = texturePtr.getBuffer().getRenderTarget();
The thing is, I no longer use SetBackBuffer. Instead, I receive a texture from the component and need to use it in Ogre.
Code: Select all
private void DoRender(IntPtr surface, bool isNewSurface)
{
if (ogre.DX==OgreImage.Engine.DX11 )
{
if(isNewSurface)
{
ogre.SetSurface(surface);
}
ogre.RenderOneFrame();
}
}
This is how the D3D11Image sample uses the surface attribute:
Code: Select all
HRESULT CCube::InitRenderTarget(void* pResource)
{
HRESULT hr = S_OK;
IUnknown* pUnk = (IUnknown*)pResource;
IDXGIResource* pDXGIResource;
hr = pUnk->QueryInterface(__uuidof(IDXGIResource), (void**)&pDXGIResource);
if (FAILED(hr))
{
return hr;
}
HANDLE sharedHandle;
hr = pDXGIResource->GetSharedHandle(&sharedHandle);
if (FAILED(hr))
{
return hr;
}
pDXGIResource->Release();
IUnknown* tempResource11;
hr = m_pd3dDevice->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&tempResource11));
if (FAILED(hr))
{
return hr;
}
ID3D11Texture2D* pOutputResource;
hr = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&pOutputResource));
if (FAILED(hr))
{
return hr;
}
tempResource11->Release();
D3D11_RENDER_TARGET_VIEW_DESC rtDesc;
rtDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtDesc.Texture2D.MipSlice = 0;
hr = m_pd3dDevice->CreateRenderTargetView(pOutputResource, &rtDesc, &m_pRenderTargetView);
if (FAILED(hr))
{
return hr;
}
D3D11_TEXTURE2D_DESC outputResourceDesc;
pOutputResource->GetDesc(&outputResourceDesc);
if (outputResourceDesc.Width != m_Width || outputResourceDesc.Height != m_Height)
{
m_Width = outputResourceDesc.Width;
m_Height = outputResourceDesc.Height;
SetUpViewport();
}
m_pImmediateContext->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);
if (NULL != pOutputResource)
{
pOutputResource->Release();
}
return hr;
}
This is the texture description provided by the component.
**************TEXTURE DESCRIPTION**************
Width: 884
Height: 501
MipLevel: 1
ArraySize: 1
Format: 87
Usage: 0
BindFlags: 40
CPUAccessFlags: 0
MiscFlags: 2
SampleDesc Count: 1
SampleDesc Quality: 0