Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Shader.OpenGL.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
4 using System;
5 using System.IO;
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;
11 #else
12 using OpenTK.Graphics.OpenGL;
13 #endif
14 
15 namespace SiliconStudio.Paradox.Graphics
16 {
17  public partial class Shader
18  {
19  // Used for separate shader objects
20  private int shaderProgramId;
21 
22  private Shader(GraphicsDevice device, ShaderStage shaderStage, byte[] shaderStageBytecode)
23  : base(device)
24  {
25  this.stage = shaderStage;
26 
27  var shaderStageGl = ConvertShaderStage(shaderStage);
28 
29  // Decode shader StageBytecode
30  var binarySerializationReader = new BinarySerializationReader(new MemoryStream(shaderStageBytecode));
31  var shaderBytecodeData = new OpenGLShaderBytecodeData();
32  shaderBytecodeData.Serialize(binarySerializationReader, ArchiveMode.Deserialize);
33 
34  using (GraphicsDevice.UseOpenGLCreationContext())
35  {
36  resourceId = GL.CreateShader(shaderStageGl);
37 
38  if (shaderBytecodeData.IsBinary)
39  {
40  GL.ShaderBinary(1, ref resourceId, (BinaryFormat)shaderBytecodeData.BinaryFormat, shaderBytecodeData.Binary, shaderBytecodeData.Binary.Length);
41  }
42  else
43  {
44  GL.ShaderSource(resourceId, shaderBytecodeData.Source);
45  GL.CompileShader(resourceId);
46 
47  var log = GL.GetShaderInfoLog(resourceId);
48 
49  int compileStatus;
50  GL.GetShader(resourceId, ShaderParameter.CompileStatus, out compileStatus);
51 
52  if (compileStatus != 1)
53  throw new InvalidOperationException(string.Format("Error while compiling GLSL shader: {0}", log));
54  }
55  }
56  }
57 
58  /// <inheritdoc/>
59  protected override void Destroy()
60  {
61  using (GraphicsDevice.UseOpenGLCreationContext())
62  {
63  GL.DeleteShader(resourceId);
64  }
65 
66  resourceId = 0;
67 
68  base.Destroy();
69  }
70 
71  private static ShaderType ConvertShaderStage(ShaderStage shaderStage)
72  {
73  switch (shaderStage)
74  {
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;
82 #endif
83  default:
84  throw new NotSupportedException();
85  }
86  }
87  }
88 }
89 #endif
ComponentBase.Destroy() event.
ShaderStage
Enum to specify shader stage.
Definition: ShaderStage.cs:12