webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
gl_raii.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // gl_raii:
7 // Helper methods for containing GL objects like buffers and textures.
8 
9 #ifndef ANGLE_TESTS_GL_RAII_H_
10 #define ANGLE_TESTS_GL_RAII_H_
11 
12 #include <functional>
13 
14 #include "angle_gl.h"
15 
16 namespace angle
17 {
18 
19 // This is a bit of hack to work around a bug in MSVS intellisense, and make it very easy to
20 // use the correct function pointer type without worrying about the various definitions of
21 // GL_APICALL.
22 using GLGen = decltype(glGenBuffers);
23 using GLDelete = decltype(glDeleteBuffers);
24 
25 template <GLGen GenF, GLDelete DeleteF>
26 class GLWrapper
27 {
28  public:
29  GLWrapper() {}
30  ~GLWrapper() { DeleteF(1, &mHandle); }
31 
32  GLuint get()
33  {
34  if (!mHandle)
35  {
36  GenF(1, &mHandle);
37  }
38  return mHandle;
39  }
40 
41  private:
42  GLuint mHandle = 0;
43 };
44 
49 
50 class GLProgram
51 {
52  public:
53  GLProgram(const std::string &vertexShader, const std::string &fragmentShader)
54  : mHandle(0), mVertexShader(vertexShader), mFragmentShader(fragmentShader)
55  {
56  }
57 
58  GLProgram(const std::string &computeShader) : mHandle(0), mComputeShader(computeShader) {}
59 
60  ~GLProgram() { glDeleteProgram(mHandle); }
61 
62  GLuint get()
63  {
64  if (mHandle == 0)
65  {
66  if (!mComputeShader.empty())
67  {
68  mHandle = CompileComputeProgram(mComputeShader);
69  }
70  else
71  {
72  mHandle = CompileProgram(mVertexShader, mFragmentShader);
73  }
74  }
75  return mHandle;
76  }
77 
78  private:
79  GLuint mHandle;
80  const std::string mVertexShader;
81  const std::string mFragmentShader;
82  const std::string mComputeShader;
83 };
84 
85 #define ANGLE_GL_PROGRAM(name, vertex, fragment) \
86  GLProgram name(vertex, fragment); \
87  ASSERT_NE(0u, name.get());
88 
89 #define ANGLE_GL_COMPUTE_PROGRAM(name, compute) \
90  GLProgram name(compute); \
91  ASSERT_NE(0u, name.get());
92 
93 } // namespace angle
94 
95 #endif // ANGLE_TESTS_GL_RAII_H_
GLWrapper()
Definition: gl_raii.h:29
~GLWrapper()
Definition: gl_raii.h:30
void GL_APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
Definition: libGLESv2.cpp:264
void GL_APIENTRY glDeleteProgram(GLuint program)
Definition: libGLESv2.cpp:169
GLuint CompileComputeProgram(const std::string &csSource, bool outputErrorMessages)
Definition: shader_utils.cpp:175
GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
Definition: shader_utils.cpp:157
Definition: gl_raii.h:26
Definition: Platform.h:33
unsigned int GLuint
Definition: gl2.h:70
Definition: gl_raii.h:50
decltype(glGenBuffers) GLGen
Definition: gl_raii.h:22
GLProgram(const std::string &vertexShader, const std::string &fragmentShader)
Definition: gl_raii.h:53
GLsizei const GLchar *const * string
Definition: gl2.h:479
~GLProgram()
Definition: gl_raii.h:60
GLProgram(const std::string &computeShader)
Definition: gl_raii.h:58
decltype(glDeleteBuffers) GLDelete
Definition: gl_raii.h:23
void GL_APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
Definition: libGLESv2.cpp:159