ShaderCompileTask.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Ryujinx.Graphics.GAL;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace Ryujinx.Graphics.Gpu.Shader
  5. {
  6. delegate bool ShaderCompileTaskCallback(bool success, ShaderCompileTask task);
  7. /// <summary>
  8. /// A class that represents a shader compilation.
  9. /// </summary>
  10. class ShaderCompileTask
  11. {
  12. private bool _compiling;
  13. private Task _programsTask;
  14. private IProgram _program;
  15. private ShaderCompileTaskCallback _action;
  16. private AutoResetEvent _taskDoneEvent;
  17. public bool IsFaulted => _programsTask.IsFaulted;
  18. /// <summary>
  19. /// Create a new shader compile task, with an event to signal whenever a subtask completes.
  20. /// </summary>
  21. /// <param name="taskDoneEvent">Event to signal when a subtask completes</param>
  22. public ShaderCompileTask(AutoResetEvent taskDoneEvent)
  23. {
  24. _taskDoneEvent = taskDoneEvent;
  25. }
  26. /// <summary>
  27. /// Check the completion status of the shader compile task, and run callbacks on step completion.
  28. /// Calling this periodically is required to progress through steps of the compilation.
  29. /// </summary>
  30. /// <returns>True if the task is complete, false if it is in progress</returns>
  31. public bool IsDone()
  32. {
  33. if (_compiling)
  34. {
  35. ProgramLinkStatus status = _program.CheckProgramLink(false);
  36. if (status != ProgramLinkStatus.Incomplete)
  37. {
  38. return _action(status == ProgramLinkStatus.Success, this);
  39. }
  40. }
  41. else
  42. {
  43. // Waiting on the task.
  44. if (_programsTask.IsCompleted)
  45. {
  46. return _action(true, this);
  47. }
  48. }
  49. return false;
  50. }
  51. /// <summary>
  52. /// Run a callback when the specified task has completed.
  53. /// </summary>
  54. /// <param name="task">The task object that needs to complete</param>
  55. /// <param name="action">The action to perform when it is complete</param>
  56. public void OnTask(Task task, ShaderCompileTaskCallback action)
  57. {
  58. _compiling = false;
  59. _programsTask = task;
  60. _action = action;
  61. task.ContinueWith(task => _taskDoneEvent.Set());
  62. }
  63. /// <summary>
  64. /// Run a callback when the specified program has been linked.
  65. /// </summary>
  66. /// <param name="task">The program that needs to be linked</param>
  67. /// <param name="action">The action to perform when linking is complete</param>
  68. public void OnCompiled(IProgram program, ShaderCompileTaskCallback action)
  69. {
  70. _compiling = true;
  71. _program = program;
  72. _action = action;
  73. if (program == null)
  74. {
  75. action(false, this);
  76. }
  77. }
  78. }
  79. }