OglShaderProgram.cs 2.2 KB

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