c++ - DirectX 11 - Compute Shader, copy data from the GPU to the CPU -
i've started using direct compute in attempt move fluid simulation have been working on, onto gpu. have found similar (if not identical) question here seems resolution problem not same theirs; have copyresource right way round sure! pasted question, buffer filled 0's when copy gpu. can't see error don't understand how can reaching out of bounds limits. i'm going apologise mass amount of code pasting occur want sure i've not got of setup wrong.
output buffer, uav , 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 shader in 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 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 = 0; < 4; i++) { debug::instance()->log(newbc[i].x.x); } _devicecontext->unmap(m_outputresult, 0);
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 compute shader after begin frame , before end frame. have played around moving shaders dispatch call outside of end scene , before present ect nothing seems effect process. can't seem figure 1 out!
holy smokes fixed error! creating compute shader different id3d11computeshader pointer! d: works charm! pheew sorry , adam!
Comments
Post a Comment