urho
urho copied to clipboard
[UWP] Failed to create sample state and related errors
I have a scene with a model where I want to periodically change the material of the model upon user interaction. Sometimes it works but at least in half of the cases I get an error message from Urho itself, or even from the underlying Win DirectX implementation. In some cases, the whole screen turns black for a second. The reason for mentioning it here first rather than in the original Urho project is that I can't seem to be able to at least isolate and report the error in UrhoSharp cleanly.
The error is:
11/25/2018 11:28:11 +00:00 - Failed to create sampler state (HRESULT 887a0005). You can omit this exception by subscribing to Urho.Application.UnhandledException event and set Handled property to True.
ApplicationOptions: args -w -x 737 -y 826 -p "CoreData" -touch -hd -landscape -portrait
11/25/2018 11:28:11 +00:00 - Unhandled error -- NullReferenceException: Object reference not set to an instance of an object.
at MyUrhoApplication.<>c.<.cctor>b__17_0(Object s, UnhandledExceptionEventArgs e)
at Urho.Application.ThrowUnhandledException(Exception exc)
at Urho.Runtime.OnNativeCallback(CallbackType type, IntPtr target, IntPtr param1, Int32 param2, String param3)
at Urho.Engine.Engine_RunFrame(IntPtr handle)
at Urho.UWP.UrhoSurface.<>c__DisplayClass11_0.<<Run>b__0>d.MoveNext()
The first comes in via the UnhandledException
exception handler and is only logged but the second one brings my app down.
I set up the scene the usual way, no problem with that and the first image always appears with the texture-based material all right, so that's not part of the issue. I change the material with this function:
public void Redraw(byte[] bitmap) {
try {
using (var image = new Image())
if (image != null)
using (var buffer = new MemoryBuffer(bitmap))
if (image.Load(buffer)) {
model.Material?.Dispose();
model.Material = Material.FromImage(image);
}
}
catch (Exception e) {
Logger.Log.Error(e);
}
}
As you can see, it's all in a try-catch
, still I get the exception from somewhere. It refers to the app constructor which is completely empty in my case but, of course, calls the overridden one:
public MyUrhoApplication(ApplicationOptions options = null)
: base(options) {
}
First, I can't see the obvious culprit in the UrhoSharp source code. Second, perhaps more importantly, I can't understand why this constructor is called again later in the process, not when I set it up but when I merely change the material and update the scene.
Ensure you're doing this from the main thread.
InvokeOnMain or InvokeOnMainAsync
Messing with materials is a main thread activity.
This might not be your problem, but I've seen it throw errors like you point out, which don't necessarily correlate to what's happening. The reason I point this out is because of the screen blacking out that you mentioned, there is a reason for that.
Another possible issue is Actions running on the node you're changing the material of, and they are using the old material and have a reference to it, but you came along and changed the material, and urho didn't respect their reference, which brings up the AddRef() call that can help if you don't want to remove the actions before changing materials.
Also, some of those errors like you point out can be e.Handled=true, and life goes on. But you have to specifically do it. So maybe you're not yet chasing the right error for your material. In my experience, changing the materials is very problematic, but in general works, you have to have very good scene management. Alternatively, other patterns work very well, without problems. Such as enabling one node, and disabling a second, instead of doing a material changeover. Or use Actions and do a Hide() on one, and a Show() on the other.
this constructor is called again later
Probably not the constructor, but it is looking for the Application.UnhandledException handler. That's where you e.Handled=true the unhandled exceptions, and attempt to move on.