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

This topic is 420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have code like this:

groupshared uint tempData[ElementsCount];

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];

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 on other sites

Based on a quick search I found these:

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 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])
{

}

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 on other sites

https://blogs.msdn.microsoft.com/chuckw/2012/05/07/hlsl-fxc-and-d3dcompile/ explains all the details of how the compiler DLL works.

If you want to check which version of the DLL your program is using, then just pause it in the debugger and look through the modules window for the DLL. I believe the latest version is D3dcompiler_47.dll

Have you tried compiling the shader using fxc.exe?

##### 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 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 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])
{
}

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;
}

void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
MyFunc(gtID);
}


##### 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 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 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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007208

×