So I’ve been having this problem for the last few days and have been unable to find anything in the forums or elsewhere.
Everything works fine until, at the slightest change in assets, my shader stops working entirely. There isn’t even any wireframe showing up, so it’s not just a texture problem. I assume that means my shader breaks when it’s reloaded by the editor but I have no idea how. And if it is, I don’t know whether I did something wrong and where.
I have a custom instanced shader where I use SV_InstanceID and a structured buffer of a custom struct to retrieve my instances’ data:
StructuredBuffer<Properties> _Properties;
Properties InstanceProperties(uint instanceID)
{
return _Properties[instanceID];
}
Interpolator vert (MeshVertex mesh, uint instanceID: SV_InstanceID)
{
Interpolator interpolator;
// my custom instance struct
Properties properties = InstanceProperties(instanceID);
// the transform matrix is computed in csharp
float4 pos = mul(properties.mat, mesh.position);
interpolator.position = UnityObjectToClipPos(pos); // : SV_Position
// ... setting the interpolator up
return interpolator;
}
On the C# side, my ComputeBuffer is created once, and assigned to my material at the same time with the following code:
propertiesBuffer = new ComputeBuffer(/* args */);
material.SetBuffer("_Properties", propertiesBuffer);
Then whenever my custom render pass needs to render, I update my buffer with ComputeBuffer.BeginWrite and ComputeBuffer.EndWrite. And render using CommandBuffer.DrawMeshInstancedIndirect. (Note: I had the same problem when I was using Graphics.DrawMeshInstancedIndirect in an Update so I don’t think the render pass is the issue here).
public void Draw(CommandBuffer cmd)
{
cmd.DrawMeshInstancedIndirect(mesh, 0, material, -1, argsBuffer);
}
The material is created at runtime from the shader:
material = new Material(shader);
material.enableInstancing = true;
And the shader is retrieved using addressables:
Addressables.LoadAssetAsync<Shader>("Sprite shader").Completed += handle =>
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
shader = handle.Result;
}
else
{
Debug.LogError("Failed to load sprite shader.");
}
};
I don’t know what step is broken by the asset hot reload, does anyone have a clue ?