3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
6 using SiliconStudio.Core.Serialization;
7 using SiliconStudio.Paradox.Shaders;
8 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
9 using OpenTK.Graphics.ES30;
10 using BinaryFormat = OpenTK.Graphics.ES30.ShaderBinaryFormat;
12 using OpenTK.Graphics.OpenGL;
15 namespace SiliconStudio.
Paradox.Graphics
17 public partial class Shader
20 private int shaderProgramId;
22 private Shader(GraphicsDevice device,
ShaderStage shaderStage, byte[] shaderStageBytecode)
25 this.stage = shaderStage;
27 var shaderStageGl = ConvertShaderStage(shaderStage);
30 var binarySerializationReader =
new BinarySerializationReader(
new MemoryStream(shaderStageBytecode));
31 var shaderBytecodeData =
new OpenGLShaderBytecodeData();
32 shaderBytecodeData.Serialize(binarySerializationReader, ArchiveMode.Deserialize);
34 using (GraphicsDevice.UseOpenGLCreationContext())
36 resourceId = GL.CreateShader(shaderStageGl);
38 if (shaderBytecodeData.IsBinary)
40 GL.ShaderBinary(1, ref resourceId, (BinaryFormat)shaderBytecodeData.BinaryFormat, shaderBytecodeData.Binary, shaderBytecodeData.Binary.Length);
44 GL.ShaderSource(resourceId, shaderBytecodeData.Source);
45 GL.CompileShader(resourceId);
47 var log = GL.GetShaderInfoLog(resourceId);
50 GL.GetShader(resourceId, ShaderParameter.CompileStatus, out compileStatus);
52 if (compileStatus != 1)
53 throw new InvalidOperationException(
string.Format(
"Error while compiling GLSL shader: {0}", log));
59 protected override void Destroy()
61 using (GraphicsDevice.UseOpenGLCreationContext())
63 GL.DeleteShader(resourceId);
71 private static ShaderType ConvertShaderStage(
ShaderStage shaderStage)
75 case ShaderStage.Pixel:
76 return ShaderType.FragmentShader;
77 case ShaderStage.Vertex:
78 return ShaderType.VertexShader;
79 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
80 case ShaderStage.Geometry:
81 return ShaderType.GeometryShader;
84 throw new NotSupportedException();
ComponentBase.Destroy() event.
ShaderStage
Enum to specify shader stage.