Render target crash

Hello!

I’m having problems creating a render texture that Unreal can use. Here is the code that creates the render texture (ends up getting assigned to mRenderTexture):



    virtual bool AllocateRenderTargetTexture(uint32 index, uint32 sizeX, uint32 sizeY, uint8 format, uint32 numMips, uint32 flags, uint32 targetableTextureFlags, FTexture2DRHIRef& outTargetableTexture, FTexture2DRHIRef& outShaderResourceTexture, uint32 numSamples = 1) override {
        FScopeLock lock(&mOSVRMutex);
        auto d3d11RHI = static_cast<FD3D11DynamicRHI*>(GDynamicRHI);
        auto graphicsDevice = GetGraphicsDevice();
        HRESULT hr;
        D3D11_TEXTURE2D_DESC textureDesc;
        memset(&textureDesc, 0, sizeof(textureDesc));
        textureDesc.Width = sizeX;
        textureDesc.Height = sizeY;
        textureDesc.MipLevels = 1;
        textureDesc.ArraySize = 1;
        textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        textureDesc.SampleDesc.Count = 1;
        textureDesc.SampleDesc.Quality = 0;
        textureDesc.Usage = D3D11_USAGE_DEFAULT;
        // We need it to be both a render target and a shader resource
        textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
        textureDesc.CPUAccessFlags = 0;
        textureDesc.MiscFlags = 0;

        ID3D11Texture2D *D3DTexture = nullptr;
        hr = graphicsDevice->CreateTexture2D(
            &textureDesc, NULL, &D3DTexture);
        check(!FAILED(hr));

        SetRenderTargetTexture(D3DTexture);

        ID3D11ShaderResourceView* shaderResourceView = nullptr;
        bool createdRTVsPerSlice = false;
        int32 rtvArraySize = 1;
        TArray<TRefCountPtr<ID3D11RenderTargetView>> renderTargetViews;
        TRefCountPtr<ID3D11DepthStencilView>* depthStencilViews = nullptr;
        uint32 sizeZ = 0;
        EPixelFormat epFormat = EPixelFormat(format);
        bool cubemap = false;
        bool pooled = false;
        // override flags
        flags = TexCreate_RenderTargetable | TexCreate_ShaderResource;

        auto targetableTexture = new FD3D11Texture2D(
            d3d11RHI, D3DTexture, shaderResourceView, createdRTVsPerSlice,
            rtvArraySize, renderTargetViews, depthStencilViews,
            textureDesc.Width, textureDesc.Height, sizeZ, numMips, numSamples, epFormat,
            cubemap, flags, pooled, FClearValueBinding::Black);

        outTargetableTexture = targetableTexture->GetTexture2D();
        outShaderResourceTexture = targetableTexture->GetTexture2D();
        mRenderTexture = targetableTexture;
        mRenderBuffersNeedToUpdate = true;
        UpdateRenderBuffers();
        return true;
    }


This code runs to completion successfully. However, later, when unreal attempts to set the render target to this texture, it crashes with the following stack trace (I can’t seem to get a run configuration with debug symbols for the engine code, but that’s a separate issue):



OSVRUnreal-Win64-DebugGame.exe!FD3D11DynamicRHI::RHISetRenderTargets
OSVRUnreal-Win64-DebugGame.exe!SetRenderTarget
OSVRUnreal-Win64-DebugGame.exe!FSceneViewport::BeginFrame
OSVRUnreal-Win64-DebugGame.exe!TGraphTask<class FViewport::EnqueueBeginRenderFrame(void)::2::EURCMacro_BeginDrawing>::ExecuteTask
OSVRUnreal-Win64-DebugGame.exe!FTaskThread::ProcessTasks
OSVRUnreal-Win64-DebugGame.exe!FTaskThread::ProcessTasksUntilQuit
OSVRUnreal-Win64-DebugGame.exe!RenderingThreadMain
OSVRUnreal-Win64-DebugGame.exe!FRenderingThread::Run
OSVRUnreal-Win64-DebugGame.exe!FRunnableThreadWin::Run
OSVRUnreal-Win64-DebugGame.exe!FRunnableThreadWin::GuardedRun
kernel32.dll!BaseThreadInitThunk
ntdll.dll~RtlUserThreadStart


I can also reproduce a similar crash by attempting to set the render target myself (texture here is the texture created above):



SetRenderTarget(rhiCmdList, mRenderTexture, FTextureRHIRef());


Any ideas? This is blocking some direct mode functionality I’d like to implement in a VR plugin.