OglShaderProgram.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.Shader;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gal.OpenGL
  6. {
  7. struct OglShaderProgram
  8. {
  9. public OglShaderStage Vertex;
  10. public OglShaderStage TessControl;
  11. public OglShaderStage TessEvaluation;
  12. public OglShaderStage Geometry;
  13. public OglShaderStage Fragment;
  14. }
  15. class OglShaderStage : IDisposable
  16. {
  17. public int Handle { get; private set; }
  18. public bool IsCompiled { get; private set; }
  19. public GalShaderType Type { get; private set; }
  20. public string Code { get; private set; }
  21. public IEnumerable<CBufferDescriptor> ConstBufferUsage { get; private set; }
  22. public IEnumerable<TextureDescriptor> TextureUsage { get; private set; }
  23. public OglShaderStage(
  24. GalShaderType type,
  25. string code,
  26. IEnumerable<CBufferDescriptor> constBufferUsage,
  27. IEnumerable<TextureDescriptor> textureUsage)
  28. {
  29. Type = type;
  30. Code = code;
  31. ConstBufferUsage = constBufferUsage;
  32. TextureUsage = textureUsage;
  33. }
  34. public void Compile()
  35. {
  36. if (Handle == 0)
  37. {
  38. Handle = GL.CreateShader(OglEnumConverter.GetShaderType(Type));
  39. CompileAndCheck(Handle, Code);
  40. }
  41. }
  42. public void Dispose()
  43. {
  44. Dispose(true);
  45. }
  46. protected virtual void Dispose(bool disposing)
  47. {
  48. if (disposing && Handle != 0)
  49. {
  50. GL.DeleteShader(Handle);
  51. Handle = 0;
  52. }
  53. }
  54. public static void CompileAndCheck(int handle, string code)
  55. {
  56. GL.ShaderSource(handle, code);
  57. GL.CompileShader(handle);
  58. CheckCompilation(handle);
  59. }
  60. private static void CheckCompilation(int handle)
  61. {
  62. int status = 0;
  63. GL.GetShader(handle, ShaderParameter.CompileStatus, out status);
  64. if (status == 0)
  65. {
  66. throw new ShaderException(GL.GetShaderInfoLog(handle));
  67. }
  68. }
  69. }
  70. }