Sign in to follow this  

DX11 How to solve an error with unordered access view?

Recommended Posts

[left][background=rgb(250, 251, 252)]I try to modidy and reorganize of OIT soure code, [/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)], and the source code can be downloaded from [/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]. But I get an error "[/background][/size][/font][/color][/left]
The Unordered Access View in slot 1 of the Pixel Shader unit was not created with the D3D11_BUFFER_UAV_FLAG_RAW flag" when I rendered


HRESULT CreateFragmentLink( const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, ID3D11Device* pd3dDevice )

const int NUM_ELEMENTS = pBackBufferSurfaceDesc->Width * pBackBufferSurfaceDesc->Height * 8;

// create a structured buffer
D3D11_BUFFER_DESC descBuf;
memset( &descBuf, 0, sizeof( descBuf ) );
descBuf.StructureByteStride = sizeof( FragmentLink );
descBuf.ByteWidth = NUM_ELEMENTS * descBuf.StructureByteStride;
descBuf.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; // indicates that this buffer will be used as a structured buffer
descBuf.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; // GPU read & write, CPU cannot write
V_RETURN( pd3dDevice->CreateBuffer( &descBuf, NULL, &g_pFragmentLink ) );

// create UAV
memset( &descUAV, 0, sizeof( descUAV ) );
descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
descUAV.Buffer.FirstElement = 0;
descUAV.Format = DXGI_FORMAT_UNKNOWN; // for structured buffer, this value must be used
descUAV.Buffer.NumElements = NUM_ELEMENTS;
descUAV.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_COUNTER; // To enable to use internal counter
V_RETURN( pd3dDevice->CreateUnorderedAccessView( g_pFragmentLink, &descUAV, &g_pFragmentLinkUAV ) );

// create SRV
descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
descSRV.Buffer.FirstElement = 0;
descSRV.Buffer.NumElements = NUM_ELEMENTS;
V_RETURN( pd3dDevice->CreateShaderResourceView( g_pFragmentLink, &descSRV, &g_pFragmentLinkSRV ) );

return hr;

HRESULT CreateStartOffset( const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, ID3D11Device* pd3dDevice )
const int NUM_ELEMENTS = pBackBufferSurfaceDesc->Width * pBackBufferSurfaceDesc->Height;
// create a byte address buffer
D3D11_BUFFER_DESC descBuf;
memset( &descBuf, 0, sizeof( descBuf ) );
descBuf.StructureByteStride = sizeof( unsigned int );
descBuf.ByteWidth = NUM_ELEMENTS * descBuf.StructureByteStride;
descBuf.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS; // allow to get the returned value in 4 bytes
V_RETURN( pd3dDevice->CreateBuffer( &descBuf, NULL, &g_pStartOffsetBuffer ) );
// create UAV
memset( &descUAV, 0, sizeof( descUAV ) );
descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
descUAV.Buffer.FirstElement = 0;
descUAV.Buffer.NumElements = NUM_ELEMENTS;
descUAV.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
V_RETURN( pd3dDevice->CreateUnorderedAccessView( g_pStartOffsetBuffer, &descUAV, &g_pStartOffsetUAV ) );
// create SRV
descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
descSRV.Buffer.FirstElement = 0;
descSRV.Format = DXGI_FORMAT_R32_UINT;
descSRV.Buffer.NumElements = NUM_ELEMENTS;
V_RETURN( pd3dDevice->CreateShaderResourceView( g_pStartOffsetBuffer, &descSRV, &g_pStartOffsetSRV ) );
return hr;

void StoreFragments::OnRender( ID3D11DeviceContext* pd3dImmediateContext, ID3D11Device* pd3dDevice, D3DXMATRIX* pmWorld, D3DXMATRIX* pmViewProj,
ID3D11UnorderedAccessView* pFragmentLinkUAV, ID3D11UnorderedAccessView* pStartOffsetUAV,
const DXGI_SURFACE_DESC* pBackBufferDesc ) {

#pragma region clear Render target & depth stencil & UAV
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor );

ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0f, 0.0f );

static const UINT clearValueUINT[1] = { 0xFFFFFFFF };
pd3dImmediateContext->ClearUnorderedAccessViewUint( pStartOffsetUAV, clearValueUINT );
#pragma endregion

