提问者:小点点

从ID3D11DeviceChild::GetDevice()方法获取设备


我尝试使用以下方法返回应用程序中的ID3D11Device对象:

https://docs.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11deviceChild-GetDevice

更具体地说,我使用返回的顶点缓冲区来尝试获取如下所示的设备:

pContext->IAGetVertexBuffers(0, 1, &veBuffer, &Stride, &veBufferOffset);
if (veBuffer)
    veBuffer->GetDesc(&vedesc);

D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D11_USAGE_STAGING;
bufferDesc.ByteWidth = vedesc.ByteWidth;
bufferDesc.BindFlags = 0;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
bufferDesc.MiscFlags = 0;

ID3D11Device **pDeviceExtra = nullptr;
veBuffer->GetDevice(pDeviceExtra); //Can I use this to get to the device object?

//Create the buffer.
HRESULT hr = pDeviceExtra->CreateBuffer(&bufferDesc, NULL, &readVB);
assert(hr == S_OK);

pContext->CopyResource(readVB, veBuffer);

我想创建一个缓冲区资源,但是,当我尝试使用返回的设备指针时,它说“Expression must have a pointer to class type”。 现在我对此做了一些研究,根据我收集到的信息,你必须取消对指向对象本身的指针的引用。 然而,在这种情况下,我不知道该怎么做? 我已经在我的DirectX代码中创建了一个设备,但是由于不同的原因,如果可能的话,我希望从顶点缓冲区中获取该设备。


共1个答案

匿名用户

ID3D11Device *device = nullptr;
veBuffer->GetDevice(&device);

// Call device method

device->Release();

注意事项:

>

  • 您应该将设备指针传递到可能想要创建资源的代码,或者为设备抽象创建一个单例,以便您可以从任何地方调用它,而不是使用像这样的黑客。

    使用Microsoft::WRL::Comptr而不是原始指针来管理引用计数。