CachedShaderProgram.cs
1 using Ryujinx.Graphics.GAL; 2 using System; 3 4 namespace Ryujinx.Graphics.Gpu.Shader 5 { 6 /// <summary> 7 /// Represents a program composed of one or more shader stages (for graphics shaders), 8 /// or a single shader (for compute shaders). 9 /// </summary> 10 class CachedShaderProgram : IDisposable 11 { 12 /// <summary> 13 /// Host shader program object. 14 /// </summary> 15 public IProgram HostProgram { get; } 16 17 /// <summary> 18 /// Optional vertex shader converted to compute. 19 /// </summary> 20 public ShaderAsCompute VertexAsCompute { get; } 21 22 /// <summary> 23 /// Optional geometry shader converted to compute. 24 /// </summary> 25 public ShaderAsCompute GeometryAsCompute { get; } 26 27 /// <summary> 28 /// GPU state used to create this version of the shader. 29 /// </summary> 30 public ShaderSpecializationState SpecializationState { get; } 31 32 /// <summary> 33 /// Compiled shader for each shader stage. 34 /// </summary> 35 public CachedShaderStage[] Shaders { get; } 36 37 /// <summary> 38 /// Cached shader bindings, ready for placing into the bindings manager. 39 /// </summary> 40 public CachedShaderBindings Bindings { get; } 41 42 /// <summary> 43 /// Creates a new instance of the shader bundle. 44 /// </summary> 45 /// <param name="hostProgram">Host program with all the shader stages</param> 46 /// <param name="specializationState">GPU state used to create this version of the shader</param> 47 /// <param name="shaders">Shaders</param> 48 public CachedShaderProgram(IProgram hostProgram, ShaderSpecializationState specializationState, params CachedShaderStage[] shaders) 49 { 50 HostProgram = hostProgram; 51 SpecializationState = specializationState; 52 Shaders = shaders; 53 54 SpecializationState.Prepare(shaders); 55 Bindings = new CachedShaderBindings(shaders.Length == 1, shaders); 56 } 57 58 public CachedShaderProgram( 59 IProgram hostProgram, 60 ShaderAsCompute vertexAsCompute, 61 ShaderAsCompute geometryAsCompute, 62 ShaderSpecializationState specializationState, 63 CachedShaderStage[] shaders) : this(hostProgram, specializationState, shaders) 64 { 65 VertexAsCompute = vertexAsCompute; 66 GeometryAsCompute = geometryAsCompute; 67 } 68 69 /// <summary> 70 /// Dispose of the host shader resources. 71 /// </summary> 72 public void Dispose() 73 { 74 HostProgram.Dispose(); 75 VertexAsCompute?.HostProgram.Dispose(); 76 GeometryAsCompute?.HostProgram.Dispose(); 77 } 78 } 79 }