Dynamic Buffers and Compute Shaders

Started by
1 comment, last by _the_phantom_ 14 years, 5 months ago
I'm currently working on a compute shader which is effectively a rehash of my final year degree project and I've hit a bit of a snag. I need to have a structured buffer which I can write to every frame (or maybe every couple of frames, but certainly often) and read from via the shader. So, from the GPU's point of view it is read only and the cpu write only. Looking through the docs I came to the following conclusion with regards to the buffer description;

D3D11_BUFFER_DESC desc;
	ZeroMemory( &desc, sizeof(desc) );
	desc.Usage = D3D11_USAGE_DYNAMIC;
	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE;
	desc.ByteWidth = uElementSize * uCount;
	desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
	desc.StructureByteStride = uElementSize;




Which seems logical to me; - dynamic flag allows me to read on gpu, write on cpu - cpu access write says I'll be wanting to write to the buffer However, on calling CreateBuffer on my device I get;

D3D11: ERROR: ID3D11Device::CreateBuffer: A D3D11_USAGE_DYNAMIC Resource cannot be bound to certain parts of the graphics pipeline, but must have at least one BindFlags bit set. 
The BindFlags bits (0x88) have the following settings: D3D11_BIND_STREAM_OUTPUT (0), D3D11_BIND_RENDER_TARGET (0), D3D11_BIND_DEPTH_STENCIL (0), D3D11_BIND_UNORDERED_ACCESS (1). 
[ STATE_CREATION ERROR #64: CREATEBUFFER_INVALIDBINDFLAGS ]
First-chance exception at 0x7560b727 in D3D11VisualiserTest.exe: Microsoft C++ exception: _com_error at memory location 0x0018f4c4..
D3D11: ERROR: ID3D11Device::CreateBuffer: CreateBuffer returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #69: CREATEBUFFER_INVALIDARG_RETURN ]

thrown back at me. I'm a bit confused as to what it wants me to do, as from what I can see I HAVE a bind buffer bit set (UNORDERED_ACCESS) which should let me do what I need. So, any clues from anyone out there? Or even a better way to do it? (I'll be uploading somewhere in the region of 5120 floats per frame, in a linear manner but not contiguous in destination memory).
Advertisement
By definition, an unordered_access object is writable from the GPU, which is what's causing the problem. Since you only want GPU read, you can just bind your structured buffer as a shader_resource and the problem will be fixed. In HLSL that will be StructuredBuffer instead of RWStructuredBuffer.
Ah, I see.. yes, that makes sense and removing the D3D11_BIND_UNORDERED_ACCESS from my bind flags did indeed fix the problem.

Cheers and enjoy your rating++ [grin]

This topic is closed to new replies.

Advertisement