Sign in to follow this  
loreStefani

[D3D12] FXC shader model 5.1 crash

Recommended Posts

Hello everyone,

I'm having problem compiling the following HLSL pixel shader with shader model 5.1:

struct VShaderOutput
{
	float4 clipPos : SV_POSITION;
	float3 position : POSITION;
	float3 normal : NORMAL;
	float2 textCoord : TEXTCOORD;
};

struct DirectionalLight
{
	float3 color;
	float pad;
	float3 direction;
	float pad1;
};

#define DIR_LIGHTS_COUNT 10

ConstantBuffer<DirectionalLight> directionalLights[DIR_LIGHTS_COUNT] : register(b0);

struct SceneState
{
	float3 eyePosition;
};

ConstantBuffer<SceneState> sceneState : register(b0, space1);


float4 main(VShaderOutput input) : SV_TARGET
{
	float3 normal = normalize(input.normal);
	float3 position = input.position;
	float3 toEye = normalize(sceneState.eyePosition - position);
	float3 color = float3(0.05f, 0.05f, 0.05f);

	float3 diffuseColor = float3(0.5f, 0.5f, 0.5f);

	for (int i = 0; i < DIR_LIGHTS_COUNT; i++)
	{
		float3 toLight = -directionalLights[i].direction;
		float3 lightColor = directionalLights[i].color;

		float cosThetaL = dot(normal, toLight);
		float3 incidentRadiance = max(cosThetaL, 0.0f)* lightColor;
		color += diffuseColor*incidentRadiance;
	}

	return float4(color, 1.0f);
}

FXC crashes and the function D3DCompileFromFile triggers an access violation reading exception. This only happens when optimizations are enabled (in the first case with the "Disable Optimizations" option set to "No" in the Visual Studio property page, and using any D3DCOMPILE_OPTIMIZATION_LEVELi flag in the second one).

The error message generated in the first case is the following:

 

Severity Code Description Project File Line Suppression State
Error MSB6006 "fxc.exe" exited with code -1073741819. FXCBug C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets 1231
 
Where FXCBug is the project name.
 
Any help/suggestion is really really appreciated.
 
 
 

Share this post


Link to post
Share on other sites

This still crashes on the latest internal builds of the compiler, so I'll pass this shader on for the team to take a look at.

 

In the meantime, it's probably unlikely that you actually wanted 10 constant buffers for your 10 directional lights? Is there a reason you didn't have a single constant buffer with an array of 10 directional lights inside?

 

This works fine, but will need a small adjustment to your CPU-side code:

//ConstantBuffer<DirectionalLight> directionalLights[DIR_LIGHTS_COUNT] : register(b0);  // 10 constant buffers, crash.

cbuffer DirectionalLights : register(b0)
{
	DirectionalLight directionalLights[DIR_LIGHTS_COUNT];   // 1 constant buffer, with 10 structures inside.
}

Thanks!

Edited by Adam Miles

Share this post


Link to post
Share on other sites

Thank you very much for your attention.

 

For the 10 directional lights thing, I was experimenting with unbounded descriptor tables and dynamic indexing, and the shader was a bit different. But I started removing pieces trying to find the crash cause.

My goal was to specify the number of lights dynamically through another constant buffer in order to compile just one version of the shader. I can't do it with one constant buffer, am I right?

Share this post


Link to post
Share on other sites

Since we're here, I have another case in which the compiler fails:

struct VertexOut
{
	float4 positionH : SV_POSITION;
	float2 textCoord : TEXTCOORD;
};

struct TextureIndex
{
	uint index;
};

ConstantBuffer<TextureIndex> textureIndex : register(b0);

#define TEXTURES_COUNT 10

Texture2D<float4> textures[TEXTURES_COUNT] : register(t0);

SamplerState textureSampler : register(s0);

float4 main(VertexOut pin) : SV_TARGET
{
	return textures.Sample(textureSampler, pin.textCoord); //should be textures[textureIndex.index].Sample(...)
}

The compiler crashes regardless any optimization setting.

I understand you are in contact with the team, can you pass this too or teach me how to report a bug, please?

Edited by loreStefani

Share this post


Link to post
Share on other sites

If you want threads to dynamically index into a data structure containing lots of lights then you probably just want a StructuredBuffer<DirectionalLight>. Dynamically indexing into a StructuredBuffer is far faster than dynamically indexing into an unbounded (or bounded) array of Constant Buffers, each containing a DirectionalLight.

 

You're right that you can't compile a shader with a single Constant Buffer containing an unbounded array of Directional Lights, but the solution is not an array of Constant Buffers.

 

The second shader you've provided doesn't crash the newest version of the compiler, so the bug has already been fixed and will be available when the next version of the Windows SDK is made available.

C:\Users\amile\Desktop\crash.txt(22,9-54): error X3087: const Texture2D<float4>[1] object does not have methods

compilation failed; no code produced

I believe the right way to file bugs against the Windows SDK is to file it on http://connect.microsoft.com/VisualStudio, however if I find that's not the case I'll update this post.

Edited by Adam Miles

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