简体   繁体   English

DirectX 11-Compute Shader,将数据从GPU复制到CPU

[英]DirectX 11 - Compute Shader, copy data from the GPU to the CPU

I've just started up using Direct compute in an attempt to move a fluid simulation I have been working on, onto the GPU. 我刚刚开始使用Direct计算,试图将一直在进行的流体模拟移动到GPU上。 I have found a very similar (if not identical) question here however seems the resolution to my problem is not the same as theirs; 我在这里发现了一个非常相似(如果不相同)的问题但是看来我的问题的解决方案与他们的解决方案不同; I do have my CopyResource the right way round for sure! 我的确有正确的方式来使用CopyResource! As with the pasted question, I only get a buffer filled with 0's when copy back from the GPU. 与粘贴的问题一样,从GPU复制回时,我只会得到一个填充有0的缓冲区。 I really can't see the error as I don't understand how I can be reaching out of bounds limits. 我真的看不到错误,因为我不知道如何达到极限。 I'm going to apologise for the mass amount of code pasting about to occur but I want be sure I've not got any of the setup wrong. 对于要粘贴的大量代码,我将深表歉意,但是我想确保没有设置错误。

Output Buffer, UAV and System Buffer set up 输出缓冲区,无人机和系统缓冲区设置

  outputDesc.Usage = D3D11_USAGE_DEFAULT; outputDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; outputDesc.ByteWidth = sizeof(BoundaryConditions) * numElements; outputDesc.CPUAccessFlags = 0; outputDesc.StructureByteStride = sizeof(BoundaryConditions); outputDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; result =_device->CreateBuffer(&outputDesc, 0, &m_outputBuffer); outputDesc.Usage = D3D11_USAGE_STAGING; outputDesc.BindFlags = 0; outputDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; result = _device->CreateBuffer(&outputDesc, 0, &m_outputresult); D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc; uavDesc.Format = DXGI_FORMAT_UNKNOWN; uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; uavDesc.Buffer.FirstElement = 0; uavDesc.Buffer.Flags = 0; uavDesc.Buffer.NumElements = numElements; result =_device->CreateUnorderedAccessView(m_outputBuffer, &uavDesc, &m_BoundaryConditionsUAV); 

Running the Shader in my frame loop 在我的框架循环中运行着色器

 HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; _deviceContext->CSSetShader(m_BoundaryConditionsCS, nullptr, 0); _deviceContext->CSSetUnorderedAccessViews(0, 1, &m_BoundaryConditionsUAV, 0); _deviceContext->Dispatch(1, 1, 1); // Unbind output from compute shader ID3D11UnorderedAccessView* nullUAV[] = { NULL }; _deviceContext->CSSetUnorderedAccessViews(0, 1, nullUAV, 0); // Disable Compute Shader _deviceContext->CSSetShader(nullptr, nullptr, 0); _deviceContext->CopyResource(m_outputresult, m_outputBuffer); D3D11_MAPPED_SUBRESOURCE mappedData; result = _deviceContext->Map(m_outputresult, 0, D3D11_MAP_READ, 0, &mappedData); BoundaryConditions* newbc = reinterpret_cast<BoundaryConditions*>(mappedData.pData); for (int i = 0; i < 4; i++) { Debug::Instance()->Log(newbc[i].xx); } _deviceContext->Unmap(m_outputresult, 0); 

HLSL HLSL

 struct BoundaryConditions { float3 x; float3 y; }; RWStructuredBuffer<BoundaryConditions> _boundaryConditions; [numthreads(4, 1, 1)] void ComputeBoundaryConditions(int3 id : SV_DispatchThreadID) { _boundaryConditions[id.x].x = float3(id.x,id.y,id.z); } 

I dispatch the Compute shader after I begin a frame and before I end the frame. 在开始帧之后和结束帧之前,我调度计算着色器。 I have played around with moving the shaders dispatch call outside of the end scene and before the present ect but nothing seems to effect the process. 我一直在尝试将着色器调度调用移到最终场景之外和当前示例之前,但是似乎没有任何影响。 Can't seem to figure this one out! 似乎无法弄清楚这一点!

Holy Smokes I fixed the error! 我解决了错误! I was creating the compute shader to a different ID3D11ComputeShader pointer! 我正在为另一个ID3D11ComputeShader指针创建计算着色器! D: Works like a charm! D:像魅力一样! Pheew Sorry and thanks Adam! 抱歉,谢谢亚当!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM