ShaderCompileTask.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Ryujinx.Graphics.GAL;
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace Ryujinx.Graphics.Gpu.Shader
  6. {
  7. delegate bool ShaderCompileTaskCallback(bool success, ShaderCompileTask task);
  8. /// <summary>
  9. /// A class that represents a shader compilation.
  10. /// </summary>
  11. class ShaderCompileTask
  12. {
  13. private bool _compiling;
  14. private Task _programsTask;
  15. private IProgram _program;
  16. private ShaderCompileTaskCallback _action;
  17. private AutoResetEvent _taskDoneEvent;
  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. }