Jump to content
  • Advertisement
Sign in to follow this  
maxest

DX11 HLSL race condition when writing to shared memory passed to function

Recommended Posts

I have code like this:

groupshared uint tempData[ElementsCount];

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    tempData[gtID.x] = 0;
}

And it works fine. Now I change it to this:

void MyFunc(inout uint3 gtID: SV_GroupThreadID, inout uint inputData[ElementsCount])
{
    inputData[gtID.x] = 0;
}

groupshared uint tempData[ElementsCount];

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID, tempData);
}

and I get "error X3695: race condition writing to shared memory detected, consider making this write conditional.". Any way to go around this?

Share this post


Link to post
Share on other sites
Advertisement

Based on a quick search I found these:

 

https://www.gamedev.net/topic/594131-dx11-compute-shader-race-condition-error-when-using-optimization-level-2-or-3/

http://xboxforums.create.msdn.com/forums/t/63981.aspx

 

Based on those it sounds like there might be a bug in certain versions of the compiler. I'd suggest trying to use either command line fxc.exe or a more recent version of the d3dcompiler dll to see if it makes any difference.

Share this post


Link to post
Share on other sites

I stumbled upon those threads as well and it's not it.

Also, I'm not really sure how to update my d3dcompiler. I'm using Windows 10 so I presume it gets updated automatically. Although I use Visual Studio 2013 so I cannot really be sure if the most up-to-date dll is used.

I found out that the problem appears even in this code:

static const int ElementsCount = 512;


groupshared uint tempData[2 * ElementsCount];


void MyFunc(inout uint3 gtID: SV_GroupThreadID, inout uint inputData[2 * ElementsCount])
{

}


[numthreads(ElementsCount, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID, tempData);
}

Note that I don't even write anything to tempData in MyFunc.
I also found out the problem goes away if I remove the "inout" modifier but then the array just gets copied probably as the code doesn't work as expected.

Edited by maxest

Share this post


Link to post
Share on other sites

Yeah, I have D3dcompiler_47.dll indeed.

I did try. Forgot to mention that in previous post. The same problem persists.

Share this post


Link to post
Share on other sites

There is many version of d3dcompiler_47.dll, a very dumb idea…

If your shader just compile from visual studio as a hlsl source file, the fxc and dll you use is probably bound to the windows sdk that is setup in your project.

 

Not saying that getting the latest one would solve this, but you may still run an outdated compiler :)

Share this post


Link to post
Share on other sites

I've reproduced the behaviour, and simplified the case that goes wrong. Here's my minimal failing case:

groupshared uint tempData[1];

void MyFunc(inout uint inputData[1])
{
}

[numthreads(2, 1, 1)]
void CSMain()
{
    MyFunc(tempData);
}

It looks like just passing the argument to the function is enough to make it fail to compile.

Here's a workaround for the problem - don't pass the array as a function argument:

#define ElementsCount 256

groupshared uint tempData[ElementsCount];

void MyFunc(in uint3 gtID)
{
      tempData[gtID.x] = 0;
}

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID);
}

Share this post


Link to post
Share on other sites

Yeah, I'm perfectly aware of that workaround and I do it this way. But because I can't pass a shared memory array to function I can't make the function more general. Instead I need to copy it to a few files I use it in.

Share this post


Link to post
Share on other sites

I found a better workaround. So simple I can't imagine how I could had not come up with it before. I just used macro.

Still, would be nice if this bug was fixed. In the meantime I will be using macros on functions getting shared buffers as input.

Share this post


Link to post
Share on other sites

In the latest Xbox compiler the alleged race condition is just a warning rather than an error like it is in the 15063 Windows SDK. I've added a bug to make sure it's neither.

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  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!