summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/tests/test_utils/gl_raii.h
blob: 9adc0cf02362187475a6ec8cb7ab4667bc83fa68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// gl_raii:
//   Helper methods for containing GL objects like buffers and textures.

#ifndef ANGLE_TESTS_GL_RAII_H_
#define ANGLE_TESTS_GL_RAII_H_

#include <functional>

#include "angle_gl.h"

namespace angle
{

// This is a bit of hack to work around a bug in MSVS intellisense, and make it very easy to
// use the correct function pointer type without worrying about the various definitions of
// GL_APICALL.
using GLGen    = decltype(glGenBuffers);
using GLDelete = decltype(glDeleteBuffers);

template <GLGen GenF, GLDelete DeleteF>
class GLWrapper
{
  public:
    GLWrapper() {}
    ~GLWrapper() { DeleteF(1, &mHandle); }

    GLuint get()
    {
        if (!mHandle)
        {
            GenF(1, &mHandle);
        }
        return mHandle;
    }

  private:
    GLuint mHandle = 0;
};

using GLBuffer       = GLWrapper<glGenBuffers, glDeleteBuffers>;
using GLTexture      = GLWrapper<glGenTextures, glDeleteTextures>;
using GLFramebuffer  = GLWrapper<glGenFramebuffers, glDeleteFramebuffers>;
using GLRenderbuffer = GLWrapper<glGenRenderbuffers, glDeleteRenderbuffers>;

class GLProgram
{
  public:
    GLProgram(const std::string &vertexShader, const std::string &fragmentShader)
        : mHandle(0), mVertexShader(vertexShader), mFragmentShader(fragmentShader)
    {
    }

    GLProgram(const std::string &computeShader) : mHandle(0), mComputeShader(computeShader) {}

    ~GLProgram() { glDeleteProgram(mHandle); }

    GLuint get()
    {
        if (mHandle == 0)
        {
            if (!mComputeShader.empty())
            {
                mHandle = CompileComputeProgram(mComputeShader);
            }
            else
            {
                mHandle = CompileProgram(mVertexShader, mFragmentShader);
            }
        }
        return mHandle;
    }

  private:
    GLuint mHandle;
    const std::string mVertexShader;
    const std::string mFragmentShader;
    const std::string mComputeShader;
};

#define ANGLE_GL_PROGRAM(name, vertex, fragment) \
    GLProgram name(vertex, fragment);            \
    ASSERT_NE(0u, name.get());

#define ANGLE_GL_COMPUTE_PROGRAM(name, compute) \
    GLProgram name(compute);                    \
    ASSERT_NE(0u, name.get());

}  // namespace angle

#endif  // ANGLE_TESTS_GL_RAII_H_