#pragma region update constant buffers
V( pd3dImmediateContext->Map( m_pVS_CB, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
VS_CB* pVS_CB = ( VS_CB* )MappedResource.pData;
D3DXMatrixTranspose( &pVS_CB->m_mWorldViewProjection, pmViewProj );
pd3dImmediateContext->Unmap( m_pVS_CB, 0 );
pd3dImmediateContext->VSSetConstantBuffers( 0, 1, &m_pVS_CB );

V( pd3dImmediateContext->Map( m_pPS_CB, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource2 ) );
PS_CB* pPS_CB = ( PS_CB* )MappedResource2.pData;
pPS_CB->nFrameWidth = pBackBufferDesc->Width;
pPS_CB->nFrameHeight = pBackBufferDesc->Height;
pd3dImmediateContext->Unmap( m_pPS_CB, 0 );
pd3dImmediateContext->PSSetConstantBuffers( 0, 1, &m_pPS_CB );
#pragma endregion

#pragma region set UAV & RTV
ID3D11UnorderedAccessView* pUAVs[2];
pUAVs[0] = pFragmentLinkUAV;
pUAVs[1] = pStartOffsetUAV;
UINT Indices[] = {0, 0};
pd3dImmediateContext->OMSetRenderTargetsAndUnorderedAccessViews( 0, NULL, pDSV, 1, 2, pUAVs, Indices );
#pragma endregion

pd3dImmediateContext->OMSetDepthStencilState( m_pDepthStencilState, 0 );

#pragma region set VB & render
UINT uStrides = sizeof( SCENE_VERTEX );
UINT uOffsets = 0;
pd3dImmediateContext->IASetVertexBuffers( 0, 1, &m_pVB, &uStrides, &uOffsets );
//pd3dImmediateContext->IASetIndexBuffer( NULL, DXGI_FORMAT_R32_UINT, 0 );
pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
pd3dImmediateContext->IASetInputLayout( m_pVertexLayout );
pd3dImmediateContext->VSSetShader( m_pVS, NULL, 0 );
pd3dImmediateContext->PSSetShader( m_pPS, NULL, 0 );

pd3dImmediateContext->Draw( 12, 0 );
#pragma endregion

And this is the shader code
cbuffer cbPerObject : register( b0 )
matrix g_mWorldViewProjection : packoffset( c0 );
struct SceneVS_Input
float4 pos : POSITION;
float4 color : COLOR;
struct SceneVS_Output
float4 pos : SV_POSITION;
float4 color : COLOR0;
// Fragment And Link Buffer
RWStructuredBuffer< FragmentLink > FLBuffer : register( u0 );
// Start Offset Buffer
RWByteAddressBuffer StartOffsetBuffer : register( u1 );
SceneVS_Output SceneVS( SceneVS_Input input )
SceneVS_Output output;

output.color = input.color;
output.pos = mul(input.pos, g_mWorldViewProjection );

return output;
void StoreFragmentsPS( SceneVS_Output input )
uint x = input.pos.x; // [0,g_nFrameWidth]
uint y = input.pos.y; // [0,g_nFrameHeight]
// Create fragment data.
uint4 nColor = saturate( input.color ) * 255;
FragmentLink element;
element.fragmentData.nColor = (nColor.x) | (nColor.y << 8) | (nColor.z << 16) | (nColor.a << 24);
element.fragmentData.fDepth = input.pos.z;
// Increment and get current pixel count.
uint nPixelCount= FLBuffer.IncrementCounter();
// Read and update Start Offset Buffer.
uint nIndex = y * g_nFrameWidth + x;
uint nStartOffsetAddress = 4 * nIndex;
uint nOldStartOffset;
nStartOffsetAddress, nPixelCount, nOldStartOffset );
// Store fragment link.
element.nNext = nOldStartOffset;
FLBuffer[ nPixelCount ] = element;

With the error, I think that UAV in slot 1 is StartOffsetUAV but I used D3D11_BUFFER_UAV_FLAG_RAW flag as I was creating StartOffsetUAV, so why do I get this sort of error? Anybody help me, please? Edited by steven166

Share this post

Link to post
Share on other sites
I have already found out. In shader, I start from slot 0, but the parameters I used for [color=#660066][size=2][left][background=rgb(248, 248, 248)]OMSetRenderTargetsAndUnorderedAccessViews [/background][/left][/size][/color]function is star from slot 1 :(

Share this post

Link to post
Share on other sites
It doesn't start from slot 1, it starts from the first slot not used by a render target. This is because UAV's and render targets share slots for pixel shaders. So if you had 3 render targets, your UAV's would start at slot 3.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By evelyn4you
      i have read very much about the binding of a constantbuffer to a shader but something is still unclear to me.
      e.g. when performing :   vertexshader.setConstantbuffer ( buffer,  slot )
       is the buffer bound
      a.  to the VertexShaderStage
      b. to the VertexShader that is currently set as the active VertexShader
      Is it possible to bind a constantBuffer to a VertexShader e.g. VS_A and keep this binding even after the active VertexShader has changed ?
      I mean i want to bind constantbuffer_A  to VS_A, an Constantbuffer_B to VS_B  and  only use updateSubresource without using setConstantBuffer command every time.

      Look at this example:
      SetVertexShader ( VS_A )
      vertexshader.setConstantbuffer ( buffer_A,  slot_A )
      perform drawcall       ( buffer_A is used )

      SetVertexShader ( VS_B )
      vertexshader.setConstantbuffer ( buffer_B,  slot_A )
      perform drawcall   ( buffer_B is used )
      SetVertexShader ( VS_A )
      perform drawcall   (now which buffer is used ??? )
      I ask this question because i have made a custom render engine an want to optimize to
      the minimum  updateSubresource, and setConstantbuffer  calls
    • By noodleBowl
      I got a quick question about buffers when it comes to DirectX 11. If I bind a buffer using a command like:
      IASetVertexBuffers IASetIndexBuffer VSSetConstantBuffers PSSetConstantBuffers  and then later on I update that bound buffer's data using commands like Map/Unmap or any of the other update commands.
      Do I need to rebind the buffer again in order for my update to take effect? If I dont rebind is that really bad as in I get a performance hit? My thought process behind this is that if the buffer is already bound why do I need to rebind it? I'm using that same buffer it is just different data
    • By Rockmover
      I am really stuck with something that should be very simple in DirectX 11. 
      1. I can draw lines using a PC (position, colored) vertices and a simple shader just fine.
      2. I can draw 3D triangles using PCN (position, colored, normal) vertices just fine (even transparency and SpecularBlinnPhong shaders).
      However, if I'm using my 3D shader, and I want to draw my PC lines in the same scene how can I do that?
      If I change my lines to PCN and pass them to the 3D shader with my triangles, then the lighting screws them all up.  I only want the lighting for the 3D triangles, but no SpecularBlinnPhong/Lighting for the lines (just PC). 
      I am sure this is because if I change the lines to PNC there is not really a correct "normal" for the lines.  
      I assume I somehow need to draw the 3D triangles using one shader, and then "switch" to another shader and draw the lines?  But I have no clue how to use two different shaders in the same scene.  And then are the lines just drawn on top of the triangles, or vice versa (maybe draw order dependent)?  
      I must be missing something really basic, so if anyone can just point me in the right direction (or link to an example showing the implementation of multiple shaders) that would be REALLY appreciated.
      I'm also more than happy to post my simple test code if that helps as well!
    • By Reitano
      I am writing a linear allocator of per-frame constants using the DirectX 11.1 API. My plan is to replace the traditional constant allocation strategy, where most of the work is done by the driver behind my back, with a manual one inspired by the DirectX 12 and Vulkan APIs.
      In brief, the allocator maintains a list of 64K pages, each page owns a constant buffer managed as a ring buffer. Each page has a history of the N previous frames. At the beginning of a new frame, the allocator retires the frames that have been processed by the GPU and frees up the corresponding space in each page. I use DirectX 11 queries for detecting when a frame is complete and the ID3D11DeviceContext1::VS/PSSetConstantBuffers1 methods for binding constant buffers with an offset.
      The new allocator appears to be working but I am not 100% confident it is actually correct. In particular:
      1) it relies on queries which I am not too familiar with. Are they 100% reliable ?
      2) it maps/unmaps the constant buffer of each page at the beginning of a new frame and then writes the mapped memory as the frame is built. In pseudo code:
 = device.Map(page.buffer)
          Alloc(size, initData)
              memcpy( + page.start, initData, size)
          Alloc(size, initData)
              memcpy( + page.start, initData, size)
      (Note: calling Unmap at the end of a frame prevents binding the mapped constant buffers and triggers an error in the debug layer)
      Is this valid ? 
      3) I don't fully understand how many frames I should keep in the history. My intuition says it should be equal to the maximum latency reported by IDXGIDevice1::GetMaximumFrameLatency, which is 3 on my machine. But, this value works fine in an unit test while on a more complex demo I need to manually set it to 5, otherwise the allocator starts overwriting previous frames that have not completed yet. Shouldn't the swap chain Present method block the CPU in this case ?
      4) Should I expect this approach to be more efficient than the one managed by the driver ? I don't have meaningful profile data yet.
      Is anybody familiar with the approach described above and can answer my questions and discuss the pros and cons of this technique based on his experience ? 
      For reference, I've uploaded the (WIP) allocator code at  Feel free to adapt it in your engine and please let me know if you spot any mistakes
      Stefano Lanza
    • By Matt Barr
      Hey all. I've been working with compute shaders lately, and was hoping to build out some libraries to reuse code. As a prerequisite for my current project, I needed to sort a big array of data in my compute shader, so I was going to implement quicksort as a library function. My implementation was going to use an inout array to apply the changes to the referenced array.

      I spent half the day yesterday debugging in visual studio before I realized that the solution, while it worked INSIDE the function, reverted to the original state after returning from the function.

      My hack fix was just to inline the code, but this is not a great solution for the future.  Any ideas? I've considered just returning an array of ints that represents the sorted indices.
  • Popular Now